1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/interrupt.h>
22#include <linux/mutex.h>
23
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/partitions.h>
26
27#include <linux/spi/spi.h>
28#include <linux/spi/flash.h>
29
30
31#define FLASH_PAGESIZE 256
32
33
34#define OPCODE_WREN 0x06
35#define OPCODE_RDSR 0x05
36#define OPCODE_WRSR 0x01
37#define OPCODE_NORM_READ 0x03
38#define OPCODE_FAST_READ 0x0b
39#define OPCODE_PP 0x02
40#define OPCODE_BE_4K 0x20
41#define OPCODE_BE_32K 0x52
42#define OPCODE_CHIP_ERASE 0xc7
43#define OPCODE_SE 0xd8
44#define OPCODE_RDID 0x9f
45
46
47#define SR_WIP 1
48#define SR_WEL 2
49
50#define SR_BP0 4
51#define SR_BP1 8
52#define SR_BP2 0x10
53#define SR_SRWD 0x80
54
55
56#define MAX_READY_WAIT_COUNT 100000
57#define CMD_SIZE 4
58
59#ifdef CONFIG_M25PXX_USE_FAST_READ
60#define OPCODE_READ OPCODE_FAST_READ
61#define FAST_READ_DUMMY_BYTE 1
62#else
63#define OPCODE_READ OPCODE_NORM_READ
64#define FAST_READ_DUMMY_BYTE 0
65#endif
66
67#ifdef CONFIG_MTD_PARTITIONS
68#define mtd_has_partitions() (1)
69#else
70#define mtd_has_partitions() (0)
71#endif
72
73
74
75struct m25p {
76 struct spi_device *spi;
77 struct mutex lock;
78 struct mtd_info mtd;
79 unsigned partitioned:1;
80 u8 erase_opcode;
81 u8 command[CMD_SIZE + FAST_READ_DUMMY_BYTE];
82};
83
84static inline struct m25p *mtd_to_m25p(struct mtd_info *mtd)
85{
86 return container_of(mtd, struct m25p, mtd);
87}
88
89
90
91
92
93
94
95
96
97
98
99
100static int read_sr(struct m25p *flash)
101{
102 ssize_t retval;
103 u8 code = OPCODE_RDSR;
104 u8 val;
105
106 retval = spi_write_then_read(flash->spi, &code, 1, &val, 1);
107
108 if (retval < 0) {
109 dev_err(&flash->spi->dev, "error %d reading SR\n",
110 (int) retval);
111 return retval;
112 }
113
114 return val;
115}
116
117
118
119
120
121static int write_sr(struct m25p *flash, u8 val)
122{
123 flash->command[0] = OPCODE_WRSR;
124 flash->command[1] = val;
125
126 return spi_write(flash->spi, flash->command, 2);
127}
128
129
130
131
132
133static inline int write_enable(struct m25p *flash)
134{
135 u8 code = OPCODE_WREN;
136
137 return spi_write_then_read(flash->spi, &code, 1, NULL, 0);
138}
139
140
141
142
143
144
145static int wait_till_ready(struct m25p *flash)
146{
147 int count;
148 int sr;
149
150
151
152
153 for (count = 0; count < MAX_READY_WAIT_COUNT; count++) {
154 if ((sr = read_sr(flash)) < 0)
155 break;
156 else if (!(sr & SR_WIP))
157 return 0;
158
159
160 }
161
162 return 1;
163}
164
165
166
167
168
169
170static int erase_chip(struct m25p *flash)
171{
172 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB\n",
173 flash->spi->dev.bus_id, __func__,
174 flash->mtd.size / 1024);
175
176
177 if (wait_till_ready(flash))
178 return 1;
179
180
181 write_enable(flash);
182
183
184 flash->command[0] = OPCODE_CHIP_ERASE;
185
186 spi_write(flash->spi, flash->command, 1);
187
188 return 0;
189}
190
191
192
193
194
195
196
197static int erase_sector(struct m25p *flash, u32 offset)
198{
199 DEBUG(MTD_DEBUG_LEVEL3, "%s: %s %dKiB at 0x%08x\n",
200 flash->spi->dev.bus_id, __func__,
201 flash->mtd.erasesize / 1024, offset);
202
203
204 if (wait_till_ready(flash))
205 return 1;
206
207
208 write_enable(flash);
209
210
211 flash->command[0] = flash->erase_opcode;
212 flash->command[1] = offset >> 16;
213 flash->command[2] = offset >> 8;
214 flash->command[3] = offset;
215
216 spi_write(flash->spi, flash->command, CMD_SIZE);
217
218 return 0;
219}
220
221
222
223
224
225
226
227
228
229
230
231static int m25p80_erase(struct mtd_info *mtd, struct erase_info *instr)
232{
233 struct m25p *flash = mtd_to_m25p(mtd);
234 u32 addr,len;
235
236 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %d\n",
237 flash->spi->dev.bus_id, __func__, "at",
238 (u32)instr->addr, instr->len);
239
240
241 if (instr->addr + instr->len > flash->mtd.size)
242 return -EINVAL;
243 if ((instr->addr % mtd->erasesize) != 0
244 || (instr->len % mtd->erasesize) != 0) {
245 return -EINVAL;
246 }
247
248 addr = instr->addr;
249 len = instr->len;
250
251 mutex_lock(&flash->lock);
252
253
254 if (len == flash->mtd.size && erase_chip(flash)) {
255 instr->state = MTD_ERASE_FAILED;
256 mutex_unlock(&flash->lock);
257 return -EIO;
258
259
260
261
262
263
264
265 } else {
266 while (len) {
267 if (erase_sector(flash, addr)) {
268 instr->state = MTD_ERASE_FAILED;
269 mutex_unlock(&flash->lock);
270 return -EIO;
271 }
272
273 addr += mtd->erasesize;
274 len -= mtd->erasesize;
275 }
276 }
277
278 mutex_unlock(&flash->lock);
279
280 instr->state = MTD_ERASE_DONE;
281 mtd_erase_callback(instr);
282
283 return 0;
284}
285
286
287
288
289
290static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
291 size_t *retlen, u_char *buf)
292{
293 struct m25p *flash = mtd_to_m25p(mtd);
294 struct spi_transfer t[2];
295 struct spi_message m;
296
297 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
298 flash->spi->dev.bus_id, __func__, "from",
299 (u32)from, len);
300
301
302 if (!len)
303 return 0;
304
305 if (from + len > flash->mtd.size)
306 return -EINVAL;
307
308 spi_message_init(&m);
309 memset(t, 0, (sizeof t));
310
311
312
313
314
315 t[0].tx_buf = flash->command;
316 t[0].len = CMD_SIZE + FAST_READ_DUMMY_BYTE;
317 spi_message_add_tail(&t[0], &m);
318
319 t[1].rx_buf = buf;
320 t[1].len = len;
321 spi_message_add_tail(&t[1], &m);
322
323
324 if (retlen)
325 *retlen = 0;
326
327 mutex_lock(&flash->lock);
328
329
330 if (wait_till_ready(flash)) {
331
332 mutex_unlock(&flash->lock);
333 return 1;
334 }
335
336
337
338
339
340
341
342 flash->command[0] = OPCODE_READ;
343 flash->command[1] = from >> 16;
344 flash->command[2] = from >> 8;
345 flash->command[3] = from;
346
347 spi_sync(flash->spi, &m);
348
349 *retlen = m.actual_length - CMD_SIZE - FAST_READ_DUMMY_BYTE;
350
351 mutex_unlock(&flash->lock);
352
353 return 0;
354}
355
356
357
358
359
360
361static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
362 size_t *retlen, const u_char *buf)
363{
364 struct m25p *flash = mtd_to_m25p(mtd);
365 u32 page_offset, page_size;
366 struct spi_transfer t[2];
367 struct spi_message m;
368
369 DEBUG(MTD_DEBUG_LEVEL2, "%s: %s %s 0x%08x, len %zd\n",
370 flash->spi->dev.bus_id, __func__, "to",
371 (u32)to, len);
372
373 if (retlen)
374 *retlen = 0;
375
376
377 if (!len)
378 return(0);
379
380 if (to + len > flash->mtd.size)
381 return -EINVAL;
382
383 spi_message_init(&m);
384 memset(t, 0, (sizeof t));
385
386 t[0].tx_buf = flash->command;
387 t[0].len = CMD_SIZE;
388 spi_message_add_tail(&t[0], &m);
389
390 t[1].tx_buf = buf;
391 spi_message_add_tail(&t[1], &m);
392
393 mutex_lock(&flash->lock);
394
395
396 if (wait_till_ready(flash)) {
397 mutex_unlock(&flash->lock);
398 return 1;
399 }
400
401 write_enable(flash);
402
403
404 flash->command[0] = OPCODE_PP;
405 flash->command[1] = to >> 16;
406 flash->command[2] = to >> 8;
407 flash->command[3] = to;
408
409
410 page_offset = to % FLASH_PAGESIZE;
411
412
413 if (page_offset + len <= FLASH_PAGESIZE) {
414 t[1].len = len;
415
416 spi_sync(flash->spi, &m);
417
418 *retlen = m.actual_length - CMD_SIZE;
419 } else {
420 u32 i;
421
422
423 page_size = FLASH_PAGESIZE - page_offset;
424
425 t[1].len = page_size;
426 spi_sync(flash->spi, &m);
427
428 *retlen = m.actual_length - CMD_SIZE;
429
430
431 for (i = page_size; i < len; i += page_size) {
432 page_size = len - i;
433 if (page_size > FLASH_PAGESIZE)
434 page_size = FLASH_PAGESIZE;
435
436
437 flash->command[1] = (to + i) >> 16;
438 flash->command[2] = (to + i) >> 8;
439 flash->command[3] = (to + i);
440
441 t[1].tx_buf = buf + i;
442 t[1].len = page_size;
443
444 wait_till_ready(flash);
445
446 write_enable(flash);
447
448 spi_sync(flash->spi, &m);
449
450 if (retlen)
451 *retlen += m.actual_length - CMD_SIZE;
452 }
453 }
454
455 mutex_unlock(&flash->lock);
456
457 return 0;
458}
459
460
461
462
463
464
465
466
467struct flash_info {
468 char *name;
469
470
471
472
473
474 u32 jedec_id;
475 u16 ext_id;
476
477
478
479
480 unsigned sector_size;
481 u16 n_sectors;
482
483 u16 flags;
484#define SECT_4K 0x01
485};
486
487
488
489
490
491
492static struct flash_info __devinitdata m25p_data [] = {
493
494
495 { "at25fs010", 0x1f6601, 0, 32 * 1024, 4, SECT_4K, },
496 { "at25fs040", 0x1f6604, 0, 64 * 1024, 8, SECT_4K, },
497
498 { "at25df041a", 0x1f4401, 0, 64 * 1024, 8, SECT_4K, },
499 { "at25df641", 0x1f4800, 0, 64 * 1024, 128, SECT_4K, },
500
501 { "at26f004", 0x1f0400, 0, 64 * 1024, 8, SECT_4K, },
502 { "at26df081a", 0x1f4501, 0, 64 * 1024, 16, SECT_4K, },
503 { "at26df161a", 0x1f4601, 0, 64 * 1024, 32, SECT_4K, },
504 { "at26df321", 0x1f4701, 0, 64 * 1024, 64, SECT_4K, },
505
506
507
508
509 { "s25sl004a", 0x010212, 0, 64 * 1024, 8, },
510 { "s25sl008a", 0x010213, 0, 64 * 1024, 16, },
511 { "s25sl016a", 0x010214, 0, 64 * 1024, 32, },
512 { "s25sl032a", 0x010215, 0, 64 * 1024, 64, },
513 { "s25sl064a", 0x010216, 0, 64 * 1024, 128, },
514 { "s25sl12800", 0x012018, 0x0300, 256 * 1024, 64, },
515 { "s25sl12801", 0x012018, 0x0301, 64 * 1024, 256, },
516
517
518 { "sst25vf040b", 0xbf258d, 0, 64 * 1024, 8, SECT_4K, },
519 { "sst25vf080b", 0xbf258e, 0, 64 * 1024, 16, SECT_4K, },
520 { "sst25vf016b", 0xbf2541, 0, 64 * 1024, 32, SECT_4K, },
521 { "sst25vf032b", 0xbf254a, 0, 64 * 1024, 64, SECT_4K, },
522
523
524 { "m25p05", 0x202010, 0, 32 * 1024, 2, },
525 { "m25p10", 0x202011, 0, 32 * 1024, 4, },
526 { "m25p20", 0x202012, 0, 64 * 1024, 4, },
527 { "m25p40", 0x202013, 0, 64 * 1024, 8, },
528 { "m25p80", 0, 0, 64 * 1024, 16, },
529 { "m25p16", 0x202015, 0, 64 * 1024, 32, },
530 { "m25p32", 0x202016, 0, 64 * 1024, 64, },
531 { "m25p64", 0x202017, 0, 64 * 1024, 128, },
532 { "m25p128", 0x202018, 0, 256 * 1024, 64, },
533
534 { "m45pe80", 0x204014, 0, 64 * 1024, 16, },
535 { "m45pe16", 0x204015, 0, 64 * 1024, 32, },
536
537 { "m25pe80", 0x208014, 0, 64 * 1024, 16, },
538 { "m25pe16", 0x208015, 0, 64 * 1024, 32, SECT_4K, },
539
540
541 { "w25x10", 0xef3011, 0, 64 * 1024, 2, SECT_4K, },
542 { "w25x20", 0xef3012, 0, 64 * 1024, 4, SECT_4K, },
543 { "w25x40", 0xef3013, 0, 64 * 1024, 8, SECT_4K, },
544 { "w25x80", 0xef3014, 0, 64 * 1024, 16, SECT_4K, },
545 { "w25x16", 0xef3015, 0, 64 * 1024, 32, SECT_4K, },
546 { "w25x32", 0xef3016, 0, 64 * 1024, 64, SECT_4K, },
547 { "w25x64", 0xef3017, 0, 64 * 1024, 128, SECT_4K, },
548};
549
550static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
551{
552 int tmp;
553 u8 code = OPCODE_RDID;
554 u8 id[5];
555 u32 jedec;
556 u16 ext_jedec;
557 struct flash_info *info;
558
559
560
561
562
563 tmp = spi_write_then_read(spi, &code, 1, id, 5);
564 if (tmp < 0) {
565 DEBUG(MTD_DEBUG_LEVEL0, "%s: error %d reading JEDEC ID\n",
566 spi->dev.bus_id, tmp);
567 return NULL;
568 }
569 jedec = id[0];
570 jedec = jedec << 8;
571 jedec |= id[1];
572 jedec = jedec << 8;
573 jedec |= id[2];
574
575 ext_jedec = id[3] << 8 | id[4];
576
577 for (tmp = 0, info = m25p_data;
578 tmp < ARRAY_SIZE(m25p_data);
579 tmp++, info++) {
580 if (info->jedec_id == jedec) {
581 if (info->ext_id != 0 && info->ext_id != ext_jedec)
582 continue;
583 return info;
584 }
585 }
586 dev_err(&spi->dev, "unrecognized JEDEC id %06x\n", jedec);
587 return NULL;
588}
589
590
591
592
593
594
595
596static int __devinit m25p_probe(struct spi_device *spi)
597{
598 struct flash_platform_data *data;
599 struct m25p *flash;
600 struct flash_info *info;
601 unsigned i;
602
603
604
605
606
607
608 data = spi->dev.platform_data;
609 if (data && data->type) {
610 for (i = 0, info = m25p_data;
611 i < ARRAY_SIZE(m25p_data);
612 i++, info++) {
613 if (strcmp(data->type, info->name) == 0)
614 break;
615 }
616
617
618 if (i == ARRAY_SIZE(m25p_data)) {
619 DEBUG(MTD_DEBUG_LEVEL0, "%s: unrecognized id %s\n",
620 spi->dev.bus_id, data->type);
621 info = NULL;
622
623
624 } else if (info->jedec_id) {
625 struct flash_info *chip = jedec_probe(spi);
626
627 if (!chip || chip != info) {
628 dev_warn(&spi->dev, "found %s, expected %s\n",
629 chip ? chip->name : "UNKNOWN",
630 info->name);
631 info = NULL;
632 }
633 }
634 } else
635 info = jedec_probe(spi);
636
637 if (!info)
638 return -ENODEV;
639
640 flash = kzalloc(sizeof *flash, GFP_KERNEL);
641 if (!flash)
642 return -ENOMEM;
643
644 flash->spi = spi;
645 mutex_init(&flash->lock);
646 dev_set_drvdata(&spi->dev, flash);
647
648
649
650
651
652
653 if (info->jedec_id >> 16 == 0x1f) {
654 write_enable(flash);
655 write_sr(flash, 0);
656 }
657
658 if (data && data->name)
659 flash->mtd.name = data->name;
660 else
661 flash->mtd.name = spi->dev.bus_id;
662
663 flash->mtd.type = MTD_NORFLASH;
664 flash->mtd.writesize = 1;
665 flash->mtd.flags = MTD_CAP_NORFLASH;
666 flash->mtd.size = info->sector_size * info->n_sectors;
667 flash->mtd.erase = m25p80_erase;
668 flash->mtd.read = m25p80_read;
669 flash->mtd.write = m25p80_write;
670
671
672 if (info->flags & SECT_4K) {
673 flash->erase_opcode = OPCODE_BE_4K;
674 flash->mtd.erasesize = 4096;
675 } else {
676 flash->erase_opcode = OPCODE_SE;
677 flash->mtd.erasesize = info->sector_size;
678 }
679
680 dev_info(&spi->dev, "%s (%d Kbytes)\n", info->name,
681 flash->mtd.size / 1024);
682
683 DEBUG(MTD_DEBUG_LEVEL2,
684 "mtd .name = %s, .size = 0x%.8x (%uMiB) "
685 ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
686 flash->mtd.name,
687 flash->mtd.size, flash->mtd.size / (1024*1024),
688 flash->mtd.erasesize, flash->mtd.erasesize / 1024,
689 flash->mtd.numeraseregions);
690
691 if (flash->mtd.numeraseregions)
692 for (i = 0; i < flash->mtd.numeraseregions; i++)
693 DEBUG(MTD_DEBUG_LEVEL2,
694 "mtd.eraseregions[%d] = { .offset = 0x%.8x, "
695 ".erasesize = 0x%.8x (%uKiB), "
696 ".numblocks = %d }\n",
697 i, flash->mtd.eraseregions[i].offset,
698 flash->mtd.eraseregions[i].erasesize,
699 flash->mtd.eraseregions[i].erasesize / 1024,
700 flash->mtd.eraseregions[i].numblocks);
701
702
703
704
705
706 if (mtd_has_partitions()) {
707 struct mtd_partition *parts = NULL;
708 int nr_parts = 0;
709
710#ifdef CONFIG_MTD_CMDLINE_PARTS
711 static const char *part_probes[] = { "cmdlinepart", NULL, };
712
713 nr_parts = parse_mtd_partitions(&flash->mtd,
714 part_probes, &parts, 0);
715#endif
716
717 if (nr_parts <= 0 && data && data->parts) {
718 parts = data->parts;
719 nr_parts = data->nr_parts;
720 }
721
722 if (nr_parts > 0) {
723 for (i = 0; i < nr_parts; i++) {
724 DEBUG(MTD_DEBUG_LEVEL2, "partitions[%d] = "
725 "{.name = %s, .offset = 0x%.8x, "
726 ".size = 0x%.8x (%uKiB) }\n",
727 i, parts[i].name,
728 parts[i].offset,
729 parts[i].size,
730 parts[i].size / 1024);
731 }
732 flash->partitioned = 1;
733 return add_mtd_partitions(&flash->mtd, parts, nr_parts);
734 }
735 } else if (data->nr_parts)
736 dev_warn(&spi->dev, "ignoring %d default partitions on %s\n",
737 data->nr_parts, data->name);
738
739 return add_mtd_device(&flash->mtd) == 1 ? -ENODEV : 0;
740}
741
742
743static int __devexit m25p_remove(struct spi_device *spi)
744{
745 struct m25p *flash = dev_get_drvdata(&spi->dev);
746 int status;
747
748
749 if (mtd_has_partitions() && flash->partitioned)
750 status = del_mtd_partitions(&flash->mtd);
751 else
752 status = del_mtd_device(&flash->mtd);
753 if (status == 0)
754 kfree(flash);
755 return 0;
756}
757
758
759static struct spi_driver m25p80_driver = {
760 .driver = {
761 .name = "m25p80",
762 .bus = &spi_bus_type,
763 .owner = THIS_MODULE,
764 },
765 .probe = m25p_probe,
766 .remove = __devexit_p(m25p_remove),
767
768
769
770
771
772};
773
774
775static int m25p80_init(void)
776{
777 return spi_register_driver(&m25p80_driver);
778}
779
780
781static void m25p80_exit(void)
782{
783 spi_unregister_driver(&m25p80_driver);
784}
785
786
787module_init(m25p80_init);
788module_exit(m25p80_exit);
789
790MODULE_LICENSE("GPL");
791MODULE_AUTHOR("Mike Lavender");
792MODULE_DESCRIPTION("MTD SPI driver for ST M25Pxx flash chips");