1 /* Integrated Flash Controller NAND Machine Driver
3 * Copyright (c) 2012 Freescale Semiconductor, Inc
5 * Authors: Dipen Dudhat <Dipen.Dudhat@freescale.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <linux/mtd/nand_ecc.h>
30 #include <asm/errno.h>
31 #include <asm/fsl_ifc.h>
33 #define FSL_IFC_V1_1_0 0x01010000
35 #define ERR_BYTE 0xFF /* Value returned for read bytes
37 #define IFC_TIMEOUT_MSECS 10 /* Maximum number of mSecs to wait for IFC
42 /* mtd information per set */
45 struct nand_chip chip;
46 struct fsl_ifc_ctrl *ctrl;
49 int bank; /* Chip select bank number */
50 unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
51 u8 __iomem *vbase; /* Chip select base virtual address */
54 /* overview of the fsl ifc controller */
56 struct nand_hw_control controller;
57 struct fsl_ifc_mtd *chips[MAX_BANKS];
61 uint8_t __iomem *addr; /* Address of assigned IFC buffer */
62 unsigned int cs_nand; /* On which chipsel NAND is connected */
63 unsigned int page; /* Last page written to / read from */
64 unsigned int read_bytes; /* Number of bytes read during command */
65 unsigned int column; /* Saved column from SEQIN */
66 unsigned int index; /* Pointer to next byte to 'read' */
67 unsigned int status; /* status read from NEESR after last op */
68 unsigned int oob; /* Non zero if operating on OOB data */
69 unsigned int eccread; /* Non zero for a full-page ECC read */
72 static struct fsl_ifc_ctrl *ifc_ctrl;
74 /* 512-byte page with 4-bit ECC, 8-bit */
75 static struct nand_ecclayout oob_512_8bit_ecc4 = {
77 .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
78 .oobfree = { {0, 5}, {6, 2} },
81 /* 512-byte page with 4-bit ECC, 16-bit */
82 static struct nand_ecclayout oob_512_16bit_ecc4 = {
84 .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
85 .oobfree = { {2, 6}, },
88 /* 2048-byte page size with 4-bit ECC */
89 static struct nand_ecclayout oob_2048_ecc4 = {
92 8, 9, 10, 11, 12, 13, 14, 15,
93 16, 17, 18, 19, 20, 21, 22, 23,
94 24, 25, 26, 27, 28, 29, 30, 31,
95 32, 33, 34, 35, 36, 37, 38, 39,
97 .oobfree = { {2, 6}, {40, 24} },
100 /* 4096-byte page size with 4-bit ECC */
101 static struct nand_ecclayout oob_4096_ecc4 = {
104 8, 9, 10, 11, 12, 13, 14, 15,
105 16, 17, 18, 19, 20, 21, 22, 23,
106 24, 25, 26, 27, 28, 29, 30, 31,
107 32, 33, 34, 35, 36, 37, 38, 39,
108 40, 41, 42, 43, 44, 45, 46, 47,
109 48, 49, 50, 51, 52, 53, 54, 55,
110 56, 57, 58, 59, 60, 61, 62, 63,
111 64, 65, 66, 67, 68, 69, 70, 71,
113 .oobfree = { {2, 6}, {72, 56} },
116 /* 4096-byte page size with 8-bit ECC -- requires 218-byte OOB */
117 static struct nand_ecclayout oob_4096_ecc8 = {
120 8, 9, 10, 11, 12, 13, 14, 15,
121 16, 17, 18, 19, 20, 21, 22, 23,
122 24, 25, 26, 27, 28, 29, 30, 31,
123 32, 33, 34, 35, 36, 37, 38, 39,
124 40, 41, 42, 43, 44, 45, 46, 47,
125 48, 49, 50, 51, 52, 53, 54, 55,
126 56, 57, 58, 59, 60, 61, 62, 63,
127 64, 65, 66, 67, 68, 69, 70, 71,
128 72, 73, 74, 75, 76, 77, 78, 79,
129 80, 81, 82, 83, 84, 85, 86, 87,
130 88, 89, 90, 91, 92, 93, 94, 95,
131 96, 97, 98, 99, 100, 101, 102, 103,
132 104, 105, 106, 107, 108, 109, 110, 111,
133 112, 113, 114, 115, 116, 117, 118, 119,
134 120, 121, 122, 123, 124, 125, 126, 127,
135 128, 129, 130, 131, 132, 133, 134, 135,
137 .oobfree = { {2, 6}, {136, 82} },
142 * Generic flash bbt descriptors
144 static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
145 static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
147 static struct nand_bbt_descr bbt_main_descr = {
148 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
149 NAND_BBT_2BIT | NAND_BBT_VERSION,
150 .offs = 2, /* 0 on 8-bit small page */
154 .pattern = bbt_pattern,
157 static struct nand_bbt_descr bbt_mirror_descr = {
158 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
159 NAND_BBT_2BIT | NAND_BBT_VERSION,
160 .offs = 2, /* 0 on 8-bit small page */
164 .pattern = mirror_pattern,
168 * Set up the IFC hardware block and page address fields, and the ifc nand
169 * structure addr field to point to the correct IFC buffer in memory
171 static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
173 struct nand_chip *chip = mtd->priv;
174 struct fsl_ifc_mtd *priv = chip->priv;
175 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
176 struct fsl_ifc *ifc = ctrl->regs;
179 ctrl->page = page_addr;
181 /* Program ROW0/COL0 */
182 out_be32(&ifc->ifc_nand.row0, page_addr);
183 out_be32(&ifc->ifc_nand.col0, (oob ? IFC_NAND_COL_MS : 0) | column);
185 buf_num = page_addr & priv->bufnum_mask;
187 ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
188 ctrl->index = column;
190 /* for OOB data point to the second half of the buffer */
192 ctrl->index += mtd->writesize;
195 static int is_blank(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
198 struct nand_chip *chip = mtd->priv;
199 struct fsl_ifc_mtd *priv = chip->priv;
200 u8 __iomem *addr = priv->vbase + bufnum * (mtd->writesize * 2);
201 u32 __iomem *main = (u32 *)addr;
202 u8 __iomem *oob = addr + mtd->writesize;
205 for (i = 0; i < mtd->writesize / 4; i++) {
206 if (__raw_readl(&main[i]) != 0xffffffff)
210 for (i = 0; i < chip->ecc.layout->eccbytes; i++) {
211 int pos = chip->ecc.layout->eccpos[i];
213 if (__raw_readb(&oob[pos]) != 0xff)
220 /* returns nonzero if entire page is blank */
221 static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
222 u32 *eccstat, unsigned int bufnum)
224 u32 reg = eccstat[bufnum / 4];
227 errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
233 * execute IFC NAND command and wait for it to complete
235 static int fsl_ifc_run_command(struct mtd_info *mtd)
237 struct nand_chip *chip = mtd->priv;
238 struct fsl_ifc_mtd *priv = chip->priv;
239 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
240 struct fsl_ifc *ifc = ctrl->regs;
245 /* set the chip select for NAND Transaction */
246 out_be32(&ifc->ifc_nand.nand_csel, ifc_ctrl->cs_nand);
248 /* start read/write seq */
249 out_be32(&ifc->ifc_nand.nandseq_strt,
250 IFC_NAND_SEQ_STRT_FIR_STRT);
252 /* wait for NAND Machine complete flag or timeout */
253 end_tick = usec2ticks(IFC_TIMEOUT_MSECS * 1000) + get_ticks();
255 while (end_tick > get_ticks()) {
256 ctrl->status = in_be32(&ifc->ifc_nand.nand_evter_stat);
258 if (ctrl->status & IFC_NAND_EVTER_STAT_OPC)
262 out_be32(&ifc->ifc_nand.nand_evter_stat, ctrl->status);
264 if (ctrl->status & IFC_NAND_EVTER_STAT_FTOER)
265 printf("%s: Flash Time Out Error\n", __func__);
266 if (ctrl->status & IFC_NAND_EVTER_STAT_WPER)
267 printf("%s: Write Protect Error\n", __func__);
271 int bufnum = ctrl->page & priv->bufnum_mask;
272 int sector = bufnum * chip->ecc.steps;
273 int sector_end = sector + chip->ecc.steps - 1;
275 for (i = sector / 4; i <= sector_end / 4; i++)
276 eccstat[i] = in_be32(&ifc->ifc_nand.nand_eccstat[i]);
278 for (i = sector; i <= sector_end; i++) {
279 errors = check_read_ecc(mtd, ctrl, eccstat, i);
283 * Uncorrectable error.
284 * OK only if the whole page is blank.
286 * We disable ECCER reporting due to erratum
287 * IFC-A002770 -- so report it now if we
288 * see an uncorrectable error in ECCSTAT.
290 if (!is_blank(mtd, ctrl, bufnum))
292 IFC_NAND_EVTER_STAT_ECCER;
296 mtd->ecc_stats.corrected += errors;
302 /* returns 0 on success otherwise non-zero) */
303 return ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
306 static void fsl_ifc_do_read(struct nand_chip *chip,
308 struct mtd_info *mtd)
310 struct fsl_ifc_mtd *priv = chip->priv;
311 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
312 struct fsl_ifc *ifc = ctrl->regs;
314 /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
315 if (mtd->writesize > 512) {
316 out_be32(&ifc->ifc_nand.nand_fir0,
317 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
318 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
319 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
320 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
321 (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT));
322 out_be32(&ifc->ifc_nand.nand_fir1, 0x0);
324 out_be32(&ifc->ifc_nand.nand_fcr0,
325 (NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
326 (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT));
328 out_be32(&ifc->ifc_nand.nand_fir0,
329 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
330 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
331 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
332 (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT));
335 out_be32(&ifc->ifc_nand.nand_fcr0,
336 NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT);
338 out_be32(&ifc->ifc_nand.nand_fcr0,
339 NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT);
343 /* cmdfunc send commands to the IFC NAND Machine */
344 static void fsl_ifc_cmdfunc(struct mtd_info *mtd, unsigned int command,
345 int column, int page_addr)
347 struct nand_chip *chip = mtd->priv;
348 struct fsl_ifc_mtd *priv = chip->priv;
349 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
350 struct fsl_ifc *ifc = ctrl->regs;
352 /* clear the read buffer */
353 ctrl->read_bytes = 0;
354 if (command != NAND_CMD_PAGEPROG)
358 /* READ0 read the entire buffer to use hardware ECC. */
359 case NAND_CMD_READ0: {
360 out_be32(&ifc->ifc_nand.nand_fbcr, 0);
361 set_addr(mtd, 0, page_addr, 0);
363 ctrl->read_bytes = mtd->writesize + mtd->oobsize;
364 ctrl->index += column;
366 if (chip->ecc.mode == NAND_ECC_HW)
369 fsl_ifc_do_read(chip, 0, mtd);
370 fsl_ifc_run_command(mtd);
374 /* READOOB reads only the OOB because no ECC is performed. */
375 case NAND_CMD_READOOB:
376 out_be32(&ifc->ifc_nand.nand_fbcr, mtd->oobsize - column);
377 set_addr(mtd, column, page_addr, 1);
379 ctrl->read_bytes = mtd->writesize + mtd->oobsize;
381 fsl_ifc_do_read(chip, 1, mtd);
382 fsl_ifc_run_command(mtd);
386 /* READID must read all possible bytes while CEB is active */
387 case NAND_CMD_READID:
388 case NAND_CMD_PARAM: {
389 int timing = IFC_FIR_OP_RB;
390 if (command == NAND_CMD_PARAM)
391 timing = IFC_FIR_OP_RBCD;
393 out_be32(&ifc->ifc_nand.nand_fir0,
394 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
395 (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
396 (timing << IFC_NAND_FIR0_OP2_SHIFT));
397 out_be32(&ifc->ifc_nand.nand_fcr0,
398 command << IFC_NAND_FCR0_CMD0_SHIFT);
399 out_be32(&ifc->ifc_nand.row3, column);
402 * although currently it's 8 bytes for READID, we always read
403 * the maximum 256 bytes(for PARAM)
405 out_be32(&ifc->ifc_nand.nand_fbcr, 256);
406 ctrl->read_bytes = 256;
408 set_addr(mtd, 0, 0, 0);
409 fsl_ifc_run_command(mtd);
413 /* ERASE1 stores the block and page address */
414 case NAND_CMD_ERASE1:
415 set_addr(mtd, 0, page_addr, 0);
418 /* ERASE2 uses the block and page address from ERASE1 */
419 case NAND_CMD_ERASE2:
420 out_be32(&ifc->ifc_nand.nand_fir0,
421 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
422 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
423 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT));
425 out_be32(&ifc->ifc_nand.nand_fcr0,
426 (NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
427 (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT));
429 out_be32(&ifc->ifc_nand.nand_fbcr, 0);
430 ctrl->read_bytes = 0;
431 fsl_ifc_run_command(mtd);
434 /* SEQIN sets up the addr buffer and all registers except the length */
435 case NAND_CMD_SEQIN: {
437 ctrl->column = column;
440 if (mtd->writesize > 512) {
442 (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
443 (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD1_SHIFT);
445 out_be32(&ifc->ifc_nand.nand_fir0,
446 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
447 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
448 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
449 (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
450 (IFC_FIR_OP_CW1 << IFC_NAND_FIR0_OP4_SHIFT));
451 out_be32(&ifc->ifc_nand.nand_fir1, 0);
453 nand_fcr0 = ((NAND_CMD_PAGEPROG <<
454 IFC_NAND_FCR0_CMD1_SHIFT) |
456 IFC_NAND_FCR0_CMD2_SHIFT));
458 out_be32(&ifc->ifc_nand.nand_fir0,
459 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
460 (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
461 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
462 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
463 (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT));
464 out_be32(&ifc->ifc_nand.nand_fir1,
465 (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT));
467 if (column >= mtd->writesize)
469 NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
472 NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
475 if (column >= mtd->writesize) {
476 /* OOB area --> READOOB */
477 column -= mtd->writesize;
480 out_be32(&ifc->ifc_nand.nand_fcr0, nand_fcr0);
481 set_addr(mtd, column, page_addr, ctrl->oob);
485 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
486 case NAND_CMD_PAGEPROG:
488 out_be32(&ifc->ifc_nand.nand_fbcr,
489 ctrl->index - ctrl->column);
491 out_be32(&ifc->ifc_nand.nand_fbcr, 0);
493 fsl_ifc_run_command(mtd);
496 case NAND_CMD_STATUS:
497 out_be32(&ifc->ifc_nand.nand_fir0,
498 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
499 (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT));
500 out_be32(&ifc->ifc_nand.nand_fcr0,
501 NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT);
502 out_be32(&ifc->ifc_nand.nand_fbcr, 1);
503 set_addr(mtd, 0, 0, 0);
504 ctrl->read_bytes = 1;
506 fsl_ifc_run_command(mtd);
508 /* Chip sometimes reporting write protect even when it's not */
509 out_8(ctrl->addr, in_8(ctrl->addr) | NAND_STATUS_WP);
513 out_be32(&ifc->ifc_nand.nand_fir0,
514 IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT);
515 out_be32(&ifc->ifc_nand.nand_fcr0,
516 NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT);
517 fsl_ifc_run_command(mtd);
521 printf("%s: error, unsupported command 0x%x.\n",
527 * Write buf to the IFC NAND Controller Data Buffer
529 static void fsl_ifc_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
531 struct nand_chip *chip = mtd->priv;
532 struct fsl_ifc_mtd *priv = chip->priv;
533 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
534 unsigned int bufsize = mtd->writesize + mtd->oobsize;
537 printf("%s of %d bytes", __func__, len);
542 if ((unsigned int)len > bufsize - ctrl->index) {
543 printf("%s beyond end of buffer "
544 "(%d requested, %u available)\n",
545 __func__, len, bufsize - ctrl->index);
546 len = bufsize - ctrl->index;
549 memcpy_toio(&ctrl->addr[ctrl->index], buf, len);
554 * read a byte from either the IFC hardware buffer if it has any data left
555 * otherwise issue a command to read a single byte.
557 static u8 fsl_ifc_read_byte(struct mtd_info *mtd)
559 struct nand_chip *chip = mtd->priv;
560 struct fsl_ifc_mtd *priv = chip->priv;
561 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
563 /* If there are still bytes in the IFC buffer, then use the
565 if (ctrl->index < ctrl->read_bytes)
566 return in_8(&ctrl->addr[ctrl->index++]);
568 printf("%s beyond end of buffer\n", __func__);
573 * Read two bytes from the IFC hardware buffer
574 * read function for 16-bit buswith
576 static uint8_t fsl_ifc_read_byte16(struct mtd_info *mtd)
578 struct nand_chip *chip = mtd->priv;
579 struct fsl_ifc_mtd *priv = chip->priv;
580 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
584 * If there are still bytes in the IFC buffer, then use the
587 if (ctrl->index < ctrl->read_bytes) {
588 data = in_be16((uint16_t *)&ctrl->
591 return (uint8_t)data;
594 printf("%s beyond end of buffer\n", __func__);
599 * Read from the IFC Controller Data Buffer
601 static void fsl_ifc_read_buf(struct mtd_info *mtd, u8 *buf, int len)
603 struct nand_chip *chip = mtd->priv;
604 struct fsl_ifc_mtd *priv = chip->priv;
605 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
611 avail = min((unsigned int)len, ctrl->read_bytes - ctrl->index);
612 memcpy_fromio(buf, &ctrl->addr[ctrl->index], avail);
613 ctrl->index += avail;
616 printf("%s beyond end of buffer "
617 "(%d requested, %d available)\n",
618 __func__, len, avail);
622 * Verify buffer against the IFC Controller Data Buffer
624 static int fsl_ifc_verify_buf(struct mtd_info *mtd,
625 const u_char *buf, int len)
627 struct nand_chip *chip = mtd->priv;
628 struct fsl_ifc_mtd *priv = chip->priv;
629 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
633 printf("%s of %d bytes", __func__, len);
637 if ((unsigned int)len > ctrl->read_bytes - ctrl->index) {
638 printf("%s beyond end of buffer "
639 "(%d requested, %u available)\n",
640 __func__, len, ctrl->read_bytes - ctrl->index);
642 ctrl->index = ctrl->read_bytes;
646 for (i = 0; i < len; i++)
647 if (in_8(&ctrl->addr[ctrl->index + i]) != buf[i])
651 return i == len && ctrl->status == IFC_NAND_EVTER_STAT_OPC ? 0 : -EIO;
654 /* This function is called after Program and Erase Operations to
655 * check for success or failure.
657 static int fsl_ifc_wait(struct mtd_info *mtd, struct nand_chip *chip)
659 struct fsl_ifc_mtd *priv = chip->priv;
660 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
661 struct fsl_ifc *ifc = ctrl->regs;
664 if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
665 return NAND_STATUS_FAIL;
667 /* Use READ_STATUS command, but wait for the device to be ready */
668 out_be32(&ifc->ifc_nand.nand_fir0,
669 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
670 (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT));
671 out_be32(&ifc->ifc_nand.nand_fcr0, NAND_CMD_STATUS <<
672 IFC_NAND_FCR0_CMD0_SHIFT);
673 out_be32(&ifc->ifc_nand.nand_fbcr, 1);
674 set_addr(mtd, 0, 0, 0);
675 ctrl->read_bytes = 1;
677 fsl_ifc_run_command(mtd);
679 if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
680 return NAND_STATUS_FAIL;
682 nand_fsr = in_be32(&ifc->ifc_nand.nand_fsr);
684 /* Chip sometimes reporting write protect even when it's not */
685 nand_fsr = nand_fsr | NAND_STATUS_WP;
689 static int fsl_ifc_read_page(struct mtd_info *mtd,
690 struct nand_chip *chip,
691 uint8_t *buf, int page)
693 struct fsl_ifc_mtd *priv = chip->priv;
694 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
696 fsl_ifc_read_buf(mtd, buf, mtd->writesize);
697 fsl_ifc_read_buf(mtd, chip->oob_poi, mtd->oobsize);
699 if (ctrl->status != IFC_NAND_EVTER_STAT_OPC)
700 mtd->ecc_stats.failed++;
705 /* ECC will be calculated automatically, and errors will be detected in
708 static void fsl_ifc_write_page(struct mtd_info *mtd,
709 struct nand_chip *chip,
712 fsl_ifc_write_buf(mtd, buf, mtd->writesize);
713 fsl_ifc_write_buf(mtd, chip->oob_poi, mtd->oobsize);
716 static void fsl_ifc_ctrl_init(void)
718 ifc_ctrl = kzalloc(sizeof(*ifc_ctrl), GFP_KERNEL);
722 ifc_ctrl->regs = IFC_BASE_ADDR;
724 /* clear event registers */
725 out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_stat, ~0U);
726 out_be32(&ifc_ctrl->regs->ifc_nand.pgrdcmpl_evt_stat, ~0U);
728 /* Enable error and event for any detected errors */
729 out_be32(&ifc_ctrl->regs->ifc_nand.nand_evter_en,
730 IFC_NAND_EVTER_EN_OPC_EN |
731 IFC_NAND_EVTER_EN_PGRDCMPL_EN |
732 IFC_NAND_EVTER_EN_FTOER_EN |
733 IFC_NAND_EVTER_EN_WPER_EN);
735 out_be32(&ifc_ctrl->regs->ifc_nand.ncfgr, 0x0);
738 static void fsl_ifc_select_chip(struct mtd_info *mtd, int chip)
742 static void fsl_ifc_sram_init(void)
744 struct fsl_ifc *ifc = ifc_ctrl->regs;
745 uint32_t cs = 0, csor = 0, csor_8k = 0, csor_ext = 0;
748 cs = ifc_ctrl->cs_nand >> IFC_NAND_CSEL_SHIFT;
750 /* Save CSOR and CSOR_ext */
751 csor = in_be32(&ifc_ctrl->regs->csor_cs[cs].csor);
752 csor_ext = in_be32(&ifc_ctrl->regs->csor_cs[cs].csor_ext);
754 /* chage PageSize 8K and SpareSize 1K*/
755 csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
756 out_be32(&ifc_ctrl->regs->csor_cs[cs].csor, csor_8k);
757 out_be32(&ifc_ctrl->regs->csor_cs[cs].csor_ext, 0x0000400);
760 out_be32(&ifc->ifc_nand.nand_fir0,
761 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
762 (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
763 (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT));
764 out_be32(&ifc->ifc_nand.nand_fcr0,
765 NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT);
766 out_be32(&ifc->ifc_nand.row3, 0x0);
768 out_be32(&ifc->ifc_nand.nand_fbcr, 0x0);
770 /* Program ROW0/COL0 */
771 out_be32(&ifc->ifc_nand.row0, 0x0);
772 out_be32(&ifc->ifc_nand.col0, 0x0);
774 /* set the chip select for NAND Transaction */
775 out_be32(&ifc->ifc_nand.nand_csel, ifc_ctrl->cs_nand);
778 out_be32(&ifc->ifc_nand.nandseq_strt, IFC_NAND_SEQ_STRT_FIR_STRT);
780 /* wait for NAND Machine complete flag or timeout */
781 end_tick = usec2ticks(IFC_TIMEOUT_MSECS * 1000) + get_ticks();
783 while (end_tick > get_ticks()) {
784 ifc_ctrl->status = in_be32(&ifc->ifc_nand.nand_evter_stat);
786 if (ifc_ctrl->status & IFC_NAND_EVTER_STAT_OPC)
790 out_be32(&ifc->ifc_nand.nand_evter_stat, ifc_ctrl->status);
792 /* Restore CSOR and CSOR_ext */
793 out_be32(&ifc_ctrl->regs->csor_cs[cs].csor, csor);
794 out_be32(&ifc_ctrl->regs->csor_cs[cs].csor_ext, csor_ext);
797 int board_nand_init(struct nand_chip *nand)
799 struct fsl_ifc_mtd *priv;
800 struct nand_ecclayout *layout;
801 uint32_t cspr = 0, csor = 0, ver = 0;
809 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
813 priv->ctrl = ifc_ctrl;
814 priv->vbase = nand->IO_ADDR_R;
816 /* Find which chip select it is connected to.
818 for (priv->bank = 0; priv->bank < MAX_BANKS; priv->bank++) {
819 phys_addr_t base_addr = virt_to_phys(nand->IO_ADDR_R);
821 cspr = in_be32(&ifc_ctrl->regs->cspr_cs[priv->bank].cspr);
822 csor = in_be32(&ifc_ctrl->regs->csor_cs[priv->bank].csor);
824 if ((cspr & CSPR_V) && (cspr & CSPR_MSEL) == CSPR_MSEL_NAND &&
825 (cspr & CSPR_BA) == CSPR_PHYS_ADDR(base_addr)) {
826 ifc_ctrl->cs_nand = priv->bank << IFC_NAND_CSEL_SHIFT;
831 if (priv->bank >= MAX_BANKS) {
832 printf("%s: address did not match any "
833 "chip selects\n", __func__);
838 ifc_ctrl->chips[priv->bank] = priv;
840 /* fill in nand_chip structure */
841 /* set up function call table */
843 nand->write_buf = fsl_ifc_write_buf;
844 nand->read_buf = fsl_ifc_read_buf;
845 nand->verify_buf = fsl_ifc_verify_buf;
846 nand->select_chip = fsl_ifc_select_chip;
847 nand->cmdfunc = fsl_ifc_cmdfunc;
848 nand->waitfunc = fsl_ifc_wait;
850 /* set up nand options */
851 nand->bbt_td = &bbt_main_descr;
852 nand->bbt_md = &bbt_mirror_descr;
854 /* set up nand options */
855 nand->options = NAND_NO_READRDY | NAND_NO_AUTOINCR |
856 NAND_USE_FLASH_BBT | NAND_NO_SUBPAGE_WRITE;
858 if (cspr & CSPR_PORT_SIZE_16) {
859 nand->read_byte = fsl_ifc_read_byte16;
860 nand->options |= NAND_BUSWIDTH_16;
862 nand->read_byte = fsl_ifc_read_byte;
865 nand->controller = &ifc_ctrl->controller;
868 nand->ecc.read_page = fsl_ifc_read_page;
869 nand->ecc.write_page = fsl_ifc_write_page;
871 /* Hardware generates ECC per 512 Bytes */
872 nand->ecc.size = 512;
875 switch (csor & CSOR_NAND_PGS_MASK) {
876 case CSOR_NAND_PGS_512:
877 if (nand->options & NAND_BUSWIDTH_16) {
878 layout = &oob_512_16bit_ecc4;
880 layout = &oob_512_8bit_ecc4;
882 /* Avoid conflict with bad block marker */
883 bbt_main_descr.offs = 0;
884 bbt_mirror_descr.offs = 0;
887 priv->bufnum_mask = 15;
890 case CSOR_NAND_PGS_2K:
891 layout = &oob_2048_ecc4;
892 priv->bufnum_mask = 3;
895 case CSOR_NAND_PGS_4K:
896 if ((csor & CSOR_NAND_ECC_MODE_MASK) ==
897 CSOR_NAND_ECC_MODE_4) {
898 layout = &oob_4096_ecc4;
900 layout = &oob_4096_ecc8;
901 nand->ecc.bytes = 16;
904 priv->bufnum_mask = 1;
908 printf("ifc nand: bad csor %#x: bad page size\n", csor);
912 /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
913 if (csor & CSOR_NAND_ECC_DEC_EN) {
914 nand->ecc.mode = NAND_ECC_HW;
915 nand->ecc.layout = layout;
917 nand->ecc.mode = NAND_ECC_SOFT;
920 ver = in_be32(&ifc_ctrl->regs->ifc_rev);
921 if (ver == FSL_IFC_V1_1_0)