blob: f44044381360d8dbe63f5334fb253671d02b3b5c [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 Zhuang82b95ecb2009-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)
30
31/* registers and bit definitions */
32#define NDCR (0x00) /* Control register */
33#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
34#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
35#define NDSR (0x14) /* Status Register */
36#define NDPCR (0x18) /* Page Count Register */
37#define NDBDR0 (0x1C) /* Bad Block Register 0 */
38#define NDBDR1 (0x20) /* Bad Block Register 1 */
39#define NDDB (0x40) /* Data Buffer */
40#define NDCB0 (0x48) /* Command Buffer0 */
41#define NDCB1 (0x4C) /* Command Buffer1 */
42#define NDCB2 (0x50) /* Command Buffer2 */
43
44#define NDCR_SPARE_EN (0x1 << 31)
45#define NDCR_ECC_EN (0x1 << 30)
46#define NDCR_DMA_EN (0x1 << 29)
47#define NDCR_ND_RUN (0x1 << 28)
48#define NDCR_DWIDTH_C (0x1 << 27)
49#define NDCR_DWIDTH_M (0x1 << 26)
50#define NDCR_PAGE_SZ (0x1 << 24)
51#define NDCR_NCSX (0x1 << 23)
52#define NDCR_ND_MODE (0x3 << 21)
53#define NDCR_NAND_MODE (0x0)
54#define NDCR_CLR_PG_CNT (0x1 << 20)
55#define NDCR_CLR_ECC (0x1 << 19)
56#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
57#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
58
59#define NDCR_RA_START (0x1 << 15)
60#define NDCR_PG_PER_BLK (0x1 << 14)
61#define NDCR_ND_ARB_EN (0x1 << 12)
62
63#define NDSR_MASK (0xfff)
64#define NDSR_RDY (0x1 << 11)
65#define NDSR_CS0_PAGED (0x1 << 10)
66#define NDSR_CS1_PAGED (0x1 << 9)
67#define NDSR_CS0_CMDD (0x1 << 8)
68#define NDSR_CS1_CMDD (0x1 << 7)
69#define NDSR_CS0_BBD (0x1 << 6)
70#define NDSR_CS1_BBD (0x1 << 5)
71#define NDSR_DBERR (0x1 << 4)
72#define NDSR_SBERR (0x1 << 3)
73#define NDSR_WRDREQ (0x1 << 2)
74#define NDSR_RDDREQ (0x1 << 1)
75#define NDSR_WRCMDREQ (0x1)
76
77#define NDCB0_AUTO_RS (0x1 << 25)
78#define NDCB0_CSEL (0x1 << 24)
79#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
80#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
81#define NDCB0_NC (0x1 << 20)
82#define NDCB0_DBC (0x1 << 19)
83#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
84#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
85#define NDCB0_CMD2_MASK (0xff << 8)
86#define NDCB0_CMD1_MASK (0xff)
87#define NDCB0_ADDR_CYC_SHIFT (16)
88
eric miaofe69af02008-02-14 15:48:23 +080089/* macros for registers read/write */
90#define nand_writel(info, off, val) \
91 __raw_writel((val), (info)->mmio_base + (off))
92
93#define nand_readl(info, off) \
94 __raw_readl((info)->mmio_base + (off))
95
96/* error code and state */
97enum {
98 ERR_NONE = 0,
99 ERR_DMABUSERR = -1,
100 ERR_SENDCMD = -2,
101 ERR_DBERR = -3,
102 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300103 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800104};
105
106enum {
107 STATE_READY = 0,
108 STATE_CMD_HANDLE,
109 STATE_DMA_READING,
110 STATE_DMA_WRITING,
111 STATE_DMA_DONE,
112 STATE_PIO_READING,
113 STATE_PIO_WRITING,
114};
115
eric miaofe69af02008-02-14 15:48:23 +0800116struct pxa3xx_nand_info {
117 struct nand_chip nand_chip;
118
119 struct platform_device *pdev;
Lei Wen18c81b12010-08-17 17:25:57 +0800120 struct pxa3xx_nand_cmdset *cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800121
122 struct clk *clk;
123 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800124 unsigned long mmio_phys;
eric miaofe69af02008-02-14 15:48:23 +0800125
126 unsigned int buf_start;
127 unsigned int buf_count;
128
Lei Wene353a202011-03-03 11:08:30 +0800129 struct mtd_info *mtd;
eric miaofe69af02008-02-14 15:48:23 +0800130 /* DMA information */
131 int drcmr_dat;
132 int drcmr_cmd;
133
134 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800135 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800136 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
Lei Wen18c81b12010-08-17 17:25:57 +0800154 unsigned int page_size; /* page size of attached chip */
155 unsigned int data_size; /* data size in FIFO */
eric miaofe69af02008-02-14 15:48:23 +0800156 int retcode;
157 struct completion cmd_complete;
158
159 /* generated NDCBx register values */
160 uint32_t ndcb0;
161 uint32_t ndcb1;
162 uint32_t ndcb2;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200163
Lei Wen18c81b12010-08-17 17:25:57 +0800164 /* timing calcuted from setting */
165 uint32_t ndtr0cs0;
166 uint32_t ndtr1cs0;
167
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200168 /* calculated from pxa3xx_nand_flash data */
169 size_t oob_size;
170 size_t read_id_bytes;
171
172 unsigned int col_addr_cycles;
173 unsigned int row_addr_cycles;
eric miaofe69af02008-02-14 15:48:23 +0800174};
175
176static int use_dma = 1;
177module_param(use_dma, bool, 0444);
178MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
179
Mike Rapoportf2710492009-02-17 13:54:47 +0200180/*
181 * Default NAND flash controller configuration setup by the
182 * bootloader. This configuration is used only when pdata->keep_config is set
183 */
Lei Wenc1f82472010-08-17 13:50:23 +0800184static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800185 .read1 = 0x3000,
186 .read2 = 0x0050,
187 .program = 0x1080,
188 .read_status = 0x0070,
189 .read_id = 0x0090,
190 .erase = 0xD060,
191 .reset = 0x00FF,
192 .lock = 0x002A,
193 .unlock = 0x2423,
194 .lock_status = 0x007A,
195};
196
Lei Wenc1f82472010-08-17 13:50:23 +0800197static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800198 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
199 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
200 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
201 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800202};
203
Lei Wenc1f82472010-08-17 13:50:23 +0800204static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800205 { 0, 0, 2048, 8, 8, 0, &default_cmdset, &timing[0] },
206 { 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset, &timing[1] },
207 { 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset, &timing[1] },
208 { 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset, &timing[1] },
209 { 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset, &timing[2] },
210 { 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset, &timing[2] },
211 { 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset, &timing[2] },
212 { 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset, &timing[2] },
213 { 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800214};
215
Lei Wen227a8862010-08-18 18:00:03 +0800216/* Define a default flash type setting serve as flash detecting only */
217#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
218
eric miaofe69af02008-02-14 15:48:23 +0800219#define NDTR0_tCH(c) (min((c), 7) << 19)
220#define NDTR0_tCS(c) (min((c), 7) << 16)
221#define NDTR0_tWH(c) (min((c), 7) << 11)
222#define NDTR0_tWP(c) (min((c), 7) << 8)
223#define NDTR0_tRH(c) (min((c), 7) << 3)
224#define NDTR0_tRP(c) (min((c), 7) << 0)
225
226#define NDTR1_tR(c) (min((c), 65535) << 16)
227#define NDTR1_tWHR(c) (min((c), 15) << 4)
228#define NDTR1_tAR(c) (min((c), 15) << 0)
229
230/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800231#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800232
233static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200234 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800235{
236 unsigned long nand_clk = clk_get_rate(info->clk);
237 uint32_t ndtr0, ndtr1;
238
239 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
240 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
241 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
242 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
243 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
244 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
245
246 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
247 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
248 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
249
Lei Wen18c81b12010-08-17 17:25:57 +0800250 info->ndtr0cs0 = ndtr0;
251 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800252 nand_writel(info, NDTR0CS0, ndtr0);
253 nand_writel(info, NDTR1CS0, ndtr1);
254}
255
256#define WAIT_EVENT_TIMEOUT 10
257
258static int wait_for_event(struct pxa3xx_nand_info *info, uint32_t event)
259{
260 int timeout = WAIT_EVENT_TIMEOUT;
261 uint32_t ndsr;
262
263 while (timeout--) {
264 ndsr = nand_readl(info, NDSR) & NDSR_MASK;
265 if (ndsr & event) {
266 nand_writel(info, NDSR, ndsr);
267 return 0;
268 }
269 udelay(10);
270 }
271
272 return -ETIMEDOUT;
273}
274
Lei Wen18c81b12010-08-17 17:25:57 +0800275static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800276{
Lei Wen9d8b1042010-08-17 14:09:30 +0800277 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
278
279 info->data_size = info->page_size;
280 if (!oob_enable) {
281 info->oob_size = 0;
282 return;
283 }
284
Lei Wen18c81b12010-08-17 17:25:57 +0800285 switch (info->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800286 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800287 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800288 break;
289 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800290 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800291 break;
eric miaofe69af02008-02-14 15:48:23 +0800292 }
Lei Wen18c81b12010-08-17 17:25:57 +0800293}
294
295static int prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
296 uint16_t cmd, int column, int page_addr)
297{
298 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
299 pxa3xx_set_datasize(info);
eric miaofe69af02008-02-14 15:48:23 +0800300
301 /* generate values for NDCBx registers */
302 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
303 info->ndcb1 = 0;
304 info->ndcb2 = 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200305 info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
eric miaofe69af02008-02-14 15:48:23 +0800306
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200307 if (info->col_addr_cycles == 2) {
eric miaofe69af02008-02-14 15:48:23 +0800308 /* large block, 2 cycles for column address
309 * row address starts from 3rd cycle
310 */
Matt Reimer7f9938d2008-11-18 10:47:42 -0800311 info->ndcb1 |= page_addr << 16;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200312 if (info->row_addr_cycles == 3)
eric miaofe69af02008-02-14 15:48:23 +0800313 info->ndcb2 = (page_addr >> 16) & 0xff;
314 } else
315 /* small block, 1 cycles for column address
316 * row address starts from 2nd cycle
317 */
Matt Reimer7f9938d2008-11-18 10:47:42 -0800318 info->ndcb1 = page_addr << 8;
eric miaofe69af02008-02-14 15:48:23 +0800319
320 if (cmd == cmdset->program)
321 info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
322
323 return 0;
324}
325
326static int prepare_erase_cmd(struct pxa3xx_nand_info *info,
327 uint16_t cmd, int page_addr)
328{
329 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
330 info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
331 info->ndcb1 = page_addr;
332 info->ndcb2 = 0;
333 return 0;
334}
335
336static int prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
337{
Lei Wen18c81b12010-08-17 17:25:57 +0800338 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800339
340 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
341 info->ndcb1 = 0;
342 info->ndcb2 = 0;
343
Lei Wen9d8b1042010-08-17 14:09:30 +0800344 info->oob_size = 0;
eric miaofe69af02008-02-14 15:48:23 +0800345 if (cmd == cmdset->read_id) {
346 info->ndcb0 |= NDCB0_CMD_TYPE(3);
347 info->data_size = 8;
348 } else if (cmd == cmdset->read_status) {
349 info->ndcb0 |= NDCB0_CMD_TYPE(4);
350 info->data_size = 8;
351 } else if (cmd == cmdset->reset || cmd == cmdset->lock ||
352 cmd == cmdset->unlock) {
353 info->ndcb0 |= NDCB0_CMD_TYPE(5);
354 } else
355 return -EINVAL;
356
357 return 0;
358}
359
360static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
361{
362 uint32_t ndcr;
363
364 ndcr = nand_readl(info, NDCR);
365 nand_writel(info, NDCR, ndcr & ~int_mask);
366}
367
368static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
369{
370 uint32_t ndcr;
371
372 ndcr = nand_readl(info, NDCR);
373 nand_writel(info, NDCR, ndcr | int_mask);
374}
375
376/* NOTE: it is a must to set ND_RUN firstly, then write command buffer
377 * otherwise, it does not work
378 */
379static int write_cmd(struct pxa3xx_nand_info *info)
380{
381 uint32_t ndcr;
382
383 /* clear status bits and run */
384 nand_writel(info, NDSR, NDSR_MASK);
385
386 ndcr = info->reg_ndcr;
387
388 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
389 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
390 ndcr |= NDCR_ND_RUN;
391
392 nand_writel(info, NDCR, ndcr);
393
394 if (wait_for_event(info, NDSR_WRCMDREQ)) {
395 printk(KERN_ERR "timed out writing command\n");
396 return -ETIMEDOUT;
397 }
398
399 nand_writel(info, NDCB0, info->ndcb0);
400 nand_writel(info, NDCB0, info->ndcb1);
401 nand_writel(info, NDCB0, info->ndcb2);
402 return 0;
403}
404
405static int handle_data_pio(struct pxa3xx_nand_info *info)
406{
407 int ret, timeout = CHIP_DELAY_TIMEOUT;
408
409 switch (info->state) {
410 case STATE_PIO_WRITING:
411 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800412 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800413 if (info->oob_size > 0)
414 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
415 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800416
417 enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
418
419 ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
420 if (!ret) {
421 printk(KERN_ERR "program command time out\n");
422 return -1;
423 }
424 break;
425 case STATE_PIO_READING:
426 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800427 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800428 if (info->oob_size > 0)
429 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
430 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800431 break;
432 default:
David Woodhousea1c06ee2008-04-22 20:39:43 +0100433 printk(KERN_ERR "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800434 info->state);
435 return -EINVAL;
436 }
437
438 info->state = STATE_READY;
439 return 0;
440}
441
442static void start_data_dma(struct pxa3xx_nand_info *info, int dir_out)
443{
444 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800445 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800446
447 desc->ddadr = DDADR_STOP;
448 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
449
450 if (dir_out) {
451 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800452 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800453 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
454 } else {
455 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800456 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800457 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
458 }
459
460 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
461 DDADR(info->data_dma_ch) = info->data_desc_addr;
462 DCSR(info->data_dma_ch) |= DCSR_RUN;
463}
464
465static void pxa3xx_nand_data_dma_irq(int channel, void *data)
466{
467 struct pxa3xx_nand_info *info = data;
468 uint32_t dcsr;
469
470 dcsr = DCSR(channel);
471 DCSR(channel) = dcsr;
472
473 if (dcsr & DCSR_BUSERR) {
474 info->retcode = ERR_DMABUSERR;
475 complete(&info->cmd_complete);
476 }
477
478 if (info->state == STATE_DMA_WRITING) {
479 info->state = STATE_DMA_DONE;
480 enable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
481 } else {
482 info->state = STATE_READY;
483 complete(&info->cmd_complete);
484 }
485}
486
487static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
488{
489 struct pxa3xx_nand_info *info = devid;
490 unsigned int status;
491
492 status = nand_readl(info, NDSR);
493
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300494 if (status & (NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR)) {
eric miaofe69af02008-02-14 15:48:23 +0800495 if (status & NDSR_DBERR)
496 info->retcode = ERR_DBERR;
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300497 else if (status & NDSR_SBERR)
498 info->retcode = ERR_SBERR;
eric miaofe69af02008-02-14 15:48:23 +0800499
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300500 disable_int(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
eric miaofe69af02008-02-14 15:48:23 +0800501
502 if (info->use_dma) {
503 info->state = STATE_DMA_READING;
504 start_data_dma(info, 0);
505 } else {
506 info->state = STATE_PIO_READING;
507 complete(&info->cmd_complete);
508 }
509 } else if (status & NDSR_WRDREQ) {
510 disable_int(info, NDSR_WRDREQ);
511 if (info->use_dma) {
512 info->state = STATE_DMA_WRITING;
513 start_data_dma(info, 1);
514 } else {
515 info->state = STATE_PIO_WRITING;
516 complete(&info->cmd_complete);
517 }
518 } else if (status & (NDSR_CS0_BBD | NDSR_CS0_CMDD)) {
519 if (status & NDSR_CS0_BBD)
520 info->retcode = ERR_BBERR;
521
522 disable_int(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
523 info->state = STATE_READY;
524 complete(&info->cmd_complete);
525 }
526 nand_writel(info, NDSR, status);
527 return IRQ_HANDLED;
528}
529
530static int pxa3xx_nand_do_cmd(struct pxa3xx_nand_info *info, uint32_t event)
531{
532 uint32_t ndcr;
533 int ret, timeout = CHIP_DELAY_TIMEOUT;
534
535 if (write_cmd(info)) {
536 info->retcode = ERR_SENDCMD;
537 goto fail_stop;
538 }
539
540 info->state = STATE_CMD_HANDLE;
541
542 enable_int(info, event);
543
544 ret = wait_for_completion_timeout(&info->cmd_complete, timeout);
545 if (!ret) {
546 printk(KERN_ERR "command execution timed out\n");
547 info->retcode = ERR_SENDCMD;
548 goto fail_stop;
549 }
550
551 if (info->use_dma == 0 && info->data_size > 0)
552 if (handle_data_pio(info))
553 goto fail_stop;
554
555 return 0;
556
557fail_stop:
558 ndcr = nand_readl(info, NDCR);
559 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
560 udelay(10);
561 return -ETIMEDOUT;
562}
563
564static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
565{
566 struct pxa3xx_nand_info *info = mtd->priv;
567 return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
568}
569
570static inline int is_buf_blank(uint8_t *buf, size_t len)
571{
572 for (; len > 0; len--)
573 if (*buf++ != 0xff)
574 return 0;
575 return 1;
576}
577
578static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100579 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800580{
581 struct pxa3xx_nand_info *info = mtd->priv;
Lei Wen18c81b12010-08-17 17:25:57 +0800582 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800583 int ret;
584
585 info->use_dma = (use_dma) ? 1 : 0;
586 info->use_ecc = 0;
587 info->data_size = 0;
588 info->state = STATE_READY;
589
590 init_completion(&info->cmd_complete);
591
592 switch (command) {
593 case NAND_CMD_READOOB:
594 /* disable HW ECC to get all the OOB data */
595 info->buf_count = mtd->writesize + mtd->oobsize;
596 info->buf_start = mtd->writesize + column;
Haojian Zhuang7ce33af2009-09-14 20:21:01 +0800597 memset(info->data_buff, 0xFF, info->buf_count);
eric miaofe69af02008-02-14 15:48:23 +0800598
599 if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
600 break;
601
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300602 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
eric miaofe69af02008-02-14 15:48:23 +0800603
604 /* We only are OOB, so if the data has error, does not matter */
605 if (info->retcode == ERR_DBERR)
606 info->retcode = ERR_NONE;
607 break;
608
609 case NAND_CMD_READ0:
610 info->use_ecc = 1;
611 info->retcode = ERR_NONE;
612 info->buf_start = column;
613 info->buf_count = mtd->writesize + mtd->oobsize;
614 memset(info->data_buff, 0xFF, info->buf_count);
615
616 if (prepare_read_prog_cmd(info, cmdset->read1, column, page_addr))
617 break;
618
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300619 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ | NDSR_DBERR | NDSR_SBERR);
eric miaofe69af02008-02-14 15:48:23 +0800620
621 if (info->retcode == ERR_DBERR) {
622 /* for blank page (all 0xff), HW will calculate its ECC as
623 * 0, which is different from the ECC information within
624 * OOB, ignore such double bit errors
625 */
626 if (is_buf_blank(info->data_buff, mtd->writesize))
627 info->retcode = ERR_NONE;
628 }
629 break;
630 case NAND_CMD_SEQIN:
631 info->buf_start = column;
632 info->buf_count = mtd->writesize + mtd->oobsize;
633 memset(info->data_buff, 0xff, info->buf_count);
634
635 /* save column/page_addr for next CMD_PAGEPROG */
636 info->seqin_column = column;
637 info->seqin_page_addr = page_addr;
638 break;
639 case NAND_CMD_PAGEPROG:
640 info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
641
642 if (prepare_read_prog_cmd(info, cmdset->program,
643 info->seqin_column, info->seqin_page_addr))
644 break;
645
646 pxa3xx_nand_do_cmd(info, NDSR_WRDREQ);
647 break;
648 case NAND_CMD_ERASE1:
649 if (prepare_erase_cmd(info, cmdset->erase, page_addr))
650 break;
651
652 pxa3xx_nand_do_cmd(info, NDSR_CS0_BBD | NDSR_CS0_CMDD);
653 break;
654 case NAND_CMD_ERASE2:
655 break;
656 case NAND_CMD_READID:
657 case NAND_CMD_STATUS:
658 info->use_dma = 0; /* force PIO read */
659 info->buf_start = 0;
660 info->buf_count = (command == NAND_CMD_READID) ?
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200661 info->read_id_bytes : 1;
eric miaofe69af02008-02-14 15:48:23 +0800662
663 if (prepare_other_cmd(info, (command == NAND_CMD_READID) ?
664 cmdset->read_id : cmdset->read_status))
665 break;
666
667 pxa3xx_nand_do_cmd(info, NDSR_RDDREQ);
668 break;
669 case NAND_CMD_RESET:
670 if (prepare_other_cmd(info, cmdset->reset))
671 break;
672
673 ret = pxa3xx_nand_do_cmd(info, NDSR_CS0_CMDD);
674 if (ret == 0) {
675 int timeout = 2;
676 uint32_t ndcr;
677
678 while (timeout--) {
679 if (nand_readl(info, NDSR) & NDSR_RDY)
680 break;
681 msleep(10);
682 }
683
684 ndcr = nand_readl(info, NDCR);
685 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
686 }
687 break;
688 default:
689 printk(KERN_ERR "non-supported command.\n");
690 break;
691 }
692
693 if (info->retcode == ERR_DBERR) {
694 printk(KERN_ERR "double bit error @ page %08x\n", page_addr);
695 info->retcode = ERR_NONE;
696 }
697}
698
699static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
700{
701 struct pxa3xx_nand_info *info = mtd->priv;
702 char retval = 0xFF;
703
704 if (info->buf_start < info->buf_count)
705 /* Has just send a new command? */
706 retval = info->data_buff[info->buf_start++];
707
708 return retval;
709}
710
711static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
712{
713 struct pxa3xx_nand_info *info = mtd->priv;
714 u16 retval = 0xFFFF;
715
716 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
717 retval = *((u16 *)(info->data_buff+info->buf_start));
718 info->buf_start += 2;
719 }
720 return retval;
721}
722
723static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
724{
725 struct pxa3xx_nand_info *info = mtd->priv;
726 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
727
728 memcpy(buf, info->data_buff + info->buf_start, real_len);
729 info->buf_start += real_len;
730}
731
732static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
733 const uint8_t *buf, int len)
734{
735 struct pxa3xx_nand_info *info = mtd->priv;
736 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
737
738 memcpy(info->data_buff + info->buf_start, buf, real_len);
739 info->buf_start += real_len;
740}
741
742static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
743 const uint8_t *buf, int len)
744{
745 return 0;
746}
747
748static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
749{
750 return;
751}
752
753static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
754{
755 struct pxa3xx_nand_info *info = mtd->priv;
756
757 /* pxa3xx_nand_send_command has waited for command complete */
758 if (this->state == FL_WRITING || this->state == FL_ERASING) {
759 if (info->retcode == ERR_NONE)
760 return 0;
761 else {
762 /*
763 * any error make it return 0x01 which will tell
764 * the caller the erase and write fail
765 */
766 return 0x01;
767 }
768 }
769
770 return 0;
771}
772
773static void pxa3xx_nand_ecc_hwctl(struct mtd_info *mtd, int mode)
774{
775 return;
776}
777
778static int pxa3xx_nand_ecc_calculate(struct mtd_info *mtd,
779 const uint8_t *dat, uint8_t *ecc_code)
780{
781 return 0;
782}
783
784static int pxa3xx_nand_ecc_correct(struct mtd_info *mtd,
785 uint8_t *dat, uint8_t *read_ecc, uint8_t *calc_ecc)
786{
787 struct pxa3xx_nand_info *info = mtd->priv;
788 /*
789 * Any error include ERR_SEND_CMD, ERR_DBERR, ERR_BUSERR, we
790 * consider it as a ecc error which will tell the caller the
791 * read fail We have distinguish all the errors, but the
792 * nand_read_ecc only check this function return value
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300793 *
794 * Corrected (single-bit) errors must also be noted.
eric miaofe69af02008-02-14 15:48:23 +0800795 */
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300796 if (info->retcode == ERR_SBERR)
797 return 1;
798 else if (info->retcode != ERR_NONE)
eric miaofe69af02008-02-14 15:48:23 +0800799 return -1;
800
801 return 0;
802}
803
804static int __readid(struct pxa3xx_nand_info *info, uint32_t *id)
805{
Lei Wen18c81b12010-08-17 17:25:57 +0800806 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800807 uint32_t ndcr;
808 uint8_t id_buff[8];
809
810 if (prepare_other_cmd(info, cmdset->read_id)) {
811 printk(KERN_ERR "failed to prepare command\n");
812 return -EINVAL;
813 }
814
815 /* Send command */
816 if (write_cmd(info))
817 goto fail_timeout;
818
819 /* Wait for CMDDM(command done successfully) */
820 if (wait_for_event(info, NDSR_RDDREQ))
821 goto fail_timeout;
822
823 __raw_readsl(info->mmio_base + NDDB, id_buff, 2);
824 *id = id_buff[0] | (id_buff[1] << 8);
825 return 0;
826
827fail_timeout:
828 ndcr = nand_readl(info, NDCR);
829 nand_writel(info, NDCR, ndcr & ~NDCR_ND_RUN);
830 udelay(10);
831 return -ETIMEDOUT;
832}
833
834static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200835 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800836{
837 struct platform_device *pdev = info->pdev;
838 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
839 uint32_t ndcr = 0x00000FFF; /* disable all interrupts */
840
841 if (f->page_size != 2048 && f->page_size != 512)
842 return -EINVAL;
843
844 if (f->flash_width != 16 && f->flash_width != 8)
845 return -EINVAL;
846
847 /* calculate flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800848 info->cmdset = f->cmdset;
849 info->page_size = f->page_size;
850 info->oob_buff = info->data_buff + f->page_size;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200851 info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800852
853 /* calculate addressing information */
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200854 info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800855
856 if (f->num_blocks * f->page_per_block > 65536)
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200857 info->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800858 else
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200859 info->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800860
861 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200862 ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800863 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
864 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
865 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
866 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
867
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200868 ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800869 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
870
871 info->reg_ndcr = ndcr;
872
873 pxa3xx_nand_set_timing(info, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800874 return 0;
875}
876
Mike Rapoportf2710492009-02-17 13:54:47 +0200877static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
878{
879 uint32_t ndcr = nand_readl(info, NDCR);
880 struct nand_flash_dev *type = NULL;
Lei Wen18c81b12010-08-17 17:25:57 +0800881 uint32_t id = -1, page_per_block, num_blocks;
Mike Rapoportf2710492009-02-17 13:54:47 +0200882 int i;
883
Lei Wen18c81b12010-08-17 17:25:57 +0800884 page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
885 info->page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Mike Rapoportf2710492009-02-17 13:54:47 +0200886 /* set info fields needed to __readid */
Lei Wen18c81b12010-08-17 17:25:57 +0800887 info->read_id_bytes = (info->page_size == 2048) ? 4 : 2;
Mike Rapoportf2710492009-02-17 13:54:47 +0200888 info->reg_ndcr = ndcr;
Dan Carpenter52d039f2011-01-06 17:05:36 +0300889 info->cmdset = &default_cmdset;
Mike Rapoportf2710492009-02-17 13:54:47 +0200890
891 if (__readid(info, &id))
892 return -ENODEV;
893
894 /* Lookup the flash id */
895 id = (id >> 8) & 0xff; /* device id is byte 2 */
896 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
897 if (id == nand_flash_ids[i].id) {
898 type = &nand_flash_ids[i];
899 break;
900 }
901 }
902
903 if (!type)
904 return -ENODEV;
905
906 /* fill the missing flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800907 i = __ffs(page_per_block * info->page_size);
908 num_blocks = type->chipsize << (20 - i);
Mike Rapoportf2710492009-02-17 13:54:47 +0200909
Mike Rapoportf2710492009-02-17 13:54:47 +0200910 /* calculate addressing information */
Lei Wen18c81b12010-08-17 17:25:57 +0800911 info->col_addr_cycles = (info->page_size == 2048) ? 2 : 1;
Mike Rapoportf2710492009-02-17 13:54:47 +0200912
Lei Wen18c81b12010-08-17 17:25:57 +0800913 if (num_blocks * page_per_block > 65536)
Mike Rapoportf2710492009-02-17 13:54:47 +0200914 info->row_addr_cycles = 3;
915 else
916 info->row_addr_cycles = 2;
917
Lei Wen18c81b12010-08-17 17:25:57 +0800918 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
919 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200920
921 return 0;
922}
923
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200924static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
925 const struct pxa3xx_nand_platform_data *pdata)
eric miaofe69af02008-02-14 15:48:23 +0800926{
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200927 const struct pxa3xx_nand_flash *f;
Enrico Scholz2675e942008-08-29 12:59:52 +0200928 uint32_t id = -1;
eric miaofe69af02008-02-14 15:48:23 +0800929 int i;
930
Mike Rapoportf2710492009-02-17 13:54:47 +0200931 if (pdata->keep_config)
932 if (pxa3xx_nand_detect_config(info) == 0)
933 return 0;
934
Lei Wen227a8862010-08-18 18:00:03 +0800935 /* we use default timing to detect id */
936 f = DEFAULT_FLASH_TYPE;
937 pxa3xx_nand_config_flash(info, f);
938 if (__readid(info, &id))
939 goto fail_detect;
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200940
Lei Wen227a8862010-08-18 18:00:03 +0800941 for (i=0; i<ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; i++) {
942 /* we first choose the flash definition from platfrom */
943 if (i < pdata->num_flash)
944 f = pdata->flash + i;
945 else
946 f = &builtin_flash_types[i - pdata->num_flash + 1];
947 if (f->chip_id == id) {
948 dev_info(&info->pdev->dev, "detect chip id: 0x%x\n", id);
949 pxa3xx_nand_config_flash(info, f);
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200950 return 0;
Lei Wen227a8862010-08-18 18:00:03 +0800951 }
eric miaofe69af02008-02-14 15:48:23 +0800952 }
953
Enrico Scholz2675e942008-08-29 12:59:52 +0200954 dev_warn(&info->pdev->dev,
955 "failed to detect configured nand flash; found %04x instead of\n",
956 id);
Lei Wen227a8862010-08-18 18:00:03 +0800957fail_detect:
eric miaofe69af02008-02-14 15:48:23 +0800958 return -ENODEV;
959}
960
961/* the maximum possible buffer size for large page with OOB data
962 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
963 * data buffer and the DMA descriptor
964 */
965#define MAX_BUFF_SIZE PAGE_SIZE
966
967static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
968{
969 struct platform_device *pdev = info->pdev;
970 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
971
972 if (use_dma == 0) {
973 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
974 if (info->data_buff == NULL)
975 return -ENOMEM;
976 return 0;
977 }
978
979 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
980 &info->data_buff_phys, GFP_KERNEL);
981 if (info->data_buff == NULL) {
982 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
983 return -ENOMEM;
984 }
985
986 info->data_buff_size = MAX_BUFF_SIZE;
987 info->data_desc = (void *)info->data_buff + data_desc_offset;
988 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
989
990 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
991 pxa3xx_nand_data_dma_irq, info);
992 if (info->data_dma_ch < 0) {
993 dev_err(&pdev->dev, "failed to request data dma\n");
994 dma_free_coherent(&pdev->dev, info->data_buff_size,
995 info->data_buff, info->data_buff_phys);
996 return info->data_dma_ch;
997 }
998
999 return 0;
1000}
1001
1002static struct nand_ecclayout hw_smallpage_ecclayout = {
1003 .eccbytes = 6,
1004 .eccpos = {8, 9, 10, 11, 12, 13 },
1005 .oobfree = { {2, 6} }
1006};
1007
1008static struct nand_ecclayout hw_largepage_ecclayout = {
1009 .eccbytes = 24,
1010 .eccpos = {
1011 40, 41, 42, 43, 44, 45, 46, 47,
1012 48, 49, 50, 51, 52, 53, 54, 55,
1013 56, 57, 58, 59, 60, 61, 62, 63},
1014 .oobfree = { {2, 38} }
1015};
1016
1017static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
1018 struct pxa3xx_nand_info *info)
1019{
eric miaofe69af02008-02-14 15:48:23 +08001020 struct nand_chip *this = &info->nand_chip;
1021
Lei Wen18c81b12010-08-17 17:25:57 +08001022 this->options = (info->reg_ndcr & NDCR_DWIDTH_C) ? NAND_BUSWIDTH_16: 0;
eric miaofe69af02008-02-14 15:48:23 +08001023
1024 this->waitfunc = pxa3xx_nand_waitfunc;
1025 this->select_chip = pxa3xx_nand_select_chip;
1026 this->dev_ready = pxa3xx_nand_dev_ready;
1027 this->cmdfunc = pxa3xx_nand_cmdfunc;
1028 this->read_word = pxa3xx_nand_read_word;
1029 this->read_byte = pxa3xx_nand_read_byte;
1030 this->read_buf = pxa3xx_nand_read_buf;
1031 this->write_buf = pxa3xx_nand_write_buf;
1032 this->verify_buf = pxa3xx_nand_verify_buf;
1033
1034 this->ecc.mode = NAND_ECC_HW;
1035 this->ecc.hwctl = pxa3xx_nand_ecc_hwctl;
1036 this->ecc.calculate = pxa3xx_nand_ecc_calculate;
1037 this->ecc.correct = pxa3xx_nand_ecc_correct;
Lei Wen18c81b12010-08-17 17:25:57 +08001038 this->ecc.size = info->page_size;
eric miaofe69af02008-02-14 15:48:23 +08001039
Lei Wen18c81b12010-08-17 17:25:57 +08001040 if (info->page_size == 2048)
eric miaofe69af02008-02-14 15:48:23 +08001041 this->ecc.layout = &hw_largepage_ecclayout;
1042 else
1043 this->ecc.layout = &hw_smallpage_ecclayout;
1044
David Woodhousea1c06ee2008-04-22 20:39:43 +01001045 this->chip_delay = 25;
eric miaofe69af02008-02-14 15:48:23 +08001046}
1047
Lei Wene353a202011-03-03 11:08:30 +08001048static
1049struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001050{
Lei Wene353a202011-03-03 11:08:30 +08001051 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
eric miaofe69af02008-02-14 15:48:23 +08001052 struct pxa3xx_nand_info *info;
eric miaofe69af02008-02-14 15:48:23 +08001053 struct mtd_info *mtd;
1054 struct resource *r;
Lei Wene353a202011-03-03 11:08:30 +08001055 int ret, irq;
eric miaofe69af02008-02-14 15:48:23 +08001056
1057 mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
1058 GFP_KERNEL);
David Woodhousea1c06ee2008-04-22 20:39:43 +01001059 if (!mtd) {
eric miaofe69af02008-02-14 15:48:23 +08001060 dev_err(&pdev->dev, "failed to allocate memory\n");
Lei Wene353a202011-03-03 11:08:30 +08001061 return NULL;
David Woodhousea1c06ee2008-04-22 20:39:43 +01001062 }
eric miaofe69af02008-02-14 15:48:23 +08001063
1064 info = (struct pxa3xx_nand_info *)(&mtd[1]);
1065 info->pdev = pdev;
1066
eric miaofe69af02008-02-14 15:48:23 +08001067 mtd->priv = info;
Lei Wene353a202011-03-03 11:08:30 +08001068 info->mtd = mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001069 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001070
Russell Kinge0d8b132008-11-11 17:52:32 +00001071 info->clk = clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001072 if (IS_ERR(info->clk)) {
1073 dev_err(&pdev->dev, "failed to get nand clock\n");
1074 ret = PTR_ERR(info->clk);
1075 goto fail_free_mtd;
1076 }
1077 clk_enable(info->clk);
1078
1079 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1080 if (r == NULL) {
1081 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1082 ret = -ENXIO;
1083 goto fail_put_clk;
1084 }
1085 info->drcmr_dat = r->start;
1086
1087 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1088 if (r == NULL) {
1089 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1090 ret = -ENXIO;
1091 goto fail_put_clk;
1092 }
1093 info->drcmr_cmd = r->start;
1094
1095 irq = platform_get_irq(pdev, 0);
1096 if (irq < 0) {
1097 dev_err(&pdev->dev, "no IRQ resource defined\n");
1098 ret = -ENXIO;
1099 goto fail_put_clk;
1100 }
1101
1102 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1103 if (r == NULL) {
1104 dev_err(&pdev->dev, "no IO memory resource defined\n");
1105 ret = -ENODEV;
1106 goto fail_put_clk;
1107 }
1108
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001109 r = request_mem_region(r->start, resource_size(r), pdev->name);
eric miaofe69af02008-02-14 15:48:23 +08001110 if (r == NULL) {
1111 dev_err(&pdev->dev, "failed to request memory resource\n");
1112 ret = -EBUSY;
1113 goto fail_put_clk;
1114 }
1115
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001116 info->mmio_base = ioremap(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001117 if (info->mmio_base == NULL) {
1118 dev_err(&pdev->dev, "ioremap() failed\n");
1119 ret = -ENODEV;
1120 goto fail_free_res;
1121 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001122 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001123
1124 ret = pxa3xx_nand_init_buff(info);
1125 if (ret)
1126 goto fail_free_io;
1127
Haojian Zhuang346e1252009-09-10 14:27:23 +08001128 /* initialize all interrupts to be disabled */
1129 disable_int(info, NDSR_MASK);
1130
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001131 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1132 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001133 if (ret < 0) {
1134 dev_err(&pdev->dev, "failed to request IRQ\n");
1135 goto fail_free_buf;
1136 }
1137
Enrico Scholzc8ac3f82008-08-29 12:59:48 +02001138 ret = pxa3xx_nand_detect_flash(info, pdata);
eric miaofe69af02008-02-14 15:48:23 +08001139 if (ret) {
1140 dev_err(&pdev->dev, "failed to detect flash\n");
1141 ret = -ENODEV;
1142 goto fail_free_irq;
1143 }
1144
1145 pxa3xx_nand_init_mtd(mtd, info);
Lei Wene353a202011-03-03 11:08:30 +08001146 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001147
Lei Wene353a202011-03-03 11:08:30 +08001148 return info;
eric miaofe69af02008-02-14 15:48:23 +08001149
1150fail_free_irq:
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001151 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001152fail_free_buf:
1153 if (use_dma) {
1154 pxa_free_dma(info->data_dma_ch);
1155 dma_free_coherent(&pdev->dev, info->data_buff_size,
1156 info->data_buff, info->data_buff_phys);
1157 } else
1158 kfree(info->data_buff);
1159fail_free_io:
1160 iounmap(info->mmio_base);
1161fail_free_res:
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001162 release_mem_region(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001163fail_put_clk:
1164 clk_disable(info->clk);
1165 clk_put(info->clk);
1166fail_free_mtd:
1167 kfree(mtd);
Lei Wene353a202011-03-03 11:08:30 +08001168 return NULL;
eric miaofe69af02008-02-14 15:48:23 +08001169}
1170
1171static int pxa3xx_nand_remove(struct platform_device *pdev)
1172{
Lei Wene353a202011-03-03 11:08:30 +08001173 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1174 struct mtd_info *mtd = info->mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001175 struct resource *r;
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001176 int irq;
eric miaofe69af02008-02-14 15:48:23 +08001177
1178 platform_set_drvdata(pdev, NULL);
1179
1180 del_mtd_device(mtd);
Mark F. Brown99d38962010-08-26 04:56:51 -04001181#ifdef CONFIG_MTD_PARTITIONS
eric miaofe69af02008-02-14 15:48:23 +08001182 del_mtd_partitions(mtd);
Mark F. Brown99d38962010-08-26 04:56:51 -04001183#endif
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001184 irq = platform_get_irq(pdev, 0);
1185 if (irq >= 0)
1186 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001187 if (use_dma) {
1188 pxa_free_dma(info->data_dma_ch);
1189 dma_free_writecombine(&pdev->dev, info->data_buff_size,
1190 info->data_buff, info->data_buff_phys);
1191 } else
1192 kfree(info->data_buff);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001193
1194 iounmap(info->mmio_base);
1195 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1196 release_mem_region(r->start, resource_size(r));
1197
1198 clk_disable(info->clk);
1199 clk_put(info->clk);
1200
eric miaofe69af02008-02-14 15:48:23 +08001201 kfree(mtd);
1202 return 0;
1203}
1204
Lei Wene353a202011-03-03 11:08:30 +08001205static int pxa3xx_nand_probe(struct platform_device *pdev)
1206{
1207 struct pxa3xx_nand_platform_data *pdata;
1208 struct pxa3xx_nand_info *info;
1209
1210 pdata = pdev->dev.platform_data;
1211 if (!pdata) {
1212 dev_err(&pdev->dev, "no platform data defined\n");
1213 return -ENODEV;
1214 }
1215
1216 info = alloc_nand_resource(pdev);
1217 if (info == NULL)
1218 return -ENOMEM;
1219
1220 if (nand_scan(info->mtd, 1)) {
1221 dev_err(&pdev->dev, "failed to scan nand\n");
1222 pxa3xx_nand_remove(pdev);
1223 return -ENODEV;
1224 }
1225
1226#ifdef CONFIG_MTD_PARTITIONS
1227 if (mtd_has_cmdlinepart()) {
1228 const char *probes[] = { "cmdlinepart", NULL };
1229 struct mtd_partition *parts;
1230 int nr_parts;
1231
1232 nr_parts = parse_mtd_partitions(info->mtd, probes, &parts, 0);
1233
1234 if (nr_parts)
1235 return add_mtd_partitions(mtd, parts, nr_parts);
1236 }
1237
1238 return add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
1239#else
1240 return 0;
1241#endif
1242}
1243
eric miaofe69af02008-02-14 15:48:23 +08001244#ifdef CONFIG_PM
1245static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1246{
Lei Wene353a202011-03-03 11:08:30 +08001247 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1248 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001249
1250 if (info->state != STATE_READY) {
1251 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1252 return -EAGAIN;
1253 }
1254
1255 return 0;
1256}
1257
1258static int pxa3xx_nand_resume(struct platform_device *pdev)
1259{
Lei Wene353a202011-03-03 11:08:30 +08001260 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1261 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001262
Lei Wen18c81b12010-08-17 17:25:57 +08001263 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1264 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
eric miaofe69af02008-02-14 15:48:23 +08001265 clk_enable(info->clk);
1266
Lei Wen18c81b12010-08-17 17:25:57 +08001267 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001268}
1269#else
1270#define pxa3xx_nand_suspend NULL
1271#define pxa3xx_nand_resume NULL
1272#endif
1273
1274static struct platform_driver pxa3xx_nand_driver = {
1275 .driver = {
1276 .name = "pxa3xx-nand",
1277 },
1278 .probe = pxa3xx_nand_probe,
1279 .remove = pxa3xx_nand_remove,
1280 .suspend = pxa3xx_nand_suspend,
1281 .resume = pxa3xx_nand_resume,
1282};
1283
1284static int __init pxa3xx_nand_init(void)
1285{
1286 return platform_driver_register(&pxa3xx_nand_driver);
1287}
1288module_init(pxa3xx_nand_init);
1289
1290static void __exit pxa3xx_nand_exit(void)
1291{
1292 platform_driver_unregister(&pxa3xx_nand_driver);
1293}
1294module_exit(pxa3xx_nand_exit);
1295
1296MODULE_LICENSE("GPL");
1297MODULE_DESCRIPTION("PXA3xx NAND controller driver");