2 * Freescale Integrated Flash Controller NAND driver
4 * Copyright 2011-2012 Freescale Semiconductor, Inc
6 * Author: Dipen Dudhat <Dipen.Dudhat@freescale.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/kernel.h>
26 #include <linux/of_address.h>
27 #include <linux/slab.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/nand.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/mtd/nand_ecc.h>
32 #include <linux/fsl_ifc.h>
34 #define ERR_BYTE 0xFF /* Value returned for read
35 bytes when read failed */
36 #define IFC_TIMEOUT_MSECS 500 /* Maximum number of mSecs to wait
37 for IFC NAND Machine */
41 /* mtd information per set */
43 struct nand_chip chip
;
44 struct fsl_ifc_ctrl
*ctrl
;
47 int bank
; /* Chip select bank number */
48 unsigned int bufnum_mask
; /* bufnum = page & bufnum_mask */
49 u8 __iomem
*vbase
; /* Chip select base virtual address */
52 /* overview of the fsl ifc controller */
53 struct fsl_ifc_nand_ctrl
{
54 struct nand_hw_control controller
;
55 struct fsl_ifc_mtd
*chips
[FSL_IFC_BANK_COUNT
];
57 void __iomem
*addr
; /* Address of assigned IFC buffer */
58 unsigned int page
; /* Last page written to / read from */
59 unsigned int read_bytes
;/* Number of bytes read during command */
60 unsigned int column
; /* Saved column from SEQIN */
61 unsigned int index
; /* Pointer to next byte to 'read' */
62 unsigned int oob
; /* Non zero if operating on OOB data */
63 unsigned int eccread
; /* Non zero for a full-page ECC read */
64 unsigned int counter
; /* counter for the initializations */
65 unsigned int max_bitflips
; /* Saved during READ0 cmd */
68 static struct fsl_ifc_nand_ctrl
*ifc_nand_ctrl
;
71 * Generic flash bbt descriptors
73 static u8 bbt_pattern
[] = {'B', 'b', 't', '0' };
74 static u8 mirror_pattern
[] = {'1', 't', 'b', 'B' };
76 static struct nand_bbt_descr bbt_main_descr
= {
77 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
|
78 NAND_BBT_2BIT
| NAND_BBT_VERSION
,
79 .offs
= 2, /* 0 on 8-bit small page */
83 .pattern
= bbt_pattern
,
86 static struct nand_bbt_descr bbt_mirror_descr
= {
87 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
|
88 NAND_BBT_2BIT
| NAND_BBT_VERSION
,
89 .offs
= 2, /* 0 on 8-bit small page */
93 .pattern
= mirror_pattern
,
96 static int fsl_ifc_ooblayout_ecc(struct mtd_info
*mtd
, int section
,
97 struct mtd_oob_region
*oobregion
)
99 struct nand_chip
*chip
= mtd_to_nand(mtd
);
104 oobregion
->offset
= 8;
105 oobregion
->length
= chip
->ecc
.total
;
110 static int fsl_ifc_ooblayout_free(struct mtd_info
*mtd
, int section
,
111 struct mtd_oob_region
*oobregion
)
113 struct nand_chip
*chip
= mtd_to_nand(mtd
);
118 if (mtd
->writesize
== 512 &&
119 !(chip
->options
& NAND_BUSWIDTH_16
)) {
121 oobregion
->offset
= 0;
122 oobregion
->length
= 5;
124 oobregion
->offset
= 6;
125 oobregion
->length
= 2;
132 oobregion
->offset
= 2;
133 oobregion
->length
= 6;
135 oobregion
->offset
= chip
->ecc
.total
+ 8;
136 oobregion
->length
= mtd
->oobsize
- oobregion
->offset
;
142 static const struct mtd_ooblayout_ops fsl_ifc_ooblayout_ops
= {
143 .ecc
= fsl_ifc_ooblayout_ecc
,
144 .free
= fsl_ifc_ooblayout_free
,
148 * Set up the IFC hardware block and page address fields, and the ifc nand
149 * structure addr field to point to the correct IFC buffer in memory
151 static void set_addr(struct mtd_info
*mtd
, int column
, int page_addr
, int oob
)
153 struct nand_chip
*chip
= mtd_to_nand(mtd
);
154 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
155 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
156 struct fsl_ifc_runtime __iomem
*ifc
= ctrl
->rregs
;
159 ifc_nand_ctrl
->page
= page_addr
;
160 /* Program ROW0/COL0 */
161 ifc_out32(page_addr
, &ifc
->ifc_nand
.row0
);
162 ifc_out32((oob
? IFC_NAND_COL_MS
: 0) | column
, &ifc
->ifc_nand
.col0
);
164 buf_num
= page_addr
& priv
->bufnum_mask
;
166 ifc_nand_ctrl
->addr
= priv
->vbase
+ buf_num
* (mtd
->writesize
* 2);
167 ifc_nand_ctrl
->index
= column
;
169 /* for OOB data point to the second half of the buffer */
171 ifc_nand_ctrl
->index
+= mtd
->writesize
;
174 static int is_blank(struct mtd_info
*mtd
, unsigned int bufnum
)
176 struct nand_chip
*chip
= mtd_to_nand(mtd
);
177 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
178 u8 __iomem
*addr
= priv
->vbase
+ bufnum
* (mtd
->writesize
* 2);
179 u32 __iomem
*mainarea
= (u32 __iomem
*)addr
;
180 u8 __iomem
*oob
= addr
+ mtd
->writesize
;
181 struct mtd_oob_region oobregion
= { };
184 for (i
= 0; i
< mtd
->writesize
/ 4; i
++) {
185 if (__raw_readl(&mainarea
[i
]) != 0xffffffff)
189 mtd_ooblayout_ecc(mtd
, section
++, &oobregion
);
190 while (oobregion
.length
) {
191 for (i
= 0; i
< oobregion
.length
; i
++) {
192 if (__raw_readb(&oob
[oobregion
.offset
+ i
]) != 0xff)
196 mtd_ooblayout_ecc(mtd
, section
++, &oobregion
);
202 /* returns nonzero if entire page is blank */
203 static int check_read_ecc(struct mtd_info
*mtd
, struct fsl_ifc_ctrl
*ctrl
,
204 u32 eccstat
, unsigned int bufnum
)
206 return (eccstat
>> ((3 - bufnum
% 4) * 8)) & 15;
210 * execute IFC NAND command and wait for it to complete
212 static void fsl_ifc_run_command(struct mtd_info
*mtd
)
214 struct nand_chip
*chip
= mtd_to_nand(mtd
);
215 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
216 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
217 struct fsl_ifc_nand_ctrl
*nctrl
= ifc_nand_ctrl
;
218 struct fsl_ifc_runtime __iomem
*ifc
= ctrl
->rregs
;
222 /* set the chip select for NAND Transaction */
223 ifc_out32(priv
->bank
<< IFC_NAND_CSEL_SHIFT
,
224 &ifc
->ifc_nand
.nand_csel
);
227 "%s: fir0=%08x fcr0=%08x\n",
229 ifc_in32(&ifc
->ifc_nand
.nand_fir0
),
230 ifc_in32(&ifc
->ifc_nand
.nand_fcr0
));
234 /* start read/write seq */
235 ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT
, &ifc
->ifc_nand
.nandseq_strt
);
237 /* wait for command complete flag or timeout */
238 wait_event_timeout(ctrl
->nand_wait
, ctrl
->nand_stat
,
239 msecs_to_jiffies(IFC_TIMEOUT_MSECS
));
241 /* ctrl->nand_stat will be updated from IRQ context */
242 if (!ctrl
->nand_stat
)
243 dev_err(priv
->dev
, "Controller is not responding\n");
244 if (ctrl
->nand_stat
& IFC_NAND_EVTER_STAT_FTOER
)
245 dev_err(priv
->dev
, "NAND Flash Timeout Error\n");
246 if (ctrl
->nand_stat
& IFC_NAND_EVTER_STAT_WPER
)
247 dev_err(priv
->dev
, "NAND Flash Write Protect Error\n");
249 nctrl
->max_bitflips
= 0;
251 if (nctrl
->eccread
) {
253 int bufnum
= nctrl
->page
& priv
->bufnum_mask
;
254 int sector_start
= bufnum
* chip
->ecc
.steps
;
255 int sector_end
= sector_start
+ chip
->ecc
.steps
- 1;
256 __be32
*eccstat_regs
;
258 eccstat_regs
= ifc
->ifc_nand
.nand_eccstat
;
259 eccstat
= ifc_in32(&eccstat_regs
[sector_start
/ 4]);
261 for (i
= sector_start
; i
<= sector_end
; i
++) {
262 if (i
!= sector_start
&& !(i
% 4))
263 eccstat
= ifc_in32(&eccstat_regs
[i
/ 4]);
265 errors
= check_read_ecc(mtd
, ctrl
, eccstat
, i
);
269 * Uncorrectable error.
270 * OK only if the whole page is blank.
272 * We disable ECCER reporting due to...
273 * erratum IFC-A002770 -- so report it now if we
274 * see an uncorrectable error in ECCSTAT.
276 if (!is_blank(mtd
, bufnum
))
278 IFC_NAND_EVTER_STAT_ECCER
;
282 mtd
->ecc_stats
.corrected
+= errors
;
283 nctrl
->max_bitflips
= max_t(unsigned int,
292 static void fsl_ifc_do_read(struct nand_chip
*chip
,
294 struct mtd_info
*mtd
)
296 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
297 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
298 struct fsl_ifc_runtime __iomem
*ifc
= ctrl
->rregs
;
300 /* Program FIR/IFC_NAND_FCR0 for Small/Large page */
301 if (mtd
->writesize
> 512) {
302 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
303 (IFC_FIR_OP_CA0
<< IFC_NAND_FIR0_OP1_SHIFT
) |
304 (IFC_FIR_OP_RA0
<< IFC_NAND_FIR0_OP2_SHIFT
) |
305 (IFC_FIR_OP_CMD1
<< IFC_NAND_FIR0_OP3_SHIFT
) |
306 (IFC_FIR_OP_RBCD
<< IFC_NAND_FIR0_OP4_SHIFT
),
307 &ifc
->ifc_nand
.nand_fir0
);
308 ifc_out32(0x0, &ifc
->ifc_nand
.nand_fir1
);
310 ifc_out32((NAND_CMD_READ0
<< IFC_NAND_FCR0_CMD0_SHIFT
) |
311 (NAND_CMD_READSTART
<< IFC_NAND_FCR0_CMD1_SHIFT
),
312 &ifc
->ifc_nand
.nand_fcr0
);
314 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
315 (IFC_FIR_OP_CA0
<< IFC_NAND_FIR0_OP1_SHIFT
) |
316 (IFC_FIR_OP_RA0
<< IFC_NAND_FIR0_OP2_SHIFT
) |
317 (IFC_FIR_OP_RBCD
<< IFC_NAND_FIR0_OP3_SHIFT
),
318 &ifc
->ifc_nand
.nand_fir0
);
319 ifc_out32(0x0, &ifc
->ifc_nand
.nand_fir1
);
322 ifc_out32(NAND_CMD_READOOB
<<
323 IFC_NAND_FCR0_CMD0_SHIFT
,
324 &ifc
->ifc_nand
.nand_fcr0
);
326 ifc_out32(NAND_CMD_READ0
<<
327 IFC_NAND_FCR0_CMD0_SHIFT
,
328 &ifc
->ifc_nand
.nand_fcr0
);
332 /* cmdfunc send commands to the IFC NAND Machine */
333 static void fsl_ifc_cmdfunc(struct mtd_info
*mtd
, unsigned int command
,
334 int column
, int page_addr
) {
335 struct nand_chip
*chip
= mtd_to_nand(mtd
);
336 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
337 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
338 struct fsl_ifc_runtime __iomem
*ifc
= ctrl
->rregs
;
340 /* clear the read buffer */
341 ifc_nand_ctrl
->read_bytes
= 0;
342 if (command
!= NAND_CMD_PAGEPROG
)
343 ifc_nand_ctrl
->index
= 0;
346 /* READ0 read the entire buffer to use hardware ECC. */
348 ifc_out32(0, &ifc
->ifc_nand
.nand_fbcr
);
349 set_addr(mtd
, 0, page_addr
, 0);
351 ifc_nand_ctrl
->read_bytes
= mtd
->writesize
+ mtd
->oobsize
;
352 ifc_nand_ctrl
->index
+= column
;
354 if (chip
->ecc
.mode
== NAND_ECC_HW
)
355 ifc_nand_ctrl
->eccread
= 1;
357 fsl_ifc_do_read(chip
, 0, mtd
);
358 fsl_ifc_run_command(mtd
);
361 /* READOOB reads only the OOB because no ECC is performed. */
362 case NAND_CMD_READOOB
:
363 ifc_out32(mtd
->oobsize
- column
, &ifc
->ifc_nand
.nand_fbcr
);
364 set_addr(mtd
, column
, page_addr
, 1);
366 ifc_nand_ctrl
->read_bytes
= mtd
->writesize
+ mtd
->oobsize
;
368 fsl_ifc_do_read(chip
, 1, mtd
);
369 fsl_ifc_run_command(mtd
);
373 case NAND_CMD_READID
:
374 case NAND_CMD_PARAM
: {
376 * For READID, read 8 bytes that are currently used.
377 * For PARAM, read all 3 copies of 256-bytes pages.
380 int timing
= IFC_FIR_OP_RB
;
381 if (command
== NAND_CMD_PARAM
) {
382 timing
= IFC_FIR_OP_RBCD
;
386 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
387 (IFC_FIR_OP_UA
<< IFC_NAND_FIR0_OP1_SHIFT
) |
388 (timing
<< IFC_NAND_FIR0_OP2_SHIFT
),
389 &ifc
->ifc_nand
.nand_fir0
);
390 ifc_out32(command
<< IFC_NAND_FCR0_CMD0_SHIFT
,
391 &ifc
->ifc_nand
.nand_fcr0
);
392 ifc_out32(column
, &ifc
->ifc_nand
.row3
);
394 ifc_out32(len
, &ifc
->ifc_nand
.nand_fbcr
);
395 ifc_nand_ctrl
->read_bytes
= len
;
397 set_addr(mtd
, 0, 0, 0);
398 fsl_ifc_run_command(mtd
);
402 /* ERASE1 stores the block and page address */
403 case NAND_CMD_ERASE1
:
404 set_addr(mtd
, 0, page_addr
, 0);
407 /* ERASE2 uses the block and page address from ERASE1 */
408 case NAND_CMD_ERASE2
:
409 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
410 (IFC_FIR_OP_RA0
<< IFC_NAND_FIR0_OP1_SHIFT
) |
411 (IFC_FIR_OP_CMD1
<< IFC_NAND_FIR0_OP2_SHIFT
),
412 &ifc
->ifc_nand
.nand_fir0
);
414 ifc_out32((NAND_CMD_ERASE1
<< IFC_NAND_FCR0_CMD0_SHIFT
) |
415 (NAND_CMD_ERASE2
<< IFC_NAND_FCR0_CMD1_SHIFT
),
416 &ifc
->ifc_nand
.nand_fcr0
);
418 ifc_out32(0, &ifc
->ifc_nand
.nand_fbcr
);
419 ifc_nand_ctrl
->read_bytes
= 0;
420 fsl_ifc_run_command(mtd
);
423 /* SEQIN sets up the addr buffer and all registers except the length */
424 case NAND_CMD_SEQIN
: {
426 ifc_nand_ctrl
->column
= column
;
427 ifc_nand_ctrl
->oob
= 0;
429 if (mtd
->writesize
> 512) {
431 (NAND_CMD_SEQIN
<< IFC_NAND_FCR0_CMD0_SHIFT
) |
432 (NAND_CMD_STATUS
<< IFC_NAND_FCR0_CMD1_SHIFT
) |
433 (NAND_CMD_PAGEPROG
<< IFC_NAND_FCR0_CMD2_SHIFT
);
436 (IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
437 (IFC_FIR_OP_CA0
<< IFC_NAND_FIR0_OP1_SHIFT
) |
438 (IFC_FIR_OP_RA0
<< IFC_NAND_FIR0_OP2_SHIFT
) |
439 (IFC_FIR_OP_WBCD
<< IFC_NAND_FIR0_OP3_SHIFT
) |
440 (IFC_FIR_OP_CMD2
<< IFC_NAND_FIR0_OP4_SHIFT
),
441 &ifc
->ifc_nand
.nand_fir0
);
443 (IFC_FIR_OP_CW1
<< IFC_NAND_FIR1_OP5_SHIFT
) |
444 (IFC_FIR_OP_RDSTAT
<< IFC_NAND_FIR1_OP6_SHIFT
) |
445 (IFC_FIR_OP_NOP
<< IFC_NAND_FIR1_OP7_SHIFT
),
446 &ifc
->ifc_nand
.nand_fir1
);
448 nand_fcr0
= ((NAND_CMD_PAGEPROG
<<
449 IFC_NAND_FCR0_CMD1_SHIFT
) |
451 IFC_NAND_FCR0_CMD2_SHIFT
) |
453 IFC_NAND_FCR0_CMD3_SHIFT
));
456 (IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
457 (IFC_FIR_OP_CMD2
<< IFC_NAND_FIR0_OP1_SHIFT
) |
458 (IFC_FIR_OP_CA0
<< IFC_NAND_FIR0_OP2_SHIFT
) |
459 (IFC_FIR_OP_RA0
<< IFC_NAND_FIR0_OP3_SHIFT
) |
460 (IFC_FIR_OP_WBCD
<< IFC_NAND_FIR0_OP4_SHIFT
),
461 &ifc
->ifc_nand
.nand_fir0
);
463 (IFC_FIR_OP_CMD1
<< IFC_NAND_FIR1_OP5_SHIFT
) |
464 (IFC_FIR_OP_CW3
<< IFC_NAND_FIR1_OP6_SHIFT
) |
465 (IFC_FIR_OP_RDSTAT
<< IFC_NAND_FIR1_OP7_SHIFT
) |
466 (IFC_FIR_OP_NOP
<< IFC_NAND_FIR1_OP8_SHIFT
),
467 &ifc
->ifc_nand
.nand_fir1
);
469 if (column
>= mtd
->writesize
)
471 NAND_CMD_READOOB
<< IFC_NAND_FCR0_CMD0_SHIFT
;
474 NAND_CMD_READ0
<< IFC_NAND_FCR0_CMD0_SHIFT
;
477 if (column
>= mtd
->writesize
) {
478 /* OOB area --> READOOB */
479 column
-= mtd
->writesize
;
480 ifc_nand_ctrl
->oob
= 1;
482 ifc_out32(nand_fcr0
, &ifc
->ifc_nand
.nand_fcr0
);
483 set_addr(mtd
, column
, page_addr
, ifc_nand_ctrl
->oob
);
487 /* PAGEPROG reuses all of the setup from SEQIN and adds the length */
488 case NAND_CMD_PAGEPROG
: {
489 if (ifc_nand_ctrl
->oob
) {
490 ifc_out32(ifc_nand_ctrl
->index
-
491 ifc_nand_ctrl
->column
,
492 &ifc
->ifc_nand
.nand_fbcr
);
494 ifc_out32(0, &ifc
->ifc_nand
.nand_fbcr
);
497 fsl_ifc_run_command(mtd
);
501 case NAND_CMD_STATUS
: {
504 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
505 (IFC_FIR_OP_RB
<< IFC_NAND_FIR0_OP1_SHIFT
),
506 &ifc
->ifc_nand
.nand_fir0
);
507 ifc_out32(NAND_CMD_STATUS
<< IFC_NAND_FCR0_CMD0_SHIFT
,
508 &ifc
->ifc_nand
.nand_fcr0
);
509 ifc_out32(1, &ifc
->ifc_nand
.nand_fbcr
);
510 set_addr(mtd
, 0, 0, 0);
511 ifc_nand_ctrl
->read_bytes
= 1;
513 fsl_ifc_run_command(mtd
);
516 * The chip always seems to report that it is
517 * write-protected, even when it is not.
519 addr
= ifc_nand_ctrl
->addr
;
520 if (chip
->options
& NAND_BUSWIDTH_16
)
521 ifc_out16(ifc_in16(addr
) | (NAND_STATUS_WP
), addr
);
523 ifc_out8(ifc_in8(addr
) | (NAND_STATUS_WP
), addr
);
528 ifc_out32(IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
,
529 &ifc
->ifc_nand
.nand_fir0
);
530 ifc_out32(NAND_CMD_RESET
<< IFC_NAND_FCR0_CMD0_SHIFT
,
531 &ifc
->ifc_nand
.nand_fcr0
);
532 fsl_ifc_run_command(mtd
);
536 dev_err(priv
->dev
, "%s: error, unsupported command 0x%x.\n",
541 static void fsl_ifc_select_chip(struct mtd_info
*mtd
, int chip
)
543 /* The hardware does not seem to support multiple
549 * Write buf to the IFC NAND Controller Data Buffer
551 static void fsl_ifc_write_buf(struct mtd_info
*mtd
, const u8
*buf
, int len
)
553 struct nand_chip
*chip
= mtd_to_nand(mtd
);
554 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
555 unsigned int bufsize
= mtd
->writesize
+ mtd
->oobsize
;
558 dev_err(priv
->dev
, "%s: len %d bytes", __func__
, len
);
562 if ((unsigned int)len
> bufsize
- ifc_nand_ctrl
->index
) {
564 "%s: beyond end of buffer (%d requested, %u available)\n",
565 __func__
, len
, bufsize
- ifc_nand_ctrl
->index
);
566 len
= bufsize
- ifc_nand_ctrl
->index
;
569 memcpy_toio(ifc_nand_ctrl
->addr
+ ifc_nand_ctrl
->index
, buf
, len
);
570 ifc_nand_ctrl
->index
+= len
;
574 * Read a byte from either the IFC hardware buffer
575 * read function for 8-bit buswidth
577 static uint8_t fsl_ifc_read_byte(struct mtd_info
*mtd
)
579 struct nand_chip
*chip
= mtd_to_nand(mtd
);
580 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
584 * If there are still bytes in the IFC buffer, then use the
587 if (ifc_nand_ctrl
->index
< ifc_nand_ctrl
->read_bytes
) {
588 offset
= ifc_nand_ctrl
->index
++;
589 return ifc_in8(ifc_nand_ctrl
->addr
+ offset
);
592 dev_err(priv
->dev
, "%s: beyond end of buffer\n", __func__
);
597 * Read two bytes from the IFC hardware buffer
598 * read function for 16-bit buswith
600 static uint8_t fsl_ifc_read_byte16(struct mtd_info
*mtd
)
602 struct nand_chip
*chip
= mtd_to_nand(mtd
);
603 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
607 * If there are still bytes in the IFC buffer, then use the
610 if (ifc_nand_ctrl
->index
< ifc_nand_ctrl
->read_bytes
) {
611 data
= ifc_in16(ifc_nand_ctrl
->addr
+ ifc_nand_ctrl
->index
);
612 ifc_nand_ctrl
->index
+= 2;
613 return (uint8_t) data
;
616 dev_err(priv
->dev
, "%s: beyond end of buffer\n", __func__
);
621 * Read from the IFC Controller Data Buffer
623 static void fsl_ifc_read_buf(struct mtd_info
*mtd
, u8
*buf
, int len
)
625 struct nand_chip
*chip
= mtd_to_nand(mtd
);
626 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
630 dev_err(priv
->dev
, "%s: len %d bytes", __func__
, len
);
634 avail
= min((unsigned int)len
,
635 ifc_nand_ctrl
->read_bytes
- ifc_nand_ctrl
->index
);
636 memcpy_fromio(buf
, ifc_nand_ctrl
->addr
+ ifc_nand_ctrl
->index
, avail
);
637 ifc_nand_ctrl
->index
+= avail
;
641 "%s: beyond end of buffer (%d requested, %d available)\n",
642 __func__
, len
, avail
);
646 * This function is called after Program and Erase Operations to
647 * check for success or failure.
649 static int fsl_ifc_wait(struct mtd_info
*mtd
, struct nand_chip
*chip
)
651 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
652 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
653 struct fsl_ifc_runtime __iomem
*ifc
= ctrl
->rregs
;
657 /* Use READ_STATUS command, but wait for the device to be ready */
658 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
659 (IFC_FIR_OP_RDSTAT
<< IFC_NAND_FIR0_OP1_SHIFT
),
660 &ifc
->ifc_nand
.nand_fir0
);
661 ifc_out32(NAND_CMD_STATUS
<< IFC_NAND_FCR0_CMD0_SHIFT
,
662 &ifc
->ifc_nand
.nand_fcr0
);
663 ifc_out32(1, &ifc
->ifc_nand
.nand_fbcr
);
664 set_addr(mtd
, 0, 0, 0);
665 ifc_nand_ctrl
->read_bytes
= 1;
667 fsl_ifc_run_command(mtd
);
669 nand_fsr
= ifc_in32(&ifc
->ifc_nand
.nand_fsr
);
670 status
= nand_fsr
>> 24;
672 * The chip always seems to report that it is
673 * write-protected, even when it is not.
675 return status
| NAND_STATUS_WP
;
678 static int fsl_ifc_read_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
679 uint8_t *buf
, int oob_required
, int page
)
681 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
682 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
683 struct fsl_ifc_nand_ctrl
*nctrl
= ifc_nand_ctrl
;
685 fsl_ifc_read_buf(mtd
, buf
, mtd
->writesize
);
687 fsl_ifc_read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
689 if (ctrl
->nand_stat
& IFC_NAND_EVTER_STAT_ECCER
)
690 dev_err(priv
->dev
, "NAND Flash ECC Uncorrectable Error\n");
692 if (ctrl
->nand_stat
!= IFC_NAND_EVTER_STAT_OPC
)
693 mtd
->ecc_stats
.failed
++;
695 return nctrl
->max_bitflips
;
698 /* ECC will be calculated automatically, and errors will be detected in
701 static int fsl_ifc_write_page(struct mtd_info
*mtd
, struct nand_chip
*chip
,
702 const uint8_t *buf
, int oob_required
, int page
)
704 fsl_ifc_write_buf(mtd
, buf
, mtd
->writesize
);
705 fsl_ifc_write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
710 static int fsl_ifc_chip_init_tail(struct mtd_info
*mtd
)
712 struct nand_chip
*chip
= mtd_to_nand(mtd
);
713 struct fsl_ifc_mtd
*priv
= nand_get_controller_data(chip
);
715 dev_dbg(priv
->dev
, "%s: nand->numchips = %d\n", __func__
,
717 dev_dbg(priv
->dev
, "%s: nand->chipsize = %lld\n", __func__
,
719 dev_dbg(priv
->dev
, "%s: nand->pagemask = %8x\n", __func__
,
721 dev_dbg(priv
->dev
, "%s: nand->chip_delay = %d\n", __func__
,
723 dev_dbg(priv
->dev
, "%s: nand->badblockpos = %d\n", __func__
,
725 dev_dbg(priv
->dev
, "%s: nand->chip_shift = %d\n", __func__
,
727 dev_dbg(priv
->dev
, "%s: nand->page_shift = %d\n", __func__
,
729 dev_dbg(priv
->dev
, "%s: nand->phys_erase_shift = %d\n", __func__
,
730 chip
->phys_erase_shift
);
731 dev_dbg(priv
->dev
, "%s: nand->ecc.mode = %d\n", __func__
,
733 dev_dbg(priv
->dev
, "%s: nand->ecc.steps = %d\n", __func__
,
735 dev_dbg(priv
->dev
, "%s: nand->ecc.bytes = %d\n", __func__
,
737 dev_dbg(priv
->dev
, "%s: nand->ecc.total = %d\n", __func__
,
739 dev_dbg(priv
->dev
, "%s: mtd->ooblayout = %p\n", __func__
,
741 dev_dbg(priv
->dev
, "%s: mtd->flags = %08x\n", __func__
, mtd
->flags
);
742 dev_dbg(priv
->dev
, "%s: mtd->size = %lld\n", __func__
, mtd
->size
);
743 dev_dbg(priv
->dev
, "%s: mtd->erasesize = %d\n", __func__
,
745 dev_dbg(priv
->dev
, "%s: mtd->writesize = %d\n", __func__
,
747 dev_dbg(priv
->dev
, "%s: mtd->oobsize = %d\n", __func__
,
753 static void fsl_ifc_sram_init(struct fsl_ifc_mtd
*priv
)
755 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
756 struct fsl_ifc_runtime __iomem
*ifc_runtime
= ctrl
->rregs
;
757 struct fsl_ifc_global __iomem
*ifc_global
= ctrl
->gregs
;
758 uint32_t csor
= 0, csor_8k
= 0, csor_ext
= 0;
759 uint32_t cs
= priv
->bank
;
761 /* Save CSOR and CSOR_ext */
762 csor
= ifc_in32(&ifc_global
->csor_cs
[cs
].csor
);
763 csor_ext
= ifc_in32(&ifc_global
->csor_cs
[cs
].csor_ext
);
765 /* chage PageSize 8K and SpareSize 1K*/
766 csor_8k
= (csor
& ~(CSOR_NAND_PGS_MASK
)) | 0x0018C000;
767 ifc_out32(csor_8k
, &ifc_global
->csor_cs
[cs
].csor
);
768 ifc_out32(0x0000400, &ifc_global
->csor_cs
[cs
].csor_ext
);
771 ifc_out32((IFC_FIR_OP_CW0
<< IFC_NAND_FIR0_OP0_SHIFT
) |
772 (IFC_FIR_OP_UA
<< IFC_NAND_FIR0_OP1_SHIFT
) |
773 (IFC_FIR_OP_RB
<< IFC_NAND_FIR0_OP2_SHIFT
),
774 &ifc_runtime
->ifc_nand
.nand_fir0
);
775 ifc_out32(NAND_CMD_READID
<< IFC_NAND_FCR0_CMD0_SHIFT
,
776 &ifc_runtime
->ifc_nand
.nand_fcr0
);
777 ifc_out32(0x0, &ifc_runtime
->ifc_nand
.row3
);
779 ifc_out32(0x0, &ifc_runtime
->ifc_nand
.nand_fbcr
);
781 /* Program ROW0/COL0 */
782 ifc_out32(0x0, &ifc_runtime
->ifc_nand
.row0
);
783 ifc_out32(0x0, &ifc_runtime
->ifc_nand
.col0
);
785 /* set the chip select for NAND Transaction */
786 ifc_out32(cs
<< IFC_NAND_CSEL_SHIFT
,
787 &ifc_runtime
->ifc_nand
.nand_csel
);
790 ifc_out32(IFC_NAND_SEQ_STRT_FIR_STRT
,
791 &ifc_runtime
->ifc_nand
.nandseq_strt
);
793 /* wait for command complete flag or timeout */
794 wait_event_timeout(ctrl
->nand_wait
, ctrl
->nand_stat
,
795 msecs_to_jiffies(IFC_TIMEOUT_MSECS
));
797 if (ctrl
->nand_stat
!= IFC_NAND_EVTER_STAT_OPC
)
798 printk(KERN_ERR
"fsl-ifc: Failed to Initialise SRAM\n");
800 /* Restore CSOR and CSOR_ext */
801 ifc_out32(csor
, &ifc_global
->csor_cs
[cs
].csor
);
802 ifc_out32(csor_ext
, &ifc_global
->csor_cs
[cs
].csor_ext
);
805 static int fsl_ifc_chip_init(struct fsl_ifc_mtd
*priv
)
807 struct fsl_ifc_ctrl
*ctrl
= priv
->ctrl
;
808 struct fsl_ifc_global __iomem
*ifc_global
= ctrl
->gregs
;
809 struct fsl_ifc_runtime __iomem
*ifc_runtime
= ctrl
->rregs
;
810 struct nand_chip
*chip
= &priv
->chip
;
811 struct mtd_info
*mtd
= nand_to_mtd(&priv
->chip
);
814 /* Fill in fsl_ifc_mtd structure */
815 mtd
->dev
.parent
= priv
->dev
;
816 nand_set_flash_node(chip
, priv
->dev
->of_node
);
818 /* fill in nand_chip structure */
819 /* set up function call table */
820 if ((ifc_in32(&ifc_global
->cspr_cs
[priv
->bank
].cspr
))
822 chip
->read_byte
= fsl_ifc_read_byte16
;
824 chip
->read_byte
= fsl_ifc_read_byte
;
826 chip
->write_buf
= fsl_ifc_write_buf
;
827 chip
->read_buf
= fsl_ifc_read_buf
;
828 chip
->select_chip
= fsl_ifc_select_chip
;
829 chip
->cmdfunc
= fsl_ifc_cmdfunc
;
830 chip
->waitfunc
= fsl_ifc_wait
;
832 chip
->bbt_td
= &bbt_main_descr
;
833 chip
->bbt_md
= &bbt_mirror_descr
;
835 ifc_out32(0x0, &ifc_runtime
->ifc_nand
.ncfgr
);
837 /* set up nand options */
838 chip
->bbt_options
= NAND_BBT_USE_FLASH
;
839 chip
->options
= NAND_NO_SUBPAGE_WRITE
;
841 if (ifc_in32(&ifc_global
->cspr_cs
[priv
->bank
].cspr
)
842 & CSPR_PORT_SIZE_16
) {
843 chip
->read_byte
= fsl_ifc_read_byte16
;
844 chip
->options
|= NAND_BUSWIDTH_16
;
846 chip
->read_byte
= fsl_ifc_read_byte
;
849 chip
->controller
= &ifc_nand_ctrl
->controller
;
850 nand_set_controller_data(chip
, priv
);
852 chip
->ecc
.read_page
= fsl_ifc_read_page
;
853 chip
->ecc
.write_page
= fsl_ifc_write_page
;
855 csor
= ifc_in32(&ifc_global
->csor_cs
[priv
->bank
].csor
);
857 switch (csor
& CSOR_NAND_PGS_MASK
) {
858 case CSOR_NAND_PGS_512
:
859 if (!(chip
->options
& NAND_BUSWIDTH_16
)) {
860 /* Avoid conflict with bad block marker */
861 bbt_main_descr
.offs
= 0;
862 bbt_mirror_descr
.offs
= 0;
865 priv
->bufnum_mask
= 15;
868 case CSOR_NAND_PGS_2K
:
869 priv
->bufnum_mask
= 3;
872 case CSOR_NAND_PGS_4K
:
873 priv
->bufnum_mask
= 1;
876 case CSOR_NAND_PGS_8K
:
877 priv
->bufnum_mask
= 0;
881 dev_err(priv
->dev
, "bad csor %#x: bad page size\n", csor
);
885 /* Must also set CSOR_NAND_ECC_ENC_EN if DEC_EN set */
886 if (csor
& CSOR_NAND_ECC_DEC_EN
) {
887 chip
->ecc
.mode
= NAND_ECC_HW
;
888 mtd_set_ooblayout(mtd
, &fsl_ifc_ooblayout_ops
);
890 /* Hardware generates ECC per 512 Bytes */
891 chip
->ecc
.size
= 512;
892 if ((csor
& CSOR_NAND_ECC_MODE_MASK
) == CSOR_NAND_ECC_MODE_4
) {
894 chip
->ecc
.strength
= 4;
896 chip
->ecc
.bytes
= 16;
897 chip
->ecc
.strength
= 8;
900 chip
->ecc
.mode
= NAND_ECC_SOFT
;
901 chip
->ecc
.algo
= NAND_ECC_HAMMING
;
904 if (ctrl
->version
== FSL_IFC_VERSION_1_1_0
)
905 fsl_ifc_sram_init(priv
);
908 * As IFC version 2.0.0 has 16KB of internal SRAM as compared to older
909 * versions which had 8KB. Hence bufnum mask needs to be updated.
911 if (ctrl
->version
>= FSL_IFC_VERSION_2_0_0
)
912 priv
->bufnum_mask
= (priv
->bufnum_mask
* 2) + 1;
917 static int fsl_ifc_chip_remove(struct fsl_ifc_mtd
*priv
)
919 struct mtd_info
*mtd
= nand_to_mtd(&priv
->chip
);
921 nand_release(&priv
->chip
);
926 iounmap(priv
->vbase
);
928 ifc_nand_ctrl
->chips
[priv
->bank
] = NULL
;
933 static int match_bank(struct fsl_ifc_global __iomem
*ifc_global
, int bank
,
936 u32 cspr
= ifc_in32(&ifc_global
->cspr_cs
[bank
].cspr
);
938 if (!(cspr
& CSPR_V
))
940 if ((cspr
& CSPR_MSEL
) != CSPR_MSEL_NAND
)
943 return (cspr
& CSPR_BA
) == convert_ifc_address(addr
);
946 static DEFINE_MUTEX(fsl_ifc_nand_mutex
);
948 static int fsl_ifc_nand_probe(struct platform_device
*dev
)
950 struct fsl_ifc_runtime __iomem
*ifc
;
951 struct fsl_ifc_mtd
*priv
;
953 static const char *part_probe_types
[]
954 = { "cmdlinepart", "RedBoot", "ofpart", NULL
};
957 struct device_node
*node
= dev
->dev
.of_node
;
958 struct mtd_info
*mtd
;
960 if (!fsl_ifc_ctrl_dev
|| !fsl_ifc_ctrl_dev
->rregs
)
962 ifc
= fsl_ifc_ctrl_dev
->rregs
;
964 /* get, allocate and map the memory resource */
965 ret
= of_address_to_resource(node
, 0, &res
);
967 dev_err(&dev
->dev
, "%s: failed to get resource\n", __func__
);
971 /* find which chip select it is connected to */
972 for (bank
= 0; bank
< fsl_ifc_ctrl_dev
->banks
; bank
++) {
973 if (match_bank(fsl_ifc_ctrl_dev
->gregs
, bank
, res
.start
))
977 if (bank
>= fsl_ifc_ctrl_dev
->banks
) {
978 dev_err(&dev
->dev
, "%s: address did not match any chip selects\n",
983 priv
= devm_kzalloc(&dev
->dev
, sizeof(*priv
), GFP_KERNEL
);
987 mutex_lock(&fsl_ifc_nand_mutex
);
988 if (!fsl_ifc_ctrl_dev
->nand
) {
989 ifc_nand_ctrl
= kzalloc(sizeof(*ifc_nand_ctrl
), GFP_KERNEL
);
990 if (!ifc_nand_ctrl
) {
991 mutex_unlock(&fsl_ifc_nand_mutex
);
995 ifc_nand_ctrl
->read_bytes
= 0;
996 ifc_nand_ctrl
->index
= 0;
997 ifc_nand_ctrl
->addr
= NULL
;
998 fsl_ifc_ctrl_dev
->nand
= ifc_nand_ctrl
;
1000 nand_hw_control_init(&ifc_nand_ctrl
->controller
);
1002 ifc_nand_ctrl
= fsl_ifc_ctrl_dev
->nand
;
1004 mutex_unlock(&fsl_ifc_nand_mutex
);
1006 ifc_nand_ctrl
->chips
[bank
] = priv
;
1008 priv
->ctrl
= fsl_ifc_ctrl_dev
;
1009 priv
->dev
= &dev
->dev
;
1011 priv
->vbase
= ioremap(res
.start
, resource_size(&res
));
1013 dev_err(priv
->dev
, "%s: failed to map chip region\n", __func__
);
1018 dev_set_drvdata(priv
->dev
, priv
);
1020 ifc_out32(IFC_NAND_EVTER_EN_OPC_EN
|
1021 IFC_NAND_EVTER_EN_FTOER_EN
|
1022 IFC_NAND_EVTER_EN_WPER_EN
,
1023 &ifc
->ifc_nand
.nand_evter_en
);
1025 /* enable NAND Machine Interrupts */
1026 ifc_out32(IFC_NAND_EVTER_INTR_OPCIR_EN
|
1027 IFC_NAND_EVTER_INTR_FTOERIR_EN
|
1028 IFC_NAND_EVTER_INTR_WPERIR_EN
,
1029 &ifc
->ifc_nand
.nand_evter_intr_en
);
1031 mtd
= nand_to_mtd(&priv
->chip
);
1032 mtd
->name
= kasprintf(GFP_KERNEL
, "%llx.flash", (u64
)res
.start
);
1038 ret
= fsl_ifc_chip_init(priv
);
1042 ret
= nand_scan_ident(mtd
, 1, NULL
);
1046 ret
= fsl_ifc_chip_init_tail(mtd
);
1050 ret
= nand_scan_tail(mtd
);
1054 /* First look for RedBoot table or partitions on the command
1055 * line, these take precedence over device tree information */
1056 mtd_device_parse_register(mtd
, part_probe_types
, NULL
, NULL
, 0);
1058 dev_info(priv
->dev
, "IFC NAND device at 0x%llx, bank %d\n",
1059 (unsigned long long)res
.start
, priv
->bank
);
1063 fsl_ifc_chip_remove(priv
);
1067 static int fsl_ifc_nand_remove(struct platform_device
*dev
)
1069 struct fsl_ifc_mtd
*priv
= dev_get_drvdata(&dev
->dev
);
1071 fsl_ifc_chip_remove(priv
);
1073 mutex_lock(&fsl_ifc_nand_mutex
);
1074 ifc_nand_ctrl
->counter
--;
1075 if (!ifc_nand_ctrl
->counter
) {
1076 fsl_ifc_ctrl_dev
->nand
= NULL
;
1077 kfree(ifc_nand_ctrl
);
1079 mutex_unlock(&fsl_ifc_nand_mutex
);
1084 static const struct of_device_id fsl_ifc_nand_match
[] = {
1086 .compatible
= "fsl,ifc-nand",
1090 MODULE_DEVICE_TABLE(of
, fsl_ifc_nand_match
);
1092 static struct platform_driver fsl_ifc_nand_driver
= {
1094 .name
= "fsl,ifc-nand",
1095 .of_match_table
= fsl_ifc_nand_match
,
1097 .probe
= fsl_ifc_nand_probe
,
1098 .remove
= fsl_ifc_nand_remove
,
1101 module_platform_driver(fsl_ifc_nand_driver
);
1103 MODULE_LICENSE("GPL");
1104 MODULE_AUTHOR("Freescale");
1105 MODULE_DESCRIPTION("Freescale Integrated Flash Controller MTD NAND driver");