]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/mxs_nand.c
mtd: nand: mxs support oobsize bigger than 512
[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 #define MXS_NAND_BITS_PER_ECC_LEVEL             13
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 static int chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
179 static int galois_field = 13;
180
181 /*
182  * Cache management functions
183  */
184 #ifndef CONFIG_SYS_DCACHE_OFF
185 static void mxs_nand_flush_data_buf(struct mxs_nand_info *info)
186 {
187         uint32_t addr = (uint32_t)info->data_buf;
188
189         flush_dcache_range(addr, addr + info->data_buf_size);
190 }
191
192 static void mxs_nand_inval_data_buf(struct mxs_nand_info *info)
193 {
194         uint32_t addr = (uint32_t)info->data_buf;
195
196         invalidate_dcache_range(addr, addr + info->data_buf_size);
197 }
198
199 static void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info)
200 {
201         uint32_t addr = (uint32_t)info->cmd_buf;
202
203         flush_dcache_range(addr, addr + MXS_NAND_COMMAND_BUFFER_SIZE);
204 }
205 #else
206 static inline void mxs_nand_flush_data_buf(struct mxs_nand_info *info) {}
207 static inline void mxs_nand_inval_data_buf(struct mxs_nand_info *info) {}
208 static inline void mxs_nand_flush_cmd_buf(struct mxs_nand_info *info) {}
209 #endif
210
211 static struct mxs_dma_desc *mxs_nand_get_dma_desc(struct mxs_nand_info *info)
212 {
213         struct mxs_dma_desc *desc;
214
215         if (info->desc_index >= MXS_NAND_DMA_DESCRIPTOR_COUNT) {
216                 printf("MXS NAND: Too many DMA descriptors requested\n");
217                 return NULL;
218         }
219
220         desc = info->desc[info->desc_index];
221         info->desc_index++;
222
223         return desc;
224 }
225
226 static void mxs_nand_return_dma_descs(struct mxs_nand_info *info)
227 {
228         int i;
229         struct mxs_dma_desc *desc;
230
231         for (i = 0; i < info->desc_index; i++) {
232                 desc = info->desc[i];
233                 memset(desc, 0, sizeof(struct mxs_dma_desc));
234                 desc->address = (dma_addr_t)desc;
235         }
236
237         info->desc_index = 0;
238 }
239
240 static uint32_t mxs_nand_ecc_chunk_cnt(uint32_t page_data_size)
241 {
242         return page_data_size / chunk_data_size;
243 }
244
245 static uint32_t mxs_nand_ecc_size_in_bits(uint32_t ecc_strength)
246 {
247         return ecc_strength * galois_field;
248 }
249
250 static uint32_t mxs_nand_aux_status_offset(void)
251 {
252         return (MXS_NAND_METADATA_SIZE + 0x3) & ~0x3;
253 }
254
255 static int mxs_nand_gpmi_init(void)
256 {
257         int ret;
258
259         /* Reset the GPMI block. */
260         ret = mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
261         if (ret) {
262                 printf("Failed to reset GPMI block\n");
263                 return ret;
264         }
265
266         ret = mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
267         if (ret) {
268                 printf("Failed to reset BCH block\n");
269                 return ret;
270         }
271
272         /*
273          * Choose NAND mode, set IRQ polarity, disable write protection and
274          * select BCH ECC.
275          */
276         clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
277                         GPMI_CTRL1_GPMI_MODE,
278                         GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
279                         GPMI_CTRL1_BCH_MODE);
280         writel(0x500 << 16, &gpmi_regs->hw_gpmi_timing1);
281         return 0;
282 }
283
284 static inline uint32_t mxs_nand_get_ecc_strength(uint32_t page_data_size,
285                                                 uint32_t page_oob_size)
286 {
287         int ecc_strength;
288
289         /*
290          * Determine the ECC layout with the formula:
291          *      ECC bits per chunk = (total page spare data bits) /
292          *              (bits per ECC level) / (chunks per page)
293          * where:
294          *      total page spare data bits =
295          *              (page oob size - meta data size) * (bits per byte)
296          */
297         ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8)
298                         / (galois_field *
299                            mxs_nand_ecc_chunk_cnt(page_data_size));
300
301         return round_down(ecc_strength, 2);
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 = chunk_data_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 inline 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 inline 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 #ifndef CONFIG_NAND_MXS_NO_BBM_SWAP
514 static void mxs_nand_swap_block_mark(struct mtd_info *mtd,
515                                         uint8_t *data_buf, uint8_t *oob_buf)
516 {
517         uint32_t bit_offset;
518         uint32_t buf_offset;
519
520         uint32_t src;
521         uint32_t dst;
522
523         bit_offset = mxs_nand_mark_bit_offset(mtd);
524         buf_offset = mxs_nand_mark_byte_offset(mtd);
525
526         /*
527          * Get the byte from the data area that overlays the block mark. Since
528          * the ECC engine applies its own view to the bits in the page, the
529          * physical block mark won't (in general) appear on a byte boundary in
530          * the data.
531          */
532         src = data_buf[buf_offset] >> bit_offset;
533         src |= data_buf[buf_offset + 1] << (8 - bit_offset);
534
535         dst = oob_buf[0];
536
537         debug("Swapping byte %02x @ %03x.%d with %02x @ %03x\n",
538                 src & 0xff, buf_offset, bit_offset, dst & 0xff, 0);
539
540         oob_buf[0] = src;
541
542         data_buf[buf_offset] &= ~(0xff << bit_offset);
543         data_buf[buf_offset + 1] &= 0xff << bit_offset;
544
545         data_buf[buf_offset] |= dst << bit_offset;
546         data_buf[buf_offset + 1] |= dst >> (8 - bit_offset);
547 }
548 #else
549 static inline void mxs_nand_swap_block_mark(struct mtd_info *mtd,
550                                         uint8_t *data_buf, uint8_t *oob_buf)
551 {
552 }
553 #endif
554
555 /*
556  * Read data from NAND.
557  */
558 static void mxs_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int length)
559 {
560         struct nand_chip *nand = mtd->priv;
561         struct mxs_nand_info *nand_info = nand->priv;
562         struct mxs_dma_desc *d;
563         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
564         int ret;
565
566         if (length > NAND_MAX_PAGESIZE) {
567                 printf("MXS NAND: DMA buffer too big\n");
568                 return;
569         }
570
571         if (!buf) {
572                 printf("MXS NAND: DMA buffer is NULL\n");
573                 return;
574         }
575
576         /* Compile the DMA descriptor - a descriptor that reads data. */
577         d = mxs_nand_get_dma_desc(nand_info);
578         d->cmd.data =
579                 MXS_DMA_DESC_COMMAND_DMA_WRITE | MXS_DMA_DESC_IRQ |
580                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
581                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
582                 (length << MXS_DMA_DESC_BYTES_OFFSET);
583
584         d->cmd.address = (dma_addr_t)nand_info->data_buf;
585
586         d->cmd.pio_words[0] =
587                 GPMI_CTRL0_COMMAND_MODE_READ |
588                 GPMI_CTRL0_WORD_LENGTH |
589                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
590                 GPMI_CTRL0_ADDRESS_NAND_DATA |
591                 length;
592
593         mxs_dma_desc_append(channel, d);
594
595 #ifndef CONFIG_SOC_MX6Q
596         /*
597          * A DMA descriptor that waits for the command to end and the chip to
598          * become ready.
599          *
600          * I think we actually should *not* be waiting for the chip to become
601          * ready because, after all, we don't care. I think the original code
602          * did that and no one has re-thought it yet.
603          */
604         d = mxs_nand_get_dma_desc(nand_info);
605         d->cmd.data =
606                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
607                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_DEC_SEM |
608                 MXS_DMA_DESC_WAIT4END | (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
609
610         d->cmd.address = 0;
611
612         d->cmd.pio_words[0] =
613                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
614                 GPMI_CTRL0_WORD_LENGTH |
615                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
616                 GPMI_CTRL0_ADDRESS_NAND_DATA;
617
618         mxs_dma_desc_append(channel, d);
619 #endif
620         /* Execute the DMA chain. */
621         ret = mxs_dma_go(channel);
622         if (ret) {
623                 printf("%s: DMA read error\n", __func__);
624                 goto rtn;
625         }
626
627         /* Invalidate caches */
628         mxs_nand_inval_data_buf(nand_info);
629
630         memcpy(buf, nand_info->data_buf, length);
631
632 rtn:
633         mxs_nand_return_dma_descs(nand_info);
634 }
635
636 /*
637  * Write data to NAND.
638  */
639 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
640                                 int length)
641 {
642         struct nand_chip *nand = mtd->priv;
643         struct mxs_nand_info *nand_info = nand->priv;
644         struct mxs_dma_desc *d;
645         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
646         int ret;
647
648         if (length > NAND_MAX_PAGESIZE) {
649                 printf("MXS NAND: DMA buffer too big\n");
650                 return;
651         }
652
653         if (!buf) {
654                 printf("MXS NAND: DMA buffer is NULL\n");
655                 return;
656         }
657
658         memcpy(nand_info->data_buf, buf, length);
659
660         /* Compile the DMA descriptor - a descriptor that writes data. */
661         d = mxs_nand_get_dma_desc(nand_info);
662         d->cmd.data =
663                 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
664                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
665                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
666                 (length << MXS_DMA_DESC_BYTES_OFFSET);
667
668         d->cmd.address = (dma_addr_t)nand_info->data_buf;
669
670         d->cmd.pio_words[0] =
671                 GPMI_CTRL0_COMMAND_MODE_WRITE |
672                 GPMI_CTRL0_WORD_LENGTH |
673                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
674                 GPMI_CTRL0_ADDRESS_NAND_DATA |
675                 length;
676
677         mxs_dma_desc_append(channel, d);
678
679         /* Flush caches */
680         mxs_nand_flush_data_buf(nand_info);
681
682         /* Execute the DMA chain. */
683         ret = mxs_dma_go(channel);
684         if (ret)
685                 printf("%s: DMA write error\n", __func__);
686
687         mxs_nand_return_dma_descs(nand_info);
688 }
689
690 /*
691  * Read a single byte from NAND.
692  */
693 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
694 {
695         uint8_t buf;
696         mxs_nand_read_buf(mtd, &buf, 1);
697         return buf;
698 }
699
700 static void flush_buffers(struct mtd_info *mtd, struct mxs_nand_info *nand_info)
701 {
702         flush_dcache_range((unsigned long)nand_info->data_buf,
703                         (unsigned long)nand_info->data_buf +
704                         mtd->writesize);
705         flush_dcache_range((unsigned long)nand_info->oob_buf,
706                         (unsigned long)nand_info->oob_buf +
707                         mtd->oobsize);
708 }
709
710 /*
711  * Read a page from NAND.
712  */
713 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
714                                         uint8_t *buf, int oob_required,
715                                         int page)
716 {
717         struct mxs_nand_info *nand_info = nand->priv;
718         struct mxs_dma_desc *d;
719         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
720         uint32_t corrected = 0, failed = 0;
721         uint8_t *status;
722         int i, ret;
723
724         /* Compile the DMA descriptor - wait for ready. */
725         d = mxs_nand_get_dma_desc(nand_info);
726         d->cmd.data =
727                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
728                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
729                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
730
731         d->cmd.address = 0;
732
733         d->cmd.pio_words[0] =
734                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
735                 GPMI_CTRL0_WORD_LENGTH |
736                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
737                 GPMI_CTRL0_ADDRESS_NAND_DATA;
738
739         mxs_dma_desc_append(channel, d);
740
741         /* Compile the DMA descriptor - enable the BCH block and read. */
742         d = mxs_nand_get_dma_desc(nand_info);
743         d->cmd.data =
744                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
745                 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
746
747         d->cmd.address = 0;
748
749         d->cmd.pio_words[0] =
750                 GPMI_CTRL0_COMMAND_MODE_READ |
751                 GPMI_CTRL0_WORD_LENGTH |
752                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
753                 GPMI_CTRL0_ADDRESS_NAND_DATA |
754                 (mtd->writesize + mtd->oobsize);
755         d->cmd.pio_words[1] = 0;
756         d->cmd.pio_words[2] =
757                 GPMI_ECCCTRL_ENABLE_ECC |
758                 GPMI_ECCCTRL_ECC_CMD_DECODE |
759                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
760         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
761         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
762         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
763
764         flush_buffers(mtd, nand_info);
765
766         mxs_dma_desc_append(channel, d);
767
768         /* Compile the DMA descriptor - disable the BCH block. */
769         d = mxs_nand_get_dma_desc(nand_info);
770         d->cmd.data =
771                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
772                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
773                 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
774
775         d->cmd.address = 0;
776
777         d->cmd.pio_words[0] =
778                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
779                 GPMI_CTRL0_WORD_LENGTH |
780                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
781                 GPMI_CTRL0_ADDRESS_NAND_DATA |
782                 (mtd->writesize + mtd->oobsize);
783         d->cmd.pio_words[1] = 0;
784         d->cmd.pio_words[2] = 0;
785
786         mxs_dma_desc_append(channel, d);
787
788         /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
789         d = mxs_nand_get_dma_desc(nand_info);
790         d->cmd.data =
791                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
792                 MXS_DMA_DESC_DEC_SEM;
793
794         d->cmd.address = 0;
795
796         mxs_dma_desc_append(channel, d);
797
798         /* Execute the DMA chain. */
799         ret = mxs_dma_go(channel);
800         if (ret) {
801                 printf("%s: DMA read error\n", __func__);
802                 goto rtn;
803         }
804
805         ret = mxs_nand_wait_for_bch_complete();
806         if (ret) {
807                 printf("MXS NAND: BCH read timeout\n");
808                 goto rtn;
809         }
810
811         /* Invalidate caches */
812         mxs_nand_inval_data_buf(nand_info);
813
814         /* Read DMA completed, now do the mark swapping. */
815         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
816
817         /* Loop over status bytes, accumulating ECC status. */
818         status = nand_info->oob_buf + mxs_nand_aux_status_offset();
819         for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
820                 if (status[i] == 0x00)
821                         continue;
822
823                 if (status[i] == 0xff)
824                         continue;
825
826                 if (status[i] == 0xfe) {
827                         failed++;
828                         continue;
829                 }
830
831                 corrected += status[i];
832         }
833
834         /* Propagate ECC status to the owning MTD. */
835         mtd->ecc_stats.failed += failed;
836         mtd->ecc_stats.corrected += corrected;
837
838         /*
839          * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
840          * details about our policy for delivering the OOB.
841          *
842          * We fill the caller's buffer with set bits, and then copy the block
843          * mark to the caller's buffer. Note that, if block mark swapping was
844          * necessary, it has already been done, so we can rely on the first
845          * byte of the auxiliary buffer to contain the block mark.
846          */
847         memset(nand->oob_poi, 0xff, mtd->oobsize);
848
849         nand->oob_poi[0] = nand_info->oob_buf[0];
850
851         memcpy(buf, nand_info->data_buf, mtd->writesize);
852
853 rtn:
854         mxs_nand_return_dma_descs(nand_info);
855
856         return ret;
857 }
858
859 /*
860  * Write a page to NAND.
861  */
862 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
863                                 struct nand_chip *nand, const uint8_t *buf,
864                                 int oob_required)
865 {
866         struct mxs_nand_info *nand_info = nand->priv;
867         struct mxs_dma_desc *d;
868         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
869         int ret;
870
871         memcpy(nand_info->data_buf, buf, mtd->writesize);
872         memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
873
874         /* Handle block mark swapping. */
875         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
876
877         /* Compile the DMA descriptor - write data. */
878         d = mxs_nand_get_dma_desc(nand_info);
879         d->cmd.data =
880                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
881                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
882                 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
883
884         d->cmd.address = 0;
885
886         d->cmd.pio_words[0] =
887                 GPMI_CTRL0_COMMAND_MODE_WRITE |
888                 GPMI_CTRL0_WORD_LENGTH |
889                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
890                 GPMI_CTRL0_ADDRESS_NAND_DATA;
891         d->cmd.pio_words[1] = 0;
892         d->cmd.pio_words[2] =
893                 GPMI_ECCCTRL_ENABLE_ECC |
894                 GPMI_ECCCTRL_ECC_CMD_ENCODE |
895                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
896         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
897         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
898         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
899
900         flush_buffers(mtd, nand_info);
901
902         mxs_dma_desc_append(channel, d);
903
904         /* Flush caches */
905         mxs_nand_flush_data_buf(nand_info);
906
907         /* Execute the DMA chain. */
908         ret = mxs_dma_go(channel);
909         if (ret) {
910                 printf("%s: DMA write error\n", __func__);
911                 goto rtn;
912         }
913
914         ret = mxs_nand_wait_for_bch_complete();
915         if (ret) {
916                 printf("%s: BCH write timeout\n", __func__);
917                 goto rtn;
918         }
919
920 rtn:
921         mxs_nand_return_dma_descs(nand_info);
922         return 0;
923 }
924
925 /*
926  * Read OOB from NAND.
927  *
928  * This function is a veneer that replaces the function originally installed by
929  * the NAND Flash MTD code.
930  */
931 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
932                                         struct mtd_oob_ops *ops)
933 {
934         struct nand_chip *chip = mtd->priv;
935         struct mxs_nand_info *nand_info = chip->priv;
936         int ret;
937
938         if (ops->mode == MTD_OPS_RAW)
939                 nand_info->raw_oob_mode = 1;
940         else
941                 nand_info->raw_oob_mode = 0;
942
943         ret = nand_info->hooked_read_oob(mtd, from, ops);
944
945         nand_info->raw_oob_mode = 0;
946
947         return ret;
948 }
949
950 /*
951  * Write OOB to NAND.
952  *
953  * This function is a veneer that replaces the function originally installed by
954  * the NAND Flash MTD code.
955  */
956 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
957                                         struct mtd_oob_ops *ops)
958 {
959         struct nand_chip *chip = mtd->priv;
960         struct mxs_nand_info *nand_info = chip->priv;
961         int ret;
962
963         if (ops->mode == MTD_OPS_RAW)
964                 nand_info->raw_oob_mode = 1;
965         else
966                 nand_info->raw_oob_mode = 0;
967
968         ret = nand_info->hooked_write_oob(mtd, to, ops);
969
970         nand_info->raw_oob_mode = 0;
971
972         return ret;
973 }
974
975 /*
976  * Mark a block bad in NAND.
977  *
978  * This function is a veneer that replaces the function originally installed by
979  * the NAND Flash MTD code.
980  */
981 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
982 {
983         struct nand_chip *chip = mtd->priv;
984         struct mxs_nand_info *nand_info = chip->priv;
985         int ret;
986
987         nand_info->marking_block_bad = 1;
988
989         ret = nand_info->hooked_block_markbad(mtd, ofs);
990
991         nand_info->marking_block_bad = 0;
992
993         return ret;
994 }
995
996 /*
997  * There are several places in this driver where we have to handle the OOB and
998  * block marks. This is the function where things are the most complicated, so
999  * this is where we try to explain it all. All the other places refer back to
1000  * here.
1001  *
1002  * These are the rules, in order of decreasing importance:
1003  *
1004  * 1) Nothing the caller does can be allowed to imperil the block mark, so all
1005  *    write operations take measures to protect it.
1006  *
1007  * 2) In read operations, the first byte of the OOB we return must reflect the
1008  *    true state of the block mark, no matter where that block mark appears in
1009  *    the physical page.
1010  *
1011  * 3) ECC-based read operations return an OOB full of set bits (since we never
1012  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
1013  *    return).
1014  *
1015  * 4) "Raw" read operations return a direct view of the physical bytes in the
1016  *    page, using the conventional definition of which bytes are data and which
1017  *    are OOB. This gives the caller a way to see the actual, physical bytes
1018  *    in the page, without the distortions applied by our ECC engine.
1019  *
1020  * What we do for this specific read operation depends on whether we're doing
1021  * "raw" read, or an ECC-based read.
1022  *
1023  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
1024  * easy. When reading a page, for example, the NAND Flash MTD code calls our
1025  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
1026  * ECC-based or raw view of the page is implicit in which function it calls
1027  * (there is a similar pair of ECC-based/raw functions for writing).
1028  *
1029  * Since MTD assumes the OOB is not covered by ECC, there is no pair of
1030  * ECC-based/raw functions for reading or or writing the OOB. The fact that the
1031  * caller wants an ECC-based or raw view of the page is not propagated down to
1032  * this driver.
1033  *
1034  * Since our OOB *is* covered by ECC, we need this information. So, we hook the
1035  * ecc.read_oob and ecc.write_oob function pointers in the owning
1036  * struct mtd_info with our own functions. These hook functions set the
1037  * raw_oob_mode field so that, when control finally arrives here, we'll know
1038  * what to do.
1039  */
1040 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
1041                                 int page)
1042 {
1043         struct mxs_nand_info *nand_info = nand->priv;
1044
1045         /*
1046          * First, fill in the OOB buffer. If we're doing a raw read, we need to
1047          * get the bytes from the physical page. If we're not doing a raw read,
1048          * we need to fill the buffer with set bits.
1049          */
1050         if (nand_info->raw_oob_mode) {
1051                 /*
1052                  * If control arrives here, we're doing a "raw" read. Send the
1053                  * command to read the conventional OOB and read it.
1054                  */
1055                 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1056                 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
1057         } else {
1058                 /*
1059                  * If control arrives here, we're not doing a "raw" read. Fill
1060                  * the OOB buffer with set bits and correct the block mark.
1061                  */
1062                 memset(nand->oob_poi, 0xff, mtd->oobsize);
1063
1064                 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
1065                 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
1066         }
1067
1068         return 0;
1069
1070 }
1071
1072 /*
1073  * Write OOB data to NAND.
1074  */
1075 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
1076                                         int page)
1077 {
1078         struct mxs_nand_info *nand_info = nand->priv;
1079         uint8_t block_mark = 0;
1080
1081         /*
1082          * There are fundamental incompatibilities between the i.MX GPMI NFC and
1083          * the NAND Flash MTD model that make it essentially impossible to write
1084          * the out-of-band bytes.
1085          *
1086          * We permit *ONE* exception. If the *intent* of writing the OOB is to
1087          * mark a block bad, we can do that.
1088          */
1089
1090         if (!nand_info->marking_block_bad) {
1091                 printf("NXS NAND: Writing OOB isn't supported\n");
1092                 return -EIO;
1093         }
1094
1095         /* Write the block mark. */
1096         nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
1097         nand->write_buf(mtd, &block_mark, 1);
1098         nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1099
1100         /* Check if it worked. */
1101         if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
1102                 return -EIO;
1103
1104         return 0;
1105 }
1106
1107 /*
1108  * Claims all blocks are good.
1109  *
1110  * In principle, this function is *only* called when the NAND Flash MTD system
1111  * isn't allowed to keep an in-memory bad block table, so it is forced to ask
1112  * the driver for bad block information.
1113  *
1114  * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
1115  * this function is *only* called when we take it away.
1116  *
1117  * Thus, this function is only called when we want *all* blocks to look good,
1118  * so it *always* return success.
1119  */
1120 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
1121 {
1122         return 0;
1123 }
1124
1125 /*
1126  * Nominally, the purpose of this function is to look for or create the bad
1127  * block table. In fact, since the we call this function at the very end of
1128  * the initialization process started by nand_scan(), and we don't have a
1129  * more formal mechanism, we "hook" this function to continue init process.
1130  *
1131  * At this point, the physical NAND Flash chips have been identified and
1132  * counted, so we know the physical geometry. This enables us to make some
1133  * important configuration decisions.
1134  *
1135  * The return value of this function propogates directly back to this driver's
1136  * call to nand_scan(). Anything other than zero will cause this driver to
1137  * tear everything down and declare failure.
1138  */
1139 static int mxs_nand_scan_bbt(struct mtd_info *mtd)
1140 {
1141         struct nand_chip *nand = mtd->priv;
1142         struct mxs_nand_info *nand_info = nand->priv;
1143         uint32_t tmp;
1144
1145         if (mtd->oobsize > MXS_NAND_CHUNK_DATA_CHUNK_SIZE) {
1146                 galois_field = 14;
1147                 chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 2;
1148         }
1149
1150         if (mtd->oobsize > chunk_data_size) {
1151                 printf("OOB size of chip (%u bytes) is larger than max. supported size (%u bytes)\n",
1152                         mtd->oobsize, chunk_data_size);
1153                 return -EINVAL;
1154         }
1155
1156         /* Configure BCH and set NFC geometry */
1157         mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1158
1159         debug("mtd->writesize=%d\n", mtd->writesize);
1160         debug("mtd->oobsize=%d\n", mtd->oobsize);
1161         debug("ecc_strength=%d\n", mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize));
1162
1163         /* Configure layout 0 */
1164         tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
1165                 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
1166         tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
1167         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1168                 << BCH_FLASHLAYOUT0_ECC0_OFFSET;
1169         tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1170         tmp |= (14 == galois_field ? 1 : 0) <<
1171                 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
1172         writel(tmp, &bch_regs->hw_bch_flash0layout0);
1173
1174         tmp = (mtd->writesize + mtd->oobsize)
1175                 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1176         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1177                 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1178         tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1179         tmp |= (14 == galois_field ? 1 : 0) <<
1180                 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1181         writel(tmp, &bch_regs->hw_bch_flash0layout1);
1182
1183         /* Set *all* chip selects to use layout 0 */
1184         writel(0, &bch_regs->hw_bch_layoutselect);
1185
1186         /* Enable BCH complete interrupt */
1187         writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1188
1189         /* Hook some operations at the MTD level. */
1190         if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1191                 nand_info->hooked_read_oob = mtd->_read_oob;
1192                 mtd->_read_oob = mxs_nand_hook_read_oob;
1193         }
1194
1195         if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1196                 nand_info->hooked_write_oob = mtd->_write_oob;
1197                 mtd->_write_oob = mxs_nand_hook_write_oob;
1198         }
1199
1200         if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1201                 nand_info->hooked_block_markbad = mtd->_block_markbad;
1202                 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1203         }
1204
1205         /* We use the reference implementation for bad block management. */
1206         return nand_default_bbt(mtd);
1207 }
1208
1209 /*
1210  * Allocate DMA buffers
1211  */
1212 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1213 {
1214         uint8_t *buf;
1215         const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1216
1217         nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1218
1219         /* DMA buffers */
1220         buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1221         if (!buf) {
1222                 printf("%s: Error allocating DMA buffers\n", __func__);
1223                 return -ENOMEM;
1224         }
1225
1226         memset(buf, 0, nand_info->data_buf_size);
1227
1228         nand_info->data_buf = buf;
1229         nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1230         /* Command buffers */
1231         nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1232                                 MXS_NAND_COMMAND_BUFFER_SIZE);
1233         if (!nand_info->cmd_buf) {
1234                 free(buf);
1235                 printf("MXS NAND: Error allocating command buffers\n");
1236                 return -ENOMEM;
1237         }
1238         memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1239         nand_info->cmd_queue_len = 0;
1240
1241         return 0;
1242 }
1243
1244 /*
1245  * Initializes the NFC hardware.
1246  */
1247 int mxs_nand_init(struct mxs_nand_info *info)
1248 {
1249         int ret;
1250         int i;
1251
1252         info->desc = calloc(MXS_NAND_DMA_DESCRIPTOR_COUNT,
1253                         sizeof(struct mxs_dma_desc *));
1254
1255         if (!info->desc) {
1256                 printf("MXS NAND: Unable to allocate DMA descriptor table\n");
1257                 ret = -ENOMEM;
1258                 goto err1;
1259         }
1260
1261         mxs_dma_init();
1262
1263         /* Allocate the DMA descriptors. */
1264         for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1265                 info->desc[i] = mxs_dma_desc_alloc();
1266                 if (!info->desc[i]) {
1267                         printf("MXS NAND: Unable to allocate DMA descriptors\n");
1268                         ret = -ENOMEM;
1269                         goto err2;
1270                 }
1271         }
1272
1273         /* Init the DMA controller. */
1274         for (i = 0; i < CONFIG_SYS_NAND_MAX_CHIPS; i++) {
1275                 const int chan = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + i;
1276
1277                 ret = mxs_dma_init_channel(chan);
1278                 if (ret) {
1279                         printf("Failed to initialize DMA channel %d\n", chan);
1280                         goto err3;
1281                 }
1282         }
1283
1284         ret = mxs_nand_gpmi_init();
1285         if (ret)
1286                 goto err3;
1287
1288         return 0;
1289
1290 err3:
1291         for (--i; i >= 0; i--)
1292                 mxs_dma_release(i + MXS_DMA_CHANNEL_AHB_APBH_GPMI0);
1293         i = MXS_NAND_DMA_DESCRIPTOR_COUNT - 1;
1294 err2:
1295         free(info->desc);
1296         for (--i; i >= 0; i--)
1297                 mxs_dma_desc_free(info->desc[i]);
1298 err1:
1299         return ret;
1300 }
1301
1302 /*!
1303  * This function is called during the driver binding process.
1304  *
1305  * @param   pdev  the device structure used to store device specific
1306  *                information that is used by the suspend, resume and
1307  *                remove functions
1308  *
1309  * @return  0 for success; errno value in case of error
1310  */
1311 int board_nand_init(struct nand_chip *nand)
1312 {
1313         struct mxs_nand_info *nand_info;
1314         int err;
1315
1316         nand_info = kzalloc(sizeof(struct mxs_nand_info), 0);
1317         if (!nand_info) {
1318                 printf("MXS NAND: Failed to allocate private data\n");
1319                 return -ENOMEM;
1320         }
1321
1322         err = mxs_nand_alloc_buffers(nand_info);
1323         if (err)
1324                 goto err1;
1325
1326         err = mxs_nand_init(nand_info);
1327         if (err)
1328                 goto err2;
1329
1330         memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1331
1332         nand->priv = nand_info;
1333         nand->options |= NAND_NO_SUBPAGE_WRITE;
1334 #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
1335         nand->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
1336 #endif
1337         nand->cmd_ctrl          = mxs_nand_cmd_ctrl;
1338
1339         nand->dev_ready         = mxs_nand_device_ready;
1340         nand->select_chip       = mxs_nand_select_chip;
1341         nand->block_bad         = mxs_nand_block_bad;
1342         nand->scan_bbt          = mxs_nand_scan_bbt;
1343
1344         nand->read_byte         = mxs_nand_read_byte;
1345
1346         nand->read_buf          = mxs_nand_read_buf;
1347         nand->write_buf         = mxs_nand_write_buf;
1348
1349         nand->ecc.read_page     = mxs_nand_ecc_read_page;
1350         nand->ecc.write_page    = mxs_nand_ecc_write_page;
1351         nand->ecc.read_oob      = mxs_nand_ecc_read_oob;
1352         nand->ecc.write_oob     = mxs_nand_ecc_write_oob;
1353
1354         nand->ecc.layout        = &fake_ecc_layout;
1355         nand->ecc.mode          = NAND_ECC_HW;
1356         nand->ecc.bytes         = 9;
1357         nand->ecc.size          = 512;
1358         nand->ecc.strength      = 8;
1359
1360         return 0;
1361
1362 err2:
1363         free(nand_info->data_buf);
1364         free(nand_info->cmd_buf);
1365 err1:
1366         free(nand_info);
1367         return err;
1368 }