0017-mtd-spi-nor-properly-detect-the-memory-when-it-boots.patch 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. From 06883bee58d1beedef783f7f5662367736d90924 Mon Sep 17 00:00:00 2001
  2. From: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  3. Date: Fri, 8 Jan 2016 17:02:14 +0100
  4. Subject: [PATCH 17/33] mtd: spi-nor: properly detect the memory when it boots
  5. in Quad or Dual mode
  6. The quad (or dual) mode of a spi-nor memory may be enabled at boot time by
  7. non-volatile bits in some setting register. Also such a mode may have
  8. already been enabled at early stage by some boot loader.
  9. Hence, we should not guess the spi-nor memory is always configured for the
  10. regular SPI 1-1-1 protocol.
  11. Micron and Macronix memories, once their Quad (or dual for Micron) mode
  12. enabled, no longer process the regular JEDEC Read ID (0x9f) command but
  13. instead reply to a new command: JEDEC Read ID Multiple I/O (0xaf).
  14. Besides, in Quad mode both memory manufacturers expect ALL commands to
  15. use the SPI 4-4-4 protocol. For Micron memories, enabling their Dual mode
  16. implies to use the SPI 2-2-2 protocol for ALL commands.
  17. Winbond memories, once their Quad mode enabled, expect ALL commands to use
  18. the SPI 4-4-4 protocol. Unlike Micron and Macronix memories, they still
  19. reply to the regular JEDEC Read ID (0x9f) command.
  20. Signed-off-by: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  21. ---
  22. drivers/mtd/spi-nor/spi-nor.c | 83 ++++++++++++++++++++++++++++++++++++++++---
  23. include/linux/mtd/spi-nor.h | 50 ++++++++++++++++++++++++--
  24. 2 files changed, 127 insertions(+), 6 deletions(-)
  25. diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
  26. index 6e72e96..9ad2d40 100644
  27. --- a/drivers/mtd/spi-nor/spi-nor.c
  28. +++ b/drivers/mtd/spi-nor/spi-nor.c
  29. @@ -73,6 +73,12 @@ struct flash_info {
  30. #define JEDEC_MFR(info) ((info)->id[0])
  31. +struct read_id_config {
  32. + enum read_mode mode;
  33. + enum spi_nor_protocol proto;
  34. + u8 opcode;
  35. +};
  36. +
  37. static const struct flash_info *spi_nor_match_id(const char *name);
  38. /*
  39. @@ -871,11 +877,22 @@ static const struct flash_info spi_nor_ids[] = {
  40. { },
  41. };
  42. -static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
  43. +static const struct flash_info *spi_nor_read_id(struct spi_nor *nor,
  44. + enum read_mode mode)
  45. {
  46. - int tmp;
  47. + int i, tmp;
  48. u8 id[SPI_NOR_MAX_ID_LEN];
  49. const struct flash_info *info;
  50. + static const struct read_id_config configs[] = {
  51. + /* Winbond QPI mode */
  52. + {SPI_NOR_QUAD, SNOR_PROTO_4_4_4, SPINOR_OP_RDID},
  53. +
  54. + /* Micron Quad mode & Macronix QPI mode */
  55. + {SPI_NOR_QUAD, SNOR_PROTO_4_4_4, SPINOR_OP_MIO_RDID},
  56. +
  57. + /* Micron Dual mode */
  58. + {SPI_NOR_DUAL, SNOR_PROTO_2_2_2, SPINOR_OP_MIO_RDID}
  59. + };
  60. tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
  61. if (tmp < 0) {
  62. @@ -883,6 +900,58 @@ static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
  63. return ERR_PTR(tmp);
  64. }
  65. + /*
  66. + * Check whether the SPI NOR memory has already been configured (at
  67. + * reset or by some bootloader) to use a protocol other than SPI 1-1-1.
  68. + */
  69. + for (i = 0; i < ARRAY_SIZE(configs); ++i) {
  70. + int len = SPI_NOR_MAX_ID_LEN;
  71. + bool is_multi = false;
  72. +
  73. + /*
  74. + * Check the latest read Manufacturer ID + Device ID (3 bytes):
  75. + * if they are different from both 0x000000 and 0xffffff, we
  76. + * assume that we succeeded in reading a valid JEDEC ID so we
  77. + * don't need to try other SPI protocols.
  78. + * Indeed when either the protocol or the op code are not valid,
  79. + * the SPI NOR memory should not reply to the command. Hence the
  80. + * SPI I/O lines remain in their default state: 1 when connected
  81. + * to pull-up resistors or 0 with pull-down.
  82. + */
  83. + if (!((id[0] == 0xff && id[1] == 0xff && id[2] == 0xff) ||
  84. + (id[0] == 0x00 && id[1] == 0x00 && id[2] == 0x00)))
  85. + break;
  86. +
  87. + /* Only try protocols supported by the user. */
  88. + if (configs[i].mode != mode)
  89. + continue;
  90. +
  91. + /* Set this protocol for all commands. */
  92. + nor->reg_proto = configs[i].proto;
  93. + nor->read_proto = configs[i].proto;
  94. + nor->write_proto = configs[i].proto;
  95. + nor->erase_proto = configs[i].proto;
  96. +
  97. + /*
  98. + * Multiple I/O Read ID only returns the Manufacturer ID
  99. + * (1 byte) and the Device ID (2 bytes). So we reset the
  100. + * remaining bytes.
  101. + */
  102. + if (configs[i].opcode == SPINOR_OP_MIO_RDID) {
  103. + is_multi = true;
  104. + len = 3;
  105. + memset(id + len, 0, sizeof(id) - len);
  106. + }
  107. +
  108. + tmp = nor->read_reg(nor, configs[i].opcode, id, len);
  109. + if (tmp < 0) {
  110. + dev_dbg(nor->dev,
  111. + "error %d reading JEDEC ID%s\n",
  112. + tmp, (is_multi ? " Multi I/O" : ""));
  113. + return ERR_PTR(tmp);
  114. + }
  115. + }
  116. +
  117. for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  118. info = &spi_nor_ids[tmp];
  119. if (info->id_len) {
  120. @@ -1140,11 +1209,17 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  121. if (ret)
  122. return ret;
  123. + /* Reset SPI protocol for all commands */
  124. + nor->erase_proto = SNOR_PROTO_1_1_1;
  125. + nor->read_proto = SNOR_PROTO_1_1_1;
  126. + nor->write_proto = SNOR_PROTO_1_1_1;
  127. + nor->reg_proto = SNOR_PROTO_1_1_1;
  128. +
  129. if (name)
  130. info = spi_nor_match_id(name);
  131. /* Try to auto-detect if chip name wasn't specified or not found */
  132. if (!info)
  133. - info = spi_nor_read_id(nor);
  134. + info = spi_nor_read_id(nor, mode);
  135. if (IS_ERR_OR_NULL(info))
  136. return -ENOENT;
  137. @@ -1155,7 +1230,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
  138. if (name && info->id_len) {
  139. const struct flash_info *jinfo;
  140. - jinfo = spi_nor_read_id(nor);
  141. + jinfo = spi_nor_read_id(nor, mode);
  142. if (IS_ERR(jinfo)) {
  143. return PTR_ERR(jinfo);
  144. } else if (jinfo != info) {
  145. diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
  146. index 62356d5..53932c8 100644
  147. --- a/include/linux/mtd/spi-nor.h
  148. +++ b/include/linux/mtd/spi-nor.h
  149. @@ -75,8 +75,9 @@
  150. #define SPINOR_OP_BRWR 0x17 /* Bank register write */
  151. /* Used for Micron flashes only. */
  152. -#define SPINOR_OP_RD_EVCR 0x65 /* Read EVCR register */
  153. -#define SPINOR_OP_WD_EVCR 0x61 /* Write EVCR register */
  154. +#define SPINOR_OP_MIO_RDID 0xaf /* Multiple I/O Read JEDEC ID */
  155. +#define SPINOR_OP_RD_EVCR 0x65 /* Read EVCR register */
  156. +#define SPINOR_OP_WD_EVCR 0x61 /* Write EVCR register */
  157. /* Status Register bits. */
  158. #define SR_WIP BIT(0) /* Write in progress */
  159. @@ -105,6 +106,43 @@ enum read_mode {
  160. SPI_NOR_QUAD,
  161. };
  162. +
  163. +#define SNOR_PROTO_CMD_OFF 8
  164. +#define SNOR_PROTO_CMD_MASK GENMASK(11, 8)
  165. +#define SNOR_PROTO_CMD_TO_PROTO(cmd) \
  166. + (((cmd) << SNOR_PROTO_CMD_OFF) & SNOR_PROTO_CMD_MASK)
  167. +#define SNOR_PROTO_CMD_FROM_PROTO(proto) \
  168. + ((((u32)(proto)) & SNOR_PROTO_CMD_MASK) >> SNOR_PROTO_CMD_OFF)
  169. +
  170. +#define SNOR_PROTO_ADDR_OFF 4
  171. +#define SNOR_PROTO_ADDR_MASK GENMASK(7, 4)
  172. +#define SNOR_PROTO_ADDR_TO_PROTO(addr) \
  173. + (((addr) << SNOR_PROTO_ADDR_OFF) & SNOR_PROTO_ADDR_MASK)
  174. +#define SNOR_PROTO_ADDR_FROM_PROTO(proto) \
  175. + ((((u32)(proto)) & SNOR_PROTO_ADDR_MASK) >> SNOR_PROTO_ADDR_OFF)
  176. +
  177. +#define SNOR_PROTO_DATA_OFF 0
  178. +#define SNOR_PROTO_DATA_MASK GENMASK(3, 0)
  179. +#define SNOR_PROTO_DATA_TO_PROTO(data) \
  180. + (((data) << SNOR_PROTO_DATA_OFF) & SNOR_PROTO_DATA_MASK)
  181. +#define SNOR_PROTO_DATA_FROM_PROTO(proto) \
  182. + ((((u32)(proto)) & SNOR_PROTO_DATA_MASK) >> SNOR_PROTO_DATA_OFF)
  183. +
  184. +#define SNOR_PROTO(cmd, addr, data) \
  185. + (SNOR_PROTO_CMD_TO_PROTO(cmd) | \
  186. + SNOR_PROTO_ADDR_TO_PROTO(addr) | \
  187. + SNOR_PROTO_DATA_TO_PROTO(data))
  188. +
  189. +enum spi_nor_protocol {
  190. + SNOR_PROTO_1_1_1 = SNOR_PROTO(1, 1, 1), /* SPI */
  191. + SNOR_PROTO_1_1_2 = SNOR_PROTO(1, 1, 2), /* Dual Output */
  192. + SNOR_PROTO_1_1_4 = SNOR_PROTO(1, 1, 4), /* Quad Output */
  193. + SNOR_PROTO_1_2_2 = SNOR_PROTO(1, 2, 2), /* Dual IO */
  194. + SNOR_PROTO_1_4_4 = SNOR_PROTO(1, 4, 4), /* Quad IO */
  195. + SNOR_PROTO_2_2_2 = SNOR_PROTO(2, 2, 2), /* Dual Command */
  196. + SNOR_PROTO_4_4_4 = SNOR_PROTO(4, 4, 4), /* Quad Command */
  197. +};
  198. +
  199. #define SPI_NOR_MAX_CMD_SIZE 8
  200. enum spi_nor_ops {
  201. SPI_NOR_OPS_READ = 0,
  202. @@ -132,6 +170,10 @@ enum spi_nor_option_flags {
  203. * @flash_read: the mode of the read
  204. * @sst_write_second: used by the SST write operation
  205. * @flags: flag options for the current SPI-NOR (SNOR_F_*)
  206. + * @erase_proto: the SPI protocol used by erase operations
  207. + * @read_proto: the SPI protocol used by read operations
  208. + * @write_proto: the SPI protocol used by write operations
  209. + * @reg_proto the SPI protocol used by read_reg/write_reg operations
  210. * @cmd_buf: used by the write_reg
  211. * @prepare: [OPTIONAL] do some preparations for the
  212. * read/write/erase/lock/unlock operations
  213. @@ -160,6 +202,10 @@ struct spi_nor {
  214. u8 read_opcode;
  215. u8 read_dummy;
  216. u8 program_opcode;
  217. + enum spi_nor_protocol erase_proto;
  218. + enum spi_nor_protocol read_proto;
  219. + enum spi_nor_protocol write_proto;
  220. + enum spi_nor_protocol reg_proto;
  221. enum read_mode flash_read;
  222. bool sst_write_second;
  223. u32 flags;
  224. --
  225. 2.8.1