blob: 8386e9a5ec88073ee8a1fe01938a0d56bc48d088 [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
593 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700594 cmd = host->cmdset->read_id;
595 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800596 info->ndcb0 |= NDCB0_CMD_TYPE(3)
597 | NDCB0_ADDR_CYC(1)
598 | cmd;
599
600 info->data_size = 8;
601 break;
602 case NAND_CMD_STATUS:
Lei Wend4568822011-07-14 20:44:32 -0700603 cmd = host->cmdset->read_status;
Lei Wen4eb2da82011-02-28 10:32:13 +0800604 info->buf_count = 1;
605 info->ndcb0 |= NDCB0_CMD_TYPE(4)
606 | NDCB0_ADDR_CYC(1)
607 | cmd;
608
609 info->data_size = 8;
610 break;
611
612 case NAND_CMD_ERASE1:
Lei Wend4568822011-07-14 20:44:32 -0700613 cmd = host->cmdset->erase;
Lei Wen4eb2da82011-02-28 10:32:13 +0800614 info->ndcb0 |= NDCB0_CMD_TYPE(2)
615 | NDCB0_AUTO_RS
616 | NDCB0_ADDR_CYC(3)
617 | NDCB0_DBC
618 | cmd;
619 info->ndcb1 = page_addr;
620 info->ndcb2 = 0;
621
622 break;
623 case NAND_CMD_RESET:
Lei Wend4568822011-07-14 20:44:32 -0700624 cmd = host->cmdset->reset;
Lei Wen4eb2da82011-02-28 10:32:13 +0800625 info->ndcb0 |= NDCB0_CMD_TYPE(5)
626 | cmd;
627
628 break;
629
630 case NAND_CMD_ERASE2:
631 exec_cmd = 0;
632 break;
633
634 default:
635 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700636 dev_err(&info->pdev->dev, "non-supported command %x\n",
637 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800638 break;
639 }
640
641 return exec_cmd;
642}
643
eric miaofe69af02008-02-14 15:48:23 +0800644static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100645 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800646{
Lei Wend4568822011-07-14 20:44:32 -0700647 struct pxa3xx_nand_host *host = mtd->priv;
648 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800649 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800650
Lei Wen4eb2da82011-02-28 10:32:13 +0800651 /*
652 * if this is a x16 device ,then convert the input
653 * "byte" address into a "word" address appropriate
654 * for indexing a word-oriented device
655 */
Lei Wend4568822011-07-14 20:44:32 -0700656 if (host->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800657 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800658
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700659 /*
660 * There may be different NAND chip hooked to
661 * different chip select, so check whether
662 * chip select has been changed, if yes, reset the timing
663 */
664 if (info->cs != host->cs) {
665 info->cs = host->cs;
666 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
667 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
668 }
669
Lei Wend4568822011-07-14 20:44:32 -0700670 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800671 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800672 if (exec_cmd) {
673 init_completion(&info->cmd_complete);
674 pxa3xx_nand_start(info);
675
676 ret = wait_for_completion_timeout(&info->cmd_complete,
677 CHIP_DELAY_TIMEOUT);
678 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700679 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800680 /* Stop State Machine for next command cycle */
681 pxa3xx_nand_stop(info);
682 }
eric miaofe69af02008-02-14 15:48:23 +0800683 }
Lei Wend4568822011-07-14 20:44:32 -0700684 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800685}
686
Josh Wufdbad98d2012-06-25 18:07:45 +0800687static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700688 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800689{
690 chip->write_buf(mtd, buf, mtd->writesize);
691 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800692
693 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800694}
695
696static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700697 struct nand_chip *chip, uint8_t *buf, int oob_required,
698 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800699{
Lei Wend4568822011-07-14 20:44:32 -0700700 struct pxa3xx_nand_host *host = mtd->priv;
701 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800702
703 chip->read_buf(mtd, buf, mtd->writesize);
704 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
705
706 if (info->retcode == ERR_SBERR) {
707 switch (info->use_ecc) {
708 case 1:
709 mtd->ecc_stats.corrected++;
710 break;
711 case 0:
712 default:
713 break;
714 }
715 } else if (info->retcode == ERR_DBERR) {
716 /*
717 * for blank page (all 0xff), HW will calculate its ECC as
718 * 0, which is different from the ECC information within
719 * OOB, ignore such double bit errors
720 */
721 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700722 info->retcode = ERR_NONE;
723 else
Lei Wenf8155a42011-02-28 10:32:11 +0800724 mtd->ecc_stats.failed++;
725 }
726
727 return 0;
728}
729
eric miaofe69af02008-02-14 15:48:23 +0800730static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
731{
Lei Wend4568822011-07-14 20:44:32 -0700732 struct pxa3xx_nand_host *host = mtd->priv;
733 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800734 char retval = 0xFF;
735
736 if (info->buf_start < info->buf_count)
737 /* Has just send a new command? */
738 retval = info->data_buff[info->buf_start++];
739
740 return retval;
741}
742
743static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
744{
Lei Wend4568822011-07-14 20:44:32 -0700745 struct pxa3xx_nand_host *host = mtd->priv;
746 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800747 u16 retval = 0xFFFF;
748
749 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
750 retval = *((u16 *)(info->data_buff+info->buf_start));
751 info->buf_start += 2;
752 }
753 return retval;
754}
755
756static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
757{
Lei Wend4568822011-07-14 20:44:32 -0700758 struct pxa3xx_nand_host *host = mtd->priv;
759 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800760 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
761
762 memcpy(buf, info->data_buff + info->buf_start, real_len);
763 info->buf_start += real_len;
764}
765
766static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
767 const 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(info->data_buff + info->buf_start, buf, real_len);
774 info->buf_start += real_len;
775}
776
eric miaofe69af02008-02-14 15:48:23 +0800777static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
778{
779 return;
780}
781
782static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
783{
Lei Wend4568822011-07-14 20:44:32 -0700784 struct pxa3xx_nand_host *host = mtd->priv;
785 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800786
787 /* pxa3xx_nand_send_command has waited for command complete */
788 if (this->state == FL_WRITING || this->state == FL_ERASING) {
789 if (info->retcode == ERR_NONE)
790 return 0;
791 else {
792 /*
793 * any error make it return 0x01 which will tell
794 * the caller the erase and write fail
795 */
796 return 0x01;
797 }
798 }
799
800 return 0;
801}
802
eric miaofe69af02008-02-14 15:48:23 +0800803static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200804 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800805{
806 struct platform_device *pdev = info->pdev;
807 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700808 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800809 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800810
Lei Wenda675b42011-07-14 20:44:31 -0700811 if (f->page_size != 2048 && f->page_size != 512) {
812 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800813 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700814 }
eric miaofe69af02008-02-14 15:48:23 +0800815
Lei Wenda675b42011-07-14 20:44:31 -0700816 if (f->flash_width != 16 && f->flash_width != 8) {
817 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800818 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700819 }
eric miaofe69af02008-02-14 15:48:23 +0800820
821 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700822 host->cmdset = &default_cmdset;
823 host->page_size = f->page_size;
824 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800825
826 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700827 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800828
829 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700830 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800831 else
Lei Wend4568822011-07-14 20:44:32 -0700832 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800833
834 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700835 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800836 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
837 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
838 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
839 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
840
Lei Wend4568822011-07-14 20:44:32 -0700841 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800842 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
843
Lei Wend4568822011-07-14 20:44:32 -0700844 host->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800845
Lei Wend4568822011-07-14 20:44:32 -0700846 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800847 return 0;
848}
849
Mike Rapoportf2710492009-02-17 13:54:47 +0200850static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
851{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700852 /*
853 * We set 0 by hard coding here, for we don't support keep_config
854 * when there is more than one chip attached to the controller
855 */
856 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200857 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200858
Lei Wend4568822011-07-14 20:44:32 -0700859 if (ndcr & NDCR_PAGE_SZ) {
860 host->page_size = 2048;
861 host->read_id_bytes = 4;
862 } else {
863 host->page_size = 512;
864 host->read_id_bytes = 2;
865 }
866
867 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
868 host->cmdset = &default_cmdset;
869
870 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
871 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200872
873 return 0;
874}
875
eric miaofe69af02008-02-14 15:48:23 +0800876/* the maximum possible buffer size for large page with OOB data
877 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
878 * data buffer and the DMA descriptor
879 */
880#define MAX_BUFF_SIZE PAGE_SIZE
881
882static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
883{
884 struct platform_device *pdev = info->pdev;
885 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
886
887 if (use_dma == 0) {
888 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
889 if (info->data_buff == NULL)
890 return -ENOMEM;
891 return 0;
892 }
893
894 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
895 &info->data_buff_phys, GFP_KERNEL);
896 if (info->data_buff == NULL) {
897 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
898 return -ENOMEM;
899 }
900
eric miaofe69af02008-02-14 15:48:23 +0800901 info->data_desc = (void *)info->data_buff + data_desc_offset;
902 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
903
904 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
905 pxa3xx_nand_data_dma_irq, info);
906 if (info->data_dma_ch < 0) {
907 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700908 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800909 info->data_buff, info->data_buff_phys);
910 return info->data_dma_ch;
911 }
912
913 return 0;
914}
915
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300916static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
917{
918 struct platform_device *pdev = info->pdev;
919 if (use_dma) {
920 pxa_free_dma(info->data_dma_ch);
921 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
922 info->data_buff, info->data_buff_phys);
923 } else {
924 kfree(info->data_buff);
925 }
926}
927
Lei Wen401e67e2011-02-28 10:32:14 +0800928static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800929{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700930 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700931 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700932 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800933 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700934 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
935 if (ret)
936 return ret;
937
938 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800939 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800940 return 0;
Lei Wend4568822011-07-14 20:44:32 -0700941
942 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +0800943}
eric miaofe69af02008-02-14 15:48:23 +0800944
Lei Wen401e67e2011-02-28 10:32:14 +0800945static int pxa3xx_nand_scan(struct mtd_info *mtd)
946{
Lei Wend4568822011-07-14 20:44:32 -0700947 struct pxa3xx_nand_host *host = mtd->priv;
948 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +0800949 struct platform_device *pdev = info->pdev;
950 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wen0fab0282011-06-07 03:01:06 -0700951 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +0800952 const struct pxa3xx_nand_flash *f = NULL;
953 struct nand_chip *chip = mtd->priv;
954 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +0800955 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +0800956 int i, ret, num;
957
958 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +0800959 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +0800960
961 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -0700962 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700963 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
964 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +0800965
Lei Wend4568822011-07-14 20:44:32 -0700966 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +0800967 }
968
969 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
970 id = *((uint16_t *)(info->data_buff));
971 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -0700972 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +0800973 else {
Lei Wenda675b42011-07-14 20:44:31 -0700974 dev_warn(&info->pdev->dev,
975 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +0800976
977 return -EINVAL;
978 }
979
980 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
981 for (i = 0; i < num; i++) {
982 if (i < pdata->num_flash)
983 f = pdata->flash + i;
984 else
985 f = &builtin_flash_types[i - pdata->num_flash + 1];
986
987 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +0800988 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +0800989 break;
Lei Wen401e67e2011-02-28 10:32:14 +0800990 }
991
Lei Wen4332c112011-03-03 11:27:01 +0800992 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -0700993 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +0800994
995 return -EINVAL;
996 }
997
Lei Wend4568822011-07-14 20:44:32 -0700998 ret = pxa3xx_nand_config_flash(info, f);
999 if (ret) {
1000 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1001 return ret;
1002 }
1003
Lei Wen4332c112011-03-03 11:27:01 +08001004 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001005 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001006 pxa3xx_flash_ids[0].pagesize = f->page_size;
1007 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1008 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1009 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1010 if (f->flash_width == 16)
1011 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001012 pxa3xx_flash_ids[1].name = NULL;
1013 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001014KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001015 chip->ecc.mode = NAND_ECC_HW;
1016 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001017 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001018
Lei Wend4568822011-07-14 20:44:32 -07001019 if (host->reg_ndcr & NDCR_DWIDTH_M)
1020 chip->options |= NAND_BUSWIDTH_16;
1021
Lei Wen0fab0282011-06-07 03:01:06 -07001022 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001023 return -ENODEV;
1024 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001025 if (mtd->writesize >= 2048)
1026 host->col_addr_cycles = 2;
1027 else
1028 host->col_addr_cycles = 1;
1029
Lei Wen4332c112011-03-03 11:27:01 +08001030 info->oob_buff = info->data_buff + mtd->writesize;
1031 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001032 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001033 else
Lei Wend4568822011-07-14 20:44:32 -07001034 host->row_addr_cycles = 2;
1035
Lei Wen4332c112011-03-03 11:27:01 +08001036 mtd->name = mtd_names[0];
Lei Wen401e67e2011-02-28 10:32:14 +08001037 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001038}
1039
Lei Wend4568822011-07-14 20:44:32 -07001040static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001041{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001042 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001043 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001044 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001045 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001046 struct mtd_info *mtd;
1047 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001048 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001049
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001050 pdata = pdev->dev.platform_data;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001051 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1052 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1053 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001054 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001055
eric miaofe69af02008-02-14 15:48:23 +08001056 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001057 for (cs = 0; cs < pdata->num_cs; cs++) {
1058 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1059 (sizeof(*mtd) + sizeof(*host)) * cs);
1060 chip = (struct nand_chip *)(&mtd[1]);
1061 host = (struct pxa3xx_nand_host *)chip;
1062 info->host[cs] = host;
1063 host->mtd = mtd;
1064 host->cs = cs;
1065 host->info_data = info;
1066 mtd->priv = host;
1067 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001068
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001069 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1070 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1071 chip->controller = &info->controller;
1072 chip->waitfunc = pxa3xx_nand_waitfunc;
1073 chip->select_chip = pxa3xx_nand_select_chip;
1074 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1075 chip->read_word = pxa3xx_nand_read_word;
1076 chip->read_byte = pxa3xx_nand_read_byte;
1077 chip->read_buf = pxa3xx_nand_read_buf;
1078 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001079 }
Lei Wen401e67e2011-02-28 10:32:14 +08001080
1081 spin_lock_init(&chip->controller->lock);
1082 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001083 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001084 if (IS_ERR(info->clk)) {
1085 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001086 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001087 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001088 ret = clk_prepare_enable(info->clk);
1089 if (ret < 0)
1090 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001091
Daniel Mack1e7ba632012-07-22 19:51:02 +02001092 /*
1093 * This is a dirty hack to make this driver work from devicetree
1094 * bindings. It can be removed once we have a prober DMA controller
1095 * framework for DT.
1096 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001097 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001098 info->drcmr_dat = 97;
1099 info->drcmr_cmd = 99;
1100 } else {
1101 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1102 if (r == NULL) {
1103 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1104 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001105 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001106 }
1107 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001108
Daniel Mack1e7ba632012-07-22 19:51:02 +02001109 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1110 if (r == NULL) {
1111 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1112 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001113 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001114 }
1115 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001116 }
eric miaofe69af02008-02-14 15:48:23 +08001117
1118 irq = platform_get_irq(pdev, 0);
1119 if (irq < 0) {
1120 dev_err(&pdev->dev, "no IRQ resource defined\n");
1121 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001122 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001123 }
1124
1125 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001126 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1127 if (IS_ERR(info->mmio_base)) {
1128 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001129 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001130 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001131 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001132
1133 ret = pxa3xx_nand_init_buff(info);
1134 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001135 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001136
Haojian Zhuang346e1252009-09-10 14:27:23 +08001137 /* initialize all interrupts to be disabled */
1138 disable_int(info, NDSR_MASK);
1139
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001140 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1141 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001142 if (ret < 0) {
1143 dev_err(&pdev->dev, "failed to request IRQ\n");
1144 goto fail_free_buf;
1145 }
1146
Lei Wene353a202011-03-03 11:08:30 +08001147 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001148
Lei Wend4568822011-07-14 20:44:32 -07001149 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001150
eric miaofe69af02008-02-14 15:48:23 +08001151fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001152 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001153 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001154fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001155 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001156 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001157}
1158
1159static int pxa3xx_nand_remove(struct platform_device *pdev)
1160{
Lei Wene353a202011-03-03 11:08:30 +08001161 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001162 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001163 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001164
Lei Wend4568822011-07-14 20:44:32 -07001165 if (!info)
1166 return 0;
1167
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001168 pdata = pdev->dev.platform_data;
eric miaofe69af02008-02-14 15:48:23 +08001169 platform_set_drvdata(pdev, NULL);
1170
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001171 irq = platform_get_irq(pdev, 0);
1172 if (irq >= 0)
1173 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001174 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001175
Ezequiel Garciafb320612013-04-17 13:38:12 -03001176 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001177
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001178 for (cs = 0; cs < pdata->num_cs; cs++)
1179 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001180 return 0;
1181}
1182
Daniel Mack1e7ba632012-07-22 19:51:02 +02001183#ifdef CONFIG_OF
1184static struct of_device_id pxa3xx_nand_dt_ids[] = {
1185 { .compatible = "marvell,pxa3xx-nand" },
1186 {}
1187};
1188MODULE_DEVICE_TABLE(of, i2c_pxa_dt_ids);
1189
1190static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1191{
1192 struct pxa3xx_nand_platform_data *pdata;
1193 struct device_node *np = pdev->dev.of_node;
1194 const struct of_device_id *of_id =
1195 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1196
1197 if (!of_id)
1198 return 0;
1199
1200 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1201 if (!pdata)
1202 return -ENOMEM;
1203
1204 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1205 pdata->enable_arbiter = 1;
1206 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1207 pdata->keep_config = 1;
1208 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1209
1210 pdev->dev.platform_data = pdata;
1211
1212 return 0;
1213}
1214#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001215static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001216{
1217 return 0;
1218}
1219#endif
1220
Lei Wene353a202011-03-03 11:08:30 +08001221static int pxa3xx_nand_probe(struct platform_device *pdev)
1222{
1223 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001224 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001225 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001226 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001227
Daniel Mack1e7ba632012-07-22 19:51:02 +02001228 ret = pxa3xx_nand_probe_dt(pdev);
1229 if (ret)
1230 return ret;
1231
Lei Wene353a202011-03-03 11:08:30 +08001232 pdata = pdev->dev.platform_data;
1233 if (!pdata) {
1234 dev_err(&pdev->dev, "no platform data defined\n");
1235 return -ENODEV;
1236 }
1237
Lei Wend4568822011-07-14 20:44:32 -07001238 ret = alloc_nand_resource(pdev);
1239 if (ret) {
1240 dev_err(&pdev->dev, "alloc nand resource failed\n");
1241 return ret;
1242 }
Lei Wene353a202011-03-03 11:08:30 +08001243
Lei Wend4568822011-07-14 20:44:32 -07001244 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001245 probe_success = 0;
1246 for (cs = 0; cs < pdata->num_cs; cs++) {
1247 info->cs = cs;
1248 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1249 if (ret) {
1250 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1251 cs);
1252 continue;
1253 }
1254
Daniel Mack1e7ba632012-07-22 19:51:02 +02001255 ppdata.of_node = pdev->dev.of_node;
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001256 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001257 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001258 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001259 if (!ret)
1260 probe_success = 1;
1261 }
1262
1263 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001264 pxa3xx_nand_remove(pdev);
1265 return -ENODEV;
1266 }
1267
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001268 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001269}
1270
eric miaofe69af02008-02-14 15:48:23 +08001271#ifdef CONFIG_PM
1272static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1273{
Lei Wene353a202011-03-03 11:08:30 +08001274 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001275 struct pxa3xx_nand_platform_data *pdata;
1276 struct mtd_info *mtd;
1277 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001278
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001279 pdata = pdev->dev.platform_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001280 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001281 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1282 return -EAGAIN;
1283 }
1284
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001285 for (cs = 0; cs < pdata->num_cs; cs++) {
1286 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001287 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001288 }
1289
eric miaofe69af02008-02-14 15:48:23 +08001290 return 0;
1291}
1292
1293static int pxa3xx_nand_resume(struct platform_device *pdev)
1294{
Lei Wene353a202011-03-03 11:08:30 +08001295 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001296 struct pxa3xx_nand_platform_data *pdata;
1297 struct mtd_info *mtd;
1298 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001299
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001300 pdata = pdev->dev.platform_data;
Lei Wen051fc412011-07-14 20:44:30 -07001301 /* We don't want to handle interrupt without calling mtd routine */
1302 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001303
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001304 /*
1305 * Directly set the chip select to a invalid value,
1306 * then the driver would reset the timing according
1307 * to current chip select at the beginning of cmdfunc
1308 */
1309 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001310
Lei Wen051fc412011-07-14 20:44:30 -07001311 /*
1312 * As the spec says, the NDSR would be updated to 0x1800 when
1313 * doing the nand_clk disable/enable.
1314 * To prevent it damaging state machine of the driver, clear
1315 * all status before resume
1316 */
1317 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001318 for (cs = 0; cs < pdata->num_cs; cs++) {
1319 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001320 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001321 }
1322
Lei Wen18c81b12010-08-17 17:25:57 +08001323 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001324}
1325#else
1326#define pxa3xx_nand_suspend NULL
1327#define pxa3xx_nand_resume NULL
1328#endif
1329
1330static struct platform_driver pxa3xx_nand_driver = {
1331 .driver = {
1332 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001333 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001334 },
1335 .probe = pxa3xx_nand_probe,
1336 .remove = pxa3xx_nand_remove,
1337 .suspend = pxa3xx_nand_suspend,
1338 .resume = pxa3xx_nand_resume,
1339};
1340
Axel Linf99640d2011-11-27 20:45:03 +08001341module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001342
1343MODULE_LICENSE("GPL");
1344MODULE_DESCRIPTION("PXA3xx NAND controller driver");