1*cfcc706cSMiquel Raynal /*
2*cfcc706cSMiquel Raynal * Copyright 2004-2007 Freescale Semiconductor, Inc.
3*cfcc706cSMiquel Raynal * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4*cfcc706cSMiquel Raynal * Copyright 2009 Ilya Yanok, <yanok@emcraft.com>
5*cfcc706cSMiquel Raynal *
6*cfcc706cSMiquel Raynal * SPDX-License-Identifier: GPL-2.0+
7*cfcc706cSMiquel Raynal */
8*cfcc706cSMiquel Raynal
9*cfcc706cSMiquel Raynal #include <common.h>
10*cfcc706cSMiquel Raynal #include <nand.h>
11*cfcc706cSMiquel Raynal #include <linux/err.h>
12*cfcc706cSMiquel Raynal #include <asm/io.h>
13*cfcc706cSMiquel Raynal #if defined(CONFIG_MX25) || defined(CONFIG_MX27) || defined(CONFIG_MX35) || \
14*cfcc706cSMiquel Raynal defined(CONFIG_MX51) || defined(CONFIG_MX53)
15*cfcc706cSMiquel Raynal #include <asm/arch/imx-regs.h>
16*cfcc706cSMiquel Raynal #endif
17*cfcc706cSMiquel Raynal #include "mxc_nand.h"
18*cfcc706cSMiquel Raynal
19*cfcc706cSMiquel Raynal #define DRIVER_NAME "mxc_nand"
20*cfcc706cSMiquel Raynal
21*cfcc706cSMiquel Raynal struct mxc_nand_host {
22*cfcc706cSMiquel Raynal struct nand_chip *nand;
23*cfcc706cSMiquel Raynal
24*cfcc706cSMiquel Raynal struct mxc_nand_regs __iomem *regs;
25*cfcc706cSMiquel Raynal #ifdef MXC_NFC_V3_2
26*cfcc706cSMiquel Raynal struct mxc_nand_ip_regs __iomem *ip_regs;
27*cfcc706cSMiquel Raynal #endif
28*cfcc706cSMiquel Raynal int spare_only;
29*cfcc706cSMiquel Raynal int status_request;
30*cfcc706cSMiquel Raynal int pagesize_2k;
31*cfcc706cSMiquel Raynal int clk_act;
32*cfcc706cSMiquel Raynal uint16_t col_addr;
33*cfcc706cSMiquel Raynal unsigned int page_addr;
34*cfcc706cSMiquel Raynal };
35*cfcc706cSMiquel Raynal
36*cfcc706cSMiquel Raynal static struct mxc_nand_host mxc_host;
37*cfcc706cSMiquel Raynal static struct mxc_nand_host *host = &mxc_host;
38*cfcc706cSMiquel Raynal
39*cfcc706cSMiquel Raynal /* Define delays in microsec for NAND device operations */
40*cfcc706cSMiquel Raynal #define TROP_US_DELAY 2000
41*cfcc706cSMiquel Raynal /* Macros to get byte and bit positions of ECC */
42*cfcc706cSMiquel Raynal #define COLPOS(x) ((x) >> 3)
43*cfcc706cSMiquel Raynal #define BITPOS(x) ((x) & 0xf)
44*cfcc706cSMiquel Raynal
45*cfcc706cSMiquel Raynal /* Define single bit Error positions in Main & Spare area */
46*cfcc706cSMiquel Raynal #define MAIN_SINGLEBIT_ERROR 0x4
47*cfcc706cSMiquel Raynal #define SPARE_SINGLEBIT_ERROR 0x1
48*cfcc706cSMiquel Raynal
49*cfcc706cSMiquel Raynal /* OOB placement block for use with hardware ecc generation */
50*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1)
51*cfcc706cSMiquel Raynal #ifndef CONFIG_SYS_NAND_LARGEPAGE
52*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_hw_eccoob = {
53*cfcc706cSMiquel Raynal .eccbytes = 5,
54*cfcc706cSMiquel Raynal .eccpos = {6, 7, 8, 9, 10},
55*cfcc706cSMiquel Raynal .oobfree = { {0, 5}, {11, 5}, }
56*cfcc706cSMiquel Raynal };
57*cfcc706cSMiquel Raynal #else
58*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_hw_eccoob2k = {
59*cfcc706cSMiquel Raynal .eccbytes = 20,
60*cfcc706cSMiquel Raynal .eccpos = {
61*cfcc706cSMiquel Raynal 6, 7, 8, 9, 10,
62*cfcc706cSMiquel Raynal 22, 23, 24, 25, 26,
63*cfcc706cSMiquel Raynal 38, 39, 40, 41, 42,
64*cfcc706cSMiquel Raynal 54, 55, 56, 57, 58,
65*cfcc706cSMiquel Raynal },
66*cfcc706cSMiquel Raynal .oobfree = { {2, 4}, {11, 11}, {27, 11}, {43, 11}, {59, 5} },
67*cfcc706cSMiquel Raynal };
68*cfcc706cSMiquel Raynal #endif
69*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
70*cfcc706cSMiquel Raynal #ifndef CONFIG_SYS_NAND_LARGEPAGE
71*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_hw_eccoob = {
72*cfcc706cSMiquel Raynal .eccbytes = 9,
73*cfcc706cSMiquel Raynal .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
74*cfcc706cSMiquel Raynal .oobfree = { {2, 5} }
75*cfcc706cSMiquel Raynal };
76*cfcc706cSMiquel Raynal #else
77*cfcc706cSMiquel Raynal static struct nand_ecclayout nand_hw_eccoob2k = {
78*cfcc706cSMiquel Raynal .eccbytes = 36,
79*cfcc706cSMiquel Raynal .eccpos = {
80*cfcc706cSMiquel Raynal 7, 8, 9, 10, 11, 12, 13, 14, 15,
81*cfcc706cSMiquel Raynal 23, 24, 25, 26, 27, 28, 29, 30, 31,
82*cfcc706cSMiquel Raynal 39, 40, 41, 42, 43, 44, 45, 46, 47,
83*cfcc706cSMiquel Raynal 55, 56, 57, 58, 59, 60, 61, 62, 63,
84*cfcc706cSMiquel Raynal },
85*cfcc706cSMiquel Raynal .oobfree = { {2, 5}, {16, 7}, {32, 7}, {48, 7} },
86*cfcc706cSMiquel Raynal };
87*cfcc706cSMiquel Raynal #endif
88*cfcc706cSMiquel Raynal #endif
89*cfcc706cSMiquel Raynal
is_16bit_nand(void)90*cfcc706cSMiquel Raynal static int is_16bit_nand(void)
91*cfcc706cSMiquel Raynal {
92*cfcc706cSMiquel Raynal #if defined(CONFIG_SYS_NAND_BUSWIDTH_16BIT)
93*cfcc706cSMiquel Raynal return 1;
94*cfcc706cSMiquel Raynal #else
95*cfcc706cSMiquel Raynal return 0;
96*cfcc706cSMiquel Raynal #endif
97*cfcc706cSMiquel Raynal }
98*cfcc706cSMiquel Raynal
mxc_nand_memcpy32(uint32_t * dest,uint32_t * source,size_t size)99*cfcc706cSMiquel Raynal static uint32_t *mxc_nand_memcpy32(uint32_t *dest, uint32_t *source, size_t size)
100*cfcc706cSMiquel Raynal {
101*cfcc706cSMiquel Raynal uint32_t *d = dest;
102*cfcc706cSMiquel Raynal
103*cfcc706cSMiquel Raynal size >>= 2;
104*cfcc706cSMiquel Raynal while (size--)
105*cfcc706cSMiquel Raynal __raw_writel(__raw_readl(source++), d++);
106*cfcc706cSMiquel Raynal return dest;
107*cfcc706cSMiquel Raynal }
108*cfcc706cSMiquel Raynal
109*cfcc706cSMiquel Raynal /*
110*cfcc706cSMiquel Raynal * This function polls the NANDFC to wait for the basic operation to
111*cfcc706cSMiquel Raynal * complete by checking the INT bit.
112*cfcc706cSMiquel Raynal */
wait_op_done(struct mxc_nand_host * host,int max_retries,uint16_t param)113*cfcc706cSMiquel Raynal static void wait_op_done(struct mxc_nand_host *host, int max_retries,
114*cfcc706cSMiquel Raynal uint16_t param)
115*cfcc706cSMiquel Raynal {
116*cfcc706cSMiquel Raynal uint32_t tmp;
117*cfcc706cSMiquel Raynal
118*cfcc706cSMiquel Raynal while (max_retries-- > 0) {
119*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
120*cfcc706cSMiquel Raynal tmp = readnfc(&host->regs->config2);
121*cfcc706cSMiquel Raynal if (tmp & NFC_V1_V2_CONFIG2_INT) {
122*cfcc706cSMiquel Raynal tmp &= ~NFC_V1_V2_CONFIG2_INT;
123*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config2);
124*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
125*cfcc706cSMiquel Raynal tmp = readnfc(&host->ip_regs->ipc);
126*cfcc706cSMiquel Raynal if (tmp & NFC_V3_IPC_INT) {
127*cfcc706cSMiquel Raynal tmp &= ~NFC_V3_IPC_INT;
128*cfcc706cSMiquel Raynal writenfc(tmp, &host->ip_regs->ipc);
129*cfcc706cSMiquel Raynal #endif
130*cfcc706cSMiquel Raynal break;
131*cfcc706cSMiquel Raynal }
132*cfcc706cSMiquel Raynal udelay(1);
133*cfcc706cSMiquel Raynal }
134*cfcc706cSMiquel Raynal if (max_retries < 0) {
135*cfcc706cSMiquel Raynal pr_debug("%s(%d): INT not set\n",
136*cfcc706cSMiquel Raynal __func__, param);
137*cfcc706cSMiquel Raynal }
138*cfcc706cSMiquel Raynal }
139*cfcc706cSMiquel Raynal
140*cfcc706cSMiquel Raynal /*
141*cfcc706cSMiquel Raynal * This function issues the specified command to the NAND device and
142*cfcc706cSMiquel Raynal * waits for completion.
143*cfcc706cSMiquel Raynal */
144*cfcc706cSMiquel Raynal static void send_cmd(struct mxc_nand_host *host, uint16_t cmd)
145*cfcc706cSMiquel Raynal {
146*cfcc706cSMiquel Raynal pr_debug("send_cmd(host, 0x%x)\n", cmd);
147*cfcc706cSMiquel Raynal
148*cfcc706cSMiquel Raynal writenfc(cmd, &host->regs->flash_cmd);
149*cfcc706cSMiquel Raynal writenfc(NFC_CMD, &host->regs->operation);
150*cfcc706cSMiquel Raynal
151*cfcc706cSMiquel Raynal /* Wait for operation to complete */
152*cfcc706cSMiquel Raynal wait_op_done(host, TROP_US_DELAY, cmd);
153*cfcc706cSMiquel Raynal }
154*cfcc706cSMiquel Raynal
155*cfcc706cSMiquel Raynal /*
156*cfcc706cSMiquel Raynal * This function sends an address (or partial address) to the
157*cfcc706cSMiquel Raynal * NAND device. The address is used to select the source/destination for
158*cfcc706cSMiquel Raynal * a NAND command.
159*cfcc706cSMiquel Raynal */
160*cfcc706cSMiquel Raynal static void send_addr(struct mxc_nand_host *host, uint16_t addr)
161*cfcc706cSMiquel Raynal {
162*cfcc706cSMiquel Raynal pr_debug("send_addr(host, 0x%x)\n", addr);
163*cfcc706cSMiquel Raynal
164*cfcc706cSMiquel Raynal writenfc(addr, &host->regs->flash_addr);
165*cfcc706cSMiquel Raynal writenfc(NFC_ADDR, &host->regs->operation);
166*cfcc706cSMiquel Raynal
167*cfcc706cSMiquel Raynal /* Wait for operation to complete */
168*cfcc706cSMiquel Raynal wait_op_done(host, TROP_US_DELAY, addr);
169*cfcc706cSMiquel Raynal }
170*cfcc706cSMiquel Raynal
171*cfcc706cSMiquel Raynal /*
172*cfcc706cSMiquel Raynal * This function requests the NANDFC to initiate the transfer
173*cfcc706cSMiquel Raynal * of data currently in the NANDFC RAM buffer to the NAND device.
174*cfcc706cSMiquel Raynal */
175*cfcc706cSMiquel Raynal static void send_prog_page(struct mxc_nand_host *host, uint8_t buf_id,
176*cfcc706cSMiquel Raynal int spare_only)
177*cfcc706cSMiquel Raynal {
178*cfcc706cSMiquel Raynal if (spare_only)
179*cfcc706cSMiquel Raynal pr_debug("send_prog_page (%d)\n", spare_only);
180*cfcc706cSMiquel Raynal
181*cfcc706cSMiquel Raynal if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
182*cfcc706cSMiquel Raynal int i;
183*cfcc706cSMiquel Raynal /*
184*cfcc706cSMiquel Raynal * The controller copies the 64 bytes of spare data from
185*cfcc706cSMiquel Raynal * the first 16 bytes of each of the 4 64 byte spare buffers.
186*cfcc706cSMiquel Raynal * Copy the contiguous data starting in spare_area[0] to
187*cfcc706cSMiquel Raynal * the four spare area buffers.
188*cfcc706cSMiquel Raynal */
189*cfcc706cSMiquel Raynal for (i = 1; i < 4; i++) {
190*cfcc706cSMiquel Raynal void __iomem *src = &host->regs->spare_area[0][i * 16];
191*cfcc706cSMiquel Raynal void __iomem *dst = &host->regs->spare_area[i][0];
192*cfcc706cSMiquel Raynal
193*cfcc706cSMiquel Raynal mxc_nand_memcpy32(dst, src, 16);
194*cfcc706cSMiquel Raynal }
195*cfcc706cSMiquel Raynal }
196*cfcc706cSMiquel Raynal
197*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
198*cfcc706cSMiquel Raynal writenfc(buf_id, &host->regs->buf_addr);
199*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
200*cfcc706cSMiquel Raynal uint32_t tmp = readnfc(&host->regs->config1);
201*cfcc706cSMiquel Raynal tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
202*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG1_RBA(buf_id);
203*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
204*cfcc706cSMiquel Raynal #endif
205*cfcc706cSMiquel Raynal
206*cfcc706cSMiquel Raynal /* Configure spare or page+spare access */
207*cfcc706cSMiquel Raynal if (!host->pagesize_2k) {
208*cfcc706cSMiquel Raynal uint32_t config1 = readnfc(&host->regs->config1);
209*cfcc706cSMiquel Raynal if (spare_only)
210*cfcc706cSMiquel Raynal config1 |= NFC_CONFIG1_SP_EN;
211*cfcc706cSMiquel Raynal else
212*cfcc706cSMiquel Raynal config1 &= ~NFC_CONFIG1_SP_EN;
213*cfcc706cSMiquel Raynal writenfc(config1, &host->regs->config1);
214*cfcc706cSMiquel Raynal }
215*cfcc706cSMiquel Raynal
216*cfcc706cSMiquel Raynal writenfc(NFC_INPUT, &host->regs->operation);
217*cfcc706cSMiquel Raynal
218*cfcc706cSMiquel Raynal /* Wait for operation to complete */
219*cfcc706cSMiquel Raynal wait_op_done(host, TROP_US_DELAY, spare_only);
220*cfcc706cSMiquel Raynal }
221*cfcc706cSMiquel Raynal
222*cfcc706cSMiquel Raynal /*
223*cfcc706cSMiquel Raynal * Requests NANDFC to initiate the transfer of data from the
224*cfcc706cSMiquel Raynal * NAND device into in the NANDFC ram buffer.
225*cfcc706cSMiquel Raynal */
226*cfcc706cSMiquel Raynal static void send_read_page(struct mxc_nand_host *host, uint8_t buf_id,
227*cfcc706cSMiquel Raynal int spare_only)
228*cfcc706cSMiquel Raynal {
229*cfcc706cSMiquel Raynal pr_debug("send_read_page (%d)\n", spare_only);
230*cfcc706cSMiquel Raynal
231*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
232*cfcc706cSMiquel Raynal writenfc(buf_id, &host->regs->buf_addr);
233*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
234*cfcc706cSMiquel Raynal uint32_t tmp = readnfc(&host->regs->config1);
235*cfcc706cSMiquel Raynal tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
236*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG1_RBA(buf_id);
237*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
238*cfcc706cSMiquel Raynal #endif
239*cfcc706cSMiquel Raynal
240*cfcc706cSMiquel Raynal /* Configure spare or page+spare access */
241*cfcc706cSMiquel Raynal if (!host->pagesize_2k) {
242*cfcc706cSMiquel Raynal uint32_t config1 = readnfc(&host->regs->config1);
243*cfcc706cSMiquel Raynal if (spare_only)
244*cfcc706cSMiquel Raynal config1 |= NFC_CONFIG1_SP_EN;
245*cfcc706cSMiquel Raynal else
246*cfcc706cSMiquel Raynal config1 &= ~NFC_CONFIG1_SP_EN;
247*cfcc706cSMiquel Raynal writenfc(config1, &host->regs->config1);
248*cfcc706cSMiquel Raynal }
249*cfcc706cSMiquel Raynal
250*cfcc706cSMiquel Raynal writenfc(NFC_OUTPUT, &host->regs->operation);
251*cfcc706cSMiquel Raynal
252*cfcc706cSMiquel Raynal /* Wait for operation to complete */
253*cfcc706cSMiquel Raynal wait_op_done(host, TROP_US_DELAY, spare_only);
254*cfcc706cSMiquel Raynal
255*cfcc706cSMiquel Raynal if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
256*cfcc706cSMiquel Raynal int i;
257*cfcc706cSMiquel Raynal
258*cfcc706cSMiquel Raynal /*
259*cfcc706cSMiquel Raynal * The controller copies the 64 bytes of spare data to
260*cfcc706cSMiquel Raynal * the first 16 bytes of each of the 4 spare buffers.
261*cfcc706cSMiquel Raynal * Make the data contiguous starting in spare_area[0].
262*cfcc706cSMiquel Raynal */
263*cfcc706cSMiquel Raynal for (i = 1; i < 4; i++) {
264*cfcc706cSMiquel Raynal void __iomem *src = &host->regs->spare_area[i][0];
265*cfcc706cSMiquel Raynal void __iomem *dst = &host->regs->spare_area[0][i * 16];
266*cfcc706cSMiquel Raynal
267*cfcc706cSMiquel Raynal mxc_nand_memcpy32(dst, src, 16);
268*cfcc706cSMiquel Raynal }
269*cfcc706cSMiquel Raynal }
270*cfcc706cSMiquel Raynal }
271*cfcc706cSMiquel Raynal
272*cfcc706cSMiquel Raynal /* Request the NANDFC to perform a read of the NAND device ID. */
273*cfcc706cSMiquel Raynal static void send_read_id(struct mxc_nand_host *host)
274*cfcc706cSMiquel Raynal {
275*cfcc706cSMiquel Raynal uint32_t tmp;
276*cfcc706cSMiquel Raynal
277*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
278*cfcc706cSMiquel Raynal /* NANDFC buffer 0 is used for device ID output */
279*cfcc706cSMiquel Raynal writenfc(0x0, &host->regs->buf_addr);
280*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
281*cfcc706cSMiquel Raynal tmp = readnfc(&host->regs->config1);
282*cfcc706cSMiquel Raynal tmp &= ~NFC_V3_CONFIG1_RBA_MASK;
283*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
284*cfcc706cSMiquel Raynal #endif
285*cfcc706cSMiquel Raynal
286*cfcc706cSMiquel Raynal /* Read ID into main buffer */
287*cfcc706cSMiquel Raynal tmp = readnfc(&host->regs->config1);
288*cfcc706cSMiquel Raynal tmp &= ~NFC_CONFIG1_SP_EN;
289*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
290*cfcc706cSMiquel Raynal
291*cfcc706cSMiquel Raynal writenfc(NFC_ID, &host->regs->operation);
292*cfcc706cSMiquel Raynal
293*cfcc706cSMiquel Raynal /* Wait for operation to complete */
294*cfcc706cSMiquel Raynal wait_op_done(host, TROP_US_DELAY, 0);
295*cfcc706cSMiquel Raynal }
296*cfcc706cSMiquel Raynal
297*cfcc706cSMiquel Raynal /*
298*cfcc706cSMiquel Raynal * This function requests the NANDFC to perform a read of the
299*cfcc706cSMiquel Raynal * NAND device status and returns the current status.
300*cfcc706cSMiquel Raynal */
301*cfcc706cSMiquel Raynal static uint16_t get_dev_status(struct mxc_nand_host *host)
302*cfcc706cSMiquel Raynal {
303*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
304*cfcc706cSMiquel Raynal void __iomem *main_buf = host->regs->main_area[1];
305*cfcc706cSMiquel Raynal uint32_t store;
306*cfcc706cSMiquel Raynal #endif
307*cfcc706cSMiquel Raynal uint32_t ret, tmp;
308*cfcc706cSMiquel Raynal /* Issue status request to NAND device */
309*cfcc706cSMiquel Raynal
310*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
311*cfcc706cSMiquel Raynal /* store the main area1 first word, later do recovery */
312*cfcc706cSMiquel Raynal store = readl(main_buf);
313*cfcc706cSMiquel Raynal /* NANDFC buffer 1 is used for device status */
314*cfcc706cSMiquel Raynal writenfc(1, &host->regs->buf_addr);
315*cfcc706cSMiquel Raynal #endif
316*cfcc706cSMiquel Raynal
317*cfcc706cSMiquel Raynal /* Read status into main buffer */
318*cfcc706cSMiquel Raynal tmp = readnfc(&host->regs->config1);
319*cfcc706cSMiquel Raynal tmp &= ~NFC_CONFIG1_SP_EN;
320*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
321*cfcc706cSMiquel Raynal
322*cfcc706cSMiquel Raynal writenfc(NFC_STATUS, &host->regs->operation);
323*cfcc706cSMiquel Raynal
324*cfcc706cSMiquel Raynal /* Wait for operation to complete */
325*cfcc706cSMiquel Raynal wait_op_done(host, TROP_US_DELAY, 0);
326*cfcc706cSMiquel Raynal
327*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
328*cfcc706cSMiquel Raynal /*
329*cfcc706cSMiquel Raynal * Status is placed in first word of main buffer
330*cfcc706cSMiquel Raynal * get status, then recovery area 1 data
331*cfcc706cSMiquel Raynal */
332*cfcc706cSMiquel Raynal ret = readw(main_buf);
333*cfcc706cSMiquel Raynal writel(store, main_buf);
334*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
335*cfcc706cSMiquel Raynal ret = readnfc(&host->regs->config1) >> 16;
336*cfcc706cSMiquel Raynal #endif
337*cfcc706cSMiquel Raynal
338*cfcc706cSMiquel Raynal return ret;
339*cfcc706cSMiquel Raynal }
340*cfcc706cSMiquel Raynal
341*cfcc706cSMiquel Raynal /* This function is used by upper layer to checks if device is ready */
342*cfcc706cSMiquel Raynal static int mxc_nand_dev_ready(struct mtd_info *mtd)
343*cfcc706cSMiquel Raynal {
344*cfcc706cSMiquel Raynal /*
345*cfcc706cSMiquel Raynal * NFC handles R/B internally. Therefore, this function
346*cfcc706cSMiquel Raynal * always returns status as ready.
347*cfcc706cSMiquel Raynal */
348*cfcc706cSMiquel Raynal return 1;
349*cfcc706cSMiquel Raynal }
350*cfcc706cSMiquel Raynal
351*cfcc706cSMiquel Raynal static void _mxc_nand_enable_hwecc(struct mtd_info *mtd, int on)
352*cfcc706cSMiquel Raynal {
353*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
354*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
355*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
356*cfcc706cSMiquel Raynal uint16_t tmp = readnfc(&host->regs->config1);
357*cfcc706cSMiquel Raynal
358*cfcc706cSMiquel Raynal if (on)
359*cfcc706cSMiquel Raynal tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
360*cfcc706cSMiquel Raynal else
361*cfcc706cSMiquel Raynal tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
362*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
363*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
364*cfcc706cSMiquel Raynal uint32_t tmp = readnfc(&host->ip_regs->config2);
365*cfcc706cSMiquel Raynal
366*cfcc706cSMiquel Raynal if (on)
367*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG2_ECC_EN;
368*cfcc706cSMiquel Raynal else
369*cfcc706cSMiquel Raynal tmp &= ~NFC_V3_CONFIG2_ECC_EN;
370*cfcc706cSMiquel Raynal writenfc(tmp, &host->ip_regs->config2);
371*cfcc706cSMiquel Raynal #endif
372*cfcc706cSMiquel Raynal }
373*cfcc706cSMiquel Raynal
374*cfcc706cSMiquel Raynal #ifdef CONFIG_MXC_NAND_HWECC
375*cfcc706cSMiquel Raynal static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
376*cfcc706cSMiquel Raynal {
377*cfcc706cSMiquel Raynal /*
378*cfcc706cSMiquel Raynal * If HW ECC is enabled, we turn it on during init. There is
379*cfcc706cSMiquel Raynal * no need to enable again here.
380*cfcc706cSMiquel Raynal */
381*cfcc706cSMiquel Raynal }
382*cfcc706cSMiquel Raynal
383*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
384*cfcc706cSMiquel Raynal static int mxc_nand_read_oob_syndrome(struct mtd_info *mtd,
385*cfcc706cSMiquel Raynal struct nand_chip *chip,
386*cfcc706cSMiquel Raynal int page)
387*cfcc706cSMiquel Raynal {
388*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(chip);
389*cfcc706cSMiquel Raynal uint8_t *buf = chip->oob_poi;
390*cfcc706cSMiquel Raynal int length = mtd->oobsize;
391*cfcc706cSMiquel Raynal int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
392*cfcc706cSMiquel Raynal uint8_t *bufpoi = buf;
393*cfcc706cSMiquel Raynal int i, toread;
394*cfcc706cSMiquel Raynal
395*cfcc706cSMiquel Raynal pr_debug("%s: Reading OOB area of page %u to oob %p\n",
396*cfcc706cSMiquel Raynal __func__, page, buf);
397*cfcc706cSMiquel Raynal
398*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
399*cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.steps; i++) {
400*cfcc706cSMiquel Raynal toread = min_t(int, length, chip->ecc.prepad);
401*cfcc706cSMiquel Raynal if (toread) {
402*cfcc706cSMiquel Raynal chip->read_buf(mtd, bufpoi, toread);
403*cfcc706cSMiquel Raynal bufpoi += toread;
404*cfcc706cSMiquel Raynal length -= toread;
405*cfcc706cSMiquel Raynal }
406*cfcc706cSMiquel Raynal bufpoi += chip->ecc.bytes;
407*cfcc706cSMiquel Raynal host->col_addr += chip->ecc.bytes;
408*cfcc706cSMiquel Raynal length -= chip->ecc.bytes;
409*cfcc706cSMiquel Raynal
410*cfcc706cSMiquel Raynal toread = min_t(int, length, chip->ecc.postpad);
411*cfcc706cSMiquel Raynal if (toread) {
412*cfcc706cSMiquel Raynal chip->read_buf(mtd, bufpoi, toread);
413*cfcc706cSMiquel Raynal bufpoi += toread;
414*cfcc706cSMiquel Raynal length -= toread;
415*cfcc706cSMiquel Raynal }
416*cfcc706cSMiquel Raynal }
417*cfcc706cSMiquel Raynal if (length > 0)
418*cfcc706cSMiquel Raynal chip->read_buf(mtd, bufpoi, length);
419*cfcc706cSMiquel Raynal
420*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 0);
421*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB,
422*cfcc706cSMiquel Raynal mtd->writesize + chip->ecc.prepad, page);
423*cfcc706cSMiquel Raynal bufpoi = buf + chip->ecc.prepad;
424*cfcc706cSMiquel Raynal length = mtd->oobsize - chip->ecc.prepad;
425*cfcc706cSMiquel Raynal for (i = 0; i < chip->ecc.steps; i++) {
426*cfcc706cSMiquel Raynal toread = min_t(int, length, chip->ecc.bytes);
427*cfcc706cSMiquel Raynal chip->read_buf(mtd, bufpoi, toread);
428*cfcc706cSMiquel Raynal bufpoi += eccpitch;
429*cfcc706cSMiquel Raynal length -= eccpitch;
430*cfcc706cSMiquel Raynal host->col_addr += chip->ecc.postpad + chip->ecc.prepad;
431*cfcc706cSMiquel Raynal }
432*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 1);
433*cfcc706cSMiquel Raynal return 1;
434*cfcc706cSMiquel Raynal }
435*cfcc706cSMiquel Raynal
436*cfcc706cSMiquel Raynal static int mxc_nand_read_page_raw_syndrome(struct mtd_info *mtd,
437*cfcc706cSMiquel Raynal struct nand_chip *chip,
438*cfcc706cSMiquel Raynal uint8_t *buf,
439*cfcc706cSMiquel Raynal int oob_required,
440*cfcc706cSMiquel Raynal int page)
441*cfcc706cSMiquel Raynal {
442*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(chip);
443*cfcc706cSMiquel Raynal int eccsize = chip->ecc.size;
444*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
445*cfcc706cSMiquel Raynal int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
446*cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi;
447*cfcc706cSMiquel Raynal int steps, size;
448*cfcc706cSMiquel Raynal int n;
449*cfcc706cSMiquel Raynal
450*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 0);
451*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
452*cfcc706cSMiquel Raynal
453*cfcc706cSMiquel Raynal for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
454*cfcc706cSMiquel Raynal host->col_addr = n * eccsize;
455*cfcc706cSMiquel Raynal chip->read_buf(mtd, buf, eccsize);
456*cfcc706cSMiquel Raynal buf += eccsize;
457*cfcc706cSMiquel Raynal
458*cfcc706cSMiquel Raynal host->col_addr = mtd->writesize + n * eccpitch;
459*cfcc706cSMiquel Raynal if (chip->ecc.prepad) {
460*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.prepad);
461*cfcc706cSMiquel Raynal oob += chip->ecc.prepad;
462*cfcc706cSMiquel Raynal }
463*cfcc706cSMiquel Raynal
464*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, eccbytes);
465*cfcc706cSMiquel Raynal oob += eccbytes;
466*cfcc706cSMiquel Raynal
467*cfcc706cSMiquel Raynal if (chip->ecc.postpad) {
468*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.postpad);
469*cfcc706cSMiquel Raynal oob += chip->ecc.postpad;
470*cfcc706cSMiquel Raynal }
471*cfcc706cSMiquel Raynal }
472*cfcc706cSMiquel Raynal
473*cfcc706cSMiquel Raynal size = mtd->oobsize - (oob - chip->oob_poi);
474*cfcc706cSMiquel Raynal if (size)
475*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, size);
476*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 1);
477*cfcc706cSMiquel Raynal
478*cfcc706cSMiquel Raynal return 0;
479*cfcc706cSMiquel Raynal }
480*cfcc706cSMiquel Raynal
481*cfcc706cSMiquel Raynal static int mxc_nand_read_page_syndrome(struct mtd_info *mtd,
482*cfcc706cSMiquel Raynal struct nand_chip *chip,
483*cfcc706cSMiquel Raynal uint8_t *buf,
484*cfcc706cSMiquel Raynal int oob_required,
485*cfcc706cSMiquel Raynal int page)
486*cfcc706cSMiquel Raynal {
487*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(chip);
488*cfcc706cSMiquel Raynal int n, eccsize = chip->ecc.size;
489*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
490*cfcc706cSMiquel Raynal int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
491*cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps;
492*cfcc706cSMiquel Raynal uint8_t *p = buf;
493*cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi;
494*cfcc706cSMiquel Raynal
495*cfcc706cSMiquel Raynal pr_debug("Reading page %u to buf %p oob %p\n",
496*cfcc706cSMiquel Raynal page, buf, oob);
497*cfcc706cSMiquel Raynal
498*cfcc706cSMiquel Raynal /* first read the data area and the available portion of OOB */
499*cfcc706cSMiquel Raynal for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
500*cfcc706cSMiquel Raynal int stat;
501*cfcc706cSMiquel Raynal
502*cfcc706cSMiquel Raynal host->col_addr = n * eccsize;
503*cfcc706cSMiquel Raynal
504*cfcc706cSMiquel Raynal chip->read_buf(mtd, p, eccsize);
505*cfcc706cSMiquel Raynal
506*cfcc706cSMiquel Raynal host->col_addr = mtd->writesize + n * eccpitch;
507*cfcc706cSMiquel Raynal
508*cfcc706cSMiquel Raynal if (chip->ecc.prepad) {
509*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.prepad);
510*cfcc706cSMiquel Raynal oob += chip->ecc.prepad;
511*cfcc706cSMiquel Raynal }
512*cfcc706cSMiquel Raynal
513*cfcc706cSMiquel Raynal stat = chip->ecc.correct(mtd, p, oob, NULL);
514*cfcc706cSMiquel Raynal
515*cfcc706cSMiquel Raynal if (stat < 0)
516*cfcc706cSMiquel Raynal mtd->ecc_stats.failed++;
517*cfcc706cSMiquel Raynal else
518*cfcc706cSMiquel Raynal mtd->ecc_stats.corrected += stat;
519*cfcc706cSMiquel Raynal oob += eccbytes;
520*cfcc706cSMiquel Raynal
521*cfcc706cSMiquel Raynal if (chip->ecc.postpad) {
522*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, chip->ecc.postpad);
523*cfcc706cSMiquel Raynal oob += chip->ecc.postpad;
524*cfcc706cSMiquel Raynal }
525*cfcc706cSMiquel Raynal }
526*cfcc706cSMiquel Raynal
527*cfcc706cSMiquel Raynal /* Calculate remaining oob bytes */
528*cfcc706cSMiquel Raynal n = mtd->oobsize - (oob - chip->oob_poi);
529*cfcc706cSMiquel Raynal if (n)
530*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, n);
531*cfcc706cSMiquel Raynal
532*cfcc706cSMiquel Raynal /* Then switch ECC off and read the OOB area to get the ECC code */
533*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 0);
534*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_READOOB, mtd->writesize, page);
535*cfcc706cSMiquel Raynal eccsteps = chip->ecc.steps;
536*cfcc706cSMiquel Raynal oob = chip->oob_poi + chip->ecc.prepad;
537*cfcc706cSMiquel Raynal for (n = 0; eccsteps; n++, eccsteps--, p += eccsize) {
538*cfcc706cSMiquel Raynal host->col_addr = mtd->writesize +
539*cfcc706cSMiquel Raynal n * eccpitch +
540*cfcc706cSMiquel Raynal chip->ecc.prepad;
541*cfcc706cSMiquel Raynal chip->read_buf(mtd, oob, eccbytes);
542*cfcc706cSMiquel Raynal oob += eccbytes + chip->ecc.postpad;
543*cfcc706cSMiquel Raynal }
544*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 1);
545*cfcc706cSMiquel Raynal return 0;
546*cfcc706cSMiquel Raynal }
547*cfcc706cSMiquel Raynal
548*cfcc706cSMiquel Raynal static int mxc_nand_write_oob_syndrome(struct mtd_info *mtd,
549*cfcc706cSMiquel Raynal struct nand_chip *chip, int page)
550*cfcc706cSMiquel Raynal {
551*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(chip);
552*cfcc706cSMiquel Raynal int eccpitch = chip->ecc.bytes + chip->ecc.prepad + chip->ecc.postpad;
553*cfcc706cSMiquel Raynal int length = mtd->oobsize;
554*cfcc706cSMiquel Raynal int i, len, status, steps = chip->ecc.steps;
555*cfcc706cSMiquel Raynal const uint8_t *bufpoi = chip->oob_poi;
556*cfcc706cSMiquel Raynal
557*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
558*cfcc706cSMiquel Raynal for (i = 0; i < steps; i++) {
559*cfcc706cSMiquel Raynal len = min_t(int, length, eccpitch);
560*cfcc706cSMiquel Raynal
561*cfcc706cSMiquel Raynal chip->write_buf(mtd, bufpoi, len);
562*cfcc706cSMiquel Raynal bufpoi += len;
563*cfcc706cSMiquel Raynal length -= len;
564*cfcc706cSMiquel Raynal host->col_addr += chip->ecc.prepad + chip->ecc.postpad;
565*cfcc706cSMiquel Raynal }
566*cfcc706cSMiquel Raynal if (length > 0)
567*cfcc706cSMiquel Raynal chip->write_buf(mtd, bufpoi, length);
568*cfcc706cSMiquel Raynal
569*cfcc706cSMiquel Raynal chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
570*cfcc706cSMiquel Raynal status = chip->waitfunc(mtd, chip);
571*cfcc706cSMiquel Raynal return status & NAND_STATUS_FAIL ? -EIO : 0;
572*cfcc706cSMiquel Raynal }
573*cfcc706cSMiquel Raynal
574*cfcc706cSMiquel Raynal static int mxc_nand_write_page_raw_syndrome(struct mtd_info *mtd,
575*cfcc706cSMiquel Raynal struct nand_chip *chip,
576*cfcc706cSMiquel Raynal const uint8_t *buf,
577*cfcc706cSMiquel Raynal int oob_required, int page)
578*cfcc706cSMiquel Raynal {
579*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(chip);
580*cfcc706cSMiquel Raynal int eccsize = chip->ecc.size;
581*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
582*cfcc706cSMiquel Raynal int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
583*cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi;
584*cfcc706cSMiquel Raynal int steps, size;
585*cfcc706cSMiquel Raynal int n;
586*cfcc706cSMiquel Raynal
587*cfcc706cSMiquel Raynal for (n = 0, steps = chip->ecc.steps; steps > 0; n++, steps--) {
588*cfcc706cSMiquel Raynal host->col_addr = n * eccsize;
589*cfcc706cSMiquel Raynal chip->write_buf(mtd, buf, eccsize);
590*cfcc706cSMiquel Raynal buf += eccsize;
591*cfcc706cSMiquel Raynal
592*cfcc706cSMiquel Raynal host->col_addr = mtd->writesize + n * eccpitch;
593*cfcc706cSMiquel Raynal
594*cfcc706cSMiquel Raynal if (chip->ecc.prepad) {
595*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.prepad);
596*cfcc706cSMiquel Raynal oob += chip->ecc.prepad;
597*cfcc706cSMiquel Raynal }
598*cfcc706cSMiquel Raynal
599*cfcc706cSMiquel Raynal host->col_addr += eccbytes;
600*cfcc706cSMiquel Raynal oob += eccbytes;
601*cfcc706cSMiquel Raynal
602*cfcc706cSMiquel Raynal if (chip->ecc.postpad) {
603*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.postpad);
604*cfcc706cSMiquel Raynal oob += chip->ecc.postpad;
605*cfcc706cSMiquel Raynal }
606*cfcc706cSMiquel Raynal }
607*cfcc706cSMiquel Raynal
608*cfcc706cSMiquel Raynal size = mtd->oobsize - (oob - chip->oob_poi);
609*cfcc706cSMiquel Raynal if (size)
610*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, size);
611*cfcc706cSMiquel Raynal return 0;
612*cfcc706cSMiquel Raynal }
613*cfcc706cSMiquel Raynal
614*cfcc706cSMiquel Raynal static int mxc_nand_write_page_syndrome(struct mtd_info *mtd,
615*cfcc706cSMiquel Raynal struct nand_chip *chip,
616*cfcc706cSMiquel Raynal const uint8_t *buf,
617*cfcc706cSMiquel Raynal int oob_required, int page)
618*cfcc706cSMiquel Raynal {
619*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(chip);
620*cfcc706cSMiquel Raynal int i, n, eccsize = chip->ecc.size;
621*cfcc706cSMiquel Raynal int eccbytes = chip->ecc.bytes;
622*cfcc706cSMiquel Raynal int eccpitch = eccbytes + chip->ecc.prepad + chip->ecc.postpad;
623*cfcc706cSMiquel Raynal int eccsteps = chip->ecc.steps;
624*cfcc706cSMiquel Raynal const uint8_t *p = buf;
625*cfcc706cSMiquel Raynal uint8_t *oob = chip->oob_poi;
626*cfcc706cSMiquel Raynal
627*cfcc706cSMiquel Raynal chip->ecc.hwctl(mtd, NAND_ECC_WRITE);
628*cfcc706cSMiquel Raynal
629*cfcc706cSMiquel Raynal for (i = n = 0;
630*cfcc706cSMiquel Raynal eccsteps;
631*cfcc706cSMiquel Raynal n++, eccsteps--, i += eccbytes, p += eccsize) {
632*cfcc706cSMiquel Raynal host->col_addr = n * eccsize;
633*cfcc706cSMiquel Raynal
634*cfcc706cSMiquel Raynal chip->write_buf(mtd, p, eccsize);
635*cfcc706cSMiquel Raynal
636*cfcc706cSMiquel Raynal host->col_addr = mtd->writesize + n * eccpitch;
637*cfcc706cSMiquel Raynal
638*cfcc706cSMiquel Raynal if (chip->ecc.prepad) {
639*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.prepad);
640*cfcc706cSMiquel Raynal oob += chip->ecc.prepad;
641*cfcc706cSMiquel Raynal }
642*cfcc706cSMiquel Raynal
643*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, eccbytes);
644*cfcc706cSMiquel Raynal oob += eccbytes;
645*cfcc706cSMiquel Raynal
646*cfcc706cSMiquel Raynal if (chip->ecc.postpad) {
647*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, chip->ecc.postpad);
648*cfcc706cSMiquel Raynal oob += chip->ecc.postpad;
649*cfcc706cSMiquel Raynal }
650*cfcc706cSMiquel Raynal }
651*cfcc706cSMiquel Raynal
652*cfcc706cSMiquel Raynal /* Calculate remaining oob bytes */
653*cfcc706cSMiquel Raynal i = mtd->oobsize - (oob - chip->oob_poi);
654*cfcc706cSMiquel Raynal if (i)
655*cfcc706cSMiquel Raynal chip->write_buf(mtd, oob, i);
656*cfcc706cSMiquel Raynal return 0;
657*cfcc706cSMiquel Raynal }
658*cfcc706cSMiquel Raynal
659*cfcc706cSMiquel Raynal static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
660*cfcc706cSMiquel Raynal u_char *read_ecc, u_char *calc_ecc)
661*cfcc706cSMiquel Raynal {
662*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
663*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
664*cfcc706cSMiquel Raynal uint32_t ecc_status = readl(&host->regs->ecc_status_result);
665*cfcc706cSMiquel Raynal int subpages = mtd->writesize / nand_chip->subpagesize;
666*cfcc706cSMiquel Raynal int pg2blk_shift = nand_chip->phys_erase_shift -
667*cfcc706cSMiquel Raynal nand_chip->page_shift;
668*cfcc706cSMiquel Raynal
669*cfcc706cSMiquel Raynal do {
670*cfcc706cSMiquel Raynal if ((ecc_status & 0xf) > 4) {
671*cfcc706cSMiquel Raynal static int last_bad = -1;
672*cfcc706cSMiquel Raynal
673*cfcc706cSMiquel Raynal if (last_bad != host->page_addr >> pg2blk_shift) {
674*cfcc706cSMiquel Raynal last_bad = host->page_addr >> pg2blk_shift;
675*cfcc706cSMiquel Raynal printk(KERN_DEBUG
676*cfcc706cSMiquel Raynal "MXC_NAND: HWECC uncorrectable ECC error"
677*cfcc706cSMiquel Raynal " in block %u page %u subpage %d\n",
678*cfcc706cSMiquel Raynal last_bad, host->page_addr,
679*cfcc706cSMiquel Raynal mtd->writesize / nand_chip->subpagesize
680*cfcc706cSMiquel Raynal - subpages);
681*cfcc706cSMiquel Raynal }
682*cfcc706cSMiquel Raynal return -EBADMSG;
683*cfcc706cSMiquel Raynal }
684*cfcc706cSMiquel Raynal ecc_status >>= 4;
685*cfcc706cSMiquel Raynal subpages--;
686*cfcc706cSMiquel Raynal } while (subpages > 0);
687*cfcc706cSMiquel Raynal
688*cfcc706cSMiquel Raynal return 0;
689*cfcc706cSMiquel Raynal }
690*cfcc706cSMiquel Raynal #else
691*cfcc706cSMiquel Raynal #define mxc_nand_read_page_syndrome NULL
692*cfcc706cSMiquel Raynal #define mxc_nand_read_page_raw_syndrome NULL
693*cfcc706cSMiquel Raynal #define mxc_nand_read_oob_syndrome NULL
694*cfcc706cSMiquel Raynal #define mxc_nand_write_page_syndrome NULL
695*cfcc706cSMiquel Raynal #define mxc_nand_write_page_raw_syndrome NULL
696*cfcc706cSMiquel Raynal #define mxc_nand_write_oob_syndrome NULL
697*cfcc706cSMiquel Raynal
698*cfcc706cSMiquel Raynal static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
699*cfcc706cSMiquel Raynal u_char *read_ecc, u_char *calc_ecc)
700*cfcc706cSMiquel Raynal {
701*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
702*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
703*cfcc706cSMiquel Raynal
704*cfcc706cSMiquel Raynal /*
705*cfcc706cSMiquel Raynal * 1-Bit errors are automatically corrected in HW. No need for
706*cfcc706cSMiquel Raynal * additional correction. 2-Bit errors cannot be corrected by
707*cfcc706cSMiquel Raynal * HW ECC, so we need to return failure
708*cfcc706cSMiquel Raynal */
709*cfcc706cSMiquel Raynal uint16_t ecc_status = readnfc(&host->regs->ecc_status_result);
710*cfcc706cSMiquel Raynal
711*cfcc706cSMiquel Raynal if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
712*cfcc706cSMiquel Raynal pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
713*cfcc706cSMiquel Raynal return -EBADMSG;
714*cfcc706cSMiquel Raynal }
715*cfcc706cSMiquel Raynal
716*cfcc706cSMiquel Raynal return 0;
717*cfcc706cSMiquel Raynal }
718*cfcc706cSMiquel Raynal #endif
719*cfcc706cSMiquel Raynal
720*cfcc706cSMiquel Raynal static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
721*cfcc706cSMiquel Raynal u_char *ecc_code)
722*cfcc706cSMiquel Raynal {
723*cfcc706cSMiquel Raynal return 0;
724*cfcc706cSMiquel Raynal }
725*cfcc706cSMiquel Raynal #endif
726*cfcc706cSMiquel Raynal
727*cfcc706cSMiquel Raynal static u_char mxc_nand_read_byte(struct mtd_info *mtd)
728*cfcc706cSMiquel Raynal {
729*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
730*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
731*cfcc706cSMiquel Raynal uint8_t ret = 0;
732*cfcc706cSMiquel Raynal uint16_t col;
733*cfcc706cSMiquel Raynal uint16_t __iomem *main_buf =
734*cfcc706cSMiquel Raynal (uint16_t __iomem *)host->regs->main_area[0];
735*cfcc706cSMiquel Raynal uint16_t __iomem *spare_buf =
736*cfcc706cSMiquel Raynal (uint16_t __iomem *)host->regs->spare_area[0];
737*cfcc706cSMiquel Raynal union {
738*cfcc706cSMiquel Raynal uint16_t word;
739*cfcc706cSMiquel Raynal uint8_t bytes[2];
740*cfcc706cSMiquel Raynal } nfc_word;
741*cfcc706cSMiquel Raynal
742*cfcc706cSMiquel Raynal /* Check for status request */
743*cfcc706cSMiquel Raynal if (host->status_request)
744*cfcc706cSMiquel Raynal return get_dev_status(host) & 0xFF;
745*cfcc706cSMiquel Raynal
746*cfcc706cSMiquel Raynal /* Get column for 16-bit access */
747*cfcc706cSMiquel Raynal col = host->col_addr >> 1;
748*cfcc706cSMiquel Raynal
749*cfcc706cSMiquel Raynal /* If we are accessing the spare region */
750*cfcc706cSMiquel Raynal if (host->spare_only)
751*cfcc706cSMiquel Raynal nfc_word.word = readw(&spare_buf[col]);
752*cfcc706cSMiquel Raynal else
753*cfcc706cSMiquel Raynal nfc_word.word = readw(&main_buf[col]);
754*cfcc706cSMiquel Raynal
755*cfcc706cSMiquel Raynal /* Pick upper/lower byte of word from RAM buffer */
756*cfcc706cSMiquel Raynal ret = nfc_word.bytes[host->col_addr & 0x1];
757*cfcc706cSMiquel Raynal
758*cfcc706cSMiquel Raynal /* Update saved column address */
759*cfcc706cSMiquel Raynal if (nand_chip->options & NAND_BUSWIDTH_16)
760*cfcc706cSMiquel Raynal host->col_addr += 2;
761*cfcc706cSMiquel Raynal else
762*cfcc706cSMiquel Raynal host->col_addr++;
763*cfcc706cSMiquel Raynal
764*cfcc706cSMiquel Raynal return ret;
765*cfcc706cSMiquel Raynal }
766*cfcc706cSMiquel Raynal
767*cfcc706cSMiquel Raynal static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
768*cfcc706cSMiquel Raynal {
769*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
770*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
771*cfcc706cSMiquel Raynal uint16_t col, ret;
772*cfcc706cSMiquel Raynal uint16_t __iomem *p;
773*cfcc706cSMiquel Raynal
774*cfcc706cSMiquel Raynal pr_debug("mxc_nand_read_word(col = %d)\n", host->col_addr);
775*cfcc706cSMiquel Raynal
776*cfcc706cSMiquel Raynal col = host->col_addr;
777*cfcc706cSMiquel Raynal /* Adjust saved column address */
778*cfcc706cSMiquel Raynal if (col < mtd->writesize && host->spare_only)
779*cfcc706cSMiquel Raynal col += mtd->writesize;
780*cfcc706cSMiquel Raynal
781*cfcc706cSMiquel Raynal if (col < mtd->writesize) {
782*cfcc706cSMiquel Raynal p = (uint16_t __iomem *)(host->regs->main_area[0] +
783*cfcc706cSMiquel Raynal (col >> 1));
784*cfcc706cSMiquel Raynal } else {
785*cfcc706cSMiquel Raynal p = (uint16_t __iomem *)(host->regs->spare_area[0] +
786*cfcc706cSMiquel Raynal ((col - mtd->writesize) >> 1));
787*cfcc706cSMiquel Raynal }
788*cfcc706cSMiquel Raynal
789*cfcc706cSMiquel Raynal if (col & 1) {
790*cfcc706cSMiquel Raynal union {
791*cfcc706cSMiquel Raynal uint16_t word;
792*cfcc706cSMiquel Raynal uint8_t bytes[2];
793*cfcc706cSMiquel Raynal } nfc_word[3];
794*cfcc706cSMiquel Raynal
795*cfcc706cSMiquel Raynal nfc_word[0].word = readw(p);
796*cfcc706cSMiquel Raynal nfc_word[1].word = readw(p + 1);
797*cfcc706cSMiquel Raynal
798*cfcc706cSMiquel Raynal nfc_word[2].bytes[0] = nfc_word[0].bytes[1];
799*cfcc706cSMiquel Raynal nfc_word[2].bytes[1] = nfc_word[1].bytes[0];
800*cfcc706cSMiquel Raynal
801*cfcc706cSMiquel Raynal ret = nfc_word[2].word;
802*cfcc706cSMiquel Raynal } else {
803*cfcc706cSMiquel Raynal ret = readw(p);
804*cfcc706cSMiquel Raynal }
805*cfcc706cSMiquel Raynal
806*cfcc706cSMiquel Raynal /* Update saved column address */
807*cfcc706cSMiquel Raynal host->col_addr = col + 2;
808*cfcc706cSMiquel Raynal
809*cfcc706cSMiquel Raynal return ret;
810*cfcc706cSMiquel Raynal }
811*cfcc706cSMiquel Raynal
812*cfcc706cSMiquel Raynal /*
813*cfcc706cSMiquel Raynal * Write data of length len to buffer buf. The data to be
814*cfcc706cSMiquel Raynal * written on NAND Flash is first copied to RAMbuffer. After the Data Input
815*cfcc706cSMiquel Raynal * Operation by the NFC, the data is written to NAND Flash
816*cfcc706cSMiquel Raynal */
817*cfcc706cSMiquel Raynal static void mxc_nand_write_buf(struct mtd_info *mtd,
818*cfcc706cSMiquel Raynal const u_char *buf, int len)
819*cfcc706cSMiquel Raynal {
820*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
821*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
822*cfcc706cSMiquel Raynal int n, col, i = 0;
823*cfcc706cSMiquel Raynal
824*cfcc706cSMiquel Raynal pr_debug("mxc_nand_write_buf(col = %d, len = %d)\n", host->col_addr,
825*cfcc706cSMiquel Raynal len);
826*cfcc706cSMiquel Raynal
827*cfcc706cSMiquel Raynal col = host->col_addr;
828*cfcc706cSMiquel Raynal
829*cfcc706cSMiquel Raynal /* Adjust saved column address */
830*cfcc706cSMiquel Raynal if (col < mtd->writesize && host->spare_only)
831*cfcc706cSMiquel Raynal col += mtd->writesize;
832*cfcc706cSMiquel Raynal
833*cfcc706cSMiquel Raynal n = mtd->writesize + mtd->oobsize - col;
834*cfcc706cSMiquel Raynal n = min(len, n);
835*cfcc706cSMiquel Raynal
836*cfcc706cSMiquel Raynal pr_debug("%s:%d: col = %d, n = %d\n", __func__, __LINE__, col, n);
837*cfcc706cSMiquel Raynal
838*cfcc706cSMiquel Raynal while (n > 0) {
839*cfcc706cSMiquel Raynal void __iomem *p;
840*cfcc706cSMiquel Raynal
841*cfcc706cSMiquel Raynal if (col < mtd->writesize) {
842*cfcc706cSMiquel Raynal p = host->regs->main_area[0] + (col & ~3);
843*cfcc706cSMiquel Raynal } else {
844*cfcc706cSMiquel Raynal p = host->regs->spare_area[0] -
845*cfcc706cSMiquel Raynal mtd->writesize + (col & ~3);
846*cfcc706cSMiquel Raynal }
847*cfcc706cSMiquel Raynal
848*cfcc706cSMiquel Raynal pr_debug("%s:%d: p = %p\n", __func__,
849*cfcc706cSMiquel Raynal __LINE__, p);
850*cfcc706cSMiquel Raynal
851*cfcc706cSMiquel Raynal if (((col | (unsigned long)&buf[i]) & 3) || n < 4) {
852*cfcc706cSMiquel Raynal union {
853*cfcc706cSMiquel Raynal uint32_t word;
854*cfcc706cSMiquel Raynal uint8_t bytes[4];
855*cfcc706cSMiquel Raynal } nfc_word;
856*cfcc706cSMiquel Raynal
857*cfcc706cSMiquel Raynal nfc_word.word = readl(p);
858*cfcc706cSMiquel Raynal nfc_word.bytes[col & 3] = buf[i++];
859*cfcc706cSMiquel Raynal n--;
860*cfcc706cSMiquel Raynal col++;
861*cfcc706cSMiquel Raynal
862*cfcc706cSMiquel Raynal writel(nfc_word.word, p);
863*cfcc706cSMiquel Raynal } else {
864*cfcc706cSMiquel Raynal int m = mtd->writesize - col;
865*cfcc706cSMiquel Raynal
866*cfcc706cSMiquel Raynal if (col >= mtd->writesize)
867*cfcc706cSMiquel Raynal m += mtd->oobsize;
868*cfcc706cSMiquel Raynal
869*cfcc706cSMiquel Raynal m = min(n, m) & ~3;
870*cfcc706cSMiquel Raynal
871*cfcc706cSMiquel Raynal pr_debug("%s:%d: n = %d, m = %d, i = %d, col = %d\n",
872*cfcc706cSMiquel Raynal __func__, __LINE__, n, m, i, col);
873*cfcc706cSMiquel Raynal
874*cfcc706cSMiquel Raynal mxc_nand_memcpy32(p, (uint32_t *)&buf[i], m);
875*cfcc706cSMiquel Raynal col += m;
876*cfcc706cSMiquel Raynal i += m;
877*cfcc706cSMiquel Raynal n -= m;
878*cfcc706cSMiquel Raynal }
879*cfcc706cSMiquel Raynal }
880*cfcc706cSMiquel Raynal /* Update saved column address */
881*cfcc706cSMiquel Raynal host->col_addr = col;
882*cfcc706cSMiquel Raynal }
883*cfcc706cSMiquel Raynal
884*cfcc706cSMiquel Raynal /*
885*cfcc706cSMiquel Raynal * Read the data buffer from the NAND Flash. To read the data from NAND
886*cfcc706cSMiquel Raynal * Flash first the data output cycle is initiated by the NFC, which copies
887*cfcc706cSMiquel Raynal * the data to RAMbuffer. This data of length len is then copied to buffer buf.
888*cfcc706cSMiquel Raynal */
889*cfcc706cSMiquel Raynal static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
890*cfcc706cSMiquel Raynal {
891*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
892*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
893*cfcc706cSMiquel Raynal int n, col, i = 0;
894*cfcc706cSMiquel Raynal
895*cfcc706cSMiquel Raynal pr_debug("mxc_nand_read_buf(col = %d, len = %d)\n", host->col_addr,
896*cfcc706cSMiquel Raynal len);
897*cfcc706cSMiquel Raynal
898*cfcc706cSMiquel Raynal col = host->col_addr;
899*cfcc706cSMiquel Raynal
900*cfcc706cSMiquel Raynal /* Adjust saved column address */
901*cfcc706cSMiquel Raynal if (col < mtd->writesize && host->spare_only)
902*cfcc706cSMiquel Raynal col += mtd->writesize;
903*cfcc706cSMiquel Raynal
904*cfcc706cSMiquel Raynal n = mtd->writesize + mtd->oobsize - col;
905*cfcc706cSMiquel Raynal n = min(len, n);
906*cfcc706cSMiquel Raynal
907*cfcc706cSMiquel Raynal while (n > 0) {
908*cfcc706cSMiquel Raynal void __iomem *p;
909*cfcc706cSMiquel Raynal
910*cfcc706cSMiquel Raynal if (col < mtd->writesize) {
911*cfcc706cSMiquel Raynal p = host->regs->main_area[0] + (col & ~3);
912*cfcc706cSMiquel Raynal } else {
913*cfcc706cSMiquel Raynal p = host->regs->spare_area[0] -
914*cfcc706cSMiquel Raynal mtd->writesize + (col & ~3);
915*cfcc706cSMiquel Raynal }
916*cfcc706cSMiquel Raynal
917*cfcc706cSMiquel Raynal if (((col | (int)&buf[i]) & 3) || n < 4) {
918*cfcc706cSMiquel Raynal union {
919*cfcc706cSMiquel Raynal uint32_t word;
920*cfcc706cSMiquel Raynal uint8_t bytes[4];
921*cfcc706cSMiquel Raynal } nfc_word;
922*cfcc706cSMiquel Raynal
923*cfcc706cSMiquel Raynal nfc_word.word = readl(p);
924*cfcc706cSMiquel Raynal buf[i++] = nfc_word.bytes[col & 3];
925*cfcc706cSMiquel Raynal n--;
926*cfcc706cSMiquel Raynal col++;
927*cfcc706cSMiquel Raynal } else {
928*cfcc706cSMiquel Raynal int m = mtd->writesize - col;
929*cfcc706cSMiquel Raynal
930*cfcc706cSMiquel Raynal if (col >= mtd->writesize)
931*cfcc706cSMiquel Raynal m += mtd->oobsize;
932*cfcc706cSMiquel Raynal
933*cfcc706cSMiquel Raynal m = min(n, m) & ~3;
934*cfcc706cSMiquel Raynal mxc_nand_memcpy32((uint32_t *)&buf[i], p, m);
935*cfcc706cSMiquel Raynal
936*cfcc706cSMiquel Raynal col += m;
937*cfcc706cSMiquel Raynal i += m;
938*cfcc706cSMiquel Raynal n -= m;
939*cfcc706cSMiquel Raynal }
940*cfcc706cSMiquel Raynal }
941*cfcc706cSMiquel Raynal /* Update saved column address */
942*cfcc706cSMiquel Raynal host->col_addr = col;
943*cfcc706cSMiquel Raynal }
944*cfcc706cSMiquel Raynal
945*cfcc706cSMiquel Raynal /*
946*cfcc706cSMiquel Raynal * This function is used by upper layer for select and
947*cfcc706cSMiquel Raynal * deselect of the NAND chip
948*cfcc706cSMiquel Raynal */
949*cfcc706cSMiquel Raynal static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
950*cfcc706cSMiquel Raynal {
951*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
952*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
953*cfcc706cSMiquel Raynal
954*cfcc706cSMiquel Raynal switch (chip) {
955*cfcc706cSMiquel Raynal case -1:
956*cfcc706cSMiquel Raynal /* TODO: Disable the NFC clock */
957*cfcc706cSMiquel Raynal if (host->clk_act)
958*cfcc706cSMiquel Raynal host->clk_act = 0;
959*cfcc706cSMiquel Raynal break;
960*cfcc706cSMiquel Raynal case 0:
961*cfcc706cSMiquel Raynal /* TODO: Enable the NFC clock */
962*cfcc706cSMiquel Raynal if (!host->clk_act)
963*cfcc706cSMiquel Raynal host->clk_act = 1;
964*cfcc706cSMiquel Raynal break;
965*cfcc706cSMiquel Raynal
966*cfcc706cSMiquel Raynal default:
967*cfcc706cSMiquel Raynal break;
968*cfcc706cSMiquel Raynal }
969*cfcc706cSMiquel Raynal }
970*cfcc706cSMiquel Raynal
971*cfcc706cSMiquel Raynal /*
972*cfcc706cSMiquel Raynal * Used by the upper layer to write command to NAND Flash for
973*cfcc706cSMiquel Raynal * different operations to be carried out on NAND Flash
974*cfcc706cSMiquel Raynal */
975*cfcc706cSMiquel Raynal void mxc_nand_command(struct mtd_info *mtd, unsigned command,
976*cfcc706cSMiquel Raynal int column, int page_addr)
977*cfcc706cSMiquel Raynal {
978*cfcc706cSMiquel Raynal struct nand_chip *nand_chip = mtd_to_nand(mtd);
979*cfcc706cSMiquel Raynal struct mxc_nand_host *host = nand_get_controller_data(nand_chip);
980*cfcc706cSMiquel Raynal
981*cfcc706cSMiquel Raynal pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
982*cfcc706cSMiquel Raynal command, column, page_addr);
983*cfcc706cSMiquel Raynal
984*cfcc706cSMiquel Raynal /* Reset command state information */
985*cfcc706cSMiquel Raynal host->status_request = false;
986*cfcc706cSMiquel Raynal
987*cfcc706cSMiquel Raynal /* Command pre-processing step */
988*cfcc706cSMiquel Raynal switch (command) {
989*cfcc706cSMiquel Raynal
990*cfcc706cSMiquel Raynal case NAND_CMD_STATUS:
991*cfcc706cSMiquel Raynal host->col_addr = 0;
992*cfcc706cSMiquel Raynal host->status_request = true;
993*cfcc706cSMiquel Raynal break;
994*cfcc706cSMiquel Raynal
995*cfcc706cSMiquel Raynal case NAND_CMD_READ0:
996*cfcc706cSMiquel Raynal host->page_addr = page_addr;
997*cfcc706cSMiquel Raynal host->col_addr = column;
998*cfcc706cSMiquel Raynal host->spare_only = false;
999*cfcc706cSMiquel Raynal break;
1000*cfcc706cSMiquel Raynal
1001*cfcc706cSMiquel Raynal case NAND_CMD_READOOB:
1002*cfcc706cSMiquel Raynal host->col_addr = column;
1003*cfcc706cSMiquel Raynal host->spare_only = true;
1004*cfcc706cSMiquel Raynal if (host->pagesize_2k)
1005*cfcc706cSMiquel Raynal command = NAND_CMD_READ0; /* only READ0 is valid */
1006*cfcc706cSMiquel Raynal break;
1007*cfcc706cSMiquel Raynal
1008*cfcc706cSMiquel Raynal case NAND_CMD_SEQIN:
1009*cfcc706cSMiquel Raynal if (column >= mtd->writesize) {
1010*cfcc706cSMiquel Raynal /*
1011*cfcc706cSMiquel Raynal * before sending SEQIN command for partial write,
1012*cfcc706cSMiquel Raynal * we need read one page out. FSL NFC does not support
1013*cfcc706cSMiquel Raynal * partial write. It always sends out 512+ecc+512+ecc
1014*cfcc706cSMiquel Raynal * for large page nand flash. But for small page nand
1015*cfcc706cSMiquel Raynal * flash, it does support SPARE ONLY operation.
1016*cfcc706cSMiquel Raynal */
1017*cfcc706cSMiquel Raynal if (host->pagesize_2k) {
1018*cfcc706cSMiquel Raynal /* call ourself to read a page */
1019*cfcc706cSMiquel Raynal mxc_nand_command(mtd, NAND_CMD_READ0, 0,
1020*cfcc706cSMiquel Raynal page_addr);
1021*cfcc706cSMiquel Raynal }
1022*cfcc706cSMiquel Raynal
1023*cfcc706cSMiquel Raynal host->col_addr = column - mtd->writesize;
1024*cfcc706cSMiquel Raynal host->spare_only = true;
1025*cfcc706cSMiquel Raynal
1026*cfcc706cSMiquel Raynal /* Set program pointer to spare region */
1027*cfcc706cSMiquel Raynal if (!host->pagesize_2k)
1028*cfcc706cSMiquel Raynal send_cmd(host, NAND_CMD_READOOB);
1029*cfcc706cSMiquel Raynal } else {
1030*cfcc706cSMiquel Raynal host->spare_only = false;
1031*cfcc706cSMiquel Raynal host->col_addr = column;
1032*cfcc706cSMiquel Raynal
1033*cfcc706cSMiquel Raynal /* Set program pointer to page start */
1034*cfcc706cSMiquel Raynal if (!host->pagesize_2k)
1035*cfcc706cSMiquel Raynal send_cmd(host, NAND_CMD_READ0);
1036*cfcc706cSMiquel Raynal }
1037*cfcc706cSMiquel Raynal break;
1038*cfcc706cSMiquel Raynal
1039*cfcc706cSMiquel Raynal case NAND_CMD_PAGEPROG:
1040*cfcc706cSMiquel Raynal send_prog_page(host, 0, host->spare_only);
1041*cfcc706cSMiquel Raynal
1042*cfcc706cSMiquel Raynal if (host->pagesize_2k && is_mxc_nfc_1()) {
1043*cfcc706cSMiquel Raynal /* data in 4 areas */
1044*cfcc706cSMiquel Raynal send_prog_page(host, 1, host->spare_only);
1045*cfcc706cSMiquel Raynal send_prog_page(host, 2, host->spare_only);
1046*cfcc706cSMiquel Raynal send_prog_page(host, 3, host->spare_only);
1047*cfcc706cSMiquel Raynal }
1048*cfcc706cSMiquel Raynal
1049*cfcc706cSMiquel Raynal break;
1050*cfcc706cSMiquel Raynal }
1051*cfcc706cSMiquel Raynal
1052*cfcc706cSMiquel Raynal /* Write out the command to the device. */
1053*cfcc706cSMiquel Raynal send_cmd(host, command);
1054*cfcc706cSMiquel Raynal
1055*cfcc706cSMiquel Raynal /* Write out column address, if necessary */
1056*cfcc706cSMiquel Raynal if (column != -1) {
1057*cfcc706cSMiquel Raynal /*
1058*cfcc706cSMiquel Raynal * MXC NANDFC can only perform full page+spare or
1059*cfcc706cSMiquel Raynal * spare-only read/write. When the upper layers perform
1060*cfcc706cSMiquel Raynal * a read/write buffer operation, we will use the saved
1061*cfcc706cSMiquel Raynal * column address to index into the full page.
1062*cfcc706cSMiquel Raynal */
1063*cfcc706cSMiquel Raynal send_addr(host, 0);
1064*cfcc706cSMiquel Raynal if (host->pagesize_2k)
1065*cfcc706cSMiquel Raynal /* another col addr cycle for 2k page */
1066*cfcc706cSMiquel Raynal send_addr(host, 0);
1067*cfcc706cSMiquel Raynal }
1068*cfcc706cSMiquel Raynal
1069*cfcc706cSMiquel Raynal /* Write out page address, if necessary */
1070*cfcc706cSMiquel Raynal if (page_addr != -1) {
1071*cfcc706cSMiquel Raynal u32 page_mask = nand_chip->pagemask;
1072*cfcc706cSMiquel Raynal do {
1073*cfcc706cSMiquel Raynal send_addr(host, page_addr & 0xFF);
1074*cfcc706cSMiquel Raynal page_addr >>= 8;
1075*cfcc706cSMiquel Raynal page_mask >>= 8;
1076*cfcc706cSMiquel Raynal } while (page_mask);
1077*cfcc706cSMiquel Raynal }
1078*cfcc706cSMiquel Raynal
1079*cfcc706cSMiquel Raynal /* Command post-processing step */
1080*cfcc706cSMiquel Raynal switch (command) {
1081*cfcc706cSMiquel Raynal
1082*cfcc706cSMiquel Raynal case NAND_CMD_RESET:
1083*cfcc706cSMiquel Raynal break;
1084*cfcc706cSMiquel Raynal
1085*cfcc706cSMiquel Raynal case NAND_CMD_READOOB:
1086*cfcc706cSMiquel Raynal case NAND_CMD_READ0:
1087*cfcc706cSMiquel Raynal if (host->pagesize_2k) {
1088*cfcc706cSMiquel Raynal /* send read confirm command */
1089*cfcc706cSMiquel Raynal send_cmd(host, NAND_CMD_READSTART);
1090*cfcc706cSMiquel Raynal /* read for each AREA */
1091*cfcc706cSMiquel Raynal send_read_page(host, 0, host->spare_only);
1092*cfcc706cSMiquel Raynal if (is_mxc_nfc_1()) {
1093*cfcc706cSMiquel Raynal send_read_page(host, 1, host->spare_only);
1094*cfcc706cSMiquel Raynal send_read_page(host, 2, host->spare_only);
1095*cfcc706cSMiquel Raynal send_read_page(host, 3, host->spare_only);
1096*cfcc706cSMiquel Raynal }
1097*cfcc706cSMiquel Raynal } else {
1098*cfcc706cSMiquel Raynal send_read_page(host, 0, host->spare_only);
1099*cfcc706cSMiquel Raynal }
1100*cfcc706cSMiquel Raynal break;
1101*cfcc706cSMiquel Raynal
1102*cfcc706cSMiquel Raynal case NAND_CMD_READID:
1103*cfcc706cSMiquel Raynal host->col_addr = 0;
1104*cfcc706cSMiquel Raynal send_read_id(host);
1105*cfcc706cSMiquel Raynal break;
1106*cfcc706cSMiquel Raynal
1107*cfcc706cSMiquel Raynal case NAND_CMD_PAGEPROG:
1108*cfcc706cSMiquel Raynal break;
1109*cfcc706cSMiquel Raynal
1110*cfcc706cSMiquel Raynal case NAND_CMD_STATUS:
1111*cfcc706cSMiquel Raynal break;
1112*cfcc706cSMiquel Raynal
1113*cfcc706cSMiquel Raynal case NAND_CMD_ERASE2:
1114*cfcc706cSMiquel Raynal break;
1115*cfcc706cSMiquel Raynal }
1116*cfcc706cSMiquel Raynal }
1117*cfcc706cSMiquel Raynal
1118*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1119*cfcc706cSMiquel Raynal
1120*cfcc706cSMiquel Raynal static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
1121*cfcc706cSMiquel Raynal static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
1122*cfcc706cSMiquel Raynal
1123*cfcc706cSMiquel Raynal static struct nand_bbt_descr bbt_main_descr = {
1124*cfcc706cSMiquel Raynal .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1125*cfcc706cSMiquel Raynal NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1126*cfcc706cSMiquel Raynal .offs = 0,
1127*cfcc706cSMiquel Raynal .len = 4,
1128*cfcc706cSMiquel Raynal .veroffs = 4,
1129*cfcc706cSMiquel Raynal .maxblocks = 4,
1130*cfcc706cSMiquel Raynal .pattern = bbt_pattern,
1131*cfcc706cSMiquel Raynal };
1132*cfcc706cSMiquel Raynal
1133*cfcc706cSMiquel Raynal static struct nand_bbt_descr bbt_mirror_descr = {
1134*cfcc706cSMiquel Raynal .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
1135*cfcc706cSMiquel Raynal NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1136*cfcc706cSMiquel Raynal .offs = 0,
1137*cfcc706cSMiquel Raynal .len = 4,
1138*cfcc706cSMiquel Raynal .veroffs = 4,
1139*cfcc706cSMiquel Raynal .maxblocks = 4,
1140*cfcc706cSMiquel Raynal .pattern = mirror_pattern,
1141*cfcc706cSMiquel Raynal };
1142*cfcc706cSMiquel Raynal
1143*cfcc706cSMiquel Raynal #endif
1144*cfcc706cSMiquel Raynal
1145*cfcc706cSMiquel Raynal int board_nand_init(struct nand_chip *this)
1146*cfcc706cSMiquel Raynal {
1147*cfcc706cSMiquel Raynal struct mtd_info *mtd;
1148*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
1149*cfcc706cSMiquel Raynal uint32_t tmp;
1150*cfcc706cSMiquel Raynal #endif
1151*cfcc706cSMiquel Raynal
1152*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1153*cfcc706cSMiquel Raynal this->bbt_options |= NAND_BBT_USE_FLASH;
1154*cfcc706cSMiquel Raynal this->bbt_td = &bbt_main_descr;
1155*cfcc706cSMiquel Raynal this->bbt_md = &bbt_mirror_descr;
1156*cfcc706cSMiquel Raynal #endif
1157*cfcc706cSMiquel Raynal
1158*cfcc706cSMiquel Raynal /* structures must be linked */
1159*cfcc706cSMiquel Raynal mtd = &this->mtd;
1160*cfcc706cSMiquel Raynal host->nand = this;
1161*cfcc706cSMiquel Raynal
1162*cfcc706cSMiquel Raynal /* 5 us command delay time */
1163*cfcc706cSMiquel Raynal this->chip_delay = 5;
1164*cfcc706cSMiquel Raynal
1165*cfcc706cSMiquel Raynal nand_set_controller_data(this, host);
1166*cfcc706cSMiquel Raynal this->dev_ready = mxc_nand_dev_ready;
1167*cfcc706cSMiquel Raynal this->cmdfunc = mxc_nand_command;
1168*cfcc706cSMiquel Raynal this->select_chip = mxc_nand_select_chip;
1169*cfcc706cSMiquel Raynal this->read_byte = mxc_nand_read_byte;
1170*cfcc706cSMiquel Raynal this->read_word = mxc_nand_read_word;
1171*cfcc706cSMiquel Raynal this->write_buf = mxc_nand_write_buf;
1172*cfcc706cSMiquel Raynal this->read_buf = mxc_nand_read_buf;
1173*cfcc706cSMiquel Raynal
1174*cfcc706cSMiquel Raynal host->regs = (struct mxc_nand_regs __iomem *)CONFIG_MXC_NAND_REGS_BASE;
1175*cfcc706cSMiquel Raynal #ifdef MXC_NFC_V3_2
1176*cfcc706cSMiquel Raynal host->ip_regs =
1177*cfcc706cSMiquel Raynal (struct mxc_nand_ip_regs __iomem *)CONFIG_MXC_NAND_IP_REGS_BASE;
1178*cfcc706cSMiquel Raynal #endif
1179*cfcc706cSMiquel Raynal host->clk_act = 1;
1180*cfcc706cSMiquel Raynal
1181*cfcc706cSMiquel Raynal #ifdef CONFIG_MXC_NAND_HWECC
1182*cfcc706cSMiquel Raynal this->ecc.calculate = mxc_nand_calculate_ecc;
1183*cfcc706cSMiquel Raynal this->ecc.hwctl = mxc_nand_enable_hwecc;
1184*cfcc706cSMiquel Raynal this->ecc.correct = mxc_nand_correct_data;
1185*cfcc706cSMiquel Raynal if (is_mxc_nfc_21() || is_mxc_nfc_32()) {
1186*cfcc706cSMiquel Raynal this->ecc.mode = NAND_ECC_HW_SYNDROME;
1187*cfcc706cSMiquel Raynal this->ecc.read_page = mxc_nand_read_page_syndrome;
1188*cfcc706cSMiquel Raynal this->ecc.read_page_raw = mxc_nand_read_page_raw_syndrome;
1189*cfcc706cSMiquel Raynal this->ecc.read_oob = mxc_nand_read_oob_syndrome;
1190*cfcc706cSMiquel Raynal this->ecc.write_page = mxc_nand_write_page_syndrome;
1191*cfcc706cSMiquel Raynal this->ecc.write_page_raw = mxc_nand_write_page_raw_syndrome;
1192*cfcc706cSMiquel Raynal this->ecc.write_oob = mxc_nand_write_oob_syndrome;
1193*cfcc706cSMiquel Raynal this->ecc.bytes = 9;
1194*cfcc706cSMiquel Raynal this->ecc.prepad = 7;
1195*cfcc706cSMiquel Raynal } else {
1196*cfcc706cSMiquel Raynal this->ecc.mode = NAND_ECC_HW;
1197*cfcc706cSMiquel Raynal }
1198*cfcc706cSMiquel Raynal
1199*cfcc706cSMiquel Raynal if (is_mxc_nfc_1())
1200*cfcc706cSMiquel Raynal this->ecc.strength = 1;
1201*cfcc706cSMiquel Raynal else
1202*cfcc706cSMiquel Raynal this->ecc.strength = 4;
1203*cfcc706cSMiquel Raynal
1204*cfcc706cSMiquel Raynal host->pagesize_2k = 0;
1205*cfcc706cSMiquel Raynal
1206*cfcc706cSMiquel Raynal this->ecc.size = 512;
1207*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 1);
1208*cfcc706cSMiquel Raynal #else
1209*cfcc706cSMiquel Raynal this->ecc.layout = &nand_soft_eccoob;
1210*cfcc706cSMiquel Raynal this->ecc.mode = NAND_ECC_SOFT;
1211*cfcc706cSMiquel Raynal _mxc_nand_enable_hwecc(mtd, 0);
1212*cfcc706cSMiquel Raynal #endif
1213*cfcc706cSMiquel Raynal /* Reset NAND */
1214*cfcc706cSMiquel Raynal this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1215*cfcc706cSMiquel Raynal
1216*cfcc706cSMiquel Raynal /* NAND bus width determines access functions used by upper layer */
1217*cfcc706cSMiquel Raynal if (is_16bit_nand())
1218*cfcc706cSMiquel Raynal this->options |= NAND_BUSWIDTH_16;
1219*cfcc706cSMiquel Raynal
1220*cfcc706cSMiquel Raynal #ifdef CONFIG_SYS_NAND_LARGEPAGE
1221*cfcc706cSMiquel Raynal host->pagesize_2k = 1;
1222*cfcc706cSMiquel Raynal this->ecc.layout = &nand_hw_eccoob2k;
1223*cfcc706cSMiquel Raynal #else
1224*cfcc706cSMiquel Raynal host->pagesize_2k = 0;
1225*cfcc706cSMiquel Raynal this->ecc.layout = &nand_hw_eccoob;
1226*cfcc706cSMiquel Raynal #endif
1227*cfcc706cSMiquel Raynal
1228*cfcc706cSMiquel Raynal #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
1229*cfcc706cSMiquel Raynal #ifdef MXC_NFC_V2_1
1230*cfcc706cSMiquel Raynal tmp = readnfc(&host->regs->config1);
1231*cfcc706cSMiquel Raynal tmp |= NFC_V2_CONFIG1_ONE_CYCLE;
1232*cfcc706cSMiquel Raynal tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
1233*cfcc706cSMiquel Raynal writenfc(tmp, &host->regs->config1);
1234*cfcc706cSMiquel Raynal if (host->pagesize_2k)
1235*cfcc706cSMiquel Raynal writenfc(64/2, &host->regs->spare_area_size);
1236*cfcc706cSMiquel Raynal else
1237*cfcc706cSMiquel Raynal writenfc(16/2, &host->regs->spare_area_size);
1238*cfcc706cSMiquel Raynal #endif
1239*cfcc706cSMiquel Raynal
1240*cfcc706cSMiquel Raynal /*
1241*cfcc706cSMiquel Raynal * preset operation
1242*cfcc706cSMiquel Raynal * Unlock the internal RAM Buffer
1243*cfcc706cSMiquel Raynal */
1244*cfcc706cSMiquel Raynal writenfc(0x2, &host->regs->config);
1245*cfcc706cSMiquel Raynal
1246*cfcc706cSMiquel Raynal /* Blocks to be unlocked */
1247*cfcc706cSMiquel Raynal writenfc(0x0, &host->regs->unlockstart_blkaddr);
1248*cfcc706cSMiquel Raynal /* Originally (Freescale LTIB 2.6.21) 0x4000 was written to the
1249*cfcc706cSMiquel Raynal * unlockend_blkaddr, but the magic 0x4000 does not always work
1250*cfcc706cSMiquel Raynal * when writing more than some 32 megabytes (on 2k page nands)
1251*cfcc706cSMiquel Raynal * However 0xFFFF doesn't seem to have this kind
1252*cfcc706cSMiquel Raynal * of limitation (tried it back and forth several times).
1253*cfcc706cSMiquel Raynal * The linux kernel driver sets this to 0xFFFF for the v2 controller
1254*cfcc706cSMiquel Raynal * only, but probably this was not tested there for v1.
1255*cfcc706cSMiquel Raynal * The very same limitation seems to apply to this kernel driver.
1256*cfcc706cSMiquel Raynal * This might be NAND chip specific and the i.MX31 datasheet is
1257*cfcc706cSMiquel Raynal * extremely vague about the semantics of this register.
1258*cfcc706cSMiquel Raynal */
1259*cfcc706cSMiquel Raynal writenfc(0xFFFF, &host->regs->unlockend_blkaddr);
1260*cfcc706cSMiquel Raynal
1261*cfcc706cSMiquel Raynal /* Unlock Block Command for given address range */
1262*cfcc706cSMiquel Raynal writenfc(0x4, &host->regs->wrprot);
1263*cfcc706cSMiquel Raynal #elif defined(MXC_NFC_V3_2)
1264*cfcc706cSMiquel Raynal writenfc(NFC_V3_CONFIG1_RBA(0), &host->regs->config1);
1265*cfcc706cSMiquel Raynal writenfc(NFC_V3_IPC_CREQ, &host->ip_regs->ipc);
1266*cfcc706cSMiquel Raynal
1267*cfcc706cSMiquel Raynal /* Unlock the internal RAM Buffer */
1268*cfcc706cSMiquel Raynal writenfc(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
1269*cfcc706cSMiquel Raynal &host->ip_regs->wrprot);
1270*cfcc706cSMiquel Raynal
1271*cfcc706cSMiquel Raynal /* Blocks to be unlocked */
1272*cfcc706cSMiquel Raynal for (tmp = 0; tmp < CONFIG_SYS_NAND_MAX_CHIPS; tmp++)
1273*cfcc706cSMiquel Raynal writenfc(0x0 | 0xFFFF << 16,
1274*cfcc706cSMiquel Raynal &host->ip_regs->wrprot_unlock_blkaddr[tmp]);
1275*cfcc706cSMiquel Raynal
1276*cfcc706cSMiquel Raynal writenfc(0, &host->ip_regs->ipc);
1277*cfcc706cSMiquel Raynal
1278*cfcc706cSMiquel Raynal tmp = readnfc(&host->ip_regs->config2);
1279*cfcc706cSMiquel Raynal tmp &= ~(NFC_V3_CONFIG2_SPAS_MASK | NFC_V3_CONFIG2_EDC_MASK |
1280*cfcc706cSMiquel Raynal NFC_V3_CONFIG2_ECC_MODE_8 | NFC_V3_CONFIG2_PS_MASK);
1281*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG2_ONE_CYCLE;
1282*cfcc706cSMiquel Raynal
1283*cfcc706cSMiquel Raynal if (host->pagesize_2k) {
1284*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG2_SPAS(64/2);
1285*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG2_PS_2048;
1286*cfcc706cSMiquel Raynal } else {
1287*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG2_SPAS(16/2);
1288*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG2_PS_512;
1289*cfcc706cSMiquel Raynal }
1290*cfcc706cSMiquel Raynal
1291*cfcc706cSMiquel Raynal writenfc(tmp, &host->ip_regs->config2);
1292*cfcc706cSMiquel Raynal
1293*cfcc706cSMiquel Raynal tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
1294*cfcc706cSMiquel Raynal NFC_V3_CONFIG3_NO_SDMA |
1295*cfcc706cSMiquel Raynal NFC_V3_CONFIG3_RBB_MODE |
1296*cfcc706cSMiquel Raynal NFC_V3_CONFIG3_SBB(6) | /* Reset default */
1297*cfcc706cSMiquel Raynal NFC_V3_CONFIG3_ADD_OP(0);
1298*cfcc706cSMiquel Raynal
1299*cfcc706cSMiquel Raynal if (!(this->options & NAND_BUSWIDTH_16))
1300*cfcc706cSMiquel Raynal tmp |= NFC_V3_CONFIG3_FW8;
1301*cfcc706cSMiquel Raynal
1302*cfcc706cSMiquel Raynal writenfc(tmp, &host->ip_regs->config3);
1303*cfcc706cSMiquel Raynal
1304*cfcc706cSMiquel Raynal writenfc(0, &host->ip_regs->delay_line);
1305*cfcc706cSMiquel Raynal #endif
1306*cfcc706cSMiquel Raynal
1307*cfcc706cSMiquel Raynal return 0;
1308*cfcc706cSMiquel Raynal }
1309