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 struct pxa3xx_nand_cmdset
*cmdset
;
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 unsigned char *oob_buff
;
135 dma_addr_t data_buff_phys
;
136 size_t data_buff_size
;
138 struct pxa_dma_desc
*data_desc
;
139 dma_addr_t data_desc_addr
;
143 /* saved column/page_addr during CMD_SEQIN */
147 /* relate to the command */
150 int use_ecc
; /* use HW ECC ? */
151 int use_dma
; /* use DMA ? */
153 unsigned int page_size
; /* page size of attached chip */
154 unsigned int data_size
; /* data size in FIFO */
156 struct completion cmd_complete
;
158 /* generated NDCBx register values */
163 /* timing calcuted from setting */
167 /* calculated from pxa3xx_nand_flash data */
169 size_t read_id_bytes
;
171 unsigned int col_addr_cycles
;
172 unsigned int row_addr_cycles
;
175 static int use_dma
= 1;
176 module_param(use_dma
, bool, 0444);
177 MODULE_PARM_DESC(use_dma
, "enable DMA for data transfering to/from NAND HW");
180 * Default NAND flash controller configuration setup by the
181 * bootloader. This configuration is used only when pdata->keep_config is set
183 static struct pxa3xx_nand_cmdset default_cmdset
= {
187 .read_status
= 0x0070,
193 .lock_status
= 0x007A,
196 static struct pxa3xx_nand_timing timing
[] = {
197 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
198 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
199 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
200 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
203 static struct pxa3xx_nand_flash builtin_flash_types
[] = {
204 { 0, 0, 2048, 8, 8, 0, &default_cmdset
, &timing
[0] },
205 { 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset
, &timing
[1] },
206 { 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset
, &timing
[1] },
207 { 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset
, &timing
[1] },
208 { 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset
, &timing
[2] },
209 { 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset
, &timing
[2] },
210 { 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset
, &timing
[2] },
211 { 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset
, &timing
[2] },
212 { 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset
, &timing
[3] },
215 /* Define a default flash type setting serve as flash detecting only */
216 #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
218 #define NDTR0_tCH(c) (min((c), 7) << 19)
219 #define NDTR0_tCS(c) (min((c), 7) << 16)
220 #define NDTR0_tWH(c) (min((c), 7) << 11)
221 #define NDTR0_tWP(c) (min((c), 7) << 8)
222 #define NDTR0_tRH(c) (min((c), 7) << 3)
223 #define NDTR0_tRP(c) (min((c), 7) << 0)
225 #define NDTR1_tR(c) (min((c), 65535) << 16)
226 #define NDTR1_tWHR(c) (min((c), 15) << 4)
227 #define NDTR1_tAR(c) (min((c), 15) << 0)
229 /* convert nano-seconds to nand flash controller clock cycles */
230 #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
232 static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info
*info
,
233 const struct pxa3xx_nand_timing
*t
)
235 unsigned long nand_clk
= clk_get_rate(info
->clk
);
236 uint32_t ndtr0
, ndtr1
;
238 ndtr0
= NDTR0_tCH(ns2cycle(t
->tCH
, nand_clk
)) |
239 NDTR0_tCS(ns2cycle(t
->tCS
, nand_clk
)) |
240 NDTR0_tWH(ns2cycle(t
->tWH
, nand_clk
)) |
241 NDTR0_tWP(ns2cycle(t
->tWP
, nand_clk
)) |
242 NDTR0_tRH(ns2cycle(t
->tRH
, nand_clk
)) |
243 NDTR0_tRP(ns2cycle(t
->tRP
, nand_clk
));
245 ndtr1
= NDTR1_tR(ns2cycle(t
->tR
, nand_clk
)) |
246 NDTR1_tWHR(ns2cycle(t
->tWHR
, nand_clk
)) |
247 NDTR1_tAR(ns2cycle(t
->tAR
, nand_clk
));
249 info
->ndtr0cs0
= ndtr0
;
250 info
->ndtr1cs0
= ndtr1
;
251 nand_writel(info
, NDTR0CS0
, ndtr0
);
252 nand_writel(info
, NDTR1CS0
, ndtr1
);
255 #define WAIT_EVENT_TIMEOUT 10
257 static int wait_for_event(struct pxa3xx_nand_info
*info
, uint32_t event
)
259 int timeout
= WAIT_EVENT_TIMEOUT
;
263 ndsr
= nand_readl(info
, NDSR
) & NDSR_MASK
;
265 nand_writel(info
, NDSR
, ndsr
);
274 static void pxa3xx_set_datasize(struct pxa3xx_nand_info
*info
)
276 int oob_enable
= info
->reg_ndcr
& NDCR_SPARE_EN
;
278 info
->data_size
= info
->page_size
;
284 switch (info
->page_size
) {
286 info
->oob_size
= (info
->use_ecc
) ? 40 : 64;
289 info
->oob_size
= (info
->use_ecc
) ? 8 : 16;
294 static int prepare_read_prog_cmd(struct pxa3xx_nand_info
*info
,
295 uint16_t cmd
, int column
, int page_addr
)
297 const struct pxa3xx_nand_cmdset
*cmdset
= info
->cmdset
;
298 pxa3xx_set_datasize(info
);
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
->cmdset
;
339 info
->ndcb0
= cmd
| ((cmd
& 0xff00) ? NDCB0_DBC
: 0);
344 if (cmd
== cmdset
->read_id
) {
345 info
->ndcb0
|= NDCB0_CMD_TYPE(3);
347 } else if (cmd
== cmdset
->read_status
) {
348 info
->ndcb0
|= NDCB0_CMD_TYPE(4);
350 } else if (cmd
== cmdset
->reset
|| cmd
== cmdset
->lock
||
351 cmd
== cmdset
->unlock
) {
352 info
->ndcb0
|= NDCB0_CMD_TYPE(5);
359 static void enable_int(struct pxa3xx_nand_info
*info
, uint32_t int_mask
)
363 ndcr
= nand_readl(info
, NDCR
);
364 nand_writel(info
, NDCR
, ndcr
& ~int_mask
);
367 static void disable_int(struct pxa3xx_nand_info
*info
, uint32_t int_mask
)
371 ndcr
= nand_readl(info
, NDCR
);
372 nand_writel(info
, NDCR
, ndcr
| int_mask
);
375 /* NOTE: it is a must to set ND_RUN firstly, then write command buffer
376 * otherwise, it does not work
378 static int write_cmd(struct pxa3xx_nand_info
*info
)
382 /* clear status bits and run */
383 nand_writel(info
, NDSR
, NDSR_MASK
);
385 ndcr
= info
->reg_ndcr
;
387 ndcr
|= info
->use_ecc
? NDCR_ECC_EN
: 0;
388 ndcr
|= info
->use_dma
? NDCR_DMA_EN
: 0;
391 nand_writel(info
, NDCR
, ndcr
);
393 if (wait_for_event(info
, NDSR_WRCMDREQ
)) {
394 printk(KERN_ERR
"timed out writing command\n");
398 nand_writel(info
, NDCB0
, info
->ndcb0
);
399 nand_writel(info
, NDCB0
, info
->ndcb1
);
400 nand_writel(info
, NDCB0
, info
->ndcb2
);
404 static int handle_data_pio(struct pxa3xx_nand_info
*info
)
406 int ret
, timeout
= CHIP_DELAY_TIMEOUT
;
408 switch (info
->state
) {
409 case STATE_PIO_WRITING
:
410 __raw_writesl(info
->mmio_base
+ NDDB
, info
->data_buff
,
411 DIV_ROUND_UP(info
->data_size
, 4));
412 if (info
->oob_size
> 0)
413 __raw_writesl(info
->mmio_base
+ NDDB
, info
->oob_buff
,
414 DIV_ROUND_UP(info
->oob_size
, 4));
416 enable_int(info
, NDSR_CS0_BBD
| NDSR_CS0_CMDD
);
418 ret
= wait_for_completion_timeout(&info
->cmd_complete
, timeout
);
420 printk(KERN_ERR
"program command time out\n");
424 case STATE_PIO_READING
:
425 __raw_readsl(info
->mmio_base
+ NDDB
, info
->data_buff
,
426 DIV_ROUND_UP(info
->data_size
, 4));
427 if (info
->oob_size
> 0)
428 __raw_readsl(info
->mmio_base
+ NDDB
, info
->oob_buff
,
429 DIV_ROUND_UP(info
->oob_size
, 4));
432 printk(KERN_ERR
"%s: invalid state %d\n", __func__
,
437 info
->state
= STATE_READY
;
441 static void start_data_dma(struct pxa3xx_nand_info
*info
, int dir_out
)
443 struct pxa_dma_desc
*desc
= info
->data_desc
;
444 int dma_len
= ALIGN(info
->data_size
+ info
->oob_size
, 32);
446 desc
->ddadr
= DDADR_STOP
;
447 desc
->dcmd
= DCMD_ENDIRQEN
| DCMD_WIDTH4
| DCMD_BURST32
| dma_len
;
450 desc
->dsadr
= info
->data_buff_phys
;
451 desc
->dtadr
= info
->mmio_phys
+ NDDB
;
452 desc
->dcmd
|= DCMD_INCSRCADDR
| DCMD_FLOWTRG
;
454 desc
->dtadr
= info
->data_buff_phys
;
455 desc
->dsadr
= info
->mmio_phys
+ NDDB
;
456 desc
->dcmd
|= DCMD_INCTRGADDR
| DCMD_FLOWSRC
;
459 DRCMR(info
->drcmr_dat
) = DRCMR_MAPVLD
| info
->data_dma_ch
;
460 DDADR(info
->data_dma_ch
) = info
->data_desc_addr
;
461 DCSR(info
->data_dma_ch
) |= DCSR_RUN
;
464 static void pxa3xx_nand_data_dma_irq(int channel
, void *data
)
466 struct pxa3xx_nand_info
*info
= data
;
469 dcsr
= DCSR(channel
);
470 DCSR(channel
) = dcsr
;
472 if (dcsr
& DCSR_BUSERR
) {
473 info
->retcode
= ERR_DMABUSERR
;
474 complete(&info
->cmd_complete
);
477 if (info
->state
== STATE_DMA_WRITING
) {
478 info
->state
= STATE_DMA_DONE
;
479 enable_int(info
, NDSR_CS0_BBD
| NDSR_CS0_CMDD
);
481 info
->state
= STATE_READY
;
482 complete(&info
->cmd_complete
);
486 static irqreturn_t
pxa3xx_nand_irq(int irq
, void *devid
)
488 struct pxa3xx_nand_info
*info
= devid
;
491 status
= nand_readl(info
, NDSR
);
493 if (status
& (NDSR_RDDREQ
| NDSR_DBERR
| NDSR_SBERR
)) {
494 if (status
& NDSR_DBERR
)
495 info
->retcode
= ERR_DBERR
;
496 else if (status
& NDSR_SBERR
)
497 info
->retcode
= ERR_SBERR
;
499 disable_int(info
, NDSR_RDDREQ
| NDSR_DBERR
| NDSR_SBERR
);
502 info
->state
= STATE_DMA_READING
;
503 start_data_dma(info
, 0);
505 info
->state
= STATE_PIO_READING
;
506 complete(&info
->cmd_complete
);
508 } else if (status
& NDSR_WRDREQ
) {
509 disable_int(info
, NDSR_WRDREQ
);
511 info
->state
= STATE_DMA_WRITING
;
512 start_data_dma(info
, 1);
514 info
->state
= STATE_PIO_WRITING
;
515 complete(&info
->cmd_complete
);
517 } else if (status
& (NDSR_CS0_BBD
| NDSR_CS0_CMDD
)) {
518 if (status
& NDSR_CS0_BBD
)
519 info
->retcode
= ERR_BBERR
;
521 disable_int(info
, NDSR_CS0_BBD
| NDSR_CS0_CMDD
);
522 info
->state
= STATE_READY
;
523 complete(&info
->cmd_complete
);
525 nand_writel(info
, NDSR
, status
);
529 static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info
*info
, uint32_t event
)
532 int ret
, timeout
= CHIP_DELAY_TIMEOUT
;
534 if (write_cmd(info
)) {
535 info
->retcode
= ERR_SENDCMD
;
539 info
->state
= STATE_CMD_HANDLE
;
541 enable_int(info
, event
);
543 ret
= wait_for_completion_timeout(&info
->cmd_complete
, timeout
);
545 printk(KERN_ERR
"command execution timed out\n");
546 info
->retcode
= ERR_SENDCMD
;
550 if (info
->use_dma
== 0 && info
->data_size
> 0)
551 if (handle_data_pio(info
))
557 ndcr
= nand_readl(info
, NDCR
);
558 nand_writel(info
, NDCR
, ndcr
& ~NDCR_ND_RUN
);
563 static int pxa3xx_nand_dev_ready(struct mtd_info
*mtd
)
565 struct pxa3xx_nand_info
*info
= mtd
->priv
;
566 return (nand_readl(info
, NDSR
) & NDSR_RDY
) ? 1 : 0;
569 static inline int is_buf_blank(uint8_t *buf
, size_t len
)
571 for (; len
> 0; len
--)
577 static void pxa3xx_nand_cmdfunc(struct mtd_info
*mtd
, unsigned command
,
578 int column
, int page_addr
)
580 struct pxa3xx_nand_info
*info
= mtd
->priv
;
581 const struct pxa3xx_nand_cmdset
*cmdset
= info
->cmdset
;
584 info
->use_dma
= (use_dma
) ? 1 : 0;
587 info
->state
= STATE_READY
;
589 init_completion(&info
->cmd_complete
);
592 case NAND_CMD_READOOB
:
593 /* disable HW ECC to get all the OOB data */
594 info
->buf_count
= mtd
->writesize
+ mtd
->oobsize
;
595 info
->buf_start
= mtd
->writesize
+ column
;
596 memset(info
->data_buff
, 0xFF, info
->buf_count
);
598 if (prepare_read_prog_cmd(info
, cmdset
->read1
, column
, page_addr
))
601 pxa3xx_nand_do_cmd(info
, NDSR_RDDREQ
| NDSR_DBERR
| NDSR_SBERR
);
603 /* We only are OOB, so if the data has error, does not matter */
604 if (info
->retcode
== ERR_DBERR
)
605 info
->retcode
= ERR_NONE
;
610 info
->retcode
= ERR_NONE
;
611 info
->buf_start
= column
;
612 info
->buf_count
= mtd
->writesize
+ mtd
->oobsize
;
613 memset(info
->data_buff
, 0xFF, info
->buf_count
);
615 if (prepare_read_prog_cmd(info
, cmdset
->read1
, column
, page_addr
))
618 pxa3xx_nand_do_cmd(info
, NDSR_RDDREQ
| NDSR_DBERR
| NDSR_SBERR
);
620 if (info
->retcode
== ERR_DBERR
) {
621 /* for blank page (all 0xff), HW will calculate its ECC as
622 * 0, which is different from the ECC information within
623 * OOB, ignore such double bit errors
625 if (is_buf_blank(info
->data_buff
, mtd
->writesize
))
626 info
->retcode
= ERR_NONE
;
630 info
->buf_start
= column
;
631 info
->buf_count
= mtd
->writesize
+ mtd
->oobsize
;
632 memset(info
->data_buff
, 0xff, info
->buf_count
);
634 /* save column/page_addr for next CMD_PAGEPROG */
635 info
->seqin_column
= column
;
636 info
->seqin_page_addr
= page_addr
;
638 case NAND_CMD_PAGEPROG
:
639 info
->use_ecc
= (info
->seqin_column
>= mtd
->writesize
) ? 0 : 1;
641 if (prepare_read_prog_cmd(info
, cmdset
->program
,
642 info
->seqin_column
, info
->seqin_page_addr
))
645 pxa3xx_nand_do_cmd(info
, NDSR_WRDREQ
);
647 case NAND_CMD_ERASE1
:
648 if (prepare_erase_cmd(info
, cmdset
->erase
, page_addr
))
651 pxa3xx_nand_do_cmd(info
, NDSR_CS0_BBD
| NDSR_CS0_CMDD
);
653 case NAND_CMD_ERASE2
:
655 case NAND_CMD_READID
:
656 case NAND_CMD_STATUS
:
657 info
->use_dma
= 0; /* force PIO read */
659 info
->buf_count
= (command
== NAND_CMD_READID
) ?
660 info
->read_id_bytes
: 1;
662 if (prepare_other_cmd(info
, (command
== NAND_CMD_READID
) ?
663 cmdset
->read_id
: cmdset
->read_status
))
666 pxa3xx_nand_do_cmd(info
, NDSR_RDDREQ
);
669 if (prepare_other_cmd(info
, cmdset
->reset
))
672 ret
= pxa3xx_nand_do_cmd(info
, NDSR_CS0_CMDD
);
678 if (nand_readl(info
, NDSR
) & NDSR_RDY
)
683 ndcr
= nand_readl(info
, NDCR
);
684 nand_writel(info
, NDCR
, ndcr
& ~NDCR_ND_RUN
);
688 printk(KERN_ERR
"non-supported command.\n");
692 if (info
->retcode
== ERR_DBERR
) {
693 printk(KERN_ERR
"double bit error @ page %08x\n", page_addr
);
694 info
->retcode
= ERR_NONE
;
698 static uint8_t pxa3xx_nand_read_byte(struct mtd_info
*mtd
)
700 struct pxa3xx_nand_info
*info
= mtd
->priv
;
703 if (info
->buf_start
< info
->buf_count
)
704 /* Has just send a new command? */
705 retval
= info
->data_buff
[info
->buf_start
++];
710 static u16
pxa3xx_nand_read_word(struct mtd_info
*mtd
)
712 struct pxa3xx_nand_info
*info
= mtd
->priv
;
715 if (!(info
->buf_start
& 0x01) && info
->buf_start
< info
->buf_count
) {
716 retval
= *((u16
*)(info
->data_buff
+info
->buf_start
));
717 info
->buf_start
+= 2;
722 static void pxa3xx_nand_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
724 struct pxa3xx_nand_info
*info
= mtd
->priv
;
725 int real_len
= min_t(size_t, len
, info
->buf_count
- info
->buf_start
);
727 memcpy(buf
, info
->data_buff
+ info
->buf_start
, real_len
);
728 info
->buf_start
+= real_len
;
731 static void pxa3xx_nand_write_buf(struct mtd_info
*mtd
,
732 const uint8_t *buf
, int len
)
734 struct pxa3xx_nand_info
*info
= mtd
->priv
;
735 int real_len
= min_t(size_t, len
, info
->buf_count
- info
->buf_start
);
737 memcpy(info
->data_buff
+ info
->buf_start
, buf
, real_len
);
738 info
->buf_start
+= real_len
;
741 static int pxa3xx_nand_verify_buf(struct mtd_info
*mtd
,
742 const uint8_t *buf
, int len
)
747 static void pxa3xx_nand_select_chip(struct mtd_info
*mtd
, int chip
)
752 static int pxa3xx_nand_waitfunc(struct mtd_info
*mtd
, struct nand_chip
*this)
754 struct pxa3xx_nand_info
*info
= mtd
->priv
;
756 /* pxa3xx_nand_send_command has waited for command complete */
757 if (this->state
== FL_WRITING
|| this->state
== FL_ERASING
) {
758 if (info
->retcode
== ERR_NONE
)
762 * any error make it return 0x01 which will tell
763 * the caller the erase and write fail
772 static void pxa3xx_nand_ecc_hwctl(struct mtd_info
*mtd
, int mode
)
777 static int pxa3xx_nand_ecc_calculate(struct mtd_info
*mtd
,
778 const uint8_t *dat
, uint8_t *ecc_code
)
783 static int pxa3xx_nand_ecc_correct(struct mtd_info
*mtd
,
784 uint8_t *dat
, uint8_t *read_ecc
, uint8_t *calc_ecc
)
786 struct pxa3xx_nand_info
*info
= mtd
->priv
;
788 * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
789 * consider it as a ecc error which will tell the caller the
790 * read fail We have distinguish all the errors, but the
791 * nand_read_ecc only check this function return value
793 * Corrected (single-bit) errors must also be noted.
795 if (info
->retcode
== ERR_SBERR
)
797 else if (info
->retcode
!= ERR_NONE
)
803 static int __readid(struct pxa3xx_nand_info
*info
, uint32_t *id
)
805 const struct pxa3xx_nand_cmdset
*cmdset
= info
->cmdset
;
809 if (prepare_other_cmd(info
, cmdset
->read_id
)) {
810 printk(KERN_ERR
"failed to prepare command\n");
818 /* Wait for CMDDM(command done successfully) */
819 if (wait_for_event(info
, NDSR_RDDREQ
))
822 __raw_readsl(info
->mmio_base
+ NDDB
, id_buff
, 2);
823 *id
= id_buff
[0] | (id_buff
[1] << 8);
827 ndcr
= nand_readl(info
, NDCR
);
828 nand_writel(info
, NDCR
, ndcr
& ~NDCR_ND_RUN
);
833 static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info
*info
,
834 const struct pxa3xx_nand_flash
*f
)
836 struct platform_device
*pdev
= info
->pdev
;
837 struct pxa3xx_nand_platform_data
*pdata
= pdev
->dev
.platform_data
;
838 uint32_t ndcr
= 0x00000FFF; /* disable all interrupts */
840 if (f
->page_size
!= 2048 && f
->page_size
!= 512)
843 if (f
->flash_width
!= 16 && f
->flash_width
!= 8)
846 /* calculate flash information */
847 info
->cmdset
= f
->cmdset
;
848 info
->page_size
= f
->page_size
;
849 info
->oob_buff
= info
->data_buff
+ f
->page_size
;
850 info
->read_id_bytes
= (f
->page_size
== 2048) ? 4 : 2;
852 /* calculate addressing information */
853 info
->col_addr_cycles
= (f
->page_size
== 2048) ? 2 : 1;
855 if (f
->num_blocks
* f
->page_per_block
> 65536)
856 info
->row_addr_cycles
= 3;
858 info
->row_addr_cycles
= 2;
860 ndcr
|= (pdata
->enable_arbiter
) ? NDCR_ND_ARB_EN
: 0;
861 ndcr
|= (info
->col_addr_cycles
== 2) ? NDCR_RA_START
: 0;
862 ndcr
|= (f
->page_per_block
== 64) ? NDCR_PG_PER_BLK
: 0;
863 ndcr
|= (f
->page_size
== 2048) ? NDCR_PAGE_SZ
: 0;
864 ndcr
|= (f
->flash_width
== 16) ? NDCR_DWIDTH_M
: 0;
865 ndcr
|= (f
->dfc_width
== 16) ? NDCR_DWIDTH_C
: 0;
867 ndcr
|= NDCR_RD_ID_CNT(info
->read_id_bytes
);
868 ndcr
|= NDCR_SPARE_EN
; /* enable spare by default */
870 info
->reg_ndcr
= ndcr
;
872 pxa3xx_nand_set_timing(info
, f
->timing
);
876 static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info
*info
)
878 uint32_t ndcr
= nand_readl(info
, NDCR
);
879 struct nand_flash_dev
*type
= NULL
;
880 uint32_t id
= -1, page_per_block
, num_blocks
;
883 page_per_block
= ndcr
& NDCR_PG_PER_BLK
? 64 : 32;
884 info
->page_size
= ndcr
& NDCR_PAGE_SZ
? 2048 : 512;
885 /* set info fields needed to __readid */
886 info
->read_id_bytes
= (info
->page_size
== 2048) ? 4 : 2;
887 info
->reg_ndcr
= ndcr
;
888 info
->cmdset
= &default_cmdset
;
890 if (__readid(info
, &id
))
893 /* Lookup the flash id */
894 id
= (id
>> 8) & 0xff; /* device id is byte 2 */
895 for (i
= 0; nand_flash_ids
[i
].name
!= NULL
; i
++) {
896 if (id
== nand_flash_ids
[i
].id
) {
897 type
= &nand_flash_ids
[i
];
905 /* fill the missing flash information */
906 i
= __ffs(page_per_block
* info
->page_size
);
907 num_blocks
= type
->chipsize
<< (20 - i
);
909 /* calculate addressing information */
910 info
->col_addr_cycles
= (info
->page_size
== 2048) ? 2 : 1;
912 if (num_blocks
* page_per_block
> 65536)
913 info
->row_addr_cycles
= 3;
915 info
->row_addr_cycles
= 2;
917 info
->ndtr0cs0
= nand_readl(info
, NDTR0CS0
);
918 info
->ndtr1cs0
= nand_readl(info
, NDTR1CS0
);
923 static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info
*info
,
924 const struct pxa3xx_nand_platform_data
*pdata
)
926 const struct pxa3xx_nand_flash
*f
;
930 if (pdata
->keep_config
)
931 if (pxa3xx_nand_detect_config(info
) == 0)
934 /* we use default timing to detect id */
935 f
= DEFAULT_FLASH_TYPE
;
936 pxa3xx_nand_config_flash(info
, f
);
937 if (__readid(info
, &id
))
940 for (i
=0; i
<ARRAY_SIZE(builtin_flash_types
) + pdata
->num_flash
- 1; i
++) {
941 /* we first choose the flash definition from platfrom */
942 if (i
< pdata
->num_flash
)
943 f
= pdata
->flash
+ i
;
945 f
= &builtin_flash_types
[i
- pdata
->num_flash
+ 1];
946 if (f
->chip_id
== id
) {
947 dev_info(&info
->pdev
->dev
, "detect chip id: 0x%x\n", id
);
948 pxa3xx_nand_config_flash(info
, f
);
953 dev_warn(&info
->pdev
->dev
,
954 "failed to detect configured nand flash; found %04x instead of\n",
960 /* the maximum possible buffer size for large page with OOB data
961 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
962 * data buffer and the DMA descriptor
964 #define MAX_BUFF_SIZE PAGE_SIZE
966 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info
*info
)
968 struct platform_device
*pdev
= info
->pdev
;
969 int data_desc_offset
= MAX_BUFF_SIZE
- sizeof(struct pxa_dma_desc
);
972 info
->data_buff
= kmalloc(MAX_BUFF_SIZE
, GFP_KERNEL
);
973 if (info
->data_buff
== NULL
)
978 info
->data_buff
= dma_alloc_coherent(&pdev
->dev
, MAX_BUFF_SIZE
,
979 &info
->data_buff_phys
, GFP_KERNEL
);
980 if (info
->data_buff
== NULL
) {
981 dev_err(&pdev
->dev
, "failed to allocate dma buffer\n");
985 info
->data_buff_size
= MAX_BUFF_SIZE
;
986 info
->data_desc
= (void *)info
->data_buff
+ data_desc_offset
;
987 info
->data_desc_addr
= info
->data_buff_phys
+ data_desc_offset
;
989 info
->data_dma_ch
= pxa_request_dma("nand-data", DMA_PRIO_LOW
,
990 pxa3xx_nand_data_dma_irq
, info
);
991 if (info
->data_dma_ch
< 0) {
992 dev_err(&pdev
->dev
, "failed to request data dma\n");
993 dma_free_coherent(&pdev
->dev
, info
->data_buff_size
,
994 info
->data_buff
, info
->data_buff_phys
);
995 return info
->data_dma_ch
;
1001 static struct nand_ecclayout hw_smallpage_ecclayout
= {
1003 .eccpos
= {8, 9, 10, 11, 12, 13 },
1004 .oobfree
= { {2, 6} }
1007 static struct nand_ecclayout hw_largepage_ecclayout
= {
1010 40, 41, 42, 43, 44, 45, 46, 47,
1011 48, 49, 50, 51, 52, 53, 54, 55,
1012 56, 57, 58, 59, 60, 61, 62, 63},
1013 .oobfree
= { {2, 38} }
1016 static void pxa3xx_nand_init_mtd(struct mtd_info
*mtd
,
1017 struct pxa3xx_nand_info
*info
)
1019 struct nand_chip
*this = &info
->nand_chip
;
1021 this->options
= (info
->reg_ndcr
& NDCR_DWIDTH_C
) ? NAND_BUSWIDTH_16
: 0;
1023 this->waitfunc
= pxa3xx_nand_waitfunc
;
1024 this->select_chip
= pxa3xx_nand_select_chip
;
1025 this->dev_ready
= pxa3xx_nand_dev_ready
;
1026 this->cmdfunc
= pxa3xx_nand_cmdfunc
;
1027 this->read_word
= pxa3xx_nand_read_word
;
1028 this->read_byte
= pxa3xx_nand_read_byte
;
1029 this->read_buf
= pxa3xx_nand_read_buf
;
1030 this->write_buf
= pxa3xx_nand_write_buf
;
1031 this->verify_buf
= pxa3xx_nand_verify_buf
;
1033 this->ecc
.mode
= NAND_ECC_HW
;
1034 this->ecc
.hwctl
= pxa3xx_nand_ecc_hwctl
;
1035 this->ecc
.calculate
= pxa3xx_nand_ecc_calculate
;
1036 this->ecc
.correct
= pxa3xx_nand_ecc_correct
;
1037 this->ecc
.size
= info
->page_size
;
1039 if (info
->page_size
== 2048)
1040 this->ecc
.layout
= &hw_largepage_ecclayout
;
1042 this->ecc
.layout
= &hw_smallpage_ecclayout
;
1044 this->chip_delay
= 25;
1047 static int pxa3xx_nand_probe(struct platform_device
*pdev
)
1049 struct pxa3xx_nand_platform_data
*pdata
;
1050 struct pxa3xx_nand_info
*info
;
1051 struct nand_chip
*this;
1052 struct mtd_info
*mtd
;
1056 pdata
= pdev
->dev
.platform_data
;
1059 dev_err(&pdev
->dev
, "no platform data defined\n");
1063 mtd
= kzalloc(sizeof(struct mtd_info
) + sizeof(struct pxa3xx_nand_info
),
1066 dev_err(&pdev
->dev
, "failed to allocate memory\n");
1070 info
= (struct pxa3xx_nand_info
*)(&mtd
[1]);
1073 this = &info
->nand_chip
;
1075 mtd
->owner
= THIS_MODULE
;
1077 info
->clk
= clk_get(&pdev
->dev
, NULL
);
1078 if (IS_ERR(info
->clk
)) {
1079 dev_err(&pdev
->dev
, "failed to get nand clock\n");
1080 ret
= PTR_ERR(info
->clk
);
1083 clk_enable(info
->clk
);
1085 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1087 dev_err(&pdev
->dev
, "no resource defined for data DMA\n");
1091 info
->drcmr_dat
= r
->start
;
1093 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1095 dev_err(&pdev
->dev
, "no resource defined for command DMA\n");
1099 info
->drcmr_cmd
= r
->start
;
1101 irq
= platform_get_irq(pdev
, 0);
1103 dev_err(&pdev
->dev
, "no IRQ resource defined\n");
1108 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1110 dev_err(&pdev
->dev
, "no IO memory resource defined\n");
1115 r
= request_mem_region(r
->start
, resource_size(r
), pdev
->name
);
1117 dev_err(&pdev
->dev
, "failed to request memory resource\n");
1122 info
->mmio_base
= ioremap(r
->start
, resource_size(r
));
1123 if (info
->mmio_base
== NULL
) {
1124 dev_err(&pdev
->dev
, "ioremap() failed\n");
1128 info
->mmio_phys
= r
->start
;
1130 ret
= pxa3xx_nand_init_buff(info
);
1134 /* initialize all interrupts to be disabled */
1135 disable_int(info
, NDSR_MASK
);
1137 ret
= request_irq(irq
, pxa3xx_nand_irq
, IRQF_DISABLED
,
1140 dev_err(&pdev
->dev
, "failed to request IRQ\n");
1144 ret
= pxa3xx_nand_detect_flash(info
, pdata
);
1146 dev_err(&pdev
->dev
, "failed to detect flash\n");
1151 pxa3xx_nand_init_mtd(mtd
, info
);
1153 platform_set_drvdata(pdev
, mtd
);
1155 if (nand_scan(mtd
, 1)) {
1156 dev_err(&pdev
->dev
, "failed to scan nand\n");
1161 #ifdef CONFIG_MTD_PARTITIONS
1162 if (mtd_has_cmdlinepart()) {
1163 static const char *probes
[] = { "cmdlinepart", NULL
};
1164 struct mtd_partition
*parts
;
1167 nr_parts
= parse_mtd_partitions(mtd
, probes
, &parts
, 0);
1170 return add_mtd_partitions(mtd
, parts
, nr_parts
);
1173 return add_mtd_partitions(mtd
, pdata
->parts
, pdata
->nr_parts
);
1179 free_irq(irq
, info
);
1182 pxa_free_dma(info
->data_dma_ch
);
1183 dma_free_coherent(&pdev
->dev
, info
->data_buff_size
,
1184 info
->data_buff
, info
->data_buff_phys
);
1186 kfree(info
->data_buff
);
1188 iounmap(info
->mmio_base
);
1190 release_mem_region(r
->start
, resource_size(r
));
1192 clk_disable(info
->clk
);
1199 static int pxa3xx_nand_remove(struct platform_device
*pdev
)
1201 struct mtd_info
*mtd
= platform_get_drvdata(pdev
);
1202 struct pxa3xx_nand_info
*info
= mtd
->priv
;
1206 platform_set_drvdata(pdev
, NULL
);
1208 del_mtd_device(mtd
);
1209 #ifdef CONFIG_MTD_PARTITIONS
1210 del_mtd_partitions(mtd
);
1212 irq
= platform_get_irq(pdev
, 0);
1214 free_irq(irq
, info
);
1216 pxa_free_dma(info
->data_dma_ch
);
1217 dma_free_writecombine(&pdev
->dev
, info
->data_buff_size
,
1218 info
->data_buff
, info
->data_buff_phys
);
1220 kfree(info
->data_buff
);
1222 iounmap(info
->mmio_base
);
1223 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1224 release_mem_region(r
->start
, resource_size(r
));
1226 clk_disable(info
->clk
);
1234 static int pxa3xx_nand_suspend(struct platform_device
*pdev
, pm_message_t state
)
1236 struct mtd_info
*mtd
= (struct mtd_info
*)platform_get_drvdata(pdev
);
1237 struct pxa3xx_nand_info
*info
= mtd
->priv
;
1239 if (info
->state
!= STATE_READY
) {
1240 dev_err(&pdev
->dev
, "driver busy, state = %d\n", info
->state
);
1247 static int pxa3xx_nand_resume(struct platform_device
*pdev
)
1249 struct mtd_info
*mtd
= (struct mtd_info
*)platform_get_drvdata(pdev
);
1250 struct pxa3xx_nand_info
*info
= mtd
->priv
;
1252 nand_writel(info
, NDTR0CS0
, info
->ndtr0cs0
);
1253 nand_writel(info
, NDTR1CS0
, info
->ndtr1cs0
);
1254 clk_enable(info
->clk
);
1259 #define pxa3xx_nand_suspend NULL
1260 #define pxa3xx_nand_resume NULL
1263 static struct platform_driver pxa3xx_nand_driver
= {
1265 .name
= "pxa3xx-nand",
1267 .probe
= pxa3xx_nand_probe
,
1268 .remove
= pxa3xx_nand_remove
,
1269 .suspend
= pxa3xx_nand_suspend
,
1270 .resume
= pxa3xx_nand_resume
,
1273 static int __init
pxa3xx_nand_init(void)
1275 return platform_driver_register(&pxa3xx_nand_driver
);
1277 module_init(pxa3xx_nand_init
);
1279 static void __exit
pxa3xx_nand_exit(void)
1281 platform_driver_unregister(&pxa3xx_nand_driver
);
1283 module_exit(pxa3xx_nand_exit
);
1285 MODULE_LICENSE("GPL");
1286 MODULE_DESCRIPTION("PXA3xx NAND controller driver");