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_SR_WIP (1 << 0) /* Write-in-Progress */
29 struct winbond_spi_flash_params {
31 /* Log2 of page size in power-of-two mode */
33 uint16_t pages_per_sector;
34 uint16_t sectors_per_block;
39 /* spi_flash needs to be first so upper layers can free() it */
40 struct winbond_spi_flash {
41 struct spi_flash flash;
42 const struct winbond_spi_flash_params *params;
45 static inline struct winbond_spi_flash *
46 to_winbond_spi_flash(struct spi_flash *flash)
48 return container_of(flash, struct winbond_spi_flash, flash);
51 static const struct winbond_spi_flash_params winbond_spi_flash_table[] = {
55 .pages_per_sector = 16,
56 .sectors_per_block = 16,
63 .pages_per_sector = 16,
64 .sectors_per_block = 16,
71 .pages_per_sector = 16,
72 .sectors_per_block = 16,
79 .pages_per_sector = 16,
80 .sectors_per_block = 16,
87 .pages_per_sector = 16,
88 .sectors_per_block = 16,
95 .pages_per_sector = 16,
96 .sectors_per_block = 16,
103 .pages_per_sector = 16,
104 .sectors_per_block = 16,
110 static int winbond_wait_ready(struct spi_flash *flash, unsigned long timeout)
112 struct spi_slave *spi = flash->spi;
113 unsigned long timebase;
116 u8 cmd[4] = { CMD_W25_RDSR, 0xff, 0xff, 0xff };
118 ret = spi_xfer(spi, 32, &cmd[0], NULL, SPI_XFER_BEGIN);
120 debug("SF: Failed to send command %02x: %d\n", cmd, ret);
124 timebase = get_timer(0);
126 ret = spi_xfer(spi, 8, NULL, &status, 0);
128 debug("SF: Failed to get status for cmd %02x: %d\n", cmd, ret);
132 if ((status & WINBOND_SR_WIP) == 0)
135 } while (get_timer(timebase) < timeout);
137 spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END);
139 if ((status & WINBOND_SR_WIP) == 0)
142 debug("SF: Timed out on command %02x: %d\n", cmd, ret);
148 * Assemble the address part of a command for Winbond devices in
149 * non-power-of-two page size mode.
151 static void winbond_build_address(struct winbond_spi_flash *stm, u8 *cmd, u32 offset)
153 unsigned long page_addr;
154 unsigned long byte_addr;
155 unsigned long page_size;
156 unsigned int page_shift;
159 * The "extra" space per page is the power-of-two page size
162 page_shift = stm->params->l2_page_size;
163 page_size = (1 << page_shift);
164 page_addr = offset / page_size;
165 byte_addr = offset % page_size;
167 cmd[0] = page_addr >> (16 - page_shift);
168 cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8);
172 static int winbond_read_fast(struct spi_flash *flash,
173 u32 offset, size_t len, void *buf)
175 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
178 cmd[0] = CMD_READ_ARRAY_FAST;
179 winbond_build_address(stm, cmd + 1, offset);
182 return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len);
185 static int winbond_write(struct spi_flash *flash,
186 u32 offset, size_t len, const void *buf)
188 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
189 unsigned long page_addr;
190 unsigned long byte_addr;
191 unsigned long page_size;
192 unsigned int page_shift;
198 page_shift = stm->params->l2_page_size;
199 page_size = (1 << page_shift);
200 page_addr = offset / page_size;
201 byte_addr = offset % page_size;
203 ret = spi_claim_bus(flash->spi);
205 debug("SF: Unable to claim SPI bus\n");
209 for (actual = 0; actual < len; actual += chunk_len) {
210 chunk_len = min(len - actual, page_size - byte_addr);
213 cmd[1] = page_addr >> (16 - page_shift);
214 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
216 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
218 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
220 ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
222 debug("SF: Enabling Write failed\n");
226 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
227 buf + actual, chunk_len);
229 debug("SF: Winbond Page Program failed\n");
233 ret = winbond_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
235 debug("SF: Winbond page programming timed out\n");
243 debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n",
248 spi_release_bus(flash->spi);
252 int winbond_erase(struct spi_flash *flash, u32 offset, size_t len)
254 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
255 unsigned long sector_size;
256 unsigned int page_shift;
262 * This function currently uses sector erase only.
263 * probably speed things up by using bulk erase
267 page_shift = stm->params->l2_page_size;
268 sector_size = (1 << page_shift) * stm->params->pages_per_sector;
270 if (offset % sector_size || len % sector_size) {
271 debug("SF: Erase offset/length not multiple of sector size\n");
278 ret = spi_claim_bus(flash->spi);
280 debug("SF: Unable to claim SPI bus\n");
284 for (actual = 0; actual < len; actual++) {
285 winbond_build_address(stm, &cmd[1], offset + actual * sector_size);
286 printf("Erase: %02x %02x %02x %02x\n",
287 cmd[0], cmd[1], cmd[2], cmd[3]);
289 ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
291 debug("SF: Enabling Write failed\n");
295 ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0);
297 debug("SF: Winbond sector erase failed\n");
301 ret = winbond_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT);
303 debug("SF: Winbond sector erase timed out\n");
308 debug("SF: Winbond: Successfully erased %u bytes @ 0x%x\n",
309 len * sector_size, offset);
313 spi_release_bus(flash->spi);
317 struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
319 const struct winbond_spi_flash_params *params;
321 struct winbond_spi_flash *stm;
324 for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
325 params = &winbond_spi_flash_table[i];
326 if (params->id == ((idcode[1] << 8) | idcode[2]))
330 if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
331 debug("SF: Unsupported Winbond ID %02x%02x\n",
332 idcode[1], idcode[2]);
336 stm = malloc(sizeof(struct winbond_spi_flash));
338 debug("SF: Failed to allocate memory\n");
342 stm->params = params;
343 stm->flash.spi = spi;
344 stm->flash.name = params->name;
346 /* Assuming power-of-two page size initially. */
347 page_size = 1 << params->l2_page_size;
349 stm->flash.write = winbond_write;
350 stm->flash.erase = winbond_erase;
351 stm->flash.read = winbond_read_fast;
352 stm->flash.size = page_size * params->pages_per_sector
353 * params->sectors_per_block
356 printf("SF: Detected %s with page size %u, total ",
357 params->name, page_size);
358 print_size(stm->flash.size, "\n");