blob: 0a037b15c11b5551d3024976e9996232a1a9830d [file] [log] [blame]
Philip Avinashbf224332013-01-04 13:26:50 +05301/*
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
Pekon Guptac7ea95a2014-03-20 18:48:37 +053018#define DRIVER_NAME "omap-elm"
19
Philip Avinashbf224332013-01-04 13:26:50 +053020#include <linux/platform_device.h>
21#include <linux/module.h>
22#include <linux/interrupt.h>
23#include <linux/io.h>
24#include <linux/of.h>
avinash philip11f164d2013-06-18 00:16:39 +053025#include <linux/sched.h>
Philip Avinashbf224332013-01-04 13:26:50 +053026#include <linux/pm_runtime.h>
27#include <linux/platform_data/elm.h>
28
avinash philip11f164d2013-06-18 00:16:39 +053029#define ELM_SYSCONFIG 0x010
Philip Avinashbf224332013-01-04 13:26:50 +053030#define ELM_IRQSTATUS 0x018
31#define ELM_IRQENABLE 0x01c
32#define ELM_LOCATION_CONFIG 0x020
33#define ELM_PAGE_CTRL 0x080
34#define ELM_SYNDROME_FRAGMENT_0 0x400
avinash philip11f164d2013-06-18 00:16:39 +053035#define ELM_SYNDROME_FRAGMENT_1 0x404
36#define ELM_SYNDROME_FRAGMENT_2 0x408
37#define ELM_SYNDROME_FRAGMENT_3 0x40c
38#define ELM_SYNDROME_FRAGMENT_4 0x410
39#define ELM_SYNDROME_FRAGMENT_5 0x414
Philip Avinashbf224332013-01-04 13:26:50 +053040#define ELM_SYNDROME_FRAGMENT_6 0x418
41#define ELM_LOCATION_STATUS 0x800
42#define ELM_ERROR_LOCATION_0 0x880
43
44/* ELM Interrupt Status Register */
45#define INTR_STATUS_PAGE_VALID BIT(8)
46
47/* ELM Interrupt Enable Register */
48#define INTR_EN_PAGE_MASK BIT(8)
49
50/* ELM Location Configuration Register */
51#define ECC_BCH_LEVEL_MASK 0x3
52
53/* ELM syndrome */
54#define ELM_SYNDROME_VALID BIT(16)
55
56/* ELM_LOCATION_STATUS Register */
57#define ECC_CORRECTABLE_MASK BIT(8)
58#define ECC_NB_ERRORS_MASK 0x1f
59
60/* ELM_ERROR_LOCATION_0-15 Registers */
61#define ECC_ERROR_LOCATION_MASK 0x1fff
62
63#define ELM_ECC_SIZE 0x7ff
64
65#define SYNDROME_FRAGMENT_REG_SIZE 0x40
66#define ERROR_LOCATION_SIZE 0x100
67
avinash philip11f164d2013-06-18 00:16:39 +053068struct elm_registers {
69 u32 elm_irqenable;
70 u32 elm_sysconfig;
71 u32 elm_location_config;
72 u32 elm_page_ctrl;
73 u32 elm_syndrome_fragment_6[ERROR_VECTOR_MAX];
74 u32 elm_syndrome_fragment_5[ERROR_VECTOR_MAX];
75 u32 elm_syndrome_fragment_4[ERROR_VECTOR_MAX];
76 u32 elm_syndrome_fragment_3[ERROR_VECTOR_MAX];
77 u32 elm_syndrome_fragment_2[ERROR_VECTOR_MAX];
78 u32 elm_syndrome_fragment_1[ERROR_VECTOR_MAX];
79 u32 elm_syndrome_fragment_0[ERROR_VECTOR_MAX];
80};
81
Philip Avinashbf224332013-01-04 13:26:50 +053082struct elm_info {
83 struct device *dev;
84 void __iomem *elm_base;
85 struct completion elm_completion;
86 struct list_head list;
87 enum bch_ecc bch_type;
avinash philip11f164d2013-06-18 00:16:39 +053088 struct elm_registers elm_regs;
Pekon Gupta79f01352014-03-20 18:48:36 +053089 int ecc_steps;
Pekon Guptaea076022014-03-20 18:48:35 +053090 int ecc_syndrome_size;
Philip Avinashbf224332013-01-04 13:26:50 +053091};
92
93static LIST_HEAD(elm_devices);
94
95static void elm_write_reg(struct elm_info *info, int offset, u32 val)
96{
97 writel(val, info->elm_base + offset);
98}
99
100static u32 elm_read_reg(struct elm_info *info, int offset)
101{
102 return readl(info->elm_base + offset);
103}
104
105/**
106 * elm_config - Configure ELM module
107 * @dev: ELM device
108 * @bch_type: Type of BCH ecc
109 */
Pekon Gupta3f4eb142014-03-20 18:48:34 +0530110int elm_config(struct device *dev, enum bch_ecc bch_type,
111 int ecc_steps, int ecc_step_size, int ecc_syndrome_size)
Philip Avinashbf224332013-01-04 13:26:50 +0530112{
113 u32 reg_val;
114 struct elm_info *info = dev_get_drvdata(dev);
115
Daniel Mackad245782013-03-04 00:57:20 +0100116 if (!info) {
117 dev_err(dev, "Unable to configure elm - device not probed?\n");
118 return -ENODEV;
119 }
Pekon Gupta3f4eb142014-03-20 18:48:34 +0530120 /* ELM cannot detect ECC errors for chunks > 1KB */
121 if (ecc_step_size > ((ELM_ECC_SIZE + 1) / 2)) {
122 dev_err(dev, "unsupported config ecc-size=%d\n", ecc_step_size);
123 return -EINVAL;
124 }
125 /* ELM support 8 error syndrome process */
126 if (ecc_steps > ERROR_VECTOR_MAX) {
127 dev_err(dev, "unsupported config ecc-step=%d\n", ecc_steps);
128 return -EINVAL;
129 }
Daniel Mackad245782013-03-04 00:57:20 +0100130
Philip Avinashbf224332013-01-04 13:26:50 +0530131 reg_val = (bch_type & ECC_BCH_LEVEL_MASK) | (ELM_ECC_SIZE << 16);
132 elm_write_reg(info, ELM_LOCATION_CONFIG, reg_val);
Pekon Guptaea076022014-03-20 18:48:35 +0530133 info->bch_type = bch_type;
Pekon Gupta79f01352014-03-20 18:48:36 +0530134 info->ecc_steps = ecc_steps;
Pekon Guptaea076022014-03-20 18:48:35 +0530135 info->ecc_syndrome_size = ecc_syndrome_size;
Daniel Mackad245782013-03-04 00:57:20 +0100136
137 return 0;
Philip Avinashbf224332013-01-04 13:26:50 +0530138}
139EXPORT_SYMBOL(elm_config);
140
141/**
142 * elm_configure_page_mode - Enable/Disable page mode
143 * @info: elm info
144 * @index: index number of syndrome fragment vector
145 * @enable: enable/disable flag for page mode
146 *
147 * Enable page mode for syndrome fragment index
148 */
149static void elm_configure_page_mode(struct elm_info *info, int index,
150 bool enable)
151{
152 u32 reg_val;
153
154 reg_val = elm_read_reg(info, ELM_PAGE_CTRL);
155 if (enable)
156 reg_val |= BIT(index); /* enable page mode */
157 else
158 reg_val &= ~BIT(index); /* disable page mode */
159
160 elm_write_reg(info, ELM_PAGE_CTRL, reg_val);
161}
162
163/**
164 * elm_load_syndrome - Load ELM syndrome reg
165 * @info: elm info
166 * @err_vec: elm error vectors
167 * @ecc: buffer with calculated ecc
168 *
169 * Load syndrome fragment registers with calculated ecc in reverse order.
170 */
171static void elm_load_syndrome(struct elm_info *info,
172 struct elm_errorvec *err_vec, u8 *ecc)
173{
174 int i, offset;
175 u32 val;
176
Pekon Gupta79f01352014-03-20 18:48:36 +0530177 for (i = 0; i < info->ecc_steps; i++) {
Philip Avinashbf224332013-01-04 13:26:50 +0530178
179 /* Check error reported */
180 if (err_vec[i].error_reported) {
181 elm_configure_page_mode(info, i, true);
182 offset = ELM_SYNDROME_FRAGMENT_0 +
183 SYNDROME_FRAGMENT_REG_SIZE * i;
Pekon Guptaea076022014-03-20 18:48:35 +0530184 switch (info->bch_type) {
185 case BCH8_ECC:
Philip Avinashbf224332013-01-04 13:26:50 +0530186 /* syndrome fragment 0 = ecc[9-12B] */
187 val = cpu_to_be32(*(u32 *) &ecc[9]);
188 elm_write_reg(info, offset, val);
189
190 /* syndrome fragment 1 = ecc[5-8B] */
191 offset += 4;
192 val = cpu_to_be32(*(u32 *) &ecc[5]);
193 elm_write_reg(info, offset, val);
194
195 /* syndrome fragment 2 = ecc[1-4B] */
196 offset += 4;
197 val = cpu_to_be32(*(u32 *) &ecc[1]);
198 elm_write_reg(info, offset, val);
199
200 /* syndrome fragment 3 = ecc[0B] */
201 offset += 4;
202 val = ecc[0];
203 elm_write_reg(info, offset, val);
Pekon Guptaea076022014-03-20 18:48:35 +0530204 break;
205 case BCH4_ECC:
Philip Avinashbf224332013-01-04 13:26:50 +0530206 /* syndrome fragment 0 = ecc[20-52b] bits */
207 val = (cpu_to_be32(*(u32 *) &ecc[3]) >> 4) |
208 ((ecc[2] & 0xf) << 28);
209 elm_write_reg(info, offset, val);
210
211 /* syndrome fragment 1 = ecc[0-20b] bits */
212 offset += 4;
213 val = cpu_to_be32(*(u32 *) &ecc[0]) >> 12;
214 elm_write_reg(info, offset, val);
Pekon Guptaea076022014-03-20 18:48:35 +0530215 break;
216 default:
217 pr_err("invalid config bch_type\n");
Philip Avinashbf224332013-01-04 13:26:50 +0530218 }
219 }
220
221 /* Update ecc pointer with ecc byte size */
Pekon Guptaea076022014-03-20 18:48:35 +0530222 ecc += info->ecc_syndrome_size;
Philip Avinashbf224332013-01-04 13:26:50 +0530223 }
224}
225
226/**
227 * elm_start_processing - start elm syndrome processing
228 * @info: elm info
229 * @err_vec: elm error vectors
230 *
231 * Set syndrome valid bit for syndrome fragment registers for which
232 * elm syndrome fragment registers are loaded. This enables elm module
233 * to start processing syndrome vectors.
234 */
235static void elm_start_processing(struct elm_info *info,
236 struct elm_errorvec *err_vec)
237{
238 int i, offset;
239 u32 reg_val;
240
241 /*
242 * Set syndrome vector valid, so that ELM module
243 * will process it for vectors error is reported
244 */
Pekon Gupta79f01352014-03-20 18:48:36 +0530245 for (i = 0; i < info->ecc_steps; i++) {
Philip Avinashbf224332013-01-04 13:26:50 +0530246 if (err_vec[i].error_reported) {
247 offset = ELM_SYNDROME_FRAGMENT_6 +
248 SYNDROME_FRAGMENT_REG_SIZE * i;
249 reg_val = elm_read_reg(info, offset);
250 reg_val |= ELM_SYNDROME_VALID;
251 elm_write_reg(info, offset, reg_val);
252 }
253 }
254}
255
256/**
257 * elm_error_correction - locate correctable error position
258 * @info: elm info
259 * @err_vec: elm error vectors
260 *
261 * On completion of processing by elm module, error location status
262 * register updated with correctable/uncorrectable error information.
263 * In case of correctable errors, number of errors located from
264 * elm location status register & read the positions from
265 * elm error location register.
266 */
267static void elm_error_correction(struct elm_info *info,
268 struct elm_errorvec *err_vec)
269{
270 int i, j, errors = 0;
271 int offset;
272 u32 reg_val;
273
Pekon Gupta79f01352014-03-20 18:48:36 +0530274 for (i = 0; i < info->ecc_steps; i++) {
Philip Avinashbf224332013-01-04 13:26:50 +0530275
276 /* Check error reported */
277 if (err_vec[i].error_reported) {
278 offset = ELM_LOCATION_STATUS + ERROR_LOCATION_SIZE * i;
279 reg_val = elm_read_reg(info, offset);
280
281 /* Check correctable error or not */
282 if (reg_val & ECC_CORRECTABLE_MASK) {
283 offset = ELM_ERROR_LOCATION_0 +
284 ERROR_LOCATION_SIZE * i;
285
286 /* Read count of correctable errors */
287 err_vec[i].error_count = reg_val &
288 ECC_NB_ERRORS_MASK;
289
290 /* Update the error locations in error vector */
291 for (j = 0; j < err_vec[i].error_count; j++) {
292
293 reg_val = elm_read_reg(info, offset);
294 err_vec[i].error_loc[j] = reg_val &
295 ECC_ERROR_LOCATION_MASK;
296
297 /* Update error location register */
298 offset += 4;
299 }
300
301 errors += err_vec[i].error_count;
302 } else {
303 err_vec[i].error_uncorrectable = true;
304 }
305
306 /* Clearing interrupts for processed error vectors */
307 elm_write_reg(info, ELM_IRQSTATUS, BIT(i));
308
309 /* Disable page mode */
310 elm_configure_page_mode(info, i, false);
311 }
312 }
313}
314
315/**
316 * elm_decode_bch_error_page - Locate error position
317 * @dev: device pointer
318 * @ecc_calc: calculated ECC bytes from GPMC
319 * @err_vec: elm error vectors
320 *
321 * Called with one or more error reported vectors & vectors with
322 * error reported is updated in err_vec[].error_reported
323 */
324void elm_decode_bch_error_page(struct device *dev, u8 *ecc_calc,
325 struct elm_errorvec *err_vec)
326{
327 struct elm_info *info = dev_get_drvdata(dev);
328 u32 reg_val;
329
330 /* Enable page mode interrupt */
331 reg_val = elm_read_reg(info, ELM_IRQSTATUS);
332 elm_write_reg(info, ELM_IRQSTATUS, reg_val & INTR_STATUS_PAGE_VALID);
333 elm_write_reg(info, ELM_IRQENABLE, INTR_EN_PAGE_MASK);
334
335 /* Load valid ecc byte to syndrome fragment register */
336 elm_load_syndrome(info, err_vec, ecc_calc);
337
338 /* Enable syndrome processing for which syndrome fragment is updated */
339 elm_start_processing(info, err_vec);
340
341 /* Wait for ELM module to finish locating error correction */
342 wait_for_completion(&info->elm_completion);
343
344 /* Disable page mode interrupt */
345 reg_val = elm_read_reg(info, ELM_IRQENABLE);
346 elm_write_reg(info, ELM_IRQENABLE, reg_val & ~INTR_EN_PAGE_MASK);
347 elm_error_correction(info, err_vec);
348}
349EXPORT_SYMBOL(elm_decode_bch_error_page);
350
351static irqreturn_t elm_isr(int this_irq, void *dev_id)
352{
353 u32 reg_val;
354 struct elm_info *info = dev_id;
355
356 reg_val = elm_read_reg(info, ELM_IRQSTATUS);
357
358 /* All error vectors processed */
359 if (reg_val & INTR_STATUS_PAGE_VALID) {
360 elm_write_reg(info, ELM_IRQSTATUS,
361 reg_val & INTR_STATUS_PAGE_VALID);
362 complete(&info->elm_completion);
363 return IRQ_HANDLED;
364 }
365
366 return IRQ_NONE;
367}
368
369static int elm_probe(struct platform_device *pdev)
370{
371 int ret = 0;
372 struct resource *res, *irq;
373 struct elm_info *info;
374
375 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
Jingoo Han834fa8a2014-02-06 15:08:53 +0900376 if (!info)
Philip Avinashbf224332013-01-04 13:26:50 +0530377 return -ENOMEM;
Philip Avinashbf224332013-01-04 13:26:50 +0530378
379 info->dev = &pdev->dev;
380
381 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
382 if (!irq) {
383 dev_err(&pdev->dev, "no irq resource defined\n");
384 return -ENODEV;
385 }
386
387 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Silviu-Mihai Popescu43579682013-04-11 00:01:51 +0300388 info->elm_base = devm_ioremap_resource(&pdev->dev, res);
389 if (IS_ERR(info->elm_base))
390 return PTR_ERR(info->elm_base);
Philip Avinashbf224332013-01-04 13:26:50 +0530391
392 ret = devm_request_irq(&pdev->dev, irq->start, elm_isr, 0,
393 pdev->name, info);
394 if (ret) {
395 dev_err(&pdev->dev, "failure requesting irq %i\n", irq->start);
396 return ret;
397 }
398
399 pm_runtime_enable(&pdev->dev);
Stefan Sørensen0a215522014-02-03 15:54:09 +0100400 if (pm_runtime_get_sync(&pdev->dev) < 0) {
Philip Avinashbf224332013-01-04 13:26:50 +0530401 ret = -EINVAL;
402 pm_runtime_disable(&pdev->dev);
403 dev_err(&pdev->dev, "can't enable clock\n");
404 return ret;
405 }
406
407 init_completion(&info->elm_completion);
408 INIT_LIST_HEAD(&info->list);
409 list_add(&info->list, &elm_devices);
410 platform_set_drvdata(pdev, info);
411 return ret;
412}
413
414static int elm_remove(struct platform_device *pdev)
415{
416 pm_runtime_put_sync(&pdev->dev);
417 pm_runtime_disable(&pdev->dev);
Philip Avinashbf224332013-01-04 13:26:50 +0530418 return 0;
419}
420
Wolfram Sang8d808952014-04-17 09:39:07 +0200421#ifdef CONFIG_PM_SLEEP
avinash philip11f164d2013-06-18 00:16:39 +0530422/**
423 * elm_context_save
424 * saves ELM configurations to preserve them across Hardware powered-down
425 */
426static int elm_context_save(struct elm_info *info)
427{
428 struct elm_registers *regs = &info->elm_regs;
429 enum bch_ecc bch_type = info->bch_type;
430 u32 offset = 0, i;
431
432 regs->elm_irqenable = elm_read_reg(info, ELM_IRQENABLE);
433 regs->elm_sysconfig = elm_read_reg(info, ELM_SYSCONFIG);
434 regs->elm_location_config = elm_read_reg(info, ELM_LOCATION_CONFIG);
435 regs->elm_page_ctrl = elm_read_reg(info, ELM_PAGE_CTRL);
436 for (i = 0; i < ERROR_VECTOR_MAX; i++) {
437 offset = i * SYNDROME_FRAGMENT_REG_SIZE;
438 switch (bch_type) {
439 case BCH8_ECC:
440 regs->elm_syndrome_fragment_3[i] = elm_read_reg(info,
441 ELM_SYNDROME_FRAGMENT_3 + offset);
442 regs->elm_syndrome_fragment_2[i] = elm_read_reg(info,
443 ELM_SYNDROME_FRAGMENT_2 + offset);
444 case BCH4_ECC:
445 regs->elm_syndrome_fragment_1[i] = elm_read_reg(info,
446 ELM_SYNDROME_FRAGMENT_1 + offset);
447 regs->elm_syndrome_fragment_0[i] = elm_read_reg(info,
448 ELM_SYNDROME_FRAGMENT_0 + offset);
449 default:
450 return -EINVAL;
451 }
452 /* ELM SYNDROME_VALID bit in SYNDROME_FRAGMENT_6[] needs
453 * to be saved for all BCH schemes*/
454 regs->elm_syndrome_fragment_6[i] = elm_read_reg(info,
455 ELM_SYNDROME_FRAGMENT_6 + offset);
456 }
457 return 0;
458}
459
460/**
461 * elm_context_restore
462 * writes configurations saved duing power-down back into ELM registers
463 */
464static int elm_context_restore(struct elm_info *info)
465{
466 struct elm_registers *regs = &info->elm_regs;
467 enum bch_ecc bch_type = info->bch_type;
468 u32 offset = 0, i;
469
470 elm_write_reg(info, ELM_IRQENABLE, regs->elm_irqenable);
471 elm_write_reg(info, ELM_SYSCONFIG, regs->elm_sysconfig);
472 elm_write_reg(info, ELM_LOCATION_CONFIG, regs->elm_location_config);
473 elm_write_reg(info, ELM_PAGE_CTRL, regs->elm_page_ctrl);
474 for (i = 0; i < ERROR_VECTOR_MAX; i++) {
475 offset = i * SYNDROME_FRAGMENT_REG_SIZE;
476 switch (bch_type) {
477 case BCH8_ECC:
478 elm_write_reg(info, ELM_SYNDROME_FRAGMENT_3 + offset,
479 regs->elm_syndrome_fragment_3[i]);
480 elm_write_reg(info, ELM_SYNDROME_FRAGMENT_2 + offset,
481 regs->elm_syndrome_fragment_2[i]);
482 case BCH4_ECC:
483 elm_write_reg(info, ELM_SYNDROME_FRAGMENT_1 + offset,
484 regs->elm_syndrome_fragment_1[i]);
485 elm_write_reg(info, ELM_SYNDROME_FRAGMENT_0 + offset,
486 regs->elm_syndrome_fragment_0[i]);
487 default:
488 return -EINVAL;
489 }
490 /* ELM_SYNDROME_VALID bit to be set in last to trigger FSM */
491 elm_write_reg(info, ELM_SYNDROME_FRAGMENT_6 + offset,
492 regs->elm_syndrome_fragment_6[i] &
493 ELM_SYNDROME_VALID);
494 }
495 return 0;
496}
497
498static int elm_suspend(struct device *dev)
499{
500 struct elm_info *info = dev_get_drvdata(dev);
501 elm_context_save(info);
502 pm_runtime_put_sync(dev);
503 return 0;
504}
505
506static int elm_resume(struct device *dev)
507{
508 struct elm_info *info = dev_get_drvdata(dev);
509 pm_runtime_get_sync(dev);
510 elm_context_restore(info);
511 return 0;
512}
Wolfram Sang8d808952014-04-17 09:39:07 +0200513#endif
avinash philip11f164d2013-06-18 00:16:39 +0530514
515static SIMPLE_DEV_PM_OPS(elm_pm_ops, elm_suspend, elm_resume);
516
Philip Avinashbf224332013-01-04 13:26:50 +0530517#ifdef CONFIG_OF
518static const struct of_device_id elm_of_match[] = {
Philip Avinashc80e5722013-01-24 12:23:30 +0530519 { .compatible = "ti,am3352-elm" },
Philip Avinashbf224332013-01-04 13:26:50 +0530520 {},
521};
522MODULE_DEVICE_TABLE(of, elm_of_match);
523#endif
524
525static struct platform_driver elm_driver = {
526 .driver = {
Pekon Guptac7ea95a2014-03-20 18:48:37 +0530527 .name = DRIVER_NAME,
Philip Avinashbf224332013-01-04 13:26:50 +0530528 .owner = THIS_MODULE,
529 .of_match_table = of_match_ptr(elm_of_match),
avinash philip11f164d2013-06-18 00:16:39 +0530530 .pm = &elm_pm_ops,
Philip Avinashbf224332013-01-04 13:26:50 +0530531 },
532 .probe = elm_probe,
533 .remove = elm_remove,
534};
535
536module_platform_driver(elm_driver);
537
538MODULE_DESCRIPTION("ELM driver for BCH error correction");
539MODULE_AUTHOR("Texas Instruments");
540MODULE_ALIAS("platform: elm");
541MODULE_LICENSE("GPL v2");