2 * (C) Copyright 2010, ucRobotics Inc.
3 * Author: Chong Huang <chuang@ucrobotics.com>
4 * Licensed under the GPL-2 or later.
11 #include "spi_flash_internal.h"
13 /* EN25Q128-specific commands */
14 #define CMD_EN25Q128_WREN 0x06 /* Write Enable */
15 #define CMD_EN25Q128_WRDI 0x04 /* Write Disable */
16 #define CMD_EN25Q128_RDSR 0x05 /* Read Status Register */
17 #define CMD_EN25Q128_WRSR 0x01 /* Write Status Register */
18 #define CMD_EN25Q128_READ 0x03 /* Read Data Bytes */
19 #define CMD_EN25Q128_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
20 #define CMD_EN25Q128_PP 0x02 /* Page Program */
21 #define CMD_EN25Q128_SE 0x20 /* Sector Erase */
22 #define CMD_EN25Q128_BE 0xd8 /* Block Erase */
23 #define CMD_EN25Q128_DP 0xb9 /* Deep Power-down */
24 #define CMD_EN25Q128_RES 0xab /* Release from DP, and Read Signature */
26 #define EON_ID_EN25Q128 0x18
28 #define EON_SR_WIP (1 << 0) /* Write-in-Progress */
30 struct eon_spi_flash_params {
34 u16 sectors_per_block;
39 /* spi_flash needs to be first so upper layers can free() it */
40 struct eon_spi_flash {
41 struct spi_flash flash;
42 const struct eon_spi_flash_params *params;
45 static inline struct eon_spi_flash *to_eon_spi_flash(struct spi_flash *flash)
47 return container_of(flash, struct eon_spi_flash, flash);
50 static const struct eon_spi_flash_params eon_spi_flash_table[] = {
52 .idcode1 = EON_ID_EN25Q128,
54 .pages_per_sector = 16,
55 .sectors_per_block = 16,
61 static int eon_wait_ready(struct spi_flash *flash, unsigned long timeout)
63 struct spi_slave *spi = flash->spi;
64 unsigned long timebase;
66 u8 cmd = CMD_EN25Q128_RDSR;
69 ret = spi_xfer(spi, 8, &cmd, NULL, SPI_XFER_BEGIN);
71 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
75 timebase = get_timer(0);
77 ret = spi_xfer(spi, 8, NULL, &status, 0);
81 if ((status & EON_SR_WIP) == 0)
84 } while (get_timer(timebase) < timeout);
86 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
88 if ((status & EON_SR_WIP) == 0)
95 static int eon_read_fast(struct spi_flash *flash,
96 u32 offset, size_t len, void *buf)
98 struct eon_spi_flash *eon = to_eon_spi_flash(flash);
99 unsigned long page_addr;
100 unsigned long page_size;
103 page_size = eon->params->page_size;
104 page_addr = offset / page_size;
106 cmd[0] = CMD_READ_ARRAY_FAST;
107 cmd[1] = page_addr >> 8;
109 cmd[3] = offset % page_size;
112 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
115 static int eon_write(struct spi_flash *flash,
116 u32 offset, size_t len, const void *buf)
118 struct eon_spi_flash *eon = to_eon_spi_flash(flash);
119 unsigned long page_addr;
120 unsigned long byte_addr;
121 unsigned long page_size;
127 page_size = eon->params->page_size;
128 page_addr = offset / page_size;
129 byte_addr = offset % page_size;
131 ret = spi_claim_bus(flash->spi);
133 debug("SF: Unable to claim SPI bus\n");
138 for (actual = 0; actual < len; actual += chunk_len) {
139 chunk_len = min(len - actual, page_size - byte_addr);
141 cmd[0] = CMD_EN25Q128_PP;
142 cmd[1] = page_addr >> 8;
147 ("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
148 buf + actual, cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
150 ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0);
152 debug("SF: Enabling Write failed\n");
156 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
157 buf + actual, chunk_len);
159 debug("SF: EON Page Program failed\n");
163 ret = eon_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
165 debug("SF: EON page programming timed out\n");
173 debug("SF: EON: Successfully programmed %u bytes @ 0x%x\n",
176 spi_release_bus(flash->spi);
180 int eon_erase(struct spi_flash *flash, u32 offset, size_t len)
183 struct eon_spi_flash *eon = to_eon_spi_flash(flash);
184 unsigned long block_size;
190 block_size = eon->params->page_size * eon->params->pages_per_sector
191 * eon->params->sectors_per_block;
193 if (offset % block_size || len % block_size) {
194 debug("SF: Erase offset/length not multiple of block size\n");
199 cmd[0] = CMD_EN25Q128_BE;
203 ret = spi_claim_bus(flash->spi);
205 debug("SF: Unable to claim SPI bus\n");
210 for (actual = 0; actual < len; actual++) {
211 cmd[1] = (offset / block_size) + actual;
212 ret = spi_flash_cmd(flash->spi, CMD_EN25Q128_WREN, NULL, 0);
214 debug("SF: Enabling Write failed\n");
218 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
220 debug("SF: EON page erase failed\n");
224 ret = eon_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
226 debug("SF: EON page erase timed out\n");
231 debug("SF: EON: Successfully erased %u bytes @ 0x%x\n",
232 len * block_size, offset);
234 spi_release_bus(flash->spi);
238 struct spi_flash *spi_flash_probe_eon(struct spi_slave *spi, u8 *idcode)
240 const struct eon_spi_flash_params *params;
241 struct eon_spi_flash *eon;
244 for (i = 0; i < ARRAY_SIZE(eon_spi_flash_table); ++i) {
245 params = &eon_spi_flash_table[i];
246 if (params->idcode1 == idcode[2])
250 if (i == ARRAY_SIZE(eon_spi_flash_table)) {
251 debug("SF: Unsupported EON ID %02x\n", idcode[1]);
255 eon = malloc(sizeof(*eon));
257 debug("SF: Failed to allocate memory\n");
261 eon->params = params;
262 eon->flash.spi = spi;
263 eon->flash.name = params->name;
265 eon->flash.write = eon_write;
266 eon->flash.erase = eon_erase;
267 eon->flash.read = eon_read_fast;
268 eon->flash.size = params->page_size * params->pages_per_sector
269 * params->nr_sectors;
271 debug("SF: Detected %s with page size %u, total %u bytes\n",
272 params->name, params->page_size, eon->flash.size);