mtd: nand: teach write_page and write_page_raw return an error code
[deliverable/linux.git] / drivers / mtd / nand / pxa3xx_nand.c
CommitLineData
fe69af00 1/*
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
a88bdbb5 12#include <linux/kernel.h>
fe69af00 13#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>
a1c06ee1
DW
22#include <linux/io.h>
23#include <linux/irq.h>
5a0e3ad6 24#include <linux/slab.h>
fe69af00 25
afb5b5c9 26#include <mach/dma.h>
82b95ecb 27#include <plat/pxa3xx_nand.h>
fe69af00 28
29#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
f8155a40 30#define NAND_STOP_DELAY (2 * HZ/50)
4eb2da89 31#define PAGE_CHUNK_SIZE (2048)
fe69af00 32
33/* registers and bit definitions */
34#define NDCR (0x00) /* Control register */
35#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
36#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
37#define NDSR (0x14) /* Status Register */
38#define NDPCR (0x18) /* Page Count Register */
39#define NDBDR0 (0x1C) /* Bad Block Register 0 */
40#define NDBDR1 (0x20) /* Bad Block Register 1 */
41#define NDDB (0x40) /* Data Buffer */
42#define NDCB0 (0x48) /* Command Buffer0 */
43#define NDCB1 (0x4C) /* Command Buffer1 */
44#define NDCB2 (0x50) /* Command Buffer2 */
45
46#define NDCR_SPARE_EN (0x1 << 31)
47#define NDCR_ECC_EN (0x1 << 30)
48#define NDCR_DMA_EN (0x1 << 29)
49#define NDCR_ND_RUN (0x1 << 28)
50#define NDCR_DWIDTH_C (0x1 << 27)
51#define NDCR_DWIDTH_M (0x1 << 26)
52#define NDCR_PAGE_SZ (0x1 << 24)
53#define NDCR_NCSX (0x1 << 23)
54#define NDCR_ND_MODE (0x3 << 21)
55#define NDCR_NAND_MODE (0x0)
56#define NDCR_CLR_PG_CNT (0x1 << 20)
f8155a40 57#define NDCR_STOP_ON_UNCOR (0x1 << 19)
fe69af00 58#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
59#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
60
61#define NDCR_RA_START (0x1 << 15)
62#define NDCR_PG_PER_BLK (0x1 << 14)
63#define NDCR_ND_ARB_EN (0x1 << 12)
f8155a40 64#define NDCR_INT_MASK (0xFFF)
fe69af00 65
66#define NDSR_MASK (0xfff)
f8155a40
LW
67#define NDSR_RDY (0x1 << 12)
68#define NDSR_FLASH_RDY (0x1 << 11)
fe69af00 69#define NDSR_CS0_PAGED (0x1 << 10)
70#define NDSR_CS1_PAGED (0x1 << 9)
71#define NDSR_CS0_CMDD (0x1 << 8)
72#define NDSR_CS1_CMDD (0x1 << 7)
73#define NDSR_CS0_BBD (0x1 << 6)
74#define NDSR_CS1_BBD (0x1 << 5)
75#define NDSR_DBERR (0x1 << 4)
76#define NDSR_SBERR (0x1 << 3)
77#define NDSR_WRDREQ (0x1 << 2)
78#define NDSR_RDDREQ (0x1 << 1)
79#define NDSR_WRCMDREQ (0x1)
80
4eb2da89 81#define NDCB0_ST_ROW_EN (0x1 << 26)
fe69af00 82#define NDCB0_AUTO_RS (0x1 << 25)
83#define NDCB0_CSEL (0x1 << 24)
84#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
85#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
86#define NDCB0_NC (0x1 << 20)
87#define NDCB0_DBC (0x1 << 19)
88#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
89#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
90#define NDCB0_CMD2_MASK (0xff << 8)
91#define NDCB0_CMD1_MASK (0xff)
92#define NDCB0_ADDR_CYC_SHIFT (16)
93
fe69af00 94/* macros for registers read/write */
95#define nand_writel(info, off, val) \
96 __raw_writel((val), (info)->mmio_base + (off))
97
98#define nand_readl(info, off) \
99 __raw_readl((info)->mmio_base + (off))
100
101/* error code and state */
102enum {
103 ERR_NONE = 0,
104 ERR_DMABUSERR = -1,
105 ERR_SENDCMD = -2,
106 ERR_DBERR = -3,
107 ERR_BBERR = -4,
223cf6c3 108 ERR_SBERR = -5,
fe69af00 109};
110
111enum {
f8155a40 112 STATE_IDLE = 0,
d456882b 113 STATE_PREPARED,
fe69af00 114 STATE_CMD_HANDLE,
115 STATE_DMA_READING,
116 STATE_DMA_WRITING,
117 STATE_DMA_DONE,
118 STATE_PIO_READING,
119 STATE_PIO_WRITING,
f8155a40
LW
120 STATE_CMD_DONE,
121 STATE_READY,
fe69af00 122};
123
d456882b
LW
124struct pxa3xx_nand_host {
125 struct nand_chip chip;
126 struct pxa3xx_nand_cmdset *cmdset;
127 struct mtd_info *mtd;
128 void *info_data;
129
130 /* page size of attached chip */
131 unsigned int page_size;
132 int use_ecc;
f3c8cfc2 133 int cs;
fe69af00 134
d456882b
LW
135 /* calculated from pxa3xx_nand_flash data */
136 unsigned int col_addr_cycles;
137 unsigned int row_addr_cycles;
138 size_t read_id_bytes;
139
140 /* cached register value */
141 uint32_t reg_ndcr;
142 uint32_t ndtr0cs0;
143 uint32_t ndtr1cs0;
144};
145
146struct pxa3xx_nand_info {
401e67e2 147 struct nand_hw_control controller;
fe69af00 148 struct platform_device *pdev;
fe69af00 149
150 struct clk *clk;
151 void __iomem *mmio_base;
8638fac8 152 unsigned long mmio_phys;
d456882b 153 struct completion cmd_complete;
fe69af00 154
155 unsigned int buf_start;
156 unsigned int buf_count;
157
158 /* DMA information */
159 int drcmr_dat;
160 int drcmr_cmd;
161
162 unsigned char *data_buff;
18c81b18 163 unsigned char *oob_buff;
fe69af00 164 dma_addr_t data_buff_phys;
fe69af00 165 int data_dma_ch;
166 struct pxa_dma_desc *data_desc;
167 dma_addr_t data_desc_addr;
168
f3c8cfc2 169 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
fe69af00 170 unsigned int state;
171
f3c8cfc2 172 int cs;
fe69af00 173 int use_ecc; /* use HW ECC ? */
174 int use_dma; /* use DMA ? */
401e67e2 175 int is_ready;
fe69af00 176
18c81b18
LW
177 unsigned int page_size; /* page size of attached chip */
178 unsigned int data_size; /* data size in FIFO */
d456882b 179 unsigned int oob_size;
fe69af00 180 int retcode;
fe69af00 181
182 /* generated NDCBx register values */
183 uint32_t ndcb0;
184 uint32_t ndcb1;
185 uint32_t ndcb2;
186};
187
90ab5ee9 188static bool use_dma = 1;
fe69af00 189module_param(use_dma, bool, 0444);
25985edc 190MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
fe69af00 191
f271049e
MR
192/*
193 * Default NAND flash controller configuration setup by the
194 * bootloader. This configuration is used only when pdata->keep_config is set
195 */
c1f82478 196static struct pxa3xx_nand_cmdset default_cmdset = {
fe69af00 197 .read1 = 0x3000,
198 .read2 = 0x0050,
199 .program = 0x1080,
200 .read_status = 0x0070,
201 .read_id = 0x0090,
202 .erase = 0xD060,
203 .reset = 0x00FF,
204 .lock = 0x002A,
205 .unlock = 0x2423,
206 .lock_status = 0x007A,
207};
208
c1f82478 209static struct pxa3xx_nand_timing timing[] = {
227a886c
LW
210 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
211 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
212 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
213 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
d3490dfd
HZ
214};
215
c1f82478 216static struct pxa3xx_nand_flash builtin_flash_types[] = {
4332c116
LW
217{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
218{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
219{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
220{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
221{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
222{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
223{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
224{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
225{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
d3490dfd
HZ
226};
227
227a886c
LW
228/* Define a default flash type setting serve as flash detecting only */
229#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
230
f3c8cfc2 231const char *mtd_names[] = {"pxa3xx_nand-0", "pxa3xx_nand-1", NULL};
401e67e2 232
fe69af00 233#define NDTR0_tCH(c) (min((c), 7) << 19)
234#define NDTR0_tCS(c) (min((c), 7) << 16)
235#define NDTR0_tWH(c) (min((c), 7) << 11)
236#define NDTR0_tWP(c) (min((c), 7) << 8)
237#define NDTR0_tRH(c) (min((c), 7) << 3)
238#define NDTR0_tRP(c) (min((c), 7) << 0)
239
240#define NDTR1_tR(c) (min((c), 65535) << 16)
241#define NDTR1_tWHR(c) (min((c), 15) << 4)
242#define NDTR1_tAR(c) (min((c), 15) << 0)
243
244/* convert nano-seconds to nand flash controller clock cycles */
93b352fc 245#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
fe69af00 246
d456882b 247static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
7dad482e 248 const struct pxa3xx_nand_timing *t)
fe69af00 249{
d456882b 250 struct pxa3xx_nand_info *info = host->info_data;
fe69af00 251 unsigned long nand_clk = clk_get_rate(info->clk);
252 uint32_t ndtr0, ndtr1;
253
254 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
255 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
256 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
257 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
258 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
259 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
260
261 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
262 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
263 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
264
d456882b
LW
265 host->ndtr0cs0 = ndtr0;
266 host->ndtr1cs0 = ndtr1;
fe69af00 267 nand_writel(info, NDTR0CS0, ndtr0);
268 nand_writel(info, NDTR1CS0, ndtr1);
269}
270
18c81b18 271static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
fe69af00 272{
f3c8cfc2 273 struct pxa3xx_nand_host *host = info->host[info->cs];
d456882b 274 int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
9d8b1043 275
d456882b 276 info->data_size = host->page_size;
9d8b1043
LW
277 if (!oob_enable) {
278 info->oob_size = 0;
279 return;
280 }
281
d456882b 282 switch (host->page_size) {
fe69af00 283 case 2048:
9d8b1043 284 info->oob_size = (info->use_ecc) ? 40 : 64;
fe69af00 285 break;
286 case 512:
9d8b1043 287 info->oob_size = (info->use_ecc) ? 8 : 16;
fe69af00 288 break;
fe69af00 289 }
18c81b18
LW
290}
291
f8155a40
LW
292/**
293 * NOTE: it is a must to set ND_RUN firstly, then write
294 * command buffer, otherwise, it does not work.
295 * We enable all the interrupt at the same time, and
296 * let pxa3xx_nand_irq to handle all logic.
297 */
298static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
299{
f3c8cfc2 300 struct pxa3xx_nand_host *host = info->host[info->cs];
f8155a40
LW
301 uint32_t ndcr;
302
d456882b 303 ndcr = host->reg_ndcr;
f8155a40
LW
304 ndcr |= info->use_ecc ? NDCR_ECC_EN : 0;
305 ndcr |= info->use_dma ? NDCR_DMA_EN : 0;
306 ndcr |= NDCR_ND_RUN;
307
308 /* clear status bits and run */
309 nand_writel(info, NDCR, 0);
310 nand_writel(info, NDSR, NDSR_MASK);
311 nand_writel(info, NDCR, ndcr);
312}
313
314static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
315{
316 uint32_t ndcr;
317 int timeout = NAND_STOP_DELAY;
318
319 /* wait RUN bit in NDCR become 0 */
320 ndcr = nand_readl(info, NDCR);
321 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
322 ndcr = nand_readl(info, NDCR);
323 udelay(1);
324 }
325
326 if (timeout <= 0) {
327 ndcr &= ~NDCR_ND_RUN;
328 nand_writel(info, NDCR, ndcr);
329 }
330 /* clear status bits */
331 nand_writel(info, NDSR, NDSR_MASK);
332}
333
fe69af00 334static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
335{
336 uint32_t ndcr;
337
338 ndcr = nand_readl(info, NDCR);
339 nand_writel(info, NDCR, ndcr & ~int_mask);
340}
341
342static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
343{
344 uint32_t ndcr;
345
346 ndcr = nand_readl(info, NDCR);
347 nand_writel(info, NDCR, ndcr | int_mask);
348}
349
f8155a40 350static void handle_data_pio(struct pxa3xx_nand_info *info)
fe69af00 351{
fe69af00 352 switch (info->state) {
353 case STATE_PIO_WRITING:
354 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
a88bdbb5 355 DIV_ROUND_UP(info->data_size, 4));
9d8b1043
LW
356 if (info->oob_size > 0)
357 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
358 DIV_ROUND_UP(info->oob_size, 4));
fe69af00 359 break;
360 case STATE_PIO_READING:
361 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
a88bdbb5 362 DIV_ROUND_UP(info->data_size, 4));
9d8b1043
LW
363 if (info->oob_size > 0)
364 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
365 DIV_ROUND_UP(info->oob_size, 4));
fe69af00 366 break;
367 default:
da675b4e 368 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
fe69af00 369 info->state);
f8155a40 370 BUG();
fe69af00 371 }
fe69af00 372}
373
f8155a40 374static void start_data_dma(struct pxa3xx_nand_info *info)
fe69af00 375{
376 struct pxa_dma_desc *desc = info->data_desc;
9d8b1043 377 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
fe69af00 378
379 desc->ddadr = DDADR_STOP;
380 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
381
f8155a40
LW
382 switch (info->state) {
383 case STATE_DMA_WRITING:
fe69af00 384 desc->dsadr = info->data_buff_phys;
8638fac8 385 desc->dtadr = info->mmio_phys + NDDB;
fe69af00 386 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
f8155a40
LW
387 break;
388 case STATE_DMA_READING:
fe69af00 389 desc->dtadr = info->data_buff_phys;
8638fac8 390 desc->dsadr = info->mmio_phys + NDDB;
fe69af00 391 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
f8155a40
LW
392 break;
393 default:
da675b4e 394 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
f8155a40
LW
395 info->state);
396 BUG();
fe69af00 397 }
398
399 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
400 DDADR(info->data_dma_ch) = info->data_desc_addr;
401 DCSR(info->data_dma_ch) |= DCSR_RUN;
402}
403
404static void pxa3xx_nand_data_dma_irq(int channel, void *data)
405{
406 struct pxa3xx_nand_info *info = data;
407 uint32_t dcsr;
408
409 dcsr = DCSR(channel);
410 DCSR(channel) = dcsr;
411
412 if (dcsr & DCSR_BUSERR) {
413 info->retcode = ERR_DMABUSERR;
fe69af00 414 }
415
f8155a40
LW
416 info->state = STATE_DMA_DONE;
417 enable_int(info, NDCR_INT_MASK);
418 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
fe69af00 419}
420
421static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
422{
423 struct pxa3xx_nand_info *info = devid;
f8155a40 424 unsigned int status, is_completed = 0;
f3c8cfc2
LW
425 unsigned int ready, cmd_done;
426
427 if (info->cs == 0) {
428 ready = NDSR_FLASH_RDY;
429 cmd_done = NDSR_CS0_CMDD;
430 } else {
431 ready = NDSR_RDY;
432 cmd_done = NDSR_CS1_CMDD;
433 }
fe69af00 434
435 status = nand_readl(info, NDSR);
436
f8155a40
LW
437 if (status & NDSR_DBERR)
438 info->retcode = ERR_DBERR;
439 if (status & NDSR_SBERR)
440 info->retcode = ERR_SBERR;
441 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
442 /* whether use dma to transfer data */
fe69af00 443 if (info->use_dma) {
f8155a40
LW
444 disable_int(info, NDCR_INT_MASK);
445 info->state = (status & NDSR_RDDREQ) ?
446 STATE_DMA_READING : STATE_DMA_WRITING;
447 start_data_dma(info);
448 goto NORMAL_IRQ_EXIT;
fe69af00 449 } else {
f8155a40
LW
450 info->state = (status & NDSR_RDDREQ) ?
451 STATE_PIO_READING : STATE_PIO_WRITING;
452 handle_data_pio(info);
fe69af00 453 }
fe69af00 454 }
f3c8cfc2 455 if (status & cmd_done) {
f8155a40
LW
456 info->state = STATE_CMD_DONE;
457 is_completed = 1;
fe69af00 458 }
f3c8cfc2 459 if (status & ready) {
401e67e2 460 info->is_ready = 1;
f8155a40 461 info->state = STATE_READY;
401e67e2 462 }
fe69af00 463
f8155a40
LW
464 if (status & NDSR_WRCMDREQ) {
465 nand_writel(info, NDSR, NDSR_WRCMDREQ);
466 status &= ~NDSR_WRCMDREQ;
467 info->state = STATE_CMD_HANDLE;
468 nand_writel(info, NDCB0, info->ndcb0);
469 nand_writel(info, NDCB0, info->ndcb1);
470 nand_writel(info, NDCB0, info->ndcb2);
fe69af00 471 }
472
f8155a40
LW
473 /* clear NDSR to let the controller exit the IRQ */
474 nand_writel(info, NDSR, status);
475 if (is_completed)
476 complete(&info->cmd_complete);
477NORMAL_IRQ_EXIT:
478 return IRQ_HANDLED;
fe69af00 479}
480
fe69af00 481static inline int is_buf_blank(uint8_t *buf, size_t len)
482{
483 for (; len > 0; len--)
484 if (*buf++ != 0xff)
485 return 0;
486 return 1;
487}
488
4eb2da89
LW
489static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
490 uint16_t column, int page_addr)
fe69af00 491{
4eb2da89 492 uint16_t cmd;
d456882b 493 int addr_cycle, exec_cmd;
f3c8cfc2
LW
494 struct pxa3xx_nand_host *host;
495 struct mtd_info *mtd;
fe69af00 496
f3c8cfc2
LW
497 host = info->host[info->cs];
498 mtd = host->mtd;
4eb2da89
LW
499 addr_cycle = 0;
500 exec_cmd = 1;
501
502 /* reset data and oob column point to handle data */
401e67e2
LW
503 info->buf_start = 0;
504 info->buf_count = 0;
4eb2da89
LW
505 info->oob_size = 0;
506 info->use_ecc = 0;
401e67e2 507 info->is_ready = 0;
4eb2da89 508 info->retcode = ERR_NONE;
f3c8cfc2
LW
509 if (info->cs != 0)
510 info->ndcb0 = NDCB0_CSEL;
511 else
512 info->ndcb0 = 0;
fe69af00 513
514 switch (command) {
4eb2da89
LW
515 case NAND_CMD_READ0:
516 case NAND_CMD_PAGEPROG:
517 info->use_ecc = 1;
fe69af00 518 case NAND_CMD_READOOB:
4eb2da89 519 pxa3xx_set_datasize(info);
fe69af00 520 break;
4eb2da89
LW
521 case NAND_CMD_SEQIN:
522 exec_cmd = 0;
523 break;
524 default:
525 info->ndcb1 = 0;
526 info->ndcb2 = 0;
527 break;
528 }
529
d456882b
LW
530 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
531 + host->col_addr_cycles);
fe69af00 532
4eb2da89
LW
533 switch (command) {
534 case NAND_CMD_READOOB:
fe69af00 535 case NAND_CMD_READ0:
d456882b 536 cmd = host->cmdset->read1;
4eb2da89
LW
537 if (command == NAND_CMD_READOOB)
538 info->buf_start = mtd->writesize + column;
539 else
540 info->buf_start = column;
541
d456882b 542 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
4eb2da89
LW
543 info->ndcb0 |= NDCB0_CMD_TYPE(0)
544 | addr_cycle
545 | (cmd & NDCB0_CMD1_MASK);
546 else
547 info->ndcb0 |= NDCB0_CMD_TYPE(0)
548 | NDCB0_DBC
549 | addr_cycle
550 | cmd;
fe69af00 551
fe69af00 552 case NAND_CMD_SEQIN:
4eb2da89 553 /* small page addr setting */
d456882b 554 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
4eb2da89
LW
555 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
556 | (column & 0xFF);
557
558 info->ndcb2 = 0;
559 } else {
560 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
561 | (column & 0xFFFF);
562
563 if (page_addr & 0xFF0000)
564 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
565 else
566 info->ndcb2 = 0;
567 }
568
fe69af00 569 info->buf_count = mtd->writesize + mtd->oobsize;
4eb2da89 570 memset(info->data_buff, 0xFF, info->buf_count);
fe69af00 571
fe69af00 572 break;
4eb2da89 573
fe69af00 574 case NAND_CMD_PAGEPROG:
4eb2da89
LW
575 if (is_buf_blank(info->data_buff,
576 (mtd->writesize + mtd->oobsize))) {
577 exec_cmd = 0;
578 break;
579 }
fe69af00 580
d456882b 581 cmd = host->cmdset->program;
4eb2da89
LW
582 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
583 | NDCB0_AUTO_RS
584 | NDCB0_ST_ROW_EN
585 | NDCB0_DBC
586 | cmd
587 | addr_cycle;
fe69af00 588 break;
4eb2da89 589
fe69af00 590 case NAND_CMD_READID:
d456882b
LW
591 cmd = host->cmdset->read_id;
592 info->buf_count = host->read_id_bytes;
4eb2da89
LW
593 info->ndcb0 |= NDCB0_CMD_TYPE(3)
594 | NDCB0_ADDR_CYC(1)
595 | cmd;
596
597 info->data_size = 8;
598 break;
fe69af00 599 case NAND_CMD_STATUS:
d456882b 600 cmd = host->cmdset->read_status;
4eb2da89
LW
601 info->buf_count = 1;
602 info->ndcb0 |= NDCB0_CMD_TYPE(4)
603 | NDCB0_ADDR_CYC(1)
604 | cmd;
605
606 info->data_size = 8;
607 break;
608
609 case NAND_CMD_ERASE1:
d456882b 610 cmd = host->cmdset->erase;
4eb2da89
LW
611 info->ndcb0 |= NDCB0_CMD_TYPE(2)
612 | NDCB0_AUTO_RS
613 | NDCB0_ADDR_CYC(3)
614 | NDCB0_DBC
615 | cmd;
616 info->ndcb1 = page_addr;
617 info->ndcb2 = 0;
618
fe69af00 619 break;
620 case NAND_CMD_RESET:
d456882b 621 cmd = host->cmdset->reset;
4eb2da89
LW
622 info->ndcb0 |= NDCB0_CMD_TYPE(5)
623 | cmd;
624
625 break;
626
627 case NAND_CMD_ERASE2:
628 exec_cmd = 0;
fe69af00 629 break;
4eb2da89 630
fe69af00 631 default:
4eb2da89 632 exec_cmd = 0;
da675b4e
LW
633 dev_err(&info->pdev->dev, "non-supported command %x\n",
634 command);
fe69af00 635 break;
636 }
637
4eb2da89
LW
638 return exec_cmd;
639}
640
641static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
642 int column, int page_addr)
643{
d456882b
LW
644 struct pxa3xx_nand_host *host = mtd->priv;
645 struct pxa3xx_nand_info *info = host->info_data;
4eb2da89
LW
646 int ret, exec_cmd;
647
648 /*
649 * if this is a x16 device ,then convert the input
650 * "byte" address into a "word" address appropriate
651 * for indexing a word-oriented device
652 */
d456882b 653 if (host->reg_ndcr & NDCR_DWIDTH_M)
4eb2da89
LW
654 column /= 2;
655
f3c8cfc2
LW
656 /*
657 * There may be different NAND chip hooked to
658 * different chip select, so check whether
659 * chip select has been changed, if yes, reset the timing
660 */
661 if (info->cs != host->cs) {
662 info->cs = host->cs;
663 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
664 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
665 }
666
d456882b 667 info->state = STATE_PREPARED;
4eb2da89 668 exec_cmd = prepare_command_pool(info, command, column, page_addr);
f8155a40
LW
669 if (exec_cmd) {
670 init_completion(&info->cmd_complete);
671 pxa3xx_nand_start(info);
672
673 ret = wait_for_completion_timeout(&info->cmd_complete,
674 CHIP_DELAY_TIMEOUT);
675 if (!ret) {
da675b4e 676 dev_err(&info->pdev->dev, "Wait time out!!!\n");
f8155a40
LW
677 /* Stop State Machine for next command cycle */
678 pxa3xx_nand_stop(info);
679 }
f8155a40 680 }
d456882b 681 info->state = STATE_IDLE;
f8155a40
LW
682}
683
fdbad98d 684static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
1fbb938d 685 struct nand_chip *chip, const uint8_t *buf, int oob_required)
f8155a40
LW
686{
687 chip->write_buf(mtd, buf, mtd->writesize);
688 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
fdbad98d
JW
689
690 return 0;
f8155a40
LW
691}
692
693static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
1fbb938d
BN
694 struct nand_chip *chip, uint8_t *buf, int oob_required,
695 int page)
f8155a40 696{
d456882b
LW
697 struct pxa3xx_nand_host *host = mtd->priv;
698 struct pxa3xx_nand_info *info = host->info_data;
f8155a40
LW
699
700 chip->read_buf(mtd, buf, mtd->writesize);
701 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
702
703 if (info->retcode == ERR_SBERR) {
704 switch (info->use_ecc) {
705 case 1:
706 mtd->ecc_stats.corrected++;
707 break;
708 case 0:
709 default:
710 break;
711 }
712 } else if (info->retcode == ERR_DBERR) {
713 /*
714 * for blank page (all 0xff), HW will calculate its ECC as
715 * 0, which is different from the ECC information within
716 * OOB, ignore such double bit errors
717 */
718 if (is_buf_blank(buf, mtd->writesize))
543e32d5
DM
719 info->retcode = ERR_NONE;
720 else
f8155a40 721 mtd->ecc_stats.failed++;
fe69af00 722 }
f8155a40
LW
723
724 return 0;
fe69af00 725}
726
727static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
728{
d456882b
LW
729 struct pxa3xx_nand_host *host = mtd->priv;
730 struct pxa3xx_nand_info *info = host->info_data;
fe69af00 731 char retval = 0xFF;
732
733 if (info->buf_start < info->buf_count)
734 /* Has just send a new command? */
735 retval = info->data_buff[info->buf_start++];
736
737 return retval;
738}
739
740static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
741{
d456882b
LW
742 struct pxa3xx_nand_host *host = mtd->priv;
743 struct pxa3xx_nand_info *info = host->info_data;
fe69af00 744 u16 retval = 0xFFFF;
745
746 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
747 retval = *((u16 *)(info->data_buff+info->buf_start));
748 info->buf_start += 2;
749 }
750 return retval;
751}
752
753static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
754{
d456882b
LW
755 struct pxa3xx_nand_host *host = mtd->priv;
756 struct pxa3xx_nand_info *info = host->info_data;
fe69af00 757 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
758
759 memcpy(buf, info->data_buff + info->buf_start, real_len);
760 info->buf_start += real_len;
761}
762
763static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
764 const uint8_t *buf, int len)
765{
d456882b
LW
766 struct pxa3xx_nand_host *host = mtd->priv;
767 struct pxa3xx_nand_info *info = host->info_data;
fe69af00 768 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
769
770 memcpy(info->data_buff + info->buf_start, buf, real_len);
771 info->buf_start += real_len;
772}
773
774static int pxa3xx_nand_verify_buf(struct mtd_info *mtd,
775 const uint8_t *buf, int len)
776{
777 return 0;
778}
779
780static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
781{
782 return;
783}
784
785static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
786{
d456882b
LW
787 struct pxa3xx_nand_host *host = mtd->priv;
788 struct pxa3xx_nand_info *info = host->info_data;
fe69af00 789
790 /* pxa3xx_nand_send_command has waited for command complete */
791 if (this->state == FL_WRITING || this->state == FL_ERASING) {
792 if (info->retcode == ERR_NONE)
793 return 0;
794 else {
795 /*
796 * any error make it return 0x01 which will tell
797 * the caller the erase and write fail
798 */
799 return 0x01;
800 }
801 }
802
803 return 0;
804}
805
fe69af00 806static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
c8c17c88 807 const struct pxa3xx_nand_flash *f)
fe69af00 808{
809 struct platform_device *pdev = info->pdev;
810 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
f3c8cfc2 811 struct pxa3xx_nand_host *host = info->host[info->cs];
f8155a40 812 uint32_t ndcr = 0x0; /* enable all interrupts */
fe69af00 813
da675b4e
LW
814 if (f->page_size != 2048 && f->page_size != 512) {
815 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
fe69af00 816 return -EINVAL;
da675b4e 817 }
fe69af00 818
da675b4e
LW
819 if (f->flash_width != 16 && f->flash_width != 8) {
820 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
fe69af00 821 return -EINVAL;
da675b4e 822 }
fe69af00 823
824 /* calculate flash information */
d456882b
LW
825 host->cmdset = &default_cmdset;
826 host->page_size = f->page_size;
827 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
fe69af00 828
829 /* calculate addressing information */
d456882b 830 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
fe69af00 831
832 if (f->num_blocks * f->page_per_block > 65536)
d456882b 833 host->row_addr_cycles = 3;
fe69af00 834 else
d456882b 835 host->row_addr_cycles = 2;
fe69af00 836
837 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
d456882b 838 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
fe69af00 839 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
840 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
841 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
842 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
843
d456882b 844 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
fe69af00 845 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
846
d456882b 847 host->reg_ndcr = ndcr;
fe69af00 848
d456882b 849 pxa3xx_nand_set_timing(host, f->timing);
fe69af00 850 return 0;
851}
852
f271049e
MR
853static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
854{
f3c8cfc2
LW
855 /*
856 * We set 0 by hard coding here, for we don't support keep_config
857 * when there is more than one chip attached to the controller
858 */
859 struct pxa3xx_nand_host *host = info->host[0];
f271049e 860 uint32_t ndcr = nand_readl(info, NDCR);
f271049e 861
d456882b
LW
862 if (ndcr & NDCR_PAGE_SZ) {
863 host->page_size = 2048;
864 host->read_id_bytes = 4;
865 } else {
866 host->page_size = 512;
867 host->read_id_bytes = 2;
868 }
869
870 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
871 host->cmdset = &default_cmdset;
872
873 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
874 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
f271049e
MR
875
876 return 0;
877}
878
fe69af00 879/* the maximum possible buffer size for large page with OOB data
880 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
881 * data buffer and the DMA descriptor
882 */
883#define MAX_BUFF_SIZE PAGE_SIZE
884
885static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
886{
887 struct platform_device *pdev = info->pdev;
888 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
889
890 if (use_dma == 0) {
891 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
892 if (info->data_buff == NULL)
893 return -ENOMEM;
894 return 0;
895 }
896
897 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
898 &info->data_buff_phys, GFP_KERNEL);
899 if (info->data_buff == NULL) {
900 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
901 return -ENOMEM;
902 }
903
fe69af00 904 info->data_desc = (void *)info->data_buff + data_desc_offset;
905 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
906
907 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
908 pxa3xx_nand_data_dma_irq, info);
909 if (info->data_dma_ch < 0) {
910 dev_err(&pdev->dev, "failed to request data dma\n");
d456882b 911 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
fe69af00 912 info->data_buff, info->data_buff_phys);
913 return info->data_dma_ch;
914 }
915
916 return 0;
917}
918
401e67e2
LW
919static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
920{
f3c8cfc2 921 struct mtd_info *mtd;
d456882b 922 int ret;
f3c8cfc2 923 mtd = info->host[info->cs]->mtd;
401e67e2 924 /* use the common timing to make a try */
d456882b
LW
925 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
926 if (ret)
927 return ret;
928
929 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
401e67e2 930 if (info->is_ready)
401e67e2 931 return 0;
d456882b
LW
932
933 return -ENODEV;
401e67e2 934}
fe69af00 935
401e67e2 936static int pxa3xx_nand_scan(struct mtd_info *mtd)
fe69af00 937{
d456882b
LW
938 struct pxa3xx_nand_host *host = mtd->priv;
939 struct pxa3xx_nand_info *info = host->info_data;
401e67e2
LW
940 struct platform_device *pdev = info->pdev;
941 struct pxa3xx_nand_platform_data *pdata = pdev->dev.platform_data;
0fab028b 942 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
401e67e2
LW
943 const struct pxa3xx_nand_flash *f = NULL;
944 struct nand_chip *chip = mtd->priv;
945 uint32_t id = -1;
4332c116 946 uint64_t chipsize;
401e67e2
LW
947 int i, ret, num;
948
949 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
4332c116 950 goto KEEP_CONFIG;
401e67e2
LW
951
952 ret = pxa3xx_nand_sensing(info);
d456882b 953 if (ret) {
f3c8cfc2
LW
954 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
955 info->cs);
401e67e2 956
d456882b 957 return ret;
401e67e2
LW
958 }
959
960 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
961 id = *((uint16_t *)(info->data_buff));
962 if (id != 0)
da675b4e 963 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
401e67e2 964 else {
da675b4e
LW
965 dev_warn(&info->pdev->dev,
966 "Read out ID 0, potential timing set wrong!!\n");
401e67e2
LW
967
968 return -EINVAL;
969 }
970
971 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
972 for (i = 0; i < num; i++) {
973 if (i < pdata->num_flash)
974 f = pdata->flash + i;
975 else
976 f = &builtin_flash_types[i - pdata->num_flash + 1];
977
978 /* find the chip in default list */
4332c116 979 if (f->chip_id == id)
401e67e2 980 break;
401e67e2
LW
981 }
982
4332c116 983 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
da675b4e 984 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
401e67e2
LW
985
986 return -EINVAL;
987 }
988
d456882b
LW
989 ret = pxa3xx_nand_config_flash(info, f);
990 if (ret) {
991 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
992 return ret;
993 }
994
4332c116
LW
995 pxa3xx_flash_ids[0].name = f->name;
996 pxa3xx_flash_ids[0].id = (f->chip_id >> 8) & 0xffff;
997 pxa3xx_flash_ids[0].pagesize = f->page_size;
998 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
999 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1000 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1001 if (f->flash_width == 16)
1002 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
0fab028b
LW
1003 pxa3xx_flash_ids[1].name = NULL;
1004 def = pxa3xx_flash_ids;
4332c116 1005KEEP_CONFIG:
d456882b
LW
1006 chip->ecc.mode = NAND_ECC_HW;
1007 chip->ecc.size = host->page_size;
6a918bad 1008 chip->ecc.strength = 1;
d456882b 1009
d456882b
LW
1010 if (host->reg_ndcr & NDCR_DWIDTH_M)
1011 chip->options |= NAND_BUSWIDTH_16;
1012
0fab028b 1013 if (nand_scan_ident(mtd, 1, def))
4332c116
LW
1014 return -ENODEV;
1015 /* calculate addressing information */
d456882b
LW
1016 if (mtd->writesize >= 2048)
1017 host->col_addr_cycles = 2;
1018 else
1019 host->col_addr_cycles = 1;
1020
4332c116
LW
1021 info->oob_buff = info->data_buff + mtd->writesize;
1022 if ((mtd->size >> chip->page_shift) > 65536)
d456882b 1023 host->row_addr_cycles = 3;
4332c116 1024 else
d456882b 1025 host->row_addr_cycles = 2;
fe69af00 1026
d456882b 1027 mtd->name = mtd_names[0];
401e67e2 1028 return nand_scan_tail(mtd);
fe69af00 1029}
1030
d456882b 1031static int alloc_nand_resource(struct platform_device *pdev)
fe69af00 1032{
f3c8cfc2 1033 struct pxa3xx_nand_platform_data *pdata;
fe69af00 1034 struct pxa3xx_nand_info *info;
d456882b 1035 struct pxa3xx_nand_host *host;
401e67e2 1036 struct nand_chip *chip;
fe69af00 1037 struct mtd_info *mtd;
1038 struct resource *r;
f3c8cfc2 1039 int ret, irq, cs;
fe69af00 1040
f3c8cfc2
LW
1041 pdata = pdev->dev.platform_data;
1042 info = kzalloc(sizeof(*info) + (sizeof(*mtd) +
1043 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
d456882b 1044 if (!info) {
fe69af00 1045 dev_err(&pdev->dev, "failed to allocate memory\n");
d456882b 1046 return -ENOMEM;
a1c06ee1 1047 }
fe69af00 1048
fe69af00 1049 info->pdev = pdev;
f3c8cfc2
LW
1050 for (cs = 0; cs < pdata->num_cs; cs++) {
1051 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1052 (sizeof(*mtd) + sizeof(*host)) * cs);
1053 chip = (struct nand_chip *)(&mtd[1]);
1054 host = (struct pxa3xx_nand_host *)chip;
1055 info->host[cs] = host;
1056 host->mtd = mtd;
1057 host->cs = cs;
1058 host->info_data = info;
1059 mtd->priv = host;
1060 mtd->owner = THIS_MODULE;
1061
1062 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1063 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1064 chip->controller = &info->controller;
1065 chip->waitfunc = pxa3xx_nand_waitfunc;
1066 chip->select_chip = pxa3xx_nand_select_chip;
1067 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1068 chip->read_word = pxa3xx_nand_read_word;
1069 chip->read_byte = pxa3xx_nand_read_byte;
1070 chip->read_buf = pxa3xx_nand_read_buf;
1071 chip->write_buf = pxa3xx_nand_write_buf;
1072 chip->verify_buf = pxa3xx_nand_verify_buf;
1073 }
401e67e2
LW
1074
1075 spin_lock_init(&chip->controller->lock);
1076 init_waitqueue_head(&chip->controller->wq);
e0d8b13a 1077 info->clk = clk_get(&pdev->dev, NULL);
fe69af00 1078 if (IS_ERR(info->clk)) {
1079 dev_err(&pdev->dev, "failed to get nand clock\n");
1080 ret = PTR_ERR(info->clk);
1081 goto fail_free_mtd;
1082 }
1083 clk_enable(info->clk);
1084
1085 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1086 if (r == NULL) {
1087 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1088 ret = -ENXIO;
1089 goto fail_put_clk;
1090 }
1091 info->drcmr_dat = r->start;
1092
1093 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1094 if (r == NULL) {
1095 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1096 ret = -ENXIO;
1097 goto fail_put_clk;
1098 }
1099 info->drcmr_cmd = r->start;
1100
1101 irq = platform_get_irq(pdev, 0);
1102 if (irq < 0) {
1103 dev_err(&pdev->dev, "no IRQ resource defined\n");
1104 ret = -ENXIO;
1105 goto fail_put_clk;
1106 }
1107
1108 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1109 if (r == NULL) {
1110 dev_err(&pdev->dev, "no IO memory resource defined\n");
1111 ret = -ENODEV;
1112 goto fail_put_clk;
1113 }
1114
b2ed3680 1115 r = request_mem_region(r->start, resource_size(r), pdev->name);
fe69af00 1116 if (r == NULL) {
1117 dev_err(&pdev->dev, "failed to request memory resource\n");
1118 ret = -EBUSY;
1119 goto fail_put_clk;
1120 }
1121
b2ed3680 1122 info->mmio_base = ioremap(r->start, resource_size(r));
fe69af00 1123 if (info->mmio_base == NULL) {
1124 dev_err(&pdev->dev, "ioremap() failed\n");
1125 ret = -ENODEV;
1126 goto fail_free_res;
1127 }
8638fac8 1128 info->mmio_phys = r->start;
fe69af00 1129
1130 ret = pxa3xx_nand_init_buff(info);
1131 if (ret)
1132 goto fail_free_io;
1133
346e1259
HZ
1134 /* initialize all interrupts to be disabled */
1135 disable_int(info, NDSR_MASK);
1136
dbf5986a
HZ
1137 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1138 pdev->name, info);
fe69af00 1139 if (ret < 0) {
1140 dev_err(&pdev->dev, "failed to request IRQ\n");
1141 goto fail_free_buf;
1142 }
1143
e353a20a 1144 platform_set_drvdata(pdev, info);
fe69af00 1145
d456882b 1146 return 0;
fe69af00 1147
fe69af00 1148fail_free_buf:
401e67e2 1149 free_irq(irq, info);
fe69af00 1150 if (use_dma) {
1151 pxa_free_dma(info->data_dma_ch);
d456882b 1152 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
fe69af00 1153 info->data_buff, info->data_buff_phys);
1154 } else
1155 kfree(info->data_buff);
1156fail_free_io:
1157 iounmap(info->mmio_base);
1158fail_free_res:
b2ed3680 1159 release_mem_region(r->start, resource_size(r));
fe69af00 1160fail_put_clk:
1161 clk_disable(info->clk);
1162 clk_put(info->clk);
1163fail_free_mtd:
d456882b
LW
1164 kfree(info);
1165 return ret;
fe69af00 1166}
1167
1168static int pxa3xx_nand_remove(struct platform_device *pdev)
1169{
e353a20a 1170 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
f3c8cfc2 1171 struct pxa3xx_nand_platform_data *pdata;
82a72d10 1172 struct resource *r;
f3c8cfc2 1173 int irq, cs;
fe69af00 1174
d456882b
LW
1175 if (!info)
1176 return 0;
1177
f3c8cfc2 1178 pdata = pdev->dev.platform_data;
fe69af00 1179 platform_set_drvdata(pdev, NULL);
1180
dbf5986a
HZ
1181 irq = platform_get_irq(pdev, 0);
1182 if (irq >= 0)
1183 free_irq(irq, info);
fe69af00 1184 if (use_dma) {
1185 pxa_free_dma(info->data_dma_ch);
d456882b 1186 dma_free_writecombine(&pdev->dev, MAX_BUFF_SIZE,
fe69af00 1187 info->data_buff, info->data_buff_phys);
1188 } else
1189 kfree(info->data_buff);
82a72d10
MR
1190
1191 iounmap(info->mmio_base);
1192 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1193 release_mem_region(r->start, resource_size(r));
1194
1195 clk_disable(info->clk);
1196 clk_put(info->clk);
1197
f3c8cfc2
LW
1198 for (cs = 0; cs < pdata->num_cs; cs++)
1199 nand_release(info->host[cs]->mtd);
d456882b 1200 kfree(info);
fe69af00 1201 return 0;
1202}
1203
e353a20a
LW
1204static int pxa3xx_nand_probe(struct platform_device *pdev)
1205{
1206 struct pxa3xx_nand_platform_data *pdata;
1207 struct pxa3xx_nand_info *info;
f3c8cfc2 1208 int ret, cs, probe_success;
e353a20a
LW
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
d456882b
LW
1216 ret = alloc_nand_resource(pdev);
1217 if (ret) {
1218 dev_err(&pdev->dev, "alloc nand resource failed\n");
1219 return ret;
1220 }
e353a20a 1221
d456882b 1222 info = platform_get_drvdata(pdev);
f3c8cfc2
LW
1223 probe_success = 0;
1224 for (cs = 0; cs < pdata->num_cs; cs++) {
1225 info->cs = cs;
1226 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1227 if (ret) {
1228 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1229 cs);
1230 continue;
1231 }
1232
42d7fbe2
AB
1233 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
1234 NULL, pdata->parts[cs],
1235 pdata->nr_parts[cs]);
f3c8cfc2
LW
1236 if (!ret)
1237 probe_success = 1;
1238 }
1239
1240 if (!probe_success) {
e353a20a
LW
1241 pxa3xx_nand_remove(pdev);
1242 return -ENODEV;
1243 }
1244
f3c8cfc2 1245 return 0;
e353a20a
LW
1246}
1247
fe69af00 1248#ifdef CONFIG_PM
1249static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1250{
e353a20a 1251 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
f3c8cfc2
LW
1252 struct pxa3xx_nand_platform_data *pdata;
1253 struct mtd_info *mtd;
1254 int cs;
fe69af00 1255
f3c8cfc2 1256 pdata = pdev->dev.platform_data;
f8155a40 1257 if (info->state) {
fe69af00 1258 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1259 return -EAGAIN;
1260 }
1261
f3c8cfc2
LW
1262 for (cs = 0; cs < pdata->num_cs; cs++) {
1263 mtd = info->host[cs]->mtd;
3fe4bae8 1264 mtd_suspend(mtd);
f3c8cfc2
LW
1265 }
1266
fe69af00 1267 return 0;
1268}
1269
1270static int pxa3xx_nand_resume(struct platform_device *pdev)
1271{
e353a20a 1272 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
f3c8cfc2
LW
1273 struct pxa3xx_nand_platform_data *pdata;
1274 struct mtd_info *mtd;
1275 int cs;
051fc41c 1276
f3c8cfc2 1277 pdata = pdev->dev.platform_data;
051fc41c
LW
1278 /* We don't want to handle interrupt without calling mtd routine */
1279 disable_int(info, NDCR_INT_MASK);
fe69af00 1280
f3c8cfc2
LW
1281 /*
1282 * Directly set the chip select to a invalid value,
1283 * then the driver would reset the timing according
1284 * to current chip select at the beginning of cmdfunc
1285 */
1286 info->cs = 0xff;
fe69af00 1287
051fc41c
LW
1288 /*
1289 * As the spec says, the NDSR would be updated to 0x1800 when
1290 * doing the nand_clk disable/enable.
1291 * To prevent it damaging state machine of the driver, clear
1292 * all status before resume
1293 */
1294 nand_writel(info, NDSR, NDSR_MASK);
f3c8cfc2
LW
1295 for (cs = 0; cs < pdata->num_cs; cs++) {
1296 mtd = info->host[cs]->mtd;
ead995f8 1297 mtd_resume(mtd);
f3c8cfc2
LW
1298 }
1299
18c81b18 1300 return 0;
fe69af00 1301}
1302#else
1303#define pxa3xx_nand_suspend NULL
1304#define pxa3xx_nand_resume NULL
1305#endif
1306
1307static struct platform_driver pxa3xx_nand_driver = {
1308 .driver = {
1309 .name = "pxa3xx-nand",
1310 },
1311 .probe = pxa3xx_nand_probe,
1312 .remove = pxa3xx_nand_remove,
1313 .suspend = pxa3xx_nand_suspend,
1314 .resume = pxa3xx_nand_resume,
1315};
1316
f99640de 1317module_platform_driver(pxa3xx_nand_driver);
fe69af00 1318
1319MODULE_LICENSE("GPL");
1320MODULE_DESCRIPTION("PXA3xx NAND controller driver");
This page took 0.30169 seconds and 5 git commands to generate.