blob: a085cd0a6e5ed31df8e624769cb394f650a1e7f2 [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>
eric miaofe69af02008-02-14 15:48:23 +080024
Eric Miaoafb5b5c2008-12-01 11:43:08 +080025#include <mach/dma.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/pxa3xx_nand.h>
eric miaofe69af02008-02-14 15:48:23 +080027
28#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
29
30/* registers and bit definitions */
31#define NDCR (0x00) /* Control register */
32#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
33#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
34#define NDSR (0x14) /* Status Register */
35#define NDPCR (0x18) /* Page Count Register */
36#define NDBDR0 (0x1C) /* Bad Block Register 0 */
37#define NDBDR1 (0x20) /* Bad Block Register 1 */
38#define NDDB (0x40) /* Data Buffer */
39#define NDCB0 (0x48) /* Command Buffer0 */
40#define NDCB1 (0x4C) /* Command Buffer1 */
41#define NDCB2 (0x50) /* Command Buffer2 */
42
43#define NDCR_SPARE_EN (0x1 << 31)
44#define NDCR_ECC_EN (0x1 << 30)
45#define NDCR_DMA_EN (0x1 << 29)
46#define NDCR_ND_RUN (0x1 << 28)
47#define NDCR_DWIDTH_C (0x1 << 27)
48#define NDCR_DWIDTH_M (0x1 << 26)
49#define NDCR_PAGE_SZ (0x1 << 24)
50#define NDCR_NCSX (0x1 << 23)
51#define NDCR_ND_MODE (0x3 << 21)
52#define NDCR_NAND_MODE (0x0)
53#define NDCR_CLR_PG_CNT (0x1 << 20)
54#define NDCR_CLR_ECC (0x1 << 19)
55#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
56#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
57
58#define NDCR_RA_START (0x1 << 15)
59#define NDCR_PG_PER_BLK (0x1 << 14)
60#define NDCR_ND_ARB_EN (0x1 << 12)
61
62#define NDSR_MASK (0xfff)
63#define NDSR_RDY (0x1 << 11)
64#define NDSR_CS0_PAGED (0x1 << 10)
65#define NDSR_CS1_PAGED (0x1 << 9)
66#define NDSR_CS0_CMDD (0x1 << 8)
67#define NDSR_CS1_CMDD (0x1 << 7)
68#define NDSR_CS0_BBD (0x1 << 6)
69#define NDSR_CS1_BBD (0x1 << 5)
70#define NDSR_DBERR (0x1 << 4)
71#define NDSR_SBERR (0x1 << 3)
72#define NDSR_WRDREQ (0x1 << 2)
73#define NDSR_RDDREQ (0x1 << 1)
74#define NDSR_WRCMDREQ (0x1)
75
76#define NDCB0_AUTO_RS (0x1 << 25)
77#define NDCB0_CSEL (0x1 << 24)
78#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
79#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
80#define NDCB0_NC (0x1 << 20)
81#define NDCB0_DBC (0x1 << 19)
82#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
83#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
84#define NDCB0_CMD2_MASK (0xff << 8)
85#define NDCB0_CMD1_MASK (0xff)
86#define NDCB0_ADDR_CYC_SHIFT (16)
87
88/* dma-able I/O address for the NAND data and commands */
89#define NDCB0_DMA_ADDR (0x43100048)
90#define NDDB_DMA_ADDR (0x43100040)
91
92/* macros for registers read/write */
93#define nand_writel(info, off, val) \
94 __raw_writel((val), (info)->mmio_base + (off))
95
96#define nand_readl(info, off) \
97 __raw_readl((info)->mmio_base + (off))
98
99/* error code and state */
100enum {
101 ERR_NONE = 0,
102 ERR_DMABUSERR = -1,
103 ERR_SENDCMD = -2,
104 ERR_DBERR = -3,
105 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300106 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800107};
108
109enum {
110 STATE_READY = 0,
111 STATE_CMD_HANDLE,
112 STATE_DMA_READING,
113 STATE_DMA_WRITING,
114 STATE_DMA_DONE,
115 STATE_PIO_READING,
116 STATE_PIO_WRITING,
117};
118
eric miaofe69af02008-02-14 15:48:23 +0800119struct pxa3xx_nand_info {
120 struct nand_chip nand_chip;
121
122 struct platform_device *pdev;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200123 const struct pxa3xx_nand_flash *flash_info;
eric miaofe69af02008-02-14 15:48:23 +0800124
125 struct clk *clk;
126 void __iomem *mmio_base;
127
128 unsigned int buf_start;
129 unsigned int buf_count;
130
131 /* DMA information */
132 int drcmr_dat;
133 int drcmr_cmd;
134
135 unsigned char *data_buff;
136 dma_addr_t data_buff_phys;
137 size_t data_buff_size;
138 int data_dma_ch;
139 struct pxa_dma_desc *data_desc;
140 dma_addr_t data_desc_addr;
141
142 uint32_t reg_ndcr;
143
144 /* saved column/page_addr during CMD_SEQIN */
145 int seqin_column;
146 int seqin_page_addr;
147
148 /* relate to the command */
149 unsigned int state;
150
151 int use_ecc; /* use HW ECC ? */
152 int use_dma; /* use DMA ? */
153
154 size_t data_size; /* data size in FIFO */
155 int retcode;
156 struct completion cmd_complete;
157
158 /* generated NDCBx register values */
159 uint32_t ndcb0;
160 uint32_t ndcb1;
161 uint32_t ndcb2;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200162
163 /* calculated from pxa3xx_nand_flash data */
164 size_t oob_size;
165 size_t read_id_bytes;
166
167 unsigned int col_addr_cycles;
168 unsigned int row_addr_cycles;
eric miaofe69af02008-02-14 15:48:23 +0800169};
170
171static int use_dma = 1;
172module_param(use_dma, bool, 0444);
173MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
174
Mike Rapoportf2710492009-02-17 13:54:47 +0200175/*
176 * Default NAND flash controller configuration setup by the
177 * bootloader. This configuration is used only when pdata->keep_config is set
178 */
179static struct pxa3xx_nand_timing default_timing;
180static struct pxa3xx_nand_flash default_flash;
181
eric miaofe69af02008-02-14 15:48:23 +0800182static struct pxa3xx_nand_cmdset smallpage_cmdset = {
183 .read1 = 0x0000,
184 .read2 = 0x0050,
185 .program = 0x1080,
186 .read_status = 0x0070,
187 .read_id = 0x0090,
188 .erase = 0xD060,
189 .reset = 0x00FF,
190 .lock = 0x002A,
191 .unlock = 0x2423,
192 .lock_status = 0x007A,
193};
194
195static struct pxa3xx_nand_cmdset largepage_cmdset = {
196 .read1 = 0x3000,
197 .read2 = 0x0050,
198 .program = 0x1080,
199 .read_status = 0x0070,
200 .read_id = 0x0090,
201 .erase = 0xD060,
202 .reset = 0x00FF,
203 .lock = 0x002A,
204 .unlock = 0x2423,
205 .lock_status = 0x007A,
206};
207
Mike Rapoportf2710492009-02-17 13:54:47 +0200208#ifdef CONFIG_MTD_NAND_PXA3xx_BUILTIN
eric miaofe69af02008-02-14 15:48:23 +0800209static struct pxa3xx_nand_timing samsung512MbX16_timing = {
210 .tCH = 10,
211 .tCS = 0,
212 .tWH = 20,
213 .tWP = 40,
214 .tRH = 30,
215 .tRP = 40,
216 .tR = 11123,
217 .tWHR = 110,
218 .tAR = 10,
219};
220
221static struct pxa3xx_nand_flash samsung512MbX16 = {
222 .timing = &samsung512MbX16_timing,
223 .cmdset = &smallpage_cmdset,
224 .page_per_block = 32,
225 .page_size = 512,
226 .flash_width = 16,
227 .dfc_width = 16,
228 .num_blocks = 4096,
229 .chip_id = 0x46ec,
230};
231
232static struct pxa3xx_nand_timing micron_timing = {
233 .tCH = 10,
234 .tCS = 25,
235 .tWH = 15,
236 .tWP = 25,
237 .tRH = 15,
Haojian Zhuang726de6e2009-10-14 15:47:01 +0800238 .tRP = 30,
eric miaofe69af02008-02-14 15:48:23 +0800239 .tR = 25000,
240 .tWHR = 60,
241 .tAR = 10,
242};
243
244static struct pxa3xx_nand_flash micron1GbX8 = {
245 .timing = &micron_timing,
246 .cmdset = &largepage_cmdset,
247 .page_per_block = 64,
248 .page_size = 2048,
249 .flash_width = 8,
250 .dfc_width = 8,
251 .num_blocks = 1024,
252 .chip_id = 0xa12c,
253};
254
255static struct pxa3xx_nand_flash micron1GbX16 = {
256 .timing = &micron_timing,
257 .cmdset = &largepage_cmdset,
258 .page_per_block = 64,
259 .page_size = 2048,
260 .flash_width = 16,
261 .dfc_width = 16,
262 .num_blocks = 1024,
263 .chip_id = 0xb12c,
264};
265
Semun Lee4262bd22008-09-01 11:49:27 +0100266static struct pxa3xx_nand_timing stm2GbX16_timing = {
267 .tCH = 10,
268 .tCS = 35,
269 .tWH = 15,
270 .tWP = 25,
271 .tRH = 15,
272 .tRP = 25,
273 .tR = 25000,
274 .tWHR = 60,
275 .tAR = 10,
276};
277
278static struct pxa3xx_nand_flash stm2GbX16 = {
279 .timing = &stm2GbX16_timing,
Denis V. Luneve93f1be2008-12-03 10:47:20 +0000280 .cmdset = &largepage_cmdset,
Semun Lee4262bd22008-09-01 11:49:27 +0100281 .page_per_block = 64,
282 .page_size = 2048,
283 .flash_width = 16,
284 .dfc_width = 16,
285 .num_blocks = 2048,
286 .chip_id = 0xba20,
287};
288
eric miaofe69af02008-02-14 15:48:23 +0800289static struct pxa3xx_nand_flash *builtin_flash_types[] = {
290 &samsung512MbX16,
291 &micron1GbX8,
292 &micron1GbX16,
Semun Lee4262bd22008-09-01 11:49:27 +0100293 &stm2GbX16,
eric miaofe69af02008-02-14 15:48:23 +0800294};
Enrico Scholz80ebf202008-08-29 12:59:49 +0200295#endif /* CONFIG_MTD_NAND_PXA3xx_BUILTIN */
eric miaofe69af02008-02-14 15:48:23 +0800296
297#define NDTR0_tCH(c) (min((c), 7) << 19)
298#define NDTR0_tCS(c) (min((c), 7) << 16)
299#define NDTR0_tWH(c) (min((c), 7) << 11)
300#define NDTR0_tWP(c) (min((c), 7) << 8)
301#define NDTR0_tRH(c) (min((c), 7) << 3)
302#define NDTR0_tRP(c) (min((c), 7) << 0)
303
304#define NDTR1_tR(c) (min((c), 65535) << 16)
305#define NDTR1_tWHR(c) (min((c), 15) << 4)
306#define NDTR1_tAR(c) (min((c), 15) << 0)
307
Mike Rapoportf2710492009-02-17 13:54:47 +0200308#define tCH_NDTR0(r) (((r) >> 19) & 0x7)
309#define tCS_NDTR0(r) (((r) >> 16) & 0x7)
310#define tWH_NDTR0(r) (((r) >> 11) & 0x7)
311#define tWP_NDTR0(r) (((r) >> 8) & 0x7)
312#define tRH_NDTR0(r) (((r) >> 3) & 0x7)
313#define tRP_NDTR0(r) (((r) >> 0) & 0x7)
314
315#define tR_NDTR1(r) (((r) >> 16) & 0xffff)
316#define tWHR_NDTR1(r) (((r) >> 4) & 0xf)
317#define tAR_NDTR1(r) (((r) >> 0) & 0xf)
318
eric miaofe69af02008-02-14 15:48:23 +0800319/* convert nano-seconds to nand flash controller clock cycles */
Matt Reimer5b0d4d72008-11-18 10:54:32 -0800320#define ns2cycle(ns, clk) (int)(((ns) * (clk / 1000000) / 1000) - 1)
eric miaofe69af02008-02-14 15:48:23 +0800321
Mike Rapoportf2710492009-02-17 13:54:47 +0200322/* convert nand flash controller clock cycles to nano-seconds */
323#define cycle2ns(c, clk) ((((c) + 1) * 1000000 + clk / 500) / (clk / 1000))
324
eric miaofe69af02008-02-14 15:48:23 +0800325static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200326 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800327{
328 unsigned long nand_clk = clk_get_rate(info->clk);
329 uint32_t ndtr0, ndtr1;
330
331 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
332 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
333 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
334 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
335 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
336 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
337
338 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
339 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
340 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
341
342 nand_writel(info, NDTR0CS0, ndtr0);
343 nand_writel(info, NDTR1CS0, ndtr1);
344}
345
346#define WAIT_EVENT_TIMEOUT 10
347
348static int wait_for_event(struct pxa3xx_nand_info *info, uint32_t event)
349{
350 int timeout = WAIT_EVENT_TIMEOUT;
351 uint32_t ndsr;
352
353 while (timeout--) {
354 ndsr = nand_readl(info, NDSR) & NDSR_MASK;
355 if (ndsr & event) {
356 nand_writel(info, NDSR, ndsr);
357 return 0;
358 }
359 udelay(10);
360 }
361
362 return -ETIMEDOUT;
363}
364
365static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
366 uint16_t cmd, int column, int page_addr)
367{
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200368 const struct pxa3xx_nand_flash *f = info->flash_info;
Enrico Scholz7dad4822008-08-29 12:59:50 +0200369 const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800370
371 /* calculate data size */
372 switch (f->page_size) {
373 case 2048:
374 info->data_size = (info->use_ecc) ? 2088 : 2112;
375 break;
376 case 512:
377 info->data_size = (info->use_ecc) ? 520 : 528;
378 break;
379 default:
380 return -EINVAL;
381 }
382
383 /* generate values for NDCBx registers */
384 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
385 info->ndcb1 = 0;
386 info->ndcb2 = 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200387 info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
eric miaofe69af02008-02-14 15:48:23 +0800388
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200389 if (info->col_addr_cycles == 2) {
eric miaofe69af02008-02-14 15:48:23 +0800390 /* large block, 2 cycles for column address
391 * row address starts from 3rd cycle
392 */
Matt Reimer7f9938d2008-11-18 10:47:42 -0800393 info->ndcb1 |= page_addr << 16;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200394 if (info->row_addr_cycles == 3)
eric miaofe69af02008-02-14 15:48:23 +0800395 info->ndcb2 = (page_addr >> 16) & 0xff;
396 } else
397 /* small block, 1 cycles for column address
398 * row address starts from 2nd cycle
399 */
Matt Reimer7f9938d2008-11-18 10:47:42 -0800400 info->ndcb1 = page_addr << 8;
eric miaofe69af02008-02-14 15:48:23 +0800401
402 if (cmd == cmdset->program)
403 info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
404
405 return 0;
406}
407
408static int prepare_erase_cmd(struct pxa3xx_nand_info *info,
409 uint16_t cmd, int page_addr)
410{
411 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
412 info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
413 info->ndcb1 = page_addr;
414 info->ndcb2 = 0;
415 return 0;
416}
417
418static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
419{
Enrico Scholz7dad4822008-08-29 12:59:50 +0200420 const struct pxa3xx_nand_cmdset *cmdset = info->flash_info->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800421
422 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
423 info->ndcb1 = 0;
424 info->ndcb2 = 0;
425
426 if (cmd == cmdset->read_id) {
427 info->ndcb0 |= NDCB0_CMD_TYPE(3);
428 info->data_size = 8;
429 } else if (cmd == cmdset->read_status) {
430 info->ndcb0 |= NDCB0_CMD_TYPE(4);
431 info->data_size = 8;
432 } else if (cmd == cmdset->reset || cmd == cmdset->lock ||
433 cmd == cmdset->unlock) {
434 info->ndcb0 |= NDCB0_CMD_TYPE(5);
435 } else
436 return -EINVAL;
437
438 return 0;
439}
440
441static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
442{
443 uint32_t ndcr;
444
445 ndcr = nand_readl(info, NDCR);
446 nand_writel(info, NDCR, ndcr & ~int_mask);
447}
448
449static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
450{
451 uint32_t ndcr;
452
453 ndcr = nand_readl(info, NDCR);
454 nand_writel(info, NDCR, ndcr | int_mask);
455}
456
457/* NOTE: it is a must to set ND_RUN firstly, then write command buffer
458 * otherwise, it does not work
459 */
460static int write_cmd(struct pxa3xx_nand_info *info)
461{
462 uint32_t ndcr;
463
464 /* clear status bits and run */
465 nand_writel(info, NDSR, NDSR_MASK);
466
467 ndcr = info->reg_ndcr;
468
469 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
470 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
471 ndcr |= NDCR_ND_RUN;
472
473 nand_writel(info, NDCR, ndcr);
474
475 if (wait_for_event(info, NDSR_WRCMDREQ)) {
476 printk(KERN_ERR "timed out writing command\n");
477 return -ETIMEDOUT;
478 }
479
480 nand_writel(info, NDCB0, info->ndcb0);
481 nand_writel(info, NDCB0, info->ndcb1);
482 nand_writel(info, NDCB0, info->ndcb2);
483 return 0;
484}
485
486static int handle_data_pio(struct pxa3xx_nand_info *info)
487{
488 int ret, timeout = CHIP_DELAY_TIMEOUT;
489
490 switch (info->state) {
491 case STATE_PIO_WRITING:
492 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800493 DIV_ROUND_UP(info->data_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800494
495 enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
496
497 ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
498 if (!ret) {
499 printk(KERN_ERR "program command time out\n");
500 return -1;
501 }
502 break;
503 case STATE_PIO_READING:
504 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800505 DIV_ROUND_UP(info->data_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800506 break;
507 default:
David Woodhousea1c06ee2008-04-22 20:39:43 +0100508 printk(KERN_ERR "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800509 info->state);
510 return -EINVAL;
511 }
512
513 info->state = STATE_READY;
514 return 0;
515}
516
517static void start_data_dma(struct pxa3xx_nand_info *info, int dir_out)
518{
519 struct pxa_dma_desc *desc = info->data_desc;
520 int dma_len = ALIGN(info->data_size, 32);
521
522 desc->ddadr = DDADR_STOP;
523 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
524
525 if (dir_out) {
526 desc->dsadr = info->data_buff_phys;
527 desc->dtadr = NDDB_DMA_ADDR;
528 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
529 } else {
530 desc->dtadr = info->data_buff_phys;
531 desc->dsadr = NDDB_DMA_ADDR;
532 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
533 }
534
535 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
536 DDADR(info->data_dma_ch) = info->data_desc_addr;
537 DCSR(info->data_dma_ch) |= DCSR_RUN;
538}
539
540static void pxa3xx_nand_data_dma_irq(int channel, void *data)
541{
542 struct pxa3xx_nand_info *info = data;
543 uint32_t dcsr;
544
545 dcsr = DCSR(channel);
546 DCSR(channel) = dcsr;
547
548 if (dcsr & DCSR_BUSERR) {
549 info->retcode = ERR_DMABUSERR;
550 complete(&info->cmd_complete);
551 }
552
553 if (info->state == STATE_DMA_WRITING) {
554 info->state = STATE_DMA_DONE;
555 enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
556 } else {
557 info->state = STATE_READY;
558 complete(&info->cmd_complete);
559 }
560}
561
562static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
563{
564 struct pxa3xx_nand_info *info = devid;
565 unsigned int status;
566
567 status = nand_readl(info, NDSR);
568
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300569 if (status & (NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR)) {
eric miaofe69af02008-02-14 15:48:23 +0800570 if (status & NDSR_DBERR)
571 info->retcode = ERR_DBERR;
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300572 else if (status & NDSR_SBERR)
573 info->retcode = ERR_SBERR;
eric miaofe69af02008-02-14 15:48:23 +0800574
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300575 disable_int(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
eric miaofe69af02008-02-14 15:48:23 +0800576
577 if (info->use_dma) {
578 info->state = STATE_DMA_READING;
579 start_data_dma(info, 0);
580 } else {
581 info->state = STATE_PIO_READING;
582 complete(&info->cmd_complete);
583 }
584 } else if (status & NDSR_WRDREQ) {
585 disable_int(info, NDSR_WRDREQ);
586 if (info->use_dma) {
587 info->state = STATE_DMA_WRITING;
588 start_data_dma(info, 1);
589 } else {
590 info->state = STATE_PIO_WRITING;
591 complete(&info->cmd_complete);
592 }
593 } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
594 if (status & NDSR_CS0_BBD)
595 info->retcode = ERR_BBERR;
596
597 disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
598 info->state = STATE_READY;
599 complete(&info->cmd_complete);
600 }
601 nand_writel(info, NDSR, status);
602 return IRQ_HANDLED;
603}
604
605static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
606{
607 uint32_t ndcr;
608 int ret, timeout = CHIP_DELAY_TIMEOUT;
609
610 if (write_cmd(info)) {
611 info->retcode = ERR_SENDCMD;
612 goto fail_stop;
613 }
614
615 info->state = STATE_CMD_HANDLE;
616
617 enable_int(info, event);
618
619 ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
620 if (!ret) {
621 printk(KERN_ERR "command execution timed out\n");
622 info->retcode = ERR_SENDCMD;
623 goto fail_stop;
624 }
625
626 if (info->use_dma == 0 && info->data_size > 0)
627 if (handle_data_pio(info))
628 goto fail_stop;
629
630 return 0;
631
632fail_stop:
633 ndcr = nand_readl(info, NDCR);
634 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
635 udelay(10);
636 return -ETIMEDOUT;
637}
638
639static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
640{
641 struct pxa3xx_nand_info *info = mtd->priv;
642 return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
643}
644
645static inline int is_buf_blank(uint8_t *buf, size_t len)
646{
647 for (; len > 0; len--)
648 if (*buf++ != 0xff)
649 return 0;
650 return 1;
651}
652
653static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100654 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800655{
656 struct pxa3xx_nand_info *info = mtd->priv;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200657 const struct pxa3xx_nand_flash *flash_info = info->flash_info;
Enrico Scholz7dad4822008-08-29 12:59:50 +0200658 const struct pxa3xx_nand_cmdset *cmdset = flash_info->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800659 int ret;
660
661 info->use_dma = (use_dma) ? 1 : 0;
662 info->use_ecc = 0;
663 info->data_size = 0;
664 info->state = STATE_READY;
665
666 init_completion(&info->cmd_complete);
667
668 switch (command) {
669 case NAND_CMD_READOOB:
670 /* disable HW ECC to get all the OOB data */
671 info->buf_count = mtd->writesize + mtd->oobsize;
672 info->buf_start = mtd->writesize + column;
Haojian Zhuang7ce33af2009-09-14 20:21:01 +0800673 memset(info->data_buff, 0xFF, info->buf_count);
eric miaofe69af02008-02-14 15:48:23 +0800674
675 if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
676 break;
677
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300678 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
eric miaofe69af02008-02-14 15:48:23 +0800679
680 /* We only are OOB, so if the data has error, does not matter */
681 if (info->retcode == ERR_DBERR)
682 info->retcode = ERR_NONE;
683 break;
684
685 case NAND_CMD_READ0:
686 info->use_ecc = 1;
687 info->retcode = ERR_NONE;
688 info->buf_start = column;
689 info->buf_count = mtd->writesize + mtd->oobsize;
690 memset(info->data_buff, 0xFF, info->buf_count);
691
692 if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
693 break;
694
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300695 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
eric miaofe69af02008-02-14 15:48:23 +0800696
697 if (info->retcode == ERR_DBERR) {
698 /* for blank page (all 0xff), HW will calculate its ECC as
699 * 0, which is different from the ECC information within
700 * OOB, ignore such double bit errors
701 */
702 if (is_buf_blank(info->data_buff, mtd->writesize))
703 info->retcode = ERR_NONE;
704 }
705 break;
706 case NAND_CMD_SEQIN:
707 info->buf_start = column;
708 info->buf_count = mtd->writesize + mtd->oobsize;
709 memset(info->data_buff, 0xff, info->buf_count);
710
711 /* save column/page_addr for next CMD_PAGEPROG */
712 info->seqin_column = column;
713 info->seqin_page_addr = page_addr;
714 break;
715 case NAND_CMD_PAGEPROG:
716 info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
717
718 if (prepare_read_prog_cmd(info, cmdset->program,
719 info->seqin_column, info->seqin_page_addr))
720 break;
721
722 pxa3xx_nand_do_cmd(info, NDSR_WRDREQ);
723 break;
724 case NAND_CMD_ERASE1:
725 if (prepare_erase_cmd(info, cmdset->erase, page_addr))
726 break;
727
728 pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
729 break;
730 case NAND_CMD_ERASE2:
731 break;
732 case NAND_CMD_READID:
733 case NAND_CMD_STATUS:
734 info->use_dma = 0; /* force PIO read */
735 info->buf_start = 0;
736 info->buf_count = (command == NAND_CMD_READID) ?
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200737 info->read_id_bytes : 1;
eric miaofe69af02008-02-14 15:48:23 +0800738
739 if (prepare_other_cmd(info, (command == NAND_CMD_READID) ?
740 cmdset->read_id : cmdset->read_status))
741 break;
742
743 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ);
744 break;
745 case NAND_CMD_RESET:
746 if (prepare_other_cmd(info, cmdset->reset))
747 break;
748
749 ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD);
750 if (ret == 0) {
751 int timeout = 2;
752 uint32_t ndcr;
753
754 while (timeout--) {
755 if (nand_readl(info, NDSR) & NDSR_RDY)
756 break;
757 msleep(10);
758 }
759
760 ndcr = nand_readl(info, NDCR);
761 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
762 }
763 break;
764 default:
765 printk(KERN_ERR "non-supported command.\n");
766 break;
767 }
768
769 if (info->retcode == ERR_DBERR) {
770 printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
771 info->retcode = ERR_NONE;
772 }
773}
774
775static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
776{
777 struct pxa3xx_nand_info *info = mtd->priv;
778 char retval = 0xFF;
779
780 if (info->buf_start < info->buf_count)
781 /* Has just send a new command? */
782 retval = info->data_buff[info->buf_start++];
783
784 return retval;
785}
786
787static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
788{
789 struct pxa3xx_nand_info *info = mtd->priv;
790 u16 retval = 0xFFFF;
791
792 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
793 retval = *((u16 *)(info->data_buff+info->buf_start));
794 info->buf_start += 2;
795 }
796 return retval;
797}
798
799static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
800{
801 struct pxa3xx_nand_info *info = mtd->priv;
802 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
803
804 memcpy(buf, info->data_buff + info->buf_start, real_len);
805 info->buf_start += real_len;
806}
807
808static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
809 const uint8_t *buf, int len)
810{
811 struct pxa3xx_nand_info *info = mtd->priv;
812 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
813
814 memcpy(info->data_buff + info->buf_start, buf, real_len);
815 info->buf_start += real_len;
816}
817
818static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
819 const uint8_t *buf, int len)
820{
821 return 0;
822}
823
824static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
825{
826 return;
827}
828
829static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
830{
831 struct pxa3xx_nand_info *info = mtd->priv;
832
833 /* pxa3xx_nand_send_command has waited for command complete */
834 if (this->state == FL_WRITING || this->state == FL_ERASING) {
835 if (info->retcode == ERR_NONE)
836 return 0;
837 else {
838 /*
839 * any error make it return 0x01 which will tell
840 * the caller the erase and write fail
841 */
842 return 0x01;
843 }
844 }
845
846 return 0;
847}
848
849static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
850{
851 return;
852}
853
854static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
855 const uint8_t *dat, uint8_t *ecc_code)
856{
857 return 0;
858}
859
860static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
861 uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
862{
863 struct pxa3xx_nand_info *info = mtd->priv;
864 /*
865 * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
866 * consider it as a ecc error which will tell the caller the
867 * read fail We have distinguish all the errors, but the
868 * nand_read_ecc only check this function return value
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300869 *
870 * Corrected (single-bit) errors must also be noted.
eric miaofe69af02008-02-14 15:48:23 +0800871 */
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300872 if (info->retcode == ERR_SBERR)
873 return 1;
874 else if (info->retcode != ERR_NONE)
eric miaofe69af02008-02-14 15:48:23 +0800875 return -1;
876
877 return 0;
878}
879
880static int __readid(struct pxa3xx_nand_info *info, uint32_t *id)
881{
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200882 const struct pxa3xx_nand_flash *f = info->flash_info;
Enrico Scholz7dad4822008-08-29 12:59:50 +0200883 const struct pxa3xx_nand_cmdset *cmdset = f->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800884 uint32_t ndcr;
885 uint8_t id_buff[8];
886
887 if (prepare_other_cmd(info, cmdset->read_id)) {
888 printk(KERN_ERR "failed to prepare command\n");
889 return -EINVAL;
890 }
891
892 /* Send command */
893 if (write_cmd(info))
894 goto fail_timeout;
895
896 /* Wait for CMDDM(command done successfully) */
897 if (wait_for_event(info, NDSR_RDDREQ))
898 goto fail_timeout;
899
900 __raw_readsl(info->mmio_base + NDDB, id_buff, 2);
901 *id = id_buff[0] | (id_buff[1] << 8);
902 return 0;
903
904fail_timeout:
905 ndcr = nand_readl(info, NDCR);
906 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
907 udelay(10);
908 return -ETIMEDOUT;
909}
910
911static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200912 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800913{
914 struct platform_device *pdev = info->pdev;
915 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
916 uint32_t ndcr = 0x00000FFF; /* disable all interrupts */
917
918 if (f->page_size != 2048 && f->page_size != 512)
919 return -EINVAL;
920
921 if (f->flash_width != 16 && f->flash_width != 8)
922 return -EINVAL;
923
924 /* calculate flash information */
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200925 info->oob_size = (f->page_size == 2048) ? 64 : 16;
926 info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800927
928 /* calculate addressing information */
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200929 info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800930
931 if (f->num_blocks * f->page_per_block > 65536)
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200932 info->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800933 else
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200934 info->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800935
936 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200937 ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800938 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
939 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
940 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
941 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
942
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200943 ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800944 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
945
946 info->reg_ndcr = ndcr;
947
948 pxa3xx_nand_set_timing(info, f->timing);
949 info->flash_info = f;
950 return 0;
951}
952
Mike Rapoportf2710492009-02-17 13:54:47 +0200953static void pxa3xx_nand_detect_timing(struct pxa3xx_nand_info *info,
954 struct pxa3xx_nand_timing *t)
955{
956 unsigned long nand_clk = clk_get_rate(info->clk);
957 uint32_t ndtr0 = nand_readl(info, NDTR0CS0);
958 uint32_t ndtr1 = nand_readl(info, NDTR1CS0);
959
960 t->tCH = cycle2ns(tCH_NDTR0(ndtr0), nand_clk);
961 t->tCS = cycle2ns(tCS_NDTR0(ndtr0), nand_clk);
962 t->tWH = cycle2ns(tWH_NDTR0(ndtr0), nand_clk);
963 t->tWP = cycle2ns(tWP_NDTR0(ndtr0), nand_clk);
964 t->tRH = cycle2ns(tRH_NDTR0(ndtr0), nand_clk);
965 t->tRP = cycle2ns(tRP_NDTR0(ndtr0), nand_clk);
966
967 t->tR = cycle2ns(tR_NDTR1(ndtr1), nand_clk);
968 t->tWHR = cycle2ns(tWHR_NDTR1(ndtr1), nand_clk);
969 t->tAR = cycle2ns(tAR_NDTR1(ndtr1), nand_clk);
970}
971
972static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
973{
974 uint32_t ndcr = nand_readl(info, NDCR);
975 struct nand_flash_dev *type = NULL;
976 uint32_t id = -1;
977 int i;
978
979 default_flash.page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
980 default_flash.page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
981 default_flash.flash_width = ndcr & NDCR_DWIDTH_M ? 16 : 8;
982 default_flash.dfc_width = ndcr & NDCR_DWIDTH_C ? 16 : 8;
983
984 if (default_flash.page_size == 2048)
985 default_flash.cmdset = &largepage_cmdset;
986 else
987 default_flash.cmdset = &smallpage_cmdset;
988
989 /* set info fields needed to __readid */
990 info->flash_info = &default_flash;
991 info->read_id_bytes = (default_flash.page_size == 2048) ? 4 : 2;
992 info->reg_ndcr = ndcr;
993
994 if (__readid(info, &id))
995 return -ENODEV;
996
997 /* Lookup the flash id */
998 id = (id >> 8) & 0xff; /* device id is byte 2 */
999 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
1000 if (id == nand_flash_ids[i].id) {
1001 type = &nand_flash_ids[i];
1002 break;
1003 }
1004 }
1005
1006 if (!type)
1007 return -ENODEV;
1008
1009 /* fill the missing flash information */
1010 i = __ffs(default_flash.page_per_block * default_flash.page_size);
1011 default_flash.num_blocks = type->chipsize << (20 - i);
1012
1013 info->oob_size = (default_flash.page_size == 2048) ? 64 : 16;
1014
1015 /* calculate addressing information */
1016 info->col_addr_cycles = (default_flash.page_size == 2048) ? 2 : 1;
1017
1018 if (default_flash.num_blocks * default_flash.page_per_block > 65536)
1019 info->row_addr_cycles = 3;
1020 else
1021 info->row_addr_cycles = 2;
1022
1023 pxa3xx_nand_detect_timing(info, &default_timing);
1024 default_flash.timing = &default_timing;
1025
1026 return 0;
1027}
1028
Enrico Scholzc8ac3f82008-08-29 12:59:48 +02001029static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
1030 const struct pxa3xx_nand_platform_data *pdata)
eric miaofe69af02008-02-14 15:48:23 +08001031{
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001032 const struct pxa3xx_nand_flash *f;
Enrico Scholz2675e942008-08-29 12:59:52 +02001033 uint32_t id = -1;
eric miaofe69af02008-02-14 15:48:23 +08001034 int i;
1035
Mike Rapoportf2710492009-02-17 13:54:47 +02001036 if (pdata->keep_config)
1037 if (pxa3xx_nand_detect_config(info) == 0)
1038 return 0;
1039
Enrico Scholzc8ac3f82008-08-29 12:59:48 +02001040 for (i = 0; i<pdata->num_flash; ++i) {
1041 f = pdata->flash + i;
1042
1043 if (pxa3xx_nand_config_flash(info, f))
1044 continue;
1045
1046 if (__readid(info, &id))
1047 continue;
1048
1049 if (id == f->chip_id)
1050 return 0;
1051 }
1052
Enrico Scholz80ebf202008-08-29 12:59:49 +02001053#ifdef CONFIG_MTD_NAND_PXA3xx_BUILTIN
eric miaofe69af02008-02-14 15:48:23 +08001054 for (i = 0; i < ARRAY_SIZE(builtin_flash_types); i++) {
1055
1056 f = builtin_flash_types[i];
1057
1058 if (pxa3xx_nand_config_flash(info, f))
1059 continue;
1060
1061 if (__readid(info, &id))
1062 continue;
1063
1064 if (id == f->chip_id)
1065 return 0;
1066 }
Enrico Scholz80ebf202008-08-29 12:59:49 +02001067#endif
eric miaofe69af02008-02-14 15:48:23 +08001068
Enrico Scholz2675e942008-08-29 12:59:52 +02001069 dev_warn(&info->pdev->dev,
1070 "failed to detect configured nand flash; found %04x instead of\n",
1071 id);
eric miaofe69af02008-02-14 15:48:23 +08001072 return -ENODEV;
1073}
1074
1075/* the maximum possible buffer size for large page with OOB data
1076 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
1077 * data buffer and the DMA descriptor
1078 */
1079#define MAX_BUFF_SIZE PAGE_SIZE
1080
1081static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1082{
1083 struct platform_device *pdev = info->pdev;
1084 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
1085
1086 if (use_dma == 0) {
1087 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
1088 if (info->data_buff == NULL)
1089 return -ENOMEM;
1090 return 0;
1091 }
1092
1093 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
1094 &info->data_buff_phys, GFP_KERNEL);
1095 if (info->data_buff == NULL) {
1096 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1097 return -ENOMEM;
1098 }
1099
1100 info->data_buff_size = MAX_BUFF_SIZE;
1101 info->data_desc = (void *)info->data_buff + data_desc_offset;
1102 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1103
1104 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1105 pxa3xx_nand_data_dma_irq, info);
1106 if (info->data_dma_ch < 0) {
1107 dev_err(&pdev->dev, "failed to request data dma\n");
1108 dma_free_coherent(&pdev->dev, info->data_buff_size,
1109 info->data_buff, info->data_buff_phys);
1110 return info->data_dma_ch;
1111 }
1112
1113 return 0;
1114}
1115
1116static struct nand_ecclayout hw_smallpage_ecclayout = {
1117 .eccbytes = 6,
1118 .eccpos = {8, 9, 10, 11, 12, 13 },
1119 .oobfree = { {2, 6} }
1120};
1121
1122static struct nand_ecclayout hw_largepage_ecclayout = {
1123 .eccbytes = 24,
1124 .eccpos = {
1125 40, 41, 42, 43, 44, 45, 46, 47,
1126 48, 49, 50, 51, 52, 53, 54, 55,
1127 56, 57, 58, 59, 60, 61, 62, 63},
1128 .oobfree = { {2, 38} }
1129};
1130
1131static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
1132 struct pxa3xx_nand_info *info)
1133{
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001134 const struct pxa3xx_nand_flash *f = info->flash_info;
eric miaofe69af02008-02-14 15:48:23 +08001135 struct nand_chip *this = &info->nand_chip;
1136
1137 this->options = (f->flash_width == 16) ? NAND_BUSWIDTH_16: 0;
1138
1139 this->waitfunc = pxa3xx_nand_waitfunc;
1140 this->select_chip = pxa3xx_nand_select_chip;
1141 this->dev_ready = pxa3xx_nand_dev_ready;
1142 this->cmdfunc = pxa3xx_nand_cmdfunc;
1143 this->read_word = pxa3xx_nand_read_word;
1144 this->read_byte = pxa3xx_nand_read_byte;
1145 this->read_buf = pxa3xx_nand_read_buf;
1146 this->write_buf = pxa3xx_nand_write_buf;
1147 this->verify_buf = pxa3xx_nand_verify_buf;
1148
1149 this->ecc.mode = NAND_ECC_HW;
1150 this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
1151 this->ecc.calculate = pxa3xx_nand_ecc_calculate;
1152 this->ecc.correct = pxa3xx_nand_ecc_correct;
1153 this->ecc.size = f->page_size;
1154
1155 if (f->page_size == 2048)
1156 this->ecc.layout = &hw_largepage_ecclayout;
1157 else
1158 this->ecc.layout = &hw_smallpage_ecclayout;
1159
David Woodhousea1c06ee2008-04-22 20:39:43 +01001160 this->chip_delay = 25;
eric miaofe69af02008-02-14 15:48:23 +08001161}
1162
1163static int pxa3xx_nand_probe(struct platform_device *pdev)
1164{
1165 struct pxa3xx_nand_platform_data *pdata;
1166 struct pxa3xx_nand_info *info;
1167 struct nand_chip *this;
1168 struct mtd_info *mtd;
1169 struct resource *r;
1170 int ret = 0, irq;
1171
1172 pdata = pdev->dev.platform_data;
1173
David Woodhousea1c06ee2008-04-22 20:39:43 +01001174 if (!pdata) {
eric miaofe69af02008-02-14 15:48:23 +08001175 dev_err(&pdev->dev, "no platform data defined\n");
1176 return -ENODEV;
1177 }
1178
1179 mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
1180 GFP_KERNEL);
David Woodhousea1c06ee2008-04-22 20:39:43 +01001181 if (!mtd) {
eric miaofe69af02008-02-14 15:48:23 +08001182 dev_err(&pdev->dev, "failed to allocate memory\n");
1183 return -ENOMEM;
David Woodhousea1c06ee2008-04-22 20:39:43 +01001184 }
eric miaofe69af02008-02-14 15:48:23 +08001185
1186 info = (struct pxa3xx_nand_info *)(&mtd[1]);
1187 info->pdev = pdev;
1188
1189 this = &info->nand_chip;
1190 mtd->priv = info;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001191 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001192
Russell Kinge0d8b132008-11-11 17:52:32 +00001193 info->clk = clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001194 if (IS_ERR(info->clk)) {
1195 dev_err(&pdev->dev, "failed to get nand clock\n");
1196 ret = PTR_ERR(info->clk);
1197 goto fail_free_mtd;
1198 }
1199 clk_enable(info->clk);
1200
1201 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1202 if (r == NULL) {
1203 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1204 ret = -ENXIO;
1205 goto fail_put_clk;
1206 }
1207 info->drcmr_dat = r->start;
1208
1209 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1210 if (r == NULL) {
1211 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1212 ret = -ENXIO;
1213 goto fail_put_clk;
1214 }
1215 info->drcmr_cmd = r->start;
1216
1217 irq = platform_get_irq(pdev, 0);
1218 if (irq < 0) {
1219 dev_err(&pdev->dev, "no IRQ resource defined\n");
1220 ret = -ENXIO;
1221 goto fail_put_clk;
1222 }
1223
1224 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1225 if (r == NULL) {
1226 dev_err(&pdev->dev, "no IO memory resource defined\n");
1227 ret = -ENODEV;
1228 goto fail_put_clk;
1229 }
1230
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001231 r = request_mem_region(r->start, resource_size(r), pdev->name);
eric miaofe69af02008-02-14 15:48:23 +08001232 if (r == NULL) {
1233 dev_err(&pdev->dev, "failed to request memory resource\n");
1234 ret = -EBUSY;
1235 goto fail_put_clk;
1236 }
1237
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001238 info->mmio_base = ioremap(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001239 if (info->mmio_base == NULL) {
1240 dev_err(&pdev->dev, "ioremap() failed\n");
1241 ret = -ENODEV;
1242 goto fail_free_res;
1243 }
1244
1245 ret = pxa3xx_nand_init_buff(info);
1246 if (ret)
1247 goto fail_free_io;
1248
1249 ret = request_irq(IRQ_NAND, pxa3xx_nand_irq, IRQF_DISABLED,
1250 pdev->name, info);
1251 if (ret < 0) {
1252 dev_err(&pdev->dev, "failed to request IRQ\n");
1253 goto fail_free_buf;
1254 }
1255
Enrico Scholzc8ac3f82008-08-29 12:59:48 +02001256 ret = pxa3xx_nand_detect_flash(info, pdata);
eric miaofe69af02008-02-14 15:48:23 +08001257 if (ret) {
1258 dev_err(&pdev->dev, "failed to detect flash\n");
1259 ret = -ENODEV;
1260 goto fail_free_irq;
1261 }
1262
1263 pxa3xx_nand_init_mtd(mtd, info);
1264
1265 platform_set_drvdata(pdev, mtd);
1266
1267 if (nand_scan(mtd, 1)) {
1268 dev_err(&pdev->dev, "failed to scan nand\n");
1269 ret = -ENXIO;
1270 goto fail_free_irq;
1271 }
1272
1273 return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
1274
1275fail_free_irq:
1276 free_irq(IRQ_NAND, info);
1277fail_free_buf:
1278 if (use_dma) {
1279 pxa_free_dma(info->data_dma_ch);
1280 dma_free_coherent(&pdev->dev, info->data_buff_size,
1281 info->data_buff, info->data_buff_phys);
1282 } else
1283 kfree(info->data_buff);
1284fail_free_io:
1285 iounmap(info->mmio_base);
1286fail_free_res:
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001287 release_mem_region(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001288fail_put_clk:
1289 clk_disable(info->clk);
1290 clk_put(info->clk);
1291fail_free_mtd:
1292 kfree(mtd);
1293 return ret;
1294}
1295
1296static int pxa3xx_nand_remove(struct platform_device *pdev)
1297{
1298 struct mtd_info *mtd = platform_get_drvdata(pdev);
1299 struct pxa3xx_nand_info *info = mtd->priv;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001300 struct resource *r;
eric miaofe69af02008-02-14 15:48:23 +08001301
1302 platform_set_drvdata(pdev, NULL);
1303
1304 del_mtd_device(mtd);
1305 del_mtd_partitions(mtd);
1306 free_irq(IRQ_NAND, info);
1307 if (use_dma) {
1308 pxa_free_dma(info->data_dma_ch);
1309 dma_free_writecombine(&pdev->dev, info->data_buff_size,
1310 info->data_buff, info->data_buff_phys);
1311 } else
1312 kfree(info->data_buff);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001313
1314 iounmap(info->mmio_base);
1315 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1316 release_mem_region(r->start, resource_size(r));
1317
1318 clk_disable(info->clk);
1319 clk_put(info->clk);
1320
eric miaofe69af02008-02-14 15:48:23 +08001321 kfree(mtd);
1322 return 0;
1323}
1324
1325#ifdef CONFIG_PM
1326static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1327{
1328 struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1329 struct pxa3xx_nand_info *info = mtd->priv;
1330
1331 if (info->state != STATE_READY) {
1332 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1333 return -EAGAIN;
1334 }
1335
1336 return 0;
1337}
1338
1339static int pxa3xx_nand_resume(struct platform_device *pdev)
1340{
1341 struct mtd_info *mtd = (struct mtd_info *)platform_get_drvdata(pdev);
1342 struct pxa3xx_nand_info *info = mtd->priv;
1343
1344 clk_enable(info->clk);
1345
Eric Miao9b62d862008-05-21 17:26:15 +08001346 return pxa3xx_nand_config_flash(info, info->flash_info);
eric miaofe69af02008-02-14 15:48:23 +08001347}
1348#else
1349#define pxa3xx_nand_suspend NULL
1350#define pxa3xx_nand_resume NULL
1351#endif
1352
1353static struct platform_driver pxa3xx_nand_driver = {
1354 .driver = {
1355 .name = "pxa3xx-nand",
1356 },
1357 .probe = pxa3xx_nand_probe,
1358 .remove = pxa3xx_nand_remove,
1359 .suspend = pxa3xx_nand_suspend,
1360 .resume = pxa3xx_nand_resume,
1361};
1362
1363static int __init pxa3xx_nand_init(void)
1364{
1365 return platform_driver_register(&pxa3xx_nand_driver);
1366}
1367module_init(pxa3xx_nand_init);
1368
1369static void __exit pxa3xx_nand_exit(void)
1370{
1371 platform_driver_unregister(&pxa3xx_nand_driver);
1372}
1373module_exit(pxa3xx_nand_exit);
1374
1375MODULE_LICENSE("GPL");
1376MODULE_DESCRIPTION("PXA3xx NAND controller driver");