blob: 54512ce254432cedaeffc2aeeb0afb9574256f22 [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 {
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300269 uint32_t chip_id;
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300270 unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */
271 unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300272 struct pxa3xx_nand_timing *timing; /* NAND Flash timing */
273};
274
Lei Wenc1f82472010-08-17 13:50:23 +0800275static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800276 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
277 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
278 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
279 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800280};
281
Lei Wenc1f82472010-08-17 13:50:23 +0800282static struct pxa3xx_nand_flash builtin_flash_types[] = {
Antoine Ténart89c17022015-10-21 10:29:04 +0200283 { 0x46ec, 16, 16, &timing[1] },
284 { 0xdaec, 8, 8, &timing[1] },
285 { 0xd7ec, 8, 8, &timing[1] },
286 { 0xa12c, 8, 8, &timing[2] },
287 { 0xb12c, 16, 16, &timing[2] },
288 { 0xdc2c, 8, 8, &timing[2] },
289 { 0xcc2c, 16, 16, &timing[2] },
290 { 0xba20, 16, 16, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800291};
292
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300293static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
294static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
295
296static struct nand_bbt_descr bbt_main_descr = {
297 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
298 | NAND_BBT_2BIT | NAND_BBT_VERSION,
299 .offs = 8,
300 .len = 6,
301 .veroffs = 14,
302 .maxblocks = 8, /* Last 8 blocks in each chip */
303 .pattern = bbt_pattern
304};
305
306static struct nand_bbt_descr bbt_mirror_descr = {
307 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
308 | NAND_BBT_2BIT | NAND_BBT_VERSION,
309 .offs = 8,
310 .len = 6,
311 .veroffs = 14,
312 .maxblocks = 8, /* Last 8 blocks in each chip */
313 .pattern = bbt_mirror_pattern
314};
315
Rodolfo Giometti3db227b2014-01-13 15:35:38 +0100316static struct nand_ecclayout ecc_layout_2KB_bch4bit = {
317 .eccbytes = 32,
318 .eccpos = {
319 32, 33, 34, 35, 36, 37, 38, 39,
320 40, 41, 42, 43, 44, 45, 46, 47,
321 48, 49, 50, 51, 52, 53, 54, 55,
322 56, 57, 58, 59, 60, 61, 62, 63},
323 .oobfree = { {2, 30} }
324};
325
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300326static struct nand_ecclayout ecc_layout_4KB_bch4bit = {
327 .eccbytes = 64,
328 .eccpos = {
329 32, 33, 34, 35, 36, 37, 38, 39,
330 40, 41, 42, 43, 44, 45, 46, 47,
331 48, 49, 50, 51, 52, 53, 54, 55,
332 56, 57, 58, 59, 60, 61, 62, 63,
333 96, 97, 98, 99, 100, 101, 102, 103,
334 104, 105, 106, 107, 108, 109, 110, 111,
335 112, 113, 114, 115, 116, 117, 118, 119,
336 120, 121, 122, 123, 124, 125, 126, 127},
337 /* Bootrom looks in bytes 0 & 5 for bad blocks */
338 .oobfree = { {6, 26}, { 64, 32} }
339};
340
341static struct nand_ecclayout ecc_layout_4KB_bch8bit = {
342 .eccbytes = 128,
343 .eccpos = {
344 32, 33, 34, 35, 36, 37, 38, 39,
345 40, 41, 42, 43, 44, 45, 46, 47,
346 48, 49, 50, 51, 52, 53, 54, 55,
347 56, 57, 58, 59, 60, 61, 62, 63},
348 .oobfree = { }
349};
350
eric miaofe69af02008-02-14 15:48:23 +0800351#define NDTR0_tCH(c) (min((c), 7) << 19)
352#define NDTR0_tCS(c) (min((c), 7) << 16)
353#define NDTR0_tWH(c) (min((c), 7) << 11)
354#define NDTR0_tWP(c) (min((c), 7) << 8)
355#define NDTR0_tRH(c) (min((c), 7) << 3)
356#define NDTR0_tRP(c) (min((c), 7) << 0)
357
358#define NDTR1_tR(c) (min((c), 65535) << 16)
359#define NDTR1_tWHR(c) (min((c), 15) << 4)
360#define NDTR1_tAR(c) (min((c), 15) << 0)
361
362/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800363#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800364
Jingoo Han17754ad2014-05-07 17:49:13 +0900365static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300366 {
367 .compatible = "marvell,pxa3xx-nand",
368 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
369 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300370 {
371 .compatible = "marvell,armada370-nand",
372 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
373 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300374 {}
375};
376MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
377
378static enum pxa3xx_nand_variant
379pxa3xx_nand_get_variant(struct platform_device *pdev)
380{
381 const struct of_device_id *of_id =
382 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
383 if (!of_id)
384 return PXA3XX_NAND_VARIANT_PXA;
385 return (enum pxa3xx_nand_variant)of_id->data;
386}
387
Lei Wend4568822011-07-14 20:44:32 -0700388static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200389 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800390{
Lei Wend4568822011-07-14 20:44:32 -0700391 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800392 unsigned long nand_clk = clk_get_rate(info->clk);
393 uint32_t ndtr0, ndtr1;
394
395 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
396 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
397 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
398 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
399 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
400 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
401
402 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
403 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
404 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
405
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300406 info->ndtr0cs0 = ndtr0;
407 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800408 nand_writel(info, NDTR0CS0, ndtr0);
409 nand_writel(info, NDTR1CS0, ndtr1);
410}
411
Antoine Ténart3f225b72015-10-21 10:29:02 +0200412static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
413 const struct nand_sdr_timings *t)
414{
415 struct pxa3xx_nand_info *info = host->info_data;
416 struct nand_chip *chip = &host->chip;
417 unsigned long nand_clk = clk_get_rate(info->clk);
418 uint32_t ndtr0, ndtr1;
419
420 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
421 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
422 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
423 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
424 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
425 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
426 u32 tR = chip->chip_delay * 1000;
427 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
428 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
429
430 /* fallback to a default value if tR = 0 */
431 if (!tR)
432 tR = 20000;
433
434 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
435 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
436 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
437 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
438 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
439 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
440
441 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
442 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
443 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
444
445 info->ndtr0cs0 = ndtr0;
446 info->ndtr1cs0 = ndtr1;
447 nand_writel(info, NDTR0CS0, ndtr0);
448 nand_writel(info, NDTR1CS0, ndtr1);
449}
450
451static int pxa3xx_nand_init_timings_compat(struct pxa3xx_nand_host *host,
452 unsigned int *flash_width,
453 unsigned int *dfc_width)
454{
455 struct nand_chip *chip = &host->chip;
456 struct pxa3xx_nand_info *info = host->info_data;
457 const struct pxa3xx_nand_flash *f = NULL;
458 int i, id, ntypes;
459
460 ntypes = ARRAY_SIZE(builtin_flash_types);
461
462 chip->cmdfunc(host->mtd, NAND_CMD_READID, 0x00, -1);
463
464 id = chip->read_byte(host->mtd);
465 id |= chip->read_byte(host->mtd) << 0x8;
466
467 for (i = 0; i < ntypes; i++) {
468 f = &builtin_flash_types[i];
469
470 if (f->chip_id == id)
471 break;
472 }
473
474 if (i == ntypes) {
475 dev_err(&info->pdev->dev, "Error: timings not found\n");
476 return -EINVAL;
477 }
478
479 pxa3xx_nand_set_timing(host, f->timing);
480
481 *flash_width = f->flash_width;
482 *dfc_width = f->dfc_width;
483
484 return 0;
485}
486
487static int pxa3xx_nand_init_timings_onfi(struct pxa3xx_nand_host *host,
488 int mode)
489{
490 const struct nand_sdr_timings *timings;
491
492 mode = fls(mode) - 1;
493 if (mode < 0)
494 mode = 0;
495
496 timings = onfi_async_timing_mode_to_sdr_timings(mode);
497 if (IS_ERR(timings))
498 return PTR_ERR(timings);
499
500 pxa3xx_nand_set_sdr_timing(host, timings);
501
502 return 0;
503}
504
505static int pxa3xx_nand_init(struct pxa3xx_nand_host *host)
506{
507 struct nand_chip *chip = &host->chip;
508 struct pxa3xx_nand_info *info = host->info_data;
509 unsigned int flash_width = 0, dfc_width = 0;
510 int mode, err;
511
512 mode = onfi_get_async_timing_mode(chip);
513 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
514 err = pxa3xx_nand_init_timings_compat(host, &flash_width,
515 &dfc_width);
516 if (err)
517 return err;
518
519 if (flash_width == 16) {
520 info->reg_ndcr |= NDCR_DWIDTH_M;
521 chip->options |= NAND_BUSWIDTH_16;
522 }
523
524 info->reg_ndcr |= (dfc_width == 16) ? NDCR_DWIDTH_C : 0;
525 } else {
526 err = pxa3xx_nand_init_timings_onfi(host, mode);
527 if (err)
528 return err;
529 }
530
531 return 0;
532}
533
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300534/*
535 * Set the data and OOB size, depending on the selected
536 * spare and ECC configuration.
537 * Only applicable to READ0, READOOB and PAGEPROG commands.
538 */
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300539static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info,
540 struct mtd_info *mtd)
eric miaofe69af02008-02-14 15:48:23 +0800541{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300542 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800543
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300544 info->data_size = mtd->writesize;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300545 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800546 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800547
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300548 info->oob_size = info->spare_size;
549 if (!info->use_ecc)
550 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800551}
552
Lei Wenf8155a42011-02-28 10:32:11 +0800553/**
554 * NOTE: it is a must to set ND_RUN firstly, then write
555 * command buffer, otherwise, it does not work.
556 * We enable all the interrupt at the same time, and
557 * let pxa3xx_nand_irq to handle all logic.
558 */
559static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
560{
561 uint32_t ndcr;
562
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300563 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300564
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300565 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300566 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300567 if (info->ecc_bch)
568 nand_writel(info, NDECCCTRL, 0x1);
569 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300570 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300571 if (info->ecc_bch)
572 nand_writel(info, NDECCCTRL, 0x0);
573 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300574
575 if (info->use_dma)
576 ndcr |= NDCR_DMA_EN;
577 else
578 ndcr &= ~NDCR_DMA_EN;
579
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300580 if (info->use_spare)
581 ndcr |= NDCR_SPARE_EN;
582 else
583 ndcr &= ~NDCR_SPARE_EN;
584
Lei Wenf8155a42011-02-28 10:32:11 +0800585 ndcr |= NDCR_ND_RUN;
586
587 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800588 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200589 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800590 nand_writel(info, NDCR, ndcr);
591}
592
593static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
594{
595 uint32_t ndcr;
596 int timeout = NAND_STOP_DELAY;
597
598 /* wait RUN bit in NDCR become 0 */
599 ndcr = nand_readl(info, NDCR);
600 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
601 ndcr = nand_readl(info, NDCR);
602 udelay(1);
603 }
604
605 if (timeout <= 0) {
606 ndcr &= ~NDCR_ND_RUN;
607 nand_writel(info, NDCR, ndcr);
608 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200609 if (info->dma_chan)
610 dmaengine_terminate_all(info->dma_chan);
611
Lei Wenf8155a42011-02-28 10:32:11 +0800612 /* clear status bits */
613 nand_writel(info, NDSR, NDSR_MASK);
614}
615
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300616static void __maybe_unused
617enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800618{
619 uint32_t ndcr;
620
621 ndcr = nand_readl(info, NDCR);
622 nand_writel(info, NDCR, ndcr & ~int_mask);
623}
624
625static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
626{
627 uint32_t ndcr;
628
629 ndcr = nand_readl(info, NDCR);
630 nand_writel(info, NDCR, ndcr | int_mask);
631}
632
Maxime Ripard8dad0382015-02-18 11:32:07 +0100633static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
634{
635 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200636 u32 val;
637 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100638
639 /*
640 * According to the datasheet, when reading from NDDB
641 * with BCH enabled, after each 32 bytes reads, we
642 * have to make sure that the NDSR.RDDREQ bit is set.
643 *
644 * Drain the FIFO 8 32 bits reads at a time, and skip
645 * the polling on the last read.
646 */
647 while (len > 8) {
Antoine Ténartab53a572015-10-21 10:29:00 +0200648 ioread32_rep(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100649
Maxime Ripardafca11e2015-04-07 15:32:45 +0200650 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
651 val & NDSR_RDDREQ, 1000, 5000);
652 if (ret) {
653 dev_err(&info->pdev->dev,
654 "Timeout on RDDREQ while draining the FIFO\n");
655 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100656 }
657
658 data += 32;
659 len -= 8;
660 }
661 }
662
Antoine Ténartab53a572015-10-21 10:29:00 +0200663 ioread32_rep(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100664}
665
Lei Wenf8155a42011-02-28 10:32:11 +0800666static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800667{
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300668 unsigned int do_bytes = min(info->data_size, info->chunk_size);
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300669
eric miaofe69af02008-02-14 15:48:23 +0800670 switch (info->state) {
671 case STATE_PIO_WRITING:
Rob Herringce914e62015-04-30 15:17:47 -0500672 writesl(info->mmio_base + NDDB,
673 info->data_buff + info->data_buff_pos,
674 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300675
Lei Wen9d8b1042010-08-17 14:09:30 +0800676 if (info->oob_size > 0)
Rob Herringce914e62015-04-30 15:17:47 -0500677 writesl(info->mmio_base + NDDB,
678 info->oob_buff + info->oob_buff_pos,
679 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800680 break;
681 case STATE_PIO_READING:
Maxime Ripard8dad0382015-02-18 11:32:07 +0100682 drain_fifo(info,
683 info->data_buff + info->data_buff_pos,
684 DIV_ROUND_UP(do_bytes, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300685
Lei Wen9d8b1042010-08-17 14:09:30 +0800686 if (info->oob_size > 0)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100687 drain_fifo(info,
688 info->oob_buff + info->oob_buff_pos,
689 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800690 break;
691 default:
Lei Wenda675b42011-07-14 20:44:31 -0700692 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800693 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800694 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800695 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300696
697 /* Update buffer pointers for multi-page read/write */
698 info->data_buff_pos += do_bytes;
699 info->oob_buff_pos += info->oob_size;
700 info->data_size -= do_bytes;
eric miaofe69af02008-02-14 15:48:23 +0800701}
702
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200703static void pxa3xx_nand_data_dma_irq(void *data)
704{
705 struct pxa3xx_nand_info *info = data;
706 struct dma_tx_state state;
707 enum dma_status status;
708
709 status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state);
710 if (likely(status == DMA_COMPLETE)) {
711 info->state = STATE_DMA_DONE;
712 } else {
713 dev_err(&info->pdev->dev, "DMA error on data channel\n");
714 info->retcode = ERR_DMABUSERR;
715 }
716 dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
717
718 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
719 enable_int(info, NDCR_INT_MASK);
720}
721
Lei Wenf8155a42011-02-28 10:32:11 +0800722static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800723{
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200724 enum dma_transfer_direction direction;
725 struct dma_async_tx_descriptor *tx;
eric miaofe69af02008-02-14 15:48:23 +0800726
Lei Wenf8155a42011-02-28 10:32:11 +0800727 switch (info->state) {
728 case STATE_DMA_WRITING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200729 info->dma_dir = DMA_TO_DEVICE;
730 direction = DMA_MEM_TO_DEV;
Lei Wenf8155a42011-02-28 10:32:11 +0800731 break;
732 case STATE_DMA_READING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200733 info->dma_dir = DMA_FROM_DEVICE;
734 direction = DMA_DEV_TO_MEM;
Lei Wenf8155a42011-02-28 10:32:11 +0800735 break;
736 default:
Lei Wenda675b42011-07-14 20:44:31 -0700737 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800738 info->state);
739 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800740 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200741 info->sg.length = info->data_size +
742 (info->oob_size ? info->spare_size + info->ecc_size : 0);
743 dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
eric miaofe69af02008-02-14 15:48:23 +0800744
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200745 tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
746 DMA_PREP_INTERRUPT);
747 if (!tx) {
748 dev_err(&info->pdev->dev, "prep_slave_sg() failed\n");
749 return;
eric miaofe69af02008-02-14 15:48:23 +0800750 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200751 tx->callback = pxa3xx_nand_data_dma_irq;
752 tx->callback_param = info;
753 info->dma_cookie = dmaengine_submit(tx);
754 dma_async_issue_pending(info->dma_chan);
755 dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n",
756 __func__, direction, info->dma_cookie, info->sg.length);
eric miaofe69af02008-02-14 15:48:23 +0800757}
758
Robert Jarzmik24542252015-02-20 19:36:43 +0100759static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
760{
761 struct pxa3xx_nand_info *info = data;
762
763 handle_data_pio(info);
764
765 info->state = STATE_CMD_DONE;
766 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
767
768 return IRQ_HANDLED;
769}
770
eric miaofe69af02008-02-14 15:48:23 +0800771static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
772{
773 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300774 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700775 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100776 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700777
778 if (info->cs == 0) {
779 ready = NDSR_FLASH_RDY;
780 cmd_done = NDSR_CS0_CMDD;
781 } else {
782 ready = NDSR_RDY;
783 cmd_done = NDSR_CS1_CMDD;
784 }
eric miaofe69af02008-02-14 15:48:23 +0800785
786 status = nand_readl(info, NDSR);
787
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300788 if (status & NDSR_UNCORERR)
789 info->retcode = ERR_UNCORERR;
790 if (status & NDSR_CORERR) {
791 info->retcode = ERR_CORERR;
792 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
793 info->ecc_bch)
794 info->ecc_err_cnt = NDSR_ERR_CNT(status);
795 else
796 info->ecc_err_cnt = 1;
797
798 /*
799 * Each chunk composing a page is corrected independently,
800 * and we need to store maximum number of corrected bitflips
801 * to return it to the MTD layer in ecc.read_page().
802 */
803 info->max_bitflips = max_t(unsigned int,
804 info->max_bitflips,
805 info->ecc_err_cnt);
806 }
Lei Wenf8155a42011-02-28 10:32:11 +0800807 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
808 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800809 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800810 disable_int(info, NDCR_INT_MASK);
811 info->state = (status & NDSR_RDDREQ) ?
812 STATE_DMA_READING : STATE_DMA_WRITING;
813 start_data_dma(info);
814 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800815 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800816 info->state = (status & NDSR_RDDREQ) ?
817 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100818 ret = IRQ_WAKE_THREAD;
819 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800820 }
Lei Wenf8155a42011-02-28 10:32:11 +0800821 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700822 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800823 info->state = STATE_CMD_DONE;
824 is_completed = 1;
825 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700826 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800827 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300828 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800829 }
Lei Wenf8155a42011-02-28 10:32:11 +0800830
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200831 /*
832 * Clear all status bit before issuing the next command, which
833 * can and will alter the status bits and will deserve a new
834 * interrupt on its own. This lets the controller exit the IRQ
835 */
836 nand_writel(info, NDSR, status);
837
Lei Wenf8155a42011-02-28 10:32:11 +0800838 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800839 status &= ~NDSR_WRCMDREQ;
840 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300841
842 /*
843 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
844 * must be loaded by writing directly either 12 or 16
845 * bytes directly to NDCB0, four bytes at a time.
846 *
847 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
848 * but each NDCBx register can be read.
849 */
Lei Wenf8155a42011-02-28 10:32:11 +0800850 nand_writel(info, NDCB0, info->ndcb0);
851 nand_writel(info, NDCB0, info->ndcb1);
852 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300853
854 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
855 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
856 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800857 }
Lei Wenf8155a42011-02-28 10:32:11 +0800858
Lei Wenf8155a42011-02-28 10:32:11 +0800859 if (is_completed)
860 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300861 if (is_ready)
862 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800863NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100864 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800865}
866
eric miaofe69af02008-02-14 15:48:23 +0800867static inline int is_buf_blank(uint8_t *buf, size_t len)
868{
869 for (; len > 0; len--)
870 if (*buf++ != 0xff)
871 return 0;
872 return 1;
873}
874
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300875static void set_command_address(struct pxa3xx_nand_info *info,
876 unsigned int page_size, uint16_t column, int page_addr)
877{
878 /* small page addr setting */
879 if (page_size < PAGE_CHUNK_SIZE) {
880 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
881 | (column & 0xFF);
882
883 info->ndcb2 = 0;
884 } else {
885 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
886 | (column & 0xFFFF);
887
888 if (page_addr & 0xFF0000)
889 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
890 else
891 info->ndcb2 = 0;
892 }
893}
894
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300895static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800896{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300897 struct pxa3xx_nand_host *host = info->host[info->cs];
898 struct mtd_info *mtd = host->mtd;
899
Lei Wen4eb2da82011-02-28 10:32:13 +0800900 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800901 info->buf_start = 0;
902 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800903 info->oob_size = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300904 info->data_buff_pos = 0;
905 info->oob_buff_pos = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800906 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300907 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800908 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300909 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300910 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300911 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800912
913 switch (command) {
914 case NAND_CMD_READ0:
915 case NAND_CMD_PAGEPROG:
916 info->use_ecc = 1;
917 case NAND_CMD_READOOB:
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300918 pxa3xx_set_datasize(info, mtd);
Lei Wen4eb2da82011-02-28 10:32:13 +0800919 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300920 case NAND_CMD_PARAM:
921 info->use_spare = 0;
922 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800923 default:
924 info->ndcb1 = 0;
925 info->ndcb2 = 0;
926 break;
927 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300928
929 /*
930 * If we are about to issue a read command, or about to set
931 * the write address, then clean the data buffer.
932 */
933 if (command == NAND_CMD_READ0 ||
934 command == NAND_CMD_READOOB ||
935 command == NAND_CMD_SEQIN) {
936
937 info->buf_count = mtd->writesize + mtd->oobsize;
938 memset(info->data_buff, 0xFF, info->buf_count);
939 }
940
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300941}
942
943static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300944 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300945{
946 int addr_cycle, exec_cmd;
947 struct pxa3xx_nand_host *host;
948 struct mtd_info *mtd;
949
950 host = info->host[info->cs];
951 mtd = host->mtd;
952 addr_cycle = 0;
953 exec_cmd = 1;
954
955 if (info->cs != 0)
956 info->ndcb0 = NDCB0_CSEL;
957 else
958 info->ndcb0 = 0;
959
960 if (command == NAND_CMD_SEQIN)
961 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800962
Lei Wend4568822011-07-14 20:44:32 -0700963 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
964 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800965
966 switch (command) {
967 case NAND_CMD_READOOB:
968 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300969 info->buf_start = column;
970 info->ndcb0 |= NDCB0_CMD_TYPE(0)
971 | addr_cycle
972 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800973
Ezequiel Garciaec821352013-08-12 14:14:54 -0300974 if (command == NAND_CMD_READOOB)
975 info->buf_start += mtd->writesize;
976
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300977 /*
978 * Multiple page read needs an 'extended command type' field,
979 * which is either naked-read or last-read according to the
980 * state.
981 */
982 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -0300983 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300984 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
985 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
986 | NDCB0_LEN_OVRD
987 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
988 info->ndcb3 = info->chunk_size +
989 info->oob_size;
990 }
Lei Wen4eb2da82011-02-28 10:32:13 +0800991
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300992 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300993 break;
994
Lei Wen4eb2da82011-02-28 10:32:13 +0800995 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800996
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300997 info->buf_start = column;
998 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -0300999
1000 /*
1001 * Multiple page programming needs to execute the initial
1002 * SEQIN command that sets the page address.
1003 */
1004 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1005 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1006 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1007 | addr_cycle
1008 | command;
1009 /* No data transfer in this case */
1010 info->data_size = 0;
1011 exec_cmd = 1;
1012 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001013 break;
1014
1015 case NAND_CMD_PAGEPROG:
1016 if (is_buf_blank(info->data_buff,
1017 (mtd->writesize + mtd->oobsize))) {
1018 exec_cmd = 0;
1019 break;
1020 }
1021
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001022 /* Second command setting for large pages */
1023 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1024 /*
1025 * Multiple page write uses the 'extended command'
1026 * field. This can be used to issue a command dispatch
1027 * or a naked-write depending on the current stage.
1028 */
1029 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1030 | NDCB0_LEN_OVRD
1031 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
1032 info->ndcb3 = info->chunk_size +
1033 info->oob_size;
1034
1035 /*
1036 * This is the command dispatch that completes a chunked
1037 * page program operation.
1038 */
1039 if (info->data_size == 0) {
1040 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
1041 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1042 | command;
1043 info->ndcb1 = 0;
1044 info->ndcb2 = 0;
1045 info->ndcb3 = 0;
1046 }
1047 } else {
1048 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1049 | NDCB0_AUTO_RS
1050 | NDCB0_ST_ROW_EN
1051 | NDCB0_DBC
1052 | (NAND_CMD_PAGEPROG << 8)
1053 | NAND_CMD_SEQIN
1054 | addr_cycle;
1055 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001056 break;
1057
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001058 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -03001059 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001060 info->ndcb0 |= NDCB0_CMD_TYPE(0)
1061 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -03001062 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -03001063 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001064 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -03001065 info->ndcb3 = INIT_BUFFER_SIZE;
1066 info->data_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001067 break;
1068
Lei Wen4eb2da82011-02-28 10:32:13 +08001069 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001070 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +08001071 info->ndcb0 |= NDCB0_CMD_TYPE(3)
1072 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001073 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -03001074 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +08001075
1076 info->data_size = 8;
1077 break;
1078 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +08001079 info->buf_count = 1;
1080 info->ndcb0 |= NDCB0_CMD_TYPE(4)
1081 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001082 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001083
1084 info->data_size = 8;
1085 break;
1086
1087 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +08001088 info->ndcb0 |= NDCB0_CMD_TYPE(2)
1089 | NDCB0_AUTO_RS
1090 | NDCB0_ADDR_CYC(3)
1091 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -03001092 | (NAND_CMD_ERASE2 << 8)
1093 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +08001094 info->ndcb1 = page_addr;
1095 info->ndcb2 = 0;
1096
1097 break;
1098 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +08001099 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001100 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001101
1102 break;
1103
1104 case NAND_CMD_ERASE2:
1105 exec_cmd = 0;
1106 break;
1107
1108 default:
1109 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -07001110 dev_err(&info->pdev->dev, "non-supported command %x\n",
1111 command);
Lei Wen4eb2da82011-02-28 10:32:13 +08001112 break;
1113 }
1114
1115 return exec_cmd;
1116}
1117
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001118static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1119 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +08001120{
Lei Wend4568822011-07-14 20:44:32 -07001121 struct pxa3xx_nand_host *host = mtd->priv;
1122 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001123 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +08001124
Lei Wen4eb2da82011-02-28 10:32:13 +08001125 /*
1126 * if this is a x16 device ,then convert the input
1127 * "byte" address into a "word" address appropriate
1128 * for indexing a word-oriented device
1129 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001130 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +08001131 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +08001132
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001133 /*
1134 * There may be different NAND chip hooked to
1135 * different chip select, so check whether
1136 * chip select has been changed, if yes, reset the timing
1137 */
1138 if (info->cs != host->cs) {
1139 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001140 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1141 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001142 }
1143
Ezequiel Garciac39ff032013-11-14 18:25:33 -03001144 prepare_start_command(info, command);
1145
Lei Wend4568822011-07-14 20:44:32 -07001146 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001147 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1148
Lei Wenf8155a42011-02-28 10:32:11 +08001149 if (exec_cmd) {
1150 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001151 init_completion(&info->dev_ready);
1152 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001153 pxa3xx_nand_start(info);
1154
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001155 if (!wait_for_completion_timeout(&info->cmd_complete,
1156 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001157 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001158 /* Stop State Machine for next command cycle */
1159 pxa3xx_nand_stop(info);
1160 }
eric miaofe69af02008-02-14 15:48:23 +08001161 }
Lei Wend4568822011-07-14 20:44:32 -07001162 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001163}
1164
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001165static void nand_cmdfunc_extended(struct mtd_info *mtd,
1166 const unsigned command,
1167 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001168{
1169 struct pxa3xx_nand_host *host = mtd->priv;
1170 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001171 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001172
1173 /*
1174 * if this is a x16 device then convert the input
1175 * "byte" address into a "word" address appropriate
1176 * for indexing a word-oriented device
1177 */
1178 if (info->reg_ndcr & NDCR_DWIDTH_M)
1179 column /= 2;
1180
1181 /*
1182 * There may be different NAND chip hooked to
1183 * different chip select, so check whether
1184 * chip select has been changed, if yes, reset the timing
1185 */
1186 if (info->cs != host->cs) {
1187 info->cs = host->cs;
1188 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1189 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1190 }
1191
1192 /* Select the extended command for the first command */
1193 switch (command) {
1194 case NAND_CMD_READ0:
1195 case NAND_CMD_READOOB:
1196 ext_cmd_type = EXT_CMD_TYPE_MONO;
1197 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001198 case NAND_CMD_SEQIN:
1199 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1200 break;
1201 case NAND_CMD_PAGEPROG:
1202 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1203 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001204 default:
1205 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001206 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001207 }
1208
1209 prepare_start_command(info, command);
1210
1211 /*
1212 * Prepare the "is ready" completion before starting a command
1213 * transaction sequence. If the command is not executed the
1214 * completion will be completed, see below.
1215 *
1216 * We can do that inside the loop because the command variable
1217 * is invariant and thus so is the exec_cmd.
1218 */
1219 info->need_wait = 1;
1220 init_completion(&info->dev_ready);
1221 do {
1222 info->state = STATE_PREPARED;
1223 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1224 column, page_addr);
1225 if (!exec_cmd) {
1226 info->need_wait = 0;
1227 complete(&info->dev_ready);
1228 break;
1229 }
1230
1231 init_completion(&info->cmd_complete);
1232 pxa3xx_nand_start(info);
1233
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001234 if (!wait_for_completion_timeout(&info->cmd_complete,
1235 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001236 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1237 /* Stop State Machine for next command cycle */
1238 pxa3xx_nand_stop(info);
1239 break;
1240 }
1241
1242 /* Check if the sequence is complete */
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001243 if (info->data_size == 0 && command != NAND_CMD_PAGEPROG)
1244 break;
1245
1246 /*
1247 * After a splitted program command sequence has issued
1248 * the command dispatch, the command sequence is complete.
1249 */
1250 if (info->data_size == 0 &&
1251 command == NAND_CMD_PAGEPROG &&
1252 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001253 break;
1254
1255 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1256 /* Last read: issue a 'last naked read' */
1257 if (info->data_size == info->chunk_size)
1258 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1259 else
1260 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001261
1262 /*
1263 * If a splitted program command has no more data to transfer,
1264 * the command dispatch must be issued to complete.
1265 */
1266 } else if (command == NAND_CMD_PAGEPROG &&
1267 info->data_size == 0) {
1268 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001269 }
1270 } while (1);
1271
1272 info->state = STATE_IDLE;
1273}
1274
Josh Wufdbad98d2012-06-25 18:07:45 +08001275static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001276 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1277 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001278{
1279 chip->write_buf(mtd, buf, mtd->writesize);
1280 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001281
1282 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001283}
1284
1285static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001286 struct nand_chip *chip, uint8_t *buf, int oob_required,
1287 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001288{
Lei Wend4568822011-07-14 20:44:32 -07001289 struct pxa3xx_nand_host *host = mtd->priv;
1290 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001291
1292 chip->read_buf(mtd, buf, mtd->writesize);
1293 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1294
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001295 if (info->retcode == ERR_CORERR && info->use_ecc) {
1296 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1297
1298 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001299 /*
1300 * for blank page (all 0xff), HW will calculate its ECC as
1301 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001302 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001303 */
1304 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001305 info->retcode = ERR_NONE;
1306 else
Lei Wenf8155a42011-02-28 10:32:11 +08001307 mtd->ecc_stats.failed++;
1308 }
1309
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001310 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001311}
1312
eric miaofe69af02008-02-14 15:48:23 +08001313static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1314{
Lei Wend4568822011-07-14 20:44:32 -07001315 struct pxa3xx_nand_host *host = mtd->priv;
1316 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001317 char retval = 0xFF;
1318
1319 if (info->buf_start < info->buf_count)
1320 /* Has just send a new command? */
1321 retval = info->data_buff[info->buf_start++];
1322
1323 return retval;
1324}
1325
1326static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1327{
Lei Wend4568822011-07-14 20:44:32 -07001328 struct pxa3xx_nand_host *host = mtd->priv;
1329 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001330 u16 retval = 0xFFFF;
1331
1332 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1333 retval = *((u16 *)(info->data_buff+info->buf_start));
1334 info->buf_start += 2;
1335 }
1336 return retval;
1337}
1338
1339static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1340{
Lei Wend4568822011-07-14 20:44:32 -07001341 struct pxa3xx_nand_host *host = mtd->priv;
1342 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001343 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1344
1345 memcpy(buf, info->data_buff + info->buf_start, real_len);
1346 info->buf_start += real_len;
1347}
1348
1349static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1350 const uint8_t *buf, int len)
1351{
Lei Wend4568822011-07-14 20:44:32 -07001352 struct pxa3xx_nand_host *host = mtd->priv;
1353 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001354 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1355
1356 memcpy(info->data_buff + info->buf_start, buf, real_len);
1357 info->buf_start += real_len;
1358}
1359
eric miaofe69af02008-02-14 15:48:23 +08001360static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1361{
1362 return;
1363}
1364
1365static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1366{
Lei Wend4568822011-07-14 20:44:32 -07001367 struct pxa3xx_nand_host *host = mtd->priv;
1368 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001369
1370 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001371 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001372 if (!wait_for_completion_timeout(&info->dev_ready,
1373 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001374 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1375 return NAND_STATUS_FAIL;
1376 }
1377 }
eric miaofe69af02008-02-14 15:48:23 +08001378
1379 /* pxa3xx_nand_send_command has waited for command complete */
1380 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1381 if (info->retcode == ERR_NONE)
1382 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001383 else
1384 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001385 }
1386
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001387 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001388}
1389
Ezequiel García66e8e472015-11-04 13:13:42 -03001390static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001391{
1392 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001393 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001394
Ezequiel García66e8e472015-11-04 13:13:42 -03001395 /* Configure default flash values */
1396 info->chunk_size = PAGE_CHUNK_SIZE;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001397 info->reg_ndcr = 0x0; /* enable all interrupts */
1398 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
1399 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
Ezequiel García66e8e472015-11-04 13:13:42 -03001400 info->reg_ndcr |= NDCR_SPARE_EN;
eric miaofe69af02008-02-14 15:48:23 +08001401
eric miaofe69af02008-02-14 15:48:23 +08001402 return 0;
1403}
1404
Ezequiel García66e8e472015-11-04 13:13:42 -03001405static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
1406{
1407 struct pxa3xx_nand_host *host = info->host[info->cs];
1408 struct mtd_info *mtd = host->mtd;
1409 struct nand_chip *chip = mtd->priv;
1410
1411 info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
1412 info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
1413 info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
1414}
1415
Mike Rapoportf2710492009-02-17 13:54:47 +02001416static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
1417{
Ezequiel García66e8e472015-11-04 13:13:42 -03001418 struct platform_device *pdev = info->pdev;
1419 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Mike Rapoportf2710492009-02-17 13:54:47 +02001420 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001421
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001422 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001423 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Robert Jarzmike971aff2015-09-28 22:56:51 +02001424 info->reg_ndcr = ndcr &
1425 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
Ezequiel García66e8e472015-11-04 13:13:42 -03001426 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001427 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1428 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001429 return 0;
1430}
1431
eric miaofe69af02008-02-14 15:48:23 +08001432static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1433{
1434 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001435 struct dma_slave_config config;
1436 dma_cap_mask_t mask;
1437 struct pxad_param param;
1438 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001439
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001440 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1441 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001442 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001443 if (use_dma == 0)
1444 return 0;
1445
1446 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1447 if (ret)
1448 return ret;
1449
1450 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1451 dma_cap_zero(mask);
1452 dma_cap_set(DMA_SLAVE, mask);
1453 param.prio = PXAD_PRIO_LOWEST;
1454 param.drcmr = info->drcmr_dat;
1455 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1456 &param, &pdev->dev,
1457 "data");
1458 if (!info->dma_chan) {
1459 dev_err(&pdev->dev, "unable to request data dma channel\n");
1460 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001461 }
1462
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001463 memset(&config, 0, sizeof(config));
1464 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1465 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1466 config.src_addr = info->mmio_phys + NDDB;
1467 config.dst_addr = info->mmio_phys + NDDB;
1468 config.src_maxburst = 32;
1469 config.dst_maxburst = 32;
1470 ret = dmaengine_slave_config(info->dma_chan, &config);
1471 if (ret < 0) {
1472 dev_err(&info->pdev->dev,
1473 "dma channel configuration failed: %d\n",
1474 ret);
1475 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001476 }
1477
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001478 /*
1479 * Now that DMA buffers are allocated we turn on
1480 * DMA proper for I/O operations.
1481 */
1482 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001483 return 0;
1484}
1485
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001486static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1487{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001488 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001489 dmaengine_terminate_all(info->dma_chan);
1490 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001491 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001492 kfree(info->data_buff);
1493}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001494
Antoine Ténartf19fe982015-10-21 10:29:03 +02001495static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host)
eric miaofe69af02008-02-14 15:48:23 +08001496{
Antoine Ténartf19fe982015-10-21 10:29:03 +02001497 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001498 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001499 struct nand_chip *chip;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001500 const struct nand_sdr_timings *timings;
Lei Wend4568822011-07-14 20:44:32 -07001501 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001502
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001503 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001504 chip = mtd->priv;
1505
Lei Wen401e67e2011-02-28 10:32:14 +08001506 /* use the common timing to make a try */
Antoine Ténartf19fe982015-10-21 10:29:03 +02001507 timings = onfi_async_timing_mode_to_sdr_timings(0);
1508 if (IS_ERR(timings))
1509 return PTR_ERR(timings);
1510
1511 pxa3xx_nand_set_sdr_timing(host, timings);
Lei Wend4568822011-07-14 20:44:32 -07001512
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001513 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001514 ret = chip->waitfunc(mtd, chip);
1515 if (ret & NAND_STATUS_FAIL)
1516 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001517
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001518 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001519}
eric miaofe69af02008-02-14 15:48:23 +08001520
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001521static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1522 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001523 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001524{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001525 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001526 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001527 info->spare_size = 40;
1528 info->ecc_size = 24;
1529 ecc->mode = NAND_ECC_HW;
1530 ecc->size = 512;
1531 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001532
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001533 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001534 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001535 info->spare_size = 8;
1536 info->ecc_size = 8;
1537 ecc->mode = NAND_ECC_HW;
1538 ecc->size = 512;
1539 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001540
Brian Norris6033a942013-11-14 14:41:32 -08001541 /*
1542 * Required ECC: 4-bit correction per 512 bytes
1543 * Select: 16-bit correction per 2048 bytes
1544 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001545 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1546 info->ecc_bch = 1;
1547 info->chunk_size = 2048;
1548 info->spare_size = 32;
1549 info->ecc_size = 32;
1550 ecc->mode = NAND_ECC_HW;
1551 ecc->size = info->chunk_size;
1552 ecc->layout = &ecc_layout_2KB_bch4bit;
1553 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001554
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001555 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001556 info->ecc_bch = 1;
1557 info->chunk_size = 2048;
1558 info->spare_size = 32;
1559 info->ecc_size = 32;
1560 ecc->mode = NAND_ECC_HW;
1561 ecc->size = info->chunk_size;
1562 ecc->layout = &ecc_layout_4KB_bch4bit;
1563 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001564
Brian Norris6033a942013-11-14 14:41:32 -08001565 /*
1566 * Required ECC: 8-bit correction per 512 bytes
1567 * Select: 16-bit correction per 1024 bytes
1568 */
1569 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001570 info->ecc_bch = 1;
1571 info->chunk_size = 1024;
1572 info->spare_size = 0;
1573 info->ecc_size = 32;
1574 ecc->mode = NAND_ECC_HW;
1575 ecc->size = info->chunk_size;
1576 ecc->layout = &ecc_layout_4KB_bch8bit;
1577 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001578 } else {
1579 dev_err(&info->pdev->dev,
1580 "ECC strength %d at page size %d is not supported\n",
1581 strength, page_size);
1582 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001583 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001584
1585 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1586 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001587 return 0;
1588}
1589
Lei Wen401e67e2011-02-28 10:32:14 +08001590static int pxa3xx_nand_scan(struct mtd_info *mtd)
1591{
Lei Wend4568822011-07-14 20:44:32 -07001592 struct pxa3xx_nand_host *host = mtd->priv;
1593 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001594 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001595 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen401e67e2011-02-28 10:32:14 +08001596 struct nand_chip *chip = mtd->priv;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001597 int ret;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001598 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001599
1600 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001601 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001602
Ezequiel García66e8e472015-11-04 13:13:42 -03001603 ret = pxa3xx_nand_config_ident(info);
Antoine Ténartf19fe982015-10-21 10:29:03 +02001604 if (ret)
1605 return ret;
1606
1607 ret = pxa3xx_nand_sensing(host);
Lei Wend4568822011-07-14 20:44:32 -07001608 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001609 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1610 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001611
Lei Wend4568822011-07-14 20:44:32 -07001612 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001613 }
1614
Lei Wen4332c112011-03-03 11:27:01 +08001615KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001616 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001617 chip->options |= NAND_BUSWIDTH_16;
1618
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001619 /* Device detection must be done with ECC disabled */
1620 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1621 nand_writel(info, NDECCCTRL, 0x0);
1622
Antoine Ténartf19fe982015-10-21 10:29:03 +02001623 if (nand_scan_ident(mtd, 1, NULL))
Lei Wen4332c112011-03-03 11:27:01 +08001624 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001625
Antoine Ténartf19fe982015-10-21 10:29:03 +02001626 if (!pdata->keep_config) {
1627 ret = pxa3xx_nand_init(host);
1628 if (ret) {
1629 dev_err(&info->pdev->dev, "Failed to init nand: %d\n",
1630 ret);
1631 return ret;
1632 }
1633 }
1634
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001635 if (pdata->flash_bbt) {
1636 /*
1637 * We'll use a bad block table stored in-flash and don't
1638 * allow writing the bad block marker to the flash.
1639 */
1640 chip->bbt_options |= NAND_BBT_USE_FLASH |
1641 NAND_BBT_NO_OOB_BBM;
1642 chip->bbt_td = &bbt_main_descr;
1643 chip->bbt_md = &bbt_mirror_descr;
1644 }
1645
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001646 /*
1647 * If the page size is bigger than the FIFO size, let's check
1648 * we are given the right variant and then switch to the extended
1649 * (aka splitted) command handling,
1650 */
1651 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1652 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1653 chip->cmdfunc = nand_cmdfunc_extended;
1654 } else {
1655 dev_err(&info->pdev->dev,
1656 "unsupported page size on this variant\n");
1657 return -ENODEV;
1658 }
1659 }
1660
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001661 if (pdata->ecc_strength && pdata->ecc_step_size) {
1662 ecc_strength = pdata->ecc_strength;
1663 ecc_step = pdata->ecc_step_size;
1664 } else {
1665 ecc_strength = chip->ecc_strength_ds;
1666 ecc_step = chip->ecc_step_ds;
1667 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001668
1669 /* Set default ECC strength requirements on non-ONFI devices */
1670 if (ecc_strength < 1 && ecc_step < 1) {
1671 ecc_strength = 1;
1672 ecc_step = 512;
1673 }
1674
1675 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1676 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001677 if (ret)
1678 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001679
Lei Wen4332c112011-03-03 11:27:01 +08001680 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001681 if (mtd->writesize >= 2048)
1682 host->col_addr_cycles = 2;
1683 else
1684 host->col_addr_cycles = 1;
1685
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001686 /* release the initial buffer */
1687 kfree(info->data_buff);
1688
1689 /* allocate the real data + oob buffer */
1690 info->buf_size = mtd->writesize + mtd->oobsize;
1691 ret = pxa3xx_nand_init_buff(info);
1692 if (ret)
1693 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001694 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001695
Lei Wen4332c112011-03-03 11:27:01 +08001696 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001697 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001698 else
Lei Wend4568822011-07-14 20:44:32 -07001699 host->row_addr_cycles = 2;
Ezequiel García66e8e472015-11-04 13:13:42 -03001700
1701 if (!pdata->keep_config)
1702 pxa3xx_nand_config_tail(info);
1703
Lei Wen401e67e2011-02-28 10:32:14 +08001704 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001705}
1706
Lei Wend4568822011-07-14 20:44:32 -07001707static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001708{
Brian Norrisa61ae812015-10-30 20:33:25 -07001709 struct device_node *np = pdev->dev.of_node;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001710 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001711 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001712 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001713 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001714 struct mtd_info *mtd;
1715 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001716 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001717
Jingoo Han453810b2013-07-30 17:18:33 +09001718 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001719 if (pdata->num_cs <= 0)
1720 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001721 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1722 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1723 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001724 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001725
eric miaofe69af02008-02-14 15:48:23 +08001726 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001727 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001728 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001729 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001730 chip = (struct nand_chip *)(&mtd[1]);
1731 host = (struct pxa3xx_nand_host *)chip;
1732 info->host[cs] = host;
1733 host->mtd = mtd;
1734 host->cs = cs;
1735 host->info_data = info;
1736 mtd->priv = host;
Frans Klaver550dab52015-06-10 22:39:01 +02001737 mtd->dev.parent = &pdev->dev;
Brian Norrisa61ae812015-10-30 20:33:25 -07001738 /* FIXME: all chips use the same device tree partitions */
1739 nand_set_flash_node(chip, np);
eric miaofe69af02008-02-14 15:48:23 +08001740
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001741 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1742 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1743 chip->controller = &info->controller;
1744 chip->waitfunc = pxa3xx_nand_waitfunc;
1745 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001746 chip->read_word = pxa3xx_nand_read_word;
1747 chip->read_byte = pxa3xx_nand_read_byte;
1748 chip->read_buf = pxa3xx_nand_read_buf;
1749 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001750 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001751 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001752 }
Lei Wen401e67e2011-02-28 10:32:14 +08001753
1754 spin_lock_init(&chip->controller->lock);
1755 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001756 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001757 if (IS_ERR(info->clk)) {
1758 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001759 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001760 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001761 ret = clk_prepare_enable(info->clk);
1762 if (ret < 0)
1763 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001764
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001765 if (use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001766 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1767 if (r == NULL) {
1768 dev_err(&pdev->dev,
1769 "no resource defined for data DMA\n");
1770 ret = -ENXIO;
1771 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001772 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001773 info->drcmr_dat = r->start;
1774
1775 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1776 if (r == NULL) {
1777 dev_err(&pdev->dev,
1778 "no resource defined for cmd DMA\n");
1779 ret = -ENXIO;
1780 goto fail_disable_clk;
1781 }
1782 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001783 }
eric miaofe69af02008-02-14 15:48:23 +08001784
1785 irq = platform_get_irq(pdev, 0);
1786 if (irq < 0) {
1787 dev_err(&pdev->dev, "no IRQ resource defined\n");
1788 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001789 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001790 }
1791
1792 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001793 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1794 if (IS_ERR(info->mmio_base)) {
1795 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001796 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001797 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001798 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001799
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001800 /* Allocate a buffer to allow flash detection */
1801 info->buf_size = INIT_BUFFER_SIZE;
1802 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1803 if (info->data_buff == NULL) {
1804 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001805 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001806 }
eric miaofe69af02008-02-14 15:48:23 +08001807
Haojian Zhuang346e1252009-09-10 14:27:23 +08001808 /* initialize all interrupts to be disabled */
1809 disable_int(info, NDSR_MASK);
1810
Robert Jarzmik24542252015-02-20 19:36:43 +01001811 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1812 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1813 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001814 if (ret < 0) {
1815 dev_err(&pdev->dev, "failed to request IRQ\n");
1816 goto fail_free_buf;
1817 }
1818
Lei Wene353a202011-03-03 11:08:30 +08001819 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001820
Lei Wend4568822011-07-14 20:44:32 -07001821 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001822
eric miaofe69af02008-02-14 15:48:23 +08001823fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001824 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001825 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001826fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001827 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001828 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001829}
1830
1831static int pxa3xx_nand_remove(struct platform_device *pdev)
1832{
Lei Wene353a202011-03-03 11:08:30 +08001833 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001834 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001835 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001836
Lei Wend4568822011-07-14 20:44:32 -07001837 if (!info)
1838 return 0;
1839
Jingoo Han453810b2013-07-30 17:18:33 +09001840 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001841
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001842 irq = platform_get_irq(pdev, 0);
1843 if (irq >= 0)
1844 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001845 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001846
Robert Jarzmike971aff2015-09-28 22:56:51 +02001847 /*
1848 * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
1849 * In order to prevent a lockup of the system bus, the DFI bus
1850 * arbitration is granted to SMC upon driver removal. This is done by
1851 * setting the x_ARB_CNTL bit, which also prevents the NAND to have
1852 * access to the bus anymore.
1853 */
1854 nand_writel(info, NDCR,
1855 (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
1856 NFCV1_NDCR_ARB_CNTL);
Ezequiel Garciafb320612013-04-17 13:38:12 -03001857 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001858
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001859 for (cs = 0; cs < pdata->num_cs; cs++)
1860 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001861 return 0;
1862}
1863
Daniel Mack1e7ba632012-07-22 19:51:02 +02001864static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1865{
1866 struct pxa3xx_nand_platform_data *pdata;
1867 struct device_node *np = pdev->dev.of_node;
1868 const struct of_device_id *of_id =
1869 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1870
1871 if (!of_id)
1872 return 0;
1873
1874 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1875 if (!pdata)
1876 return -ENOMEM;
1877
1878 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1879 pdata->enable_arbiter = 1;
1880 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1881 pdata->keep_config = 1;
1882 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001883 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001884
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001885 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1886 if (pdata->ecc_strength < 0)
1887 pdata->ecc_strength = 0;
1888
1889 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1890 if (pdata->ecc_step_size < 0)
1891 pdata->ecc_step_size = 0;
1892
Daniel Mack1e7ba632012-07-22 19:51:02 +02001893 pdev->dev.platform_data = pdata;
1894
1895 return 0;
1896}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001897
Lei Wene353a202011-03-03 11:08:30 +08001898static int pxa3xx_nand_probe(struct platform_device *pdev)
1899{
1900 struct pxa3xx_nand_platform_data *pdata;
1901 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001902 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001903
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001904 dma_available = IS_ENABLED(CONFIG_ARM) &&
1905 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1906 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001907 use_dma = 0;
1908 dev_warn(&pdev->dev,
1909 "This platform can't do DMA on this device\n");
1910 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001911
Daniel Mack1e7ba632012-07-22 19:51:02 +02001912 ret = pxa3xx_nand_probe_dt(pdev);
1913 if (ret)
1914 return ret;
1915
Jingoo Han453810b2013-07-30 17:18:33 +09001916 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001917 if (!pdata) {
1918 dev_err(&pdev->dev, "no platform data defined\n");
1919 return -ENODEV;
1920 }
1921
Lei Wend4568822011-07-14 20:44:32 -07001922 ret = alloc_nand_resource(pdev);
1923 if (ret) {
1924 dev_err(&pdev->dev, "alloc nand resource failed\n");
1925 return ret;
1926 }
Lei Wene353a202011-03-03 11:08:30 +08001927
Lei Wend4568822011-07-14 20:44:32 -07001928 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001929 probe_success = 0;
1930 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001931 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001932
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001933 /*
1934 * The mtd name matches the one used in 'mtdparts' kernel
1935 * parameter. This name cannot be changed or otherwise
1936 * user's mtd partitions configuration would get broken.
1937 */
1938 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001939 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001940 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001941 if (ret) {
1942 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1943 cs);
1944 continue;
1945 }
1946
Brian Norrisa61ae812015-10-30 20:33:25 -07001947 ret = mtd_device_register(mtd, pdata->parts[cs],
1948 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001949 if (!ret)
1950 probe_success = 1;
1951 }
1952
1953 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001954 pxa3xx_nand_remove(pdev);
1955 return -ENODEV;
1956 }
1957
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001958 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001959}
1960
eric miaofe69af02008-02-14 15:48:23 +08001961#ifdef CONFIG_PM
Brian Norrisd3e94f32015-10-12 14:07:41 -07001962static int pxa3xx_nand_suspend(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08001963{
Brian Norrisd3e94f32015-10-12 14:07:41 -07001964 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
eric miaofe69af02008-02-14 15:48:23 +08001965
Lei Wenf8155a42011-02-28 10:32:11 +08001966 if (info->state) {
Brian Norrisd3e94f32015-10-12 14:07:41 -07001967 dev_err(dev, "driver busy, state = %d\n", info->state);
eric miaofe69af02008-02-14 15:48:23 +08001968 return -EAGAIN;
1969 }
1970
1971 return 0;
1972}
1973
Brian Norrisd3e94f32015-10-12 14:07:41 -07001974static int pxa3xx_nand_resume(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08001975{
Brian Norrisd3e94f32015-10-12 14:07:41 -07001976 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
Lei Wen051fc412011-07-14 20:44:30 -07001977
1978 /* We don't want to handle interrupt without calling mtd routine */
1979 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001980
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001981 /*
1982 * Directly set the chip select to a invalid value,
1983 * then the driver would reset the timing according
1984 * to current chip select at the beginning of cmdfunc
1985 */
1986 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001987
Lei Wen051fc412011-07-14 20:44:30 -07001988 /*
1989 * As the spec says, the NDSR would be updated to 0x1800 when
1990 * doing the nand_clk disable/enable.
1991 * To prevent it damaging state machine of the driver, clear
1992 * all status before resume
1993 */
1994 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001995
Lei Wen18c81b12010-08-17 17:25:57 +08001996 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001997}
1998#else
1999#define pxa3xx_nand_suspend NULL
2000#define pxa3xx_nand_resume NULL
2001#endif
2002
Brian Norrisd3e94f32015-10-12 14:07:41 -07002003static const struct dev_pm_ops pxa3xx_nand_pm_ops = {
2004 .suspend = pxa3xx_nand_suspend,
2005 .resume = pxa3xx_nand_resume,
2006};
2007
eric miaofe69af02008-02-14 15:48:23 +08002008static struct platform_driver pxa3xx_nand_driver = {
2009 .driver = {
2010 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05302011 .of_match_table = pxa3xx_nand_dt_ids,
Brian Norrisd3e94f32015-10-12 14:07:41 -07002012 .pm = &pxa3xx_nand_pm_ops,
eric miaofe69af02008-02-14 15:48:23 +08002013 },
2014 .probe = pxa3xx_nand_probe,
2015 .remove = pxa3xx_nand_remove,
eric miaofe69af02008-02-14 15:48:23 +08002016};
2017
Axel Linf99640d2011-11-27 20:45:03 +08002018module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08002019
2020MODULE_LICENSE("GPL");
2021MODULE_DESCRIPTION("PXA3xx NAND controller driver");