0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/slab.h>
0017 #include <linux/module.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/mtd/mtd.h>
0020 #include <linux/mtd/rawnand.h>
0021 #include <linux/mtd/partitions.h>
0022 #include <linux/clk.h>
0023 #include <linux/err.h>
0024 #include <linux/delay.h>
0025 #include <linux/completion.h>
0026 #include <linux/interrupt.h>
0027 #include <linux/of.h>
0028 #include <linux/of_gpio.h>
0029 #include <linux/mtd/lpc32xx_mlc.h>
0030 #include <linux/io.h>
0031 #include <linux/mm.h>
0032 #include <linux/dma-mapping.h>
0033 #include <linux/dmaengine.h>
0034
0035 #define DRV_NAME "lpc32xx_mlc"
0036
0037
0038
0039
0040
0041 #define MLC_BUFF(x) (x + 0x00000)
0042 #define MLC_DATA(x) (x + 0x08000)
0043 #define MLC_CMD(x) (x + 0x10000)
0044 #define MLC_ADDR(x) (x + 0x10004)
0045 #define MLC_ECC_ENC_REG(x) (x + 0x10008)
0046 #define MLC_ECC_DEC_REG(x) (x + 0x1000C)
0047 #define MLC_ECC_AUTO_ENC_REG(x) (x + 0x10010)
0048 #define MLC_ECC_AUTO_DEC_REG(x) (x + 0x10014)
0049 #define MLC_RPR(x) (x + 0x10018)
0050 #define MLC_WPR(x) (x + 0x1001C)
0051 #define MLC_RUBP(x) (x + 0x10020)
0052 #define MLC_ROBP(x) (x + 0x10024)
0053 #define MLC_SW_WP_ADD_LOW(x) (x + 0x10028)
0054 #define MLC_SW_WP_ADD_HIG(x) (x + 0x1002C)
0055 #define MLC_ICR(x) (x + 0x10030)
0056 #define MLC_TIME_REG(x) (x + 0x10034)
0057 #define MLC_IRQ_MR(x) (x + 0x10038)
0058 #define MLC_IRQ_SR(x) (x + 0x1003C)
0059 #define MLC_LOCK_PR(x) (x + 0x10044)
0060 #define MLC_ISR(x) (x + 0x10048)
0061 #define MLC_CEH(x) (x + 0x1004C)
0062
0063
0064
0065
0066 #define MLCCMD_RESET 0xFF
0067
0068
0069
0070
0071 #define MLCICR_WPROT (1 << 3)
0072 #define MLCICR_LARGEBLOCK (1 << 2)
0073 #define MLCICR_LONGADDR (1 << 1)
0074 #define MLCICR_16BIT (1 << 0)
0075
0076
0077
0078
0079 #define MLCTIMEREG_TCEA_DELAY(n) (((n) & 0x03) << 24)
0080 #define MLCTIMEREG_BUSY_DELAY(n) (((n) & 0x1F) << 19)
0081 #define MLCTIMEREG_NAND_TA(n) (((n) & 0x07) << 16)
0082 #define MLCTIMEREG_RD_HIGH(n) (((n) & 0x0F) << 12)
0083 #define MLCTIMEREG_RD_LOW(n) (((n) & 0x0F) << 8)
0084 #define MLCTIMEREG_WR_HIGH(n) (((n) & 0x0F) << 4)
0085 #define MLCTIMEREG_WR_LOW(n) (((n) & 0x0F) << 0)
0086
0087
0088
0089
0090 #define MLCIRQ_NAND_READY (1 << 5)
0091 #define MLCIRQ_CONTROLLER_READY (1 << 4)
0092 #define MLCIRQ_DECODE_FAILURE (1 << 3)
0093 #define MLCIRQ_DECODE_ERROR (1 << 2)
0094 #define MLCIRQ_ECC_READY (1 << 1)
0095 #define MLCIRQ_WRPROT_FAULT (1 << 0)
0096
0097
0098
0099
0100 #define MLCLOCKPR_MAGIC 0xA25E
0101
0102
0103
0104
0105 #define MLCISR_DECODER_FAILURE (1 << 6)
0106 #define MLCISR_ERRORS ((1 << 4) | (1 << 5))
0107 #define MLCISR_ERRORS_DETECTED (1 << 3)
0108 #define MLCISR_ECC_READY (1 << 2)
0109 #define MLCISR_CONTROLLER_READY (1 << 1)
0110 #define MLCISR_NAND_READY (1 << 0)
0111
0112
0113
0114
0115 #define MLCCEH_NORMAL (1 << 0)
0116
0117 struct lpc32xx_nand_cfg_mlc {
0118 uint32_t tcea_delay;
0119 uint32_t busy_delay;
0120 uint32_t nand_ta;
0121 uint32_t rd_high;
0122 uint32_t rd_low;
0123 uint32_t wr_high;
0124 uint32_t wr_low;
0125 int wp_gpio;
0126 struct mtd_partition *parts;
0127 unsigned num_parts;
0128 };
0129
0130 static int lpc32xx_ooblayout_ecc(struct mtd_info *mtd, int section,
0131 struct mtd_oob_region *oobregion)
0132 {
0133 struct nand_chip *nand_chip = mtd_to_nand(mtd);
0134
0135 if (section >= nand_chip->ecc.steps)
0136 return -ERANGE;
0137
0138 oobregion->offset = ((section + 1) * 16) - nand_chip->ecc.bytes;
0139 oobregion->length = nand_chip->ecc.bytes;
0140
0141 return 0;
0142 }
0143
0144 static int lpc32xx_ooblayout_free(struct mtd_info *mtd, int section,
0145 struct mtd_oob_region *oobregion)
0146 {
0147 struct nand_chip *nand_chip = mtd_to_nand(mtd);
0148
0149 if (section >= nand_chip->ecc.steps)
0150 return -ERANGE;
0151
0152 oobregion->offset = 16 * section;
0153 oobregion->length = 16 - nand_chip->ecc.bytes;
0154
0155 return 0;
0156 }
0157
0158 static const struct mtd_ooblayout_ops lpc32xx_ooblayout_ops = {
0159 .ecc = lpc32xx_ooblayout_ecc,
0160 .free = lpc32xx_ooblayout_free,
0161 };
0162
0163 static struct nand_bbt_descr lpc32xx_nand_bbt = {
0164 .options = NAND_BBT_ABSPAGE | NAND_BBT_2BIT | NAND_BBT_NO_OOB |
0165 NAND_BBT_WRITE,
0166 .pages = { 524224, 0, 0, 0, 0, 0, 0, 0 },
0167 };
0168
0169 static struct nand_bbt_descr lpc32xx_nand_bbt_mirror = {
0170 .options = NAND_BBT_ABSPAGE | NAND_BBT_2BIT | NAND_BBT_NO_OOB |
0171 NAND_BBT_WRITE,
0172 .pages = { 524160, 0, 0, 0, 0, 0, 0, 0 },
0173 };
0174
0175 struct lpc32xx_nand_host {
0176 struct platform_device *pdev;
0177 struct nand_chip nand_chip;
0178 struct lpc32xx_mlc_platform_data *pdata;
0179 struct clk *clk;
0180 void __iomem *io_base;
0181 int irq;
0182 struct lpc32xx_nand_cfg_mlc *ncfg;
0183 struct completion comp_nand;
0184 struct completion comp_controller;
0185 uint32_t llptr;
0186
0187
0188
0189 dma_addr_t oob_buf_phy;
0190
0191
0192
0193 uint8_t *oob_buf;
0194
0195 dma_addr_t io_base_phy;
0196
0197 struct completion comp_dma;
0198 struct dma_chan *dma_chan;
0199 struct dma_slave_config dma_slave_config;
0200 struct scatterlist sgl;
0201 uint8_t *dma_buf;
0202 uint8_t *dummy_buf;
0203 int mlcsubpages;
0204 };
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 static int use_dma;
0230
0231 static void lpc32xx_nand_setup(struct lpc32xx_nand_host *host)
0232 {
0233 uint32_t clkrate, tmp;
0234
0235
0236 writel(MLCCMD_RESET, MLC_CMD(host->io_base));
0237 udelay(1000);
0238
0239
0240 clkrate = clk_get_rate(host->clk);
0241 if (clkrate == 0)
0242 clkrate = 104000000;
0243
0244
0245
0246 writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
0247
0248
0249 tmp = MLCICR_LARGEBLOCK | MLCICR_LONGADDR;
0250 writel(tmp, MLC_ICR(host->io_base));
0251
0252
0253
0254 writew(MLCLOCKPR_MAGIC, MLC_LOCK_PR(host->io_base));
0255
0256
0257 tmp = 0;
0258 tmp |= MLCTIMEREG_TCEA_DELAY(clkrate / host->ncfg->tcea_delay + 1);
0259 tmp |= MLCTIMEREG_BUSY_DELAY(clkrate / host->ncfg->busy_delay + 1);
0260 tmp |= MLCTIMEREG_NAND_TA(clkrate / host->ncfg->nand_ta + 1);
0261 tmp |= MLCTIMEREG_RD_HIGH(clkrate / host->ncfg->rd_high + 1);
0262 tmp |= MLCTIMEREG_RD_LOW(clkrate / host->ncfg->rd_low);
0263 tmp |= MLCTIMEREG_WR_HIGH(clkrate / host->ncfg->wr_high + 1);
0264 tmp |= MLCTIMEREG_WR_LOW(clkrate / host->ncfg->wr_low);
0265 writel(tmp, MLC_TIME_REG(host->io_base));
0266
0267
0268 writeb(MLCIRQ_CONTROLLER_READY | MLCIRQ_NAND_READY,
0269 MLC_IRQ_MR(host->io_base));
0270
0271
0272 writel(MLCCEH_NORMAL, MLC_CEH(host->io_base));
0273 }
0274
0275
0276
0277
0278 static void lpc32xx_nand_cmd_ctrl(struct nand_chip *nand_chip, int cmd,
0279 unsigned int ctrl)
0280 {
0281 struct lpc32xx_nand_host *host = nand_get_controller_data(nand_chip);
0282
0283 if (cmd != NAND_CMD_NONE) {
0284 if (ctrl & NAND_CLE)
0285 writel(cmd, MLC_CMD(host->io_base));
0286 else
0287 writel(cmd, MLC_ADDR(host->io_base));
0288 }
0289 }
0290
0291
0292
0293
0294 static int lpc32xx_nand_device_ready(struct nand_chip *nand_chip)
0295 {
0296 struct lpc32xx_nand_host *host = nand_get_controller_data(nand_chip);
0297
0298 if ((readb(MLC_ISR(host->io_base)) &
0299 (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY)) ==
0300 (MLCISR_CONTROLLER_READY | MLCISR_NAND_READY))
0301 return 1;
0302
0303 return 0;
0304 }
0305
0306 static irqreturn_t lpc3xxx_nand_irq(int irq, struct lpc32xx_nand_host *host)
0307 {
0308 uint8_t sr;
0309
0310
0311 sr = readb(MLC_IRQ_SR(host->io_base));
0312 if (sr & MLCIRQ_NAND_READY)
0313 complete(&host->comp_nand);
0314 if (sr & MLCIRQ_CONTROLLER_READY)
0315 complete(&host->comp_controller);
0316
0317 return IRQ_HANDLED;
0318 }
0319
0320 static int lpc32xx_waitfunc_nand(struct nand_chip *chip)
0321 {
0322 struct mtd_info *mtd = nand_to_mtd(chip);
0323 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0324
0325 if (readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)
0326 goto exit;
0327
0328 wait_for_completion(&host->comp_nand);
0329
0330 while (!(readb(MLC_ISR(host->io_base)) & MLCISR_NAND_READY)) {
0331
0332 dev_dbg(&mtd->dev, "Warning: NAND not ready.\n");
0333 cpu_relax();
0334 }
0335
0336 exit:
0337 return NAND_STATUS_READY;
0338 }
0339
0340 static int lpc32xx_waitfunc_controller(struct nand_chip *chip)
0341 {
0342 struct mtd_info *mtd = nand_to_mtd(chip);
0343 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0344
0345 if (readb(MLC_ISR(host->io_base)) & MLCISR_CONTROLLER_READY)
0346 goto exit;
0347
0348 wait_for_completion(&host->comp_controller);
0349
0350 while (!(readb(MLC_ISR(host->io_base)) &
0351 MLCISR_CONTROLLER_READY)) {
0352 dev_dbg(&mtd->dev, "Warning: Controller not ready.\n");
0353 cpu_relax();
0354 }
0355
0356 exit:
0357 return NAND_STATUS_READY;
0358 }
0359
0360 static int lpc32xx_waitfunc(struct nand_chip *chip)
0361 {
0362 lpc32xx_waitfunc_nand(chip);
0363 lpc32xx_waitfunc_controller(chip);
0364
0365 return NAND_STATUS_READY;
0366 }
0367
0368
0369
0370
0371 static void lpc32xx_wp_enable(struct lpc32xx_nand_host *host)
0372 {
0373 if (gpio_is_valid(host->ncfg->wp_gpio))
0374 gpio_set_value(host->ncfg->wp_gpio, 0);
0375 }
0376
0377
0378
0379
0380 static void lpc32xx_wp_disable(struct lpc32xx_nand_host *host)
0381 {
0382 if (gpio_is_valid(host->ncfg->wp_gpio))
0383 gpio_set_value(host->ncfg->wp_gpio, 1);
0384 }
0385
0386 static void lpc32xx_dma_complete_func(void *completion)
0387 {
0388 complete(completion);
0389 }
0390
0391 static int lpc32xx_xmit_dma(struct mtd_info *mtd, void *mem, int len,
0392 enum dma_transfer_direction dir)
0393 {
0394 struct nand_chip *chip = mtd_to_nand(mtd);
0395 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0396 struct dma_async_tx_descriptor *desc;
0397 int flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
0398 int res;
0399
0400 sg_init_one(&host->sgl, mem, len);
0401
0402 res = dma_map_sg(host->dma_chan->device->dev, &host->sgl, 1,
0403 DMA_BIDIRECTIONAL);
0404 if (res != 1) {
0405 dev_err(mtd->dev.parent, "Failed to map sg list\n");
0406 return -ENXIO;
0407 }
0408 desc = dmaengine_prep_slave_sg(host->dma_chan, &host->sgl, 1, dir,
0409 flags);
0410 if (!desc) {
0411 dev_err(mtd->dev.parent, "Failed to prepare slave sg\n");
0412 goto out1;
0413 }
0414
0415 init_completion(&host->comp_dma);
0416 desc->callback = lpc32xx_dma_complete_func;
0417 desc->callback_param = &host->comp_dma;
0418
0419 dmaengine_submit(desc);
0420 dma_async_issue_pending(host->dma_chan);
0421
0422 wait_for_completion_timeout(&host->comp_dma, msecs_to_jiffies(1000));
0423
0424 dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
0425 DMA_BIDIRECTIONAL);
0426 return 0;
0427 out1:
0428 dma_unmap_sg(host->dma_chan->device->dev, &host->sgl, 1,
0429 DMA_BIDIRECTIONAL);
0430 return -ENXIO;
0431 }
0432
0433 static int lpc32xx_read_page(struct nand_chip *chip, uint8_t *buf,
0434 int oob_required, int page)
0435 {
0436 struct mtd_info *mtd = nand_to_mtd(chip);
0437 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0438 int i, j;
0439 uint8_t *oobbuf = chip->oob_poi;
0440 uint32_t mlc_isr;
0441 int res;
0442 uint8_t *dma_buf;
0443 bool dma_mapped;
0444
0445 if ((void *)buf <= high_memory) {
0446 dma_buf = buf;
0447 dma_mapped = true;
0448 } else {
0449 dma_buf = host->dma_buf;
0450 dma_mapped = false;
0451 }
0452
0453
0454 nand_read_page_op(chip, page, 0, NULL, 0);
0455
0456
0457 for (i = 0; i < host->mlcsubpages; i++) {
0458
0459 writeb(0x00, MLC_ECC_AUTO_DEC_REG(host->io_base));
0460
0461
0462 lpc32xx_waitfunc_controller(chip);
0463
0464
0465 mlc_isr = readl(MLC_ISR(host->io_base));
0466 if (mlc_isr & MLCISR_DECODER_FAILURE) {
0467 mtd->ecc_stats.failed++;
0468 dev_warn(&mtd->dev, "%s: DECODER_FAILURE\n", __func__);
0469 } else if (mlc_isr & MLCISR_ERRORS_DETECTED) {
0470 mtd->ecc_stats.corrected += ((mlc_isr >> 4) & 0x3) + 1;
0471 }
0472
0473
0474 if (use_dma) {
0475 res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
0476 DMA_DEV_TO_MEM);
0477 if (res)
0478 return res;
0479 } else {
0480 for (j = 0; j < (512 >> 2); j++) {
0481 *((uint32_t *)(buf)) =
0482 readl(MLC_BUFF(host->io_base));
0483 buf += 4;
0484 }
0485 }
0486 for (j = 0; j < (16 >> 2); j++) {
0487 *((uint32_t *)(oobbuf)) =
0488 readl(MLC_BUFF(host->io_base));
0489 oobbuf += 4;
0490 }
0491 }
0492
0493 if (use_dma && !dma_mapped)
0494 memcpy(buf, dma_buf, mtd->writesize);
0495
0496 return 0;
0497 }
0498
0499 static int lpc32xx_write_page_lowlevel(struct nand_chip *chip,
0500 const uint8_t *buf, int oob_required,
0501 int page)
0502 {
0503 struct mtd_info *mtd = nand_to_mtd(chip);
0504 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0505 const uint8_t *oobbuf = chip->oob_poi;
0506 uint8_t *dma_buf = (uint8_t *)buf;
0507 int res;
0508 int i, j;
0509
0510 if (use_dma && (void *)buf >= high_memory) {
0511 dma_buf = host->dma_buf;
0512 memcpy(dma_buf, buf, mtd->writesize);
0513 }
0514
0515 nand_prog_page_begin_op(chip, page, 0, NULL, 0);
0516
0517 for (i = 0; i < host->mlcsubpages; i++) {
0518
0519 writeb(0x00, MLC_ECC_ENC_REG(host->io_base));
0520
0521
0522 if (use_dma) {
0523 res = lpc32xx_xmit_dma(mtd, dma_buf + i * 512, 512,
0524 DMA_MEM_TO_DEV);
0525 if (res)
0526 return res;
0527 } else {
0528 for (j = 0; j < (512 >> 2); j++) {
0529 writel(*((uint32_t *)(buf)),
0530 MLC_BUFF(host->io_base));
0531 buf += 4;
0532 }
0533 }
0534 writel(*((uint32_t *)(oobbuf)), MLC_BUFF(host->io_base));
0535 oobbuf += 4;
0536 writew(*((uint16_t *)(oobbuf)), MLC_BUFF(host->io_base));
0537 oobbuf += 12;
0538
0539
0540 writeb(0x00, MLC_ECC_AUTO_ENC_REG(host->io_base));
0541
0542
0543 lpc32xx_waitfunc_controller(chip);
0544 }
0545
0546 return nand_prog_page_end_op(chip);
0547 }
0548
0549 static int lpc32xx_read_oob(struct nand_chip *chip, int page)
0550 {
0551 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0552
0553
0554 lpc32xx_read_page(chip, host->dummy_buf, 1, page);
0555
0556 return 0;
0557 }
0558
0559 static int lpc32xx_write_oob(struct nand_chip *chip, int page)
0560 {
0561
0562 return 0;
0563 }
0564
0565
0566 static void lpc32xx_ecc_enable(struct nand_chip *chip, int mode)
0567 {
0568
0569 }
0570
0571 static int lpc32xx_dma_setup(struct lpc32xx_nand_host *host)
0572 {
0573 struct mtd_info *mtd = nand_to_mtd(&host->nand_chip);
0574 dma_cap_mask_t mask;
0575
0576 if (!host->pdata || !host->pdata->dma_filter) {
0577 dev_err(mtd->dev.parent, "no DMA platform data\n");
0578 return -ENOENT;
0579 }
0580
0581 dma_cap_zero(mask);
0582 dma_cap_set(DMA_SLAVE, mask);
0583 host->dma_chan = dma_request_channel(mask, host->pdata->dma_filter,
0584 "nand-mlc");
0585 if (!host->dma_chan) {
0586 dev_err(mtd->dev.parent, "Failed to request DMA channel\n");
0587 return -EBUSY;
0588 }
0589
0590
0591
0592
0593
0594
0595 host->dma_slave_config.direction = DMA_DEV_TO_MEM;
0596 host->dma_slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
0597 host->dma_slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
0598 host->dma_slave_config.src_maxburst = 128;
0599 host->dma_slave_config.dst_maxburst = 128;
0600
0601 host->dma_slave_config.device_fc = false;
0602 host->dma_slave_config.src_addr = MLC_BUFF(host->io_base_phy);
0603 host->dma_slave_config.dst_addr = MLC_BUFF(host->io_base_phy);
0604 if (dmaengine_slave_config(host->dma_chan, &host->dma_slave_config)) {
0605 dev_err(mtd->dev.parent, "Failed to setup DMA slave\n");
0606 goto out1;
0607 }
0608
0609 return 0;
0610 out1:
0611 dma_release_channel(host->dma_chan);
0612 return -ENXIO;
0613 }
0614
0615 static struct lpc32xx_nand_cfg_mlc *lpc32xx_parse_dt(struct device *dev)
0616 {
0617 struct lpc32xx_nand_cfg_mlc *ncfg;
0618 struct device_node *np = dev->of_node;
0619
0620 ncfg = devm_kzalloc(dev, sizeof(*ncfg), GFP_KERNEL);
0621 if (!ncfg)
0622 return NULL;
0623
0624 of_property_read_u32(np, "nxp,tcea-delay", &ncfg->tcea_delay);
0625 of_property_read_u32(np, "nxp,busy-delay", &ncfg->busy_delay);
0626 of_property_read_u32(np, "nxp,nand-ta", &ncfg->nand_ta);
0627 of_property_read_u32(np, "nxp,rd-high", &ncfg->rd_high);
0628 of_property_read_u32(np, "nxp,rd-low", &ncfg->rd_low);
0629 of_property_read_u32(np, "nxp,wr-high", &ncfg->wr_high);
0630 of_property_read_u32(np, "nxp,wr-low", &ncfg->wr_low);
0631
0632 if (!ncfg->tcea_delay || !ncfg->busy_delay || !ncfg->nand_ta ||
0633 !ncfg->rd_high || !ncfg->rd_low || !ncfg->wr_high ||
0634 !ncfg->wr_low) {
0635 dev_err(dev, "chip parameters not specified correctly\n");
0636 return NULL;
0637 }
0638
0639 ncfg->wp_gpio = of_get_named_gpio(np, "gpios", 0);
0640
0641 return ncfg;
0642 }
0643
0644 static int lpc32xx_nand_attach_chip(struct nand_chip *chip)
0645 {
0646 struct mtd_info *mtd = nand_to_mtd(chip);
0647 struct lpc32xx_nand_host *host = nand_get_controller_data(chip);
0648 struct device *dev = &host->pdev->dev;
0649
0650 if (chip->ecc.engine_type != NAND_ECC_ENGINE_TYPE_ON_HOST)
0651 return 0;
0652
0653 host->dma_buf = devm_kzalloc(dev, mtd->writesize, GFP_KERNEL);
0654 if (!host->dma_buf)
0655 return -ENOMEM;
0656
0657 host->dummy_buf = devm_kzalloc(dev, mtd->writesize, GFP_KERNEL);
0658 if (!host->dummy_buf)
0659 return -ENOMEM;
0660
0661 chip->ecc.size = 512;
0662 chip->ecc.hwctl = lpc32xx_ecc_enable;
0663 chip->ecc.read_page_raw = lpc32xx_read_page;
0664 chip->ecc.read_page = lpc32xx_read_page;
0665 chip->ecc.write_page_raw = lpc32xx_write_page_lowlevel;
0666 chip->ecc.write_page = lpc32xx_write_page_lowlevel;
0667 chip->ecc.write_oob = lpc32xx_write_oob;
0668 chip->ecc.read_oob = lpc32xx_read_oob;
0669 chip->ecc.strength = 4;
0670 chip->ecc.bytes = 10;
0671
0672 mtd_set_ooblayout(mtd, &lpc32xx_ooblayout_ops);
0673 host->mlcsubpages = mtd->writesize / 512;
0674
0675 return 0;
0676 }
0677
0678 static const struct nand_controller_ops lpc32xx_nand_controller_ops = {
0679 .attach_chip = lpc32xx_nand_attach_chip,
0680 };
0681
0682
0683
0684
0685 static int lpc32xx_nand_probe(struct platform_device *pdev)
0686 {
0687 struct lpc32xx_nand_host *host;
0688 struct mtd_info *mtd;
0689 struct nand_chip *nand_chip;
0690 struct resource *rc;
0691 int res;
0692
0693
0694 host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
0695 if (!host)
0696 return -ENOMEM;
0697
0698 host->pdev = pdev;
0699
0700 rc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0701 host->io_base = devm_ioremap_resource(&pdev->dev, rc);
0702 if (IS_ERR(host->io_base))
0703 return PTR_ERR(host->io_base);
0704
0705 host->io_base_phy = rc->start;
0706
0707 nand_chip = &host->nand_chip;
0708 mtd = nand_to_mtd(nand_chip);
0709 if (pdev->dev.of_node)
0710 host->ncfg = lpc32xx_parse_dt(&pdev->dev);
0711 if (!host->ncfg) {
0712 dev_err(&pdev->dev,
0713 "Missing or bad NAND config from device tree\n");
0714 return -ENOENT;
0715 }
0716 if (host->ncfg->wp_gpio == -EPROBE_DEFER)
0717 return -EPROBE_DEFER;
0718 if (gpio_is_valid(host->ncfg->wp_gpio) &&
0719 gpio_request(host->ncfg->wp_gpio, "NAND WP")) {
0720 dev_err(&pdev->dev, "GPIO not available\n");
0721 return -EBUSY;
0722 }
0723 lpc32xx_wp_disable(host);
0724
0725 host->pdata = dev_get_platdata(&pdev->dev);
0726
0727
0728 nand_set_controller_data(nand_chip, host);
0729 nand_set_flash_node(nand_chip, pdev->dev.of_node);
0730 mtd->dev.parent = &pdev->dev;
0731
0732
0733 host->clk = clk_get(&pdev->dev, NULL);
0734 if (IS_ERR(host->clk)) {
0735 dev_err(&pdev->dev, "Clock initialization failure\n");
0736 res = -ENOENT;
0737 goto free_gpio;
0738 }
0739 res = clk_prepare_enable(host->clk);
0740 if (res)
0741 goto put_clk;
0742
0743 nand_chip->legacy.cmd_ctrl = lpc32xx_nand_cmd_ctrl;
0744 nand_chip->legacy.dev_ready = lpc32xx_nand_device_ready;
0745 nand_chip->legacy.chip_delay = 25;
0746 nand_chip->legacy.IO_ADDR_R = MLC_DATA(host->io_base);
0747 nand_chip->legacy.IO_ADDR_W = MLC_DATA(host->io_base);
0748
0749
0750 lpc32xx_nand_setup(host);
0751
0752 platform_set_drvdata(pdev, host);
0753
0754
0755 nand_chip->legacy.waitfunc = lpc32xx_waitfunc;
0756
0757 nand_chip->options = NAND_NO_SUBPAGE_WRITE;
0758 nand_chip->bbt_options = NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
0759 nand_chip->bbt_td = &lpc32xx_nand_bbt;
0760 nand_chip->bbt_md = &lpc32xx_nand_bbt_mirror;
0761
0762 if (use_dma) {
0763 res = lpc32xx_dma_setup(host);
0764 if (res) {
0765 res = -EIO;
0766 goto unprepare_clk;
0767 }
0768 }
0769
0770
0771 readb(MLC_IRQ_SR(host->io_base));
0772
0773 init_completion(&host->comp_nand);
0774 init_completion(&host->comp_controller);
0775
0776 host->irq = platform_get_irq(pdev, 0);
0777 if (host->irq < 0) {
0778 res = -EINVAL;
0779 goto release_dma_chan;
0780 }
0781
0782 if (request_irq(host->irq, (irq_handler_t)&lpc3xxx_nand_irq,
0783 IRQF_TRIGGER_HIGH, DRV_NAME, host)) {
0784 dev_err(&pdev->dev, "Error requesting NAND IRQ\n");
0785 res = -ENXIO;
0786 goto release_dma_chan;
0787 }
0788
0789
0790
0791
0792
0793 nand_chip->legacy.dummy_controller.ops = &lpc32xx_nand_controller_ops;
0794 res = nand_scan(nand_chip, 1);
0795 if (res)
0796 goto free_irq;
0797
0798 mtd->name = DRV_NAME;
0799
0800 res = mtd_device_register(mtd, host->ncfg->parts,
0801 host->ncfg->num_parts);
0802 if (res)
0803 goto cleanup_nand;
0804
0805 return 0;
0806
0807 cleanup_nand:
0808 nand_cleanup(nand_chip);
0809 free_irq:
0810 free_irq(host->irq, host);
0811 release_dma_chan:
0812 if (use_dma)
0813 dma_release_channel(host->dma_chan);
0814 unprepare_clk:
0815 clk_disable_unprepare(host->clk);
0816 put_clk:
0817 clk_put(host->clk);
0818 free_gpio:
0819 lpc32xx_wp_enable(host);
0820 gpio_free(host->ncfg->wp_gpio);
0821
0822 return res;
0823 }
0824
0825
0826
0827
0828 static int lpc32xx_nand_remove(struct platform_device *pdev)
0829 {
0830 struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
0831 struct nand_chip *chip = &host->nand_chip;
0832 int ret;
0833
0834 ret = mtd_device_unregister(nand_to_mtd(chip));
0835 WARN_ON(ret);
0836 nand_cleanup(chip);
0837
0838 free_irq(host->irq, host);
0839 if (use_dma)
0840 dma_release_channel(host->dma_chan);
0841
0842 clk_disable_unprepare(host->clk);
0843 clk_put(host->clk);
0844
0845 lpc32xx_wp_enable(host);
0846 gpio_free(host->ncfg->wp_gpio);
0847
0848 return 0;
0849 }
0850
0851 #ifdef CONFIG_PM
0852 static int lpc32xx_nand_resume(struct platform_device *pdev)
0853 {
0854 struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
0855 int ret;
0856
0857
0858 ret = clk_prepare_enable(host->clk);
0859 if (ret)
0860 return ret;
0861
0862
0863 lpc32xx_nand_setup(host);
0864
0865
0866 lpc32xx_wp_disable(host);
0867
0868 return 0;
0869 }
0870
0871 static int lpc32xx_nand_suspend(struct platform_device *pdev, pm_message_t pm)
0872 {
0873 struct lpc32xx_nand_host *host = platform_get_drvdata(pdev);
0874
0875
0876 lpc32xx_wp_enable(host);
0877
0878
0879 clk_disable_unprepare(host->clk);
0880 return 0;
0881 }
0882
0883 #else
0884 #define lpc32xx_nand_resume NULL
0885 #define lpc32xx_nand_suspend NULL
0886 #endif
0887
0888 static const struct of_device_id lpc32xx_nand_match[] = {
0889 { .compatible = "nxp,lpc3220-mlc" },
0890 { },
0891 };
0892 MODULE_DEVICE_TABLE(of, lpc32xx_nand_match);
0893
0894 static struct platform_driver lpc32xx_nand_driver = {
0895 .probe = lpc32xx_nand_probe,
0896 .remove = lpc32xx_nand_remove,
0897 .resume = lpc32xx_nand_resume,
0898 .suspend = lpc32xx_nand_suspend,
0899 .driver = {
0900 .name = DRV_NAME,
0901 .of_match_table = lpc32xx_nand_match,
0902 },
0903 };
0904
0905 module_platform_driver(lpc32xx_nand_driver);
0906
0907 MODULE_LICENSE("GPL");
0908 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
0909 MODULE_DESCRIPTION("NAND driver for the NXP LPC32XX MLC controller");