]>
Commit | Line | Data |
---|---|---|
ec77e21b RM |
1 | /* |
2 | * sst25l.c | |
3 | * | |
4 | * Driver for SST25L SPI Flash chips | |
5 | * | |
6 | * Copyright © 2009 Bluewater Systems Ltd | |
7 | * Author: Andre Renaud <andre@bluewatersys.com> | |
8 | * Author: Ryan Mallon <ryan@bluewatersys.com> | |
9 | * | |
10 | * Based on m25p80.c | |
11 | * | |
12 | * This code is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License version 2 as | |
14 | * published by the Free Software Foundation. | |
15 | * | |
16 | */ | |
17 | ||
18 | #include <linux/init.h> | |
19 | #include <linux/module.h> | |
20 | #include <linux/device.h> | |
21 | #include <linux/mutex.h> | |
22 | #include <linux/interrupt.h> | |
5a0e3ad6 | 23 | #include <linux/slab.h> |
d43c36dc | 24 | #include <linux/sched.h> |
ec77e21b RM |
25 | |
26 | #include <linux/mtd/mtd.h> | |
27 | #include <linux/mtd/partitions.h> | |
28 | ||
29 | #include <linux/spi/spi.h> | |
30 | #include <linux/spi/flash.h> | |
31 | ||
32 | /* Erases can take up to 3 seconds! */ | |
33 | #define MAX_READY_WAIT_JIFFIES msecs_to_jiffies(3000) | |
34 | ||
35 | #define SST25L_CMD_WRSR 0x01 /* Write status register */ | |
36 | #define SST25L_CMD_WRDI 0x04 /* Write disable */ | |
37 | #define SST25L_CMD_RDSR 0x05 /* Read status register */ | |
38 | #define SST25L_CMD_WREN 0x06 /* Write enable */ | |
39 | #define SST25L_CMD_READ 0x03 /* High speed read */ | |
40 | ||
41 | #define SST25L_CMD_EWSR 0x50 /* Enable write status register */ | |
42 | #define SST25L_CMD_SECTOR_ERASE 0x20 /* Erase sector */ | |
43 | #define SST25L_CMD_READ_ID 0x90 /* Read device ID */ | |
44 | #define SST25L_CMD_AAI_PROGRAM 0xaf /* Auto address increment */ | |
45 | ||
46 | #define SST25L_STATUS_BUSY (1 << 0) /* Chip is busy */ | |
47 | #define SST25L_STATUS_WREN (1 << 1) /* Write enabled */ | |
48 | #define SST25L_STATUS_BP0 (1 << 2) /* Block protection 0 */ | |
49 | #define SST25L_STATUS_BP1 (1 << 3) /* Block protection 1 */ | |
50 | ||
51 | struct sst25l_flash { | |
52 | struct spi_device *spi; | |
53 | struct mutex lock; | |
54 | struct mtd_info mtd; | |
55 | ||
56 | int partitioned; | |
57 | }; | |
58 | ||
59 | struct flash_info { | |
60 | const char *name; | |
61 | uint16_t device_id; | |
62 | unsigned page_size; | |
63 | unsigned nr_pages; | |
64 | unsigned erase_size; | |
65 | }; | |
66 | ||
67 | #define to_sst25l_flash(x) container_of(x, struct sst25l_flash, mtd) | |
68 | ||
69 | static struct flash_info __initdata sst25l_flash_info[] = { | |
70 | {"sst25lf020a", 0xbf43, 256, 1024, 4096}, | |
71 | {"sst25lf040a", 0xbf44, 256, 2048, 4096}, | |
72 | }; | |
73 | ||
74 | static int sst25l_status(struct sst25l_flash *flash, int *status) | |
75 | { | |
76 | unsigned char command, response; | |
77 | int err; | |
78 | ||
79 | command = SST25L_CMD_RDSR; | |
80 | err = spi_write_then_read(flash->spi, &command, 1, &response, 1); | |
81 | if (err < 0) | |
82 | return err; | |
83 | ||
84 | *status = response; | |
85 | return 0; | |
86 | } | |
87 | ||
88 | static int sst25l_write_enable(struct sst25l_flash *flash, int enable) | |
89 | { | |
90 | unsigned char command[2]; | |
91 | int status, err; | |
92 | ||
93 | command[0] = enable ? SST25L_CMD_WREN : SST25L_CMD_WRDI; | |
94 | err = spi_write(flash->spi, command, 1); | |
95 | if (err) | |
96 | return err; | |
97 | ||
98 | command[0] = SST25L_CMD_EWSR; | |
99 | err = spi_write(flash->spi, command, 1); | |
100 | if (err) | |
101 | return err; | |
102 | ||
103 | command[0] = SST25L_CMD_WRSR; | |
104 | command[1] = enable ? 0 : SST25L_STATUS_BP0 | SST25L_STATUS_BP1; | |
105 | err = spi_write(flash->spi, command, 2); | |
106 | if (err) | |
107 | return err; | |
108 | ||
109 | if (enable) { | |
110 | err = sst25l_status(flash, &status); | |
111 | if (err) | |
112 | return err; | |
113 | if (!(status & SST25L_STATUS_WREN)) | |
114 | return -EROFS; | |
115 | } | |
116 | ||
117 | return 0; | |
118 | } | |
119 | ||
120 | static int sst25l_wait_till_ready(struct sst25l_flash *flash) | |
121 | { | |
122 | unsigned long deadline; | |
123 | int status, err; | |
124 | ||
125 | deadline = jiffies + MAX_READY_WAIT_JIFFIES; | |
126 | do { | |
127 | err = sst25l_status(flash, &status); | |
128 | if (err) | |
129 | return err; | |
130 | if (!(status & SST25L_STATUS_BUSY)) | |
131 | return 0; | |
132 | ||
133 | cond_resched(); | |
134 | } while (!time_after_eq(jiffies, deadline)); | |
135 | ||
136 | return -ETIMEDOUT; | |
137 | } | |
138 | ||
139 | static int sst25l_erase_sector(struct sst25l_flash *flash, uint32_t offset) | |
140 | { | |
141 | unsigned char command[4]; | |
142 | int err; | |
143 | ||
144 | err = sst25l_write_enable(flash, 1); | |
145 | if (err) | |
146 | return err; | |
147 | ||
148 | command[0] = SST25L_CMD_SECTOR_ERASE; | |
149 | command[1] = offset >> 16; | |
150 | command[2] = offset >> 8; | |
151 | command[3] = offset; | |
152 | err = spi_write(flash->spi, command, 4); | |
153 | if (err) | |
154 | return err; | |
155 | ||
156 | err = sst25l_wait_till_ready(flash); | |
157 | if (err) | |
158 | return err; | |
159 | ||
160 | return sst25l_write_enable(flash, 0); | |
161 | } | |
162 | ||
163 | static int sst25l_erase(struct mtd_info *mtd, struct erase_info *instr) | |
164 | { | |
165 | struct sst25l_flash *flash = to_sst25l_flash(mtd); | |
166 | uint32_t addr, end; | |
167 | int err; | |
168 | ||
169 | /* Sanity checks */ | |
170 | if (instr->addr + instr->len > flash->mtd.size) | |
171 | return -EINVAL; | |
172 | ||
173 | if ((uint32_t)instr->len % mtd->erasesize) | |
174 | return -EINVAL; | |
175 | ||
176 | if ((uint32_t)instr->addr % mtd->erasesize) | |
177 | return -EINVAL; | |
178 | ||
179 | addr = instr->addr; | |
180 | end = addr + instr->len; | |
181 | ||
182 | mutex_lock(&flash->lock); | |
183 | ||
184 | err = sst25l_wait_till_ready(flash); | |
2eaaa5ff JS |
185 | if (err) { |
186 | mutex_unlock(&flash->lock); | |
ec77e21b | 187 | return err; |
2eaaa5ff | 188 | } |
ec77e21b RM |
189 | |
190 | while (addr < end) { | |
191 | err = sst25l_erase_sector(flash, addr); | |
192 | if (err) { | |
193 | mutex_unlock(&flash->lock); | |
194 | instr->state = MTD_ERASE_FAILED; | |
195 | dev_err(&flash->spi->dev, "Erase failed\n"); | |
196 | return err; | |
197 | } | |
198 | ||
199 | addr += mtd->erasesize; | |
200 | } | |
201 | ||
202 | mutex_unlock(&flash->lock); | |
203 | ||
204 | instr->state = MTD_ERASE_DONE; | |
205 | mtd_erase_callback(instr); | |
206 | return 0; | |
207 | } | |
208 | ||
209 | static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len, | |
210 | size_t *retlen, unsigned char *buf) | |
211 | { | |
212 | struct sst25l_flash *flash = to_sst25l_flash(mtd); | |
213 | struct spi_transfer transfer[2]; | |
214 | struct spi_message message; | |
215 | unsigned char command[4]; | |
216 | int ret; | |
217 | ||
218 | /* Sanity checking */ | |
219 | if (len == 0) | |
220 | return 0; | |
221 | ||
222 | if (from + len > flash->mtd.size) | |
223 | return -EINVAL; | |
224 | ||
225 | if (retlen) | |
226 | *retlen = 0; | |
227 | ||
228 | spi_message_init(&message); | |
229 | memset(&transfer, 0, sizeof(transfer)); | |
230 | ||
231 | command[0] = SST25L_CMD_READ; | |
232 | command[1] = from >> 16; | |
233 | command[2] = from >> 8; | |
234 | command[3] = from; | |
235 | ||
236 | transfer[0].tx_buf = command; | |
237 | transfer[0].len = sizeof(command); | |
238 | spi_message_add_tail(&transfer[0], &message); | |
239 | ||
240 | transfer[1].rx_buf = buf; | |
241 | transfer[1].len = len; | |
242 | spi_message_add_tail(&transfer[1], &message); | |
243 | ||
244 | mutex_lock(&flash->lock); | |
245 | ||
246 | /* Wait for previous write/erase to complete */ | |
247 | ret = sst25l_wait_till_ready(flash); | |
248 | if (ret) { | |
249 | mutex_unlock(&flash->lock); | |
250 | return ret; | |
251 | } | |
252 | ||
253 | spi_sync(flash->spi, &message); | |
254 | ||
255 | if (retlen && message.actual_length > sizeof(command)) | |
256 | *retlen += message.actual_length - sizeof(command); | |
257 | ||
258 | mutex_unlock(&flash->lock); | |
259 | return 0; | |
260 | } | |
261 | ||
262 | static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len, | |
263 | size_t *retlen, const unsigned char *buf) | |
264 | { | |
265 | struct sst25l_flash *flash = to_sst25l_flash(mtd); | |
266 | int i, j, ret, bytes, copied = 0; | |
267 | unsigned char command[5]; | |
268 | ||
269 | /* Sanity checks */ | |
270 | if (!len) | |
271 | return 0; | |
272 | ||
273 | if (to + len > flash->mtd.size) | |
274 | return -EINVAL; | |
275 | ||
276 | if ((uint32_t)to % mtd->writesize) | |
277 | return -EINVAL; | |
278 | ||
279 | mutex_lock(&flash->lock); | |
280 | ||
281 | ret = sst25l_write_enable(flash, 1); | |
282 | if (ret) | |
283 | goto out; | |
284 | ||
285 | for (i = 0; i < len; i += mtd->writesize) { | |
286 | ret = sst25l_wait_till_ready(flash); | |
287 | if (ret) | |
288 | goto out; | |
289 | ||
290 | /* Write the first byte of the page */ | |
291 | command[0] = SST25L_CMD_AAI_PROGRAM; | |
292 | command[1] = (to + i) >> 16; | |
293 | command[2] = (to + i) >> 8; | |
294 | command[3] = (to + i); | |
295 | command[4] = buf[i]; | |
296 | ret = spi_write(flash->spi, command, 5); | |
297 | if (ret < 0) | |
298 | goto out; | |
299 | copied++; | |
300 | ||
301 | /* | |
302 | * Write the remaining bytes using auto address | |
303 | * increment mode | |
304 | */ | |
305 | bytes = min_t(uint32_t, mtd->writesize, len - i); | |
306 | for (j = 1; j < bytes; j++, copied++) { | |
307 | ret = sst25l_wait_till_ready(flash); | |
308 | if (ret) | |
309 | goto out; | |
310 | ||
311 | command[1] = buf[i + j]; | |
312 | ret = spi_write(flash->spi, command, 2); | |
313 | if (ret) | |
314 | goto out; | |
315 | } | |
316 | } | |
317 | ||
318 | out: | |
319 | ret = sst25l_write_enable(flash, 0); | |
320 | ||
321 | if (retlen) | |
322 | *retlen = copied; | |
323 | ||
324 | mutex_unlock(&flash->lock); | |
325 | return ret; | |
326 | } | |
327 | ||
328 | static struct flash_info *__init sst25l_match_device(struct spi_device *spi) | |
329 | { | |
330 | struct flash_info *flash_info = NULL; | |
331 | unsigned char command[4], response; | |
332 | int i, err; | |
333 | uint16_t id; | |
334 | ||
335 | command[0] = SST25L_CMD_READ_ID; | |
336 | command[1] = 0; | |
337 | command[2] = 0; | |
338 | command[3] = 0; | |
339 | err = spi_write_then_read(spi, command, sizeof(command), &response, 1); | |
340 | if (err < 0) { | |
341 | dev_err(&spi->dev, "error reading device id msb\n"); | |
342 | return NULL; | |
343 | } | |
344 | ||
345 | id = response << 8; | |
346 | ||
347 | command[0] = SST25L_CMD_READ_ID; | |
348 | command[1] = 0; | |
349 | command[2] = 0; | |
350 | command[3] = 1; | |
351 | err = spi_write_then_read(spi, command, sizeof(command), &response, 1); | |
352 | if (err < 0) { | |
353 | dev_err(&spi->dev, "error reading device id lsb\n"); | |
354 | return NULL; | |
355 | } | |
356 | ||
357 | id |= response; | |
358 | ||
359 | for (i = 0; i < ARRAY_SIZE(sst25l_flash_info); i++) | |
360 | if (sst25l_flash_info[i].device_id == id) | |
361 | flash_info = &sst25l_flash_info[i]; | |
362 | ||
363 | if (!flash_info) | |
364 | dev_err(&spi->dev, "unknown id %.4x\n", id); | |
365 | ||
366 | return flash_info; | |
367 | } | |
368 | ||
369 | static int __init sst25l_probe(struct spi_device *spi) | |
370 | { | |
371 | struct flash_info *flash_info; | |
372 | struct sst25l_flash *flash; | |
373 | struct flash_platform_data *data; | |
374 | int ret, i; | |
375 | ||
376 | flash_info = sst25l_match_device(spi); | |
377 | if (!flash_info) | |
378 | return -ENODEV; | |
379 | ||
380 | flash = kzalloc(sizeof(struct sst25l_flash), GFP_KERNEL); | |
381 | if (!flash) | |
382 | return -ENOMEM; | |
383 | ||
384 | flash->spi = spi; | |
385 | mutex_init(&flash->lock); | |
386 | dev_set_drvdata(&spi->dev, flash); | |
387 | ||
388 | data = spi->dev.platform_data; | |
389 | if (data && data->name) | |
390 | flash->mtd.name = data->name; | |
391 | else | |
392 | flash->mtd.name = dev_name(&spi->dev); | |
393 | ||
394 | flash->mtd.type = MTD_NORFLASH; | |
395 | flash->mtd.flags = MTD_CAP_NORFLASH; | |
396 | flash->mtd.erasesize = flash_info->erase_size; | |
397 | flash->mtd.writesize = flash_info->page_size; | |
398 | flash->mtd.size = flash_info->page_size * flash_info->nr_pages; | |
399 | flash->mtd.erase = sst25l_erase; | |
400 | flash->mtd.read = sst25l_read; | |
401 | flash->mtd.write = sst25l_write; | |
402 | ||
403 | dev_info(&spi->dev, "%s (%lld KiB)\n", flash_info->name, | |
404 | (long long)flash->mtd.size >> 10); | |
405 | ||
406 | DEBUG(MTD_DEBUG_LEVEL2, | |
407 | "mtd .name = %s, .size = 0x%llx (%lldMiB) " | |
408 | ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n", | |
409 | flash->mtd.name, | |
410 | (long long)flash->mtd.size, (long long)(flash->mtd.size >> 20), | |
411 | flash->mtd.erasesize, flash->mtd.erasesize / 1024, | |
412 | flash->mtd.numeraseregions); | |
413 | ||
414 | if (flash->mtd.numeraseregions) | |
415 | for (i = 0; i < flash->mtd.numeraseregions; i++) | |
416 | DEBUG(MTD_DEBUG_LEVEL2, | |
417 | "mtd.eraseregions[%d] = { .offset = 0x%llx, " | |
418 | ".erasesize = 0x%.8x (%uKiB), " | |
419 | ".numblocks = %d }\n", | |
420 | i, (long long)flash->mtd.eraseregions[i].offset, | |
421 | flash->mtd.eraseregions[i].erasesize, | |
422 | flash->mtd.eraseregions[i].erasesize / 1024, | |
423 | flash->mtd.eraseregions[i].numblocks); | |
424 | ||
425 | if (mtd_has_partitions()) { | |
426 | struct mtd_partition *parts = NULL; | |
427 | int nr_parts = 0; | |
428 | ||
429 | if (mtd_has_cmdlinepart()) { | |
430 | static const char *part_probes[] = | |
431 | {"cmdlinepart", NULL}; | |
432 | ||
433 | nr_parts = parse_mtd_partitions(&flash->mtd, | |
434 | part_probes, | |
435 | &parts, 0); | |
436 | } | |
437 | ||
438 | if (nr_parts <= 0 && data && data->parts) { | |
439 | parts = data->parts; | |
440 | nr_parts = data->nr_parts; | |
441 | } | |
442 | ||
443 | if (nr_parts > 0) { | |
444 | for (i = 0; i < nr_parts; i++) { | |
445 | DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = " | |
446 | "{.name = %s, .offset = 0x%llx, " | |
447 | ".size = 0x%llx (%lldKiB) }\n", | |
448 | i, parts[i].name, | |
449 | (long long)parts[i].offset, | |
450 | (long long)parts[i].size, | |
451 | (long long)(parts[i].size >> 10)); | |
452 | } | |
453 | ||
454 | flash->partitioned = 1; | |
455 | return add_mtd_partitions(&flash->mtd, | |
456 | parts, nr_parts); | |
457 | } | |
458 | ||
459 | } else if (data->nr_parts) { | |
460 | dev_warn(&spi->dev, "ignoring %d default partitions on %s\n", | |
461 | data->nr_parts, data->name); | |
462 | } | |
463 | ||
464 | ret = add_mtd_device(&flash->mtd); | |
465 | if (ret == 1) { | |
466 | kfree(flash); | |
467 | dev_set_drvdata(&spi->dev, NULL); | |
468 | return -ENODEV; | |
469 | } | |
470 | ||
471 | return 0; | |
472 | } | |
473 | ||
474 | static int __exit sst25l_remove(struct spi_device *spi) | |
475 | { | |
476 | struct sst25l_flash *flash = dev_get_drvdata(&spi->dev); | |
477 | int ret; | |
478 | ||
479 | if (mtd_has_partitions() && flash->partitioned) | |
480 | ret = del_mtd_partitions(&flash->mtd); | |
481 | else | |
482 | ret = del_mtd_device(&flash->mtd); | |
483 | if (ret == 0) | |
484 | kfree(flash); | |
485 | return ret; | |
486 | } | |
487 | ||
488 | static struct spi_driver sst25l_driver = { | |
489 | .driver = { | |
490 | .name = "sst25l", | |
491 | .bus = &spi_bus_type, | |
492 | .owner = THIS_MODULE, | |
493 | }, | |
494 | .probe = sst25l_probe, | |
495 | .remove = __exit_p(sst25l_remove), | |
496 | }; | |
497 | ||
498 | static int __init sst25l_init(void) | |
499 | { | |
500 | return spi_register_driver(&sst25l_driver); | |
501 | } | |
502 | ||
503 | static void __exit sst25l_exit(void) | |
504 | { | |
505 | spi_unregister_driver(&sst25l_driver); | |
506 | } | |
507 | ||
508 | module_init(sst25l_init); | |
509 | module_exit(sst25l_exit); | |
510 | ||
511 | MODULE_DESCRIPTION("MTD SPI driver for SST25L Flash chips"); | |
512 | MODULE_AUTHOR("Andre Renaud <andre@bluewatersys.com>, " | |
513 | "Ryan Mallon <ryan@bluewatersys.com>"); | |
514 | MODULE_LICENSE("GPL"); |