blob: 3779b70623a0e3fddbdfc42809c6dc23c4243613 [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.
Ezequiel Garciade484a32013-11-07 12:17:10 -030010 *
11 * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
eric miaofe69af02008-02-14 15:48:23 +080012 */
13
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080014#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080015#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/clk.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/nand.h>
23#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010024#include <linux/io.h>
25#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020027#include <linux/of.h>
28#include <linux/of_device.h>
eric miaofe69af02008-02-14 15:48:23 +080029
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030030#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
31#define ARCH_HAS_DMA
32#endif
33
34#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080035#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030036#endif
37
Arnd Bergmann293b2da2012-08-24 15:16:48 +020038#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080039
40#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080041#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080042#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080043
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030044/*
45 * Define a buffer size for the initial command that detects the flash device:
46 * STATUS, READID and PARAM. The largest of these is the PARAM command,
47 * needing 256 bytes.
48 */
49#define INIT_BUFFER_SIZE 256
50
eric miaofe69af02008-02-14 15:48:23 +080051/* registers and bit definitions */
52#define NDCR (0x00) /* Control register */
53#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
54#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
55#define NDSR (0x14) /* Status Register */
56#define NDPCR (0x18) /* Page Count Register */
57#define NDBDR0 (0x1C) /* Bad Block Register 0 */
58#define NDBDR1 (0x20) /* Bad Block Register 1 */
59#define NDDB (0x40) /* Data Buffer */
60#define NDCB0 (0x48) /* Command Buffer0 */
61#define NDCB1 (0x4C) /* Command Buffer1 */
62#define NDCB2 (0x50) /* Command Buffer2 */
63
64#define NDCR_SPARE_EN (0x1 << 31)
65#define NDCR_ECC_EN (0x1 << 30)
66#define NDCR_DMA_EN (0x1 << 29)
67#define NDCR_ND_RUN (0x1 << 28)
68#define NDCR_DWIDTH_C (0x1 << 27)
69#define NDCR_DWIDTH_M (0x1 << 26)
70#define NDCR_PAGE_SZ (0x1 << 24)
71#define NDCR_NCSX (0x1 << 23)
72#define NDCR_ND_MODE (0x3 << 21)
73#define NDCR_NAND_MODE (0x0)
74#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080075#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080076#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
77#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
78
79#define NDCR_RA_START (0x1 << 15)
80#define NDCR_PG_PER_BLK (0x1 << 14)
81#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080082#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080083
84#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080085#define NDSR_RDY (0x1 << 12)
86#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080087#define NDSR_CS0_PAGED (0x1 << 10)
88#define NDSR_CS1_PAGED (0x1 << 9)
89#define NDSR_CS0_CMDD (0x1 << 8)
90#define NDSR_CS1_CMDD (0x1 << 7)
91#define NDSR_CS0_BBD (0x1 << 6)
92#define NDSR_CS1_BBD (0x1 << 5)
93#define NDSR_DBERR (0x1 << 4)
94#define NDSR_SBERR (0x1 << 3)
95#define NDSR_WRDREQ (0x1 << 2)
96#define NDSR_RDDREQ (0x1 << 1)
97#define NDSR_WRCMDREQ (0x1)
98
Ezequiel Garcia41a63432013-08-12 14:14:51 -030099#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800100#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800101#define NDCB0_AUTO_RS (0x1 << 25)
102#define NDCB0_CSEL (0x1 << 24)
103#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
104#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
105#define NDCB0_NC (0x1 << 20)
106#define NDCB0_DBC (0x1 << 19)
107#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
108#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
109#define NDCB0_CMD2_MASK (0xff << 8)
110#define NDCB0_CMD1_MASK (0xff)
111#define NDCB0_ADDR_CYC_SHIFT (16)
112
eric miaofe69af02008-02-14 15:48:23 +0800113/* macros for registers read/write */
114#define nand_writel(info, off, val) \
115 __raw_writel((val), (info)->mmio_base + (off))
116
117#define nand_readl(info, off) \
118 __raw_readl((info)->mmio_base + (off))
119
120/* error code and state */
121enum {
122 ERR_NONE = 0,
123 ERR_DMABUSERR = -1,
124 ERR_SENDCMD = -2,
125 ERR_DBERR = -3,
126 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300127 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800128};
129
130enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800131 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700132 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800133 STATE_CMD_HANDLE,
134 STATE_DMA_READING,
135 STATE_DMA_WRITING,
136 STATE_DMA_DONE,
137 STATE_PIO_READING,
138 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800139 STATE_CMD_DONE,
140 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800141};
142
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300143enum pxa3xx_nand_variant {
144 PXA3XX_NAND_VARIANT_PXA,
145 PXA3XX_NAND_VARIANT_ARMADA370,
146};
147
Lei Wend4568822011-07-14 20:44:32 -0700148struct pxa3xx_nand_host {
149 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700150 struct mtd_info *mtd;
151 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800152
Lei Wend4568822011-07-14 20:44:32 -0700153 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700154 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700155 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700156
157 /* calculated from pxa3xx_nand_flash data */
158 unsigned int col_addr_cycles;
159 unsigned int row_addr_cycles;
160 size_t read_id_bytes;
161
Lei Wend4568822011-07-14 20:44:32 -0700162};
163
164struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800165 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800166 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800167
168 struct clk *clk;
169 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800170 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700171 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800172
173 unsigned int buf_start;
174 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300175 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800176
177 /* DMA information */
178 int drcmr_dat;
179 int drcmr_cmd;
180
181 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800182 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800183 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800184 int data_dma_ch;
185 struct pxa_dma_desc *data_desc;
186 dma_addr_t data_desc_addr;
187
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700188 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800189 unsigned int state;
190
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300191 /*
192 * This driver supports NFCv1 (as found in PXA SoC)
193 * and NFCv2 (as found in Armada 370/XP SoC).
194 */
195 enum pxa3xx_nand_variant variant;
196
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700197 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800198 int use_ecc; /* use HW ECC ? */
199 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300200 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800201 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800202
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300203 unsigned int fifo_size; /* max. data size in the FIFO */
204 unsigned int data_size; /* data to be read from FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700205 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800206 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800207
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300208 /* cached register value */
209 uint32_t reg_ndcr;
210 uint32_t ndtr0cs0;
211 uint32_t ndtr1cs0;
212
eric miaofe69af02008-02-14 15:48:23 +0800213 /* generated NDCBx register values */
214 uint32_t ndcb0;
215 uint32_t ndcb1;
216 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300217 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800218};
219
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030220static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800221module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300222MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800223
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
eric miaofe69af02008-02-14 15:48:23 +0800246#define NDTR0_tCH(c) (min((c), 7) << 19)
247#define NDTR0_tCS(c) (min((c), 7) << 16)
248#define NDTR0_tWH(c) (min((c), 7) << 11)
249#define NDTR0_tWP(c) (min((c), 7) << 8)
250#define NDTR0_tRH(c) (min((c), 7) << 3)
251#define NDTR0_tRP(c) (min((c), 7) << 0)
252
253#define NDTR1_tR(c) (min((c), 65535) << 16)
254#define NDTR1_tWHR(c) (min((c), 15) << 4)
255#define NDTR1_tAR(c) (min((c), 15) << 0)
256
257/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800258#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800259
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300260static struct of_device_id pxa3xx_nand_dt_ids[] = {
261 {
262 .compatible = "marvell,pxa3xx-nand",
263 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
264 },
265 {}
266};
267MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
268
269static enum pxa3xx_nand_variant
270pxa3xx_nand_get_variant(struct platform_device *pdev)
271{
272 const struct of_device_id *of_id =
273 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
274 if (!of_id)
275 return PXA3XX_NAND_VARIANT_PXA;
276 return (enum pxa3xx_nand_variant)of_id->data;
277}
278
Lei Wend4568822011-07-14 20:44:32 -0700279static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200280 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800281{
Lei Wend4568822011-07-14 20:44:32 -0700282 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800283 unsigned long nand_clk = clk_get_rate(info->clk);
284 uint32_t ndtr0, ndtr1;
285
286 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
287 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
288 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
289 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
290 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
291 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
292
293 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
294 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
295 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
296
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300297 info->ndtr0cs0 = ndtr0;
298 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800299 nand_writel(info, NDTR0CS0, ndtr0);
300 nand_writel(info, NDTR1CS0, ndtr1);
301}
302
Lei Wen18c81b12010-08-17 17:25:57 +0800303static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800304{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300305 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800306
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300307 info->data_size = info->fifo_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800308 if (!oob_enable) {
309 info->oob_size = 0;
310 return;
311 }
312
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300313 switch (info->fifo_size) {
eric miaofe69af02008-02-14 15:48:23 +0800314 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800315 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800316 break;
317 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800318 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800319 break;
eric miaofe69af02008-02-14 15:48:23 +0800320 }
Lei Wen18c81b12010-08-17 17:25:57 +0800321}
322
Lei Wenf8155a42011-02-28 10:32:11 +0800323/**
324 * NOTE: it is a must to set ND_RUN firstly, then write
325 * command buffer, otherwise, it does not work.
326 * We enable all the interrupt at the same time, and
327 * let pxa3xx_nand_irq to handle all logic.
328 */
329static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
330{
331 uint32_t ndcr;
332
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300333 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300334
335 if (info->use_ecc)
336 ndcr |= NDCR_ECC_EN;
337 else
338 ndcr &= ~NDCR_ECC_EN;
339
340 if (info->use_dma)
341 ndcr |= NDCR_DMA_EN;
342 else
343 ndcr &= ~NDCR_DMA_EN;
344
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300345 if (info->use_spare)
346 ndcr |= NDCR_SPARE_EN;
347 else
348 ndcr &= ~NDCR_SPARE_EN;
349
Lei Wenf8155a42011-02-28 10:32:11 +0800350 ndcr |= NDCR_ND_RUN;
351
352 /* clear status bits and run */
353 nand_writel(info, NDCR, 0);
354 nand_writel(info, NDSR, NDSR_MASK);
355 nand_writel(info, NDCR, ndcr);
356}
357
358static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
359{
360 uint32_t ndcr;
361 int timeout = NAND_STOP_DELAY;
362
363 /* wait RUN bit in NDCR become 0 */
364 ndcr = nand_readl(info, NDCR);
365 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
366 ndcr = nand_readl(info, NDCR);
367 udelay(1);
368 }
369
370 if (timeout <= 0) {
371 ndcr &= ~NDCR_ND_RUN;
372 nand_writel(info, NDCR, ndcr);
373 }
374 /* clear status bits */
375 nand_writel(info, NDSR, NDSR_MASK);
376}
377
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300378static void __maybe_unused
379enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800380{
381 uint32_t ndcr;
382
383 ndcr = nand_readl(info, NDCR);
384 nand_writel(info, NDCR, ndcr & ~int_mask);
385}
386
387static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
388{
389 uint32_t ndcr;
390
391 ndcr = nand_readl(info, NDCR);
392 nand_writel(info, NDCR, ndcr | int_mask);
393}
394
Lei Wenf8155a42011-02-28 10:32:11 +0800395static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800396{
eric miaofe69af02008-02-14 15:48:23 +0800397 switch (info->state) {
398 case STATE_PIO_WRITING:
399 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800400 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800401 if (info->oob_size > 0)
402 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
403 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800404 break;
405 case STATE_PIO_READING:
406 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800407 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800408 if (info->oob_size > 0)
409 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
410 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800411 break;
412 default:
Lei Wenda675b42011-07-14 20:44:31 -0700413 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800414 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800415 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800416 }
eric miaofe69af02008-02-14 15:48:23 +0800417}
418
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300419#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800420static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800421{
422 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800423 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800424
425 desc->ddadr = DDADR_STOP;
426 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
427
Lei Wenf8155a42011-02-28 10:32:11 +0800428 switch (info->state) {
429 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800430 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800431 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800432 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800433 break;
434 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800435 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800436 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800437 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800438 break;
439 default:
Lei Wenda675b42011-07-14 20:44:31 -0700440 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800441 info->state);
442 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800443 }
444
445 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
446 DDADR(info->data_dma_ch) = info->data_desc_addr;
447 DCSR(info->data_dma_ch) |= DCSR_RUN;
448}
449
450static void pxa3xx_nand_data_dma_irq(int channel, void *data)
451{
452 struct pxa3xx_nand_info *info = data;
453 uint32_t dcsr;
454
455 dcsr = DCSR(channel);
456 DCSR(channel) = dcsr;
457
458 if (dcsr & DCSR_BUSERR) {
459 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800460 }
461
Lei Wenf8155a42011-02-28 10:32:11 +0800462 info->state = STATE_DMA_DONE;
463 enable_int(info, NDCR_INT_MASK);
464 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800465}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300466#else
467static void start_data_dma(struct pxa3xx_nand_info *info)
468{}
469#endif
eric miaofe69af02008-02-14 15:48:23 +0800470
471static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
472{
473 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800474 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700475 unsigned int ready, cmd_done;
476
477 if (info->cs == 0) {
478 ready = NDSR_FLASH_RDY;
479 cmd_done = NDSR_CS0_CMDD;
480 } else {
481 ready = NDSR_RDY;
482 cmd_done = NDSR_CS1_CMDD;
483 }
eric miaofe69af02008-02-14 15:48:23 +0800484
485 status = nand_readl(info, NDSR);
486
Lei Wenf8155a42011-02-28 10:32:11 +0800487 if (status & NDSR_DBERR)
488 info->retcode = ERR_DBERR;
489 if (status & NDSR_SBERR)
490 info->retcode = ERR_SBERR;
491 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
492 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800493 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800494 disable_int(info, NDCR_INT_MASK);
495 info->state = (status & NDSR_RDDREQ) ?
496 STATE_DMA_READING : STATE_DMA_WRITING;
497 start_data_dma(info);
498 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800499 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800500 info->state = (status & NDSR_RDDREQ) ?
501 STATE_PIO_READING : STATE_PIO_WRITING;
502 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800503 }
Lei Wenf8155a42011-02-28 10:32:11 +0800504 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700505 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800506 info->state = STATE_CMD_DONE;
507 is_completed = 1;
508 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700509 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800510 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800511 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800512 }
Lei Wenf8155a42011-02-28 10:32:11 +0800513
514 if (status & NDSR_WRCMDREQ) {
515 nand_writel(info, NDSR, NDSR_WRCMDREQ);
516 status &= ~NDSR_WRCMDREQ;
517 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300518
519 /*
520 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
521 * must be loaded by writing directly either 12 or 16
522 * bytes directly to NDCB0, four bytes at a time.
523 *
524 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
525 * but each NDCBx register can be read.
526 */
Lei Wenf8155a42011-02-28 10:32:11 +0800527 nand_writel(info, NDCB0, info->ndcb0);
528 nand_writel(info, NDCB0, info->ndcb1);
529 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300530
531 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
532 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
533 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800534 }
Lei Wenf8155a42011-02-28 10:32:11 +0800535
536 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800537 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800538 if (is_completed)
539 complete(&info->cmd_complete);
540NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800541 return IRQ_HANDLED;
542}
543
eric miaofe69af02008-02-14 15:48:23 +0800544static inline int is_buf_blank(uint8_t *buf, size_t len)
545{
546 for (; len > 0; len--)
547 if (*buf++ != 0xff)
548 return 0;
549 return 1;
550}
551
Lei Wen4eb2da82011-02-28 10:32:13 +0800552static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
553 uint16_t column, int page_addr)
554{
Lei Wend4568822011-07-14 20:44:32 -0700555 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700556 struct pxa3xx_nand_host *host;
557 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800558
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700559 host = info->host[info->cs];
560 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800561 addr_cycle = 0;
562 exec_cmd = 1;
563
564 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800565 info->buf_start = 0;
566 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800567 info->oob_size = 0;
568 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300569 info->use_spare = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800570 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800571 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700572 if (info->cs != 0)
573 info->ndcb0 = NDCB0_CSEL;
574 else
575 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800576
577 switch (command) {
578 case NAND_CMD_READ0:
579 case NAND_CMD_PAGEPROG:
580 info->use_ecc = 1;
581 case NAND_CMD_READOOB:
582 pxa3xx_set_datasize(info);
583 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300584 case NAND_CMD_PARAM:
585 info->use_spare = 0;
586 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800587 case NAND_CMD_SEQIN:
588 exec_cmd = 0;
589 break;
590 default:
591 info->ndcb1 = 0;
592 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300593 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800594 break;
595 }
596
Lei Wend4568822011-07-14 20:44:32 -0700597 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
598 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800599
600 switch (command) {
601 case NAND_CMD_READOOB:
602 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300603 info->buf_start = column;
604 info->ndcb0 |= NDCB0_CMD_TYPE(0)
605 | addr_cycle
606 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800607
Ezequiel Garciaec821352013-08-12 14:14:54 -0300608 if (command == NAND_CMD_READOOB)
609 info->buf_start += mtd->writesize;
610
611 /* Second command setting for large pages */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300612 if (mtd->writesize >= PAGE_CHUNK_SIZE)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300613 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800614
615 case NAND_CMD_SEQIN:
616 /* small page addr setting */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300617 if (unlikely(mtd->writesize < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800618 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
619 | (column & 0xFF);
620
621 info->ndcb2 = 0;
622 } else {
623 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
624 | (column & 0xFFFF);
625
626 if (page_addr & 0xFF0000)
627 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
628 else
629 info->ndcb2 = 0;
630 }
631
632 info->buf_count = mtd->writesize + mtd->oobsize;
633 memset(info->data_buff, 0xFF, info->buf_count);
634
635 break;
636
637 case NAND_CMD_PAGEPROG:
638 if (is_buf_blank(info->data_buff,
639 (mtd->writesize + mtd->oobsize))) {
640 exec_cmd = 0;
641 break;
642 }
643
Lei Wen4eb2da82011-02-28 10:32:13 +0800644 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
645 | NDCB0_AUTO_RS
646 | NDCB0_ST_ROW_EN
647 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300648 | (NAND_CMD_PAGEPROG << 8)
649 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800650 | addr_cycle;
651 break;
652
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300653 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300654 info->buf_count = 256;
655 info->ndcb0 |= NDCB0_CMD_TYPE(0)
656 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300657 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300658 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300659 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300660 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300661 info->data_size = 256;
662 break;
663
Lei Wen4eb2da82011-02-28 10:32:13 +0800664 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700665 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800666 info->ndcb0 |= NDCB0_CMD_TYPE(3)
667 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300668 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300669 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800670
671 info->data_size = 8;
672 break;
673 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800674 info->buf_count = 1;
675 info->ndcb0 |= NDCB0_CMD_TYPE(4)
676 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300677 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800678
679 info->data_size = 8;
680 break;
681
682 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800683 info->ndcb0 |= NDCB0_CMD_TYPE(2)
684 | NDCB0_AUTO_RS
685 | NDCB0_ADDR_CYC(3)
686 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300687 | (NAND_CMD_ERASE2 << 8)
688 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800689 info->ndcb1 = page_addr;
690 info->ndcb2 = 0;
691
692 break;
693 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800694 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300695 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800696
697 break;
698
699 case NAND_CMD_ERASE2:
700 exec_cmd = 0;
701 break;
702
703 default:
704 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700705 dev_err(&info->pdev->dev, "non-supported command %x\n",
706 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800707 break;
708 }
709
710 return exec_cmd;
711}
712
eric miaofe69af02008-02-14 15:48:23 +0800713static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100714 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800715{
Lei Wend4568822011-07-14 20:44:32 -0700716 struct pxa3xx_nand_host *host = mtd->priv;
717 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800718 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800719
Lei Wen4eb2da82011-02-28 10:32:13 +0800720 /*
721 * if this is a x16 device ,then convert the input
722 * "byte" address into a "word" address appropriate
723 * for indexing a word-oriented device
724 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300725 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800726 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800727
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700728 /*
729 * There may be different NAND chip hooked to
730 * different chip select, so check whether
731 * chip select has been changed, if yes, reset the timing
732 */
733 if (info->cs != host->cs) {
734 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300735 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
736 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700737 }
738
Lei Wend4568822011-07-14 20:44:32 -0700739 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800740 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800741 if (exec_cmd) {
742 init_completion(&info->cmd_complete);
743 pxa3xx_nand_start(info);
744
745 ret = wait_for_completion_timeout(&info->cmd_complete,
746 CHIP_DELAY_TIMEOUT);
747 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700748 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800749 /* Stop State Machine for next command cycle */
750 pxa3xx_nand_stop(info);
751 }
eric miaofe69af02008-02-14 15:48:23 +0800752 }
Lei Wend4568822011-07-14 20:44:32 -0700753 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800754}
755
Josh Wufdbad98d2012-06-25 18:07:45 +0800756static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700757 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800758{
759 chip->write_buf(mtd, buf, mtd->writesize);
760 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800761
762 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800763}
764
765static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700766 struct nand_chip *chip, uint8_t *buf, int oob_required,
767 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800768{
Lei Wend4568822011-07-14 20:44:32 -0700769 struct pxa3xx_nand_host *host = mtd->priv;
770 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300771 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800772
773 chip->read_buf(mtd, buf, mtd->writesize);
774 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
775
776 if (info->retcode == ERR_SBERR) {
777 switch (info->use_ecc) {
778 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300779 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800780 mtd->ecc_stats.corrected++;
781 break;
782 case 0:
783 default:
784 break;
785 }
786 } else if (info->retcode == ERR_DBERR) {
787 /*
788 * for blank page (all 0xff), HW will calculate its ECC as
789 * 0, which is different from the ECC information within
790 * OOB, ignore such double bit errors
791 */
792 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700793 info->retcode = ERR_NONE;
794 else
Lei Wenf8155a42011-02-28 10:32:11 +0800795 mtd->ecc_stats.failed++;
796 }
797
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300798 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800799}
800
eric miaofe69af02008-02-14 15:48:23 +0800801static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
802{
Lei Wend4568822011-07-14 20:44:32 -0700803 struct pxa3xx_nand_host *host = mtd->priv;
804 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800805 char retval = 0xFF;
806
807 if (info->buf_start < info->buf_count)
808 /* Has just send a new command? */
809 retval = info->data_buff[info->buf_start++];
810
811 return retval;
812}
813
814static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
815{
Lei Wend4568822011-07-14 20:44:32 -0700816 struct pxa3xx_nand_host *host = mtd->priv;
817 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800818 u16 retval = 0xFFFF;
819
820 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
821 retval = *((u16 *)(info->data_buff+info->buf_start));
822 info->buf_start += 2;
823 }
824 return retval;
825}
826
827static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
828{
Lei Wend4568822011-07-14 20:44:32 -0700829 struct pxa3xx_nand_host *host = mtd->priv;
830 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800831 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
832
833 memcpy(buf, info->data_buff + info->buf_start, real_len);
834 info->buf_start += real_len;
835}
836
837static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
838 const uint8_t *buf, int len)
839{
Lei Wend4568822011-07-14 20:44:32 -0700840 struct pxa3xx_nand_host *host = mtd->priv;
841 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800842 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
843
844 memcpy(info->data_buff + info->buf_start, buf, real_len);
845 info->buf_start += real_len;
846}
847
eric miaofe69af02008-02-14 15:48:23 +0800848static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
849{
850 return;
851}
852
853static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
854{
Lei Wend4568822011-07-14 20:44:32 -0700855 struct pxa3xx_nand_host *host = mtd->priv;
856 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800857
858 /* pxa3xx_nand_send_command has waited for command complete */
859 if (this->state == FL_WRITING || this->state == FL_ERASING) {
860 if (info->retcode == ERR_NONE)
861 return 0;
862 else {
863 /*
864 * any error make it return 0x01 which will tell
865 * the caller the erase and write fail
866 */
867 return 0x01;
868 }
869 }
870
871 return 0;
872}
873
eric miaofe69af02008-02-14 15:48:23 +0800874static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200875 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800876{
877 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900878 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700879 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800880 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800881
Lei Wenda675b42011-07-14 20:44:31 -0700882 if (f->page_size != 2048 && f->page_size != 512) {
883 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800884 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700885 }
eric miaofe69af02008-02-14 15:48:23 +0800886
Lei Wenda675b42011-07-14 20:44:31 -0700887 if (f->flash_width != 16 && f->flash_width != 8) {
888 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800889 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700890 }
eric miaofe69af02008-02-14 15:48:23 +0800891
892 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700893 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800894
895 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700896 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800897
898 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700899 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800900 else
Lei Wend4568822011-07-14 20:44:32 -0700901 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800902
903 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700904 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800905 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
906 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
907 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
908 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
909
Lei Wend4568822011-07-14 20:44:32 -0700910 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800911 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
912
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300913 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800914
Lei Wend4568822011-07-14 20:44:32 -0700915 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800916 return 0;
917}
918
Mike Rapoportf2710492009-02-17 13:54:47 +0200919static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
920{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700921 /*
922 * We set 0 by hard coding here, for we don't support keep_config
923 * when there is more than one chip attached to the controller
924 */
925 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200926 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200927
Lei Wend4568822011-07-14 20:44:32 -0700928 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300929 /* Controller's FIFO size */
930 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -0700931 host->read_id_bytes = 4;
932 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300933 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -0700934 host->read_id_bytes = 2;
935 }
936
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300937 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
938 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
939 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200940 return 0;
941}
942
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300943#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800944static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
945{
946 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300947 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800948
949 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300950 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800951 if (info->data_buff == NULL)
952 return -ENOMEM;
953 return 0;
954 }
955
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300956 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800957 &info->data_buff_phys, GFP_KERNEL);
958 if (info->data_buff == NULL) {
959 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
960 return -ENOMEM;
961 }
962
eric miaofe69af02008-02-14 15:48:23 +0800963 info->data_desc = (void *)info->data_buff + data_desc_offset;
964 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
965
966 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
967 pxa3xx_nand_data_dma_irq, info);
968 if (info->data_dma_ch < 0) {
969 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300970 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800971 info->data_buff, info->data_buff_phys);
972 return info->data_dma_ch;
973 }
974
Ezequiel Garcia95b26562013-10-04 15:30:37 -0300975 /*
976 * Now that DMA buffers are allocated we turn on
977 * DMA proper for I/O operations.
978 */
979 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800980 return 0;
981}
982
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300983static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
984{
985 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -0300986 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300987 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300988 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300989 info->data_buff, info->data_buff_phys);
990 } else {
991 kfree(info->data_buff);
992 }
993}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300994#else
995static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
996{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300997 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300998 if (info->data_buff == NULL)
999 return -ENOMEM;
1000 return 0;
1001}
1002
1003static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1004{
1005 kfree(info->data_buff);
1006}
1007#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001008
Lei Wen401e67e2011-02-28 10:32:14 +08001009static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001010{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001011 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001012 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001013 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001014
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001015 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001016 chip = mtd->priv;
1017
Lei Wen401e67e2011-02-28 10:32:14 +08001018 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001019 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1020 if (ret)
1021 return ret;
1022
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001023 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +08001024 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +08001025 return 0;
Lei Wend4568822011-07-14 20:44:32 -07001026
1027 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001028}
eric miaofe69af02008-02-14 15:48:23 +08001029
Lei Wen401e67e2011-02-28 10:32:14 +08001030static int pxa3xx_nand_scan(struct mtd_info *mtd)
1031{
Lei Wend4568822011-07-14 20:44:32 -07001032 struct pxa3xx_nand_host *host = mtd->priv;
1033 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001034 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001035 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001036 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001037 const struct pxa3xx_nand_flash *f = NULL;
1038 struct nand_chip *chip = mtd->priv;
1039 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001040 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001041 int i, ret, num;
1042
1043 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001044 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001045
1046 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001047 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001048 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1049 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001050
Lei Wend4568822011-07-14 20:44:32 -07001051 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001052 }
1053
1054 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1055 id = *((uint16_t *)(info->data_buff));
1056 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001057 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001058 else {
Lei Wenda675b42011-07-14 20:44:31 -07001059 dev_warn(&info->pdev->dev,
1060 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001061
1062 return -EINVAL;
1063 }
1064
1065 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1066 for (i = 0; i < num; i++) {
1067 if (i < pdata->num_flash)
1068 f = pdata->flash + i;
1069 else
1070 f = &builtin_flash_types[i - pdata->num_flash + 1];
1071
1072 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001073 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001074 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001075 }
1076
Lei Wen4332c112011-03-03 11:27:01 +08001077 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001078 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001079
1080 return -EINVAL;
1081 }
1082
Lei Wend4568822011-07-14 20:44:32 -07001083 ret = pxa3xx_nand_config_flash(info, f);
1084 if (ret) {
1085 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1086 return ret;
1087 }
1088
Lei Wen4332c112011-03-03 11:27:01 +08001089 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001090 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001091 pxa3xx_flash_ids[0].pagesize = f->page_size;
1092 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1093 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1094 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1095 if (f->flash_width == 16)
1096 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001097 pxa3xx_flash_ids[1].name = NULL;
1098 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001099KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001100 chip->ecc.mode = NAND_ECC_HW;
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -03001101 chip->ecc.size = info->fifo_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001102 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001103
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001104 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001105 chip->options |= NAND_BUSWIDTH_16;
1106
Lei Wen0fab0282011-06-07 03:01:06 -07001107 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001108 return -ENODEV;
1109 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001110 if (mtd->writesize >= 2048)
1111 host->col_addr_cycles = 2;
1112 else
1113 host->col_addr_cycles = 1;
1114
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001115 /* release the initial buffer */
1116 kfree(info->data_buff);
1117
1118 /* allocate the real data + oob buffer */
1119 info->buf_size = mtd->writesize + mtd->oobsize;
1120 ret = pxa3xx_nand_init_buff(info);
1121 if (ret)
1122 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001123 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001124
Lei Wen4332c112011-03-03 11:27:01 +08001125 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001126 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001127 else
Lei Wend4568822011-07-14 20:44:32 -07001128 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001129 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001130}
1131
Lei Wend4568822011-07-14 20:44:32 -07001132static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001133{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001134 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001135 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001136 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001137 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001138 struct mtd_info *mtd;
1139 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001140 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001141
Jingoo Han453810b2013-07-30 17:18:33 +09001142 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001143 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1144 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1145 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001146 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001147
eric miaofe69af02008-02-14 15:48:23 +08001148 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001149 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001150 for (cs = 0; cs < pdata->num_cs; cs++) {
1151 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1152 (sizeof(*mtd) + sizeof(*host)) * cs);
1153 chip = (struct nand_chip *)(&mtd[1]);
1154 host = (struct pxa3xx_nand_host *)chip;
1155 info->host[cs] = host;
1156 host->mtd = mtd;
1157 host->cs = cs;
1158 host->info_data = info;
1159 mtd->priv = host;
1160 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001161
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001162 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1163 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1164 chip->controller = &info->controller;
1165 chip->waitfunc = pxa3xx_nand_waitfunc;
1166 chip->select_chip = pxa3xx_nand_select_chip;
1167 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1168 chip->read_word = pxa3xx_nand_read_word;
1169 chip->read_byte = pxa3xx_nand_read_byte;
1170 chip->read_buf = pxa3xx_nand_read_buf;
1171 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001172 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001173 }
Lei Wen401e67e2011-02-28 10:32:14 +08001174
1175 spin_lock_init(&chip->controller->lock);
1176 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001177 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001178 if (IS_ERR(info->clk)) {
1179 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001180 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001181 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001182 ret = clk_prepare_enable(info->clk);
1183 if (ret < 0)
1184 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001185
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001186 if (use_dma) {
1187 /*
1188 * This is a dirty hack to make this driver work from
1189 * devicetree bindings. It can be removed once we have
1190 * a prober DMA controller framework for DT.
1191 */
1192 if (pdev->dev.of_node &&
1193 of_machine_is_compatible("marvell,pxa3xx")) {
1194 info->drcmr_dat = 97;
1195 info->drcmr_cmd = 99;
1196 } else {
1197 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1198 if (r == NULL) {
1199 dev_err(&pdev->dev,
1200 "no resource defined for data DMA\n");
1201 ret = -ENXIO;
1202 goto fail_disable_clk;
1203 }
1204 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001205
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001206 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1207 if (r == NULL) {
1208 dev_err(&pdev->dev,
1209 "no resource defined for cmd DMA\n");
1210 ret = -ENXIO;
1211 goto fail_disable_clk;
1212 }
1213 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001214 }
eric miaofe69af02008-02-14 15:48:23 +08001215 }
eric miaofe69af02008-02-14 15:48:23 +08001216
1217 irq = platform_get_irq(pdev, 0);
1218 if (irq < 0) {
1219 dev_err(&pdev->dev, "no IRQ resource defined\n");
1220 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001221 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001222 }
1223
1224 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001225 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1226 if (IS_ERR(info->mmio_base)) {
1227 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001228 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001229 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001230 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001231
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001232 /* Allocate a buffer to allow flash detection */
1233 info->buf_size = INIT_BUFFER_SIZE;
1234 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1235 if (info->data_buff == NULL) {
1236 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001237 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001238 }
eric miaofe69af02008-02-14 15:48:23 +08001239
Haojian Zhuang346e1252009-09-10 14:27:23 +08001240 /* initialize all interrupts to be disabled */
1241 disable_int(info, NDSR_MASK);
1242
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001243 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001244 if (ret < 0) {
1245 dev_err(&pdev->dev, "failed to request IRQ\n");
1246 goto fail_free_buf;
1247 }
1248
Lei Wene353a202011-03-03 11:08:30 +08001249 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001250
Lei Wend4568822011-07-14 20:44:32 -07001251 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001252
eric miaofe69af02008-02-14 15:48:23 +08001253fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001254 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001255 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001256fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001257 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001258 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001259}
1260
1261static int pxa3xx_nand_remove(struct platform_device *pdev)
1262{
Lei Wene353a202011-03-03 11:08:30 +08001263 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001264 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001265 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001266
Lei Wend4568822011-07-14 20:44:32 -07001267 if (!info)
1268 return 0;
1269
Jingoo Han453810b2013-07-30 17:18:33 +09001270 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001271
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001272 irq = platform_get_irq(pdev, 0);
1273 if (irq >= 0)
1274 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001275 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001276
Ezequiel Garciafb320612013-04-17 13:38:12 -03001277 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001278
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001279 for (cs = 0; cs < pdata->num_cs; cs++)
1280 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001281 return 0;
1282}
1283
Daniel Mack1e7ba632012-07-22 19:51:02 +02001284static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1285{
1286 struct pxa3xx_nand_platform_data *pdata;
1287 struct device_node *np = pdev->dev.of_node;
1288 const struct of_device_id *of_id =
1289 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1290
1291 if (!of_id)
1292 return 0;
1293
1294 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1295 if (!pdata)
1296 return -ENOMEM;
1297
1298 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1299 pdata->enable_arbiter = 1;
1300 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1301 pdata->keep_config = 1;
1302 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1303
1304 pdev->dev.platform_data = pdata;
1305
1306 return 0;
1307}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001308
Lei Wene353a202011-03-03 11:08:30 +08001309static int pxa3xx_nand_probe(struct platform_device *pdev)
1310{
1311 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001312 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001313 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001314 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001315
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001316#ifndef ARCH_HAS_DMA
1317 if (use_dma) {
1318 use_dma = 0;
1319 dev_warn(&pdev->dev,
1320 "This platform can't do DMA on this device\n");
1321 }
1322#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001323 ret = pxa3xx_nand_probe_dt(pdev);
1324 if (ret)
1325 return ret;
1326
Jingoo Han453810b2013-07-30 17:18:33 +09001327 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001328 if (!pdata) {
1329 dev_err(&pdev->dev, "no platform data defined\n");
1330 return -ENODEV;
1331 }
1332
Lei Wend4568822011-07-14 20:44:32 -07001333 ret = alloc_nand_resource(pdev);
1334 if (ret) {
1335 dev_err(&pdev->dev, "alloc nand resource failed\n");
1336 return ret;
1337 }
Lei Wene353a202011-03-03 11:08:30 +08001338
Lei Wend4568822011-07-14 20:44:32 -07001339 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001340 probe_success = 0;
1341 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001342 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001343
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001344 /*
1345 * The mtd name matches the one used in 'mtdparts' kernel
1346 * parameter. This name cannot be changed or otherwise
1347 * user's mtd partitions configuration would get broken.
1348 */
1349 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001350 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001351 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001352 if (ret) {
1353 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1354 cs);
1355 continue;
1356 }
1357
Daniel Mack1e7ba632012-07-22 19:51:02 +02001358 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001359 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001360 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001361 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001362 if (!ret)
1363 probe_success = 1;
1364 }
1365
1366 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001367 pxa3xx_nand_remove(pdev);
1368 return -ENODEV;
1369 }
1370
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001371 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001372}
1373
eric miaofe69af02008-02-14 15:48:23 +08001374#ifdef CONFIG_PM
1375static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1376{
Lei Wene353a202011-03-03 11:08:30 +08001377 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001378 struct pxa3xx_nand_platform_data *pdata;
1379 struct mtd_info *mtd;
1380 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001381
Jingoo Han453810b2013-07-30 17:18:33 +09001382 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001383 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001384 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1385 return -EAGAIN;
1386 }
1387
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001388 for (cs = 0; cs < pdata->num_cs; cs++) {
1389 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001390 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001391 }
1392
eric miaofe69af02008-02-14 15:48:23 +08001393 return 0;
1394}
1395
1396static int pxa3xx_nand_resume(struct platform_device *pdev)
1397{
Lei Wene353a202011-03-03 11:08:30 +08001398 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001399 struct pxa3xx_nand_platform_data *pdata;
1400 struct mtd_info *mtd;
1401 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001402
Jingoo Han453810b2013-07-30 17:18:33 +09001403 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001404 /* We don't want to handle interrupt without calling mtd routine */
1405 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001406
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001407 /*
1408 * Directly set the chip select to a invalid value,
1409 * then the driver would reset the timing according
1410 * to current chip select at the beginning of cmdfunc
1411 */
1412 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001413
Lei Wen051fc412011-07-14 20:44:30 -07001414 /*
1415 * As the spec says, the NDSR would be updated to 0x1800 when
1416 * doing the nand_clk disable/enable.
1417 * To prevent it damaging state machine of the driver, clear
1418 * all status before resume
1419 */
1420 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001421 for (cs = 0; cs < pdata->num_cs; cs++) {
1422 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001423 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001424 }
1425
Lei Wen18c81b12010-08-17 17:25:57 +08001426 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001427}
1428#else
1429#define pxa3xx_nand_suspend NULL
1430#define pxa3xx_nand_resume NULL
1431#endif
1432
1433static struct platform_driver pxa3xx_nand_driver = {
1434 .driver = {
1435 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301436 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001437 },
1438 .probe = pxa3xx_nand_probe,
1439 .remove = pxa3xx_nand_remove,
1440 .suspend = pxa3xx_nand_suspend,
1441 .resume = pxa3xx_nand_resume,
1442};
1443
Axel Linf99640d2011-11-27 20:45:03 +08001444module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001445
1446MODULE_LICENSE("GPL");
1447MODULE_DESCRIPTION("PXA3xx NAND controller driver");