xref: /rk3399_rockchip-uboot/drivers/mtd/nand/raw/am335x_spl_bch.c (revision cfcc706c901d603707657919484e4f65467be9ff)
1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal  * (C) Copyright 2012
3*cfcc706cSMiquel Raynal  * Konstantin Kozhevnikov, Cogent Embedded
4*cfcc706cSMiquel Raynal  *
5*cfcc706cSMiquel Raynal  * based on nand_spl_simple code
6*cfcc706cSMiquel Raynal  *
7*cfcc706cSMiquel Raynal  * (C) Copyright 2006-2008
8*cfcc706cSMiquel Raynal  * Stefan Roese, DENX Software Engineering, sr@denx.de.
9*cfcc706cSMiquel Raynal  *
10*cfcc706cSMiquel Raynal  * SPDX-License-Identifier:	GPL-2.0+
11*cfcc706cSMiquel Raynal  */
12*cfcc706cSMiquel Raynal 
13*cfcc706cSMiquel Raynal #include <common.h>
14*cfcc706cSMiquel Raynal #include <nand.h>
15*cfcc706cSMiquel Raynal #include <asm/io.h>
16*cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
17*cfcc706cSMiquel Raynal 
18*cfcc706cSMiquel Raynal static int nand_ecc_pos[] = CONFIG_SYS_NAND_ECCPOS;
19*cfcc706cSMiquel Raynal static struct mtd_info *mtd;
20*cfcc706cSMiquel Raynal static struct nand_chip nand_chip;
21*cfcc706cSMiquel Raynal 
22*cfcc706cSMiquel Raynal #define ECCSTEPS	(CONFIG_SYS_NAND_PAGE_SIZE / \
23*cfcc706cSMiquel Raynal 					CONFIG_SYS_NAND_ECCSIZE)
24*cfcc706cSMiquel Raynal #define ECCTOTAL	(ECCSTEPS * CONFIG_SYS_NAND_ECCBYTES)
25*cfcc706cSMiquel Raynal 
26*cfcc706cSMiquel Raynal 
27*cfcc706cSMiquel Raynal /*
28*cfcc706cSMiquel Raynal  * NAND command for large page NAND devices (2k)
29*cfcc706cSMiquel Raynal  */
nand_command(int block,int page,uint32_t offs,u8 cmd)30*cfcc706cSMiquel Raynal static int nand_command(int block, int page, uint32_t offs,
31*cfcc706cSMiquel Raynal 	u8 cmd)
32*cfcc706cSMiquel Raynal {
33*cfcc706cSMiquel Raynal 	struct nand_chip *this = mtd_to_nand(mtd);
34*cfcc706cSMiquel Raynal 	int page_addr = page + block * CONFIG_SYS_NAND_PAGE_COUNT;
35*cfcc706cSMiquel Raynal 	void (*hwctrl)(struct mtd_info *mtd, int cmd,
36*cfcc706cSMiquel Raynal 			unsigned int ctrl) = this->cmd_ctrl;
37*cfcc706cSMiquel Raynal 
38*cfcc706cSMiquel Raynal 	while (!this->dev_ready(mtd))
39*cfcc706cSMiquel Raynal 		;
40*cfcc706cSMiquel Raynal 
41*cfcc706cSMiquel Raynal 	/* Emulate NAND_CMD_READOOB */
42*cfcc706cSMiquel Raynal 	if (cmd == NAND_CMD_READOOB) {
43*cfcc706cSMiquel Raynal 		offs += CONFIG_SYS_NAND_PAGE_SIZE;
44*cfcc706cSMiquel Raynal 		cmd = NAND_CMD_READ0;
45*cfcc706cSMiquel Raynal 	}
46*cfcc706cSMiquel Raynal 
47*cfcc706cSMiquel Raynal 	/* Begin command latch cycle */
48*cfcc706cSMiquel Raynal 	hwctrl(mtd, cmd, NAND_CTRL_CLE | NAND_CTRL_CHANGE);
49*cfcc706cSMiquel Raynal 
50*cfcc706cSMiquel Raynal 	if (cmd == NAND_CMD_RESET) {
51*cfcc706cSMiquel Raynal 		hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
52*cfcc706cSMiquel Raynal 
53*cfcc706cSMiquel Raynal 		/*
54*cfcc706cSMiquel Raynal 		 * Apply this short delay always to ensure that we do wait
55*cfcc706cSMiquel Raynal 		 * tWB in any case on any machine.
56*cfcc706cSMiquel Raynal 		 */
57*cfcc706cSMiquel Raynal 		ndelay(150);
58*cfcc706cSMiquel Raynal 
59*cfcc706cSMiquel Raynal 		while (!this->dev_ready(mtd))
60*cfcc706cSMiquel Raynal 			;
61*cfcc706cSMiquel Raynal 		return 0;
62*cfcc706cSMiquel Raynal 	}
63*cfcc706cSMiquel Raynal 
64*cfcc706cSMiquel Raynal 	/* Shift the offset from byte addressing to word addressing. */
65*cfcc706cSMiquel Raynal 	if ((this->options & NAND_BUSWIDTH_16) && !nand_opcode_8bits(cmd))
66*cfcc706cSMiquel Raynal 		offs >>= 1;
67*cfcc706cSMiquel Raynal 
68*cfcc706cSMiquel Raynal 	/* Set ALE and clear CLE to start address cycle */
69*cfcc706cSMiquel Raynal 	/* Column address */
70*cfcc706cSMiquel Raynal 	hwctrl(mtd, offs & 0xff,
71*cfcc706cSMiquel Raynal 		       NAND_CTRL_ALE | NAND_CTRL_CHANGE); /* A[7:0] */
72*cfcc706cSMiquel Raynal 	hwctrl(mtd, (offs >> 8) & 0xff, NAND_CTRL_ALE); /* A[11:9] */
73*cfcc706cSMiquel Raynal 	/* Row address */
74*cfcc706cSMiquel Raynal 	if (cmd != NAND_CMD_RNDOUT) {
75*cfcc706cSMiquel Raynal 		hwctrl(mtd, (page_addr & 0xff),
76*cfcc706cSMiquel Raynal 		       NAND_CTRL_ALE); /* A[19:12] */
77*cfcc706cSMiquel Raynal 		hwctrl(mtd, ((page_addr >> 8) & 0xff),
78*cfcc706cSMiquel Raynal 		       NAND_CTRL_ALE); /* A[27:20] */
79*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_5_ADDR_CYCLE
80*cfcc706cSMiquel Raynal 		/* One more address cycle for devices > 128MiB */
81*cfcc706cSMiquel Raynal 		hwctrl(mtd, (page_addr >> 16) & 0x0f,
82*cfcc706cSMiquel Raynal 		       NAND_CTRL_ALE); /* A[31:28] */
83*cfcc706cSMiquel Raynal #endif
84*cfcc706cSMiquel Raynal 	}
85*cfcc706cSMiquel Raynal 
86*cfcc706cSMiquel Raynal 	hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
87*cfcc706cSMiquel Raynal 
88*cfcc706cSMiquel Raynal 
89*cfcc706cSMiquel Raynal 	/*
90*cfcc706cSMiquel Raynal 	 * Program and erase have their own busy handlers status, sequential
91*cfcc706cSMiquel Raynal 	 * in and status need no delay.
92*cfcc706cSMiquel Raynal 	 */
93*cfcc706cSMiquel Raynal 	switch (cmd) {
94*cfcc706cSMiquel Raynal 	case NAND_CMD_CACHEDPROG:
95*cfcc706cSMiquel Raynal 	case NAND_CMD_PAGEPROG:
96*cfcc706cSMiquel Raynal 	case NAND_CMD_ERASE1:
97*cfcc706cSMiquel Raynal 	case NAND_CMD_ERASE2:
98*cfcc706cSMiquel Raynal 	case NAND_CMD_SEQIN:
99*cfcc706cSMiquel Raynal 	case NAND_CMD_RNDIN:
100*cfcc706cSMiquel Raynal 	case NAND_CMD_STATUS:
101*cfcc706cSMiquel Raynal 		return 0;
102*cfcc706cSMiquel Raynal 
103*cfcc706cSMiquel Raynal 	case NAND_CMD_RNDOUT:
104*cfcc706cSMiquel Raynal 		/* No ready / busy check necessary */
105*cfcc706cSMiquel Raynal 		hwctrl(mtd, NAND_CMD_RNDOUTSTART, NAND_CTRL_CLE |
106*cfcc706cSMiquel Raynal 		       NAND_CTRL_CHANGE);
107*cfcc706cSMiquel Raynal 		hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
108*cfcc706cSMiquel Raynal 		return 0;
109*cfcc706cSMiquel Raynal 
110*cfcc706cSMiquel Raynal 	case NAND_CMD_READ0:
111*cfcc706cSMiquel Raynal 		/* Latch in address */
112*cfcc706cSMiquel Raynal 		hwctrl(mtd, NAND_CMD_READSTART,
113*cfcc706cSMiquel Raynal 		       NAND_CTRL_CLE | NAND_CTRL_CHANGE);
114*cfcc706cSMiquel Raynal 		hwctrl(mtd, NAND_CMD_NONE, NAND_NCE | NAND_CTRL_CHANGE);
115*cfcc706cSMiquel Raynal 	}
116*cfcc706cSMiquel Raynal 
117*cfcc706cSMiquel Raynal 	/*
118*cfcc706cSMiquel Raynal 	 * Apply this short delay always to ensure that we do wait tWB in
119*cfcc706cSMiquel Raynal 	 * any case on any machine.
120*cfcc706cSMiquel Raynal 	 */
121*cfcc706cSMiquel Raynal 	ndelay(150);
122*cfcc706cSMiquel Raynal 
123*cfcc706cSMiquel Raynal 	while (!this->dev_ready(mtd))
124*cfcc706cSMiquel Raynal 		;
125*cfcc706cSMiquel Raynal 
126*cfcc706cSMiquel Raynal 	return 0;
127*cfcc706cSMiquel Raynal }
128*cfcc706cSMiquel Raynal 
nand_is_bad_block(int block)129*cfcc706cSMiquel Raynal static int nand_is_bad_block(int block)
130*cfcc706cSMiquel Raynal {
131*cfcc706cSMiquel Raynal 	struct nand_chip *this = mtd_to_nand(mtd);
132*cfcc706cSMiquel Raynal 
133*cfcc706cSMiquel Raynal 	nand_command(block, 0, CONFIG_SYS_NAND_BAD_BLOCK_POS,
134*cfcc706cSMiquel Raynal 		NAND_CMD_READOOB);
135*cfcc706cSMiquel Raynal 
136*cfcc706cSMiquel Raynal 	/*
137*cfcc706cSMiquel Raynal 	 * Read one byte (or two if it's a 16 bit chip).
138*cfcc706cSMiquel Raynal 	 */
139*cfcc706cSMiquel Raynal 	if (this->options & NAND_BUSWIDTH_16) {
140*cfcc706cSMiquel Raynal 		if (readw(this->IO_ADDR_R) != 0xffff)
141*cfcc706cSMiquel Raynal 			return 1;
142*cfcc706cSMiquel Raynal 	} else {
143*cfcc706cSMiquel Raynal 		if (readb(this->IO_ADDR_R) != 0xff)
144*cfcc706cSMiquel Raynal 			return 1;
145*cfcc706cSMiquel Raynal 	}
146*cfcc706cSMiquel Raynal 
147*cfcc706cSMiquel Raynal 	return 0;
148*cfcc706cSMiquel Raynal }
149*cfcc706cSMiquel Raynal 
nand_read_page(int block,int page,void * dst)150*cfcc706cSMiquel Raynal static int nand_read_page(int block, int page, void *dst)
151*cfcc706cSMiquel Raynal {
152*cfcc706cSMiquel Raynal 	struct nand_chip *this = mtd_to_nand(mtd);
153*cfcc706cSMiquel Raynal 	u_char ecc_calc[ECCTOTAL];
154*cfcc706cSMiquel Raynal 	u_char ecc_code[ECCTOTAL];
155*cfcc706cSMiquel Raynal 	u_char oob_data[CONFIG_SYS_NAND_OOBSIZE];
156*cfcc706cSMiquel Raynal 	int i;
157*cfcc706cSMiquel Raynal 	int eccsize = CONFIG_SYS_NAND_ECCSIZE;
158*cfcc706cSMiquel Raynal 	int eccbytes = CONFIG_SYS_NAND_ECCBYTES;
159*cfcc706cSMiquel Raynal 	int eccsteps = ECCSTEPS;
160*cfcc706cSMiquel Raynal 	uint8_t *p = dst;
161*cfcc706cSMiquel Raynal 	uint32_t data_pos = 0;
162*cfcc706cSMiquel Raynal 	uint8_t *oob = &oob_data[0] + nand_ecc_pos[0];
163*cfcc706cSMiquel Raynal 	uint32_t oob_pos = eccsize * eccsteps + nand_ecc_pos[0];
164*cfcc706cSMiquel Raynal 
165*cfcc706cSMiquel Raynal 	nand_command(block, page, 0, NAND_CMD_READ0);
166*cfcc706cSMiquel Raynal 
167*cfcc706cSMiquel Raynal 	for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
168*cfcc706cSMiquel Raynal 		this->ecc.hwctl(mtd, NAND_ECC_READ);
169*cfcc706cSMiquel Raynal 		nand_command(block, page, data_pos, NAND_CMD_RNDOUT);
170*cfcc706cSMiquel Raynal 
171*cfcc706cSMiquel Raynal 		this->read_buf(mtd, p, eccsize);
172*cfcc706cSMiquel Raynal 
173*cfcc706cSMiquel Raynal 		nand_command(block, page, oob_pos, NAND_CMD_RNDOUT);
174*cfcc706cSMiquel Raynal 
175*cfcc706cSMiquel Raynal 		this->read_buf(mtd, oob, eccbytes);
176*cfcc706cSMiquel Raynal 		this->ecc.calculate(mtd, p, &ecc_calc[i]);
177*cfcc706cSMiquel Raynal 
178*cfcc706cSMiquel Raynal 		data_pos += eccsize;
179*cfcc706cSMiquel Raynal 		oob_pos += eccbytes;
180*cfcc706cSMiquel Raynal 		oob += eccbytes;
181*cfcc706cSMiquel Raynal 	}
182*cfcc706cSMiquel Raynal 
183*cfcc706cSMiquel Raynal 	/* Pick the ECC bytes out of the oob data */
184*cfcc706cSMiquel Raynal 	for (i = 0; i < ECCTOTAL; i++)
185*cfcc706cSMiquel Raynal 		ecc_code[i] = oob_data[nand_ecc_pos[i]];
186*cfcc706cSMiquel Raynal 
187*cfcc706cSMiquel Raynal 	eccsteps = ECCSTEPS;
188*cfcc706cSMiquel Raynal 	p = dst;
189*cfcc706cSMiquel Raynal 
190*cfcc706cSMiquel Raynal 	for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
191*cfcc706cSMiquel Raynal 		/* No chance to do something with the possible error message
192*cfcc706cSMiquel Raynal 		 * from correct_data(). We just hope that all possible errors
193*cfcc706cSMiquel Raynal 		 * are corrected by this routine.
194*cfcc706cSMiquel Raynal 		 */
195*cfcc706cSMiquel Raynal 		this->ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
196*cfcc706cSMiquel Raynal 	}
197*cfcc706cSMiquel Raynal 
198*cfcc706cSMiquel Raynal 	return 0;
199*cfcc706cSMiquel Raynal }
200*cfcc706cSMiquel Raynal 
201*cfcc706cSMiquel Raynal /* nand_init() - initialize data to make nand usable by SPL */
nand_init(void)202*cfcc706cSMiquel Raynal void nand_init(void)
203*cfcc706cSMiquel Raynal {
204*cfcc706cSMiquel Raynal 	/*
205*cfcc706cSMiquel Raynal 	 * Init board specific nand support
206*cfcc706cSMiquel Raynal 	 */
207*cfcc706cSMiquel Raynal 	mtd = nand_to_mtd(&nand_chip);
208*cfcc706cSMiquel Raynal 	nand_chip.IO_ADDR_R = nand_chip.IO_ADDR_W =
209*cfcc706cSMiquel Raynal 		(void  __iomem *)CONFIG_SYS_NAND_BASE;
210*cfcc706cSMiquel Raynal 	board_nand_init(&nand_chip);
211*cfcc706cSMiquel Raynal 
212*cfcc706cSMiquel Raynal 	if (nand_chip.select_chip)
213*cfcc706cSMiquel Raynal 		nand_chip.select_chip(mtd, 0);
214*cfcc706cSMiquel Raynal 
215*cfcc706cSMiquel Raynal 	/* NAND chip may require reset after power-on */
216*cfcc706cSMiquel Raynal 	nand_command(0, 0, 0, NAND_CMD_RESET);
217*cfcc706cSMiquel Raynal }
218*cfcc706cSMiquel Raynal 
219*cfcc706cSMiquel Raynal /* Unselect after operation */
nand_deselect(void)220*cfcc706cSMiquel Raynal void nand_deselect(void)
221*cfcc706cSMiquel Raynal {
222*cfcc706cSMiquel Raynal 	if (nand_chip.select_chip)
223*cfcc706cSMiquel Raynal 		nand_chip.select_chip(mtd, -1);
224*cfcc706cSMiquel Raynal }
225*cfcc706cSMiquel Raynal 
226*cfcc706cSMiquel Raynal #include "nand_spl_loaders.c"
227