35 #include <linux/module.h>
37 #include <linux/errno.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/types.h>
47 #include <linux/bitops.h>
65 .eccpos = {0, 1, 2, 3, 6, 7},
74 40, 41, 42, 43, 44, 45, 46, 47,
75 48, 49, 50, 51, 52, 53, 54, 55,
76 56, 57, 58, 59, 60, 61, 62, 63},
85 80, 81, 82, 83, 84, 85, 86, 87,
86 88, 89, 90, 91, 92, 93, 94, 95,
87 96, 97, 98, 99, 100, 101, 102, 103,
88 104, 105, 106, 107, 108, 109, 110, 111,
89 112, 113, 114, 115, 116, 117, 118, 119,
90 120, 121, 122, 123, 124, 125, 126, 127},
99 static int nand_do_write_oob(
struct mtd_info *mtd, loff_t to,
108 static int check_offs_len(
struct mtd_info *mtd,
116 pr_debug(
"%s: unaligned address\n", __func__);
122 pr_debug(
"%s: length not block aligned\n", __func__);
135 static void nand_release_device(
struct mtd_info *mtd)
195 static void nand_select_chip(
struct mtd_info *mtd,
int chipnr)
224 for (i = 0; i < len; i++)
241 for (i = 0; i < len; i++)
253 static void nand_write_buf16(
struct mtd_info *mtd,
const uint8_t *buf,
int len)
260 for (i = 0; i < len; i++)
273 static void nand_read_buf16(
struct mtd_info *mtd,
uint8_t *buf,
int len)
280 for (i = 0; i < len; i++)
292 static int nand_block_bad(
struct mtd_info *mtd, loff_t ofs,
int getchip)
294 int page, chipnr,
res = 0, i = 0;
337 nand_release_device(mtd);
357 static int nand_default_block_markbad(
struct mtd_info *mtd, loff_t ofs)
368 memset(&einfo, 0,
sizeof(einfo));
379 chip->
bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
392 ops.ooboffs &= ~0x01;
403 res = nand_do_write_oob(mtd, wr_ofs, &ops);
411 nand_release_device(mtd);
434 static int nand_check_wp(
struct mtd_info *mtd)
457 static int nand_block_checkbad(
struct mtd_info *mtd, loff_t ofs,
int getchip,
463 return chip->
block_bad(mtd, ofs, getchip);
477 static void panic_nand_wait_ready(
struct mtd_info *mtd,
unsigned long timeo)
483 for (i = 0; i < timeo; i++) {
495 unsigned long timeo =
jiffies + 2;
499 return panic_nand_wait_ready(mtd, 400);
523 int column,
int page_addr)
536 }
else if (column < 256) {
558 if (page_addr != -1) {
559 chip->
cmd_ctrl(mtd, page_addr, ctrl);
561 chip->
cmd_ctrl(mtd, page_addr >> 8, ctrl);
564 chip->
cmd_ctrl(mtd, page_addr >> 16, ctrl);
624 static void nand_command_lp(
struct mtd_info *mtd,
unsigned int command,
625 int column,
int page_addr)
639 if (column != -1 || page_addr != -1) {
649 chip->
cmd_ctrl(mtd, column >> 8, ctrl);
651 if (page_addr != -1) {
652 chip->
cmd_ctrl(mtd, page_addr, ctrl);
657 chip->
cmd_ctrl(mtd, page_addr >> 16,
743 static void panic_nand_get_device(
struct nand_chip *chip,
806 for (i = 0; i < timeo; i++) {
834 timeo += (
HZ * 400) / 1000;
836 timeo += (
HZ * 20) / 1000;
852 panic_nand_wait(mtd, chip, timeo);
883 static int __nand_unlock(
struct mtd_info *mtd, loff_t ofs,
903 pr_debug(
"%s: error status = 0x%08x\n",
925 pr_debug(
"%s: start = 0x%012llx, len = %llu\n",
926 __func__, (
unsigned long long)ofs, len);
928 if (check_offs_len(mtd, ofs, len))
932 if (ofs + len == mtd->
size)
943 if (nand_check_wp(mtd)) {
944 pr_debug(
"%s: device is write protected!\n",
950 ret = __nand_unlock(mtd, ofs, len, 0);
953 nand_release_device(mtd);
978 pr_debug(
"%s: start = 0x%012llx, len = %llu\n",
979 __func__, (
unsigned long long)ofs, len);
981 if (check_offs_len(mtd, ofs, len))
992 if (nand_check_wp(mtd)) {
993 pr_debug(
"%s: device is write protected!\n",
1005 status = chip->
waitfunc(mtd, chip);
1007 if (status & 0x01) {
1008 pr_debug(
"%s: error status = 0x%08x\n",
1014 ret = __nand_unlock(mtd, ofs, len, 0x1);
1017 nand_release_device(mtd);
1034 uint8_t *buf,
int oob_required,
int page)
1052 static int nand_read_page_raw_syndrome(
struct mtd_info *mtd,
1054 int oob_required,
int page)
1056 int eccsize = chip->
ecc.size;
1057 int eccbytes = chip->
ecc.bytes;
1061 for (steps = chip->
ecc.steps; steps > 0; steps--) {
1065 if (chip->
ecc.prepad) {
1067 oob += chip->
ecc.prepad;
1070 chip->
read_buf(mtd, oob, eccbytes);
1073 if (chip->
ecc.postpad) {
1075 oob += chip->
ecc.postpad;
1095 uint8_t *buf,
int oob_required,
int page)
1097 int i, eccsize = chip->
ecc.size;
1098 int eccbytes = chip->
ecc.bytes;
1099 int eccsteps = chip->
ecc.steps;
1104 unsigned int max_bitflips = 0;
1106 chip->
ecc.read_page_raw(mtd, chip, buf, 1, page);
1108 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1109 chip->
ecc.calculate(mtd, p, &ecc_calc[i]);
1111 for (i = 0; i < chip->
ecc.total; i++)
1112 ecc_code[i] = chip->
oob_poi[eccpos[i]];
1114 eccsteps = chip->
ecc.steps;
1117 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1120 stat = chip->
ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1125 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
1128 return max_bitflips;
1142 int start_step, end_step, num_steps;
1145 int data_col_addr,
i, gaps = 0;
1146 int datafrag_len, eccfrag_len, aligned_len, aligned_pos;
1149 unsigned int max_bitflips = 0;
1152 start_step = data_offs / chip->
ecc.size;
1153 end_step = (data_offs + readlen - 1) / chip->
ecc.size;
1154 num_steps = end_step - start_step + 1;
1157 datafrag_len = num_steps * chip->
ecc.size;
1158 eccfrag_len = num_steps * chip->
ecc.bytes;
1160 data_col_addr = start_step * chip->
ecc.size;
1162 if (data_col_addr != 0)
1165 p = bufpoi + data_col_addr;
1166 chip->
read_buf(mtd, p, datafrag_len);
1169 for (i = 0; i < eccfrag_len ; i += chip->
ecc.bytes, p += chip->
ecc.size)
1170 chip->
ecc.calculate(mtd, p, &chip->
buffers->ecccalc[i]);
1176 for (i = 0; i < eccfrag_len - 1; i++) {
1177 if (eccpos[i + start_step * chip->
ecc.bytes] + 1 !=
1178 eccpos[i + start_step * chip->
ecc.bytes + 1]) {
1191 index = start_step * chip->
ecc.bytes;
1193 aligned_pos = eccpos[
index] & ~(busw - 1);
1194 aligned_len = eccfrag_len;
1195 if (eccpos[index] & (busw - 1))
1197 if (eccpos[index + (num_steps * chip->
ecc.bytes)] & (busw - 1))
1205 for (i = 0; i < eccfrag_len; i++)
1208 p = bufpoi + data_col_addr;
1209 for (i = 0; i < eccfrag_len ; i += chip->
ecc.bytes, p += chip->
ecc.size) {
1212 stat = chip->
ecc.correct(mtd, p,
1218 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
1221 return max_bitflips;
1235 uint8_t *buf,
int oob_required,
int page)
1237 int i, eccsize = chip->
ecc.size;
1238 int eccbytes = chip->
ecc.bytes;
1239 int eccsteps = chip->
ecc.steps;
1244 unsigned int max_bitflips = 0;
1246 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1249 chip->
ecc.calculate(mtd, p, &ecc_calc[i]);
1253 for (i = 0; i < chip->
ecc.total; i++)
1254 ecc_code[i] = chip->
oob_poi[eccpos[i]];
1256 eccsteps = chip->
ecc.steps;
1259 for (i = 0 ; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1262 stat = chip->
ecc.correct(mtd, p, &ecc_code[i], &ecc_calc[i]);
1267 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
1270 return max_bitflips;
1287 static int nand_read_page_hwecc_oob_first(
struct mtd_info *mtd,
1290 int i, eccsize = chip->
ecc.size;
1291 int eccbytes = chip->
ecc.bytes;
1292 int eccsteps = chip->
ecc.steps;
1297 unsigned int max_bitflips = 0;
1304 for (i = 0; i < chip->
ecc.total; i++)
1305 ecc_code[i] = chip->
oob_poi[eccpos[i]];
1307 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1312 chip->
ecc.calculate(mtd, p, &ecc_calc[i]);
1314 stat = chip->
ecc.correct(mtd, p, &ecc_code[i],
NULL);
1319 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
1322 return max_bitflips;
1336 static int nand_read_page_syndrome(
struct mtd_info *mtd,
struct nand_chip *chip,
1337 uint8_t *buf,
int oob_required,
int page)
1339 int i, eccsize = chip->
ecc.size;
1340 int eccbytes = chip->
ecc.bytes;
1341 int eccsteps = chip->
ecc.steps;
1344 unsigned int max_bitflips = 0;
1346 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1352 if (chip->
ecc.prepad) {
1354 oob += chip->
ecc.prepad;
1358 chip->
read_buf(mtd, oob, eccbytes);
1359 stat = chip->
ecc.correct(mtd, p, oob,
NULL);
1365 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
1370 if (chip->
ecc.postpad) {
1372 oob += chip->
ecc.postpad;
1381 return max_bitflips;
1394 switch (ops->
mode) {
1406 for (; free->
length && len; free++, len -=
bytes) {
1409 if (roffs >= free->
length) {
1413 boffs = free->
offset + roffs;
1414 bytes =
min_t(
size_t, len,
1440 static int nand_do_read_ops(
struct mtd_info *mtd, loff_t
from,
1453 unsigned int max_bitflips = 0;
1467 oob_required = oob ? 1 : 0;
1474 if (realpage != chip->
pagebuf || oob) {
1475 bufpoi = aligned ? buf : chip->
buffers->databuf;
1484 ret = chip->
ecc.read_page_raw(mtd, chip, bufpoi,
1489 ret = chip->
ecc.read_subpage(mtd, chip,
1490 col, bytes, bufpoi);
1492 ret = chip->
ecc.read_page(mtd, chip, bufpoi,
1493 oob_required, page);
1501 max_bitflips =
max_t(
unsigned int, max_bitflips, ret);
1520 int toread =
min(oobreadlen, max_oobsize);
1523 oob = nand_transfer_oob(chip,
1525 oobreadlen -= toread;
1531 max_bitflips =
max_t(
unsigned int, max_bitflips,
1564 return max_bitflips;
1577 static int nand_read(
struct mtd_info *mtd, loff_t from,
size_t len,
1589 ret = nand_do_read_ops(mtd, from, &ops);
1591 nand_release_device(mtd);
1621 int chunk = chip->
ecc.bytes + chip->
ecc.prepad + chip->
ecc.postpad;
1622 int eccsize = chip->
ecc.size;
1624 int i, toread, sndrnd = 0,
pos;
1627 for (i = 0; i < chip->
ecc.steps; i++) {
1629 pos = eccsize + i * (eccsize +
chunk);
1636 toread =
min_t(
int, length, chunk);
1637 chip->
read_buf(mtd, bufpoi, toread);
1642 chip->
read_buf(mtd, bufpoi, length);
1665 status = chip->
waitfunc(mtd, chip);
1677 static int nand_write_oob_syndrome(
struct mtd_info *mtd,
1680 int chunk = chip->
ecc.bytes + chip->
ecc.prepad + chip->
ecc.postpad;
1681 int eccsize = chip->
ecc.size, length = mtd->
oobsize;
1682 int i,
len,
pos, status = 0, sndcmd = 0, steps = chip->
ecc.steps;
1690 if (!chip->
ecc.prepad && !chip->
ecc.postpad) {
1691 pos = steps * (eccsize +
chunk);
1697 for (i = 0; i <
steps; i++) {
1710 pos = eccsize + i * (eccsize +
chunk);
1715 len =
min_t(
int, length, chunk);
1724 status = chip->
waitfunc(mtd, chip);
1737 static int nand_do_read_oob(
struct mtd_info *mtd, loff_t from,
1740 int page, realpage, chipnr;
1743 int readlen = ops->
ooblen;
1748 pr_debug(
"%s: from = 0x%08Lx, len = %i\n",
1749 __func__, (
unsigned long long)from, readlen);
1754 len = chip->
ecc.layout->oobavail;
1759 pr_debug(
"%s: attempt to start read outside oob\n",
1768 pr_debug(
"%s: attempt to read beyond end of device\n",
1782 ret = chip->
ecc.read_oob_raw(mtd, chip, page);
1784 ret = chip->
ecc.read_oob(mtd, chip, page);
1789 len =
min(len, readlen);
1790 buf = nand_transfer_oob(chip, buf, ops, len);
1827 static int nand_read_oob(
struct mtd_info *mtd, loff_t from,
1837 pr_debug(
"%s: attempt to read beyond end of device\n",
1844 switch (ops->
mode) {
1855 ret = nand_do_read_oob(mtd, from, ops);
1857 ret = nand_do_read_ops(mtd, from, ops);
1860 nand_release_device(mtd);
1875 const uint8_t *buf,
int oob_required)
1893 static int nand_write_page_raw_syndrome(
struct mtd_info *mtd,
1895 const uint8_t *buf,
int oob_required)
1897 int eccsize = chip->
ecc.size;
1898 int eccbytes = chip->
ecc.bytes;
1902 for (steps = chip->
ecc.steps; steps > 0; steps--) {
1906 if (chip->
ecc.prepad) {
1908 oob += chip->
ecc.prepad;
1911 chip->
read_buf(mtd, oob, eccbytes);
1914 if (chip->
ecc.postpad) {
1916 oob += chip->
ecc.postpad;
1934 const uint8_t *buf,
int oob_required)
1936 int i, eccsize = chip->
ecc.size;
1937 int eccbytes = chip->
ecc.bytes;
1938 int eccsteps = chip->
ecc.steps;
1944 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize)
1945 chip->
ecc.calculate(mtd, p, &ecc_calc[i]);
1947 for (i = 0; i < chip->
ecc.total; i++)
1948 chip->
oob_poi[eccpos[i]] = ecc_calc[i];
1950 return chip->
ecc.write_page_raw(mtd, chip, buf, 1);
1961 const uint8_t *buf,
int oob_required)
1963 int i, eccsize = chip->
ecc.size;
1964 int eccbytes = chip->
ecc.bytes;
1965 int eccsteps = chip->
ecc.steps;
1970 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
1973 chip->
ecc.calculate(mtd, p, &ecc_calc[i]);
1976 for (i = 0; i < chip->
ecc.total; i++)
1977 chip->
oob_poi[eccpos[i]] = ecc_calc[i];
1994 static int nand_write_page_syndrome(
struct mtd_info *mtd,
1996 const uint8_t *buf,
int oob_required)
1998 int i, eccsize = chip->
ecc.size;
1999 int eccbytes = chip->
ecc.bytes;
2000 int eccsteps = chip->
ecc.steps;
2004 for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
2009 if (chip->
ecc.prepad) {
2011 oob += chip->
ecc.prepad;
2014 chip->
ecc.calculate(mtd, p, oob);
2018 if (chip->
ecc.postpad) {
2020 oob += chip->
ecc.postpad;
2043 const uint8_t *buf,
int oob_required,
int page,
2051 status = chip->
ecc.write_page_raw(mtd, chip, buf, oob_required);
2053 status = chip->
ecc.write_page(mtd, chip, buf, oob_required);
2067 status = chip->
waitfunc(mtd, chip);
2076 if (status & NAND_STATUS_FAIL)
2080 status = chip->
waitfunc(mtd, chip);
2104 switch (ops->
mode) {
2116 for (; free->
length && len; free++, len -=
bytes) {
2119 if (woffs >= free->
length) {
2123 boffs = free->
offset + woffs;
2124 bytes =
min_t(
size_t, len,
2142 #define NOTALIGNED(x) ((x & (chip->subpagesize - 1)) != 0)
2152 static int nand_do_write_ops(
struct mtd_info *mtd, loff_t to,
2155 int chipnr, realpage,
page, blockmask, column;
2166 int oob_required = oob ? 1 : 0;
2174 pr_notice(
"%s: attempt to write non page aligned data\n",
2180 subpage = column || (writelen & (mtd->
writesize - 1));
2189 if (nand_check_wp(mtd))
2207 int cached = writelen > bytes && page != blockmask;
2213 bytes =
min_t(
int, bytes - column, (
int) writelen);
2217 wbuf = chip->
buffers->databuf;
2221 size_t len =
min(oobwritelen, oobmaxlen);
2222 oob = nand_fill_oob(mtd, oob, len, ops);
2229 ret = chip->
write_page(mtd, chip, wbuf, oob_required, page,
2268 static int panic_nand_write(
struct mtd_info *mtd, loff_t to,
size_t len,
2269 size_t *retlen,
const uint8_t *buf)
2276 panic_nand_wait(mtd, chip, 400);
2279 panic_nand_get_device(chip, mtd,
FL_WRITING);
2286 ret = nand_do_write_ops(mtd, to, &ops);
2302 static int nand_write(
struct mtd_info *mtd, loff_t to,
size_t len,
2314 ret = nand_do_write_ops(mtd, to, &ops);
2316 nand_release_device(mtd);
2328 static int nand_do_write_oob(
struct mtd_info *mtd, loff_t to,
2334 pr_debug(
"%s: to = 0x%08x, len = %i\n",
2335 __func__, (
unsigned int)to, (
int)ops->
ooblen);
2338 len = chip->
ecc.layout->oobavail;
2344 pr_debug(
"%s: attempt to write past end of page\n",
2350 pr_debug(
"%s: attempt to start write outside oob\n",
2360 pr_debug(
"%s: attempt to write beyond end of device\n",
2380 if (nand_check_wp(mtd))
2390 status = chip->
ecc.write_oob_raw(mtd, chip, page & chip->
pagemask);
2392 status = chip->
ecc.write_oob(mtd, chip, page & chip->
pagemask);
2408 static int nand_write_oob(
struct mtd_info *mtd, loff_t to,
2418 pr_debug(
"%s: attempt to write beyond end of device\n",
2425 switch (ops->
mode) {
2436 ret = nand_do_write_oob(mtd, to, ops);
2438 ret = nand_do_write_ops(mtd, to, ops);
2441 nand_release_device(mtd);
2452 static void single_erase_cmd(
struct mtd_info *mtd,
int page)
2467 static void multi_erase_cmd(
struct mtd_info *mtd,
int page)
2490 #define BBT_PAGE_MASK 0xffffff3f
2505 unsigned int bbt_masked_page = 0xffffffff;
2508 pr_debug(
"%s: start = 0x%012llx, len = %llu\n",
2509 __func__, (
unsigned long long)instr->
addr,
2510 (
unsigned long long)instr->
len);
2512 if (check_offs_len(mtd, instr->
addr, instr->
len))
2529 if (nand_check_wp(mtd)) {
2530 pr_debug(
"%s: device is write protected!\n",
2552 if (nand_block_checkbad(mtd, ((loff_t) page) <<
2554 pr_warn(
"%s: attempt to erase a bad block at page 0x%08x\n",
2565 (page + pages_per_block))
2570 status = chip->
waitfunc(mtd, chip);
2576 if ((status & NAND_STATUS_FAIL) && (chip->
errstat))
2581 if (status & NAND_STATUS_FAIL) {
2582 pr_debug(
"%s: failed erase, page 0x%08x\n",
2594 if (bbt_masked_page != 0xffffffff &&
2596 rewrite_bbt[chipnr] =
2604 if (len && !(page & chip->
pagemask)) {
2613 if (bbt_masked_page != 0xffffffff &&
2615 bbt_masked_page = chip->
bbt_td->pages[chipnr] &
2626 nand_release_device(mtd);
2636 if (bbt_masked_page == 0xffffffff || ret)
2639 for (chipnr = 0; chipnr < chip->
numchips; chipnr++) {
2640 if (!rewrite_bbt[chipnr])
2643 pr_debug(
"%s: nand_update_bbt (%d:0x%0llx 0x%0x)\n",
2644 __func__, chipnr, rewrite_bbt[chipnr],
2645 chip->
bbt_td->pages[chipnr]);
2659 static void nand_sync(
struct mtd_info *mtd)
2663 pr_debug(
"%s: called\n", __func__);
2668 nand_release_device(mtd);
2676 static int nand_block_isbad(
struct mtd_info *mtd, loff_t
offs)
2678 return nand_block_checkbad(mtd, offs, 1, 0);
2686 static int nand_block_markbad(
struct mtd_info *mtd, loff_t ofs)
2691 ret = nand_block_isbad(mtd, ofs);
2719 status = chip->
waitfunc(mtd, chip);
2720 if (status & NAND_STATUS_FAIL)
2733 int addr,
uint8_t *subfeature_param)
2750 static int nand_suspend(
struct mtd_info *mtd)
2761 static void nand_resume(
struct mtd_info *mtd)
2766 nand_release_device(mtd);
2768 pr_err(
"%s called for a chip which is not in suspended state\n",
2773 static void nand_set_defaults(
struct nand_chip *chip,
int busw)
2790 chip->
read_byte = busw ? nand_read_byte16 : nand_read_byte;
2798 chip->
write_buf = busw ? nand_write_buf16 : nand_write_buf;
2800 chip->
read_buf = busw ? nand_read_buf16 : nand_read_buf;
2813 static void sanitize_string(
uint8_t *
s,
size_t len)
2821 for (i = 0; i < len - 1; i++) {
2822 if (s[i] <
' ' || s[i] > 127)
2830 static u16 onfi_crc16(
u16 crc,
u8 const *p,
size_t len)
2835 for (i = 0; i < 8; i++)
2836 crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0);
2859 for (i = 0; i < 3; i++) {
2863 pr_info(
"ONFI param page %d valid\n", i);
2875 else if (val & (1 << 4))
2877 else if (val & (1 << 3))
2879 else if (val & (1 << 2))
2881 else if (val & (1 << 1))
2887 pr_info(
"%s: unsupported ONFI version: %d\n", __func__, val);
2904 pr_info(
"ONFI flash detected\n");
2919 static int nand_id_has_period(
u8 *id_data,
int arrlen,
int period)
2922 for (i = 0; i <
period; i++)
2923 for (j = i + period; j < arrlen; j +=
period)
2924 if (id_data[i] != id_data[j])
2937 static int nand_id_len(
u8 *id_data,
int arrlen)
2939 int last_nonzero,
period;
2942 for (last_nonzero = arrlen - 1; last_nonzero >= 0; last_nonzero--)
2943 if (id_data[last_nonzero])
2947 if (last_nonzero < 0)
2951 for (period = 1; period < arrlen; period++)
2952 if (nand_id_has_period(id_data, arrlen, period))
2956 if (period < arrlen)
2960 if (last_nonzero < arrlen - 1)
2961 return last_nonzero + 1;
2973 u8 id_data[8],
int *busw)
2981 id_len = nand_id_len(id_data, 8);
2994 id_data[5] != 0x00) {
2996 mtd->
writesize = 2048 << (extid & 0x03);
2999 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3023 (((extid >> 1) & 0x04) | (extid & 0x03));
3030 mtd->
writesize = 2048 << (extid & 0x03);
3033 switch (((extid >> 2) & 0x04) | (extid & 0x03)) {
3058 tmp = ((extid >> 1) & 0x04) | (extid & 0x03);
3061 else if (tmp == 0x03)
3068 mtd->
writesize = 1024 << (extid & 0x03);
3071 mtd->
oobsize = (8 << (extid & 0x01)) *
3075 mtd->
erasesize = (64 * 1024) << (extid & 0x03);
3091 int maf_id = id_data[0];
3104 if (maf_id ==
NAND_MFR_AMD && id_data[4] != 0x00 && id_data[5] == 0x00
3105 && id_data[6] == 0x00 && id_data[7] == 0x00
3108 mtd->
erasesize <<= ((id_data[3] & 0x03) << 1);
3117 static void nand_decode_bbm_options(
struct mtd_info *mtd,
3120 int maf_id = id_data[0];
3155 int *maf_id,
int *
dev_id,
3187 for (i = 0; i < 8; i++)
3190 if (id_data[0] != *maf_id || id_data[1] != *dev_id) {
3191 pr_info(
"%s: second ID read did not match "
3192 "%02x,%02x against %02x,%02x\n", __func__,
3193 *maf_id, *dev_id, id_data[0], id_data[1]);
3201 if (*dev_id == type->
id)
3207 if (nand_flash_detect_onfi(mtd, chip, &busw))
3221 busw = chip->
init_size(mtd, chip, id_data);
3224 nand_decode_ext_id(mtd, chip, id_data, &busw);
3226 nand_decode_id(mtd, chip, type, id_data, &busw);
3240 for (maf_idx = 0;
nand_manuf_ids[maf_idx].id != 0x0; maf_idx++) {
3250 pr_info(
"NAND device: Manufacturer ID:"
3251 " 0x%02x, Chip ID: 0x%02x (%s %s)\n", *maf_id,
3253 pr_warn(
"NAND bus width %d instead %d bit\n",
3259 nand_decode_bbm_options(mtd, chip, id_data);
3285 chip->
cmdfunc = nand_command_lp;
3287 pr_info(
"NAND device: Manufacturer ID: 0x%02x, Chip ID: 0x%02x (%s %s),"
3288 " page size: %d, OOB size: %d\n",
3310 int i, busw, nand_maf_id, nand_dev_id;
3317 nand_set_defaults(chip, busw);
3320 type = nand_get_flash_type(mtd, chip, busw,
3321 &nand_maf_id, &nand_dev_id, table);
3325 pr_warn(
"No NAND device found\n");
3327 return PTR_ERR(type);
3331 for (i = 1; i < maxchips; i++) {
3338 if (nand_maf_id != chip->
read_byte(mtd) ||
3343 pr_info(
"%d NAND chips detected\n", i);
3385 chip->
ecc.layout = &nand_oob_8;
3388 chip->
ecc.layout = &nand_oob_16;
3391 chip->
ecc.layout = &nand_oob_64;
3394 chip->
ecc.layout = &nand_oob_128;
3397 pr_warn(
"No oob scheme defined for oobsize %d\n",
3417 switch (chip->
ecc.mode) {
3420 if (!chip->
ecc.calculate || !chip->
ecc.correct ||
3422 pr_warn(
"No ECC functions supplied; "
3423 "hardware ECC not possible\n");
3426 if (!chip->
ecc.read_page)
3427 chip->
ecc.read_page = nand_read_page_hwecc_oob_first;
3431 if (!chip->
ecc.read_page)
3432 chip->
ecc.read_page = nand_read_page_hwecc;
3433 if (!chip->
ecc.write_page)
3434 chip->
ecc.write_page = nand_write_page_hwecc;
3435 if (!chip->
ecc.read_page_raw)
3436 chip->
ecc.read_page_raw = nand_read_page_raw;
3437 if (!chip->
ecc.write_page_raw)
3438 chip->
ecc.write_page_raw = nand_write_page_raw;
3439 if (!chip->
ecc.read_oob)
3440 chip->
ecc.read_oob = nand_read_oob_std;
3441 if (!chip->
ecc.write_oob)
3442 chip->
ecc.write_oob = nand_write_oob_std;
3445 if ((!chip->
ecc.calculate || !chip->
ecc.correct ||
3446 !chip->
ecc.hwctl) &&
3447 (!chip->
ecc.read_page ||
3448 chip->
ecc.read_page == nand_read_page_hwecc ||
3449 !chip->
ecc.write_page ||
3450 chip->
ecc.write_page == nand_write_page_hwecc)) {
3451 pr_warn(
"No ECC functions supplied; "
3452 "hardware ECC not possible\n");
3456 if (!chip->
ecc.read_page)
3457 chip->
ecc.read_page = nand_read_page_syndrome;
3458 if (!chip->
ecc.write_page)
3459 chip->
ecc.write_page = nand_write_page_syndrome;
3460 if (!chip->
ecc.read_page_raw)
3461 chip->
ecc.read_page_raw = nand_read_page_raw_syndrome;
3462 if (!chip->
ecc.write_page_raw)
3463 chip->
ecc.write_page_raw = nand_write_page_raw_syndrome;
3464 if (!chip->
ecc.read_oob)
3465 chip->
ecc.read_oob = nand_read_oob_syndrome;
3466 if (!chip->
ecc.write_oob)
3467 chip->
ecc.write_oob = nand_write_oob_syndrome;
3470 if (!chip->
ecc.strength) {
3471 pr_warn(
"Driver must set ecc.strength when using hardware ECC\n");
3476 pr_warn(
"%d byte HW ECC not possible on "
3477 "%d byte page size, fallback to SW ECC\n",
3484 chip->
ecc.read_page = nand_read_page_swecc;
3485 chip->
ecc.read_subpage = nand_read_subpage;
3486 chip->
ecc.write_page = nand_write_page_swecc;
3487 chip->
ecc.read_page_raw = nand_read_page_raw;
3488 chip->
ecc.write_page_raw = nand_write_page_raw;
3489 chip->
ecc.read_oob = nand_read_oob_std;
3490 chip->
ecc.write_oob = nand_write_oob_std;
3491 if (!chip->
ecc.size)
3492 chip->
ecc.size = 256;
3493 chip->
ecc.bytes = 3;
3494 chip->
ecc.strength = 1;
3498 if (!mtd_nand_has_bch()) {
3499 pr_warn(
"CONFIG_MTD_ECC_BCH not enabled\n");
3502 chip->
ecc.calculate = nand_bch_calculate_ecc;
3503 chip->
ecc.correct = nand_bch_correct_data;
3504 chip->
ecc.read_page = nand_read_page_swecc;
3505 chip->
ecc.read_subpage = nand_read_subpage;
3506 chip->
ecc.write_page = nand_write_page_swecc;
3507 chip->
ecc.read_page_raw = nand_read_page_raw;
3508 chip->
ecc.write_page_raw = nand_write_page_raw;
3509 chip->
ecc.read_oob = nand_read_oob_std;
3510 chip->
ecc.write_oob = nand_write_oob_std;
3517 if (!chip->
ecc.size && (mtd->
oobsize >= 64)) {
3518 chip->
ecc.size = 512;
3519 chip->
ecc.bytes = 7;
3521 chip->
ecc.priv = nand_bch_init(mtd,
3525 if (!chip->
ecc.priv) {
3526 pr_warn(
"BCH ECC initialization failed!\n");
3529 chip->
ecc.strength =
3530 chip->
ecc.bytes * 8 / fls(8 * chip->
ecc.size);
3534 pr_warn(
"NAND_ECC_NONE selected by board driver. "
3535 "This is not recommended!\n");
3536 chip->
ecc.read_page = nand_read_page_raw;
3537 chip->
ecc.write_page = nand_write_page_raw;
3538 chip->
ecc.read_oob = nand_read_oob_std;
3539 chip->
ecc.read_page_raw = nand_read_page_raw;
3540 chip->
ecc.write_page_raw = nand_write_page_raw;
3541 chip->
ecc.write_oob = nand_write_oob_std;
3543 chip->
ecc.bytes = 0;
3544 chip->
ecc.strength = 0;
3548 pr_warn(
"Invalid NAND_ECC_MODE %d\n", chip->
ecc.mode);
3553 if (!chip->
ecc.read_oob_raw)
3554 chip->
ecc.read_oob_raw = chip->
ecc.read_oob;
3555 if (!chip->
ecc.write_oob_raw)
3556 chip->
ecc.write_oob_raw = chip->
ecc.write_oob;
3562 chip->
ecc.layout->oobavail = 0;
3563 for (i = 0; chip->
ecc.layout->oobfree[
i].length
3565 chip->
ecc.layout->oobavail +=
3566 chip->
ecc.layout->oobfree[i].length;
3575 pr_warn(
"Invalid ECC parameters\n");
3578 chip->
ecc.total = chip->
ecc.steps * chip->
ecc.bytes;
3583 switch (chip->
ecc.steps) {
3613 mtd->
_erase = nand_erase;
3616 mtd->
_read = nand_read;
3617 mtd->
_write = nand_write;
3621 mtd->
_sync = nand_sync;
3656 #define caller_is_module() (1)
3658 #define caller_is_module() \
3659 is_module_text_address((unsigned long)__builtin_return_address(0))
3678 pr_crit(
"%s called with NULL mtd->owner!\n", __func__);
3714 static int __init nand_base_init(
void)
3720 static void __exit nand_base_exit(
void)