blob: ce343bfd0d65f640fe262192067fba4a5b63de26 [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
42/* registers and bit definitions */
43#define NDCR (0x00) /* Control register */
44#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
45#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
46#define NDSR (0x14) /* Status Register */
47#define NDPCR (0x18) /* Page Count Register */
48#define NDBDR0 (0x1C) /* Bad Block Register 0 */
49#define NDBDR1 (0x20) /* Bad Block Register 1 */
50#define NDDB (0x40) /* Data Buffer */
51#define NDCB0 (0x48) /* Command Buffer0 */
52#define NDCB1 (0x4C) /* Command Buffer1 */
53#define NDCB2 (0x50) /* Command Buffer2 */
54
55#define NDCR_SPARE_EN (0x1 << 31)
56#define NDCR_ECC_EN (0x1 << 30)
57#define NDCR_DMA_EN (0x1 << 29)
58#define NDCR_ND_RUN (0x1 << 28)
59#define NDCR_DWIDTH_C (0x1 << 27)
60#define NDCR_DWIDTH_M (0x1 << 26)
61#define NDCR_PAGE_SZ (0x1 << 24)
62#define NDCR_NCSX (0x1 << 23)
63#define NDCR_ND_MODE (0x3 << 21)
64#define NDCR_NAND_MODE (0x0)
65#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080066#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080067#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
68#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
69
70#define NDCR_RA_START (0x1 << 15)
71#define NDCR_PG_PER_BLK (0x1 << 14)
72#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080073#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080074
75#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080076#define NDSR_RDY (0x1 << 12)
77#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080078#define NDSR_CS0_PAGED (0x1 << 10)
79#define NDSR_CS1_PAGED (0x1 << 9)
80#define NDSR_CS0_CMDD (0x1 << 8)
81#define NDSR_CS1_CMDD (0x1 << 7)
82#define NDSR_CS0_BBD (0x1 << 6)
83#define NDSR_CS1_BBD (0x1 << 5)
84#define NDSR_DBERR (0x1 << 4)
85#define NDSR_SBERR (0x1 << 3)
86#define NDSR_WRDREQ (0x1 << 2)
87#define NDSR_RDDREQ (0x1 << 1)
88#define NDSR_WRCMDREQ (0x1)
89
Ezequiel Garcia41a63432013-08-12 14:14:51 -030090#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +080091#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +080092#define NDCB0_AUTO_RS (0x1 << 25)
93#define NDCB0_CSEL (0x1 << 24)
94#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
95#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
96#define NDCB0_NC (0x1 << 20)
97#define NDCB0_DBC (0x1 << 19)
98#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
99#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
100#define NDCB0_CMD2_MASK (0xff << 8)
101#define NDCB0_CMD1_MASK (0xff)
102#define NDCB0_ADDR_CYC_SHIFT (16)
103
eric miaofe69af02008-02-14 15:48:23 +0800104/* macros for registers read/write */
105#define nand_writel(info, off, val) \
106 __raw_writel((val), (info)->mmio_base + (off))
107
108#define nand_readl(info, off) \
109 __raw_readl((info)->mmio_base + (off))
110
111/* error code and state */
112enum {
113 ERR_NONE = 0,
114 ERR_DMABUSERR = -1,
115 ERR_SENDCMD = -2,
116 ERR_DBERR = -3,
117 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300118 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800119};
120
121enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800122 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700123 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800124 STATE_CMD_HANDLE,
125 STATE_DMA_READING,
126 STATE_DMA_WRITING,
127 STATE_DMA_DONE,
128 STATE_PIO_READING,
129 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800130 STATE_CMD_DONE,
131 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800132};
133
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300134enum pxa3xx_nand_variant {
135 PXA3XX_NAND_VARIANT_PXA,
136 PXA3XX_NAND_VARIANT_ARMADA370,
137};
138
Lei Wend4568822011-07-14 20:44:32 -0700139struct pxa3xx_nand_host {
140 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700141 struct mtd_info *mtd;
142 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800143
Lei Wend4568822011-07-14 20:44:32 -0700144 /* page size of attached chip */
145 unsigned int page_size;
146 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700147 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700148
149 /* calculated from pxa3xx_nand_flash data */
150 unsigned int col_addr_cycles;
151 unsigned int row_addr_cycles;
152 size_t read_id_bytes;
153
Lei Wend4568822011-07-14 20:44:32 -0700154};
155
156struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800157 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800158 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800159
160 struct clk *clk;
161 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800162 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700163 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800164
165 unsigned int buf_start;
166 unsigned int buf_count;
167
168 /* DMA information */
169 int drcmr_dat;
170 int drcmr_cmd;
171
172 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800173 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800174 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800175 int data_dma_ch;
176 struct pxa_dma_desc *data_desc;
177 dma_addr_t data_desc_addr;
178
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700179 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800180 unsigned int state;
181
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300182 /*
183 * This driver supports NFCv1 (as found in PXA SoC)
184 * and NFCv2 (as found in Armada 370/XP SoC).
185 */
186 enum pxa3xx_nand_variant variant;
187
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700188 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800189 int use_ecc; /* use HW ECC ? */
190 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300191 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800192 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800193
Lei Wen18c81b12010-08-17 17:25:57 +0800194 unsigned int page_size; /* page size of attached chip */
195 unsigned int data_size; /* data size in FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700196 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800197 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800198
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300199 /* cached register value */
200 uint32_t reg_ndcr;
201 uint32_t ndtr0cs0;
202 uint32_t ndtr1cs0;
203
eric miaofe69af02008-02-14 15:48:23 +0800204 /* generated NDCBx register values */
205 uint32_t ndcb0;
206 uint32_t ndcb1;
207 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300208 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800209};
210
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030211static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800212module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300213MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800214
Lei Wenc1f82472010-08-17 13:50:23 +0800215static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800216 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
217 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
218 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
219 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800220};
221
Lei Wenc1f82472010-08-17 13:50:23 +0800222static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800223{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
224{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
225{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
226{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
227{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
228{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
229{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
230{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
231{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800232};
233
Lei Wen227a8862010-08-18 18:00:03 +0800234/* Define a default flash type setting serve as flash detecting only */
235#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
236
eric miaofe69af02008-02-14 15:48:23 +0800237#define NDTR0_tCH(c) (min((c), 7) << 19)
238#define NDTR0_tCS(c) (min((c), 7) << 16)
239#define NDTR0_tWH(c) (min((c), 7) << 11)
240#define NDTR0_tWP(c) (min((c), 7) << 8)
241#define NDTR0_tRH(c) (min((c), 7) << 3)
242#define NDTR0_tRP(c) (min((c), 7) << 0)
243
244#define NDTR1_tR(c) (min((c), 65535) << 16)
245#define NDTR1_tWHR(c) (min((c), 15) << 4)
246#define NDTR1_tAR(c) (min((c), 15) << 0)
247
248/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800249#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800250
Lei Wend4568822011-07-14 20:44:32 -0700251static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200252 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800253{
Lei Wend4568822011-07-14 20:44:32 -0700254 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800255 unsigned long nand_clk = clk_get_rate(info->clk);
256 uint32_t ndtr0, ndtr1;
257
258 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
259 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
260 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
261 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
262 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
263 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
264
265 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
266 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
267 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
268
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300269 info->ndtr0cs0 = ndtr0;
270 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800271 nand_writel(info, NDTR0CS0, ndtr0);
272 nand_writel(info, NDTR1CS0, ndtr1);
273}
274
Lei Wen18c81b12010-08-17 17:25:57 +0800275static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800276{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700277 struct pxa3xx_nand_host *host = info->host[info->cs];
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300278 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800279
Lei Wend4568822011-07-14 20:44:32 -0700280 info->data_size = host->page_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800281 if (!oob_enable) {
282 info->oob_size = 0;
283 return;
284 }
285
Lei Wend4568822011-07-14 20:44:32 -0700286 switch (host->page_size) {
eric miaofe69af02008-02-14 15:48:23 +0800287 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800288 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800289 break;
290 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800291 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800292 break;
eric miaofe69af02008-02-14 15:48:23 +0800293 }
Lei Wen18c81b12010-08-17 17:25:57 +0800294}
295
Lei Wenf8155a42011-02-28 10:32:11 +0800296/**
297 * NOTE: it is a must to set ND_RUN firstly, then write
298 * command buffer, otherwise, it does not work.
299 * We enable all the interrupt at the same time, and
300 * let pxa3xx_nand_irq to handle all logic.
301 */
302static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
303{
304 uint32_t ndcr;
305
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300306 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300307
308 if (info->use_ecc)
309 ndcr |= NDCR_ECC_EN;
310 else
311 ndcr &= ~NDCR_ECC_EN;
312
313 if (info->use_dma)
314 ndcr |= NDCR_DMA_EN;
315 else
316 ndcr &= ~NDCR_DMA_EN;
317
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300318 if (info->use_spare)
319 ndcr |= NDCR_SPARE_EN;
320 else
321 ndcr &= ~NDCR_SPARE_EN;
322
Lei Wenf8155a42011-02-28 10:32:11 +0800323 ndcr |= NDCR_ND_RUN;
324
325 /* clear status bits and run */
326 nand_writel(info, NDCR, 0);
327 nand_writel(info, NDSR, NDSR_MASK);
328 nand_writel(info, NDCR, ndcr);
329}
330
331static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
332{
333 uint32_t ndcr;
334 int timeout = NAND_STOP_DELAY;
335
336 /* wait RUN bit in NDCR become 0 */
337 ndcr = nand_readl(info, NDCR);
338 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
339 ndcr = nand_readl(info, NDCR);
340 udelay(1);
341 }
342
343 if (timeout <= 0) {
344 ndcr &= ~NDCR_ND_RUN;
345 nand_writel(info, NDCR, ndcr);
346 }
347 /* clear status bits */
348 nand_writel(info, NDSR, NDSR_MASK);
349}
350
eric miaofe69af02008-02-14 15:48:23 +0800351static void enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
352{
353 uint32_t ndcr;
354
355 ndcr = nand_readl(info, NDCR);
356 nand_writel(info, NDCR, ndcr & ~int_mask);
357}
358
359static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
360{
361 uint32_t ndcr;
362
363 ndcr = nand_readl(info, NDCR);
364 nand_writel(info, NDCR, ndcr | int_mask);
365}
366
Lei Wenf8155a42011-02-28 10:32:11 +0800367static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800368{
eric miaofe69af02008-02-14 15:48:23 +0800369 switch (info->state) {
370 case STATE_PIO_WRITING:
371 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800372 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800373 if (info->oob_size > 0)
374 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
375 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800376 break;
377 case STATE_PIO_READING:
378 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800379 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800380 if (info->oob_size > 0)
381 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
382 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800383 break;
384 default:
Lei Wenda675b42011-07-14 20:44:31 -0700385 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800386 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800387 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800388 }
eric miaofe69af02008-02-14 15:48:23 +0800389}
390
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300391#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800392static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800393{
394 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800395 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800396
397 desc->ddadr = DDADR_STOP;
398 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
399
Lei Wenf8155a42011-02-28 10:32:11 +0800400 switch (info->state) {
401 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800402 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800403 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800404 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800405 break;
406 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800407 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800408 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800409 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800410 break;
411 default:
Lei Wenda675b42011-07-14 20:44:31 -0700412 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800413 info->state);
414 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800415 }
416
417 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
418 DDADR(info->data_dma_ch) = info->data_desc_addr;
419 DCSR(info->data_dma_ch) |= DCSR_RUN;
420}
421
422static void pxa3xx_nand_data_dma_irq(int channel, void *data)
423{
424 struct pxa3xx_nand_info *info = data;
425 uint32_t dcsr;
426
427 dcsr = DCSR(channel);
428 DCSR(channel) = dcsr;
429
430 if (dcsr & DCSR_BUSERR) {
431 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800432 }
433
Lei Wenf8155a42011-02-28 10:32:11 +0800434 info->state = STATE_DMA_DONE;
435 enable_int(info, NDCR_INT_MASK);
436 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800437}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300438#else
439static void start_data_dma(struct pxa3xx_nand_info *info)
440{}
441#endif
eric miaofe69af02008-02-14 15:48:23 +0800442
443static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
444{
445 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800446 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700447 unsigned int ready, cmd_done;
448
449 if (info->cs == 0) {
450 ready = NDSR_FLASH_RDY;
451 cmd_done = NDSR_CS0_CMDD;
452 } else {
453 ready = NDSR_RDY;
454 cmd_done = NDSR_CS1_CMDD;
455 }
eric miaofe69af02008-02-14 15:48:23 +0800456
457 status = nand_readl(info, NDSR);
458
Lei Wenf8155a42011-02-28 10:32:11 +0800459 if (status & NDSR_DBERR)
460 info->retcode = ERR_DBERR;
461 if (status & NDSR_SBERR)
462 info->retcode = ERR_SBERR;
463 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
464 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800465 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800466 disable_int(info, NDCR_INT_MASK);
467 info->state = (status & NDSR_RDDREQ) ?
468 STATE_DMA_READING : STATE_DMA_WRITING;
469 start_data_dma(info);
470 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800471 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800472 info->state = (status & NDSR_RDDREQ) ?
473 STATE_PIO_READING : STATE_PIO_WRITING;
474 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800475 }
Lei Wenf8155a42011-02-28 10:32:11 +0800476 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700477 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800478 info->state = STATE_CMD_DONE;
479 is_completed = 1;
480 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700481 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800482 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800483 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800484 }
Lei Wenf8155a42011-02-28 10:32:11 +0800485
486 if (status & NDSR_WRCMDREQ) {
487 nand_writel(info, NDSR, NDSR_WRCMDREQ);
488 status &= ~NDSR_WRCMDREQ;
489 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300490
491 /*
492 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
493 * must be loaded by writing directly either 12 or 16
494 * bytes directly to NDCB0, four bytes at a time.
495 *
496 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
497 * but each NDCBx register can be read.
498 */
Lei Wenf8155a42011-02-28 10:32:11 +0800499 nand_writel(info, NDCB0, info->ndcb0);
500 nand_writel(info, NDCB0, info->ndcb1);
501 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300502
503 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
504 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
505 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800506 }
Lei Wenf8155a42011-02-28 10:32:11 +0800507
508 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800509 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800510 if (is_completed)
511 complete(&info->cmd_complete);
512NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800513 return IRQ_HANDLED;
514}
515
eric miaofe69af02008-02-14 15:48:23 +0800516static inline int is_buf_blank(uint8_t *buf, size_t len)
517{
518 for (; len > 0; len--)
519 if (*buf++ != 0xff)
520 return 0;
521 return 1;
522}
523
Lei Wen4eb2da82011-02-28 10:32:13 +0800524static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
525 uint16_t column, int page_addr)
526{
Lei Wend4568822011-07-14 20:44:32 -0700527 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700528 struct pxa3xx_nand_host *host;
529 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800530
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700531 host = info->host[info->cs];
532 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800533 addr_cycle = 0;
534 exec_cmd = 1;
535
536 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800537 info->buf_start = 0;
538 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800539 info->oob_size = 0;
540 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300541 info->use_spare = 1;
Ezequiel Garcia0a60d042013-05-14 08:15:21 -0300542 info->use_dma = (use_dma) ? 1 : 0;
Lei Wen401e67e2011-02-28 10:32:14 +0800543 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800544 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700545 if (info->cs != 0)
546 info->ndcb0 = NDCB0_CSEL;
547 else
548 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800549
550 switch (command) {
551 case NAND_CMD_READ0:
552 case NAND_CMD_PAGEPROG:
553 info->use_ecc = 1;
554 case NAND_CMD_READOOB:
555 pxa3xx_set_datasize(info);
556 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300557 case NAND_CMD_PARAM:
558 info->use_spare = 0;
559 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800560 case NAND_CMD_SEQIN:
561 exec_cmd = 0;
562 break;
563 default:
564 info->ndcb1 = 0;
565 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300566 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800567 break;
568 }
569
Lei Wend4568822011-07-14 20:44:32 -0700570 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
571 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800572
573 switch (command) {
574 case NAND_CMD_READOOB:
575 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300576 info->buf_start = column;
577 info->ndcb0 |= NDCB0_CMD_TYPE(0)
578 | addr_cycle
579 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800580
Ezequiel Garciaec821352013-08-12 14:14:54 -0300581 if (command == NAND_CMD_READOOB)
582 info->buf_start += mtd->writesize;
583
584 /* Second command setting for large pages */
585 if (host->page_size >= PAGE_CHUNK_SIZE)
586 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800587
588 case NAND_CMD_SEQIN:
589 /* small page addr setting */
Lei Wend4568822011-07-14 20:44:32 -0700590 if (unlikely(host->page_size < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800591 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
592 | (column & 0xFF);
593
594 info->ndcb2 = 0;
595 } else {
596 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
597 | (column & 0xFFFF);
598
599 if (page_addr & 0xFF0000)
600 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
601 else
602 info->ndcb2 = 0;
603 }
604
605 info->buf_count = mtd->writesize + mtd->oobsize;
606 memset(info->data_buff, 0xFF, info->buf_count);
607
608 break;
609
610 case NAND_CMD_PAGEPROG:
611 if (is_buf_blank(info->data_buff,
612 (mtd->writesize + mtd->oobsize))) {
613 exec_cmd = 0;
614 break;
615 }
616
Lei Wen4eb2da82011-02-28 10:32:13 +0800617 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
618 | NDCB0_AUTO_RS
619 | NDCB0_ST_ROW_EN
620 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300621 | (NAND_CMD_PAGEPROG << 8)
622 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800623 | addr_cycle;
624 break;
625
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300626 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300627 info->buf_count = 256;
628 info->ndcb0 |= NDCB0_CMD_TYPE(0)
629 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300630 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300631 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300632 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300633 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300634 info->data_size = 256;
635 break;
636
Lei Wen4eb2da82011-02-28 10:32:13 +0800637 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700638 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800639 info->ndcb0 |= NDCB0_CMD_TYPE(3)
640 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300641 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300642 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800643
644 info->data_size = 8;
645 break;
646 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800647 info->buf_count = 1;
648 info->ndcb0 |= NDCB0_CMD_TYPE(4)
649 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300650 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800651
652 info->data_size = 8;
653 break;
654
655 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800656 info->ndcb0 |= NDCB0_CMD_TYPE(2)
657 | NDCB0_AUTO_RS
658 | NDCB0_ADDR_CYC(3)
659 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300660 | (NAND_CMD_ERASE2 << 8)
661 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800662 info->ndcb1 = page_addr;
663 info->ndcb2 = 0;
664
665 break;
666 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800667 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300668 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800669
670 break;
671
672 case NAND_CMD_ERASE2:
673 exec_cmd = 0;
674 break;
675
676 default:
677 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700678 dev_err(&info->pdev->dev, "non-supported command %x\n",
679 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800680 break;
681 }
682
683 return exec_cmd;
684}
685
eric miaofe69af02008-02-14 15:48:23 +0800686static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100687 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800688{
Lei Wend4568822011-07-14 20:44:32 -0700689 struct pxa3xx_nand_host *host = mtd->priv;
690 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800691 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800692
Lei Wen4eb2da82011-02-28 10:32:13 +0800693 /*
694 * if this is a x16 device ,then convert the input
695 * "byte" address into a "word" address appropriate
696 * for indexing a word-oriented device
697 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300698 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800699 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800700
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700701 /*
702 * There may be different NAND chip hooked to
703 * different chip select, so check whether
704 * chip select has been changed, if yes, reset the timing
705 */
706 if (info->cs != host->cs) {
707 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300708 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
709 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700710 }
711
Lei Wend4568822011-07-14 20:44:32 -0700712 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800713 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800714 if (exec_cmd) {
715 init_completion(&info->cmd_complete);
716 pxa3xx_nand_start(info);
717
718 ret = wait_for_completion_timeout(&info->cmd_complete,
719 CHIP_DELAY_TIMEOUT);
720 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700721 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800722 /* Stop State Machine for next command cycle */
723 pxa3xx_nand_stop(info);
724 }
eric miaofe69af02008-02-14 15:48:23 +0800725 }
Lei Wend4568822011-07-14 20:44:32 -0700726 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800727}
728
Josh Wufdbad98d2012-06-25 18:07:45 +0800729static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700730 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800731{
732 chip->write_buf(mtd, buf, mtd->writesize);
733 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800734
735 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800736}
737
738static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700739 struct nand_chip *chip, uint8_t *buf, int oob_required,
740 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800741{
Lei Wend4568822011-07-14 20:44:32 -0700742 struct pxa3xx_nand_host *host = mtd->priv;
743 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +0800744
745 chip->read_buf(mtd, buf, mtd->writesize);
746 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
747
748 if (info->retcode == ERR_SBERR) {
749 switch (info->use_ecc) {
750 case 1:
751 mtd->ecc_stats.corrected++;
752 break;
753 case 0:
754 default:
755 break;
756 }
757 } else if (info->retcode == ERR_DBERR) {
758 /*
759 * for blank page (all 0xff), HW will calculate its ECC as
760 * 0, which is different from the ECC information within
761 * OOB, ignore such double bit errors
762 */
763 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700764 info->retcode = ERR_NONE;
765 else
Lei Wenf8155a42011-02-28 10:32:11 +0800766 mtd->ecc_stats.failed++;
767 }
768
769 return 0;
770}
771
eric miaofe69af02008-02-14 15:48:23 +0800772static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
773{
Lei Wend4568822011-07-14 20:44:32 -0700774 struct pxa3xx_nand_host *host = mtd->priv;
775 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800776 char retval = 0xFF;
777
778 if (info->buf_start < info->buf_count)
779 /* Has just send a new command? */
780 retval = info->data_buff[info->buf_start++];
781
782 return retval;
783}
784
785static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
786{
Lei Wend4568822011-07-14 20:44:32 -0700787 struct pxa3xx_nand_host *host = mtd->priv;
788 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800789 u16 retval = 0xFFFF;
790
791 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
792 retval = *((u16 *)(info->data_buff+info->buf_start));
793 info->buf_start += 2;
794 }
795 return retval;
796}
797
798static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
799{
Lei Wend4568822011-07-14 20:44:32 -0700800 struct pxa3xx_nand_host *host = mtd->priv;
801 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800802 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
803
804 memcpy(buf, info->data_buff + info->buf_start, real_len);
805 info->buf_start += real_len;
806}
807
808static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
809 const uint8_t *buf, int len)
810{
Lei Wend4568822011-07-14 20:44:32 -0700811 struct pxa3xx_nand_host *host = mtd->priv;
812 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800813 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
814
815 memcpy(info->data_buff + info->buf_start, buf, real_len);
816 info->buf_start += real_len;
817}
818
eric miaofe69af02008-02-14 15:48:23 +0800819static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
820{
821 return;
822}
823
824static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
825{
Lei Wend4568822011-07-14 20:44:32 -0700826 struct pxa3xx_nand_host *host = mtd->priv;
827 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800828
829 /* pxa3xx_nand_send_command has waited for command complete */
830 if (this->state == FL_WRITING || this->state == FL_ERASING) {
831 if (info->retcode == ERR_NONE)
832 return 0;
833 else {
834 /*
835 * any error make it return 0x01 which will tell
836 * the caller the erase and write fail
837 */
838 return 0x01;
839 }
840 }
841
842 return 0;
843}
844
eric miaofe69af02008-02-14 15:48:23 +0800845static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200846 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800847{
848 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900849 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700850 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800851 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800852
Lei Wenda675b42011-07-14 20:44:31 -0700853 if (f->page_size != 2048 && f->page_size != 512) {
854 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800855 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700856 }
eric miaofe69af02008-02-14 15:48:23 +0800857
Lei Wenda675b42011-07-14 20:44:31 -0700858 if (f->flash_width != 16 && f->flash_width != 8) {
859 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800860 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700861 }
eric miaofe69af02008-02-14 15:48:23 +0800862
863 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700864 host->page_size = f->page_size;
865 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800866
867 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700868 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800869
870 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700871 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800872 else
Lei Wend4568822011-07-14 20:44:32 -0700873 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800874
875 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700876 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800877 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
878 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
879 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
880 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
881
Lei Wend4568822011-07-14 20:44:32 -0700882 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800883 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
884
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300885 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800886
Lei Wend4568822011-07-14 20:44:32 -0700887 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800888 return 0;
889}
890
Mike Rapoportf2710492009-02-17 13:54:47 +0200891static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
892{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700893 /*
894 * We set 0 by hard coding here, for we don't support keep_config
895 * when there is more than one chip attached to the controller
896 */
897 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200898 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200899
Lei Wend4568822011-07-14 20:44:32 -0700900 if (ndcr & NDCR_PAGE_SZ) {
901 host->page_size = 2048;
902 host->read_id_bytes = 4;
903 } else {
904 host->page_size = 512;
905 host->read_id_bytes = 2;
906 }
907
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300908 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
909 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
910 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200911 return 0;
912}
913
eric miaofe69af02008-02-14 15:48:23 +0800914/* the maximum possible buffer size for large page with OOB data
915 * is: 2048 + 64 = 2112 bytes, allocate a page here for both the
916 * data buffer and the DMA descriptor
917 */
918#define MAX_BUFF_SIZE PAGE_SIZE
919
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300920#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800921static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
922{
923 struct platform_device *pdev = info->pdev;
924 int data_desc_offset = MAX_BUFF_SIZE - sizeof(struct pxa_dma_desc);
925
926 if (use_dma == 0) {
927 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
928 if (info->data_buff == NULL)
929 return -ENOMEM;
930 return 0;
931 }
932
933 info->data_buff = dma_alloc_coherent(&pdev->dev, MAX_BUFF_SIZE,
934 &info->data_buff_phys, GFP_KERNEL);
935 if (info->data_buff == NULL) {
936 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
937 return -ENOMEM;
938 }
939
eric miaofe69af02008-02-14 15:48:23 +0800940 info->data_desc = (void *)info->data_buff + data_desc_offset;
941 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
942
943 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
944 pxa3xx_nand_data_dma_irq, info);
945 if (info->data_dma_ch < 0) {
946 dev_err(&pdev->dev, "failed to request data dma\n");
Lei Wend4568822011-07-14 20:44:32 -0700947 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
eric miaofe69af02008-02-14 15:48:23 +0800948 info->data_buff, info->data_buff_phys);
949 return info->data_dma_ch;
950 }
951
952 return 0;
953}
954
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300955static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
956{
957 struct platform_device *pdev = info->pdev;
958 if (use_dma) {
959 pxa_free_dma(info->data_dma_ch);
960 dma_free_coherent(&pdev->dev, MAX_BUFF_SIZE,
961 info->data_buff, info->data_buff_phys);
962 } else {
963 kfree(info->data_buff);
964 }
965}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300966#else
967static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
968{
969 info->data_buff = kmalloc(MAX_BUFF_SIZE, GFP_KERNEL);
970 if (info->data_buff == NULL)
971 return -ENOMEM;
972 return 0;
973}
974
975static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
976{
977 kfree(info->data_buff);
978}
979#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300980
Lei Wen401e67e2011-02-28 10:32:14 +0800981static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800982{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700983 struct mtd_info *mtd;
Lei Wend4568822011-07-14 20:44:32 -0700984 int ret;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700985 mtd = info->host[info->cs]->mtd;
Lei Wen401e67e2011-02-28 10:32:14 +0800986 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -0700987 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
988 if (ret)
989 return ret;
990
991 pxa3xx_nand_cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +0800992 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +0800993 return 0;
Lei Wend4568822011-07-14 20:44:32 -0700994
995 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +0800996}
eric miaofe69af02008-02-14 15:48:23 +0800997
Lei Wen401e67e2011-02-28 10:32:14 +0800998static int pxa3xx_nand_scan(struct mtd_info *mtd)
999{
Lei Wend4568822011-07-14 20:44:32 -07001000 struct pxa3xx_nand_host *host = mtd->priv;
1001 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001002 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001003 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001004 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001005 const struct pxa3xx_nand_flash *f = NULL;
1006 struct nand_chip *chip = mtd->priv;
1007 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001008 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001009 int i, ret, num;
1010
1011 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001012 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001013
1014 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001015 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001016 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1017 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001018
Lei Wend4568822011-07-14 20:44:32 -07001019 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001020 }
1021
1022 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1023 id = *((uint16_t *)(info->data_buff));
1024 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001025 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001026 else {
Lei Wenda675b42011-07-14 20:44:31 -07001027 dev_warn(&info->pdev->dev,
1028 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001029
1030 return -EINVAL;
1031 }
1032
1033 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1034 for (i = 0; i < num; i++) {
1035 if (i < pdata->num_flash)
1036 f = pdata->flash + i;
1037 else
1038 f = &builtin_flash_types[i - pdata->num_flash + 1];
1039
1040 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001041 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001042 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001043 }
1044
Lei Wen4332c112011-03-03 11:27:01 +08001045 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001046 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001047
1048 return -EINVAL;
1049 }
1050
Lei Wend4568822011-07-14 20:44:32 -07001051 ret = pxa3xx_nand_config_flash(info, f);
1052 if (ret) {
1053 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1054 return ret;
1055 }
1056
Lei Wen4332c112011-03-03 11:27:01 +08001057 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001058 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001059 pxa3xx_flash_ids[0].pagesize = f->page_size;
1060 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1061 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1062 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1063 if (f->flash_width == 16)
1064 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001065 pxa3xx_flash_ids[1].name = NULL;
1066 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001067KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001068 chip->ecc.mode = NAND_ECC_HW;
1069 chip->ecc.size = host->page_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001070 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001071
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001072 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001073 chip->options |= NAND_BUSWIDTH_16;
1074
Lei Wen0fab0282011-06-07 03:01:06 -07001075 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001076 return -ENODEV;
1077 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001078 if (mtd->writesize >= 2048)
1079 host->col_addr_cycles = 2;
1080 else
1081 host->col_addr_cycles = 1;
1082
Lei Wen4332c112011-03-03 11:27:01 +08001083 info->oob_buff = info->data_buff + mtd->writesize;
1084 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001085 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001086 else
Lei Wend4568822011-07-14 20:44:32 -07001087 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001088 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001089}
1090
Lei Wend4568822011-07-14 20:44:32 -07001091static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001092{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001093 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001094 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001095 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001096 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001097 struct mtd_info *mtd;
1098 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001099 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001100
Jingoo Han453810b2013-07-30 17:18:33 +09001101 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001102 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1103 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1104 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001105 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001106
eric miaofe69af02008-02-14 15:48:23 +08001107 info->pdev = pdev;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001108 for (cs = 0; cs < pdata->num_cs; cs++) {
1109 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1110 (sizeof(*mtd) + sizeof(*host)) * cs);
1111 chip = (struct nand_chip *)(&mtd[1]);
1112 host = (struct pxa3xx_nand_host *)chip;
1113 info->host[cs] = host;
1114 host->mtd = mtd;
1115 host->cs = cs;
1116 host->info_data = info;
1117 mtd->priv = host;
1118 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001119
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001120 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1121 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1122 chip->controller = &info->controller;
1123 chip->waitfunc = pxa3xx_nand_waitfunc;
1124 chip->select_chip = pxa3xx_nand_select_chip;
1125 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1126 chip->read_word = pxa3xx_nand_read_word;
1127 chip->read_byte = pxa3xx_nand_read_byte;
1128 chip->read_buf = pxa3xx_nand_read_buf;
1129 chip->write_buf = pxa3xx_nand_write_buf;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001130 }
Lei Wen401e67e2011-02-28 10:32:14 +08001131
1132 spin_lock_init(&chip->controller->lock);
1133 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001134 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001135 if (IS_ERR(info->clk)) {
1136 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001137 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001138 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001139 ret = clk_prepare_enable(info->clk);
1140 if (ret < 0)
1141 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001142
Daniel Mack1e7ba632012-07-22 19:51:02 +02001143 /*
1144 * This is a dirty hack to make this driver work from devicetree
1145 * bindings. It can be removed once we have a prober DMA controller
1146 * framework for DT.
1147 */
Ezequiel Garciaa33e4352013-05-14 08:15:22 -03001148 if (pdev->dev.of_node && of_machine_is_compatible("marvell,pxa3xx")) {
Daniel Mack1e7ba632012-07-22 19:51:02 +02001149 info->drcmr_dat = 97;
1150 info->drcmr_cmd = 99;
1151 } else {
1152 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1153 if (r == NULL) {
1154 dev_err(&pdev->dev, "no resource defined for data DMA\n");
1155 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001156 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001157 }
1158 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001159
Daniel Mack1e7ba632012-07-22 19:51:02 +02001160 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1161 if (r == NULL) {
1162 dev_err(&pdev->dev, "no resource defined for command DMA\n");
1163 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001164 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001165 }
1166 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001167 }
eric miaofe69af02008-02-14 15:48:23 +08001168
1169 irq = platform_get_irq(pdev, 0);
1170 if (irq < 0) {
1171 dev_err(&pdev->dev, "no IRQ resource defined\n");
1172 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001173 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001174 }
1175
1176 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001177 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1178 if (IS_ERR(info->mmio_base)) {
1179 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001180 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001181 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001182 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001183
1184 ret = pxa3xx_nand_init_buff(info);
1185 if (ret)
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001186 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001187
Haojian Zhuang346e1252009-09-10 14:27:23 +08001188 /* initialize all interrupts to be disabled */
1189 disable_int(info, NDSR_MASK);
1190
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001191 ret = request_irq(irq, pxa3xx_nand_irq, IRQF_DISABLED,
1192 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001193 if (ret < 0) {
1194 dev_err(&pdev->dev, "failed to request IRQ\n");
1195 goto fail_free_buf;
1196 }
1197
Lei Wene353a202011-03-03 11:08:30 +08001198 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001199
Lei Wend4568822011-07-14 20:44:32 -07001200 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001201
eric miaofe69af02008-02-14 15:48:23 +08001202fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001203 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001204 pxa3xx_nand_free_buff(info);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001205fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001206 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001207 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001208}
1209
1210static int pxa3xx_nand_remove(struct platform_device *pdev)
1211{
Lei Wene353a202011-03-03 11:08:30 +08001212 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001213 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001214 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001215
Lei Wend4568822011-07-14 20:44:32 -07001216 if (!info)
1217 return 0;
1218
Jingoo Han453810b2013-07-30 17:18:33 +09001219 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001220
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001221 irq = platform_get_irq(pdev, 0);
1222 if (irq >= 0)
1223 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001224 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001225
Ezequiel Garciafb320612013-04-17 13:38:12 -03001226 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001227
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001228 for (cs = 0; cs < pdata->num_cs; cs++)
1229 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001230 return 0;
1231}
1232
Daniel Mack1e7ba632012-07-22 19:51:02 +02001233#ifdef CONFIG_OF
1234static struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001235 {
1236 .compatible = "marvell,pxa3xx-nand",
1237 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
1238 },
1239 {
1240 .compatible = "marvell,armada370-nand",
1241 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
1242 },
Daniel Mack1e7ba632012-07-22 19:51:02 +02001243 {}
1244};
Ezequiel Garciaf3958982013-05-14 08:15:23 -03001245MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001246
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001247static enum pxa3xx_nand_variant
1248pxa3xx_nand_get_variant(struct platform_device *pdev)
1249{
1250 const struct of_device_id *of_id =
1251 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1252 if (!of_id)
1253 return PXA3XX_NAND_VARIANT_PXA;
1254 return (enum pxa3xx_nand_variant)of_id->data;
1255}
1256
Daniel Mack1e7ba632012-07-22 19:51:02 +02001257static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1258{
1259 struct pxa3xx_nand_platform_data *pdata;
1260 struct device_node *np = pdev->dev.of_node;
1261 const struct of_device_id *of_id =
1262 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1263
1264 if (!of_id)
1265 return 0;
1266
1267 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1268 if (!pdata)
1269 return -ENOMEM;
1270
1271 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1272 pdata->enable_arbiter = 1;
1273 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1274 pdata->keep_config = 1;
1275 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1276
1277 pdev->dev.platform_data = pdata;
1278
1279 return 0;
1280}
1281#else
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001282static inline int pxa3xx_nand_probe_dt(struct platform_device *pdev)
Daniel Mack1e7ba632012-07-22 19:51:02 +02001283{
1284 return 0;
1285}
1286#endif
1287
Lei Wene353a202011-03-03 11:08:30 +08001288static int pxa3xx_nand_probe(struct platform_device *pdev)
1289{
1290 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001291 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001292 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001293 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001294
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001295#ifndef ARCH_HAS_DMA
1296 if (use_dma) {
1297 use_dma = 0;
1298 dev_warn(&pdev->dev,
1299 "This platform can't do DMA on this device\n");
1300 }
1301#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001302 ret = pxa3xx_nand_probe_dt(pdev);
1303 if (ret)
1304 return ret;
1305
Jingoo Han453810b2013-07-30 17:18:33 +09001306 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001307 if (!pdata) {
1308 dev_err(&pdev->dev, "no platform data defined\n");
1309 return -ENODEV;
1310 }
1311
Lei Wend4568822011-07-14 20:44:32 -07001312 ret = alloc_nand_resource(pdev);
1313 if (ret) {
1314 dev_err(&pdev->dev, "alloc nand resource failed\n");
1315 return ret;
1316 }
Lei Wene353a202011-03-03 11:08:30 +08001317
Lei Wend4568822011-07-14 20:44:32 -07001318 info = platform_get_drvdata(pdev);
Ezequiel Garciac0f3b862013-08-10 16:34:52 -03001319 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001320 probe_success = 0;
1321 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001322 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001323
1324 mtd->name = pdev->name;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001325 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001326 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001327 if (ret) {
1328 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1329 cs);
1330 continue;
1331 }
1332
Daniel Mack1e7ba632012-07-22 19:51:02 +02001333 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001334 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001335 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001336 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001337 if (!ret)
1338 probe_success = 1;
1339 }
1340
1341 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001342 pxa3xx_nand_remove(pdev);
1343 return -ENODEV;
1344 }
1345
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001346 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001347}
1348
eric miaofe69af02008-02-14 15:48:23 +08001349#ifdef CONFIG_PM
1350static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1351{
Lei Wene353a202011-03-03 11:08:30 +08001352 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001353 struct pxa3xx_nand_platform_data *pdata;
1354 struct mtd_info *mtd;
1355 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001356
Jingoo Han453810b2013-07-30 17:18:33 +09001357 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001358 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001359 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1360 return -EAGAIN;
1361 }
1362
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001363 for (cs = 0; cs < pdata->num_cs; cs++) {
1364 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001365 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001366 }
1367
eric miaofe69af02008-02-14 15:48:23 +08001368 return 0;
1369}
1370
1371static int pxa3xx_nand_resume(struct platform_device *pdev)
1372{
Lei Wene353a202011-03-03 11:08:30 +08001373 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001374 struct pxa3xx_nand_platform_data *pdata;
1375 struct mtd_info *mtd;
1376 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001377
Jingoo Han453810b2013-07-30 17:18:33 +09001378 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001379 /* We don't want to handle interrupt without calling mtd routine */
1380 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001381
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001382 /*
1383 * Directly set the chip select to a invalid value,
1384 * then the driver would reset the timing according
1385 * to current chip select at the beginning of cmdfunc
1386 */
1387 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001388
Lei Wen051fc412011-07-14 20:44:30 -07001389 /*
1390 * As the spec says, the NDSR would be updated to 0x1800 when
1391 * doing the nand_clk disable/enable.
1392 * To prevent it damaging state machine of the driver, clear
1393 * all status before resume
1394 */
1395 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001396 for (cs = 0; cs < pdata->num_cs; cs++) {
1397 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001398 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001399 }
1400
Lei Wen18c81b12010-08-17 17:25:57 +08001401 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001402}
1403#else
1404#define pxa3xx_nand_suspend NULL
1405#define pxa3xx_nand_resume NULL
1406#endif
1407
1408static struct platform_driver pxa3xx_nand_driver = {
1409 .driver = {
1410 .name = "pxa3xx-nand",
Daniel Mack1e7ba632012-07-22 19:51:02 +02001411 .of_match_table = of_match_ptr(pxa3xx_nand_dt_ids),
eric miaofe69af02008-02-14 15:48:23 +08001412 },
1413 .probe = pxa3xx_nand_probe,
1414 .remove = pxa3xx_nand_remove,
1415 .suspend = pxa3xx_nand_suspend,
1416 .resume = pxa3xx_nand_resume,
1417};
1418
Axel Linf99640d2011-11-27 20:45:03 +08001419module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001420
1421MODULE_LICENSE("GPL");
1422MODULE_DESCRIPTION("PXA3xx NAND controller driver");