blob: 69869e9ff0837bafc76be651b8492d74280ca4ec [file] [log] [blame]
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -07001/*
2 * (C) Copyright 2010, ucRobotics Inc.
3 * Author: Chong Huang <chuang@ucrobotics.com>
4 * Licensed under the GPL-2 or later.
5 */
6
7#include <stdlib.h>
8#include <spi_flash.h>
Edward O'Callaghanc4561e22014-06-26 15:02:40 +10009
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070010#include "spi_flash_internal.h"
11
12/* EN25Q128-specific commands */
13#define CMD_EN25Q128_WREN 0x06 /* Write Enable */
14#define CMD_EN25Q128_WRDI 0x04 /* Write Disable */
15#define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */
16#define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */
17#define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */
18#define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
19#define CMD_EN25Q128_PP 0x02 /* Page Program */
20#define CMD_EN25Q128_SE 0x20 /* Sector Erase */
21#define CMD_EN25Q128_BE 0xd8 /* Block Erase */
22#define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */
23#define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */
24
Vladimir Serbinenko066dcec2014-01-05 06:46:19 +010025#define EON_ID_EN25Q64 0x17
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070026#define EON_ID_EN25Q128 0x18
27
28struct eon_spi_flash_params {
29 u8 idcode1;
30 u16 page_size;
31 u16 pages_per_sector;
32 u16 sectors_per_block;
33 u16 nr_sectors;
34 const char *name;
35};
36
37/* spi_flash needs to be first so upper layers can free() it */
38struct eon_spi_flash {
39 struct spi_flash flash;
40 const struct eon_spi_flash_params *params;
41};
42
43static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash)
44{
45 return container_of(flash, struct eon_spi_flash, flash);
46}
47
48static const struct eon_spi_flash_params eon_spi_flash_table[] = {
49 {
50 .idcode1 = EON_ID_EN25Q128,
51 .page_size = 256,
52 .pages_per_sector = 16,
53 .sectors_per_block = 16,
54 .nr_sectors = 4096,
55 .name = "EN25Q128",
56 },
Vladimir Serbinenko066dcec2014-01-05 06:46:19 +010057 {
58 .idcode1 = EON_ID_EN25Q64,
59 .page_size = 256,
60 .pages_per_sector = 16,
61 .sectors_per_block = 16,
62 .nr_sectors = 2048,
63 .name = "EN25Q64",
64 },
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070065};
66
67static int eon_write(struct spi_flash *flash,
68 u32 offset, size_t len, const void *buf)
69{
70 struct eon_spi_flash *eon = to_eon_spi_flash(flash);
71 unsigned long page_addr;
72 unsigned long byte_addr;
73 unsigned long page_size;
74 size_t chunk_len;
75 size_t actual;
David Hendricks032c8432014-04-11 19:48:55 -070076 int ret = 0;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070077 u8 cmd[4];
78
79 page_size = eon->params->page_size;
80 page_addr = offset / page_size;
81 byte_addr = offset % page_size;
82
Martin Roth3316cf22012-12-05 16:22:54 -070083 flash->spi->rw = SPI_WRITE_FLAG;
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070084
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070085 for (actual = 0; actual < len; actual += chunk_len) {
86 chunk_len = min(len - actual, page_size - byte_addr);
87
88 cmd[0] = CMD_EN25Q128_PP;
89 cmd[1] = page_addr >> 8;
90 cmd[2] = page_addr;
91 cmd[3] = byte_addr;
92
Stefan Reinauer5649b082012-05-23 11:03:29 -070093#if CONFIG_DEBUG_SPI_FLASH
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070094 printk(BIOS_SPEW,
Stefan Reinauer8ea5a342012-05-14 13:52:32 -070095 "PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %zu\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070096 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
Stefan Reinauer5649b082012-05-23 11:03:29 -070097#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -070098
99 ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0);
100 if (ret < 0) {
101 printk(BIOS_WARNING, "SF: Enabling Write failed\n");
102 break;
103 }
104
105 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
106 buf + actual, chunk_len);
107 if (ret < 0) {
108 printk(BIOS_WARNING, "SF: EON Page Program failed\n");
109 break;
110 }
111
112 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
113 if (ret)
114 break;
115
116 page_addr++;
117 byte_addr = 0;
118 }
119
Marc Jones747127d2012-12-03 22:16:29 -0700120#if CONFIG_DEBUG_SPI_FLASH
121 printk(BIOS_SPEW, "SF: EON: Successfully programmed %zu bytes @ 0x%x\n",
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700122 len, offset);
Marc Jones747127d2012-12-03 22:16:29 -0700123#endif
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700124
Stefan Reinauer1c56d9b2012-05-10 11:27:32 -0700125 return ret;
126}
127
128static int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
129{
130 return spi_flash_cmd_erase(flash, CMD_EN25Q128_BE, offset, len);
131}
132
133struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
134{
135 const struct eon_spi_flash_params *params;
136 struct eon_spi_flash *eon;
137 unsigned int i;
138
139 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
140 params = &eon_spi_flash_table[i];
141 if (params->idcode1 == idcode[2])
142 break;
143 }
144
145 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
146 printk(BIOS_WARNING, "SF: Unsupported EON ID %02x\n", idcode[1]);
147 return NULL;
148 }
149
150 eon = malloc(sizeof(*eon));
151 if (!eon) {
152 printk(BIOS_WARNING, "SF: Failed to allocate memory\n");
153 return NULL;
154 }
155
156 eon->params = params;
157 eon->flash.spi = spi;
158 eon->flash.name = params->name;
159
160 eon->flash.write = eon_write;
161 eon->flash.erase = eon_erase;
162 eon->flash.read = spi_flash_cmd_read_fast;
163 eon->flash.sector_size = params->page_size * params->pages_per_sector
164 * params->sectors_per_block;
165 eon->flash.size = params->page_size * params->pages_per_sector
166 * params->nr_sectors;
167
168 return &eon->flash;
169}