elm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /*
  2. * Error Location Module
  3. *
  4. * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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. */
  17. #include <linux/platform_device.h>
  18. #include <linux/module.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/of.h>
  22. #include <linux/sched.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/platform_data/elm.h>
  25. #define ELM_SYSCONFIG 0x010
  26. #define ELM_IRQSTATUS 0x018
  27. #define ELM_IRQENABLE 0x01c
  28. #define ELM_LOCATION_CONFIG 0x020
  29. #define ELM_PAGE_CTRL 0x080
  30. #define ELM_SYNDROME_FRAGMENT_0 0x400
  31. #define ELM_SYNDROME_FRAGMENT_1 0x404
  32. #define ELM_SYNDROME_FRAGMENT_2 0x408
  33. #define ELM_SYNDROME_FRAGMENT_3 0x40c
  34. #define ELM_SYNDROME_FRAGMENT_4 0x410
  35. #define ELM_SYNDROME_FRAGMENT_5 0x414
  36. #define ELM_SYNDROME_FRAGMENT_6 0x418
  37. #define ELM_LOCATION_STATUS 0x800
  38. #define ELM_ERROR_LOCATION_0 0x880
  39. /* ELM Interrupt Status Register */
  40. #define INTR_STATUS_PAGE_VALID BIT(8)
  41. /* ELM Interrupt Enable Register */
  42. #define INTR_EN_PAGE_MASK BIT(8)
  43. /* ELM Location Configuration Register */
  44. #define ECC_BCH_LEVEL_MASK 0x3
  45. /* ELM syndrome */
  46. #define ELM_SYNDROME_VALID BIT(16)
  47. /* ELM_LOCATION_STATUS Register */
  48. #define ECC_CORRECTABLE_MASK BIT(8)
  49. #define ECC_NB_ERRORS_MASK 0x1f
  50. /* ELM_ERROR_LOCATION_0-15 Registers */
  51. #define ECC_ERROR_LOCATION_MASK 0x1fff
  52. #define ELM_ECC_SIZE 0x7ff
  53. #define SYNDROME_FRAGMENT_REG_SIZE 0x40
  54. #define ERROR_LOCATION_SIZE 0x100
  55. struct elm_registers {
  56. u32 elm_irqenable;
  57. u32 elm_sysconfig;
  58. u32 elm_location_config;
  59. u32 elm_page_ctrl;
  60. u32 elm_syndrome_fragment_6[ERROR_VECTOR_MAX];
  61. u32 elm_syndrome_fragment_5[ERROR_VECTOR_MAX];
  62. u32 elm_syndrome_fragment_4[ERROR_VECTOR_MAX];
  63. u32 elm_syndrome_fragment_3[ERROR_VECTOR_MAX];
  64. u32 elm_syndrome_fragment_2[ERROR_VECTOR_MAX];
  65. u32 elm_syndrome_fragment_1[ERROR_VECTOR_MAX];
  66. u32 elm_syndrome_fragment_0[ERROR_VECTOR_MAX];
  67. };
  68. struct elm_info {
  69. struct device *dev;
  70. void __iomem *elm_base;
  71. struct completion elm_completion;
  72. struct list_head list;
  73. enum bch_ecc bch_type;
  74. struct elm_registers elm_regs;
  75. };
  76. static LIST_HEAD(elm_devices);
  77. static void elm_write_reg(struct elm_info *info, int offset, u32 val)
  78. {
  79. writel(val, info->elm_base + offset);
  80. }
  81. static u32 elm_read_reg(struct elm_info *info, int offset)
  82. {
  83. return readl(info->elm_base + offset);
  84. }
  85. /**
  86. * elm_config - Configure ELM module
  87. * @dev: ELM device
  88. * @bch_type: Type of BCH ecc
  89. */
  90. int elm_config(struct device *dev, enum bch_ecc bch_type)
  91. {
  92. u32 reg_val;
  93. struct elm_info *info = dev_get_drvdata(dev);
  94. if (!info) {
  95. dev_err(dev, "Unable to configure elm - device not probed?\n");
  96. return -ENODEV;
  97. }
  98. reg_val = (bch_type & ECC_BCH_LEVEL_MASK) | (ELM_ECC_SIZE << 16);
  99. elm_write_reg(info, ELM_LOCATION_CONFIG, reg_val);
  100. info->bch_type = bch_type;
  101. return 0;
  102. }
  103. EXPORT_SYMBOL(elm_config);
  104. /**
  105. * elm_configure_page_mode - Enable/Disable page mode
  106. * @info: elm info
  107. * @index: index number of syndrome fragment vector
  108. * @enable: enable/disable flag for page mode
  109. *
  110. * Enable page mode for syndrome fragment index
  111. */
  112. static void elm_configure_page_mode(struct elm_info *info, int index,
  113. bool enable)
  114. {
  115. u32 reg_val;
  116. reg_val = elm_read_reg(info, ELM_PAGE_CTRL);
  117. if (enable)
  118. reg_val |= BIT(index); /* enable page mode */
  119. else
  120. reg_val &= ~BIT(index); /* disable page mode */
  121. elm_write_reg(info, ELM_PAGE_CTRL, reg_val);
  122. }
  123. /**
  124. * elm_load_syndrome - Load ELM syndrome reg
  125. * @info: elm info
  126. * @err_vec: elm error vectors
  127. * @ecc: buffer with calculated ecc
  128. *
  129. * Load syndrome fragment registers with calculated ecc in reverse order.
  130. */
  131. static void elm_load_syndrome(struct elm_info *info,
  132. struct elm_errorvec *err_vec, u8 *ecc)
  133. {
  134. int i, offset;
  135. u32 val;
  136. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  137. /* Check error reported */
  138. if (err_vec[i].error_reported) {
  139. elm_configure_page_mode(info, i, true);
  140. offset = ELM_SYNDROME_FRAGMENT_0 +
  141. SYNDROME_FRAGMENT_REG_SIZE * i;
  142. /* BCH8 */
  143. if (info->bch_type) {
  144. /* syndrome fragment 0 = ecc[9-12B] */
  145. val = cpu_to_be32(*(u32 *) &ecc[9]);
  146. elm_write_reg(info, offset, val);
  147. /* syndrome fragment 1 = ecc[5-8B] */
  148. offset += 4;
  149. val = cpu_to_be32(*(u32 *) &ecc[5]);
  150. elm_write_reg(info, offset, val);
  151. /* syndrome fragment 2 = ecc[1-4B] */
  152. offset += 4;
  153. val = cpu_to_be32(*(u32 *) &ecc[1]);
  154. elm_write_reg(info, offset, val);
  155. /* syndrome fragment 3 = ecc[0B] */
  156. offset += 4;
  157. val = ecc[0];
  158. elm_write_reg(info, offset, val);
  159. } else {
  160. /* syndrome fragment 0 = ecc[20-52b] bits */
  161. val = (cpu_to_be32(*(u32 *) &ecc[3]) >> 4) |
  162. ((ecc[2] & 0xf) << 28);
  163. elm_write_reg(info, offset, val);
  164. /* syndrome fragment 1 = ecc[0-20b] bits */
  165. offset += 4;
  166. val = cpu_to_be32(*(u32 *) &ecc[0]) >> 12;
  167. elm_write_reg(info, offset, val);
  168. }
  169. }
  170. /* Update ecc pointer with ecc byte size */
  171. ecc += info->bch_type ? BCH8_SIZE : BCH4_SIZE;
  172. }
  173. }
  174. /**
  175. * elm_start_processing - start elm syndrome processing
  176. * @info: elm info
  177. * @err_vec: elm error vectors
  178. *
  179. * Set syndrome valid bit for syndrome fragment registers for which
  180. * elm syndrome fragment registers are loaded. This enables elm module
  181. * to start processing syndrome vectors.
  182. */
  183. static void elm_start_processing(struct elm_info *info,
  184. struct elm_errorvec *err_vec)
  185. {
  186. int i, offset;
  187. u32 reg_val;
  188. /*
  189. * Set syndrome vector valid, so that ELM module
  190. * will process it for vectors error is reported
  191. */
  192. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  193. if (err_vec[i].error_reported) {
  194. offset = ELM_SYNDROME_FRAGMENT_6 +
  195. SYNDROME_FRAGMENT_REG_SIZE * i;
  196. reg_val = elm_read_reg(info, offset);
  197. reg_val |= ELM_SYNDROME_VALID;
  198. elm_write_reg(info, offset, reg_val);
  199. }
  200. }
  201. }
  202. /**
  203. * elm_error_correction - locate correctable error position
  204. * @info: elm info
  205. * @err_vec: elm error vectors
  206. *
  207. * On completion of processing by elm module, error location status
  208. * register updated with correctable/uncorrectable error information.
  209. * In case of correctable errors, number of errors located from
  210. * elm location status register & read the positions from
  211. * elm error location register.
  212. */
  213. static void elm_error_correction(struct elm_info *info,
  214. struct elm_errorvec *err_vec)
  215. {
  216. int i, j, errors = 0;
  217. int offset;
  218. u32 reg_val;
  219. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  220. /* Check error reported */
  221. if (err_vec[i].error_reported) {
  222. offset = ELM_LOCATION_STATUS + ERROR_LOCATION_SIZE * i;
  223. reg_val = elm_read_reg(info, offset);
  224. /* Check correctable error or not */
  225. if (reg_val & ECC_CORRECTABLE_MASK) {
  226. offset = ELM_ERROR_LOCATION_0 +
  227. ERROR_LOCATION_SIZE * i;
  228. /* Read count of correctable errors */
  229. err_vec[i].error_count = reg_val &
  230. ECC_NB_ERRORS_MASK;
  231. /* Update the error locations in error vector */
  232. for (j = 0; j < err_vec[i].error_count; j++) {
  233. reg_val = elm_read_reg(info, offset);
  234. err_vec[i].error_loc[j] = reg_val &
  235. ECC_ERROR_LOCATION_MASK;
  236. /* Update error location register */
  237. offset += 4;
  238. }
  239. errors += err_vec[i].error_count;
  240. } else {
  241. err_vec[i].error_uncorrectable = true;
  242. }
  243. /* Clearing interrupts for processed error vectors */
  244. elm_write_reg(info, ELM_IRQSTATUS, BIT(i));
  245. /* Disable page mode */
  246. elm_configure_page_mode(info, i, false);
  247. }
  248. }
  249. }
  250. /**
  251. * elm_decode_bch_error_page - Locate error position
  252. * @dev: device pointer
  253. * @ecc_calc: calculated ECC bytes from GPMC
  254. * @err_vec: elm error vectors
  255. *
  256. * Called with one or more error reported vectors & vectors with
  257. * error reported is updated in err_vec[].error_reported
  258. */
  259. void elm_decode_bch_error_page(struct device *dev, u8 *ecc_calc,
  260. struct elm_errorvec *err_vec)
  261. {
  262. struct elm_info *info = dev_get_drvdata(dev);
  263. u32 reg_val;
  264. /* Enable page mode interrupt */
  265. reg_val = elm_read_reg(info, ELM_IRQSTATUS);
  266. elm_write_reg(info, ELM_IRQSTATUS, reg_val & INTR_STATUS_PAGE_VALID);
  267. elm_write_reg(info, ELM_IRQENABLE, INTR_EN_PAGE_MASK);
  268. /* Load valid ecc byte to syndrome fragment register */
  269. elm_load_syndrome(info, err_vec, ecc_calc);
  270. /* Enable syndrome processing for which syndrome fragment is updated */
  271. elm_start_processing(info, err_vec);
  272. /* Wait for ELM module to finish locating error correction */
  273. wait_for_completion(&info->elm_completion);
  274. /* Disable page mode interrupt */
  275. reg_val = elm_read_reg(info, ELM_IRQENABLE);
  276. elm_write_reg(info, ELM_IRQENABLE, reg_val & ~INTR_EN_PAGE_MASK);
  277. elm_error_correction(info, err_vec);
  278. }
  279. EXPORT_SYMBOL(elm_decode_bch_error_page);
  280. static irqreturn_t elm_isr(int this_irq, void *dev_id)
  281. {
  282. u32 reg_val;
  283. struct elm_info *info = dev_id;
  284. reg_val = elm_read_reg(info, ELM_IRQSTATUS);
  285. /* All error vectors processed */
  286. if (reg_val & INTR_STATUS_PAGE_VALID) {
  287. elm_write_reg(info, ELM_IRQSTATUS,
  288. reg_val & INTR_STATUS_PAGE_VALID);
  289. complete(&info->elm_completion);
  290. return IRQ_HANDLED;
  291. }
  292. return IRQ_NONE;
  293. }
  294. static int elm_probe(struct platform_device *pdev)
  295. {
  296. int ret = 0;
  297. struct resource *res, *irq;
  298. struct elm_info *info;
  299. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  300. if (!info) {
  301. dev_err(&pdev->dev, "failed to allocate memory\n");
  302. return -ENOMEM;
  303. }
  304. info->dev = &pdev->dev;
  305. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  306. if (!irq) {
  307. dev_err(&pdev->dev, "no irq resource defined\n");
  308. return -ENODEV;
  309. }
  310. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  311. info->elm_base = devm_ioremap_resource(&pdev->dev, res);
  312. if (IS_ERR(info->elm_base))
  313. return PTR_ERR(info->elm_base);
  314. ret = devm_request_irq(&pdev->dev, irq->start, elm_isr, 0,
  315. pdev->name, info);
  316. if (ret) {
  317. dev_err(&pdev->dev, "failure requesting irq %i\n", irq->start);
  318. return ret;
  319. }
  320. pm_runtime_enable(&pdev->dev);
  321. if (pm_runtime_get_sync(&pdev->dev)) {
  322. ret = -EINVAL;
  323. pm_runtime_disable(&pdev->dev);
  324. dev_err(&pdev->dev, "can't enable clock\n");
  325. return ret;
  326. }
  327. init_completion(&info->elm_completion);
  328. INIT_LIST_HEAD(&info->list);
  329. list_add(&info->list, &elm_devices);
  330. platform_set_drvdata(pdev, info);
  331. return ret;
  332. }
  333. static int elm_remove(struct platform_device *pdev)
  334. {
  335. pm_runtime_put_sync(&pdev->dev);
  336. pm_runtime_disable(&pdev->dev);
  337. return 0;
  338. }
  339. /**
  340. * elm_context_save
  341. * saves ELM configurations to preserve them across Hardware powered-down
  342. */
  343. static int elm_context_save(struct elm_info *info)
  344. {
  345. struct elm_registers *regs = &info->elm_regs;
  346. enum bch_ecc bch_type = info->bch_type;
  347. u32 offset = 0, i;
  348. regs->elm_irqenable = elm_read_reg(info, ELM_IRQENABLE);
  349. regs->elm_sysconfig = elm_read_reg(info, ELM_SYSCONFIG);
  350. regs->elm_location_config = elm_read_reg(info, ELM_LOCATION_CONFIG);
  351. regs->elm_page_ctrl = elm_read_reg(info, ELM_PAGE_CTRL);
  352. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  353. offset = i * SYNDROME_FRAGMENT_REG_SIZE;
  354. switch (bch_type) {
  355. case BCH8_ECC:
  356. regs->elm_syndrome_fragment_3[i] = elm_read_reg(info,
  357. ELM_SYNDROME_FRAGMENT_3 + offset);
  358. regs->elm_syndrome_fragment_2[i] = elm_read_reg(info,
  359. ELM_SYNDROME_FRAGMENT_2 + offset);
  360. case BCH4_ECC:
  361. regs->elm_syndrome_fragment_1[i] = elm_read_reg(info,
  362. ELM_SYNDROME_FRAGMENT_1 + offset);
  363. regs->elm_syndrome_fragment_0[i] = elm_read_reg(info,
  364. ELM_SYNDROME_FRAGMENT_0 + offset);
  365. default:
  366. return -EINVAL;
  367. }
  368. /* ELM SYNDROME_VALID bit in SYNDROME_FRAGMENT_6[] needs
  369. * to be saved for all BCH schemes*/
  370. regs->elm_syndrome_fragment_6[i] = elm_read_reg(info,
  371. ELM_SYNDROME_FRAGMENT_6 + offset);
  372. }
  373. return 0;
  374. }
  375. /**
  376. * elm_context_restore
  377. * writes configurations saved duing power-down back into ELM registers
  378. */
  379. static int elm_context_restore(struct elm_info *info)
  380. {
  381. struct elm_registers *regs = &info->elm_regs;
  382. enum bch_ecc bch_type = info->bch_type;
  383. u32 offset = 0, i;
  384. elm_write_reg(info, ELM_IRQENABLE, regs->elm_irqenable);
  385. elm_write_reg(info, ELM_SYSCONFIG, regs->elm_sysconfig);
  386. elm_write_reg(info, ELM_LOCATION_CONFIG, regs->elm_location_config);
  387. elm_write_reg(info, ELM_PAGE_CTRL, regs->elm_page_ctrl);
  388. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  389. offset = i * SYNDROME_FRAGMENT_REG_SIZE;
  390. switch (bch_type) {
  391. case BCH8_ECC:
  392. elm_write_reg(info, ELM_SYNDROME_FRAGMENT_3 + offset,
  393. regs->elm_syndrome_fragment_3[i]);
  394. elm_write_reg(info, ELM_SYNDROME_FRAGMENT_2 + offset,
  395. regs->elm_syndrome_fragment_2[i]);
  396. case BCH4_ECC:
  397. elm_write_reg(info, ELM_SYNDROME_FRAGMENT_1 + offset,
  398. regs->elm_syndrome_fragment_1[i]);
  399. elm_write_reg(info, ELM_SYNDROME_FRAGMENT_0 + offset,
  400. regs->elm_syndrome_fragment_0[i]);
  401. default:
  402. return -EINVAL;
  403. }
  404. /* ELM_SYNDROME_VALID bit to be set in last to trigger FSM */
  405. elm_write_reg(info, ELM_SYNDROME_FRAGMENT_6 + offset,
  406. regs->elm_syndrome_fragment_6[i] &
  407. ELM_SYNDROME_VALID);
  408. }
  409. return 0;
  410. }
  411. static int elm_suspend(struct device *dev)
  412. {
  413. struct elm_info *info = dev_get_drvdata(dev);
  414. elm_context_save(info);
  415. pm_runtime_put_sync(dev);
  416. return 0;
  417. }
  418. static int elm_resume(struct device *dev)
  419. {
  420. struct elm_info *info = dev_get_drvdata(dev);
  421. pm_runtime_get_sync(dev);
  422. elm_context_restore(info);
  423. return 0;
  424. }
  425. static SIMPLE_DEV_PM_OPS(elm_pm_ops, elm_suspend, elm_resume);
  426. #ifdef CONFIG_OF
  427. static const struct of_device_id elm_of_match[] = {
  428. { .compatible = "ti,am3352-elm" },
  429. {},
  430. };
  431. MODULE_DEVICE_TABLE(of, elm_of_match);
  432. #endif
  433. static struct platform_driver elm_driver = {
  434. .driver = {
  435. .name = "elm",
  436. .owner = THIS_MODULE,
  437. .of_match_table = of_match_ptr(elm_of_match),
  438. .pm = &elm_pm_ops,
  439. },
  440. .probe = elm_probe,
  441. .remove = elm_remove,
  442. };
  443. module_platform_driver(elm_driver);
  444. MODULE_DESCRIPTION("ELM driver for BCH error correction");
  445. MODULE_AUTHOR("Texas Instruments");
  446. MODULE_ALIAS("platform: elm");
  447. MODULE_LICENSE("GPL v2");