xref: /OK3568_Linux_fs/kernel/drivers/mtd/nand/raw/mxc_nand.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0+
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
4*4882a593Smuzhiyun  * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/delay.h>
8*4882a593Smuzhiyun #include <linux/slab.h>
9*4882a593Smuzhiyun #include <linux/init.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/mtd/mtd.h>
12*4882a593Smuzhiyun #include <linux/mtd/rawnand.h>
13*4882a593Smuzhiyun #include <linux/mtd/partitions.h>
14*4882a593Smuzhiyun #include <linux/interrupt.h>
15*4882a593Smuzhiyun #include <linux/device.h>
16*4882a593Smuzhiyun #include <linux/platform_device.h>
17*4882a593Smuzhiyun #include <linux/clk.h>
18*4882a593Smuzhiyun #include <linux/err.h>
19*4882a593Smuzhiyun #include <linux/io.h>
20*4882a593Smuzhiyun #include <linux/irq.h>
21*4882a593Smuzhiyun #include <linux/completion.h>
22*4882a593Smuzhiyun #include <linux/of.h>
23*4882a593Smuzhiyun #include <linux/of_device.h>
24*4882a593Smuzhiyun #include <linux/platform_data/mtd-mxc_nand.h>
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #define DRIVER_NAME "mxc_nand"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun /* Addresses for NFC registers */
29*4882a593Smuzhiyun #define NFC_V1_V2_BUF_SIZE		(host->regs + 0x00)
30*4882a593Smuzhiyun #define NFC_V1_V2_BUF_ADDR		(host->regs + 0x04)
31*4882a593Smuzhiyun #define NFC_V1_V2_FLASH_ADDR		(host->regs + 0x06)
32*4882a593Smuzhiyun #define NFC_V1_V2_FLASH_CMD		(host->regs + 0x08)
33*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG		(host->regs + 0x0a)
34*4882a593Smuzhiyun #define NFC_V1_V2_ECC_STATUS_RESULT	(host->regs + 0x0c)
35*4882a593Smuzhiyun #define NFC_V1_V2_RSLTMAIN_AREA		(host->regs + 0x0e)
36*4882a593Smuzhiyun #define NFC_V21_RSLTSPARE_AREA		(host->regs + 0x10)
37*4882a593Smuzhiyun #define NFC_V1_V2_WRPROT		(host->regs + 0x12)
38*4882a593Smuzhiyun #define NFC_V1_UNLOCKSTART_BLKADDR	(host->regs + 0x14)
39*4882a593Smuzhiyun #define NFC_V1_UNLOCKEND_BLKADDR	(host->regs + 0x16)
40*4882a593Smuzhiyun #define NFC_V21_UNLOCKSTART_BLKADDR0	(host->regs + 0x20)
41*4882a593Smuzhiyun #define NFC_V21_UNLOCKSTART_BLKADDR1	(host->regs + 0x24)
42*4882a593Smuzhiyun #define NFC_V21_UNLOCKSTART_BLKADDR2	(host->regs + 0x28)
43*4882a593Smuzhiyun #define NFC_V21_UNLOCKSTART_BLKADDR3	(host->regs + 0x2c)
44*4882a593Smuzhiyun #define NFC_V21_UNLOCKEND_BLKADDR0	(host->regs + 0x22)
45*4882a593Smuzhiyun #define NFC_V21_UNLOCKEND_BLKADDR1	(host->regs + 0x26)
46*4882a593Smuzhiyun #define NFC_V21_UNLOCKEND_BLKADDR2	(host->regs + 0x2a)
47*4882a593Smuzhiyun #define NFC_V21_UNLOCKEND_BLKADDR3	(host->regs + 0x2e)
48*4882a593Smuzhiyun #define NFC_V1_V2_NF_WRPRST		(host->regs + 0x18)
49*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1		(host->regs + 0x1a)
50*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG2		(host->regs + 0x1c)
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun #define NFC_V2_CONFIG1_ECC_MODE_4	(1 << 0)
53*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1_SP_EN		(1 << 2)
54*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1_ECC_EN	(1 << 3)
55*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1_INT_MSK	(1 << 4)
56*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1_BIG		(1 << 5)
57*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1_RST		(1 << 6)
58*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG1_CE		(1 << 7)
59*4882a593Smuzhiyun #define NFC_V2_CONFIG1_ONE_CYCLE	(1 << 8)
60*4882a593Smuzhiyun #define NFC_V2_CONFIG1_PPB(x)		(((x) & 0x3) << 9)
61*4882a593Smuzhiyun #define NFC_V2_CONFIG1_FP_INT		(1 << 11)
62*4882a593Smuzhiyun 
63*4882a593Smuzhiyun #define NFC_V1_V2_CONFIG2_INT		(1 << 15)
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /*
66*4882a593Smuzhiyun  * Operation modes for the NFC. Valid for v1, v2 and v3
67*4882a593Smuzhiyun  * type controllers.
68*4882a593Smuzhiyun  */
69*4882a593Smuzhiyun #define NFC_CMD				(1 << 0)
70*4882a593Smuzhiyun #define NFC_ADDR			(1 << 1)
71*4882a593Smuzhiyun #define NFC_INPUT			(1 << 2)
72*4882a593Smuzhiyun #define NFC_OUTPUT			(1 << 3)
73*4882a593Smuzhiyun #define NFC_ID				(1 << 4)
74*4882a593Smuzhiyun #define NFC_STATUS			(1 << 5)
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define NFC_V3_FLASH_CMD		(host->regs_axi + 0x00)
77*4882a593Smuzhiyun #define NFC_V3_FLASH_ADDR0		(host->regs_axi + 0x04)
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun #define NFC_V3_CONFIG1			(host->regs_axi + 0x34)
80*4882a593Smuzhiyun #define NFC_V3_CONFIG1_SP_EN		(1 << 0)
81*4882a593Smuzhiyun #define NFC_V3_CONFIG1_RBA(x)		(((x) & 0x7 ) << 4)
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun #define NFC_V3_ECC_STATUS_RESULT	(host->regs_axi + 0x38)
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define NFC_V3_LAUNCH			(host->regs_axi + 0x40)
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun #define NFC_V3_WRPROT			(host->regs_ip + 0x0)
88*4882a593Smuzhiyun #define NFC_V3_WRPROT_LOCK_TIGHT	(1 << 0)
89*4882a593Smuzhiyun #define NFC_V3_WRPROT_LOCK		(1 << 1)
90*4882a593Smuzhiyun #define NFC_V3_WRPROT_UNLOCK		(1 << 2)
91*4882a593Smuzhiyun #define NFC_V3_WRPROT_BLS_UNLOCK	(2 << 6)
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0   (host->regs_ip + 0x04)
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun #define NFC_V3_CONFIG2			(host->regs_ip + 0x24)
96*4882a593Smuzhiyun #define NFC_V3_CONFIG2_PS_512			(0 << 0)
97*4882a593Smuzhiyun #define NFC_V3_CONFIG2_PS_2048			(1 << 0)
98*4882a593Smuzhiyun #define NFC_V3_CONFIG2_PS_4096			(2 << 0)
99*4882a593Smuzhiyun #define NFC_V3_CONFIG2_ONE_CYCLE		(1 << 2)
100*4882a593Smuzhiyun #define NFC_V3_CONFIG2_ECC_EN			(1 << 3)
101*4882a593Smuzhiyun #define NFC_V3_CONFIG2_2CMD_PHASES		(1 << 4)
102*4882a593Smuzhiyun #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0		(1 << 5)
103*4882a593Smuzhiyun #define NFC_V3_CONFIG2_ECC_MODE_8		(1 << 6)
104*4882a593Smuzhiyun #define NFC_V3_CONFIG2_PPB(x, shift)		(((x) & 0x3) << shift)
105*4882a593Smuzhiyun #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x)	(((x) & 0x3) << 12)
106*4882a593Smuzhiyun #define NFC_V3_CONFIG2_INT_MSK			(1 << 15)
107*4882a593Smuzhiyun #define NFC_V3_CONFIG2_ST_CMD(x)		(((x) & 0xff) << 24)
108*4882a593Smuzhiyun #define NFC_V3_CONFIG2_SPAS(x)			(((x) & 0xff) << 16)
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun #define NFC_V3_CONFIG3				(host->regs_ip + 0x28)
111*4882a593Smuzhiyun #define NFC_V3_CONFIG3_ADD_OP(x)		(((x) & 0x3) << 0)
112*4882a593Smuzhiyun #define NFC_V3_CONFIG3_FW8			(1 << 3)
113*4882a593Smuzhiyun #define NFC_V3_CONFIG3_SBB(x)			(((x) & 0x7) << 8)
114*4882a593Smuzhiyun #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x)	(((x) & 0x7) << 12)
115*4882a593Smuzhiyun #define NFC_V3_CONFIG3_RBB_MODE			(1 << 15)
116*4882a593Smuzhiyun #define NFC_V3_CONFIG3_NO_SDMA			(1 << 20)
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #define NFC_V3_IPC			(host->regs_ip + 0x2C)
119*4882a593Smuzhiyun #define NFC_V3_IPC_CREQ			(1 << 0)
120*4882a593Smuzhiyun #define NFC_V3_IPC_INT			(1 << 31)
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun #define NFC_V3_DELAY_LINE		(host->regs_ip + 0x34)
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun struct mxc_nand_host;
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun struct mxc_nand_devtype_data {
127*4882a593Smuzhiyun 	void (*preset)(struct mtd_info *);
128*4882a593Smuzhiyun 	int (*read_page)(struct nand_chip *chip, void *buf, void *oob, bool ecc,
129*4882a593Smuzhiyun 			 int page);
130*4882a593Smuzhiyun 	void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
131*4882a593Smuzhiyun 	void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
132*4882a593Smuzhiyun 	void (*send_page)(struct mtd_info *, unsigned int);
133*4882a593Smuzhiyun 	void (*send_read_id)(struct mxc_nand_host *);
134*4882a593Smuzhiyun 	uint16_t (*get_dev_status)(struct mxc_nand_host *);
135*4882a593Smuzhiyun 	int (*check_int)(struct mxc_nand_host *);
136*4882a593Smuzhiyun 	void (*irq_control)(struct mxc_nand_host *, int);
137*4882a593Smuzhiyun 	u32 (*get_ecc_status)(struct mxc_nand_host *);
138*4882a593Smuzhiyun 	const struct mtd_ooblayout_ops *ooblayout;
139*4882a593Smuzhiyun 	void (*select_chip)(struct nand_chip *chip, int cs);
140*4882a593Smuzhiyun 	int (*setup_interface)(struct nand_chip *chip, int csline,
141*4882a593Smuzhiyun 			       const struct nand_interface_config *conf);
142*4882a593Smuzhiyun 	void (*enable_hwecc)(struct nand_chip *chip, bool enable);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	/*
145*4882a593Smuzhiyun 	 * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
146*4882a593Smuzhiyun 	 * (CONFIG1:INT_MSK is set). To handle this the driver uses
147*4882a593Smuzhiyun 	 * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
148*4882a593Smuzhiyun 	 */
149*4882a593Smuzhiyun 	int irqpending_quirk;
150*4882a593Smuzhiyun 	int needs_ip;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	size_t regs_offset;
153*4882a593Smuzhiyun 	size_t spare0_offset;
154*4882a593Smuzhiyun 	size_t axi_offset;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	int spare_len;
157*4882a593Smuzhiyun 	int eccbytes;
158*4882a593Smuzhiyun 	int eccsize;
159*4882a593Smuzhiyun 	int ppb_shift;
160*4882a593Smuzhiyun };
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun struct mxc_nand_host {
163*4882a593Smuzhiyun 	struct nand_chip	nand;
164*4882a593Smuzhiyun 	struct device		*dev;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	void __iomem		*spare0;
167*4882a593Smuzhiyun 	void __iomem		*main_area0;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	void __iomem		*base;
170*4882a593Smuzhiyun 	void __iomem		*regs;
171*4882a593Smuzhiyun 	void __iomem		*regs_axi;
172*4882a593Smuzhiyun 	void __iomem		*regs_ip;
173*4882a593Smuzhiyun 	int			status_request;
174*4882a593Smuzhiyun 	struct clk		*clk;
175*4882a593Smuzhiyun 	int			clk_act;
176*4882a593Smuzhiyun 	int			irq;
177*4882a593Smuzhiyun 	int			eccsize;
178*4882a593Smuzhiyun 	int			used_oobsize;
179*4882a593Smuzhiyun 	int			active_cs;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	struct completion	op_completion;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	uint8_t			*data_buf;
184*4882a593Smuzhiyun 	unsigned int		buf_start;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 	const struct mxc_nand_devtype_data *devtype_data;
187*4882a593Smuzhiyun 	struct mxc_nand_platform_data pdata;
188*4882a593Smuzhiyun };
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun static const char * const part_probes[] = {
191*4882a593Smuzhiyun 	"cmdlinepart", "RedBoot", "ofpart", NULL };
192*4882a593Smuzhiyun 
memcpy32_fromio(void * trg,const void __iomem * src,size_t size)193*4882a593Smuzhiyun static void memcpy32_fromio(void *trg, const void __iomem  *src, size_t size)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun 	int i;
196*4882a593Smuzhiyun 	u32 *t = trg;
197*4882a593Smuzhiyun 	const __iomem u32 *s = src;
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	for (i = 0; i < (size >> 2); i++)
200*4882a593Smuzhiyun 		*t++ = __raw_readl(s++);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
memcpy16_fromio(void * trg,const void __iomem * src,size_t size)203*4882a593Smuzhiyun static void memcpy16_fromio(void *trg, const void __iomem  *src, size_t size)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	int i;
206*4882a593Smuzhiyun 	u16 *t = trg;
207*4882a593Smuzhiyun 	const __iomem u16 *s = src;
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	/* We assume that src (IO) is always 32bit aligned */
210*4882a593Smuzhiyun 	if (PTR_ALIGN(trg, 4) == trg && IS_ALIGNED(size, 4)) {
211*4882a593Smuzhiyun 		memcpy32_fromio(trg, src, size);
212*4882a593Smuzhiyun 		return;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	for (i = 0; i < (size >> 1); i++)
216*4882a593Smuzhiyun 		*t++ = __raw_readw(s++);
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
memcpy32_toio(void __iomem * trg,const void * src,int size)219*4882a593Smuzhiyun static inline void memcpy32_toio(void __iomem *trg, const void *src, int size)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	/* __iowrite32_copy use 32bit size values so divide by 4 */
222*4882a593Smuzhiyun 	__iowrite32_copy(trg, src, size / 4);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun 
memcpy16_toio(void __iomem * trg,const void * src,int size)225*4882a593Smuzhiyun static void memcpy16_toio(void __iomem *trg, const void *src, int size)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun 	int i;
228*4882a593Smuzhiyun 	__iomem u16 *t = trg;
229*4882a593Smuzhiyun 	const u16 *s = src;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* We assume that trg (IO) is always 32bit aligned */
232*4882a593Smuzhiyun 	if (PTR_ALIGN(src, 4) == src && IS_ALIGNED(size, 4)) {
233*4882a593Smuzhiyun 		memcpy32_toio(trg, src, size);
234*4882a593Smuzhiyun 		return;
235*4882a593Smuzhiyun 	}
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	for (i = 0; i < (size >> 1); i++)
238*4882a593Smuzhiyun 		__raw_writew(*s++, t++);
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun /*
242*4882a593Smuzhiyun  * The controller splits a page into data chunks of 512 bytes + partial oob.
243*4882a593Smuzhiyun  * There are writesize / 512 such chunks, the size of the partial oob parts is
244*4882a593Smuzhiyun  * oobsize / #chunks rounded down to a multiple of 2. The last oob chunk then
245*4882a593Smuzhiyun  * contains additionally the byte lost by rounding (if any).
246*4882a593Smuzhiyun  * This function handles the needed shuffling between host->data_buf (which
247*4882a593Smuzhiyun  * holds a page in natural order, i.e. writesize bytes data + oobsize bytes
248*4882a593Smuzhiyun  * spare) and the NFC buffer.
249*4882a593Smuzhiyun  */
copy_spare(struct mtd_info * mtd,bool bfrom,void * buf)250*4882a593Smuzhiyun static void copy_spare(struct mtd_info *mtd, bool bfrom, void *buf)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	struct nand_chip *this = mtd_to_nand(mtd);
253*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(this);
254*4882a593Smuzhiyun 	u16 i, oob_chunk_size;
255*4882a593Smuzhiyun 	u16 num_chunks = mtd->writesize / 512;
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	u8 *d = buf;
258*4882a593Smuzhiyun 	u8 __iomem *s = host->spare0;
259*4882a593Smuzhiyun 	u16 sparebuf_size = host->devtype_data->spare_len;
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun 	/* size of oob chunk for all but possibly the last one */
262*4882a593Smuzhiyun 	oob_chunk_size = (host->used_oobsize / num_chunks) & ~1;
263*4882a593Smuzhiyun 
264*4882a593Smuzhiyun 	if (bfrom) {
265*4882a593Smuzhiyun 		for (i = 0; i < num_chunks - 1; i++)
266*4882a593Smuzhiyun 			memcpy16_fromio(d + i * oob_chunk_size,
267*4882a593Smuzhiyun 					s + i * sparebuf_size,
268*4882a593Smuzhiyun 					oob_chunk_size);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 		/* the last chunk */
271*4882a593Smuzhiyun 		memcpy16_fromio(d + i * oob_chunk_size,
272*4882a593Smuzhiyun 				s + i * sparebuf_size,
273*4882a593Smuzhiyun 				host->used_oobsize - i * oob_chunk_size);
274*4882a593Smuzhiyun 	} else {
275*4882a593Smuzhiyun 		for (i = 0; i < num_chunks - 1; i++)
276*4882a593Smuzhiyun 			memcpy16_toio(&s[i * sparebuf_size],
277*4882a593Smuzhiyun 				      &d[i * oob_chunk_size],
278*4882a593Smuzhiyun 				      oob_chunk_size);
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		/* the last chunk */
281*4882a593Smuzhiyun 		memcpy16_toio(&s[i * sparebuf_size],
282*4882a593Smuzhiyun 			      &d[i * oob_chunk_size],
283*4882a593Smuzhiyun 			      host->used_oobsize - i * oob_chunk_size);
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun /*
288*4882a593Smuzhiyun  * MXC NANDFC can only perform full page+spare or spare-only read/write.  When
289*4882a593Smuzhiyun  * the upper layers perform a read/write buf operation, the saved column address
290*4882a593Smuzhiyun  * is used to index into the full page. So usually this function is called with
291*4882a593Smuzhiyun  * column == 0 (unless no column cycle is needed indicated by column == -1)
292*4882a593Smuzhiyun  */
mxc_do_addr_cycle(struct mtd_info * mtd,int column,int page_addr)293*4882a593Smuzhiyun static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
294*4882a593Smuzhiyun {
295*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
296*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/* Write out column address, if necessary */
299*4882a593Smuzhiyun 	if (column != -1) {
300*4882a593Smuzhiyun 		host->devtype_data->send_addr(host, column & 0xff,
301*4882a593Smuzhiyun 					      page_addr == -1);
302*4882a593Smuzhiyun 		if (mtd->writesize > 512)
303*4882a593Smuzhiyun 			/* another col addr cycle for 2k page */
304*4882a593Smuzhiyun 			host->devtype_data->send_addr(host,
305*4882a593Smuzhiyun 						      (column >> 8) & 0xff,
306*4882a593Smuzhiyun 						      false);
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/* Write out page address, if necessary */
310*4882a593Smuzhiyun 	if (page_addr != -1) {
311*4882a593Smuzhiyun 		/* paddr_0 - p_addr_7 */
312*4882a593Smuzhiyun 		host->devtype_data->send_addr(host, (page_addr & 0xff), false);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 		if (mtd->writesize > 512) {
315*4882a593Smuzhiyun 			if (mtd->size >= 0x10000000) {
316*4882a593Smuzhiyun 				/* paddr_8 - paddr_15 */
317*4882a593Smuzhiyun 				host->devtype_data->send_addr(host,
318*4882a593Smuzhiyun 						(page_addr >> 8) & 0xff,
319*4882a593Smuzhiyun 						false);
320*4882a593Smuzhiyun 				host->devtype_data->send_addr(host,
321*4882a593Smuzhiyun 						(page_addr >> 16) & 0xff,
322*4882a593Smuzhiyun 						true);
323*4882a593Smuzhiyun 			} else
324*4882a593Smuzhiyun 				/* paddr_8 - paddr_15 */
325*4882a593Smuzhiyun 				host->devtype_data->send_addr(host,
326*4882a593Smuzhiyun 						(page_addr >> 8) & 0xff, true);
327*4882a593Smuzhiyun 		} else {
328*4882a593Smuzhiyun 			if (nand_chip->options & NAND_ROW_ADDR_3) {
329*4882a593Smuzhiyun 				/* paddr_8 - paddr_15 */
330*4882a593Smuzhiyun 				host->devtype_data->send_addr(host,
331*4882a593Smuzhiyun 						(page_addr >> 8) & 0xff,
332*4882a593Smuzhiyun 						false);
333*4882a593Smuzhiyun 				host->devtype_data->send_addr(host,
334*4882a593Smuzhiyun 						(page_addr >> 16) & 0xff,
335*4882a593Smuzhiyun 						true);
336*4882a593Smuzhiyun 			} else
337*4882a593Smuzhiyun 				/* paddr_8 - paddr_15 */
338*4882a593Smuzhiyun 				host->devtype_data->send_addr(host,
339*4882a593Smuzhiyun 						(page_addr >> 8) & 0xff, true);
340*4882a593Smuzhiyun 		}
341*4882a593Smuzhiyun 	}
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
check_int_v3(struct mxc_nand_host * host)344*4882a593Smuzhiyun static int check_int_v3(struct mxc_nand_host *host)
345*4882a593Smuzhiyun {
346*4882a593Smuzhiyun 	uint32_t tmp;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	tmp = readl(NFC_V3_IPC);
349*4882a593Smuzhiyun 	if (!(tmp & NFC_V3_IPC_INT))
350*4882a593Smuzhiyun 		return 0;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	tmp &= ~NFC_V3_IPC_INT;
353*4882a593Smuzhiyun 	writel(tmp, NFC_V3_IPC);
354*4882a593Smuzhiyun 
355*4882a593Smuzhiyun 	return 1;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
check_int_v1_v2(struct mxc_nand_host * host)358*4882a593Smuzhiyun static int check_int_v1_v2(struct mxc_nand_host *host)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	uint32_t tmp;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	tmp = readw(NFC_V1_V2_CONFIG2);
363*4882a593Smuzhiyun 	if (!(tmp & NFC_V1_V2_CONFIG2_INT))
364*4882a593Smuzhiyun 		return 0;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (!host->devtype_data->irqpending_quirk)
367*4882a593Smuzhiyun 		writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	return 1;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun 
irq_control_v1_v2(struct mxc_nand_host * host,int activate)372*4882a593Smuzhiyun static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	uint16_t tmp;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	tmp = readw(NFC_V1_V2_CONFIG1);
377*4882a593Smuzhiyun 
378*4882a593Smuzhiyun 	if (activate)
379*4882a593Smuzhiyun 		tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
380*4882a593Smuzhiyun 	else
381*4882a593Smuzhiyun 		tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	writew(tmp, NFC_V1_V2_CONFIG1);
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun 
irq_control_v3(struct mxc_nand_host * host,int activate)386*4882a593Smuzhiyun static void irq_control_v3(struct mxc_nand_host *host, int activate)
387*4882a593Smuzhiyun {
388*4882a593Smuzhiyun 	uint32_t tmp;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	tmp = readl(NFC_V3_CONFIG2);
391*4882a593Smuzhiyun 
392*4882a593Smuzhiyun 	if (activate)
393*4882a593Smuzhiyun 		tmp &= ~NFC_V3_CONFIG2_INT_MSK;
394*4882a593Smuzhiyun 	else
395*4882a593Smuzhiyun 		tmp |= NFC_V3_CONFIG2_INT_MSK;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	writel(tmp, NFC_V3_CONFIG2);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
irq_control(struct mxc_nand_host * host,int activate)400*4882a593Smuzhiyun static void irq_control(struct mxc_nand_host *host, int activate)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	if (host->devtype_data->irqpending_quirk) {
403*4882a593Smuzhiyun 		if (activate)
404*4882a593Smuzhiyun 			enable_irq(host->irq);
405*4882a593Smuzhiyun 		else
406*4882a593Smuzhiyun 			disable_irq_nosync(host->irq);
407*4882a593Smuzhiyun 	} else {
408*4882a593Smuzhiyun 		host->devtype_data->irq_control(host, activate);
409*4882a593Smuzhiyun 	}
410*4882a593Smuzhiyun }
411*4882a593Smuzhiyun 
get_ecc_status_v1(struct mxc_nand_host * host)412*4882a593Smuzhiyun static u32 get_ecc_status_v1(struct mxc_nand_host *host)
413*4882a593Smuzhiyun {
414*4882a593Smuzhiyun 	return readw(NFC_V1_V2_ECC_STATUS_RESULT);
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun 
get_ecc_status_v2(struct mxc_nand_host * host)417*4882a593Smuzhiyun static u32 get_ecc_status_v2(struct mxc_nand_host *host)
418*4882a593Smuzhiyun {
419*4882a593Smuzhiyun 	return readl(NFC_V1_V2_ECC_STATUS_RESULT);
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun 
get_ecc_status_v3(struct mxc_nand_host * host)422*4882a593Smuzhiyun static u32 get_ecc_status_v3(struct mxc_nand_host *host)
423*4882a593Smuzhiyun {
424*4882a593Smuzhiyun 	return readl(NFC_V3_ECC_STATUS_RESULT);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun 
mxc_nfc_irq(int irq,void * dev_id)427*4882a593Smuzhiyun static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun 	struct mxc_nand_host *host = dev_id;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	if (!host->devtype_data->check_int(host))
432*4882a593Smuzhiyun 		return IRQ_NONE;
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	irq_control(host, 0);
435*4882a593Smuzhiyun 
436*4882a593Smuzhiyun 	complete(&host->op_completion);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	return IRQ_HANDLED;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
441*4882a593Smuzhiyun /* This function polls the NANDFC to wait for the basic operation to
442*4882a593Smuzhiyun  * complete by checking the INT bit of config2 register.
443*4882a593Smuzhiyun  */
wait_op_done(struct mxc_nand_host * host,int useirq)444*4882a593Smuzhiyun static int wait_op_done(struct mxc_nand_host *host, int useirq)
445*4882a593Smuzhiyun {
446*4882a593Smuzhiyun 	int ret = 0;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	/*
449*4882a593Smuzhiyun 	 * If operation is already complete, don't bother to setup an irq or a
450*4882a593Smuzhiyun 	 * loop.
451*4882a593Smuzhiyun 	 */
452*4882a593Smuzhiyun 	if (host->devtype_data->check_int(host))
453*4882a593Smuzhiyun 		return 0;
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun 	if (useirq) {
456*4882a593Smuzhiyun 		unsigned long timeout;
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 		reinit_completion(&host->op_completion);
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 		irq_control(host, 1);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 		timeout = wait_for_completion_timeout(&host->op_completion, HZ);
463*4882a593Smuzhiyun 		if (!timeout && !host->devtype_data->check_int(host)) {
464*4882a593Smuzhiyun 			dev_dbg(host->dev, "timeout waiting for irq\n");
465*4882a593Smuzhiyun 			ret = -ETIMEDOUT;
466*4882a593Smuzhiyun 		}
467*4882a593Smuzhiyun 	} else {
468*4882a593Smuzhiyun 		int max_retries = 8000;
469*4882a593Smuzhiyun 		int done;
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 		do {
472*4882a593Smuzhiyun 			udelay(1);
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 			done = host->devtype_data->check_int(host);
475*4882a593Smuzhiyun 			if (done)
476*4882a593Smuzhiyun 				break;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 		} while (--max_retries);
479*4882a593Smuzhiyun 
480*4882a593Smuzhiyun 		if (!done) {
481*4882a593Smuzhiyun 			dev_dbg(host->dev, "timeout polling for completion\n");
482*4882a593Smuzhiyun 			ret = -ETIMEDOUT;
483*4882a593Smuzhiyun 		}
484*4882a593Smuzhiyun 	}
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	WARN_ONCE(ret < 0, "timeout! useirq=%d\n", useirq);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	return ret;
489*4882a593Smuzhiyun }
490*4882a593Smuzhiyun 
send_cmd_v3(struct mxc_nand_host * host,uint16_t cmd,int useirq)491*4882a593Smuzhiyun static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	/* fill command */
494*4882a593Smuzhiyun 	writel(cmd, NFC_V3_FLASH_CMD);
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 	/* send out command */
497*4882a593Smuzhiyun 	writel(NFC_CMD, NFC_V3_LAUNCH);
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	/* Wait for operation to complete */
500*4882a593Smuzhiyun 	wait_op_done(host, useirq);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun /* This function issues the specified command to the NAND device and
504*4882a593Smuzhiyun  * waits for completion. */
send_cmd_v1_v2(struct mxc_nand_host * host,uint16_t cmd,int useirq)505*4882a593Smuzhiyun static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun 	dev_dbg(host->dev, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun 	writew(cmd, NFC_V1_V2_FLASH_CMD);
510*4882a593Smuzhiyun 	writew(NFC_CMD, NFC_V1_V2_CONFIG2);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	if (host->devtype_data->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
513*4882a593Smuzhiyun 		int max_retries = 100;
514*4882a593Smuzhiyun 		/* Reset completion is indicated by NFC_CONFIG2 */
515*4882a593Smuzhiyun 		/* being set to 0 */
516*4882a593Smuzhiyun 		while (max_retries-- > 0) {
517*4882a593Smuzhiyun 			if (readw(NFC_V1_V2_CONFIG2) == 0) {
518*4882a593Smuzhiyun 				break;
519*4882a593Smuzhiyun 			}
520*4882a593Smuzhiyun 			udelay(1);
521*4882a593Smuzhiyun 		}
522*4882a593Smuzhiyun 		if (max_retries < 0)
523*4882a593Smuzhiyun 			dev_dbg(host->dev, "%s: RESET failed\n", __func__);
524*4882a593Smuzhiyun 	} else {
525*4882a593Smuzhiyun 		/* Wait for operation to complete */
526*4882a593Smuzhiyun 		wait_op_done(host, useirq);
527*4882a593Smuzhiyun 	}
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
send_addr_v3(struct mxc_nand_host * host,uint16_t addr,int islast)530*4882a593Smuzhiyun static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun 	/* fill address */
533*4882a593Smuzhiyun 	writel(addr, NFC_V3_FLASH_ADDR0);
534*4882a593Smuzhiyun 
535*4882a593Smuzhiyun 	/* send out address */
536*4882a593Smuzhiyun 	writel(NFC_ADDR, NFC_V3_LAUNCH);
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	wait_op_done(host, 0);
539*4882a593Smuzhiyun }
540*4882a593Smuzhiyun 
541*4882a593Smuzhiyun /* This function sends an address (or partial address) to the
542*4882a593Smuzhiyun  * NAND device. The address is used to select the source/destination for
543*4882a593Smuzhiyun  * a NAND command. */
send_addr_v1_v2(struct mxc_nand_host * host,uint16_t addr,int islast)544*4882a593Smuzhiyun static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
545*4882a593Smuzhiyun {
546*4882a593Smuzhiyun 	dev_dbg(host->dev, "send_addr(host, 0x%x %d)\n", addr, islast);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	writew(addr, NFC_V1_V2_FLASH_ADDR);
549*4882a593Smuzhiyun 	writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun 	/* Wait for operation to complete */
552*4882a593Smuzhiyun 	wait_op_done(host, islast);
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
send_page_v3(struct mtd_info * mtd,unsigned int ops)555*4882a593Smuzhiyun static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
556*4882a593Smuzhiyun {
557*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
558*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
559*4882a593Smuzhiyun 	uint32_t tmp;
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun 	tmp = readl(NFC_V3_CONFIG1);
562*4882a593Smuzhiyun 	tmp &= ~(7 << 4);
563*4882a593Smuzhiyun 	writel(tmp, NFC_V3_CONFIG1);
564*4882a593Smuzhiyun 
565*4882a593Smuzhiyun 	/* transfer data from NFC ram to nand */
566*4882a593Smuzhiyun 	writel(ops, NFC_V3_LAUNCH);
567*4882a593Smuzhiyun 
568*4882a593Smuzhiyun 	wait_op_done(host, false);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
send_page_v2(struct mtd_info * mtd,unsigned int ops)571*4882a593Smuzhiyun static void send_page_v2(struct mtd_info *mtd, unsigned int ops)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
574*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
575*4882a593Smuzhiyun 
576*4882a593Smuzhiyun 	/* NANDFC buffer 0 is used for page read/write */
577*4882a593Smuzhiyun 	writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
578*4882a593Smuzhiyun 
579*4882a593Smuzhiyun 	writew(ops, NFC_V1_V2_CONFIG2);
580*4882a593Smuzhiyun 
581*4882a593Smuzhiyun 	/* Wait for operation to complete */
582*4882a593Smuzhiyun 	wait_op_done(host, true);
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun 
send_page_v1(struct mtd_info * mtd,unsigned int ops)585*4882a593Smuzhiyun static void send_page_v1(struct mtd_info *mtd, unsigned int ops)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
588*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
589*4882a593Smuzhiyun 	int bufs, i;
590*4882a593Smuzhiyun 
591*4882a593Smuzhiyun 	if (mtd->writesize > 512)
592*4882a593Smuzhiyun 		bufs = 4;
593*4882a593Smuzhiyun 	else
594*4882a593Smuzhiyun 		bufs = 1;
595*4882a593Smuzhiyun 
596*4882a593Smuzhiyun 	for (i = 0; i < bufs; i++) {
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun 		/* NANDFC buffer 0 is used for page read/write */
599*4882a593Smuzhiyun 		writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		writew(ops, NFC_V1_V2_CONFIG2);
602*4882a593Smuzhiyun 
603*4882a593Smuzhiyun 		/* Wait for operation to complete */
604*4882a593Smuzhiyun 		wait_op_done(host, true);
605*4882a593Smuzhiyun 	}
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
send_read_id_v3(struct mxc_nand_host * host)608*4882a593Smuzhiyun static void send_read_id_v3(struct mxc_nand_host *host)
609*4882a593Smuzhiyun {
610*4882a593Smuzhiyun 	/* Read ID into main buffer */
611*4882a593Smuzhiyun 	writel(NFC_ID, NFC_V3_LAUNCH);
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 	wait_op_done(host, true);
614*4882a593Smuzhiyun 
615*4882a593Smuzhiyun 	memcpy32_fromio(host->data_buf, host->main_area0, 16);
616*4882a593Smuzhiyun }
617*4882a593Smuzhiyun 
618*4882a593Smuzhiyun /* Request the NANDFC to perform a read of the NAND device ID. */
send_read_id_v1_v2(struct mxc_nand_host * host)619*4882a593Smuzhiyun static void send_read_id_v1_v2(struct mxc_nand_host *host)
620*4882a593Smuzhiyun {
621*4882a593Smuzhiyun 	/* NANDFC buffer 0 is used for device ID output */
622*4882a593Smuzhiyun 	writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	writew(NFC_ID, NFC_V1_V2_CONFIG2);
625*4882a593Smuzhiyun 
626*4882a593Smuzhiyun 	/* Wait for operation to complete */
627*4882a593Smuzhiyun 	wait_op_done(host, true);
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	memcpy32_fromio(host->data_buf, host->main_area0, 16);
630*4882a593Smuzhiyun }
631*4882a593Smuzhiyun 
get_dev_status_v3(struct mxc_nand_host * host)632*4882a593Smuzhiyun static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun 	writew(NFC_STATUS, NFC_V3_LAUNCH);
635*4882a593Smuzhiyun 	wait_op_done(host, true);
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun 	return readl(NFC_V3_CONFIG1) >> 16;
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun 
640*4882a593Smuzhiyun /* This function requests the NANDFC to perform a read of the
641*4882a593Smuzhiyun  * NAND device status and returns the current status. */
get_dev_status_v1_v2(struct mxc_nand_host * host)642*4882a593Smuzhiyun static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun 	void __iomem *main_buf = host->main_area0;
645*4882a593Smuzhiyun 	uint32_t store;
646*4882a593Smuzhiyun 	uint16_t ret;
647*4882a593Smuzhiyun 
648*4882a593Smuzhiyun 	writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 	/*
651*4882a593Smuzhiyun 	 * The device status is stored in main_area0. To
652*4882a593Smuzhiyun 	 * prevent corruption of the buffer save the value
653*4882a593Smuzhiyun 	 * and restore it afterwards.
654*4882a593Smuzhiyun 	 */
655*4882a593Smuzhiyun 	store = readl(main_buf);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
658*4882a593Smuzhiyun 	wait_op_done(host, true);
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	ret = readw(main_buf);
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	writel(store, main_buf);
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun 	return ret;
665*4882a593Smuzhiyun }
666*4882a593Smuzhiyun 
mxc_nand_enable_hwecc_v1_v2(struct nand_chip * chip,bool enable)667*4882a593Smuzhiyun static void mxc_nand_enable_hwecc_v1_v2(struct nand_chip *chip, bool enable)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
670*4882a593Smuzhiyun 	uint16_t config1;
671*4882a593Smuzhiyun 
672*4882a593Smuzhiyun 	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
673*4882a593Smuzhiyun 		return;
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	config1 = readw(NFC_V1_V2_CONFIG1);
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun 	if (enable)
678*4882a593Smuzhiyun 		config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
679*4882a593Smuzhiyun 	else
680*4882a593Smuzhiyun 		config1 &= ~NFC_V1_V2_CONFIG1_ECC_EN;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	writew(config1, NFC_V1_V2_CONFIG1);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun 
mxc_nand_enable_hwecc_v3(struct nand_chip * chip,bool enable)685*4882a593Smuzhiyun static void mxc_nand_enable_hwecc_v3(struct nand_chip *chip, bool enable)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
688*4882a593Smuzhiyun 	uint32_t config2;
689*4882a593Smuzhiyun 
690*4882a593Smuzhiyun 	if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
691*4882a593Smuzhiyun 		return;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	config2 = readl(NFC_V3_CONFIG2);
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	if (enable)
696*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_ECC_EN;
697*4882a593Smuzhiyun 	else
698*4882a593Smuzhiyun 		config2 &= ~NFC_V3_CONFIG2_ECC_EN;
699*4882a593Smuzhiyun 
700*4882a593Smuzhiyun 	writel(config2, NFC_V3_CONFIG2);
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun /* This functions is used by upper layer to checks if device is ready */
mxc_nand_dev_ready(struct nand_chip * chip)704*4882a593Smuzhiyun static int mxc_nand_dev_ready(struct nand_chip *chip)
705*4882a593Smuzhiyun {
706*4882a593Smuzhiyun 	/*
707*4882a593Smuzhiyun 	 * NFC handles R/B internally. Therefore, this function
708*4882a593Smuzhiyun 	 * always returns status as ready.
709*4882a593Smuzhiyun 	 */
710*4882a593Smuzhiyun 	return 1;
711*4882a593Smuzhiyun }
712*4882a593Smuzhiyun 
mxc_nand_read_page_v1(struct nand_chip * chip,void * buf,void * oob,bool ecc,int page)713*4882a593Smuzhiyun static int mxc_nand_read_page_v1(struct nand_chip *chip, void *buf, void *oob,
714*4882a593Smuzhiyun 				 bool ecc, int page)
715*4882a593Smuzhiyun {
716*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
717*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
718*4882a593Smuzhiyun 	unsigned int bitflips_corrected = 0;
719*4882a593Smuzhiyun 	int no_subpages;
720*4882a593Smuzhiyun 	int i;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	host->devtype_data->enable_hwecc(chip, ecc);
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	host->devtype_data->send_cmd(host, NAND_CMD_READ0, false);
725*4882a593Smuzhiyun 	mxc_do_addr_cycle(mtd, 0, page);
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	if (mtd->writesize > 512)
728*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host, NAND_CMD_READSTART, true);
729*4882a593Smuzhiyun 
730*4882a593Smuzhiyun 	no_subpages = mtd->writesize >> 9;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	for (i = 0; i < no_subpages; i++) {
733*4882a593Smuzhiyun 		uint16_t ecc_stats;
734*4882a593Smuzhiyun 
735*4882a593Smuzhiyun 		/* NANDFC buffer 0 is used for page read/write */
736*4882a593Smuzhiyun 		writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		writew(NFC_OUTPUT, NFC_V1_V2_CONFIG2);
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 		/* Wait for operation to complete */
741*4882a593Smuzhiyun 		wait_op_done(host, true);
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 		ecc_stats = get_ecc_status_v1(host);
744*4882a593Smuzhiyun 
745*4882a593Smuzhiyun 		ecc_stats >>= 2;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun 		if (buf && ecc) {
748*4882a593Smuzhiyun 			switch (ecc_stats & 0x3) {
749*4882a593Smuzhiyun 			case 0:
750*4882a593Smuzhiyun 			default:
751*4882a593Smuzhiyun 				break;
752*4882a593Smuzhiyun 			case 1:
753*4882a593Smuzhiyun 				mtd->ecc_stats.corrected++;
754*4882a593Smuzhiyun 				bitflips_corrected = 1;
755*4882a593Smuzhiyun 				break;
756*4882a593Smuzhiyun 			case 2:
757*4882a593Smuzhiyun 				mtd->ecc_stats.failed++;
758*4882a593Smuzhiyun 				break;
759*4882a593Smuzhiyun 			}
760*4882a593Smuzhiyun 		}
761*4882a593Smuzhiyun 	}
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun 	if (buf)
764*4882a593Smuzhiyun 		memcpy32_fromio(buf, host->main_area0, mtd->writesize);
765*4882a593Smuzhiyun 	if (oob)
766*4882a593Smuzhiyun 		copy_spare(mtd, true, oob);
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	return bitflips_corrected;
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
mxc_nand_read_page_v2_v3(struct nand_chip * chip,void * buf,void * oob,bool ecc,int page)771*4882a593Smuzhiyun static int mxc_nand_read_page_v2_v3(struct nand_chip *chip, void *buf,
772*4882a593Smuzhiyun 				    void *oob, bool ecc, int page)
773*4882a593Smuzhiyun {
774*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
775*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
776*4882a593Smuzhiyun 	unsigned int max_bitflips = 0;
777*4882a593Smuzhiyun 	u32 ecc_stat, err;
778*4882a593Smuzhiyun 	int no_subpages;
779*4882a593Smuzhiyun 	u8 ecc_bit_mask, err_limit;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	host->devtype_data->enable_hwecc(chip, ecc);
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	host->devtype_data->send_cmd(host, NAND_CMD_READ0, false);
784*4882a593Smuzhiyun 	mxc_do_addr_cycle(mtd, 0, page);
785*4882a593Smuzhiyun 
786*4882a593Smuzhiyun 	if (mtd->writesize > 512)
787*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host,
788*4882a593Smuzhiyun 				NAND_CMD_READSTART, true);
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	host->devtype_data->send_page(mtd, NFC_OUTPUT);
791*4882a593Smuzhiyun 
792*4882a593Smuzhiyun 	if (buf)
793*4882a593Smuzhiyun 		memcpy32_fromio(buf, host->main_area0, mtd->writesize);
794*4882a593Smuzhiyun 	if (oob)
795*4882a593Smuzhiyun 		copy_spare(mtd, true, oob);
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
798*4882a593Smuzhiyun 	err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
799*4882a593Smuzhiyun 
800*4882a593Smuzhiyun 	no_subpages = mtd->writesize >> 9;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	ecc_stat = host->devtype_data->get_ecc_status(host);
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	do {
805*4882a593Smuzhiyun 		err = ecc_stat & ecc_bit_mask;
806*4882a593Smuzhiyun 		if (err > err_limit) {
807*4882a593Smuzhiyun 			mtd->ecc_stats.failed++;
808*4882a593Smuzhiyun 		} else {
809*4882a593Smuzhiyun 			mtd->ecc_stats.corrected += err;
810*4882a593Smuzhiyun 			max_bitflips = max_t(unsigned int, max_bitflips, err);
811*4882a593Smuzhiyun 		}
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun 		ecc_stat >>= 4;
814*4882a593Smuzhiyun 	} while (--no_subpages);
815*4882a593Smuzhiyun 
816*4882a593Smuzhiyun 	return max_bitflips;
817*4882a593Smuzhiyun }
818*4882a593Smuzhiyun 
mxc_nand_read_page(struct nand_chip * chip,uint8_t * buf,int oob_required,int page)819*4882a593Smuzhiyun static int mxc_nand_read_page(struct nand_chip *chip, uint8_t *buf,
820*4882a593Smuzhiyun 			      int oob_required, int page)
821*4882a593Smuzhiyun {
822*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
823*4882a593Smuzhiyun 	void *oob_buf;
824*4882a593Smuzhiyun 
825*4882a593Smuzhiyun 	if (oob_required)
826*4882a593Smuzhiyun 		oob_buf = chip->oob_poi;
827*4882a593Smuzhiyun 	else
828*4882a593Smuzhiyun 		oob_buf = NULL;
829*4882a593Smuzhiyun 
830*4882a593Smuzhiyun 	return host->devtype_data->read_page(chip, buf, oob_buf, 1, page);
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun 
mxc_nand_read_page_raw(struct nand_chip * chip,uint8_t * buf,int oob_required,int page)833*4882a593Smuzhiyun static int mxc_nand_read_page_raw(struct nand_chip *chip, uint8_t *buf,
834*4882a593Smuzhiyun 				  int oob_required, int page)
835*4882a593Smuzhiyun {
836*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
837*4882a593Smuzhiyun 	void *oob_buf;
838*4882a593Smuzhiyun 
839*4882a593Smuzhiyun 	if (oob_required)
840*4882a593Smuzhiyun 		oob_buf = chip->oob_poi;
841*4882a593Smuzhiyun 	else
842*4882a593Smuzhiyun 		oob_buf = NULL;
843*4882a593Smuzhiyun 
844*4882a593Smuzhiyun 	return host->devtype_data->read_page(chip, buf, oob_buf, 0, page);
845*4882a593Smuzhiyun }
846*4882a593Smuzhiyun 
mxc_nand_read_oob(struct nand_chip * chip,int page)847*4882a593Smuzhiyun static int mxc_nand_read_oob(struct nand_chip *chip, int page)
848*4882a593Smuzhiyun {
849*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
850*4882a593Smuzhiyun 
851*4882a593Smuzhiyun 	return host->devtype_data->read_page(chip, NULL, chip->oob_poi, 0,
852*4882a593Smuzhiyun 					     page);
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun 
mxc_nand_write_page(struct nand_chip * chip,const uint8_t * buf,bool ecc,int page)855*4882a593Smuzhiyun static int mxc_nand_write_page(struct nand_chip *chip, const uint8_t *buf,
856*4882a593Smuzhiyun 			       bool ecc, int page)
857*4882a593Smuzhiyun {
858*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
859*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
860*4882a593Smuzhiyun 
861*4882a593Smuzhiyun 	host->devtype_data->enable_hwecc(chip, ecc);
862*4882a593Smuzhiyun 
863*4882a593Smuzhiyun 	host->devtype_data->send_cmd(host, NAND_CMD_SEQIN, false);
864*4882a593Smuzhiyun 	mxc_do_addr_cycle(mtd, 0, page);
865*4882a593Smuzhiyun 
866*4882a593Smuzhiyun 	memcpy32_toio(host->main_area0, buf, mtd->writesize);
867*4882a593Smuzhiyun 	copy_spare(mtd, false, chip->oob_poi);
868*4882a593Smuzhiyun 
869*4882a593Smuzhiyun 	host->devtype_data->send_page(mtd, NFC_INPUT);
870*4882a593Smuzhiyun 	host->devtype_data->send_cmd(host, NAND_CMD_PAGEPROG, true);
871*4882a593Smuzhiyun 	mxc_do_addr_cycle(mtd, 0, page);
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun 	return 0;
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun 
mxc_nand_write_page_ecc(struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)876*4882a593Smuzhiyun static int mxc_nand_write_page_ecc(struct nand_chip *chip, const uint8_t *buf,
877*4882a593Smuzhiyun 				   int oob_required, int page)
878*4882a593Smuzhiyun {
879*4882a593Smuzhiyun 	return mxc_nand_write_page(chip, buf, true, page);
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun 
mxc_nand_write_page_raw(struct nand_chip * chip,const uint8_t * buf,int oob_required,int page)882*4882a593Smuzhiyun static int mxc_nand_write_page_raw(struct nand_chip *chip, const uint8_t *buf,
883*4882a593Smuzhiyun 				   int oob_required, int page)
884*4882a593Smuzhiyun {
885*4882a593Smuzhiyun 	return mxc_nand_write_page(chip, buf, false, page);
886*4882a593Smuzhiyun }
887*4882a593Smuzhiyun 
mxc_nand_write_oob(struct nand_chip * chip,int page)888*4882a593Smuzhiyun static int mxc_nand_write_oob(struct nand_chip *chip, int page)
889*4882a593Smuzhiyun {
890*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
891*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun 	memset(host->data_buf, 0xff, mtd->writesize);
894*4882a593Smuzhiyun 
895*4882a593Smuzhiyun 	return mxc_nand_write_page(chip, host->data_buf, false, page);
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun 
mxc_nand_read_byte(struct nand_chip * nand_chip)898*4882a593Smuzhiyun static u_char mxc_nand_read_byte(struct nand_chip *nand_chip)
899*4882a593Smuzhiyun {
900*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
901*4882a593Smuzhiyun 	uint8_t ret;
902*4882a593Smuzhiyun 
903*4882a593Smuzhiyun 	/* Check for status request */
904*4882a593Smuzhiyun 	if (host->status_request)
905*4882a593Smuzhiyun 		return host->devtype_data->get_dev_status(host) & 0xFF;
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	if (nand_chip->options & NAND_BUSWIDTH_16) {
908*4882a593Smuzhiyun 		/* only take the lower byte of each word */
909*4882a593Smuzhiyun 		ret = *(uint16_t *)(host->data_buf + host->buf_start);
910*4882a593Smuzhiyun 
911*4882a593Smuzhiyun 		host->buf_start += 2;
912*4882a593Smuzhiyun 	} else {
913*4882a593Smuzhiyun 		ret = *(uint8_t *)(host->data_buf + host->buf_start);
914*4882a593Smuzhiyun 		host->buf_start++;
915*4882a593Smuzhiyun 	}
916*4882a593Smuzhiyun 
917*4882a593Smuzhiyun 	dev_dbg(host->dev, "%s: ret=0x%hhx (start=%u)\n", __func__, ret, host->buf_start);
918*4882a593Smuzhiyun 	return ret;
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun /* Write data of length len to buffer buf. The data to be
922*4882a593Smuzhiyun  * written on NAND Flash is first copied to RAMbuffer. After the Data Input
923*4882a593Smuzhiyun  * Operation by the NFC, the data is written to NAND Flash */
mxc_nand_write_buf(struct nand_chip * nand_chip,const u_char * buf,int len)924*4882a593Smuzhiyun static void mxc_nand_write_buf(struct nand_chip *nand_chip, const u_char *buf,
925*4882a593Smuzhiyun 			       int len)
926*4882a593Smuzhiyun {
927*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(nand_chip);
928*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
929*4882a593Smuzhiyun 	u16 col = host->buf_start;
930*4882a593Smuzhiyun 	int n = mtd->oobsize + mtd->writesize - col;
931*4882a593Smuzhiyun 
932*4882a593Smuzhiyun 	n = min(n, len);
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun 	memcpy(host->data_buf + col, buf, n);
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun 	host->buf_start += n;
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun 
939*4882a593Smuzhiyun /* Read the data buffer from the NAND Flash. To read the data from NAND
940*4882a593Smuzhiyun  * Flash first the data output cycle is initiated by the NFC, which copies
941*4882a593Smuzhiyun  * the data to RAMbuffer. This data of length len is then copied to buffer buf.
942*4882a593Smuzhiyun  */
mxc_nand_read_buf(struct nand_chip * nand_chip,u_char * buf,int len)943*4882a593Smuzhiyun static void mxc_nand_read_buf(struct nand_chip *nand_chip, u_char *buf,
944*4882a593Smuzhiyun 			      int len)
945*4882a593Smuzhiyun {
946*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(nand_chip);
947*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
948*4882a593Smuzhiyun 	u16 col = host->buf_start;
949*4882a593Smuzhiyun 	int n = mtd->oobsize + mtd->writesize - col;
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	n = min(n, len);
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 	memcpy(buf, host->data_buf + col, n);
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	host->buf_start += n;
956*4882a593Smuzhiyun }
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun /* This function is used by upper layer for select and
959*4882a593Smuzhiyun  * deselect of the NAND chip */
mxc_nand_select_chip_v1_v3(struct nand_chip * nand_chip,int chip)960*4882a593Smuzhiyun static void mxc_nand_select_chip_v1_v3(struct nand_chip *nand_chip, int chip)
961*4882a593Smuzhiyun {
962*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
963*4882a593Smuzhiyun 
964*4882a593Smuzhiyun 	if (chip == -1) {
965*4882a593Smuzhiyun 		/* Disable the NFC clock */
966*4882a593Smuzhiyun 		if (host->clk_act) {
967*4882a593Smuzhiyun 			clk_disable_unprepare(host->clk);
968*4882a593Smuzhiyun 			host->clk_act = 0;
969*4882a593Smuzhiyun 		}
970*4882a593Smuzhiyun 		return;
971*4882a593Smuzhiyun 	}
972*4882a593Smuzhiyun 
973*4882a593Smuzhiyun 	if (!host->clk_act) {
974*4882a593Smuzhiyun 		/* Enable the NFC clock */
975*4882a593Smuzhiyun 		clk_prepare_enable(host->clk);
976*4882a593Smuzhiyun 		host->clk_act = 1;
977*4882a593Smuzhiyun 	}
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun 
mxc_nand_select_chip_v2(struct nand_chip * nand_chip,int chip)980*4882a593Smuzhiyun static void mxc_nand_select_chip_v2(struct nand_chip *nand_chip, int chip)
981*4882a593Smuzhiyun {
982*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	if (chip == -1) {
985*4882a593Smuzhiyun 		/* Disable the NFC clock */
986*4882a593Smuzhiyun 		if (host->clk_act) {
987*4882a593Smuzhiyun 			clk_disable_unprepare(host->clk);
988*4882a593Smuzhiyun 			host->clk_act = 0;
989*4882a593Smuzhiyun 		}
990*4882a593Smuzhiyun 		return;
991*4882a593Smuzhiyun 	}
992*4882a593Smuzhiyun 
993*4882a593Smuzhiyun 	if (!host->clk_act) {
994*4882a593Smuzhiyun 		/* Enable the NFC clock */
995*4882a593Smuzhiyun 		clk_prepare_enable(host->clk);
996*4882a593Smuzhiyun 		host->clk_act = 1;
997*4882a593Smuzhiyun 	}
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun 	host->active_cs = chip;
1000*4882a593Smuzhiyun 	writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
1001*4882a593Smuzhiyun }
1002*4882a593Smuzhiyun 
1003*4882a593Smuzhiyun #define MXC_V1_ECCBYTES		5
1004*4882a593Smuzhiyun 
mxc_v1_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)1005*4882a593Smuzhiyun static int mxc_v1_ooblayout_ecc(struct mtd_info *mtd, int section,
1006*4882a593Smuzhiyun 				struct mtd_oob_region *oobregion)
1007*4882a593Smuzhiyun {
1008*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	if (section >= nand_chip->ecc.steps)
1011*4882a593Smuzhiyun 		return -ERANGE;
1012*4882a593Smuzhiyun 
1013*4882a593Smuzhiyun 	oobregion->offset = (section * 16) + 6;
1014*4882a593Smuzhiyun 	oobregion->length = MXC_V1_ECCBYTES;
1015*4882a593Smuzhiyun 
1016*4882a593Smuzhiyun 	return 0;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun 
mxc_v1_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)1019*4882a593Smuzhiyun static int mxc_v1_ooblayout_free(struct mtd_info *mtd, int section,
1020*4882a593Smuzhiyun 				 struct mtd_oob_region *oobregion)
1021*4882a593Smuzhiyun {
1022*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun 	if (section > nand_chip->ecc.steps)
1025*4882a593Smuzhiyun 		return -ERANGE;
1026*4882a593Smuzhiyun 
1027*4882a593Smuzhiyun 	if (!section) {
1028*4882a593Smuzhiyun 		if (mtd->writesize <= 512) {
1029*4882a593Smuzhiyun 			oobregion->offset = 0;
1030*4882a593Smuzhiyun 			oobregion->length = 5;
1031*4882a593Smuzhiyun 		} else {
1032*4882a593Smuzhiyun 			oobregion->offset = 2;
1033*4882a593Smuzhiyun 			oobregion->length = 4;
1034*4882a593Smuzhiyun 		}
1035*4882a593Smuzhiyun 	} else {
1036*4882a593Smuzhiyun 		oobregion->offset = ((section - 1) * 16) + MXC_V1_ECCBYTES + 6;
1037*4882a593Smuzhiyun 		if (section < nand_chip->ecc.steps)
1038*4882a593Smuzhiyun 			oobregion->length = (section * 16) + 6 -
1039*4882a593Smuzhiyun 					    oobregion->offset;
1040*4882a593Smuzhiyun 		else
1041*4882a593Smuzhiyun 			oobregion->length = mtd->oobsize - oobregion->offset;
1042*4882a593Smuzhiyun 	}
1043*4882a593Smuzhiyun 
1044*4882a593Smuzhiyun 	return 0;
1045*4882a593Smuzhiyun }
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun static const struct mtd_ooblayout_ops mxc_v1_ooblayout_ops = {
1048*4882a593Smuzhiyun 	.ecc = mxc_v1_ooblayout_ecc,
1049*4882a593Smuzhiyun 	.free = mxc_v1_ooblayout_free,
1050*4882a593Smuzhiyun };
1051*4882a593Smuzhiyun 
mxc_v2_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)1052*4882a593Smuzhiyun static int mxc_v2_ooblayout_ecc(struct mtd_info *mtd, int section,
1053*4882a593Smuzhiyun 				struct mtd_oob_region *oobregion)
1054*4882a593Smuzhiyun {
1055*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
1056*4882a593Smuzhiyun 	int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
1057*4882a593Smuzhiyun 
1058*4882a593Smuzhiyun 	if (section >= nand_chip->ecc.steps)
1059*4882a593Smuzhiyun 		return -ERANGE;
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 	oobregion->offset = (section * stepsize) + 7;
1062*4882a593Smuzhiyun 	oobregion->length = nand_chip->ecc.bytes;
1063*4882a593Smuzhiyun 
1064*4882a593Smuzhiyun 	return 0;
1065*4882a593Smuzhiyun }
1066*4882a593Smuzhiyun 
mxc_v2_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * oobregion)1067*4882a593Smuzhiyun static int mxc_v2_ooblayout_free(struct mtd_info *mtd, int section,
1068*4882a593Smuzhiyun 				 struct mtd_oob_region *oobregion)
1069*4882a593Smuzhiyun {
1070*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
1071*4882a593Smuzhiyun 	int stepsize = nand_chip->ecc.bytes == 9 ? 16 : 26;
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	if (section >= nand_chip->ecc.steps)
1074*4882a593Smuzhiyun 		return -ERANGE;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	if (!section) {
1077*4882a593Smuzhiyun 		if (mtd->writesize <= 512) {
1078*4882a593Smuzhiyun 			oobregion->offset = 0;
1079*4882a593Smuzhiyun 			oobregion->length = 5;
1080*4882a593Smuzhiyun 		} else {
1081*4882a593Smuzhiyun 			oobregion->offset = 2;
1082*4882a593Smuzhiyun 			oobregion->length = 4;
1083*4882a593Smuzhiyun 		}
1084*4882a593Smuzhiyun 	} else {
1085*4882a593Smuzhiyun 		oobregion->offset = section * stepsize;
1086*4882a593Smuzhiyun 		oobregion->length = 7;
1087*4882a593Smuzhiyun 	}
1088*4882a593Smuzhiyun 
1089*4882a593Smuzhiyun 	return 0;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun static const struct mtd_ooblayout_ops mxc_v2_ooblayout_ops = {
1093*4882a593Smuzhiyun 	.ecc = mxc_v2_ooblayout_ecc,
1094*4882a593Smuzhiyun 	.free = mxc_v2_ooblayout_free,
1095*4882a593Smuzhiyun };
1096*4882a593Smuzhiyun 
1097*4882a593Smuzhiyun /*
1098*4882a593Smuzhiyun  * v2 and v3 type controllers can do 4bit or 8bit ecc depending
1099*4882a593Smuzhiyun  * on how much oob the nand chip has. For 8bit ecc we need at least
1100*4882a593Smuzhiyun  * 26 bytes of oob data per 512 byte block.
1101*4882a593Smuzhiyun  */
get_eccsize(struct mtd_info * mtd)1102*4882a593Smuzhiyun static int get_eccsize(struct mtd_info *mtd)
1103*4882a593Smuzhiyun {
1104*4882a593Smuzhiyun 	int oobbytes_per_512 = 0;
1105*4882a593Smuzhiyun 
1106*4882a593Smuzhiyun 	oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
1107*4882a593Smuzhiyun 
1108*4882a593Smuzhiyun 	if (oobbytes_per_512 < 26)
1109*4882a593Smuzhiyun 		return 4;
1110*4882a593Smuzhiyun 	else
1111*4882a593Smuzhiyun 		return 8;
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun 
preset_v1(struct mtd_info * mtd)1114*4882a593Smuzhiyun static void preset_v1(struct mtd_info *mtd)
1115*4882a593Smuzhiyun {
1116*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
1117*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1118*4882a593Smuzhiyun 	uint16_t config1 = 0;
1119*4882a593Smuzhiyun 
1120*4882a593Smuzhiyun 	if (nand_chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST &&
1121*4882a593Smuzhiyun 	    mtd->writesize)
1122*4882a593Smuzhiyun 		config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1123*4882a593Smuzhiyun 
1124*4882a593Smuzhiyun 	if (!host->devtype_data->irqpending_quirk)
1125*4882a593Smuzhiyun 		config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
1126*4882a593Smuzhiyun 
1127*4882a593Smuzhiyun 	host->eccsize = 1;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	writew(config1, NFC_V1_V2_CONFIG1);
1130*4882a593Smuzhiyun 	/* preset operation */
1131*4882a593Smuzhiyun 
1132*4882a593Smuzhiyun 	/* Unlock the internal RAM Buffer */
1133*4882a593Smuzhiyun 	writew(0x2, NFC_V1_V2_CONFIG);
1134*4882a593Smuzhiyun 
1135*4882a593Smuzhiyun 	/* Blocks to be unlocked */
1136*4882a593Smuzhiyun 	writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
1137*4882a593Smuzhiyun 	writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 	/* Unlock Block Command for given address range */
1140*4882a593Smuzhiyun 	writew(0x4, NFC_V1_V2_WRPROT);
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun 
mxc_nand_v2_setup_interface(struct nand_chip * chip,int csline,const struct nand_interface_config * conf)1143*4882a593Smuzhiyun static int mxc_nand_v2_setup_interface(struct nand_chip *chip, int csline,
1144*4882a593Smuzhiyun 				       const struct nand_interface_config *conf)
1145*4882a593Smuzhiyun {
1146*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
1147*4882a593Smuzhiyun 	int tRC_min_ns, tRC_ps, ret;
1148*4882a593Smuzhiyun 	unsigned long rate, rate_round;
1149*4882a593Smuzhiyun 	const struct nand_sdr_timings *timings;
1150*4882a593Smuzhiyun 	u16 config1;
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun 	timings = nand_get_sdr_timings(conf);
1153*4882a593Smuzhiyun 	if (IS_ERR(timings))
1154*4882a593Smuzhiyun 		return -ENOTSUPP;
1155*4882a593Smuzhiyun 
1156*4882a593Smuzhiyun 	config1 = readw(NFC_V1_V2_CONFIG1);
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	tRC_min_ns = timings->tRC_min / 1000;
1159*4882a593Smuzhiyun 	rate = 1000000000 / tRC_min_ns;
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	/*
1162*4882a593Smuzhiyun 	 * For tRC < 30ns we have to use EDO mode. In this case the controller
1163*4882a593Smuzhiyun 	 * does one access per clock cycle. Otherwise the controller does one
1164*4882a593Smuzhiyun 	 * access in two clock cycles, thus we have to double the rate to the
1165*4882a593Smuzhiyun 	 * controller.
1166*4882a593Smuzhiyun 	 */
1167*4882a593Smuzhiyun 	if (tRC_min_ns < 30) {
1168*4882a593Smuzhiyun 		rate_round = clk_round_rate(host->clk, rate);
1169*4882a593Smuzhiyun 		config1 |= NFC_V2_CONFIG1_ONE_CYCLE;
1170*4882a593Smuzhiyun 		tRC_ps = 1000000000 / (rate_round / 1000);
1171*4882a593Smuzhiyun 	} else {
1172*4882a593Smuzhiyun 		rate *= 2;
1173*4882a593Smuzhiyun 		rate_round = clk_round_rate(host->clk, rate);
1174*4882a593Smuzhiyun 		config1 &= ~NFC_V2_CONFIG1_ONE_CYCLE;
1175*4882a593Smuzhiyun 		tRC_ps = 1000000000 / (rate_round / 1000 / 2);
1176*4882a593Smuzhiyun 	}
1177*4882a593Smuzhiyun 
1178*4882a593Smuzhiyun 	/*
1179*4882a593Smuzhiyun 	 * The timing values compared against are from the i.MX25 Automotive
1180*4882a593Smuzhiyun 	 * datasheet, Table 50. NFC Timing Parameters
1181*4882a593Smuzhiyun 	 */
1182*4882a593Smuzhiyun 	if (timings->tCLS_min > tRC_ps - 1000 ||
1183*4882a593Smuzhiyun 	    timings->tCLH_min > tRC_ps - 2000 ||
1184*4882a593Smuzhiyun 	    timings->tCS_min > tRC_ps - 1000 ||
1185*4882a593Smuzhiyun 	    timings->tCH_min > tRC_ps - 2000 ||
1186*4882a593Smuzhiyun 	    timings->tWP_min > tRC_ps - 1500 ||
1187*4882a593Smuzhiyun 	    timings->tALS_min > tRC_ps ||
1188*4882a593Smuzhiyun 	    timings->tALH_min > tRC_ps - 3000 ||
1189*4882a593Smuzhiyun 	    timings->tDS_min > tRC_ps ||
1190*4882a593Smuzhiyun 	    timings->tDH_min > tRC_ps - 5000 ||
1191*4882a593Smuzhiyun 	    timings->tWC_min > 2 * tRC_ps ||
1192*4882a593Smuzhiyun 	    timings->tWH_min > tRC_ps - 2500 ||
1193*4882a593Smuzhiyun 	    timings->tRR_min > 6 * tRC_ps ||
1194*4882a593Smuzhiyun 	    timings->tRP_min > 3 * tRC_ps / 2 ||
1195*4882a593Smuzhiyun 	    timings->tRC_min > 2 * tRC_ps ||
1196*4882a593Smuzhiyun 	    timings->tREH_min > (tRC_ps / 2) - 2500) {
1197*4882a593Smuzhiyun 		dev_dbg(host->dev, "Timing out of bounds\n");
1198*4882a593Smuzhiyun 		return -EINVAL;
1199*4882a593Smuzhiyun 	}
1200*4882a593Smuzhiyun 
1201*4882a593Smuzhiyun 	if (csline == NAND_DATA_IFACE_CHECK_ONLY)
1202*4882a593Smuzhiyun 		return 0;
1203*4882a593Smuzhiyun 
1204*4882a593Smuzhiyun 	ret = clk_set_rate(host->clk, rate);
1205*4882a593Smuzhiyun 	if (ret)
1206*4882a593Smuzhiyun 		return ret;
1207*4882a593Smuzhiyun 
1208*4882a593Smuzhiyun 	writew(config1, NFC_V1_V2_CONFIG1);
1209*4882a593Smuzhiyun 
1210*4882a593Smuzhiyun 	dev_dbg(host->dev, "Setting rate to %ldHz, %s mode\n", rate_round,
1211*4882a593Smuzhiyun 		config1 & NFC_V2_CONFIG1_ONE_CYCLE ? "One cycle (EDO)" :
1212*4882a593Smuzhiyun 		"normal");
1213*4882a593Smuzhiyun 
1214*4882a593Smuzhiyun 	return 0;
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun 
preset_v2(struct mtd_info * mtd)1217*4882a593Smuzhiyun static void preset_v2(struct mtd_info *mtd)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun 	struct nand_chip *nand_chip = mtd_to_nand(mtd);
1220*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1221*4882a593Smuzhiyun 	uint16_t config1 = 0;
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun 	config1 |= NFC_V2_CONFIG1_FP_INT;
1224*4882a593Smuzhiyun 
1225*4882a593Smuzhiyun 	if (!host->devtype_data->irqpending_quirk)
1226*4882a593Smuzhiyun 		config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
1227*4882a593Smuzhiyun 
1228*4882a593Smuzhiyun 	if (mtd->writesize) {
1229*4882a593Smuzhiyun 		uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
1230*4882a593Smuzhiyun 
1231*4882a593Smuzhiyun 		if (nand_chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
1232*4882a593Smuzhiyun 			config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
1233*4882a593Smuzhiyun 
1234*4882a593Smuzhiyun 		host->eccsize = get_eccsize(mtd);
1235*4882a593Smuzhiyun 		if (host->eccsize == 4)
1236*4882a593Smuzhiyun 			config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
1237*4882a593Smuzhiyun 
1238*4882a593Smuzhiyun 		config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
1239*4882a593Smuzhiyun 	} else {
1240*4882a593Smuzhiyun 		host->eccsize = 1;
1241*4882a593Smuzhiyun 	}
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	writew(config1, NFC_V1_V2_CONFIG1);
1244*4882a593Smuzhiyun 	/* preset operation */
1245*4882a593Smuzhiyun 
1246*4882a593Smuzhiyun 	/* spare area size in 16-bit half-words */
1247*4882a593Smuzhiyun 	writew(mtd->oobsize / 2, NFC_V21_RSLTSPARE_AREA);
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun 	/* Unlock the internal RAM Buffer */
1250*4882a593Smuzhiyun 	writew(0x2, NFC_V1_V2_CONFIG);
1251*4882a593Smuzhiyun 
1252*4882a593Smuzhiyun 	/* Blocks to be unlocked */
1253*4882a593Smuzhiyun 	writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
1254*4882a593Smuzhiyun 	writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
1255*4882a593Smuzhiyun 	writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
1256*4882a593Smuzhiyun 	writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
1257*4882a593Smuzhiyun 	writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
1258*4882a593Smuzhiyun 	writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
1259*4882a593Smuzhiyun 	writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
1260*4882a593Smuzhiyun 	writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
1261*4882a593Smuzhiyun 
1262*4882a593Smuzhiyun 	/* Unlock Block Command for given address range */
1263*4882a593Smuzhiyun 	writew(0x4, NFC_V1_V2_WRPROT);
1264*4882a593Smuzhiyun }
1265*4882a593Smuzhiyun 
preset_v3(struct mtd_info * mtd)1266*4882a593Smuzhiyun static void preset_v3(struct mtd_info *mtd)
1267*4882a593Smuzhiyun {
1268*4882a593Smuzhiyun 	struct nand_chip *chip = mtd_to_nand(mtd);
1269*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
1270*4882a593Smuzhiyun 	uint32_t config2, config3;
1271*4882a593Smuzhiyun 	int i, addr_phases;
1272*4882a593Smuzhiyun 
1273*4882a593Smuzhiyun 	writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
1274*4882a593Smuzhiyun 	writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
1275*4882a593Smuzhiyun 
1276*4882a593Smuzhiyun 	/* Unlock the internal RAM Buffer */
1277*4882a593Smuzhiyun 	writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1278*4882a593Smuzhiyun 			NFC_V3_WRPROT);
1279*4882a593Smuzhiyun 
1280*4882a593Smuzhiyun 	/* Blocks to be unlocked */
1281*4882a593Smuzhiyun 	for (i = 0; i < NAND_MAX_CHIPS; i++)
1282*4882a593Smuzhiyun 		writel(0xffff << 16, NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
1283*4882a593Smuzhiyun 
1284*4882a593Smuzhiyun 	writel(0, NFC_V3_IPC);
1285*4882a593Smuzhiyun 
1286*4882a593Smuzhiyun 	config2 = NFC_V3_CONFIG2_ONE_CYCLE |
1287*4882a593Smuzhiyun 		NFC_V3_CONFIG2_2CMD_PHASES |
1288*4882a593Smuzhiyun 		NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
1289*4882a593Smuzhiyun 		NFC_V3_CONFIG2_ST_CMD(0x70) |
1290*4882a593Smuzhiyun 		NFC_V3_CONFIG2_INT_MSK |
1291*4882a593Smuzhiyun 		NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 	addr_phases = fls(chip->pagemask) >> 3;
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	if (mtd->writesize == 2048) {
1296*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_PS_2048;
1297*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1298*4882a593Smuzhiyun 	} else if (mtd->writesize == 4096) {
1299*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_PS_4096;
1300*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
1301*4882a593Smuzhiyun 	} else {
1302*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_PS_512;
1303*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
1304*4882a593Smuzhiyun 	}
1305*4882a593Smuzhiyun 
1306*4882a593Smuzhiyun 	if (mtd->writesize) {
1307*4882a593Smuzhiyun 		if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
1308*4882a593Smuzhiyun 			config2 |= NFC_V3_CONFIG2_ECC_EN;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 		config2 |= NFC_V3_CONFIG2_PPB(
1311*4882a593Smuzhiyun 				ffs(mtd->erasesize / mtd->writesize) - 6,
1312*4882a593Smuzhiyun 				host->devtype_data->ppb_shift);
1313*4882a593Smuzhiyun 		host->eccsize = get_eccsize(mtd);
1314*4882a593Smuzhiyun 		if (host->eccsize == 8)
1315*4882a593Smuzhiyun 			config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
1316*4882a593Smuzhiyun 	}
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	writel(config2, NFC_V3_CONFIG2);
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
1321*4882a593Smuzhiyun 			NFC_V3_CONFIG3_NO_SDMA |
1322*4882a593Smuzhiyun 			NFC_V3_CONFIG3_RBB_MODE |
1323*4882a593Smuzhiyun 			NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1324*4882a593Smuzhiyun 			NFC_V3_CONFIG3_ADD_OP(0);
1325*4882a593Smuzhiyun 
1326*4882a593Smuzhiyun 	if (!(chip->options & NAND_BUSWIDTH_16))
1327*4882a593Smuzhiyun 		config3 |= NFC_V3_CONFIG3_FW8;
1328*4882a593Smuzhiyun 
1329*4882a593Smuzhiyun 	writel(config3, NFC_V3_CONFIG3);
1330*4882a593Smuzhiyun 
1331*4882a593Smuzhiyun 	writel(0, NFC_V3_DELAY_LINE);
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun /* Used by the upper layer to write command to NAND Flash for
1335*4882a593Smuzhiyun  * different operations to be carried out on NAND Flash */
mxc_nand_command(struct nand_chip * nand_chip,unsigned command,int column,int page_addr)1336*4882a593Smuzhiyun static void mxc_nand_command(struct nand_chip *nand_chip, unsigned command,
1337*4882a593Smuzhiyun 			     int column, int page_addr)
1338*4882a593Smuzhiyun {
1339*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(nand_chip);
1340*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
1341*4882a593Smuzhiyun 
1342*4882a593Smuzhiyun 	dev_dbg(host->dev, "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
1343*4882a593Smuzhiyun 	      command, column, page_addr);
1344*4882a593Smuzhiyun 
1345*4882a593Smuzhiyun 	/* Reset command state information */
1346*4882a593Smuzhiyun 	host->status_request = false;
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun 	/* Command pre-processing step */
1349*4882a593Smuzhiyun 	switch (command) {
1350*4882a593Smuzhiyun 	case NAND_CMD_RESET:
1351*4882a593Smuzhiyun 		host->devtype_data->preset(mtd);
1352*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host, command, false);
1353*4882a593Smuzhiyun 		break;
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	case NAND_CMD_STATUS:
1356*4882a593Smuzhiyun 		host->buf_start = 0;
1357*4882a593Smuzhiyun 		host->status_request = true;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host, command, true);
1360*4882a593Smuzhiyun 		WARN_ONCE(column != -1 || page_addr != -1,
1361*4882a593Smuzhiyun 			  "Unexpected column/row value (cmd=%u, col=%d, row=%d)\n",
1362*4882a593Smuzhiyun 			  command, column, page_addr);
1363*4882a593Smuzhiyun 		mxc_do_addr_cycle(mtd, column, page_addr);
1364*4882a593Smuzhiyun 		break;
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun 	case NAND_CMD_READID:
1367*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host, command, true);
1368*4882a593Smuzhiyun 		mxc_do_addr_cycle(mtd, column, page_addr);
1369*4882a593Smuzhiyun 		host->devtype_data->send_read_id(host);
1370*4882a593Smuzhiyun 		host->buf_start = 0;
1371*4882a593Smuzhiyun 		break;
1372*4882a593Smuzhiyun 
1373*4882a593Smuzhiyun 	case NAND_CMD_ERASE1:
1374*4882a593Smuzhiyun 	case NAND_CMD_ERASE2:
1375*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host, command, false);
1376*4882a593Smuzhiyun 		WARN_ONCE(column != -1,
1377*4882a593Smuzhiyun 			  "Unexpected column value (cmd=%u, col=%d)\n",
1378*4882a593Smuzhiyun 			  command, column);
1379*4882a593Smuzhiyun 		mxc_do_addr_cycle(mtd, column, page_addr);
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 		break;
1382*4882a593Smuzhiyun 	case NAND_CMD_PARAM:
1383*4882a593Smuzhiyun 		host->devtype_data->send_cmd(host, command, false);
1384*4882a593Smuzhiyun 		mxc_do_addr_cycle(mtd, column, page_addr);
1385*4882a593Smuzhiyun 		host->devtype_data->send_page(mtd, NFC_OUTPUT);
1386*4882a593Smuzhiyun 		memcpy32_fromio(host->data_buf, host->main_area0, 512);
1387*4882a593Smuzhiyun 		host->buf_start = 0;
1388*4882a593Smuzhiyun 		break;
1389*4882a593Smuzhiyun 	default:
1390*4882a593Smuzhiyun 		WARN_ONCE(1, "Unimplemented command (cmd=%u)\n",
1391*4882a593Smuzhiyun 			  command);
1392*4882a593Smuzhiyun 		break;
1393*4882a593Smuzhiyun 	}
1394*4882a593Smuzhiyun }
1395*4882a593Smuzhiyun 
mxc_nand_set_features(struct nand_chip * chip,int addr,u8 * subfeature_param)1396*4882a593Smuzhiyun static int mxc_nand_set_features(struct nand_chip *chip, int addr,
1397*4882a593Smuzhiyun 				 u8 *subfeature_param)
1398*4882a593Smuzhiyun {
1399*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
1400*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
1401*4882a593Smuzhiyun 	int i;
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun 	host->buf_start = 0;
1404*4882a593Smuzhiyun 
1405*4882a593Smuzhiyun 	for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1406*4882a593Smuzhiyun 		chip->legacy.write_byte(chip, subfeature_param[i]);
1407*4882a593Smuzhiyun 
1408*4882a593Smuzhiyun 	memcpy32_toio(host->main_area0, host->data_buf, mtd->writesize);
1409*4882a593Smuzhiyun 	host->devtype_data->send_cmd(host, NAND_CMD_SET_FEATURES, false);
1410*4882a593Smuzhiyun 	mxc_do_addr_cycle(mtd, addr, -1);
1411*4882a593Smuzhiyun 	host->devtype_data->send_page(mtd, NFC_INPUT);
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun 	return 0;
1414*4882a593Smuzhiyun }
1415*4882a593Smuzhiyun 
mxc_nand_get_features(struct nand_chip * chip,int addr,u8 * subfeature_param)1416*4882a593Smuzhiyun static int mxc_nand_get_features(struct nand_chip *chip, int addr,
1417*4882a593Smuzhiyun 				 u8 *subfeature_param)
1418*4882a593Smuzhiyun {
1419*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
1420*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
1421*4882a593Smuzhiyun 	int i;
1422*4882a593Smuzhiyun 
1423*4882a593Smuzhiyun 	host->devtype_data->send_cmd(host, NAND_CMD_GET_FEATURES, false);
1424*4882a593Smuzhiyun 	mxc_do_addr_cycle(mtd, addr, -1);
1425*4882a593Smuzhiyun 	host->devtype_data->send_page(mtd, NFC_OUTPUT);
1426*4882a593Smuzhiyun 	memcpy32_fromio(host->data_buf, host->main_area0, 512);
1427*4882a593Smuzhiyun 	host->buf_start = 0;
1428*4882a593Smuzhiyun 
1429*4882a593Smuzhiyun 	for (i = 0; i < ONFI_SUBFEATURE_PARAM_LEN; ++i)
1430*4882a593Smuzhiyun 		*subfeature_param++ = chip->legacy.read_byte(chip);
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun 	return 0;
1433*4882a593Smuzhiyun }
1434*4882a593Smuzhiyun 
1435*4882a593Smuzhiyun /*
1436*4882a593Smuzhiyun  * The generic flash bbt descriptors overlap with our ecc
1437*4882a593Smuzhiyun  * hardware, so define some i.MX specific ones.
1438*4882a593Smuzhiyun  */
1439*4882a593Smuzhiyun static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
1440*4882a593Smuzhiyun static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun static struct nand_bbt_descr bbt_main_descr = {
1443*4882a593Smuzhiyun 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1444*4882a593Smuzhiyun 	    | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1445*4882a593Smuzhiyun 	.offs = 0,
1446*4882a593Smuzhiyun 	.len = 4,
1447*4882a593Smuzhiyun 	.veroffs = 4,
1448*4882a593Smuzhiyun 	.maxblocks = 4,
1449*4882a593Smuzhiyun 	.pattern = bbt_pattern,
1450*4882a593Smuzhiyun };
1451*4882a593Smuzhiyun 
1452*4882a593Smuzhiyun static struct nand_bbt_descr bbt_mirror_descr = {
1453*4882a593Smuzhiyun 	.options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1454*4882a593Smuzhiyun 	    | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1455*4882a593Smuzhiyun 	.offs = 0,
1456*4882a593Smuzhiyun 	.len = 4,
1457*4882a593Smuzhiyun 	.veroffs = 4,
1458*4882a593Smuzhiyun 	.maxblocks = 4,
1459*4882a593Smuzhiyun 	.pattern = mirror_pattern,
1460*4882a593Smuzhiyun };
1461*4882a593Smuzhiyun 
1462*4882a593Smuzhiyun /* v1 + irqpending_quirk: i.MX21 */
1463*4882a593Smuzhiyun static const struct mxc_nand_devtype_data imx21_nand_devtype_data = {
1464*4882a593Smuzhiyun 	.preset = preset_v1,
1465*4882a593Smuzhiyun 	.read_page = mxc_nand_read_page_v1,
1466*4882a593Smuzhiyun 	.send_cmd = send_cmd_v1_v2,
1467*4882a593Smuzhiyun 	.send_addr = send_addr_v1_v2,
1468*4882a593Smuzhiyun 	.send_page = send_page_v1,
1469*4882a593Smuzhiyun 	.send_read_id = send_read_id_v1_v2,
1470*4882a593Smuzhiyun 	.get_dev_status = get_dev_status_v1_v2,
1471*4882a593Smuzhiyun 	.check_int = check_int_v1_v2,
1472*4882a593Smuzhiyun 	.irq_control = irq_control_v1_v2,
1473*4882a593Smuzhiyun 	.get_ecc_status = get_ecc_status_v1,
1474*4882a593Smuzhiyun 	.ooblayout = &mxc_v1_ooblayout_ops,
1475*4882a593Smuzhiyun 	.select_chip = mxc_nand_select_chip_v1_v3,
1476*4882a593Smuzhiyun 	.enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
1477*4882a593Smuzhiyun 	.irqpending_quirk = 1,
1478*4882a593Smuzhiyun 	.needs_ip = 0,
1479*4882a593Smuzhiyun 	.regs_offset = 0xe00,
1480*4882a593Smuzhiyun 	.spare0_offset = 0x800,
1481*4882a593Smuzhiyun 	.spare_len = 16,
1482*4882a593Smuzhiyun 	.eccbytes = 3,
1483*4882a593Smuzhiyun 	.eccsize = 1,
1484*4882a593Smuzhiyun };
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun /* v1 + !irqpending_quirk: i.MX27, i.MX31 */
1487*4882a593Smuzhiyun static const struct mxc_nand_devtype_data imx27_nand_devtype_data = {
1488*4882a593Smuzhiyun 	.preset = preset_v1,
1489*4882a593Smuzhiyun 	.read_page = mxc_nand_read_page_v1,
1490*4882a593Smuzhiyun 	.send_cmd = send_cmd_v1_v2,
1491*4882a593Smuzhiyun 	.send_addr = send_addr_v1_v2,
1492*4882a593Smuzhiyun 	.send_page = send_page_v1,
1493*4882a593Smuzhiyun 	.send_read_id = send_read_id_v1_v2,
1494*4882a593Smuzhiyun 	.get_dev_status = get_dev_status_v1_v2,
1495*4882a593Smuzhiyun 	.check_int = check_int_v1_v2,
1496*4882a593Smuzhiyun 	.irq_control = irq_control_v1_v2,
1497*4882a593Smuzhiyun 	.get_ecc_status = get_ecc_status_v1,
1498*4882a593Smuzhiyun 	.ooblayout = &mxc_v1_ooblayout_ops,
1499*4882a593Smuzhiyun 	.select_chip = mxc_nand_select_chip_v1_v3,
1500*4882a593Smuzhiyun 	.enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
1501*4882a593Smuzhiyun 	.irqpending_quirk = 0,
1502*4882a593Smuzhiyun 	.needs_ip = 0,
1503*4882a593Smuzhiyun 	.regs_offset = 0xe00,
1504*4882a593Smuzhiyun 	.spare0_offset = 0x800,
1505*4882a593Smuzhiyun 	.axi_offset = 0,
1506*4882a593Smuzhiyun 	.spare_len = 16,
1507*4882a593Smuzhiyun 	.eccbytes = 3,
1508*4882a593Smuzhiyun 	.eccsize = 1,
1509*4882a593Smuzhiyun };
1510*4882a593Smuzhiyun 
1511*4882a593Smuzhiyun /* v21: i.MX25, i.MX35 */
1512*4882a593Smuzhiyun static const struct mxc_nand_devtype_data imx25_nand_devtype_data = {
1513*4882a593Smuzhiyun 	.preset = preset_v2,
1514*4882a593Smuzhiyun 	.read_page = mxc_nand_read_page_v2_v3,
1515*4882a593Smuzhiyun 	.send_cmd = send_cmd_v1_v2,
1516*4882a593Smuzhiyun 	.send_addr = send_addr_v1_v2,
1517*4882a593Smuzhiyun 	.send_page = send_page_v2,
1518*4882a593Smuzhiyun 	.send_read_id = send_read_id_v1_v2,
1519*4882a593Smuzhiyun 	.get_dev_status = get_dev_status_v1_v2,
1520*4882a593Smuzhiyun 	.check_int = check_int_v1_v2,
1521*4882a593Smuzhiyun 	.irq_control = irq_control_v1_v2,
1522*4882a593Smuzhiyun 	.get_ecc_status = get_ecc_status_v2,
1523*4882a593Smuzhiyun 	.ooblayout = &mxc_v2_ooblayout_ops,
1524*4882a593Smuzhiyun 	.select_chip = mxc_nand_select_chip_v2,
1525*4882a593Smuzhiyun 	.setup_interface = mxc_nand_v2_setup_interface,
1526*4882a593Smuzhiyun 	.enable_hwecc = mxc_nand_enable_hwecc_v1_v2,
1527*4882a593Smuzhiyun 	.irqpending_quirk = 0,
1528*4882a593Smuzhiyun 	.needs_ip = 0,
1529*4882a593Smuzhiyun 	.regs_offset = 0x1e00,
1530*4882a593Smuzhiyun 	.spare0_offset = 0x1000,
1531*4882a593Smuzhiyun 	.axi_offset = 0,
1532*4882a593Smuzhiyun 	.spare_len = 64,
1533*4882a593Smuzhiyun 	.eccbytes = 9,
1534*4882a593Smuzhiyun 	.eccsize = 0,
1535*4882a593Smuzhiyun };
1536*4882a593Smuzhiyun 
1537*4882a593Smuzhiyun /* v3.2a: i.MX51 */
1538*4882a593Smuzhiyun static const struct mxc_nand_devtype_data imx51_nand_devtype_data = {
1539*4882a593Smuzhiyun 	.preset = preset_v3,
1540*4882a593Smuzhiyun 	.read_page = mxc_nand_read_page_v2_v3,
1541*4882a593Smuzhiyun 	.send_cmd = send_cmd_v3,
1542*4882a593Smuzhiyun 	.send_addr = send_addr_v3,
1543*4882a593Smuzhiyun 	.send_page = send_page_v3,
1544*4882a593Smuzhiyun 	.send_read_id = send_read_id_v3,
1545*4882a593Smuzhiyun 	.get_dev_status = get_dev_status_v3,
1546*4882a593Smuzhiyun 	.check_int = check_int_v3,
1547*4882a593Smuzhiyun 	.irq_control = irq_control_v3,
1548*4882a593Smuzhiyun 	.get_ecc_status = get_ecc_status_v3,
1549*4882a593Smuzhiyun 	.ooblayout = &mxc_v2_ooblayout_ops,
1550*4882a593Smuzhiyun 	.select_chip = mxc_nand_select_chip_v1_v3,
1551*4882a593Smuzhiyun 	.enable_hwecc = mxc_nand_enable_hwecc_v3,
1552*4882a593Smuzhiyun 	.irqpending_quirk = 0,
1553*4882a593Smuzhiyun 	.needs_ip = 1,
1554*4882a593Smuzhiyun 	.regs_offset = 0,
1555*4882a593Smuzhiyun 	.spare0_offset = 0x1000,
1556*4882a593Smuzhiyun 	.axi_offset = 0x1e00,
1557*4882a593Smuzhiyun 	.spare_len = 64,
1558*4882a593Smuzhiyun 	.eccbytes = 0,
1559*4882a593Smuzhiyun 	.eccsize = 0,
1560*4882a593Smuzhiyun 	.ppb_shift = 7,
1561*4882a593Smuzhiyun };
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun /* v3.2b: i.MX53 */
1564*4882a593Smuzhiyun static const struct mxc_nand_devtype_data imx53_nand_devtype_data = {
1565*4882a593Smuzhiyun 	.preset = preset_v3,
1566*4882a593Smuzhiyun 	.read_page = mxc_nand_read_page_v2_v3,
1567*4882a593Smuzhiyun 	.send_cmd = send_cmd_v3,
1568*4882a593Smuzhiyun 	.send_addr = send_addr_v3,
1569*4882a593Smuzhiyun 	.send_page = send_page_v3,
1570*4882a593Smuzhiyun 	.send_read_id = send_read_id_v3,
1571*4882a593Smuzhiyun 	.get_dev_status = get_dev_status_v3,
1572*4882a593Smuzhiyun 	.check_int = check_int_v3,
1573*4882a593Smuzhiyun 	.irq_control = irq_control_v3,
1574*4882a593Smuzhiyun 	.get_ecc_status = get_ecc_status_v3,
1575*4882a593Smuzhiyun 	.ooblayout = &mxc_v2_ooblayout_ops,
1576*4882a593Smuzhiyun 	.select_chip = mxc_nand_select_chip_v1_v3,
1577*4882a593Smuzhiyun 	.enable_hwecc = mxc_nand_enable_hwecc_v3,
1578*4882a593Smuzhiyun 	.irqpending_quirk = 0,
1579*4882a593Smuzhiyun 	.needs_ip = 1,
1580*4882a593Smuzhiyun 	.regs_offset = 0,
1581*4882a593Smuzhiyun 	.spare0_offset = 0x1000,
1582*4882a593Smuzhiyun 	.axi_offset = 0x1e00,
1583*4882a593Smuzhiyun 	.spare_len = 64,
1584*4882a593Smuzhiyun 	.eccbytes = 0,
1585*4882a593Smuzhiyun 	.eccsize = 0,
1586*4882a593Smuzhiyun 	.ppb_shift = 8,
1587*4882a593Smuzhiyun };
1588*4882a593Smuzhiyun 
is_imx21_nfc(struct mxc_nand_host * host)1589*4882a593Smuzhiyun static inline int is_imx21_nfc(struct mxc_nand_host *host)
1590*4882a593Smuzhiyun {
1591*4882a593Smuzhiyun 	return host->devtype_data == &imx21_nand_devtype_data;
1592*4882a593Smuzhiyun }
1593*4882a593Smuzhiyun 
is_imx27_nfc(struct mxc_nand_host * host)1594*4882a593Smuzhiyun static inline int is_imx27_nfc(struct mxc_nand_host *host)
1595*4882a593Smuzhiyun {
1596*4882a593Smuzhiyun 	return host->devtype_data == &imx27_nand_devtype_data;
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun 
is_imx25_nfc(struct mxc_nand_host * host)1599*4882a593Smuzhiyun static inline int is_imx25_nfc(struct mxc_nand_host *host)
1600*4882a593Smuzhiyun {
1601*4882a593Smuzhiyun 	return host->devtype_data == &imx25_nand_devtype_data;
1602*4882a593Smuzhiyun }
1603*4882a593Smuzhiyun 
is_imx51_nfc(struct mxc_nand_host * host)1604*4882a593Smuzhiyun static inline int is_imx51_nfc(struct mxc_nand_host *host)
1605*4882a593Smuzhiyun {
1606*4882a593Smuzhiyun 	return host->devtype_data == &imx51_nand_devtype_data;
1607*4882a593Smuzhiyun }
1608*4882a593Smuzhiyun 
is_imx53_nfc(struct mxc_nand_host * host)1609*4882a593Smuzhiyun static inline int is_imx53_nfc(struct mxc_nand_host *host)
1610*4882a593Smuzhiyun {
1611*4882a593Smuzhiyun 	return host->devtype_data == &imx53_nand_devtype_data;
1612*4882a593Smuzhiyun }
1613*4882a593Smuzhiyun 
1614*4882a593Smuzhiyun static const struct platform_device_id mxcnd_devtype[] = {
1615*4882a593Smuzhiyun 	{
1616*4882a593Smuzhiyun 		.name = "imx21-nand",
1617*4882a593Smuzhiyun 		.driver_data = (kernel_ulong_t) &imx21_nand_devtype_data,
1618*4882a593Smuzhiyun 	}, {
1619*4882a593Smuzhiyun 		.name = "imx27-nand",
1620*4882a593Smuzhiyun 		.driver_data = (kernel_ulong_t) &imx27_nand_devtype_data,
1621*4882a593Smuzhiyun 	}, {
1622*4882a593Smuzhiyun 		.name = "imx25-nand",
1623*4882a593Smuzhiyun 		.driver_data = (kernel_ulong_t) &imx25_nand_devtype_data,
1624*4882a593Smuzhiyun 	}, {
1625*4882a593Smuzhiyun 		.name = "imx51-nand",
1626*4882a593Smuzhiyun 		.driver_data = (kernel_ulong_t) &imx51_nand_devtype_data,
1627*4882a593Smuzhiyun 	}, {
1628*4882a593Smuzhiyun 		.name = "imx53-nand",
1629*4882a593Smuzhiyun 		.driver_data = (kernel_ulong_t) &imx53_nand_devtype_data,
1630*4882a593Smuzhiyun 	}, {
1631*4882a593Smuzhiyun 		/* sentinel */
1632*4882a593Smuzhiyun 	}
1633*4882a593Smuzhiyun };
1634*4882a593Smuzhiyun MODULE_DEVICE_TABLE(platform, mxcnd_devtype);
1635*4882a593Smuzhiyun 
1636*4882a593Smuzhiyun #ifdef CONFIG_OF
1637*4882a593Smuzhiyun static const struct of_device_id mxcnd_dt_ids[] = {
1638*4882a593Smuzhiyun 	{
1639*4882a593Smuzhiyun 		.compatible = "fsl,imx21-nand",
1640*4882a593Smuzhiyun 		.data = &imx21_nand_devtype_data,
1641*4882a593Smuzhiyun 	}, {
1642*4882a593Smuzhiyun 		.compatible = "fsl,imx27-nand",
1643*4882a593Smuzhiyun 		.data = &imx27_nand_devtype_data,
1644*4882a593Smuzhiyun 	}, {
1645*4882a593Smuzhiyun 		.compatible = "fsl,imx25-nand",
1646*4882a593Smuzhiyun 		.data = &imx25_nand_devtype_data,
1647*4882a593Smuzhiyun 	}, {
1648*4882a593Smuzhiyun 		.compatible = "fsl,imx51-nand",
1649*4882a593Smuzhiyun 		.data = &imx51_nand_devtype_data,
1650*4882a593Smuzhiyun 	}, {
1651*4882a593Smuzhiyun 		.compatible = "fsl,imx53-nand",
1652*4882a593Smuzhiyun 		.data = &imx53_nand_devtype_data,
1653*4882a593Smuzhiyun 	},
1654*4882a593Smuzhiyun 	{ /* sentinel */ }
1655*4882a593Smuzhiyun };
1656*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, mxcnd_dt_ids);
1657*4882a593Smuzhiyun 
mxcnd_probe_dt(struct mxc_nand_host * host)1658*4882a593Smuzhiyun static int mxcnd_probe_dt(struct mxc_nand_host *host)
1659*4882a593Smuzhiyun {
1660*4882a593Smuzhiyun 	struct device_node *np = host->dev->of_node;
1661*4882a593Smuzhiyun 	const struct of_device_id *of_id =
1662*4882a593Smuzhiyun 		of_match_device(mxcnd_dt_ids, host->dev);
1663*4882a593Smuzhiyun 
1664*4882a593Smuzhiyun 	if (!np)
1665*4882a593Smuzhiyun 		return 1;
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun 	host->devtype_data = of_id->data;
1668*4882a593Smuzhiyun 
1669*4882a593Smuzhiyun 	return 0;
1670*4882a593Smuzhiyun }
1671*4882a593Smuzhiyun #else
mxcnd_probe_dt(struct mxc_nand_host * host)1672*4882a593Smuzhiyun static int mxcnd_probe_dt(struct mxc_nand_host *host)
1673*4882a593Smuzhiyun {
1674*4882a593Smuzhiyun 	return 1;
1675*4882a593Smuzhiyun }
1676*4882a593Smuzhiyun #endif
1677*4882a593Smuzhiyun 
mxcnd_attach_chip(struct nand_chip * chip)1678*4882a593Smuzhiyun static int mxcnd_attach_chip(struct nand_chip *chip)
1679*4882a593Smuzhiyun {
1680*4882a593Smuzhiyun 	struct mtd_info *mtd = nand_to_mtd(chip);
1681*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
1682*4882a593Smuzhiyun 	struct device *dev = mtd->dev.parent;
1683*4882a593Smuzhiyun 
1684*4882a593Smuzhiyun 	chip->ecc.bytes = host->devtype_data->eccbytes;
1685*4882a593Smuzhiyun 	host->eccsize = host->devtype_data->eccsize;
1686*4882a593Smuzhiyun 	chip->ecc.size = 512;
1687*4882a593Smuzhiyun 	mtd_set_ooblayout(mtd, host->devtype_data->ooblayout);
1688*4882a593Smuzhiyun 
1689*4882a593Smuzhiyun 	switch (chip->ecc.engine_type) {
1690*4882a593Smuzhiyun 	case NAND_ECC_ENGINE_TYPE_ON_HOST:
1691*4882a593Smuzhiyun 		chip->ecc.read_page = mxc_nand_read_page;
1692*4882a593Smuzhiyun 		chip->ecc.read_page_raw = mxc_nand_read_page_raw;
1693*4882a593Smuzhiyun 		chip->ecc.read_oob = mxc_nand_read_oob;
1694*4882a593Smuzhiyun 		chip->ecc.write_page = mxc_nand_write_page_ecc;
1695*4882a593Smuzhiyun 		chip->ecc.write_page_raw = mxc_nand_write_page_raw;
1696*4882a593Smuzhiyun 		chip->ecc.write_oob = mxc_nand_write_oob;
1697*4882a593Smuzhiyun 		break;
1698*4882a593Smuzhiyun 
1699*4882a593Smuzhiyun 	case NAND_ECC_ENGINE_TYPE_SOFT:
1700*4882a593Smuzhiyun 		break;
1701*4882a593Smuzhiyun 
1702*4882a593Smuzhiyun 	default:
1703*4882a593Smuzhiyun 		return -EINVAL;
1704*4882a593Smuzhiyun 	}
1705*4882a593Smuzhiyun 
1706*4882a593Smuzhiyun 	if (chip->bbt_options & NAND_BBT_USE_FLASH) {
1707*4882a593Smuzhiyun 		chip->bbt_td = &bbt_main_descr;
1708*4882a593Smuzhiyun 		chip->bbt_md = &bbt_mirror_descr;
1709*4882a593Smuzhiyun 	}
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 	/* Allocate the right size buffer now */
1712*4882a593Smuzhiyun 	devm_kfree(dev, (void *)host->data_buf);
1713*4882a593Smuzhiyun 	host->data_buf = devm_kzalloc(dev, mtd->writesize + mtd->oobsize,
1714*4882a593Smuzhiyun 				      GFP_KERNEL);
1715*4882a593Smuzhiyun 	if (!host->data_buf)
1716*4882a593Smuzhiyun 		return -ENOMEM;
1717*4882a593Smuzhiyun 
1718*4882a593Smuzhiyun 	/* Call preset again, with correct writesize chip time */
1719*4882a593Smuzhiyun 	host->devtype_data->preset(mtd);
1720*4882a593Smuzhiyun 
1721*4882a593Smuzhiyun 	if (!chip->ecc.bytes) {
1722*4882a593Smuzhiyun 		if (host->eccsize == 8)
1723*4882a593Smuzhiyun 			chip->ecc.bytes = 18;
1724*4882a593Smuzhiyun 		else if (host->eccsize == 4)
1725*4882a593Smuzhiyun 			chip->ecc.bytes = 9;
1726*4882a593Smuzhiyun 	}
1727*4882a593Smuzhiyun 
1728*4882a593Smuzhiyun 	/*
1729*4882a593Smuzhiyun 	 * Experimentation shows that i.MX NFC can only handle up to 218 oob
1730*4882a593Smuzhiyun 	 * bytes. Limit used_oobsize to 218 so as to not confuse copy_spare()
1731*4882a593Smuzhiyun 	 * into copying invalid data to/from the spare IO buffer, as this
1732*4882a593Smuzhiyun 	 * might cause ECC data corruption when doing sub-page write to a
1733*4882a593Smuzhiyun 	 * partially written page.
1734*4882a593Smuzhiyun 	 */
1735*4882a593Smuzhiyun 	host->used_oobsize = min(mtd->oobsize, 218U);
1736*4882a593Smuzhiyun 
1737*4882a593Smuzhiyun 	if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST) {
1738*4882a593Smuzhiyun 		if (is_imx21_nfc(host) || is_imx27_nfc(host))
1739*4882a593Smuzhiyun 			chip->ecc.strength = 1;
1740*4882a593Smuzhiyun 		else
1741*4882a593Smuzhiyun 			chip->ecc.strength = (host->eccsize == 4) ? 4 : 8;
1742*4882a593Smuzhiyun 	}
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun 	return 0;
1745*4882a593Smuzhiyun }
1746*4882a593Smuzhiyun 
mxcnd_setup_interface(struct nand_chip * chip,int chipnr,const struct nand_interface_config * conf)1747*4882a593Smuzhiyun static int mxcnd_setup_interface(struct nand_chip *chip, int chipnr,
1748*4882a593Smuzhiyun 				 const struct nand_interface_config *conf)
1749*4882a593Smuzhiyun {
1750*4882a593Smuzhiyun 	struct mxc_nand_host *host = nand_get_controller_data(chip);
1751*4882a593Smuzhiyun 
1752*4882a593Smuzhiyun 	return host->devtype_data->setup_interface(chip, chipnr, conf);
1753*4882a593Smuzhiyun }
1754*4882a593Smuzhiyun 
1755*4882a593Smuzhiyun static const struct nand_controller_ops mxcnd_controller_ops = {
1756*4882a593Smuzhiyun 	.attach_chip = mxcnd_attach_chip,
1757*4882a593Smuzhiyun 	.setup_interface = mxcnd_setup_interface,
1758*4882a593Smuzhiyun };
1759*4882a593Smuzhiyun 
mxcnd_probe(struct platform_device * pdev)1760*4882a593Smuzhiyun static int mxcnd_probe(struct platform_device *pdev)
1761*4882a593Smuzhiyun {
1762*4882a593Smuzhiyun 	struct nand_chip *this;
1763*4882a593Smuzhiyun 	struct mtd_info *mtd;
1764*4882a593Smuzhiyun 	struct mxc_nand_host *host;
1765*4882a593Smuzhiyun 	struct resource *res;
1766*4882a593Smuzhiyun 	int err = 0;
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 	/* Allocate memory for MTD device structure and private data */
1769*4882a593Smuzhiyun 	host = devm_kzalloc(&pdev->dev, sizeof(struct mxc_nand_host),
1770*4882a593Smuzhiyun 			GFP_KERNEL);
1771*4882a593Smuzhiyun 	if (!host)
1772*4882a593Smuzhiyun 		return -ENOMEM;
1773*4882a593Smuzhiyun 
1774*4882a593Smuzhiyun 	/* allocate a temporary buffer for the nand_scan_ident() */
1775*4882a593Smuzhiyun 	host->data_buf = devm_kzalloc(&pdev->dev, PAGE_SIZE, GFP_KERNEL);
1776*4882a593Smuzhiyun 	if (!host->data_buf)
1777*4882a593Smuzhiyun 		return -ENOMEM;
1778*4882a593Smuzhiyun 
1779*4882a593Smuzhiyun 	host->dev = &pdev->dev;
1780*4882a593Smuzhiyun 	/* structures must be linked */
1781*4882a593Smuzhiyun 	this = &host->nand;
1782*4882a593Smuzhiyun 	mtd = nand_to_mtd(this);
1783*4882a593Smuzhiyun 	mtd->dev.parent = &pdev->dev;
1784*4882a593Smuzhiyun 	mtd->name = DRIVER_NAME;
1785*4882a593Smuzhiyun 
1786*4882a593Smuzhiyun 	/* 50 us command delay time */
1787*4882a593Smuzhiyun 	this->legacy.chip_delay = 5;
1788*4882a593Smuzhiyun 
1789*4882a593Smuzhiyun 	nand_set_controller_data(this, host);
1790*4882a593Smuzhiyun 	nand_set_flash_node(this, pdev->dev.of_node),
1791*4882a593Smuzhiyun 	this->legacy.dev_ready = mxc_nand_dev_ready;
1792*4882a593Smuzhiyun 	this->legacy.cmdfunc = mxc_nand_command;
1793*4882a593Smuzhiyun 	this->legacy.read_byte = mxc_nand_read_byte;
1794*4882a593Smuzhiyun 	this->legacy.write_buf = mxc_nand_write_buf;
1795*4882a593Smuzhiyun 	this->legacy.read_buf = mxc_nand_read_buf;
1796*4882a593Smuzhiyun 	this->legacy.set_features = mxc_nand_set_features;
1797*4882a593Smuzhiyun 	this->legacy.get_features = mxc_nand_get_features;
1798*4882a593Smuzhiyun 
1799*4882a593Smuzhiyun 	host->clk = devm_clk_get(&pdev->dev, NULL);
1800*4882a593Smuzhiyun 	if (IS_ERR(host->clk))
1801*4882a593Smuzhiyun 		return PTR_ERR(host->clk);
1802*4882a593Smuzhiyun 
1803*4882a593Smuzhiyun 	err = mxcnd_probe_dt(host);
1804*4882a593Smuzhiyun 	if (err > 0) {
1805*4882a593Smuzhiyun 		struct mxc_nand_platform_data *pdata =
1806*4882a593Smuzhiyun 					dev_get_platdata(&pdev->dev);
1807*4882a593Smuzhiyun 		if (pdata) {
1808*4882a593Smuzhiyun 			host->pdata = *pdata;
1809*4882a593Smuzhiyun 			host->devtype_data = (struct mxc_nand_devtype_data *)
1810*4882a593Smuzhiyun 						pdev->id_entry->driver_data;
1811*4882a593Smuzhiyun 		} else {
1812*4882a593Smuzhiyun 			err = -ENODEV;
1813*4882a593Smuzhiyun 		}
1814*4882a593Smuzhiyun 	}
1815*4882a593Smuzhiyun 	if (err < 0)
1816*4882a593Smuzhiyun 		return err;
1817*4882a593Smuzhiyun 
1818*4882a593Smuzhiyun 	if (!host->devtype_data->setup_interface)
1819*4882a593Smuzhiyun 		this->options |= NAND_KEEP_TIMINGS;
1820*4882a593Smuzhiyun 
1821*4882a593Smuzhiyun 	if (host->devtype_data->needs_ip) {
1822*4882a593Smuzhiyun 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1823*4882a593Smuzhiyun 		host->regs_ip = devm_ioremap_resource(&pdev->dev, res);
1824*4882a593Smuzhiyun 		if (IS_ERR(host->regs_ip))
1825*4882a593Smuzhiyun 			return PTR_ERR(host->regs_ip);
1826*4882a593Smuzhiyun 
1827*4882a593Smuzhiyun 		res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1828*4882a593Smuzhiyun 	} else {
1829*4882a593Smuzhiyun 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1830*4882a593Smuzhiyun 	}
1831*4882a593Smuzhiyun 
1832*4882a593Smuzhiyun 	host->base = devm_ioremap_resource(&pdev->dev, res);
1833*4882a593Smuzhiyun 	if (IS_ERR(host->base))
1834*4882a593Smuzhiyun 		return PTR_ERR(host->base);
1835*4882a593Smuzhiyun 
1836*4882a593Smuzhiyun 	host->main_area0 = host->base;
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	if (host->devtype_data->regs_offset)
1839*4882a593Smuzhiyun 		host->regs = host->base + host->devtype_data->regs_offset;
1840*4882a593Smuzhiyun 	host->spare0 = host->base + host->devtype_data->spare0_offset;
1841*4882a593Smuzhiyun 	if (host->devtype_data->axi_offset)
1842*4882a593Smuzhiyun 		host->regs_axi = host->base + host->devtype_data->axi_offset;
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 	this->legacy.select_chip = host->devtype_data->select_chip;
1845*4882a593Smuzhiyun 
1846*4882a593Smuzhiyun 	/* NAND bus width determines access functions used by upper layer */
1847*4882a593Smuzhiyun 	if (host->pdata.width == 2)
1848*4882a593Smuzhiyun 		this->options |= NAND_BUSWIDTH_16;
1849*4882a593Smuzhiyun 
1850*4882a593Smuzhiyun 	/* update flash based bbt */
1851*4882a593Smuzhiyun 	if (host->pdata.flash_bbt)
1852*4882a593Smuzhiyun 		this->bbt_options |= NAND_BBT_USE_FLASH;
1853*4882a593Smuzhiyun 
1854*4882a593Smuzhiyun 	init_completion(&host->op_completion);
1855*4882a593Smuzhiyun 
1856*4882a593Smuzhiyun 	host->irq = platform_get_irq(pdev, 0);
1857*4882a593Smuzhiyun 	if (host->irq < 0)
1858*4882a593Smuzhiyun 		return host->irq;
1859*4882a593Smuzhiyun 
1860*4882a593Smuzhiyun 	/*
1861*4882a593Smuzhiyun 	 * Use host->devtype_data->irq_control() here instead of irq_control()
1862*4882a593Smuzhiyun 	 * because we must not disable_irq_nosync without having requested the
1863*4882a593Smuzhiyun 	 * irq.
1864*4882a593Smuzhiyun 	 */
1865*4882a593Smuzhiyun 	host->devtype_data->irq_control(host, 0);
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	err = devm_request_irq(&pdev->dev, host->irq, mxc_nfc_irq,
1868*4882a593Smuzhiyun 			0, DRIVER_NAME, host);
1869*4882a593Smuzhiyun 	if (err)
1870*4882a593Smuzhiyun 		return err;
1871*4882a593Smuzhiyun 
1872*4882a593Smuzhiyun 	err = clk_prepare_enable(host->clk);
1873*4882a593Smuzhiyun 	if (err)
1874*4882a593Smuzhiyun 		return err;
1875*4882a593Smuzhiyun 	host->clk_act = 1;
1876*4882a593Smuzhiyun 
1877*4882a593Smuzhiyun 	/*
1878*4882a593Smuzhiyun 	 * Now that we "own" the interrupt make sure the interrupt mask bit is
1879*4882a593Smuzhiyun 	 * cleared on i.MX21. Otherwise we can't read the interrupt status bit
1880*4882a593Smuzhiyun 	 * on this machine.
1881*4882a593Smuzhiyun 	 */
1882*4882a593Smuzhiyun 	if (host->devtype_data->irqpending_quirk) {
1883*4882a593Smuzhiyun 		disable_irq_nosync(host->irq);
1884*4882a593Smuzhiyun 		host->devtype_data->irq_control(host, 1);
1885*4882a593Smuzhiyun 	}
1886*4882a593Smuzhiyun 
1887*4882a593Smuzhiyun 	/* Scan the NAND device */
1888*4882a593Smuzhiyun 	this->legacy.dummy_controller.ops = &mxcnd_controller_ops;
1889*4882a593Smuzhiyun 	err = nand_scan(this, is_imx25_nfc(host) ? 4 : 1);
1890*4882a593Smuzhiyun 	if (err)
1891*4882a593Smuzhiyun 		goto escan;
1892*4882a593Smuzhiyun 
1893*4882a593Smuzhiyun 	/* Register the partitions */
1894*4882a593Smuzhiyun 	err = mtd_device_parse_register(mtd, part_probes, NULL,
1895*4882a593Smuzhiyun 					host->pdata.parts,
1896*4882a593Smuzhiyun 					host->pdata.nr_parts);
1897*4882a593Smuzhiyun 	if (err)
1898*4882a593Smuzhiyun 		goto cleanup_nand;
1899*4882a593Smuzhiyun 
1900*4882a593Smuzhiyun 	platform_set_drvdata(pdev, host);
1901*4882a593Smuzhiyun 
1902*4882a593Smuzhiyun 	return 0;
1903*4882a593Smuzhiyun 
1904*4882a593Smuzhiyun cleanup_nand:
1905*4882a593Smuzhiyun 	nand_cleanup(this);
1906*4882a593Smuzhiyun escan:
1907*4882a593Smuzhiyun 	if (host->clk_act)
1908*4882a593Smuzhiyun 		clk_disable_unprepare(host->clk);
1909*4882a593Smuzhiyun 
1910*4882a593Smuzhiyun 	return err;
1911*4882a593Smuzhiyun }
1912*4882a593Smuzhiyun 
mxcnd_remove(struct platform_device * pdev)1913*4882a593Smuzhiyun static int mxcnd_remove(struct platform_device *pdev)
1914*4882a593Smuzhiyun {
1915*4882a593Smuzhiyun 	struct mxc_nand_host *host = platform_get_drvdata(pdev);
1916*4882a593Smuzhiyun 	struct nand_chip *chip = &host->nand;
1917*4882a593Smuzhiyun 	int ret;
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	ret = mtd_device_unregister(nand_to_mtd(chip));
1920*4882a593Smuzhiyun 	WARN_ON(ret);
1921*4882a593Smuzhiyun 	nand_cleanup(chip);
1922*4882a593Smuzhiyun 	if (host->clk_act)
1923*4882a593Smuzhiyun 		clk_disable_unprepare(host->clk);
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun 	return 0;
1926*4882a593Smuzhiyun }
1927*4882a593Smuzhiyun 
1928*4882a593Smuzhiyun static struct platform_driver mxcnd_driver = {
1929*4882a593Smuzhiyun 	.driver = {
1930*4882a593Smuzhiyun 		   .name = DRIVER_NAME,
1931*4882a593Smuzhiyun 		   .of_match_table = of_match_ptr(mxcnd_dt_ids),
1932*4882a593Smuzhiyun 	},
1933*4882a593Smuzhiyun 	.id_table = mxcnd_devtype,
1934*4882a593Smuzhiyun 	.probe = mxcnd_probe,
1935*4882a593Smuzhiyun 	.remove = mxcnd_remove,
1936*4882a593Smuzhiyun };
1937*4882a593Smuzhiyun module_platform_driver(mxcnd_driver);
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1940*4882a593Smuzhiyun MODULE_DESCRIPTION("MXC NAND MTD driver");
1941*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1942