1081-mtd-spi-nor-provide-default-erase_sector-implementat.patch 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. From 56bd0e13d8bc3b4486251b10ac9d2ba7434c21ee Mon Sep 17 00:00:00 2001
  2. From: Brian Norris <computersforpeace@gmail.com>
  3. Date: Tue, 10 Nov 2015 12:15:27 -0800
  4. Subject: [PATCH 081/113] mtd: spi-nor: provide default erase_sector
  5. implementation
  6. Some spi-nor drivers perform sector erase by duplicating their
  7. write_reg() command. Let's not require that the driver fill this out,
  8. and provide a default instead.
  9. Tested on m25p80.c and Medatek's MT8173 SPI NOR flash driver.
  10. Signed-off-by: Brian Norris <computersforpeace@gmail.com>
  11. ---
  12. drivers/mtd/spi-nor/spi-nor.c | 37 +++++++++++++++++++++++++++++++++----
  13. include/linux/mtd/spi-nor.h | 3 ++-
  14. 2 files changed, 35 insertions(+), 5 deletions(-)
  15. --- a/drivers/mtd/spi-nor/spi-nor.c
  16. +++ b/drivers/mtd/spi-nor/spi-nor.c
  17. @@ -38,6 +38,7 @@
  18. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  19. #define SPI_NOR_MAX_ID_LEN 6
  20. +#define SPI_NOR_MAX_ADDR_WIDTH 4
  21. struct flash_info {
  22. char *name;
  23. @@ -314,6 +315,29 @@ static void spi_nor_unlock_and_unprep(st
  24. }
  25. /*
  26. + * Initiate the erasure of a single sector
  27. + */
  28. +static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  29. +{
  30. + u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  31. + int i;
  32. +
  33. + if (nor->erase)
  34. + return nor->erase(nor, addr);
  35. +
  36. + /*
  37. + * Default implementation, if driver doesn't have a specialized HW
  38. + * control
  39. + */
  40. + for (i = nor->addr_width - 1; i >= 0; i--) {
  41. + buf[i] = addr & 0xff;
  42. + addr >>= 8;
  43. + }
  44. +
  45. + return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  46. +}
  47. +
  48. +/*
  49. * Erase an address range on the nor chip. The address range may extend
  50. * one or more erase sectors. Return an error is there is a problem erasing.
  51. */
  52. @@ -372,10 +396,9 @@ static int spi_nor_erase(struct mtd_info
  53. while (len) {
  54. write_enable(nor);
  55. - if (nor->erase(nor, addr)) {
  56. - ret = -EIO;
  57. + ret = spi_nor_erase_sector(nor, addr);
  58. + if (ret)
  59. goto erase_err;
  60. - }
  61. addr += mtd->erasesize;
  62. len -= mtd->erasesize;
  63. @@ -1113,7 +1136,7 @@ static int set_quad_mode(struct spi_nor
  64. static int spi_nor_check(struct spi_nor *nor)
  65. {
  66. if (!nor->dev || !nor->read || !nor->write ||
  67. - !nor->read_reg || !nor->write_reg || !nor->erase) {
  68. + !nor->read_reg || !nor->write_reg) {
  69. pr_err("spi-nor: please fill all the necessary fields!\n");
  70. return -EINVAL;
  71. }
  72. @@ -1316,6 +1339,12 @@ int spi_nor_scan(struct spi_nor *nor, co
  73. nor->addr_width = 3;
  74. }
  75. + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  76. + dev_err(dev, "address width is too large: %u\n",
  77. + nor->addr_width);
  78. + return -EINVAL;
  79. + }
  80. +
  81. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  82. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  83. --- a/include/linux/mtd/spi-nor.h
  84. +++ b/include/linux/mtd/spi-nor.h
  85. @@ -142,7 +142,8 @@ enum spi_nor_option_flags {
  86. * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
  87. * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
  88. * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
  89. - * at the offset @offs
  90. + * at the offset @offs; if not provided by the driver,
  91. + * spi-nor will send the erase opcode via write_reg()
  92. * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
  93. * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
  94. * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is