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