mtk_ecc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. /*
  2. * MTK ECC controller driver.
  3. * Copyright (C) 2016 MediaTek Inc.
  4. * Authors: Xiaolei Li <xiaolei.li@mediatek.com>
  5. * Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/platform_device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/clk.h>
  20. #include <linux/module.h>
  21. #include <linux/iopoll.h>
  22. #include <linux/of.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/mutex.h>
  25. #include "mtk_ecc.h"
  26. #define ECC_IDLE_MASK BIT(0)
  27. #define ECC_IRQ_EN BIT(0)
  28. #define ECC_OP_ENABLE (1)
  29. #define ECC_OP_DISABLE (0)
  30. #define ECC_ENCCON (0x00)
  31. #define ECC_ENCCNFG (0x04)
  32. #define ECC_CNFG_4BIT (0)
  33. #define ECC_CNFG_6BIT (1)
  34. #define ECC_CNFG_8BIT (2)
  35. #define ECC_CNFG_10BIT (3)
  36. #define ECC_CNFG_12BIT (4)
  37. #define ECC_CNFG_14BIT (5)
  38. #define ECC_CNFG_16BIT (6)
  39. #define ECC_CNFG_18BIT (7)
  40. #define ECC_CNFG_20BIT (8)
  41. #define ECC_CNFG_22BIT (9)
  42. #define ECC_CNFG_24BIT (0xa)
  43. #define ECC_CNFG_28BIT (0xb)
  44. #define ECC_CNFG_32BIT (0xc)
  45. #define ECC_CNFG_36BIT (0xd)
  46. #define ECC_CNFG_40BIT (0xe)
  47. #define ECC_CNFG_44BIT (0xf)
  48. #define ECC_CNFG_48BIT (0x10)
  49. #define ECC_CNFG_52BIT (0x11)
  50. #define ECC_CNFG_56BIT (0x12)
  51. #define ECC_CNFG_60BIT (0x13)
  52. #define ECC_MODE_SHIFT (5)
  53. #define ECC_MS_SHIFT (16)
  54. #define ECC_ENCDIADDR (0x08)
  55. #define ECC_ENCIDLE (0x0C)
  56. #define ECC_ENCPAR(x) (0x10 + (x) * sizeof(u32))
  57. #define ECC_ENCIRQ_EN (0x80)
  58. #define ECC_ENCIRQ_STA (0x84)
  59. #define ECC_DECCON (0x100)
  60. #define ECC_DECCNFG (0x104)
  61. #define DEC_EMPTY_EN BIT(31)
  62. #define DEC_CNFG_CORRECT (0x3 << 12)
  63. #define ECC_DECIDLE (0x10C)
  64. #define ECC_DECENUM0 (0x114)
  65. #define ERR_MASK (0x3f)
  66. #define ECC_DECDONE (0x124)
  67. #define ECC_DECIRQ_EN (0x200)
  68. #define ECC_DECIRQ_STA (0x204)
  69. #define ECC_TIMEOUT (500000)
  70. #define ECC_IDLE_REG(op) ((op) == ECC_ENCODE ? ECC_ENCIDLE : ECC_DECIDLE)
  71. #define ECC_CTL_REG(op) ((op) == ECC_ENCODE ? ECC_ENCCON : ECC_DECCON)
  72. #define ECC_IRQ_REG(op) ((op) == ECC_ENCODE ? \
  73. ECC_ENCIRQ_EN : ECC_DECIRQ_EN)
  74. struct mtk_ecc {
  75. struct device *dev;
  76. void __iomem *regs;
  77. struct clk *clk;
  78. struct completion done;
  79. struct mutex lock;
  80. u32 sectors;
  81. u8 eccdata[112];
  82. };
  83. static inline void mtk_ecc_wait_idle(struct mtk_ecc *ecc,
  84. enum mtk_ecc_operation op)
  85. {
  86. struct device *dev = ecc->dev;
  87. u32 val;
  88. int ret;
  89. ret = readl_poll_timeout_atomic(ecc->regs + ECC_IDLE_REG(op), val,
  90. val & ECC_IDLE_MASK,
  91. 10, ECC_TIMEOUT);
  92. if (ret)
  93. dev_warn(dev, "%s NOT idle\n",
  94. op == ECC_ENCODE ? "encoder" : "decoder");
  95. }
  96. static irqreturn_t mtk_ecc_irq(int irq, void *id)
  97. {
  98. struct mtk_ecc *ecc = id;
  99. enum mtk_ecc_operation op;
  100. u32 dec, enc;
  101. dec = readw(ecc->regs + ECC_DECIRQ_STA) & ECC_IRQ_EN;
  102. if (dec) {
  103. op = ECC_DECODE;
  104. dec = readw(ecc->regs + ECC_DECDONE);
  105. if (dec & ecc->sectors) {
  106. /*
  107. * Clear decode IRQ status once again to ensure that
  108. * there will be no extra IRQ.
  109. */
  110. readw(ecc->regs + ECC_DECIRQ_STA);
  111. ecc->sectors = 0;
  112. complete(&ecc->done);
  113. } else {
  114. return IRQ_HANDLED;
  115. }
  116. } else {
  117. enc = readl(ecc->regs + ECC_ENCIRQ_STA) & ECC_IRQ_EN;
  118. if (enc) {
  119. op = ECC_ENCODE;
  120. complete(&ecc->done);
  121. } else {
  122. return IRQ_NONE;
  123. }
  124. }
  125. return IRQ_HANDLED;
  126. }
  127. static void mtk_ecc_config(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
  128. {
  129. u32 ecc_bit = ECC_CNFG_4BIT, dec_sz, enc_sz;
  130. u32 reg;
  131. switch (config->strength) {
  132. case 4:
  133. ecc_bit = ECC_CNFG_4BIT;
  134. break;
  135. case 6:
  136. ecc_bit = ECC_CNFG_6BIT;
  137. break;
  138. case 8:
  139. ecc_bit = ECC_CNFG_8BIT;
  140. break;
  141. case 10:
  142. ecc_bit = ECC_CNFG_10BIT;
  143. break;
  144. case 12:
  145. ecc_bit = ECC_CNFG_12BIT;
  146. break;
  147. case 14:
  148. ecc_bit = ECC_CNFG_14BIT;
  149. break;
  150. case 16:
  151. ecc_bit = ECC_CNFG_16BIT;
  152. break;
  153. case 18:
  154. ecc_bit = ECC_CNFG_18BIT;
  155. break;
  156. case 20:
  157. ecc_bit = ECC_CNFG_20BIT;
  158. break;
  159. case 22:
  160. ecc_bit = ECC_CNFG_22BIT;
  161. break;
  162. case 24:
  163. ecc_bit = ECC_CNFG_24BIT;
  164. break;
  165. case 28:
  166. ecc_bit = ECC_CNFG_28BIT;
  167. break;
  168. case 32:
  169. ecc_bit = ECC_CNFG_32BIT;
  170. break;
  171. case 36:
  172. ecc_bit = ECC_CNFG_36BIT;
  173. break;
  174. case 40:
  175. ecc_bit = ECC_CNFG_40BIT;
  176. break;
  177. case 44:
  178. ecc_bit = ECC_CNFG_44BIT;
  179. break;
  180. case 48:
  181. ecc_bit = ECC_CNFG_48BIT;
  182. break;
  183. case 52:
  184. ecc_bit = ECC_CNFG_52BIT;
  185. break;
  186. case 56:
  187. ecc_bit = ECC_CNFG_56BIT;
  188. break;
  189. case 60:
  190. ecc_bit = ECC_CNFG_60BIT;
  191. break;
  192. default:
  193. dev_err(ecc->dev, "invalid strength %d, default to 4 bits\n",
  194. config->strength);
  195. }
  196. if (config->op == ECC_ENCODE) {
  197. /* configure ECC encoder (in bits) */
  198. enc_sz = config->len << 3;
  199. reg = ecc_bit | (config->mode << ECC_MODE_SHIFT);
  200. reg |= (enc_sz << ECC_MS_SHIFT);
  201. writel(reg, ecc->regs + ECC_ENCCNFG);
  202. if (config->mode != ECC_NFI_MODE)
  203. writel(lower_32_bits(config->addr),
  204. ecc->regs + ECC_ENCDIADDR);
  205. } else {
  206. /* configure ECC decoder (in bits) */
  207. dec_sz = (config->len << 3) +
  208. config->strength * ECC_PARITY_BITS;
  209. reg = ecc_bit | (config->mode << ECC_MODE_SHIFT);
  210. reg |= (dec_sz << ECC_MS_SHIFT) | DEC_CNFG_CORRECT;
  211. reg |= DEC_EMPTY_EN;
  212. writel(reg, ecc->regs + ECC_DECCNFG);
  213. if (config->sectors)
  214. ecc->sectors = 1 << (config->sectors - 1);
  215. }
  216. }
  217. void mtk_ecc_get_stats(struct mtk_ecc *ecc, struct mtk_ecc_stats *stats,
  218. int sectors)
  219. {
  220. u32 offset, i, err;
  221. u32 bitflips = 0;
  222. stats->corrected = 0;
  223. stats->failed = 0;
  224. for (i = 0; i < sectors; i++) {
  225. offset = (i >> 2) << 2;
  226. err = readl(ecc->regs + ECC_DECENUM0 + offset);
  227. err = err >> ((i % 4) * 8);
  228. err &= ERR_MASK;
  229. if (err == ERR_MASK) {
  230. /* uncorrectable errors */
  231. stats->failed++;
  232. continue;
  233. }
  234. stats->corrected += err;
  235. bitflips = max_t(u32, bitflips, err);
  236. }
  237. stats->bitflips = bitflips;
  238. }
  239. EXPORT_SYMBOL(mtk_ecc_get_stats);
  240. void mtk_ecc_release(struct mtk_ecc *ecc)
  241. {
  242. clk_disable_unprepare(ecc->clk);
  243. put_device(ecc->dev);
  244. }
  245. EXPORT_SYMBOL(mtk_ecc_release);
  246. static void mtk_ecc_hw_init(struct mtk_ecc *ecc)
  247. {
  248. mtk_ecc_wait_idle(ecc, ECC_ENCODE);
  249. writew(ECC_OP_DISABLE, ecc->regs + ECC_ENCCON);
  250. mtk_ecc_wait_idle(ecc, ECC_DECODE);
  251. writel(ECC_OP_DISABLE, ecc->regs + ECC_DECCON);
  252. }
  253. static struct mtk_ecc *mtk_ecc_get(struct device_node *np)
  254. {
  255. struct platform_device *pdev;
  256. struct mtk_ecc *ecc;
  257. pdev = of_find_device_by_node(np);
  258. if (!pdev || !platform_get_drvdata(pdev))
  259. return ERR_PTR(-EPROBE_DEFER);
  260. get_device(&pdev->dev);
  261. ecc = platform_get_drvdata(pdev);
  262. clk_prepare_enable(ecc->clk);
  263. mtk_ecc_hw_init(ecc);
  264. return ecc;
  265. }
  266. struct mtk_ecc *of_mtk_ecc_get(struct device_node *of_node)
  267. {
  268. struct mtk_ecc *ecc = NULL;
  269. struct device_node *np;
  270. np = of_parse_phandle(of_node, "ecc-engine", 0);
  271. if (np) {
  272. ecc = mtk_ecc_get(np);
  273. of_node_put(np);
  274. }
  275. return ecc;
  276. }
  277. EXPORT_SYMBOL(of_mtk_ecc_get);
  278. int mtk_ecc_enable(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
  279. {
  280. enum mtk_ecc_operation op = config->op;
  281. int ret;
  282. ret = mutex_lock_interruptible(&ecc->lock);
  283. if (ret) {
  284. dev_err(ecc->dev, "interrupted when attempting to lock\n");
  285. return ret;
  286. }
  287. mtk_ecc_wait_idle(ecc, op);
  288. mtk_ecc_config(ecc, config);
  289. writew(ECC_OP_ENABLE, ecc->regs + ECC_CTL_REG(op));
  290. init_completion(&ecc->done);
  291. writew(ECC_IRQ_EN, ecc->regs + ECC_IRQ_REG(op));
  292. return 0;
  293. }
  294. EXPORT_SYMBOL(mtk_ecc_enable);
  295. void mtk_ecc_disable(struct mtk_ecc *ecc)
  296. {
  297. enum mtk_ecc_operation op = ECC_ENCODE;
  298. /* find out the running operation */
  299. if (readw(ecc->regs + ECC_CTL_REG(op)) != ECC_OP_ENABLE)
  300. op = ECC_DECODE;
  301. /* disable it */
  302. mtk_ecc_wait_idle(ecc, op);
  303. if (op == ECC_DECODE)
  304. /*
  305. * Clear decode IRQ status in case there is a timeout to wait
  306. * decode IRQ.
  307. */
  308. readw(ecc->regs + ECC_DECIRQ_STA);
  309. writew(0, ecc->regs + ECC_IRQ_REG(op));
  310. writew(ECC_OP_DISABLE, ecc->regs + ECC_CTL_REG(op));
  311. mutex_unlock(&ecc->lock);
  312. }
  313. EXPORT_SYMBOL(mtk_ecc_disable);
  314. int mtk_ecc_wait_done(struct mtk_ecc *ecc, enum mtk_ecc_operation op)
  315. {
  316. int ret;
  317. ret = wait_for_completion_timeout(&ecc->done, msecs_to_jiffies(500));
  318. if (!ret) {
  319. dev_err(ecc->dev, "%s timeout - interrupt did not arrive)\n",
  320. (op == ECC_ENCODE) ? "encoder" : "decoder");
  321. return -ETIMEDOUT;
  322. }
  323. return 0;
  324. }
  325. EXPORT_SYMBOL(mtk_ecc_wait_done);
  326. int mtk_ecc_encode(struct mtk_ecc *ecc, struct mtk_ecc_config *config,
  327. u8 *data, u32 bytes)
  328. {
  329. dma_addr_t addr;
  330. u32 len;
  331. int ret;
  332. addr = dma_map_single(ecc->dev, data, bytes, DMA_TO_DEVICE);
  333. ret = dma_mapping_error(ecc->dev, addr);
  334. if (ret) {
  335. dev_err(ecc->dev, "dma mapping error\n");
  336. return -EINVAL;
  337. }
  338. config->op = ECC_ENCODE;
  339. config->addr = addr;
  340. ret = mtk_ecc_enable(ecc, config);
  341. if (ret) {
  342. dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
  343. return ret;
  344. }
  345. ret = mtk_ecc_wait_done(ecc, ECC_ENCODE);
  346. if (ret)
  347. goto timeout;
  348. mtk_ecc_wait_idle(ecc, ECC_ENCODE);
  349. /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */
  350. len = (config->strength * ECC_PARITY_BITS + 7) >> 3;
  351. /* write the parity bytes generated by the ECC back to temp buffer */
  352. __ioread32_copy(ecc->eccdata, ecc->regs + ECC_ENCPAR(0), round_up(len, 4));
  353. /* copy into possibly unaligned OOB region with actual length */
  354. memcpy(data + bytes, ecc->eccdata, len);
  355. timeout:
  356. dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
  357. mtk_ecc_disable(ecc);
  358. return ret;
  359. }
  360. EXPORT_SYMBOL(mtk_ecc_encode);
  361. void mtk_ecc_adjust_strength(u32 *p)
  362. {
  363. u32 ecc[] = {4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
  364. 40, 44, 48, 52, 56, 60};
  365. int i;
  366. for (i = 0; i < ARRAY_SIZE(ecc); i++) {
  367. if (*p <= ecc[i]) {
  368. if (!i)
  369. *p = ecc[i];
  370. else if (*p != ecc[i])
  371. *p = ecc[i - 1];
  372. return;
  373. }
  374. }
  375. *p = ecc[ARRAY_SIZE(ecc) - 1];
  376. }
  377. EXPORT_SYMBOL(mtk_ecc_adjust_strength);
  378. static int mtk_ecc_probe(struct platform_device *pdev)
  379. {
  380. struct device *dev = &pdev->dev;
  381. struct mtk_ecc *ecc;
  382. struct resource *res;
  383. int irq, ret;
  384. ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
  385. if (!ecc)
  386. return -ENOMEM;
  387. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  388. ecc->regs = devm_ioremap_resource(dev, res);
  389. if (IS_ERR(ecc->regs)) {
  390. dev_err(dev, "failed to map regs: %ld\n", PTR_ERR(ecc->regs));
  391. return PTR_ERR(ecc->regs);
  392. }
  393. ecc->clk = devm_clk_get(dev, NULL);
  394. if (IS_ERR(ecc->clk)) {
  395. dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk));
  396. return PTR_ERR(ecc->clk);
  397. }
  398. irq = platform_get_irq(pdev, 0);
  399. if (irq < 0) {
  400. dev_err(dev, "failed to get irq\n");
  401. return -EINVAL;
  402. }
  403. ret = dma_set_mask(dev, DMA_BIT_MASK(32));
  404. if (ret) {
  405. dev_err(dev, "failed to set DMA mask\n");
  406. return ret;
  407. }
  408. ret = devm_request_irq(dev, irq, mtk_ecc_irq, 0x0, "mtk-ecc", ecc);
  409. if (ret) {
  410. dev_err(dev, "failed to request irq\n");
  411. return -EINVAL;
  412. }
  413. ecc->dev = dev;
  414. mutex_init(&ecc->lock);
  415. platform_set_drvdata(pdev, ecc);
  416. dev_info(dev, "probed\n");
  417. return 0;
  418. }
  419. #ifdef CONFIG_PM_SLEEP
  420. static int mtk_ecc_suspend(struct device *dev)
  421. {
  422. struct mtk_ecc *ecc = dev_get_drvdata(dev);
  423. clk_disable_unprepare(ecc->clk);
  424. return 0;
  425. }
  426. static int mtk_ecc_resume(struct device *dev)
  427. {
  428. struct mtk_ecc *ecc = dev_get_drvdata(dev);
  429. int ret;
  430. ret = clk_prepare_enable(ecc->clk);
  431. if (ret) {
  432. dev_err(dev, "failed to enable clk\n");
  433. return ret;
  434. }
  435. mtk_ecc_hw_init(ecc);
  436. return 0;
  437. }
  438. static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops, mtk_ecc_suspend, mtk_ecc_resume);
  439. #endif
  440. static const struct of_device_id mtk_ecc_dt_match[] = {
  441. { .compatible = "mediatek,mt2701-ecc" },
  442. {},
  443. };
  444. MODULE_DEVICE_TABLE(of, mtk_ecc_dt_match);
  445. static struct platform_driver mtk_ecc_driver = {
  446. .probe = mtk_ecc_probe,
  447. .driver = {
  448. .name = "mtk-ecc",
  449. .of_match_table = of_match_ptr(mtk_ecc_dt_match),
  450. #ifdef CONFIG_PM_SLEEP
  451. .pm = &mtk_ecc_pm_ops,
  452. #endif
  453. },
  454. };
  455. module_platform_driver(mtk_ecc_driver);
  456. MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
  457. MODULE_DESCRIPTION("MTK Nand ECC Driver");
  458. MODULE_LICENSE("GPL");