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

mtd: cfi_cmdset_0002: Add support for polling status register

HyperFlash devices are compliant with CFI AMD/Fujitsu Extended Command
Set (0x0002) for flash operations, therefore
drivers/mtd/chips/cfi_cmdset_0002.c can be used as is. But these devices
do not support DQ polling method of determining chip ready/good status.
These flashes provide Status Register whose bits can be polled to know
status of flash operation.

Cypress HyperFlash datasheet here[1], talks about CFI Amd/Fujitsu
Extended Query version 1.5. Bit 0 of "Software Features supported" field
of CFI Primary Vendor-Specific Extended Query table indicates
presence/absence of status register and Bit 1 indicates whether or not
DQ polling is supported. Using these bits, its possible to determine
whether flash supports DQ polling or need to use Status Register.

Add support for polling Status Register to know device ready/status of
erase/write operations when DQ polling is not supported.
Print error messages on erase/program failure by looking at related
Status Register bits.

[1] https://www.cypress.com/file/213346/download

Signed-off-by: Vignesh Raghavendra <vigneshr@ti.com>
Reviewed-by: Tokunori Ikegami <ikegami.t@gmail.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>

authored by

Vignesh Raghavendra and committed by
Miquel Raynal
4844ef80 99a125f8

+120 -17
+113 -17
drivers/mtd/chips/cfi_cmdset_0002.c
··· 49 49 #define SST49LF008A 0x005a 50 50 #define AT49BV6416 0x00d6 51 51 52 + /* 53 + * Status Register bit description. Used by flash devices that don't 54 + * support DQ polling (e.g. HyperFlash) 55 + */ 56 + #define CFI_SR_DRB BIT(7) 57 + #define CFI_SR_ESB BIT(5) 58 + #define CFI_SR_PSB BIT(4) 59 + #define CFI_SR_WBASB BIT(3) 60 + #define CFI_SR_SLSB BIT(1) 61 + 52 62 static int cfi_amdstd_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *); 53 63 static int cfi_amdstd_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); 54 64 static int cfi_amdstd_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *); ··· 107 97 .module = THIS_MODULE 108 98 }; 109 99 100 + /* 101 + * Use status register to poll for Erase/write completion when DQ is not 102 + * supported. This is indicated by Bit[1:0] of SoftwareFeatures field in 103 + * CFI Primary Vendor-Specific Extended Query table 1.5 104 + */ 105 + static int cfi_use_status_reg(struct cfi_private *cfi) 106 + { 107 + struct cfi_pri_amdstd *extp = cfi->cmdset_priv; 108 + u8 poll_mask = CFI_POLL_STATUS_REG | CFI_POLL_DQ; 109 + 110 + return extp->MinorVersion >= '5' && 111 + (extp->SoftwareFeatures & poll_mask) == CFI_POLL_STATUS_REG; 112 + } 113 + 114 + static void cfi_check_err_status(struct map_info *map, struct flchip *chip, 115 + unsigned long adr) 116 + { 117 + struct cfi_private *cfi = map->fldrv_priv; 118 + map_word status; 119 + 120 + if (!cfi_use_status_reg(cfi)) 121 + return; 122 + 123 + cfi_send_gen_cmd(0x70, cfi->addr_unlock1, chip->start, map, cfi, 124 + cfi->device_type, NULL); 125 + status = map_read(map, adr); 126 + 127 + if (map_word_bitsset(map, status, CMD(0x3a))) { 128 + unsigned long chipstatus = MERGESTATUS(status); 129 + 130 + if (chipstatus & CFI_SR_ESB) 131 + pr_err("%s erase operation failed, status %lx\n", 132 + map->name, chipstatus); 133 + if (chipstatus & CFI_SR_PSB) 134 + pr_err("%s program operation failed, status %lx\n", 135 + map->name, chipstatus); 136 + if (chipstatus & CFI_SR_WBASB) 137 + pr_err("%s buffer program command aborted, status %lx\n", 138 + map->name, chipstatus); 139 + if (chipstatus & CFI_SR_SLSB) 140 + pr_err("%s sector write protected, status %lx\n", 141 + map->name, chipstatus); 142 + } 143 + } 110 144 111 145 /* #define DEBUG_CFI_FEATURES */ 112 146 ··· 796 742 * correctly and is therefore not done (particularly with interleaved chips 797 743 * as each chip must be checked independently of the others). 798 744 */ 799 - static int __xipram chip_ready(struct map_info *map, unsigned long addr) 745 + static int __xipram chip_ready(struct map_info *map, struct flchip *chip, 746 + unsigned long addr) 800 747 { 748 + struct cfi_private *cfi = map->fldrv_priv; 801 749 map_word d, t; 750 + 751 + if (cfi_use_status_reg(cfi)) { 752 + map_word ready = CMD(CFI_SR_DRB); 753 + /* 754 + * For chips that support status register, check device 755 + * ready bit 756 + */ 757 + cfi_send_gen_cmd(0x70, cfi->addr_unlock1, chip->start, map, cfi, 758 + cfi->device_type, NULL); 759 + d = map_read(map, addr); 760 + 761 + return map_word_andequal(map, d, ready, ready); 762 + } 802 763 803 764 d = map_read(map, addr); 804 765 t = map_read(map, addr); ··· 836 767 * as each chip must be checked independently of the others). 837 768 * 838 769 */ 839 - static int __xipram chip_good(struct map_info *map, unsigned long addr, map_word expected) 770 + static int __xipram chip_good(struct map_info *map, struct flchip *chip, 771 + unsigned long addr, map_word expected) 840 772 { 773 + struct cfi_private *cfi = map->fldrv_priv; 841 774 map_word oldd, curd; 775 + 776 + if (cfi_use_status_reg(cfi)) { 777 + map_word ready = CMD(CFI_SR_DRB); 778 + map_word err = CMD(CFI_SR_PSB | CFI_SR_ESB); 779 + /* 780 + * For chips that support status register, check device 781 + * ready bit and Erase/Program status bit to know if 782 + * operation succeeded. 783 + */ 784 + cfi_send_gen_cmd(0x70, cfi->addr_unlock1, chip->start, map, cfi, 785 + cfi->device_type, NULL); 786 + curd = map_read(map, addr); 787 + 788 + if (map_word_andequal(map, curd, ready, ready)) 789 + return !map_word_bitsset(map, curd, err); 790 + 791 + return 0; 792 + } 842 793 843 794 oldd = map_read(map, addr); 844 795 curd = map_read(map, addr); ··· 881 792 882 793 case FL_STATUS: 883 794 for (;;) { 884 - if (chip_ready(map, adr)) 795 + if (chip_ready(map, chip, adr)) 885 796 break; 886 797 887 798 if (time_after(jiffies, timeo)) { ··· 919 830 chip->state = FL_ERASE_SUSPENDING; 920 831 chip->erase_suspended = 1; 921 832 for (;;) { 922 - if (chip_ready(map, adr)) 833 + if (chip_ready(map, chip, adr)) 923 834 break; 924 835 925 836 if (time_after(jiffies, timeo)) { ··· 1451 1362 /* wait for chip to become ready */ 1452 1363 timeo = jiffies + msecs_to_jiffies(2); 1453 1364 for (;;) { 1454 - if (chip_ready(map, adr)) 1365 + if (chip_ready(map, chip, adr)) 1455 1366 break; 1456 1367 1457 1368 if (time_after(jiffies, timeo)) { ··· 1717 1628 continue; 1718 1629 } 1719 1630 1720 - if (time_after(jiffies, timeo) && !chip_ready(map, adr)){ 1631 + if (time_after(jiffies, timeo) && 1632 + !chip_ready(map, chip, adr)) { 1721 1633 xip_enable(map, chip, adr); 1722 1634 printk(KERN_WARNING "MTD %s(): software timeout\n", __func__); 1723 1635 xip_disable(map, chip, adr); 1724 1636 break; 1725 1637 } 1726 1638 1727 - if (chip_ready(map, adr)) 1639 + if (chip_ready(map, chip, adr)) 1728 1640 break; 1729 1641 1730 1642 /* Latency issues. Drop the lock, wait a while and retry */ 1731 1643 UDELAY(map, chip, adr, 1); 1732 1644 } 1733 1645 /* Did we succeed? */ 1734 - if (!chip_good(map, adr, datum)) { 1646 + if (!chip_good(map, chip, adr, datum)) { 1735 1647 /* reset on all failures. */ 1648 + cfi_check_err_status(map, chip, adr); 1736 1649 map_write(map, CMD(0xF0), chip->start); 1737 1650 /* FIXME - should have reset delay before continuing */ 1738 1651 ··· 1972 1881 * We check "time_after" and "!chip_good" before checking "chip_good" to avoid 1973 1882 * the failure due to scheduling. 1974 1883 */ 1975 - if (time_after(jiffies, timeo) && !chip_good(map, adr, datum)) 1884 + if (time_after(jiffies, timeo) && 1885 + !chip_good(map, chip, adr, datum)) 1976 1886 break; 1977 1887 1978 - if (chip_good(map, adr, datum)) { 1888 + if (chip_good(map, chip, adr, datum)) { 1979 1889 xip_enable(map, chip, adr); 1980 1890 goto op_done; 1981 1891 } ··· 1993 1901 * See e.g. 1994 1902 * http://www.spansion.com/Support/Application%20Notes/MirrorBit_Write_Buffer_Prog_Page_Buffer_Read_AN.pdf 1995 1903 */ 1904 + cfi_check_err_status(map, chip, adr); 1996 1905 cfi_send_gen_cmd(0xAA, cfi->addr_unlock1, chip->start, map, cfi, 1997 1906 cfi->device_type, NULL); 1998 1907 cfi_send_gen_cmd(0x55, cfi->addr_unlock2, chip->start, map, cfi, ··· 2111 2018 * If the driver thinks the chip is idle, and no toggle bits 2112 2019 * are changing, then the chip is actually idle for sure. 2113 2020 */ 2114 - if (chip->state == FL_READY && chip_ready(map, adr)) 2021 + if (chip->state == FL_READY && chip_ready(map, chip, adr)) 2115 2022 return 0; 2116 2023 2117 2024 /* ··· 2128 2035 2129 2036 /* wait for the chip to become ready */ 2130 2037 for (i = 0; i < jiffies_to_usecs(timeo); i++) { 2131 - if (chip_ready(map, adr)) 2038 + if (chip_ready(map, chip, adr)) 2132 2039 return 0; 2133 2040 2134 2041 udelay(1); ··· 2192 2099 map_write(map, datum, adr); 2193 2100 2194 2101 for (i = 0; i < jiffies_to_usecs(uWriteTimeout); i++) { 2195 - if (chip_ready(map, adr)) 2102 + if (chip_ready(map, chip, adr)) 2196 2103 break; 2197 2104 2198 2105 udelay(1); 2199 2106 } 2200 2107 2201 - if (!chip_good(map, adr, datum)) { 2108 + if (!chip_good(map, chip, adr, datum)) { 2202 2109 /* reset on all failures. */ 2110 + cfi_check_err_status(map, chip, adr); 2203 2111 map_write(map, CMD(0xF0), chip->start); 2204 2112 /* FIXME - should have reset delay before continuing */ 2205 2113 ··· 2394 2300 chip->erase_suspended = 0; 2395 2301 } 2396 2302 2397 - if (chip_good(map, adr, map_word_ff(map))) 2303 + if (chip_good(map, chip, adr, map_word_ff(map))) 2398 2304 break; 2399 2305 2400 2306 if (time_after(jiffies, timeo)) { ··· 2410 2316 /* Did we succeed? */ 2411 2317 if (ret) { 2412 2318 /* reset on all failures. */ 2319 + cfi_check_err_status(map, chip, adr); 2413 2320 map_write(map, CMD(0xF0), chip->start); 2414 2321 /* FIXME - should have reset delay before continuing */ 2415 2322 ··· 2491 2396 chip->erase_suspended = 0; 2492 2397 } 2493 2398 2494 - if (chip_good(map, adr, map_word_ff(map))) 2399 + if (chip_good(map, chip, adr, map_word_ff(map))) 2495 2400 break; 2496 2401 2497 2402 if (time_after(jiffies, timeo)) { ··· 2507 2412 /* Did we succeed? */ 2508 2413 if (ret) { 2509 2414 /* reset on all failures. */ 2415 + cfi_check_err_status(map, chip, adr); 2510 2416 map_write(map, CMD(0xF0), chip->start); 2511 2417 /* FIXME - should have reset delay before continuing */ 2512 2418 ··· 2683 2587 */ 2684 2588 timeo = jiffies + msecs_to_jiffies(2000); /* 2s max (un)locking */ 2685 2589 for (;;) { 2686 - if (chip_ready(map, adr)) 2590 + if (chip_ready(map, chip, adr)) 2687 2591 break; 2688 2592 2689 2593 if (time_after(jiffies, timeo)) {
+7
include/linux/mtd/cfi.h
··· 233 233 uint8_t VppMin; 234 234 uint8_t VppMax; 235 235 uint8_t TopBottom; 236 + /* Below field are added from version 1.5 */ 237 + uint8_t ProgramSuspend; 238 + uint8_t UnlockBypass; 239 + uint8_t SecureSiliconSector; 240 + uint8_t SoftwareFeatures; 241 + #define CFI_POLL_STATUS_REG BIT(0) 242 + #define CFI_POLL_DQ BIT(1) 236 243 } __packed; 237 244 238 245 /* Vendor-Specific PRI for Atmel chips (command set 0x0002) */