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