blob: 0024a9c3563158774f3600e410d91107958c5043 [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
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300421/*
422 * Set the data and OOB size, depending on the selected
423 * spare and ECC configuration.
424 * Only applicable to READ0, READOOB and PAGEPROG commands.
425 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300426static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
427 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800428{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300429 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800430
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300431 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300432 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800433 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800434
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300435 info->oob_size = info->spare_size;
436 if (!info->use_ecc)
437 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800438}
439
Lei Wenf8155a42011-02-28 10:32:11 +0800440/**
441 * NOTE: it is a must to set ND_RUN firstly, then write
442 * command buffer, otherwise, it does not work.
443 * We enable all the interrupt at the same time, and
444 * let pxa3xx_nand_irq to handle all logic.
445 */
446static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
447{
448 uint32_t ndcr;
449
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300450 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300451
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300452 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300453 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300454 if (info->ecc_bch)
455 nand_writel(info, NDECCCTRL, 0x1);
456 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300457 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300458 if (info->ecc_bch)
459 nand_writel(info, NDECCCTRL, 0x0);
460 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300461
462 if (info->use_dma)
463 ndcr |= NDCR_DMA_EN;
464 else
465 ndcr &= ~NDCR_DMA_EN;
466
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300467 if (info->use_spare)
468 ndcr |= NDCR_SPARE_EN;
469 else
470 ndcr &= ~NDCR_SPARE_EN;
471
Lei Wenf8155a42011-02-28 10:32:11 +0800472 ndcr |= NDCR_ND_RUN;
473
474 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800475 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200476 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800477 nand_writel(info, NDCR, ndcr);
478}
479
480static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
481{
482 uint32_t ndcr;
483 int timeout = NAND_STOP_DELAY;
484
485 /* wait RUN bit in NDCR become 0 */
486 ndcr = nand_readl(info, NDCR);
487 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
488 ndcr = nand_readl(info, NDCR);
489 udelay(1);
490 }
491
492 if (timeout <= 0) {
493 ndcr &= ~NDCR_ND_RUN;
494 nand_writel(info, NDCR, ndcr);
495 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200496 if (info->dma_chan)
497 dmaengine_terminate_all(info->dma_chan);
498
Lei Wenf8155a42011-02-28 10:32:11 +0800499 /* clear status bits */
500 nand_writel(info, NDSR, NDSR_MASK);
501}
502
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300503static void __maybe_unused
504enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800505{
506 uint32_t ndcr;
507
508 ndcr = nand_readl(info, NDCR);
509 nand_writel(info, NDCR, ndcr & ~int_mask);
510}
511
512static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
513{
514 uint32_t ndcr;
515
516 ndcr = nand_readl(info, NDCR);
517 nand_writel(info, NDCR, ndcr | int_mask);
518}
519
Maxime Ripard8dad0382015-02-18 11:32:07 +0100520static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
521{
522 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200523 u32 val;
524 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100525
526 /*
527 * According to the datasheet, when reading from NDDB
528 * with BCH enabled, after each 32 bytes reads, we
529 * have to make sure that the NDSR.RDDREQ bit is set.
530 *
531 * Drain the FIFO 8 32 bits reads at a time, and skip
532 * the polling on the last read.
533 */
534 while (len > 8) {
Rob Herringce914e62015-04-30 15:17:47 -0500535 readsl(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100536
Maxime Ripardafca11e2015-04-07 15:32:45 +0200537 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
538 val & NDSR_RDDREQ, 1000, 5000);
539 if (ret) {
540 dev_err(&info->pdev->dev,
541 "Timeout on RDDREQ while draining the FIFO\n");
542 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100543 }
544
545 data += 32;
546 len -= 8;
547 }
548 }
549
Rob Herringce914e62015-04-30 15:17:47 -0500550 readsl(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100551}
552
Lei Wenf8155a42011-02-28 10:32:11 +0800553static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800554{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300555 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300556
eric miaofe69af02008-02-14 15:48:23 +0800557 switch (info->state) {
558 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500559 writesl(info->mmio_base + NDDB,
560 info->data_buff + info->data_buff_pos,
561 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300562
Lei Wen9d8b1042010-08-17 14:09:30 +0800563 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500564 writesl(info->mmio_base + NDDB,
565 info->oob_buff + info->oob_buff_pos,
566 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800567 break;
568 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100569 drain_fifo(info,
570 info->data_buff + info->data_buff_pos,
571 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300572
Lei Wen9d8b1042010-08-17 14:09:30 +0800573 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100574 drain_fifo(info,
575 info->oob_buff + info->oob_buff_pos,
576 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800577 break;
578 default:
Lei Wenda675b42011-07-14 20:44:31 -0700579 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800580 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800581 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800582 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300583
584 /* Update buffer pointers for multi-page read/write */
585 info->data_buff_pos += do_bytes;
586 info->oob_buff_pos += info->oob_size;
587 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800588}
589
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200590static void pxa3xx_nand_data_dma_irq(void *data)
591{
592 struct pxa3xx_nand_info *info = data;
593 struct dma_tx_state state;
594 enum dma_status status;
595
596 status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state);
597 if (likely(status == DMA_COMPLETE)) {
598 info->state = STATE_DMA_DONE;
599 } else {
600 dev_err(&info->pdev->dev, "DMA error on data channel\n");
601 info->retcode = ERR_DMABUSERR;
602 }
603 dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
604
605 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
606 enable_int(info, NDCR_INT_MASK);
607}
608
Lei Wenf8155a42011-02-28 10:32:11 +0800609static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800610{
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200611 enum dma_transfer_direction direction;
612 struct dma_async_tx_descriptor *tx;
eric miaofe69af02008-02-14 15:48:23 +0800613
Lei Wenf8155a42011-02-28 10:32:11 +0800614 switch (info->state) {
615 case STATE_DMA_WRITING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200616 info->dma_dir = DMA_TO_DEVICE;
617 direction = DMA_MEM_TO_DEV;
Lei Wenf8155a42011-02-28 10:32:11 +0800618 break;
619 case STATE_DMA_READING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200620 info->dma_dir = DMA_FROM_DEVICE;
621 direction = DMA_DEV_TO_MEM;
Lei Wenf8155a42011-02-28 10:32:11 +0800622 break;
623 default:
Lei Wenda675b42011-07-14 20:44:31 -0700624 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800625 info->state);
626 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800627 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200628 info->sg.length = info->data_size +
629 (info->oob_size ? info->spare_size + info->ecc_size : 0);
630 dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
eric miaofe69af02008-02-14 15:48:23 +0800631
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200632 tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
633 DMA_PREP_INTERRUPT);
634 if (!tx) {
635 dev_err(&info->pdev->dev, "prep_slave_sg() failed\n");
636 return;
eric miaofe69af02008-02-14 15:48:23 +0800637 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200638 tx->callback = pxa3xx_nand_data_dma_irq;
639 tx->callback_param = info;
640 info->dma_cookie = dmaengine_submit(tx);
641 dma_async_issue_pending(info->dma_chan);
642 dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n",
643 __func__, direction, info->dma_cookie, info->sg.length);
eric miaofe69af02008-02-14 15:48:23 +0800644}
645
Robert Jarzmik24542252015-02-20 19:36:43 +0100646static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
647{
648 struct pxa3xx_nand_info *info = data;
649
650 handle_data_pio(info);
651
652 info->state = STATE_CMD_DONE;
653 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
654
655 return IRQ_HANDLED;
656}
657
eric miaofe69af02008-02-14 15:48:23 +0800658static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
659{
660 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300661 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700662 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100663 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700664
665 if (info->cs == 0) {
666 ready = NDSR_FLASH_RDY;
667 cmd_done = NDSR_CS0_CMDD;
668 } else {
669 ready = NDSR_RDY;
670 cmd_done = NDSR_CS1_CMDD;
671 }
eric miaofe69af02008-02-14 15:48:23 +0800672
673 status = nand_readl(info, NDSR);
674
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300675 if (status & NDSR_UNCORERR)
676 info->retcode = ERR_UNCORERR;
677 if (status & NDSR_CORERR) {
678 info->retcode = ERR_CORERR;
679 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
680 info->ecc_bch)
681 info->ecc_err_cnt = NDSR_ERR_CNT(status);
682 else
683 info->ecc_err_cnt = 1;
684
685 /*
686 * Each chunk composing a page is corrected independently,
687 * and we need to store maximum number of corrected bitflips
688 * to return it to the MTD layer in ecc.read_page().
689 */
690 info->max_bitflips = max_t(unsigned int,
691 info->max_bitflips,
692 info->ecc_err_cnt);
693 }
Lei Wenf8155a42011-02-28 10:32:11 +0800694 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
695 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800696 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800697 disable_int(info, NDCR_INT_MASK);
698 info->state = (status & NDSR_RDDREQ) ?
699 STATE_DMA_READING : STATE_DMA_WRITING;
700 start_data_dma(info);
701 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800702 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800703 info->state = (status & NDSR_RDDREQ) ?
704 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100705 ret = IRQ_WAKE_THREAD;
706 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800707 }
Lei Wenf8155a42011-02-28 10:32:11 +0800708 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700709 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800710 info->state = STATE_CMD_DONE;
711 is_completed = 1;
712 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700713 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800714 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300715 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800716 }
Lei Wenf8155a42011-02-28 10:32:11 +0800717
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200718 /*
719 * Clear all status bit before issuing the next command, which
720 * can and will alter the status bits and will deserve a new
721 * interrupt on its own. This lets the controller exit the IRQ
722 */
723 nand_writel(info, NDSR, status);
724
Lei Wenf8155a42011-02-28 10:32:11 +0800725 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800726 status &= ~NDSR_WRCMDREQ;
727 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300728
729 /*
730 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
731 * must be loaded by writing directly either 12 or 16
732 * bytes directly to NDCB0, four bytes at a time.
733 *
734 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
735 * but each NDCBx register can be read.
736 */
Lei Wenf8155a42011-02-28 10:32:11 +0800737 nand_writel(info, NDCB0, info->ndcb0);
738 nand_writel(info, NDCB0, info->ndcb1);
739 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300740
741 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
742 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
743 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800744 }
Lei Wenf8155a42011-02-28 10:32:11 +0800745
Lei Wenf8155a42011-02-28 10:32:11 +0800746 if (is_completed)
747 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300748 if (is_ready)
749 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800750NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100751 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800752}
753
eric miaofe69af02008-02-14 15:48:23 +0800754static inline int is_buf_blank(uint8_t *buf, size_t len)
755{
756 for (; len > 0; len--)
757 if (*buf++ != 0xff)
758 return 0;
759 return 1;
760}
761
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300762static void set_command_address(struct pxa3xx_nand_info *info,
763 unsigned int page_size, uint16_t column, int page_addr)
764{
765 /* small page addr setting */
766 if (page_size < PAGE_CHUNK_SIZE) {
767 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
768 | (column & 0xFF);
769
770 info->ndcb2 = 0;
771 } else {
772 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
773 | (column & 0xFFFF);
774
775 if (page_addr & 0xFF0000)
776 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
777 else
778 info->ndcb2 = 0;
779 }
780}
781
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300782static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800783{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300784 struct pxa3xx_nand_host *host = info->host[info->cs];
785 struct mtd_info *mtd = host->mtd;
786
Lei Wen4eb2da82011-02-28 10:32:13 +0800787 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800788 info->buf_start = 0;
789 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800790 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300791 info->data_buff_pos = 0;
792 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800793 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300794 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800795 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300796 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300797 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300798 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800799
800 switch (command) {
801 case NAND_CMD_READ0:
802 case NAND_CMD_PAGEPROG:
803 info->use_ecc = 1;
804 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300805 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800806 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300807 case NAND_CMD_PARAM:
808 info->use_spare = 0;
809 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800810 default:
811 info->ndcb1 = 0;
812 info->ndcb2 = 0;
813 break;
814 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300815
816 /*
817 * If we are about to issue a read command, or about to set
818 * the write address, then clean the data buffer.
819 */
820 if (command == NAND_CMD_READ0 ||
821 command == NAND_CMD_READOOB ||
822 command == NAND_CMD_SEQIN) {
823
824 info->buf_count = mtd->writesize + mtd->oobsize;
825 memset(info->data_buff, 0xFF, info->buf_count);
826 }
827
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300828}
829
830static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300831 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300832{
833 int addr_cycle, exec_cmd;
834 struct pxa3xx_nand_host *host;
835 struct mtd_info *mtd;
836
837 host = info->host[info->cs];
838 mtd = host->mtd;
839 addr_cycle = 0;
840 exec_cmd = 1;
841
842 if (info->cs != 0)
843 info->ndcb0 = NDCB0_CSEL;
844 else
845 info->ndcb0 = 0;
846
847 if (command == NAND_CMD_SEQIN)
848 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800849
Lei Wend4568822011-07-14 20:44:32 -0700850 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
851 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800852
853 switch (command) {
854 case NAND_CMD_READOOB:
855 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300856 info->buf_start = column;
857 info->ndcb0 |= NDCB0_CMD_TYPE(0)
858 | addr_cycle
859 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800860
Ezequiel Garciaec821352013-08-12 14:14:54 -0300861 if (command == NAND_CMD_READOOB)
862 info->buf_start += mtd->writesize;
863
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300864 /*
865 * Multiple page read needs an 'extended command type' field,
866 * which is either naked-read or last-read according to the
867 * state.
868 */
869 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300870 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300871 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
872 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
873 | NDCB0_LEN_OVRD
874 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
875 info->ndcb3 = info->chunk_size +
876 info->oob_size;
877 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800878
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300879 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300880 break;
881
Lei Wen4eb2da82011-02-28 10:32:13 +0800882 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800883
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300884 info->buf_start = column;
885 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300886
887 /*
888 * Multiple page programming needs to execute the initial
889 * SEQIN command that sets the page address.
890 */
891 if (mtd->writesize > PAGE_CHUNK_SIZE) {
892 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
893 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
894 | addr_cycle
895 | command;
896 /* No data transfer in this case */
897 info->data_size = 0;
898 exec_cmd = 1;
899 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800900 break;
901
902 case NAND_CMD_PAGEPROG:
903 if (is_buf_blank(info->data_buff,
904 (mtd->writesize + mtd->oobsize))) {
905 exec_cmd = 0;
906 break;
907 }
908
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300909 /* Second command setting for large pages */
910 if (mtd->writesize > PAGE_CHUNK_SIZE) {
911 /*
912 * Multiple page write uses the 'extended command'
913 * field. This can be used to issue a command dispatch
914 * or a naked-write depending on the current stage.
915 */
916 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
917 | NDCB0_LEN_OVRD
918 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
919 info->ndcb3 = info->chunk_size +
920 info->oob_size;
921
922 /*
923 * This is the command dispatch that completes a chunked
924 * page program operation.
925 */
926 if (info->data_size == 0) {
927 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
928 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
929 | command;
930 info->ndcb1 = 0;
931 info->ndcb2 = 0;
932 info->ndcb3 = 0;
933 }
934 } else {
935 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
936 | NDCB0_AUTO_RS
937 | NDCB0_ST_ROW_EN
938 | NDCB0_DBC
939 | (NAND_CMD_PAGEPROG << 8)
940 | NAND_CMD_SEQIN
941 | addr_cycle;
942 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800943 break;
944
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300945 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -0300946 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300947 info->ndcb0 |= NDCB0_CMD_TYPE(0)
948 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300949 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300950 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300951 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -0300952 info->ndcb3 = INIT_BUFFER_SIZE;
953 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300954 break;
955
Lei Wen4eb2da82011-02-28 10:32:13 +0800956 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300957 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +0800958 info->ndcb0 |= NDCB0_CMD_TYPE(3)
959 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300960 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300961 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800962
963 info->data_size = 8;
964 break;
965 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800966 info->buf_count = 1;
967 info->ndcb0 |= NDCB0_CMD_TYPE(4)
968 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300969 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800970
971 info->data_size = 8;
972 break;
973
974 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800975 info->ndcb0 |= NDCB0_CMD_TYPE(2)
976 | NDCB0_AUTO_RS
977 | NDCB0_ADDR_CYC(3)
978 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300979 | (NAND_CMD_ERASE2 << 8)
980 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800981 info->ndcb1 = page_addr;
982 info->ndcb2 = 0;
983
984 break;
985 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800986 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300987 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800988
989 break;
990
991 case NAND_CMD_ERASE2:
992 exec_cmd = 0;
993 break;
994
995 default:
996 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700997 dev_err(&info->pdev->dev, "non-supported command %x\n",
998 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800999 break;
1000 }
1001
1002 return exec_cmd;
1003}
1004
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001005static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1006 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +08001007{
Lei Wend4568822011-07-14 20:44:32 -07001008 struct pxa3xx_nand_host *host = mtd->priv;
1009 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001010 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +08001011
Lei Wen4eb2da82011-02-28 10:32:13 +08001012 /*
1013 * if this is a x16 device ,then convert the input
1014 * "byte" address into a "word" address appropriate
1015 * for indexing a word-oriented device
1016 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001017 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +08001018 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +08001019
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001020 /*
1021 * There may be different NAND chip hooked to
1022 * different chip select, so check whether
1023 * chip select has been changed, if yes, reset the timing
1024 */
1025 if (info->cs != host->cs) {
1026 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001027 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1028 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001029 }
1030
Ezequiel Garciac39ff032013-11-14 18:25:33 -03001031 prepare_start_command(info, command);
1032
Lei Wend4568822011-07-14 20:44:32 -07001033 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001034 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1035
Lei Wenf8155a42011-02-28 10:32:11 +08001036 if (exec_cmd) {
1037 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001038 init_completion(&info->dev_ready);
1039 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001040 pxa3xx_nand_start(info);
1041
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001042 if (!wait_for_completion_timeout(&info->cmd_complete,
1043 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001044 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001045 /* Stop State Machine for next command cycle */
1046 pxa3xx_nand_stop(info);
1047 }
eric miaofe69af02008-02-14 15:48:23 +08001048 }
Lei Wend4568822011-07-14 20:44:32 -07001049 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001050}
1051
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001052static void nand_cmdfunc_extended(struct mtd_info *mtd,
1053 const unsigned command,
1054 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001055{
1056 struct pxa3xx_nand_host *host = mtd->priv;
1057 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001058 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001059
1060 /*
1061 * if this is a x16 device then convert the input
1062 * "byte" address into a "word" address appropriate
1063 * for indexing a word-oriented device
1064 */
1065 if (info->reg_ndcr & NDCR_DWIDTH_M)
1066 column /= 2;
1067
1068 /*
1069 * There may be different NAND chip hooked to
1070 * different chip select, so check whether
1071 * chip select has been changed, if yes, reset the timing
1072 */
1073 if (info->cs != host->cs) {
1074 info->cs = host->cs;
1075 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1076 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1077 }
1078
1079 /* Select the extended command for the first command */
1080 switch (command) {
1081 case NAND_CMD_READ0:
1082 case NAND_CMD_READOOB:
1083 ext_cmd_type = EXT_CMD_TYPE_MONO;
1084 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001085 case NAND_CMD_SEQIN:
1086 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1087 break;
1088 case NAND_CMD_PAGEPROG:
1089 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1090 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001091 default:
1092 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001093 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001094 }
1095
1096 prepare_start_command(info, command);
1097
1098 /*
1099 * Prepare the "is ready" completion before starting a command
1100 * transaction sequence. If the command is not executed the
1101 * completion will be completed, see below.
1102 *
1103 * We can do that inside the loop because the command variable
1104 * is invariant and thus so is the exec_cmd.
1105 */
1106 info->need_wait = 1;
1107 init_completion(&info->dev_ready);
1108 do {
1109 info->state = STATE_PREPARED;
1110 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1111 column, page_addr);
1112 if (!exec_cmd) {
1113 info->need_wait = 0;
1114 complete(&info->dev_ready);
1115 break;
1116 }
1117
1118 init_completion(&info->cmd_complete);
1119 pxa3xx_nand_start(info);
1120
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001121 if (!wait_for_completion_timeout(&info->cmd_complete,
1122 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001123 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1124 /* Stop State Machine for next command cycle */
1125 pxa3xx_nand_stop(info);
1126 break;
1127 }
1128
1129 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001130 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1131 break;
1132
1133 /*
1134 * After a splitted program command sequence has issued
1135 * the command dispatch, the command sequence is complete.
1136 */
1137 if (info->data_size == 0 &&
1138 command == NAND_CMD_PAGEPROG &&
1139 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001140 break;
1141
1142 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1143 /* Last read: issue a 'last naked read' */
1144 if (info->data_size == info->chunk_size)
1145 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1146 else
1147 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001148
1149 /*
1150 * If a splitted program command has no more data to transfer,
1151 * the command dispatch must be issued to complete.
1152 */
1153 } else if (command == NAND_CMD_PAGEPROG &&
1154 info->data_size == 0) {
1155 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001156 }
1157 } while (1);
1158
1159 info->state = STATE_IDLE;
1160}
1161
Josh Wufdbad98d2012-06-25 18:07:45 +08001162static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001163 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +08001164{
1165 chip->write_buf(mtd, buf, mtd->writesize);
1166 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001167
1168 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001169}
1170
1171static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001172 struct nand_chip *chip, uint8_t *buf, int oob_required,
1173 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001174{
Lei Wend4568822011-07-14 20:44:32 -07001175 struct pxa3xx_nand_host *host = mtd->priv;
1176 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001177
1178 chip->read_buf(mtd, buf, mtd->writesize);
1179 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1180
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001181 if (info->retcode == ERR_CORERR && info->use_ecc) {
1182 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1183
1184 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001185 /*
1186 * for blank page (all 0xff), HW will calculate its ECC as
1187 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001188 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001189 */
1190 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001191 info->retcode = ERR_NONE;
1192 else
Lei Wenf8155a42011-02-28 10:32:11 +08001193 mtd->ecc_stats.failed++;
1194 }
1195
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001196 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001197}
1198
eric miaofe69af02008-02-14 15:48:23 +08001199static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1200{
Lei Wend4568822011-07-14 20:44:32 -07001201 struct pxa3xx_nand_host *host = mtd->priv;
1202 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001203 char retval = 0xFF;
1204
1205 if (info->buf_start < info->buf_count)
1206 /* Has just send a new command? */
1207 retval = info->data_buff[info->buf_start++];
1208
1209 return retval;
1210}
1211
1212static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1213{
Lei Wend4568822011-07-14 20:44:32 -07001214 struct pxa3xx_nand_host *host = mtd->priv;
1215 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001216 u16 retval = 0xFFFF;
1217
1218 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1219 retval = *((u16 *)(info->data_buff+info->buf_start));
1220 info->buf_start += 2;
1221 }
1222 return retval;
1223}
1224
1225static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1226{
Lei Wend4568822011-07-14 20:44:32 -07001227 struct pxa3xx_nand_host *host = mtd->priv;
1228 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001229 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1230
1231 memcpy(buf, info->data_buff + info->buf_start, real_len);
1232 info->buf_start += real_len;
1233}
1234
1235static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1236 const uint8_t *buf, int len)
1237{
Lei Wend4568822011-07-14 20:44:32 -07001238 struct pxa3xx_nand_host *host = mtd->priv;
1239 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001240 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1241
1242 memcpy(info->data_buff + info->buf_start, buf, real_len);
1243 info->buf_start += real_len;
1244}
1245
eric miaofe69af02008-02-14 15:48:23 +08001246static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1247{
1248 return;
1249}
1250
1251static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1252{
Lei Wend4568822011-07-14 20:44:32 -07001253 struct pxa3xx_nand_host *host = mtd->priv;
1254 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001255
1256 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001257 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001258 if (!wait_for_completion_timeout(&info->dev_ready,
1259 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001260 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1261 return NAND_STATUS_FAIL;
1262 }
1263 }
eric miaofe69af02008-02-14 15:48:23 +08001264
1265 /* pxa3xx_nand_send_command has waited for command complete */
1266 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1267 if (info->retcode == ERR_NONE)
1268 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001269 else
1270 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001271 }
1272
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001273 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001274}
1275
eric miaofe69af02008-02-14 15:48:23 +08001276static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +02001277 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +08001278{
1279 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001280 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001281 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +08001282 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +08001283
Lei Wenda675b42011-07-14 20:44:31 -07001284 if (f->page_size != 2048 && f->page_size != 512) {
1285 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +08001286 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001287 }
eric miaofe69af02008-02-14 15:48:23 +08001288
Lei Wenda675b42011-07-14 20:44:31 -07001289 if (f->flash_width != 16 && f->flash_width != 8) {
1290 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +08001291 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -07001292 }
eric miaofe69af02008-02-14 15:48:23 +08001293
eric miaofe69af02008-02-14 15:48:23 +08001294 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001295 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +08001296
1297 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001298 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +08001299 else
Lei Wend4568822011-07-14 20:44:32 -07001300 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +08001301
1302 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -07001303 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +08001304 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
1305 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
1306 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
1307 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
1308
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001309 ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
eric miaofe69af02008-02-14 15:48:23 +08001310 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
1311
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001312 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +08001313
Lei Wend4568822011-07-14 20:44:32 -07001314 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +08001315 return 0;
1316}
1317
Mike Rapoportf2710492009-02-17 13:54:47 +02001318static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1319{
1320 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001321
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001322 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001323 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Robert Jarzmike971aff2015-09-28 22:56:51 +02001324 info->reg_ndcr = ndcr &
1325 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001326 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1327 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001328 return 0;
1329}
1330
eric miaofe69af02008-02-14 15:48:23 +08001331static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1332{
1333 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001334 struct dma_slave_config config;
1335 dma_cap_mask_t mask;
1336 struct pxad_param param;
1337 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001338
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001339 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1340 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001341 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001342 if (use_dma == 0)
1343 return 0;
1344
1345 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1346 if (ret)
1347 return ret;
1348
1349 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1350 dma_cap_zero(mask);
1351 dma_cap_set(DMA_SLAVE, mask);
1352 param.prio = PXAD_PRIO_LOWEST;
1353 param.drcmr = info->drcmr_dat;
1354 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1355 &param, &pdev->dev,
1356 "data");
1357 if (!info->dma_chan) {
1358 dev_err(&pdev->dev, "unable to request data dma channel\n");
1359 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001360 }
1361
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001362 memset(&config, 0, sizeof(config));
1363 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1364 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1365 config.src_addr = info->mmio_phys + NDDB;
1366 config.dst_addr = info->mmio_phys + NDDB;
1367 config.src_maxburst = 32;
1368 config.dst_maxburst = 32;
1369 ret = dmaengine_slave_config(info->dma_chan, &config);
1370 if (ret < 0) {
1371 dev_err(&info->pdev->dev,
1372 "dma channel configuration failed: %d\n",
1373 ret);
1374 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001375 }
1376
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001377 /*
1378 * Now that DMA buffers are allocated we turn on
1379 * DMA proper for I/O operations.
1380 */
1381 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001382 return 0;
1383}
1384
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001385static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1386{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001387 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001388 dmaengine_terminate_all(info->dma_chan);
1389 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001390 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001391 kfree(info->data_buff);
1392}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001393
Lei Wen401e67e2011-02-28 10:32:14 +08001394static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001395{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001396 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001397 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001398 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001399
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001400 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001401 chip = mtd->priv;
1402
Lei Wen401e67e2011-02-28 10:32:14 +08001403 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001404 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1405 if (ret)
1406 return ret;
1407
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001408 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001409 ret = chip->waitfunc(mtd, chip);
1410 if (ret & NAND_STATUS_FAIL)
1411 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001412
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001413 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001414}
eric miaofe69af02008-02-14 15:48:23 +08001415
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001416static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1417 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001418 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001419{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001420 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001421 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001422 info->spare_size = 40;
1423 info->ecc_size = 24;
1424 ecc->mode = NAND_ECC_HW;
1425 ecc->size = 512;
1426 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001427
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001428 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001429 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001430 info->spare_size = 8;
1431 info->ecc_size = 8;
1432 ecc->mode = NAND_ECC_HW;
1433 ecc->size = 512;
1434 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001435
Brian Norris6033a942013-11-14 14:41:32 -08001436 /*
1437 * Required ECC: 4-bit correction per 512 bytes
1438 * Select: 16-bit correction per 2048 bytes
1439 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001440 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1441 info->ecc_bch = 1;
1442 info->chunk_size = 2048;
1443 info->spare_size = 32;
1444 info->ecc_size = 32;
1445 ecc->mode = NAND_ECC_HW;
1446 ecc->size = info->chunk_size;
1447 ecc->layout = &ecc_layout_2KB_bch4bit;
1448 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001449
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001450 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001451 info->ecc_bch = 1;
1452 info->chunk_size = 2048;
1453 info->spare_size = 32;
1454 info->ecc_size = 32;
1455 ecc->mode = NAND_ECC_HW;
1456 ecc->size = info->chunk_size;
1457 ecc->layout = &ecc_layout_4KB_bch4bit;
1458 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001459
Brian Norris6033a942013-11-14 14:41:32 -08001460 /*
1461 * Required ECC: 8-bit correction per 512 bytes
1462 * Select: 16-bit correction per 1024 bytes
1463 */
1464 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001465 info->ecc_bch = 1;
1466 info->chunk_size = 1024;
1467 info->spare_size = 0;
1468 info->ecc_size = 32;
1469 ecc->mode = NAND_ECC_HW;
1470 ecc->size = info->chunk_size;
1471 ecc->layout = &ecc_layout_4KB_bch8bit;
1472 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001473 } else {
1474 dev_err(&info->pdev->dev,
1475 "ECC strength %d at page size %d is not supported\n",
1476 strength, page_size);
1477 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001478 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001479
1480 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1481 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001482 return 0;
1483}
1484
Lei Wen401e67e2011-02-28 10:32:14 +08001485static int pxa3xx_nand_scan(struct mtd_info *mtd)
1486{
Lei Wend4568822011-07-14 20:44:32 -07001487 struct pxa3xx_nand_host *host = mtd->priv;
1488 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001489 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001490 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001491 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001492 const struct pxa3xx_nand_flash *f = NULL;
1493 struct nand_chip *chip = mtd->priv;
1494 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001495 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001496 int i, ret, num;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001497 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001498
1499 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001500 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001501
Antoine Ténartbc3e00f2015-08-18 10:59:10 +02001502 /* Set a default chunk size */
1503 info->chunk_size = 512;
1504
Lei Wen401e67e2011-02-28 10:32:14 +08001505 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001506 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001507 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1508 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001509
Lei Wend4568822011-07-14 20:44:32 -07001510 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001511 }
1512
1513 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1514 id = *((uint16_t *)(info->data_buff));
1515 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001516 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001517 else {
Lei Wenda675b42011-07-14 20:44:31 -07001518 dev_warn(&info->pdev->dev,
1519 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001520
1521 return -EINVAL;
1522 }
1523
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001524 num = ARRAY_SIZE(builtin_flash_types) - 1;
Lei Wen401e67e2011-02-28 10:32:14 +08001525 for (i = 0; i < num; i++) {
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001526 f = &builtin_flash_types[i + 1];
Lei Wen401e67e2011-02-28 10:32:14 +08001527
1528 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001529 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001530 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001531 }
1532
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -03001533 if (i >= (ARRAY_SIZE(builtin_flash_types) - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001534 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001535
1536 return -EINVAL;
1537 }
1538
Lei Wend4568822011-07-14 20:44:32 -07001539 ret = pxa3xx_nand_config_flash(info, f);
1540 if (ret) {
1541 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1542 return ret;
1543 }
1544
Antoine Ténart7c2f7172015-02-12 15:53:27 +01001545 memset(pxa3xx_flash_ids, 0, sizeof(pxa3xx_flash_ids));
1546
Lei Wen4332c112011-03-03 11:27:01 +08001547 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001548 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001549 pxa3xx_flash_ids[0].pagesize = f->page_size;
1550 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1551 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1552 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1553 if (f->flash_width == 16)
1554 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001555 pxa3xx_flash_ids[1].name = NULL;
1556 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001557KEEP_CONFIG:
Robert Jarzmike971aff2015-09-28 22:56:51 +02001558 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001559 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001560 chip->options |= NAND_BUSWIDTH_16;
1561
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001562 /* Device detection must be done with ECC disabled */
1563 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1564 nand_writel(info, NDECCCTRL, 0x0);
1565
Lei Wen0fab0282011-06-07 03:01:06 -07001566 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001567 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001568
1569 if (pdata->flash_bbt) {
1570 /*
1571 * We'll use a bad block table stored in-flash and don't
1572 * allow writing the bad block marker to the flash.
1573 */
1574 chip->bbt_options |= NAND_BBT_USE_FLASH |
1575 NAND_BBT_NO_OOB_BBM;
1576 chip->bbt_td = &bbt_main_descr;
1577 chip->bbt_md = &bbt_mirror_descr;
1578 }
1579
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001580 /*
1581 * If the page size is bigger than the FIFO size, let's check
1582 * we are given the right variant and then switch to the extended
1583 * (aka splitted) command handling,
1584 */
1585 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1586 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1587 chip->cmdfunc = nand_cmdfunc_extended;
1588 } else {
1589 dev_err(&info->pdev->dev,
1590 "unsupported page size on this variant\n");
1591 return -ENODEV;
1592 }
1593 }
1594
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001595 if (pdata->ecc_strength && pdata->ecc_step_size) {
1596 ecc_strength = pdata->ecc_strength;
1597 ecc_step = pdata->ecc_step_size;
1598 } else {
1599 ecc_strength = chip->ecc_strength_ds;
1600 ecc_step = chip->ecc_step_ds;
1601 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001602
1603 /* Set default ECC strength requirements on non-ONFI devices */
1604 if (ecc_strength < 1 && ecc_step < 1) {
1605 ecc_strength = 1;
1606 ecc_step = 512;
1607 }
1608
1609 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1610 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001611 if (ret)
1612 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001613
Lei Wen4332c112011-03-03 11:27:01 +08001614 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001615 if (mtd->writesize >= 2048)
1616 host->col_addr_cycles = 2;
1617 else
1618 host->col_addr_cycles = 1;
1619
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001620 /* release the initial buffer */
1621 kfree(info->data_buff);
1622
1623 /* allocate the real data + oob buffer */
1624 info->buf_size = mtd->writesize + mtd->oobsize;
1625 ret = pxa3xx_nand_init_buff(info);
1626 if (ret)
1627 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001628 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001629
Lei Wen4332c112011-03-03 11:27:01 +08001630 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001631 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001632 else
Lei Wend4568822011-07-14 20:44:32 -07001633 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001634 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001635}
1636
Lei Wend4568822011-07-14 20:44:32 -07001637static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001638{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001639 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001640 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001641 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001642 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001643 struct mtd_info *mtd;
1644 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001645 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001646
Jingoo Han453810b2013-07-30 17:18:33 +09001647 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001648 if (pdata->num_cs <= 0)
1649 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001650 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1651 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1652 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001653 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001654
eric miaofe69af02008-02-14 15:48:23 +08001655 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001656 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001657 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001658 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001659 chip = (struct nand_chip *)(&mtd[1]);
1660 host = (struct pxa3xx_nand_host *)chip;
1661 info->host[cs] = host;
1662 host->mtd = mtd;
1663 host->cs = cs;
1664 host->info_data = info;
1665 mtd->priv = host;
Frans Klaver550dab52015-06-10 22:39:01 +02001666 mtd->dev.parent = &pdev->dev;
eric miaofe69af02008-02-14 15:48:23 +08001667
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001668 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1669 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1670 chip->controller = &info->controller;
1671 chip->waitfunc = pxa3xx_nand_waitfunc;
1672 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001673 chip->read_word = pxa3xx_nand_read_word;
1674 chip->read_byte = pxa3xx_nand_read_byte;
1675 chip->read_buf = pxa3xx_nand_read_buf;
1676 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001677 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001678 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001679 }
Lei Wen401e67e2011-02-28 10:32:14 +08001680
1681 spin_lock_init(&chip->controller->lock);
1682 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001683 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001684 if (IS_ERR(info->clk)) {
1685 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001686 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001687 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001688 ret = clk_prepare_enable(info->clk);
1689 if (ret < 0)
1690 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001691
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001692 if (use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001693 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1694 if (r == NULL) {
1695 dev_err(&pdev->dev,
1696 "no resource defined for data DMA\n");
1697 ret = -ENXIO;
1698 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001699 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001700 info->drcmr_dat = r->start;
1701
1702 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1703 if (r == NULL) {
1704 dev_err(&pdev->dev,
1705 "no resource defined for cmd DMA\n");
1706 ret = -ENXIO;
1707 goto fail_disable_clk;
1708 }
1709 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001710 }
eric miaofe69af02008-02-14 15:48:23 +08001711
1712 irq = platform_get_irq(pdev, 0);
1713 if (irq < 0) {
1714 dev_err(&pdev->dev, "no IRQ resource defined\n");
1715 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001716 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001717 }
1718
1719 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001720 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1721 if (IS_ERR(info->mmio_base)) {
1722 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001723 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001724 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001725 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001726
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001727 /* Allocate a buffer to allow flash detection */
1728 info->buf_size = INIT_BUFFER_SIZE;
1729 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1730 if (info->data_buff == NULL) {
1731 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001732 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001733 }
eric miaofe69af02008-02-14 15:48:23 +08001734
Haojian Zhuang346e1252009-09-10 14:27:23 +08001735 /* initialize all interrupts to be disabled */
1736 disable_int(info, NDSR_MASK);
1737
Robert Jarzmik24542252015-02-20 19:36:43 +01001738 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1739 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1740 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001741 if (ret < 0) {
1742 dev_err(&pdev->dev, "failed to request IRQ\n");
1743 goto fail_free_buf;
1744 }
1745
Lei Wene353a202011-03-03 11:08:30 +08001746 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001747
Lei Wend4568822011-07-14 20:44:32 -07001748 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001749
eric miaofe69af02008-02-14 15:48:23 +08001750fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001751 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001752 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001753fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001754 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001755 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001756}
1757
1758static int pxa3xx_nand_remove(struct platform_device *pdev)
1759{
Lei Wene353a202011-03-03 11:08:30 +08001760 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001761 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001762 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001763
Lei Wend4568822011-07-14 20:44:32 -07001764 if (!info)
1765 return 0;
1766
Jingoo Han453810b2013-07-30 17:18:33 +09001767 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001768
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001769 irq = platform_get_irq(pdev, 0);
1770 if (irq >= 0)
1771 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001772 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001773
Robert Jarzmike971aff2015-09-28 22:56:51 +02001774 /*
1775 * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
1776 * In order to prevent a lockup of the system bus, the DFI bus
1777 * arbitration is granted to SMC upon driver removal. This is done by
1778 * setting the x_ARB_CNTL bit, which also prevents the NAND to have
1779 * access to the bus anymore.
1780 */
1781 nand_writel(info, NDCR,
1782 (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
1783 NFCV1_NDCR_ARB_CNTL);
Ezequiel Garciafb320612013-04-17 13:38:12 -03001784 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001785
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001786 for (cs = 0; cs < pdata->num_cs; cs++)
1787 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001788 return 0;
1789}
1790
Daniel Mack1e7ba632012-07-22 19:51:02 +02001791static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1792{
1793 struct pxa3xx_nand_platform_data *pdata;
1794 struct device_node *np = pdev->dev.of_node;
1795 const struct of_device_id *of_id =
1796 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1797
1798 if (!of_id)
1799 return 0;
1800
1801 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1802 if (!pdata)
1803 return -ENOMEM;
1804
1805 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1806 pdata->enable_arbiter = 1;
1807 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1808 pdata->keep_config = 1;
1809 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001810 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001811
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001812 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1813 if (pdata->ecc_strength < 0)
1814 pdata->ecc_strength = 0;
1815
1816 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1817 if (pdata->ecc_step_size < 0)
1818 pdata->ecc_step_size = 0;
1819
Daniel Mack1e7ba632012-07-22 19:51:02 +02001820 pdev->dev.platform_data = pdata;
1821
1822 return 0;
1823}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001824
Lei Wene353a202011-03-03 11:08:30 +08001825static int pxa3xx_nand_probe(struct platform_device *pdev)
1826{
1827 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001828 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001829 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001830 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001831
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001832 dma_available = IS_ENABLED(CONFIG_ARM) &&
1833 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1834 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001835 use_dma = 0;
1836 dev_warn(&pdev->dev,
1837 "This platform can't do DMA on this device\n");
1838 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001839
Daniel Mack1e7ba632012-07-22 19:51:02 +02001840 ret = pxa3xx_nand_probe_dt(pdev);
1841 if (ret)
1842 return ret;
1843
Jingoo Han453810b2013-07-30 17:18:33 +09001844 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001845 if (!pdata) {
1846 dev_err(&pdev->dev, "no platform data defined\n");
1847 return -ENODEV;
1848 }
1849
Lei Wend4568822011-07-14 20:44:32 -07001850 ret = alloc_nand_resource(pdev);
1851 if (ret) {
1852 dev_err(&pdev->dev, "alloc nand resource failed\n");
1853 return ret;
1854 }
Lei Wene353a202011-03-03 11:08:30 +08001855
Lei Wend4568822011-07-14 20:44:32 -07001856 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001857 probe_success = 0;
1858 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001859 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001860
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001861 /*
1862 * The mtd name matches the one used in 'mtdparts' kernel
1863 * parameter. This name cannot be changed or otherwise
1864 * user's mtd partitions configuration would get broken.
1865 */
1866 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001867 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001868 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001869 if (ret) {
1870 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1871 cs);
1872 continue;
1873 }
1874
Daniel Mack1e7ba632012-07-22 19:51:02 +02001875 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001876 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001877 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001878 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001879 if (!ret)
1880 probe_success = 1;
1881 }
1882
1883 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001884 pxa3xx_nand_remove(pdev);
1885 return -ENODEV;
1886 }
1887
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001888 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001889}
1890
eric miaofe69af02008-02-14 15:48:23 +08001891#ifdef CONFIG_PM
1892static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1893{
Lei Wene353a202011-03-03 11:08:30 +08001894 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001895 struct pxa3xx_nand_platform_data *pdata;
1896 struct mtd_info *mtd;
1897 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001898
Jingoo Han453810b2013-07-30 17:18:33 +09001899 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001900 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001901 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1902 return -EAGAIN;
1903 }
1904
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001905 for (cs = 0; cs < pdata->num_cs; cs++) {
1906 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001907 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001908 }
1909
eric miaofe69af02008-02-14 15:48:23 +08001910 return 0;
1911}
1912
1913static int pxa3xx_nand_resume(struct platform_device *pdev)
1914{
Lei Wene353a202011-03-03 11:08:30 +08001915 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001916 struct pxa3xx_nand_platform_data *pdata;
1917 struct mtd_info *mtd;
1918 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001919
Jingoo Han453810b2013-07-30 17:18:33 +09001920 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001921 /* We don't want to handle interrupt without calling mtd routine */
1922 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001923
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001924 /*
1925 * Directly set the chip select to a invalid value,
1926 * then the driver would reset the timing according
1927 * to current chip select at the beginning of cmdfunc
1928 */
1929 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001930
Lei Wen051fc412011-07-14 20:44:30 -07001931 /*
1932 * As the spec says, the NDSR would be updated to 0x1800 when
1933 * doing the nand_clk disable/enable.
1934 * To prevent it damaging state machine of the driver, clear
1935 * all status before resume
1936 */
1937 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001938 for (cs = 0; cs < pdata->num_cs; cs++) {
1939 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001940 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001941 }
1942
Lei Wen18c81b12010-08-17 17:25:57 +08001943 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001944}
1945#else
1946#define pxa3xx_nand_suspend NULL
1947#define pxa3xx_nand_resume NULL
1948#endif
1949
1950static struct platform_driver pxa3xx_nand_driver = {
1951 .driver = {
1952 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301953 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001954 },
1955 .probe = pxa3xx_nand_probe,
1956 .remove = pxa3xx_nand_remove,
1957 .suspend = pxa3xx_nand_suspend,
1958 .resume = pxa3xx_nand_resume,
1959};
1960
Axel Linf99640d2011-11-27 20:45:03 +08001961module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001962
1963MODULE_LICENSE("GPL");
1964MODULE_DESCRIPTION("PXA3xx NAND controller driver");