blob: 6e28121d31183e5f0faf5ba24eb72b49c04ed92d [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>
Daniel Mack1e7ba632012-07-22 19:51:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
eric miaofe69af02008-02-14 15:48:23 +080027
Eric Miaoafb5b5c2008-12-01 11:43:08 +080028#include <mach/dma.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020029#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080030
31#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080032#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080033#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080034
35/* registers and bit definitions */
36#define NDCR (0x00) /* Control register */
37#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
38#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
39#define NDSR (0x14) /* Status Register */
40#define NDPCR (0x18) /* Page Count Register */
41#define NDBDR0 (0x1C) /* Bad Block Register 0 */
42#define NDBDR1 (0x20) /* Bad Block Register 1 */
43#define NDDB (0x40) /* Data Buffer */
44#define NDCB0 (0x48) /* Command Buffer0 */
45#define NDCB1 (0x4C) /* Command Buffer1 */
46#define NDCB2 (0x50) /* Command Buffer2 */
47
48#define NDCR_SPARE_EN (0x1 << 31)
49#define NDCR_ECC_EN (0x1 << 30)
50#define NDCR_DMA_EN (0x1 << 29)
51#define NDCR_ND_RUN (0x1 << 28)
52#define NDCR_DWIDTH_C (0x1 << 27)
53#define NDCR_DWIDTH_M (0x1 << 26)
54#define NDCR_PAGE_SZ (0x1 << 24)
55#define NDCR_NCSX (0x1 << 23)
56#define NDCR_ND_MODE (0x3 << 21)
57#define NDCR_NAND_MODE (0x0)
58#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080059#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080060#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
61#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
62
63#define NDCR_RA_START (0x1 << 15)
64#define NDCR_PG_PER_BLK (0x1 << 14)
65#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080066#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080067
68#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080069#define NDSR_RDY (0x1 << 12)
70#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080071#define NDSR_CS0_PAGED (0x1 << 10)
72#define NDSR_CS1_PAGED (0x1 << 9)
73#define NDSR_CS0_CMDD (0x1 << 8)
74#define NDSR_CS1_CMDD (0x1 << 7)
75#define NDSR_CS0_BBD (0x1 << 6)
76#define NDSR_CS1_BBD (0x1 << 5)
77#define NDSR_DBERR (0x1 << 4)
78#define NDSR_SBERR (0x1 << 3)
79#define NDSR_WRDREQ (0x1 << 2)
80#define NDSR_RDDREQ (0x1 << 1)
81#define NDSR_WRCMDREQ (0x1)
82
Lei Wen4eb2da82011-02-28 10:32:13 +080083#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080084#define NDCB0_AUTO_RS (0x1 << 25)
85#define NDCB0_CSEL (0x1 << 24)
86#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
87#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
88#define NDCB0_NC (0x1 << 20)
89#define NDCB0_DBC (0x1 << 19)
90#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
91#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
92#define NDCB0_CMD2_MASK (0xff << 8)
93#define NDCB0_CMD1_MASK (0xff)
94#define NDCB0_ADDR_CYC_SHIFT (16)
95
eric miaofe69af02008-02-14 15:48:23 +080096/* macros for registers read/write */
97#define nand_writel(info, off, val) \
98 __raw_writel((val), (info)->mmio_base + (off))
99
100#define nand_readl(info, off) \
101 __raw_readl((info)->mmio_base + (off))
102
103/* error code and state */
104enum {
105 ERR_NONE = 0,
106 ERR_DMABUSERR = -1,
107 ERR_SENDCMD = -2,
108 ERR_DBERR = -3,
109 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300110 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800111};
112
113enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800114 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700115 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800116 STATE_CMD_HANDLE,
117 STATE_DMA_READING,
118 STATE_DMA_WRITING,
119 STATE_DMA_DONE,
120 STATE_PIO_READING,
121 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800122 STATE_CMD_DONE,
123 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800124};
125
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300126enum pxa3xx_nand_variant {
127 PXA3XX_NAND_VARIANT_PXA,
128 PXA3XX_NAND_VARIANT_ARMADA370,
129};
130
Lei Wend4568822011-07-14 20:44:32 -0700131struct pxa3xx_nand_host {
132 struct nand_chip chip;
133 struct pxa3xx_nand_cmdset *cmdset;
134 struct mtd_info *mtd;
135 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800136
Lei Wend4568822011-07-14 20:44:32 -0700137 /* page size of attached chip */
138 unsigned int page_size;
139 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700140 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700141
142 /* calculated from pxa3xx_nand_flash data */
143 unsigned int col_addr_cycles;
144 unsigned int row_addr_cycles;
145 size_t read_id_bytes;
146
147 /* cached register value */
148 uint32_t reg_ndcr;
149 uint32_t ndtr0cs0;
150 uint32_t ndtr1cs0;
151};
152
153struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800154 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800155 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800156
157 struct clk *clk;
158 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800159 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700160 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800161
162 unsigned int buf_start;
163 unsigned int buf_count;
164
165 /* DMA information */
166 int drcmr_dat;
167 int drcmr_cmd;
168
169 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800170 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800171 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800172 int data_dma_ch;
173 struct pxa_dma_desc *data_desc;
174 dma_addr_t data_desc_addr;
175
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700176 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800177 unsigned int state;
178
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300179 /*
180 * This driver supports NFCv1 (as found in PXA SoC)
181 * and NFCv2 (as found in Armada 370/XP SoC).
182 */
183 enum pxa3xx_nand_variant variant;
184
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700185 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800186 int use_ecc; /* use HW ECC ? */
187 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300188 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800189 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800190
Lei Wen18c81b12010-08-17 17:25:57 +0800191 unsigned int page_size; /* page size of attached chip */
192 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700193 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800194 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800195
196 /* generated NDCBx register values */
197 uint32_t ndcb0;
198 uint32_t ndcb1;
199 uint32_t ndcb2;
200};
201
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030202static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800203module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300204MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800205
Mike Rapoportf2710492009-02-17 13:54:47 +0200206/*
207 * Default NAND flash controller configuration setup by the
208 * bootloader. This configuration is used only when pdata->keep_config is set
209 */
Lei Wenc1f82472010-08-17 13:50:23 +0800210static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800211 .read1 = 0x3000,
212 .read2 = 0x0050,
213 .program = 0x1080,
214 .read_status = 0x0070,
215 .read_id = 0x0090,
216 .erase = 0xD060,
217 .reset = 0x00FF,
218 .lock = 0x002A,
219 .unlock = 0x2423,
220 .lock_status = 0x007A,
221};
222
Lei Wenc1f82472010-08-17 13:50:23 +0800223static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800224 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
225 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
226 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
227 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800228};
229
Lei Wenc1f82472010-08-17 13:50:23 +0800230static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800231{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
232{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
233{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
234{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
235{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
236{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
237{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
238{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
239{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800240};
241
Lei Wen227a8862010-08-18 18:00:03 +0800242/* Define a default flash type setting serve as flash detecting only */
243#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
244
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700245const char *mtd_names[] = {"pxa3xx_nand-0", "pxa3xx_nand-1", NULL};
Lei Wen401e67e2011-02-28 10:32:14 +0800246
eric miaofe69af02008-02-14 15:48:23 +0800247#define NDTR0_tCH(c) (min((c), 7) << 19)
248#define NDTR0_tCS(c) (min((c), 7) << 16)
249#define NDTR0_tWH(c) (min((c), 7) << 11)
250#define NDTR0_tWP(c) (min((c), 7) << 8)
251#define NDTR0_tRH(c) (min((c), 7) << 3)
252#define NDTR0_tRP(c) (min((c), 7) << 0)
253
254#define NDTR1_tR(c) (min((c), 65535) << 16)
255#define NDTR1_tWHR(c) (min((c), 15) << 4)
256#define NDTR1_tAR(c) (min((c), 15) << 0)
257
258/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800259#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800260
Lei Wend4568822011-07-14 20:44:32 -0700261static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200262 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800263{
Lei Wend4568822011-07-14 20:44:32 -0700264 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800265 unsigned long nand_clk = clk_get_rate(info->clk);
266 uint32_t ndtr0, ndtr1;
267
268 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
269 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
270 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
271 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
272 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
273 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
274
275 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
276 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
277 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
278
Lei Wend4568822011-07-14 20:44:32 -0700279 host->ndtr0cs0 = ndtr0;
280 host->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800281 nand_writel(info, NDTR0CS0, ndtr0);
282 nand_writel(info, NDTR1CS0, ndtr1);
283}
284
Lei Wen18c81b12010-08-17 17:25:57 +0800285static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800286{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700287 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wend4568822011-07-14 20:44:32 -0700288 int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800289
Lei Wend4568822011-07-14 20:44:32 -0700290 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800291 if (!oob_enable) {
292 info->oob_size = 0;
293 return;
294 }
295
Lei Wend4568822011-07-14 20:44:32 -0700296 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800297 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800298 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800299 break;
300 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800301 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800302 break;
eric miaofe69af02008-02-14 15:48:23 +0800303 }
Lei Wen18c81b12010-08-17 17:25:57 +0800304}
305
Lei Wenf8155a42011-02-28 10:32:11 +0800306/**
307 * NOTE: it is a must to set ND_RUN firstly, then write
308 * command buffer, otherwise, it does not work.
309 * We enable all the interrupt at the same time, and
310 * let pxa3xx_nand_irq to handle all logic.
311 */
312static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
313{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700314 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800315 uint32_t ndcr;
316
Lei Wend4568822011-07-14 20:44:32 -0700317 ndcr = host->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300318
319 if (info->use_ecc)
320 ndcr |= NDCR_ECC_EN;
321 else
322 ndcr &= ~NDCR_ECC_EN;
323
324 if (info->use_dma)
325 ndcr |= NDCR_DMA_EN;
326 else
327 ndcr &= ~NDCR_DMA_EN;
328
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300329 if (info->use_spare)
330 ndcr |= NDCR_SPARE_EN;
331 else
332 ndcr &= ~NDCR_SPARE_EN;
333
Lei Wenf8155a42011-02-28 10:32:11 +0800334 ndcr |= NDCR_ND_RUN;
335
336 /* clear status bits and run */
337 nand_writel(info, NDCR, 0);
338 nand_writel(info, NDSR, NDSR_MASK);
339 nand_writel(info, NDCR, ndcr);
340}
341
342static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
343{
344 uint32_t ndcr;
345 int timeout = NAND_STOP_DELAY;
346
347 /* wait RUN bit in NDCR become 0 */
348 ndcr = nand_readl(info, NDCR);
349 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
350 ndcr = nand_readl(info, NDCR);
351 udelay(1);
352 }
353
354 if (timeout <= 0) {
355 ndcr &= ~NDCR_ND_RUN;
356 nand_writel(info, NDCR, ndcr);
357 }
358 /* clear status bits */
359 nand_writel(info, NDSR, NDSR_MASK);
360}
361
eric miaofe69af02008-02-14 15:48:23 +0800362static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
363{
364 uint32_t ndcr;
365
366 ndcr = nand_readl(info, NDCR);
367 nand_writel(info, NDCR, ndcr & ~int_mask);
368}
369
370static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
371{
372 uint32_t ndcr;
373
374 ndcr = nand_readl(info, NDCR);
375 nand_writel(info, NDCR, ndcr | int_mask);
376}
377
Lei Wenf8155a42011-02-28 10:32:11 +0800378static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800379{
eric miaofe69af02008-02-14 15:48:23 +0800380 switch (info->state) {
381 case STATE_PIO_WRITING:
382 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800383 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800384 if (info->oob_size > 0)
385 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
386 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800387 break;
388 case STATE_PIO_READING:
389 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800390 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800391 if (info->oob_size > 0)
392 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
393 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800394 break;
395 default:
Lei Wenda675b42011-07-14 20:44:31 -0700396 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800397 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800398 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800399 }
eric miaofe69af02008-02-14 15:48:23 +0800400}
401
Lei Wenf8155a42011-02-28 10:32:11 +0800402static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800403{
404 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800405 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800406
407 desc->ddadr = DDADR_STOP;
408 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
409
Lei Wenf8155a42011-02-28 10:32:11 +0800410 switch (info->state) {
411 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800412 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800413 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800414 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800415 break;
416 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800417 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800418 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800419 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800420 break;
421 default:
Lei Wenda675b42011-07-14 20:44:31 -0700422 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800423 info->state);
424 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800425 }
426
427 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
428 DDADR(info->data_dma_ch) = info->data_desc_addr;
429 DCSR(info->data_dma_ch) |= DCSR_RUN;
430}
431
432static void pxa3xx_nand_data_dma_irq(int channel, void *data)
433{
434 struct pxa3xx_nand_info *info = data;
435 uint32_t dcsr;
436
437 dcsr = DCSR(channel);
438 DCSR(channel) = dcsr;
439
440 if (dcsr & DCSR_BUSERR) {
441 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800442 }
443
Lei Wenf8155a42011-02-28 10:32:11 +0800444 info->state = STATE_DMA_DONE;
445 enable_int(info, NDCR_INT_MASK);
446 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800447}
448
449static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
450{
451 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800452 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700453 unsigned int ready, cmd_done;
454
455 if (info->cs == 0) {
456 ready = NDSR_FLASH_RDY;
457 cmd_done = NDSR_CS0_CMDD;
458 } else {
459 ready = NDSR_RDY;
460 cmd_done = NDSR_CS1_CMDD;
461 }
eric miaofe69af02008-02-14 15:48:23 +0800462
463 status = nand_readl(info, NDSR);
464
Lei Wenf8155a42011-02-28 10:32:11 +0800465 if (status & NDSR_DBERR)
466 info->retcode = ERR_DBERR;
467 if (status & NDSR_SBERR)
468 info->retcode = ERR_SBERR;
469 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
470 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800471 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800472 disable_int(info, NDCR_INT_MASK);
473 info->state = (status & NDSR_RDDREQ) ?
474 STATE_DMA_READING : STATE_DMA_WRITING;
475 start_data_dma(info);
476 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800477 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800478 info->state = (status & NDSR_RDDREQ) ?
479 STATE_PIO_READING : STATE_PIO_WRITING;
480 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800481 }
Lei Wenf8155a42011-02-28 10:32:11 +0800482 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700483 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800484 info->state = STATE_CMD_DONE;
485 is_completed = 1;
486 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700487 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800488 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800489 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800490 }
Lei Wenf8155a42011-02-28 10:32:11 +0800491
492 if (status & NDSR_WRCMDREQ) {
493 nand_writel(info, NDSR, NDSR_WRCMDREQ);
494 status &= ~NDSR_WRCMDREQ;
495 info->state = STATE_CMD_HANDLE;
496 nand_writel(info, NDCB0, info->ndcb0);
497 nand_writel(info, NDCB0, info->ndcb1);
498 nand_writel(info, NDCB0, info->ndcb2);
eric miaofe69af02008-02-14 15:48:23 +0800499 }
Lei Wenf8155a42011-02-28 10:32:11 +0800500
501 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800502 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800503 if (is_completed)
504 complete(&info->cmd_complete);
505NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800506 return IRQ_HANDLED;
507}
508
eric miaofe69af02008-02-14 15:48:23 +0800509static inline int is_buf_blank(uint8_t *buf, size_t len)
510{
511 for (; len > 0; len--)
512 if (*buf++ != 0xff)
513 return 0;
514 return 1;
515}
516
Lei Wen4eb2da82011-02-28 10:32:13 +0800517static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
518 uint16_t column, int page_addr)
519{
520 uint16_t cmd;
Lei Wend4568822011-07-14 20:44:32 -0700521 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700522 struct pxa3xx_nand_host *host;
523 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800524
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700525 host = info->host[info->cs];
526 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800527 addr_cycle = 0;
528 exec_cmd = 1;
529
530 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800531 info->buf_start = 0;
532 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800533 info->oob_size = 0;
534 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300535 info->use_spare = 1;
Ezequiel Garcia0a60d042013-05-14 08:15:21 -0300536 info->use_dma = (use_dma) ? 1 : 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800537 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800538 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700539 if (info->cs != 0)
540 info->ndcb0 = NDCB0_CSEL;
541 else
542 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800543
544 switch (command) {
545 case NAND_CMD_READ0:
546 case NAND_CMD_PAGEPROG:
547 info->use_ecc = 1;
548 case NAND_CMD_READOOB:
549 pxa3xx_set_datasize(info);
550 break;
551 case NAND_CMD_SEQIN:
552 exec_cmd = 0;
553 break;
554 default:
555 info->ndcb1 = 0;
556 info->ndcb2 = 0;
557 break;
558 }
559
Lei Wend4568822011-07-14 20:44:32 -0700560 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
561 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800562
563 switch (command) {
564 case NAND_CMD_READOOB:
565 case NAND_CMD_READ0:
Lei Wend4568822011-07-14 20:44:32 -0700566 cmd = host->cmdset->read1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800567 if (command == NAND_CMD_READOOB)
568 info->buf_start = mtd->writesize + column;
569 else
570 info->buf_start = column;
571
Lei Wend4568822011-07-14 20:44:32 -0700572 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
Lei Wen4eb2da82011-02-28 10:32:13 +0800573 info->ndcb0 |= NDCB0_CMD_TYPE(0)
574 | addr_cycle
575 | (cmd & NDCB0_CMD1_MASK);
576 else
577 info->ndcb0 |= NDCB0_CMD_TYPE(0)
578 | NDCB0_DBC
579 | addr_cycle
580 | cmd;
581
582 case NAND_CMD_SEQIN:
583 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700584 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800585 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
586 | (column & 0xFF);
587
588 info->ndcb2 = 0;
589 } else {
590 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
591 | (column & 0xFFFF);
592
593 if (page_addr & 0xFF0000)
594 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
595 else
596 info->ndcb2 = 0;
597 }
598
599 info->buf_count = mtd->writesize + mtd->oobsize;
600 memset(info->data_buff, 0xFF, info->buf_count);
601
602 break;
603
604 case NAND_CMD_PAGEPROG:
605 if (is_buf_blank(info->data_buff,
606 (mtd->writesize + mtd->oobsize))) {
607 exec_cmd = 0;
608 break;
609 }
610
Lei Wend4568822011-07-14 20:44:32 -0700611 cmd = host->cmdset->program;
Lei Wen4eb2da82011-02-28 10:32:13 +0800612 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
613 | NDCB0_AUTO_RS
614 | NDCB0_ST_ROW_EN
615 | NDCB0_DBC
616 | cmd
617 | addr_cycle;
618 break;
619
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300620 case NAND_CMD_PARAM:
621 cmd = NAND_CMD_PARAM;
622 info->buf_count = 256;
623 info->ndcb0 |= NDCB0_CMD_TYPE(0)
624 | NDCB0_ADDR_CYC(1)
625 | cmd;
626 info->ndcb1 = (column & 0xFF);
627 info->data_size = 256;
628 break;
629
Lei Wen4eb2da82011-02-28 10:32:13 +0800630 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700631 cmd = host->cmdset->read_id;
632 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800633 info->ndcb0 |= NDCB0_CMD_TYPE(3)
634 | NDCB0_ADDR_CYC(1)
635 | cmd;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300636 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800637
638 info->data_size = 8;
639 break;
640 case NAND_CMD_STATUS:
Lei Wend4568822011-07-14 20:44:32 -0700641 cmd = host->cmdset->read_status;
Lei Wen4eb2da82011-02-28 10:32:13 +0800642 info->buf_count = 1;
643 info->ndcb0 |= NDCB0_CMD_TYPE(4)
644 | NDCB0_ADDR_CYC(1)
645 | cmd;
646
647 info->data_size = 8;
648 break;
649
650 case NAND_CMD_ERASE1:
Lei Wend4568822011-07-14 20:44:32 -0700651 cmd = host->cmdset->erase;
Lei Wen4eb2da82011-02-28 10:32:13 +0800652 info->ndcb0 |= NDCB0_CMD_TYPE(2)
653 | NDCB0_AUTO_RS
654 | NDCB0_ADDR_CYC(3)
655 | NDCB0_DBC
656 | cmd;
657 info->ndcb1 = page_addr;
658 info->ndcb2 = 0;
659
660 break;
661 case NAND_CMD_RESET:
Lei Wend4568822011-07-14 20:44:32 -0700662 cmd = host->cmdset->reset;
Lei Wen4eb2da82011-02-28 10:32:13 +0800663 info->ndcb0 |= NDCB0_CMD_TYPE(5)
664 | cmd;
665
666 break;
667
668 case NAND_CMD_ERASE2:
669 exec_cmd = 0;
670 break;
671
672 default:
673 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700674 dev_err(&info->pdev->dev, "non-supported command %x\n",
675 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800676 break;
677 }
678
679 return exec_cmd;
680}
681
eric miaofe69af02008-02-14 15:48:23 +0800682static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100683 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800684{
Lei Wend4568822011-07-14 20:44:32 -0700685 struct pxa3xx_nand_host *host = mtd->priv;
686 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800687 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800688
Lei Wen4eb2da82011-02-28 10:32:13 +0800689 /*
690 * if this is a x16 device ,then convert the input
691 * "byte" address into a "word" address appropriate
692 * for indexing a word-oriented device
693 */
Lei Wend4568822011-07-14 20:44:32 -0700694 if (host->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800695 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800696
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700697 /*
698 * There may be different NAND chip hooked to
699 * different chip select, so check whether
700 * chip select has been changed, if yes, reset the timing
701 */
702 if (info->cs != host->cs) {
703 info->cs = host->cs;
704 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
705 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
706 }
707
Lei Wend4568822011-07-14 20:44:32 -0700708 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800709 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800710 if (exec_cmd) {
711 init_completion(&info->cmd_complete);
712 pxa3xx_nand_start(info);
713
714 ret = wait_for_completion_timeout(&info->cmd_complete,
715 CHIP_DELAY_TIMEOUT);
716 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700717 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800718 /* Stop State Machine for next command cycle */
719 pxa3xx_nand_stop(info);
720 }
eric miaofe69af02008-02-14 15:48:23 +0800721 }
Lei Wend4568822011-07-14 20:44:32 -0700722 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800723}
724
Josh Wufdbad98d2012-06-25 18:07:45 +0800725static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700726 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800727{
728 chip->write_buf(mtd, buf, mtd->writesize);
729 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800730
731 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800732}
733
734static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700735 struct nand_chip *chip, uint8_t *buf, int oob_required,
736 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800737{
Lei Wend4568822011-07-14 20:44:32 -0700738 struct pxa3xx_nand_host *host = mtd->priv;
739 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800740
741 chip->read_buf(mtd, buf, mtd->writesize);
742 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
743
744 if (info->retcode == ERR_SBERR) {
745 switch (info->use_ecc) {
746 case 1:
747 mtd->ecc_stats.corrected++;
748 break;
749 case 0:
750 default:
751 break;
752 }
753 } else if (info->retcode == ERR_DBERR) {
754 /*
755 * for blank page (all 0xff), HW will calculate its ECC as
756 * 0, which is different from the ECC information within
757 * OOB, ignore such double bit errors
758 */
759 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700760 info->retcode = ERR_NONE;
761 else
Lei Wenf8155a42011-02-28 10:32:11 +0800762 mtd->ecc_stats.failed++;
763 }
764
765 return 0;
766}
767
eric miaofe69af02008-02-14 15:48:23 +0800768static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
769{
Lei Wend4568822011-07-14 20:44:32 -0700770 struct pxa3xx_nand_host *host = mtd->priv;
771 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800772 char retval = 0xFF;
773
774 if (info->buf_start < info->buf_count)
775 /* Has just send a new command? */
776 retval = info->data_buff[info->buf_start++];
777
778 return retval;
779}
780
781static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
782{
Lei Wend4568822011-07-14 20:44:32 -0700783 struct pxa3xx_nand_host *host = mtd->priv;
784 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800785 u16 retval = 0xFFFF;
786
787 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
788 retval = *((u16 *)(info->data_buff+info->buf_start));
789 info->buf_start += 2;
790 }
791 return retval;
792}
793
794static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
795{
Lei Wend4568822011-07-14 20:44:32 -0700796 struct pxa3xx_nand_host *host = mtd->priv;
797 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800798 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
799
800 memcpy(buf, info->data_buff + info->buf_start, real_len);
801 info->buf_start += real_len;
802}
803
804static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
805 const uint8_t *buf, int len)
806{
Lei Wend4568822011-07-14 20:44:32 -0700807 struct pxa3xx_nand_host *host = mtd->priv;
808 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800809 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
810
811 memcpy(info->data_buff + info->buf_start, buf, real_len);
812 info->buf_start += real_len;
813}
814
eric miaofe69af02008-02-14 15:48:23 +0800815static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
816{
817 return;
818}
819
820static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
821{
Lei Wend4568822011-07-14 20:44:32 -0700822 struct pxa3xx_nand_host *host = mtd->priv;
823 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800824
825 /* pxa3xx_nand_send_command has waited for command complete */
826 if (this->state == FL_WRITING || this->state == FL_ERASING) {
827 if (info->retcode == ERR_NONE)
828 return 0;
829 else {
830 /*
831 * any error make it return 0x01 which will tell
832 * the caller the erase and write fail
833 */
834 return 0x01;
835 }
836 }
837
838 return 0;
839}
840
eric miaofe69af02008-02-14 15:48:23 +0800841static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200842 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800843{
844 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900845 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700846 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800847 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800848
Lei Wenda675b42011-07-14 20:44:31 -0700849 if (f->page_size != 2048 && f->page_size != 512) {
850 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800851 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700852 }
eric miaofe69af02008-02-14 15:48:23 +0800853
Lei Wenda675b42011-07-14 20:44:31 -0700854 if (f->flash_width != 16 && f->flash_width != 8) {
855 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800856 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700857 }
eric miaofe69af02008-02-14 15:48:23 +0800858
859 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700860 host->cmdset = &default_cmdset;
861 host->page_size = f->page_size;
862 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800863
864 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700865 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800866
867 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700868 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800869 else
Lei Wend4568822011-07-14 20:44:32 -0700870 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800871
872 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700873 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800874 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
875 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
876 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
877 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
878
Lei Wend4568822011-07-14 20:44:32 -0700879 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800880 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
881
Lei Wend4568822011-07-14 20:44:32 -0700882 host->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800883
Lei Wend4568822011-07-14 20:44:32 -0700884 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800885 return 0;
886}
887
Mike Rapoportf2710492009-02-17 13:54:47 +0200888static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
889{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700890 /*
891 * We set 0 by hard coding here, for we don't support keep_config
892 * when there is more than one chip attached to the controller
893 */
894 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200895 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200896
Lei Wend4568822011-07-14 20:44:32 -0700897 if (ndcr & NDCR_PAGE_SZ) {
898 host->page_size = 2048;
899 host->read_id_bytes = 4;
900 } else {
901 host->page_size = 512;
902 host->read_id_bytes = 2;
903 }
904
905 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
906 host->cmdset = &default_cmdset;
907
908 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
909 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200910
911 return 0;
912}
913
eric miaofe69af02008-02-14 15:48:23 +0800914/* the maximum possible buffer size for large page with OOB data
915 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
916 * data buffer and the DMA descriptor
917 */
918#define MAX_BUFF_SIZE PAGE_SIZE
919
920static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
921{
922 struct platform_device *pdev = info->pdev;
923 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
924
925 if (use_dma == 0) {
926 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
927 if (info->data_buff == NULL)
928 return -ENOMEM;
929 return 0;
930 }
931
932 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
933 &info->data_buff_phys, GFP_KERNEL);
934 if (info->data_buff == NULL) {
935 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
936 return -ENOMEM;
937 }
938
eric miaofe69af02008-02-14 15:48:23 +0800939 info->data_desc = (void *)info->data_buff + data_desc_offset;
940 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
941
942 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
943 pxa3xx_nand_data_dma_irq, info);
944 if (info->data_dma_ch < 0) {
945 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700946 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800947 info->data_buff, info->data_buff_phys);
948 return info->data_dma_ch;
949 }
950
951 return 0;
952}
953
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300954static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
955{
956 struct platform_device *pdev = info->pdev;
957 if (use_dma) {
958 pxa_free_dma(info->data_dma_ch);
959 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
960 info->data_buff, info->data_buff_phys);
961 } else {
962 kfree(info->data_buff);
963 }
964}
965
Lei Wen401e67e2011-02-28 10:32:14 +0800966static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800967{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700968 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700969 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700970 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800971 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700972 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
973 if (ret)
974 return ret;
975
976 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800977 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800978 return 0;
Lei Wend4568822011-07-14 20:44:32 -0700979
980 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +0800981}
eric miaofe69af02008-02-14 15:48:23 +0800982
Lei Wen401e67e2011-02-28 10:32:14 +0800983static int pxa3xx_nand_scan(struct mtd_info *mtd)
984{
Lei Wend4568822011-07-14 20:44:32 -0700985 struct pxa3xx_nand_host *host = mtd->priv;
986 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +0800987 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900988 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -0700989 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +0800990 const struct pxa3xx_nand_flash *f = NULL;
991 struct nand_chip *chip = mtd->priv;
992 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +0800993 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +0800994 int i, ret, num;
995
996 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +0800997 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +0800998
999 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001000 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001001 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1002 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001003
Lei Wend4568822011-07-14 20:44:32 -07001004 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001005 }
1006
1007 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1008 id = *((uint16_t *)(info->data_buff));
1009 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001010 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001011 else {
Lei Wenda675b42011-07-14 20:44:31 -07001012 dev_warn(&info->pdev->dev,
1013 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001014
1015 return -EINVAL;
1016 }
1017
1018 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1019 for (i = 0; i < num; i++) {
1020 if (i < pdata->num_flash)
1021 f = pdata->flash + i;
1022 else
1023 f = &builtin_flash_types[i - pdata->num_flash + 1];
1024
1025 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001026 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001027 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001028 }
1029
Lei Wen4332c112011-03-03 11:27:01 +08001030 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001031 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001032
1033 return -EINVAL;
1034 }
1035
Lei Wend4568822011-07-14 20:44:32 -07001036 ret = pxa3xx_nand_config_flash(info, f);
1037 if (ret) {
1038 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1039 return ret;
1040 }
1041
Lei Wen4332c112011-03-03 11:27:01 +08001042 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001043 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001044 pxa3xx_flash_ids[0].pagesize = f->page_size;
1045 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1046 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1047 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1048 if (f->flash_width == 16)
1049 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001050 pxa3xx_flash_ids[1].name = NULL;
1051 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001052KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001053 chip->ecc.mode = NAND_ECC_HW;
1054 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001055 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001056
Lei Wend4568822011-07-14 20:44:32 -07001057 if (host->reg_ndcr & NDCR_DWIDTH_M)
1058 chip->options |= NAND_BUSWIDTH_16;
1059
Lei Wen0fab0282011-06-07 03:01:06 -07001060 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001061 return -ENODEV;
1062 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001063 if (mtd->writesize >= 2048)
1064 host->col_addr_cycles = 2;
1065 else
1066 host->col_addr_cycles = 1;
1067
Lei Wen4332c112011-03-03 11:27:01 +08001068 info->oob_buff = info->data_buff + mtd->writesize;
1069 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001070 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001071 else
Lei Wend4568822011-07-14 20:44:32 -07001072 host->row_addr_cycles = 2;
1073
Lei Wen4332c112011-03-03 11:27:01 +08001074 mtd->name = mtd_names[0];
Lei Wen401e67e2011-02-28 10:32:14 +08001075 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001076}
1077
Lei Wend4568822011-07-14 20:44:32 -07001078static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001079{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001080 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001081 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001082 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001083 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001084 struct mtd_info *mtd;
1085 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001086 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001087
Jingoo Han453810b2013-07-30 17:18:33 +09001088 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001089 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1090 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1091 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001092 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001093
eric miaofe69af02008-02-14 15:48:23 +08001094 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001095 for (cs = 0; cs < pdata->num_cs; cs++) {
1096 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1097 (sizeof(*mtd) + sizeof(*host)) * cs);
1098 chip = (struct nand_chip *)(&mtd[1]);
1099 host = (struct pxa3xx_nand_host *)chip;
1100 info->host[cs] = host;
1101 host->mtd = mtd;
1102 host->cs = cs;
1103 host->info_data = info;
1104 mtd->priv = host;
1105 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001106
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001107 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1108 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1109 chip->controller = &info->controller;
1110 chip->waitfunc = pxa3xx_nand_waitfunc;
1111 chip->select_chip = pxa3xx_nand_select_chip;
1112 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1113 chip->read_word = pxa3xx_nand_read_word;
1114 chip->read_byte = pxa3xx_nand_read_byte;
1115 chip->read_buf = pxa3xx_nand_read_buf;
1116 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001117 }
Lei Wen401e67e2011-02-28 10:32:14 +08001118
1119 spin_lock_init(&chip->controller->lock);
1120 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001121 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001122 if (IS_ERR(info->clk)) {
1123 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001124 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001125 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001126 ret = clk_prepare_enable(info->clk);
1127 if (ret < 0)
1128 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001129
Daniel Mack1e7ba632012-07-22 19:51:02 +02001130 /*
1131 * This is a dirty hack to make this driver work from devicetree
1132 * bindings. It can be removed once we have a prober DMA controller
1133 * framework for DT.
1134 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001135 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001136 info->drcmr_dat = 97;
1137 info->drcmr_cmd = 99;
1138 } else {
1139 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1140 if (r == NULL) {
1141 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1142 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001143 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001144 }
1145 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001146
Daniel Mack1e7ba632012-07-22 19:51:02 +02001147 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1148 if (r == NULL) {
1149 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1150 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001151 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001152 }
1153 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001154 }
eric miaofe69af02008-02-14 15:48:23 +08001155
1156 irq = platform_get_irq(pdev, 0);
1157 if (irq < 0) {
1158 dev_err(&pdev->dev, "no IRQ resource defined\n");
1159 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001160 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001161 }
1162
1163 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001164 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1165 if (IS_ERR(info->mmio_base)) {
1166 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001167 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001168 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001169 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001170
1171 ret = pxa3xx_nand_init_buff(info);
1172 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001173 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001174
Haojian Zhuang346e1252009-09-10 14:27:23 +08001175 /* initialize all interrupts to be disabled */
1176 disable_int(info, NDSR_MASK);
1177
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001178 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1179 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001180 if (ret < 0) {
1181 dev_err(&pdev->dev, "failed to request IRQ\n");
1182 goto fail_free_buf;
1183 }
1184
Lei Wene353a202011-03-03 11:08:30 +08001185 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001186
Lei Wend4568822011-07-14 20:44:32 -07001187 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001188
eric miaofe69af02008-02-14 15:48:23 +08001189fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001190 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001191 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001192fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001193 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001194 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001195}
1196
1197static int pxa3xx_nand_remove(struct platform_device *pdev)
1198{
Lei Wene353a202011-03-03 11:08:30 +08001199 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001200 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001201 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001202
Lei Wend4568822011-07-14 20:44:32 -07001203 if (!info)
1204 return 0;
1205
Jingoo Han453810b2013-07-30 17:18:33 +09001206 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001207
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001208 irq = platform_get_irq(pdev, 0);
1209 if (irq >= 0)
1210 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001211 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001212
Ezequiel Garciafb320612013-04-17 13:38:12 -03001213 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001214
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001215 for (cs = 0; cs < pdata->num_cs; cs++)
1216 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001217 return 0;
1218}
1219
Daniel Mack1e7ba632012-07-22 19:51:02 +02001220#ifdef CONFIG_OF
1221static struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001222 {
1223 .compatible = "marvell,pxa3xx-nand",
1224 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1225 },
1226 {
1227 .compatible = "marvell,armada370-nand",
1228 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1229 },
Daniel Mack1e7ba632012-07-22 19:51:02 +02001230 {}
1231};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001232MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001233
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001234static enum pxa3xx_nand_variant
1235pxa3xx_nand_get_variant(struct platform_device *pdev)
1236{
1237 const struct of_device_id *of_id =
1238 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1239 if (!of_id)
1240 return PXA3XX_NAND_VARIANT_PXA;
1241 return (enum pxa3xx_nand_variant)of_id->data;
1242}
1243
Daniel Mack1e7ba632012-07-22 19:51:02 +02001244static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1245{
1246 struct pxa3xx_nand_platform_data *pdata;
1247 struct device_node *np = pdev->dev.of_node;
1248 const struct of_device_id *of_id =
1249 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1250
1251 if (!of_id)
1252 return 0;
1253
1254 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1255 if (!pdata)
1256 return -ENOMEM;
1257
1258 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1259 pdata->enable_arbiter = 1;
1260 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1261 pdata->keep_config = 1;
1262 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1263
1264 pdev->dev.platform_data = pdata;
1265
1266 return 0;
1267}
1268#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001269static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001270{
1271 return 0;
1272}
1273#endif
1274
Lei Wene353a202011-03-03 11:08:30 +08001275static int pxa3xx_nand_probe(struct platform_device *pdev)
1276{
1277 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001278 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001279 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001280 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001281
Daniel Mack1e7ba632012-07-22 19:51:02 +02001282 ret = pxa3xx_nand_probe_dt(pdev);
1283 if (ret)
1284 return ret;
1285
Jingoo Han453810b2013-07-30 17:18:33 +09001286 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001287 if (!pdata) {
1288 dev_err(&pdev->dev, "no platform data defined\n");
1289 return -ENODEV;
1290 }
1291
Lei Wend4568822011-07-14 20:44:32 -07001292 ret = alloc_nand_resource(pdev);
1293 if (ret) {
1294 dev_err(&pdev->dev, "alloc nand resource failed\n");
1295 return ret;
1296 }
Lei Wene353a202011-03-03 11:08:30 +08001297
Lei Wend4568822011-07-14 20:44:32 -07001298 info = platform_get_drvdata(pdev);
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001299 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001300 probe_success = 0;
1301 for (cs = 0; cs < pdata->num_cs; cs++) {
1302 info->cs = cs;
1303 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1304 if (ret) {
1305 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1306 cs);
1307 continue;
1308 }
1309
Daniel Mack1e7ba632012-07-22 19:51:02 +02001310 ppdata.of_node = pdev->dev.of_node;
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001311 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001312 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001313 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001314 if (!ret)
1315 probe_success = 1;
1316 }
1317
1318 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001319 pxa3xx_nand_remove(pdev);
1320 return -ENODEV;
1321 }
1322
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001323 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001324}
1325
eric miaofe69af02008-02-14 15:48:23 +08001326#ifdef CONFIG_PM
1327static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1328{
Lei Wene353a202011-03-03 11:08:30 +08001329 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001330 struct pxa3xx_nand_platform_data *pdata;
1331 struct mtd_info *mtd;
1332 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001333
Jingoo Han453810b2013-07-30 17:18:33 +09001334 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001335 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001336 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1337 return -EAGAIN;
1338 }
1339
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001340 for (cs = 0; cs < pdata->num_cs; cs++) {
1341 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001342 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001343 }
1344
eric miaofe69af02008-02-14 15:48:23 +08001345 return 0;
1346}
1347
1348static int pxa3xx_nand_resume(struct platform_device *pdev)
1349{
Lei Wene353a202011-03-03 11:08:30 +08001350 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001351 struct pxa3xx_nand_platform_data *pdata;
1352 struct mtd_info *mtd;
1353 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001354
Jingoo Han453810b2013-07-30 17:18:33 +09001355 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001356 /* We don't want to handle interrupt without calling mtd routine */
1357 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001358
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001359 /*
1360 * Directly set the chip select to a invalid value,
1361 * then the driver would reset the timing according
1362 * to current chip select at the beginning of cmdfunc
1363 */
1364 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001365
Lei Wen051fc412011-07-14 20:44:30 -07001366 /*
1367 * As the spec says, the NDSR would be updated to 0x1800 when
1368 * doing the nand_clk disable/enable.
1369 * To prevent it damaging state machine of the driver, clear
1370 * all status before resume
1371 */
1372 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001373 for (cs = 0; cs < pdata->num_cs; cs++) {
1374 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001375 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001376 }
1377
Lei Wen18c81b12010-08-17 17:25:57 +08001378 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001379}
1380#else
1381#define pxa3xx_nand_suspend NULL
1382#define pxa3xx_nand_resume NULL
1383#endif
1384
1385static struct platform_driver pxa3xx_nand_driver = {
1386 .driver = {
1387 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001388 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001389 },
1390 .probe = pxa3xx_nand_probe,
1391 .remove = pxa3xx_nand_remove,
1392 .suspend = pxa3xx_nand_suspend,
1393 .resume = pxa3xx_nand_resume,
1394};
1395
Axel Linf99640d2011-11-27 20:45:03 +08001396module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001397
1398MODULE_LICENSE("GPL");
1399MODULE_DESCRIPTION("PXA3xx NAND controller driver");