blob: 54f92837be8a783a5a72711290b448da952927dc [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)
Robert Jarzmike971aff2015-09-28 22:56:51 +020079#define NFCV1_NDCR_ARB_CNTL (0x1 << 19)
80#define NFCV2_NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080081#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
82#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
83
84#define NDCR_RA_START (0x1 << 15)
85#define NDCR_PG_PER_BLK (0x1 << 14)
86#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080087#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080088
89#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030090#define NDSR_ERR_CNT_OFF (16)
91#define NDSR_ERR_CNT_MASK (0x1f)
92#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080093#define NDSR_RDY (0x1 << 12)
94#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080095#define NDSR_CS0_PAGED (0x1 << 10)
96#define NDSR_CS1_PAGED (0x1 << 9)
97#define NDSR_CS0_CMDD (0x1 << 8)
98#define NDSR_CS1_CMDD (0x1 << 7)
99#define NDSR_CS0_BBD (0x1 << 6)
100#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300101#define NDSR_UNCORERR (0x1 << 4)
102#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +0800103#define NDSR_WRDREQ (0x1 << 2)
104#define NDSR_RDDREQ (0x1 << 1)
105#define NDSR_WRCMDREQ (0x1)
106
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300107#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800108#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800109#define NDCB0_AUTO_RS (0x1 << 25)
110#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300111#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
112#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800113#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
114#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
115#define NDCB0_NC (0x1 << 20)
116#define NDCB0_DBC (0x1 << 19)
117#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
118#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
119#define NDCB0_CMD2_MASK (0xff << 8)
120#define NDCB0_CMD1_MASK (0xff)
121#define NDCB0_ADDR_CYC_SHIFT (16)
122
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300123#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
124#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
125#define EXT_CMD_TYPE_READ 4 /* Read */
126#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
127#define EXT_CMD_TYPE_FINAL 3 /* Final command */
128#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
129#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
130
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300131/*
132 * This should be large enough to read 'ONFI' and 'JEDEC'.
133 * Let's use 7 bytes, which is the maximum ID count supported
134 * by the controller (see NDCR_RD_ID_CNT_MASK).
135 */
136#define READ_ID_BYTES 7
137
eric miaofe69af02008-02-14 15:48:23 +0800138/* macros for registers read/write */
139#define nand_writel(info, off, val) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200140 writel_relaxed((val), (info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800141
142#define nand_readl(info, off) \
Thomas Petazzonib7e460622014-05-22 14:56:52 +0200143 readl_relaxed((info)->mmio_base + (off))
eric miaofe69af02008-02-14 15:48:23 +0800144
145/* error code and state */
146enum {
147 ERR_NONE = 0,
148 ERR_DMABUSERR = -1,
149 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300150 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800151 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300152 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800153};
154
155enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800156 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700157 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800158 STATE_CMD_HANDLE,
159 STATE_DMA_READING,
160 STATE_DMA_WRITING,
161 STATE_DMA_DONE,
162 STATE_PIO_READING,
163 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800164 STATE_CMD_DONE,
165 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800166};
167
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300168enum pxa3xx_nand_variant {
169 PXA3XX_NAND_VARIANT_PXA,
170 PXA3XX_NAND_VARIANT_ARMADA370,
171};
172
Lei Wend4568822011-07-14 20:44:32 -0700173struct pxa3xx_nand_host {
174 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700175 struct mtd_info *mtd;
176 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800177
Lei Wend4568822011-07-14 20:44:32 -0700178 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700179 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700180 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700181
182 /* calculated from pxa3xx_nand_flash data */
183 unsigned int col_addr_cycles;
184 unsigned int row_addr_cycles;
Lei Wend4568822011-07-14 20:44:32 -0700185};
186
187struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800188 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800189 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800190
191 struct clk *clk;
192 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800193 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300194 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800195
196 unsigned int buf_start;
197 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300198 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300199 unsigned int data_buff_pos;
200 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800201
202 /* DMA information */
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200203 struct scatterlist sg;
204 enum dma_data_direction dma_dir;
205 struct dma_chan *dma_chan;
206 dma_cookie_t dma_cookie;
eric miaofe69af02008-02-14 15:48:23 +0800207 int drcmr_dat;
208 int drcmr_cmd;
209
210 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800211 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800212 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800213 int data_dma_ch;
eric miaofe69af02008-02-14 15:48:23 +0800214
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700215 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800216 unsigned int state;
217
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300218 /*
219 * This driver supports NFCv1 (as found in PXA SoC)
220 * and NFCv2 (as found in Armada 370/XP SoC).
221 */
222 enum pxa3xx_nand_variant variant;
223
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700224 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800225 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300226 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800227 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300228 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300229 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800230
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300231 unsigned int data_size; /* data to be read from FIFO */
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300232 unsigned int chunk_size; /* split commands chunk size */
Lei Wend4568822011-07-14 20:44:32 -0700233 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300234 unsigned int spare_size;
235 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300236 unsigned int ecc_err_cnt;
237 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800238 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800239
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300240 /* cached register value */
241 uint32_t reg_ndcr;
242 uint32_t ndtr0cs0;
243 uint32_t ndtr1cs0;
244
eric miaofe69af02008-02-14 15:48:23 +0800245 /* generated NDCBx register values */
246 uint32_t ndcb0;
247 uint32_t ndcb1;
248 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300249 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800250};
251
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030252static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800253module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300254MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800255
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300256struct pxa3xx_nand_timing {
257 unsigned int tCH; /* Enable signal hold time */
258 unsigned int tCS; /* Enable signal setup time */
259 unsigned int tWH; /* ND_nWE high duration */
260 unsigned int tWP; /* ND_nWE pulse time */
261 unsigned int tRH; /* ND_nRE high duration */
262 unsigned int tRP; /* ND_nRE pulse width */
263 unsigned int tR; /* ND_nWE high to ND_nRE low for read */
264 unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */
265 unsigned int tAR; /* ND_ALE low to ND_nRE low delay */
266};
267
268struct pxa3xx_nand_flash {
269 char *name;
270 uint32_t chip_id;
271 unsigned int page_per_block; /* Pages per block (PG_PER_BLK) */
272 unsigned int page_size; /* Page size in bytes (PAGE_SZ) */
273 unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */
274 unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */
275 unsigned int num_blocks; /* Number of physical blocks in Flash */
276
277 struct pxa3xx_nand_timing *timing; /* NAND Flash timing */
278};
279
Lei Wenc1f82472010-08-17 13:50:23 +0800280static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800281 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
282 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
283 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
284 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800285};
286
Lei Wenc1f82472010-08-17 13:50:23 +0800287static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800288{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
289{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
290{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
291{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
292{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
293{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
294{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
295{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
296{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800297};
298
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300299static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
300static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
301
302static struct nand_bbt_descr bbt_main_descr = {
303 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
304 | NAND_BBT_2BIT | NAND_BBT_VERSION,
305 .offs = 8,
306 .len = 6,
307 .veroffs = 14,
308 .maxblocks = 8, /* Last 8 blocks in each chip */
309 .pattern = bbt_pattern
310};
311
312static struct nand_bbt_descr bbt_mirror_descr = {
313 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
314 | NAND_BBT_2BIT | NAND_BBT_VERSION,
315 .offs = 8,
316 .len = 6,
317 .veroffs = 14,
318 .maxblocks = 8, /* Last 8 blocks in each chip */
319 .pattern = bbt_mirror_pattern
320};
321
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100322static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
323 .eccbytes = 32,
324 .eccpos = {
325 32, 33, 34, 35, 36, 37, 38, 39,
326 40, 41, 42, 43, 44, 45, 46, 47,
327 48, 49, 50, 51, 52, 53, 54, 55,
328 56, 57, 58, 59, 60, 61, 62, 63},
329 .oobfree = { {2, 30} }
330};
331
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300332static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
333 .eccbytes = 64,
334 .eccpos = {
335 32, 33, 34, 35, 36, 37, 38, 39,
336 40, 41, 42, 43, 44, 45, 46, 47,
337 48, 49, 50, 51, 52, 53, 54, 55,
338 56, 57, 58, 59, 60, 61, 62, 63,
339 96, 97, 98, 99, 100, 101, 102, 103,
340 104, 105, 106, 107, 108, 109, 110, 111,
341 112, 113, 114, 115, 116, 117, 118, 119,
342 120, 121, 122, 123, 124, 125, 126, 127},
343 /* Bootrom looks in bytes 0 & 5 for bad blocks */
344 .oobfree = { {6, 26}, { 64, 32} }
345};
346
347static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
348 .eccbytes = 128,
349 .eccpos = {
350 32, 33, 34, 35, 36, 37, 38, 39,
351 40, 41, 42, 43, 44, 45, 46, 47,
352 48, 49, 50, 51, 52, 53, 54, 55,
353 56, 57, 58, 59, 60, 61, 62, 63},
354 .oobfree = { }
355};
356
Lei Wen227a8862010-08-18 18:00:03 +0800357/* Define a default flash type setting serve as flash detecting only */
358#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
359
eric miaofe69af02008-02-14 15:48:23 +0800360#define NDTR0_tCH(c) (min((c), 7) << 19)
361#define NDTR0_tCS(c) (min((c), 7) << 16)
362#define NDTR0_tWH(c) (min((c), 7) << 11)
363#define NDTR0_tWP(c) (min((c), 7) << 8)
364#define NDTR0_tRH(c) (min((c), 7) << 3)
365#define NDTR0_tRP(c) (min((c), 7) << 0)
366
367#define NDTR1_tR(c) (min((c), 65535) << 16)
368#define NDTR1_tWHR(c) (min((c), 15) << 4)
369#define NDTR1_tAR(c) (min((c), 15) << 0)
370
371/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800372#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800373
Jingoo Han17754ad2014-05-07 17:49:13 +0900374static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300375 {
376 .compatible = "marvell,pxa3xx-nand",
377 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
378 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300379 {
380 .compatible = "marvell,armada370-nand",
381 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
382 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300383 {}
384};
385MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
386
387static enum pxa3xx_nand_variant
388pxa3xx_nand_get_variant(struct platform_device *pdev)
389{
390 const struct of_device_id *of_id =
391 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
392 if (!of_id)
393 return PXA3XX_NAND_VARIANT_PXA;
394 return (enum pxa3xx_nand_variant)of_id->data;
395}
396
Lei Wend4568822011-07-14 20:44:32 -0700397static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200398 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800399{
Lei Wend4568822011-07-14 20:44:32 -0700400 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800401 unsigned long nand_clk = clk_get_rate(info->clk);
402 uint32_t ndtr0, ndtr1;
403
404 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
405 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
406 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
407 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
408 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
409 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
410
411 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
412 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
413 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
414
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300415 info->ndtr0cs0 = ndtr0;
416 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800417 nand_writel(info, NDTR0CS0, ndtr0);
418 nand_writel(info, NDTR1CS0, ndtr1);
419}
420
Antoine Ténart3f225b72015-10-21 10:29:02 +0200421static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
422 const struct nand_sdr_timings *t)
423{
424 struct pxa3xx_nand_info *info = host->info_data;
425 struct nand_chip *chip = &host->chip;
426 unsigned long nand_clk = clk_get_rate(info->clk);
427 uint32_t ndtr0, ndtr1;
428
429 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
430 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
431 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
432 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
433 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
434 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
435 u32 tR = chip->chip_delay * 1000;
436 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
437 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
438
439 /* fallback to a default value if tR = 0 */
440 if (!tR)
441 tR = 20000;
442
443 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
444 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
445 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
446 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
447 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
448 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
449
450 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
451 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
452 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
453
454 info->ndtr0cs0 = ndtr0;
455 info->ndtr1cs0 = ndtr1;
456 nand_writel(info, NDTR0CS0, ndtr0);
457 nand_writel(info, NDTR1CS0, ndtr1);
458}
459
460static int pxa3xx_nand_init_timings_compat(struct pxa3xx_nand_host *host,
461 unsigned int *flash_width,
462 unsigned int *dfc_width)
463{
464 struct nand_chip *chip = &host->chip;
465 struct pxa3xx_nand_info *info = host->info_data;
466 const struct pxa3xx_nand_flash *f = NULL;
467 int i, id, ntypes;
468
469 ntypes = ARRAY_SIZE(builtin_flash_types);
470
471 chip->cmdfunc(host->mtd, NAND_CMD_READID, 0x00, -1);
472
473 id = chip->read_byte(host->mtd);
474 id |= chip->read_byte(host->mtd) << 0x8;
475
476 for (i = 0; i < ntypes; i++) {
477 f = &builtin_flash_types[i];
478
479 if (f->chip_id == id)
480 break;
481 }
482
483 if (i == ntypes) {
484 dev_err(&info->pdev->dev, "Error: timings not found\n");
485 return -EINVAL;
486 }
487
488 pxa3xx_nand_set_timing(host, f->timing);
489
490 *flash_width = f->flash_width;
491 *dfc_width = f->dfc_width;
492
493 return 0;
494}
495
496static int pxa3xx_nand_init_timings_onfi(struct pxa3xx_nand_host *host,
497 int mode)
498{
499 const struct nand_sdr_timings *timings;
500
501 mode = fls(mode) - 1;
502 if (mode < 0)
503 mode = 0;
504
505 timings = onfi_async_timing_mode_to_sdr_timings(mode);
506 if (IS_ERR(timings))
507 return PTR_ERR(timings);
508
509 pxa3xx_nand_set_sdr_timing(host, timings);
510
511 return 0;
512}
513
514static int pxa3xx_nand_init(struct pxa3xx_nand_host *host)
515{
516 struct nand_chip *chip = &host->chip;
517 struct pxa3xx_nand_info *info = host->info_data;
518 unsigned int flash_width = 0, dfc_width = 0;
519 int mode, err;
520
521 mode = onfi_get_async_timing_mode(chip);
522 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
523 err = pxa3xx_nand_init_timings_compat(host, &flash_width,
524 &dfc_width);
525 if (err)
526 return err;
527
528 if (flash_width == 16) {
529 info->reg_ndcr |= NDCR_DWIDTH_M;
530 chip->options |= NAND_BUSWIDTH_16;
531 }
532
533 info->reg_ndcr |= (dfc_width == 16) ? NDCR_DWIDTH_C : 0;
534 } else {
535 err = pxa3xx_nand_init_timings_onfi(host, mode);
536 if (err)
537 return err;
538 }
539
540 return 0;
541}
542
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300543/*
544 * Set the data and OOB size, depending on the selected
545 * spare and ECC configuration.
546 * Only applicable to READ0, READOOB and PAGEPROG commands.
547 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300548static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
549 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800550{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300551 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800552
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300553 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300554 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800555 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800556
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300557 info->oob_size = info->spare_size;
558 if (!info->use_ecc)
559 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800560}
561
Lei Wenf8155a42011-02-28 10:32:11 +0800562/**
563 * NOTE: it is a must to set ND_RUN firstly, then write
564 * command buffer, otherwise, it does not work.
565 * We enable all the interrupt at the same time, and
566 * let pxa3xx_nand_irq to handle all logic.
567 */
568static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
569{
570 uint32_t ndcr;
571
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300572 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300573
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300574 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300575 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300576 if (info->ecc_bch)
577 nand_writel(info, NDECCCTRL, 0x1);
578 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300579 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300580 if (info->ecc_bch)
581 nand_writel(info, NDECCCTRL, 0x0);
582 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300583
584 if (info->use_dma)
585 ndcr |= NDCR_DMA_EN;
586 else
587 ndcr &= ~NDCR_DMA_EN;
588
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300589 if (info->use_spare)
590 ndcr |= NDCR_SPARE_EN;
591 else
592 ndcr &= ~NDCR_SPARE_EN;
593
Lei Wenf8155a42011-02-28 10:32:11 +0800594 ndcr |= NDCR_ND_RUN;
595
596 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800597 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200598 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800599 nand_writel(info, NDCR, ndcr);
600}
601
602static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
603{
604 uint32_t ndcr;
605 int timeout = NAND_STOP_DELAY;
606
607 /* wait RUN bit in NDCR become 0 */
608 ndcr = nand_readl(info, NDCR);
609 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
610 ndcr = nand_readl(info, NDCR);
611 udelay(1);
612 }
613
614 if (timeout <= 0) {
615 ndcr &= ~NDCR_ND_RUN;
616 nand_writel(info, NDCR, ndcr);
617 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200618 if (info->dma_chan)
619 dmaengine_terminate_all(info->dma_chan);
620
Lei Wenf8155a42011-02-28 10:32:11 +0800621 /* clear status bits */
622 nand_writel(info, NDSR, NDSR_MASK);
623}
624
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300625static void __maybe_unused
626enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800627{
628 uint32_t ndcr;
629
630 ndcr = nand_readl(info, NDCR);
631 nand_writel(info, NDCR, ndcr & ~int_mask);
632}
633
634static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
635{
636 uint32_t ndcr;
637
638 ndcr = nand_readl(info, NDCR);
639 nand_writel(info, NDCR, ndcr | int_mask);
640}
641
Maxime Ripard8dad0382015-02-18 11:32:07 +0100642static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
643{
644 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200645 u32 val;
646 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100647
648 /*
649 * According to the datasheet, when reading from NDDB
650 * with BCH enabled, after each 32 bytes reads, we
651 * have to make sure that the NDSR.RDDREQ bit is set.
652 *
653 * Drain the FIFO 8 32 bits reads at a time, and skip
654 * the polling on the last read.
655 */
656 while (len > 8) {
Antoine Ténartab53a572015-10-21 10:29:00 +0200657 ioread32_rep(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100658
Maxime Ripardafca11e2015-04-07 15:32:45 +0200659 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
660 val & NDSR_RDDREQ, 1000, 5000);
661 if (ret) {
662 dev_err(&info->pdev->dev,
663 "Timeout on RDDREQ while draining the FIFO\n");
664 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100665 }
666
667 data += 32;
668 len -= 8;
669 }
670 }
671
Antoine Ténartab53a572015-10-21 10:29:00 +0200672 ioread32_rep(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100673}
674
Lei Wenf8155a42011-02-28 10:32:11 +0800675static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800676{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300677 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300678
eric miaofe69af02008-02-14 15:48:23 +0800679 switch (info->state) {
680 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500681 writesl(info->mmio_base + NDDB,
682 info->data_buff + info->data_buff_pos,
683 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300684
Lei Wen9d8b1042010-08-17 14:09:30 +0800685 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500686 writesl(info->mmio_base + NDDB,
687 info->oob_buff + info->oob_buff_pos,
688 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800689 break;
690 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100691 drain_fifo(info,
692 info->data_buff + info->data_buff_pos,
693 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300694
Lei Wen9d8b1042010-08-17 14:09:30 +0800695 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100696 drain_fifo(info,
697 info->oob_buff + info->oob_buff_pos,
698 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800699 break;
700 default:
Lei Wenda675b42011-07-14 20:44:31 -0700701 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800702 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800703 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800704 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300705
706 /* Update buffer pointers for multi-page read/write */
707 info->data_buff_pos += do_bytes;
708 info->oob_buff_pos += info->oob_size;
709 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800710}
711
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200712static void pxa3xx_nand_data_dma_irq(void *data)
713{
714 struct pxa3xx_nand_info *info = data;
715 struct dma_tx_state state;
716 enum dma_status status;
717
718 status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state);
719 if (likely(status == DMA_COMPLETE)) {
720 info->state = STATE_DMA_DONE;
721 } else {
722 dev_err(&info->pdev->dev, "DMA error on data channel\n");
723 info->retcode = ERR_DMABUSERR;
724 }
725 dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
726
727 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
728 enable_int(info, NDCR_INT_MASK);
729}
730
Lei Wenf8155a42011-02-28 10:32:11 +0800731static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800732{
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200733 enum dma_transfer_direction direction;
734 struct dma_async_tx_descriptor *tx;
eric miaofe69af02008-02-14 15:48:23 +0800735
Lei Wenf8155a42011-02-28 10:32:11 +0800736 switch (info->state) {
737 case STATE_DMA_WRITING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200738 info->dma_dir = DMA_TO_DEVICE;
739 direction = DMA_MEM_TO_DEV;
Lei Wenf8155a42011-02-28 10:32:11 +0800740 break;
741 case STATE_DMA_READING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200742 info->dma_dir = DMA_FROM_DEVICE;
743 direction = DMA_DEV_TO_MEM;
Lei Wenf8155a42011-02-28 10:32:11 +0800744 break;
745 default:
Lei Wenda675b42011-07-14 20:44:31 -0700746 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800747 info->state);
748 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800749 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200750 info->sg.length = info->data_size +
751 (info->oob_size ? info->spare_size + info->ecc_size : 0);
752 dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
eric miaofe69af02008-02-14 15:48:23 +0800753
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200754 tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
755 DMA_PREP_INTERRUPT);
756 if (!tx) {
757 dev_err(&info->pdev->dev, "prep_slave_sg() failed\n");
758 return;
eric miaofe69af02008-02-14 15:48:23 +0800759 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200760 tx->callback = pxa3xx_nand_data_dma_irq;
761 tx->callback_param = info;
762 info->dma_cookie = dmaengine_submit(tx);
763 dma_async_issue_pending(info->dma_chan);
764 dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n",
765 __func__, direction, info->dma_cookie, info->sg.length);
eric miaofe69af02008-02-14 15:48:23 +0800766}
767
Robert Jarzmik24542252015-02-20 19:36:43 +0100768static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
769{
770 struct pxa3xx_nand_info *info = data;
771
772 handle_data_pio(info);
773
774 info->state = STATE_CMD_DONE;
775 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
776
777 return IRQ_HANDLED;
778}
779
eric miaofe69af02008-02-14 15:48:23 +0800780static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
781{
782 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300783 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700784 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100785 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700786
787 if (info->cs == 0) {
788 ready = NDSR_FLASH_RDY;
789 cmd_done = NDSR_CS0_CMDD;
790 } else {
791 ready = NDSR_RDY;
792 cmd_done = NDSR_CS1_CMDD;
793 }
eric miaofe69af02008-02-14 15:48:23 +0800794
795 status = nand_readl(info, NDSR);
796
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300797 if (status & NDSR_UNCORERR)
798 info->retcode = ERR_UNCORERR;
799 if (status & NDSR_CORERR) {
800 info->retcode = ERR_CORERR;
801 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
802 info->ecc_bch)
803 info->ecc_err_cnt = NDSR_ERR_CNT(status);
804 else
805 info->ecc_err_cnt = 1;
806
807 /*
808 * Each chunk composing a page is corrected independently,
809 * and we need to store maximum number of corrected bitflips
810 * to return it to the MTD layer in ecc.read_page().
811 */
812 info->max_bitflips = max_t(unsigned int,
813 info->max_bitflips,
814 info->ecc_err_cnt);
815 }
Lei Wenf8155a42011-02-28 10:32:11 +0800816 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
817 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800818 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800819 disable_int(info, NDCR_INT_MASK);
820 info->state = (status & NDSR_RDDREQ) ?
821 STATE_DMA_READING : STATE_DMA_WRITING;
822 start_data_dma(info);
823 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800824 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800825 info->state = (status & NDSR_RDDREQ) ?
826 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100827 ret = IRQ_WAKE_THREAD;
828 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800829 }
Lei Wenf8155a42011-02-28 10:32:11 +0800830 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700831 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800832 info->state = STATE_CMD_DONE;
833 is_completed = 1;
834 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700835 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800836 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300837 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800838 }
Lei Wenf8155a42011-02-28 10:32:11 +0800839
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200840 /*
841 * Clear all status bit before issuing the next command, which
842 * can and will alter the status bits and will deserve a new
843 * interrupt on its own. This lets the controller exit the IRQ
844 */
845 nand_writel(info, NDSR, status);
846
Lei Wenf8155a42011-02-28 10:32:11 +0800847 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800848 status &= ~NDSR_WRCMDREQ;
849 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300850
851 /*
852 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
853 * must be loaded by writing directly either 12 or 16
854 * bytes directly to NDCB0, four bytes at a time.
855 *
856 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
857 * but each NDCBx register can be read.
858 */
Lei Wenf8155a42011-02-28 10:32:11 +0800859 nand_writel(info, NDCB0, info->ndcb0);
860 nand_writel(info, NDCB0, info->ndcb1);
861 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300862
863 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
864 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
865 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800866 }
Lei Wenf8155a42011-02-28 10:32:11 +0800867
Lei Wenf8155a42011-02-28 10:32:11 +0800868 if (is_completed)
869 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300870 if (is_ready)
871 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800872NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100873 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800874}
875
eric miaofe69af02008-02-14 15:48:23 +0800876static inline int is_buf_blank(uint8_t *buf, size_t len)
877{
878 for (; len > 0; len--)
879 if (*buf++ != 0xff)
880 return 0;
881 return 1;
882}
883
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300884static void set_command_address(struct pxa3xx_nand_info *info,
885 unsigned int page_size, uint16_t column, int page_addr)
886{
887 /* small page addr setting */
888 if (page_size < PAGE_CHUNK_SIZE) {
889 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
890 | (column & 0xFF);
891
892 info->ndcb2 = 0;
893 } else {
894 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
895 | (column & 0xFFFF);
896
897 if (page_addr & 0xFF0000)
898 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
899 else
900 info->ndcb2 = 0;
901 }
902}
903
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300904static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800905{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300906 struct pxa3xx_nand_host *host = info->host[info->cs];
907 struct mtd_info *mtd = host->mtd;
908
Lei Wen4eb2da82011-02-28 10:32:13 +0800909 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800910 info->buf_start = 0;
911 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800912 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300913 info->data_buff_pos = 0;
914 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800915 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300916 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800917 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300918 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300919 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300920 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800921
922 switch (command) {
923 case NAND_CMD_READ0:
924 case NAND_CMD_PAGEPROG:
925 info->use_ecc = 1;
926 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300927 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800928 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300929 case NAND_CMD_PARAM:
930 info->use_spare = 0;
931 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800932 default:
933 info->ndcb1 = 0;
934 info->ndcb2 = 0;
935 break;
936 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300937
938 /*
939 * If we are about to issue a read command, or about to set
940 * the write address, then clean the data buffer.
941 */
942 if (command == NAND_CMD_READ0 ||
943 command == NAND_CMD_READOOB ||
944 command == NAND_CMD_SEQIN) {
945
946 info->buf_count = mtd->writesize + mtd->oobsize;
947 memset(info->data_buff, 0xFF, info->buf_count);
948 }
949
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300950}
951
952static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300953 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300954{
955 int addr_cycle, exec_cmd;
956 struct pxa3xx_nand_host *host;
957 struct mtd_info *mtd;
958
959 host = info->host[info->cs];
960 mtd = host->mtd;
961 addr_cycle = 0;
962 exec_cmd = 1;
963
964 if (info->cs != 0)
965 info->ndcb0 = NDCB0_CSEL;
966 else
967 info->ndcb0 = 0;
968
969 if (command == NAND_CMD_SEQIN)
970 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800971
Lei Wend4568822011-07-14 20:44:32 -0700972 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
973 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800974
975 switch (command) {
976 case NAND_CMD_READOOB:
977 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300978 info->buf_start = column;
979 info->ndcb0 |= NDCB0_CMD_TYPE(0)
980 | addr_cycle
981 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800982
Ezequiel Garciaec821352013-08-12 14:14:54 -0300983 if (command == NAND_CMD_READOOB)
984 info->buf_start += mtd->writesize;
985
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300986 /*
987 * Multiple page read needs an 'extended command type' field,
988 * which is either naked-read or last-read according to the
989 * state.
990 */
991 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300992 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300993 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
994 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
995 | NDCB0_LEN_OVRD
996 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
997 info->ndcb3 = info->chunk_size +
998 info->oob_size;
999 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001000
Ezequiel Garcia01d99472013-11-14 18:25:32 -03001001 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -03001002 break;
1003
Lei Wen4eb2da82011-02-28 10:32:13 +08001004 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +08001005
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -03001006 info->buf_start = column;
1007 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001008
1009 /*
1010 * Multiple page programming needs to execute the initial
1011 * SEQIN command that sets the page address.
1012 */
1013 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1014 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1015 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1016 | addr_cycle
1017 | command;
1018 /* No data transfer in this case */
1019 info->data_size = 0;
1020 exec_cmd = 1;
1021 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001022 break;
1023
1024 case NAND_CMD_PAGEPROG:
1025 if (is_buf_blank(info->data_buff,
1026 (mtd->writesize + mtd->oobsize))) {
1027 exec_cmd = 0;
1028 break;
1029 }
1030
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001031 /* Second command setting for large pages */
1032 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1033 /*
1034 * Multiple page write uses the 'extended command'
1035 * field. This can be used to issue a command dispatch
1036 * or a naked-write depending on the current stage.
1037 */
1038 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1039 | NDCB0_LEN_OVRD
1040 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
1041 info->ndcb3 = info->chunk_size +
1042 info->oob_size;
1043
1044 /*
1045 * This is the command dispatch that completes a chunked
1046 * page program operation.
1047 */
1048 if (info->data_size == 0) {
1049 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
1050 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1051 | command;
1052 info->ndcb1 = 0;
1053 info->ndcb2 = 0;
1054 info->ndcb3 = 0;
1055 }
1056 } else {
1057 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1058 | NDCB0_AUTO_RS
1059 | NDCB0_ST_ROW_EN
1060 | NDCB0_DBC
1061 | (NAND_CMD_PAGEPROG << 8)
1062 | NAND_CMD_SEQIN
1063 | addr_cycle;
1064 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001065 break;
1066
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001067 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -03001068 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001069 info->ndcb0 |= NDCB0_CMD_TYPE(0)
1070 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -03001071 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -03001072 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001073 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -03001074 info->ndcb3 = INIT_BUFFER_SIZE;
1075 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001076 break;
1077
Lei Wen4eb2da82011-02-28 10:32:13 +08001078 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001079 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +08001080 info->ndcb0 |= NDCB0_CMD_TYPE(3)
1081 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001082 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -03001083 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +08001084
1085 info->data_size = 8;
1086 break;
1087 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +08001088 info->buf_count = 1;
1089 info->ndcb0 |= NDCB0_CMD_TYPE(4)
1090 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001091 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001092
1093 info->data_size = 8;
1094 break;
1095
1096 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +08001097 info->ndcb0 |= NDCB0_CMD_TYPE(2)
1098 | NDCB0_AUTO_RS
1099 | NDCB0_ADDR_CYC(3)
1100 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -03001101 | (NAND_CMD_ERASE2 << 8)
1102 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +08001103 info->ndcb1 = page_addr;
1104 info->ndcb2 = 0;
1105
1106 break;
1107 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +08001108 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001109 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001110
1111 break;
1112
1113 case NAND_CMD_ERASE2:
1114 exec_cmd = 0;
1115 break;
1116
1117 default:
1118 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -07001119 dev_err(&info->pdev->dev, "non-supported command %x\n",
1120 command);
Lei Wen4eb2da82011-02-28 10:32:13 +08001121 break;
1122 }
1123
1124 return exec_cmd;
1125}
1126
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001127static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1128 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +08001129{
Lei Wend4568822011-07-14 20:44:32 -07001130 struct pxa3xx_nand_host *host = mtd->priv;
1131 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001132 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +08001133
Lei Wen4eb2da82011-02-28 10:32:13 +08001134 /*
1135 * if this is a x16 device ,then convert the input
1136 * "byte" address into a "word" address appropriate
1137 * for indexing a word-oriented device
1138 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001139 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +08001140 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +08001141
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001142 /*
1143 * There may be different NAND chip hooked to
1144 * different chip select, so check whether
1145 * chip select has been changed, if yes, reset the timing
1146 */
1147 if (info->cs != host->cs) {
1148 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001149 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1150 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001151 }
1152
Ezequiel Garciac39ff032013-11-14 18:25:33 -03001153 prepare_start_command(info, command);
1154
Lei Wend4568822011-07-14 20:44:32 -07001155 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001156 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1157
Lei Wenf8155a42011-02-28 10:32:11 +08001158 if (exec_cmd) {
1159 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001160 init_completion(&info->dev_ready);
1161 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001162 pxa3xx_nand_start(info);
1163
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001164 if (!wait_for_completion_timeout(&info->cmd_complete,
1165 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001166 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001167 /* Stop State Machine for next command cycle */
1168 pxa3xx_nand_stop(info);
1169 }
eric miaofe69af02008-02-14 15:48:23 +08001170 }
Lei Wend4568822011-07-14 20:44:32 -07001171 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001172}
1173
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001174static void nand_cmdfunc_extended(struct mtd_info *mtd,
1175 const unsigned command,
1176 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001177{
1178 struct pxa3xx_nand_host *host = mtd->priv;
1179 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001180 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001181
1182 /*
1183 * if this is a x16 device then convert the input
1184 * "byte" address into a "word" address appropriate
1185 * for indexing a word-oriented device
1186 */
1187 if (info->reg_ndcr & NDCR_DWIDTH_M)
1188 column /= 2;
1189
1190 /*
1191 * There may be different NAND chip hooked to
1192 * different chip select, so check whether
1193 * chip select has been changed, if yes, reset the timing
1194 */
1195 if (info->cs != host->cs) {
1196 info->cs = host->cs;
1197 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1198 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1199 }
1200
1201 /* Select the extended command for the first command */
1202 switch (command) {
1203 case NAND_CMD_READ0:
1204 case NAND_CMD_READOOB:
1205 ext_cmd_type = EXT_CMD_TYPE_MONO;
1206 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001207 case NAND_CMD_SEQIN:
1208 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1209 break;
1210 case NAND_CMD_PAGEPROG:
1211 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1212 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001213 default:
1214 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001215 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001216 }
1217
1218 prepare_start_command(info, command);
1219
1220 /*
1221 * Prepare the "is ready" completion before starting a command
1222 * transaction sequence. If the command is not executed the
1223 * completion will be completed, see below.
1224 *
1225 * We can do that inside the loop because the command variable
1226 * is invariant and thus so is the exec_cmd.
1227 */
1228 info->need_wait = 1;
1229 init_completion(&info->dev_ready);
1230 do {
1231 info->state = STATE_PREPARED;
1232 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1233 column, page_addr);
1234 if (!exec_cmd) {
1235 info->need_wait = 0;
1236 complete(&info->dev_ready);
1237 break;
1238 }
1239
1240 init_completion(&info->cmd_complete);
1241 pxa3xx_nand_start(info);
1242
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001243 if (!wait_for_completion_timeout(&info->cmd_complete,
1244 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001245 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1246 /* Stop State Machine for next command cycle */
1247 pxa3xx_nand_stop(info);
1248 break;
1249 }
1250
1251 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001252 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1253 break;
1254
1255 /*
1256 * After a splitted program command sequence has issued
1257 * the command dispatch, the command sequence is complete.
1258 */
1259 if (info->data_size == 0 &&
1260 command == NAND_CMD_PAGEPROG &&
1261 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001262 break;
1263
1264 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1265 /* Last read: issue a 'last naked read' */
1266 if (info->data_size == info->chunk_size)
1267 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1268 else
1269 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001270
1271 /*
1272 * If a splitted program command has no more data to transfer,
1273 * the command dispatch must be issued to complete.
1274 */
1275 } else if (command == NAND_CMD_PAGEPROG &&
1276 info->data_size == 0) {
1277 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001278 }
1279 } while (1);
1280
1281 info->state = STATE_IDLE;
1282}
1283
Josh Wufdbad98d2012-06-25 18:07:45 +08001284static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001285 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1286 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001287{
1288 chip->write_buf(mtd, buf, mtd->writesize);
1289 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001290
1291 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001292}
1293
1294static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001295 struct nand_chip *chip, uint8_t *buf, int oob_required,
1296 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001297{
Lei Wend4568822011-07-14 20:44:32 -07001298 struct pxa3xx_nand_host *host = mtd->priv;
1299 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001300
1301 chip->read_buf(mtd, buf, mtd->writesize);
1302 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1303
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001304 if (info->retcode == ERR_CORERR && info->use_ecc) {
1305 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1306
1307 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001308 /*
1309 * for blank page (all 0xff), HW will calculate its ECC as
1310 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001311 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001312 */
1313 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001314 info->retcode = ERR_NONE;
1315 else
Lei Wenf8155a42011-02-28 10:32:11 +08001316 mtd->ecc_stats.failed++;
1317 }
1318
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001319 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001320}
1321
eric miaofe69af02008-02-14 15:48:23 +08001322static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1323{
Lei Wend4568822011-07-14 20:44:32 -07001324 struct pxa3xx_nand_host *host = mtd->priv;
1325 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001326 char retval = 0xFF;
1327
1328 if (info->buf_start < info->buf_count)
1329 /* Has just send a new command? */
1330 retval = info->data_buff[info->buf_start++];
1331
1332 return retval;
1333}
1334
1335static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1336{
Lei Wend4568822011-07-14 20:44:32 -07001337 struct pxa3xx_nand_host *host = mtd->priv;
1338 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001339 u16 retval = 0xFFFF;
1340
1341 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1342 retval = *((u16 *)(info->data_buff+info->buf_start));
1343 info->buf_start += 2;
1344 }
1345 return retval;
1346}
1347
1348static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1349{
Lei Wend4568822011-07-14 20:44:32 -07001350 struct pxa3xx_nand_host *host = mtd->priv;
1351 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001352 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1353
1354 memcpy(buf, info->data_buff + info->buf_start, real_len);
1355 info->buf_start += real_len;
1356}
1357
1358static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1359 const uint8_t *buf, int len)
1360{
Lei Wend4568822011-07-14 20:44:32 -07001361 struct pxa3xx_nand_host *host = mtd->priv;
1362 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001363 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1364
1365 memcpy(info->data_buff + info->buf_start, buf, real_len);
1366 info->buf_start += real_len;
1367}
1368
eric miaofe69af02008-02-14 15:48:23 +08001369static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1370{
1371 return;
1372}
1373
1374static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1375{
Lei Wend4568822011-07-14 20:44:32 -07001376 struct pxa3xx_nand_host *host = mtd->priv;
1377 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001378
1379 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001380 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001381 if (!wait_for_completion_timeout(&info->dev_ready,
1382 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001383 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1384 return NAND_STATUS_FAIL;
1385 }
1386 }
eric miaofe69af02008-02-14 15:48:23 +08001387
1388 /* pxa3xx_nand_send_command has waited for command complete */
1389 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1390 if (info->retcode == ERR_NONE)
1391 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001392 else
1393 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001394 }
1395
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001396 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001397}
1398
eric miaofe69af02008-02-14 15:48:23 +08001399static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001400 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001401{
1402 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001403 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001404 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001405 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001406
Lei Wenda675b42011-07-14 20:44:31 -07001407 if (f->page_size != 2048 && f->page_size != 512) {
1408 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001409 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001410 }
eric miaofe69af02008-02-14 15:48:23 +08001411
Lei Wenda675b42011-07-14 20:44:31 -07001412 if (f->flash_width != 16 && f->flash_width != 8) {
1413 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001414 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001415 }
eric miaofe69af02008-02-14 15:48:23 +08001416
eric miaofe69af02008-02-14 15:48:23 +08001417 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001418 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001419
1420 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001421 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001422 else
Lei Wend4568822011-07-14 20:44:32 -07001423 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001424
1425 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001426 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001427 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1428 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1429 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1430 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1431
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001432 ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
eric miaofe69af02008-02-14 15:48:23 +08001433 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1434
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001435 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001436
Lei Wend4568822011-07-14 20:44:32 -07001437 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001438 return 0;
1439}
1440
Mike Rapoportf2710492009-02-17 13:54:47 +02001441static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1442{
1443 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001444
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001445 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001446 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Robert Jarzmike971aff2015-09-28 22:56:51 +02001447 info->reg_ndcr = ndcr &
1448 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001449 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1450 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001451 return 0;
1452}
1453
eric miaofe69af02008-02-14 15:48:23 +08001454static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1455{
1456 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001457 struct dma_slave_config config;
1458 dma_cap_mask_t mask;
1459 struct pxad_param param;
1460 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001461
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001462 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1463 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001464 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001465 if (use_dma == 0)
1466 return 0;
1467
1468 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1469 if (ret)
1470 return ret;
1471
1472 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1473 dma_cap_zero(mask);
1474 dma_cap_set(DMA_SLAVE, mask);
1475 param.prio = PXAD_PRIO_LOWEST;
1476 param.drcmr = info->drcmr_dat;
1477 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1478 &param, &pdev->dev,
1479 "data");
1480 if (!info->dma_chan) {
1481 dev_err(&pdev->dev, "unable to request data dma channel\n");
1482 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001483 }
1484
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001485 memset(&config, 0, sizeof(config));
1486 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1487 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1488 config.src_addr = info->mmio_phys + NDDB;
1489 config.dst_addr = info->mmio_phys + NDDB;
1490 config.src_maxburst = 32;
1491 config.dst_maxburst = 32;
1492 ret = dmaengine_slave_config(info->dma_chan, &config);
1493 if (ret < 0) {
1494 dev_err(&info->pdev->dev,
1495 "dma channel configuration failed: %d\n",
1496 ret);
1497 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001498 }
1499
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001500 /*
1501 * Now that DMA buffers are allocated we turn on
1502 * DMA proper for I/O operations.
1503 */
1504 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001505 return 0;
1506}
1507
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001508static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1509{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001510 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001511 dmaengine_terminate_all(info->dma_chan);
1512 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001513 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001514 kfree(info->data_buff);
1515}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001516
Lei Wen401e67e2011-02-28 10:32:14 +08001517static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001518{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001519 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001520 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001521 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001522
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001523 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001524 chip = mtd->priv;
1525
Lei Wen401e67e2011-02-28 10:32:14 +08001526 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001527 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1528 if (ret)
1529 return ret;
1530
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001531 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001532 ret = chip->waitfunc(mtd, chip);
1533 if (ret & NAND_STATUS_FAIL)
1534 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001535
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001536 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001537}
eric miaofe69af02008-02-14 15:48:23 +08001538
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001539static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1540 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001541 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001542{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001543 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001544 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001545 info->spare_size = 40;
1546 info->ecc_size = 24;
1547 ecc->mode = NAND_ECC_HW;
1548 ecc->size = 512;
1549 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001550
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001551 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001552 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001553 info->spare_size = 8;
1554 info->ecc_size = 8;
1555 ecc->mode = NAND_ECC_HW;
1556 ecc->size = 512;
1557 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001558
Brian Norris6033a942013-11-14 14:41:32 -08001559 /*
1560 * Required ECC: 4-bit correction per 512 bytes
1561 * Select: 16-bit correction per 2048 bytes
1562 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001563 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1564 info->ecc_bch = 1;
1565 info->chunk_size = 2048;
1566 info->spare_size = 32;
1567 info->ecc_size = 32;
1568 ecc->mode = NAND_ECC_HW;
1569 ecc->size = info->chunk_size;
1570 ecc->layout = &ecc_layout_2KB_bch4bit;
1571 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001572
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001573 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001574 info->ecc_bch = 1;
1575 info->chunk_size = 2048;
1576 info->spare_size = 32;
1577 info->ecc_size = 32;
1578 ecc->mode = NAND_ECC_HW;
1579 ecc->size = info->chunk_size;
1580 ecc->layout = &ecc_layout_4KB_bch4bit;
1581 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001582
Brian Norris6033a942013-11-14 14:41:32 -08001583 /*
1584 * Required ECC: 8-bit correction per 512 bytes
1585 * Select: 16-bit correction per 1024 bytes
1586 */
1587 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001588 info->ecc_bch = 1;
1589 info->chunk_size = 1024;
1590 info->spare_size = 0;
1591 info->ecc_size = 32;
1592 ecc->mode = NAND_ECC_HW;
1593 ecc->size = info->chunk_size;
1594 ecc->layout = &ecc_layout_4KB_bch8bit;
1595 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001596 } else {
1597 dev_err(&info->pdev->dev,
1598 "ECC strength %d at page size %d is not supported\n",
1599 strength, page_size);
1600 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001601 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001602
1603 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1604 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001605 return 0;
1606}
1607
Lei Wen401e67e2011-02-28 10:32:14 +08001608static int pxa3xx_nand_scan(struct mtd_info *mtd)
1609{
Lei Wend4568822011-07-14 20:44:32 -07001610 struct pxa3xx_nand_host *host = mtd->priv;
1611 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001612 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001613 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001614 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001615 const struct pxa3xx_nand_flash *f = NULL;
1616 struct nand_chip *chip = mtd->priv;
1617 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001618 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001619 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001620 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001621
1622 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001623 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001624
Antoine Ténartbc3e00f2015-08-18 10:59:10 +02001625 /* Set a default chunk size */
1626 info->chunk_size = 512;
1627
Lei Wen401e67e2011-02-28 10:32:14 +08001628 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001629 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001630 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1631 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001632
Lei Wend4568822011-07-14 20:44:32 -07001633 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001634 }
1635
1636 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1637 id = *((uint16_t *)(info->data_buff));
1638 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001639 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001640 else {
Lei Wenda675b42011-07-14 20:44:31 -07001641 dev_warn(&info->pdev->dev,
1642 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001643
1644 return -EINVAL;
1645 }
1646
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001647 num = ARRAY_SIZE(builtin_flash_types) - 1;
Lei Wen401e67e2011-02-28 10:32:14 +08001648 for (i = 0; i < num; i++) {
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001649 f = &builtin_flash_types[i + 1];
Lei Wen401e67e2011-02-28 10:32:14 +08001650
1651 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001652 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001653 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001654 }
1655
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001656 if (i >= (ARRAY_SIZE(builtin_flash_types) - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001657 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001658
1659 return -EINVAL;
1660 }
1661
Lei Wend4568822011-07-14 20:44:32 -07001662 ret = pxa3xx_nand_config_flash(info, f);
1663 if (ret) {
1664 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1665 return ret;
1666 }
1667
Antoine Ténart7c2f7172015-02-12 15:53:27 +01001668 memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids));
1669
Lei Wen4332c112011-03-03 11:27:01 +08001670 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001671 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001672 pxa3xx_flash_ids[0].pagesize = f->page_size;
1673 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1674 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1675 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1676 if (f->flash_width == 16)
1677 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001678 pxa3xx_flash_ids[1].name = NULL;
1679 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001680KEEP_CONFIG:
Robert Jarzmike971aff2015-09-28 22:56:51 +02001681 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001682 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001683 chip->options |= NAND_BUSWIDTH_16;
1684
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001685 /* Device detection must be done with ECC disabled */
1686 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1687 nand_writel(info, NDECCCTRL, 0x0);
1688
Lei Wen0fab0282011-06-07 03:01:06 -07001689 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001690 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001691
1692 if (pdata->flash_bbt) {
1693 /*
1694 * We'll use a bad block table stored in-flash and don't
1695 * allow writing the bad block marker to the flash.
1696 */
1697 chip->bbt_options |= NAND_BBT_USE_FLASH |
1698 NAND_BBT_NO_OOB_BBM;
1699 chip->bbt_td = &bbt_main_descr;
1700 chip->bbt_md = &bbt_mirror_descr;
1701 }
1702
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001703 /*
1704 * If the page size is bigger than the FIFO size, let's check
1705 * we are given the right variant and then switch to the extended
1706 * (aka splitted) command handling,
1707 */
1708 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1709 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1710 chip->cmdfunc = nand_cmdfunc_extended;
1711 } else {
1712 dev_err(&info->pdev->dev,
1713 "unsupported page size on this variant\n");
1714 return -ENODEV;
1715 }
1716 }
1717
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001718 if (pdata->ecc_strength && pdata->ecc_step_size) {
1719 ecc_strength = pdata->ecc_strength;
1720 ecc_step = pdata->ecc_step_size;
1721 } else {
1722 ecc_strength = chip->ecc_strength_ds;
1723 ecc_step = chip->ecc_step_ds;
1724 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001725
1726 /* Set default ECC strength requirements on non-ONFI devices */
1727 if (ecc_strength < 1 && ecc_step < 1) {
1728 ecc_strength = 1;
1729 ecc_step = 512;
1730 }
1731
1732 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1733 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001734 if (ret)
1735 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001736
Lei Wen4332c112011-03-03 11:27:01 +08001737 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001738 if (mtd->writesize >= 2048)
1739 host->col_addr_cycles = 2;
1740 else
1741 host->col_addr_cycles = 1;
1742
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001743 /* release the initial buffer */
1744 kfree(info->data_buff);
1745
1746 /* allocate the real data + oob buffer */
1747 info->buf_size = mtd->writesize + mtd->oobsize;
1748 ret = pxa3xx_nand_init_buff(info);
1749 if (ret)
1750 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001751 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001752
Lei Wen4332c112011-03-03 11:27:01 +08001753 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001754 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001755 else
Lei Wend4568822011-07-14 20:44:32 -07001756 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001757 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001758}
1759
Lei Wend4568822011-07-14 20:44:32 -07001760static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001761{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001762 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001763 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001764 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001765 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001766 struct mtd_info *mtd;
1767 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001768 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001769
Jingoo Han453810b2013-07-30 17:18:33 +09001770 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001771 if (pdata->num_cs <= 0)
1772 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001773 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1774 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1775 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001776 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001777
eric miaofe69af02008-02-14 15:48:23 +08001778 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001779 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001780 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001781 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001782 chip = (struct nand_chip *)(&mtd[1]);
1783 host = (struct pxa3xx_nand_host *)chip;
1784 info->host[cs] = host;
1785 host->mtd = mtd;
1786 host->cs = cs;
1787 host->info_data = info;
1788 mtd->priv = host;
Frans Klaver550dab52015-06-10 22:39:01 +02001789 mtd->dev.parent = &pdev->dev;
eric miaofe69af02008-02-14 15:48:23 +08001790
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001791 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1792 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1793 chip->controller = &info->controller;
1794 chip->waitfunc = pxa3xx_nand_waitfunc;
1795 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001796 chip->read_word = pxa3xx_nand_read_word;
1797 chip->read_byte = pxa3xx_nand_read_byte;
1798 chip->read_buf = pxa3xx_nand_read_buf;
1799 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001800 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001801 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001802 }
Lei Wen401e67e2011-02-28 10:32:14 +08001803
1804 spin_lock_init(&chip->controller->lock);
1805 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001806 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001807 if (IS_ERR(info->clk)) {
1808 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001809 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001810 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001811 ret = clk_prepare_enable(info->clk);
1812 if (ret < 0)
1813 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001814
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001815 if (use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001816 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1817 if (r == NULL) {
1818 dev_err(&pdev->dev,
1819 "no resource defined for data DMA\n");
1820 ret = -ENXIO;
1821 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001822 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001823 info->drcmr_dat = r->start;
1824
1825 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1826 if (r == NULL) {
1827 dev_err(&pdev->dev,
1828 "no resource defined for cmd DMA\n");
1829 ret = -ENXIO;
1830 goto fail_disable_clk;
1831 }
1832 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001833 }
eric miaofe69af02008-02-14 15:48:23 +08001834
1835 irq = platform_get_irq(pdev, 0);
1836 if (irq < 0) {
1837 dev_err(&pdev->dev, "no IRQ resource defined\n");
1838 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001839 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001840 }
1841
1842 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001843 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1844 if (IS_ERR(info->mmio_base)) {
1845 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001846 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001847 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001848 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001849
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001850 /* Allocate a buffer to allow flash detection */
1851 info->buf_size = INIT_BUFFER_SIZE;
1852 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1853 if (info->data_buff == NULL) {
1854 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001855 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001856 }
eric miaofe69af02008-02-14 15:48:23 +08001857
Haojian Zhuang346e1252009-09-10 14:27:23 +08001858 /* initialize all interrupts to be disabled */
1859 disable_int(info, NDSR_MASK);
1860
Robert Jarzmik24542252015-02-20 19:36:43 +01001861 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1862 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1863 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001864 if (ret < 0) {
1865 dev_err(&pdev->dev, "failed to request IRQ\n");
1866 goto fail_free_buf;
1867 }
1868
Lei Wene353a202011-03-03 11:08:30 +08001869 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001870
Lei Wend4568822011-07-14 20:44:32 -07001871 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001872
eric miaofe69af02008-02-14 15:48:23 +08001873fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001874 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001875 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001876fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001877 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001878 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001879}
1880
1881static int pxa3xx_nand_remove(struct platform_device *pdev)
1882{
Lei Wene353a202011-03-03 11:08:30 +08001883 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001884 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001885 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001886
Lei Wend4568822011-07-14 20:44:32 -07001887 if (!info)
1888 return 0;
1889
Jingoo Han453810b2013-07-30 17:18:33 +09001890 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001891
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001892 irq = platform_get_irq(pdev, 0);
1893 if (irq >= 0)
1894 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001895 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001896
Robert Jarzmike971aff2015-09-28 22:56:51 +02001897 /*
1898 * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
1899 * In order to prevent a lockup of the system bus, the DFI bus
1900 * arbitration is granted to SMC upon driver removal. This is done by
1901 * setting the x_ARB_CNTL bit, which also prevents the NAND to have
1902 * access to the bus anymore.
1903 */
1904 nand_writel(info, NDCR,
1905 (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
1906 NFCV1_NDCR_ARB_CNTL);
Ezequiel Garciafb320612013-04-17 13:38:12 -03001907 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001908
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001909 for (cs = 0; cs < pdata->num_cs; cs++)
1910 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001911 return 0;
1912}
1913
Daniel Mack1e7ba632012-07-22 19:51:02 +02001914static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1915{
1916 struct pxa3xx_nand_platform_data *pdata;
1917 struct device_node *np = pdev->dev.of_node;
1918 const struct of_device_id *of_id =
1919 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1920
1921 if (!of_id)
1922 return 0;
1923
1924 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1925 if (!pdata)
1926 return -ENOMEM;
1927
1928 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1929 pdata->enable_arbiter = 1;
1930 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1931 pdata->keep_config = 1;
1932 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001933 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001934
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001935 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1936 if (pdata->ecc_strength < 0)
1937 pdata->ecc_strength = 0;
1938
1939 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1940 if (pdata->ecc_step_size < 0)
1941 pdata->ecc_step_size = 0;
1942
Daniel Mack1e7ba632012-07-22 19:51:02 +02001943 pdev->dev.platform_data = pdata;
1944
1945 return 0;
1946}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001947
Lei Wene353a202011-03-03 11:08:30 +08001948static int pxa3xx_nand_probe(struct platform_device *pdev)
1949{
1950 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001951 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001952 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001953 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001954
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001955 dma_available = IS_ENABLED(CONFIG_ARM) &&
1956 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1957 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001958 use_dma = 0;
1959 dev_warn(&pdev->dev,
1960 "This platform can't do DMA on this device\n");
1961 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001962
Daniel Mack1e7ba632012-07-22 19:51:02 +02001963 ret = pxa3xx_nand_probe_dt(pdev);
1964 if (ret)
1965 return ret;
1966
Jingoo Han453810b2013-07-30 17:18:33 +09001967 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001968 if (!pdata) {
1969 dev_err(&pdev->dev, "no platform data defined\n");
1970 return -ENODEV;
1971 }
1972
Lei Wend4568822011-07-14 20:44:32 -07001973 ret = alloc_nand_resource(pdev);
1974 if (ret) {
1975 dev_err(&pdev->dev, "alloc nand resource failed\n");
1976 return ret;
1977 }
Lei Wene353a202011-03-03 11:08:30 +08001978
Lei Wend4568822011-07-14 20:44:32 -07001979 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001980 probe_success = 0;
1981 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001982 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001983
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001984 /*
1985 * The mtd name matches the one used in 'mtdparts' kernel
1986 * parameter. This name cannot be changed or otherwise
1987 * user's mtd partitions configuration would get broken.
1988 */
1989 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001990 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001991 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001992 if (ret) {
1993 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1994 cs);
1995 continue;
1996 }
1997
Daniel Mack1e7ba632012-07-22 19:51:02 +02001998 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001999 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02002000 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02002001 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07002002 if (!ret)
2003 probe_success = 1;
2004 }
2005
2006 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08002007 pxa3xx_nand_remove(pdev);
2008 return -ENODEV;
2009 }
2010
Lei Wenf3c8cfc2011-07-14 20:44:33 -07002011 return 0;
Lei Wene353a202011-03-03 11:08:30 +08002012}
2013
eric miaofe69af02008-02-14 15:48:23 +08002014#ifdef CONFIG_PM
Brian Norrisd3e94f32015-10-12 14:07:41 -07002015static int pxa3xx_nand_suspend(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08002016{
Brian Norrisd3e94f32015-10-12 14:07:41 -07002017 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
eric miaofe69af02008-02-14 15:48:23 +08002018
Lei Wenf8155a42011-02-28 10:32:11 +08002019 if (info->state) {
Brian Norrisd3e94f32015-10-12 14:07:41 -07002020 dev_err(dev, "driver busy, state = %d\n", info->state);
eric miaofe69af02008-02-14 15:48:23 +08002021 return -EAGAIN;
2022 }
2023
2024 return 0;
2025}
2026
Brian Norrisd3e94f32015-10-12 14:07:41 -07002027static int pxa3xx_nand_resume(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08002028{
Brian Norrisd3e94f32015-10-12 14:07:41 -07002029 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
Lei Wen051fc412011-07-14 20:44:30 -07002030
2031 /* We don't want to handle interrupt without calling mtd routine */
2032 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08002033
Lei Wenf3c8cfc2011-07-14 20:44:33 -07002034 /*
2035 * Directly set the chip select to a invalid value,
2036 * then the driver would reset the timing according
2037 * to current chip select at the beginning of cmdfunc
2038 */
2039 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08002040
Lei Wen051fc412011-07-14 20:44:30 -07002041 /*
2042 * As the spec says, the NDSR would be updated to 0x1800 when
2043 * doing the nand_clk disable/enable.
2044 * To prevent it damaging state machine of the driver, clear
2045 * all status before resume
2046 */
2047 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07002048
Lei Wen18c81b12010-08-17 17:25:57 +08002049 return 0;
eric miaofe69af02008-02-14 15:48:23 +08002050}
2051#else
2052#define pxa3xx_nand_suspend NULL
2053#define pxa3xx_nand_resume NULL
2054#endif
2055
Brian Norrisd3e94f32015-10-12 14:07:41 -07002056static const struct dev_pm_ops pxa3xx_nand_pm_ops = {
2057 .suspend = pxa3xx_nand_suspend,
2058 .resume = pxa3xx_nand_resume,
2059};
2060
eric miaofe69af02008-02-14 15:48:23 +08002061static struct platform_driver pxa3xx_nand_driver = {
2062 .driver = {
2063 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05302064 .of_match_table = pxa3xx_nand_dt_ids,
Brian Norrisd3e94f32015-10-12 14:07:41 -07002065 .pm = &pxa3xx_nand_pm_ops,
eric miaofe69af02008-02-14 15:48:23 +08002066 },
2067 .probe = pxa3xx_nand_probe,
2068 .remove = pxa3xx_nand_remove,
eric miaofe69af02008-02-14 15:48:23 +08002069};
2070
Axel Linf99640d2011-11-27 20:45:03 +08002071module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08002072
2073MODULE_LICENSE("GPL");
2074MODULE_DESCRIPTION("PXA3xx NAND controller driver");