ecc-realtek.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Support for Realtek hardware ECC engine in RTL93xx SoCs
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/dma-mapping.h>
  7. #include <linux/mtd/nand.h>
  8. #include <linux/mutex.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regmap.h>
  11. /*
  12. * The Realtek ECC engine has two operation modes.
  13. *
  14. * - BCH6 : Generate 10 ECC bytes from 512 data bytes plus 6 free bytes
  15. * - BCH12 : Generate 20 ECC bytes from 512 data bytes plus 6 free bytes
  16. *
  17. * It can run for arbitrary NAND flash chips with different block and OOB sizes. Currently there
  18. * are only two known devices in the wild that have NAND flash and make use of this ECC engine
  19. * (Linksys LGS328C & LGS352C). To keep compatibility with vendor firmware, new modes can only
  20. * be added when new data layouts have been analyzed. For now allow BCH6 on flash with 2048 byte
  21. * blocks and 64 bytes oob.
  22. *
  23. * This driver aligns with kernel ECC naming conventions. Neverthless a short notice on the
  24. * Realtek naming conventions for the different structures in the OOB area.
  25. *
  26. * - BBI : Bad block indicator. The first two bytes of OOB. Protected by ECC!
  27. * - tag : 6 User/free bytes. First tag "contains" 2 bytes BBI. Protected by ECC!
  28. * - syndrome : ECC/parity bytes
  29. *
  30. * Altogether this gives currently the following block layout.
  31. *
  32. * +------+------+------+------+-----+------+------+------+------+-----+-----+-----+-----+
  33. * | 512 | 512 | 512 | 512 | 2 | 4 | 6 | 6 | 6 | 10 | 10 | 10 | 10 |
  34. * +------+------+------+------+-----+------+------+------+------+-----+-----+-----+-----+
  35. * | data | data | data | data | BBI | free | free | free | free | ECC | ECC | ECC | ECC |
  36. * +------+------+------+------+-----+------+------+------+------+-----+-----+-----+-----+
  37. */
  38. #define RTL_ECC_ALLOWED_PAGE_SIZE 2048
  39. #define RTL_ECC_ALLOWED_OOB_SIZE 64
  40. #define RTL_ECC_ALLOWED_STRENGTH 6
  41. #define RTL_ECC_BLOCK_SIZE 512
  42. #define RTL_ECC_FREE_SIZE 6
  43. #define RTL_ECC_PARITY_SIZE_BCH6 10
  44. #define RTL_ECC_PARITY_SIZE_BCH12 20
  45. /*
  46. * The engine is fed with two DMA regions. One for data (always 512 bytes) and one for free bytes
  47. * and parity (either 16 bytes for BCH6 or 26 bytes for BCH12). Start and length of each must be
  48. * aligned to a multiple of 4.
  49. */
  50. #define RTL_ECC_DMA_FREE_PARITY_SIZE ALIGN(RTL_ECC_FREE_SIZE + RTL_ECC_PARITY_SIZE_BCH12, 4)
  51. #define RTL_ECC_DMA_SIZE (RTL_ECC_BLOCK_SIZE + RTL_ECC_DMA_FREE_PARITY_SIZE)
  52. #define RTL_ECC_CFG 0x00
  53. #define RTL_ECC_BCH6 0
  54. #define RTL_ECC_BCH12 BIT(28)
  55. #define RTL_ECC_DMA_PRECISE BIT(12)
  56. #define RTL_ECC_BURST_128 GENMASK(1, 0)
  57. #define RTL_ECC_DMA_TRIGGER 0x08
  58. #define RTL_ECC_OP_DECODE 0
  59. #define RTL_ECC_OP_ENCODE BIT(0)
  60. #define RTL_ECC_DMA_START 0x0c
  61. #define RTL_ECC_DMA_TAG 0x10
  62. #define RTL_ECC_STATUS 0x14
  63. #define RTL_ECC_CORR_COUNT GENMASK(19, 12)
  64. #define RTL_ECC_RESULT BIT(8)
  65. #define RTL_ECC_ALL_ONE BIT(4)
  66. #define RTL_ECC_OP_STATUS BIT(0)
  67. struct rtl_ecc_engine {
  68. struct device *dev;
  69. struct nand_ecc_engine engine;
  70. struct mutex lock;
  71. char *buf;
  72. dma_addr_t buf_dma;
  73. struct regmap *regmap;
  74. };
  75. struct rtl_ecc_ctx {
  76. struct rtl_ecc_engine * rtlc;
  77. struct nand_ecc_req_tweak_ctx req_ctx;
  78. int steps;
  79. int bch_mode;
  80. int strength;
  81. int parity_size;
  82. };
  83. static const struct regmap_config rtl_ecc_regmap_config = {
  84. .reg_bits = 32,
  85. .val_bits = 32,
  86. .reg_stride = 4,
  87. };
  88. static inline void *nand_to_ctx(struct nand_device *nand)
  89. {
  90. return nand->ecc.ctx.priv;
  91. }
  92. static inline struct rtl_ecc_engine *nand_to_rtlc(struct nand_device *nand)
  93. {
  94. struct nand_ecc_engine *eng = nand->ecc.engine;
  95. return container_of(eng, struct rtl_ecc_engine, engine);
  96. }
  97. static int rtl_ecc_ooblayout_ecc(struct mtd_info *mtd, int section,
  98. struct mtd_oob_region *oobregion)
  99. {
  100. struct nand_device *nand = mtd_to_nanddev(mtd);
  101. struct rtl_ecc_ctx *ctx = nand_to_ctx(nand);
  102. if (section < 0 || section >= ctx->steps)
  103. return -ERANGE;
  104. oobregion->offset = ctx->steps * RTL_ECC_FREE_SIZE + section * ctx->parity_size;
  105. oobregion->length = ctx->parity_size;
  106. return 0;
  107. }
  108. static int rtl_ecc_ooblayout_free(struct mtd_info *mtd, int section,
  109. struct mtd_oob_region *oobregion)
  110. {
  111. struct nand_device *nand = mtd_to_nanddev(mtd);
  112. struct rtl_ecc_ctx *ctx = nand_to_ctx(nand);
  113. int bbm;
  114. if (section < 0 || section >= ctx->steps)
  115. return -ERANGE;
  116. /* reserve 2 BBM bytes in first block */
  117. bbm = section ? 0 : 2;
  118. oobregion->offset = section * RTL_ECC_FREE_SIZE + bbm;
  119. oobregion->length = RTL_ECC_FREE_SIZE - bbm;
  120. return 0;
  121. }
  122. static const struct mtd_ooblayout_ops rtl_ecc_ooblayout_ops = {
  123. .ecc = rtl_ecc_ooblayout_ecc,
  124. .free = rtl_ecc_ooblayout_free,
  125. };
  126. static void rtl_ecc_kick_engine(struct rtl_ecc_ctx *ctx, int operation)
  127. {
  128. struct rtl_ecc_engine *rtlc = ctx->rtlc;
  129. regmap_write(rtlc->regmap, RTL_ECC_CFG,
  130. ctx->bch_mode | RTL_ECC_BURST_128 | RTL_ECC_DMA_PRECISE);
  131. regmap_write(rtlc->regmap, RTL_ECC_DMA_START, rtlc->buf_dma);
  132. regmap_write(rtlc->regmap, RTL_ECC_DMA_TAG, rtlc->buf_dma + RTL_ECC_BLOCK_SIZE);
  133. regmap_write(rtlc->regmap, RTL_ECC_DMA_TRIGGER, operation);
  134. }
  135. static int rtl_ecc_wait_for_engine(struct rtl_ecc_ctx *ctx)
  136. {
  137. struct rtl_ecc_engine *rtlc = ctx->rtlc;
  138. int ret, status, bitflips;
  139. bool all_one;
  140. /*
  141. * The ECC engine needs 6-8 us to encode/decode a BCH6 syndrome for 512 bytes of data
  142. * and 6 free bytes. In case the NAND area has been erased and all data and oob is
  143. * set to 0xff, decoding takes 30us (reason unknown). Although the engine can trigger
  144. * interrupts when finished, use active polling for now. 12 us maximum wait time has
  145. * proven to be a good tradeoff between performance and overhead.
  146. */
  147. ret = regmap_read_poll_timeout(rtlc->regmap, RTL_ECC_STATUS, status,
  148. !(status & RTL_ECC_OP_STATUS), 12, 1000000);
  149. if (ret)
  150. return ret;
  151. ret = FIELD_GET(RTL_ECC_RESULT, status);
  152. all_one = FIELD_GET(RTL_ECC_ALL_ONE, status);
  153. bitflips = FIELD_GET(RTL_ECC_CORR_COUNT, status);
  154. /* For erased blocks (all bits one) error status can be ignored */
  155. if (all_one)
  156. ret = 0;
  157. return ret ? -EBADMSG : bitflips;
  158. }
  159. static int rtl_ecc_run_engine(struct rtl_ecc_ctx *ctx, char *data, char *free,
  160. char *parity, int operation)
  161. {
  162. struct rtl_ecc_engine *rtlc = ctx->rtlc;
  163. char *buf_parity = rtlc->buf + RTL_ECC_BLOCK_SIZE + RTL_ECC_FREE_SIZE;
  164. char *buf_free = rtlc->buf + RTL_ECC_BLOCK_SIZE;
  165. char *buf_data = rtlc->buf;
  166. int ret;
  167. mutex_lock(&rtlc->lock);
  168. memcpy(buf_data, data, RTL_ECC_BLOCK_SIZE);
  169. memcpy(buf_free, free, RTL_ECC_FREE_SIZE);
  170. memcpy(buf_parity, parity, ctx->parity_size);
  171. dma_sync_single_for_device(rtlc->dev, rtlc->buf_dma, RTL_ECC_DMA_SIZE, DMA_TO_DEVICE);
  172. rtl_ecc_kick_engine(ctx, operation);
  173. ret = rtl_ecc_wait_for_engine(ctx);
  174. dma_sync_single_for_cpu(rtlc->dev, rtlc->buf_dma, RTL_ECC_DMA_SIZE, DMA_FROM_DEVICE);
  175. if (ret >= 0) {
  176. memcpy(data, buf_data, RTL_ECC_BLOCK_SIZE);
  177. memcpy(free, buf_free, RTL_ECC_FREE_SIZE);
  178. memcpy(parity, buf_parity, ctx->parity_size);
  179. }
  180. mutex_unlock(&rtlc->lock);
  181. return ret;
  182. }
  183. static int rtl_ecc_prepare_io_req(struct nand_device *nand, struct nand_page_io_req *req)
  184. {
  185. struct rtl_ecc_engine *rtlc = nand_to_rtlc(nand);
  186. struct rtl_ecc_ctx *ctx = nand_to_ctx(nand);
  187. char *data, *free, *parity;
  188. int ret = 0;
  189. if (req->mode == MTD_OPS_RAW)
  190. return 0;
  191. nand_ecc_tweak_req(&ctx->req_ctx, req);
  192. if (req->type == NAND_PAGE_READ)
  193. return 0;
  194. free = req->oobbuf.in;
  195. data = req->databuf.in;
  196. parity = req->oobbuf.in + ctx->steps * RTL_ECC_FREE_SIZE;
  197. for (int i = 0; i < ctx->steps; i++) {
  198. ret |= rtl_ecc_run_engine(ctx, data, free, parity, RTL_ECC_OP_ENCODE);
  199. free += RTL_ECC_FREE_SIZE;
  200. data += RTL_ECC_BLOCK_SIZE;
  201. parity += ctx->parity_size;
  202. }
  203. if (unlikely(ret))
  204. dev_dbg(rtlc->dev, "ECC calculation failed\n");
  205. return ret ? -EBADMSG : 0;
  206. }
  207. static int rtl_ecc_finish_io_req(struct nand_device *nand, struct nand_page_io_req *req)
  208. {
  209. struct rtl_ecc_engine *rtlc = nand_to_rtlc(nand);
  210. struct rtl_ecc_ctx *ctx = nand_to_ctx(nand);
  211. struct mtd_info *mtd = nanddev_to_mtd(nand);
  212. char *data, *free, *parity;
  213. bool failure = false;
  214. int bitflips = 0;
  215. if (req->mode == MTD_OPS_RAW)
  216. return 0;
  217. if (req->type == NAND_PAGE_WRITE) {
  218. nand_ecc_restore_req(&ctx->req_ctx, req);
  219. return 0;
  220. }
  221. free = req->oobbuf.in;
  222. data = req->databuf.in;
  223. parity = req->oobbuf.in + ctx->steps * RTL_ECC_FREE_SIZE;
  224. for (int i = 0 ; i < ctx->steps; i++) {
  225. int ret = rtl_ecc_run_engine(ctx, data, free, parity, RTL_ECC_OP_DECODE);
  226. if (unlikely(ret < 0))
  227. /* ECC totally fails for bitflips in erased blocks */
  228. ret = nand_check_erased_ecc_chunk(data, RTL_ECC_BLOCK_SIZE,
  229. parity, ctx->parity_size,
  230. free, RTL_ECC_FREE_SIZE,
  231. ctx->strength);
  232. if (unlikely(ret < 0)) {
  233. failure = true;
  234. mtd->ecc_stats.failed++;
  235. } else {
  236. mtd->ecc_stats.corrected += ret;
  237. bitflips = max_t(unsigned int, bitflips, ret);
  238. }
  239. free += RTL_ECC_FREE_SIZE;
  240. data += RTL_ECC_BLOCK_SIZE;
  241. parity += ctx->parity_size;
  242. }
  243. nand_ecc_restore_req(&ctx->req_ctx, req);
  244. if (unlikely(failure))
  245. dev_dbg(rtlc->dev, "ECC correction failed\n");
  246. else if (unlikely(bitflips > 2))
  247. dev_dbg(rtlc->dev, "%d bitflips detected\n", bitflips);
  248. return failure ? -EBADMSG : bitflips;
  249. }
  250. static int rtl_ecc_check_support(struct nand_device *nand)
  251. {
  252. struct mtd_info *mtd = nanddev_to_mtd(nand);
  253. struct device *dev = nand->ecc.engine->dev;
  254. if (mtd->oobsize != RTL_ECC_ALLOWED_OOB_SIZE ||
  255. mtd->writesize != RTL_ECC_ALLOWED_PAGE_SIZE) {
  256. dev_err(dev, "only flash geometry data=%d, oob=%d supported\n",
  257. RTL_ECC_ALLOWED_PAGE_SIZE, RTL_ECC_ALLOWED_OOB_SIZE);
  258. return -EINVAL;
  259. }
  260. if (nand->ecc.user_conf.algo != NAND_ECC_ALGO_BCH ||
  261. nand->ecc.user_conf.strength != RTL_ECC_ALLOWED_STRENGTH ||
  262. nand->ecc.user_conf.placement != NAND_ECC_PLACEMENT_OOB ||
  263. nand->ecc.user_conf.step_size != RTL_ECC_BLOCK_SIZE) {
  264. dev_err(dev, "only algo=bch, strength=%d, placement=oob, step=%d supported\n",
  265. RTL_ECC_ALLOWED_STRENGTH, RTL_ECC_BLOCK_SIZE);
  266. return -EINVAL;
  267. }
  268. return 0;
  269. }
  270. static int rtl_ecc_init_ctx(struct nand_device *nand)
  271. {
  272. struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
  273. struct rtl_ecc_engine *rtlc = nand_to_rtlc(nand);
  274. struct mtd_info *mtd = nanddev_to_mtd(nand);
  275. int strength = nand->ecc.user_conf.strength;
  276. struct device *dev = nand->ecc.engine->dev;
  277. struct rtl_ecc_ctx *ctx;
  278. int ret;
  279. ret = rtl_ecc_check_support(nand);
  280. if (ret)
  281. return ret;
  282. ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
  283. if (!ctx)
  284. return -ENOMEM;
  285. nand->ecc.ctx.priv = ctx;
  286. mtd_set_ooblayout(mtd, &rtl_ecc_ooblayout_ops);
  287. conf->algo = NAND_ECC_ALGO_BCH;
  288. conf->strength = strength;
  289. conf->step_size = RTL_ECC_BLOCK_SIZE;
  290. conf->engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
  291. ctx->rtlc = rtlc;
  292. ctx->steps = mtd->writesize / RTL_ECC_BLOCK_SIZE;
  293. ctx->strength = strength;
  294. ctx->bch_mode = strength == 6 ? RTL_ECC_BCH6 : RTL_ECC_BCH12;
  295. ctx->parity_size = strength == 6 ? RTL_ECC_PARITY_SIZE_BCH6 : RTL_ECC_PARITY_SIZE_BCH12;
  296. ret = nand_ecc_init_req_tweaking(&ctx->req_ctx, nand);
  297. if (ret)
  298. return ret;
  299. dev_dbg(dev, "using bch%d with geometry data=%dx%d, free=%dx6, parity=%dx%d",
  300. conf->strength, ctx->steps, conf->step_size,
  301. ctx->steps, ctx->steps, ctx->parity_size);
  302. return 0;
  303. }
  304. static void rtl_ecc_cleanup_ctx(struct nand_device *nand)
  305. {
  306. struct rtl_ecc_ctx *ctx = nand_to_ctx(nand);
  307. if (ctx)
  308. nand_ecc_cleanup_req_tweaking(&ctx->req_ctx);
  309. }
  310. static const struct nand_ecc_engine_ops rtl_ecc_engine_ops = {
  311. .init_ctx = rtl_ecc_init_ctx,
  312. .cleanup_ctx = rtl_ecc_cleanup_ctx,
  313. .prepare_io_req = rtl_ecc_prepare_io_req,
  314. .finish_io_req = rtl_ecc_finish_io_req,
  315. };
  316. static int rtl_ecc_probe(struct platform_device *pdev)
  317. {
  318. struct device *dev = &pdev->dev;
  319. struct rtl_ecc_engine *rtlc;
  320. void __iomem *base;
  321. int ret;
  322. rtlc = devm_kzalloc(dev, sizeof(*rtlc), GFP_KERNEL);
  323. if (!rtlc)
  324. return -ENOMEM;
  325. base = devm_platform_ioremap_resource(pdev, 0);
  326. if (IS_ERR(base))
  327. return PTR_ERR(base);
  328. ret = devm_mutex_init(dev, &rtlc->lock);
  329. if (ret)
  330. return ret;
  331. rtlc->regmap = devm_regmap_init_mmio(dev, base, &rtl_ecc_regmap_config);
  332. if (IS_ERR(rtlc->regmap))
  333. return PTR_ERR(rtlc->regmap);
  334. /*
  335. * Focus on simplicity and use a preallocated DMA buffer for data exchange with the
  336. * engine. For now make it a noncoherent memory model as invalidating/flushing caches
  337. * is faster than reading/writing uncached memory on the known architectures.
  338. */
  339. rtlc->buf = dma_alloc_noncoherent(dev, RTL_ECC_DMA_SIZE, &rtlc->buf_dma,
  340. DMA_BIDIRECTIONAL, GFP_KERNEL);
  341. if (!rtlc->buf)
  342. return -ENOMEM;
  343. rtlc->dev = dev;
  344. rtlc->engine.dev = dev;
  345. rtlc->engine.ops = &rtl_ecc_engine_ops;
  346. rtlc->engine.integration = NAND_ECC_ENGINE_INTEGRATION_EXTERNAL;
  347. nand_ecc_register_on_host_hw_engine(&rtlc->engine);
  348. platform_set_drvdata(pdev, rtlc);
  349. return 0;
  350. }
  351. static void rtl_ecc_remove(struct platform_device *pdev)
  352. {
  353. struct rtl_ecc_engine *rtlc = platform_get_drvdata(pdev);
  354. nand_ecc_unregister_on_host_hw_engine(&rtlc->engine);
  355. dma_free_noncoherent(rtlc->dev, RTL_ECC_DMA_SIZE, rtlc->buf, rtlc->buf_dma,
  356. DMA_BIDIRECTIONAL);
  357. }
  358. static const struct of_device_id rtl_ecc_of_ids[] = {
  359. {
  360. .compatible = "realtek,rtl9301-ecc",
  361. },
  362. { /* sentinel */ },
  363. };
  364. static struct platform_driver rtl_ecc_driver = {
  365. .driver = {
  366. .name = "rtl-nand-ecc-engine",
  367. .of_match_table = rtl_ecc_of_ids,
  368. },
  369. .probe = rtl_ecc_probe,
  370. .remove = rtl_ecc_remove,
  371. };
  372. module_platform_driver(rtl_ecc_driver);
  373. MODULE_LICENSE("GPL");
  374. MODULE_AUTHOR("Markus Stockhausen <markus.stockhausen@gmx.de>");
  375. MODULE_DESCRIPTION("Realtek NAND hardware ECC controller");