blob: 8757491d19d13b93ff95dbe205faf60fe5268bd8 [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
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;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300496
497 /*
498 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
499 * must be loaded by writing directly either 12 or 16
500 * bytes directly to NDCB0, four bytes at a time.
501 *
502 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
503 * but each NDCBx register can be read.
504 */
Lei Wenf8155a42011-02-28 10:32:11 +0800505 nand_writel(info, NDCB0, info->ndcb0);
506 nand_writel(info, NDCB0, info->ndcb1);
507 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300508
509 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
510 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
511 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800512 }
Lei Wenf8155a42011-02-28 10:32:11 +0800513
514 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800515 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800516 if (is_completed)
517 complete(&info->cmd_complete);
518NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800519 return IRQ_HANDLED;
520}
521
eric miaofe69af02008-02-14 15:48:23 +0800522static inline int is_buf_blank(uint8_t *buf, size_t len)
523{
524 for (; len > 0; len--)
525 if (*buf++ != 0xff)
526 return 0;
527 return 1;
528}
529
Lei Wen4eb2da82011-02-28 10:32:13 +0800530static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
531 uint16_t column, int page_addr)
532{
533 uint16_t cmd;
Lei Wend4568822011-07-14 20:44:32 -0700534 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700535 struct pxa3xx_nand_host *host;
536 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800537
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700538 host = info->host[info->cs];
539 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800540 addr_cycle = 0;
541 exec_cmd = 1;
542
543 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800544 info->buf_start = 0;
545 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800546 info->oob_size = 0;
547 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300548 info->use_spare = 1;
Ezequiel Garcia0a60d042013-05-14 08:15:21 -0300549 info->use_dma = (use_dma) ? 1 : 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800550 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800551 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700552 if (info->cs != 0)
553 info->ndcb0 = NDCB0_CSEL;
554 else
555 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800556
557 switch (command) {
558 case NAND_CMD_READ0:
559 case NAND_CMD_PAGEPROG:
560 info->use_ecc = 1;
561 case NAND_CMD_READOOB:
562 pxa3xx_set_datasize(info);
563 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300564 case NAND_CMD_PARAM:
565 info->use_spare = 0;
566 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800567 case NAND_CMD_SEQIN:
568 exec_cmd = 0;
569 break;
570 default:
571 info->ndcb1 = 0;
572 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300573 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800574 break;
575 }
576
Lei Wend4568822011-07-14 20:44:32 -0700577 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
578 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800579
580 switch (command) {
581 case NAND_CMD_READOOB:
582 case NAND_CMD_READ0:
Lei Wend4568822011-07-14 20:44:32 -0700583 cmd = host->cmdset->read1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800584 if (command == NAND_CMD_READOOB)
585 info->buf_start = mtd->writesize + column;
586 else
587 info->buf_start = column;
588
Lei Wend4568822011-07-14 20:44:32 -0700589 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
Lei Wen4eb2da82011-02-28 10:32:13 +0800590 info->ndcb0 |= NDCB0_CMD_TYPE(0)
591 | addr_cycle
592 | (cmd & NDCB0_CMD1_MASK);
593 else
594 info->ndcb0 |= NDCB0_CMD_TYPE(0)
595 | NDCB0_DBC
596 | addr_cycle
597 | cmd;
598
599 case NAND_CMD_SEQIN:
600 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700601 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800602 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
603 | (column & 0xFF);
604
605 info->ndcb2 = 0;
606 } else {
607 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
608 | (column & 0xFFFF);
609
610 if (page_addr & 0xFF0000)
611 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
612 else
613 info->ndcb2 = 0;
614 }
615
616 info->buf_count = mtd->writesize + mtd->oobsize;
617 memset(info->data_buff, 0xFF, info->buf_count);
618
619 break;
620
621 case NAND_CMD_PAGEPROG:
622 if (is_buf_blank(info->data_buff,
623 (mtd->writesize + mtd->oobsize))) {
624 exec_cmd = 0;
625 break;
626 }
627
Lei Wend4568822011-07-14 20:44:32 -0700628 cmd = host->cmdset->program;
Lei Wen4eb2da82011-02-28 10:32:13 +0800629 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
630 | NDCB0_AUTO_RS
631 | NDCB0_ST_ROW_EN
632 | NDCB0_DBC
633 | cmd
634 | addr_cycle;
635 break;
636
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300637 case NAND_CMD_PARAM:
638 cmd = NAND_CMD_PARAM;
639 info->buf_count = 256;
640 info->ndcb0 |= NDCB0_CMD_TYPE(0)
641 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300642 | NDCB0_LEN_OVRD
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300643 | cmd;
644 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300645 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300646 info->data_size = 256;
647 break;
648
Lei Wen4eb2da82011-02-28 10:32:13 +0800649 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700650 cmd = host->cmdset->read_id;
651 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800652 info->ndcb0 |= NDCB0_CMD_TYPE(3)
653 | NDCB0_ADDR_CYC(1)
654 | cmd;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300655 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800656
657 info->data_size = 8;
658 break;
659 case NAND_CMD_STATUS:
Lei Wend4568822011-07-14 20:44:32 -0700660 cmd = host->cmdset->read_status;
Lei Wen4eb2da82011-02-28 10:32:13 +0800661 info->buf_count = 1;
662 info->ndcb0 |= NDCB0_CMD_TYPE(4)
663 | NDCB0_ADDR_CYC(1)
664 | cmd;
665
666 info->data_size = 8;
667 break;
668
669 case NAND_CMD_ERASE1:
Lei Wend4568822011-07-14 20:44:32 -0700670 cmd = host->cmdset->erase;
Lei Wen4eb2da82011-02-28 10:32:13 +0800671 info->ndcb0 |= NDCB0_CMD_TYPE(2)
672 | NDCB0_AUTO_RS
673 | NDCB0_ADDR_CYC(3)
674 | NDCB0_DBC
675 | cmd;
676 info->ndcb1 = page_addr;
677 info->ndcb2 = 0;
678
679 break;
680 case NAND_CMD_RESET:
Lei Wend4568822011-07-14 20:44:32 -0700681 cmd = host->cmdset->reset;
Lei Wen4eb2da82011-02-28 10:32:13 +0800682 info->ndcb0 |= NDCB0_CMD_TYPE(5)
683 | cmd;
684
685 break;
686
687 case NAND_CMD_ERASE2:
688 exec_cmd = 0;
689 break;
690
691 default:
692 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700693 dev_err(&info->pdev->dev, "non-supported command %x\n",
694 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800695 break;
696 }
697
698 return exec_cmd;
699}
700
eric miaofe69af02008-02-14 15:48:23 +0800701static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100702 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800703{
Lei Wend4568822011-07-14 20:44:32 -0700704 struct pxa3xx_nand_host *host = mtd->priv;
705 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800706 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800707
Lei Wen4eb2da82011-02-28 10:32:13 +0800708 /*
709 * if this is a x16 device ,then convert the input
710 * "byte" address into a "word" address appropriate
711 * for indexing a word-oriented device
712 */
Lei Wend4568822011-07-14 20:44:32 -0700713 if (host->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800714 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800715
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700716 /*
717 * There may be different NAND chip hooked to
718 * different chip select, so check whether
719 * chip select has been changed, if yes, reset the timing
720 */
721 if (info->cs != host->cs) {
722 info->cs = host->cs;
723 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
724 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
725 }
726
Lei Wend4568822011-07-14 20:44:32 -0700727 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800728 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800729 if (exec_cmd) {
730 init_completion(&info->cmd_complete);
731 pxa3xx_nand_start(info);
732
733 ret = wait_for_completion_timeout(&info->cmd_complete,
734 CHIP_DELAY_TIMEOUT);
735 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700736 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800737 /* Stop State Machine for next command cycle */
738 pxa3xx_nand_stop(info);
739 }
eric miaofe69af02008-02-14 15:48:23 +0800740 }
Lei Wend4568822011-07-14 20:44:32 -0700741 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800742}
743
Josh Wufdbad98d2012-06-25 18:07:45 +0800744static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700745 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800746{
747 chip->write_buf(mtd, buf, mtd->writesize);
748 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800749
750 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800751}
752
753static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700754 struct nand_chip *chip, uint8_t *buf, int oob_required,
755 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800756{
Lei Wend4568822011-07-14 20:44:32 -0700757 struct pxa3xx_nand_host *host = mtd->priv;
758 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800759
760 chip->read_buf(mtd, buf, mtd->writesize);
761 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
762
763 if (info->retcode == ERR_SBERR) {
764 switch (info->use_ecc) {
765 case 1:
766 mtd->ecc_stats.corrected++;
767 break;
768 case 0:
769 default:
770 break;
771 }
772 } else if (info->retcode == ERR_DBERR) {
773 /*
774 * for blank page (all 0xff), HW will calculate its ECC as
775 * 0, which is different from the ECC information within
776 * OOB, ignore such double bit errors
777 */
778 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700779 info->retcode = ERR_NONE;
780 else
Lei Wenf8155a42011-02-28 10:32:11 +0800781 mtd->ecc_stats.failed++;
782 }
783
784 return 0;
785}
786
eric miaofe69af02008-02-14 15:48:23 +0800787static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
788{
Lei Wend4568822011-07-14 20:44:32 -0700789 struct pxa3xx_nand_host *host = mtd->priv;
790 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800791 char retval = 0xFF;
792
793 if (info->buf_start < info->buf_count)
794 /* Has just send a new command? */
795 retval = info->data_buff[info->buf_start++];
796
797 return retval;
798}
799
800static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
801{
Lei Wend4568822011-07-14 20:44:32 -0700802 struct pxa3xx_nand_host *host = mtd->priv;
803 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800804 u16 retval = 0xFFFF;
805
806 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
807 retval = *((u16 *)(info->data_buff+info->buf_start));
808 info->buf_start += 2;
809 }
810 return retval;
811}
812
813static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
814{
Lei Wend4568822011-07-14 20:44:32 -0700815 struct pxa3xx_nand_host *host = mtd->priv;
816 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800817 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
818
819 memcpy(buf, info->data_buff + info->buf_start, real_len);
820 info->buf_start += real_len;
821}
822
823static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
824 const uint8_t *buf, int len)
825{
Lei Wend4568822011-07-14 20:44:32 -0700826 struct pxa3xx_nand_host *host = mtd->priv;
827 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800828 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
829
830 memcpy(info->data_buff + info->buf_start, buf, real_len);
831 info->buf_start += real_len;
832}
833
eric miaofe69af02008-02-14 15:48:23 +0800834static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
835{
836 return;
837}
838
839static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
840{
Lei Wend4568822011-07-14 20:44:32 -0700841 struct pxa3xx_nand_host *host = mtd->priv;
842 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800843
844 /* pxa3xx_nand_send_command has waited for command complete */
845 if (this->state == FL_WRITING || this->state == FL_ERASING) {
846 if (info->retcode == ERR_NONE)
847 return 0;
848 else {
849 /*
850 * any error make it return 0x01 which will tell
851 * the caller the erase and write fail
852 */
853 return 0x01;
854 }
855 }
856
857 return 0;
858}
859
eric miaofe69af02008-02-14 15:48:23 +0800860static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200861 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800862{
863 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900864 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700865 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800866 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800867
Lei Wenda675b42011-07-14 20:44:31 -0700868 if (f->page_size != 2048 && f->page_size != 512) {
869 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800870 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700871 }
eric miaofe69af02008-02-14 15:48:23 +0800872
Lei Wenda675b42011-07-14 20:44:31 -0700873 if (f->flash_width != 16 && f->flash_width != 8) {
874 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800875 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700876 }
eric miaofe69af02008-02-14 15:48:23 +0800877
878 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700879 host->cmdset = &default_cmdset;
880 host->page_size = f->page_size;
881 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800882
883 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700884 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800885
886 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700887 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800888 else
Lei Wend4568822011-07-14 20:44:32 -0700889 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800890
891 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700892 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800893 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
894 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
895 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
896 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
897
Lei Wend4568822011-07-14 20:44:32 -0700898 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800899 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
900
Lei Wend4568822011-07-14 20:44:32 -0700901 host->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800902
Lei Wend4568822011-07-14 20:44:32 -0700903 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800904 return 0;
905}
906
Mike Rapoportf2710492009-02-17 13:54:47 +0200907static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
908{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700909 /*
910 * We set 0 by hard coding here, for we don't support keep_config
911 * when there is more than one chip attached to the controller
912 */
913 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200914 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200915
Lei Wend4568822011-07-14 20:44:32 -0700916 if (ndcr & NDCR_PAGE_SZ) {
917 host->page_size = 2048;
918 host->read_id_bytes = 4;
919 } else {
920 host->page_size = 512;
921 host->read_id_bytes = 2;
922 }
923
924 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
925 host->cmdset = &default_cmdset;
926
927 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
928 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200929
930 return 0;
931}
932
eric miaofe69af02008-02-14 15:48:23 +0800933/* the maximum possible buffer size for large page with OOB data
934 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
935 * data buffer and the DMA descriptor
936 */
937#define MAX_BUFF_SIZE PAGE_SIZE
938
939static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
940{
941 struct platform_device *pdev = info->pdev;
942 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
943
944 if (use_dma == 0) {
945 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
946 if (info->data_buff == NULL)
947 return -ENOMEM;
948 return 0;
949 }
950
951 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
952 &info->data_buff_phys, GFP_KERNEL);
953 if (info->data_buff == NULL) {
954 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
955 return -ENOMEM;
956 }
957
eric miaofe69af02008-02-14 15:48:23 +0800958 info->data_desc = (void *)info->data_buff + data_desc_offset;
959 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
960
961 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
962 pxa3xx_nand_data_dma_irq, info);
963 if (info->data_dma_ch < 0) {
964 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700965 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800966 info->data_buff, info->data_buff_phys);
967 return info->data_dma_ch;
968 }
969
970 return 0;
971}
972
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300973static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
974{
975 struct platform_device *pdev = info->pdev;
976 if (use_dma) {
977 pxa_free_dma(info->data_dma_ch);
978 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
979 info->data_buff, info->data_buff_phys);
980 } else {
981 kfree(info->data_buff);
982 }
983}
984
Lei Wen401e67e2011-02-28 10:32:14 +0800985static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800986{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700987 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700988 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700989 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800990 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700991 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
992 if (ret)
993 return ret;
994
995 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800996 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800997 return 0;
Lei Wend4568822011-07-14 20:44:32 -0700998
999 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001000}
eric miaofe69af02008-02-14 15:48:23 +08001001
Lei Wen401e67e2011-02-28 10:32:14 +08001002static int pxa3xx_nand_scan(struct mtd_info *mtd)
1003{
Lei Wend4568822011-07-14 20:44:32 -07001004 struct pxa3xx_nand_host *host = mtd->priv;
1005 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001006 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001007 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001008 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001009 const struct pxa3xx_nand_flash *f = NULL;
1010 struct nand_chip *chip = mtd->priv;
1011 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001012 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001013 int i, ret, num;
1014
1015 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001016 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001017
1018 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001019 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001020 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1021 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001022
Lei Wend4568822011-07-14 20:44:32 -07001023 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001024 }
1025
1026 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1027 id = *((uint16_t *)(info->data_buff));
1028 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001029 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001030 else {
Lei Wenda675b42011-07-14 20:44:31 -07001031 dev_warn(&info->pdev->dev,
1032 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001033
1034 return -EINVAL;
1035 }
1036
1037 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1038 for (i = 0; i < num; i++) {
1039 if (i < pdata->num_flash)
1040 f = pdata->flash + i;
1041 else
1042 f = &builtin_flash_types[i - pdata->num_flash + 1];
1043
1044 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001045 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001046 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001047 }
1048
Lei Wen4332c112011-03-03 11:27:01 +08001049 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001050 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001051
1052 return -EINVAL;
1053 }
1054
Lei Wend4568822011-07-14 20:44:32 -07001055 ret = pxa3xx_nand_config_flash(info, f);
1056 if (ret) {
1057 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1058 return ret;
1059 }
1060
Lei Wen4332c112011-03-03 11:27:01 +08001061 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001062 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001063 pxa3xx_flash_ids[0].pagesize = f->page_size;
1064 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1065 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1066 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1067 if (f->flash_width == 16)
1068 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001069 pxa3xx_flash_ids[1].name = NULL;
1070 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001071KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001072 chip->ecc.mode = NAND_ECC_HW;
1073 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001074 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001075
Lei Wend4568822011-07-14 20:44:32 -07001076 if (host->reg_ndcr & NDCR_DWIDTH_M)
1077 chip->options |= NAND_BUSWIDTH_16;
1078
Lei Wen0fab0282011-06-07 03:01:06 -07001079 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001080 return -ENODEV;
1081 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001082 if (mtd->writesize >= 2048)
1083 host->col_addr_cycles = 2;
1084 else
1085 host->col_addr_cycles = 1;
1086
Lei Wen4332c112011-03-03 11:27:01 +08001087 info->oob_buff = info->data_buff + mtd->writesize;
1088 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001089 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001090 else
Lei Wend4568822011-07-14 20:44:32 -07001091 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001092 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001093}
1094
Lei Wend4568822011-07-14 20:44:32 -07001095static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001096{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001097 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001098 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001099 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001100 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001101 struct mtd_info *mtd;
1102 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001103 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001104
Jingoo Han453810b2013-07-30 17:18:33 +09001105 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001106 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1107 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1108 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001109 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001110
eric miaofe69af02008-02-14 15:48:23 +08001111 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001112 for (cs = 0; cs < pdata->num_cs; cs++) {
1113 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1114 (sizeof(*mtd) + sizeof(*host)) * cs);
1115 chip = (struct nand_chip *)(&mtd[1]);
1116 host = (struct pxa3xx_nand_host *)chip;
1117 info->host[cs] = host;
1118 host->mtd = mtd;
1119 host->cs = cs;
1120 host->info_data = info;
1121 mtd->priv = host;
1122 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001123
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001124 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1125 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1126 chip->controller = &info->controller;
1127 chip->waitfunc = pxa3xx_nand_waitfunc;
1128 chip->select_chip = pxa3xx_nand_select_chip;
1129 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1130 chip->read_word = pxa3xx_nand_read_word;
1131 chip->read_byte = pxa3xx_nand_read_byte;
1132 chip->read_buf = pxa3xx_nand_read_buf;
1133 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001134 }
Lei Wen401e67e2011-02-28 10:32:14 +08001135
1136 spin_lock_init(&chip->controller->lock);
1137 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001138 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001139 if (IS_ERR(info->clk)) {
1140 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001141 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001142 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001143 ret = clk_prepare_enable(info->clk);
1144 if (ret < 0)
1145 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001146
Daniel Mack1e7ba632012-07-22 19:51:02 +02001147 /*
1148 * This is a dirty hack to make this driver work from devicetree
1149 * bindings. It can be removed once we have a prober DMA controller
1150 * framework for DT.
1151 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001152 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001153 info->drcmr_dat = 97;
1154 info->drcmr_cmd = 99;
1155 } else {
1156 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1157 if (r == NULL) {
1158 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1159 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001160 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001161 }
1162 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001163
Daniel Mack1e7ba632012-07-22 19:51:02 +02001164 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1165 if (r == NULL) {
1166 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1167 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001168 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001169 }
1170 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001171 }
eric miaofe69af02008-02-14 15:48:23 +08001172
1173 irq = platform_get_irq(pdev, 0);
1174 if (irq < 0) {
1175 dev_err(&pdev->dev, "no IRQ resource defined\n");
1176 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001177 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001178 }
1179
1180 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001181 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1182 if (IS_ERR(info->mmio_base)) {
1183 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001184 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001185 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001186 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001187
1188 ret = pxa3xx_nand_init_buff(info);
1189 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001190 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001191
Haojian Zhuang346e1252009-09-10 14:27:23 +08001192 /* initialize all interrupts to be disabled */
1193 disable_int(info, NDSR_MASK);
1194
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001195 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1196 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001197 if (ret < 0) {
1198 dev_err(&pdev->dev, "failed to request IRQ\n");
1199 goto fail_free_buf;
1200 }
1201
Lei Wene353a202011-03-03 11:08:30 +08001202 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001203
Lei Wend4568822011-07-14 20:44:32 -07001204 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001205
eric miaofe69af02008-02-14 15:48:23 +08001206fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001207 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001208 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001209fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001210 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001211 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001212}
1213
1214static int pxa3xx_nand_remove(struct platform_device *pdev)
1215{
Lei Wene353a202011-03-03 11:08:30 +08001216 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001217 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001218 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001219
Lei Wend4568822011-07-14 20:44:32 -07001220 if (!info)
1221 return 0;
1222
Jingoo Han453810b2013-07-30 17:18:33 +09001223 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001224
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001225 irq = platform_get_irq(pdev, 0);
1226 if (irq >= 0)
1227 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001228 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001229
Ezequiel Garciafb320612013-04-17 13:38:12 -03001230 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001231
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001232 for (cs = 0; cs < pdata->num_cs; cs++)
1233 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001234 return 0;
1235}
1236
Daniel Mack1e7ba632012-07-22 19:51:02 +02001237#ifdef CONFIG_OF
1238static struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001239 {
1240 .compatible = "marvell,pxa3xx-nand",
1241 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1242 },
1243 {
1244 .compatible = "marvell,armada370-nand",
1245 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1246 },
Daniel Mack1e7ba632012-07-22 19:51:02 +02001247 {}
1248};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001249MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001250
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001251static enum pxa3xx_nand_variant
1252pxa3xx_nand_get_variant(struct platform_device *pdev)
1253{
1254 const struct of_device_id *of_id =
1255 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1256 if (!of_id)
1257 return PXA3XX_NAND_VARIANT_PXA;
1258 return (enum pxa3xx_nand_variant)of_id->data;
1259}
1260
Daniel Mack1e7ba632012-07-22 19:51:02 +02001261static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1262{
1263 struct pxa3xx_nand_platform_data *pdata;
1264 struct device_node *np = pdev->dev.of_node;
1265 const struct of_device_id *of_id =
1266 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1267
1268 if (!of_id)
1269 return 0;
1270
1271 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1272 if (!pdata)
1273 return -ENOMEM;
1274
1275 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1276 pdata->enable_arbiter = 1;
1277 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1278 pdata->keep_config = 1;
1279 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1280
1281 pdev->dev.platform_data = pdata;
1282
1283 return 0;
1284}
1285#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001286static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001287{
1288 return 0;
1289}
1290#endif
1291
Lei Wene353a202011-03-03 11:08:30 +08001292static int pxa3xx_nand_probe(struct platform_device *pdev)
1293{
1294 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001295 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001296 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001297 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001298
Daniel Mack1e7ba632012-07-22 19:51:02 +02001299 ret = pxa3xx_nand_probe_dt(pdev);
1300 if (ret)
1301 return ret;
1302
Jingoo Han453810b2013-07-30 17:18:33 +09001303 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001304 if (!pdata) {
1305 dev_err(&pdev->dev, "no platform data defined\n");
1306 return -ENODEV;
1307 }
1308
Lei Wend4568822011-07-14 20:44:32 -07001309 ret = alloc_nand_resource(pdev);
1310 if (ret) {
1311 dev_err(&pdev->dev, "alloc nand resource failed\n");
1312 return ret;
1313 }
Lei Wene353a202011-03-03 11:08:30 +08001314
Lei Wend4568822011-07-14 20:44:32 -07001315 info = platform_get_drvdata(pdev);
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001316 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001317 probe_success = 0;
1318 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001319 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001320
1321 mtd->name = pdev->name;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001322 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001323 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001324 if (ret) {
1325 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1326 cs);
1327 continue;
1328 }
1329
Daniel Mack1e7ba632012-07-22 19:51:02 +02001330 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001331 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001332 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001333 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001334 if (!ret)
1335 probe_success = 1;
1336 }
1337
1338 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001339 pxa3xx_nand_remove(pdev);
1340 return -ENODEV;
1341 }
1342
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001343 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001344}
1345
eric miaofe69af02008-02-14 15:48:23 +08001346#ifdef CONFIG_PM
1347static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1348{
Lei Wene353a202011-03-03 11:08:30 +08001349 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001350 struct pxa3xx_nand_platform_data *pdata;
1351 struct mtd_info *mtd;
1352 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001353
Jingoo Han453810b2013-07-30 17:18:33 +09001354 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001355 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001356 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1357 return -EAGAIN;
1358 }
1359
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001360 for (cs = 0; cs < pdata->num_cs; cs++) {
1361 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001362 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001363 }
1364
eric miaofe69af02008-02-14 15:48:23 +08001365 return 0;
1366}
1367
1368static int pxa3xx_nand_resume(struct platform_device *pdev)
1369{
Lei Wene353a202011-03-03 11:08:30 +08001370 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001371 struct pxa3xx_nand_platform_data *pdata;
1372 struct mtd_info *mtd;
1373 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001374
Jingoo Han453810b2013-07-30 17:18:33 +09001375 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001376 /* We don't want to handle interrupt without calling mtd routine */
1377 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001378
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001379 /*
1380 * Directly set the chip select to a invalid value,
1381 * then the driver would reset the timing according
1382 * to current chip select at the beginning of cmdfunc
1383 */
1384 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001385
Lei Wen051fc412011-07-14 20:44:30 -07001386 /*
1387 * As the spec says, the NDSR would be updated to 0x1800 when
1388 * doing the nand_clk disable/enable.
1389 * To prevent it damaging state machine of the driver, clear
1390 * all status before resume
1391 */
1392 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001393 for (cs = 0; cs < pdata->num_cs; cs++) {
1394 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001395 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001396 }
1397
Lei Wen18c81b12010-08-17 17:25:57 +08001398 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001399}
1400#else
1401#define pxa3xx_nand_suspend NULL
1402#define pxa3xx_nand_resume NULL
1403#endif
1404
1405static struct platform_driver pxa3xx_nand_driver = {
1406 .driver = {
1407 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001408 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001409 },
1410 .probe = pxa3xx_nand_probe,
1411 .remove = pxa3xx_nand_remove,
1412 .suspend = pxa3xx_nand_suspend,
1413 .resume = pxa3xx_nand_resume,
1414};
1415
Axel Linf99640d2011-11-27 20:45:03 +08001416module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001417
1418MODULE_LICENSE("GPL");
1419MODULE_DESCRIPTION("PXA3xx NAND controller driver");