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