blob: 4cabdc9fda9076ceff440af42e028fb1073f9ed7 [file] [log] [blame]
eric miaofe69af02008-02-14 15:48:23 +08001/*
2 * drivers/mtd/nand/pxa3xx_nand.c
3 *
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080012#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080013#include <linux/module.h>
14#include <linux/interrupt.h>
15#include <linux/platform_device.h>
16#include <linux/dma-mapping.h>
17#include <linux/delay.h>
18#include <linux/clk.h>
19#include <linux/mtd/mtd.h>
20#include <linux/mtd/nand.h>
21#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010022#include <linux/io.h>
23#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020025#include <linux/of.h>
26#include <linux/of_device.h>
eric miaofe69af02008-02-14 15:48:23 +080027
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030028#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
29#define ARCH_HAS_DMA
30#endif
31
32#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080033#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030034#endif
35
Arnd Bergmann293b2da2012-08-24 15:16:48 +020036#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080037
38#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080039#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080040#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080041
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030042/*
43 * Define a buffer size for the initial command that detects the flash device:
44 * STATUS, READID and PARAM. The largest of these is the PARAM command,
45 * needing 256 bytes.
46 */
47#define INIT_BUFFER_SIZE 256
48
eric miaofe69af02008-02-14 15:48:23 +080049/* registers and bit definitions */
50#define NDCR (0x00) /* Control register */
51#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
52#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
53#define NDSR (0x14) /* Status Register */
54#define NDPCR (0x18) /* Page Count Register */
55#define NDBDR0 (0x1C) /* Bad Block Register 0 */
56#define NDBDR1 (0x20) /* Bad Block Register 1 */
57#define NDDB (0x40) /* Data Buffer */
58#define NDCB0 (0x48) /* Command Buffer0 */
59#define NDCB1 (0x4C) /* Command Buffer1 */
60#define NDCB2 (0x50) /* Command Buffer2 */
61
62#define NDCR_SPARE_EN (0x1 << 31)
63#define NDCR_ECC_EN (0x1 << 30)
64#define NDCR_DMA_EN (0x1 << 29)
65#define NDCR_ND_RUN (0x1 << 28)
66#define NDCR_DWIDTH_C (0x1 << 27)
67#define NDCR_DWIDTH_M (0x1 << 26)
68#define NDCR_PAGE_SZ (0x1 << 24)
69#define NDCR_NCSX (0x1 << 23)
70#define NDCR_ND_MODE (0x3 << 21)
71#define NDCR_NAND_MODE (0x0)
72#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080073#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080074#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
75#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
76
77#define NDCR_RA_START (0x1 << 15)
78#define NDCR_PG_PER_BLK (0x1 << 14)
79#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080080#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080081
82#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080083#define NDSR_RDY (0x1 << 12)
84#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080085#define NDSR_CS0_PAGED (0x1 << 10)
86#define NDSR_CS1_PAGED (0x1 << 9)
87#define NDSR_CS0_CMDD (0x1 << 8)
88#define NDSR_CS1_CMDD (0x1 << 7)
89#define NDSR_CS0_BBD (0x1 << 6)
90#define NDSR_CS1_BBD (0x1 << 5)
91#define NDSR_DBERR (0x1 << 4)
92#define NDSR_SBERR (0x1 << 3)
93#define NDSR_WRDREQ (0x1 << 2)
94#define NDSR_RDDREQ (0x1 << 1)
95#define NDSR_WRCMDREQ (0x1)
96
Ezequiel Garcia41a63432013-08-12 14:14:51 -030097#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +080098#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080099#define NDCB0_AUTO_RS (0x1 << 25)
100#define NDCB0_CSEL (0x1 << 24)
101#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
102#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
103#define NDCB0_NC (0x1 << 20)
104#define NDCB0_DBC (0x1 << 19)
105#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
106#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
107#define NDCB0_CMD2_MASK (0xff << 8)
108#define NDCB0_CMD1_MASK (0xff)
109#define NDCB0_ADDR_CYC_SHIFT (16)
110
eric miaofe69af02008-02-14 15:48:23 +0800111/* macros for registers read/write */
112#define nand_writel(info, off, val) \
113 __raw_writel((val), (info)->mmio_base + (off))
114
115#define nand_readl(info, off) \
116 __raw_readl((info)->mmio_base + (off))
117
118/* error code and state */
119enum {
120 ERR_NONE = 0,
121 ERR_DMABUSERR = -1,
122 ERR_SENDCMD = -2,
123 ERR_DBERR = -3,
124 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300125 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800126};
127
128enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800129 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700130 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800131 STATE_CMD_HANDLE,
132 STATE_DMA_READING,
133 STATE_DMA_WRITING,
134 STATE_DMA_DONE,
135 STATE_PIO_READING,
136 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800137 STATE_CMD_DONE,
138 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800139};
140
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300141enum pxa3xx_nand_variant {
142 PXA3XX_NAND_VARIANT_PXA,
143 PXA3XX_NAND_VARIANT_ARMADA370,
144};
145
Lei Wend4568822011-07-14 20:44:32 -0700146struct pxa3xx_nand_host {
147 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700148 struct mtd_info *mtd;
149 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800150
Lei Wend4568822011-07-14 20:44:32 -0700151 /* page size of attached chip */
152 unsigned int page_size;
153 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700154 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700155
156 /* calculated from pxa3xx_nand_flash data */
157 unsigned int col_addr_cycles;
158 unsigned int row_addr_cycles;
159 size_t read_id_bytes;
160
Lei Wend4568822011-07-14 20:44:32 -0700161};
162
163struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800164 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800165 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800166
167 struct clk *clk;
168 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800169 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700170 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800171
172 unsigned int buf_start;
173 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300174 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800175
176 /* DMA information */
177 int drcmr_dat;
178 int drcmr_cmd;
179
180 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800181 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800182 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800183 int data_dma_ch;
184 struct pxa_dma_desc *data_desc;
185 dma_addr_t data_desc_addr;
186
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700187 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800188 unsigned int state;
189
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300190 /*
191 * This driver supports NFCv1 (as found in PXA SoC)
192 * and NFCv2 (as found in Armada 370/XP SoC).
193 */
194 enum pxa3xx_nand_variant variant;
195
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700196 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800197 int use_ecc; /* use HW ECC ? */
198 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300199 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800200 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800201
Lei Wen18c81b12010-08-17 17:25:57 +0800202 unsigned int page_size; /* page size of attached chip */
203 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700204 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800205 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800206
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300207 /* cached register value */
208 uint32_t reg_ndcr;
209 uint32_t ndtr0cs0;
210 uint32_t ndtr1cs0;
211
eric miaofe69af02008-02-14 15:48:23 +0800212 /* generated NDCBx register values */
213 uint32_t ndcb0;
214 uint32_t ndcb1;
215 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300216 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800217};
218
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030219static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800220module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300221MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800222
Lei Wenc1f82472010-08-17 13:50:23 +0800223static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800224 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
225 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
226 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
227 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800228};
229
Lei Wenc1f82472010-08-17 13:50:23 +0800230static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800231{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
232{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
233{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
234{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
235{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
236{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
237{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
238{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
239{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800240};
241
Lei Wen227a8862010-08-18 18:00:03 +0800242/* Define a default flash type setting serve as flash detecting only */
243#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
244
eric miaofe69af02008-02-14 15:48:23 +0800245#define NDTR0_tCH(c) (min((c), 7) << 19)
246#define NDTR0_tCS(c) (min((c), 7) << 16)
247#define NDTR0_tWH(c) (min((c), 7) << 11)
248#define NDTR0_tWP(c) (min((c), 7) << 8)
249#define NDTR0_tRH(c) (min((c), 7) << 3)
250#define NDTR0_tRP(c) (min((c), 7) << 0)
251
252#define NDTR1_tR(c) (min((c), 65535) << 16)
253#define NDTR1_tWHR(c) (min((c), 15) << 4)
254#define NDTR1_tAR(c) (min((c), 15) << 0)
255
256/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800257#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800258
Lei Wend4568822011-07-14 20:44:32 -0700259static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200260 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800261{
Lei Wend4568822011-07-14 20:44:32 -0700262 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800263 unsigned long nand_clk = clk_get_rate(info->clk);
264 uint32_t ndtr0, ndtr1;
265
266 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
267 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
268 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
269 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
270 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
271 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
272
273 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
274 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
275 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
276
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300277 info->ndtr0cs0 = ndtr0;
278 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800279 nand_writel(info, NDTR0CS0, ndtr0);
280 nand_writel(info, NDTR1CS0, ndtr1);
281}
282
Lei Wen18c81b12010-08-17 17:25:57 +0800283static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800284{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700285 struct pxa3xx_nand_host *host = info->host[info->cs];
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300286 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800287
Lei Wend4568822011-07-14 20:44:32 -0700288 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800289 if (!oob_enable) {
290 info->oob_size = 0;
291 return;
292 }
293
Lei Wend4568822011-07-14 20:44:32 -0700294 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800295 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800296 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800297 break;
298 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800299 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800300 break;
eric miaofe69af02008-02-14 15:48:23 +0800301 }
Lei Wen18c81b12010-08-17 17:25:57 +0800302}
303
Lei Wenf8155a42011-02-28 10:32:11 +0800304/**
305 * NOTE: it is a must to set ND_RUN firstly, then write
306 * command buffer, otherwise, it does not work.
307 * We enable all the interrupt at the same time, and
308 * let pxa3xx_nand_irq to handle all logic.
309 */
310static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
311{
312 uint32_t ndcr;
313
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300314 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300315
316 if (info->use_ecc)
317 ndcr |= NDCR_ECC_EN;
318 else
319 ndcr &= ~NDCR_ECC_EN;
320
321 if (info->use_dma)
322 ndcr |= NDCR_DMA_EN;
323 else
324 ndcr &= ~NDCR_DMA_EN;
325
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300326 if (info->use_spare)
327 ndcr |= NDCR_SPARE_EN;
328 else
329 ndcr &= ~NDCR_SPARE_EN;
330
Lei Wenf8155a42011-02-28 10:32:11 +0800331 ndcr |= NDCR_ND_RUN;
332
333 /* clear status bits and run */
334 nand_writel(info, NDCR, 0);
335 nand_writel(info, NDSR, NDSR_MASK);
336 nand_writel(info, NDCR, ndcr);
337}
338
339static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
340{
341 uint32_t ndcr;
342 int timeout = NAND_STOP_DELAY;
343
344 /* wait RUN bit in NDCR become 0 */
345 ndcr = nand_readl(info, NDCR);
346 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
347 ndcr = nand_readl(info, NDCR);
348 udelay(1);
349 }
350
351 if (timeout <= 0) {
352 ndcr &= ~NDCR_ND_RUN;
353 nand_writel(info, NDCR, ndcr);
354 }
355 /* clear status bits */
356 nand_writel(info, NDSR, NDSR_MASK);
357}
358
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300359static void __maybe_unused
360enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800361{
362 uint32_t ndcr;
363
364 ndcr = nand_readl(info, NDCR);
365 nand_writel(info, NDCR, ndcr & ~int_mask);
366}
367
368static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
369{
370 uint32_t ndcr;
371
372 ndcr = nand_readl(info, NDCR);
373 nand_writel(info, NDCR, ndcr | int_mask);
374}
375
Lei Wenf8155a42011-02-28 10:32:11 +0800376static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800377{
eric miaofe69af02008-02-14 15:48:23 +0800378 switch (info->state) {
379 case STATE_PIO_WRITING:
380 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800381 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800382 if (info->oob_size > 0)
383 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
384 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800385 break;
386 case STATE_PIO_READING:
387 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800388 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800389 if (info->oob_size > 0)
390 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
391 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800392 break;
393 default:
Lei Wenda675b42011-07-14 20:44:31 -0700394 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800395 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800396 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800397 }
eric miaofe69af02008-02-14 15:48:23 +0800398}
399
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300400#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800401static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800402{
403 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800404 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800405
406 desc->ddadr = DDADR_STOP;
407 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
408
Lei Wenf8155a42011-02-28 10:32:11 +0800409 switch (info->state) {
410 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800411 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800412 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800413 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800414 break;
415 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800416 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800417 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800418 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800419 break;
420 default:
Lei Wenda675b42011-07-14 20:44:31 -0700421 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800422 info->state);
423 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800424 }
425
426 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
427 DDADR(info->data_dma_ch) = info->data_desc_addr;
428 DCSR(info->data_dma_ch) |= DCSR_RUN;
429}
430
431static void pxa3xx_nand_data_dma_irq(int channel, void *data)
432{
433 struct pxa3xx_nand_info *info = data;
434 uint32_t dcsr;
435
436 dcsr = DCSR(channel);
437 DCSR(channel) = dcsr;
438
439 if (dcsr & DCSR_BUSERR) {
440 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800441 }
442
Lei Wenf8155a42011-02-28 10:32:11 +0800443 info->state = STATE_DMA_DONE;
444 enable_int(info, NDCR_INT_MASK);
445 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800446}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300447#else
448static void start_data_dma(struct pxa3xx_nand_info *info)
449{}
450#endif
eric miaofe69af02008-02-14 15:48:23 +0800451
452static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
453{
454 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800455 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700456 unsigned int ready, cmd_done;
457
458 if (info->cs == 0) {
459 ready = NDSR_FLASH_RDY;
460 cmd_done = NDSR_CS0_CMDD;
461 } else {
462 ready = NDSR_RDY;
463 cmd_done = NDSR_CS1_CMDD;
464 }
eric miaofe69af02008-02-14 15:48:23 +0800465
466 status = nand_readl(info, NDSR);
467
Lei Wenf8155a42011-02-28 10:32:11 +0800468 if (status & NDSR_DBERR)
469 info->retcode = ERR_DBERR;
470 if (status & NDSR_SBERR)
471 info->retcode = ERR_SBERR;
472 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
473 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800474 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800475 disable_int(info, NDCR_INT_MASK);
476 info->state = (status & NDSR_RDDREQ) ?
477 STATE_DMA_READING : STATE_DMA_WRITING;
478 start_data_dma(info);
479 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800480 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800481 info->state = (status & NDSR_RDDREQ) ?
482 STATE_PIO_READING : STATE_PIO_WRITING;
483 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800484 }
Lei Wenf8155a42011-02-28 10:32:11 +0800485 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700486 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800487 info->state = STATE_CMD_DONE;
488 is_completed = 1;
489 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700490 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800491 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800492 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800493 }
Lei Wenf8155a42011-02-28 10:32:11 +0800494
495 if (status & NDSR_WRCMDREQ) {
496 nand_writel(info, NDSR, NDSR_WRCMDREQ);
497 status &= ~NDSR_WRCMDREQ;
498 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300499
500 /*
501 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
502 * must be loaded by writing directly either 12 or 16
503 * bytes directly to NDCB0, four bytes at a time.
504 *
505 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
506 * but each NDCBx register can be read.
507 */
Lei Wenf8155a42011-02-28 10:32:11 +0800508 nand_writel(info, NDCB0, info->ndcb0);
509 nand_writel(info, NDCB0, info->ndcb1);
510 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300511
512 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
513 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
514 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800515 }
Lei Wenf8155a42011-02-28 10:32:11 +0800516
517 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800518 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800519 if (is_completed)
520 complete(&info->cmd_complete);
521NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800522 return IRQ_HANDLED;
523}
524
eric miaofe69af02008-02-14 15:48:23 +0800525static inline int is_buf_blank(uint8_t *buf, size_t len)
526{
527 for (; len > 0; len--)
528 if (*buf++ != 0xff)
529 return 0;
530 return 1;
531}
532
Lei Wen4eb2da82011-02-28 10:32:13 +0800533static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
534 uint16_t column, int page_addr)
535{
Lei Wend4568822011-07-14 20:44:32 -0700536 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700537 struct pxa3xx_nand_host *host;
538 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800539
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700540 host = info->host[info->cs];
541 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800542 addr_cycle = 0;
543 exec_cmd = 1;
544
545 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800546 info->buf_start = 0;
547 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800548 info->oob_size = 0;
549 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300550 info->use_spare = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800551 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800552 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700553 if (info->cs != 0)
554 info->ndcb0 = NDCB0_CSEL;
555 else
556 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800557
558 switch (command) {
559 case NAND_CMD_READ0:
560 case NAND_CMD_PAGEPROG:
561 info->use_ecc = 1;
562 case NAND_CMD_READOOB:
563 pxa3xx_set_datasize(info);
564 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300565 case NAND_CMD_PARAM:
566 info->use_spare = 0;
567 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800568 case NAND_CMD_SEQIN:
569 exec_cmd = 0;
570 break;
571 default:
572 info->ndcb1 = 0;
573 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300574 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800575 break;
576 }
577
Lei Wend4568822011-07-14 20:44:32 -0700578 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
579 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800580
581 switch (command) {
582 case NAND_CMD_READOOB:
583 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300584 info->buf_start = column;
585 info->ndcb0 |= NDCB0_CMD_TYPE(0)
586 | addr_cycle
587 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800588
Ezequiel Garciaec821352013-08-12 14:14:54 -0300589 if (command == NAND_CMD_READOOB)
590 info->buf_start += mtd->writesize;
591
592 /* Second command setting for large pages */
593 if (host->page_size >= PAGE_CHUNK_SIZE)
594 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800595
596 case NAND_CMD_SEQIN:
597 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700598 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800599 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
600 | (column & 0xFF);
601
602 info->ndcb2 = 0;
603 } else {
604 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
605 | (column & 0xFFFF);
606
607 if (page_addr & 0xFF0000)
608 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
609 else
610 info->ndcb2 = 0;
611 }
612
613 info->buf_count = mtd->writesize + mtd->oobsize;
614 memset(info->data_buff, 0xFF, info->buf_count);
615
616 break;
617
618 case NAND_CMD_PAGEPROG:
619 if (is_buf_blank(info->data_buff,
620 (mtd->writesize + mtd->oobsize))) {
621 exec_cmd = 0;
622 break;
623 }
624
Lei Wen4eb2da82011-02-28 10:32:13 +0800625 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
626 | NDCB0_AUTO_RS
627 | NDCB0_ST_ROW_EN
628 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300629 | (NAND_CMD_PAGEPROG << 8)
630 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800631 | addr_cycle;
632 break;
633
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300634 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300635 info->buf_count = 256;
636 info->ndcb0 |= NDCB0_CMD_TYPE(0)
637 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300638 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300639 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300640 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300641 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300642 info->data_size = 256;
643 break;
644
Lei Wen4eb2da82011-02-28 10:32:13 +0800645 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700646 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800647 info->ndcb0 |= NDCB0_CMD_TYPE(3)
648 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300649 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300650 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800651
652 info->data_size = 8;
653 break;
654 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800655 info->buf_count = 1;
656 info->ndcb0 |= NDCB0_CMD_TYPE(4)
657 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300658 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800659
660 info->data_size = 8;
661 break;
662
663 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800664 info->ndcb0 |= NDCB0_CMD_TYPE(2)
665 | NDCB0_AUTO_RS
666 | NDCB0_ADDR_CYC(3)
667 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300668 | (NAND_CMD_ERASE2 << 8)
669 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800670 info->ndcb1 = page_addr;
671 info->ndcb2 = 0;
672
673 break;
674 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800675 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300676 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800677
678 break;
679
680 case NAND_CMD_ERASE2:
681 exec_cmd = 0;
682 break;
683
684 default:
685 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700686 dev_err(&info->pdev->dev, "non-supported command %x\n",
687 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800688 break;
689 }
690
691 return exec_cmd;
692}
693
eric miaofe69af02008-02-14 15:48:23 +0800694static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100695 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800696{
Lei Wend4568822011-07-14 20:44:32 -0700697 struct pxa3xx_nand_host *host = mtd->priv;
698 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800699 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800700
Lei Wen4eb2da82011-02-28 10:32:13 +0800701 /*
702 * if this is a x16 device ,then convert the input
703 * "byte" address into a "word" address appropriate
704 * for indexing a word-oriented device
705 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300706 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800707 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800708
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700709 /*
710 * There may be different NAND chip hooked to
711 * different chip select, so check whether
712 * chip select has been changed, if yes, reset the timing
713 */
714 if (info->cs != host->cs) {
715 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300716 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
717 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700718 }
719
Lei Wend4568822011-07-14 20:44:32 -0700720 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800721 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800722 if (exec_cmd) {
723 init_completion(&info->cmd_complete);
724 pxa3xx_nand_start(info);
725
726 ret = wait_for_completion_timeout(&info->cmd_complete,
727 CHIP_DELAY_TIMEOUT);
728 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700729 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800730 /* Stop State Machine for next command cycle */
731 pxa3xx_nand_stop(info);
732 }
eric miaofe69af02008-02-14 15:48:23 +0800733 }
Lei Wend4568822011-07-14 20:44:32 -0700734 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800735}
736
Josh Wufdbad98d2012-06-25 18:07:45 +0800737static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700738 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800739{
740 chip->write_buf(mtd, buf, mtd->writesize);
741 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800742
743 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800744}
745
746static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700747 struct nand_chip *chip, uint8_t *buf, int oob_required,
748 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800749{
Lei Wend4568822011-07-14 20:44:32 -0700750 struct pxa3xx_nand_host *host = mtd->priv;
751 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800752
753 chip->read_buf(mtd, buf, mtd->writesize);
754 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
755
756 if (info->retcode == ERR_SBERR) {
757 switch (info->use_ecc) {
758 case 1:
759 mtd->ecc_stats.corrected++;
760 break;
761 case 0:
762 default:
763 break;
764 }
765 } else if (info->retcode == ERR_DBERR) {
766 /*
767 * for blank page (all 0xff), HW will calculate its ECC as
768 * 0, which is different from the ECC information within
769 * OOB, ignore such double bit errors
770 */
771 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700772 info->retcode = ERR_NONE;
773 else
Lei Wenf8155a42011-02-28 10:32:11 +0800774 mtd->ecc_stats.failed++;
775 }
776
777 return 0;
778}
779
eric miaofe69af02008-02-14 15:48:23 +0800780static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
781{
Lei Wend4568822011-07-14 20:44:32 -0700782 struct pxa3xx_nand_host *host = mtd->priv;
783 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800784 char retval = 0xFF;
785
786 if (info->buf_start < info->buf_count)
787 /* Has just send a new command? */
788 retval = info->data_buff[info->buf_start++];
789
790 return retval;
791}
792
793static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
794{
Lei Wend4568822011-07-14 20:44:32 -0700795 struct pxa3xx_nand_host *host = mtd->priv;
796 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800797 u16 retval = 0xFFFF;
798
799 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
800 retval = *((u16 *)(info->data_buff+info->buf_start));
801 info->buf_start += 2;
802 }
803 return retval;
804}
805
806static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
807{
Lei Wend4568822011-07-14 20:44:32 -0700808 struct pxa3xx_nand_host *host = mtd->priv;
809 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800810 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
811
812 memcpy(buf, info->data_buff + info->buf_start, real_len);
813 info->buf_start += real_len;
814}
815
816static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
817 const uint8_t *buf, int len)
818{
Lei Wend4568822011-07-14 20:44:32 -0700819 struct pxa3xx_nand_host *host = mtd->priv;
820 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800821 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
822
823 memcpy(info->data_buff + info->buf_start, buf, real_len);
824 info->buf_start += real_len;
825}
826
eric miaofe69af02008-02-14 15:48:23 +0800827static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
828{
829 return;
830}
831
832static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
833{
Lei Wend4568822011-07-14 20:44:32 -0700834 struct pxa3xx_nand_host *host = mtd->priv;
835 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800836
837 /* pxa3xx_nand_send_command has waited for command complete */
838 if (this->state == FL_WRITING || this->state == FL_ERASING) {
839 if (info->retcode == ERR_NONE)
840 return 0;
841 else {
842 /*
843 * any error make it return 0x01 which will tell
844 * the caller the erase and write fail
845 */
846 return 0x01;
847 }
848 }
849
850 return 0;
851}
852
eric miaofe69af02008-02-14 15:48:23 +0800853static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200854 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800855{
856 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900857 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700858 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800859 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800860
Lei Wenda675b42011-07-14 20:44:31 -0700861 if (f->page_size != 2048 && f->page_size != 512) {
862 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800863 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700864 }
eric miaofe69af02008-02-14 15:48:23 +0800865
Lei Wenda675b42011-07-14 20:44:31 -0700866 if (f->flash_width != 16 && f->flash_width != 8) {
867 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800868 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700869 }
eric miaofe69af02008-02-14 15:48:23 +0800870
871 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700872 host->page_size = f->page_size;
873 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800874
875 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700876 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800877
878 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700879 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800880 else
Lei Wend4568822011-07-14 20:44:32 -0700881 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800882
883 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700884 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800885 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
886 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
887 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
888 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
889
Lei Wend4568822011-07-14 20:44:32 -0700890 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800891 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
892
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300893 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800894
Lei Wend4568822011-07-14 20:44:32 -0700895 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800896 return 0;
897}
898
Mike Rapoportf2710492009-02-17 13:54:47 +0200899static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
900{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700901 /*
902 * We set 0 by hard coding here, for we don't support keep_config
903 * when there is more than one chip attached to the controller
904 */
905 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200906 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200907
Lei Wend4568822011-07-14 20:44:32 -0700908 if (ndcr & NDCR_PAGE_SZ) {
909 host->page_size = 2048;
910 host->read_id_bytes = 4;
911 } else {
912 host->page_size = 512;
913 host->read_id_bytes = 2;
914 }
915
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300916 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
917 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
918 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200919 return 0;
920}
921
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300922#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800923static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
924{
925 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300926 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800927
928 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300929 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800930 if (info->data_buff == NULL)
931 return -ENOMEM;
932 return 0;
933 }
934
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300935 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800936 &info->data_buff_phys, GFP_KERNEL);
937 if (info->data_buff == NULL) {
938 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
939 return -ENOMEM;
940 }
941
eric miaofe69af02008-02-14 15:48:23 +0800942 info->data_desc = (void *)info->data_buff + data_desc_offset;
943 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
944
945 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
946 pxa3xx_nand_data_dma_irq, info);
947 if (info->data_dma_ch < 0) {
948 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300949 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800950 info->data_buff, info->data_buff_phys);
951 return info->data_dma_ch;
952 }
953
Ezequiel Garcia95b26562013-10-04 15:30:37 -0300954 /*
955 * Now that DMA buffers are allocated we turn on
956 * DMA proper for I/O operations.
957 */
958 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800959 return 0;
960}
961
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300962static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
963{
964 struct platform_device *pdev = info->pdev;
965 if (use_dma) {
966 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300967 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300968 info->data_buff, info->data_buff_phys);
969 } else {
970 kfree(info->data_buff);
971 }
972}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300973#else
974static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
975{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300976 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300977 if (info->data_buff == NULL)
978 return -ENOMEM;
979 return 0;
980}
981
982static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
983{
984 kfree(info->data_buff);
985}
986#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300987
Lei Wen401e67e2011-02-28 10:32:14 +0800988static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800989{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700990 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700991 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700992 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800993 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700994 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
995 if (ret)
996 return ret;
997
998 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800999 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +08001000 return 0;
Lei Wend4568822011-07-14 20:44:32 -07001001
1002 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001003}
eric miaofe69af02008-02-14 15:48:23 +08001004
Lei Wen401e67e2011-02-28 10:32:14 +08001005static int pxa3xx_nand_scan(struct mtd_info *mtd)
1006{
Lei Wend4568822011-07-14 20:44:32 -07001007 struct pxa3xx_nand_host *host = mtd->priv;
1008 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001009 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001010 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001011 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001012 const struct pxa3xx_nand_flash *f = NULL;
1013 struct nand_chip *chip = mtd->priv;
1014 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001015 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001016 int i, ret, num;
1017
1018 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001019 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001020
1021 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001022 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001023 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1024 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001025
Lei Wend4568822011-07-14 20:44:32 -07001026 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001027 }
1028
1029 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1030 id = *((uint16_t *)(info->data_buff));
1031 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001032 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001033 else {
Lei Wenda675b42011-07-14 20:44:31 -07001034 dev_warn(&info->pdev->dev,
1035 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001036
1037 return -EINVAL;
1038 }
1039
1040 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1041 for (i = 0; i < num; i++) {
1042 if (i < pdata->num_flash)
1043 f = pdata->flash + i;
1044 else
1045 f = &builtin_flash_types[i - pdata->num_flash + 1];
1046
1047 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001048 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001049 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001050 }
1051
Lei Wen4332c112011-03-03 11:27:01 +08001052 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001053 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001054
1055 return -EINVAL;
1056 }
1057
Lei Wend4568822011-07-14 20:44:32 -07001058 ret = pxa3xx_nand_config_flash(info, f);
1059 if (ret) {
1060 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1061 return ret;
1062 }
1063
Lei Wen4332c112011-03-03 11:27:01 +08001064 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001065 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001066 pxa3xx_flash_ids[0].pagesize = f->page_size;
1067 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1068 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1069 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1070 if (f->flash_width == 16)
1071 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001072 pxa3xx_flash_ids[1].name = NULL;
1073 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001074KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001075 chip->ecc.mode = NAND_ECC_HW;
1076 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001077 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001078
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001079 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001080 chip->options |= NAND_BUSWIDTH_16;
1081
Lei Wen0fab0282011-06-07 03:01:06 -07001082 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001083 return -ENODEV;
1084 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001085 if (mtd->writesize >= 2048)
1086 host->col_addr_cycles = 2;
1087 else
1088 host->col_addr_cycles = 1;
1089
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001090 /* release the initial buffer */
1091 kfree(info->data_buff);
1092
1093 /* allocate the real data + oob buffer */
1094 info->buf_size = mtd->writesize + mtd->oobsize;
1095 ret = pxa3xx_nand_init_buff(info);
1096 if (ret)
1097 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001098 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001099
Lei Wen4332c112011-03-03 11:27:01 +08001100 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001101 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001102 else
Lei Wend4568822011-07-14 20:44:32 -07001103 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001104 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001105}
1106
Lei Wend4568822011-07-14 20:44:32 -07001107static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001108{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001109 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001110 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001111 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001112 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001113 struct mtd_info *mtd;
1114 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001115 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001116
Jingoo Han453810b2013-07-30 17:18:33 +09001117 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001118 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1119 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1120 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001121 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001122
eric miaofe69af02008-02-14 15:48:23 +08001123 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001124 for (cs = 0; cs < pdata->num_cs; cs++) {
1125 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1126 (sizeof(*mtd) + sizeof(*host)) * cs);
1127 chip = (struct nand_chip *)(&mtd[1]);
1128 host = (struct pxa3xx_nand_host *)chip;
1129 info->host[cs] = host;
1130 host->mtd = mtd;
1131 host->cs = cs;
1132 host->info_data = info;
1133 mtd->priv = host;
1134 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001135
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001136 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1137 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1138 chip->controller = &info->controller;
1139 chip->waitfunc = pxa3xx_nand_waitfunc;
1140 chip->select_chip = pxa3xx_nand_select_chip;
1141 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1142 chip->read_word = pxa3xx_nand_read_word;
1143 chip->read_byte = pxa3xx_nand_read_byte;
1144 chip->read_buf = pxa3xx_nand_read_buf;
1145 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001146 }
Lei Wen401e67e2011-02-28 10:32:14 +08001147
1148 spin_lock_init(&chip->controller->lock);
1149 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001150 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001151 if (IS_ERR(info->clk)) {
1152 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001153 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001154 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001155 ret = clk_prepare_enable(info->clk);
1156 if (ret < 0)
1157 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001158
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001159 if (use_dma) {
1160 /*
1161 * This is a dirty hack to make this driver work from
1162 * devicetree bindings. It can be removed once we have
1163 * a prober DMA controller framework for DT.
1164 */
1165 if (pdev->dev.of_node &&
1166 of_machine_is_compatible("marvell,pxa3xx")) {
1167 info->drcmr_dat = 97;
1168 info->drcmr_cmd = 99;
1169 } else {
1170 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1171 if (r == NULL) {
1172 dev_err(&pdev->dev,
1173 "no resource defined for data DMA\n");
1174 ret = -ENXIO;
1175 goto fail_disable_clk;
1176 }
1177 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001178
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001179 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1180 if (r == NULL) {
1181 dev_err(&pdev->dev,
1182 "no resource defined for cmd DMA\n");
1183 ret = -ENXIO;
1184 goto fail_disable_clk;
1185 }
1186 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001187 }
eric miaofe69af02008-02-14 15:48:23 +08001188 }
eric miaofe69af02008-02-14 15:48:23 +08001189
1190 irq = platform_get_irq(pdev, 0);
1191 if (irq < 0) {
1192 dev_err(&pdev->dev, "no IRQ resource defined\n");
1193 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001194 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001195 }
1196
1197 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001198 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1199 if (IS_ERR(info->mmio_base)) {
1200 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001201 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001202 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001203 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001204
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001205 /* Allocate a buffer to allow flash detection */
1206 info->buf_size = INIT_BUFFER_SIZE;
1207 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1208 if (info->data_buff == NULL) {
1209 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001210 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001211 }
eric miaofe69af02008-02-14 15:48:23 +08001212
Haojian Zhuang346e1252009-09-10 14:27:23 +08001213 /* initialize all interrupts to be disabled */
1214 disable_int(info, NDSR_MASK);
1215
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001216 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001217 if (ret < 0) {
1218 dev_err(&pdev->dev, "failed to request IRQ\n");
1219 goto fail_free_buf;
1220 }
1221
Lei Wene353a202011-03-03 11:08:30 +08001222 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001223
Lei Wend4568822011-07-14 20:44:32 -07001224 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001225
eric miaofe69af02008-02-14 15:48:23 +08001226fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001227 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001228 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001229fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001230 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001231 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001232}
1233
1234static int pxa3xx_nand_remove(struct platform_device *pdev)
1235{
Lei Wene353a202011-03-03 11:08:30 +08001236 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001237 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001238 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001239
Lei Wend4568822011-07-14 20:44:32 -07001240 if (!info)
1241 return 0;
1242
Jingoo Han453810b2013-07-30 17:18:33 +09001243 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001244
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001245 irq = platform_get_irq(pdev, 0);
1246 if (irq >= 0)
1247 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001248 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001249
Ezequiel Garciafb320612013-04-17 13:38:12 -03001250 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001251
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001252 for (cs = 0; cs < pdata->num_cs; cs++)
1253 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001254 return 0;
1255}
1256
Daniel Mack1e7ba632012-07-22 19:51:02 +02001257static struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001258 {
1259 .compatible = "marvell,pxa3xx-nand",
1260 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1261 },
1262 {
1263 .compatible = "marvell,armada370-nand",
1264 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1265 },
Daniel Mack1e7ba632012-07-22 19:51:02 +02001266 {}
1267};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001268MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001269
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001270static enum pxa3xx_nand_variant
1271pxa3xx_nand_get_variant(struct platform_device *pdev)
1272{
1273 const struct of_device_id *of_id =
1274 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1275 if (!of_id)
1276 return PXA3XX_NAND_VARIANT_PXA;
1277 return (enum pxa3xx_nand_variant)of_id->data;
1278}
1279
Daniel Mack1e7ba632012-07-22 19:51:02 +02001280static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1281{
1282 struct pxa3xx_nand_platform_data *pdata;
1283 struct device_node *np = pdev->dev.of_node;
1284 const struct of_device_id *of_id =
1285 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1286
1287 if (!of_id)
1288 return 0;
1289
1290 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1291 if (!pdata)
1292 return -ENOMEM;
1293
1294 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1295 pdata->enable_arbiter = 1;
1296 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1297 pdata->keep_config = 1;
1298 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1299
1300 pdev->dev.platform_data = pdata;
1301
1302 return 0;
1303}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001304
Lei Wene353a202011-03-03 11:08:30 +08001305static int pxa3xx_nand_probe(struct platform_device *pdev)
1306{
1307 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001308 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001309 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001310 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001311
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001312#ifndef ARCH_HAS_DMA
1313 if (use_dma) {
1314 use_dma = 0;
1315 dev_warn(&pdev->dev,
1316 "This platform can't do DMA on this device\n");
1317 }
1318#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001319 ret = pxa3xx_nand_probe_dt(pdev);
1320 if (ret)
1321 return ret;
1322
Jingoo Han453810b2013-07-30 17:18:33 +09001323 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001324 if (!pdata) {
1325 dev_err(&pdev->dev, "no platform data defined\n");
1326 return -ENODEV;
1327 }
1328
Lei Wend4568822011-07-14 20:44:32 -07001329 ret = alloc_nand_resource(pdev);
1330 if (ret) {
1331 dev_err(&pdev->dev, "alloc nand resource failed\n");
1332 return ret;
1333 }
Lei Wene353a202011-03-03 11:08:30 +08001334
Lei Wend4568822011-07-14 20:44:32 -07001335 info = platform_get_drvdata(pdev);
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001336 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001337 probe_success = 0;
1338 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001339 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001340
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001341 /*
1342 * The mtd name matches the one used in 'mtdparts' kernel
1343 * parameter. This name cannot be changed or otherwise
1344 * user's mtd partitions configuration would get broken.
1345 */
1346 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001347 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001348 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001349 if (ret) {
1350 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1351 cs);
1352 continue;
1353 }
1354
Daniel Mack1e7ba632012-07-22 19:51:02 +02001355 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001356 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001357 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001358 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001359 if (!ret)
1360 probe_success = 1;
1361 }
1362
1363 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001364 pxa3xx_nand_remove(pdev);
1365 return -ENODEV;
1366 }
1367
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001368 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001369}
1370
eric miaofe69af02008-02-14 15:48:23 +08001371#ifdef CONFIG_PM
1372static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1373{
Lei Wene353a202011-03-03 11:08:30 +08001374 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001375 struct pxa3xx_nand_platform_data *pdata;
1376 struct mtd_info *mtd;
1377 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001378
Jingoo Han453810b2013-07-30 17:18:33 +09001379 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001380 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001381 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1382 return -EAGAIN;
1383 }
1384
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001385 for (cs = 0; cs < pdata->num_cs; cs++) {
1386 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001387 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001388 }
1389
eric miaofe69af02008-02-14 15:48:23 +08001390 return 0;
1391}
1392
1393static int pxa3xx_nand_resume(struct platform_device *pdev)
1394{
Lei Wene353a202011-03-03 11:08:30 +08001395 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001396 struct pxa3xx_nand_platform_data *pdata;
1397 struct mtd_info *mtd;
1398 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001399
Jingoo Han453810b2013-07-30 17:18:33 +09001400 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001401 /* We don't want to handle interrupt without calling mtd routine */
1402 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001403
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001404 /*
1405 * Directly set the chip select to a invalid value,
1406 * then the driver would reset the timing according
1407 * to current chip select at the beginning of cmdfunc
1408 */
1409 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001410
Lei Wen051fc412011-07-14 20:44:30 -07001411 /*
1412 * As the spec says, the NDSR would be updated to 0x1800 when
1413 * doing the nand_clk disable/enable.
1414 * To prevent it damaging state machine of the driver, clear
1415 * all status before resume
1416 */
1417 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001418 for (cs = 0; cs < pdata->num_cs; cs++) {
1419 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001420 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001421 }
1422
Lei Wen18c81b12010-08-17 17:25:57 +08001423 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001424}
1425#else
1426#define pxa3xx_nand_suspend NULL
1427#define pxa3xx_nand_resume NULL
1428#endif
1429
1430static struct platform_driver pxa3xx_nand_driver = {
1431 .driver = {
1432 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301433 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001434 },
1435 .probe = pxa3xx_nand_probe,
1436 .remove = pxa3xx_nand_remove,
1437 .suspend = pxa3xx_nand_suspend,
1438 .resume = pxa3xx_nand_resume,
1439};
1440
Axel Linf99640d2011-11-27 20:45:03 +08001441module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001442
1443MODULE_LICENSE("GPL");
1444MODULE_DESCRIPTION("PXA3xx NAND controller driver");