1cfcc706cSMiquel Raynal /*
2cfcc706cSMiquel Raynal * (C) Copyright 2007-2008
3cfcc706cSMiquel Raynal * Stelian Pop <stelian@popies.net>
4cfcc706cSMiquel Raynal * Lead Tech Design <www.leadtechdesign.com>
5cfcc706cSMiquel Raynal *
6cfcc706cSMiquel Raynal * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
7cfcc706cSMiquel Raynal *
8cfcc706cSMiquel Raynal * Add Programmable Multibit ECC support for various AT91 SoC
9cfcc706cSMiquel Raynal * (C) Copyright 2012 ATMEL, Hong Xu
10cfcc706cSMiquel Raynal *
11cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
12cfcc706cSMiquel Raynal */
13cfcc706cSMiquel Raynal
14cfcc706cSMiquel Raynal #include <common.h>
15cfcc706cSMiquel Raynal #include <asm/gpio.h>
16cfcc706cSMiquel Raynal #include <asm/arch/gpio.h>
17cfcc706cSMiquel Raynal
18cfcc706cSMiquel Raynal #include <malloc.h>
19cfcc706cSMiquel Raynal #include <nand.h>
20cfcc706cSMiquel Raynal #include <watchdog.h>
21cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
22cfcc706cSMiquel Raynal
23cfcc706cSMiquel Raynal #ifdef CONFIG_ATMEL_NAND_HWECC
24cfcc706cSMiquel Raynal
25cfcc706cSMiquel Raynal /* Register access macros */
26cfcc706cSMiquel Raynal #define ecc_readl(add, reg) \
27cfcc706cSMiquel Raynal readl(add + ATMEL_ECC_##reg)
28cfcc706cSMiquel Raynal #define ecc_writel(add, reg, value) \
29cfcc706cSMiquel Raynal writel((value), add + ATMEL_ECC_##reg)
30cfcc706cSMiquel Raynal
31cfcc706cSMiquel Raynal #include "atmel_nand_ecc.h" /* Hardware ECC registers */
32cfcc706cSMiquel Raynal
33cfcc706cSMiquel Raynal #ifdef CONFIG_ATMEL_NAND_HW_PMECC
34cfcc706cSMiquel Raynal
35cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_BUILD
36cfcc706cSMiquel Raynal #undef CONFIG_SYS_NAND_ONFI_DETECTION
37cfcc706cSMiquel Raynal #endif
38cfcc706cSMiquel Raynal
39cfcc706cSMiquel Raynal struct atmel_nand_host {
40cfcc706cSMiquel Raynal struct pmecc_regs __iomem *pmecc;
41cfcc706cSMiquel Raynal struct pmecc_errloc_regs __iomem *pmerrloc;
42cfcc706cSMiquel Raynal void __iomem *pmecc_rom_base;
43cfcc706cSMiquel Raynal
44cfcc706cSMiquel Raynal u8 pmecc_corr_cap;
45cfcc706cSMiquel Raynal u16 pmecc_sector_size;
46cfcc706cSMiquel Raynal u32 pmecc_index_table_offset;
47cfcc706cSMiquel Raynal u32 pmecc_version;
48cfcc706cSMiquel Raynal
49cfcc706cSMiquel Raynal int pmecc_bytes_per_sector;
50cfcc706cSMiquel Raynal int pmecc_sector_number;
51cfcc706cSMiquel Raynal int pmecc_degree; /* Degree of remainders */
52cfcc706cSMiquel Raynal int pmecc_cw_len; /* Length of codeword */
53cfcc706cSMiquel Raynal
54cfcc706cSMiquel Raynal /* lookup table for alpha_to and index_of */
55cfcc706cSMiquel Raynal void __iomem *pmecc_alpha_to;
56cfcc706cSMiquel Raynal void __iomem *pmecc_index_of;
57cfcc706cSMiquel Raynal
58cfcc706cSMiquel Raynal /* data for pmecc computation */
59cfcc706cSMiquel Raynal int16_t *pmecc_smu;
60cfcc706cSMiquel Raynal int16_t *pmecc_partial_syn;
61cfcc706cSMiquel Raynal int16_t *pmecc_si;
62cfcc706cSMiquel Raynal int16_t *pmecc_lmu; /* polynomal order */
63cfcc706cSMiquel Raynal int *pmecc_mu;
64cfcc706cSMiquel Raynal int *pmecc_dmu;
65cfcc706cSMiquel Raynal int *pmecc_delta;
66cfcc706cSMiquel Raynal };
67cfcc706cSMiquel Raynal
68cfcc706cSMiquel Raynal static struct atmel_nand_host pmecc_host;
69cfcc706cSMiquel Raynal static struct nand_ecclayout atmel_pmecc_oobinfo;
70cfcc706cSMiquel Raynal
71cfcc706cSMiquel Raynal /*
72cfcc706cSMiquel Raynal * Return number of ecc bytes per sector according to sector size and
73cfcc706cSMiquel Raynal * correction capability
74cfcc706cSMiquel Raynal *
75cfcc706cSMiquel Raynal * Following table shows what at91 PMECC supported:
76cfcc706cSMiquel Raynal * Correction Capability Sector_512_bytes Sector_1024_bytes
77cfcc706cSMiquel Raynal * ===================== ================ =================
78cfcc706cSMiquel Raynal * 2-bits 4-bytes 4-bytes
79cfcc706cSMiquel Raynal * 4-bits 7-bytes 7-bytes
80cfcc706cSMiquel Raynal * 8-bits 13-bytes 14-bytes
81cfcc706cSMiquel Raynal * 12-bits 20-bytes 21-bytes
82cfcc706cSMiquel Raynal * 24-bits 39-bytes 42-bytes
83cfcc706cSMiquel Raynal * 32-bits 52-bytes 56-bytes
84cfcc706cSMiquel Raynal */
pmecc_get_ecc_bytes(int cap,int sector_size)85cfcc706cSMiquel Raynal static int pmecc_get_ecc_bytes(int cap, int sector_size)
86cfcc706cSMiquel Raynal {
87cfcc706cSMiquel Raynal int m = 12 + sector_size / 512;
88cfcc706cSMiquel Raynal return (m * cap + 7) / 8;
89cfcc706cSMiquel Raynal }
90cfcc706cSMiquel Raynal
pmecc_config_ecc_layout(struct nand_ecclayout * layout,int oobsize,int ecc_len)91cfcc706cSMiquel Raynal static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
92cfcc706cSMiquel Raynal int oobsize, int ecc_len)
93cfcc706cSMiquel Raynal {
94cfcc706cSMiquel Raynal int i;
95cfcc706cSMiquel Raynal
96cfcc706cSMiquel Raynal layout->eccbytes = ecc_len;
97cfcc706cSMiquel Raynal
98cfcc706cSMiquel Raynal /* ECC will occupy the last ecc_len bytes continuously */
99cfcc706cSMiquel Raynal for (i = 0; i < ecc_len; i++)
100cfcc706cSMiquel Raynal layout->eccpos[i] = oobsize - ecc_len + i;
101cfcc706cSMiquel Raynal
102cfcc706cSMiquel Raynal layout->oobfree[0].offset = 2;
103cfcc706cSMiquel Raynal layout->oobfree[0].length =
104cfcc706cSMiquel Raynal oobsize - ecc_len - layout->oobfree[0].offset;
105cfcc706cSMiquel Raynal }
106cfcc706cSMiquel Raynal
pmecc_get_alpha_to(struct atmel_nand_host * host)107cfcc706cSMiquel Raynal static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
108cfcc706cSMiquel Raynal {
109cfcc706cSMiquel Raynal int table_size;
110cfcc706cSMiquel Raynal
111cfcc706cSMiquel Raynal table_size = host->pmecc_sector_size == 512 ?
112cfcc706cSMiquel Raynal PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024;
113cfcc706cSMiquel Raynal
114cfcc706cSMiquel Raynal /* the ALPHA lookup table is right behind the INDEX lookup table. */
115cfcc706cSMiquel Raynal return host->pmecc_rom_base + host->pmecc_index_table_offset +
116cfcc706cSMiquel Raynal table_size * sizeof(int16_t);
117cfcc706cSMiquel Raynal }
118cfcc706cSMiquel Raynal
pmecc_data_free(struct atmel_nand_host * host)119cfcc706cSMiquel Raynal static void pmecc_data_free(struct atmel_nand_host *host)
120cfcc706cSMiquel Raynal {
121cfcc706cSMiquel Raynal free(host->pmecc_partial_syn);
122cfcc706cSMiquel Raynal free(host->pmecc_si);
123cfcc706cSMiquel Raynal free(host->pmecc_lmu);
124cfcc706cSMiquel Raynal free(host->pmecc_smu);
125cfcc706cSMiquel Raynal free(host->pmecc_mu);
126cfcc706cSMiquel Raynal free(host->pmecc_dmu);
127cfcc706cSMiquel Raynal free(host->pmecc_delta);
128cfcc706cSMiquel Raynal }
129cfcc706cSMiquel Raynal
pmecc_data_alloc(struct atmel_nand_host * host)130cfcc706cSMiquel Raynal static int pmecc_data_alloc(struct atmel_nand_host *host)
131cfcc706cSMiquel Raynal {
132cfcc706cSMiquel Raynal const int cap = host->pmecc_corr_cap;
133cfcc706cSMiquel Raynal int size;
134cfcc706cSMiquel Raynal
135cfcc706cSMiquel Raynal size = (2 * cap + 1) * sizeof(int16_t);
136cfcc706cSMiquel Raynal host->pmecc_partial_syn = malloc(size);
137cfcc706cSMiquel Raynal host->pmecc_si = malloc(size);
138cfcc706cSMiquel Raynal host->pmecc_lmu = malloc((cap + 1) * sizeof(int16_t));
139cfcc706cSMiquel Raynal host->pmecc_smu = malloc((cap + 2) * size);
140cfcc706cSMiquel Raynal
141cfcc706cSMiquel Raynal size = (cap + 1) * sizeof(int);
142cfcc706cSMiquel Raynal host->pmecc_mu = malloc(size);
143cfcc706cSMiquel Raynal host->pmecc_dmu = malloc(size);
144cfcc706cSMiquel Raynal host->pmecc_delta = malloc(size);
145cfcc706cSMiquel Raynal
146cfcc706cSMiquel Raynal if (host->pmecc_partial_syn &&
147cfcc706cSMiquel Raynal host->pmecc_si &&
148cfcc706cSMiquel Raynal host->pmecc_lmu &&
149cfcc706cSMiquel Raynal host->pmecc_smu &&
150cfcc706cSMiquel Raynal host->pmecc_mu &&
151cfcc706cSMiquel Raynal host->pmecc_dmu &&
152cfcc706cSMiquel Raynal host->pmecc_delta)
153cfcc706cSMiquel Raynal return 0;
154cfcc706cSMiquel Raynal
155cfcc706cSMiquel Raynal /* error happened */
156cfcc706cSMiquel Raynal pmecc_data_free(host);
157cfcc706cSMiquel Raynal return -ENOMEM;
158cfcc706cSMiquel Raynal
159cfcc706cSMiquel Raynal }
160cfcc706cSMiquel Raynal
pmecc_gen_syndrome(struct mtd_info * mtd,int sector)161cfcc706cSMiquel Raynal static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
162cfcc706cSMiquel Raynal {
163cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
164cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
165cfcc706cSMiquel Raynal int i;
166cfcc706cSMiquel Raynal uint32_t value;
167cfcc706cSMiquel Raynal
168cfcc706cSMiquel Raynal /* Fill odd syndromes */
169cfcc706cSMiquel Raynal for (i = 0; i < host->pmecc_corr_cap; i++) {
170cfcc706cSMiquel Raynal value = pmecc_readl(host->pmecc, rem_port[sector].rem[i / 2]);
171cfcc706cSMiquel Raynal if (i & 1)
172cfcc706cSMiquel Raynal value >>= 16;
173cfcc706cSMiquel Raynal value &= 0xffff;
174cfcc706cSMiquel Raynal host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
175cfcc706cSMiquel Raynal }
176cfcc706cSMiquel Raynal }
177cfcc706cSMiquel Raynal
pmecc_substitute(struct mtd_info * mtd)178cfcc706cSMiquel Raynal static void pmecc_substitute(struct mtd_info *mtd)
179cfcc706cSMiquel Raynal {
180cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
181cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
182cfcc706cSMiquel Raynal int16_t __iomem *alpha_to = host->pmecc_alpha_to;
183cfcc706cSMiquel Raynal int16_t __iomem *index_of = host->pmecc_index_of;
184cfcc706cSMiquel Raynal int16_t *partial_syn = host->pmecc_partial_syn;
185cfcc706cSMiquel Raynal const int cap = host->pmecc_corr_cap;
186cfcc706cSMiquel Raynal int16_t *si;
187cfcc706cSMiquel Raynal int i, j;
188cfcc706cSMiquel Raynal
189cfcc706cSMiquel Raynal /* si[] is a table that holds the current syndrome value,
190cfcc706cSMiquel Raynal * an element of that table belongs to the field
191cfcc706cSMiquel Raynal */
192cfcc706cSMiquel Raynal si = host->pmecc_si;
193cfcc706cSMiquel Raynal
194cfcc706cSMiquel Raynal memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
195cfcc706cSMiquel Raynal
196cfcc706cSMiquel Raynal /* Computation 2t syndromes based on S(x) */
197cfcc706cSMiquel Raynal /* Odd syndromes */
198cfcc706cSMiquel Raynal for (i = 1; i < 2 * cap; i += 2) {
199cfcc706cSMiquel Raynal for (j = 0; j < host->pmecc_degree; j++) {
200cfcc706cSMiquel Raynal if (partial_syn[i] & (0x1 << j))
201cfcc706cSMiquel Raynal si[i] = readw(alpha_to + i * j) ^ si[i];
202cfcc706cSMiquel Raynal }
203cfcc706cSMiquel Raynal }
204cfcc706cSMiquel Raynal /* Even syndrome = (Odd syndrome) ** 2 */
205cfcc706cSMiquel Raynal for (i = 2, j = 1; j <= cap; i = ++j << 1) {
206cfcc706cSMiquel Raynal if (si[j] == 0) {
207cfcc706cSMiquel Raynal si[i] = 0;
208cfcc706cSMiquel Raynal } else {
209cfcc706cSMiquel Raynal int16_t tmp;
210cfcc706cSMiquel Raynal
211cfcc706cSMiquel Raynal tmp = readw(index_of + si[j]);
212cfcc706cSMiquel Raynal tmp = (tmp * 2) % host->pmecc_cw_len;
213cfcc706cSMiquel Raynal si[i] = readw(alpha_to + tmp);
214cfcc706cSMiquel Raynal }
215cfcc706cSMiquel Raynal }
216cfcc706cSMiquel Raynal }
217cfcc706cSMiquel Raynal
218cfcc706cSMiquel Raynal /*
219cfcc706cSMiquel Raynal * This function defines a Berlekamp iterative procedure for
220cfcc706cSMiquel Raynal * finding the value of the error location polynomial.
221cfcc706cSMiquel Raynal * The input is si[], initialize by pmecc_substitute().
222cfcc706cSMiquel Raynal * The output is smu[][].
223cfcc706cSMiquel Raynal *
224cfcc706cSMiquel Raynal * This function is written according to chip datasheet Chapter:
225cfcc706cSMiquel Raynal * Find the Error Location Polynomial Sigma(x) of Section:
226cfcc706cSMiquel Raynal * Programmable Multibit ECC Control (PMECC).
227cfcc706cSMiquel Raynal */
pmecc_get_sigma(struct mtd_info * mtd)228cfcc706cSMiquel Raynal static void pmecc_get_sigma(struct mtd_info *mtd)
229cfcc706cSMiquel Raynal {
230cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
231cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
232cfcc706cSMiquel Raynal
233cfcc706cSMiquel Raynal int16_t *lmu = host->pmecc_lmu;
234cfcc706cSMiquel Raynal int16_t *si = host->pmecc_si;
235cfcc706cSMiquel Raynal int *mu = host->pmecc_mu;
236cfcc706cSMiquel Raynal int *dmu = host->pmecc_dmu; /* Discrepancy */
237cfcc706cSMiquel Raynal int *delta = host->pmecc_delta; /* Delta order */
238cfcc706cSMiquel Raynal int cw_len = host->pmecc_cw_len;
239cfcc706cSMiquel Raynal const int16_t cap = host->pmecc_corr_cap;
240cfcc706cSMiquel Raynal const int num = 2 * cap + 1;
241cfcc706cSMiquel Raynal int16_t __iomem *index_of = host->pmecc_index_of;
242cfcc706cSMiquel Raynal int16_t __iomem *alpha_to = host->pmecc_alpha_to;
243cfcc706cSMiquel Raynal int i, j, k;
244cfcc706cSMiquel Raynal uint32_t dmu_0_count, tmp;
245cfcc706cSMiquel Raynal int16_t *smu = host->pmecc_smu;
246cfcc706cSMiquel Raynal
247cfcc706cSMiquel Raynal /* index of largest delta */
248cfcc706cSMiquel Raynal int ro;
249cfcc706cSMiquel Raynal int largest;
250cfcc706cSMiquel Raynal int diff;
251cfcc706cSMiquel Raynal
252cfcc706cSMiquel Raynal /* Init the Sigma(x) */
253*adf2f01fSBin Meng memset(smu, 0, sizeof(int16_t) * num * (cap + 2));
254cfcc706cSMiquel Raynal
255cfcc706cSMiquel Raynal dmu_0_count = 0;
256cfcc706cSMiquel Raynal
257cfcc706cSMiquel Raynal /* First Row */
258cfcc706cSMiquel Raynal
259cfcc706cSMiquel Raynal /* Mu */
260cfcc706cSMiquel Raynal mu[0] = -1;
261cfcc706cSMiquel Raynal
262cfcc706cSMiquel Raynal smu[0] = 1;
263cfcc706cSMiquel Raynal
264cfcc706cSMiquel Raynal /* discrepancy set to 1 */
265cfcc706cSMiquel Raynal dmu[0] = 1;
266cfcc706cSMiquel Raynal /* polynom order set to 0 */
267cfcc706cSMiquel Raynal lmu[0] = 0;
268cfcc706cSMiquel Raynal /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */
269cfcc706cSMiquel Raynal delta[0] = -1;
270cfcc706cSMiquel Raynal
271cfcc706cSMiquel Raynal /* Second Row */
272cfcc706cSMiquel Raynal
273cfcc706cSMiquel Raynal /* Mu */
274cfcc706cSMiquel Raynal mu[1] = 0;
275cfcc706cSMiquel Raynal /* Sigma(x) set to 1 */
276cfcc706cSMiquel Raynal smu[num] = 1;
277cfcc706cSMiquel Raynal
278cfcc706cSMiquel Raynal /* discrepancy set to S1 */
279cfcc706cSMiquel Raynal dmu[1] = si[1];
280cfcc706cSMiquel Raynal
281cfcc706cSMiquel Raynal /* polynom order set to 0 */
282cfcc706cSMiquel Raynal lmu[1] = 0;
283cfcc706cSMiquel Raynal
284cfcc706cSMiquel Raynal /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */
285cfcc706cSMiquel Raynal delta[1] = 0;
286cfcc706cSMiquel Raynal
287cfcc706cSMiquel Raynal for (i = 1; i <= cap; i++) {
288cfcc706cSMiquel Raynal mu[i + 1] = i << 1;
289cfcc706cSMiquel Raynal /* Begin Computing Sigma (Mu+1) and L(mu) */
290cfcc706cSMiquel Raynal /* check if discrepancy is set to 0 */
291cfcc706cSMiquel Raynal if (dmu[i] == 0) {
292cfcc706cSMiquel Raynal dmu_0_count++;
293cfcc706cSMiquel Raynal
294cfcc706cSMiquel Raynal tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
295cfcc706cSMiquel Raynal if ((cap - (lmu[i] >> 1) - 1) & 0x1)
296cfcc706cSMiquel Raynal tmp += 2;
297cfcc706cSMiquel Raynal else
298cfcc706cSMiquel Raynal tmp += 1;
299cfcc706cSMiquel Raynal
300cfcc706cSMiquel Raynal if (dmu_0_count == tmp) {
301cfcc706cSMiquel Raynal for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
302cfcc706cSMiquel Raynal smu[(cap + 1) * num + j] =
303cfcc706cSMiquel Raynal smu[i * num + j];
304cfcc706cSMiquel Raynal
305cfcc706cSMiquel Raynal lmu[cap + 1] = lmu[i];
306cfcc706cSMiquel Raynal return;
307cfcc706cSMiquel Raynal }
308cfcc706cSMiquel Raynal
309cfcc706cSMiquel Raynal /* copy polynom */
310cfcc706cSMiquel Raynal for (j = 0; j <= lmu[i] >> 1; j++)
311cfcc706cSMiquel Raynal smu[(i + 1) * num + j] = smu[i * num + j];
312cfcc706cSMiquel Raynal
313cfcc706cSMiquel Raynal /* copy previous polynom order to the next */
314cfcc706cSMiquel Raynal lmu[i + 1] = lmu[i];
315cfcc706cSMiquel Raynal } else {
316cfcc706cSMiquel Raynal ro = 0;
317cfcc706cSMiquel Raynal largest = -1;
318cfcc706cSMiquel Raynal /* find largest delta with dmu != 0 */
319cfcc706cSMiquel Raynal for (j = 0; j < i; j++) {
320cfcc706cSMiquel Raynal if ((dmu[j]) && (delta[j] > largest)) {
321cfcc706cSMiquel Raynal largest = delta[j];
322cfcc706cSMiquel Raynal ro = j;
323cfcc706cSMiquel Raynal }
324cfcc706cSMiquel Raynal }
325cfcc706cSMiquel Raynal
326cfcc706cSMiquel Raynal /* compute difference */
327cfcc706cSMiquel Raynal diff = (mu[i] - mu[ro]);
328cfcc706cSMiquel Raynal
329cfcc706cSMiquel Raynal /* Compute degree of the new smu polynomial */
330cfcc706cSMiquel Raynal if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
331cfcc706cSMiquel Raynal lmu[i + 1] = lmu[i];
332cfcc706cSMiquel Raynal else
333cfcc706cSMiquel Raynal lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
334cfcc706cSMiquel Raynal
335cfcc706cSMiquel Raynal /* Init smu[i+1] with 0 */
336cfcc706cSMiquel Raynal for (k = 0; k < num; k++)
337cfcc706cSMiquel Raynal smu[(i + 1) * num + k] = 0;
338cfcc706cSMiquel Raynal
339cfcc706cSMiquel Raynal /* Compute smu[i+1] */
340cfcc706cSMiquel Raynal for (k = 0; k <= lmu[ro] >> 1; k++) {
341cfcc706cSMiquel Raynal int16_t a, b, c;
342cfcc706cSMiquel Raynal
343cfcc706cSMiquel Raynal if (!(smu[ro * num + k] && dmu[i]))
344cfcc706cSMiquel Raynal continue;
345cfcc706cSMiquel Raynal a = readw(index_of + dmu[i]);
346cfcc706cSMiquel Raynal b = readw(index_of + dmu[ro]);
347cfcc706cSMiquel Raynal c = readw(index_of + smu[ro * num + k]);
348cfcc706cSMiquel Raynal tmp = a + (cw_len - b) + c;
349cfcc706cSMiquel Raynal a = readw(alpha_to + tmp % cw_len);
350cfcc706cSMiquel Raynal smu[(i + 1) * num + (k + diff)] = a;
351cfcc706cSMiquel Raynal }
352cfcc706cSMiquel Raynal
353cfcc706cSMiquel Raynal for (k = 0; k <= lmu[i] >> 1; k++)
354cfcc706cSMiquel Raynal smu[(i + 1) * num + k] ^= smu[i * num + k];
355cfcc706cSMiquel Raynal }
356cfcc706cSMiquel Raynal
357cfcc706cSMiquel Raynal /* End Computing Sigma (Mu+1) and L(mu) */
358cfcc706cSMiquel Raynal /* In either case compute delta */
359cfcc706cSMiquel Raynal delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
360cfcc706cSMiquel Raynal
361cfcc706cSMiquel Raynal /* Do not compute discrepancy for the last iteration */
362cfcc706cSMiquel Raynal if (i >= cap)
363cfcc706cSMiquel Raynal continue;
364cfcc706cSMiquel Raynal
365cfcc706cSMiquel Raynal for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
366cfcc706cSMiquel Raynal tmp = 2 * (i - 1);
367cfcc706cSMiquel Raynal if (k == 0) {
368cfcc706cSMiquel Raynal dmu[i + 1] = si[tmp + 3];
369cfcc706cSMiquel Raynal } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
370cfcc706cSMiquel Raynal int16_t a, b, c;
371cfcc706cSMiquel Raynal a = readw(index_of +
372cfcc706cSMiquel Raynal smu[(i + 1) * num + k]);
373cfcc706cSMiquel Raynal b = si[2 * (i - 1) + 3 - k];
374cfcc706cSMiquel Raynal c = readw(index_of + b);
375cfcc706cSMiquel Raynal tmp = a + c;
376cfcc706cSMiquel Raynal tmp %= cw_len;
377cfcc706cSMiquel Raynal dmu[i + 1] = readw(alpha_to + tmp) ^
378cfcc706cSMiquel Raynal dmu[i + 1];
379cfcc706cSMiquel Raynal }
380cfcc706cSMiquel Raynal }
381cfcc706cSMiquel Raynal }
382cfcc706cSMiquel Raynal }
383cfcc706cSMiquel Raynal
pmecc_err_location(struct mtd_info * mtd)384cfcc706cSMiquel Raynal static int pmecc_err_location(struct mtd_info *mtd)
385cfcc706cSMiquel Raynal {
386cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
387cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
388cfcc706cSMiquel Raynal const int cap = host->pmecc_corr_cap;
389cfcc706cSMiquel Raynal const int num = 2 * cap + 1;
390cfcc706cSMiquel Raynal int sector_size = host->pmecc_sector_size;
391cfcc706cSMiquel Raynal int err_nbr = 0; /* number of error */
392cfcc706cSMiquel Raynal int roots_nbr; /* number of roots */
393cfcc706cSMiquel Raynal int i;
394cfcc706cSMiquel Raynal uint32_t val;
395cfcc706cSMiquel Raynal int16_t *smu = host->pmecc_smu;
396cfcc706cSMiquel Raynal int timeout = PMECC_MAX_TIMEOUT_US;
397cfcc706cSMiquel Raynal
398cfcc706cSMiquel Raynal pmecc_writel(host->pmerrloc, eldis, PMERRLOC_DISABLE);
399cfcc706cSMiquel Raynal
400cfcc706cSMiquel Raynal for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
401cfcc706cSMiquel Raynal pmecc_writel(host->pmerrloc, sigma[i],
402cfcc706cSMiquel Raynal smu[(cap + 1) * num + i]);
403cfcc706cSMiquel Raynal err_nbr++;
404cfcc706cSMiquel Raynal }
405cfcc706cSMiquel Raynal
406cfcc706cSMiquel Raynal val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1);
407cfcc706cSMiquel Raynal if (sector_size == 1024)
408cfcc706cSMiquel Raynal val |= PMERRLOC_ELCFG_SECTOR_1024;
409cfcc706cSMiquel Raynal
410cfcc706cSMiquel Raynal pmecc_writel(host->pmerrloc, elcfg, val);
411cfcc706cSMiquel Raynal pmecc_writel(host->pmerrloc, elen,
412cfcc706cSMiquel Raynal sector_size * 8 + host->pmecc_degree * cap);
413cfcc706cSMiquel Raynal
414cfcc706cSMiquel Raynal while (--timeout) {
415cfcc706cSMiquel Raynal if (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_CALC_DONE)
416cfcc706cSMiquel Raynal break;
417cfcc706cSMiquel Raynal WATCHDOG_RESET();
418cfcc706cSMiquel Raynal udelay(1);
419cfcc706cSMiquel Raynal }
420cfcc706cSMiquel Raynal
421cfcc706cSMiquel Raynal if (!timeout) {
422cfcc706cSMiquel Raynal dev_err(host->dev, "atmel_nand : Timeout to calculate PMECC error location\n");
423cfcc706cSMiquel Raynal return -1;
424cfcc706cSMiquel Raynal }
425cfcc706cSMiquel Raynal
426cfcc706cSMiquel Raynal roots_nbr = (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_ERR_NUM_MASK)
427cfcc706cSMiquel Raynal >> 8;
428cfcc706cSMiquel Raynal /* Number of roots == degree of smu hence <= cap */
429cfcc706cSMiquel Raynal if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
430cfcc706cSMiquel Raynal return err_nbr - 1;
431cfcc706cSMiquel Raynal
432cfcc706cSMiquel Raynal /* Number of roots does not match the degree of smu
433cfcc706cSMiquel Raynal * unable to correct error */
434cfcc706cSMiquel Raynal return -1;
435cfcc706cSMiquel Raynal }
436cfcc706cSMiquel Raynal
pmecc_correct_data(struct mtd_info * mtd,uint8_t * buf,uint8_t * ecc,int sector_num,int extra_bytes,int err_nbr)437cfcc706cSMiquel Raynal static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
438cfcc706cSMiquel Raynal int sector_num, int extra_bytes, int err_nbr)
439cfcc706cSMiquel Raynal {
440cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
441cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
442cfcc706cSMiquel Raynal int i = 0;
443cfcc706cSMiquel Raynal int byte_pos, bit_pos, sector_size, pos;
444cfcc706cSMiquel Raynal uint32_t tmp;
445cfcc706cSMiquel Raynal uint8_t err_byte;
446cfcc706cSMiquel Raynal
447cfcc706cSMiquel Raynal sector_size = host->pmecc_sector_size;
448cfcc706cSMiquel Raynal
449cfcc706cSMiquel Raynal while (err_nbr) {
450cfcc706cSMiquel Raynal tmp = pmecc_readl(host->pmerrloc, el[i]) - 1;
451cfcc706cSMiquel Raynal byte_pos = tmp / 8;
452cfcc706cSMiquel Raynal bit_pos = tmp % 8;
453cfcc706cSMiquel Raynal
454cfcc706cSMiquel Raynal if (byte_pos >= (sector_size + extra_bytes))
455cfcc706cSMiquel Raynal BUG(); /* should never happen */
456cfcc706cSMiquel Raynal
457cfcc706cSMiquel Raynal if (byte_pos < sector_size) {
458cfcc706cSMiquel Raynal err_byte = *(buf + byte_pos);
459cfcc706cSMiquel Raynal *(buf + byte_pos) ^= (1 << bit_pos);
460cfcc706cSMiquel Raynal
461cfcc706cSMiquel Raynal pos = sector_num * host->pmecc_sector_size + byte_pos;
462cfcc706cSMiquel Raynal dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
463cfcc706cSMiquel Raynal pos, bit_pos, err_byte, *(buf + byte_pos));
464cfcc706cSMiquel Raynal } else {
465cfcc706cSMiquel Raynal /* Bit flip in OOB area */
466cfcc706cSMiquel Raynal tmp = sector_num * host->pmecc_bytes_per_sector
467cfcc706cSMiquel Raynal + (byte_pos - sector_size);
468cfcc706cSMiquel Raynal err_byte = ecc[tmp];
469cfcc706cSMiquel Raynal ecc[tmp] ^= (1 << bit_pos);
470cfcc706cSMiquel Raynal
471cfcc706cSMiquel Raynal pos = tmp + nand_chip->ecc.layout->eccpos[0];
472cfcc706cSMiquel Raynal dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
473cfcc706cSMiquel Raynal pos, bit_pos, err_byte, ecc[tmp]);
474cfcc706cSMiquel Raynal }
475cfcc706cSMiquel Raynal
476cfcc706cSMiquel Raynal i++;
477cfcc706cSMiquel Raynal err_nbr--;
478cfcc706cSMiquel Raynal }
479cfcc706cSMiquel Raynal
480cfcc706cSMiquel Raynal return;
481cfcc706cSMiquel Raynal }
482cfcc706cSMiquel Raynal
pmecc_correction(struct mtd_info * mtd,u32 pmecc_stat,uint8_t * buf,u8 * ecc)483cfcc706cSMiquel Raynal static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
484cfcc706cSMiquel Raynal u8 *ecc)
485cfcc706cSMiquel Raynal {
486cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
487cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
488cfcc706cSMiquel Raynal int i, err_nbr, eccbytes;
489cfcc706cSMiquel Raynal uint8_t *buf_pos;
490cfcc706cSMiquel Raynal
491cfcc706cSMiquel Raynal /* SAMA5D4 PMECC IP can correct errors for all 0xff page */
492cfcc706cSMiquel Raynal if (host->pmecc_version >= PMECC_VERSION_SAMA5D4)
493cfcc706cSMiquel Raynal goto normal_check;
494cfcc706cSMiquel Raynal
495cfcc706cSMiquel Raynal eccbytes = nand_chip->ecc.bytes;
496cfcc706cSMiquel Raynal for (i = 0; i < eccbytes; i++)
497cfcc706cSMiquel Raynal if (ecc[i] != 0xff)
498cfcc706cSMiquel Raynal goto normal_check;
499cfcc706cSMiquel Raynal /* Erased page, return OK */
500cfcc706cSMiquel Raynal return 0;
501cfcc706cSMiquel Raynal
502cfcc706cSMiquel Raynal normal_check:
503cfcc706cSMiquel Raynal for (i = 0; i < host->pmecc_sector_number; i++) {
504cfcc706cSMiquel Raynal err_nbr = 0;
505cfcc706cSMiquel Raynal if (pmecc_stat & 0x1) {
506cfcc706cSMiquel Raynal buf_pos = buf + i * host->pmecc_sector_size;
507cfcc706cSMiquel Raynal
508cfcc706cSMiquel Raynal pmecc_gen_syndrome(mtd, i);
509cfcc706cSMiquel Raynal pmecc_substitute(mtd);
510cfcc706cSMiquel Raynal pmecc_get_sigma(mtd);
511cfcc706cSMiquel Raynal
512cfcc706cSMiquel Raynal err_nbr = pmecc_err_location(mtd);
513cfcc706cSMiquel Raynal if (err_nbr == -1) {
514cfcc706cSMiquel Raynal dev_err(host->dev, "PMECC: Too many errors\n");
515cfcc706cSMiquel Raynal mtd->ecc_stats.failed++;
516cfcc706cSMiquel Raynal return -EBADMSG;
517cfcc706cSMiquel Raynal } else {
518cfcc706cSMiquel Raynal pmecc_correct_data(mtd, buf_pos, ecc, i,
519cfcc706cSMiquel Raynal host->pmecc_bytes_per_sector, err_nbr);
520cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += err_nbr;
521cfcc706cSMiquel Raynal }
522cfcc706cSMiquel Raynal }
523cfcc706cSMiquel Raynal pmecc_stat >>= 1;
524cfcc706cSMiquel Raynal }
525cfcc706cSMiquel Raynal
526cfcc706cSMiquel Raynal return 0;
527cfcc706cSMiquel Raynal }
528cfcc706cSMiquel Raynal
atmel_nand_pmecc_read_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)529cfcc706cSMiquel Raynal static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
530cfcc706cSMiquel Raynal struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
531cfcc706cSMiquel Raynal {
532cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(chip);
533cfcc706cSMiquel Raynal int eccsize = chip->ecc.size;
534cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi;
535cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos;
536cfcc706cSMiquel Raynal uint32_t stat;
537cfcc706cSMiquel Raynal int timeout = PMECC_MAX_TIMEOUT_US;
538cfcc706cSMiquel Raynal
539cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
540cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
541cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg))
542cfcc706cSMiquel Raynal & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
543cfcc706cSMiquel Raynal
544cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
545cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
546cfcc706cSMiquel Raynal
547cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, eccsize);
548cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, mtd->oobsize);
549cfcc706cSMiquel Raynal
550cfcc706cSMiquel Raynal while (--timeout) {
551cfcc706cSMiquel Raynal if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
552cfcc706cSMiquel Raynal break;
553cfcc706cSMiquel Raynal WATCHDOG_RESET();
554cfcc706cSMiquel Raynal udelay(1);
555cfcc706cSMiquel Raynal }
556cfcc706cSMiquel Raynal
557cfcc706cSMiquel Raynal if (!timeout) {
558cfcc706cSMiquel Raynal dev_err(host->dev, "atmel_nand : Timeout to read PMECC page\n");
559cfcc706cSMiquel Raynal return -1;
560cfcc706cSMiquel Raynal }
561cfcc706cSMiquel Raynal
562cfcc706cSMiquel Raynal stat = pmecc_readl(host->pmecc, isr);
563cfcc706cSMiquel Raynal if (stat != 0)
564cfcc706cSMiquel Raynal if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
565cfcc706cSMiquel Raynal return -EBADMSG;
566cfcc706cSMiquel Raynal
567cfcc706cSMiquel Raynal return 0;
568cfcc706cSMiquel Raynal }
569cfcc706cSMiquel Raynal
atmel_nand_pmecc_write_page(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)570cfcc706cSMiquel Raynal static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
571cfcc706cSMiquel Raynal struct nand_chip *chip, const uint8_t *buf,
572cfcc706cSMiquel Raynal int oob_required, int page)
573cfcc706cSMiquel Raynal {
574cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(chip);
575cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos;
576cfcc706cSMiquel Raynal int i, j;
577cfcc706cSMiquel Raynal int timeout = PMECC_MAX_TIMEOUT_US;
578cfcc706cSMiquel Raynal
579cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
580cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
581cfcc706cSMiquel Raynal
582cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
583cfcc706cSMiquel Raynal PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
584cfcc706cSMiquel Raynal
585cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
586cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
587cfcc706cSMiquel Raynal
588cfcc706cSMiquel Raynal chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
589cfcc706cSMiquel Raynal
590cfcc706cSMiquel Raynal while (--timeout) {
591cfcc706cSMiquel Raynal if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
592cfcc706cSMiquel Raynal break;
593cfcc706cSMiquel Raynal WATCHDOG_RESET();
594cfcc706cSMiquel Raynal udelay(1);
595cfcc706cSMiquel Raynal }
596cfcc706cSMiquel Raynal
597cfcc706cSMiquel Raynal if (!timeout) {
598cfcc706cSMiquel Raynal dev_err(host->dev, "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
599cfcc706cSMiquel Raynal goto out;
600cfcc706cSMiquel Raynal }
601cfcc706cSMiquel Raynal
602cfcc706cSMiquel Raynal for (i = 0; i < host->pmecc_sector_number; i++) {
603cfcc706cSMiquel Raynal for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
604cfcc706cSMiquel Raynal int pos;
605cfcc706cSMiquel Raynal
606cfcc706cSMiquel Raynal pos = i * host->pmecc_bytes_per_sector + j;
607cfcc706cSMiquel Raynal chip->oob_poi[eccpos[pos]] =
608cfcc706cSMiquel Raynal pmecc_readb(host->pmecc, ecc_port[i].ecc[j]);
609cfcc706cSMiquel Raynal }
610cfcc706cSMiquel Raynal }
611cfcc706cSMiquel Raynal chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
612cfcc706cSMiquel Raynal out:
613cfcc706cSMiquel Raynal return 0;
614cfcc706cSMiquel Raynal }
615cfcc706cSMiquel Raynal
atmel_pmecc_core_init(struct mtd_info * mtd)616cfcc706cSMiquel Raynal static void atmel_pmecc_core_init(struct mtd_info *mtd)
617cfcc706cSMiquel Raynal {
618cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
619cfcc706cSMiquel Raynal struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
620cfcc706cSMiquel Raynal uint32_t val = 0;
621cfcc706cSMiquel Raynal struct nand_ecclayout *ecc_layout;
622cfcc706cSMiquel Raynal
623cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
624cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
625cfcc706cSMiquel Raynal
626cfcc706cSMiquel Raynal switch (host->pmecc_corr_cap) {
627cfcc706cSMiquel Raynal case 2:
628cfcc706cSMiquel Raynal val = PMECC_CFG_BCH_ERR2;
629cfcc706cSMiquel Raynal break;
630cfcc706cSMiquel Raynal case 4:
631cfcc706cSMiquel Raynal val = PMECC_CFG_BCH_ERR4;
632cfcc706cSMiquel Raynal break;
633cfcc706cSMiquel Raynal case 8:
634cfcc706cSMiquel Raynal val = PMECC_CFG_BCH_ERR8;
635cfcc706cSMiquel Raynal break;
636cfcc706cSMiquel Raynal case 12:
637cfcc706cSMiquel Raynal val = PMECC_CFG_BCH_ERR12;
638cfcc706cSMiquel Raynal break;
639cfcc706cSMiquel Raynal case 24:
640cfcc706cSMiquel Raynal val = PMECC_CFG_BCH_ERR24;
641cfcc706cSMiquel Raynal break;
642cfcc706cSMiquel Raynal case 32:
643cfcc706cSMiquel Raynal val = PMECC_CFG_BCH_ERR32;
644cfcc706cSMiquel Raynal break;
645cfcc706cSMiquel Raynal }
646cfcc706cSMiquel Raynal
647cfcc706cSMiquel Raynal if (host->pmecc_sector_size == 512)
648cfcc706cSMiquel Raynal val |= PMECC_CFG_SECTOR512;
649cfcc706cSMiquel Raynal else if (host->pmecc_sector_size == 1024)
650cfcc706cSMiquel Raynal val |= PMECC_CFG_SECTOR1024;
651cfcc706cSMiquel Raynal
652cfcc706cSMiquel Raynal switch (host->pmecc_sector_number) {
653cfcc706cSMiquel Raynal case 1:
654cfcc706cSMiquel Raynal val |= PMECC_CFG_PAGE_1SECTOR;
655cfcc706cSMiquel Raynal break;
656cfcc706cSMiquel Raynal case 2:
657cfcc706cSMiquel Raynal val |= PMECC_CFG_PAGE_2SECTORS;
658cfcc706cSMiquel Raynal break;
659cfcc706cSMiquel Raynal case 4:
660cfcc706cSMiquel Raynal val |= PMECC_CFG_PAGE_4SECTORS;
661cfcc706cSMiquel Raynal break;
662cfcc706cSMiquel Raynal case 8:
663cfcc706cSMiquel Raynal val |= PMECC_CFG_PAGE_8SECTORS;
664cfcc706cSMiquel Raynal break;
665cfcc706cSMiquel Raynal }
666cfcc706cSMiquel Raynal
667cfcc706cSMiquel Raynal val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
668cfcc706cSMiquel Raynal | PMECC_CFG_AUTO_DISABLE);
669cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, cfg, val);
670cfcc706cSMiquel Raynal
671cfcc706cSMiquel Raynal ecc_layout = nand_chip->ecc.layout;
672cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
673cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
674cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, eaddr,
675cfcc706cSMiquel Raynal ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
676cfcc706cSMiquel Raynal /* See datasheet about PMECC Clock Control Register */
677cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
678cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, idr, 0xff);
679cfcc706cSMiquel Raynal pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
680cfcc706cSMiquel Raynal }
681cfcc706cSMiquel Raynal
682cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
683cfcc706cSMiquel Raynal /*
684cfcc706cSMiquel Raynal * pmecc_choose_ecc - Get ecc requirement from ONFI parameters. If
685cfcc706cSMiquel Raynal * pmecc_corr_cap or pmecc_sector_size is 0, then set it as
686cfcc706cSMiquel Raynal * ONFI ECC parameters.
687cfcc706cSMiquel Raynal * @host: point to an atmel_nand_host structure.
688cfcc706cSMiquel Raynal * if host->pmecc_corr_cap is 0 then set it as the ONFI ecc_bits.
689cfcc706cSMiquel Raynal * if host->pmecc_sector_size is 0 then set it as the ONFI sector_size.
690cfcc706cSMiquel Raynal * @chip: point to an nand_chip structure.
691cfcc706cSMiquel Raynal * @cap: store the ONFI ECC correct bits capbility
692cfcc706cSMiquel Raynal * @sector_size: in how many bytes that ONFI require to correct @ecc_bits
693cfcc706cSMiquel Raynal *
694cfcc706cSMiquel Raynal * Return 0 if success. otherwise return the error code.
695cfcc706cSMiquel Raynal */
pmecc_choose_ecc(struct atmel_nand_host * host,struct nand_chip * chip,int * cap,int * sector_size)696cfcc706cSMiquel Raynal static int pmecc_choose_ecc(struct atmel_nand_host *host,
697cfcc706cSMiquel Raynal struct nand_chip *chip,
698cfcc706cSMiquel Raynal int *cap, int *sector_size)
699cfcc706cSMiquel Raynal {
700cfcc706cSMiquel Raynal /* Get ECC requirement from ONFI parameters */
701cfcc706cSMiquel Raynal *cap = *sector_size = 0;
702cfcc706cSMiquel Raynal if (chip->onfi_version) {
703cfcc706cSMiquel Raynal *cap = chip->ecc_strength_ds;
704cfcc706cSMiquel Raynal *sector_size = chip->ecc_step_ds;
705cfcc706cSMiquel Raynal pr_debug("ONFI params, minimum required ECC: %d bits in %d bytes\n",
706cfcc706cSMiquel Raynal *cap, *sector_size);
707cfcc706cSMiquel Raynal }
708cfcc706cSMiquel Raynal
709cfcc706cSMiquel Raynal if (*cap == 0 && *sector_size == 0) {
710cfcc706cSMiquel Raynal /* Non-ONFI compliant */
711cfcc706cSMiquel Raynal dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes\n");
712cfcc706cSMiquel Raynal *cap = 2;
713cfcc706cSMiquel Raynal *sector_size = 512;
714cfcc706cSMiquel Raynal }
715cfcc706cSMiquel Raynal
716cfcc706cSMiquel Raynal /* If head file doesn't specify then use the one in ONFI parameters */
717cfcc706cSMiquel Raynal if (host->pmecc_corr_cap == 0) {
718cfcc706cSMiquel Raynal /* use the most fitable ecc bits (the near bigger one ) */
719cfcc706cSMiquel Raynal if (*cap <= 2)
720cfcc706cSMiquel Raynal host->pmecc_corr_cap = 2;
721cfcc706cSMiquel Raynal else if (*cap <= 4)
722cfcc706cSMiquel Raynal host->pmecc_corr_cap = 4;
723cfcc706cSMiquel Raynal else if (*cap <= 8)
724cfcc706cSMiquel Raynal host->pmecc_corr_cap = 8;
725cfcc706cSMiquel Raynal else if (*cap <= 12)
726cfcc706cSMiquel Raynal host->pmecc_corr_cap = 12;
727cfcc706cSMiquel Raynal else if (*cap <= 24)
728cfcc706cSMiquel Raynal host->pmecc_corr_cap = 24;
729cfcc706cSMiquel Raynal else
730cfcc706cSMiquel Raynal #ifdef CONFIG_SAMA5D2
731cfcc706cSMiquel Raynal host->pmecc_corr_cap = 32;
732cfcc706cSMiquel Raynal #else
733cfcc706cSMiquel Raynal host->pmecc_corr_cap = 24;
734cfcc706cSMiquel Raynal #endif
735cfcc706cSMiquel Raynal }
736cfcc706cSMiquel Raynal if (host->pmecc_sector_size == 0) {
737cfcc706cSMiquel Raynal /* use the most fitable sector size (the near smaller one ) */
738cfcc706cSMiquel Raynal if (*sector_size >= 1024)
739cfcc706cSMiquel Raynal host->pmecc_sector_size = 1024;
740cfcc706cSMiquel Raynal else if (*sector_size >= 512)
741cfcc706cSMiquel Raynal host->pmecc_sector_size = 512;
742cfcc706cSMiquel Raynal else
743cfcc706cSMiquel Raynal return -EINVAL;
744cfcc706cSMiquel Raynal }
745cfcc706cSMiquel Raynal return 0;
746cfcc706cSMiquel Raynal }
747cfcc706cSMiquel Raynal #endif
748cfcc706cSMiquel Raynal
749cfcc706cSMiquel Raynal #if defined(NO_GALOIS_TABLE_IN_ROM)
750cfcc706cSMiquel Raynal static uint16_t *pmecc_galois_table;
deg(unsigned int poly)751cfcc706cSMiquel Raynal static inline int deg(unsigned int poly)
752cfcc706cSMiquel Raynal {
753cfcc706cSMiquel Raynal /* polynomial degree is the most-significant bit index */
754cfcc706cSMiquel Raynal return fls(poly) - 1;
755cfcc706cSMiquel Raynal }
756cfcc706cSMiquel Raynal
build_gf_tables(int mm,unsigned int poly,int16_t * index_of,int16_t * alpha_to)757cfcc706cSMiquel Raynal static int build_gf_tables(int mm, unsigned int poly,
758cfcc706cSMiquel Raynal int16_t *index_of, int16_t *alpha_to)
759cfcc706cSMiquel Raynal {
760cfcc706cSMiquel Raynal unsigned int i, x = 1;
761cfcc706cSMiquel Raynal const unsigned int k = 1 << deg(poly);
762cfcc706cSMiquel Raynal unsigned int nn = (1 << mm) - 1;
763cfcc706cSMiquel Raynal
764cfcc706cSMiquel Raynal /* primitive polynomial must be of degree m */
765cfcc706cSMiquel Raynal if (k != (1u << mm))
766cfcc706cSMiquel Raynal return -EINVAL;
767cfcc706cSMiquel Raynal
768cfcc706cSMiquel Raynal for (i = 0; i < nn; i++) {
769cfcc706cSMiquel Raynal alpha_to[i] = x;
770cfcc706cSMiquel Raynal index_of[x] = i;
771cfcc706cSMiquel Raynal if (i && (x == 1))
772cfcc706cSMiquel Raynal /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
773cfcc706cSMiquel Raynal return -EINVAL;
774cfcc706cSMiquel Raynal x <<= 1;
775cfcc706cSMiquel Raynal if (x & k)
776cfcc706cSMiquel Raynal x ^= poly;
777cfcc706cSMiquel Raynal }
778cfcc706cSMiquel Raynal
779cfcc706cSMiquel Raynal alpha_to[nn] = 1;
780cfcc706cSMiquel Raynal index_of[0] = 0;
781cfcc706cSMiquel Raynal
782cfcc706cSMiquel Raynal return 0;
783cfcc706cSMiquel Raynal }
784cfcc706cSMiquel Raynal
create_lookup_table(int sector_size)785cfcc706cSMiquel Raynal static uint16_t *create_lookup_table(int sector_size)
786cfcc706cSMiquel Raynal {
787cfcc706cSMiquel Raynal int degree = (sector_size == 512) ?
788cfcc706cSMiquel Raynal PMECC_GF_DIMENSION_13 :
789cfcc706cSMiquel Raynal PMECC_GF_DIMENSION_14;
790cfcc706cSMiquel Raynal unsigned int poly = (sector_size == 512) ?
791cfcc706cSMiquel Raynal PMECC_GF_13_PRIMITIVE_POLY :
792cfcc706cSMiquel Raynal PMECC_GF_14_PRIMITIVE_POLY;
793cfcc706cSMiquel Raynal int table_size = (sector_size == 512) ?
794cfcc706cSMiquel Raynal PMECC_INDEX_TABLE_SIZE_512 :
795cfcc706cSMiquel Raynal PMECC_INDEX_TABLE_SIZE_1024;
796cfcc706cSMiquel Raynal
797cfcc706cSMiquel Raynal int16_t *addr = kzalloc(2 * table_size * sizeof(uint16_t), GFP_KERNEL);
798cfcc706cSMiquel Raynal if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
799cfcc706cSMiquel Raynal return NULL;
800cfcc706cSMiquel Raynal
801cfcc706cSMiquel Raynal return (uint16_t *)addr;
802cfcc706cSMiquel Raynal }
803cfcc706cSMiquel Raynal #endif
804cfcc706cSMiquel Raynal
atmel_pmecc_nand_init_params(struct nand_chip * nand,struct mtd_info * mtd)805cfcc706cSMiquel Raynal static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
806cfcc706cSMiquel Raynal struct mtd_info *mtd)
807cfcc706cSMiquel Raynal {
808cfcc706cSMiquel Raynal struct atmel_nand_host *host;
809cfcc706cSMiquel Raynal int cap, sector_size;
810cfcc706cSMiquel Raynal
811cfcc706cSMiquel Raynal host = &pmecc_host;
812cfcc706cSMiquel Raynal nand_set_controller_data(nand, host);
813cfcc706cSMiquel Raynal
814cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_HW;
815cfcc706cSMiquel Raynal nand->ecc.calculate = NULL;
816cfcc706cSMiquel Raynal nand->ecc.correct = NULL;
817cfcc706cSMiquel Raynal nand->ecc.hwctl = NULL;
818cfcc706cSMiquel Raynal
819cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ONFI_DETECTION
820cfcc706cSMiquel Raynal host->pmecc_corr_cap = host->pmecc_sector_size = 0;
821cfcc706cSMiquel Raynal
822cfcc706cSMiquel Raynal #ifdef CONFIG_PMECC_CAP
823cfcc706cSMiquel Raynal host->pmecc_corr_cap = CONFIG_PMECC_CAP;
824cfcc706cSMiquel Raynal #endif
825cfcc706cSMiquel Raynal #ifdef CONFIG_PMECC_SECTOR_SIZE
826cfcc706cSMiquel Raynal host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
827cfcc706cSMiquel Raynal #endif
828cfcc706cSMiquel Raynal /* Get ECC requirement of ONFI parameters. And if CONFIG_PMECC_CAP or
829cfcc706cSMiquel Raynal * CONFIG_PMECC_SECTOR_SIZE not defined, then use ecc_bits, sector_size
830cfcc706cSMiquel Raynal * from ONFI.
831cfcc706cSMiquel Raynal */
832cfcc706cSMiquel Raynal if (pmecc_choose_ecc(host, nand, &cap, §or_size)) {
833cfcc706cSMiquel Raynal dev_err(host->dev, "Required ECC %d bits in %d bytes not supported!\n",
834cfcc706cSMiquel Raynal cap, sector_size);
835cfcc706cSMiquel Raynal return -EINVAL;
836cfcc706cSMiquel Raynal }
837cfcc706cSMiquel Raynal
838cfcc706cSMiquel Raynal if (cap > host->pmecc_corr_cap)
839cfcc706cSMiquel Raynal dev_info(host->dev, "WARNING: Using different ecc correct bits(%d bit) from Nand ONFI ECC reqirement (%d bit).\n",
840cfcc706cSMiquel Raynal host->pmecc_corr_cap, cap);
841cfcc706cSMiquel Raynal if (sector_size < host->pmecc_sector_size)
842cfcc706cSMiquel Raynal dev_info(host->dev, "WARNING: Using different ecc correct sector size (%d bytes) from Nand ONFI ECC reqirement (%d bytes).\n",
843cfcc706cSMiquel Raynal host->pmecc_sector_size, sector_size);
844cfcc706cSMiquel Raynal #else /* CONFIG_SYS_NAND_ONFI_DETECTION */
845cfcc706cSMiquel Raynal host->pmecc_corr_cap = CONFIG_PMECC_CAP;
846cfcc706cSMiquel Raynal host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
847cfcc706cSMiquel Raynal #endif
848cfcc706cSMiquel Raynal
849cfcc706cSMiquel Raynal cap = host->pmecc_corr_cap;
850cfcc706cSMiquel Raynal sector_size = host->pmecc_sector_size;
851cfcc706cSMiquel Raynal
852cfcc706cSMiquel Raynal /* TODO: need check whether cap & sector_size is validate */
853cfcc706cSMiquel Raynal #if defined(NO_GALOIS_TABLE_IN_ROM)
854cfcc706cSMiquel Raynal /*
855cfcc706cSMiquel Raynal * As pmecc_rom_base is the begin of the gallois field table, So the
856cfcc706cSMiquel Raynal * index offset just set as 0.
857cfcc706cSMiquel Raynal */
858cfcc706cSMiquel Raynal host->pmecc_index_table_offset = 0;
859cfcc706cSMiquel Raynal #else
860cfcc706cSMiquel Raynal if (host->pmecc_sector_size == 512)
861cfcc706cSMiquel Raynal host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_512;
862cfcc706cSMiquel Raynal else
863cfcc706cSMiquel Raynal host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024;
864cfcc706cSMiquel Raynal #endif
865cfcc706cSMiquel Raynal
866cfcc706cSMiquel Raynal pr_debug("Initialize PMECC params, cap: %d, sector: %d\n",
867cfcc706cSMiquel Raynal cap, sector_size);
868cfcc706cSMiquel Raynal
869cfcc706cSMiquel Raynal host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
870cfcc706cSMiquel Raynal host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
871cfcc706cSMiquel Raynal ATMEL_BASE_PMERRLOC;
872cfcc706cSMiquel Raynal #if defined(NO_GALOIS_TABLE_IN_ROM)
873cfcc706cSMiquel Raynal pmecc_galois_table = create_lookup_table(host->pmecc_sector_size);
874cfcc706cSMiquel Raynal if (!pmecc_galois_table) {
875cfcc706cSMiquel Raynal dev_err(host->dev, "out of memory\n");
876cfcc706cSMiquel Raynal return -ENOMEM;
877cfcc706cSMiquel Raynal }
878cfcc706cSMiquel Raynal
879cfcc706cSMiquel Raynal host->pmecc_rom_base = (void __iomem *)pmecc_galois_table;
880cfcc706cSMiquel Raynal #else
881cfcc706cSMiquel Raynal host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
882cfcc706cSMiquel Raynal #endif
883cfcc706cSMiquel Raynal
884cfcc706cSMiquel Raynal /* ECC is calculated for the whole page (1 step) */
885cfcc706cSMiquel Raynal nand->ecc.size = mtd->writesize;
886cfcc706cSMiquel Raynal
887cfcc706cSMiquel Raynal /* set ECC page size and oob layout */
888cfcc706cSMiquel Raynal switch (mtd->writesize) {
889cfcc706cSMiquel Raynal case 2048:
890cfcc706cSMiquel Raynal case 4096:
891cfcc706cSMiquel Raynal case 8192:
892cfcc706cSMiquel Raynal host->pmecc_degree = (sector_size == 512) ?
893cfcc706cSMiquel Raynal PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
894cfcc706cSMiquel Raynal host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
895cfcc706cSMiquel Raynal host->pmecc_sector_number = mtd->writesize / sector_size;
896cfcc706cSMiquel Raynal host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
897cfcc706cSMiquel Raynal cap, sector_size);
898cfcc706cSMiquel Raynal host->pmecc_alpha_to = pmecc_get_alpha_to(host);
899cfcc706cSMiquel Raynal host->pmecc_index_of = host->pmecc_rom_base +
900cfcc706cSMiquel Raynal host->pmecc_index_table_offset;
901cfcc706cSMiquel Raynal
902cfcc706cSMiquel Raynal nand->ecc.steps = 1;
903cfcc706cSMiquel Raynal nand->ecc.bytes = host->pmecc_bytes_per_sector *
904cfcc706cSMiquel Raynal host->pmecc_sector_number;
905cfcc706cSMiquel Raynal
906cfcc706cSMiquel Raynal if (nand->ecc.bytes > MTD_MAX_ECCPOS_ENTRIES_LARGE) {
907cfcc706cSMiquel Raynal dev_err(host->dev, "too large eccpos entries. max support ecc.bytes is %d\n",
908cfcc706cSMiquel Raynal MTD_MAX_ECCPOS_ENTRIES_LARGE);
909cfcc706cSMiquel Raynal return -EINVAL;
910cfcc706cSMiquel Raynal }
911cfcc706cSMiquel Raynal
912cfcc706cSMiquel Raynal if (nand->ecc.bytes > mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
913cfcc706cSMiquel Raynal dev_err(host->dev, "No room for ECC bytes\n");
914cfcc706cSMiquel Raynal return -EINVAL;
915cfcc706cSMiquel Raynal }
916cfcc706cSMiquel Raynal pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
917cfcc706cSMiquel Raynal mtd->oobsize,
918cfcc706cSMiquel Raynal nand->ecc.bytes);
919cfcc706cSMiquel Raynal nand->ecc.layout = &atmel_pmecc_oobinfo;
920cfcc706cSMiquel Raynal break;
921cfcc706cSMiquel Raynal case 512:
922cfcc706cSMiquel Raynal case 1024:
923cfcc706cSMiquel Raynal /* TODO */
924cfcc706cSMiquel Raynal dev_err(host->dev, "Unsupported page size for PMECC, use Software ECC\n");
925cfcc706cSMiquel Raynal default:
926cfcc706cSMiquel Raynal /* page size not handled by HW ECC */
927cfcc706cSMiquel Raynal /* switching back to soft ECC */
928cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_SOFT;
929cfcc706cSMiquel Raynal nand->ecc.read_page = NULL;
930cfcc706cSMiquel Raynal nand->ecc.postpad = 0;
931cfcc706cSMiquel Raynal nand->ecc.prepad = 0;
932cfcc706cSMiquel Raynal nand->ecc.bytes = 0;
933cfcc706cSMiquel Raynal return 0;
934cfcc706cSMiquel Raynal }
935cfcc706cSMiquel Raynal
936cfcc706cSMiquel Raynal /* Allocate data for PMECC computation */
937cfcc706cSMiquel Raynal if (pmecc_data_alloc(host)) {
938cfcc706cSMiquel Raynal dev_err(host->dev, "Cannot allocate memory for PMECC computation!\n");
939cfcc706cSMiquel Raynal return -ENOMEM;
940cfcc706cSMiquel Raynal }
941cfcc706cSMiquel Raynal
942cfcc706cSMiquel Raynal nand->options |= NAND_NO_SUBPAGE_WRITE;
943cfcc706cSMiquel Raynal nand->ecc.read_page = atmel_nand_pmecc_read_page;
944cfcc706cSMiquel Raynal nand->ecc.write_page = atmel_nand_pmecc_write_page;
945cfcc706cSMiquel Raynal nand->ecc.strength = cap;
946cfcc706cSMiquel Raynal
947cfcc706cSMiquel Raynal /* Check the PMECC ip version */
948cfcc706cSMiquel Raynal host->pmecc_version = pmecc_readl(host->pmerrloc, version);
949cfcc706cSMiquel Raynal dev_dbg(host->dev, "PMECC IP version is: %x\n", host->pmecc_version);
950cfcc706cSMiquel Raynal
951cfcc706cSMiquel Raynal atmel_pmecc_core_init(mtd);
952cfcc706cSMiquel Raynal
953cfcc706cSMiquel Raynal return 0;
954cfcc706cSMiquel Raynal }
955cfcc706cSMiquel Raynal
956cfcc706cSMiquel Raynal #else
957cfcc706cSMiquel Raynal
958cfcc706cSMiquel Raynal /* oob layout for large page size
959cfcc706cSMiquel Raynal * bad block info is on bytes 0 and 1
960cfcc706cSMiquel Raynal * the bytes have to be consecutives to avoid
961cfcc706cSMiquel Raynal * several NAND_CMD_RNDOUT during read
962cfcc706cSMiquel Raynal */
963cfcc706cSMiquel Raynal static struct nand_ecclayout atmel_oobinfo_large = {
964cfcc706cSMiquel Raynal .eccbytes = 4,
965cfcc706cSMiquel Raynal .eccpos = {60, 61, 62, 63},
966cfcc706cSMiquel Raynal .oobfree = {
967cfcc706cSMiquel Raynal {2, 58}
968cfcc706cSMiquel Raynal },
969cfcc706cSMiquel Raynal };
970cfcc706cSMiquel Raynal
971cfcc706cSMiquel Raynal /* oob layout for small page size
972cfcc706cSMiquel Raynal * bad block info is on bytes 4 and 5
973cfcc706cSMiquel Raynal * the bytes have to be consecutives to avoid
974cfcc706cSMiquel Raynal * several NAND_CMD_RNDOUT during read
975cfcc706cSMiquel Raynal */
976cfcc706cSMiquel Raynal static struct nand_ecclayout atmel_oobinfo_small = {
977cfcc706cSMiquel Raynal .eccbytes = 4,
978cfcc706cSMiquel Raynal .eccpos = {0, 1, 2, 3},
979cfcc706cSMiquel Raynal .oobfree = {
980cfcc706cSMiquel Raynal {6, 10}
981cfcc706cSMiquel Raynal },
982cfcc706cSMiquel Raynal };
983cfcc706cSMiquel Raynal
984cfcc706cSMiquel Raynal /*
985cfcc706cSMiquel Raynal * Calculate HW ECC
986cfcc706cSMiquel Raynal *
987cfcc706cSMiquel Raynal * function called after a write
988cfcc706cSMiquel Raynal *
989cfcc706cSMiquel Raynal * mtd: MTD block structure
990cfcc706cSMiquel Raynal * dat: raw data (unused)
991cfcc706cSMiquel Raynal * ecc_code: buffer for ECC
992cfcc706cSMiquel Raynal */
atmel_nand_calculate(struct mtd_info * mtd,const u_char * dat,unsigned char * ecc_code)993cfcc706cSMiquel Raynal static int atmel_nand_calculate(struct mtd_info *mtd,
994cfcc706cSMiquel Raynal const u_char *dat, unsigned char *ecc_code)
995cfcc706cSMiquel Raynal {
996cfcc706cSMiquel Raynal unsigned int ecc_value;
997cfcc706cSMiquel Raynal
998cfcc706cSMiquel Raynal /* get the first 2 ECC bytes */
999cfcc706cSMiquel Raynal ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
1000cfcc706cSMiquel Raynal
1001cfcc706cSMiquel Raynal ecc_code[0] = ecc_value & 0xFF;
1002cfcc706cSMiquel Raynal ecc_code[1] = (ecc_value >> 8) & 0xFF;
1003cfcc706cSMiquel Raynal
1004cfcc706cSMiquel Raynal /* get the last 2 ECC bytes */
1005cfcc706cSMiquel Raynal ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
1006cfcc706cSMiquel Raynal
1007cfcc706cSMiquel Raynal ecc_code[2] = ecc_value & 0xFF;
1008cfcc706cSMiquel Raynal ecc_code[3] = (ecc_value >> 8) & 0xFF;
1009cfcc706cSMiquel Raynal
1010cfcc706cSMiquel Raynal return 0;
1011cfcc706cSMiquel Raynal }
1012cfcc706cSMiquel Raynal
1013cfcc706cSMiquel Raynal /*
1014cfcc706cSMiquel Raynal * HW ECC read page function
1015cfcc706cSMiquel Raynal *
1016cfcc706cSMiquel Raynal * mtd: mtd info structure
1017cfcc706cSMiquel Raynal * chip: nand chip info structure
1018cfcc706cSMiquel Raynal * buf: buffer to store read data
1019cfcc706cSMiquel Raynal * oob_required: caller expects OOB data read to chip->oob_poi
1020cfcc706cSMiquel Raynal */
atmel_nand_read_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)1021cfcc706cSMiquel Raynal static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1022cfcc706cSMiquel Raynal uint8_t *buf, int oob_required, int page)
1023cfcc706cSMiquel Raynal {
1024cfcc706cSMiquel Raynal int eccsize = chip->ecc.size;
1025cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
1026cfcc706cSMiquel Raynal uint32_t *eccpos = chip->ecc.layout->eccpos;
1027cfcc706cSMiquel Raynal uint8_t *p = buf;
1028cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi;
1029cfcc706cSMiquel Raynal uint8_t *ecc_pos;
1030cfcc706cSMiquel Raynal int stat;
1031cfcc706cSMiquel Raynal
1032cfcc706cSMiquel Raynal /* read the page */
1033cfcc706cSMiquel Raynal chip->read_buf(mtd, p, eccsize);
1034cfcc706cSMiquel Raynal
1035cfcc706cSMiquel Raynal /* move to ECC position if needed */
1036cfcc706cSMiquel Raynal if (eccpos[0] != 0) {
1037cfcc706cSMiquel Raynal /* This only works on large pages
1038cfcc706cSMiquel Raynal * because the ECC controller waits for
1039cfcc706cSMiquel Raynal * NAND_CMD_RNDOUTSTART after the
1040cfcc706cSMiquel Raynal * NAND_CMD_RNDOUT.
1041cfcc706cSMiquel Raynal * anyway, for small pages, the eccpos[0] == 0
1042cfcc706cSMiquel Raynal */
1043cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1044cfcc706cSMiquel Raynal mtd->writesize + eccpos[0], -1);
1045cfcc706cSMiquel Raynal }
1046cfcc706cSMiquel Raynal
1047cfcc706cSMiquel Raynal /* the ECC controller needs to read the ECC just after the data */
1048cfcc706cSMiquel Raynal ecc_pos = oob + eccpos[0];
1049cfcc706cSMiquel Raynal chip->read_buf(mtd, ecc_pos, eccbytes);
1050cfcc706cSMiquel Raynal
1051cfcc706cSMiquel Raynal /* check if there's an error */
1052cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, oob, NULL);
1053cfcc706cSMiquel Raynal
1054cfcc706cSMiquel Raynal if (stat < 0)
1055cfcc706cSMiquel Raynal mtd->ecc_stats.failed++;
1056cfcc706cSMiquel Raynal else
1057cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat;
1058cfcc706cSMiquel Raynal
1059cfcc706cSMiquel Raynal /* get back to oob start (end of page) */
1060cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1061cfcc706cSMiquel Raynal
1062cfcc706cSMiquel Raynal /* read the oob */
1063cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, mtd->oobsize);
1064cfcc706cSMiquel Raynal
1065cfcc706cSMiquel Raynal return 0;
1066cfcc706cSMiquel Raynal }
1067cfcc706cSMiquel Raynal
1068cfcc706cSMiquel Raynal /*
1069cfcc706cSMiquel Raynal * HW ECC Correction
1070cfcc706cSMiquel Raynal *
1071cfcc706cSMiquel Raynal * function called after a read
1072cfcc706cSMiquel Raynal *
1073cfcc706cSMiquel Raynal * mtd: MTD block structure
1074cfcc706cSMiquel Raynal * dat: raw data read from the chip
1075cfcc706cSMiquel Raynal * read_ecc: ECC from the chip (unused)
1076cfcc706cSMiquel Raynal * isnull: unused
1077cfcc706cSMiquel Raynal *
1078cfcc706cSMiquel Raynal * Detect and correct a 1 bit error for a page
1079cfcc706cSMiquel Raynal */
atmel_nand_correct(struct mtd_info * mtd,u_char * dat,u_char * read_ecc,u_char * isnull)1080cfcc706cSMiquel Raynal static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
1081cfcc706cSMiquel Raynal u_char *read_ecc, u_char *isnull)
1082cfcc706cSMiquel Raynal {
1083cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
1084cfcc706cSMiquel Raynal unsigned int ecc_status;
1085cfcc706cSMiquel Raynal unsigned int ecc_word, ecc_bit;
1086cfcc706cSMiquel Raynal
1087cfcc706cSMiquel Raynal /* get the status from the Status Register */
1088cfcc706cSMiquel Raynal ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
1089cfcc706cSMiquel Raynal
1090cfcc706cSMiquel Raynal /* if there's no error */
1091cfcc706cSMiquel Raynal if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
1092cfcc706cSMiquel Raynal return 0;
1093cfcc706cSMiquel Raynal
1094cfcc706cSMiquel Raynal /* get error bit offset (4 bits) */
1095cfcc706cSMiquel Raynal ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
1096cfcc706cSMiquel Raynal /* get word address (12 bits) */
1097cfcc706cSMiquel Raynal ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
1098cfcc706cSMiquel Raynal ecc_word >>= 4;
1099cfcc706cSMiquel Raynal
1100cfcc706cSMiquel Raynal /* if there are multiple errors */
1101cfcc706cSMiquel Raynal if (ecc_status & ATMEL_ECC_MULERR) {
1102cfcc706cSMiquel Raynal /* check if it is a freshly erased block
1103cfcc706cSMiquel Raynal * (filled with 0xff) */
1104cfcc706cSMiquel Raynal if ((ecc_bit == ATMEL_ECC_BITADDR)
1105cfcc706cSMiquel Raynal && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
1106cfcc706cSMiquel Raynal /* the block has just been erased, return OK */
1107cfcc706cSMiquel Raynal return 0;
1108cfcc706cSMiquel Raynal }
1109cfcc706cSMiquel Raynal /* it doesn't seems to be a freshly
1110cfcc706cSMiquel Raynal * erased block.
1111cfcc706cSMiquel Raynal * We can't correct so many errors */
1112cfcc706cSMiquel Raynal dev_warn(host->dev, "atmel_nand : multiple errors detected."
1113cfcc706cSMiquel Raynal " Unable to correct.\n");
1114cfcc706cSMiquel Raynal return -EBADMSG;
1115cfcc706cSMiquel Raynal }
1116cfcc706cSMiquel Raynal
1117cfcc706cSMiquel Raynal /* if there's a single bit error : we can correct it */
1118cfcc706cSMiquel Raynal if (ecc_status & ATMEL_ECC_ECCERR) {
1119cfcc706cSMiquel Raynal /* there's nothing much to do here.
1120cfcc706cSMiquel Raynal * the bit error is on the ECC itself.
1121cfcc706cSMiquel Raynal */
1122cfcc706cSMiquel Raynal dev_warn(host->dev, "atmel_nand : one bit error on ECC code."
1123cfcc706cSMiquel Raynal " Nothing to correct\n");
1124cfcc706cSMiquel Raynal return 0;
1125cfcc706cSMiquel Raynal }
1126cfcc706cSMiquel Raynal
1127cfcc706cSMiquel Raynal dev_warn(host->dev, "atmel_nand : one bit error on data."
1128cfcc706cSMiquel Raynal " (word offset in the page :"
1129cfcc706cSMiquel Raynal " 0x%x bit offset : 0x%x)\n",
1130cfcc706cSMiquel Raynal ecc_word, ecc_bit);
1131cfcc706cSMiquel Raynal /* correct the error */
1132cfcc706cSMiquel Raynal if (nand_chip->options & NAND_BUSWIDTH_16) {
1133cfcc706cSMiquel Raynal /* 16 bits words */
1134cfcc706cSMiquel Raynal ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1135cfcc706cSMiquel Raynal } else {
1136cfcc706cSMiquel Raynal /* 8 bits words */
1137cfcc706cSMiquel Raynal dat[ecc_word] ^= (1 << ecc_bit);
1138cfcc706cSMiquel Raynal }
1139cfcc706cSMiquel Raynal dev_warn(host->dev, "atmel_nand : error corrected\n");
1140cfcc706cSMiquel Raynal return 1;
1141cfcc706cSMiquel Raynal }
1142cfcc706cSMiquel Raynal
1143cfcc706cSMiquel Raynal /*
1144cfcc706cSMiquel Raynal * Enable HW ECC : unused on most chips
1145cfcc706cSMiquel Raynal */
atmel_nand_hwctl(struct mtd_info * mtd,int mode)1146cfcc706cSMiquel Raynal static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1147cfcc706cSMiquel Raynal {
1148cfcc706cSMiquel Raynal }
1149cfcc706cSMiquel Raynal
atmel_hwecc_nand_init_param(struct nand_chip * nand,struct mtd_info * mtd)1150cfcc706cSMiquel Raynal int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
1151cfcc706cSMiquel Raynal {
1152cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_HW;
1153cfcc706cSMiquel Raynal nand->ecc.calculate = atmel_nand_calculate;
1154cfcc706cSMiquel Raynal nand->ecc.correct = atmel_nand_correct;
1155cfcc706cSMiquel Raynal nand->ecc.hwctl = atmel_nand_hwctl;
1156cfcc706cSMiquel Raynal nand->ecc.read_page = atmel_nand_read_page;
1157cfcc706cSMiquel Raynal nand->ecc.bytes = 4;
1158cfcc706cSMiquel Raynal nand->ecc.strength = 4;
1159cfcc706cSMiquel Raynal
1160cfcc706cSMiquel Raynal if (nand->ecc.mode == NAND_ECC_HW) {
1161cfcc706cSMiquel Raynal /* ECC is calculated for the whole page (1 step) */
1162cfcc706cSMiquel Raynal nand->ecc.size = mtd->writesize;
1163cfcc706cSMiquel Raynal
1164cfcc706cSMiquel Raynal /* set ECC page size and oob layout */
1165cfcc706cSMiquel Raynal switch (mtd->writesize) {
1166cfcc706cSMiquel Raynal case 512:
1167cfcc706cSMiquel Raynal nand->ecc.layout = &atmel_oobinfo_small;
1168cfcc706cSMiquel Raynal ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1169cfcc706cSMiquel Raynal ATMEL_ECC_PAGESIZE_528);
1170cfcc706cSMiquel Raynal break;
1171cfcc706cSMiquel Raynal case 1024:
1172cfcc706cSMiquel Raynal nand->ecc.layout = &atmel_oobinfo_large;
1173cfcc706cSMiquel Raynal ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1174cfcc706cSMiquel Raynal ATMEL_ECC_PAGESIZE_1056);
1175cfcc706cSMiquel Raynal break;
1176cfcc706cSMiquel Raynal case 2048:
1177cfcc706cSMiquel Raynal nand->ecc.layout = &atmel_oobinfo_large;
1178cfcc706cSMiquel Raynal ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1179cfcc706cSMiquel Raynal ATMEL_ECC_PAGESIZE_2112);
1180cfcc706cSMiquel Raynal break;
1181cfcc706cSMiquel Raynal case 4096:
1182cfcc706cSMiquel Raynal nand->ecc.layout = &atmel_oobinfo_large;
1183cfcc706cSMiquel Raynal ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1184cfcc706cSMiquel Raynal ATMEL_ECC_PAGESIZE_4224);
1185cfcc706cSMiquel Raynal break;
1186cfcc706cSMiquel Raynal default:
1187cfcc706cSMiquel Raynal /* page size not handled by HW ECC */
1188cfcc706cSMiquel Raynal /* switching back to soft ECC */
1189cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_SOFT;
1190cfcc706cSMiquel Raynal nand->ecc.calculate = NULL;
1191cfcc706cSMiquel Raynal nand->ecc.correct = NULL;
1192cfcc706cSMiquel Raynal nand->ecc.hwctl = NULL;
1193cfcc706cSMiquel Raynal nand->ecc.read_page = NULL;
1194cfcc706cSMiquel Raynal nand->ecc.postpad = 0;
1195cfcc706cSMiquel Raynal nand->ecc.prepad = 0;
1196cfcc706cSMiquel Raynal nand->ecc.bytes = 0;
1197cfcc706cSMiquel Raynal break;
1198cfcc706cSMiquel Raynal }
1199cfcc706cSMiquel Raynal }
1200cfcc706cSMiquel Raynal
1201cfcc706cSMiquel Raynal return 0;
1202cfcc706cSMiquel Raynal }
1203cfcc706cSMiquel Raynal
1204cfcc706cSMiquel Raynal #endif /* CONFIG_ATMEL_NAND_HW_PMECC */
1205cfcc706cSMiquel Raynal
1206cfcc706cSMiquel Raynal #endif /* CONFIG_ATMEL_NAND_HWECC */
1207cfcc706cSMiquel Raynal
at91_nand_hwcontrol(struct mtd_info * mtd,int cmd,unsigned int ctrl)1208cfcc706cSMiquel Raynal static void at91_nand_hwcontrol(struct mtd_info *mtd,
1209cfcc706cSMiquel Raynal int cmd, unsigned int ctrl)
1210cfcc706cSMiquel Raynal {
1211cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1212cfcc706cSMiquel Raynal
1213cfcc706cSMiquel Raynal if (ctrl & NAND_CTRL_CHANGE) {
1214cfcc706cSMiquel Raynal ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
1215cfcc706cSMiquel Raynal IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
1216cfcc706cSMiquel Raynal | CONFIG_SYS_NAND_MASK_CLE);
1217cfcc706cSMiquel Raynal
1218cfcc706cSMiquel Raynal if (ctrl & NAND_CLE)
1219cfcc706cSMiquel Raynal IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
1220cfcc706cSMiquel Raynal if (ctrl & NAND_ALE)
1221cfcc706cSMiquel Raynal IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
1222cfcc706cSMiquel Raynal
1223cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_ENABLE_PIN
1224cfcc706cSMiquel Raynal at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
1225cfcc706cSMiquel Raynal !(ctrl & NAND_NCE));
1226cfcc706cSMiquel Raynal #endif
1227cfcc706cSMiquel Raynal this->IO_ADDR_W = (void *) IO_ADDR_W;
1228cfcc706cSMiquel Raynal }
1229cfcc706cSMiquel Raynal
1230cfcc706cSMiquel Raynal if (cmd != NAND_CMD_NONE)
1231cfcc706cSMiquel Raynal writeb(cmd, this->IO_ADDR_W);
1232cfcc706cSMiquel Raynal }
1233cfcc706cSMiquel Raynal
1234cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_READY_PIN
at91_nand_ready(struct mtd_info * mtd)1235cfcc706cSMiquel Raynal static int at91_nand_ready(struct mtd_info *mtd)
1236cfcc706cSMiquel Raynal {
1237cfcc706cSMiquel Raynal return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
1238cfcc706cSMiquel Raynal }
1239cfcc706cSMiquel Raynal #endif
1240cfcc706cSMiquel Raynal
1241cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_BUILD
1242cfcc706cSMiquel Raynal /* The following code is for SPL */
1243cfcc706cSMiquel Raynal static struct mtd_info *mtd;
1244cfcc706cSMiquel Raynal static struct nand_chip nand_chip;
1245cfcc706cSMiquel Raynal
nand_command(int block,int page,uint32_t offs,u8 cmd)1246cfcc706cSMiquel Raynal static int nand_command(int block, int page, uint32_t offs, u8 cmd)
1247cfcc706cSMiquel Raynal {
1248cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1249cfcc706cSMiquel Raynal int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
1250cfcc706cSMiquel Raynal void (*hwctrl)(struct mtd_info *mtd, int cmd,
1251cfcc706cSMiquel Raynal unsigned int ctrl) = this->cmd_ctrl;
1252cfcc706cSMiquel Raynal
1253cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
1254cfcc706cSMiquel Raynal ;
1255cfcc706cSMiquel Raynal
1256cfcc706cSMiquel Raynal if (cmd == NAND_CMD_READOOB) {
1257cfcc706cSMiquel Raynal offs += CONFIG_SYS_NAND_PAGE_SIZE;
1258cfcc706cSMiquel Raynal cmd = NAND_CMD_READ0;
1259cfcc706cSMiquel Raynal }
1260cfcc706cSMiquel Raynal
1261cfcc706cSMiquel Raynal hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1262cfcc706cSMiquel Raynal
1263cfcc706cSMiquel Raynal if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
1264cfcc706cSMiquel Raynal offs >>= 1;
1265cfcc706cSMiquel Raynal
1266cfcc706cSMiquel Raynal hwctrl(mtd, offs & 0xff, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1267cfcc706cSMiquel Raynal hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE);
1268cfcc706cSMiquel Raynal hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE);
1269cfcc706cSMiquel Raynal hwctrl(mtd, ((page_addr >> 8) & 0xff), NAND_CTRL_ALE);
1270cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
1271cfcc706cSMiquel Raynal hwctrl(mtd, (page_addr >> 16) & 0x0f, NAND_CTRL_ALE);
1272cfcc706cSMiquel Raynal #endif
1273cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
1274cfcc706cSMiquel Raynal
1275cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_READSTART, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1276cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
1277cfcc706cSMiquel Raynal
1278cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
1279cfcc706cSMiquel Raynal ;
1280cfcc706cSMiquel Raynal
1281cfcc706cSMiquel Raynal return 0;
1282cfcc706cSMiquel Raynal }
1283cfcc706cSMiquel Raynal
nand_is_bad_block(int block)1284cfcc706cSMiquel Raynal static int nand_is_bad_block(int block)
1285cfcc706cSMiquel Raynal {
1286cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1287cfcc706cSMiquel Raynal
1288cfcc706cSMiquel Raynal nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
1289cfcc706cSMiquel Raynal
1290cfcc706cSMiquel Raynal if (this->options & NAND_BUSWIDTH_16) {
1291cfcc706cSMiquel Raynal if (readw(this->IO_ADDR_R) != 0xffff)
1292cfcc706cSMiquel Raynal return 1;
1293cfcc706cSMiquel Raynal } else {
1294cfcc706cSMiquel Raynal if (readb(this->IO_ADDR_R) != 0xff)
1295cfcc706cSMiquel Raynal return 1;
1296cfcc706cSMiquel Raynal }
1297cfcc706cSMiquel Raynal
1298cfcc706cSMiquel Raynal return 0;
1299cfcc706cSMiquel Raynal }
1300cfcc706cSMiquel Raynal
1301cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_NAND_ECC
1302cfcc706cSMiquel Raynal static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
1303cfcc706cSMiquel Raynal #define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
1304cfcc706cSMiquel Raynal CONFIG_SYS_NAND_ECCSIZE)
1305cfcc706cSMiquel Raynal #define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
1306cfcc706cSMiquel Raynal
nand_read_page(int block,int page,void * dst)1307cfcc706cSMiquel Raynal static int nand_read_page(int block, int page, void *dst)
1308cfcc706cSMiquel Raynal {
1309cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1310cfcc706cSMiquel Raynal u_char ecc_calc[ECCTOTAL];
1311cfcc706cSMiquel Raynal u_char ecc_code[ECCTOTAL];
1312cfcc706cSMiquel Raynal u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
1313cfcc706cSMiquel Raynal int eccsize = CONFIG_SYS_NAND_ECCSIZE;
1314cfcc706cSMiquel Raynal int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
1315cfcc706cSMiquel Raynal int eccsteps = ECCSTEPS;
1316cfcc706cSMiquel Raynal int i;
1317cfcc706cSMiquel Raynal uint8_t *p = dst;
1318cfcc706cSMiquel Raynal nand_command(block, page, 0, NAND_CMD_READ0);
1319cfcc706cSMiquel Raynal
1320cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1321cfcc706cSMiquel Raynal if (this->ecc.mode != NAND_ECC_SOFT)
1322cfcc706cSMiquel Raynal this->ecc.hwctl(mtd, NAND_ECC_READ);
1323cfcc706cSMiquel Raynal this->read_buf(mtd, p, eccsize);
1324cfcc706cSMiquel Raynal this->ecc.calculate(mtd, p, &ecc_calc[i]);
1325cfcc706cSMiquel Raynal }
1326cfcc706cSMiquel Raynal this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
1327cfcc706cSMiquel Raynal
1328cfcc706cSMiquel Raynal for (i = 0; i < ECCTOTAL; i++)
1329cfcc706cSMiquel Raynal ecc_code[i] = oob_data[nand_ecc_pos[i]];
1330cfcc706cSMiquel Raynal
1331cfcc706cSMiquel Raynal eccsteps = ECCSTEPS;
1332cfcc706cSMiquel Raynal p = dst;
1333cfcc706cSMiquel Raynal
1334cfcc706cSMiquel Raynal for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1335cfcc706cSMiquel Raynal this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1336cfcc706cSMiquel Raynal
1337cfcc706cSMiquel Raynal return 0;
1338cfcc706cSMiquel Raynal }
1339cfcc706cSMiquel Raynal
spl_nand_erase_one(int block,int page)1340cfcc706cSMiquel Raynal int spl_nand_erase_one(int block, int page)
1341cfcc706cSMiquel Raynal {
1342cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1343cfcc706cSMiquel Raynal void (*hwctrl)(struct mtd_info *mtd, int cmd,
1344cfcc706cSMiquel Raynal unsigned int ctrl) = this->cmd_ctrl;
1345cfcc706cSMiquel Raynal int page_addr;
1346cfcc706cSMiquel Raynal
1347cfcc706cSMiquel Raynal if (nand_chip.select_chip)
1348cfcc706cSMiquel Raynal nand_chip.select_chip(mtd, 0);
1349cfcc706cSMiquel Raynal
1350cfcc706cSMiquel Raynal page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
1351cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1352cfcc706cSMiquel Raynal /* Row address */
1353cfcc706cSMiquel Raynal hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1354cfcc706cSMiquel Raynal hwctrl(mtd, ((page_addr >> 8) & 0xff),
1355cfcc706cSMiquel Raynal NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1356cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
1357cfcc706cSMiquel Raynal /* One more address cycle for devices > 128MiB */
1358cfcc706cSMiquel Raynal hwctrl(mtd, (page_addr >> 16) & 0x0f,
1359cfcc706cSMiquel Raynal NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1360cfcc706cSMiquel Raynal #endif
1361cfcc706cSMiquel Raynal hwctrl(mtd, NAND_CMD_ERASE2, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1362cfcc706cSMiquel Raynal
1363cfcc706cSMiquel Raynal while (!this->dev_ready(mtd))
1364cfcc706cSMiquel Raynal ;
1365cfcc706cSMiquel Raynal
1366cfcc706cSMiquel Raynal nand_deselect();
1367cfcc706cSMiquel Raynal
1368cfcc706cSMiquel Raynal return 0;
1369cfcc706cSMiquel Raynal }
1370cfcc706cSMiquel Raynal #else
nand_read_page(int block,int page,void * dst)1371cfcc706cSMiquel Raynal static int nand_read_page(int block, int page, void *dst)
1372cfcc706cSMiquel Raynal {
1373cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1374cfcc706cSMiquel Raynal
1375cfcc706cSMiquel Raynal nand_command(block, page, 0, NAND_CMD_READ0);
1376cfcc706cSMiquel Raynal atmel_nand_pmecc_read_page(mtd, this, dst, 0, page);
1377cfcc706cSMiquel Raynal
1378cfcc706cSMiquel Raynal return 0;
1379cfcc706cSMiquel Raynal }
1380cfcc706cSMiquel Raynal #endif /* CONFIG_SPL_NAND_ECC */
1381cfcc706cSMiquel Raynal
at91_nand_wait_ready(struct mtd_info * mtd)1382cfcc706cSMiquel Raynal int at91_nand_wait_ready(struct mtd_info *mtd)
1383cfcc706cSMiquel Raynal {
1384cfcc706cSMiquel Raynal struct nand_chip *this = mtd_to_nand(mtd);
1385cfcc706cSMiquel Raynal
1386cfcc706cSMiquel Raynal udelay(this->chip_delay);
1387cfcc706cSMiquel Raynal
1388cfcc706cSMiquel Raynal return 1;
1389cfcc706cSMiquel Raynal }
1390cfcc706cSMiquel Raynal
board_nand_init(struct nand_chip * nand)1391cfcc706cSMiquel Raynal int board_nand_init(struct nand_chip *nand)
1392cfcc706cSMiquel Raynal {
1393cfcc706cSMiquel Raynal int ret = 0;
1394cfcc706cSMiquel Raynal
1395cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_SOFT;
1396cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_DBW_16
1397cfcc706cSMiquel Raynal nand->options = NAND_BUSWIDTH_16;
1398cfcc706cSMiquel Raynal nand->read_buf = nand_read_buf16;
1399cfcc706cSMiquel Raynal #else
1400cfcc706cSMiquel Raynal nand->read_buf = nand_read_buf;
1401cfcc706cSMiquel Raynal #endif
1402cfcc706cSMiquel Raynal nand->cmd_ctrl = at91_nand_hwcontrol;
1403cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_READY_PIN
1404cfcc706cSMiquel Raynal nand->dev_ready = at91_nand_ready;
1405cfcc706cSMiquel Raynal #else
1406cfcc706cSMiquel Raynal nand->dev_ready = at91_nand_wait_ready;
1407cfcc706cSMiquel Raynal #endif
1408cfcc706cSMiquel Raynal nand->chip_delay = 20;
1409cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1410cfcc706cSMiquel Raynal nand->bbt_options |= NAND_BBT_USE_FLASH;
1411cfcc706cSMiquel Raynal #endif
1412cfcc706cSMiquel Raynal
1413cfcc706cSMiquel Raynal #ifdef CONFIG_ATMEL_NAND_HWECC
1414cfcc706cSMiquel Raynal #ifdef CONFIG_ATMEL_NAND_HW_PMECC
1415cfcc706cSMiquel Raynal ret = atmel_pmecc_nand_init_params(nand, mtd);
1416cfcc706cSMiquel Raynal #endif
1417cfcc706cSMiquel Raynal #endif
1418cfcc706cSMiquel Raynal
1419cfcc706cSMiquel Raynal return ret;
1420cfcc706cSMiquel Raynal }
1421cfcc706cSMiquel Raynal
nand_init(void)1422cfcc706cSMiquel Raynal void nand_init(void)
1423cfcc706cSMiquel Raynal {
1424cfcc706cSMiquel Raynal mtd = nand_to_mtd(&nand_chip);
1425cfcc706cSMiquel Raynal mtd->writesize = CONFIG_SYS_NAND_PAGE_SIZE;
1426cfcc706cSMiquel Raynal mtd->oobsize = CONFIG_SYS_NAND_OOBSIZE;
1427cfcc706cSMiquel Raynal nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE;
1428cfcc706cSMiquel Raynal nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
1429cfcc706cSMiquel Raynal board_nand_init(&nand_chip);
1430cfcc706cSMiquel Raynal
1431cfcc706cSMiquel Raynal #ifdef CONFIG_SPL_NAND_ECC
1432cfcc706cSMiquel Raynal if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
1433cfcc706cSMiquel Raynal nand_chip.ecc.calculate = nand_calculate_ecc;
1434cfcc706cSMiquel Raynal nand_chip.ecc.correct = nand_correct_data;
1435cfcc706cSMiquel Raynal }
1436cfcc706cSMiquel Raynal #endif
1437cfcc706cSMiquel Raynal
1438cfcc706cSMiquel Raynal if (nand_chip.select_chip)
1439cfcc706cSMiquel Raynal nand_chip.select_chip(mtd, 0);
1440cfcc706cSMiquel Raynal }
1441cfcc706cSMiquel Raynal
nand_deselect(void)1442cfcc706cSMiquel Raynal void nand_deselect(void)
1443cfcc706cSMiquel Raynal {
1444cfcc706cSMiquel Raynal if (nand_chip.select_chip)
1445cfcc706cSMiquel Raynal nand_chip.select_chip(mtd, -1);
1446cfcc706cSMiquel Raynal }
1447cfcc706cSMiquel Raynal
1448cfcc706cSMiquel Raynal #include "nand_spl_loaders.c"
1449cfcc706cSMiquel Raynal
1450cfcc706cSMiquel Raynal #else
1451cfcc706cSMiquel Raynal
1452cfcc706cSMiquel Raynal #ifndef CONFIG_SYS_NAND_BASE_LIST
1453cfcc706cSMiquel Raynal #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
1454cfcc706cSMiquel Raynal #endif
1455cfcc706cSMiquel Raynal static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
1456cfcc706cSMiquel Raynal static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
1457cfcc706cSMiquel Raynal
atmel_nand_chip_init(int devnum,ulong base_addr)1458cfcc706cSMiquel Raynal int atmel_nand_chip_init(int devnum, ulong base_addr)
1459cfcc706cSMiquel Raynal {
1460cfcc706cSMiquel Raynal int ret;
1461cfcc706cSMiquel Raynal struct nand_chip *nand = &nand_chip[devnum];
1462cfcc706cSMiquel Raynal struct mtd_info *mtd = nand_to_mtd(nand);
1463cfcc706cSMiquel Raynal
1464cfcc706cSMiquel Raynal nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
1465cfcc706cSMiquel Raynal
1466cfcc706cSMiquel Raynal #ifdef CONFIG_NAND_ECC_BCH
1467cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_SOFT_BCH;
1468cfcc706cSMiquel Raynal #else
1469cfcc706cSMiquel Raynal nand->ecc.mode = NAND_ECC_SOFT;
1470cfcc706cSMiquel Raynal #endif
1471cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_DBW_16
1472cfcc706cSMiquel Raynal nand->options = NAND_BUSWIDTH_16;
1473cfcc706cSMiquel Raynal #endif
1474cfcc706cSMiquel Raynal nand->cmd_ctrl = at91_nand_hwcontrol;
1475cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_READY_PIN
1476cfcc706cSMiquel Raynal nand->dev_ready = at91_nand_ready;
1477cfcc706cSMiquel Raynal #endif
1478cfcc706cSMiquel Raynal nand->chip_delay = 75;
1479cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1480cfcc706cSMiquel Raynal nand->bbt_options |= NAND_BBT_USE_FLASH;
1481cfcc706cSMiquel Raynal #endif
1482cfcc706cSMiquel Raynal
1483cfcc706cSMiquel Raynal ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
1484cfcc706cSMiquel Raynal if (ret)
1485cfcc706cSMiquel Raynal return ret;
1486cfcc706cSMiquel Raynal
1487cfcc706cSMiquel Raynal #ifdef CONFIG_ATMEL_NAND_HWECC
1488cfcc706cSMiquel Raynal #ifdef CONFIG_ATMEL_NAND_HW_PMECC
1489cfcc706cSMiquel Raynal ret = atmel_pmecc_nand_init_params(nand, mtd);
1490cfcc706cSMiquel Raynal #else
1491cfcc706cSMiquel Raynal ret = atmel_hwecc_nand_init_param(nand, mtd);
1492cfcc706cSMiquel Raynal #endif
1493cfcc706cSMiquel Raynal if (ret)
1494cfcc706cSMiquel Raynal return ret;
1495cfcc706cSMiquel Raynal #endif
1496cfcc706cSMiquel Raynal
1497cfcc706cSMiquel Raynal ret = nand_scan_tail(mtd);
1498cfcc706cSMiquel Raynal if (!ret)
1499cfcc706cSMiquel Raynal nand_register(devnum, mtd);
1500cfcc706cSMiquel Raynal
1501cfcc706cSMiquel Raynal return ret;
1502cfcc706cSMiquel Raynal }
1503cfcc706cSMiquel Raynal
board_nand_init(void)1504cfcc706cSMiquel Raynal void board_nand_init(void)
1505cfcc706cSMiquel Raynal {
1506cfcc706cSMiquel Raynal int i;
1507cfcc706cSMiquel Raynal for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
1508cfcc706cSMiquel Raynal if (atmel_nand_chip_init(i, base_addr[i]))
1509cfcc706cSMiquel Raynal dev_err(host->dev, "atmel_nand: Fail to initialize #%d chip",
1510cfcc706cSMiquel Raynal i);
1511cfcc706cSMiquel Raynal }
1512cfcc706cSMiquel Raynal #endif /* CONFIG_SPL_BUILD */
1513