blob: be0aa44ada427aebdc6dc6139dc2f21c30da5662 [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)
eric miaofe69af02008-02-14 15:48:23 +080031
32/* registers and bit definitions */
33#define NDCR (0x00) /* Control register */
34#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
35#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
36#define NDSR (0x14) /* Status Register */
37#define NDPCR (0x18) /* Page Count Register */
38#define NDBDR0 (0x1C) /* Bad Block Register 0 */
39#define NDBDR1 (0x20) /* Bad Block Register 1 */
40#define NDDB (0x40) /* Data Buffer */
41#define NDCB0 (0x48) /* Command Buffer0 */
42#define NDCB1 (0x4C) /* Command Buffer1 */
43#define NDCB2 (0x50) /* Command Buffer2 */
44
45#define NDCR_SPARE_EN (0x1 << 31)
46#define NDCR_ECC_EN (0x1 << 30)
47#define NDCR_DMA_EN (0x1 << 29)
48#define NDCR_ND_RUN (0x1 << 28)
49#define NDCR_DWIDTH_C (0x1 << 27)
50#define NDCR_DWIDTH_M (0x1 << 26)
51#define NDCR_PAGE_SZ (0x1 << 24)
52#define NDCR_NCSX (0x1 << 23)
53#define NDCR_ND_MODE (0x3 << 21)
54#define NDCR_NAND_MODE (0x0)
55#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080056#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080057#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
58#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
59
60#define NDCR_RA_START (0x1 << 15)
61#define NDCR_PG_PER_BLK (0x1 << 14)
62#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080063#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080064
65#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080066#define NDSR_RDY (0x1 << 12)
67#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080068#define NDSR_CS0_PAGED (0x1 << 10)
69#define NDSR_CS1_PAGED (0x1 << 9)
70#define NDSR_CS0_CMDD (0x1 << 8)
71#define NDSR_CS1_CMDD (0x1 << 7)
72#define NDSR_CS0_BBD (0x1 << 6)
73#define NDSR_CS1_BBD (0x1 << 5)
74#define NDSR_DBERR (0x1 << 4)
75#define NDSR_SBERR (0x1 << 3)
76#define NDSR_WRDREQ (0x1 << 2)
77#define NDSR_RDDREQ (0x1 << 1)
78#define NDSR_WRCMDREQ (0x1)
79
80#define NDCB0_AUTO_RS (0x1 << 25)
81#define NDCB0_CSEL (0x1 << 24)
82#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
83#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
84#define NDCB0_NC (0x1 << 20)
85#define NDCB0_DBC (0x1 << 19)
86#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
87#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
88#define NDCB0_CMD2_MASK (0xff << 8)
89#define NDCB0_CMD1_MASK (0xff)
90#define NDCB0_ADDR_CYC_SHIFT (16)
91
eric miaofe69af02008-02-14 15:48:23 +080092/* 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 {
Lei Wenf8155a42011-02-28 10:32:11 +0800110 STATE_IDLE = 0,
eric miaofe69af02008-02-14 15:48:23 +0800111 STATE_CMD_HANDLE,
112 STATE_DMA_READING,
113 STATE_DMA_WRITING,
114 STATE_DMA_DONE,
115 STATE_PIO_READING,
116 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800117 STATE_CMD_DONE,
118 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800119};
120
eric miaofe69af02008-02-14 15:48:23 +0800121struct pxa3xx_nand_info {
122 struct nand_chip nand_chip;
123
124 struct platform_device *pdev;
Lei Wen18c81b12010-08-17 17:25:57 +0800125 struct pxa3xx_nand_cmdset *cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800126
127 struct clk *clk;
128 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800129 unsigned long mmio_phys;
eric miaofe69af02008-02-14 15:48:23 +0800130
131 unsigned int buf_start;
132 unsigned int buf_count;
133
Lei Wene353a202011-03-03 11:08:30 +0800134 struct mtd_info *mtd;
eric miaofe69af02008-02-14 15:48:23 +0800135 /* DMA information */
136 int drcmr_dat;
137 int drcmr_cmd;
138
139 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800140 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800141 dma_addr_t data_buff_phys;
142 size_t data_buff_size;
143 int data_dma_ch;
144 struct pxa_dma_desc *data_desc;
145 dma_addr_t data_desc_addr;
146
147 uint32_t reg_ndcr;
148
149 /* saved column/page_addr during CMD_SEQIN */
150 int seqin_column;
151 int seqin_page_addr;
152
153 /* relate to the command */
154 unsigned int state;
155
156 int use_ecc; /* use HW ECC ? */
157 int use_dma; /* use DMA ? */
158
Lei Wen18c81b12010-08-17 17:25:57 +0800159 unsigned int page_size; /* page size of attached chip */
160 unsigned int data_size; /* data size in FIFO */
eric miaofe69af02008-02-14 15:48:23 +0800161 int retcode;
162 struct completion cmd_complete;
163
164 /* generated NDCBx register values */
165 uint32_t ndcb0;
166 uint32_t ndcb1;
167 uint32_t ndcb2;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200168
Lei Wen18c81b12010-08-17 17:25:57 +0800169 /* timing calcuted from setting */
170 uint32_t ndtr0cs0;
171 uint32_t ndtr1cs0;
172
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200173 /* calculated from pxa3xx_nand_flash data */
174 size_t oob_size;
175 size_t read_id_bytes;
176
177 unsigned int col_addr_cycles;
178 unsigned int row_addr_cycles;
eric miaofe69af02008-02-14 15:48:23 +0800179};
180
181static int use_dma = 1;
182module_param(use_dma, bool, 0444);
183MODULE_PARM_DESC(use_dma, "enable DMA for data transfering to/from NAND HW");
184
Mike Rapoportf2710492009-02-17 13:54:47 +0200185/*
186 * Default NAND flash controller configuration setup by the
187 * bootloader. This configuration is used only when pdata->keep_config is set
188 */
Lei Wenc1f82472010-08-17 13:50:23 +0800189static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800190 .read1 = 0x3000,
191 .read2 = 0x0050,
192 .program = 0x1080,
193 .read_status = 0x0070,
194 .read_id = 0x0090,
195 .erase = 0xD060,
196 .reset = 0x00FF,
197 .lock = 0x002A,
198 .unlock = 0x2423,
199 .lock_status = 0x007A,
200};
201
Lei Wenc1f82472010-08-17 13:50:23 +0800202static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800203 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
204 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
205 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
206 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800207};
208
Lei Wenc1f82472010-08-17 13:50:23 +0800209static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800210 { 0, 0, 2048, 8, 8, 0, &default_cmdset, &timing[0] },
211 { 0x46ec, 32, 512, 16, 16, 4096, &default_cmdset, &timing[1] },
212 { 0xdaec, 64, 2048, 8, 8, 2048, &default_cmdset, &timing[1] },
213 { 0xd7ec, 128, 4096, 8, 8, 8192, &default_cmdset, &timing[1] },
214 { 0xa12c, 64, 2048, 8, 8, 1024, &default_cmdset, &timing[2] },
215 { 0xb12c, 64, 2048, 16, 16, 1024, &default_cmdset, &timing[2] },
216 { 0xdc2c, 64, 2048, 8, 8, 4096, &default_cmdset, &timing[2] },
217 { 0xcc2c, 64, 2048, 16, 16, 4096, &default_cmdset, &timing[2] },
218 { 0xba20, 64, 2048, 16, 16, 2048, &default_cmdset, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800219};
220
Lei Wen227a8862010-08-18 18:00:03 +0800221/* Define a default flash type setting serve as flash detecting only */
222#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
223
eric miaofe69af02008-02-14 15:48:23 +0800224#define NDTR0_tCH(c) (min((c), 7) << 19)
225#define NDTR0_tCS(c) (min((c), 7) << 16)
226#define NDTR0_tWH(c) (min((c), 7) << 11)
227#define NDTR0_tWP(c) (min((c), 7) << 8)
228#define NDTR0_tRH(c) (min((c), 7) << 3)
229#define NDTR0_tRP(c) (min((c), 7) << 0)
230
231#define NDTR1_tR(c) (min((c), 65535) << 16)
232#define NDTR1_tWHR(c) (min((c), 15) << 4)
233#define NDTR1_tAR(c) (min((c), 15) << 0)
234
235/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800236#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800237
238static void pxa3xx_nand_set_timing(struct pxa3xx_nand_info *info,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200239 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800240{
241 unsigned long nand_clk = clk_get_rate(info->clk);
242 uint32_t ndtr0, ndtr1;
243
244 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
245 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
246 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
247 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
248 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
249 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
250
251 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
252 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
253 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
254
Lei Wen18c81b12010-08-17 17:25:57 +0800255 info->ndtr0cs0 = ndtr0;
256 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800257 nand_writel(info, NDTR0CS0, ndtr0);
258 nand_writel(info, NDTR1CS0, ndtr1);
259}
260
Lei Wen18c81b12010-08-17 17:25:57 +0800261static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800262{
Lei Wen9d8b1042010-08-17 14:09:30 +0800263 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
264
265 info->data_size = info->page_size;
266 if (!oob_enable) {
267 info->oob_size = 0;
268 return;
269 }
270
Lei Wen18c81b12010-08-17 17:25:57 +0800271 switch (info->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800272 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800273 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800274 break;
275 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800276 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800277 break;
eric miaofe69af02008-02-14 15:48:23 +0800278 }
Lei Wen18c81b12010-08-17 17:25:57 +0800279}
280
Lei Wenf8155a42011-02-28 10:32:11 +0800281/**
282 * NOTE: it is a must to set ND_RUN firstly, then write
283 * command buffer, otherwise, it does not work.
284 * We enable all the interrupt at the same time, and
285 * let pxa3xx_nand_irq to handle all logic.
286 */
287static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
288{
289 uint32_t ndcr;
290
291 ndcr = info->reg_ndcr;
292 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
293 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
294 ndcr |= NDCR_ND_RUN;
295
296 /* clear status bits and run */
297 nand_writel(info, NDCR, 0);
298 nand_writel(info, NDSR, NDSR_MASK);
299 nand_writel(info, NDCR, ndcr);
300}
301
302static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
303{
304 uint32_t ndcr;
305 int timeout = NAND_STOP_DELAY;
306
307 /* wait RUN bit in NDCR become 0 */
308 ndcr = nand_readl(info, NDCR);
309 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
310 ndcr = nand_readl(info, NDCR);
311 udelay(1);
312 }
313
314 if (timeout <= 0) {
315 ndcr &= ~NDCR_ND_RUN;
316 nand_writel(info, NDCR, ndcr);
317 }
318 /* clear status bits */
319 nand_writel(info, NDSR, NDSR_MASK);
320}
321
322static void prepare_read_prog_cmd(struct pxa3xx_nand_info *info,
Lei Wen18c81b12010-08-17 17:25:57 +0800323 uint16_t cmd, int column, int page_addr)
324{
325 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
326 pxa3xx_set_datasize(info);
eric miaofe69af02008-02-14 15:48:23 +0800327
328 /* generate values for NDCBx registers */
329 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
330 info->ndcb1 = 0;
331 info->ndcb2 = 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200332 info->ndcb0 |= NDCB0_ADDR_CYC(info->row_addr_cycles + info->col_addr_cycles);
eric miaofe69af02008-02-14 15:48:23 +0800333
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200334 if (info->col_addr_cycles == 2) {
eric miaofe69af02008-02-14 15:48:23 +0800335 /* large block, 2 cycles for column address
336 * row address starts from 3rd cycle
337 */
Matt Reimer7f9938d2008-11-18 10:47:42 -0800338 info->ndcb1 |= page_addr << 16;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200339 if (info->row_addr_cycles == 3)
eric miaofe69af02008-02-14 15:48:23 +0800340 info->ndcb2 = (page_addr >> 16) & 0xff;
341 } else
342 /* small block, 1 cycles for column address
343 * row address starts from 2nd cycle
344 */
Matt Reimer7f9938d2008-11-18 10:47:42 -0800345 info->ndcb1 = page_addr << 8;
eric miaofe69af02008-02-14 15:48:23 +0800346
347 if (cmd == cmdset->program)
348 info->ndcb0 |= NDCB0_CMD_TYPE(1) | NDCB0_AUTO_RS;
eric miaofe69af02008-02-14 15:48:23 +0800349}
350
Lei Wenf8155a42011-02-28 10:32:11 +0800351static void prepare_erase_cmd(struct pxa3xx_nand_info *info,
eric miaofe69af02008-02-14 15:48:23 +0800352 uint16_t cmd, int page_addr)
353{
354 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
355 info->ndcb0 |= NDCB0_CMD_TYPE(2) | NDCB0_AUTO_RS | NDCB0_ADDR_CYC(3);
356 info->ndcb1 = page_addr;
357 info->ndcb2 = 0;
eric miaofe69af02008-02-14 15:48:23 +0800358}
359
Lei Wenf8155a42011-02-28 10:32:11 +0800360static void prepare_other_cmd(struct pxa3xx_nand_info *info, uint16_t cmd)
eric miaofe69af02008-02-14 15:48:23 +0800361{
Lei Wen18c81b12010-08-17 17:25:57 +0800362 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
eric miaofe69af02008-02-14 15:48:23 +0800363
364 info->ndcb0 = cmd | ((cmd & 0xff00) ? NDCB0_DBC : 0);
365 info->ndcb1 = 0;
366 info->ndcb2 = 0;
367
Lei Wen9d8b1042010-08-17 14:09:30 +0800368 info->oob_size = 0;
eric miaofe69af02008-02-14 15:48:23 +0800369 if (cmd == cmdset->read_id) {
Lei Wenf8155a42011-02-28 10:32:11 +0800370 info->ndcb0 |= NDCB0_CMD_TYPE(3) | NDCB0_ADDR_CYC(1);
eric miaofe69af02008-02-14 15:48:23 +0800371 info->data_size = 8;
372 } else if (cmd == cmdset->read_status) {
373 info->ndcb0 |= NDCB0_CMD_TYPE(4);
374 info->data_size = 8;
375 } else if (cmd == cmdset->reset || cmd == cmdset->lock ||
376 cmd == cmdset->unlock) {
377 info->ndcb0 |= NDCB0_CMD_TYPE(5);
378 } else
Lei Wenf8155a42011-02-28 10:32:11 +0800379 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800380}
381
382static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
383{
384 uint32_t ndcr;
385
386 ndcr = nand_readl(info, NDCR);
387 nand_writel(info, NDCR, ndcr & ~int_mask);
388}
389
390static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
391{
392 uint32_t ndcr;
393
394 ndcr = nand_readl(info, NDCR);
395 nand_writel(info, NDCR, ndcr | int_mask);
396}
397
Lei Wenf8155a42011-02-28 10:32:11 +0800398static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800399{
eric miaofe69af02008-02-14 15:48:23 +0800400 switch (info->state) {
401 case STATE_PIO_WRITING:
402 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800403 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800404 if (info->oob_size > 0)
405 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
406 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800407 break;
408 case STATE_PIO_READING:
409 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800410 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800411 if (info->oob_size > 0)
412 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
413 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800414 break;
415 default:
David Woodhousea1c06ee2008-04-22 20:39:43 +0100416 printk(KERN_ERR "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800417 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800418 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800419 }
eric miaofe69af02008-02-14 15:48:23 +0800420}
421
Lei Wenf8155a42011-02-28 10:32:11 +0800422static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800423{
424 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800425 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800426
427 desc->ddadr = DDADR_STOP;
428 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
429
Lei Wenf8155a42011-02-28 10:32:11 +0800430 switch (info->state) {
431 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800432 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800433 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800434 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800435 break;
436 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800437 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800438 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800439 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800440 break;
441 default:
442 printk(KERN_ERR "%s: invalid state %d\n", __func__,
443 info->state);
444 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800445 }
446
447 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
448 DDADR(info->data_dma_ch) = info->data_desc_addr;
449 DCSR(info->data_dma_ch) |= DCSR_RUN;
450}
451
452static void pxa3xx_nand_data_dma_irq(int channel, void *data)
453{
454 struct pxa3xx_nand_info *info = data;
455 uint32_t dcsr;
456
457 dcsr = DCSR(channel);
458 DCSR(channel) = dcsr;
459
460 if (dcsr & DCSR_BUSERR) {
461 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800462 }
463
Lei Wenf8155a42011-02-28 10:32:11 +0800464 info->state = STATE_DMA_DONE;
465 enable_int(info, NDCR_INT_MASK);
466 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800467}
468
469static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
470{
471 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800472 unsigned int status, is_completed = 0;
eric miaofe69af02008-02-14 15:48:23 +0800473
474 status = nand_readl(info, NDSR);
475
Lei Wenf8155a42011-02-28 10:32:11 +0800476 if (status & NDSR_DBERR)
477 info->retcode = ERR_DBERR;
478 if (status & NDSR_SBERR)
479 info->retcode = ERR_SBERR;
480 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
481 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800482 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800483 disable_int(info, NDCR_INT_MASK);
484 info->state = (status & NDSR_RDDREQ) ?
485 STATE_DMA_READING : STATE_DMA_WRITING;
486 start_data_dma(info);
487 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800488 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800489 info->state = (status & NDSR_RDDREQ) ?
490 STATE_PIO_READING : STATE_PIO_WRITING;
491 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800492 }
Lei Wenf8155a42011-02-28 10:32:11 +0800493 }
494 if (status & NDSR_CS0_CMDD) {
495 info->state = STATE_CMD_DONE;
496 is_completed = 1;
497 }
498 if (status & NDSR_FLASH_RDY)
eric miaofe69af02008-02-14 15:48:23 +0800499 info->state = STATE_READY;
Lei Wenf8155a42011-02-28 10:32:11 +0800500
501 if (status & NDSR_WRCMDREQ) {
502 nand_writel(info, NDSR, NDSR_WRCMDREQ);
503 status &= ~NDSR_WRCMDREQ;
504 info->state = STATE_CMD_HANDLE;
505 nand_writel(info, NDCB0, info->ndcb0);
506 nand_writel(info, NDCB0, info->ndcb1);
507 nand_writel(info, NDCB0, info->ndcb2);
eric miaofe69af02008-02-14 15:48:23 +0800508 }
Lei Wenf8155a42011-02-28 10:32:11 +0800509
510 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800511 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800512 if (is_completed)
513 complete(&info->cmd_complete);
514NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800515 return IRQ_HANDLED;
516}
517
eric miaofe69af02008-02-14 15:48:23 +0800518static int pxa3xx_nand_dev_ready(struct mtd_info *mtd)
519{
520 struct pxa3xx_nand_info *info = mtd->priv;
521 return (nand_readl(info, NDSR) & NDSR_RDY) ? 1 : 0;
522}
523
524static inline int is_buf_blank(uint8_t *buf, size_t len)
525{
526 for (; len > 0; len--)
527 if (*buf++ != 0xff)
528 return 0;
529 return 1;
530}
531
532static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100533 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800534{
535 struct pxa3xx_nand_info *info = mtd->priv;
Lei Wen18c81b12010-08-17 17:25:57 +0800536 const struct pxa3xx_nand_cmdset *cmdset = info->cmdset;
Lei Wenf8155a42011-02-28 10:32:11 +0800537 int ret, exec_cmd = 0;
eric miaofe69af02008-02-14 15:48:23 +0800538
539 info->use_dma = (use_dma) ? 1 : 0;
540 info->use_ecc = 0;
541 info->data_size = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800542 info->state = 0;
543 info->retcode = ERR_NONE;
eric miaofe69af02008-02-14 15:48:23 +0800544
545 switch (command) {
546 case NAND_CMD_READOOB:
547 /* disable HW ECC to get all the OOB data */
548 info->buf_count = mtd->writesize + mtd->oobsize;
549 info->buf_start = mtd->writesize + column;
Haojian Zhuang7ce33af2009-09-14 20:21:01 +0800550 memset(info->data_buff, 0xFF, info->buf_count);
eric miaofe69af02008-02-14 15:48:23 +0800551
Lei Wenf8155a42011-02-28 10:32:11 +0800552 prepare_read_prog_cmd(info, cmdset->read1, column, page_addr);
553 exec_cmd = 1;
eric miaofe69af02008-02-14 15:48:23 +0800554 break;
555
556 case NAND_CMD_READ0:
557 info->use_ecc = 1;
eric miaofe69af02008-02-14 15:48:23 +0800558 info->buf_start = column;
559 info->buf_count = mtd->writesize + mtd->oobsize;
560 memset(info->data_buff, 0xFF, info->buf_count);
561
Lei Wenf8155a42011-02-28 10:32:11 +0800562 prepare_read_prog_cmd(info, cmdset->read1, column, page_addr);
563 exec_cmd = 1;
eric miaofe69af02008-02-14 15:48:23 +0800564 break;
565 case NAND_CMD_SEQIN:
566 info->buf_start = column;
567 info->buf_count = mtd->writesize + mtd->oobsize;
568 memset(info->data_buff, 0xff, info->buf_count);
569
570 /* save column/page_addr for next CMD_PAGEPROG */
571 info->seqin_column = column;
572 info->seqin_page_addr = page_addr;
573 break;
574 case NAND_CMD_PAGEPROG:
575 info->use_ecc = (info->seqin_column >= mtd->writesize) ? 0 : 1;
576
Lei Wenf8155a42011-02-28 10:32:11 +0800577 prepare_read_prog_cmd(info, cmdset->program,
578 info->seqin_column, info->seqin_page_addr);
579 exec_cmd = 1;
eric miaofe69af02008-02-14 15:48:23 +0800580 break;
581 case NAND_CMD_ERASE1:
Lei Wenf8155a42011-02-28 10:32:11 +0800582 prepare_erase_cmd(info, cmdset->erase, page_addr);
583 exec_cmd = 1;
eric miaofe69af02008-02-14 15:48:23 +0800584 break;
585 case NAND_CMD_ERASE2:
586 break;
587 case NAND_CMD_READID:
588 case NAND_CMD_STATUS:
589 info->use_dma = 0; /* force PIO read */
590 info->buf_start = 0;
591 info->buf_count = (command == NAND_CMD_READID) ?
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200592 info->read_id_bytes : 1;
eric miaofe69af02008-02-14 15:48:23 +0800593
Lei Wenf8155a42011-02-28 10:32:11 +0800594 prepare_other_cmd(info, (command == NAND_CMD_READID) ?
595 cmdset->read_id : cmdset->read_status);
596 exec_cmd = 1;
eric miaofe69af02008-02-14 15:48:23 +0800597 break;
598 case NAND_CMD_RESET:
Lei Wenf8155a42011-02-28 10:32:11 +0800599 prepare_other_cmd(info, cmdset->reset);
600 exec_cmd = 1;
eric miaofe69af02008-02-14 15:48:23 +0800601 break;
602 default:
603 printk(KERN_ERR "non-supported command.\n");
604 break;
605 }
606
Lei Wenf8155a42011-02-28 10:32:11 +0800607 if (exec_cmd) {
608 init_completion(&info->cmd_complete);
609 pxa3xx_nand_start(info);
610
611 ret = wait_for_completion_timeout(&info->cmd_complete,
612 CHIP_DELAY_TIMEOUT);
613 if (!ret) {
614 printk(KERN_ERR "Wait time out!!!\n");
615 /* Stop State Machine for next command cycle */
616 pxa3xx_nand_stop(info);
617 }
618 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800619 }
620}
621
Lei Wenf8155a42011-02-28 10:32:11 +0800622static void pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
623 struct nand_chip *chip, const uint8_t *buf)
624{
625 chip->write_buf(mtd, buf, mtd->writesize);
626 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
627}
628
629static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
630 struct nand_chip *chip, uint8_t *buf, int page)
631{
632 struct pxa3xx_nand_info *info = mtd->priv;
633
634 chip->read_buf(mtd, buf, mtd->writesize);
635 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
636
637 if (info->retcode == ERR_SBERR) {
638 switch (info->use_ecc) {
639 case 1:
640 mtd->ecc_stats.corrected++;
641 break;
642 case 0:
643 default:
644 break;
645 }
646 } else if (info->retcode == ERR_DBERR) {
647 /*
648 * for blank page (all 0xff), HW will calculate its ECC as
649 * 0, which is different from the ECC information within
650 * OOB, ignore such double bit errors
651 */
652 if (is_buf_blank(buf, mtd->writesize))
653 mtd->ecc_stats.failed++;
654 }
655
656 return 0;
657}
658
eric miaofe69af02008-02-14 15:48:23 +0800659static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
660{
661 struct pxa3xx_nand_info *info = mtd->priv;
662 char retval = 0xFF;
663
664 if (info->buf_start < info->buf_count)
665 /* Has just send a new command? */
666 retval = info->data_buff[info->buf_start++];
667
668 return retval;
669}
670
671static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
672{
673 struct pxa3xx_nand_info *info = mtd->priv;
674 u16 retval = 0xFFFF;
675
676 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
677 retval = *((u16 *)(info->data_buff+info->buf_start));
678 info->buf_start += 2;
679 }
680 return retval;
681}
682
683static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
684{
685 struct pxa3xx_nand_info *info = mtd->priv;
686 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
687
688 memcpy(buf, info->data_buff + info->buf_start, real_len);
689 info->buf_start += real_len;
690}
691
692static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
693 const uint8_t *buf, int len)
694{
695 struct pxa3xx_nand_info *info = mtd->priv;
696 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
697
698 memcpy(info->data_buff + info->buf_start, buf, real_len);
699 info->buf_start += real_len;
700}
701
702static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
703 const uint8_t *buf, int len)
704{
705 return 0;
706}
707
708static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
709{
710 return;
711}
712
713static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
714{
715 struct pxa3xx_nand_info *info = mtd->priv;
716
717 /* pxa3xx_nand_send_command has waited for command complete */
718 if (this->state == FL_WRITING || this->state == FL_ERASING) {
719 if (info->retcode == ERR_NONE)
720 return 0;
721 else {
722 /*
723 * any error make it return 0x01 which will tell
724 * the caller the erase and write fail
725 */
726 return 0x01;
727 }
728 }
729
730 return 0;
731}
732
eric miaofe69af02008-02-14 15:48:23 +0800733static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200734 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800735{
736 struct platform_device *pdev = info->pdev;
737 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800738 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800739
740 if (f->page_size != 2048 && f->page_size != 512)
741 return -EINVAL;
742
743 if (f->flash_width != 16 && f->flash_width != 8)
744 return -EINVAL;
745
746 /* calculate flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800747 info->cmdset = f->cmdset;
748 info->page_size = f->page_size;
749 info->oob_buff = info->data_buff + f->page_size;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200750 info->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800751
752 /* calculate addressing information */
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200753 info->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800754
755 if (f->num_blocks * f->page_per_block > 65536)
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200756 info->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800757 else
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200758 info->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800759
760 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200761 ndcr |= (info->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800762 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
763 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
764 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
765 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
766
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200767 ndcr |= NDCR_RD_ID_CNT(info->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800768 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
769
770 info->reg_ndcr = ndcr;
771
772 pxa3xx_nand_set_timing(info, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800773 return 0;
774}
775
Mike Rapoportf2710492009-02-17 13:54:47 +0200776static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
777{
778 uint32_t ndcr = nand_readl(info, NDCR);
779 struct nand_flash_dev *type = NULL;
Lei Wen18c81b12010-08-17 17:25:57 +0800780 uint32_t id = -1, page_per_block, num_blocks;
Mike Rapoportf2710492009-02-17 13:54:47 +0200781 int i;
782
Lei Wen18c81b12010-08-17 17:25:57 +0800783 page_per_block = ndcr & NDCR_PG_PER_BLK ? 64 : 32;
784 info->page_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Lei Wen38caf7f2011-02-28 10:32:12 +0800785 /* set info fields needed to read id */
Lei Wen18c81b12010-08-17 17:25:57 +0800786 info->read_id_bytes = (info->page_size == 2048) ? 4 : 2;
Mike Rapoportf2710492009-02-17 13:54:47 +0200787 info->reg_ndcr = ndcr;
Dan Carpenter52d039f2011-01-06 17:05:36 +0300788 info->cmdset = &default_cmdset;
Mike Rapoportf2710492009-02-17 13:54:47 +0200789
Lei Wenf8155a42011-02-28 10:32:11 +0800790 pxa3xx_nand_cmdfunc(info->mtd, NAND_CMD_READID, 0, 0);
791 id = *((uint16_t *)(info->data_buff));
792 if (id == 0)
Mike Rapoportf2710492009-02-17 13:54:47 +0200793 return -ENODEV;
794
795 /* Lookup the flash id */
Mike Rapoportf2710492009-02-17 13:54:47 +0200796 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
797 if (id == nand_flash_ids[i].id) {
798 type = &nand_flash_ids[i];
799 break;
800 }
801 }
802
803 if (!type)
804 return -ENODEV;
805
806 /* fill the missing flash information */
Lei Wen18c81b12010-08-17 17:25:57 +0800807 i = __ffs(page_per_block * info->page_size);
808 num_blocks = type->chipsize << (20 - i);
Mike Rapoportf2710492009-02-17 13:54:47 +0200809
Mike Rapoportf2710492009-02-17 13:54:47 +0200810 /* calculate addressing information */
Lei Wen18c81b12010-08-17 17:25:57 +0800811 info->col_addr_cycles = (info->page_size == 2048) ? 2 : 1;
Mike Rapoportf2710492009-02-17 13:54:47 +0200812
Lei Wen18c81b12010-08-17 17:25:57 +0800813 if (num_blocks * page_per_block > 65536)
Mike Rapoportf2710492009-02-17 13:54:47 +0200814 info->row_addr_cycles = 3;
815 else
816 info->row_addr_cycles = 2;
817
Lei Wen18c81b12010-08-17 17:25:57 +0800818 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
819 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200820
821 return 0;
822}
823
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200824static int pxa3xx_nand_detect_flash(struct pxa3xx_nand_info *info,
825 const struct pxa3xx_nand_platform_data *pdata)
eric miaofe69af02008-02-14 15:48:23 +0800826{
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200827 const struct pxa3xx_nand_flash *f;
Enrico Scholz2675e942008-08-29 12:59:52 +0200828 uint32_t id = -1;
eric miaofe69af02008-02-14 15:48:23 +0800829 int i;
830
Mike Rapoportf2710492009-02-17 13:54:47 +0200831 if (pdata->keep_config)
832 if (pxa3xx_nand_detect_config(info) == 0)
833 return 0;
834
Lei Wen227a8862010-08-18 18:00:03 +0800835 /* we use default timing to detect id */
836 f = DEFAULT_FLASH_TYPE;
837 pxa3xx_nand_config_flash(info, f);
Lei Wenf8155a42011-02-28 10:32:11 +0800838 pxa3xx_nand_cmdfunc(info->mtd, NAND_CMD_READID, 0, 0);
839 id = *((uint16_t *)(info->data_buff));
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200840
Lei Wen227a8862010-08-18 18:00:03 +0800841 for (i=0; i<ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1; i++) {
842 /* we first choose the flash definition from platfrom */
843 if (i < pdata->num_flash)
844 f = pdata->flash + i;
845 else
846 f = &builtin_flash_types[i - pdata->num_flash + 1];
847 if (f->chip_id == id) {
848 dev_info(&info->pdev->dev, "detect chip id: 0x%x\n", id);
849 pxa3xx_nand_config_flash(info, f);
Enrico Scholzc8ac3f82008-08-29 12:59:48 +0200850 return 0;
Lei Wen227a8862010-08-18 18:00:03 +0800851 }
eric miaofe69af02008-02-14 15:48:23 +0800852 }
853
Enrico Scholz2675e942008-08-29 12:59:52 +0200854 dev_warn(&info->pdev->dev,
855 "failed to detect configured nand flash; found %04x instead of\n",
856 id);
eric miaofe69af02008-02-14 15:48:23 +0800857 return -ENODEV;
858}
859
860/* the maximum possible buffer size for large page with OOB data
861 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
862 * data buffer and the DMA descriptor
863 */
864#define MAX_BUFF_SIZE PAGE_SIZE
865
866static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
867{
868 struct platform_device *pdev = info->pdev;
869 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
870
871 if (use_dma == 0) {
872 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
873 if (info->data_buff == NULL)
874 return -ENOMEM;
875 return 0;
876 }
877
878 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
879 &info->data_buff_phys, GFP_KERNEL);
880 if (info->data_buff == NULL) {
881 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
882 return -ENOMEM;
883 }
884
885 info->data_buff_size = MAX_BUFF_SIZE;
886 info->data_desc = (void *)info->data_buff + data_desc_offset;
887 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
888
889 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
890 pxa3xx_nand_data_dma_irq, info);
891 if (info->data_dma_ch < 0) {
892 dev_err(&pdev->dev, "failed to request data dma\n");
893 dma_free_coherent(&pdev->dev, info->data_buff_size,
894 info->data_buff, info->data_buff_phys);
895 return info->data_dma_ch;
896 }
897
898 return 0;
899}
900
901static struct nand_ecclayout hw_smallpage_ecclayout = {
902 .eccbytes = 6,
903 .eccpos = {8, 9, 10, 11, 12, 13 },
904 .oobfree = { {2, 6} }
905};
906
907static struct nand_ecclayout hw_largepage_ecclayout = {
908 .eccbytes = 24,
909 .eccpos = {
910 40, 41, 42, 43, 44, 45, 46, 47,
911 48, 49, 50, 51, 52, 53, 54, 55,
912 56, 57, 58, 59, 60, 61, 62, 63},
913 .oobfree = { {2, 38} }
914};
915
916static void pxa3xx_nand_init_mtd(struct mtd_info *mtd,
917 struct pxa3xx_nand_info *info)
918{
eric miaofe69af02008-02-14 15:48:23 +0800919 struct nand_chip *this = &info->nand_chip;
920
Lei Wen18c81b12010-08-17 17:25:57 +0800921 this->options = (info->reg_ndcr & NDCR_DWIDTH_C) ? NAND_BUSWIDTH_16: 0;
eric miaofe69af02008-02-14 15:48:23 +0800922
923 this->waitfunc = pxa3xx_nand_waitfunc;
924 this->select_chip = pxa3xx_nand_select_chip;
925 this->dev_ready = pxa3xx_nand_dev_ready;
926 this->cmdfunc = pxa3xx_nand_cmdfunc;
Lei Wenf8155a42011-02-28 10:32:11 +0800927 this->ecc.read_page = pxa3xx_nand_read_page_hwecc;
928 this->ecc.write_page = pxa3xx_nand_write_page_hwecc;
eric miaofe69af02008-02-14 15:48:23 +0800929 this->read_word = pxa3xx_nand_read_word;
930 this->read_byte = pxa3xx_nand_read_byte;
931 this->read_buf = pxa3xx_nand_read_buf;
932 this->write_buf = pxa3xx_nand_write_buf;
933 this->verify_buf = pxa3xx_nand_verify_buf;
934
935 this->ecc.mode = NAND_ECC_HW;
Lei Wen18c81b12010-08-17 17:25:57 +0800936 this->ecc.size = info->page_size;
eric miaofe69af02008-02-14 15:48:23 +0800937
Lei Wen18c81b12010-08-17 17:25:57 +0800938 if (info->page_size == 2048)
eric miaofe69af02008-02-14 15:48:23 +0800939 this->ecc.layout = &hw_largepage_ecclayout;
940 else
941 this->ecc.layout = &hw_smallpage_ecclayout;
942
David Woodhousea1c06ee2008-04-22 20:39:43 +0100943 this->chip_delay = 25;
eric miaofe69af02008-02-14 15:48:23 +0800944}
945
Lei Wene353a202011-03-03 11:08:30 +0800946static
947struct pxa3xx_nand_info *alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +0800948{
Lei Wene353a202011-03-03 11:08:30 +0800949 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
eric miaofe69af02008-02-14 15:48:23 +0800950 struct pxa3xx_nand_info *info;
eric miaofe69af02008-02-14 15:48:23 +0800951 struct mtd_info *mtd;
952 struct resource *r;
Lei Wene353a202011-03-03 11:08:30 +0800953 int ret, irq;
eric miaofe69af02008-02-14 15:48:23 +0800954
955 mtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct pxa3xx_nand_info),
956 GFP_KERNEL);
David Woodhousea1c06ee2008-04-22 20:39:43 +0100957 if (!mtd) {
eric miaofe69af02008-02-14 15:48:23 +0800958 dev_err(&pdev->dev, "failed to allocate memory\n");
Lei Wene353a202011-03-03 11:08:30 +0800959 return NULL;
David Woodhousea1c06ee2008-04-22 20:39:43 +0100960 }
eric miaofe69af02008-02-14 15:48:23 +0800961
962 info = (struct pxa3xx_nand_info *)(&mtd[1]);
963 info->pdev = pdev;
964
eric miaofe69af02008-02-14 15:48:23 +0800965 mtd->priv = info;
Lei Wene353a202011-03-03 11:08:30 +0800966 info->mtd = mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +0200967 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +0800968
Russell Kinge0d8b132008-11-11 17:52:32 +0000969 info->clk = clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +0800970 if (IS_ERR(info->clk)) {
971 dev_err(&pdev->dev, "failed to get nand clock\n");
972 ret = PTR_ERR(info->clk);
973 goto fail_free_mtd;
974 }
975 clk_enable(info->clk);
976
977 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
978 if (r == NULL) {
979 dev_err(&pdev->dev, "no resource defined for data DMA\n");
980 ret = -ENXIO;
981 goto fail_put_clk;
982 }
983 info->drcmr_dat = r->start;
984
985 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
986 if (r == NULL) {
987 dev_err(&pdev->dev, "no resource defined for command DMA\n");
988 ret = -ENXIO;
989 goto fail_put_clk;
990 }
991 info->drcmr_cmd = r->start;
992
993 irq = platform_get_irq(pdev, 0);
994 if (irq < 0) {
995 dev_err(&pdev->dev, "no IRQ resource defined\n");
996 ret = -ENXIO;
997 goto fail_put_clk;
998 }
999
1000 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1001 if (r == NULL) {
1002 dev_err(&pdev->dev, "no IO memory resource defined\n");
1003 ret = -ENODEV;
1004 goto fail_put_clk;
1005 }
1006
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001007 r = request_mem_region(r->start, resource_size(r), pdev->name);
eric miaofe69af02008-02-14 15:48:23 +08001008 if (r == NULL) {
1009 dev_err(&pdev->dev, "failed to request memory resource\n");
1010 ret = -EBUSY;
1011 goto fail_put_clk;
1012 }
1013
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001014 info->mmio_base = ioremap(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001015 if (info->mmio_base == NULL) {
1016 dev_err(&pdev->dev, "ioremap() failed\n");
1017 ret = -ENODEV;
1018 goto fail_free_res;
1019 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001020 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001021
1022 ret = pxa3xx_nand_init_buff(info);
1023 if (ret)
1024 goto fail_free_io;
1025
Haojian Zhuang346e1252009-09-10 14:27:23 +08001026 /* initialize all interrupts to be disabled */
1027 disable_int(info, NDSR_MASK);
1028
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001029 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1030 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001031 if (ret < 0) {
1032 dev_err(&pdev->dev, "failed to request IRQ\n");
1033 goto fail_free_buf;
1034 }
1035
Enrico Scholzc8ac3f82008-08-29 12:59:48 +02001036 ret = pxa3xx_nand_detect_flash(info, pdata);
eric miaofe69af02008-02-14 15:48:23 +08001037 if (ret) {
1038 dev_err(&pdev->dev, "failed to detect flash\n");
1039 ret = -ENODEV;
1040 goto fail_free_irq;
1041 }
1042
1043 pxa3xx_nand_init_mtd(mtd, info);
Lei Wene353a202011-03-03 11:08:30 +08001044 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001045
Lei Wene353a202011-03-03 11:08:30 +08001046 return info;
eric miaofe69af02008-02-14 15:48:23 +08001047
1048fail_free_irq:
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001049 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001050fail_free_buf:
1051 if (use_dma) {
1052 pxa_free_dma(info->data_dma_ch);
1053 dma_free_coherent(&pdev->dev, info->data_buff_size,
1054 info->data_buff, info->data_buff_phys);
1055 } else
1056 kfree(info->data_buff);
1057fail_free_io:
1058 iounmap(info->mmio_base);
1059fail_free_res:
Mike Rapoportb2ed3682009-02-17 13:54:45 +02001060 release_mem_region(r->start, resource_size(r));
eric miaofe69af02008-02-14 15:48:23 +08001061fail_put_clk:
1062 clk_disable(info->clk);
1063 clk_put(info->clk);
1064fail_free_mtd:
1065 kfree(mtd);
Lei Wene353a202011-03-03 11:08:30 +08001066 return NULL;
eric miaofe69af02008-02-14 15:48:23 +08001067}
1068
1069static int pxa3xx_nand_remove(struct platform_device *pdev)
1070{
Lei Wene353a202011-03-03 11:08:30 +08001071 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1072 struct mtd_info *mtd = info->mtd;
Mike Rapoport82a72d12009-02-17 13:54:46 +02001073 struct resource *r;
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001074 int irq;
eric miaofe69af02008-02-14 15:48:23 +08001075
1076 platform_set_drvdata(pdev, NULL);
1077
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001078 irq = platform_get_irq(pdev, 0);
1079 if (irq >= 0)
1080 free_irq(irq, info);
eric miaofe69af02008-02-14 15:48:23 +08001081 if (use_dma) {
1082 pxa_free_dma(info->data_dma_ch);
1083 dma_free_writecombine(&pdev->dev, info->data_buff_size,
1084 info->data_buff, info->data_buff_phys);
1085 } else
1086 kfree(info->data_buff);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001087
1088 iounmap(info->mmio_base);
1089 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1090 release_mem_region(r->start, resource_size(r));
1091
1092 clk_disable(info->clk);
1093 clk_put(info->clk);
1094
Lei Wenf8155a42011-02-28 10:32:11 +08001095 if (mtd) {
1096 del_mtd_device(mtd);
1097#ifdef CONFIG_MTD_PARTITIONS
1098 del_mtd_partitions(mtd);
1099#endif
1100 kfree(mtd);
1101 }
eric miaofe69af02008-02-14 15:48:23 +08001102 return 0;
1103}
1104
Lei Wene353a202011-03-03 11:08:30 +08001105static int pxa3xx_nand_probe(struct platform_device *pdev)
1106{
1107 struct pxa3xx_nand_platform_data *pdata;
1108 struct pxa3xx_nand_info *info;
1109
1110 pdata = pdev->dev.platform_data;
1111 if (!pdata) {
1112 dev_err(&pdev->dev, "no platform data defined\n");
1113 return -ENODEV;
1114 }
1115
1116 info = alloc_nand_resource(pdev);
1117 if (info == NULL)
1118 return -ENOMEM;
1119
1120 if (nand_scan(info->mtd, 1)) {
1121 dev_err(&pdev->dev, "failed to scan nand\n");
1122 pxa3xx_nand_remove(pdev);
1123 return -ENODEV;
1124 }
1125
1126#ifdef CONFIG_MTD_PARTITIONS
1127 if (mtd_has_cmdlinepart()) {
1128 const char *probes[] = { "cmdlinepart", NULL };
1129 struct mtd_partition *parts;
1130 int nr_parts;
1131
1132 nr_parts = parse_mtd_partitions(info->mtd, probes, &parts, 0);
1133
1134 if (nr_parts)
Lei Wenf8155a42011-02-28 10:32:11 +08001135 return add_mtd_partitions(info->mtd, parts, nr_parts);
Lei Wene353a202011-03-03 11:08:30 +08001136 }
1137
Lei Wenf8155a42011-02-28 10:32:11 +08001138 return add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
Lei Wene353a202011-03-03 11:08:30 +08001139#else
1140 return 0;
1141#endif
1142}
1143
eric miaofe69af02008-02-14 15:48:23 +08001144#ifdef CONFIG_PM
1145static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1146{
Lei Wene353a202011-03-03 11:08:30 +08001147 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1148 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001149
Lei Wenf8155a42011-02-28 10:32:11 +08001150 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001151 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1152 return -EAGAIN;
1153 }
1154
1155 return 0;
1156}
1157
1158static int pxa3xx_nand_resume(struct platform_device *pdev)
1159{
Lei Wene353a202011-03-03 11:08:30 +08001160 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
1161 struct mtd_info *mtd = info->mtd;
eric miaofe69af02008-02-14 15:48:23 +08001162
Lei Wen18c81b12010-08-17 17:25:57 +08001163 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1164 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
eric miaofe69af02008-02-14 15:48:23 +08001165 clk_enable(info->clk);
1166
Lei Wen18c81b12010-08-17 17:25:57 +08001167 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001168}
1169#else
1170#define pxa3xx_nand_suspend NULL
1171#define pxa3xx_nand_resume NULL
1172#endif
1173
1174static struct platform_driver pxa3xx_nand_driver = {
1175 .driver = {
1176 .name = "pxa3xx-nand",
1177 },
1178 .probe = pxa3xx_nand_probe,
1179 .remove = pxa3xx_nand_remove,
1180 .suspend = pxa3xx_nand_suspend,
1181 .resume = pxa3xx_nand_resume,
1182};
1183
1184static int __init pxa3xx_nand_init(void)
1185{
1186 return platform_driver_register(&pxa3xx_nand_driver);
1187}
1188module_init(pxa3xx_nand_init);
1189
1190static void __exit pxa3xx_nand_exit(void)
1191{
1192 platform_driver_unregister(&pxa3xx_nand_driver);
1193}
1194module_exit(pxa3xx_nand_exit);
1195
1196MODULE_LICENSE("GPL");
1197MODULE_DESCRIPTION("PXA3xx NAND controller driver");