blob: 9f8c58f55c75dd553f7b82b19be41f670b5a3dcd [file] [log] [blame]
eric miaofe69af02008-02-14 15:48:23 +08001/*
2 * drivers/mtd/nand/pxa3xx_nand.c
3 *
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
Ezequiel Garciade484a32013-11-07 12:17:10 -030010 *
11 * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
eric miaofe69af02008-02-14 15:48:23 +080012 */
13
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080014#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080015#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/clk.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/nand.h>
23#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010024#include <linux/io.h>
25#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020027#include <linux/of.h>
28#include <linux/of_device.h>
eric miaofe69af02008-02-14 15:48:23 +080029
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030030#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
31#define ARCH_HAS_DMA
32#endif
33
34#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080035#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030036#endif
37
Arnd Bergmann293b2da2012-08-24 15:16:48 +020038#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080039
40#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080041#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080042#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080043
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030044/*
45 * Define a buffer size for the initial command that detects the flash device:
46 * STATUS, READID and PARAM. The largest of these is the PARAM command,
47 * needing 256 bytes.
48 */
49#define INIT_BUFFER_SIZE 256
50
eric miaofe69af02008-02-14 15:48:23 +080051/* registers and bit definitions */
52#define NDCR (0x00) /* Control register */
53#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
54#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
55#define NDSR (0x14) /* Status Register */
56#define NDPCR (0x18) /* Page Count Register */
57#define NDBDR0 (0x1C) /* Bad Block Register 0 */
58#define NDBDR1 (0x20) /* Bad Block Register 1 */
59#define NDDB (0x40) /* Data Buffer */
60#define NDCB0 (0x48) /* Command Buffer0 */
61#define NDCB1 (0x4C) /* Command Buffer1 */
62#define NDCB2 (0x50) /* Command Buffer2 */
63
64#define NDCR_SPARE_EN (0x1 << 31)
65#define NDCR_ECC_EN (0x1 << 30)
66#define NDCR_DMA_EN (0x1 << 29)
67#define NDCR_ND_RUN (0x1 << 28)
68#define NDCR_DWIDTH_C (0x1 << 27)
69#define NDCR_DWIDTH_M (0x1 << 26)
70#define NDCR_PAGE_SZ (0x1 << 24)
71#define NDCR_NCSX (0x1 << 23)
72#define NDCR_ND_MODE (0x3 << 21)
73#define NDCR_NAND_MODE (0x0)
74#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080075#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080076#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
77#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
78
79#define NDCR_RA_START (0x1 << 15)
80#define NDCR_PG_PER_BLK (0x1 << 14)
81#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080082#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080083
84#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080085#define NDSR_RDY (0x1 << 12)
86#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080087#define NDSR_CS0_PAGED (0x1 << 10)
88#define NDSR_CS1_PAGED (0x1 << 9)
89#define NDSR_CS0_CMDD (0x1 << 8)
90#define NDSR_CS1_CMDD (0x1 << 7)
91#define NDSR_CS0_BBD (0x1 << 6)
92#define NDSR_CS1_BBD (0x1 << 5)
93#define NDSR_DBERR (0x1 << 4)
94#define NDSR_SBERR (0x1 << 3)
95#define NDSR_WRDREQ (0x1 << 2)
96#define NDSR_RDDREQ (0x1 << 1)
97#define NDSR_WRCMDREQ (0x1)
98
Ezequiel Garcia41a63432013-08-12 14:14:51 -030099#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800100#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800101#define NDCB0_AUTO_RS (0x1 << 25)
102#define NDCB0_CSEL (0x1 << 24)
103#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
104#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
105#define NDCB0_NC (0x1 << 20)
106#define NDCB0_DBC (0x1 << 19)
107#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
108#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
109#define NDCB0_CMD2_MASK (0xff << 8)
110#define NDCB0_CMD1_MASK (0xff)
111#define NDCB0_ADDR_CYC_SHIFT (16)
112
eric miaofe69af02008-02-14 15:48:23 +0800113/* macros for registers read/write */
114#define nand_writel(info, off, val) \
115 __raw_writel((val), (info)->mmio_base + (off))
116
117#define nand_readl(info, off) \
118 __raw_readl((info)->mmio_base + (off))
119
120/* error code and state */
121enum {
122 ERR_NONE = 0,
123 ERR_DMABUSERR = -1,
124 ERR_SENDCMD = -2,
125 ERR_DBERR = -3,
126 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300127 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800128};
129
130enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800131 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700132 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800133 STATE_CMD_HANDLE,
134 STATE_DMA_READING,
135 STATE_DMA_WRITING,
136 STATE_DMA_DONE,
137 STATE_PIO_READING,
138 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800139 STATE_CMD_DONE,
140 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800141};
142
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300143enum pxa3xx_nand_variant {
144 PXA3XX_NAND_VARIANT_PXA,
145 PXA3XX_NAND_VARIANT_ARMADA370,
146};
147
Lei Wend4568822011-07-14 20:44:32 -0700148struct pxa3xx_nand_host {
149 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700150 struct mtd_info *mtd;
151 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800152
Lei Wend4568822011-07-14 20:44:32 -0700153 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700154 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700155 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700156
157 /* calculated from pxa3xx_nand_flash data */
158 unsigned int col_addr_cycles;
159 unsigned int row_addr_cycles;
160 size_t read_id_bytes;
161
Lei Wend4568822011-07-14 20:44:32 -0700162};
163
164struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800165 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800166 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800167
168 struct clk *clk;
169 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800170 unsigned long mmio_phys;
Lei Wend4568822011-07-14 20:44:32 -0700171 struct completion cmd_complete;
eric miaofe69af02008-02-14 15:48:23 +0800172
173 unsigned int buf_start;
174 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300175 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800176
177 /* DMA information */
178 int drcmr_dat;
179 int drcmr_cmd;
180
181 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800182 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800183 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800184 int data_dma_ch;
185 struct pxa_dma_desc *data_desc;
186 dma_addr_t data_desc_addr;
187
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700188 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800189 unsigned int state;
190
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300191 /*
192 * This driver supports NFCv1 (as found in PXA SoC)
193 * and NFCv2 (as found in Armada 370/XP SoC).
194 */
195 enum pxa3xx_nand_variant variant;
196
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700197 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800198 int use_ecc; /* use HW ECC ? */
199 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300200 int use_spare; /* use spare ? */
Lei Wen401e67e2011-02-28 10:32:14 +0800201 int is_ready;
eric miaofe69af02008-02-14 15:48:23 +0800202
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300203 unsigned int fifo_size; /* max. data size in the FIFO */
204 unsigned int data_size; /* data to be read from FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700205 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800206 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800207
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300208 /* cached register value */
209 uint32_t reg_ndcr;
210 uint32_t ndtr0cs0;
211 uint32_t ndtr1cs0;
212
eric miaofe69af02008-02-14 15:48:23 +0800213 /* generated NDCBx register values */
214 uint32_t ndcb0;
215 uint32_t ndcb1;
216 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300217 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800218};
219
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030220static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800221module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300222MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800223
Lei Wenc1f82472010-08-17 13:50:23 +0800224static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800225 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
226 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
227 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
228 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800229};
230
Lei Wenc1f82472010-08-17 13:50:23 +0800231static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800232{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
233{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
234{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
235{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
236{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
237{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
238{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
239{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
240{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800241};
242
Lei Wen227a8862010-08-18 18:00:03 +0800243/* Define a default flash type setting serve as flash detecting only */
244#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
245
eric miaofe69af02008-02-14 15:48:23 +0800246#define NDTR0_tCH(c) (min((c), 7) << 19)
247#define NDTR0_tCS(c) (min((c), 7) << 16)
248#define NDTR0_tWH(c) (min((c), 7) << 11)
249#define NDTR0_tWP(c) (min((c), 7) << 8)
250#define NDTR0_tRH(c) (min((c), 7) << 3)
251#define NDTR0_tRP(c) (min((c), 7) << 0)
252
253#define NDTR1_tR(c) (min((c), 65535) << 16)
254#define NDTR1_tWHR(c) (min((c), 15) << 4)
255#define NDTR1_tAR(c) (min((c), 15) << 0)
256
257/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800258#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800259
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300260static struct of_device_id pxa3xx_nand_dt_ids[] = {
261 {
262 .compatible = "marvell,pxa3xx-nand",
263 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
264 },
265 {}
266};
267MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
268
269static enum pxa3xx_nand_variant
270pxa3xx_nand_get_variant(struct platform_device *pdev)
271{
272 const struct of_device_id *of_id =
273 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
274 if (!of_id)
275 return PXA3XX_NAND_VARIANT_PXA;
276 return (enum pxa3xx_nand_variant)of_id->data;
277}
278
Lei Wend4568822011-07-14 20:44:32 -0700279static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200280 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800281{
Lei Wend4568822011-07-14 20:44:32 -0700282 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800283 unsigned long nand_clk = clk_get_rate(info->clk);
284 uint32_t ndtr0, ndtr1;
285
286 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
287 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
288 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
289 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
290 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
291 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
292
293 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
294 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
295 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
296
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300297 info->ndtr0cs0 = ndtr0;
298 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800299 nand_writel(info, NDTR0CS0, ndtr0);
300 nand_writel(info, NDTR1CS0, ndtr1);
301}
302
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300303/*
304 * Set the data and OOB size, depending on the selected
305 * spare and ECC configuration.
306 * Only applicable to READ0, READOOB and PAGEPROG commands.
307 */
Lei Wen18c81b12010-08-17 17:25:57 +0800308static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800309{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300310 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800311
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300312 info->data_size = info->fifo_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800313 if (!oob_enable) {
314 info->oob_size = 0;
315 return;
316 }
317
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300318 switch (info->fifo_size) {
eric miaofe69af02008-02-14 15:48:23 +0800319 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800320 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800321 break;
322 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800323 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800324 break;
eric miaofe69af02008-02-14 15:48:23 +0800325 }
Lei Wen18c81b12010-08-17 17:25:57 +0800326}
327
Lei Wenf8155a42011-02-28 10:32:11 +0800328/**
329 * NOTE: it is a must to set ND_RUN firstly, then write
330 * command buffer, otherwise, it does not work.
331 * We enable all the interrupt at the same time, and
332 * let pxa3xx_nand_irq to handle all logic.
333 */
334static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
335{
336 uint32_t ndcr;
337
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300338 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300339
340 if (info->use_ecc)
341 ndcr |= NDCR_ECC_EN;
342 else
343 ndcr &= ~NDCR_ECC_EN;
344
345 if (info->use_dma)
346 ndcr |= NDCR_DMA_EN;
347 else
348 ndcr &= ~NDCR_DMA_EN;
349
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300350 if (info->use_spare)
351 ndcr |= NDCR_SPARE_EN;
352 else
353 ndcr &= ~NDCR_SPARE_EN;
354
Lei Wenf8155a42011-02-28 10:32:11 +0800355 ndcr |= NDCR_ND_RUN;
356
357 /* clear status bits and run */
358 nand_writel(info, NDCR, 0);
359 nand_writel(info, NDSR, NDSR_MASK);
360 nand_writel(info, NDCR, ndcr);
361}
362
363static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
364{
365 uint32_t ndcr;
366 int timeout = NAND_STOP_DELAY;
367
368 /* wait RUN bit in NDCR become 0 */
369 ndcr = nand_readl(info, NDCR);
370 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
371 ndcr = nand_readl(info, NDCR);
372 udelay(1);
373 }
374
375 if (timeout <= 0) {
376 ndcr &= ~NDCR_ND_RUN;
377 nand_writel(info, NDCR, ndcr);
378 }
379 /* clear status bits */
380 nand_writel(info, NDSR, NDSR_MASK);
381}
382
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300383static void __maybe_unused
384enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800385{
386 uint32_t ndcr;
387
388 ndcr = nand_readl(info, NDCR);
389 nand_writel(info, NDCR, ndcr & ~int_mask);
390}
391
392static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
393{
394 uint32_t ndcr;
395
396 ndcr = nand_readl(info, NDCR);
397 nand_writel(info, NDCR, ndcr | int_mask);
398}
399
Lei Wenf8155a42011-02-28 10:32:11 +0800400static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800401{
eric miaofe69af02008-02-14 15:48:23 +0800402 switch (info->state) {
403 case STATE_PIO_WRITING:
404 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800405 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800406 if (info->oob_size > 0)
407 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
408 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800409 break;
410 case STATE_PIO_READING:
411 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800412 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800413 if (info->oob_size > 0)
414 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
415 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800416 break;
417 default:
Lei Wenda675b42011-07-14 20:44:31 -0700418 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800419 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800420 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800421 }
eric miaofe69af02008-02-14 15:48:23 +0800422}
423
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300424#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800425static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800426{
427 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800428 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800429
430 desc->ddadr = DDADR_STOP;
431 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
432
Lei Wenf8155a42011-02-28 10:32:11 +0800433 switch (info->state) {
434 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800435 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800436 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800437 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800438 break;
439 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800440 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800441 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800442 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800443 break;
444 default:
Lei Wenda675b42011-07-14 20:44:31 -0700445 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800446 info->state);
447 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800448 }
449
450 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
451 DDADR(info->data_dma_ch) = info->data_desc_addr;
452 DCSR(info->data_dma_ch) |= DCSR_RUN;
453}
454
455static void pxa3xx_nand_data_dma_irq(int channel, void *data)
456{
457 struct pxa3xx_nand_info *info = data;
458 uint32_t dcsr;
459
460 dcsr = DCSR(channel);
461 DCSR(channel) = dcsr;
462
463 if (dcsr & DCSR_BUSERR) {
464 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800465 }
466
Lei Wenf8155a42011-02-28 10:32:11 +0800467 info->state = STATE_DMA_DONE;
468 enable_int(info, NDCR_INT_MASK);
469 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800470}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300471#else
472static void start_data_dma(struct pxa3xx_nand_info *info)
473{}
474#endif
eric miaofe69af02008-02-14 15:48:23 +0800475
476static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
477{
478 struct pxa3xx_nand_info *info = devid;
Lei Wenf8155a42011-02-28 10:32:11 +0800479 unsigned int status, is_completed = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700480 unsigned int ready, cmd_done;
481
482 if (info->cs == 0) {
483 ready = NDSR_FLASH_RDY;
484 cmd_done = NDSR_CS0_CMDD;
485 } else {
486 ready = NDSR_RDY;
487 cmd_done = NDSR_CS1_CMDD;
488 }
eric miaofe69af02008-02-14 15:48:23 +0800489
490 status = nand_readl(info, NDSR);
491
Lei Wenf8155a42011-02-28 10:32:11 +0800492 if (status & NDSR_DBERR)
493 info->retcode = ERR_DBERR;
494 if (status & NDSR_SBERR)
495 info->retcode = ERR_SBERR;
496 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
497 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800498 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800499 disable_int(info, NDCR_INT_MASK);
500 info->state = (status & NDSR_RDDREQ) ?
501 STATE_DMA_READING : STATE_DMA_WRITING;
502 start_data_dma(info);
503 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800504 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800505 info->state = (status & NDSR_RDDREQ) ?
506 STATE_PIO_READING : STATE_PIO_WRITING;
507 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800508 }
Lei Wenf8155a42011-02-28 10:32:11 +0800509 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700510 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800511 info->state = STATE_CMD_DONE;
512 is_completed = 1;
513 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700514 if (status & ready) {
Lei Wen401e67e2011-02-28 10:32:14 +0800515 info->is_ready = 1;
eric miaofe69af02008-02-14 15:48:23 +0800516 info->state = STATE_READY;
Lei Wen401e67e2011-02-28 10:32:14 +0800517 }
Lei Wenf8155a42011-02-28 10:32:11 +0800518
519 if (status & NDSR_WRCMDREQ) {
520 nand_writel(info, NDSR, NDSR_WRCMDREQ);
521 status &= ~NDSR_WRCMDREQ;
522 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300523
524 /*
525 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
526 * must be loaded by writing directly either 12 or 16
527 * bytes directly to NDCB0, four bytes at a time.
528 *
529 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
530 * but each NDCBx register can be read.
531 */
Lei Wenf8155a42011-02-28 10:32:11 +0800532 nand_writel(info, NDCB0, info->ndcb0);
533 nand_writel(info, NDCB0, info->ndcb1);
534 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300535
536 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
537 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
538 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800539 }
Lei Wenf8155a42011-02-28 10:32:11 +0800540
541 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800542 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800543 if (is_completed)
544 complete(&info->cmd_complete);
545NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800546 return IRQ_HANDLED;
547}
548
eric miaofe69af02008-02-14 15:48:23 +0800549static inline int is_buf_blank(uint8_t *buf, size_t len)
550{
551 for (; len > 0; len--)
552 if (*buf++ != 0xff)
553 return 0;
554 return 1;
555}
556
Lei Wen4eb2da82011-02-28 10:32:13 +0800557static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
558 uint16_t column, int page_addr)
559{
Lei Wend4568822011-07-14 20:44:32 -0700560 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700561 struct pxa3xx_nand_host *host;
562 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800563
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700564 host = info->host[info->cs];
565 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800566 addr_cycle = 0;
567 exec_cmd = 1;
568
569 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800570 info->buf_start = 0;
571 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800572 info->oob_size = 0;
573 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300574 info->use_spare = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800575 info->is_ready = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800576 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700577 if (info->cs != 0)
578 info->ndcb0 = NDCB0_CSEL;
579 else
580 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800581
582 switch (command) {
583 case NAND_CMD_READ0:
584 case NAND_CMD_PAGEPROG:
585 info->use_ecc = 1;
586 case NAND_CMD_READOOB:
587 pxa3xx_set_datasize(info);
588 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300589 case NAND_CMD_PARAM:
590 info->use_spare = 0;
591 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800592 case NAND_CMD_SEQIN:
593 exec_cmd = 0;
594 break;
595 default:
596 info->ndcb1 = 0;
597 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300598 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800599 break;
600 }
601
Lei Wend4568822011-07-14 20:44:32 -0700602 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
603 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800604
605 switch (command) {
606 case NAND_CMD_READOOB:
607 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300608 info->buf_start = column;
609 info->ndcb0 |= NDCB0_CMD_TYPE(0)
610 | addr_cycle
611 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800612
Ezequiel Garciaec821352013-08-12 14:14:54 -0300613 if (command == NAND_CMD_READOOB)
614 info->buf_start += mtd->writesize;
615
616 /* Second command setting for large pages */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300617 if (mtd->writesize >= PAGE_CHUNK_SIZE)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300618 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800619
620 case NAND_CMD_SEQIN:
621 /* small page addr setting */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300622 if (unlikely(mtd->writesize < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800623 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
624 | (column & 0xFF);
625
626 info->ndcb2 = 0;
627 } else {
628 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
629 | (column & 0xFFFF);
630
631 if (page_addr & 0xFF0000)
632 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
633 else
634 info->ndcb2 = 0;
635 }
636
637 info->buf_count = mtd->writesize + mtd->oobsize;
638 memset(info->data_buff, 0xFF, info->buf_count);
639
640 break;
641
642 case NAND_CMD_PAGEPROG:
643 if (is_buf_blank(info->data_buff,
644 (mtd->writesize + mtd->oobsize))) {
645 exec_cmd = 0;
646 break;
647 }
648
Lei Wen4eb2da82011-02-28 10:32:13 +0800649 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
650 | NDCB0_AUTO_RS
651 | NDCB0_ST_ROW_EN
652 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300653 | (NAND_CMD_PAGEPROG << 8)
654 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800655 | addr_cycle;
656 break;
657
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300658 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300659 info->buf_count = 256;
660 info->ndcb0 |= NDCB0_CMD_TYPE(0)
661 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300662 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300663 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300664 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300665 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300666 info->data_size = 256;
667 break;
668
Lei Wen4eb2da82011-02-28 10:32:13 +0800669 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700670 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800671 info->ndcb0 |= NDCB0_CMD_TYPE(3)
672 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300673 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300674 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800675
676 info->data_size = 8;
677 break;
678 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800679 info->buf_count = 1;
680 info->ndcb0 |= NDCB0_CMD_TYPE(4)
681 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300682 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800683
684 info->data_size = 8;
685 break;
686
687 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800688 info->ndcb0 |= NDCB0_CMD_TYPE(2)
689 | NDCB0_AUTO_RS
690 | NDCB0_ADDR_CYC(3)
691 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300692 | (NAND_CMD_ERASE2 << 8)
693 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800694 info->ndcb1 = page_addr;
695 info->ndcb2 = 0;
696
697 break;
698 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800699 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300700 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800701
702 break;
703
704 case NAND_CMD_ERASE2:
705 exec_cmd = 0;
706 break;
707
708 default:
709 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700710 dev_err(&info->pdev->dev, "non-supported command %x\n",
711 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800712 break;
713 }
714
715 return exec_cmd;
716}
717
eric miaofe69af02008-02-14 15:48:23 +0800718static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100719 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800720{
Lei Wend4568822011-07-14 20:44:32 -0700721 struct pxa3xx_nand_host *host = mtd->priv;
722 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800723 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800724
Lei Wen4eb2da82011-02-28 10:32:13 +0800725 /*
726 * if this is a x16 device ,then convert the input
727 * "byte" address into a "word" address appropriate
728 * for indexing a word-oriented device
729 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300730 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800731 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800732
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700733 /*
734 * There may be different NAND chip hooked to
735 * different chip select, so check whether
736 * chip select has been changed, if yes, reset the timing
737 */
738 if (info->cs != host->cs) {
739 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300740 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
741 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700742 }
743
Lei Wend4568822011-07-14 20:44:32 -0700744 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800745 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800746 if (exec_cmd) {
747 init_completion(&info->cmd_complete);
748 pxa3xx_nand_start(info);
749
750 ret = wait_for_completion_timeout(&info->cmd_complete,
751 CHIP_DELAY_TIMEOUT);
752 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700753 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800754 /* Stop State Machine for next command cycle */
755 pxa3xx_nand_stop(info);
756 }
eric miaofe69af02008-02-14 15:48:23 +0800757 }
Lei Wend4568822011-07-14 20:44:32 -0700758 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800759}
760
Josh Wufdbad98d2012-06-25 18:07:45 +0800761static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700762 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800763{
764 chip->write_buf(mtd, buf, mtd->writesize);
765 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800766
767 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800768}
769
770static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700771 struct nand_chip *chip, uint8_t *buf, int oob_required,
772 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800773{
Lei Wend4568822011-07-14 20:44:32 -0700774 struct pxa3xx_nand_host *host = mtd->priv;
775 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300776 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800777
778 chip->read_buf(mtd, buf, mtd->writesize);
779 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
780
781 if (info->retcode == ERR_SBERR) {
782 switch (info->use_ecc) {
783 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300784 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800785 mtd->ecc_stats.corrected++;
786 break;
787 case 0:
788 default:
789 break;
790 }
791 } else if (info->retcode == ERR_DBERR) {
792 /*
793 * for blank page (all 0xff), HW will calculate its ECC as
794 * 0, which is different from the ECC information within
795 * OOB, ignore such double bit errors
796 */
797 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700798 info->retcode = ERR_NONE;
799 else
Lei Wenf8155a42011-02-28 10:32:11 +0800800 mtd->ecc_stats.failed++;
801 }
802
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300803 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800804}
805
eric miaofe69af02008-02-14 15:48:23 +0800806static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
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 char retval = 0xFF;
811
812 if (info->buf_start < info->buf_count)
813 /* Has just send a new command? */
814 retval = info->data_buff[info->buf_start++];
815
816 return retval;
817}
818
819static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
820{
Lei Wend4568822011-07-14 20:44:32 -0700821 struct pxa3xx_nand_host *host = mtd->priv;
822 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800823 u16 retval = 0xFFFF;
824
825 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
826 retval = *((u16 *)(info->data_buff+info->buf_start));
827 info->buf_start += 2;
828 }
829 return retval;
830}
831
832static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
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 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
837
838 memcpy(buf, info->data_buff + info->buf_start, real_len);
839 info->buf_start += real_len;
840}
841
842static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
843 const uint8_t *buf, int len)
844{
Lei Wend4568822011-07-14 20:44:32 -0700845 struct pxa3xx_nand_host *host = mtd->priv;
846 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800847 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
848
849 memcpy(info->data_buff + info->buf_start, buf, real_len);
850 info->buf_start += real_len;
851}
852
eric miaofe69af02008-02-14 15:48:23 +0800853static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
854{
855 return;
856}
857
858static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
859{
Lei Wend4568822011-07-14 20:44:32 -0700860 struct pxa3xx_nand_host *host = mtd->priv;
861 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800862
863 /* pxa3xx_nand_send_command has waited for command complete */
864 if (this->state == FL_WRITING || this->state == FL_ERASING) {
865 if (info->retcode == ERR_NONE)
866 return 0;
867 else {
868 /*
869 * any error make it return 0x01 which will tell
870 * the caller the erase and write fail
871 */
872 return 0x01;
873 }
874 }
875
876 return 0;
877}
878
eric miaofe69af02008-02-14 15:48:23 +0800879static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200880 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800881{
882 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900883 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700884 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800885 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800886
Lei Wenda675b42011-07-14 20:44:31 -0700887 if (f->page_size != 2048 && f->page_size != 512) {
888 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800889 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700890 }
eric miaofe69af02008-02-14 15:48:23 +0800891
Lei Wenda675b42011-07-14 20:44:31 -0700892 if (f->flash_width != 16 && f->flash_width != 8) {
893 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800894 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700895 }
eric miaofe69af02008-02-14 15:48:23 +0800896
897 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700898 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800899
900 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700901 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800902
903 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700904 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800905 else
Lei Wend4568822011-07-14 20:44:32 -0700906 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800907
908 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700909 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800910 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
911 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
912 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
913 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
914
Lei Wend4568822011-07-14 20:44:32 -0700915 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800916 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
917
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300918 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800919
Lei Wend4568822011-07-14 20:44:32 -0700920 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800921 return 0;
922}
923
Mike Rapoportf2710492009-02-17 13:54:47 +0200924static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
925{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700926 /*
927 * We set 0 by hard coding here, for we don't support keep_config
928 * when there is more than one chip attached to the controller
929 */
930 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200931 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200932
Lei Wend4568822011-07-14 20:44:32 -0700933 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300934 /* Controller's FIFO size */
935 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -0700936 host->read_id_bytes = 4;
937 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300938 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -0700939 host->read_id_bytes = 2;
940 }
941
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300942 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
943 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
944 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200945 return 0;
946}
947
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300948#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800949static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
950{
951 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300952 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800953
954 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300955 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800956 if (info->data_buff == NULL)
957 return -ENOMEM;
958 return 0;
959 }
960
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300961 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800962 &info->data_buff_phys, GFP_KERNEL);
963 if (info->data_buff == NULL) {
964 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
965 return -ENOMEM;
966 }
967
eric miaofe69af02008-02-14 15:48:23 +0800968 info->data_desc = (void *)info->data_buff + data_desc_offset;
969 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
970
971 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
972 pxa3xx_nand_data_dma_irq, info);
973 if (info->data_dma_ch < 0) {
974 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300975 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800976 info->data_buff, info->data_buff_phys);
977 return info->data_dma_ch;
978 }
979
Ezequiel Garcia95b26562013-10-04 15:30:37 -0300980 /*
981 * Now that DMA buffers are allocated we turn on
982 * DMA proper for I/O operations.
983 */
984 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800985 return 0;
986}
987
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300988static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
989{
990 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -0300991 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300992 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300993 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300994 info->data_buff, info->data_buff_phys);
995 } else {
996 kfree(info->data_buff);
997 }
998}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300999#else
1000static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1001{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001002 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001003 if (info->data_buff == NULL)
1004 return -ENOMEM;
1005 return 0;
1006}
1007
1008static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1009{
1010 kfree(info->data_buff);
1011}
1012#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001013
Lei Wen401e67e2011-02-28 10:32:14 +08001014static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001015{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001016 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001017 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001018 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001019
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001020 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001021 chip = mtd->priv;
1022
Lei Wen401e67e2011-02-28 10:32:14 +08001023 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001024 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1025 if (ret)
1026 return ret;
1027
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001028 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Lei Wen401e67e2011-02-28 10:32:14 +08001029 if (info->is_ready)
Lei Wen401e67e2011-02-28 10:32:14 +08001030 return 0;
Lei Wend4568822011-07-14 20:44:32 -07001031
1032 return -ENODEV;
Lei Wen401e67e2011-02-28 10:32:14 +08001033}
eric miaofe69af02008-02-14 15:48:23 +08001034
Lei Wen401e67e2011-02-28 10:32:14 +08001035static int pxa3xx_nand_scan(struct mtd_info *mtd)
1036{
Lei Wend4568822011-07-14 20:44:32 -07001037 struct pxa3xx_nand_host *host = mtd->priv;
1038 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001039 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001040 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001041 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001042 const struct pxa3xx_nand_flash *f = NULL;
1043 struct nand_chip *chip = mtd->priv;
1044 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001045 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001046 int i, ret, num;
1047
1048 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001049 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001050
1051 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001052 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001053 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1054 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001055
Lei Wend4568822011-07-14 20:44:32 -07001056 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001057 }
1058
1059 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1060 id = *((uint16_t *)(info->data_buff));
1061 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001062 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001063 else {
Lei Wenda675b42011-07-14 20:44:31 -07001064 dev_warn(&info->pdev->dev,
1065 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001066
1067 return -EINVAL;
1068 }
1069
1070 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1071 for (i = 0; i < num; i++) {
1072 if (i < pdata->num_flash)
1073 f = pdata->flash + i;
1074 else
1075 f = &builtin_flash_types[i - pdata->num_flash + 1];
1076
1077 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001078 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001079 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001080 }
1081
Lei Wen4332c112011-03-03 11:27:01 +08001082 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001083 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001084
1085 return -EINVAL;
1086 }
1087
Lei Wend4568822011-07-14 20:44:32 -07001088 ret = pxa3xx_nand_config_flash(info, f);
1089 if (ret) {
1090 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1091 return ret;
1092 }
1093
Lei Wen4332c112011-03-03 11:27:01 +08001094 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001095 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001096 pxa3xx_flash_ids[0].pagesize = f->page_size;
1097 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1098 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1099 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1100 if (f->flash_width == 16)
1101 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001102 pxa3xx_flash_ids[1].name = NULL;
1103 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001104KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001105 chip->ecc.mode = NAND_ECC_HW;
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -03001106 chip->ecc.size = info->fifo_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001107 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001108
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001109 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001110 chip->options |= NAND_BUSWIDTH_16;
1111
Lei Wen0fab0282011-06-07 03:01:06 -07001112 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001113 return -ENODEV;
1114 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001115 if (mtd->writesize >= 2048)
1116 host->col_addr_cycles = 2;
1117 else
1118 host->col_addr_cycles = 1;
1119
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001120 /* release the initial buffer */
1121 kfree(info->data_buff);
1122
1123 /* allocate the real data + oob buffer */
1124 info->buf_size = mtd->writesize + mtd->oobsize;
1125 ret = pxa3xx_nand_init_buff(info);
1126 if (ret)
1127 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001128 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001129
Lei Wen4332c112011-03-03 11:27:01 +08001130 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001131 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001132 else
Lei Wend4568822011-07-14 20:44:32 -07001133 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001134 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001135}
1136
Lei Wend4568822011-07-14 20:44:32 -07001137static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001138{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001139 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001140 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001141 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001142 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001143 struct mtd_info *mtd;
1144 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001145 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001146
Jingoo Han453810b2013-07-30 17:18:33 +09001147 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001148 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1149 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1150 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001151 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001152
eric miaofe69af02008-02-14 15:48:23 +08001153 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001154 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001155 for (cs = 0; cs < pdata->num_cs; cs++) {
1156 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1157 (sizeof(*mtd) + sizeof(*host)) * cs);
1158 chip = (struct nand_chip *)(&mtd[1]);
1159 host = (struct pxa3xx_nand_host *)chip;
1160 info->host[cs] = host;
1161 host->mtd = mtd;
1162 host->cs = cs;
1163 host->info_data = info;
1164 mtd->priv = host;
1165 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001166
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001167 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1168 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1169 chip->controller = &info->controller;
1170 chip->waitfunc = pxa3xx_nand_waitfunc;
1171 chip->select_chip = pxa3xx_nand_select_chip;
1172 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1173 chip->read_word = pxa3xx_nand_read_word;
1174 chip->read_byte = pxa3xx_nand_read_byte;
1175 chip->read_buf = pxa3xx_nand_read_buf;
1176 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001177 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001178 }
Lei Wen401e67e2011-02-28 10:32:14 +08001179
1180 spin_lock_init(&chip->controller->lock);
1181 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001182 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001183 if (IS_ERR(info->clk)) {
1184 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001185 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001186 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001187 ret = clk_prepare_enable(info->clk);
1188 if (ret < 0)
1189 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001190
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001191 if (use_dma) {
1192 /*
1193 * This is a dirty hack to make this driver work from
1194 * devicetree bindings. It can be removed once we have
1195 * a prober DMA controller framework for DT.
1196 */
1197 if (pdev->dev.of_node &&
1198 of_machine_is_compatible("marvell,pxa3xx")) {
1199 info->drcmr_dat = 97;
1200 info->drcmr_cmd = 99;
1201 } else {
1202 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1203 if (r == NULL) {
1204 dev_err(&pdev->dev,
1205 "no resource defined for data DMA\n");
1206 ret = -ENXIO;
1207 goto fail_disable_clk;
1208 }
1209 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001210
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001211 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1212 if (r == NULL) {
1213 dev_err(&pdev->dev,
1214 "no resource defined for cmd DMA\n");
1215 ret = -ENXIO;
1216 goto fail_disable_clk;
1217 }
1218 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001219 }
eric miaofe69af02008-02-14 15:48:23 +08001220 }
eric miaofe69af02008-02-14 15:48:23 +08001221
1222 irq = platform_get_irq(pdev, 0);
1223 if (irq < 0) {
1224 dev_err(&pdev->dev, "no IRQ resource defined\n");
1225 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001226 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001227 }
1228
1229 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001230 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1231 if (IS_ERR(info->mmio_base)) {
1232 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001233 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001234 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001235 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001236
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001237 /* Allocate a buffer to allow flash detection */
1238 info->buf_size = INIT_BUFFER_SIZE;
1239 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1240 if (info->data_buff == NULL) {
1241 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001242 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001243 }
eric miaofe69af02008-02-14 15:48:23 +08001244
Haojian Zhuang346e1252009-09-10 14:27:23 +08001245 /* initialize all interrupts to be disabled */
1246 disable_int(info, NDSR_MASK);
1247
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001248 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001249 if (ret < 0) {
1250 dev_err(&pdev->dev, "failed to request IRQ\n");
1251 goto fail_free_buf;
1252 }
1253
Lei Wene353a202011-03-03 11:08:30 +08001254 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001255
Lei Wend4568822011-07-14 20:44:32 -07001256 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001257
eric miaofe69af02008-02-14 15:48:23 +08001258fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001259 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001260 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001261fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001262 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001263 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001264}
1265
1266static int pxa3xx_nand_remove(struct platform_device *pdev)
1267{
Lei Wene353a202011-03-03 11:08:30 +08001268 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001269 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001270 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001271
Lei Wend4568822011-07-14 20:44:32 -07001272 if (!info)
1273 return 0;
1274
Jingoo Han453810b2013-07-30 17:18:33 +09001275 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001276
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001277 irq = platform_get_irq(pdev, 0);
1278 if (irq >= 0)
1279 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001280 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001281
Ezequiel Garciafb320612013-04-17 13:38:12 -03001282 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001283
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001284 for (cs = 0; cs < pdata->num_cs; cs++)
1285 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001286 return 0;
1287}
1288
Daniel Mack1e7ba632012-07-22 19:51:02 +02001289static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1290{
1291 struct pxa3xx_nand_platform_data *pdata;
1292 struct device_node *np = pdev->dev.of_node;
1293 const struct of_device_id *of_id =
1294 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1295
1296 if (!of_id)
1297 return 0;
1298
1299 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1300 if (!pdata)
1301 return -ENOMEM;
1302
1303 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1304 pdata->enable_arbiter = 1;
1305 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1306 pdata->keep_config = 1;
1307 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1308
1309 pdev->dev.platform_data = pdata;
1310
1311 return 0;
1312}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001313
Lei Wene353a202011-03-03 11:08:30 +08001314static int pxa3xx_nand_probe(struct platform_device *pdev)
1315{
1316 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001317 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001318 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001319 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001320
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001321#ifndef ARCH_HAS_DMA
1322 if (use_dma) {
1323 use_dma = 0;
1324 dev_warn(&pdev->dev,
1325 "This platform can't do DMA on this device\n");
1326 }
1327#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001328 ret = pxa3xx_nand_probe_dt(pdev);
1329 if (ret)
1330 return ret;
1331
Jingoo Han453810b2013-07-30 17:18:33 +09001332 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001333 if (!pdata) {
1334 dev_err(&pdev->dev, "no platform data defined\n");
1335 return -ENODEV;
1336 }
1337
Lei Wend4568822011-07-14 20:44:32 -07001338 ret = alloc_nand_resource(pdev);
1339 if (ret) {
1340 dev_err(&pdev->dev, "alloc nand resource failed\n");
1341 return ret;
1342 }
Lei Wene353a202011-03-03 11:08:30 +08001343
Lei Wend4568822011-07-14 20:44:32 -07001344 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001345 probe_success = 0;
1346 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001347 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001348
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001349 /*
1350 * The mtd name matches the one used in 'mtdparts' kernel
1351 * parameter. This name cannot be changed or otherwise
1352 * user's mtd partitions configuration would get broken.
1353 */
1354 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001355 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001356 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001357 if (ret) {
1358 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1359 cs);
1360 continue;
1361 }
1362
Daniel Mack1e7ba632012-07-22 19:51:02 +02001363 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001364 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001365 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001366 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001367 if (!ret)
1368 probe_success = 1;
1369 }
1370
1371 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001372 pxa3xx_nand_remove(pdev);
1373 return -ENODEV;
1374 }
1375
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001376 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001377}
1378
eric miaofe69af02008-02-14 15:48:23 +08001379#ifdef CONFIG_PM
1380static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1381{
Lei Wene353a202011-03-03 11:08:30 +08001382 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001383 struct pxa3xx_nand_platform_data *pdata;
1384 struct mtd_info *mtd;
1385 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001386
Jingoo Han453810b2013-07-30 17:18:33 +09001387 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001388 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001389 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1390 return -EAGAIN;
1391 }
1392
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001393 for (cs = 0; cs < pdata->num_cs; cs++) {
1394 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001395 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001396 }
1397
eric miaofe69af02008-02-14 15:48:23 +08001398 return 0;
1399}
1400
1401static int pxa3xx_nand_resume(struct platform_device *pdev)
1402{
Lei Wene353a202011-03-03 11:08:30 +08001403 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001404 struct pxa3xx_nand_platform_data *pdata;
1405 struct mtd_info *mtd;
1406 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001407
Jingoo Han453810b2013-07-30 17:18:33 +09001408 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001409 /* We don't want to handle interrupt without calling mtd routine */
1410 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001411
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001412 /*
1413 * Directly set the chip select to a invalid value,
1414 * then the driver would reset the timing according
1415 * to current chip select at the beginning of cmdfunc
1416 */
1417 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001418
Lei Wen051fc412011-07-14 20:44:30 -07001419 /*
1420 * As the spec says, the NDSR would be updated to 0x1800 when
1421 * doing the nand_clk disable/enable.
1422 * To prevent it damaging state machine of the driver, clear
1423 * all status before resume
1424 */
1425 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001426 for (cs = 0; cs < pdata->num_cs; cs++) {
1427 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001428 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001429 }
1430
Lei Wen18c81b12010-08-17 17:25:57 +08001431 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001432}
1433#else
1434#define pxa3xx_nand_suspend NULL
1435#define pxa3xx_nand_resume NULL
1436#endif
1437
1438static struct platform_driver pxa3xx_nand_driver = {
1439 .driver = {
1440 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301441 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001442 },
1443 .probe = pxa3xx_nand_probe,
1444 .remove = pxa3xx_nand_remove,
1445 .suspend = pxa3xx_nand_suspend,
1446 .resume = pxa3xx_nand_resume,
1447};
1448
Axel Linf99640d2011-11-27 20:45:03 +08001449module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001450
1451MODULE_LICENSE("GPL");
1452MODULE_DESCRIPTION("PXA3xx NAND controller driver");