22 #include <linux/slab.h>
23 #include <linux/module.h>
42 #define LPC32XX_MODNAME "lpc32xx-nand"
48 #define SLC_DATA(x) (x + 0x000)
49 #define SLC_ADDR(x) (x + 0x004)
50 #define SLC_CMD(x) (x + 0x008)
51 #define SLC_STOP(x) (x + 0x00C)
52 #define SLC_CTRL(x) (x + 0x010)
53 #define SLC_CFG(x) (x + 0x014)
54 #define SLC_STAT(x) (x + 0x018)
55 #define SLC_INT_STAT(x) (x + 0x01C)
56 #define SLC_IEN(x) (x + 0x020)
57 #define SLC_ISR(x) (x + 0x024)
58 #define SLC_ICR(x) (x + 0x028)
59 #define SLC_TAC(x) (x + 0x02C)
60 #define SLC_TC(x) (x + 0x030)
61 #define SLC_ECC(x) (x + 0x034)
62 #define SLC_DMA_DATA(x) (x + 0x038)
67 #define SLCCTRL_SW_RESET (1 << 2)
68 #define SLCCTRL_ECC_CLEAR (1 << 1)
69 #define SLCCTRL_DMA_START (1 << 0)
74 #define SLCCFG_CE_LOW (1 << 5)
75 #define SLCCFG_DMA_ECC (1 << 4)
76 #define SLCCFG_ECC_EN (1 << 3)
77 #define SLCCFG_DMA_BURST (1 << 2)
78 #define SLCCFG_DMA_DIR (1 << 1)
79 #define SLCCFG_WIDTH (1 << 0)
84 #define SLCSTAT_DMA_FIFO (1 << 2)
85 #define SLCSTAT_SLC_FIFO (1 << 1)
86 #define SLCSTAT_NAND_READY (1 << 0)
91 #define SLCSTAT_INT_TC (1 << 1)
92 #define SLCSTAT_INT_RDY_EN (1 << 0)
98 #define SLCTAC_WDR(n) (((n) & 0xF) << 28)
100 #define SLCTAC_WWIDTH(n) (((n) & 0xF) << 24)
102 #define SLCTAC_WHOLD(n) (((n) & 0xF) << 20)
104 #define SLCTAC_WSETUP(n) (((n) & 0xF) << 16)
106 #define SLCTAC_RDR(n) (((n) & 0xF) << 12)
108 #define SLCTAC_RWIDTH(n) (((n) & 0xF) << 8)
110 #define SLCTAC_RHOLD(n) (((n) & 0xF) << 4)
112 #define SLCTAC_RSETUP(n) (((n) & 0xF) << 0)
118 #define SLCECC_TO_LINEPAR(n) (((n) >> 6) & 0x7FFF)
119 #define SLCECC_TO_COLPAR(n) ((n) & 0x3F)
126 #define LPC32XX_DMA_DATA_SIZE 4096
127 #define LPC32XX_ECC_SAVE_SIZE ((4096 / 256) * 4)
130 #define LPC32XX_SLC_DEV_ECC_BYTES 3
137 #define LPC32XX_DEF_BUS_RATE 133250000
140 #define LPC32XX_DMA_TIMEOUT 100
148 .eccpos = {10, 11, 12, 13, 14, 15},
150 { .offset = 0, .length = 4 },
151 { .offset = 6, .length = 4 },
155 static u8 bbt_pattern[] = {
'B',
'b',
't',
'0' };
156 static u8 mirror_pattern[] = {
'1',
't',
'b',
'B' };
169 .pattern = bbt_pattern
179 .pattern = mirror_pattern
256 static void lpc32xx_nand_cmd_ctrl(
struct mtd_info *mtd,
int cmd,
282 static int lpc32xx_nand_device_ready(
struct mtd_info *mtd)
299 if (gpio_is_valid(host->
ncfg->wp_gpio))
308 if (gpio_is_valid(host->
ncfg->wp_gpio))
315 static void lpc32xx_nand_ecc_enable(
struct mtd_info *mtd,
int mode)
323 static int lpc32xx_nand_ecc_calculate(
struct mtd_info *mtd,
324 const unsigned char *
buf,
361 static void lpc32xx_nand_write_buf(
struct mtd_info *mtd,
const uint8_t *buf,
int len)
374 static int lpc32xx_nand_read_oob_syndrome(
struct mtd_info *mtd,
386 static int lpc32xx_nand_write_oob_syndrome(
struct mtd_info *mtd,
409 for (i = 0; i < (count * 3); i += 3) {
411 ce = ~(ce << 2) & 0xFFFFFF;
412 spare[i + 2] = (
uint8_t)(ce & 0xFF);
414 spare[i + 1] = (
uint8_t)(ce & 0xFF);
420 static void lpc32xx_dma_complete_func(
void *
completion)
444 dev_err(mtd->
dev.parent,
"Failed to setup DMA slave\n");
453 dev_err(mtd->
dev.parent,
"Failed to map sg list\n");
456 desc = dmaengine_prep_slave_sg(host->
dma_chan, &host->
sgl, 1, dir,
459 dev_err(mtd->
dev.parent,
"Failed to prepare slave sg\n");
463 init_completion(&host->
comp);
464 desc->
callback = lpc32xx_dma_complete_func;
467 dmaengine_submit(desc);
468 dma_async_issue_pending(host->
dma_chan);
485 static int lpc32xx_xfer(
struct mtd_info *mtd,
uint8_t *buf,
int eccsubpages,
498 if ((
void *)buf <= high_memory) {
529 for (i = 0; i < chip->
ecc.steps; i++) {
532 dma_buf + i * chip->
ecc.size,
538 if (i == chip->
ecc.steps - 1)
562 dev_err(mtd->
dev.parent,
"FIFO held data too long\n");
574 dmaengine_terminate_all(host->
dma_chan);
579 dev_err(mtd->
dev.parent,
"DMA FIFO failure\n");
590 if (!dma_mapped && read)
600 static int lpc32xx_nand_read_page_syndrome(
struct mtd_info *mtd,
602 int oob_required,
int page)
612 status = lpc32xx_xfer(mtd, buf, chip->
ecc.steps, 1);
621 oobecc = chip->
oob_poi + chip->
ecc.layout->eccpos[0];
623 for (i = 0; i < chip->
ecc.steps; i++) {
624 stat = chip->
ecc.correct(mtd, buf, oobecc,
625 &tmpecc[i * chip->
ecc.bytes]);
631 buf += chip->
ecc.size;
632 oobecc += chip->
ecc.bytes;
642 static int lpc32xx_nand_read_page_raw_syndrome(
struct mtd_info *mtd,
644 uint8_t *buf,
int oob_required,
661 static int lpc32xx_nand_write_page_syndrome(
struct mtd_info *mtd,
663 const uint8_t *buf,
int oob_required)
670 error = lpc32xx_xfer(mtd, (
uint8_t *)buf, chip->
ecc.steps, 0);
689 static int lpc32xx_nand_write_page_raw_syndrome(
struct mtd_info *mtd,
705 if (!host->
pdata || !host->
pdata->dma_filter) {
706 dev_err(mtd->
dev.parent,
"no DMA platform data\n");
715 dev_err(mtd->
dev.parent,
"Failed to request DMA channel\n");
729 dev_err(dev,
"could not allocate memory for NAND config\n");
733 of_property_read_u32(np,
"nxp,wdr-clks", &ncfg->
wdr_clks);
734 of_property_read_u32(np,
"nxp,wwidth", &ncfg->
wwidth);
735 of_property_read_u32(np,
"nxp,whold", &ncfg->
whold);
736 of_property_read_u32(np,
"nxp,wsetup", &ncfg->
wsetup);
737 of_property_read_u32(np,
"nxp,rdr-clks", &ncfg->
rdr_clks);
738 of_property_read_u32(np,
"nxp,rwidth", &ncfg->
rwidth);
739 of_property_read_u32(np,
"nxp,rhold", &ncfg->
rhold);
740 of_property_read_u32(np,
"nxp,rsetup", &ncfg->
rsetup);
745 dev_err(dev,
"chip parameters not specified correctly\n");
750 ncfg->
wp_gpio = of_get_named_gpio(np,
"gpios", 0);
769 dev_err(&pdev->
dev,
"No memory resource found for device\n");
776 dev_err(&pdev->
dev,
"failed to allocate device structure\n");
787 if (pdev->
dev.of_node)
788 host->
ncfg = lpc32xx_parse_dt(&pdev->
dev);
791 "Missing or bad NAND config from device tree\n");
796 if (gpio_is_valid(host->
ncfg->wp_gpio) &&
801 lpc32xx_wp_disable(host);
803 host->
pdata = pdev->
dev.platform_data;
810 mtd->
dev.parent = &pdev->
dev;
814 if (IS_ERR(host->
clk)) {
824 chip->
cmd_ctrl = lpc32xx_nand_cmd_ctrl;
825 chip->
dev_ready = lpc32xx_nand_device_ready;
829 lpc32xx_nand_setup(host);
831 platform_set_drvdata(pdev, host);
835 chip->
read_byte = lpc32xx_nand_read_byte;
836 chip->
read_buf = lpc32xx_nand_read_buf;
837 chip->
write_buf = lpc32xx_nand_write_buf;
838 chip->
ecc.read_page_raw = lpc32xx_nand_read_page_raw_syndrome;
839 chip->
ecc.read_page = lpc32xx_nand_read_page_syndrome;
840 chip->
ecc.write_page_raw = lpc32xx_nand_write_page_raw_syndrome;
841 chip->
ecc.write_page = lpc32xx_nand_write_page_syndrome;
842 chip->
ecc.write_oob = lpc32xx_nand_write_oob_syndrome;
843 chip->
ecc.read_oob = lpc32xx_nand_read_oob_syndrome;
844 chip->
ecc.calculate = lpc32xx_nand_ecc_calculate;
846 chip->
ecc.strength = 1;
847 chip->
ecc.hwctl = lpc32xx_nand_ecc_enable;
863 dev_err(&pdev->
dev,
"Error allocating memory\n");
868 res = lpc32xx_nand_dma_setup(host);
889 chip->
ecc.layout = &lpc32xx_nand_oob_16;
892 chip->
ecc.size = 256;
894 chip->
ecc.prepad = chip->
ecc.postpad = 0;
897 if (host->
ncfg->use_bbt) {
907 chip->
bbt_td = &bbt_smallpage_main_descr;
908 chip->
bbt_md = &bbt_smallpage_mirror_descr;
921 if (host->
ncfg->use_bbt) {
924 "Error initializing default bad block tables\n");
927 mtd->
name =
"nxp_lpc3220_slc";
930 host->
ncfg->num_parts);
941 platform_set_drvdata(pdev,
NULL);
943 lpc32xx_wp_enable(host);
968 platform_set_drvdata(pdev,
NULL);
969 lpc32xx_wp_enable(host);
984 lpc32xx_nand_setup(host);
987 lpc32xx_wp_disable(host);
1003 lpc32xx_wp_enable(host);
1012 #define lpc32xx_nand_resume NULL
1013 #define lpc32xx_nand_suspend NULL
1016 static const struct of_device_id lpc32xx_nand_match[] = {
1017 { .compatible =
"nxp,lpc3220-slc" },
1023 .probe = lpc32xx_nand_probe,