blob: 10b1f7a4fe50511e9fdac06242343f14df9cf71f [file] [log] [blame]
eric miaofe69af02008-02-14 15:48:23 +08001/*
2 * drivers/mtd/nand/pxa3xx_nand.c
3 *
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
Ezequiel Garciade484a32013-11-07 12:17:10 -030010 *
11 * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
eric miaofe69af02008-02-14 15:48:23 +080012 */
13
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080014#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080015#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/clk.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/nand.h>
23#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010024#include <linux/io.h>
25#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020027#include <linux/of.h>
28#include <linux/of_device.h>
Ezequiel Garcia776f2652013-11-14 18:25:28 -030029#include <linux/of_mtd.h>
eric miaofe69af02008-02-14 15:48:23 +080030
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030031#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
32#define ARCH_HAS_DMA
33#endif
34
35#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080036#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030037#endif
38
Arnd Bergmann293b2da2012-08-24 15:16:48 +020039#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080040
41#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080042#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080043#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080044
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030045/*
46 * Define a buffer size for the initial command that detects the flash device:
47 * STATUS, READID and PARAM. The largest of these is the PARAM command,
48 * needing 256 bytes.
49 */
50#define INIT_BUFFER_SIZE 256
51
eric miaofe69af02008-02-14 15:48:23 +080052/* registers and bit definitions */
53#define NDCR (0x00) /* Control register */
54#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
55#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
56#define NDSR (0x14) /* Status Register */
57#define NDPCR (0x18) /* Page Count Register */
58#define NDBDR0 (0x1C) /* Bad Block Register 0 */
59#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030060#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080061#define NDDB (0x40) /* Data Buffer */
62#define NDCB0 (0x48) /* Command Buffer0 */
63#define NDCB1 (0x4C) /* Command Buffer1 */
64#define NDCB2 (0x50) /* Command Buffer2 */
65
66#define NDCR_SPARE_EN (0x1 << 31)
67#define NDCR_ECC_EN (0x1 << 30)
68#define NDCR_DMA_EN (0x1 << 29)
69#define NDCR_ND_RUN (0x1 << 28)
70#define NDCR_DWIDTH_C (0x1 << 27)
71#define NDCR_DWIDTH_M (0x1 << 26)
72#define NDCR_PAGE_SZ (0x1 << 24)
73#define NDCR_NCSX (0x1 << 23)
74#define NDCR_ND_MODE (0x3 << 21)
75#define NDCR_NAND_MODE (0x0)
76#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080077#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080078#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
79#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
80
81#define NDCR_RA_START (0x1 << 15)
82#define NDCR_PG_PER_BLK (0x1 << 14)
83#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080084#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080085
86#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030087#define NDSR_ERR_CNT_OFF (16)
88#define NDSR_ERR_CNT_MASK (0x1f)
89#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080090#define NDSR_RDY (0x1 << 12)
91#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080092#define NDSR_CS0_PAGED (0x1 << 10)
93#define NDSR_CS1_PAGED (0x1 << 9)
94#define NDSR_CS0_CMDD (0x1 << 8)
95#define NDSR_CS1_CMDD (0x1 << 7)
96#define NDSR_CS0_BBD (0x1 << 6)
97#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030098#define NDSR_UNCORERR (0x1 << 4)
99#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +0800100#define NDSR_WRDREQ (0x1 << 2)
101#define NDSR_RDDREQ (0x1 << 1)
102#define NDSR_WRCMDREQ (0x1)
103
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300104#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800105#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800106#define NDCB0_AUTO_RS (0x1 << 25)
107#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300108#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
109#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800110#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
111#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
112#define NDCB0_NC (0x1 << 20)
113#define NDCB0_DBC (0x1 << 19)
114#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
115#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
116#define NDCB0_CMD2_MASK (0xff << 8)
117#define NDCB0_CMD1_MASK (0xff)
118#define NDCB0_ADDR_CYC_SHIFT (16)
119
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300120#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
121#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
122#define EXT_CMD_TYPE_READ 4 /* Read */
123#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
124#define EXT_CMD_TYPE_FINAL 3 /* Final command */
125#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
126#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
127
eric miaofe69af02008-02-14 15:48:23 +0800128/* macros for registers read/write */
129#define nand_writel(info, off, val) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200130 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800131
132#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200133 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800134
135/* error code and state */
136enum {
137 ERR_NONE = 0,
138 ERR_DMABUSERR = -1,
139 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300140 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800141 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300142 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800143};
144
145enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800146 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700147 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800148 STATE_CMD_HANDLE,
149 STATE_DMA_READING,
150 STATE_DMA_WRITING,
151 STATE_DMA_DONE,
152 STATE_PIO_READING,
153 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800154 STATE_CMD_DONE,
155 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800156};
157
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300158enum pxa3xx_nand_variant {
159 PXA3XX_NAND_VARIANT_PXA,
160 PXA3XX_NAND_VARIANT_ARMADA370,
161};
162
Lei Wend4568822011-07-14 20:44:32 -0700163struct pxa3xx_nand_host {
164 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700165 struct mtd_info *mtd;
166 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800167
Lei Wend4568822011-07-14 20:44:32 -0700168 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700169 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700170 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700171
172 /* calculated from pxa3xx_nand_flash data */
173 unsigned int col_addr_cycles;
174 unsigned int row_addr_cycles;
175 size_t read_id_bytes;
176
Lei Wend4568822011-07-14 20:44:32 -0700177};
178
179struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800180 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800181 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800182
183 struct clk *clk;
184 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800185 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300186 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800187
188 unsigned int buf_start;
189 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300190 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300191 unsigned int data_buff_pos;
192 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800193
194 /* DMA information */
195 int drcmr_dat;
196 int drcmr_cmd;
197
198 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800199 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800200 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800201 int data_dma_ch;
202 struct pxa_dma_desc *data_desc;
203 dma_addr_t data_desc_addr;
204
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700205 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800206 unsigned int state;
207
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300208 /*
209 * This driver supports NFCv1 (as found in PXA SoC)
210 * and NFCv2 (as found in Armada 370/XP SoC).
211 */
212 enum pxa3xx_nand_variant variant;
213
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700214 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800215 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300216 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800217 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300218 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300219 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800220
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300221 unsigned int data_size; /* data to be read from FIFO */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300222 unsigned int chunk_size; /* split commands chunk size */
Lei Wend4568822011-07-14 20:44:32 -0700223 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300224 unsigned int spare_size;
225 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300226 unsigned int ecc_err_cnt;
227 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800228 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800229
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300230 /* cached register value */
231 uint32_t reg_ndcr;
232 uint32_t ndtr0cs0;
233 uint32_t ndtr1cs0;
234
eric miaofe69af02008-02-14 15:48:23 +0800235 /* generated NDCBx register values */
236 uint32_t ndcb0;
237 uint32_t ndcb1;
238 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300239 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800240};
241
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030242static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800243module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300244MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800245
Lei Wenc1f82472010-08-17 13:50:23 +0800246static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800247 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
248 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
249 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
250 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800251};
252
Lei Wenc1f82472010-08-17 13:50:23 +0800253static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800254{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
255{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
256{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
257{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
258{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
259{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
260{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
261{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
262{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800263};
264
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300265static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
266static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
267
268static struct nand_bbt_descr bbt_main_descr = {
269 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
270 | NAND_BBT_2BIT | NAND_BBT_VERSION,
271 .offs = 8,
272 .len = 6,
273 .veroffs = 14,
274 .maxblocks = 8, /* Last 8 blocks in each chip */
275 .pattern = bbt_pattern
276};
277
278static struct nand_bbt_descr bbt_mirror_descr = {
279 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
280 | NAND_BBT_2BIT | NAND_BBT_VERSION,
281 .offs = 8,
282 .len = 6,
283 .veroffs = 14,
284 .maxblocks = 8, /* Last 8 blocks in each chip */
285 .pattern = bbt_mirror_pattern
286};
287
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100288static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
289 .eccbytes = 32,
290 .eccpos = {
291 32, 33, 34, 35, 36, 37, 38, 39,
292 40, 41, 42, 43, 44, 45, 46, 47,
293 48, 49, 50, 51, 52, 53, 54, 55,
294 56, 57, 58, 59, 60, 61, 62, 63},
295 .oobfree = { {2, 30} }
296};
297
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300298static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
299 .eccbytes = 64,
300 .eccpos = {
301 32, 33, 34, 35, 36, 37, 38, 39,
302 40, 41, 42, 43, 44, 45, 46, 47,
303 48, 49, 50, 51, 52, 53, 54, 55,
304 56, 57, 58, 59, 60, 61, 62, 63,
305 96, 97, 98, 99, 100, 101, 102, 103,
306 104, 105, 106, 107, 108, 109, 110, 111,
307 112, 113, 114, 115, 116, 117, 118, 119,
308 120, 121, 122, 123, 124, 125, 126, 127},
309 /* Bootrom looks in bytes 0 & 5 for bad blocks */
310 .oobfree = { {6, 26}, { 64, 32} }
311};
312
313static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
314 .eccbytes = 128,
315 .eccpos = {
316 32, 33, 34, 35, 36, 37, 38, 39,
317 40, 41, 42, 43, 44, 45, 46, 47,
318 48, 49, 50, 51, 52, 53, 54, 55,
319 56, 57, 58, 59, 60, 61, 62, 63},
320 .oobfree = { }
321};
322
Lei Wen227a8862010-08-18 18:00:03 +0800323/* Define a default flash type setting serve as flash detecting only */
324#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
325
eric miaofe69af02008-02-14 15:48:23 +0800326#define NDTR0_tCH(c) (min((c), 7) << 19)
327#define NDTR0_tCS(c) (min((c), 7) << 16)
328#define NDTR0_tWH(c) (min((c), 7) << 11)
329#define NDTR0_tWP(c) (min((c), 7) << 8)
330#define NDTR0_tRH(c) (min((c), 7) << 3)
331#define NDTR0_tRP(c) (min((c), 7) << 0)
332
333#define NDTR1_tR(c) (min((c), 65535) << 16)
334#define NDTR1_tWHR(c) (min((c), 15) << 4)
335#define NDTR1_tAR(c) (min((c), 15) << 0)
336
337/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800338#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800339
Jingoo Han17754ad2014-05-07 17:49:13 +0900340static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300341 {
342 .compatible = "marvell,pxa3xx-nand",
343 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
344 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300345 {
346 .compatible = "marvell,armada370-nand",
347 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
348 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300349 {}
350};
351MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
352
353static enum pxa3xx_nand_variant
354pxa3xx_nand_get_variant(struct platform_device *pdev)
355{
356 const struct of_device_id *of_id =
357 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
358 if (!of_id)
359 return PXA3XX_NAND_VARIANT_PXA;
360 return (enum pxa3xx_nand_variant)of_id->data;
361}
362
Lei Wend4568822011-07-14 20:44:32 -0700363static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200364 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800365{
Lei Wend4568822011-07-14 20:44:32 -0700366 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800367 unsigned long nand_clk = clk_get_rate(info->clk);
368 uint32_t ndtr0, ndtr1;
369
370 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
371 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
372 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
373 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
374 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
375 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
376
377 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
378 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
379 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
380
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300381 info->ndtr0cs0 = ndtr0;
382 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800383 nand_writel(info, NDTR0CS0, ndtr0);
384 nand_writel(info, NDTR1CS0, ndtr1);
385}
386
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300387/*
388 * Set the data and OOB size, depending on the selected
389 * spare and ECC configuration.
390 * Only applicable to READ0, READOOB and PAGEPROG commands.
391 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300392static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
393 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800394{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300395 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800396
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300397 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300398 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800399 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800400
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300401 info->oob_size = info->spare_size;
402 if (!info->use_ecc)
403 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800404}
405
Lei Wenf8155a42011-02-28 10:32:11 +0800406/**
407 * NOTE: it is a must to set ND_RUN firstly, then write
408 * command buffer, otherwise, it does not work.
409 * We enable all the interrupt at the same time, and
410 * let pxa3xx_nand_irq to handle all logic.
411 */
412static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
413{
414 uint32_t ndcr;
415
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300416 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300417
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300418 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300419 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300420 if (info->ecc_bch)
421 nand_writel(info, NDECCCTRL, 0x1);
422 } else {
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, 0x0);
426 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300427
428 if (info->use_dma)
429 ndcr |= NDCR_DMA_EN;
430 else
431 ndcr &= ~NDCR_DMA_EN;
432
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300433 if (info->use_spare)
434 ndcr |= NDCR_SPARE_EN;
435 else
436 ndcr &= ~NDCR_SPARE_EN;
437
Lei Wenf8155a42011-02-28 10:32:11 +0800438 ndcr |= NDCR_ND_RUN;
439
440 /* clear status bits and run */
441 nand_writel(info, NDCR, 0);
442 nand_writel(info, NDSR, NDSR_MASK);
443 nand_writel(info, NDCR, ndcr);
444}
445
446static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
447{
448 uint32_t ndcr;
449 int timeout = NAND_STOP_DELAY;
450
451 /* wait RUN bit in NDCR become 0 */
452 ndcr = nand_readl(info, NDCR);
453 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
454 ndcr = nand_readl(info, NDCR);
455 udelay(1);
456 }
457
458 if (timeout <= 0) {
459 ndcr &= ~NDCR_ND_RUN;
460 nand_writel(info, NDCR, ndcr);
461 }
462 /* clear status bits */
463 nand_writel(info, NDSR, NDSR_MASK);
464}
465
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300466static void __maybe_unused
467enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800468{
469 uint32_t ndcr;
470
471 ndcr = nand_readl(info, NDCR);
472 nand_writel(info, NDCR, ndcr & ~int_mask);
473}
474
475static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
476{
477 uint32_t ndcr;
478
479 ndcr = nand_readl(info, NDCR);
480 nand_writel(info, NDCR, ndcr | int_mask);
481}
482
Maxime Ripard8dad0382015-02-18 11:32:07 +0100483static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
484{
485 if (info->ecc_bch) {
486 int timeout;
487
488 /*
489 * According to the datasheet, when reading from NDDB
490 * with BCH enabled, after each 32 bytes reads, we
491 * have to make sure that the NDSR.RDDREQ bit is set.
492 *
493 * Drain the FIFO 8 32 bits reads at a time, and skip
494 * the polling on the last read.
495 */
496 while (len > 8) {
497 __raw_readsl(info->mmio_base + NDDB, data, 8);
498
499 for (timeout = 0;
500 !(nand_readl(info, NDSR) & NDSR_RDDREQ);
501 timeout++) {
502 if (timeout >= 5) {
503 dev_err(&info->pdev->dev,
504 "Timeout on RDDREQ while draining the FIFO\n");
505 return;
506 }
507
508 mdelay(1);
509 }
510
511 data += 32;
512 len -= 8;
513 }
514 }
515
516 __raw_readsl(info->mmio_base + NDDB, data, len);
517}
518
Lei Wenf8155a42011-02-28 10:32:11 +0800519static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800520{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300521 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300522
eric miaofe69af02008-02-14 15:48:23 +0800523 switch (info->state) {
524 case STATE_PIO_WRITING:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300525 __raw_writesl(info->mmio_base + NDDB,
526 info->data_buff + info->data_buff_pos,
527 DIV_ROUND_UP(do_bytes, 4));
528
Lei Wen9d8b1042010-08-17 14:09:30 +0800529 if (info->oob_size > 0)
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300530 __raw_writesl(info->mmio_base + NDDB,
531 info->oob_buff + info->oob_buff_pos,
532 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800533 break;
534 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100535 drain_fifo(info,
536 info->data_buff + info->data_buff_pos,
537 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300538
Lei Wen9d8b1042010-08-17 14:09:30 +0800539 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100540 drain_fifo(info,
541 info->oob_buff + info->oob_buff_pos,
542 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800543 break;
544 default:
Lei Wenda675b42011-07-14 20:44:31 -0700545 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800546 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800547 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800548 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300549
550 /* Update buffer pointers for multi-page read/write */
551 info->data_buff_pos += do_bytes;
552 info->oob_buff_pos += info->oob_size;
553 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800554}
555
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300556#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800557static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800558{
559 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800560 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800561
562 desc->ddadr = DDADR_STOP;
563 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
564
Lei Wenf8155a42011-02-28 10:32:11 +0800565 switch (info->state) {
566 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800567 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800568 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800569 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800570 break;
571 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800572 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800573 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800574 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800575 break;
576 default:
Lei Wenda675b42011-07-14 20:44:31 -0700577 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800578 info->state);
579 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800580 }
581
582 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
583 DDADR(info->data_dma_ch) = info->data_desc_addr;
584 DCSR(info->data_dma_ch) |= DCSR_RUN;
585}
586
587static void pxa3xx_nand_data_dma_irq(int channel, void *data)
588{
589 struct pxa3xx_nand_info *info = data;
590 uint32_t dcsr;
591
592 dcsr = DCSR(channel);
593 DCSR(channel) = dcsr;
594
595 if (dcsr & DCSR_BUSERR) {
596 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800597 }
598
Lei Wenf8155a42011-02-28 10:32:11 +0800599 info->state = STATE_DMA_DONE;
600 enable_int(info, NDCR_INT_MASK);
601 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800602}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300603#else
604static void start_data_dma(struct pxa3xx_nand_info *info)
605{}
606#endif
eric miaofe69af02008-02-14 15:48:23 +0800607
608static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
609{
610 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300611 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700612 unsigned int ready, cmd_done;
613
614 if (info->cs == 0) {
615 ready = NDSR_FLASH_RDY;
616 cmd_done = NDSR_CS0_CMDD;
617 } else {
618 ready = NDSR_RDY;
619 cmd_done = NDSR_CS1_CMDD;
620 }
eric miaofe69af02008-02-14 15:48:23 +0800621
622 status = nand_readl(info, NDSR);
623
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300624 if (status & NDSR_UNCORERR)
625 info->retcode = ERR_UNCORERR;
626 if (status & NDSR_CORERR) {
627 info->retcode = ERR_CORERR;
628 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
629 info->ecc_bch)
630 info->ecc_err_cnt = NDSR_ERR_CNT(status);
631 else
632 info->ecc_err_cnt = 1;
633
634 /*
635 * Each chunk composing a page is corrected independently,
636 * and we need to store maximum number of corrected bitflips
637 * to return it to the MTD layer in ecc.read_page().
638 */
639 info->max_bitflips = max_t(unsigned int,
640 info->max_bitflips,
641 info->ecc_err_cnt);
642 }
Lei Wenf8155a42011-02-28 10:32:11 +0800643 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
644 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800645 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800646 disable_int(info, NDCR_INT_MASK);
647 info->state = (status & NDSR_RDDREQ) ?
648 STATE_DMA_READING : STATE_DMA_WRITING;
649 start_data_dma(info);
650 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800651 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800652 info->state = (status & NDSR_RDDREQ) ?
653 STATE_PIO_READING : STATE_PIO_WRITING;
654 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800655 }
Lei Wenf8155a42011-02-28 10:32:11 +0800656 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700657 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800658 info->state = STATE_CMD_DONE;
659 is_completed = 1;
660 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700661 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800662 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300663 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800664 }
Lei Wenf8155a42011-02-28 10:32:11 +0800665
666 if (status & NDSR_WRCMDREQ) {
667 nand_writel(info, NDSR, NDSR_WRCMDREQ);
668 status &= ~NDSR_WRCMDREQ;
669 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300670
671 /*
672 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
673 * must be loaded by writing directly either 12 or 16
674 * bytes directly to NDCB0, four bytes at a time.
675 *
676 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
677 * but each NDCBx register can be read.
678 */
Lei Wenf8155a42011-02-28 10:32:11 +0800679 nand_writel(info, NDCB0, info->ndcb0);
680 nand_writel(info, NDCB0, info->ndcb1);
681 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300682
683 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
684 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
685 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800686 }
Lei Wenf8155a42011-02-28 10:32:11 +0800687
688 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800689 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800690 if (is_completed)
691 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300692 if (is_ready)
693 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800694NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800695 return IRQ_HANDLED;
696}
697
eric miaofe69af02008-02-14 15:48:23 +0800698static inline int is_buf_blank(uint8_t *buf, size_t len)
699{
700 for (; len > 0; len--)
701 if (*buf++ != 0xff)
702 return 0;
703 return 1;
704}
705
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300706static void set_command_address(struct pxa3xx_nand_info *info,
707 unsigned int page_size, uint16_t column, int page_addr)
708{
709 /* small page addr setting */
710 if (page_size < PAGE_CHUNK_SIZE) {
711 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
712 | (column & 0xFF);
713
714 info->ndcb2 = 0;
715 } else {
716 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
717 | (column & 0xFFFF);
718
719 if (page_addr & 0xFF0000)
720 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
721 else
722 info->ndcb2 = 0;
723 }
724}
725
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300726static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800727{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300728 struct pxa3xx_nand_host *host = info->host[info->cs];
729 struct mtd_info *mtd = host->mtd;
730
Lei Wen4eb2da82011-02-28 10:32:13 +0800731 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800732 info->buf_start = 0;
733 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800734 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300735 info->data_buff_pos = 0;
736 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800737 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300738 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800739 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300740 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300741 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300742 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800743
744 switch (command) {
745 case NAND_CMD_READ0:
746 case NAND_CMD_PAGEPROG:
747 info->use_ecc = 1;
748 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300749 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800750 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300751 case NAND_CMD_PARAM:
752 info->use_spare = 0;
753 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800754 default:
755 info->ndcb1 = 0;
756 info->ndcb2 = 0;
757 break;
758 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300759
760 /*
761 * If we are about to issue a read command, or about to set
762 * the write address, then clean the data buffer.
763 */
764 if (command == NAND_CMD_READ0 ||
765 command == NAND_CMD_READOOB ||
766 command == NAND_CMD_SEQIN) {
767
768 info->buf_count = mtd->writesize + mtd->oobsize;
769 memset(info->data_buff, 0xFF, info->buf_count);
770 }
771
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300772}
773
774static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300775 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300776{
777 int addr_cycle, exec_cmd;
778 struct pxa3xx_nand_host *host;
779 struct mtd_info *mtd;
780
781 host = info->host[info->cs];
782 mtd = host->mtd;
783 addr_cycle = 0;
784 exec_cmd = 1;
785
786 if (info->cs != 0)
787 info->ndcb0 = NDCB0_CSEL;
788 else
789 info->ndcb0 = 0;
790
791 if (command == NAND_CMD_SEQIN)
792 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800793
Lei Wend4568822011-07-14 20:44:32 -0700794 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
795 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800796
797 switch (command) {
798 case NAND_CMD_READOOB:
799 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300800 info->buf_start = column;
801 info->ndcb0 |= NDCB0_CMD_TYPE(0)
802 | addr_cycle
803 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800804
Ezequiel Garciaec821352013-08-12 14:14:54 -0300805 if (command == NAND_CMD_READOOB)
806 info->buf_start += mtd->writesize;
807
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300808 /*
809 * Multiple page read needs an 'extended command type' field,
810 * which is either naked-read or last-read according to the
811 * state.
812 */
813 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300814 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300815 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
816 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
817 | NDCB0_LEN_OVRD
818 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
819 info->ndcb3 = info->chunk_size +
820 info->oob_size;
821 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800822
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300823 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300824 break;
825
Lei Wen4eb2da82011-02-28 10:32:13 +0800826 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800827
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300828 info->buf_start = column;
829 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300830
831 /*
832 * Multiple page programming needs to execute the initial
833 * SEQIN command that sets the page address.
834 */
835 if (mtd->writesize > PAGE_CHUNK_SIZE) {
836 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
837 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
838 | addr_cycle
839 | command;
840 /* No data transfer in this case */
841 info->data_size = 0;
842 exec_cmd = 1;
843 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800844 break;
845
846 case NAND_CMD_PAGEPROG:
847 if (is_buf_blank(info->data_buff,
848 (mtd->writesize + mtd->oobsize))) {
849 exec_cmd = 0;
850 break;
851 }
852
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300853 /* Second command setting for large pages */
854 if (mtd->writesize > PAGE_CHUNK_SIZE) {
855 /*
856 * Multiple page write uses the 'extended command'
857 * field. This can be used to issue a command dispatch
858 * or a naked-write depending on the current stage.
859 */
860 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
861 | NDCB0_LEN_OVRD
862 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
863 info->ndcb3 = info->chunk_size +
864 info->oob_size;
865
866 /*
867 * This is the command dispatch that completes a chunked
868 * page program operation.
869 */
870 if (info->data_size == 0) {
871 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
872 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
873 | command;
874 info->ndcb1 = 0;
875 info->ndcb2 = 0;
876 info->ndcb3 = 0;
877 }
878 } else {
879 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
880 | NDCB0_AUTO_RS
881 | NDCB0_ST_ROW_EN
882 | NDCB0_DBC
883 | (NAND_CMD_PAGEPROG << 8)
884 | NAND_CMD_SEQIN
885 | addr_cycle;
886 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800887 break;
888
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300889 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300890 info->buf_count = 256;
891 info->ndcb0 |= NDCB0_CMD_TYPE(0)
892 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300893 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300894 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300895 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300896 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300897 info->data_size = 256;
898 break;
899
Lei Wen4eb2da82011-02-28 10:32:13 +0800900 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700901 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800902 info->ndcb0 |= NDCB0_CMD_TYPE(3)
903 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300904 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300905 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800906
907 info->data_size = 8;
908 break;
909 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800910 info->buf_count = 1;
911 info->ndcb0 |= NDCB0_CMD_TYPE(4)
912 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300913 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800914
915 info->data_size = 8;
916 break;
917
918 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800919 info->ndcb0 |= NDCB0_CMD_TYPE(2)
920 | NDCB0_AUTO_RS
921 | NDCB0_ADDR_CYC(3)
922 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300923 | (NAND_CMD_ERASE2 << 8)
924 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800925 info->ndcb1 = page_addr;
926 info->ndcb2 = 0;
927
928 break;
929 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800930 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300931 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800932
933 break;
934
935 case NAND_CMD_ERASE2:
936 exec_cmd = 0;
937 break;
938
939 default:
940 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700941 dev_err(&info->pdev->dev, "non-supported command %x\n",
942 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800943 break;
944 }
945
946 return exec_cmd;
947}
948
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -0300949static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
950 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800951{
Lei Wend4568822011-07-14 20:44:32 -0700952 struct pxa3xx_nand_host *host = mtd->priv;
953 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800954 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800955
Lei Wen4eb2da82011-02-28 10:32:13 +0800956 /*
957 * if this is a x16 device ,then convert the input
958 * "byte" address into a "word" address appropriate
959 * for indexing a word-oriented device
960 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300961 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800962 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800963
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700964 /*
965 * There may be different NAND chip hooked to
966 * different chip select, so check whether
967 * chip select has been changed, if yes, reset the timing
968 */
969 if (info->cs != host->cs) {
970 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300971 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
972 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700973 }
974
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300975 prepare_start_command(info, command);
976
Lei Wend4568822011-07-14 20:44:32 -0700977 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300978 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
979
Lei Wenf8155a42011-02-28 10:32:11 +0800980 if (exec_cmd) {
981 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300982 init_completion(&info->dev_ready);
983 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800984 pxa3xx_nand_start(info);
985
986 ret = wait_for_completion_timeout(&info->cmd_complete,
987 CHIP_DELAY_TIMEOUT);
988 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700989 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800990 /* Stop State Machine for next command cycle */
991 pxa3xx_nand_stop(info);
992 }
eric miaofe69af02008-02-14 15:48:23 +0800993 }
Lei Wend4568822011-07-14 20:44:32 -0700994 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800995}
996
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -0300997static void nand_cmdfunc_extended(struct mtd_info *mtd,
998 const unsigned command,
999 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001000{
1001 struct pxa3xx_nand_host *host = mtd->priv;
1002 struct pxa3xx_nand_info *info = host->info_data;
1003 int ret, exec_cmd, ext_cmd_type;
1004
1005 /*
1006 * if this is a x16 device then convert the input
1007 * "byte" address into a "word" address appropriate
1008 * for indexing a word-oriented device
1009 */
1010 if (info->reg_ndcr & NDCR_DWIDTH_M)
1011 column /= 2;
1012
1013 /*
1014 * There may be different NAND chip hooked to
1015 * different chip select, so check whether
1016 * chip select has been changed, if yes, reset the timing
1017 */
1018 if (info->cs != host->cs) {
1019 info->cs = host->cs;
1020 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1021 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1022 }
1023
1024 /* Select the extended command for the first command */
1025 switch (command) {
1026 case NAND_CMD_READ0:
1027 case NAND_CMD_READOOB:
1028 ext_cmd_type = EXT_CMD_TYPE_MONO;
1029 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001030 case NAND_CMD_SEQIN:
1031 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1032 break;
1033 case NAND_CMD_PAGEPROG:
1034 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1035 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001036 default:
1037 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001038 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001039 }
1040
1041 prepare_start_command(info, command);
1042
1043 /*
1044 * Prepare the "is ready" completion before starting a command
1045 * transaction sequence. If the command is not executed the
1046 * completion will be completed, see below.
1047 *
1048 * We can do that inside the loop because the command variable
1049 * is invariant and thus so is the exec_cmd.
1050 */
1051 info->need_wait = 1;
1052 init_completion(&info->dev_ready);
1053 do {
1054 info->state = STATE_PREPARED;
1055 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1056 column, page_addr);
1057 if (!exec_cmd) {
1058 info->need_wait = 0;
1059 complete(&info->dev_ready);
1060 break;
1061 }
1062
1063 init_completion(&info->cmd_complete);
1064 pxa3xx_nand_start(info);
1065
1066 ret = wait_for_completion_timeout(&info->cmd_complete,
1067 CHIP_DELAY_TIMEOUT);
1068 if (!ret) {
1069 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1070 /* Stop State Machine for next command cycle */
1071 pxa3xx_nand_stop(info);
1072 break;
1073 }
1074
1075 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001076 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1077 break;
1078
1079 /*
1080 * After a splitted program command sequence has issued
1081 * the command dispatch, the command sequence is complete.
1082 */
1083 if (info->data_size == 0 &&
1084 command == NAND_CMD_PAGEPROG &&
1085 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001086 break;
1087
1088 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1089 /* Last read: issue a 'last naked read' */
1090 if (info->data_size == info->chunk_size)
1091 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1092 else
1093 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001094
1095 /*
1096 * If a splitted program command has no more data to transfer,
1097 * the command dispatch must be issued to complete.
1098 */
1099 } else if (command == NAND_CMD_PAGEPROG &&
1100 info->data_size == 0) {
1101 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001102 }
1103 } while (1);
1104
1105 info->state = STATE_IDLE;
1106}
1107
Josh Wufdbad98d2012-06-25 18:07:45 +08001108static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001109 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001110{
1111 chip->write_buf(mtd, buf, mtd->writesize);
1112 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001113
1114 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001115}
1116
1117static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001118 struct nand_chip *chip, uint8_t *buf, int oob_required,
1119 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001120{
Lei Wend4568822011-07-14 20:44:32 -07001121 struct pxa3xx_nand_host *host = mtd->priv;
1122 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001123
1124 chip->read_buf(mtd, buf, mtd->writesize);
1125 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1126
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001127 if (info->retcode == ERR_CORERR && info->use_ecc) {
1128 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1129
1130 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001131 /*
1132 * for blank page (all 0xff), HW will calculate its ECC as
1133 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001134 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001135 */
1136 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001137 info->retcode = ERR_NONE;
1138 else
Lei Wenf8155a42011-02-28 10:32:11 +08001139 mtd->ecc_stats.failed++;
1140 }
1141
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001142 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001143}
1144
eric miaofe69af02008-02-14 15:48:23 +08001145static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1146{
Lei Wend4568822011-07-14 20:44:32 -07001147 struct pxa3xx_nand_host *host = mtd->priv;
1148 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001149 char retval = 0xFF;
1150
1151 if (info->buf_start < info->buf_count)
1152 /* Has just send a new command? */
1153 retval = info->data_buff[info->buf_start++];
1154
1155 return retval;
1156}
1157
1158static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1159{
Lei Wend4568822011-07-14 20:44:32 -07001160 struct pxa3xx_nand_host *host = mtd->priv;
1161 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001162 u16 retval = 0xFFFF;
1163
1164 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1165 retval = *((u16 *)(info->data_buff+info->buf_start));
1166 info->buf_start += 2;
1167 }
1168 return retval;
1169}
1170
1171static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1172{
Lei Wend4568822011-07-14 20:44:32 -07001173 struct pxa3xx_nand_host *host = mtd->priv;
1174 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001175 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1176
1177 memcpy(buf, info->data_buff + info->buf_start, real_len);
1178 info->buf_start += real_len;
1179}
1180
1181static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1182 const uint8_t *buf, int len)
1183{
Lei Wend4568822011-07-14 20:44:32 -07001184 struct pxa3xx_nand_host *host = mtd->priv;
1185 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001186 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1187
1188 memcpy(info->data_buff + info->buf_start, buf, real_len);
1189 info->buf_start += real_len;
1190}
1191
eric miaofe69af02008-02-14 15:48:23 +08001192static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1193{
1194 return;
1195}
1196
1197static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1198{
Lei Wend4568822011-07-14 20:44:32 -07001199 struct pxa3xx_nand_host *host = mtd->priv;
1200 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001201 int ret;
1202
1203 if (info->need_wait) {
1204 ret = wait_for_completion_timeout(&info->dev_ready,
1205 CHIP_DELAY_TIMEOUT);
1206 info->need_wait = 0;
1207 if (!ret) {
1208 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1209 return NAND_STATUS_FAIL;
1210 }
1211 }
eric miaofe69af02008-02-14 15:48:23 +08001212
1213 /* pxa3xx_nand_send_command has waited for command complete */
1214 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1215 if (info->retcode == ERR_NONE)
1216 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001217 else
1218 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001219 }
1220
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001221 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001222}
1223
eric miaofe69af02008-02-14 15:48:23 +08001224static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001225 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001226{
1227 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001228 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001229 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001230 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001231
Lei Wenda675b42011-07-14 20:44:31 -07001232 if (f->page_size != 2048 && f->page_size != 512) {
1233 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001234 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001235 }
eric miaofe69af02008-02-14 15:48:23 +08001236
Lei Wenda675b42011-07-14 20:44:31 -07001237 if (f->flash_width != 16 && f->flash_width != 8) {
1238 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001239 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001240 }
eric miaofe69af02008-02-14 15:48:23 +08001241
1242 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -07001243 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +08001244
1245 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001246 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001247
1248 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001249 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001250 else
Lei Wend4568822011-07-14 20:44:32 -07001251 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001252
1253 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001254 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001255 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1256 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1257 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1258 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1259
Lei Wend4568822011-07-14 20:44:32 -07001260 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +08001261 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1262
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001263 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001264
Lei Wend4568822011-07-14 20:44:32 -07001265 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001266 return 0;
1267}
1268
Mike Rapoportf2710492009-02-17 13:54:47 +02001269static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1270{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001271 /*
1272 * We set 0 by hard coding here, for we don't support keep_config
1273 * when there is more than one chip attached to the controller
1274 */
1275 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +02001276 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001277
Lei Wend4568822011-07-14 20:44:32 -07001278 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -03001279 /* Controller's FIFO size */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001280 info->chunk_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -07001281 host->read_id_bytes = 4;
1282 } else {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001283 info->chunk_size = 512;
Lei Wend4568822011-07-14 20:44:32 -07001284 host->read_id_bytes = 2;
1285 }
1286
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001287 /* Set an initial chunk size */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001288 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1289 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1290 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001291 return 0;
1292}
1293
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001294#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +08001295static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1296{
1297 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001298 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001299
1300 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001301 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001302 if (info->data_buff == NULL)
1303 return -ENOMEM;
1304 return 0;
1305 }
1306
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001307 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001308 &info->data_buff_phys, GFP_KERNEL);
1309 if (info->data_buff == NULL) {
1310 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1311 return -ENOMEM;
1312 }
1313
eric miaofe69af02008-02-14 15:48:23 +08001314 info->data_desc = (void *)info->data_buff + data_desc_offset;
1315 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1316
1317 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1318 pxa3xx_nand_data_dma_irq, info);
1319 if (info->data_dma_ch < 0) {
1320 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001321 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001322 info->data_buff, info->data_buff_phys);
1323 return info->data_dma_ch;
1324 }
1325
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001326 /*
1327 * Now that DMA buffers are allocated we turn on
1328 * DMA proper for I/O operations.
1329 */
1330 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001331 return 0;
1332}
1333
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001334static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1335{
1336 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001337 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001338 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001339 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001340 info->data_buff, info->data_buff_phys);
1341 } else {
1342 kfree(info->data_buff);
1343 }
1344}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001345#else
1346static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1347{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001348 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001349 if (info->data_buff == NULL)
1350 return -ENOMEM;
1351 return 0;
1352}
1353
1354static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1355{
1356 kfree(info->data_buff);
1357}
1358#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001359
Lei Wen401e67e2011-02-28 10:32:14 +08001360static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001361{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001362 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001363 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001364 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001365
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001366 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001367 chip = mtd->priv;
1368
Lei Wen401e67e2011-02-28 10:32:14 +08001369 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001370 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1371 if (ret)
1372 return ret;
1373
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001374 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001375 ret = chip->waitfunc(mtd, chip);
1376 if (ret & NAND_STATUS_FAIL)
1377 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001378
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001379 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001380}
eric miaofe69af02008-02-14 15:48:23 +08001381
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001382static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1383 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001384 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001385{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001386 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001387 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001388 info->spare_size = 40;
1389 info->ecc_size = 24;
1390 ecc->mode = NAND_ECC_HW;
1391 ecc->size = 512;
1392 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001393
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001394 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001395 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001396 info->spare_size = 8;
1397 info->ecc_size = 8;
1398 ecc->mode = NAND_ECC_HW;
1399 ecc->size = 512;
1400 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001401
Brian Norris6033a942013-11-14 14:41:32 -08001402 /*
1403 * Required ECC: 4-bit correction per 512 bytes
1404 * Select: 16-bit correction per 2048 bytes
1405 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001406 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1407 info->ecc_bch = 1;
1408 info->chunk_size = 2048;
1409 info->spare_size = 32;
1410 info->ecc_size = 32;
1411 ecc->mode = NAND_ECC_HW;
1412 ecc->size = info->chunk_size;
1413 ecc->layout = &ecc_layout_2KB_bch4bit;
1414 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001415
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001416 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001417 info->ecc_bch = 1;
1418 info->chunk_size = 2048;
1419 info->spare_size = 32;
1420 info->ecc_size = 32;
1421 ecc->mode = NAND_ECC_HW;
1422 ecc->size = info->chunk_size;
1423 ecc->layout = &ecc_layout_4KB_bch4bit;
1424 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001425
Brian Norris6033a942013-11-14 14:41:32 -08001426 /*
1427 * Required ECC: 8-bit correction per 512 bytes
1428 * Select: 16-bit correction per 1024 bytes
1429 */
1430 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001431 info->ecc_bch = 1;
1432 info->chunk_size = 1024;
1433 info->spare_size = 0;
1434 info->ecc_size = 32;
1435 ecc->mode = NAND_ECC_HW;
1436 ecc->size = info->chunk_size;
1437 ecc->layout = &ecc_layout_4KB_bch8bit;
1438 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001439 } else {
1440 dev_err(&info->pdev->dev,
1441 "ECC strength %d at page size %d is not supported\n",
1442 strength, page_size);
1443 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001444 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001445
1446 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1447 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001448 return 0;
1449}
1450
Lei Wen401e67e2011-02-28 10:32:14 +08001451static int pxa3xx_nand_scan(struct mtd_info *mtd)
1452{
Lei Wend4568822011-07-14 20:44:32 -07001453 struct pxa3xx_nand_host *host = mtd->priv;
1454 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001455 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001456 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001457 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001458 const struct pxa3xx_nand_flash *f = NULL;
1459 struct nand_chip *chip = mtd->priv;
1460 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001461 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001462 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001463 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001464
1465 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001466 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001467
1468 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001469 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001470 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1471 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001472
Lei Wend4568822011-07-14 20:44:32 -07001473 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001474 }
1475
1476 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1477 id = *((uint16_t *)(info->data_buff));
1478 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001479 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001480 else {
Lei Wenda675b42011-07-14 20:44:31 -07001481 dev_warn(&info->pdev->dev,
1482 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001483
1484 return -EINVAL;
1485 }
1486
1487 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1488 for (i = 0; i < num; i++) {
1489 if (i < pdata->num_flash)
1490 f = pdata->flash + i;
1491 else
1492 f = &builtin_flash_types[i - pdata->num_flash + 1];
1493
1494 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001495 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001496 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001497 }
1498
Lei Wen4332c112011-03-03 11:27:01 +08001499 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001500 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001501
1502 return -EINVAL;
1503 }
1504
Lei Wend4568822011-07-14 20:44:32 -07001505 ret = pxa3xx_nand_config_flash(info, f);
1506 if (ret) {
1507 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1508 return ret;
1509 }
1510
Lei Wen4332c112011-03-03 11:27:01 +08001511 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001512 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001513 pxa3xx_flash_ids[0].pagesize = f->page_size;
1514 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1515 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1516 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1517 if (f->flash_width == 16)
1518 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001519 pxa3xx_flash_ids[1].name = NULL;
1520 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001521KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001522 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001523 chip->options |= NAND_BUSWIDTH_16;
1524
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001525 /* Device detection must be done with ECC disabled */
1526 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1527 nand_writel(info, NDECCCTRL, 0x0);
1528
Lei Wen0fab0282011-06-07 03:01:06 -07001529 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001530 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001531
1532 if (pdata->flash_bbt) {
1533 /*
1534 * We'll use a bad block table stored in-flash and don't
1535 * allow writing the bad block marker to the flash.
1536 */
1537 chip->bbt_options |= NAND_BBT_USE_FLASH |
1538 NAND_BBT_NO_OOB_BBM;
1539 chip->bbt_td = &bbt_main_descr;
1540 chip->bbt_md = &bbt_mirror_descr;
1541 }
1542
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001543 /*
1544 * If the page size is bigger than the FIFO size, let's check
1545 * we are given the right variant and then switch to the extended
1546 * (aka splitted) command handling,
1547 */
1548 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1549 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1550 chip->cmdfunc = nand_cmdfunc_extended;
1551 } else {
1552 dev_err(&info->pdev->dev,
1553 "unsupported page size on this variant\n");
1554 return -ENODEV;
1555 }
1556 }
1557
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001558 if (pdata->ecc_strength && pdata->ecc_step_size) {
1559 ecc_strength = pdata->ecc_strength;
1560 ecc_step = pdata->ecc_step_size;
1561 } else {
1562 ecc_strength = chip->ecc_strength_ds;
1563 ecc_step = chip->ecc_step_ds;
1564 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001565
1566 /* Set default ECC strength requirements on non-ONFI devices */
1567 if (ecc_strength < 1 && ecc_step < 1) {
1568 ecc_strength = 1;
1569 ecc_step = 512;
1570 }
1571
1572 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1573 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001574 if (ret)
1575 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001576
Lei Wen4332c112011-03-03 11:27:01 +08001577 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001578 if (mtd->writesize >= 2048)
1579 host->col_addr_cycles = 2;
1580 else
1581 host->col_addr_cycles = 1;
1582
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001583 /* release the initial buffer */
1584 kfree(info->data_buff);
1585
1586 /* allocate the real data + oob buffer */
1587 info->buf_size = mtd->writesize + mtd->oobsize;
1588 ret = pxa3xx_nand_init_buff(info);
1589 if (ret)
1590 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001591 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001592
Lei Wen4332c112011-03-03 11:27:01 +08001593 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001594 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001595 else
Lei Wend4568822011-07-14 20:44:32 -07001596 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001597 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001598}
1599
Lei Wend4568822011-07-14 20:44:32 -07001600static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001601{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001602 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001603 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001604 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001605 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001606 struct mtd_info *mtd;
1607 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001608 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001609
Jingoo Han453810b2013-07-30 17:18:33 +09001610 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001611 if (pdata->num_cs <= 0)
1612 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001613 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1614 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1615 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001616 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001617
eric miaofe69af02008-02-14 15:48:23 +08001618 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001619 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001620 for (cs = 0; cs < pdata->num_cs; cs++) {
1621 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1622 (sizeof(*mtd) + sizeof(*host)) * cs);
1623 chip = (struct nand_chip *)(&mtd[1]);
1624 host = (struct pxa3xx_nand_host *)chip;
1625 info->host[cs] = host;
1626 host->mtd = mtd;
1627 host->cs = cs;
1628 host->info_data = info;
1629 mtd->priv = host;
1630 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001631
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001632 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1633 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1634 chip->controller = &info->controller;
1635 chip->waitfunc = pxa3xx_nand_waitfunc;
1636 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001637 chip->read_word = pxa3xx_nand_read_word;
1638 chip->read_byte = pxa3xx_nand_read_byte;
1639 chip->read_buf = pxa3xx_nand_read_buf;
1640 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001641 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001642 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001643 }
Lei Wen401e67e2011-02-28 10:32:14 +08001644
1645 spin_lock_init(&chip->controller->lock);
1646 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001647 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001648 if (IS_ERR(info->clk)) {
1649 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001650 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001651 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001652 ret = clk_prepare_enable(info->clk);
1653 if (ret < 0)
1654 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001655
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001656 if (use_dma) {
1657 /*
1658 * This is a dirty hack to make this driver work from
1659 * devicetree bindings. It can be removed once we have
1660 * a prober DMA controller framework for DT.
1661 */
1662 if (pdev->dev.of_node &&
1663 of_machine_is_compatible("marvell,pxa3xx")) {
1664 info->drcmr_dat = 97;
1665 info->drcmr_cmd = 99;
1666 } else {
1667 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1668 if (r == NULL) {
1669 dev_err(&pdev->dev,
1670 "no resource defined for data DMA\n");
1671 ret = -ENXIO;
1672 goto fail_disable_clk;
1673 }
1674 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001675
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001676 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1677 if (r == NULL) {
1678 dev_err(&pdev->dev,
1679 "no resource defined for cmd DMA\n");
1680 ret = -ENXIO;
1681 goto fail_disable_clk;
1682 }
1683 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001684 }
eric miaofe69af02008-02-14 15:48:23 +08001685 }
eric miaofe69af02008-02-14 15:48:23 +08001686
1687 irq = platform_get_irq(pdev, 0);
1688 if (irq < 0) {
1689 dev_err(&pdev->dev, "no IRQ resource defined\n");
1690 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001691 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001692 }
1693
1694 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001695 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1696 if (IS_ERR(info->mmio_base)) {
1697 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001698 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001699 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001700 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001701
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001702 /* Allocate a buffer to allow flash detection */
1703 info->buf_size = INIT_BUFFER_SIZE;
1704 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1705 if (info->data_buff == NULL) {
1706 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001707 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001708 }
eric miaofe69af02008-02-14 15:48:23 +08001709
Haojian Zhuang346e1252009-09-10 14:27:23 +08001710 /* initialize all interrupts to be disabled */
1711 disable_int(info, NDSR_MASK);
1712
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001713 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001714 if (ret < 0) {
1715 dev_err(&pdev->dev, "failed to request IRQ\n");
1716 goto fail_free_buf;
1717 }
1718
Lei Wene353a202011-03-03 11:08:30 +08001719 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001720
Lei Wend4568822011-07-14 20:44:32 -07001721 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001722
eric miaofe69af02008-02-14 15:48:23 +08001723fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001724 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001725 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001726fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001727 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001728 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001729}
1730
1731static int pxa3xx_nand_remove(struct platform_device *pdev)
1732{
Lei Wene353a202011-03-03 11:08:30 +08001733 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001734 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001735 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001736
Lei Wend4568822011-07-14 20:44:32 -07001737 if (!info)
1738 return 0;
1739
Jingoo Han453810b2013-07-30 17:18:33 +09001740 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001741
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001742 irq = platform_get_irq(pdev, 0);
1743 if (irq >= 0)
1744 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001745 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001746
Ezequiel Garciafb320612013-04-17 13:38:12 -03001747 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001748
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001749 for (cs = 0; cs < pdata->num_cs; cs++)
1750 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001751 return 0;
1752}
1753
Daniel Mack1e7ba632012-07-22 19:51:02 +02001754static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1755{
1756 struct pxa3xx_nand_platform_data *pdata;
1757 struct device_node *np = pdev->dev.of_node;
1758 const struct of_device_id *of_id =
1759 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1760
1761 if (!of_id)
1762 return 0;
1763
1764 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1765 if (!pdata)
1766 return -ENOMEM;
1767
1768 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1769 pdata->enable_arbiter = 1;
1770 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1771 pdata->keep_config = 1;
1772 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001773 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001774
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001775 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1776 if (pdata->ecc_strength < 0)
1777 pdata->ecc_strength = 0;
1778
1779 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1780 if (pdata->ecc_step_size < 0)
1781 pdata->ecc_step_size = 0;
1782
Daniel Mack1e7ba632012-07-22 19:51:02 +02001783 pdev->dev.platform_data = pdata;
1784
1785 return 0;
1786}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001787
Lei Wene353a202011-03-03 11:08:30 +08001788static int pxa3xx_nand_probe(struct platform_device *pdev)
1789{
1790 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001791 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001792 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001793 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001794
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001795#ifndef ARCH_HAS_DMA
1796 if (use_dma) {
1797 use_dma = 0;
1798 dev_warn(&pdev->dev,
1799 "This platform can't do DMA on this device\n");
1800 }
1801#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001802 ret = pxa3xx_nand_probe_dt(pdev);
1803 if (ret)
1804 return ret;
1805
Jingoo Han453810b2013-07-30 17:18:33 +09001806 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001807 if (!pdata) {
1808 dev_err(&pdev->dev, "no platform data defined\n");
1809 return -ENODEV;
1810 }
1811
Lei Wend4568822011-07-14 20:44:32 -07001812 ret = alloc_nand_resource(pdev);
1813 if (ret) {
1814 dev_err(&pdev->dev, "alloc nand resource failed\n");
1815 return ret;
1816 }
Lei Wene353a202011-03-03 11:08:30 +08001817
Lei Wend4568822011-07-14 20:44:32 -07001818 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001819 probe_success = 0;
1820 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001821 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001822
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001823 /*
1824 * The mtd name matches the one used in 'mtdparts' kernel
1825 * parameter. This name cannot be changed or otherwise
1826 * user's mtd partitions configuration would get broken.
1827 */
1828 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001829 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001830 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001831 if (ret) {
1832 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1833 cs);
1834 continue;
1835 }
1836
Daniel Mack1e7ba632012-07-22 19:51:02 +02001837 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001838 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001839 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001840 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001841 if (!ret)
1842 probe_success = 1;
1843 }
1844
1845 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001846 pxa3xx_nand_remove(pdev);
1847 return -ENODEV;
1848 }
1849
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001850 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001851}
1852
eric miaofe69af02008-02-14 15:48:23 +08001853#ifdef CONFIG_PM
1854static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1855{
Lei Wene353a202011-03-03 11:08:30 +08001856 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001857 struct pxa3xx_nand_platform_data *pdata;
1858 struct mtd_info *mtd;
1859 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001860
Jingoo Han453810b2013-07-30 17:18:33 +09001861 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001862 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001863 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1864 return -EAGAIN;
1865 }
1866
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001867 for (cs = 0; cs < pdata->num_cs; cs++) {
1868 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001869 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001870 }
1871
eric miaofe69af02008-02-14 15:48:23 +08001872 return 0;
1873}
1874
1875static int pxa3xx_nand_resume(struct platform_device *pdev)
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;
Lei Wen051fc412011-07-14 20:44:30 -07001881
Jingoo Han453810b2013-07-30 17:18:33 +09001882 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001883 /* We don't want to handle interrupt without calling mtd routine */
1884 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001885
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001886 /*
1887 * Directly set the chip select to a invalid value,
1888 * then the driver would reset the timing according
1889 * to current chip select at the beginning of cmdfunc
1890 */
1891 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001892
Lei Wen051fc412011-07-14 20:44:30 -07001893 /*
1894 * As the spec says, the NDSR would be updated to 0x1800 when
1895 * doing the nand_clk disable/enable.
1896 * To prevent it damaging state machine of the driver, clear
1897 * all status before resume
1898 */
1899 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001900 for (cs = 0; cs < pdata->num_cs; cs++) {
1901 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001902 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001903 }
1904
Lei Wen18c81b12010-08-17 17:25:57 +08001905 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001906}
1907#else
1908#define pxa3xx_nand_suspend NULL
1909#define pxa3xx_nand_resume NULL
1910#endif
1911
1912static struct platform_driver pxa3xx_nand_driver = {
1913 .driver = {
1914 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301915 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001916 },
1917 .probe = pxa3xx_nand_probe,
1918 .remove = pxa3xx_nand_remove,
1919 .suspend = pxa3xx_nand_suspend,
1920 .resume = pxa3xx_nand_resume,
1921};
1922
Axel Linf99640d2011-11-27 20:45:03 +08001923module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001924
1925MODULE_LICENSE("GPL");
1926MODULE_DESCRIPTION("PXA3xx NAND controller driver");