]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/mxs_nand.c
TX6 Release 2013-04-22
[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  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, write to the Free Software Foundation, Inc.,
25  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26  */
27 //#define DEBUG
28
29 #include <common.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/nand.h>
32 #include <linux/types.h>
33 #include <malloc.h>
34 #include <asm/errno.h>
35 #include <asm/io.h>
36 #include <asm/arch/clock.h>
37 #include <asm/arch/imx-regs.h>
38 #include <asm/arch/regs-bch.h>
39 #include <asm/arch/regs-gpmi.h>
40 #include <asm/arch/sys_proto.h>
41 #include <asm/arch/dma.h>
42
43 #define MXS_NAND_DMA_DESCRIPTOR_COUNT           4
44
45 #ifndef CONFIG_MX6Q
46 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE          512
47 #else
48 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE          (512 / 4)
49 #endif
50
51 #define MXS_NAND_METADATA_SIZE                  10
52
53 #define MXS_NAND_COMMAND_BUFFER_SIZE            32
54
55 /* BCH timeout in microseconds */
56 #define MXS_NAND_BCH_TIMEOUT                    10000
57
58 static struct bch_regs *bch_regs = (void *)BCH_BASE_ADDRESS;
59 static struct gpmi_regs *gpmi_regs = (void *)GPMI_BASE_ADDRESS;
60 struct mxs_nand_info {
61         int             cur_chip;
62
63         uint32_t        cmd_queue_len;
64         uint32_t        data_buf_size;
65
66         uint8_t         *cmd_buf;
67         uint8_t         *data_buf;
68         uint8_t         *oob_buf;
69
70         uint8_t         marking_block_bad;
71         uint8_t         raw_oob_mode;
72
73         /* Functions with altered behaviour */
74         int             (*hooked_read_oob)(struct mtd_info *mtd,
75                                 loff_t from, struct mtd_oob_ops *ops);
76         int             (*hooked_write_oob)(struct mtd_info *mtd,
77                                 loff_t to, struct mtd_oob_ops *ops);
78         int             (*hooked_block_markbad)(struct mtd_info *mtd,
79                                 loff_t ofs);
80
81         /* DMA descriptors */
82         struct mxs_dma_desc     **desc;
83         uint32_t                desc_index;
84 };
85
86 #ifdef DEBUG
87 #define dump_reg(b, r)  __dump_reg(&b->r, #r)
88 static inline void __dump_reg(void *addr, const char *name)
89 {
90         printf("%16s[%p]=%08x\n", name, addr, readl(addr));
91 }
92
93 #define dump_bch_reg(n) __dump_reg(&bch_regs->hw_bch_##n, #n)
94 #define dump_gpmi_reg(n) __dump_reg(&gpmi_regs->hw_gpmi_##n, #n)
95 static inline void dump_regs(void)
96 {
97         printf("BCH:\n");
98         dump_bch_reg(ctrl);
99         dump_bch_reg(status0);
100         dump_bch_reg(mode);
101         dump_bch_reg(debug0);
102         dump_bch_reg(dbgkesread);
103         dump_bch_reg(dbgcsferead);
104         dump_bch_reg(dbgsyndegread);
105         dump_bch_reg(dbgahbmread);
106         dump_bch_reg(blockname);
107         dump_bch_reg(version);
108
109         printf("\nGPMI:\n");
110         dump_gpmi_reg(ctrl0);
111         dump_gpmi_reg(eccctrl);
112         dump_gpmi_reg(ecccount);
113         dump_gpmi_reg(payload);
114         dump_gpmi_reg(auxiliary);
115         dump_gpmi_reg(ctrl1);
116         dump_gpmi_reg(data);
117         dump_gpmi_reg(stat);
118         dump_gpmi_reg(debug);
119         dump_gpmi_reg(version);
120         dump_gpmi_reg(debug2);
121         dump_gpmi_reg(debug3);
122 }
123
124 static inline int dbg_addr(void *addr)
125 {
126         if (((unsigned long)addr & ~0xfff) == BCH_BASE_ADDRESS)
127                 return 1;
128         return 1;
129 }
130
131 static inline u32 mxs_readl(void *addr,
132                         const char *fn, int ln)
133 {
134         u32 val = readl(addr);
135         static void *last_addr;
136         static u32 last_val;
137
138         if (!dbg_addr(addr))
139                 return val;
140
141         if (addr != last_addr || last_val != val) {
142                 printf("%s@%d: Read %08x from %p\n", fn, ln, val, addr);
143                 last_addr = addr;
144                 last_val = val;
145         }
146         return val;
147 }
148
149 static inline void mxs_writel(u32 val, void *addr,
150                         const char *fn, int ln)
151 {
152         if (dbg_addr(addr))
153                 printf("%s@%d: Writing %08x to %p...", fn, ln, val, addr);
154         writel(val, addr);
155         if (dbg_addr(addr))
156                 printf(" result: %08x\n", readl(addr));
157 }
158
159 #undef readl
160 #define readl(a) mxs_readl(a, __func__, __LINE__)
161
162 #undef writel
163 #define writel(v, a) mxs_writel(v, a, __func__, __LINE__)
164 static inline void memdump(const void *addr, size_t len)
165 {
166         const char *buf = addr;
167         int i;
168
169         for (i = 0; i < len; i++) {
170                 if (i % 16 == 0) {
171                         if (i > 0)
172                                 printf("\n");
173                         printf("%p:", &buf[i]);
174                 }
175                 printf(" %02x", buf[i]);
176         }
177         printf("\n");
178 }
179 #else
180 static inline void memdump(void *addr, size_t len)
181 {
182 }
183
184 static inline void dump_regs(void)
185 {
186 }
187 #endif
188
189 struct nand_ecclayout fake_ecc_layout;
190
191 /*
192  * Cache management functions
193  */
194 #ifndef CONFIG_SYS_DCACHE_OFF
195 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
196 {
197         uint32_t addr = (uint32_t)info->data_buf;
198
199         flush_dcache_range(addr, addr + info->data_buf_size);
200 }
201
202 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
203 {
204         uint32_t addr = (uint32_t)info->data_buf;
205
206         invalidate_dcache_range(addr, addr + info->data_buf_size);
207 }
208
209 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
210 {
211         uint32_t addr = (uint32_t)info->cmd_buf;
212
213         flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
214 }
215 #else
216 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
217 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
218 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
219 #endif
220
221 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
222 {
223         struct mxs_dma_desc *desc;
224
225         if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
226                 printf("MXS NAND: Too many DMA descriptors requested\n");
227                 return NULL;
228         }
229
230         desc = info->desc[info->desc_index];
231         info->desc_index++;
232
233         return desc;
234 }
235
236 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
237 {
238         int i;
239         struct mxs_dma_desc *desc;
240
241         for (i = 0; i < info->desc_index; i++) {
242                 desc = info->desc[i];
243                 memset(desc, 0, sizeof(struct mxs_dma_desc));
244                 desc->address = (dma_addr_t)desc;
245         }
246
247         info->desc_index = 0;
248 }
249
250 static uint32_t mxs_nand_ecc_chunk_cnt(struct mtd_info *mtd)
251 {
252         struct nand_chip *nand = mtd->priv;
253         return mtd->writesize / nand->ecc.size;
254 }
255
256 static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
257 {
258         return ecc_strength * 13;
259 }
260
261 static uint32_t mxs_nand_aux_status_offset(void)
262 {
263         return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
264 }
265
266 static int mxs_nand_gpmi_init(void)
267 {
268         int ret;
269
270         /* Reset the GPMI block. */
271         ret = mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
272         if (ret)
273                 return ret;
274
275         /*
276          * Choose NAND mode, set IRQ polarity, disable write protection and
277          * select BCH ECC.
278          */
279         clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
280                         GPMI_CTRL1_GPMI_MODE,
281                         GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
282                         GPMI_CTRL1_BCH_MODE);
283         writel(0x500 << 16, &gpmi_regs->hw_gpmi_timing1);
284         return 0;
285 }
286
287 static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
288                                                 uint32_t page_oob_size)
289 {
290         if (page_data_size == 2048)
291                 return 8;
292
293         if (page_data_size == 4096) {
294                 if (page_oob_size == 128)
295                         return 8;
296
297                 if (page_oob_size == 218)
298                         return 16;
299         }
300
301         return 0;
302 }
303
304 static inline uint32_t mxs_nand_get_mark_offset(uint32_t page_data_size,
305                                                 uint32_t ecc_strength)
306 {
307         uint32_t chunk_data_size_in_bits;
308         uint32_t chunk_ecc_size_in_bits;
309         uint32_t chunk_total_size_in_bits;
310         uint32_t block_mark_chunk_number;
311         uint32_t block_mark_chunk_bit_offset;
312         uint32_t block_mark_bit_offset;
313
314         chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
315         chunk_ecc_size_in_bits  = mxs_nand_ecc_size_in_bits(ecc_strength);
316
317         chunk_total_size_in_bits =
318                         chunk_data_size_in_bits + chunk_ecc_size_in_bits;
319
320         /* Compute the bit offset of the block mark within the physical page. */
321         block_mark_bit_offset = page_data_size * 8;
322
323         /* Subtract the metadata bits. */
324         block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
325
326         /*
327          * Compute the chunk number (starting at zero) in which the block mark
328          * appears.
329          */
330         block_mark_chunk_number =
331                         block_mark_bit_offset / chunk_total_size_in_bits;
332
333         /*
334          * Compute the bit offset of the block mark within its chunk, and
335          * validate it.
336          */
337         block_mark_chunk_bit_offset = block_mark_bit_offset -
338                         (block_mark_chunk_number * chunk_total_size_in_bits);
339
340         if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
341                 return 1;
342
343         /*
344          * Now that we know the chunk number in which the block mark appears,
345          * we can subtract all the ECC bits that appear before it.
346          */
347         block_mark_bit_offset -=
348                 block_mark_chunk_number * chunk_ecc_size_in_bits;
349
350         return block_mark_bit_offset;
351 }
352
353 static uint32_t mxs_nand_mark_byte_offset(struct mtd_info *mtd)
354 {
355         uint32_t ecc_strength;
356         ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
357         return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) >> 3;
358 }
359
360 static uint32_t mxs_nand_mark_bit_offset(struct mtd_info *mtd)
361 {
362         uint32_t ecc_strength;
363         ecc_strength = mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize);
364         return mxs_nand_get_mark_offset(mtd->writesize, ecc_strength) & 0x7;
365 }
366
367 /*
368  * Wait for BCH complete IRQ and clear the IRQ
369  */
370 static int mxs_nand_wait_for_bch_complete(void)
371 {
372         int timeout = MXS_NAND_BCH_TIMEOUT;
373         int ret;
374
375         ret = mxs_wait_mask_set(&bch_regs->hw_bch_ctrl_reg,
376                 BCH_CTRL_COMPLETE_IRQ, timeout);
377         if (ret) {
378                 debug("%s@%d: %d\n", __func__, __LINE__, ret);
379                 mxs_nand_gpmi_init();
380         }
381
382         writel(BCH_CTRL_COMPLETE_IRQ, &bch_regs->hw_bch_ctrl_clr);
383
384         return ret;
385 }
386
387 /*
388  * This is the function that we install in the cmd_ctrl function pointer of the
389  * owning struct nand_chip. The only functions in the reference implementation
390  * that use these functions pointers are cmdfunc and select_chip.
391  *
392  * In this driver, we implement our own select_chip, so this function will only
393  * be called by the reference implementation's cmdfunc. For this reason, we can
394  * ignore the chip enable bit and concentrate only on sending bytes to the NAND
395  * Flash.
396  */
397 static void mxs_nand_cmd_ctrl(struct mtd_info *mtd, int data, unsigned int ctrl)
398 {
399         struct nand_chip *nand = mtd->priv;
400         struct mxs_nand_info *nand_info = nand->priv;
401         struct mxs_dma_desc *d;
402         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
403         int ret;
404
405         /*
406          * If this condition is true, something is _VERY_ wrong in MTD
407          * subsystem!
408          */
409         if (nand_info->cmd_queue_len == MXS_NAND_COMMAND_BUFFER_SIZE) {
410                 printf("MXS NAND: Command queue too long\n");
411                 return;
412         }
413
414         /*
415          * Every operation begins with a command byte and a series of zero or
416          * more address bytes. These are distinguished by either the Address
417          * Latch Enable (ALE) or Command Latch Enable (CLE) signals being
418          * asserted. When MTD is ready to execute the command, it will
419          * deasert both latch enables.
420          *
421          * Rather than run a separate DMA operation for every single byte, we
422          * queue them up and run a single DMA operation for the entire series
423          * of command and data bytes.
424          */
425         if (ctrl & (NAND_ALE | NAND_CLE)) {
426                 if (data != NAND_CMD_NONE)
427                         nand_info->cmd_buf[nand_info->cmd_queue_len++] = data;
428                 return;
429         }
430
431         /*
432          * If control arrives here, MTD has deasserted both the ALE and CLE,
433          * which means it's ready to run an operation. Check if we have any
434          * bytes to send.
435          */
436         if (nand_info->cmd_queue_len == 0)
437                 return;
438
439         /* Compile the DMA descriptor -- a descriptor that sends command. */
440         d = mxs_nand_get_dma_desc(nand_info);
441         d->cmd.data =
442                 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
443                 MXS_DMA_DESC_CHAIN | MXS_DMA_DESC_DEC_SEM |
444                 MXS_DMA_DESC_WAIT4END | (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
445                 (nand_info->cmd_queue_len << MXS_DMA_DESC_BYTES_OFFSET);
446
447         d->cmd.address = (dma_addr_t)nand_info->cmd_buf;
448
449         d->cmd.pio_words[0] =
450                 GPMI_CTRL0_COMMAND_MODE_WRITE |
451                 GPMI_CTRL0_WORD_LENGTH |
452                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
453                 GPMI_CTRL0_ADDRESS_NAND_CLE |
454                 GPMI_CTRL0_ADDRESS_INCREMENT |
455                 nand_info->cmd_queue_len;
456
457         mxs_dma_desc_append(channel, d);
458
459         /* Flush caches */
460         mxs_nand_flush_cmd_buf(nand_info);
461
462         /* Execute the DMA chain. */
463         ret = mxs_dma_go(channel);
464         if (ret) {
465                 int i;
466
467                 printf("MXS NAND: Error sending command %08lx\n", d->cmd.pio_words[0]);
468                 for (i = 0; i < nand_info->cmd_queue_len; i++) {
469                         printf("%02x ", nand_info->cmd_buf[i]);
470                 }
471                 printf("\n");
472         }
473
474         mxs_nand_return_dma_descs(nand_info);
475
476         /* Reset the command queue. */
477         nand_info->cmd_queue_len = 0;
478 }
479
480 /*
481  * Test if the NAND flash is ready.
482  */
483 static int mxs_nand_device_ready(struct mtd_info *mtd)
484 {
485         struct nand_chip *chip = mtd->priv;
486         struct mxs_nand_info *nand_info = chip->priv;
487         uint32_t tmp;
488
489         tmp = readl(&gpmi_regs->hw_gpmi_stat);
490         tmp >>= (GPMI_STAT_READY_BUSY_OFFSET + nand_info->cur_chip);
491
492         return tmp & 1;
493 }
494
495 /*
496  * Select the NAND chip.
497  */
498 static void mxs_nand_select_chip(struct mtd_info *mtd, int chip)
499 {
500         struct nand_chip *nand = mtd->priv;
501         struct mxs_nand_info *nand_info = nand->priv;
502
503         nand_info->cur_chip = chip;
504 }
505
506 /*
507  * Handle block mark swapping.
508  *
509  * Note that, when this function is called, it doesn't know whether it's
510  * swapping the block mark, or swapping it *back* -- but it doesn't matter
511  * because the the operation is the same.
512  */
513 static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
514                                         uint8_t *data_buf, uint8_t *oob_buf)
515 {
516         uint32_t bit_offset;
517         uint32_t buf_offset;
518
519         uint32_t src;
520         uint32_t dst;
521 return;
522         bit_offset = mxs_nand_mark_bit_offset(mtd);
523         buf_offset = mxs_nand_mark_byte_offset(mtd);
524
525         /*
526          * Get the byte from the data area that overlays the block mark. Since
527          * the ECC engine applies its own view to the bits in the page, the
528          * physical block mark won't (in general) appear on a byte boundary in
529          * the data.
530          */
531         src = data_buf[buf_offset] >> bit_offset;
532         src |= data_buf[buf_offset + 1] << (8 - bit_offset);
533
534         dst = oob_buf[0];
535
536         debug("Swapping byte %02x @ %03x.%d with %02x @ %03x\n",
537                 src & 0xff, buf_offset, bit_offset, dst & 0xff, 0);
538
539         oob_buf[0] = src;
540
541         data_buf[buf_offset] &= ~(0xff << bit_offset);
542         data_buf[buf_offset + 1] &= 0xff << bit_offset;
543
544         data_buf[buf_offset] |= dst << bit_offset;
545         data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
546 }
547
548 /*
549  * Read data from NAND.
550  */
551 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
552 {
553         struct nand_chip *nand = mtd->priv;
554         struct mxs_nand_info *nand_info = nand->priv;
555         struct mxs_dma_desc *d;
556         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
557         int ret;
558
559         if (length > NAND_MAX_PAGESIZE) {
560                 printf("MXS NAND: DMA buffer too big\n");
561                 return;
562         }
563
564         if (!buf) {
565                 printf("MXS NAND: DMA buffer is NULL\n");
566                 return;
567         }
568
569         memset(buf, 0xee, length);
570
571         /* Compile the DMA descriptor - a descriptor that reads data. */
572         d = mxs_nand_get_dma_desc(nand_info);
573         d->cmd.data =
574                 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
575                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
576                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
577                 (length << MXS_DMA_DESC_BYTES_OFFSET);
578
579         d->cmd.address = (dma_addr_t)nand_info->data_buf;
580
581         d->cmd.pio_words[0] =
582                 GPMI_CTRL0_COMMAND_MODE_READ |
583                 GPMI_CTRL0_WORD_LENGTH |
584                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
585                 GPMI_CTRL0_ADDRESS_NAND_DATA |
586                 length;
587
588         mxs_dma_desc_append(channel, d);
589 #ifndef CONFIG_MX6Q
590         /*
591          * A DMA descriptor that waits for the command to end and the chip to
592          * become ready.
593          *
594          * I think we actually should *not* be waiting for the chip to become
595          * ready because, after all, we don't care. I think the original code
596          * did that and no one has re-thought it yet.
597          */
598         d = mxs_nand_get_dma_desc(nand_info);
599         d->cmd.data =
600                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
601                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
602                 MXS_DMA_DESC_WAIT4END | (4 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
603
604         d->cmd.address = 0;
605
606         d->cmd.pio_words[0] =
607                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
608                 GPMI_CTRL0_WORD_LENGTH |
609                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
610                 GPMI_CTRL0_ADDRESS_NAND_DATA;
611
612         mxs_dma_desc_append(channel, d);
613 #endif
614         /* Execute the DMA chain. */
615         ret = mxs_dma_go(channel);
616         if (ret) {
617                 printf("%s: DMA read error\n", __func__);
618                 goto rtn;
619         }
620
621         /* Invalidate caches */
622         mxs_nand_inval_data_buf(nand_info);
623
624         memcpy(buf, nand_info->data_buf, length);
625
626 rtn:
627         mxs_nand_return_dma_descs(nand_info);
628 }
629
630 /*
631  * Write data to NAND.
632  */
633 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
634                                 int length)
635 {
636         struct nand_chip *nand = mtd->priv;
637         struct mxs_nand_info *nand_info = nand->priv;
638         struct mxs_dma_desc *d;
639         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
640         int ret;
641
642         if (length > NAND_MAX_PAGESIZE) {
643                 printf("MXS NAND: DMA buffer too big\n");
644                 return;
645         }
646
647         if (!buf) {
648                 printf("MXS NAND: DMA buffer is NULL\n");
649                 return;
650         }
651
652         memcpy(nand_info->data_buf, buf, length);
653
654         /* Compile the DMA descriptor - a descriptor that writes data. */
655         d = mxs_nand_get_dma_desc(nand_info);
656         d->cmd.data =
657                 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
658                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
659                 (4 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
660                 (length << MXS_DMA_DESC_BYTES_OFFSET);
661
662         d->cmd.address = (dma_addr_t)nand_info->data_buf;
663
664         d->cmd.pio_words[0] =
665                 GPMI_CTRL0_COMMAND_MODE_WRITE |
666                 GPMI_CTRL0_WORD_LENGTH |
667                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
668                 GPMI_CTRL0_ADDRESS_NAND_DATA |
669                 length;
670
671         mxs_dma_desc_append(channel, d);
672
673         /* Flush caches */
674         mxs_nand_flush_data_buf(nand_info);
675
676         /* Execute the DMA chain. */
677         ret = mxs_dma_go(channel);
678         if (ret)
679                 printf("%s: DMA write error\n", __func__);
680
681         mxs_nand_return_dma_descs(nand_info);
682 }
683
684 /*
685  * Read a single byte from NAND.
686  */
687 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
688 {
689         uint8_t buf;
690         mxs_nand_read_buf(mtd, &buf, 1);
691         return buf;
692 }
693
694 static void flush_buffers(struct mtd_info *mtd, struct mxs_nand_info *nand_info)
695 {
696         flush_dcache_range((unsigned long)nand_info->data_buf,
697                         (unsigned long)nand_info->data_buf +
698                         mtd->writesize);
699         flush_dcache_range((unsigned long)nand_info->oob_buf,
700                         (unsigned long)nand_info->oob_buf +
701                         mtd->oobsize);
702 }
703
704 /*
705  * Read a page from NAND.
706  */
707 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
708                                         uint8_t *buf, int page)
709 {
710         struct mxs_nand_info *nand_info = nand->priv;
711         struct mxs_dma_desc *d;
712         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
713         uint32_t corrected = 0, failed = 0;
714         uint8_t *status;
715         int i, ret;
716
717         /* Compile the DMA descriptor - wait for ready. */
718         d = mxs_nand_get_dma_desc(nand_info);
719         d->cmd.data =
720                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
721                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
722                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
723
724         d->cmd.address = 0;
725
726         d->cmd.pio_words[0] =
727                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
728                 GPMI_CTRL0_WORD_LENGTH |
729                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
730                 GPMI_CTRL0_ADDRESS_NAND_DATA;
731
732         mxs_dma_desc_append(channel, d);
733
734         /* Compile the DMA descriptor - enable the BCH block and read. */
735         d = mxs_nand_get_dma_desc(nand_info);
736         d->cmd.data =
737                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
738                 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
739
740         d->cmd.address = 0;
741
742         d->cmd.pio_words[0] =
743                 GPMI_CTRL0_COMMAND_MODE_READ |
744                 GPMI_CTRL0_WORD_LENGTH |
745                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
746                 GPMI_CTRL0_ADDRESS_NAND_DATA |
747                 (mtd->writesize + mtd->oobsize);
748         d->cmd.pio_words[1] = 0;
749         d->cmd.pio_words[2] =
750                 GPMI_ECCCTRL_ENABLE_ECC |
751                 GPMI_ECCCTRL_ECC_CMD_DECODE |
752                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
753         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
754         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
755         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
756
757         flush_buffers(mtd, nand_info);
758
759         mxs_dma_desc_append(channel, d);
760
761         /* Compile the DMA descriptor - disable the BCH block. */
762         d = mxs_nand_get_dma_desc(nand_info);
763         d->cmd.data =
764                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
765                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
766                 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
767
768         d->cmd.address = 0;
769
770         d->cmd.pio_words[0] =
771                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
772                 GPMI_CTRL0_WORD_LENGTH |
773                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
774                 GPMI_CTRL0_ADDRESS_NAND_DATA |
775                 (mtd->writesize + mtd->oobsize);
776         d->cmd.pio_words[1] = 0;
777         d->cmd.pio_words[2] = 0;
778
779         mxs_dma_desc_append(channel, d);
780
781         /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
782         d = mxs_nand_get_dma_desc(nand_info);
783         d->cmd.data =
784                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
785                 MXS_DMA_DESC_DEC_SEM;
786
787         d->cmd.address = 0;
788
789         mxs_dma_desc_append(channel, d);
790
791         /* Execute the DMA chain. */
792         ret = mxs_dma_go(channel);
793         if (ret) {
794                 printf("%s: DMA read error\n", __func__);
795                 goto rtn;
796         }
797
798         ret = mxs_nand_wait_for_bch_complete();
799         if (ret) {
800                 printf("MXS NAND: BCH read timeout\n");
801                 goto rtn;
802         }
803
804         /* Invalidate caches */
805         mxs_nand_inval_data_buf(nand_info);
806
807         /* Read DMA completed, now do the mark swapping. */
808         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
809
810         /* Loop over status bytes, accumulating ECC status. */
811         status = nand_info->oob_buf + mxs_nand_aux_status_offset();
812         for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd); i++) {
813                 if (status[i] == 0x00)
814                         continue;
815
816                 if (status[i] == 0xff)
817                         continue;
818
819                 if (status[i] == 0xfe) {
820                         failed++;
821                         continue;
822                 }
823
824                 corrected += status[i];
825         }
826
827         /* Propagate ECC status to the owning MTD. */
828         mtd->ecc_stats.failed += failed;
829         mtd->ecc_stats.corrected += corrected;
830
831         /*
832          * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
833          * details about our policy for delivering the OOB.
834          *
835          * We fill the caller's buffer with set bits, and then copy the block
836          * mark to the caller's buffer. Note that, if block mark swapping was
837          * necessary, it has already been done, so we can rely on the first
838          * byte of the auxiliary buffer to contain the block mark.
839          */
840         memset(nand->oob_poi, 0xff, mtd->oobsize);
841
842         nand->oob_poi[0] = nand_info->oob_buf[0];
843
844         memcpy(buf, nand_info->data_buf, mtd->writesize);
845
846 rtn:
847         mxs_nand_return_dma_descs(nand_info);
848
849         return ret;
850 }
851
852 /*
853  * Write a page to NAND.
854  */
855 static void mxs_nand_ecc_write_page(struct mtd_info *mtd,
856                                 struct nand_chip *nand, const uint8_t *buf)
857 {
858         struct mxs_nand_info *nand_info = nand->priv;
859         struct mxs_dma_desc *d;
860         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
861         int ret;
862
863         memcpy(nand_info->data_buf, buf, mtd->writesize);
864         memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
865
866         /* Handle block mark swapping. */
867         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
868
869         /* Compile the DMA descriptor - write data. */
870         d = mxs_nand_get_dma_desc(nand_info);
871         d->cmd.data =
872                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
873                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
874                 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
875
876         d->cmd.address = 0;
877
878         d->cmd.pio_words[0] =
879                 GPMI_CTRL0_COMMAND_MODE_WRITE |
880                 GPMI_CTRL0_WORD_LENGTH |
881                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
882                 GPMI_CTRL0_ADDRESS_NAND_DATA;
883         d->cmd.pio_words[1] = 0;
884         d->cmd.pio_words[2] =
885                 GPMI_ECCCTRL_ENABLE_ECC |
886                 GPMI_ECCCTRL_ECC_CMD_ENCODE |
887                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
888         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
889         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
890         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
891
892         flush_buffers(mtd, nand_info);
893
894         mxs_dma_desc_append(channel, d);
895
896         /* Flush caches */
897         mxs_nand_flush_data_buf(nand_info);
898
899         /* Execute the DMA chain. */
900         ret = mxs_dma_go(channel);
901         if (ret) {
902                 printf("%s: DMA write error\n", __func__);
903                 goto rtn;
904         }
905
906         ret = mxs_nand_wait_for_bch_complete();
907         if (ret) {
908                 printf("%s: BCH write timeout\n", __func__);
909                 goto rtn;
910         }
911
912 rtn:
913         mxs_nand_return_dma_descs(nand_info);
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_OOB_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_OOB_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, int cmd)
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         writel(tmp, &bch_regs->hw_bch_flash0layout0);
1159
1160         tmp = (mtd->writesize + mtd->oobsize)
1161                 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1162         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1163                 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1164         tmp |= MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
1165         writel(tmp, &bch_regs->hw_bch_flash0layout1);
1166
1167         /* Set *all* chip selects to use layout 0 */
1168         writel(0, &bch_regs->hw_bch_layoutselect);
1169
1170         /* Enable BCH complete interrupt */
1171         writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1172
1173         /* Hook some operations at the MTD level. */
1174         if (mtd->read_oob != mxs_nand_hook_read_oob) {
1175                 nand_info->hooked_read_oob = mtd->read_oob;
1176                 mtd->read_oob = mxs_nand_hook_read_oob;
1177         }
1178
1179         if (mtd->write_oob != mxs_nand_hook_write_oob) {
1180                 nand_info->hooked_write_oob = mtd->write_oob;
1181                 mtd->write_oob = mxs_nand_hook_write_oob;
1182         }
1183
1184         if (mtd->block_markbad != mxs_nand_hook_block_markbad) {
1185                 nand_info->hooked_block_markbad = mtd->block_markbad;
1186                 mtd->block_markbad = mxs_nand_hook_block_markbad;
1187         }
1188
1189         /* We use the reference implementation for bad block management. */
1190         return nand_default_bbt(mtd);
1191 }
1192
1193 /*
1194  * Allocate DMA buffers
1195  */
1196 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1197 {
1198         uint8_t *buf;
1199         const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1200
1201         nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1202
1203         /* DMA buffers */
1204         buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1205         if (!buf) {
1206                 printf("%s: Error allocating DMA buffers\n", __func__);
1207                 return -ENOMEM;
1208         }
1209
1210         memset(buf, 0, nand_info->data_buf_size);
1211
1212         nand_info->data_buf = buf;
1213         nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1214         /* Command buffers */
1215         nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1216                                 MXS_NAND_COMMAND_BUFFER_SIZE);
1217         if (!nand_info->cmd_buf) {
1218                 free(buf);
1219                 printf("MXS NAND: Error allocating command buffers\n");
1220                 return -ENOMEM;
1221         }
1222         memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1223         nand_info->cmd_queue_len = 0;
1224
1225         return 0;
1226 }
1227
1228 /*
1229  * Initializes the NFC hardware.
1230  */
1231 int mxs_nand_init(struct mxs_nand_info *info)
1232 {
1233         int ret;
1234         int i;
1235
1236         info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1237                                 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1238         if (!info->desc) {
1239                 printf("MXS NAND: Unable to allocate DMA descriptor table\n");
1240                 ret = -ENOMEM;
1241                 goto err1;
1242         }
1243
1244         mxs_dma_init();
1245
1246         /* Allocate the DMA descriptors. */
1247         for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1248                 info->desc[i] = mxs_dma_desc_alloc();
1249                 if (!info->desc[i]) {
1250                         printf("MXS NAND: Unable to allocate DMA descriptors\n");
1251                         ret = -ENOMEM;
1252                         goto err2;
1253                 }
1254         }
1255
1256         /* Init the DMA controller. */
1257         for (i = 0; i < CONFIG_SYS_NAND_MAX_CHIPS; i++) {
1258                 const int chan = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + i;
1259
1260                 ret = mxs_dma_init_channel(chan);
1261                 if (ret) {
1262                         printf("Failed to initialize DMA channel %d\n", chan);
1263                         goto err3;
1264                 }
1265         }
1266
1267         ret = mxs_nand_gpmi_init();
1268         if (ret)
1269                 goto err3;
1270
1271         return 0;
1272
1273 err3:
1274         for (--i; i >= 0; i--)
1275                 mxs_dma_release(i + MXS_DMA_CHANNEL_AHB_APBH_GPMI0);
1276         i = MXS_NAND_DMA_DESCRIPTOR_COUNT - 1;
1277 err2:
1278         free(info->desc);
1279         for (--i; i >= 0; i--)
1280                 mxs_dma_desc_free(info->desc[i]);
1281 err1:
1282         return ret;
1283 }
1284
1285 /*!
1286  * This function is called during the driver binding process.
1287  *
1288  * @param   pdev  the device structure used to store device specific
1289  *                information that is used by the suspend, resume and
1290  *                remove functions
1291  *
1292  * @return  The function always returns 0.
1293  */
1294 int board_nand_init(struct nand_chip *nand)
1295 {
1296         struct mxs_nand_info *nand_info;
1297         int err;
1298
1299         nand_info = malloc(sizeof(struct mxs_nand_info));
1300         if (!nand_info) {
1301                 printf("MXS NAND: Failed to allocate private data\n");
1302                 return -ENOMEM;
1303         }
1304         memset(nand_info, 0, sizeof(struct mxs_nand_info));
1305
1306         err = mxs_nand_alloc_buffers(nand_info);
1307         if (err)
1308                 goto err1;
1309
1310         err = mxs_nand_init(nand_info);
1311         if (err)
1312                 goto err2;
1313
1314         memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1315
1316         nand->priv = nand_info;
1317         nand->options |= NAND_NO_SUBPAGE_WRITE;
1318 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1319         nand->options |= NAND_USE_FLASH_BBT | NAND_USE_FLASH_BBT_NO_OOB;
1320 #endif
1321         nand->cmd_ctrl          = mxs_nand_cmd_ctrl;
1322
1323         nand->dev_ready         = mxs_nand_device_ready;
1324         nand->select_chip       = mxs_nand_select_chip;
1325         nand->block_bad         = mxs_nand_block_bad;
1326         nand->scan_bbt          = mxs_nand_scan_bbt;
1327
1328         nand->read_byte         = mxs_nand_read_byte;
1329
1330         nand->read_buf          = mxs_nand_read_buf;
1331         nand->write_buf         = mxs_nand_write_buf;
1332
1333         nand->ecc.read_page     = mxs_nand_ecc_read_page;
1334         nand->ecc.write_page    = mxs_nand_ecc_write_page;
1335         nand->ecc.read_oob      = mxs_nand_ecc_read_oob;
1336         nand->ecc.write_oob     = mxs_nand_ecc_write_oob;
1337
1338         nand->ecc.layout        = &fake_ecc_layout;
1339         nand->ecc.mode          = NAND_ECC_HW;
1340         nand->ecc.bytes         = 9;
1341         nand->ecc.size          = 512;
1342
1343         return 0;
1344
1345 err2:
1346         free(nand_info->data_buf);
1347         free(nand_info->cmd_buf);
1348 err1:
1349         free(nand_info);
1350         return err;
1351 }