blob: de0a2a25eb016f2b9a9ab732529b183848f17de2 [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
126 struct platform_device *pdev;
Lei Wen18c81b12010-08-17 17:25:57 +0800127 struct pxa3xx_nand_cmdset *cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800128
129 struct clk *clk;
130 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800131 unsigned long mmio_phys;
eric miaofe69af02008-02-14 15:48:23 +0800132
133 unsigned int buf_start;
134 unsigned int buf_count;
135
Lei Wene353a202011-03-03 11:08:30 +0800136 struct mtd_info *mtd;
eric miaofe69af02008-02-14 15:48:23 +0800137 /* DMA information */
138 int drcmr_dat;
139 int drcmr_cmd;
140
141 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800142 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800143 dma_addr_t data_buff_phys;
144 size_t data_buff_size;
145 int data_dma_ch;
146 struct pxa_dma_desc *data_desc;
147 dma_addr_t data_desc_addr;
148
149 uint32_t reg_ndcr;
150
151 /* saved column/page_addr during CMD_SEQIN */
152 int seqin_column;
153 int seqin_page_addr;
154
155 /* relate to the command */
156 unsigned int state;
157
158 int use_ecc; /* use HW ECC ? */
159 int use_dma; /* use DMA ? */
160
Lei Wen18c81b12010-08-17 17:25:57 +0800161 unsigned int page_size; /* page size of attached chip */
162 unsigned int data_size; /* data size in FIFO */
eric miaofe69af02008-02-14 15:48:23 +0800163 int retcode;
164 struct completion cmd_complete;
165
166 /* generated NDCBx register values */
167 uint32_t ndcb0;
168 uint32_t ndcb1;
169 uint32_t ndcb2;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200170
Lei Wen18c81b12010-08-17 17:25:57 +0800171 /* timing calcuted from setting */
172 uint32_t ndtr0cs0;
173 uint32_t ndtr1cs0;
174
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200175 /* calculated from pxa3xx_nand_flash data */
176 size_t oob_size;
177 size_t read_id_bytes;
178
179 unsigned int col_addr_cycles;
180 unsigned int row_addr_cycles;
eric miaofe69af02008-02-14 15:48:23 +0800181};
182
183static int use_dma = 1;
184module_param(use_dma, bool, 0444);
185MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
186
Mike Rapoportf2710492009-02-17 13:54:47 +0200187/*
188 * Default NAND flash controller configuration setup by the
189 * bootloader. This configuration is used only when pdata->keep_config is set
190 */
Lei Wenc1f82472010-08-17 13:50:23 +0800191static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800192 .read1 = 0x3000,
193 .read2 = 0x0050,
194 .program = 0x1080,
195 .read_status = 0x0070,
196 .read_id = 0x0090,
197 .erase = 0xD060,
198 .reset = 0x00FF,
199 .lock = 0x002A,
200 .unlock = 0x2423,
201 .lock_status = 0x007A,
202};
203
Lei Wenc1f82472010-08-17 13:50:23 +0800204static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800205 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
206 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
207 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
208 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800209};
210
Lei Wenc1f82472010-08-17 13:50:23 +0800211static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800212 { 0, 0, 2048, 8, 8, 0, &default_cmdset, &timing[0] },
213 { 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset, &timing[1] },
214 { 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset, &timing[1] },
215 { 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset, &timing[1] },
216 { 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset, &timing[2] },
217 { 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset, &timing[2] },
218 { 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset, &timing[2] },
219 { 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset, &timing[2] },
220 { 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800221};
222
Lei Wen227a8862010-08-18 18:00:03 +0800223/* Define a default flash type setting serve as flash detecting only */
224#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
225
eric miaofe69af02008-02-14 15:48:23 +0800226#define NDTR0_tCH(c) (min((c), 7) << 19)
227#define NDTR0_tCS(c) (min((c), 7) << 16)
228#define NDTR0_tWH(c) (min((c), 7) << 11)
229#define NDTR0_tWP(c) (min((c), 7) << 8)
230#define NDTR0_tRH(c) (min((c), 7) << 3)
231#define NDTR0_tRP(c) (min((c), 7) << 0)
232
233#define NDTR1_tR(c) (min((c), 65535) << 16)
234#define NDTR1_tWHR(c) (min((c), 15) << 4)
235#define NDTR1_tAR(c) (min((c), 15) << 0)
236
237/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800238#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800239
240static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200241 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800242{
243 unsigned long nand_clk = clk_get_rate(info->clk);
244 uint32_t ndtr0, ndtr1;
245
246 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
247 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
248 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
249 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
250 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
251 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
252
253 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
254 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
255 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
256
Lei Wen18c81b12010-08-17 17:25:57 +0800257 info->ndtr0cs0 = ndtr0;
258 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800259 nand_writel(info, NDTR0CS0, ndtr0);
260 nand_writel(info, NDTR1CS0, ndtr1);
261}
262
Lei Wen18c81b12010-08-17 17:25:57 +0800263static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800264{
Lei Wen9d8b1042010-08-17 14:09:30 +0800265 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
266
267 info->data_size = info->page_size;
268 if (!oob_enable) {
269 info->oob_size = 0;
270 return;
271 }
272
Lei Wen18c81b12010-08-17 17:25:57 +0800273 switch (info->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800274 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800275 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800276 break;
277 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800278 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800279 break;
eric miaofe69af02008-02-14 15:48:23 +0800280 }
Lei Wen18c81b12010-08-17 17:25:57 +0800281}
282
Lei Wenf8155a42011-02-28 10:32:11 +0800283/**
284 * NOTE: it is a must to set ND_RUN firstly, then write
285 * command buffer, otherwise, it does not work.
286 * We enable all the interrupt at the same time, and
287 * let pxa3xx_nand_irq to handle all logic.
288 */
289static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
290{
291 uint32_t ndcr;
292
293 ndcr = info->reg_ndcr;
294 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
295 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
296 ndcr |= NDCR_ND_RUN;
297
298 /* clear status bits and run */
299 nand_writel(info, NDCR, 0);
300 nand_writel(info, NDSR, NDSR_MASK);
301 nand_writel(info, NDCR, ndcr);
302}
303
304static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
305{
306 uint32_t ndcr;
307 int timeout = NAND_STOP_DELAY;
308
309 /* wait RUN bit in NDCR become 0 */
310 ndcr = nand_readl(info, NDCR);
311 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
312 ndcr = nand_readl(info, NDCR);
313 udelay(1);
314 }
315
316 if (timeout <= 0) {
317 ndcr &= ~NDCR_ND_RUN;
318 nand_writel(info, NDCR, ndcr);
319 }
320 /* clear status bits */
321 nand_writel(info, NDSR, NDSR_MASK);
322}
323
eric miaofe69af02008-02-14 15:48:23 +0800324static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
325{
326 uint32_t ndcr;
327
328 ndcr = nand_readl(info, NDCR);
329 nand_writel(info, NDCR, ndcr & ~int_mask);
330}
331
332static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
333{
334 uint32_t ndcr;
335
336 ndcr = nand_readl(info, NDCR);
337 nand_writel(info, NDCR, ndcr | int_mask);
338}
339
Lei Wenf8155a42011-02-28 10:32:11 +0800340static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800341{
eric miaofe69af02008-02-14 15:48:23 +0800342 switch (info->state) {
343 case STATE_PIO_WRITING:
344 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800345 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800346 if (info->oob_size > 0)
347 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
348 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800349 break;
350 case STATE_PIO_READING:
351 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800352 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800353 if (info->oob_size > 0)
354 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
355 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800356 break;
357 default:
David Woodhousea1c06ee2008-04-22 20:39:43 +0100358 printk(KERN_ERR "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800359 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800360 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800361 }
eric miaofe69af02008-02-14 15:48:23 +0800362}
363
Lei Wenf8155a42011-02-28 10:32:11 +0800364static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800365{
366 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800367 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800368
369 desc->ddadr = DDADR_STOP;
370 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
371
Lei Wenf8155a42011-02-28 10:32:11 +0800372 switch (info->state) {
373 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800374 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800375 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800376 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800377 break;
378 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800379 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800380 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800381 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800382 break;
383 default:
384 printk(KERN_ERR "%s: invalid state %d\n", __func__,
385 info->state);
386 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800387 }
388
389 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
390 DDADR(info->data_dma_ch) = info->data_desc_addr;
391 DCSR(info->data_dma_ch) |= DCSR_RUN;
392}
393
394static void pxa3xx_nand_data_dma_irq(int channel, void *data)
395{
396 struct pxa3xx_nand_info *info = data;
397 uint32_t dcsr;
398
399 dcsr = DCSR(channel);
400 DCSR(channel) = dcsr;
401
402 if (dcsr & DCSR_BUSERR) {
403 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800404 }
405
Lei Wenf8155a42011-02-28 10:32:11 +0800406 info->state = STATE_DMA_DONE;
407 enable_int(info, NDCR_INT_MASK);
408 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800409}
410
411static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
412{
413 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800414 unsigned int status, is_completed = 0;
eric miaofe69af02008-02-14 15:48:23 +0800415
416 status = nand_readl(info, NDSR);
417
Lei Wenf8155a42011-02-28 10:32:11 +0800418 if (status & NDSR_DBERR)
419 info->retcode = ERR_DBERR;
420 if (status & NDSR_SBERR)
421 info->retcode = ERR_SBERR;
422 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
423 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800424 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800425 disable_int(info, NDCR_INT_MASK);
426 info->state = (status & NDSR_RDDREQ) ?
427 STATE_DMA_READING : STATE_DMA_WRITING;
428 start_data_dma(info);
429 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800430 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800431 info->state = (status & NDSR_RDDREQ) ?
432 STATE_PIO_READING : STATE_PIO_WRITING;
433 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800434 }
Lei Wenf8155a42011-02-28 10:32:11 +0800435 }
436 if (status & NDSR_CS0_CMDD) {
437 info->state = STATE_CMD_DONE;
438 is_completed = 1;
439 }
440 if (status & NDSR_FLASH_RDY)
eric miaofe69af02008-02-14 15:48:23 +0800441 info->state = STATE_READY;
Lei Wenf8155a42011-02-28 10:32:11 +0800442
443 if (status & NDSR_WRCMDREQ) {
444 nand_writel(info, NDSR, NDSR_WRCMDREQ);
445 status &= ~NDSR_WRCMDREQ;
446 info->state = STATE_CMD_HANDLE;
447 nand_writel(info, NDCB0, info->ndcb0);
448 nand_writel(info, NDCB0, info->ndcb1);
449 nand_writel(info, NDCB0, info->ndcb2);
eric miaofe69af02008-02-14 15:48:23 +0800450 }
Lei Wenf8155a42011-02-28 10:32:11 +0800451
452 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800453 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800454 if (is_completed)
455 complete(&info->cmd_complete);
456NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800457 return IRQ_HANDLED;
458}
459
eric miaofe69af02008-02-14 15:48:23 +0800460static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
461{
462 struct pxa3xx_nand_info *info = mtd->priv;
463 return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
464}
465
466static inline int is_buf_blank(uint8_t *buf, size_t len)
467{
468 for (; len > 0; len--)
469 if (*buf++ != 0xff)
470 return 0;
471 return 1;
472}
473
Lei Wen4eb2da82011-02-28 10:32:13 +0800474static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
475 uint16_t column, int page_addr)
476{
477 uint16_t cmd;
478 int addr_cycle, exec_cmd, ndcb0;
479 struct mtd_info *mtd = info->mtd;
480
481 ndcb0 = 0;
482 addr_cycle = 0;
483 exec_cmd = 1;
484
485 /* reset data and oob column point to handle data */
486 info->buf_start = 0;
487 info->buf_count = 0;
488 info->oob_size = 0;
489 info->use_ecc = 0;
490 info->retcode = ERR_NONE;
491
492 switch (command) {
493 case NAND_CMD_READ0:
494 case NAND_CMD_PAGEPROG:
495 info->use_ecc = 1;
496 case NAND_CMD_READOOB:
497 pxa3xx_set_datasize(info);
498 break;
499 case NAND_CMD_SEQIN:
500 exec_cmd = 0;
501 break;
502 default:
503 info->ndcb1 = 0;
504 info->ndcb2 = 0;
505 break;
506 }
507
508 info->ndcb0 = ndcb0;
509 addr_cycle = NDCB0_ADDR_CYC(info->row_addr_cycles
510 + info->col_addr_cycles);
511
512 switch (command) {
513 case NAND_CMD_READOOB:
514 case NAND_CMD_READ0:
515 cmd = info->cmdset->read1;
516 if (command == NAND_CMD_READOOB)
517 info->buf_start = mtd->writesize + column;
518 else
519 info->buf_start = column;
520
521 if (unlikely(info->page_size < PAGE_CHUNK_SIZE))
522 info->ndcb0 |= NDCB0_CMD_TYPE(0)
523 | addr_cycle
524 | (cmd & NDCB0_CMD1_MASK);
525 else
526 info->ndcb0 |= NDCB0_CMD_TYPE(0)
527 | NDCB0_DBC
528 | addr_cycle
529 | cmd;
530
531 case NAND_CMD_SEQIN:
532 /* small page addr setting */
533 if (unlikely(info->page_size < PAGE_CHUNK_SIZE)) {
534 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
535 | (column & 0xFF);
536
537 info->ndcb2 = 0;
538 } else {
539 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
540 | (column & 0xFFFF);
541
542 if (page_addr & 0xFF0000)
543 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
544 else
545 info->ndcb2 = 0;
546 }
547
548 info->buf_count = mtd->writesize + mtd->oobsize;
549 memset(info->data_buff, 0xFF, info->buf_count);
550
551 break;
552
553 case NAND_CMD_PAGEPROG:
554 if (is_buf_blank(info->data_buff,
555 (mtd->writesize + mtd->oobsize))) {
556 exec_cmd = 0;
557 break;
558 }
559
560 cmd = info->cmdset->program;
561 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
562 | NDCB0_AUTO_RS
563 | NDCB0_ST_ROW_EN
564 | NDCB0_DBC
565 | cmd
566 | addr_cycle;
567 break;
568
569 case NAND_CMD_READID:
570 cmd = info->cmdset->read_id;
571 info->buf_count = info->read_id_bytes;
572 info->ndcb0 |= NDCB0_CMD_TYPE(3)
573 | NDCB0_ADDR_CYC(1)
574 | cmd;
575
576 info->data_size = 8;
577 break;
578 case NAND_CMD_STATUS:
579 cmd = info->cmdset->read_status;
580 info->buf_count = 1;
581 info->ndcb0 |= NDCB0_CMD_TYPE(4)
582 | NDCB0_ADDR_CYC(1)
583 | cmd;
584
585 info->data_size = 8;
586 break;
587
588 case NAND_CMD_ERASE1:
589 cmd = info->cmdset->erase;
590 info->ndcb0 |= NDCB0_CMD_TYPE(2)
591 | NDCB0_AUTO_RS
592 | NDCB0_ADDR_CYC(3)
593 | NDCB0_DBC
594 | cmd;
595 info->ndcb1 = page_addr;
596 info->ndcb2 = 0;
597
598 break;
599 case NAND_CMD_RESET:
600 cmd = info->cmdset->reset;
601 info->ndcb0 |= NDCB0_CMD_TYPE(5)
602 | cmd;
603
604 break;
605
606 case NAND_CMD_ERASE2:
607 exec_cmd = 0;
608 break;
609
610 default:
611 exec_cmd = 0;
612 printk(KERN_ERR "pxa3xx-nand: non-supported"
613 " command %x\n", command);
614 break;
615 }
616
617 return exec_cmd;
618}
619
eric miaofe69af02008-02-14 15:48:23 +0800620static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100621 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800622{
623 struct pxa3xx_nand_info *info = mtd->priv;
Lei Wen4eb2da82011-02-28 10:32:13 +0800624 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800625
Lei Wen4eb2da82011-02-28 10:32:13 +0800626 /*
627 * if this is a x16 device ,then convert the input
628 * "byte" address into a "word" address appropriate
629 * for indexing a word-oriented device
630 */
631 if (info->reg_ndcr & NDCR_DWIDTH_M)
632 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800633
Lei Wen4eb2da82011-02-28 10:32:13 +0800634 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800635 if (exec_cmd) {
636 init_completion(&info->cmd_complete);
637 pxa3xx_nand_start(info);
638
639 ret = wait_for_completion_timeout(&info->cmd_complete,
640 CHIP_DELAY_TIMEOUT);
641 if (!ret) {
642 printk(KERN_ERR "Wait time out!!!\n");
643 /* Stop State Machine for next command cycle */
644 pxa3xx_nand_stop(info);
645 }
646 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800647 }
648}
649
Lei Wenf8155a42011-02-28 10:32:11 +0800650static void pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
651 struct nand_chip *chip, const uint8_t *buf)
652{
653 chip->write_buf(mtd, buf, mtd->writesize);
654 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
655}
656
657static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
658 struct nand_chip *chip, uint8_t *buf, int page)
659{
660 struct pxa3xx_nand_info *info = mtd->priv;
661
662 chip->read_buf(mtd, buf, mtd->writesize);
663 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
664
665 if (info->retcode == ERR_SBERR) {
666 switch (info->use_ecc) {
667 case 1:
668 mtd->ecc_stats.corrected++;
669 break;
670 case 0:
671 default:
672 break;
673 }
674 } else if (info->retcode == ERR_DBERR) {
675 /*
676 * for blank page (all 0xff), HW will calculate its ECC as
677 * 0, which is different from the ECC information within
678 * OOB, ignore such double bit errors
679 */
680 if (is_buf_blank(buf, mtd->writesize))
681 mtd->ecc_stats.failed++;
682 }
683
684 return 0;
685}
686
eric miaofe69af02008-02-14 15:48:23 +0800687static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
688{
689 struct pxa3xx_nand_info *info = mtd->priv;
690 char retval = 0xFF;
691
692 if (info->buf_start < info->buf_count)
693 /* Has just send a new command? */
694 retval = info->data_buff[info->buf_start++];
695
696 return retval;
697}
698
699static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
700{
701 struct pxa3xx_nand_info *info = mtd->priv;
702 u16 retval = 0xFFFF;
703
704 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
705 retval = *((u16 *)(info->data_buff+info->buf_start));
706 info->buf_start += 2;
707 }
708 return retval;
709}
710
711static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
712{
713 struct pxa3xx_nand_info *info = mtd->priv;
714 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
715
716 memcpy(buf, info->data_buff + info->buf_start, real_len);
717 info->buf_start += real_len;
718}
719
720static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
721 const uint8_t *buf, int len)
722{
723 struct pxa3xx_nand_info *info = mtd->priv;
724 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
725
726 memcpy(info->data_buff + info->buf_start, buf, real_len);
727 info->buf_start += real_len;
728}
729
730static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
731 const uint8_t *buf, int len)
732{
733 return 0;
734}
735
736static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
737{
738 return;
739}
740
741static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
742{
743 struct pxa3xx_nand_info *info = mtd->priv;
744
745 /* pxa3xx_nand_send_command has waited for command complete */
746 if (this->state == FL_WRITING || this->state == FL_ERASING) {
747 if (info->retcode == ERR_NONE)
748 return 0;
749 else {
750 /*
751 * any error make it return 0x01 which will tell
752 * the caller the erase and write fail
753 */
754 return 0x01;
755 }
756 }
757
758 return 0;
759}
760
eric miaofe69af02008-02-14 15:48:23 +0800761static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200762 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800763{
764 struct platform_device *pdev = info->pdev;
765 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800766 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800767
768 if (f->page_size != 2048 && f->page_size != 512)
769 return -EINVAL;
770
771 if (f->flash_width != 16 && f->flash_width != 8)
772 return -EINVAL;
773
774 /* calculate flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800775 info->cmdset = f->cmdset;
776 info->page_size = f->page_size;
777 info->oob_buff = info->data_buff + f->page_size;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200778 info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800779
780 /* calculate addressing information */
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200781 info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800782
783 if (f->num_blocks * f->page_per_block > 65536)
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200784 info->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800785 else
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200786 info->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800787
788 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200789 ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800790 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
791 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
792 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
793 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
794
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200795 ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800796 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
797
798 info->reg_ndcr = ndcr;
799
800 pxa3xx_nand_set_timing(info, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800801 return 0;
802}
803
Mike Rapoportf2710492009-02-17 13:54:47 +0200804static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
805{
806 uint32_t ndcr = nand_readl(info, NDCR);
807 struct nand_flash_dev *type = NULL;
Lei Wen18c81b12010-08-17 17:25:57 +0800808 uint32_t id = -1, page_per_block, num_blocks;
Mike Rapoportf2710492009-02-17 13:54:47 +0200809 int i;
810
Lei Wen18c81b12010-08-17 17:25:57 +0800811 page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
812 info->page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Lei Wen38caf7f2011-02-28 10:32:12 +0800813 /* set info fields needed to read id */
Lei Wen18c81b12010-08-17 17:25:57 +0800814 info->read_id_bytes = (info->page_size == 2048) ? 4 : 2;
Mike Rapoportf2710492009-02-17 13:54:47 +0200815 info->reg_ndcr = ndcr;
Dan Carpenter52d039f2011-01-06 17:05:36 +0300816 info->cmdset = &default_cmdset;
Mike Rapoportf2710492009-02-17 13:54:47 +0200817
Lei Wenf8155a42011-02-28 10:32:11 +0800818 pxa3xx_nand_cmdfunc(info->mtd, NAND_CMD_READID, 0, 0);
819 id = *((uint16_t *)(info->data_buff));
820 if (id == 0)
Mike Rapoportf2710492009-02-17 13:54:47 +0200821 return -ENODEV;
822
823 /* Lookup the flash id */
Mike Rapoportf2710492009-02-17 13:54:47 +0200824 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
825 if (id == nand_flash_ids[i].id) {
826 type = &nand_flash_ids[i];
827 break;
828 }
829 }
830
831 if (!type)
832 return -ENODEV;
833
834 /* fill the missing flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800835 i = __ffs(page_per_block * info->page_size);
836 num_blocks = type->chipsize << (20 - i);
Mike Rapoportf2710492009-02-17 13:54:47 +0200837
Mike Rapoportf2710492009-02-17 13:54:47 +0200838 /* calculate addressing information */
Lei Wen18c81b12010-08-17 17:25:57 +0800839 info->col_addr_cycles = (info->page_size == 2048) ? 2 : 1;
Mike Rapoportf2710492009-02-17 13:54:47 +0200840
Lei Wen18c81b12010-08-17 17:25:57 +0800841 if (num_blocks * page_per_block > 65536)
Mike Rapoportf2710492009-02-17 13:54:47 +0200842 info->row_addr_cycles = 3;
843 else
844 info->row_addr_cycles = 2;
845
Lei Wen18c81b12010-08-17 17:25:57 +0800846 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
847 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200848
849 return 0;
850}
851
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200852static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
853 const struct pxa3xx_nand_platform_data *pdata)
eric miaofe69af02008-02-14 15:48:23 +0800854{
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200855 const struct pxa3xx_nand_flash *f;
Enrico Scholz2675e942008-08-29 12:59:52 +0200856 uint32_t id = -1;
eric miaofe69af02008-02-14 15:48:23 +0800857 int i;
858
Mike Rapoportf2710492009-02-17 13:54:47 +0200859 if (pdata->keep_config)
860 if (pxa3xx_nand_detect_config(info) == 0)
861 return 0;
862
Lei Wen227a8862010-08-18 18:00:03 +0800863 /* we use default timing to detect id */
864 f = DEFAULT_FLASH_TYPE;
865 pxa3xx_nand_config_flash(info, f);
Lei Wenf8155a42011-02-28 10:32:11 +0800866 pxa3xx_nand_cmdfunc(info->mtd, NAND_CMD_READID, 0, 0);
867 id = *((uint16_t *)(info->data_buff));
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200868
Lei Wen227a8862010-08-18 18:00:03 +0800869 for (i=0; i<ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; i++) {
870 /* we first choose the flash definition from platfrom */
871 if (i < pdata->num_flash)
872 f = pdata->flash + i;
873 else
874 f = &builtin_flash_types[i - pdata->num_flash + 1];
875 if (f->chip_id == id) {
876 dev_info(&info->pdev->dev, "detect chip id: 0x%x\n", id);
877 pxa3xx_nand_config_flash(info, f);
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200878 return 0;
Lei Wen227a8862010-08-18 18:00:03 +0800879 }
eric miaofe69af02008-02-14 15:48:23 +0800880 }
881
Enrico Scholz2675e942008-08-29 12:59:52 +0200882 dev_warn(&info->pdev->dev,
883 "failed to detect configured nand flash; found %04x instead of\n",
884 id);
eric miaofe69af02008-02-14 15:48:23 +0800885 return -ENODEV;
886}
887
888/* the maximum possible buffer size for large page with OOB data
889 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
890 * data buffer and the DMA descriptor
891 */
892#define MAX_BUFF_SIZE PAGE_SIZE
893
894static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
895{
896 struct platform_device *pdev = info->pdev;
897 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
898
899 if (use_dma == 0) {
900 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
901 if (info->data_buff == NULL)
902 return -ENOMEM;
903 return 0;
904 }
905
906 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
907 &info->data_buff_phys, GFP_KERNEL);
908 if (info->data_buff == NULL) {
909 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
910 return -ENOMEM;
911 }
912
913 info->data_buff_size = MAX_BUFF_SIZE;
914 info->data_desc = (void *)info->data_buff + data_desc_offset;
915 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
916
917 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
918 pxa3xx_nand_data_dma_irq, info);
919 if (info->data_dma_ch < 0) {
920 dev_err(&pdev->dev, "failed to request data dma\n");
921 dma_free_coherent(&pdev->dev, info->data_buff_size,
922 info->data_buff, info->data_buff_phys);
923 return info->data_dma_ch;
924 }
925
926 return 0;
927}
928
929static struct nand_ecclayout hw_smallpage_ecclayout = {
930 .eccbytes = 6,
931 .eccpos = {8, 9, 10, 11, 12, 13 },
932 .oobfree = { {2, 6} }
933};
934
935static struct nand_ecclayout hw_largepage_ecclayout = {
936 .eccbytes = 24,
937 .eccpos = {
938 40, 41, 42, 43, 44, 45, 46, 47,
939 48, 49, 50, 51, 52, 53, 54, 55,
940 56, 57, 58, 59, 60, 61, 62, 63},
941 .oobfree = { {2, 38} }
942};
943
944static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
945 struct pxa3xx_nand_info *info)
946{
eric miaofe69af02008-02-14 15:48:23 +0800947 struct nand_chip *this = &info->nand_chip;
948
Lei Wen18c81b12010-08-17 17:25:57 +0800949 this->options = (info->reg_ndcr & NDCR_DWIDTH_C) ? NAND_BUSWIDTH_16: 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800950 this->options |= NAND_NO_AUTOINCR;
eric miaofe69af02008-02-14 15:48:23 +0800951
952 this->waitfunc = pxa3xx_nand_waitfunc;
953 this->select_chip = pxa3xx_nand_select_chip;
954 this->dev_ready = pxa3xx_nand_dev_ready;
955 this->cmdfunc = pxa3xx_nand_cmdfunc;
Lei Wenf8155a42011-02-28 10:32:11 +0800956 this->ecc.read_page = pxa3xx_nand_read_page_hwecc;
957 this->ecc.write_page = pxa3xx_nand_write_page_hwecc;
eric miaofe69af02008-02-14 15:48:23 +0800958 this->read_word = pxa3xx_nand_read_word;
959 this->read_byte = pxa3xx_nand_read_byte;
960 this->read_buf = pxa3xx_nand_read_buf;
961 this->write_buf = pxa3xx_nand_write_buf;
962 this->verify_buf = pxa3xx_nand_verify_buf;
963
964 this->ecc.mode = NAND_ECC_HW;
Lei Wen18c81b12010-08-17 17:25:57 +0800965 this->ecc.size = info->page_size;
eric miaofe69af02008-02-14 15:48:23 +0800966
Lei Wen18c81b12010-08-17 17:25:57 +0800967 if (info->page_size == 2048)
eric miaofe69af02008-02-14 15:48:23 +0800968 this->ecc.layout = &hw_largepage_ecclayout;
969 else
970 this->ecc.layout = &hw_smallpage_ecclayout;
971
David Woodhousea1c06ee2008-04-22 20:39:43 +0100972 this->chip_delay = 25;
eric miaofe69af02008-02-14 15:48:23 +0800973}
974
Lei Wene353a202011-03-03 11:08:30 +0800975static
976struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +0800977{
Lei Wene353a202011-03-03 11:08:30 +0800978 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
eric miaofe69af02008-02-14 15:48:23 +0800979 struct pxa3xx_nand_info *info;
eric miaofe69af02008-02-14 15:48:23 +0800980 struct mtd_info *mtd;
981 struct resource *r;
Lei Wene353a202011-03-03 11:08:30 +0800982 int ret, irq;
eric miaofe69af02008-02-14 15:48:23 +0800983
984 mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
985 GFP_KERNEL);
David Woodhousea1c06ee2008-04-22 20:39:43 +0100986 if (!mtd) {
eric miaofe69af02008-02-14 15:48:23 +0800987 dev_err(&pdev->dev, "failed to allocate memory\n");
Lei Wene353a202011-03-03 11:08:30 +0800988 return NULL;
David Woodhousea1c06ee2008-04-22 20:39:43 +0100989 }
eric miaofe69af02008-02-14 15:48:23 +0800990
991 info = (struct pxa3xx_nand_info *)(&mtd[1]);
992 info->pdev = pdev;
993
eric miaofe69af02008-02-14 15:48:23 +0800994 mtd->priv = info;
Lei Wene353a202011-03-03 11:08:30 +0800995 info->mtd = mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +0200996 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +0800997
Russell Kinge0d8b132008-11-11 17:52:32 +0000998 info->clk = clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +0800999 if (IS_ERR(info->clk)) {
1000 dev_err(&pdev->dev, "failed to get nand clock\n");
1001 ret = PTR_ERR(info->clk);
1002 goto fail_free_mtd;
1003 }
1004 clk_enable(info->clk);
1005
1006 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1007 if (r == NULL) {
1008 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1009 ret = -ENXIO;
1010 goto fail_put_clk;
1011 }
1012 info->drcmr_dat = r->start;
1013
1014 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1015 if (r == NULL) {
1016 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1017 ret = -ENXIO;
1018 goto fail_put_clk;
1019 }
1020 info->drcmr_cmd = r->start;
1021
1022 irq = platform_get_irq(pdev, 0);
1023 if (irq < 0) {
1024 dev_err(&pdev->dev, "no IRQ resource defined\n");
1025 ret = -ENXIO;
1026 goto fail_put_clk;
1027 }
1028
1029 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1030 if (r == NULL) {
1031 dev_err(&pdev->dev, "no IO memory resource defined\n");
1032 ret = -ENODEV;
1033 goto fail_put_clk;
1034 }
1035
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001036 r = request_mem_region(r->start, resource_size(r), pdev->name);
eric miaofe69af02008-02-14 15:48:23 +08001037 if (r == NULL) {
1038 dev_err(&pdev->dev, "failed to request memory resource\n");
1039 ret = -EBUSY;
1040 goto fail_put_clk;
1041 }
1042
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001043 info->mmio_base = ioremap(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001044 if (info->mmio_base == NULL) {
1045 dev_err(&pdev->dev, "ioremap() failed\n");
1046 ret = -ENODEV;
1047 goto fail_free_res;
1048 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001049 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001050
1051 ret = pxa3xx_nand_init_buff(info);
1052 if (ret)
1053 goto fail_free_io;
1054
Haojian Zhuang346e1252009-09-10 14:27:23 +08001055 /* initialize all interrupts to be disabled */
1056 disable_int(info, NDSR_MASK);
1057
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001058 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1059 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001060 if (ret < 0) {
1061 dev_err(&pdev->dev, "failed to request IRQ\n");
1062 goto fail_free_buf;
1063 }
1064
Enrico Scholzc8ac3f82008-08-29 12:59:48 +02001065 ret = pxa3xx_nand_detect_flash(info, pdata);
eric miaofe69af02008-02-14 15:48:23 +08001066 if (ret) {
1067 dev_err(&pdev->dev, "failed to detect flash\n");
1068 ret = -ENODEV;
1069 goto fail_free_irq;
1070 }
1071
1072 pxa3xx_nand_init_mtd(mtd, info);
Lei Wene353a202011-03-03 11:08:30 +08001073 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001074
Lei Wene353a202011-03-03 11:08:30 +08001075 return info;
eric miaofe69af02008-02-14 15:48:23 +08001076
1077fail_free_irq:
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001078 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001079fail_free_buf:
1080 if (use_dma) {
1081 pxa_free_dma(info->data_dma_ch);
1082 dma_free_coherent(&pdev->dev, info->data_buff_size,
1083 info->data_buff, info->data_buff_phys);
1084 } else
1085 kfree(info->data_buff);
1086fail_free_io:
1087 iounmap(info->mmio_base);
1088fail_free_res:
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001089 release_mem_region(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001090fail_put_clk:
1091 clk_disable(info->clk);
1092 clk_put(info->clk);
1093fail_free_mtd:
1094 kfree(mtd);
Lei Wene353a202011-03-03 11:08:30 +08001095 return NULL;
eric miaofe69af02008-02-14 15:48:23 +08001096}
1097
1098static int pxa3xx_nand_remove(struct platform_device *pdev)
1099{
Lei Wene353a202011-03-03 11:08:30 +08001100 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1101 struct mtd_info *mtd = info->mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001102 struct resource *r;
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001103 int irq;
eric miaofe69af02008-02-14 15:48:23 +08001104
1105 platform_set_drvdata(pdev, NULL);
1106
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001107 irq = platform_get_irq(pdev, 0);
1108 if (irq >= 0)
1109 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_writecombine(&pdev->dev, info->data_buff_size,
1113 info->data_buff, info->data_buff_phys);
1114 } else
1115 kfree(info->data_buff);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001116
1117 iounmap(info->mmio_base);
1118 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1119 release_mem_region(r->start, resource_size(r));
1120
1121 clk_disable(info->clk);
1122 clk_put(info->clk);
1123
Lei Wenf8155a42011-02-28 10:32:11 +08001124 if (mtd) {
1125 del_mtd_device(mtd);
1126#ifdef CONFIG_MTD_PARTITIONS
1127 del_mtd_partitions(mtd);
1128#endif
1129 kfree(mtd);
1130 }
eric miaofe69af02008-02-14 15:48:23 +08001131 return 0;
1132}
1133
Lei Wene353a202011-03-03 11:08:30 +08001134static int pxa3xx_nand_probe(struct platform_device *pdev)
1135{
1136 struct pxa3xx_nand_platform_data *pdata;
1137 struct pxa3xx_nand_info *info;
1138
1139 pdata = pdev->dev.platform_data;
1140 if (!pdata) {
1141 dev_err(&pdev->dev, "no platform data defined\n");
1142 return -ENODEV;
1143 }
1144
1145 info = alloc_nand_resource(pdev);
1146 if (info == NULL)
1147 return -ENOMEM;
1148
1149 if (nand_scan(info->mtd, 1)) {
1150 dev_err(&pdev->dev, "failed to scan nand\n");
1151 pxa3xx_nand_remove(pdev);
1152 return -ENODEV;
1153 }
1154
1155#ifdef CONFIG_MTD_PARTITIONS
1156 if (mtd_has_cmdlinepart()) {
1157 const char *probes[] = { "cmdlinepart", NULL };
1158 struct mtd_partition *parts;
1159 int nr_parts;
1160
1161 nr_parts = parse_mtd_partitions(info->mtd, probes, &parts, 0);
1162
1163 if (nr_parts)
Lei Wenf8155a42011-02-28 10:32:11 +08001164 return add_mtd_partitions(info->mtd, parts, nr_parts);
Lei Wene353a202011-03-03 11:08:30 +08001165 }
1166
Lei Wenf8155a42011-02-28 10:32:11 +08001167 return add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
Lei Wene353a202011-03-03 11:08:30 +08001168#else
1169 return 0;
1170#endif
1171}
1172
eric miaofe69af02008-02-14 15:48:23 +08001173#ifdef CONFIG_PM
1174static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1175{
Lei Wene353a202011-03-03 11:08:30 +08001176 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1177 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001178
Lei Wenf8155a42011-02-28 10:32:11 +08001179 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001180 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1181 return -EAGAIN;
1182 }
1183
1184 return 0;
1185}
1186
1187static int pxa3xx_nand_resume(struct platform_device *pdev)
1188{
Lei Wene353a202011-03-03 11:08:30 +08001189 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1190 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001191
Lei Wen18c81b12010-08-17 17:25:57 +08001192 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1193 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
eric miaofe69af02008-02-14 15:48:23 +08001194 clk_enable(info->clk);
1195
Lei Wen18c81b12010-08-17 17:25:57 +08001196 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001197}
1198#else
1199#define pxa3xx_nand_suspend NULL
1200#define pxa3xx_nand_resume NULL
1201#endif
1202
1203static struct platform_driver pxa3xx_nand_driver = {
1204 .driver = {
1205 .name = "pxa3xx-nand",
1206 },
1207 .probe = pxa3xx_nand_probe,
1208 .remove = pxa3xx_nand_remove,
1209 .suspend = pxa3xx_nand_suspend,
1210 .resume = pxa3xx_nand_resume,
1211};
1212
1213static int __init pxa3xx_nand_init(void)
1214{
1215 return platform_driver_register(&pxa3xx_nand_driver);
1216}
1217module_init(pxa3xx_nand_init);
1218
1219static void __exit pxa3xx_nand_exit(void)
1220{
1221 platform_driver_unregister(&pxa3xx_nand_driver);
1222}
1223module_exit(pxa3xx_nand_exit);
1224
1225MODULE_LICENSE("GPL");
1226MODULE_DESCRIPTION("PXA3xx NAND controller driver");