24 #include <linux/module.h>
25 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
41 .eccpos = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
50 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
52 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
53 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 },
55 {.offset = 2, .length = 4},
56 {.offset = 16, .length = 6},
57 {.offset = 32, .length = 6},
58 {.offset = 48, .length = 6} },
61 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
67 .pattern = scan_ff_pattern,
74 .pattern = scan_ff_pattern,
77 static void empty_fifo(
struct sh_flctl *flctl)
83 static void start_translation(
struct sh_flctl *flctl)
88 static void timeout_error(
struct sh_flctl *flctl,
const char *
str)
90 dev_err(&flctl->
pdev->dev,
"Timeout occurred in %s\n", str);
93 static void wait_completion(
struct sh_flctl *flctl)
105 timeout_error(flctl, __func__);
109 static void set_addr(
struct mtd_info *mtd,
int column,
int page_addr)
111 struct sh_flctl *flctl = mtd_to_flctl(mtd);
116 }
else if (page_addr != -1) {
121 addr = column & 0x0FFF;
122 addr |= (page_addr & 0xff) << 16;
123 addr |= ((page_addr >> 8) & 0xff) << 24;
127 addr2 = (page_addr >> 16) & 0xff;
132 addr |= (page_addr & 0xff) << 8;
133 addr |= ((page_addr >> 8) & 0xff) << 16;
134 addr |= ((page_addr >> 16) & 0xff) << 24;
140 static void wait_rfifo_ready(
struct sh_flctl *flctl)
152 timeout_error(flctl, __func__);
155 static void wait_wfifo_ready(
struct sh_flctl *flctl)
166 timeout_error(flctl, __func__);
188 if ((size & 0xFF) == 4)
204 for (i = 0; i < 512; i++) {
213 "reading empty sector %d, ecc error ignored\n",
226 for (i = 0; i < 3; i++) {
230 data =
readl(ecc_reg[i]);
245 timeout_error(flctl, __func__);
249 static void wait_wecfifo_ready(
struct sh_flctl *flctl)
261 timeout_error(flctl, __func__);
267 unsigned long *
buf = (
unsigned long *)&flctl->
done_buff[offset];
269 wait_completion(flctl);
275 static void read_fiforeg(
struct sh_flctl *flctl,
int rlen,
int offset)
278 unsigned long *buf = (
unsigned long *)&flctl->
done_buff[offset];
280 len_4align = (rlen + 3) / 4;
282 for (i = 0; i < len_4align; i++) {
283 wait_rfifo_ready(flctl);
294 unsigned long *ecc_buf = (
unsigned long *)buff;
296 res = wait_recfifo_ready(flctl , sector);
299 for (i = 0; i < 4; i++) {
308 static void write_fiforeg(
struct sh_flctl *flctl,
int rlen,
int offset)
311 unsigned long *data = (
unsigned long *)&flctl->
done_buff[offset];
312 void *fifo_addr = (
void *)
FLDTFIFO(flctl);
314 len_4align = (rlen + 3) / 4;
315 for (i = 0; i < len_4align; i++) {
316 wait_wfifo_ready(flctl);
321 static void write_ec_fiforeg(
struct sh_flctl *flctl,
int rlen,
int offset)
324 unsigned long *data = (
unsigned long *)&flctl->
done_buff[offset];
326 len_4align = (rlen + 3) / 4;
327 for (i = 0; i < len_4align; i++) {
328 wait_wecfifo_ready(flctl);
335 struct sh_flctl *flctl = mtd_to_flctl(mtd);
337 uint32_t flcmdcr_val, addr_len_bytes = 0;
387 flcmdcr_val |= addr_len_bytes;
405 const uint8_t *buf,
int oob_required)
412 static void execmd_read_page_sector(
struct mtd_info *mtd,
int page_addr)
414 struct sh_flctl *flctl = mtd_to_flctl(mtd);
429 start_translation(flctl);
431 for (sector = 0; sector < page_sectors; sector++) {
432 read_fiforeg(flctl, 512, 512 * sector);
434 ecc_result = read_ecfiforeg(flctl,
438 switch (ecc_result) {
441 "applied ecc on page 0x%x", page_addr);
442 flctl->
mtd.ecc_stats.corrected++;
446 "page 0x%x contains corrupted data\n",
448 flctl->
mtd.ecc_stats.failed++;
455 wait_completion(flctl);
461 static void execmd_read_oob(
struct mtd_info *mtd,
int page_addr)
463 struct sh_flctl *flctl = mtd_to_flctl(mtd);
464 int page_sectors = flctl->
page_size ? 4 : 1;
472 for (i = 0; i < page_sectors; i++) {
473 set_addr(mtd, (512 + 16) * i + 512 , page_addr);
476 start_translation(flctl);
477 read_fiforeg(flctl, 16, 16 * i);
478 wait_completion(flctl);
482 static void execmd_write_page_sector(
struct mtd_info *mtd)
484 struct sh_flctl *flctl = mtd_to_flctl(mtd);
497 start_translation(flctl);
499 for (sector = 0; sector < page_sectors; sector++) {
500 write_fiforeg(flctl, 512, 512 * sector);
501 write_ec_fiforeg(flctl, 16, mtd->
writesize + 16 * sector);
504 wait_completion(flctl);
508 static void execmd_write_oob(
struct mtd_info *mtd)
510 struct sh_flctl *flctl = mtd_to_flctl(mtd);
519 for (sector = 0; sector < page_sectors; sector++) {
521 set_addr(mtd, sector * 528 + 512, page_addr);
524 start_translation(flctl);
525 write_fiforeg(flctl, 16, 16 * sector);
526 wait_completion(flctl);
531 int column,
int page_addr)
533 struct sh_flctl *flctl = mtd_to_flctl(mtd);
536 pm_runtime_get_sync(&flctl->
pdev->dev);
547 execmd_read_page_sector(mtd, page_addr);
554 set_cmd_regs(mtd, command, command);
556 set_addr(mtd, 0, page_addr);
561 flctl->
index += column;
562 goto read_normal_exit;
567 execmd_read_oob(mtd, page_addr);
574 set_addr(mtd, mtd->
writesize, page_addr);
576 set_cmd_regs(mtd, command, command);
577 set_addr(mtd, 0, page_addr);
580 goto read_normal_exit;
590 set_cmd_regs(mtd, command, command);
592 set_addr(mtd, column, 0);
595 goto read_normal_exit;
598 set_cmd_regs(mtd, command, command);
603 set_addr(mtd, column, 0);
608 start_translation(flctl);
610 wait_completion(flctl);
621 start_translation(flctl);
622 wait_completion(flctl);
631 }
else if (column < 256) {
648 set_addr(mtd, -1, -1);
650 start_translation(flctl);
651 wait_completion(flctl);
656 execmd_write_oob(mtd);
658 execmd_write_page_sector(mtd);
666 start_translation(flctl);
667 write_fiforeg(flctl, flctl->
index, 0);
668 wait_completion(flctl);
672 set_cmd_regs(mtd, command, command);
673 set_addr(mtd, -1, -1);
677 start_translation(flctl);
678 read_datareg(flctl, 0);
682 set_cmd_regs(mtd, command, command);
683 set_addr(mtd, -1, -1);
686 start_translation(flctl);
687 wait_completion(flctl);
698 start_translation(flctl);
700 wait_completion(flctl);
702 pm_runtime_put_sync(&flctl->
pdev->dev);
706 static void flctl_select_chip(
struct mtd_info *mtd,
int chipnr)
708 struct sh_flctl *flctl = mtd_to_flctl(mtd);
715 pm_runtime_get_sync(&flctl->
pdev->dev);
723 pm_runtime_put_sync(&flctl->
pdev->dev);
733 "PM QoS request failed: %d\n", ret);
738 pm_runtime_get_sync(&flctl->
pdev->dev);
740 pm_runtime_put_sync(&flctl->
pdev->dev);
748 static void flctl_write_buf(
struct mtd_info *mtd,
const uint8_t *buf,
int len)
750 struct sh_flctl *flctl = mtd_to_flctl(mtd);
751 int index = flctl->
index;
759 struct sh_flctl *flctl = mtd_to_flctl(mtd);
760 int index = flctl->
index;
770 struct sh_flctl *flctl = mtd_to_flctl(mtd);
771 int index = flctl->
index;
780 static void flctl_read_buf(
struct mtd_info *mtd,
uint8_t *buf,
int len)
782 struct sh_flctl *flctl = mtd_to_flctl(mtd);
783 int index = flctl->
index;
789 static int flctl_chip_init_tail(
struct mtd_info *mtd)
791 struct sh_flctl *flctl = mtd_to_flctl(mtd);
800 }
else if (chip->
chipsize > (2 << 16)) {
814 }
else if (chip->
chipsize > (8 << 16)) {
826 chip->
ecc.layout = &flctl_4secc_oob_16;
829 chip->
ecc.layout = &flctl_4secc_oob_64;
833 chip->
ecc.size = 512;
834 chip->
ecc.bytes = 10;
835 chip->
ecc.strength = 4;
836 chip->
ecc.read_page = flctl_read_page_hwecc;
837 chip->
ecc.write_page = flctl_write_page_hwecc;
869 pdata = pdev->
dev.platform_data;
871 dev_err(&pdev->
dev,
"no platform data defined\n");
877 dev_err(&pdev->
dev,
"failed to allocate driver data\n");
883 dev_err(&pdev->
dev,
"failed to get I/O memory\n");
889 dev_err(&pdev->
dev,
"failed to remap I/O memory\n");
895 dev_err(&pdev->
dev,
"failed to get flste irq data\n");
901 dev_err(&pdev->
dev,
"request interrupt failed.\n");
905 platform_set_drvdata(pdev, flctl);
906 flctl_mtd = &flctl->
mtd;
908 flctl_mtd->
priv = nand;
931 pm_runtime_resume(&pdev->
dev);
937 ret = flctl_chip_init_tail(flctl_mtd);
950 pm_runtime_disable(&pdev->
dev);
961 struct sh_flctl *flctl = platform_get_drvdata(pdev);
964 pm_runtime_disable(&pdev->
dev);
973 .remove = flctl_remove,
980 static int __init flctl_nand_init(
void)
985 static void __exit flctl_nand_cleanup(
void)