1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale Integrated Flash Controller NAND driver
5 * Copyright 2011-2012 Freescale Semiconductor, Inc
7 * Author: Dipen Dudhat <Dipen.Dudhat@freescale.com>
10 #include <linux/module.h>
11 #include <linux/types.h>
12 #include <linux/kernel.h>
13 #include <linux/of_address.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/rawnand.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/mtd/nand_ecc.h>
19 #include <linux/fsl_ifc.h>
20 #include <linux/iopoll.h>
22 #define ERR_BYTE 0xFF /* Value returned for read
23 bytes when read failed */
24 #define IFC_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait
25 for IFC NAND Machine */
29 /* mtd information per set */
31 struct nand_chip chip;
32 struct fsl_ifc_ctrl *ctrl;
35 int bank; /* Chip select bank number */
36 unsigned int bufnum_mask; /* bufnum = page & bufnum_mask */
37 u8 __iomem *vbase; /* Chip select base virtual address */
40 /* overview of the fsl ifc controller */
41 struct fsl_ifc_nand_ctrl {
42 struct nand_controller controller;
43 struct fsl_ifc_mtd *chips[FSL_IFC_BANK_COUNT];
45 void __iomem *addr; /* Address of assigned IFC buffer */
46 unsigned int page; /* Last page written to / read from */
47 unsigned int read_bytes;/* Number of bytes read during command */
48 unsigned int column; /* Saved column from SEQIN */
49 unsigned int index; /* Pointer to next byte to 'read' */
50 unsigned int oob; /* Non zero if operating on OOB data */
51 unsigned int eccread; /* Non zero for a full-page ECC read */
52 unsigned int counter; /* counter for the initializations */
53 unsigned int max_bitflips; /* Saved during READ0 cmd */
56 static struct fsl_ifc_nand_ctrl *ifc_nand_ctrl;
59 * Generic flash bbt descriptors
61 static u8 bbt_pattern[] = {'B', 'b', 't', '0' };
62 static u8 mirror_pattern[] = {'1', 't', 'b', 'B' };
64 static struct nand_bbt_descr bbt_main_descr = {
65 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
66 NAND_BBT_2BIT | NAND_BBT_VERSION,
67 .offs = 2, /* 0 on 8-bit small page */
71 .pattern = bbt_pattern,
74 static struct nand_bbt_descr bbt_mirror_descr = {
75 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE |
76 NAND_BBT_2BIT | NAND_BBT_VERSION,
77 .offs = 2, /* 0 on 8-bit small page */
81 .pattern = mirror_pattern,
84 static int fsl_ifc_ooblayout_ecc(struct mtd_info *mtd, int section,
85 struct mtd_oob_region *oobregion)
87 struct nand_chip *chip = mtd_to_nand(mtd);
92 oobregion->offset = 8;
93 oobregion->length = chip->ecc.total;
98 static int fsl_ifc_ooblayout_free(struct mtd_info *mtd, int section,
99 struct mtd_oob_region *oobregion)
101 struct nand_chip *chip = mtd_to_nand(mtd);
106 if (mtd->writesize == 512 &&
107 !(chip->options & NAND_BUSWIDTH_16)) {
109 oobregion->offset = 0;
110 oobregion->length = 5;
112 oobregion->offset = 6;
113 oobregion->length = 2;
120 oobregion->offset = 2;
121 oobregion->length = 6;
123 oobregion->offset = chip->ecc.total + 8;
124 oobregion->length = mtd->oobsize - oobregion->offset;
130 static const struct mtd_ooblayout_ops fsl_ifc_ooblayout_ops = {
131 .ecc = fsl_ifc_ooblayout_ecc,
132 .free = fsl_ifc_ooblayout_free,
136 * Set up the IFC hardware block and page address fields, and the ifc nand
137 * structure addr field to point to the correct IFC buffer in memory
139 static void set_addr(struct mtd_info *mtd, int column, int page_addr, int oob)
141 struct nand_chip *chip = mtd_to_nand(mtd);
142 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
143 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
144 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
147 ifc_nand_ctrl->page = page_addr;
148 /* Program ROW0/COL0 */
149 ifc_out32(page_addr, &ifc->ifc_nand.row0);
150 ifc_out32((oob ? IFC_NAND_COL_MS : 0) | column, &ifc->ifc_nand.col0);
152 buf_num = page_addr & priv->bufnum_mask;
154 ifc_nand_ctrl->addr = priv->vbase + buf_num * (mtd->writesize * 2);
155 ifc_nand_ctrl->index = column;
157 /* for OOB data point to the second half of the buffer */
159 ifc_nand_ctrl->index += mtd->writesize;
162 /* returns nonzero if entire page is blank */
163 static int check_read_ecc(struct mtd_info *mtd, struct fsl_ifc_ctrl *ctrl,
164 u32 eccstat, unsigned int bufnum)
166 return (eccstat >> ((3 - bufnum % 4) * 8)) & 15;
170 * execute IFC NAND command and wait for it to complete
172 static void fsl_ifc_run_command(struct mtd_info *mtd)
174 struct nand_chip *chip = mtd_to_nand(mtd);
175 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
176 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
177 struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
178 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
182 /* set the chip select for NAND Transaction */
183 ifc_out32(priv->bank << IFC_NAND_CSEL_SHIFT,
184 &ifc->ifc_nand.nand_csel);
187 "%s: fir0=%08x fcr0=%08x\n",
189 ifc_in32(&ifc->ifc_nand.nand_fir0),
190 ifc_in32(&ifc->ifc_nand.nand_fcr0));
194 /* start read/write seq */
195 ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT, &ifc->ifc_nand.nandseq_strt);
197 /* wait for command complete flag or timeout */
198 wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
199 msecs_to_jiffies(IFC_TIMEOUT_MSECS));
201 /* ctrl->nand_stat will be updated from IRQ context */
202 if (!ctrl->nand_stat)
203 dev_err(priv->dev, "Controller is not responding\n");
204 if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_FTOER)
205 dev_err(priv->dev, "NAND Flash Timeout Error\n");
206 if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_WPER)
207 dev_err(priv->dev, "NAND Flash Write Protect Error\n");
209 nctrl->max_bitflips = 0;
211 if (nctrl->eccread) {
213 int bufnum = nctrl->page & priv->bufnum_mask;
214 int sector_start = bufnum * chip->ecc.steps;
215 int sector_end = sector_start + chip->ecc.steps - 1;
216 __be32 __iomem *eccstat_regs;
218 eccstat_regs = ifc->ifc_nand.nand_eccstat;
219 eccstat = ifc_in32(&eccstat_regs[sector_start / 4]);
221 for (i = sector_start; i <= sector_end; i++) {
222 if (i != sector_start && !(i % 4))
223 eccstat = ifc_in32(&eccstat_regs[i / 4]);
225 errors = check_read_ecc(mtd, ctrl, eccstat, i);
229 * Uncorrectable error.
230 * We'll check for blank pages later.
232 * We disable ECCER reporting due to...
233 * erratum IFC-A002770 -- so report it now if we
234 * see an uncorrectable error in ECCSTAT.
236 ctrl->nand_stat |= IFC_NAND_EVTER_STAT_ECCER;
240 mtd->ecc_stats.corrected += errors;
241 nctrl->max_bitflips = max_t(unsigned int,
250 static void fsl_ifc_do_read(struct nand_chip *chip,
252 struct mtd_info *mtd)
254 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
255 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
256 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
258 /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
259 if (mtd->writesize > 512) {
260 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
261 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
262 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
263 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP3_SHIFT) |
264 (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP4_SHIFT),
265 &ifc->ifc_nand.nand_fir0);
266 ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
268 ifc_out32((NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT) |
269 (NAND_CMD_READSTART << IFC_NAND_FCR0_CMD1_SHIFT),
270 &ifc->ifc_nand.nand_fcr0);
272 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
273 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
274 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
275 (IFC_FIR_OP_RBCD << IFC_NAND_FIR0_OP3_SHIFT),
276 &ifc->ifc_nand.nand_fir0);
277 ifc_out32(0x0, &ifc->ifc_nand.nand_fir1);
280 ifc_out32(NAND_CMD_READOOB <<
281 IFC_NAND_FCR0_CMD0_SHIFT,
282 &ifc->ifc_nand.nand_fcr0);
284 ifc_out32(NAND_CMD_READ0 <<
285 IFC_NAND_FCR0_CMD0_SHIFT,
286 &ifc->ifc_nand.nand_fcr0);
290 /* cmdfunc send commands to the IFC NAND Machine */
291 static void fsl_ifc_cmdfunc(struct nand_chip *chip, unsigned int command,
292 int column, int page_addr) {
293 struct mtd_info *mtd = nand_to_mtd(chip);
294 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
295 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
296 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
298 /* clear the read buffer */
299 ifc_nand_ctrl->read_bytes = 0;
300 if (command != NAND_CMD_PAGEPROG)
301 ifc_nand_ctrl->index = 0;
304 /* READ0 read the entire buffer to use hardware ECC. */
306 ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
307 set_addr(mtd, 0, page_addr, 0);
309 ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
310 ifc_nand_ctrl->index += column;
312 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_ON_HOST)
313 ifc_nand_ctrl->eccread = 1;
315 fsl_ifc_do_read(chip, 0, mtd);
316 fsl_ifc_run_command(mtd);
319 /* READOOB reads only the OOB because no ECC is performed. */
320 case NAND_CMD_READOOB:
321 ifc_out32(mtd->oobsize - column, &ifc->ifc_nand.nand_fbcr);
322 set_addr(mtd, column, page_addr, 1);
324 ifc_nand_ctrl->read_bytes = mtd->writesize + mtd->oobsize;
326 fsl_ifc_do_read(chip, 1, mtd);
327 fsl_ifc_run_command(mtd);
331 case NAND_CMD_READID:
332 case NAND_CMD_PARAM: {
334 * For READID, read 8 bytes that are currently used.
335 * For PARAM, read all 3 copies of 256-bytes pages.
338 int timing = IFC_FIR_OP_RB;
339 if (command == NAND_CMD_PARAM) {
340 timing = IFC_FIR_OP_RBCD;
344 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
345 (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
346 (timing << IFC_NAND_FIR0_OP2_SHIFT),
347 &ifc->ifc_nand.nand_fir0);
348 ifc_out32(command << IFC_NAND_FCR0_CMD0_SHIFT,
349 &ifc->ifc_nand.nand_fcr0);
350 ifc_out32(column, &ifc->ifc_nand.row3);
352 ifc_out32(len, &ifc->ifc_nand.nand_fbcr);
353 ifc_nand_ctrl->read_bytes = len;
355 set_addr(mtd, 0, 0, 0);
356 fsl_ifc_run_command(mtd);
360 /* ERASE1 stores the block and page address */
361 case NAND_CMD_ERASE1:
362 set_addr(mtd, 0, page_addr, 0);
365 /* ERASE2 uses the block and page address from ERASE1 */
366 case NAND_CMD_ERASE2:
367 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
368 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP1_SHIFT) |
369 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR0_OP2_SHIFT),
370 &ifc->ifc_nand.nand_fir0);
372 ifc_out32((NAND_CMD_ERASE1 << IFC_NAND_FCR0_CMD0_SHIFT) |
373 (NAND_CMD_ERASE2 << IFC_NAND_FCR0_CMD1_SHIFT),
374 &ifc->ifc_nand.nand_fcr0);
376 ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
377 ifc_nand_ctrl->read_bytes = 0;
378 fsl_ifc_run_command(mtd);
381 /* SEQIN sets up the addr buffer and all registers except the length */
382 case NAND_CMD_SEQIN: {
384 ifc_nand_ctrl->column = column;
385 ifc_nand_ctrl->oob = 0;
387 if (mtd->writesize > 512) {
389 (NAND_CMD_SEQIN << IFC_NAND_FCR0_CMD0_SHIFT) |
390 (NAND_CMD_STATUS << IFC_NAND_FCR0_CMD1_SHIFT) |
391 (NAND_CMD_PAGEPROG << IFC_NAND_FCR0_CMD2_SHIFT);
394 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
395 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP1_SHIFT) |
396 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP2_SHIFT) |
397 (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP3_SHIFT) |
398 (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP4_SHIFT),
399 &ifc->ifc_nand.nand_fir0);
401 (IFC_FIR_OP_CW1 << IFC_NAND_FIR1_OP5_SHIFT) |
402 (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP6_SHIFT) |
403 (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP7_SHIFT),
404 &ifc->ifc_nand.nand_fir1);
406 nand_fcr0 = ((NAND_CMD_PAGEPROG <<
407 IFC_NAND_FCR0_CMD1_SHIFT) |
409 IFC_NAND_FCR0_CMD2_SHIFT) |
411 IFC_NAND_FCR0_CMD3_SHIFT));
414 (IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
415 (IFC_FIR_OP_CMD2 << IFC_NAND_FIR0_OP1_SHIFT) |
416 (IFC_FIR_OP_CA0 << IFC_NAND_FIR0_OP2_SHIFT) |
417 (IFC_FIR_OP_RA0 << IFC_NAND_FIR0_OP3_SHIFT) |
418 (IFC_FIR_OP_WBCD << IFC_NAND_FIR0_OP4_SHIFT),
419 &ifc->ifc_nand.nand_fir0);
421 (IFC_FIR_OP_CMD1 << IFC_NAND_FIR1_OP5_SHIFT) |
422 (IFC_FIR_OP_CW3 << IFC_NAND_FIR1_OP6_SHIFT) |
423 (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR1_OP7_SHIFT) |
424 (IFC_FIR_OP_NOP << IFC_NAND_FIR1_OP8_SHIFT),
425 &ifc->ifc_nand.nand_fir1);
427 if (column >= mtd->writesize)
429 NAND_CMD_READOOB << IFC_NAND_FCR0_CMD0_SHIFT;
432 NAND_CMD_READ0 << IFC_NAND_FCR0_CMD0_SHIFT;
435 if (column >= mtd->writesize) {
436 /* OOB area --> READOOB */
437 column -= mtd->writesize;
438 ifc_nand_ctrl->oob = 1;
440 ifc_out32(nand_fcr0, &ifc->ifc_nand.nand_fcr0);
441 set_addr(mtd, column, page_addr, ifc_nand_ctrl->oob);
445 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
446 case NAND_CMD_PAGEPROG: {
447 if (ifc_nand_ctrl->oob) {
448 ifc_out32(ifc_nand_ctrl->index -
449 ifc_nand_ctrl->column,
450 &ifc->ifc_nand.nand_fbcr);
452 ifc_out32(0, &ifc->ifc_nand.nand_fbcr);
455 fsl_ifc_run_command(mtd);
459 case NAND_CMD_STATUS: {
462 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
463 (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP1_SHIFT),
464 &ifc->ifc_nand.nand_fir0);
465 ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
466 &ifc->ifc_nand.nand_fcr0);
467 ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
468 set_addr(mtd, 0, 0, 0);
469 ifc_nand_ctrl->read_bytes = 1;
471 fsl_ifc_run_command(mtd);
474 * The chip always seems to report that it is
475 * write-protected, even when it is not.
477 addr = ifc_nand_ctrl->addr;
478 if (chip->options & NAND_BUSWIDTH_16)
479 ifc_out16(ifc_in16(addr) | (NAND_STATUS_WP), addr);
481 ifc_out8(ifc_in8(addr) | (NAND_STATUS_WP), addr);
486 ifc_out32(IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT,
487 &ifc->ifc_nand.nand_fir0);
488 ifc_out32(NAND_CMD_RESET << IFC_NAND_FCR0_CMD0_SHIFT,
489 &ifc->ifc_nand.nand_fcr0);
490 fsl_ifc_run_command(mtd);
494 dev_err(priv->dev, "%s: error, unsupported command 0x%x.\n",
499 static void fsl_ifc_select_chip(struct nand_chip *chip, int cs)
501 /* The hardware does not seem to support multiple
507 * Write buf to the IFC NAND Controller Data Buffer
509 static void fsl_ifc_write_buf(struct nand_chip *chip, const u8 *buf, int len)
511 struct mtd_info *mtd = nand_to_mtd(chip);
512 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
513 unsigned int bufsize = mtd->writesize + mtd->oobsize;
516 dev_err(priv->dev, "%s: len %d bytes", __func__, len);
520 if ((unsigned int)len > bufsize - ifc_nand_ctrl->index) {
522 "%s: beyond end of buffer (%d requested, %u available)\n",
523 __func__, len, bufsize - ifc_nand_ctrl->index);
524 len = bufsize - ifc_nand_ctrl->index;
527 memcpy_toio(ifc_nand_ctrl->addr + ifc_nand_ctrl->index, buf, len);
528 ifc_nand_ctrl->index += len;
532 * Read a byte from either the IFC hardware buffer
533 * read function for 8-bit buswidth
535 static uint8_t fsl_ifc_read_byte(struct nand_chip *chip)
537 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
541 * If there are still bytes in the IFC buffer, then use the
544 if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
545 offset = ifc_nand_ctrl->index++;
546 return ifc_in8(ifc_nand_ctrl->addr + offset);
549 dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
554 * Read two bytes from the IFC hardware buffer
555 * read function for 16-bit buswith
557 static uint8_t fsl_ifc_read_byte16(struct nand_chip *chip)
559 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
563 * If there are still bytes in the IFC buffer, then use the
566 if (ifc_nand_ctrl->index < ifc_nand_ctrl->read_bytes) {
567 data = ifc_in16(ifc_nand_ctrl->addr + ifc_nand_ctrl->index);
568 ifc_nand_ctrl->index += 2;
569 return (uint8_t) data;
572 dev_err(priv->dev, "%s: beyond end of buffer\n", __func__);
577 * Read from the IFC Controller Data Buffer
579 static void fsl_ifc_read_buf(struct nand_chip *chip, u8 *buf, int len)
581 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
585 dev_err(priv->dev, "%s: len %d bytes", __func__, len);
589 avail = min((unsigned int)len,
590 ifc_nand_ctrl->read_bytes - ifc_nand_ctrl->index);
591 memcpy_fromio(buf, ifc_nand_ctrl->addr + ifc_nand_ctrl->index, avail);
592 ifc_nand_ctrl->index += avail;
596 "%s: beyond end of buffer (%d requested, %d available)\n",
597 __func__, len, avail);
601 * This function is called after Program and Erase Operations to
602 * check for success or failure.
604 static int fsl_ifc_wait(struct nand_chip *chip)
606 struct mtd_info *mtd = nand_to_mtd(chip);
607 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
608 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
609 struct fsl_ifc_runtime __iomem *ifc = ctrl->rregs;
613 /* Use READ_STATUS command, but wait for the device to be ready */
614 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
615 (IFC_FIR_OP_RDSTAT << IFC_NAND_FIR0_OP1_SHIFT),
616 &ifc->ifc_nand.nand_fir0);
617 ifc_out32(NAND_CMD_STATUS << IFC_NAND_FCR0_CMD0_SHIFT,
618 &ifc->ifc_nand.nand_fcr0);
619 ifc_out32(1, &ifc->ifc_nand.nand_fbcr);
620 set_addr(mtd, 0, 0, 0);
621 ifc_nand_ctrl->read_bytes = 1;
623 fsl_ifc_run_command(mtd);
625 nand_fsr = ifc_in32(&ifc->ifc_nand.nand_fsr);
626 status = nand_fsr >> 24;
628 * The chip always seems to report that it is
629 * write-protected, even when it is not.
631 return status | NAND_STATUS_WP;
635 * The controller does not check for bitflips in erased pages,
636 * therefore software must check instead.
638 static int check_erased_page(struct nand_chip *chip, u8 *buf)
640 struct mtd_info *mtd = nand_to_mtd(chip);
641 u8 *ecc = chip->oob_poi;
642 const int ecc_size = chip->ecc.bytes;
643 const int pkt_size = chip->ecc.size;
644 int i, res, bitflips = 0;
645 struct mtd_oob_region oobregion = { };
647 mtd_ooblayout_ecc(mtd, 0, &oobregion);
648 ecc += oobregion.offset;
650 for (i = 0; i < chip->ecc.steps; ++i) {
651 res = nand_check_erased_ecc_chunk(buf, pkt_size, ecc, ecc_size,
655 mtd->ecc_stats.failed++;
657 mtd->ecc_stats.corrected += res;
659 bitflips = max(res, bitflips);
667 static int fsl_ifc_read_page(struct nand_chip *chip, uint8_t *buf,
668 int oob_required, int page)
670 struct mtd_info *mtd = nand_to_mtd(chip);
671 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
672 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
673 struct fsl_ifc_nand_ctrl *nctrl = ifc_nand_ctrl;
675 nand_read_page_op(chip, page, 0, buf, mtd->writesize);
677 fsl_ifc_read_buf(chip, chip->oob_poi, mtd->oobsize);
679 if (ctrl->nand_stat & IFC_NAND_EVTER_STAT_ECCER) {
681 fsl_ifc_read_buf(chip, chip->oob_poi, mtd->oobsize);
683 return check_erased_page(chip, buf);
686 if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC)
687 mtd->ecc_stats.failed++;
689 return nctrl->max_bitflips;
692 /* ECC will be calculated automatically, and errors will be detected in
695 static int fsl_ifc_write_page(struct nand_chip *chip, const uint8_t *buf,
696 int oob_required, int page)
698 struct mtd_info *mtd = nand_to_mtd(chip);
700 nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
701 fsl_ifc_write_buf(chip, chip->oob_poi, mtd->oobsize);
703 return nand_prog_page_end_op(chip);
706 static int fsl_ifc_attach_chip(struct nand_chip *chip)
708 struct mtd_info *mtd = nand_to_mtd(chip);
709 struct fsl_ifc_mtd *priv = nand_get_controller_data(chip);
710 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
711 struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs;
714 csor = ifc_in32(&ifc_global->csor_cs[priv->bank].csor);
716 /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
717 if (csor & CSOR_NAND_ECC_DEC_EN) {
718 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
719 mtd_set_ooblayout(mtd, &fsl_ifc_ooblayout_ops);
721 /* Hardware generates ECC per 512 Bytes */
722 chip->ecc.size = 512;
723 if ((csor & CSOR_NAND_ECC_MODE_MASK) == CSOR_NAND_ECC_MODE_4) {
725 chip->ecc.strength = 4;
727 chip->ecc.bytes = 16;
728 chip->ecc.strength = 8;
731 chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
732 chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
735 dev_dbg(priv->dev, "%s: nand->numchips = %d\n", __func__,
736 nanddev_ntargets(&chip->base));
737 dev_dbg(priv->dev, "%s: nand->chipsize = %lld\n", __func__,
738 nanddev_target_size(&chip->base));
739 dev_dbg(priv->dev, "%s: nand->pagemask = %8x\n", __func__,
741 dev_dbg(priv->dev, "%s: nand->legacy.chip_delay = %d\n", __func__,
742 chip->legacy.chip_delay);
743 dev_dbg(priv->dev, "%s: nand->badblockpos = %d\n", __func__,
745 dev_dbg(priv->dev, "%s: nand->chip_shift = %d\n", __func__,
747 dev_dbg(priv->dev, "%s: nand->page_shift = %d\n", __func__,
749 dev_dbg(priv->dev, "%s: nand->phys_erase_shift = %d\n", __func__,
750 chip->phys_erase_shift);
751 dev_dbg(priv->dev, "%s: nand->ecc.engine_type = %d\n", __func__,
752 chip->ecc.engine_type);
753 dev_dbg(priv->dev, "%s: nand->ecc.steps = %d\n", __func__,
755 dev_dbg(priv->dev, "%s: nand->ecc.bytes = %d\n", __func__,
757 dev_dbg(priv->dev, "%s: nand->ecc.total = %d\n", __func__,
759 dev_dbg(priv->dev, "%s: mtd->ooblayout = %p\n", __func__,
761 dev_dbg(priv->dev, "%s: mtd->flags = %08x\n", __func__, mtd->flags);
762 dev_dbg(priv->dev, "%s: mtd->size = %lld\n", __func__, mtd->size);
763 dev_dbg(priv->dev, "%s: mtd->erasesize = %d\n", __func__,
765 dev_dbg(priv->dev, "%s: mtd->writesize = %d\n", __func__,
767 dev_dbg(priv->dev, "%s: mtd->oobsize = %d\n", __func__,
773 static const struct nand_controller_ops fsl_ifc_controller_ops = {
774 .attach_chip = fsl_ifc_attach_chip,
777 static int fsl_ifc_sram_init(struct fsl_ifc_mtd *priv)
779 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
780 struct fsl_ifc_runtime __iomem *ifc_runtime = ctrl->rregs;
781 struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs;
782 uint32_t csor = 0, csor_8k = 0, csor_ext = 0;
783 uint32_t cs = priv->bank;
785 if (ctrl->version < FSL_IFC_VERSION_1_1_0)
788 if (ctrl->version > FSL_IFC_VERSION_1_1_0) {
792 /* Trigger auto initialization */
793 ncfgr = ifc_in32(&ifc_runtime->ifc_nand.ncfgr);
794 ifc_out32(ncfgr | IFC_NAND_NCFGR_SRAM_INIT_EN, &ifc_runtime->ifc_nand.ncfgr);
796 /* Wait until done */
797 ret = readx_poll_timeout(ifc_in32, &ifc_runtime->ifc_nand.ncfgr,
798 status, !(status & IFC_NAND_NCFGR_SRAM_INIT_EN),
799 10, IFC_TIMEOUT_MSECS * 1000);
801 dev_err(priv->dev, "Failed to initialize SRAM!\n");
806 /* Save CSOR and CSOR_ext */
807 csor = ifc_in32(&ifc_global->csor_cs[cs].csor);
808 csor_ext = ifc_in32(&ifc_global->csor_cs[cs].csor_ext);
810 /* chage PageSize 8K and SpareSize 1K*/
811 csor_8k = (csor & ~(CSOR_NAND_PGS_MASK)) | 0x0018C000;
812 ifc_out32(csor_8k, &ifc_global->csor_cs[cs].csor);
813 ifc_out32(0x0000400, &ifc_global->csor_cs[cs].csor_ext);
816 ifc_out32((IFC_FIR_OP_CW0 << IFC_NAND_FIR0_OP0_SHIFT) |
817 (IFC_FIR_OP_UA << IFC_NAND_FIR0_OP1_SHIFT) |
818 (IFC_FIR_OP_RB << IFC_NAND_FIR0_OP2_SHIFT),
819 &ifc_runtime->ifc_nand.nand_fir0);
820 ifc_out32(NAND_CMD_READID << IFC_NAND_FCR0_CMD0_SHIFT,
821 &ifc_runtime->ifc_nand.nand_fcr0);
822 ifc_out32(0x0, &ifc_runtime->ifc_nand.row3);
824 ifc_out32(0x0, &ifc_runtime->ifc_nand.nand_fbcr);
826 /* Program ROW0/COL0 */
827 ifc_out32(0x0, &ifc_runtime->ifc_nand.row0);
828 ifc_out32(0x0, &ifc_runtime->ifc_nand.col0);
830 /* set the chip select for NAND Transaction */
831 ifc_out32(cs << IFC_NAND_CSEL_SHIFT,
832 &ifc_runtime->ifc_nand.nand_csel);
835 ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT,
836 &ifc_runtime->ifc_nand.nandseq_strt);
838 /* wait for command complete flag or timeout */
839 wait_event_timeout(ctrl->nand_wait, ctrl->nand_stat,
840 msecs_to_jiffies(IFC_TIMEOUT_MSECS));
842 if (ctrl->nand_stat != IFC_NAND_EVTER_STAT_OPC) {
843 pr_err("fsl-ifc: Failed to Initialise SRAM\n");
847 /* Restore CSOR and CSOR_ext */
848 ifc_out32(csor, &ifc_global->csor_cs[cs].csor);
849 ifc_out32(csor_ext, &ifc_global->csor_cs[cs].csor_ext);
854 static int fsl_ifc_chip_init(struct fsl_ifc_mtd *priv)
856 struct fsl_ifc_ctrl *ctrl = priv->ctrl;
857 struct fsl_ifc_global __iomem *ifc_global = ctrl->gregs;
858 struct fsl_ifc_runtime __iomem *ifc_runtime = ctrl->rregs;
859 struct nand_chip *chip = &priv->chip;
860 struct mtd_info *mtd = nand_to_mtd(&priv->chip);
864 /* Fill in fsl_ifc_mtd structure */
865 mtd->dev.parent = priv->dev;
866 nand_set_flash_node(chip, priv->dev->of_node);
868 /* fill in nand_chip structure */
869 /* set up function call table */
870 if ((ifc_in32(&ifc_global->cspr_cs[priv->bank].cspr))
872 chip->legacy.read_byte = fsl_ifc_read_byte16;
874 chip->legacy.read_byte = fsl_ifc_read_byte;
876 chip->legacy.write_buf = fsl_ifc_write_buf;
877 chip->legacy.read_buf = fsl_ifc_read_buf;
878 chip->legacy.select_chip = fsl_ifc_select_chip;
879 chip->legacy.cmdfunc = fsl_ifc_cmdfunc;
880 chip->legacy.waitfunc = fsl_ifc_wait;
881 chip->legacy.set_features = nand_get_set_features_notsupp;
882 chip->legacy.get_features = nand_get_set_features_notsupp;
884 chip->bbt_td = &bbt_main_descr;
885 chip->bbt_md = &bbt_mirror_descr;
887 ifc_out32(0x0, &ifc_runtime->ifc_nand.ncfgr);
889 /* set up nand options */
890 chip->bbt_options = NAND_BBT_USE_FLASH;
891 chip->options = NAND_NO_SUBPAGE_WRITE;
893 if (ifc_in32(&ifc_global->cspr_cs[priv->bank].cspr)
894 & CSPR_PORT_SIZE_16) {
895 chip->legacy.read_byte = fsl_ifc_read_byte16;
896 chip->options |= NAND_BUSWIDTH_16;
898 chip->legacy.read_byte = fsl_ifc_read_byte;
901 chip->controller = &ifc_nand_ctrl->controller;
902 nand_set_controller_data(chip, priv);
904 chip->ecc.read_page = fsl_ifc_read_page;
905 chip->ecc.write_page = fsl_ifc_write_page;
907 csor = ifc_in32(&ifc_global->csor_cs[priv->bank].csor);
909 switch (csor & CSOR_NAND_PGS_MASK) {
910 case CSOR_NAND_PGS_512:
911 if (!(chip->options & NAND_BUSWIDTH_16)) {
912 /* Avoid conflict with bad block marker */
913 bbt_main_descr.offs = 0;
914 bbt_mirror_descr.offs = 0;
917 priv->bufnum_mask = 15;
920 case CSOR_NAND_PGS_2K:
921 priv->bufnum_mask = 3;
924 case CSOR_NAND_PGS_4K:
925 priv->bufnum_mask = 1;
928 case CSOR_NAND_PGS_8K:
929 priv->bufnum_mask = 0;
933 dev_err(priv->dev, "bad csor %#x: bad page size\n", csor);
937 ret = fsl_ifc_sram_init(priv);
942 * As IFC version 2.0.0 has 16KB of internal SRAM as compared to older
943 * versions which had 8KB. Hence bufnum mask needs to be updated.
945 if (ctrl->version >= FSL_IFC_VERSION_2_0_0)
946 priv->bufnum_mask = (priv->bufnum_mask * 2) + 1;
951 static int fsl_ifc_chip_remove(struct fsl_ifc_mtd *priv)
953 struct mtd_info *mtd = nand_to_mtd(&priv->chip);
958 iounmap(priv->vbase);
960 ifc_nand_ctrl->chips[priv->bank] = NULL;
965 static int match_bank(struct fsl_ifc_global __iomem *ifc_global, int bank,
968 u32 cspr = ifc_in32(&ifc_global->cspr_cs[bank].cspr);
970 if (!(cspr & CSPR_V))
972 if ((cspr & CSPR_MSEL) != CSPR_MSEL_NAND)
975 return (cspr & CSPR_BA) == convert_ifc_address(addr);
978 static DEFINE_MUTEX(fsl_ifc_nand_mutex);
980 static int fsl_ifc_nand_probe(struct platform_device *dev)
982 struct fsl_ifc_runtime __iomem *ifc;
983 struct fsl_ifc_mtd *priv;
985 static const char *part_probe_types[]
986 = { "cmdlinepart", "RedBoot", "ofpart", NULL };
989 struct device_node *node = dev->dev.of_node;
990 struct mtd_info *mtd;
992 if (!fsl_ifc_ctrl_dev || !fsl_ifc_ctrl_dev->rregs)
994 ifc = fsl_ifc_ctrl_dev->rregs;
996 /* get, allocate and map the memory resource */
997 ret = of_address_to_resource(node, 0, &res);
999 dev_err(&dev->dev, "%s: failed to get resource\n", __func__);
1003 /* find which chip select it is connected to */
1004 for (bank = 0; bank < fsl_ifc_ctrl_dev->banks; bank++) {
1005 if (match_bank(fsl_ifc_ctrl_dev->gregs, bank, res.start))
1009 if (bank >= fsl_ifc_ctrl_dev->banks) {
1010 dev_err(&dev->dev, "%s: address did not match any chip selects\n",
1015 priv = devm_kzalloc(&dev->dev, sizeof(*priv), GFP_KERNEL);
1019 mutex_lock(&fsl_ifc_nand_mutex);
1020 if (!fsl_ifc_ctrl_dev->nand) {
1021 ifc_nand_ctrl = kzalloc(sizeof(*ifc_nand_ctrl), GFP_KERNEL);
1022 if (!ifc_nand_ctrl) {
1023 mutex_unlock(&fsl_ifc_nand_mutex);
1027 ifc_nand_ctrl->read_bytes = 0;
1028 ifc_nand_ctrl->index = 0;
1029 ifc_nand_ctrl->addr = NULL;
1030 fsl_ifc_ctrl_dev->nand = ifc_nand_ctrl;
1032 nand_controller_init(&ifc_nand_ctrl->controller);
1034 ifc_nand_ctrl = fsl_ifc_ctrl_dev->nand;
1036 mutex_unlock(&fsl_ifc_nand_mutex);
1038 ifc_nand_ctrl->chips[bank] = priv;
1040 priv->ctrl = fsl_ifc_ctrl_dev;
1041 priv->dev = &dev->dev;
1043 priv->vbase = ioremap(res.start, resource_size(&res));
1045 dev_err(priv->dev, "%s: failed to map chip region\n", __func__);
1050 dev_set_drvdata(priv->dev, priv);
1052 ifc_out32(IFC_NAND_EVTER_EN_OPC_EN |
1053 IFC_NAND_EVTER_EN_FTOER_EN |
1054 IFC_NAND_EVTER_EN_WPER_EN,
1055 &ifc->ifc_nand.nand_evter_en);
1057 /* enable NAND Machine Interrupts */
1058 ifc_out32(IFC_NAND_EVTER_INTR_OPCIR_EN |
1059 IFC_NAND_EVTER_INTR_FTOERIR_EN |
1060 IFC_NAND_EVTER_INTR_WPERIR_EN,
1061 &ifc->ifc_nand.nand_evter_intr_en);
1063 mtd = nand_to_mtd(&priv->chip);
1064 mtd->name = kasprintf(GFP_KERNEL, "%llx.flash", (u64)res.start);
1070 ret = fsl_ifc_chip_init(priv);
1074 priv->chip.controller->ops = &fsl_ifc_controller_ops;
1075 ret = nand_scan(&priv->chip, 1);
1079 /* First look for RedBoot table or partitions on the command
1080 * line, these take precedence over device tree information */
1081 ret = mtd_device_parse_register(mtd, part_probe_types, NULL, NULL, 0);
1085 dev_info(priv->dev, "IFC NAND device at 0x%llx, bank %d\n",
1086 (unsigned long long)res.start, priv->bank);
1091 nand_cleanup(&priv->chip);
1093 fsl_ifc_chip_remove(priv);
1098 static int fsl_ifc_nand_remove(struct platform_device *dev)
1100 struct fsl_ifc_mtd *priv = dev_get_drvdata(&dev->dev);
1101 struct nand_chip *chip = &priv->chip;
1104 ret = mtd_device_unregister(nand_to_mtd(chip));
1108 fsl_ifc_chip_remove(priv);
1110 mutex_lock(&fsl_ifc_nand_mutex);
1111 ifc_nand_ctrl->counter--;
1112 if (!ifc_nand_ctrl->counter) {
1113 fsl_ifc_ctrl_dev->nand = NULL;
1114 kfree(ifc_nand_ctrl);
1116 mutex_unlock(&fsl_ifc_nand_mutex);
1121 static const struct of_device_id fsl_ifc_nand_match[] = {
1123 .compatible = "fsl,ifc-nand",
1127 MODULE_DEVICE_TABLE(of, fsl_ifc_nand_match);
1129 static struct platform_driver fsl_ifc_nand_driver = {
1131 .name = "fsl,ifc-nand",
1132 .of_match_table = fsl_ifc_nand_match,
1134 .probe = fsl_ifc_nand_probe,
1135 .remove = fsl_ifc_nand_remove,
1138 module_platform_driver(fsl_ifc_nand_driver);
1140 MODULE_LICENSE("GPL");
1141 MODULE_AUTHOR("Freescale");
1142 MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");