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