blob: bb50cf2b11c4d821224588d1e9b716d5c0aadf69 [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>
eric miaofe69af02008-02-14 15:48:23 +080025
Eric Miaoafb5b5c2008-12-01 11:43:08 +080026#include <mach/dma.h>
Haojian Zhuang82b95ec2009-09-10 13:55:23 +080027#include <plat/pxa3xx_nand.h>
eric miaofe69af02008-02-14 15:48:23 +080028
29#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080030#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080031#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080032
33/* registers and bit definitions */
34#define NDCR (0x00) /* Control register */
35#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
36#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
37#define NDSR (0x14) /* Status Register */
38#define NDPCR (0x18) /* Page Count Register */
39#define NDBDR0 (0x1C) /* Bad Block Register 0 */
40#define NDBDR1 (0x20) /* Bad Block Register 1 */
41#define NDDB (0x40) /* Data Buffer */
42#define NDCB0 (0x48) /* Command Buffer0 */
43#define NDCB1 (0x4C) /* Command Buffer1 */
44#define NDCB2 (0x50) /* Command Buffer2 */
45
46#define NDCR_SPARE_EN (0x1 << 31)
47#define NDCR_ECC_EN (0x1 << 30)
48#define NDCR_DMA_EN (0x1 << 29)
49#define NDCR_ND_RUN (0x1 << 28)
50#define NDCR_DWIDTH_C (0x1 << 27)
51#define NDCR_DWIDTH_M (0x1 << 26)
52#define NDCR_PAGE_SZ (0x1 << 24)
53#define NDCR_NCSX (0x1 << 23)
54#define NDCR_ND_MODE (0x3 << 21)
55#define NDCR_NAND_MODE (0x0)
56#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080057#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080058#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
59#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
60
61#define NDCR_RA_START (0x1 << 15)
62#define NDCR_PG_PER_BLK (0x1 << 14)
63#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080064#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080065
66#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080067#define NDSR_RDY (0x1 << 12)
68#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080069#define NDSR_CS0_PAGED (0x1 << 10)
70#define NDSR_CS1_PAGED (0x1 << 9)
71#define NDSR_CS0_CMDD (0x1 << 8)
72#define NDSR_CS1_CMDD (0x1 << 7)
73#define NDSR_CS0_BBD (0x1 << 6)
74#define NDSR_CS1_BBD (0x1 << 5)
75#define NDSR_DBERR (0x1 << 4)
76#define NDSR_SBERR (0x1 << 3)
77#define NDSR_WRDREQ (0x1 << 2)
78#define NDSR_RDDREQ (0x1 << 1)
79#define NDSR_WRCMDREQ (0x1)
80
Lei Wen4eb2da82011-02-28 10:32:13 +080081#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080082#define NDCB0_AUTO_RS (0x1 << 25)
83#define NDCB0_CSEL (0x1 << 24)
84#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
85#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
86#define NDCB0_NC (0x1 << 20)
87#define NDCB0_DBC (0x1 << 19)
88#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
89#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
90#define NDCB0_CMD2_MASK (0xff << 8)
91#define NDCB0_CMD1_MASK (0xff)
92#define NDCB0_ADDR_CYC_SHIFT (16)
93
eric miaofe69af02008-02-14 15:48:23 +080094/* macros for registers read/write */
95#define nand_writel(info, off, val) \
96 __raw_writel((val), (info)->mmio_base + (off))
97
98#define nand_readl(info, off) \
99 __raw_readl((info)->mmio_base + (off))
100
101/* error code and state */
102enum {
103 ERR_NONE = 0,
104 ERR_DMABUSERR = -1,
105 ERR_SENDCMD = -2,
106 ERR_DBERR = -3,
107 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300108 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800109};
110
111enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800112 STATE_IDLE = 0,
eric miaofe69af02008-02-14 15:48:23 +0800113 STATE_CMD_HANDLE,
114 STATE_DMA_READING,
115 STATE_DMA_WRITING,
116 STATE_DMA_DONE,
117 STATE_PIO_READING,
118 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800119 STATE_CMD_DONE,
120 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800121};
122
eric miaofe69af02008-02-14 15:48:23 +0800123struct pxa3xx_nand_info {
124 struct nand_chip nand_chip;
125
Lei Wen401e67e2011-02-28 10:32:14 +0800126 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800127 struct platform_device *pdev;
Lei Wen18c81b12010-08-17 17:25:57 +0800128 struct pxa3xx_nand_cmdset *cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800129
130 struct clk *clk;
131 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800132 unsigned long mmio_phys;
eric miaofe69af02008-02-14 15:48:23 +0800133
134 unsigned int buf_start;
135 unsigned int buf_count;
136
Lei Wene353a202011-03-03 11:08:30 +0800137 struct mtd_info *mtd;
eric miaofe69af02008-02-14 15:48:23 +0800138 /* DMA information */
139 int drcmr_dat;
140 int drcmr_cmd;
141
142 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800143 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800144 dma_addr_t data_buff_phys;
145 size_t data_buff_size;
146 int data_dma_ch;
147 struct pxa_dma_desc *data_desc;
148 dma_addr_t data_desc_addr;
149
150 uint32_t reg_ndcr;
151
152 /* saved column/page_addr during CMD_SEQIN */
153 int seqin_column;
154 int seqin_page_addr;
155
156 /* relate to the command */
157 unsigned int state;
158
159 int use_ecc; /* use HW ECC ? */
160 int use_dma; /* use DMA ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800161 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800162
Lei Wen18c81b12010-08-17 17:25:57 +0800163 unsigned int page_size; /* page size of attached chip */
164 unsigned int data_size; /* data size in FIFO */
eric miaofe69af02008-02-14 15:48:23 +0800165 int retcode;
166 struct completion cmd_complete;
167
168 /* generated NDCBx register values */
169 uint32_t ndcb0;
170 uint32_t ndcb1;
171 uint32_t ndcb2;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200172
Lei Wen18c81b12010-08-17 17:25:57 +0800173 /* timing calcuted from setting */
174 uint32_t ndtr0cs0;
175 uint32_t ndtr1cs0;
176
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200177 /* calculated from pxa3xx_nand_flash data */
178 size_t oob_size;
179 size_t read_id_bytes;
180
181 unsigned int col_addr_cycles;
182 unsigned int row_addr_cycles;
eric miaofe69af02008-02-14 15:48:23 +0800183};
184
185static int use_dma = 1;
186module_param(use_dma, bool, 0444);
187MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
188
Mike Rapoportf2710492009-02-17 13:54:47 +0200189/*
190 * Default NAND flash controller configuration setup by the
191 * bootloader. This configuration is used only when pdata->keep_config is set
192 */
Lei Wenc1f82472010-08-17 13:50:23 +0800193static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800194 .read1 = 0x3000,
195 .read2 = 0x0050,
196 .program = 0x1080,
197 .read_status = 0x0070,
198 .read_id = 0x0090,
199 .erase = 0xD060,
200 .reset = 0x00FF,
201 .lock = 0x002A,
202 .unlock = 0x2423,
203 .lock_status = 0x007A,
204};
205
Lei Wenc1f82472010-08-17 13:50:23 +0800206static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800207 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
208 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
209 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
210 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800211};
212
Lei Wenc1f82472010-08-17 13:50:23 +0800213static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800214 { 0, 0, 2048, 8, 8, 0, &default_cmdset, &timing[0] },
215 { 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset, &timing[1] },
216 { 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset, &timing[1] },
217 { 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset, &timing[1] },
218 { 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset, &timing[2] },
219 { 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset, &timing[2] },
220 { 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset, &timing[2] },
221 { 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset, &timing[2] },
222 { 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800223};
224
Lei Wen227a8862010-08-18 18:00:03 +0800225/* Define a default flash type setting serve as flash detecting only */
226#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
227
Lei Wen401e67e2011-02-28 10:32:14 +0800228const char *mtd_names[] = {"pxa3xx_nand-0", NULL};
229
eric miaofe69af02008-02-14 15:48:23 +0800230#define NDTR0_tCH(c) (min((c), 7) << 19)
231#define NDTR0_tCS(c) (min((c), 7) << 16)
232#define NDTR0_tWH(c) (min((c), 7) << 11)
233#define NDTR0_tWP(c) (min((c), 7) << 8)
234#define NDTR0_tRH(c) (min((c), 7) << 3)
235#define NDTR0_tRP(c) (min((c), 7) << 0)
236
237#define NDTR1_tR(c) (min((c), 65535) << 16)
238#define NDTR1_tWHR(c) (min((c), 15) << 4)
239#define NDTR1_tAR(c) (min((c), 15) << 0)
240
241/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800242#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800243
244static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200245 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800246{
247 unsigned long nand_clk = clk_get_rate(info->clk);
248 uint32_t ndtr0, ndtr1;
249
250 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
251 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
252 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
253 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
254 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
255 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
256
257 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
258 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
259 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
260
Lei Wen18c81b12010-08-17 17:25:57 +0800261 info->ndtr0cs0 = ndtr0;
262 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800263 nand_writel(info, NDTR0CS0, ndtr0);
264 nand_writel(info, NDTR1CS0, ndtr1);
265}
266
Lei Wen18c81b12010-08-17 17:25:57 +0800267static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800268{
Lei Wen9d8b1042010-08-17 14:09:30 +0800269 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
270
271 info->data_size = info->page_size;
272 if (!oob_enable) {
273 info->oob_size = 0;
274 return;
275 }
276
Lei Wen18c81b12010-08-17 17:25:57 +0800277 switch (info->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800278 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800279 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800280 break;
281 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800282 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800283 break;
eric miaofe69af02008-02-14 15:48:23 +0800284 }
Lei Wen18c81b12010-08-17 17:25:57 +0800285}
286
Lei Wenf8155a42011-02-28 10:32:11 +0800287/**
288 * NOTE: it is a must to set ND_RUN firstly, then write
289 * command buffer, otherwise, it does not work.
290 * We enable all the interrupt at the same time, and
291 * let pxa3xx_nand_irq to handle all logic.
292 */
293static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
294{
295 uint32_t ndcr;
296
297 ndcr = info->reg_ndcr;
298 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
299 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
300 ndcr |= NDCR_ND_RUN;
301
302 /* clear status bits and run */
303 nand_writel(info, NDCR, 0);
304 nand_writel(info, NDSR, NDSR_MASK);
305 nand_writel(info, NDCR, ndcr);
306}
307
308static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
309{
310 uint32_t ndcr;
311 int timeout = NAND_STOP_DELAY;
312
313 /* wait RUN bit in NDCR become 0 */
314 ndcr = nand_readl(info, NDCR);
315 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
316 ndcr = nand_readl(info, NDCR);
317 udelay(1);
318 }
319
320 if (timeout <= 0) {
321 ndcr &= ~NDCR_ND_RUN;
322 nand_writel(info, NDCR, ndcr);
323 }
324 /* clear status bits */
325 nand_writel(info, NDSR, NDSR_MASK);
326}
327
eric miaofe69af02008-02-14 15:48:23 +0800328static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
329{
330 uint32_t ndcr;
331
332 ndcr = nand_readl(info, NDCR);
333 nand_writel(info, NDCR, ndcr & ~int_mask);
334}
335
336static void disable_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
Lei Wenf8155a42011-02-28 10:32:11 +0800344static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800345{
eric miaofe69af02008-02-14 15:48:23 +0800346 switch (info->state) {
347 case STATE_PIO_WRITING:
348 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800349 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800350 if (info->oob_size > 0)
351 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
352 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800353 break;
354 case STATE_PIO_READING:
355 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800356 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800357 if (info->oob_size > 0)
358 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
359 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800360 break;
361 default:
David Woodhousea1c06ee2008-04-22 20:39:43 +0100362 printk(KERN_ERR "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800363 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800364 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800365 }
eric miaofe69af02008-02-14 15:48:23 +0800366}
367
Lei Wenf8155a42011-02-28 10:32:11 +0800368static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800369{
370 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800371 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800372
373 desc->ddadr = DDADR_STOP;
374 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
375
Lei Wenf8155a42011-02-28 10:32:11 +0800376 switch (info->state) {
377 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800378 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800379 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800380 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800381 break;
382 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800383 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800384 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800385 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800386 break;
387 default:
388 printk(KERN_ERR "%s: invalid state %d\n", __func__,
389 info->state);
390 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800391 }
392
393 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
394 DDADR(info->data_dma_ch) = info->data_desc_addr;
395 DCSR(info->data_dma_ch) |= DCSR_RUN;
396}
397
398static void pxa3xx_nand_data_dma_irq(int channel, void *data)
399{
400 struct pxa3xx_nand_info *info = data;
401 uint32_t dcsr;
402
403 dcsr = DCSR(channel);
404 DCSR(channel) = dcsr;
405
406 if (dcsr & DCSR_BUSERR) {
407 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800408 }
409
Lei Wenf8155a42011-02-28 10:32:11 +0800410 info->state = STATE_DMA_DONE;
411 enable_int(info, NDCR_INT_MASK);
412 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800413}
414
415static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
416{
417 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800418 unsigned int status, is_completed = 0;
eric miaofe69af02008-02-14 15:48:23 +0800419
420 status = nand_readl(info, NDSR);
421
Lei Wenf8155a42011-02-28 10:32:11 +0800422 if (status & NDSR_DBERR)
423 info->retcode = ERR_DBERR;
424 if (status & NDSR_SBERR)
425 info->retcode = ERR_SBERR;
426 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
427 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800428 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800429 disable_int(info, NDCR_INT_MASK);
430 info->state = (status & NDSR_RDDREQ) ?
431 STATE_DMA_READING : STATE_DMA_WRITING;
432 start_data_dma(info);
433 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800434 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800435 info->state = (status & NDSR_RDDREQ) ?
436 STATE_PIO_READING : STATE_PIO_WRITING;
437 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800438 }
Lei Wenf8155a42011-02-28 10:32:11 +0800439 }
440 if (status & NDSR_CS0_CMDD) {
441 info->state = STATE_CMD_DONE;
442 is_completed = 1;
443 }
Lei Wen401e67e2011-02-28 10:32:14 +0800444 if (status & NDSR_FLASH_RDY) {
445 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800446 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800447 }
Lei Wenf8155a42011-02-28 10:32:11 +0800448
449 if (status & NDSR_WRCMDREQ) {
450 nand_writel(info, NDSR, NDSR_WRCMDREQ);
451 status &= ~NDSR_WRCMDREQ;
452 info->state = STATE_CMD_HANDLE;
453 nand_writel(info, NDCB0, info->ndcb0);
454 nand_writel(info, NDCB0, info->ndcb1);
455 nand_writel(info, NDCB0, info->ndcb2);
eric miaofe69af02008-02-14 15:48:23 +0800456 }
Lei Wenf8155a42011-02-28 10:32:11 +0800457
458 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800459 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800460 if (is_completed)
461 complete(&info->cmd_complete);
462NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800463 return IRQ_HANDLED;
464}
465
eric miaofe69af02008-02-14 15:48:23 +0800466static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
467{
468 struct pxa3xx_nand_info *info = mtd->priv;
469 return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
470}
471
472static inline int is_buf_blank(uint8_t *buf, size_t len)
473{
474 for (; len > 0; len--)
475 if (*buf++ != 0xff)
476 return 0;
477 return 1;
478}
479
Lei Wen4eb2da82011-02-28 10:32:13 +0800480static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
481 uint16_t column, int page_addr)
482{
483 uint16_t cmd;
484 int addr_cycle, exec_cmd, ndcb0;
485 struct mtd_info *mtd = info->mtd;
486
487 ndcb0 = 0;
488 addr_cycle = 0;
489 exec_cmd = 1;
490
491 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800492 info->buf_start = 0;
493 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800494 info->oob_size = 0;
495 info->use_ecc = 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800496 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800497 info->retcode = ERR_NONE;
498
499 switch (command) {
500 case NAND_CMD_READ0:
501 case NAND_CMD_PAGEPROG:
502 info->use_ecc = 1;
503 case NAND_CMD_READOOB:
504 pxa3xx_set_datasize(info);
505 break;
506 case NAND_CMD_SEQIN:
507 exec_cmd = 0;
508 break;
509 default:
510 info->ndcb1 = 0;
511 info->ndcb2 = 0;
512 break;
513 }
514
515 info->ndcb0 = ndcb0;
516 addr_cycle = NDCB0_ADDR_CYC(info->row_addr_cycles
517 + info->col_addr_cycles);
518
519 switch (command) {
520 case NAND_CMD_READOOB:
521 case NAND_CMD_READ0:
522 cmd = info->cmdset->read1;
523 if (command == NAND_CMD_READOOB)
524 info->buf_start = mtd->writesize + column;
525 else
526 info->buf_start = column;
527
528 if (unlikely(info->page_size < PAGE_CHUNK_SIZE))
529 info->ndcb0 |= NDCB0_CMD_TYPE(0)
530 | addr_cycle
531 | (cmd & NDCB0_CMD1_MASK);
532 else
533 info->ndcb0 |= NDCB0_CMD_TYPE(0)
534 | NDCB0_DBC
535 | addr_cycle
536 | cmd;
537
538 case NAND_CMD_SEQIN:
539 /* small page addr setting */
540 if (unlikely(info->page_size < PAGE_CHUNK_SIZE)) {
541 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
542 | (column & 0xFF);
543
544 info->ndcb2 = 0;
545 } else {
546 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
547 | (column & 0xFFFF);
548
549 if (page_addr & 0xFF0000)
550 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
551 else
552 info->ndcb2 = 0;
553 }
554
555 info->buf_count = mtd->writesize + mtd->oobsize;
556 memset(info->data_buff, 0xFF, info->buf_count);
557
558 break;
559
560 case NAND_CMD_PAGEPROG:
561 if (is_buf_blank(info->data_buff,
562 (mtd->writesize + mtd->oobsize))) {
563 exec_cmd = 0;
564 break;
565 }
566
567 cmd = info->cmdset->program;
568 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
569 | NDCB0_AUTO_RS
570 | NDCB0_ST_ROW_EN
571 | NDCB0_DBC
572 | cmd
573 | addr_cycle;
574 break;
575
576 case NAND_CMD_READID:
577 cmd = info->cmdset->read_id;
578 info->buf_count = info->read_id_bytes;
579 info->ndcb0 |= NDCB0_CMD_TYPE(3)
580 | NDCB0_ADDR_CYC(1)
581 | cmd;
582
583 info->data_size = 8;
584 break;
585 case NAND_CMD_STATUS:
586 cmd = info->cmdset->read_status;
587 info->buf_count = 1;
588 info->ndcb0 |= NDCB0_CMD_TYPE(4)
589 | NDCB0_ADDR_CYC(1)
590 | cmd;
591
592 info->data_size = 8;
593 break;
594
595 case NAND_CMD_ERASE1:
596 cmd = info->cmdset->erase;
597 info->ndcb0 |= NDCB0_CMD_TYPE(2)
598 | NDCB0_AUTO_RS
599 | NDCB0_ADDR_CYC(3)
600 | NDCB0_DBC
601 | cmd;
602 info->ndcb1 = page_addr;
603 info->ndcb2 = 0;
604
605 break;
606 case NAND_CMD_RESET:
607 cmd = info->cmdset->reset;
608 info->ndcb0 |= NDCB0_CMD_TYPE(5)
609 | cmd;
610
611 break;
612
613 case NAND_CMD_ERASE2:
614 exec_cmd = 0;
615 break;
616
617 default:
618 exec_cmd = 0;
619 printk(KERN_ERR "pxa3xx-nand: non-supported"
620 " command %x\n", command);
621 break;
622 }
623
624 return exec_cmd;
625}
626
eric miaofe69af02008-02-14 15:48:23 +0800627static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100628 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800629{
630 struct pxa3xx_nand_info *info = mtd->priv;
Lei Wen4eb2da82011-02-28 10:32:13 +0800631 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800632
Lei Wen4eb2da82011-02-28 10:32:13 +0800633 /*
634 * if this is a x16 device ,then convert the input
635 * "byte" address into a "word" address appropriate
636 * for indexing a word-oriented device
637 */
638 if (info->reg_ndcr & NDCR_DWIDTH_M)
639 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800640
Lei Wen4eb2da82011-02-28 10:32:13 +0800641 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800642 if (exec_cmd) {
643 init_completion(&info->cmd_complete);
644 pxa3xx_nand_start(info);
645
646 ret = wait_for_completion_timeout(&info->cmd_complete,
647 CHIP_DELAY_TIMEOUT);
648 if (!ret) {
649 printk(KERN_ERR "Wait time out!!!\n");
650 /* Stop State Machine for next command cycle */
651 pxa3xx_nand_stop(info);
652 }
653 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800654 }
655}
656
Lei Wenf8155a42011-02-28 10:32:11 +0800657static void pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
658 struct nand_chip *chip, const uint8_t *buf)
659{
660 chip->write_buf(mtd, buf, mtd->writesize);
661 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
662}
663
664static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
665 struct nand_chip *chip, uint8_t *buf, int page)
666{
667 struct pxa3xx_nand_info *info = mtd->priv;
668
669 chip->read_buf(mtd, buf, mtd->writesize);
670 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
671
672 if (info->retcode == ERR_SBERR) {
673 switch (info->use_ecc) {
674 case 1:
675 mtd->ecc_stats.corrected++;
676 break;
677 case 0:
678 default:
679 break;
680 }
681 } else if (info->retcode == ERR_DBERR) {
682 /*
683 * for blank page (all 0xff), HW will calculate its ECC as
684 * 0, which is different from the ECC information within
685 * OOB, ignore such double bit errors
686 */
687 if (is_buf_blank(buf, mtd->writesize))
688 mtd->ecc_stats.failed++;
689 }
690
691 return 0;
692}
693
eric miaofe69af02008-02-14 15:48:23 +0800694static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
695{
696 struct pxa3xx_nand_info *info = mtd->priv;
697 char retval = 0xFF;
698
699 if (info->buf_start < info->buf_count)
700 /* Has just send a new command? */
701 retval = info->data_buff[info->buf_start++];
702
703 return retval;
704}
705
706static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
707{
708 struct pxa3xx_nand_info *info = mtd->priv;
709 u16 retval = 0xFFFF;
710
711 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
712 retval = *((u16 *)(info->data_buff+info->buf_start));
713 info->buf_start += 2;
714 }
715 return retval;
716}
717
718static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
719{
720 struct pxa3xx_nand_info *info = mtd->priv;
721 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
722
723 memcpy(buf, info->data_buff + info->buf_start, real_len);
724 info->buf_start += real_len;
725}
726
727static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
728 const uint8_t *buf, int len)
729{
730 struct pxa3xx_nand_info *info = mtd->priv;
731 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
732
733 memcpy(info->data_buff + info->buf_start, buf, real_len);
734 info->buf_start += real_len;
735}
736
737static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
738 const uint8_t *buf, int len)
739{
740 return 0;
741}
742
743static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
744{
745 return;
746}
747
748static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
749{
750 struct pxa3xx_nand_info *info = mtd->priv;
751
752 /* pxa3xx_nand_send_command has waited for command complete */
753 if (this->state == FL_WRITING || this->state == FL_ERASING) {
754 if (info->retcode == ERR_NONE)
755 return 0;
756 else {
757 /*
758 * any error make it return 0x01 which will tell
759 * the caller the erase and write fail
760 */
761 return 0x01;
762 }
763 }
764
765 return 0;
766}
767
eric miaofe69af02008-02-14 15:48:23 +0800768static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200769 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800770{
771 struct platform_device *pdev = info->pdev;
772 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800773 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800774
775 if (f->page_size != 2048 && f->page_size != 512)
776 return -EINVAL;
777
778 if (f->flash_width != 16 && f->flash_width != 8)
779 return -EINVAL;
780
781 /* calculate flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800782 info->cmdset = f->cmdset;
783 info->page_size = f->page_size;
784 info->oob_buff = info->data_buff + f->page_size;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200785 info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800786
787 /* calculate addressing information */
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200788 info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800789
790 if (f->num_blocks * f->page_per_block > 65536)
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200791 info->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800792 else
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200793 info->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800794
795 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200796 ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800797 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
798 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
799 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
800 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
801
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200802 ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800803 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
804
805 info->reg_ndcr = ndcr;
806
807 pxa3xx_nand_set_timing(info, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800808 return 0;
809}
810
Mike Rapoportf2710492009-02-17 13:54:47 +0200811static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
812{
813 uint32_t ndcr = nand_readl(info, NDCR);
814 struct nand_flash_dev *type = NULL;
Lei Wen18c81b12010-08-17 17:25:57 +0800815 uint32_t id = -1, page_per_block, num_blocks;
Mike Rapoportf2710492009-02-17 13:54:47 +0200816 int i;
817
Lei Wen18c81b12010-08-17 17:25:57 +0800818 page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
819 info->page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Lei Wen38caf7f2011-02-28 10:32:12 +0800820 /* set info fields needed to read id */
Lei Wen18c81b12010-08-17 17:25:57 +0800821 info->read_id_bytes = (info->page_size == 2048) ? 4 : 2;
Mike Rapoportf2710492009-02-17 13:54:47 +0200822 info->reg_ndcr = ndcr;
Dan Carpenter52d039f2011-01-06 17:05:36 +0300823 info->cmdset = &default_cmdset;
Mike Rapoportf2710492009-02-17 13:54:47 +0200824
Lei Wenf8155a42011-02-28 10:32:11 +0800825 pxa3xx_nand_cmdfunc(info->mtd, NAND_CMD_READID, 0, 0);
826 id = *((uint16_t *)(info->data_buff));
827 if (id == 0)
Mike Rapoportf2710492009-02-17 13:54:47 +0200828 return -ENODEV;
829
830 /* Lookup the flash id */
Mike Rapoportf2710492009-02-17 13:54:47 +0200831 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
832 if (id == nand_flash_ids[i].id) {
833 type = &nand_flash_ids[i];
834 break;
835 }
836 }
837
838 if (!type)
839 return -ENODEV;
840
841 /* fill the missing flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800842 i = __ffs(page_per_block * info->page_size);
843 num_blocks = type->chipsize << (20 - i);
Mike Rapoportf2710492009-02-17 13:54:47 +0200844
Mike Rapoportf2710492009-02-17 13:54:47 +0200845 /* calculate addressing information */
Lei Wen18c81b12010-08-17 17:25:57 +0800846 info->col_addr_cycles = (info->page_size == 2048) ? 2 : 1;
Mike Rapoportf2710492009-02-17 13:54:47 +0200847
Lei Wen18c81b12010-08-17 17:25:57 +0800848 if (num_blocks * page_per_block > 65536)
Mike Rapoportf2710492009-02-17 13:54:47 +0200849 info->row_addr_cycles = 3;
850 else
851 info->row_addr_cycles = 2;
852
Lei Wen18c81b12010-08-17 17:25:57 +0800853 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
854 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200855
856 return 0;
857}
858
eric miaofe69af02008-02-14 15:48:23 +0800859/* the maximum possible buffer size for large page with OOB data
860 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
861 * data buffer and the DMA descriptor
862 */
863#define MAX_BUFF_SIZE PAGE_SIZE
864
865static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
866{
867 struct platform_device *pdev = info->pdev;
868 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
869
870 if (use_dma == 0) {
871 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
872 if (info->data_buff == NULL)
873 return -ENOMEM;
874 return 0;
875 }
876
877 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
878 &info->data_buff_phys, GFP_KERNEL);
879 if (info->data_buff == NULL) {
880 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
881 return -ENOMEM;
882 }
883
884 info->data_buff_size = MAX_BUFF_SIZE;
885 info->data_desc = (void *)info->data_buff + data_desc_offset;
886 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
887
888 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
889 pxa3xx_nand_data_dma_irq, info);
890 if (info->data_dma_ch < 0) {
891 dev_err(&pdev->dev, "failed to request data dma\n");
892 dma_free_coherent(&pdev->dev, info->data_buff_size,
893 info->data_buff, info->data_buff_phys);
894 return info->data_dma_ch;
895 }
896
897 return 0;
898}
899
Lei Wen401e67e2011-02-28 10:32:14 +0800900static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800901{
Lei Wen401e67e2011-02-28 10:32:14 +0800902 struct mtd_info *mtd = info->mtd;
903 struct nand_chip *chip = mtd->priv;
eric miaofe69af02008-02-14 15:48:23 +0800904
Lei Wen401e67e2011-02-28 10:32:14 +0800905 /* use the common timing to make a try */
906 pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
907 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
908 if (info->is_ready)
909 return 1;
eric miaofe69af02008-02-14 15:48:23 +0800910 else
Lei Wen401e67e2011-02-28 10:32:14 +0800911 return 0;
912}
eric miaofe69af02008-02-14 15:48:23 +0800913
Lei Wen401e67e2011-02-28 10:32:14 +0800914static int pxa3xx_nand_scan(struct mtd_info *mtd)
915{
916 struct pxa3xx_nand_info *info = mtd->priv;
917 struct platform_device *pdev = info->pdev;
918 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
919 const struct pxa3xx_nand_flash *f = NULL;
920 struct nand_chip *chip = mtd->priv;
921 uint32_t id = -1;
922 int i, ret, num;
923
924 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
925 return 0;
926
927 ret = pxa3xx_nand_sensing(info);
928 if (!ret) {
929 kfree(mtd);
930 info->mtd = NULL;
931 printk(KERN_INFO "There is no nand chip on cs 0!\n");
932
933 return -EINVAL;
934 }
935
936 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
937 id = *((uint16_t *)(info->data_buff));
938 if (id != 0)
939 printk(KERN_INFO "Detect a flash id %x\n", id);
940 else {
941 kfree(mtd);
942 info->mtd = NULL;
943 printk(KERN_WARNING "Read out ID 0, potential timing set wrong!!\n");
944
945 return -EINVAL;
946 }
947
948 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
949 for (i = 0; i < num; i++) {
950 if (i < pdata->num_flash)
951 f = pdata->flash + i;
952 else
953 f = &builtin_flash_types[i - pdata->num_flash + 1];
954
955 /* find the chip in default list */
956 if (f->chip_id == id) {
957 pxa3xx_nand_config_flash(info, f);
958 mtd->writesize = f->page_size;
959 mtd->writesize_shift = ffs(mtd->writesize) - 1;
960 mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
961 mtd->oobsize = mtd->writesize / 32;
962 mtd->erasesize = f->page_size * f->page_per_block;
963 mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
964 mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
965
966 mtd->name = mtd_names[0];
967 break;
968 }
969 }
970
971 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash)) {
972 kfree(mtd);
973 info->mtd = NULL;
974 printk(KERN_ERR "ERROR!! flash not defined!!!\n");
975
976 return -EINVAL;
977 }
978
979 chip->ecc.mode = NAND_ECC_HW;
980 chip->ecc.size = f->page_size;
981 chip->chipsize = (uint64_t)f->num_blocks * f->page_per_block
982 * f->page_size;
983 mtd->size = chip->chipsize;
984
985 /* Calculate the address shift from the page size */
986 chip->page_shift = ffs(mtd->writesize) - 1;
987 chip->pagemask = mtd_div_by_ws(chip->chipsize, mtd) - 1;
988 chip->numchips = 1;
989 chip->phys_erase_shift = ffs(mtd->erasesize) - 1;
990 chip->bbt_erase_shift = chip->phys_erase_shift;
991
992 chip->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16 : 0;
993 chip->options |= NAND_NO_AUTOINCR;
994 chip->options |= NAND_NO_READRDY;
995
996 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +0800997}
998
Lei Wene353a202011-03-03 11:08:30 +0800999static
1000struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001001{
eric miaofe69af02008-02-14 15:48:23 +08001002 struct pxa3xx_nand_info *info;
Lei Wen401e67e2011-02-28 10:32:14 +08001003 struct nand_chip *chip;
eric miaofe69af02008-02-14 15:48:23 +08001004 struct mtd_info *mtd;
1005 struct resource *r;
Lei Wene353a202011-03-03 11:08:30 +08001006 int ret, irq;
eric miaofe69af02008-02-14 15:48:23 +08001007
1008 mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
1009 GFP_KERNEL);
David Woodhousea1c06ee2008-04-22 20:39:43 +01001010 if (!mtd) {
eric miaofe69af02008-02-14 15:48:23 +08001011 dev_err(&pdev->dev, "failed to allocate memory\n");
Lei Wene353a202011-03-03 11:08:30 +08001012 return NULL;
David Woodhousea1c06ee2008-04-22 20:39:43 +01001013 }
eric miaofe69af02008-02-14 15:48:23 +08001014
1015 info = (struct pxa3xx_nand_info *)(&mtd[1]);
Lei Wen401e67e2011-02-28 10:32:14 +08001016 chip = (struct nand_chip *)(&mtd[1]);
eric miaofe69af02008-02-14 15:48:23 +08001017 info->pdev = pdev;
Lei Wene353a202011-03-03 11:08:30 +08001018 info->mtd = mtd;
Lei Wen401e67e2011-02-28 10:32:14 +08001019 mtd->priv = info;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001020 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001021
Lei Wen401e67e2011-02-28 10:32:14 +08001022 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1023 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1024 chip->controller = &info->controller;
1025 chip->waitfunc = pxa3xx_nand_waitfunc;
1026 chip->select_chip = pxa3xx_nand_select_chip;
1027 chip->dev_ready = pxa3xx_nand_dev_ready;
1028 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1029 chip->read_word = pxa3xx_nand_read_word;
1030 chip->read_byte = pxa3xx_nand_read_byte;
1031 chip->read_buf = pxa3xx_nand_read_buf;
1032 chip->write_buf = pxa3xx_nand_write_buf;
1033 chip->verify_buf = pxa3xx_nand_verify_buf;
1034
1035 spin_lock_init(&chip->controller->lock);
1036 init_waitqueue_head(&chip->controller->wq);
Russell Kinge0d8b132008-11-11 17:52:32 +00001037 info->clk = clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001038 if (IS_ERR(info->clk)) {
1039 dev_err(&pdev->dev, "failed to get nand clock\n");
1040 ret = PTR_ERR(info->clk);
1041 goto fail_free_mtd;
1042 }
1043 clk_enable(info->clk);
1044
1045 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1046 if (r == NULL) {
1047 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1048 ret = -ENXIO;
1049 goto fail_put_clk;
1050 }
1051 info->drcmr_dat = r->start;
1052
1053 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1054 if (r == NULL) {
1055 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1056 ret = -ENXIO;
1057 goto fail_put_clk;
1058 }
1059 info->drcmr_cmd = r->start;
1060
1061 irq = platform_get_irq(pdev, 0);
1062 if (irq < 0) {
1063 dev_err(&pdev->dev, "no IRQ resource defined\n");
1064 ret = -ENXIO;
1065 goto fail_put_clk;
1066 }
1067
1068 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1069 if (r == NULL) {
1070 dev_err(&pdev->dev, "no IO memory resource defined\n");
1071 ret = -ENODEV;
1072 goto fail_put_clk;
1073 }
1074
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001075 r = request_mem_region(r->start, resource_size(r), pdev->name);
eric miaofe69af02008-02-14 15:48:23 +08001076 if (r == NULL) {
1077 dev_err(&pdev->dev, "failed to request memory resource\n");
1078 ret = -EBUSY;
1079 goto fail_put_clk;
1080 }
1081
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001082 info->mmio_base = ioremap(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001083 if (info->mmio_base == NULL) {
1084 dev_err(&pdev->dev, "ioremap() failed\n");
1085 ret = -ENODEV;
1086 goto fail_free_res;
1087 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001088 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001089
1090 ret = pxa3xx_nand_init_buff(info);
1091 if (ret)
1092 goto fail_free_io;
1093
Haojian Zhuang346e1252009-09-10 14:27:23 +08001094 /* initialize all interrupts to be disabled */
1095 disable_int(info, NDSR_MASK);
1096
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001097 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1098 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001099 if (ret < 0) {
1100 dev_err(&pdev->dev, "failed to request IRQ\n");
1101 goto fail_free_buf;
1102 }
1103
Lei Wene353a202011-03-03 11:08:30 +08001104 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001105
Lei Wene353a202011-03-03 11:08:30 +08001106 return info;
eric miaofe69af02008-02-14 15:48:23 +08001107
eric miaofe69af02008-02-14 15:48:23 +08001108fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001109 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001110 if (use_dma) {
1111 pxa_free_dma(info->data_dma_ch);
1112 dma_free_coherent(&pdev->dev, info->data_buff_size,
1113 info->data_buff, info->data_buff_phys);
1114 } else
1115 kfree(info->data_buff);
1116fail_free_io:
1117 iounmap(info->mmio_base);
1118fail_free_res:
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001119 release_mem_region(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001120fail_put_clk:
1121 clk_disable(info->clk);
1122 clk_put(info->clk);
1123fail_free_mtd:
1124 kfree(mtd);
Lei Wene353a202011-03-03 11:08:30 +08001125 return NULL;
eric miaofe69af02008-02-14 15:48:23 +08001126}
1127
1128static int pxa3xx_nand_remove(struct platform_device *pdev)
1129{
Lei Wene353a202011-03-03 11:08:30 +08001130 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1131 struct mtd_info *mtd = info->mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001132 struct resource *r;
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001133 int irq;
eric miaofe69af02008-02-14 15:48:23 +08001134
1135 platform_set_drvdata(pdev, NULL);
1136
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001137 irq = platform_get_irq(pdev, 0);
1138 if (irq >= 0)
1139 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001140 if (use_dma) {
1141 pxa_free_dma(info->data_dma_ch);
1142 dma_free_writecombine(&pdev->dev, info->data_buff_size,
1143 info->data_buff, info->data_buff_phys);
1144 } else
1145 kfree(info->data_buff);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001146
1147 iounmap(info->mmio_base);
1148 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1149 release_mem_region(r->start, resource_size(r));
1150
1151 clk_disable(info->clk);
1152 clk_put(info->clk);
1153
Lei Wenf8155a42011-02-28 10:32:11 +08001154 if (mtd) {
1155 del_mtd_device(mtd);
1156#ifdef CONFIG_MTD_PARTITIONS
1157 del_mtd_partitions(mtd);
1158#endif
1159 kfree(mtd);
1160 }
eric miaofe69af02008-02-14 15:48:23 +08001161 return 0;
1162}
1163
Lei Wene353a202011-03-03 11:08:30 +08001164static int pxa3xx_nand_probe(struct platform_device *pdev)
1165{
1166 struct pxa3xx_nand_platform_data *pdata;
1167 struct pxa3xx_nand_info *info;
1168
1169 pdata = pdev->dev.platform_data;
1170 if (!pdata) {
1171 dev_err(&pdev->dev, "no platform data defined\n");
1172 return -ENODEV;
1173 }
1174
1175 info = alloc_nand_resource(pdev);
1176 if (info == NULL)
1177 return -ENOMEM;
1178
Lei Wen401e67e2011-02-28 10:32:14 +08001179 if (pxa3xx_nand_scan(info->mtd)) {
Lei Wene353a202011-03-03 11:08:30 +08001180 dev_err(&pdev->dev, "failed to scan nand\n");
1181 pxa3xx_nand_remove(pdev);
1182 return -ENODEV;
1183 }
1184
1185#ifdef CONFIG_MTD_PARTITIONS
1186 if (mtd_has_cmdlinepart()) {
1187 const char *probes[] = { "cmdlinepart", NULL };
1188 struct mtd_partition *parts;
1189 int nr_parts;
1190
1191 nr_parts = parse_mtd_partitions(info->mtd, probes, &parts, 0);
1192
1193 if (nr_parts)
Lei Wenf8155a42011-02-28 10:32:11 +08001194 return add_mtd_partitions(info->mtd, parts, nr_parts);
Lei Wene353a202011-03-03 11:08:30 +08001195 }
1196
Lei Wenf8155a42011-02-28 10:32:11 +08001197 return add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
Lei Wene353a202011-03-03 11:08:30 +08001198#else
1199 return 0;
1200#endif
1201}
1202
eric miaofe69af02008-02-14 15:48:23 +08001203#ifdef CONFIG_PM
1204static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1205{
Lei Wene353a202011-03-03 11:08:30 +08001206 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1207 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001208
Lei Wenf8155a42011-02-28 10:32:11 +08001209 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001210 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1211 return -EAGAIN;
1212 }
1213
1214 return 0;
1215}
1216
1217static int pxa3xx_nand_resume(struct platform_device *pdev)
1218{
Lei Wene353a202011-03-03 11:08:30 +08001219 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1220 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001221
Lei Wen18c81b12010-08-17 17:25:57 +08001222 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1223 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
eric miaofe69af02008-02-14 15:48:23 +08001224 clk_enable(info->clk);
1225
Lei Wen18c81b12010-08-17 17:25:57 +08001226 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001227}
1228#else
1229#define pxa3xx_nand_suspend NULL
1230#define pxa3xx_nand_resume NULL
1231#endif
1232
1233static struct platform_driver pxa3xx_nand_driver = {
1234 .driver = {
1235 .name = "pxa3xx-nand",
1236 },
1237 .probe = pxa3xx_nand_probe,
1238 .remove = pxa3xx_nand_remove,
1239 .suspend = pxa3xx_nand_suspend,
1240 .resume = pxa3xx_nand_resume,
1241};
1242
1243static int __init pxa3xx_nand_init(void)
1244{
1245 return platform_driver_register(&pxa3xx_nand_driver);
1246}
1247module_init(pxa3xx_nand_init);
1248
1249static void __exit pxa3xx_nand_exit(void)
1250{
1251 platform_driver_unregister(&pxa3xx_nand_driver);
1252}
1253module_exit(pxa3xx_nand_exit);
1254
1255MODULE_LICENSE("GPL");
1256MODULE_DESCRIPTION("PXA3xx NAND controller driver");