blob: b3fc146cdccf05030d5635281dc9ab91c50ba623 [file] [log] [blame]
eric miaofe69af02008-02-14 15:48:23 +08001/*
2 * drivers/mtd/nand/pxa3xx_nand.c
3 *
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080012#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080013#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/platform_device.h>
16#include <linux/dma-mapping.h>
17#include <linux/delay.h>
18#include <linux/clk.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/nand.h>
21#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010022#include <linux/io.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
eric miaofe69af02008-02-14 15:48:23 +080027
Eric Miaoafb5b5c2008-12-01 11:43:08 +080028#include <mach/dma.h>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020029#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080030
31#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080032#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080033#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080034
35/* registers and bit definitions */
36#define NDCR (0x00) /* Control register */
37#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
38#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
39#define NDSR (0x14) /* Status Register */
40#define NDPCR (0x18) /* Page Count Register */
41#define NDBDR0 (0x1C) /* Bad Block Register 0 */
42#define NDBDR1 (0x20) /* Bad Block Register 1 */
43#define NDDB (0x40) /* Data Buffer */
44#define NDCB0 (0x48) /* Command Buffer0 */
45#define NDCB1 (0x4C) /* Command Buffer1 */
46#define NDCB2 (0x50) /* Command Buffer2 */
47
48#define NDCR_SPARE_EN (0x1 << 31)
49#define NDCR_ECC_EN (0x1 << 30)
50#define NDCR_DMA_EN (0x1 << 29)
51#define NDCR_ND_RUN (0x1 << 28)
52#define NDCR_DWIDTH_C (0x1 << 27)
53#define NDCR_DWIDTH_M (0x1 << 26)
54#define NDCR_PAGE_SZ (0x1 << 24)
55#define NDCR_NCSX (0x1 << 23)
56#define NDCR_ND_MODE (0x3 << 21)
57#define NDCR_NAND_MODE (0x0)
58#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080059#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080060#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
61#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
62
63#define NDCR_RA_START (0x1 << 15)
64#define NDCR_PG_PER_BLK (0x1 << 14)
65#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080066#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080067
68#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080069#define NDSR_RDY (0x1 << 12)
70#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080071#define NDSR_CS0_PAGED (0x1 << 10)
72#define NDSR_CS1_PAGED (0x1 << 9)
73#define NDSR_CS0_CMDD (0x1 << 8)
74#define NDSR_CS1_CMDD (0x1 << 7)
75#define NDSR_CS0_BBD (0x1 << 6)
76#define NDSR_CS1_BBD (0x1 << 5)
77#define NDSR_DBERR (0x1 << 4)
78#define NDSR_SBERR (0x1 << 3)
79#define NDSR_WRDREQ (0x1 << 2)
80#define NDSR_RDDREQ (0x1 << 1)
81#define NDSR_WRCMDREQ (0x1)
82
Lei Wen4eb2da82011-02-28 10:32:13 +080083#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080084#define NDCB0_AUTO_RS (0x1 << 25)
85#define NDCB0_CSEL (0x1 << 24)
86#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
87#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
88#define NDCB0_NC (0x1 << 20)
89#define NDCB0_DBC (0x1 << 19)
90#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
91#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
92#define NDCB0_CMD2_MASK (0xff << 8)
93#define NDCB0_CMD1_MASK (0xff)
94#define NDCB0_ADDR_CYC_SHIFT (16)
95
eric miaofe69af02008-02-14 15:48:23 +080096/* macros for registers read/write */
97#define nand_writel(info, off, val) \
98 __raw_writel((val), (info)->mmio_base + (off))
99
100#define nand_readl(info, off) \
101 __raw_readl((info)->mmio_base + (off))
102
103/* error code and state */
104enum {
105 ERR_NONE = 0,
106 ERR_DMABUSERR = -1,
107 ERR_SENDCMD = -2,
108 ERR_DBERR = -3,
109 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300110 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800111};
112
113enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800114 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700115 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800116 STATE_CMD_HANDLE,
117 STATE_DMA_READING,
118 STATE_DMA_WRITING,
119 STATE_DMA_DONE,
120 STATE_PIO_READING,
121 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800122 STATE_CMD_DONE,
123 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800124};
125
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300126enum pxa3xx_nand_variant {
127 PXA3XX_NAND_VARIANT_PXA,
128 PXA3XX_NAND_VARIANT_ARMADA370,
129};
130
Lei Wend4568822011-07-14 20:44:32 -0700131struct pxa3xx_nand_host {
132 struct nand_chip chip;
133 struct pxa3xx_nand_cmdset *cmdset;
134 struct mtd_info *mtd;
135 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800136
Lei Wend4568822011-07-14 20:44:32 -0700137 /* page size of attached chip */
138 unsigned int page_size;
139 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700140 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700141
142 /* calculated from pxa3xx_nand_flash data */
143 unsigned int col_addr_cycles;
144 unsigned int row_addr_cycles;
145 size_t read_id_bytes;
146
147 /* cached register value */
148 uint32_t reg_ndcr;
149 uint32_t ndtr0cs0;
150 uint32_t ndtr1cs0;
151};
152
153struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800154 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800155 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800156
157 struct clk *clk;
158 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800159 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700160 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800161
162 unsigned int buf_start;
163 unsigned int buf_count;
164
165 /* DMA information */
166 int drcmr_dat;
167 int drcmr_cmd;
168
169 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800170 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800171 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800172 int data_dma_ch;
173 struct pxa_dma_desc *data_desc;
174 dma_addr_t data_desc_addr;
175
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700176 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800177 unsigned int state;
178
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300179 /*
180 * This driver supports NFCv1 (as found in PXA SoC)
181 * and NFCv2 (as found in Armada 370/XP SoC).
182 */
183 enum pxa3xx_nand_variant variant;
184
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700185 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800186 int use_ecc; /* use HW ECC ? */
187 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300188 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800189 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800190
Lei Wen18c81b12010-08-17 17:25:57 +0800191 unsigned int page_size; /* page size of attached chip */
192 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700193 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800194 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800195
196 /* generated NDCBx register values */
197 uint32_t ndcb0;
198 uint32_t ndcb1;
199 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300200 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800201};
202
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030203static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800204module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300205MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800206
Mike Rapoportf2710492009-02-17 13:54:47 +0200207/*
208 * Default NAND flash controller configuration setup by the
209 * bootloader. This configuration is used only when pdata->keep_config is set
210 */
Lei Wenc1f82472010-08-17 13:50:23 +0800211static struct pxa3xx_nand_cmdset default_cmdset = {
eric miaofe69af02008-02-14 15:48:23 +0800212 .read1 = 0x3000,
213 .read2 = 0x0050,
214 .program = 0x1080,
215 .read_status = 0x0070,
216 .read_id = 0x0090,
217 .erase = 0xD060,
218 .reset = 0x00FF,
219 .lock = 0x002A,
220 .unlock = 0x2423,
221 .lock_status = 0x007A,
222};
223
Lei Wenc1f82472010-08-17 13:50:23 +0800224static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800225 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
226 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
227 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
228 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800229};
230
Lei Wenc1f82472010-08-17 13:50:23 +0800231static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800232{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
233{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
234{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
235{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
236{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
237{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
238{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
239{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
240{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800241};
242
Lei Wen227a8862010-08-18 18:00:03 +0800243/* Define a default flash type setting serve as flash detecting only */
244#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
245
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700246const char *mtd_names[] = {"pxa3xx_nand-0", "pxa3xx_nand-1", NULL};
Lei Wen401e67e2011-02-28 10:32:14 +0800247
eric miaofe69af02008-02-14 15:48:23 +0800248#define NDTR0_tCH(c) (min((c), 7) << 19)
249#define NDTR0_tCS(c) (min((c), 7) << 16)
250#define NDTR0_tWH(c) (min((c), 7) << 11)
251#define NDTR0_tWP(c) (min((c), 7) << 8)
252#define NDTR0_tRH(c) (min((c), 7) << 3)
253#define NDTR0_tRP(c) (min((c), 7) << 0)
254
255#define NDTR1_tR(c) (min((c), 65535) << 16)
256#define NDTR1_tWHR(c) (min((c), 15) << 4)
257#define NDTR1_tAR(c) (min((c), 15) << 0)
258
259/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800260#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800261
Lei Wend4568822011-07-14 20:44:32 -0700262static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200263 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800264{
Lei Wend4568822011-07-14 20:44:32 -0700265 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800266 unsigned long nand_clk = clk_get_rate(info->clk);
267 uint32_t ndtr0, ndtr1;
268
269 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
270 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
271 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
272 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
273 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
274 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
275
276 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
277 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
278 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
279
Lei Wend4568822011-07-14 20:44:32 -0700280 host->ndtr0cs0 = ndtr0;
281 host->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800282 nand_writel(info, NDTR0CS0, ndtr0);
283 nand_writel(info, NDTR1CS0, ndtr1);
284}
285
Lei Wen18c81b12010-08-17 17:25:57 +0800286static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800287{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700288 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wend4568822011-07-14 20:44:32 -0700289 int oob_enable = host->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800290
Lei Wend4568822011-07-14 20:44:32 -0700291 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800292 if (!oob_enable) {
293 info->oob_size = 0;
294 return;
295 }
296
Lei Wend4568822011-07-14 20:44:32 -0700297 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800298 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800299 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800300 break;
301 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800302 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800303 break;
eric miaofe69af02008-02-14 15:48:23 +0800304 }
Lei Wen18c81b12010-08-17 17:25:57 +0800305}
306
Lei Wenf8155a42011-02-28 10:32:11 +0800307/**
308 * NOTE: it is a must to set ND_RUN firstly, then write
309 * command buffer, otherwise, it does not work.
310 * We enable all the interrupt at the same time, and
311 * let pxa3xx_nand_irq to handle all logic.
312 */
313static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
314{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700315 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800316 uint32_t ndcr;
317
Lei Wend4568822011-07-14 20:44:32 -0700318 ndcr = host->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300319
320 if (info->use_ecc)
321 ndcr |= NDCR_ECC_EN;
322 else
323 ndcr &= ~NDCR_ECC_EN;
324
325 if (info->use_dma)
326 ndcr |= NDCR_DMA_EN;
327 else
328 ndcr &= ~NDCR_DMA_EN;
329
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300330 if (info->use_spare)
331 ndcr |= NDCR_SPARE_EN;
332 else
333 ndcr &= ~NDCR_SPARE_EN;
334
Lei Wenf8155a42011-02-28 10:32:11 +0800335 ndcr |= NDCR_ND_RUN;
336
337 /* clear status bits and run */
338 nand_writel(info, NDCR, 0);
339 nand_writel(info, NDSR, NDSR_MASK);
340 nand_writel(info, NDCR, ndcr);
341}
342
343static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
344{
345 uint32_t ndcr;
346 int timeout = NAND_STOP_DELAY;
347
348 /* wait RUN bit in NDCR become 0 */
349 ndcr = nand_readl(info, NDCR);
350 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
351 ndcr = nand_readl(info, NDCR);
352 udelay(1);
353 }
354
355 if (timeout <= 0) {
356 ndcr &= ~NDCR_ND_RUN;
357 nand_writel(info, NDCR, ndcr);
358 }
359 /* clear status bits */
360 nand_writel(info, NDSR, NDSR_MASK);
361}
362
eric miaofe69af02008-02-14 15:48:23 +0800363static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
364{
365 uint32_t ndcr;
366
367 ndcr = nand_readl(info, NDCR);
368 nand_writel(info, NDCR, ndcr & ~int_mask);
369}
370
371static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
372{
373 uint32_t ndcr;
374
375 ndcr = nand_readl(info, NDCR);
376 nand_writel(info, NDCR, ndcr | int_mask);
377}
378
Lei Wenf8155a42011-02-28 10:32:11 +0800379static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800380{
eric miaofe69af02008-02-14 15:48:23 +0800381 switch (info->state) {
382 case STATE_PIO_WRITING:
383 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800384 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800385 if (info->oob_size > 0)
386 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
387 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800388 break;
389 case STATE_PIO_READING:
390 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800391 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800392 if (info->oob_size > 0)
393 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
394 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800395 break;
396 default:
Lei Wenda675b42011-07-14 20:44:31 -0700397 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800398 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800399 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800400 }
eric miaofe69af02008-02-14 15:48:23 +0800401}
402
Lei Wenf8155a42011-02-28 10:32:11 +0800403static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800404{
405 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800406 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800407
408 desc->ddadr = DDADR_STOP;
409 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
410
Lei Wenf8155a42011-02-28 10:32:11 +0800411 switch (info->state) {
412 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800413 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800414 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800415 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800416 break;
417 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800418 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800419 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800420 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800421 break;
422 default:
Lei Wenda675b42011-07-14 20:44:31 -0700423 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800424 info->state);
425 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800426 }
427
428 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
429 DDADR(info->data_dma_ch) = info->data_desc_addr;
430 DCSR(info->data_dma_ch) |= DCSR_RUN;
431}
432
433static void pxa3xx_nand_data_dma_irq(int channel, void *data)
434{
435 struct pxa3xx_nand_info *info = data;
436 uint32_t dcsr;
437
438 dcsr = DCSR(channel);
439 DCSR(channel) = dcsr;
440
441 if (dcsr & DCSR_BUSERR) {
442 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800443 }
444
Lei Wenf8155a42011-02-28 10:32:11 +0800445 info->state = STATE_DMA_DONE;
446 enable_int(info, NDCR_INT_MASK);
447 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800448}
449
450static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
451{
452 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800453 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700454 unsigned int ready, cmd_done;
455
456 if (info->cs == 0) {
457 ready = NDSR_FLASH_RDY;
458 cmd_done = NDSR_CS0_CMDD;
459 } else {
460 ready = NDSR_RDY;
461 cmd_done = NDSR_CS1_CMDD;
462 }
eric miaofe69af02008-02-14 15:48:23 +0800463
464 status = nand_readl(info, NDSR);
465
Lei Wenf8155a42011-02-28 10:32:11 +0800466 if (status & NDSR_DBERR)
467 info->retcode = ERR_DBERR;
468 if (status & NDSR_SBERR)
469 info->retcode = ERR_SBERR;
470 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
471 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800472 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800473 disable_int(info, NDCR_INT_MASK);
474 info->state = (status & NDSR_RDDREQ) ?
475 STATE_DMA_READING : STATE_DMA_WRITING;
476 start_data_dma(info);
477 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800478 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800479 info->state = (status & NDSR_RDDREQ) ?
480 STATE_PIO_READING : STATE_PIO_WRITING;
481 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800482 }
Lei Wenf8155a42011-02-28 10:32:11 +0800483 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700484 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800485 info->state = STATE_CMD_DONE;
486 is_completed = 1;
487 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700488 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800489 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800490 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800491 }
Lei Wenf8155a42011-02-28 10:32:11 +0800492
493 if (status & NDSR_WRCMDREQ) {
494 nand_writel(info, NDSR, NDSR_WRCMDREQ);
495 status &= ~NDSR_WRCMDREQ;
496 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300497
498 /*
499 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
500 * must be loaded by writing directly either 12 or 16
501 * bytes directly to NDCB0, four bytes at a time.
502 *
503 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
504 * but each NDCBx register can be read.
505 */
Lei Wenf8155a42011-02-28 10:32:11 +0800506 nand_writel(info, NDCB0, info->ndcb0);
507 nand_writel(info, NDCB0, info->ndcb1);
508 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300509
510 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
511 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
512 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800513 }
Lei Wenf8155a42011-02-28 10:32:11 +0800514
515 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800516 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800517 if (is_completed)
518 complete(&info->cmd_complete);
519NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800520 return IRQ_HANDLED;
521}
522
eric miaofe69af02008-02-14 15:48:23 +0800523static inline int is_buf_blank(uint8_t *buf, size_t len)
524{
525 for (; len > 0; len--)
526 if (*buf++ != 0xff)
527 return 0;
528 return 1;
529}
530
Lei Wen4eb2da82011-02-28 10:32:13 +0800531static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
532 uint16_t column, int page_addr)
533{
534 uint16_t cmd;
Lei Wend4568822011-07-14 20:44:32 -0700535 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700536 struct pxa3xx_nand_host *host;
537 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800538
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700539 host = info->host[info->cs];
540 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800541 addr_cycle = 0;
542 exec_cmd = 1;
543
544 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800545 info->buf_start = 0;
546 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800547 info->oob_size = 0;
548 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300549 info->use_spare = 1;
Ezequiel Garcia0a60d042013-05-14 08:15:21 -0300550 info->use_dma = (use_dma) ? 1 : 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800551 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800552 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700553 if (info->cs != 0)
554 info->ndcb0 = NDCB0_CSEL;
555 else
556 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800557
558 switch (command) {
559 case NAND_CMD_READ0:
560 case NAND_CMD_PAGEPROG:
561 info->use_ecc = 1;
562 case NAND_CMD_READOOB:
563 pxa3xx_set_datasize(info);
564 break;
565 case NAND_CMD_SEQIN:
566 exec_cmd = 0;
567 break;
568 default:
569 info->ndcb1 = 0;
570 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300571 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800572 break;
573 }
574
Lei Wend4568822011-07-14 20:44:32 -0700575 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
576 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800577
578 switch (command) {
579 case NAND_CMD_READOOB:
580 case NAND_CMD_READ0:
Lei Wend4568822011-07-14 20:44:32 -0700581 cmd = host->cmdset->read1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800582 if (command == NAND_CMD_READOOB)
583 info->buf_start = mtd->writesize + column;
584 else
585 info->buf_start = column;
586
Lei Wend4568822011-07-14 20:44:32 -0700587 if (unlikely(host->page_size < PAGE_CHUNK_SIZE))
Lei Wen4eb2da82011-02-28 10:32:13 +0800588 info->ndcb0 |= NDCB0_CMD_TYPE(0)
589 | addr_cycle
590 | (cmd & NDCB0_CMD1_MASK);
591 else
592 info->ndcb0 |= NDCB0_CMD_TYPE(0)
593 | NDCB0_DBC
594 | addr_cycle
595 | cmd;
596
597 case NAND_CMD_SEQIN:
598 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700599 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800600 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
601 | (column & 0xFF);
602
603 info->ndcb2 = 0;
604 } else {
605 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
606 | (column & 0xFFFF);
607
608 if (page_addr & 0xFF0000)
609 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
610 else
611 info->ndcb2 = 0;
612 }
613
614 info->buf_count = mtd->writesize + mtd->oobsize;
615 memset(info->data_buff, 0xFF, info->buf_count);
616
617 break;
618
619 case NAND_CMD_PAGEPROG:
620 if (is_buf_blank(info->data_buff,
621 (mtd->writesize + mtd->oobsize))) {
622 exec_cmd = 0;
623 break;
624 }
625
Lei Wend4568822011-07-14 20:44:32 -0700626 cmd = host->cmdset->program;
Lei Wen4eb2da82011-02-28 10:32:13 +0800627 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
628 | NDCB0_AUTO_RS
629 | NDCB0_ST_ROW_EN
630 | NDCB0_DBC
631 | cmd
632 | addr_cycle;
633 break;
634
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300635 case NAND_CMD_PARAM:
636 cmd = NAND_CMD_PARAM;
637 info->buf_count = 256;
638 info->ndcb0 |= NDCB0_CMD_TYPE(0)
639 | NDCB0_ADDR_CYC(1)
640 | cmd;
641 info->ndcb1 = (column & 0xFF);
642 info->data_size = 256;
643 break;
644
Lei Wen4eb2da82011-02-28 10:32:13 +0800645 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700646 cmd = host->cmdset->read_id;
647 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800648 info->ndcb0 |= NDCB0_CMD_TYPE(3)
649 | NDCB0_ADDR_CYC(1)
650 | cmd;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300651 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800652
653 info->data_size = 8;
654 break;
655 case NAND_CMD_STATUS:
Lei Wend4568822011-07-14 20:44:32 -0700656 cmd = host->cmdset->read_status;
Lei Wen4eb2da82011-02-28 10:32:13 +0800657 info->buf_count = 1;
658 info->ndcb0 |= NDCB0_CMD_TYPE(4)
659 | NDCB0_ADDR_CYC(1)
660 | cmd;
661
662 info->data_size = 8;
663 break;
664
665 case NAND_CMD_ERASE1:
Lei Wend4568822011-07-14 20:44:32 -0700666 cmd = host->cmdset->erase;
Lei Wen4eb2da82011-02-28 10:32:13 +0800667 info->ndcb0 |= NDCB0_CMD_TYPE(2)
668 | NDCB0_AUTO_RS
669 | NDCB0_ADDR_CYC(3)
670 | NDCB0_DBC
671 | cmd;
672 info->ndcb1 = page_addr;
673 info->ndcb2 = 0;
674
675 break;
676 case NAND_CMD_RESET:
Lei Wend4568822011-07-14 20:44:32 -0700677 cmd = host->cmdset->reset;
Lei Wen4eb2da82011-02-28 10:32:13 +0800678 info->ndcb0 |= NDCB0_CMD_TYPE(5)
679 | cmd;
680
681 break;
682
683 case NAND_CMD_ERASE2:
684 exec_cmd = 0;
685 break;
686
687 default:
688 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700689 dev_err(&info->pdev->dev, "non-supported command %x\n",
690 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800691 break;
692 }
693
694 return exec_cmd;
695}
696
eric miaofe69af02008-02-14 15:48:23 +0800697static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100698 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800699{
Lei Wend4568822011-07-14 20:44:32 -0700700 struct pxa3xx_nand_host *host = mtd->priv;
701 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800702 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800703
Lei Wen4eb2da82011-02-28 10:32:13 +0800704 /*
705 * if this is a x16 device ,then convert the input
706 * "byte" address into a "word" address appropriate
707 * for indexing a word-oriented device
708 */
Lei Wend4568822011-07-14 20:44:32 -0700709 if (host->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800710 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800711
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700712 /*
713 * There may be different NAND chip hooked to
714 * different chip select, so check whether
715 * chip select has been changed, if yes, reset the timing
716 */
717 if (info->cs != host->cs) {
718 info->cs = host->cs;
719 nand_writel(info, NDTR0CS0, host->ndtr0cs0);
720 nand_writel(info, NDTR1CS0, host->ndtr1cs0);
721 }
722
Lei Wend4568822011-07-14 20:44:32 -0700723 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800724 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800725 if (exec_cmd) {
726 init_completion(&info->cmd_complete);
727 pxa3xx_nand_start(info);
728
729 ret = wait_for_completion_timeout(&info->cmd_complete,
730 CHIP_DELAY_TIMEOUT);
731 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700732 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800733 /* Stop State Machine for next command cycle */
734 pxa3xx_nand_stop(info);
735 }
eric miaofe69af02008-02-14 15:48:23 +0800736 }
Lei Wend4568822011-07-14 20:44:32 -0700737 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800738}
739
Josh Wufdbad98d2012-06-25 18:07:45 +0800740static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700741 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800742{
743 chip->write_buf(mtd, buf, mtd->writesize);
744 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800745
746 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800747}
748
749static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700750 struct nand_chip *chip, uint8_t *buf, int oob_required,
751 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800752{
Lei Wend4568822011-07-14 20:44:32 -0700753 struct pxa3xx_nand_host *host = mtd->priv;
754 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800755
756 chip->read_buf(mtd, buf, mtd->writesize);
757 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
758
759 if (info->retcode == ERR_SBERR) {
760 switch (info->use_ecc) {
761 case 1:
762 mtd->ecc_stats.corrected++;
763 break;
764 case 0:
765 default:
766 break;
767 }
768 } else if (info->retcode == ERR_DBERR) {
769 /*
770 * for blank page (all 0xff), HW will calculate its ECC as
771 * 0, which is different from the ECC information within
772 * OOB, ignore such double bit errors
773 */
774 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700775 info->retcode = ERR_NONE;
776 else
Lei Wenf8155a42011-02-28 10:32:11 +0800777 mtd->ecc_stats.failed++;
778 }
779
780 return 0;
781}
782
eric miaofe69af02008-02-14 15:48:23 +0800783static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
784{
Lei Wend4568822011-07-14 20:44:32 -0700785 struct pxa3xx_nand_host *host = mtd->priv;
786 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800787 char retval = 0xFF;
788
789 if (info->buf_start < info->buf_count)
790 /* Has just send a new command? */
791 retval = info->data_buff[info->buf_start++];
792
793 return retval;
794}
795
796static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
797{
Lei Wend4568822011-07-14 20:44:32 -0700798 struct pxa3xx_nand_host *host = mtd->priv;
799 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800800 u16 retval = 0xFFFF;
801
802 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
803 retval = *((u16 *)(info->data_buff+info->buf_start));
804 info->buf_start += 2;
805 }
806 return retval;
807}
808
809static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
810{
Lei Wend4568822011-07-14 20:44:32 -0700811 struct pxa3xx_nand_host *host = mtd->priv;
812 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800813 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
814
815 memcpy(buf, info->data_buff + info->buf_start, real_len);
816 info->buf_start += real_len;
817}
818
819static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
820 const uint8_t *buf, int len)
821{
Lei Wend4568822011-07-14 20:44:32 -0700822 struct pxa3xx_nand_host *host = mtd->priv;
823 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800824 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
825
826 memcpy(info->data_buff + info->buf_start, buf, real_len);
827 info->buf_start += real_len;
828}
829
eric miaofe69af02008-02-14 15:48:23 +0800830static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
831{
832 return;
833}
834
835static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
836{
Lei Wend4568822011-07-14 20:44:32 -0700837 struct pxa3xx_nand_host *host = mtd->priv;
838 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800839
840 /* pxa3xx_nand_send_command has waited for command complete */
841 if (this->state == FL_WRITING || this->state == FL_ERASING) {
842 if (info->retcode == ERR_NONE)
843 return 0;
844 else {
845 /*
846 * any error make it return 0x01 which will tell
847 * the caller the erase and write fail
848 */
849 return 0x01;
850 }
851 }
852
853 return 0;
854}
855
eric miaofe69af02008-02-14 15:48:23 +0800856static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200857 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800858{
859 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900860 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700861 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800862 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800863
Lei Wenda675b42011-07-14 20:44:31 -0700864 if (f->page_size != 2048 && f->page_size != 512) {
865 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800866 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700867 }
eric miaofe69af02008-02-14 15:48:23 +0800868
Lei Wenda675b42011-07-14 20:44:31 -0700869 if (f->flash_width != 16 && f->flash_width != 8) {
870 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800871 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700872 }
eric miaofe69af02008-02-14 15:48:23 +0800873
874 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700875 host->cmdset = &default_cmdset;
876 host->page_size = f->page_size;
877 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800878
879 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700880 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800881
882 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700883 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800884 else
Lei Wend4568822011-07-14 20:44:32 -0700885 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800886
887 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700888 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800889 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
890 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
891 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
892 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
893
Lei Wend4568822011-07-14 20:44:32 -0700894 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800895 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
896
Lei Wend4568822011-07-14 20:44:32 -0700897 host->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800898
Lei Wend4568822011-07-14 20:44:32 -0700899 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800900 return 0;
901}
902
Mike Rapoportf2710492009-02-17 13:54:47 +0200903static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
904{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700905 /*
906 * We set 0 by hard coding here, for we don't support keep_config
907 * when there is more than one chip attached to the controller
908 */
909 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200910 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200911
Lei Wend4568822011-07-14 20:44:32 -0700912 if (ndcr & NDCR_PAGE_SZ) {
913 host->page_size = 2048;
914 host->read_id_bytes = 4;
915 } else {
916 host->page_size = 512;
917 host->read_id_bytes = 2;
918 }
919
920 host->reg_ndcr = ndcr & ~NDCR_INT_MASK;
921 host->cmdset = &default_cmdset;
922
923 host->ndtr0cs0 = nand_readl(info, NDTR0CS0);
924 host->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200925
926 return 0;
927}
928
eric miaofe69af02008-02-14 15:48:23 +0800929/* the maximum possible buffer size for large page with OOB data
930 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
931 * data buffer and the DMA descriptor
932 */
933#define MAX_BUFF_SIZE PAGE_SIZE
934
935static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
936{
937 struct platform_device *pdev = info->pdev;
938 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
939
940 if (use_dma == 0) {
941 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
942 if (info->data_buff == NULL)
943 return -ENOMEM;
944 return 0;
945 }
946
947 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
948 &info->data_buff_phys, GFP_KERNEL);
949 if (info->data_buff == NULL) {
950 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
951 return -ENOMEM;
952 }
953
eric miaofe69af02008-02-14 15:48:23 +0800954 info->data_desc = (void *)info->data_buff + data_desc_offset;
955 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
956
957 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
958 pxa3xx_nand_data_dma_irq, info);
959 if (info->data_dma_ch < 0) {
960 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700961 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800962 info->data_buff, info->data_buff_phys);
963 return info->data_dma_ch;
964 }
965
966 return 0;
967}
968
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300969static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
970{
971 struct platform_device *pdev = info->pdev;
972 if (use_dma) {
973 pxa_free_dma(info->data_dma_ch);
974 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
975 info->data_buff, info->data_buff_phys);
976 } else {
977 kfree(info->data_buff);
978 }
979}
980
Lei Wen401e67e2011-02-28 10:32:14 +0800981static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800982{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700983 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700984 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700985 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800986 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700987 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
988 if (ret)
989 return ret;
990
991 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800992 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800993 return 0;
Lei Wend4568822011-07-14 20:44:32 -0700994
995 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +0800996}
eric miaofe69af02008-02-14 15:48:23 +0800997
Lei Wen401e67e2011-02-28 10:32:14 +0800998static int pxa3xx_nand_scan(struct mtd_info *mtd)
999{
Lei Wend4568822011-07-14 20:44:32 -07001000 struct pxa3xx_nand_host *host = mtd->priv;
1001 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001002 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001003 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001004 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001005 const struct pxa3xx_nand_flash *f = NULL;
1006 struct nand_chip *chip = mtd->priv;
1007 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001008 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001009 int i, ret, num;
1010
1011 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001012 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001013
1014 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001015 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001016 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1017 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001018
Lei Wend4568822011-07-14 20:44:32 -07001019 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001020 }
1021
1022 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1023 id = *((uint16_t *)(info->data_buff));
1024 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001025 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001026 else {
Lei Wenda675b42011-07-14 20:44:31 -07001027 dev_warn(&info->pdev->dev,
1028 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001029
1030 return -EINVAL;
1031 }
1032
1033 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1034 for (i = 0; i < num; i++) {
1035 if (i < pdata->num_flash)
1036 f = pdata->flash + i;
1037 else
1038 f = &builtin_flash_types[i - pdata->num_flash + 1];
1039
1040 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001041 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001042 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001043 }
1044
Lei Wen4332c112011-03-03 11:27:01 +08001045 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001046 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001047
1048 return -EINVAL;
1049 }
1050
Lei Wend4568822011-07-14 20:44:32 -07001051 ret = pxa3xx_nand_config_flash(info, f);
1052 if (ret) {
1053 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1054 return ret;
1055 }
1056
Lei Wen4332c112011-03-03 11:27:01 +08001057 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001058 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001059 pxa3xx_flash_ids[0].pagesize = f->page_size;
1060 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1061 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1062 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1063 if (f->flash_width == 16)
1064 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001065 pxa3xx_flash_ids[1].name = NULL;
1066 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001067KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001068 chip->ecc.mode = NAND_ECC_HW;
1069 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001070 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001071
Lei Wend4568822011-07-14 20:44:32 -07001072 if (host->reg_ndcr & NDCR_DWIDTH_M)
1073 chip->options |= NAND_BUSWIDTH_16;
1074
Lei Wen0fab0282011-06-07 03:01:06 -07001075 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001076 return -ENODEV;
1077 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001078 if (mtd->writesize >= 2048)
1079 host->col_addr_cycles = 2;
1080 else
1081 host->col_addr_cycles = 1;
1082
Lei Wen4332c112011-03-03 11:27:01 +08001083 info->oob_buff = info->data_buff + mtd->writesize;
1084 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001085 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001086 else
Lei Wend4568822011-07-14 20:44:32 -07001087 host->row_addr_cycles = 2;
1088
Lei Wen4332c112011-03-03 11:27:01 +08001089 mtd->name = mtd_names[0];
Lei Wen401e67e2011-02-28 10:32:14 +08001090 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001091}
1092
Lei Wend4568822011-07-14 20:44:32 -07001093static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001094{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001095 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001096 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001097 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001098 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001099 struct mtd_info *mtd;
1100 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001101 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001102
Jingoo Han453810b2013-07-30 17:18:33 +09001103 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001104 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1105 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1106 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001107 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001108
eric miaofe69af02008-02-14 15:48:23 +08001109 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001110 for (cs = 0; cs < pdata->num_cs; cs++) {
1111 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1112 (sizeof(*mtd) + sizeof(*host)) * cs);
1113 chip = (struct nand_chip *)(&mtd[1]);
1114 host = (struct pxa3xx_nand_host *)chip;
1115 info->host[cs] = host;
1116 host->mtd = mtd;
1117 host->cs = cs;
1118 host->info_data = info;
1119 mtd->priv = host;
1120 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001121
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001122 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1123 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1124 chip->controller = &info->controller;
1125 chip->waitfunc = pxa3xx_nand_waitfunc;
1126 chip->select_chip = pxa3xx_nand_select_chip;
1127 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1128 chip->read_word = pxa3xx_nand_read_word;
1129 chip->read_byte = pxa3xx_nand_read_byte;
1130 chip->read_buf = pxa3xx_nand_read_buf;
1131 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001132 }
Lei Wen401e67e2011-02-28 10:32:14 +08001133
1134 spin_lock_init(&chip->controller->lock);
1135 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001136 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001137 if (IS_ERR(info->clk)) {
1138 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001139 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001140 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001141 ret = clk_prepare_enable(info->clk);
1142 if (ret < 0)
1143 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001144
Daniel Mack1e7ba632012-07-22 19:51:02 +02001145 /*
1146 * This is a dirty hack to make this driver work from devicetree
1147 * bindings. It can be removed once we have a prober DMA controller
1148 * framework for DT.
1149 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001150 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001151 info->drcmr_dat = 97;
1152 info->drcmr_cmd = 99;
1153 } else {
1154 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1155 if (r == NULL) {
1156 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1157 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001158 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001159 }
1160 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001161
Daniel Mack1e7ba632012-07-22 19:51:02 +02001162 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1163 if (r == NULL) {
1164 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1165 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001166 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001167 }
1168 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001169 }
eric miaofe69af02008-02-14 15:48:23 +08001170
1171 irq = platform_get_irq(pdev, 0);
1172 if (irq < 0) {
1173 dev_err(&pdev->dev, "no IRQ resource defined\n");
1174 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001175 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001176 }
1177
1178 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001179 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1180 if (IS_ERR(info->mmio_base)) {
1181 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001182 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001183 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001184 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001185
1186 ret = pxa3xx_nand_init_buff(info);
1187 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001188 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001189
Haojian Zhuang346e1252009-09-10 14:27:23 +08001190 /* initialize all interrupts to be disabled */
1191 disable_int(info, NDSR_MASK);
1192
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001193 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1194 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001195 if (ret < 0) {
1196 dev_err(&pdev->dev, "failed to request IRQ\n");
1197 goto fail_free_buf;
1198 }
1199
Lei Wene353a202011-03-03 11:08:30 +08001200 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001201
Lei Wend4568822011-07-14 20:44:32 -07001202 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001203
eric miaofe69af02008-02-14 15:48:23 +08001204fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001205 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001206 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001207fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001208 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001209 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001210}
1211
1212static int pxa3xx_nand_remove(struct platform_device *pdev)
1213{
Lei Wene353a202011-03-03 11:08:30 +08001214 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001215 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001216 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001217
Lei Wend4568822011-07-14 20:44:32 -07001218 if (!info)
1219 return 0;
1220
Jingoo Han453810b2013-07-30 17:18:33 +09001221 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001222
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001223 irq = platform_get_irq(pdev, 0);
1224 if (irq >= 0)
1225 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001226 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001227
Ezequiel Garciafb320612013-04-17 13:38:12 -03001228 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001229
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001230 for (cs = 0; cs < pdata->num_cs; cs++)
1231 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001232 return 0;
1233}
1234
Daniel Mack1e7ba632012-07-22 19:51:02 +02001235#ifdef CONFIG_OF
1236static struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001237 {
1238 .compatible = "marvell,pxa3xx-nand",
1239 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1240 },
1241 {
1242 .compatible = "marvell,armada370-nand",
1243 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1244 },
Daniel Mack1e7ba632012-07-22 19:51:02 +02001245 {}
1246};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001247MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001248
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001249static enum pxa3xx_nand_variant
1250pxa3xx_nand_get_variant(struct platform_device *pdev)
1251{
1252 const struct of_device_id *of_id =
1253 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1254 if (!of_id)
1255 return PXA3XX_NAND_VARIANT_PXA;
1256 return (enum pxa3xx_nand_variant)of_id->data;
1257}
1258
Daniel Mack1e7ba632012-07-22 19:51:02 +02001259static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1260{
1261 struct pxa3xx_nand_platform_data *pdata;
1262 struct device_node *np = pdev->dev.of_node;
1263 const struct of_device_id *of_id =
1264 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1265
1266 if (!of_id)
1267 return 0;
1268
1269 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1270 if (!pdata)
1271 return -ENOMEM;
1272
1273 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1274 pdata->enable_arbiter = 1;
1275 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1276 pdata->keep_config = 1;
1277 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1278
1279 pdev->dev.platform_data = pdata;
1280
1281 return 0;
1282}
1283#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001284static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001285{
1286 return 0;
1287}
1288#endif
1289
Lei Wene353a202011-03-03 11:08:30 +08001290static int pxa3xx_nand_probe(struct platform_device *pdev)
1291{
1292 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001293 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001294 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001295 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001296
Daniel Mack1e7ba632012-07-22 19:51:02 +02001297 ret = pxa3xx_nand_probe_dt(pdev);
1298 if (ret)
1299 return ret;
1300
Jingoo Han453810b2013-07-30 17:18:33 +09001301 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001302 if (!pdata) {
1303 dev_err(&pdev->dev, "no platform data defined\n");
1304 return -ENODEV;
1305 }
1306
Lei Wend4568822011-07-14 20:44:32 -07001307 ret = alloc_nand_resource(pdev);
1308 if (ret) {
1309 dev_err(&pdev->dev, "alloc nand resource failed\n");
1310 return ret;
1311 }
Lei Wene353a202011-03-03 11:08:30 +08001312
Lei Wend4568822011-07-14 20:44:32 -07001313 info = platform_get_drvdata(pdev);
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001314 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001315 probe_success = 0;
1316 for (cs = 0; cs < pdata->num_cs; cs++) {
1317 info->cs = cs;
1318 ret = pxa3xx_nand_scan(info->host[cs]->mtd);
1319 if (ret) {
1320 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1321 cs);
1322 continue;
1323 }
1324
Daniel Mack1e7ba632012-07-22 19:51:02 +02001325 ppdata.of_node = pdev->dev.of_node;
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001326 ret = mtd_device_parse_register(info->host[cs]->mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001327 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001328 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001329 if (!ret)
1330 probe_success = 1;
1331 }
1332
1333 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001334 pxa3xx_nand_remove(pdev);
1335 return -ENODEV;
1336 }
1337
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001338 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001339}
1340
eric miaofe69af02008-02-14 15:48:23 +08001341#ifdef CONFIG_PM
1342static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1343{
Lei Wene353a202011-03-03 11:08:30 +08001344 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001345 struct pxa3xx_nand_platform_data *pdata;
1346 struct mtd_info *mtd;
1347 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001348
Jingoo Han453810b2013-07-30 17:18:33 +09001349 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001350 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001351 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1352 return -EAGAIN;
1353 }
1354
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001355 for (cs = 0; cs < pdata->num_cs; cs++) {
1356 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001357 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001358 }
1359
eric miaofe69af02008-02-14 15:48:23 +08001360 return 0;
1361}
1362
1363static int pxa3xx_nand_resume(struct platform_device *pdev)
1364{
Lei Wene353a202011-03-03 11:08:30 +08001365 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001366 struct pxa3xx_nand_platform_data *pdata;
1367 struct mtd_info *mtd;
1368 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001369
Jingoo Han453810b2013-07-30 17:18:33 +09001370 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001371 /* We don't want to handle interrupt without calling mtd routine */
1372 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001373
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001374 /*
1375 * Directly set the chip select to a invalid value,
1376 * then the driver would reset the timing according
1377 * to current chip select at the beginning of cmdfunc
1378 */
1379 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001380
Lei Wen051fc412011-07-14 20:44:30 -07001381 /*
1382 * As the spec says, the NDSR would be updated to 0x1800 when
1383 * doing the nand_clk disable/enable.
1384 * To prevent it damaging state machine of the driver, clear
1385 * all status before resume
1386 */
1387 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001388 for (cs = 0; cs < pdata->num_cs; cs++) {
1389 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001390 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001391 }
1392
Lei Wen18c81b12010-08-17 17:25:57 +08001393 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001394}
1395#else
1396#define pxa3xx_nand_suspend NULL
1397#define pxa3xx_nand_resume NULL
1398#endif
1399
1400static struct platform_driver pxa3xx_nand_driver = {
1401 .driver = {
1402 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001403 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001404 },
1405 .probe = pxa3xx_nand_probe,
1406 .remove = pxa3xx_nand_remove,
1407 .suspend = pxa3xx_nand_suspend,
1408 .resume = pxa3xx_nand_resume,
1409};
1410
Axel Linf99640d2011-11-27 20:45:03 +08001411module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001412
1413MODULE_LICENSE("GPL");
1414MODULE_DESCRIPTION("PXA3xx NAND controller driver");