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