1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2004-2008 Texas Instruments, <www.ti.com>
4  * Rohit Choraria <rohitkc@ti.com>
5  */
6 
7 #include <common.h>
8 #include <log.h>
9 #include <asm/io.h>
10 #include <dm/uclass.h>
11 #include <linux/errno.h>
12 
13 #ifdef CONFIG_ARCH_OMAP2PLUS
14 #include <asm/arch/mem.h>
15 #endif
16 
17 #include <linux/mtd/omap_gpmc.h>
18 #include <linux/mtd/nand_ecc.h>
19 #include <linux/mtd/rawnand.h>
20 #include <linux/bch.h>
21 #include <linux/compiler.h>
22 #include <nand.h>
23 
24 #include "omap_elm.h"
25 
26 #ifndef GPMC_MAX_CS
27 #define GPMC_MAX_CS	4
28 #endif
29 
30 #define BADBLOCK_MARKER_LENGTH	2
31 #define SECTOR_BYTES		512
32 #define ECCSIZE0_SHIFT		12
33 #define ECCSIZE1_SHIFT		22
34 #define ECC1RESULTSIZE		0x1
35 #define ECCCLEAR		(0x1 << 8)
36 #define ECCRESULTREG1		(0x1 << 0)
37 /* 4 bit padding to make byte aligned, 56 = 52 + 4 */
38 #define BCH4_BIT_PAD		4
39 
40 #ifdef CONFIG_BCH
41 static u8  bch8_polynomial[] = {0xef, 0x51, 0x2e, 0x09, 0xed, 0x93, 0x9a, 0xc2,
42 				0x97, 0x79, 0xe5, 0x24, 0xb5};
43 #endif
44 static uint8_t cs_next;
45 
46 #if defined(CONFIG_NAND_OMAP_GPMC_WSCFG)
47 static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE] =
48 	{ CONFIG_NAND_OMAP_GPMC_WSCFG };
49 #else
50 /* wscfg is preset to zero since its a static variable */
51 static const int8_t wscfg[CONFIG_SYS_MAX_NAND_DEVICE];
52 #endif
53 
54 /*
55  * Driver configurations
56  */
57 struct omap_nand_info {
58 	struct bch_control *control;
59 	enum omap_ecc ecc_scheme;
60 	uint8_t cs;
61 	uint8_t ws;		/* wait status pin (0,1) */
62 	void __iomem *fifo;
63 };
64 
65 /* We are wasting a bit of memory but al least we are safe */
66 static struct omap_nand_info omap_nand_info[GPMC_MAX_CS];
67 
68 /*
69  * omap_nand_hwcontrol - Set the address pointers corretly for the
70  *			following address/data/command operation
71  */
omap_nand_hwcontrol(struct mtd_info * mtd,int32_t cmd,uint32_t ctrl)72 static void omap_nand_hwcontrol(struct mtd_info *mtd, int32_t cmd,
73 				uint32_t ctrl)
74 {
75 	register struct nand_chip *this = mtd_to_nand(mtd);
76 	struct omap_nand_info *info = nand_get_controller_data(this);
77 	int cs = info->cs;
78 
79 	/*
80 	 * Point the IO_ADDR to DATA and ADDRESS registers instead
81 	 * of chip address
82 	 */
83 	switch (ctrl) {
84 	case NAND_CTRL_CHANGE | NAND_CTRL_CLE:
85 		this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
86 		break;
87 	case NAND_CTRL_CHANGE | NAND_CTRL_ALE:
88 		this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_adr;
89 		break;
90 	case NAND_CTRL_CHANGE | NAND_NCE:
91 		this->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
92 		break;
93 	}
94 
95 	if (cmd != NAND_CMD_NONE)
96 		writeb(cmd, this->IO_ADDR_W);
97 }
98 
99 /* Check wait pin as dev ready indicator */
omap_dev_ready(struct mtd_info * mtd)100 static int omap_dev_ready(struct mtd_info *mtd)
101 {
102 	register struct nand_chip *this = mtd_to_nand(mtd);
103 	struct omap_nand_info *info = nand_get_controller_data(this);
104 	return gpmc_cfg->status & (1 << (8 + info->ws));
105 }
106 
107 /*
108  * gen_true_ecc - This function will generate true ECC value, which
109  * can be used when correcting data read from NAND flash memory core
110  *
111  * @ecc_buf:	buffer to store ecc code
112  *
113  * @return:	re-formatted ECC value
114  */
gen_true_ecc(uint8_t * ecc_buf)115 static uint32_t gen_true_ecc(uint8_t *ecc_buf)
116 {
117 	return ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xF0) << 20) |
118 		((ecc_buf[2] & 0x0F) << 8);
119 }
120 
121 /*
122  * omap_correct_data - Compares the ecc read from nand spare area with ECC
123  * registers values and corrects one bit error if it has occurred
124  * Further details can be had from OMAP TRM and the following selected links:
125  * http://en.wikipedia.org/wiki/Hamming_code
126  * http://www.cs.utexas.edu/users/plaxton/c/337/05f/slides/ErrorCorrection-4.pdf
127  *
128  * @mtd:		 MTD device structure
129  * @dat:		 page data
130  * @read_ecc:		 ecc read from nand flash
131  * @calc_ecc:		 ecc read from ECC registers
132  *
133  * Return: 0 if data is OK or corrected, else returns -1
134  */
omap_correct_data(struct mtd_info * mtd,uint8_t * dat,uint8_t * read_ecc,uint8_t * calc_ecc)135 static int __maybe_unused omap_correct_data(struct mtd_info *mtd, uint8_t *dat,
136 				uint8_t *read_ecc, uint8_t *calc_ecc)
137 {
138 	uint32_t orig_ecc, new_ecc, res, hm;
139 	uint16_t parity_bits, byte;
140 	uint8_t bit;
141 
142 	/* Regenerate the orginal ECC */
143 	orig_ecc = gen_true_ecc(read_ecc);
144 	new_ecc = gen_true_ecc(calc_ecc);
145 	/* Get the XOR of real ecc */
146 	res = orig_ecc ^ new_ecc;
147 	if (res) {
148 		/* Get the hamming width */
149 		hm = hweight32(res);
150 		/* Single bit errors can be corrected! */
151 		if (hm == 12) {
152 			/* Correctable data! */
153 			parity_bits = res >> 16;
154 			bit = (parity_bits & 0x7);
155 			byte = (parity_bits >> 3) & 0x1FF;
156 			/* Flip the bit to correct */
157 			dat[byte] ^= (0x1 << bit);
158 		} else if (hm == 1) {
159 			printf("Error: Ecc is wrong\n");
160 			/* ECC itself is corrupted */
161 			return 2;
162 		} else {
163 			/*
164 			 * hm distance != parity pairs OR one, could mean 2 bit
165 			 * error OR potentially be on a blank page..
166 			 * orig_ecc: contains spare area data from nand flash.
167 			 * new_ecc: generated ecc while reading data area.
168 			 * Note: if the ecc = 0, all data bits from which it was
169 			 * generated are 0xFF.
170 			 * The 3 byte(24 bits) ecc is generated per 512byte
171 			 * chunk of a page. If orig_ecc(from spare area)
172 			 * is 0xFF && new_ecc(computed now from data area)=0x0,
173 			 * this means that data area is 0xFF and spare area is
174 			 * 0xFF. A sure sign of a erased page!
175 			 */
176 			if ((orig_ecc == 0x0FFF0FFF) && (new_ecc == 0x00000000))
177 				return 0;
178 			printf("Error: Bad compare! failed\n");
179 			/* detected 2 bit error */
180 			return -EBADMSG;
181 		}
182 	}
183 	return 0;
184 }
185 
186 /*
187  * omap_enable_hwecc - configures GPMC as per ECC scheme before read/write
188  * @mtd:	MTD device structure
189  * @mode:	Read/Write mode
190  */
191 __maybe_unused
omap_enable_hwecc(struct mtd_info * mtd,int32_t mode)192 static void omap_enable_hwecc(struct mtd_info *mtd, int32_t mode)
193 {
194 	struct nand_chip *nand = mtd_to_nand(mtd);
195 	struct omap_nand_info *info = nand_get_controller_data(nand);
196 	unsigned int dev_width = (nand->options & NAND_BUSWIDTH_16) ? 1 : 0;
197 	u32 val;
198 
199 	/* Clear ecc and enable bits */
200 	writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
201 
202 	/* program ecc and result sizes */
203 	val = ((((nand->ecc.size >> 1) - 1) << ECCSIZE1_SHIFT) |
204 			ECC1RESULTSIZE);
205 	writel(val, &gpmc_cfg->ecc_size_config);
206 
207 	switch (mode) {
208 	case NAND_ECC_READ:
209 	case NAND_ECC_WRITE:
210 		writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
211 		break;
212 	case NAND_ECC_READSYN:
213 		writel(ECCCLEAR, &gpmc_cfg->ecc_control);
214 		break;
215 	default:
216 		printf("%s: error: unrecognized Mode[%d]!\n", __func__, mode);
217 		break;
218 	}
219 
220 	/* (ECC 16 or 8 bit col) | ( CS  )  | ECC Enable */
221 	val = (dev_width << 7) | (info->cs << 1) | (0x1);
222 	writel(val, &gpmc_cfg->ecc_config);
223 }
224 
225 /*
226  *  omap_calculate_ecc - Read ECC result
227  *  @mtd:	MTD structure
228  *  @dat:	unused
229  *  @ecc_code:	ecc_code buffer
230  *  Using noninverted ECC can be considered ugly since writing a blank
231  *  page ie. padding will clear the ECC bytes. This is no problem as
232  *  long nobody is trying to write data on the seemingly unused page.
233  *  Reading an erased page will produce an ECC mismatch between
234  *  generated and read ECC bytes that has to be dealt with separately.
235  *  E.g. if page is 0xFF (fresh erased), and if HW ECC engine within GPMC
236  *  is used, the result of read will be 0x0 while the ECC offsets of the
237  *  spare area will be 0xFF which will result in an ECC mismatch.
238  */
omap_calculate_ecc(struct mtd_info * mtd,const uint8_t * dat,uint8_t * ecc_code)239 static int omap_calculate_ecc(struct mtd_info *mtd, const uint8_t *dat,
240 				uint8_t *ecc_code)
241 {
242 	u32 val;
243 
244 	val = readl(&gpmc_cfg->ecc1_result);
245 	ecc_code[0] = val & 0xFF;
246 	ecc_code[1] = (val >> 16) & 0xFF;
247 	ecc_code[2] = ((val >> 8) & 0x0F) | ((val >> 20) & 0xF0);
248 
249 	return 0;
250 }
251 
252 /* GPMC ecc engine settings for read */
253 #define BCH_WRAPMODE_1          1       /* BCH wrap mode 1 */
254 #define BCH8R_ECC_SIZE0         0x1a    /* ecc_size0 = 26 */
255 #define BCH8R_ECC_SIZE1         0x2     /* ecc_size1 = 2 */
256 #define BCH4R_ECC_SIZE0         0xd     /* ecc_size0 = 13 */
257 #define BCH4R_ECC_SIZE1         0x3     /* ecc_size1 = 3 */
258 
259 /* GPMC ecc engine settings for write */
260 #define BCH_WRAPMODE_6          6       /* BCH wrap mode 6 */
261 #define BCH_ECC_SIZE0           0x0     /* ecc_size0 = 0, no oob protection */
262 #define BCH_ECC_SIZE1           0x20    /* ecc_size1 = 32 */
263 
264 /**
265  * omap_enable_hwecc_bch - Program GPMC to perform BCH ECC calculation
266  * @mtd: MTD device structure
267  * @mode: Read/Write mode
268  *
269  * When using BCH with SW correction (i.e. no ELM), sector size is set
270  * to 512 bytes and we use BCH_WRAPMODE_6 wrapping mode
271  * for both reading and writing with:
272  * eccsize0 = 0  (no additional protected byte in spare area)
273  * eccsize1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
274  */
omap_enable_hwecc_bch(struct mtd_info * mtd,int mode)275 static void __maybe_unused omap_enable_hwecc_bch(struct mtd_info *mtd,
276 						 int mode)
277 {
278 	unsigned int bch_type;
279 	unsigned int dev_width, nsectors;
280 	struct nand_chip *chip = mtd_to_nand(mtd);
281 	struct omap_nand_info *info = nand_get_controller_data(chip);
282 	u32 val, wr_mode;
283 	unsigned int ecc_size1, ecc_size0;
284 
285 	/* GPMC configurations for calculating ECC */
286 	switch (info->ecc_scheme) {
287 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
288 		bch_type = 1;
289 		nsectors = 1;
290 		wr_mode   = BCH_WRAPMODE_6;
291 		ecc_size0 = BCH_ECC_SIZE0;
292 		ecc_size1 = BCH_ECC_SIZE1;
293 		break;
294 	case OMAP_ECC_BCH8_CODE_HW:
295 		bch_type = 1;
296 		nsectors = chip->ecc.steps;
297 		if (mode == NAND_ECC_READ) {
298 			wr_mode   = BCH_WRAPMODE_1;
299 			ecc_size0 = BCH8R_ECC_SIZE0;
300 			ecc_size1 = BCH8R_ECC_SIZE1;
301 		} else {
302 			wr_mode   = BCH_WRAPMODE_6;
303 			ecc_size0 = BCH_ECC_SIZE0;
304 			ecc_size1 = BCH_ECC_SIZE1;
305 		}
306 		break;
307 	case OMAP_ECC_BCH16_CODE_HW:
308 		bch_type = 0x2;
309 		nsectors = chip->ecc.steps;
310 		if (mode == NAND_ECC_READ) {
311 			wr_mode   = 0x01;
312 			ecc_size0 = 52; /* ECC bits in nibbles per sector */
313 			ecc_size1 = 0;  /* non-ECC bits in nibbles per sector */
314 		} else {
315 			wr_mode   = 0x01;
316 			ecc_size0 = 0;  /* extra bits in nibbles per sector */
317 			ecc_size1 = 52; /* OOB bits in nibbles per sector */
318 		}
319 		break;
320 	default:
321 		return;
322 	}
323 
324 	writel(ECCRESULTREG1, &gpmc_cfg->ecc_control);
325 
326 	/* Configure ecc size for BCH */
327 	val = (ecc_size1 << ECCSIZE1_SHIFT) | (ecc_size0 << ECCSIZE0_SHIFT);
328 	writel(val, &gpmc_cfg->ecc_size_config);
329 
330 	dev_width = (chip->options & NAND_BUSWIDTH_16) ? 1 : 0;
331 
332 	/* BCH configuration */
333 	val = ((1			<< 16) | /* enable BCH */
334 	       (bch_type		<< 12) | /* BCH4/BCH8/BCH16 */
335 	       (wr_mode			<<  8) | /* wrap mode */
336 	       (dev_width		<<  7) | /* bus width */
337 	       (((nsectors - 1) & 0x7)	<<  4) | /* number of sectors */
338 	       (info->cs		<<  1) | /* ECC CS */
339 	       (0x1));				 /* enable ECC */
340 
341 	writel(val, &gpmc_cfg->ecc_config);
342 
343 	/* Clear ecc and enable bits */
344 	writel(ECCCLEAR | ECCRESULTREG1, &gpmc_cfg->ecc_control);
345 }
346 
347 /**
348  * _omap_calculate_ecc_bch - Generate BCH ECC bytes for one sector
349  * @mtd:        MTD device structure
350  * @dat:        The pointer to data on which ecc is computed
351  * @ecc_code:   The ecc_code buffer
352  * @sector:     The sector number (for a multi sector page)
353  *
354  * Support calculating of BCH4/8/16 ECC vectors for one sector
355  * within a page. Sector number is in @sector.
356  */
_omap_calculate_ecc_bch(struct mtd_info * mtd,const u8 * dat,u8 * ecc_code,int sector)357 static int _omap_calculate_ecc_bch(struct mtd_info *mtd, const u8 *dat,
358 				   u8 *ecc_code, int sector)
359 {
360 	struct nand_chip *chip = mtd_to_nand(mtd);
361 	struct omap_nand_info *info = nand_get_controller_data(chip);
362 	const uint32_t *ptr;
363 	uint32_t val = 0;
364 	int8_t i = 0, j;
365 
366 	switch (info->ecc_scheme) {
367 #ifdef CONFIG_BCH
368 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
369 #endif
370 	case OMAP_ECC_BCH8_CODE_HW:
371 		ptr = &gpmc_cfg->bch_result_0_3[sector].bch_result_x[3];
372 		val = readl(ptr);
373 		ecc_code[i++] = (val >>  0) & 0xFF;
374 		ptr--;
375 		for (j = 0; j < 3; j++) {
376 			val = readl(ptr);
377 			ecc_code[i++] = (val >> 24) & 0xFF;
378 			ecc_code[i++] = (val >> 16) & 0xFF;
379 			ecc_code[i++] = (val >>  8) & 0xFF;
380 			ecc_code[i++] = (val >>  0) & 0xFF;
381 			ptr--;
382 		}
383 
384 		break;
385 	case OMAP_ECC_BCH16_CODE_HW:
386 		val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[2]);
387 		ecc_code[i++] = (val >>  8) & 0xFF;
388 		ecc_code[i++] = (val >>  0) & 0xFF;
389 		val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[1]);
390 		ecc_code[i++] = (val >> 24) & 0xFF;
391 		ecc_code[i++] = (val >> 16) & 0xFF;
392 		ecc_code[i++] = (val >>  8) & 0xFF;
393 		ecc_code[i++] = (val >>  0) & 0xFF;
394 		val = readl(&gpmc_cfg->bch_result_4_6[sector].bch_result_x[0]);
395 		ecc_code[i++] = (val >> 24) & 0xFF;
396 		ecc_code[i++] = (val >> 16) & 0xFF;
397 		ecc_code[i++] = (val >>  8) & 0xFF;
398 		ecc_code[i++] = (val >>  0) & 0xFF;
399 		for (j = 3; j >= 0; j--) {
400 			val = readl(&gpmc_cfg->bch_result_0_3[sector].bch_result_x[j]
401 									);
402 			ecc_code[i++] = (val >> 24) & 0xFF;
403 			ecc_code[i++] = (val >> 16) & 0xFF;
404 			ecc_code[i++] = (val >>  8) & 0xFF;
405 			ecc_code[i++] = (val >>  0) & 0xFF;
406 		}
407 		break;
408 	default:
409 		return -EINVAL;
410 	}
411 	/* ECC scheme specific syndrome customizations */
412 	switch (info->ecc_scheme) {
413 #ifdef CONFIG_BCH
414 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
415 		/* Add constant polynomial to remainder, so that
416 		 * ECC of blank pages results in 0x0 on reading back
417 		 */
418 		for (i = 0; i < chip->ecc.bytes; i++)
419 			ecc_code[i] ^= bch8_polynomial[i];
420 		break;
421 #endif
422 	case OMAP_ECC_BCH8_CODE_HW:
423 		/* Set 14th ECC byte as 0x0 for ROM compatibility */
424 		ecc_code[chip->ecc.bytes - 1] = 0x0;
425 		break;
426 	case OMAP_ECC_BCH16_CODE_HW:
427 		break;
428 	default:
429 		return -EINVAL;
430 	}
431 	return 0;
432 }
433 
434 /**
435  * omap_calculate_ecc_bch - ECC generator for 1 sector
436  * @mtd:        MTD device structure
437  * @dat:	The pointer to data on which ecc is computed
438  * @ecc_code:	The ecc_code buffer
439  *
440  * Support calculating of BCH4/8/16 ECC vectors for one sector. This is used
441  * when SW based correction is required as ECC is required for one sector
442  * at a time.
443  */
omap_calculate_ecc_bch(struct mtd_info * mtd,const u_char * dat,u_char * ecc_calc)444 static int __maybe_unused omap_calculate_ecc_bch(struct mtd_info *mtd,
445 				  const u_char *dat, u_char *ecc_calc)
446 {
447 	return _omap_calculate_ecc_bch(mtd, dat, ecc_calc, 0);
448 }
449 
omap_nand_read_buf(struct mtd_info * mtd,uint8_t * buf,int len)450 static inline void omap_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
451 {
452 	struct nand_chip *chip = mtd_to_nand(mtd);
453 	struct omap_nand_info *info = nand_get_controller_data(chip);
454 	u32 alignment = ((uintptr_t)buf | len) & 3;
455 
456 	if (alignment & 1)
457 		readsb(info->fifo, buf, len);
458 	else if (alignment & 3)
459 		readsw(info->fifo, buf, len >> 1);
460 	else
461 		readsl(info->fifo, buf, len >> 2);
462 }
463 
464 #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
465 
466 #define PREFETCH_CONFIG1_CS_SHIFT	24
467 #define PREFETCH_FIFOTHRESHOLD_MAX	0x40
468 #define PREFETCH_FIFOTHRESHOLD(val)	((val) << 8)
469 #define PREFETCH_STATUS_COUNT(val)	(val & 0x00003fff)
470 #define PREFETCH_STATUS_FIFO_CNT(val)	((val >> 24) & 0x7F)
471 #define ENABLE_PREFETCH			(1 << 7)
472 
473 /**
474  * omap_prefetch_enable - configures and starts prefetch transfer
475  * @fifo_th: fifo threshold to be used for read/ write
476  * @count: number of bytes to be transferred
477  * @is_write: prefetch read(0) or write post(1) mode
478  * @cs: chip select to use
479  */
omap_prefetch_enable(int fifo_th,unsigned int count,int is_write,int cs)480 static int omap_prefetch_enable(int fifo_th, unsigned int count, int is_write, int cs)
481 {
482 	uint32_t val;
483 
484 	if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX)
485 		return -EINVAL;
486 
487 	if (readl(&gpmc_cfg->prefetch_control))
488 		return -EBUSY;
489 
490 	/* Set the amount of bytes to be prefetched */
491 	writel(count, &gpmc_cfg->prefetch_config2);
492 
493 	val = (cs << PREFETCH_CONFIG1_CS_SHIFT) | (is_write & 1) |
494 		PREFETCH_FIFOTHRESHOLD(fifo_th) | ENABLE_PREFETCH;
495 	writel(val, &gpmc_cfg->prefetch_config1);
496 
497 	/*  Start the prefetch engine */
498 	writel(1, &gpmc_cfg->prefetch_control);
499 
500 	return 0;
501 }
502 
503 /**
504  * omap_prefetch_reset - disables and stops the prefetch engine
505  */
omap_prefetch_reset(void)506 static void omap_prefetch_reset(void)
507 {
508 	writel(0, &gpmc_cfg->prefetch_control);
509 	writel(0, &gpmc_cfg->prefetch_config1);
510 }
511 
__read_prefetch_aligned(struct nand_chip * chip,uint32_t * buf,int len)512 static int __read_prefetch_aligned(struct nand_chip *chip, uint32_t *buf, int len)
513 {
514 	int ret;
515 	uint32_t cnt;
516 	struct omap_nand_info *info = nand_get_controller_data(chip);
517 
518 	ret = omap_prefetch_enable(PREFETCH_FIFOTHRESHOLD_MAX, len, 0, info->cs);
519 	if (ret < 0)
520 		return ret;
521 
522 	do {
523 		int i;
524 
525 		cnt = readl(&gpmc_cfg->prefetch_status);
526 		cnt = PREFETCH_STATUS_FIFO_CNT(cnt);
527 
528 		for (i = 0; i < cnt / 4; i++) {
529 			*buf++ = readl(info->fifo);
530 			len -= 4;
531 		}
532 	} while (len);
533 
534 	omap_prefetch_reset();
535 
536 	return 0;
537 }
538 
omap_nand_read_prefetch(struct mtd_info * mtd,uint8_t * buf,int len)539 static void omap_nand_read_prefetch(struct mtd_info *mtd, uint8_t *buf, int len)
540 {
541 	int ret;
542 	uintptr_t head, tail;
543 	struct nand_chip *chip = mtd_to_nand(mtd);
544 
545 	/*
546 	 * If the destination buffer is unaligned, start with reading
547 	 * the overlap byte-wise.
548 	 */
549 	head = ((uintptr_t)buf) % 4;
550 	if (head) {
551 		omap_nand_read_buf(mtd, buf, head);
552 		buf += head;
553 		len -= head;
554 	}
555 
556 	/*
557 	 * Only transfer multiples of 4 bytes in a pre-fetched fashion.
558 	 * If there's a residue, care for it byte-wise afterwards.
559 	 */
560 	tail = len % 4;
561 
562 	ret = __read_prefetch_aligned(chip, (uint32_t *)buf, len - tail);
563 	if (ret < 0) {
564 		/* fallback in case the prefetch engine is busy */
565 		omap_nand_read_buf(mtd, buf, len);
566 	} else if (tail) {
567 		buf += len - tail;
568 		omap_nand_read_buf(mtd, buf, tail);
569 	}
570 }
571 #endif /* CONFIG_NAND_OMAP_GPMC_PREFETCH */
572 
573 #ifdef CONFIG_NAND_OMAP_ELM
574 
575 /**
576  * omap_calculate_ecc_bch_multi - Generate ECC for multiple sectors
577  * @mtd:	MTD device structure
578  * @dat:	The pointer to data on which ecc is computed
579  * @ecc_code:	The ecc_code buffer
580  *
581  * Support calculating of BCH4/8/16 ecc vectors for the entire page in one go.
582  */
omap_calculate_ecc_bch_multi(struct mtd_info * mtd,const u_char * dat,u_char * ecc_calc)583 static int omap_calculate_ecc_bch_multi(struct mtd_info *mtd,
584 					const u_char *dat, u_char *ecc_calc)
585 {
586 	struct nand_chip *chip = mtd_to_nand(mtd);
587 	int eccbytes = chip->ecc.bytes;
588 	unsigned long nsectors;
589 	int i, ret;
590 
591 	nsectors = ((readl(&gpmc_cfg->ecc_config) >> 4) & 0x7) + 1;
592 	for (i = 0; i < nsectors; i++) {
593 		ret = _omap_calculate_ecc_bch(mtd, dat, ecc_calc, i);
594 		if (ret)
595 			return ret;
596 
597 		ecc_calc += eccbytes;
598 	}
599 
600 	return 0;
601 }
602 
603 /*
604  * omap_reverse_list - re-orders list elements in reverse order [internal]
605  * @list:	pointer to start of list
606  * @length:	length of list
607 */
omap_reverse_list(u8 * list,unsigned int length)608 static void omap_reverse_list(u8 *list, unsigned int length)
609 {
610 	unsigned int i, j;
611 	unsigned int half_length = length / 2;
612 	u8 tmp;
613 	for (i = 0, j = length - 1; i < half_length; i++, j--) {
614 		tmp = list[i];
615 		list[i] = list[j];
616 		list[j] = tmp;
617 	}
618 }
619 
620 /*
621  * omap_correct_data_bch - Compares the ecc read from nand spare area
622  * with ECC registers values and corrects one bit error if it has occurred
623  *
624  * @mtd:	MTD device structure
625  * @dat:	page data
626  * @read_ecc:	ecc read from nand flash (ignored)
627  * @calc_ecc:	ecc read from ECC registers
628  *
629  * Return: 0 if data is OK or corrected, else returns -1
630  */
omap_correct_data_bch(struct mtd_info * mtd,uint8_t * dat,uint8_t * read_ecc,uint8_t * calc_ecc)631 static int omap_correct_data_bch(struct mtd_info *mtd, uint8_t *dat,
632 				uint8_t *read_ecc, uint8_t *calc_ecc)
633 {
634 	struct nand_chip *chip = mtd_to_nand(mtd);
635 	struct omap_nand_info *info = nand_get_controller_data(chip);
636 	struct nand_ecc_ctrl *ecc = &chip->ecc;
637 	uint32_t error_count = 0, error_max;
638 	uint32_t error_loc[ELM_MAX_ERROR_COUNT];
639 	enum bch_level bch_type;
640 	uint32_t i, ecc_flag = 0;
641 	uint8_t count;
642 	uint32_t byte_pos, bit_pos;
643 	int err = 0;
644 
645 	/* check calculated ecc */
646 	for (i = 0; i < ecc->bytes && !ecc_flag; i++) {
647 		if (calc_ecc[i] != 0x00)
648 			goto not_ecc_match;
649 	}
650 	return 0;
651 not_ecc_match:
652 
653 	/* check for whether it's an erased-page */
654 	for (i = 0; i < ecc->bytes; i++) {
655 		if (read_ecc[i] != 0xff)
656 			goto not_erased;
657 	}
658 	for (i = 0; i < SECTOR_BYTES; i++) {
659 		if (dat[i] != 0xff)
660 			goto not_erased;
661 	}
662 	return 0;
663 not_erased:
664 
665 	/*
666 	 * Check for whether it's an erased page with a correctable
667 	 * number of bitflips. Erased pages have all 1's in the data,
668 	 * so we just compute the number of 0 bits in the data and
669 	 * see if it's under the correction threshold.
670 	 *
671 	 * NOTE: The check for a perfect erased page above is faster for
672 	 * the more common case, even though it's logically redundant.
673 	 */
674 	for (i = 0; i < ecc->bytes; i++)
675 		error_count += hweight8(~read_ecc[i]);
676 
677 	for (i = 0; i < SECTOR_BYTES; i++)
678 		error_count += hweight8(~dat[i]);
679 
680 	if (error_count <= ecc->strength) {
681 		memset(read_ecc, 0xFF, ecc->bytes);
682 		memset(dat, 0xFF, SECTOR_BYTES);
683 		debug("nand: %u bit-flip(s) corrected in erased page\n",
684 		      error_count);
685 		return error_count;
686 	}
687 
688 	/*
689 	 * while reading ECC result we read it in big endian.
690 	 * Hence while loading to ELM we have rotate to get the right endian.
691 	 */
692 	switch (info->ecc_scheme) {
693 	case OMAP_ECC_BCH8_CODE_HW:
694 		bch_type = BCH_8_BIT;
695 		omap_reverse_list(calc_ecc, ecc->bytes - 1);
696 		break;
697 	case OMAP_ECC_BCH16_CODE_HW:
698 		bch_type = BCH_16_BIT;
699 		omap_reverse_list(calc_ecc, ecc->bytes);
700 		break;
701 	default:
702 		return -EINVAL;
703 	}
704 	/* use elm module to check for errors */
705 	elm_config(bch_type);
706 	error_count = 0;
707 	err = elm_check_error(calc_ecc, bch_type, &error_count, error_loc);
708 	if (err)
709 		return err;
710 
711 	/* correct bch error */
712 	for (count = 0; count < error_count; count++) {
713 		switch (info->ecc_scheme) {
714 		case OMAP_ECC_BCH8_CODE_HW:
715 			/* 14th byte in ECC is reserved to match ROM layout */
716 			error_max = SECTOR_BYTES + (ecc->bytes - 1);
717 			break;
718 		case OMAP_ECC_BCH16_CODE_HW:
719 			error_max = SECTOR_BYTES + ecc->bytes;
720 			break;
721 		default:
722 			return -EINVAL;
723 		}
724 		byte_pos = error_max - (error_loc[count] / 8) - 1;
725 		bit_pos  = error_loc[count] % 8;
726 		if (byte_pos < SECTOR_BYTES) {
727 			dat[byte_pos] ^= 1 << bit_pos;
728 			debug("nand: bit-flip corrected @data=%d\n", byte_pos);
729 		} else if (byte_pos < error_max) {
730 			read_ecc[byte_pos - SECTOR_BYTES] ^= 1 << bit_pos;
731 			debug("nand: bit-flip corrected @oob=%d\n", byte_pos -
732 								SECTOR_BYTES);
733 		} else {
734 			err = -EBADMSG;
735 			printf("nand: error: invalid bit-flip location\n");
736 		}
737 	}
738 	return (err) ? err : error_count;
739 }
740 
741 /**
742  * omap_read_page_bch - hardware ecc based page read function
743  * @mtd:	mtd info structure
744  * @chip:	nand chip info structure
745  * @buf:	buffer to store read data
746  * @oob_required: caller expects OOB data read to chip->oob_poi
747  * @page:	page number to read
748  *
749  */
omap_read_page_bch(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)750 static int omap_read_page_bch(struct mtd_info *mtd, struct nand_chip *chip,
751 				uint8_t *buf, int oob_required, int page)
752 {
753 	int i, eccsize = chip->ecc.size;
754 	int eccbytes = chip->ecc.bytes;
755 	int ecctotal = chip->ecc.total;
756 	int eccsteps = chip->ecc.steps;
757 	uint8_t *p = buf;
758 	uint8_t *ecc_calc = chip->buffers->ecccalc;
759 	uint8_t *ecc_code = chip->buffers->ecccode;
760 	uint32_t *eccpos = chip->ecc.layout->eccpos;
761 	uint8_t *oob = chip->oob_poi;
762 	uint32_t oob_pos;
763 
764 	/* oob area start */
765 	oob_pos = (eccsize * eccsteps) + chip->ecc.layout->eccpos[0];
766 	oob += chip->ecc.layout->eccpos[0];
767 
768 	/* Enable ECC engine */
769 	chip->ecc.hwctl(mtd, NAND_ECC_READ);
770 
771 	/* read entire page */
772 	chip->cmdfunc(mtd, NAND_CMD_RNDOUT, 0, -1);
773 	chip->read_buf(mtd, buf, mtd->writesize);
774 
775 	/* read all ecc bytes from oob area */
776 	chip->cmdfunc(mtd, NAND_CMD_RNDOUT, oob_pos, -1);
777 	chip->read_buf(mtd, oob, ecctotal);
778 
779 	/* Calculate ecc bytes */
780 	omap_calculate_ecc_bch_multi(mtd, buf, ecc_calc);
781 
782 	for (i = 0; i < chip->ecc.total; i++)
783 		ecc_code[i] = chip->oob_poi[eccpos[i]];
784 
785 	/* error detect & correct */
786 	eccsteps = chip->ecc.steps;
787 	p = buf;
788 
789 	for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
790 		int stat;
791 		stat = chip->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
792 		if (stat < 0)
793 			mtd->ecc_stats.failed++;
794 		else
795 			mtd->ecc_stats.corrected += stat;
796 	}
797 
798 	return 0;
799 }
800 #endif /* CONFIG_NAND_OMAP_ELM */
801 
802 /*
803  * OMAP3 BCH8 support (with BCH library)
804  */
805 #ifdef CONFIG_BCH
806 /**
807  * omap_correct_data_bch_sw - Decode received data and correct errors
808  * @mtd: MTD device structure
809  * @data: page data
810  * @read_ecc: ecc read from nand flash
811  * @calc_ecc: ecc read from HW ECC registers
812  */
omap_correct_data_bch_sw(struct mtd_info * mtd,u_char * data,u_char * read_ecc,u_char * calc_ecc)813 static int omap_correct_data_bch_sw(struct mtd_info *mtd, u_char *data,
814 				 u_char *read_ecc, u_char *calc_ecc)
815 {
816 	int i, count;
817 	/* cannot correct more than 8 errors */
818 	unsigned int errloc[8];
819 	struct nand_chip *chip = mtd_to_nand(mtd);
820 	struct omap_nand_info *info = nand_get_controller_data(chip);
821 
822 	count = decode_bch(info->control, NULL, SECTOR_BYTES,
823 				read_ecc, calc_ecc, NULL, errloc);
824 	if (count > 0) {
825 		/* correct errors */
826 		for (i = 0; i < count; i++) {
827 			/* correct data only, not ecc bytes */
828 			if (errloc[i] < SECTOR_BYTES << 3)
829 				data[errloc[i] >> 3] ^= 1 << (errloc[i] & 7);
830 			debug("corrected bitflip %u\n", errloc[i]);
831 #ifdef DEBUG
832 			puts("read_ecc: ");
833 			/*
834 			 * BCH8 have 13 bytes of ECC; BCH4 needs adoption
835 			 * here!
836 			 */
837 			for (i = 0; i < 13; i++)
838 				printf("%02x ", read_ecc[i]);
839 			puts("\n");
840 			puts("calc_ecc: ");
841 			for (i = 0; i < 13; i++)
842 				printf("%02x ", calc_ecc[i]);
843 			puts("\n");
844 #endif
845 		}
846 	} else if (count < 0) {
847 		puts("ecc unrecoverable error\n");
848 	}
849 	return count;
850 }
851 
852 /**
853  * omap_free_bch - Release BCH ecc resources
854  * @mtd: MTD device structure
855  */
omap_free_bch(struct mtd_info * mtd)856 static void __maybe_unused omap_free_bch(struct mtd_info *mtd)
857 {
858 	struct nand_chip *chip = mtd_to_nand(mtd);
859 	struct omap_nand_info *info = nand_get_controller_data(chip);
860 
861 	if (info->control) {
862 		free_bch(info->control);
863 		info->control = NULL;
864 	}
865 }
866 #endif /* CONFIG_BCH */
867 
868 /**
869  * omap_select_ecc_scheme - configures driver for particular ecc-scheme
870  * @nand: NAND chip device structure
871  * @ecc_scheme: ecc scheme to configure
872  * @pagesize: number of main-area bytes per page of NAND device
873  * @oobsize: number of OOB/spare bytes per page of NAND device
874  */
omap_select_ecc_scheme(struct nand_chip * nand,enum omap_ecc ecc_scheme,unsigned int pagesize,unsigned int oobsize)875 static int omap_select_ecc_scheme(struct nand_chip *nand,
876 	enum omap_ecc ecc_scheme, unsigned int pagesize, unsigned int oobsize) {
877 	struct omap_nand_info	*info		= nand_get_controller_data(nand);
878 	struct nand_ecclayout	*ecclayout	= nand->ecc.layout;
879 	int eccsteps = pagesize / SECTOR_BYTES;
880 	int i;
881 
882 	switch (ecc_scheme) {
883 	case OMAP_ECC_HAM1_CODE_SW:
884 		debug("nand: selected OMAP_ECC_HAM1_CODE_SW\n");
885 		/* For this ecc-scheme, ecc.bytes, ecc.layout, ... are
886 		 * initialized in nand_scan_tail(), so just set ecc.mode */
887 		info->control		= NULL;
888 		nand->ecc.mode		= NAND_ECC_SOFT;
889 		nand->ecc.layout	= NULL;
890 		nand->ecc.size		= 0;
891 		break;
892 
893 	case OMAP_ECC_HAM1_CODE_HW:
894 		debug("nand: selected OMAP_ECC_HAM1_CODE_HW\n");
895 		/* check ecc-scheme requirements before updating ecc info */
896 		if ((3 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
897 			printf("nand: error: insufficient OOB: require=%d\n", (
898 				(3 * eccsteps) + BADBLOCK_MARKER_LENGTH));
899 			return -EINVAL;
900 		}
901 		info->control		= NULL;
902 		/* populate ecc specific fields */
903 		memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
904 		nand->ecc.mode		= NAND_ECC_HW;
905 		nand->ecc.strength	= 1;
906 		nand->ecc.size		= SECTOR_BYTES;
907 		nand->ecc.bytes		= 3;
908 		nand->ecc.hwctl		= omap_enable_hwecc;
909 		nand->ecc.correct	= omap_correct_data;
910 		nand->ecc.calculate	= omap_calculate_ecc;
911 		/* define ecc-layout */
912 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
913 		for (i = 0; i < ecclayout->eccbytes; i++) {
914 			if (nand->options & NAND_BUSWIDTH_16)
915 				ecclayout->eccpos[i] = i + 2;
916 			else
917 				ecclayout->eccpos[i] = i + 1;
918 		}
919 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
920 		ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
921 						BADBLOCK_MARKER_LENGTH;
922 		break;
923 
924 	case OMAP_ECC_BCH8_CODE_HW_DETECTION_SW:
925 #ifdef CONFIG_BCH
926 		debug("nand: selected OMAP_ECC_BCH8_CODE_HW_DETECTION_SW\n");
927 		/* check ecc-scheme requirements before updating ecc info */
928 		if ((13 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
929 			printf("nand: error: insufficient OOB: require=%d\n", (
930 				(13 * eccsteps) + BADBLOCK_MARKER_LENGTH));
931 			return -EINVAL;
932 		}
933 		/* check if BCH S/W library can be used for error detection */
934 		info->control = init_bch(13, 8, 0x201b);
935 		if (!info->control) {
936 			printf("nand: error: could not init_bch()\n");
937 			return -ENODEV;
938 		}
939 		/* populate ecc specific fields */
940 		memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
941 		nand->ecc.mode		= NAND_ECC_HW;
942 		nand->ecc.strength	= 8;
943 		nand->ecc.size		= SECTOR_BYTES;
944 		nand->ecc.bytes		= 13;
945 		nand->ecc.hwctl		= omap_enable_hwecc_bch;
946 		nand->ecc.correct	= omap_correct_data_bch_sw;
947 		nand->ecc.calculate	= omap_calculate_ecc_bch;
948 		/* define ecc-layout */
949 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
950 		ecclayout->eccpos[0]	= BADBLOCK_MARKER_LENGTH;
951 		for (i = 1; i < ecclayout->eccbytes; i++) {
952 			if (i % nand->ecc.bytes)
953 				ecclayout->eccpos[i] =
954 						ecclayout->eccpos[i - 1] + 1;
955 			else
956 				ecclayout->eccpos[i] =
957 						ecclayout->eccpos[i - 1] + 2;
958 		}
959 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
960 		ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
961 						BADBLOCK_MARKER_LENGTH;
962 		break;
963 #else
964 		printf("nand: error: CONFIG_BCH required for ECC\n");
965 		return -EINVAL;
966 #endif
967 
968 	case OMAP_ECC_BCH8_CODE_HW:
969 #ifdef CONFIG_NAND_OMAP_ELM
970 		debug("nand: selected OMAP_ECC_BCH8_CODE_HW\n");
971 		/* check ecc-scheme requirements before updating ecc info */
972 		if ((14 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
973 			printf("nand: error: insufficient OOB: require=%d\n", (
974 				(14 * eccsteps) + BADBLOCK_MARKER_LENGTH));
975 			return -EINVAL;
976 		}
977 		/* intialize ELM for ECC error detection */
978 		elm_init();
979 		info->control		= NULL;
980 		/* populate ecc specific fields */
981 		memset(&nand->ecc, 0, sizeof(struct nand_ecc_ctrl));
982 		nand->ecc.mode		= NAND_ECC_HW;
983 		nand->ecc.strength	= 8;
984 		nand->ecc.size		= SECTOR_BYTES;
985 		nand->ecc.bytes		= 14;
986 		nand->ecc.hwctl		= omap_enable_hwecc_bch;
987 		nand->ecc.correct	= omap_correct_data_bch;
988 		nand->ecc.calculate	= omap_calculate_ecc_bch;
989 		nand->ecc.read_page	= omap_read_page_bch;
990 		/* define ecc-layout */
991 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
992 		for (i = 0; i < ecclayout->eccbytes; i++)
993 			ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
994 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
995 		ecclayout->oobfree[0].length = oobsize - ecclayout->eccbytes -
996 						BADBLOCK_MARKER_LENGTH;
997 		break;
998 #else
999 		printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1000 		return -EINVAL;
1001 #endif
1002 
1003 	case OMAP_ECC_BCH16_CODE_HW:
1004 #ifdef CONFIG_NAND_OMAP_ELM
1005 		debug("nand: using OMAP_ECC_BCH16_CODE_HW\n");
1006 		/* check ecc-scheme requirements before updating ecc info */
1007 		if ((26 * eccsteps) + BADBLOCK_MARKER_LENGTH > oobsize) {
1008 			printf("nand: error: insufficient OOB: require=%d\n", (
1009 				(26 * eccsteps) + BADBLOCK_MARKER_LENGTH));
1010 			return -EINVAL;
1011 		}
1012 		/* intialize ELM for ECC error detection */
1013 		elm_init();
1014 		/* populate ecc specific fields */
1015 		nand->ecc.mode		= NAND_ECC_HW;
1016 		nand->ecc.size		= SECTOR_BYTES;
1017 		nand->ecc.bytes		= 26;
1018 		nand->ecc.strength	= 16;
1019 		nand->ecc.hwctl		= omap_enable_hwecc_bch;
1020 		nand->ecc.correct	= omap_correct_data_bch;
1021 		nand->ecc.calculate	= omap_calculate_ecc_bch;
1022 		nand->ecc.read_page	= omap_read_page_bch;
1023 		/* define ecc-layout */
1024 		ecclayout->eccbytes	= nand->ecc.bytes * eccsteps;
1025 		for (i = 0; i < ecclayout->eccbytes; i++)
1026 			ecclayout->eccpos[i] = i + BADBLOCK_MARKER_LENGTH;
1027 		ecclayout->oobfree[0].offset = i + BADBLOCK_MARKER_LENGTH;
1028 		ecclayout->oobfree[0].length = oobsize - nand->ecc.bytes -
1029 						BADBLOCK_MARKER_LENGTH;
1030 		break;
1031 #else
1032 		printf("nand: error: CONFIG_NAND_OMAP_ELM required for ECC\n");
1033 		return -EINVAL;
1034 #endif
1035 	default:
1036 		debug("nand: error: ecc scheme not enabled or supported\n");
1037 		return -EINVAL;
1038 	}
1039 
1040 	/* nand_scan_tail() sets ham1 sw ecc; hw ecc layout is set by driver */
1041 	if (ecc_scheme != OMAP_ECC_HAM1_CODE_SW)
1042 		nand->ecc.layout = ecclayout;
1043 
1044 	info->ecc_scheme = ecc_scheme;
1045 	return 0;
1046 }
1047 
1048 #ifndef CONFIG_SPL_BUILD
1049 /*
1050  * omap_nand_switch_ecc - switch the ECC operation between different engines
1051  * (h/w and s/w) and different algorithms (hamming and BCHx)
1052  *
1053  * @hardware		- true if one of the HW engines should be used
1054  * @eccstrength		- the number of bits that could be corrected
1055  *			  (1 - hamming, 4 - BCH4, 8 - BCH8, 16 - BCH16)
1056  */
omap_nand_switch_ecc(uint32_t hardware,uint32_t eccstrength)1057 int __maybe_unused omap_nand_switch_ecc(uint32_t hardware, uint32_t eccstrength)
1058 {
1059 	struct nand_chip *nand;
1060 	struct mtd_info *mtd = get_nand_dev_by_index(nand_curr_device);
1061 	int err = 0;
1062 
1063 	if (!mtd) {
1064 		printf("nand: error: no NAND devices found\n");
1065 		return -ENODEV;
1066 	}
1067 
1068 	nand = mtd_to_nand(mtd);
1069 	nand->options |= NAND_OWN_BUFFERS;
1070 	nand->options &= ~NAND_SUBPAGE_READ;
1071 	/* Setup the ecc configurations again */
1072 	if (hardware) {
1073 		if (eccstrength == 1) {
1074 			err = omap_select_ecc_scheme(nand,
1075 					OMAP_ECC_HAM1_CODE_HW,
1076 					mtd->writesize, mtd->oobsize);
1077 		} else if (eccstrength == 8) {
1078 			err = omap_select_ecc_scheme(nand,
1079 					OMAP_ECC_BCH8_CODE_HW,
1080 					mtd->writesize, mtd->oobsize);
1081 		} else if (eccstrength == 16) {
1082 			err = omap_select_ecc_scheme(nand,
1083 					OMAP_ECC_BCH16_CODE_HW,
1084 					mtd->writesize, mtd->oobsize);
1085 		} else {
1086 			printf("nand: error: unsupported ECC scheme\n");
1087 			return -EINVAL;
1088 		}
1089 	} else {
1090 		if (eccstrength == 1) {
1091 			err = omap_select_ecc_scheme(nand,
1092 					OMAP_ECC_HAM1_CODE_SW,
1093 					mtd->writesize, mtd->oobsize);
1094 		} else if (eccstrength == 8) {
1095 			err = omap_select_ecc_scheme(nand,
1096 					OMAP_ECC_BCH8_CODE_HW_DETECTION_SW,
1097 					mtd->writesize, mtd->oobsize);
1098 		} else {
1099 			printf("nand: error: unsupported ECC scheme\n");
1100 			return -EINVAL;
1101 		}
1102 	}
1103 
1104 	/* Update NAND handling after ECC mode switch */
1105 	if (!err)
1106 		err = nand_scan_tail(mtd);
1107 	return err;
1108 }
1109 #endif /* CONFIG_SPL_BUILD */
1110 
1111 /*
1112  * Board-specific NAND initialization. The following members of the
1113  * argument are board-specific:
1114  * - IO_ADDR_R: address to read the 8 I/O lines of the flash device
1115  * - IO_ADDR_W: address to write the 8 I/O lines of the flash device
1116  * - cmd_ctrl: hardwarespecific function for accesing control-lines
1117  * - waitfunc: hardwarespecific function for accesing device ready/busy line
1118  * - ecc.hwctl: function to enable (reset) hardware ecc generator
1119  * - ecc.mode: mode of ecc, see defines
1120  * - chip_delay: chip dependent delay for transfering data from array to
1121  *   read regs (tR)
1122  * - options: various chip options. They can partly be set to inform
1123  *   nand_scan about special functionality. See the defines for further
1124  *   explanation
1125  */
gpmc_nand_init(struct nand_chip * nand)1126 int gpmc_nand_init(struct nand_chip *nand)
1127 {
1128 	int32_t gpmc_config = 0;
1129 	int cs = cs_next++;
1130 	int err = 0;
1131 	struct omap_nand_info *info;
1132 
1133 	/*
1134 	 * xloader/Uboot's gpmc configuration would have configured GPMC for
1135 	 * nand type of memory. The following logic scans and latches on to the
1136 	 * first CS with NAND type memory.
1137 	 * TBD: need to make this logic generic to handle multiple CS NAND
1138 	 * devices.
1139 	 */
1140 	while (cs < GPMC_MAX_CS) {
1141 		/* Check if NAND type is set */
1142 		if ((readl(&gpmc_cfg->cs[cs].config1) & 0xC00) == 0x800) {
1143 			/* Found it!! */
1144 			break;
1145 		}
1146 		cs++;
1147 	}
1148 	if (cs >= GPMC_MAX_CS) {
1149 		printf("nand: error: Unable to find NAND settings in "
1150 			"GPMC Configuration - quitting\n");
1151 		return -ENODEV;
1152 	}
1153 
1154 	gpmc_config = readl(&gpmc_cfg->config);
1155 	/* Disable Write protect */
1156 	gpmc_config |= 0x10;
1157 	writel(gpmc_config, &gpmc_cfg->config);
1158 
1159 	nand->IO_ADDR_R = (void __iomem *)&gpmc_cfg->cs[cs].nand_dat;
1160 	nand->IO_ADDR_W = (void __iomem *)&gpmc_cfg->cs[cs].nand_cmd;
1161 
1162 	info = &omap_nand_info[cs];
1163 	info->control = NULL;
1164 	info->cs = cs;
1165 	info->ws = wscfg[cs];
1166 	info->fifo = (void __iomem *)CFG_SYS_NAND_BASE;
1167 	nand_set_controller_data(nand, &omap_nand_info[cs]);
1168 	nand->cmd_ctrl	= omap_nand_hwcontrol;
1169 	nand->options	|= NAND_NO_PADDING | NAND_CACHEPRG;
1170 	nand->chip_delay = 100;
1171 	nand->ecc.layout = kzalloc(sizeof(*nand->ecc.layout), GFP_KERNEL);
1172 	if (!nand->ecc.layout)
1173 		return -ENOMEM;
1174 
1175 	/* configure driver and controller based on NAND device bus-width */
1176 	gpmc_config = readl(&gpmc_cfg->cs[cs].config1);
1177 #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
1178 	nand->options |= NAND_BUSWIDTH_16;
1179 	writel(gpmc_config | (0x1 << 12), &gpmc_cfg->cs[cs].config1);
1180 #else
1181 	nand->options &= ~NAND_BUSWIDTH_16;
1182 	writel(gpmc_config & ~(0x1 << 12), &gpmc_cfg->cs[cs].config1);
1183 #endif
1184 	/* select ECC scheme */
1185 #if defined(CONFIG_NAND_OMAP_ECCSCHEME)
1186 	err = omap_select_ecc_scheme(nand, CONFIG_NAND_OMAP_ECCSCHEME,
1187 			CONFIG_SYS_NAND_PAGE_SIZE, CONFIG_SYS_NAND_OOBSIZE);
1188 #else
1189 	/* pagesize and oobsize are not required to configure sw ecc-scheme */
1190 	err = omap_select_ecc_scheme(nand, OMAP_ECC_HAM1_CODE_SW,
1191 			0, 0);
1192 #endif
1193 	if (err)
1194 		return err;
1195 
1196 #ifdef CONFIG_NAND_OMAP_GPMC_PREFETCH
1197 	nand->read_buf = omap_nand_read_prefetch;
1198 #else
1199 	nand->read_buf = omap_nand_read_buf;
1200 #endif
1201 
1202 	nand->dev_ready = omap_dev_ready;
1203 
1204 	return 0;
1205 }
1206 
1207 /* First NAND chip for SPL use only */
1208 static __maybe_unused struct nand_chip *nand_chip;
1209 
1210 #if CONFIG_IS_ENABLED(SYS_NAND_SELF_INIT)
1211 
gpmc_nand_probe(struct udevice * dev)1212 static int gpmc_nand_probe(struct udevice *dev)
1213 {
1214 	struct nand_chip *nand = dev_get_priv(dev);
1215 	struct mtd_info *mtd = nand_to_mtd(nand);
1216 	int ret;
1217 
1218 	gpmc_nand_init(nand);
1219 
1220 	ret = nand_scan(mtd, CONFIG_SYS_NAND_MAX_CHIPS);
1221 	if (ret)
1222 		return ret;
1223 
1224 	ret = nand_register(0, mtd);
1225 	if (ret)
1226 		return ret;
1227 
1228 	if (!nand_chip)
1229 		nand_chip = nand;
1230 
1231 	return 0;
1232 }
1233 
1234 static const struct udevice_id gpmc_nand_ids[] = {
1235 	{ .compatible = "ti,am64-nand" },
1236 	{ .compatible = "ti,omap2-nand" },
1237 	{ }
1238 };
1239 
1240 U_BOOT_DRIVER(gpmc_nand) = {
1241 	.name           = "gpmc-nand",
1242 	.id             = UCLASS_MTD,
1243 	.of_match       = gpmc_nand_ids,
1244 	.probe          = gpmc_nand_probe,
1245 	.priv_auto	= sizeof(struct nand_chip),
1246 };
1247 
board_nand_init(void)1248 void board_nand_init(void)
1249 {
1250 	struct udevice *dev;
1251 	int ret;
1252 
1253 #ifdef CONFIG_NAND_OMAP_ELM
1254 	ret = uclass_get_device_by_driver(UCLASS_MTD,
1255 					  DM_DRIVER_GET(gpmc_elm), &dev);
1256 	if (ret && ret != -ENODEV) {
1257 		pr_err("%s: Failed to get ELM device: %d\n", __func__, ret);
1258 		return;
1259 	}
1260 #endif
1261 
1262 	ret = uclass_get_device_by_driver(UCLASS_MTD,
1263 					  DM_DRIVER_GET(gpmc_nand), &dev);
1264 	if (ret && ret != -ENODEV)
1265 		pr_err("%s: Failed to get GPMC device: %d\n", __func__, ret);
1266 }
1267 
1268 #else
1269 
board_nand_init(struct nand_chip * nand)1270 int board_nand_init(struct nand_chip *nand)
1271 {
1272 	return gpmc_nand_init(nand);
1273 }
1274 
1275 #endif /* CONFIG_SYS_NAND_SELF_INIT */
1276 
1277 #if defined(CONFIG_SPL_NAND_INIT)
1278 
1279 /* nand_init() is provided by nand.c */
1280 
1281 /* Unselect after operation */
nand_deselect(void)1282 void nand_deselect(void)
1283 {
1284 	struct mtd_info *mtd = nand_to_mtd(nand_chip);
1285 
1286 	if (nand_chip->select_chip)
1287 		nand_chip->select_chip(mtd, -1);
1288 }
1289 
nand_is_bad_block(int block)1290 static int nand_is_bad_block(int block)
1291 {
1292 	struct mtd_info *mtd = nand_to_mtd(nand_chip);
1293 
1294 	loff_t ofs = block * CONFIG_SYS_NAND_BLOCK_SIZE;
1295 
1296 	return nand_chip->block_bad(mtd, ofs);
1297 }
1298 
nand_read_page(int block,int page,uchar * dst)1299 static int nand_read_page(int block, int page, uchar *dst)
1300 {
1301 	int page_addr = block * CONFIG_SYS_NAND_PAGE_COUNT + page;
1302 	loff_t ofs = page_addr * CONFIG_SYS_NAND_PAGE_SIZE;
1303 	int ret;
1304 	size_t len = CONFIG_SYS_NAND_PAGE_SIZE;
1305 	struct mtd_info *mtd = nand_to_mtd(nand_chip);
1306 
1307 	ret = nand_read(mtd, ofs, &len, dst);
1308 	if (ret)
1309 		printf("nand_read failed %d\n", ret);
1310 
1311 	return ret;
1312 }
1313 
1314 #include "nand_spl_loaders.c"
1315 #endif /* CONFIG_SPL_NAND_INIT */
1316