mtk_ecc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. ecc->sectors = 0;
  107. complete(&ecc->done);
  108. } else {
  109. return IRQ_HANDLED;
  110. }
  111. } else {
  112. enc = readl(ecc->regs + ECC_ENCIRQ_STA) & ECC_IRQ_EN;
  113. if (enc) {
  114. op = ECC_ENCODE;
  115. complete(&ecc->done);
  116. } else {
  117. return IRQ_NONE;
  118. }
  119. }
  120. writel(0, ecc->regs + ECC_IRQ_REG(op));
  121. return IRQ_HANDLED;
  122. }
  123. static void mtk_ecc_config(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
  124. {
  125. u32 ecc_bit = ECC_CNFG_4BIT, dec_sz, enc_sz;
  126. u32 reg;
  127. switch (config->strength) {
  128. case 4:
  129. ecc_bit = ECC_CNFG_4BIT;
  130. break;
  131. case 6:
  132. ecc_bit = ECC_CNFG_6BIT;
  133. break;
  134. case 8:
  135. ecc_bit = ECC_CNFG_8BIT;
  136. break;
  137. case 10:
  138. ecc_bit = ECC_CNFG_10BIT;
  139. break;
  140. case 12:
  141. ecc_bit = ECC_CNFG_12BIT;
  142. break;
  143. case 14:
  144. ecc_bit = ECC_CNFG_14BIT;
  145. break;
  146. case 16:
  147. ecc_bit = ECC_CNFG_16BIT;
  148. break;
  149. case 18:
  150. ecc_bit = ECC_CNFG_18BIT;
  151. break;
  152. case 20:
  153. ecc_bit = ECC_CNFG_20BIT;
  154. break;
  155. case 22:
  156. ecc_bit = ECC_CNFG_22BIT;
  157. break;
  158. case 24:
  159. ecc_bit = ECC_CNFG_24BIT;
  160. break;
  161. case 28:
  162. ecc_bit = ECC_CNFG_28BIT;
  163. break;
  164. case 32:
  165. ecc_bit = ECC_CNFG_32BIT;
  166. break;
  167. case 36:
  168. ecc_bit = ECC_CNFG_36BIT;
  169. break;
  170. case 40:
  171. ecc_bit = ECC_CNFG_40BIT;
  172. break;
  173. case 44:
  174. ecc_bit = ECC_CNFG_44BIT;
  175. break;
  176. case 48:
  177. ecc_bit = ECC_CNFG_48BIT;
  178. break;
  179. case 52:
  180. ecc_bit = ECC_CNFG_52BIT;
  181. break;
  182. case 56:
  183. ecc_bit = ECC_CNFG_56BIT;
  184. break;
  185. case 60:
  186. ecc_bit = ECC_CNFG_60BIT;
  187. break;
  188. default:
  189. dev_err(ecc->dev, "invalid strength %d, default to 4 bits\n",
  190. config->strength);
  191. }
  192. if (config->op == ECC_ENCODE) {
  193. /* configure ECC encoder (in bits) */
  194. enc_sz = config->len << 3;
  195. reg = ecc_bit | (config->mode << ECC_MODE_SHIFT);
  196. reg |= (enc_sz << ECC_MS_SHIFT);
  197. writel(reg, ecc->regs + ECC_ENCCNFG);
  198. if (config->mode != ECC_NFI_MODE)
  199. writel(lower_32_bits(config->addr),
  200. ecc->regs + ECC_ENCDIADDR);
  201. } else {
  202. /* configure ECC decoder (in bits) */
  203. dec_sz = (config->len << 3) +
  204. config->strength * ECC_PARITY_BITS;
  205. reg = ecc_bit | (config->mode << ECC_MODE_SHIFT);
  206. reg |= (dec_sz << ECC_MS_SHIFT) | DEC_CNFG_CORRECT;
  207. reg |= DEC_EMPTY_EN;
  208. writel(reg, ecc->regs + ECC_DECCNFG);
  209. if (config->sectors)
  210. ecc->sectors = 1 << (config->sectors - 1);
  211. }
  212. }
  213. void mtk_ecc_get_stats(struct mtk_ecc *ecc, struct mtk_ecc_stats *stats,
  214. int sectors)
  215. {
  216. u32 offset, i, err;
  217. u32 bitflips = 0;
  218. stats->corrected = 0;
  219. stats->failed = 0;
  220. for (i = 0; i < sectors; i++) {
  221. offset = (i >> 2) << 2;
  222. err = readl(ecc->regs + ECC_DECENUM0 + offset);
  223. err = err >> ((i % 4) * 8);
  224. err &= ERR_MASK;
  225. if (err == ERR_MASK) {
  226. /* uncorrectable errors */
  227. stats->failed++;
  228. continue;
  229. }
  230. stats->corrected += err;
  231. bitflips = max_t(u32, bitflips, err);
  232. }
  233. stats->bitflips = bitflips;
  234. }
  235. EXPORT_SYMBOL(mtk_ecc_get_stats);
  236. void mtk_ecc_release(struct mtk_ecc *ecc)
  237. {
  238. clk_disable_unprepare(ecc->clk);
  239. put_device(ecc->dev);
  240. }
  241. EXPORT_SYMBOL(mtk_ecc_release);
  242. static void mtk_ecc_hw_init(struct mtk_ecc *ecc)
  243. {
  244. mtk_ecc_wait_idle(ecc, ECC_ENCODE);
  245. writew(ECC_OP_DISABLE, ecc->regs + ECC_ENCCON);
  246. mtk_ecc_wait_idle(ecc, ECC_DECODE);
  247. writel(ECC_OP_DISABLE, ecc->regs + ECC_DECCON);
  248. }
  249. static struct mtk_ecc *mtk_ecc_get(struct device_node *np)
  250. {
  251. struct platform_device *pdev;
  252. struct mtk_ecc *ecc;
  253. pdev = of_find_device_by_node(np);
  254. if (!pdev || !platform_get_drvdata(pdev))
  255. return ERR_PTR(-EPROBE_DEFER);
  256. get_device(&pdev->dev);
  257. ecc = platform_get_drvdata(pdev);
  258. clk_prepare_enable(ecc->clk);
  259. mtk_ecc_hw_init(ecc);
  260. return ecc;
  261. }
  262. struct mtk_ecc *of_mtk_ecc_get(struct device_node *of_node)
  263. {
  264. struct mtk_ecc *ecc = NULL;
  265. struct device_node *np;
  266. np = of_parse_phandle(of_node, "ecc-engine", 0);
  267. if (np) {
  268. ecc = mtk_ecc_get(np);
  269. of_node_put(np);
  270. }
  271. return ecc;
  272. }
  273. EXPORT_SYMBOL(of_mtk_ecc_get);
  274. int mtk_ecc_enable(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
  275. {
  276. enum mtk_ecc_operation op = config->op;
  277. int ret;
  278. ret = mutex_lock_interruptible(&ecc->lock);
  279. if (ret) {
  280. dev_err(ecc->dev, "interrupted when attempting to lock\n");
  281. return ret;
  282. }
  283. mtk_ecc_wait_idle(ecc, op);
  284. mtk_ecc_config(ecc, config);
  285. writew(ECC_OP_ENABLE, ecc->regs + ECC_CTL_REG(op));
  286. init_completion(&ecc->done);
  287. writew(ECC_IRQ_EN, ecc->regs + ECC_IRQ_REG(op));
  288. return 0;
  289. }
  290. EXPORT_SYMBOL(mtk_ecc_enable);
  291. void mtk_ecc_disable(struct mtk_ecc *ecc)
  292. {
  293. enum mtk_ecc_operation op = ECC_ENCODE;
  294. /* find out the running operation */
  295. if (readw(ecc->regs + ECC_CTL_REG(op)) != ECC_OP_ENABLE)
  296. op = ECC_DECODE;
  297. /* disable it */
  298. mtk_ecc_wait_idle(ecc, op);
  299. writew(0, ecc->regs + ECC_IRQ_REG(op));
  300. writew(ECC_OP_DISABLE, ecc->regs + ECC_CTL_REG(op));
  301. mutex_unlock(&ecc->lock);
  302. }
  303. EXPORT_SYMBOL(mtk_ecc_disable);
  304. int mtk_ecc_wait_done(struct mtk_ecc *ecc, enum mtk_ecc_operation op)
  305. {
  306. int ret;
  307. ret = wait_for_completion_timeout(&ecc->done, msecs_to_jiffies(500));
  308. if (!ret) {
  309. dev_err(ecc->dev, "%s timeout - interrupt did not arrive)\n",
  310. (op == ECC_ENCODE) ? "encoder" : "decoder");
  311. return -ETIMEDOUT;
  312. }
  313. return 0;
  314. }
  315. EXPORT_SYMBOL(mtk_ecc_wait_done);
  316. int mtk_ecc_encode(struct mtk_ecc *ecc, struct mtk_ecc_config *config,
  317. u8 *data, u32 bytes)
  318. {
  319. dma_addr_t addr;
  320. u32 len;
  321. int ret;
  322. addr = dma_map_single(ecc->dev, data, bytes, DMA_TO_DEVICE);
  323. ret = dma_mapping_error(ecc->dev, addr);
  324. if (ret) {
  325. dev_err(ecc->dev, "dma mapping error\n");
  326. return -EINVAL;
  327. }
  328. config->op = ECC_ENCODE;
  329. config->addr = addr;
  330. ret = mtk_ecc_enable(ecc, config);
  331. if (ret) {
  332. dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
  333. return ret;
  334. }
  335. ret = mtk_ecc_wait_done(ecc, ECC_ENCODE);
  336. if (ret)
  337. goto timeout;
  338. mtk_ecc_wait_idle(ecc, ECC_ENCODE);
  339. /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */
  340. len = (config->strength * ECC_PARITY_BITS + 7) >> 3;
  341. /* write the parity bytes generated by the ECC back to temp buffer */
  342. __ioread32_copy(ecc->eccdata, ecc->regs + ECC_ENCPAR(0), round_up(len, 4));
  343. /* copy into possibly unaligned OOB region with actual length */
  344. memcpy(data + bytes, ecc->eccdata, len);
  345. timeout:
  346. dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
  347. mtk_ecc_disable(ecc);
  348. return ret;
  349. }
  350. EXPORT_SYMBOL(mtk_ecc_encode);
  351. void mtk_ecc_adjust_strength(u32 *p)
  352. {
  353. u32 ecc[] = {4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
  354. 40, 44, 48, 52, 56, 60};
  355. int i;
  356. for (i = 0; i < ARRAY_SIZE(ecc); i++) {
  357. if (*p <= ecc[i]) {
  358. if (!i)
  359. *p = ecc[i];
  360. else if (*p != ecc[i])
  361. *p = ecc[i - 1];
  362. return;
  363. }
  364. }
  365. *p = ecc[ARRAY_SIZE(ecc) - 1];
  366. }
  367. EXPORT_SYMBOL(mtk_ecc_adjust_strength);
  368. static int mtk_ecc_probe(struct platform_device *pdev)
  369. {
  370. struct device *dev = &pdev->dev;
  371. struct mtk_ecc *ecc;
  372. struct resource *res;
  373. int irq, ret;
  374. ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
  375. if (!ecc)
  376. return -ENOMEM;
  377. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  378. ecc->regs = devm_ioremap_resource(dev, res);
  379. if (IS_ERR(ecc->regs)) {
  380. dev_err(dev, "failed to map regs: %ld\n", PTR_ERR(ecc->regs));
  381. return PTR_ERR(ecc->regs);
  382. }
  383. ecc->clk = devm_clk_get(dev, NULL);
  384. if (IS_ERR(ecc->clk)) {
  385. dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk));
  386. return PTR_ERR(ecc->clk);
  387. }
  388. irq = platform_get_irq(pdev, 0);
  389. if (irq < 0) {
  390. dev_err(dev, "failed to get irq\n");
  391. return -EINVAL;
  392. }
  393. ret = dma_set_mask(dev, DMA_BIT_MASK(32));
  394. if (ret) {
  395. dev_err(dev, "failed to set DMA mask\n");
  396. return ret;
  397. }
  398. ret = devm_request_irq(dev, irq, mtk_ecc_irq, 0x0, "mtk-ecc", ecc);
  399. if (ret) {
  400. dev_err(dev, "failed to request irq\n");
  401. return -EINVAL;
  402. }
  403. ecc->dev = dev;
  404. mutex_init(&ecc->lock);
  405. platform_set_drvdata(pdev, ecc);
  406. dev_info(dev, "probed\n");
  407. return 0;
  408. }
  409. #ifdef CONFIG_PM_SLEEP
  410. static int mtk_ecc_suspend(struct device *dev)
  411. {
  412. struct mtk_ecc *ecc = dev_get_drvdata(dev);
  413. clk_disable_unprepare(ecc->clk);
  414. return 0;
  415. }
  416. static int mtk_ecc_resume(struct device *dev)
  417. {
  418. struct mtk_ecc *ecc = dev_get_drvdata(dev);
  419. int ret;
  420. ret = clk_prepare_enable(ecc->clk);
  421. if (ret) {
  422. dev_err(dev, "failed to enable clk\n");
  423. return ret;
  424. }
  425. mtk_ecc_hw_init(ecc);
  426. return 0;
  427. }
  428. static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops, mtk_ecc_suspend, mtk_ecc_resume);
  429. #endif
  430. static const struct of_device_id mtk_ecc_dt_match[] = {
  431. { .compatible = "mediatek,mt2701-ecc" },
  432. {},
  433. };
  434. MODULE_DEVICE_TABLE(of, mtk_ecc_dt_match);
  435. static struct platform_driver mtk_ecc_driver = {
  436. .probe = mtk_ecc_probe,
  437. .driver = {
  438. .name = "mtk-ecc",
  439. .of_match_table = of_match_ptr(mtk_ecc_dt_match),
  440. #ifdef CONFIG_PM_SLEEP
  441. .pm = &mtk_ecc_pm_ops,
  442. #endif
  443. },
  444. };
  445. module_platform_driver(mtk_ecc_driver);
  446. MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
  447. MODULE_DESCRIPTION("MTK Nand ECC Driver");
  448. MODULE_LICENSE("GPL");