blob: e88802f0e67bdf5a7d62420fcdfa8bbf2222d16f [file] [log] [blame]
Jason McMullan64e5f3a2009-10-09 17:12:23 -04001/*
2 * Copyright 2008, Network Appliance Inc.
3 * Author: Jason McMullan <mcmullan <at> netapp.com>
4 * Licensed under the GPL-2 or later.
5 */
6
7#include <common.h>
8#include <malloc.h>
9#include <spi_flash.h>
10
11#include "spi_flash_internal.h"
12
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 */
26
Jason McMullan64e5f3a2009-10-09 17:12:23 -040027struct winbond_spi_flash_params {
28 uint16_t id;
29 /* Log2 of page size in power-of-two mode */
30 uint8_t l2_page_size;
31 uint16_t pages_per_sector;
32 uint16_t sectors_per_block;
Wojtek Skulskib1637e52010-12-07 01:07:45 -050033 uint16_t nr_blocks;
Jason McMullan64e5f3a2009-10-09 17:12:23 -040034 const char *name;
35};
36
37/* spi_flash needs to be first so upper layers can free() it */
38struct winbond_spi_flash {
39 struct spi_flash flash;
40 const struct winbond_spi_flash_params *params;
41};
42
43static inline struct winbond_spi_flash *
44to_winbond_spi_flash(struct spi_flash *flash)
45{
46 return container_of(flash, struct winbond_spi_flash, flash);
47}
48
49static const struct winbond_spi_flash_params winbond_spi_flash_table[] = {
50 {
Wojtek Skulskib1637e52010-12-07 01:07:45 -050051 .id = 0x3015,
Jason McMullan64e5f3a2009-10-09 17:12:23 -040052 .l2_page_size = 8,
53 .pages_per_sector = 16,
54 .sectors_per_block = 16,
55 .nr_blocks = 32,
56 .name = "W25X16",
57 },
58 {
Wojtek Skulskib1637e52010-12-07 01:07:45 -050059 .id = 0x3016,
Jason McMullan64e5f3a2009-10-09 17:12:23 -040060 .l2_page_size = 8,
61 .pages_per_sector = 16,
62 .sectors_per_block = 16,
63 .nr_blocks = 64,
64 .name = "W25X32",
65 },
66 {
Wojtek Skulskib1637e52010-12-07 01:07:45 -050067 .id = 0x3017,
Jason McMullan64e5f3a2009-10-09 17:12:23 -040068 .l2_page_size = 8,
69 .pages_per_sector = 16,
70 .sectors_per_block = 16,
71 .nr_blocks = 128,
72 .name = "W25X64",
73 },
Graeme Smechera448bce2010-07-29 09:00:02 -040074 {
Wojtek Skulskib1637e52010-12-07 01:07:45 -050075 .id = 0x4015,
76 .l2_page_size = 8,
77 .pages_per_sector = 16,
78 .sectors_per_block = 16,
79 .nr_blocks = 32,
80 .name = "W25Q16",
81 },
82 {
83 .id = 0x4016,
84 .l2_page_size = 8,
85 .pages_per_sector = 16,
86 .sectors_per_block = 16,
87 .nr_blocks = 64,
88 .name = "W25Q32",
89 },
90 {
91 .id = 0x4017,
Graeme Smechera448bce2010-07-29 09:00:02 -040092 .l2_page_size = 8,
93 .pages_per_sector = 16,
94 .sectors_per_block = 16,
95 .nr_blocks = 128,
96 .name = "W25Q64",
97 },
Wojtek Skulskib1637e52010-12-07 01:07:45 -050098 {
99 .id = 0x4018,
100 .l2_page_size = 8,
101 .pages_per_sector = 16,
102 .sectors_per_block = 16,
103 .nr_blocks = 256,
104 .name = "W25Q128",
105 },
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400106};
107
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400108static int winbond_write(struct spi_flash *flash,
109 u32 offset, size_t len, const void *buf)
110{
111 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
112 unsigned long page_addr;
113 unsigned long byte_addr;
114 unsigned long page_size;
115 unsigned int page_shift;
116 size_t chunk_len;
117 size_t actual;
118 int ret;
119 u8 cmd[4];
120
121 page_shift = stm->params->l2_page_size;
122 page_size = (1 << page_shift);
123 page_addr = offset / page_size;
124 byte_addr = offset % page_size;
125
126 ret = spi_claim_bus(flash->spi);
127 if (ret) {
128 debug("SF: Unable to claim SPI bus\n");
129 return ret;
130 }
131
132 for (actual = 0; actual < len; actual += chunk_len) {
133 chunk_len = min(len - actual, page_size - byte_addr);
134
135 cmd[0] = CMD_W25_PP;
136 cmd[1] = page_addr >> (16 - page_shift);
137 cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8);
138 cmd[3] = byte_addr;
139 debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n",
140 buf + actual,
141 cmd[0], cmd[1], cmd[2], cmd[3], chunk_len);
142
143 ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0);
144 if (ret < 0) {
145 debug("SF: Enabling Write failed\n");
146 goto out;
147 }
148
149 ret = spi_flash_cmd_write(flash->spi, cmd, 4,
150 buf + actual, chunk_len);
151 if (ret < 0) {
152 debug("SF: Winbond Page Program failed\n");
153 goto out;
154 }
155
Mike Frysinger37e13bc2011-01-10 02:20:12 -0500156 ret = spi_flash_cmd_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT);
157 if (ret)
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400158 goto out;
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400159
160 page_addr++;
161 byte_addr = 0;
162 }
163
164 debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n",
165 len, offset);
166 ret = 0;
167
168out:
169 spi_release_bus(flash->spi);
170 return ret;
171}
172
173int winbond_erase(struct spi_flash *flash, u32 offset, size_t len)
174{
175 struct winbond_spi_flash *stm = to_winbond_spi_flash(flash);
Mike Frysinger53421bb2011-01-10 02:20:13 -0500176 return spi_flash_cmd_erase(flash, CMD_W25_SE,
177 (1 << stm->params->l2_page_size) * stm->params->pages_per_sector,
178 offset, len);
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400179}
180
181struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode)
182{
183 const struct winbond_spi_flash_params *params;
Mike Frysinger02110b52010-04-29 00:35:12 -0400184 unsigned page_size;
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400185 struct winbond_spi_flash *stm;
186 unsigned int i;
187
188 for (i = 0; i < ARRAY_SIZE(winbond_spi_flash_table); i++) {
189 params = &winbond_spi_flash_table[i];
190 if (params->id == ((idcode[1] << 8) | idcode[2]))
191 break;
192 }
193
194 if (i == ARRAY_SIZE(winbond_spi_flash_table)) {
195 debug("SF: Unsupported Winbond ID %02x%02x\n",
196 idcode[1], idcode[2]);
197 return NULL;
198 }
199
200 stm = malloc(sizeof(struct winbond_spi_flash));
201 if (!stm) {
202 debug("SF: Failed to allocate memory\n");
203 return NULL;
204 }
205
206 stm->params = params;
207 stm->flash.spi = spi;
208 stm->flash.name = params->name;
209
210 /* Assuming power-of-two page size initially. */
211 page_size = 1 << params->l2_page_size;
212
213 stm->flash.write = winbond_write;
214 stm->flash.erase = winbond_erase;
Mike Frysinger373e7d62011-01-10 02:20:14 -0500215 stm->flash.read = spi_flash_cmd_read_fast;
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400216 stm->flash.size = page_size * params->pages_per_sector
217 * params->sectors_per_block
218 * params->nr_blocks;
219
Mike Frysinger02110b52010-04-29 00:35:12 -0400220 printf("SF: Detected %s with page size %u, total ",
221 params->name, page_size);
222 print_size(stm->flash.size, "\n");
Jason McMullan64e5f3a2009-10-09 17:12:23 -0400223
224 return &stm->flash;
225}