26 #include <linux/slab.h>
27 #include <linux/module.h>
47 #define DRV_NAME "lpc32xx_mlc"
53 #define MLC_BUFF(x) (x + 0x00000)
54 #define MLC_DATA(x) (x + 0x08000)
55 #define MLC_CMD(x) (x + 0x10000)
56 #define MLC_ADDR(x) (x + 0x10004)
57 #define MLC_ECC_ENC_REG(x) (x + 0x10008)
58 #define MLC_ECC_DEC_REG(x) (x + 0x1000C)
59 #define MLC_ECC_AUTO_ENC_REG(x) (x + 0x10010)
60 #define MLC_ECC_AUTO_DEC_REG(x) (x + 0x10014)
61 #define MLC_RPR(x) (x + 0x10018)
62 #define MLC_WPR(x) (x + 0x1001C)
63 #define MLC_RUBP(x) (x + 0x10020)
64 #define MLC_ROBP(x) (x + 0x10024)
65 #define MLC_SW_WP_ADD_LOW(x) (x + 0x10028)
66 #define MLC_SW_WP_ADD_HIG(x) (x + 0x1002C)
67 #define MLC_ICR(x) (x + 0x10030)
68 #define MLC_TIME_REG(x) (x + 0x10034)
69 #define MLC_IRQ_MR(x) (x + 0x10038)
70 #define MLC_IRQ_SR(x) (x + 0x1003C)
71 #define MLC_LOCK_PR(x) (x + 0x10044)
72 #define MLC_ISR(x) (x + 0x10048)
73 #define MLC_CEH(x) (x + 0x1004C)
78 #define MLCCMD_RESET 0xFF
83 #define MLCICR_WPROT (1 << 3)
84 #define MLCICR_LARGEBLOCK (1 << 2)
85 #define MLCICR_LONGADDR (1 << 1)
86 #define MLCICR_16BIT (1 << 0)
91 #define MLCTIMEREG_TCEA_DELAY(n) (((n) & 0x03) << 24)
92 #define MLCTIMEREG_BUSY_DELAY(n) (((n) & 0x1F) << 19)
93 #define MLCTIMEREG_NAND_TA(n) (((n) & 0x07) << 16)
94 #define MLCTIMEREG_RD_HIGH(n) (((n) & 0x0F) << 12)
95 #define MLCTIMEREG_RD_LOW(n) (((n) & 0x0F) << 8)
96 #define MLCTIMEREG_WR_HIGH(n) (((n) & 0x0F) << 4)
97 #define MLCTIMEREG_WR_LOW(n) (((n) & 0x0F) << 0)
102 #define MLCIRQ_NAND_READY (1 << 5)
103 #define MLCIRQ_CONTROLLER_READY (1 << 4)
104 #define MLCIRQ_DECODE_FAILURE (1 << 3)
105 #define MLCIRQ_DECODE_ERROR (1 << 2)
106 #define MLCIRQ_ECC_READY (1 << 1)
107 #define MLCIRQ_WRPROT_FAULT (1 << 0)
112 #define MLCLOCKPR_MAGIC 0xA25E
117 #define MLCISR_DECODER_FAILURE (1 << 6)
118 #define MLCISR_ERRORS ((1 << 4) | (1 << 5))
119 #define MLCISR_ERRORS_DETECTED (1 << 3)
120 #define MLCISR_ECC_READY (1 << 2)
121 #define MLCISR_CONTROLLER_READY (1 << 1)
122 #define MLCISR_NAND_READY (1 << 0)
127 #define MLCCEH_NORMAL (1 << 0)
144 .eccpos = { 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
145 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
146 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
147 54, 55, 56, 57, 58, 59, 60, 61, 62, 63 },
163 .pages = { 524224, 0, 0, 0, 0, 0, 0, 0 },
169 .pages = { 524160, 0, 0, 0, 0, 0, 0, 0 },
275 static void lpc32xx_nand_cmd_ctrl(
struct mtd_info *mtd,
int cmd,
292 static int lpc32xx_nand_device_ready(
struct mtd_info *mtd)
294 struct nand_chip *nand_chip = mtd->
priv;
319 static int lpc32xx_waitfunc_nand(
struct mtd_info *mtd,
struct nand_chip *
chip)
330 dev_dbg(&mtd->
dev,
"Warning: NAND not ready.\n");
338 static int lpc32xx_waitfunc_controller(
struct mtd_info *mtd,
339 struct nand_chip *chip)
350 dev_dbg(&mtd->
dev,
"Warning: Controller not ready.\n");
358 static int lpc32xx_waitfunc(
struct mtd_info *mtd,
struct nand_chip *chip)
360 lpc32xx_waitfunc_nand(mtd, chip);
361 lpc32xx_waitfunc_controller(mtd, chip);
371 if (gpio_is_valid(host->
ncfg->wp_gpio))
380 if (gpio_is_valid(host->
ncfg->wp_gpio))
384 static void lpc32xx_dma_complete_func(
void *
completion)
389 static int lpc32xx_xmit_dma(
struct mtd_info *mtd,
void *
mem,
int len,
392 struct nand_chip *chip = mtd->
priv;
403 dev_err(mtd->
dev.parent,
"Failed to map sg list\n");
406 desc = dmaengine_prep_slave_sg(host->
dma_chan, &host->
sgl, 1, dir,
409 dev_err(mtd->
dev.parent,
"Failed to prepare slave sg\n");
414 desc->
callback = lpc32xx_dma_complete_func;
417 dmaengine_submit(desc);
418 dma_async_issue_pending(host->
dma_chan);
431 static int lpc32xx_read_page(
struct mtd_info *mtd,
struct nand_chip *chip,
442 if ((
void *)buf <= high_memory) {
459 lpc32xx_waitfunc_controller(mtd, chip);
465 dev_warn(&mtd->
dev,
"%s: DECODER_FAILURE\n", __func__);
467 mtd->
ecc_stats.corrected += ((mlc_isr >> 4) & 0x3) + 1;
472 res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
477 for (j = 0; j < (512 >> 2); j++) {
483 for (j = 0; j < (16 >> 2); j++) {
496 static int lpc32xx_write_page_lowlevel(
struct mtd_info *mtd,
497 struct nand_chip *chip,
498 const uint8_t *buf,
int oob_required)
517 res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
522 for (j = 0; j < (512 >> 2); j++) {
537 lpc32xx_waitfunc_controller(mtd, chip);
542 static int lpc32xx_write_page(
struct mtd_info *mtd,
struct nand_chip *chip,
543 const uint8_t *buf,
int oob_required,
int page,
549 res = lpc32xx_write_page_lowlevel(mtd, chip, buf, oob_required);
551 lpc32xx_waitfunc(mtd, chip);
556 static int lpc32xx_read_oob(
struct mtd_info *mtd,
struct nand_chip *chip,
562 lpc32xx_read_page(mtd, chip, host->
dummy_buf, 1, page);
567 static int lpc32xx_write_oob(
struct mtd_info *mtd,
struct nand_chip *chip,
575 static void lpc32xx_ecc_enable(
struct mtd_info *mtd,
int mode)
585 if (!host->
pdata || !host->
pdata->dma_filter) {
586 dev_err(mtd->
dev.parent,
"no DMA platform data\n");
595 dev_err(mtd->
dev.parent,
"Failed to request DMA channel\n");
614 dev_err(mtd->
dev.parent,
"Failed to setup DMA slave\n");
631 dev_err(dev,
"could not allocate memory for platform data\n");
635 of_property_read_u32(np,
"nxp,tcea-delay", &ncfg->
tcea_delay);
636 of_property_read_u32(np,
"nxp,busy-delay", &ncfg->
busy_delay);
637 of_property_read_u32(np,
"nxp,nand-ta", &ncfg->
nand_ta);
638 of_property_read_u32(np,
"nxp,rd-high", &ncfg->
rd_high);
639 of_property_read_u32(np,
"nxp,rd-low", &ncfg->
rd_low);
640 of_property_read_u32(np,
"nxp,wr-high", &ncfg->
wr_high);
641 of_property_read_u32(np,
"nxp,wr-low", &ncfg->
wr_low);
646 dev_err(dev,
"chip parameters not specified correctly\n");
650 ncfg->
wp_gpio = of_get_named_gpio(np,
"gpios", 0);
662 struct nand_chip *nand_chip;
670 dev_err(&pdev->
dev,
"failed to allocate device structure.\n");
676 dev_err(&pdev->
dev,
"No memory resource found for device!\r\n");
689 if (pdev->
dev.of_node)
690 host->
ncfg = lpc32xx_parse_dt(&pdev->
dev);
693 "Missing or bad NAND config from device tree\n");
698 if (gpio_is_valid(host->
ncfg->wp_gpio) &&
703 lpc32xx_wp_disable(host);
705 host->
pdata = pdev->
dev.platform_data;
707 nand_chip->
priv = host;
708 mtd->
priv = nand_chip;
710 mtd->
dev.parent = &pdev->
dev;
714 if (IS_ERR(host->
clk)) {
715 dev_err(&pdev->
dev,
"Clock initialization failure\n");
721 nand_chip->
cmd_ctrl = lpc32xx_nand_cmd_ctrl;
722 nand_chip->
dev_ready = lpc32xx_nand_device_ready;
728 lpc32xx_nand_setup(host);
730 platform_set_drvdata(pdev, host);
733 nand_chip->
ecc.hwctl = lpc32xx_ecc_enable;
734 nand_chip->
ecc.read_page_raw = lpc32xx_read_page;
735 nand_chip->
ecc.read_page = lpc32xx_read_page;
736 nand_chip->
ecc.write_page_raw = lpc32xx_write_page_lowlevel;
737 nand_chip->
ecc.write_page = lpc32xx_write_page_lowlevel;
738 nand_chip->
ecc.write_oob = lpc32xx_write_oob;
739 nand_chip->
ecc.read_oob = lpc32xx_read_oob;
740 nand_chip->
ecc.strength = 4;
742 nand_chip->
waitfunc = lpc32xx_waitfunc;
745 nand_chip->
bbt_td = &lpc32xx_nand_bbt;
746 nand_chip->
bbt_md = &lpc32xx_nand_bbt_mirror;
755 res = lpc32xx_dma_setup(host);
773 dev_err(&pdev->
dev,
"Error allocating dma_buf memory\n");
780 dev_err(&pdev->
dev,
"Error allocating dummy_buf memory\n");
787 nand_chip->
ecc.layout = &lpc32xx_nand_oob;
798 dev_err(&pdev->
dev,
"failed to get platform irq\n");
805 dev_err(&pdev->
dev,
"Error requesting NAND IRQ\n");
823 host->
ncfg->num_parts);
837 platform_set_drvdata(pdev,
NULL);
839 lpc32xx_wp_enable(host);
860 platform_set_drvdata(pdev,
NULL);
862 lpc32xx_wp_enable(host);
877 lpc32xx_nand_setup(host);
880 lpc32xx_wp_disable(host);
890 lpc32xx_wp_enable(host);
898 #define lpc32xx_nand_resume NULL
899 #define lpc32xx_nand_suspend NULL
902 static const struct of_device_id lpc32xx_nand_match[] = {
903 { .compatible =
"nxp,lpc3220-mlc" },
909 .probe = lpc32xx_nand_probe,