blob: d6e96210c25122b48ea71a8aed6a01ddf6d255cc [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
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300204 unsigned int fifo_size; /* max. data size in the FIFO */
205 unsigned int data_size; /* data to be read from 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{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300306 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800307
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300308 info->data_size = info->fifo_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800309 if (!oob_enable) {
310 info->oob_size = 0;
311 return;
312 }
313
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300314 switch (info->fifo_size) {
eric miaofe69af02008-02-14 15:48:23 +0800315 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800316 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800317 break;
318 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800319 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800320 break;
eric miaofe69af02008-02-14 15:48:23 +0800321 }
Lei Wen18c81b12010-08-17 17:25:57 +0800322}
323
Lei Wenf8155a42011-02-28 10:32:11 +0800324/**
325 * NOTE: it is a must to set ND_RUN firstly, then write
326 * command buffer, otherwise, it does not work.
327 * We enable all the interrupt at the same time, and
328 * let pxa3xx_nand_irq to handle all logic.
329 */
330static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
331{
332 uint32_t ndcr;
333
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300334 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300335
336 if (info->use_ecc)
337 ndcr |= NDCR_ECC_EN;
338 else
339 ndcr &= ~NDCR_ECC_EN;
340
341 if (info->use_dma)
342 ndcr |= NDCR_DMA_EN;
343 else
344 ndcr &= ~NDCR_DMA_EN;
345
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300346 if (info->use_spare)
347 ndcr |= NDCR_SPARE_EN;
348 else
349 ndcr &= ~NDCR_SPARE_EN;
350
Lei Wenf8155a42011-02-28 10:32:11 +0800351 ndcr |= NDCR_ND_RUN;
352
353 /* clear status bits and run */
354 nand_writel(info, NDCR, 0);
355 nand_writel(info, NDSR, NDSR_MASK);
356 nand_writel(info, NDCR, ndcr);
357}
358
359static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
360{
361 uint32_t ndcr;
362 int timeout = NAND_STOP_DELAY;
363
364 /* wait RUN bit in NDCR become 0 */
365 ndcr = nand_readl(info, NDCR);
366 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
367 ndcr = nand_readl(info, NDCR);
368 udelay(1);
369 }
370
371 if (timeout <= 0) {
372 ndcr &= ~NDCR_ND_RUN;
373 nand_writel(info, NDCR, ndcr);
374 }
375 /* clear status bits */
376 nand_writel(info, NDSR, NDSR_MASK);
377}
378
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300379static void __maybe_unused
380enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800381{
382 uint32_t ndcr;
383
384 ndcr = nand_readl(info, NDCR);
385 nand_writel(info, NDCR, ndcr & ~int_mask);
386}
387
388static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
389{
390 uint32_t ndcr;
391
392 ndcr = nand_readl(info, NDCR);
393 nand_writel(info, NDCR, ndcr | int_mask);
394}
395
Lei Wenf8155a42011-02-28 10:32:11 +0800396static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800397{
eric miaofe69af02008-02-14 15:48:23 +0800398 switch (info->state) {
399 case STATE_PIO_WRITING:
400 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800401 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800402 if (info->oob_size > 0)
403 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
404 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800405 break;
406 case STATE_PIO_READING:
407 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800408 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800409 if (info->oob_size > 0)
410 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
411 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800412 break;
413 default:
Lei Wenda675b42011-07-14 20:44:31 -0700414 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800415 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800416 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800417 }
eric miaofe69af02008-02-14 15:48:23 +0800418}
419
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300420#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800421static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800422{
423 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800424 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800425
426 desc->ddadr = DDADR_STOP;
427 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
428
Lei Wenf8155a42011-02-28 10:32:11 +0800429 switch (info->state) {
430 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800431 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800432 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800433 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800434 break;
435 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800436 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800437 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800438 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800439 break;
440 default:
Lei Wenda675b42011-07-14 20:44:31 -0700441 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800442 info->state);
443 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800444 }
445
446 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
447 DDADR(info->data_dma_ch) = info->data_desc_addr;
448 DCSR(info->data_dma_ch) |= DCSR_RUN;
449}
450
451static void pxa3xx_nand_data_dma_irq(int channel, void *data)
452{
453 struct pxa3xx_nand_info *info = data;
454 uint32_t dcsr;
455
456 dcsr = DCSR(channel);
457 DCSR(channel) = dcsr;
458
459 if (dcsr & DCSR_BUSERR) {
460 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800461 }
462
Lei Wenf8155a42011-02-28 10:32:11 +0800463 info->state = STATE_DMA_DONE;
464 enable_int(info, NDCR_INT_MASK);
465 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800466}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300467#else
468static void start_data_dma(struct pxa3xx_nand_info *info)
469{}
470#endif
eric miaofe69af02008-02-14 15:48:23 +0800471
472static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
473{
474 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800475 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700476 unsigned int ready, cmd_done;
477
478 if (info->cs == 0) {
479 ready = NDSR_FLASH_RDY;
480 cmd_done = NDSR_CS0_CMDD;
481 } else {
482 ready = NDSR_RDY;
483 cmd_done = NDSR_CS1_CMDD;
484 }
eric miaofe69af02008-02-14 15:48:23 +0800485
486 status = nand_readl(info, NDSR);
487
Lei Wenf8155a42011-02-28 10:32:11 +0800488 if (status & NDSR_DBERR)
489 info->retcode = ERR_DBERR;
490 if (status & NDSR_SBERR)
491 info->retcode = ERR_SBERR;
492 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
493 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800494 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800495 disable_int(info, NDCR_INT_MASK);
496 info->state = (status & NDSR_RDDREQ) ?
497 STATE_DMA_READING : STATE_DMA_WRITING;
498 start_data_dma(info);
499 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800500 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800501 info->state = (status & NDSR_RDDREQ) ?
502 STATE_PIO_READING : STATE_PIO_WRITING;
503 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800504 }
Lei Wenf8155a42011-02-28 10:32:11 +0800505 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700506 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800507 info->state = STATE_CMD_DONE;
508 is_completed = 1;
509 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700510 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800511 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800512 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800513 }
Lei Wenf8155a42011-02-28 10:32:11 +0800514
515 if (status & NDSR_WRCMDREQ) {
516 nand_writel(info, NDSR, NDSR_WRCMDREQ);
517 status &= ~NDSR_WRCMDREQ;
518 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300519
520 /*
521 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
522 * must be loaded by writing directly either 12 or 16
523 * bytes directly to NDCB0, four bytes at a time.
524 *
525 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
526 * but each NDCBx register can be read.
527 */
Lei Wenf8155a42011-02-28 10:32:11 +0800528 nand_writel(info, NDCB0, info->ndcb0);
529 nand_writel(info, NDCB0, info->ndcb1);
530 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300531
532 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
533 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
534 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800535 }
Lei Wenf8155a42011-02-28 10:32:11 +0800536
537 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800538 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800539 if (is_completed)
540 complete(&info->cmd_complete);
541NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800542 return IRQ_HANDLED;
543}
544
eric miaofe69af02008-02-14 15:48:23 +0800545static inline int is_buf_blank(uint8_t *buf, size_t len)
546{
547 for (; len > 0; len--)
548 if (*buf++ != 0xff)
549 return 0;
550 return 1;
551}
552
Lei Wen4eb2da82011-02-28 10:32:13 +0800553static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
554 uint16_t column, int page_addr)
555{
Lei Wend4568822011-07-14 20:44:32 -0700556 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700557 struct pxa3xx_nand_host *host;
558 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800559
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700560 host = info->host[info->cs];
561 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800562 addr_cycle = 0;
563 exec_cmd = 1;
564
565 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800566 info->buf_start = 0;
567 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800568 info->oob_size = 0;
569 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300570 info->use_spare = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800571 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800572 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700573 if (info->cs != 0)
574 info->ndcb0 = NDCB0_CSEL;
575 else
576 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800577
578 switch (command) {
579 case NAND_CMD_READ0:
580 case NAND_CMD_PAGEPROG:
581 info->use_ecc = 1;
582 case NAND_CMD_READOOB:
583 pxa3xx_set_datasize(info);
584 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300585 case NAND_CMD_PARAM:
586 info->use_spare = 0;
587 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800588 case NAND_CMD_SEQIN:
589 exec_cmd = 0;
590 break;
591 default:
592 info->ndcb1 = 0;
593 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300594 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800595 break;
596 }
597
Lei Wend4568822011-07-14 20:44:32 -0700598 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
599 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800600
601 switch (command) {
602 case NAND_CMD_READOOB:
603 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300604 info->buf_start = column;
605 info->ndcb0 |= NDCB0_CMD_TYPE(0)
606 | addr_cycle
607 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800608
Ezequiel Garciaec821352013-08-12 14:14:54 -0300609 if (command == NAND_CMD_READOOB)
610 info->buf_start += mtd->writesize;
611
612 /* Second command setting for large pages */
613 if (host->page_size >= PAGE_CHUNK_SIZE)
614 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800615
616 case NAND_CMD_SEQIN:
617 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700618 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800619 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
620 | (column & 0xFF);
621
622 info->ndcb2 = 0;
623 } else {
624 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
625 | (column & 0xFFFF);
626
627 if (page_addr & 0xFF0000)
628 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
629 else
630 info->ndcb2 = 0;
631 }
632
633 info->buf_count = mtd->writesize + mtd->oobsize;
634 memset(info->data_buff, 0xFF, info->buf_count);
635
636 break;
637
638 case NAND_CMD_PAGEPROG:
639 if (is_buf_blank(info->data_buff,
640 (mtd->writesize + mtd->oobsize))) {
641 exec_cmd = 0;
642 break;
643 }
644
Lei Wen4eb2da82011-02-28 10:32:13 +0800645 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
646 | NDCB0_AUTO_RS
647 | NDCB0_ST_ROW_EN
648 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300649 | (NAND_CMD_PAGEPROG << 8)
650 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800651 | addr_cycle;
652 break;
653
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300654 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300655 info->buf_count = 256;
656 info->ndcb0 |= NDCB0_CMD_TYPE(0)
657 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300658 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300659 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300660 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300661 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300662 info->data_size = 256;
663 break;
664
Lei Wen4eb2da82011-02-28 10:32:13 +0800665 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700666 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800667 info->ndcb0 |= NDCB0_CMD_TYPE(3)
668 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300669 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300670 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800671
672 info->data_size = 8;
673 break;
674 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800675 info->buf_count = 1;
676 info->ndcb0 |= NDCB0_CMD_TYPE(4)
677 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300678 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800679
680 info->data_size = 8;
681 break;
682
683 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800684 info->ndcb0 |= NDCB0_CMD_TYPE(2)
685 | NDCB0_AUTO_RS
686 | NDCB0_ADDR_CYC(3)
687 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300688 | (NAND_CMD_ERASE2 << 8)
689 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800690 info->ndcb1 = page_addr;
691 info->ndcb2 = 0;
692
693 break;
694 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800695 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300696 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800697
698 break;
699
700 case NAND_CMD_ERASE2:
701 exec_cmd = 0;
702 break;
703
704 default:
705 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700706 dev_err(&info->pdev->dev, "non-supported command %x\n",
707 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800708 break;
709 }
710
711 return exec_cmd;
712}
713
eric miaofe69af02008-02-14 15:48:23 +0800714static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100715 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800716{
Lei Wend4568822011-07-14 20:44:32 -0700717 struct pxa3xx_nand_host *host = mtd->priv;
718 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800719 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800720
Lei Wen4eb2da82011-02-28 10:32:13 +0800721 /*
722 * if this is a x16 device ,then convert the input
723 * "byte" address into a "word" address appropriate
724 * for indexing a word-oriented device
725 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300726 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800727 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800728
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700729 /*
730 * There may be different NAND chip hooked to
731 * different chip select, so check whether
732 * chip select has been changed, if yes, reset the timing
733 */
734 if (info->cs != host->cs) {
735 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300736 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
737 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700738 }
739
Lei Wend4568822011-07-14 20:44:32 -0700740 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800741 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800742 if (exec_cmd) {
743 init_completion(&info->cmd_complete);
744 pxa3xx_nand_start(info);
745
746 ret = wait_for_completion_timeout(&info->cmd_complete,
747 CHIP_DELAY_TIMEOUT);
748 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700749 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800750 /* Stop State Machine for next command cycle */
751 pxa3xx_nand_stop(info);
752 }
eric miaofe69af02008-02-14 15:48:23 +0800753 }
Lei Wend4568822011-07-14 20:44:32 -0700754 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800755}
756
Josh Wufdbad98d2012-06-25 18:07:45 +0800757static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700758 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800759{
760 chip->write_buf(mtd, buf, mtd->writesize);
761 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800762
763 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800764}
765
766static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700767 struct nand_chip *chip, uint8_t *buf, int oob_required,
768 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800769{
Lei Wend4568822011-07-14 20:44:32 -0700770 struct pxa3xx_nand_host *host = mtd->priv;
771 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300772 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800773
774 chip->read_buf(mtd, buf, mtd->writesize);
775 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
776
777 if (info->retcode == ERR_SBERR) {
778 switch (info->use_ecc) {
779 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300780 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800781 mtd->ecc_stats.corrected++;
782 break;
783 case 0:
784 default:
785 break;
786 }
787 } else if (info->retcode == ERR_DBERR) {
788 /*
789 * for blank page (all 0xff), HW will calculate its ECC as
790 * 0, which is different from the ECC information within
791 * OOB, ignore such double bit errors
792 */
793 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700794 info->retcode = ERR_NONE;
795 else
Lei Wenf8155a42011-02-28 10:32:11 +0800796 mtd->ecc_stats.failed++;
797 }
798
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300799 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800800}
801
eric miaofe69af02008-02-14 15:48:23 +0800802static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
803{
Lei Wend4568822011-07-14 20:44:32 -0700804 struct pxa3xx_nand_host *host = mtd->priv;
805 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800806 char retval = 0xFF;
807
808 if (info->buf_start < info->buf_count)
809 /* Has just send a new command? */
810 retval = info->data_buff[info->buf_start++];
811
812 return retval;
813}
814
815static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
816{
Lei Wend4568822011-07-14 20:44:32 -0700817 struct pxa3xx_nand_host *host = mtd->priv;
818 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800819 u16 retval = 0xFFFF;
820
821 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
822 retval = *((u16 *)(info->data_buff+info->buf_start));
823 info->buf_start += 2;
824 }
825 return retval;
826}
827
828static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
829{
Lei Wend4568822011-07-14 20:44:32 -0700830 struct pxa3xx_nand_host *host = mtd->priv;
831 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800832 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
833
834 memcpy(buf, info->data_buff + info->buf_start, real_len);
835 info->buf_start += real_len;
836}
837
838static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
839 const uint8_t *buf, int len)
840{
Lei Wend4568822011-07-14 20:44:32 -0700841 struct pxa3xx_nand_host *host = mtd->priv;
842 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800843 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
844
845 memcpy(info->data_buff + info->buf_start, buf, real_len);
846 info->buf_start += real_len;
847}
848
eric miaofe69af02008-02-14 15:48:23 +0800849static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
850{
851 return;
852}
853
854static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
855{
Lei Wend4568822011-07-14 20:44:32 -0700856 struct pxa3xx_nand_host *host = mtd->priv;
857 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800858
859 /* pxa3xx_nand_send_command has waited for command complete */
860 if (this->state == FL_WRITING || this->state == FL_ERASING) {
861 if (info->retcode == ERR_NONE)
862 return 0;
863 else {
864 /*
865 * any error make it return 0x01 which will tell
866 * the caller the erase and write fail
867 */
868 return 0x01;
869 }
870 }
871
872 return 0;
873}
874
eric miaofe69af02008-02-14 15:48:23 +0800875static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200876 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800877{
878 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900879 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700880 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800881 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800882
Lei Wenda675b42011-07-14 20:44:31 -0700883 if (f->page_size != 2048 && f->page_size != 512) {
884 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800885 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700886 }
eric miaofe69af02008-02-14 15:48:23 +0800887
Lei Wenda675b42011-07-14 20:44:31 -0700888 if (f->flash_width != 16 && f->flash_width != 8) {
889 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800890 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700891 }
eric miaofe69af02008-02-14 15:48:23 +0800892
893 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700894 host->page_size = f->page_size;
895 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800896
897 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700898 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800899
900 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700901 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800902 else
Lei Wend4568822011-07-14 20:44:32 -0700903 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800904
905 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700906 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800907 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
908 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
909 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
910 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
911
Lei Wend4568822011-07-14 20:44:32 -0700912 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800913 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
914
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300915 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800916
Lei Wend4568822011-07-14 20:44:32 -0700917 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800918 return 0;
919}
920
Mike Rapoportf2710492009-02-17 13:54:47 +0200921static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
922{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700923 /*
924 * We set 0 by hard coding here, for we don't support keep_config
925 * when there is more than one chip attached to the controller
926 */
927 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200928 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200929
Lei Wend4568822011-07-14 20:44:32 -0700930 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300931 /* Controller's FIFO size */
932 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -0700933 host->page_size = 2048;
934 host->read_id_bytes = 4;
935 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300936 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -0700937 host->page_size = 512;
938 host->read_id_bytes = 2;
939 }
940
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300941 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
942 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
943 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200944 return 0;
945}
946
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300947#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800948static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
949{
950 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300951 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800952
953 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300954 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800955 if (info->data_buff == NULL)
956 return -ENOMEM;
957 return 0;
958 }
959
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300960 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800961 &info->data_buff_phys, GFP_KERNEL);
962 if (info->data_buff == NULL) {
963 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
964 return -ENOMEM;
965 }
966
eric miaofe69af02008-02-14 15:48:23 +0800967 info->data_desc = (void *)info->data_buff + data_desc_offset;
968 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
969
970 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
971 pxa3xx_nand_data_dma_irq, info);
972 if (info->data_dma_ch < 0) {
973 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300974 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800975 info->data_buff, info->data_buff_phys);
976 return info->data_dma_ch;
977 }
978
Ezequiel Garcia95b26562013-10-04 15:30:37 -0300979 /*
980 * Now that DMA buffers are allocated we turn on
981 * DMA proper for I/O operations.
982 */
983 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800984 return 0;
985}
986
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300987static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
988{
989 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -0300990 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300991 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300992 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300993 info->data_buff, info->data_buff_phys);
994 } else {
995 kfree(info->data_buff);
996 }
997}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300998#else
999static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1000{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001001 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001002 if (info->data_buff == NULL)
1003 return -ENOMEM;
1004 return 0;
1005}
1006
1007static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1008{
1009 kfree(info->data_buff);
1010}
1011#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001012
Lei Wen401e67e2011-02-28 10:32:14 +08001013static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001014{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001015 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001016 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001017 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001018
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001019 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001020 chip = mtd->priv;
1021
Lei Wen401e67e2011-02-28 10:32:14 +08001022 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001023 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1024 if (ret)
1025 return ret;
1026
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001027 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +08001028 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +08001029 return 0;
Lei Wend4568822011-07-14 20:44:32 -07001030
1031 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001032}
eric miaofe69af02008-02-14 15:48:23 +08001033
Lei Wen401e67e2011-02-28 10:32:14 +08001034static int pxa3xx_nand_scan(struct mtd_info *mtd)
1035{
Lei Wend4568822011-07-14 20:44:32 -07001036 struct pxa3xx_nand_host *host = mtd->priv;
1037 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001038 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001039 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001040 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001041 const struct pxa3xx_nand_flash *f = NULL;
1042 struct nand_chip *chip = mtd->priv;
1043 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001044 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001045 int i, ret, num;
1046
1047 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001048 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001049
1050 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001051 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001052 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1053 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001054
Lei Wend4568822011-07-14 20:44:32 -07001055 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001056 }
1057
1058 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1059 id = *((uint16_t *)(info->data_buff));
1060 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001061 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001062 else {
Lei Wenda675b42011-07-14 20:44:31 -07001063 dev_warn(&info->pdev->dev,
1064 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001065
1066 return -EINVAL;
1067 }
1068
1069 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1070 for (i = 0; i < num; i++) {
1071 if (i < pdata->num_flash)
1072 f = pdata->flash + i;
1073 else
1074 f = &builtin_flash_types[i - pdata->num_flash + 1];
1075
1076 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001077 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001078 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001079 }
1080
Lei Wen4332c112011-03-03 11:27:01 +08001081 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001082 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001083
1084 return -EINVAL;
1085 }
1086
Lei Wend4568822011-07-14 20:44:32 -07001087 ret = pxa3xx_nand_config_flash(info, f);
1088 if (ret) {
1089 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1090 return ret;
1091 }
1092
Lei Wen4332c112011-03-03 11:27:01 +08001093 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001094 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001095 pxa3xx_flash_ids[0].pagesize = f->page_size;
1096 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1097 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1098 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1099 if (f->flash_width == 16)
1100 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001101 pxa3xx_flash_ids[1].name = NULL;
1102 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001103KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001104 chip->ecc.mode = NAND_ECC_HW;
1105 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001106 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001107
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001108 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001109 chip->options |= NAND_BUSWIDTH_16;
1110
Lei Wen0fab0282011-06-07 03:01:06 -07001111 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001112 return -ENODEV;
1113 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001114 if (mtd->writesize >= 2048)
1115 host->col_addr_cycles = 2;
1116 else
1117 host->col_addr_cycles = 1;
1118
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001119 /* release the initial buffer */
1120 kfree(info->data_buff);
1121
1122 /* allocate the real data + oob buffer */
1123 info->buf_size = mtd->writesize + mtd->oobsize;
1124 ret = pxa3xx_nand_init_buff(info);
1125 if (ret)
1126 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001127 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001128
Lei Wen4332c112011-03-03 11:27:01 +08001129 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001130 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001131 else
Lei Wend4568822011-07-14 20:44:32 -07001132 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001133 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001134}
1135
Lei Wend4568822011-07-14 20:44:32 -07001136static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001137{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001138 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001139 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001140 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001141 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001142 struct mtd_info *mtd;
1143 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001144 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001145
Jingoo Han453810b2013-07-30 17:18:33 +09001146 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001147 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1148 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1149 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001150 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001151
eric miaofe69af02008-02-14 15:48:23 +08001152 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001153 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001154 for (cs = 0; cs < pdata->num_cs; cs++) {
1155 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1156 (sizeof(*mtd) + sizeof(*host)) * cs);
1157 chip = (struct nand_chip *)(&mtd[1]);
1158 host = (struct pxa3xx_nand_host *)chip;
1159 info->host[cs] = host;
1160 host->mtd = mtd;
1161 host->cs = cs;
1162 host->info_data = info;
1163 mtd->priv = host;
1164 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001165
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001166 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1167 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1168 chip->controller = &info->controller;
1169 chip->waitfunc = pxa3xx_nand_waitfunc;
1170 chip->select_chip = pxa3xx_nand_select_chip;
1171 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1172 chip->read_word = pxa3xx_nand_read_word;
1173 chip->read_byte = pxa3xx_nand_read_byte;
1174 chip->read_buf = pxa3xx_nand_read_buf;
1175 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001176 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001177 }
Lei Wen401e67e2011-02-28 10:32:14 +08001178
1179 spin_lock_init(&chip->controller->lock);
1180 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001181 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001182 if (IS_ERR(info->clk)) {
1183 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001184 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001185 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001186 ret = clk_prepare_enable(info->clk);
1187 if (ret < 0)
1188 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001189
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001190 if (use_dma) {
1191 /*
1192 * This is a dirty hack to make this driver work from
1193 * devicetree bindings. It can be removed once we have
1194 * a prober DMA controller framework for DT.
1195 */
1196 if (pdev->dev.of_node &&
1197 of_machine_is_compatible("marvell,pxa3xx")) {
1198 info->drcmr_dat = 97;
1199 info->drcmr_cmd = 99;
1200 } else {
1201 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1202 if (r == NULL) {
1203 dev_err(&pdev->dev,
1204 "no resource defined for data DMA\n");
1205 ret = -ENXIO;
1206 goto fail_disable_clk;
1207 }
1208 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001209
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001210 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1211 if (r == NULL) {
1212 dev_err(&pdev->dev,
1213 "no resource defined for cmd DMA\n");
1214 ret = -ENXIO;
1215 goto fail_disable_clk;
1216 }
1217 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001218 }
eric miaofe69af02008-02-14 15:48:23 +08001219 }
eric miaofe69af02008-02-14 15:48:23 +08001220
1221 irq = platform_get_irq(pdev, 0);
1222 if (irq < 0) {
1223 dev_err(&pdev->dev, "no IRQ resource defined\n");
1224 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001225 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001226 }
1227
1228 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001229 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1230 if (IS_ERR(info->mmio_base)) {
1231 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001232 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001233 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001234 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001235
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001236 /* Allocate a buffer to allow flash detection */
1237 info->buf_size = INIT_BUFFER_SIZE;
1238 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1239 if (info->data_buff == NULL) {
1240 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001241 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001242 }
eric miaofe69af02008-02-14 15:48:23 +08001243
Haojian Zhuang346e1252009-09-10 14:27:23 +08001244 /* initialize all interrupts to be disabled */
1245 disable_int(info, NDSR_MASK);
1246
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001247 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001248 if (ret < 0) {
1249 dev_err(&pdev->dev, "failed to request IRQ\n");
1250 goto fail_free_buf;
1251 }
1252
Lei Wene353a202011-03-03 11:08:30 +08001253 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001254
Lei Wend4568822011-07-14 20:44:32 -07001255 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001256
eric miaofe69af02008-02-14 15:48:23 +08001257fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001258 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001259 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001260fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001261 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001262 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001263}
1264
1265static int pxa3xx_nand_remove(struct platform_device *pdev)
1266{
Lei Wene353a202011-03-03 11:08:30 +08001267 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001268 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001269 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001270
Lei Wend4568822011-07-14 20:44:32 -07001271 if (!info)
1272 return 0;
1273
Jingoo Han453810b2013-07-30 17:18:33 +09001274 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001275
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001276 irq = platform_get_irq(pdev, 0);
1277 if (irq >= 0)
1278 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001279 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001280
Ezequiel Garciafb320612013-04-17 13:38:12 -03001281 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001282
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001283 for (cs = 0; cs < pdata->num_cs; cs++)
1284 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001285 return 0;
1286}
1287
Daniel Mack1e7ba632012-07-22 19:51:02 +02001288static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1289{
1290 struct pxa3xx_nand_platform_data *pdata;
1291 struct device_node *np = pdev->dev.of_node;
1292 const struct of_device_id *of_id =
1293 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1294
1295 if (!of_id)
1296 return 0;
1297
1298 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1299 if (!pdata)
1300 return -ENOMEM;
1301
1302 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1303 pdata->enable_arbiter = 1;
1304 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1305 pdata->keep_config = 1;
1306 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1307
1308 pdev->dev.platform_data = pdata;
1309
1310 return 0;
1311}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001312
Lei Wene353a202011-03-03 11:08:30 +08001313static int pxa3xx_nand_probe(struct platform_device *pdev)
1314{
1315 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001316 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001317 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001318 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001319
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001320#ifndef ARCH_HAS_DMA
1321 if (use_dma) {
1322 use_dma = 0;
1323 dev_warn(&pdev->dev,
1324 "This platform can't do DMA on this device\n");
1325 }
1326#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001327 ret = pxa3xx_nand_probe_dt(pdev);
1328 if (ret)
1329 return ret;
1330
Jingoo Han453810b2013-07-30 17:18:33 +09001331 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001332 if (!pdata) {
1333 dev_err(&pdev->dev, "no platform data defined\n");
1334 return -ENODEV;
1335 }
1336
Lei Wend4568822011-07-14 20:44:32 -07001337 ret = alloc_nand_resource(pdev);
1338 if (ret) {
1339 dev_err(&pdev->dev, "alloc nand resource failed\n");
1340 return ret;
1341 }
Lei Wene353a202011-03-03 11:08:30 +08001342
Lei Wend4568822011-07-14 20:44:32 -07001343 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001344 probe_success = 0;
1345 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001346 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001347
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001348 /*
1349 * The mtd name matches the one used in 'mtdparts' kernel
1350 * parameter. This name cannot be changed or otherwise
1351 * user's mtd partitions configuration would get broken.
1352 */
1353 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001354 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001355 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001356 if (ret) {
1357 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1358 cs);
1359 continue;
1360 }
1361
Daniel Mack1e7ba632012-07-22 19:51:02 +02001362 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001363 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001364 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001365 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001366 if (!ret)
1367 probe_success = 1;
1368 }
1369
1370 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001371 pxa3xx_nand_remove(pdev);
1372 return -ENODEV;
1373 }
1374
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001375 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001376}
1377
eric miaofe69af02008-02-14 15:48:23 +08001378#ifdef CONFIG_PM
1379static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1380{
Lei Wene353a202011-03-03 11:08:30 +08001381 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001382 struct pxa3xx_nand_platform_data *pdata;
1383 struct mtd_info *mtd;
1384 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001385
Jingoo Han453810b2013-07-30 17:18:33 +09001386 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001387 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001388 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1389 return -EAGAIN;
1390 }
1391
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001392 for (cs = 0; cs < pdata->num_cs; cs++) {
1393 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001394 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001395 }
1396
eric miaofe69af02008-02-14 15:48:23 +08001397 return 0;
1398}
1399
1400static int pxa3xx_nand_resume(struct platform_device *pdev)
1401{
Lei Wene353a202011-03-03 11:08:30 +08001402 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001403 struct pxa3xx_nand_platform_data *pdata;
1404 struct mtd_info *mtd;
1405 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001406
Jingoo Han453810b2013-07-30 17:18:33 +09001407 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001408 /* We don't want to handle interrupt without calling mtd routine */
1409 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001410
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001411 /*
1412 * Directly set the chip select to a invalid value,
1413 * then the driver would reset the timing according
1414 * to current chip select at the beginning of cmdfunc
1415 */
1416 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001417
Lei Wen051fc412011-07-14 20:44:30 -07001418 /*
1419 * As the spec says, the NDSR would be updated to 0x1800 when
1420 * doing the nand_clk disable/enable.
1421 * To prevent it damaging state machine of the driver, clear
1422 * all status before resume
1423 */
1424 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001425 for (cs = 0; cs < pdata->num_cs; cs++) {
1426 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001427 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001428 }
1429
Lei Wen18c81b12010-08-17 17:25:57 +08001430 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001431}
1432#else
1433#define pxa3xx_nand_suspend NULL
1434#define pxa3xx_nand_resume NULL
1435#endif
1436
1437static struct platform_driver pxa3xx_nand_driver = {
1438 .driver = {
1439 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301440 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001441 },
1442 .probe = pxa3xx_nand_probe,
1443 .remove = pxa3xx_nand_remove,
1444 .suspend = pxa3xx_nand_suspend,
1445 .resume = pxa3xx_nand_resume,
1446};
1447
Axel Linf99640d2011-11-27 20:45:03 +08001448module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001449
1450MODULE_LICENSE("GPL");
1451MODULE_DESCRIPTION("PXA3xx NAND controller driver");