elm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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/pm_runtime.h>
  23. #include <linux/platform_data/elm.h>
  24. #define ELM_IRQSTATUS 0x018
  25. #define ELM_IRQENABLE 0x01c
  26. #define ELM_LOCATION_CONFIG 0x020
  27. #define ELM_PAGE_CTRL 0x080
  28. #define ELM_SYNDROME_FRAGMENT_0 0x400
  29. #define ELM_SYNDROME_FRAGMENT_6 0x418
  30. #define ELM_LOCATION_STATUS 0x800
  31. #define ELM_ERROR_LOCATION_0 0x880
  32. /* ELM Interrupt Status Register */
  33. #define INTR_STATUS_PAGE_VALID BIT(8)
  34. /* ELM Interrupt Enable Register */
  35. #define INTR_EN_PAGE_MASK BIT(8)
  36. /* ELM Location Configuration Register */
  37. #define ECC_BCH_LEVEL_MASK 0x3
  38. /* ELM syndrome */
  39. #define ELM_SYNDROME_VALID BIT(16)
  40. /* ELM_LOCATION_STATUS Register */
  41. #define ECC_CORRECTABLE_MASK BIT(8)
  42. #define ECC_NB_ERRORS_MASK 0x1f
  43. /* ELM_ERROR_LOCATION_0-15 Registers */
  44. #define ECC_ERROR_LOCATION_MASK 0x1fff
  45. #define ELM_ECC_SIZE 0x7ff
  46. #define SYNDROME_FRAGMENT_REG_SIZE 0x40
  47. #define ERROR_LOCATION_SIZE 0x100
  48. struct elm_info {
  49. struct device *dev;
  50. void __iomem *elm_base;
  51. struct completion elm_completion;
  52. struct list_head list;
  53. enum bch_ecc bch_type;
  54. };
  55. static LIST_HEAD(elm_devices);
  56. static void elm_write_reg(struct elm_info *info, int offset, u32 val)
  57. {
  58. writel(val, info->elm_base + offset);
  59. }
  60. static u32 elm_read_reg(struct elm_info *info, int offset)
  61. {
  62. return readl(info->elm_base + offset);
  63. }
  64. /**
  65. * elm_config - Configure ELM module
  66. * @dev: ELM device
  67. * @bch_type: Type of BCH ecc
  68. */
  69. int elm_config(struct device *dev, enum bch_ecc bch_type)
  70. {
  71. u32 reg_val;
  72. struct elm_info *info = dev_get_drvdata(dev);
  73. if (!info) {
  74. dev_err(dev, "Unable to configure elm - device not probed?\n");
  75. return -ENODEV;
  76. }
  77. reg_val = (bch_type & ECC_BCH_LEVEL_MASK) | (ELM_ECC_SIZE << 16);
  78. elm_write_reg(info, ELM_LOCATION_CONFIG, reg_val);
  79. info->bch_type = bch_type;
  80. return 0;
  81. }
  82. EXPORT_SYMBOL(elm_config);
  83. /**
  84. * elm_configure_page_mode - Enable/Disable page mode
  85. * @info: elm info
  86. * @index: index number of syndrome fragment vector
  87. * @enable: enable/disable flag for page mode
  88. *
  89. * Enable page mode for syndrome fragment index
  90. */
  91. static void elm_configure_page_mode(struct elm_info *info, int index,
  92. bool enable)
  93. {
  94. u32 reg_val;
  95. reg_val = elm_read_reg(info, ELM_PAGE_CTRL);
  96. if (enable)
  97. reg_val |= BIT(index); /* enable page mode */
  98. else
  99. reg_val &= ~BIT(index); /* disable page mode */
  100. elm_write_reg(info, ELM_PAGE_CTRL, reg_val);
  101. }
  102. /**
  103. * elm_load_syndrome - Load ELM syndrome reg
  104. * @info: elm info
  105. * @err_vec: elm error vectors
  106. * @ecc: buffer with calculated ecc
  107. *
  108. * Load syndrome fragment registers with calculated ecc in reverse order.
  109. */
  110. static void elm_load_syndrome(struct elm_info *info,
  111. struct elm_errorvec *err_vec, u8 *ecc)
  112. {
  113. int i, offset;
  114. u32 val;
  115. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  116. /* Check error reported */
  117. if (err_vec[i].error_reported) {
  118. elm_configure_page_mode(info, i, true);
  119. offset = ELM_SYNDROME_FRAGMENT_0 +
  120. SYNDROME_FRAGMENT_REG_SIZE * i;
  121. /* BCH8 */
  122. if (info->bch_type) {
  123. /* syndrome fragment 0 = ecc[9-12B] */
  124. val = cpu_to_be32(*(u32 *) &ecc[9]);
  125. elm_write_reg(info, offset, val);
  126. /* syndrome fragment 1 = ecc[5-8B] */
  127. offset += 4;
  128. val = cpu_to_be32(*(u32 *) &ecc[5]);
  129. elm_write_reg(info, offset, val);
  130. /* syndrome fragment 2 = ecc[1-4B] */
  131. offset += 4;
  132. val = cpu_to_be32(*(u32 *) &ecc[1]);
  133. elm_write_reg(info, offset, val);
  134. /* syndrome fragment 3 = ecc[0B] */
  135. offset += 4;
  136. val = ecc[0];
  137. elm_write_reg(info, offset, val);
  138. } else {
  139. /* syndrome fragment 0 = ecc[20-52b] bits */
  140. val = (cpu_to_be32(*(u32 *) &ecc[3]) >> 4) |
  141. ((ecc[2] & 0xf) << 28);
  142. elm_write_reg(info, offset, val);
  143. /* syndrome fragment 1 = ecc[0-20b] bits */
  144. offset += 4;
  145. val = cpu_to_be32(*(u32 *) &ecc[0]) >> 12;
  146. elm_write_reg(info, offset, val);
  147. }
  148. }
  149. /* Update ecc pointer with ecc byte size */
  150. ecc += info->bch_type ? BCH8_SIZE : BCH4_SIZE;
  151. }
  152. }
  153. /**
  154. * elm_start_processing - start elm syndrome processing
  155. * @info: elm info
  156. * @err_vec: elm error vectors
  157. *
  158. * Set syndrome valid bit for syndrome fragment registers for which
  159. * elm syndrome fragment registers are loaded. This enables elm module
  160. * to start processing syndrome vectors.
  161. */
  162. static void elm_start_processing(struct elm_info *info,
  163. struct elm_errorvec *err_vec)
  164. {
  165. int i, offset;
  166. u32 reg_val;
  167. /*
  168. * Set syndrome vector valid, so that ELM module
  169. * will process it for vectors error is reported
  170. */
  171. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  172. if (err_vec[i].error_reported) {
  173. offset = ELM_SYNDROME_FRAGMENT_6 +
  174. SYNDROME_FRAGMENT_REG_SIZE * i;
  175. reg_val = elm_read_reg(info, offset);
  176. reg_val |= ELM_SYNDROME_VALID;
  177. elm_write_reg(info, offset, reg_val);
  178. }
  179. }
  180. }
  181. /**
  182. * elm_error_correction - locate correctable error position
  183. * @info: elm info
  184. * @err_vec: elm error vectors
  185. *
  186. * On completion of processing by elm module, error location status
  187. * register updated with correctable/uncorrectable error information.
  188. * In case of correctable errors, number of errors located from
  189. * elm location status register & read the positions from
  190. * elm error location register.
  191. */
  192. static void elm_error_correction(struct elm_info *info,
  193. struct elm_errorvec *err_vec)
  194. {
  195. int i, j, errors = 0;
  196. int offset;
  197. u32 reg_val;
  198. for (i = 0; i < ERROR_VECTOR_MAX; i++) {
  199. /* Check error reported */
  200. if (err_vec[i].error_reported) {
  201. offset = ELM_LOCATION_STATUS + ERROR_LOCATION_SIZE * i;
  202. reg_val = elm_read_reg(info, offset);
  203. /* Check correctable error or not */
  204. if (reg_val & ECC_CORRECTABLE_MASK) {
  205. offset = ELM_ERROR_LOCATION_0 +
  206. ERROR_LOCATION_SIZE * i;
  207. /* Read count of correctable errors */
  208. err_vec[i].error_count = reg_val &
  209. ECC_NB_ERRORS_MASK;
  210. /* Update the error locations in error vector */
  211. for (j = 0; j < err_vec[i].error_count; j++) {
  212. reg_val = elm_read_reg(info, offset);
  213. err_vec[i].error_loc[j] = reg_val &
  214. ECC_ERROR_LOCATION_MASK;
  215. /* Update error location register */
  216. offset += 4;
  217. }
  218. errors += err_vec[i].error_count;
  219. } else {
  220. err_vec[i].error_uncorrectable = true;
  221. }
  222. /* Clearing interrupts for processed error vectors */
  223. elm_write_reg(info, ELM_IRQSTATUS, BIT(i));
  224. /* Disable page mode */
  225. elm_configure_page_mode(info, i, false);
  226. }
  227. }
  228. }
  229. /**
  230. * elm_decode_bch_error_page - Locate error position
  231. * @dev: device pointer
  232. * @ecc_calc: calculated ECC bytes from GPMC
  233. * @err_vec: elm error vectors
  234. *
  235. * Called with one or more error reported vectors & vectors with
  236. * error reported is updated in err_vec[].error_reported
  237. */
  238. void elm_decode_bch_error_page(struct device *dev, u8 *ecc_calc,
  239. struct elm_errorvec *err_vec)
  240. {
  241. struct elm_info *info = dev_get_drvdata(dev);
  242. u32 reg_val;
  243. /* Enable page mode interrupt */
  244. reg_val = elm_read_reg(info, ELM_IRQSTATUS);
  245. elm_write_reg(info, ELM_IRQSTATUS, reg_val & INTR_STATUS_PAGE_VALID);
  246. elm_write_reg(info, ELM_IRQENABLE, INTR_EN_PAGE_MASK);
  247. /* Load valid ecc byte to syndrome fragment register */
  248. elm_load_syndrome(info, err_vec, ecc_calc);
  249. /* Enable syndrome processing for which syndrome fragment is updated */
  250. elm_start_processing(info, err_vec);
  251. /* Wait for ELM module to finish locating error correction */
  252. wait_for_completion(&info->elm_completion);
  253. /* Disable page mode interrupt */
  254. reg_val = elm_read_reg(info, ELM_IRQENABLE);
  255. elm_write_reg(info, ELM_IRQENABLE, reg_val & ~INTR_EN_PAGE_MASK);
  256. elm_error_correction(info, err_vec);
  257. }
  258. EXPORT_SYMBOL(elm_decode_bch_error_page);
  259. static irqreturn_t elm_isr(int this_irq, void *dev_id)
  260. {
  261. u32 reg_val;
  262. struct elm_info *info = dev_id;
  263. reg_val = elm_read_reg(info, ELM_IRQSTATUS);
  264. /* All error vectors processed */
  265. if (reg_val & INTR_STATUS_PAGE_VALID) {
  266. elm_write_reg(info, ELM_IRQSTATUS,
  267. reg_val & INTR_STATUS_PAGE_VALID);
  268. complete(&info->elm_completion);
  269. return IRQ_HANDLED;
  270. }
  271. return IRQ_NONE;
  272. }
  273. static int elm_probe(struct platform_device *pdev)
  274. {
  275. int ret = 0;
  276. struct resource *res, *irq;
  277. struct elm_info *info;
  278. info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
  279. if (!info) {
  280. dev_err(&pdev->dev, "failed to allocate memory\n");
  281. return -ENOMEM;
  282. }
  283. info->dev = &pdev->dev;
  284. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  285. if (!irq) {
  286. dev_err(&pdev->dev, "no irq resource defined\n");
  287. return -ENODEV;
  288. }
  289. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  290. if (!res) {
  291. dev_err(&pdev->dev, "no memory resource defined\n");
  292. return -ENODEV;
  293. }
  294. info->elm_base = devm_request_and_ioremap(&pdev->dev, res);
  295. if (!info->elm_base)
  296. return -EADDRNOTAVAIL;
  297. ret = devm_request_irq(&pdev->dev, irq->start, elm_isr, 0,
  298. pdev->name, info);
  299. if (ret) {
  300. dev_err(&pdev->dev, "failure requesting irq %i\n", irq->start);
  301. return ret;
  302. }
  303. pm_runtime_enable(&pdev->dev);
  304. if (pm_runtime_get_sync(&pdev->dev)) {
  305. ret = -EINVAL;
  306. pm_runtime_disable(&pdev->dev);
  307. dev_err(&pdev->dev, "can't enable clock\n");
  308. return ret;
  309. }
  310. init_completion(&info->elm_completion);
  311. INIT_LIST_HEAD(&info->list);
  312. list_add(&info->list, &elm_devices);
  313. platform_set_drvdata(pdev, info);
  314. return ret;
  315. }
  316. static int elm_remove(struct platform_device *pdev)
  317. {
  318. pm_runtime_put_sync(&pdev->dev);
  319. pm_runtime_disable(&pdev->dev);
  320. platform_set_drvdata(pdev, NULL);
  321. return 0;
  322. }
  323. #ifdef CONFIG_OF
  324. static const struct of_device_id elm_of_match[] = {
  325. { .compatible = "ti,am3352-elm" },
  326. {},
  327. };
  328. MODULE_DEVICE_TABLE(of, elm_of_match);
  329. #endif
  330. static struct platform_driver elm_driver = {
  331. .driver = {
  332. .name = "elm",
  333. .owner = THIS_MODULE,
  334. .of_match_table = of_match_ptr(elm_of_match),
  335. },
  336. .probe = elm_probe,
  337. .remove = elm_remove,
  338. };
  339. module_platform_driver(elm_driver);
  340. MODULE_DESCRIPTION("ELM driver for BCH error correction");
  341. MODULE_AUTHOR("Texas Instruments");
  342. MODULE_ALIAS("platform: elm");
  343. MODULE_LICENSE("GPL v2");