1a430fa06SMiquel Raynal // SPDX-License-Identifier: GPL-2.0+
2a430fa06SMiquel Raynal /*
3a430fa06SMiquel Raynal  * LPC32xx MLC NAND flash controller driver
4a430fa06SMiquel Raynal  *
5a430fa06SMiquel Raynal  * (C) Copyright 2014 3ADEV <http://3adev.com>
6a430fa06SMiquel Raynal  * Written by Albert ARIBAUD <albert.aribaud@3adev.fr>
7a430fa06SMiquel Raynal  *
8a430fa06SMiquel Raynal  * NOTE:
9a430fa06SMiquel Raynal  *
10a430fa06SMiquel Raynal  * The MLC NAND flash controller provides hardware Reed-Solomon ECC
11a430fa06SMiquel Raynal  * covering in- and out-of-band data together. Therefore, in- and out-
12a430fa06SMiquel Raynal  * of-band data must be written together in order to have a valid ECC.
13a430fa06SMiquel Raynal  *
14a430fa06SMiquel Raynal  * Consequently, pages with meaningful in-band data are written with
15a430fa06SMiquel Raynal  * blank (all-ones) out-of-band data and a valid ECC, and any later
16a430fa06SMiquel Raynal  * out-of-band data write will void the ECC.
17a430fa06SMiquel Raynal  *
18a430fa06SMiquel Raynal  * Therefore, code which reads such late-written out-of-band data
19a430fa06SMiquel Raynal  * should not rely on the ECC validity.
20a430fa06SMiquel Raynal  */
21a430fa06SMiquel Raynal 
22a430fa06SMiquel Raynal #include <common.h>
23a430fa06SMiquel Raynal #include <nand.h>
24a430fa06SMiquel Raynal #include <linux/errno.h>
25a430fa06SMiquel Raynal #include <asm/io.h>
26a430fa06SMiquel Raynal #include <nand.h>
27a430fa06SMiquel Raynal #include <asm/arch/clk.h>
28a430fa06SMiquel Raynal #include <asm/arch/sys_proto.h>
29a430fa06SMiquel Raynal 
30a430fa06SMiquel Raynal /*
31a430fa06SMiquel Raynal  * MLC NAND controller registers.
32a430fa06SMiquel Raynal  */
33a430fa06SMiquel Raynal struct lpc32xx_nand_mlc_registers {
34a430fa06SMiquel Raynal 	u8 buff[32768]; /* controller's serial data buffer */
35a430fa06SMiquel Raynal 	u8 data[32768]; /* NAND's raw data buffer */
36a430fa06SMiquel Raynal 	u32 cmd;
37a430fa06SMiquel Raynal 	u32 addr;
38a430fa06SMiquel Raynal 	u32 ecc_enc_reg;
39a430fa06SMiquel Raynal 	u32 ecc_dec_reg;
40a430fa06SMiquel Raynal 	u32 ecc_auto_enc_reg;
41a430fa06SMiquel Raynal 	u32 ecc_auto_dec_reg;
42a430fa06SMiquel Raynal 	u32 rpr;
43a430fa06SMiquel Raynal 	u32 wpr;
44a430fa06SMiquel Raynal 	u32 rubp;
45a430fa06SMiquel Raynal 	u32 robp;
46a430fa06SMiquel Raynal 	u32 sw_wp_add_low;
47a430fa06SMiquel Raynal 	u32 sw_wp_add_hig;
48a430fa06SMiquel Raynal 	u32 icr;
49a430fa06SMiquel Raynal 	u32 time_reg;
50a430fa06SMiquel Raynal 	u32 irq_mr;
51a430fa06SMiquel Raynal 	u32 irq_sr;
52a430fa06SMiquel Raynal 	u32 lock_pr;
53a430fa06SMiquel Raynal 	u32 isr;
54a430fa06SMiquel Raynal 	u32 ceh;
55a430fa06SMiquel Raynal };
56a430fa06SMiquel Raynal 
57a430fa06SMiquel Raynal /* LOCK_PR register defines */
58a430fa06SMiquel Raynal #define LOCK_PR_UNLOCK_KEY 0x0000A25E  /* Magic unlock value */
59a430fa06SMiquel Raynal 
60a430fa06SMiquel Raynal /* ICR defines */
61a430fa06SMiquel Raynal #define ICR_LARGE_BLOCKS 0x00000004	/* configure for 2KB blocks */
62a430fa06SMiquel Raynal #define ICR_ADDR4        0x00000002	/* configure for 4-word addrs */
63a430fa06SMiquel Raynal 
64a430fa06SMiquel Raynal /* CEH defines */
65a430fa06SMiquel Raynal #define CEH_NORMAL_CE  0x00000001	/* do not force CE ON */
66a430fa06SMiquel Raynal 
67a430fa06SMiquel Raynal /* ISR register defines */
68a430fa06SMiquel Raynal #define ISR_NAND_READY        0x00000001
69a430fa06SMiquel Raynal #define ISR_CONTROLLER_READY  0x00000002
70a430fa06SMiquel Raynal #define ISR_ECC_READY         0x00000004
71a430fa06SMiquel Raynal #define ISR_DECODER_ERRORS(s) ((((s) >> 4) & 3)+1)
72a430fa06SMiquel Raynal #define ISR_DECODER_FAILURE   0x00000040
73a430fa06SMiquel Raynal #define ISR_DECODER_ERROR     0x00000008
74a430fa06SMiquel Raynal 
75a430fa06SMiquel Raynal /* time-out for NAND chip / controller loops, in us */
76a430fa06SMiquel Raynal #define LPC32X_NAND_TIMEOUT 5000
77a430fa06SMiquel Raynal 
78a430fa06SMiquel Raynal /*
79a430fa06SMiquel Raynal  * There is a single instance of the NAND MLC controller
80a430fa06SMiquel Raynal  */
81a430fa06SMiquel Raynal 
82a430fa06SMiquel Raynal static struct lpc32xx_nand_mlc_registers __iomem *lpc32xx_nand_mlc_registers
83a430fa06SMiquel Raynal 	= (struct lpc32xx_nand_mlc_registers __iomem *)MLC_NAND_BASE;
84a430fa06SMiquel Raynal 
85*a098ce21SVladimir Zapolskiy #if !defined(CONFIG_SYS_MAX_NAND_CHIPS)
86*a098ce21SVladimir Zapolskiy #define CONFIG_SYS_MAX_NAND_CHIPS	1
87*a098ce21SVladimir Zapolskiy #endif
88*a098ce21SVladimir Zapolskiy 
89a430fa06SMiquel Raynal #define clkdiv(v, w, o) (((1+(clk/v)) & w) << o)
90a430fa06SMiquel Raynal 
91a430fa06SMiquel Raynal /**
92a430fa06SMiquel Raynal  * OOB data in each small page are 6 'free' then 10 ECC bytes.
93a430fa06SMiquel Raynal  * To make things easier, when reading large pages, the four pages'
94a430fa06SMiquel Raynal  * 'free' OOB bytes are grouped in the first 24 bytes of the OOB buffer,
95a430fa06SMiquel Raynal  * while the the four ECC bytes are groupe in its last 40 bytes.
96a430fa06SMiquel Raynal  *
97a430fa06SMiquel Raynal  * The struct below represents how free vs ecc oob bytes are stored
98a430fa06SMiquel Raynal  * in the buffer.
99a430fa06SMiquel Raynal  *
100a430fa06SMiquel Raynal  * Note: the OOB bytes contain the bad block marker at offsets 0 and 1.
101a430fa06SMiquel Raynal  */
102a430fa06SMiquel Raynal 
103a430fa06SMiquel Raynal struct lpc32xx_oob {
104a430fa06SMiquel Raynal 	struct {
105a430fa06SMiquel Raynal 		uint8_t free_oob_bytes[6];
106a430fa06SMiquel Raynal 	} free[4];
107a430fa06SMiquel Raynal 	struct {
108a430fa06SMiquel Raynal 		uint8_t ecc_oob_bytes[10];
109a430fa06SMiquel Raynal 	} ecc[4];
110a430fa06SMiquel Raynal };
111a430fa06SMiquel Raynal 
112a430fa06SMiquel Raynal /*
113a430fa06SMiquel Raynal  * Initialize the controller
114a430fa06SMiquel Raynal  */
115a430fa06SMiquel Raynal 
lpc32xx_nand_init(void)116a430fa06SMiquel Raynal static void lpc32xx_nand_init(void)
117a430fa06SMiquel Raynal {
118a430fa06SMiquel Raynal 	unsigned int clk;
119a430fa06SMiquel Raynal 
120a430fa06SMiquel Raynal 	/* Configure controller for no software write protection, x8 bus
121a430fa06SMiquel Raynal 	   width, large block device, and 4 address words */
122a430fa06SMiquel Raynal 
123a430fa06SMiquel Raynal 	/* unlock controller registers with magic key */
124a430fa06SMiquel Raynal 	writel(LOCK_PR_UNLOCK_KEY,
125a430fa06SMiquel Raynal 	       &lpc32xx_nand_mlc_registers->lock_pr);
126a430fa06SMiquel Raynal 
127a430fa06SMiquel Raynal 	/* enable large blocks and large NANDs */
128a430fa06SMiquel Raynal 	writel(ICR_LARGE_BLOCKS | ICR_ADDR4,
129a430fa06SMiquel Raynal 	       &lpc32xx_nand_mlc_registers->icr);
130a430fa06SMiquel Raynal 
131a430fa06SMiquel Raynal 	/* Make sure MLC interrupts are disabled */
132a430fa06SMiquel Raynal 	writel(0, &lpc32xx_nand_mlc_registers->irq_mr);
133a430fa06SMiquel Raynal 
134a430fa06SMiquel Raynal 	/* Normal chip enable operation */
135a430fa06SMiquel Raynal 	writel(CEH_NORMAL_CE,
136a430fa06SMiquel Raynal 	       &lpc32xx_nand_mlc_registers->ceh);
137a430fa06SMiquel Raynal 
138a430fa06SMiquel Raynal 	/* Setup NAND timing */
139a430fa06SMiquel Raynal 	clk = get_hclk_clk_rate();
140a430fa06SMiquel Raynal 
141a430fa06SMiquel Raynal 	writel(
142a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_TCEA_DELAY, 0x03, 24) |
143a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_BUSY_DELAY, 0x1F, 19) |
144a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_NAND_TA,    0x07, 16) |
145a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_RD_HIGH,    0x0F, 12) |
146a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_RD_LOW,     0x0F, 8) |
147a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_WR_HIGH,    0x0F, 4) |
148a430fa06SMiquel Raynal 		clkdiv(CONFIG_LPC32XX_NAND_MLC_WR_LOW,     0x0F, 0),
149a430fa06SMiquel Raynal 		&lpc32xx_nand_mlc_registers->time_reg);
150a430fa06SMiquel Raynal }
151a430fa06SMiquel Raynal 
152a430fa06SMiquel Raynal #if !defined(CONFIG_SPL_BUILD)
153a430fa06SMiquel Raynal 
154a430fa06SMiquel Raynal /**
155a430fa06SMiquel Raynal  * lpc32xx_cmd_ctrl - write command to either cmd or data register
156a430fa06SMiquel Raynal  */
157a430fa06SMiquel Raynal 
lpc32xx_cmd_ctrl(struct mtd_info * mtd,int cmd,unsigned int ctrl)158a430fa06SMiquel Raynal static void lpc32xx_cmd_ctrl(struct mtd_info *mtd, int cmd,
159a430fa06SMiquel Raynal 				   unsigned int ctrl)
160a430fa06SMiquel Raynal {
161a430fa06SMiquel Raynal 	if (cmd == NAND_CMD_NONE)
162a430fa06SMiquel Raynal 		return;
163a430fa06SMiquel Raynal 
164a430fa06SMiquel Raynal 	if (ctrl & NAND_CLE)
165a430fa06SMiquel Raynal 		writeb(cmd & 0Xff, &lpc32xx_nand_mlc_registers->cmd);
166a430fa06SMiquel Raynal 	else if (ctrl & NAND_ALE)
167a430fa06SMiquel Raynal 		writeb(cmd & 0Xff, &lpc32xx_nand_mlc_registers->addr);
168a430fa06SMiquel Raynal }
169a430fa06SMiquel Raynal 
170a430fa06SMiquel Raynal /**
171a430fa06SMiquel Raynal  * lpc32xx_read_byte - read a byte from the NAND
172a430fa06SMiquel Raynal  * @mtd:	MTD device structure
173a430fa06SMiquel Raynal  */
174a430fa06SMiquel Raynal 
lpc32xx_read_byte(struct mtd_info * mtd)175a430fa06SMiquel Raynal static uint8_t lpc32xx_read_byte(struct mtd_info *mtd)
176a430fa06SMiquel Raynal {
177a430fa06SMiquel Raynal 	return readb(&lpc32xx_nand_mlc_registers->data);
178a430fa06SMiquel Raynal }
179a430fa06SMiquel Raynal 
180a430fa06SMiquel Raynal /**
181a430fa06SMiquel Raynal  * lpc32xx_dev_ready - test if NAND device (actually controller) is ready
182a430fa06SMiquel Raynal  * @mtd:	MTD device structure
183a430fa06SMiquel Raynal  * @mode:	mode to set the ECC HW to.
184a430fa06SMiquel Raynal  */
185a430fa06SMiquel Raynal 
lpc32xx_dev_ready(struct mtd_info * mtd)186a430fa06SMiquel Raynal static int lpc32xx_dev_ready(struct mtd_info *mtd)
187a430fa06SMiquel Raynal {
188a430fa06SMiquel Raynal 	/* means *controller* ready for us */
189a430fa06SMiquel Raynal 	int status = readl(&lpc32xx_nand_mlc_registers->isr);
190a430fa06SMiquel Raynal 	return status & ISR_CONTROLLER_READY;
191a430fa06SMiquel Raynal }
192a430fa06SMiquel Raynal 
193a430fa06SMiquel Raynal /**
194a430fa06SMiquel Raynal  * ECC layout -- this is needed whatever ECC mode we are using.
195a430fa06SMiquel Raynal  * In a 2KB (4*512B) page, R/S codes occupy 40 (4*10) bytes.
196a430fa06SMiquel Raynal  * To make U-Boot's life easier, we pack 'useable' OOB at the
197a430fa06SMiquel Raynal  * front and R/S ECC at the back.
198a430fa06SMiquel Raynal  */
199a430fa06SMiquel Raynal 
200a430fa06SMiquel Raynal static struct nand_ecclayout lpc32xx_largepage_ecclayout = {
201a430fa06SMiquel Raynal 	.eccbytes = 40,
202a430fa06SMiquel Raynal 	.eccpos = {24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
203a430fa06SMiquel Raynal 		   34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
204a430fa06SMiquel Raynal 		   44, 45, 46, 47, 48, 48, 50, 51, 52, 53,
205a430fa06SMiquel Raynal 		   54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
206a430fa06SMiquel Raynal 		   },
207a430fa06SMiquel Raynal 	.oobfree = {
208a430fa06SMiquel Raynal 		/* bytes 0 and 1 are used for the bad block marker */
209a430fa06SMiquel Raynal 		{
210a430fa06SMiquel Raynal 			.offset = 2,
211a430fa06SMiquel Raynal 			.length = 22
212a430fa06SMiquel Raynal 		},
213a430fa06SMiquel Raynal 	}
214a430fa06SMiquel Raynal };
215a430fa06SMiquel Raynal 
216a430fa06SMiquel Raynal /**
217a430fa06SMiquel Raynal  * lpc32xx_read_page_hwecc - read in- and out-of-band data with ECC
218a430fa06SMiquel Raynal  * @mtd: mtd info structure
219a430fa06SMiquel Raynal  * @chip: nand chip info structure
220a430fa06SMiquel Raynal  * @buf: buffer to store read data
221a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
222a430fa06SMiquel Raynal  * @page: page number to read
223a430fa06SMiquel Raynal  *
224a430fa06SMiquel Raynal  * Use large block Auto Decode Read Mode(1) as described in User Manual
225a430fa06SMiquel Raynal  * section 8.6.2.1.
226a430fa06SMiquel Raynal  *
227a430fa06SMiquel Raynal  * The initial Read Mode and Read Start commands are sent by the caller.
228a430fa06SMiquel Raynal  *
229a430fa06SMiquel Raynal  * ECC will be false if out-of-band data has been updated since in-band
230a430fa06SMiquel Raynal  * data was initially written.
231a430fa06SMiquel Raynal  */
232a430fa06SMiquel Raynal 
lpc32xx_read_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)233a430fa06SMiquel Raynal static int lpc32xx_read_page_hwecc(struct mtd_info *mtd,
234a430fa06SMiquel Raynal 	struct nand_chip *chip, uint8_t *buf, int oob_required,
235a430fa06SMiquel Raynal 	int page)
236a430fa06SMiquel Raynal {
237a430fa06SMiquel Raynal 	unsigned int i, status, timeout, err, max_bitflips = 0;
238a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
239a430fa06SMiquel Raynal 
240a430fa06SMiquel Raynal 	/* go through all four small pages */
241a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
242a430fa06SMiquel Raynal 		/* start auto decode (reads 528 NAND bytes) */
243a430fa06SMiquel Raynal 		writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
244a430fa06SMiquel Raynal 		/* wait for controller to return to ready state */
245a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
246a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
247a430fa06SMiquel Raynal 			if (status & ISR_CONTROLLER_READY)
248a430fa06SMiquel Raynal 				break;
249a430fa06SMiquel Raynal 			udelay(1);
250a430fa06SMiquel Raynal 		}
251a430fa06SMiquel Raynal 		/* if decoder failed, return failure */
252a430fa06SMiquel Raynal 		if (status & ISR_DECODER_FAILURE)
253a430fa06SMiquel Raynal 			return -1;
254a430fa06SMiquel Raynal 		/* keep count of maximum bitflips performed */
255a430fa06SMiquel Raynal 		if (status & ISR_DECODER_ERROR) {
256a430fa06SMiquel Raynal 			err = ISR_DECODER_ERRORS(status);
257a430fa06SMiquel Raynal 			if (err > max_bitflips)
258a430fa06SMiquel Raynal 				max_bitflips = err;
259a430fa06SMiquel Raynal 		}
260a430fa06SMiquel Raynal 		/* copy first 512 bytes into buffer */
261a430fa06SMiquel Raynal 		memcpy(buf+512*i, lpc32xx_nand_mlc_registers->buff, 512);
262a430fa06SMiquel Raynal 		/* copy next 6 bytes at front of OOB buffer */
263a430fa06SMiquel Raynal 		memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
264a430fa06SMiquel Raynal 		/* copy last 10 bytes (R/S ECC) at back of OOB buffer */
265a430fa06SMiquel Raynal 		memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->buff, 10);
266a430fa06SMiquel Raynal 	}
267a430fa06SMiquel Raynal 	return max_bitflips;
268a430fa06SMiquel Raynal }
269a430fa06SMiquel Raynal 
270a430fa06SMiquel Raynal /**
271a430fa06SMiquel Raynal  * lpc32xx_read_page_raw - read raw (in-band, out-of-band and ECC) data
272a430fa06SMiquel Raynal  * @mtd: mtd info structure
273a430fa06SMiquel Raynal  * @chip: nand chip info structure
274a430fa06SMiquel Raynal  * @buf: buffer to store read data
275a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
276a430fa06SMiquel Raynal  * @page: page number to read
277a430fa06SMiquel Raynal  *
278a430fa06SMiquel Raynal  * Read NAND directly; can read pages with invalid ECC.
279a430fa06SMiquel Raynal  */
280a430fa06SMiquel Raynal 
lpc32xx_read_page_raw(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)281a430fa06SMiquel Raynal static int lpc32xx_read_page_raw(struct mtd_info *mtd,
282a430fa06SMiquel Raynal 	struct nand_chip *chip, uint8_t *buf, int oob_required,
283a430fa06SMiquel Raynal 	int page)
284a430fa06SMiquel Raynal {
285a430fa06SMiquel Raynal 	unsigned int i, status, timeout;
286a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
287a430fa06SMiquel Raynal 
288a430fa06SMiquel Raynal 	/* when we get here we've already had the Read Mode(1) */
289a430fa06SMiquel Raynal 
290a430fa06SMiquel Raynal 	/* go through all four small pages */
291a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
292a430fa06SMiquel Raynal 		/* wait for NAND to return to ready state */
293a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
294a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
295a430fa06SMiquel Raynal 			if (status & ISR_NAND_READY)
296a430fa06SMiquel Raynal 				break;
297a430fa06SMiquel Raynal 			udelay(1);
298a430fa06SMiquel Raynal 		}
299a430fa06SMiquel Raynal 		/* if NAND stalled, return failure */
300a430fa06SMiquel Raynal 		if (!(status & ISR_NAND_READY))
301a430fa06SMiquel Raynal 			return -1;
302a430fa06SMiquel Raynal 		/* copy first 512 bytes into buffer */
303a430fa06SMiquel Raynal 		memcpy(buf+512*i, lpc32xx_nand_mlc_registers->data, 512);
304a430fa06SMiquel Raynal 		/* copy next 6 bytes at front of OOB buffer */
305a430fa06SMiquel Raynal 		memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->data, 6);
306a430fa06SMiquel Raynal 		/* copy last 10 bytes (R/S ECC) at back of OOB buffer */
307a430fa06SMiquel Raynal 		memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->data, 10);
308a430fa06SMiquel Raynal 	}
309a430fa06SMiquel Raynal 	return 0;
310a430fa06SMiquel Raynal }
311a430fa06SMiquel Raynal 
312a430fa06SMiquel Raynal /**
313a430fa06SMiquel Raynal  * lpc32xx_read_oob - read out-of-band data
314a430fa06SMiquel Raynal  * @mtd: mtd info structure
315a430fa06SMiquel Raynal  * @chip: nand chip info structure
316a430fa06SMiquel Raynal  * @page: page number to read
317a430fa06SMiquel Raynal  *
318a430fa06SMiquel Raynal  * Read out-of-band data. User Manual section 8.6.4 suggests using Read
319a430fa06SMiquel Raynal  * Mode(3) which the controller will turn into a Read Mode(1) internally
320a430fa06SMiquel Raynal  * but nand_base.c will turn Mode(3) into Mode(0), so let's use Mode(0)
321a430fa06SMiquel Raynal  * directly.
322a430fa06SMiquel Raynal  *
323a430fa06SMiquel Raynal  * ECC covers in- and out-of-band data and was written when out-of-band
324a430fa06SMiquel Raynal  * data was blank. Therefore, if the out-of-band being read here is not
325a430fa06SMiquel Raynal  * blank, then the ECC will be false and the read will return bitflips,
326a430fa06SMiquel Raynal  * even in case of ECC failure where we will return 5 bitflips. The
327a430fa06SMiquel Raynal  * caller should be prepared to handle this.
328a430fa06SMiquel Raynal  */
329a430fa06SMiquel Raynal 
lpc32xx_read_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)330a430fa06SMiquel Raynal static int lpc32xx_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
331a430fa06SMiquel Raynal 	int page)
332a430fa06SMiquel Raynal {
333a430fa06SMiquel Raynal 	unsigned int i, status, timeout, err, max_bitflips = 0;
334a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
335a430fa06SMiquel Raynal 
336a430fa06SMiquel Raynal 	/* No command was sent before calling read_oob() so send one */
337a430fa06SMiquel Raynal 
338a430fa06SMiquel Raynal 	chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
339a430fa06SMiquel Raynal 
340a430fa06SMiquel Raynal 	/* go through all four small pages */
341a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
342a430fa06SMiquel Raynal 		/* start auto decode (reads 528 NAND bytes) */
343a430fa06SMiquel Raynal 		writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
344a430fa06SMiquel Raynal 		/* wait for controller to return to ready state */
345a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
346a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
347a430fa06SMiquel Raynal 			if (status & ISR_CONTROLLER_READY)
348a430fa06SMiquel Raynal 				break;
349a430fa06SMiquel Raynal 			udelay(1);
350a430fa06SMiquel Raynal 		}
351a430fa06SMiquel Raynal 		/* if decoder failure, count 'one too many' bitflips */
352a430fa06SMiquel Raynal 		if (status & ISR_DECODER_FAILURE)
353a430fa06SMiquel Raynal 			max_bitflips = 5;
354a430fa06SMiquel Raynal 		/* keep count of maximum bitflips performed */
355a430fa06SMiquel Raynal 		if (status & ISR_DECODER_ERROR) {
356a430fa06SMiquel Raynal 			err = ISR_DECODER_ERRORS(status);
357a430fa06SMiquel Raynal 			if (err > max_bitflips)
358a430fa06SMiquel Raynal 				max_bitflips = err;
359a430fa06SMiquel Raynal 		}
360a430fa06SMiquel Raynal 		/* set read pointer to OOB area */
361a430fa06SMiquel Raynal 		writel(0, &lpc32xx_nand_mlc_registers->robp);
362a430fa06SMiquel Raynal 		/* copy next 6 bytes at front of OOB buffer */
363a430fa06SMiquel Raynal 		memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
364a430fa06SMiquel Raynal 		/* copy next 10 bytes (R/S ECC) at back of OOB buffer */
365a430fa06SMiquel Raynal 		memcpy(&oob->ecc[i], lpc32xx_nand_mlc_registers->buff, 10);
366a430fa06SMiquel Raynal 	}
367a430fa06SMiquel Raynal 	return max_bitflips;
368a430fa06SMiquel Raynal }
369a430fa06SMiquel Raynal 
370a430fa06SMiquel Raynal /**
371a430fa06SMiquel Raynal  * lpc32xx_write_page_hwecc - write in- and out-of-band data with ECC
372a430fa06SMiquel Raynal  * @mtd: mtd info structure
373a430fa06SMiquel Raynal  * @chip: nand chip info structure
374a430fa06SMiquel Raynal  * @buf: data buffer
375a430fa06SMiquel Raynal  * @oob_required: must write chip->oob_poi to OOB
376a430fa06SMiquel Raynal  *
377a430fa06SMiquel Raynal  * Use large block Auto Encode as per User Manual section 8.6.4.
378a430fa06SMiquel Raynal  *
379a430fa06SMiquel Raynal  * The initial Write Serial Input and final Auto Program commands are
380a430fa06SMiquel Raynal  * sent by the caller.
381a430fa06SMiquel Raynal  */
382a430fa06SMiquel Raynal 
lpc32xx_write_page_hwecc(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)383a430fa06SMiquel Raynal static int lpc32xx_write_page_hwecc(struct mtd_info *mtd,
384a430fa06SMiquel Raynal 	struct nand_chip *chip, const uint8_t *buf, int oob_required,
385a430fa06SMiquel Raynal 	int page)
386a430fa06SMiquel Raynal {
387a430fa06SMiquel Raynal 	unsigned int i, status, timeout;
388a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
389a430fa06SMiquel Raynal 
390a430fa06SMiquel Raynal 	/* when we get here we've already had the SEQIN */
391a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
392a430fa06SMiquel Raynal 		/* start encode (expects 518 writes to buff) */
393a430fa06SMiquel Raynal 		writel(0, &lpc32xx_nand_mlc_registers->ecc_enc_reg);
394a430fa06SMiquel Raynal 		/* copy first 512 bytes from buffer */
395a430fa06SMiquel Raynal 		memcpy(&lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
396a430fa06SMiquel Raynal 		/* copy next 6 bytes from OOB buffer -- excluding ECC */
397a430fa06SMiquel Raynal 		memcpy(&lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
398a430fa06SMiquel Raynal 		/* wait for ECC to return to ready state */
399a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
400a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
401a430fa06SMiquel Raynal 			if (status & ISR_ECC_READY)
402a430fa06SMiquel Raynal 				break;
403a430fa06SMiquel Raynal 			udelay(1);
404a430fa06SMiquel Raynal 		}
405a430fa06SMiquel Raynal 		/* if ECC stalled, return failure */
406a430fa06SMiquel Raynal 		if (!(status & ISR_ECC_READY))
407a430fa06SMiquel Raynal 			return -1;
408a430fa06SMiquel Raynal 		/* Trigger auto encode (writes 528 bytes to NAND) */
409a430fa06SMiquel Raynal 		writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_enc_reg);
410a430fa06SMiquel Raynal 		/* wait for controller to return to ready state */
411a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
412a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
413a430fa06SMiquel Raynal 			if (status & ISR_CONTROLLER_READY)
414a430fa06SMiquel Raynal 				break;
415a430fa06SMiquel Raynal 			udelay(1);
416a430fa06SMiquel Raynal 		}
417a430fa06SMiquel Raynal 		/* if controller stalled, return error */
418a430fa06SMiquel Raynal 		if (!(status & ISR_CONTROLLER_READY))
419a430fa06SMiquel Raynal 			return -1;
420a430fa06SMiquel Raynal 	}
421a430fa06SMiquel Raynal 	return 0;
422a430fa06SMiquel Raynal }
423a430fa06SMiquel Raynal 
424a430fa06SMiquel Raynal /**
425a430fa06SMiquel Raynal  * lpc32xx_write_page_raw - write raw (in-band, out-of-band and ECC) data
426a430fa06SMiquel Raynal  * @mtd: mtd info structure
427a430fa06SMiquel Raynal  * @chip: nand chip info structure
428a430fa06SMiquel Raynal  * @buf: buffer to store read data
429a430fa06SMiquel Raynal  * @oob_required: caller requires OOB data read to chip->oob_poi
430a430fa06SMiquel Raynal  * @page: page number to read
431a430fa06SMiquel Raynal  *
432a430fa06SMiquel Raynal  * Use large block write but without encode.
433a430fa06SMiquel Raynal  *
434a430fa06SMiquel Raynal  * The initial Write Serial Input and final Auto Program commands are
435a430fa06SMiquel Raynal  * sent by the caller.
436a430fa06SMiquel Raynal  *
437a430fa06SMiquel Raynal  * This function will write the full out-of-band data, including the
438a430fa06SMiquel Raynal  * ECC area. Therefore, it can write pages with valid *or* invalid ECC.
439a430fa06SMiquel Raynal  */
440a430fa06SMiquel Raynal 
lpc32xx_write_page_raw(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)441a430fa06SMiquel Raynal static int lpc32xx_write_page_raw(struct mtd_info *mtd,
442a430fa06SMiquel Raynal 	struct nand_chip *chip, const uint8_t *buf, int oob_required,
443a430fa06SMiquel Raynal 	int page)
444a430fa06SMiquel Raynal {
445a430fa06SMiquel Raynal 	unsigned int i;
446a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
447a430fa06SMiquel Raynal 
448a430fa06SMiquel Raynal 	/* when we get here we've already had the Read Mode(1) */
449a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
450a430fa06SMiquel Raynal 		/* copy first 512 bytes from buffer */
451a430fa06SMiquel Raynal 		memcpy(lpc32xx_nand_mlc_registers->buff, buf+512*i, 512);
452a430fa06SMiquel Raynal 		/* copy next 6 bytes into OOB buffer -- excluding ECC */
453a430fa06SMiquel Raynal 		memcpy(lpc32xx_nand_mlc_registers->buff, &oob->free[i], 6);
454a430fa06SMiquel Raynal 		/* copy next 10 bytes into OOB buffer -- that is 'ECC' */
455a430fa06SMiquel Raynal 		memcpy(lpc32xx_nand_mlc_registers->buff, &oob->ecc[i], 10);
456a430fa06SMiquel Raynal 	}
457a430fa06SMiquel Raynal 	return 0;
458a430fa06SMiquel Raynal }
459a430fa06SMiquel Raynal 
460a430fa06SMiquel Raynal /**
461a430fa06SMiquel Raynal  * lpc32xx_write_oob - write out-of-band data
462a430fa06SMiquel Raynal  * @mtd: mtd info structure
463a430fa06SMiquel Raynal  * @chip: nand chip info structure
464a430fa06SMiquel Raynal  * @page: page number to read
465a430fa06SMiquel Raynal  *
466a430fa06SMiquel Raynal  * Since ECC covers in- and out-of-band data, writing out-of-band data
467a430fa06SMiquel Raynal  * with ECC will render the page ECC wrong -- or, if the page was blank,
468a430fa06SMiquel Raynal  * then it will produce a good ECC but a later in-band data write will
469a430fa06SMiquel Raynal  * render it wrong.
470a430fa06SMiquel Raynal  *
471a430fa06SMiquel Raynal  * Therefore, do not compute or write any ECC, and always return success.
472a430fa06SMiquel Raynal  *
473a430fa06SMiquel Raynal  * This implies that we do four writes, since non-ECC out-of-band data
474a430fa06SMiquel Raynal  * are not contiguous in a large page.
475a430fa06SMiquel Raynal  */
476a430fa06SMiquel Raynal 
lpc32xx_write_oob(struct mtd_info * mtd,struct nand_chip * chip,int page)477a430fa06SMiquel Raynal static int lpc32xx_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
478a430fa06SMiquel Raynal 	int page)
479a430fa06SMiquel Raynal {
480a430fa06SMiquel Raynal 	/* update oob on all 4 subpages in sequence */
481a430fa06SMiquel Raynal 	unsigned int i, status, timeout;
482a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob = (struct lpc32xx_oob *)chip->oob_poi;
483a430fa06SMiquel Raynal 
484a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
485a430fa06SMiquel Raynal 		/* start data input */
486a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x200+0x210*i, page);
487a430fa06SMiquel Raynal 		/* copy 6 non-ECC out-of-band bytes directly into NAND */
488a430fa06SMiquel Raynal 		memcpy(lpc32xx_nand_mlc_registers->data, &oob->free[i], 6);
489a430fa06SMiquel Raynal 		/* program page */
490a430fa06SMiquel Raynal 		chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
491a430fa06SMiquel Raynal 		/* wait for NAND to return to ready state */
492a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
493a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
494a430fa06SMiquel Raynal 			if (status & ISR_NAND_READY)
495a430fa06SMiquel Raynal 				break;
496a430fa06SMiquel Raynal 			udelay(1);
497a430fa06SMiquel Raynal 		}
498a430fa06SMiquel Raynal 		/* if NAND stalled, return error */
499a430fa06SMiquel Raynal 		if (!(status & ISR_NAND_READY))
500a430fa06SMiquel Raynal 			return -1;
501a430fa06SMiquel Raynal 	}
502a430fa06SMiquel Raynal 	return 0;
503a430fa06SMiquel Raynal }
504a430fa06SMiquel Raynal 
505a430fa06SMiquel Raynal /**
506a430fa06SMiquel Raynal  * lpc32xx_waitfunc - wait until a command is done
507a430fa06SMiquel Raynal  * @mtd: MTD device structure
508a430fa06SMiquel Raynal  * @chip: NAND chip structure
509a430fa06SMiquel Raynal  *
510a430fa06SMiquel Raynal  * Wait for controller and FLASH to both be ready.
511a430fa06SMiquel Raynal  */
512a430fa06SMiquel Raynal 
lpc32xx_waitfunc(struct mtd_info * mtd,struct nand_chip * chip)513a430fa06SMiquel Raynal static int lpc32xx_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
514a430fa06SMiquel Raynal {
515a430fa06SMiquel Raynal 	int status;
516a430fa06SMiquel Raynal 	unsigned int timeout;
517a430fa06SMiquel Raynal 	/* wait until both controller and NAND are ready */
518a430fa06SMiquel Raynal 	for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
519a430fa06SMiquel Raynal 		status = readl(&lpc32xx_nand_mlc_registers->isr);
520a430fa06SMiquel Raynal 		if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
521a430fa06SMiquel Raynal 		    == (ISR_CONTROLLER_READY || ISR_NAND_READY))
522a430fa06SMiquel Raynal 			break;
523a430fa06SMiquel Raynal 		udelay(1);
524a430fa06SMiquel Raynal 	}
525a430fa06SMiquel Raynal 	/* if controller or NAND stalled, return error */
526a430fa06SMiquel Raynal 	if ((status & (ISR_CONTROLLER_READY || ISR_NAND_READY))
527a430fa06SMiquel Raynal 	    != (ISR_CONTROLLER_READY || ISR_NAND_READY))
528a430fa06SMiquel Raynal 		return -1;
529a430fa06SMiquel Raynal 	/* write NAND status command */
530a430fa06SMiquel Raynal 	writel(NAND_CMD_STATUS, &lpc32xx_nand_mlc_registers->cmd);
531a430fa06SMiquel Raynal 	/* read back status and return it */
532a430fa06SMiquel Raynal 	return readb(&lpc32xx_nand_mlc_registers->data);
533a430fa06SMiquel Raynal }
534a430fa06SMiquel Raynal 
535a430fa06SMiquel Raynal /*
536a430fa06SMiquel Raynal  * We are self-initializing, so we need our own chip struct
537a430fa06SMiquel Raynal  */
538a430fa06SMiquel Raynal 
539a430fa06SMiquel Raynal static struct nand_chip lpc32xx_chip;
540a430fa06SMiquel Raynal 
541a430fa06SMiquel Raynal /*
542a430fa06SMiquel Raynal  * Initialize the controller
543a430fa06SMiquel Raynal  */
544a430fa06SMiquel Raynal 
board_nand_init(void)545a430fa06SMiquel Raynal void board_nand_init(void)
546a430fa06SMiquel Raynal {
547a430fa06SMiquel Raynal 	struct mtd_info *mtd = nand_to_mtd(&lpc32xx_chip);
548a430fa06SMiquel Raynal 	int ret;
549a430fa06SMiquel Raynal 
550a430fa06SMiquel Raynal 	/* Set all BOARDSPECIFIC (actually core-specific) fields  */
551a430fa06SMiquel Raynal 
552a430fa06SMiquel Raynal 	lpc32xx_chip.IO_ADDR_R = &lpc32xx_nand_mlc_registers->buff;
553a430fa06SMiquel Raynal 	lpc32xx_chip.IO_ADDR_W = &lpc32xx_nand_mlc_registers->buff;
554a430fa06SMiquel Raynal 	lpc32xx_chip.cmd_ctrl = lpc32xx_cmd_ctrl;
555a430fa06SMiquel Raynal 	/* do not set init_size: nand_base.c will read sizes from chip */
556a430fa06SMiquel Raynal 	lpc32xx_chip.dev_ready = lpc32xx_dev_ready;
557a430fa06SMiquel Raynal 	/* do not set setup_read_retry: this is NAND-chip-specific */
558a430fa06SMiquel Raynal 	/* do not set chip_delay: we have dev_ready defined. */
559a430fa06SMiquel Raynal 	lpc32xx_chip.options |= NAND_NO_SUBPAGE_WRITE;
560a430fa06SMiquel Raynal 
561a430fa06SMiquel Raynal 	/* Set needed ECC fields */
562a430fa06SMiquel Raynal 
563a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.mode = NAND_ECC_HW;
564a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.layout = &lpc32xx_largepage_ecclayout;
565a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.size = 512;
566a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.bytes = 10;
567a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.strength = 4;
568a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.read_page = lpc32xx_read_page_hwecc;
569a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.read_page_raw = lpc32xx_read_page_raw;
570a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.write_page = lpc32xx_write_page_hwecc;
571a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.write_page_raw = lpc32xx_write_page_raw;
572a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.read_oob = lpc32xx_read_oob;
573a430fa06SMiquel Raynal 	lpc32xx_chip.ecc.write_oob = lpc32xx_write_oob;
574a430fa06SMiquel Raynal 	lpc32xx_chip.waitfunc = lpc32xx_waitfunc;
575a430fa06SMiquel Raynal 
576a430fa06SMiquel Raynal 	lpc32xx_chip.read_byte = lpc32xx_read_byte; /* FIXME: NEEDED? */
577a430fa06SMiquel Raynal 
578a430fa06SMiquel Raynal 	/* BBT options: read from last two pages */
579a430fa06SMiquel Raynal 	lpc32xx_chip.bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_LASTBLOCK
580a430fa06SMiquel Raynal 		| NAND_BBT_SCANLASTPAGE | NAND_BBT_SCAN2NDPAGE
581a430fa06SMiquel Raynal 		| NAND_BBT_WRITE;
582a430fa06SMiquel Raynal 
583a430fa06SMiquel Raynal 	/* Initialize NAND interface */
584a430fa06SMiquel Raynal 	lpc32xx_nand_init();
585a430fa06SMiquel Raynal 
586a430fa06SMiquel Raynal 	/* identify chip */
587a430fa06SMiquel Raynal 	ret = nand_scan_ident(mtd, CONFIG_SYS_MAX_NAND_CHIPS, NULL);
588a430fa06SMiquel Raynal 	if (ret) {
589a430fa06SMiquel Raynal 		pr_err("nand_scan_ident returned %i", ret);
590a430fa06SMiquel Raynal 		return;
591a430fa06SMiquel Raynal 	}
592a430fa06SMiquel Raynal 
593a430fa06SMiquel Raynal 	/* finish scanning the chip */
594a430fa06SMiquel Raynal 	ret = nand_scan_tail(mtd);
595a430fa06SMiquel Raynal 	if (ret) {
596a430fa06SMiquel Raynal 		pr_err("nand_scan_tail returned %i", ret);
597a430fa06SMiquel Raynal 		return;
598a430fa06SMiquel Raynal 	}
599a430fa06SMiquel Raynal 
600a430fa06SMiquel Raynal 	/* chip is good, register it */
601a430fa06SMiquel Raynal 	ret = nand_register(0, mtd);
602a430fa06SMiquel Raynal 	if (ret)
603a430fa06SMiquel Raynal 		pr_err("nand_register returned %i", ret);
604a430fa06SMiquel Raynal }
605a430fa06SMiquel Raynal 
606a430fa06SMiquel Raynal #else /* defined(CONFIG_SPL_BUILD) */
607a430fa06SMiquel Raynal 
nand_init(void)608a430fa06SMiquel Raynal void nand_init(void)
609a430fa06SMiquel Raynal {
610a430fa06SMiquel Raynal 	/* enable NAND controller */
611a430fa06SMiquel Raynal 	lpc32xx_mlc_nand_init();
612a430fa06SMiquel Raynal 	/* initialize NAND controller */
613a430fa06SMiquel Raynal 	lpc32xx_nand_init();
614a430fa06SMiquel Raynal }
615a430fa06SMiquel Raynal 
nand_deselect(void)616a430fa06SMiquel Raynal void nand_deselect(void)
617a430fa06SMiquel Raynal {
618a430fa06SMiquel Raynal 	/* nothing to do, but SPL requires this function */
619a430fa06SMiquel Raynal }
620a430fa06SMiquel Raynal 
read_single_page(uint8_t * dest,int page,struct lpc32xx_oob * oob)621a430fa06SMiquel Raynal static int read_single_page(uint8_t *dest, int page,
622a430fa06SMiquel Raynal 	struct lpc32xx_oob *oob)
623a430fa06SMiquel Raynal {
624a430fa06SMiquel Raynal 	int status, i, timeout, err, max_bitflips = 0;
625a430fa06SMiquel Raynal 
626a430fa06SMiquel Raynal 	/* enter read mode */
627a430fa06SMiquel Raynal 	writel(NAND_CMD_READ0, &lpc32xx_nand_mlc_registers->cmd);
628a430fa06SMiquel Raynal 	/* send column (lsb then MSB) and page (lsb to MSB) */
629a430fa06SMiquel Raynal 	writel(0, &lpc32xx_nand_mlc_registers->addr);
630a430fa06SMiquel Raynal 	writel(0, &lpc32xx_nand_mlc_registers->addr);
631a430fa06SMiquel Raynal 	writel(page & 0xff, &lpc32xx_nand_mlc_registers->addr);
632a430fa06SMiquel Raynal 	writel((page>>8) & 0xff, &lpc32xx_nand_mlc_registers->addr);
633a430fa06SMiquel Raynal 	writel((page>>16) & 0xff, &lpc32xx_nand_mlc_registers->addr);
634a430fa06SMiquel Raynal 	/* start reading */
635a430fa06SMiquel Raynal 	writel(NAND_CMD_READSTART, &lpc32xx_nand_mlc_registers->cmd);
636a430fa06SMiquel Raynal 
637a430fa06SMiquel Raynal 	/* large page auto decode read */
638a430fa06SMiquel Raynal 	for (i = 0; i < 4; i++) {
639a430fa06SMiquel Raynal 		/* start auto decode (reads 528 NAND bytes) */
640a430fa06SMiquel Raynal 		writel(0, &lpc32xx_nand_mlc_registers->ecc_auto_dec_reg);
641a430fa06SMiquel Raynal 		/* wait for controller to return to ready state */
642a430fa06SMiquel Raynal 		for (timeout = LPC32X_NAND_TIMEOUT; timeout; timeout--) {
643a430fa06SMiquel Raynal 			status = readl(&lpc32xx_nand_mlc_registers->isr);
644a430fa06SMiquel Raynal 			if (status & ISR_CONTROLLER_READY)
645a430fa06SMiquel Raynal 				break;
646a430fa06SMiquel Raynal 			udelay(1);
647a430fa06SMiquel Raynal 		}
648a430fa06SMiquel Raynal 		/* if controller stalled, return error */
649a430fa06SMiquel Raynal 		if (!(status & ISR_CONTROLLER_READY))
650a430fa06SMiquel Raynal 			return -1;
651a430fa06SMiquel Raynal 		/* if decoder failure, return error */
652a430fa06SMiquel Raynal 		if (status & ISR_DECODER_FAILURE)
653a430fa06SMiquel Raynal 			return -1;
654a430fa06SMiquel Raynal 		/* keep count of maximum bitflips performed */
655a430fa06SMiquel Raynal 		if (status & ISR_DECODER_ERROR) {
656a430fa06SMiquel Raynal 			err = ISR_DECODER_ERRORS(status);
657a430fa06SMiquel Raynal 			if (err > max_bitflips)
658a430fa06SMiquel Raynal 				max_bitflips = err;
659a430fa06SMiquel Raynal 		}
660a430fa06SMiquel Raynal 		/* copy first 512 bytes into buffer */
661a430fa06SMiquel Raynal 		memcpy(dest+i*512, lpc32xx_nand_mlc_registers->buff, 512);
662a430fa06SMiquel Raynal 		/* copy next 6 bytes bytes into OOB buffer */
663a430fa06SMiquel Raynal 		memcpy(&oob->free[i], lpc32xx_nand_mlc_registers->buff, 6);
664a430fa06SMiquel Raynal 	}
665a430fa06SMiquel Raynal 	return max_bitflips;
666a430fa06SMiquel Raynal }
667a430fa06SMiquel Raynal 
668a430fa06SMiquel Raynal /*
669a430fa06SMiquel Raynal  * Load U-Boot signed image.
670a430fa06SMiquel Raynal  * This loads an image from NAND, skipping bad blocks.
671a430fa06SMiquel Raynal  * A block is declared bad if at least one of its readable pages has
672a430fa06SMiquel Raynal  * a bad block marker in its OOB at position 0.
673a430fa06SMiquel Raynal  * If all pages ion a block are unreadable, the block is considered
674a430fa06SMiquel Raynal  * bad (i.e., assumed not to be part of the image) and skipped.
675a430fa06SMiquel Raynal  *
676a430fa06SMiquel Raynal  * IMPORTANT NOTE:
677a430fa06SMiquel Raynal  *
678a430fa06SMiquel Raynal  * If the first block of the image is fully unreadable, it will be
679a430fa06SMiquel Raynal  * ignored and skipped as if it had been marked bad. If it was not
680a430fa06SMiquel Raynal  * actually marked bad at the time of writing the image, the resulting
681a430fa06SMiquel Raynal  * image loaded will lack a header and magic number. It could thus be
682a430fa06SMiquel Raynal  * considered as a raw, headerless, image and SPL might erroneously
683a430fa06SMiquel Raynal  * jump into it.
684a430fa06SMiquel Raynal  *
685a430fa06SMiquel Raynal  * In order to avoid this risk, LPC32XX-based boards which use this
686a430fa06SMiquel Raynal  * driver MUST define CONFIG_SPL_PANIC_ON_RAW_IMAGE.
687a430fa06SMiquel Raynal  */
688a430fa06SMiquel Raynal 
689a430fa06SMiquel Raynal #define BYTES_PER_PAGE 2048
690a430fa06SMiquel Raynal #define PAGES_PER_BLOCK 64
691a430fa06SMiquel Raynal #define BYTES_PER_BLOCK (BYTES_PER_PAGE * PAGES_PER_BLOCK)
692a430fa06SMiquel Raynal #define PAGES_PER_CHIP_MAX 524288
693a430fa06SMiquel Raynal 
nand_spl_load_image(uint32_t offs,unsigned int size,void * dst)694a430fa06SMiquel Raynal int nand_spl_load_image(uint32_t offs, unsigned int size, void *dst)
695a430fa06SMiquel Raynal {
696a430fa06SMiquel Raynal 	int bytes_left = size;
697a430fa06SMiquel Raynal 	int pages_left = DIV_ROUND_UP(size, BYTES_PER_PAGE);
698a430fa06SMiquel Raynal 	int blocks_left = DIV_ROUND_UP(size, BYTES_PER_BLOCK);
699a430fa06SMiquel Raynal 	int block = 0;
700a430fa06SMiquel Raynal 	int page = offs / BYTES_PER_PAGE;
701a430fa06SMiquel Raynal 	/* perform reads block by block */
702a430fa06SMiquel Raynal 	while (blocks_left) {
703a430fa06SMiquel Raynal 		/* compute first page number to read */
704a430fa06SMiquel Raynal 		void *block_page_dst = dst;
705a430fa06SMiquel Raynal 		/* read at most one block, possibly less */
706a430fa06SMiquel Raynal 		int block_bytes_left = bytes_left;
707a430fa06SMiquel Raynal 		if (block_bytes_left > BYTES_PER_BLOCK)
708a430fa06SMiquel Raynal 			block_bytes_left = BYTES_PER_BLOCK;
709a430fa06SMiquel Raynal 		/* keep track of good, failed, and "bad" pages */
710a430fa06SMiquel Raynal 		int block_pages_good = 0;
711a430fa06SMiquel Raynal 		int block_pages_bad = 0;
712a430fa06SMiquel Raynal 		int block_pages_err = 0;
713a430fa06SMiquel Raynal 		/* we shall read a full block of pages, maybe less */
714a430fa06SMiquel Raynal 		int block_pages_left = pages_left;
715a430fa06SMiquel Raynal 		if (block_pages_left > PAGES_PER_BLOCK)
716a430fa06SMiquel Raynal 			block_pages_left = PAGES_PER_BLOCK;
717a430fa06SMiquel Raynal 		int block_pages = block_pages_left;
718a430fa06SMiquel Raynal 		int block_page = page;
719a430fa06SMiquel Raynal 		/* while pages are left and the block is not known as bad */
720a430fa06SMiquel Raynal 		while ((block_pages > 0) && (block_pages_bad == 0)) {
721a430fa06SMiquel Raynal 			/* we will read OOB, too, for bad block markers */
722a430fa06SMiquel Raynal 			struct lpc32xx_oob oob;
723a430fa06SMiquel Raynal 			/* read page */
724a430fa06SMiquel Raynal 			int res = read_single_page(block_page_dst, block_page,
725a430fa06SMiquel Raynal 						   &oob);
726a430fa06SMiquel Raynal 			/* count readable pages */
727a430fa06SMiquel Raynal 			if (res >= 0) {
728a430fa06SMiquel Raynal 				/* this page is good */
729a430fa06SMiquel Raynal 				block_pages_good++;
730a430fa06SMiquel Raynal 				/* this page is bad */
731a430fa06SMiquel Raynal 				if ((oob.free[0].free_oob_bytes[0] != 0xff)
732a430fa06SMiquel Raynal 				    | (oob.free[0].free_oob_bytes[1] != 0xff))
733a430fa06SMiquel Raynal 					block_pages_bad++;
734a430fa06SMiquel Raynal 			} else
735a430fa06SMiquel Raynal 				/* count errors */
736a430fa06SMiquel Raynal 				block_pages_err++;
737a430fa06SMiquel Raynal 			/* we're done with this page */
738a430fa06SMiquel Raynal 			block_page++;
739a430fa06SMiquel Raynal 			block_page_dst += BYTES_PER_PAGE;
740a430fa06SMiquel Raynal 			if (block_pages)
741a430fa06SMiquel Raynal 				block_pages--;
742a430fa06SMiquel Raynal 		}
743a430fa06SMiquel Raynal 		/* a fully unreadable block is considered bad */
744a430fa06SMiquel Raynal 		if (block_pages_good == 0)
745a430fa06SMiquel Raynal 			block_pages_bad = block_pages_err;
746a430fa06SMiquel Raynal 		/* errors are fatal only in good blocks */
747a430fa06SMiquel Raynal 		if ((block_pages_err > 0) && (block_pages_bad == 0))
748a430fa06SMiquel Raynal 			return -1;
749a430fa06SMiquel Raynal 		/* we keep reads only of good blocks */
750a430fa06SMiquel Raynal 		if (block_pages_bad == 0) {
751a430fa06SMiquel Raynal 			dst += block_bytes_left;
752a430fa06SMiquel Raynal 			bytes_left -= block_bytes_left;
753a430fa06SMiquel Raynal 			pages_left -= block_pages_left;
754a430fa06SMiquel Raynal 			blocks_left--;
755a430fa06SMiquel Raynal 		}
756a430fa06SMiquel Raynal 		/* good or bad, we're done with this block */
757a430fa06SMiquel Raynal 		block++;
758a430fa06SMiquel Raynal 		page += PAGES_PER_BLOCK;
759a430fa06SMiquel Raynal 	}
760a430fa06SMiquel Raynal 
761a430fa06SMiquel Raynal 	/* report success */
762a430fa06SMiquel Raynal 	return 0;
763a430fa06SMiquel Raynal }
764a430fa06SMiquel Raynal 
765a430fa06SMiquel Raynal #endif /* CONFIG_SPL_BUILD */
766