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