2 * Error Location Module
4 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
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.
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.
18 #include <linux/platform_device.h>
19 #include <linux/module.h>
20 #include <linux/interrupt.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/platform_data/elm.h>
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_6 0x418
32 #define ELM_LOCATION_STATUS 0x800
33 #define ELM_ERROR_LOCATION_0 0x880
35 /* ELM Interrupt Status Register */
36 #define INTR_STATUS_PAGE_VALID BIT(8)
38 /* ELM Interrupt Enable Register */
39 #define INTR_EN_PAGE_MASK BIT(8)
41 /* ELM Location Configuration Register */
42 #define ECC_BCH_LEVEL_MASK 0x3
45 #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
51 /* ELM_ERROR_LOCATION_0-15 Registers */
52 #define ECC_ERROR_LOCATION_MASK 0x1fff
54 #define ELM_ECC_SIZE 0x7ff
56 #define SYNDROME_FRAGMENT_REG_SIZE 0x40
57 #define ERROR_LOCATION_SIZE 0x100
61 void __iomem *elm_base;
62 struct completion elm_completion;
63 struct list_head list;
64 enum bch_ecc bch_type;
67 static LIST_HEAD(elm_devices);
69 static void elm_write_reg(struct elm_info *info, int offset, u32 val)
71 writel(val, info->elm_base + offset);
74 static u32 elm_read_reg(struct elm_info *info, int offset)
76 return readl(info->elm_base + offset);
80 * elm_config - Configure ELM module
82 * @bch_type: Type of BCH ecc
84 void elm_config(struct device *dev, enum bch_ecc bch_type)
87 struct elm_info *info = dev_get_drvdata(dev);
89 reg_val = (bch_type & ECC_BCH_LEVEL_MASK) | (ELM_ECC_SIZE << 16);
90 elm_write_reg(info, ELM_LOCATION_CONFIG, reg_val);
91 info->bch_type = bch_type;
93 EXPORT_SYMBOL(elm_config);
96 * elm_configure_page_mode - Enable/Disable page mode
98 * @index: index number of syndrome fragment vector
99 * @enable: enable/disable flag for page mode
101 * Enable page mode for syndrome fragment index
103 static void elm_configure_page_mode(struct elm_info *info, int index,
108 reg_val = elm_read_reg(info, ELM_PAGE_CTRL);
110 reg_val |= BIT(index); /* enable page mode */
112 reg_val &= ~BIT(index); /* disable page mode */
114 elm_write_reg(info, ELM_PAGE_CTRL, reg_val);
118 * elm_load_syndrome - Load ELM syndrome reg
120 * @err_vec: elm error vectors
121 * @ecc: buffer with calculated ecc
123 * Load syndrome fragment registers with calculated ecc in reverse order.
125 static void elm_load_syndrome(struct elm_info *info,
126 struct elm_errorvec *err_vec, u8 *ecc)
131 for (i = 0; i < ERROR_VECTOR_MAX; i++) {
133 /* Check error reported */
134 if (err_vec[i].error_reported) {
135 elm_configure_page_mode(info, i, true);
136 offset = ELM_SYNDROME_FRAGMENT_0 +
137 SYNDROME_FRAGMENT_REG_SIZE * i;
140 if (info->bch_type) {
142 /* syndrome fragment 0 = ecc[9-12B] */
143 val = cpu_to_be32(*(u32 *) &ecc[9]);
144 elm_write_reg(info, offset, val);
146 /* syndrome fragment 1 = ecc[5-8B] */
148 val = cpu_to_be32(*(u32 *) &ecc[5]);
149 elm_write_reg(info, offset, val);
151 /* syndrome fragment 2 = ecc[1-4B] */
153 val = cpu_to_be32(*(u32 *) &ecc[1]);
154 elm_write_reg(info, offset, val);
156 /* syndrome fragment 3 = ecc[0B] */
159 elm_write_reg(info, offset, val);
161 /* syndrome fragment 0 = ecc[20-52b] bits */
162 val = (cpu_to_be32(*(u32 *) &ecc[3]) >> 4) |
163 ((ecc[2] & 0xf) << 28);
164 elm_write_reg(info, offset, val);
166 /* syndrome fragment 1 = ecc[0-20b] bits */
168 val = cpu_to_be32(*(u32 *) &ecc[0]) >> 12;
169 elm_write_reg(info, offset, val);
173 /* Update ecc pointer with ecc byte size */
174 ecc += info->bch_type ? BCH8_SIZE : BCH4_SIZE;
179 * elm_start_processing - start elm syndrome processing
181 * @err_vec: elm error vectors
183 * Set syndrome valid bit for syndrome fragment registers for which
184 * elm syndrome fragment registers are loaded. This enables elm module
185 * to start processing syndrome vectors.
187 static void elm_start_processing(struct elm_info *info,
188 struct elm_errorvec *err_vec)
194 * Set syndrome vector valid, so that ELM module
195 * will process it for vectors error is reported
197 for (i = 0; i < ERROR_VECTOR_MAX; i++) {
198 if (err_vec[i].error_reported) {
199 offset = ELM_SYNDROME_FRAGMENT_6 +
200 SYNDROME_FRAGMENT_REG_SIZE * i;
201 reg_val = elm_read_reg(info, offset);
202 reg_val |= ELM_SYNDROME_VALID;
203 elm_write_reg(info, offset, reg_val);
209 * elm_error_correction - locate correctable error position
211 * @err_vec: elm error vectors
213 * On completion of processing by elm module, error location status
214 * register updated with correctable/uncorrectable error information.
215 * In case of correctable errors, number of errors located from
216 * elm location status register & read the positions from
217 * elm error location register.
219 static void elm_error_correction(struct elm_info *info,
220 struct elm_errorvec *err_vec)
222 int i, j, errors = 0;
226 for (i = 0; i < ERROR_VECTOR_MAX; i++) {
228 /* Check error reported */
229 if (err_vec[i].error_reported) {
230 offset = ELM_LOCATION_STATUS + ERROR_LOCATION_SIZE * i;
231 reg_val = elm_read_reg(info, offset);
233 /* Check correctable error or not */
234 if (reg_val & ECC_CORRECTABLE_MASK) {
235 offset = ELM_ERROR_LOCATION_0 +
236 ERROR_LOCATION_SIZE * i;
238 /* Read count of correctable errors */
239 err_vec[i].error_count = reg_val &
242 /* Update the error locations in error vector */
243 for (j = 0; j < err_vec[i].error_count; j++) {
245 reg_val = elm_read_reg(info, offset);
246 err_vec[i].error_loc[j] = reg_val &
247 ECC_ERROR_LOCATION_MASK;
249 /* Update error location register */
253 errors += err_vec[i].error_count;
255 err_vec[i].error_uncorrectable = true;
258 /* Clearing interrupts for processed error vectors */
259 elm_write_reg(info, ELM_IRQSTATUS, BIT(i));
261 /* Disable page mode */
262 elm_configure_page_mode(info, i, false);
268 * elm_decode_bch_error_page - Locate error position
269 * @dev: device pointer
270 * @ecc_calc: calculated ECC bytes from GPMC
271 * @err_vec: elm error vectors
273 * Called with one or more error reported vectors & vectors with
274 * error reported is updated in err_vec[].error_reported
276 void elm_decode_bch_error_page(struct device *dev, u8 *ecc_calc,
277 struct elm_errorvec *err_vec)
279 struct elm_info *info = dev_get_drvdata(dev);
282 /* Enable page mode interrupt */
283 reg_val = elm_read_reg(info, ELM_IRQSTATUS);
284 elm_write_reg(info, ELM_IRQSTATUS, reg_val & INTR_STATUS_PAGE_VALID);
285 elm_write_reg(info, ELM_IRQENABLE, INTR_EN_PAGE_MASK);
287 /* Load valid ecc byte to syndrome fragment register */
288 elm_load_syndrome(info, err_vec, ecc_calc);
290 /* Enable syndrome processing for which syndrome fragment is updated */
291 elm_start_processing(info, err_vec);
293 /* Wait for ELM module to finish locating error correction */
294 wait_for_completion(&info->elm_completion);
296 /* Disable page mode interrupt */
297 reg_val = elm_read_reg(info, ELM_IRQENABLE);
298 elm_write_reg(info, ELM_IRQENABLE, reg_val & ~INTR_EN_PAGE_MASK);
299 elm_error_correction(info, err_vec);
301 EXPORT_SYMBOL(elm_decode_bch_error_page);
303 static irqreturn_t elm_isr(int this_irq, void *dev_id)
306 struct elm_info *info = dev_id;
308 reg_val = elm_read_reg(info, ELM_IRQSTATUS);
310 /* All error vectors processed */
311 if (reg_val & INTR_STATUS_PAGE_VALID) {
312 elm_write_reg(info, ELM_IRQSTATUS,
313 reg_val & INTR_STATUS_PAGE_VALID);
314 complete(&info->elm_completion);
321 static int elm_probe(struct platform_device *pdev)
324 struct resource *res, *irq;
325 struct elm_info *info;
327 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
329 dev_err(&pdev->dev, "failed to allocate memory\n");
333 info->dev = &pdev->dev;
335 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
337 dev_err(&pdev->dev, "no irq resource defined\n");
341 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
343 dev_err(&pdev->dev, "no memory resource defined\n");
347 info->elm_base = devm_request_and_ioremap(&pdev->dev, res);
349 return -EADDRNOTAVAIL;
351 ret = devm_request_irq(&pdev->dev, irq->start, elm_isr, 0,
354 dev_err(&pdev->dev, "failure requesting irq %i\n", irq->start);
358 pm_runtime_enable(&pdev->dev);
359 if (pm_runtime_get_sync(&pdev->dev)) {
361 pm_runtime_disable(&pdev->dev);
362 dev_err(&pdev->dev, "can't enable clock\n");
366 init_completion(&info->elm_completion);
367 INIT_LIST_HEAD(&info->list);
368 list_add(&info->list, &elm_devices);
369 platform_set_drvdata(pdev, info);
373 static int elm_remove(struct platform_device *pdev)
375 pm_runtime_put_sync(&pdev->dev);
376 pm_runtime_disable(&pdev->dev);
377 platform_set_drvdata(pdev, NULL);
382 static const struct of_device_id elm_of_match[] = {
383 { .compatible = "ti,am3352-elm" },
386 MODULE_DEVICE_TABLE(of, elm_of_match);
389 static struct platform_driver elm_driver = {
392 .owner = THIS_MODULE,
393 .of_match_table = of_match_ptr(elm_of_match),
396 .remove = elm_remove,
399 module_platform_driver(elm_driver);
401 MODULE_DESCRIPTION("ELM driver for BCH error correction");
402 MODULE_AUTHOR("Texas Instruments");
403 MODULE_ALIAS("platform: elm");
404 MODULE_LICENSE("GPL v2");