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