elm.c 10 KB

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