Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[MTD] dataflash OTP support

Now that we can tell when we have one of the newer DataFlash chips,
optionally expose the 128 bytes of OTP memory they provide. Tested
on at45db642 revision B and D chips.

Switch mtdchar over to a generic HAVE_MTD_OTP flag instead of adding
another #ifdef for each type of chip whose driver has OTP support.

Signed-off-by: David Brownell <dbrownell@users.sourceforge.net>
Cc: Bryan Wu <cooloney@kernel.org>
Cc: Michael Hennerich <michael.hennerich@analog.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>

authored by

David Brownell and committed by
David Woodhouse
34a82443 623fa579

+219 -9
+5
drivers/mtd/Kconfig
··· 172 172 memory chips, and also use ioctl() to obtain information about 173 173 the device, or to erase parts of it. 174 174 175 + config HAVE_MTD_OTP 176 + bool 177 + help 178 + Enable access to OTP regions using MTD_CHAR. 179 + 175 180 config MTD_BLKDEVS 176 181 tristate "Common interface to block layer for MTD 'translation layers'" 177 182 depends on BLOCK
+1
drivers/mtd/chips/Kconfig
··· 154 154 config MTD_OTP 155 155 bool "Protection Registers aka one-time programmable (OTP) bits" 156 156 depends on MTD_CFI_ADV_OPTIONS 157 + select HAVE_MTD_OTP 157 158 default n 158 159 help 159 160 This enables support for reading, writing and locking so called
+11
drivers/mtd/devices/Kconfig
··· 59 59 Sometimes DataFlash chips are packaged inside MMC-format 60 60 cards; at this writing, the MMC stack won't handle those. 61 61 62 + config MTD_DATAFLASH_OTP 63 + bool "DataFlash OTP support (Security Register)" 64 + depends on MTD_DATAFLASH 65 + select HAVE_MTD_OTP 66 + help 67 + Newer DataFlash chips (revisions C and D) support 128 bytes of 68 + one-time-programmable (OTP) data. The first half may be written 69 + (once) with up to 64 bytes of data, such as a serial number or 70 + other key product data. The second half is programmed with a 71 + unique-to-each-chip bit pattern at the factory. 72 + 62 73 config MTD_M25P80 63 74 tristate "Support most SPI Flash chips (AT26DF, M25P, W25X, ...)" 64 75 depends on SPI_MASTER && EXPERIMENTAL
+199 -7
drivers/mtd/devices/mtd_dataflash.c
··· 80 80 */ 81 81 #define OP_READ_ID 0x9F 82 82 #define OP_READ_SECURITY 0x77 83 - #define OP_WRITE_SECURITY 0x9A /* OTP bits */ 83 + #define OP_WRITE_SECURITY_REVC 0x9A 84 + #define OP_WRITE_SECURITY 0x9B /* revision D */ 84 85 85 86 86 87 struct dataflash { ··· 452 451 453 452 /* ......................................................................... */ 454 453 454 + #ifdef CONFIG_MTD_DATAFLASH_OTP 455 + 456 + static int dataflash_get_otp_info(struct mtd_info *mtd, 457 + struct otp_info *info, size_t len) 458 + { 459 + /* Report both blocks as identical: bytes 0..64, locked. 460 + * Unless the user block changed from all-ones, we can't 461 + * tell whether it's still writable; so we assume it isn't. 462 + */ 463 + info->start = 0; 464 + info->length = 64; 465 + info->locked = 1; 466 + return sizeof(*info); 467 + } 468 + 469 + static ssize_t otp_read(struct spi_device *spi, unsigned base, 470 + uint8_t *buf, loff_t off, size_t len) 471 + { 472 + struct spi_message m; 473 + size_t l; 474 + uint8_t *scratch; 475 + struct spi_transfer t; 476 + int status; 477 + 478 + if (off > 64) 479 + return -EINVAL; 480 + 481 + if ((off + len) > 64) 482 + len = 64 - off; 483 + if (len == 0) 484 + return len; 485 + 486 + spi_message_init(&m); 487 + 488 + l = 4 + base + off + len; 489 + scratch = kzalloc(l, GFP_KERNEL); 490 + if (!scratch) 491 + return -ENOMEM; 492 + 493 + /* OUT: OP_READ_SECURITY, 3 don't-care bytes, zeroes 494 + * IN: ignore 4 bytes, data bytes 0..N (max 127) 495 + */ 496 + scratch[0] = OP_READ_SECURITY; 497 + 498 + memset(&t, 0, sizeof t); 499 + t.tx_buf = scratch; 500 + t.rx_buf = scratch; 501 + t.len = l; 502 + spi_message_add_tail(&t, &m); 503 + 504 + dataflash_waitready(spi); 505 + 506 + status = spi_sync(spi, &m); 507 + if (status >= 0) { 508 + memcpy(buf, scratch + 4 + base + off, len); 509 + status = len; 510 + } 511 + 512 + kfree(scratch); 513 + return status; 514 + } 515 + 516 + static int dataflash_read_fact_otp(struct mtd_info *mtd, 517 + loff_t from, size_t len, size_t *retlen, u_char *buf) 518 + { 519 + struct dataflash *priv = (struct dataflash *)mtd->priv; 520 + int status; 521 + 522 + /* 64 bytes, from 0..63 ... start at 64 on-chip */ 523 + mutex_lock(&priv->lock); 524 + status = otp_read(priv->spi, 64, buf, from, len); 525 + mutex_unlock(&priv->lock); 526 + 527 + if (status < 0) 528 + return status; 529 + *retlen = status; 530 + return 0; 531 + } 532 + 533 + static int dataflash_read_user_otp(struct mtd_info *mtd, 534 + loff_t from, size_t len, size_t *retlen, u_char *buf) 535 + { 536 + struct dataflash *priv = (struct dataflash *)mtd->priv; 537 + int status; 538 + 539 + /* 64 bytes, from 0..63 ... start at 0 on-chip */ 540 + mutex_lock(&priv->lock); 541 + status = otp_read(priv->spi, 0, buf, from, len); 542 + mutex_unlock(&priv->lock); 543 + 544 + if (status < 0) 545 + return status; 546 + *retlen = status; 547 + return 0; 548 + } 549 + 550 + static int dataflash_write_user_otp(struct mtd_info *mtd, 551 + loff_t from, size_t len, size_t *retlen, u_char *buf) 552 + { 553 + struct spi_message m; 554 + const size_t l = 4 + 64; 555 + uint8_t *scratch; 556 + struct spi_transfer t; 557 + struct dataflash *priv = (struct dataflash *)mtd->priv; 558 + int status; 559 + 560 + if (len > 64) 561 + return -EINVAL; 562 + 563 + /* Strictly speaking, we *could* truncate the write ... but 564 + * let's not do that for the only write that's ever possible. 565 + */ 566 + if ((from + len) > 64) 567 + return -EINVAL; 568 + 569 + /* OUT: OP_WRITE_SECURITY, 3 zeroes, 64 data-or-zero bytes 570 + * IN: ignore all 571 + */ 572 + scratch = kzalloc(l, GFP_KERNEL); 573 + if (!scratch) 574 + return -ENOMEM; 575 + scratch[0] = OP_WRITE_SECURITY; 576 + memcpy(scratch + 4 + from, buf, len); 577 + 578 + spi_message_init(&m); 579 + 580 + memset(&t, 0, sizeof t); 581 + t.tx_buf = scratch; 582 + t.len = l; 583 + spi_message_add_tail(&t, &m); 584 + 585 + /* Write the OTP bits, if they've not yet been written. 586 + * This modifies SRAM buffer1. 587 + */ 588 + mutex_lock(&priv->lock); 589 + dataflash_waitready(priv->spi); 590 + status = spi_sync(priv->spi, &m); 591 + mutex_unlock(&priv->lock); 592 + 593 + kfree(scratch); 594 + 595 + if (status >= 0) { 596 + status = 0; 597 + *retlen = len; 598 + } 599 + return status; 600 + } 601 + 602 + static char *otp_setup(struct mtd_info *device, char revision) 603 + { 604 + device->get_fact_prot_info = dataflash_get_otp_info; 605 + device->read_fact_prot_reg = dataflash_read_fact_otp; 606 + device->get_user_prot_info = dataflash_get_otp_info; 607 + device->read_user_prot_reg = dataflash_read_user_otp; 608 + 609 + /* rev c parts (at45db321c and at45db1281 only!) use a 610 + * different write procedure; not (yet?) implemented. 611 + */ 612 + if (revision > 'c') 613 + device->write_user_prot_reg = dataflash_write_user_otp; 614 + 615 + return ", OTP"; 616 + } 617 + 618 + #else 619 + 620 + static char *otp_setup(struct mtd_info *device) 621 + { 622 + return " (OTP)"; 623 + } 624 + 625 + #endif 626 + 627 + /* ......................................................................... */ 628 + 455 629 /* 456 630 * Register DataFlash device with MTD subsystem. 457 631 */ 458 632 static int __devinit 459 - add_dataflash(struct spi_device *spi, char *name, 460 - int nr_pages, int pagesize, int pageoffset) 633 + add_dataflash_otp(struct spi_device *spi, char *name, 634 + int nr_pages, int pagesize, int pageoffset, char revision) 461 635 { 462 636 struct dataflash *priv; 463 637 struct mtd_info *device; 464 638 struct flash_platform_data *pdata = spi->dev.platform_data; 639 + char *otp_tag = ""; 465 640 466 641 priv = kzalloc(sizeof *priv, GFP_KERNEL); 467 642 if (!priv) ··· 666 489 device->write = dataflash_write; 667 490 device->priv = priv; 668 491 669 - dev_info(&spi->dev, "%s (%d KBytes) pagesize %d bytes\n", 670 - name, DIV_ROUND_UP(device->size, 1024), pagesize); 492 + if (revision >= 'c') 493 + otp_tag = otp_setup(device, revision); 494 + 495 + dev_info(&spi->dev, "%s (%d KBytes) pagesize %d bytes%s\n", 496 + name, DIV_ROUND_UP(device->size, 1024), 497 + pagesize, otp_tag); 671 498 dev_set_drvdata(&spi->dev, priv); 672 499 673 500 if (mtd_has_partitions()) { ··· 698 517 pdata->nr_parts, device->name); 699 518 700 519 return add_mtd_device(device) == 1 ? -ENODEV : 0; 520 + } 521 + 522 + static inline int __devinit 523 + add_dataflash(struct spi_device *spi, char *name, 524 + int nr_pages, int pagesize, int pageoffset) 525 + { 526 + return add_dataflash_otp(spi, name, nr_pages, pagesize, 527 + pageoffset, 0); 701 528 } 702 529 703 530 struct flash_info { ··· 853 664 * Try to detect dataflash by JEDEC ID. 854 665 * If it succeeds we know we have either a C or D part. 855 666 * D will support power of 2 pagesize option. 667 + * Both support the security register, though with different 668 + * write procedures. 856 669 */ 857 670 info = jedec_probe(spi); 858 671 if (IS_ERR(info)) 859 672 return PTR_ERR(info); 860 673 if (info != NULL) 861 - return add_dataflash(spi, info->name, info->nr_pages, 862 - info->pagesize, info->pageoffset); 674 + return add_dataflash_otp(spi, info->name, info->nr_pages, 675 + info->pagesize, info->pageoffset, 676 + (info->flags & SUP_POW2PS) ? 'd' : 'c'); 863 677 864 678 /* 865 679 * Older chips support only legacy commands, identifing
+2 -2
drivers/mtd/mtdchar.c
··· 350 350 wake_up((wait_queue_head_t *)instr->priv); 351 351 } 352 352 353 - #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP) 353 + #ifdef CONFIG_HAVE_MTD_OTP 354 354 static int otp_select_filemode(struct mtd_file_info *mfi, int mode) 355 355 { 356 356 struct mtd_info *mtd = mfi->mtd; ··· 663 663 break; 664 664 } 665 665 666 - #if defined(CONFIG_MTD_OTP) || defined(CONFIG_MTD_ONENAND_OTP) 666 + #ifdef CONFIG_HAVE_MTD_OTP 667 667 case OTPSELECT: 668 668 { 669 669 int mode;
+1
drivers/mtd/onenand/Kconfig
··· 29 29 30 30 config MTD_ONENAND_OTP 31 31 bool "OneNAND OTP Support" 32 + select HAVE_MTD_OTP 32 33 help 33 34 One Block of the NAND Flash Array memory is reserved as 34 35 a One-Time Programmable Block memory area.