2 * Copyright 2008, Network Appliance Inc.
3 * Author: Jason McMullan <mcmullan <at> netapp.com>
4 * Licensed under the GPL-2 or later.
11 #include "spi_flash_internal.h"
13 /* M25Pxx-specific commands */
14 #define CMD_W25_WREN 0x06 /* Write Enable */
15 #define CMD_W25_WRDI 0x04 /* Write Disable */
16 #define CMD_W25_RDSR 0x05 /* Read Status Register */
17 #define CMD_W25_WRSR 0x01 /* Write Status Register */
18 #define CMD_W25_READ 0x03 /* Read Data Bytes */
19 #define CMD_W25_FAST_READ 0x0b /* Read Data Bytes at Higher Speed */
20 #define CMD_W25_PP 0x02 /* Page Program */
21 #define CMD_W25_SE 0x20 /* Sector (4K) Erase */
22 #define CMD_W25_BE 0xd8 /* Block (64K) Erase */
23 #define CMD_W25_CE 0xc7 /* Chip Erase */
24 #define CMD_W25_DP 0xb9 /* Deep Power-down */
25 #define CMD_W25_RES 0xab /* Release from DP, and Read Signature */
27 #define WINBOND_ID_W25X16 0x3015
28 #define WINBOND_ID_W25X32 0x3016
29 #define WINBOND_ID_W25X64 0x3017
30 #define WINBOND_ID_W25Q64 0x4017
32 #define WINBOND_SR_WIP (1 << 0) /* Write-in-Progress */
34 struct winbond_spi_flash_params {
36 /* Log2 of page size in power-of-two mode */
38 uint16_t pages_per_sector;
39 uint16_t sectors_per_block;
44 /* spi_flash needs to be first so upper layers can free() it */
45 struct winbond_spi_flash {
46 struct spi_flash flash;
47 const struct winbond_spi_flash_params *params;
50 static inline struct winbond_spi_flash *
51 to_winbond_spi_flash(struct spi_flash *flash)
53 return container_of(flash, struct winbond_spi_flash, flash);
56 static const struct winbond_spi_flash_params winbond_spi_flash_table[] = {
58 .id = WINBOND_ID_W25X16,
60 .pages_per_sector = 16,
61 .sectors_per_block = 16,
66 .id = WINBOND_ID_W25X32,
68 .pages_per_sector = 16,
69 .sectors_per_block = 16,
74 .id = WINBOND_ID_W25X64,
76 .pages_per_sector = 16,
77 .sectors_per_block = 16,
82 .id = WINBOND_ID_W25Q64,
84 .pages_per_sector = 16,
85 .sectors_per_block = 16,
91 static int winbond_wait_ready(struct spi_flash *flash, unsigned long timeout)
93 struct spi_slave *spi = flash->spi;
94 unsigned long timebase;
97 u8 cmd[4] = { CMD_W25_RDSR, 0xff, 0xff, 0xff };
99 ret = spi_xfer(spi, 32, &cmd[0], NULL, SPI_XFER_BEGIN);
101 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
105 timebase = get_timer(0);
107 ret = spi_xfer(spi, 8, NULL, &status, 0);
109 debug("SF: Failed to get status for cmd %02x: %d\n", cmd, ret);
113 if ((status & WINBOND_SR_WIP) == 0)
116 } while (get_timer(timebase) < timeout);
118 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
120 if ((status & WINBOND_SR_WIP) == 0)
123 debug("SF: Timed out on command %02x: %d\n", cmd, ret);
129 * Assemble the address part of a command for Winbond devices in
130 * non-power-of-two page size mode.
132 static void winbond_build_address(struct winbond_spi_flash *stm, u8 *cmd, u32 offset)
134 unsigned long page_addr;
135 unsigned long byte_addr;
136 unsigned long page_size;
137 unsigned int page_shift;
140 * The "extra" space per page is the power-of-two page size
143 page_shift = stm->params->l2_page_size;
144 page_size = (1 << page_shift);
145 page_addr = offset / page_size;
146 byte_addr = offset % page_size;
148 cmd[0] = page_addr >> (16 - page_shift);
149 cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
153 static int winbond_read_fast(struct spi_flash *flash,
154 u32 offset, size_t len, void *buf)
156 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
159 cmd[0] = CMD_READ_ARRAY_FAST;
160 winbond_build_address(stm, cmd + 1, offset);
163 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
166 static int winbond_write(struct spi_flash *flash,
167 u32 offset, size_t len, const void *buf)
169 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
170 unsigned long page_addr;
171 unsigned long byte_addr;
172 unsigned long page_size;
173 unsigned int page_shift;
179 page_shift = stm->params->l2_page_size;
180 page_size = (1 << page_shift);
181 page_addr = offset / page_size;
182 byte_addr = offset % page_size;
184 ret = spi_claim_bus(flash->spi);
186 debug("SF: Unable to claim SPI bus\n");
190 for (actual = 0; actual < len; actual += chunk_len) {
191 chunk_len = min(len - actual, page_size - byte_addr);
194 cmd[1] = page_addr >> (16 - page_shift);
195 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
197 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
199 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
201 ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
203 debug("SF: Enabling Write failed\n");
207 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
208 buf + actual, chunk_len);
210 debug("SF: Winbond Page Program failed\n");
214 ret = winbond_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
216 debug("SF: Winbond page programming timed out\n");
224 debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n",
229 spi_release_bus(flash->spi);
233 int winbond_erase(struct spi_flash *flash, u32 offset, size_t len)
235 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
236 unsigned long sector_size;
237 unsigned int page_shift;
243 * This function currently uses sector erase only.
244 * probably speed things up by using bulk erase
248 page_shift = stm->params->l2_page_size;
249 sector_size = (1 << page_shift) * stm->params->pages_per_sector;
251 if (offset % sector_size || len % sector_size) {
252 debug("SF: Erase offset/length not multiple of sector size\n");
259 ret = spi_claim_bus(flash->spi);
261 debug("SF: Unable to claim SPI bus\n");
265 for (actual = 0; actual < len; actual++) {
266 winbond_build_address(stm, &cmd[1], offset + actual * sector_size);
267 printf("Erase: %02x %02x %02x %02x\n",
268 cmd[0], cmd[1], cmd[2], cmd[3]);
270 ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
272 debug("SF: Enabling Write failed\n");
276 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
278 debug("SF: Winbond sector erase failed\n");
282 ret = winbond_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
284 debug("SF: Winbond sector erase timed out\n");
289 debug("SF: Winbond: Successfully erased %u bytes @ 0x%x\n",
290 len * sector_size, offset);
294 spi_release_bus(flash->spi);
298 struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
300 const struct winbond_spi_flash_params *params;
302 struct winbond_spi_flash *stm;
305 for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
306 params = &winbond_spi_flash_table[i];
307 if (params->id == ((idcode[1] << 8) | idcode[2]))
311 if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
312 debug("SF: Unsupported Winbond ID %02x%02x\n",
313 idcode[1], idcode[2]);
317 stm = malloc(sizeof(struct winbond_spi_flash));
319 debug("SF: Failed to allocate memory\n");
323 stm->params = params;
324 stm->flash.spi = spi;
325 stm->flash.name = params->name;
327 /* Assuming power-of-two page size initially. */
328 page_size = 1 << params->l2_page_size;
330 stm->flash.write = winbond_write;
331 stm->flash.erase = winbond_erase;
332 stm->flash.read = winbond_read_fast;
333 stm->flash.size = page_size * params->pages_per_sector
334 * params->sectors_per_block
337 printf("SF: Detected %s with page size %u, total ",
338 params->name, page_size);
339 print_size(stm->flash.size, "\n");