062-v4.11-0005-mtd-spi-nor-add-a-stateless-method-to-support-memory.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. From 3274ba26f27becfc4193ec6e229288140651f240 Mon Sep 17 00:00:00 2001
  2. From: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  3. Date: Thu, 27 Oct 2016 12:03:57 +0200
  4. Subject: [PATCH] mtd: spi-nor: add a stateless method to support memory size
  5. above 128Mib
  6. This patch provides an alternative mean to support memory above 16MiB
  7. (128Mib) by replacing 3byte address op codes by their associated 4byte
  8. address versions.
  9. Using the dedicated 4byte address op codes doesn't change the internal
  10. state of the SPI NOR memory as opposed to using other means such as
  11. updating a Base Address Register (BAR) and sending command to enter/leave
  12. the 4byte mode.
  13. Hence when a CPU reset occurs, early bootloaders don't need to be aware
  14. of BAR value or 4byte mode being enabled: they can still access the first
  15. 16MiB of the SPI NOR memory using the regular 3byte address op codes.
  16. Signed-off-by: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  17. Tested-by: Vignesh R <vigneshr@ti.com>
  18. Acked-by: Marek Vasut <marek.vasut@gmail.com>
  19. ---
  20. drivers/mtd/spi-nor/spi-nor.c | 101 +++++++++++++++++++++++++++++++++---------
  21. 1 file changed, 80 insertions(+), 21 deletions(-)
  22. --- a/drivers/mtd/spi-nor/spi-nor.c
  23. +++ b/drivers/mtd/spi-nor/spi-nor.c
  24. @@ -81,6 +81,10 @@ struct flash_info {
  25. * because it has the same value as
  26. * ATMEL flashes)
  27. */
  28. +#define SPI_NOR_4B_OPCODES BIT(11) /*
  29. + * Use dedicated 4byte address op codes
  30. + * to support memory size above 128Mib.
  31. + */
  32. };
  33. #define JEDEC_MFR(info) ((info)->id[0])
  34. @@ -194,6 +198,78 @@ static inline struct spi_nor *mtd_to_spi
  35. return mtd->priv;
  36. }
  37. +
  38. +static u8 spi_nor_convert_opcode(u8 opcode, const u8 table[][2], size_t size)
  39. +{
  40. + size_t i;
  41. +
  42. + for (i = 0; i < size; i++)
  43. + if (table[i][0] == opcode)
  44. + return table[i][1];
  45. +
  46. + /* No conversion found, keep input op code. */
  47. + return opcode;
  48. +}
  49. +
  50. +static inline u8 spi_nor_convert_3to4_read(u8 opcode)
  51. +{
  52. + static const u8 spi_nor_3to4_read[][2] = {
  53. + { SPINOR_OP_READ, SPINOR_OP_READ_4B },
  54. + { SPINOR_OP_READ_FAST, SPINOR_OP_READ_FAST_4B },
  55. + { SPINOR_OP_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B },
  56. + { SPINOR_OP_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B },
  57. + { SPINOR_OP_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B },
  58. + { SPINOR_OP_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B },
  59. + };
  60. +
  61. + return spi_nor_convert_opcode(opcode, spi_nor_3to4_read,
  62. + ARRAY_SIZE(spi_nor_3to4_read));
  63. +}
  64. +
  65. +static inline u8 spi_nor_convert_3to4_program(u8 opcode)
  66. +{
  67. + static const u8 spi_nor_3to4_program[][2] = {
  68. + { SPINOR_OP_PP, SPINOR_OP_PP_4B },
  69. + { SPINOR_OP_PP_1_1_4, SPINOR_OP_PP_1_1_4_4B },
  70. + { SPINOR_OP_PP_1_4_4, SPINOR_OP_PP_1_4_4_4B },
  71. + };
  72. +
  73. + return spi_nor_convert_opcode(opcode, spi_nor_3to4_program,
  74. + ARRAY_SIZE(spi_nor_3to4_program));
  75. +}
  76. +
  77. +static inline u8 spi_nor_convert_3to4_erase(u8 opcode)
  78. +{
  79. + static const u8 spi_nor_3to4_erase[][2] = {
  80. + { SPINOR_OP_BE_4K, SPINOR_OP_BE_4K_4B },
  81. + { SPINOR_OP_BE_32K, SPINOR_OP_BE_32K_4B },
  82. + { SPINOR_OP_SE, SPINOR_OP_SE_4B },
  83. + };
  84. +
  85. + return spi_nor_convert_opcode(opcode, spi_nor_3to4_erase,
  86. + ARRAY_SIZE(spi_nor_3to4_erase));
  87. +}
  88. +
  89. +static void spi_nor_set_4byte_opcodes(struct spi_nor *nor,
  90. + const struct flash_info *info)
  91. +{
  92. + /* Do some manufacturer fixups first */
  93. + switch (JEDEC_MFR(info)) {
  94. + case SNOR_MFR_SPANSION:
  95. + /* No small sector erase for 4-byte command set */
  96. + nor->erase_opcode = SPINOR_OP_SE;
  97. + nor->mtd.erasesize = info->sector_size;
  98. + break;
  99. +
  100. + default:
  101. + break;
  102. + }
  103. +
  104. + nor->read_opcode = spi_nor_convert_3to4_read(nor->read_opcode);
  105. + nor->program_opcode = spi_nor_convert_3to4_program(nor->program_opcode);
  106. + nor->erase_opcode = spi_nor_convert_3to4_erase(nor->erase_opcode);
  107. +}
  108. +
  109. /* Enable/disable 4-byte addressing mode. */
  110. static inline int set_4byte(struct spi_nor *nor, const struct flash_info *info,
  111. int enable)
  112. @@ -1628,27 +1704,10 @@ int spi_nor_scan(struct spi_nor *nor, co
  113. else if (mtd->size > 0x1000000) {
  114. /* enable 4-byte addressing if the device exceeds 16MiB */
  115. nor->addr_width = 4;
  116. - if (JEDEC_MFR(info) == SNOR_MFR_SPANSION) {
  117. - /* Dedicated 4-byte command set */
  118. - switch (nor->flash_read) {
  119. - case SPI_NOR_QUAD:
  120. - nor->read_opcode = SPINOR_OP_READ_1_1_4_4B;
  121. - break;
  122. - case SPI_NOR_DUAL:
  123. - nor->read_opcode = SPINOR_OP_READ_1_1_2_4B;
  124. - break;
  125. - case SPI_NOR_FAST:
  126. - nor->read_opcode = SPINOR_OP_READ_FAST_4B;
  127. - break;
  128. - case SPI_NOR_NORMAL:
  129. - nor->read_opcode = SPINOR_OP_READ_4B;
  130. - break;
  131. - }
  132. - nor->program_opcode = SPINOR_OP_PP_4B;
  133. - /* No small sector erase for 4-byte command set */
  134. - nor->erase_opcode = SPINOR_OP_SE_4B;
  135. - mtd->erasesize = info->sector_size;
  136. - } else
  137. + if (JEDEC_MFR(info) == SNOR_MFR_SPANSION ||
  138. + info->flags & SPI_NOR_4B_OPCODES)
  139. + spi_nor_set_4byte_opcodes(nor, info);
  140. + else
  141. set_4byte(nor, info, 1);
  142. } else {
  143. nor->addr_width = 3;