blob: 002f1cc75543a4ebf2e10de6c51760fc753a04a1 [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
Ezequiel García154f50f2015-11-04 13:13:43 -03001416static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
Mike Rapoportf2710492009-02-17 13:54:47 +02001417{
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}
1430
eric miaofe69af02008-02-14 15:48:23 +08001431static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1432{
1433 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001434 struct dma_slave_config config;
1435 dma_cap_mask_t mask;
1436 struct pxad_param param;
1437 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001438
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001439 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1440 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001441 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001442 if (use_dma == 0)
1443 return 0;
1444
1445 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1446 if (ret)
1447 return ret;
1448
1449 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1450 dma_cap_zero(mask);
1451 dma_cap_set(DMA_SLAVE, mask);
1452 param.prio = PXAD_PRIO_LOWEST;
1453 param.drcmr = info->drcmr_dat;
1454 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1455 &param, &pdev->dev,
1456 "data");
1457 if (!info->dma_chan) {
1458 dev_err(&pdev->dev, "unable to request data dma channel\n");
1459 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001460 }
1461
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001462 memset(&config, 0, sizeof(config));
1463 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1464 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1465 config.src_addr = info->mmio_phys + NDDB;
1466 config.dst_addr = info->mmio_phys + NDDB;
1467 config.src_maxburst = 32;
1468 config.dst_maxburst = 32;
1469 ret = dmaengine_slave_config(info->dma_chan, &config);
1470 if (ret < 0) {
1471 dev_err(&info->pdev->dev,
1472 "dma channel configuration failed: %d\n",
1473 ret);
1474 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001475 }
1476
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001477 /*
1478 * Now that DMA buffers are allocated we turn on
1479 * DMA proper for I/O operations.
1480 */
1481 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001482 return 0;
1483}
1484
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001485static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1486{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001487 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001488 dmaengine_terminate_all(info->dma_chan);
1489 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001490 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001491 kfree(info->data_buff);
1492}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001493
Antoine Ténartf19fe982015-10-21 10:29:03 +02001494static int pxa3xx_nand_sensing(struct pxa3xx_nand_host *host)
eric miaofe69af02008-02-14 15:48:23 +08001495{
Antoine Ténartf19fe982015-10-21 10:29:03 +02001496 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001497 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001498 struct nand_chip *chip;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001499 const struct nand_sdr_timings *timings;
Lei Wend4568822011-07-14 20:44:32 -07001500 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001501
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001502 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001503 chip = mtd->priv;
1504
Lei Wen401e67e2011-02-28 10:32:14 +08001505 /* use the common timing to make a try */
Antoine Ténartf19fe982015-10-21 10:29:03 +02001506 timings = onfi_async_timing_mode_to_sdr_timings(0);
1507 if (IS_ERR(timings))
1508 return PTR_ERR(timings);
1509
1510 pxa3xx_nand_set_sdr_timing(host, timings);
Lei Wend4568822011-07-14 20:44:32 -07001511
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001512 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001513 ret = chip->waitfunc(mtd, chip);
1514 if (ret & NAND_STATUS_FAIL)
1515 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001516
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001517 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001518}
eric miaofe69af02008-02-14 15:48:23 +08001519
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001520static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1521 struct nand_ecc_ctrl *ecc,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001522 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001523{
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001524 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001525 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001526 info->spare_size = 40;
1527 info->ecc_size = 24;
1528 ecc->mode = NAND_ECC_HW;
1529 ecc->size = 512;
1530 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001531
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001532 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001533 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001534 info->spare_size = 8;
1535 info->ecc_size = 8;
1536 ecc->mode = NAND_ECC_HW;
1537 ecc->size = 512;
1538 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001539
Brian Norris6033a942013-11-14 14:41:32 -08001540 /*
1541 * Required ECC: 4-bit correction per 512 bytes
1542 * Select: 16-bit correction per 2048 bytes
1543 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001544 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1545 info->ecc_bch = 1;
1546 info->chunk_size = 2048;
1547 info->spare_size = 32;
1548 info->ecc_size = 32;
1549 ecc->mode = NAND_ECC_HW;
1550 ecc->size = info->chunk_size;
1551 ecc->layout = &ecc_layout_2KB_bch4bit;
1552 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001553
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001554 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001555 info->ecc_bch = 1;
1556 info->chunk_size = 2048;
1557 info->spare_size = 32;
1558 info->ecc_size = 32;
1559 ecc->mode = NAND_ECC_HW;
1560 ecc->size = info->chunk_size;
1561 ecc->layout = &ecc_layout_4KB_bch4bit;
1562 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001563
Brian Norris6033a942013-11-14 14:41:32 -08001564 /*
1565 * Required ECC: 8-bit correction per 512 bytes
1566 * Select: 16-bit correction per 1024 bytes
1567 */
1568 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001569 info->ecc_bch = 1;
1570 info->chunk_size = 1024;
1571 info->spare_size = 0;
1572 info->ecc_size = 32;
1573 ecc->mode = NAND_ECC_HW;
1574 ecc->size = info->chunk_size;
1575 ecc->layout = &ecc_layout_4KB_bch8bit;
1576 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001577 } else {
1578 dev_err(&info->pdev->dev,
1579 "ECC strength %d at page size %d is not supported\n",
1580 strength, page_size);
1581 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001582 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001583
1584 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1585 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001586 return 0;
1587}
1588
Lei Wen401e67e2011-02-28 10:32:14 +08001589static int pxa3xx_nand_scan(struct mtd_info *mtd)
1590{
Lei Wend4568822011-07-14 20:44:32 -07001591 struct pxa3xx_nand_host *host = mtd->priv;
1592 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001593 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001594 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen401e67e2011-02-28 10:32:14 +08001595 struct nand_chip *chip = mtd->priv;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001596 int ret;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001597 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001598
Ezequiel García154f50f2015-11-04 13:13:43 -03001599 if (pdata->keep_config) {
1600 pxa3xx_nand_detect_config(info);
1601 } else {
1602 ret = pxa3xx_nand_config_ident(info);
1603 if (ret)
1604 return ret;
1605 ret = pxa3xx_nand_sensing(host);
1606 if (ret) {
1607 dev_info(&info->pdev->dev,
1608 "There is no chip on cs %d!\n",
1609 info->cs);
1610 return ret;
1611 }
Lei Wen401e67e2011-02-28 10:32:14 +08001612 }
1613
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001614 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001615 chip->options |= NAND_BUSWIDTH_16;
1616
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001617 /* Device detection must be done with ECC disabled */
1618 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1619 nand_writel(info, NDECCCTRL, 0x0);
1620
Antoine Ténartf19fe982015-10-21 10:29:03 +02001621 if (nand_scan_ident(mtd, 1, NULL))
Lei Wen4332c112011-03-03 11:27:01 +08001622 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001623
Antoine Ténartf19fe982015-10-21 10:29:03 +02001624 if (!pdata->keep_config) {
1625 ret = pxa3xx_nand_init(host);
1626 if (ret) {
1627 dev_err(&info->pdev->dev, "Failed to init nand: %d\n",
1628 ret);
1629 return ret;
1630 }
1631 }
1632
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001633 if (pdata->flash_bbt) {
1634 /*
1635 * We'll use a bad block table stored in-flash and don't
1636 * allow writing the bad block marker to the flash.
1637 */
1638 chip->bbt_options |= NAND_BBT_USE_FLASH |
1639 NAND_BBT_NO_OOB_BBM;
1640 chip->bbt_td = &bbt_main_descr;
1641 chip->bbt_md = &bbt_mirror_descr;
1642 }
1643
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001644 /*
1645 * If the page size is bigger than the FIFO size, let's check
1646 * we are given the right variant and then switch to the extended
1647 * (aka splitted) command handling,
1648 */
1649 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1650 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1651 chip->cmdfunc = nand_cmdfunc_extended;
1652 } else {
1653 dev_err(&info->pdev->dev,
1654 "unsupported page size on this variant\n");
1655 return -ENODEV;
1656 }
1657 }
1658
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001659 if (pdata->ecc_strength && pdata->ecc_step_size) {
1660 ecc_strength = pdata->ecc_strength;
1661 ecc_step = pdata->ecc_step_size;
1662 } else {
1663 ecc_strength = chip->ecc_strength_ds;
1664 ecc_step = chip->ecc_step_ds;
1665 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001666
1667 /* Set default ECC strength requirements on non-ONFI devices */
1668 if (ecc_strength < 1 && ecc_step < 1) {
1669 ecc_strength = 1;
1670 ecc_step = 512;
1671 }
1672
1673 ret = pxa_ecc_init(info, &chip->ecc, ecc_strength,
1674 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001675 if (ret)
1676 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001677
Lei Wen4332c112011-03-03 11:27:01 +08001678 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001679 if (mtd->writesize >= 2048)
1680 host->col_addr_cycles = 2;
1681 else
1682 host->col_addr_cycles = 1;
1683
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001684 /* release the initial buffer */
1685 kfree(info->data_buff);
1686
1687 /* allocate the real data + oob buffer */
1688 info->buf_size = mtd->writesize + mtd->oobsize;
1689 ret = pxa3xx_nand_init_buff(info);
1690 if (ret)
1691 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001692 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001693
Lei Wen4332c112011-03-03 11:27:01 +08001694 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001695 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001696 else
Lei Wend4568822011-07-14 20:44:32 -07001697 host->row_addr_cycles = 2;
Ezequiel García66e8e472015-11-04 13:13:42 -03001698
1699 if (!pdata->keep_config)
1700 pxa3xx_nand_config_tail(info);
1701
Lei Wen401e67e2011-02-28 10:32:14 +08001702 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001703}
1704
Lei Wend4568822011-07-14 20:44:32 -07001705static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001706{
Brian Norrisa61ae812015-10-30 20:33:25 -07001707 struct device_node *np = pdev->dev.of_node;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001708 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001709 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001710 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001711 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001712 struct mtd_info *mtd;
1713 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001714 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001715
Jingoo Han453810b2013-07-30 17:18:33 +09001716 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001717 if (pdata->num_cs <= 0)
1718 return -ENODEV;
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001719 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1720 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1721 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001722 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001723
eric miaofe69af02008-02-14 15:48:23 +08001724 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001725 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001726 for (cs = 0; cs < pdata->num_cs; cs++) {
Rob Herringce914e62015-04-30 15:17:47 -05001727 mtd = (void *)&info[1] + (sizeof(*mtd) + sizeof(*host)) * cs;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001728 chip = (struct nand_chip *)(&mtd[1]);
1729 host = (struct pxa3xx_nand_host *)chip;
1730 info->host[cs] = host;
1731 host->mtd = mtd;
1732 host->cs = cs;
1733 host->info_data = info;
1734 mtd->priv = host;
Frans Klaver550dab52015-06-10 22:39:01 +02001735 mtd->dev.parent = &pdev->dev;
Brian Norrisa61ae812015-10-30 20:33:25 -07001736 /* FIXME: all chips use the same device tree partitions */
1737 nand_set_flash_node(chip, np);
eric miaofe69af02008-02-14 15:48:23 +08001738
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001739 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1740 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1741 chip->controller = &info->controller;
1742 chip->waitfunc = pxa3xx_nand_waitfunc;
1743 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001744 chip->read_word = pxa3xx_nand_read_word;
1745 chip->read_byte = pxa3xx_nand_read_byte;
1746 chip->read_buf = pxa3xx_nand_read_buf;
1747 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001748 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001749 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001750 }
Lei Wen401e67e2011-02-28 10:32:14 +08001751
1752 spin_lock_init(&chip->controller->lock);
1753 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001754 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001755 if (IS_ERR(info->clk)) {
1756 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001757 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001758 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001759 ret = clk_prepare_enable(info->clk);
1760 if (ret < 0)
1761 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001762
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001763 if (use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001764 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1765 if (r == NULL) {
1766 dev_err(&pdev->dev,
1767 "no resource defined for data DMA\n");
1768 ret = -ENXIO;
1769 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001770 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001771 info->drcmr_dat = r->start;
1772
1773 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1774 if (r == NULL) {
1775 dev_err(&pdev->dev,
1776 "no resource defined for cmd DMA\n");
1777 ret = -ENXIO;
1778 goto fail_disable_clk;
1779 }
1780 info->drcmr_cmd = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001781 }
eric miaofe69af02008-02-14 15:48:23 +08001782
1783 irq = platform_get_irq(pdev, 0);
1784 if (irq < 0) {
1785 dev_err(&pdev->dev, "no IRQ resource defined\n");
1786 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001787 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001788 }
1789
1790 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001791 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1792 if (IS_ERR(info->mmio_base)) {
1793 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001794 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001795 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001796 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001797
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001798 /* Allocate a buffer to allow flash detection */
1799 info->buf_size = INIT_BUFFER_SIZE;
1800 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1801 if (info->data_buff == NULL) {
1802 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001803 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001804 }
eric miaofe69af02008-02-14 15:48:23 +08001805
Haojian Zhuang346e1252009-09-10 14:27:23 +08001806 /* initialize all interrupts to be disabled */
1807 disable_int(info, NDSR_MASK);
1808
Robert Jarzmik24542252015-02-20 19:36:43 +01001809 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1810 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1811 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001812 if (ret < 0) {
1813 dev_err(&pdev->dev, "failed to request IRQ\n");
1814 goto fail_free_buf;
1815 }
1816
Lei Wene353a202011-03-03 11:08:30 +08001817 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001818
Lei Wend4568822011-07-14 20:44:32 -07001819 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001820
eric miaofe69af02008-02-14 15:48:23 +08001821fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001822 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001823 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001824fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001825 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001826 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001827}
1828
1829static int pxa3xx_nand_remove(struct platform_device *pdev)
1830{
Lei Wene353a202011-03-03 11:08:30 +08001831 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001832 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001833 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001834
Lei Wend4568822011-07-14 20:44:32 -07001835 if (!info)
1836 return 0;
1837
Jingoo Han453810b2013-07-30 17:18:33 +09001838 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001839
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001840 irq = platform_get_irq(pdev, 0);
1841 if (irq >= 0)
1842 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001843 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001844
Robert Jarzmike971aff2015-09-28 22:56:51 +02001845 /*
1846 * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
1847 * In order to prevent a lockup of the system bus, the DFI bus
1848 * arbitration is granted to SMC upon driver removal. This is done by
1849 * setting the x_ARB_CNTL bit, which also prevents the NAND to have
1850 * access to the bus anymore.
1851 */
1852 nand_writel(info, NDCR,
1853 (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
1854 NFCV1_NDCR_ARB_CNTL);
Ezequiel Garciafb320612013-04-17 13:38:12 -03001855 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001856
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001857 for (cs = 0; cs < pdata->num_cs; cs++)
1858 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001859 return 0;
1860}
1861
Daniel Mack1e7ba632012-07-22 19:51:02 +02001862static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1863{
1864 struct pxa3xx_nand_platform_data *pdata;
1865 struct device_node *np = pdev->dev.of_node;
1866 const struct of_device_id *of_id =
1867 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1868
1869 if (!of_id)
1870 return 0;
1871
1872 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1873 if (!pdata)
1874 return -ENOMEM;
1875
1876 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1877 pdata->enable_arbiter = 1;
1878 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1879 pdata->keep_config = 1;
1880 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001881 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001882
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001883 pdata->ecc_strength = of_get_nand_ecc_strength(np);
1884 if (pdata->ecc_strength < 0)
1885 pdata->ecc_strength = 0;
1886
1887 pdata->ecc_step_size = of_get_nand_ecc_step_size(np);
1888 if (pdata->ecc_step_size < 0)
1889 pdata->ecc_step_size = 0;
1890
Daniel Mack1e7ba632012-07-22 19:51:02 +02001891 pdev->dev.platform_data = pdata;
1892
1893 return 0;
1894}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001895
Lei Wene353a202011-03-03 11:08:30 +08001896static int pxa3xx_nand_probe(struct platform_device *pdev)
1897{
1898 struct pxa3xx_nand_platform_data *pdata;
1899 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001900 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001901
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001902 dma_available = IS_ENABLED(CONFIG_ARM) &&
1903 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1904 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001905 use_dma = 0;
1906 dev_warn(&pdev->dev,
1907 "This platform can't do DMA on this device\n");
1908 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001909
Daniel Mack1e7ba632012-07-22 19:51:02 +02001910 ret = pxa3xx_nand_probe_dt(pdev);
1911 if (ret)
1912 return ret;
1913
Jingoo Han453810b2013-07-30 17:18:33 +09001914 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001915 if (!pdata) {
1916 dev_err(&pdev->dev, "no platform data defined\n");
1917 return -ENODEV;
1918 }
1919
Lei Wend4568822011-07-14 20:44:32 -07001920 ret = alloc_nand_resource(pdev);
1921 if (ret) {
1922 dev_err(&pdev->dev, "alloc nand resource failed\n");
1923 return ret;
1924 }
Lei Wene353a202011-03-03 11:08:30 +08001925
Lei Wend4568822011-07-14 20:44:32 -07001926 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001927 probe_success = 0;
1928 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001929 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001930
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001931 /*
1932 * The mtd name matches the one used in 'mtdparts' kernel
1933 * parameter. This name cannot be changed or otherwise
1934 * user's mtd partitions configuration would get broken.
1935 */
1936 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001937 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001938 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001939 if (ret) {
1940 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1941 cs);
1942 continue;
1943 }
1944
Brian Norrisa61ae812015-10-30 20:33:25 -07001945 ret = mtd_device_register(mtd, pdata->parts[cs],
1946 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001947 if (!ret)
1948 probe_success = 1;
1949 }
1950
1951 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001952 pxa3xx_nand_remove(pdev);
1953 return -ENODEV;
1954 }
1955
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001956 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001957}
1958
eric miaofe69af02008-02-14 15:48:23 +08001959#ifdef CONFIG_PM
Brian Norrisd3e94f32015-10-12 14:07:41 -07001960static int pxa3xx_nand_suspend(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08001961{
Brian Norrisd3e94f32015-10-12 14:07:41 -07001962 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
eric miaofe69af02008-02-14 15:48:23 +08001963
Lei Wenf8155a42011-02-28 10:32:11 +08001964 if (info->state) {
Brian Norrisd3e94f32015-10-12 14:07:41 -07001965 dev_err(dev, "driver busy, state = %d\n", info->state);
eric miaofe69af02008-02-14 15:48:23 +08001966 return -EAGAIN;
1967 }
1968
1969 return 0;
1970}
1971
Brian Norrisd3e94f32015-10-12 14:07:41 -07001972static int pxa3xx_nand_resume(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08001973{
Brian Norrisd3e94f32015-10-12 14:07:41 -07001974 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
Lei Wen051fc412011-07-14 20:44:30 -07001975
1976 /* We don't want to handle interrupt without calling mtd routine */
1977 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001978
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001979 /*
1980 * Directly set the chip select to a invalid value,
1981 * then the driver would reset the timing according
1982 * to current chip select at the beginning of cmdfunc
1983 */
1984 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001985
Lei Wen051fc412011-07-14 20:44:30 -07001986 /*
1987 * As the spec says, the NDSR would be updated to 0x1800 when
1988 * doing the nand_clk disable/enable.
1989 * To prevent it damaging state machine of the driver, clear
1990 * all status before resume
1991 */
1992 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001993
Lei Wen18c81b12010-08-17 17:25:57 +08001994 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001995}
1996#else
1997#define pxa3xx_nand_suspend NULL
1998#define pxa3xx_nand_resume NULL
1999#endif
2000
Brian Norrisd3e94f32015-10-12 14:07:41 -07002001static const struct dev_pm_ops pxa3xx_nand_pm_ops = {
2002 .suspend = pxa3xx_nand_suspend,
2003 .resume = pxa3xx_nand_resume,
2004};
2005
eric miaofe69af02008-02-14 15:48:23 +08002006static struct platform_driver pxa3xx_nand_driver = {
2007 .driver = {
2008 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05302009 .of_match_table = pxa3xx_nand_dt_ids,
Brian Norrisd3e94f32015-10-12 14:07:41 -07002010 .pm = &pxa3xx_nand_pm_ops,
eric miaofe69af02008-02-14 15:48:23 +08002011 },
2012 .probe = pxa3xx_nand_probe,
2013 .remove = pxa3xx_nand_remove,
eric miaofe69af02008-02-14 15:48:23 +08002014};
2015
Axel Linf99640d2011-11-27 20:45:03 +08002016module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08002017
2018MODULE_LICENSE("GPL");
2019MODULE_DESCRIPTION("PXA3xx NAND controller driver");