blob: da563cdd59f5c5a6236ad9f6726da5a32925db10 [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>
Robert Jarzmik8f5ba312015-09-06 15:12:47 +020018#include <linux/dmaengine.h>
eric miaofe69af02008-02-14 15:48:23 +080019#include <linux/dma-mapping.h>
Robert Jarzmik8f5ba312015-09-06 15:12:47 +020020#include <linux/dma/pxa-dma.h>
eric miaofe69af02008-02-14 15:48:23 +080021#include <linux/delay.h>
22#include <linux/clk.h>
23#include <linux/mtd/mtd.h>
24#include <linux/mtd/nand.h>
25#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010026#include <linux/io.h>
Maxime Ripardafca11e2015-04-07 15:32:45 +020027#include <linux/iopoll.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010028#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020030#include <linux/of.h>
31#include <linux/of_device.h>
Ezequiel Garcia776f2652013-11-14 18:25:28 -030032#include <linux/of_mtd.h>
eric miaofe69af02008-02-14 15:48:23 +080033
Rob Herringce914e62015-04-30 15:17:47 -050034#if defined(CONFIG_ARM) && (defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP))
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030035#define ARCH_HAS_DMA
36#endif
37
Arnd Bergmann293b2da2012-08-24 15:16:48 +020038#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080039
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -050040#define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200)
41#define NAND_STOP_DELAY msecs_to_jiffies(40)
Lei Wen4eb2da82011-02-28 10:32:13 +080042#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080043
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030044/*
45 * Define a buffer size for the initial command that detects the flash device:
Ezequiel Garciac1634092015-08-03 11:31:26 -030046 * STATUS, READID and PARAM.
47 * ONFI param page is 256 bytes, and there are three redundant copies
48 * to be read. JEDEC param page is 512 bytes, and there are also three
49 * redundant copies to be read.
50 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030051 */
Ezequiel Garciac1634092015-08-03 11:31:26 -030052#define INIT_BUFFER_SIZE 2048
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030053
eric miaofe69af02008-02-14 15:48:23 +080054/* registers and bit definitions */
55#define NDCR (0x00) /* Control register */
56#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
57#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
58#define NDSR (0x14) /* Status Register */
59#define NDPCR (0x18) /* Page Count Register */
60#define NDBDR0 (0x1C) /* Bad Block Register 0 */
61#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030062#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080063#define NDDB (0x40) /* Data Buffer */
64#define NDCB0 (0x48) /* Command Buffer0 */
65#define NDCB1 (0x4C) /* Command Buffer1 */
66#define NDCB2 (0x50) /* Command Buffer2 */
67
68#define NDCR_SPARE_EN (0x1 << 31)
69#define NDCR_ECC_EN (0x1 << 30)
70#define NDCR_DMA_EN (0x1 << 29)
71#define NDCR_ND_RUN (0x1 << 28)
72#define NDCR_DWIDTH_C (0x1 << 27)
73#define NDCR_DWIDTH_M (0x1 << 26)
74#define NDCR_PAGE_SZ (0x1 << 24)
75#define NDCR_NCSX (0x1 << 23)
76#define NDCR_ND_MODE (0x3 << 21)
77#define NDCR_NAND_MODE (0x0)
78#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080079#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080080#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
81#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
82
83#define NDCR_RA_START (0x1 << 15)
84#define NDCR_PG_PER_BLK (0x1 << 14)
85#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080086#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080087
88#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030089#define NDSR_ERR_CNT_OFF (16)
90#define NDSR_ERR_CNT_MASK (0x1f)
91#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080092#define NDSR_RDY (0x1 << 12)
93#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080094#define NDSR_CS0_PAGED (0x1 << 10)
95#define NDSR_CS1_PAGED (0x1 << 9)
96#define NDSR_CS0_CMDD (0x1 << 8)
97#define NDSR_CS1_CMDD (0x1 << 7)
98#define NDSR_CS0_BBD (0x1 << 6)
99#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300100#define NDSR_UNCORERR (0x1 << 4)
101#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +0800102#define NDSR_WRDREQ (0x1 << 2)
103#define NDSR_RDDREQ (0x1 << 1)
104#define NDSR_WRCMDREQ (0x1)
105
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300106#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800107#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800108#define NDCB0_AUTO_RS (0x1 << 25)
109#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300110#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
111#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800112#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
113#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
114#define NDCB0_NC (0x1 << 20)
115#define NDCB0_DBC (0x1 << 19)
116#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
117#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
118#define NDCB0_CMD2_MASK (0xff << 8)
119#define NDCB0_CMD1_MASK (0xff)
120#define NDCB0_ADDR_CYC_SHIFT (16)
121
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300122#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
123#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
124#define EXT_CMD_TYPE_READ 4 /* Read */
125#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
126#define EXT_CMD_TYPE_FINAL 3 /* Final command */
127#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
128#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
129
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300130/*
131 * This should be large enough to read 'ONFI' and 'JEDEC'.
132 * Let's use 7 bytes, which is the maximum ID count supported
133 * by the controller (see NDCR_RD_ID_CNT_MASK).
134 */
135#define READ_ID_BYTES 7
136
eric miaofe69af02008-02-14 15:48:23 +0800137/* macros for registers read/write */
138#define nand_writel(info, off, val) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200139 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800140
141#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200142 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800143
144/* error code and state */
145enum {
146 ERR_NONE = 0,
147 ERR_DMABUSERR = -1,
148 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300149 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800150 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300151 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800152};
153
154enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800155 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700156 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800157 STATE_CMD_HANDLE,
158 STATE_DMA_READING,
159 STATE_DMA_WRITING,
160 STATE_DMA_DONE,
161 STATE_PIO_READING,
162 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800163 STATE_CMD_DONE,
164 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800165};
166
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300167enum pxa3xx_nand_variant {
168 PXA3XX_NAND_VARIANT_PXA,
169 PXA3XX_NAND_VARIANT_ARMADA370,
170};
171
Lei Wend4568822011-07-14 20:44:32 -0700172struct pxa3xx_nand_host {
173 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700174 struct mtd_info *mtd;
175 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800176
Lei Wend4568822011-07-14 20:44:32 -0700177 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700178 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700179 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700180
181 /* calculated from pxa3xx_nand_flash data */
182 unsigned int col_addr_cycles;
183 unsigned int row_addr_cycles;
Lei Wend4568822011-07-14 20:44:32 -0700184};
185
186struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800187 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800188 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800189
190 struct clk *clk;
191 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800192 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300193 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800194
195 unsigned int buf_start;
196 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300197 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300198 unsigned int data_buff_pos;
199 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800200
201 /* DMA information */
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200202 struct scatterlist sg;
203 enum dma_data_direction dma_dir;
204 struct dma_chan *dma_chan;
205 dma_cookie_t dma_cookie;
eric miaofe69af02008-02-14 15:48:23 +0800206 int drcmr_dat;
207 int drcmr_cmd;
208
209 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800210 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800211 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800212 int data_dma_ch;
eric miaofe69af02008-02-14 15:48:23 +0800213
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
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300255struct pxa3xx_nand_timing {
256 unsigned int tCH; /* Enable signal hold time */
257 unsigned int tCS; /* Enable signal setup time */
258 unsigned int tWH; /* ND_nWE high duration */
259 unsigned int tWP; /* ND_nWE pulse time */
260 unsigned int tRH; /* ND_nRE high duration */
261 unsigned int tRP; /* ND_nRE pulse width */
262 unsigned int tR; /* ND_nWE high to ND_nRE low for read */
263 unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */
264 unsigned int tAR; /* ND_ALE low to ND_nRE low delay */
265};
266
267struct pxa3xx_nand_flash {
268 char *name;
269 uint32_t chip_id;
270 unsigned int page_per_block; /* Pages per block (PG_PER_BLK) */
271 unsigned int page_size; /* Page size in bytes (PAGE_SZ) */
272 unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */
273 unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */
274 unsigned int num_blocks; /* Number of physical blocks in Flash */
275
276 struct pxa3xx_nand_timing *timing; /* NAND Flash timing */
277};
278
Lei Wenc1f82472010-08-17 13:50:23 +0800279static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800280 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
281 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
282 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
283 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800284};
285
Lei Wenc1f82472010-08-17 13:50:23 +0800286static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800287{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
288{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
289{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
290{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
291{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
292{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
293{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
294{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
295{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800296};
297
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300298static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
299static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
300
301static struct nand_bbt_descr bbt_main_descr = {
302 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
303 | NAND_BBT_2BIT | NAND_BBT_VERSION,
304 .offs = 8,
305 .len = 6,
306 .veroffs = 14,
307 .maxblocks = 8, /* Last 8 blocks in each chip */
308 .pattern = bbt_pattern
309};
310
311static struct nand_bbt_descr bbt_mirror_descr = {
312 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
313 | NAND_BBT_2BIT | NAND_BBT_VERSION,
314 .offs = 8,
315 .len = 6,
316 .veroffs = 14,
317 .maxblocks = 8, /* Last 8 blocks in each chip */
318 .pattern = bbt_mirror_pattern
319};
320
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100321static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
322 .eccbytes = 32,
323 .eccpos = {
324 32, 33, 34, 35, 36, 37, 38, 39,
325 40, 41, 42, 43, 44, 45, 46, 47,
326 48, 49, 50, 51, 52, 53, 54, 55,
327 56, 57, 58, 59, 60, 61, 62, 63},
328 .oobfree = { {2, 30} }
329};
330
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300331static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
332 .eccbytes = 64,
333 .eccpos = {
334 32, 33, 34, 35, 36, 37, 38, 39,
335 40, 41, 42, 43, 44, 45, 46, 47,
336 48, 49, 50, 51, 52, 53, 54, 55,
337 56, 57, 58, 59, 60, 61, 62, 63,
338 96, 97, 98, 99, 100, 101, 102, 103,
339 104, 105, 106, 107, 108, 109, 110, 111,
340 112, 113, 114, 115, 116, 117, 118, 119,
341 120, 121, 122, 123, 124, 125, 126, 127},
342 /* Bootrom looks in bytes 0 & 5 for bad blocks */
343 .oobfree = { {6, 26}, { 64, 32} }
344};
345
346static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
347 .eccbytes = 128,
348 .eccpos = {
349 32, 33, 34, 35, 36, 37, 38, 39,
350 40, 41, 42, 43, 44, 45, 46, 47,
351 48, 49, 50, 51, 52, 53, 54, 55,
352 56, 57, 58, 59, 60, 61, 62, 63},
353 .oobfree = { }
354};
355
Lei Wen227a8862010-08-18 18:00:03 +0800356/* Define a default flash type setting serve as flash detecting only */
357#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
358
eric miaofe69af02008-02-14 15:48:23 +0800359#define NDTR0_tCH(c) (min((c), 7) << 19)
360#define NDTR0_tCS(c) (min((c), 7) << 16)
361#define NDTR0_tWH(c) (min((c), 7) << 11)
362#define NDTR0_tWP(c) (min((c), 7) << 8)
363#define NDTR0_tRH(c) (min((c), 7) << 3)
364#define NDTR0_tRP(c) (min((c), 7) << 0)
365
366#define NDTR1_tR(c) (min((c), 65535) << 16)
367#define NDTR1_tWHR(c) (min((c), 15) << 4)
368#define NDTR1_tAR(c) (min((c), 15) << 0)
369
370/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800371#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800372
Jingoo Han17754ad2014-05-07 17:49:13 +0900373static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300374 {
375 .compatible = "marvell,pxa3xx-nand",
376 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
377 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300378 {
379 .compatible = "marvell,armada370-nand",
380 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
381 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300382 {}
383};
384MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
385
386static enum pxa3xx_nand_variant
387pxa3xx_nand_get_variant(struct platform_device *pdev)
388{
389 const struct of_device_id *of_id =
390 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
391 if (!of_id)
392 return PXA3XX_NAND_VARIANT_PXA;
393 return (enum pxa3xx_nand_variant)of_id->data;
394}
395
Lei Wend4568822011-07-14 20:44:32 -0700396static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200397 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800398{
Lei Wend4568822011-07-14 20:44:32 -0700399 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800400 unsigned long nand_clk = clk_get_rate(info->clk);
401 uint32_t ndtr0, ndtr1;
402
403 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
404 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
405 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
406 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
407 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
408 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
409
410 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
411 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
412 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
413
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300414 info->ndtr0cs0 = ndtr0;
415 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800416 nand_writel(info, NDTR0CS0, ndtr0);
417 nand_writel(info, NDTR1CS0, ndtr1);
418}
419
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300420/*
421 * Set the data and OOB size, depending on the selected
422 * spare and ECC configuration.
423 * Only applicable to READ0, READOOB and PAGEPROG commands.
424 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300425static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
426 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800427{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300428 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800429
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300430 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300431 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800432 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800433
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300434 info->oob_size = info->spare_size;
435 if (!info->use_ecc)
436 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800437}
438
Lei Wenf8155a42011-02-28 10:32:11 +0800439/**
440 * NOTE: it is a must to set ND_RUN firstly, then write
441 * command buffer, otherwise, it does not work.
442 * We enable all the interrupt at the same time, and
443 * let pxa3xx_nand_irq to handle all logic.
444 */
445static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
446{
447 uint32_t ndcr;
448
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300449 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300450
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300451 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300452 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300453 if (info->ecc_bch)
454 nand_writel(info, NDECCCTRL, 0x1);
455 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300456 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300457 if (info->ecc_bch)
458 nand_writel(info, NDECCCTRL, 0x0);
459 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300460
461 if (info->use_dma)
462 ndcr |= NDCR_DMA_EN;
463 else
464 ndcr &= ~NDCR_DMA_EN;
465
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300466 if (info->use_spare)
467 ndcr |= NDCR_SPARE_EN;
468 else
469 ndcr &= ~NDCR_SPARE_EN;
470
Lei Wenf8155a42011-02-28 10:32:11 +0800471 ndcr |= NDCR_ND_RUN;
472
473 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800474 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200475 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800476 nand_writel(info, NDCR, ndcr);
477}
478
479static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
480{
481 uint32_t ndcr;
482 int timeout = NAND_STOP_DELAY;
483
484 /* wait RUN bit in NDCR become 0 */
485 ndcr = nand_readl(info, NDCR);
486 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
487 ndcr = nand_readl(info, NDCR);
488 udelay(1);
489 }
490
491 if (timeout <= 0) {
492 ndcr &= ~NDCR_ND_RUN;
493 nand_writel(info, NDCR, ndcr);
494 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200495 if (info->dma_chan)
496 dmaengine_terminate_all(info->dma_chan);
497
Lei Wenf8155a42011-02-28 10:32:11 +0800498 /* clear status bits */
499 nand_writel(info, NDSR, NDSR_MASK);
500}
501
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300502static void __maybe_unused
503enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800504{
505 uint32_t ndcr;
506
507 ndcr = nand_readl(info, NDCR);
508 nand_writel(info, NDCR, ndcr & ~int_mask);
509}
510
511static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
512{
513 uint32_t ndcr;
514
515 ndcr = nand_readl(info, NDCR);
516 nand_writel(info, NDCR, ndcr | int_mask);
517}
518
Maxime Ripard8dad0382015-02-18 11:32:07 +0100519static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
520{
521 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200522 u32 val;
523 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100524
525 /*
526 * According to the datasheet, when reading from NDDB
527 * with BCH enabled, after each 32 bytes reads, we
528 * have to make sure that the NDSR.RDDREQ bit is set.
529 *
530 * Drain the FIFO 8 32 bits reads at a time, and skip
531 * the polling on the last read.
532 */
533 while (len > 8) {
Rob Herringce914e62015-04-30 15:17:47 -0500534 readsl(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100535
Maxime Ripardafca11e2015-04-07 15:32:45 +0200536 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
537 val & NDSR_RDDREQ, 1000, 5000);
538 if (ret) {
539 dev_err(&info->pdev->dev,
540 "Timeout on RDDREQ while draining the FIFO\n");
541 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100542 }
543
544 data += 32;
545 len -= 8;
546 }
547 }
548
Rob Herringce914e62015-04-30 15:17:47 -0500549 readsl(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100550}
551
Lei Wenf8155a42011-02-28 10:32:11 +0800552static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800553{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300554 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300555
eric miaofe69af02008-02-14 15:48:23 +0800556 switch (info->state) {
557 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500558 writesl(info->mmio_base + NDDB,
559 info->data_buff + info->data_buff_pos,
560 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300561
Lei Wen9d8b1042010-08-17 14:09:30 +0800562 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500563 writesl(info->mmio_base + NDDB,
564 info->oob_buff + info->oob_buff_pos,
565 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800566 break;
567 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100568 drain_fifo(info,
569 info->data_buff + info->data_buff_pos,
570 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300571
Lei Wen9d8b1042010-08-17 14:09:30 +0800572 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100573 drain_fifo(info,
574 info->oob_buff + info->oob_buff_pos,
575 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800576 break;
577 default:
Lei Wenda675b42011-07-14 20:44:31 -0700578 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800579 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800580 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800581 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300582
583 /* Update buffer pointers for multi-page read/write */
584 info->data_buff_pos += do_bytes;
585 info->oob_buff_pos += info->oob_size;
586 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800587}
588
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200589static void pxa3xx_nand_data_dma_irq(void *data)
590{
591 struct pxa3xx_nand_info *info = data;
592 struct dma_tx_state state;
593 enum dma_status status;
594
595 status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state);
596 if (likely(status == DMA_COMPLETE)) {
597 info->state = STATE_DMA_DONE;
598 } else {
599 dev_err(&info->pdev->dev, "DMA error on data channel\n");
600 info->retcode = ERR_DMABUSERR;
601 }
602 dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
603
604 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
605 enable_int(info, NDCR_INT_MASK);
606}
607
Lei Wenf8155a42011-02-28 10:32:11 +0800608static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800609{
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200610 enum dma_transfer_direction direction;
611 struct dma_async_tx_descriptor *tx;
eric miaofe69af02008-02-14 15:48:23 +0800612
Lei Wenf8155a42011-02-28 10:32:11 +0800613 switch (info->state) {
614 case STATE_DMA_WRITING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200615 info->dma_dir = DMA_TO_DEVICE;
616 direction = DMA_MEM_TO_DEV;
Lei Wenf8155a42011-02-28 10:32:11 +0800617 break;
618 case STATE_DMA_READING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200619 info->dma_dir = DMA_FROM_DEVICE;
620 direction = DMA_DEV_TO_MEM;
Lei Wenf8155a42011-02-28 10:32:11 +0800621 break;
622 default:
Lei Wenda675b42011-07-14 20:44:31 -0700623 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800624 info->state);
625 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800626 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200627 info->sg.length = info->data_size +
628 (info->oob_size ? info->spare_size + info->ecc_size : 0);
629 dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
eric miaofe69af02008-02-14 15:48:23 +0800630
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200631 tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
632 DMA_PREP_INTERRUPT);
633 if (!tx) {
634 dev_err(&info->pdev->dev, "prep_slave_sg() failed\n");
635 return;
eric miaofe69af02008-02-14 15:48:23 +0800636 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200637 tx->callback = pxa3xx_nand_data_dma_irq;
638 tx->callback_param = info;
639 info->dma_cookie = dmaengine_submit(tx);
640 dma_async_issue_pending(info->dma_chan);
641 dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n",
642 __func__, direction, info->dma_cookie, info->sg.length);
eric miaofe69af02008-02-14 15:48:23 +0800643}
644
Robert Jarzmik24542252015-02-20 19:36:43 +0100645static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
646{
647 struct pxa3xx_nand_info *info = data;
648
649 handle_data_pio(info);
650
651 info->state = STATE_CMD_DONE;
652 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
653
654 return IRQ_HANDLED;
655}
656
eric miaofe69af02008-02-14 15:48:23 +0800657static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
658{
659 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300660 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700661 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100662 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700663
664 if (info->cs == 0) {
665 ready = NDSR_FLASH_RDY;
666 cmd_done = NDSR_CS0_CMDD;
667 } else {
668 ready = NDSR_RDY;
669 cmd_done = NDSR_CS1_CMDD;
670 }
eric miaofe69af02008-02-14 15:48:23 +0800671
672 status = nand_readl(info, NDSR);
673
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300674 if (status & NDSR_UNCORERR)
675 info->retcode = ERR_UNCORERR;
676 if (status & NDSR_CORERR) {
677 info->retcode = ERR_CORERR;
678 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
679 info->ecc_bch)
680 info->ecc_err_cnt = NDSR_ERR_CNT(status);
681 else
682 info->ecc_err_cnt = 1;
683
684 /*
685 * Each chunk composing a page is corrected independently,
686 * and we need to store maximum number of corrected bitflips
687 * to return it to the MTD layer in ecc.read_page().
688 */
689 info->max_bitflips = max_t(unsigned int,
690 info->max_bitflips,
691 info->ecc_err_cnt);
692 }
Lei Wenf8155a42011-02-28 10:32:11 +0800693 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
694 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800695 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800696 disable_int(info, NDCR_INT_MASK);
697 info->state = (status & NDSR_RDDREQ) ?
698 STATE_DMA_READING : STATE_DMA_WRITING;
699 start_data_dma(info);
700 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800701 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800702 info->state = (status & NDSR_RDDREQ) ?
703 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100704 ret = IRQ_WAKE_THREAD;
705 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800706 }
Lei Wenf8155a42011-02-28 10:32:11 +0800707 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700708 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800709 info->state = STATE_CMD_DONE;
710 is_completed = 1;
711 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700712 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800713 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300714 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800715 }
Lei Wenf8155a42011-02-28 10:32:11 +0800716
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200717 /*
718 * Clear all status bit before issuing the next command, which
719 * can and will alter the status bits and will deserve a new
720 * interrupt on its own. This lets the controller exit the IRQ
721 */
722 nand_writel(info, NDSR, status);
723
Lei Wenf8155a42011-02-28 10:32:11 +0800724 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800725 status &= ~NDSR_WRCMDREQ;
726 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300727
728 /*
729 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
730 * must be loaded by writing directly either 12 or 16
731 * bytes directly to NDCB0, four bytes at a time.
732 *
733 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
734 * but each NDCBx register can be read.
735 */
Lei Wenf8155a42011-02-28 10:32:11 +0800736 nand_writel(info, NDCB0, info->ndcb0);
737 nand_writel(info, NDCB0, info->ndcb1);
738 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300739
740 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
741 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
742 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800743 }
Lei Wenf8155a42011-02-28 10:32:11 +0800744
Lei Wenf8155a42011-02-28 10:32:11 +0800745 if (is_completed)
746 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300747 if (is_ready)
748 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800749NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100750 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800751}
752
eric miaofe69af02008-02-14 15:48:23 +0800753static inline int is_buf_blank(uint8_t *buf, size_t len)
754{
755 for (; len > 0; len--)
756 if (*buf++ != 0xff)
757 return 0;
758 return 1;
759}
760
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300761static void set_command_address(struct pxa3xx_nand_info *info,
762 unsigned int page_size, uint16_t column, int page_addr)
763{
764 /* small page addr setting */
765 if (page_size < PAGE_CHUNK_SIZE) {
766 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
767 | (column & 0xFF);
768
769 info->ndcb2 = 0;
770 } else {
771 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
772 | (column & 0xFFFF);
773
774 if (page_addr & 0xFF0000)
775 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
776 else
777 info->ndcb2 = 0;
778 }
779}
780
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300781static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800782{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300783 struct pxa3xx_nand_host *host = info->host[info->cs];
784 struct mtd_info *mtd = host->mtd;
785
Lei Wen4eb2da82011-02-28 10:32:13 +0800786 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800787 info->buf_start = 0;
788 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800789 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300790 info->data_buff_pos = 0;
791 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800792 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300793 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800794 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300795 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300796 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300797 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800798
799 switch (command) {
800 case NAND_CMD_READ0:
801 case NAND_CMD_PAGEPROG:
802 info->use_ecc = 1;
803 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300804 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800805 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300806 case NAND_CMD_PARAM:
807 info->use_spare = 0;
808 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800809 default:
810 info->ndcb1 = 0;
811 info->ndcb2 = 0;
812 break;
813 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300814
815 /*
816 * If we are about to issue a read command, or about to set
817 * the write address, then clean the data buffer.
818 */
819 if (command == NAND_CMD_READ0 ||
820 command == NAND_CMD_READOOB ||
821 command == NAND_CMD_SEQIN) {
822
823 info->buf_count = mtd->writesize + mtd->oobsize;
824 memset(info->data_buff, 0xFF, info->buf_count);
825 }
826
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300827}
828
829static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300830 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300831{
832 int addr_cycle, exec_cmd;
833 struct pxa3xx_nand_host *host;
834 struct mtd_info *mtd;
835
836 host = info->host[info->cs];
837 mtd = host->mtd;
838 addr_cycle = 0;
839 exec_cmd = 1;
840
841 if (info->cs != 0)
842 info->ndcb0 = NDCB0_CSEL;
843 else
844 info->ndcb0 = 0;
845
846 if (command == NAND_CMD_SEQIN)
847 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800848
Lei Wend4568822011-07-14 20:44:32 -0700849 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
850 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800851
852 switch (command) {
853 case NAND_CMD_READOOB:
854 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300855 info->buf_start = column;
856 info->ndcb0 |= NDCB0_CMD_TYPE(0)
857 | addr_cycle
858 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800859
Ezequiel Garciaec821352013-08-12 14:14:54 -0300860 if (command == NAND_CMD_READOOB)
861 info->buf_start += mtd->writesize;
862
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300863 /*
864 * Multiple page read needs an 'extended command type' field,
865 * which is either naked-read or last-read according to the
866 * state.
867 */
868 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300869 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300870 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
871 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
872 | NDCB0_LEN_OVRD
873 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
874 info->ndcb3 = info->chunk_size +
875 info->oob_size;
876 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800877
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300878 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300879 break;
880
Lei Wen4eb2da82011-02-28 10:32:13 +0800881 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800882
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300883 info->buf_start = column;
884 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300885
886 /*
887 * Multiple page programming needs to execute the initial
888 * SEQIN command that sets the page address.
889 */
890 if (mtd->writesize > PAGE_CHUNK_SIZE) {
891 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
892 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
893 | addr_cycle
894 | command;
895 /* No data transfer in this case */
896 info->data_size = 0;
897 exec_cmd = 1;
898 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800899 break;
900
901 case NAND_CMD_PAGEPROG:
902 if (is_buf_blank(info->data_buff,
903 (mtd->writesize + mtd->oobsize))) {
904 exec_cmd = 0;
905 break;
906 }
907
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300908 /* Second command setting for large pages */
909 if (mtd->writesize > PAGE_CHUNK_SIZE) {
910 /*
911 * Multiple page write uses the 'extended command'
912 * field. This can be used to issue a command dispatch
913 * or a naked-write depending on the current stage.
914 */
915 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
916 | NDCB0_LEN_OVRD
917 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
918 info->ndcb3 = info->chunk_size +
919 info->oob_size;
920
921 /*
922 * This is the command dispatch that completes a chunked
923 * page program operation.
924 */
925 if (info->data_size == 0) {
926 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
927 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
928 | command;
929 info->ndcb1 = 0;
930 info->ndcb2 = 0;
931 info->ndcb3 = 0;
932 }
933 } else {
934 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
935 | NDCB0_AUTO_RS
936 | NDCB0_ST_ROW_EN
937 | NDCB0_DBC
938 | (NAND_CMD_PAGEPROG << 8)
939 | NAND_CMD_SEQIN
940 | addr_cycle;
941 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800942 break;
943
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300944 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -0300945 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300946 info->ndcb0 |= NDCB0_CMD_TYPE(0)
947 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300948 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300949 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300950 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -0300951 info->ndcb3 = INIT_BUFFER_SIZE;
952 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300953 break;
954
Lei Wen4eb2da82011-02-28 10:32:13 +0800955 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300956 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +0800957 info->ndcb0 |= NDCB0_CMD_TYPE(3)
958 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300959 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300960 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800961
962 info->data_size = 8;
963 break;
964 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800965 info->buf_count = 1;
966 info->ndcb0 |= NDCB0_CMD_TYPE(4)
967 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300968 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800969
970 info->data_size = 8;
971 break;
972
973 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800974 info->ndcb0 |= NDCB0_CMD_TYPE(2)
975 | NDCB0_AUTO_RS
976 | NDCB0_ADDR_CYC(3)
977 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300978 | (NAND_CMD_ERASE2 << 8)
979 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800980 info->ndcb1 = page_addr;
981 info->ndcb2 = 0;
982
983 break;
984 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800985 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300986 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800987
988 break;
989
990 case NAND_CMD_ERASE2:
991 exec_cmd = 0;
992 break;
993
994 default:
995 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700996 dev_err(&info->pdev->dev, "non-supported command %x\n",
997 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800998 break;
999 }
1000
1001 return exec_cmd;
1002}
1003
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001004static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1005 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +08001006{
Lei Wend4568822011-07-14 20:44:32 -07001007 struct pxa3xx_nand_host *host = mtd->priv;
1008 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001009 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +08001010
Lei Wen4eb2da82011-02-28 10:32:13 +08001011 /*
1012 * if this is a x16 device ,then convert the input
1013 * "byte" address into a "word" address appropriate
1014 * for indexing a word-oriented device
1015 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001016 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +08001017 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +08001018
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001019 /*
1020 * There may be different NAND chip hooked to
1021 * different chip select, so check whether
1022 * chip select has been changed, if yes, reset the timing
1023 */
1024 if (info->cs != host->cs) {
1025 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001026 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1027 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001028 }
1029
Ezequiel Garciac39ff032013-11-14 18:25:33 -03001030 prepare_start_command(info, command);
1031
Lei Wend4568822011-07-14 20:44:32 -07001032 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001033 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1034
Lei Wenf8155a42011-02-28 10:32:11 +08001035 if (exec_cmd) {
1036 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001037 init_completion(&info->dev_ready);
1038 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001039 pxa3xx_nand_start(info);
1040
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001041 if (!wait_for_completion_timeout(&info->cmd_complete,
1042 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001043 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001044 /* Stop State Machine for next command cycle */
1045 pxa3xx_nand_stop(info);
1046 }
eric miaofe69af02008-02-14 15:48:23 +08001047 }
Lei Wend4568822011-07-14 20:44:32 -07001048 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001049}
1050
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001051static void nand_cmdfunc_extended(struct mtd_info *mtd,
1052 const unsigned command,
1053 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001054{
1055 struct pxa3xx_nand_host *host = mtd->priv;
1056 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001057 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001058
1059 /*
1060 * if this is a x16 device then convert the input
1061 * "byte" address into a "word" address appropriate
1062 * for indexing a word-oriented device
1063 */
1064 if (info->reg_ndcr & NDCR_DWIDTH_M)
1065 column /= 2;
1066
1067 /*
1068 * There may be different NAND chip hooked to
1069 * different chip select, so check whether
1070 * chip select has been changed, if yes, reset the timing
1071 */
1072 if (info->cs != host->cs) {
1073 info->cs = host->cs;
1074 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1075 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1076 }
1077
1078 /* Select the extended command for the first command */
1079 switch (command) {
1080 case NAND_CMD_READ0:
1081 case NAND_CMD_READOOB:
1082 ext_cmd_type = EXT_CMD_TYPE_MONO;
1083 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001084 case NAND_CMD_SEQIN:
1085 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1086 break;
1087 case NAND_CMD_PAGEPROG:
1088 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1089 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001090 default:
1091 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001092 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001093 }
1094
1095 prepare_start_command(info, command);
1096
1097 /*
1098 * Prepare the "is ready" completion before starting a command
1099 * transaction sequence. If the command is not executed the
1100 * completion will be completed, see below.
1101 *
1102 * We can do that inside the loop because the command variable
1103 * is invariant and thus so is the exec_cmd.
1104 */
1105 info->need_wait = 1;
1106 init_completion(&info->dev_ready);
1107 do {
1108 info->state = STATE_PREPARED;
1109 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1110 column, page_addr);
1111 if (!exec_cmd) {
1112 info->need_wait = 0;
1113 complete(&info->dev_ready);
1114 break;
1115 }
1116
1117 init_completion(&info->cmd_complete);
1118 pxa3xx_nand_start(info);
1119
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001120 if (!wait_for_completion_timeout(&info->cmd_complete,
1121 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001122 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1123 /* Stop State Machine for next command cycle */
1124 pxa3xx_nand_stop(info);
1125 break;
1126 }
1127
1128 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001129 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1130 break;
1131
1132 /*
1133 * After a splitted program command sequence has issued
1134 * the command dispatch, the command sequence is complete.
1135 */
1136 if (info->data_size == 0 &&
1137 command == NAND_CMD_PAGEPROG &&
1138 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001139 break;
1140
1141 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1142 /* Last read: issue a 'last naked read' */
1143 if (info->data_size == info->chunk_size)
1144 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1145 else
1146 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001147
1148 /*
1149 * If a splitted program command has no more data to transfer,
1150 * the command dispatch must be issued to complete.
1151 */
1152 } else if (command == NAND_CMD_PAGEPROG &&
1153 info->data_size == 0) {
1154 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001155 }
1156 } while (1);
1157
1158 info->state = STATE_IDLE;
1159}
1160
Josh Wufdbad98d2012-06-25 18:07:45 +08001161static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001162 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001163{
1164 chip->write_buf(mtd, buf, mtd->writesize);
1165 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001166
1167 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001168}
1169
1170static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001171 struct nand_chip *chip, uint8_t *buf, int oob_required,
1172 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001173{
Lei Wend4568822011-07-14 20:44:32 -07001174 struct pxa3xx_nand_host *host = mtd->priv;
1175 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001176
1177 chip->read_buf(mtd, buf, mtd->writesize);
1178 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1179
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001180 if (info->retcode == ERR_CORERR && info->use_ecc) {
1181 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1182
1183 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001184 /*
1185 * for blank page (all 0xff), HW will calculate its ECC as
1186 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001187 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001188 */
1189 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001190 info->retcode = ERR_NONE;
1191 else
Lei Wenf8155a42011-02-28 10:32:11 +08001192 mtd->ecc_stats.failed++;
1193 }
1194
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001195 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001196}
1197
eric miaofe69af02008-02-14 15:48:23 +08001198static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1199{
Lei Wend4568822011-07-14 20:44:32 -07001200 struct pxa3xx_nand_host *host = mtd->priv;
1201 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001202 char retval = 0xFF;
1203
1204 if (info->buf_start < info->buf_count)
1205 /* Has just send a new command? */
1206 retval = info->data_buff[info->buf_start++];
1207
1208 return retval;
1209}
1210
1211static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1212{
Lei Wend4568822011-07-14 20:44:32 -07001213 struct pxa3xx_nand_host *host = mtd->priv;
1214 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001215 u16 retval = 0xFFFF;
1216
1217 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1218 retval = *((u16 *)(info->data_buff+info->buf_start));
1219 info->buf_start += 2;
1220 }
1221 return retval;
1222}
1223
1224static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1225{
Lei Wend4568822011-07-14 20:44:32 -07001226 struct pxa3xx_nand_host *host = mtd->priv;
1227 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001228 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1229
1230 memcpy(buf, info->data_buff + info->buf_start, real_len);
1231 info->buf_start += real_len;
1232}
1233
1234static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1235 const uint8_t *buf, int len)
1236{
Lei Wend4568822011-07-14 20:44:32 -07001237 struct pxa3xx_nand_host *host = mtd->priv;
1238 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001239 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1240
1241 memcpy(info->data_buff + info->buf_start, buf, real_len);
1242 info->buf_start += real_len;
1243}
1244
eric miaofe69af02008-02-14 15:48:23 +08001245static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1246{
1247 return;
1248}
1249
1250static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1251{
Lei Wend4568822011-07-14 20:44:32 -07001252 struct pxa3xx_nand_host *host = mtd->priv;
1253 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001254
1255 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001256 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001257 if (!wait_for_completion_timeout(&info->dev_ready,
1258 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001259 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1260 return NAND_STATUS_FAIL;
1261 }
1262 }
eric miaofe69af02008-02-14 15:48:23 +08001263
1264 /* pxa3xx_nand_send_command has waited for command complete */
1265 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1266 if (info->retcode == ERR_NONE)
1267 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001268 else
1269 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001270 }
1271
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001272 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001273}
1274
eric miaofe69af02008-02-14 15:48:23 +08001275static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001276 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001277{
1278 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001279 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001280 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001281 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001282
Lei Wenda675b42011-07-14 20:44:31 -07001283 if (f->page_size != 2048 && f->page_size != 512) {
1284 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001285 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001286 }
eric miaofe69af02008-02-14 15:48:23 +08001287
Lei Wenda675b42011-07-14 20:44:31 -07001288 if (f->flash_width != 16 && f->flash_width != 8) {
1289 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001290 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001291 }
eric miaofe69af02008-02-14 15:48:23 +08001292
eric miaofe69af02008-02-14 15:48:23 +08001293 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001294 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001295
1296 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001297 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001298 else
Lei Wend4568822011-07-14 20:44:32 -07001299 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001300
1301 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001302 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001303 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1304 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1305 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1306 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1307
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001308 ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
eric miaofe69af02008-02-14 15:48:23 +08001309 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1310
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001311 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001312
Lei Wend4568822011-07-14 20:44:32 -07001313 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001314 return 0;
1315}
1316
Mike Rapoportf2710492009-02-17 13:54:47 +02001317static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1318{
1319 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001320
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001321 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001322 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001323 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1324 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1325 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001326 return 0;
1327}
1328
eric miaofe69af02008-02-14 15:48:23 +08001329static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1330{
1331 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001332 struct dma_slave_config config;
1333 dma_cap_mask_t mask;
1334 struct pxad_param param;
1335 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001336
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001337 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1338 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001339 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001340 if (use_dma == 0)
1341 return 0;
1342
1343 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1344 if (ret)
1345 return ret;
1346
1347 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1348 dma_cap_zero(mask);
1349 dma_cap_set(DMA_SLAVE, mask);
1350 param.prio = PXAD_PRIO_LOWEST;
1351 param.drcmr = info->drcmr_dat;
1352 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1353 &param, &pdev->dev,
1354 "data");
1355 if (!info->dma_chan) {
1356 dev_err(&pdev->dev, "unable to request data dma channel\n");
1357 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001358 }
1359
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001360 memset(&config, 0, sizeof(config));
1361 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1362 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1363 config.src_addr = info->mmio_phys + NDDB;
1364 config.dst_addr = info->mmio_phys + NDDB;
1365 config.src_maxburst = 32;
1366 config.dst_maxburst = 32;
1367 ret = dmaengine_slave_config(info->dma_chan, &config);
1368 if (ret < 0) {
1369 dev_err(&info->pdev->dev,
1370 "dma channel configuration failed: %d\n",
1371 ret);
1372 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001373 }
1374
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001375 /*
1376 * Now that DMA buffers are allocated we turn on
1377 * DMA proper for I/O operations.
1378 */
1379 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001380 return 0;
1381}
1382
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001383static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1384{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001385 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001386 dmaengine_terminate_all(info->dma_chan);
1387 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001388 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001389 kfree(info->data_buff);
1390}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001391
Lei Wen401e67e2011-02-28 10:32:14 +08001392static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001393{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001394 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001395 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001396 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001397
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001398 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001399 chip = mtd->priv;
1400
Lei Wen401e67e2011-02-28 10:32:14 +08001401 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001402 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1403 if (ret)
1404 return ret;
1405
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001406 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001407 ret = chip->waitfunc(mtd, chip);
1408 if (ret & NAND_STATUS_FAIL)
1409 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001410
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001411 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001412}
eric miaofe69af02008-02-14 15:48:23 +08001413
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001414static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1415 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001416 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001417{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001418 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001419 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001420 info->spare_size = 40;
1421 info->ecc_size = 24;
1422 ecc->mode = NAND_ECC_HW;
1423 ecc->size = 512;
1424 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001425
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001426 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001427 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001428 info->spare_size = 8;
1429 info->ecc_size = 8;
1430 ecc->mode = NAND_ECC_HW;
1431 ecc->size = 512;
1432 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001433
Brian Norris6033a942013-11-14 14:41:32 -08001434 /*
1435 * Required ECC: 4-bit correction per 512 bytes
1436 * Select: 16-bit correction per 2048 bytes
1437 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001438 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1439 info->ecc_bch = 1;
1440 info->chunk_size = 2048;
1441 info->spare_size = 32;
1442 info->ecc_size = 32;
1443 ecc->mode = NAND_ECC_HW;
1444 ecc->size = info->chunk_size;
1445 ecc->layout = &ecc_layout_2KB_bch4bit;
1446 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001447
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001448 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001449 info->ecc_bch = 1;
1450 info->chunk_size = 2048;
1451 info->spare_size = 32;
1452 info->ecc_size = 32;
1453 ecc->mode = NAND_ECC_HW;
1454 ecc->size = info->chunk_size;
1455 ecc->layout = &ecc_layout_4KB_bch4bit;
1456 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001457
Brian Norris6033a942013-11-14 14:41:32 -08001458 /*
1459 * Required ECC: 8-bit correction per 512 bytes
1460 * Select: 16-bit correction per 1024 bytes
1461 */
1462 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001463 info->ecc_bch = 1;
1464 info->chunk_size = 1024;
1465 info->spare_size = 0;
1466 info->ecc_size = 32;
1467 ecc->mode = NAND_ECC_HW;
1468 ecc->size = info->chunk_size;
1469 ecc->layout = &ecc_layout_4KB_bch8bit;
1470 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001471 } else {
1472 dev_err(&info->pdev->dev,
1473 "ECC strength %d at page size %d is not supported\n",
1474 strength, page_size);
1475 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001476 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001477
1478 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1479 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001480 return 0;
1481}
1482
Lei Wen401e67e2011-02-28 10:32:14 +08001483static int pxa3xx_nand_scan(struct mtd_info *mtd)
1484{
Lei Wend4568822011-07-14 20:44:32 -07001485 struct pxa3xx_nand_host *host = mtd->priv;
1486 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001487 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001488 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001489 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001490 const struct pxa3xx_nand_flash *f = NULL;
1491 struct nand_chip *chip = mtd->priv;
1492 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001493 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001494 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001495 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001496
1497 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001498 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001499
Antoine Ténartbc3e00f2015-08-18 10:59:10 +02001500 /* Set a default chunk size */
1501 info->chunk_size = 512;
1502
Lei Wen401e67e2011-02-28 10:32:14 +08001503 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001504 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001505 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1506 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001507
Lei Wend4568822011-07-14 20:44:32 -07001508 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001509 }
1510
1511 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1512 id = *((uint16_t *)(info->data_buff));
1513 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001514 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001515 else {
Lei Wenda675b42011-07-14 20:44:31 -07001516 dev_warn(&info->pdev->dev,
1517 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001518
1519 return -EINVAL;
1520 }
1521
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001522 num = ARRAY_SIZE(builtin_flash_types) - 1;
Lei Wen401e67e2011-02-28 10:32:14 +08001523 for (i = 0; i < num; i++) {
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001524 f = &builtin_flash_types[i + 1];
Lei Wen401e67e2011-02-28 10:32:14 +08001525
1526 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001527 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001528 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001529 }
1530
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001531 if (i >= (ARRAY_SIZE(builtin_flash_types) - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001532 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001533
1534 return -EINVAL;
1535 }
1536
Lei Wend4568822011-07-14 20:44:32 -07001537 ret = pxa3xx_nand_config_flash(info, f);
1538 if (ret) {
1539 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1540 return ret;
1541 }
1542
Antoine Ténart7c2f7172015-02-12 15:53:27 +01001543 memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids));
1544
Lei Wen4332c112011-03-03 11:27:01 +08001545 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001546 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001547 pxa3xx_flash_ids[0].pagesize = f->page_size;
1548 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1549 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1550 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1551 if (f->flash_width == 16)
1552 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001553 pxa3xx_flash_ids[1].name = NULL;
1554 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001555KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001556 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001557 chip->options |= NAND_BUSWIDTH_16;
1558
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001559 /* Device detection must be done with ECC disabled */
1560 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1561 nand_writel(info, NDECCCTRL, 0x0);
1562
Lei Wen0fab0282011-06-07 03:01:06 -07001563 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001564 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001565
1566 if (pdata->flash_bbt) {
1567 /*
1568 * We'll use a bad block table stored in-flash and don't
1569 * allow writing the bad block marker to the flash.
1570 */
1571 chip->bbt_options |= NAND_BBT_USE_FLASH |
1572 NAND_BBT_NO_OOB_BBM;
1573 chip->bbt_td = &bbt_main_descr;
1574 chip->bbt_md = &bbt_mirror_descr;
1575 }
1576
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001577 /*
1578 * If the page size is bigger than the FIFO size, let's check
1579 * we are given the right variant and then switch to the extended
1580 * (aka splitted) command handling,
1581 */
1582 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1583 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1584 chip->cmdfunc = nand_cmdfunc_extended;
1585 } else {
1586 dev_err(&info->pdev->dev,
1587 "unsupported page size on this variant\n");
1588 return -ENODEV;
1589 }
1590 }
1591
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001592 if (pdata->ecc_strength && pdata->ecc_step_size) {
1593 ecc_strength = pdata->ecc_strength;
1594 ecc_step = pdata->ecc_step_size;
1595 } else {
1596 ecc_strength = chip->ecc_strength_ds;
1597 ecc_step = chip->ecc_step_ds;
1598 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001599
1600 /* Set default ECC strength requirements on non-ONFI devices */
1601 if (ecc_strength < 1 && ecc_step < 1) {
1602 ecc_strength = 1;
1603 ecc_step = 512;
1604 }
1605
1606 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1607 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001608 if (ret)
1609 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001610
Lei Wen4332c112011-03-03 11:27:01 +08001611 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001612 if (mtd->writesize >= 2048)
1613 host->col_addr_cycles = 2;
1614 else
1615 host->col_addr_cycles = 1;
1616
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001617 /* release the initial buffer */
1618 kfree(info->data_buff);
1619
1620 /* allocate the real data + oob buffer */
1621 info->buf_size = mtd->writesize + mtd->oobsize;
1622 ret = pxa3xx_nand_init_buff(info);
1623 if (ret)
1624 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001625 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001626
Lei Wen4332c112011-03-03 11:27:01 +08001627 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001628 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001629 else
Lei Wend4568822011-07-14 20:44:32 -07001630 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001631 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001632}
1633
Lei Wend4568822011-07-14 20:44:32 -07001634static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001635{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001636 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001637 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001638 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001639 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001640 struct mtd_info *mtd;
1641 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001642 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001643
Jingoo Han453810b2013-07-30 17:18:33 +09001644 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001645 if (pdata->num_cs <= 0)
1646 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001647 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1648 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1649 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001650 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001651
eric miaofe69af02008-02-14 15:48:23 +08001652 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001653 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001654 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001655 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001656 chip = (struct nand_chip *)(&mtd[1]);
1657 host = (struct pxa3xx_nand_host *)chip;
1658 info->host[cs] = host;
1659 host->mtd = mtd;
1660 host->cs = cs;
1661 host->info_data = info;
1662 mtd->priv = host;
1663 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001664
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001665 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1666 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1667 chip->controller = &info->controller;
1668 chip->waitfunc = pxa3xx_nand_waitfunc;
1669 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001670 chip->read_word = pxa3xx_nand_read_word;
1671 chip->read_byte = pxa3xx_nand_read_byte;
1672 chip->read_buf = pxa3xx_nand_read_buf;
1673 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001674 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001675 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001676 }
Lei Wen401e67e2011-02-28 10:32:14 +08001677
1678 spin_lock_init(&chip->controller->lock);
1679 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001680 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001681 if (IS_ERR(info->clk)) {
1682 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001683 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001684 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001685 ret = clk_prepare_enable(info->clk);
1686 if (ret < 0)
1687 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001688
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001689 if (use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001690 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1691 if (r == NULL) {
1692 dev_err(&pdev->dev,
1693 "no resource defined for data DMA\n");
1694 ret = -ENXIO;
1695 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001696 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001697 info->drcmr_dat = r->start;
1698
1699 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1700 if (r == NULL) {
1701 dev_err(&pdev->dev,
1702 "no resource defined for cmd DMA\n");
1703 ret = -ENXIO;
1704 goto fail_disable_clk;
1705 }
1706 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001707 }
eric miaofe69af02008-02-14 15:48:23 +08001708
1709 irq = platform_get_irq(pdev, 0);
1710 if (irq < 0) {
1711 dev_err(&pdev->dev, "no IRQ resource defined\n");
1712 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001713 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001714 }
1715
1716 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001717 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1718 if (IS_ERR(info->mmio_base)) {
1719 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001720 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001721 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001722 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001723
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001724 /* Allocate a buffer to allow flash detection */
1725 info->buf_size = INIT_BUFFER_SIZE;
1726 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1727 if (info->data_buff == NULL) {
1728 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001729 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001730 }
eric miaofe69af02008-02-14 15:48:23 +08001731
Haojian Zhuang346e1252009-09-10 14:27:23 +08001732 /* initialize all interrupts to be disabled */
1733 disable_int(info, NDSR_MASK);
1734
Robert Jarzmik24542252015-02-20 19:36:43 +01001735 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1736 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1737 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001738 if (ret < 0) {
1739 dev_err(&pdev->dev, "failed to request IRQ\n");
1740 goto fail_free_buf;
1741 }
1742
Lei Wene353a202011-03-03 11:08:30 +08001743 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001744
Lei Wend4568822011-07-14 20:44:32 -07001745 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001746
eric miaofe69af02008-02-14 15:48:23 +08001747fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001748 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001749 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001750fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001751 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001752 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001753}
1754
1755static int pxa3xx_nand_remove(struct platform_device *pdev)
1756{
Lei Wene353a202011-03-03 11:08:30 +08001757 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001758 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001759 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001760
Lei Wend4568822011-07-14 20:44:32 -07001761 if (!info)
1762 return 0;
1763
Jingoo Han453810b2013-07-30 17:18:33 +09001764 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001765
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001766 irq = platform_get_irq(pdev, 0);
1767 if (irq >= 0)
1768 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001769 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001770
Ezequiel Garciafb320612013-04-17 13:38:12 -03001771 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001772
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001773 for (cs = 0; cs < pdata->num_cs; cs++)
1774 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001775 return 0;
1776}
1777
Daniel Mack1e7ba632012-07-22 19:51:02 +02001778static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1779{
1780 struct pxa3xx_nand_platform_data *pdata;
1781 struct device_node *np = pdev->dev.of_node;
1782 const struct of_device_id *of_id =
1783 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1784
1785 if (!of_id)
1786 return 0;
1787
1788 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1789 if (!pdata)
1790 return -ENOMEM;
1791
1792 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1793 pdata->enable_arbiter = 1;
1794 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1795 pdata->keep_config = 1;
1796 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001797 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001798
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001799 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1800 if (pdata->ecc_strength < 0)
1801 pdata->ecc_strength = 0;
1802
1803 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1804 if (pdata->ecc_step_size < 0)
1805 pdata->ecc_step_size = 0;
1806
Daniel Mack1e7ba632012-07-22 19:51:02 +02001807 pdev->dev.platform_data = pdata;
1808
1809 return 0;
1810}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001811
Lei Wene353a202011-03-03 11:08:30 +08001812static int pxa3xx_nand_probe(struct platform_device *pdev)
1813{
1814 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001815 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001816 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001817 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001818
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001819 dma_available = IS_ENABLED(CONFIG_ARM) &&
1820 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1821 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001822 use_dma = 0;
1823 dev_warn(&pdev->dev,
1824 "This platform can't do DMA on this device\n");
1825 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001826
Daniel Mack1e7ba632012-07-22 19:51:02 +02001827 ret = pxa3xx_nand_probe_dt(pdev);
1828 if (ret)
1829 return ret;
1830
Jingoo Han453810b2013-07-30 17:18:33 +09001831 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001832 if (!pdata) {
1833 dev_err(&pdev->dev, "no platform data defined\n");
1834 return -ENODEV;
1835 }
1836
Lei Wend4568822011-07-14 20:44:32 -07001837 ret = alloc_nand_resource(pdev);
1838 if (ret) {
1839 dev_err(&pdev->dev, "alloc nand resource failed\n");
1840 return ret;
1841 }
Lei Wene353a202011-03-03 11:08:30 +08001842
Lei Wend4568822011-07-14 20:44:32 -07001843 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001844 probe_success = 0;
1845 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001846 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001847
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001848 /*
1849 * The mtd name matches the one used in 'mtdparts' kernel
1850 * parameter. This name cannot be changed or otherwise
1851 * user's mtd partitions configuration would get broken.
1852 */
1853 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001854 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001855 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001856 if (ret) {
1857 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1858 cs);
1859 continue;
1860 }
1861
Daniel Mack1e7ba632012-07-22 19:51:02 +02001862 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001863 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001864 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001865 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001866 if (!ret)
1867 probe_success = 1;
1868 }
1869
1870 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001871 pxa3xx_nand_remove(pdev);
1872 return -ENODEV;
1873 }
1874
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001875 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001876}
1877
eric miaofe69af02008-02-14 15:48:23 +08001878#ifdef CONFIG_PM
1879static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1880{
Lei Wene353a202011-03-03 11:08:30 +08001881 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001882 struct pxa3xx_nand_platform_data *pdata;
1883 struct mtd_info *mtd;
1884 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001885
Jingoo Han453810b2013-07-30 17:18:33 +09001886 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001887 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001888 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1889 return -EAGAIN;
1890 }
1891
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001892 for (cs = 0; cs < pdata->num_cs; cs++) {
1893 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001894 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001895 }
1896
eric miaofe69af02008-02-14 15:48:23 +08001897 return 0;
1898}
1899
1900static int pxa3xx_nand_resume(struct platform_device *pdev)
1901{
Lei Wene353a202011-03-03 11:08:30 +08001902 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001903 struct pxa3xx_nand_platform_data *pdata;
1904 struct mtd_info *mtd;
1905 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001906
Jingoo Han453810b2013-07-30 17:18:33 +09001907 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001908 /* We don't want to handle interrupt without calling mtd routine */
1909 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001910
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001911 /*
1912 * Directly set the chip select to a invalid value,
1913 * then the driver would reset the timing according
1914 * to current chip select at the beginning of cmdfunc
1915 */
1916 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001917
Lei Wen051fc412011-07-14 20:44:30 -07001918 /*
1919 * As the spec says, the NDSR would be updated to 0x1800 when
1920 * doing the nand_clk disable/enable.
1921 * To prevent it damaging state machine of the driver, clear
1922 * all status before resume
1923 */
1924 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001925 for (cs = 0; cs < pdata->num_cs; cs++) {
1926 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001927 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001928 }
1929
Lei Wen18c81b12010-08-17 17:25:57 +08001930 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001931}
1932#else
1933#define pxa3xx_nand_suspend NULL
1934#define pxa3xx_nand_resume NULL
1935#endif
1936
1937static struct platform_driver pxa3xx_nand_driver = {
1938 .driver = {
1939 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301940 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001941 },
1942 .probe = pxa3xx_nand_probe,
1943 .remove = pxa3xx_nand_remove,
1944 .suspend = pxa3xx_nand_suspend,
1945 .resume = pxa3xx_nand_resume,
1946};
1947
Axel Linf99640d2011-11-27 20:45:03 +08001948module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001949
1950MODULE_LICENSE("GPL");
1951MODULE_DESCRIPTION("PXA3xx NAND controller driver");