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