23 #include <linux/module.h>
24 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
34 #define FSL_IFC_V1_1_0 0x01010000
37 #define IFC_TIMEOUT_MSECS 500
75 .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
76 .oobfree = { {0, 5}, {6, 2} },
82 .eccpos = {8, 9, 10, 11, 12, 13, 14, 15},
83 .oobfree = { {2, 6}, },
90 8, 9, 10, 11, 12, 13, 14, 15,
91 16, 17, 18, 19, 20, 21, 22, 23,
92 24, 25, 26, 27, 28, 29, 30, 31,
93 32, 33, 34, 35, 36, 37, 38, 39,
95 .oobfree = { {2, 6}, {40, 24} },
102 8, 9, 10, 11, 12, 13, 14, 15,
103 16, 17, 18, 19, 20, 21, 22, 23,
104 24, 25, 26, 27, 28, 29, 30, 31,
105 32, 33, 34, 35, 36, 37, 38, 39,
106 40, 41, 42, 43, 44, 45, 46, 47,
107 48, 49, 50, 51, 52, 53, 54, 55,
108 56, 57, 58, 59, 60, 61, 62, 63,
109 64, 65, 66, 67, 68, 69, 70, 71,
111 .oobfree = { {2, 6}, {72, 56} },
118 8, 9, 10, 11, 12, 13, 14, 15,
119 16, 17, 18, 19, 20, 21, 22, 23,
120 24, 25, 26, 27, 28, 29, 30, 31,
121 32, 33, 34, 35, 36, 37, 38, 39,
122 40, 41, 42, 43, 44, 45, 46, 47,
123 48, 49, 50, 51, 52, 53, 54, 55,
124 56, 57, 58, 59, 60, 61, 62, 63,
125 64, 65, 66, 67, 68, 69, 70, 71,
126 72, 73, 74, 75, 76, 77, 78, 79,
127 80, 81, 82, 83, 84, 85, 86, 87,
128 88, 89, 90, 91, 92, 93, 94, 95,
129 96, 97, 98, 99, 100, 101, 102, 103,
130 104, 105, 106, 107, 108, 109, 110, 111,
131 112, 113, 114, 115, 116, 117, 118, 119,
132 120, 121, 122, 123, 124, 125, 126, 127,
133 128, 129, 130, 131, 132, 133, 134, 135,
135 .oobfree = { {2, 6}, {136, 82} },
142 static u8 bbt_pattern[] = {
'B',
'b',
't',
'0' };
143 static u8 mirror_pattern[] = {
'1',
't',
'b',
'B' };
152 .pattern = bbt_pattern,
162 .pattern = mirror_pattern,
169 static void set_addr(
struct mtd_info *mtd,
int column,
int page_addr,
int oob)
177 ifc_nand_ctrl->
page = page_addr;
185 ifc_nand_ctrl->
index = column;
192 static int is_blank(
struct mtd_info *mtd,
unsigned int bufnum)
201 for (i = 0; i < mtd->
writesize / 4; i++) {
206 for (i = 0; i < chip->
ecc.layout->eccbytes; i++) {
207 int pos = chip->
ecc.layout->eccpos[
i];
218 u32 *eccstat,
unsigned int bufnum)
220 u32 reg = eccstat[bufnum / 4];
223 errors = (reg >> ((3 - bufnum % 4) * 8)) & 15;
231 static void fsl_ifc_run_command(
struct mtd_info *mtd)
245 "%s: fir0=%08x fcr0=%08x\n",
261 dev_err(priv->
dev,
"Controller is not responding\n");
263 dev_err(priv->
dev,
"NAND Flash Timeout Error\n");
265 dev_err(priv->
dev,
"NAND Flash Write Protect Error\n");
273 int sector_end = sector + chip->
ecc.steps - 1;
275 for (i = sector / 4; i <= sector_end / 4; i++)
278 for (i = sector; i <= sector_end; i++) {
279 errors = check_read_ecc(mtd, ctrl, eccstat, i);
290 if (!is_blank(mtd, bufnum))
306 static void fsl_ifc_do_read(
struct nand_chip *chip,
345 static void fsl_ifc_cmdfunc(
struct mtd_info *mtd,
unsigned int command,
346 int column,
int page_addr) {
355 ifc_nand_ctrl->
index = 0;
361 set_addr(mtd, 0, page_addr, 0);
364 ifc_nand_ctrl->
index += column;
369 fsl_ifc_do_read(chip, 0, mtd);
370 fsl_ifc_run_command(mtd);
376 set_addr(mtd, column, page_addr, 1);
380 fsl_ifc_do_read(chip, 1, mtd);
381 fsl_ifc_run_command(mtd);
406 set_addr(mtd, 0, 0, 0);
407 fsl_ifc_run_command(mtd);
413 set_addr(mtd, 0, page_addr, 0);
429 fsl_ifc_run_command(mtd);
435 ifc_nand_ctrl->
column = column;
436 ifc_nand_ctrl->
oob = 0;
475 ifc_nand_ctrl->
oob = 1;
478 set_addr(mtd, column, page_addr, ifc_nand_ctrl->
oob);
484 if (ifc_nand_ctrl->
oob) {
491 fsl_ifc_run_command(mtd);
502 set_addr(mtd, 0, 0, 0);
505 fsl_ifc_run_command(mtd);
519 fsl_ifc_run_command(mtd);
523 dev_err(priv->
dev,
"%s: error, unsupported command 0x%x.\n",
528 static void fsl_ifc_select_chip(
struct mtd_info *mtd,
int chip)
538 static void fsl_ifc_write_buf(
struct mtd_info *mtd,
const u8 *
buf,
int len)
545 dev_err(priv->
dev,
"%s: len %d bytes", __func__, len);
549 if ((
unsigned int)len > bufsize - ifc_nand_ctrl->
index) {
551 "%s: beyond end of buffer (%d requested, %u available)\n",
552 __func__, len, bufsize - ifc_nand_ctrl->
index);
553 len = bufsize - ifc_nand_ctrl->
index;
557 ifc_nand_ctrl->
index += len;
576 dev_err(priv->
dev,
"%s: beyond end of buffer\n", __func__);
596 addr[ifc_nand_ctrl->
index]);
597 ifc_nand_ctrl->
index += 2;
601 dev_err(priv->
dev,
"%s: beyond end of buffer\n", __func__);
608 static void fsl_ifc_read_buf(
struct mtd_info *mtd,
u8 *buf,
int len)
615 dev_err(priv->
dev,
"%s: len %d bytes", __func__, len);
619 avail =
min((
unsigned int)len,
626 "%s: beyond end of buffer (%d requested, %d available)\n",
627 __func__, len, avail);
648 set_addr(mtd, 0, 0, 0);
651 fsl_ifc_run_command(mtd);
669 fsl_ifc_read_buf(mtd, buf, mtd->
writesize);
674 dev_err(priv->
dev,
"NAND Flash ECC Uncorrectable Error\n");
686 const uint8_t *buf,
int oob_required)
688 fsl_ifc_write_buf(mtd, buf, mtd->
writesize);
694 static int fsl_ifc_chip_init_tail(
struct mtd_info *mtd)
699 dev_dbg(priv->
dev,
"%s: nand->numchips = %d\n", __func__,
701 dev_dbg(priv->
dev,
"%s: nand->chipsize = %lld\n", __func__,
703 dev_dbg(priv->
dev,
"%s: nand->pagemask = %8x\n", __func__,
705 dev_dbg(priv->
dev,
"%s: nand->chip_delay = %d\n", __func__,
707 dev_dbg(priv->
dev,
"%s: nand->badblockpos = %d\n", __func__,
709 dev_dbg(priv->
dev,
"%s: nand->chip_shift = %d\n", __func__,
711 dev_dbg(priv->
dev,
"%s: nand->page_shift = %d\n", __func__,
713 dev_dbg(priv->
dev,
"%s: nand->phys_erase_shift = %d\n", __func__,
715 dev_dbg(priv->
dev,
"%s: nand->ecclayout = %p\n", __func__,
717 dev_dbg(priv->
dev,
"%s: nand->ecc.mode = %d\n", __func__,
719 dev_dbg(priv->
dev,
"%s: nand->ecc.steps = %d\n", __func__,
721 dev_dbg(priv->
dev,
"%s: nand->ecc.bytes = %d\n", __func__,
723 dev_dbg(priv->
dev,
"%s: nand->ecc.total = %d\n", __func__,
725 dev_dbg(priv->
dev,
"%s: nand->ecc.layout = %p\n", __func__,
728 dev_dbg(priv->
dev,
"%s: mtd->size = %lld\n", __func__, mtd->
size);
729 dev_dbg(priv->
dev,
"%s: mtd->erasesize = %d\n", __func__,
731 dev_dbg(priv->
dev,
"%s: mtd->writesize = %d\n", __func__,
733 dev_dbg(priv->
dev,
"%s: mtd->oobsize = %d\n", __func__,
739 static void fsl_ifc_sram_init(
struct fsl_ifc_mtd *priv)
788 static int fsl_ifc_chip_init(
struct fsl_ifc_mtd *priv)
810 chip->
cmdfunc = fsl_ifc_cmdfunc;
813 chip->
bbt_td = &bbt_main_descr;
814 chip->
bbt_md = &bbt_mirror_descr;
832 chip->
ecc.read_page = fsl_ifc_read_page;
833 chip->
ecc.write_page = fsl_ifc_write_page;
838 chip->
ecc.size = 512;
840 chip->
ecc.strength = 4;
845 layout = &oob_512_16bit_ecc4;
847 layout = &oob_512_8bit_ecc4;
850 bbt_main_descr.
offs = 0;
851 bbt_mirror_descr.
offs = 0;
858 layout = &oob_2048_ecc4;
865 layout = &oob_4096_ecc4;
867 layout = &oob_4096_ecc8;
868 chip->
ecc.bytes = 16;
875 dev_err(priv->
dev,
"bad csor %#x: bad page size\n", csor);
889 fsl_ifc_sram_init(priv);
894 static int fsl_ifc_chip_remove(
struct fsl_ifc_mtd *priv)
930 static const char *part_probe_types[]
931 = {
"cmdlinepart",
"RedBoot",
"ofpart",
NULL };
945 dev_err(&dev->
dev,
"%s: failed to get resource\n", __func__);
951 if (match_bank(ifc, bank,
res.start))
955 if (bank >= FSL_IFC_BANK_COUNT) {
956 dev_err(&dev->
dev,
"%s: address did not match any chip selects\n",
967 ifc_nand_ctrl = kzalloc(
sizeof(*ifc_nand_ctrl),
GFP_KERNEL);
968 if (!ifc_nand_ctrl) {
969 dev_err(&dev->
dev,
"failed to allocate memory\n");
975 ifc_nand_ctrl->
index = 0;
993 dev_err(priv->
dev,
"%s: failed to map chip region\n", __func__);
1012 if (!priv->
mtd.name) {
1017 ret = fsl_ifc_chip_init(priv);
1025 ret = fsl_ifc_chip_init_tail(&priv->
mtd);
1038 dev_info(priv->
dev,
"IFC NAND device at 0x%llx, bank %d\n",
1039 (
unsigned long long)
res.start, priv->
bank);
1043 fsl_ifc_chip_remove(priv);
1051 fsl_ifc_chip_remove(priv);
1055 if (!ifc_nand_ctrl->
counter) {
1057 kfree(ifc_nand_ctrl);
1064 static const struct of_device_id fsl_ifc_nand_match[] = {
1066 .compatible =
"fsl,ifc-nand",
1073 .name =
"fsl,ifc-nand",
1075 .of_match_table = fsl_ifc_nand_match,
1077 .probe = fsl_ifc_nand_probe,
1078 .remove = fsl_ifc_nand_remove,
1081 static int __init fsl_ifc_nand_init(
void)
1093 static void __exit fsl_ifc_nand_exit(
void)