blob: bdbc2c231ceb289dff99c44ce2cfe5a10199522e [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>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020033#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080034
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -050035#define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200)
36#define NAND_STOP_DELAY msecs_to_jiffies(40)
Lei Wen4eb2da82011-02-28 10:32:13 +080037#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080038
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030039/*
40 * Define a buffer size for the initial command that detects the flash device:
Ezequiel Garciac1634092015-08-03 11:31:26 -030041 * STATUS, READID and PARAM.
42 * ONFI param page is 256 bytes, and there are three redundant copies
43 * to be read. JEDEC param page is 512 bytes, and there are also three
44 * redundant copies to be read.
45 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030046 */
Ezequiel Garciac1634092015-08-03 11:31:26 -030047#define INIT_BUFFER_SIZE 2048
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030048
eric miaofe69af02008-02-14 15:48:23 +080049/* registers and bit definitions */
50#define NDCR (0x00) /* Control register */
51#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
52#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
53#define NDSR (0x14) /* Status Register */
54#define NDPCR (0x18) /* Page Count Register */
55#define NDBDR0 (0x1C) /* Bad Block Register 0 */
56#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030057#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080058#define NDDB (0x40) /* Data Buffer */
59#define NDCB0 (0x48) /* Command Buffer0 */
60#define NDCB1 (0x4C) /* Command Buffer1 */
61#define NDCB2 (0x50) /* Command Buffer2 */
62
63#define NDCR_SPARE_EN (0x1 << 31)
64#define NDCR_ECC_EN (0x1 << 30)
65#define NDCR_DMA_EN (0x1 << 29)
66#define NDCR_ND_RUN (0x1 << 28)
67#define NDCR_DWIDTH_C (0x1 << 27)
68#define NDCR_DWIDTH_M (0x1 << 26)
69#define NDCR_PAGE_SZ (0x1 << 24)
70#define NDCR_NCSX (0x1 << 23)
71#define NDCR_ND_MODE (0x3 << 21)
72#define NDCR_NAND_MODE (0x0)
73#define NDCR_CLR_PG_CNT (0x1 << 20)
Robert Jarzmike971aff2015-09-28 22:56:51 +020074#define NFCV1_NDCR_ARB_CNTL (0x1 << 19)
75#define NFCV2_NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080076#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
77#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
78
79#define NDCR_RA_START (0x1 << 15)
80#define NDCR_PG_PER_BLK (0x1 << 14)
81#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080082#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080083
84#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030085#define NDSR_ERR_CNT_OFF (16)
86#define NDSR_ERR_CNT_MASK (0x1f)
87#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080088#define NDSR_RDY (0x1 << 12)
89#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080090#define NDSR_CS0_PAGED (0x1 << 10)
91#define NDSR_CS1_PAGED (0x1 << 9)
92#define NDSR_CS0_CMDD (0x1 << 8)
93#define NDSR_CS1_CMDD (0x1 << 7)
94#define NDSR_CS0_BBD (0x1 << 6)
95#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030096#define NDSR_UNCORERR (0x1 << 4)
97#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +080098#define NDSR_WRDREQ (0x1 << 2)
99#define NDSR_RDDREQ (0x1 << 1)
100#define NDSR_WRCMDREQ (0x1)
101
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300102#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800103#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800104#define NDCB0_AUTO_RS (0x1 << 25)
105#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300106#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
107#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800108#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
109#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
110#define NDCB0_NC (0x1 << 20)
111#define NDCB0_DBC (0x1 << 19)
112#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
113#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
114#define NDCB0_CMD2_MASK (0xff << 8)
115#define NDCB0_CMD1_MASK (0xff)
116#define NDCB0_ADDR_CYC_SHIFT (16)
117
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300118#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
119#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
120#define EXT_CMD_TYPE_READ 4 /* Read */
121#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
122#define EXT_CMD_TYPE_FINAL 3 /* Final command */
123#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
124#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
125
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300126/*
127 * This should be large enough to read 'ONFI' and 'JEDEC'.
128 * Let's use 7 bytes, which is the maximum ID count supported
129 * by the controller (see NDCR_RD_ID_CNT_MASK).
130 */
131#define READ_ID_BYTES 7
132
eric miaofe69af02008-02-14 15:48:23 +0800133/* macros for registers read/write */
134#define nand_writel(info, off, val) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200135 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800136
137#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200138 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800139
140/* error code and state */
141enum {
142 ERR_NONE = 0,
143 ERR_DMABUSERR = -1,
144 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300145 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800146 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300147 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800148};
149
150enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800151 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700152 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800153 STATE_CMD_HANDLE,
154 STATE_DMA_READING,
155 STATE_DMA_WRITING,
156 STATE_DMA_DONE,
157 STATE_PIO_READING,
158 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800159 STATE_CMD_DONE,
160 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800161};
162
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300163enum pxa3xx_nand_variant {
164 PXA3XX_NAND_VARIANT_PXA,
165 PXA3XX_NAND_VARIANT_ARMADA370,
166};
167
Lei Wend4568822011-07-14 20:44:32 -0700168struct pxa3xx_nand_host {
169 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700170 struct mtd_info *mtd;
171 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800172
Lei Wend4568822011-07-14 20:44:32 -0700173 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700174 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700175 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700176
177 /* calculated from pxa3xx_nand_flash data */
178 unsigned int col_addr_cycles;
179 unsigned int row_addr_cycles;
Lei Wend4568822011-07-14 20:44:32 -0700180};
181
182struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800183 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800184 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800185
186 struct clk *clk;
187 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800188 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300189 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800190
191 unsigned int buf_start;
192 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300193 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300194 unsigned int data_buff_pos;
195 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800196
197 /* DMA information */
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200198 struct scatterlist sg;
199 enum dma_data_direction dma_dir;
200 struct dma_chan *dma_chan;
201 dma_cookie_t dma_cookie;
eric miaofe69af02008-02-14 15:48:23 +0800202 int drcmr_dat;
203 int drcmr_cmd;
204
205 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800206 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800207 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800208 int data_dma_ch;
eric miaofe69af02008-02-14 15:48:23 +0800209
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700210 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800211 unsigned int state;
212
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300213 /*
214 * This driver supports NFCv1 (as found in PXA SoC)
215 * and NFCv2 (as found in Armada 370/XP SoC).
216 */
217 enum pxa3xx_nand_variant variant;
218
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700219 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800220 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300221 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800222 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300223 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300224 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800225
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300226 unsigned int data_size; /* data to be read from FIFO */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300227 unsigned int chunk_size; /* split commands chunk size */
Lei Wend4568822011-07-14 20:44:32 -0700228 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300229 unsigned int spare_size;
230 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300231 unsigned int ecc_err_cnt;
232 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800233 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800234
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300235 /* cached register value */
236 uint32_t reg_ndcr;
237 uint32_t ndtr0cs0;
238 uint32_t ndtr1cs0;
239
eric miaofe69af02008-02-14 15:48:23 +0800240 /* generated NDCBx register values */
241 uint32_t ndcb0;
242 uint32_t ndcb1;
243 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300244 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800245};
246
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030247static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800248module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300249MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800250
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300251struct pxa3xx_nand_timing {
252 unsigned int tCH; /* Enable signal hold time */
253 unsigned int tCS; /* Enable signal setup time */
254 unsigned int tWH; /* ND_nWE high duration */
255 unsigned int tWP; /* ND_nWE pulse time */
256 unsigned int tRH; /* ND_nRE high duration */
257 unsigned int tRP; /* ND_nRE pulse width */
258 unsigned int tR; /* ND_nWE high to ND_nRE low for read */
259 unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */
260 unsigned int tAR; /* ND_ALE low to ND_nRE low delay */
261};
262
263struct pxa3xx_nand_flash {
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300264 uint32_t chip_id;
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300265 unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */
266 unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300267 struct pxa3xx_nand_timing *timing; /* NAND Flash timing */
268};
269
Lei Wenc1f82472010-08-17 13:50:23 +0800270static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800271 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
272 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
273 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
274 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800275};
276
Lei Wenc1f82472010-08-17 13:50:23 +0800277static struct pxa3xx_nand_flash builtin_flash_types[] = {
Antoine Ténart89c17022015-10-21 10:29:04 +0200278 { 0x46ec, 16, 16, &timing[1] },
279 { 0xdaec, 8, 8, &timing[1] },
280 { 0xd7ec, 8, 8, &timing[1] },
281 { 0xa12c, 8, 8, &timing[2] },
282 { 0xb12c, 16, 16, &timing[2] },
283 { 0xdc2c, 8, 8, &timing[2] },
284 { 0xcc2c, 16, 16, &timing[2] },
285 { 0xba20, 16, 16, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800286};
287
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300288static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
289static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
290
291static struct nand_bbt_descr bbt_main_descr = {
292 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
293 | NAND_BBT_2BIT | NAND_BBT_VERSION,
294 .offs = 8,
295 .len = 6,
296 .veroffs = 14,
297 .maxblocks = 8, /* Last 8 blocks in each chip */
298 .pattern = bbt_pattern
299};
300
301static struct nand_bbt_descr bbt_mirror_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_mirror_pattern
309};
310
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100311static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
312 .eccbytes = 32,
313 .eccpos = {
314 32, 33, 34, 35, 36, 37, 38, 39,
315 40, 41, 42, 43, 44, 45, 46, 47,
316 48, 49, 50, 51, 52, 53, 54, 55,
317 56, 57, 58, 59, 60, 61, 62, 63},
318 .oobfree = { {2, 30} }
319};
320
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300321static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
322 .eccbytes = 64,
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 96, 97, 98, 99, 100, 101, 102, 103,
329 104, 105, 106, 107, 108, 109, 110, 111,
330 112, 113, 114, 115, 116, 117, 118, 119,
331 120, 121, 122, 123, 124, 125, 126, 127},
332 /* Bootrom looks in bytes 0 & 5 for bad blocks */
333 .oobfree = { {6, 26}, { 64, 32} }
334};
335
336static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
337 .eccbytes = 128,
338 .eccpos = {
339 32, 33, 34, 35, 36, 37, 38, 39,
340 40, 41, 42, 43, 44, 45, 46, 47,
341 48, 49, 50, 51, 52, 53, 54, 55,
342 56, 57, 58, 59, 60, 61, 62, 63},
343 .oobfree = { }
344};
345
eric miaofe69af02008-02-14 15:48:23 +0800346#define NDTR0_tCH(c) (min((c), 7) << 19)
347#define NDTR0_tCS(c) (min((c), 7) << 16)
348#define NDTR0_tWH(c) (min((c), 7) << 11)
349#define NDTR0_tWP(c) (min((c), 7) << 8)
350#define NDTR0_tRH(c) (min((c), 7) << 3)
351#define NDTR0_tRP(c) (min((c), 7) << 0)
352
353#define NDTR1_tR(c) (min((c), 65535) << 16)
354#define NDTR1_tWHR(c) (min((c), 15) << 4)
355#define NDTR1_tAR(c) (min((c), 15) << 0)
356
357/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800358#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800359
Jingoo Han17754ad2014-05-07 17:49:13 +0900360static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300361 {
362 .compatible = "marvell,pxa3xx-nand",
363 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
364 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300365 {
366 .compatible = "marvell,armada370-nand",
367 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
368 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300369 {}
370};
371MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
372
373static enum pxa3xx_nand_variant
374pxa3xx_nand_get_variant(struct platform_device *pdev)
375{
376 const struct of_device_id *of_id =
377 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
378 if (!of_id)
379 return PXA3XX_NAND_VARIANT_PXA;
380 return (enum pxa3xx_nand_variant)of_id->data;
381}
382
Lei Wend4568822011-07-14 20:44:32 -0700383static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200384 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800385{
Lei Wend4568822011-07-14 20:44:32 -0700386 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800387 unsigned long nand_clk = clk_get_rate(info->clk);
388 uint32_t ndtr0, ndtr1;
389
390 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
391 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
392 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
393 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
394 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
395 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
396
397 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
398 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
399 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
400
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300401 info->ndtr0cs0 = ndtr0;
402 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800403 nand_writel(info, NDTR0CS0, ndtr0);
404 nand_writel(info, NDTR1CS0, ndtr1);
405}
406
Antoine Ténart3f225b72015-10-21 10:29:02 +0200407static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
408 const struct nand_sdr_timings *t)
409{
410 struct pxa3xx_nand_info *info = host->info_data;
411 struct nand_chip *chip = &host->chip;
412 unsigned long nand_clk = clk_get_rate(info->clk);
413 uint32_t ndtr0, ndtr1;
414
415 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
416 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
417 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
418 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
419 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
420 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
421 u32 tR = chip->chip_delay * 1000;
422 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
423 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
424
425 /* fallback to a default value if tR = 0 */
426 if (!tR)
427 tR = 20000;
428
429 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
430 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
431 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
432 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
433 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
434 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
435
436 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
437 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
438 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
439
440 info->ndtr0cs0 = ndtr0;
441 info->ndtr1cs0 = ndtr1;
442 nand_writel(info, NDTR0CS0, ndtr0);
443 nand_writel(info, NDTR1CS0, ndtr1);
444}
445
446static int pxa3xx_nand_init_timings_compat(struct pxa3xx_nand_host *host,
447 unsigned int *flash_width,
448 unsigned int *dfc_width)
449{
450 struct nand_chip *chip = &host->chip;
451 struct pxa3xx_nand_info *info = host->info_data;
452 const struct pxa3xx_nand_flash *f = NULL;
453 int i, id, ntypes;
454
455 ntypes = ARRAY_SIZE(builtin_flash_types);
456
457 chip->cmdfunc(host->mtd, NAND_CMD_READID, 0x00, -1);
458
459 id = chip->read_byte(host->mtd);
460 id |= chip->read_byte(host->mtd) << 0x8;
461
462 for (i = 0; i < ntypes; i++) {
463 f = &builtin_flash_types[i];
464
465 if (f->chip_id == id)
466 break;
467 }
468
469 if (i == ntypes) {
470 dev_err(&info->pdev->dev, "Error: timings not found\n");
471 return -EINVAL;
472 }
473
474 pxa3xx_nand_set_timing(host, f->timing);
475
476 *flash_width = f->flash_width;
477 *dfc_width = f->dfc_width;
478
479 return 0;
480}
481
482static int pxa3xx_nand_init_timings_onfi(struct pxa3xx_nand_host *host,
483 int mode)
484{
485 const struct nand_sdr_timings *timings;
486
487 mode = fls(mode) - 1;
488 if (mode < 0)
489 mode = 0;
490
491 timings = onfi_async_timing_mode_to_sdr_timings(mode);
492 if (IS_ERR(timings))
493 return PTR_ERR(timings);
494
495 pxa3xx_nand_set_sdr_timing(host, timings);
496
497 return 0;
498}
499
500static int pxa3xx_nand_init(struct pxa3xx_nand_host *host)
501{
502 struct nand_chip *chip = &host->chip;
503 struct pxa3xx_nand_info *info = host->info_data;
504 unsigned int flash_width = 0, dfc_width = 0;
505 int mode, err;
506
507 mode = onfi_get_async_timing_mode(chip);
508 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
509 err = pxa3xx_nand_init_timings_compat(host, &flash_width,
510 &dfc_width);
511 if (err)
512 return err;
513
514 if (flash_width == 16) {
515 info->reg_ndcr |= NDCR_DWIDTH_M;
516 chip->options |= NAND_BUSWIDTH_16;
517 }
518
519 info->reg_ndcr |= (dfc_width == 16) ? NDCR_DWIDTH_C : 0;
520 } else {
521 err = pxa3xx_nand_init_timings_onfi(host, mode);
522 if (err)
523 return err;
524 }
525
526 return 0;
527}
528
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300529/*
530 * Set the data and OOB size, depending on the selected
531 * spare and ECC configuration.
532 * Only applicable to READ0, READOOB and PAGEPROG commands.
533 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300534static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
535 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800536{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300537 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800538
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300539 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300540 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800541 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800542
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300543 info->oob_size = info->spare_size;
544 if (!info->use_ecc)
545 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800546}
547
Lei Wenf8155a42011-02-28 10:32:11 +0800548/**
549 * NOTE: it is a must to set ND_RUN firstly, then write
550 * command buffer, otherwise, it does not work.
551 * We enable all the interrupt at the same time, and
552 * let pxa3xx_nand_irq to handle all logic.
553 */
554static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
555{
556 uint32_t ndcr;
557
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300558 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300559
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300560 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300561 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300562 if (info->ecc_bch)
563 nand_writel(info, NDECCCTRL, 0x1);
564 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300565 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300566 if (info->ecc_bch)
567 nand_writel(info, NDECCCTRL, 0x0);
568 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300569
570 if (info->use_dma)
571 ndcr |= NDCR_DMA_EN;
572 else
573 ndcr &= ~NDCR_DMA_EN;
574
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300575 if (info->use_spare)
576 ndcr |= NDCR_SPARE_EN;
577 else
578 ndcr &= ~NDCR_SPARE_EN;
579
Lei Wenf8155a42011-02-28 10:32:11 +0800580 ndcr |= NDCR_ND_RUN;
581
582 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800583 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200584 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800585 nand_writel(info, NDCR, ndcr);
586}
587
588static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
589{
590 uint32_t ndcr;
591 int timeout = NAND_STOP_DELAY;
592
593 /* wait RUN bit in NDCR become 0 */
594 ndcr = nand_readl(info, NDCR);
595 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
596 ndcr = nand_readl(info, NDCR);
597 udelay(1);
598 }
599
600 if (timeout <= 0) {
601 ndcr &= ~NDCR_ND_RUN;
602 nand_writel(info, NDCR, ndcr);
603 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200604 if (info->dma_chan)
605 dmaengine_terminate_all(info->dma_chan);
606
Lei Wenf8155a42011-02-28 10:32:11 +0800607 /* clear status bits */
608 nand_writel(info, NDSR, NDSR_MASK);
609}
610
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300611static void __maybe_unused
612enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800613{
614 uint32_t ndcr;
615
616 ndcr = nand_readl(info, NDCR);
617 nand_writel(info, NDCR, ndcr & ~int_mask);
618}
619
620static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
621{
622 uint32_t ndcr;
623
624 ndcr = nand_readl(info, NDCR);
625 nand_writel(info, NDCR, ndcr | int_mask);
626}
627
Maxime Ripard8dad0382015-02-18 11:32:07 +0100628static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
629{
630 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200631 u32 val;
632 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100633
634 /*
635 * According to the datasheet, when reading from NDDB
636 * with BCH enabled, after each 32 bytes reads, we
637 * have to make sure that the NDSR.RDDREQ bit is set.
638 *
639 * Drain the FIFO 8 32 bits reads at a time, and skip
640 * the polling on the last read.
641 */
642 while (len > 8) {
Antoine Ténartab53a572015-10-21 10:29:00 +0200643 ioread32_rep(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100644
Maxime Ripardafca11e2015-04-07 15:32:45 +0200645 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
646 val & NDSR_RDDREQ, 1000, 5000);
647 if (ret) {
648 dev_err(&info->pdev->dev,
649 "Timeout on RDDREQ while draining the FIFO\n");
650 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100651 }
652
653 data += 32;
654 len -= 8;
655 }
656 }
657
Antoine Ténartab53a572015-10-21 10:29:00 +0200658 ioread32_rep(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100659}
660
Lei Wenf8155a42011-02-28 10:32:11 +0800661static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800662{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300663 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300664
eric miaofe69af02008-02-14 15:48:23 +0800665 switch (info->state) {
666 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500667 writesl(info->mmio_base + NDDB,
668 info->data_buff + info->data_buff_pos,
669 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300670
Lei Wen9d8b1042010-08-17 14:09:30 +0800671 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500672 writesl(info->mmio_base + NDDB,
673 info->oob_buff + info->oob_buff_pos,
674 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800675 break;
676 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100677 drain_fifo(info,
678 info->data_buff + info->data_buff_pos,
679 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300680
Lei Wen9d8b1042010-08-17 14:09:30 +0800681 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100682 drain_fifo(info,
683 info->oob_buff + info->oob_buff_pos,
684 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800685 break;
686 default:
Lei Wenda675b42011-07-14 20:44:31 -0700687 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800688 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800689 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800690 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300691
692 /* Update buffer pointers for multi-page read/write */
693 info->data_buff_pos += do_bytes;
694 info->oob_buff_pos += info->oob_size;
695 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800696}
697
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200698static void pxa3xx_nand_data_dma_irq(void *data)
699{
700 struct pxa3xx_nand_info *info = data;
701 struct dma_tx_state state;
702 enum dma_status status;
703
704 status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state);
705 if (likely(status == DMA_COMPLETE)) {
706 info->state = STATE_DMA_DONE;
707 } else {
708 dev_err(&info->pdev->dev, "DMA error on data channel\n");
709 info->retcode = ERR_DMABUSERR;
710 }
711 dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
712
713 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
714 enable_int(info, NDCR_INT_MASK);
715}
716
Lei Wenf8155a42011-02-28 10:32:11 +0800717static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800718{
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200719 enum dma_transfer_direction direction;
720 struct dma_async_tx_descriptor *tx;
eric miaofe69af02008-02-14 15:48:23 +0800721
Lei Wenf8155a42011-02-28 10:32:11 +0800722 switch (info->state) {
723 case STATE_DMA_WRITING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200724 info->dma_dir = DMA_TO_DEVICE;
725 direction = DMA_MEM_TO_DEV;
Lei Wenf8155a42011-02-28 10:32:11 +0800726 break;
727 case STATE_DMA_READING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200728 info->dma_dir = DMA_FROM_DEVICE;
729 direction = DMA_DEV_TO_MEM;
Lei Wenf8155a42011-02-28 10:32:11 +0800730 break;
731 default:
Lei Wenda675b42011-07-14 20:44:31 -0700732 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800733 info->state);
734 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800735 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200736 info->sg.length = info->data_size +
737 (info->oob_size ? info->spare_size + info->ecc_size : 0);
738 dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
eric miaofe69af02008-02-14 15:48:23 +0800739
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200740 tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
741 DMA_PREP_INTERRUPT);
742 if (!tx) {
743 dev_err(&info->pdev->dev, "prep_slave_sg() failed\n");
744 return;
eric miaofe69af02008-02-14 15:48:23 +0800745 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200746 tx->callback = pxa3xx_nand_data_dma_irq;
747 tx->callback_param = info;
748 info->dma_cookie = dmaengine_submit(tx);
749 dma_async_issue_pending(info->dma_chan);
750 dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n",
751 __func__, direction, info->dma_cookie, info->sg.length);
eric miaofe69af02008-02-14 15:48:23 +0800752}
753
Robert Jarzmik24542252015-02-20 19:36:43 +0100754static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
755{
756 struct pxa3xx_nand_info *info = data;
757
758 handle_data_pio(info);
759
760 info->state = STATE_CMD_DONE;
761 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
762
763 return IRQ_HANDLED;
764}
765
eric miaofe69af02008-02-14 15:48:23 +0800766static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
767{
768 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300769 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700770 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100771 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700772
773 if (info->cs == 0) {
774 ready = NDSR_FLASH_RDY;
775 cmd_done = NDSR_CS0_CMDD;
776 } else {
777 ready = NDSR_RDY;
778 cmd_done = NDSR_CS1_CMDD;
779 }
eric miaofe69af02008-02-14 15:48:23 +0800780
781 status = nand_readl(info, NDSR);
782
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300783 if (status & NDSR_UNCORERR)
784 info->retcode = ERR_UNCORERR;
785 if (status & NDSR_CORERR) {
786 info->retcode = ERR_CORERR;
787 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
788 info->ecc_bch)
789 info->ecc_err_cnt = NDSR_ERR_CNT(status);
790 else
791 info->ecc_err_cnt = 1;
792
793 /*
794 * Each chunk composing a page is corrected independently,
795 * and we need to store maximum number of corrected bitflips
796 * to return it to the MTD layer in ecc.read_page().
797 */
798 info->max_bitflips = max_t(unsigned int,
799 info->max_bitflips,
800 info->ecc_err_cnt);
801 }
Lei Wenf8155a42011-02-28 10:32:11 +0800802 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
803 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800804 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800805 disable_int(info, NDCR_INT_MASK);
806 info->state = (status & NDSR_RDDREQ) ?
807 STATE_DMA_READING : STATE_DMA_WRITING;
808 start_data_dma(info);
809 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800810 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800811 info->state = (status & NDSR_RDDREQ) ?
812 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100813 ret = IRQ_WAKE_THREAD;
814 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800815 }
Lei Wenf8155a42011-02-28 10:32:11 +0800816 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700817 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800818 info->state = STATE_CMD_DONE;
819 is_completed = 1;
820 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700821 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800822 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300823 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800824 }
Lei Wenf8155a42011-02-28 10:32:11 +0800825
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200826 /*
827 * Clear all status bit before issuing the next command, which
828 * can and will alter the status bits and will deserve a new
829 * interrupt on its own. This lets the controller exit the IRQ
830 */
831 nand_writel(info, NDSR, status);
832
Lei Wenf8155a42011-02-28 10:32:11 +0800833 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800834 status &= ~NDSR_WRCMDREQ;
835 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300836
837 /*
838 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
839 * must be loaded by writing directly either 12 or 16
840 * bytes directly to NDCB0, four bytes at a time.
841 *
842 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
843 * but each NDCBx register can be read.
844 */
Lei Wenf8155a42011-02-28 10:32:11 +0800845 nand_writel(info, NDCB0, info->ndcb0);
846 nand_writel(info, NDCB0, info->ndcb1);
847 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300848
849 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
850 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
851 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800852 }
Lei Wenf8155a42011-02-28 10:32:11 +0800853
Lei Wenf8155a42011-02-28 10:32:11 +0800854 if (is_completed)
855 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300856 if (is_ready)
857 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800858NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100859 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800860}
861
eric miaofe69af02008-02-14 15:48:23 +0800862static inline int is_buf_blank(uint8_t *buf, size_t len)
863{
864 for (; len > 0; len--)
865 if (*buf++ != 0xff)
866 return 0;
867 return 1;
868}
869
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300870static void set_command_address(struct pxa3xx_nand_info *info,
871 unsigned int page_size, uint16_t column, int page_addr)
872{
873 /* small page addr setting */
874 if (page_size < PAGE_CHUNK_SIZE) {
875 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
876 | (column & 0xFF);
877
878 info->ndcb2 = 0;
879 } else {
880 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
881 | (column & 0xFFFF);
882
883 if (page_addr & 0xFF0000)
884 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
885 else
886 info->ndcb2 = 0;
887 }
888}
889
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300890static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800891{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300892 struct pxa3xx_nand_host *host = info->host[info->cs];
893 struct mtd_info *mtd = host->mtd;
894
Lei Wen4eb2da82011-02-28 10:32:13 +0800895 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800896 info->buf_start = 0;
897 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800898 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300899 info->data_buff_pos = 0;
900 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800901 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300902 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800903 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300904 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300905 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300906 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800907
908 switch (command) {
909 case NAND_CMD_READ0:
910 case NAND_CMD_PAGEPROG:
911 info->use_ecc = 1;
912 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300913 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800914 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300915 case NAND_CMD_PARAM:
916 info->use_spare = 0;
917 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800918 default:
919 info->ndcb1 = 0;
920 info->ndcb2 = 0;
921 break;
922 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300923
924 /*
925 * If we are about to issue a read command, or about to set
926 * the write address, then clean the data buffer.
927 */
928 if (command == NAND_CMD_READ0 ||
929 command == NAND_CMD_READOOB ||
930 command == NAND_CMD_SEQIN) {
931
932 info->buf_count = mtd->writesize + mtd->oobsize;
933 memset(info->data_buff, 0xFF, info->buf_count);
934 }
935
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300936}
937
938static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300939 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300940{
941 int addr_cycle, exec_cmd;
942 struct pxa3xx_nand_host *host;
943 struct mtd_info *mtd;
944
945 host = info->host[info->cs];
946 mtd = host->mtd;
947 addr_cycle = 0;
948 exec_cmd = 1;
949
950 if (info->cs != 0)
951 info->ndcb0 = NDCB0_CSEL;
952 else
953 info->ndcb0 = 0;
954
955 if (command == NAND_CMD_SEQIN)
956 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800957
Lei Wend4568822011-07-14 20:44:32 -0700958 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
959 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800960
961 switch (command) {
962 case NAND_CMD_READOOB:
963 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300964 info->buf_start = column;
965 info->ndcb0 |= NDCB0_CMD_TYPE(0)
966 | addr_cycle
967 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800968
Ezequiel Garciaec821352013-08-12 14:14:54 -0300969 if (command == NAND_CMD_READOOB)
970 info->buf_start += mtd->writesize;
971
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300972 /*
973 * Multiple page read needs an 'extended command type' field,
974 * which is either naked-read or last-read according to the
975 * state.
976 */
977 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300978 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300979 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
980 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
981 | NDCB0_LEN_OVRD
982 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
983 info->ndcb3 = info->chunk_size +
984 info->oob_size;
985 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800986
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300987 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300988 break;
989
Lei Wen4eb2da82011-02-28 10:32:13 +0800990 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800991
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300992 info->buf_start = column;
993 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300994
995 /*
996 * Multiple page programming needs to execute the initial
997 * SEQIN command that sets the page address.
998 */
999 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1000 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1001 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1002 | addr_cycle
1003 | command;
1004 /* No data transfer in this case */
1005 info->data_size = 0;
1006 exec_cmd = 1;
1007 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001008 break;
1009
1010 case NAND_CMD_PAGEPROG:
1011 if (is_buf_blank(info->data_buff,
1012 (mtd->writesize + mtd->oobsize))) {
1013 exec_cmd = 0;
1014 break;
1015 }
1016
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001017 /* Second command setting for large pages */
1018 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1019 /*
1020 * Multiple page write uses the 'extended command'
1021 * field. This can be used to issue a command dispatch
1022 * or a naked-write depending on the current stage.
1023 */
1024 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1025 | NDCB0_LEN_OVRD
1026 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
1027 info->ndcb3 = info->chunk_size +
1028 info->oob_size;
1029
1030 /*
1031 * This is the command dispatch that completes a chunked
1032 * page program operation.
1033 */
1034 if (info->data_size == 0) {
1035 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
1036 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1037 | command;
1038 info->ndcb1 = 0;
1039 info->ndcb2 = 0;
1040 info->ndcb3 = 0;
1041 }
1042 } else {
1043 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1044 | NDCB0_AUTO_RS
1045 | NDCB0_ST_ROW_EN
1046 | NDCB0_DBC
1047 | (NAND_CMD_PAGEPROG << 8)
1048 | NAND_CMD_SEQIN
1049 | addr_cycle;
1050 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001051 break;
1052
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001053 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -03001054 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001055 info->ndcb0 |= NDCB0_CMD_TYPE(0)
1056 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -03001057 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -03001058 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001059 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -03001060 info->ndcb3 = INIT_BUFFER_SIZE;
1061 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001062 break;
1063
Lei Wen4eb2da82011-02-28 10:32:13 +08001064 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001065 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +08001066 info->ndcb0 |= NDCB0_CMD_TYPE(3)
1067 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001068 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -03001069 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +08001070
1071 info->data_size = 8;
1072 break;
1073 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +08001074 info->buf_count = 1;
1075 info->ndcb0 |= NDCB0_CMD_TYPE(4)
1076 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001077 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001078
1079 info->data_size = 8;
1080 break;
1081
1082 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +08001083 info->ndcb0 |= NDCB0_CMD_TYPE(2)
1084 | NDCB0_AUTO_RS
1085 | NDCB0_ADDR_CYC(3)
1086 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -03001087 | (NAND_CMD_ERASE2 << 8)
1088 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +08001089 info->ndcb1 = page_addr;
1090 info->ndcb2 = 0;
1091
1092 break;
1093 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +08001094 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001095 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001096
1097 break;
1098
1099 case NAND_CMD_ERASE2:
1100 exec_cmd = 0;
1101 break;
1102
1103 default:
1104 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -07001105 dev_err(&info->pdev->dev, "non-supported command %x\n",
1106 command);
Lei Wen4eb2da82011-02-28 10:32:13 +08001107 break;
1108 }
1109
1110 return exec_cmd;
1111}
1112
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001113static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1114 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +08001115{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001116 struct nand_chip *chip = mtd->priv;
1117 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001118 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001119 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +08001120
Lei Wen4eb2da82011-02-28 10:32:13 +08001121 /*
1122 * if this is a x16 device ,then convert the input
1123 * "byte" address into a "word" address appropriate
1124 * for indexing a word-oriented device
1125 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001126 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +08001127 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +08001128
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001129 /*
1130 * There may be different NAND chip hooked to
1131 * different chip select, so check whether
1132 * chip select has been changed, if yes, reset the timing
1133 */
1134 if (info->cs != host->cs) {
1135 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001136 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1137 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001138 }
1139
Ezequiel Garciac39ff032013-11-14 18:25:33 -03001140 prepare_start_command(info, command);
1141
Lei Wend4568822011-07-14 20:44:32 -07001142 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001143 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1144
Lei Wenf8155a42011-02-28 10:32:11 +08001145 if (exec_cmd) {
1146 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001147 init_completion(&info->dev_ready);
1148 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001149 pxa3xx_nand_start(info);
1150
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001151 if (!wait_for_completion_timeout(&info->cmd_complete,
1152 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001153 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001154 /* Stop State Machine for next command cycle */
1155 pxa3xx_nand_stop(info);
1156 }
eric miaofe69af02008-02-14 15:48:23 +08001157 }
Lei Wend4568822011-07-14 20:44:32 -07001158 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001159}
1160
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001161static void nand_cmdfunc_extended(struct mtd_info *mtd,
1162 const unsigned command,
1163 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001164{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001165 struct nand_chip *chip = mtd->priv;
1166 struct pxa3xx_nand_host *host = chip->priv;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001167 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001168 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001169
1170 /*
1171 * if this is a x16 device then convert the input
1172 * "byte" address into a "word" address appropriate
1173 * for indexing a word-oriented device
1174 */
1175 if (info->reg_ndcr & NDCR_DWIDTH_M)
1176 column /= 2;
1177
1178 /*
1179 * There may be different NAND chip hooked to
1180 * different chip select, so check whether
1181 * chip select has been changed, if yes, reset the timing
1182 */
1183 if (info->cs != host->cs) {
1184 info->cs = host->cs;
1185 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1186 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1187 }
1188
1189 /* Select the extended command for the first command */
1190 switch (command) {
1191 case NAND_CMD_READ0:
1192 case NAND_CMD_READOOB:
1193 ext_cmd_type = EXT_CMD_TYPE_MONO;
1194 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001195 case NAND_CMD_SEQIN:
1196 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1197 break;
1198 case NAND_CMD_PAGEPROG:
1199 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1200 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001201 default:
1202 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001203 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001204 }
1205
1206 prepare_start_command(info, command);
1207
1208 /*
1209 * Prepare the "is ready" completion before starting a command
1210 * transaction sequence. If the command is not executed the
1211 * completion will be completed, see below.
1212 *
1213 * We can do that inside the loop because the command variable
1214 * is invariant and thus so is the exec_cmd.
1215 */
1216 info->need_wait = 1;
1217 init_completion(&info->dev_ready);
1218 do {
1219 info->state = STATE_PREPARED;
1220 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1221 column, page_addr);
1222 if (!exec_cmd) {
1223 info->need_wait = 0;
1224 complete(&info->dev_ready);
1225 break;
1226 }
1227
1228 init_completion(&info->cmd_complete);
1229 pxa3xx_nand_start(info);
1230
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001231 if (!wait_for_completion_timeout(&info->cmd_complete,
1232 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001233 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1234 /* Stop State Machine for next command cycle */
1235 pxa3xx_nand_stop(info);
1236 break;
1237 }
1238
1239 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001240 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1241 break;
1242
1243 /*
1244 * After a splitted program command sequence has issued
1245 * the command dispatch, the command sequence is complete.
1246 */
1247 if (info->data_size == 0 &&
1248 command == NAND_CMD_PAGEPROG &&
1249 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001250 break;
1251
1252 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1253 /* Last read: issue a 'last naked read' */
1254 if (info->data_size == info->chunk_size)
1255 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1256 else
1257 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001258
1259 /*
1260 * If a splitted program command has no more data to transfer,
1261 * the command dispatch must be issued to complete.
1262 */
1263 } else if (command == NAND_CMD_PAGEPROG &&
1264 info->data_size == 0) {
1265 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001266 }
1267 } while (1);
1268
1269 info->state = STATE_IDLE;
1270}
1271
Josh Wufdbad98d2012-06-25 18:07:45 +08001272static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001273 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1274 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001275{
1276 chip->write_buf(mtd, buf, mtd->writesize);
1277 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001278
1279 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001280}
1281
1282static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001283 struct nand_chip *chip, uint8_t *buf, int oob_required,
1284 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001285{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001286 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001287 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001288
1289 chip->read_buf(mtd, buf, mtd->writesize);
1290 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1291
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001292 if (info->retcode == ERR_CORERR && info->use_ecc) {
1293 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1294
1295 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001296 /*
1297 * for blank page (all 0xff), HW will calculate its ECC as
1298 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001299 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001300 */
1301 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001302 info->retcode = ERR_NONE;
1303 else
Lei Wenf8155a42011-02-28 10:32:11 +08001304 mtd->ecc_stats.failed++;
1305 }
1306
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001307 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001308}
1309
eric miaofe69af02008-02-14 15:48:23 +08001310static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1311{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001312 struct nand_chip *chip = mtd->priv;
1313 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001314 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001315 char retval = 0xFF;
1316
1317 if (info->buf_start < info->buf_count)
1318 /* Has just send a new command? */
1319 retval = info->data_buff[info->buf_start++];
1320
1321 return retval;
1322}
1323
1324static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1325{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001326 struct nand_chip *chip = mtd->priv;
1327 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001328 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001329 u16 retval = 0xFFFF;
1330
1331 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1332 retval = *((u16 *)(info->data_buff+info->buf_start));
1333 info->buf_start += 2;
1334 }
1335 return retval;
1336}
1337
1338static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1339{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001340 struct nand_chip *chip = mtd->priv;
1341 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001342 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001343 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1344
1345 memcpy(buf, info->data_buff + info->buf_start, real_len);
1346 info->buf_start += real_len;
1347}
1348
1349static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1350 const uint8_t *buf, int len)
1351{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001352 struct nand_chip *chip = mtd->priv;
1353 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001354 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001355 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1356
1357 memcpy(info->data_buff + info->buf_start, buf, real_len);
1358 info->buf_start += real_len;
1359}
1360
eric miaofe69af02008-02-14 15:48:23 +08001361static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1362{
1363 return;
1364}
1365
1366static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1367{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001368 struct nand_chip *chip = mtd->priv;
1369 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001370 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001371
1372 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001373 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001374 if (!wait_for_completion_timeout(&info->dev_ready,
1375 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001376 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1377 return NAND_STATUS_FAIL;
1378 }
1379 }
eric miaofe69af02008-02-14 15:48:23 +08001380
1381 /* pxa3xx_nand_send_command has waited for command complete */
1382 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1383 if (info->retcode == ERR_NONE)
1384 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001385 else
1386 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001387 }
1388
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001389 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001390}
1391
Ezequiel García66e8e472015-11-04 13:13:42 -03001392static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001393{
Ezequiel Garcíab1e48572015-11-04 13:13:44 -03001394 struct pxa3xx_nand_host *host = info->host[info->cs];
eric miaofe69af02008-02-14 15:48:23 +08001395 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001396 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcíab1e48572015-11-04 13:13:44 -03001397 const struct nand_sdr_timings *timings;
eric miaofe69af02008-02-14 15:48:23 +08001398
Ezequiel García66e8e472015-11-04 13:13:42 -03001399 /* Configure default flash values */
1400 info->chunk_size = PAGE_CHUNK_SIZE;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001401 info->reg_ndcr = 0x0; /* enable all interrupts */
1402 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
1403 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
Ezequiel García66e8e472015-11-04 13:13:42 -03001404 info->reg_ndcr |= NDCR_SPARE_EN;
eric miaofe69af02008-02-14 15:48:23 +08001405
Ezequiel Garcíab1e48572015-11-04 13:13:44 -03001406 /* use the common timing to make a try */
1407 timings = onfi_async_timing_mode_to_sdr_timings(0);
1408 if (IS_ERR(timings))
1409 return PTR_ERR(timings);
1410
1411 pxa3xx_nand_set_sdr_timing(host, timings);
eric miaofe69af02008-02-14 15:48:23 +08001412 return 0;
1413}
1414
Ezequiel García66e8e472015-11-04 13:13:42 -03001415static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
1416{
1417 struct pxa3xx_nand_host *host = info->host[info->cs];
1418 struct mtd_info *mtd = host->mtd;
1419 struct nand_chip *chip = mtd->priv;
1420
1421 info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
1422 info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
1423 info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
1424}
1425
Ezequiel García154f50f2015-11-04 13:13:43 -03001426static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
Mike Rapoportf2710492009-02-17 13:54:47 +02001427{
Ezequiel García66e8e472015-11-04 13:13:42 -03001428 struct platform_device *pdev = info->pdev;
1429 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Mike Rapoportf2710492009-02-17 13:54:47 +02001430 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001431
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001432 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001433 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Robert Jarzmike971aff2015-09-28 22:56:51 +02001434 info->reg_ndcr = ndcr &
1435 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
Ezequiel García66e8e472015-11-04 13:13:42 -03001436 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001437 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1438 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001439}
1440
eric miaofe69af02008-02-14 15:48:23 +08001441static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1442{
1443 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001444 struct dma_slave_config config;
1445 dma_cap_mask_t mask;
1446 struct pxad_param param;
1447 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001448
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001449 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1450 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001451 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001452 if (use_dma == 0)
1453 return 0;
1454
1455 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1456 if (ret)
1457 return ret;
1458
1459 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1460 dma_cap_zero(mask);
1461 dma_cap_set(DMA_SLAVE, mask);
1462 param.prio = PXAD_PRIO_LOWEST;
1463 param.drcmr = info->drcmr_dat;
1464 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1465 &param, &pdev->dev,
1466 "data");
1467 if (!info->dma_chan) {
1468 dev_err(&pdev->dev, "unable to request data dma channel\n");
1469 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001470 }
1471
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001472 memset(&config, 0, sizeof(config));
1473 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1474 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1475 config.src_addr = info->mmio_phys + NDDB;
1476 config.dst_addr = info->mmio_phys + NDDB;
1477 config.src_maxburst = 32;
1478 config.dst_maxburst = 32;
1479 ret = dmaengine_slave_config(info->dma_chan, &config);
1480 if (ret < 0) {
1481 dev_err(&info->pdev->dev,
1482 "dma channel configuration failed: %d\n",
1483 ret);
1484 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001485 }
1486
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001487 /*
1488 * Now that DMA buffers are allocated we turn on
1489 * DMA proper for I/O operations.
1490 */
1491 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001492 return 0;
1493}
1494
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001495static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1496{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001497 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001498 dmaengine_terminate_all(info->dma_chan);
1499 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001500 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001501 kfree(info->data_buff);
1502}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001503
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001504static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1505 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001506 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001507{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001508 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001509 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001510 info->spare_size = 40;
1511 info->ecc_size = 24;
1512 ecc->mode = NAND_ECC_HW;
1513 ecc->size = 512;
1514 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001515
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001516 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001517 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001518 info->spare_size = 8;
1519 info->ecc_size = 8;
1520 ecc->mode = NAND_ECC_HW;
1521 ecc->size = 512;
1522 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001523
Brian Norris6033a942013-11-14 14:41:32 -08001524 /*
1525 * Required ECC: 4-bit correction per 512 bytes
1526 * Select: 16-bit correction per 2048 bytes
1527 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001528 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1529 info->ecc_bch = 1;
1530 info->chunk_size = 2048;
1531 info->spare_size = 32;
1532 info->ecc_size = 32;
1533 ecc->mode = NAND_ECC_HW;
1534 ecc->size = info->chunk_size;
1535 ecc->layout = &ecc_layout_2KB_bch4bit;
1536 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001537
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001538 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001539 info->ecc_bch = 1;
1540 info->chunk_size = 2048;
1541 info->spare_size = 32;
1542 info->ecc_size = 32;
1543 ecc->mode = NAND_ECC_HW;
1544 ecc->size = info->chunk_size;
1545 ecc->layout = &ecc_layout_4KB_bch4bit;
1546 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001547
Brian Norris6033a942013-11-14 14:41:32 -08001548 /*
1549 * Required ECC: 8-bit correction per 512 bytes
1550 * Select: 16-bit correction per 1024 bytes
1551 */
1552 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001553 info->ecc_bch = 1;
1554 info->chunk_size = 1024;
1555 info->spare_size = 0;
1556 info->ecc_size = 32;
1557 ecc->mode = NAND_ECC_HW;
1558 ecc->size = info->chunk_size;
1559 ecc->layout = &ecc_layout_4KB_bch8bit;
1560 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001561 } else {
1562 dev_err(&info->pdev->dev,
1563 "ECC strength %d at page size %d is not supported\n",
1564 strength, page_size);
1565 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001566 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001567
1568 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1569 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001570 return 0;
1571}
1572
Lei Wen401e67e2011-02-28 10:32:14 +08001573static int pxa3xx_nand_scan(struct mtd_info *mtd)
1574{
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001575 struct nand_chip *chip = mtd->priv;
1576 struct pxa3xx_nand_host *host = chip->priv;
Lei Wend4568822011-07-14 20:44:32 -07001577 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001578 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001579 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Antoine Ténartf19fe982015-10-21 10:29:03 +02001580 int ret;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001581 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001582
Ezequiel García154f50f2015-11-04 13:13:43 -03001583 if (pdata->keep_config) {
1584 pxa3xx_nand_detect_config(info);
1585 } else {
1586 ret = pxa3xx_nand_config_ident(info);
1587 if (ret)
1588 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001589 }
1590
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001591 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001592 chip->options |= NAND_BUSWIDTH_16;
1593
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001594 /* Device detection must be done with ECC disabled */
1595 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1596 nand_writel(info, NDECCCTRL, 0x0);
1597
Antoine Ténartf19fe982015-10-21 10:29:03 +02001598 if (nand_scan_ident(mtd, 1, NULL))
Lei Wen4332c112011-03-03 11:27:01 +08001599 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001600
Antoine Ténartf19fe982015-10-21 10:29:03 +02001601 if (!pdata->keep_config) {
1602 ret = pxa3xx_nand_init(host);
1603 if (ret) {
1604 dev_err(&info->pdev->dev, "Failed to init nand: %d\n",
1605 ret);
1606 return ret;
1607 }
1608 }
1609
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001610 if (pdata->flash_bbt) {
1611 /*
1612 * We'll use a bad block table stored in-flash and don't
1613 * allow writing the bad block marker to the flash.
1614 */
1615 chip->bbt_options |= NAND_BBT_USE_FLASH |
1616 NAND_BBT_NO_OOB_BBM;
1617 chip->bbt_td = &bbt_main_descr;
1618 chip->bbt_md = &bbt_mirror_descr;
1619 }
1620
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001621 /*
1622 * If the page size is bigger than the FIFO size, let's check
1623 * we are given the right variant and then switch to the extended
1624 * (aka splitted) command handling,
1625 */
1626 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1627 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1628 chip->cmdfunc = nand_cmdfunc_extended;
1629 } else {
1630 dev_err(&info->pdev->dev,
1631 "unsupported page size on this variant\n");
1632 return -ENODEV;
1633 }
1634 }
1635
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001636 if (pdata->ecc_strength && pdata->ecc_step_size) {
1637 ecc_strength = pdata->ecc_strength;
1638 ecc_step = pdata->ecc_step_size;
1639 } else {
1640 ecc_strength = chip->ecc_strength_ds;
1641 ecc_step = chip->ecc_step_ds;
1642 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001643
1644 /* Set default ECC strength requirements on non-ONFI devices */
1645 if (ecc_strength < 1 && ecc_step < 1) {
1646 ecc_strength = 1;
1647 ecc_step = 512;
1648 }
1649
1650 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1651 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001652 if (ret)
1653 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001654
Lei Wen4332c112011-03-03 11:27:01 +08001655 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001656 if (mtd->writesize >= 2048)
1657 host->col_addr_cycles = 2;
1658 else
1659 host->col_addr_cycles = 1;
1660
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001661 /* release the initial buffer */
1662 kfree(info->data_buff);
1663
1664 /* allocate the real data + oob buffer */
1665 info->buf_size = mtd->writesize + mtd->oobsize;
1666 ret = pxa3xx_nand_init_buff(info);
1667 if (ret)
1668 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001669 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001670
Lei Wen4332c112011-03-03 11:27:01 +08001671 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001672 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001673 else
Lei Wend4568822011-07-14 20:44:32 -07001674 host->row_addr_cycles = 2;
Ezequiel García66e8e472015-11-04 13:13:42 -03001675
1676 if (!pdata->keep_config)
1677 pxa3xx_nand_config_tail(info);
1678
Lei Wen401e67e2011-02-28 10:32:14 +08001679 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001680}
1681
Lei Wend4568822011-07-14 20:44:32 -07001682static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001683{
Brian Norrisa61ae812015-10-30 20:33:25 -07001684 struct device_node *np = pdev->dev.of_node;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001685 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001686 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001687 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001688 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001689 struct mtd_info *mtd;
1690 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001691 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001692
Jingoo Han453810b2013-07-30 17:18:33 +09001693 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001694 if (pdata->num_cs <= 0)
1695 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001696 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1697 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1698 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001699 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001700
eric miaofe69af02008-02-14 15:48:23 +08001701 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001702 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001703 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001704 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001705 chip = (struct nand_chip *)(&mtd[1]);
1706 host = (struct pxa3xx_nand_host *)chip;
1707 info->host[cs] = host;
1708 host->mtd = mtd;
1709 host->cs = cs;
1710 host->info_data = info;
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001711 mtd->priv = chip;
Frans Klaver550dab52015-06-10 22:39:01 +02001712 mtd->dev.parent = &pdev->dev;
Brian Norrisa61ae812015-10-30 20:33:25 -07001713 /* FIXME: all chips use the same device tree partitions */
1714 nand_set_flash_node(chip, np);
eric miaofe69af02008-02-14 15:48:23 +08001715
Boris BREZILLON1d8d8b52015-11-16 14:37:34 +01001716 chip->priv = host;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001717 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1718 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1719 chip->controller = &info->controller;
1720 chip->waitfunc = pxa3xx_nand_waitfunc;
1721 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001722 chip->read_word = pxa3xx_nand_read_word;
1723 chip->read_byte = pxa3xx_nand_read_byte;
1724 chip->read_buf = pxa3xx_nand_read_buf;
1725 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001726 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001727 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001728 }
Lei Wen401e67e2011-02-28 10:32:14 +08001729
1730 spin_lock_init(&chip->controller->lock);
1731 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001732 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001733 if (IS_ERR(info->clk)) {
1734 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001735 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001736 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001737 ret = clk_prepare_enable(info->clk);
1738 if (ret < 0)
1739 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001740
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001741 if (use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001742 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1743 if (r == NULL) {
1744 dev_err(&pdev->dev,
1745 "no resource defined for data DMA\n");
1746 ret = -ENXIO;
1747 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001748 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001749 info->drcmr_dat = r->start;
1750
1751 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1752 if (r == NULL) {
1753 dev_err(&pdev->dev,
1754 "no resource defined for cmd DMA\n");
1755 ret = -ENXIO;
1756 goto fail_disable_clk;
1757 }
1758 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001759 }
eric miaofe69af02008-02-14 15:48:23 +08001760
1761 irq = platform_get_irq(pdev, 0);
1762 if (irq < 0) {
1763 dev_err(&pdev->dev, "no IRQ resource defined\n");
1764 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001765 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001766 }
1767
1768 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001769 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1770 if (IS_ERR(info->mmio_base)) {
1771 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001772 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001773 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001774 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001775
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001776 /* Allocate a buffer to allow flash detection */
1777 info->buf_size = INIT_BUFFER_SIZE;
1778 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1779 if (info->data_buff == NULL) {
1780 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001781 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001782 }
eric miaofe69af02008-02-14 15:48:23 +08001783
Haojian Zhuang346e1252009-09-10 14:27:23 +08001784 /* initialize all interrupts to be disabled */
1785 disable_int(info, NDSR_MASK);
1786
Robert Jarzmik24542252015-02-20 19:36:43 +01001787 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1788 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1789 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001790 if (ret < 0) {
1791 dev_err(&pdev->dev, "failed to request IRQ\n");
1792 goto fail_free_buf;
1793 }
1794
Lei Wene353a202011-03-03 11:08:30 +08001795 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001796
Lei Wend4568822011-07-14 20:44:32 -07001797 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001798
eric miaofe69af02008-02-14 15:48:23 +08001799fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001800 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001801 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001802fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001803 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001804 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001805}
1806
1807static int pxa3xx_nand_remove(struct platform_device *pdev)
1808{
Lei Wene353a202011-03-03 11:08:30 +08001809 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001810 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001811 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001812
Lei Wend4568822011-07-14 20:44:32 -07001813 if (!info)
1814 return 0;
1815
Jingoo Han453810b2013-07-30 17:18:33 +09001816 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001817
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001818 irq = platform_get_irq(pdev, 0);
1819 if (irq >= 0)
1820 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001821 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001822
Robert Jarzmike971aff2015-09-28 22:56:51 +02001823 /*
1824 * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
1825 * In order to prevent a lockup of the system bus, the DFI bus
1826 * arbitration is granted to SMC upon driver removal. This is done by
1827 * setting the x_ARB_CNTL bit, which also prevents the NAND to have
1828 * access to the bus anymore.
1829 */
1830 nand_writel(info, NDCR,
1831 (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
1832 NFCV1_NDCR_ARB_CNTL);
Ezequiel Garciafb320612013-04-17 13:38:12 -03001833 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001834
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001835 for (cs = 0; cs < pdata->num_cs; cs++)
1836 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001837 return 0;
1838}
1839
Daniel Mack1e7ba632012-07-22 19:51:02 +02001840static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1841{
1842 struct pxa3xx_nand_platform_data *pdata;
1843 struct device_node *np = pdev->dev.of_node;
1844 const struct of_device_id *of_id =
1845 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1846
1847 if (!of_id)
1848 return 0;
1849
1850 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1851 if (!pdata)
1852 return -ENOMEM;
1853
1854 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1855 pdata->enable_arbiter = 1;
1856 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1857 pdata->keep_config = 1;
1858 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001859 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001860
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001861 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1862 if (pdata->ecc_strength < 0)
1863 pdata->ecc_strength = 0;
1864
1865 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1866 if (pdata->ecc_step_size < 0)
1867 pdata->ecc_step_size = 0;
1868
Daniel Mack1e7ba632012-07-22 19:51:02 +02001869 pdev->dev.platform_data = pdata;
1870
1871 return 0;
1872}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001873
Lei Wene353a202011-03-03 11:08:30 +08001874static int pxa3xx_nand_probe(struct platform_device *pdev)
1875{
1876 struct pxa3xx_nand_platform_data *pdata;
1877 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001878 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001879
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001880 dma_available = IS_ENABLED(CONFIG_ARM) &&
1881 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1882 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001883 use_dma = 0;
1884 dev_warn(&pdev->dev,
1885 "This platform can't do DMA on this device\n");
1886 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001887
Daniel Mack1e7ba632012-07-22 19:51:02 +02001888 ret = pxa3xx_nand_probe_dt(pdev);
1889 if (ret)
1890 return ret;
1891
Jingoo Han453810b2013-07-30 17:18:33 +09001892 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001893 if (!pdata) {
1894 dev_err(&pdev->dev, "no platform data defined\n");
1895 return -ENODEV;
1896 }
1897
Lei Wend4568822011-07-14 20:44:32 -07001898 ret = alloc_nand_resource(pdev);
1899 if (ret) {
1900 dev_err(&pdev->dev, "alloc nand resource failed\n");
1901 return ret;
1902 }
Lei Wene353a202011-03-03 11:08:30 +08001903
Lei Wend4568822011-07-14 20:44:32 -07001904 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001905 probe_success = 0;
1906 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001907 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001908
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001909 /*
1910 * The mtd name matches the one used in 'mtdparts' kernel
1911 * parameter. This name cannot be changed or otherwise
1912 * user's mtd partitions configuration would get broken.
1913 */
1914 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001915 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001916 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001917 if (ret) {
1918 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1919 cs);
1920 continue;
1921 }
1922
Brian Norrisa61ae812015-10-30 20:33:25 -07001923 ret = mtd_device_register(mtd, pdata->parts[cs],
1924 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001925 if (!ret)
1926 probe_success = 1;
1927 }
1928
1929 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001930 pxa3xx_nand_remove(pdev);
1931 return -ENODEV;
1932 }
1933
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001934 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001935}
1936
eric miaofe69af02008-02-14 15:48:23 +08001937#ifdef CONFIG_PM
Brian Norrisd3e94f32015-10-12 14:07:41 -07001938static int pxa3xx_nand_suspend(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08001939{
Brian Norrisd3e94f32015-10-12 14:07:41 -07001940 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
eric miaofe69af02008-02-14 15:48:23 +08001941
Lei Wenf8155a42011-02-28 10:32:11 +08001942 if (info->state) {
Brian Norrisd3e94f32015-10-12 14:07:41 -07001943 dev_err(dev, "driver busy, state = %d\n", info->state);
eric miaofe69af02008-02-14 15:48:23 +08001944 return -EAGAIN;
1945 }
1946
Ezequiel Garcíad55d31a2015-11-04 13:13:46 -03001947 clk_disable(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001948 return 0;
1949}
1950
Brian Norrisd3e94f32015-10-12 14:07:41 -07001951static int pxa3xx_nand_resume(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08001952{
Brian Norrisd3e94f32015-10-12 14:07:41 -07001953 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
Ezequiel Garcíad55d31a2015-11-04 13:13:46 -03001954 int ret;
1955
1956 ret = clk_enable(info->clk);
1957 if (ret < 0)
1958 return ret;
Lei Wen051fc412011-07-14 20:44:30 -07001959
1960 /* We don't want to handle interrupt without calling mtd routine */
1961 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001962
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001963 /*
1964 * Directly set the chip select to a invalid value,
1965 * then the driver would reset the timing according
1966 * to current chip select at the beginning of cmdfunc
1967 */
1968 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001969
Lei Wen051fc412011-07-14 20:44:30 -07001970 /*
1971 * As the spec says, the NDSR would be updated to 0x1800 when
1972 * doing the nand_clk disable/enable.
1973 * To prevent it damaging state machine of the driver, clear
1974 * all status before resume
1975 */
1976 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001977
Lei Wen18c81b12010-08-17 17:25:57 +08001978 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001979}
1980#else
1981#define pxa3xx_nand_suspend NULL
1982#define pxa3xx_nand_resume NULL
1983#endif
1984
Brian Norrisd3e94f32015-10-12 14:07:41 -07001985static const struct dev_pm_ops pxa3xx_nand_pm_ops = {
1986 .suspend = pxa3xx_nand_suspend,
1987 .resume = pxa3xx_nand_resume,
1988};
1989
eric miaofe69af02008-02-14 15:48:23 +08001990static struct platform_driver pxa3xx_nand_driver = {
1991 .driver = {
1992 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301993 .of_match_table = pxa3xx_nand_dt_ids,
Brian Norrisd3e94f32015-10-12 14:07:41 -07001994 .pm = &pxa3xx_nand_pm_ops,
eric miaofe69af02008-02-14 15:48:23 +08001995 },
1996 .probe = pxa3xx_nand_probe,
1997 .remove = pxa3xx_nand_remove,
eric miaofe69af02008-02-14 15:48:23 +08001998};
1999
Axel Linf99640d2011-11-27 20:45:03 +08002000module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08002001
2002MODULE_LICENSE("GPL");
2003MODULE_DESCRIPTION("PXA3xx NAND controller driver");