0011-mtd-spi-nor-provide-default-erase_sector-implementat.patch 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. From 29675718c3880cbe7a8d8c6819c07dcec656c544 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 11/33] 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. diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
  16. index 107571e..0d2be38 100644
  17. --- a/drivers/mtd/spi-nor/spi-nor.c
  18. +++ b/drivers/mtd/spi-nor/spi-nor.c
  19. @@ -38,6 +38,7 @@
  20. #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
  21. #define SPI_NOR_MAX_ID_LEN 6
  22. +#define SPI_NOR_MAX_ADDR_WIDTH 4
  23. struct flash_info {
  24. char *name;
  25. @@ -313,6 +314,29 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  26. }
  27. /*
  28. + * Initiate the erasure of a single sector
  29. + */
  30. +static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
  31. +{
  32. + u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
  33. + int i;
  34. +
  35. + if (nor->erase)
  36. + return nor->erase(nor, addr);
  37. +
  38. + /*
  39. + * Default implementation, if driver doesn't have a specialized HW
  40. + * control
  41. + */
  42. + for (i = nor->addr_width - 1; i >= 0; i--) {
  43. + buf[i] = addr & 0xff;
  44. + addr >>= 8;
  45. + }
  46. +
  47. + return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
  48. +}
  49. +
  50. +/*
  51. * Erase an address range on the nor chip. The address range may extend
  52. * one or more erase sectors. Return an error is there is a problem erasing.
  53. */
  54. @@ -371,10 +395,9 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  55. while (len) {
  56. write_enable(nor);
  57. - if (nor->erase(nor, addr)) {
  58. - ret = -EIO;
  59. + ret = spi_nor_erase_sector(nor, addr);
  60. + if (ret)
  61. goto erase_err;
  62. - }
  63. addr += mtd->erasesize;
  64. len -= mtd->erasesize;
  65. @@ -1138,7 +1161,7 @@ static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
  66. static int spi_nor_check(struct spi_nor *nor)
  67. {
  68. if (!nor->dev || !nor->read || !nor->write ||
  69. - !nor->read_reg || !nor->write_reg || !nor->erase) {
  70. + !nor->read_reg || !nor->write_reg) {
  71. pr_err("spi-nor: please fill all the necessary fields!\n");
  72. return -EINVAL;
  73. }
  74. @@ -1338,6 +1361,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  75. nor->addr_width = 3;
  76. }
  77. + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
  78. + dev_err(dev, "address width is too large: %u\n",
  79. + nor->addr_width);
  80. + return -EINVAL;
  81. + }
  82. +
  83. nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  84. dev_info(dev, "%s (%lld Kbytes)\n", info->name,
  85. diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
  86. index 592420b..62356d5 100644
  87. --- a/include/linux/mtd/spi-nor.h
  88. +++ b/include/linux/mtd/spi-nor.h
  89. @@ -142,7 +142,8 @@ enum spi_nor_option_flags {
  90. * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
  91. * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
  92. * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
  93. - * at the offset @offs
  94. + * at the offset @offs; if not provided by the driver,
  95. + * spi-nor will send the erase opcode via write_reg()
  96. * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
  97. * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
  98. * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is
  99. --
  100. 2.8.1