Partially revert "mtd: nand: pxa3xx: Introduce 'marvell,armada370-nand' compatible...
[linux-2.6/btrfs-unstable.git] / drivers / mtd / nand / pxa3xx_nand.c
blobf3d4feafd02bd3ed00477c714d2e212009a69bbe
1 /*
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>
22 #include <linux/io.h>
23 #include <linux/irq.h>
24 #include <linux/slab.h>
25 #include <linux/of.h>
26 #include <linux/of_device.h>
28 #if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
29 #define ARCH_HAS_DMA
30 #endif
32 #ifdef ARCH_HAS_DMA
33 #include <mach/dma.h>
34 #endif
36 #include <linux/platform_data/mtd-nand-pxa3xx.h>
38 #define CHIP_DELAY_TIMEOUT (2 * HZ/10)
39 #define NAND_STOP_DELAY (2 * HZ/50)
40 #define PAGE_CHUNK_SIZE (2048)
43 * Define a buffer size for the initial command that detects the flash device:
44 * STATUS, READID and PARAM. The largest of these is the PARAM command,
45 * needing 256 bytes.
47 #define INIT_BUFFER_SIZE 256
49 /* registers and bit definitions */
50 #define NDCR (0x00) /* Control register */
51 #define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
52 #define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
53 #define NDSR (0x14) /* Status Register */
54 #define NDPCR (0x18) /* Page Count Register */
55 #define NDBDR0 (0x1C) /* Bad Block Register 0 */
56 #define NDBDR1 (0x20) /* Bad Block Register 1 */
57 #define NDDB (0x40) /* Data Buffer */
58 #define NDCB0 (0x48) /* Command Buffer0 */
59 #define NDCB1 (0x4C) /* Command Buffer1 */
60 #define NDCB2 (0x50) /* Command Buffer2 */
62 #define NDCR_SPARE_EN (0x1 << 31)
63 #define NDCR_ECC_EN (0x1 << 30)
64 #define NDCR_DMA_EN (0x1 << 29)
65 #define NDCR_ND_RUN (0x1 << 28)
66 #define NDCR_DWIDTH_C (0x1 << 27)
67 #define NDCR_DWIDTH_M (0x1 << 26)
68 #define NDCR_PAGE_SZ (0x1 << 24)
69 #define NDCR_NCSX (0x1 << 23)
70 #define NDCR_ND_MODE (0x3 << 21)
71 #define NDCR_NAND_MODE (0x0)
72 #define NDCR_CLR_PG_CNT (0x1 << 20)
73 #define NDCR_STOP_ON_UNCOR (0x1 << 19)
74 #define NDCR_RD_ID_CNT_MASK (0x7 << 16)
75 #define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
77 #define NDCR_RA_START (0x1 << 15)
78 #define NDCR_PG_PER_BLK (0x1 << 14)
79 #define NDCR_ND_ARB_EN (0x1 << 12)
80 #define NDCR_INT_MASK (0xFFF)
82 #define NDSR_MASK (0xfff)
83 #define NDSR_RDY (0x1 << 12)
84 #define NDSR_FLASH_RDY (0x1 << 11)
85 #define NDSR_CS0_PAGED (0x1 << 10)
86 #define NDSR_CS1_PAGED (0x1 << 9)
87 #define NDSR_CS0_CMDD (0x1 << 8)
88 #define NDSR_CS1_CMDD (0x1 << 7)
89 #define NDSR_CS0_BBD (0x1 << 6)
90 #define NDSR_CS1_BBD (0x1 << 5)
91 #define NDSR_DBERR (0x1 << 4)
92 #define NDSR_SBERR (0x1 << 3)
93 #define NDSR_WRDREQ (0x1 << 2)
94 #define NDSR_RDDREQ (0x1 << 1)
95 #define NDSR_WRCMDREQ (0x1)
97 #define NDCB0_LEN_OVRD (0x1 << 28)
98 #define NDCB0_ST_ROW_EN (0x1 << 26)
99 #define NDCB0_AUTO_RS (0x1 << 25)
100 #define NDCB0_CSEL (0x1 << 24)
101 #define NDCB0_CMD_TYPE_MASK (0x7 << 21)
102 #define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
103 #define NDCB0_NC (0x1 << 20)
104 #define NDCB0_DBC (0x1 << 19)
105 #define NDCB0_ADDR_CYC_MASK (0x7 << 16)
106 #define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
107 #define NDCB0_CMD2_MASK (0xff << 8)
108 #define NDCB0_CMD1_MASK (0xff)
109 #define NDCB0_ADDR_CYC_SHIFT (16)
111 /* macros for registers read/write */
112 #define nand_writel(info, off, val) \
113 __raw_writel((val), (info)->mmio_base + (off))
115 #define nand_readl(info, off) \
116 __raw_readl((info)->mmio_base + (off))
118 /* error code and state */
119 enum {
120 ERR_NONE = 0,
121 ERR_DMABUSERR = -1,
122 ERR_SENDCMD = -2,
123 ERR_DBERR = -3,
124 ERR_BBERR = -4,
125 ERR_SBERR = -5,
128 enum {
129 STATE_IDLE = 0,
130 STATE_PREPARED,
131 STATE_CMD_HANDLE,
132 STATE_DMA_READING,
133 STATE_DMA_WRITING,
134 STATE_DMA_DONE,
135 STATE_PIO_READING,
136 STATE_PIO_WRITING,
137 STATE_CMD_DONE,
138 STATE_READY,
141 enum pxa3xx_nand_variant {
142 PXA3XX_NAND_VARIANT_PXA,
143 PXA3XX_NAND_VARIANT_ARMADA370,
146 struct pxa3xx_nand_host {
147 struct nand_chip chip;
148 struct mtd_info *mtd;
149 void *info_data;
151 /* page size of attached chip */
152 unsigned int page_size;
153 int use_ecc;
154 int cs;
156 /* calculated from pxa3xx_nand_flash data */
157 unsigned int col_addr_cycles;
158 unsigned int row_addr_cycles;
159 size_t read_id_bytes;
163 struct pxa3xx_nand_info {
164 struct nand_hw_control controller;
165 struct platform_device *pdev;
167 struct clk *clk;
168 void __iomem *mmio_base;
169 unsigned long mmio_phys;
170 struct completion cmd_complete;
172 unsigned int buf_start;
173 unsigned int buf_count;
174 unsigned int buf_size;
176 /* DMA information */
177 int drcmr_dat;
178 int drcmr_cmd;
180 unsigned char *data_buff;
181 unsigned char *oob_buff;
182 dma_addr_t data_buff_phys;
183 int data_dma_ch;
184 struct pxa_dma_desc *data_desc;
185 dma_addr_t data_desc_addr;
187 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
188 unsigned int state;
191 * This driver supports NFCv1 (as found in PXA SoC)
192 * and NFCv2 (as found in Armada 370/XP SoC).
194 enum pxa3xx_nand_variant variant;
196 int cs;
197 int use_ecc; /* use HW ECC ? */
198 int use_dma; /* use DMA ? */
199 int use_spare; /* use spare ? */
200 int is_ready;
202 unsigned int page_size; /* page size of attached chip */
203 unsigned int data_size; /* data size in FIFO */
204 unsigned int oob_size;
205 int retcode;
207 /* cached register value */
208 uint32_t reg_ndcr;
209 uint32_t ndtr0cs0;
210 uint32_t ndtr1cs0;
212 /* generated NDCBx register values */
213 uint32_t ndcb0;
214 uint32_t ndcb1;
215 uint32_t ndcb2;
216 uint32_t ndcb3;
219 static bool use_dma = 1;
220 module_param(use_dma, bool, 0444);
221 MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
223 static struct pxa3xx_nand_timing timing[] = {
224 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
225 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
226 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
227 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
230 static struct pxa3xx_nand_flash builtin_flash_types[] = {
231 { "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
232 { "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
233 { "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
234 { "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
235 { "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
236 { "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
237 { "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
238 { "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
239 { "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
242 /* Define a default flash type setting serve as flash detecting only */
243 #define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
245 #define NDTR0_tCH(c) (min((c), 7) << 19)
246 #define NDTR0_tCS(c) (min((c), 7) << 16)
247 #define NDTR0_tWH(c) (min((c), 7) << 11)
248 #define NDTR0_tWP(c) (min((c), 7) << 8)
249 #define NDTR0_tRH(c) (min((c), 7) << 3)
250 #define NDTR0_tRP(c) (min((c), 7) << 0)
252 #define NDTR1_tR(c) (min((c), 65535) << 16)
253 #define NDTR1_tWHR(c) (min((c), 15) << 4)
254 #define NDTR1_tAR(c) (min((c), 15) << 0)
256 /* convert nano-seconds to nand flash controller clock cycles */
257 #define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
259 static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
260 const struct pxa3xx_nand_timing *t)
262 struct pxa3xx_nand_info *info = host->info_data;
263 unsigned long nand_clk = clk_get_rate(info->clk);
264 uint32_t ndtr0, ndtr1;
266 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
267 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
268 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
269 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
270 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
271 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
273 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
274 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
275 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
277 info->ndtr0cs0 = ndtr0;
278 info->ndtr1cs0 = ndtr1;
279 nand_writel(info, NDTR0CS0, ndtr0);
280 nand_writel(info, NDTR1CS0, ndtr1);
283 static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
285 struct pxa3xx_nand_host *host = info->host[info->cs];
286 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
288 info->data_size = host->page_size;
289 if (!oob_enable) {
290 info->oob_size = 0;
291 return;
294 switch (host->page_size) {
295 case 2048:
296 info->oob_size = (info->use_ecc) ? 40 : 64;
297 break;
298 case 512:
299 info->oob_size = (info->use_ecc) ? 8 : 16;
300 break;
305 * NOTE: it is a must to set ND_RUN firstly, then write
306 * command buffer, otherwise, it does not work.
307 * We enable all the interrupt at the same time, and
308 * let pxa3xx_nand_irq to handle all logic.
310 static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
312 uint32_t ndcr;
314 ndcr = info->reg_ndcr;
316 if (info->use_ecc)
317 ndcr |= NDCR_ECC_EN;
318 else
319 ndcr &= ~NDCR_ECC_EN;
321 if (info->use_dma)
322 ndcr |= NDCR_DMA_EN;
323 else
324 ndcr &= ~NDCR_DMA_EN;
326 if (info->use_spare)
327 ndcr |= NDCR_SPARE_EN;
328 else
329 ndcr &= ~NDCR_SPARE_EN;
331 ndcr |= NDCR_ND_RUN;
333 /* clear status bits and run */
334 nand_writel(info, NDCR, 0);
335 nand_writel(info, NDSR, NDSR_MASK);
336 nand_writel(info, NDCR, ndcr);
339 static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
341 uint32_t ndcr;
342 int timeout = NAND_STOP_DELAY;
344 /* wait RUN bit in NDCR become 0 */
345 ndcr = nand_readl(info, NDCR);
346 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
347 ndcr = nand_readl(info, NDCR);
348 udelay(1);
351 if (timeout <= 0) {
352 ndcr &= ~NDCR_ND_RUN;
353 nand_writel(info, NDCR, ndcr);
355 /* clear status bits */
356 nand_writel(info, NDSR, NDSR_MASK);
359 static void __maybe_unused
360 enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
362 uint32_t ndcr;
364 ndcr = nand_readl(info, NDCR);
365 nand_writel(info, NDCR, ndcr & ~int_mask);
368 static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
370 uint32_t ndcr;
372 ndcr = nand_readl(info, NDCR);
373 nand_writel(info, NDCR, ndcr | int_mask);
376 static void handle_data_pio(struct pxa3xx_nand_info *info)
378 switch (info->state) {
379 case STATE_PIO_WRITING:
380 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
381 DIV_ROUND_UP(info->data_size, 4));
382 if (info->oob_size > 0)
383 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
384 DIV_ROUND_UP(info->oob_size, 4));
385 break;
386 case STATE_PIO_READING:
387 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
388 DIV_ROUND_UP(info->data_size, 4));
389 if (info->oob_size > 0)
390 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
391 DIV_ROUND_UP(info->oob_size, 4));
392 break;
393 default:
394 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
395 info->state);
396 BUG();
400 #ifdef ARCH_HAS_DMA
401 static void start_data_dma(struct pxa3xx_nand_info *info)
403 struct pxa_dma_desc *desc = info->data_desc;
404 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
406 desc->ddadr = DDADR_STOP;
407 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
409 switch (info->state) {
410 case STATE_DMA_WRITING:
411 desc->dsadr = info->data_buff_phys;
412 desc->dtadr = info->mmio_phys + NDDB;
413 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
414 break;
415 case STATE_DMA_READING:
416 desc->dtadr = info->data_buff_phys;
417 desc->dsadr = info->mmio_phys + NDDB;
418 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
419 break;
420 default:
421 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
422 info->state);
423 BUG();
426 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
427 DDADR(info->data_dma_ch) = info->data_desc_addr;
428 DCSR(info->data_dma_ch) |= DCSR_RUN;
431 static void pxa3xx_nand_data_dma_irq(int channel, void *data)
433 struct pxa3xx_nand_info *info = data;
434 uint32_t dcsr;
436 dcsr = DCSR(channel);
437 DCSR(channel) = dcsr;
439 if (dcsr & DCSR_BUSERR) {
440 info->retcode = ERR_DMABUSERR;
443 info->state = STATE_DMA_DONE;
444 enable_int(info, NDCR_INT_MASK);
445 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
447 #else
448 static void start_data_dma(struct pxa3xx_nand_info *info)
450 #endif
452 static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
454 struct pxa3xx_nand_info *info = devid;
455 unsigned int status, is_completed = 0;
456 unsigned int ready, cmd_done;
458 if (info->cs == 0) {
459 ready = NDSR_FLASH_RDY;
460 cmd_done = NDSR_CS0_CMDD;
461 } else {
462 ready = NDSR_RDY;
463 cmd_done = NDSR_CS1_CMDD;
466 status = nand_readl(info, NDSR);
468 if (status & NDSR_DBERR)
469 info->retcode = ERR_DBERR;
470 if (status & NDSR_SBERR)
471 info->retcode = ERR_SBERR;
472 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
473 /* whether use dma to transfer data */
474 if (info->use_dma) {
475 disable_int(info, NDCR_INT_MASK);
476 info->state = (status & NDSR_RDDREQ) ?
477 STATE_DMA_READING : STATE_DMA_WRITING;
478 start_data_dma(info);
479 goto NORMAL_IRQ_EXIT;
480 } else {
481 info->state = (status & NDSR_RDDREQ) ?
482 STATE_PIO_READING : STATE_PIO_WRITING;
483 handle_data_pio(info);
486 if (status & cmd_done) {
487 info->state = STATE_CMD_DONE;
488 is_completed = 1;
490 if (status & ready) {
491 info->is_ready = 1;
492 info->state = STATE_READY;
495 if (status & NDSR_WRCMDREQ) {
496 nand_writel(info, NDSR, NDSR_WRCMDREQ);
497 status &= ~NDSR_WRCMDREQ;
498 info->state = STATE_CMD_HANDLE;
501 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
502 * must be loaded by writing directly either 12 or 16
503 * bytes directly to NDCB0, four bytes at a time.
505 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
506 * but each NDCBx register can be read.
508 nand_writel(info, NDCB0, info->ndcb0);
509 nand_writel(info, NDCB0, info->ndcb1);
510 nand_writel(info, NDCB0, info->ndcb2);
512 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
513 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
514 nand_writel(info, NDCB0, info->ndcb3);
517 /* clear NDSR to let the controller exit the IRQ */
518 nand_writel(info, NDSR, status);
519 if (is_completed)
520 complete(&info->cmd_complete);
521 NORMAL_IRQ_EXIT:
522 return IRQ_HANDLED;
525 static inline int is_buf_blank(uint8_t *buf, size_t len)
527 for (; len > 0; len--)
528 if (*buf++ != 0xff)
529 return 0;
530 return 1;
533 static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
534 uint16_t column, int page_addr)
536 int addr_cycle, exec_cmd;
537 struct pxa3xx_nand_host *host;
538 struct mtd_info *mtd;
540 host = info->host[info->cs];
541 mtd = host->mtd;
542 addr_cycle = 0;
543 exec_cmd = 1;
545 /* reset data and oob column point to handle data */
546 info->buf_start = 0;
547 info->buf_count = 0;
548 info->oob_size = 0;
549 info->use_ecc = 0;
550 info->use_spare = 1;
551 info->is_ready = 0;
552 info->retcode = ERR_NONE;
553 if (info->cs != 0)
554 info->ndcb0 = NDCB0_CSEL;
555 else
556 info->ndcb0 = 0;
558 switch (command) {
559 case NAND_CMD_READ0:
560 case NAND_CMD_PAGEPROG:
561 info->use_ecc = 1;
562 case NAND_CMD_READOOB:
563 pxa3xx_set_datasize(info);
564 break;
565 case NAND_CMD_PARAM:
566 info->use_spare = 0;
567 break;
568 case NAND_CMD_SEQIN:
569 exec_cmd = 0;
570 break;
571 default:
572 info->ndcb1 = 0;
573 info->ndcb2 = 0;
574 info->ndcb3 = 0;
575 break;
578 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
579 + host->col_addr_cycles);
581 switch (command) {
582 case NAND_CMD_READOOB:
583 case NAND_CMD_READ0:
584 info->buf_start = column;
585 info->ndcb0 |= NDCB0_CMD_TYPE(0)
586 | addr_cycle
587 | NAND_CMD_READ0;
589 if (command == NAND_CMD_READOOB)
590 info->buf_start += mtd->writesize;
592 /* Second command setting for large pages */
593 if (host->page_size >= PAGE_CHUNK_SIZE)
594 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
596 case NAND_CMD_SEQIN:
597 /* small page addr setting */
598 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
599 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
600 | (column & 0xFF);
602 info->ndcb2 = 0;
603 } else {
604 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
605 | (column & 0xFFFF);
607 if (page_addr & 0xFF0000)
608 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
609 else
610 info->ndcb2 = 0;
613 info->buf_count = mtd->writesize + mtd->oobsize;
614 memset(info->data_buff, 0xFF, info->buf_count);
616 break;
618 case NAND_CMD_PAGEPROG:
619 if (is_buf_blank(info->data_buff,
620 (mtd->writesize + mtd->oobsize))) {
621 exec_cmd = 0;
622 break;
625 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
626 | NDCB0_AUTO_RS
627 | NDCB0_ST_ROW_EN
628 | NDCB0_DBC
629 | (NAND_CMD_PAGEPROG << 8)
630 | NAND_CMD_SEQIN
631 | addr_cycle;
632 break;
634 case NAND_CMD_PARAM:
635 info->buf_count = 256;
636 info->ndcb0 |= NDCB0_CMD_TYPE(0)
637 | NDCB0_ADDR_CYC(1)
638 | NDCB0_LEN_OVRD
639 | command;
640 info->ndcb1 = (column & 0xFF);
641 info->ndcb3 = 256;
642 info->data_size = 256;
643 break;
645 case NAND_CMD_READID:
646 info->buf_count = host->read_id_bytes;
647 info->ndcb0 |= NDCB0_CMD_TYPE(3)
648 | NDCB0_ADDR_CYC(1)
649 | command;
650 info->ndcb1 = (column & 0xFF);
652 info->data_size = 8;
653 break;
654 case NAND_CMD_STATUS:
655 info->buf_count = 1;
656 info->ndcb0 |= NDCB0_CMD_TYPE(4)
657 | NDCB0_ADDR_CYC(1)
658 | command;
660 info->data_size = 8;
661 break;
663 case NAND_CMD_ERASE1:
664 info->ndcb0 |= NDCB0_CMD_TYPE(2)
665 | NDCB0_AUTO_RS
666 | NDCB0_ADDR_CYC(3)
667 | NDCB0_DBC
668 | (NAND_CMD_ERASE2 << 8)
669 | NAND_CMD_ERASE1;
670 info->ndcb1 = page_addr;
671 info->ndcb2 = 0;
673 break;
674 case NAND_CMD_RESET:
675 info->ndcb0 |= NDCB0_CMD_TYPE(5)
676 | command;
678 break;
680 case NAND_CMD_ERASE2:
681 exec_cmd = 0;
682 break;
684 default:
685 exec_cmd = 0;
686 dev_err(&info->pdev->dev, "non-supported command %x\n",
687 command);
688 break;
691 return exec_cmd;
694 static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
695 int column, int page_addr)
697 struct pxa3xx_nand_host *host = mtd->priv;
698 struct pxa3xx_nand_info *info = host->info_data;
699 int ret, exec_cmd;
702 * if this is a x16 device ,then convert the input
703 * "byte" address into a "word" address appropriate
704 * for indexing a word-oriented device
706 if (info->reg_ndcr & NDCR_DWIDTH_M)
707 column /= 2;
710 * There may be different NAND chip hooked to
711 * different chip select, so check whether
712 * chip select has been changed, if yes, reset the timing
714 if (info->cs != host->cs) {
715 info->cs = host->cs;
716 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
717 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
720 info->state = STATE_PREPARED;
721 exec_cmd = prepare_command_pool(info, command, column, page_addr);
722 if (exec_cmd) {
723 init_completion(&info->cmd_complete);
724 pxa3xx_nand_start(info);
726 ret = wait_for_completion_timeout(&info->cmd_complete,
727 CHIP_DELAY_TIMEOUT);
728 if (!ret) {
729 dev_err(&info->pdev->dev, "Wait time out!!!\n");
730 /* Stop State Machine for next command cycle */
731 pxa3xx_nand_stop(info);
734 info->state = STATE_IDLE;
737 static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
738 struct nand_chip *chip, const uint8_t *buf, int oob_required)
740 chip->write_buf(mtd, buf, mtd->writesize);
741 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
743 return 0;
746 static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
747 struct nand_chip *chip, uint8_t *buf, int oob_required,
748 int page)
750 struct pxa3xx_nand_host *host = mtd->priv;
751 struct pxa3xx_nand_info *info = host->info_data;
753 chip->read_buf(mtd, buf, mtd->writesize);
754 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
756 if (info->retcode == ERR_SBERR) {
757 switch (info->use_ecc) {
758 case 1:
759 mtd->ecc_stats.corrected++;
760 break;
761 case 0:
762 default:
763 break;
765 } else if (info->retcode == ERR_DBERR) {
767 * for blank page (all 0xff), HW will calculate its ECC as
768 * 0, which is different from the ECC information within
769 * OOB, ignore such double bit errors
771 if (is_buf_blank(buf, mtd->writesize))
772 info->retcode = ERR_NONE;
773 else
774 mtd->ecc_stats.failed++;
777 return 0;
780 static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
782 struct pxa3xx_nand_host *host = mtd->priv;
783 struct pxa3xx_nand_info *info = host->info_data;
784 char retval = 0xFF;
786 if (info->buf_start < info->buf_count)
787 /* Has just send a new command? */
788 retval = info->data_buff[info->buf_start++];
790 return retval;
793 static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
795 struct pxa3xx_nand_host *host = mtd->priv;
796 struct pxa3xx_nand_info *info = host->info_data;
797 u16 retval = 0xFFFF;
799 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
800 retval = *((u16 *)(info->data_buff+info->buf_start));
801 info->buf_start += 2;
803 return retval;
806 static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
808 struct pxa3xx_nand_host *host = mtd->priv;
809 struct pxa3xx_nand_info *info = host->info_data;
810 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
812 memcpy(buf, info->data_buff + info->buf_start, real_len);
813 info->buf_start += real_len;
816 static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
817 const uint8_t *buf, int len)
819 struct pxa3xx_nand_host *host = mtd->priv;
820 struct pxa3xx_nand_info *info = host->info_data;
821 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
823 memcpy(info->data_buff + info->buf_start, buf, real_len);
824 info->buf_start += real_len;
827 static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
829 return;
832 static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
834 struct pxa3xx_nand_host *host = mtd->priv;
835 struct pxa3xx_nand_info *info = host->info_data;
837 /* pxa3xx_nand_send_command has waited for command complete */
838 if (this->state == FL_WRITING || this->state == FL_ERASING) {
839 if (info->retcode == ERR_NONE)
840 return 0;
841 else {
843 * any error make it return 0x01 which will tell
844 * the caller the erase and write fail
846 return 0x01;
850 return 0;
853 static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
854 const struct pxa3xx_nand_flash *f)
856 struct platform_device *pdev = info->pdev;
857 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
858 struct pxa3xx_nand_host *host = info->host[info->cs];
859 uint32_t ndcr = 0x0; /* enable all interrupts */
861 if (f->page_size != 2048 && f->page_size != 512) {
862 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
863 return -EINVAL;
866 if (f->flash_width != 16 && f->flash_width != 8) {
867 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
868 return -EINVAL;
871 /* calculate flash information */
872 host->page_size = f->page_size;
873 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
875 /* calculate addressing information */
876 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
878 if (f->num_blocks * f->page_per_block > 65536)
879 host->row_addr_cycles = 3;
880 else
881 host->row_addr_cycles = 2;
883 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
884 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
885 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
886 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
887 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
888 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
890 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
891 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
893 info->reg_ndcr = ndcr;
895 pxa3xx_nand_set_timing(host, f->timing);
896 return 0;
899 static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
902 * We set 0 by hard coding here, for we don't support keep_config
903 * when there is more than one chip attached to the controller
905 struct pxa3xx_nand_host *host = info->host[0];
906 uint32_t ndcr = nand_readl(info, NDCR);
908 if (ndcr & NDCR_PAGE_SZ) {
909 host->page_size = 2048;
910 host->read_id_bytes = 4;
911 } else {
912 host->page_size = 512;
913 host->read_id_bytes = 2;
916 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
917 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
918 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
919 return 0;
922 #ifdef ARCH_HAS_DMA
923 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
925 struct platform_device *pdev = info->pdev;
926 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
928 if (use_dma == 0) {
929 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
930 if (info->data_buff == NULL)
931 return -ENOMEM;
932 return 0;
935 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
936 &info->data_buff_phys, GFP_KERNEL);
937 if (info->data_buff == NULL) {
938 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
939 return -ENOMEM;
942 info->data_desc = (void *)info->data_buff + data_desc_offset;
943 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
945 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
946 pxa3xx_nand_data_dma_irq, info);
947 if (info->data_dma_ch < 0) {
948 dev_err(&pdev->dev, "failed to request data dma\n");
949 dma_free_coherent(&pdev->dev, info->buf_size,
950 info->data_buff, info->data_buff_phys);
951 return info->data_dma_ch;
955 * Now that DMA buffers are allocated we turn on
956 * DMA proper for I/O operations.
958 info->use_dma = 1;
959 return 0;
962 static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
964 struct platform_device *pdev = info->pdev;
965 if (use_dma) {
966 pxa_free_dma(info->data_dma_ch);
967 dma_free_coherent(&pdev->dev, info->buf_size,
968 info->data_buff, info->data_buff_phys);
969 } else {
970 kfree(info->data_buff);
973 #else
974 static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
976 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
977 if (info->data_buff == NULL)
978 return -ENOMEM;
979 return 0;
982 static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
984 kfree(info->data_buff);
986 #endif
988 static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
990 struct mtd_info *mtd;
991 int ret;
992 mtd = info->host[info->cs]->mtd;
993 /* use the common timing to make a try */
994 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
995 if (ret)
996 return ret;
998 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
999 if (info->is_ready)
1000 return 0;
1002 return -ENODEV;
1005 static int pxa3xx_nand_scan(struct mtd_info *mtd)
1007 struct pxa3xx_nand_host *host = mtd->priv;
1008 struct pxa3xx_nand_info *info = host->info_data;
1009 struct platform_device *pdev = info->pdev;
1010 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
1011 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
1012 const struct pxa3xx_nand_flash *f = NULL;
1013 struct nand_chip *chip = mtd->priv;
1014 uint32_t id = -1;
1015 uint64_t chipsize;
1016 int i, ret, num;
1018 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
1019 goto KEEP_CONFIG;
1021 ret = pxa3xx_nand_sensing(info);
1022 if (ret) {
1023 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1024 info->cs);
1026 return ret;
1029 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1030 id = *((uint16_t *)(info->data_buff));
1031 if (id != 0)
1032 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
1033 else {
1034 dev_warn(&info->pdev->dev,
1035 "Read out ID 0, potential timing set wrong!!\n");
1037 return -EINVAL;
1040 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1041 for (i = 0; i < num; i++) {
1042 if (i < pdata->num_flash)
1043 f = pdata->flash + i;
1044 else
1045 f = &builtin_flash_types[i - pdata->num_flash + 1];
1047 /* find the chip in default list */
1048 if (f->chip_id == id)
1049 break;
1052 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
1053 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
1055 return -EINVAL;
1058 ret = pxa3xx_nand_config_flash(info, f);
1059 if (ret) {
1060 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1061 return ret;
1064 pxa3xx_flash_ids[0].name = f->name;
1065 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
1066 pxa3xx_flash_ids[0].pagesize = f->page_size;
1067 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1068 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1069 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1070 if (f->flash_width == 16)
1071 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
1072 pxa3xx_flash_ids[1].name = NULL;
1073 def = pxa3xx_flash_ids;
1074 KEEP_CONFIG:
1075 chip->ecc.mode = NAND_ECC_HW;
1076 chip->ecc.size = host->page_size;
1077 chip->ecc.strength = 1;
1079 if (info->reg_ndcr & NDCR_DWIDTH_M)
1080 chip->options |= NAND_BUSWIDTH_16;
1082 if (nand_scan_ident(mtd, 1, def))
1083 return -ENODEV;
1084 /* calculate addressing information */
1085 if (mtd->writesize >= 2048)
1086 host->col_addr_cycles = 2;
1087 else
1088 host->col_addr_cycles = 1;
1090 /* release the initial buffer */
1091 kfree(info->data_buff);
1093 /* allocate the real data + oob buffer */
1094 info->buf_size = mtd->writesize + mtd->oobsize;
1095 ret = pxa3xx_nand_init_buff(info);
1096 if (ret)
1097 return ret;
1098 info->oob_buff = info->data_buff + mtd->writesize;
1100 if ((mtd->size >> chip->page_shift) > 65536)
1101 host->row_addr_cycles = 3;
1102 else
1103 host->row_addr_cycles = 2;
1104 return nand_scan_tail(mtd);
1107 static int alloc_nand_resource(struct platform_device *pdev)
1109 struct pxa3xx_nand_platform_data *pdata;
1110 struct pxa3xx_nand_info *info;
1111 struct pxa3xx_nand_host *host;
1112 struct nand_chip *chip = NULL;
1113 struct mtd_info *mtd;
1114 struct resource *r;
1115 int ret, irq, cs;
1117 pdata = dev_get_platdata(&pdev->dev);
1118 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1119 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1120 if (!info)
1121 return -ENOMEM;
1123 info->pdev = pdev;
1124 for (cs = 0; cs < pdata->num_cs; cs++) {
1125 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1126 (sizeof(*mtd) + sizeof(*host)) * cs);
1127 chip = (struct nand_chip *)(&mtd[1]);
1128 host = (struct pxa3xx_nand_host *)chip;
1129 info->host[cs] = host;
1130 host->mtd = mtd;
1131 host->cs = cs;
1132 host->info_data = info;
1133 mtd->priv = host;
1134 mtd->owner = THIS_MODULE;
1136 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1137 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1138 chip->controller = &info->controller;
1139 chip->waitfunc = pxa3xx_nand_waitfunc;
1140 chip->select_chip = pxa3xx_nand_select_chip;
1141 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1142 chip->read_word = pxa3xx_nand_read_word;
1143 chip->read_byte = pxa3xx_nand_read_byte;
1144 chip->read_buf = pxa3xx_nand_read_buf;
1145 chip->write_buf = pxa3xx_nand_write_buf;
1148 spin_lock_init(&chip->controller->lock);
1149 init_waitqueue_head(&chip->controller->wq);
1150 info->clk = devm_clk_get(&pdev->dev, NULL);
1151 if (IS_ERR(info->clk)) {
1152 dev_err(&pdev->dev, "failed to get nand clock\n");
1153 return PTR_ERR(info->clk);
1155 ret = clk_prepare_enable(info->clk);
1156 if (ret < 0)
1157 return ret;
1159 if (use_dma) {
1161 * This is a dirty hack to make this driver work from
1162 * devicetree bindings. It can be removed once we have
1163 * a prober DMA controller framework for DT.
1165 if (pdev->dev.of_node &&
1166 of_machine_is_compatible("marvell,pxa3xx")) {
1167 info->drcmr_dat = 97;
1168 info->drcmr_cmd = 99;
1169 } else {
1170 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1171 if (r == NULL) {
1172 dev_err(&pdev->dev,
1173 "no resource defined for data DMA\n");
1174 ret = -ENXIO;
1175 goto fail_disable_clk;
1177 info->drcmr_dat = r->start;
1179 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1180 if (r == NULL) {
1181 dev_err(&pdev->dev,
1182 "no resource defined for cmd DMA\n");
1183 ret = -ENXIO;
1184 goto fail_disable_clk;
1186 info->drcmr_cmd = r->start;
1190 irq = platform_get_irq(pdev, 0);
1191 if (irq < 0) {
1192 dev_err(&pdev->dev, "no IRQ resource defined\n");
1193 ret = -ENXIO;
1194 goto fail_disable_clk;
1197 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1198 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1199 if (IS_ERR(info->mmio_base)) {
1200 ret = PTR_ERR(info->mmio_base);
1201 goto fail_disable_clk;
1203 info->mmio_phys = r->start;
1205 /* Allocate a buffer to allow flash detection */
1206 info->buf_size = INIT_BUFFER_SIZE;
1207 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1208 if (info->data_buff == NULL) {
1209 ret = -ENOMEM;
1210 goto fail_disable_clk;
1213 /* initialize all interrupts to be disabled */
1214 disable_int(info, NDSR_MASK);
1216 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
1217 if (ret < 0) {
1218 dev_err(&pdev->dev, "failed to request IRQ\n");
1219 goto fail_free_buf;
1222 platform_set_drvdata(pdev, info);
1224 return 0;
1226 fail_free_buf:
1227 free_irq(irq, info);
1228 kfree(info->data_buff);
1229 fail_disable_clk:
1230 clk_disable_unprepare(info->clk);
1231 return ret;
1234 static int pxa3xx_nand_remove(struct platform_device *pdev)
1236 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1237 struct pxa3xx_nand_platform_data *pdata;
1238 int irq, cs;
1240 if (!info)
1241 return 0;
1243 pdata = dev_get_platdata(&pdev->dev);
1245 irq = platform_get_irq(pdev, 0);
1246 if (irq >= 0)
1247 free_irq(irq, info);
1248 pxa3xx_nand_free_buff(info);
1250 clk_disable_unprepare(info->clk);
1252 for (cs = 0; cs < pdata->num_cs; cs++)
1253 nand_release(info->host[cs]->mtd);
1254 return 0;
1257 static struct of_device_id pxa3xx_nand_dt_ids[] = {
1259 .compatible = "marvell,pxa3xx-nand",
1260 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1264 MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
1266 static enum pxa3xx_nand_variant
1267 pxa3xx_nand_get_variant(struct platform_device *pdev)
1269 const struct of_device_id *of_id =
1270 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1271 if (!of_id)
1272 return PXA3XX_NAND_VARIANT_PXA;
1273 return (enum pxa3xx_nand_variant)of_id->data;
1276 static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1278 struct pxa3xx_nand_platform_data *pdata;
1279 struct device_node *np = pdev->dev.of_node;
1280 const struct of_device_id *of_id =
1281 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1283 if (!of_id)
1284 return 0;
1286 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1287 if (!pdata)
1288 return -ENOMEM;
1290 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1291 pdata->enable_arbiter = 1;
1292 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1293 pdata->keep_config = 1;
1294 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1296 pdev->dev.platform_data = pdata;
1298 return 0;
1301 static int pxa3xx_nand_probe(struct platform_device *pdev)
1303 struct pxa3xx_nand_platform_data *pdata;
1304 struct mtd_part_parser_data ppdata = {};
1305 struct pxa3xx_nand_info *info;
1306 int ret, cs, probe_success;
1308 #ifndef ARCH_HAS_DMA
1309 if (use_dma) {
1310 use_dma = 0;
1311 dev_warn(&pdev->dev,
1312 "This platform can't do DMA on this device\n");
1314 #endif
1315 ret = pxa3xx_nand_probe_dt(pdev);
1316 if (ret)
1317 return ret;
1319 pdata = dev_get_platdata(&pdev->dev);
1320 if (!pdata) {
1321 dev_err(&pdev->dev, "no platform data defined\n");
1322 return -ENODEV;
1325 ret = alloc_nand_resource(pdev);
1326 if (ret) {
1327 dev_err(&pdev->dev, "alloc nand resource failed\n");
1328 return ret;
1331 info = platform_get_drvdata(pdev);
1332 info->variant = pxa3xx_nand_get_variant(pdev);
1333 probe_success = 0;
1334 for (cs = 0; cs < pdata->num_cs; cs++) {
1335 struct mtd_info *mtd = info->host[cs]->mtd;
1338 * The mtd name matches the one used in 'mtdparts' kernel
1339 * parameter. This name cannot be changed or otherwise
1340 * user's mtd partitions configuration would get broken.
1342 mtd->name = "pxa3xx_nand-0";
1343 info->cs = cs;
1344 ret = pxa3xx_nand_scan(mtd);
1345 if (ret) {
1346 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1347 cs);
1348 continue;
1351 ppdata.of_node = pdev->dev.of_node;
1352 ret = mtd_device_parse_register(mtd, NULL,
1353 &ppdata, pdata->parts[cs],
1354 pdata->nr_parts[cs]);
1355 if (!ret)
1356 probe_success = 1;
1359 if (!probe_success) {
1360 pxa3xx_nand_remove(pdev);
1361 return -ENODEV;
1364 return 0;
1367 #ifdef CONFIG_PM
1368 static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1370 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1371 struct pxa3xx_nand_platform_data *pdata;
1372 struct mtd_info *mtd;
1373 int cs;
1375 pdata = dev_get_platdata(&pdev->dev);
1376 if (info->state) {
1377 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1378 return -EAGAIN;
1381 for (cs = 0; cs < pdata->num_cs; cs++) {
1382 mtd = info->host[cs]->mtd;
1383 mtd_suspend(mtd);
1386 return 0;
1389 static int pxa3xx_nand_resume(struct platform_device *pdev)
1391 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1392 struct pxa3xx_nand_platform_data *pdata;
1393 struct mtd_info *mtd;
1394 int cs;
1396 pdata = dev_get_platdata(&pdev->dev);
1397 /* We don't want to handle interrupt without calling mtd routine */
1398 disable_int(info, NDCR_INT_MASK);
1401 * Directly set the chip select to a invalid value,
1402 * then the driver would reset the timing according
1403 * to current chip select at the beginning of cmdfunc
1405 info->cs = 0xff;
1408 * As the spec says, the NDSR would be updated to 0x1800 when
1409 * doing the nand_clk disable/enable.
1410 * To prevent it damaging state machine of the driver, clear
1411 * all status before resume
1413 nand_writel(info, NDSR, NDSR_MASK);
1414 for (cs = 0; cs < pdata->num_cs; cs++) {
1415 mtd = info->host[cs]->mtd;
1416 mtd_resume(mtd);
1419 return 0;
1421 #else
1422 #define pxa3xx_nand_suspend NULL
1423 #define pxa3xx_nand_resume NULL
1424 #endif
1426 static struct platform_driver pxa3xx_nand_driver = {
1427 .driver = {
1428 .name = "pxa3xx-nand",
1429 .of_match_table = pxa3xx_nand_dt_ids,
1431 .probe = pxa3xx_nand_probe,
1432 .remove = pxa3xx_nand_remove,
1433 .suspend = pxa3xx_nand_suspend,
1434 .resume = pxa3xx_nand_resume,
1437 module_platform_driver(pxa3xx_nand_driver);
1439 MODULE_LICENSE("GPL");
1440 MODULE_DESCRIPTION("PXA3xx NAND controller driver");