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.
11 * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/delay.h>
20 #include <linux/clk.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/nand.h>
23 #include <linux/mtd/partitions.h>
25 #include <linux/irq.h>
26 #include <linux/slab.h>
28 #include <linux/of_device.h>
29 #include <linux/of_mtd.h>
31 #if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
39 #include <linux/platform_data/mtd-nand-pxa3xx.h>
41 #define NAND_DEV_READY_TIMEOUT 50
42 #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
43 #define NAND_STOP_DELAY (2 * HZ/50)
44 #define PAGE_CHUNK_SIZE (2048)
47 * Define a buffer size for the initial command that detects the flash device:
48 * STATUS, READID and PARAM. The largest of these is the PARAM command,
51 #define INIT_BUFFER_SIZE 256
53 /* registers and bit definitions */
54 #define NDCR (0x00) /* Control register */
55 #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
56 #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
57 #define NDSR (0x14) /* Status Register */
58 #define NDPCR (0x18) /* Page Count Register */
59 #define NDBDR0 (0x1C) /* Bad Block Register 0 */
60 #define NDBDR1 (0x20) /* Bad Block Register 1 */
61 #define NDECCCTRL (0x28) /* ECC control */
62 #define NDDB (0x40) /* Data Buffer */
63 #define NDCB0 (0x48) /* Command Buffer0 */
64 #define NDCB1 (0x4C) /* Command Buffer1 */
65 #define NDCB2 (0x50) /* Command Buffer2 */
67 #define NDCR_SPARE_EN (0x1 << 31)
68 #define NDCR_ECC_EN (0x1 << 30)
69 #define NDCR_DMA_EN (0x1 << 29)
70 #define NDCR_ND_RUN (0x1 << 28)
71 #define NDCR_DWIDTH_C (0x1 << 27)
72 #define NDCR_DWIDTH_M (0x1 << 26)
73 #define NDCR_PAGE_SZ (0x1 << 24)
74 #define NDCR_NCSX (0x1 << 23)
75 #define NDCR_ND_MODE (0x3 << 21)
76 #define NDCR_NAND_MODE (0x0)
77 #define NDCR_CLR_PG_CNT (0x1 << 20)
78 #define NDCR_STOP_ON_UNCOR (0x1 << 19)
79 #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
80 #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
82 #define NDCR_RA_START (0x1 << 15)
83 #define NDCR_PG_PER_BLK (0x1 << 14)
84 #define NDCR_ND_ARB_EN (0x1 << 12)
85 #define NDCR_INT_MASK (0xFFF)
87 #define NDSR_MASK (0xfff)
88 #define NDSR_ERR_CNT_OFF (16)
89 #define NDSR_ERR_CNT_MASK (0x1f)
90 #define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
91 #define NDSR_RDY (0x1 << 12)
92 #define NDSR_FLASH_RDY (0x1 << 11)
93 #define NDSR_CS0_PAGED (0x1 << 10)
94 #define NDSR_CS1_PAGED (0x1 << 9)
95 #define NDSR_CS0_CMDD (0x1 << 8)
96 #define NDSR_CS1_CMDD (0x1 << 7)
97 #define NDSR_CS0_BBD (0x1 << 6)
98 #define NDSR_CS1_BBD (0x1 << 5)
99 #define NDSR_UNCORERR (0x1 << 4)
100 #define NDSR_CORERR (0x1 << 3)
101 #define NDSR_WRDREQ (0x1 << 2)
102 #define NDSR_RDDREQ (0x1 << 1)
103 #define NDSR_WRCMDREQ (0x1)
105 #define NDCB0_LEN_OVRD (0x1 << 28)
106 #define NDCB0_ST_ROW_EN (0x1 << 26)
107 #define NDCB0_AUTO_RS (0x1 << 25)
108 #define NDCB0_CSEL (0x1 << 24)
109 #define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
110 #define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
111 #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
112 #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
113 #define NDCB0_NC (0x1 << 20)
114 #define NDCB0_DBC (0x1 << 19)
115 #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
116 #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
117 #define NDCB0_CMD2_MASK (0xff << 8)
118 #define NDCB0_CMD1_MASK (0xff)
119 #define NDCB0_ADDR_CYC_SHIFT (16)
121 #define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
122 #define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
123 #define EXT_CMD_TYPE_READ 4 /* Read */
124 #define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
125 #define EXT_CMD_TYPE_FINAL 3 /* Final command */
126 #define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
127 #define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
129 /* macros for registers read/write */
130 #define nand_writel(info, off, val) \
131 __raw_writel((val), (info)->mmio_base + (off))
133 #define nand_readl(info, off) \
134 __raw_readl((info)->mmio_base + (off))
136 /* error code and state */
159 enum pxa3xx_nand_variant
{
160 PXA3XX_NAND_VARIANT_PXA
,
161 PXA3XX_NAND_VARIANT_ARMADA370
,
164 struct pxa3xx_nand_host
{
165 struct nand_chip chip
;
166 struct mtd_info
*mtd
;
169 /* page size of attached chip */
173 /* calculated from pxa3xx_nand_flash data */
174 unsigned int col_addr_cycles
;
175 unsigned int row_addr_cycles
;
176 size_t read_id_bytes
;
180 struct pxa3xx_nand_info
{
181 struct nand_hw_control controller
;
182 struct platform_device
*pdev
;
185 void __iomem
*mmio_base
;
186 unsigned long mmio_phys
;
187 struct completion cmd_complete
, dev_ready
;
189 unsigned int buf_start
;
190 unsigned int buf_count
;
191 unsigned int buf_size
;
192 unsigned int data_buff_pos
;
193 unsigned int oob_buff_pos
;
195 /* DMA information */
199 unsigned char *data_buff
;
200 unsigned char *oob_buff
;
201 dma_addr_t data_buff_phys
;
203 struct pxa_dma_desc
*data_desc
;
204 dma_addr_t data_desc_addr
;
206 struct pxa3xx_nand_host
*host
[NUM_CHIP_SELECT
];
210 * This driver supports NFCv1 (as found in PXA SoC)
211 * and NFCv2 (as found in Armada 370/XP SoC).
213 enum pxa3xx_nand_variant variant
;
216 int use_ecc
; /* use HW ECC ? */
217 int ecc_bch
; /* using BCH ECC? */
218 int use_dma
; /* use DMA ? */
219 int use_spare
; /* use spare ? */
222 unsigned int data_size
; /* data to be read from FIFO */
223 unsigned int chunk_size
; /* split commands chunk size */
224 unsigned int oob_size
;
225 unsigned int spare_size
;
226 unsigned int ecc_size
;
227 unsigned int ecc_err_cnt
;
228 unsigned int max_bitflips
;
231 /* cached register value */
236 /* generated NDCBx register values */
243 static bool use_dma
= 1;
244 module_param(use_dma
, bool, 0444);
245 MODULE_PARM_DESC(use_dma
, "enable DMA for data transferring to/from NAND HW");
247 static struct pxa3xx_nand_timing timing
[] = {
248 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
249 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
250 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
251 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
254 static struct pxa3xx_nand_flash builtin_flash_types
[] = {
255 { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing
[0] },
256 { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing
[1] },
257 { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing
[1] },
258 { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing
[1] },
259 { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing
[2] },
260 { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing
[2] },
261 { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing
[2] },
262 { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing
[2] },
263 { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing
[3] },
266 static u8 bbt_pattern
[] = {'M', 'V', 'B', 'b', 't', '0' };
267 static u8 bbt_mirror_pattern
[] = {'1', 't', 'b', 'B', 'V', 'M' };
269 static struct nand_bbt_descr bbt_main_descr
= {
270 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
271 | NAND_BBT_2BIT
| NAND_BBT_VERSION
,
275 .maxblocks
= 8, /* Last 8 blocks in each chip */
276 .pattern
= bbt_pattern
279 static struct nand_bbt_descr bbt_mirror_descr
= {
280 .options
= NAND_BBT_LASTBLOCK
| NAND_BBT_CREATE
| NAND_BBT_WRITE
281 | NAND_BBT_2BIT
| NAND_BBT_VERSION
,
285 .maxblocks
= 8, /* Last 8 blocks in each chip */
286 .pattern
= bbt_mirror_pattern
289 static struct nand_ecclayout ecc_layout_2KB_bch4bit
= {
292 32, 33, 34, 35, 36, 37, 38, 39,
293 40, 41, 42, 43, 44, 45, 46, 47,
294 48, 49, 50, 51, 52, 53, 54, 55,
295 56, 57, 58, 59, 60, 61, 62, 63},
296 .oobfree
= { {2, 30} }
299 static struct nand_ecclayout ecc_layout_4KB_bch4bit
= {
302 32, 33, 34, 35, 36, 37, 38, 39,
303 40, 41, 42, 43, 44, 45, 46, 47,
304 48, 49, 50, 51, 52, 53, 54, 55,
305 56, 57, 58, 59, 60, 61, 62, 63,
306 96, 97, 98, 99, 100, 101, 102, 103,
307 104, 105, 106, 107, 108, 109, 110, 111,
308 112, 113, 114, 115, 116, 117, 118, 119,
309 120, 121, 122, 123, 124, 125, 126, 127},
310 /* Bootrom looks in bytes 0 & 5 for bad blocks */
311 .oobfree
= { {6, 26}, { 64, 32} }
314 static struct nand_ecclayout ecc_layout_4KB_bch8bit
= {
317 32, 33, 34, 35, 36, 37, 38, 39,
318 40, 41, 42, 43, 44, 45, 46, 47,
319 48, 49, 50, 51, 52, 53, 54, 55,
320 56, 57, 58, 59, 60, 61, 62, 63},
324 /* Define a default flash type setting serve as flash detecting only */
325 #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
327 #define NDTR0_tCH(c) (min((c), 7) << 19)
328 #define NDTR0_tCS(c) (min((c), 7) << 16)
329 #define NDTR0_tWH(c) (min((c), 7) << 11)
330 #define NDTR0_tWP(c) (min((c), 7) << 8)
331 #define NDTR0_tRH(c) (min((c), 7) << 3)
332 #define NDTR0_tRP(c) (min((c), 7) << 0)
334 #define NDTR1_tR(c) (min((c), 65535) << 16)
335 #define NDTR1_tWHR(c) (min((c), 15) << 4)
336 #define NDTR1_tAR(c) (min((c), 15) << 0)
338 /* convert nano-seconds to nand flash controller clock cycles */
339 #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
341 static struct of_device_id pxa3xx_nand_dt_ids
[] = {
343 .compatible
= "marvell,pxa3xx-nand",
344 .data
= (void *)PXA3XX_NAND_VARIANT_PXA
,
347 .compatible
= "marvell,armada370-nand",
348 .data
= (void *)PXA3XX_NAND_VARIANT_ARMADA370
,
352 MODULE_DEVICE_TABLE(of
, pxa3xx_nand_dt_ids
);
354 static enum pxa3xx_nand_variant
355 pxa3xx_nand_get_variant(struct platform_device
*pdev
)
357 const struct of_device_id
*of_id
=
358 of_match_device(pxa3xx_nand_dt_ids
, &pdev
->dev
);
360 return PXA3XX_NAND_VARIANT_PXA
;
361 return (enum pxa3xx_nand_variant
)of_id
->data
;
364 static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host
*host
,
365 const struct pxa3xx_nand_timing
*t
)
367 struct pxa3xx_nand_info
*info
= host
->info_data
;
368 unsigned long nand_clk
= clk_get_rate(info
->clk
);
369 uint32_t ndtr0
, ndtr1
;
371 ndtr0
= NDTR0_tCH(ns2cycle(t
->tCH
, nand_clk
)) |
372 NDTR0_tCS(ns2cycle(t
->tCS
, nand_clk
)) |
373 NDTR0_tWH(ns2cycle(t
->tWH
, nand_clk
)) |
374 NDTR0_tWP(ns2cycle(t
->tWP
, nand_clk
)) |
375 NDTR0_tRH(ns2cycle(t
->tRH
, nand_clk
)) |
376 NDTR0_tRP(ns2cycle(t
->tRP
, nand_clk
));
378 ndtr1
= NDTR1_tR(ns2cycle(t
->tR
, nand_clk
)) |
379 NDTR1_tWHR(ns2cycle(t
->tWHR
, nand_clk
)) |
380 NDTR1_tAR(ns2cycle(t
->tAR
, nand_clk
));
382 info
->ndtr0cs0
= ndtr0
;
383 info
->ndtr1cs0
= ndtr1
;
384 nand_writel(info
, NDTR0CS0
, ndtr0
);
385 nand_writel(info
, NDTR1CS0
, ndtr1
);
389 * Set the data and OOB size, depending on the selected
390 * spare and ECC configuration.
391 * Only applicable to READ0, READOOB and PAGEPROG commands.
393 static void pxa3xx_set_datasize(struct pxa3xx_nand_info
*info
,
394 struct mtd_info
*mtd
)
396 int oob_enable
= info
->reg_ndcr
& NDCR_SPARE_EN
;
398 info
->data_size
= mtd
->writesize
;
402 info
->oob_size
= info
->spare_size
;
404 info
->oob_size
+= info
->ecc_size
;
408 * NOTE: it is a must to set ND_RUN firstly, then write
409 * command buffer, otherwise, it does not work.
410 * We enable all the interrupt at the same time, and
411 * let pxa3xx_nand_irq to handle all logic.
413 static void pxa3xx_nand_start(struct pxa3xx_nand_info
*info
)
417 ndcr
= info
->reg_ndcr
;
422 nand_writel(info
, NDECCCTRL
, 0x1);
424 ndcr
&= ~NDCR_ECC_EN
;
426 nand_writel(info
, NDECCCTRL
, 0x0);
432 ndcr
&= ~NDCR_DMA_EN
;
435 ndcr
|= NDCR_SPARE_EN
;
437 ndcr
&= ~NDCR_SPARE_EN
;
441 /* clear status bits and run */
442 nand_writel(info
, NDCR
, 0);
443 nand_writel(info
, NDSR
, NDSR_MASK
);
444 nand_writel(info
, NDCR
, ndcr
);
447 static void pxa3xx_nand_stop(struct pxa3xx_nand_info
*info
)
450 int timeout
= NAND_STOP_DELAY
;
452 /* wait RUN bit in NDCR become 0 */
453 ndcr
= nand_readl(info
, NDCR
);
454 while ((ndcr
& NDCR_ND_RUN
) && (timeout
-- > 0)) {
455 ndcr
= nand_readl(info
, NDCR
);
460 ndcr
&= ~NDCR_ND_RUN
;
461 nand_writel(info
, NDCR
, ndcr
);
463 /* clear status bits */
464 nand_writel(info
, NDSR
, NDSR_MASK
);
467 static void __maybe_unused
468 enable_int(struct pxa3xx_nand_info
*info
, uint32_t int_mask
)
472 ndcr
= nand_readl(info
, NDCR
);
473 nand_writel(info
, NDCR
, ndcr
& ~int_mask
);
476 static void disable_int(struct pxa3xx_nand_info
*info
, uint32_t int_mask
)
480 ndcr
= nand_readl(info
, NDCR
);
481 nand_writel(info
, NDCR
, ndcr
| int_mask
);
484 static void handle_data_pio(struct pxa3xx_nand_info
*info
)
486 unsigned int do_bytes
= min(info
->data_size
, info
->chunk_size
);
488 switch (info
->state
) {
489 case STATE_PIO_WRITING
:
490 __raw_writesl(info
->mmio_base
+ NDDB
,
491 info
->data_buff
+ info
->data_buff_pos
,
492 DIV_ROUND_UP(do_bytes
, 4));
494 if (info
->oob_size
> 0)
495 __raw_writesl(info
->mmio_base
+ NDDB
,
496 info
->oob_buff
+ info
->oob_buff_pos
,
497 DIV_ROUND_UP(info
->oob_size
, 4));
499 case STATE_PIO_READING
:
500 __raw_readsl(info
->mmio_base
+ NDDB
,
501 info
->data_buff
+ info
->data_buff_pos
,
502 DIV_ROUND_UP(do_bytes
, 4));
504 if (info
->oob_size
> 0)
505 __raw_readsl(info
->mmio_base
+ NDDB
,
506 info
->oob_buff
+ info
->oob_buff_pos
,
507 DIV_ROUND_UP(info
->oob_size
, 4));
510 dev_err(&info
->pdev
->dev
, "%s: invalid state %d\n", __func__
,
515 /* Update buffer pointers for multi-page read/write */
516 info
->data_buff_pos
+= do_bytes
;
517 info
->oob_buff_pos
+= info
->oob_size
;
518 info
->data_size
-= do_bytes
;
522 static void start_data_dma(struct pxa3xx_nand_info
*info
)
524 struct pxa_dma_desc
*desc
= info
->data_desc
;
525 int dma_len
= ALIGN(info
->data_size
+ info
->oob_size
, 32);
527 desc
->ddadr
= DDADR_STOP
;
528 desc
->dcmd
= DCMD_ENDIRQEN
| DCMD_WIDTH4
| DCMD_BURST32
| dma_len
;
530 switch (info
->state
) {
531 case STATE_DMA_WRITING
:
532 desc
->dsadr
= info
->data_buff_phys
;
533 desc
->dtadr
= info
->mmio_phys
+ NDDB
;
534 desc
->dcmd
|= DCMD_INCSRCADDR
| DCMD_FLOWTRG
;
536 case STATE_DMA_READING
:
537 desc
->dtadr
= info
->data_buff_phys
;
538 desc
->dsadr
= info
->mmio_phys
+ NDDB
;
539 desc
->dcmd
|= DCMD_INCTRGADDR
| DCMD_FLOWSRC
;
542 dev_err(&info
->pdev
->dev
, "%s: invalid state %d\n", __func__
,
547 DRCMR(info
->drcmr_dat
) = DRCMR_MAPVLD
| info
->data_dma_ch
;
548 DDADR(info
->data_dma_ch
) = info
->data_desc_addr
;
549 DCSR(info
->data_dma_ch
) |= DCSR_RUN
;
552 static void pxa3xx_nand_data_dma_irq(int channel
, void *data
)
554 struct pxa3xx_nand_info
*info
= data
;
557 dcsr
= DCSR(channel
);
558 DCSR(channel
) = dcsr
;
560 if (dcsr
& DCSR_BUSERR
) {
561 info
->retcode
= ERR_DMABUSERR
;
564 info
->state
= STATE_DMA_DONE
;
565 enable_int(info
, NDCR_INT_MASK
);
566 nand_writel(info
, NDSR
, NDSR_WRDREQ
| NDSR_RDDREQ
);
569 static void start_data_dma(struct pxa3xx_nand_info
*info
)
573 static irqreturn_t
pxa3xx_nand_irq(int irq
, void *devid
)
575 struct pxa3xx_nand_info
*info
= devid
;
576 unsigned int status
, is_completed
= 0, is_ready
= 0;
577 unsigned int ready
, cmd_done
;
580 ready
= NDSR_FLASH_RDY
;
581 cmd_done
= NDSR_CS0_CMDD
;
584 cmd_done
= NDSR_CS1_CMDD
;
587 status
= nand_readl(info
, NDSR
);
589 if (status
& NDSR_UNCORERR
)
590 info
->retcode
= ERR_UNCORERR
;
591 if (status
& NDSR_CORERR
) {
592 info
->retcode
= ERR_CORERR
;
593 if (info
->variant
== PXA3XX_NAND_VARIANT_ARMADA370
&&
595 info
->ecc_err_cnt
= NDSR_ERR_CNT(status
);
597 info
->ecc_err_cnt
= 1;
600 * Each chunk composing a page is corrected independently,
601 * and we need to store maximum number of corrected bitflips
602 * to return it to the MTD layer in ecc.read_page().
604 info
->max_bitflips
= max_t(unsigned int,
608 if (status
& (NDSR_RDDREQ
| NDSR_WRDREQ
)) {
609 /* whether use dma to transfer data */
611 disable_int(info
, NDCR_INT_MASK
);
612 info
->state
= (status
& NDSR_RDDREQ
) ?
613 STATE_DMA_READING
: STATE_DMA_WRITING
;
614 start_data_dma(info
);
615 goto NORMAL_IRQ_EXIT
;
617 info
->state
= (status
& NDSR_RDDREQ
) ?
618 STATE_PIO_READING
: STATE_PIO_WRITING
;
619 handle_data_pio(info
);
622 if (status
& cmd_done
) {
623 info
->state
= STATE_CMD_DONE
;
626 if (status
& ready
) {
627 info
->state
= STATE_READY
;
631 if (status
& NDSR_WRCMDREQ
) {
632 nand_writel(info
, NDSR
, NDSR_WRCMDREQ
);
633 status
&= ~NDSR_WRCMDREQ
;
634 info
->state
= STATE_CMD_HANDLE
;
637 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
638 * must be loaded by writing directly either 12 or 16
639 * bytes directly to NDCB0, four bytes at a time.
641 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
642 * but each NDCBx register can be read.
644 nand_writel(info
, NDCB0
, info
->ndcb0
);
645 nand_writel(info
, NDCB0
, info
->ndcb1
);
646 nand_writel(info
, NDCB0
, info
->ndcb2
);
648 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
649 if (info
->variant
== PXA3XX_NAND_VARIANT_ARMADA370
)
650 nand_writel(info
, NDCB0
, info
->ndcb3
);
653 /* clear NDSR to let the controller exit the IRQ */
654 nand_writel(info
, NDSR
, status
);
656 complete(&info
->cmd_complete
);
658 complete(&info
->dev_ready
);
663 static inline int is_buf_blank(uint8_t *buf
, size_t len
)
665 for (; len
> 0; len
--)
671 static void set_command_address(struct pxa3xx_nand_info
*info
,
672 unsigned int page_size
, uint16_t column
, int page_addr
)
674 /* small page addr setting */
675 if (page_size
< PAGE_CHUNK_SIZE
) {
676 info
->ndcb1
= ((page_addr
& 0xFFFFFF) << 8)
681 info
->ndcb1
= ((page_addr
& 0xFFFF) << 16)
684 if (page_addr
& 0xFF0000)
685 info
->ndcb2
= (page_addr
& 0xFF0000) >> 16;
691 static void prepare_start_command(struct pxa3xx_nand_info
*info
, int command
)
693 struct pxa3xx_nand_host
*host
= info
->host
[info
->cs
];
694 struct mtd_info
*mtd
= host
->mtd
;
696 /* reset data and oob column point to handle data */
700 info
->data_buff_pos
= 0;
701 info
->oob_buff_pos
= 0;
704 info
->retcode
= ERR_NONE
;
705 info
->ecc_err_cnt
= 0;
711 case NAND_CMD_PAGEPROG
:
713 case NAND_CMD_READOOB
:
714 pxa3xx_set_datasize(info
, mtd
);
726 * If we are about to issue a read command, or about to set
727 * the write address, then clean the data buffer.
729 if (command
== NAND_CMD_READ0
||
730 command
== NAND_CMD_READOOB
||
731 command
== NAND_CMD_SEQIN
) {
733 info
->buf_count
= mtd
->writesize
+ mtd
->oobsize
;
734 memset(info
->data_buff
, 0xFF, info
->buf_count
);
739 static int prepare_set_command(struct pxa3xx_nand_info
*info
, int command
,
740 int ext_cmd_type
, uint16_t column
, int page_addr
)
742 int addr_cycle
, exec_cmd
;
743 struct pxa3xx_nand_host
*host
;
744 struct mtd_info
*mtd
;
746 host
= info
->host
[info
->cs
];
752 info
->ndcb0
= NDCB0_CSEL
;
756 if (command
== NAND_CMD_SEQIN
)
759 addr_cycle
= NDCB0_ADDR_CYC(host
->row_addr_cycles
760 + host
->col_addr_cycles
);
763 case NAND_CMD_READOOB
:
765 info
->buf_start
= column
;
766 info
->ndcb0
|= NDCB0_CMD_TYPE(0)
770 if (command
== NAND_CMD_READOOB
)
771 info
->buf_start
+= mtd
->writesize
;
774 * Multiple page read needs an 'extended command type' field,
775 * which is either naked-read or last-read according to the
778 if (mtd
->writesize
== PAGE_CHUNK_SIZE
) {
779 info
->ndcb0
|= NDCB0_DBC
| (NAND_CMD_READSTART
<< 8);
780 } else if (mtd
->writesize
> PAGE_CHUNK_SIZE
) {
781 info
->ndcb0
|= NDCB0_DBC
| (NAND_CMD_READSTART
<< 8)
783 | NDCB0_EXT_CMD_TYPE(ext_cmd_type
);
784 info
->ndcb3
= info
->chunk_size
+
788 set_command_address(info
, mtd
->writesize
, column
, page_addr
);
793 info
->buf_start
= column
;
794 set_command_address(info
, mtd
->writesize
, 0, page_addr
);
797 * Multiple page programming needs to execute the initial
798 * SEQIN command that sets the page address.
800 if (mtd
->writesize
> PAGE_CHUNK_SIZE
) {
801 info
->ndcb0
|= NDCB0_CMD_TYPE(0x1)
802 | NDCB0_EXT_CMD_TYPE(ext_cmd_type
)
805 /* No data transfer in this case */
811 case NAND_CMD_PAGEPROG
:
812 if (is_buf_blank(info
->data_buff
,
813 (mtd
->writesize
+ mtd
->oobsize
))) {
818 /* Second command setting for large pages */
819 if (mtd
->writesize
> PAGE_CHUNK_SIZE
) {
821 * Multiple page write uses the 'extended command'
822 * field. This can be used to issue a command dispatch
823 * or a naked-write depending on the current stage.
825 info
->ndcb0
|= NDCB0_CMD_TYPE(0x1)
827 | NDCB0_EXT_CMD_TYPE(ext_cmd_type
);
828 info
->ndcb3
= info
->chunk_size
+
832 * This is the command dispatch that completes a chunked
833 * page program operation.
835 if (info
->data_size
== 0) {
836 info
->ndcb0
= NDCB0_CMD_TYPE(0x1)
837 | NDCB0_EXT_CMD_TYPE(ext_cmd_type
)
844 info
->ndcb0
|= NDCB0_CMD_TYPE(0x1)
848 | (NAND_CMD_PAGEPROG
<< 8)
855 info
->buf_count
= 256;
856 info
->ndcb0
|= NDCB0_CMD_TYPE(0)
860 info
->ndcb1
= (column
& 0xFF);
862 info
->data_size
= 256;
865 case NAND_CMD_READID
:
866 info
->buf_count
= host
->read_id_bytes
;
867 info
->ndcb0
|= NDCB0_CMD_TYPE(3)
870 info
->ndcb1
= (column
& 0xFF);
874 case NAND_CMD_STATUS
:
876 info
->ndcb0
|= NDCB0_CMD_TYPE(4)
883 case NAND_CMD_ERASE1
:
884 info
->ndcb0
|= NDCB0_CMD_TYPE(2)
888 | (NAND_CMD_ERASE2
<< 8)
890 info
->ndcb1
= page_addr
;
895 info
->ndcb0
|= NDCB0_CMD_TYPE(5)
900 case NAND_CMD_ERASE2
:
906 dev_err(&info
->pdev
->dev
, "non-supported command %x\n",
914 static void nand_cmdfunc(struct mtd_info
*mtd
, unsigned command
,
915 int column
, int page_addr
)
917 struct pxa3xx_nand_host
*host
= mtd
->priv
;
918 struct pxa3xx_nand_info
*info
= host
->info_data
;
922 * if this is a x16 device ,then convert the input
923 * "byte" address into a "word" address appropriate
924 * for indexing a word-oriented device
926 if (info
->reg_ndcr
& NDCR_DWIDTH_M
)
930 * There may be different NAND chip hooked to
931 * different chip select, so check whether
932 * chip select has been changed, if yes, reset the timing
934 if (info
->cs
!= host
->cs
) {
936 nand_writel(info
, NDTR0CS0
, info
->ndtr0cs0
);
937 nand_writel(info
, NDTR1CS0
, info
->ndtr1cs0
);
940 prepare_start_command(info
, command
);
942 info
->state
= STATE_PREPARED
;
943 exec_cmd
= prepare_set_command(info
, command
, 0, column
, page_addr
);
946 init_completion(&info
->cmd_complete
);
947 init_completion(&info
->dev_ready
);
949 pxa3xx_nand_start(info
);
951 ret
= wait_for_completion_timeout(&info
->cmd_complete
,
954 dev_err(&info
->pdev
->dev
, "Wait time out!!!\n");
955 /* Stop State Machine for next command cycle */
956 pxa3xx_nand_stop(info
);
959 info
->state
= STATE_IDLE
;
962 static void nand_cmdfunc_extended(struct mtd_info
*mtd
,
963 const unsigned command
,
964 int column
, int page_addr
)
966 struct pxa3xx_nand_host
*host
= mtd
->priv
;
967 struct pxa3xx_nand_info
*info
= host
->info_data
;
968 int ret
, exec_cmd
, ext_cmd_type
;
971 * if this is a x16 device then convert the input
972 * "byte" address into a "word" address appropriate
973 * for indexing a word-oriented device
975 if (info
->reg_ndcr
& NDCR_DWIDTH_M
)
979 * There may be different NAND chip hooked to
980 * different chip select, so check whether
981 * chip select has been changed, if yes, reset the timing
983 if (info
->cs
!= host
->cs
) {
985 nand_writel(info
, NDTR0CS0
, info
->ndtr0cs0
);
986 nand_writel(info
, NDTR1CS0
, info
->ndtr1cs0
);
989 /* Select the extended command for the first command */
992 case NAND_CMD_READOOB
:
993 ext_cmd_type
= EXT_CMD_TYPE_MONO
;
996 ext_cmd_type
= EXT_CMD_TYPE_DISPATCH
;
998 case NAND_CMD_PAGEPROG
:
999 ext_cmd_type
= EXT_CMD_TYPE_NAKED_RW
;
1006 prepare_start_command(info
, command
);
1009 * Prepare the "is ready" completion before starting a command
1010 * transaction sequence. If the command is not executed the
1011 * completion will be completed, see below.
1013 * We can do that inside the loop because the command variable
1014 * is invariant and thus so is the exec_cmd.
1016 info
->need_wait
= 1;
1017 init_completion(&info
->dev_ready
);
1019 info
->state
= STATE_PREPARED
;
1020 exec_cmd
= prepare_set_command(info
, command
, ext_cmd_type
,
1023 info
->need_wait
= 0;
1024 complete(&info
->dev_ready
);
1028 init_completion(&info
->cmd_complete
);
1029 pxa3xx_nand_start(info
);
1031 ret
= wait_for_completion_timeout(&info
->cmd_complete
,
1032 CHIP_DELAY_TIMEOUT
);
1034 dev_err(&info
->pdev
->dev
, "Wait time out!!!\n");
1035 /* Stop State Machine for next command cycle */
1036 pxa3xx_nand_stop(info
);
1040 /* Check if the sequence is complete */
1041 if (info
->data_size
== 0 && command
!= NAND_CMD_PAGEPROG
)
1045 * After a splitted program command sequence has issued
1046 * the command dispatch, the command sequence is complete.
1048 if (info
->data_size
== 0 &&
1049 command
== NAND_CMD_PAGEPROG
&&
1050 ext_cmd_type
== EXT_CMD_TYPE_DISPATCH
)
1053 if (command
== NAND_CMD_READ0
|| command
== NAND_CMD_READOOB
) {
1054 /* Last read: issue a 'last naked read' */
1055 if (info
->data_size
== info
->chunk_size
)
1056 ext_cmd_type
= EXT_CMD_TYPE_LAST_RW
;
1058 ext_cmd_type
= EXT_CMD_TYPE_NAKED_RW
;
1061 * If a splitted program command has no more data to transfer,
1062 * the command dispatch must be issued to complete.
1064 } else if (command
== NAND_CMD_PAGEPROG
&&
1065 info
->data_size
== 0) {
1066 ext_cmd_type
= EXT_CMD_TYPE_DISPATCH
;
1070 info
->state
= STATE_IDLE
;
1073 static int pxa3xx_nand_write_page_hwecc(struct mtd_info
*mtd
,
1074 struct nand_chip
*chip
, const uint8_t *buf
, int oob_required
)
1076 chip
->write_buf(mtd
, buf
, mtd
->writesize
);
1077 chip
->write_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1082 static int pxa3xx_nand_read_page_hwecc(struct mtd_info
*mtd
,
1083 struct nand_chip
*chip
, uint8_t *buf
, int oob_required
,
1086 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1087 struct pxa3xx_nand_info
*info
= host
->info_data
;
1089 chip
->read_buf(mtd
, buf
, mtd
->writesize
);
1090 chip
->read_buf(mtd
, chip
->oob_poi
, mtd
->oobsize
);
1092 if (info
->retcode
== ERR_CORERR
&& info
->use_ecc
) {
1093 mtd
->ecc_stats
.corrected
+= info
->ecc_err_cnt
;
1095 } else if (info
->retcode
== ERR_UNCORERR
) {
1097 * for blank page (all 0xff), HW will calculate its ECC as
1098 * 0, which is different from the ECC information within
1099 * OOB, ignore such uncorrectable errors
1101 if (is_buf_blank(buf
, mtd
->writesize
))
1102 info
->retcode
= ERR_NONE
;
1104 mtd
->ecc_stats
.failed
++;
1107 return info
->max_bitflips
;
1110 static uint8_t pxa3xx_nand_read_byte(struct mtd_info
*mtd
)
1112 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1113 struct pxa3xx_nand_info
*info
= host
->info_data
;
1116 if (info
->buf_start
< info
->buf_count
)
1117 /* Has just send a new command? */
1118 retval
= info
->data_buff
[info
->buf_start
++];
1123 static u16
pxa3xx_nand_read_word(struct mtd_info
*mtd
)
1125 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1126 struct pxa3xx_nand_info
*info
= host
->info_data
;
1127 u16 retval
= 0xFFFF;
1129 if (!(info
->buf_start
& 0x01) && info
->buf_start
< info
->buf_count
) {
1130 retval
= *((u16
*)(info
->data_buff
+info
->buf_start
));
1131 info
->buf_start
+= 2;
1136 static void pxa3xx_nand_read_buf(struct mtd_info
*mtd
, uint8_t *buf
, int len
)
1138 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1139 struct pxa3xx_nand_info
*info
= host
->info_data
;
1140 int real_len
= min_t(size_t, len
, info
->buf_count
- info
->buf_start
);
1142 memcpy(buf
, info
->data_buff
+ info
->buf_start
, real_len
);
1143 info
->buf_start
+= real_len
;
1146 static void pxa3xx_nand_write_buf(struct mtd_info
*mtd
,
1147 const uint8_t *buf
, int len
)
1149 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1150 struct pxa3xx_nand_info
*info
= host
->info_data
;
1151 int real_len
= min_t(size_t, len
, info
->buf_count
- info
->buf_start
);
1153 memcpy(info
->data_buff
+ info
->buf_start
, buf
, real_len
);
1154 info
->buf_start
+= real_len
;
1157 static void pxa3xx_nand_select_chip(struct mtd_info
*mtd
, int chip
)
1162 static int pxa3xx_nand_waitfunc(struct mtd_info
*mtd
, struct nand_chip
*this)
1164 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1165 struct pxa3xx_nand_info
*info
= host
->info_data
;
1168 if (info
->need_wait
) {
1169 ret
= wait_for_completion_timeout(&info
->dev_ready
,
1170 CHIP_DELAY_TIMEOUT
);
1171 info
->need_wait
= 0;
1173 dev_err(&info
->pdev
->dev
, "Ready time out!!!\n");
1174 return NAND_STATUS_FAIL
;
1178 /* pxa3xx_nand_send_command has waited for command complete */
1179 if (this->state
== FL_WRITING
|| this->state
== FL_ERASING
) {
1180 if (info
->retcode
== ERR_NONE
)
1183 return NAND_STATUS_FAIL
;
1186 return NAND_STATUS_READY
;
1189 static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info
*info
,
1190 const struct pxa3xx_nand_flash
*f
)
1192 struct platform_device
*pdev
= info
->pdev
;
1193 struct pxa3xx_nand_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1194 struct pxa3xx_nand_host
*host
= info
->host
[info
->cs
];
1195 uint32_t ndcr
= 0x0; /* enable all interrupts */
1197 if (f
->page_size
!= 2048 && f
->page_size
!= 512) {
1198 dev_err(&pdev
->dev
, "Current only support 2048 and 512 size\n");
1202 if (f
->flash_width
!= 16 && f
->flash_width
!= 8) {
1203 dev_err(&pdev
->dev
, "Only support 8bit and 16 bit!\n");
1207 /* calculate flash information */
1208 host
->read_id_bytes
= (f
->page_size
== 2048) ? 4 : 2;
1210 /* calculate addressing information */
1211 host
->col_addr_cycles
= (f
->page_size
== 2048) ? 2 : 1;
1213 if (f
->num_blocks
* f
->page_per_block
> 65536)
1214 host
->row_addr_cycles
= 3;
1216 host
->row_addr_cycles
= 2;
1218 ndcr
|= (pdata
->enable_arbiter
) ? NDCR_ND_ARB_EN
: 0;
1219 ndcr
|= (host
->col_addr_cycles
== 2) ? NDCR_RA_START
: 0;
1220 ndcr
|= (f
->page_per_block
== 64) ? NDCR_PG_PER_BLK
: 0;
1221 ndcr
|= (f
->page_size
== 2048) ? NDCR_PAGE_SZ
: 0;
1222 ndcr
|= (f
->flash_width
== 16) ? NDCR_DWIDTH_M
: 0;
1223 ndcr
|= (f
->dfc_width
== 16) ? NDCR_DWIDTH_C
: 0;
1225 ndcr
|= NDCR_RD_ID_CNT(host
->read_id_bytes
);
1226 ndcr
|= NDCR_SPARE_EN
; /* enable spare by default */
1228 info
->reg_ndcr
= ndcr
;
1230 pxa3xx_nand_set_timing(host
, f
->timing
);
1234 static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info
*info
)
1237 * We set 0 by hard coding here, for we don't support keep_config
1238 * when there is more than one chip attached to the controller
1240 struct pxa3xx_nand_host
*host
= info
->host
[0];
1241 uint32_t ndcr
= nand_readl(info
, NDCR
);
1243 if (ndcr
& NDCR_PAGE_SZ
) {
1244 /* Controller's FIFO size */
1245 info
->chunk_size
= 2048;
1246 host
->read_id_bytes
= 4;
1248 info
->chunk_size
= 512;
1249 host
->read_id_bytes
= 2;
1252 /* Set an initial chunk size */
1253 info
->reg_ndcr
= ndcr
& ~NDCR_INT_MASK
;
1254 info
->ndtr0cs0
= nand_readl(info
, NDTR0CS0
);
1255 info
->ndtr1cs0
= nand_readl(info
, NDTR1CS0
);
1260 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info
*info
)
1262 struct platform_device
*pdev
= info
->pdev
;
1263 int data_desc_offset
= info
->buf_size
- sizeof(struct pxa_dma_desc
);
1266 info
->data_buff
= kmalloc(info
->buf_size
, GFP_KERNEL
);
1267 if (info
->data_buff
== NULL
)
1272 info
->data_buff
= dma_alloc_coherent(&pdev
->dev
, info
->buf_size
,
1273 &info
->data_buff_phys
, GFP_KERNEL
);
1274 if (info
->data_buff
== NULL
) {
1275 dev_err(&pdev
->dev
, "failed to allocate dma buffer\n");
1279 info
->data_desc
= (void *)info
->data_buff
+ data_desc_offset
;
1280 info
->data_desc_addr
= info
->data_buff_phys
+ data_desc_offset
;
1282 info
->data_dma_ch
= pxa_request_dma("nand-data", DMA_PRIO_LOW
,
1283 pxa3xx_nand_data_dma_irq
, info
);
1284 if (info
->data_dma_ch
< 0) {
1285 dev_err(&pdev
->dev
, "failed to request data dma\n");
1286 dma_free_coherent(&pdev
->dev
, info
->buf_size
,
1287 info
->data_buff
, info
->data_buff_phys
);
1288 return info
->data_dma_ch
;
1292 * Now that DMA buffers are allocated we turn on
1293 * DMA proper for I/O operations.
1299 static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info
*info
)
1301 struct platform_device
*pdev
= info
->pdev
;
1302 if (info
->use_dma
) {
1303 pxa_free_dma(info
->data_dma_ch
);
1304 dma_free_coherent(&pdev
->dev
, info
->buf_size
,
1305 info
->data_buff
, info
->data_buff_phys
);
1307 kfree(info
->data_buff
);
1311 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info
*info
)
1313 info
->data_buff
= kmalloc(info
->buf_size
, GFP_KERNEL
);
1314 if (info
->data_buff
== NULL
)
1319 static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info
*info
)
1321 kfree(info
->data_buff
);
1325 static int pxa3xx_nand_sensing(struct pxa3xx_nand_info
*info
)
1327 struct mtd_info
*mtd
;
1328 struct nand_chip
*chip
;
1331 mtd
= info
->host
[info
->cs
]->mtd
;
1334 /* use the common timing to make a try */
1335 ret
= pxa3xx_nand_config_flash(info
, &builtin_flash_types
[0]);
1339 chip
->cmdfunc(mtd
, NAND_CMD_RESET
, 0, 0);
1340 ret
= chip
->waitfunc(mtd
, chip
);
1341 if (ret
& NAND_STATUS_FAIL
)
1347 static int pxa_ecc_init(struct pxa3xx_nand_info
*info
,
1348 struct nand_ecc_ctrl
*ecc
,
1349 int strength
, int ecc_stepsize
, int page_size
)
1351 if (strength
== 1 && ecc_stepsize
== 512 && page_size
== 2048) {
1352 info
->chunk_size
= 2048;
1353 info
->spare_size
= 40;
1354 info
->ecc_size
= 24;
1355 ecc
->mode
= NAND_ECC_HW
;
1360 } else if (strength
== 1 && ecc_stepsize
== 512 && page_size
== 512) {
1361 info
->chunk_size
= 512;
1362 info
->spare_size
= 8;
1364 ecc
->mode
= NAND_ECC_HW
;
1370 * Required ECC: 4-bit correction per 512 bytes
1371 * Select: 16-bit correction per 2048 bytes
1373 } else if (strength
== 4 && ecc_stepsize
== 512 && page_size
== 2048) {
1375 info
->chunk_size
= 2048;
1376 info
->spare_size
= 32;
1377 info
->ecc_size
= 32;
1378 ecc
->mode
= NAND_ECC_HW
;
1379 ecc
->size
= info
->chunk_size
;
1380 ecc
->layout
= &ecc_layout_2KB_bch4bit
;
1384 } else if (strength
== 4 && ecc_stepsize
== 512 && page_size
== 4096) {
1386 info
->chunk_size
= 2048;
1387 info
->spare_size
= 32;
1388 info
->ecc_size
= 32;
1389 ecc
->mode
= NAND_ECC_HW
;
1390 ecc
->size
= info
->chunk_size
;
1391 ecc
->layout
= &ecc_layout_4KB_bch4bit
;
1396 * Required ECC: 8-bit correction per 512 bytes
1397 * Select: 16-bit correction per 1024 bytes
1399 } else if (strength
== 8 && ecc_stepsize
== 512 && page_size
== 4096) {
1401 info
->chunk_size
= 1024;
1402 info
->spare_size
= 0;
1403 info
->ecc_size
= 32;
1404 ecc
->mode
= NAND_ECC_HW
;
1405 ecc
->size
= info
->chunk_size
;
1406 ecc
->layout
= &ecc_layout_4KB_bch8bit
;
1413 static int pxa3xx_nand_scan(struct mtd_info
*mtd
)
1415 struct pxa3xx_nand_host
*host
= mtd
->priv
;
1416 struct pxa3xx_nand_info
*info
= host
->info_data
;
1417 struct platform_device
*pdev
= info
->pdev
;
1418 struct pxa3xx_nand_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
1419 struct nand_flash_dev pxa3xx_flash_ids
[2], *def
= NULL
;
1420 const struct pxa3xx_nand_flash
*f
= NULL
;
1421 struct nand_chip
*chip
= mtd
->priv
;
1425 uint16_t ecc_strength
, ecc_step
;
1427 if (pdata
->keep_config
&& !pxa3xx_nand_detect_config(info
))
1430 ret
= pxa3xx_nand_sensing(info
);
1432 dev_info(&info
->pdev
->dev
, "There is no chip on cs %d!\n",
1438 chip
->cmdfunc(mtd
, NAND_CMD_READID
, 0, 0);
1439 id
= *((uint16_t *)(info
->data_buff
));
1441 dev_info(&info
->pdev
->dev
, "Detect a flash id %x\n", id
);
1443 dev_warn(&info
->pdev
->dev
,
1444 "Read out ID 0, potential timing set wrong!!\n");
1449 num
= ARRAY_SIZE(builtin_flash_types
) + pdata
->num_flash
- 1;
1450 for (i
= 0; i
< num
; i
++) {
1451 if (i
< pdata
->num_flash
)
1452 f
= pdata
->flash
+ i
;
1454 f
= &builtin_flash_types
[i
- pdata
->num_flash
+ 1];
1456 /* find the chip in default list */
1457 if (f
->chip_id
== id
)
1461 if (i
>= (ARRAY_SIZE(builtin_flash_types
) + pdata
->num_flash
- 1)) {
1462 dev_err(&info
->pdev
->dev
, "ERROR!! flash not defined!!!\n");
1467 ret
= pxa3xx_nand_config_flash(info
, f
);
1469 dev_err(&info
->pdev
->dev
, "ERROR! Configure failed\n");
1473 pxa3xx_flash_ids
[0].name
= f
->name
;
1474 pxa3xx_flash_ids
[0].dev_id
= (f
->chip_id
>> 8) & 0xffff;
1475 pxa3xx_flash_ids
[0].pagesize
= f
->page_size
;
1476 chipsize
= (uint64_t)f
->num_blocks
* f
->page_per_block
* f
->page_size
;
1477 pxa3xx_flash_ids
[0].chipsize
= chipsize
>> 20;
1478 pxa3xx_flash_ids
[0].erasesize
= f
->page_size
* f
->page_per_block
;
1479 if (f
->flash_width
== 16)
1480 pxa3xx_flash_ids
[0].options
= NAND_BUSWIDTH_16
;
1481 pxa3xx_flash_ids
[1].name
= NULL
;
1482 def
= pxa3xx_flash_ids
;
1484 if (info
->reg_ndcr
& NDCR_DWIDTH_M
)
1485 chip
->options
|= NAND_BUSWIDTH_16
;
1487 /* Device detection must be done with ECC disabled */
1488 if (info
->variant
== PXA3XX_NAND_VARIANT_ARMADA370
)
1489 nand_writel(info
, NDECCCTRL
, 0x0);
1491 if (nand_scan_ident(mtd
, 1, def
))
1494 if (pdata
->flash_bbt
) {
1496 * We'll use a bad block table stored in-flash and don't
1497 * allow writing the bad block marker to the flash.
1499 chip
->bbt_options
|= NAND_BBT_USE_FLASH
|
1500 NAND_BBT_NO_OOB_BBM
;
1501 chip
->bbt_td
= &bbt_main_descr
;
1502 chip
->bbt_md
= &bbt_mirror_descr
;
1506 * If the page size is bigger than the FIFO size, let's check
1507 * we are given the right variant and then switch to the extended
1508 * (aka splitted) command handling,
1510 if (mtd
->writesize
> PAGE_CHUNK_SIZE
) {
1511 if (info
->variant
== PXA3XX_NAND_VARIANT_ARMADA370
) {
1512 chip
->cmdfunc
= nand_cmdfunc_extended
;
1514 dev_err(&info
->pdev
->dev
,
1515 "unsupported page size on this variant\n");
1520 ecc_strength
= chip
->ecc_strength_ds
;
1521 ecc_step
= chip
->ecc_step_ds
;
1523 /* Set default ECC strength requirements on non-ONFI devices */
1524 if (ecc_strength
< 1 && ecc_step
< 1) {
1529 ret
= pxa_ecc_init(info
, &chip
->ecc
, ecc_strength
,
1530 ecc_step
, mtd
->writesize
);
1532 dev_err(&info
->pdev
->dev
,
1533 "ECC strength %d at page size %d is not supported\n",
1534 chip
->ecc_strength_ds
, mtd
->writesize
);
1538 /* calculate addressing information */
1539 if (mtd
->writesize
>= 2048)
1540 host
->col_addr_cycles
= 2;
1542 host
->col_addr_cycles
= 1;
1544 /* release the initial buffer */
1545 kfree(info
->data_buff
);
1547 /* allocate the real data + oob buffer */
1548 info
->buf_size
= mtd
->writesize
+ mtd
->oobsize
;
1549 ret
= pxa3xx_nand_init_buff(info
);
1552 info
->oob_buff
= info
->data_buff
+ mtd
->writesize
;
1554 if ((mtd
->size
>> chip
->page_shift
) > 65536)
1555 host
->row_addr_cycles
= 3;
1557 host
->row_addr_cycles
= 2;
1558 return nand_scan_tail(mtd
);
1561 static int alloc_nand_resource(struct platform_device
*pdev
)
1563 struct pxa3xx_nand_platform_data
*pdata
;
1564 struct pxa3xx_nand_info
*info
;
1565 struct pxa3xx_nand_host
*host
;
1566 struct nand_chip
*chip
= NULL
;
1567 struct mtd_info
*mtd
;
1571 pdata
= dev_get_platdata(&pdev
->dev
);
1572 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
) + (sizeof(*mtd
) +
1573 sizeof(*host
)) * pdata
->num_cs
, GFP_KERNEL
);
1578 info
->variant
= pxa3xx_nand_get_variant(pdev
);
1579 for (cs
= 0; cs
< pdata
->num_cs
; cs
++) {
1580 mtd
= (struct mtd_info
*)((unsigned int)&info
[1] +
1581 (sizeof(*mtd
) + sizeof(*host
)) * cs
);
1582 chip
= (struct nand_chip
*)(&mtd
[1]);
1583 host
= (struct pxa3xx_nand_host
*)chip
;
1584 info
->host
[cs
] = host
;
1587 host
->info_data
= info
;
1589 mtd
->owner
= THIS_MODULE
;
1591 chip
->ecc
.read_page
= pxa3xx_nand_read_page_hwecc
;
1592 chip
->ecc
.write_page
= pxa3xx_nand_write_page_hwecc
;
1593 chip
->controller
= &info
->controller
;
1594 chip
->waitfunc
= pxa3xx_nand_waitfunc
;
1595 chip
->select_chip
= pxa3xx_nand_select_chip
;
1596 chip
->read_word
= pxa3xx_nand_read_word
;
1597 chip
->read_byte
= pxa3xx_nand_read_byte
;
1598 chip
->read_buf
= pxa3xx_nand_read_buf
;
1599 chip
->write_buf
= pxa3xx_nand_write_buf
;
1600 chip
->options
|= NAND_NO_SUBPAGE_WRITE
;
1601 chip
->cmdfunc
= nand_cmdfunc
;
1604 spin_lock_init(&chip
->controller
->lock
);
1605 init_waitqueue_head(&chip
->controller
->wq
);
1606 info
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
1607 if (IS_ERR(info
->clk
)) {
1608 dev_err(&pdev
->dev
, "failed to get nand clock\n");
1609 return PTR_ERR(info
->clk
);
1611 ret
= clk_prepare_enable(info
->clk
);
1617 * This is a dirty hack to make this driver work from
1618 * devicetree bindings. It can be removed once we have
1619 * a prober DMA controller framework for DT.
1621 if (pdev
->dev
.of_node
&&
1622 of_machine_is_compatible("marvell,pxa3xx")) {
1623 info
->drcmr_dat
= 97;
1624 info
->drcmr_cmd
= 99;
1626 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1629 "no resource defined for data DMA\n");
1631 goto fail_disable_clk
;
1633 info
->drcmr_dat
= r
->start
;
1635 r
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
1638 "no resource defined for cmd DMA\n");
1640 goto fail_disable_clk
;
1642 info
->drcmr_cmd
= r
->start
;
1646 irq
= platform_get_irq(pdev
, 0);
1648 dev_err(&pdev
->dev
, "no IRQ resource defined\n");
1650 goto fail_disable_clk
;
1653 r
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1654 info
->mmio_base
= devm_ioremap_resource(&pdev
->dev
, r
);
1655 if (IS_ERR(info
->mmio_base
)) {
1656 ret
= PTR_ERR(info
->mmio_base
);
1657 goto fail_disable_clk
;
1659 info
->mmio_phys
= r
->start
;
1661 /* Allocate a buffer to allow flash detection */
1662 info
->buf_size
= INIT_BUFFER_SIZE
;
1663 info
->data_buff
= kmalloc(info
->buf_size
, GFP_KERNEL
);
1664 if (info
->data_buff
== NULL
) {
1666 goto fail_disable_clk
;
1669 /* initialize all interrupts to be disabled */
1670 disable_int(info
, NDSR_MASK
);
1672 ret
= request_irq(irq
, pxa3xx_nand_irq
, 0, pdev
->name
, info
);
1674 dev_err(&pdev
->dev
, "failed to request IRQ\n");
1678 platform_set_drvdata(pdev
, info
);
1683 free_irq(irq
, info
);
1684 kfree(info
->data_buff
);
1686 clk_disable_unprepare(info
->clk
);
1690 static int pxa3xx_nand_remove(struct platform_device
*pdev
)
1692 struct pxa3xx_nand_info
*info
= platform_get_drvdata(pdev
);
1693 struct pxa3xx_nand_platform_data
*pdata
;
1699 pdata
= dev_get_platdata(&pdev
->dev
);
1701 irq
= platform_get_irq(pdev
, 0);
1703 free_irq(irq
, info
);
1704 pxa3xx_nand_free_buff(info
);
1706 clk_disable_unprepare(info
->clk
);
1708 for (cs
= 0; cs
< pdata
->num_cs
; cs
++)
1709 nand_release(info
->host
[cs
]->mtd
);
1713 static int pxa3xx_nand_probe_dt(struct platform_device
*pdev
)
1715 struct pxa3xx_nand_platform_data
*pdata
;
1716 struct device_node
*np
= pdev
->dev
.of_node
;
1717 const struct of_device_id
*of_id
=
1718 of_match_device(pxa3xx_nand_dt_ids
, &pdev
->dev
);
1723 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1727 if (of_get_property(np
, "marvell,nand-enable-arbiter", NULL
))
1728 pdata
->enable_arbiter
= 1;
1729 if (of_get_property(np
, "marvell,nand-keep-config", NULL
))
1730 pdata
->keep_config
= 1;
1731 of_property_read_u32(np
, "num-cs", &pdata
->num_cs
);
1732 pdata
->flash_bbt
= of_get_nand_on_flash_bbt(np
);
1734 pdev
->dev
.platform_data
= pdata
;
1739 static int pxa3xx_nand_probe(struct platform_device
*pdev
)
1741 struct pxa3xx_nand_platform_data
*pdata
;
1742 struct mtd_part_parser_data ppdata
= {};
1743 struct pxa3xx_nand_info
*info
;
1744 int ret
, cs
, probe_success
;
1746 #ifndef ARCH_HAS_DMA
1749 dev_warn(&pdev
->dev
,
1750 "This platform can't do DMA on this device\n");
1753 ret
= pxa3xx_nand_probe_dt(pdev
);
1757 pdata
= dev_get_platdata(&pdev
->dev
);
1759 dev_err(&pdev
->dev
, "no platform data defined\n");
1763 ret
= alloc_nand_resource(pdev
);
1765 dev_err(&pdev
->dev
, "alloc nand resource failed\n");
1769 info
= platform_get_drvdata(pdev
);
1771 for (cs
= 0; cs
< pdata
->num_cs
; cs
++) {
1772 struct mtd_info
*mtd
= info
->host
[cs
]->mtd
;
1775 * The mtd name matches the one used in 'mtdparts' kernel
1776 * parameter. This name cannot be changed or otherwise
1777 * user's mtd partitions configuration would get broken.
1779 mtd
->name
= "pxa3xx_nand-0";
1781 ret
= pxa3xx_nand_scan(mtd
);
1783 dev_warn(&pdev
->dev
, "failed to scan nand at cs %d\n",
1788 ppdata
.of_node
= pdev
->dev
.of_node
;
1789 ret
= mtd_device_parse_register(mtd
, NULL
,
1790 &ppdata
, pdata
->parts
[cs
],
1791 pdata
->nr_parts
[cs
]);
1796 if (!probe_success
) {
1797 pxa3xx_nand_remove(pdev
);
1805 static int pxa3xx_nand_suspend(struct platform_device
*pdev
, pm_message_t state
)
1807 struct pxa3xx_nand_info
*info
= platform_get_drvdata(pdev
);
1808 struct pxa3xx_nand_platform_data
*pdata
;
1809 struct mtd_info
*mtd
;
1812 pdata
= dev_get_platdata(&pdev
->dev
);
1814 dev_err(&pdev
->dev
, "driver busy, state = %d\n", info
->state
);
1818 for (cs
= 0; cs
< pdata
->num_cs
; cs
++) {
1819 mtd
= info
->host
[cs
]->mtd
;
1826 static int pxa3xx_nand_resume(struct platform_device
*pdev
)
1828 struct pxa3xx_nand_info
*info
= platform_get_drvdata(pdev
);
1829 struct pxa3xx_nand_platform_data
*pdata
;
1830 struct mtd_info
*mtd
;
1833 pdata
= dev_get_platdata(&pdev
->dev
);
1834 /* We don't want to handle interrupt without calling mtd routine */
1835 disable_int(info
, NDCR_INT_MASK
);
1838 * Directly set the chip select to a invalid value,
1839 * then the driver would reset the timing according
1840 * to current chip select at the beginning of cmdfunc
1845 * As the spec says, the NDSR would be updated to 0x1800 when
1846 * doing the nand_clk disable/enable.
1847 * To prevent it damaging state machine of the driver, clear
1848 * all status before resume
1850 nand_writel(info
, NDSR
, NDSR_MASK
);
1851 for (cs
= 0; cs
< pdata
->num_cs
; cs
++) {
1852 mtd
= info
->host
[cs
]->mtd
;
1859 #define pxa3xx_nand_suspend NULL
1860 #define pxa3xx_nand_resume NULL
1863 static struct platform_driver pxa3xx_nand_driver
= {
1865 .name
= "pxa3xx-nand",
1866 .of_match_table
= pxa3xx_nand_dt_ids
,
1868 .probe
= pxa3xx_nand_probe
,
1869 .remove
= pxa3xx_nand_remove
,
1870 .suspend
= pxa3xx_nand_suspend
,
1871 .resume
= pxa3xx_nand_resume
,
1874 module_platform_driver(pxa3xx_nand_driver
);
1876 MODULE_LICENSE("GPL");
1877 MODULE_DESCRIPTION("PXA3xx NAND controller driver");