]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/mxc_nand.c
sunxi_nand_spl: Remove NAND_SUNXI_SPL_SYNDROME_PARTITIONS_END
[karo-tx-uboot.git] / drivers / mtd / nand / mxc_nand.c
1 /*
2  * Copyright 2004-2007 Freescale Semiconductor, Inc.
3  * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4  * Copyright 2009 Ilya Yanok, <yanok@emcraft.com>
5  *
6  * SPDX-License-Identifier:     GPL-2.0+
7  */
8
9 #include <common.h>
10 #include <malloc.h>
11 #include <nand.h>
12 #include <linux/err.h>
13 #include <asm/io.h>
14 #if defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX35) || \
15         defined(CONFIG_SOC_MX51) || defined(CONFIG_SOC_MX53)
16 #include <asm/arch/imx-regs.h>
17 #endif
18
19 static struct mxc_nand_host mxc_host;
20 static struct mxc_nand_host *host = &mxc_host;
21
22 #ifdef CONFIG_SOC_MX27
23 static int is_16bit_nand(void)
24 {
25         struct system_control_regs *sc_regs =
26                 (struct system_control_regs *)IMX_SYSTEM_CTL_BASE;
27
28         if (readl(&sc_regs->fmcr) & NF_16BIT_SEL)
29                 return 1;
30         else
31                 return 0;
32 }
33 #elif defined(CONFIG_SOC_MX31)
34 static int is_16bit_nand(void)
35 {
36         struct clock_control_regs *sc_regs =
37                 (struct clock_control_regs *)CCM_BASE;
38
39         if (readl(&sc_regs->rcsr) & CCM_RCSR_NF16B)
40                 return 1;
41         else
42                 return 0;
43 }
44 #elif defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX35)
45 static int is_16bit_nand(void)
46 {
47         struct ccm_regs *ccm =
48                 (struct ccm_regs *)IMX_CCM_BASE;
49
50         if (readl(&ccm->rcsr) & CCM_RCSR_NF_16BIT_SEL)
51                 return 1;
52         else
53                 return 0;
54 }
55 #elif defined(CONFIG_SOC_MX51)
56 static int is_16bit_nand(void)
57 {
58         struct src *src = (struct src *)SRC_BASE_ADDR;
59
60         if (readl(&src->sbmr) & (1 << 2))
61                 return 1;
62         else
63                 return 0;
64 }
65 #elif defined(CONFIG_SOC_MX53)
66 /* BOOT_CFG[1..3][0..7] */
67 #define SRC_BOOT_CFG(m, n)              (1 << ((m) * 8 + (n)))
68 static int is_16bit_nand(void)
69 {
70         struct src *src = (struct src *)SRC_BASE_ADDR;
71
72         if (readl(&src->sbmr) & SRC_BOOT_CFG(2, 5))
73                 return 1;
74         else
75                 return 0;
76 }
77 #else
78 #warning "8/16 bit NAND autodetection not supported"
79 static int is_16bit_nand(void)
80 {
81         return 0;
82 }
83 #endif
84
85 #define MXC_NAND_TIMEOUT        (1 * HZ)
86
87 #define DRIVER_NAME "mxc_nand"
88
89 #ifndef CONFIG_MXC_NAND_REGS_BASE
90 #error CONFIG_MXC_NAND_REGS_BASE not defined
91 #endif
92
93 #if defined(CONFIG_SOC_MX27) || defined(CONFIG_SOC_MX31)
94 #define nfc_is_v1()             1
95 #define nfc_is_v21()            0
96 #define nfc_is_v3_2()           0
97 #define nfc_is_v3()             nfc_is_v3_2()
98 #define NFC_VERSION             "V1"
99 #elif defined(CONFIG_SOC_MX25) || defined(CONFIG_SOC_MX35)
100 #define nfc_is_v1()             0
101 #define nfc_is_v21()            1
102 #define nfc_is_v3_2()           0
103 #define nfc_is_v3()             nfc_is_v3_2()
104 #define NFC_VERSION             "V2"
105 #elif defined(CONFIG_SOC_MX51) || defined(CONFIG_SOC_MX53)
106 #define nfc_is_v1()             0
107 #define nfc_is_v21()            0
108 #define nfc_is_v3_2()           1
109 #define nfc_is_v3()             nfc_is_v3_2()
110 #define NFC_VERSION             "V3"
111 #ifndef CONFIG_MXC_NAND_IP_REGS_BASE
112 #error CONFIG_MXC_NAND_IP_REGS_BASE not defined
113 #endif
114 #else
115 #error mxc_nand driver not supported on this platform
116 #define NFC_VERSION             "unknown"
117 #endif
118
119 /* Addresses for NFC registers */
120 #define NFC_V1_V2_BUF_SIZE              (host->regs + 0x00)
121 #define NFC_V1_V2_BUF_ADDR              (host->regs + 0x04)
122 #define NFC_V1_V2_FLASH_ADDR            (host->regs + 0x06)
123 #define NFC_V1_V2_FLASH_CMD             (host->regs + 0x08)
124 #define NFC_V1_V2_CONFIG                (host->regs + 0x0a)
125 #define NFC_V1_V2_ECC_STATUS_RESULT     (host->regs + 0x0c)
126 #define NFC_V1_V2_RSLTMAIN_AREA         (host->regs + 0x0e)
127 #define NFC_V1_V2_RSLTSPARE_AREA        (host->regs + 0x10)
128 #define NFC_V1_V2_WRPROT                (host->regs + 0x12)
129 #define NFC_V1_UNLOCKSTART_BLKADDR      (host->regs + 0x14)
130 #define NFC_V1_UNLOCKEND_BLKADDR        (host->regs + 0x16)
131 #define NFC_V21_UNLOCKSTART_BLKADDR0    (host->regs + 0x20)
132 #define NFC_V21_UNLOCKSTART_BLKADDR1    (host->regs + 0x24)
133 #define NFC_V21_UNLOCKSTART_BLKADDR2    (host->regs + 0x28)
134 #define NFC_V21_UNLOCKSTART_BLKADDR3    (host->regs + 0x2c)
135 #define NFC_V21_UNLOCKEND_BLKADDR0      (host->regs + 0x22)
136 #define NFC_V21_UNLOCKEND_BLKADDR1      (host->regs + 0x26)
137 #define NFC_V21_UNLOCKEND_BLKADDR2      (host->regs + 0x2a)
138 #define NFC_V21_UNLOCKEND_BLKADDR3      (host->regs + 0x2e)
139 #define NFC_V1_V2_NF_WRPRST             (host->regs + 0x18)
140 #define NFC_V1_V2_CONFIG1               (host->regs + 0x1a)
141 #define NFC_V1_V2_CONFIG2               (host->regs + 0x1c)
142
143 #define NFC_V2_CONFIG1_ECC_MODE_4       (1 << 0)
144 #define NFC_V1_V2_CONFIG1_SP_EN         (1 << 2)
145 #define NFC_V1_V2_CONFIG1_ECC_EN        (1 << 3)
146 #define NFC_V1_V2_CONFIG1_INT_MSK       (1 << 4)
147 #define NFC_V1_V2_CONFIG1_BIG           (1 << 5)
148 #define NFC_V1_V2_CONFIG1_RST           (1 << 6)
149 #define NFC_V1_V2_CONFIG1_CE            (1 << 7)
150 #define NFC_V2_CONFIG1_ONE_CYCLE        (1 << 8)
151 #define NFC_V2_CONFIG1_PPB(x)           (((x) & 0x3) << 9)
152 #define NFC_V2_CONFIG1_FP_INT           (1 << 11)
153
154 #define NFC_V1_V2_CONFIG2_INT           (1 << 15)
155
156 /*
157  * Operation modes for the NFC. Valid for v1, v2 and v3
158  * type controllers.
159  */
160 #define NFC_CMD                         (1 << 0)
161 #define NFC_ADDR                        (1 << 1)
162 #define NFC_INPUT                       (1 << 2)
163 #define NFC_OUTPUT                      (1 << 3)
164 #define NFC_ID                          (1 << 4)
165 #define NFC_STATUS                      (1 << 5)
166
167 #define NFC_V3_FLASH_CMD                (host->regs_axi + 0x00)
168 #define NFC_V3_FLASH_ADDR0              (host->regs_axi + 0x04)
169
170 #define NFC_V3_CONFIG1                  (host->regs_axi + 0x34)
171 #define NFC_V3_CONFIG1_SP_EN            (1 << 0)
172 #define NFC_V3_CONFIG1_RBA(x)           (((x) & 0x7 ) << 4)
173
174 #define NFC_V3_ECC_STATUS_RESULT        (host->regs_axi + 0x38)
175
176 #define NFC_V3_LAUNCH                   (host->regs_axi + 0x40)
177
178 #define NFC_V3_WRPROT                   (host->regs_ip + 0x0)
179 #define NFC_V3_WRPROT_LOCK_TIGHT        (1 << 0)
180 #define NFC_V3_WRPROT_LOCK              (1 << 1)
181 #define NFC_V3_WRPROT_UNLOCK            (1 << 2)
182 #define NFC_V3_WRPROT_BLS_UNLOCK        (2 << 6)
183
184 #define NFC_V3_WRPROT_UNLOCK_BLK_ADD0   (host->regs_ip + 0x04)
185
186 #define NFC_V3_CONFIG2                  (host->regs_ip + 0x24)
187 #define NFC_V3_CONFIG2_PS_512                   (0 << 0)
188 #define NFC_V3_CONFIG2_PS_2048                  (1 << 0)
189 #define NFC_V3_CONFIG2_PS_4096                  (2 << 0)
190 #define NFC_V3_CONFIG2_ONE_CYCLE                (1 << 2)
191 #define NFC_V3_CONFIG2_ECC_EN                   (1 << 3)
192 #define NFC_V3_CONFIG2_2CMD_PHASES              (1 << 4)
193 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE0          (1 << 5)
194 #define NFC_V3_CONFIG2_ECC_MODE_8               (1 << 6)
195 #define NFC_V3_CONFIG2_PPB(x)                   (((x) & 0x3) << 7)
196 #define MX53_CONFIG2_PPB(x)                     (((x) & 0x3) << 8)
197 #define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x)       (((x) & 0x3) << 12)
198 #define NFC_V3_CONFIG2_INT_MSK                  (1 << 15)
199 #define NFC_V3_CONFIG2_ST_CMD(x)                (((x) & 0xff) << 24)
200 #define NFC_V3_CONFIG2_SPAS(x)                  (((x) & 0xff) << 16)
201
202 #define NFC_V3_CONFIG3                          (host->regs_ip + 0x28)
203 #define NFC_V3_CONFIG3_ADD_OP(x)                (((x) & 0x3) << 0)
204 #define NFC_V3_CONFIG3_FW8                      (1 << 3)
205 #define NFC_V3_CONFIG3_SBB(x)                   (((x) & 0x7) << 8)
206 #define NFC_V3_CONFIG3_NUM_OF_DEVICES(x)        (((x) & 0x7) << 12)
207 #define NFC_V3_CONFIG3_RBB_MODE                 (1 << 15)
208 #define NFC_V3_CONFIG3_NO_SDMA                  (1 << 20)
209
210 #define NFC_V3_IPC                      (host->regs_ip + 0x2C)
211 #define NFC_V3_IPC_CREQ                 (1 << 0)
212 #define NFC_V3_IPC_CACK                 (1 << 1)
213 #define NFC_V3_IPC_INT                  (1 << 31)
214
215 #define NFC_V3_DELAY_LINE               (host->regs_ip + 0x34)
216
217 struct mxc_nand_host {
218         struct mtd_info         mtd;
219         struct nand_chip        nand;
220
221         void                    *spare0;
222         void                    *main_area0;
223
224         void __iomem            *base;
225         void __iomem            *regs;
226         void __iomem            *regs_axi;
227         void __iomem            *regs_ip;
228         int                     status_request;
229         int                     eccsize;
230         int                     active_cs;
231
232         uint8_t                 *data_buf;
233         unsigned int            buf_start;
234         int                     spare_len;
235
236         void                    (*preset)(struct mtd_info *);
237         void                    (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
238         void                    (*send_addr)(struct mxc_nand_host *, uint16_t, int);
239         void                    (*send_page)(struct mtd_info *, unsigned int);
240         void                    (*send_read_id)(struct mxc_nand_host *);
241         uint16_t                (*get_dev_status)(struct mxc_nand_host *);
242         int                     (*check_int)(struct mxc_nand_host *);
243 };
244
245 /* OOB placement block for use with hardware ecc generation */
246 static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
247         .eccbytes = 5,
248         .eccpos = {6, 7, 8, 9, 10},
249         .oobfree = {{0, 5}, {12, 4}, }
250 };
251
252 static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
253         .eccbytes = 20,
254         .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
255                    38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
256         .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
257 };
258
259 /* OOB description for 512 byte pages with 16 byte OOB */
260 static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
261         .eccbytes = 1 * 9,
262         .eccpos = {
263                  7,  8,  9, 10, 11, 12, 13, 14, 15
264         },
265         .oobfree = {
266                 {.offset = 0, .length = 5}
267         }
268 };
269
270 /* OOB description for 2048 byte pages with 64 byte OOB */
271 static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
272         .eccbytes = 4 * 9,
273         .eccpos = {
274                  7,  8,  9, 10, 11, 12, 13, 14, 15,
275                 23, 24, 25, 26, 27, 28, 29, 30, 31,
276                 39, 40, 41, 42, 43, 44, 45, 46, 47,
277                 55, 56, 57, 58, 59, 60, 61, 62, 63
278         },
279         .oobfree = {
280                 {.offset = 2, .length = 4},
281                 {.offset = 16, .length = 7},
282                 {.offset = 32, .length = 7},
283                 {.offset = 48, .length = 7}
284         }
285 };
286
287 /* OOB description for 4096 byte pages with 128 byte OOB */
288 static struct nand_ecclayout nandv2_hw_eccoob_4k = {
289         .eccbytes = 8 * 9,
290         .eccpos = {
291                 7,  8,  9, 10, 11, 12, 13, 14, 15,
292                 23, 24, 25, 26, 27, 28, 29, 30, 31,
293                 39, 40, 41, 42, 43, 44, 45, 46, 47,
294                 55, 56, 57, 58, 59, 60, 61, 62, 63,
295                 71, 72, 73, 74, 75, 76, 77, 78, 79,
296                 87, 88, 89, 90, 91, 92, 93, 94, 95,
297                 103, 104, 105, 106, 107, 108, 109, 110, 111,
298                 119, 120, 121, 122, 123, 124, 125, 126, 127,
299         },
300         .oobfree = {
301                 {.offset = 2, .length = 4},
302                 {.offset = 16, .length = 7},
303                 {.offset = 32, .length = 7},
304                 {.offset = 48, .length = 7},
305                 {.offset = 64, .length = 7},
306                 {.offset = 80, .length = 7},
307                 {.offset = 96, .length = 7},
308                 {.offset = 112, .length = 7},
309         }
310 };
311
312 static int check_int_v3(struct mxc_nand_host *host)
313 {
314         uint32_t tmp;
315
316         tmp = readl(NFC_V3_IPC);
317         if (!(tmp & NFC_V3_IPC_INT))
318                 return 0;
319
320         tmp &= ~NFC_V3_IPC_INT;
321         writel(tmp, NFC_V3_IPC);
322
323         return 1;
324 }
325
326 static int check_int_v1_v2(struct mxc_nand_host *host)
327 {
328         uint32_t tmp;
329
330         tmp = readw(NFC_V1_V2_CONFIG2);
331         if (!(tmp & NFC_V1_V2_CONFIG2_INT))
332                 return 0;
333
334         writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
335
336         return 1;
337 }
338
339 /* This function polls the NANDFC to wait for the basic operation to
340  * complete by checking the INT bit of config2 register.
341  */
342 static void wait_op_done(struct mxc_nand_host *host, bool useirq)
343 {
344         int max_retries = 8000;
345
346         while (max_retries-- > 0) {
347                 if (host->check_int(host))
348                         break;
349
350                 udelay(1);
351         }
352         if (max_retries < 0)
353                 pr_debug("%s: INT not set\n", __func__);
354 }
355
356 static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
357 {
358         /* fill command */
359         writel(cmd, NFC_V3_FLASH_CMD);
360
361         /* send out command */
362         writel(NFC_CMD, NFC_V3_LAUNCH);
363
364         /* Wait for operation to complete */
365         wait_op_done(host, useirq);
366 }
367
368 /* This function issues the specified command to the NAND device and
369  * waits for completion. */
370 static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
371 {
372         pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq);
373
374         writew(cmd, NFC_V1_V2_FLASH_CMD);
375         writew(NFC_CMD, NFC_V1_V2_CONFIG2);
376
377         /* Wait for operation to complete */
378         wait_op_done(host, useirq);
379 }
380
381 static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
382 {
383         /* fill address */
384         writel(addr, NFC_V3_FLASH_ADDR0);
385
386         /* send out address */
387         writel(NFC_ADDR, NFC_V3_LAUNCH);
388
389         wait_op_done(host, islast);
390 }
391
392 /* This function sends an address (or partial address) to the
393  * NAND device. The address is used to select the source/destination for
394  * a NAND command. */
395 static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
396 {
397         pr_debug("send_addr(host, 0x%x %d)\n", addr, islast);
398
399         writew(addr, NFC_V1_V2_FLASH_ADDR);
400         writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
401
402         /* Wait for operation to complete */
403         wait_op_done(host, islast);
404 }
405
406 static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
407 {
408         struct nand_chip *nand_chip = mtd->priv;
409         struct mxc_nand_host *host = nand_chip->priv;
410         uint32_t tmp;
411
412         tmp = readl(NFC_V3_CONFIG1);
413         tmp &= ~(7 << 4);
414         writel(tmp, NFC_V3_CONFIG1);
415
416         /* transfer data from NFC ram to nand */
417         writel(ops, NFC_V3_LAUNCH);
418
419         wait_op_done(host, false);
420 }
421
422 static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
423 {
424         struct nand_chip *nand_chip = mtd->priv;
425         struct mxc_nand_host *host = nand_chip->priv;
426         int bufs, i;
427
428         if (nfc_is_v1() && mtd->writesize > 512)
429                 bufs = 4;
430         else
431                 bufs = 1;
432
433         for (i = 0; i < bufs; i++) {
434
435                 /* NANDFC buffer 0 is used for page read/write */
436                 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
437
438                 writew(ops, NFC_V1_V2_CONFIG2);
439
440                 /* Wait for operation to complete */
441                 wait_op_done(host, true);
442         }
443 }
444
445 static void send_read_id_v3(struct mxc_nand_host *host)
446 {
447         /* Read ID into main buffer */
448         writel(NFC_ID, NFC_V3_LAUNCH);
449
450         wait_op_done(host, true);
451
452         memcpy(host->data_buf, host->main_area0, 16);
453
454         pr_debug("read ID %02x %02x %02x %02x\n",
455                 host->data_buf[0], host->data_buf[1],
456                 host->data_buf[2], host->data_buf[3]);
457 }
458
459 /* Request the NANDFC to perform a read of the NAND device ID. */
460 static void send_read_id_v1_v2(struct mxc_nand_host *host)
461 {
462         struct nand_chip *this = &host->nand;
463
464         /* NANDFC buffer 0 is used for device ID output */
465         writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
466
467         writew(NFC_ID, NFC_V1_V2_CONFIG2);
468
469         /* Wait for operation to complete */
470         wait_op_done(host, true);
471
472         memcpy(host->data_buf, host->main_area0, 16);
473
474         if (this->options & NAND_BUSWIDTH_16) {
475                 /* compress the ID info */
476                 host->data_buf[1] = host->data_buf[2];
477                 host->data_buf[2] = host->data_buf[4];
478                 host->data_buf[3] = host->data_buf[6];
479                 host->data_buf[4] = host->data_buf[8];
480                 host->data_buf[5] = host->data_buf[10];
481         }
482 }
483
484 static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
485 {
486         writel(NFC_STATUS, NFC_V3_LAUNCH);
487         wait_op_done(host, true);
488
489         return readl(NFC_V3_CONFIG1) >> 16;
490 }
491
492 /* This function requests the NANDFC to perform a read of the
493  * NAND device status and returns the current status. */
494 static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
495 {
496         void __iomem *main_buf = host->main_area0;
497         uint32_t store;
498         uint16_t ret;
499
500         writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
501
502         /*
503          * The device status is stored in main_area0. To
504          * prevent corruption of the buffer save the value
505          * and restore it afterwards.
506          */
507         store = readl(main_buf);
508
509         writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
510         wait_op_done(host, true);
511
512         ret = readw(main_buf);
513
514         writel(store, main_buf);
515
516         return ret;
517 }
518
519 /* This functions is used by upper layer to checks if device is ready */
520 static int mxc_nand_dev_ready(struct mtd_info *mtd)
521 {
522         /*
523          * NFC handles R/B internally. Therefore, this function
524          * always returns status as ready.
525          */
526         return 1;
527 }
528
529 static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
530 {
531         /*
532          * If HW ECC is enabled, we turn it on during init. There is
533          * no need to enable again here.
534          */
535 }
536
537 static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
538                                  u_char *read_ecc, u_char *calc_ecc)
539 {
540         struct nand_chip *nand_chip = mtd->priv;
541         struct mxc_nand_host *host = nand_chip->priv;
542
543         /*
544          * 1-Bit errors are automatically corrected in HW.  No need for
545          * additional correction.  2-Bit errors cannot be corrected by
546          * HW ECC, so we need to return failure
547          */
548         uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
549
550         if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
551                 printk("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
552                 return -1;
553         }
554
555         return 0;
556 }
557
558 static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
559                                  u_char *read_ecc, u_char *calc_ecc)
560 {
561         struct nand_chip *nand_chip = mtd->priv;
562         struct mxc_nand_host *host = nand_chip->priv;
563         u32 ecc_stat, err;
564         int no_subpages = 1;
565         int ret = 0;
566         u8 ecc_bit_mask, err_limit;
567
568         ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
569         err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
570
571         no_subpages = mtd->writesize >> 9;
572
573         if (nfc_is_v21())
574                 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
575         else
576                 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
577
578         do {
579                 err = ecc_stat & ecc_bit_mask;
580                 if (err > err_limit) {
581                         printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
582                         return -1;
583                 } else {
584                         ret += err;
585                 }
586                 ecc_stat >>= 4;
587         } while (--no_subpages);
588
589         mtd->ecc_stats.corrected += ret;
590         if (ret)
591                 pr_debug("%d Symbol Correctable RS-ECC Errors\n", ret);
592
593         return ret;
594 }
595
596 static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
597                                   u_char *ecc_code)
598 {
599         return 0;
600 }
601
602 static u_char mxc_nand_read_byte(struct mtd_info *mtd)
603 {
604         struct nand_chip *nand_chip = mtd->priv;
605         struct mxc_nand_host *host = nand_chip->priv;
606         uint8_t ret;
607
608         /* Check for status request */
609         if (host->status_request)
610                 return host->get_dev_status(host) & 0xFF;
611
612         ret = *(uint8_t *)(host->data_buf + host->buf_start);
613         host->buf_start++;
614
615         return ret;
616 }
617
618 static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
619 {
620         struct nand_chip *nand_chip = mtd->priv;
621         struct mxc_nand_host *host = nand_chip->priv;
622         uint16_t ret;
623
624         ret = *(uint16_t *)(host->data_buf + host->buf_start);
625         host->buf_start += 2;
626
627         return ret;
628 }
629
630 /* Write data of length len to buffer buf. The data to be
631  * written on NAND Flash is first copied to RAMbuffer. After the Data Input
632  * Operation by the NFC, the data is written to NAND Flash */
633 static void mxc_nand_write_buf(struct mtd_info *mtd,
634                                 const u_char *buf, int len)
635 {
636         struct nand_chip *nand_chip = mtd->priv;
637         struct mxc_nand_host *host = nand_chip->priv;
638         u16 col = host->buf_start;
639         int n = mtd->oobsize + mtd->writesize - col;
640
641         n = min(n, len);
642
643         memcpy(host->data_buf + col, buf, n);
644
645         host->buf_start += n;
646 }
647
648 /* Read the data buffer from the NAND Flash. To read the data from NAND
649  * Flash first the data output cycle is initiated by the NFC, which copies
650  * the data to RAMbuffer. This data of length len is then copied to buffer buf.
651  */
652 static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
653 {
654         struct nand_chip *nand_chip = mtd->priv;
655         struct mxc_nand_host *host = nand_chip->priv;
656         u16 col = host->buf_start;
657         int n = mtd->oobsize + mtd->writesize - col;
658
659         n = min(n, len);
660
661         memcpy(buf, host->data_buf + col, n);
662
663         host->buf_start += n;
664 }
665
666 #if defined(__UBOOT__) && defined(CONFIG_MTD_NAND_VERIFY_WRITE)
667 /* Used by the upper layer to verify the data in NAND Flash
668  * with the data in the buf. */
669 static int mxc_nand_verify_buf(struct mtd_info *mtd,
670                                 const u_char *buf, int len)
671 {
672         return -EFAULT;
673 }
674 #endif
675
676 /* This function is used by upper layer for select and
677  * deselect of the NAND chip */
678 static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
679 {
680         struct nand_chip *nand_chip = mtd->priv;
681         struct mxc_nand_host *host = nand_chip->priv;
682
683         if (nfc_is_v21()) {
684                 host->active_cs = chip;
685                 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
686         }
687 }
688
689 /*
690  * Function to transfer data to/from spare area.
691  */
692 static void copy_spare(struct mtd_info *mtd, bool bfrom)
693 {
694         struct nand_chip *this = mtd->priv;
695         struct mxc_nand_host *host = this->priv;
696         u16 i, j;
697         u16 n = mtd->writesize >> 9;
698         u8 *d = host->data_buf + mtd->writesize;
699         u8 *s = host->spare0;
700         u16 t = host->spare_len;
701
702         j = (mtd->oobsize / n >> 1) << 1;
703
704         if (bfrom) {
705                 for (i = 0; i < n - 1; i++)
706                         memcpy(d + i * j, s + i * t, j);
707
708                 /* the last section */
709                 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
710         } else {
711                 for (i = 0; i < n - 1; i++)
712                         memcpy(&s[i * t], &d[i * j], j);
713
714                 /* the last section */
715                 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
716         }
717 }
718
719 static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
720 {
721         struct nand_chip *nand_chip = mtd->priv;
722         struct mxc_nand_host *host = nand_chip->priv;
723
724         /* Write out column address, if necessary */
725         if (column != -1) {
726                 /*
727                  * MXC NANDFC can only perform full page+spare or
728                  * spare-only read/write.  When the upper layers
729                  * perform a read/write buf operation, the saved column
730                   * address is used to index into the full page.
731                  */
732                 host->send_addr(host, 0, page_addr == -1);
733                 if (mtd->writesize > 512)
734                         /* another col addr cycle for 2k page */
735                         host->send_addr(host, 0, false);
736         }
737
738         /* Write out page address, if necessary */
739         if (page_addr != -1) {
740                 /* paddr_0 - p_addr_7 */
741                 host->send_addr(host, (page_addr & 0xff), false);
742
743                 if (mtd->writesize > 512) {
744                         if (mtd->size >= 0x10000000) {
745                                 /* paddr_8 - paddr_15 */
746                                 host->send_addr(host, (page_addr >> 8) & 0xff, false);
747                                 host->send_addr(host, (page_addr >> 16) & 0xff, true);
748                         } else
749                                 /* paddr_8 - paddr_15 */
750                                 host->send_addr(host, (page_addr >> 8) & 0xff, true);
751                 } else {
752                         /* One more address cycle for higher density devices */
753                         if (mtd->size >= 0x4000000) {
754                                 /* paddr_8 - paddr_15 */
755                                 host->send_addr(host, (page_addr >> 8) & 0xff, false);
756                                 host->send_addr(host, (page_addr >> 16) & 0xff, true);
757                         } else
758                                 /* paddr_8 - paddr_15 */
759                                 host->send_addr(host, (page_addr >> 8) & 0xff, true);
760                 }
761         }
762 }
763
764 /*
765  * v2 and v3 type controllers can do 4bit or 8bit ecc depending
766  * on how much oob the nand chip has. For 8bit ecc we need at least
767  * 26 bytes of oob data per 512 byte block.
768  */
769 static int get_eccsize(struct mtd_info *mtd)
770 {
771         int oobbytes_per_512 = 0;
772
773         oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
774
775         if (oobbytes_per_512 < 26)
776                 return 4;
777         else
778                 return 8;
779 }
780
781 static void preset_v1_v2(struct mtd_info *mtd)
782 {
783         struct nand_chip *nand_chip = mtd->priv;
784         struct mxc_nand_host *host = nand_chip->priv;
785         uint16_t config1 = 0;
786
787         if (nand_chip->ecc.mode == NAND_ECC_HW)
788                 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
789
790         if (nfc_is_v21())
791                 config1 |= NFC_V2_CONFIG1_FP_INT;
792
793         if (nfc_is_v21() && mtd->writesize) {
794                 uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
795
796                 host->eccsize = get_eccsize(mtd);
797                 if (host->eccsize == 4)
798                         config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
799
800                 config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
801         } else {
802                 host->eccsize = 1;
803         }
804
805         writew(config1, NFC_V1_V2_CONFIG1);
806         /* preset operation */
807
808         /* Unlock the internal RAM Buffer */
809         writew(0x2, NFC_V1_V2_CONFIG);
810
811         /* Blocks to be unlocked */
812         if (nfc_is_v21()) {
813                 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
814                 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
815                 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
816                 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
817                 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
818                 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
819                 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
820                 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
821         } else if (nfc_is_v1()) {
822                 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
823                 writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
824         } else
825                 BUG();
826
827         /* Unlock Block Command for given address range */
828         writew(0x4, NFC_V1_V2_WRPROT);
829 }
830
831 static void preset_v3(struct mtd_info *mtd)
832 {
833         struct nand_chip *chip = mtd->priv;
834         struct mxc_nand_host *host = chip->priv;
835         uint32_t config2, config3;
836         int i, addr_phases;
837
838         writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
839         writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
840         WARN_ON(!(readl(NFC_V3_IPC) & NFC_V3_IPC_CACK));
841
842         /* Unlock the internal RAM Buffer */
843         writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
844                         NFC_V3_WRPROT);
845
846         /* Blocks to be unlocked */
847         for (i = 0; i < CONFIG_SYS_NAND_MAX_CHIPS; i++)
848                 writel(0x0 | (0xffff << 16),
849                                 NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
850
851         config2 = NFC_V3_CONFIG2_ONE_CYCLE |
852                 NFC_V3_CONFIG2_2CMD_PHASES |
853                 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
854                 NFC_V3_CONFIG2_ST_CMD(0x70) |
855                 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
856
857         if (chip->ecc.mode == NAND_ECC_HW)
858                 config2 |= NFC_V3_CONFIG2_ECC_EN;
859
860         addr_phases = fls(chip->pagemask) >> 3;
861
862         if (mtd->writesize == 2048) {
863                 config2 |= NFC_V3_CONFIG2_PS_2048;
864                 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
865         } else if (mtd->writesize == 4096) {
866                 config2 |= NFC_V3_CONFIG2_PS_4096;
867                 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
868         } else {
869                 config2 |= NFC_V3_CONFIG2_PS_512;
870                 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
871         }
872
873         if (mtd->writesize) {
874 #if defined CONFIG_SOC_MX53
875                 config2 |= MX53_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
876 #else
877                 config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
878 #endif
879                 host->eccsize = get_eccsize(mtd);
880                 if (host->eccsize == 8)
881                         config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
882         }
883
884         writel(config2, NFC_V3_CONFIG2);
885
886         config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
887                         NFC_V3_CONFIG3_NO_SDMA |
888                         NFC_V3_CONFIG3_RBB_MODE |
889                         NFC_V3_CONFIG3_SBB(6) | /* Reset default */
890                         NFC_V3_CONFIG3_ADD_OP(0);
891
892         if (!(chip->options & NAND_BUSWIDTH_16))
893                 config3 |= NFC_V3_CONFIG3_FW8;
894
895         writel(config3, NFC_V3_CONFIG3);
896
897         writel(0, NFC_V3_DELAY_LINE);
898         writel(0, NFC_V3_IPC);
899 }
900
901 /* Used by the upper layer to write command to NAND Flash for
902  * different operations to be carried out on NAND Flash */
903 static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
904                                 int column, int page_addr)
905 {
906         struct nand_chip *nand_chip = mtd->priv;
907         struct mxc_nand_host *host = nand_chip->priv;
908
909         pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
910               command, column, page_addr);
911
912         /* Reset command state information */
913         host->status_request = false;
914
915         /* Command pre-processing step */
916         switch (command) {
917         case NAND_CMD_RESET:
918                 host->preset(mtd);
919                 host->send_cmd(host, command, false);
920                 break;
921
922         case NAND_CMD_STATUS:
923                 host->buf_start = 0;
924                 host->status_request = true;
925
926                 host->send_cmd(host, command, true);
927                 mxc_do_addr_cycle(mtd, column, page_addr);
928                 break;
929
930         case NAND_CMD_READ0:
931         case NAND_CMD_READOOB:
932                 if (command == NAND_CMD_READ0)
933                         host->buf_start = column;
934                 else
935                         host->buf_start = column + mtd->writesize;
936
937                 command = NAND_CMD_READ0; /* only READ0 is valid */
938
939                 host->send_cmd(host, command, false);
940                 mxc_do_addr_cycle(mtd, column, page_addr);
941
942                 if (mtd->writesize > 512)
943                         host->send_cmd(host, NAND_CMD_READSTART, true);
944
945                 host->send_page(mtd, NFC_OUTPUT);
946
947                 memcpy(host->data_buf, host->main_area0, mtd->writesize);
948                 copy_spare(mtd, true);
949                 break;
950
951         case NAND_CMD_SEQIN:
952                 if (column >= mtd->writesize)
953                         /* call ourself to read a page */
954                         mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
955
956                 host->buf_start = column;
957
958                 host->send_cmd(host, command, false);
959                 mxc_do_addr_cycle(mtd, column, page_addr);
960                 break;
961
962         case NAND_CMD_PAGEPROG:
963                 memcpy(host->main_area0, host->data_buf, mtd->writesize);
964                 copy_spare(mtd, false);
965                 host->send_page(mtd, NFC_INPUT);
966                 host->send_cmd(host, command, true);
967                 mxc_do_addr_cycle(mtd, column, page_addr);
968                 break;
969
970         case NAND_CMD_READID:
971                 host->send_cmd(host, command, true);
972                 mxc_do_addr_cycle(mtd, column, page_addr);
973                 host->send_read_id(host);
974                 host->buf_start = column;
975                 break;
976
977         case NAND_CMD_ERASE1:
978         case NAND_CMD_ERASE2:
979                 host->send_cmd(host, command, false);
980                 mxc_do_addr_cycle(mtd, column, page_addr);
981
982                 break;
983         }
984 }
985
986 /*
987  * The generic flash bbt decriptors overlap with our ecc
988  * hardware, so define some i.MX specific ones.
989  */
990 static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
991 static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
992
993 static struct nand_bbt_descr bbt_main_descr = {
994         .options = NAND_BBT_LASTBLOCK | NAND_BBT_WRITE |
995                 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
996         .offs = 0,
997         .len = 4,
998         .veroffs = 4,
999         .maxblocks = 4,
1000         .pattern = bbt_pattern,
1001 };
1002
1003 static struct nand_bbt_descr bbt_mirror_descr = {
1004         .options = NAND_BBT_LASTBLOCK | NAND_BBT_WRITE |
1005                 NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1006         .offs = 0,
1007         .len = 4,
1008         .veroffs = 4,
1009         .maxblocks = 4,
1010         .pattern = mirror_pattern,
1011 };
1012
1013 static void mxc_nand_chip_init(int devno)
1014 {
1015         int err;
1016         struct nand_chip *this;
1017         struct mtd_info *mtd = &nand_info[devno];
1018         struct nand_ecclayout *oob_smallpage, *oob_largepage;
1019
1020         /* allocate a minimal buffer for the read_id command */
1021         host->data_buf = malloc(16);
1022         if (!host->data_buf) {
1023                 printk("Failed to allocate ID buffer\n");
1024                 return;
1025         }
1026
1027         /* structures must be linked */
1028         this = &host->nand;
1029 //      host->mtd = mtd;
1030         mtd->priv = this;
1031         mtd->name = DRIVER_NAME;
1032
1033         /* 50 us command delay time */
1034         this->chip_delay = 5;
1035
1036         this->priv = host;
1037         this->dev_ready = mxc_nand_dev_ready;
1038         this->cmdfunc = mxc_nand_command;
1039         this->select_chip = mxc_nand_select_chip;
1040         this->read_byte = mxc_nand_read_byte;
1041         this->read_word = mxc_nand_read_word;
1042         this->write_buf = mxc_nand_write_buf;
1043         this->read_buf = mxc_nand_read_buf;
1044         host->base = (void __iomem *)CONFIG_MXC_NAND_REGS_BASE;
1045         if (!host->base) {
1046                 return;
1047         }
1048
1049         host->main_area0 = host->base;
1050
1051         if (nfc_is_v1() || nfc_is_v21()) {
1052                 host->preset = preset_v1_v2;
1053                 host->send_cmd = send_cmd_v1_v2;
1054                 host->send_addr = send_addr_v1_v2;
1055                 host->send_page = send_page_v1_v2;
1056                 host->send_read_id = send_read_id_v1_v2;
1057                 host->get_dev_status = get_dev_status_v1_v2;
1058                 host->check_int = check_int_v1_v2;
1059         }
1060
1061         if (nfc_is_v21()) {
1062                 host->regs = host->base + 0x1e00;
1063                 host->spare0 = host->base + 0x1000;
1064                 host->spare_len = 64;
1065                 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1066                 oob_largepage = &nandv2_hw_eccoob_largepage;
1067                 this->ecc.bytes = 9;
1068         } else if (nfc_is_v1()) {
1069                 host->regs = host->base + 0xe00;
1070                 host->spare0 = host->base + 0x800;
1071                 host->spare_len = 16;
1072                 oob_smallpage = &nandv1_hw_eccoob_smallpage;
1073                 oob_largepage = &nandv1_hw_eccoob_largepage;
1074                 this->ecc.bytes = 3;
1075                 host->eccsize = 1;
1076         } else if (nfc_is_v3_2()) {
1077                 host->regs_ip = (void __iomem *)CONFIG_MXC_NAND_IP_REGS_BASE;
1078                 host->regs_axi = host->base + 0x1e00;
1079                 host->spare0 = host->base + 0x1000;
1080                 host->spare_len = 64;
1081                 host->preset = preset_v3;
1082                 host->send_cmd = send_cmd_v3;
1083                 host->send_addr = send_addr_v3;
1084                 host->send_page = send_page_v3;
1085                 host->send_read_id = send_read_id_v3;
1086                 host->check_int = check_int_v3;
1087                 host->get_dev_status = get_dev_status_v3;
1088                 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1089                 oob_largepage = &nandv2_hw_eccoob_largepage;
1090                 this->ecc.strength = 4;
1091         } else
1092                 hang();
1093
1094         this->ecc.size = 512;
1095         this->ecc.layout = oob_smallpage;
1096
1097 #ifdef CONFIG_MXC_NAND_HWECC
1098         this->ecc.calculate = mxc_nand_calculate_ecc;
1099         this->ecc.hwctl = mxc_nand_enable_hwecc;
1100         if (nfc_is_v1())
1101                 this->ecc.correct = mxc_nand_correct_data_v1;
1102         else
1103                 this->ecc.correct = mxc_nand_correct_data_v2_v3;
1104         this->ecc.mode = NAND_ECC_HW;
1105 #else
1106         this->ecc.mode = NAND_ECC_SOFT;
1107 #endif
1108         /* NAND bus width determines access funtions used by upper layer */
1109         if (is_16bit_nand())
1110                 this->options |= NAND_BUSWIDTH_16;
1111
1112 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1113         this->bbt_options |= NAND_BBT_USE_FLASH;
1114         this->bbt_td = &bbt_main_descr;
1115         this->bbt_md = &bbt_mirror_descr;
1116         this->bbt_td->options |= NAND_BBT_CREATE;
1117         this->bbt_md->options |= NAND_BBT_CREATE;
1118 #endif
1119
1120         /* first scan to find the device and get the page size */
1121         if (nand_scan_ident(mtd, nfc_is_v21() ? 4 : 1, NULL)) {
1122                 return;
1123         }
1124
1125         host->data_buf = realloc(host->data_buf,
1126                                 mtd->writesize + mtd->oobsize);
1127         if (!host->data_buf) {
1128                 printk("Failed to allocate data buffer of %u byte\n",
1129                         mtd->writesize + mtd->oobsize);
1130                 return;
1131         }
1132         pr_debug("Allocated %u byte data buffer\n",
1133                 mtd->writesize + mtd->oobsize);
1134
1135         /* Call preset again, with correct writesize this time */
1136         host->preset(mtd);
1137
1138         if (mtd->writesize == 2048)
1139                 this->ecc.layout = oob_largepage;
1140         if (nfc_is_v21() && mtd->writesize == 4096)
1141                 this->ecc.layout = &nandv2_hw_eccoob_4k;
1142
1143         /* second phase scan */
1144         err = nand_scan_tail(mtd);
1145         if (err) {
1146                 printk("Nand scan failed: %d\n", err);
1147                 return;
1148         }
1149
1150         err = nand_register(devno);
1151         if (err)
1152                 return;
1153 }
1154
1155 void board_nand_init(void)
1156 {
1157         int i;
1158
1159         for (i = 0; i < CONFIG_SYS_MAX_NAND_DEVICE; i++)
1160                 mxc_nand_chip_init(i);
1161 }