nand_macronix.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2017 Free Electrons
  4. * Copyright (C) 2017 NextThing Co
  5. *
  6. * Author: Boris Brezillon <[email protected]>
  7. */
  8. #include "linux/delay.h"
  9. #include "internals.h"
  10. #define MACRONIX_READ_RETRY_BIT BIT(0)
  11. #define MACRONIX_NUM_READ_RETRY_MODES 6
  12. #define ONFI_FEATURE_ADDR_MXIC_PROTECTION 0xA0
  13. #define MXIC_BLOCK_PROTECTION_ALL_LOCK 0x38
  14. #define MXIC_BLOCK_PROTECTION_ALL_UNLOCK 0x0
  15. #define ONFI_FEATURE_ADDR_MXIC_RANDOMIZER 0xB0
  16. #define MACRONIX_RANDOMIZER_BIT BIT(1)
  17. #define MACRONIX_RANDOMIZER_ENPGM BIT(0)
  18. #define MACRONIX_RANDOMIZER_RANDEN BIT(1)
  19. #define MACRONIX_RANDOMIZER_RANDOPT BIT(2)
  20. #define MACRONIX_RANDOMIZER_MODE_ENTER \
  21. (MACRONIX_RANDOMIZER_ENPGM | \
  22. MACRONIX_RANDOMIZER_RANDEN | \
  23. MACRONIX_RANDOMIZER_RANDOPT)
  24. #define MACRONIX_RANDOMIZER_MODE_EXIT \
  25. (MACRONIX_RANDOMIZER_RANDEN | \
  26. MACRONIX_RANDOMIZER_RANDOPT)
  27. #define MXIC_CMD_POWER_DOWN 0xB9
  28. struct nand_onfi_vendor_macronix {
  29. u8 reserved;
  30. u8 reliability_func;
  31. } __packed;
  32. static int macronix_nand_setup_read_retry(struct nand_chip *chip, int mode)
  33. {
  34. u8 feature[ONFI_SUBFEATURE_PARAM_LEN];
  35. if (!chip->parameters.supports_set_get_features ||
  36. !test_bit(ONFI_FEATURE_ADDR_READ_RETRY,
  37. chip->parameters.set_feature_list))
  38. return -ENOTSUPP;
  39. feature[0] = mode;
  40. return nand_set_features(chip, ONFI_FEATURE_ADDR_READ_RETRY, feature);
  41. }
  42. static int macronix_nand_randomizer_check_enable(struct nand_chip *chip)
  43. {
  44. u8 feature[ONFI_SUBFEATURE_PARAM_LEN];
  45. int ret;
  46. ret = nand_get_features(chip, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER,
  47. feature);
  48. if (ret < 0)
  49. return ret;
  50. if (feature[0])
  51. return feature[0];
  52. feature[0] = MACRONIX_RANDOMIZER_MODE_ENTER;
  53. ret = nand_set_features(chip, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER,
  54. feature);
  55. if (ret < 0)
  56. return ret;
  57. /* RANDEN and RANDOPT OTP bits are programmed */
  58. feature[0] = 0x0;
  59. ret = nand_prog_page_op(chip, 0, 0, feature, 1);
  60. if (ret < 0)
  61. return ret;
  62. ret = nand_get_features(chip, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER,
  63. feature);
  64. if (ret < 0)
  65. return ret;
  66. feature[0] &= MACRONIX_RANDOMIZER_MODE_EXIT;
  67. ret = nand_set_features(chip, ONFI_FEATURE_ADDR_MXIC_RANDOMIZER,
  68. feature);
  69. if (ret < 0)
  70. return ret;
  71. return 0;
  72. }
  73. static void macronix_nand_onfi_init(struct nand_chip *chip)
  74. {
  75. struct nand_parameters *p = &chip->parameters;
  76. struct nand_onfi_vendor_macronix *mxic;
  77. struct device_node *dn = nand_get_flash_node(chip);
  78. int rand_otp = 0;
  79. int ret;
  80. if (!p->onfi)
  81. return;
  82. if (of_find_property(dn, "mxic,enable-randomizer-otp", NULL))
  83. rand_otp = 1;
  84. mxic = (struct nand_onfi_vendor_macronix *)p->onfi->vendor;
  85. /* Subpage write is prohibited in randomizer operatoin */
  86. if (rand_otp && chip->options & NAND_NO_SUBPAGE_WRITE &&
  87. mxic->reliability_func & MACRONIX_RANDOMIZER_BIT) {
  88. if (p->supports_set_get_features) {
  89. bitmap_set(p->set_feature_list,
  90. ONFI_FEATURE_ADDR_MXIC_RANDOMIZER, 1);
  91. bitmap_set(p->get_feature_list,
  92. ONFI_FEATURE_ADDR_MXIC_RANDOMIZER, 1);
  93. ret = macronix_nand_randomizer_check_enable(chip);
  94. if (ret < 0) {
  95. bitmap_clear(p->set_feature_list,
  96. ONFI_FEATURE_ADDR_MXIC_RANDOMIZER,
  97. 1);
  98. bitmap_clear(p->get_feature_list,
  99. ONFI_FEATURE_ADDR_MXIC_RANDOMIZER,
  100. 1);
  101. pr_info("Macronix NAND randomizer failed\n");
  102. } else {
  103. pr_info("Macronix NAND randomizer enabled\n");
  104. }
  105. }
  106. }
  107. if ((mxic->reliability_func & MACRONIX_READ_RETRY_BIT) == 0)
  108. return;
  109. chip->read_retries = MACRONIX_NUM_READ_RETRY_MODES;
  110. chip->ops.setup_read_retry = macronix_nand_setup_read_retry;
  111. if (p->supports_set_get_features) {
  112. bitmap_set(p->set_feature_list,
  113. ONFI_FEATURE_ADDR_READ_RETRY, 1);
  114. bitmap_set(p->get_feature_list,
  115. ONFI_FEATURE_ADDR_READ_RETRY, 1);
  116. }
  117. }
  118. /*
  119. * Macronix AC series does not support using SET/GET_FEATURES to change
  120. * the timings unlike what is declared in the parameter page. Unflag
  121. * this feature to avoid unnecessary downturns.
  122. */
  123. static void macronix_nand_fix_broken_get_timings(struct nand_chip *chip)
  124. {
  125. int i;
  126. static const char * const broken_get_timings[] = {
  127. "MX30LF1G18AC",
  128. "MX30LF1G28AC",
  129. "MX30LF2G18AC",
  130. "MX30LF2G28AC",
  131. "MX30LF4G18AC",
  132. "MX30LF4G28AC",
  133. "MX60LF8G18AC",
  134. "MX30UF1G18AC",
  135. "MX30UF1G16AC",
  136. "MX30UF2G18AC",
  137. "MX30UF2G16AC",
  138. "MX30UF4G18AC",
  139. "MX30UF4G16AC",
  140. "MX30UF4G28AC",
  141. };
  142. if (!chip->parameters.supports_set_get_features)
  143. return;
  144. i = match_string(broken_get_timings, ARRAY_SIZE(broken_get_timings),
  145. chip->parameters.model);
  146. if (i < 0)
  147. return;
  148. bitmap_clear(chip->parameters.get_feature_list,
  149. ONFI_FEATURE_ADDR_TIMING_MODE, 1);
  150. bitmap_clear(chip->parameters.set_feature_list,
  151. ONFI_FEATURE_ADDR_TIMING_MODE, 1);
  152. }
  153. /*
  154. * Macronix NAND supports Block Protection by Protectoin(PT) pin;
  155. * active high at power-on which protects the entire chip even the #WP is
  156. * disabled. Lock/unlock protection area can be partition according to
  157. * protection bits, i.e. upper 1/2 locked, upper 1/4 locked and so on.
  158. */
  159. static int mxic_nand_lock(struct nand_chip *chip, loff_t ofs, uint64_t len)
  160. {
  161. u8 feature[ONFI_SUBFEATURE_PARAM_LEN];
  162. int ret;
  163. feature[0] = MXIC_BLOCK_PROTECTION_ALL_LOCK;
  164. nand_select_target(chip, 0);
  165. ret = nand_set_features(chip, ONFI_FEATURE_ADDR_MXIC_PROTECTION,
  166. feature);
  167. nand_deselect_target(chip);
  168. if (ret)
  169. pr_err("%s all blocks failed\n", __func__);
  170. return ret;
  171. }
  172. static int mxic_nand_unlock(struct nand_chip *chip, loff_t ofs, uint64_t len)
  173. {
  174. u8 feature[ONFI_SUBFEATURE_PARAM_LEN];
  175. int ret;
  176. feature[0] = MXIC_BLOCK_PROTECTION_ALL_UNLOCK;
  177. nand_select_target(chip, 0);
  178. ret = nand_set_features(chip, ONFI_FEATURE_ADDR_MXIC_PROTECTION,
  179. feature);
  180. nand_deselect_target(chip);
  181. if (ret)
  182. pr_err("%s all blocks failed\n", __func__);
  183. return ret;
  184. }
  185. static void macronix_nand_block_protection_support(struct nand_chip *chip)
  186. {
  187. u8 feature[ONFI_SUBFEATURE_PARAM_LEN];
  188. int ret;
  189. bitmap_set(chip->parameters.get_feature_list,
  190. ONFI_FEATURE_ADDR_MXIC_PROTECTION, 1);
  191. feature[0] = MXIC_BLOCK_PROTECTION_ALL_UNLOCK;
  192. nand_select_target(chip, 0);
  193. ret = nand_get_features(chip, ONFI_FEATURE_ADDR_MXIC_PROTECTION,
  194. feature);
  195. nand_deselect_target(chip);
  196. if (ret || feature[0] != MXIC_BLOCK_PROTECTION_ALL_LOCK) {
  197. if (ret)
  198. pr_err("Block protection check failed\n");
  199. bitmap_clear(chip->parameters.get_feature_list,
  200. ONFI_FEATURE_ADDR_MXIC_PROTECTION, 1);
  201. return;
  202. }
  203. bitmap_set(chip->parameters.set_feature_list,
  204. ONFI_FEATURE_ADDR_MXIC_PROTECTION, 1);
  205. chip->ops.lock_area = mxic_nand_lock;
  206. chip->ops.unlock_area = mxic_nand_unlock;
  207. }
  208. static int nand_power_down_op(struct nand_chip *chip)
  209. {
  210. int ret;
  211. if (nand_has_exec_op(chip)) {
  212. struct nand_op_instr instrs[] = {
  213. NAND_OP_CMD(MXIC_CMD_POWER_DOWN, 0),
  214. };
  215. struct nand_operation op = NAND_OPERATION(chip->cur_cs, instrs);
  216. ret = nand_exec_op(chip, &op);
  217. if (ret)
  218. return ret;
  219. } else {
  220. chip->legacy.cmdfunc(chip, MXIC_CMD_POWER_DOWN, -1, -1);
  221. }
  222. return 0;
  223. }
  224. static int mxic_nand_suspend(struct nand_chip *chip)
  225. {
  226. int ret;
  227. nand_select_target(chip, 0);
  228. ret = nand_power_down_op(chip);
  229. if (ret < 0)
  230. pr_err("Suspending MXIC NAND chip failed (%d)\n", ret);
  231. nand_deselect_target(chip);
  232. return ret;
  233. }
  234. static void mxic_nand_resume(struct nand_chip *chip)
  235. {
  236. /*
  237. * Toggle #CS pin to resume NAND device and don't care
  238. * of the others CLE, #WE, #RE pins status.
  239. * A NAND controller ensure it is able to assert/de-assert #CS
  240. * by sending any byte over the NAND bus.
  241. * i.e.,
  242. * NAND power down command or reset command w/o R/B# status checking.
  243. */
  244. nand_select_target(chip, 0);
  245. nand_power_down_op(chip);
  246. /* The minimum of a recovery time tRDP is 35 us */
  247. usleep_range(35, 100);
  248. nand_deselect_target(chip);
  249. }
  250. static void macronix_nand_deep_power_down_support(struct nand_chip *chip)
  251. {
  252. int i;
  253. static const char * const deep_power_down_dev[] = {
  254. "MX30UF1G28AD",
  255. "MX30UF2G28AD",
  256. "MX30UF4G28AD",
  257. };
  258. i = match_string(deep_power_down_dev, ARRAY_SIZE(deep_power_down_dev),
  259. chip->parameters.model);
  260. if (i < 0)
  261. return;
  262. chip->ops.suspend = mxic_nand_suspend;
  263. chip->ops.resume = mxic_nand_resume;
  264. }
  265. static int macronix_nand_init(struct nand_chip *chip)
  266. {
  267. if (nand_is_slc(chip))
  268. chip->options |= NAND_BBM_FIRSTPAGE | NAND_BBM_SECONDPAGE;
  269. macronix_nand_fix_broken_get_timings(chip);
  270. macronix_nand_onfi_init(chip);
  271. macronix_nand_block_protection_support(chip);
  272. macronix_nand_deep_power_down_support(chip);
  273. return 0;
  274. }
  275. const struct nand_manufacturer_ops macronix_nand_manuf_ops = {
  276. .init = macronix_nand_init,
  277. };