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