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