blob: 740983a3462644a306f8839929b40a21525b29dd [file] [log] [blame]
eric miaofe69af02008-02-14 15:48:23 +08001/*
2 * drivers/mtd/nand/pxa3xx_nand.c
3 *
4 * Copyright © 2005 Intel Corporation
5 * Copyright © 2006 Marvell International Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
Ezequiel Garciade484a32013-11-07 12:17:10 -030010 *
11 * See Documentation/mtd/nand/pxa3xx-nand.txt for more details.
eric miaofe69af02008-02-14 15:48:23 +080012 */
13
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +080014#include <linux/kernel.h>
eric miaofe69af02008-02-14 15:48:23 +080015#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/clk.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/nand.h>
23#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010024#include <linux/io.h>
Maxime Ripardafca11e2015-04-07 15:32:45 +020025#include <linux/iopoll.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010026#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020028#include <linux/of.h>
29#include <linux/of_device.h>
Ezequiel Garcia776f2652013-11-14 18:25:28 -030030#include <linux/of_mtd.h>
eric miaofe69af02008-02-14 15:48:23 +080031
Rob Herringce914e62015-04-30 15:17:47 -050032#if defined(CONFIG_ARM) && (defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP))
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030033#define ARCH_HAS_DMA
34#endif
35
36#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080037#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030038#endif
39
Arnd Bergmann293b2da2012-08-24 15:16:48 +020040#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080041
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -050042#define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200)
43#define NAND_STOP_DELAY msecs_to_jiffies(40)
Lei Wen4eb2da82011-02-28 10:32:13 +080044#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080045
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030046/*
47 * Define a buffer size for the initial command that detects the flash device:
Ezequiel Garciac1634092015-08-03 11:31:26 -030048 * STATUS, READID and PARAM.
49 * ONFI param page is 256 bytes, and there are three redundant copies
50 * to be read. JEDEC param page is 512 bytes, and there are also three
51 * redundant copies to be read.
52 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030053 */
Ezequiel Garciac1634092015-08-03 11:31:26 -030054#define INIT_BUFFER_SIZE 2048
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030055
eric miaofe69af02008-02-14 15:48:23 +080056/* registers and bit definitions */
57#define NDCR (0x00) /* Control register */
58#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
59#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
60#define NDSR (0x14) /* Status Register */
61#define NDPCR (0x18) /* Page Count Register */
62#define NDBDR0 (0x1C) /* Bad Block Register 0 */
63#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030064#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080065#define NDDB (0x40) /* Data Buffer */
66#define NDCB0 (0x48) /* Command Buffer0 */
67#define NDCB1 (0x4C) /* Command Buffer1 */
68#define NDCB2 (0x50) /* Command Buffer2 */
69
70#define NDCR_SPARE_EN (0x1 << 31)
71#define NDCR_ECC_EN (0x1 << 30)
72#define NDCR_DMA_EN (0x1 << 29)
73#define NDCR_ND_RUN (0x1 << 28)
74#define NDCR_DWIDTH_C (0x1 << 27)
75#define NDCR_DWIDTH_M (0x1 << 26)
76#define NDCR_PAGE_SZ (0x1 << 24)
77#define NDCR_NCSX (0x1 << 23)
78#define NDCR_ND_MODE (0x3 << 21)
79#define NDCR_NAND_MODE (0x0)
80#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080081#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080082#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
83#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
84
85#define NDCR_RA_START (0x1 << 15)
86#define NDCR_PG_PER_BLK (0x1 << 14)
87#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080088#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080089
90#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030091#define NDSR_ERR_CNT_OFF (16)
92#define NDSR_ERR_CNT_MASK (0x1f)
93#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080094#define NDSR_RDY (0x1 << 12)
95#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080096#define NDSR_CS0_PAGED (0x1 << 10)
97#define NDSR_CS1_PAGED (0x1 << 9)
98#define NDSR_CS0_CMDD (0x1 << 8)
99#define NDSR_CS1_CMDD (0x1 << 7)
100#define NDSR_CS0_BBD (0x1 << 6)
101#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300102#define NDSR_UNCORERR (0x1 << 4)
103#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +0800104#define NDSR_WRDREQ (0x1 << 2)
105#define NDSR_RDDREQ (0x1 << 1)
106#define NDSR_WRCMDREQ (0x1)
107
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300108#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800109#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800110#define NDCB0_AUTO_RS (0x1 << 25)
111#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300112#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
113#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800114#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
115#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
116#define NDCB0_NC (0x1 << 20)
117#define NDCB0_DBC (0x1 << 19)
118#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
119#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
120#define NDCB0_CMD2_MASK (0xff << 8)
121#define NDCB0_CMD1_MASK (0xff)
122#define NDCB0_ADDR_CYC_SHIFT (16)
123
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300124#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
125#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
126#define EXT_CMD_TYPE_READ 4 /* Read */
127#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
128#define EXT_CMD_TYPE_FINAL 3 /* Final command */
129#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
130#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
131
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300132/*
133 * This should be large enough to read 'ONFI' and 'JEDEC'.
134 * Let's use 7 bytes, which is the maximum ID count supported
135 * by the controller (see NDCR_RD_ID_CNT_MASK).
136 */
137#define READ_ID_BYTES 7
138
eric miaofe69af02008-02-14 15:48:23 +0800139/* macros for registers read/write */
140#define nand_writel(info, off, val) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200141 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800142
143#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200144 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800145
146/* error code and state */
147enum {
148 ERR_NONE = 0,
149 ERR_DMABUSERR = -1,
150 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300151 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800152 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300153 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800154};
155
156enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800157 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700158 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800159 STATE_CMD_HANDLE,
160 STATE_DMA_READING,
161 STATE_DMA_WRITING,
162 STATE_DMA_DONE,
163 STATE_PIO_READING,
164 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800165 STATE_CMD_DONE,
166 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800167};
168
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300169enum pxa3xx_nand_variant {
170 PXA3XX_NAND_VARIANT_PXA,
171 PXA3XX_NAND_VARIANT_ARMADA370,
172};
173
Lei Wend4568822011-07-14 20:44:32 -0700174struct pxa3xx_nand_host {
175 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700176 struct mtd_info *mtd;
177 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800178
Lei Wend4568822011-07-14 20:44:32 -0700179 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700180 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700181 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700182
183 /* calculated from pxa3xx_nand_flash data */
184 unsigned int col_addr_cycles;
185 unsigned int row_addr_cycles;
Lei Wend4568822011-07-14 20:44:32 -0700186};
187
188struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800189 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800190 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800191
192 struct clk *clk;
193 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800194 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300195 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800196
197 unsigned int buf_start;
198 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300199 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300200 unsigned int data_buff_pos;
201 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800202
203 /* DMA information */
204 int drcmr_dat;
205 int drcmr_cmd;
206
207 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800208 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800209 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800210 int data_dma_ch;
211 struct pxa_dma_desc *data_desc;
212 dma_addr_t data_desc_addr;
213
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700214 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800215 unsigned int state;
216
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300217 /*
218 * This driver supports NFCv1 (as found in PXA SoC)
219 * and NFCv2 (as found in Armada 370/XP SoC).
220 */
221 enum pxa3xx_nand_variant variant;
222
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700223 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800224 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300225 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800226 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300227 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300228 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800229
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300230 unsigned int data_size; /* data to be read from FIFO */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300231 unsigned int chunk_size; /* split commands chunk size */
Lei Wend4568822011-07-14 20:44:32 -0700232 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300233 unsigned int spare_size;
234 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300235 unsigned int ecc_err_cnt;
236 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800237 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800238
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300239 /* cached register value */
240 uint32_t reg_ndcr;
241 uint32_t ndtr0cs0;
242 uint32_t ndtr1cs0;
243
eric miaofe69af02008-02-14 15:48:23 +0800244 /* generated NDCBx register values */
245 uint32_t ndcb0;
246 uint32_t ndcb1;
247 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300248 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800249};
250
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030251static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800252module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300253MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800254
Lei Wenc1f82472010-08-17 13:50:23 +0800255static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800256 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
257 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
258 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
259 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800260};
261
Lei Wenc1f82472010-08-17 13:50:23 +0800262static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800263{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
264{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
265{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
266{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
267{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
268{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
269{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
270{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
271{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800272};
273
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300274static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
275static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
276
277static struct nand_bbt_descr bbt_main_descr = {
278 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
279 | NAND_BBT_2BIT | NAND_BBT_VERSION,
280 .offs = 8,
281 .len = 6,
282 .veroffs = 14,
283 .maxblocks = 8, /* Last 8 blocks in each chip */
284 .pattern = bbt_pattern
285};
286
287static struct nand_bbt_descr bbt_mirror_descr = {
288 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
289 | NAND_BBT_2BIT | NAND_BBT_VERSION,
290 .offs = 8,
291 .len = 6,
292 .veroffs = 14,
293 .maxblocks = 8, /* Last 8 blocks in each chip */
294 .pattern = bbt_mirror_pattern
295};
296
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100297static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
298 .eccbytes = 32,
299 .eccpos = {
300 32, 33, 34, 35, 36, 37, 38, 39,
301 40, 41, 42, 43, 44, 45, 46, 47,
302 48, 49, 50, 51, 52, 53, 54, 55,
303 56, 57, 58, 59, 60, 61, 62, 63},
304 .oobfree = { {2, 30} }
305};
306
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300307static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
308 .eccbytes = 64,
309 .eccpos = {
310 32, 33, 34, 35, 36, 37, 38, 39,
311 40, 41, 42, 43, 44, 45, 46, 47,
312 48, 49, 50, 51, 52, 53, 54, 55,
313 56, 57, 58, 59, 60, 61, 62, 63,
314 96, 97, 98, 99, 100, 101, 102, 103,
315 104, 105, 106, 107, 108, 109, 110, 111,
316 112, 113, 114, 115, 116, 117, 118, 119,
317 120, 121, 122, 123, 124, 125, 126, 127},
318 /* Bootrom looks in bytes 0 & 5 for bad blocks */
319 .oobfree = { {6, 26}, { 64, 32} }
320};
321
322static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
323 .eccbytes = 128,
324 .eccpos = {
325 32, 33, 34, 35, 36, 37, 38, 39,
326 40, 41, 42, 43, 44, 45, 46, 47,
327 48, 49, 50, 51, 52, 53, 54, 55,
328 56, 57, 58, 59, 60, 61, 62, 63},
329 .oobfree = { }
330};
331
Lei Wen227a8862010-08-18 18:00:03 +0800332/* Define a default flash type setting serve as flash detecting only */
333#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
334
eric miaofe69af02008-02-14 15:48:23 +0800335#define NDTR0_tCH(c) (min((c), 7) << 19)
336#define NDTR0_tCS(c) (min((c), 7) << 16)
337#define NDTR0_tWH(c) (min((c), 7) << 11)
338#define NDTR0_tWP(c) (min((c), 7) << 8)
339#define NDTR0_tRH(c) (min((c), 7) << 3)
340#define NDTR0_tRP(c) (min((c), 7) << 0)
341
342#define NDTR1_tR(c) (min((c), 65535) << 16)
343#define NDTR1_tWHR(c) (min((c), 15) << 4)
344#define NDTR1_tAR(c) (min((c), 15) << 0)
345
346/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800347#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800348
Jingoo Han17754ad2014-05-07 17:49:13 +0900349static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300350 {
351 .compatible = "marvell,pxa3xx-nand",
352 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
353 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300354 {
355 .compatible = "marvell,armada370-nand",
356 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
357 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300358 {}
359};
360MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
361
362static enum pxa3xx_nand_variant
363pxa3xx_nand_get_variant(struct platform_device *pdev)
364{
365 const struct of_device_id *of_id =
366 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
367 if (!of_id)
368 return PXA3XX_NAND_VARIANT_PXA;
369 return (enum pxa3xx_nand_variant)of_id->data;
370}
371
Lei Wend4568822011-07-14 20:44:32 -0700372static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200373 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800374{
Lei Wend4568822011-07-14 20:44:32 -0700375 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800376 unsigned long nand_clk = clk_get_rate(info->clk);
377 uint32_t ndtr0, ndtr1;
378
379 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
380 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
381 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
382 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
383 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
384 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
385
386 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
387 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
388 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
389
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300390 info->ndtr0cs0 = ndtr0;
391 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800392 nand_writel(info, NDTR0CS0, ndtr0);
393 nand_writel(info, NDTR1CS0, ndtr1);
394}
395
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300396/*
397 * Set the data and OOB size, depending on the selected
398 * spare and ECC configuration.
399 * Only applicable to READ0, READOOB and PAGEPROG commands.
400 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300401static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
402 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800403{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300404 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800405
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300406 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300407 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800408 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800409
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300410 info->oob_size = info->spare_size;
411 if (!info->use_ecc)
412 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800413}
414
Lei Wenf8155a42011-02-28 10:32:11 +0800415/**
416 * NOTE: it is a must to set ND_RUN firstly, then write
417 * command buffer, otherwise, it does not work.
418 * We enable all the interrupt at the same time, and
419 * let pxa3xx_nand_irq to handle all logic.
420 */
421static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
422{
423 uint32_t ndcr;
424
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300425 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300426
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300427 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300428 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300429 if (info->ecc_bch)
430 nand_writel(info, NDECCCTRL, 0x1);
431 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300432 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300433 if (info->ecc_bch)
434 nand_writel(info, NDECCCTRL, 0x0);
435 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300436
437 if (info->use_dma)
438 ndcr |= NDCR_DMA_EN;
439 else
440 ndcr &= ~NDCR_DMA_EN;
441
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300442 if (info->use_spare)
443 ndcr |= NDCR_SPARE_EN;
444 else
445 ndcr &= ~NDCR_SPARE_EN;
446
Lei Wenf8155a42011-02-28 10:32:11 +0800447 ndcr |= NDCR_ND_RUN;
448
449 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800450 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200451 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800452 nand_writel(info, NDCR, ndcr);
453}
454
455static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
456{
457 uint32_t ndcr;
458 int timeout = NAND_STOP_DELAY;
459
460 /* wait RUN bit in NDCR become 0 */
461 ndcr = nand_readl(info, NDCR);
462 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
463 ndcr = nand_readl(info, NDCR);
464 udelay(1);
465 }
466
467 if (timeout <= 0) {
468 ndcr &= ~NDCR_ND_RUN;
469 nand_writel(info, NDCR, ndcr);
470 }
471 /* clear status bits */
472 nand_writel(info, NDSR, NDSR_MASK);
473}
474
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300475static void __maybe_unused
476enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800477{
478 uint32_t ndcr;
479
480 ndcr = nand_readl(info, NDCR);
481 nand_writel(info, NDCR, ndcr & ~int_mask);
482}
483
484static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
485{
486 uint32_t ndcr;
487
488 ndcr = nand_readl(info, NDCR);
489 nand_writel(info, NDCR, ndcr | int_mask);
490}
491
Maxime Ripard8dad0382015-02-18 11:32:07 +0100492static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
493{
494 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200495 u32 val;
496 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100497
498 /*
499 * According to the datasheet, when reading from NDDB
500 * with BCH enabled, after each 32 bytes reads, we
501 * have to make sure that the NDSR.RDDREQ bit is set.
502 *
503 * Drain the FIFO 8 32 bits reads at a time, and skip
504 * the polling on the last read.
505 */
506 while (len > 8) {
Rob Herringce914e62015-04-30 15:17:47 -0500507 readsl(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100508
Maxime Ripardafca11e2015-04-07 15:32:45 +0200509 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
510 val & NDSR_RDDREQ, 1000, 5000);
511 if (ret) {
512 dev_err(&info->pdev->dev,
513 "Timeout on RDDREQ while draining the FIFO\n");
514 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100515 }
516
517 data += 32;
518 len -= 8;
519 }
520 }
521
Rob Herringce914e62015-04-30 15:17:47 -0500522 readsl(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100523}
524
Lei Wenf8155a42011-02-28 10:32:11 +0800525static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800526{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300527 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300528
eric miaofe69af02008-02-14 15:48:23 +0800529 switch (info->state) {
530 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500531 writesl(info->mmio_base + NDDB,
532 info->data_buff + info->data_buff_pos,
533 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300534
Lei Wen9d8b1042010-08-17 14:09:30 +0800535 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500536 writesl(info->mmio_base + NDDB,
537 info->oob_buff + info->oob_buff_pos,
538 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800539 break;
540 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100541 drain_fifo(info,
542 info->data_buff + info->data_buff_pos,
543 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300544
Lei Wen9d8b1042010-08-17 14:09:30 +0800545 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100546 drain_fifo(info,
547 info->oob_buff + info->oob_buff_pos,
548 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800549 break;
550 default:
Lei Wenda675b42011-07-14 20:44:31 -0700551 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800552 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800553 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800554 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300555
556 /* Update buffer pointers for multi-page read/write */
557 info->data_buff_pos += do_bytes;
558 info->oob_buff_pos += info->oob_size;
559 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800560}
561
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300562#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800563static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800564{
565 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800566 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800567
568 desc->ddadr = DDADR_STOP;
569 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
570
Lei Wenf8155a42011-02-28 10:32:11 +0800571 switch (info->state) {
572 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800573 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800574 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800575 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800576 break;
577 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800578 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800579 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800580 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800581 break;
582 default:
Lei Wenda675b42011-07-14 20:44:31 -0700583 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800584 info->state);
585 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800586 }
587
588 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
589 DDADR(info->data_dma_ch) = info->data_desc_addr;
590 DCSR(info->data_dma_ch) |= DCSR_RUN;
591}
592
593static void pxa3xx_nand_data_dma_irq(int channel, void *data)
594{
595 struct pxa3xx_nand_info *info = data;
596 uint32_t dcsr;
597
598 dcsr = DCSR(channel);
599 DCSR(channel) = dcsr;
600
601 if (dcsr & DCSR_BUSERR) {
602 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800603 }
604
Lei Wenf8155a42011-02-28 10:32:11 +0800605 info->state = STATE_DMA_DONE;
606 enable_int(info, NDCR_INT_MASK);
607 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800608}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300609#else
610static void start_data_dma(struct pxa3xx_nand_info *info)
611{}
612#endif
eric miaofe69af02008-02-14 15:48:23 +0800613
Robert Jarzmik24542252015-02-20 19:36:43 +0100614static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
615{
616 struct pxa3xx_nand_info *info = data;
617
618 handle_data_pio(info);
619
620 info->state = STATE_CMD_DONE;
621 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
622
623 return IRQ_HANDLED;
624}
625
eric miaofe69af02008-02-14 15:48:23 +0800626static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
627{
628 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300629 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700630 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100631 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700632
633 if (info->cs == 0) {
634 ready = NDSR_FLASH_RDY;
635 cmd_done = NDSR_CS0_CMDD;
636 } else {
637 ready = NDSR_RDY;
638 cmd_done = NDSR_CS1_CMDD;
639 }
eric miaofe69af02008-02-14 15:48:23 +0800640
641 status = nand_readl(info, NDSR);
642
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300643 if (status & NDSR_UNCORERR)
644 info->retcode = ERR_UNCORERR;
645 if (status & NDSR_CORERR) {
646 info->retcode = ERR_CORERR;
647 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
648 info->ecc_bch)
649 info->ecc_err_cnt = NDSR_ERR_CNT(status);
650 else
651 info->ecc_err_cnt = 1;
652
653 /*
654 * Each chunk composing a page is corrected independently,
655 * and we need to store maximum number of corrected bitflips
656 * to return it to the MTD layer in ecc.read_page().
657 */
658 info->max_bitflips = max_t(unsigned int,
659 info->max_bitflips,
660 info->ecc_err_cnt);
661 }
Lei Wenf8155a42011-02-28 10:32:11 +0800662 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
663 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800664 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800665 disable_int(info, NDCR_INT_MASK);
666 info->state = (status & NDSR_RDDREQ) ?
667 STATE_DMA_READING : STATE_DMA_WRITING;
668 start_data_dma(info);
669 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800670 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800671 info->state = (status & NDSR_RDDREQ) ?
672 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100673 ret = IRQ_WAKE_THREAD;
674 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800675 }
Lei Wenf8155a42011-02-28 10:32:11 +0800676 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700677 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800678 info->state = STATE_CMD_DONE;
679 is_completed = 1;
680 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700681 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800682 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300683 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800684 }
Lei Wenf8155a42011-02-28 10:32:11 +0800685
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200686 /*
687 * Clear all status bit before issuing the next command, which
688 * can and will alter the status bits and will deserve a new
689 * interrupt on its own. This lets the controller exit the IRQ
690 */
691 nand_writel(info, NDSR, status);
692
Lei Wenf8155a42011-02-28 10:32:11 +0800693 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800694 status &= ~NDSR_WRCMDREQ;
695 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300696
697 /*
698 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
699 * must be loaded by writing directly either 12 or 16
700 * bytes directly to NDCB0, four bytes at a time.
701 *
702 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
703 * but each NDCBx register can be read.
704 */
Lei Wenf8155a42011-02-28 10:32:11 +0800705 nand_writel(info, NDCB0, info->ndcb0);
706 nand_writel(info, NDCB0, info->ndcb1);
707 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300708
709 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
710 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
711 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800712 }
Lei Wenf8155a42011-02-28 10:32:11 +0800713
Lei Wenf8155a42011-02-28 10:32:11 +0800714 if (is_completed)
715 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300716 if (is_ready)
717 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800718NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100719 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800720}
721
eric miaofe69af02008-02-14 15:48:23 +0800722static inline int is_buf_blank(uint8_t *buf, size_t len)
723{
724 for (; len > 0; len--)
725 if (*buf++ != 0xff)
726 return 0;
727 return 1;
728}
729
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300730static void set_command_address(struct pxa3xx_nand_info *info,
731 unsigned int page_size, uint16_t column, int page_addr)
732{
733 /* small page addr setting */
734 if (page_size < PAGE_CHUNK_SIZE) {
735 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
736 | (column & 0xFF);
737
738 info->ndcb2 = 0;
739 } else {
740 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
741 | (column & 0xFFFF);
742
743 if (page_addr & 0xFF0000)
744 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
745 else
746 info->ndcb2 = 0;
747 }
748}
749
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300750static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800751{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300752 struct pxa3xx_nand_host *host = info->host[info->cs];
753 struct mtd_info *mtd = host->mtd;
754
Lei Wen4eb2da82011-02-28 10:32:13 +0800755 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800756 info->buf_start = 0;
757 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800758 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300759 info->data_buff_pos = 0;
760 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800761 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300762 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800763 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300764 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300765 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300766 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800767
768 switch (command) {
769 case NAND_CMD_READ0:
770 case NAND_CMD_PAGEPROG:
771 info->use_ecc = 1;
772 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300773 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800774 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300775 case NAND_CMD_PARAM:
776 info->use_spare = 0;
777 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800778 default:
779 info->ndcb1 = 0;
780 info->ndcb2 = 0;
781 break;
782 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300783
784 /*
785 * If we are about to issue a read command, or about to set
786 * the write address, then clean the data buffer.
787 */
788 if (command == NAND_CMD_READ0 ||
789 command == NAND_CMD_READOOB ||
790 command == NAND_CMD_SEQIN) {
791
792 info->buf_count = mtd->writesize + mtd->oobsize;
793 memset(info->data_buff, 0xFF, info->buf_count);
794 }
795
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300796}
797
798static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300799 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300800{
801 int addr_cycle, exec_cmd;
802 struct pxa3xx_nand_host *host;
803 struct mtd_info *mtd;
804
805 host = info->host[info->cs];
806 mtd = host->mtd;
807 addr_cycle = 0;
808 exec_cmd = 1;
809
810 if (info->cs != 0)
811 info->ndcb0 = NDCB0_CSEL;
812 else
813 info->ndcb0 = 0;
814
815 if (command == NAND_CMD_SEQIN)
816 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800817
Lei Wend4568822011-07-14 20:44:32 -0700818 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
819 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800820
821 switch (command) {
822 case NAND_CMD_READOOB:
823 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300824 info->buf_start = column;
825 info->ndcb0 |= NDCB0_CMD_TYPE(0)
826 | addr_cycle
827 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800828
Ezequiel Garciaec821352013-08-12 14:14:54 -0300829 if (command == NAND_CMD_READOOB)
830 info->buf_start += mtd->writesize;
831
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300832 /*
833 * Multiple page read needs an 'extended command type' field,
834 * which is either naked-read or last-read according to the
835 * state.
836 */
837 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300838 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300839 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
840 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
841 | NDCB0_LEN_OVRD
842 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
843 info->ndcb3 = info->chunk_size +
844 info->oob_size;
845 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800846
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300847 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300848 break;
849
Lei Wen4eb2da82011-02-28 10:32:13 +0800850 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800851
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300852 info->buf_start = column;
853 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300854
855 /*
856 * Multiple page programming needs to execute the initial
857 * SEQIN command that sets the page address.
858 */
859 if (mtd->writesize > PAGE_CHUNK_SIZE) {
860 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
861 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
862 | addr_cycle
863 | command;
864 /* No data transfer in this case */
865 info->data_size = 0;
866 exec_cmd = 1;
867 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800868 break;
869
870 case NAND_CMD_PAGEPROG:
871 if (is_buf_blank(info->data_buff,
872 (mtd->writesize + mtd->oobsize))) {
873 exec_cmd = 0;
874 break;
875 }
876
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300877 /* Second command setting for large pages */
878 if (mtd->writesize > PAGE_CHUNK_SIZE) {
879 /*
880 * Multiple page write uses the 'extended command'
881 * field. This can be used to issue a command dispatch
882 * or a naked-write depending on the current stage.
883 */
884 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
885 | NDCB0_LEN_OVRD
886 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
887 info->ndcb3 = info->chunk_size +
888 info->oob_size;
889
890 /*
891 * This is the command dispatch that completes a chunked
892 * page program operation.
893 */
894 if (info->data_size == 0) {
895 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
896 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
897 | command;
898 info->ndcb1 = 0;
899 info->ndcb2 = 0;
900 info->ndcb3 = 0;
901 }
902 } else {
903 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
904 | NDCB0_AUTO_RS
905 | NDCB0_ST_ROW_EN
906 | NDCB0_DBC
907 | (NAND_CMD_PAGEPROG << 8)
908 | NAND_CMD_SEQIN
909 | addr_cycle;
910 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800911 break;
912
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300913 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -0300914 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300915 info->ndcb0 |= NDCB0_CMD_TYPE(0)
916 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300917 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300918 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300919 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -0300920 info->ndcb3 = INIT_BUFFER_SIZE;
921 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300922 break;
923
Lei Wen4eb2da82011-02-28 10:32:13 +0800924 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300925 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +0800926 info->ndcb0 |= NDCB0_CMD_TYPE(3)
927 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300928 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300929 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800930
931 info->data_size = 8;
932 break;
933 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800934 info->buf_count = 1;
935 info->ndcb0 |= NDCB0_CMD_TYPE(4)
936 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300937 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800938
939 info->data_size = 8;
940 break;
941
942 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800943 info->ndcb0 |= NDCB0_CMD_TYPE(2)
944 | NDCB0_AUTO_RS
945 | NDCB0_ADDR_CYC(3)
946 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300947 | (NAND_CMD_ERASE2 << 8)
948 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800949 info->ndcb1 = page_addr;
950 info->ndcb2 = 0;
951
952 break;
953 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800954 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300955 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800956
957 break;
958
959 case NAND_CMD_ERASE2:
960 exec_cmd = 0;
961 break;
962
963 default:
964 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700965 dev_err(&info->pdev->dev, "non-supported command %x\n",
966 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800967 break;
968 }
969
970 return exec_cmd;
971}
972
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -0300973static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
974 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800975{
Lei Wend4568822011-07-14 20:44:32 -0700976 struct pxa3xx_nand_host *host = mtd->priv;
977 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -0500978 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800979
Lei Wen4eb2da82011-02-28 10:32:13 +0800980 /*
981 * if this is a x16 device ,then convert the input
982 * "byte" address into a "word" address appropriate
983 * for indexing a word-oriented device
984 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300985 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800986 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800987
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700988 /*
989 * There may be different NAND chip hooked to
990 * different chip select, so check whether
991 * chip select has been changed, if yes, reset the timing
992 */
993 if (info->cs != host->cs) {
994 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300995 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
996 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700997 }
998
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300999 prepare_start_command(info, command);
1000
Lei Wend4568822011-07-14 20:44:32 -07001001 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001002 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1003
Lei Wenf8155a42011-02-28 10:32:11 +08001004 if (exec_cmd) {
1005 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001006 init_completion(&info->dev_ready);
1007 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001008 pxa3xx_nand_start(info);
1009
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001010 if (!wait_for_completion_timeout(&info->cmd_complete,
1011 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001012 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001013 /* Stop State Machine for next command cycle */
1014 pxa3xx_nand_stop(info);
1015 }
eric miaofe69af02008-02-14 15:48:23 +08001016 }
Lei Wend4568822011-07-14 20:44:32 -07001017 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001018}
1019
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001020static void nand_cmdfunc_extended(struct mtd_info *mtd,
1021 const unsigned command,
1022 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001023{
1024 struct pxa3xx_nand_host *host = mtd->priv;
1025 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001026 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001027
1028 /*
1029 * if this is a x16 device then convert the input
1030 * "byte" address into a "word" address appropriate
1031 * for indexing a word-oriented device
1032 */
1033 if (info->reg_ndcr & NDCR_DWIDTH_M)
1034 column /= 2;
1035
1036 /*
1037 * There may be different NAND chip hooked to
1038 * different chip select, so check whether
1039 * chip select has been changed, if yes, reset the timing
1040 */
1041 if (info->cs != host->cs) {
1042 info->cs = host->cs;
1043 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1044 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1045 }
1046
1047 /* Select the extended command for the first command */
1048 switch (command) {
1049 case NAND_CMD_READ0:
1050 case NAND_CMD_READOOB:
1051 ext_cmd_type = EXT_CMD_TYPE_MONO;
1052 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001053 case NAND_CMD_SEQIN:
1054 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1055 break;
1056 case NAND_CMD_PAGEPROG:
1057 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1058 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001059 default:
1060 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001061 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001062 }
1063
1064 prepare_start_command(info, command);
1065
1066 /*
1067 * Prepare the "is ready" completion before starting a command
1068 * transaction sequence. If the command is not executed the
1069 * completion will be completed, see below.
1070 *
1071 * We can do that inside the loop because the command variable
1072 * is invariant and thus so is the exec_cmd.
1073 */
1074 info->need_wait = 1;
1075 init_completion(&info->dev_ready);
1076 do {
1077 info->state = STATE_PREPARED;
1078 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1079 column, page_addr);
1080 if (!exec_cmd) {
1081 info->need_wait = 0;
1082 complete(&info->dev_ready);
1083 break;
1084 }
1085
1086 init_completion(&info->cmd_complete);
1087 pxa3xx_nand_start(info);
1088
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001089 if (!wait_for_completion_timeout(&info->cmd_complete,
1090 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001091 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1092 /* Stop State Machine for next command cycle */
1093 pxa3xx_nand_stop(info);
1094 break;
1095 }
1096
1097 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001098 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1099 break;
1100
1101 /*
1102 * After a splitted program command sequence has issued
1103 * the command dispatch, the command sequence is complete.
1104 */
1105 if (info->data_size == 0 &&
1106 command == NAND_CMD_PAGEPROG &&
1107 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001108 break;
1109
1110 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1111 /* Last read: issue a 'last naked read' */
1112 if (info->data_size == info->chunk_size)
1113 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1114 else
1115 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001116
1117 /*
1118 * If a splitted program command has no more data to transfer,
1119 * the command dispatch must be issued to complete.
1120 */
1121 } else if (command == NAND_CMD_PAGEPROG &&
1122 info->data_size == 0) {
1123 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001124 }
1125 } while (1);
1126
1127 info->state = STATE_IDLE;
1128}
1129
Josh Wufdbad98d2012-06-25 18:07:45 +08001130static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001131 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001132{
1133 chip->write_buf(mtd, buf, mtd->writesize);
1134 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001135
1136 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001137}
1138
1139static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001140 struct nand_chip *chip, uint8_t *buf, int oob_required,
1141 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001142{
Lei Wend4568822011-07-14 20:44:32 -07001143 struct pxa3xx_nand_host *host = mtd->priv;
1144 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001145
1146 chip->read_buf(mtd, buf, mtd->writesize);
1147 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1148
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001149 if (info->retcode == ERR_CORERR && info->use_ecc) {
1150 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1151
1152 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001153 /*
1154 * for blank page (all 0xff), HW will calculate its ECC as
1155 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001156 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001157 */
1158 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001159 info->retcode = ERR_NONE;
1160 else
Lei Wenf8155a42011-02-28 10:32:11 +08001161 mtd->ecc_stats.failed++;
1162 }
1163
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001164 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001165}
1166
eric miaofe69af02008-02-14 15:48:23 +08001167static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1168{
Lei Wend4568822011-07-14 20:44:32 -07001169 struct pxa3xx_nand_host *host = mtd->priv;
1170 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001171 char retval = 0xFF;
1172
1173 if (info->buf_start < info->buf_count)
1174 /* Has just send a new command? */
1175 retval = info->data_buff[info->buf_start++];
1176
1177 return retval;
1178}
1179
1180static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1181{
Lei Wend4568822011-07-14 20:44:32 -07001182 struct pxa3xx_nand_host *host = mtd->priv;
1183 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001184 u16 retval = 0xFFFF;
1185
1186 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1187 retval = *((u16 *)(info->data_buff+info->buf_start));
1188 info->buf_start += 2;
1189 }
1190 return retval;
1191}
1192
1193static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1194{
Lei Wend4568822011-07-14 20:44:32 -07001195 struct pxa3xx_nand_host *host = mtd->priv;
1196 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001197 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1198
1199 memcpy(buf, info->data_buff + info->buf_start, real_len);
1200 info->buf_start += real_len;
1201}
1202
1203static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1204 const uint8_t *buf, int len)
1205{
Lei Wend4568822011-07-14 20:44:32 -07001206 struct pxa3xx_nand_host *host = mtd->priv;
1207 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001208 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1209
1210 memcpy(info->data_buff + info->buf_start, buf, real_len);
1211 info->buf_start += real_len;
1212}
1213
eric miaofe69af02008-02-14 15:48:23 +08001214static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1215{
1216 return;
1217}
1218
1219static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1220{
Lei Wend4568822011-07-14 20:44:32 -07001221 struct pxa3xx_nand_host *host = mtd->priv;
1222 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001223
1224 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001225 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001226 if (!wait_for_completion_timeout(&info->dev_ready,
1227 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001228 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1229 return NAND_STATUS_FAIL;
1230 }
1231 }
eric miaofe69af02008-02-14 15:48:23 +08001232
1233 /* pxa3xx_nand_send_command has waited for command complete */
1234 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1235 if (info->retcode == ERR_NONE)
1236 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001237 else
1238 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001239 }
1240
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001241 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001242}
1243
eric miaofe69af02008-02-14 15:48:23 +08001244static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001245 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001246{
1247 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001248 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001249 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001250 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001251
Lei Wenda675b42011-07-14 20:44:31 -07001252 if (f->page_size != 2048 && f->page_size != 512) {
1253 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001254 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001255 }
eric miaofe69af02008-02-14 15:48:23 +08001256
Lei Wenda675b42011-07-14 20:44:31 -07001257 if (f->flash_width != 16 && f->flash_width != 8) {
1258 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001259 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001260 }
eric miaofe69af02008-02-14 15:48:23 +08001261
eric miaofe69af02008-02-14 15:48:23 +08001262 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001263 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001264
1265 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001266 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001267 else
Lei Wend4568822011-07-14 20:44:32 -07001268 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001269
1270 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001271 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001272 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1273 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1274 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1275 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1276
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001277 ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
eric miaofe69af02008-02-14 15:48:23 +08001278 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1279
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001280 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001281
Lei Wend4568822011-07-14 20:44:32 -07001282 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001283 return 0;
1284}
1285
Mike Rapoportf2710492009-02-17 13:54:47 +02001286static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1287{
1288 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001289
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001290 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001291 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001292 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1293 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1294 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001295 return 0;
1296}
1297
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001298#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +08001299static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1300{
1301 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001302 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001303
1304 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001305 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001306 if (info->data_buff == NULL)
1307 return -ENOMEM;
1308 return 0;
1309 }
1310
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001311 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001312 &info->data_buff_phys, GFP_KERNEL);
1313 if (info->data_buff == NULL) {
1314 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1315 return -ENOMEM;
1316 }
1317
eric miaofe69af02008-02-14 15:48:23 +08001318 info->data_desc = (void *)info->data_buff + data_desc_offset;
1319 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1320
1321 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1322 pxa3xx_nand_data_dma_irq, info);
1323 if (info->data_dma_ch < 0) {
1324 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001325 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001326 info->data_buff, info->data_buff_phys);
1327 return info->data_dma_ch;
1328 }
1329
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001330 /*
1331 * Now that DMA buffers are allocated we turn on
1332 * DMA proper for I/O operations.
1333 */
1334 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001335 return 0;
1336}
1337
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001338static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1339{
1340 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001341 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001342 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001343 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001344 info->data_buff, info->data_buff_phys);
1345 } else {
1346 kfree(info->data_buff);
1347 }
1348}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001349#else
1350static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1351{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001352 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001353 if (info->data_buff == NULL)
1354 return -ENOMEM;
1355 return 0;
1356}
1357
1358static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1359{
1360 kfree(info->data_buff);
1361}
1362#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001363
Lei Wen401e67e2011-02-28 10:32:14 +08001364static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001365{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001366 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001367 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001368 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001369
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001370 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001371 chip = mtd->priv;
1372
Lei Wen401e67e2011-02-28 10:32:14 +08001373 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001374 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1375 if (ret)
1376 return ret;
1377
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001378 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001379 ret = chip->waitfunc(mtd, chip);
1380 if (ret & NAND_STATUS_FAIL)
1381 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001382
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001383 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001384}
eric miaofe69af02008-02-14 15:48:23 +08001385
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001386static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1387 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001388 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001389{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001390 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001391 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001392 info->spare_size = 40;
1393 info->ecc_size = 24;
1394 ecc->mode = NAND_ECC_HW;
1395 ecc->size = 512;
1396 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001397
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001398 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001399 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001400 info->spare_size = 8;
1401 info->ecc_size = 8;
1402 ecc->mode = NAND_ECC_HW;
1403 ecc->size = 512;
1404 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001405
Brian Norris6033a942013-11-14 14:41:32 -08001406 /*
1407 * Required ECC: 4-bit correction per 512 bytes
1408 * Select: 16-bit correction per 2048 bytes
1409 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001410 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1411 info->ecc_bch = 1;
1412 info->chunk_size = 2048;
1413 info->spare_size = 32;
1414 info->ecc_size = 32;
1415 ecc->mode = NAND_ECC_HW;
1416 ecc->size = info->chunk_size;
1417 ecc->layout = &ecc_layout_2KB_bch4bit;
1418 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001419
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001420 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001421 info->ecc_bch = 1;
1422 info->chunk_size = 2048;
1423 info->spare_size = 32;
1424 info->ecc_size = 32;
1425 ecc->mode = NAND_ECC_HW;
1426 ecc->size = info->chunk_size;
1427 ecc->layout = &ecc_layout_4KB_bch4bit;
1428 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001429
Brian Norris6033a942013-11-14 14:41:32 -08001430 /*
1431 * Required ECC: 8-bit correction per 512 bytes
1432 * Select: 16-bit correction per 1024 bytes
1433 */
1434 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001435 info->ecc_bch = 1;
1436 info->chunk_size = 1024;
1437 info->spare_size = 0;
1438 info->ecc_size = 32;
1439 ecc->mode = NAND_ECC_HW;
1440 ecc->size = info->chunk_size;
1441 ecc->layout = &ecc_layout_4KB_bch8bit;
1442 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001443 } else {
1444 dev_err(&info->pdev->dev,
1445 "ECC strength %d at page size %d is not supported\n",
1446 strength, page_size);
1447 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001448 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001449
1450 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1451 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001452 return 0;
1453}
1454
Lei Wen401e67e2011-02-28 10:32:14 +08001455static int pxa3xx_nand_scan(struct mtd_info *mtd)
1456{
Lei Wend4568822011-07-14 20:44:32 -07001457 struct pxa3xx_nand_host *host = mtd->priv;
1458 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001459 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001460 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001461 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001462 const struct pxa3xx_nand_flash *f = NULL;
1463 struct nand_chip *chip = mtd->priv;
1464 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001465 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001466 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001467 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001468
1469 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001470 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001471
Antoine Ténartbc3e00f2015-08-18 10:59:10 +02001472 /* Set a default chunk size */
1473 info->chunk_size = 512;
1474
Lei Wen401e67e2011-02-28 10:32:14 +08001475 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001476 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001477 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1478 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001479
Lei Wend4568822011-07-14 20:44:32 -07001480 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001481 }
1482
1483 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1484 id = *((uint16_t *)(info->data_buff));
1485 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001486 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001487 else {
Lei Wenda675b42011-07-14 20:44:31 -07001488 dev_warn(&info->pdev->dev,
1489 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001490
1491 return -EINVAL;
1492 }
1493
1494 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1495 for (i = 0; i < num; i++) {
1496 if (i < pdata->num_flash)
1497 f = pdata->flash + i;
1498 else
1499 f = &builtin_flash_types[i - pdata->num_flash + 1];
1500
1501 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001502 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001503 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001504 }
1505
Lei Wen4332c112011-03-03 11:27:01 +08001506 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001507 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001508
1509 return -EINVAL;
1510 }
1511
Lei Wend4568822011-07-14 20:44:32 -07001512 ret = pxa3xx_nand_config_flash(info, f);
1513 if (ret) {
1514 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1515 return ret;
1516 }
1517
Antoine Ténart7c2f7172015-02-12 15:53:27 +01001518 memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids));
1519
Lei Wen4332c112011-03-03 11:27:01 +08001520 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001521 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001522 pxa3xx_flash_ids[0].pagesize = f->page_size;
1523 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1524 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1525 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1526 if (f->flash_width == 16)
1527 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001528 pxa3xx_flash_ids[1].name = NULL;
1529 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001530KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001531 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001532 chip->options |= NAND_BUSWIDTH_16;
1533
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001534 /* Device detection must be done with ECC disabled */
1535 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1536 nand_writel(info, NDECCCTRL, 0x0);
1537
Lei Wen0fab0282011-06-07 03:01:06 -07001538 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001539 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001540
1541 if (pdata->flash_bbt) {
1542 /*
1543 * We'll use a bad block table stored in-flash and don't
1544 * allow writing the bad block marker to the flash.
1545 */
1546 chip->bbt_options |= NAND_BBT_USE_FLASH |
1547 NAND_BBT_NO_OOB_BBM;
1548 chip->bbt_td = &bbt_main_descr;
1549 chip->bbt_md = &bbt_mirror_descr;
1550 }
1551
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001552 /*
1553 * If the page size is bigger than the FIFO size, let's check
1554 * we are given the right variant and then switch to the extended
1555 * (aka splitted) command handling,
1556 */
1557 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1558 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1559 chip->cmdfunc = nand_cmdfunc_extended;
1560 } else {
1561 dev_err(&info->pdev->dev,
1562 "unsupported page size on this variant\n");
1563 return -ENODEV;
1564 }
1565 }
1566
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001567 if (pdata->ecc_strength && pdata->ecc_step_size) {
1568 ecc_strength = pdata->ecc_strength;
1569 ecc_step = pdata->ecc_step_size;
1570 } else {
1571 ecc_strength = chip->ecc_strength_ds;
1572 ecc_step = chip->ecc_step_ds;
1573 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001574
1575 /* Set default ECC strength requirements on non-ONFI devices */
1576 if (ecc_strength < 1 && ecc_step < 1) {
1577 ecc_strength = 1;
1578 ecc_step = 512;
1579 }
1580
1581 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1582 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001583 if (ret)
1584 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001585
Lei Wen4332c112011-03-03 11:27:01 +08001586 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001587 if (mtd->writesize >= 2048)
1588 host->col_addr_cycles = 2;
1589 else
1590 host->col_addr_cycles = 1;
1591
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001592 /* release the initial buffer */
1593 kfree(info->data_buff);
1594
1595 /* allocate the real data + oob buffer */
1596 info->buf_size = mtd->writesize + mtd->oobsize;
1597 ret = pxa3xx_nand_init_buff(info);
1598 if (ret)
1599 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001600 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001601
Lei Wen4332c112011-03-03 11:27:01 +08001602 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001603 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001604 else
Lei Wend4568822011-07-14 20:44:32 -07001605 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001606 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001607}
1608
Lei Wend4568822011-07-14 20:44:32 -07001609static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001610{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001611 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001612 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001613 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001614 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001615 struct mtd_info *mtd;
1616 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001617 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001618
Jingoo Han453810b2013-07-30 17:18:33 +09001619 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001620 if (pdata->num_cs <= 0)
1621 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001622 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1623 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1624 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001625 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001626
eric miaofe69af02008-02-14 15:48:23 +08001627 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001628 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001629 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001630 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001631 chip = (struct nand_chip *)(&mtd[1]);
1632 host = (struct pxa3xx_nand_host *)chip;
1633 info->host[cs] = host;
1634 host->mtd = mtd;
1635 host->cs = cs;
1636 host->info_data = info;
1637 mtd->priv = host;
1638 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001639
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001640 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1641 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1642 chip->controller = &info->controller;
1643 chip->waitfunc = pxa3xx_nand_waitfunc;
1644 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001645 chip->read_word = pxa3xx_nand_read_word;
1646 chip->read_byte = pxa3xx_nand_read_byte;
1647 chip->read_buf = pxa3xx_nand_read_buf;
1648 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001649 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001650 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001651 }
Lei Wen401e67e2011-02-28 10:32:14 +08001652
1653 spin_lock_init(&chip->controller->lock);
1654 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001655 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001656 if (IS_ERR(info->clk)) {
1657 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001658 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001659 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001660 ret = clk_prepare_enable(info->clk);
1661 if (ret < 0)
1662 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001663
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001664 if (use_dma) {
1665 /*
1666 * This is a dirty hack to make this driver work from
1667 * devicetree bindings. It can be removed once we have
1668 * a prober DMA controller framework for DT.
1669 */
1670 if (pdev->dev.of_node &&
1671 of_machine_is_compatible("marvell,pxa3xx")) {
1672 info->drcmr_dat = 97;
1673 info->drcmr_cmd = 99;
1674 } else {
1675 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1676 if (r == NULL) {
1677 dev_err(&pdev->dev,
1678 "no resource defined for data DMA\n");
1679 ret = -ENXIO;
1680 goto fail_disable_clk;
1681 }
1682 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001683
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001684 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1685 if (r == NULL) {
1686 dev_err(&pdev->dev,
1687 "no resource defined for cmd DMA\n");
1688 ret = -ENXIO;
1689 goto fail_disable_clk;
1690 }
1691 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001692 }
eric miaofe69af02008-02-14 15:48:23 +08001693 }
eric miaofe69af02008-02-14 15:48:23 +08001694
1695 irq = platform_get_irq(pdev, 0);
1696 if (irq < 0) {
1697 dev_err(&pdev->dev, "no IRQ resource defined\n");
1698 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001699 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001700 }
1701
1702 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001703 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1704 if (IS_ERR(info->mmio_base)) {
1705 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001706 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001707 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001708 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001709
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001710 /* Allocate a buffer to allow flash detection */
1711 info->buf_size = INIT_BUFFER_SIZE;
1712 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1713 if (info->data_buff == NULL) {
1714 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001715 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001716 }
eric miaofe69af02008-02-14 15:48:23 +08001717
Haojian Zhuang346e1252009-09-10 14:27:23 +08001718 /* initialize all interrupts to be disabled */
1719 disable_int(info, NDSR_MASK);
1720
Robert Jarzmik24542252015-02-20 19:36:43 +01001721 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1722 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1723 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001724 if (ret < 0) {
1725 dev_err(&pdev->dev, "failed to request IRQ\n");
1726 goto fail_free_buf;
1727 }
1728
Lei Wene353a202011-03-03 11:08:30 +08001729 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001730
Lei Wend4568822011-07-14 20:44:32 -07001731 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001732
eric miaofe69af02008-02-14 15:48:23 +08001733fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001734 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001735 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001736fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001737 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001738 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001739}
1740
1741static int pxa3xx_nand_remove(struct platform_device *pdev)
1742{
Lei Wene353a202011-03-03 11:08:30 +08001743 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001744 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001745 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001746
Lei Wend4568822011-07-14 20:44:32 -07001747 if (!info)
1748 return 0;
1749
Jingoo Han453810b2013-07-30 17:18:33 +09001750 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001751
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001752 irq = platform_get_irq(pdev, 0);
1753 if (irq >= 0)
1754 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001755 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001756
Ezequiel Garciafb320612013-04-17 13:38:12 -03001757 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001758
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001759 for (cs = 0; cs < pdata->num_cs; cs++)
1760 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001761 return 0;
1762}
1763
Daniel Mack1e7ba632012-07-22 19:51:02 +02001764static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1765{
1766 struct pxa3xx_nand_platform_data *pdata;
1767 struct device_node *np = pdev->dev.of_node;
1768 const struct of_device_id *of_id =
1769 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1770
1771 if (!of_id)
1772 return 0;
1773
1774 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1775 if (!pdata)
1776 return -ENOMEM;
1777
1778 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1779 pdata->enable_arbiter = 1;
1780 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1781 pdata->keep_config = 1;
1782 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001783 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001784
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001785 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1786 if (pdata->ecc_strength < 0)
1787 pdata->ecc_strength = 0;
1788
1789 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1790 if (pdata->ecc_step_size < 0)
1791 pdata->ecc_step_size = 0;
1792
Daniel Mack1e7ba632012-07-22 19:51:02 +02001793 pdev->dev.platform_data = pdata;
1794
1795 return 0;
1796}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001797
Lei Wene353a202011-03-03 11:08:30 +08001798static int pxa3xx_nand_probe(struct platform_device *pdev)
1799{
1800 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001801 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001802 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001803 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001804
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001805#ifndef ARCH_HAS_DMA
1806 if (use_dma) {
1807 use_dma = 0;
1808 dev_warn(&pdev->dev,
1809 "This platform can't do DMA on this device\n");
1810 }
1811#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001812 ret = pxa3xx_nand_probe_dt(pdev);
1813 if (ret)
1814 return ret;
1815
Jingoo Han453810b2013-07-30 17:18:33 +09001816 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001817 if (!pdata) {
1818 dev_err(&pdev->dev, "no platform data defined\n");
1819 return -ENODEV;
1820 }
1821
Lei Wend4568822011-07-14 20:44:32 -07001822 ret = alloc_nand_resource(pdev);
1823 if (ret) {
1824 dev_err(&pdev->dev, "alloc nand resource failed\n");
1825 return ret;
1826 }
Lei Wene353a202011-03-03 11:08:30 +08001827
Lei Wend4568822011-07-14 20:44:32 -07001828 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001829 probe_success = 0;
1830 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001831 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001832
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001833 /*
1834 * The mtd name matches the one used in 'mtdparts' kernel
1835 * parameter. This name cannot be changed or otherwise
1836 * user's mtd partitions configuration would get broken.
1837 */
1838 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001839 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001840 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001841 if (ret) {
1842 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1843 cs);
1844 continue;
1845 }
1846
Daniel Mack1e7ba632012-07-22 19:51:02 +02001847 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001848 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001849 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001850 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001851 if (!ret)
1852 probe_success = 1;
1853 }
1854
1855 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001856 pxa3xx_nand_remove(pdev);
1857 return -ENODEV;
1858 }
1859
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001860 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001861}
1862
eric miaofe69af02008-02-14 15:48:23 +08001863#ifdef CONFIG_PM
1864static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1865{
Lei Wene353a202011-03-03 11:08:30 +08001866 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001867 struct pxa3xx_nand_platform_data *pdata;
1868 struct mtd_info *mtd;
1869 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001870
Jingoo Han453810b2013-07-30 17:18:33 +09001871 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001872 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001873 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1874 return -EAGAIN;
1875 }
1876
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001877 for (cs = 0; cs < pdata->num_cs; cs++) {
1878 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001879 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001880 }
1881
eric miaofe69af02008-02-14 15:48:23 +08001882 return 0;
1883}
1884
1885static int pxa3xx_nand_resume(struct platform_device *pdev)
1886{
Lei Wene353a202011-03-03 11:08:30 +08001887 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001888 struct pxa3xx_nand_platform_data *pdata;
1889 struct mtd_info *mtd;
1890 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001891
Jingoo Han453810b2013-07-30 17:18:33 +09001892 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001893 /* We don't want to handle interrupt without calling mtd routine */
1894 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001895
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001896 /*
1897 * Directly set the chip select to a invalid value,
1898 * then the driver would reset the timing according
1899 * to current chip select at the beginning of cmdfunc
1900 */
1901 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001902
Lei Wen051fc412011-07-14 20:44:30 -07001903 /*
1904 * As the spec says, the NDSR would be updated to 0x1800 when
1905 * doing the nand_clk disable/enable.
1906 * To prevent it damaging state machine of the driver, clear
1907 * all status before resume
1908 */
1909 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001910 for (cs = 0; cs < pdata->num_cs; cs++) {
1911 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001912 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001913 }
1914
Lei Wen18c81b12010-08-17 17:25:57 +08001915 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001916}
1917#else
1918#define pxa3xx_nand_suspend NULL
1919#define pxa3xx_nand_resume NULL
1920#endif
1921
1922static struct platform_driver pxa3xx_nand_driver = {
1923 .driver = {
1924 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301925 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001926 },
1927 .probe = pxa3xx_nand_probe,
1928 .remove = pxa3xx_nand_remove,
1929 .suspend = pxa3xx_nand_suspend,
1930 .resume = pxa3xx_nand_resume,
1931};
1932
Axel Linf99640d2011-11-27 20:45:03 +08001933module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001934
1935MODULE_LICENSE("GPL");
1936MODULE_DESCRIPTION("PXA3xx NAND controller driver");