]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/omap_elm.c
mtd: nand: omap_elm: remove #include omap_gpmc.h
[karo-tx-uboot.git] / drivers / mtd / nand / omap_elm.c
1 /*
2  * (C) Copyright 2010-2011 Texas Instruments, <www.ti.com>
3  * Mansoor Ahamed <mansoor.ahamed@ti.com>
4  *
5  * BCH Error Location Module (ELM) support.
6  *
7  * NOTE:
8  * 1. Supports only continuous mode. Dont see need for page mode in uboot
9  * 2. Supports only syndrome polynomial 0. i.e. poly local variable is
10  *    always set to ELM_DEFAULT_POLY. Dont see need for other polynomial
11  *    sets in uboot
12  *
13  * SPDX-License-Identifier:     GPL-2.0+
14  */
15
16 #include <common.h>
17 #include <asm/io.h>
18 #include <asm/errno.h>
19 #include <linux/mtd/omap_elm.h>
20 #include <asm/arch/hardware.h>
21
22 #define ELM_DEFAULT_POLY (0)
23
24 struct elm *elm_cfg;
25
26 /**
27  * elm_load_syndromes - Load BCH syndromes based on nibble selection
28  * @syndrome: BCH syndrome
29  * @nibbles:
30  * @poly: Syndrome Polynomial set to use
31  *
32  * Load BCH syndromes based on nibble selection
33  */
34 static void elm_load_syndromes(u8 *syndrome, u32 nibbles, u8 poly)
35 {
36         u32 *ptr;
37         u32 val;
38
39         /* reg 0 */
40         ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
41         val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
42                                 (syndrome[3] << 24);
43         writel(val, ptr);
44         /* reg 1 */
45         ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
46         val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
47                                 (syndrome[7] << 24);
48         writel(val, ptr);
49
50         /* BCH 8-bit with 26 nibbles (4*8=32) */
51         if (nibbles > 13) {
52                 /* reg 2 */
53                 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
54                 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
55                                 (syndrome[11] << 24);
56                 writel(val, ptr);
57                 /* reg 3 */
58                 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
59                 val = syndrome[12] | (syndrome[13] << 8) |
60                         (syndrome[14] << 16) | (syndrome[15] << 24);
61                 writel(val, ptr);
62         }
63
64         /* BCH 16-bit with 52 nibbles (7*8=56) */
65         if (nibbles > 26) {
66                 /* reg 4 */
67                 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
68                 val = syndrome[16] | (syndrome[17] << 8) |
69                         (syndrome[18] << 16) | (syndrome[19] << 24);
70                 writel(val, ptr);
71
72                 /* reg 5 */
73                 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
74                 val = syndrome[20] | (syndrome[21] << 8) |
75                         (syndrome[22] << 16) | (syndrome[23] << 24);
76                 writel(val, ptr);
77
78                 /* reg 6 */
79                 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
80                 val = syndrome[24] | (syndrome[25] << 8) |
81                         (syndrome[26] << 16) | (syndrome[27] << 24);
82                 writel(val, ptr);
83         }
84 }
85
86 /**
87  * elm_check_errors - Check for BCH errors and return error locations
88  * @syndrome: BCH syndrome
89  * @nibbles:
90  * @error_count: Returns number of errrors in the syndrome
91  * @error_locations: Returns error locations (in decimal) in this array
92  *
93  * Check the provided syndrome for BCH errors and return error count
94  * and locations in the array passed. Returns -1 if error is not correctable,
95  * else returns 0
96  */
97 int elm_check_error(u8 *syndrome, u32 nibbles, u32 *error_count,
98                 u32 *error_locations)
99 {
100         u8 poly = ELM_DEFAULT_POLY;
101         s8 i;
102         u32 location_status;
103
104         elm_load_syndromes(syndrome, nibbles, poly);
105
106         /* start processing */
107         writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
108                                 | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
109                 &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
110
111         /* wait for processing to complete */
112         while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
113                 ;
114         /* clear status */
115         writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
116                         &elm_cfg->irqstatus);
117
118         /* check if correctable */
119         location_status = readl(&elm_cfg->error_location[poly].location_status);
120         if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK))
121                 return -1;
122
123         /* get error count */
124         *error_count = readl(&elm_cfg->error_location[poly].location_status) &
125                                         ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
126
127         for (i = 0; i < *error_count; i++) {
128                 error_locations[i] =
129                      readl(&elm_cfg->error_location[poly].error_location_x[i]);
130         }
131
132         return 0;
133 }
134
135
136 /**
137  * elm_config - Configure ELM module
138  * @level: 4 / 8 / 16 bit BCH
139  *
140  * Configure ELM module based on BCH level.
141  * Set mode as continuous mode.
142  * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
143  * Also, the mode is set only for syndrome 0
144  */
145 int elm_config(enum bch_level level)
146 {
147         u32 val;
148         u8 poly = ELM_DEFAULT_POLY;
149         u32 buffer_size = 0x7FF;
150
151         /* config size and level */
152         val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
153         val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
154                                 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
155         writel(val, &elm_cfg->location_config);
156
157         /* config continous mode */
158         /* enable interrupt generation for syndrome polynomial set */
159         writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
160                         &elm_cfg->irqenable);
161         /* set continuous mode for the syndrome polynomial set */
162         writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
163                         &elm_cfg->page_ctrl);
164
165         return 0;
166 }
167
168 /**
169  * elm_reset - Do a soft reset of ELM
170  *
171  * Perform a soft reset of ELM and return after reset is done.
172  */
173 void elm_reset(void)
174 {
175         /* initiate reset */
176         writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
177                         &elm_cfg->sysconfig);
178
179         /* wait for reset complete and normal operation */
180         while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
181                 ELM_SYSSTATUS_RESETDONE)
182                 ;
183 }
184
185 /**
186  * elm_init - Initialize ELM module
187  *
188  * Initialize ELM support. Currently it does only base address init
189  * and ELM reset.
190  */
191 void elm_init(void)
192 {
193         elm_cfg = (struct elm *)ELM_BASE;
194         elm_reset();
195 }