blob: beef5bcf84ec91cb57114dbb79b5de7c3ef2072c [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
Ezequiel Garcia41a63432013-08-12 14:14:51 -030083#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +080084#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080085#define NDCB0_AUTO_RS (0x1 << 25)
86#define NDCB0_CSEL (0x1 << 24)
87#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
88#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
89#define NDCB0_NC (0x1 << 20)
90#define NDCB0_DBC (0x1 << 19)
91#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
92#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
93#define NDCB0_CMD2_MASK (0xff << 8)
94#define NDCB0_CMD1_MASK (0xff)
95#define NDCB0_ADDR_CYC_SHIFT (16)
96
eric miaofe69af02008-02-14 15:48:23 +080097/* macros for registers read/write */
98#define nand_writel(info, off, val) \
99 __raw_writel((val), (info)->mmio_base + (off))
100
101#define nand_readl(info, off) \
102 __raw_readl((info)->mmio_base + (off))
103
104/* error code and state */
105enum {
106 ERR_NONE = 0,
107 ERR_DMABUSERR = -1,
108 ERR_SENDCMD = -2,
109 ERR_DBERR = -3,
110 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300111 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800112};
113
114enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800115 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700116 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800117 STATE_CMD_HANDLE,
118 STATE_DMA_READING,
119 STATE_DMA_WRITING,
120 STATE_DMA_DONE,
121 STATE_PIO_READING,
122 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800123 STATE_CMD_DONE,
124 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800125};
126
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300127enum pxa3xx_nand_variant {
128 PXA3XX_NAND_VARIANT_PXA,
129 PXA3XX_NAND_VARIANT_ARMADA370,
130};
131
Lei Wend4568822011-07-14 20:44:32 -0700132struct pxa3xx_nand_host {
133 struct nand_chip chip;
134 struct pxa3xx_nand_cmdset *cmdset;
135 struct mtd_info *mtd;
136 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800137
Lei Wend4568822011-07-14 20:44:32 -0700138 /* page size of attached chip */
139 unsigned int page_size;
140 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700141 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700142
143 /* calculated from pxa3xx_nand_flash data */
144 unsigned int col_addr_cycles;
145 unsigned int row_addr_cycles;
146 size_t read_id_bytes;
147
148 /* cached register value */
149 uint32_t reg_ndcr;
150 uint32_t ndtr0cs0;
151 uint32_t ndtr1cs0;
152};
153
154struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800155 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800156 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800157
158 struct clk *clk;
159 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800160 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700161 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800162
163 unsigned int buf_start;
164 unsigned int buf_count;
165
166 /* DMA information */
167 int drcmr_dat;
168 int drcmr_cmd;
169
170 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800171 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800172 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800173 int data_dma_ch;
174 struct pxa_dma_desc *data_desc;
175 dma_addr_t data_desc_addr;
176
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700177 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800178 unsigned int state;
179
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300180 /*
181 * This driver supports NFCv1 (as found in PXA SoC)
182 * and NFCv2 (as found in Armada 370/XP SoC).
183 */
184 enum pxa3xx_nand_variant variant;
185
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700186 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800187 int use_ecc; /* use HW ECC ? */
188 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300189 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800190 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800191
Lei Wen18c81b12010-08-17 17:25:57 +0800192 unsigned int page_size; /* page size of attached chip */
193 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700194 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800195 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800196
197 /* generated NDCBx register values */
198 uint32_t ndcb0;
199 uint32_t ndcb1;
200 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300201 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800202};
203
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030204static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800205module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300206MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800207
Mike Rapoportf2710492009-02-17 13:54:47 +0200208/*
209 * Default NAND flash controller configuration setup by the
210 * bootloader. This configuration is used only when pdata->keep_config is set
211 */
Lei Wenc1f82472010-08-17 13:50:23 +0800212static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800213 .read1 = 0x3000,
214 .read2 = 0x0050,
215 .program = 0x1080,
216 .read_status = 0x0070,
217 .read_id = 0x0090,
218 .erase = 0xD060,
219 .reset = 0x00FF,
220 .lock = 0x002A,
221 .unlock = 0x2423,
222 .lock_status = 0x007A,
223};
224
Lei Wenc1f82472010-08-17 13:50:23 +0800225static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800226 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
227 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
228 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
229 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800230};
231
Lei Wenc1f82472010-08-17 13:50:23 +0800232static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800233{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
234{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
235{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
236{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
237{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
238{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
239{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
240{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
241{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800242};
243
Lei Wen227a8862010-08-18 18:00:03 +0800244/* Define a default flash type setting serve as flash detecting only */
245#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
246
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700247const char *mtd_names[] = {"pxa3xx_nand-0", "pxa3xx_nand-1", NULL};
Lei Wen401e67e2011-02-28 10:32:14 +0800248
eric miaofe69af02008-02-14 15:48:23 +0800249#define NDTR0_tCH(c) (min((c), 7) << 19)
250#define NDTR0_tCS(c) (min((c), 7) << 16)
251#define NDTR0_tWH(c) (min((c), 7) << 11)
252#define NDTR0_tWP(c) (min((c), 7) << 8)
253#define NDTR0_tRH(c) (min((c), 7) << 3)
254#define NDTR0_tRP(c) (min((c), 7) << 0)
255
256#define NDTR1_tR(c) (min((c), 65535) << 16)
257#define NDTR1_tWHR(c) (min((c), 15) << 4)
258#define NDTR1_tAR(c) (min((c), 15) << 0)
259
260/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800261#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800262
Lei Wend4568822011-07-14 20:44:32 -0700263static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200264 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800265{
Lei Wend4568822011-07-14 20:44:32 -0700266 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800267 unsigned long nand_clk = clk_get_rate(info->clk);
268 uint32_t ndtr0, ndtr1;
269
270 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
271 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
272 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
273 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
274 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
275 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
276
277 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
278 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
279 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
280
Lei Wend4568822011-07-14 20:44:32 -0700281 host->ndtr0cs0 = ndtr0;
282 host->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800283 nand_writel(info, NDTR0CS0, ndtr0);
284 nand_writel(info, NDTR1CS0, ndtr1);
285}
286
Lei Wen18c81b12010-08-17 17:25:57 +0800287static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800288{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700289 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wend4568822011-07-14 20:44:32 -0700290 int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800291
Lei Wend4568822011-07-14 20:44:32 -0700292 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800293 if (!oob_enable) {
294 info->oob_size = 0;
295 return;
296 }
297
Lei Wend4568822011-07-14 20:44:32 -0700298 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800299 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800300 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800301 break;
302 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800303 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800304 break;
eric miaofe69af02008-02-14 15:48:23 +0800305 }
Lei Wen18c81b12010-08-17 17:25:57 +0800306}
307
Lei Wenf8155a42011-02-28 10:32:11 +0800308/**
309 * NOTE: it is a must to set ND_RUN firstly, then write
310 * command buffer, otherwise, it does not work.
311 * We enable all the interrupt at the same time, and
312 * let pxa3xx_nand_irq to handle all logic.
313 */
314static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
315{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700316 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800317 uint32_t ndcr;
318
Lei Wend4568822011-07-14 20:44:32 -0700319 ndcr = host->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300320
321 if (info->use_ecc)
322 ndcr |= NDCR_ECC_EN;
323 else
324 ndcr &= ~NDCR_ECC_EN;
325
326 if (info->use_dma)
327 ndcr |= NDCR_DMA_EN;
328 else
329 ndcr &= ~NDCR_DMA_EN;
330
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300331 if (info->use_spare)
332 ndcr |= NDCR_SPARE_EN;
333 else
334 ndcr &= ~NDCR_SPARE_EN;
335
Lei Wenf8155a42011-02-28 10:32:11 +0800336 ndcr |= NDCR_ND_RUN;
337
338 /* clear status bits and run */
339 nand_writel(info, NDCR, 0);
340 nand_writel(info, NDSR, NDSR_MASK);
341 nand_writel(info, NDCR, ndcr);
342}
343
344static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
345{
346 uint32_t ndcr;
347 int timeout = NAND_STOP_DELAY;
348
349 /* wait RUN bit in NDCR become 0 */
350 ndcr = nand_readl(info, NDCR);
351 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
352 ndcr = nand_readl(info, NDCR);
353 udelay(1);
354 }
355
356 if (timeout <= 0) {
357 ndcr &= ~NDCR_ND_RUN;
358 nand_writel(info, NDCR, ndcr);
359 }
360 /* clear status bits */
361 nand_writel(info, NDSR, NDSR_MASK);
362}
363
eric miaofe69af02008-02-14 15:48:23 +0800364static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
365{
366 uint32_t ndcr;
367
368 ndcr = nand_readl(info, NDCR);
369 nand_writel(info, NDCR, ndcr & ~int_mask);
370}
371
372static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
373{
374 uint32_t ndcr;
375
376 ndcr = nand_readl(info, NDCR);
377 nand_writel(info, NDCR, ndcr | int_mask);
378}
379
Lei Wenf8155a42011-02-28 10:32:11 +0800380static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800381{
eric miaofe69af02008-02-14 15:48:23 +0800382 switch (info->state) {
383 case STATE_PIO_WRITING:
384 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800385 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800386 if (info->oob_size > 0)
387 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
388 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800389 break;
390 case STATE_PIO_READING:
391 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800392 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800393 if (info->oob_size > 0)
394 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
395 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800396 break;
397 default:
Lei Wenda675b42011-07-14 20:44:31 -0700398 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800399 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800400 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800401 }
eric miaofe69af02008-02-14 15:48:23 +0800402}
403
Lei Wenf8155a42011-02-28 10:32:11 +0800404static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800405{
406 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800407 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800408
409 desc->ddadr = DDADR_STOP;
410 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
411
Lei Wenf8155a42011-02-28 10:32:11 +0800412 switch (info->state) {
413 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800414 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800415 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800416 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800417 break;
418 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800419 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800420 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800421 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800422 break;
423 default:
Lei Wenda675b42011-07-14 20:44:31 -0700424 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800425 info->state);
426 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800427 }
428
429 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
430 DDADR(info->data_dma_ch) = info->data_desc_addr;
431 DCSR(info->data_dma_ch) |= DCSR_RUN;
432}
433
434static void pxa3xx_nand_data_dma_irq(int channel, void *data)
435{
436 struct pxa3xx_nand_info *info = data;
437 uint32_t dcsr;
438
439 dcsr = DCSR(channel);
440 DCSR(channel) = dcsr;
441
442 if (dcsr & DCSR_BUSERR) {
443 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800444 }
445
Lei Wenf8155a42011-02-28 10:32:11 +0800446 info->state = STATE_DMA_DONE;
447 enable_int(info, NDCR_INT_MASK);
448 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800449}
450
451static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
452{
453 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800454 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700455 unsigned int ready, cmd_done;
456
457 if (info->cs == 0) {
458 ready = NDSR_FLASH_RDY;
459 cmd_done = NDSR_CS0_CMDD;
460 } else {
461 ready = NDSR_RDY;
462 cmd_done = NDSR_CS1_CMDD;
463 }
eric miaofe69af02008-02-14 15:48:23 +0800464
465 status = nand_readl(info, NDSR);
466
Lei Wenf8155a42011-02-28 10:32:11 +0800467 if (status & NDSR_DBERR)
468 info->retcode = ERR_DBERR;
469 if (status & NDSR_SBERR)
470 info->retcode = ERR_SBERR;
471 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
472 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800473 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800474 disable_int(info, NDCR_INT_MASK);
475 info->state = (status & NDSR_RDDREQ) ?
476 STATE_DMA_READING : STATE_DMA_WRITING;
477 start_data_dma(info);
478 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800479 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800480 info->state = (status & NDSR_RDDREQ) ?
481 STATE_PIO_READING : STATE_PIO_WRITING;
482 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800483 }
Lei Wenf8155a42011-02-28 10:32:11 +0800484 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700485 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800486 info->state = STATE_CMD_DONE;
487 is_completed = 1;
488 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700489 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800490 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800491 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800492 }
Lei Wenf8155a42011-02-28 10:32:11 +0800493
494 if (status & NDSR_WRCMDREQ) {
495 nand_writel(info, NDSR, NDSR_WRCMDREQ);
496 status &= ~NDSR_WRCMDREQ;
497 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300498
499 /*
500 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
501 * must be loaded by writing directly either 12 or 16
502 * bytes directly to NDCB0, four bytes at a time.
503 *
504 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
505 * but each NDCBx register can be read.
506 */
Lei Wenf8155a42011-02-28 10:32:11 +0800507 nand_writel(info, NDCB0, info->ndcb0);
508 nand_writel(info, NDCB0, info->ndcb1);
509 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300510
511 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
512 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
513 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800514 }
Lei Wenf8155a42011-02-28 10:32:11 +0800515
516 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800517 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800518 if (is_completed)
519 complete(&info->cmd_complete);
520NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800521 return IRQ_HANDLED;
522}
523
eric miaofe69af02008-02-14 15:48:23 +0800524static inline int is_buf_blank(uint8_t *buf, size_t len)
525{
526 for (; len > 0; len--)
527 if (*buf++ != 0xff)
528 return 0;
529 return 1;
530}
531
Lei Wen4eb2da82011-02-28 10:32:13 +0800532static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
533 uint16_t column, int page_addr)
534{
535 uint16_t cmd;
Lei Wend4568822011-07-14 20:44:32 -0700536 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700537 struct pxa3xx_nand_host *host;
538 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800539
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700540 host = info->host[info->cs];
541 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800542 addr_cycle = 0;
543 exec_cmd = 1;
544
545 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800546 info->buf_start = 0;
547 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800548 info->oob_size = 0;
549 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300550 info->use_spare = 1;
Ezequiel Garcia0a60d042013-05-14 08:15:21 -0300551 info->use_dma = (use_dma) ? 1 : 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800552 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800553 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700554 if (info->cs != 0)
555 info->ndcb0 = NDCB0_CSEL;
556 else
557 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800558
559 switch (command) {
560 case NAND_CMD_READ0:
561 case NAND_CMD_PAGEPROG:
562 info->use_ecc = 1;
563 case NAND_CMD_READOOB:
564 pxa3xx_set_datasize(info);
565 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300566 case NAND_CMD_PARAM:
567 info->use_spare = 0;
568 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800569 case NAND_CMD_SEQIN:
570 exec_cmd = 0;
571 break;
572 default:
573 info->ndcb1 = 0;
574 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300575 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800576 break;
577 }
578
Lei Wend4568822011-07-14 20:44:32 -0700579 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
580 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800581
582 switch (command) {
583 case NAND_CMD_READOOB:
584 case NAND_CMD_READ0:
Lei Wend4568822011-07-14 20:44:32 -0700585 cmd = host->cmdset->read1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800586 if (command == NAND_CMD_READOOB)
587 info->buf_start = mtd->writesize + column;
588 else
589 info->buf_start = column;
590
Lei Wend4568822011-07-14 20:44:32 -0700591 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
Lei Wen4eb2da82011-02-28 10:32:13 +0800592 info->ndcb0 |= NDCB0_CMD_TYPE(0)
593 | addr_cycle
594 | (cmd & NDCB0_CMD1_MASK);
595 else
596 info->ndcb0 |= NDCB0_CMD_TYPE(0)
597 | NDCB0_DBC
598 | addr_cycle
599 | cmd;
600
601 case NAND_CMD_SEQIN:
602 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700603 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800604 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
605 | (column & 0xFF);
606
607 info->ndcb2 = 0;
608 } else {
609 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
610 | (column & 0xFFFF);
611
612 if (page_addr & 0xFF0000)
613 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
614 else
615 info->ndcb2 = 0;
616 }
617
618 info->buf_count = mtd->writesize + mtd->oobsize;
619 memset(info->data_buff, 0xFF, info->buf_count);
620
621 break;
622
623 case NAND_CMD_PAGEPROG:
624 if (is_buf_blank(info->data_buff,
625 (mtd->writesize + mtd->oobsize))) {
626 exec_cmd = 0;
627 break;
628 }
629
Lei Wend4568822011-07-14 20:44:32 -0700630 cmd = host->cmdset->program;
Lei Wen4eb2da82011-02-28 10:32:13 +0800631 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
632 | NDCB0_AUTO_RS
633 | NDCB0_ST_ROW_EN
634 | NDCB0_DBC
635 | cmd
636 | addr_cycle;
637 break;
638
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300639 case NAND_CMD_PARAM:
640 cmd = NAND_CMD_PARAM;
641 info->buf_count = 256;
642 info->ndcb0 |= NDCB0_CMD_TYPE(0)
643 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300644 | NDCB0_LEN_OVRD
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300645 | cmd;
646 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300647 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300648 info->data_size = 256;
649 break;
650
Lei Wen4eb2da82011-02-28 10:32:13 +0800651 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700652 cmd = host->cmdset->read_id;
653 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800654 info->ndcb0 |= NDCB0_CMD_TYPE(3)
655 | NDCB0_ADDR_CYC(1)
656 | cmd;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300657 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800658
659 info->data_size = 8;
660 break;
661 case NAND_CMD_STATUS:
Lei Wend4568822011-07-14 20:44:32 -0700662 cmd = host->cmdset->read_status;
Lei Wen4eb2da82011-02-28 10:32:13 +0800663 info->buf_count = 1;
664 info->ndcb0 |= NDCB0_CMD_TYPE(4)
665 | NDCB0_ADDR_CYC(1)
666 | cmd;
667
668 info->data_size = 8;
669 break;
670
671 case NAND_CMD_ERASE1:
Lei Wend4568822011-07-14 20:44:32 -0700672 cmd = host->cmdset->erase;
Lei Wen4eb2da82011-02-28 10:32:13 +0800673 info->ndcb0 |= NDCB0_CMD_TYPE(2)
674 | NDCB0_AUTO_RS
675 | NDCB0_ADDR_CYC(3)
676 | NDCB0_DBC
677 | cmd;
678 info->ndcb1 = page_addr;
679 info->ndcb2 = 0;
680
681 break;
682 case NAND_CMD_RESET:
Lei Wend4568822011-07-14 20:44:32 -0700683 cmd = host->cmdset->reset;
Lei Wen4eb2da82011-02-28 10:32:13 +0800684 info->ndcb0 |= NDCB0_CMD_TYPE(5)
685 | cmd;
686
687 break;
688
689 case NAND_CMD_ERASE2:
690 exec_cmd = 0;
691 break;
692
693 default:
694 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700695 dev_err(&info->pdev->dev, "non-supported command %x\n",
696 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800697 break;
698 }
699
700 return exec_cmd;
701}
702
eric miaofe69af02008-02-14 15:48:23 +0800703static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100704 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800705{
Lei Wend4568822011-07-14 20:44:32 -0700706 struct pxa3xx_nand_host *host = mtd->priv;
707 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800708 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800709
Lei Wen4eb2da82011-02-28 10:32:13 +0800710 /*
711 * if this is a x16 device ,then convert the input
712 * "byte" address into a "word" address appropriate
713 * for indexing a word-oriented device
714 */
Lei Wend4568822011-07-14 20:44:32 -0700715 if (host->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800716 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800717
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700718 /*
719 * There may be different NAND chip hooked to
720 * different chip select, so check whether
721 * chip select has been changed, if yes, reset the timing
722 */
723 if (info->cs != host->cs) {
724 info->cs = host->cs;
725 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
726 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
727 }
728
Lei Wend4568822011-07-14 20:44:32 -0700729 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800730 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800731 if (exec_cmd) {
732 init_completion(&info->cmd_complete);
733 pxa3xx_nand_start(info);
734
735 ret = wait_for_completion_timeout(&info->cmd_complete,
736 CHIP_DELAY_TIMEOUT);
737 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700738 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800739 /* Stop State Machine for next command cycle */
740 pxa3xx_nand_stop(info);
741 }
eric miaofe69af02008-02-14 15:48:23 +0800742 }
Lei Wend4568822011-07-14 20:44:32 -0700743 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800744}
745
Josh Wufdbad98d2012-06-25 18:07:45 +0800746static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700747 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800748{
749 chip->write_buf(mtd, buf, mtd->writesize);
750 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800751
752 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800753}
754
755static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700756 struct nand_chip *chip, uint8_t *buf, int oob_required,
757 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800758{
Lei Wend4568822011-07-14 20:44:32 -0700759 struct pxa3xx_nand_host *host = mtd->priv;
760 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800761
762 chip->read_buf(mtd, buf, mtd->writesize);
763 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
764
765 if (info->retcode == ERR_SBERR) {
766 switch (info->use_ecc) {
767 case 1:
768 mtd->ecc_stats.corrected++;
769 break;
770 case 0:
771 default:
772 break;
773 }
774 } else if (info->retcode == ERR_DBERR) {
775 /*
776 * for blank page (all 0xff), HW will calculate its ECC as
777 * 0, which is different from the ECC information within
778 * OOB, ignore such double bit errors
779 */
780 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700781 info->retcode = ERR_NONE;
782 else
Lei Wenf8155a42011-02-28 10:32:11 +0800783 mtd->ecc_stats.failed++;
784 }
785
786 return 0;
787}
788
eric miaofe69af02008-02-14 15:48:23 +0800789static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
790{
Lei Wend4568822011-07-14 20:44:32 -0700791 struct pxa3xx_nand_host *host = mtd->priv;
792 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800793 char retval = 0xFF;
794
795 if (info->buf_start < info->buf_count)
796 /* Has just send a new command? */
797 retval = info->data_buff[info->buf_start++];
798
799 return retval;
800}
801
802static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
803{
Lei Wend4568822011-07-14 20:44:32 -0700804 struct pxa3xx_nand_host *host = mtd->priv;
805 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800806 u16 retval = 0xFFFF;
807
808 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
809 retval = *((u16 *)(info->data_buff+info->buf_start));
810 info->buf_start += 2;
811 }
812 return retval;
813}
814
815static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
816{
Lei Wend4568822011-07-14 20:44:32 -0700817 struct pxa3xx_nand_host *host = mtd->priv;
818 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800819 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
820
821 memcpy(buf, info->data_buff + info->buf_start, real_len);
822 info->buf_start += real_len;
823}
824
825static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
826 const uint8_t *buf, int len)
827{
Lei Wend4568822011-07-14 20:44:32 -0700828 struct pxa3xx_nand_host *host = mtd->priv;
829 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800830 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
831
832 memcpy(info->data_buff + info->buf_start, buf, real_len);
833 info->buf_start += real_len;
834}
835
eric miaofe69af02008-02-14 15:48:23 +0800836static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
837{
838 return;
839}
840
841static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
842{
Lei Wend4568822011-07-14 20:44:32 -0700843 struct pxa3xx_nand_host *host = mtd->priv;
844 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800845
846 /* pxa3xx_nand_send_command has waited for command complete */
847 if (this->state == FL_WRITING || this->state == FL_ERASING) {
848 if (info->retcode == ERR_NONE)
849 return 0;
850 else {
851 /*
852 * any error make it return 0x01 which will tell
853 * the caller the erase and write fail
854 */
855 return 0x01;
856 }
857 }
858
859 return 0;
860}
861
eric miaofe69af02008-02-14 15:48:23 +0800862static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200863 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800864{
865 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900866 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700867 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800868 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800869
Lei Wenda675b42011-07-14 20:44:31 -0700870 if (f->page_size != 2048 && f->page_size != 512) {
871 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800872 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700873 }
eric miaofe69af02008-02-14 15:48:23 +0800874
Lei Wenda675b42011-07-14 20:44:31 -0700875 if (f->flash_width != 16 && f->flash_width != 8) {
876 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800877 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700878 }
eric miaofe69af02008-02-14 15:48:23 +0800879
880 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700881 host->cmdset = &default_cmdset;
882 host->page_size = f->page_size;
883 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800884
885 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700886 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800887
888 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700889 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800890 else
Lei Wend4568822011-07-14 20:44:32 -0700891 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800892
893 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700894 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800895 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
896 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
897 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
898 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
899
Lei Wend4568822011-07-14 20:44:32 -0700900 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800901 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
902
Lei Wend4568822011-07-14 20:44:32 -0700903 host->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800904
Lei Wend4568822011-07-14 20:44:32 -0700905 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800906 return 0;
907}
908
Mike Rapoportf2710492009-02-17 13:54:47 +0200909static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
910{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700911 /*
912 * We set 0 by hard coding here, for we don't support keep_config
913 * when there is more than one chip attached to the controller
914 */
915 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200916 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200917
Lei Wend4568822011-07-14 20:44:32 -0700918 if (ndcr & NDCR_PAGE_SZ) {
919 host->page_size = 2048;
920 host->read_id_bytes = 4;
921 } else {
922 host->page_size = 512;
923 host->read_id_bytes = 2;
924 }
925
926 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
927 host->cmdset = &default_cmdset;
928
929 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
930 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200931
932 return 0;
933}
934
eric miaofe69af02008-02-14 15:48:23 +0800935/* the maximum possible buffer size for large page with OOB data
936 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
937 * data buffer and the DMA descriptor
938 */
939#define MAX_BUFF_SIZE PAGE_SIZE
940
941static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
942{
943 struct platform_device *pdev = info->pdev;
944 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
945
946 if (use_dma == 0) {
947 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
948 if (info->data_buff == NULL)
949 return -ENOMEM;
950 return 0;
951 }
952
953 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
954 &info->data_buff_phys, GFP_KERNEL);
955 if (info->data_buff == NULL) {
956 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
957 return -ENOMEM;
958 }
959
eric miaofe69af02008-02-14 15:48:23 +0800960 info->data_desc = (void *)info->data_buff + data_desc_offset;
961 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
962
963 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
964 pxa3xx_nand_data_dma_irq, info);
965 if (info->data_dma_ch < 0) {
966 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700967 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800968 info->data_buff, info->data_buff_phys);
969 return info->data_dma_ch;
970 }
971
972 return 0;
973}
974
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300975static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
976{
977 struct platform_device *pdev = info->pdev;
978 if (use_dma) {
979 pxa_free_dma(info->data_dma_ch);
980 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
981 info->data_buff, info->data_buff_phys);
982 } else {
983 kfree(info->data_buff);
984 }
985}
986
Lei Wen401e67e2011-02-28 10:32:14 +0800987static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800988{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700989 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700990 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700991 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800992 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700993 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
994 if (ret)
995 return ret;
996
997 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800998 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800999 return 0;
Lei Wend4568822011-07-14 20:44:32 -07001000
1001 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001002}
eric miaofe69af02008-02-14 15:48:23 +08001003
Lei Wen401e67e2011-02-28 10:32:14 +08001004static int pxa3xx_nand_scan(struct mtd_info *mtd)
1005{
Lei Wend4568822011-07-14 20:44:32 -07001006 struct pxa3xx_nand_host *host = mtd->priv;
1007 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001008 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001009 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001010 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001011 const struct pxa3xx_nand_flash *f = NULL;
1012 struct nand_chip *chip = mtd->priv;
1013 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001014 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001015 int i, ret, num;
1016
1017 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001018 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001019
1020 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001021 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001022 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1023 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001024
Lei Wend4568822011-07-14 20:44:32 -07001025 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001026 }
1027
1028 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1029 id = *((uint16_t *)(info->data_buff));
1030 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001031 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001032 else {
Lei Wenda675b42011-07-14 20:44:31 -07001033 dev_warn(&info->pdev->dev,
1034 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001035
1036 return -EINVAL;
1037 }
1038
1039 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1040 for (i = 0; i < num; i++) {
1041 if (i < pdata->num_flash)
1042 f = pdata->flash + i;
1043 else
1044 f = &builtin_flash_types[i - pdata->num_flash + 1];
1045
1046 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001047 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001048 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001049 }
1050
Lei Wen4332c112011-03-03 11:27:01 +08001051 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001052 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001053
1054 return -EINVAL;
1055 }
1056
Lei Wend4568822011-07-14 20:44:32 -07001057 ret = pxa3xx_nand_config_flash(info, f);
1058 if (ret) {
1059 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1060 return ret;
1061 }
1062
Lei Wen4332c112011-03-03 11:27:01 +08001063 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001064 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001065 pxa3xx_flash_ids[0].pagesize = f->page_size;
1066 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1067 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1068 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1069 if (f->flash_width == 16)
1070 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001071 pxa3xx_flash_ids[1].name = NULL;
1072 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001073KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001074 chip->ecc.mode = NAND_ECC_HW;
1075 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001076 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001077
Lei Wend4568822011-07-14 20:44:32 -07001078 if (host->reg_ndcr & NDCR_DWIDTH_M)
1079 chip->options |= NAND_BUSWIDTH_16;
1080
Lei Wen0fab0282011-06-07 03:01:06 -07001081 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001082 return -ENODEV;
1083 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001084 if (mtd->writesize >= 2048)
1085 host->col_addr_cycles = 2;
1086 else
1087 host->col_addr_cycles = 1;
1088
Lei Wen4332c112011-03-03 11:27:01 +08001089 info->oob_buff = info->data_buff + mtd->writesize;
1090 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001091 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001092 else
Lei Wend4568822011-07-14 20:44:32 -07001093 host->row_addr_cycles = 2;
1094
Lei Wen4332c112011-03-03 11:27:01 +08001095 mtd->name = mtd_names[0];
Lei Wen401e67e2011-02-28 10:32:14 +08001096 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001097}
1098
Lei Wend4568822011-07-14 20:44:32 -07001099static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001100{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001101 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001102 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001103 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001104 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001105 struct mtd_info *mtd;
1106 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001107 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001108
Jingoo Han453810b2013-07-30 17:18:33 +09001109 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001110 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1111 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1112 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001113 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001114
eric miaofe69af02008-02-14 15:48:23 +08001115 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001116 for (cs = 0; cs < pdata->num_cs; cs++) {
1117 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1118 (sizeof(*mtd) + sizeof(*host)) * cs);
1119 chip = (struct nand_chip *)(&mtd[1]);
1120 host = (struct pxa3xx_nand_host *)chip;
1121 info->host[cs] = host;
1122 host->mtd = mtd;
1123 host->cs = cs;
1124 host->info_data = info;
1125 mtd->priv = host;
1126 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001127
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001128 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1129 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1130 chip->controller = &info->controller;
1131 chip->waitfunc = pxa3xx_nand_waitfunc;
1132 chip->select_chip = pxa3xx_nand_select_chip;
1133 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1134 chip->read_word = pxa3xx_nand_read_word;
1135 chip->read_byte = pxa3xx_nand_read_byte;
1136 chip->read_buf = pxa3xx_nand_read_buf;
1137 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001138 }
Lei Wen401e67e2011-02-28 10:32:14 +08001139
1140 spin_lock_init(&chip->controller->lock);
1141 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001142 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001143 if (IS_ERR(info->clk)) {
1144 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001145 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001146 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001147 ret = clk_prepare_enable(info->clk);
1148 if (ret < 0)
1149 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001150
Daniel Mack1e7ba632012-07-22 19:51:02 +02001151 /*
1152 * This is a dirty hack to make this driver work from devicetree
1153 * bindings. It can be removed once we have a prober DMA controller
1154 * framework for DT.
1155 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001156 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001157 info->drcmr_dat = 97;
1158 info->drcmr_cmd = 99;
1159 } else {
1160 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1161 if (r == NULL) {
1162 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1163 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001164 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001165 }
1166 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001167
Daniel Mack1e7ba632012-07-22 19:51:02 +02001168 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1169 if (r == NULL) {
1170 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1171 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001172 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001173 }
1174 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001175 }
eric miaofe69af02008-02-14 15:48:23 +08001176
1177 irq = platform_get_irq(pdev, 0);
1178 if (irq < 0) {
1179 dev_err(&pdev->dev, "no IRQ resource defined\n");
1180 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001181 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001182 }
1183
1184 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001185 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1186 if (IS_ERR(info->mmio_base)) {
1187 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001188 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001189 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001190 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001191
1192 ret = pxa3xx_nand_init_buff(info);
1193 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001194 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001195
Haojian Zhuang346e1252009-09-10 14:27:23 +08001196 /* initialize all interrupts to be disabled */
1197 disable_int(info, NDSR_MASK);
1198
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001199 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1200 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001201 if (ret < 0) {
1202 dev_err(&pdev->dev, "failed to request IRQ\n");
1203 goto fail_free_buf;
1204 }
1205
Lei Wene353a202011-03-03 11:08:30 +08001206 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001207
Lei Wend4568822011-07-14 20:44:32 -07001208 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001209
eric miaofe69af02008-02-14 15:48:23 +08001210fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001211 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001212 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001213fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001214 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001215 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001216}
1217
1218static int pxa3xx_nand_remove(struct platform_device *pdev)
1219{
Lei Wene353a202011-03-03 11:08:30 +08001220 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001221 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001222 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001223
Lei Wend4568822011-07-14 20:44:32 -07001224 if (!info)
1225 return 0;
1226
Jingoo Han453810b2013-07-30 17:18:33 +09001227 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001228
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001229 irq = platform_get_irq(pdev, 0);
1230 if (irq >= 0)
1231 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001232 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001233
Ezequiel Garciafb320612013-04-17 13:38:12 -03001234 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001235
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001236 for (cs = 0; cs < pdata->num_cs; cs++)
1237 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001238 return 0;
1239}
1240
Daniel Mack1e7ba632012-07-22 19:51:02 +02001241#ifdef CONFIG_OF
1242static struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001243 {
1244 .compatible = "marvell,pxa3xx-nand",
1245 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1246 },
1247 {
1248 .compatible = "marvell,armada370-nand",
1249 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1250 },
Daniel Mack1e7ba632012-07-22 19:51:02 +02001251 {}
1252};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001253MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001254
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001255static enum pxa3xx_nand_variant
1256pxa3xx_nand_get_variant(struct platform_device *pdev)
1257{
1258 const struct of_device_id *of_id =
1259 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1260 if (!of_id)
1261 return PXA3XX_NAND_VARIANT_PXA;
1262 return (enum pxa3xx_nand_variant)of_id->data;
1263}
1264
Daniel Mack1e7ba632012-07-22 19:51:02 +02001265static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1266{
1267 struct pxa3xx_nand_platform_data *pdata;
1268 struct device_node *np = pdev->dev.of_node;
1269 const struct of_device_id *of_id =
1270 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1271
1272 if (!of_id)
1273 return 0;
1274
1275 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1276 if (!pdata)
1277 return -ENOMEM;
1278
1279 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1280 pdata->enable_arbiter = 1;
1281 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1282 pdata->keep_config = 1;
1283 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1284
1285 pdev->dev.platform_data = pdata;
1286
1287 return 0;
1288}
1289#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001290static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001291{
1292 return 0;
1293}
1294#endif
1295
Lei Wene353a202011-03-03 11:08:30 +08001296static int pxa3xx_nand_probe(struct platform_device *pdev)
1297{
1298 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001299 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001300 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001301 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001302
Daniel Mack1e7ba632012-07-22 19:51:02 +02001303 ret = pxa3xx_nand_probe_dt(pdev);
1304 if (ret)
1305 return ret;
1306
Jingoo Han453810b2013-07-30 17:18:33 +09001307 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001308 if (!pdata) {
1309 dev_err(&pdev->dev, "no platform data defined\n");
1310 return -ENODEV;
1311 }
1312
Lei Wend4568822011-07-14 20:44:32 -07001313 ret = alloc_nand_resource(pdev);
1314 if (ret) {
1315 dev_err(&pdev->dev, "alloc nand resource failed\n");
1316 return ret;
1317 }
Lei Wene353a202011-03-03 11:08:30 +08001318
Lei Wend4568822011-07-14 20:44:32 -07001319 info = platform_get_drvdata(pdev);
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001320 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001321 probe_success = 0;
1322 for (cs = 0; cs < pdata->num_cs; cs++) {
1323 info->cs = cs;
1324 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1325 if (ret) {
1326 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1327 cs);
1328 continue;
1329 }
1330
Daniel Mack1e7ba632012-07-22 19:51:02 +02001331 ppdata.of_node = pdev->dev.of_node;
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001332 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001333 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001334 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001335 if (!ret)
1336 probe_success = 1;
1337 }
1338
1339 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001340 pxa3xx_nand_remove(pdev);
1341 return -ENODEV;
1342 }
1343
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001344 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001345}
1346
eric miaofe69af02008-02-14 15:48:23 +08001347#ifdef CONFIG_PM
1348static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
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;
eric miaofe69af02008-02-14 15:48:23 +08001354
Jingoo Han453810b2013-07-30 17:18:33 +09001355 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001356 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001357 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1358 return -EAGAIN;
1359 }
1360
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001361 for (cs = 0; cs < pdata->num_cs; cs++) {
1362 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001363 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001364 }
1365
eric miaofe69af02008-02-14 15:48:23 +08001366 return 0;
1367}
1368
1369static int pxa3xx_nand_resume(struct platform_device *pdev)
1370{
Lei Wene353a202011-03-03 11:08:30 +08001371 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001372 struct pxa3xx_nand_platform_data *pdata;
1373 struct mtd_info *mtd;
1374 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001375
Jingoo Han453810b2013-07-30 17:18:33 +09001376 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001377 /* We don't want to handle interrupt without calling mtd routine */
1378 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001379
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001380 /*
1381 * Directly set the chip select to a invalid value,
1382 * then the driver would reset the timing according
1383 * to current chip select at the beginning of cmdfunc
1384 */
1385 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001386
Lei Wen051fc412011-07-14 20:44:30 -07001387 /*
1388 * As the spec says, the NDSR would be updated to 0x1800 when
1389 * doing the nand_clk disable/enable.
1390 * To prevent it damaging state machine of the driver, clear
1391 * all status before resume
1392 */
1393 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001394 for (cs = 0; cs < pdata->num_cs; cs++) {
1395 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001396 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001397 }
1398
Lei Wen18c81b12010-08-17 17:25:57 +08001399 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001400}
1401#else
1402#define pxa3xx_nand_suspend NULL
1403#define pxa3xx_nand_resume NULL
1404#endif
1405
1406static struct platform_driver pxa3xx_nand_driver = {
1407 .driver = {
1408 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001409 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001410 },
1411 .probe = pxa3xx_nand_probe,
1412 .remove = pxa3xx_nand_remove,
1413 .suspend = pxa3xx_nand_suspend,
1414 .resume = pxa3xx_nand_resume,
1415};
1416
Axel Linf99640d2011-11-27 20:45:03 +08001417module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001418
1419MODULE_LICENSE("GPL");
1420MODULE_DESCRIPTION("PXA3xx NAND controller driver");