xref: /rk3399_rockchip-uboot/drivers/mtd/nand/raw/fsl_elbc_nand.c (revision cfcc706c901d603707657919484e4f65467be9ff)
1*cfcc706cSMiquel Raynal /* Freescale Enhanced Local Bus Controller FCM NAND driver
2*cfcc706cSMiquel Raynal  *
3*cfcc706cSMiquel Raynal  * Copyright (c) 2006-2008 Freescale Semiconductor
4*cfcc706cSMiquel Raynal  *
5*cfcc706cSMiquel Raynal  * Authors: Nick Spence <nick.spence@freescale.com>,
6*cfcc706cSMiquel Raynal  *          Scott Wood <scottwood@freescale.com>
7*cfcc706cSMiquel Raynal  *
8*cfcc706cSMiquel Raynal  * SPDX-License-Identifier:	GPL-2.0+
9*cfcc706cSMiquel Raynal  */
10*cfcc706cSMiquel Raynal 
11*cfcc706cSMiquel Raynal #include <common.h>
12*cfcc706cSMiquel Raynal #include <malloc.h>
13*cfcc706cSMiquel Raynal #include <nand.h>
14*cfcc706cSMiquel Raynal 
15*cfcc706cSMiquel Raynal #include <linux/mtd/mtd.h>
16*cfcc706cSMiquel Raynal #include <linux/mtd/rawnand.h>
17*cfcc706cSMiquel Raynal #include <linux/mtd/nand_ecc.h>
18*cfcc706cSMiquel Raynal 
19*cfcc706cSMiquel Raynal #include <asm/io.h>
20*cfcc706cSMiquel Raynal #include <linux/errno.h>
21*cfcc706cSMiquel Raynal 
22*cfcc706cSMiquel Raynal #ifdef VERBOSE_DEBUG
23*cfcc706cSMiquel Raynal #define DEBUG_ELBC
24*cfcc706cSMiquel Raynal #define vdbg(format, arg...) printf("DEBUG: " format, ##arg)
25*cfcc706cSMiquel Raynal #else
26*cfcc706cSMiquel Raynal #define vdbg(format, arg...) do {} while (0)
27*cfcc706cSMiquel Raynal #endif
28*cfcc706cSMiquel Raynal 
29*cfcc706cSMiquel Raynal /* Can't use plain old DEBUG because the linux mtd
30*cfcc706cSMiquel Raynal  * headers define it as a macro.
31*cfcc706cSMiquel Raynal  */
32*cfcc706cSMiquel Raynal #ifdef DEBUG_ELBC
33*cfcc706cSMiquel Raynal #define dbg(format, arg...) printf("DEBUG: " format, ##arg)
34*cfcc706cSMiquel Raynal #else
35*cfcc706cSMiquel Raynal #define dbg(format, arg...) do {} while (0)
36*cfcc706cSMiquel Raynal #endif
37*cfcc706cSMiquel Raynal 
38*cfcc706cSMiquel Raynal #define MAX_BANKS 8
39*cfcc706cSMiquel Raynal #define ERR_BYTE 0xFF /* Value returned for read bytes when read failed */
40*cfcc706cSMiquel Raynal 
41*cfcc706cSMiquel Raynal #define LTESR_NAND_MASK (LTESR_FCT | LTESR_PAR | LTESR_CC)
42*cfcc706cSMiquel Raynal 
43*cfcc706cSMiquel Raynal struct fsl_elbc_ctrl;
44*cfcc706cSMiquel Raynal 
45*cfcc706cSMiquel Raynal /* mtd information per set */
46*cfcc706cSMiquel Raynal 
47*cfcc706cSMiquel Raynal struct fsl_elbc_mtd {
48*cfcc706cSMiquel Raynal 	struct nand_chip chip;
49*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl;
50*cfcc706cSMiquel Raynal 
51*cfcc706cSMiquel Raynal 	struct device *dev;
52*cfcc706cSMiquel Raynal 	int bank;               /* Chip select bank number           */
53*cfcc706cSMiquel Raynal 	u8 __iomem *vbase;      /* Chip select base virtual address  */
54*cfcc706cSMiquel Raynal 	int page_size;          /* NAND page size (0=512, 1=2048)    */
55*cfcc706cSMiquel Raynal 	unsigned int fmr;       /* FCM Flash Mode Register value     */
56*cfcc706cSMiquel Raynal };
57*cfcc706cSMiquel Raynal 
58*cfcc706cSMiquel Raynal /* overview of the fsl elbc controller */
59*cfcc706cSMiquel Raynal 
60*cfcc706cSMiquel Raynal struct fsl_elbc_ctrl {
61*cfcc706cSMiquel Raynal 	struct nand_hw_control controller;
62*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *chips[MAX_BANKS];
63*cfcc706cSMiquel Raynal 
64*cfcc706cSMiquel Raynal 	/* device info */
65*cfcc706cSMiquel Raynal 	fsl_lbc_t *regs;
66*cfcc706cSMiquel Raynal 	u8 __iomem *addr;        /* Address of assigned FCM buffer        */
67*cfcc706cSMiquel Raynal 	unsigned int page;       /* Last page written to / read from      */
68*cfcc706cSMiquel Raynal 	unsigned int read_bytes; /* Number of bytes read during command   */
69*cfcc706cSMiquel Raynal 	unsigned int column;     /* Saved column from SEQIN               */
70*cfcc706cSMiquel Raynal 	unsigned int index;      /* Pointer to next byte to 'read'        */
71*cfcc706cSMiquel Raynal 	unsigned int status;     /* status read from LTESR after last op  */
72*cfcc706cSMiquel Raynal 	unsigned int mdr;        /* UPM/FCM Data Register value           */
73*cfcc706cSMiquel Raynal 	unsigned int use_mdr;    /* Non zero if the MDR is to be set      */
74*cfcc706cSMiquel Raynal 	unsigned int oob;        /* Non zero if operating on OOB data     */
75*cfcc706cSMiquel Raynal };
76*cfcc706cSMiquel Raynal 
77*cfcc706cSMiquel Raynal /* These map to the positions used by the FCM hardware ECC generator */
78*cfcc706cSMiquel Raynal 
79*cfcc706cSMiquel Raynal /* Small Page FLASH with FMR[ECCM] = 0 */
80*cfcc706cSMiquel Raynal static struct nand_ecclayout fsl_elbc_oob_sp_eccm0 = {
81*cfcc706cSMiquel Raynal 	.eccbytes = 3,
82*cfcc706cSMiquel Raynal 	.eccpos = {6, 7, 8},
83*cfcc706cSMiquel Raynal 	.oobfree = { {0, 5}, {9, 7} },
84*cfcc706cSMiquel Raynal };
85*cfcc706cSMiquel Raynal 
86*cfcc706cSMiquel Raynal /* Small Page FLASH with FMR[ECCM] = 1 */
87*cfcc706cSMiquel Raynal static struct nand_ecclayout fsl_elbc_oob_sp_eccm1 = {
88*cfcc706cSMiquel Raynal 	.eccbytes = 3,
89*cfcc706cSMiquel Raynal 	.eccpos = {8, 9, 10},
90*cfcc706cSMiquel Raynal 	.oobfree = { {0, 5}, {6, 2}, {11, 5} },
91*cfcc706cSMiquel Raynal };
92*cfcc706cSMiquel Raynal 
93*cfcc706cSMiquel Raynal /* Large Page FLASH with FMR[ECCM] = 0 */
94*cfcc706cSMiquel Raynal static struct nand_ecclayout fsl_elbc_oob_lp_eccm0 = {
95*cfcc706cSMiquel Raynal 	.eccbytes = 12,
96*cfcc706cSMiquel Raynal 	.eccpos = {6, 7, 8, 22, 23, 24, 38, 39, 40, 54, 55, 56},
97*cfcc706cSMiquel Raynal 	.oobfree = { {1, 5}, {9, 13}, {25, 13}, {41, 13}, {57, 7} },
98*cfcc706cSMiquel Raynal };
99*cfcc706cSMiquel Raynal 
100*cfcc706cSMiquel Raynal /* Large Page FLASH with FMR[ECCM] = 1 */
101*cfcc706cSMiquel Raynal static struct nand_ecclayout fsl_elbc_oob_lp_eccm1 = {
102*cfcc706cSMiquel Raynal 	.eccbytes = 12,
103*cfcc706cSMiquel Raynal 	.eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
104*cfcc706cSMiquel Raynal 	.oobfree = { {1, 7}, {11, 13}, {27, 13}, {43, 13}, {59, 5} },
105*cfcc706cSMiquel Raynal };
106*cfcc706cSMiquel Raynal 
107*cfcc706cSMiquel Raynal /*
108*cfcc706cSMiquel Raynal  * fsl_elbc_oob_lp_eccm* specify that LP NAND's OOB free area starts at offset
109*cfcc706cSMiquel Raynal  * 1, so we have to adjust bad block pattern. This pattern should be used for
110*cfcc706cSMiquel Raynal  * x8 chips only. So far hardware does not support x16 chips anyway.
111*cfcc706cSMiquel Raynal  */
112*cfcc706cSMiquel Raynal static u8 scan_ff_pattern[] = { 0xff, };
113*cfcc706cSMiquel Raynal 
114*cfcc706cSMiquel Raynal static struct nand_bbt_descr largepage_memorybased = {
115*cfcc706cSMiquel Raynal 	.options = 0,
116*cfcc706cSMiquel Raynal 	.offs = 0,
117*cfcc706cSMiquel Raynal 	.len = 1,
118*cfcc706cSMiquel Raynal 	.pattern = scan_ff_pattern,
119*cfcc706cSMiquel Raynal };
120*cfcc706cSMiquel Raynal 
121*cfcc706cSMiquel Raynal /*
122*cfcc706cSMiquel Raynal  * ELBC may use HW ECC, so that OOB offsets, that NAND core uses for bbt,
123*cfcc706cSMiquel Raynal  * interfere with ECC positions, that's why we implement our own descriptors.
124*cfcc706cSMiquel Raynal  * OOB {11, 5}, works for both SP and LP chips, with ECCM = 1 and ECCM = 0.
125*cfcc706cSMiquel Raynal  */
126*cfcc706cSMiquel Raynal static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
127*cfcc706cSMiquel Raynal static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
128*cfcc706cSMiquel Raynal 
129*cfcc706cSMiquel Raynal static struct nand_bbt_descr bbt_main_descr = {
130*cfcc706cSMiquel Raynal 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
131*cfcc706cSMiquel Raynal 		   NAND_BBT_2BIT | NAND_BBT_VERSION,
132*cfcc706cSMiquel Raynal 	.offs =	11,
133*cfcc706cSMiquel Raynal 	.len = 4,
134*cfcc706cSMiquel Raynal 	.veroffs = 15,
135*cfcc706cSMiquel Raynal 	.maxblocks = 4,
136*cfcc706cSMiquel Raynal 	.pattern = bbt_pattern,
137*cfcc706cSMiquel Raynal };
138*cfcc706cSMiquel Raynal 
139*cfcc706cSMiquel Raynal static struct nand_bbt_descr bbt_mirror_descr = {
140*cfcc706cSMiquel Raynal 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
141*cfcc706cSMiquel Raynal 		   NAND_BBT_2BIT | NAND_BBT_VERSION,
142*cfcc706cSMiquel Raynal 	.offs =	11,
143*cfcc706cSMiquel Raynal 	.len = 4,
144*cfcc706cSMiquel Raynal 	.veroffs = 15,
145*cfcc706cSMiquel Raynal 	.maxblocks = 4,
146*cfcc706cSMiquel Raynal 	.pattern = mirror_pattern,
147*cfcc706cSMiquel Raynal };
148*cfcc706cSMiquel Raynal 
149*cfcc706cSMiquel Raynal /*=================================*/
150*cfcc706cSMiquel Raynal 
151*cfcc706cSMiquel Raynal /*
152*cfcc706cSMiquel Raynal  * Set up the FCM hardware block and page address fields, and the fcm
153*cfcc706cSMiquel Raynal  * structure addr field to point to the correct FCM buffer in memory
154*cfcc706cSMiquel Raynal  */
set_addr(struct mtd_info * mtd,int column,int page_addr,int oob)155*cfcc706cSMiquel Raynal static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
156*cfcc706cSMiquel Raynal {
157*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
158*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
159*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
160*cfcc706cSMiquel Raynal 	fsl_lbc_t *lbc = ctrl->regs;
161*cfcc706cSMiquel Raynal 	int buf_num;
162*cfcc706cSMiquel Raynal 
163*cfcc706cSMiquel Raynal 	ctrl->page = page_addr;
164*cfcc706cSMiquel Raynal 
165*cfcc706cSMiquel Raynal 	if (priv->page_size) {
166*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbar, page_addr >> 6);
167*cfcc706cSMiquel Raynal 		out_be32(&lbc->fpar,
168*cfcc706cSMiquel Raynal 			 ((page_addr << FPAR_LP_PI_SHIFT) & FPAR_LP_PI) |
169*cfcc706cSMiquel Raynal 			 (oob ? FPAR_LP_MS : 0) | column);
170*cfcc706cSMiquel Raynal 		buf_num = (page_addr & 1) << 2;
171*cfcc706cSMiquel Raynal 	} else {
172*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbar, page_addr >> 5);
173*cfcc706cSMiquel Raynal 		out_be32(&lbc->fpar,
174*cfcc706cSMiquel Raynal 			 ((page_addr << FPAR_SP_PI_SHIFT) & FPAR_SP_PI) |
175*cfcc706cSMiquel Raynal 			 (oob ? FPAR_SP_MS : 0) | column);
176*cfcc706cSMiquel Raynal 		buf_num = page_addr & 7;
177*cfcc706cSMiquel Raynal 	}
178*cfcc706cSMiquel Raynal 
179*cfcc706cSMiquel Raynal 	ctrl->addr = priv->vbase + buf_num * 1024;
180*cfcc706cSMiquel Raynal 	ctrl->index = column;
181*cfcc706cSMiquel Raynal 
182*cfcc706cSMiquel Raynal 	/* for OOB data point to the second half of the buffer */
183*cfcc706cSMiquel Raynal 	if (oob)
184*cfcc706cSMiquel Raynal 		ctrl->index += priv->page_size ? 2048 : 512;
185*cfcc706cSMiquel Raynal 
186*cfcc706cSMiquel Raynal 	vdbg("set_addr: bank=%d, ctrl->addr=0x%p (0x%p), "
187*cfcc706cSMiquel Raynal 	     "index %x, pes %d ps %d\n",
188*cfcc706cSMiquel Raynal 	     buf_num, ctrl->addr, priv->vbase, ctrl->index,
189*cfcc706cSMiquel Raynal 	     chip->phys_erase_shift, chip->page_shift);
190*cfcc706cSMiquel Raynal }
191*cfcc706cSMiquel Raynal 
192*cfcc706cSMiquel Raynal /*
193*cfcc706cSMiquel Raynal  * execute FCM command and wait for it to complete
194*cfcc706cSMiquel Raynal  */
fsl_elbc_run_command(struct mtd_info * mtd)195*cfcc706cSMiquel Raynal static int fsl_elbc_run_command(struct mtd_info *mtd)
196*cfcc706cSMiquel Raynal {
197*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
198*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
199*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
200*cfcc706cSMiquel Raynal 	fsl_lbc_t *lbc = ctrl->regs;
201*cfcc706cSMiquel Raynal 	u32 timeo = (CONFIG_SYS_HZ * 10) / 1000;
202*cfcc706cSMiquel Raynal 	u32 time_start;
203*cfcc706cSMiquel Raynal 	u32 ltesr;
204*cfcc706cSMiquel Raynal 
205*cfcc706cSMiquel Raynal 	/* Setup the FMR[OP] to execute without write protection */
206*cfcc706cSMiquel Raynal 	out_be32(&lbc->fmr, priv->fmr | 3);
207*cfcc706cSMiquel Raynal 	if (ctrl->use_mdr)
208*cfcc706cSMiquel Raynal 		out_be32(&lbc->mdr, ctrl->mdr);
209*cfcc706cSMiquel Raynal 
210*cfcc706cSMiquel Raynal 	vdbg("fsl_elbc_run_command: fmr=%08x fir=%08x fcr=%08x\n",
211*cfcc706cSMiquel Raynal 	     in_be32(&lbc->fmr), in_be32(&lbc->fir), in_be32(&lbc->fcr));
212*cfcc706cSMiquel Raynal 	vdbg("fsl_elbc_run_command: fbar=%08x fpar=%08x "
213*cfcc706cSMiquel Raynal 	     "fbcr=%08x bank=%d\n",
214*cfcc706cSMiquel Raynal 	     in_be32(&lbc->fbar), in_be32(&lbc->fpar),
215*cfcc706cSMiquel Raynal 	     in_be32(&lbc->fbcr), priv->bank);
216*cfcc706cSMiquel Raynal 
217*cfcc706cSMiquel Raynal 	/* execute special operation */
218*cfcc706cSMiquel Raynal 	out_be32(&lbc->lsor, priv->bank);
219*cfcc706cSMiquel Raynal 
220*cfcc706cSMiquel Raynal 	/* wait for FCM complete flag or timeout */
221*cfcc706cSMiquel Raynal 	time_start = get_timer(0);
222*cfcc706cSMiquel Raynal 
223*cfcc706cSMiquel Raynal 	ltesr = 0;
224*cfcc706cSMiquel Raynal 	while (get_timer(time_start) < timeo) {
225*cfcc706cSMiquel Raynal 		ltesr = in_be32(&lbc->ltesr);
226*cfcc706cSMiquel Raynal 		if (ltesr & LTESR_CC)
227*cfcc706cSMiquel Raynal 			break;
228*cfcc706cSMiquel Raynal 	}
229*cfcc706cSMiquel Raynal 
230*cfcc706cSMiquel Raynal 	ctrl->status = ltesr & LTESR_NAND_MASK;
231*cfcc706cSMiquel Raynal 	out_be32(&lbc->ltesr, ctrl->status);
232*cfcc706cSMiquel Raynal 	out_be32(&lbc->lteatr, 0);
233*cfcc706cSMiquel Raynal 
234*cfcc706cSMiquel Raynal 	/* store mdr value in case it was needed */
235*cfcc706cSMiquel Raynal 	if (ctrl->use_mdr)
236*cfcc706cSMiquel Raynal 		ctrl->mdr = in_be32(&lbc->mdr);
237*cfcc706cSMiquel Raynal 
238*cfcc706cSMiquel Raynal 	ctrl->use_mdr = 0;
239*cfcc706cSMiquel Raynal 
240*cfcc706cSMiquel Raynal 	vdbg("fsl_elbc_run_command: stat=%08x mdr=%08x fmr=%08x\n",
241*cfcc706cSMiquel Raynal 	     ctrl->status, ctrl->mdr, in_be32(&lbc->fmr));
242*cfcc706cSMiquel Raynal 
243*cfcc706cSMiquel Raynal 	/* returns 0 on success otherwise non-zero) */
244*cfcc706cSMiquel Raynal 	return ctrl->status == LTESR_CC ? 0 : -EIO;
245*cfcc706cSMiquel Raynal }
246*cfcc706cSMiquel Raynal 
fsl_elbc_do_read(struct nand_chip * chip,int oob)247*cfcc706cSMiquel Raynal static void fsl_elbc_do_read(struct nand_chip *chip, int oob)
248*cfcc706cSMiquel Raynal {
249*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
250*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
251*cfcc706cSMiquel Raynal 	fsl_lbc_t *lbc = ctrl->regs;
252*cfcc706cSMiquel Raynal 
253*cfcc706cSMiquel Raynal 	if (priv->page_size) {
254*cfcc706cSMiquel Raynal 		out_be32(&lbc->fir,
255*cfcc706cSMiquel Raynal 			 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
256*cfcc706cSMiquel Raynal 			 (FIR_OP_CA  << FIR_OP1_SHIFT) |
257*cfcc706cSMiquel Raynal 			 (FIR_OP_PA  << FIR_OP2_SHIFT) |
258*cfcc706cSMiquel Raynal 			 (FIR_OP_CW1 << FIR_OP3_SHIFT) |
259*cfcc706cSMiquel Raynal 			 (FIR_OP_RBW << FIR_OP4_SHIFT));
260*cfcc706cSMiquel Raynal 
261*cfcc706cSMiquel Raynal 		out_be32(&lbc->fcr, (NAND_CMD_READ0 << FCR_CMD0_SHIFT) |
262*cfcc706cSMiquel Raynal 				    (NAND_CMD_READSTART << FCR_CMD1_SHIFT));
263*cfcc706cSMiquel Raynal 	} else {
264*cfcc706cSMiquel Raynal 		out_be32(&lbc->fir,
265*cfcc706cSMiquel Raynal 			 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
266*cfcc706cSMiquel Raynal 			 (FIR_OP_CA  << FIR_OP1_SHIFT) |
267*cfcc706cSMiquel Raynal 			 (FIR_OP_PA  << FIR_OP2_SHIFT) |
268*cfcc706cSMiquel Raynal 			 (FIR_OP_RBW << FIR_OP3_SHIFT));
269*cfcc706cSMiquel Raynal 
270*cfcc706cSMiquel Raynal 		if (oob)
271*cfcc706cSMiquel Raynal 			out_be32(&lbc->fcr,
272*cfcc706cSMiquel Raynal 				 NAND_CMD_READOOB << FCR_CMD0_SHIFT);
273*cfcc706cSMiquel Raynal 		else
274*cfcc706cSMiquel Raynal 			out_be32(&lbc->fcr, NAND_CMD_READ0 << FCR_CMD0_SHIFT);
275*cfcc706cSMiquel Raynal 	}
276*cfcc706cSMiquel Raynal }
277*cfcc706cSMiquel Raynal 
278*cfcc706cSMiquel Raynal /* cmdfunc send commands to the FCM */
fsl_elbc_cmdfunc(struct mtd_info * mtd,unsigned int command,int column,int page_addr)279*cfcc706cSMiquel Raynal static void fsl_elbc_cmdfunc(struct mtd_info *mtd, unsigned int command,
280*cfcc706cSMiquel Raynal 			     int column, int page_addr)
281*cfcc706cSMiquel Raynal {
282*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
283*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
284*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
285*cfcc706cSMiquel Raynal 	fsl_lbc_t *lbc = ctrl->regs;
286*cfcc706cSMiquel Raynal 
287*cfcc706cSMiquel Raynal 	ctrl->use_mdr = 0;
288*cfcc706cSMiquel Raynal 
289*cfcc706cSMiquel Raynal 	/* clear the read buffer */
290*cfcc706cSMiquel Raynal 	ctrl->read_bytes = 0;
291*cfcc706cSMiquel Raynal 	if (command != NAND_CMD_PAGEPROG)
292*cfcc706cSMiquel Raynal 		ctrl->index = 0;
293*cfcc706cSMiquel Raynal 
294*cfcc706cSMiquel Raynal 	switch (command) {
295*cfcc706cSMiquel Raynal 	/* READ0 and READ1 read the entire buffer to use hardware ECC. */
296*cfcc706cSMiquel Raynal 	case NAND_CMD_READ1:
297*cfcc706cSMiquel Raynal 		column += 256;
298*cfcc706cSMiquel Raynal 
299*cfcc706cSMiquel Raynal 	/* fall-through */
300*cfcc706cSMiquel Raynal 	case NAND_CMD_READ0:
301*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD_READ0, page_addr:"
302*cfcc706cSMiquel Raynal 		     " 0x%x, column: 0x%x.\n", page_addr, column);
303*cfcc706cSMiquel Raynal 
304*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbcr, 0); /* read entire page to enable ECC */
305*cfcc706cSMiquel Raynal 		set_addr(mtd, 0, page_addr, 0);
306*cfcc706cSMiquel Raynal 
307*cfcc706cSMiquel Raynal 		ctrl->read_bytes = mtd->writesize + mtd->oobsize;
308*cfcc706cSMiquel Raynal 		ctrl->index += column;
309*cfcc706cSMiquel Raynal 
310*cfcc706cSMiquel Raynal 		fsl_elbc_do_read(chip, 0);
311*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
312*cfcc706cSMiquel Raynal 		return;
313*cfcc706cSMiquel Raynal 
314*cfcc706cSMiquel Raynal 	/* READOOB reads only the OOB because no ECC is performed. */
315*cfcc706cSMiquel Raynal 	case NAND_CMD_READOOB:
316*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD_READOOB, page_addr:"
317*cfcc706cSMiquel Raynal 		     " 0x%x, column: 0x%x.\n", page_addr, column);
318*cfcc706cSMiquel Raynal 
319*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbcr, mtd->oobsize - column);
320*cfcc706cSMiquel Raynal 		set_addr(mtd, column, page_addr, 1);
321*cfcc706cSMiquel Raynal 
322*cfcc706cSMiquel Raynal 		ctrl->read_bytes = mtd->writesize + mtd->oobsize;
323*cfcc706cSMiquel Raynal 
324*cfcc706cSMiquel Raynal 		fsl_elbc_do_read(chip, 1);
325*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
326*cfcc706cSMiquel Raynal 
327*cfcc706cSMiquel Raynal 		return;
328*cfcc706cSMiquel Raynal 
329*cfcc706cSMiquel Raynal 	/* READID must read all 5 possible bytes while CEB is active */
330*cfcc706cSMiquel Raynal 	case NAND_CMD_READID:
331*cfcc706cSMiquel Raynal 	case NAND_CMD_PARAM:
332*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD 0x%x.\n", command);
333*cfcc706cSMiquel Raynal 
334*cfcc706cSMiquel Raynal 		out_be32(&lbc->fir, (FIR_OP_CW0 << FIR_OP0_SHIFT) |
335*cfcc706cSMiquel Raynal 				    (FIR_OP_UA  << FIR_OP1_SHIFT) |
336*cfcc706cSMiquel Raynal 				    (FIR_OP_RBW << FIR_OP2_SHIFT));
337*cfcc706cSMiquel Raynal 		out_be32(&lbc->fcr, command << FCR_CMD0_SHIFT);
338*cfcc706cSMiquel Raynal 		/*
339*cfcc706cSMiquel Raynal 		 * although currently it's 8 bytes for READID, we always read
340*cfcc706cSMiquel Raynal 		 * the maximum 256 bytes(for PARAM)
341*cfcc706cSMiquel Raynal 		 */
342*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbcr, 256);
343*cfcc706cSMiquel Raynal 		ctrl->read_bytes = 256;
344*cfcc706cSMiquel Raynal 		ctrl->use_mdr = 1;
345*cfcc706cSMiquel Raynal 		ctrl->mdr = column;
346*cfcc706cSMiquel Raynal 		set_addr(mtd, 0, 0, 0);
347*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
348*cfcc706cSMiquel Raynal 		return;
349*cfcc706cSMiquel Raynal 
350*cfcc706cSMiquel Raynal 	/* ERASE1 stores the block and page address */
351*cfcc706cSMiquel Raynal 	case NAND_CMD_ERASE1:
352*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE1, "
353*cfcc706cSMiquel Raynal 		     "page_addr: 0x%x.\n", page_addr);
354*cfcc706cSMiquel Raynal 		set_addr(mtd, 0, page_addr, 0);
355*cfcc706cSMiquel Raynal 		return;
356*cfcc706cSMiquel Raynal 
357*cfcc706cSMiquel Raynal 	/* ERASE2 uses the block and page address from ERASE1 */
358*cfcc706cSMiquel Raynal 	case NAND_CMD_ERASE2:
359*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD_ERASE2.\n");
360*cfcc706cSMiquel Raynal 
361*cfcc706cSMiquel Raynal 		out_be32(&lbc->fir,
362*cfcc706cSMiquel Raynal 			 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
363*cfcc706cSMiquel Raynal 			 (FIR_OP_PA  << FIR_OP1_SHIFT) |
364*cfcc706cSMiquel Raynal 			 (FIR_OP_CM1 << FIR_OP2_SHIFT));
365*cfcc706cSMiquel Raynal 
366*cfcc706cSMiquel Raynal 		out_be32(&lbc->fcr,
367*cfcc706cSMiquel Raynal 			 (NAND_CMD_ERASE1 << FCR_CMD0_SHIFT) |
368*cfcc706cSMiquel Raynal 			 (NAND_CMD_ERASE2 << FCR_CMD1_SHIFT));
369*cfcc706cSMiquel Raynal 
370*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbcr, 0);
371*cfcc706cSMiquel Raynal 		ctrl->read_bytes = 0;
372*cfcc706cSMiquel Raynal 
373*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
374*cfcc706cSMiquel Raynal 		return;
375*cfcc706cSMiquel Raynal 
376*cfcc706cSMiquel Raynal 	/* SEQIN sets up the addr buffer and all registers except the length */
377*cfcc706cSMiquel Raynal 	case NAND_CMD_SEQIN: {
378*cfcc706cSMiquel Raynal 		u32 fcr;
379*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, "
380*cfcc706cSMiquel Raynal 		     "page_addr: 0x%x, column: 0x%x.\n",
381*cfcc706cSMiquel Raynal 		     page_addr, column);
382*cfcc706cSMiquel Raynal 
383*cfcc706cSMiquel Raynal 		ctrl->column = column;
384*cfcc706cSMiquel Raynal 		ctrl->oob = 0;
385*cfcc706cSMiquel Raynal 
386*cfcc706cSMiquel Raynal 		if (priv->page_size) {
387*cfcc706cSMiquel Raynal 			fcr = (NAND_CMD_SEQIN << FCR_CMD0_SHIFT) |
388*cfcc706cSMiquel Raynal 			      (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT);
389*cfcc706cSMiquel Raynal 
390*cfcc706cSMiquel Raynal 			out_be32(&lbc->fir,
391*cfcc706cSMiquel Raynal 				 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
392*cfcc706cSMiquel Raynal 				 (FIR_OP_CA  << FIR_OP1_SHIFT) |
393*cfcc706cSMiquel Raynal 				 (FIR_OP_PA  << FIR_OP2_SHIFT) |
394*cfcc706cSMiquel Raynal 				 (FIR_OP_WB  << FIR_OP3_SHIFT) |
395*cfcc706cSMiquel Raynal 				 (FIR_OP_CW1 << FIR_OP4_SHIFT));
396*cfcc706cSMiquel Raynal 		} else {
397*cfcc706cSMiquel Raynal 			fcr = (NAND_CMD_PAGEPROG << FCR_CMD1_SHIFT) |
398*cfcc706cSMiquel Raynal 			      (NAND_CMD_SEQIN << FCR_CMD2_SHIFT);
399*cfcc706cSMiquel Raynal 
400*cfcc706cSMiquel Raynal 			out_be32(&lbc->fir,
401*cfcc706cSMiquel Raynal 				 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
402*cfcc706cSMiquel Raynal 				 (FIR_OP_CM2 << FIR_OP1_SHIFT) |
403*cfcc706cSMiquel Raynal 				 (FIR_OP_CA  << FIR_OP2_SHIFT) |
404*cfcc706cSMiquel Raynal 				 (FIR_OP_PA  << FIR_OP3_SHIFT) |
405*cfcc706cSMiquel Raynal 				 (FIR_OP_WB  << FIR_OP4_SHIFT) |
406*cfcc706cSMiquel Raynal 				 (FIR_OP_CW1 << FIR_OP5_SHIFT));
407*cfcc706cSMiquel Raynal 
408*cfcc706cSMiquel Raynal 			if (column >= mtd->writesize) {
409*cfcc706cSMiquel Raynal 				/* OOB area --> READOOB */
410*cfcc706cSMiquel Raynal 				column -= mtd->writesize;
411*cfcc706cSMiquel Raynal 				fcr |= NAND_CMD_READOOB << FCR_CMD0_SHIFT;
412*cfcc706cSMiquel Raynal 				ctrl->oob = 1;
413*cfcc706cSMiquel Raynal 			} else if (column < 256) {
414*cfcc706cSMiquel Raynal 				/* First 256 bytes --> READ0 */
415*cfcc706cSMiquel Raynal 				fcr |= NAND_CMD_READ0 << FCR_CMD0_SHIFT;
416*cfcc706cSMiquel Raynal 			} else {
417*cfcc706cSMiquel Raynal 				/* Second 256 bytes --> READ1 */
418*cfcc706cSMiquel Raynal 				fcr |= NAND_CMD_READ1 << FCR_CMD0_SHIFT;
419*cfcc706cSMiquel Raynal 			}
420*cfcc706cSMiquel Raynal 		}
421*cfcc706cSMiquel Raynal 
422*cfcc706cSMiquel Raynal 		out_be32(&lbc->fcr, fcr);
423*cfcc706cSMiquel Raynal 		set_addr(mtd, column, page_addr, ctrl->oob);
424*cfcc706cSMiquel Raynal 		return;
425*cfcc706cSMiquel Raynal 	}
426*cfcc706cSMiquel Raynal 
427*cfcc706cSMiquel Raynal 	/* PAGEPROG reuses all of the setup from SEQIN and adds the length */
428*cfcc706cSMiquel Raynal 	case NAND_CMD_PAGEPROG: {
429*cfcc706cSMiquel Raynal 		vdbg("fsl_elbc_cmdfunc: NAND_CMD_PAGEPROG "
430*cfcc706cSMiquel Raynal 		     "writing %d bytes.\n", ctrl->index);
431*cfcc706cSMiquel Raynal 
432*cfcc706cSMiquel Raynal 		/* if the write did not start at 0 or is not a full page
433*cfcc706cSMiquel Raynal 		 * then set the exact length, otherwise use a full page
434*cfcc706cSMiquel Raynal 		 * write so the HW generates the ECC.
435*cfcc706cSMiquel Raynal 		 */
436*cfcc706cSMiquel Raynal 		if (ctrl->oob || ctrl->column != 0 ||
437*cfcc706cSMiquel Raynal 		    ctrl->index != mtd->writesize + mtd->oobsize)
438*cfcc706cSMiquel Raynal 			out_be32(&lbc->fbcr, ctrl->index);
439*cfcc706cSMiquel Raynal 		else
440*cfcc706cSMiquel Raynal 			out_be32(&lbc->fbcr, 0);
441*cfcc706cSMiquel Raynal 
442*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
443*cfcc706cSMiquel Raynal 
444*cfcc706cSMiquel Raynal 		return;
445*cfcc706cSMiquel Raynal 	}
446*cfcc706cSMiquel Raynal 
447*cfcc706cSMiquel Raynal 	/* CMD_STATUS must read the status byte while CEB is active */
448*cfcc706cSMiquel Raynal 	/* Note - it does not wait for the ready line */
449*cfcc706cSMiquel Raynal 	case NAND_CMD_STATUS:
450*cfcc706cSMiquel Raynal 		out_be32(&lbc->fir,
451*cfcc706cSMiquel Raynal 			 (FIR_OP_CM0 << FIR_OP0_SHIFT) |
452*cfcc706cSMiquel Raynal 			 (FIR_OP_RBW << FIR_OP1_SHIFT));
453*cfcc706cSMiquel Raynal 		out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
454*cfcc706cSMiquel Raynal 		out_be32(&lbc->fbcr, 1);
455*cfcc706cSMiquel Raynal 		set_addr(mtd, 0, 0, 0);
456*cfcc706cSMiquel Raynal 		ctrl->read_bytes = 1;
457*cfcc706cSMiquel Raynal 
458*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
459*cfcc706cSMiquel Raynal 
460*cfcc706cSMiquel Raynal 		/* The chip always seems to report that it is
461*cfcc706cSMiquel Raynal 		 * write-protected, even when it is not.
462*cfcc706cSMiquel Raynal 		 */
463*cfcc706cSMiquel Raynal 		out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
464*cfcc706cSMiquel Raynal 		return;
465*cfcc706cSMiquel Raynal 
466*cfcc706cSMiquel Raynal 	/* RESET without waiting for the ready line */
467*cfcc706cSMiquel Raynal 	case NAND_CMD_RESET:
468*cfcc706cSMiquel Raynal 		dbg("fsl_elbc_cmdfunc: NAND_CMD_RESET.\n");
469*cfcc706cSMiquel Raynal 		out_be32(&lbc->fir, FIR_OP_CM0 << FIR_OP0_SHIFT);
470*cfcc706cSMiquel Raynal 		out_be32(&lbc->fcr, NAND_CMD_RESET << FCR_CMD0_SHIFT);
471*cfcc706cSMiquel Raynal 		fsl_elbc_run_command(mtd);
472*cfcc706cSMiquel Raynal 		return;
473*cfcc706cSMiquel Raynal 
474*cfcc706cSMiquel Raynal 	default:
475*cfcc706cSMiquel Raynal 		printf("fsl_elbc_cmdfunc: error, unsupported command 0x%x.\n",
476*cfcc706cSMiquel Raynal 			command);
477*cfcc706cSMiquel Raynal 	}
478*cfcc706cSMiquel Raynal }
479*cfcc706cSMiquel Raynal 
fsl_elbc_select_chip(struct mtd_info * mtd,int chip)480*cfcc706cSMiquel Raynal static void fsl_elbc_select_chip(struct mtd_info *mtd, int chip)
481*cfcc706cSMiquel Raynal {
482*cfcc706cSMiquel Raynal 	/* The hardware does not seem to support multiple
483*cfcc706cSMiquel Raynal 	 * chips per bank.
484*cfcc706cSMiquel Raynal 	 */
485*cfcc706cSMiquel Raynal }
486*cfcc706cSMiquel Raynal 
487*cfcc706cSMiquel Raynal /*
488*cfcc706cSMiquel Raynal  * Write buf to the FCM Controller Data Buffer
489*cfcc706cSMiquel Raynal  */
fsl_elbc_write_buf(struct mtd_info * mtd,const u8 * buf,int len)490*cfcc706cSMiquel Raynal static void fsl_elbc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
491*cfcc706cSMiquel Raynal {
492*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
493*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
494*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
495*cfcc706cSMiquel Raynal 	unsigned int bufsize = mtd->writesize + mtd->oobsize;
496*cfcc706cSMiquel Raynal 
497*cfcc706cSMiquel Raynal 	if (len <= 0) {
498*cfcc706cSMiquel Raynal 		printf("write_buf of %d bytes", len);
499*cfcc706cSMiquel Raynal 		ctrl->status = 0;
500*cfcc706cSMiquel Raynal 		return;
501*cfcc706cSMiquel Raynal 	}
502*cfcc706cSMiquel Raynal 
503*cfcc706cSMiquel Raynal 	if ((unsigned int)len > bufsize - ctrl->index) {
504*cfcc706cSMiquel Raynal 		printf("write_buf beyond end of buffer "
505*cfcc706cSMiquel Raynal 		       "(%d requested, %u available)\n",
506*cfcc706cSMiquel Raynal 		       len, bufsize - ctrl->index);
507*cfcc706cSMiquel Raynal 		len = bufsize - ctrl->index;
508*cfcc706cSMiquel Raynal 	}
509*cfcc706cSMiquel Raynal 
510*cfcc706cSMiquel Raynal 	memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
511*cfcc706cSMiquel Raynal 	/*
512*cfcc706cSMiquel Raynal 	 * This is workaround for the weird elbc hangs during nand write,
513*cfcc706cSMiquel Raynal 	 * Scott Wood says: "...perhaps difference in how long it takes a
514*cfcc706cSMiquel Raynal 	 * write to make it through the localbus compared to a write to IMMR
515*cfcc706cSMiquel Raynal 	 * is causing problems, and sync isn't helping for some reason."
516*cfcc706cSMiquel Raynal 	 * Reading back the last byte helps though.
517*cfcc706cSMiquel Raynal 	 */
518*cfcc706cSMiquel Raynal 	in_8(&ctrl->addr[ctrl->index] + len - 1);
519*cfcc706cSMiquel Raynal 
520*cfcc706cSMiquel Raynal 	ctrl->index += len;
521*cfcc706cSMiquel Raynal }
522*cfcc706cSMiquel Raynal 
523*cfcc706cSMiquel Raynal /*
524*cfcc706cSMiquel Raynal  * read a byte from either the FCM hardware buffer if it has any data left
525*cfcc706cSMiquel Raynal  * otherwise issue a command to read a single byte.
526*cfcc706cSMiquel Raynal  */
fsl_elbc_read_byte(struct mtd_info * mtd)527*cfcc706cSMiquel Raynal static u8 fsl_elbc_read_byte(struct mtd_info *mtd)
528*cfcc706cSMiquel Raynal {
529*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
530*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
531*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
532*cfcc706cSMiquel Raynal 
533*cfcc706cSMiquel Raynal 	/* If there are still bytes in the FCM, then use the next byte. */
534*cfcc706cSMiquel Raynal 	if (ctrl->index < ctrl->read_bytes)
535*cfcc706cSMiquel Raynal 		return in_8(&ctrl->addr[ctrl->index++]);
536*cfcc706cSMiquel Raynal 
537*cfcc706cSMiquel Raynal 	printf("read_byte beyond end of buffer\n");
538*cfcc706cSMiquel Raynal 	return ERR_BYTE;
539*cfcc706cSMiquel Raynal }
540*cfcc706cSMiquel Raynal 
541*cfcc706cSMiquel Raynal /*
542*cfcc706cSMiquel Raynal  * Read from the FCM Controller Data Buffer
543*cfcc706cSMiquel Raynal  */
fsl_elbc_read_buf(struct mtd_info * mtd,u8 * buf,int len)544*cfcc706cSMiquel Raynal static void fsl_elbc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
545*cfcc706cSMiquel Raynal {
546*cfcc706cSMiquel Raynal 	struct nand_chip *chip = mtd_to_nand(mtd);
547*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
548*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
549*cfcc706cSMiquel Raynal 	int avail;
550*cfcc706cSMiquel Raynal 
551*cfcc706cSMiquel Raynal 	if (len < 0)
552*cfcc706cSMiquel Raynal 		return;
553*cfcc706cSMiquel Raynal 
554*cfcc706cSMiquel Raynal 	avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
555*cfcc706cSMiquel Raynal 	memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
556*cfcc706cSMiquel Raynal 	ctrl->index += avail;
557*cfcc706cSMiquel Raynal 
558*cfcc706cSMiquel Raynal 	if (len > avail)
559*cfcc706cSMiquel Raynal 		printf("read_buf beyond end of buffer "
560*cfcc706cSMiquel Raynal 		       "(%d requested, %d available)\n",
561*cfcc706cSMiquel Raynal 		       len, avail);
562*cfcc706cSMiquel Raynal }
563*cfcc706cSMiquel Raynal 
564*cfcc706cSMiquel Raynal /* This function is called after Program and Erase Operations to
565*cfcc706cSMiquel Raynal  * check for success or failure.
566*cfcc706cSMiquel Raynal  */
fsl_elbc_wait(struct mtd_info * mtd,struct nand_chip * chip)567*cfcc706cSMiquel Raynal static int fsl_elbc_wait(struct mtd_info *mtd, struct nand_chip *chip)
568*cfcc706cSMiquel Raynal {
569*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv = nand_get_controller_data(chip);
570*cfcc706cSMiquel Raynal 	struct fsl_elbc_ctrl *ctrl = priv->ctrl;
571*cfcc706cSMiquel Raynal 	fsl_lbc_t *lbc = ctrl->regs;
572*cfcc706cSMiquel Raynal 
573*cfcc706cSMiquel Raynal 	if (ctrl->status != LTESR_CC)
574*cfcc706cSMiquel Raynal 		return NAND_STATUS_FAIL;
575*cfcc706cSMiquel Raynal 
576*cfcc706cSMiquel Raynal 	/* Use READ_STATUS command, but wait for the device to be ready */
577*cfcc706cSMiquel Raynal 	ctrl->use_mdr = 0;
578*cfcc706cSMiquel Raynal 	out_be32(&lbc->fir,
579*cfcc706cSMiquel Raynal 		 (FIR_OP_CW0 << FIR_OP0_SHIFT) |
580*cfcc706cSMiquel Raynal 		 (FIR_OP_RBW << FIR_OP1_SHIFT));
581*cfcc706cSMiquel Raynal 	out_be32(&lbc->fcr, NAND_CMD_STATUS << FCR_CMD0_SHIFT);
582*cfcc706cSMiquel Raynal 	out_be32(&lbc->fbcr, 1);
583*cfcc706cSMiquel Raynal 	set_addr(mtd, 0, 0, 0);
584*cfcc706cSMiquel Raynal 	ctrl->read_bytes = 1;
585*cfcc706cSMiquel Raynal 
586*cfcc706cSMiquel Raynal 	fsl_elbc_run_command(mtd);
587*cfcc706cSMiquel Raynal 
588*cfcc706cSMiquel Raynal 	if (ctrl->status != LTESR_CC)
589*cfcc706cSMiquel Raynal 		return NAND_STATUS_FAIL;
590*cfcc706cSMiquel Raynal 
591*cfcc706cSMiquel Raynal 	/* The chip always seems to report that it is
592*cfcc706cSMiquel Raynal 	 * write-protected, even when it is not.
593*cfcc706cSMiquel Raynal 	 */
594*cfcc706cSMiquel Raynal 	out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
595*cfcc706cSMiquel Raynal 	return fsl_elbc_read_byte(mtd);
596*cfcc706cSMiquel Raynal }
597*cfcc706cSMiquel Raynal 
fsl_elbc_read_page(struct mtd_info * mtd,struct nand_chip * chip,uint8_t * buf,int oob_required,int page)598*cfcc706cSMiquel Raynal static int fsl_elbc_read_page(struct mtd_info *mtd, struct nand_chip *chip,
599*cfcc706cSMiquel Raynal 			      uint8_t *buf, int oob_required, int page)
600*cfcc706cSMiquel Raynal {
601*cfcc706cSMiquel Raynal 	fsl_elbc_read_buf(mtd, buf, mtd->writesize);
602*cfcc706cSMiquel Raynal 	fsl_elbc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
603*cfcc706cSMiquel Raynal 
604*cfcc706cSMiquel Raynal 	if (fsl_elbc_wait(mtd, chip) & NAND_STATUS_FAIL)
605*cfcc706cSMiquel Raynal 		mtd->ecc_stats.failed++;
606*cfcc706cSMiquel Raynal 
607*cfcc706cSMiquel Raynal 	return 0;
608*cfcc706cSMiquel Raynal }
609*cfcc706cSMiquel Raynal 
610*cfcc706cSMiquel Raynal /* ECC will be calculated automatically, and errors will be detected in
611*cfcc706cSMiquel Raynal  * waitfunc.
612*cfcc706cSMiquel Raynal  */
fsl_elbc_write_page(struct mtd_info * mtd,struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)613*cfcc706cSMiquel Raynal static int fsl_elbc_write_page(struct mtd_info *mtd, struct nand_chip *chip,
614*cfcc706cSMiquel Raynal 				const uint8_t *buf, int oob_required,
615*cfcc706cSMiquel Raynal 				int page)
616*cfcc706cSMiquel Raynal {
617*cfcc706cSMiquel Raynal 	fsl_elbc_write_buf(mtd, buf, mtd->writesize);
618*cfcc706cSMiquel Raynal 	fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
619*cfcc706cSMiquel Raynal 
620*cfcc706cSMiquel Raynal 	return 0;
621*cfcc706cSMiquel Raynal }
622*cfcc706cSMiquel Raynal 
623*cfcc706cSMiquel Raynal static struct fsl_elbc_ctrl *elbc_ctrl;
624*cfcc706cSMiquel Raynal 
625*cfcc706cSMiquel Raynal /* ECC will be calculated automatically, and errors will be detected in
626*cfcc706cSMiquel Raynal  * waitfunc.
627*cfcc706cSMiquel Raynal  */
fsl_elbc_write_subpage(struct mtd_info * mtd,struct nand_chip * chip,uint32_t offset,uint32_t data_len,const uint8_t * buf,int oob_required,int page)628*cfcc706cSMiquel Raynal static int fsl_elbc_write_subpage(struct mtd_info *mtd, struct nand_chip *chip,
629*cfcc706cSMiquel Raynal 				uint32_t offset, uint32_t data_len,
630*cfcc706cSMiquel Raynal 				const uint8_t *buf, int oob_required, int page)
631*cfcc706cSMiquel Raynal {
632*cfcc706cSMiquel Raynal 	fsl_elbc_write_buf(mtd, buf, mtd->writesize);
633*cfcc706cSMiquel Raynal 	fsl_elbc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
634*cfcc706cSMiquel Raynal 
635*cfcc706cSMiquel Raynal 	return 0;
636*cfcc706cSMiquel Raynal }
637*cfcc706cSMiquel Raynal 
fsl_elbc_ctrl_init(void)638*cfcc706cSMiquel Raynal static void fsl_elbc_ctrl_init(void)
639*cfcc706cSMiquel Raynal {
640*cfcc706cSMiquel Raynal 	elbc_ctrl = kzalloc(sizeof(*elbc_ctrl), GFP_KERNEL);
641*cfcc706cSMiquel Raynal 	if (!elbc_ctrl)
642*cfcc706cSMiquel Raynal 		return;
643*cfcc706cSMiquel Raynal 
644*cfcc706cSMiquel Raynal 	elbc_ctrl->regs = LBC_BASE_ADDR;
645*cfcc706cSMiquel Raynal 
646*cfcc706cSMiquel Raynal 	/* clear event registers */
647*cfcc706cSMiquel Raynal 	out_be32(&elbc_ctrl->regs->ltesr, LTESR_NAND_MASK);
648*cfcc706cSMiquel Raynal 	out_be32(&elbc_ctrl->regs->lteatr, 0);
649*cfcc706cSMiquel Raynal 
650*cfcc706cSMiquel Raynal 	/* Enable interrupts for any detected events */
651*cfcc706cSMiquel Raynal 	out_be32(&elbc_ctrl->regs->lteir, LTESR_NAND_MASK);
652*cfcc706cSMiquel Raynal 
653*cfcc706cSMiquel Raynal 	elbc_ctrl->read_bytes = 0;
654*cfcc706cSMiquel Raynal 	elbc_ctrl->index = 0;
655*cfcc706cSMiquel Raynal 	elbc_ctrl->addr = NULL;
656*cfcc706cSMiquel Raynal }
657*cfcc706cSMiquel Raynal 
fsl_elbc_chip_init(int devnum,u8 * addr)658*cfcc706cSMiquel Raynal static int fsl_elbc_chip_init(int devnum, u8 *addr)
659*cfcc706cSMiquel Raynal {
660*cfcc706cSMiquel Raynal 	struct mtd_info *mtd;
661*cfcc706cSMiquel Raynal 	struct nand_chip *nand;
662*cfcc706cSMiquel Raynal 	struct fsl_elbc_mtd *priv;
663*cfcc706cSMiquel Raynal 	uint32_t br = 0, or = 0;
664*cfcc706cSMiquel Raynal 	int ret;
665*cfcc706cSMiquel Raynal 
666*cfcc706cSMiquel Raynal 	if (!elbc_ctrl) {
667*cfcc706cSMiquel Raynal 		fsl_elbc_ctrl_init();
668*cfcc706cSMiquel Raynal 		if (!elbc_ctrl)
669*cfcc706cSMiquel Raynal 			return -1;
670*cfcc706cSMiquel Raynal 	}
671*cfcc706cSMiquel Raynal 
672*cfcc706cSMiquel Raynal 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
673*cfcc706cSMiquel Raynal 	if (!priv)
674*cfcc706cSMiquel Raynal 		return -ENOMEM;
675*cfcc706cSMiquel Raynal 
676*cfcc706cSMiquel Raynal 	priv->ctrl = elbc_ctrl;
677*cfcc706cSMiquel Raynal 	priv->vbase = addr;
678*cfcc706cSMiquel Raynal 
679*cfcc706cSMiquel Raynal 	/* Find which chip select it is connected to.  It'd be nice
680*cfcc706cSMiquel Raynal 	 * if we could pass more than one datum to the NAND driver...
681*cfcc706cSMiquel Raynal 	 */
682*cfcc706cSMiquel Raynal 	for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
683*cfcc706cSMiquel Raynal 		phys_addr_t phys_addr = virt_to_phys(addr);
684*cfcc706cSMiquel Raynal 
685*cfcc706cSMiquel Raynal 		br = in_be32(&elbc_ctrl->regs->bank[priv->bank].br);
686*cfcc706cSMiquel Raynal 		or = in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
687*cfcc706cSMiquel Raynal 
688*cfcc706cSMiquel Raynal 		if ((br & BR_V) && (br & BR_MSEL) == BR_MS_FCM &&
689*cfcc706cSMiquel Raynal 		    (br & or & BR_BA) == BR_PHYS_ADDR(phys_addr))
690*cfcc706cSMiquel Raynal 			break;
691*cfcc706cSMiquel Raynal 	}
692*cfcc706cSMiquel Raynal 
693*cfcc706cSMiquel Raynal 	if (priv->bank >= MAX_BANKS) {
694*cfcc706cSMiquel Raynal 		printf("fsl_elbc_nand: address did not match any "
695*cfcc706cSMiquel Raynal 		       "chip selects\n");
696*cfcc706cSMiquel Raynal 		kfree(priv);
697*cfcc706cSMiquel Raynal 		return -ENODEV;
698*cfcc706cSMiquel Raynal 	}
699*cfcc706cSMiquel Raynal 
700*cfcc706cSMiquel Raynal 	nand = &priv->chip;
701*cfcc706cSMiquel Raynal 	mtd = nand_to_mtd(nand);
702*cfcc706cSMiquel Raynal 
703*cfcc706cSMiquel Raynal 	elbc_ctrl->chips[priv->bank] = priv;
704*cfcc706cSMiquel Raynal 
705*cfcc706cSMiquel Raynal 	/* fill in nand_chip structure */
706*cfcc706cSMiquel Raynal 	/* set up function call table */
707*cfcc706cSMiquel Raynal 	nand->read_byte = fsl_elbc_read_byte;
708*cfcc706cSMiquel Raynal 	nand->write_buf = fsl_elbc_write_buf;
709*cfcc706cSMiquel Raynal 	nand->read_buf = fsl_elbc_read_buf;
710*cfcc706cSMiquel Raynal 	nand->select_chip = fsl_elbc_select_chip;
711*cfcc706cSMiquel Raynal 	nand->cmdfunc = fsl_elbc_cmdfunc;
712*cfcc706cSMiquel Raynal 	nand->waitfunc = fsl_elbc_wait;
713*cfcc706cSMiquel Raynal 
714*cfcc706cSMiquel Raynal 	/* set up nand options */
715*cfcc706cSMiquel Raynal 	nand->bbt_td = &bbt_main_descr;
716*cfcc706cSMiquel Raynal 	nand->bbt_md = &bbt_mirror_descr;
717*cfcc706cSMiquel Raynal 
718*cfcc706cSMiquel Raynal   	/* set up nand options */
719*cfcc706cSMiquel Raynal 	nand->options = NAND_NO_SUBPAGE_WRITE;
720*cfcc706cSMiquel Raynal 	nand->bbt_options = NAND_BBT_USE_FLASH;
721*cfcc706cSMiquel Raynal 
722*cfcc706cSMiquel Raynal 	nand->controller = &elbc_ctrl->controller;
723*cfcc706cSMiquel Raynal 	nand_set_controller_data(nand, priv);
724*cfcc706cSMiquel Raynal 
725*cfcc706cSMiquel Raynal 	nand->ecc.read_page = fsl_elbc_read_page;
726*cfcc706cSMiquel Raynal 	nand->ecc.write_page = fsl_elbc_write_page;
727*cfcc706cSMiquel Raynal 	nand->ecc.write_subpage = fsl_elbc_write_subpage;
728*cfcc706cSMiquel Raynal 
729*cfcc706cSMiquel Raynal 	priv->fmr = (15 << FMR_CWTO_SHIFT) | (2 << FMR_AL_SHIFT);
730*cfcc706cSMiquel Raynal 
731*cfcc706cSMiquel Raynal 	/* If CS Base Register selects full hardware ECC then use it */
732*cfcc706cSMiquel Raynal 	if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
733*cfcc706cSMiquel Raynal 		nand->ecc.mode = NAND_ECC_HW;
734*cfcc706cSMiquel Raynal 
735*cfcc706cSMiquel Raynal 		nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
736*cfcc706cSMiquel Raynal 				   &fsl_elbc_oob_sp_eccm1 :
737*cfcc706cSMiquel Raynal 				   &fsl_elbc_oob_sp_eccm0;
738*cfcc706cSMiquel Raynal 
739*cfcc706cSMiquel Raynal 		nand->ecc.size = 512;
740*cfcc706cSMiquel Raynal 		nand->ecc.bytes = 3;
741*cfcc706cSMiquel Raynal 		nand->ecc.steps = 1;
742*cfcc706cSMiquel Raynal 		nand->ecc.strength = 1;
743*cfcc706cSMiquel Raynal 	} else {
744*cfcc706cSMiquel Raynal 		/* otherwise fall back to software ECC */
745*cfcc706cSMiquel Raynal #if defined(CONFIG_NAND_ECC_BCH)
746*cfcc706cSMiquel Raynal 		nand->ecc.mode = NAND_ECC_SOFT_BCH;
747*cfcc706cSMiquel Raynal #else
748*cfcc706cSMiquel Raynal 		nand->ecc.mode = NAND_ECC_SOFT;
749*cfcc706cSMiquel Raynal #endif
750*cfcc706cSMiquel Raynal 	}
751*cfcc706cSMiquel Raynal 
752*cfcc706cSMiquel Raynal 	ret = nand_scan_ident(mtd, 1, NULL);
753*cfcc706cSMiquel Raynal 	if (ret)
754*cfcc706cSMiquel Raynal 		return ret;
755*cfcc706cSMiquel Raynal 
756*cfcc706cSMiquel Raynal 	/* Large-page-specific setup */
757*cfcc706cSMiquel Raynal 	if (mtd->writesize == 2048) {
758*cfcc706cSMiquel Raynal 		setbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
759*cfcc706cSMiquel Raynal 			     OR_FCM_PGS);
760*cfcc706cSMiquel Raynal 		in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
761*cfcc706cSMiquel Raynal 
762*cfcc706cSMiquel Raynal 		priv->page_size = 1;
763*cfcc706cSMiquel Raynal 		nand->badblock_pattern = &largepage_memorybased;
764*cfcc706cSMiquel Raynal 
765*cfcc706cSMiquel Raynal 		/*
766*cfcc706cSMiquel Raynal 		 * Hardware expects small page has ECCM0, large page has
767*cfcc706cSMiquel Raynal 		 * ECCM1 when booting from NAND, and we follow that even
768*cfcc706cSMiquel Raynal 		 * when not booting from NAND.
769*cfcc706cSMiquel Raynal 		 */
770*cfcc706cSMiquel Raynal 		priv->fmr |= FMR_ECCM;
771*cfcc706cSMiquel Raynal 
772*cfcc706cSMiquel Raynal 		/* adjust ecc setup if needed */
773*cfcc706cSMiquel Raynal 		if ((br & BR_DECC) == BR_DECC_CHK_GEN) {
774*cfcc706cSMiquel Raynal 			nand->ecc.steps = 4;
775*cfcc706cSMiquel Raynal 			nand->ecc.layout = (priv->fmr & FMR_ECCM) ?
776*cfcc706cSMiquel Raynal 					   &fsl_elbc_oob_lp_eccm1 :
777*cfcc706cSMiquel Raynal 					   &fsl_elbc_oob_lp_eccm0;
778*cfcc706cSMiquel Raynal 		}
779*cfcc706cSMiquel Raynal 	} else if (mtd->writesize == 512) {
780*cfcc706cSMiquel Raynal 		clrbits_be32(&elbc_ctrl->regs->bank[priv->bank].or,
781*cfcc706cSMiquel Raynal 			     OR_FCM_PGS);
782*cfcc706cSMiquel Raynal 		in_be32(&elbc_ctrl->regs->bank[priv->bank].or);
783*cfcc706cSMiquel Raynal 	} else {
784*cfcc706cSMiquel Raynal 		return -ENODEV;
785*cfcc706cSMiquel Raynal 	}
786*cfcc706cSMiquel Raynal 
787*cfcc706cSMiquel Raynal 	ret = nand_scan_tail(mtd);
788*cfcc706cSMiquel Raynal 	if (ret)
789*cfcc706cSMiquel Raynal 		return ret;
790*cfcc706cSMiquel Raynal 
791*cfcc706cSMiquel Raynal 	ret = nand_register(devnum, mtd);
792*cfcc706cSMiquel Raynal 	if (ret)
793*cfcc706cSMiquel Raynal 		return ret;
794*cfcc706cSMiquel Raynal 
795*cfcc706cSMiquel Raynal 	return 0;
796*cfcc706cSMiquel Raynal }
797*cfcc706cSMiquel Raynal 
798*cfcc706cSMiquel Raynal #ifndef CONFIG_SYS_NAND_BASE_LIST
799*cfcc706cSMiquel Raynal #define CONFIG_SYS_NAND_BASE_LIST { CONFIG_SYS_NAND_BASE }
800*cfcc706cSMiquel Raynal #endif
801*cfcc706cSMiquel Raynal 
802*cfcc706cSMiquel Raynal static unsigned long base_address[CONFIG_SYS_MAX_NAND_DEVICE] =
803*cfcc706cSMiquel Raynal 	CONFIG_SYS_NAND_BASE_LIST;
804*cfcc706cSMiquel Raynal 
board_nand_init(void)805*cfcc706cSMiquel Raynal void board_nand_init(void)
806*cfcc706cSMiquel Raynal {
807*cfcc706cSMiquel Raynal 	int i;
808*cfcc706cSMiquel Raynal 
809*cfcc706cSMiquel Raynal 	for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
810*cfcc706cSMiquel Raynal 		fsl_elbc_chip_init(i, (u8 *)base_address[i]);
811*cfcc706cSMiquel Raynal }
812