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>
27 #include <plat/pxa3xx_nand.h>
29 #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
31 /* registers and bit definitions */
32 #define NDCR (0x00) /* Control register */
33 #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
34 #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
35 #define NDSR (0x14) /* Status Register */
36 #define NDPCR (0x18) /* Page Count Register */
37 #define NDBDR0 (0x1C) /* Bad Block Register 0 */
38 #define NDBDR1 (0x20) /* Bad Block Register 1 */
39 #define NDDB (0x40) /* Data Buffer */
40 #define NDCB0 (0x48) /* Command Buffer0 */
41 #define NDCB1 (0x4C) /* Command Buffer1 */
42 #define NDCB2 (0x50) /* Command Buffer2 */
44 #define NDCR_SPARE_EN (0x1 << 31)
45 #define NDCR_ECC_EN (0x1 << 30)
46 #define NDCR_DMA_EN (0x1 << 29)
47 #define NDCR_ND_RUN (0x1 << 28)
48 #define NDCR_DWIDTH_C (0x1 << 27)
49 #define NDCR_DWIDTH_M (0x1 << 26)
50 #define NDCR_PAGE_SZ (0x1 << 24)
51 #define NDCR_NCSX (0x1 << 23)
52 #define NDCR_ND_MODE (0x3 << 21)
53 #define NDCR_NAND_MODE (0x0)
54 #define NDCR_CLR_PG_CNT (0x1 << 20)
55 #define NDCR_CLR_ECC (0x1 << 19)
56 #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
57 #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
59 #define NDCR_RA_START (0x1 << 15)
60 #define NDCR_PG_PER_BLK (0x1 << 14)
61 #define NDCR_ND_ARB_EN (0x1 << 12)
63 #define NDSR_MASK (0xfff)
64 #define NDSR_RDY (0x1 << 11)
65 #define NDSR_CS0_PAGED (0x1 << 10)
66 #define NDSR_CS1_PAGED (0x1 << 9)
67 #define NDSR_CS0_CMDD (0x1 << 8)
68 #define NDSR_CS1_CMDD (0x1 << 7)
69 #define NDSR_CS0_BBD (0x1 << 6)
70 #define NDSR_CS1_BBD (0x1 << 5)
71 #define NDSR_DBERR (0x1 << 4)
72 #define NDSR_SBERR (0x1 << 3)
73 #define NDSR_WRDREQ (0x1 << 2)
74 #define NDSR_RDDREQ (0x1 << 1)
75 #define NDSR_WRCMDREQ (0x1)
77 #define NDCB0_AUTO_RS (0x1 << 25)
78 #define NDCB0_CSEL (0x1 << 24)
79 #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
80 #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
81 #define NDCB0_NC (0x1 << 20)
82 #define NDCB0_DBC (0x1 << 19)
83 #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
84 #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
85 #define NDCB0_CMD2_MASK (0xff << 8)
86 #define NDCB0_CMD1_MASK (0xff)
87 #define NDCB0_ADDR_CYC_SHIFT (16)
89 /* macros for registers read/write */
90 #define nand_writel(info, off, val) \
91 __raw_writel((val), (info)->mmio_base + (off))
93 #define nand_readl(info, off) \
94 __raw_readl((info)->mmio_base + (off))
96 /* error code and state */
116 struct pxa3xx_nand_info {
117 struct nand_chip nand_chip;
119 struct platform_device *pdev;
120 const struct pxa3xx_nand_flash *flash_info;
123 void __iomem *mmio_base;
124 unsigned long mmio_phys;
126 unsigned int buf_start;
127 unsigned int buf_count;
129 /* DMA information */
133 unsigned char *data_buff;
134 dma_addr_t data_buff_phys;
135 size_t data_buff_size;
137 struct pxa_dma_desc *data_desc;
138 dma_addr_t data_desc_addr;
142 /* saved column/page_addr during CMD_SEQIN */
146 /* relate to the command */
149 int use_ecc; /* use HW ECC ? */
150 int use_dma; /* use DMA ? */
152 size_t data_size; /* data size in FIFO */
154 struct completion cmd_complete;
156 /* generated NDCBx register values */
161 /* calculated from pxa3xx_nand_flash data */
163 size_t read_id_bytes;
165 unsigned int col_addr_cycles;
166 unsigned int row_addr_cycles;
169 static int use_dma = 1;
170 module_param(use_dma, bool, 0444);
171 MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
174 * Default NAND flash controller configuration setup by the
175 * bootloader. This configuration is used only when pdata->keep_config is set
177 static struct pxa3xx_nand_timing default_timing;
178 static struct pxa3xx_nand_flash default_flash;
179 static struct pxa3xx_nand_cmdset default_cmdset = {
183 .read_status = 0x0070,
189 .lock_status = 0x007A,
192 static struct pxa3xx_nand_timing timing[] = {
193 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
194 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
195 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
196 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
199 static struct pxa3xx_nand_flash builtin_flash_types[] = {
200 { 0, 0, 2048, 8, 8, 0, &default_cmdset, &timing[0] },
201 { 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset, &timing[1] },
202 { 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset, &timing[1] },
203 { 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset, &timing[1] },
204 { 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset, &timing[2] },
205 { 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset, &timing[2] },
206 { 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset, &timing[2] },
207 { 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset, &timing[2] },
208 { 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset, &timing[3] },
211 /* Define a default flash type setting serve as flash detecting only */
212 #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
214 #define NDTR0_tCH(c) (min((c), 7) << 19)
215 #define NDTR0_tCS(c) (min((c), 7) << 16)
216 #define NDTR0_tWH(c) (min((c), 7) << 11)
217 #define NDTR0_tWP(c) (min((c), 7) << 8)
218 #define NDTR0_tRH(c) (min((c), 7) << 3)
219 #define NDTR0_tRP(c) (min((c), 7) << 0)
221 #define NDTR1_tR(c) (min((c), 65535) << 16)
222 #define NDTR1_tWHR(c) (min((c), 15) << 4)
223 #define NDTR1_tAR(c) (min((c), 15) << 0)
225 #define tCH_NDTR0(r) (((r) >> 19) & 0x7)
226 #define tCS_NDTR0(r) (((r) >> 16) & 0x7)
227 #define tWH_NDTR0(r) (((r) >> 11) & 0x7)
228 #define tWP_NDTR0(r) (((r) >> 8) & 0x7)
229 #define tRH_NDTR0(r) (((r) >> 3) & 0x7)
230 #define tRP_NDTR0(r) (((r) >> 0) & 0x7)
232 #define tR_NDTR1(r) (((r) >> 16) & 0xffff)
233 #define tWHR_NDTR1(r) (((r) >> 4) & 0xf)
234 #define tAR_NDTR1(r) (((r) >> 0) & 0xf)
236 /* convert nano-seconds to nand flash controller clock cycles */
237 #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
239 /* convert nand flash controller clock cycles to nano-seconds */
240 #define cycle2ns(c, clk) ((((c) + 1) * 1000000 + clk / 500) / (clk / 1000))
242 static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
243 const struct pxa3xx_nand_timing *t)
245 unsigned long nand_clk = clk_get_rate(info->clk);
246 uint32_t ndtr0, ndtr1;
248 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
249 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
250 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
251 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
252 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
253 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
255 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
256 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
257 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
259 nand_writel(info, NDTR0CS0, ndtr0);
260 nand_writel(info, NDTR1CS0, ndtr1);
263 #define WAIT_EVENT_TIMEOUT 10
265 static int wait_for_event(struct pxa3xx_nand_info *info, uint32_t event)
267 int timeout = WAIT_EVENT_TIMEOUT;
271 ndsr = nand_readl(info, NDSR) & NDSR_MASK;
273 nand_writel(info, NDSR, ndsr);
282 static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
283 uint16_t cmd, int column, int page_addr)
285 const struct pxa3xx_nand_flash *f = info->flash_info;
286 const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
288 /* calculate data size */
289 switch (f->page_size) {
291 info->data_size = (info->use_ecc) ? 2088 : 2112;
294 info->data_size = (info->use_ecc) ? 520 : 528;
300 /* generate values for NDCBx registers */
301 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
304 info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
306 if (info->col_addr_cycles == 2) {
307 /* large block, 2 cycles for column address
308 * row address starts from 3rd cycle
310 info->ndcb1 |= page_addr << 16;
311 if (info->row_addr_cycles == 3)
312 info->ndcb2 = (page_addr >> 16) & 0xff;
314 /* small block, 1 cycles for column address
315 * row address starts from 2nd cycle
317 info->ndcb1 = page_addr << 8;
319 if (cmd == cmdset->program)
320 info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
325 static int prepare_erase_cmd(struct pxa3xx_nand_info *info,
326 uint16_t cmd, int page_addr)
328 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
329 info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
330 info->ndcb1 = page_addr;
335 static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
337 const struct pxa3xx_nand_cmdset *cmdset = info->flash_info->cmdset;
339 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
343 if (cmd == cmdset->read_id) {
344 info->ndcb0 |= NDCB0_CMD_TYPE(3);
346 } else if (cmd == cmdset->read_status) {
347 info->ndcb0 |= NDCB0_CMD_TYPE(4);
349 } else if (cmd == cmdset->reset || cmd == cmdset->lock ||
350 cmd == cmdset->unlock) {
351 info->ndcb0 |= NDCB0_CMD_TYPE(5);
358 static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
362 ndcr = nand_readl(info, NDCR);
363 nand_writel(info, NDCR, ndcr & ~int_mask);
366 static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
370 ndcr = nand_readl(info, NDCR);
371 nand_writel(info, NDCR, ndcr | int_mask);
374 /* NOTE: it is a must to set ND_RUN firstly, then write command buffer
375 * otherwise, it does not work
377 static int write_cmd(struct pxa3xx_nand_info *info)
381 /* clear status bits and run */
382 nand_writel(info, NDSR, NDSR_MASK);
384 ndcr = info->reg_ndcr;
386 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
387 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
390 nand_writel(info, NDCR, ndcr);
392 if (wait_for_event(info, NDSR_WRCMDREQ)) {
393 printk(KERN_ERR "timed out writing command\n");
397 nand_writel(info, NDCB0, info->ndcb0);
398 nand_writel(info, NDCB0, info->ndcb1);
399 nand_writel(info, NDCB0, info->ndcb2);
403 static int handle_data_pio(struct pxa3xx_nand_info *info)
405 int ret, timeout = CHIP_DELAY_TIMEOUT;
407 switch (info->state) {
408 case STATE_PIO_WRITING:
409 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
410 DIV_ROUND_UP(info->data_size, 4));
412 enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
414 ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
416 printk(KERN_ERR "program command time out\n");
420 case STATE_PIO_READING:
421 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
422 DIV_ROUND_UP(info->data_size, 4));
425 printk(KERN_ERR "%s: invalid state %d\n", __func__,
430 info->state = STATE_READY;
434 static void start_data_dma(struct pxa3xx_nand_info *info, int dir_out)
436 struct pxa_dma_desc *desc = info->data_desc;
437 int dma_len = ALIGN(info->data_size, 32);
439 desc->ddadr = DDADR_STOP;
440 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
443 desc->dsadr = info->data_buff_phys;
444 desc->dtadr = info->mmio_phys + NDDB;
445 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
447 desc->dtadr = info->data_buff_phys;
448 desc->dsadr = info->mmio_phys + NDDB;
449 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
452 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
453 DDADR(info->data_dma_ch) = info->data_desc_addr;
454 DCSR(info->data_dma_ch) |= DCSR_RUN;
457 static void pxa3xx_nand_data_dma_irq(int channel, void *data)
459 struct pxa3xx_nand_info *info = data;
462 dcsr = DCSR(channel);
463 DCSR(channel) = dcsr;
465 if (dcsr & DCSR_BUSERR) {
466 info->retcode = ERR_DMABUSERR;
467 complete(&info->cmd_complete);
470 if (info->state == STATE_DMA_WRITING) {
471 info->state = STATE_DMA_DONE;
472 enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
474 info->state = STATE_READY;
475 complete(&info->cmd_complete);
479 static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
481 struct pxa3xx_nand_info *info = devid;
484 status = nand_readl(info, NDSR);
486 if (status & (NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR)) {
487 if (status & NDSR_DBERR)
488 info->retcode = ERR_DBERR;
489 else if (status & NDSR_SBERR)
490 info->retcode = ERR_SBERR;
492 disable_int(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
495 info->state = STATE_DMA_READING;
496 start_data_dma(info, 0);
498 info->state = STATE_PIO_READING;
499 complete(&info->cmd_complete);
501 } else if (status & NDSR_WRDREQ) {
502 disable_int(info, NDSR_WRDREQ);
504 info->state = STATE_DMA_WRITING;
505 start_data_dma(info, 1);
507 info->state = STATE_PIO_WRITING;
508 complete(&info->cmd_complete);
510 } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
511 if (status & NDSR_CS0_BBD)
512 info->retcode = ERR_BBERR;
514 disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
515 info->state = STATE_READY;
516 complete(&info->cmd_complete);
518 nand_writel(info, NDSR, status);
522 static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
525 int ret, timeout = CHIP_DELAY_TIMEOUT;
527 if (write_cmd(info)) {
528 info->retcode = ERR_SENDCMD;
532 info->state = STATE_CMD_HANDLE;
534 enable_int(info, event);
536 ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
538 printk(KERN_ERR "command execution timed out\n");
539 info->retcode = ERR_SENDCMD;
543 if (info->use_dma == 0 && info->data_size > 0)
544 if (handle_data_pio(info))
550 ndcr = nand_readl(info, NDCR);
551 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
556 static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
558 struct pxa3xx_nand_info *info = mtd->priv;
559 return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
562 static inline int is_buf_blank(uint8_t *buf, size_t len)
564 for (; len > 0; len--)
570 static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
571 int column, int page_addr)
573 struct pxa3xx_nand_info *info = mtd->priv;
574 const struct pxa3xx_nand_flash *flash_info = info->flash_info;
575 const struct pxa3xx_nand_cmdset *cmdset = flash_info->cmdset;
578 info->use_dma = (use_dma) ? 1 : 0;
581 info->state = STATE_READY;
583 init_completion(&info->cmd_complete);
586 case NAND_CMD_READOOB:
587 /* disable HW ECC to get all the OOB data */
588 info->buf_count = mtd->writesize + mtd->oobsize;
589 info->buf_start = mtd->writesize + column;
590 memset(info->data_buff, 0xFF, info->buf_count);
592 if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
595 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
597 /* We only are OOB, so if the data has error, does not matter */
598 if (info->retcode == ERR_DBERR)
599 info->retcode = ERR_NONE;
604 info->retcode = ERR_NONE;
605 info->buf_start = column;
606 info->buf_count = mtd->writesize + mtd->oobsize;
607 memset(info->data_buff, 0xFF, info->buf_count);
609 if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
612 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
614 if (info->retcode == ERR_DBERR) {
615 /* for blank page (all 0xff), HW will calculate its ECC as
616 * 0, which is different from the ECC information within
617 * OOB, ignore such double bit errors
619 if (is_buf_blank(info->data_buff, mtd->writesize))
620 info->retcode = ERR_NONE;
624 info->buf_start = column;
625 info->buf_count = mtd->writesize + mtd->oobsize;
626 memset(info->data_buff, 0xff, info->buf_count);
628 /* save column/page_addr for next CMD_PAGEPROG */
629 info->seqin_column = column;
630 info->seqin_page_addr = page_addr;
632 case NAND_CMD_PAGEPROG:
633 info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
635 if (prepare_read_prog_cmd(info, cmdset->program,
636 info->seqin_column, info->seqin_page_addr))
639 pxa3xx_nand_do_cmd(info, NDSR_WRDREQ);
641 case NAND_CMD_ERASE1:
642 if (prepare_erase_cmd(info, cmdset->erase, page_addr))
645 pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
647 case NAND_CMD_ERASE2:
649 case NAND_CMD_READID:
650 case NAND_CMD_STATUS:
651 info->use_dma = 0; /* force PIO read */
653 info->buf_count = (command == NAND_CMD_READID) ?
654 info->read_id_bytes : 1;
656 if (prepare_other_cmd(info, (command == NAND_CMD_READID) ?
657 cmdset->read_id : cmdset->read_status))
660 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ);
663 if (prepare_other_cmd(info, cmdset->reset))
666 ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD);
672 if (nand_readl(info, NDSR) & NDSR_RDY)
677 ndcr = nand_readl(info, NDCR);
678 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
682 printk(KERN_ERR "non-supported command.\n");
686 if (info->retcode == ERR_DBERR) {
687 printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
688 info->retcode = ERR_NONE;
692 static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
694 struct pxa3xx_nand_info *info = mtd->priv;
697 if (info->buf_start < info->buf_count)
698 /* Has just send a new command? */
699 retval = info->data_buff[info->buf_start++];
704 static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
706 struct pxa3xx_nand_info *info = mtd->priv;
709 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
710 retval = *((u16 *)(info->data_buff+info->buf_start));
711 info->buf_start += 2;
716 static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
718 struct pxa3xx_nand_info *info = mtd->priv;
719 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
721 memcpy(buf, info->data_buff + info->buf_start, real_len);
722 info->buf_start += real_len;
725 static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
726 const uint8_t *buf, int len)
728 struct pxa3xx_nand_info *info = mtd->priv;
729 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
731 memcpy(info->data_buff + info->buf_start, buf, real_len);
732 info->buf_start += real_len;
735 static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
736 const uint8_t *buf, int len)
741 static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
746 static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
748 struct pxa3xx_nand_info *info = mtd->priv;
750 /* pxa3xx_nand_send_command has waited for command complete */
751 if (this->state == FL_WRITING || this->state == FL_ERASING) {
752 if (info->retcode == ERR_NONE)
756 * any error make it return 0x01 which will tell
757 * the caller the erase and write fail
766 static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
771 static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
772 const uint8_t *dat, uint8_t *ecc_code)
777 static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
778 uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
780 struct pxa3xx_nand_info *info = mtd->priv;
782 * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
783 * consider it as a ecc error which will tell the caller the
784 * read fail We have distinguish all the errors, but the
785 * nand_read_ecc only check this function return value
787 * Corrected (single-bit) errors must also be noted.
789 if (info->retcode == ERR_SBERR)
791 else if (info->retcode != ERR_NONE)
797 static int __readid(struct pxa3xx_nand_info *info, uint32_t *id)
799 const struct pxa3xx_nand_flash *f = info->flash_info;
800 const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
804 if (prepare_other_cmd(info, cmdset->read_id)) {
805 printk(KERN_ERR "failed to prepare command\n");
813 /* Wait for CMDDM(command done successfully) */
814 if (wait_for_event(info, NDSR_RDDREQ))
817 __raw_readsl(info->mmio_base + NDDB, id_buff, 2);
818 *id = id_buff[0] | (id_buff[1] << 8);
822 ndcr = nand_readl(info, NDCR);
823 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
828 static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
829 const struct pxa3xx_nand_flash *f)
831 struct platform_device *pdev = info->pdev;
832 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
833 uint32_t ndcr = 0x00000FFF; /* disable all interrupts */
835 if (f->page_size != 2048 && f->page_size != 512)
838 if (f->flash_width != 16 && f->flash_width != 8)
841 /* calculate flash information */
842 info->oob_size = (f->page_size == 2048) ? 64 : 16;
843 info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
845 /* calculate addressing information */
846 info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
848 if (f->num_blocks * f->page_per_block > 65536)
849 info->row_addr_cycles = 3;
851 info->row_addr_cycles = 2;
853 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
854 ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
855 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
856 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
857 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
858 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
860 ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
861 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
863 info->reg_ndcr = ndcr;
865 pxa3xx_nand_set_timing(info, f->timing);
866 info->flash_info = f;
870 static void pxa3xx_nand_detect_timing(struct pxa3xx_nand_info *info,
871 struct pxa3xx_nand_timing *t)
873 unsigned long nand_clk = clk_get_rate(info->clk);
874 uint32_t ndtr0 = nand_readl(info, NDTR0CS0);
875 uint32_t ndtr1 = nand_readl(info, NDTR1CS0);
877 t->tCH = cycle2ns(tCH_NDTR0(ndtr0), nand_clk);
878 t->tCS = cycle2ns(tCS_NDTR0(ndtr0), nand_clk);
879 t->tWH = cycle2ns(tWH_NDTR0(ndtr0), nand_clk);
880 t->tWP = cycle2ns(tWP_NDTR0(ndtr0), nand_clk);
881 t->tRH = cycle2ns(tRH_NDTR0(ndtr0), nand_clk);
882 t->tRP = cycle2ns(tRP_NDTR0(ndtr0), nand_clk);
884 t->tR = cycle2ns(tR_NDTR1(ndtr1), nand_clk);
885 t->tWHR = cycle2ns(tWHR_NDTR1(ndtr1), nand_clk);
886 t->tAR = cycle2ns(tAR_NDTR1(ndtr1), nand_clk);
889 static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
891 uint32_t ndcr = nand_readl(info, NDCR);
892 struct nand_flash_dev *type = NULL;
896 default_flash.page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
897 default_flash.page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
898 default_flash.flash_width = ndcr & NDCR_DWIDTH_M ? 16 : 8;
899 default_flash.dfc_width = ndcr & NDCR_DWIDTH_C ? 16 : 8;
901 /* set info fields needed to __readid */
902 info->flash_info = &default_flash;
903 info->read_id_bytes = (default_flash.page_size == 2048) ? 4 : 2;
904 info->reg_ndcr = ndcr;
906 if (__readid(info, &id))
909 /* Lookup the flash id */
910 id = (id >> 8) & 0xff; /* device id is byte 2 */
911 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
912 if (id == nand_flash_ids[i].id) {
913 type = &nand_flash_ids[i];
921 /* fill the missing flash information */
922 i = __ffs(default_flash.page_per_block * default_flash.page_size);
923 default_flash.num_blocks = type->chipsize << (20 - i);
925 info->oob_size = (default_flash.page_size == 2048) ? 64 : 16;
927 /* calculate addressing information */
928 info->col_addr_cycles = (default_flash.page_size == 2048) ? 2 : 1;
930 if (default_flash.num_blocks * default_flash.page_per_block > 65536)
931 info->row_addr_cycles = 3;
933 info->row_addr_cycles = 2;
935 pxa3xx_nand_detect_timing(info, &default_timing);
936 default_flash.timing = &default_timing;
937 default_flash.cmdset = &default_cmdset;
942 static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
943 const struct pxa3xx_nand_platform_data *pdata)
945 const struct pxa3xx_nand_flash *f;
949 if (pdata->keep_config)
950 if (pxa3xx_nand_detect_config(info) == 0)
953 /* we use default timing to detect id */
954 f = DEFAULT_FLASH_TYPE;
955 pxa3xx_nand_config_flash(info, f);
956 if (__readid(info, &id))
959 for (i=0; i<ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; i++) {
960 /* we first choose the flash definition from platfrom */
961 if (i < pdata->num_flash)
962 f = pdata->flash + i;
964 f = &builtin_flash_types[i - pdata->num_flash + 1];
965 if (f->chip_id == id) {
966 dev_info(&info->pdev->dev, "detect chip id: 0x%x\n", id);
967 pxa3xx_nand_config_flash(info, f);
972 dev_warn(&info->pdev->dev,
973 "failed to detect configured nand flash; found %04x instead of\n",
979 /* the maximum possible buffer size for large page with OOB data
980 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
981 * data buffer and the DMA descriptor
983 #define MAX_BUFF_SIZE PAGE_SIZE
985 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
987 struct platform_device *pdev = info->pdev;
988 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
991 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
992 if (info->data_buff == NULL)
997 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
998 &info->data_buff_phys, GFP_KERNEL);
999 if (info->data_buff == NULL) {
1000 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1004 info->data_buff_size = MAX_BUFF_SIZE;
1005 info->data_desc = (void *)info->data_buff + data_desc_offset;
1006 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1008 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1009 pxa3xx_nand_data_dma_irq, info);
1010 if (info->data_dma_ch < 0) {
1011 dev_err(&pdev->dev, "failed to request data dma\n");
1012 dma_free_coherent(&pdev->dev, info->data_buff_size,
1013 info->data_buff, info->data_buff_phys);
1014 return info->data_dma_ch;
1020 static struct nand_ecclayout hw_smallpage_ecclayout = {
1022 .eccpos = {8, 9, 10, 11, 12, 13 },
1023 .oobfree = { {2, 6} }
1026 static struct nand_ecclayout hw_largepage_ecclayout = {
1029 40, 41, 42, 43, 44, 45, 46, 47,
1030 48, 49, 50, 51, 52, 53, 54, 55,
1031 56, 57, 58, 59, 60, 61, 62, 63},
1032 .oobfree = { {2, 38} }
1035 static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
1036 struct pxa3xx_nand_info *info)
1038 const struct pxa3xx_nand_flash *f = info->flash_info;
1039 struct nand_chip *this = &info->nand_chip;
1041 this->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16: 0;
1043 this->waitfunc = pxa3xx_nand_waitfunc;
1044 this->select_chip = pxa3xx_nand_select_chip;
1045 this->dev_ready = pxa3xx_nand_dev_ready;
1046 this->cmdfunc = pxa3xx_nand_cmdfunc;
1047 this->read_word = pxa3xx_nand_read_word;
1048 this->read_byte = pxa3xx_nand_read_byte;
1049 this->read_buf = pxa3xx_nand_read_buf;
1050 this->write_buf = pxa3xx_nand_write_buf;
1051 this->verify_buf = pxa3xx_nand_verify_buf;
1053 this->ecc.mode = NAND_ECC_HW;
1054 this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
1055 this->ecc.calculate = pxa3xx_nand_ecc_calculate;
1056 this->ecc.correct = pxa3xx_nand_ecc_correct;
1057 this->ecc.size = f->page_size;
1059 if (f->page_size == 2048)
1060 this->ecc.layout = &hw_largepage_ecclayout;
1062 this->ecc.layout = &hw_smallpage_ecclayout;
1064 this->chip_delay = 25;
1067 static int pxa3xx_nand_probe(struct platform_device *pdev)
1069 struct pxa3xx_nand_platform_data *pdata;
1070 struct pxa3xx_nand_info *info;
1071 struct nand_chip *this;
1072 struct mtd_info *mtd;
1076 pdata = pdev->dev.platform_data;
1079 dev_err(&pdev->dev, "no platform data defined\n");
1083 mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
1086 dev_err(&pdev->dev, "failed to allocate memory\n");
1090 info = (struct pxa3xx_nand_info *)(&mtd[1]);
1093 this = &info->nand_chip;
1095 mtd->owner = THIS_MODULE;
1097 info->clk = clk_get(&pdev->dev, NULL);
1098 if (IS_ERR(info->clk)) {
1099 dev_err(&pdev->dev, "failed to get nand clock\n");
1100 ret = PTR_ERR(info->clk);
1103 clk_enable(info->clk);
1105 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1107 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1111 info->drcmr_dat = r->start;
1113 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1115 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1119 info->drcmr_cmd = r->start;
1121 irq = platform_get_irq(pdev, 0);
1123 dev_err(&pdev->dev, "no IRQ resource defined\n");
1128 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1130 dev_err(&pdev->dev, "no IO memory resource defined\n");
1135 r = request_mem_region(r->start, resource_size(r), pdev->name);
1137 dev_err(&pdev->dev, "failed to request memory resource\n");
1142 info->mmio_base = ioremap(r->start, resource_size(r));
1143 if (info->mmio_base == NULL) {
1144 dev_err(&pdev->dev, "ioremap() failed\n");
1148 info->mmio_phys = r->start;
1150 ret = pxa3xx_nand_init_buff(info);
1154 /* initialize all interrupts to be disabled */
1155 disable_int(info, NDSR_MASK);
1157 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1160 dev_err(&pdev->dev, "failed to request IRQ\n");
1164 ret = pxa3xx_nand_detect_flash(info, pdata);
1166 dev_err(&pdev->dev, "failed to detect flash\n");
1171 pxa3xx_nand_init_mtd(mtd, info);
1173 platform_set_drvdata(pdev, mtd);
1175 if (nand_scan(mtd, 1)) {
1176 dev_err(&pdev->dev, "failed to scan nand\n");
1181 #ifdef CONFIG_MTD_PARTITIONS
1182 if (mtd_has_cmdlinepart()) {
1183 static const char *probes[] = { "cmdlinepart", NULL };
1184 struct mtd_partition *parts;
1187 nr_parts = parse_mtd_partitions(mtd, probes, &parts, 0);
1190 return add_mtd_partitions(mtd, parts, nr_parts);
1193 return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
1199 free_irq(irq, info);
1202 pxa_free_dma(info->data_dma_ch);
1203 dma_free_coherent(&pdev->dev, info->data_buff_size,
1204 info->data_buff, info->data_buff_phys);
1206 kfree(info->data_buff);
1208 iounmap(info->mmio_base);
1210 release_mem_region(r->start, resource_size(r));
1212 clk_disable(info->clk);
1219 static int pxa3xx_nand_remove(struct platform_device *pdev)
1221 struct mtd_info *mtd = platform_get_drvdata(pdev);
1222 struct pxa3xx_nand_info *info = mtd->priv;
1226 platform_set_drvdata(pdev, NULL);
1228 del_mtd_device(mtd);
1229 #ifdef CONFIG_MTD_PARTITIONS
1230 del_mtd_partitions(mtd);
1232 irq = platform_get_irq(pdev, 0);
1234 free_irq(irq, info);
1236 pxa_free_dma(info->data_dma_ch);
1237 dma_free_writecombine(&pdev->dev, info->data_buff_size,
1238 info->data_buff, info->data_buff_phys);
1240 kfree(info->data_buff);
1242 iounmap(info->mmio_base);
1243 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1244 release_mem_region(r->start, resource_size(r));
1246 clk_disable(info->clk);
1254 static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1256 struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1257 struct pxa3xx_nand_info *info = mtd->priv;
1259 if (info->state != STATE_READY) {
1260 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1267 static int pxa3xx_nand_resume(struct platform_device *pdev)
1269 struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1270 struct pxa3xx_nand_info *info = mtd->priv;
1272 clk_enable(info->clk);
1274 return pxa3xx_nand_config_flash(info, info->flash_info);
1277 #define pxa3xx_nand_suspend NULL
1278 #define pxa3xx_nand_resume NULL
1281 static struct platform_driver pxa3xx_nand_driver = {
1283 .name = "pxa3xx-nand",
1285 .probe = pxa3xx_nand_probe,
1286 .remove = pxa3xx_nand_remove,
1287 .suspend = pxa3xx_nand_suspend,
1288 .resume = pxa3xx_nand_resume,
1291 static int __init pxa3xx_nand_init(void)
1293 return platform_driver_register(&pxa3xx_nand_driver);
1295 module_init(pxa3xx_nand_init);
1297 static void __exit pxa3xx_nand_exit(void)
1299 platform_driver_unregister(&pxa3xx_nand_driver);
1301 module_exit(pxa3xx_nand_exit);
1303 MODULE_LICENSE("GPL");
1304 MODULE_DESCRIPTION("PXA3xx NAND controller driver");