]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - arch/arm/cpu/armv7/am33xx/elm.c
merged tx6dl-devel into denx master branch
[karo-tx-uboot.git] / arch / arm / cpu / armv7 / am33xx / 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 <asm/arch/cpu.h>
20 #include <asm/omap_gpmc.h>
21 #include <asm/arch/elm.h>
22
23 #define ELM_DEFAULT_POLY 0
24
25 /* make sure this variable does not end up in bss
26  * because that would corrupt the relocation section
27  * that is overlayed with the bss section
28  */
29 static struct elm *elm_cfg __attribute__((section(".data")));
30
31 /**
32  * elm_load_syndromes - Load BCH syndromes based on nibble selection
33  * @syndrome: BCH syndrome
34  * @nibbles:
35  * @poly: Syndrome Polynomial set to use
36  *
37  * Load BCH syndromes based on nibble selection
38  */
39 static void elm_load_syndromes(u8 *syndrome, u32 nibbles, u8 poly)
40 {
41         u32 val;
42         struct  syndrome *sf = &elm_cfg->syndrome_fragments[poly];
43
44         /* reg 0 */
45         val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
46                                 (syndrome[3] << 24);
47         writel(val, &sf->syndrome_fragment_x[0]);
48
49         /* reg 1 */
50         val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
51                                 (syndrome[7] << 24);
52         writel(val, &sf->syndrome_fragment_x[1]);
53
54         /* BCH 8-bit with 26 nibbles (4*8=32) */
55         if (nibbles > 13) {
56                 /* reg 2 */
57                 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
58                                 (syndrome[11] << 24);
59                 writel(val, &sf->syndrome_fragment_x[2]);
60
61                 /* reg 3 */
62                 val = syndrome[12] | (syndrome[13] << 8) | (syndrome[14] << 16) |
63                                 (syndrome[15] << 24);
64                 writel(val, &sf->syndrome_fragment_x[3]);
65         }
66
67         /* BCH 16-bit with 52 nibbles (7*8=56) */
68         if (nibbles > 26) {
69                 /* reg 4 */
70                 val = syndrome[16] | (syndrome[17] << 8) | (syndrome[18] << 16) |
71                                 (syndrome[19] << 24);
72                 writel(val, &sf->syndrome_fragment_x[4]);
73
74                 /* reg 5 */
75                 val = syndrome[20] | (syndrome[21] << 8) | (syndrome[22] << 16) |
76                                 (syndrome[23] << 24);
77                 writel(val, &sf->syndrome_fragment_x[5]);
78
79                 /* reg 6 */
80                 val = syndrome[24] | (syndrome[25] << 8) | (syndrome[26] << 16) |
81                                 (syndrome[27] << 24);
82                 writel(val, &sf->syndrome_fragment_x[6]);
83         }
84 }
85
86 /**
87  * elm_check_error - 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)));
113         /* clear status */
114         writel((readl(&elm_cfg->irqstatus) & ~(0x1 << poly)),
115                 &elm_cfg->irqstatus);
116
117         /* check if correctable */
118         location_status = readl(&elm_cfg->error_location[poly].location_status);
119         if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK))
120                 return -1;
121
122         /* get error count */
123         *error_count = readl(&elm_cfg->error_location[poly].location_status) &
124                                                 ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
125
126         for (i = 0; i < *error_count; i++)
127                 error_locations[i] =
128                         readl(&elm_cfg->error_location[poly].error_location_x[i]);
129
130         return 0;
131 }
132
133
134 /**
135  * elm_config - Configure ELM module
136  * @level: 4 / 8 / 16 bit BCH
137  *
138  * Configure ELM module based on BCH level.
139  * Set mode as continuous mode.
140  * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
141  * Also, the mode is set only for syndrome 0
142  */
143 int elm_config(enum bch_level level)
144 {
145         u32 val;
146         u8 poly = ELM_DEFAULT_POLY;
147         u32 buffer_size = 0x7FF;
148
149         /* config size and level */
150         val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
151         val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
152                                 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
153         writel(val, &elm_cfg->location_config);
154
155         /* config continous mode */
156         /* enable interrupt generation for syndrome polynomial set */
157         writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
158                         &elm_cfg->irqenable);
159         /* set continuous mode for the syndrome polynomial set */
160         writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
161                         &elm_cfg->page_ctrl);
162
163         return 0;
164 }
165
166 /**
167  * elm_reset - Do a soft reset of ELM
168  *
169  * Perform a soft reset of ELM and return after reset is done.
170  */
171 void elm_reset(void)
172 {
173         /* initiate reset */
174         writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
175                                 &elm_cfg->sysconfig);
176
177         /* wait for reset complete and normal operation */
178         while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
179                 ELM_SYSSTATUS_RESETDONE)
180                 ;
181 }
182
183 /**
184  * elm_init - Initialize ELM module
185  *
186  * Initialize ELM support. Currently it does only base address init
187  * and ELM reset.
188  */
189 void elm_init(void)
190 {
191         elm_cfg = (struct elm *)ELM_BASE;
192         elm_reset();
193 }