]> git.sur5r.net Git - u-boot/blob - drivers/mtd/nand/mxs_nand.c
mtd: nand: mxs support oobsize bigger than 512
[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         /* Execute the DMA chain. */
466         ret = mxs_dma_go(channel);
467         if (ret) {
468                 printf("MXS NAND: DMA read error\n");
469                 goto rtn;
470         }
471
472         /* Invalidate caches */
473         mxs_nand_inval_data_buf(nand_info);
474
475         memcpy(buf, nand_info->data_buf, length);
476
477 rtn:
478         mxs_nand_return_dma_descs(nand_info);
479 }
480
481 /*
482  * Write data to NAND.
483  */
484 static void mxs_nand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
485                                 int length)
486 {
487         struct nand_chip *nand = mtd->priv;
488         struct mxs_nand_info *nand_info = nand->priv;
489         struct mxs_dma_desc *d;
490         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
491         int ret;
492
493         if (length > NAND_MAX_PAGESIZE) {
494                 printf("MXS NAND: DMA buffer too big\n");
495                 return;
496         }
497
498         if (!buf) {
499                 printf("MXS NAND: DMA buffer is NULL\n");
500                 return;
501         }
502
503         memcpy(nand_info->data_buf, buf, length);
504
505         /* Compile the DMA descriptor - a descriptor that writes data. */
506         d = mxs_nand_get_dma_desc(nand_info);
507         d->cmd.data =
508                 MXS_DMA_DESC_COMMAND_DMA_READ | MXS_DMA_DESC_IRQ |
509                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
510                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET) |
511                 (length << MXS_DMA_DESC_BYTES_OFFSET);
512
513         d->cmd.address = (dma_addr_t)nand_info->data_buf;
514
515         d->cmd.pio_words[0] =
516                 GPMI_CTRL0_COMMAND_MODE_WRITE |
517                 GPMI_CTRL0_WORD_LENGTH |
518                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
519                 GPMI_CTRL0_ADDRESS_NAND_DATA |
520                 length;
521
522         mxs_dma_desc_append(channel, d);
523
524         /* Flush caches */
525         mxs_nand_flush_data_buf(nand_info);
526
527         /* Execute the DMA chain. */
528         ret = mxs_dma_go(channel);
529         if (ret)
530                 printf("MXS NAND: DMA write error\n");
531
532         mxs_nand_return_dma_descs(nand_info);
533 }
534
535 /*
536  * Read a single byte from NAND.
537  */
538 static uint8_t mxs_nand_read_byte(struct mtd_info *mtd)
539 {
540         uint8_t buf;
541         mxs_nand_read_buf(mtd, &buf, 1);
542         return buf;
543 }
544
545 /*
546  * Read a page from NAND.
547  */
548 static int mxs_nand_ecc_read_page(struct mtd_info *mtd, struct nand_chip *nand,
549                                         uint8_t *buf, int oob_required,
550                                         int page)
551 {
552         struct mxs_nand_info *nand_info = nand->priv;
553         struct mxs_dma_desc *d;
554         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
555         uint32_t corrected = 0, failed = 0;
556         uint8_t *status;
557         int i, ret;
558
559         /* Compile the DMA descriptor - wait for ready. */
560         d = mxs_nand_get_dma_desc(nand_info);
561         d->cmd.data =
562                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
563                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
564                 (1 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
565
566         d->cmd.address = 0;
567
568         d->cmd.pio_words[0] =
569                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
570                 GPMI_CTRL0_WORD_LENGTH |
571                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
572                 GPMI_CTRL0_ADDRESS_NAND_DATA;
573
574         mxs_dma_desc_append(channel, d);
575
576         /* Compile the DMA descriptor - enable the BCH block and read. */
577         d = mxs_nand_get_dma_desc(nand_info);
578         d->cmd.data =
579                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
580                 MXS_DMA_DESC_WAIT4END | (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
581
582         d->cmd.address = 0;
583
584         d->cmd.pio_words[0] =
585                 GPMI_CTRL0_COMMAND_MODE_READ |
586                 GPMI_CTRL0_WORD_LENGTH |
587                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
588                 GPMI_CTRL0_ADDRESS_NAND_DATA |
589                 (mtd->writesize + mtd->oobsize);
590         d->cmd.pio_words[1] = 0;
591         d->cmd.pio_words[2] =
592                 GPMI_ECCCTRL_ENABLE_ECC |
593                 GPMI_ECCCTRL_ECC_CMD_DECODE |
594                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
595         d->cmd.pio_words[3] = mtd->writesize + mtd->oobsize;
596         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
597         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
598
599         mxs_dma_desc_append(channel, d);
600
601         /* Compile the DMA descriptor - disable the BCH block. */
602         d = mxs_nand_get_dma_desc(nand_info);
603         d->cmd.data =
604                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_CHAIN |
605                 MXS_DMA_DESC_NAND_WAIT_4_READY | MXS_DMA_DESC_WAIT4END |
606                 (3 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
607
608         d->cmd.address = 0;
609
610         d->cmd.pio_words[0] =
611                 GPMI_CTRL0_COMMAND_MODE_WAIT_FOR_READY |
612                 GPMI_CTRL0_WORD_LENGTH |
613                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
614                 GPMI_CTRL0_ADDRESS_NAND_DATA |
615                 (mtd->writesize + mtd->oobsize);
616         d->cmd.pio_words[1] = 0;
617         d->cmd.pio_words[2] = 0;
618
619         mxs_dma_desc_append(channel, d);
620
621         /* Compile the DMA descriptor - deassert the NAND lock and interrupt. */
622         d = mxs_nand_get_dma_desc(nand_info);
623         d->cmd.data =
624                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
625                 MXS_DMA_DESC_DEC_SEM;
626
627         d->cmd.address = 0;
628
629         mxs_dma_desc_append(channel, d);
630
631         /* Execute the DMA chain. */
632         ret = mxs_dma_go(channel);
633         if (ret) {
634                 printf("MXS NAND: DMA read error\n");
635                 goto rtn;
636         }
637
638         ret = mxs_nand_wait_for_bch_complete();
639         if (ret) {
640                 printf("MXS NAND: BCH read timeout\n");
641                 goto rtn;
642         }
643
644         /* Invalidate caches */
645         mxs_nand_inval_data_buf(nand_info);
646
647         /* Read DMA completed, now do the mark swapping. */
648         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
649
650         /* Loop over status bytes, accumulating ECC status. */
651         status = nand_info->oob_buf + mxs_nand_aux_status_offset();
652         for (i = 0; i < mxs_nand_ecc_chunk_cnt(mtd->writesize); i++) {
653                 if (status[i] == 0x00)
654                         continue;
655
656                 if (status[i] == 0xff)
657                         continue;
658
659                 if (status[i] == 0xfe) {
660                         failed++;
661                         continue;
662                 }
663
664                 corrected += status[i];
665         }
666
667         /* Propagate ECC status to the owning MTD. */
668         mtd->ecc_stats.failed += failed;
669         mtd->ecc_stats.corrected += corrected;
670
671         /*
672          * It's time to deliver the OOB bytes. See mxs_nand_ecc_read_oob() for
673          * details about our policy for delivering the OOB.
674          *
675          * We fill the caller's buffer with set bits, and then copy the block
676          * mark to the caller's buffer. Note that, if block mark swapping was
677          * necessary, it has already been done, so we can rely on the first
678          * byte of the auxiliary buffer to contain the block mark.
679          */
680         memset(nand->oob_poi, 0xff, mtd->oobsize);
681
682         nand->oob_poi[0] = nand_info->oob_buf[0];
683
684         memcpy(buf, nand_info->data_buf, mtd->writesize);
685
686 rtn:
687         mxs_nand_return_dma_descs(nand_info);
688
689         return ret;
690 }
691
692 /*
693  * Write a page to NAND.
694  */
695 static int mxs_nand_ecc_write_page(struct mtd_info *mtd,
696                                 struct nand_chip *nand, const uint8_t *buf,
697                                 int oob_required)
698 {
699         struct mxs_nand_info *nand_info = nand->priv;
700         struct mxs_dma_desc *d;
701         uint32_t channel = MXS_DMA_CHANNEL_AHB_APBH_GPMI0 + nand_info->cur_chip;
702         int ret;
703
704         memcpy(nand_info->data_buf, buf, mtd->writesize);
705         memcpy(nand_info->oob_buf, nand->oob_poi, mtd->oobsize);
706
707         /* Handle block mark swapping. */
708         mxs_nand_swap_block_mark(mtd, nand_info->data_buf, nand_info->oob_buf);
709
710         /* Compile the DMA descriptor - write data. */
711         d = mxs_nand_get_dma_desc(nand_info);
712         d->cmd.data =
713                 MXS_DMA_DESC_COMMAND_NO_DMAXFER | MXS_DMA_DESC_IRQ |
714                 MXS_DMA_DESC_DEC_SEM | MXS_DMA_DESC_WAIT4END |
715                 (6 << MXS_DMA_DESC_PIO_WORDS_OFFSET);
716
717         d->cmd.address = 0;
718
719         d->cmd.pio_words[0] =
720                 GPMI_CTRL0_COMMAND_MODE_WRITE |
721                 GPMI_CTRL0_WORD_LENGTH |
722                 (nand_info->cur_chip << GPMI_CTRL0_CS_OFFSET) |
723                 GPMI_CTRL0_ADDRESS_NAND_DATA;
724         d->cmd.pio_words[1] = 0;
725         d->cmd.pio_words[2] =
726                 GPMI_ECCCTRL_ENABLE_ECC |
727                 GPMI_ECCCTRL_ECC_CMD_ENCODE |
728                 GPMI_ECCCTRL_BUFFER_MASK_BCH_PAGE;
729         d->cmd.pio_words[3] = (mtd->writesize + mtd->oobsize);
730         d->cmd.pio_words[4] = (dma_addr_t)nand_info->data_buf;
731         d->cmd.pio_words[5] = (dma_addr_t)nand_info->oob_buf;
732
733         mxs_dma_desc_append(channel, d);
734
735         /* Flush caches */
736         mxs_nand_flush_data_buf(nand_info);
737
738         /* Execute the DMA chain. */
739         ret = mxs_dma_go(channel);
740         if (ret) {
741                 printf("MXS NAND: DMA write error\n");
742                 goto rtn;
743         }
744
745         ret = mxs_nand_wait_for_bch_complete();
746         if (ret) {
747                 printf("MXS NAND: BCH write timeout\n");
748                 goto rtn;
749         }
750
751 rtn:
752         mxs_nand_return_dma_descs(nand_info);
753         return 0;
754 }
755
756 /*
757  * Read OOB from NAND.
758  *
759  * This function is a veneer that replaces the function originally installed by
760  * the NAND Flash MTD code.
761  */
762 static int mxs_nand_hook_read_oob(struct mtd_info *mtd, loff_t from,
763                                         struct mtd_oob_ops *ops)
764 {
765         struct nand_chip *chip = mtd->priv;
766         struct mxs_nand_info *nand_info = chip->priv;
767         int ret;
768
769         if (ops->mode == MTD_OPS_RAW)
770                 nand_info->raw_oob_mode = 1;
771         else
772                 nand_info->raw_oob_mode = 0;
773
774         ret = nand_info->hooked_read_oob(mtd, from, ops);
775
776         nand_info->raw_oob_mode = 0;
777
778         return ret;
779 }
780
781 /*
782  * Write OOB to NAND.
783  *
784  * This function is a veneer that replaces the function originally installed by
785  * the NAND Flash MTD code.
786  */
787 static int mxs_nand_hook_write_oob(struct mtd_info *mtd, loff_t to,
788                                         struct mtd_oob_ops *ops)
789 {
790         struct nand_chip *chip = mtd->priv;
791         struct mxs_nand_info *nand_info = chip->priv;
792         int ret;
793
794         if (ops->mode == MTD_OPS_RAW)
795                 nand_info->raw_oob_mode = 1;
796         else
797                 nand_info->raw_oob_mode = 0;
798
799         ret = nand_info->hooked_write_oob(mtd, to, ops);
800
801         nand_info->raw_oob_mode = 0;
802
803         return ret;
804 }
805
806 /*
807  * Mark a block bad in NAND.
808  *
809  * This function is a veneer that replaces the function originally installed by
810  * the NAND Flash MTD code.
811  */
812 static int mxs_nand_hook_block_markbad(struct mtd_info *mtd, loff_t ofs)
813 {
814         struct nand_chip *chip = mtd->priv;
815         struct mxs_nand_info *nand_info = chip->priv;
816         int ret;
817
818         nand_info->marking_block_bad = 1;
819
820         ret = nand_info->hooked_block_markbad(mtd, ofs);
821
822         nand_info->marking_block_bad = 0;
823
824         return ret;
825 }
826
827 /*
828  * There are several places in this driver where we have to handle the OOB and
829  * block marks. This is the function where things are the most complicated, so
830  * this is where we try to explain it all. All the other places refer back to
831  * here.
832  *
833  * These are the rules, in order of decreasing importance:
834  *
835  * 1) Nothing the caller does can be allowed to imperil the block mark, so all
836  *    write operations take measures to protect it.
837  *
838  * 2) In read operations, the first byte of the OOB we return must reflect the
839  *    true state of the block mark, no matter where that block mark appears in
840  *    the physical page.
841  *
842  * 3) ECC-based read operations return an OOB full of set bits (since we never
843  *    allow ECC-based writes to the OOB, it doesn't matter what ECC-based reads
844  *    return).
845  *
846  * 4) "Raw" read operations return a direct view of the physical bytes in the
847  *    page, using the conventional definition of which bytes are data and which
848  *    are OOB. This gives the caller a way to see the actual, physical bytes
849  *    in the page, without the distortions applied by our ECC engine.
850  *
851  * What we do for this specific read operation depends on whether we're doing
852  * "raw" read, or an ECC-based read.
853  *
854  * It turns out that knowing whether we want an "ECC-based" or "raw" read is not
855  * easy. When reading a page, for example, the NAND Flash MTD code calls our
856  * ecc.read_page or ecc.read_page_raw function. Thus, the fact that MTD wants an
857  * ECC-based or raw view of the page is implicit in which function it calls
858  * (there is a similar pair of ECC-based/raw functions for writing).
859  *
860  * Since MTD assumes the OOB is not covered by ECC, there is no pair of
861  * ECC-based/raw functions for reading or or writing the OOB. The fact that the
862  * caller wants an ECC-based or raw view of the page is not propagated down to
863  * this driver.
864  *
865  * Since our OOB *is* covered by ECC, we need this information. So, we hook the
866  * ecc.read_oob and ecc.write_oob function pointers in the owning
867  * struct mtd_info with our own functions. These hook functions set the
868  * raw_oob_mode field so that, when control finally arrives here, we'll know
869  * what to do.
870  */
871 static int mxs_nand_ecc_read_oob(struct mtd_info *mtd, struct nand_chip *nand,
872                                 int page)
873 {
874         struct mxs_nand_info *nand_info = nand->priv;
875
876         /*
877          * First, fill in the OOB buffer. If we're doing a raw read, we need to
878          * get the bytes from the physical page. If we're not doing a raw read,
879          * we need to fill the buffer with set bits.
880          */
881         if (nand_info->raw_oob_mode) {
882                 /*
883                  * If control arrives here, we're doing a "raw" read. Send the
884                  * command to read the conventional OOB and read it.
885                  */
886                 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
887                 nand->read_buf(mtd, nand->oob_poi, mtd->oobsize);
888         } else {
889                 /*
890                  * If control arrives here, we're not doing a "raw" read. Fill
891                  * the OOB buffer with set bits and correct the block mark.
892                  */
893                 memset(nand->oob_poi, 0xff, mtd->oobsize);
894
895                 nand->cmdfunc(mtd, NAND_CMD_READ0, mtd->writesize, page);
896                 mxs_nand_read_buf(mtd, nand->oob_poi, 1);
897         }
898
899         return 0;
900
901 }
902
903 /*
904  * Write OOB data to NAND.
905  */
906 static int mxs_nand_ecc_write_oob(struct mtd_info *mtd, struct nand_chip *nand,
907                                         int page)
908 {
909         struct mxs_nand_info *nand_info = nand->priv;
910         uint8_t block_mark = 0;
911
912         /*
913          * There are fundamental incompatibilities between the i.MX GPMI NFC and
914          * the NAND Flash MTD model that make it essentially impossible to write
915          * the out-of-band bytes.
916          *
917          * We permit *ONE* exception. If the *intent* of writing the OOB is to
918          * mark a block bad, we can do that.
919          */
920
921         if (!nand_info->marking_block_bad) {
922                 printf("NXS NAND: Writing OOB isn't supported\n");
923                 return -EIO;
924         }
925
926         /* Write the block mark. */
927         nand->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
928         nand->write_buf(mtd, &block_mark, 1);
929         nand->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
930
931         /* Check if it worked. */
932         if (nand->waitfunc(mtd, nand) & NAND_STATUS_FAIL)
933                 return -EIO;
934
935         return 0;
936 }
937
938 /*
939  * Claims all blocks are good.
940  *
941  * In principle, this function is *only* called when the NAND Flash MTD system
942  * isn't allowed to keep an in-memory bad block table, so it is forced to ask
943  * the driver for bad block information.
944  *
945  * In fact, we permit the NAND Flash MTD system to have an in-memory BBT, so
946  * this function is *only* called when we take it away.
947  *
948  * Thus, this function is only called when we want *all* blocks to look good,
949  * so it *always* return success.
950  */
951 static int mxs_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
952 {
953         return 0;
954 }
955
956 /*
957  * Nominally, the purpose of this function is to look for or create the bad
958  * block table. In fact, since the we call this function at the very end of
959  * the initialization process started by nand_scan(), and we doesn't have a
960  * more formal mechanism, we "hook" this function to continue init process.
961  *
962  * At this point, the physical NAND Flash chips have been identified and
963  * counted, so we know the physical geometry. This enables us to make some
964  * important configuration decisions.
965  *
966  * The return value of this function propogates directly back to this driver's
967  * call to nand_scan(). Anything other than zero will cause this driver to
968  * tear everything down and declare failure.
969  */
970 static int mxs_nand_scan_bbt(struct mtd_info *mtd)
971 {
972         struct nand_chip *nand = mtd->priv;
973         struct mxs_nand_info *nand_info = nand->priv;
974         struct mxs_bch_regs *bch_regs = (struct mxs_bch_regs *)MXS_BCH_BASE;
975         uint32_t tmp;
976
977         if (mtd->oobsize > MXS_NAND_CHUNK_DATA_CHUNK_SIZE) {
978                 galois_field = 14;
979                 chunk_data_size = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 2;
980         }
981
982         if (mtd->oobsize > chunk_data_size) {
983                 printf("Not support the NAND chips whose oob size is larger then %d bytes!\n", chunk_data_size);
984                 return -EINVAL;
985         }
986
987         /* Configure BCH and set NFC geometry */
988         mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
989
990         /* Configure layout 0 */
991         tmp = (mxs_nand_ecc_chunk_cnt(mtd->writesize) - 1)
992                 << BCH_FLASHLAYOUT0_NBLOCKS_OFFSET;
993         tmp |= MXS_NAND_METADATA_SIZE << BCH_FLASHLAYOUT0_META_SIZE_OFFSET;
994         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
995                 << BCH_FLASHLAYOUT0_ECC0_OFFSET;
996         tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
997         tmp |= (14 == galois_field ? 1 : 0) <<
998                 BCH_FLASHLAYOUT0_GF13_0_GF14_1_OFFSET;
999         writel(tmp, &bch_regs->hw_bch_flash0layout0);
1000
1001         tmp = (mtd->writesize + mtd->oobsize)
1002                 << BCH_FLASHLAYOUT1_PAGE_SIZE_OFFSET;
1003         tmp |= (mxs_nand_get_ecc_strength(mtd->writesize, mtd->oobsize) >> 1)
1004                 << BCH_FLASHLAYOUT1_ECCN_OFFSET;
1005         tmp |= chunk_data_size >> MXS_NAND_CHUNK_DATA_CHUNK_SIZE_SHIFT;
1006         tmp |= (14 == galois_field ? 1 : 0) <<
1007                 BCH_FLASHLAYOUT1_GF13_0_GF14_1_OFFSET;
1008         writel(tmp, &bch_regs->hw_bch_flash0layout1);
1009
1010         /* Set *all* chip selects to use layout 0 */
1011         writel(0, &bch_regs->hw_bch_layoutselect);
1012
1013         /* Enable BCH complete interrupt */
1014         writel(BCH_CTRL_COMPLETE_IRQ_EN, &bch_regs->hw_bch_ctrl_set);
1015
1016         /* Hook some operations at the MTD level. */
1017         if (mtd->_read_oob != mxs_nand_hook_read_oob) {
1018                 nand_info->hooked_read_oob = mtd->_read_oob;
1019                 mtd->_read_oob = mxs_nand_hook_read_oob;
1020         }
1021
1022         if (mtd->_write_oob != mxs_nand_hook_write_oob) {
1023                 nand_info->hooked_write_oob = mtd->_write_oob;
1024                 mtd->_write_oob = mxs_nand_hook_write_oob;
1025         }
1026
1027         if (mtd->_block_markbad != mxs_nand_hook_block_markbad) {
1028                 nand_info->hooked_block_markbad = mtd->_block_markbad;
1029                 mtd->_block_markbad = mxs_nand_hook_block_markbad;
1030         }
1031
1032         /* We use the reference implementation for bad block management. */
1033         return nand_default_bbt(mtd);
1034 }
1035
1036 /*
1037  * Allocate DMA buffers
1038  */
1039 int mxs_nand_alloc_buffers(struct mxs_nand_info *nand_info)
1040 {
1041         uint8_t *buf;
1042         const int size = NAND_MAX_PAGESIZE + NAND_MAX_OOBSIZE;
1043
1044         nand_info->data_buf_size = roundup(size, MXS_DMA_ALIGNMENT);
1045
1046         /* DMA buffers */
1047         buf = memalign(MXS_DMA_ALIGNMENT, nand_info->data_buf_size);
1048         if (!buf) {
1049                 printf("MXS NAND: Error allocating DMA buffers\n");
1050                 return -ENOMEM;
1051         }
1052
1053         memset(buf, 0, nand_info->data_buf_size);
1054
1055         nand_info->data_buf = buf;
1056         nand_info->oob_buf = buf + NAND_MAX_PAGESIZE;
1057         /* Command buffers */
1058         nand_info->cmd_buf = memalign(MXS_DMA_ALIGNMENT,
1059                                 MXS_NAND_COMMAND_BUFFER_SIZE);
1060         if (!nand_info->cmd_buf) {
1061                 free(buf);
1062                 printf("MXS NAND: Error allocating command buffers\n");
1063                 return -ENOMEM;
1064         }
1065         memset(nand_info->cmd_buf, 0, MXS_NAND_COMMAND_BUFFER_SIZE);
1066         nand_info->cmd_queue_len = 0;
1067
1068         return 0;
1069 }
1070
1071 /*
1072  * Initializes the NFC hardware.
1073  */
1074 int mxs_nand_init(struct mxs_nand_info *info)
1075 {
1076         struct mxs_gpmi_regs *gpmi_regs =
1077                 (struct mxs_gpmi_regs *)MXS_GPMI_BASE;
1078         struct mxs_bch_regs *bch_regs =
1079                 (struct mxs_bch_regs *)MXS_BCH_BASE;
1080         int i = 0, j;
1081
1082         info->desc = malloc(sizeof(struct mxs_dma_desc *) *
1083                                 MXS_NAND_DMA_DESCRIPTOR_COUNT);
1084         if (!info->desc)
1085                 goto err1;
1086
1087         /* Allocate the DMA descriptors. */
1088         for (i = 0; i < MXS_NAND_DMA_DESCRIPTOR_COUNT; i++) {
1089                 info->desc[i] = mxs_dma_desc_alloc();
1090                 if (!info->desc[i])
1091                         goto err2;
1092         }
1093
1094         /* Init the DMA controller. */
1095         for (j = MXS_DMA_CHANNEL_AHB_APBH_GPMI0;
1096                 j <= MXS_DMA_CHANNEL_AHB_APBH_GPMI7; j++) {
1097                 if (mxs_dma_init_channel(j))
1098                         goto err3;
1099         }
1100
1101         /* Reset the GPMI block. */
1102         mxs_reset_block(&gpmi_regs->hw_gpmi_ctrl0_reg);
1103         mxs_reset_block(&bch_regs->hw_bch_ctrl_reg);
1104
1105         /*
1106          * Choose NAND mode, set IRQ polarity, disable write protection and
1107          * select BCH ECC.
1108          */
1109         clrsetbits_le32(&gpmi_regs->hw_gpmi_ctrl1,
1110                         GPMI_CTRL1_GPMI_MODE,
1111                         GPMI_CTRL1_ATA_IRQRDY_POLARITY | GPMI_CTRL1_DEV_RESET |
1112                         GPMI_CTRL1_BCH_MODE);
1113
1114         return 0;
1115
1116 err3:
1117         for (--j; j >= 0; j--)
1118                 mxs_dma_release(j);
1119 err2:
1120         free(info->desc);
1121 err1:
1122         for (--i; i >= 0; i--)
1123                 mxs_dma_desc_free(info->desc[i]);
1124         printf("MXS NAND: Unable to allocate DMA descriptors\n");
1125         return -ENOMEM;
1126 }
1127
1128 /*!
1129  * This function is called during the driver binding process.
1130  *
1131  * @param   pdev  the device structure used to store device specific
1132  *                information that is used by the suspend, resume and
1133  *                remove functions
1134  *
1135  * @return  The function always returns 0.
1136  */
1137 int board_nand_init(struct nand_chip *nand)
1138 {
1139         struct mxs_nand_info *nand_info;
1140         int err;
1141
1142         nand_info = malloc(sizeof(struct mxs_nand_info));
1143         if (!nand_info) {
1144                 printf("MXS NAND: Failed to allocate private data\n");
1145                 return -ENOMEM;
1146         }
1147         memset(nand_info, 0, sizeof(struct mxs_nand_info));
1148
1149         err = mxs_nand_alloc_buffers(nand_info);
1150         if (err)
1151                 goto err1;
1152
1153         err = mxs_nand_init(nand_info);
1154         if (err)
1155                 goto err2;
1156
1157         memset(&fake_ecc_layout, 0, sizeof(fake_ecc_layout));
1158
1159         nand->priv = nand_info;
1160         nand->options |= NAND_NO_SUBPAGE_WRITE;
1161
1162         nand->cmd_ctrl          = mxs_nand_cmd_ctrl;
1163
1164         nand->dev_ready         = mxs_nand_device_ready;
1165         nand->select_chip       = mxs_nand_select_chip;
1166         nand->block_bad         = mxs_nand_block_bad;
1167         nand->scan_bbt          = mxs_nand_scan_bbt;
1168
1169         nand->read_byte         = mxs_nand_read_byte;
1170
1171         nand->read_buf          = mxs_nand_read_buf;
1172         nand->write_buf         = mxs_nand_write_buf;
1173
1174         nand->ecc.read_page     = mxs_nand_ecc_read_page;
1175         nand->ecc.write_page    = mxs_nand_ecc_write_page;
1176         nand->ecc.read_oob      = mxs_nand_ecc_read_oob;
1177         nand->ecc.write_oob     = mxs_nand_ecc_write_oob;
1178
1179         nand->ecc.layout        = &fake_ecc_layout;
1180         nand->ecc.mode          = NAND_ECC_HW;
1181         nand->ecc.bytes         = 9;
1182         nand->ecc.size          = 512;
1183         nand->ecc.strength      = 8;
1184
1185         return 0;
1186
1187 err2:
1188         free(nand_info->data_buf);
1189         free(nand_info->cmd_buf);
1190 err1:
1191         free(nand_info);
1192         return err;
1193 }