esmt.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Author:
  4. * Chuanhong Guo <gch981213@gmail.com> - the main driver logic
  5. * Martin Kurbanov <mmkurbanov@sberdevices.ru> - OOB layout
  6. */
  7. #include <linux/device.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mtd/spinand.h>
  10. #include <linux/spi/spi-mem.h>
  11. /* ESMT uses GigaDevice 0xc8 JECDEC ID on some SPI NANDs */
  12. #define SPINAND_MFR_ESMT_C8 0xc8
  13. #define SPINAND_MFR_ESMT_8C 0x8c
  14. #define ESMT_F50L1G41LB_CFG_OTP_PROTECT BIT(7)
  15. #define ESMT_F50L1G41LB_CFG_OTP_LOCK \
  16. (CFG_OTP_ENABLE | ESMT_F50L1G41LB_CFG_OTP_PROTECT)
  17. static SPINAND_OP_VARIANTS(read_cache_variants,
  18. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_4S_OP(0, 1, NULL, 0, 0),
  19. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_2S_OP(0, 1, NULL, 0, 0),
  20. SPINAND_PAGE_READ_FROM_CACHE_FAST_1S_1S_1S_OP(0, 1, NULL, 0, 0),
  21. SPINAND_PAGE_READ_FROM_CACHE_1S_1S_1S_OP(0, 1, NULL, 0, 0));
  22. static SPINAND_OP_VARIANTS(write_cache_variants,
  23. SPINAND_PROG_LOAD_1S_1S_4S_OP(true, 0, NULL, 0),
  24. SPINAND_PROG_LOAD_1S_1S_1S_OP(true, 0, NULL, 0));
  25. static SPINAND_OP_VARIANTS(update_cache_variants,
  26. SPINAND_PROG_LOAD_1S_1S_4S_OP(false, 0, NULL, 0),
  27. SPINAND_PROG_LOAD_1S_1S_1S_OP(false, 0, NULL, 0));
  28. /*
  29. * OOB spare area map (64 bytes)
  30. *
  31. * Bad Block Markers
  32. * filled by HW and kernel Reserved
  33. * | +-----------------------+-----------------------+
  34. * | | | |
  35. * | | OOB free data Area |non ECC protected |
  36. * | +-------------|-----+-----------------|-----+-----------------|-----+
  37. * | | | | | | | |
  38. * +-|---|----------+--|-----|--------------+--|-----|--------------+--|-----|--------------+
  39. * | | | section0 | | | section1 | | | section2 | | | section3 |
  40. * +-v-+-v-+---+----+--v--+--v--+-----+-----+--v--+--v--+-----+-----+--v--+--v--+-----+-----+
  41. * | | | | | | | | | | | | | | | | |
  42. * |0:1|2:3|4:7|8:15|16:17|18:19|20:23|24:31|32:33|34:35|36:39|40:47|48:49|50:51|52:55|56:63|
  43. * | | | | | | | | | | | | | | | | |
  44. * +---+---+-^-+--^-+-----+-----+--^--+--^--+-----+-----+--^--+--^--+-----+-----+--^--+--^--+
  45. * | | | | | | | |
  46. * | +----------------|-----+-----------------|-----+-----------------|-----+
  47. * | ECC Area|(Main + Spare) - filled|by ESMT NAND HW |
  48. * | | | |
  49. * +---------------------+-----------------------+-----------------------+
  50. * OOB ECC protected Area - not used due to
  51. * partial programming from some filesystems
  52. * (like JFFS2 with cleanmarkers)
  53. */
  54. #define ESMT_OOB_SECTION_COUNT 4
  55. #define ESMT_OOB_SECTION_SIZE(nand) \
  56. (nanddev_per_page_oobsize(nand) / ESMT_OOB_SECTION_COUNT)
  57. #define ESMT_OOB_FREE_SIZE(nand) \
  58. (ESMT_OOB_SECTION_SIZE(nand) / 2)
  59. #define ESMT_OOB_ECC_SIZE(nand) \
  60. (ESMT_OOB_SECTION_SIZE(nand) - ESMT_OOB_FREE_SIZE(nand))
  61. #define ESMT_OOB_BBM_SIZE 2
  62. static int f50l1g41lb_ooblayout_ecc(struct mtd_info *mtd, int section,
  63. struct mtd_oob_region *region)
  64. {
  65. struct nand_device *nand = mtd_to_nanddev(mtd);
  66. if (section >= ESMT_OOB_SECTION_COUNT)
  67. return -ERANGE;
  68. region->offset = section * ESMT_OOB_SECTION_SIZE(nand) +
  69. ESMT_OOB_FREE_SIZE(nand);
  70. region->length = ESMT_OOB_ECC_SIZE(nand);
  71. return 0;
  72. }
  73. static int f50l1g41lb_ooblayout_free(struct mtd_info *mtd, int section,
  74. struct mtd_oob_region *region)
  75. {
  76. struct nand_device *nand = mtd_to_nanddev(mtd);
  77. if (section >= ESMT_OOB_SECTION_COUNT)
  78. return -ERANGE;
  79. /*
  80. * Reserve space for bad blocks markers (section0) and
  81. * reserved bytes (sections 1-3)
  82. */
  83. region->offset = section * ESMT_OOB_SECTION_SIZE(nand) + 2;
  84. /* Use only 2 non-protected ECC bytes per each OOB section */
  85. region->length = 2;
  86. return 0;
  87. }
  88. static const struct mtd_ooblayout_ops f50l1g41lb_ooblayout = {
  89. .ecc = f50l1g41lb_ooblayout_ecc,
  90. .free = f50l1g41lb_ooblayout_free,
  91. };
  92. static int f50l1g41lb_otp_info(struct spinand_device *spinand, size_t len,
  93. struct otp_info *buf, size_t *retlen, bool user)
  94. {
  95. if (len < sizeof(*buf))
  96. return -EINVAL;
  97. buf->locked = 0;
  98. buf->start = 0;
  99. buf->length = user ? spinand_user_otp_size(spinand) :
  100. spinand_fact_otp_size(spinand);
  101. *retlen = sizeof(*buf);
  102. return 0;
  103. }
  104. static int f50l1g41lb_fact_otp_info(struct spinand_device *spinand, size_t len,
  105. struct otp_info *buf, size_t *retlen)
  106. {
  107. return f50l1g41lb_otp_info(spinand, len, buf, retlen, false);
  108. }
  109. static int f50l1g41lb_user_otp_info(struct spinand_device *spinand, size_t len,
  110. struct otp_info *buf, size_t *retlen)
  111. {
  112. return f50l1g41lb_otp_info(spinand, len, buf, retlen, true);
  113. }
  114. static int f50l1g41lb_otp_lock(struct spinand_device *spinand, loff_t from,
  115. size_t len)
  116. {
  117. struct spi_mem_op write_op = SPINAND_OP(spinand, wr_en);
  118. struct spi_mem_op exec_op = SPINAND_OP(spinand, prog_exec, 0);
  119. u8 status;
  120. int ret;
  121. ret = spinand_upd_cfg(spinand, ESMT_F50L1G41LB_CFG_OTP_LOCK,
  122. ESMT_F50L1G41LB_CFG_OTP_LOCK);
  123. if (!ret)
  124. return ret;
  125. ret = spi_mem_exec_op(spinand->spimem, &write_op);
  126. if (!ret)
  127. goto out;
  128. ret = spi_mem_exec_op(spinand->spimem, &exec_op);
  129. if (!ret)
  130. goto out;
  131. ret = spinand_wait(spinand,
  132. SPINAND_WRITE_INITIAL_DELAY_US,
  133. SPINAND_WRITE_POLL_DELAY_US,
  134. &status);
  135. if (!ret && (status & STATUS_PROG_FAILED))
  136. ret = -EIO;
  137. out:
  138. if (spinand_upd_cfg(spinand, ESMT_F50L1G41LB_CFG_OTP_LOCK, 0)) {
  139. dev_warn(&spinand_to_mtd(spinand)->dev,
  140. "Can not disable OTP mode\n");
  141. ret = -EIO;
  142. }
  143. return ret;
  144. }
  145. static const struct spinand_user_otp_ops f50l1g41lb_user_otp_ops = {
  146. .info = f50l1g41lb_user_otp_info,
  147. .lock = f50l1g41lb_otp_lock,
  148. .read = spinand_user_otp_read,
  149. .write = spinand_user_otp_write,
  150. };
  151. static const struct spinand_fact_otp_ops f50l1g41lb_fact_otp_ops = {
  152. .info = f50l1g41lb_fact_otp_info,
  153. .read = spinand_fact_otp_read,
  154. };
  155. static const struct spinand_info esmt_8c_spinand_table[] = {
  156. SPINAND_INFO("F50L1G41LC",
  157. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0x2C),
  158. NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
  159. NAND_ECCREQ(1, 512),
  160. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  161. &write_cache_variants,
  162. &update_cache_variants),
  163. 0,
  164. SPINAND_ECCINFO(&f50l1g41lb_ooblayout, NULL),
  165. SPINAND_USER_OTP_INFO(28, 2, &f50l1g41lb_user_otp_ops),
  166. SPINAND_FACT_OTP_INFO(2, 0, &f50l1g41lb_fact_otp_ops)),
  167. };
  168. static const struct spinand_info esmt_c8_spinand_table[] = {
  169. SPINAND_INFO("F50L1G41LB",
  170. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0x01, 0x7f,
  171. 0x7f, 0x7f),
  172. NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
  173. NAND_ECCREQ(1, 512),
  174. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  175. &write_cache_variants,
  176. &update_cache_variants),
  177. 0,
  178. SPINAND_ECCINFO(&f50l1g41lb_ooblayout, NULL),
  179. SPINAND_USER_OTP_INFO(28, 2, &f50l1g41lb_user_otp_ops),
  180. SPINAND_FACT_OTP_INFO(2, 0, &f50l1g41lb_fact_otp_ops)),
  181. SPINAND_INFO("F50D1G41LB",
  182. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0x11, 0x7f,
  183. 0x7f, 0x7f),
  184. NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
  185. NAND_ECCREQ(1, 512),
  186. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  187. &write_cache_variants,
  188. &update_cache_variants),
  189. 0,
  190. SPINAND_ECCINFO(&f50l1g41lb_ooblayout, NULL),
  191. SPINAND_USER_OTP_INFO(28, 2, &f50l1g41lb_user_otp_ops),
  192. SPINAND_FACT_OTP_INFO(2, 0, &f50l1g41lb_fact_otp_ops)),
  193. SPINAND_INFO("F50D2G41KA",
  194. SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0x51, 0x7f,
  195. 0x7f, 0x7f),
  196. NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
  197. NAND_ECCREQ(8, 512),
  198. SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
  199. &write_cache_variants,
  200. &update_cache_variants),
  201. 0,
  202. SPINAND_ECCINFO(&f50l1g41lb_ooblayout, NULL)),
  203. };
  204. static const struct spinand_manufacturer_ops esmt_spinand_manuf_ops = {
  205. };
  206. const struct spinand_manufacturer esmt_8c_spinand_manufacturer = {
  207. .id = SPINAND_MFR_ESMT_8C,
  208. .name = "ESMT",
  209. .chips = esmt_8c_spinand_table,
  210. .nchips = ARRAY_SIZE(esmt_8c_spinand_table),
  211. .ops = &esmt_spinand_manuf_ops,
  212. };
  213. const struct spinand_manufacturer esmt_c8_spinand_manufacturer = {
  214. .id = SPINAND_MFR_ESMT_C8,
  215. .name = "ESMT",
  216. .chips = esmt_c8_spinand_table,
  217. .nchips = ARRAY_SIZE(esmt_c8_spinand_table),
  218. .ops = &esmt_spinand_manuf_ops,
  219. };