blob: 51f8a58ed1a9c9a8f1d4cbf2ef92b52d8e2b8408 [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>
Maxime Ripardafca11e2015-04-07 15:32:45 +020025#include <linux/iopoll.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010026#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020028#include <linux/of.h>
29#include <linux/of_device.h>
Ezequiel Garcia776f2652013-11-14 18:25:28 -030030#include <linux/of_mtd.h>
eric miaofe69af02008-02-14 15:48:23 +080031
Rob Herringce914e62015-04-30 15:17:47 -050032#if defined(CONFIG_ARM) && (defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP))
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030033#define ARCH_HAS_DMA
34#endif
35
36#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080037#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030038#endif
39
Arnd Bergmann293b2da2012-08-24 15:16:48 +020040#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080041
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -050042#define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200)
43#define NAND_STOP_DELAY msecs_to_jiffies(40)
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:
Ezequiel Garciac1634092015-08-03 11:31:26 -030048 * STATUS, READID and PARAM.
49 * ONFI param page is 256 bytes, and there are three redundant copies
50 * to be read. JEDEC param page is 512 bytes, and there are also three
51 * redundant copies to be read.
52 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030053 */
Ezequiel Garciac1634092015-08-03 11:31:26 -030054#define INIT_BUFFER_SIZE 2048
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030055
eric miaofe69af02008-02-14 15:48:23 +080056/* registers and bit definitions */
57#define NDCR (0x00) /* Control register */
58#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
59#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
60#define NDSR (0x14) /* Status Register */
61#define NDPCR (0x18) /* Page Count Register */
62#define NDBDR0 (0x1C) /* Bad Block Register 0 */
63#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030064#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080065#define NDDB (0x40) /* Data Buffer */
66#define NDCB0 (0x48) /* Command Buffer0 */
67#define NDCB1 (0x4C) /* Command Buffer1 */
68#define NDCB2 (0x50) /* Command Buffer2 */
69
70#define NDCR_SPARE_EN (0x1 << 31)
71#define NDCR_ECC_EN (0x1 << 30)
72#define NDCR_DMA_EN (0x1 << 29)
73#define NDCR_ND_RUN (0x1 << 28)
74#define NDCR_DWIDTH_C (0x1 << 27)
75#define NDCR_DWIDTH_M (0x1 << 26)
76#define NDCR_PAGE_SZ (0x1 << 24)
77#define NDCR_NCSX (0x1 << 23)
78#define NDCR_ND_MODE (0x3 << 21)
79#define NDCR_NAND_MODE (0x0)
80#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080081#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080082#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
83#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
84
85#define NDCR_RA_START (0x1 << 15)
86#define NDCR_PG_PER_BLK (0x1 << 14)
87#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080088#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080089
90#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030091#define NDSR_ERR_CNT_OFF (16)
92#define NDSR_ERR_CNT_MASK (0x1f)
93#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080094#define NDSR_RDY (0x1 << 12)
95#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080096#define NDSR_CS0_PAGED (0x1 << 10)
97#define NDSR_CS1_PAGED (0x1 << 9)
98#define NDSR_CS0_CMDD (0x1 << 8)
99#define NDSR_CS1_CMDD (0x1 << 7)
100#define NDSR_CS0_BBD (0x1 << 6)
101#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300102#define NDSR_UNCORERR (0x1 << 4)
103#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +0800104#define NDSR_WRDREQ (0x1 << 2)
105#define NDSR_RDDREQ (0x1 << 1)
106#define NDSR_WRCMDREQ (0x1)
107
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300108#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800109#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800110#define NDCB0_AUTO_RS (0x1 << 25)
111#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300112#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
113#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800114#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
115#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
116#define NDCB0_NC (0x1 << 20)
117#define NDCB0_DBC (0x1 << 19)
118#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
119#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
120#define NDCB0_CMD2_MASK (0xff << 8)
121#define NDCB0_CMD1_MASK (0xff)
122#define NDCB0_ADDR_CYC_SHIFT (16)
123
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300124#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
125#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
126#define EXT_CMD_TYPE_READ 4 /* Read */
127#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
128#define EXT_CMD_TYPE_FINAL 3 /* Final command */
129#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
130#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
131
eric miaofe69af02008-02-14 15:48:23 +0800132/* macros for registers read/write */
133#define nand_writel(info, off, val) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200134 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800135
136#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200137 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800138
139/* error code and state */
140enum {
141 ERR_NONE = 0,
142 ERR_DMABUSERR = -1,
143 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300144 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800145 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300146 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800147};
148
149enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800150 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700151 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800152 STATE_CMD_HANDLE,
153 STATE_DMA_READING,
154 STATE_DMA_WRITING,
155 STATE_DMA_DONE,
156 STATE_PIO_READING,
157 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800158 STATE_CMD_DONE,
159 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800160};
161
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300162enum pxa3xx_nand_variant {
163 PXA3XX_NAND_VARIANT_PXA,
164 PXA3XX_NAND_VARIANT_ARMADA370,
165};
166
Lei Wend4568822011-07-14 20:44:32 -0700167struct pxa3xx_nand_host {
168 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700169 struct mtd_info *mtd;
170 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800171
Lei Wend4568822011-07-14 20:44:32 -0700172 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700173 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700174 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700175
176 /* calculated from pxa3xx_nand_flash data */
177 unsigned int col_addr_cycles;
178 unsigned int row_addr_cycles;
179 size_t read_id_bytes;
180
Lei Wend4568822011-07-14 20:44:32 -0700181};
182
183struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800184 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800185 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800186
187 struct clk *clk;
188 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800189 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300190 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800191
192 unsigned int buf_start;
193 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300194 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300195 unsigned int data_buff_pos;
196 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800197
198 /* DMA information */
199 int drcmr_dat;
200 int drcmr_cmd;
201
202 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800203 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800204 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800205 int data_dma_ch;
206 struct pxa_dma_desc *data_desc;
207 dma_addr_t data_desc_addr;
208
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700209 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800210 unsigned int state;
211
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300212 /*
213 * This driver supports NFCv1 (as found in PXA SoC)
214 * and NFCv2 (as found in Armada 370/XP SoC).
215 */
216 enum pxa3xx_nand_variant variant;
217
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700218 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800219 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300220 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800221 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300222 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300223 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800224
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300225 unsigned int data_size; /* data to be read from FIFO */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300226 unsigned int chunk_size; /* split commands chunk size */
Lei Wend4568822011-07-14 20:44:32 -0700227 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300228 unsigned int spare_size;
229 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300230 unsigned int ecc_err_cnt;
231 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800232 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800233
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300234 /* cached register value */
235 uint32_t reg_ndcr;
236 uint32_t ndtr0cs0;
237 uint32_t ndtr1cs0;
238
eric miaofe69af02008-02-14 15:48:23 +0800239 /* generated NDCBx register values */
240 uint32_t ndcb0;
241 uint32_t ndcb1;
242 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300243 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800244};
245
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030246static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800247module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300248MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800249
Lei Wenc1f82472010-08-17 13:50:23 +0800250static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800251 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
252 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
253 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
254 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800255};
256
Lei Wenc1f82472010-08-17 13:50:23 +0800257static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800258{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
259{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
260{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
261{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
262{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
263{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
264{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
265{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
266{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800267};
268
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300269static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
270static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
271
272static struct nand_bbt_descr bbt_main_descr = {
273 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
274 | NAND_BBT_2BIT | NAND_BBT_VERSION,
275 .offs = 8,
276 .len = 6,
277 .veroffs = 14,
278 .maxblocks = 8, /* Last 8 blocks in each chip */
279 .pattern = bbt_pattern
280};
281
282static struct nand_bbt_descr bbt_mirror_descr = {
283 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
284 | NAND_BBT_2BIT | NAND_BBT_VERSION,
285 .offs = 8,
286 .len = 6,
287 .veroffs = 14,
288 .maxblocks = 8, /* Last 8 blocks in each chip */
289 .pattern = bbt_mirror_pattern
290};
291
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100292static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
293 .eccbytes = 32,
294 .eccpos = {
295 32, 33, 34, 35, 36, 37, 38, 39,
296 40, 41, 42, 43, 44, 45, 46, 47,
297 48, 49, 50, 51, 52, 53, 54, 55,
298 56, 57, 58, 59, 60, 61, 62, 63},
299 .oobfree = { {2, 30} }
300};
301
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300302static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
303 .eccbytes = 64,
304 .eccpos = {
305 32, 33, 34, 35, 36, 37, 38, 39,
306 40, 41, 42, 43, 44, 45, 46, 47,
307 48, 49, 50, 51, 52, 53, 54, 55,
308 56, 57, 58, 59, 60, 61, 62, 63,
309 96, 97, 98, 99, 100, 101, 102, 103,
310 104, 105, 106, 107, 108, 109, 110, 111,
311 112, 113, 114, 115, 116, 117, 118, 119,
312 120, 121, 122, 123, 124, 125, 126, 127},
313 /* Bootrom looks in bytes 0 & 5 for bad blocks */
314 .oobfree = { {6, 26}, { 64, 32} }
315};
316
317static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
318 .eccbytes = 128,
319 .eccpos = {
320 32, 33, 34, 35, 36, 37, 38, 39,
321 40, 41, 42, 43, 44, 45, 46, 47,
322 48, 49, 50, 51, 52, 53, 54, 55,
323 56, 57, 58, 59, 60, 61, 62, 63},
324 .oobfree = { }
325};
326
Lei Wen227a8862010-08-18 18:00:03 +0800327/* Define a default flash type setting serve as flash detecting only */
328#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
329
eric miaofe69af02008-02-14 15:48:23 +0800330#define NDTR0_tCH(c) (min((c), 7) << 19)
331#define NDTR0_tCS(c) (min((c), 7) << 16)
332#define NDTR0_tWH(c) (min((c), 7) << 11)
333#define NDTR0_tWP(c) (min((c), 7) << 8)
334#define NDTR0_tRH(c) (min((c), 7) << 3)
335#define NDTR0_tRP(c) (min((c), 7) << 0)
336
337#define NDTR1_tR(c) (min((c), 65535) << 16)
338#define NDTR1_tWHR(c) (min((c), 15) << 4)
339#define NDTR1_tAR(c) (min((c), 15) << 0)
340
341/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800342#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800343
Jingoo Han17754ad2014-05-07 17:49:13 +0900344static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300345 {
346 .compatible = "marvell,pxa3xx-nand",
347 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
348 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300349 {
350 .compatible = "marvell,armada370-nand",
351 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
352 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300353 {}
354};
355MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
356
357static enum pxa3xx_nand_variant
358pxa3xx_nand_get_variant(struct platform_device *pdev)
359{
360 const struct of_device_id *of_id =
361 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
362 if (!of_id)
363 return PXA3XX_NAND_VARIANT_PXA;
364 return (enum pxa3xx_nand_variant)of_id->data;
365}
366
Lei Wend4568822011-07-14 20:44:32 -0700367static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200368 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800369{
Lei Wend4568822011-07-14 20:44:32 -0700370 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800371 unsigned long nand_clk = clk_get_rate(info->clk);
372 uint32_t ndtr0, ndtr1;
373
374 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
375 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
376 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
377 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
378 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
379 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
380
381 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
382 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
383 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
384
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300385 info->ndtr0cs0 = ndtr0;
386 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800387 nand_writel(info, NDTR0CS0, ndtr0);
388 nand_writel(info, NDTR1CS0, ndtr1);
389}
390
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300391/*
392 * Set the data and OOB size, depending on the selected
393 * spare and ECC configuration.
394 * Only applicable to READ0, READOOB and PAGEPROG commands.
395 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300396static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
397 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800398{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300399 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800400
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300401 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300402 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800403 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800404
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300405 info->oob_size = info->spare_size;
406 if (!info->use_ecc)
407 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800408}
409
Lei Wenf8155a42011-02-28 10:32:11 +0800410/**
411 * NOTE: it is a must to set ND_RUN firstly, then write
412 * command buffer, otherwise, it does not work.
413 * We enable all the interrupt at the same time, and
414 * let pxa3xx_nand_irq to handle all logic.
415 */
416static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
417{
418 uint32_t ndcr;
419
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300420 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300421
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300422 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300423 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300424 if (info->ecc_bch)
425 nand_writel(info, NDECCCTRL, 0x1);
426 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300427 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300428 if (info->ecc_bch)
429 nand_writel(info, NDECCCTRL, 0x0);
430 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300431
432 if (info->use_dma)
433 ndcr |= NDCR_DMA_EN;
434 else
435 ndcr &= ~NDCR_DMA_EN;
436
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300437 if (info->use_spare)
438 ndcr |= NDCR_SPARE_EN;
439 else
440 ndcr &= ~NDCR_SPARE_EN;
441
Lei Wenf8155a42011-02-28 10:32:11 +0800442 ndcr |= NDCR_ND_RUN;
443
444 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800445 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200446 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800447 nand_writel(info, NDCR, ndcr);
448}
449
450static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
451{
452 uint32_t ndcr;
453 int timeout = NAND_STOP_DELAY;
454
455 /* wait RUN bit in NDCR become 0 */
456 ndcr = nand_readl(info, NDCR);
457 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
458 ndcr = nand_readl(info, NDCR);
459 udelay(1);
460 }
461
462 if (timeout <= 0) {
463 ndcr &= ~NDCR_ND_RUN;
464 nand_writel(info, NDCR, ndcr);
465 }
466 /* clear status bits */
467 nand_writel(info, NDSR, NDSR_MASK);
468}
469
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300470static void __maybe_unused
471enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800472{
473 uint32_t ndcr;
474
475 ndcr = nand_readl(info, NDCR);
476 nand_writel(info, NDCR, ndcr & ~int_mask);
477}
478
479static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
480{
481 uint32_t ndcr;
482
483 ndcr = nand_readl(info, NDCR);
484 nand_writel(info, NDCR, ndcr | int_mask);
485}
486
Maxime Ripard8dad0382015-02-18 11:32:07 +0100487static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
488{
489 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200490 u32 val;
491 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100492
493 /*
494 * According to the datasheet, when reading from NDDB
495 * with BCH enabled, after each 32 bytes reads, we
496 * have to make sure that the NDSR.RDDREQ bit is set.
497 *
498 * Drain the FIFO 8 32 bits reads at a time, and skip
499 * the polling on the last read.
500 */
501 while (len > 8) {
Rob Herringce914e62015-04-30 15:17:47 -0500502 readsl(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100503
Maxime Ripardafca11e2015-04-07 15:32:45 +0200504 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
505 val & NDSR_RDDREQ, 1000, 5000);
506 if (ret) {
507 dev_err(&info->pdev->dev,
508 "Timeout on RDDREQ while draining the FIFO\n");
509 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100510 }
511
512 data += 32;
513 len -= 8;
514 }
515 }
516
Rob Herringce914e62015-04-30 15:17:47 -0500517 readsl(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100518}
519
Lei Wenf8155a42011-02-28 10:32:11 +0800520static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800521{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300522 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300523
eric miaofe69af02008-02-14 15:48:23 +0800524 switch (info->state) {
525 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500526 writesl(info->mmio_base + NDDB,
527 info->data_buff + info->data_buff_pos,
528 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300529
Lei Wen9d8b1042010-08-17 14:09:30 +0800530 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500531 writesl(info->mmio_base + NDDB,
532 info->oob_buff + info->oob_buff_pos,
533 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800534 break;
535 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100536 drain_fifo(info,
537 info->data_buff + info->data_buff_pos,
538 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300539
Lei Wen9d8b1042010-08-17 14:09:30 +0800540 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100541 drain_fifo(info,
542 info->oob_buff + info->oob_buff_pos,
543 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800544 break;
545 default:
Lei Wenda675b42011-07-14 20:44:31 -0700546 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800547 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800548 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800549 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300550
551 /* Update buffer pointers for multi-page read/write */
552 info->data_buff_pos += do_bytes;
553 info->oob_buff_pos += info->oob_size;
554 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800555}
556
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300557#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800558static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800559{
560 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800561 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800562
563 desc->ddadr = DDADR_STOP;
564 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
565
Lei Wenf8155a42011-02-28 10:32:11 +0800566 switch (info->state) {
567 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800568 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800569 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800570 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800571 break;
572 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800573 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800574 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800575 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800576 break;
577 default:
Lei Wenda675b42011-07-14 20:44:31 -0700578 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800579 info->state);
580 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800581 }
582
583 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
584 DDADR(info->data_dma_ch) = info->data_desc_addr;
585 DCSR(info->data_dma_ch) |= DCSR_RUN;
586}
587
588static void pxa3xx_nand_data_dma_irq(int channel, void *data)
589{
590 struct pxa3xx_nand_info *info = data;
591 uint32_t dcsr;
592
593 dcsr = DCSR(channel);
594 DCSR(channel) = dcsr;
595
596 if (dcsr & DCSR_BUSERR) {
597 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800598 }
599
Lei Wenf8155a42011-02-28 10:32:11 +0800600 info->state = STATE_DMA_DONE;
601 enable_int(info, NDCR_INT_MASK);
602 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800603}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300604#else
605static void start_data_dma(struct pxa3xx_nand_info *info)
606{}
607#endif
eric miaofe69af02008-02-14 15:48:23 +0800608
Robert Jarzmik24542252015-02-20 19:36:43 +0100609static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
610{
611 struct pxa3xx_nand_info *info = data;
612
613 handle_data_pio(info);
614
615 info->state = STATE_CMD_DONE;
616 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
617
618 return IRQ_HANDLED;
619}
620
eric miaofe69af02008-02-14 15:48:23 +0800621static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
622{
623 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300624 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700625 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100626 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700627
628 if (info->cs == 0) {
629 ready = NDSR_FLASH_RDY;
630 cmd_done = NDSR_CS0_CMDD;
631 } else {
632 ready = NDSR_RDY;
633 cmd_done = NDSR_CS1_CMDD;
634 }
eric miaofe69af02008-02-14 15:48:23 +0800635
636 status = nand_readl(info, NDSR);
637
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300638 if (status & NDSR_UNCORERR)
639 info->retcode = ERR_UNCORERR;
640 if (status & NDSR_CORERR) {
641 info->retcode = ERR_CORERR;
642 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
643 info->ecc_bch)
644 info->ecc_err_cnt = NDSR_ERR_CNT(status);
645 else
646 info->ecc_err_cnt = 1;
647
648 /*
649 * Each chunk composing a page is corrected independently,
650 * and we need to store maximum number of corrected bitflips
651 * to return it to the MTD layer in ecc.read_page().
652 */
653 info->max_bitflips = max_t(unsigned int,
654 info->max_bitflips,
655 info->ecc_err_cnt);
656 }
Lei Wenf8155a42011-02-28 10:32:11 +0800657 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
658 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800659 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800660 disable_int(info, NDCR_INT_MASK);
661 info->state = (status & NDSR_RDDREQ) ?
662 STATE_DMA_READING : STATE_DMA_WRITING;
663 start_data_dma(info);
664 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800665 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800666 info->state = (status & NDSR_RDDREQ) ?
667 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100668 ret = IRQ_WAKE_THREAD;
669 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800670 }
Lei Wenf8155a42011-02-28 10:32:11 +0800671 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700672 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800673 info->state = STATE_CMD_DONE;
674 is_completed = 1;
675 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700676 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800677 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300678 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800679 }
Lei Wenf8155a42011-02-28 10:32:11 +0800680
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200681 /*
682 * Clear all status bit before issuing the next command, which
683 * can and will alter the status bits and will deserve a new
684 * interrupt on its own. This lets the controller exit the IRQ
685 */
686 nand_writel(info, NDSR, status);
687
Lei Wenf8155a42011-02-28 10:32:11 +0800688 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800689 status &= ~NDSR_WRCMDREQ;
690 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300691
692 /*
693 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
694 * must be loaded by writing directly either 12 or 16
695 * bytes directly to NDCB0, four bytes at a time.
696 *
697 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
698 * but each NDCBx register can be read.
699 */
Lei Wenf8155a42011-02-28 10:32:11 +0800700 nand_writel(info, NDCB0, info->ndcb0);
701 nand_writel(info, NDCB0, info->ndcb1);
702 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300703
704 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
705 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
706 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800707 }
Lei Wenf8155a42011-02-28 10:32:11 +0800708
Lei Wenf8155a42011-02-28 10:32:11 +0800709 if (is_completed)
710 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300711 if (is_ready)
712 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800713NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100714 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800715}
716
eric miaofe69af02008-02-14 15:48:23 +0800717static inline int is_buf_blank(uint8_t *buf, size_t len)
718{
719 for (; len > 0; len--)
720 if (*buf++ != 0xff)
721 return 0;
722 return 1;
723}
724
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300725static void set_command_address(struct pxa3xx_nand_info *info,
726 unsigned int page_size, uint16_t column, int page_addr)
727{
728 /* small page addr setting */
729 if (page_size < PAGE_CHUNK_SIZE) {
730 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
731 | (column & 0xFF);
732
733 info->ndcb2 = 0;
734 } else {
735 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
736 | (column & 0xFFFF);
737
738 if (page_addr & 0xFF0000)
739 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
740 else
741 info->ndcb2 = 0;
742 }
743}
744
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300745static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800746{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300747 struct pxa3xx_nand_host *host = info->host[info->cs];
748 struct mtd_info *mtd = host->mtd;
749
Lei Wen4eb2da82011-02-28 10:32:13 +0800750 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800751 info->buf_start = 0;
752 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800753 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300754 info->data_buff_pos = 0;
755 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800756 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300757 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800758 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300759 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300760 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300761 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800762
763 switch (command) {
764 case NAND_CMD_READ0:
765 case NAND_CMD_PAGEPROG:
766 info->use_ecc = 1;
767 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300768 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800769 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300770 case NAND_CMD_PARAM:
771 info->use_spare = 0;
772 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800773 default:
774 info->ndcb1 = 0;
775 info->ndcb2 = 0;
776 break;
777 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300778
779 /*
780 * If we are about to issue a read command, or about to set
781 * the write address, then clean the data buffer.
782 */
783 if (command == NAND_CMD_READ0 ||
784 command == NAND_CMD_READOOB ||
785 command == NAND_CMD_SEQIN) {
786
787 info->buf_count = mtd->writesize + mtd->oobsize;
788 memset(info->data_buff, 0xFF, info->buf_count);
789 }
790
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300791}
792
793static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300794 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300795{
796 int addr_cycle, exec_cmd;
797 struct pxa3xx_nand_host *host;
798 struct mtd_info *mtd;
799
800 host = info->host[info->cs];
801 mtd = host->mtd;
802 addr_cycle = 0;
803 exec_cmd = 1;
804
805 if (info->cs != 0)
806 info->ndcb0 = NDCB0_CSEL;
807 else
808 info->ndcb0 = 0;
809
810 if (command == NAND_CMD_SEQIN)
811 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800812
Lei Wend4568822011-07-14 20:44:32 -0700813 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
814 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800815
816 switch (command) {
817 case NAND_CMD_READOOB:
818 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300819 info->buf_start = column;
820 info->ndcb0 |= NDCB0_CMD_TYPE(0)
821 | addr_cycle
822 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800823
Ezequiel Garciaec821352013-08-12 14:14:54 -0300824 if (command == NAND_CMD_READOOB)
825 info->buf_start += mtd->writesize;
826
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300827 /*
828 * Multiple page read needs an 'extended command type' field,
829 * which is either naked-read or last-read according to the
830 * state.
831 */
832 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300833 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300834 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
835 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
836 | NDCB0_LEN_OVRD
837 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
838 info->ndcb3 = info->chunk_size +
839 info->oob_size;
840 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800841
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300842 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300843 break;
844
Lei Wen4eb2da82011-02-28 10:32:13 +0800845 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800846
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300847 info->buf_start = column;
848 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300849
850 /*
851 * Multiple page programming needs to execute the initial
852 * SEQIN command that sets the page address.
853 */
854 if (mtd->writesize > PAGE_CHUNK_SIZE) {
855 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
856 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
857 | addr_cycle
858 | command;
859 /* No data transfer in this case */
860 info->data_size = 0;
861 exec_cmd = 1;
862 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800863 break;
864
865 case NAND_CMD_PAGEPROG:
866 if (is_buf_blank(info->data_buff,
867 (mtd->writesize + mtd->oobsize))) {
868 exec_cmd = 0;
869 break;
870 }
871
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300872 /* Second command setting for large pages */
873 if (mtd->writesize > PAGE_CHUNK_SIZE) {
874 /*
875 * Multiple page write uses the 'extended command'
876 * field. This can be used to issue a command dispatch
877 * or a naked-write depending on the current stage.
878 */
879 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
880 | NDCB0_LEN_OVRD
881 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
882 info->ndcb3 = info->chunk_size +
883 info->oob_size;
884
885 /*
886 * This is the command dispatch that completes a chunked
887 * page program operation.
888 */
889 if (info->data_size == 0) {
890 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
891 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
892 | command;
893 info->ndcb1 = 0;
894 info->ndcb2 = 0;
895 info->ndcb3 = 0;
896 }
897 } else {
898 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
899 | NDCB0_AUTO_RS
900 | NDCB0_ST_ROW_EN
901 | NDCB0_DBC
902 | (NAND_CMD_PAGEPROG << 8)
903 | NAND_CMD_SEQIN
904 | addr_cycle;
905 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800906 break;
907
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300908 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -0300909 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300910 info->ndcb0 |= NDCB0_CMD_TYPE(0)
911 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300912 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300913 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300914 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -0300915 info->ndcb3 = INIT_BUFFER_SIZE;
916 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300917 break;
918
Lei Wen4eb2da82011-02-28 10:32:13 +0800919 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700920 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800921 info->ndcb0 |= NDCB0_CMD_TYPE(3)
922 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300923 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300924 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800925
926 info->data_size = 8;
927 break;
928 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800929 info->buf_count = 1;
930 info->ndcb0 |= NDCB0_CMD_TYPE(4)
931 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300932 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800933
934 info->data_size = 8;
935 break;
936
937 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800938 info->ndcb0 |= NDCB0_CMD_TYPE(2)
939 | NDCB0_AUTO_RS
940 | NDCB0_ADDR_CYC(3)
941 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300942 | (NAND_CMD_ERASE2 << 8)
943 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800944 info->ndcb1 = page_addr;
945 info->ndcb2 = 0;
946
947 break;
948 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800949 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300950 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800951
952 break;
953
954 case NAND_CMD_ERASE2:
955 exec_cmd = 0;
956 break;
957
958 default:
959 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700960 dev_err(&info->pdev->dev, "non-supported command %x\n",
961 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800962 break;
963 }
964
965 return exec_cmd;
966}
967
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -0300968static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
969 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800970{
Lei Wend4568822011-07-14 20:44:32 -0700971 struct pxa3xx_nand_host *host = mtd->priv;
972 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -0500973 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800974
Lei Wen4eb2da82011-02-28 10:32:13 +0800975 /*
976 * if this is a x16 device ,then convert the input
977 * "byte" address into a "word" address appropriate
978 * for indexing a word-oriented device
979 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300980 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800981 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800982
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700983 /*
984 * There may be different NAND chip hooked to
985 * different chip select, so check whether
986 * chip select has been changed, if yes, reset the timing
987 */
988 if (info->cs != host->cs) {
989 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300990 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
991 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700992 }
993
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300994 prepare_start_command(info, command);
995
Lei Wend4568822011-07-14 20:44:32 -0700996 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300997 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
998
Lei Wenf8155a42011-02-28 10:32:11 +0800999 if (exec_cmd) {
1000 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001001 init_completion(&info->dev_ready);
1002 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001003 pxa3xx_nand_start(info);
1004
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001005 if (!wait_for_completion_timeout(&info->cmd_complete,
1006 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001007 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001008 /* Stop State Machine for next command cycle */
1009 pxa3xx_nand_stop(info);
1010 }
eric miaofe69af02008-02-14 15:48:23 +08001011 }
Lei Wend4568822011-07-14 20:44:32 -07001012 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001013}
1014
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001015static void nand_cmdfunc_extended(struct mtd_info *mtd,
1016 const unsigned command,
1017 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001018{
1019 struct pxa3xx_nand_host *host = mtd->priv;
1020 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001021 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001022
1023 /*
1024 * if this is a x16 device then convert the input
1025 * "byte" address into a "word" address appropriate
1026 * for indexing a word-oriented device
1027 */
1028 if (info->reg_ndcr & NDCR_DWIDTH_M)
1029 column /= 2;
1030
1031 /*
1032 * There may be different NAND chip hooked to
1033 * different chip select, so check whether
1034 * chip select has been changed, if yes, reset the timing
1035 */
1036 if (info->cs != host->cs) {
1037 info->cs = host->cs;
1038 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1039 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1040 }
1041
1042 /* Select the extended command for the first command */
1043 switch (command) {
1044 case NAND_CMD_READ0:
1045 case NAND_CMD_READOOB:
1046 ext_cmd_type = EXT_CMD_TYPE_MONO;
1047 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001048 case NAND_CMD_SEQIN:
1049 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1050 break;
1051 case NAND_CMD_PAGEPROG:
1052 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1053 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001054 default:
1055 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001056 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001057 }
1058
1059 prepare_start_command(info, command);
1060
1061 /*
1062 * Prepare the "is ready" completion before starting a command
1063 * transaction sequence. If the command is not executed the
1064 * completion will be completed, see below.
1065 *
1066 * We can do that inside the loop because the command variable
1067 * is invariant and thus so is the exec_cmd.
1068 */
1069 info->need_wait = 1;
1070 init_completion(&info->dev_ready);
1071 do {
1072 info->state = STATE_PREPARED;
1073 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1074 column, page_addr);
1075 if (!exec_cmd) {
1076 info->need_wait = 0;
1077 complete(&info->dev_ready);
1078 break;
1079 }
1080
1081 init_completion(&info->cmd_complete);
1082 pxa3xx_nand_start(info);
1083
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001084 if (!wait_for_completion_timeout(&info->cmd_complete,
1085 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001086 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1087 /* Stop State Machine for next command cycle */
1088 pxa3xx_nand_stop(info);
1089 break;
1090 }
1091
1092 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001093 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1094 break;
1095
1096 /*
1097 * After a splitted program command sequence has issued
1098 * the command dispatch, the command sequence is complete.
1099 */
1100 if (info->data_size == 0 &&
1101 command == NAND_CMD_PAGEPROG &&
1102 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001103 break;
1104
1105 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1106 /* Last read: issue a 'last naked read' */
1107 if (info->data_size == info->chunk_size)
1108 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1109 else
1110 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001111
1112 /*
1113 * If a splitted program command has no more data to transfer,
1114 * the command dispatch must be issued to complete.
1115 */
1116 } else if (command == NAND_CMD_PAGEPROG &&
1117 info->data_size == 0) {
1118 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001119 }
1120 } while (1);
1121
1122 info->state = STATE_IDLE;
1123}
1124
Josh Wufdbad98d2012-06-25 18:07:45 +08001125static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001126 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001127{
1128 chip->write_buf(mtd, buf, mtd->writesize);
1129 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001130
1131 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001132}
1133
1134static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001135 struct nand_chip *chip, uint8_t *buf, int oob_required,
1136 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001137{
Lei Wend4568822011-07-14 20:44:32 -07001138 struct pxa3xx_nand_host *host = mtd->priv;
1139 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001140
1141 chip->read_buf(mtd, buf, mtd->writesize);
1142 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1143
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001144 if (info->retcode == ERR_CORERR && info->use_ecc) {
1145 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1146
1147 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001148 /*
1149 * for blank page (all 0xff), HW will calculate its ECC as
1150 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001151 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001152 */
1153 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001154 info->retcode = ERR_NONE;
1155 else
Lei Wenf8155a42011-02-28 10:32:11 +08001156 mtd->ecc_stats.failed++;
1157 }
1158
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001159 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001160}
1161
eric miaofe69af02008-02-14 15:48:23 +08001162static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1163{
Lei Wend4568822011-07-14 20:44:32 -07001164 struct pxa3xx_nand_host *host = mtd->priv;
1165 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001166 char retval = 0xFF;
1167
1168 if (info->buf_start < info->buf_count)
1169 /* Has just send a new command? */
1170 retval = info->data_buff[info->buf_start++];
1171
1172 return retval;
1173}
1174
1175static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1176{
Lei Wend4568822011-07-14 20:44:32 -07001177 struct pxa3xx_nand_host *host = mtd->priv;
1178 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001179 u16 retval = 0xFFFF;
1180
1181 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1182 retval = *((u16 *)(info->data_buff+info->buf_start));
1183 info->buf_start += 2;
1184 }
1185 return retval;
1186}
1187
1188static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1189{
Lei Wend4568822011-07-14 20:44:32 -07001190 struct pxa3xx_nand_host *host = mtd->priv;
1191 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001192 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1193
1194 memcpy(buf, info->data_buff + info->buf_start, real_len);
1195 info->buf_start += real_len;
1196}
1197
1198static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1199 const uint8_t *buf, int len)
1200{
Lei Wend4568822011-07-14 20:44:32 -07001201 struct pxa3xx_nand_host *host = mtd->priv;
1202 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001203 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1204
1205 memcpy(info->data_buff + info->buf_start, buf, real_len);
1206 info->buf_start += real_len;
1207}
1208
eric miaofe69af02008-02-14 15:48:23 +08001209static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1210{
1211 return;
1212}
1213
1214static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1215{
Lei Wend4568822011-07-14 20:44:32 -07001216 struct pxa3xx_nand_host *host = mtd->priv;
1217 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001218
1219 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001220 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001221 if (!wait_for_completion_timeout(&info->dev_ready,
1222 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001223 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1224 return NAND_STATUS_FAIL;
1225 }
1226 }
eric miaofe69af02008-02-14 15:48:23 +08001227
1228 /* pxa3xx_nand_send_command has waited for command complete */
1229 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1230 if (info->retcode == ERR_NONE)
1231 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001232 else
1233 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001234 }
1235
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001236 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001237}
1238
eric miaofe69af02008-02-14 15:48:23 +08001239static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001240 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001241{
1242 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001243 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001244 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001245 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001246
Lei Wenda675b42011-07-14 20:44:31 -07001247 if (f->page_size != 2048 && f->page_size != 512) {
1248 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001249 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001250 }
eric miaofe69af02008-02-14 15:48:23 +08001251
Lei Wenda675b42011-07-14 20:44:31 -07001252 if (f->flash_width != 16 && f->flash_width != 8) {
1253 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001254 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001255 }
eric miaofe69af02008-02-14 15:48:23 +08001256
1257 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -07001258 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +08001259
1260 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001261 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001262
1263 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001264 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001265 else
Lei Wend4568822011-07-14 20:44:32 -07001266 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001267
1268 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001269 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001270 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1271 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1272 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1273 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1274
Lei Wend4568822011-07-14 20:44:32 -07001275 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +08001276 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1277
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001278 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001279
Lei Wend4568822011-07-14 20:44:32 -07001280 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001281 return 0;
1282}
1283
Mike Rapoportf2710492009-02-17 13:54:47 +02001284static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1285{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001286 /*
1287 * We set 0 by hard coding here, for we don't support keep_config
1288 * when there is more than one chip attached to the controller
1289 */
1290 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +02001291 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001292
Lei Wend4568822011-07-14 20:44:32 -07001293 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -03001294 /* Controller's FIFO size */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001295 info->chunk_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -07001296 host->read_id_bytes = 4;
1297 } else {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001298 info->chunk_size = 512;
Lei Wend4568822011-07-14 20:44:32 -07001299 host->read_id_bytes = 2;
1300 }
1301
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001302 /* Set an initial chunk size */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001303 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1304 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1305 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001306 return 0;
1307}
1308
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001309#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +08001310static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1311{
1312 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001313 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001314
1315 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001316 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001317 if (info->data_buff == NULL)
1318 return -ENOMEM;
1319 return 0;
1320 }
1321
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001322 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001323 &info->data_buff_phys, GFP_KERNEL);
1324 if (info->data_buff == NULL) {
1325 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1326 return -ENOMEM;
1327 }
1328
eric miaofe69af02008-02-14 15:48:23 +08001329 info->data_desc = (void *)info->data_buff + data_desc_offset;
1330 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1331
1332 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1333 pxa3xx_nand_data_dma_irq, info);
1334 if (info->data_dma_ch < 0) {
1335 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001336 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001337 info->data_buff, info->data_buff_phys);
1338 return info->data_dma_ch;
1339 }
1340
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001341 /*
1342 * Now that DMA buffers are allocated we turn on
1343 * DMA proper for I/O operations.
1344 */
1345 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001346 return 0;
1347}
1348
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001349static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1350{
1351 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001352 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001353 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001354 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001355 info->data_buff, info->data_buff_phys);
1356 } else {
1357 kfree(info->data_buff);
1358 }
1359}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001360#else
1361static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1362{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001363 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001364 if (info->data_buff == NULL)
1365 return -ENOMEM;
1366 return 0;
1367}
1368
1369static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1370{
1371 kfree(info->data_buff);
1372}
1373#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001374
Lei Wen401e67e2011-02-28 10:32:14 +08001375static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001376{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001377 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001378 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001379 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001380
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001381 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001382 chip = mtd->priv;
1383
Lei Wen401e67e2011-02-28 10:32:14 +08001384 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001385 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1386 if (ret)
1387 return ret;
1388
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001389 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001390 ret = chip->waitfunc(mtd, chip);
1391 if (ret & NAND_STATUS_FAIL)
1392 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001393
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001394 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001395}
eric miaofe69af02008-02-14 15:48:23 +08001396
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001397static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1398 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001399 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001400{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001401 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001402 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001403 info->spare_size = 40;
1404 info->ecc_size = 24;
1405 ecc->mode = NAND_ECC_HW;
1406 ecc->size = 512;
1407 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001408
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001409 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001410 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001411 info->spare_size = 8;
1412 info->ecc_size = 8;
1413 ecc->mode = NAND_ECC_HW;
1414 ecc->size = 512;
1415 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001416
Brian Norris6033a942013-11-14 14:41:32 -08001417 /*
1418 * Required ECC: 4-bit correction per 512 bytes
1419 * Select: 16-bit correction per 2048 bytes
1420 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001421 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1422 info->ecc_bch = 1;
1423 info->chunk_size = 2048;
1424 info->spare_size = 32;
1425 info->ecc_size = 32;
1426 ecc->mode = NAND_ECC_HW;
1427 ecc->size = info->chunk_size;
1428 ecc->layout = &ecc_layout_2KB_bch4bit;
1429 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001430
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001431 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001432 info->ecc_bch = 1;
1433 info->chunk_size = 2048;
1434 info->spare_size = 32;
1435 info->ecc_size = 32;
1436 ecc->mode = NAND_ECC_HW;
1437 ecc->size = info->chunk_size;
1438 ecc->layout = &ecc_layout_4KB_bch4bit;
1439 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001440
Brian Norris6033a942013-11-14 14:41:32 -08001441 /*
1442 * Required ECC: 8-bit correction per 512 bytes
1443 * Select: 16-bit correction per 1024 bytes
1444 */
1445 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001446 info->ecc_bch = 1;
1447 info->chunk_size = 1024;
1448 info->spare_size = 0;
1449 info->ecc_size = 32;
1450 ecc->mode = NAND_ECC_HW;
1451 ecc->size = info->chunk_size;
1452 ecc->layout = &ecc_layout_4KB_bch8bit;
1453 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001454 } else {
1455 dev_err(&info->pdev->dev,
1456 "ECC strength %d at page size %d is not supported\n",
1457 strength, page_size);
1458 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001459 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001460
1461 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1462 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001463 return 0;
1464}
1465
Lei Wen401e67e2011-02-28 10:32:14 +08001466static int pxa3xx_nand_scan(struct mtd_info *mtd)
1467{
Lei Wend4568822011-07-14 20:44:32 -07001468 struct pxa3xx_nand_host *host = mtd->priv;
1469 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001470 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001471 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001472 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001473 const struct pxa3xx_nand_flash *f = NULL;
1474 struct nand_chip *chip = mtd->priv;
1475 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001476 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001477 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001478 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001479
1480 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001481 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001482
Antoine Ténartbc3e00f2015-08-18 10:59:10 +02001483 /* Set a default chunk size */
1484 info->chunk_size = 512;
1485
Lei Wen401e67e2011-02-28 10:32:14 +08001486 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001487 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001488 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1489 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001490
Lei Wend4568822011-07-14 20:44:32 -07001491 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001492 }
1493
1494 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1495 id = *((uint16_t *)(info->data_buff));
1496 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001497 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001498 else {
Lei Wenda675b42011-07-14 20:44:31 -07001499 dev_warn(&info->pdev->dev,
1500 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001501
1502 return -EINVAL;
1503 }
1504
1505 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1506 for (i = 0; i < num; i++) {
1507 if (i < pdata->num_flash)
1508 f = pdata->flash + i;
1509 else
1510 f = &builtin_flash_types[i - pdata->num_flash + 1];
1511
1512 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001513 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001514 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001515 }
1516
Lei Wen4332c112011-03-03 11:27:01 +08001517 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001518 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001519
1520 return -EINVAL;
1521 }
1522
Lei Wend4568822011-07-14 20:44:32 -07001523 ret = pxa3xx_nand_config_flash(info, f);
1524 if (ret) {
1525 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1526 return ret;
1527 }
1528
Antoine Ténart7c2f7172015-02-12 15:53:27 +01001529 memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids));
1530
Lei Wen4332c112011-03-03 11:27:01 +08001531 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001532 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001533 pxa3xx_flash_ids[0].pagesize = f->page_size;
1534 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1535 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1536 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1537 if (f->flash_width == 16)
1538 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001539 pxa3xx_flash_ids[1].name = NULL;
1540 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001541KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001542 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001543 chip->options |= NAND_BUSWIDTH_16;
1544
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001545 /* Device detection must be done with ECC disabled */
1546 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1547 nand_writel(info, NDECCCTRL, 0x0);
1548
Lei Wen0fab0282011-06-07 03:01:06 -07001549 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001550 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001551
1552 if (pdata->flash_bbt) {
1553 /*
1554 * We'll use a bad block table stored in-flash and don't
1555 * allow writing the bad block marker to the flash.
1556 */
1557 chip->bbt_options |= NAND_BBT_USE_FLASH |
1558 NAND_BBT_NO_OOB_BBM;
1559 chip->bbt_td = &bbt_main_descr;
1560 chip->bbt_md = &bbt_mirror_descr;
1561 }
1562
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001563 /*
1564 * If the page size is bigger than the FIFO size, let's check
1565 * we are given the right variant and then switch to the extended
1566 * (aka splitted) command handling,
1567 */
1568 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1569 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1570 chip->cmdfunc = nand_cmdfunc_extended;
1571 } else {
1572 dev_err(&info->pdev->dev,
1573 "unsupported page size on this variant\n");
1574 return -ENODEV;
1575 }
1576 }
1577
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001578 if (pdata->ecc_strength && pdata->ecc_step_size) {
1579 ecc_strength = pdata->ecc_strength;
1580 ecc_step = pdata->ecc_step_size;
1581 } else {
1582 ecc_strength = chip->ecc_strength_ds;
1583 ecc_step = chip->ecc_step_ds;
1584 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001585
1586 /* Set default ECC strength requirements on non-ONFI devices */
1587 if (ecc_strength < 1 && ecc_step < 1) {
1588 ecc_strength = 1;
1589 ecc_step = 512;
1590 }
1591
1592 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1593 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001594 if (ret)
1595 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001596
Lei Wen4332c112011-03-03 11:27:01 +08001597 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001598 if (mtd->writesize >= 2048)
1599 host->col_addr_cycles = 2;
1600 else
1601 host->col_addr_cycles = 1;
1602
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001603 /* release the initial buffer */
1604 kfree(info->data_buff);
1605
1606 /* allocate the real data + oob buffer */
1607 info->buf_size = mtd->writesize + mtd->oobsize;
1608 ret = pxa3xx_nand_init_buff(info);
1609 if (ret)
1610 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001611 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001612
Lei Wen4332c112011-03-03 11:27:01 +08001613 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001614 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001615 else
Lei Wend4568822011-07-14 20:44:32 -07001616 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001617 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001618}
1619
Lei Wend4568822011-07-14 20:44:32 -07001620static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001621{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001622 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001623 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001624 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001625 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001626 struct mtd_info *mtd;
1627 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001628 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001629
Jingoo Han453810b2013-07-30 17:18:33 +09001630 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001631 if (pdata->num_cs <= 0)
1632 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001633 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1634 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1635 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001636 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001637
eric miaofe69af02008-02-14 15:48:23 +08001638 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001639 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001640 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001641 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001642 chip = (struct nand_chip *)(&mtd[1]);
1643 host = (struct pxa3xx_nand_host *)chip;
1644 info->host[cs] = host;
1645 host->mtd = mtd;
1646 host->cs = cs;
1647 host->info_data = info;
1648 mtd->priv = host;
1649 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001650
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001651 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1652 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1653 chip->controller = &info->controller;
1654 chip->waitfunc = pxa3xx_nand_waitfunc;
1655 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001656 chip->read_word = pxa3xx_nand_read_word;
1657 chip->read_byte = pxa3xx_nand_read_byte;
1658 chip->read_buf = pxa3xx_nand_read_buf;
1659 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001660 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001661 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001662 }
Lei Wen401e67e2011-02-28 10:32:14 +08001663
1664 spin_lock_init(&chip->controller->lock);
1665 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001666 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001667 if (IS_ERR(info->clk)) {
1668 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001669 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001670 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001671 ret = clk_prepare_enable(info->clk);
1672 if (ret < 0)
1673 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001674
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001675 if (use_dma) {
1676 /*
1677 * This is a dirty hack to make this driver work from
1678 * devicetree bindings. It can be removed once we have
1679 * a prober DMA controller framework for DT.
1680 */
1681 if (pdev->dev.of_node &&
1682 of_machine_is_compatible("marvell,pxa3xx")) {
1683 info->drcmr_dat = 97;
1684 info->drcmr_cmd = 99;
1685 } else {
1686 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1687 if (r == NULL) {
1688 dev_err(&pdev->dev,
1689 "no resource defined for data DMA\n");
1690 ret = -ENXIO;
1691 goto fail_disable_clk;
1692 }
1693 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001694
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001695 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1696 if (r == NULL) {
1697 dev_err(&pdev->dev,
1698 "no resource defined for cmd DMA\n");
1699 ret = -ENXIO;
1700 goto fail_disable_clk;
1701 }
1702 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001703 }
eric miaofe69af02008-02-14 15:48:23 +08001704 }
eric miaofe69af02008-02-14 15:48:23 +08001705
1706 irq = platform_get_irq(pdev, 0);
1707 if (irq < 0) {
1708 dev_err(&pdev->dev, "no IRQ resource defined\n");
1709 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001710 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001711 }
1712
1713 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001714 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1715 if (IS_ERR(info->mmio_base)) {
1716 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001717 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001718 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001719 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001720
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001721 /* Allocate a buffer to allow flash detection */
1722 info->buf_size = INIT_BUFFER_SIZE;
1723 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1724 if (info->data_buff == NULL) {
1725 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001726 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001727 }
eric miaofe69af02008-02-14 15:48:23 +08001728
Haojian Zhuang346e1252009-09-10 14:27:23 +08001729 /* initialize all interrupts to be disabled */
1730 disable_int(info, NDSR_MASK);
1731
Robert Jarzmik24542252015-02-20 19:36:43 +01001732 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1733 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1734 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001735 if (ret < 0) {
1736 dev_err(&pdev->dev, "failed to request IRQ\n");
1737 goto fail_free_buf;
1738 }
1739
Lei Wene353a202011-03-03 11:08:30 +08001740 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001741
Lei Wend4568822011-07-14 20:44:32 -07001742 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001743
eric miaofe69af02008-02-14 15:48:23 +08001744fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001745 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001746 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001747fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001748 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001749 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001750}
1751
1752static int pxa3xx_nand_remove(struct platform_device *pdev)
1753{
Lei Wene353a202011-03-03 11:08:30 +08001754 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001755 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001756 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001757
Lei Wend4568822011-07-14 20:44:32 -07001758 if (!info)
1759 return 0;
1760
Jingoo Han453810b2013-07-30 17:18:33 +09001761 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001762
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001763 irq = platform_get_irq(pdev, 0);
1764 if (irq >= 0)
1765 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001766 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001767
Ezequiel Garciafb320612013-04-17 13:38:12 -03001768 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001769
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001770 for (cs = 0; cs < pdata->num_cs; cs++)
1771 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001772 return 0;
1773}
1774
Daniel Mack1e7ba632012-07-22 19:51:02 +02001775static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1776{
1777 struct pxa3xx_nand_platform_data *pdata;
1778 struct device_node *np = pdev->dev.of_node;
1779 const struct of_device_id *of_id =
1780 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1781
1782 if (!of_id)
1783 return 0;
1784
1785 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1786 if (!pdata)
1787 return -ENOMEM;
1788
1789 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1790 pdata->enable_arbiter = 1;
1791 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1792 pdata->keep_config = 1;
1793 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001794 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001795
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001796 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1797 if (pdata->ecc_strength < 0)
1798 pdata->ecc_strength = 0;
1799
1800 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1801 if (pdata->ecc_step_size < 0)
1802 pdata->ecc_step_size = 0;
1803
Daniel Mack1e7ba632012-07-22 19:51:02 +02001804 pdev->dev.platform_data = pdata;
1805
1806 return 0;
1807}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001808
Lei Wene353a202011-03-03 11:08:30 +08001809static int pxa3xx_nand_probe(struct platform_device *pdev)
1810{
1811 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001812 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001813 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001814 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001815
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001816#ifndef ARCH_HAS_DMA
1817 if (use_dma) {
1818 use_dma = 0;
1819 dev_warn(&pdev->dev,
1820 "This platform can't do DMA on this device\n");
1821 }
1822#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001823 ret = pxa3xx_nand_probe_dt(pdev);
1824 if (ret)
1825 return ret;
1826
Jingoo Han453810b2013-07-30 17:18:33 +09001827 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001828 if (!pdata) {
1829 dev_err(&pdev->dev, "no platform data defined\n");
1830 return -ENODEV;
1831 }
1832
Lei Wend4568822011-07-14 20:44:32 -07001833 ret = alloc_nand_resource(pdev);
1834 if (ret) {
1835 dev_err(&pdev->dev, "alloc nand resource failed\n");
1836 return ret;
1837 }
Lei Wene353a202011-03-03 11:08:30 +08001838
Lei Wend4568822011-07-14 20:44:32 -07001839 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001840 probe_success = 0;
1841 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001842 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001843
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001844 /*
1845 * The mtd name matches the one used in 'mtdparts' kernel
1846 * parameter. This name cannot be changed or otherwise
1847 * user's mtd partitions configuration would get broken.
1848 */
1849 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001850 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001851 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001852 if (ret) {
1853 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1854 cs);
1855 continue;
1856 }
1857
Daniel Mack1e7ba632012-07-22 19:51:02 +02001858 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001859 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001860 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001861 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001862 if (!ret)
1863 probe_success = 1;
1864 }
1865
1866 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001867 pxa3xx_nand_remove(pdev);
1868 return -ENODEV;
1869 }
1870
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001871 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001872}
1873
eric miaofe69af02008-02-14 15:48:23 +08001874#ifdef CONFIG_PM
1875static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1876{
Lei Wene353a202011-03-03 11:08:30 +08001877 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001878 struct pxa3xx_nand_platform_data *pdata;
1879 struct mtd_info *mtd;
1880 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001881
Jingoo Han453810b2013-07-30 17:18:33 +09001882 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001883 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001884 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1885 return -EAGAIN;
1886 }
1887
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001888 for (cs = 0; cs < pdata->num_cs; cs++) {
1889 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001890 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001891 }
1892
eric miaofe69af02008-02-14 15:48:23 +08001893 return 0;
1894}
1895
1896static int pxa3xx_nand_resume(struct platform_device *pdev)
1897{
Lei Wene353a202011-03-03 11:08:30 +08001898 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001899 struct pxa3xx_nand_platform_data *pdata;
1900 struct mtd_info *mtd;
1901 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001902
Jingoo Han453810b2013-07-30 17:18:33 +09001903 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001904 /* We don't want to handle interrupt without calling mtd routine */
1905 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001906
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001907 /*
1908 * Directly set the chip select to a invalid value,
1909 * then the driver would reset the timing according
1910 * to current chip select at the beginning of cmdfunc
1911 */
1912 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001913
Lei Wen051fc412011-07-14 20:44:30 -07001914 /*
1915 * As the spec says, the NDSR would be updated to 0x1800 when
1916 * doing the nand_clk disable/enable.
1917 * To prevent it damaging state machine of the driver, clear
1918 * all status before resume
1919 */
1920 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001921 for (cs = 0; cs < pdata->num_cs; cs++) {
1922 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001923 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001924 }
1925
Lei Wen18c81b12010-08-17 17:25:57 +08001926 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001927}
1928#else
1929#define pxa3xx_nand_suspend NULL
1930#define pxa3xx_nand_resume NULL
1931#endif
1932
1933static struct platform_driver pxa3xx_nand_driver = {
1934 .driver = {
1935 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301936 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001937 },
1938 .probe = pxa3xx_nand_probe,
1939 .remove = pxa3xx_nand_remove,
1940 .suspend = pxa3xx_nand_suspend,
1941 .resume = pxa3xx_nand_resume,
1942};
1943
Axel Linf99640d2011-11-27 20:45:03 +08001944module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001945
1946MODULE_LICENSE("GPL");
1947MODULE_DESCRIPTION("PXA3xx NAND controller driver");