31 #include <linux/kernel.h>
32 #include <linux/slab.h>
34 #include <linux/string.h>
35 #include <linux/sched.h>
37 #include <linux/module.h>
38 #include <linux/export.h>
41 #include <linux/bitops.h>
54 static bool ignore_badblocks;
82 #define DOC_IOSPACE_DATA 0x0800
85 #define DOC_CHIPID 0x1000
86 #define DOC_DEVICESELECT 0x100a
87 #define DOC_ASICMODE 0x100c
88 #define DOC_DATAEND 0x101e
89 #define DOC_NOP 0x103e
91 #define DOC_FLASHSEQUENCE 0x1032
92 #define DOC_FLASHCOMMAND 0x1034
93 #define DOC_FLASHADDRESS 0x1036
94 #define DOC_FLASHCONTROL 0x1038
95 #define DOC_ECCCONF0 0x1040
96 #define DOC_ECCCONF1 0x1042
97 #define DOC_HAMMINGPARITY 0x1046
98 #define DOC_BCH_SYNDROM(idx) (0x1048 + idx)
100 #define DOC_ASICMODECONFIRM 0x1072
101 #define DOC_CHIPID_INV 0x1074
102 #define DOC_POWERMODE 0x107c
104 #define DOCG4_MYSTERY_REG 0x1050
107 #define DOCG4_OOB_6_7 0x1052
110 #define DOC_SEQ_RESET 0x00
111 #define DOCG4_SEQ_PAGE_READ 0x03
112 #define DOCG4_SEQ_FLUSH 0x29
113 #define DOCG4_SEQ_PAGEWRITE 0x16
114 #define DOCG4_SEQ_PAGEPROG 0x1e
115 #define DOCG4_SEQ_BLOCKERASE 0x24
118 #define DOCG4_CMD_PAGE_READ 0x00
119 #define DOC_CMD_ERASECYCLE2 0xd0
120 #define DOCG4_CMD_FLUSH 0x70
121 #define DOCG4_CMD_READ2 0x30
122 #define DOC_CMD_PROG_BLOCK_ADDR 0x60
123 #define DOCG4_CMD_PAGEWRITE 0x80
124 #define DOC_CMD_PROG_CYCLE2 0x10
125 #define DOC_CMD_RESET 0xff
128 #define DOC_POWERDOWN_READY 0x80
131 #define DOC_CTRL_CE 0x10
132 #define DOC_CTRL_UNKNOWN 0x40
133 #define DOC_CTRL_FLASHREADY 0x01
136 #define DOC_ECCCONF0_READ_MODE 0x8000
137 #define DOC_ECCCONF0_UNKNOWN 0x2000
138 #define DOC_ECCCONF0_ECC_ENABLE 0x1000
139 #define DOC_ECCCONF0_DATA_BYTES_MASK 0x07ff
142 #define DOC_ECCCONF1_BCH_SYNDROM_ERR 0x80
143 #define DOC_ECCCONF1_ECC_ENABLE 0x07
144 #define DOC_ECCCONF1_PAGE_IS_WRITTEN 0x20
147 #define DOC_ASICMODE_RESET 0x00
148 #define DOC_ASICMODE_NORMAL 0x01
149 #define DOC_ASICMODE_POWERDOWN 0x02
150 #define DOC_ASICMODE_MDWREN 0x04
151 #define DOC_ASICMODE_BDETCT_RESET 0x08
152 #define DOC_ASICMODE_RSTIN_RESET 0x10
153 #define DOC_ASICMODE_RAM_WE 0x20
156 #define DOCG4_PROGSTATUS_GOOD 0x51
157 #define DOCG4_PROGSTATUS_GOOD_2 0xe0
164 #define DOCG4_READ_ERROR 0x02
167 #define DOCG4_CHIP_SIZE 0x8000000
168 #define DOCG4_PAGE_SIZE 0x200
169 #define DOCG4_PAGES_PER_BLOCK 0x200
170 #define DOCG4_BLOCK_SIZE (DOCG4_PAGES_PER_BLOCK * DOCG4_PAGE_SIZE)
171 #define DOCG4_NUMBLOCKS (DOCG4_CHIP_SIZE / DOCG4_BLOCK_SIZE)
172 #define DOCG4_OOB_SIZE 0x10
173 #define DOCG4_CHIP_SHIFT 27
174 #define DOCG4_PAGE_SHIFT 9
175 #define DOCG4_ERASE_SHIFT 18
178 #define DOCG4_BCH_SIZE (DOCG4_PAGE_SIZE + DOCG4_OOB_SIZE - 1)
180 #define DOCG4_USERDATA_LEN 520
183 #define DOCG4_IDREG1_VALUE 0x0400
184 #define DOCG4_IDREG2_VALUE 0xfbff
187 #define DOCG4_PRIMITIVE_POLY 0x4443
192 #define DOCG4_FACTORY_BBT_PAGE 16
201 .eccpos = {7, 8, 9, 10, 11, 12, 13, 14, 15},
203 .oobfree = { {0, 7} }
211 static inline void write_nop(
void __iomem *docptr)
223 for (i = 0; i < len; i++)
227 static void docg4_write_buf16(
struct mtd_info *mtd,
const uint8_t *buf,
int len)
234 for (i = 0; i < len; i++)
238 static int poll_status(
struct docg4_priv *doc)
263 dev_err(doc->
dev,
"%s: timed out!\n", __func__);
268 dev_warn(doc->
dev,
"%s: nearly timed out; %d remaining\n",
289 status |= poll_status(doc);
293 static void docg4_select_chip(
struct mtd_info *mtd,
int chip)
303 dev_dbg(doc->
dev,
"%s: chip %d\n", __func__, chip);
309 dev_warn(doc->
dev,
"multiple floors currently unsupported\n");
343 for (i = 0; i < 7; i++) {
359 int i, numerrs, errpos[4];
360 const uint8_t blank_read_hwecc[8] = {
361 0xcf, 0x72, 0xfc, 0x1b, 0xa9, 0xc7, 0xb9, 0 };
363 read_hw_ecc(docptr, doc->
ecc_buf);
370 if (ignore_badblocks ==
false) {
382 int bit, numsetbits = 0;
383 unsigned long written_flag = nand->
oob_poi[15];
386 if (numsetbits > 4) {
388 "error(s) in blank page "
403 for (i = 0; i < 7; i++)
410 dev_warn(doc->
dev,
"uncorrectable errors at offset %08x\n",
418 for (i = 0; i < numerrs; i++)
419 errpos[i] = (errpos[i] & ~7)|(7-(errpos[
i] & 7));
422 for (i = 0; i < numerrs; i++) {
431 (
unsigned long *)nand->
oob_poi);
437 dev_notice(doc->
dev,
"%d error(s) corrected at offset %08x\n",
472 dev_warn(doc->
dev,
"unexpectd call to read_byte()\n");
491 static int read_progstatus(
struct docg4_priv *doc)
505 dev_dbg(doc->
dev,
"docg4: %s: %02x %02x %02x\n",
506 __func__, status1, status2, status3);
513 "%02x, %02x, %02x\n", status1, status2, status3);
519 static int pageprog(
struct mtd_info *mtd)
550 retval = read_progstatus(doc);
559 static void sequence_reset(
struct mtd_info *mtd)
585 "docg4: %s: g4 page %08x\n", __func__, docg4_addr);
593 write_addr(doc, docg4_addr);
612 "docg4: %s: g4 addr: %x\n", __func__, docg4_addr);
617 write_addr(doc, docg4_addr);
654 int g4_page = page / 4;
655 int g4_index = (page % 4) * 0x108 + column/2;
656 return (g4_page << 16) | g4_index;
659 static void docg4_command(
struct mtd_info *mtd,
unsigned command,
int column,
666 uint32_t g4_addr = mtd_to_docg4_address(page_addr, column);
668 dev_dbg(doc->
dev,
"%s %x, page_addr=%x, column=%x\n",
669 __func__, command, page_addr, column);
686 read_page_prologue(mtd, g4_addr);
694 write_page_prologue(mtd, g4_addr);
711 "unexpected nand command 0x%x\n", command);
718 uint8_t *buf,
int page,
bool use_ecc)
723 int bits_corrected = 0;
725 dev_dbg(doc->
dev,
"%s: page %08x\n", __func__, page);
742 "docg4_read_page: bad status: 0x%02x\n", status);
747 dev_dbg(doc->
dev,
"%s: status = 0x%x\n", __func__, status);
753 docg4_read_buf(mtd, nand->
oob_poi, 14);
761 if (
likely(use_ecc ==
true)) {
766 dev_dbg(doc->
dev,
"%s: edc_err = 0x%02x\n", __func__, edc_err);
770 bits_corrected = correct_data(mtd, buf, page);
771 if (bits_corrected == -
EBADMSG)
774 mtd->
ecc_stats.corrected += bits_corrected;
779 if (bits_corrected == -
EBADMSG)
781 return bits_corrected;
786 uint8_t *buf,
int oob_required,
int page)
788 return read_page(mtd, nand, buf, page,
false);
792 uint8_t *buf,
int oob_required,
int page)
794 return read_page(mtd, nand, buf, page,
true);
804 dev_dbg(doc->
dev,
"%s: page %x\n", __func__, page);
817 if (status & DOCG4_READ_ERROR) {
819 "docg4_read_oob failed: status = 0x%02x\n", status);
823 dev_dbg(doc->
dev,
"%s: status = 0x%x\n", __func__, status);
825 docg4_read_buf(mtd, nand->
oob_poi, 16);
836 static void docg4_erase_block(
struct mtd_info *mtd,
int page)
843 dev_dbg(doc->
dev,
"%s: page %04x\n", __func__, page);
874 read_progstatus(doc);
883 const uint8_t *buf,
bool use_ecc)
901 docg4_write_buf16(mtd, nand->
oob_poi, 6);
910 if (
likely(use_ecc ==
true)) {
918 read_hw_ecc(docptr, ecc_buf);
929 docg4_write_buf16(mtd, ecc_buf, 8);
939 const uint8_t *buf,
int oob_required)
941 return write_page(mtd, nand, buf,
false);
945 const uint8_t *buf,
int oob_required)
947 return write_page(mtd, nand, buf,
true);
988 read_page_prologue(mtd, g4_addr);
1010 unsigned long bits = ~buf[
i];
1012 int badblock = block + 7 - bitnum;
1013 nand->
bbt[badblock / 4] |=
1014 0x03 << ((badblock % 4) * 2);
1016 dev_notice(doc->
dev,
"factory-marked bad block: %d\n",
1025 static int docg4_block_markbad(
struct mtd_info *mtd, loff_t ofs)
1043 uint32_t g4_addr = mtd_to_docg4_address(page, 0);
1045 dev_dbg(doc->
dev,
"%s: %08llx\n", __func__, ofs);
1048 dev_warn(doc->
dev,
"%s: ofs %llx not start of block!\n",
1057 nand->
bbt[block / 4] |= 0x01 << ((block & 0x03) * 2);
1061 for (i = 0; i < bbtd->
len; i++)
1065 write_page_prologue(mtd, g4_addr);
1066 docg4_write_page(mtd, nand, buf, 1);
1067 ret = pageprog(mtd);
1076 static int docg4_block_neverbad(
struct mtd_info *mtd, loff_t ofs,
int getchip)
1093 struct docg4_priv *doc = platform_get_drvdata(pdev);
1099 for (i = 0; i < 10; i++) {
1108 "timeout polling DOC_POWERDOWN_READY\n");
1130 struct docg4_priv *doc = platform_get_drvdata(pdev);
1136 for (i = 0; i < 12; i++)
1137 readb(docptr + 0x1fff);
1161 mtd->
name =
"Msys_Diskonchip_G4";
1173 nand->
ecc.layout = &docg4_oobinfo;
1176 nand->
ecc.prepad = 8;
1177 nand->
ecc.bytes = 8;
1186 nand->
cmdfunc = docg4_command;
1195 nand->
ecc.read_page = docg4_read_page;
1196 nand->
ecc.write_page = docg4_write_page;
1197 nand->
ecc.read_page_raw = docg4_read_page_raw;
1198 nand->
ecc.write_page_raw = docg4_write_page_raw;
1199 nand->
ecc.read_oob = docg4_read_oob;
1200 nand->
ecc.write_oob = docg4_write_oob;
1208 if (ignore_badblocks) {
1230 "NAND device: 128MiB Diskonchip G4 detected\n");
1237 static char const *part_probes[] = {
"cmdlinepart",
"saftlpart",
NULL };
1251 dev_err(dev,
"no io memory resource defined!\n");
1257 dev_err(dev,
"Diskonchip ioremap failed: %pR\n", r);
1276 init_mtd_structs(mtd);
1285 platform_set_drvdata(pdev, doc);
1288 retval = read_id_reg(mtd);
1290 dev_warn(dev,
"No diskonchip G4 device found.\n");
1298 retval = read_factory_bbt(mtd);
1316 platform_set_drvdata(pdev,
NULL);
1326 struct docg4_priv *doc = platform_get_drvdata(pdev);
1328 platform_set_drvdata(pdev,
NULL);
1340 .suspend = docg4_suspend,
1341 .resume = docg4_resume,
1345 static int __init docg4_init(
void)
1350 static void __exit docg4_exit(
void)