0025-mtd-spi-nor-configure-the-number-of-dummy-clock-cycl.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. From a714a2af12d0de527be168b821373f29f4343cb7 Mon Sep 17 00:00:00 2001
  2. From: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  3. Date: Fri, 8 Jan 2016 17:02:22 +0100
  4. Subject: [PATCH 25/33] mtd: spi-nor: configure the number of dummy clock
  5. cycles on Macronix memories
  6. The spi-nor framework currently expects all Fast Read operations to use 8
  7. dummy clock cycles. Especially some drivers like m25p80 can only support
  8. multiple of 8 dummy clock cycles.
  9. On Macronix memories, the number of dummy clock cycles to be used by Fast
  10. Read commands can be safely set to 8 by updating the DC0 and DC1 volatile
  11. bits inside the Configuration Register.
  12. According to the mx66l1g45g datasheet from Macronix, using 8 dummy clock
  13. cycles should be enough to set the SPI bus clock frequency up to:
  14. - 133 MHz for Fast Read 1-1-1, 1-1-2, 1-1-4 and 1-2-2 commands in Single
  15. Transfer Rate (STR)
  16. - 104 MHz for Fast Read 1-4-4 (or 4-4-4 in QPI mode) commands (STR)
  17. Signed-off-by: Cyrille Pitchen <cyrille.pitchen@atmel.com>
  18. ---
  19. drivers/mtd/spi-nor/spi-nor.c | 155 +++++++++++++++++++++++++++++++++++++++---
  20. 1 file changed, 147 insertions(+), 8 deletions(-)
  21. diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
  22. index 5232984..55a1d74 100644
  23. --- a/drivers/mtd/spi-nor/spi-nor.c
  24. +++ b/drivers/mtd/spi-nor/spi-nor.c
  25. @@ -1182,6 +1182,136 @@ static int winbond_quad_enable(struct spi_nor *nor)
  26. return 0;
  27. }
  28. +static int macronix_dummy2code(u8 read_opcode, u8 read_dummy, u8 *dc)
  29. +{
  30. + switch (read_opcode) {
  31. + case SPINOR_OP_READ:
  32. + case SPINOR_OP_READ4:
  33. + *dc = 0;
  34. + break;
  35. +
  36. + case SPINOR_OP_READ_FAST:
  37. + case SPINOR_OP_READ_1_1_2:
  38. + case SPINOR_OP_READ_1_1_4:
  39. + case SPINOR_OP_READ4_FAST:
  40. + case SPINOR_OP_READ4_1_1_2:
  41. + case SPINOR_OP_READ4_1_1_4:
  42. + switch (read_dummy) {
  43. + case 6:
  44. + *dc = 1;
  45. + break;
  46. + case 8:
  47. + *dc = 0;
  48. + break;
  49. + case 10:
  50. + *dc = 3;
  51. + break;
  52. + default:
  53. + return -EINVAL;
  54. + }
  55. + break;
  56. +
  57. + case SPINOR_OP_READ_1_2_2:
  58. + case SPINOR_OP_READ4_1_2_2:
  59. + switch (read_dummy) {
  60. + case 4:
  61. + *dc = 0;
  62. + break;
  63. + case 6:
  64. + *dc = 1;
  65. + break;
  66. + case 8:
  67. + *dc = 2;
  68. + break;
  69. + case 10:
  70. + *dc = 3;
  71. + default:
  72. + return -EINVAL;
  73. + }
  74. + break;
  75. +
  76. + case SPINOR_OP_READ_1_4_4:
  77. + case SPINOR_OP_READ4_1_4_4:
  78. + switch (read_dummy) {
  79. + case 4:
  80. + *dc = 1;
  81. + break;
  82. + case 6:
  83. + *dc = 0;
  84. + break;
  85. + case 8:
  86. + *dc = 2;
  87. + break;
  88. + case 10:
  89. + *dc = 3;
  90. + default:
  91. + return -EINVAL;
  92. + }
  93. + break;
  94. +
  95. + default:
  96. + return -EINVAL;
  97. + }
  98. +
  99. + return 0;
  100. +}
  101. +
  102. +static int macronix_set_dummy_cycles(struct spi_nor *nor, u8 read_dummy)
  103. +{
  104. + int ret, sr, cr, mask, val;
  105. + u16 sr_cr;
  106. + u8 dc;
  107. +
  108. + /* Convert the number of dummy cycles into Macronix DC volatile bits */
  109. + ret = macronix_dummy2code(nor->read_opcode, read_dummy, &dc);
  110. + if (ret)
  111. + return ret;
  112. +
  113. + mask = GENMASK(7, 6);
  114. + val = (dc << 6) & mask;
  115. +
  116. + cr = read_cr(nor);
  117. + if (cr < 0) {
  118. + dev_err(nor->dev, "error while reading the config register\n");
  119. + return cr;
  120. + }
  121. +
  122. + if ((cr & mask) == val) {
  123. + nor->read_dummy = read_dummy;
  124. + return 0;
  125. + }
  126. +
  127. + sr = read_sr(nor);
  128. + if (sr < 0) {
  129. + dev_err(nor->dev, "error while reading the status register\n");
  130. + return sr;
  131. + }
  132. +
  133. + cr = (cr & ~mask) | val;
  134. + sr_cr = (sr & 0xff) | ((cr & 0xff) << 8);
  135. + write_enable(nor);
  136. + ret = write_sr_cr(nor, sr_cr);
  137. + if (ret) {
  138. + dev_err(nor->dev,
  139. + "error while writing the SR and CR registers\n");
  140. + return ret;
  141. + }
  142. +
  143. + ret = spi_nor_wait_till_ready(nor);
  144. + if (ret)
  145. + return ret;
  146. +
  147. + cr = read_cr(nor);
  148. + if (cr < 0 || (cr & mask) != val) {
  149. + dev_err(nor->dev, "Macronix Dummy Cycle bits not updated\n");
  150. + return -EINVAL;
  151. + }
  152. +
  153. + /* Save the number of dummy cycles to use with Fast Read commands */
  154. + nor->read_dummy = read_dummy;
  155. + return 0;
  156. +}
  157. +
  158. static int macronix_set_quad_mode(struct spi_nor *nor)
  159. {
  160. int status;
  161. @@ -1199,8 +1329,7 @@ static int macronix_set_quad_mode(struct spi_nor *nor)
  162. * read (performance enhance) mode by mistake!
  163. */
  164. nor->read_opcode = SPINOR_OP_READ_1_4_4;
  165. - nor->read_dummy = 8;
  166. - return 0;
  167. + return macronix_set_dummy_cycles(nor, 8);
  168. }
  169. /*
  170. @@ -1213,6 +1342,9 @@ static int macronix_set_quad_mode(struct spi_nor *nor)
  171. * entering the continuous read mode by mistake if some
  172. * performance enhance toggling bits P0-P7 were written during
  173. * dummy/mode cycles.
  174. + *
  175. + * Use the Fast Read Quad Output 1-1-4 (0x6b) command with 8 dummy
  176. + * cycles (up to 133MHz for STR and 66MHz for DTR).
  177. */
  178. status = macronix_quad_enable(nor);
  179. if (status) {
  180. @@ -1221,8 +1353,7 @@ static int macronix_set_quad_mode(struct spi_nor *nor)
  181. }
  182. nor->read_proto = SNOR_PROTO_1_1_4;
  183. nor->read_opcode = SPINOR_OP_READ_1_1_4;
  184. - nor->read_dummy = 8;
  185. - return 0;
  186. + return macronix_set_dummy_cycles(nor, 8);
  187. }
  188. /*
  189. @@ -1233,16 +1364,25 @@ static int macronix_set_quad_mode(struct spi_nor *nor)
  190. static int macronix_set_dual_mode(struct spi_nor *nor)
  191. {
  192. + /*
  193. + * Use the Fast Read Dual Output 1-1-2 (0x3b) command with 8 dummy
  194. + * cycles (up to 133MHz for STR and 66MHz for DTR).
  195. + */
  196. nor->read_proto = SNOR_PROTO_1_1_2;
  197. nor->read_opcode = SPINOR_OP_READ_1_1_2;
  198. - nor->read_dummy = 8;
  199. - return 0;
  200. + return macronix_set_dummy_cycles(nor, 8);
  201. }
  202. static int macronix_set_single_mode(struct spi_nor *nor)
  203. {
  204. u8 read_dummy;
  205. + /*
  206. + * Configure 8 dummy cycles for Fast Read 1-1-1 (0x0b) command (up to
  207. + * 133MHz for STR and 66MHz for DTR). The Read 1-1-1 (0x03) command
  208. + * expects no dummy cycle.
  209. + * read_opcode should not be overridden here!
  210. + */
  211. switch (nor->read_opcode) {
  212. case SPINOR_OP_READ:
  213. case SPINOR_OP_READ4:
  214. @@ -1255,8 +1395,7 @@ static int macronix_set_single_mode(struct spi_nor *nor)
  215. }
  216. nor->read_proto = SNOR_PROTO_1_1_1;
  217. - nor->read_dummy = read_dummy;
  218. - return 0;
  219. + return macronix_set_dummy_cycles(nor, read_dummy);
  220. }
  221. static int winbond_set_quad_mode(struct spi_nor *nor)
  222. --
  223. 2.8.1