blob: 9828033721852f0f741b7c36894dc3026eb90d80 [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>
Ezequiel Garcia776f2652013-11-14 18:25:28 -030029#include <linux/of_mtd.h>
eric miaofe69af02008-02-14 15:48:23 +080030
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030031#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
32#define ARCH_HAS_DMA
33#endif
34
35#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080036#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030037#endif
38
Arnd Bergmann293b2da2012-08-24 15:16:48 +020039#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080040
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -030041#define NAND_DEV_READY_TIMEOUT 50
eric miaofe69af02008-02-14 15:48:23 +080042#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080043#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080044#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080045
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030046/*
47 * Define a buffer size for the initial command that detects the flash device:
48 * STATUS, READID and PARAM. The largest of these is the PARAM command,
49 * needing 256 bytes.
50 */
51#define INIT_BUFFER_SIZE 256
52
eric miaofe69af02008-02-14 15:48:23 +080053/* registers and bit definitions */
54#define NDCR (0x00) /* Control register */
55#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
56#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
57#define NDSR (0x14) /* Status Register */
58#define NDPCR (0x18) /* Page Count Register */
59#define NDBDR0 (0x1C) /* Bad Block Register 0 */
60#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030061#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080062#define NDDB (0x40) /* Data Buffer */
63#define NDCB0 (0x48) /* Command Buffer0 */
64#define NDCB1 (0x4C) /* Command Buffer1 */
65#define NDCB2 (0x50) /* Command Buffer2 */
66
67#define NDCR_SPARE_EN (0x1 << 31)
68#define NDCR_ECC_EN (0x1 << 30)
69#define NDCR_DMA_EN (0x1 << 29)
70#define NDCR_ND_RUN (0x1 << 28)
71#define NDCR_DWIDTH_C (0x1 << 27)
72#define NDCR_DWIDTH_M (0x1 << 26)
73#define NDCR_PAGE_SZ (0x1 << 24)
74#define NDCR_NCSX (0x1 << 23)
75#define NDCR_ND_MODE (0x3 << 21)
76#define NDCR_NAND_MODE (0x0)
77#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080078#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080079#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
80#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
81
82#define NDCR_RA_START (0x1 << 15)
83#define NDCR_PG_PER_BLK (0x1 << 14)
84#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080085#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080086
87#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030088#define NDSR_ERR_CNT_OFF (16)
89#define NDSR_ERR_CNT_MASK (0x1f)
90#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080091#define NDSR_RDY (0x1 << 12)
92#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080093#define NDSR_CS0_PAGED (0x1 << 10)
94#define NDSR_CS1_PAGED (0x1 << 9)
95#define NDSR_CS0_CMDD (0x1 << 8)
96#define NDSR_CS1_CMDD (0x1 << 7)
97#define NDSR_CS0_BBD (0x1 << 6)
98#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030099#define NDSR_UNCORERR (0x1 << 4)
100#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +0800101#define NDSR_WRDREQ (0x1 << 2)
102#define NDSR_RDDREQ (0x1 << 1)
103#define NDSR_WRCMDREQ (0x1)
104
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300105#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800106#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800107#define NDCB0_AUTO_RS (0x1 << 25)
108#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300109#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
110#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800111#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
112#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
113#define NDCB0_NC (0x1 << 20)
114#define NDCB0_DBC (0x1 << 19)
115#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
116#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
117#define NDCB0_CMD2_MASK (0xff << 8)
118#define NDCB0_CMD1_MASK (0xff)
119#define NDCB0_ADDR_CYC_SHIFT (16)
120
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300121#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
122#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
123#define EXT_CMD_TYPE_READ 4 /* Read */
124#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
125#define EXT_CMD_TYPE_FINAL 3 /* Final command */
126#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
127#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
128
eric miaofe69af02008-02-14 15:48:23 +0800129/* macros for registers read/write */
130#define nand_writel(info, off, val) \
131 __raw_writel((val), (info)->mmio_base + (off))
132
133#define nand_readl(info, off) \
134 __raw_readl((info)->mmio_base + (off))
135
136/* error code and state */
137enum {
138 ERR_NONE = 0,
139 ERR_DMABUSERR = -1,
140 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300141 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800142 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300143 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800144};
145
146enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800147 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700148 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800149 STATE_CMD_HANDLE,
150 STATE_DMA_READING,
151 STATE_DMA_WRITING,
152 STATE_DMA_DONE,
153 STATE_PIO_READING,
154 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800155 STATE_CMD_DONE,
156 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800157};
158
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300159enum pxa3xx_nand_variant {
160 PXA3XX_NAND_VARIANT_PXA,
161 PXA3XX_NAND_VARIANT_ARMADA370,
162};
163
Lei Wend4568822011-07-14 20:44:32 -0700164struct pxa3xx_nand_host {
165 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700166 struct mtd_info *mtd;
167 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800168
Lei Wend4568822011-07-14 20:44:32 -0700169 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700170 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700171 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700172
173 /* calculated from pxa3xx_nand_flash data */
174 unsigned int col_addr_cycles;
175 unsigned int row_addr_cycles;
176 size_t read_id_bytes;
177
Lei Wend4568822011-07-14 20:44:32 -0700178};
179
180struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800181 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800182 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800183
184 struct clk *clk;
185 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800186 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300187 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800188
189 unsigned int buf_start;
190 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300191 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300192 unsigned int data_buff_pos;
193 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800194
195 /* DMA information */
196 int drcmr_dat;
197 int drcmr_cmd;
198
199 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800200 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800201 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800202 int data_dma_ch;
203 struct pxa_dma_desc *data_desc;
204 dma_addr_t data_desc_addr;
205
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700206 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800207 unsigned int state;
208
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300209 /*
210 * This driver supports NFCv1 (as found in PXA SoC)
211 * and NFCv2 (as found in Armada 370/XP SoC).
212 */
213 enum pxa3xx_nand_variant variant;
214
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700215 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800216 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300217 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800218 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300219 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300220 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800221
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300222 unsigned int data_size; /* data to be read from FIFO */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300223 unsigned int chunk_size; /* split commands chunk size */
Lei Wend4568822011-07-14 20:44:32 -0700224 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300225 unsigned int spare_size;
226 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300227 unsigned int ecc_err_cnt;
228 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800229 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800230
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300231 /* cached register value */
232 uint32_t reg_ndcr;
233 uint32_t ndtr0cs0;
234 uint32_t ndtr1cs0;
235
eric miaofe69af02008-02-14 15:48:23 +0800236 /* generated NDCBx register values */
237 uint32_t ndcb0;
238 uint32_t ndcb1;
239 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300240 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800241};
242
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030243static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800244module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300245MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800246
Lei Wenc1f82472010-08-17 13:50:23 +0800247static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800248 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
249 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
250 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
251 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800252};
253
Lei Wenc1f82472010-08-17 13:50:23 +0800254static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800255{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
256{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
257{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
258{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
259{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
260{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
261{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
262{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
263{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800264};
265
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300266static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
267static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
268
269static struct nand_bbt_descr bbt_main_descr = {
270 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
271 | NAND_BBT_2BIT | NAND_BBT_VERSION,
272 .offs = 8,
273 .len = 6,
274 .veroffs = 14,
275 .maxblocks = 8, /* Last 8 blocks in each chip */
276 .pattern = bbt_pattern
277};
278
279static struct nand_bbt_descr bbt_mirror_descr = {
280 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
281 | NAND_BBT_2BIT | NAND_BBT_VERSION,
282 .offs = 8,
283 .len = 6,
284 .veroffs = 14,
285 .maxblocks = 8, /* Last 8 blocks in each chip */
286 .pattern = bbt_mirror_pattern
287};
288
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300289static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
290 .eccbytes = 64,
291 .eccpos = {
292 32, 33, 34, 35, 36, 37, 38, 39,
293 40, 41, 42, 43, 44, 45, 46, 47,
294 48, 49, 50, 51, 52, 53, 54, 55,
295 56, 57, 58, 59, 60, 61, 62, 63,
296 96, 97, 98, 99, 100, 101, 102, 103,
297 104, 105, 106, 107, 108, 109, 110, 111,
298 112, 113, 114, 115, 116, 117, 118, 119,
299 120, 121, 122, 123, 124, 125, 126, 127},
300 /* Bootrom looks in bytes 0 & 5 for bad blocks */
301 .oobfree = { {6, 26}, { 64, 32} }
302};
303
304static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
305 .eccbytes = 128,
306 .eccpos = {
307 32, 33, 34, 35, 36, 37, 38, 39,
308 40, 41, 42, 43, 44, 45, 46, 47,
309 48, 49, 50, 51, 52, 53, 54, 55,
310 56, 57, 58, 59, 60, 61, 62, 63},
311 .oobfree = { }
312};
313
Lei Wen227a8862010-08-18 18:00:03 +0800314/* Define a default flash type setting serve as flash detecting only */
315#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
316
eric miaofe69af02008-02-14 15:48:23 +0800317#define NDTR0_tCH(c) (min((c), 7) << 19)
318#define NDTR0_tCS(c) (min((c), 7) << 16)
319#define NDTR0_tWH(c) (min((c), 7) << 11)
320#define NDTR0_tWP(c) (min((c), 7) << 8)
321#define NDTR0_tRH(c) (min((c), 7) << 3)
322#define NDTR0_tRP(c) (min((c), 7) << 0)
323
324#define NDTR1_tR(c) (min((c), 65535) << 16)
325#define NDTR1_tWHR(c) (min((c), 15) << 4)
326#define NDTR1_tAR(c) (min((c), 15) << 0)
327
328/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800329#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800330
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300331static struct of_device_id pxa3xx_nand_dt_ids[] = {
332 {
333 .compatible = "marvell,pxa3xx-nand",
334 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
335 },
336 {}
337};
338MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
339
340static enum pxa3xx_nand_variant
341pxa3xx_nand_get_variant(struct platform_device *pdev)
342{
343 const struct of_device_id *of_id =
344 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
345 if (!of_id)
346 return PXA3XX_NAND_VARIANT_PXA;
347 return (enum pxa3xx_nand_variant)of_id->data;
348}
349
Lei Wend4568822011-07-14 20:44:32 -0700350static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200351 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800352{
Lei Wend4568822011-07-14 20:44:32 -0700353 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800354 unsigned long nand_clk = clk_get_rate(info->clk);
355 uint32_t ndtr0, ndtr1;
356
357 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
358 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
359 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
360 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
361 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
362 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
363
364 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
365 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
366 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
367
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300368 info->ndtr0cs0 = ndtr0;
369 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800370 nand_writel(info, NDTR0CS0, ndtr0);
371 nand_writel(info, NDTR1CS0, ndtr1);
372}
373
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300374/*
375 * Set the data and OOB size, depending on the selected
376 * spare and ECC configuration.
377 * Only applicable to READ0, READOOB and PAGEPROG commands.
378 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300379static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
380 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800381{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300382 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800383
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300384 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300385 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800386 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800387
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300388 info->oob_size = info->spare_size;
389 if (!info->use_ecc)
390 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800391}
392
Lei Wenf8155a42011-02-28 10:32:11 +0800393/**
394 * NOTE: it is a must to set ND_RUN firstly, then write
395 * command buffer, otherwise, it does not work.
396 * We enable all the interrupt at the same time, and
397 * let pxa3xx_nand_irq to handle all logic.
398 */
399static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
400{
401 uint32_t ndcr;
402
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300403 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300404
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300405 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300406 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300407 if (info->ecc_bch)
408 nand_writel(info, NDECCCTRL, 0x1);
409 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300410 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300411 if (info->ecc_bch)
412 nand_writel(info, NDECCCTRL, 0x0);
413 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300414
415 if (info->use_dma)
416 ndcr |= NDCR_DMA_EN;
417 else
418 ndcr &= ~NDCR_DMA_EN;
419
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300420 if (info->use_spare)
421 ndcr |= NDCR_SPARE_EN;
422 else
423 ndcr &= ~NDCR_SPARE_EN;
424
Lei Wenf8155a42011-02-28 10:32:11 +0800425 ndcr |= NDCR_ND_RUN;
426
427 /* clear status bits and run */
428 nand_writel(info, NDCR, 0);
429 nand_writel(info, NDSR, NDSR_MASK);
430 nand_writel(info, NDCR, ndcr);
431}
432
433static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
434{
435 uint32_t ndcr;
436 int timeout = NAND_STOP_DELAY;
437
438 /* wait RUN bit in NDCR become 0 */
439 ndcr = nand_readl(info, NDCR);
440 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
441 ndcr = nand_readl(info, NDCR);
442 udelay(1);
443 }
444
445 if (timeout <= 0) {
446 ndcr &= ~NDCR_ND_RUN;
447 nand_writel(info, NDCR, ndcr);
448 }
449 /* clear status bits */
450 nand_writel(info, NDSR, NDSR_MASK);
451}
452
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300453static void __maybe_unused
454enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800455{
456 uint32_t ndcr;
457
458 ndcr = nand_readl(info, NDCR);
459 nand_writel(info, NDCR, ndcr & ~int_mask);
460}
461
462static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
463{
464 uint32_t ndcr;
465
466 ndcr = nand_readl(info, NDCR);
467 nand_writel(info, NDCR, ndcr | int_mask);
468}
469
Lei Wenf8155a42011-02-28 10:32:11 +0800470static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800471{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300472 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300473
eric miaofe69af02008-02-14 15:48:23 +0800474 switch (info->state) {
475 case STATE_PIO_WRITING:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300476 __raw_writesl(info->mmio_base + NDDB,
477 info->data_buff + info->data_buff_pos,
478 DIV_ROUND_UP(do_bytes, 4));
479
Lei Wen9d8b1042010-08-17 14:09:30 +0800480 if (info->oob_size > 0)
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300481 __raw_writesl(info->mmio_base + NDDB,
482 info->oob_buff + info->oob_buff_pos,
483 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800484 break;
485 case STATE_PIO_READING:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300486 __raw_readsl(info->mmio_base + NDDB,
487 info->data_buff + info->data_buff_pos,
488 DIV_ROUND_UP(do_bytes, 4));
489
Lei Wen9d8b1042010-08-17 14:09:30 +0800490 if (info->oob_size > 0)
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300491 __raw_readsl(info->mmio_base + NDDB,
492 info->oob_buff + info->oob_buff_pos,
493 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800494 break;
495 default:
Lei Wenda675b42011-07-14 20:44:31 -0700496 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800497 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800498 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800499 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300500
501 /* Update buffer pointers for multi-page read/write */
502 info->data_buff_pos += do_bytes;
503 info->oob_buff_pos += info->oob_size;
504 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800505}
506
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300507#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800508static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800509{
510 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800511 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800512
513 desc->ddadr = DDADR_STOP;
514 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
515
Lei Wenf8155a42011-02-28 10:32:11 +0800516 switch (info->state) {
517 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800518 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800519 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800520 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800521 break;
522 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800523 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800524 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800525 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800526 break;
527 default:
Lei Wenda675b42011-07-14 20:44:31 -0700528 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800529 info->state);
530 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800531 }
532
533 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
534 DDADR(info->data_dma_ch) = info->data_desc_addr;
535 DCSR(info->data_dma_ch) |= DCSR_RUN;
536}
537
538static void pxa3xx_nand_data_dma_irq(int channel, void *data)
539{
540 struct pxa3xx_nand_info *info = data;
541 uint32_t dcsr;
542
543 dcsr = DCSR(channel);
544 DCSR(channel) = dcsr;
545
546 if (dcsr & DCSR_BUSERR) {
547 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800548 }
549
Lei Wenf8155a42011-02-28 10:32:11 +0800550 info->state = STATE_DMA_DONE;
551 enable_int(info, NDCR_INT_MASK);
552 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800553}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300554#else
555static void start_data_dma(struct pxa3xx_nand_info *info)
556{}
557#endif
eric miaofe69af02008-02-14 15:48:23 +0800558
559static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
560{
561 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300562 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700563 unsigned int ready, cmd_done;
564
565 if (info->cs == 0) {
566 ready = NDSR_FLASH_RDY;
567 cmd_done = NDSR_CS0_CMDD;
568 } else {
569 ready = NDSR_RDY;
570 cmd_done = NDSR_CS1_CMDD;
571 }
eric miaofe69af02008-02-14 15:48:23 +0800572
573 status = nand_readl(info, NDSR);
574
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300575 if (status & NDSR_UNCORERR)
576 info->retcode = ERR_UNCORERR;
577 if (status & NDSR_CORERR) {
578 info->retcode = ERR_CORERR;
579 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
580 info->ecc_bch)
581 info->ecc_err_cnt = NDSR_ERR_CNT(status);
582 else
583 info->ecc_err_cnt = 1;
584
585 /*
586 * Each chunk composing a page is corrected independently,
587 * and we need to store maximum number of corrected bitflips
588 * to return it to the MTD layer in ecc.read_page().
589 */
590 info->max_bitflips = max_t(unsigned int,
591 info->max_bitflips,
592 info->ecc_err_cnt);
593 }
Lei Wenf8155a42011-02-28 10:32:11 +0800594 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
595 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800596 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800597 disable_int(info, NDCR_INT_MASK);
598 info->state = (status & NDSR_RDDREQ) ?
599 STATE_DMA_READING : STATE_DMA_WRITING;
600 start_data_dma(info);
601 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800602 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800603 info->state = (status & NDSR_RDDREQ) ?
604 STATE_PIO_READING : STATE_PIO_WRITING;
605 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800606 }
Lei Wenf8155a42011-02-28 10:32:11 +0800607 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700608 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800609 info->state = STATE_CMD_DONE;
610 is_completed = 1;
611 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700612 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800613 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300614 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800615 }
Lei Wenf8155a42011-02-28 10:32:11 +0800616
617 if (status & NDSR_WRCMDREQ) {
618 nand_writel(info, NDSR, NDSR_WRCMDREQ);
619 status &= ~NDSR_WRCMDREQ;
620 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300621
622 /*
623 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
624 * must be loaded by writing directly either 12 or 16
625 * bytes directly to NDCB0, four bytes at a time.
626 *
627 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
628 * but each NDCBx register can be read.
629 */
Lei Wenf8155a42011-02-28 10:32:11 +0800630 nand_writel(info, NDCB0, info->ndcb0);
631 nand_writel(info, NDCB0, info->ndcb1);
632 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300633
634 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
635 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
636 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800637 }
Lei Wenf8155a42011-02-28 10:32:11 +0800638
639 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800640 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800641 if (is_completed)
642 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300643 if (is_ready)
644 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800645NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800646 return IRQ_HANDLED;
647}
648
eric miaofe69af02008-02-14 15:48:23 +0800649static inline int is_buf_blank(uint8_t *buf, size_t len)
650{
651 for (; len > 0; len--)
652 if (*buf++ != 0xff)
653 return 0;
654 return 1;
655}
656
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300657static void set_command_address(struct pxa3xx_nand_info *info,
658 unsigned int page_size, uint16_t column, int page_addr)
659{
660 /* small page addr setting */
661 if (page_size < PAGE_CHUNK_SIZE) {
662 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
663 | (column & 0xFF);
664
665 info->ndcb2 = 0;
666 } else {
667 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
668 | (column & 0xFFFF);
669
670 if (page_addr & 0xFF0000)
671 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
672 else
673 info->ndcb2 = 0;
674 }
675}
676
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300677static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800678{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300679 struct pxa3xx_nand_host *host = info->host[info->cs];
680 struct mtd_info *mtd = host->mtd;
681
Lei Wen4eb2da82011-02-28 10:32:13 +0800682 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800683 info->buf_start = 0;
684 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800685 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300686 info->data_buff_pos = 0;
687 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800688 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300689 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800690 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300691 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300692 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300693 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800694
695 switch (command) {
696 case NAND_CMD_READ0:
697 case NAND_CMD_PAGEPROG:
698 info->use_ecc = 1;
699 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300700 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800701 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300702 case NAND_CMD_PARAM:
703 info->use_spare = 0;
704 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800705 default:
706 info->ndcb1 = 0;
707 info->ndcb2 = 0;
708 break;
709 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300710
711 /*
712 * If we are about to issue a read command, or about to set
713 * the write address, then clean the data buffer.
714 */
715 if (command == NAND_CMD_READ0 ||
716 command == NAND_CMD_READOOB ||
717 command == NAND_CMD_SEQIN) {
718
719 info->buf_count = mtd->writesize + mtd->oobsize;
720 memset(info->data_buff, 0xFF, info->buf_count);
721 }
722
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300723}
724
725static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300726 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300727{
728 int addr_cycle, exec_cmd;
729 struct pxa3xx_nand_host *host;
730 struct mtd_info *mtd;
731
732 host = info->host[info->cs];
733 mtd = host->mtd;
734 addr_cycle = 0;
735 exec_cmd = 1;
736
737 if (info->cs != 0)
738 info->ndcb0 = NDCB0_CSEL;
739 else
740 info->ndcb0 = 0;
741
742 if (command == NAND_CMD_SEQIN)
743 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800744
Lei Wend4568822011-07-14 20:44:32 -0700745 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
746 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800747
748 switch (command) {
749 case NAND_CMD_READOOB:
750 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300751 info->buf_start = column;
752 info->ndcb0 |= NDCB0_CMD_TYPE(0)
753 | addr_cycle
754 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800755
Ezequiel Garciaec821352013-08-12 14:14:54 -0300756 if (command == NAND_CMD_READOOB)
757 info->buf_start += mtd->writesize;
758
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300759 /*
760 * Multiple page read needs an 'extended command type' field,
761 * which is either naked-read or last-read according to the
762 * state.
763 */
764 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300765 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300766 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
767 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
768 | NDCB0_LEN_OVRD
769 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
770 info->ndcb3 = info->chunk_size +
771 info->oob_size;
772 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800773
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300774 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300775 break;
776
Lei Wen4eb2da82011-02-28 10:32:13 +0800777 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800778
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300779 info->buf_start = column;
780 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300781
782 /*
783 * Multiple page programming needs to execute the initial
784 * SEQIN command that sets the page address.
785 */
786 if (mtd->writesize > PAGE_CHUNK_SIZE) {
787 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
788 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
789 | addr_cycle
790 | command;
791 /* No data transfer in this case */
792 info->data_size = 0;
793 exec_cmd = 1;
794 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800795 break;
796
797 case NAND_CMD_PAGEPROG:
798 if (is_buf_blank(info->data_buff,
799 (mtd->writesize + mtd->oobsize))) {
800 exec_cmd = 0;
801 break;
802 }
803
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300804 /* Second command setting for large pages */
805 if (mtd->writesize > PAGE_CHUNK_SIZE) {
806 /*
807 * Multiple page write uses the 'extended command'
808 * field. This can be used to issue a command dispatch
809 * or a naked-write depending on the current stage.
810 */
811 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
812 | NDCB0_LEN_OVRD
813 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
814 info->ndcb3 = info->chunk_size +
815 info->oob_size;
816
817 /*
818 * This is the command dispatch that completes a chunked
819 * page program operation.
820 */
821 if (info->data_size == 0) {
822 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
823 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
824 | command;
825 info->ndcb1 = 0;
826 info->ndcb2 = 0;
827 info->ndcb3 = 0;
828 }
829 } else {
830 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
831 | NDCB0_AUTO_RS
832 | NDCB0_ST_ROW_EN
833 | NDCB0_DBC
834 | (NAND_CMD_PAGEPROG << 8)
835 | NAND_CMD_SEQIN
836 | addr_cycle;
837 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800838 break;
839
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300840 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300841 info->buf_count = 256;
842 info->ndcb0 |= NDCB0_CMD_TYPE(0)
843 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300844 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300845 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300846 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300847 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300848 info->data_size = 256;
849 break;
850
Lei Wen4eb2da82011-02-28 10:32:13 +0800851 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700852 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800853 info->ndcb0 |= NDCB0_CMD_TYPE(3)
854 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300855 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300856 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800857
858 info->data_size = 8;
859 break;
860 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800861 info->buf_count = 1;
862 info->ndcb0 |= NDCB0_CMD_TYPE(4)
863 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300864 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800865
866 info->data_size = 8;
867 break;
868
869 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800870 info->ndcb0 |= NDCB0_CMD_TYPE(2)
871 | NDCB0_AUTO_RS
872 | NDCB0_ADDR_CYC(3)
873 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300874 | (NAND_CMD_ERASE2 << 8)
875 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800876 info->ndcb1 = page_addr;
877 info->ndcb2 = 0;
878
879 break;
880 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800881 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300882 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800883
884 break;
885
886 case NAND_CMD_ERASE2:
887 exec_cmd = 0;
888 break;
889
890 default:
891 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700892 dev_err(&info->pdev->dev, "non-supported command %x\n",
893 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800894 break;
895 }
896
897 return exec_cmd;
898}
899
eric miaofe69af02008-02-14 15:48:23 +0800900static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100901 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800902{
Lei Wend4568822011-07-14 20:44:32 -0700903 struct pxa3xx_nand_host *host = mtd->priv;
904 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800905 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800906
Lei Wen4eb2da82011-02-28 10:32:13 +0800907 /*
908 * if this is a x16 device ,then convert the input
909 * "byte" address into a "word" address appropriate
910 * for indexing a word-oriented device
911 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300912 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800913 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800914
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700915 /*
916 * There may be different NAND chip hooked to
917 * different chip select, so check whether
918 * chip select has been changed, if yes, reset the timing
919 */
920 if (info->cs != host->cs) {
921 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300922 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
923 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700924 }
925
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300926 prepare_start_command(info, command);
927
Lei Wend4568822011-07-14 20:44:32 -0700928 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300929 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
930
Lei Wenf8155a42011-02-28 10:32:11 +0800931 if (exec_cmd) {
932 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300933 init_completion(&info->dev_ready);
934 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800935 pxa3xx_nand_start(info);
936
937 ret = wait_for_completion_timeout(&info->cmd_complete,
938 CHIP_DELAY_TIMEOUT);
939 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700940 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800941 /* Stop State Machine for next command cycle */
942 pxa3xx_nand_stop(info);
943 }
eric miaofe69af02008-02-14 15:48:23 +0800944 }
Lei Wend4568822011-07-14 20:44:32 -0700945 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800946}
947
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300948static void armada370_nand_cmdfunc(struct mtd_info *mtd,
949 const unsigned command,
950 int column, int page_addr)
951{
952 struct pxa3xx_nand_host *host = mtd->priv;
953 struct pxa3xx_nand_info *info = host->info_data;
954 int ret, exec_cmd, ext_cmd_type;
955
956 /*
957 * if this is a x16 device then convert the input
958 * "byte" address into a "word" address appropriate
959 * for indexing a word-oriented device
960 */
961 if (info->reg_ndcr & NDCR_DWIDTH_M)
962 column /= 2;
963
964 /*
965 * There may be different NAND chip hooked to
966 * different chip select, so check whether
967 * chip select has been changed, if yes, reset the timing
968 */
969 if (info->cs != host->cs) {
970 info->cs = host->cs;
971 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
972 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
973 }
974
975 /* Select the extended command for the first command */
976 switch (command) {
977 case NAND_CMD_READ0:
978 case NAND_CMD_READOOB:
979 ext_cmd_type = EXT_CMD_TYPE_MONO;
980 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300981 case NAND_CMD_SEQIN:
982 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
983 break;
984 case NAND_CMD_PAGEPROG:
985 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
986 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300987 default:
988 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300989 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300990 }
991
992 prepare_start_command(info, command);
993
994 /*
995 * Prepare the "is ready" completion before starting a command
996 * transaction sequence. If the command is not executed the
997 * completion will be completed, see below.
998 *
999 * We can do that inside the loop because the command variable
1000 * is invariant and thus so is the exec_cmd.
1001 */
1002 info->need_wait = 1;
1003 init_completion(&info->dev_ready);
1004 do {
1005 info->state = STATE_PREPARED;
1006 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1007 column, page_addr);
1008 if (!exec_cmd) {
1009 info->need_wait = 0;
1010 complete(&info->dev_ready);
1011 break;
1012 }
1013
1014 init_completion(&info->cmd_complete);
1015 pxa3xx_nand_start(info);
1016
1017 ret = wait_for_completion_timeout(&info->cmd_complete,
1018 CHIP_DELAY_TIMEOUT);
1019 if (!ret) {
1020 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1021 /* Stop State Machine for next command cycle */
1022 pxa3xx_nand_stop(info);
1023 break;
1024 }
1025
1026 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001027 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1028 break;
1029
1030 /*
1031 * After a splitted program command sequence has issued
1032 * the command dispatch, the command sequence is complete.
1033 */
1034 if (info->data_size == 0 &&
1035 command == NAND_CMD_PAGEPROG &&
1036 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001037 break;
1038
1039 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1040 /* Last read: issue a 'last naked read' */
1041 if (info->data_size == info->chunk_size)
1042 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1043 else
1044 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001045
1046 /*
1047 * If a splitted program command has no more data to transfer,
1048 * the command dispatch must be issued to complete.
1049 */
1050 } else if (command == NAND_CMD_PAGEPROG &&
1051 info->data_size == 0) {
1052 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001053 }
1054 } while (1);
1055
1056 info->state = STATE_IDLE;
1057}
1058
Josh Wufdbad98d2012-06-25 18:07:45 +08001059static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001060 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001061{
1062 chip->write_buf(mtd, buf, mtd->writesize);
1063 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001064
1065 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001066}
1067
1068static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001069 struct nand_chip *chip, uint8_t *buf, int oob_required,
1070 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001071{
Lei Wend4568822011-07-14 20:44:32 -07001072 struct pxa3xx_nand_host *host = mtd->priv;
1073 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001074
1075 chip->read_buf(mtd, buf, mtd->writesize);
1076 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1077
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001078 if (info->retcode == ERR_CORERR && info->use_ecc) {
1079 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1080
1081 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001082 /*
1083 * for blank page (all 0xff), HW will calculate its ECC as
1084 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001085 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001086 */
1087 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001088 info->retcode = ERR_NONE;
1089 else
Lei Wenf8155a42011-02-28 10:32:11 +08001090 mtd->ecc_stats.failed++;
1091 }
1092
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001093 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001094}
1095
eric miaofe69af02008-02-14 15:48:23 +08001096static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1097{
Lei Wend4568822011-07-14 20:44:32 -07001098 struct pxa3xx_nand_host *host = mtd->priv;
1099 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001100 char retval = 0xFF;
1101
1102 if (info->buf_start < info->buf_count)
1103 /* Has just send a new command? */
1104 retval = info->data_buff[info->buf_start++];
1105
1106 return retval;
1107}
1108
1109static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1110{
Lei Wend4568822011-07-14 20:44:32 -07001111 struct pxa3xx_nand_host *host = mtd->priv;
1112 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001113 u16 retval = 0xFFFF;
1114
1115 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1116 retval = *((u16 *)(info->data_buff+info->buf_start));
1117 info->buf_start += 2;
1118 }
1119 return retval;
1120}
1121
1122static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1123{
Lei Wend4568822011-07-14 20:44:32 -07001124 struct pxa3xx_nand_host *host = mtd->priv;
1125 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001126 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1127
1128 memcpy(buf, info->data_buff + info->buf_start, real_len);
1129 info->buf_start += real_len;
1130}
1131
1132static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1133 const uint8_t *buf, int len)
1134{
Lei Wend4568822011-07-14 20:44:32 -07001135 struct pxa3xx_nand_host *host = mtd->priv;
1136 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001137 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1138
1139 memcpy(info->data_buff + info->buf_start, buf, real_len);
1140 info->buf_start += real_len;
1141}
1142
eric miaofe69af02008-02-14 15:48:23 +08001143static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1144{
1145 return;
1146}
1147
1148static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1149{
Lei Wend4568822011-07-14 20:44:32 -07001150 struct pxa3xx_nand_host *host = mtd->priv;
1151 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001152 int ret;
1153
1154 if (info->need_wait) {
1155 ret = wait_for_completion_timeout(&info->dev_ready,
1156 CHIP_DELAY_TIMEOUT);
1157 info->need_wait = 0;
1158 if (!ret) {
1159 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1160 return NAND_STATUS_FAIL;
1161 }
1162 }
eric miaofe69af02008-02-14 15:48:23 +08001163
1164 /* pxa3xx_nand_send_command has waited for command complete */
1165 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1166 if (info->retcode == ERR_NONE)
1167 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001168 else
1169 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001170 }
1171
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001172 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001173}
1174
eric miaofe69af02008-02-14 15:48:23 +08001175static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001176 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001177{
1178 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001179 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001180 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001181 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001182
Lei Wenda675b42011-07-14 20:44:31 -07001183 if (f->page_size != 2048 && f->page_size != 512) {
1184 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001185 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001186 }
eric miaofe69af02008-02-14 15:48:23 +08001187
Lei Wenda675b42011-07-14 20:44:31 -07001188 if (f->flash_width != 16 && f->flash_width != 8) {
1189 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001190 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001191 }
eric miaofe69af02008-02-14 15:48:23 +08001192
1193 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -07001194 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +08001195
1196 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001197 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001198
1199 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001200 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001201 else
Lei Wend4568822011-07-14 20:44:32 -07001202 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001203
1204 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001205 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001206 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1207 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1208 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1209 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1210
Lei Wend4568822011-07-14 20:44:32 -07001211 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +08001212 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1213
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001214 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001215
Lei Wend4568822011-07-14 20:44:32 -07001216 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001217 return 0;
1218}
1219
Mike Rapoportf2710492009-02-17 13:54:47 +02001220static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1221{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001222 /*
1223 * We set 0 by hard coding here, for we don't support keep_config
1224 * when there is more than one chip attached to the controller
1225 */
1226 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +02001227 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001228
Lei Wend4568822011-07-14 20:44:32 -07001229 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -03001230 /* Controller's FIFO size */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001231 info->chunk_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -07001232 host->read_id_bytes = 4;
1233 } else {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001234 info->chunk_size = 512;
Lei Wend4568822011-07-14 20:44:32 -07001235 host->read_id_bytes = 2;
1236 }
1237
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001238 /* Set an initial chunk size */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001239 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1240 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1241 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001242 return 0;
1243}
1244
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001245#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +08001246static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1247{
1248 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001249 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001250
1251 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001252 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001253 if (info->data_buff == NULL)
1254 return -ENOMEM;
1255 return 0;
1256 }
1257
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001258 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001259 &info->data_buff_phys, GFP_KERNEL);
1260 if (info->data_buff == NULL) {
1261 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1262 return -ENOMEM;
1263 }
1264
eric miaofe69af02008-02-14 15:48:23 +08001265 info->data_desc = (void *)info->data_buff + data_desc_offset;
1266 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1267
1268 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1269 pxa3xx_nand_data_dma_irq, info);
1270 if (info->data_dma_ch < 0) {
1271 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001272 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001273 info->data_buff, info->data_buff_phys);
1274 return info->data_dma_ch;
1275 }
1276
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001277 /*
1278 * Now that DMA buffers are allocated we turn on
1279 * DMA proper for I/O operations.
1280 */
1281 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001282 return 0;
1283}
1284
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001285static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1286{
1287 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001288 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001289 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001290 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001291 info->data_buff, info->data_buff_phys);
1292 } else {
1293 kfree(info->data_buff);
1294 }
1295}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001296#else
1297static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1298{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001299 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001300 if (info->data_buff == NULL)
1301 return -ENOMEM;
1302 return 0;
1303}
1304
1305static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1306{
1307 kfree(info->data_buff);
1308}
1309#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001310
Lei Wen401e67e2011-02-28 10:32:14 +08001311static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001312{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001313 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001314 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001315 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001316
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001317 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001318 chip = mtd->priv;
1319
Lei Wen401e67e2011-02-28 10:32:14 +08001320 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001321 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1322 if (ret)
1323 return ret;
1324
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001325 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001326 ret = chip->waitfunc(mtd, chip);
1327 if (ret & NAND_STATUS_FAIL)
1328 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001329
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001330 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001331}
eric miaofe69af02008-02-14 15:48:23 +08001332
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001333static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1334 struct nand_ecc_ctrl *ecc,
1335 int strength, int page_size)
1336{
1337 /*
1338 * We don't use strength here as the PXA variant
1339 * is used with non-ONFI compliant devices.
1340 */
1341 if (page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001342 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001343 info->spare_size = 40;
1344 info->ecc_size = 24;
1345 ecc->mode = NAND_ECC_HW;
1346 ecc->size = 512;
1347 ecc->strength = 1;
1348 return 1;
1349
1350 } else if (page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001351 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001352 info->spare_size = 8;
1353 info->ecc_size = 8;
1354 ecc->mode = NAND_ECC_HW;
1355 ecc->size = 512;
1356 ecc->strength = 1;
1357 return 1;
1358 }
1359 return 0;
1360}
1361
1362static int armada370_ecc_init(struct pxa3xx_nand_info *info,
1363 struct nand_ecc_ctrl *ecc,
Brian Norris6033a942013-11-14 14:41:32 -08001364 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001365{
Brian Norris6033a942013-11-14 14:41:32 -08001366 /*
1367 * Required ECC: 4-bit correction per 512 bytes
1368 * Select: 16-bit correction per 2048 bytes
1369 */
1370 if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001371 info->ecc_bch = 1;
1372 info->chunk_size = 2048;
1373 info->spare_size = 32;
1374 info->ecc_size = 32;
1375 ecc->mode = NAND_ECC_HW;
1376 ecc->size = info->chunk_size;
1377 ecc->layout = &ecc_layout_4KB_bch4bit;
1378 ecc->strength = 16;
1379 return 1;
1380
Brian Norris6033a942013-11-14 14:41:32 -08001381 /*
1382 * Required ECC: 8-bit correction per 512 bytes
1383 * Select: 16-bit correction per 1024 bytes
1384 */
1385 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001386 info->ecc_bch = 1;
1387 info->chunk_size = 1024;
1388 info->spare_size = 0;
1389 info->ecc_size = 32;
1390 ecc->mode = NAND_ECC_HW;
1391 ecc->size = info->chunk_size;
1392 ecc->layout = &ecc_layout_4KB_bch8bit;
1393 ecc->strength = 16;
1394 return 1;
1395 }
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001396 return 0;
1397}
1398
Lei Wen401e67e2011-02-28 10:32:14 +08001399static int pxa3xx_nand_scan(struct mtd_info *mtd)
1400{
Lei Wend4568822011-07-14 20:44:32 -07001401 struct pxa3xx_nand_host *host = mtd->priv;
1402 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001403 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001404 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001405 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001406 const struct pxa3xx_nand_flash *f = NULL;
1407 struct nand_chip *chip = mtd->priv;
1408 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001409 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001410 int i, ret, num;
1411
1412 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001413 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001414
1415 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001416 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001417 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1418 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001419
Lei Wend4568822011-07-14 20:44:32 -07001420 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001421 }
1422
1423 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1424 id = *((uint16_t *)(info->data_buff));
1425 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001426 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001427 else {
Lei Wenda675b42011-07-14 20:44:31 -07001428 dev_warn(&info->pdev->dev,
1429 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001430
1431 return -EINVAL;
1432 }
1433
1434 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1435 for (i = 0; i < num; i++) {
1436 if (i < pdata->num_flash)
1437 f = pdata->flash + i;
1438 else
1439 f = &builtin_flash_types[i - pdata->num_flash + 1];
1440
1441 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001442 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001443 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001444 }
1445
Lei Wen4332c112011-03-03 11:27:01 +08001446 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001447 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001448
1449 return -EINVAL;
1450 }
1451
Lei Wend4568822011-07-14 20:44:32 -07001452 ret = pxa3xx_nand_config_flash(info, f);
1453 if (ret) {
1454 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1455 return ret;
1456 }
1457
Lei Wen4332c112011-03-03 11:27:01 +08001458 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001459 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001460 pxa3xx_flash_ids[0].pagesize = f->page_size;
1461 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1462 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1463 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1464 if (f->flash_width == 16)
1465 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001466 pxa3xx_flash_ids[1].name = NULL;
1467 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001468KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001469 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001470 chip->options |= NAND_BUSWIDTH_16;
1471
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001472 /* Device detection must be done with ECC disabled */
1473 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1474 nand_writel(info, NDECCCTRL, 0x0);
1475
Lei Wen0fab0282011-06-07 03:01:06 -07001476 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001477 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001478
1479 if (pdata->flash_bbt) {
1480 /*
1481 * We'll use a bad block table stored in-flash and don't
1482 * allow writing the bad block marker to the flash.
1483 */
1484 chip->bbt_options |= NAND_BBT_USE_FLASH |
1485 NAND_BBT_NO_OOB_BBM;
1486 chip->bbt_td = &bbt_main_descr;
1487 chip->bbt_md = &bbt_mirror_descr;
1488 }
1489
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001490 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1491 ret = armada370_ecc_init(info, &chip->ecc,
1492 chip->ecc_strength_ds,
Brian Norris6033a942013-11-14 14:41:32 -08001493 chip->ecc_step_ds,
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001494 mtd->writesize);
1495 else
1496 ret = pxa_ecc_init(info, &chip->ecc,
1497 chip->ecc_strength_ds,
1498 mtd->writesize);
1499 if (!ret) {
1500 dev_err(&info->pdev->dev,
1501 "ECC strength %d at page size %d is not supported\n",
1502 chip->ecc_strength_ds, mtd->writesize);
1503 return -ENODEV;
1504 }
1505
Lei Wen4332c112011-03-03 11:27:01 +08001506 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001507 if (mtd->writesize >= 2048)
1508 host->col_addr_cycles = 2;
1509 else
1510 host->col_addr_cycles = 1;
1511
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001512 /* release the initial buffer */
1513 kfree(info->data_buff);
1514
1515 /* allocate the real data + oob buffer */
1516 info->buf_size = mtd->writesize + mtd->oobsize;
1517 ret = pxa3xx_nand_init_buff(info);
1518 if (ret)
1519 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001520 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001521
Lei Wen4332c112011-03-03 11:27:01 +08001522 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001523 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001524 else
Lei Wend4568822011-07-14 20:44:32 -07001525 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001526 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001527}
1528
Lei Wend4568822011-07-14 20:44:32 -07001529static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001530{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001531 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001532 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001533 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001534 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001535 struct mtd_info *mtd;
1536 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001537 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001538
Jingoo Han453810b2013-07-30 17:18:33 +09001539 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001540 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1541 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1542 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001543 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001544
eric miaofe69af02008-02-14 15:48:23 +08001545 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001546 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001547 for (cs = 0; cs < pdata->num_cs; cs++) {
1548 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1549 (sizeof(*mtd) + sizeof(*host)) * cs);
1550 chip = (struct nand_chip *)(&mtd[1]);
1551 host = (struct pxa3xx_nand_host *)chip;
1552 info->host[cs] = host;
1553 host->mtd = mtd;
1554 host->cs = cs;
1555 host->info_data = info;
1556 mtd->priv = host;
1557 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001558
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001559 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1560 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1561 chip->controller = &info->controller;
1562 chip->waitfunc = pxa3xx_nand_waitfunc;
1563 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001564 chip->read_word = pxa3xx_nand_read_word;
1565 chip->read_byte = pxa3xx_nand_read_byte;
1566 chip->read_buf = pxa3xx_nand_read_buf;
1567 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001568 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001569
1570 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1571 chip->cmdfunc = armada370_nand_cmdfunc;
1572 else
1573 chip->cmdfunc = pxa3xx_nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001574 }
Lei Wen401e67e2011-02-28 10:32:14 +08001575
1576 spin_lock_init(&chip->controller->lock);
1577 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001578 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001579 if (IS_ERR(info->clk)) {
1580 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001581 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001582 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001583 ret = clk_prepare_enable(info->clk);
1584 if (ret < 0)
1585 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001586
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001587 if (use_dma) {
1588 /*
1589 * This is a dirty hack to make this driver work from
1590 * devicetree bindings. It can be removed once we have
1591 * a prober DMA controller framework for DT.
1592 */
1593 if (pdev->dev.of_node &&
1594 of_machine_is_compatible("marvell,pxa3xx")) {
1595 info->drcmr_dat = 97;
1596 info->drcmr_cmd = 99;
1597 } else {
1598 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1599 if (r == NULL) {
1600 dev_err(&pdev->dev,
1601 "no resource defined for data DMA\n");
1602 ret = -ENXIO;
1603 goto fail_disable_clk;
1604 }
1605 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001606
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001607 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1608 if (r == NULL) {
1609 dev_err(&pdev->dev,
1610 "no resource defined for cmd DMA\n");
1611 ret = -ENXIO;
1612 goto fail_disable_clk;
1613 }
1614 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001615 }
eric miaofe69af02008-02-14 15:48:23 +08001616 }
eric miaofe69af02008-02-14 15:48:23 +08001617
1618 irq = platform_get_irq(pdev, 0);
1619 if (irq < 0) {
1620 dev_err(&pdev->dev, "no IRQ resource defined\n");
1621 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001622 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001623 }
1624
1625 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001626 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1627 if (IS_ERR(info->mmio_base)) {
1628 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001629 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001630 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001631 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001632
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001633 /* Allocate a buffer to allow flash detection */
1634 info->buf_size = INIT_BUFFER_SIZE;
1635 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1636 if (info->data_buff == NULL) {
1637 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001638 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001639 }
eric miaofe69af02008-02-14 15:48:23 +08001640
Haojian Zhuang346e1252009-09-10 14:27:23 +08001641 /* initialize all interrupts to be disabled */
1642 disable_int(info, NDSR_MASK);
1643
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001644 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001645 if (ret < 0) {
1646 dev_err(&pdev->dev, "failed to request IRQ\n");
1647 goto fail_free_buf;
1648 }
1649
Lei Wene353a202011-03-03 11:08:30 +08001650 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001651
Lei Wend4568822011-07-14 20:44:32 -07001652 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001653
eric miaofe69af02008-02-14 15:48:23 +08001654fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001655 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001656 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001657fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001658 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001659 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001660}
1661
1662static int pxa3xx_nand_remove(struct platform_device *pdev)
1663{
Lei Wene353a202011-03-03 11:08:30 +08001664 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001665 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001666 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001667
Lei Wend4568822011-07-14 20:44:32 -07001668 if (!info)
1669 return 0;
1670
Jingoo Han453810b2013-07-30 17:18:33 +09001671 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001672
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001673 irq = platform_get_irq(pdev, 0);
1674 if (irq >= 0)
1675 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001676 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001677
Ezequiel Garciafb320612013-04-17 13:38:12 -03001678 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001679
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001680 for (cs = 0; cs < pdata->num_cs; cs++)
1681 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001682 return 0;
1683}
1684
Daniel Mack1e7ba632012-07-22 19:51:02 +02001685static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1686{
1687 struct pxa3xx_nand_platform_data *pdata;
1688 struct device_node *np = pdev->dev.of_node;
1689 const struct of_device_id *of_id =
1690 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1691
1692 if (!of_id)
1693 return 0;
1694
1695 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1696 if (!pdata)
1697 return -ENOMEM;
1698
1699 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1700 pdata->enable_arbiter = 1;
1701 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1702 pdata->keep_config = 1;
1703 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001704 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001705
1706 pdev->dev.platform_data = pdata;
1707
1708 return 0;
1709}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001710
Lei Wene353a202011-03-03 11:08:30 +08001711static int pxa3xx_nand_probe(struct platform_device *pdev)
1712{
1713 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001714 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001715 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001716 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001717
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001718#ifndef ARCH_HAS_DMA
1719 if (use_dma) {
1720 use_dma = 0;
1721 dev_warn(&pdev->dev,
1722 "This platform can't do DMA on this device\n");
1723 }
1724#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001725 ret = pxa3xx_nand_probe_dt(pdev);
1726 if (ret)
1727 return ret;
1728
Jingoo Han453810b2013-07-30 17:18:33 +09001729 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001730 if (!pdata) {
1731 dev_err(&pdev->dev, "no platform data defined\n");
1732 return -ENODEV;
1733 }
1734
Lei Wend4568822011-07-14 20:44:32 -07001735 ret = alloc_nand_resource(pdev);
1736 if (ret) {
1737 dev_err(&pdev->dev, "alloc nand resource failed\n");
1738 return ret;
1739 }
Lei Wene353a202011-03-03 11:08:30 +08001740
Lei Wend4568822011-07-14 20:44:32 -07001741 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001742 probe_success = 0;
1743 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001744 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001745
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001746 /*
1747 * The mtd name matches the one used in 'mtdparts' kernel
1748 * parameter. This name cannot be changed or otherwise
1749 * user's mtd partitions configuration would get broken.
1750 */
1751 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001752 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001753 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001754 if (ret) {
1755 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1756 cs);
1757 continue;
1758 }
1759
Daniel Mack1e7ba632012-07-22 19:51:02 +02001760 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001761 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001762 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001763 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001764 if (!ret)
1765 probe_success = 1;
1766 }
1767
1768 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001769 pxa3xx_nand_remove(pdev);
1770 return -ENODEV;
1771 }
1772
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001773 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001774}
1775
eric miaofe69af02008-02-14 15:48:23 +08001776#ifdef CONFIG_PM
1777static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1778{
Lei Wene353a202011-03-03 11:08:30 +08001779 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001780 struct pxa3xx_nand_platform_data *pdata;
1781 struct mtd_info *mtd;
1782 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001783
Jingoo Han453810b2013-07-30 17:18:33 +09001784 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001785 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001786 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1787 return -EAGAIN;
1788 }
1789
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001790 for (cs = 0; cs < pdata->num_cs; cs++) {
1791 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001792 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001793 }
1794
eric miaofe69af02008-02-14 15:48:23 +08001795 return 0;
1796}
1797
1798static int pxa3xx_nand_resume(struct platform_device *pdev)
1799{
Lei Wene353a202011-03-03 11:08:30 +08001800 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001801 struct pxa3xx_nand_platform_data *pdata;
1802 struct mtd_info *mtd;
1803 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001804
Jingoo Han453810b2013-07-30 17:18:33 +09001805 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001806 /* We don't want to handle interrupt without calling mtd routine */
1807 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001808
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001809 /*
1810 * Directly set the chip select to a invalid value,
1811 * then the driver would reset the timing according
1812 * to current chip select at the beginning of cmdfunc
1813 */
1814 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001815
Lei Wen051fc412011-07-14 20:44:30 -07001816 /*
1817 * As the spec says, the NDSR would be updated to 0x1800 when
1818 * doing the nand_clk disable/enable.
1819 * To prevent it damaging state machine of the driver, clear
1820 * all status before resume
1821 */
1822 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001823 for (cs = 0; cs < pdata->num_cs; cs++) {
1824 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001825 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001826 }
1827
Lei Wen18c81b12010-08-17 17:25:57 +08001828 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001829}
1830#else
1831#define pxa3xx_nand_suspend NULL
1832#define pxa3xx_nand_resume NULL
1833#endif
1834
1835static struct platform_driver pxa3xx_nand_driver = {
1836 .driver = {
1837 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301838 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001839 },
1840 .probe = pxa3xx_nand_probe,
1841 .remove = pxa3xx_nand_remove,
1842 .suspend = pxa3xx_nand_suspend,
1843 .resume = pxa3xx_nand_resume,
1844};
1845
Axel Linf99640d2011-11-27 20:45:03 +08001846module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001847
1848MODULE_LICENSE("GPL");
1849MODULE_DESCRIPTION("PXA3xx NAND controller driver");