blob: 9eed23d75af0661e1eb6d2a79e44c1895c1a4c51 [file] [log] [blame]
Mansoor Ahamed04c37572012-11-06 13:06:32 +00001/*
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 * See file CREDITS for list of people who contributed to this
14 * project.
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License as
18 * published by the Free Software Foundation; either version 2 of
19 * the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 * MA 02111-1307 USA
30 */
31
32#include <common.h>
33#include <asm/io.h>
34#include <asm/errno.h>
35#include <asm/arch/cpu.h>
36#include <asm/arch/omap_gpmc.h>
37#include <asm/arch/elm.h>
38
39#define ELM_DEFAULT_POLY (0)
40
41struct elm *elm_cfg;
42
43/**
44 * elm_load_syndromes - Load BCH syndromes based on nibble selection
45 * @syndrome: BCH syndrome
46 * @nibbles:
47 * @poly: Syndrome Polynomial set to use
48 *
49 * Load BCH syndromes based on nibble selection
50 */
51static void elm_load_syndromes(u8 *syndrome, u32 nibbles, u8 poly)
52{
53 u32 *ptr;
54 u32 val;
55
56 /* reg 0 */
57 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[0];
58 val = syndrome[0] | (syndrome[1] << 8) | (syndrome[2] << 16) |
59 (syndrome[3] << 24);
60 writel(val, ptr);
61 /* reg 1 */
62 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[1];
63 val = syndrome[4] | (syndrome[5] << 8) | (syndrome[6] << 16) |
64 (syndrome[7] << 24);
65 writel(val, ptr);
66
67 /* BCH 8-bit with 26 nibbles (4*8=32) */
68 if (nibbles > 13) {
69 /* reg 2 */
70 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[2];
71 val = syndrome[8] | (syndrome[9] << 8) | (syndrome[10] << 16) |
72 (syndrome[11] << 24);
73 writel(val, ptr);
74 /* reg 3 */
75 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[3];
76 val = syndrome[12] | (syndrome[13] << 8) |
77 (syndrome[14] << 16) | (syndrome[15] << 24);
78 writel(val, ptr);
79 }
80
81 /* BCH 16-bit with 52 nibbles (7*8=56) */
82 if (nibbles > 26) {
83 /* reg 4 */
84 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[4];
85 val = syndrome[16] | (syndrome[17] << 8) |
86 (syndrome[18] << 16) | (syndrome[19] << 24);
87 writel(val, ptr);
88
89 /* reg 5 */
90 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[5];
91 val = syndrome[20] | (syndrome[21] << 8) |
92 (syndrome[22] << 16) | (syndrome[23] << 24);
93 writel(val, ptr);
94
95 /* reg 6 */
96 ptr = &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6];
97 val = syndrome[24] | (syndrome[25] << 8) |
98 (syndrome[26] << 16) | (syndrome[27] << 24);
99 writel(val, ptr);
100 }
101}
102
103/**
104 * elm_check_errors - Check for BCH errors and return error locations
105 * @syndrome: BCH syndrome
106 * @nibbles:
107 * @error_count: Returns number of errrors in the syndrome
108 * @error_locations: Returns error locations (in decimal) in this array
109 *
110 * Check the provided syndrome for BCH errors and return error count
111 * and locations in the array passed. Returns -1 if error is not correctable,
112 * else returns 0
113 */
114int elm_check_error(u8 *syndrome, u32 nibbles, u32 *error_count,
115 u32 *error_locations)
116{
117 u8 poly = ELM_DEFAULT_POLY;
118 s8 i;
119 u32 location_status;
120
121 elm_load_syndromes(syndrome, nibbles, poly);
122
123 /* start processing */
124 writel((readl(&elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6])
125 | ELM_SYNDROME_FRAGMENT_6_SYNDROME_VALID),
126 &elm_cfg->syndrome_fragments[poly].syndrome_fragment_x[6]);
127
128 /* wait for processing to complete */
129 while ((readl(&elm_cfg->irqstatus) & (0x1 << poly)) != 0x1)
130 ;
131 /* clear status */
132 writel((readl(&elm_cfg->irqstatus) | (0x1 << poly)),
133 &elm_cfg->irqstatus);
134
135 /* check if correctable */
136 location_status = readl(&elm_cfg->error_location[poly].location_status);
137 if (!(location_status & ELM_LOCATION_STATUS_ECC_CORRECTABLE_MASK))
138 return -1;
139
140 /* get error count */
141 *error_count = readl(&elm_cfg->error_location[poly].location_status) &
142 ELM_LOCATION_STATUS_ECC_NB_ERRORS_MASK;
143
144 for (i = 0; i < *error_count; i++) {
145 error_locations[i] =
146 readl(&elm_cfg->error_location[poly].error_location_x[i]);
147 }
148
149 return 0;
150}
151
152
153/**
154 * elm_config - Configure ELM module
155 * @level: 4 / 8 / 16 bit BCH
156 *
157 * Configure ELM module based on BCH level.
158 * Set mode as continuous mode.
159 * Currently we are using only syndrome 0 and syndromes 1 to 6 are not used.
160 * Also, the mode is set only for syndrome 0
161 */
162int elm_config(enum bch_level level)
163{
164 u32 val;
165 u8 poly = ELM_DEFAULT_POLY;
166 u32 buffer_size = 0x7FF;
167
168 /* config size and level */
169 val = (u32)(level) & ELM_LOCATION_CONFIG_ECC_BCH_LEVEL_MASK;
170 val |= ((buffer_size << ELM_LOCATION_CONFIG_ECC_SIZE_POS) &
171 ELM_LOCATION_CONFIG_ECC_SIZE_MASK);
172 writel(val, &elm_cfg->location_config);
173
174 /* config continous mode */
175 /* enable interrupt generation for syndrome polynomial set */
176 writel((readl(&elm_cfg->irqenable) | (0x1 << poly)),
177 &elm_cfg->irqenable);
178 /* set continuous mode for the syndrome polynomial set */
179 writel((readl(&elm_cfg->page_ctrl) & ~(0x1 << poly)),
180 &elm_cfg->page_ctrl);
181
182 return 0;
183}
184
185/**
186 * elm_reset - Do a soft reset of ELM
187 *
188 * Perform a soft reset of ELM and return after reset is done.
189 */
190void elm_reset(void)
191{
192 /* initiate reset */
193 writel((readl(&elm_cfg->sysconfig) | ELM_SYSCONFIG_SOFTRESET),
194 &elm_cfg->sysconfig);
195
196 /* wait for reset complete and normal operation */
197 while ((readl(&elm_cfg->sysstatus) & ELM_SYSSTATUS_RESETDONE) !=
198 ELM_SYSSTATUS_RESETDONE)
199 ;
200}
201
202/**
203 * elm_init - Initialize ELM module
204 *
205 * Initialize ELM support. Currently it does only base address init
206 * and ELM reset.
207 */
208void elm_init(void)
209{
210 elm_cfg = (struct elm *)ELM_BASE;
211 elm_reset();
212}