]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/mxs_nand.c
Merge remote-tracking branch 'remotes/tx6/tx6' into karo-tx-uboot
[karo-tx-uboot.git] / drivers / mtd / nand / mxs_nand.c
1 /*
2  * Freescale i.MX28 NAND flash driver
3  *
4  * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
5  * on behalf of DENX Software Engineering GmbH
6  *
7  * Based on code from LTIB:
8  * Freescale GPMI NFC NAND Flash Driver
9  *
10  * Copyright (C) 2010 Freescale Semiconductor, Inc.
11  * Copyright (C) 2008 Embedded Alley Solutions, Inc.
12  *
13  * SPDX-License-Identifier:     GPL-2.0+
14  */
15 //#define DEBUG
16
17 #include <common.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/nand.h>
20 #include <linux/types.h>
21 #include <malloc.h>
22 #include <asm/errno.h>
23 #include <asm/io.h>
24 #include <asm/arch/clock.h>
25 #include <asm/arch/imx-regs.h>
26 #include <asm/imx-common/regs-bch.h>
27 #include <asm/imx-common/regs-gpmi.h>
28 #include <asm/arch/sys_proto.h>
29 #include <asm/imx-common/dma.h>
30
31 #define MXS_NAND_DMA_DESCRIPTOR_COUNT           4
32
33 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE          512
34 #if defined(CONFIG_SOC_MX6)
35 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT    2
36 #else
37 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT    0
38 #endif
39 #define MXS_NAND_METADATA_SIZE                  10
40
41 #define MXS_NAND_COMMAND_BUFFER_SIZE            32
42
43 /* BCH timeout in microseconds */
44 #define MXS_NAND_BCH_TIMEOUT                    10000
45
46 static struct bch_regs *bch_regs = (void *)BCH_BASE_ADDRESS;
47 static struct gpmi_regs *gpmi_regs = (void *)GPMI_BASE_ADDRESS;
48 struct mxs_nand_info {
49         int             cur_chip;
50
51         uint32_t        cmd_queue_len;
52         uint32_t        data_buf_size;
53
54         uint8_t         *cmd_buf;
55         uint8_t         *data_buf;
56         uint8_t         *oob_buf;
57
58         uint8_t         marking_block_bad;
59         uint8_t         raw_oob_mode;
60
61         /* Functions with altered behaviour */
62         int             (*hooked_read_oob)(struct mtd_info *mtd,
63                                 loff_t from, struct mtd_oob_ops *ops);
64         int             (*hooked_write_oob)(struct mtd_info *mtd,
65                                 loff_t to, struct mtd_oob_ops *ops);
66         int             (*hooked_block_markbad)(struct mtd_info *mtd,
67                                 loff_t ofs);
68
69         /* DMA descriptors */
70         struct mxs_dma_desc     **desc;
71         uint32_t                desc_index;
72 };
73
74 #ifdef DEBUG
75 #define dump_reg(b, r)  __dump_reg(&b->r, #r)
76 static inline void __dump_reg(void *addr, const char *name)
77 {
78         printf("%16s[%p]=%08x\n", name, addr, readl(addr));
79 }
80
81 #define dump_bch_reg(n) __dump_reg(&bch_regs->hw_bch_##n, #n)
82 #define dump_gpmi_reg(n) __dump_reg(&gpmi_regs->hw_gpmi_##n, #n)
83 static inline void dump_regs(void)
84 {
85         printf("BCH:\n");
86         dump_bch_reg(ctrl);
87         dump_bch_reg(status0);
88         dump_bch_reg(mode);
89         dump_bch_reg(debug0);
90         dump_bch_reg(dbgkesread);
91         dump_bch_reg(dbgcsferead);
92         dump_bch_reg(dbgsyndegread);
93         dump_bch_reg(dbgahbmread);
94         dump_bch_reg(blockname);
95         dump_bch_reg(version);
96
97         printf("\nGPMI:\n");
98         dump_gpmi_reg(ctrl0);
99         dump_gpmi_reg(eccctrl);
100         dump_gpmi_reg(ecccount);
101         dump_gpmi_reg(payload);
102         dump_gpmi_reg(auxiliary);
103         dump_gpmi_reg(ctrl1);
104         dump_gpmi_reg(data);
105         dump_gpmi_reg(stat);
106         dump_gpmi_reg(debug);
107         dump_gpmi_reg(version);
108         dump_gpmi_reg(debug2);
109         dump_gpmi_reg(debug3);
110 }
111
112 static inline int dbg_addr(void *addr)
113 {
114         if (((unsigned long)addr & ~0xfff) == BCH_BASE_ADDRESS)
115                 return 1;
116         return 1;
117 }
118
119 static inline u32 mxs_readl(void *addr,
120                         const char *fn, int ln)
121 {
122         u32 val = readl(addr);
123         static void *last_addr;
124         static u32 last_val;
125
126         if (!dbg_addr(addr))
127                 return val;
128
129         if (addr != last_addr || last_val != val) {
130                 printf("%s@%d: Read %08x from %p\n", fn, ln, val, addr);
131                 last_addr = addr;
132                 last_val = val;
133         }
134         return val;
135 }
136
137 static inline void mxs_writel(u32 val, void *addr,
138                         const char *fn, int ln)
139 {
140         if (dbg_addr(addr))
141                 printf("%s@%d: Writing %08x to %p...", fn, ln, val, addr);
142         writel(val, addr);
143         if (dbg_addr(addr))
144                 printf(" result: %08x\n", readl(addr));
145 }
146
147 #undef readl
148 #define readl(a) mxs_readl(a, __func__, __LINE__)
149
150 #undef writel
151 #define writel(v, a) mxs_writel(v, a, __func__, __LINE__)
152 static inline void memdump(const void *addr, size_t len)
153 {
154         const char *buf = addr;
155         int i;
156
157         for (i = 0; i < len; i++) {
158                 if (i % 16 == 0) {
159                         if (i > 0)
160                                 printf("\n");
161                         printf("%p:", &buf[i]);
162                 }
163                 printf(" %02x", buf[i]);
164         }
165         printf("\n");
166 }
167 #else
168 static inline void memdump(void *addr, size_t len)
169 {
170 }
171
172 static inline void dump_regs(void)
173 {
174 }
175 #endif
176
177 struct nand_ecclayout fake_ecc_layout;
178
179 /*
180  * Cache management functions
181  */
182 #ifndef CONFIG_SYS_DCACHE_OFF
183 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
184 {
185         uint32_t addr = (uint32_t)info->data_buf;
186
187         flush_dcache_range(addr, addr + info->data_buf_size);
188 }
189
190 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
191 {
192         uint32_t addr = (uint32_t)info->data_buf;
193
194         invalidate_dcache_range(addr, addr + info->data_buf_size);
195 }
196
197 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
198 {
199         uint32_t addr = (uint32_t)info->cmd_buf;
200
201         flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
202 }
203 #else
204 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
205 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
206 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
207 #endif
208
209 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
210 {
211         struct mxs_dma_desc *desc;
212
213         if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
214                 printf("MXS NAND: Too many DMA descriptors requested\n");
215                 return NULL;
216         }
217
218         desc = info->desc[info->desc_index];
219         info->desc_index++;
220
221         return desc;
222 }
223
224 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
225 {
226         int i;
227         struct mxs_dma_desc *desc;
228
229         for (i = 0; i < info->desc_index; i++) {
230                 desc = info->desc[i];
231                 memset(desc, 0, sizeof(struct mxs_dma_desc));
232                 desc->address = (dma_addr_t)desc;
233         }
234
235         info->desc_index = 0;
236 }
237
238 static uint32_t mxs_nand_ecc_chunk_cnt(struct mtd_info *mtd)
239 {
240         struct nand_chip *nand = mtd->priv;
241         return mtd->writesize / nand->ecc.size;
242 }
243
244 static inline uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
245 {
246         return ecc_strength * 13;
247 }
248
249 static uint32_t mxs_nand_aux_status_offset(void)
250 {
251         return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
252 }
253
254 static int mxs_nand_gpmi_init(void)
255 {
256         int ret;
257
258         /* Reset the GPMI block. */
259         ret = mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
260         if (ret)
261                 return ret;
262
263         /*
264          * Choose NAND mode, set IRQ polarity, disable write protection and
265          * select BCH ECC.
266          */
267         clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
268                         GPMI_CTRL1_GPMI_MODE,
269                         GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
270                         GPMI_CTRL1_BCH_MODE);
271         writel(0x500 << 16, &gpmi_regs->hw_gpmi_timing1);
272         return 0;
273 }
274
275 static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
276                                                 uint32_t page_oob_size)
277 {
278         if (page_data_size == 2048) {
279                 if (page_oob_size == 64)
280                         return 8;
281
282                 if (page_oob_size == 112)
283                         return 14;
284         }
285
286         if (page_data_size == 4096) {
287                 if (page_oob_size == 128)
288                         return 8;
289
290                 if (page_oob_size == 218)
291                         return 16;
292
293                 if (page_oob_size == 224)
294                         return 16;
295         }
296
297         return 0;
298 }
299
300 static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
301                                                 uint32_t ecc_strength)
302 {
303         uint32_t chunk_data_size_in_bits;
304         uint32_t chunk_ecc_size_in_bits;
305         uint32_t chunk_total_size_in_bits;
306         uint32_t block_mark_chunk_number;
307         uint32_t block_mark_chunk_bit_offset;
308         uint32_t block_mark_bit_offset;
309
310         chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
311         chunk_ecc_size_in_bits  = mxs_nand_ecc_size_in_bits(ecc_strength);
312
313         chunk_total_size_in_bits =
314                         chunk_data_size_in_bits + chunk_ecc_size_in_bits;
315
316         /* Compute the bit offset of the block mark within the physical page. */
317         block_mark_bit_offset = page_data_size * 8;
318
319         /* Subtract the metadata bits. */
320         block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
321
322         /*
323          * Compute the chunk number (starting at zero) in which the block mark
324          * appears.
325          */
326         block_mark_chunk_number =
327                         block_mark_bit_offset / chunk_total_size_in_bits;
328
329         /*
330          * Compute the bit offset of the block mark within its chunk, and
331          * validate it.
332          */
333         block_mark_chunk_bit_offset = block_mark_bit_offset -
334                         (block_mark_chunk_number * chunk_total_size_in_bits);
335
336         if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
337                 return 1;
338
339         /*
340          * Now that we know the chunk number in which the block mark appears,
341          * we can subtract all the ECC bits that appear before it.
342          */
343         block_mark_bit_offset -=
344                 block_mark_chunk_number * chunk_ecc_size_in_bits;
345
346         return block_mark_bit_offset;
347 }
348
349 static inline uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
350 {
351         uint32_t ecc_strength;
352         ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
353         return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3;
354 }
355
356 static inline uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
357 {
358         uint32_t ecc_strength;
359         ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
360         return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) & 0x7;
361 }
362
363 /*
364  * Wait for BCH complete IRQ and clear the IRQ
365  */
366 static int mxs_nand_wait_for_bch_complete(void)
367 {
368         int timeout = MXS_NAND_BCH_TIMEOUT;
369         int ret;
370
371         ret = mxs_wait_mask_set(&bch_regs->hw_bch_ctrl_reg,
372                 BCH_CTRL_COMPLETE_IRQ, timeout);
373         if (ret) {
374                 debug("%s@%d: %d\n", __func__, __LINE__, ret);
375                 mxs_nand_gpmi_init();
376         }
377
378         writel(BCH_CTRL_COMPLETE_IRQ, &bch_regs->hw_bch_ctrl_clr);
379
380         return ret;
381 }
382
383 /*
384  * This is the function that we install in the cmd_ctrl function pointer of the
385  * owning struct nand_chip. The only functions in the reference implementation
386  * that use these functions pointers are cmdfunc and select_chip.
387  *
388  * In this driver, we implement our own select_chip, so this function will only
389  * be called by the reference implementation's cmdfunc. For this reason, we can
390  * ignore the chip enable bit and concentrate only on sending bytes to the NAND
391  * Flash.
392  */
393 static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
394 {
395         struct nand_chip *nand = mtd->priv;
396         struct mxs_nand_info *nand_info = nand->priv;
397         struct mxs_dma_desc *d;
398         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
399         int ret;
400
401         /*
402          * If this condition is true, something is _VERY_ wrong in MTD
403          * subsystem!
404          */
405         if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
406                 printf("MXS NAND: Command queue too long\n");
407                 return;
408         }
409
410         /*
411          * Every operation begins with a command byte and a series of zero or
412          * more address bytes. These are distinguished by either the Address
413          * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
414          * asserted. When MTD is ready to execute the command, it will
415          * deasert both latch enables.
416          *
417          * Rather than run a separate DMA operation for every single byte, we
418          * queue them up and run a single DMA operation for the entire series
419          * of command and data bytes.
420          */
421         if (ctrl & (NAND_ALE | NAND_CLE)) {
422                 if (data != NAND_CMD_NONE)
423                         nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
424                 return;
425         }
426
427         /*
428          * If control arrives here, MTD has deasserted both the ALE and CLE,
429          * which means it's ready to run an operation. Check if we have any
430          * bytes to send.
431          */
432         if (nand_info->cmd_queue_len == 0)
433                 return;
434
435         /* Compile the DMA descriptor -- a descriptor that sends command. */
436         d = mxs_nand_get_dma_desc(nand_info);
437         d->cmd.data =
438                 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
439                 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
440                 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
441                 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
442
443         d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
444
445         d->cmd.pio_words[0] =
446                 GPMI_CTRL0_COMMAND_MODE_WRITE |
447                 GPMI_CTRL0_WORD_LENGTH |
448                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
449                 GPMI_CTRL0_ADDRESS_NAND_CLE |
450                 GPMI_CTRL0_ADDRESS_INCREMENT |
451                 nand_info->cmd_queue_len;
452
453         mxs_dma_desc_append(channel, d);
454
455         /* Flush caches */
456         mxs_nand_flush_cmd_buf(nand_info);
457
458         /* Execute the DMA chain. */
459         ret = mxs_dma_go(channel);
460         if (ret) {
461                 int i;
462
463                 printf("MXS NAND: Error sending command %08lx\n", d->cmd.pio_words[0]);
464                 for (i = 0; i < nand_info->cmd_queue_len; i++) {
465                         printf("%02x ", nand_info->cmd_buf[i]);
466                 }
467                 printf("\n");
468         }
469
470         mxs_nand_return_dma_descs(nand_info);
471
472         /* Reset the command queue. */
473         nand_info->cmd_queue_len = 0;
474 }
475
476 /*
477  * Test if the NAND flash is ready.
478  */
479 static int mxs_nand_device_ready(struct mtd_info *mtd)
480 {
481         struct nand_chip *chip = mtd->priv;
482         struct mxs_nand_info *nand_info = chip->priv;
483         uint32_t tmp;
484
485         tmp = readl(&gpmi_regs->hw_gpmi_stat);
486         tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
487
488         return tmp & 1;
489 }
490
491 /*
492  * Select the NAND chip.
493  */
494 static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
495 {
496         struct nand_chip *nand = mtd->priv;
497         struct mxs_nand_info *nand_info = nand->priv;
498
499         nand_info->cur_chip = chip;
500 }
501
502 /*
503  * Handle block mark swapping.
504  *
505  * Note that, when this function is called, it doesn't know whether it's
506  * swapping the block mark, or swapping it *back* -- but it doesn't matter
507  * because the the operation is the same.
508  */
509 #ifndef CONFIG_NAND_MXS_NO_BBM_SWAP
510 static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
511                                         uint8_t *data_buf, uint8_t *oob_buf)
512 {
513         uint32_t bit_offset;
514         uint32_t buf_offset;
515
516         uint32_t src;
517         uint32_t dst;
518
519         bit_offset = mxs_nand_mark_bit_offset(mtd);
520         buf_offset = mxs_nand_mark_byte_offset(mtd);
521
522         /*
523          * Get the byte from the data area that overlays the block mark. Since
524          * the ECC engine applies its own view to the bits in the page, the
525          * physical block mark won't (in general) appear on a byte boundary in
526          * the data.
527          */
528         src = data_buf[buf_offset] >> bit_offset;
529         src |= data_buf[buf_offset + 1] << (8 - bit_offset);
530
531         dst = oob_buf[0];
532
533         debug("Swapping byte %02x @ %03x.%d with %02x @ %03x\n",
534                 src & 0xff, buf_offset, bit_offset, dst & 0xff, 0);
535
536         oob_buf[0] = src;
537
538         data_buf[buf_offset] &= ~(0xff << bit_offset);
539         data_buf[buf_offset + 1] &= 0xff << bit_offset;
540
541         data_buf[buf_offset] |= dst << bit_offset;
542         data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
543 }
544 #else
545 static inline void mxs_nand_swap_block_mark(struct mtd_info *mtd,
546                                         uint8_t *data_buf, uint8_t *oob_buf)
547 {
548 }
549 #endif
550
551 /*
552  * Read data from NAND.
553  */
554 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
555 {
556         struct nand_chip *nand = mtd->priv;
557         struct mxs_nand_info *nand_info = nand->priv;
558         struct mxs_dma_desc *d;
559         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
560         int ret;
561
562         if (length > NAND_MAX_PAGESIZE) {
563                 printf("MXS NAND: DMA buffer too big\n");
564                 return;
565         }
566
567         if (!buf) {
568                 printf("MXS NAND: DMA buffer is NULL\n");
569                 return;
570         }
571
572         memset(buf, 0xee, length);
573
574         /* Compile the DMA descriptor - a descriptor that reads data. */
575         d = mxs_nand_get_dma_desc(nand_info);
576         d->cmd.data =
577                 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
578                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
579                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
580                 (length << MXS_DMA_DESC_BYTES_OFFSET);
581
582         d->cmd.address = (dma_addr_t)nand_info->data_buf;
583
584         d->cmd.pio_words[0] =
585                 GPMI_CTRL0_COMMAND_MODE_READ |
586                 GPMI_CTRL0_WORD_LENGTH |
587                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
588                 GPMI_CTRL0_ADDRESS_NAND_DATA |
589                 length;
590
591         mxs_dma_desc_append(channel, d);
592 #ifndef CONFIG_SOC_MX6Q
593         /*
594          * A DMA descriptor that waits for the command to end and the chip to
595          * become ready.
596          *
597          * I think we actually should *not* be waiting for the chip to become
598          * ready because, after all, we don't care. I think the original code
599          * did that and no one has re-thought it yet.
600          */
601         d = mxs_nand_get_dma_desc(nand_info);
602         d->cmd.data =
603                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
604                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
605                 MXS_DMA_DESC_WAIT4END | (4 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
606
607         d->cmd.address = 0;
608
609         d->cmd.pio_words[0] =
610                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
611                 GPMI_CTRL0_WORD_LENGTH |
612                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
613                 GPMI_CTRL0_ADDRESS_NAND_DATA;
614
615         mxs_dma_desc_append(channel, d);
616 #endif
617         /* Execute the DMA chain. */
618         ret = mxs_dma_go(channel);
619         if (ret) {
620                 printf("%s: DMA read error\n", __func__);
621                 goto rtn;
622         }
623
624         /* Invalidate caches */
625         mxs_nand_inval_data_buf(nand_info);
626
627         memcpy(buf, nand_info->data_buf, length);
628
629 rtn:
630         mxs_nand_return_dma_descs(nand_info);
631 }
632
633 /*
634  * Write data to NAND.
635  */
636 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
637                                 int length)
638 {
639         struct nand_chip *nand = mtd->priv;
640         struct mxs_nand_info *nand_info = nand->priv;
641         struct mxs_dma_desc *d;
642         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
643         int ret;
644
645         if (length > NAND_MAX_PAGESIZE) {
646                 printf("MXS NAND: DMA buffer too big\n");
647                 return;
648         }
649
650         if (!buf) {
651                 printf("MXS NAND: DMA buffer is NULL\n");
652                 return;
653         }
654
655         memcpy(nand_info->data_buf, buf, length);
656
657         /* Compile the DMA descriptor - a descriptor that writes data. */
658         d = mxs_nand_get_dma_desc(nand_info);
659         d->cmd.data =
660                 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
661                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
662                 (4 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
663                 (length << MXS_DMA_DESC_BYTES_OFFSET);
664
665         d->cmd.address = (dma_addr_t)nand_info->data_buf;
666
667         d->cmd.pio_words[0] =
668                 GPMI_CTRL0_COMMAND_MODE_WRITE |
669                 GPMI_CTRL0_WORD_LENGTH |
670                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
671                 GPMI_CTRL0_ADDRESS_NAND_DATA |
672                 length;
673
674         mxs_dma_desc_append(channel, d);
675
676         /* Flush caches */
677         mxs_nand_flush_data_buf(nand_info);
678
679         /* Execute the DMA chain. */
680         ret = mxs_dma_go(channel);
681         if (ret)
682                 printf("%s: DMA write error\n", __func__);
683
684         mxs_nand_return_dma_descs(nand_info);
685 }
686
687 /*
688  * Read a single byte from NAND.
689  */
690 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
691 {
692         uint8_t buf;
693         mxs_nand_read_buf(mtd, &buf, 1);
694         return buf;
695 }
696
697 static void flush_buffers(struct mtd_info *mtd, struct mxs_nand_info *nand_info)
698 {
699         flush_dcache_range((unsigned long)nand_info->data_buf,
700                         (unsigned long)nand_info->data_buf +
701                         mtd->writesize);
702         flush_dcache_range((unsigned long)nand_info->oob_buf,
703                         (unsigned long)nand_info->oob_buf +
704                         mtd->oobsize);
705 }
706
707 /*
708  * Read a page from NAND.
709  */
710 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
711                                         uint8_t *buf, int oob_required,
712                                         int page)
713 {
714         struct mxs_nand_info *nand_info = nand->priv;
715         struct mxs_dma_desc *d;
716         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
717         uint32_t corrected = 0, failed = 0;
718         uint8_t *status;
719         int i, ret;
720
721         /* Compile the DMA descriptor - wait for ready. */
722         d = mxs_nand_get_dma_desc(nand_info);
723         d->cmd.data =
724                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
725                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
726                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
727
728         d->cmd.address = 0;
729
730         d->cmd.pio_words[0] =
731                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
732                 GPMI_CTRL0_WORD_LENGTH |
733                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
734                 GPMI_CTRL0_ADDRESS_NAND_DATA;
735
736         mxs_dma_desc_append(channel, d);
737
738         /* Compile the DMA descriptor - enable the BCH block and read. */
739         d = mxs_nand_get_dma_desc(nand_info);
740         d->cmd.data =
741                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
742                 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
743
744         d->cmd.address = 0;
745
746         d->cmd.pio_words[0] =
747                 GPMI_CTRL0_COMMAND_MODE_READ |
748                 GPMI_CTRL0_WORD_LENGTH |
749                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
750                 GPMI_CTRL0_ADDRESS_NAND_DATA |
751                 (mtd->writesize + mtd->oobsize);
752         d->cmd.pio_words[1] = 0;
753         d->cmd.pio_words[2] =
754                 GPMI_ECCCTRL_ENABLE_ECC |
755                 GPMI_ECCCTRL_ECC_CMD_DECODE |
756                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
757         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
758         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
759         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
760
761         flush_buffers(mtd, nand_info);
762
763         mxs_dma_desc_append(channel, d);
764
765         /* Compile the DMA descriptor - disable the BCH block. */
766         d = mxs_nand_get_dma_desc(nand_info);
767         d->cmd.data =
768                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
769                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
770                 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
771
772         d->cmd.address = 0;
773
774         d->cmd.pio_words[0] =
775                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
776                 GPMI_CTRL0_WORD_LENGTH |
777                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
778                 GPMI_CTRL0_ADDRESS_NAND_DATA |
779                 (mtd->writesize + mtd->oobsize);
780         d->cmd.pio_words[1] = 0;
781         d->cmd.pio_words[2] = 0;
782
783         mxs_dma_desc_append(channel, d);
784
785         /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
786         d = mxs_nand_get_dma_desc(nand_info);
787         d->cmd.data =
788                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
789                 MXS_DMA_DESC_DEC_SEM;
790
791         d->cmd.address = 0;
792
793         mxs_dma_desc_append(channel, d);
794
795         /* Execute the DMA chain. */
796         ret = mxs_dma_go(channel);
797         if (ret) {
798                 printf("%s: DMA read error\n", __func__);
799                 goto rtn;
800         }
801
802         ret = mxs_nand_wait_for_bch_complete();
803         if (ret) {
804                 printf("MXS NAND: BCH read timeout\n");
805                 goto rtn;
806         }
807
808         /* Invalidate caches */
809         mxs_nand_inval_data_buf(nand_info);
810
811         /* Read DMA completed, now do the mark swapping. */
812         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
813
814         /* Loop over status bytes, accumulating ECC status. */
815         status = nand_info->oob_buf + mxs_nand_aux_status_offset();
816         for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd); i++) {
817                 if (status[i] == 0x00)
818                         continue;
819
820                 if (status[i] == 0xff)
821                         continue;
822
823                 if (status[i] == 0xfe) {
824                         failed++;
825                         continue;
826                 }
827
828                 corrected += status[i];
829         }
830
831         /* Propagate ECC status to the owning MTD. */
832         mtd->ecc_stats.failed += failed;
833         mtd->ecc_stats.corrected += corrected;
834
835         /*
836          * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
837          * details about our policy for delivering the OOB.
838          *
839          * We fill the caller's buffer with set bits, and then copy the block
840          * mark to the caller's buffer. Note that, if block mark swapping was
841          * necessary, it has already been done, so we can rely on the first
842          * byte of the auxiliary buffer to contain the block mark.
843          */
844         memset(nand->oob_poi, 0xff, mtd->oobsize);
845
846         nand->oob_poi[0] = nand_info->oob_buf[0];
847
848         memcpy(buf, nand_info->data_buf, mtd->writesize);
849
850 rtn:
851         mxs_nand_return_dma_descs(nand_info);
852
853         return ret;
854 }
855
856 /*
857  * Write a page to NAND.
858  */
859 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
860                                 struct nand_chip *nand, const uint8_t *buf,
861                                 int oob_required)
862 {
863         struct mxs_nand_info *nand_info = nand->priv;
864         struct mxs_dma_desc *d;
865         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
866         int ret;
867
868         memcpy(nand_info->data_buf, buf, mtd->writesize);
869         memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
870
871         /* Handle block mark swapping. */
872         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
873
874         /* Compile the DMA descriptor - write data. */
875         d = mxs_nand_get_dma_desc(nand_info);
876         d->cmd.data =
877                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
878                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
879                 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
880
881         d->cmd.address = 0;
882
883         d->cmd.pio_words[0] =
884                 GPMI_CTRL0_COMMAND_MODE_WRITE |
885                 GPMI_CTRL0_WORD_LENGTH |
886                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
887                 GPMI_CTRL0_ADDRESS_NAND_DATA;
888         d->cmd.pio_words[1] = 0;
889         d->cmd.pio_words[2] =
890                 GPMI_ECCCTRL_ENABLE_ECC |
891                 GPMI_ECCCTRL_ECC_CMD_ENCODE |
892                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
893         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
894         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
895         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
896
897         flush_buffers(mtd, nand_info);
898
899         mxs_dma_desc_append(channel, d);
900
901         /* Flush caches */
902         mxs_nand_flush_data_buf(nand_info);
903
904         /* Execute the DMA chain. */
905         ret = mxs_dma_go(channel);
906         if (ret) {
907                 printf("%s: DMA write error\n", __func__);
908                 goto rtn;
909         }
910
911         ret = mxs_nand_wait_for_bch_complete();
912         if (ret) {
913                 printf("%s: BCH write timeout\n", __func__);
914                 goto rtn;
915         }
916
917 rtn:
918         mxs_nand_return_dma_descs(nand_info);
919         return 0;
920 }
921
922 /*
923  * Read OOB from NAND.
924  *
925  * This function is a veneer that replaces the function originally installed by
926  * the NAND Flash MTD code.
927  */
928 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
929                                         struct mtd_oob_ops *ops)
930 {
931         struct nand_chip *chip = mtd->priv;
932         struct mxs_nand_info *nand_info = chip->priv;
933         int ret;
934
935         if (ops->mode == MTD_OPS_RAW)
936                 nand_info->raw_oob_mode = 1;
937         else
938                 nand_info->raw_oob_mode = 0;
939
940         ret = nand_info->hooked_read_oob(mtd, from, ops);
941
942         nand_info->raw_oob_mode = 0;
943
944         return ret;
945 }
946
947 /*
948  * Write OOB to NAND.
949  *
950  * This function is a veneer that replaces the function originally installed by
951  * the NAND Flash MTD code.
952  */
953 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
954                                         struct mtd_oob_ops *ops)
955 {
956         struct nand_chip *chip = mtd->priv;
957         struct mxs_nand_info *nand_info = chip->priv;
958         int ret;
959
960         if (ops->mode == MTD_OPS_RAW)
961                 nand_info->raw_oob_mode = 1;
962         else
963                 nand_info->raw_oob_mode = 0;
964
965         ret = nand_info->hooked_write_oob(mtd, to, ops);
966
967         nand_info->raw_oob_mode = 0;
968
969         return ret;
970 }
971
972 /*
973  * Mark a block bad in NAND.
974  *
975  * This function is a veneer that replaces the function originally installed by
976  * the NAND Flash MTD code.
977  */
978 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
979 {
980         struct nand_chip *chip = mtd->priv;
981         struct mxs_nand_info *nand_info = chip->priv;
982         int ret;
983
984         nand_info->marking_block_bad = 1;
985
986         ret = nand_info->hooked_block_markbad(mtd, ofs);
987
988         nand_info->marking_block_bad = 0;
989
990         return ret;
991 }
992
993 /*
994  * There are several places in this driver where we have to handle the OOB and
995  * block marks. This is the function where things are the most complicated, so
996  * this is where we try to explain it all. All the other places refer back to
997  * here.
998  *
999  * These are the rules, in order of decreasing importance:
1000  *
1001  * 1) Nothing the caller does can be allowed to imperil the block mark, so all
1002  *    write operations take measures to protect it.
1003  *
1004  * 2) In read operations, the first byte of the OOB we return must reflect the
1005  *    true state of the block mark, no matter where that block mark appears in
1006  *    the physical page.
1007  *
1008  * 3) ECC-based read operations return an OOB full of set bits (since we never
1009  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1010  *    return).
1011  *
1012  * 4) "Raw" read operations return a direct view of the physical bytes in the
1013  *    page, using the conventional definition of which bytes are data and which
1014  *    are OOB. This gives the caller a way to see the actual, physical bytes
1015  *    in the page, without the distortions applied by our ECC engine.
1016  *
1017  * What we do for this specific read operation depends on whether we're doing
1018  * "raw" read, or an ECC-based read.
1019  *
1020  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1021  * easy. When reading a page, for example, the NAND Flash MTD code calls our
1022  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1023  * ECC-based or raw view of the page is implicit in which function it calls
1024  * (there is a similar pair of ECC-based/raw functions for writing).
1025  *
1026  * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1027  * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1028  * caller wants an ECC-based or raw view of the page is not propagated down to
1029  * this driver.
1030  *
1031  * Since our OOB *is* covered by ECC, we need this information. So, we hook the
1032  * ecc.read_oob and ecc.write_oob function pointers in the owning
1033  * struct mtd_info with our own functions. These hook functions set the
1034  * raw_oob_mode field so that, when control finally arrives here, we'll know
1035  * what to do.
1036  */
1037 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
1038                                 int page)
1039 {
1040         struct mxs_nand_info *nand_info = nand->priv;
1041
1042         /*
1043          * First, fill in the OOB buffer. If we're doing a raw read, we need to
1044          * get the bytes from the physical page. If we're not doing a raw read,
1045          * we need to fill the buffer with set bits.
1046          */
1047         if (nand_info->raw_oob_mode) {
1048                 /*
1049                  * If control arrives here, we're doing a "raw" read. Send the
1050                  * command to read the conventional OOB and read it.
1051                  */
1052                 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1053                 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1054         } else {
1055                 /*
1056                  * If control arrives here, we're not doing a "raw" read. Fill
1057                  * the OOB buffer with set bits and correct the block mark.
1058                  */
1059                 memset(nand->oob_poi, 0xff, mtd->oobsize);
1060
1061                 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1062                 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1063         }
1064
1065         return 0;
1066
1067 }
1068
1069 /*
1070  * Write OOB data to NAND.
1071  */
1072 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1073                                         int page)
1074 {
1075         struct mxs_nand_info *nand_info = nand->priv;
1076         uint8_t block_mark = 0;
1077
1078         /*
1079          * There are fundamental incompatibilities between the i.MX GPMI NFC and
1080          * the NAND Flash MTD model that make it essentially impossible to write
1081          * the out-of-band bytes.
1082          *
1083          * We permit *ONE* exception. If the *intent* of writing the OOB is to
1084          * mark a block bad, we can do that.
1085          */
1086
1087         if (!nand_info->marking_block_bad) {
1088                 printf("NXS NAND: Writing OOB isn't supported\n");
1089                 return -EIO;
1090         }
1091
1092         /* Write the block mark. */
1093         nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1094         nand->write_buf(mtd, &block_mark, 1);
1095         nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1096
1097         /* Check if it worked. */
1098         if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1099                 return -EIO;
1100
1101         return 0;
1102 }
1103
1104 /*
1105  * Claims all blocks are good.
1106  *
1107  * In principle, this function is *only* called when the NAND Flash MTD system
1108  * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1109  * the driver for bad block information.
1110  *
1111  * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1112  * this function is *only* called when we take it away.
1113  *
1114  * Thus, this function is only called when we want *all* blocks to look good,
1115  * so it *always* return success.
1116  */
1117 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
1118 {
1119         return 0;
1120 }
1121
1122 /*
1123  * Nominally, the purpose of this function is to look for or create the bad
1124  * block table. In fact, since the we call this function at the very end of
1125  * the initialization process started by nand_scan(), and we don't have a
1126  * more formal mechanism, we "hook" this function to continue init process.
1127  *
1128  * At this point, the physical NAND Flash chips have been identified and
1129  * counted, so we know the physical geometry. This enables us to make some
1130  * important configuration decisions.
1131  *
1132  * The return value of this function propogates directly back to this driver's
1133  * call to nand_scan(). Anything other than zero will cause this driver to
1134  * tear everything down and declare failure.
1135  */
1136 static int mxs_nand_scan_bbt(struct mtd_info *mtd)
1137 {
1138         struct nand_chip *nand = mtd->priv;
1139         struct mxs_nand_info *nand_info = nand->priv;
1140         uint32_t tmp;
1141
1142         /* Configure BCH and set NFC geometry */
1143         if (readl(&bch_regs->hw_bch_ctrl_reg) &
1144                 (BCH_CTRL_SFTRST | BCH_CTRL_CLKGATE))
1145                 /* When booting from NAND the BCH engine will already
1146                  * be operational and obviously does not like being reset here.
1147                  * There will be occasional read errors upon boot when this
1148                  * reset is done.
1149                  */
1150                 mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1151         readl(&bch_regs->hw_bch_ctrl_reg);
1152
1153         debug("mtd->writesize=%d\n", mtd->writesize);
1154         debug("mtd->oobsize=%d\n", mtd->oobsize);
1155         debug("ecc_strength=%d\n", mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize));
1156
1157         /* Configure layout 0 */
1158         tmp = (mxs_nand_ecc_chunk_cnt(mtd) - 1)
1159                 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1160         tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1161         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1162                 << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1163         tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE
1164                 >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1165         writel(tmp, &bch_regs->hw_bch_flash0layout0);
1166
1167         tmp = (mtd->writesize + mtd->oobsize)
1168                 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1169         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1170                 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1171         tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE
1172                 >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1173         writel(tmp, &bch_regs->hw_bch_flash0layout1);
1174
1175         /* Set *all* chip selects to use layout 0 */
1176         writel(0, &bch_regs->hw_bch_layoutselect);
1177
1178         /* Enable BCH complete interrupt */
1179         writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1180
1181         /* Hook some operations at the MTD level. */
1182         if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1183                 nand_info->hooked_read_oob = mtd->_read_oob;
1184                 mtd->_read_oob = mxs_nand_hook_read_oob;
1185         }
1186
1187         if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1188                 nand_info->hooked_write_oob = mtd->_write_oob;
1189                 mtd->_write_oob = mxs_nand_hook_write_oob;
1190         }
1191
1192         if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1193                 nand_info->hooked_block_markbad = mtd->_block_markbad;
1194                 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1195         }
1196
1197         /* We use the reference implementation for bad block management. */
1198         return nand_default_bbt(mtd);
1199 }
1200
1201 /*
1202  * Allocate DMA buffers
1203  */
1204 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1205 {
1206         uint8_t *buf;
1207         const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1208
1209         nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1210
1211         /* DMA buffers */
1212         buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1213         if (!buf) {
1214                 printf("%s: Error allocating DMA buffers\n", __func__);
1215                 return -ENOMEM;
1216         }
1217
1218         memset(buf, 0, nand_info->data_buf_size);
1219
1220         nand_info->data_buf = buf;
1221         nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1222         /* Command buffers */
1223         nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1224                                 MXS_NAND_COMMAND_BUFFER_SIZE);
1225         if (!nand_info->cmd_buf) {
1226                 free(buf);
1227                 printf("MXS NAND: Error allocating command buffers\n");
1228                 return -ENOMEM;
1229         }
1230         memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1231         nand_info->cmd_queue_len = 0;
1232
1233         return 0;
1234 }
1235
1236 /*
1237  * Initializes the NFC hardware.
1238  */
1239 int mxs_nand_init(struct mxs_nand_info *info)
1240 {
1241         int ret;
1242         int i;
1243
1244         info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1245                                 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1246         if (!info->desc) {
1247                 printf("MXS NAND: Unable to allocate DMA descriptor table\n");
1248                 ret = -ENOMEM;
1249                 goto err1;
1250         }
1251
1252         mxs_dma_init();
1253
1254         /* Allocate the DMA descriptors. */
1255         for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1256                 info->desc[i] = mxs_dma_desc_alloc();
1257                 if (!info->desc[i]) {
1258                         printf("MXS NAND: Unable to allocate DMA descriptors\n");
1259                         ret = -ENOMEM;
1260                         goto err2;
1261                 }
1262         }
1263
1264         /* Init the DMA controller. */
1265         for (i = 0; i < CONFIG_SYS_NAND_MAX_CHIPS; i++) {
1266                 const int chan = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + i;
1267
1268                 ret = mxs_dma_init_channel(chan);
1269                 if (ret) {
1270                         printf("Failed to initialize DMA channel %d\n", chan);
1271                         goto err3;
1272                 }
1273         }
1274
1275         ret = mxs_nand_gpmi_init();
1276         if (ret)
1277                 goto err3;
1278
1279         return 0;
1280
1281 err3:
1282         for (--i; i >= 0; i--)
1283                 mxs_dma_release(i + MXS_DMA_CHANNEL_AHB_APBH_GPMI0);
1284         i = MXS_NAND_DMA_DESCRIPTOR_COUNT - 1;
1285 err2:
1286         free(info->desc);
1287         for (--i; i >= 0; i--)
1288                 mxs_dma_desc_free(info->desc[i]);
1289 err1:
1290         return ret;
1291 }
1292
1293 /*!
1294  * This function is called during the driver binding process.
1295  *
1296  * @param   pdev  the device structure used to store device specific
1297  *                information that is used by the suspend, resume and
1298  *                remove functions
1299  *
1300  * @return  The function always returns 0.
1301  */
1302 int board_nand_init(struct nand_chip *nand)
1303 {
1304         struct mxs_nand_info *nand_info;
1305         int err;
1306
1307         nand_info = calloc(1, sizeof(struct mxs_nand_info));
1308         if (!nand_info) {
1309                 printf("MXS NAND: Failed to allocate private data\n");
1310                 return -ENOMEM;
1311         }
1312
1313         err = mxs_nand_alloc_buffers(nand_info);
1314         if (err)
1315                 goto err1;
1316
1317         err = mxs_nand_init(nand_info);
1318         if (err)
1319                 goto err2;
1320
1321         memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1322
1323         nand->priv = nand_info;
1324         nand->options |= NAND_NO_SUBPAGE_WRITE;
1325 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1326         nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1327 #endif
1328         nand->cmd_ctrl          = mxs_nand_cmd_ctrl;
1329
1330         nand->dev_ready         = mxs_nand_device_ready;
1331         nand->select_chip       = mxs_nand_select_chip;
1332         nand->block_bad         = mxs_nand_block_bad;
1333         nand->scan_bbt          = mxs_nand_scan_bbt;
1334
1335         nand->read_byte         = mxs_nand_read_byte;
1336
1337         nand->read_buf          = mxs_nand_read_buf;
1338         nand->write_buf         = mxs_nand_write_buf;
1339
1340         nand->ecc.read_page     = mxs_nand_ecc_read_page;
1341         nand->ecc.write_page    = mxs_nand_ecc_write_page;
1342         nand->ecc.read_oob      = mxs_nand_ecc_read_oob;
1343         nand->ecc.write_oob     = mxs_nand_ecc_write_oob;
1344
1345         nand->ecc.layout        = &fake_ecc_layout;
1346         nand->ecc.mode          = NAND_ECC_HW;
1347         nand->ecc.bytes         = 9;
1348         nand->ecc.size          = 512;
1349         nand->ecc.strength      = 8;
1350
1351         return 0;
1352
1353 err2:
1354         free(nand_info->data_buf);
1355         free(nand_info->cmd_buf);
1356 err1:
1357         free(nand_info);
1358         return err;
1359 }