blob: 5e95901e275169efe85755547cb7486fd122585c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sergey Lapin77e524c2008-10-31 12:28:43 +01002/*
3 * (C) Copyright 2007-2008
Stelian Pop5ee0c7f2011-11-01 00:00:39 +01004 * Stelian Pop <stelian@popies.net>
Sergey Lapin77e524c2008-10-31 12:28:43 +01005 * Lead Tech Design <www.leadtechdesign.com>
6 *
7 * (C) Copyright 2006 ATMEL Rousset, Lacressonniere Nicolas
8 *
Wu, Joshfd3091d2012-08-23 00:05:36 +00009 * Add Programmable Multibit ECC support for various AT91 SoC
10 * (C) Copyright 2012 ATMEL, Hong Xu
Sergey Lapin77e524c2008-10-31 12:28:43 +010011 */
12
13#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Andreas Bießmanna4c24d32013-11-29 12:13:45 +010015#include <asm/gpio.h>
Sergey Lapin77e524c2008-10-31 12:28:43 +010016#include <asm/arch/gpio.h>
Simon Glass9bc15642020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060020#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Sergey Lapin77e524c2008-10-31 12:28:43 +010022
Wu, Josh4e87b3152013-07-03 11:11:48 +080023#include <malloc.h>
Sergey Lapin77e524c2008-10-31 12:28:43 +010024#include <nand.h>
Wu, Joshfd3091d2012-08-23 00:05:36 +000025#include <watchdog.h>
Heiko Schocherfd683382014-10-31 08:31:01 +010026#include <linux/mtd/nand_ecc.h>
Sergey Lapin77e524c2008-10-31 12:28:43 +010027
Nikolay Petukhove6015ca2010-03-19 10:49:27 +050028#ifdef CONFIG_ATMEL_NAND_HWECC
29
30/* Register access macros */
31#define ecc_readl(add, reg) \
Andre Renaudcf44b942016-05-05 07:28:14 -060032 readl(add + ATMEL_ECC_##reg)
Nikolay Petukhove6015ca2010-03-19 10:49:27 +050033#define ecc_writel(add, reg, value) \
Andre Renaudcf44b942016-05-05 07:28:14 -060034 writel((value), add + ATMEL_ECC_##reg)
Nikolay Petukhove6015ca2010-03-19 10:49:27 +050035
36#include "atmel_nand_ecc.h" /* Hardware ECC registers */
37
Wu, Joshfd3091d2012-08-23 00:05:36 +000038#ifdef CONFIG_ATMEL_NAND_HW_PMECC
39
Bo Shen9415b872014-03-03 14:47:16 +080040#ifdef CONFIG_SPL_BUILD
41#undef CONFIG_SYS_NAND_ONFI_DETECTION
42#endif
43
Wu, Joshfd3091d2012-08-23 00:05:36 +000044struct atmel_nand_host {
45 struct pmecc_regs __iomem *pmecc;
46 struct pmecc_errloc_regs __iomem *pmerrloc;
47 void __iomem *pmecc_rom_base;
48
49 u8 pmecc_corr_cap;
50 u16 pmecc_sector_size;
51 u32 pmecc_index_table_offset;
Wu, Josh1f5c0892015-01-16 11:54:46 +080052 u32 pmecc_version;
Wu, Joshfd3091d2012-08-23 00:05:36 +000053
54 int pmecc_bytes_per_sector;
55 int pmecc_sector_number;
56 int pmecc_degree; /* Degree of remainders */
57 int pmecc_cw_len; /* Length of codeword */
58
59 /* lookup table for alpha_to and index_of */
60 void __iomem *pmecc_alpha_to;
61 void __iomem *pmecc_index_of;
62
63 /* data for pmecc computation */
Wu, Josh4e87b3152013-07-03 11:11:48 +080064 int16_t *pmecc_smu;
65 int16_t *pmecc_partial_syn;
66 int16_t *pmecc_si;
67 int16_t *pmecc_lmu; /* polynomal order */
68 int *pmecc_mu;
69 int *pmecc_dmu;
70 int *pmecc_delta;
Wu, Joshfd3091d2012-08-23 00:05:36 +000071};
72
73static struct atmel_nand_host pmecc_host;
74static struct nand_ecclayout atmel_pmecc_oobinfo;
75
76/*
77 * Return number of ecc bytes per sector according to sector size and
78 * correction capability
79 *
80 * Following table shows what at91 PMECC supported:
81 * Correction Capability Sector_512_bytes Sector_1024_bytes
82 * ===================== ================ =================
83 * 2-bits 4-bytes 4-bytes
84 * 4-bits 7-bytes 7-bytes
85 * 8-bits 13-bytes 14-bytes
86 * 12-bits 20-bytes 21-bytes
87 * 24-bits 39-bytes 42-bytes
Josh Wuce764952015-11-24 16:34:01 +080088 * 32-bits 52-bytes 56-bytes
Wu, Joshfd3091d2012-08-23 00:05:36 +000089 */
90static int pmecc_get_ecc_bytes(int cap, int sector_size)
91{
92 int m = 12 + sector_size / 512;
93 return (m * cap + 7) / 8;
94}
95
96static void pmecc_config_ecc_layout(struct nand_ecclayout *layout,
97 int oobsize, int ecc_len)
98{
99 int i;
100
101 layout->eccbytes = ecc_len;
102
103 /* ECC will occupy the last ecc_len bytes continuously */
104 for (i = 0; i < ecc_len; i++)
105 layout->eccpos[i] = oobsize - ecc_len + i;
106
107 layout->oobfree[0].offset = 2;
108 layout->oobfree[0].length =
109 oobsize - ecc_len - layout->oobfree[0].offset;
110}
111
112static void __iomem *pmecc_get_alpha_to(struct atmel_nand_host *host)
113{
114 int table_size;
115
116 table_size = host->pmecc_sector_size == 512 ?
117 PMECC_INDEX_TABLE_SIZE_512 : PMECC_INDEX_TABLE_SIZE_1024;
118
119 /* the ALPHA lookup table is right behind the INDEX lookup table. */
120 return host->pmecc_rom_base + host->pmecc_index_table_offset +
121 table_size * sizeof(int16_t);
122}
123
Wu, Josh4e87b3152013-07-03 11:11:48 +0800124static void pmecc_data_free(struct atmel_nand_host *host)
125{
126 free(host->pmecc_partial_syn);
127 free(host->pmecc_si);
128 free(host->pmecc_lmu);
129 free(host->pmecc_smu);
130 free(host->pmecc_mu);
131 free(host->pmecc_dmu);
132 free(host->pmecc_delta);
133}
134
135static int pmecc_data_alloc(struct atmel_nand_host *host)
136{
137 const int cap = host->pmecc_corr_cap;
138 int size;
139
140 size = (2 * cap + 1) * sizeof(int16_t);
141 host->pmecc_partial_syn = malloc(size);
142 host->pmecc_si = malloc(size);
143 host->pmecc_lmu = malloc((cap + 1) * sizeof(int16_t));
144 host->pmecc_smu = malloc((cap + 2) * size);
145
146 size = (cap + 1) * sizeof(int);
147 host->pmecc_mu = malloc(size);
148 host->pmecc_dmu = malloc(size);
149 host->pmecc_delta = malloc(size);
150
151 if (host->pmecc_partial_syn &&
152 host->pmecc_si &&
153 host->pmecc_lmu &&
154 host->pmecc_smu &&
155 host->pmecc_mu &&
156 host->pmecc_dmu &&
157 host->pmecc_delta)
158 return 0;
159
160 /* error happened */
161 pmecc_data_free(host);
162 return -ENOMEM;
163
164}
165
Wu, Joshfd3091d2012-08-23 00:05:36 +0000166static void pmecc_gen_syndrome(struct mtd_info *mtd, int sector)
167{
Scott Wood17fed142016-05-30 13:57:56 -0500168 struct nand_chip *nand_chip = mtd_to_nand(mtd);
169 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000170 int i;
171 uint32_t value;
172
173 /* Fill odd syndromes */
174 for (i = 0; i < host->pmecc_corr_cap; i++) {
Wu, Joshb31868f2014-06-24 18:18:06 +0800175 value = pmecc_readl(host->pmecc, rem_port[sector].rem[i / 2]);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000176 if (i & 1)
177 value >>= 16;
178 value &= 0xffff;
179 host->pmecc_partial_syn[(2 * i) + 1] = (int16_t)value;
180 }
181}
182
183static void pmecc_substitute(struct mtd_info *mtd)
184{
Scott Wood17fed142016-05-30 13:57:56 -0500185 struct nand_chip *nand_chip = mtd_to_nand(mtd);
186 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000187 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
188 int16_t __iomem *index_of = host->pmecc_index_of;
189 int16_t *partial_syn = host->pmecc_partial_syn;
190 const int cap = host->pmecc_corr_cap;
191 int16_t *si;
192 int i, j;
193
194 /* si[] is a table that holds the current syndrome value,
195 * an element of that table belongs to the field
196 */
197 si = host->pmecc_si;
198
199 memset(&si[1], 0, sizeof(int16_t) * (2 * cap - 1));
200
201 /* Computation 2t syndromes based on S(x) */
202 /* Odd syndromes */
203 for (i = 1; i < 2 * cap; i += 2) {
204 for (j = 0; j < host->pmecc_degree; j++) {
205 if (partial_syn[i] & (0x1 << j))
206 si[i] = readw(alpha_to + i * j) ^ si[i];
207 }
208 }
209 /* Even syndrome = (Odd syndrome) ** 2 */
210 for (i = 2, j = 1; j <= cap; i = ++j << 1) {
211 if (si[j] == 0) {
212 si[i] = 0;
213 } else {
214 int16_t tmp;
215
216 tmp = readw(index_of + si[j]);
217 tmp = (tmp * 2) % host->pmecc_cw_len;
218 si[i] = readw(alpha_to + tmp);
219 }
220 }
221}
222
223/*
224 * This function defines a Berlekamp iterative procedure for
225 * finding the value of the error location polynomial.
226 * The input is si[], initialize by pmecc_substitute().
227 * The output is smu[][].
228 *
229 * This function is written according to chip datasheet Chapter:
230 * Find the Error Location Polynomial Sigma(x) of Section:
231 * Programmable Multibit ECC Control (PMECC).
232 */
233static void pmecc_get_sigma(struct mtd_info *mtd)
234{
Scott Wood17fed142016-05-30 13:57:56 -0500235 struct nand_chip *nand_chip = mtd_to_nand(mtd);
236 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000237
238 int16_t *lmu = host->pmecc_lmu;
239 int16_t *si = host->pmecc_si;
240 int *mu = host->pmecc_mu;
241 int *dmu = host->pmecc_dmu; /* Discrepancy */
242 int *delta = host->pmecc_delta; /* Delta order */
243 int cw_len = host->pmecc_cw_len;
244 const int16_t cap = host->pmecc_corr_cap;
245 const int num = 2 * cap + 1;
246 int16_t __iomem *index_of = host->pmecc_index_of;
247 int16_t __iomem *alpha_to = host->pmecc_alpha_to;
248 int i, j, k;
249 uint32_t dmu_0_count, tmp;
250 int16_t *smu = host->pmecc_smu;
251
252 /* index of largest delta */
253 int ro;
254 int largest;
255 int diff;
256
257 /* Init the Sigma(x) */
Bin Meng455ef432018-10-08 02:27:44 -0700258 memset(smu, 0, sizeof(int16_t) * num * (cap + 2));
Wu, Joshfd3091d2012-08-23 00:05:36 +0000259
260 dmu_0_count = 0;
261
262 /* First Row */
263
264 /* Mu */
265 mu[0] = -1;
266
267 smu[0] = 1;
268
269 /* discrepancy set to 1 */
270 dmu[0] = 1;
271 /* polynom order set to 0 */
272 lmu[0] = 0;
273 /* delta[0] = (mu[0] * 2 - lmu[0]) >> 1; */
274 delta[0] = -1;
275
276 /* Second Row */
277
278 /* Mu */
279 mu[1] = 0;
280 /* Sigma(x) set to 1 */
281 smu[num] = 1;
282
283 /* discrepancy set to S1 */
284 dmu[1] = si[1];
285
286 /* polynom order set to 0 */
287 lmu[1] = 0;
288
289 /* delta[1] = (mu[1] * 2 - lmu[1]) >> 1; */
290 delta[1] = 0;
291
292 for (i = 1; i <= cap; i++) {
293 mu[i + 1] = i << 1;
294 /* Begin Computing Sigma (Mu+1) and L(mu) */
295 /* check if discrepancy is set to 0 */
296 if (dmu[i] == 0) {
297 dmu_0_count++;
298
299 tmp = ((cap - (lmu[i] >> 1) - 1) / 2);
300 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
301 tmp += 2;
302 else
303 tmp += 1;
304
305 if (dmu_0_count == tmp) {
306 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
307 smu[(cap + 1) * num + j] =
308 smu[i * num + j];
309
310 lmu[cap + 1] = lmu[i];
311 return;
312 }
313
314 /* copy polynom */
315 for (j = 0; j <= lmu[i] >> 1; j++)
316 smu[(i + 1) * num + j] = smu[i * num + j];
317
318 /* copy previous polynom order to the next */
319 lmu[i + 1] = lmu[i];
320 } else {
321 ro = 0;
322 largest = -1;
323 /* find largest delta with dmu != 0 */
324 for (j = 0; j < i; j++) {
325 if ((dmu[j]) && (delta[j] > largest)) {
326 largest = delta[j];
327 ro = j;
328 }
329 }
330
331 /* compute difference */
332 diff = (mu[i] - mu[ro]);
333
334 /* Compute degree of the new smu polynomial */
335 if ((lmu[i] >> 1) > ((lmu[ro] >> 1) + diff))
336 lmu[i + 1] = lmu[i];
337 else
338 lmu[i + 1] = ((lmu[ro] >> 1) + diff) * 2;
339
340 /* Init smu[i+1] with 0 */
341 for (k = 0; k < num; k++)
342 smu[(i + 1) * num + k] = 0;
343
344 /* Compute smu[i+1] */
345 for (k = 0; k <= lmu[ro] >> 1; k++) {
346 int16_t a, b, c;
347
348 if (!(smu[ro * num + k] && dmu[i]))
349 continue;
350 a = readw(index_of + dmu[i]);
351 b = readw(index_of + dmu[ro]);
352 c = readw(index_of + smu[ro * num + k]);
353 tmp = a + (cw_len - b) + c;
354 a = readw(alpha_to + tmp % cw_len);
355 smu[(i + 1) * num + (k + diff)] = a;
356 }
357
358 for (k = 0; k <= lmu[i] >> 1; k++)
359 smu[(i + 1) * num + k] ^= smu[i * num + k];
360 }
361
362 /* End Computing Sigma (Mu+1) and L(mu) */
363 /* In either case compute delta */
364 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
365
366 /* Do not compute discrepancy for the last iteration */
367 if (i >= cap)
368 continue;
369
370 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
371 tmp = 2 * (i - 1);
372 if (k == 0) {
373 dmu[i + 1] = si[tmp + 3];
374 } else if (smu[(i + 1) * num + k] && si[tmp + 3 - k]) {
375 int16_t a, b, c;
376 a = readw(index_of +
377 smu[(i + 1) * num + k]);
378 b = si[2 * (i - 1) + 3 - k];
379 c = readw(index_of + b);
380 tmp = a + c;
381 tmp %= cw_len;
382 dmu[i + 1] = readw(alpha_to + tmp) ^
383 dmu[i + 1];
384 }
385 }
386 }
387}
388
389static int pmecc_err_location(struct mtd_info *mtd)
390{
Scott Wood17fed142016-05-30 13:57:56 -0500391 struct nand_chip *nand_chip = mtd_to_nand(mtd);
392 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000393 const int cap = host->pmecc_corr_cap;
394 const int num = 2 * cap + 1;
395 int sector_size = host->pmecc_sector_size;
396 int err_nbr = 0; /* number of error */
397 int roots_nbr; /* number of roots */
398 int i;
399 uint32_t val;
400 int16_t *smu = host->pmecc_smu;
401 int timeout = PMECC_MAX_TIMEOUT_US;
402
Wu, Joshb31868f2014-06-24 18:18:06 +0800403 pmecc_writel(host->pmerrloc, eldis, PMERRLOC_DISABLE);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000404
405 for (i = 0; i <= host->pmecc_lmu[cap + 1] >> 1; i++) {
Wu, Joshb31868f2014-06-24 18:18:06 +0800406 pmecc_writel(host->pmerrloc, sigma[i],
407 smu[(cap + 1) * num + i]);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000408 err_nbr++;
409 }
410
411 val = PMERRLOC_ELCFG_NUM_ERRORS(err_nbr - 1);
412 if (sector_size == 1024)
413 val |= PMERRLOC_ELCFG_SECTOR_1024;
414
Wu, Joshb31868f2014-06-24 18:18:06 +0800415 pmecc_writel(host->pmerrloc, elcfg, val);
416 pmecc_writel(host->pmerrloc, elen,
417 sector_size * 8 + host->pmecc_degree * cap);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000418
419 while (--timeout) {
Wu, Joshb31868f2014-06-24 18:18:06 +0800420 if (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_CALC_DONE)
Wu, Joshfd3091d2012-08-23 00:05:36 +0000421 break;
422 WATCHDOG_RESET();
423 udelay(1);
424 }
425
426 if (!timeout) {
Wu, Josha8bd1d42013-10-18 17:46:34 +0800427 dev_err(host->dev, "atmel_nand : Timeout to calculate PMECC error location\n");
Wu, Joshfd3091d2012-08-23 00:05:36 +0000428 return -1;
429 }
430
Wu, Joshb31868f2014-06-24 18:18:06 +0800431 roots_nbr = (pmecc_readl(host->pmerrloc, elisr) & PMERRLOC_ERR_NUM_MASK)
Wu, Joshfd3091d2012-08-23 00:05:36 +0000432 >> 8;
433 /* Number of roots == degree of smu hence <= cap */
434 if (roots_nbr == host->pmecc_lmu[cap + 1] >> 1)
435 return err_nbr - 1;
436
437 /* Number of roots does not match the degree of smu
438 * unable to correct error */
439 return -1;
440}
441
442static void pmecc_correct_data(struct mtd_info *mtd, uint8_t *buf, uint8_t *ecc,
443 int sector_num, int extra_bytes, int err_nbr)
444{
Scott Wood17fed142016-05-30 13:57:56 -0500445 struct nand_chip *nand_chip = mtd_to_nand(mtd);
446 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000447 int i = 0;
448 int byte_pos, bit_pos, sector_size, pos;
449 uint32_t tmp;
450 uint8_t err_byte;
451
452 sector_size = host->pmecc_sector_size;
453
454 while (err_nbr) {
Wu, Joshb31868f2014-06-24 18:18:06 +0800455 tmp = pmecc_readl(host->pmerrloc, el[i]) - 1;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000456 byte_pos = tmp / 8;
457 bit_pos = tmp % 8;
458
459 if (byte_pos >= (sector_size + extra_bytes))
460 BUG(); /* should never happen */
461
462 if (byte_pos < sector_size) {
463 err_byte = *(buf + byte_pos);
464 *(buf + byte_pos) ^= (1 << bit_pos);
465
466 pos = sector_num * host->pmecc_sector_size + byte_pos;
Wu, Josh04534022013-10-18 17:46:33 +0800467 dev_dbg(host->dev, "Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Wu, Joshfd3091d2012-08-23 00:05:36 +0000468 pos, bit_pos, err_byte, *(buf + byte_pos));
469 } else {
470 /* Bit flip in OOB area */
471 tmp = sector_num * host->pmecc_bytes_per_sector
472 + (byte_pos - sector_size);
473 err_byte = ecc[tmp];
474 ecc[tmp] ^= (1 << bit_pos);
475
476 pos = tmp + nand_chip->ecc.layout->eccpos[0];
Wu, Josh04534022013-10-18 17:46:33 +0800477 dev_dbg(host->dev, "Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
Wu, Joshfd3091d2012-08-23 00:05:36 +0000478 pos, bit_pos, err_byte, ecc[tmp]);
479 }
480
481 i++;
482 err_nbr--;
483 }
484
485 return;
486}
487
488static int pmecc_correction(struct mtd_info *mtd, u32 pmecc_stat, uint8_t *buf,
489 u8 *ecc)
490{
Scott Wood17fed142016-05-30 13:57:56 -0500491 struct nand_chip *nand_chip = mtd_to_nand(mtd);
492 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000493 int i, err_nbr, eccbytes;
494 uint8_t *buf_pos;
495
Wu, Josh1f5c0892015-01-16 11:54:46 +0800496 /* SAMA5D4 PMECC IP can correct errors for all 0xff page */
497 if (host->pmecc_version >= PMECC_VERSION_SAMA5D4)
498 goto normal_check;
499
Wu, Joshfd3091d2012-08-23 00:05:36 +0000500 eccbytes = nand_chip->ecc.bytes;
501 for (i = 0; i < eccbytes; i++)
502 if (ecc[i] != 0xff)
503 goto normal_check;
504 /* Erased page, return OK */
505 return 0;
506
507normal_check:
508 for (i = 0; i < host->pmecc_sector_number; i++) {
509 err_nbr = 0;
510 if (pmecc_stat & 0x1) {
511 buf_pos = buf + i * host->pmecc_sector_size;
512
513 pmecc_gen_syndrome(mtd, i);
514 pmecc_substitute(mtd);
515 pmecc_get_sigma(mtd);
516
517 err_nbr = pmecc_err_location(mtd);
518 if (err_nbr == -1) {
Wu, Josha8bd1d42013-10-18 17:46:34 +0800519 dev_err(host->dev, "PMECC: Too many errors\n");
Wu, Joshfd3091d2012-08-23 00:05:36 +0000520 mtd->ecc_stats.failed++;
Scott Wood52ab7ce2016-05-30 13:57:58 -0500521 return -EBADMSG;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000522 } else {
523 pmecc_correct_data(mtd, buf_pos, ecc, i,
524 host->pmecc_bytes_per_sector, err_nbr);
525 mtd->ecc_stats.corrected += err_nbr;
526 }
527 }
528 pmecc_stat >>= 1;
529 }
530
531 return 0;
532}
533
534static int atmel_nand_pmecc_read_page(struct mtd_info *mtd,
Sergey Lapin3a38a552013-01-14 03:46:50 +0000535 struct nand_chip *chip, uint8_t *buf, int oob_required, int page)
Wu, Joshfd3091d2012-08-23 00:05:36 +0000536{
Scott Wood17fed142016-05-30 13:57:56 -0500537 struct atmel_nand_host *host = nand_get_controller_data(chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000538 int eccsize = chip->ecc.size;
539 uint8_t *oob = chip->oob_poi;
540 uint32_t *eccpos = chip->ecc.layout->eccpos;
541 uint32_t stat;
542 int timeout = PMECC_MAX_TIMEOUT_US;
543
544 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
545 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
546 pmecc_writel(host->pmecc, cfg, ((pmecc_readl(host->pmecc, cfg))
547 & ~PMECC_CFG_WRITE_OP) | PMECC_CFG_AUTO_ENABLE);
548
549 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
550 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
551
552 chip->read_buf(mtd, buf, eccsize);
553 chip->read_buf(mtd, oob, mtd->oobsize);
554
555 while (--timeout) {
556 if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
557 break;
558 WATCHDOG_RESET();
559 udelay(1);
560 }
561
562 if (!timeout) {
Wu, Josha8bd1d42013-10-18 17:46:34 +0800563 dev_err(host->dev, "atmel_nand : Timeout to read PMECC page\n");
Wu, Joshfd3091d2012-08-23 00:05:36 +0000564 return -1;
565 }
566
567 stat = pmecc_readl(host->pmecc, isr);
568 if (stat != 0)
569 if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
Scott Wood52ab7ce2016-05-30 13:57:58 -0500570 return -EBADMSG;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000571
572 return 0;
573}
574
Sergey Lapin3a38a552013-01-14 03:46:50 +0000575static int atmel_nand_pmecc_write_page(struct mtd_info *mtd,
576 struct nand_chip *chip, const uint8_t *buf,
Scott Wood46e13102016-05-30 13:57:57 -0500577 int oob_required, int page)
Wu, Joshfd3091d2012-08-23 00:05:36 +0000578{
Scott Wood17fed142016-05-30 13:57:56 -0500579 struct atmel_nand_host *host = nand_get_controller_data(chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000580 uint32_t *eccpos = chip->ecc.layout->eccpos;
581 int i, j;
582 int timeout = PMECC_MAX_TIMEOUT_US;
583
584 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
585 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
586
587 pmecc_writel(host->pmecc, cfg, (pmecc_readl(host->pmecc, cfg) |
588 PMECC_CFG_WRITE_OP) & ~PMECC_CFG_AUTO_ENABLE);
589
590 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
591 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DATA);
592
593 chip->write_buf(mtd, (u8 *)buf, mtd->writesize);
594
595 while (--timeout) {
596 if (!(pmecc_readl(host->pmecc, sr) & PMECC_SR_BUSY))
597 break;
598 WATCHDOG_RESET();
599 udelay(1);
600 }
601
602 if (!timeout) {
Wu, Josha8bd1d42013-10-18 17:46:34 +0800603 dev_err(host->dev, "atmel_nand : Timeout to read PMECC status, fail to write PMECC in oob\n");
Sergey Lapin3a38a552013-01-14 03:46:50 +0000604 goto out;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000605 }
606
607 for (i = 0; i < host->pmecc_sector_number; i++) {
608 for (j = 0; j < host->pmecc_bytes_per_sector; j++) {
609 int pos;
610
611 pos = i * host->pmecc_bytes_per_sector + j;
612 chip->oob_poi[eccpos[pos]] =
Wu, Joshb31868f2014-06-24 18:18:06 +0800613 pmecc_readb(host->pmecc, ecc_port[i].ecc[j]);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000614 }
615 }
616 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Sergey Lapin3a38a552013-01-14 03:46:50 +0000617out:
618 return 0;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000619}
620
621static void atmel_pmecc_core_init(struct mtd_info *mtd)
622{
Scott Wood17fed142016-05-30 13:57:56 -0500623 struct nand_chip *nand_chip = mtd_to_nand(mtd);
624 struct atmel_nand_host *host = nand_get_controller_data(nand_chip);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000625 uint32_t val = 0;
626 struct nand_ecclayout *ecc_layout;
627
628 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_RST);
629 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_DISABLE);
630
631 switch (host->pmecc_corr_cap) {
632 case 2:
633 val = PMECC_CFG_BCH_ERR2;
634 break;
635 case 4:
636 val = PMECC_CFG_BCH_ERR4;
637 break;
638 case 8:
639 val = PMECC_CFG_BCH_ERR8;
640 break;
641 case 12:
642 val = PMECC_CFG_BCH_ERR12;
643 break;
644 case 24:
645 val = PMECC_CFG_BCH_ERR24;
646 break;
Josh Wuce764952015-11-24 16:34:01 +0800647 case 32:
648 val = PMECC_CFG_BCH_ERR32;
649 break;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000650 }
651
652 if (host->pmecc_sector_size == 512)
653 val |= PMECC_CFG_SECTOR512;
654 else if (host->pmecc_sector_size == 1024)
655 val |= PMECC_CFG_SECTOR1024;
656
657 switch (host->pmecc_sector_number) {
658 case 1:
659 val |= PMECC_CFG_PAGE_1SECTOR;
660 break;
661 case 2:
662 val |= PMECC_CFG_PAGE_2SECTORS;
663 break;
664 case 4:
665 val |= PMECC_CFG_PAGE_4SECTORS;
666 break;
667 case 8:
668 val |= PMECC_CFG_PAGE_8SECTORS;
669 break;
670 }
671
672 val |= (PMECC_CFG_READ_OP | PMECC_CFG_SPARE_DISABLE
673 | PMECC_CFG_AUTO_DISABLE);
674 pmecc_writel(host->pmecc, cfg, val);
675
676 ecc_layout = nand_chip->ecc.layout;
677 pmecc_writel(host->pmecc, sarea, mtd->oobsize - 1);
678 pmecc_writel(host->pmecc, saddr, ecc_layout->eccpos[0]);
679 pmecc_writel(host->pmecc, eaddr,
680 ecc_layout->eccpos[ecc_layout->eccbytes - 1]);
681 /* See datasheet about PMECC Clock Control Register */
682 pmecc_writel(host->pmecc, clk, PMECC_CLK_133MHZ);
683 pmecc_writel(host->pmecc, idr, 0xff);
684 pmecc_writel(host->pmecc, ctrl, PMECC_CTRL_ENABLE);
685}
686
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800687#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
688/*
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800689 * pmecc_choose_ecc - Get ecc requirement from ONFI parameters. If
690 * pmecc_corr_cap or pmecc_sector_size is 0, then set it as
691 * ONFI ECC parameters.
692 * @host: point to an atmel_nand_host structure.
693 * if host->pmecc_corr_cap is 0 then set it as the ONFI ecc_bits.
694 * if host->pmecc_sector_size is 0 then set it as the ONFI sector_size.
695 * @chip: point to an nand_chip structure.
696 * @cap: store the ONFI ECC correct bits capbility
697 * @sector_size: in how many bytes that ONFI require to correct @ecc_bits
698 *
699 * Return 0 if success. otherwise return the error code.
700 */
701static int pmecc_choose_ecc(struct atmel_nand_host *host,
702 struct nand_chip *chip,
703 int *cap, int *sector_size)
704{
705 /* Get ECC requirement from ONFI parameters */
706 *cap = *sector_size = 0;
707 if (chip->onfi_version) {
Josh Wuc90cc682016-01-25 14:06:33 +0800708 *cap = chip->ecc_strength_ds;
709 *sector_size = chip->ecc_step_ds;
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900710 pr_debug("ONFI params, minimum required ECC: %d bits in %d bytes\n",
Josh Wuc90cc682016-01-25 14:06:33 +0800711 *cap, *sector_size);
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800712 }
Josh Wuc90cc682016-01-25 14:06:33 +0800713
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800714 if (*cap == 0 && *sector_size == 0) {
Josh Wuc90cc682016-01-25 14:06:33 +0800715 /* Non-ONFI compliant */
716 dev_info(host->dev, "NAND chip is not ONFI compliant, assume ecc_bits is 2 in 512 bytes\n");
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800717 *cap = 2;
718 *sector_size = 512;
719 }
720
721 /* If head file doesn't specify then use the one in ONFI parameters */
722 if (host->pmecc_corr_cap == 0) {
723 /* use the most fitable ecc bits (the near bigger one ) */
724 if (*cap <= 2)
725 host->pmecc_corr_cap = 2;
726 else if (*cap <= 4)
727 host->pmecc_corr_cap = 4;
728 else if (*cap <= 8)
729 host->pmecc_corr_cap = 8;
730 else if (*cap <= 12)
731 host->pmecc_corr_cap = 12;
732 else if (*cap <= 24)
733 host->pmecc_corr_cap = 24;
734 else
Josh Wuce764952015-11-24 16:34:01 +0800735#ifdef CONFIG_SAMA5D2
736 host->pmecc_corr_cap = 32;
737#else
738 host->pmecc_corr_cap = 24;
739#endif
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800740 }
741 if (host->pmecc_sector_size == 0) {
742 /* use the most fitable sector size (the near smaller one ) */
743 if (*sector_size >= 1024)
744 host->pmecc_sector_size = 1024;
745 else if (*sector_size >= 512)
746 host->pmecc_sector_size = 512;
747 else
748 return -EINVAL;
749 }
750 return 0;
751}
752#endif
753
Josh Wuf259ad22014-11-10 15:24:00 +0800754#if defined(NO_GALOIS_TABLE_IN_ROM)
755static uint16_t *pmecc_galois_table;
756static inline int deg(unsigned int poly)
757{
758 /* polynomial degree is the most-significant bit index */
759 return fls(poly) - 1;
760}
761
762static int build_gf_tables(int mm, unsigned int poly,
763 int16_t *index_of, int16_t *alpha_to)
764{
765 unsigned int i, x = 1;
766 const unsigned int k = 1 << deg(poly);
767 unsigned int nn = (1 << mm) - 1;
768
769 /* primitive polynomial must be of degree m */
770 if (k != (1u << mm))
771 return -EINVAL;
772
773 for (i = 0; i < nn; i++) {
774 alpha_to[i] = x;
775 index_of[x] = i;
776 if (i && (x == 1))
777 /* polynomial is not primitive (a^i=1 with 0<i<2^m-1) */
778 return -EINVAL;
779 x <<= 1;
780 if (x & k)
781 x ^= poly;
782 }
783
784 alpha_to[nn] = 1;
785 index_of[0] = 0;
786
787 return 0;
788}
789
790static uint16_t *create_lookup_table(int sector_size)
791{
792 int degree = (sector_size == 512) ?
793 PMECC_GF_DIMENSION_13 :
794 PMECC_GF_DIMENSION_14;
795 unsigned int poly = (sector_size == 512) ?
796 PMECC_GF_13_PRIMITIVE_POLY :
797 PMECC_GF_14_PRIMITIVE_POLY;
798 int table_size = (sector_size == 512) ?
799 PMECC_INDEX_TABLE_SIZE_512 :
800 PMECC_INDEX_TABLE_SIZE_1024;
801
802 int16_t *addr = kzalloc(2 * table_size * sizeof(uint16_t), GFP_KERNEL);
803 if (addr && build_gf_tables(degree, poly, addr, addr + table_size))
804 return NULL;
805
806 return (uint16_t *)addr;
807}
808#endif
809
Wu, Joshfd3091d2012-08-23 00:05:36 +0000810static int atmel_pmecc_nand_init_params(struct nand_chip *nand,
811 struct mtd_info *mtd)
812{
813 struct atmel_nand_host *host;
814 int cap, sector_size;
815
Scott Wood17fed142016-05-30 13:57:56 -0500816 host = &pmecc_host;
817 nand_set_controller_data(nand, host);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000818
819 nand->ecc.mode = NAND_ECC_HW;
820 nand->ecc.calculate = NULL;
821 nand->ecc.correct = NULL;
822 nand->ecc.hwctl = NULL;
823
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800824#ifdef CONFIG_SYS_NAND_ONFI_DETECTION
825 host->pmecc_corr_cap = host->pmecc_sector_size = 0;
826
827#ifdef CONFIG_PMECC_CAP
828 host->pmecc_corr_cap = CONFIG_PMECC_CAP;
829#endif
830#ifdef CONFIG_PMECC_SECTOR_SIZE
831 host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
832#endif
833 /* Get ECC requirement of ONFI parameters. And if CONFIG_PMECC_CAP or
834 * CONFIG_PMECC_SECTOR_SIZE not defined, then use ecc_bits, sector_size
835 * from ONFI.
836 */
837 if (pmecc_choose_ecc(host, nand, &cap, &sector_size)) {
Josh Wudaf40882016-01-25 14:06:34 +0800838 dev_err(host->dev, "Required ECC %d bits in %d bytes not supported!\n",
839 cap, sector_size);
Wu, Josh3b4c7f62013-07-04 15:36:23 +0800840 return -EINVAL;
841 }
842
843 if (cap > host->pmecc_corr_cap)
844 dev_info(host->dev, "WARNING: Using different ecc correct bits(%d bit) from Nand ONFI ECC reqirement (%d bit).\n",
845 host->pmecc_corr_cap, cap);
846 if (sector_size < host->pmecc_sector_size)
847 dev_info(host->dev, "WARNING: Using different ecc correct sector size (%d bytes) from Nand ONFI ECC reqirement (%d bytes).\n",
848 host->pmecc_sector_size, sector_size);
849#else /* CONFIG_SYS_NAND_ONFI_DETECTION */
850 host->pmecc_corr_cap = CONFIG_PMECC_CAP;
851 host->pmecc_sector_size = CONFIG_PMECC_SECTOR_SIZE;
852#endif
853
854 cap = host->pmecc_corr_cap;
855 sector_size = host->pmecc_sector_size;
856
857 /* TODO: need check whether cap & sector_size is validate */
Josh Wuf259ad22014-11-10 15:24:00 +0800858#if defined(NO_GALOIS_TABLE_IN_ROM)
859 /*
860 * As pmecc_rom_base is the begin of the gallois field table, So the
861 * index offset just set as 0.
862 */
863 host->pmecc_index_table_offset = 0;
864#else
Wu, Joshb45c9492013-07-03 11:11:45 +0800865 if (host->pmecc_sector_size == 512)
866 host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_512;
867 else
868 host->pmecc_index_table_offset = ATMEL_PMECC_INDEX_OFFSET_1024;
Josh Wuf259ad22014-11-10 15:24:00 +0800869#endif
Wu, Joshfd3091d2012-08-23 00:05:36 +0000870
Masahiro Yamadaf8a5d512017-10-18 00:10:48 +0900871 pr_debug("Initialize PMECC params, cap: %d, sector: %d\n",
872 cap, sector_size);
Wu, Joshfd3091d2012-08-23 00:05:36 +0000873
874 host->pmecc = (struct pmecc_regs __iomem *) ATMEL_BASE_PMECC;
875 host->pmerrloc = (struct pmecc_errloc_regs __iomem *)
876 ATMEL_BASE_PMERRLOC;
Josh Wuf259ad22014-11-10 15:24:00 +0800877#if defined(NO_GALOIS_TABLE_IN_ROM)
878 pmecc_galois_table = create_lookup_table(host->pmecc_sector_size);
879 if (!pmecc_galois_table) {
880 dev_err(host->dev, "out of memory\n");
881 return -ENOMEM;
882 }
883
884 host->pmecc_rom_base = (void __iomem *)pmecc_galois_table;
885#else
Wu, Joshfd3091d2012-08-23 00:05:36 +0000886 host->pmecc_rom_base = (void __iomem *) ATMEL_BASE_ROM;
Josh Wuf259ad22014-11-10 15:24:00 +0800887#endif
Wu, Joshfd3091d2012-08-23 00:05:36 +0000888
889 /* ECC is calculated for the whole page (1 step) */
890 nand->ecc.size = mtd->writesize;
891
892 /* set ECC page size and oob layout */
893 switch (mtd->writesize) {
894 case 2048:
895 case 4096:
Wu, Joshf9f69b12013-10-18 17:46:31 +0800896 case 8192:
Wu, Josh89bdc8e2013-08-23 15:09:05 +0800897 host->pmecc_degree = (sector_size == 512) ?
898 PMECC_GF_DIMENSION_13 : PMECC_GF_DIMENSION_14;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000899 host->pmecc_cw_len = (1 << host->pmecc_degree) - 1;
900 host->pmecc_sector_number = mtd->writesize / sector_size;
901 host->pmecc_bytes_per_sector = pmecc_get_ecc_bytes(
902 cap, sector_size);
903 host->pmecc_alpha_to = pmecc_get_alpha_to(host);
904 host->pmecc_index_of = host->pmecc_rom_base +
905 host->pmecc_index_table_offset;
906
907 nand->ecc.steps = 1;
908 nand->ecc.bytes = host->pmecc_bytes_per_sector *
909 host->pmecc_sector_number;
Wu, Joshf9f69b12013-10-18 17:46:31 +0800910
911 if (nand->ecc.bytes > MTD_MAX_ECCPOS_ENTRIES_LARGE) {
912 dev_err(host->dev, "too large eccpos entries. max support ecc.bytes is %d\n",
913 MTD_MAX_ECCPOS_ENTRIES_LARGE);
914 return -EINVAL;
915 }
916
Josh Wu5d3256c2016-01-25 14:06:35 +0800917 if (nand->ecc.bytes > mtd->oobsize - PMECC_OOB_RESERVED_BYTES) {
Wu, Josha8bd1d42013-10-18 17:46:34 +0800918 dev_err(host->dev, "No room for ECC bytes\n");
Wu, Joshfd3091d2012-08-23 00:05:36 +0000919 return -EINVAL;
920 }
921 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
922 mtd->oobsize,
923 nand->ecc.bytes);
924 nand->ecc.layout = &atmel_pmecc_oobinfo;
925 break;
926 case 512:
927 case 1024:
928 /* TODO */
Wu, Josha8bd1d42013-10-18 17:46:34 +0800929 dev_err(host->dev, "Unsupported page size for PMECC, use Software ECC\n");
Wu, Joshfd3091d2012-08-23 00:05:36 +0000930 default:
931 /* page size not handled by HW ECC */
932 /* switching back to soft ECC */
933 nand->ecc.mode = NAND_ECC_SOFT;
934 nand->ecc.read_page = NULL;
935 nand->ecc.postpad = 0;
936 nand->ecc.prepad = 0;
937 nand->ecc.bytes = 0;
938 return 0;
939 }
940
Wu, Josh4e87b3152013-07-03 11:11:48 +0800941 /* Allocate data for PMECC computation */
942 if (pmecc_data_alloc(host)) {
943 dev_err(host->dev, "Cannot allocate memory for PMECC computation!\n");
944 return -ENOMEM;
945 }
946
Boris BREZILLONd7915f42014-09-02 10:23:09 +0200947 nand->options |= NAND_NO_SUBPAGE_WRITE;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000948 nand->ecc.read_page = atmel_nand_pmecc_read_page;
949 nand->ecc.write_page = atmel_nand_pmecc_write_page;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000950 nand->ecc.strength = cap;
Wu, Joshfd3091d2012-08-23 00:05:36 +0000951
Wu, Josh1f5c0892015-01-16 11:54:46 +0800952 /* Check the PMECC ip version */
953 host->pmecc_version = pmecc_readl(host->pmerrloc, version);
954 dev_dbg(host->dev, "PMECC IP version is: %x\n", host->pmecc_version);
955
Wu, Joshfd3091d2012-08-23 00:05:36 +0000956 atmel_pmecc_core_init(mtd);
957
958 return 0;
959}
960
961#else
962
Nikolay Petukhove6015ca2010-03-19 10:49:27 +0500963/* oob layout for large page size
964 * bad block info is on bytes 0 and 1
965 * the bytes have to be consecutives to avoid
966 * several NAND_CMD_RNDOUT during read
967 */
968static struct nand_ecclayout atmel_oobinfo_large = {
969 .eccbytes = 4,
970 .eccpos = {60, 61, 62, 63},
971 .oobfree = {
972 {2, 58}
973 },
974};
975
976/* oob layout for small page size
977 * bad block info is on bytes 4 and 5
978 * the bytes have to be consecutives to avoid
979 * several NAND_CMD_RNDOUT during read
980 */
981static struct nand_ecclayout atmel_oobinfo_small = {
982 .eccbytes = 4,
983 .eccpos = {0, 1, 2, 3},
984 .oobfree = {
985 {6, 10}
986 },
987};
988
989/*
990 * Calculate HW ECC
991 *
992 * function called after a write
993 *
994 * mtd: MTD block structure
995 * dat: raw data (unused)
996 * ecc_code: buffer for ECC
997 */
998static int atmel_nand_calculate(struct mtd_info *mtd,
999 const u_char *dat, unsigned char *ecc_code)
1000{
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001001 unsigned int ecc_value;
1002
1003 /* get the first 2 ECC bytes */
1004 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR);
1005
1006 ecc_code[0] = ecc_value & 0xFF;
1007 ecc_code[1] = (ecc_value >> 8) & 0xFF;
1008
1009 /* get the last 2 ECC bytes */
1010 ecc_value = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, NPR) & ATMEL_ECC_NPARITY;
1011
1012 ecc_code[2] = ecc_value & 0xFF;
1013 ecc_code[3] = (ecc_value >> 8) & 0xFF;
1014
1015 return 0;
1016}
1017
1018/*
1019 * HW ECC read page function
1020 *
1021 * mtd: mtd info structure
1022 * chip: nand chip info structure
1023 * buf: buffer to store read data
Sergey Lapin3a38a552013-01-14 03:46:50 +00001024 * oob_required: caller expects OOB data read to chip->oob_poi
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001025 */
Sergey Lapin3a38a552013-01-14 03:46:50 +00001026static int atmel_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
1027 uint8_t *buf, int oob_required, int page)
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001028{
1029 int eccsize = chip->ecc.size;
1030 int eccbytes = chip->ecc.bytes;
1031 uint32_t *eccpos = chip->ecc.layout->eccpos;
1032 uint8_t *p = buf;
1033 uint8_t *oob = chip->oob_poi;
1034 uint8_t *ecc_pos;
1035 int stat;
1036
1037 /* read the page */
1038 chip->read_buf(mtd, p, eccsize);
1039
1040 /* move to ECC position if needed */
1041 if (eccpos[0] != 0) {
1042 /* This only works on large pages
1043 * because the ECC controller waits for
1044 * NAND_CMD_RNDOUTSTART after the
1045 * NAND_CMD_RNDOUT.
1046 * anyway, for small pages, the eccpos[0] == 0
1047 */
1048 chip->cmdfunc(mtd, NAND_CMD_RNDOUT,
1049 mtd->writesize + eccpos[0], -1);
1050 }
1051
1052 /* the ECC controller needs to read the ECC just after the data */
1053 ecc_pos = oob + eccpos[0];
1054 chip->read_buf(mtd, ecc_pos, eccbytes);
1055
1056 /* check if there's an error */
1057 stat = chip->ecc.correct(mtd, p, oob, NULL);
1058
1059 if (stat < 0)
1060 mtd->ecc_stats.failed++;
1061 else
1062 mtd->ecc_stats.corrected += stat;
1063
1064 /* get back to oob start (end of page) */
1065 chip->cmdfunc(mtd, NAND_CMD_RNDOUT, mtd->writesize, -1);
1066
1067 /* read the oob */
1068 chip->read_buf(mtd, oob, mtd->oobsize);
1069
1070 return 0;
1071}
1072
1073/*
1074 * HW ECC Correction
1075 *
1076 * function called after a read
1077 *
1078 * mtd: MTD block structure
1079 * dat: raw data read from the chip
1080 * read_ecc: ECC from the chip (unused)
1081 * isnull: unused
1082 *
1083 * Detect and correct a 1 bit error for a page
1084 */
1085static int atmel_nand_correct(struct mtd_info *mtd, u_char *dat,
1086 u_char *read_ecc, u_char *isnull)
1087{
Scott Wood17fed142016-05-30 13:57:56 -05001088 struct nand_chip *nand_chip = mtd_to_nand(mtd);
Wu, Josh1586d1b2012-08-23 00:05:35 +00001089 unsigned int ecc_status;
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001090 unsigned int ecc_word, ecc_bit;
1091
1092 /* get the status from the Status Register */
1093 ecc_status = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, SR);
1094
1095 /* if there's no error */
1096 if (likely(!(ecc_status & ATMEL_ECC_RECERR)))
1097 return 0;
1098
1099 /* get error bit offset (4 bits) */
1100 ecc_bit = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_BITADDR;
1101 /* get word address (12 bits) */
1102 ecc_word = ecc_readl(CONFIG_SYS_NAND_ECC_BASE, PR) & ATMEL_ECC_WORDADDR;
1103 ecc_word >>= 4;
1104
1105 /* if there are multiple errors */
1106 if (ecc_status & ATMEL_ECC_MULERR) {
1107 /* check if it is a freshly erased block
1108 * (filled with 0xff) */
1109 if ((ecc_bit == ATMEL_ECC_BITADDR)
1110 && (ecc_word == (ATMEL_ECC_WORDADDR >> 4))) {
1111 /* the block has just been erased, return OK */
1112 return 0;
1113 }
1114 /* it doesn't seems to be a freshly
1115 * erased block.
1116 * We can't correct so many errors */
Wu, Josha8bd1d42013-10-18 17:46:34 +08001117 dev_warn(host->dev, "atmel_nand : multiple errors detected."
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001118 " Unable to correct.\n");
Scott Wood52ab7ce2016-05-30 13:57:58 -05001119 return -EBADMSG;
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001120 }
1121
1122 /* if there's a single bit error : we can correct it */
1123 if (ecc_status & ATMEL_ECC_ECCERR) {
1124 /* there's nothing much to do here.
1125 * the bit error is on the ECC itself.
1126 */
Wu, Josha8bd1d42013-10-18 17:46:34 +08001127 dev_warn(host->dev, "atmel_nand : one bit error on ECC code."
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001128 " Nothing to correct\n");
1129 return 0;
1130 }
1131
Wu, Josha8bd1d42013-10-18 17:46:34 +08001132 dev_warn(host->dev, "atmel_nand : one bit error on data."
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001133 " (word offset in the page :"
1134 " 0x%x bit offset : 0x%x)\n",
1135 ecc_word, ecc_bit);
1136 /* correct the error */
1137 if (nand_chip->options & NAND_BUSWIDTH_16) {
1138 /* 16 bits words */
1139 ((unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1140 } else {
1141 /* 8 bits words */
1142 dat[ecc_word] ^= (1 << ecc_bit);
1143 }
Wu, Josha8bd1d42013-10-18 17:46:34 +08001144 dev_warn(host->dev, "atmel_nand : error corrected\n");
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001145 return 1;
1146}
1147
1148/*
1149 * Enable HW ECC : unused on most chips
1150 */
1151static void atmel_nand_hwctl(struct mtd_info *mtd, int mode)
1152{
1153}
Wu, Josh6cded6d2012-08-23 00:05:34 +00001154
1155int atmel_hwecc_nand_init_param(struct nand_chip *nand, struct mtd_info *mtd)
1156{
1157 nand->ecc.mode = NAND_ECC_HW;
1158 nand->ecc.calculate = atmel_nand_calculate;
1159 nand->ecc.correct = atmel_nand_correct;
1160 nand->ecc.hwctl = atmel_nand_hwctl;
1161 nand->ecc.read_page = atmel_nand_read_page;
1162 nand->ecc.bytes = 4;
Andre Renaudeaf23212016-05-05 07:28:15 -06001163 nand->ecc.strength = 4;
Wu, Josh6cded6d2012-08-23 00:05:34 +00001164
1165 if (nand->ecc.mode == NAND_ECC_HW) {
1166 /* ECC is calculated for the whole page (1 step) */
1167 nand->ecc.size = mtd->writesize;
1168
1169 /* set ECC page size and oob layout */
1170 switch (mtd->writesize) {
1171 case 512:
1172 nand->ecc.layout = &atmel_oobinfo_small;
1173 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1174 ATMEL_ECC_PAGESIZE_528);
1175 break;
1176 case 1024:
1177 nand->ecc.layout = &atmel_oobinfo_large;
1178 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1179 ATMEL_ECC_PAGESIZE_1056);
1180 break;
1181 case 2048:
1182 nand->ecc.layout = &atmel_oobinfo_large;
1183 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1184 ATMEL_ECC_PAGESIZE_2112);
1185 break;
1186 case 4096:
1187 nand->ecc.layout = &atmel_oobinfo_large;
1188 ecc_writel(CONFIG_SYS_NAND_ECC_BASE, MR,
1189 ATMEL_ECC_PAGESIZE_4224);
1190 break;
1191 default:
1192 /* page size not handled by HW ECC */
1193 /* switching back to soft ECC */
1194 nand->ecc.mode = NAND_ECC_SOFT;
1195 nand->ecc.calculate = NULL;
1196 nand->ecc.correct = NULL;
1197 nand->ecc.hwctl = NULL;
1198 nand->ecc.read_page = NULL;
1199 nand->ecc.postpad = 0;
1200 nand->ecc.prepad = 0;
1201 nand->ecc.bytes = 0;
1202 break;
1203 }
1204 }
1205
1206 return 0;
1207}
1208
Wu, Joshfd3091d2012-08-23 00:05:36 +00001209#endif /* CONFIG_ATMEL_NAND_HW_PMECC */
1210
1211#endif /* CONFIG_ATMEL_NAND_HWECC */
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001212
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001213static void at91_nand_hwcontrol(struct mtd_info *mtd,
Sergey Lapin77e524c2008-10-31 12:28:43 +01001214 int cmd, unsigned int ctrl)
1215{
Scott Wood17fed142016-05-30 13:57:56 -05001216 struct nand_chip *this = mtd_to_nand(mtd);
Sergey Lapin77e524c2008-10-31 12:28:43 +01001217
1218 if (ctrl & NAND_CTRL_CHANGE) {
1219 ulong IO_ADDR_W = (ulong) this->IO_ADDR_W;
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001220 IO_ADDR_W &= ~(CONFIG_SYS_NAND_MASK_ALE
1221 | CONFIG_SYS_NAND_MASK_CLE);
Sergey Lapin77e524c2008-10-31 12:28:43 +01001222
1223 if (ctrl & NAND_CLE)
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001224 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_CLE;
Sergey Lapin77e524c2008-10-31 12:28:43 +01001225 if (ctrl & NAND_ALE)
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001226 IO_ADDR_W |= CONFIG_SYS_NAND_MASK_ALE;
Sergey Lapin77e524c2008-10-31 12:28:43 +01001227
michael4b5cef72011-03-14 21:16:38 +00001228#ifdef CONFIG_SYS_NAND_ENABLE_PIN
Wenyou Yangd4aab6b2017-03-23 12:55:21 +08001229 at91_set_gpio_value(CONFIG_SYS_NAND_ENABLE_PIN,
1230 !(ctrl & NAND_NCE));
michael4b5cef72011-03-14 21:16:38 +00001231#endif
Sergey Lapin77e524c2008-10-31 12:28:43 +01001232 this->IO_ADDR_W = (void *) IO_ADDR_W;
1233 }
1234
1235 if (cmd != NAND_CMD_NONE)
1236 writeb(cmd, this->IO_ADDR_W);
1237}
1238
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001239#ifdef CONFIG_SYS_NAND_READY_PIN
1240static int at91_nand_ready(struct mtd_info *mtd)
Sergey Lapin77e524c2008-10-31 12:28:43 +01001241{
Wenyou Yangd4aab6b2017-03-23 12:55:21 +08001242 return at91_get_gpio_value(CONFIG_SYS_NAND_READY_PIN);
Sergey Lapin77e524c2008-10-31 12:28:43 +01001243}
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001244#endif
Sergey Lapin77e524c2008-10-31 12:28:43 +01001245
Bo Shen9415b872014-03-03 14:47:16 +08001246#ifdef CONFIG_SPL_BUILD
1247/* The following code is for SPL */
Scott Wood2c1b7e12016-05-30 13:57:55 -05001248static struct mtd_info *mtd;
Bo Shen9415b872014-03-03 14:47:16 +08001249static struct nand_chip nand_chip;
1250
1251static int nand_command(int block, int page, uint32_t offs, u8 cmd)
1252{
Scott Wood17fed142016-05-30 13:57:56 -05001253 struct nand_chip *this = mtd_to_nand(mtd);
Bo Shen9415b872014-03-03 14:47:16 +08001254 int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
1255 void (*hwctrl)(struct mtd_info *mtd, int cmd,
1256 unsigned int ctrl) = this->cmd_ctrl;
1257
Scott Wood2c1b7e12016-05-30 13:57:55 -05001258 while (!this->dev_ready(mtd))
Bo Shen9415b872014-03-03 14:47:16 +08001259 ;
1260
1261 if (cmd == NAND_CMD_READOOB) {
1262 offs += CONFIG_SYS_NAND_PAGE_SIZE;
1263 cmd = NAND_CMD_READ0;
1264 }
1265
Scott Wood2c1b7e12016-05-30 13:57:55 -05001266 hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Bo Shen9415b872014-03-03 14:47:16 +08001267
Brian Norris67675222014-05-06 00:46:17 +05301268 if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
Bo Shen9415b872014-03-03 14:47:16 +08001269 offs >>= 1;
1270
Scott Wood2c1b7e12016-05-30 13:57:55 -05001271 hwctrl(mtd, offs & 0xff, NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1272 hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE);
1273 hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE);
1274 hwctrl(mtd, ((page_addr >> 8) & 0xff), NAND_CTRL_ALE);
Bo Shen9415b872014-03-03 14:47:16 +08001275#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
Scott Wood2c1b7e12016-05-30 13:57:55 -05001276 hwctrl(mtd, (page_addr >> 16) & 0x0f, NAND_CTRL_ALE);
Bo Shen9415b872014-03-03 14:47:16 +08001277#endif
Scott Wood2c1b7e12016-05-30 13:57:55 -05001278 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Bo Shen9415b872014-03-03 14:47:16 +08001279
Scott Wood2c1b7e12016-05-30 13:57:55 -05001280 hwctrl(mtd, NAND_CMD_READSTART, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
1281 hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
Bo Shen9415b872014-03-03 14:47:16 +08001282
Scott Wood2c1b7e12016-05-30 13:57:55 -05001283 while (!this->dev_ready(mtd))
Bo Shen9415b872014-03-03 14:47:16 +08001284 ;
1285
1286 return 0;
1287}
1288
1289static int nand_is_bad_block(int block)
1290{
Scott Wood17fed142016-05-30 13:57:56 -05001291 struct nand_chip *this = mtd_to_nand(mtd);
Bo Shen9415b872014-03-03 14:47:16 +08001292
1293 nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS, NAND_CMD_READOOB);
1294
1295 if (this->options & NAND_BUSWIDTH_16) {
1296 if (readw(this->IO_ADDR_R) != 0xffff)
1297 return 1;
1298 } else {
1299 if (readb(this->IO_ADDR_R) != 0xff)
1300 return 1;
1301 }
1302
1303 return 0;
1304}
1305
1306#ifdef CONFIG_SPL_NAND_ECC
1307static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
1308#define ECCSTEPS (CONFIG_SYS_NAND_PAGE_SIZE / \
1309 CONFIG_SYS_NAND_ECCSIZE)
1310#define ECCTOTAL (ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
1311
1312static int nand_read_page(int block, int page, void *dst)
1313{
Scott Wood17fed142016-05-30 13:57:56 -05001314 struct nand_chip *this = mtd_to_nand(mtd);
Bo Shen9415b872014-03-03 14:47:16 +08001315 u_char ecc_calc[ECCTOTAL];
1316 u_char ecc_code[ECCTOTAL];
1317 u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
1318 int eccsize = CONFIG_SYS_NAND_ECCSIZE;
1319 int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
1320 int eccsteps = ECCSTEPS;
1321 int i;
1322 uint8_t *p = dst;
1323 nand_command(block, page, 0, NAND_CMD_READ0);
1324
1325 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1326 if (this->ecc.mode != NAND_ECC_SOFT)
Scott Wood2c1b7e12016-05-30 13:57:55 -05001327 this->ecc.hwctl(mtd, NAND_ECC_READ);
1328 this->read_buf(mtd, p, eccsize);
1329 this->ecc.calculate(mtd, p, &ecc_calc[i]);
Bo Shen9415b872014-03-03 14:47:16 +08001330 }
Scott Wood2c1b7e12016-05-30 13:57:55 -05001331 this->read_buf(mtd, oob_data, CONFIG_SYS_NAND_OOBSIZE);
Bo Shen9415b872014-03-03 14:47:16 +08001332
1333 for (i = 0; i < ECCTOTAL; i++)
1334 ecc_code[i] = oob_data[nand_ecc_pos[i]];
1335
1336 eccsteps = ECCSTEPS;
1337 p = dst;
1338
1339 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
Scott Wood2c1b7e12016-05-30 13:57:55 -05001340 this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
Bo Shen9415b872014-03-03 14:47:16 +08001341
1342 return 0;
1343}
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001344
1345int spl_nand_erase_one(int block, int page)
1346{
Scott Wood17fed142016-05-30 13:57:56 -05001347 struct nand_chip *this = mtd_to_nand(mtd);
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001348 void (*hwctrl)(struct mtd_info *mtd, int cmd,
1349 unsigned int ctrl) = this->cmd_ctrl;
1350 int page_addr;
1351
1352 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -05001353 nand_chip.select_chip(mtd, 0);
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001354
1355 page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
Scott Wood2c1b7e12016-05-30 13:57:55 -05001356 hwctrl(mtd, NAND_CMD_ERASE1, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001357 /* Row address */
Scott Wood2c1b7e12016-05-30 13:57:55 -05001358 hwctrl(mtd, (page_addr & 0xff), NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1359 hwctrl(mtd, ((page_addr >> 8) & 0xff),
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001360 NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1361#ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
1362 /* One more address cycle for devices > 128MiB */
Scott Wood2c1b7e12016-05-30 13:57:55 -05001363 hwctrl(mtd, (page_addr >> 16) & 0x0f,
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001364 NAND_CTRL_ALE | NAND_CTRL_CHANGE);
1365#endif
Scott Wood2c1b7e12016-05-30 13:57:55 -05001366 hwctrl(mtd, NAND_CMD_ERASE2, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001367
Scott Wood2c1b7e12016-05-30 13:57:55 -05001368 while (!this->dev_ready(mtd))
Heiko Schocher4ff0c372014-10-31 08:31:02 +01001369 ;
1370
1371 nand_deselect();
1372
1373 return 0;
1374}
Bo Shen9415b872014-03-03 14:47:16 +08001375#else
1376static int nand_read_page(int block, int page, void *dst)
1377{
Scott Wood17fed142016-05-30 13:57:56 -05001378 struct nand_chip *this = mtd_to_nand(mtd);
Bo Shen9415b872014-03-03 14:47:16 +08001379
1380 nand_command(block, page, 0, NAND_CMD_READ0);
Scott Wood2c1b7e12016-05-30 13:57:55 -05001381 atmel_nand_pmecc_read_page(mtd, this, dst, 0, page);
Bo Shen9415b872014-03-03 14:47:16 +08001382
1383 return 0;
1384}
1385#endif /* CONFIG_SPL_NAND_ECC */
1386
Bo Shen9415b872014-03-03 14:47:16 +08001387int at91_nand_wait_ready(struct mtd_info *mtd)
1388{
Scott Wood17fed142016-05-30 13:57:56 -05001389 struct nand_chip *this = mtd_to_nand(mtd);
Bo Shen9415b872014-03-03 14:47:16 +08001390
1391 udelay(this->chip_delay);
1392
Heiko Schocherae2af0a2014-10-31 08:31:03 +01001393 return 1;
Bo Shen9415b872014-03-03 14:47:16 +08001394}
1395
1396int board_nand_init(struct nand_chip *nand)
1397{
1398 int ret = 0;
1399
1400 nand->ecc.mode = NAND_ECC_SOFT;
1401#ifdef CONFIG_SYS_NAND_DBW_16
1402 nand->options = NAND_BUSWIDTH_16;
1403 nand->read_buf = nand_read_buf16;
1404#else
1405 nand->read_buf = nand_read_buf;
1406#endif
1407 nand->cmd_ctrl = at91_nand_hwcontrol;
1408#ifdef CONFIG_SYS_NAND_READY_PIN
1409 nand->dev_ready = at91_nand_ready;
1410#else
1411 nand->dev_ready = at91_nand_wait_ready;
1412#endif
1413 nand->chip_delay = 20;
David Dueckab7ec112015-03-20 10:52:49 +01001414#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1415 nand->bbt_options |= NAND_BBT_USE_FLASH;
1416#endif
Bo Shen9415b872014-03-03 14:47:16 +08001417
1418#ifdef CONFIG_ATMEL_NAND_HWECC
1419#ifdef CONFIG_ATMEL_NAND_HW_PMECC
Scott Wood2c1b7e12016-05-30 13:57:55 -05001420 ret = atmel_pmecc_nand_init_params(nand, mtd);
Bo Shen9415b872014-03-03 14:47:16 +08001421#endif
1422#endif
1423
1424 return ret;
1425}
1426
1427void nand_init(void)
1428{
Boris Brezillon3b5f8842016-06-15 20:56:10 +02001429 mtd = nand_to_mtd(&nand_chip);
Scott Wood2c1b7e12016-05-30 13:57:55 -05001430 mtd->writesize = CONFIG_SYS_NAND_PAGE_SIZE;
1431 mtd->oobsize = CONFIG_SYS_NAND_OOBSIZE;
Bo Shen9415b872014-03-03 14:47:16 +08001432 nand_chip.IO_ADDR_R = (void __iomem *)CONFIG_SYS_NAND_BASE;
1433 nand_chip.IO_ADDR_W = (void __iomem *)CONFIG_SYS_NAND_BASE;
1434 board_nand_init(&nand_chip);
1435
1436#ifdef CONFIG_SPL_NAND_ECC
1437 if (nand_chip.ecc.mode == NAND_ECC_SOFT) {
1438 nand_chip.ecc.calculate = nand_calculate_ecc;
1439 nand_chip.ecc.correct = nand_correct_data;
1440 }
1441#endif
1442
1443 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -05001444 nand_chip.select_chip(mtd, 0);
Bo Shen9415b872014-03-03 14:47:16 +08001445}
1446
1447void nand_deselect(void)
1448{
1449 if (nand_chip.select_chip)
Scott Wood2c1b7e12016-05-30 13:57:55 -05001450 nand_chip.select_chip(mtd, -1);
Bo Shen9415b872014-03-03 14:47:16 +08001451}
1452
Ladislav Michlc6a42002017-04-16 15:31:59 +02001453#include "nand_spl_loaders.c"
1454
Bo Shen9415b872014-03-03 14:47:16 +08001455#else
1456
Wu, Josh6cded6d2012-08-23 00:05:34 +00001457#ifndef CONFIG_SYS_NAND_BASE_LIST
1458#define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001459#endif
Wu, Josh6cded6d2012-08-23 00:05:34 +00001460static struct nand_chip nand_chip[CONFIG_SYS_MAX_NAND_DEVICE];
1461static ulong base_addr[CONFIG_SYS_MAX_NAND_DEVICE] = CONFIG_SYS_NAND_BASE_LIST;
1462
1463int atmel_nand_chip_init(int devnum, ulong base_addr)
1464{
1465 int ret;
Wu, Josh6cded6d2012-08-23 00:05:34 +00001466 struct nand_chip *nand = &nand_chip[devnum];
Scott Wood17fed142016-05-30 13:57:56 -05001467 struct mtd_info *mtd = nand_to_mtd(nand);
Wu, Josh6cded6d2012-08-23 00:05:34 +00001468
Wu, Josh6cded6d2012-08-23 00:05:34 +00001469 nand->IO_ADDR_R = nand->IO_ADDR_W = (void __iomem *)base_addr;
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001470
Bo Shenbb5a12f2013-08-28 14:54:26 +00001471#ifdef CONFIG_NAND_ECC_BCH
1472 nand->ecc.mode = NAND_ECC_SOFT_BCH;
1473#else
Sergey Lapin77e524c2008-10-31 12:28:43 +01001474 nand->ecc.mode = NAND_ECC_SOFT;
Bo Shenbb5a12f2013-08-28 14:54:26 +00001475#endif
Sergey Lapin77e524c2008-10-31 12:28:43 +01001476#ifdef CONFIG_SYS_NAND_DBW_16
1477 nand->options = NAND_BUSWIDTH_16;
1478#endif
Jean-Christophe PLAGNIOL-VILLARDc9539ba2009-03-22 10:22:34 +01001479 nand->cmd_ctrl = at91_nand_hwcontrol;
1480#ifdef CONFIG_SYS_NAND_READY_PIN
1481 nand->dev_ready = at91_nand_ready;
1482#endif
Wu, Joshf9f69b12013-10-18 17:46:31 +08001483 nand->chip_delay = 75;
David Dueckab7ec112015-03-20 10:52:49 +01001484#ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1485 nand->bbt_options |= NAND_BBT_USE_FLASH;
1486#endif
Sergey Lapin77e524c2008-10-31 12:28:43 +01001487
Wu, Josh6cded6d2012-08-23 00:05:34 +00001488 ret = nand_scan_ident(mtd, CONFIG_SYS_NAND_MAX_CHIPS, NULL);
1489 if (ret)
1490 return ret;
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001491
1492#ifdef CONFIG_ATMEL_NAND_HWECC
Wu, Joshfd3091d2012-08-23 00:05:36 +00001493#ifdef CONFIG_ATMEL_NAND_HW_PMECC
1494 ret = atmel_pmecc_nand_init_params(nand, mtd);
1495#else
Wu, Josh6cded6d2012-08-23 00:05:34 +00001496 ret = atmel_hwecc_nand_init_param(nand, mtd);
Wu, Joshfd3091d2012-08-23 00:05:36 +00001497#endif
Wu, Josh6cded6d2012-08-23 00:05:34 +00001498 if (ret)
1499 return ret;
1500#endif
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001501
Wu, Josh6cded6d2012-08-23 00:05:34 +00001502 ret = nand_scan_tail(mtd);
1503 if (!ret)
Scott Wood2c1b7e12016-05-30 13:57:55 -05001504 nand_register(devnum, mtd);
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001505
Wu, Josh6cded6d2012-08-23 00:05:34 +00001506 return ret;
1507}
Nikolay Petukhove6015ca2010-03-19 10:49:27 +05001508
Wu, Josh6cded6d2012-08-23 00:05:34 +00001509void board_nand_init(void)
1510{
1511 int i;
1512 for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
1513 if (atmel_nand_chip_init(i, base_addr[i]))
Wu, Josha8bd1d42013-10-18 17:46:34 +08001514 dev_err(host->dev, "atmel_nand: Fail to initialize #%d chip",
Wu, Josh6cded6d2012-08-23 00:05:34 +00001515 i);
Sergey Lapin77e524c2008-10-31 12:28:43 +01001516}
Bo Shen9415b872014-03-03 14:47:16 +08001517#endif /* CONFIG_SPL_BUILD */