2 * drivers/mtd/nand/pxa3xx_nand.c
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/interrupt.h>
15 #include <linux/platform_device.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/delay.h>
18 #include <linux/clk.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/nand.h>
21 #include <linux/mtd/partitions.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
26 #include <linux/of_device.h>
29 #include <linux/platform_data/mtd-nand-pxa3xx.h>
31 #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
32 #define NAND_STOP_DELAY (2 * HZ/50)
33 #define PAGE_CHUNK_SIZE (2048)
35 /* registers and bit definitions */
36 #define NDCR (0x00) /* Control register */
37 #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
38 #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
39 #define NDSR (0x14) /* Status Register */
40 #define NDPCR (0x18) /* Page Count Register */
41 #define NDBDR0 (0x1C) /* Bad Block Register 0 */
42 #define NDBDR1 (0x20) /* Bad Block Register 1 */
43 #define NDDB (0x40) /* Data Buffer */
44 #define NDCB0 (0x48) /* Command Buffer0 */
45 #define NDCB1 (0x4C) /* Command Buffer1 */
46 #define NDCB2 (0x50) /* Command Buffer2 */
48 #define NDCR_SPARE_EN (0x1 << 31)
49 #define NDCR_ECC_EN (0x1 << 30)
50 #define NDCR_DMA_EN (0x1 << 29)
51 #define NDCR_ND_RUN (0x1 << 28)
52 #define NDCR_DWIDTH_C (0x1 << 27)
53 #define NDCR_DWIDTH_M (0x1 << 26)
54 #define NDCR_PAGE_SZ (0x1 << 24)
55 #define NDCR_NCSX (0x1 << 23)
56 #define NDCR_ND_MODE (0x3 << 21)
57 #define NDCR_NAND_MODE (0x0)
58 #define NDCR_CLR_PG_CNT (0x1 << 20)
59 #define NDCR_STOP_ON_UNCOR (0x1 << 19)
60 #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
61 #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
63 #define NDCR_RA_START (0x1 << 15)
64 #define NDCR_PG_PER_BLK (0x1 << 14)
65 #define NDCR_ND_ARB_EN (0x1 << 12)
66 #define NDCR_INT_MASK (0xFFF)
68 #define NDSR_MASK (0xfff)
69 #define NDSR_RDY (0x1 << 12)
70 #define NDSR_FLASH_RDY (0x1 << 11)
71 #define NDSR_CS0_PAGED (0x1 << 10)
72 #define NDSR_CS1_PAGED (0x1 << 9)
73 #define NDSR_CS0_CMDD (0x1 << 8)
74 #define NDSR_CS1_CMDD (0x1 << 7)
75 #define NDSR_CS0_BBD (0x1 << 6)
76 #define NDSR_CS1_BBD (0x1 << 5)
77 #define NDSR_DBERR (0x1 << 4)
78 #define NDSR_SBERR (0x1 << 3)
79 #define NDSR_WRDREQ (0x1 << 2)
80 #define NDSR_RDDREQ (0x1 << 1)
81 #define NDSR_WRCMDREQ (0x1)
83 #define NDCB0_ST_ROW_EN (0x1 << 26)
84 #define NDCB0_AUTO_RS (0x1 << 25)
85 #define NDCB0_CSEL (0x1 << 24)
86 #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
87 #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
88 #define NDCB0_NC (0x1 << 20)
89 #define NDCB0_DBC (0x1 << 19)
90 #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
91 #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
92 #define NDCB0_CMD2_MASK (0xff << 8)
93 #define NDCB0_CMD1_MASK (0xff)
94 #define NDCB0_ADDR_CYC_SHIFT (16)
96 /* macros for registers read/write */
97 #define nand_writel(info, off, val) \
98 __raw_writel((val), (info)->mmio_base + (off))
100 #define nand_readl(info, off) \
101 __raw_readl((info)->mmio_base + (off))
103 /* error code and state */
126 enum pxa3xx_nand_variant {
127 PXA3XX_NAND_VARIANT_PXA,
128 PXA3XX_NAND_VARIANT_ARMADA370,
131 struct pxa3xx_nand_host {
132 struct nand_chip chip;
133 struct pxa3xx_nand_cmdset *cmdset;
134 struct mtd_info *mtd;
137 /* page size of attached chip */
138 unsigned int page_size;
142 /* calculated from pxa3xx_nand_flash data */
143 unsigned int col_addr_cycles;
144 unsigned int row_addr_cycles;
145 size_t read_id_bytes;
147 /* cached register value */
153 struct pxa3xx_nand_info {
154 struct nand_hw_control controller;
155 struct platform_device *pdev;
158 void __iomem *mmio_base;
159 unsigned long mmio_phys;
160 struct completion cmd_complete;
162 unsigned int buf_start;
163 unsigned int buf_count;
165 /* DMA information */
169 unsigned char *data_buff;
170 unsigned char *oob_buff;
171 dma_addr_t data_buff_phys;
173 struct pxa_dma_desc *data_desc;
174 dma_addr_t data_desc_addr;
176 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
180 * This driver supports NFCv1 (as found in PXA SoC)
181 * and NFCv2 (as found in Armada 370/XP SoC).
183 enum pxa3xx_nand_variant variant;
186 int use_ecc; /* use HW ECC ? */
187 int use_dma; /* use DMA ? */
190 unsigned int page_size; /* page size of attached chip */
191 unsigned int data_size; /* data size in FIFO */
192 unsigned int oob_size;
195 /* generated NDCBx register values */
201 static bool use_dma = 1;
202 module_param(use_dma, bool, 0444);
203 MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
206 * Default NAND flash controller configuration setup by the
207 * bootloader. This configuration is used only when pdata->keep_config is set
209 static struct pxa3xx_nand_cmdset default_cmdset = {
213 .read_status = 0x0070,
219 .lock_status = 0x007A,
222 static struct pxa3xx_nand_timing timing[] = {
223 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
224 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
225 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
226 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
229 static struct pxa3xx_nand_flash builtin_flash_types[] = {
230 { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
231 { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
232 { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
233 { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
234 { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
235 { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
236 { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
237 { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
238 { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
241 /* Define a default flash type setting serve as flash detecting only */
242 #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
244 const char *mtd_names[] = {"pxa3xx_nand-0", "pxa3xx_nand-1", NULL};
246 #define NDTR0_tCH(c) (min((c), 7) << 19)
247 #define NDTR0_tCS(c) (min((c), 7) << 16)
248 #define NDTR0_tWH(c) (min((c), 7) << 11)
249 #define NDTR0_tWP(c) (min((c), 7) << 8)
250 #define NDTR0_tRH(c) (min((c), 7) << 3)
251 #define NDTR0_tRP(c) (min((c), 7) << 0)
253 #define NDTR1_tR(c) (min((c), 65535) << 16)
254 #define NDTR1_tWHR(c) (min((c), 15) << 4)
255 #define NDTR1_tAR(c) (min((c), 15) << 0)
257 /* convert nano-seconds to nand flash controller clock cycles */
258 #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
260 static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
261 const struct pxa3xx_nand_timing *t)
263 struct pxa3xx_nand_info *info = host->info_data;
264 unsigned long nand_clk = clk_get_rate(info->clk);
265 uint32_t ndtr0, ndtr1;
267 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
268 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
269 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
270 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
271 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
272 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
274 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
275 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
276 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
278 host->ndtr0cs0 = ndtr0;
279 host->ndtr1cs0 = ndtr1;
280 nand_writel(info, NDTR0CS0, ndtr0);
281 nand_writel(info, NDTR1CS0, ndtr1);
284 static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
286 struct pxa3xx_nand_host *host = info->host[info->cs];
287 int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
289 info->data_size = host->page_size;
295 switch (host->page_size) {
297 info->oob_size = (info->use_ecc) ? 40 : 64;
300 info->oob_size = (info->use_ecc) ? 8 : 16;
306 * NOTE: it is a must to set ND_RUN firstly, then write
307 * command buffer, otherwise, it does not work.
308 * We enable all the interrupt at the same time, and
309 * let pxa3xx_nand_irq to handle all logic.
311 static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
313 struct pxa3xx_nand_host *host = info->host[info->cs];
316 ndcr = host->reg_ndcr;
317 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
318 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
321 /* clear status bits and run */
322 nand_writel(info, NDCR, 0);
323 nand_writel(info, NDSR, NDSR_MASK);
324 nand_writel(info, NDCR, ndcr);
327 static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
330 int timeout = NAND_STOP_DELAY;
332 /* wait RUN bit in NDCR become 0 */
333 ndcr = nand_readl(info, NDCR);
334 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
335 ndcr = nand_readl(info, NDCR);
340 ndcr &= ~NDCR_ND_RUN;
341 nand_writel(info, NDCR, ndcr);
343 /* clear status bits */
344 nand_writel(info, NDSR, NDSR_MASK);
347 static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
351 ndcr = nand_readl(info, NDCR);
352 nand_writel(info, NDCR, ndcr & ~int_mask);
355 static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
359 ndcr = nand_readl(info, NDCR);
360 nand_writel(info, NDCR, ndcr | int_mask);
363 static void handle_data_pio(struct pxa3xx_nand_info *info)
365 switch (info->state) {
366 case STATE_PIO_WRITING:
367 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
368 DIV_ROUND_UP(info->data_size, 4));
369 if (info->oob_size > 0)
370 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
371 DIV_ROUND_UP(info->oob_size, 4));
373 case STATE_PIO_READING:
374 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
375 DIV_ROUND_UP(info->data_size, 4));
376 if (info->oob_size > 0)
377 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
378 DIV_ROUND_UP(info->oob_size, 4));
381 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
387 static void start_data_dma(struct pxa3xx_nand_info *info)
389 struct pxa_dma_desc *desc = info->data_desc;
390 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
392 desc->ddadr = DDADR_STOP;
393 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
395 switch (info->state) {
396 case STATE_DMA_WRITING:
397 desc->dsadr = info->data_buff_phys;
398 desc->dtadr = info->mmio_phys + NDDB;
399 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
401 case STATE_DMA_READING:
402 desc->dtadr = info->data_buff_phys;
403 desc->dsadr = info->mmio_phys + NDDB;
404 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
407 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
412 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
413 DDADR(info->data_dma_ch) = info->data_desc_addr;
414 DCSR(info->data_dma_ch) |= DCSR_RUN;
417 static void pxa3xx_nand_data_dma_irq(int channel, void *data)
419 struct pxa3xx_nand_info *info = data;
422 dcsr = DCSR(channel);
423 DCSR(channel) = dcsr;
425 if (dcsr & DCSR_BUSERR) {
426 info->retcode = ERR_DMABUSERR;
429 info->state = STATE_DMA_DONE;
430 enable_int(info, NDCR_INT_MASK);
431 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
434 static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
436 struct pxa3xx_nand_info *info = devid;
437 unsigned int status, is_completed = 0;
438 unsigned int ready, cmd_done;
441 ready = NDSR_FLASH_RDY;
442 cmd_done = NDSR_CS0_CMDD;
445 cmd_done = NDSR_CS1_CMDD;
448 status = nand_readl(info, NDSR);
450 if (status & NDSR_DBERR)
451 info->retcode = ERR_DBERR;
452 if (status & NDSR_SBERR)
453 info->retcode = ERR_SBERR;
454 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
455 /* whether use dma to transfer data */
457 disable_int(info, NDCR_INT_MASK);
458 info->state = (status & NDSR_RDDREQ) ?
459 STATE_DMA_READING : STATE_DMA_WRITING;
460 start_data_dma(info);
461 goto NORMAL_IRQ_EXIT;
463 info->state = (status & NDSR_RDDREQ) ?
464 STATE_PIO_READING : STATE_PIO_WRITING;
465 handle_data_pio(info);
468 if (status & cmd_done) {
469 info->state = STATE_CMD_DONE;
472 if (status & ready) {
474 info->state = STATE_READY;
477 if (status & NDSR_WRCMDREQ) {
478 nand_writel(info, NDSR, NDSR_WRCMDREQ);
479 status &= ~NDSR_WRCMDREQ;
480 info->state = STATE_CMD_HANDLE;
481 nand_writel(info, NDCB0, info->ndcb0);
482 nand_writel(info, NDCB0, info->ndcb1);
483 nand_writel(info, NDCB0, info->ndcb2);
486 /* clear NDSR to let the controller exit the IRQ */
487 nand_writel(info, NDSR, status);
489 complete(&info->cmd_complete);
494 static inline int is_buf_blank(uint8_t *buf, size_t len)
496 for (; len > 0; len--)
502 static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
503 uint16_t column, int page_addr)
506 int addr_cycle, exec_cmd;
507 struct pxa3xx_nand_host *host;
508 struct mtd_info *mtd;
510 host = info->host[info->cs];
515 /* reset data and oob column point to handle data */
520 info->use_dma = (use_dma) ? 1 : 0;
522 info->retcode = ERR_NONE;
524 info->ndcb0 = NDCB0_CSEL;
530 case NAND_CMD_PAGEPROG:
532 case NAND_CMD_READOOB:
533 pxa3xx_set_datasize(info);
544 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
545 + host->col_addr_cycles);
548 case NAND_CMD_READOOB:
550 cmd = host->cmdset->read1;
551 if (command == NAND_CMD_READOOB)
552 info->buf_start = mtd->writesize + column;
554 info->buf_start = column;
556 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
557 info->ndcb0 |= NDCB0_CMD_TYPE(0)
559 | (cmd & NDCB0_CMD1_MASK);
561 info->ndcb0 |= NDCB0_CMD_TYPE(0)
567 /* small page addr setting */
568 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
569 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
574 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
577 if (page_addr & 0xFF0000)
578 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
583 info->buf_count = mtd->writesize + mtd->oobsize;
584 memset(info->data_buff, 0xFF, info->buf_count);
588 case NAND_CMD_PAGEPROG:
589 if (is_buf_blank(info->data_buff,
590 (mtd->writesize + mtd->oobsize))) {
595 cmd = host->cmdset->program;
596 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
605 cmd = NAND_CMD_PARAM;
606 info->buf_count = 256;
607 info->ndcb0 |= NDCB0_CMD_TYPE(0)
610 info->ndcb1 = (column & 0xFF);
611 info->data_size = 256;
614 case NAND_CMD_READID:
615 cmd = host->cmdset->read_id;
616 info->buf_count = host->read_id_bytes;
617 info->ndcb0 |= NDCB0_CMD_TYPE(3)
620 info->ndcb1 = (column & 0xFF);
624 case NAND_CMD_STATUS:
625 cmd = host->cmdset->read_status;
627 info->ndcb0 |= NDCB0_CMD_TYPE(4)
634 case NAND_CMD_ERASE1:
635 cmd = host->cmdset->erase;
636 info->ndcb0 |= NDCB0_CMD_TYPE(2)
641 info->ndcb1 = page_addr;
646 cmd = host->cmdset->reset;
647 info->ndcb0 |= NDCB0_CMD_TYPE(5)
652 case NAND_CMD_ERASE2:
658 dev_err(&info->pdev->dev, "non-supported command %x\n",
666 static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
667 int column, int page_addr)
669 struct pxa3xx_nand_host *host = mtd->priv;
670 struct pxa3xx_nand_info *info = host->info_data;
674 * if this is a x16 device ,then convert the input
675 * "byte" address into a "word" address appropriate
676 * for indexing a word-oriented device
678 if (host->reg_ndcr & NDCR_DWIDTH_M)
682 * There may be different NAND chip hooked to
683 * different chip select, so check whether
684 * chip select has been changed, if yes, reset the timing
686 if (info->cs != host->cs) {
688 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
689 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
692 info->state = STATE_PREPARED;
693 exec_cmd = prepare_command_pool(info, command, column, page_addr);
695 init_completion(&info->cmd_complete);
696 pxa3xx_nand_start(info);
698 ret = wait_for_completion_timeout(&info->cmd_complete,
701 dev_err(&info->pdev->dev, "Wait time out!!!\n");
702 /* Stop State Machine for next command cycle */
703 pxa3xx_nand_stop(info);
706 info->state = STATE_IDLE;
709 static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
710 struct nand_chip *chip, const uint8_t *buf, int oob_required)
712 chip->write_buf(mtd, buf, mtd->writesize);
713 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
718 static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
719 struct nand_chip *chip, uint8_t *buf, int oob_required,
722 struct pxa3xx_nand_host *host = mtd->priv;
723 struct pxa3xx_nand_info *info = host->info_data;
725 chip->read_buf(mtd, buf, mtd->writesize);
726 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
728 if (info->retcode == ERR_SBERR) {
729 switch (info->use_ecc) {
731 mtd->ecc_stats.corrected++;
737 } else if (info->retcode == ERR_DBERR) {
739 * for blank page (all 0xff), HW will calculate its ECC as
740 * 0, which is different from the ECC information within
741 * OOB, ignore such double bit errors
743 if (is_buf_blank(buf, mtd->writesize))
744 info->retcode = ERR_NONE;
746 mtd->ecc_stats.failed++;
752 static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
754 struct pxa3xx_nand_host *host = mtd->priv;
755 struct pxa3xx_nand_info *info = host->info_data;
758 if (info->buf_start < info->buf_count)
759 /* Has just send a new command? */
760 retval = info->data_buff[info->buf_start++];
765 static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
767 struct pxa3xx_nand_host *host = mtd->priv;
768 struct pxa3xx_nand_info *info = host->info_data;
771 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
772 retval = *((u16 *)(info->data_buff+info->buf_start));
773 info->buf_start += 2;
778 static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
780 struct pxa3xx_nand_host *host = mtd->priv;
781 struct pxa3xx_nand_info *info = host->info_data;
782 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
784 memcpy(buf, info->data_buff + info->buf_start, real_len);
785 info->buf_start += real_len;
788 static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
789 const uint8_t *buf, int len)
791 struct pxa3xx_nand_host *host = mtd->priv;
792 struct pxa3xx_nand_info *info = host->info_data;
793 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
795 memcpy(info->data_buff + info->buf_start, buf, real_len);
796 info->buf_start += real_len;
799 static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
804 static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
806 struct pxa3xx_nand_host *host = mtd->priv;
807 struct pxa3xx_nand_info *info = host->info_data;
809 /* pxa3xx_nand_send_command has waited for command complete */
810 if (this->state == FL_WRITING || this->state == FL_ERASING) {
811 if (info->retcode == ERR_NONE)
815 * any error make it return 0x01 which will tell
816 * the caller the erase and write fail
825 static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
826 const struct pxa3xx_nand_flash *f)
828 struct platform_device *pdev = info->pdev;
829 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
830 struct pxa3xx_nand_host *host = info->host[info->cs];
831 uint32_t ndcr = 0x0; /* enable all interrupts */
833 if (f->page_size != 2048 && f->page_size != 512) {
834 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
838 if (f->flash_width != 16 && f->flash_width != 8) {
839 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
843 /* calculate flash information */
844 host->cmdset = &default_cmdset;
845 host->page_size = f->page_size;
846 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
848 /* calculate addressing information */
849 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
851 if (f->num_blocks * f->page_per_block > 65536)
852 host->row_addr_cycles = 3;
854 host->row_addr_cycles = 2;
856 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
857 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
858 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
859 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
860 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
861 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
863 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
864 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
866 host->reg_ndcr = ndcr;
868 pxa3xx_nand_set_timing(host, f->timing);
872 static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
875 * We set 0 by hard coding here, for we don't support keep_config
876 * when there is more than one chip attached to the controller
878 struct pxa3xx_nand_host *host = info->host[0];
879 uint32_t ndcr = nand_readl(info, NDCR);
881 if (ndcr & NDCR_PAGE_SZ) {
882 host->page_size = 2048;
883 host->read_id_bytes = 4;
885 host->page_size = 512;
886 host->read_id_bytes = 2;
889 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
890 host->cmdset = &default_cmdset;
892 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
893 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
898 /* the maximum possible buffer size for large page with OOB data
899 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
900 * data buffer and the DMA descriptor
902 #define MAX_BUFF_SIZE PAGE_SIZE
904 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
906 struct platform_device *pdev = info->pdev;
907 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
910 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
911 if (info->data_buff == NULL)
916 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
917 &info->data_buff_phys, GFP_KERNEL);
918 if (info->data_buff == NULL) {
919 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
923 info->data_desc = (void *)info->data_buff + data_desc_offset;
924 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
926 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
927 pxa3xx_nand_data_dma_irq, info);
928 if (info->data_dma_ch < 0) {
929 dev_err(&pdev->dev, "failed to request data dma\n");
930 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
931 info->data_buff, info->data_buff_phys);
932 return info->data_dma_ch;
938 static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
940 struct platform_device *pdev = info->pdev;
942 pxa_free_dma(info->data_dma_ch);
943 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
944 info->data_buff, info->data_buff_phys);
946 kfree(info->data_buff);
950 static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
952 struct mtd_info *mtd;
954 mtd = info->host[info->cs]->mtd;
955 /* use the common timing to make a try */
956 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
960 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
967 static int pxa3xx_nand_scan(struct mtd_info *mtd)
969 struct pxa3xx_nand_host *host = mtd->priv;
970 struct pxa3xx_nand_info *info = host->info_data;
971 struct platform_device *pdev = info->pdev;
972 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
973 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
974 const struct pxa3xx_nand_flash *f = NULL;
975 struct nand_chip *chip = mtd->priv;
980 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
983 ret = pxa3xx_nand_sensing(info);
985 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
991 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
992 id = *((uint16_t *)(info->data_buff));
994 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
996 dev_warn(&info->pdev->dev,
997 "Read out ID 0, potential timing set wrong!!\n");
1002 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1003 for (i = 0; i < num; i++) {
1004 if (i < pdata->num_flash)
1005 f = pdata->flash + i;
1007 f = &builtin_flash_types[i - pdata->num_flash + 1];
1009 /* find the chip in default list */
1010 if (f->chip_id == id)
1014 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
1015 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
1020 ret = pxa3xx_nand_config_flash(info, f);
1022 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1026 pxa3xx_flash_ids[0].name = f->name;
1027 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
1028 pxa3xx_flash_ids[0].pagesize = f->page_size;
1029 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1030 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1031 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1032 if (f->flash_width == 16)
1033 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
1034 pxa3xx_flash_ids[1].name = NULL;
1035 def = pxa3xx_flash_ids;
1037 chip->ecc.mode = NAND_ECC_HW;
1038 chip->ecc.size = host->page_size;
1039 chip->ecc.strength = 1;
1041 if (host->reg_ndcr & NDCR_DWIDTH_M)
1042 chip->options |= NAND_BUSWIDTH_16;
1044 if (nand_scan_ident(mtd, 1, def))
1046 /* calculate addressing information */
1047 if (mtd->writesize >= 2048)
1048 host->col_addr_cycles = 2;
1050 host->col_addr_cycles = 1;
1052 info->oob_buff = info->data_buff + mtd->writesize;
1053 if ((mtd->size >> chip->page_shift) > 65536)
1054 host->row_addr_cycles = 3;
1056 host->row_addr_cycles = 2;
1058 mtd->name = mtd_names[0];
1059 return nand_scan_tail(mtd);
1062 static int alloc_nand_resource(struct platform_device *pdev)
1064 struct pxa3xx_nand_platform_data *pdata;
1065 struct pxa3xx_nand_info *info;
1066 struct pxa3xx_nand_host *host;
1067 struct nand_chip *chip = NULL;
1068 struct mtd_info *mtd;
1072 pdata = dev_get_platdata(&pdev->dev);
1073 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1074 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1079 for (cs = 0; cs < pdata->num_cs; cs++) {
1080 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1081 (sizeof(*mtd) + sizeof(*host)) * cs);
1082 chip = (struct nand_chip *)(&mtd[1]);
1083 host = (struct pxa3xx_nand_host *)chip;
1084 info->host[cs] = host;
1087 host->info_data = info;
1089 mtd->owner = THIS_MODULE;
1091 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1092 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1093 chip->controller = &info->controller;
1094 chip->waitfunc = pxa3xx_nand_waitfunc;
1095 chip->select_chip = pxa3xx_nand_select_chip;
1096 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1097 chip->read_word = pxa3xx_nand_read_word;
1098 chip->read_byte = pxa3xx_nand_read_byte;
1099 chip->read_buf = pxa3xx_nand_read_buf;
1100 chip->write_buf = pxa3xx_nand_write_buf;
1103 spin_lock_init(&chip->controller->lock);
1104 init_waitqueue_head(&chip->controller->wq);
1105 info->clk = devm_clk_get(&pdev->dev, NULL);
1106 if (IS_ERR(info->clk)) {
1107 dev_err(&pdev->dev, "failed to get nand clock\n");
1108 return PTR_ERR(info->clk);
1110 ret = clk_prepare_enable(info->clk);
1115 * This is a dirty hack to make this driver work from devicetree
1116 * bindings. It can be removed once we have a prober DMA controller
1119 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
1120 info->drcmr_dat = 97;
1121 info->drcmr_cmd = 99;
1123 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1125 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1127 goto fail_disable_clk;
1129 info->drcmr_dat = r->start;
1131 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1133 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1135 goto fail_disable_clk;
1137 info->drcmr_cmd = r->start;
1140 irq = platform_get_irq(pdev, 0);
1142 dev_err(&pdev->dev, "no IRQ resource defined\n");
1144 goto fail_disable_clk;
1147 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1148 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1149 if (IS_ERR(info->mmio_base)) {
1150 ret = PTR_ERR(info->mmio_base);
1151 goto fail_disable_clk;
1153 info->mmio_phys = r->start;
1155 ret = pxa3xx_nand_init_buff(info);
1157 goto fail_disable_clk;
1159 /* initialize all interrupts to be disabled */
1160 disable_int(info, NDSR_MASK);
1162 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1165 dev_err(&pdev->dev, "failed to request IRQ\n");
1169 platform_set_drvdata(pdev, info);
1174 free_irq(irq, info);
1175 pxa3xx_nand_free_buff(info);
1177 clk_disable_unprepare(info->clk);
1181 static int pxa3xx_nand_remove(struct platform_device *pdev)
1183 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1184 struct pxa3xx_nand_platform_data *pdata;
1190 pdata = dev_get_platdata(&pdev->dev);
1192 irq = platform_get_irq(pdev, 0);
1194 free_irq(irq, info);
1195 pxa3xx_nand_free_buff(info);
1197 clk_disable_unprepare(info->clk);
1199 for (cs = 0; cs < pdata->num_cs; cs++)
1200 nand_release(info->host[cs]->mtd);
1205 static struct of_device_id pxa3xx_nand_dt_ids[] = {
1207 .compatible = "marvell,pxa3xx-nand",
1208 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1211 .compatible = "marvell,armada370-nand",
1212 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1216 MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
1218 static enum pxa3xx_nand_variant
1219 pxa3xx_nand_get_variant(struct platform_device *pdev)
1221 const struct of_device_id *of_id =
1222 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1224 return PXA3XX_NAND_VARIANT_PXA;
1225 return (enum pxa3xx_nand_variant)of_id->data;
1228 static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1230 struct pxa3xx_nand_platform_data *pdata;
1231 struct device_node *np = pdev->dev.of_node;
1232 const struct of_device_id *of_id =
1233 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1238 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1242 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1243 pdata->enable_arbiter = 1;
1244 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1245 pdata->keep_config = 1;
1246 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1248 pdev->dev.platform_data = pdata;
1253 static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1259 static int pxa3xx_nand_probe(struct platform_device *pdev)
1261 struct pxa3xx_nand_platform_data *pdata;
1262 struct mtd_part_parser_data ppdata = {};
1263 struct pxa3xx_nand_info *info;
1264 int ret, cs, probe_success;
1266 ret = pxa3xx_nand_probe_dt(pdev);
1270 pdata = dev_get_platdata(&pdev->dev);
1272 dev_err(&pdev->dev, "no platform data defined\n");
1276 ret = alloc_nand_resource(pdev);
1278 dev_err(&pdev->dev, "alloc nand resource failed\n");
1282 info = platform_get_drvdata(pdev);
1283 info->variant = pxa3xx_nand_get_variant(pdev);
1285 for (cs = 0; cs < pdata->num_cs; cs++) {
1287 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1289 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1294 ppdata.of_node = pdev->dev.of_node;
1295 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
1296 &ppdata, pdata->parts[cs],
1297 pdata->nr_parts[cs]);
1302 if (!probe_success) {
1303 pxa3xx_nand_remove(pdev);
1311 static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1313 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1314 struct pxa3xx_nand_platform_data *pdata;
1315 struct mtd_info *mtd;
1318 pdata = dev_get_platdata(&pdev->dev);
1320 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1324 for (cs = 0; cs < pdata->num_cs; cs++) {
1325 mtd = info->host[cs]->mtd;
1332 static int pxa3xx_nand_resume(struct platform_device *pdev)
1334 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1335 struct pxa3xx_nand_platform_data *pdata;
1336 struct mtd_info *mtd;
1339 pdata = dev_get_platdata(&pdev->dev);
1340 /* We don't want to handle interrupt without calling mtd routine */
1341 disable_int(info, NDCR_INT_MASK);
1344 * Directly set the chip select to a invalid value,
1345 * then the driver would reset the timing according
1346 * to current chip select at the beginning of cmdfunc
1351 * As the spec says, the NDSR would be updated to 0x1800 when
1352 * doing the nand_clk disable/enable.
1353 * To prevent it damaging state machine of the driver, clear
1354 * all status before resume
1356 nand_writel(info, NDSR, NDSR_MASK);
1357 for (cs = 0; cs < pdata->num_cs; cs++) {
1358 mtd = info->host[cs]->mtd;
1365 #define pxa3xx_nand_suspend NULL
1366 #define pxa3xx_nand_resume NULL
1369 static struct platform_driver pxa3xx_nand_driver = {
1371 .name = "pxa3xx-nand",
1372 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
1374 .probe = pxa3xx_nand_probe,
1375 .remove = pxa3xx_nand_remove,
1376 .suspend = pxa3xx_nand_suspend,
1377 .resume = pxa3xx_nand_resume,
1380 module_platform_driver(pxa3xx_nand_driver);
1382 MODULE_LICENSE("GPL");
1383 MODULE_DESCRIPTION("PXA3xx NAND controller driver");