blob: d63d378c4baadf281770fa334353b3c2f90de808 [file] [log] [blame]
eric miaofe69af02008-02-14 15:48:23 +08001/*
2 * drivers/mtd/nand/pxa3xx_nand.c
3 *
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
6 *
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.
10 */
11
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080012#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080013#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>
David Woodhousea1c06ee2008-04-22 20:39:43 +010022#include <linux/io.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
eric miaofe69af02008-02-14 15:48:23 +080027
Eric Miaoafb5b5c2008-12-01 11:43:08 +080028#include <mach/dma.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020029#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080030
31#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080032#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080033#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080034
35/* registers and bit definitions */
36#define NDCR (0x00) /* Control register */
37#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
38#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
39#define NDSR (0x14) /* Status Register */
40#define NDPCR (0x18) /* Page Count Register */
41#define NDBDR0 (0x1C) /* Bad Block Register 0 */
42#define NDBDR1 (0x20) /* Bad Block Register 1 */
43#define NDDB (0x40) /* Data Buffer */
44#define NDCB0 (0x48) /* Command Buffer0 */
45#define NDCB1 (0x4C) /* Command Buffer1 */
46#define NDCB2 (0x50) /* Command Buffer2 */
47
48#define NDCR_SPARE_EN (0x1 << 31)
49#define NDCR_ECC_EN (0x1 << 30)
50#define NDCR_DMA_EN (0x1 << 29)
51#define NDCR_ND_RUN (0x1 << 28)
52#define NDCR_DWIDTH_C (0x1 << 27)
53#define NDCR_DWIDTH_M (0x1 << 26)
54#define NDCR_PAGE_SZ (0x1 << 24)
55#define NDCR_NCSX (0x1 << 23)
56#define NDCR_ND_MODE (0x3 << 21)
57#define NDCR_NAND_MODE (0x0)
58#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080059#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080060#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
61#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
62
63#define NDCR_RA_START (0x1 << 15)
64#define NDCR_PG_PER_BLK (0x1 << 14)
65#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080066#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080067
68#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080069#define NDSR_RDY (0x1 << 12)
70#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080071#define NDSR_CS0_PAGED (0x1 << 10)
72#define NDSR_CS1_PAGED (0x1 << 9)
73#define NDSR_CS0_CMDD (0x1 << 8)
74#define NDSR_CS1_CMDD (0x1 << 7)
75#define NDSR_CS0_BBD (0x1 << 6)
76#define NDSR_CS1_BBD (0x1 << 5)
77#define NDSR_DBERR (0x1 << 4)
78#define NDSR_SBERR (0x1 << 3)
79#define NDSR_WRDREQ (0x1 << 2)
80#define NDSR_RDDREQ (0x1 << 1)
81#define NDSR_WRCMDREQ (0x1)
82
Lei Wen4eb2da82011-02-28 10:32:13 +080083#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080084#define NDCB0_AUTO_RS (0x1 << 25)
85#define NDCB0_CSEL (0x1 << 24)
86#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
87#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
88#define NDCB0_NC (0x1 << 20)
89#define NDCB0_DBC (0x1 << 19)
90#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
91#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
92#define NDCB0_CMD2_MASK (0xff << 8)
93#define NDCB0_CMD1_MASK (0xff)
94#define NDCB0_ADDR_CYC_SHIFT (16)
95
eric miaofe69af02008-02-14 15:48:23 +080096/* macros for registers read/write */
97#define nand_writel(info, off, val) \
98 __raw_writel((val), (info)->mmio_base + (off))
99
100#define nand_readl(info, off) \
101 __raw_readl((info)->mmio_base + (off))
102
103/* error code and state */
104enum {
105 ERR_NONE = 0,
106 ERR_DMABUSERR = -1,
107 ERR_SENDCMD = -2,
108 ERR_DBERR = -3,
109 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300110 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800111};
112
113enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800114 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700115 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800116 STATE_CMD_HANDLE,
117 STATE_DMA_READING,
118 STATE_DMA_WRITING,
119 STATE_DMA_DONE,
120 STATE_PIO_READING,
121 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800122 STATE_CMD_DONE,
123 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800124};
125
Lei Wend4568822011-07-14 20:44:32 -0700126struct pxa3xx_nand_host {
127 struct nand_chip chip;
128 struct pxa3xx_nand_cmdset *cmdset;
129 struct mtd_info *mtd;
130 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800131
Lei Wend4568822011-07-14 20:44:32 -0700132 /* page size of attached chip */
133 unsigned int page_size;
134 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700135 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700136
137 /* calculated from pxa3xx_nand_flash data */
138 unsigned int col_addr_cycles;
139 unsigned int row_addr_cycles;
140 size_t read_id_bytes;
141
142 /* cached register value */
143 uint32_t reg_ndcr;
144 uint32_t ndtr0cs0;
145 uint32_t ndtr1cs0;
146};
147
148struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800149 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800150 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800151
152 struct clk *clk;
153 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800154 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700155 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800156
157 unsigned int buf_start;
158 unsigned int buf_count;
159
160 /* DMA information */
161 int drcmr_dat;
162 int drcmr_cmd;
163
164 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800165 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800166 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800167 int data_dma_ch;
168 struct pxa_dma_desc *data_desc;
169 dma_addr_t data_desc_addr;
170
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700171 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800172 unsigned int state;
173
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700174 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800175 int use_ecc; /* use HW ECC ? */
176 int use_dma; /* use DMA ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800177 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800178
Lei Wen18c81b12010-08-17 17:25:57 +0800179 unsigned int page_size; /* page size of attached chip */
180 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700181 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800182 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800183
184 /* generated NDCBx register values */
185 uint32_t ndcb0;
186 uint32_t ndcb1;
187 uint32_t ndcb2;
188};
189
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030190static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800191module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300192MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800193
Mike Rapoportf2710492009-02-17 13:54:47 +0200194/*
195 * Default NAND flash controller configuration setup by the
196 * bootloader. This configuration is used only when pdata->keep_config is set
197 */
Lei Wenc1f82472010-08-17 13:50:23 +0800198static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800199 .read1 = 0x3000,
200 .read2 = 0x0050,
201 .program = 0x1080,
202 .read_status = 0x0070,
203 .read_id = 0x0090,
204 .erase = 0xD060,
205 .reset = 0x00FF,
206 .lock = 0x002A,
207 .unlock = 0x2423,
208 .lock_status = 0x007A,
209};
210
Lei Wenc1f82472010-08-17 13:50:23 +0800211static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800212 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
213 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
214 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
215 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800216};
217
Lei Wenc1f82472010-08-17 13:50:23 +0800218static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800219{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
220{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
221{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
222{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
223{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
224{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
225{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
226{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
227{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800228};
229
Lei Wen227a8862010-08-18 18:00:03 +0800230/* Define a default flash type setting serve as flash detecting only */
231#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
232
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700233const char *mtd_names[] = {"pxa3xx_nand-0", "pxa3xx_nand-1", NULL};
Lei Wen401e67e2011-02-28 10:32:14 +0800234
eric miaofe69af02008-02-14 15:48:23 +0800235#define NDTR0_tCH(c) (min((c), 7) << 19)
236#define NDTR0_tCS(c) (min((c), 7) << 16)
237#define NDTR0_tWH(c) (min((c), 7) << 11)
238#define NDTR0_tWP(c) (min((c), 7) << 8)
239#define NDTR0_tRH(c) (min((c), 7) << 3)
240#define NDTR0_tRP(c) (min((c), 7) << 0)
241
242#define NDTR1_tR(c) (min((c), 65535) << 16)
243#define NDTR1_tWHR(c) (min((c), 15) << 4)
244#define NDTR1_tAR(c) (min((c), 15) << 0)
245
246/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800247#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800248
Lei Wend4568822011-07-14 20:44:32 -0700249static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200250 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800251{
Lei Wend4568822011-07-14 20:44:32 -0700252 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800253 unsigned long nand_clk = clk_get_rate(info->clk);
254 uint32_t ndtr0, ndtr1;
255
256 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
257 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
258 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
259 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
260 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
261 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
262
263 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
264 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
265 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
266
Lei Wend4568822011-07-14 20:44:32 -0700267 host->ndtr0cs0 = ndtr0;
268 host->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800269 nand_writel(info, NDTR0CS0, ndtr0);
270 nand_writel(info, NDTR1CS0, ndtr1);
271}
272
Lei Wen18c81b12010-08-17 17:25:57 +0800273static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800274{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700275 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wend4568822011-07-14 20:44:32 -0700276 int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800277
Lei Wend4568822011-07-14 20:44:32 -0700278 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800279 if (!oob_enable) {
280 info->oob_size = 0;
281 return;
282 }
283
Lei Wend4568822011-07-14 20:44:32 -0700284 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800285 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800286 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800287 break;
288 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800289 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800290 break;
eric miaofe69af02008-02-14 15:48:23 +0800291 }
Lei Wen18c81b12010-08-17 17:25:57 +0800292}
293
Lei Wenf8155a42011-02-28 10:32:11 +0800294/**
295 * NOTE: it is a must to set ND_RUN firstly, then write
296 * command buffer, otherwise, it does not work.
297 * We enable all the interrupt at the same time, and
298 * let pxa3xx_nand_irq to handle all logic.
299 */
300static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
301{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700302 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800303 uint32_t ndcr;
304
Lei Wend4568822011-07-14 20:44:32 -0700305 ndcr = host->reg_ndcr;
Lei Wenf8155a42011-02-28 10:32:11 +0800306 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
307 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
308 ndcr |= NDCR_ND_RUN;
309
310 /* clear status bits and run */
311 nand_writel(info, NDCR, 0);
312 nand_writel(info, NDSR, NDSR_MASK);
313 nand_writel(info, NDCR, ndcr);
314}
315
316static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
317{
318 uint32_t ndcr;
319 int timeout = NAND_STOP_DELAY;
320
321 /* wait RUN bit in NDCR become 0 */
322 ndcr = nand_readl(info, NDCR);
323 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
324 ndcr = nand_readl(info, NDCR);
325 udelay(1);
326 }
327
328 if (timeout <= 0) {
329 ndcr &= ~NDCR_ND_RUN;
330 nand_writel(info, NDCR, ndcr);
331 }
332 /* clear status bits */
333 nand_writel(info, NDSR, NDSR_MASK);
334}
335
eric miaofe69af02008-02-14 15:48:23 +0800336static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
337{
338 uint32_t ndcr;
339
340 ndcr = nand_readl(info, NDCR);
341 nand_writel(info, NDCR, ndcr & ~int_mask);
342}
343
344static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
345{
346 uint32_t ndcr;
347
348 ndcr = nand_readl(info, NDCR);
349 nand_writel(info, NDCR, ndcr | int_mask);
350}
351
Lei Wenf8155a42011-02-28 10:32:11 +0800352static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800353{
eric miaofe69af02008-02-14 15:48:23 +0800354 switch (info->state) {
355 case STATE_PIO_WRITING:
356 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800357 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800358 if (info->oob_size > 0)
359 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
360 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800361 break;
362 case STATE_PIO_READING:
363 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800364 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800365 if (info->oob_size > 0)
366 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
367 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800368 break;
369 default:
Lei Wenda675b42011-07-14 20:44:31 -0700370 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800371 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800372 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800373 }
eric miaofe69af02008-02-14 15:48:23 +0800374}
375
Lei Wenf8155a42011-02-28 10:32:11 +0800376static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800377{
378 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800379 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800380
381 desc->ddadr = DDADR_STOP;
382 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
383
Lei Wenf8155a42011-02-28 10:32:11 +0800384 switch (info->state) {
385 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800386 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800387 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800388 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800389 break;
390 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800391 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800392 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800393 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800394 break;
395 default:
Lei Wenda675b42011-07-14 20:44:31 -0700396 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800397 info->state);
398 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800399 }
400
401 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
402 DDADR(info->data_dma_ch) = info->data_desc_addr;
403 DCSR(info->data_dma_ch) |= DCSR_RUN;
404}
405
406static void pxa3xx_nand_data_dma_irq(int channel, void *data)
407{
408 struct pxa3xx_nand_info *info = data;
409 uint32_t dcsr;
410
411 dcsr = DCSR(channel);
412 DCSR(channel) = dcsr;
413
414 if (dcsr & DCSR_BUSERR) {
415 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800416 }
417
Lei Wenf8155a42011-02-28 10:32:11 +0800418 info->state = STATE_DMA_DONE;
419 enable_int(info, NDCR_INT_MASK);
420 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800421}
422
423static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
424{
425 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800426 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700427 unsigned int ready, cmd_done;
428
429 if (info->cs == 0) {
430 ready = NDSR_FLASH_RDY;
431 cmd_done = NDSR_CS0_CMDD;
432 } else {
433 ready = NDSR_RDY;
434 cmd_done = NDSR_CS1_CMDD;
435 }
eric miaofe69af02008-02-14 15:48:23 +0800436
437 status = nand_readl(info, NDSR);
438
Lei Wenf8155a42011-02-28 10:32:11 +0800439 if (status & NDSR_DBERR)
440 info->retcode = ERR_DBERR;
441 if (status & NDSR_SBERR)
442 info->retcode = ERR_SBERR;
443 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
444 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800445 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800446 disable_int(info, NDCR_INT_MASK);
447 info->state = (status & NDSR_RDDREQ) ?
448 STATE_DMA_READING : STATE_DMA_WRITING;
449 start_data_dma(info);
450 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800451 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800452 info->state = (status & NDSR_RDDREQ) ?
453 STATE_PIO_READING : STATE_PIO_WRITING;
454 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800455 }
Lei Wenf8155a42011-02-28 10:32:11 +0800456 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700457 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800458 info->state = STATE_CMD_DONE;
459 is_completed = 1;
460 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700461 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800462 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800463 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800464 }
Lei Wenf8155a42011-02-28 10:32:11 +0800465
466 if (status & NDSR_WRCMDREQ) {
467 nand_writel(info, NDSR, NDSR_WRCMDREQ);
468 status &= ~NDSR_WRCMDREQ;
469 info->state = STATE_CMD_HANDLE;
470 nand_writel(info, NDCB0, info->ndcb0);
471 nand_writel(info, NDCB0, info->ndcb1);
472 nand_writel(info, NDCB0, info->ndcb2);
eric miaofe69af02008-02-14 15:48:23 +0800473 }
Lei Wenf8155a42011-02-28 10:32:11 +0800474
475 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800476 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800477 if (is_completed)
478 complete(&info->cmd_complete);
479NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800480 return IRQ_HANDLED;
481}
482
eric miaofe69af02008-02-14 15:48:23 +0800483static inline int is_buf_blank(uint8_t *buf, size_t len)
484{
485 for (; len > 0; len--)
486 if (*buf++ != 0xff)
487 return 0;
488 return 1;
489}
490
Lei Wen4eb2da82011-02-28 10:32:13 +0800491static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
492 uint16_t column, int page_addr)
493{
494 uint16_t cmd;
Lei Wend4568822011-07-14 20:44:32 -0700495 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700496 struct pxa3xx_nand_host *host;
497 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800498
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700499 host = info->host[info->cs];
500 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800501 addr_cycle = 0;
502 exec_cmd = 1;
503
504 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800505 info->buf_start = 0;
506 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800507 info->oob_size = 0;
508 info->use_ecc = 0;
Ezequiel Garcia0a60d042013-05-14 08:15:21 -0300509 info->use_dma = (use_dma) ? 1 : 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800510 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800511 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700512 if (info->cs != 0)
513 info->ndcb0 = NDCB0_CSEL;
514 else
515 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800516
517 switch (command) {
518 case NAND_CMD_READ0:
519 case NAND_CMD_PAGEPROG:
520 info->use_ecc = 1;
521 case NAND_CMD_READOOB:
522 pxa3xx_set_datasize(info);
523 break;
524 case NAND_CMD_SEQIN:
525 exec_cmd = 0;
526 break;
527 default:
528 info->ndcb1 = 0;
529 info->ndcb2 = 0;
530 break;
531 }
532
Lei Wend4568822011-07-14 20:44:32 -0700533 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
534 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800535
536 switch (command) {
537 case NAND_CMD_READOOB:
538 case NAND_CMD_READ0:
Lei Wend4568822011-07-14 20:44:32 -0700539 cmd = host->cmdset->read1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800540 if (command == NAND_CMD_READOOB)
541 info->buf_start = mtd->writesize + column;
542 else
543 info->buf_start = column;
544
Lei Wend4568822011-07-14 20:44:32 -0700545 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
Lei Wen4eb2da82011-02-28 10:32:13 +0800546 info->ndcb0 |= NDCB0_CMD_TYPE(0)
547 | addr_cycle
548 | (cmd & NDCB0_CMD1_MASK);
549 else
550 info->ndcb0 |= NDCB0_CMD_TYPE(0)
551 | NDCB0_DBC
552 | addr_cycle
553 | cmd;
554
555 case NAND_CMD_SEQIN:
556 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700557 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800558 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
559 | (column & 0xFF);
560
561 info->ndcb2 = 0;
562 } else {
563 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
564 | (column & 0xFFFF);
565
566 if (page_addr & 0xFF0000)
567 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
568 else
569 info->ndcb2 = 0;
570 }
571
572 info->buf_count = mtd->writesize + mtd->oobsize;
573 memset(info->data_buff, 0xFF, info->buf_count);
574
575 break;
576
577 case NAND_CMD_PAGEPROG:
578 if (is_buf_blank(info->data_buff,
579 (mtd->writesize + mtd->oobsize))) {
580 exec_cmd = 0;
581 break;
582 }
583
Lei Wend4568822011-07-14 20:44:32 -0700584 cmd = host->cmdset->program;
Lei Wen4eb2da82011-02-28 10:32:13 +0800585 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
586 | NDCB0_AUTO_RS
587 | NDCB0_ST_ROW_EN
588 | NDCB0_DBC
589 | cmd
590 | addr_cycle;
591 break;
592
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300593 case NAND_CMD_PARAM:
594 cmd = NAND_CMD_PARAM;
595 info->buf_count = 256;
596 info->ndcb0 |= NDCB0_CMD_TYPE(0)
597 | NDCB0_ADDR_CYC(1)
598 | cmd;
599 info->ndcb1 = (column & 0xFF);
600 info->data_size = 256;
601 break;
602
Lei Wen4eb2da82011-02-28 10:32:13 +0800603 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700604 cmd = host->cmdset->read_id;
605 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800606 info->ndcb0 |= NDCB0_CMD_TYPE(3)
607 | NDCB0_ADDR_CYC(1)
608 | cmd;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300609 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800610
611 info->data_size = 8;
612 break;
613 case NAND_CMD_STATUS:
Lei Wend4568822011-07-14 20:44:32 -0700614 cmd = host->cmdset->read_status;
Lei Wen4eb2da82011-02-28 10:32:13 +0800615 info->buf_count = 1;
616 info->ndcb0 |= NDCB0_CMD_TYPE(4)
617 | NDCB0_ADDR_CYC(1)
618 | cmd;
619
620 info->data_size = 8;
621 break;
622
623 case NAND_CMD_ERASE1:
Lei Wend4568822011-07-14 20:44:32 -0700624 cmd = host->cmdset->erase;
Lei Wen4eb2da82011-02-28 10:32:13 +0800625 info->ndcb0 |= NDCB0_CMD_TYPE(2)
626 | NDCB0_AUTO_RS
627 | NDCB0_ADDR_CYC(3)
628 | NDCB0_DBC
629 | cmd;
630 info->ndcb1 = page_addr;
631 info->ndcb2 = 0;
632
633 break;
634 case NAND_CMD_RESET:
Lei Wend4568822011-07-14 20:44:32 -0700635 cmd = host->cmdset->reset;
Lei Wen4eb2da82011-02-28 10:32:13 +0800636 info->ndcb0 |= NDCB0_CMD_TYPE(5)
637 | cmd;
638
639 break;
640
641 case NAND_CMD_ERASE2:
642 exec_cmd = 0;
643 break;
644
645 default:
646 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700647 dev_err(&info->pdev->dev, "non-supported command %x\n",
648 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800649 break;
650 }
651
652 return exec_cmd;
653}
654
eric miaofe69af02008-02-14 15:48:23 +0800655static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100656 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800657{
Lei Wend4568822011-07-14 20:44:32 -0700658 struct pxa3xx_nand_host *host = mtd->priv;
659 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800660 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800661
Lei Wen4eb2da82011-02-28 10:32:13 +0800662 /*
663 * if this is a x16 device ,then convert the input
664 * "byte" address into a "word" address appropriate
665 * for indexing a word-oriented device
666 */
Lei Wend4568822011-07-14 20:44:32 -0700667 if (host->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800668 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800669
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700670 /*
671 * There may be different NAND chip hooked to
672 * different chip select, so check whether
673 * chip select has been changed, if yes, reset the timing
674 */
675 if (info->cs != host->cs) {
676 info->cs = host->cs;
677 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
678 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
679 }
680
Lei Wend4568822011-07-14 20:44:32 -0700681 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800682 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800683 if (exec_cmd) {
684 init_completion(&info->cmd_complete);
685 pxa3xx_nand_start(info);
686
687 ret = wait_for_completion_timeout(&info->cmd_complete,
688 CHIP_DELAY_TIMEOUT);
689 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700690 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800691 /* Stop State Machine for next command cycle */
692 pxa3xx_nand_stop(info);
693 }
eric miaofe69af02008-02-14 15:48:23 +0800694 }
Lei Wend4568822011-07-14 20:44:32 -0700695 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800696}
697
Josh Wufdbad98d2012-06-25 18:07:45 +0800698static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700699 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800700{
701 chip->write_buf(mtd, buf, mtd->writesize);
702 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800703
704 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800705}
706
707static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700708 struct nand_chip *chip, uint8_t *buf, int oob_required,
709 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800710{
Lei Wend4568822011-07-14 20:44:32 -0700711 struct pxa3xx_nand_host *host = mtd->priv;
712 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800713
714 chip->read_buf(mtd, buf, mtd->writesize);
715 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
716
717 if (info->retcode == ERR_SBERR) {
718 switch (info->use_ecc) {
719 case 1:
720 mtd->ecc_stats.corrected++;
721 break;
722 case 0:
723 default:
724 break;
725 }
726 } else if (info->retcode == ERR_DBERR) {
727 /*
728 * for blank page (all 0xff), HW will calculate its ECC as
729 * 0, which is different from the ECC information within
730 * OOB, ignore such double bit errors
731 */
732 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700733 info->retcode = ERR_NONE;
734 else
Lei Wenf8155a42011-02-28 10:32:11 +0800735 mtd->ecc_stats.failed++;
736 }
737
738 return 0;
739}
740
eric miaofe69af02008-02-14 15:48:23 +0800741static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
742{
Lei Wend4568822011-07-14 20:44:32 -0700743 struct pxa3xx_nand_host *host = mtd->priv;
744 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800745 char retval = 0xFF;
746
747 if (info->buf_start < info->buf_count)
748 /* Has just send a new command? */
749 retval = info->data_buff[info->buf_start++];
750
751 return retval;
752}
753
754static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
755{
Lei Wend4568822011-07-14 20:44:32 -0700756 struct pxa3xx_nand_host *host = mtd->priv;
757 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800758 u16 retval = 0xFFFF;
759
760 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
761 retval = *((u16 *)(info->data_buff+info->buf_start));
762 info->buf_start += 2;
763 }
764 return retval;
765}
766
767static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
768{
Lei Wend4568822011-07-14 20:44:32 -0700769 struct pxa3xx_nand_host *host = mtd->priv;
770 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800771 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
772
773 memcpy(buf, info->data_buff + info->buf_start, real_len);
774 info->buf_start += real_len;
775}
776
777static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
778 const uint8_t *buf, int len)
779{
Lei Wend4568822011-07-14 20:44:32 -0700780 struct pxa3xx_nand_host *host = mtd->priv;
781 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800782 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
783
784 memcpy(info->data_buff + info->buf_start, buf, real_len);
785 info->buf_start += real_len;
786}
787
eric miaofe69af02008-02-14 15:48:23 +0800788static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
789{
790 return;
791}
792
793static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
794{
Lei Wend4568822011-07-14 20:44:32 -0700795 struct pxa3xx_nand_host *host = mtd->priv;
796 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800797
798 /* pxa3xx_nand_send_command has waited for command complete */
799 if (this->state == FL_WRITING || this->state == FL_ERASING) {
800 if (info->retcode == ERR_NONE)
801 return 0;
802 else {
803 /*
804 * any error make it return 0x01 which will tell
805 * the caller the erase and write fail
806 */
807 return 0x01;
808 }
809 }
810
811 return 0;
812}
813
eric miaofe69af02008-02-14 15:48:23 +0800814static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200815 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800816{
817 struct platform_device *pdev = info->pdev;
818 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700819 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800820 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800821
Lei Wenda675b42011-07-14 20:44:31 -0700822 if (f->page_size != 2048 && f->page_size != 512) {
823 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800824 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700825 }
eric miaofe69af02008-02-14 15:48:23 +0800826
Lei Wenda675b42011-07-14 20:44:31 -0700827 if (f->flash_width != 16 && f->flash_width != 8) {
828 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800829 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700830 }
eric miaofe69af02008-02-14 15:48:23 +0800831
832 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700833 host->cmdset = &default_cmdset;
834 host->page_size = f->page_size;
835 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800836
837 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700838 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800839
840 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700841 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800842 else
Lei Wend4568822011-07-14 20:44:32 -0700843 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800844
845 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700846 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800847 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
848 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
849 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
850 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
851
Lei Wend4568822011-07-14 20:44:32 -0700852 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800853 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
854
Lei Wend4568822011-07-14 20:44:32 -0700855 host->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800856
Lei Wend4568822011-07-14 20:44:32 -0700857 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800858 return 0;
859}
860
Mike Rapoportf2710492009-02-17 13:54:47 +0200861static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
862{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700863 /*
864 * We set 0 by hard coding here, for we don't support keep_config
865 * when there is more than one chip attached to the controller
866 */
867 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200868 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200869
Lei Wend4568822011-07-14 20:44:32 -0700870 if (ndcr & NDCR_PAGE_SZ) {
871 host->page_size = 2048;
872 host->read_id_bytes = 4;
873 } else {
874 host->page_size = 512;
875 host->read_id_bytes = 2;
876 }
877
878 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
879 host->cmdset = &default_cmdset;
880
881 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
882 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200883
884 return 0;
885}
886
eric miaofe69af02008-02-14 15:48:23 +0800887/* the maximum possible buffer size for large page with OOB data
888 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
889 * data buffer and the DMA descriptor
890 */
891#define MAX_BUFF_SIZE PAGE_SIZE
892
893static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
894{
895 struct platform_device *pdev = info->pdev;
896 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
897
898 if (use_dma == 0) {
899 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
900 if (info->data_buff == NULL)
901 return -ENOMEM;
902 return 0;
903 }
904
905 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
906 &info->data_buff_phys, GFP_KERNEL);
907 if (info->data_buff == NULL) {
908 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
909 return -ENOMEM;
910 }
911
eric miaofe69af02008-02-14 15:48:23 +0800912 info->data_desc = (void *)info->data_buff + data_desc_offset;
913 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
914
915 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
916 pxa3xx_nand_data_dma_irq, info);
917 if (info->data_dma_ch < 0) {
918 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700919 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800920 info->data_buff, info->data_buff_phys);
921 return info->data_dma_ch;
922 }
923
924 return 0;
925}
926
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300927static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
928{
929 struct platform_device *pdev = info->pdev;
930 if (use_dma) {
931 pxa_free_dma(info->data_dma_ch);
932 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
933 info->data_buff, info->data_buff_phys);
934 } else {
935 kfree(info->data_buff);
936 }
937}
938
Lei Wen401e67e2011-02-28 10:32:14 +0800939static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800940{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700941 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700942 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700943 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800944 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700945 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
946 if (ret)
947 return ret;
948
949 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800950 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800951 return 0;
Lei Wend4568822011-07-14 20:44:32 -0700952
953 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +0800954}
eric miaofe69af02008-02-14 15:48:23 +0800955
Lei Wen401e67e2011-02-28 10:32:14 +0800956static int pxa3xx_nand_scan(struct mtd_info *mtd)
957{
Lei Wend4568822011-07-14 20:44:32 -0700958 struct pxa3xx_nand_host *host = mtd->priv;
959 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +0800960 struct platform_device *pdev = info->pdev;
961 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wen0fab0282011-06-07 03:01:06 -0700962 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +0800963 const struct pxa3xx_nand_flash *f = NULL;
964 struct nand_chip *chip = mtd->priv;
965 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +0800966 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +0800967 int i, ret, num;
968
969 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +0800970 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +0800971
972 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -0700973 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700974 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
975 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +0800976
Lei Wend4568822011-07-14 20:44:32 -0700977 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +0800978 }
979
980 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
981 id = *((uint16_t *)(info->data_buff));
982 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -0700983 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +0800984 else {
Lei Wenda675b42011-07-14 20:44:31 -0700985 dev_warn(&info->pdev->dev,
986 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +0800987
988 return -EINVAL;
989 }
990
991 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
992 for (i = 0; i < num; i++) {
993 if (i < pdata->num_flash)
994 f = pdata->flash + i;
995 else
996 f = &builtin_flash_types[i - pdata->num_flash + 1];
997
998 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +0800999 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001000 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001001 }
1002
Lei Wen4332c112011-03-03 11:27:01 +08001003 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001004 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001005
1006 return -EINVAL;
1007 }
1008
Lei Wend4568822011-07-14 20:44:32 -07001009 ret = pxa3xx_nand_config_flash(info, f);
1010 if (ret) {
1011 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1012 return ret;
1013 }
1014
Lei Wen4332c112011-03-03 11:27:01 +08001015 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001016 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001017 pxa3xx_flash_ids[0].pagesize = f->page_size;
1018 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1019 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1020 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1021 if (f->flash_width == 16)
1022 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001023 pxa3xx_flash_ids[1].name = NULL;
1024 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001025KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001026 chip->ecc.mode = NAND_ECC_HW;
1027 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001028 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001029
Lei Wend4568822011-07-14 20:44:32 -07001030 if (host->reg_ndcr & NDCR_DWIDTH_M)
1031 chip->options |= NAND_BUSWIDTH_16;
1032
Lei Wen0fab0282011-06-07 03:01:06 -07001033 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001034 return -ENODEV;
1035 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001036 if (mtd->writesize >= 2048)
1037 host->col_addr_cycles = 2;
1038 else
1039 host->col_addr_cycles = 1;
1040
Lei Wen4332c112011-03-03 11:27:01 +08001041 info->oob_buff = info->data_buff + mtd->writesize;
1042 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001043 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001044 else
Lei Wend4568822011-07-14 20:44:32 -07001045 host->row_addr_cycles = 2;
1046
Lei Wen4332c112011-03-03 11:27:01 +08001047 mtd->name = mtd_names[0];
Lei Wen401e67e2011-02-28 10:32:14 +08001048 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001049}
1050
Lei Wend4568822011-07-14 20:44:32 -07001051static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001052{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001053 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001054 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001055 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001056 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001057 struct mtd_info *mtd;
1058 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001059 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001060
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001061 pdata = pdev->dev.platform_data;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001062 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1063 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1064 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001065 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001066
eric miaofe69af02008-02-14 15:48:23 +08001067 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001068 for (cs = 0; cs < pdata->num_cs; cs++) {
1069 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1070 (sizeof(*mtd) + sizeof(*host)) * cs);
1071 chip = (struct nand_chip *)(&mtd[1]);
1072 host = (struct pxa3xx_nand_host *)chip;
1073 info->host[cs] = host;
1074 host->mtd = mtd;
1075 host->cs = cs;
1076 host->info_data = info;
1077 mtd->priv = host;
1078 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001079
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001080 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1081 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1082 chip->controller = &info->controller;
1083 chip->waitfunc = pxa3xx_nand_waitfunc;
1084 chip->select_chip = pxa3xx_nand_select_chip;
1085 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1086 chip->read_word = pxa3xx_nand_read_word;
1087 chip->read_byte = pxa3xx_nand_read_byte;
1088 chip->read_buf = pxa3xx_nand_read_buf;
1089 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001090 }
Lei Wen401e67e2011-02-28 10:32:14 +08001091
1092 spin_lock_init(&chip->controller->lock);
1093 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001094 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001095 if (IS_ERR(info->clk)) {
1096 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001097 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001098 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001099 ret = clk_prepare_enable(info->clk);
1100 if (ret < 0)
1101 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001102
Daniel Mack1e7ba632012-07-22 19:51:02 +02001103 /*
1104 * This is a dirty hack to make this driver work from devicetree
1105 * bindings. It can be removed once we have a prober DMA controller
1106 * framework for DT.
1107 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001108 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001109 info->drcmr_dat = 97;
1110 info->drcmr_cmd = 99;
1111 } else {
1112 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1113 if (r == NULL) {
1114 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1115 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001116 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001117 }
1118 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001119
Daniel Mack1e7ba632012-07-22 19:51:02 +02001120 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1121 if (r == NULL) {
1122 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1123 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001124 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001125 }
1126 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001127 }
eric miaofe69af02008-02-14 15:48:23 +08001128
1129 irq = platform_get_irq(pdev, 0);
1130 if (irq < 0) {
1131 dev_err(&pdev->dev, "no IRQ resource defined\n");
1132 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001133 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001134 }
1135
1136 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001137 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1138 if (IS_ERR(info->mmio_base)) {
1139 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001140 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001141 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001142 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001143
1144 ret = pxa3xx_nand_init_buff(info);
1145 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001146 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001147
Haojian Zhuang346e1252009-09-10 14:27:23 +08001148 /* initialize all interrupts to be disabled */
1149 disable_int(info, NDSR_MASK);
1150
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001151 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1152 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001153 if (ret < 0) {
1154 dev_err(&pdev->dev, "failed to request IRQ\n");
1155 goto fail_free_buf;
1156 }
1157
Lei Wene353a202011-03-03 11:08:30 +08001158 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001159
Lei Wend4568822011-07-14 20:44:32 -07001160 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001161
eric miaofe69af02008-02-14 15:48:23 +08001162fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001163 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001164 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001165fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001166 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001167 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001168}
1169
1170static int pxa3xx_nand_remove(struct platform_device *pdev)
1171{
Lei Wene353a202011-03-03 11:08:30 +08001172 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001173 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001174 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001175
Lei Wend4568822011-07-14 20:44:32 -07001176 if (!info)
1177 return 0;
1178
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001179 pdata = pdev->dev.platform_data;
eric miaofe69af02008-02-14 15:48:23 +08001180 platform_set_drvdata(pdev, NULL);
1181
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001182 irq = platform_get_irq(pdev, 0);
1183 if (irq >= 0)
1184 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001185 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001186
Ezequiel Garciafb320612013-04-17 13:38:12 -03001187 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001188
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001189 for (cs = 0; cs < pdata->num_cs; cs++)
1190 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001191 return 0;
1192}
1193
Daniel Mack1e7ba632012-07-22 19:51:02 +02001194#ifdef CONFIG_OF
1195static struct of_device_id pxa3xx_nand_dt_ids[] = {
1196 { .compatible = "marvell,pxa3xx-nand" },
1197 {}
1198};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001199MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001200
1201static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1202{
1203 struct pxa3xx_nand_platform_data *pdata;
1204 struct device_node *np = pdev->dev.of_node;
1205 const struct of_device_id *of_id =
1206 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1207
1208 if (!of_id)
1209 return 0;
1210
1211 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1212 if (!pdata)
1213 return -ENOMEM;
1214
1215 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1216 pdata->enable_arbiter = 1;
1217 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1218 pdata->keep_config = 1;
1219 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1220
1221 pdev->dev.platform_data = pdata;
1222
1223 return 0;
1224}
1225#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001226static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001227{
1228 return 0;
1229}
1230#endif
1231
Lei Wene353a202011-03-03 11:08:30 +08001232static int pxa3xx_nand_probe(struct platform_device *pdev)
1233{
1234 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001235 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001236 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001237 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001238
Daniel Mack1e7ba632012-07-22 19:51:02 +02001239 ret = pxa3xx_nand_probe_dt(pdev);
1240 if (ret)
1241 return ret;
1242
Lei Wene353a202011-03-03 11:08:30 +08001243 pdata = pdev->dev.platform_data;
1244 if (!pdata) {
1245 dev_err(&pdev->dev, "no platform data defined\n");
1246 return -ENODEV;
1247 }
1248
Lei Wend4568822011-07-14 20:44:32 -07001249 ret = alloc_nand_resource(pdev);
1250 if (ret) {
1251 dev_err(&pdev->dev, "alloc nand resource failed\n");
1252 return ret;
1253 }
Lei Wene353a202011-03-03 11:08:30 +08001254
Lei Wend4568822011-07-14 20:44:32 -07001255 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001256 probe_success = 0;
1257 for (cs = 0; cs < pdata->num_cs; cs++) {
1258 info->cs = cs;
1259 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1260 if (ret) {
1261 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1262 cs);
1263 continue;
1264 }
1265
Daniel Mack1e7ba632012-07-22 19:51:02 +02001266 ppdata.of_node = pdev->dev.of_node;
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001267 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001268 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001269 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001270 if (!ret)
1271 probe_success = 1;
1272 }
1273
1274 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001275 pxa3xx_nand_remove(pdev);
1276 return -ENODEV;
1277 }
1278
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001279 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001280}
1281
eric miaofe69af02008-02-14 15:48:23 +08001282#ifdef CONFIG_PM
1283static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1284{
Lei Wene353a202011-03-03 11:08:30 +08001285 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001286 struct pxa3xx_nand_platform_data *pdata;
1287 struct mtd_info *mtd;
1288 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001289
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001290 pdata = pdev->dev.platform_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001291 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001292 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1293 return -EAGAIN;
1294 }
1295
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001296 for (cs = 0; cs < pdata->num_cs; cs++) {
1297 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001298 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001299 }
1300
eric miaofe69af02008-02-14 15:48:23 +08001301 return 0;
1302}
1303
1304static int pxa3xx_nand_resume(struct platform_device *pdev)
1305{
Lei Wene353a202011-03-03 11:08:30 +08001306 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001307 struct pxa3xx_nand_platform_data *pdata;
1308 struct mtd_info *mtd;
1309 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001310
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001311 pdata = pdev->dev.platform_data;
Lei Wen051fc412011-07-14 20:44:30 -07001312 /* We don't want to handle interrupt without calling mtd routine */
1313 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001314
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001315 /*
1316 * Directly set the chip select to a invalid value,
1317 * then the driver would reset the timing according
1318 * to current chip select at the beginning of cmdfunc
1319 */
1320 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001321
Lei Wen051fc412011-07-14 20:44:30 -07001322 /*
1323 * As the spec says, the NDSR would be updated to 0x1800 when
1324 * doing the nand_clk disable/enable.
1325 * To prevent it damaging state machine of the driver, clear
1326 * all status before resume
1327 */
1328 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001329 for (cs = 0; cs < pdata->num_cs; cs++) {
1330 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001331 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001332 }
1333
Lei Wen18c81b12010-08-17 17:25:57 +08001334 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001335}
1336#else
1337#define pxa3xx_nand_suspend NULL
1338#define pxa3xx_nand_resume NULL
1339#endif
1340
1341static struct platform_driver pxa3xx_nand_driver = {
1342 .driver = {
1343 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001344 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001345 },
1346 .probe = pxa3xx_nand_probe,
1347 .remove = pxa3xx_nand_remove,
1348 .suspend = pxa3xx_nand_suspend,
1349 .resume = pxa3xx_nand_resume,
1350};
1351
Axel Linf99640d2011-11-27 20:45:03 +08001352module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001353
1354MODULE_LICENSE("GPL");
1355MODULE_DESCRIPTION("PXA3xx NAND controller driver");