]>
Commit | Line | Data |
---|---|---|
d394a779 JM |
1 | /* |
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 | ||
27 | #define WINBOND_ID_W25X16 0x3015 | |
28 | #define WINBOND_ID_W25X32 0x3016 | |
29 | #define WINBOND_ID_W25X64 0x3017 | |
74f9e0d8 | 30 | #define WINBOND_ID_W25Q64 0x4017 |
d394a779 JM |
31 | |
32 | #define WINBOND_SR_WIP (1 << 0) /* Write-in-Progress */ | |
33 | ||
34 | struct winbond_spi_flash_params { | |
35 | uint16_t id; | |
36 | /* Log2 of page size in power-of-two mode */ | |
37 | uint8_t l2_page_size; | |
38 | uint16_t pages_per_sector; | |
39 | uint16_t sectors_per_block; | |
40 | uint8_t nr_blocks; | |
41 | const char *name; | |
42 | }; | |
43 | ||
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; | |
48 | }; | |
49 | ||
50 | static inline struct winbond_spi_flash * | |
51 | to_winbond_spi_flash(struct spi_flash *flash) | |
52 | { | |
53 | return container_of(flash, struct winbond_spi_flash, flash); | |
54 | } | |
55 | ||
56 | static const struct winbond_spi_flash_params winbond_spi_flash_table[] = { | |
57 | { | |
58 | .id = WINBOND_ID_W25X16, | |
59 | .l2_page_size = 8, | |
60 | .pages_per_sector = 16, | |
61 | .sectors_per_block = 16, | |
62 | .nr_blocks = 32, | |
63 | .name = "W25X16", | |
64 | }, | |
65 | { | |
66 | .id = WINBOND_ID_W25X32, | |
67 | .l2_page_size = 8, | |
68 | .pages_per_sector = 16, | |
69 | .sectors_per_block = 16, | |
70 | .nr_blocks = 64, | |
71 | .name = "W25X32", | |
72 | }, | |
73 | { | |
74 | .id = WINBOND_ID_W25X64, | |
75 | .l2_page_size = 8, | |
76 | .pages_per_sector = 16, | |
77 | .sectors_per_block = 16, | |
78 | .nr_blocks = 128, | |
79 | .name = "W25X64", | |
80 | }, | |
74f9e0d8 GS |
81 | { |
82 | .id = WINBOND_ID_W25Q64, | |
83 | .l2_page_size = 8, | |
84 | .pages_per_sector = 16, | |
85 | .sectors_per_block = 16, | |
86 | .nr_blocks = 128, | |
87 | .name = "W25Q64", | |
88 | }, | |
d394a779 JM |
89 | }; |
90 | ||
91 | static int winbond_wait_ready(struct spi_flash *flash, unsigned long timeout) | |
92 | { | |
93 | struct spi_slave *spi = flash->spi; | |
94 | unsigned long timebase; | |
95 | int ret; | |
96 | u8 status; | |
97 | u8 cmd[4] = { CMD_W25_RDSR, 0xff, 0xff, 0xff }; | |
98 | ||
99 | ret = spi_xfer(spi, 32, &cmd[0], NULL, SPI_XFER_BEGIN); | |
100 | if (ret) { | |
101 | debug("SF: Failed to send command %02x: %d\n", cmd, ret); | |
102 | return ret; | |
103 | } | |
104 | ||
105 | timebase = get_timer(0); | |
106 | do { | |
107 | ret = spi_xfer(spi, 8, NULL, &status, 0); | |
108 | if (ret) { | |
109 | debug("SF: Failed to get status for cmd %02x: %d\n", cmd, ret); | |
110 | return -1; | |
111 | } | |
112 | ||
113 | if ((status & WINBOND_SR_WIP) == 0) | |
114 | break; | |
115 | ||
116 | } while (get_timer(timebase) < timeout); | |
117 | ||
118 | spi_xfer(spi, 0, NULL, NULL, SPI_XFER_END); | |
119 | ||
120 | if ((status & WINBOND_SR_WIP) == 0) | |
121 | return 0; | |
122 | ||
123 | debug("SF: Timed out on command %02x: %d\n", cmd, ret); | |
124 | /* Timed out */ | |
125 | return -1; | |
126 | } | |
127 | ||
128 | /* | |
129 | * Assemble the address part of a command for Winbond devices in | |
130 | * non-power-of-two page size mode. | |
131 | */ | |
132 | static void winbond_build_address(struct winbond_spi_flash *stm, u8 *cmd, u32 offset) | |
133 | { | |
134 | unsigned long page_addr; | |
135 | unsigned long byte_addr; | |
136 | unsigned long page_size; | |
137 | unsigned int page_shift; | |
138 | ||
139 | /* | |
140 | * The "extra" space per page is the power-of-two page size | |
141 | * divided by 32. | |
142 | */ | |
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; | |
147 | ||
148 | cmd[0] = page_addr >> (16 - page_shift); | |
149 | cmd[1] = page_addr << (page_shift - 8) | (byte_addr >> 8); | |
150 | cmd[2] = byte_addr; | |
151 | } | |
152 | ||
153 | static int winbond_read_fast(struct spi_flash *flash, | |
154 | u32 offset, size_t len, void *buf) | |
155 | { | |
156 | struct winbond_spi_flash *stm = to_winbond_spi_flash(flash); | |
157 | u8 cmd[5]; | |
158 | ||
159 | cmd[0] = CMD_READ_ARRAY_FAST; | |
160 | winbond_build_address(stm, cmd + 1, offset); | |
161 | cmd[4] = 0x00; | |
162 | ||
163 | return spi_flash_read_common(flash, cmd, sizeof(cmd), buf, len); | |
164 | } | |
165 | ||
166 | static int winbond_write(struct spi_flash *flash, | |
167 | u32 offset, size_t len, const void *buf) | |
168 | { | |
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; | |
174 | size_t chunk_len; | |
175 | size_t actual; | |
176 | int ret; | |
177 | u8 cmd[4]; | |
178 | ||
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; | |
183 | ||
184 | ret = spi_claim_bus(flash->spi); | |
185 | if (ret) { | |
186 | debug("SF: Unable to claim SPI bus\n"); | |
187 | return ret; | |
188 | } | |
189 | ||
190 | for (actual = 0; actual < len; actual += chunk_len) { | |
191 | chunk_len = min(len - actual, page_size - byte_addr); | |
192 | ||
193 | cmd[0] = CMD_W25_PP; | |
194 | cmd[1] = page_addr >> (16 - page_shift); | |
195 | cmd[2] = page_addr << (page_shift - 8) | (byte_addr >> 8); | |
196 | cmd[3] = byte_addr; | |
197 | debug("PP: 0x%p => cmd = { 0x%02x 0x%02x%02x%02x } chunk_len = %d\n", | |
198 | buf + actual, | |
199 | cmd[0], cmd[1], cmd[2], cmd[3], chunk_len); | |
200 | ||
201 | ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0); | |
202 | if (ret < 0) { | |
203 | debug("SF: Enabling Write failed\n"); | |
204 | goto out; | |
205 | } | |
206 | ||
207 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, | |
208 | buf + actual, chunk_len); | |
209 | if (ret < 0) { | |
210 | debug("SF: Winbond Page Program failed\n"); | |
211 | goto out; | |
212 | } | |
213 | ||
214 | ret = winbond_wait_ready(flash, SPI_FLASH_PROG_TIMEOUT); | |
215 | if (ret < 0) { | |
216 | debug("SF: Winbond page programming timed out\n"); | |
217 | goto out; | |
218 | } | |
219 | ||
220 | page_addr++; | |
221 | byte_addr = 0; | |
222 | } | |
223 | ||
224 | debug("SF: Winbond: Successfully programmed %u bytes @ 0x%x\n", | |
225 | len, offset); | |
226 | ret = 0; | |
227 | ||
228 | out: | |
229 | spi_release_bus(flash->spi); | |
230 | return ret; | |
231 | } | |
232 | ||
233 | int winbond_erase(struct spi_flash *flash, u32 offset, size_t len) | |
234 | { | |
235 | struct winbond_spi_flash *stm = to_winbond_spi_flash(flash); | |
236 | unsigned long sector_size; | |
237 | unsigned int page_shift; | |
238 | size_t actual; | |
239 | int ret; | |
240 | u8 cmd[4]; | |
241 | ||
242 | /* | |
243 | * This function currently uses sector erase only. | |
244 | * probably speed things up by using bulk erase | |
245 | * when possible. | |
246 | */ | |
247 | ||
248 | page_shift = stm->params->l2_page_size; | |
249 | sector_size = (1 << page_shift) * stm->params->pages_per_sector; | |
250 | ||
251 | if (offset % sector_size || len % sector_size) { | |
252 | debug("SF: Erase offset/length not multiple of sector size\n"); | |
253 | return -1; | |
254 | } | |
255 | ||
256 | len /= sector_size; | |
257 | cmd[0] = CMD_W25_SE; | |
258 | ||
259 | ret = spi_claim_bus(flash->spi); | |
260 | if (ret) { | |
261 | debug("SF: Unable to claim SPI bus\n"); | |
262 | return ret; | |
263 | } | |
264 | ||
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]); | |
269 | ||
270 | ret = spi_flash_cmd(flash->spi, CMD_W25_WREN, NULL, 0); | |
271 | if (ret < 0) { | |
272 | debug("SF: Enabling Write failed\n"); | |
273 | goto out; | |
274 | } | |
275 | ||
276 | ret = spi_flash_cmd_write(flash->spi, cmd, 4, NULL, 0); | |
277 | if (ret < 0) { | |
278 | debug("SF: Winbond sector erase failed\n"); | |
279 | goto out; | |
280 | } | |
281 | ||
282 | ret = winbond_wait_ready(flash, SPI_FLASH_PAGE_ERASE_TIMEOUT); | |
283 | if (ret < 0) { | |
284 | debug("SF: Winbond sector erase timed out\n"); | |
285 | goto out; | |
286 | } | |
287 | } | |
288 | ||
289 | debug("SF: Winbond: Successfully erased %u bytes @ 0x%x\n", | |
290 | len * sector_size, offset); | |
291 | ret = 0; | |
292 | ||
293 | out: | |
294 | spi_release_bus(flash->spi); | |
295 | return ret; | |
296 | } | |
297 | ||
298 | struct spi_flash *spi_flash_probe_winbond(struct spi_slave *spi, u8 *idcode) | |
299 | { | |
300 | const struct winbond_spi_flash_params *params; | |
b376bbb4 | 301 | unsigned page_size; |
d394a779 JM |
302 | struct winbond_spi_flash *stm; |
303 | unsigned int i; | |
304 | ||
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])) | |
308 | break; | |
309 | } | |
310 | ||
311 | if (i == ARRAY_SIZE(winbond_spi_flash_table)) { | |
312 | debug("SF: Unsupported Winbond ID %02x%02x\n", | |
313 | idcode[1], idcode[2]); | |
314 | return NULL; | |
315 | } | |
316 | ||
317 | stm = malloc(sizeof(struct winbond_spi_flash)); | |
318 | if (!stm) { | |
319 | debug("SF: Failed to allocate memory\n"); | |
320 | return NULL; | |
321 | } | |
322 | ||
323 | stm->params = params; | |
324 | stm->flash.spi = spi; | |
325 | stm->flash.name = params->name; | |
326 | ||
327 | /* Assuming power-of-two page size initially. */ | |
328 | page_size = 1 << params->l2_page_size; | |
329 | ||
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 | |
335 | * params->nr_blocks; | |
336 | ||
b376bbb4 MF |
337 | printf("SF: Detected %s with page size %u, total ", |
338 | params->name, page_size); | |
339 | print_size(stm->flash.size, "\n"); | |
d394a779 JM |
340 | |
341 | return &stm->flash; | |
342 | } |