mtk_ecc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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_PG_IRQ_SEL BIT(1)
  29. #define ECC_OP_ENABLE (1)
  30. #define ECC_OP_DISABLE (0)
  31. #define ECC_ENCCON (0x00)
  32. #define ECC_ENCCNFG (0x04)
  33. #define ECC_MS_SHIFT (16)
  34. #define ECC_ENCDIADDR (0x08)
  35. #define ECC_ENCIDLE (0x0C)
  36. #define ECC_DECCON (0x100)
  37. #define ECC_DECCNFG (0x104)
  38. #define DEC_EMPTY_EN BIT(31)
  39. #define DEC_CNFG_CORRECT (0x3 << 12)
  40. #define ECC_DECIDLE (0x10C)
  41. #define ECC_DECENUM0 (0x114)
  42. #define ECC_TIMEOUT (500000)
  43. #define ECC_IDLE_REG(op) ((op) == ECC_ENCODE ? ECC_ENCIDLE : ECC_DECIDLE)
  44. #define ECC_CTL_REG(op) ((op) == ECC_ENCODE ? ECC_ENCCON : ECC_DECCON)
  45. struct mtk_ecc_caps {
  46. u32 err_mask;
  47. const u8 *ecc_strength;
  48. const u32 *ecc_regs;
  49. u8 num_ecc_strength;
  50. u8 ecc_mode_shift;
  51. u32 parity_bits;
  52. int pg_irq_sel;
  53. };
  54. struct mtk_ecc {
  55. struct device *dev;
  56. const struct mtk_ecc_caps *caps;
  57. void __iomem *regs;
  58. struct clk *clk;
  59. struct completion done;
  60. struct mutex lock;
  61. u32 sectors;
  62. u8 *eccdata;
  63. };
  64. /* ecc strength that each IP supports */
  65. static const u8 ecc_strength_mt2701[] = {
  66. 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
  67. 40, 44, 48, 52, 56, 60
  68. };
  69. static const u8 ecc_strength_mt2712[] = {
  70. 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36,
  71. 40, 44, 48, 52, 56, 60, 68, 72, 80
  72. };
  73. static const u8 ecc_strength_mt7622[] = {
  74. 4, 6, 8, 10, 12, 14, 16
  75. };
  76. enum mtk_ecc_regs {
  77. ECC_ENCPAR00,
  78. ECC_ENCIRQ_EN,
  79. ECC_ENCIRQ_STA,
  80. ECC_DECDONE,
  81. ECC_DECIRQ_EN,
  82. ECC_DECIRQ_STA,
  83. };
  84. static int mt2701_ecc_regs[] = {
  85. [ECC_ENCPAR00] = 0x10,
  86. [ECC_ENCIRQ_EN] = 0x80,
  87. [ECC_ENCIRQ_STA] = 0x84,
  88. [ECC_DECDONE] = 0x124,
  89. [ECC_DECIRQ_EN] = 0x200,
  90. [ECC_DECIRQ_STA] = 0x204,
  91. };
  92. static int mt2712_ecc_regs[] = {
  93. [ECC_ENCPAR00] = 0x300,
  94. [ECC_ENCIRQ_EN] = 0x80,
  95. [ECC_ENCIRQ_STA] = 0x84,
  96. [ECC_DECDONE] = 0x124,
  97. [ECC_DECIRQ_EN] = 0x200,
  98. [ECC_DECIRQ_STA] = 0x204,
  99. };
  100. static int mt7622_ecc_regs[] = {
  101. [ECC_ENCPAR00] = 0x10,
  102. [ECC_ENCIRQ_EN] = 0x30,
  103. [ECC_ENCIRQ_STA] = 0x34,
  104. [ECC_DECDONE] = 0x11c,
  105. [ECC_DECIRQ_EN] = 0x140,
  106. [ECC_DECIRQ_STA] = 0x144,
  107. };
  108. static inline void mtk_ecc_wait_idle(struct mtk_ecc *ecc,
  109. enum mtk_ecc_operation op)
  110. {
  111. struct device *dev = ecc->dev;
  112. u32 val;
  113. int ret;
  114. ret = readl_poll_timeout_atomic(ecc->regs + ECC_IDLE_REG(op), val,
  115. val & ECC_IDLE_MASK,
  116. 10, ECC_TIMEOUT);
  117. if (ret)
  118. dev_warn(dev, "%s NOT idle\n",
  119. op == ECC_ENCODE ? "encoder" : "decoder");
  120. }
  121. static irqreturn_t mtk_ecc_irq(int irq, void *id)
  122. {
  123. struct mtk_ecc *ecc = id;
  124. u32 dec, enc;
  125. dec = readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_STA])
  126. & ECC_IRQ_EN;
  127. if (dec) {
  128. dec = readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECDONE]);
  129. if (dec & ecc->sectors) {
  130. /*
  131. * Clear decode IRQ status once again to ensure that
  132. * there will be no extra IRQ.
  133. */
  134. readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_STA]);
  135. ecc->sectors = 0;
  136. complete(&ecc->done);
  137. } else {
  138. return IRQ_HANDLED;
  139. }
  140. } else {
  141. enc = readl(ecc->regs + ecc->caps->ecc_regs[ECC_ENCIRQ_STA])
  142. & ECC_IRQ_EN;
  143. if (enc)
  144. complete(&ecc->done);
  145. else
  146. return IRQ_NONE;
  147. }
  148. return IRQ_HANDLED;
  149. }
  150. static int mtk_ecc_config(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
  151. {
  152. u32 ecc_bit, dec_sz, enc_sz;
  153. u32 reg, i;
  154. for (i = 0; i < ecc->caps->num_ecc_strength; i++) {
  155. if (ecc->caps->ecc_strength[i] == config->strength)
  156. break;
  157. }
  158. if (i == ecc->caps->num_ecc_strength) {
  159. dev_err(ecc->dev, "invalid ecc strength %d\n",
  160. config->strength);
  161. return -EINVAL;
  162. }
  163. ecc_bit = i;
  164. if (config->op == ECC_ENCODE) {
  165. /* configure ECC encoder (in bits) */
  166. enc_sz = config->len << 3;
  167. reg = ecc_bit | (config->mode << ecc->caps->ecc_mode_shift);
  168. reg |= (enc_sz << ECC_MS_SHIFT);
  169. writel(reg, ecc->regs + ECC_ENCCNFG);
  170. if (config->mode != ECC_NFI_MODE)
  171. writel(lower_32_bits(config->addr),
  172. ecc->regs + ECC_ENCDIADDR);
  173. } else {
  174. /* configure ECC decoder (in bits) */
  175. dec_sz = (config->len << 3) +
  176. config->strength * ecc->caps->parity_bits;
  177. reg = ecc_bit | (config->mode << ecc->caps->ecc_mode_shift);
  178. reg |= (dec_sz << ECC_MS_SHIFT) | DEC_CNFG_CORRECT;
  179. reg |= DEC_EMPTY_EN;
  180. writel(reg, ecc->regs + ECC_DECCNFG);
  181. if (config->sectors)
  182. ecc->sectors = 1 << (config->sectors - 1);
  183. }
  184. return 0;
  185. }
  186. void mtk_ecc_get_stats(struct mtk_ecc *ecc, struct mtk_ecc_stats *stats,
  187. int sectors)
  188. {
  189. u32 offset, i, err;
  190. u32 bitflips = 0;
  191. stats->corrected = 0;
  192. stats->failed = 0;
  193. for (i = 0; i < sectors; i++) {
  194. offset = (i >> 2) << 2;
  195. err = readl(ecc->regs + ECC_DECENUM0 + offset);
  196. err = err >> ((i % 4) * 8);
  197. err &= ecc->caps->err_mask;
  198. if (err == ecc->caps->err_mask) {
  199. /* uncorrectable errors */
  200. stats->failed++;
  201. continue;
  202. }
  203. stats->corrected += err;
  204. bitflips = max_t(u32, bitflips, err);
  205. }
  206. stats->bitflips = bitflips;
  207. }
  208. EXPORT_SYMBOL(mtk_ecc_get_stats);
  209. void mtk_ecc_release(struct mtk_ecc *ecc)
  210. {
  211. clk_disable_unprepare(ecc->clk);
  212. put_device(ecc->dev);
  213. }
  214. EXPORT_SYMBOL(mtk_ecc_release);
  215. static void mtk_ecc_hw_init(struct mtk_ecc *ecc)
  216. {
  217. mtk_ecc_wait_idle(ecc, ECC_ENCODE);
  218. writew(ECC_OP_DISABLE, ecc->regs + ECC_ENCCON);
  219. mtk_ecc_wait_idle(ecc, ECC_DECODE);
  220. writel(ECC_OP_DISABLE, ecc->regs + ECC_DECCON);
  221. }
  222. static struct mtk_ecc *mtk_ecc_get(struct device_node *np)
  223. {
  224. struct platform_device *pdev;
  225. struct mtk_ecc *ecc;
  226. pdev = of_find_device_by_node(np);
  227. if (!pdev || !platform_get_drvdata(pdev))
  228. return ERR_PTR(-EPROBE_DEFER);
  229. get_device(&pdev->dev);
  230. ecc = platform_get_drvdata(pdev);
  231. clk_prepare_enable(ecc->clk);
  232. mtk_ecc_hw_init(ecc);
  233. return ecc;
  234. }
  235. struct mtk_ecc *of_mtk_ecc_get(struct device_node *of_node)
  236. {
  237. struct mtk_ecc *ecc = NULL;
  238. struct device_node *np;
  239. np = of_parse_phandle(of_node, "ecc-engine", 0);
  240. if (np) {
  241. ecc = mtk_ecc_get(np);
  242. of_node_put(np);
  243. }
  244. return ecc;
  245. }
  246. EXPORT_SYMBOL(of_mtk_ecc_get);
  247. int mtk_ecc_enable(struct mtk_ecc *ecc, struct mtk_ecc_config *config)
  248. {
  249. enum mtk_ecc_operation op = config->op;
  250. u16 reg_val;
  251. int ret;
  252. ret = mutex_lock_interruptible(&ecc->lock);
  253. if (ret) {
  254. dev_err(ecc->dev, "interrupted when attempting to lock\n");
  255. return ret;
  256. }
  257. mtk_ecc_wait_idle(ecc, op);
  258. ret = mtk_ecc_config(ecc, config);
  259. if (ret) {
  260. mutex_unlock(&ecc->lock);
  261. return ret;
  262. }
  263. if (config->mode != ECC_NFI_MODE || op != ECC_ENCODE) {
  264. init_completion(&ecc->done);
  265. reg_val = ECC_IRQ_EN;
  266. /*
  267. * For ECC_NFI_MODE, if ecc->caps->pg_irq_sel is 1, then it
  268. * means this chip can only generate one ecc irq during page
  269. * read / write. If is 0, generate one ecc irq each ecc step.
  270. */
  271. if (ecc->caps->pg_irq_sel && config->mode == ECC_NFI_MODE)
  272. reg_val |= ECC_PG_IRQ_SEL;
  273. if (op == ECC_ENCODE)
  274. writew(reg_val, ecc->regs +
  275. ecc->caps->ecc_regs[ECC_ENCIRQ_EN]);
  276. else
  277. writew(reg_val, ecc->regs +
  278. ecc->caps->ecc_regs[ECC_DECIRQ_EN]);
  279. }
  280. writew(ECC_OP_ENABLE, ecc->regs + ECC_CTL_REG(op));
  281. return 0;
  282. }
  283. EXPORT_SYMBOL(mtk_ecc_enable);
  284. void mtk_ecc_disable(struct mtk_ecc *ecc)
  285. {
  286. enum mtk_ecc_operation op = ECC_ENCODE;
  287. /* find out the running operation */
  288. if (readw(ecc->regs + ECC_CTL_REG(op)) != ECC_OP_ENABLE)
  289. op = ECC_DECODE;
  290. /* disable it */
  291. mtk_ecc_wait_idle(ecc, op);
  292. if (op == ECC_DECODE) {
  293. /*
  294. * Clear decode IRQ status in case there is a timeout to wait
  295. * decode IRQ.
  296. */
  297. readw(ecc->regs + ecc->caps->ecc_regs[ECC_DECDONE]);
  298. writew(0, ecc->regs + ecc->caps->ecc_regs[ECC_DECIRQ_EN]);
  299. } else {
  300. writew(0, ecc->regs + ecc->caps->ecc_regs[ECC_ENCIRQ_EN]);
  301. }
  302. writew(ECC_OP_DISABLE, ecc->regs + ECC_CTL_REG(op));
  303. mutex_unlock(&ecc->lock);
  304. }
  305. EXPORT_SYMBOL(mtk_ecc_disable);
  306. int mtk_ecc_wait_done(struct mtk_ecc *ecc, enum mtk_ecc_operation op)
  307. {
  308. int ret;
  309. ret = wait_for_completion_timeout(&ecc->done, msecs_to_jiffies(500));
  310. if (!ret) {
  311. dev_err(ecc->dev, "%s timeout - interrupt did not arrive)\n",
  312. (op == ECC_ENCODE) ? "encoder" : "decoder");
  313. return -ETIMEDOUT;
  314. }
  315. return 0;
  316. }
  317. EXPORT_SYMBOL(mtk_ecc_wait_done);
  318. int mtk_ecc_encode(struct mtk_ecc *ecc, struct mtk_ecc_config *config,
  319. u8 *data, u32 bytes)
  320. {
  321. dma_addr_t addr;
  322. u32 len;
  323. int ret;
  324. addr = dma_map_single(ecc->dev, data, bytes, DMA_TO_DEVICE);
  325. ret = dma_mapping_error(ecc->dev, addr);
  326. if (ret) {
  327. dev_err(ecc->dev, "dma mapping error\n");
  328. return -EINVAL;
  329. }
  330. config->op = ECC_ENCODE;
  331. config->addr = addr;
  332. ret = mtk_ecc_enable(ecc, config);
  333. if (ret) {
  334. dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
  335. return ret;
  336. }
  337. ret = mtk_ecc_wait_done(ecc, ECC_ENCODE);
  338. if (ret)
  339. goto timeout;
  340. mtk_ecc_wait_idle(ecc, ECC_ENCODE);
  341. /* Program ECC bytes to OOB: per sector oob = FDM + ECC + SPARE */
  342. len = (config->strength * ecc->caps->parity_bits + 7) >> 3;
  343. /* write the parity bytes generated by the ECC back to temp buffer */
  344. __ioread32_copy(ecc->eccdata,
  345. ecc->regs + ecc->caps->ecc_regs[ECC_ENCPAR00],
  346. round_up(len, 4));
  347. /* copy into possibly unaligned OOB region with actual length */
  348. memcpy(data + bytes, ecc->eccdata, len);
  349. timeout:
  350. dma_unmap_single(ecc->dev, addr, bytes, DMA_TO_DEVICE);
  351. mtk_ecc_disable(ecc);
  352. return ret;
  353. }
  354. EXPORT_SYMBOL(mtk_ecc_encode);
  355. void mtk_ecc_adjust_strength(struct mtk_ecc *ecc, u32 *p)
  356. {
  357. const u8 *ecc_strength = ecc->caps->ecc_strength;
  358. int i;
  359. for (i = 0; i < ecc->caps->num_ecc_strength; i++) {
  360. if (*p <= ecc_strength[i]) {
  361. if (!i)
  362. *p = ecc_strength[i];
  363. else if (*p != ecc_strength[i])
  364. *p = ecc_strength[i - 1];
  365. return;
  366. }
  367. }
  368. *p = ecc_strength[ecc->caps->num_ecc_strength - 1];
  369. }
  370. EXPORT_SYMBOL(mtk_ecc_adjust_strength);
  371. unsigned int mtk_ecc_get_parity_bits(struct mtk_ecc *ecc)
  372. {
  373. return ecc->caps->parity_bits;
  374. }
  375. EXPORT_SYMBOL(mtk_ecc_get_parity_bits);
  376. static const struct mtk_ecc_caps mtk_ecc_caps_mt2701 = {
  377. .err_mask = 0x3f,
  378. .ecc_strength = ecc_strength_mt2701,
  379. .ecc_regs = mt2701_ecc_regs,
  380. .num_ecc_strength = 20,
  381. .ecc_mode_shift = 5,
  382. .parity_bits = 14,
  383. .pg_irq_sel = 0,
  384. };
  385. static const struct mtk_ecc_caps mtk_ecc_caps_mt2712 = {
  386. .err_mask = 0x7f,
  387. .ecc_strength = ecc_strength_mt2712,
  388. .ecc_regs = mt2712_ecc_regs,
  389. .num_ecc_strength = 23,
  390. .ecc_mode_shift = 5,
  391. .parity_bits = 14,
  392. .pg_irq_sel = 1,
  393. };
  394. static const struct mtk_ecc_caps mtk_ecc_caps_mt7622 = {
  395. .err_mask = 0x3f,
  396. .ecc_strength = ecc_strength_mt7622,
  397. .ecc_regs = mt7622_ecc_regs,
  398. .num_ecc_strength = 7,
  399. .ecc_mode_shift = 4,
  400. .parity_bits = 13,
  401. .pg_irq_sel = 0,
  402. };
  403. static const struct of_device_id mtk_ecc_dt_match[] = {
  404. {
  405. .compatible = "mediatek,mt2701-ecc",
  406. .data = &mtk_ecc_caps_mt2701,
  407. }, {
  408. .compatible = "mediatek,mt2712-ecc",
  409. .data = &mtk_ecc_caps_mt2712,
  410. }, {
  411. .compatible = "mediatek,mt7622-ecc",
  412. .data = &mtk_ecc_caps_mt7622,
  413. },
  414. {},
  415. };
  416. static int mtk_ecc_probe(struct platform_device *pdev)
  417. {
  418. struct device *dev = &pdev->dev;
  419. struct mtk_ecc *ecc;
  420. struct resource *res;
  421. u32 max_eccdata_size;
  422. int irq, ret;
  423. ecc = devm_kzalloc(dev, sizeof(*ecc), GFP_KERNEL);
  424. if (!ecc)
  425. return -ENOMEM;
  426. ecc->caps = of_device_get_match_data(dev);
  427. max_eccdata_size = ecc->caps->num_ecc_strength - 1;
  428. max_eccdata_size = ecc->caps->ecc_strength[max_eccdata_size];
  429. max_eccdata_size = (max_eccdata_size * ecc->caps->parity_bits + 7) >> 3;
  430. max_eccdata_size = round_up(max_eccdata_size, 4);
  431. ecc->eccdata = devm_kzalloc(dev, max_eccdata_size, GFP_KERNEL);
  432. if (!ecc->eccdata)
  433. return -ENOMEM;
  434. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  435. ecc->regs = devm_ioremap_resource(dev, res);
  436. if (IS_ERR(ecc->regs)) {
  437. dev_err(dev, "failed to map regs: %ld\n", PTR_ERR(ecc->regs));
  438. return PTR_ERR(ecc->regs);
  439. }
  440. ecc->clk = devm_clk_get(dev, NULL);
  441. if (IS_ERR(ecc->clk)) {
  442. dev_err(dev, "failed to get clock: %ld\n", PTR_ERR(ecc->clk));
  443. return PTR_ERR(ecc->clk);
  444. }
  445. irq = platform_get_irq(pdev, 0);
  446. if (irq < 0) {
  447. dev_err(dev, "failed to get irq: %d\n", irq);
  448. return irq;
  449. }
  450. ret = dma_set_mask(dev, DMA_BIT_MASK(32));
  451. if (ret) {
  452. dev_err(dev, "failed to set DMA mask\n");
  453. return ret;
  454. }
  455. ret = devm_request_irq(dev, irq, mtk_ecc_irq, 0x0, "mtk-ecc", ecc);
  456. if (ret) {
  457. dev_err(dev, "failed to request irq\n");
  458. return -EINVAL;
  459. }
  460. ecc->dev = dev;
  461. mutex_init(&ecc->lock);
  462. platform_set_drvdata(pdev, ecc);
  463. dev_info(dev, "probed\n");
  464. return 0;
  465. }
  466. #ifdef CONFIG_PM_SLEEP
  467. static int mtk_ecc_suspend(struct device *dev)
  468. {
  469. struct mtk_ecc *ecc = dev_get_drvdata(dev);
  470. clk_disable_unprepare(ecc->clk);
  471. return 0;
  472. }
  473. static int mtk_ecc_resume(struct device *dev)
  474. {
  475. struct mtk_ecc *ecc = dev_get_drvdata(dev);
  476. int ret;
  477. ret = clk_prepare_enable(ecc->clk);
  478. if (ret) {
  479. dev_err(dev, "failed to enable clk\n");
  480. return ret;
  481. }
  482. return 0;
  483. }
  484. static SIMPLE_DEV_PM_OPS(mtk_ecc_pm_ops, mtk_ecc_suspend, mtk_ecc_resume);
  485. #endif
  486. MODULE_DEVICE_TABLE(of, mtk_ecc_dt_match);
  487. static struct platform_driver mtk_ecc_driver = {
  488. .probe = mtk_ecc_probe,
  489. .driver = {
  490. .name = "mtk-ecc",
  491. .of_match_table = of_match_ptr(mtk_ecc_dt_match),
  492. #ifdef CONFIG_PM_SLEEP
  493. .pm = &mtk_ecc_pm_ops,
  494. #endif
  495. },
  496. };
  497. module_platform_driver(mtk_ecc_driver);
  498. MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
  499. MODULE_DESCRIPTION("MTK Nand ECC Driver");
  500. MODULE_LICENSE("GPL");