blob: 6798fae625e258a4a8bca07c4cccb2b1bb7304be [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
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030032#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
33#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:
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) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200131 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800132
133#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200134 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800135
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
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100289static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
290 .eccbytes = 32,
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 .oobfree = { {2, 30} }
297};
298
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300299static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
300 .eccbytes = 64,
301 .eccpos = {
302 32, 33, 34, 35, 36, 37, 38, 39,
303 40, 41, 42, 43, 44, 45, 46, 47,
304 48, 49, 50, 51, 52, 53, 54, 55,
305 56, 57, 58, 59, 60, 61, 62, 63,
306 96, 97, 98, 99, 100, 101, 102, 103,
307 104, 105, 106, 107, 108, 109, 110, 111,
308 112, 113, 114, 115, 116, 117, 118, 119,
309 120, 121, 122, 123, 124, 125, 126, 127},
310 /* Bootrom looks in bytes 0 & 5 for bad blocks */
311 .oobfree = { {6, 26}, { 64, 32} }
312};
313
314static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
315 .eccbytes = 128,
316 .eccpos = {
317 32, 33, 34, 35, 36, 37, 38, 39,
318 40, 41, 42, 43, 44, 45, 46, 47,
319 48, 49, 50, 51, 52, 53, 54, 55,
320 56, 57, 58, 59, 60, 61, 62, 63},
321 .oobfree = { }
322};
323
Lei Wen227a8862010-08-18 18:00:03 +0800324/* Define a default flash type setting serve as flash detecting only */
325#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
326
eric miaofe69af02008-02-14 15:48:23 +0800327#define NDTR0_tCH(c) (min((c), 7) << 19)
328#define NDTR0_tCS(c) (min((c), 7) << 16)
329#define NDTR0_tWH(c) (min((c), 7) << 11)
330#define NDTR0_tWP(c) (min((c), 7) << 8)
331#define NDTR0_tRH(c) (min((c), 7) << 3)
332#define NDTR0_tRP(c) (min((c), 7) << 0)
333
334#define NDTR1_tR(c) (min((c), 65535) << 16)
335#define NDTR1_tWHR(c) (min((c), 15) << 4)
336#define NDTR1_tAR(c) (min((c), 15) << 0)
337
338/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800339#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800340
Jingoo Han17754ad2014-05-07 17:49:13 +0900341static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300342 {
343 .compatible = "marvell,pxa3xx-nand",
344 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
345 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300346 {
347 .compatible = "marvell,armada370-nand",
348 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
349 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300350 {}
351};
352MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
353
354static enum pxa3xx_nand_variant
355pxa3xx_nand_get_variant(struct platform_device *pdev)
356{
357 const struct of_device_id *of_id =
358 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
359 if (!of_id)
360 return PXA3XX_NAND_VARIANT_PXA;
361 return (enum pxa3xx_nand_variant)of_id->data;
362}
363
Lei Wend4568822011-07-14 20:44:32 -0700364static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200365 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800366{
Lei Wend4568822011-07-14 20:44:32 -0700367 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800368 unsigned long nand_clk = clk_get_rate(info->clk);
369 uint32_t ndtr0, ndtr1;
370
371 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
372 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
373 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
374 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
375 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
376 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
377
378 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
379 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
380 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
381
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300382 info->ndtr0cs0 = ndtr0;
383 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800384 nand_writel(info, NDTR0CS0, ndtr0);
385 nand_writel(info, NDTR1CS0, ndtr1);
386}
387
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300388/*
389 * Set the data and OOB size, depending on the selected
390 * spare and ECC configuration.
391 * Only applicable to READ0, READOOB and PAGEPROG commands.
392 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300393static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
394 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800395{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300396 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800397
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300398 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300399 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800400 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800401
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300402 info->oob_size = info->spare_size;
403 if (!info->use_ecc)
404 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800405}
406
Lei Wenf8155a42011-02-28 10:32:11 +0800407/**
408 * NOTE: it is a must to set ND_RUN firstly, then write
409 * command buffer, otherwise, it does not work.
410 * We enable all the interrupt at the same time, and
411 * let pxa3xx_nand_irq to handle all logic.
412 */
413static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
414{
415 uint32_t ndcr;
416
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300417 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300418
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300419 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300420 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300421 if (info->ecc_bch)
422 nand_writel(info, NDECCCTRL, 0x1);
423 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300424 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300425 if (info->ecc_bch)
426 nand_writel(info, NDECCCTRL, 0x0);
427 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300428
429 if (info->use_dma)
430 ndcr |= NDCR_DMA_EN;
431 else
432 ndcr &= ~NDCR_DMA_EN;
433
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300434 if (info->use_spare)
435 ndcr |= NDCR_SPARE_EN;
436 else
437 ndcr &= ~NDCR_SPARE_EN;
438
Lei Wenf8155a42011-02-28 10:32:11 +0800439 ndcr |= NDCR_ND_RUN;
440
441 /* clear status bits and run */
442 nand_writel(info, NDCR, 0);
443 nand_writel(info, NDSR, NDSR_MASK);
444 nand_writel(info, NDCR, ndcr);
445}
446
447static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
448{
449 uint32_t ndcr;
450 int timeout = NAND_STOP_DELAY;
451
452 /* wait RUN bit in NDCR become 0 */
453 ndcr = nand_readl(info, NDCR);
454 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
455 ndcr = nand_readl(info, NDCR);
456 udelay(1);
457 }
458
459 if (timeout <= 0) {
460 ndcr &= ~NDCR_ND_RUN;
461 nand_writel(info, NDCR, ndcr);
462 }
463 /* clear status bits */
464 nand_writel(info, NDSR, NDSR_MASK);
465}
466
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300467static void __maybe_unused
468enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800469{
470 uint32_t ndcr;
471
472 ndcr = nand_readl(info, NDCR);
473 nand_writel(info, NDCR, ndcr & ~int_mask);
474}
475
476static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
477{
478 uint32_t ndcr;
479
480 ndcr = nand_readl(info, NDCR);
481 nand_writel(info, NDCR, ndcr | int_mask);
482}
483
Maxime Ripard8dad0382015-02-18 11:32:07 +0100484static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
485{
486 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200487 u32 val;
488 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100489
490 /*
491 * According to the datasheet, when reading from NDDB
492 * with BCH enabled, after each 32 bytes reads, we
493 * have to make sure that the NDSR.RDDREQ bit is set.
494 *
495 * Drain the FIFO 8 32 bits reads at a time, and skip
496 * the polling on the last read.
497 */
498 while (len > 8) {
499 __raw_readsl(info->mmio_base + NDDB, data, 8);
500
Maxime Ripardafca11e2015-04-07 15:32:45 +0200501 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
502 val & NDSR_RDDREQ, 1000, 5000);
503 if (ret) {
504 dev_err(&info->pdev->dev,
505 "Timeout on RDDREQ while draining the FIFO\n");
506 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100507 }
508
509 data += 32;
510 len -= 8;
511 }
512 }
513
514 __raw_readsl(info->mmio_base + NDDB, data, len);
515}
516
Lei Wenf8155a42011-02-28 10:32:11 +0800517static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800518{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300519 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300520
eric miaofe69af02008-02-14 15:48:23 +0800521 switch (info->state) {
522 case STATE_PIO_WRITING:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300523 __raw_writesl(info->mmio_base + NDDB,
524 info->data_buff + info->data_buff_pos,
525 DIV_ROUND_UP(do_bytes, 4));
526
Lei Wen9d8b1042010-08-17 14:09:30 +0800527 if (info->oob_size > 0)
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300528 __raw_writesl(info->mmio_base + NDDB,
529 info->oob_buff + info->oob_buff_pos,
530 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800531 break;
532 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100533 drain_fifo(info,
534 info->data_buff + info->data_buff_pos,
535 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300536
Lei Wen9d8b1042010-08-17 14:09:30 +0800537 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100538 drain_fifo(info,
539 info->oob_buff + info->oob_buff_pos,
540 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800541 break;
542 default:
Lei Wenda675b42011-07-14 20:44:31 -0700543 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800544 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800545 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800546 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300547
548 /* Update buffer pointers for multi-page read/write */
549 info->data_buff_pos += do_bytes;
550 info->oob_buff_pos += info->oob_size;
551 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800552}
553
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300554#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800555static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800556{
557 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800558 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800559
560 desc->ddadr = DDADR_STOP;
561 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
562
Lei Wenf8155a42011-02-28 10:32:11 +0800563 switch (info->state) {
564 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800565 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800566 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800567 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800568 break;
569 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800570 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800571 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800572 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800573 break;
574 default:
Lei Wenda675b42011-07-14 20:44:31 -0700575 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800576 info->state);
577 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800578 }
579
580 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
581 DDADR(info->data_dma_ch) = info->data_desc_addr;
582 DCSR(info->data_dma_ch) |= DCSR_RUN;
583}
584
585static void pxa3xx_nand_data_dma_irq(int channel, void *data)
586{
587 struct pxa3xx_nand_info *info = data;
588 uint32_t dcsr;
589
590 dcsr = DCSR(channel);
591 DCSR(channel) = dcsr;
592
593 if (dcsr & DCSR_BUSERR) {
594 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800595 }
596
Lei Wenf8155a42011-02-28 10:32:11 +0800597 info->state = STATE_DMA_DONE;
598 enable_int(info, NDCR_INT_MASK);
599 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800600}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300601#else
602static void start_data_dma(struct pxa3xx_nand_info *info)
603{}
604#endif
eric miaofe69af02008-02-14 15:48:23 +0800605
Robert Jarzmik24542252015-02-20 19:36:43 +0100606static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
607{
608 struct pxa3xx_nand_info *info = data;
609
610 handle_data_pio(info);
611
612 info->state = STATE_CMD_DONE;
613 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
614
615 return IRQ_HANDLED;
616}
617
eric miaofe69af02008-02-14 15:48:23 +0800618static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
619{
620 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300621 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700622 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100623 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700624
625 if (info->cs == 0) {
626 ready = NDSR_FLASH_RDY;
627 cmd_done = NDSR_CS0_CMDD;
628 } else {
629 ready = NDSR_RDY;
630 cmd_done = NDSR_CS1_CMDD;
631 }
eric miaofe69af02008-02-14 15:48:23 +0800632
633 status = nand_readl(info, NDSR);
634
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300635 if (status & NDSR_UNCORERR)
636 info->retcode = ERR_UNCORERR;
637 if (status & NDSR_CORERR) {
638 info->retcode = ERR_CORERR;
639 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
640 info->ecc_bch)
641 info->ecc_err_cnt = NDSR_ERR_CNT(status);
642 else
643 info->ecc_err_cnt = 1;
644
645 /*
646 * Each chunk composing a page is corrected independently,
647 * and we need to store maximum number of corrected bitflips
648 * to return it to the MTD layer in ecc.read_page().
649 */
650 info->max_bitflips = max_t(unsigned int,
651 info->max_bitflips,
652 info->ecc_err_cnt);
653 }
Lei Wenf8155a42011-02-28 10:32:11 +0800654 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
655 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800656 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800657 disable_int(info, NDCR_INT_MASK);
658 info->state = (status & NDSR_RDDREQ) ?
659 STATE_DMA_READING : STATE_DMA_WRITING;
660 start_data_dma(info);
661 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800662 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800663 info->state = (status & NDSR_RDDREQ) ?
664 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100665 ret = IRQ_WAKE_THREAD;
666 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800667 }
Lei Wenf8155a42011-02-28 10:32:11 +0800668 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700669 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800670 info->state = STATE_CMD_DONE;
671 is_completed = 1;
672 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700673 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800674 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300675 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800676 }
Lei Wenf8155a42011-02-28 10:32:11 +0800677
678 if (status & NDSR_WRCMDREQ) {
679 nand_writel(info, NDSR, NDSR_WRCMDREQ);
680 status &= ~NDSR_WRCMDREQ;
681 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300682
683 /*
684 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
685 * must be loaded by writing directly either 12 or 16
686 * bytes directly to NDCB0, four bytes at a time.
687 *
688 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
689 * but each NDCBx register can be read.
690 */
Lei Wenf8155a42011-02-28 10:32:11 +0800691 nand_writel(info, NDCB0, info->ndcb0);
692 nand_writel(info, NDCB0, info->ndcb1);
693 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300694
695 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
696 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
697 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800698 }
Lei Wenf8155a42011-02-28 10:32:11 +0800699
700 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800701 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800702 if (is_completed)
703 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300704 if (is_ready)
705 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800706NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100707 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800708}
709
eric miaofe69af02008-02-14 15:48:23 +0800710static inline int is_buf_blank(uint8_t *buf, size_t len)
711{
712 for (; len > 0; len--)
713 if (*buf++ != 0xff)
714 return 0;
715 return 1;
716}
717
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300718static void set_command_address(struct pxa3xx_nand_info *info,
719 unsigned int page_size, uint16_t column, int page_addr)
720{
721 /* small page addr setting */
722 if (page_size < PAGE_CHUNK_SIZE) {
723 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
724 | (column & 0xFF);
725
726 info->ndcb2 = 0;
727 } else {
728 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
729 | (column & 0xFFFF);
730
731 if (page_addr & 0xFF0000)
732 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
733 else
734 info->ndcb2 = 0;
735 }
736}
737
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300738static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800739{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300740 struct pxa3xx_nand_host *host = info->host[info->cs];
741 struct mtd_info *mtd = host->mtd;
742
Lei Wen4eb2da82011-02-28 10:32:13 +0800743 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800744 info->buf_start = 0;
745 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800746 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300747 info->data_buff_pos = 0;
748 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800749 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300750 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800751 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300752 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300753 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300754 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800755
756 switch (command) {
757 case NAND_CMD_READ0:
758 case NAND_CMD_PAGEPROG:
759 info->use_ecc = 1;
760 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300761 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800762 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300763 case NAND_CMD_PARAM:
764 info->use_spare = 0;
765 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800766 default:
767 info->ndcb1 = 0;
768 info->ndcb2 = 0;
769 break;
770 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300771
772 /*
773 * If we are about to issue a read command, or about to set
774 * the write address, then clean the data buffer.
775 */
776 if (command == NAND_CMD_READ0 ||
777 command == NAND_CMD_READOOB ||
778 command == NAND_CMD_SEQIN) {
779
780 info->buf_count = mtd->writesize + mtd->oobsize;
781 memset(info->data_buff, 0xFF, info->buf_count);
782 }
783
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300784}
785
786static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300787 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300788{
789 int addr_cycle, exec_cmd;
790 struct pxa3xx_nand_host *host;
791 struct mtd_info *mtd;
792
793 host = info->host[info->cs];
794 mtd = host->mtd;
795 addr_cycle = 0;
796 exec_cmd = 1;
797
798 if (info->cs != 0)
799 info->ndcb0 = NDCB0_CSEL;
800 else
801 info->ndcb0 = 0;
802
803 if (command == NAND_CMD_SEQIN)
804 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800805
Lei Wend4568822011-07-14 20:44:32 -0700806 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
807 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800808
809 switch (command) {
810 case NAND_CMD_READOOB:
811 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300812 info->buf_start = column;
813 info->ndcb0 |= NDCB0_CMD_TYPE(0)
814 | addr_cycle
815 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800816
Ezequiel Garciaec821352013-08-12 14:14:54 -0300817 if (command == NAND_CMD_READOOB)
818 info->buf_start += mtd->writesize;
819
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300820 /*
821 * Multiple page read needs an 'extended command type' field,
822 * which is either naked-read or last-read according to the
823 * state.
824 */
825 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300826 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300827 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
828 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
829 | NDCB0_LEN_OVRD
830 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
831 info->ndcb3 = info->chunk_size +
832 info->oob_size;
833 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800834
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300835 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300836 break;
837
Lei Wen4eb2da82011-02-28 10:32:13 +0800838 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800839
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300840 info->buf_start = column;
841 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300842
843 /*
844 * Multiple page programming needs to execute the initial
845 * SEQIN command that sets the page address.
846 */
847 if (mtd->writesize > PAGE_CHUNK_SIZE) {
848 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
849 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
850 | addr_cycle
851 | command;
852 /* No data transfer in this case */
853 info->data_size = 0;
854 exec_cmd = 1;
855 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800856 break;
857
858 case NAND_CMD_PAGEPROG:
859 if (is_buf_blank(info->data_buff,
860 (mtd->writesize + mtd->oobsize))) {
861 exec_cmd = 0;
862 break;
863 }
864
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300865 /* Second command setting for large pages */
866 if (mtd->writesize > PAGE_CHUNK_SIZE) {
867 /*
868 * Multiple page write uses the 'extended command'
869 * field. This can be used to issue a command dispatch
870 * or a naked-write depending on the current stage.
871 */
872 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
873 | NDCB0_LEN_OVRD
874 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
875 info->ndcb3 = info->chunk_size +
876 info->oob_size;
877
878 /*
879 * This is the command dispatch that completes a chunked
880 * page program operation.
881 */
882 if (info->data_size == 0) {
883 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
884 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
885 | command;
886 info->ndcb1 = 0;
887 info->ndcb2 = 0;
888 info->ndcb3 = 0;
889 }
890 } else {
891 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
892 | NDCB0_AUTO_RS
893 | NDCB0_ST_ROW_EN
894 | NDCB0_DBC
895 | (NAND_CMD_PAGEPROG << 8)
896 | NAND_CMD_SEQIN
897 | addr_cycle;
898 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800899 break;
900
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300901 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300902 info->buf_count = 256;
903 info->ndcb0 |= NDCB0_CMD_TYPE(0)
904 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300905 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300906 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300907 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300908 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300909 info->data_size = 256;
910 break;
911
Lei Wen4eb2da82011-02-28 10:32:13 +0800912 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700913 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800914 info->ndcb0 |= NDCB0_CMD_TYPE(3)
915 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300916 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300917 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800918
919 info->data_size = 8;
920 break;
921 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800922 info->buf_count = 1;
923 info->ndcb0 |= NDCB0_CMD_TYPE(4)
924 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300925 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800926
927 info->data_size = 8;
928 break;
929
930 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800931 info->ndcb0 |= NDCB0_CMD_TYPE(2)
932 | NDCB0_AUTO_RS
933 | NDCB0_ADDR_CYC(3)
934 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300935 | (NAND_CMD_ERASE2 << 8)
936 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800937 info->ndcb1 = page_addr;
938 info->ndcb2 = 0;
939
940 break;
941 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800942 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300943 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800944
945 break;
946
947 case NAND_CMD_ERASE2:
948 exec_cmd = 0;
949 break;
950
951 default:
952 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700953 dev_err(&info->pdev->dev, "non-supported command %x\n",
954 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800955 break;
956 }
957
958 return exec_cmd;
959}
960
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -0300961static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
962 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800963{
Lei Wend4568822011-07-14 20:44:32 -0700964 struct pxa3xx_nand_host *host = mtd->priv;
965 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -0500966 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800967
Lei Wen4eb2da82011-02-28 10:32:13 +0800968 /*
969 * if this is a x16 device ,then convert the input
970 * "byte" address into a "word" address appropriate
971 * for indexing a word-oriented device
972 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300973 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800974 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800975
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700976 /*
977 * There may be different NAND chip hooked to
978 * different chip select, so check whether
979 * chip select has been changed, if yes, reset the timing
980 */
981 if (info->cs != host->cs) {
982 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300983 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
984 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700985 }
986
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300987 prepare_start_command(info, command);
988
Lei Wend4568822011-07-14 20:44:32 -0700989 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300990 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
991
Lei Wenf8155a42011-02-28 10:32:11 +0800992 if (exec_cmd) {
993 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300994 init_completion(&info->dev_ready);
995 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800996 pxa3xx_nand_start(info);
997
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -0500998 if (!wait_for_completion_timeout(&info->cmd_complete,
999 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001000 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001001 /* Stop State Machine for next command cycle */
1002 pxa3xx_nand_stop(info);
1003 }
eric miaofe69af02008-02-14 15:48:23 +08001004 }
Lei Wend4568822011-07-14 20:44:32 -07001005 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001006}
1007
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001008static void nand_cmdfunc_extended(struct mtd_info *mtd,
1009 const unsigned command,
1010 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001011{
1012 struct pxa3xx_nand_host *host = mtd->priv;
1013 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001014 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001015
1016 /*
1017 * if this is a x16 device then convert the input
1018 * "byte" address into a "word" address appropriate
1019 * for indexing a word-oriented device
1020 */
1021 if (info->reg_ndcr & NDCR_DWIDTH_M)
1022 column /= 2;
1023
1024 /*
1025 * There may be different NAND chip hooked to
1026 * different chip select, so check whether
1027 * chip select has been changed, if yes, reset the timing
1028 */
1029 if (info->cs != host->cs) {
1030 info->cs = host->cs;
1031 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1032 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1033 }
1034
1035 /* Select the extended command for the first command */
1036 switch (command) {
1037 case NAND_CMD_READ0:
1038 case NAND_CMD_READOOB:
1039 ext_cmd_type = EXT_CMD_TYPE_MONO;
1040 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001041 case NAND_CMD_SEQIN:
1042 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1043 break;
1044 case NAND_CMD_PAGEPROG:
1045 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1046 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001047 default:
1048 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001049 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001050 }
1051
1052 prepare_start_command(info, command);
1053
1054 /*
1055 * Prepare the "is ready" completion before starting a command
1056 * transaction sequence. If the command is not executed the
1057 * completion will be completed, see below.
1058 *
1059 * We can do that inside the loop because the command variable
1060 * is invariant and thus so is the exec_cmd.
1061 */
1062 info->need_wait = 1;
1063 init_completion(&info->dev_ready);
1064 do {
1065 info->state = STATE_PREPARED;
1066 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1067 column, page_addr);
1068 if (!exec_cmd) {
1069 info->need_wait = 0;
1070 complete(&info->dev_ready);
1071 break;
1072 }
1073
1074 init_completion(&info->cmd_complete);
1075 pxa3xx_nand_start(info);
1076
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001077 if (!wait_for_completion_timeout(&info->cmd_complete,
1078 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001079 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1080 /* Stop State Machine for next command cycle */
1081 pxa3xx_nand_stop(info);
1082 break;
1083 }
1084
1085 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001086 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1087 break;
1088
1089 /*
1090 * After a splitted program command sequence has issued
1091 * the command dispatch, the command sequence is complete.
1092 */
1093 if (info->data_size == 0 &&
1094 command == NAND_CMD_PAGEPROG &&
1095 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001096 break;
1097
1098 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1099 /* Last read: issue a 'last naked read' */
1100 if (info->data_size == info->chunk_size)
1101 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1102 else
1103 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001104
1105 /*
1106 * If a splitted program command has no more data to transfer,
1107 * the command dispatch must be issued to complete.
1108 */
1109 } else if (command == NAND_CMD_PAGEPROG &&
1110 info->data_size == 0) {
1111 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001112 }
1113 } while (1);
1114
1115 info->state = STATE_IDLE;
1116}
1117
Josh Wufdbad98d2012-06-25 18:07:45 +08001118static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001119 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001120{
1121 chip->write_buf(mtd, buf, mtd->writesize);
1122 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001123
1124 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001125}
1126
1127static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001128 struct nand_chip *chip, uint8_t *buf, int oob_required,
1129 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001130{
Lei Wend4568822011-07-14 20:44:32 -07001131 struct pxa3xx_nand_host *host = mtd->priv;
1132 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001133
1134 chip->read_buf(mtd, buf, mtd->writesize);
1135 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1136
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001137 if (info->retcode == ERR_CORERR && info->use_ecc) {
1138 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1139
1140 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001141 /*
1142 * for blank page (all 0xff), HW will calculate its ECC as
1143 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001144 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001145 */
1146 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001147 info->retcode = ERR_NONE;
1148 else
Lei Wenf8155a42011-02-28 10:32:11 +08001149 mtd->ecc_stats.failed++;
1150 }
1151
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001152 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001153}
1154
eric miaofe69af02008-02-14 15:48:23 +08001155static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1156{
Lei Wend4568822011-07-14 20:44:32 -07001157 struct pxa3xx_nand_host *host = mtd->priv;
1158 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001159 char retval = 0xFF;
1160
1161 if (info->buf_start < info->buf_count)
1162 /* Has just send a new command? */
1163 retval = info->data_buff[info->buf_start++];
1164
1165 return retval;
1166}
1167
1168static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1169{
Lei Wend4568822011-07-14 20:44:32 -07001170 struct pxa3xx_nand_host *host = mtd->priv;
1171 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001172 u16 retval = 0xFFFF;
1173
1174 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1175 retval = *((u16 *)(info->data_buff+info->buf_start));
1176 info->buf_start += 2;
1177 }
1178 return retval;
1179}
1180
1181static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1182{
Lei Wend4568822011-07-14 20:44:32 -07001183 struct pxa3xx_nand_host *host = mtd->priv;
1184 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001185 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1186
1187 memcpy(buf, info->data_buff + info->buf_start, real_len);
1188 info->buf_start += real_len;
1189}
1190
1191static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1192 const uint8_t *buf, int len)
1193{
Lei Wend4568822011-07-14 20:44:32 -07001194 struct pxa3xx_nand_host *host = mtd->priv;
1195 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001196 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1197
1198 memcpy(info->data_buff + info->buf_start, buf, real_len);
1199 info->buf_start += real_len;
1200}
1201
eric miaofe69af02008-02-14 15:48:23 +08001202static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1203{
1204 return;
1205}
1206
1207static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1208{
Lei Wend4568822011-07-14 20:44:32 -07001209 struct pxa3xx_nand_host *host = mtd->priv;
1210 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001211
1212 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001213 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001214 if (!wait_for_completion_timeout(&info->dev_ready,
1215 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001216 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1217 return NAND_STATUS_FAIL;
1218 }
1219 }
eric miaofe69af02008-02-14 15:48:23 +08001220
1221 /* pxa3xx_nand_send_command has waited for command complete */
1222 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1223 if (info->retcode == ERR_NONE)
1224 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001225 else
1226 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001227 }
1228
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001229 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001230}
1231
eric miaofe69af02008-02-14 15:48:23 +08001232static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001233 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001234{
1235 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001236 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001237 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001238 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001239
Lei Wenda675b42011-07-14 20:44:31 -07001240 if (f->page_size != 2048 && f->page_size != 512) {
1241 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001242 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001243 }
eric miaofe69af02008-02-14 15:48:23 +08001244
Lei Wenda675b42011-07-14 20:44:31 -07001245 if (f->flash_width != 16 && f->flash_width != 8) {
1246 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001247 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001248 }
eric miaofe69af02008-02-14 15:48:23 +08001249
1250 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -07001251 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +08001252
1253 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001254 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001255
1256 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001257 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001258 else
Lei Wend4568822011-07-14 20:44:32 -07001259 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001260
1261 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001262 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001263 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1264 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1265 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1266 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1267
Lei Wend4568822011-07-14 20:44:32 -07001268 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +08001269 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1270
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001271 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001272
Lei Wend4568822011-07-14 20:44:32 -07001273 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001274 return 0;
1275}
1276
Mike Rapoportf2710492009-02-17 13:54:47 +02001277static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1278{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001279 /*
1280 * We set 0 by hard coding here, for we don't support keep_config
1281 * when there is more than one chip attached to the controller
1282 */
1283 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +02001284 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001285
Lei Wend4568822011-07-14 20:44:32 -07001286 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -03001287 /* Controller's FIFO size */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001288 info->chunk_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -07001289 host->read_id_bytes = 4;
1290 } else {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001291 info->chunk_size = 512;
Lei Wend4568822011-07-14 20:44:32 -07001292 host->read_id_bytes = 2;
1293 }
1294
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001295 /* Set an initial chunk size */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001296 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1297 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1298 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001299 return 0;
1300}
1301
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001302#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +08001303static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1304{
1305 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001306 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001307
1308 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001309 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001310 if (info->data_buff == NULL)
1311 return -ENOMEM;
1312 return 0;
1313 }
1314
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001315 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001316 &info->data_buff_phys, GFP_KERNEL);
1317 if (info->data_buff == NULL) {
1318 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1319 return -ENOMEM;
1320 }
1321
eric miaofe69af02008-02-14 15:48:23 +08001322 info->data_desc = (void *)info->data_buff + data_desc_offset;
1323 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1324
1325 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1326 pxa3xx_nand_data_dma_irq, info);
1327 if (info->data_dma_ch < 0) {
1328 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001329 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001330 info->data_buff, info->data_buff_phys);
1331 return info->data_dma_ch;
1332 }
1333
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001334 /*
1335 * Now that DMA buffers are allocated we turn on
1336 * DMA proper for I/O operations.
1337 */
1338 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001339 return 0;
1340}
1341
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001342static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1343{
1344 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001345 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001346 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001347 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001348 info->data_buff, info->data_buff_phys);
1349 } else {
1350 kfree(info->data_buff);
1351 }
1352}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001353#else
1354static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1355{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001356 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001357 if (info->data_buff == NULL)
1358 return -ENOMEM;
1359 return 0;
1360}
1361
1362static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1363{
1364 kfree(info->data_buff);
1365}
1366#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001367
Lei Wen401e67e2011-02-28 10:32:14 +08001368static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001369{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001370 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001371 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001372 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001373
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001374 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001375 chip = mtd->priv;
1376
Lei Wen401e67e2011-02-28 10:32:14 +08001377 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001378 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1379 if (ret)
1380 return ret;
1381
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001382 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001383 ret = chip->waitfunc(mtd, chip);
1384 if (ret & NAND_STATUS_FAIL)
1385 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001386
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001387 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001388}
eric miaofe69af02008-02-14 15:48:23 +08001389
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001390static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1391 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001392 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001393{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001394 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001395 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001396 info->spare_size = 40;
1397 info->ecc_size = 24;
1398 ecc->mode = NAND_ECC_HW;
1399 ecc->size = 512;
1400 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001401
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001402 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001403 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001404 info->spare_size = 8;
1405 info->ecc_size = 8;
1406 ecc->mode = NAND_ECC_HW;
1407 ecc->size = 512;
1408 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001409
Brian Norris6033a942013-11-14 14:41:32 -08001410 /*
1411 * Required ECC: 4-bit correction per 512 bytes
1412 * Select: 16-bit correction per 2048 bytes
1413 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001414 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1415 info->ecc_bch = 1;
1416 info->chunk_size = 2048;
1417 info->spare_size = 32;
1418 info->ecc_size = 32;
1419 ecc->mode = NAND_ECC_HW;
1420 ecc->size = info->chunk_size;
1421 ecc->layout = &ecc_layout_2KB_bch4bit;
1422 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001423
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001424 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001425 info->ecc_bch = 1;
1426 info->chunk_size = 2048;
1427 info->spare_size = 32;
1428 info->ecc_size = 32;
1429 ecc->mode = NAND_ECC_HW;
1430 ecc->size = info->chunk_size;
1431 ecc->layout = &ecc_layout_4KB_bch4bit;
1432 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001433
Brian Norris6033a942013-11-14 14:41:32 -08001434 /*
1435 * Required ECC: 8-bit correction per 512 bytes
1436 * Select: 16-bit correction per 1024 bytes
1437 */
1438 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001439 info->ecc_bch = 1;
1440 info->chunk_size = 1024;
1441 info->spare_size = 0;
1442 info->ecc_size = 32;
1443 ecc->mode = NAND_ECC_HW;
1444 ecc->size = info->chunk_size;
1445 ecc->layout = &ecc_layout_4KB_bch8bit;
1446 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001447 } else {
1448 dev_err(&info->pdev->dev,
1449 "ECC strength %d at page size %d is not supported\n",
1450 strength, page_size);
1451 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001452 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001453
1454 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1455 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001456 return 0;
1457}
1458
Lei Wen401e67e2011-02-28 10:32:14 +08001459static int pxa3xx_nand_scan(struct mtd_info *mtd)
1460{
Lei Wend4568822011-07-14 20:44:32 -07001461 struct pxa3xx_nand_host *host = mtd->priv;
1462 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001463 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001464 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001465 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001466 const struct pxa3xx_nand_flash *f = NULL;
1467 struct nand_chip *chip = mtd->priv;
1468 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001469 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001470 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001471 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001472
1473 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001474 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001475
1476 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001477 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001478 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1479 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001480
Lei Wend4568822011-07-14 20:44:32 -07001481 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001482 }
1483
1484 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1485 id = *((uint16_t *)(info->data_buff));
1486 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001487 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001488 else {
Lei Wenda675b42011-07-14 20:44:31 -07001489 dev_warn(&info->pdev->dev,
1490 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001491
1492 return -EINVAL;
1493 }
1494
1495 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1496 for (i = 0; i < num; i++) {
1497 if (i < pdata->num_flash)
1498 f = pdata->flash + i;
1499 else
1500 f = &builtin_flash_types[i - pdata->num_flash + 1];
1501
1502 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001503 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001504 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001505 }
1506
Lei Wen4332c112011-03-03 11:27:01 +08001507 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001508 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001509
1510 return -EINVAL;
1511 }
1512
Lei Wend4568822011-07-14 20:44:32 -07001513 ret = pxa3xx_nand_config_flash(info, f);
1514 if (ret) {
1515 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1516 return ret;
1517 }
1518
Antoine Ténart7c2f7172015-02-12 15:53:27 +01001519 memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids));
1520
Lei Wen4332c112011-03-03 11:27:01 +08001521 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001522 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001523 pxa3xx_flash_ids[0].pagesize = f->page_size;
1524 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1525 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1526 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1527 if (f->flash_width == 16)
1528 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001529 pxa3xx_flash_ids[1].name = NULL;
1530 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001531KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001532 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001533 chip->options |= NAND_BUSWIDTH_16;
1534
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001535 /* Device detection must be done with ECC disabled */
1536 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1537 nand_writel(info, NDECCCTRL, 0x0);
1538
Lei Wen0fab0282011-06-07 03:01:06 -07001539 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001540 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001541
1542 if (pdata->flash_bbt) {
1543 /*
1544 * We'll use a bad block table stored in-flash and don't
1545 * allow writing the bad block marker to the flash.
1546 */
1547 chip->bbt_options |= NAND_BBT_USE_FLASH |
1548 NAND_BBT_NO_OOB_BBM;
1549 chip->bbt_td = &bbt_main_descr;
1550 chip->bbt_md = &bbt_mirror_descr;
1551 }
1552
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001553 /*
1554 * If the page size is bigger than the FIFO size, let's check
1555 * we are given the right variant and then switch to the extended
1556 * (aka splitted) command handling,
1557 */
1558 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1559 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1560 chip->cmdfunc = nand_cmdfunc_extended;
1561 } else {
1562 dev_err(&info->pdev->dev,
1563 "unsupported page size on this variant\n");
1564 return -ENODEV;
1565 }
1566 }
1567
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001568 if (pdata->ecc_strength && pdata->ecc_step_size) {
1569 ecc_strength = pdata->ecc_strength;
1570 ecc_step = pdata->ecc_step_size;
1571 } else {
1572 ecc_strength = chip->ecc_strength_ds;
1573 ecc_step = chip->ecc_step_ds;
1574 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001575
1576 /* Set default ECC strength requirements on non-ONFI devices */
1577 if (ecc_strength < 1 && ecc_step < 1) {
1578 ecc_strength = 1;
1579 ecc_step = 512;
1580 }
1581
1582 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1583 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001584 if (ret)
1585 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001586
Lei Wen4332c112011-03-03 11:27:01 +08001587 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001588 if (mtd->writesize >= 2048)
1589 host->col_addr_cycles = 2;
1590 else
1591 host->col_addr_cycles = 1;
1592
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001593 /* release the initial buffer */
1594 kfree(info->data_buff);
1595
1596 /* allocate the real data + oob buffer */
1597 info->buf_size = mtd->writesize + mtd->oobsize;
1598 ret = pxa3xx_nand_init_buff(info);
1599 if (ret)
1600 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001601 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001602
Lei Wen4332c112011-03-03 11:27:01 +08001603 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001604 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001605 else
Lei Wend4568822011-07-14 20:44:32 -07001606 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001607 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001608}
1609
Lei Wend4568822011-07-14 20:44:32 -07001610static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001611{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001612 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001613 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001614 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001615 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001616 struct mtd_info *mtd;
1617 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001618 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001619
Jingoo Han453810b2013-07-30 17:18:33 +09001620 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001621 if (pdata->num_cs <= 0)
1622 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001623 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1624 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1625 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001626 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001627
eric miaofe69af02008-02-14 15:48:23 +08001628 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001629 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001630 for (cs = 0; cs < pdata->num_cs; cs++) {
1631 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1632 (sizeof(*mtd) + sizeof(*host)) * cs);
1633 chip = (struct nand_chip *)(&mtd[1]);
1634 host = (struct pxa3xx_nand_host *)chip;
1635 info->host[cs] = host;
1636 host->mtd = mtd;
1637 host->cs = cs;
1638 host->info_data = info;
1639 mtd->priv = host;
1640 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001641
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001642 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1643 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1644 chip->controller = &info->controller;
1645 chip->waitfunc = pxa3xx_nand_waitfunc;
1646 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001647 chip->read_word = pxa3xx_nand_read_word;
1648 chip->read_byte = pxa3xx_nand_read_byte;
1649 chip->read_buf = pxa3xx_nand_read_buf;
1650 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001651 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001652 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001653 }
Lei Wen401e67e2011-02-28 10:32:14 +08001654
1655 spin_lock_init(&chip->controller->lock);
1656 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001657 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001658 if (IS_ERR(info->clk)) {
1659 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001660 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001661 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001662 ret = clk_prepare_enable(info->clk);
1663 if (ret < 0)
1664 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001665
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001666 if (use_dma) {
1667 /*
1668 * This is a dirty hack to make this driver work from
1669 * devicetree bindings. It can be removed once we have
1670 * a prober DMA controller framework for DT.
1671 */
1672 if (pdev->dev.of_node &&
1673 of_machine_is_compatible("marvell,pxa3xx")) {
1674 info->drcmr_dat = 97;
1675 info->drcmr_cmd = 99;
1676 } else {
1677 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1678 if (r == NULL) {
1679 dev_err(&pdev->dev,
1680 "no resource defined for data DMA\n");
1681 ret = -ENXIO;
1682 goto fail_disable_clk;
1683 }
1684 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001685
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001686 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1687 if (r == NULL) {
1688 dev_err(&pdev->dev,
1689 "no resource defined for cmd DMA\n");
1690 ret = -ENXIO;
1691 goto fail_disable_clk;
1692 }
1693 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001694 }
eric miaofe69af02008-02-14 15:48:23 +08001695 }
eric miaofe69af02008-02-14 15:48:23 +08001696
1697 irq = platform_get_irq(pdev, 0);
1698 if (irq < 0) {
1699 dev_err(&pdev->dev, "no IRQ resource defined\n");
1700 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001701 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001702 }
1703
1704 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001705 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1706 if (IS_ERR(info->mmio_base)) {
1707 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001708 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001709 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001710 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001711
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001712 /* Allocate a buffer to allow flash detection */
1713 info->buf_size = INIT_BUFFER_SIZE;
1714 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1715 if (info->data_buff == NULL) {
1716 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001717 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001718 }
eric miaofe69af02008-02-14 15:48:23 +08001719
Haojian Zhuang346e1252009-09-10 14:27:23 +08001720 /* initialize all interrupts to be disabled */
1721 disable_int(info, NDSR_MASK);
1722
Robert Jarzmik24542252015-02-20 19:36:43 +01001723 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1724 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1725 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001726 if (ret < 0) {
1727 dev_err(&pdev->dev, "failed to request IRQ\n");
1728 goto fail_free_buf;
1729 }
1730
Lei Wene353a202011-03-03 11:08:30 +08001731 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001732
Lei Wend4568822011-07-14 20:44:32 -07001733 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001734
eric miaofe69af02008-02-14 15:48:23 +08001735fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001736 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001737 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001738fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001739 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001740 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001741}
1742
1743static int pxa3xx_nand_remove(struct platform_device *pdev)
1744{
Lei Wene353a202011-03-03 11:08:30 +08001745 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001746 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001747 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001748
Lei Wend4568822011-07-14 20:44:32 -07001749 if (!info)
1750 return 0;
1751
Jingoo Han453810b2013-07-30 17:18:33 +09001752 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001753
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001754 irq = platform_get_irq(pdev, 0);
1755 if (irq >= 0)
1756 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001757 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001758
Ezequiel Garciafb320612013-04-17 13:38:12 -03001759 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001760
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001761 for (cs = 0; cs < pdata->num_cs; cs++)
1762 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001763 return 0;
1764}
1765
Daniel Mack1e7ba632012-07-22 19:51:02 +02001766static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1767{
1768 struct pxa3xx_nand_platform_data *pdata;
1769 struct device_node *np = pdev->dev.of_node;
1770 const struct of_device_id *of_id =
1771 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1772
1773 if (!of_id)
1774 return 0;
1775
1776 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1777 if (!pdata)
1778 return -ENOMEM;
1779
1780 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1781 pdata->enable_arbiter = 1;
1782 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1783 pdata->keep_config = 1;
1784 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001785 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001786
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001787 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1788 if (pdata->ecc_strength < 0)
1789 pdata->ecc_strength = 0;
1790
1791 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1792 if (pdata->ecc_step_size < 0)
1793 pdata->ecc_step_size = 0;
1794
Daniel Mack1e7ba632012-07-22 19:51:02 +02001795 pdev->dev.platform_data = pdata;
1796
1797 return 0;
1798}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001799
Lei Wene353a202011-03-03 11:08:30 +08001800static int pxa3xx_nand_probe(struct platform_device *pdev)
1801{
1802 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001803 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001804 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001805 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001806
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001807#ifndef ARCH_HAS_DMA
1808 if (use_dma) {
1809 use_dma = 0;
1810 dev_warn(&pdev->dev,
1811 "This platform can't do DMA on this device\n");
1812 }
1813#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001814 ret = pxa3xx_nand_probe_dt(pdev);
1815 if (ret)
1816 return ret;
1817
Jingoo Han453810b2013-07-30 17:18:33 +09001818 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001819 if (!pdata) {
1820 dev_err(&pdev->dev, "no platform data defined\n");
1821 return -ENODEV;
1822 }
1823
Lei Wend4568822011-07-14 20:44:32 -07001824 ret = alloc_nand_resource(pdev);
1825 if (ret) {
1826 dev_err(&pdev->dev, "alloc nand resource failed\n");
1827 return ret;
1828 }
Lei Wene353a202011-03-03 11:08:30 +08001829
Lei Wend4568822011-07-14 20:44:32 -07001830 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001831 probe_success = 0;
1832 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001833 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001834
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001835 /*
1836 * The mtd name matches the one used in 'mtdparts' kernel
1837 * parameter. This name cannot be changed or otherwise
1838 * user's mtd partitions configuration would get broken.
1839 */
1840 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001841 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001842 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001843 if (ret) {
1844 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1845 cs);
1846 continue;
1847 }
1848
Daniel Mack1e7ba632012-07-22 19:51:02 +02001849 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001850 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001851 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001852 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001853 if (!ret)
1854 probe_success = 1;
1855 }
1856
1857 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001858 pxa3xx_nand_remove(pdev);
1859 return -ENODEV;
1860 }
1861
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001862 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001863}
1864
eric miaofe69af02008-02-14 15:48:23 +08001865#ifdef CONFIG_PM
1866static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1867{
Lei Wene353a202011-03-03 11:08:30 +08001868 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001869 struct pxa3xx_nand_platform_data *pdata;
1870 struct mtd_info *mtd;
1871 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001872
Jingoo Han453810b2013-07-30 17:18:33 +09001873 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001874 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001875 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1876 return -EAGAIN;
1877 }
1878
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001879 for (cs = 0; cs < pdata->num_cs; cs++) {
1880 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001881 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001882 }
1883
eric miaofe69af02008-02-14 15:48:23 +08001884 return 0;
1885}
1886
1887static int pxa3xx_nand_resume(struct platform_device *pdev)
1888{
Lei Wene353a202011-03-03 11:08:30 +08001889 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001890 struct pxa3xx_nand_platform_data *pdata;
1891 struct mtd_info *mtd;
1892 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001893
Jingoo Han453810b2013-07-30 17:18:33 +09001894 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001895 /* We don't want to handle interrupt without calling mtd routine */
1896 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001897
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001898 /*
1899 * Directly set the chip select to a invalid value,
1900 * then the driver would reset the timing according
1901 * to current chip select at the beginning of cmdfunc
1902 */
1903 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001904
Lei Wen051fc412011-07-14 20:44:30 -07001905 /*
1906 * As the spec says, the NDSR would be updated to 0x1800 when
1907 * doing the nand_clk disable/enable.
1908 * To prevent it damaging state machine of the driver, clear
1909 * all status before resume
1910 */
1911 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001912 for (cs = 0; cs < pdata->num_cs; cs++) {
1913 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001914 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001915 }
1916
Lei Wen18c81b12010-08-17 17:25:57 +08001917 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001918}
1919#else
1920#define pxa3xx_nand_suspend NULL
1921#define pxa3xx_nand_resume NULL
1922#endif
1923
1924static struct platform_driver pxa3xx_nand_driver = {
1925 .driver = {
1926 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301927 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001928 },
1929 .probe = pxa3xx_nand_probe,
1930 .remove = pxa3xx_nand_remove,
1931 .suspend = pxa3xx_nand_suspend,
1932 .resume = pxa3xx_nand_resume,
1933};
1934
Axel Linf99640d2011-11-27 20:45:03 +08001935module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001936
1937MODULE_LICENSE("GPL");
1938MODULE_DESCRIPTION("PXA3xx NAND controller driver");