micron.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016-2017 Micron Technology, Inc.
  4. *
  5. * Authors:
  6. * Peter Pan <peterpandong@micron.com>
  7. */
  8. #include <linux/device.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mtd/spinand.h>
  11. #include <linux/spi/spi-mem.h>
  12. #include <linux/string.h>
  13. #define SPINAND_MFR_MICRON 0x2c
  14. #define MICRON_STATUS_ECC_MASK GENMASK(6, 4)
  15. #define MICRON_STATUS_ECC_NO_BITFLIPS (0 << 4)
  16. #define MICRON_STATUS_ECC_1TO3_BITFLIPS (1 << 4)
  17. #define MICRON_STATUS_ECC_4TO6_BITFLIPS (3 << 4)
  18. #define MICRON_STATUS_ECC_7TO8_BITFLIPS (5 << 4)
  19. #define MICRON_CFG_CR BIT(0)
  20. /*
  21. * As per datasheet, die selection is done by the 6th bit of Die
  22. * Select Register (Address 0xD0).
  23. */
  24. #define MICRON_DIE_SELECT_REG 0xD0
  25. #define MICRON_SELECT_DIE(x) ((x) << 6)
  26. #define MICRON_MT29F2G01ABAGD_CFG_OTP_STATE BIT(7)
  27. #define MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK \
  28. (CFG_OTP_ENABLE | MICRON_MT29F2G01ABAGD_CFG_OTP_STATE)
  29. static SPINAND_OP_VARIANTS(quadio_read_cache_variants,
  30. SPINAND_PAGE_READ_FROM_CACHE_1S_4S_4S_OP(0, 2, NULL, 0, 0),
  31. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_4S_OP(0, 1, NULL, 0, 0),
  32. SPINAND_PAGE_READ_FROM_CACHE_1S_2S_2S_OP(0, 1, NULL, 0, 0),
  33. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_2S_OP(0, 1, NULL, 0, 0),
  34. SPINAND_PAGE_READ_FROM_CACHE_FAST_1S_1S_1S_OP(0, 1, NULL, 0, 0),
  35. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_1S_OP(0, 1, NULL, 0, 0));
  36. static SPINAND_OP_VARIANTS(x4_write_cache_variants,
  37. SPINAND_PROG_LOAD_1S_1S_4S_OP(true, 0, NULL, 0),
  38. SPINAND_PROG_LOAD_1S_1S_1S_OP(true, 0, NULL, 0));
  39. static SPINAND_OP_VARIANTS(x4_update_cache_variants,
  40. SPINAND_PROG_LOAD_1S_1S_4S_OP(false, 0, NULL, 0),
  41. SPINAND_PROG_LOAD_1S_1S_1S_OP(false, 0, NULL, 0));
  42. /* Micron MT29F2G01AAAED Device */
  43. static SPINAND_OP_VARIANTS(x4_read_cache_variants,
  44. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_4S_OP(0, 1, NULL, 0, 0),
  45. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_2S_OP(0, 1, NULL, 0, 0),
  46. SPINAND_PAGE_READ_FROM_CACHE_FAST_1S_1S_1S_OP(0, 1, NULL, 0, 0),
  47. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_1S_OP(0, 1, NULL, 0, 0));
  48. static SPINAND_OP_VARIANTS(x1_write_cache_variants,
  49. SPINAND_PROG_LOAD_1S_1S_1S_OP(true, 0, NULL, 0));
  50. static SPINAND_OP_VARIANTS(x1_update_cache_variants,
  51. SPINAND_PROG_LOAD_1S_1S_1S_OP(false, 0, NULL, 0));
  52. static int micron_8_ooblayout_ecc(struct mtd_info *mtd, int section,
  53. struct mtd_oob_region *region)
  54. {
  55. if (section)
  56. return -ERANGE;
  57. region->offset = mtd->oobsize / 2;
  58. region->length = mtd->oobsize / 2;
  59. return 0;
  60. }
  61. static int micron_8_ooblayout_free(struct mtd_info *mtd, int section,
  62. struct mtd_oob_region *region)
  63. {
  64. if (section)
  65. return -ERANGE;
  66. /* Reserve 2 bytes for the BBM. */
  67. region->offset = 2;
  68. region->length = (mtd->oobsize / 2) - 2;
  69. return 0;
  70. }
  71. static const struct mtd_ooblayout_ops micron_8_ooblayout = {
  72. .ecc = micron_8_ooblayout_ecc,
  73. .free = micron_8_ooblayout_free,
  74. };
  75. static int micron_4_ooblayout_ecc(struct mtd_info *mtd, int section,
  76. struct mtd_oob_region *region)
  77. {
  78. struct spinand_device *spinand = mtd_to_spinand(mtd);
  79. if (section >= spinand->base.memorg.pagesize /
  80. mtd->ecc_step_size)
  81. return -ERANGE;
  82. region->offset = (section * 16) + 8;
  83. region->length = 8;
  84. return 0;
  85. }
  86. static int micron_4_ooblayout_free(struct mtd_info *mtd, int section,
  87. struct mtd_oob_region *region)
  88. {
  89. struct spinand_device *spinand = mtd_to_spinand(mtd);
  90. if (section >= spinand->base.memorg.pagesize /
  91. mtd->ecc_step_size)
  92. return -ERANGE;
  93. if (section) {
  94. region->offset = 16 * section;
  95. region->length = 8;
  96. } else {
  97. /* section 0 has two bytes reserved for the BBM */
  98. region->offset = 2;
  99. region->length = 6;
  100. }
  101. return 0;
  102. }
  103. static const struct mtd_ooblayout_ops micron_4_ooblayout = {
  104. .ecc = micron_4_ooblayout_ecc,
  105. .free = micron_4_ooblayout_free,
  106. };
  107. static int micron_select_target(struct spinand_device *spinand,
  108. unsigned int target)
  109. {
  110. struct spi_mem_op op = SPINAND_OP(spinand, set_feature,
  111. MICRON_DIE_SELECT_REG, spinand->scratchbuf);
  112. if (target > 1)
  113. return -EINVAL;
  114. *spinand->scratchbuf = MICRON_SELECT_DIE(target);
  115. return spi_mem_exec_op(spinand->spimem, &op);
  116. }
  117. static int micron_8_ecc_get_status(struct spinand_device *spinand,
  118. u8 status)
  119. {
  120. switch (status & MICRON_STATUS_ECC_MASK) {
  121. case STATUS_ECC_NO_BITFLIPS:
  122. return 0;
  123. case STATUS_ECC_UNCOR_ERROR:
  124. return -EBADMSG;
  125. case MICRON_STATUS_ECC_1TO3_BITFLIPS:
  126. return 3;
  127. case MICRON_STATUS_ECC_4TO6_BITFLIPS:
  128. return 6;
  129. case MICRON_STATUS_ECC_7TO8_BITFLIPS:
  130. return 8;
  131. default:
  132. break;
  133. }
  134. return -EINVAL;
  135. }
  136. static int mt29f2g01abagd_otp_is_locked(struct spinand_device *spinand)
  137. {
  138. size_t bufsize = spinand_otp_page_size(spinand);
  139. size_t retlen;
  140. u8 *buf;
  141. int ret;
  142. buf = kmalloc(bufsize, GFP_KERNEL);
  143. if (!buf)
  144. return -ENOMEM;
  145. ret = spinand_upd_cfg(spinand,
  146. MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK,
  147. MICRON_MT29F2G01ABAGD_CFG_OTP_STATE);
  148. if (ret)
  149. goto free_buf;
  150. ret = spinand_user_otp_read(spinand, 0, bufsize, &retlen, buf);
  151. if (spinand_upd_cfg(spinand, MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK,
  152. 0)) {
  153. dev_warn(&spinand_to_mtd(spinand)->dev,
  154. "Can not disable OTP mode\n");
  155. ret = -EIO;
  156. }
  157. if (ret)
  158. goto free_buf;
  159. /* If all zeros, then the OTP area is locked. */
  160. if (mem_is_zero(buf, bufsize))
  161. ret = 1;
  162. free_buf:
  163. kfree(buf);
  164. return ret;
  165. }
  166. static int mt29f2g01abagd_otp_info(struct spinand_device *spinand, size_t len,
  167. struct otp_info *buf, size_t *retlen,
  168. bool user)
  169. {
  170. int locked;
  171. if (len < sizeof(*buf))
  172. return -EINVAL;
  173. locked = mt29f2g01abagd_otp_is_locked(spinand);
  174. if (locked < 0)
  175. return locked;
  176. buf->locked = locked;
  177. buf->start = 0;
  178. buf->length = user ? spinand_user_otp_size(spinand) :
  179. spinand_fact_otp_size(spinand);
  180. *retlen = sizeof(*buf);
  181. return 0;
  182. }
  183. static int mt29f2g01abagd_fact_otp_info(struct spinand_device *spinand,
  184. size_t len, struct otp_info *buf,
  185. size_t *retlen)
  186. {
  187. return mt29f2g01abagd_otp_info(spinand, len, buf, retlen, false);
  188. }
  189. static int mt29f2g01abagd_user_otp_info(struct spinand_device *spinand,
  190. size_t len, struct otp_info *buf,
  191. size_t *retlen)
  192. {
  193. return mt29f2g01abagd_otp_info(spinand, len, buf, retlen, true);
  194. }
  195. static int mt29f2g01abagd_otp_lock(struct spinand_device *spinand, loff_t from,
  196. size_t len)
  197. {
  198. struct spi_mem_op write_op = SPINAND_OP(spinand, wr_en);
  199. struct spi_mem_op exec_op = SPINAND_OP(spinand, prog_exec, 0);
  200. u8 status;
  201. int ret;
  202. ret = spinand_upd_cfg(spinand,
  203. MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK,
  204. MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK);
  205. if (!ret)
  206. return ret;
  207. ret = spi_mem_exec_op(spinand->spimem, &write_op);
  208. if (!ret)
  209. goto out;
  210. ret = spi_mem_exec_op(spinand->spimem, &exec_op);
  211. if (!ret)
  212. goto out;
  213. ret = spinand_wait(spinand,
  214. SPINAND_WRITE_INITIAL_DELAY_US,
  215. SPINAND_WRITE_POLL_DELAY_US,
  216. &status);
  217. if (!ret && (status & STATUS_PROG_FAILED))
  218. ret = -EIO;
  219. out:
  220. if (spinand_upd_cfg(spinand, MICRON_MT29F2G01ABAGD_CFG_OTP_LOCK, 0)) {
  221. dev_warn(&spinand_to_mtd(spinand)->dev,
  222. "Can not disable OTP mode\n");
  223. ret = -EIO;
  224. }
  225. return ret;
  226. }
  227. static const struct spinand_user_otp_ops mt29f2g01abagd_user_otp_ops = {
  228. .info = mt29f2g01abagd_user_otp_info,
  229. .lock = mt29f2g01abagd_otp_lock,
  230. .read = spinand_user_otp_read,
  231. .write = spinand_user_otp_write,
  232. };
  233. static const struct spinand_fact_otp_ops mt29f2g01abagd_fact_otp_ops = {
  234. .info = mt29f2g01abagd_fact_otp_info,
  235. .read = spinand_fact_otp_read,
  236. };
  237. static const struct spinand_info micron_spinand_table[] = {
  238. /* M79A 2Gb 3.3V */
  239. SPINAND_INFO("MT29F2G01ABAGD",
  240. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x24),
  241. NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1),
  242. NAND_ECCREQ(8, 512),
  243. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  244. &x4_write_cache_variants,
  245. &x4_update_cache_variants),
  246. 0,
  247. SPINAND_ECCINFO(&micron_8_ooblayout,
  248. micron_8_ecc_get_status),
  249. SPINAND_USER_OTP_INFO(12, 2, &mt29f2g01abagd_user_otp_ops),
  250. SPINAND_FACT_OTP_INFO(2, 0, &mt29f2g01abagd_fact_otp_ops)),
  251. /* M79A 2Gb 1.8V */
  252. SPINAND_INFO("MT29F2G01ABBGD",
  253. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x25),
  254. NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1),
  255. NAND_ECCREQ(8, 512),
  256. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  257. &x4_write_cache_variants,
  258. &x4_update_cache_variants),
  259. 0,
  260. SPINAND_ECCINFO(&micron_8_ooblayout,
  261. micron_8_ecc_get_status)),
  262. /* M78A 1Gb 3.3V */
  263. SPINAND_INFO("MT29F1G01ABAFD",
  264. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x14),
  265. NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
  266. NAND_ECCREQ(8, 512),
  267. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  268. &x4_write_cache_variants,
  269. &x4_update_cache_variants),
  270. 0,
  271. SPINAND_ECCINFO(&micron_8_ooblayout,
  272. micron_8_ecc_get_status)),
  273. /* M78A 1Gb 1.8V */
  274. SPINAND_INFO("MT29F1G01ABAFD",
  275. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x15),
  276. NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
  277. NAND_ECCREQ(8, 512),
  278. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  279. &x4_write_cache_variants,
  280. &x4_update_cache_variants),
  281. 0,
  282. SPINAND_ECCINFO(&micron_8_ooblayout,
  283. micron_8_ecc_get_status)),
  284. /* M79A 4Gb 3.3V */
  285. SPINAND_INFO("MT29F4G01ADAGD",
  286. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x36),
  287. NAND_MEMORG(1, 2048, 128, 64, 2048, 80, 2, 1, 2),
  288. NAND_ECCREQ(8, 512),
  289. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  290. &x4_write_cache_variants,
  291. &x4_update_cache_variants),
  292. 0,
  293. SPINAND_ECCINFO(&micron_8_ooblayout,
  294. micron_8_ecc_get_status),
  295. SPINAND_SELECT_TARGET(micron_select_target)),
  296. /* M70A 4Gb 3.3V */
  297. SPINAND_INFO("MT29F4G01ABAFD",
  298. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x34),
  299. NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
  300. NAND_ECCREQ(8, 512),
  301. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  302. &x4_write_cache_variants,
  303. &x4_update_cache_variants),
  304. SPINAND_HAS_CR_FEAT_BIT,
  305. SPINAND_ECCINFO(&micron_8_ooblayout,
  306. micron_8_ecc_get_status)),
  307. /* M70A 4Gb 1.8V */
  308. SPINAND_INFO("MT29F4G01ABBFD",
  309. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x35),
  310. NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
  311. NAND_ECCREQ(8, 512),
  312. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  313. &x4_write_cache_variants,
  314. &x4_update_cache_variants),
  315. SPINAND_HAS_CR_FEAT_BIT,
  316. SPINAND_ECCINFO(&micron_8_ooblayout,
  317. micron_8_ecc_get_status)),
  318. /* M70A 8Gb 3.3V */
  319. SPINAND_INFO("MT29F8G01ADAFD",
  320. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x46),
  321. NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 2),
  322. NAND_ECCREQ(8, 512),
  323. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  324. &x4_write_cache_variants,
  325. &x4_update_cache_variants),
  326. SPINAND_HAS_CR_FEAT_BIT,
  327. SPINAND_ECCINFO(&micron_8_ooblayout,
  328. micron_8_ecc_get_status),
  329. SPINAND_SELECT_TARGET(micron_select_target)),
  330. /* M70A 8Gb 1.8V */
  331. SPINAND_INFO("MT29F8G01ADBFD",
  332. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x47),
  333. NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 2),
  334. NAND_ECCREQ(8, 512),
  335. SPINAND_INFO_OP_VARIANTS(&quadio_read_cache_variants,
  336. &x4_write_cache_variants,
  337. &x4_update_cache_variants),
  338. SPINAND_HAS_CR_FEAT_BIT,
  339. SPINAND_ECCINFO(&micron_8_ooblayout,
  340. micron_8_ecc_get_status),
  341. SPINAND_SELECT_TARGET(micron_select_target)),
  342. /* M69A 2Gb 3.3V */
  343. SPINAND_INFO("MT29F2G01AAAED",
  344. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x9F),
  345. NAND_MEMORG(1, 2048, 64, 64, 2048, 80, 2, 1, 1),
  346. NAND_ECCREQ(4, 512),
  347. SPINAND_INFO_OP_VARIANTS(&x4_read_cache_variants,
  348. &x1_write_cache_variants,
  349. &x1_update_cache_variants),
  350. 0,
  351. SPINAND_ECCINFO(&micron_4_ooblayout, NULL)),
  352. };
  353. static int micron_spinand_init(struct spinand_device *spinand)
  354. {
  355. /*
  356. * M70A device series enable Continuous Read feature at Power-up,
  357. * which is not supported. Disable this bit to avoid any possible
  358. * failure.
  359. */
  360. if (spinand->flags & SPINAND_HAS_CR_FEAT_BIT)
  361. return spinand_upd_cfg(spinand, MICRON_CFG_CR, 0);
  362. return 0;
  363. }
  364. static const struct spinand_manufacturer_ops micron_spinand_manuf_ops = {
  365. .init = micron_spinand_init,
  366. };
  367. const struct spinand_manufacturer micron_spinand_manufacturer = {
  368. .id = SPINAND_MFR_MICRON,
  369. .name = "Micron",
  370. .chips = micron_spinand_table,
  371. .nchips = ARRAY_SIZE(micron_spinand_table),
  372. .ops = &micron_spinand_manuf_ops,
  373. };