28 #include <linux/slab.h>
29 #include <linux/module.h>
50 static int on_flash_bbt = 0;
54 #define ecc_readl(add, reg) \
55 __raw_readl(add + ATMEL_ECC_##reg)
56 #define ecc_writel(add, reg, value) \
57 __raw_writel((value), add + ATMEL_ECC_##reg)
68 .eccpos = {60, 61, 62, 63},
81 .eccpos = {0, 1, 2, 3},
128 static int cpu_has_dma(
void)
138 if (gpio_is_valid(host->
board.enable_pin))
147 if (gpio_is_valid(host->
board.enable_pin))
154 static void atmel_nand_cmd_ctrl(
struct mtd_info *mtd,
int cmd,
unsigned int ctrl)
161 atmel_nand_enable(host);
163 atmel_nand_disable(host);
177 static int atmel_nand_device_ready(
struct mtd_info *mtd)
179 struct nand_chip *nand_chip = mtd->
priv;
183 !!host->
board.rdy_pin_active_low;
189 static void atmel_read_buf8(
struct mtd_info *mtd,
u8 *
buf,
int len)
191 struct nand_chip *nand_chip = mtd->
priv;
196 static void atmel_read_buf16(
struct mtd_info *mtd,
u8 *
buf,
int len)
198 struct nand_chip *nand_chip = mtd->
priv;
203 static void atmel_write_buf8(
struct mtd_info *mtd,
const u8 *
buf,
int len)
205 struct nand_chip *nand_chip = mtd->
priv;
210 static void atmel_write_buf16(
struct mtd_info *mtd,
const u8 *
buf,
int len)
212 struct nand_chip *nand_chip = mtd->
priv;
217 static void dma_complete_func(
void *
completion)
222 static int atmel_nand_dma_op(
struct mtd_info *mtd,
void *
buf,
int len,
236 if (buf >= high_memory)
246 dev_err(host->
dev,
"Failed to dma_map_single\n");
259 dma_src_addr, len, flags);
261 dev_err(host->
dev,
"Failed to prepare DMA memcpy\n");
265 init_completion(&host->
comp);
271 dev_err(host->
dev,
"Failed to do DMA tx_submit\n");
275 dma_async_issue_pending(host->
dma_chan);
288 static void atmel_read_buf(
struct mtd_info *mtd,
u8 *buf,
int len)
290 struct nand_chip *chip = mtd->
priv;
295 if (atmel_nand_dma_op(mtd, buf, len, 1) == 0)
298 if (host->
board.bus_width_16)
299 atmel_read_buf16(mtd, buf, len);
301 atmel_read_buf8(mtd, buf, len);
304 static void atmel_write_buf(
struct mtd_info *mtd,
const u8 *buf,
int len)
306 struct nand_chip *chip = mtd->
priv;
311 if (atmel_nand_dma_op(mtd, (
void *)buf, len, 0) == 0)
314 if (host->
board.bus_width_16)
315 atmel_write_buf16(mtd, buf, len);
317 atmel_write_buf8(mtd, buf, len);
335 int m = 12 + sector_size / 512;
336 return (m * cap + 7) / 8;
340 int oobsize,
int ecc_len)
347 for (i = 0; i < ecc_len; i++)
348 layout->
eccpos[i] = oobsize - ecc_len + i;
352 oobsize - ecc_len - layout->
oobfree[0].offset;
401 pmecc_data_free(host);
407 struct nand_chip *nand_chip = mtd->
priv;
422 static void pmecc_substitute(
struct mtd_info *mtd)
424 struct nand_chip *nand_chip = mtd->
priv;
442 for (i = 1; i < 2 *
cap; i += 2) {
444 if (partial_syn[i] & ((
unsigned short)0x1 << j))
449 for (i = 2, j = 1; j <=
cap; i = ++j << 1) {
464 static void pmecc_get_sigma(
struct mtd_info *mtd)
466 struct nand_chip *nand_chip = mtd->
priv;
476 const int num = 2 * cap + 1;
502 delta[0] = (mu[0] * 2 - lmu[0]) >> 1;
518 delta[1] = (mu[1] * 2 - lmu[1]) >> 1;
523 for (i = 1; i <=
cap; i++) {
530 tmp = ((cap - (lmu[
i] >> 1) - 1) / 2);
531 if ((cap - (lmu[i] >> 1) - 1) & 0x1)
536 if (dmu_0_count == tmp) {
537 for (j = 0; j <= (lmu[i] >> 1) + 1; j++)
538 smu[(cap + 1) * num +
j] =
541 lmu[cap + 1] = lmu[
i];
546 for (j = 0; j <= lmu[i] >> 1; j++)
547 smu[(i + 1) * num +
j] = smu[i * num +
j];
555 for (j = 0; j <
i; j++) {
556 if ((dmu[j]) && (delta[
j] > largest)) {
563 diff = (mu[
i] - mu[
ro]);
566 if ((lmu[i] >> 1) > ((lmu[
ro] >> 1) + diff))
569 lmu[i + 1] = ((lmu[
ro] >> 1) + diff) * 2;
572 for (k = 0; k < num; k++)
573 smu[(i + 1) * num +
k] = 0;
576 for (k = 0; k <= lmu[ro] >> 1; k++) {
579 if (!(smu[ro * num + k] && dmu[i]))
584 tmp = a + (cw_len -
b) + c;
586 smu[(i + 1) * num + (k + diff)] =
a;
589 for (k = 0; k <= lmu[i] >> 1; k++)
590 smu[(i + 1) * num +
k] ^= smu[i * num +
k];
595 delta[i + 1] = (mu[i + 1] * 2 - lmu[i + 1]) >> 1;
601 for (k = 0; k <= (lmu[i + 1] >> 1); k++) {
604 dmu[i + 1] = si[tmp + 3];
605 }
else if (smu[(i + 1) * num +
k] && si[tmp + 3 -
k]) {
608 smu[(i + 1) * num + k]);
609 b = si[2 * (i - 1) + 3 - k];
622 static int pmecc_err_location(
struct mtd_info *mtd)
624 struct nand_chip *nand_chip = mtd->
priv;
626 unsigned long end_time;
628 const int num = 2 * cap + 1;
638 for (i = 0; i <= host->
pmecc_lmu[cap + 1] >> 1; i++) {
640 smu[(cap + 1) * num + i]);
644 val = (err_nbr - 1) << 16;
645 if (sector_size == 1024)
656 dev_err(host->
dev,
"PMECC: Timeout to calculate error location.\n");
665 if (roots_nbr == host->
pmecc_lmu[cap + 1] >> 1)
674 int sector_num,
int extra_bytes,
int err_nbr)
676 struct nand_chip *nand_chip = mtd->
priv;
690 if (byte_pos >= (sector_size + extra_bytes))
693 if (byte_pos < sector_size) {
694 err_byte = *(buf + byte_pos);
695 *(buf + byte_pos) ^= (1 << bit_pos);
698 dev_info(host->
dev,
"Bit flip in data area, byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
699 pos, bit_pos, err_byte, *(buf + byte_pos));
705 ecc[
tmp] ^= (1 << bit_pos);
707 pos = tmp + nand_chip->
ecc.layout->eccpos[0];
708 dev_info(host->
dev,
"Bit flip in OOB, oob_byte_pos: %d, bit_pos: %d, 0x%02x -> 0x%02x\n",
709 pos, bit_pos, err_byte, ecc[tmp]);
722 struct nand_chip *nand_chip = mtd->
priv;
724 int i, err_nbr, eccbytes;
727 eccbytes = nand_chip->
ecc.bytes;
728 for (i = 0; i < eccbytes; i++)
737 if (pmecc_stat & 0x1) {
740 pmecc_gen_syndrome(mtd, i);
741 pmecc_substitute(mtd);
742 pmecc_get_sigma(mtd);
744 err_nbr = pmecc_err_location(mtd);
746 dev_err(host->
dev,
"PMECC: Too many errors\n");
750 pmecc_correct_data(mtd, buf_pos, ecc, i,
761 static int atmel_nand_pmecc_read_page(
struct mtd_info *mtd,
762 struct nand_chip *chip,
uint8_t *buf,
int oob_required,
int page)
765 int eccsize = chip->
ecc.size;
769 unsigned long end_time;
785 dev_err(host->
dev,
"PMECC: Timeout to get error status.\n");
793 if (pmecc_correction(mtd, stat, buf, &oob[eccpos[0]]) != 0)
799 static int atmel_nand_pmecc_write_page(
struct mtd_info *mtd,
800 struct nand_chip *chip,
const uint8_t *buf,
int oob_required)
805 unsigned long end_time;
821 dev_err(host->
dev,
"PMECC: Timeout to get ECC value.\n");
841 static void atmel_pmecc_core_init(
struct mtd_info *mtd)
843 struct nand_chip *nand_chip = mtd->
priv;
893 ecc_layout = nand_chip->
ecc.layout;
908 struct nand_chip *nand_chip = &host->
nand_chip;
914 dev_info(host->
dev,
"Initialize PMECC params, cap: %d, sector: %d\n",
920 "Can't get I/O resource regs for PMECC controller, rolling back on software ECC\n");
929 goto err_pmecc_ioremap;
934 if (regs_pmerr && regs_rom) {
936 resource_size(regs_pmerr));
938 resource_size(regs_rom));
943 "Can not get I/O resource for PMECC ERRLOC controller or ROM!\n");
945 goto err_pmloc_ioremap;
963 nand_chip->
ecc.steps = 1;
964 nand_chip->
ecc.strength =
cap;
967 if (nand_chip->
ecc.bytes > mtd->
oobsize - 2) {
968 dev_err(host->
dev,
"No room for ECC bytes\n");
970 goto err_no_ecc_room;
972 pmecc_config_ecc_layout(&atmel_pmecc_oobinfo,
974 nand_chip->
ecc.bytes);
975 nand_chip->
ecc.layout = &atmel_pmecc_oobinfo;
982 "Unsupported page size for PMECC, use Software ECC\n");
991 err_no = pmecc_data_alloc(host);
994 "Cannot allocate memory for PMECC computation!\n");
995 goto err_pmecc_data_alloc;
998 nand_chip->
ecc.read_page = atmel_nand_pmecc_read_page;
999 nand_chip->
ecc.write_page = atmel_nand_pmecc_write_page;
1001 atmel_pmecc_core_init(mtd);
1005 err_pmecc_data_alloc:
1026 static int atmel_nand_calculate(
struct mtd_info *mtd,
1027 const u_char *
dat,
unsigned char *ecc_code)
1029 struct nand_chip *nand_chip = mtd->
priv;
1031 unsigned int ecc_value;
1036 ecc_code[0] = ecc_value & 0xFF;
1037 ecc_code[1] = (ecc_value >> 8) & 0xFF;
1042 ecc_code[2] = ecc_value & 0xFF;
1043 ecc_code[3] = (ecc_value >> 8) & 0xFF;
1056 static int atmel_nand_read_page(
struct mtd_info *mtd,
struct nand_chip *chip,
1057 uint8_t *buf,
int oob_required,
int page)
1059 int eccsize = chip->
ecc.size;
1060 int eccbytes = chip->
ecc.bytes;
1066 unsigned int max_bitflips = 0;
1085 if (eccpos[0] != 0) {
1097 ecc_pos = oob + eccpos[0];
1098 chip->
read_buf(mtd, ecc_pos, eccbytes);
1101 stat = chip->
ecc.correct(mtd, p, oob,
NULL);
1107 max_bitflips =
max_t(
unsigned int, max_bitflips, stat);
1116 return max_bitflips;
1134 struct nand_chip *nand_chip = mtd->
priv;
1136 unsigned int ecc_status;
1137 unsigned int ecc_word, ecc_bit;
1164 dev_dbg(host->
dev,
"atmel_nand : multiple errors detected."
1165 " Unable to correct.\n");
1174 dev_dbg(host->
dev,
"atmel_nand : one bit error on ECC code."
1175 " Nothing to correct\n");
1179 dev_dbg(host->
dev,
"atmel_nand : one bit error on data."
1180 " (word offset in the page :"
1181 " 0x%x bit offset : 0x%x)\n",
1186 ((
unsigned short *) dat)[ecc_word] ^= (1 << ecc_bit);
1189 dat[ecc_word] ^= (1 << ecc_bit);
1191 dev_dbg(host->
dev,
"atmel_nand : error corrected\n");
1198 static void atmel_nand_hwctl(
struct mtd_info *mtd,
int mode)
1201 struct nand_chip *nand_chip = mtd->
priv;
1207 #if defined(CONFIG_OF)
1217 if (of_property_read_u32(np,
"atmel,nand-addr-offset", &val) == 0) {
1219 dev_err(host->
dev,
"invalid addr-offset %u\n", val);
1225 if (of_property_read_u32(np,
"atmel,nand-cmd-offset", &val) == 0) {
1227 dev_err(host->
dev,
"invalid cmd-offset %u\n", val);
1242 board->
rdy_pin = of_get_gpio_flags(np, 0, &flags);
1246 board->
det_pin = of_get_gpio(np, 2);
1248 host->
has_pmecc = of_property_read_bool(np,
"atmel,has-pmecc");
1256 if (of_property_read_u32(np,
"atmel,pmecc-cap", &val) != 0) {
1257 dev_err(host->
dev,
"Cannot decide PMECC Capability\n");
1259 }
else if ((val != 2) && (val != 4) && (val != 8) && (val != 12) &&
1262 "Unsupported PMECC correction capability: %d; should be 2, 4, 8, 12 or 24\n",
1268 if (of_property_read_u32(np,
"atmel,pmecc-sector-size", &val) != 0) {
1269 dev_err(host->
dev,
"Cannot decide PMECC Sector Size\n");
1271 }
else if ((val != 512) && (val != 1024)) {
1273 "Unsupported PMECC sector size: %d; should be 512 or 1024 bytes\n",
1281 dev_err(host->
dev,
"Cannot get PMECC lookup table offset\n");
1286 if (!table_offset) {
1287 dev_err(host->
dev,
"Invalid PMECC lookup table offset\n");
1306 struct nand_chip *nand_chip = &host->
nand_chip;
1312 "Can't get I/O resource regs, use software ECC\n");
1329 nand_chip->
ecc.layout = &atmel_oobinfo_small;
1333 nand_chip->
ecc.layout = &atmel_oobinfo_large;
1337 nand_chip->
ecc.layout = &atmel_oobinfo_large;
1341 nand_chip->
ecc.layout = &atmel_oobinfo_large;
1352 nand_chip->
ecc.calculate = atmel_nand_calculate;
1353 nand_chip->
ecc.correct = atmel_nand_correct;
1354 nand_chip->
ecc.hwctl = atmel_nand_hwctl;
1355 nand_chip->
ecc.read_page = atmel_nand_read_page;
1356 nand_chip->
ecc.bytes = 4;
1357 nand_chip->
ecc.strength = 1;
1369 struct nand_chip *nand_chip;
1383 printk(
KERN_ERR "atmel_nand: failed to allocate device structure.\n");
1393 goto err_nand_ioremap;
1399 if (pdev->
dev.of_node) {
1400 res = atmel_of_init_port(host, pdev->
dev.of_node);
1402 goto err_ecc_ioremap;
1408 nand_chip->
priv = host;
1409 mtd->
priv = nand_chip;
1415 nand_chip->
cmd_ctrl = atmel_nand_cmd_ctrl;
1417 if (gpio_is_valid(host->
board.rdy_pin)) {
1421 "can't request rdy gpio %d\n",
1422 host->
board.rdy_pin);
1423 goto err_ecc_ioremap;
1429 "can't request input direction rdy gpio %d\n",
1430 host->
board.rdy_pin);
1431 goto err_ecc_ioremap;
1434 nand_chip->
dev_ready = atmel_nand_device_ready;
1437 if (gpio_is_valid(host->
board.enable_pin)) {
1441 "can't request enable gpio %d\n",
1442 host->
board.enable_pin);
1443 goto err_ecc_ioremap;
1449 "can't request output direction enable gpio %d\n",
1450 host->
board.enable_pin);
1451 goto err_ecc_ioremap;
1455 nand_chip->
ecc.mode = host->
board.ecc_mode;
1458 if (host->
board.bus_width_16)
1461 nand_chip->
read_buf = atmel_read_buf;
1464 platform_set_drvdata(pdev, host);
1465 atmel_nand_enable(host);
1467 if (gpio_is_valid(host->
board.det_pin)) {
1471 "can't request det gpio %d\n",
1472 host->
board.det_pin);
1479 "can't request input direction det gpio %d\n",
1480 host->
board.det_pin);
1491 if (host->
board.on_flash_bbt || on_flash_bbt) {
1506 dev_err(host->
dev,
"Failed to request DMA channel\n");
1511 dev_info(host->
dev,
"Using %s for DMA transfers.\n",
1514 dev_info(host->
dev,
"No DMA support for NAND access.\n");
1519 goto err_scan_ident;
1524 res = atmel_pmecc_nand_init_params(pdev, host);
1526 res = atmel_hw_nand_init_params(pdev, host);
1538 mtd->
name =
"atmel_nand";
1548 pmecc_data_free(host);
1559 atmel_nand_disable(host);
1560 platform_set_drvdata(pdev,
NULL);
1580 atmel_nand_disable(host);
1586 pmecc_data_free(host);
1589 if (gpio_is_valid(host->
board.det_pin))
1592 if (gpio_is_valid(host->
board.enable_pin))
1595 if (gpio_is_valid(host->
board.rdy_pin))
1614 #if defined(CONFIG_OF)
1615 static const struct of_device_id atmel_nand_dt_ids[] = {
1624 .remove =
__exit_p(atmel_nand_remove),
1626 .name =
"atmel_nand",
1632 static int __init atmel_nand_init(
void)
1638 static void __exit atmel_nand_exit(
void)