1515#include <linux/delay.h>
1616#include <linux/device.h>
1717#include <linux/mutex.h>
18+ #include <linux/err.h>
19+
1820#include <linux/spi/spi.h>
1921#include <linux/spi/flash.h>
2022
@@ -487,9 +489,8 @@ add_dataflash(struct spi_device *spi, char *name,
487489 device -> write = dataflash_write ;
488490 device -> priv = priv ;
489491
490- dev_info (& spi -> dev , "%s (%d KBytes) pagesize %d bytes, "
491- "erasesize %d bytes\n" , name , device -> size /1024 ,
492- pagesize , pagesize * 8 ); /* 8 pages = 1 block */
492+ dev_info (& spi -> dev , "%s (%d KBytes) pagesize %d bytes\n" ,
493+ name , DIV_ROUND_UP (device -> size , 1024 ), pagesize );
493494 dev_set_drvdata (& spi -> dev , priv );
494495
495496 if (mtd_has_partitions ()) {
@@ -518,65 +519,57 @@ add_dataflash(struct spi_device *spi, char *name,
518519 return add_mtd_device (device ) == 1 ? - ENODEV : 0 ;
519520}
520521
521- /*
522- * Detect and initialize DataFlash device:
523- *
524- * Device Density ID code #Pages PageSize Offset
525- * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
526- * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1024 264 9
527- * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
528- * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
529- * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
530- * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
531- * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
532- * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
533- */
534-
535522struct flash_info {
536523 char * name ;
537524
538- /* JEDEC id zero means "no ID" (most older chips); otherwise it has
539- * a high byte of zero plus three data bytes: the manufacturer id,
540- * then a two byte device id.
525+ /* JEDEC id has a high byte of zero plus three data bytes:
526+ * the manufacturer id, then a two byte device id.
541527 */
542528 uint32_t jedec_id ;
543529
544- /* The size listed here is what works with OPCODE_SE, which isn't
545- * necessarily called a "sector" by the vendor.
546- */
530+ /* The size listed here is what works with OP_ERASE_PAGE. */
547531 unsigned nr_pages ;
548532 uint16_t pagesize ;
549533 uint16_t pageoffset ;
550534
551535 uint16_t flags ;
552- #define SUP_POW2PS 0x02
553- #define IS_POW2PS 0x01
536+ #define SUP_POW2PS 0x0002 /* supports 2^N byte pages */
537+ #define IS_POW2PS 0x0001 /* uses 2^N byte pages */
554538};
555539
556540static struct flash_info __devinitdata dataflash_data [] = {
557541
558- { "at45db011d" , 0x1f2200 , 512 , 264 , 9 , SUP_POW2PS },
542+ /*
543+ * NOTE: chips with SUP_POW2PS (rev D and up) need two entries,
544+ * one with IS_POW2PS and the other without. The entry with the
545+ * non-2^N byte page size can't name exact chip revisions without
546+ * losing backwards compatibility for cmdlinepart.
547+ *
548+ * These newer chips also support 128-byte security registers (with
549+ * 64 bytes one-time-programmable) and software write-protection.
550+ */
551+ { "AT45DB011B" , 0x1f2200 , 512 , 264 , 9 , SUP_POW2PS },
559552 { "at45db011d" , 0x1f2200 , 512 , 256 , 8 , SUP_POW2PS | IS_POW2PS },
560553
561- { "at45db021d " , 0x1f2300 , 1024 , 264 , 9 , SUP_POW2PS },
554+ { "AT45DB021B " , 0x1f2300 , 1024 , 264 , 9 , SUP_POW2PS },
562555 { "at45db021d" , 0x1f2300 , 1024 , 256 , 8 , SUP_POW2PS | IS_POW2PS },
563556
564- { "at45db041d " , 0x1f2400 , 2048 , 264 , 9 , SUP_POW2PS },
557+ { "AT45DB041x " , 0x1f2400 , 2048 , 264 , 9 , SUP_POW2PS },
565558 { "at45db041d" , 0x1f2400 , 2048 , 256 , 8 , SUP_POW2PS | IS_POW2PS },
566559
567- { "at45db081d " , 0x1f2500 , 4096 , 264 , 9 , SUP_POW2PS },
560+ { "AT45DB081B " , 0x1f2500 , 4096 , 264 , 9 , SUP_POW2PS },
568561 { "at45db081d" , 0x1f2500 , 4096 , 256 , 8 , SUP_POW2PS | IS_POW2PS },
569562
570- { "at45db161d " , 0x1f2600 , 4096 , 528 , 10 , SUP_POW2PS },
563+ { "AT45DB161x " , 0x1f2600 , 4096 , 528 , 10 , SUP_POW2PS },
571564 { "at45db161d" , 0x1f2600 , 4096 , 512 , 9 , SUP_POW2PS | IS_POW2PS },
572565
573- { "at45db321c " , 0x1f2700 , 8192 , 528 , 10 , },
566+ { "AT45DB321x " , 0x1f2700 , 8192 , 528 , 10 , 0 }, /* rev C */
574567
575- { "at45db321d " , 0x1f2701 , 8192 , 528 , 10 , SUP_POW2PS },
568+ { "AT45DB321x " , 0x1f2701 , 8192 , 528 , 10 , SUP_POW2PS },
576569 { "at45db321d" , 0x1f2701 , 8192 , 512 , 9 , SUP_POW2PS | IS_POW2PS },
577570
578- { "at45db641d " , 0x1f2800 , 8192 , 1056 , 11 , SUP_POW2PS },
579- { "at45db641d " , 0x1f2800 , 8192 , 1024 , 10 , SUP_POW2PS | IS_POW2PS },
571+ { "AT45DB642x " , 0x1f2800 , 8192 , 1056 , 11 , SUP_POW2PS },
572+ { "at45db642d " , 0x1f2800 , 8192 , 1024 , 10 , SUP_POW2PS | IS_POW2PS },
580573};
581574
582575static struct flash_info * __devinit jedec_probe (struct spi_device * spi )
@@ -588,17 +581,23 @@ static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
588581 struct flash_info * info ;
589582 int status ;
590583
591-
592584 /* JEDEC also defines an optional "extended device information"
593585 * string for after vendor-specific data, after the three bytes
594586 * we use here. Supporting some chips might require using it.
587+ *
588+ * If the vendor ID isn't Atmel's (0x1f), assume this call failed.
589+ * That's not an error; only rev C and newer chips handle it, and
590+ * only Atmel sells these chips.
595591 */
596592 tmp = spi_write_then_read (spi , & code , 1 , id , 3 );
597593 if (tmp < 0 ) {
598594 DEBUG (MTD_DEBUG_LEVEL0 , "%s: error %d reading JEDEC ID\n" ,
599595 spi -> dev .bus_id , tmp );
600- return NULL ;
596+ return ERR_PTR ( tmp ) ;
601597 }
598+ if (id [0 ] != 0x1f )
599+ return NULL ;
600+
602601 jedec = id [0 ];
603602 jedec = jedec << 8 ;
604603 jedec |= id [1 ];
@@ -609,19 +608,53 @@ static struct flash_info *__devinit jedec_probe(struct spi_device *spi)
609608 tmp < ARRAY_SIZE (dataflash_data );
610609 tmp ++ , info ++ ) {
611610 if (info -> jedec_id == jedec ) {
611+ DEBUG (MTD_DEBUG_LEVEL1 , "%s: OTP, sector protect%s\n" ,
612+ dev_name (& spi -> dev ),
613+ (info -> flags & SUP_POW2PS )
614+ ? ", binary pagesize" : ""
615+ );
612616 if (info -> flags & SUP_POW2PS ) {
613617 status = dataflash_status (spi );
614- if (status & 0x1 )
615- /* return power of 2 pagesize */
616- return ++ info ;
617- else
618- return info ;
618+ if (status < 0 ) {
619+ DEBUG (MTD_DEBUG_LEVEL1 ,
620+ "%s: status error %d\n" ,
621+ dev_name (& spi -> dev ), status );
622+ return ERR_PTR (status );
623+ }
624+ if (status & 0x1 ) {
625+ if (info -> flags & IS_POW2PS )
626+ return info ;
627+ } else {
628+ if (!(info -> flags & IS_POW2PS ))
629+ return info ;
630+ }
619631 }
620632 }
621633 }
622- return NULL ;
634+
635+ /*
636+ * Treat other chips as errors ... we won't know the right page
637+ * size (it might be binary) even when we can tell which density
638+ * class is involved (legacy chip id scheme).
639+ */
640+ dev_warn (& spi -> dev , "JEDEC id %06x not handled\n" , jedec );
641+ return ERR_PTR (- ENODEV );
623642}
624643
644+ /*
645+ * Detect and initialize DataFlash device, using JEDEC IDs on newer chips
646+ * or else the ID code embedded in the status bits:
647+ *
648+ * Device Density ID code #Pages PageSize Offset
649+ * AT45DB011B 1Mbit (128K) xx0011xx (0x0c) 512 264 9
650+ * AT45DB021B 2Mbit (256K) xx0101xx (0x14) 1024 264 9
651+ * AT45DB041B 4Mbit (512K) xx0111xx (0x1c) 2048 264 9
652+ * AT45DB081B 8Mbit (1M) xx1001xx (0x24) 4096 264 9
653+ * AT45DB0161B 16Mbit (2M) xx1011xx (0x2c) 4096 528 10
654+ * AT45DB0321B 32Mbit (4M) xx1101xx (0x34) 8192 528 10
655+ * AT45DB0642 64Mbit (8M) xx111xxx (0x3c) 8192 1056 11
656+ * AT45DB1282 128Mbit (16M) xx0100xx (0x10) 16384 1056 11
657+ */
625658static int __devinit dataflash_probe (struct spi_device * spi )
626659{
627660 int status ;
@@ -632,14 +665,17 @@ static int __devinit dataflash_probe(struct spi_device *spi)
632665 * If it succeeds we know we have either a C or D part.
633666 * D will support power of 2 pagesize option.
634667 */
635-
636668 info = jedec_probe (spi );
637-
669+ if (IS_ERR (info ))
670+ return PTR_ERR (info );
638671 if (info != NULL )
639672 return add_dataflash (spi , info -> name , info -> nr_pages ,
640673 info -> pagesize , info -> pageoffset );
641674
642-
675+ /*
676+ * Older chips support only legacy commands, identifing
677+ * capacity using bits in the status byte.
678+ */
643679 status = dataflash_status (spi );
644680 if (status <= 0 || status == 0xff ) {
645681 DEBUG (MTD_DEBUG_LEVEL1 , "%s: status error %d\n" ,
@@ -661,13 +697,13 @@ static int __devinit dataflash_probe(struct spi_device *spi)
661697 status = add_dataflash (spi , "AT45DB021B" , 1024 , 264 , 9 );
662698 break ;
663699 case 0x1c : /* 0 1 1 1 x x */
664- status = add_dataflash (spi , "AT45DB041B " , 2048 , 264 , 9 );
700+ status = add_dataflash (spi , "AT45DB041x " , 2048 , 264 , 9 );
665701 break ;
666702 case 0x24 : /* 1 0 0 1 x x */
667703 status = add_dataflash (spi , "AT45DB081B" , 4096 , 264 , 9 );
668704 break ;
669705 case 0x2c : /* 1 0 1 1 x x */
670- status = add_dataflash (spi , "AT45DB161B " , 4096 , 528 , 10 );
706+ status = add_dataflash (spi , "AT45DB161x " , 4096 , 528 , 10 );
671707 break ;
672708 case 0x34 : /* 1 1 0 1 x x */
673709 status = add_dataflash (spi , "AT45DB321x" , 8192 , 528 , 10 );
0 commit comments