]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - drivers/mtd/nand/mxc_nand.c
applied patches from Freescale and Ka-Ro
[karo-tx-uboot.git] / drivers / mtd / nand / mxc_nand.c
1 /*
2  * Copyright 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
3  */
4
5 /*
6  * The code contained herein is licensed under the GNU General Public
7  * License. You may obtain a copy of the GNU General Public License
8  * Version 2 or later at the following locations:
9  *
10  * http://www.opensource.org/licenses/gpl-license.html
11  * http://www.gnu.org/copyleft/gpl.html
12  */
13
14 #include <common.h>
15 #include <malloc.h>
16 #include <asm/io.h>
17 #include <asm/errno.h>
18 #include <linux/mtd/nand.h>
19 #include <asm-arm/arch/mxc_nand.h>
20 #include "nand_device_info.h"
21
22 struct nand_info {
23         int status_req;
24         int large_page;
25         int auto_mode;
26         u16 col_addr;
27         u8 num_of_intlv;
28         int page_mask;
29         int hw_ecc;
30         u8 *data_buf;
31         u8 *oob_buf;
32 };
33
34 /*
35  * Define delays in microsec for NAND device operations
36  */
37 #define TROP_US_DELAY   2000
38
39 /*
40  * OOB placement block for use with hardware ecc generation
41  */
42 static struct nand_ecclayout nand_hw_eccoob_512 = {
43         .eccbytes = 9,
44         .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
45         .oobfree = {{0, 4} }
46 };
47
48 static struct nand_ecclayout nand_hw_eccoob_2k = {
49         .eccbytes = 9,
50         .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
51         .oobfree = {{2, 4} }
52 };
53
54 static struct nand_ecclayout nand_hw_eccoob_4k = {
55         .eccbytes = 9,
56         .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
57         .oobfree = {{2, 4} }
58 };
59
60
61 static void mxc_nand_bi_swap(struct mtd_info *mtd)
62 {
63         struct nand_chip *this = mtd->priv;
64         struct nand_info *info = this->priv;
65         u16 ma, sa, nma, nsa;
66
67         if (!IS_LARGE_PAGE_NAND)
68                 return;
69
70         ma = __raw_readw(BAD_BLK_MARKER_MAIN);
71         sa = __raw_readw(BAD_BLK_MARKER_SP);
72
73         nma = (ma & 0xFF00) | (sa >> 8);
74         nsa = (sa & 0x00FF) | (ma << 8);
75
76         __raw_writew(nma, BAD_BLK_MARKER_MAIN);
77         __raw_writew(nsa, BAD_BLK_MARKER_SP);
78
79 }
80
81 /*!
82  * @defgroup NAND_MTD NAND Flash MTD Driver for MXC processors
83  */
84
85 /*!
86  * @file mxc_nd2.c
87  *
88  * @brief This file contains the hardware specific layer for NAND Flash on
89  * MXC processor
90  *
91  * @ingroup NAND_MTD
92  */
93
94 /*!
95  * Half word access.Added for U-boot.
96  */
97 static void *nfc_memcpy(void *dest, const void *src, size_t n)
98 {
99         u16 *dst_16 = (u16 *) dest;
100         const u16 *src_16 = (u16 *) src;
101
102         while (n > 0) {
103                 *dst_16++ = *src_16++;
104                 n -= 2;
105         }
106
107         return dest;
108 }
109
110 /*
111  * Functions to transfer data to/from spare erea.
112  */
113 static void
114 copy_spare(struct mtd_info *mtd, void *pbuf, void *pspare, int len, int bfrom)
115 {
116         u16 i, j;
117         u16 m = mtd->oobsize;
118         u16 n = mtd->writesize >> 9;
119         u8 *d = (u8 *) pbuf;
120         u8 *s = (u8 *) pspare;
121         u16 t = SPARE_LEN;
122         struct nand_chip *this = mtd->priv;
123         struct nand_info *info = this->priv;
124
125         m /= info->num_of_intlv;
126         n /= info->num_of_intlv;
127
128         j = (m / n >> 1) << 1;
129
130         if (bfrom) {
131                 for (i = 0; i < n - 1; i++)
132                         nfc_memcpy(&d[i * j], &s[i * t], j);
133
134                 /* the last section */
135                 nfc_memcpy(&d[i * j], &s[i * t], len - i * j);
136         } else {
137                 for (i = 0; i < n - 1; i++)
138                         nfc_memcpy(&s[i * t], &d[i * j], j);
139
140                 /* the last section */
141                 nfc_memcpy(&s[i * t], &d[i * j], len - i * j);
142         }
143 }
144
145 /*!
146  * This function polls the NFC to wait for the basic operation to complete by
147  * checking the INT bit of config2 register.
148  *
149  * @param       maxRetries     number of retry attempts (separated by 1 us)
150  * @param       useirq         True if IRQ should be used rather than polling
151  */
152 static void wait_op_done(int max_retries)
153 {
154
155         while (max_retries-- > 0) {
156                 if (raw_read(REG_NFC_OPS_STAT) & NFC_OPS_STAT) {
157                         WRITE_NFC_IP_REG((raw_read(REG_NFC_OPS_STAT) &
158                                         ~NFC_OPS_STAT),
159                                         REG_NFC_OPS_STAT);
160                         break;
161                 }
162                 udelay(1);
163         }
164         if (max_retries <= 0)
165                 MTDDEBUG(MTD_DEBUG_LEVEL0, "wait: INT not set\n");
166 }
167
168 /*!
169  * This function sends an address (or partial address) to the
170  * NAND device.  The address is used to select the source/destination for
171  * a NAND command.
172  *
173  * @param       addr    address to be written to NFC.
174  * @param       useirq  True if IRQ should be used rather than polling
175  */
176 static void send_addr(u16 addr)
177 {
178         MTDDEBUG(MTD_DEBUG_LEVEL3, "send_addr(0x%x)\n", addr);
179
180         /* fill address */
181         raw_write((addr << NFC_FLASH_ADDR_SHIFT), REG_NFC_FLASH_ADDR);
182
183         /* clear status */
184         ACK_OPS;
185
186         /* send out address */
187         raw_write(NFC_ADDR, REG_NFC_OPS);
188
189         /* Wait for operation to complete */
190         wait_op_done(TROP_US_DELAY);
191 }
192
193 static void mxc_do_addr_cycle_auto(struct mtd_info *mtd, int column,
194                                                         int page_addr)
195 {
196 #ifdef CONFIG_MXC_NFC_SP_AUTO
197         if (page_addr != -1 && column != -1) {
198                 u32 mask = 0xFFFF;
199                 /* the column address */
200                 raw_write(column & mask, NFC_FLASH_ADDR0);
201                 raw_write((raw_read(NFC_FLASH_ADDR0) |
202                            ((page_addr & mask) << 16)), NFC_FLASH_ADDR0);
203                 /* the row address */
204                 raw_write(((raw_read(NFC_FLASH_ADDR8) & (mask << 16)) |
205                            ((page_addr & (mask << 16)) >> 16)),
206                           NFC_FLASH_ADDR8);
207         } else if (page_addr != -1) {
208                 raw_write(page_addr, NFC_FLASH_ADDR0);
209         }
210
211         MTDDEBUG(MTD_DEBUG_LEVEL3,
212               "AutoMode:the ADDR REGS value is (0x%x, 0x%x)\n",
213               raw_read(NFC_FLASH_ADDR0), raw_read(NFC_FLASH_ADDR8));
214 #endif
215 }
216
217 static void mxc_do_addr_cycle_atomic(struct mtd_info *mtd, int column,
218                                                         int page_addr)
219 {
220         struct nand_chip *this = mtd->priv;
221         struct nand_info *info = this->priv;
222
223         u32 page_mask = info->page_mask;
224
225         if (column != -1) {
226                 send_addr(column & 0xFF);
227                 if (IS_2K_PAGE_NAND) {
228                         /* another col addr cycle for 2k page */
229                         send_addr((column >> 8) & 0xF);
230                 } else if (IS_4K_PAGE_NAND) {
231                         /* another col addr cycle for 4k page */
232                         send_addr((column >> 8) & 0x1F);
233                 }
234         }
235         if (page_addr != -1) {
236                 do {
237                         send_addr(page_addr & 0xff);
238                         page_mask >>= 8;
239                         page_addr >>= 8;
240                 } while (page_mask != 0);
241         }
242 }
243
244 /*
245  * Function to perform the address cycles.
246  */
247 static void mxc_nand_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
248 {
249         struct nand_chip *this = mtd->priv;
250         struct nand_info *info = this->priv;
251
252         if (info->auto_mode)
253                 mxc_do_addr_cycle_auto(mtd, column, page_addr);
254         else
255                 mxc_do_addr_cycle_atomic(mtd, column, page_addr);
256 }
257
258 static void send_cmd_atomic(struct mtd_info *mtd, u16 cmd)
259 {
260         /* fill command */
261         raw_write(cmd, REG_NFC_FLASH_CMD);
262
263         /* clear status */
264         ACK_OPS;
265
266         /* send out command */
267         raw_write(NFC_CMD, REG_NFC_OPS);
268
269         /* Wait for operation to complete */
270         wait_op_done(TROP_US_DELAY);
271 }
272
273 /*
274  * Function to record the ECC corrected/uncorrected errors resulted
275  * after a page read. This NFC detects and corrects upto to 4 symbols
276  * of 9-bits each.
277  */
278 static int mxc_nand_ecc_status(struct mtd_info *mtd)
279 {
280         u32 ecc_stat, err;
281         int no_subpages = 1;
282         int ret = 0;
283         u8 ecc_bit_mask, err_limit;
284         struct nand_chip *this = mtd->priv;
285         struct nand_info *info = this->priv;
286
287         ecc_bit_mask = (IS_4BIT_ECC ? 0x7 : 0xf);
288         err_limit = (IS_4BIT_ECC ? 0x4 : 0x8);
289
290         no_subpages = mtd->writesize >> 9;
291
292         no_subpages /= info->num_of_intlv;
293
294         ecc_stat = GET_NFC_ECC_STATUS();
295         do {
296                 err = ecc_stat & ecc_bit_mask;
297                 if (err > err_limit) {
298                         printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
299                         return -1;
300                 } else {
301                         ret += err;
302                 }
303                 ecc_stat >>= 4;
304         } while (--no_subpages);
305
306         MTDDEBUG(MTD_DEBUG_LEVEL3, "%d Symbol Correctable RS-ECC Error\n", ret);
307
308         return ret;
309 }
310
311 /*!
312  * This function handle the interleave related work
313  * @param       mtd     mtd info
314  * @param       cmd     command
315  */
316 static void send_cmd_interleave(struct mtd_info *mtd, u16 cmd)
317 {
318 #ifdef CONFIG_MXC_NFC_SP_AUTO
319
320         struct nand_chip *this = mtd->priv;
321         struct nand_info *info = this->priv;
322         u32 addr_low = raw_read(NFC_FLASH_ADDR0);
323         u32 addr_high = raw_read(NFC_FLASH_ADDR8);
324         u32 page_addr = addr_low >> 16 | addr_high << 16;
325         u32 i, j = info->num_of_intlv;
326         u8 *dbuf = info->data_buf;
327         u8 *obuf = info->oob_buf;
328         u32 dlen = mtd->writesize / j;
329         u32 olen = mtd->oobsize / j;
330
331         /* adjust the addr value
332          * since ADD_OP mode is 01
333          */
334         if (j > 1)
335                 page_addr *= j;
336         else
337                 page_addr *= this->numchips;
338
339         switch (cmd) {
340         case NAND_CMD_PAGEPROG:
341                 for (i = 0; i < j; i++) {
342                         /* reset addr cycle */
343                         if (j > 1)
344                                 mxc_nand_addr_cycle(mtd, 0, page_addr++);
345
346                         /* data transfer */
347                         nfc_memcpy(MAIN_AREA0, dbuf, dlen);
348                         copy_spare(mtd, obuf, SPARE_AREA0, olen, 0);
349                         mxc_nand_bi_swap(mtd);
350
351                         /* update the value */
352                         dbuf += dlen;
353                         obuf += olen;
354
355                         NFC_SET_RBA(0);
356                         raw_write(0, REG_NFC_OPS_STAT);
357                         raw_write(NFC_AUTO_PROG, REG_NFC_OPS);
358
359                         /* wait auto_prog_done bit set */
360                         while (!(raw_read(REG_NFC_OPS_STAT) & NFC_OP_DONE))
361                                 ;
362                 }
363
364                 wait_op_done(TROP_US_DELAY);
365                 while (!(raw_read(REG_NFC_OPS_STAT) & NFC_RB));
366
367                 break;
368         case NAND_CMD_READSTART:
369                 for (i = 0; i < j; i++) {
370                         /* reset addr cycle */
371                         if (j > 1)
372                                 mxc_nand_addr_cycle(mtd, 0, page_addr++);
373
374                         NFC_SET_RBA(0);
375                         raw_write(0, REG_NFC_OPS_STAT);
376                         raw_write(NFC_AUTO_READ, REG_NFC_OPS);
377                         wait_op_done(TROP_US_DELAY);
378
379                         /* check ecc error */
380                         mxc_nand_ecc_status(mtd);
381
382                         /* data transfer */
383                         mxc_nand_bi_swap(mtd);
384                         nfc_memcpy(dbuf, MAIN_AREA0, dlen);
385                         copy_spare(mtd, obuf, SPARE_AREA0, olen, 1);
386
387                         /* update the value */
388                         dbuf += dlen;
389                         obuf += olen;
390                 }
391                 break;
392         case NAND_CMD_ERASE2:
393                 for (i = 0; i < j; i++) {
394                         if (!i) {
395                                 page_addr = addr_low;
396                                 page_addr *= (j > 1 ? j : this->numchips);
397                         }
398                         mxc_nand_addr_cycle(mtd, -1, page_addr++);
399                         raw_write(0, REG_NFC_OPS_STAT);
400                         raw_write(NFC_AUTO_ERASE, REG_NFC_OPS);
401                         wait_op_done(TROP_US_DELAY);
402                 }
403                 break;
404         case NAND_CMD_RESET:
405                 for (i = 0; i < j; i++) {
406                         if (j > 1)
407                                 NFC_SET_NFC_ACTIVE_CS(i);
408                         send_cmd_atomic(mtd, cmd);
409                 }
410                 break;
411         default:
412                 break;
413         }
414 #endif
415 }
416
417 static void send_cmd_auto(struct mtd_info *mtd, u16 cmd)
418 {
419 #ifdef CONFIG_MXC_NFC_SP_AUTO
420         switch (cmd) {
421         case NAND_CMD_READ0:
422         case NAND_CMD_READOOB:
423                 raw_write(NAND_CMD_READ0, REG_NFC_FLASH_CMD);
424                 break;
425         case NAND_CMD_SEQIN:
426         case NAND_CMD_ERASE1:
427                 raw_write(cmd, REG_NFC_FLASH_CMD);
428                 break;
429         case NAND_CMD_PAGEPROG:
430         case NAND_CMD_ERASE2:
431         case NAND_CMD_READSTART:
432                 raw_write(raw_read(REG_NFC_FLASH_CMD) | cmd << NFC_CMD_1_SHIFT,
433                           REG_NFC_FLASH_CMD);
434                 send_cmd_interleave(mtd, cmd);
435                 break;
436         case NAND_CMD_READID:
437                 send_cmd_atomic(mtd, cmd);
438                 send_addr(0);
439                 break;
440         case NAND_CMD_RESET:
441                 send_cmd_interleave(mtd, cmd);
442                 break;
443         case NAND_CMD_STATUS:
444                 send_cmd_atomic(mtd, cmd);
445                 break;
446         default:
447                 break;
448         }
449 #endif
450 }
451
452 /*!
453  * This function issues the specified command to the NAND device and
454  * waits for completion.
455  *
456  * @param       cmd     command for NAND Flash
457  * @param       useirq  True if IRQ should be used rather than polling
458  */
459 static void send_cmd(struct mtd_info *mtd, u16 cmd)
460 {
461
462         struct nand_chip *this = mtd->priv;
463         struct nand_info *info = this->priv;
464
465         if (info->auto_mode)
466                 send_cmd_auto(mtd, cmd);
467         else
468                 send_cmd_atomic(mtd, cmd);
469
470         MTDDEBUG(MTD_DEBUG_LEVEL3, "send_cmd(0x%x)\n", cmd);
471 }
472
473 /*!
474  * This function requests the NFC to initate the transfer
475  * of data currently in the NFC RAM buffer to the NAND device.
476  *
477  * @param       buf_id        Specify Internal RAM Buffer number
478  */
479 static void send_prog_page(struct mtd_info *mtd, u8 buf_id)
480 {
481         struct nand_chip *this = mtd->priv;
482         struct nand_info *info = this->priv;
483
484         if (!info->auto_mode) {
485                 /* set ram buffer id */
486                 NFC_SET_RBA(buf_id);
487
488                 /* clear status */
489                 ACK_OPS;
490
491                 /* transfer data from NFC ram to nand */
492                 raw_write(NFC_INPUT, REG_NFC_OPS);
493
494                 /* Wait for operation to complete */
495                 wait_op_done(TROP_US_DELAY);
496
497                 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s\n", __func__);
498         }
499 }
500
501 /*!
502  * This function requests the NFC to initated the transfer
503  * of data from the NAND device into in the NFC ram buffer.
504  *
505  * @param       buf_id          Specify Internal RAM Buffer number
506  */
507 static void send_read_page(struct mtd_info *mtd, u8 buf_id)
508 {
509         struct nand_chip *this = mtd->priv;
510         struct nand_info *info = this->priv;
511
512         if (!info->auto_mode) {
513
514                 /* set ram buffer id */
515                 NFC_SET_RBA(buf_id);
516
517                 /* clear status */
518                 ACK_OPS;
519
520                 /* transfer data from nand to NFC ram */
521                 raw_write(NFC_OUTPUT, REG_NFC_OPS);
522
523                 /* Wait for operation to complete */
524                 wait_op_done(TROP_US_DELAY);
525
526                 MTDDEBUG(MTD_DEBUG_LEVEL3, "%s(%d)\n", __func__, buf_id);
527
528         }
529
530 }
531
532 /*!
533  * This function requests the NFC to perform a read of the
534  * NAND device ID.
535  */
536 static void send_read_id(void)
537 {
538         /* Set RBA bits for BUFFER0 */
539         NFC_SET_RBA(0);
540
541         /* clear status */
542         ACK_OPS;
543
544         /* Read ID into main buffer */
545         raw_write(NFC_ID, REG_NFC_OPS);
546
547         /* Wait for operation to complete */
548         wait_op_done(TROP_US_DELAY);
549
550 }
551
552 static u16 mxc_do_status_auto(struct mtd_info *mtd)
553 {
554         u16 status = 0;
555 #ifdef CONFIG_MXC_NFC_SP_AUTO
556         int i = 0;
557         u32 mask = 0xFF << 16;
558         struct nand_chip *this = mtd->priv;
559         struct nand_info *info = this->priv;
560
561         for (; i < info->num_of_intlv; i++) {
562
563                 /* set ative cs */
564                 NFC_SET_NFC_ACTIVE_CS(i);
565
566                 /* clear status */
567                 ACK_OPS;
568
569                 /* use atomic mode to read status instead
570                  * of using auto mode,auto-mode has issues
571                  * and the status is not correct.
572                 */
573                 raw_write(NFC_STATUS, REG_NFC_OPS);
574
575                 wait_op_done(TROP_US_DELAY);
576
577                 status = (raw_read(NFC_CONFIG1) & mask) >> 16;
578
579                 if (status & NAND_STATUS_FAIL)
580                         break;
581         }
582 #endif
583         return status;
584 }
585
586 static u16 mxc_do_status_atomic(struct mtd_info *mtd)
587 {
588         volatile u16 *mainBuf = MAIN_AREA1;
589         u8 val = 1;
590         u16 ret;
591
592         /* Set ram buffer id */
593         NFC_SET_RBA(val);
594
595         /* clear status */
596         ACK_OPS;
597
598         /* Read status into main buffer */
599         raw_write(NFC_STATUS, REG_NFC_OPS);
600
601         /* Wait for operation to complete */
602         wait_op_done(TROP_US_DELAY);
603
604         /* Status is placed in first word of main buffer */
605         /* get status, then recovery area 1 data */
606         ret = *mainBuf;
607
608         return ret;
609 }
610
611 /*!
612  * This function requests the NFC to perform a read of the
613  * NAND device status and returns the current status.
614  *
615  * @return  device status
616  */
617 static u16 mxc_nand_get_status(struct mtd_info *mtd)
618 {
619         struct nand_chip *this = mtd->priv;
620         struct nand_info *info = this->priv;
621         u16 status;
622
623         if (info->auto_mode)
624                 status = mxc_do_status_auto(mtd);
625         else
626                 status = mxc_do_status_atomic(mtd);
627
628         return status;
629
630 }
631
632 static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
633 {
634         raw_write((raw_read(REG_NFC_ECC_EN) | NFC_ECC_EN), REG_NFC_ECC_EN);
635         return;
636 }
637
638 /*
639  * Function to correct the detected errors. This NFC corrects all the errors
640  * detected. So this function just return 0.
641  */
642 static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
643                                  u_char *read_ecc, u_char *calc_ecc)
644 {
645         return 0;
646 }
647
648 /*
649  * Function to calculate the ECC for the data to be stored in the Nand device.
650  * This NFC has a hardware RS(511,503) ECC engine together with the RS ECC
651  * CONTROL blocks are responsible for detection  and correction of up to
652  * 8 symbols of 9 bits each in 528 byte page.
653  * So this function is just return 0.
654  */
655
656 static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
657                                   u_char *ecc_code)
658 {
659         return 0;
660 }
661
662 /*!
663  * This function id is used to read the data buffer from the NAND Flash. To
664  * read the data from NAND Flash first the data output cycle is initiated by
665  * the NFC, which copies the data to RAMbuffer. This data of length \b len is
666  * then copied to buffer \b buf.
667  *
668  * @param       mtd     MTD structure for the NAND Flash
669  * @param       buf     data to be read from NAND Flash
670  * @param       len     number of bytes to be read
671  */
672 static void mxc_nand_read_buf(struct mtd_info *mtd, u_char * buf, int len)
673 {
674         struct nand_chip *this = mtd->priv;
675         struct nand_info *info = this->priv;
676         u16 col = info->col_addr;
677         u8 *data_buf = info->data_buf;
678         u8 *oob_buf = info->oob_buf;
679
680         if (mtd->writesize) {
681
682                 int j = mtd->writesize - col;
683                 int n = mtd->oobsize + j;
684
685                 n = min(n, len);
686
687                 if (j > 0) {
688                         if (n > j) {
689                                 memcpy(buf, &data_buf[col], j);
690                                 memcpy(buf + j, &oob_buf[0], n - j);
691                         } else {
692                                 memcpy(buf, &data_buf[col], n);
693                         }
694                 } else {
695                         col -= mtd->writesize;
696                         memcpy(buf, &oob_buf[col], len);
697                 }
698
699                 /* update */
700                 info->col_addr += n;
701
702         } else {
703                 /* At flash identify phase,
704                  * mtd->writesize has not been
705                  * set correctly, it should
706                  * be zero.And len will less 2
707                  */
708                 memcpy(buf, &data_buf[col], len);
709
710                 /* update */
711                 info->col_addr += len;
712         }
713
714 }
715
716 /*!
717  * This function reads byte from the NAND Flash
718  *
719  * @param       mtd     MTD structure for the NAND Flash
720  *
721  * @return    data read from the NAND Flash
722  */
723 static uint8_t mxc_nand_read_byte(struct mtd_info *mtd)
724 {
725         struct nand_chip *this = mtd->priv;
726         struct nand_info *info = this->priv;
727         uint8_t ret;
728
729         /* Check for status request */
730         if (info->status_req)
731                 return mxc_nand_get_status(mtd) & 0xFF;
732
733         mxc_nand_read_buf(mtd, &ret, 1);
734
735         return ret;
736 }
737
738 /*!
739   * This function reads word from the NAND Flash
740   *
741   * @param     mtd     MTD structure for the NAND Flash
742   *
743   * @return    data read from the NAND Flash
744   */
745 static u16 mxc_nand_read_word(struct mtd_info *mtd)
746 {
747         u16 ret;
748
749         mxc_nand_read_buf(mtd, (uint8_t *) &ret, sizeof(u16));
750
751         return ret;
752 }
753
754 /*!
755  * This function reads byte from the NAND Flash
756  *
757  * @param     mtd     MTD structure for the NAND Flash
758  *
759  * @return    data read from the NAND Flash
760  */
761 static u_char mxc_nand_read_byte16(struct mtd_info *mtd)
762 {
763         struct nand_chip *this = mtd->priv;
764         struct nand_info *info = this->priv;
765
766         /* Check for status request */
767         if (info->status_req)
768                 return mxc_nand_get_status(mtd) & 0xFF;
769
770         return mxc_nand_read_word(mtd) & 0xFF;
771 }
772
773
774 /*!
775  * This function writes data of length \b len from buffer \b buf to the NAND
776  * internal RAM buffer's MAIN area 0.
777  *
778  * @param       mtd     MTD structure for the NAND Flash
779  * @param       buf     data to be written to NAND Flash
780  * @param       len     number of bytes to be written
781  */
782 static void mxc_nand_write_buf(struct mtd_info *mtd,
783                                const u_char *buf, int len)
784 {
785         struct nand_chip *this = mtd->priv;
786         struct nand_info *info = this->priv;
787         u16 col = info->col_addr;
788         u8 *data_buf = info->data_buf;
789         u8 *oob_buf = info->oob_buf;
790         int j = mtd->writesize - col;
791         int n = mtd->oobsize + j;
792
793         n = min(n, len);
794
795         if (j > 0) {
796                 if (n > j) {
797                         memcpy(&data_buf[col], buf, j);
798                         memcpy(&oob_buf[0], buf + j, n - j);
799                 } else {
800                         memcpy(&data_buf[col], buf, n);
801                 }
802         } else {
803                 col -= mtd->writesize;
804                 memcpy(&oob_buf[col], buf, len);
805         }
806
807         /* update */
808         info->col_addr += n;
809 }
810
811 /*!
812  * This function is used by the upper layer to verify the data in NAND Flash
813  * with the data in the \b buf.
814  *
815  * @param       mtd     MTD structure for the NAND Flash
816  * @param       buf     data to be verified
817  * @param       len     length of the data to be verified
818  *
819  * @return      -EFAULT if error else 0
820  *
821  */
822 static int mxc_nand_verify_buf(struct mtd_info *mtd, const u_char *buf,
823                                int len)
824 {
825         struct nand_chip *this = mtd->priv;
826         struct nand_info *info = this->priv;
827         u_char *s = info->data_buf;
828
829         const u_char *p = buf;
830
831         for (; len > 0; len--) {
832                 if (*p++ != *s++)
833                         return -1;
834         }
835
836         return 0;
837 }
838
839 /*!
840  * This function is used by upper layer for select and deselect of the NAND
841  * chip
842  *
843  * @param       mtd     MTD structure for the NAND Flash
844  * @param       chip    val indicating select or deselect
845  */
846 static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
847 {
848
849         switch (chip) {
850         case -1:
851                 break;
852
853         case 0 ... 7:
854                 NFC_SET_NFC_ACTIVE_CS(chip);
855                 break;
856
857         default:
858                 break;
859         }
860 }
861
862 /*!
863  * This function is used by the upper layer to write command to NAND Flash for
864  * different operations to be carried out on NAND Flash
865  *
866  * @param       mtd             MTD structure for the NAND Flash
867  * @param       command         command for NAND Flash
868  * @param       column          column offset for the page read
869  * @param       page_addr       page to be read from NAND Flash
870  */
871 static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
872                              int column, int page_addr)
873 {
874         struct nand_chip *this = mtd->priv;
875         struct nand_info *info = this->priv;
876
877         MTDDEBUG(MTD_DEBUG_LEVEL3,
878               "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
879               command, column, page_addr);
880         /*
881          * Reset command state information
882          */
883         info->status_req = 0;
884
885         /*
886          * Command pre-processing step
887          */
888         switch (command) {
889         case NAND_CMD_STATUS:
890                 info->col_addr = 0;
891                 info->status_req = 1;
892                 break;
893
894         case NAND_CMD_READ0:
895                 info->col_addr = column;
896                 break;
897
898         case NAND_CMD_READOOB:
899                 info->col_addr = column;
900                 command = NAND_CMD_READ0;
901                 break;
902
903         case NAND_CMD_SEQIN:
904                 if (column != 0) {
905
906                         /* FIXME: before send SEQIN command for
907                          * partial write,We need read one page out.
908                          * FSL NFC does not support partial write
909                          * It alway send out 512+ecc+512+ecc ...
910                          * for large page nand flash. But for small
911                          * page nand flash, it did support SPARE
912                          * ONLY operation. But to make driver
913                          * simple. We take the same as large page,read
914                          * whole page out and update. As for MLC nand
915                          * NOP(num of operation) = 1. Partial written
916                          * on one programed page is not allowed! We
917                          * can't limit it on the driver, it need the
918                          * upper layer applicaiton take care it
919                          */
920
921                         mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
922                 }
923
924                 info->col_addr = column;
925                 break;
926
927         case NAND_CMD_PAGEPROG:
928                 if (!info->auto_mode) {
929                         nfc_memcpy(MAIN_AREA0, info->data_buf, mtd->writesize);
930                         copy_spare(mtd, info->oob_buf, SPARE_AREA0,
931                                                 mtd->oobsize, 0);
932                         mxc_nand_bi_swap(mtd);
933                 }
934
935                 send_prog_page(mtd, 0);
936                 break;
937
938         case NAND_CMD_ERASE1:
939         case NAND_CMD_ERASE2:
940                 break;
941         }
942
943         /*
944          * Write out the command to the device.
945          */
946         send_cmd(mtd, command);
947
948         mxc_nand_addr_cycle(mtd, column, page_addr);
949
950         /*
951          * Command post-processing step
952          */
953         switch (command) {
954
955         case NAND_CMD_READOOB:
956         case NAND_CMD_READ0:
957                 if (info->large_page)
958                         /* send read confirm command */
959                         send_cmd(mtd, NAND_CMD_READSTART);
960
961                 send_read_page(mtd, 0);
962
963                 if (!info->auto_mode) {
964                         mxc_nand_ecc_status(mtd);
965                         mxc_nand_bi_swap(mtd);
966                         nfc_memcpy(info->data_buf, MAIN_AREA0, mtd->writesize);
967                         copy_spare(mtd, info->oob_buf, SPARE_AREA0,
968                                                 mtd->oobsize, 1);
969                 }
970                 break;
971
972         case NAND_CMD_READID:
973                 send_read_id();
974                 info->col_addr = column;
975                 nfc_memcpy(info->data_buf, MAIN_AREA0, 2048);
976                 break;
977         }
978 }
979
980 static int mxc_nand_read_oob(struct mtd_info *mtd,
981                              struct nand_chip *chip, int page, int sndcmd)
982 {
983         struct nand_chip *this = mtd->priv;
984         struct nand_info *info = this->priv;
985
986         if (sndcmd) {
987
988                 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
989                 sndcmd = 0;
990         }
991
992         memcpy(chip->oob_poi, info->oob_buf, mtd->oobsize);
993
994         return sndcmd;
995 }
996
997 static int mxc_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
998                               uint8_t *buf)
999 {
1000         struct nand_chip *this = mtd->priv;
1001         struct nand_info *info = this->priv;
1002
1003 #ifndef CONFIG_MXC_NFC_SP_AUTO
1004         mxc_nand_ecc_status(mtd);
1005 #endif
1006
1007         memcpy(buf, info->data_buf, mtd->writesize);
1008         memcpy(chip->oob_poi, info->oob_buf, mtd->oobsize);
1009
1010         return 0;
1011 }
1012
1013 static void mxc_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
1014                                 const uint8_t *buf)
1015 {
1016         struct nand_chip *this = mtd->priv;
1017         struct nand_info *info = this->priv;
1018
1019         memcpy(info->data_buf, buf, mtd->writesize);
1020         memcpy(info->oob_buf, chip->oob_poi, mtd->oobsize);
1021
1022 }
1023
1024 /**
1025  * mxc_nand_prog_page - [REPLACEABLE] write one page
1026  * @mtd:        MTD device structure
1027  * @chip:       NAND chip descriptor
1028  * @buf:        the data to write
1029  * @page:       page number to write
1030  * @cached:     cached programming
1031  * @raw:        use _raw version of write_page
1032  */
1033 static int mxc_nand_prog_page(struct mtd_info *mtd, struct nand_chip *chip,
1034                         const uint8_t *buf, int page, int cached, int raw)
1035 {
1036         int status;
1037         int i;
1038
1039         for (i = 0; i < mtd->writesize; i += 4) {
1040                 if (*(u32 *)(buf + i) != (u32)-1)
1041                         break;
1042         }
1043
1044         if (i == mtd->writesize)
1045                 return 0;
1046
1047                 chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
1048
1049         if (unlikely(raw))
1050                 chip->ecc.write_page_raw(mtd, chip, buf);
1051         else
1052                 chip->ecc.write_page(mtd, chip, buf);
1053
1054         /*
1055          * Cached progamming disabled for now, Not sure if its worth the
1056          * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
1057          */
1058         cached = 0;
1059
1060         if (!cached || !(chip->options & NAND_CACHEPRG)) {
1061
1062                 chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
1063                 status = chip->waitfunc(mtd, chip);
1064                 /*
1065                  * See if operation failed and additional status checks are
1066                  * available
1067                  */
1068                 if ((status & NAND_STATUS_FAIL) && (chip->errstat))
1069                         status = chip->errstat(mtd, chip, FL_WRITING, status,
1070                                                page);
1071
1072                 if (status & NAND_STATUS_FAIL)
1073                         return -EIO;
1074         } else {
1075                 chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
1076                 status = chip->waitfunc(mtd, chip);
1077         }
1078
1079
1080         #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
1081         /* Send command to read back the data */
1082         chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1083
1084         if (chip->verify_buf(mtd, buf, mtd->writesize))
1085                 return -EIO;
1086         #endif
1087         return 0;
1088 }
1089
1090 /* Define some generic bad / good block scan pattern which are used
1091  * while scanning a device for factory marked good / bad blocks. */
1092 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
1093
1094 static struct nand_bbt_descr smallpage_memorybased = {
1095         .options = NAND_BBT_SCAN2NDPAGE,
1096         .offs = 5,
1097         .len = 1,
1098         .pattern = scan_ff_pattern
1099 };
1100
1101 static struct nand_bbt_descr largepage_memorybased = {
1102         .options = 0,
1103         .offs = 0,
1104         .len = 2,
1105         .pattern = scan_ff_pattern
1106 };
1107
1108 /* Generic flash bbt decriptors
1109 */
1110 static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
1111 static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
1112
1113 static struct nand_bbt_descr bbt_main_descr = {
1114         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1115             | NAND_BBT_2BIT | NAND_BBT_VERSION,
1116         .offs = 0,
1117         .len = 4,
1118         .veroffs = 4,
1119         .maxblocks = 4,
1120         .pattern = bbt_pattern
1121 };
1122
1123 static struct nand_bbt_descr bbt_mirror_descr = {
1124         .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1125             | NAND_BBT_2BIT | NAND_BBT_VERSION,
1126         .offs = 0,
1127         .len = 4,
1128         .veroffs = 4,
1129         .maxblocks = 4,
1130         .pattern = mirror_pattern
1131 };
1132
1133 static int mxc_nand_scan_bbt(struct mtd_info *mtd)
1134 {
1135         int i;
1136         uint8_t id_bytes[NAND_DEVICE_ID_BYTE_COUNT];
1137         struct nand_chip *this = mtd->priv;
1138         struct nand_info *info = this->priv;
1139         struct nand_device_info  *dev_info;
1140
1141         info->page_mask = this->pagemask;
1142
1143         if (!IS_LARGE_PAGE_NAND)
1144                 goto skip_it;
1145
1146         /* Read ID bytes from the first NAND Flash chip. */
1147         this->select_chip(mtd, 0);
1148
1149         this->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
1150
1151         for (i = 0; i < NAND_DEVICE_ID_BYTE_COUNT; i++)
1152                 id_bytes[i] = this->read_byte(mtd);
1153
1154         /* Get information about this device, based on the ID bytes. */
1155         dev_info = nand_device_get_info(id_bytes);
1156
1157         /* Check if we understand this device. */
1158         if (!dev_info) {
1159                 printk(KERN_ERR "Unrecognized NAND Flash device.\n");
1160                 return !0;
1161         }
1162
1163         nand_device_print_info(dev_info);
1164
1165         /* Correct mtd setting */
1166         this->chipsize = dev_info->chip_size_in_bytes;
1167         mtd->size = dev_info->chip_size_in_bytes * this->numchips;
1168         mtd->writesize = dev_info->page_total_size_in_bytes & ~0x3ff;
1169         mtd->oobsize = dev_info->page_total_size_in_bytes & 0x3ff;;
1170         mtd->erasesize = dev_info->block_size_in_pages * mtd->writesize;
1171
1172         /* limit to 2G size due to Kernel
1173          * larger 4G space support,need fix
1174          * it later
1175          */
1176         if ((u32)mtd->size == 0) {
1177                 mtd->size = (u32)(1 << 31);
1178                 this->numchips = 1;
1179                 this->chipsize = mtd->size;
1180         }
1181
1182         /* Calculate the address shift from the page size */
1183         this->page_shift = ffs(mtd->writesize) - 1;
1184         /* Convert chipsize to number of pages per chip -1. */
1185         this->pagemask = (this->chipsize >> this->page_shift) - 1;
1186
1187         this->bbt_erase_shift = this->phys_erase_shift =
1188                 ffs(mtd->erasesize) - 1;
1189         this->chip_shift = ffs(this->chipsize) - 1;
1190         this->oob_poi = this->buffers->databuf + mtd->writesize;
1191
1192 skip_it:
1193         if (IS_2K_PAGE_NAND) {
1194                 NFC_SET_NFMS(1 << NFMS_NF_PG_SZ);
1195                 this->ecc.layout = &nand_hw_eccoob_2k;
1196                 info->large_page = 1;
1197         } else if (IS_4K_PAGE_NAND) {
1198                 NFC_SET_NFMS(1 << NFMS_NF_PG_SZ);
1199                 this->ecc.layout = &nand_hw_eccoob_4k;
1200                 info->large_page = 1;
1201         } else {
1202                 this->ecc.layout = &nand_hw_eccoob_512;
1203                 info->large_page = 0;
1204         }
1205
1206         /* propagate ecc.layout to mtd_info */
1207         mtd->ecclayout = this->ecc.layout;
1208
1209         /* jffs2 not write oob */
1210         /*mtd->flags &= ~MTD_OOB_WRITEABLE;*/
1211
1212         /* fix up the offset */
1213         largepage_memorybased.offs = BAD_BLK_MARKER_OOB_OFFS;
1214
1215         /* use flash based bbt */
1216         this->bbt_td = &bbt_main_descr;
1217         this->bbt_md = &bbt_mirror_descr;
1218
1219         /* update flash based bbt */
1220         this->options |= NAND_USE_FLASH_BBT;
1221
1222         if (!this->badblock_pattern) {
1223                 this->badblock_pattern = (mtd->writesize > 512) ?
1224                     &largepage_memorybased : &smallpage_memorybased;
1225         }
1226
1227         /* Build bad block table */
1228         return nand_scan_bbt(mtd, this->badblock_pattern);
1229 }
1230
1231 static void mxc_nfc_init(void)
1232 {
1233         /* Disable interrupt */
1234         raw_write((raw_read(REG_NFC_INTRRUPT) | NFC_INT_MSK), REG_NFC_INTRRUPT);
1235
1236         /* disable spare enable */
1237         raw_write(raw_read(REG_NFC_SP_EN) & ~NFC_SP_EN, REG_NFC_SP_EN);
1238
1239         /* Unlock the internal RAM Buffer */
1240         raw_write(NFC_SET_BLS(NFC_BLS_UNLCOKED), REG_NFC_BLS);
1241
1242         /* Blocks to be unlocked */
1243         UNLOCK_ADDR(0x0, 0xFFFF);
1244
1245         /* Unlock Block Command for given address range */
1246         raw_write(NFC_SET_WPC(NFC_WPC_UNLOCK), REG_NFC_WPC);
1247
1248         /* Enable hw ecc */
1249         raw_write((raw_read(REG_NFC_ECC_EN) | NFC_ECC_EN), REG_NFC_ECC_EN);
1250 }
1251
1252 static int mxc_alloc_buf(struct nand_info *info)
1253 {
1254         int err = 0;
1255
1256         info->data_buf = kmalloc(NAND_MAX_PAGESIZE, GFP_KERNEL);
1257         if (!info->data_buf) {
1258                 printk(KERN_ERR "%s: failed to allocate data_buf\n", __func__);
1259                 err = -ENOMEM;
1260                 return err;
1261         }
1262         memset(info->data_buf, 0, NAND_MAX_PAGESIZE);
1263
1264         info->oob_buf = kmalloc(NAND_MAX_OOBSIZE, GFP_KERNEL);
1265         if (!info->oob_buf) {
1266                 printk(KERN_ERR "%s: failed to allocate oob_buf\n", __func__);
1267                 err = -ENOMEM;
1268                 return err;
1269         }
1270         memset(info->oob_buf, 0, NAND_MAX_OOBSIZE);
1271
1272         return err;
1273 }
1274
1275 /*!
1276  * This function is called during the driver binding process.
1277  *
1278  * @param   pdev  the device structure used to store device specific
1279  *                information that is used by the suspend, resume and
1280  *                remove functions
1281  *
1282  * @return  The function always returns 0.
1283  */
1284 int board_nand_init(struct nand_chip *nand)
1285 {
1286         struct nand_info *info;
1287         struct nand_chip *this = nand;
1288         struct mtd_info *mtd; /* dummy for compile */
1289         int err;
1290
1291         info = kmalloc(sizeof(struct nand_info), GFP_KERNEL);
1292         if (!info) {
1293                 printk(KERN_ERR "%s: failed to allocate nand_info\n",
1294                        __func__);
1295                 err = -ENOMEM;
1296                 return err;
1297         }
1298         memset(info, 0, sizeof(struct nand_info));
1299
1300         if (mxc_alloc_buf(info)) {
1301                 err = -ENOMEM;
1302                 return err;
1303         }
1304
1305         info->num_of_intlv = 1;
1306
1307 #ifdef CONFIG_MXC_NFC_SP_AUTO
1308         info->auto_mode = 1;
1309 #endif
1310
1311         /* init the nfc */
1312         mxc_nfc_init();
1313
1314         this->priv = info;
1315         this->cmdfunc = mxc_nand_command;
1316         this->select_chip = mxc_nand_select_chip;
1317         this->read_byte = mxc_nand_read_byte;
1318         this->read_word = mxc_nand_read_word;
1319         this->write_buf = mxc_nand_write_buf;
1320         this->read_buf = mxc_nand_read_buf;
1321         this->verify_buf = mxc_nand_verify_buf;
1322         this->scan_bbt = mxc_nand_scan_bbt;
1323         this->write_page = mxc_nand_prog_page;
1324         this->ecc.read_page = mxc_nand_read_page;
1325         this->ecc.write_page = mxc_nand_write_page;
1326         this->ecc.read_oob = mxc_nand_read_oob;
1327         this->ecc.calculate = mxc_nand_calculate_ecc;
1328         this->ecc.correct = mxc_nand_correct_data;
1329         this->ecc.hwctl = mxc_nand_enable_hwecc;
1330         this->ecc.layout = &nand_hw_eccoob_512;
1331         this->ecc.mode = NAND_ECC_HW;
1332         this->ecc.bytes = 9;
1333         this->ecc.size = 512;
1334
1335 #ifdef CONFIG_NAND_FW_16BIT
1336         if (CONFIG_NAND_FW_16BIT == 1) {
1337                 this->read_byte = mxc_nand_read_byte16;
1338                 this->options |= NAND_BUSWIDTH_16;
1339                 NFC_SET_NFMS(1 << NFMS_NF_DWIDTH);
1340         } else {
1341                 NFC_SET_NFMS(0);
1342         }
1343 #else
1344         NFC_SET_NFMS(0);
1345 #endif
1346
1347         return 0;
1348 }