blob: 5f0265de22bcf197a81b99d467ea9a30c405a3f3 [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 */
154 unsigned int page_size;
155 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700156 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700157
158 /* calculated from pxa3xx_nand_flash data */
159 unsigned int col_addr_cycles;
160 unsigned int row_addr_cycles;
161 size_t read_id_bytes;
162
Lei Wend4568822011-07-14 20:44:32 -0700163};
164
165struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800166 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800167 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800168
169 struct clk *clk;
170 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800171 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700172 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800173
174 unsigned int buf_start;
175 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300176 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800177
178 /* DMA information */
179 int drcmr_dat;
180 int drcmr_cmd;
181
182 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800183 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800184 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800185 int data_dma_ch;
186 struct pxa_dma_desc *data_desc;
187 dma_addr_t data_desc_addr;
188
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700189 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800190 unsigned int state;
191
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300192 /*
193 * This driver supports NFCv1 (as found in PXA SoC)
194 * and NFCv2 (as found in Armada 370/XP SoC).
195 */
196 enum pxa3xx_nand_variant variant;
197
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700198 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800199 int use_ecc; /* use HW ECC ? */
200 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300201 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800202 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800203
Lei Wen18c81b12010-08-17 17:25:57 +0800204 unsigned int page_size; /* page size of attached chip */
205 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700206 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800207 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800208
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300209 /* cached register value */
210 uint32_t reg_ndcr;
211 uint32_t ndtr0cs0;
212 uint32_t ndtr1cs0;
213
eric miaofe69af02008-02-14 15:48:23 +0800214 /* generated NDCBx register values */
215 uint32_t ndcb0;
216 uint32_t ndcb1;
217 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300218 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800219};
220
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030221static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800222module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300223MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800224
Lei Wenc1f82472010-08-17 13:50:23 +0800225static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800226 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
227 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
228 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
229 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800230};
231
Lei Wenc1f82472010-08-17 13:50:23 +0800232static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800233{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
234{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
235{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
236{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
237{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
238{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
239{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
240{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
241{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800242};
243
Lei Wen227a8862010-08-18 18:00:03 +0800244/* Define a default flash type setting serve as flash detecting only */
245#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
246
eric miaofe69af02008-02-14 15:48:23 +0800247#define NDTR0_tCH(c) (min((c), 7) << 19)
248#define NDTR0_tCS(c) (min((c), 7) << 16)
249#define NDTR0_tWH(c) (min((c), 7) << 11)
250#define NDTR0_tWP(c) (min((c), 7) << 8)
251#define NDTR0_tRH(c) (min((c), 7) << 3)
252#define NDTR0_tRP(c) (min((c), 7) << 0)
253
254#define NDTR1_tR(c) (min((c), 65535) << 16)
255#define NDTR1_tWHR(c) (min((c), 15) << 4)
256#define NDTR1_tAR(c) (min((c), 15) << 0)
257
258/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800259#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800260
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300261static struct of_device_id pxa3xx_nand_dt_ids[] = {
262 {
263 .compatible = "marvell,pxa3xx-nand",
264 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
265 },
266 {}
267};
268MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
269
270static enum pxa3xx_nand_variant
271pxa3xx_nand_get_variant(struct platform_device *pdev)
272{
273 const struct of_device_id *of_id =
274 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
275 if (!of_id)
276 return PXA3XX_NAND_VARIANT_PXA;
277 return (enum pxa3xx_nand_variant)of_id->data;
278}
279
Lei Wend4568822011-07-14 20:44:32 -0700280static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200281 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800282{
Lei Wend4568822011-07-14 20:44:32 -0700283 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800284 unsigned long nand_clk = clk_get_rate(info->clk);
285 uint32_t ndtr0, ndtr1;
286
287 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
288 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
289 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
290 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
291 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
292 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
293
294 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
295 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
296 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
297
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300298 info->ndtr0cs0 = ndtr0;
299 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800300 nand_writel(info, NDTR0CS0, ndtr0);
301 nand_writel(info, NDTR1CS0, ndtr1);
302}
303
Lei Wen18c81b12010-08-17 17:25:57 +0800304static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800305{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700306 struct pxa3xx_nand_host *host = info->host[info->cs];
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300307 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800308
Lei Wend4568822011-07-14 20:44:32 -0700309 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800310 if (!oob_enable) {
311 info->oob_size = 0;
312 return;
313 }
314
Lei Wend4568822011-07-14 20:44:32 -0700315 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800316 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800317 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800318 break;
319 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800320 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800321 break;
eric miaofe69af02008-02-14 15:48:23 +0800322 }
Lei Wen18c81b12010-08-17 17:25:57 +0800323}
324
Lei Wenf8155a42011-02-28 10:32:11 +0800325/**
326 * NOTE: it is a must to set ND_RUN firstly, then write
327 * command buffer, otherwise, it does not work.
328 * We enable all the interrupt at the same time, and
329 * let pxa3xx_nand_irq to handle all logic.
330 */
331static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
332{
333 uint32_t ndcr;
334
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300335 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300336
337 if (info->use_ecc)
338 ndcr |= NDCR_ECC_EN;
339 else
340 ndcr &= ~NDCR_ECC_EN;
341
342 if (info->use_dma)
343 ndcr |= NDCR_DMA_EN;
344 else
345 ndcr &= ~NDCR_DMA_EN;
346
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300347 if (info->use_spare)
348 ndcr |= NDCR_SPARE_EN;
349 else
350 ndcr &= ~NDCR_SPARE_EN;
351
Lei Wenf8155a42011-02-28 10:32:11 +0800352 ndcr |= NDCR_ND_RUN;
353
354 /* clear status bits and run */
355 nand_writel(info, NDCR, 0);
356 nand_writel(info, NDSR, NDSR_MASK);
357 nand_writel(info, NDCR, ndcr);
358}
359
360static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
361{
362 uint32_t ndcr;
363 int timeout = NAND_STOP_DELAY;
364
365 /* wait RUN bit in NDCR become 0 */
366 ndcr = nand_readl(info, NDCR);
367 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
368 ndcr = nand_readl(info, NDCR);
369 udelay(1);
370 }
371
372 if (timeout <= 0) {
373 ndcr &= ~NDCR_ND_RUN;
374 nand_writel(info, NDCR, ndcr);
375 }
376 /* clear status bits */
377 nand_writel(info, NDSR, NDSR_MASK);
378}
379
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300380static void __maybe_unused
381enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800382{
383 uint32_t ndcr;
384
385 ndcr = nand_readl(info, NDCR);
386 nand_writel(info, NDCR, ndcr & ~int_mask);
387}
388
389static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
390{
391 uint32_t ndcr;
392
393 ndcr = nand_readl(info, NDCR);
394 nand_writel(info, NDCR, ndcr | int_mask);
395}
396
Lei Wenf8155a42011-02-28 10:32:11 +0800397static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800398{
eric miaofe69af02008-02-14 15:48:23 +0800399 switch (info->state) {
400 case STATE_PIO_WRITING:
401 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800402 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800403 if (info->oob_size > 0)
404 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
405 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800406 break;
407 case STATE_PIO_READING:
408 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800409 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800410 if (info->oob_size > 0)
411 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
412 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800413 break;
414 default:
Lei Wenda675b42011-07-14 20:44:31 -0700415 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800416 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800417 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800418 }
eric miaofe69af02008-02-14 15:48:23 +0800419}
420
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300421#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800422static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800423{
424 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800425 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800426
427 desc->ddadr = DDADR_STOP;
428 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
429
Lei Wenf8155a42011-02-28 10:32:11 +0800430 switch (info->state) {
431 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800432 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800433 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800434 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800435 break;
436 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800437 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800438 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800439 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800440 break;
441 default:
Lei Wenda675b42011-07-14 20:44:31 -0700442 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800443 info->state);
444 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800445 }
446
447 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
448 DDADR(info->data_dma_ch) = info->data_desc_addr;
449 DCSR(info->data_dma_ch) |= DCSR_RUN;
450}
451
452static void pxa3xx_nand_data_dma_irq(int channel, void *data)
453{
454 struct pxa3xx_nand_info *info = data;
455 uint32_t dcsr;
456
457 dcsr = DCSR(channel);
458 DCSR(channel) = dcsr;
459
460 if (dcsr & DCSR_BUSERR) {
461 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800462 }
463
Lei Wenf8155a42011-02-28 10:32:11 +0800464 info->state = STATE_DMA_DONE;
465 enable_int(info, NDCR_INT_MASK);
466 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800467}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300468#else
469static void start_data_dma(struct pxa3xx_nand_info *info)
470{}
471#endif
eric miaofe69af02008-02-14 15:48:23 +0800472
473static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
474{
475 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800476 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700477 unsigned int ready, cmd_done;
478
479 if (info->cs == 0) {
480 ready = NDSR_FLASH_RDY;
481 cmd_done = NDSR_CS0_CMDD;
482 } else {
483 ready = NDSR_RDY;
484 cmd_done = NDSR_CS1_CMDD;
485 }
eric miaofe69af02008-02-14 15:48:23 +0800486
487 status = nand_readl(info, NDSR);
488
Lei Wenf8155a42011-02-28 10:32:11 +0800489 if (status & NDSR_DBERR)
490 info->retcode = ERR_DBERR;
491 if (status & NDSR_SBERR)
492 info->retcode = ERR_SBERR;
493 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
494 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800495 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800496 disable_int(info, NDCR_INT_MASK);
497 info->state = (status & NDSR_RDDREQ) ?
498 STATE_DMA_READING : STATE_DMA_WRITING;
499 start_data_dma(info);
500 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800501 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800502 info->state = (status & NDSR_RDDREQ) ?
503 STATE_PIO_READING : STATE_PIO_WRITING;
504 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800505 }
Lei Wenf8155a42011-02-28 10:32:11 +0800506 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700507 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800508 info->state = STATE_CMD_DONE;
509 is_completed = 1;
510 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700511 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800512 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800513 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800514 }
Lei Wenf8155a42011-02-28 10:32:11 +0800515
516 if (status & NDSR_WRCMDREQ) {
517 nand_writel(info, NDSR, NDSR_WRCMDREQ);
518 status &= ~NDSR_WRCMDREQ;
519 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300520
521 /*
522 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
523 * must be loaded by writing directly either 12 or 16
524 * bytes directly to NDCB0, four bytes at a time.
525 *
526 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
527 * but each NDCBx register can be read.
528 */
Lei Wenf8155a42011-02-28 10:32:11 +0800529 nand_writel(info, NDCB0, info->ndcb0);
530 nand_writel(info, NDCB0, info->ndcb1);
531 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300532
533 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
534 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
535 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800536 }
Lei Wenf8155a42011-02-28 10:32:11 +0800537
538 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800539 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800540 if (is_completed)
541 complete(&info->cmd_complete);
542NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800543 return IRQ_HANDLED;
544}
545
eric miaofe69af02008-02-14 15:48:23 +0800546static inline int is_buf_blank(uint8_t *buf, size_t len)
547{
548 for (; len > 0; len--)
549 if (*buf++ != 0xff)
550 return 0;
551 return 1;
552}
553
Lei Wen4eb2da82011-02-28 10:32:13 +0800554static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
555 uint16_t column, int page_addr)
556{
Lei Wend4568822011-07-14 20:44:32 -0700557 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700558 struct pxa3xx_nand_host *host;
559 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800560
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700561 host = info->host[info->cs];
562 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800563 addr_cycle = 0;
564 exec_cmd = 1;
565
566 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800567 info->buf_start = 0;
568 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800569 info->oob_size = 0;
570 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300571 info->use_spare = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800572 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800573 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700574 if (info->cs != 0)
575 info->ndcb0 = NDCB0_CSEL;
576 else
577 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800578
579 switch (command) {
580 case NAND_CMD_READ0:
581 case NAND_CMD_PAGEPROG:
582 info->use_ecc = 1;
583 case NAND_CMD_READOOB:
584 pxa3xx_set_datasize(info);
585 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300586 case NAND_CMD_PARAM:
587 info->use_spare = 0;
588 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800589 case NAND_CMD_SEQIN:
590 exec_cmd = 0;
591 break;
592 default:
593 info->ndcb1 = 0;
594 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300595 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800596 break;
597 }
598
Lei Wend4568822011-07-14 20:44:32 -0700599 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
600 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800601
602 switch (command) {
603 case NAND_CMD_READOOB:
604 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300605 info->buf_start = column;
606 info->ndcb0 |= NDCB0_CMD_TYPE(0)
607 | addr_cycle
608 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800609
Ezequiel Garciaec821352013-08-12 14:14:54 -0300610 if (command == NAND_CMD_READOOB)
611 info->buf_start += mtd->writesize;
612
613 /* Second command setting for large pages */
614 if (host->page_size >= PAGE_CHUNK_SIZE)
615 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800616
617 case NAND_CMD_SEQIN:
618 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700619 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800620 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
621 | (column & 0xFF);
622
623 info->ndcb2 = 0;
624 } else {
625 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
626 | (column & 0xFFFF);
627
628 if (page_addr & 0xFF0000)
629 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
630 else
631 info->ndcb2 = 0;
632 }
633
634 info->buf_count = mtd->writesize + mtd->oobsize;
635 memset(info->data_buff, 0xFF, info->buf_count);
636
637 break;
638
639 case NAND_CMD_PAGEPROG:
640 if (is_buf_blank(info->data_buff,
641 (mtd->writesize + mtd->oobsize))) {
642 exec_cmd = 0;
643 break;
644 }
645
Lei Wen4eb2da82011-02-28 10:32:13 +0800646 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
647 | NDCB0_AUTO_RS
648 | NDCB0_ST_ROW_EN
649 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300650 | (NAND_CMD_PAGEPROG << 8)
651 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800652 | addr_cycle;
653 break;
654
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300655 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300656 info->buf_count = 256;
657 info->ndcb0 |= NDCB0_CMD_TYPE(0)
658 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300659 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300660 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300661 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300662 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300663 info->data_size = 256;
664 break;
665
Lei Wen4eb2da82011-02-28 10:32:13 +0800666 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700667 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800668 info->ndcb0 |= NDCB0_CMD_TYPE(3)
669 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300670 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300671 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800672
673 info->data_size = 8;
674 break;
675 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800676 info->buf_count = 1;
677 info->ndcb0 |= NDCB0_CMD_TYPE(4)
678 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300679 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800680
681 info->data_size = 8;
682 break;
683
684 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800685 info->ndcb0 |= NDCB0_CMD_TYPE(2)
686 | NDCB0_AUTO_RS
687 | NDCB0_ADDR_CYC(3)
688 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300689 | (NAND_CMD_ERASE2 << 8)
690 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800691 info->ndcb1 = page_addr;
692 info->ndcb2 = 0;
693
694 break;
695 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800696 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300697 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800698
699 break;
700
701 case NAND_CMD_ERASE2:
702 exec_cmd = 0;
703 break;
704
705 default:
706 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700707 dev_err(&info->pdev->dev, "non-supported command %x\n",
708 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800709 break;
710 }
711
712 return exec_cmd;
713}
714
eric miaofe69af02008-02-14 15:48:23 +0800715static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100716 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800717{
Lei Wend4568822011-07-14 20:44:32 -0700718 struct pxa3xx_nand_host *host = mtd->priv;
719 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800720 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800721
Lei Wen4eb2da82011-02-28 10:32:13 +0800722 /*
723 * if this is a x16 device ,then convert the input
724 * "byte" address into a "word" address appropriate
725 * for indexing a word-oriented device
726 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300727 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800728 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800729
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700730 /*
731 * There may be different NAND chip hooked to
732 * different chip select, so check whether
733 * chip select has been changed, if yes, reset the timing
734 */
735 if (info->cs != host->cs) {
736 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300737 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
738 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700739 }
740
Lei Wend4568822011-07-14 20:44:32 -0700741 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800742 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800743 if (exec_cmd) {
744 init_completion(&info->cmd_complete);
745 pxa3xx_nand_start(info);
746
747 ret = wait_for_completion_timeout(&info->cmd_complete,
748 CHIP_DELAY_TIMEOUT);
749 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700750 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800751 /* Stop State Machine for next command cycle */
752 pxa3xx_nand_stop(info);
753 }
eric miaofe69af02008-02-14 15:48:23 +0800754 }
Lei Wend4568822011-07-14 20:44:32 -0700755 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800756}
757
Josh Wufdbad98d2012-06-25 18:07:45 +0800758static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700759 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800760{
761 chip->write_buf(mtd, buf, mtd->writesize);
762 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800763
764 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800765}
766
767static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700768 struct nand_chip *chip, uint8_t *buf, int oob_required,
769 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800770{
Lei Wend4568822011-07-14 20:44:32 -0700771 struct pxa3xx_nand_host *host = mtd->priv;
772 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300773 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800774
775 chip->read_buf(mtd, buf, mtd->writesize);
776 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
777
778 if (info->retcode == ERR_SBERR) {
779 switch (info->use_ecc) {
780 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300781 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800782 mtd->ecc_stats.corrected++;
783 break;
784 case 0:
785 default:
786 break;
787 }
788 } else if (info->retcode == ERR_DBERR) {
789 /*
790 * for blank page (all 0xff), HW will calculate its ECC as
791 * 0, which is different from the ECC information within
792 * OOB, ignore such double bit errors
793 */
794 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700795 info->retcode = ERR_NONE;
796 else
Lei Wenf8155a42011-02-28 10:32:11 +0800797 mtd->ecc_stats.failed++;
798 }
799
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300800 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800801}
802
eric miaofe69af02008-02-14 15:48:23 +0800803static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
804{
Lei Wend4568822011-07-14 20:44:32 -0700805 struct pxa3xx_nand_host *host = mtd->priv;
806 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800807 char retval = 0xFF;
808
809 if (info->buf_start < info->buf_count)
810 /* Has just send a new command? */
811 retval = info->data_buff[info->buf_start++];
812
813 return retval;
814}
815
816static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
817{
Lei Wend4568822011-07-14 20:44:32 -0700818 struct pxa3xx_nand_host *host = mtd->priv;
819 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800820 u16 retval = 0xFFFF;
821
822 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
823 retval = *((u16 *)(info->data_buff+info->buf_start));
824 info->buf_start += 2;
825 }
826 return retval;
827}
828
829static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
830{
Lei Wend4568822011-07-14 20:44:32 -0700831 struct pxa3xx_nand_host *host = mtd->priv;
832 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800833 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
834
835 memcpy(buf, info->data_buff + info->buf_start, real_len);
836 info->buf_start += real_len;
837}
838
839static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
840 const uint8_t *buf, int len)
841{
Lei Wend4568822011-07-14 20:44:32 -0700842 struct pxa3xx_nand_host *host = mtd->priv;
843 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800844 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
845
846 memcpy(info->data_buff + info->buf_start, buf, real_len);
847 info->buf_start += real_len;
848}
849
eric miaofe69af02008-02-14 15:48:23 +0800850static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
851{
852 return;
853}
854
855static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
856{
Lei Wend4568822011-07-14 20:44:32 -0700857 struct pxa3xx_nand_host *host = mtd->priv;
858 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800859
860 /* pxa3xx_nand_send_command has waited for command complete */
861 if (this->state == FL_WRITING || this->state == FL_ERASING) {
862 if (info->retcode == ERR_NONE)
863 return 0;
864 else {
865 /*
866 * any error make it return 0x01 which will tell
867 * the caller the erase and write fail
868 */
869 return 0x01;
870 }
871 }
872
873 return 0;
874}
875
eric miaofe69af02008-02-14 15:48:23 +0800876static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200877 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800878{
879 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900880 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700881 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800882 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800883
Lei Wenda675b42011-07-14 20:44:31 -0700884 if (f->page_size != 2048 && f->page_size != 512) {
885 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800886 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700887 }
eric miaofe69af02008-02-14 15:48:23 +0800888
Lei Wenda675b42011-07-14 20:44:31 -0700889 if (f->flash_width != 16 && f->flash_width != 8) {
890 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800891 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700892 }
eric miaofe69af02008-02-14 15:48:23 +0800893
894 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700895 host->page_size = f->page_size;
896 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800897
898 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700899 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800900
901 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700902 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800903 else
Lei Wend4568822011-07-14 20:44:32 -0700904 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800905
906 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700907 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800908 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
909 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
910 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
911 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
912
Lei Wend4568822011-07-14 20:44:32 -0700913 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800914 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
915
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300916 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800917
Lei Wend4568822011-07-14 20:44:32 -0700918 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800919 return 0;
920}
921
Mike Rapoportf2710492009-02-17 13:54:47 +0200922static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
923{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700924 /*
925 * We set 0 by hard coding here, for we don't support keep_config
926 * when there is more than one chip attached to the controller
927 */
928 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200929 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200930
Lei Wend4568822011-07-14 20:44:32 -0700931 if (ndcr & NDCR_PAGE_SZ) {
932 host->page_size = 2048;
933 host->read_id_bytes = 4;
934 } else {
935 host->page_size = 512;
936 host->read_id_bytes = 2;
937 }
938
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300939 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
940 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
941 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200942 return 0;
943}
944
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300945#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800946static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
947{
948 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300949 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800950
951 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300952 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800953 if (info->data_buff == NULL)
954 return -ENOMEM;
955 return 0;
956 }
957
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300958 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800959 &info->data_buff_phys, GFP_KERNEL);
960 if (info->data_buff == NULL) {
961 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
962 return -ENOMEM;
963 }
964
eric miaofe69af02008-02-14 15:48:23 +0800965 info->data_desc = (void *)info->data_buff + data_desc_offset;
966 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
967
968 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
969 pxa3xx_nand_data_dma_irq, info);
970 if (info->data_dma_ch < 0) {
971 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300972 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800973 info->data_buff, info->data_buff_phys);
974 return info->data_dma_ch;
975 }
976
Ezequiel Garcia95b26562013-10-04 15:30:37 -0300977 /*
978 * Now that DMA buffers are allocated we turn on
979 * DMA proper for I/O operations.
980 */
981 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800982 return 0;
983}
984
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300985static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
986{
987 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -0300988 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300989 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300990 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300991 info->data_buff, info->data_buff_phys);
992 } else {
993 kfree(info->data_buff);
994 }
995}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300996#else
997static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
998{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300999 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001000 if (info->data_buff == NULL)
1001 return -ENOMEM;
1002 return 0;
1003}
1004
1005static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1006{
1007 kfree(info->data_buff);
1008}
1009#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001010
Lei Wen401e67e2011-02-28 10:32:14 +08001011static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001012{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001013 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001014 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001015 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001016
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001017 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001018 chip = mtd->priv;
1019
Lei Wen401e67e2011-02-28 10:32:14 +08001020 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001021 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1022 if (ret)
1023 return ret;
1024
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001025 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +08001026 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +08001027 return 0;
Lei Wend4568822011-07-14 20:44:32 -07001028
1029 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001030}
eric miaofe69af02008-02-14 15:48:23 +08001031
Lei Wen401e67e2011-02-28 10:32:14 +08001032static int pxa3xx_nand_scan(struct mtd_info *mtd)
1033{
Lei Wend4568822011-07-14 20:44:32 -07001034 struct pxa3xx_nand_host *host = mtd->priv;
1035 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001036 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001037 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001038 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001039 const struct pxa3xx_nand_flash *f = NULL;
1040 struct nand_chip *chip = mtd->priv;
1041 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001042 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001043 int i, ret, num;
1044
1045 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001046 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001047
1048 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001049 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001050 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1051 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001052
Lei Wend4568822011-07-14 20:44:32 -07001053 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001054 }
1055
1056 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1057 id = *((uint16_t *)(info->data_buff));
1058 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001059 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001060 else {
Lei Wenda675b42011-07-14 20:44:31 -07001061 dev_warn(&info->pdev->dev,
1062 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001063
1064 return -EINVAL;
1065 }
1066
1067 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1068 for (i = 0; i < num; i++) {
1069 if (i < pdata->num_flash)
1070 f = pdata->flash + i;
1071 else
1072 f = &builtin_flash_types[i - pdata->num_flash + 1];
1073
1074 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001075 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001076 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001077 }
1078
Lei Wen4332c112011-03-03 11:27:01 +08001079 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001080 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001081
1082 return -EINVAL;
1083 }
1084
Lei Wend4568822011-07-14 20:44:32 -07001085 ret = pxa3xx_nand_config_flash(info, f);
1086 if (ret) {
1087 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1088 return ret;
1089 }
1090
Lei Wen4332c112011-03-03 11:27:01 +08001091 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001092 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001093 pxa3xx_flash_ids[0].pagesize = f->page_size;
1094 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1095 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1096 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1097 if (f->flash_width == 16)
1098 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001099 pxa3xx_flash_ids[1].name = NULL;
1100 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001101KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001102 chip->ecc.mode = NAND_ECC_HW;
1103 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001104 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001105
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001106 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001107 chip->options |= NAND_BUSWIDTH_16;
1108
Lei Wen0fab0282011-06-07 03:01:06 -07001109 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001110 return -ENODEV;
1111 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001112 if (mtd->writesize >= 2048)
1113 host->col_addr_cycles = 2;
1114 else
1115 host->col_addr_cycles = 1;
1116
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001117 /* release the initial buffer */
1118 kfree(info->data_buff);
1119
1120 /* allocate the real data + oob buffer */
1121 info->buf_size = mtd->writesize + mtd->oobsize;
1122 ret = pxa3xx_nand_init_buff(info);
1123 if (ret)
1124 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001125 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001126
Lei Wen4332c112011-03-03 11:27:01 +08001127 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001128 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001129 else
Lei Wend4568822011-07-14 20:44:32 -07001130 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001131 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001132}
1133
Lei Wend4568822011-07-14 20:44:32 -07001134static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001135{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001136 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001137 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001138 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001139 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001140 struct mtd_info *mtd;
1141 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001142 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001143
Jingoo Han453810b2013-07-30 17:18:33 +09001144 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001145 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1146 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1147 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001148 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001149
eric miaofe69af02008-02-14 15:48:23 +08001150 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001151 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001152 for (cs = 0; cs < pdata->num_cs; cs++) {
1153 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1154 (sizeof(*mtd) + sizeof(*host)) * cs);
1155 chip = (struct nand_chip *)(&mtd[1]);
1156 host = (struct pxa3xx_nand_host *)chip;
1157 info->host[cs] = host;
1158 host->mtd = mtd;
1159 host->cs = cs;
1160 host->info_data = info;
1161 mtd->priv = host;
1162 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001163
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001164 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1165 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1166 chip->controller = &info->controller;
1167 chip->waitfunc = pxa3xx_nand_waitfunc;
1168 chip->select_chip = pxa3xx_nand_select_chip;
1169 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1170 chip->read_word = pxa3xx_nand_read_word;
1171 chip->read_byte = pxa3xx_nand_read_byte;
1172 chip->read_buf = pxa3xx_nand_read_buf;
1173 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001174 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001175 }
Lei Wen401e67e2011-02-28 10:32:14 +08001176
1177 spin_lock_init(&chip->controller->lock);
1178 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001179 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001180 if (IS_ERR(info->clk)) {
1181 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001182 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001183 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001184 ret = clk_prepare_enable(info->clk);
1185 if (ret < 0)
1186 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001187
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001188 if (use_dma) {
1189 /*
1190 * This is a dirty hack to make this driver work from
1191 * devicetree bindings. It can be removed once we have
1192 * a prober DMA controller framework for DT.
1193 */
1194 if (pdev->dev.of_node &&
1195 of_machine_is_compatible("marvell,pxa3xx")) {
1196 info->drcmr_dat = 97;
1197 info->drcmr_cmd = 99;
1198 } else {
1199 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1200 if (r == NULL) {
1201 dev_err(&pdev->dev,
1202 "no resource defined for data DMA\n");
1203 ret = -ENXIO;
1204 goto fail_disable_clk;
1205 }
1206 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001207
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001208 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1209 if (r == NULL) {
1210 dev_err(&pdev->dev,
1211 "no resource defined for cmd DMA\n");
1212 ret = -ENXIO;
1213 goto fail_disable_clk;
1214 }
1215 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001216 }
eric miaofe69af02008-02-14 15:48:23 +08001217 }
eric miaofe69af02008-02-14 15:48:23 +08001218
1219 irq = platform_get_irq(pdev, 0);
1220 if (irq < 0) {
1221 dev_err(&pdev->dev, "no IRQ resource defined\n");
1222 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001223 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001224 }
1225
1226 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001227 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1228 if (IS_ERR(info->mmio_base)) {
1229 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001230 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001231 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001232 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001233
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001234 /* Allocate a buffer to allow flash detection */
1235 info->buf_size = INIT_BUFFER_SIZE;
1236 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1237 if (info->data_buff == NULL) {
1238 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001239 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001240 }
eric miaofe69af02008-02-14 15:48:23 +08001241
Haojian Zhuang346e1252009-09-10 14:27:23 +08001242 /* initialize all interrupts to be disabled */
1243 disable_int(info, NDSR_MASK);
1244
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001245 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001246 if (ret < 0) {
1247 dev_err(&pdev->dev, "failed to request IRQ\n");
1248 goto fail_free_buf;
1249 }
1250
Lei Wene353a202011-03-03 11:08:30 +08001251 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001252
Lei Wend4568822011-07-14 20:44:32 -07001253 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001254
eric miaofe69af02008-02-14 15:48:23 +08001255fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001256 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001257 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001258fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001259 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001260 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001261}
1262
1263static int pxa3xx_nand_remove(struct platform_device *pdev)
1264{
Lei Wene353a202011-03-03 11:08:30 +08001265 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001266 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001267 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001268
Lei Wend4568822011-07-14 20:44:32 -07001269 if (!info)
1270 return 0;
1271
Jingoo Han453810b2013-07-30 17:18:33 +09001272 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001273
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001274 irq = platform_get_irq(pdev, 0);
1275 if (irq >= 0)
1276 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001277 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001278
Ezequiel Garciafb320612013-04-17 13:38:12 -03001279 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001280
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001281 for (cs = 0; cs < pdata->num_cs; cs++)
1282 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001283 return 0;
1284}
1285
Daniel Mack1e7ba632012-07-22 19:51:02 +02001286static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1287{
1288 struct pxa3xx_nand_platform_data *pdata;
1289 struct device_node *np = pdev->dev.of_node;
1290 const struct of_device_id *of_id =
1291 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1292
1293 if (!of_id)
1294 return 0;
1295
1296 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1297 if (!pdata)
1298 return -ENOMEM;
1299
1300 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1301 pdata->enable_arbiter = 1;
1302 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1303 pdata->keep_config = 1;
1304 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1305
1306 pdev->dev.platform_data = pdata;
1307
1308 return 0;
1309}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001310
Lei Wene353a202011-03-03 11:08:30 +08001311static int pxa3xx_nand_probe(struct platform_device *pdev)
1312{
1313 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001314 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001315 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001316 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001317
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001318#ifndef ARCH_HAS_DMA
1319 if (use_dma) {
1320 use_dma = 0;
1321 dev_warn(&pdev->dev,
1322 "This platform can't do DMA on this device\n");
1323 }
1324#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001325 ret = pxa3xx_nand_probe_dt(pdev);
1326 if (ret)
1327 return ret;
1328
Jingoo Han453810b2013-07-30 17:18:33 +09001329 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001330 if (!pdata) {
1331 dev_err(&pdev->dev, "no platform data defined\n");
1332 return -ENODEV;
1333 }
1334
Lei Wend4568822011-07-14 20:44:32 -07001335 ret = alloc_nand_resource(pdev);
1336 if (ret) {
1337 dev_err(&pdev->dev, "alloc nand resource failed\n");
1338 return ret;
1339 }
Lei Wene353a202011-03-03 11:08:30 +08001340
Lei Wend4568822011-07-14 20:44:32 -07001341 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001342 probe_success = 0;
1343 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001344 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001345
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001346 /*
1347 * The mtd name matches the one used in 'mtdparts' kernel
1348 * parameter. This name cannot be changed or otherwise
1349 * user's mtd partitions configuration would get broken.
1350 */
1351 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001352 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001353 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001354 if (ret) {
1355 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1356 cs);
1357 continue;
1358 }
1359
Daniel Mack1e7ba632012-07-22 19:51:02 +02001360 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001361 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001362 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001363 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001364 if (!ret)
1365 probe_success = 1;
1366 }
1367
1368 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001369 pxa3xx_nand_remove(pdev);
1370 return -ENODEV;
1371 }
1372
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001373 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001374}
1375
eric miaofe69af02008-02-14 15:48:23 +08001376#ifdef CONFIG_PM
1377static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1378{
Lei Wene353a202011-03-03 11:08:30 +08001379 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001380 struct pxa3xx_nand_platform_data *pdata;
1381 struct mtd_info *mtd;
1382 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001383
Jingoo Han453810b2013-07-30 17:18:33 +09001384 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001385 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001386 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1387 return -EAGAIN;
1388 }
1389
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001390 for (cs = 0; cs < pdata->num_cs; cs++) {
1391 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001392 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001393 }
1394
eric miaofe69af02008-02-14 15:48:23 +08001395 return 0;
1396}
1397
1398static int pxa3xx_nand_resume(struct platform_device *pdev)
1399{
Lei Wene353a202011-03-03 11:08:30 +08001400 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001401 struct pxa3xx_nand_platform_data *pdata;
1402 struct mtd_info *mtd;
1403 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001404
Jingoo Han453810b2013-07-30 17:18:33 +09001405 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001406 /* We don't want to handle interrupt without calling mtd routine */
1407 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001408
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001409 /*
1410 * Directly set the chip select to a invalid value,
1411 * then the driver would reset the timing according
1412 * to current chip select at the beginning of cmdfunc
1413 */
1414 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001415
Lei Wen051fc412011-07-14 20:44:30 -07001416 /*
1417 * As the spec says, the NDSR would be updated to 0x1800 when
1418 * doing the nand_clk disable/enable.
1419 * To prevent it damaging state machine of the driver, clear
1420 * all status before resume
1421 */
1422 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001423 for (cs = 0; cs < pdata->num_cs; cs++) {
1424 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001425 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001426 }
1427
Lei Wen18c81b12010-08-17 17:25:57 +08001428 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001429}
1430#else
1431#define pxa3xx_nand_suspend NULL
1432#define pxa3xx_nand_resume NULL
1433#endif
1434
1435static struct platform_driver pxa3xx_nand_driver = {
1436 .driver = {
1437 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301438 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001439 },
1440 .probe = pxa3xx_nand_probe,
1441 .remove = pxa3xx_nand_remove,
1442 .suspend = pxa3xx_nand_suspend,
1443 .resume = pxa3xx_nand_resume,
1444};
1445
Axel Linf99640d2011-11-27 20:45:03 +08001446module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001447
1448MODULE_LICENSE("GPL");
1449MODULE_DESCRIPTION("PXA3xx NAND controller driver");