blob: 436dd6dc11f4a810e838059c3ed0c5254a7cb07e [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>
Arnd Bergmann293b2da2012-08-24 15:16:48 +020032#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080033
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -050034#define CHIP_DELAY_TIMEOUT msecs_to_jiffies(200)
35#define NAND_STOP_DELAY msecs_to_jiffies(40)
Lei Wen4eb2da82011-02-28 10:32:13 +080036#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080037
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030038/*
39 * Define a buffer size for the initial command that detects the flash device:
Ezequiel Garciac1634092015-08-03 11:31:26 -030040 * STATUS, READID and PARAM.
41 * ONFI param page is 256 bytes, and there are three redundant copies
42 * to be read. JEDEC param page is 512 bytes, and there are also three
43 * redundant copies to be read.
44 * Hence this buffer should be at least 512 x 3. Let's pick 2048.
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030045 */
Ezequiel Garciac1634092015-08-03 11:31:26 -030046#define INIT_BUFFER_SIZE 2048
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030047
eric miaofe69af02008-02-14 15:48:23 +080048/* registers and bit definitions */
49#define NDCR (0x00) /* Control register */
50#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
51#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
52#define NDSR (0x14) /* Status Register */
53#define NDPCR (0x18) /* Page Count Register */
54#define NDBDR0 (0x1C) /* Bad Block Register 0 */
55#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030056#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080057#define NDDB (0x40) /* Data Buffer */
58#define NDCB0 (0x48) /* Command Buffer0 */
59#define NDCB1 (0x4C) /* Command Buffer1 */
60#define NDCB2 (0x50) /* Command Buffer2 */
61
62#define NDCR_SPARE_EN (0x1 << 31)
63#define NDCR_ECC_EN (0x1 << 30)
64#define NDCR_DMA_EN (0x1 << 29)
65#define NDCR_ND_RUN (0x1 << 28)
66#define NDCR_DWIDTH_C (0x1 << 27)
67#define NDCR_DWIDTH_M (0x1 << 26)
68#define NDCR_PAGE_SZ (0x1 << 24)
69#define NDCR_NCSX (0x1 << 23)
70#define NDCR_ND_MODE (0x3 << 21)
71#define NDCR_NAND_MODE (0x0)
72#define NDCR_CLR_PG_CNT (0x1 << 20)
Robert Jarzmike971aff2015-09-28 22:56:51 +020073#define NFCV1_NDCR_ARB_CNTL (0x1 << 19)
74#define NFCV2_NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080075#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
76#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
77
78#define NDCR_RA_START (0x1 << 15)
79#define NDCR_PG_PER_BLK (0x1 << 14)
80#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080081#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080082
83#define NDSR_MASK (0xfff)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030084#define NDSR_ERR_CNT_OFF (16)
85#define NDSR_ERR_CNT_MASK (0x1f)
86#define NDSR_ERR_CNT(sr) ((sr >> NDSR_ERR_CNT_OFF) & NDSR_ERR_CNT_MASK)
Lei Wenf8155a42011-02-28 10:32:11 +080087#define NDSR_RDY (0x1 << 12)
88#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080089#define NDSR_CS0_PAGED (0x1 << 10)
90#define NDSR_CS1_PAGED (0x1 << 9)
91#define NDSR_CS0_CMDD (0x1 << 8)
92#define NDSR_CS1_CMDD (0x1 << 7)
93#define NDSR_CS0_BBD (0x1 << 6)
94#define NDSR_CS1_BBD (0x1 << 5)
Ezequiel Garcia87f53362013-11-14 18:25:39 -030095#define NDSR_UNCORERR (0x1 << 4)
96#define NDSR_CORERR (0x1 << 3)
eric miaofe69af02008-02-14 15:48:23 +080097#define NDSR_WRDREQ (0x1 << 2)
98#define NDSR_RDDREQ (0x1 << 1)
99#define NDSR_WRCMDREQ (0x1)
100
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300101#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800102#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800103#define NDCB0_AUTO_RS (0x1 << 25)
104#define NDCB0_CSEL (0x1 << 24)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300105#define NDCB0_EXT_CMD_TYPE_MASK (0x7 << 29)
106#define NDCB0_EXT_CMD_TYPE(x) (((x) << 29) & NDCB0_EXT_CMD_TYPE_MASK)
eric miaofe69af02008-02-14 15:48:23 +0800107#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
108#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
109#define NDCB0_NC (0x1 << 20)
110#define NDCB0_DBC (0x1 << 19)
111#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
112#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
113#define NDCB0_CMD2_MASK (0xff << 8)
114#define NDCB0_CMD1_MASK (0xff)
115#define NDCB0_ADDR_CYC_SHIFT (16)
116
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300117#define EXT_CMD_TYPE_DISPATCH 6 /* Command dispatch */
118#define EXT_CMD_TYPE_NAKED_RW 5 /* Naked read or Naked write */
119#define EXT_CMD_TYPE_READ 4 /* Read */
120#define EXT_CMD_TYPE_DISP_WR 4 /* Command dispatch with write */
121#define EXT_CMD_TYPE_FINAL 3 /* Final command */
122#define EXT_CMD_TYPE_LAST_RW 1 /* Last naked read/write */
123#define EXT_CMD_TYPE_MONO 0 /* Monolithic read/write */
124
Ezequiel Garcíab226eca2015-08-19 19:40:09 -0300125/*
126 * This should be large enough to read 'ONFI' and 'JEDEC'.
127 * Let's use 7 bytes, which is the maximum ID count supported
128 * by the controller (see NDCR_RD_ID_CNT_MASK).
129 */
130#define READ_ID_BYTES 7
131
eric miaofe69af02008-02-14 15:48:23 +0800132/* macros for registers read/write */
Robert Jarzmik26d072e2016-01-25 21:29:45 +0100133#define nand_writel(info, off, val) \
134 do { \
135 dev_vdbg(&info->pdev->dev, \
136 "%s():%d nand_writel(0x%x, 0x%04x)\n", \
137 __func__, __LINE__, (val), (off)); \
138 writel_relaxed((val), (info)->mmio_base + (off)); \
139 } while (0)
eric miaofe69af02008-02-14 15:48:23 +0800140
Robert Jarzmik26d072e2016-01-25 21:29:45 +0100141#define nand_readl(info, off) \
142 ({ \
143 unsigned int _v; \
144 _v = readl_relaxed((info)->mmio_base + (off)); \
145 dev_vdbg(&info->pdev->dev, \
146 "%s():%d nand_readl(0x%04x) = 0x%x\n", \
147 __func__, __LINE__, (off), _v); \
148 _v; \
149 })
eric miaofe69af02008-02-14 15:48:23 +0800150
151/* error code and state */
152enum {
153 ERR_NONE = 0,
154 ERR_DMABUSERR = -1,
155 ERR_SENDCMD = -2,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300156 ERR_UNCORERR = -3,
eric miaofe69af02008-02-14 15:48:23 +0800157 ERR_BBERR = -4,
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300158 ERR_CORERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800159};
160
161enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800162 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700163 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800164 STATE_CMD_HANDLE,
165 STATE_DMA_READING,
166 STATE_DMA_WRITING,
167 STATE_DMA_DONE,
168 STATE_PIO_READING,
169 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800170 STATE_CMD_DONE,
171 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800172};
173
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300174enum pxa3xx_nand_variant {
175 PXA3XX_NAND_VARIANT_PXA,
176 PXA3XX_NAND_VARIANT_ARMADA370,
177};
178
Lei Wend4568822011-07-14 20:44:32 -0700179struct pxa3xx_nand_host {
180 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700181 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800182
Lei Wend4568822011-07-14 20:44:32 -0700183 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700184 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700185 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700186
187 /* calculated from pxa3xx_nand_flash data */
188 unsigned int col_addr_cycles;
189 unsigned int row_addr_cycles;
Lei Wend4568822011-07-14 20:44:32 -0700190};
191
192struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800193 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800194 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800195
196 struct clk *clk;
197 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800198 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300199 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800200
201 unsigned int buf_start;
202 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300203 unsigned int buf_size;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300204 unsigned int data_buff_pos;
205 unsigned int oob_buff_pos;
eric miaofe69af02008-02-14 15:48:23 +0800206
207 /* DMA information */
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200208 struct scatterlist sg;
209 enum dma_data_direction dma_dir;
210 struct dma_chan *dma_chan;
211 dma_cookie_t dma_cookie;
eric miaofe69af02008-02-14 15:48:23 +0800212 int drcmr_dat;
eric miaofe69af02008-02-14 15:48:23 +0800213
214 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800215 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800216 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800217 int data_dma_ch;
eric miaofe69af02008-02-14 15:48:23 +0800218
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700219 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800220 unsigned int state;
221
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300222 /*
223 * This driver supports NFCv1 (as found in PXA SoC)
224 * and NFCv2 (as found in Armada 370/XP SoC).
225 */
226 enum pxa3xx_nand_variant variant;
227
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700228 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800229 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300230 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800231 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300232 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300233 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800234
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100235 /* Amount of real data per full chunk */
236 unsigned int chunk_size;
237
238 /* Amount of spare data per full chunk */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300239 unsigned int spare_size;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100240
241 /* Number of full chunks (i.e chunk_size + spare_size) */
242 unsigned int nfullchunks;
243
244 /*
245 * Total number of chunks. If equal to nfullchunks, then there
246 * are only full chunks. Otherwise, there is one last chunk of
247 * size (last_chunk_size + last_spare_size)
248 */
249 unsigned int ntotalchunks;
250
251 /* Amount of real data in the last chunk */
252 unsigned int last_chunk_size;
253
254 /* Amount of spare data in the last chunk */
255 unsigned int last_spare_size;
256
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300257 unsigned int ecc_size;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300258 unsigned int ecc_err_cnt;
259 unsigned int max_bitflips;
eric miaofe69af02008-02-14 15:48:23 +0800260 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800261
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100262 /*
263 * Variables only valid during command
264 * execution. step_chunk_size and step_spare_size is the
265 * amount of real data and spare data in the current
266 * chunk. cur_chunk is the current chunk being
267 * read/programmed.
268 */
269 unsigned int step_chunk_size;
270 unsigned int step_spare_size;
271 unsigned int cur_chunk;
272
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300273 /* cached register value */
274 uint32_t reg_ndcr;
275 uint32_t ndtr0cs0;
276 uint32_t ndtr1cs0;
277
eric miaofe69af02008-02-14 15:48:23 +0800278 /* generated NDCBx register values */
279 uint32_t ndcb0;
280 uint32_t ndcb1;
281 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300282 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800283};
284
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030285static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800286module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300287MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800288
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300289struct pxa3xx_nand_timing {
290 unsigned int tCH; /* Enable signal hold time */
291 unsigned int tCS; /* Enable signal setup time */
292 unsigned int tWH; /* ND_nWE high duration */
293 unsigned int tWP; /* ND_nWE pulse time */
294 unsigned int tRH; /* ND_nRE high duration */
295 unsigned int tRP; /* ND_nRE pulse width */
296 unsigned int tR; /* ND_nWE high to ND_nRE low for read */
297 unsigned int tWHR; /* ND_nWE high to ND_nRE low for status read */
298 unsigned int tAR; /* ND_ALE low to ND_nRE low delay */
299};
300
301struct pxa3xx_nand_flash {
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300302 uint32_t chip_id;
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300303 unsigned int flash_width; /* Width of Flash memory (DWIDTH_M) */
304 unsigned int dfc_width; /* Width of flash controller(DWIDTH_C) */
Ezequiel Garcíaa9cadf72015-08-21 15:47:28 -0300305 struct pxa3xx_nand_timing *timing; /* NAND Flash timing */
306};
307
Lei Wenc1f82472010-08-17 13:50:23 +0800308static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800309 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
310 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
311 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
312 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800313};
314
Lei Wenc1f82472010-08-17 13:50:23 +0800315static struct pxa3xx_nand_flash builtin_flash_types[] = {
Antoine Ténart89c17022015-10-21 10:29:04 +0200316 { 0x46ec, 16, 16, &timing[1] },
317 { 0xdaec, 8, 8, &timing[1] },
318 { 0xd7ec, 8, 8, &timing[1] },
319 { 0xa12c, 8, 8, &timing[2] },
320 { 0xb12c, 16, 16, &timing[2] },
321 { 0xdc2c, 8, 8, &timing[2] },
322 { 0xcc2c, 16, 16, &timing[2] },
323 { 0xba20, 16, 16, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800324};
325
Boris Brezillon39980c52016-02-03 20:03:18 +0100326static int pxa3xx_ooblayout_ecc(struct mtd_info *mtd, int section,
327 struct mtd_oob_region *oobregion)
328{
329 struct nand_chip *chip = mtd_to_nand(mtd);
330 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
331 struct pxa3xx_nand_info *info = host->info_data;
332 int nchunks = mtd->writesize / info->chunk_size;
333
334 if (section >= nchunks)
335 return -ERANGE;
336
337 oobregion->offset = ((info->ecc_size + info->spare_size) * section) +
338 info->spare_size;
339 oobregion->length = info->ecc_size;
340
341 return 0;
342}
343
344static int pxa3xx_ooblayout_free(struct mtd_info *mtd, int section,
345 struct mtd_oob_region *oobregion)
346{
347 struct nand_chip *chip = mtd_to_nand(mtd);
348 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
349 struct pxa3xx_nand_info *info = host->info_data;
350 int nchunks = mtd->writesize / info->chunk_size;
351
352 if (section >= nchunks)
353 return -ERANGE;
354
355 if (!info->spare_size)
356 return 0;
357
358 oobregion->offset = section * (info->ecc_size + info->spare_size);
359 oobregion->length = info->spare_size;
360 if (!section) {
361 /*
362 * Bootrom looks in bytes 0 & 5 for bad blocks for the
363 * 4KB page / 4bit BCH combination.
364 */
365 if (mtd->writesize == 4096 && info->chunk_size == 2048) {
366 oobregion->offset += 6;
367 oobregion->length -= 6;
368 } else {
369 oobregion->offset += 2;
370 oobregion->length -= 2;
371 }
372 }
373
374 return 0;
375}
376
377static const struct mtd_ooblayout_ops pxa3xx_ooblayout_ops = {
378 .ecc = pxa3xx_ooblayout_ecc,
379 .free = pxa3xx_ooblayout_free,
380};
381
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300382static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
383static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
384
385static struct nand_bbt_descr bbt_main_descr = {
386 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
387 | NAND_BBT_2BIT | NAND_BBT_VERSION,
388 .offs = 8,
389 .len = 6,
390 .veroffs = 14,
391 .maxblocks = 8, /* Last 8 blocks in each chip */
392 .pattern = bbt_pattern
393};
394
395static struct nand_bbt_descr bbt_mirror_descr = {
396 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
397 | NAND_BBT_2BIT | NAND_BBT_VERSION,
398 .offs = 8,
399 .len = 6,
400 .veroffs = 14,
401 .maxblocks = 8, /* Last 8 blocks in each chip */
402 .pattern = bbt_mirror_pattern
403};
404
eric miaofe69af02008-02-14 15:48:23 +0800405#define NDTR0_tCH(c) (min((c), 7) << 19)
406#define NDTR0_tCS(c) (min((c), 7) << 16)
407#define NDTR0_tWH(c) (min((c), 7) << 11)
408#define NDTR0_tWP(c) (min((c), 7) << 8)
409#define NDTR0_tRH(c) (min((c), 7) << 3)
410#define NDTR0_tRP(c) (min((c), 7) << 0)
411
412#define NDTR1_tR(c) (min((c), 65535) << 16)
413#define NDTR1_tWHR(c) (min((c), 15) << 4)
414#define NDTR1_tAR(c) (min((c), 15) << 0)
415
416/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800417#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800418
Jingoo Han17754ad2014-05-07 17:49:13 +0900419static const struct of_device_id pxa3xx_nand_dt_ids[] = {
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300420 {
421 .compatible = "marvell,pxa3xx-nand",
422 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
423 },
Ezequiel Garcia1963ff92013-12-24 12:40:07 -0300424 {
425 .compatible = "marvell,armada370-nand",
426 .data = (void *)PXA3XX_NAND_VARIANT_ARMADA370,
427 },
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300428 {}
429};
430MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
431
432static enum pxa3xx_nand_variant
433pxa3xx_nand_get_variant(struct platform_device *pdev)
434{
435 const struct of_device_id *of_id =
436 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
437 if (!of_id)
438 return PXA3XX_NAND_VARIANT_PXA;
439 return (enum pxa3xx_nand_variant)of_id->data;
440}
441
Lei Wend4568822011-07-14 20:44:32 -0700442static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200443 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800444{
Lei Wend4568822011-07-14 20:44:32 -0700445 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800446 unsigned long nand_clk = clk_get_rate(info->clk);
447 uint32_t ndtr0, ndtr1;
448
449 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
450 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
451 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
452 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
453 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
454 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
455
456 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
457 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
458 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
459
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300460 info->ndtr0cs0 = ndtr0;
461 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800462 nand_writel(info, NDTR0CS0, ndtr0);
463 nand_writel(info, NDTR1CS0, ndtr1);
464}
465
Antoine Ténart3f225b72015-10-21 10:29:02 +0200466static void pxa3xx_nand_set_sdr_timing(struct pxa3xx_nand_host *host,
467 const struct nand_sdr_timings *t)
468{
469 struct pxa3xx_nand_info *info = host->info_data;
470 struct nand_chip *chip = &host->chip;
471 unsigned long nand_clk = clk_get_rate(info->clk);
472 uint32_t ndtr0, ndtr1;
473
474 u32 tCH_min = DIV_ROUND_UP(t->tCH_min, 1000);
475 u32 tCS_min = DIV_ROUND_UP(t->tCS_min, 1000);
476 u32 tWH_min = DIV_ROUND_UP(t->tWH_min, 1000);
477 u32 tWP_min = DIV_ROUND_UP(t->tWC_min - t->tWH_min, 1000);
478 u32 tREH_min = DIV_ROUND_UP(t->tREH_min, 1000);
479 u32 tRP_min = DIV_ROUND_UP(t->tRC_min - t->tREH_min, 1000);
480 u32 tR = chip->chip_delay * 1000;
481 u32 tWHR_min = DIV_ROUND_UP(t->tWHR_min, 1000);
482 u32 tAR_min = DIV_ROUND_UP(t->tAR_min, 1000);
483
484 /* fallback to a default value if tR = 0 */
485 if (!tR)
486 tR = 20000;
487
488 ndtr0 = NDTR0_tCH(ns2cycle(tCH_min, nand_clk)) |
489 NDTR0_tCS(ns2cycle(tCS_min, nand_clk)) |
490 NDTR0_tWH(ns2cycle(tWH_min, nand_clk)) |
491 NDTR0_tWP(ns2cycle(tWP_min, nand_clk)) |
492 NDTR0_tRH(ns2cycle(tREH_min, nand_clk)) |
493 NDTR0_tRP(ns2cycle(tRP_min, nand_clk));
494
495 ndtr1 = NDTR1_tR(ns2cycle(tR, nand_clk)) |
496 NDTR1_tWHR(ns2cycle(tWHR_min, nand_clk)) |
497 NDTR1_tAR(ns2cycle(tAR_min, nand_clk));
498
499 info->ndtr0cs0 = ndtr0;
500 info->ndtr1cs0 = ndtr1;
501 nand_writel(info, NDTR0CS0, ndtr0);
502 nand_writel(info, NDTR1CS0, ndtr1);
503}
504
505static int pxa3xx_nand_init_timings_compat(struct pxa3xx_nand_host *host,
506 unsigned int *flash_width,
507 unsigned int *dfc_width)
508{
509 struct nand_chip *chip = &host->chip;
510 struct pxa3xx_nand_info *info = host->info_data;
511 const struct pxa3xx_nand_flash *f = NULL;
Boris BREZILLON063294a2015-12-10 09:00:20 +0100512 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Antoine Ténart3f225b72015-10-21 10:29:02 +0200513 int i, id, ntypes;
514
515 ntypes = ARRAY_SIZE(builtin_flash_types);
516
Boris BREZILLON063294a2015-12-10 09:00:20 +0100517 chip->cmdfunc(mtd, NAND_CMD_READID, 0x00, -1);
Antoine Ténart3f225b72015-10-21 10:29:02 +0200518
Boris BREZILLON063294a2015-12-10 09:00:20 +0100519 id = chip->read_byte(mtd);
520 id |= chip->read_byte(mtd) << 0x8;
Antoine Ténart3f225b72015-10-21 10:29:02 +0200521
522 for (i = 0; i < ntypes; i++) {
523 f = &builtin_flash_types[i];
524
525 if (f->chip_id == id)
526 break;
527 }
528
529 if (i == ntypes) {
530 dev_err(&info->pdev->dev, "Error: timings not found\n");
531 return -EINVAL;
532 }
533
534 pxa3xx_nand_set_timing(host, f->timing);
535
536 *flash_width = f->flash_width;
537 *dfc_width = f->dfc_width;
538
539 return 0;
540}
541
542static int pxa3xx_nand_init_timings_onfi(struct pxa3xx_nand_host *host,
543 int mode)
544{
545 const struct nand_sdr_timings *timings;
546
547 mode = fls(mode) - 1;
548 if (mode < 0)
549 mode = 0;
550
551 timings = onfi_async_timing_mode_to_sdr_timings(mode);
552 if (IS_ERR(timings))
553 return PTR_ERR(timings);
554
555 pxa3xx_nand_set_sdr_timing(host, timings);
556
557 return 0;
558}
559
560static int pxa3xx_nand_init(struct pxa3xx_nand_host *host)
561{
562 struct nand_chip *chip = &host->chip;
563 struct pxa3xx_nand_info *info = host->info_data;
564 unsigned int flash_width = 0, dfc_width = 0;
565 int mode, err;
566
567 mode = onfi_get_async_timing_mode(chip);
568 if (mode == ONFI_TIMING_MODE_UNKNOWN) {
569 err = pxa3xx_nand_init_timings_compat(host, &flash_width,
570 &dfc_width);
571 if (err)
572 return err;
573
574 if (flash_width == 16) {
575 info->reg_ndcr |= NDCR_DWIDTH_M;
576 chip->options |= NAND_BUSWIDTH_16;
577 }
578
579 info->reg_ndcr |= (dfc_width == 16) ? NDCR_DWIDTH_C : 0;
580 } else {
581 err = pxa3xx_nand_init_timings_onfi(host, mode);
582 if (err)
583 return err;
584 }
585
586 return 0;
587}
588
Lei Wenf8155a42011-02-28 10:32:11 +0800589/**
590 * NOTE: it is a must to set ND_RUN firstly, then write
591 * command buffer, otherwise, it does not work.
592 * We enable all the interrupt at the same time, and
593 * let pxa3xx_nand_irq to handle all logic.
594 */
595static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
596{
597 uint32_t ndcr;
598
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300599 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300600
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300601 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300602 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300603 if (info->ecc_bch)
604 nand_writel(info, NDECCCTRL, 0x1);
605 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300606 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300607 if (info->ecc_bch)
608 nand_writel(info, NDECCCTRL, 0x0);
609 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300610
611 if (info->use_dma)
612 ndcr |= NDCR_DMA_EN;
613 else
614 ndcr &= ~NDCR_DMA_EN;
615
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300616 if (info->use_spare)
617 ndcr |= NDCR_SPARE_EN;
618 else
619 ndcr &= ~NDCR_SPARE_EN;
620
Lei Wenf8155a42011-02-28 10:32:11 +0800621 ndcr |= NDCR_ND_RUN;
622
623 /* clear status bits and run */
Lei Wenf8155a42011-02-28 10:32:11 +0800624 nand_writel(info, NDSR, NDSR_MASK);
Robert Jarzmik0b143922015-08-19 20:30:14 +0200625 nand_writel(info, NDCR, 0);
Lei Wenf8155a42011-02-28 10:32:11 +0800626 nand_writel(info, NDCR, ndcr);
627}
628
629static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
630{
631 uint32_t ndcr;
632 int timeout = NAND_STOP_DELAY;
633
634 /* wait RUN bit in NDCR become 0 */
635 ndcr = nand_readl(info, NDCR);
636 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
637 ndcr = nand_readl(info, NDCR);
638 udelay(1);
639 }
640
641 if (timeout <= 0) {
642 ndcr &= ~NDCR_ND_RUN;
643 nand_writel(info, NDCR, ndcr);
644 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200645 if (info->dma_chan)
646 dmaengine_terminate_all(info->dma_chan);
647
Lei Wenf8155a42011-02-28 10:32:11 +0800648 /* clear status bits */
649 nand_writel(info, NDSR, NDSR_MASK);
650}
651
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300652static void __maybe_unused
653enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800654{
655 uint32_t ndcr;
656
657 ndcr = nand_readl(info, NDCR);
658 nand_writel(info, NDCR, ndcr & ~int_mask);
659}
660
661static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
662{
663 uint32_t ndcr;
664
665 ndcr = nand_readl(info, NDCR);
666 nand_writel(info, NDCR, ndcr | int_mask);
667}
668
Maxime Ripard8dad0382015-02-18 11:32:07 +0100669static void drain_fifo(struct pxa3xx_nand_info *info, void *data, int len)
670{
671 if (info->ecc_bch) {
Maxime Ripardafca11e2015-04-07 15:32:45 +0200672 u32 val;
673 int ret;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100674
675 /*
676 * According to the datasheet, when reading from NDDB
677 * with BCH enabled, after each 32 bytes reads, we
678 * have to make sure that the NDSR.RDDREQ bit is set.
679 *
680 * Drain the FIFO 8 32 bits reads at a time, and skip
681 * the polling on the last read.
682 */
683 while (len > 8) {
Antoine Ténartab53a572015-10-21 10:29:00 +0200684 ioread32_rep(info->mmio_base + NDDB, data, 8);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100685
Maxime Ripardafca11e2015-04-07 15:32:45 +0200686 ret = readl_relaxed_poll_timeout(info->mmio_base + NDSR, val,
687 val & NDSR_RDDREQ, 1000, 5000);
688 if (ret) {
689 dev_err(&info->pdev->dev,
690 "Timeout on RDDREQ while draining the FIFO\n");
691 return;
Maxime Ripard8dad0382015-02-18 11:32:07 +0100692 }
693
694 data += 32;
695 len -= 8;
696 }
697 }
698
Antoine Ténartab53a572015-10-21 10:29:00 +0200699 ioread32_rep(info->mmio_base + NDDB, data, len);
Maxime Ripard8dad0382015-02-18 11:32:07 +0100700}
701
Lei Wenf8155a42011-02-28 10:32:11 +0800702static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800703{
eric miaofe69af02008-02-14 15:48:23 +0800704 switch (info->state) {
705 case STATE_PIO_WRITING:
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100706 if (info->step_chunk_size)
707 writesl(info->mmio_base + NDDB,
708 info->data_buff + info->data_buff_pos,
709 DIV_ROUND_UP(info->step_chunk_size, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300710
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100711 if (info->step_spare_size)
Rob Herringce914e62015-04-30 15:17:47 -0500712 writesl(info->mmio_base + NDDB,
713 info->oob_buff + info->oob_buff_pos,
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100714 DIV_ROUND_UP(info->step_spare_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800715 break;
716 case STATE_PIO_READING:
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100717 if (info->step_chunk_size)
718 drain_fifo(info,
719 info->data_buff + info->data_buff_pos,
720 DIV_ROUND_UP(info->step_chunk_size, 4));
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300721
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100722 if (info->step_spare_size)
Maxime Ripard8dad0382015-02-18 11:32:07 +0100723 drain_fifo(info,
724 info->oob_buff + info->oob_buff_pos,
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100725 DIV_ROUND_UP(info->step_spare_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800726 break;
727 default:
Lei Wenda675b42011-07-14 20:44:31 -0700728 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800729 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800730 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800731 }
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300732
733 /* Update buffer pointers for multi-page read/write */
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100734 info->data_buff_pos += info->step_chunk_size;
735 info->oob_buff_pos += info->step_spare_size;
eric miaofe69af02008-02-14 15:48:23 +0800736}
737
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200738static void pxa3xx_nand_data_dma_irq(void *data)
739{
740 struct pxa3xx_nand_info *info = data;
741 struct dma_tx_state state;
742 enum dma_status status;
743
744 status = dmaengine_tx_status(info->dma_chan, info->dma_cookie, &state);
745 if (likely(status == DMA_COMPLETE)) {
746 info->state = STATE_DMA_DONE;
747 } else {
748 dev_err(&info->pdev->dev, "DMA error on data channel\n");
749 info->retcode = ERR_DMABUSERR;
750 }
751 dma_unmap_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
752
753 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
754 enable_int(info, NDCR_INT_MASK);
755}
756
Lei Wenf8155a42011-02-28 10:32:11 +0800757static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800758{
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200759 enum dma_transfer_direction direction;
760 struct dma_async_tx_descriptor *tx;
eric miaofe69af02008-02-14 15:48:23 +0800761
Lei Wenf8155a42011-02-28 10:32:11 +0800762 switch (info->state) {
763 case STATE_DMA_WRITING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200764 info->dma_dir = DMA_TO_DEVICE;
765 direction = DMA_MEM_TO_DEV;
Lei Wenf8155a42011-02-28 10:32:11 +0800766 break;
767 case STATE_DMA_READING:
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200768 info->dma_dir = DMA_FROM_DEVICE;
769 direction = DMA_DEV_TO_MEM;
Lei Wenf8155a42011-02-28 10:32:11 +0800770 break;
771 default:
Lei Wenda675b42011-07-14 20:44:31 -0700772 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800773 info->state);
774 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800775 }
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100776 info->sg.length = info->chunk_size;
777 if (info->use_spare)
778 info->sg.length += info->spare_size + info->ecc_size;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200779 dma_map_sg(info->dma_chan->device->dev, &info->sg, 1, info->dma_dir);
eric miaofe69af02008-02-14 15:48:23 +0800780
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200781 tx = dmaengine_prep_slave_sg(info->dma_chan, &info->sg, 1, direction,
782 DMA_PREP_INTERRUPT);
783 if (!tx) {
784 dev_err(&info->pdev->dev, "prep_slave_sg() failed\n");
785 return;
eric miaofe69af02008-02-14 15:48:23 +0800786 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +0200787 tx->callback = pxa3xx_nand_data_dma_irq;
788 tx->callback_param = info;
789 info->dma_cookie = dmaengine_submit(tx);
790 dma_async_issue_pending(info->dma_chan);
791 dev_dbg(&info->pdev->dev, "%s(dir=%d cookie=%x size=%u)\n",
792 __func__, direction, info->dma_cookie, info->sg.length);
eric miaofe69af02008-02-14 15:48:23 +0800793}
794
Robert Jarzmik24542252015-02-20 19:36:43 +0100795static irqreturn_t pxa3xx_nand_irq_thread(int irq, void *data)
796{
797 struct pxa3xx_nand_info *info = data;
798
799 handle_data_pio(info);
800
801 info->state = STATE_CMD_DONE;
802 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
803
804 return IRQ_HANDLED;
805}
806
eric miaofe69af02008-02-14 15:48:23 +0800807static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
808{
809 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300810 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700811 unsigned int ready, cmd_done;
Robert Jarzmik24542252015-02-20 19:36:43 +0100812 irqreturn_t ret = IRQ_HANDLED;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700813
814 if (info->cs == 0) {
815 ready = NDSR_FLASH_RDY;
816 cmd_done = NDSR_CS0_CMDD;
817 } else {
818 ready = NDSR_RDY;
819 cmd_done = NDSR_CS1_CMDD;
820 }
eric miaofe69af02008-02-14 15:48:23 +0800821
822 status = nand_readl(info, NDSR);
823
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300824 if (status & NDSR_UNCORERR)
825 info->retcode = ERR_UNCORERR;
826 if (status & NDSR_CORERR) {
827 info->retcode = ERR_CORERR;
828 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370 &&
829 info->ecc_bch)
830 info->ecc_err_cnt = NDSR_ERR_CNT(status);
831 else
832 info->ecc_err_cnt = 1;
833
834 /*
835 * Each chunk composing a page is corrected independently,
836 * and we need to store maximum number of corrected bitflips
837 * to return it to the MTD layer in ecc.read_page().
838 */
839 info->max_bitflips = max_t(unsigned int,
840 info->max_bitflips,
841 info->ecc_err_cnt);
842 }
Lei Wenf8155a42011-02-28 10:32:11 +0800843 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
844 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800845 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800846 disable_int(info, NDCR_INT_MASK);
847 info->state = (status & NDSR_RDDREQ) ?
848 STATE_DMA_READING : STATE_DMA_WRITING;
849 start_data_dma(info);
850 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800851 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800852 info->state = (status & NDSR_RDDREQ) ?
853 STATE_PIO_READING : STATE_PIO_WRITING;
Robert Jarzmik24542252015-02-20 19:36:43 +0100854 ret = IRQ_WAKE_THREAD;
855 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800856 }
Lei Wenf8155a42011-02-28 10:32:11 +0800857 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700858 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800859 info->state = STATE_CMD_DONE;
860 is_completed = 1;
861 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700862 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800863 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300864 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800865 }
Lei Wenf8155a42011-02-28 10:32:11 +0800866
Robert Jarzmik21fc0ef2015-08-19 20:30:15 +0200867 /*
868 * Clear all status bit before issuing the next command, which
869 * can and will alter the status bits and will deserve a new
870 * interrupt on its own. This lets the controller exit the IRQ
871 */
872 nand_writel(info, NDSR, status);
873
Lei Wenf8155a42011-02-28 10:32:11 +0800874 if (status & NDSR_WRCMDREQ) {
Lei Wenf8155a42011-02-28 10:32:11 +0800875 status &= ~NDSR_WRCMDREQ;
876 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300877
878 /*
879 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
880 * must be loaded by writing directly either 12 or 16
881 * bytes directly to NDCB0, four bytes at a time.
882 *
883 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
884 * but each NDCBx register can be read.
885 */
Lei Wenf8155a42011-02-28 10:32:11 +0800886 nand_writel(info, NDCB0, info->ndcb0);
887 nand_writel(info, NDCB0, info->ndcb1);
888 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300889
890 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
891 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
892 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800893 }
Lei Wenf8155a42011-02-28 10:32:11 +0800894
Lei Wenf8155a42011-02-28 10:32:11 +0800895 if (is_completed)
896 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300897 if (is_ready)
898 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800899NORMAL_IRQ_EXIT:
Robert Jarzmik24542252015-02-20 19:36:43 +0100900 return ret;
eric miaofe69af02008-02-14 15:48:23 +0800901}
902
eric miaofe69af02008-02-14 15:48:23 +0800903static inline int is_buf_blank(uint8_t *buf, size_t len)
904{
905 for (; len > 0; len--)
906 if (*buf++ != 0xff)
907 return 0;
908 return 1;
909}
910
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300911static void set_command_address(struct pxa3xx_nand_info *info,
912 unsigned int page_size, uint16_t column, int page_addr)
913{
914 /* small page addr setting */
915 if (page_size < PAGE_CHUNK_SIZE) {
916 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
917 | (column & 0xFF);
918
919 info->ndcb2 = 0;
920 } else {
921 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
922 | (column & 0xFFFF);
923
924 if (page_addr & 0xFF0000)
925 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
926 else
927 info->ndcb2 = 0;
928 }
929}
930
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300931static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800932{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300933 struct pxa3xx_nand_host *host = info->host[info->cs];
Boris BREZILLON063294a2015-12-10 09:00:20 +0100934 struct mtd_info *mtd = nand_to_mtd(&host->chip);
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300935
Lei Wen4eb2da82011-02-28 10:32:13 +0800936 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800937 info->buf_start = 0;
938 info->buf_count = 0;
Ezequiel Garciafa543be2013-11-14 18:25:36 -0300939 info->data_buff_pos = 0;
940 info->oob_buff_pos = 0;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +0100941 info->step_chunk_size = 0;
942 info->step_spare_size = 0;
943 info->cur_chunk = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800944 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300945 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800946 info->retcode = ERR_NONE;
Ezequiel Garcia87f53362013-11-14 18:25:39 -0300947 info->ecc_err_cnt = 0;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300948 info->ndcb3 = 0;
Ezequiel Garciad20d0a62013-12-18 18:44:08 -0300949 info->need_wait = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800950
951 switch (command) {
952 case NAND_CMD_READ0:
953 case NAND_CMD_PAGEPROG:
954 info->use_ecc = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800955 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300956 case NAND_CMD_PARAM:
957 info->use_spare = 0;
958 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800959 default:
960 info->ndcb1 = 0;
961 info->ndcb2 = 0;
962 break;
963 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300964
965 /*
966 * If we are about to issue a read command, or about to set
967 * the write address, then clean the data buffer.
968 */
969 if (command == NAND_CMD_READ0 ||
970 command == NAND_CMD_READOOB ||
971 command == NAND_CMD_SEQIN) {
972
973 info->buf_count = mtd->writesize + mtd->oobsize;
974 memset(info->data_buff, 0xFF, info->buf_count);
975 }
976
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300977}
978
979static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
Ezequiel Garcia70ed8522013-11-14 18:25:37 -0300980 int ext_cmd_type, uint16_t column, int page_addr)
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300981{
982 int addr_cycle, exec_cmd;
983 struct pxa3xx_nand_host *host;
984 struct mtd_info *mtd;
985
986 host = info->host[info->cs];
Boris BREZILLON063294a2015-12-10 09:00:20 +0100987 mtd = nand_to_mtd(&host->chip);
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300988 addr_cycle = 0;
989 exec_cmd = 1;
990
991 if (info->cs != 0)
992 info->ndcb0 = NDCB0_CSEL;
993 else
994 info->ndcb0 = 0;
995
996 if (command == NAND_CMD_SEQIN)
997 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800998
Lei Wend4568822011-07-14 20:44:32 -0700999 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
1000 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +08001001
1002 switch (command) {
1003 case NAND_CMD_READOOB:
1004 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -03001005 info->buf_start = column;
1006 info->ndcb0 |= NDCB0_CMD_TYPE(0)
1007 | addr_cycle
1008 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +08001009
Ezequiel Garciaec821352013-08-12 14:14:54 -03001010 if (command == NAND_CMD_READOOB)
1011 info->buf_start += mtd->writesize;
1012
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001013 if (info->cur_chunk < info->nfullchunks) {
1014 info->step_chunk_size = info->chunk_size;
1015 info->step_spare_size = info->spare_size;
1016 } else {
1017 info->step_chunk_size = info->last_chunk_size;
1018 info->step_spare_size = info->last_spare_size;
1019 }
1020
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001021 /*
1022 * Multiple page read needs an 'extended command type' field,
1023 * which is either naked-read or last-read according to the
1024 * state.
1025 */
1026 if (mtd->writesize == PAGE_CHUNK_SIZE) {
Ezequiel Garciaec821352013-08-12 14:14:54 -03001027 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001028 } else if (mtd->writesize > PAGE_CHUNK_SIZE) {
1029 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8)
1030 | NDCB0_LEN_OVRD
1031 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001032 info->ndcb3 = info->step_chunk_size +
1033 info->step_spare_size;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001034 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001035
Ezequiel Garcia01d99472013-11-14 18:25:32 -03001036 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -03001037 break;
1038
Lei Wen4eb2da82011-02-28 10:32:13 +08001039 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +08001040
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -03001041 info->buf_start = column;
1042 set_command_address(info, mtd->writesize, 0, page_addr);
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001043
1044 /*
1045 * Multiple page programming needs to execute the initial
1046 * SEQIN command that sets the page address.
1047 */
1048 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1049 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1050 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1051 | addr_cycle
1052 | command;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001053 exec_cmd = 1;
1054 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001055 break;
1056
1057 case NAND_CMD_PAGEPROG:
1058 if (is_buf_blank(info->data_buff,
1059 (mtd->writesize + mtd->oobsize))) {
1060 exec_cmd = 0;
1061 break;
1062 }
1063
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001064 if (info->cur_chunk < info->nfullchunks) {
1065 info->step_chunk_size = info->chunk_size;
1066 info->step_spare_size = info->spare_size;
1067 } else {
1068 info->step_chunk_size = info->last_chunk_size;
1069 info->step_spare_size = info->last_spare_size;
1070 }
1071
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001072 /* Second command setting for large pages */
1073 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1074 /*
1075 * Multiple page write uses the 'extended command'
1076 * field. This can be used to issue a command dispatch
1077 * or a naked-write depending on the current stage.
1078 */
1079 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1080 | NDCB0_LEN_OVRD
1081 | NDCB0_EXT_CMD_TYPE(ext_cmd_type);
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001082 info->ndcb3 = info->step_chunk_size +
1083 info->step_spare_size;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001084
1085 /*
1086 * This is the command dispatch that completes a chunked
1087 * page program operation.
1088 */
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001089 if (info->cur_chunk == info->ntotalchunks) {
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001090 info->ndcb0 = NDCB0_CMD_TYPE(0x1)
1091 | NDCB0_EXT_CMD_TYPE(ext_cmd_type)
1092 | command;
1093 info->ndcb1 = 0;
1094 info->ndcb2 = 0;
1095 info->ndcb3 = 0;
1096 }
1097 } else {
1098 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
1099 | NDCB0_AUTO_RS
1100 | NDCB0_ST_ROW_EN
1101 | NDCB0_DBC
1102 | (NAND_CMD_PAGEPROG << 8)
1103 | NAND_CMD_SEQIN
1104 | addr_cycle;
1105 }
Lei Wen4eb2da82011-02-28 10:32:13 +08001106 break;
1107
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001108 case NAND_CMD_PARAM:
Ezequiel Garciac1634092015-08-03 11:31:26 -03001109 info->buf_count = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001110 info->ndcb0 |= NDCB0_CMD_TYPE(0)
1111 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -03001112 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -03001113 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001114 info->ndcb1 = (column & 0xFF);
Ezequiel Garciac1634092015-08-03 11:31:26 -03001115 info->ndcb3 = INIT_BUFFER_SIZE;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001116 info->step_chunk_size = INIT_BUFFER_SIZE;
Ezequiel Garciace0268f2013-05-14 08:15:25 -03001117 break;
1118
Lei Wen4eb2da82011-02-28 10:32:13 +08001119 case NAND_CMD_READID:
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001120 info->buf_count = READ_ID_BYTES;
Lei Wen4eb2da82011-02-28 10:32:13 +08001121 info->ndcb0 |= NDCB0_CMD_TYPE(3)
1122 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001123 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -03001124 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +08001125
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001126 info->step_chunk_size = 8;
Lei Wen4eb2da82011-02-28 10:32:13 +08001127 break;
1128 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +08001129 info->buf_count = 1;
1130 info->ndcb0 |= NDCB0_CMD_TYPE(4)
1131 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001132 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001133
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001134 info->step_chunk_size = 8;
Lei Wen4eb2da82011-02-28 10:32:13 +08001135 break;
1136
1137 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +08001138 info->ndcb0 |= NDCB0_CMD_TYPE(2)
1139 | NDCB0_AUTO_RS
1140 | NDCB0_ADDR_CYC(3)
1141 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -03001142 | (NAND_CMD_ERASE2 << 8)
1143 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +08001144 info->ndcb1 = page_addr;
1145 info->ndcb2 = 0;
1146
1147 break;
1148 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +08001149 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -03001150 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +08001151
1152 break;
1153
1154 case NAND_CMD_ERASE2:
1155 exec_cmd = 0;
1156 break;
1157
1158 default:
1159 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -07001160 dev_err(&info->pdev->dev, "non-supported command %x\n",
1161 command);
Lei Wen4eb2da82011-02-28 10:32:13 +08001162 break;
1163 }
1164
1165 return exec_cmd;
1166}
1167
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001168static void nand_cmdfunc(struct mtd_info *mtd, unsigned command,
1169 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +08001170{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001171 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001172 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001173 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001174 int exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +08001175
Lei Wen4eb2da82011-02-28 10:32:13 +08001176 /*
1177 * if this is a x16 device ,then convert the input
1178 * "byte" address into a "word" address appropriate
1179 * for indexing a word-oriented device
1180 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001181 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +08001182 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +08001183
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001184 /*
1185 * There may be different NAND chip hooked to
1186 * different chip select, so check whether
1187 * chip select has been changed, if yes, reset the timing
1188 */
1189 if (info->cs != host->cs) {
1190 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001191 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1192 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001193 }
1194
Ezequiel Garciac39ff032013-11-14 18:25:33 -03001195 prepare_start_command(info, command);
1196
Lei Wend4568822011-07-14 20:44:32 -07001197 info->state = STATE_PREPARED;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001198 exec_cmd = prepare_set_command(info, command, 0, column, page_addr);
1199
Lei Wenf8155a42011-02-28 10:32:11 +08001200 if (exec_cmd) {
1201 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001202 init_completion(&info->dev_ready);
1203 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +08001204 pxa3xx_nand_start(info);
1205
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001206 if (!wait_for_completion_timeout(&info->cmd_complete,
1207 CHIP_DELAY_TIMEOUT)) {
Lei Wenda675b42011-07-14 20:44:31 -07001208 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +08001209 /* Stop State Machine for next command cycle */
1210 pxa3xx_nand_stop(info);
1211 }
eric miaofe69af02008-02-14 15:48:23 +08001212 }
Lei Wend4568822011-07-14 20:44:32 -07001213 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +08001214}
1215
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001216static void nand_cmdfunc_extended(struct mtd_info *mtd,
1217 const unsigned command,
1218 int column, int page_addr)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001219{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001220 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001221 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001222 struct pxa3xx_nand_info *info = host->info_data;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001223 int exec_cmd, ext_cmd_type;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001224
1225 /*
1226 * if this is a x16 device then convert the input
1227 * "byte" address into a "word" address appropriate
1228 * for indexing a word-oriented device
1229 */
1230 if (info->reg_ndcr & NDCR_DWIDTH_M)
1231 column /= 2;
1232
1233 /*
1234 * There may be different NAND chip hooked to
1235 * different chip select, so check whether
1236 * chip select has been changed, if yes, reset the timing
1237 */
1238 if (info->cs != host->cs) {
1239 info->cs = host->cs;
1240 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
1241 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
1242 }
1243
1244 /* Select the extended command for the first command */
1245 switch (command) {
1246 case NAND_CMD_READ0:
1247 case NAND_CMD_READOOB:
1248 ext_cmd_type = EXT_CMD_TYPE_MONO;
1249 break;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001250 case NAND_CMD_SEQIN:
1251 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
1252 break;
1253 case NAND_CMD_PAGEPROG:
1254 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
1255 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001256 default:
1257 ext_cmd_type = 0;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001258 break;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001259 }
1260
1261 prepare_start_command(info, command);
1262
1263 /*
1264 * Prepare the "is ready" completion before starting a command
1265 * transaction sequence. If the command is not executed the
1266 * completion will be completed, see below.
1267 *
1268 * We can do that inside the loop because the command variable
1269 * is invariant and thus so is the exec_cmd.
1270 */
1271 info->need_wait = 1;
1272 init_completion(&info->dev_ready);
1273 do {
1274 info->state = STATE_PREPARED;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001275
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001276 exec_cmd = prepare_set_command(info, command, ext_cmd_type,
1277 column, page_addr);
1278 if (!exec_cmd) {
1279 info->need_wait = 0;
1280 complete(&info->dev_ready);
1281 break;
1282 }
1283
1284 init_completion(&info->cmd_complete);
1285 pxa3xx_nand_start(info);
1286
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001287 if (!wait_for_completion_timeout(&info->cmd_complete,
1288 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001289 dev_err(&info->pdev->dev, "Wait time out!!!\n");
1290 /* Stop State Machine for next command cycle */
1291 pxa3xx_nand_stop(info);
1292 break;
1293 }
1294
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001295 /* Only a few commands need several steps */
1296 if (command != NAND_CMD_PAGEPROG &&
1297 command != NAND_CMD_READ0 &&
1298 command != NAND_CMD_READOOB)
1299 break;
1300
1301 info->cur_chunk++;
1302
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001303 /* Check if the sequence is complete */
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001304 if (info->cur_chunk == info->ntotalchunks && command != NAND_CMD_PAGEPROG)
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001305 break;
1306
1307 /*
1308 * After a splitted program command sequence has issued
1309 * the command dispatch, the command sequence is complete.
1310 */
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001311 if (info->cur_chunk == (info->ntotalchunks + 1) &&
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001312 command == NAND_CMD_PAGEPROG &&
1313 ext_cmd_type == EXT_CMD_TYPE_DISPATCH)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001314 break;
1315
1316 if (command == NAND_CMD_READ0 || command == NAND_CMD_READOOB) {
1317 /* Last read: issue a 'last naked read' */
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001318 if (info->cur_chunk == info->ntotalchunks - 1)
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001319 ext_cmd_type = EXT_CMD_TYPE_LAST_RW;
1320 else
1321 ext_cmd_type = EXT_CMD_TYPE_NAKED_RW;
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001322
1323 /*
1324 * If a splitted program command has no more data to transfer,
1325 * the command dispatch must be issued to complete.
1326 */
1327 } else if (command == NAND_CMD_PAGEPROG &&
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001328 info->cur_chunk == info->ntotalchunks) {
Ezequiel Garcia535cb572013-11-14 18:25:38 -03001329 ext_cmd_type = EXT_CMD_TYPE_DISPATCH;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001330 }
1331 } while (1);
1332
1333 info->state = STATE_IDLE;
1334}
1335
Josh Wufdbad98d2012-06-25 18:07:45 +08001336static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Boris BREZILLON45aaeff2015-10-13 11:22:18 +02001337 struct nand_chip *chip, const uint8_t *buf, int oob_required,
1338 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001339{
1340 chip->write_buf(mtd, buf, mtd->writesize);
1341 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +08001342
1343 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +08001344}
1345
1346static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -07001347 struct nand_chip *chip, uint8_t *buf, int oob_required,
1348 int page)
Lei Wenf8155a42011-02-28 10:32:11 +08001349{
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001350 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001351 struct pxa3xx_nand_info *info = host->info_data;
Lei Wenf8155a42011-02-28 10:32:11 +08001352
1353 chip->read_buf(mtd, buf, mtd->writesize);
1354 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
1355
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001356 if (info->retcode == ERR_CORERR && info->use_ecc) {
1357 mtd->ecc_stats.corrected += info->ecc_err_cnt;
1358
1359 } else if (info->retcode == ERR_UNCORERR) {
Lei Wenf8155a42011-02-28 10:32:11 +08001360 /*
1361 * for blank page (all 0xff), HW will calculate its ECC as
1362 * 0, which is different from the ECC information within
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001363 * OOB, ignore such uncorrectable errors
Lei Wenf8155a42011-02-28 10:32:11 +08001364 */
1365 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -07001366 info->retcode = ERR_NONE;
1367 else
Lei Wenf8155a42011-02-28 10:32:11 +08001368 mtd->ecc_stats.failed++;
1369 }
1370
Ezequiel Garcia87f53362013-11-14 18:25:39 -03001371 return info->max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +08001372}
1373
eric miaofe69af02008-02-14 15:48:23 +08001374static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
1375{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001376 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001377 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001378 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001379 char retval = 0xFF;
1380
1381 if (info->buf_start < info->buf_count)
1382 /* Has just send a new command? */
1383 retval = info->data_buff[info->buf_start++];
1384
1385 return retval;
1386}
1387
1388static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
1389{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001390 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001391 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001392 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001393 u16 retval = 0xFFFF;
1394
1395 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
1396 retval = *((u16 *)(info->data_buff+info->buf_start));
1397 info->buf_start += 2;
1398 }
1399 return retval;
1400}
1401
1402static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
1403{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001404 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001405 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001406 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001407 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1408
1409 memcpy(buf, info->data_buff + info->buf_start, real_len);
1410 info->buf_start += real_len;
1411}
1412
1413static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
1414 const uint8_t *buf, int len)
1415{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001416 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001417 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001418 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +08001419 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
1420
1421 memcpy(info->data_buff + info->buf_start, buf, real_len);
1422 info->buf_start += real_len;
1423}
1424
eric miaofe69af02008-02-14 15:48:23 +08001425static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
1426{
1427 return;
1428}
1429
1430static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
1431{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001432 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001433 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001434 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001435
1436 if (info->need_wait) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001437 info->need_wait = 0;
Nicholas Mc Guiree5860c12015-02-01 11:55:37 -05001438 if (!wait_for_completion_timeout(&info->dev_ready,
1439 CHIP_DELAY_TIMEOUT)) {
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001440 dev_err(&info->pdev->dev, "Ready time out!!!\n");
1441 return NAND_STATUS_FAIL;
1442 }
1443 }
eric miaofe69af02008-02-14 15:48:23 +08001444
1445 /* pxa3xx_nand_send_command has waited for command complete */
1446 if (this->state == FL_WRITING || this->state == FL_ERASING) {
1447 if (info->retcode == ERR_NONE)
1448 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001449 else
1450 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +08001451 }
1452
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -03001453 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +08001454}
1455
Ezequiel García66e8e472015-11-04 13:13:42 -03001456static int pxa3xx_nand_config_ident(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001457{
Ezequiel Garcíab1e48572015-11-04 13:13:44 -03001458 struct pxa3xx_nand_host *host = info->host[info->cs];
eric miaofe69af02008-02-14 15:48:23 +08001459 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001460 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcíab1e48572015-11-04 13:13:44 -03001461 const struct nand_sdr_timings *timings;
eric miaofe69af02008-02-14 15:48:23 +08001462
Ezequiel García66e8e472015-11-04 13:13:42 -03001463 /* Configure default flash values */
1464 info->chunk_size = PAGE_CHUNK_SIZE;
Antoine Ténartf19fe982015-10-21 10:29:03 +02001465 info->reg_ndcr = 0x0; /* enable all interrupts */
1466 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
1467 info->reg_ndcr |= NDCR_RD_ID_CNT(READ_ID_BYTES);
Ezequiel García66e8e472015-11-04 13:13:42 -03001468 info->reg_ndcr |= NDCR_SPARE_EN;
eric miaofe69af02008-02-14 15:48:23 +08001469
Ezequiel Garcíab1e48572015-11-04 13:13:44 -03001470 /* use the common timing to make a try */
1471 timings = onfi_async_timing_mode_to_sdr_timings(0);
1472 if (IS_ERR(timings))
1473 return PTR_ERR(timings);
1474
1475 pxa3xx_nand_set_sdr_timing(host, timings);
eric miaofe69af02008-02-14 15:48:23 +08001476 return 0;
1477}
1478
Ezequiel García66e8e472015-11-04 13:13:42 -03001479static void pxa3xx_nand_config_tail(struct pxa3xx_nand_info *info)
1480{
1481 struct pxa3xx_nand_host *host = info->host[info->cs];
Boris BREZILLON063294a2015-12-10 09:00:20 +01001482 struct nand_chip *chip = &host->chip;
1483 struct mtd_info *mtd = nand_to_mtd(chip);
Ezequiel García66e8e472015-11-04 13:13:42 -03001484
1485 info->reg_ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
1486 info->reg_ndcr |= (chip->page_shift == 6) ? NDCR_PG_PER_BLK : 0;
1487 info->reg_ndcr |= (mtd->writesize == 2048) ? NDCR_PAGE_SZ : 0;
1488}
1489
Ezequiel García154f50f2015-11-04 13:13:43 -03001490static void pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
Mike Rapoportf2710492009-02-17 13:54:47 +02001491{
Ezequiel García66e8e472015-11-04 13:13:42 -03001492 struct platform_device *pdev = info->pdev;
1493 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Mike Rapoportf2710492009-02-17 13:54:47 +02001494 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +02001495
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001496 /* Set an initial chunk size */
Ezequiel Garcíab226eca2015-08-19 19:40:09 -03001497 info->chunk_size = ndcr & NDCR_PAGE_SZ ? 2048 : 512;
Robert Jarzmike971aff2015-09-28 22:56:51 +02001498 info->reg_ndcr = ndcr &
1499 ~(NDCR_INT_MASK | NDCR_ND_ARB_EN | NFCV1_NDCR_ARB_CNTL);
Ezequiel García66e8e472015-11-04 13:13:42 -03001500 info->reg_ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001501 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1502 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001503}
1504
eric miaofe69af02008-02-14 15:48:23 +08001505static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1506{
1507 struct platform_device *pdev = info->pdev;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001508 struct dma_slave_config config;
1509 dma_cap_mask_t mask;
1510 struct pxad_param param;
1511 int ret;
eric miaofe69af02008-02-14 15:48:23 +08001512
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001513 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1514 if (info->data_buff == NULL)
eric miaofe69af02008-02-14 15:48:23 +08001515 return -ENOMEM;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001516 if (use_dma == 0)
1517 return 0;
1518
1519 ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1520 if (ret)
1521 return ret;
1522
1523 sg_init_one(&info->sg, info->data_buff, info->buf_size);
1524 dma_cap_zero(mask);
1525 dma_cap_set(DMA_SLAVE, mask);
1526 param.prio = PXAD_PRIO_LOWEST;
1527 param.drcmr = info->drcmr_dat;
1528 info->dma_chan = dma_request_slave_channel_compat(mask, pxad_filter_fn,
1529 &param, &pdev->dev,
1530 "data");
1531 if (!info->dma_chan) {
1532 dev_err(&pdev->dev, "unable to request data dma channel\n");
1533 return -ENODEV;
eric miaofe69af02008-02-14 15:48:23 +08001534 }
1535
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001536 memset(&config, 0, sizeof(config));
1537 config.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1538 config.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1539 config.src_addr = info->mmio_phys + NDDB;
1540 config.dst_addr = info->mmio_phys + NDDB;
1541 config.src_maxburst = 32;
1542 config.dst_maxburst = 32;
1543 ret = dmaengine_slave_config(info->dma_chan, &config);
1544 if (ret < 0) {
1545 dev_err(&info->pdev->dev,
1546 "dma channel configuration failed: %d\n",
1547 ret);
1548 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001549 }
1550
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001551 /*
1552 * Now that DMA buffers are allocated we turn on
1553 * DMA proper for I/O operations.
1554 */
1555 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001556 return 0;
1557}
1558
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001559static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1560{
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001561 if (info->use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001562 dmaengine_terminate_all(info->dma_chan);
1563 dma_release_channel(info->dma_chan);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001564 }
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001565 kfree(info->data_buff);
1566}
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001567
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001568static int pxa_ecc_init(struct pxa3xx_nand_info *info,
Boris Brezillon39980c52016-02-03 20:03:18 +01001569 struct mtd_info *mtd,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001570 int strength, int ecc_stepsize, int page_size)
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001571{
Boris Brezillon39980c52016-02-03 20:03:18 +01001572 struct nand_chip *chip = mtd_to_nand(mtd);
1573 struct nand_ecc_ctrl *ecc = &chip->ecc;
1574
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001575 if (strength == 1 && ecc_stepsize == 512 && page_size == 2048) {
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001576 info->nfullchunks = 1;
1577 info->ntotalchunks = 1;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001578 info->chunk_size = 2048;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001579 info->spare_size = 40;
1580 info->ecc_size = 24;
1581 ecc->mode = NAND_ECC_HW;
1582 ecc->size = 512;
1583 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001584
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001585 } else if (strength == 1 && ecc_stepsize == 512 && page_size == 512) {
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001586 info->nfullchunks = 1;
1587 info->ntotalchunks = 1;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001588 info->chunk_size = 512;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001589 info->spare_size = 8;
1590 info->ecc_size = 8;
1591 ecc->mode = NAND_ECC_HW;
1592 ecc->size = 512;
1593 ecc->strength = 1;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001594
Brian Norris6033a942013-11-14 14:41:32 -08001595 /*
1596 * Required ECC: 4-bit correction per 512 bytes
1597 * Select: 16-bit correction per 2048 bytes
1598 */
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001599 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 2048) {
1600 info->ecc_bch = 1;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001601 info->nfullchunks = 1;
1602 info->ntotalchunks = 1;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001603 info->chunk_size = 2048;
1604 info->spare_size = 32;
1605 info->ecc_size = 32;
1606 ecc->mode = NAND_ECC_HW;
1607 ecc->size = info->chunk_size;
Boris Brezillon39980c52016-02-03 20:03:18 +01001608 mtd_set_ooblayout(mtd, &pxa3xx_ooblayout_ops);
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001609 ecc->strength = 16;
Rodolfo Giometti3db227b2014-01-13 15:35:38 +01001610
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001611 } else if (strength == 4 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001612 info->ecc_bch = 1;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001613 info->nfullchunks = 2;
1614 info->ntotalchunks = 2;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001615 info->chunk_size = 2048;
1616 info->spare_size = 32;
1617 info->ecc_size = 32;
1618 ecc->mode = NAND_ECC_HW;
1619 ecc->size = info->chunk_size;
Boris Brezillon39980c52016-02-03 20:03:18 +01001620 mtd_set_ooblayout(mtd, &pxa3xx_ooblayout_ops);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001621 ecc->strength = 16;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001622
Brian Norris6033a942013-11-14 14:41:32 -08001623 /*
1624 * Required ECC: 8-bit correction per 512 bytes
1625 * Select: 16-bit correction per 1024 bytes
1626 */
1627 } else if (strength == 8 && ecc_stepsize == 512 && page_size == 4096) {
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001628 info->ecc_bch = 1;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001629 info->nfullchunks = 4;
1630 info->ntotalchunks = 5;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001631 info->chunk_size = 1024;
1632 info->spare_size = 0;
Thomas Petazzonic2cdace2016-02-10 14:54:21 +01001633 info->last_chunk_size = 0;
1634 info->last_spare_size = 64;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001635 info->ecc_size = 32;
1636 ecc->mode = NAND_ECC_HW;
1637 ecc->size = info->chunk_size;
Boris Brezillon39980c52016-02-03 20:03:18 +01001638 mtd_set_ooblayout(mtd, &pxa3xx_ooblayout_ops);
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001639 ecc->strength = 16;
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001640 } else {
1641 dev_err(&info->pdev->dev,
1642 "ECC strength %d at page size %d is not supported\n",
1643 strength, page_size);
1644 return -ENODEV;
Ezequiel Garcia70ed8522013-11-14 18:25:37 -03001645 }
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001646
1647 dev_info(&info->pdev->dev, "ECC strength %d, ECC step size %d\n",
1648 ecc->strength, ecc->size);
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001649 return 0;
1650}
1651
Lei Wen401e67e2011-02-28 10:32:14 +08001652static int pxa3xx_nand_scan(struct mtd_info *mtd)
1653{
Boris BREZILLON4bd4ebc2015-12-01 12:03:04 +01001654 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001655 struct pxa3xx_nand_host *host = nand_get_controller_data(chip);
Lei Wend4568822011-07-14 20:44:32 -07001656 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001657 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001658 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Antoine Ténartf19fe982015-10-21 10:29:03 +02001659 int ret;
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001660 uint16_t ecc_strength, ecc_step;
Lei Wen401e67e2011-02-28 10:32:14 +08001661
Ezequiel García154f50f2015-11-04 13:13:43 -03001662 if (pdata->keep_config) {
1663 pxa3xx_nand_detect_config(info);
1664 } else {
1665 ret = pxa3xx_nand_config_ident(info);
1666 if (ret)
1667 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001668 }
1669
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001670 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001671 chip->options |= NAND_BUSWIDTH_16;
1672
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001673 /* Device detection must be done with ECC disabled */
1674 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1675 nand_writel(info, NDECCCTRL, 0x0);
1676
Boris Brezillon44ccb642016-04-01 14:54:30 +02001677 if (pdata->flash_bbt)
1678 chip->bbt_options |= NAND_BBT_USE_FLASH;
1679
1680 chip->ecc.strength = pdata->ecc_strength;
1681 chip->ecc.size = pdata->ecc_step_size;
1682
Antoine Ténartf19fe982015-10-21 10:29:03 +02001683 if (nand_scan_ident(mtd, 1, NULL))
Lei Wen4332c112011-03-03 11:27:01 +08001684 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001685
Antoine Ténartf19fe982015-10-21 10:29:03 +02001686 if (!pdata->keep_config) {
1687 ret = pxa3xx_nand_init(host);
1688 if (ret) {
1689 dev_err(&info->pdev->dev, "Failed to init nand: %d\n",
1690 ret);
1691 return ret;
1692 }
1693 }
1694
Boris Brezillon44ccb642016-04-01 14:54:30 +02001695 if (chip->bbt_options & NAND_BBT_USE_FLASH) {
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001696 /*
1697 * We'll use a bad block table stored in-flash and don't
1698 * allow writing the bad block marker to the flash.
1699 */
Boris Brezillon44ccb642016-04-01 14:54:30 +02001700 chip->bbt_options |= NAND_BBT_NO_OOB_BBM;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001701 chip->bbt_td = &bbt_main_descr;
1702 chip->bbt_md = &bbt_mirror_descr;
1703 }
1704
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001705 /*
1706 * If the page size is bigger than the FIFO size, let's check
1707 * we are given the right variant and then switch to the extended
1708 * (aka splitted) command handling,
1709 */
1710 if (mtd->writesize > PAGE_CHUNK_SIZE) {
1711 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370) {
1712 chip->cmdfunc = nand_cmdfunc_extended;
1713 } else {
1714 dev_err(&info->pdev->dev,
1715 "unsupported page size on this variant\n");
1716 return -ENODEV;
1717 }
1718 }
1719
Boris Brezillon44ccb642016-04-01 14:54:30 +02001720 ecc_strength = chip->ecc.strength;
1721 ecc_step = chip->ecc.size;
1722 if (!ecc_strength || !ecc_step) {
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001723 ecc_strength = chip->ecc_strength_ds;
1724 ecc_step = chip->ecc_step_ds;
1725 }
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001726
1727 /* Set default ECC strength requirements on non-ONFI devices */
1728 if (ecc_strength < 1 && ecc_step < 1) {
1729 ecc_strength = 1;
1730 ecc_step = 512;
1731 }
1732
Boris Brezillon39980c52016-02-03 20:03:18 +01001733 ret = pxa_ecc_init(info, mtd, ecc_strength,
Ezequiel Garcia30b2afc2013-12-18 18:44:10 -03001734 ecc_step, mtd->writesize);
Ezequiel Garciaeee01662014-05-14 14:58:07 -03001735 if (ret)
1736 return ret;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001737
Lei Wen4332c112011-03-03 11:27:01 +08001738 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001739 if (mtd->writesize >= 2048)
1740 host->col_addr_cycles = 2;
1741 else
1742 host->col_addr_cycles = 1;
1743
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001744 /* release the initial buffer */
1745 kfree(info->data_buff);
1746
1747 /* allocate the real data + oob buffer */
1748 info->buf_size = mtd->writesize + mtd->oobsize;
1749 ret = pxa3xx_nand_init_buff(info);
1750 if (ret)
1751 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001752 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001753
Lei Wen4332c112011-03-03 11:27:01 +08001754 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001755 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001756 else
Lei Wend4568822011-07-14 20:44:32 -07001757 host->row_addr_cycles = 2;
Ezequiel García66e8e472015-11-04 13:13:42 -03001758
1759 if (!pdata->keep_config)
1760 pxa3xx_nand_config_tail(info);
1761
Lei Wen401e67e2011-02-28 10:32:14 +08001762 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001763}
1764
Lei Wend4568822011-07-14 20:44:32 -07001765static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001766{
Brian Norrisa61ae812015-10-30 20:33:25 -07001767 struct device_node *np = pdev->dev.of_node;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001768 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001769 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001770 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001771 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001772 struct mtd_info *mtd;
1773 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001774 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001775
Jingoo Han453810b2013-07-30 17:18:33 +09001776 pdata = dev_get_platdata(&pdev->dev);
Robert Jarzmike423c902015-02-08 21:02:09 +01001777 if (pdata->num_cs <= 0)
1778 return -ENODEV;
Boris BREZILLON063294a2015-12-10 09:00:20 +01001779 info = devm_kzalloc(&pdev->dev,
1780 sizeof(*info) + sizeof(*host) * pdata->num_cs,
1781 GFP_KERNEL);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001782 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001783 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001784
eric miaofe69af02008-02-14 15:48:23 +08001785 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001786 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001787 for (cs = 0; cs < pdata->num_cs; cs++) {
Boris BREZILLON063294a2015-12-10 09:00:20 +01001788 host = (void *)&info[1] + sizeof(*host) * cs;
1789 chip = &host->chip;
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001790 nand_set_controller_data(chip, host);
Boris BREZILLON063294a2015-12-10 09:00:20 +01001791 mtd = nand_to_mtd(chip);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001792 info->host[cs] = host;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001793 host->cs = cs;
1794 host->info_data = info;
Frans Klaver550dab52015-06-10 22:39:01 +02001795 mtd->dev.parent = &pdev->dev;
Brian Norrisa61ae812015-10-30 20:33:25 -07001796 /* FIXME: all chips use the same device tree partitions */
1797 nand_set_flash_node(chip, np);
eric miaofe69af02008-02-14 15:48:23 +08001798
Boris BREZILLONd699ed22015-12-10 09:00:41 +01001799 nand_set_controller_data(chip, host);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001800 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1801 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1802 chip->controller = &info->controller;
1803 chip->waitfunc = pxa3xx_nand_waitfunc;
1804 chip->select_chip = pxa3xx_nand_select_chip;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001805 chip->read_word = pxa3xx_nand_read_word;
1806 chip->read_byte = pxa3xx_nand_read_byte;
1807 chip->read_buf = pxa3xx_nand_read_buf;
1808 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001809 chip->options |= NAND_NO_SUBPAGE_WRITE;
Ezequiel Garcia5cbbdc62013-12-18 18:44:09 -03001810 chip->cmdfunc = nand_cmdfunc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001811 }
Lei Wen401e67e2011-02-28 10:32:14 +08001812
1813 spin_lock_init(&chip->controller->lock);
1814 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001815 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001816 if (IS_ERR(info->clk)) {
1817 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001818 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001819 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001820 ret = clk_prepare_enable(info->clk);
1821 if (ret < 0)
1822 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001823
Robert Jarzmik90971032016-02-12 23:29:04 +01001824 if (!np && use_dma) {
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001825 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1826 if (r == NULL) {
1827 dev_err(&pdev->dev,
1828 "no resource defined for data DMA\n");
1829 ret = -ENXIO;
1830 goto fail_disable_clk;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001831 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001832 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001833 }
eric miaofe69af02008-02-14 15:48:23 +08001834
1835 irq = platform_get_irq(pdev, 0);
1836 if (irq < 0) {
1837 dev_err(&pdev->dev, "no IRQ resource defined\n");
1838 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001839 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001840 }
1841
1842 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001843 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1844 if (IS_ERR(info->mmio_base)) {
1845 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001846 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001847 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001848 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001849
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001850 /* Allocate a buffer to allow flash detection */
1851 info->buf_size = INIT_BUFFER_SIZE;
1852 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1853 if (info->data_buff == NULL) {
1854 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001855 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001856 }
eric miaofe69af02008-02-14 15:48:23 +08001857
Haojian Zhuang346e1252009-09-10 14:27:23 +08001858 /* initialize all interrupts to be disabled */
1859 disable_int(info, NDSR_MASK);
1860
Robert Jarzmik24542252015-02-20 19:36:43 +01001861 ret = request_threaded_irq(irq, pxa3xx_nand_irq,
1862 pxa3xx_nand_irq_thread, IRQF_ONESHOT,
1863 pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001864 if (ret < 0) {
1865 dev_err(&pdev->dev, "failed to request IRQ\n");
1866 goto fail_free_buf;
1867 }
1868
Lei Wene353a202011-03-03 11:08:30 +08001869 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001870
Lei Wend4568822011-07-14 20:44:32 -07001871 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001872
eric miaofe69af02008-02-14 15:48:23 +08001873fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001874 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001875 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001876fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001877 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001878 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001879}
1880
1881static int pxa3xx_nand_remove(struct platform_device *pdev)
1882{
Lei Wene353a202011-03-03 11:08:30 +08001883 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001884 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001885 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001886
Lei Wend4568822011-07-14 20:44:32 -07001887 if (!info)
1888 return 0;
1889
Jingoo Han453810b2013-07-30 17:18:33 +09001890 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001891
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001892 irq = platform_get_irq(pdev, 0);
1893 if (irq >= 0)
1894 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001895 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001896
Robert Jarzmike971aff2015-09-28 22:56:51 +02001897 /*
1898 * In the pxa3xx case, the DFI bus is shared between the SMC and NFC.
1899 * In order to prevent a lockup of the system bus, the DFI bus
1900 * arbitration is granted to SMC upon driver removal. This is done by
1901 * setting the x_ARB_CNTL bit, which also prevents the NAND to have
1902 * access to the bus anymore.
1903 */
1904 nand_writel(info, NDCR,
1905 (nand_readl(info, NDCR) & ~NDCR_ND_ARB_EN) |
1906 NFCV1_NDCR_ARB_CNTL);
Ezequiel Garciafb320612013-04-17 13:38:12 -03001907 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001908
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001909 for (cs = 0; cs < pdata->num_cs; cs++)
Boris BREZILLON063294a2015-12-10 09:00:20 +01001910 nand_release(nand_to_mtd(&info->host[cs]->chip));
eric miaofe69af02008-02-14 15:48:23 +08001911 return 0;
1912}
1913
Daniel Mack1e7ba632012-07-22 19:51:02 +02001914static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1915{
1916 struct pxa3xx_nand_platform_data *pdata;
1917 struct device_node *np = pdev->dev.of_node;
1918 const struct of_device_id *of_id =
1919 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1920
1921 if (!of_id)
1922 return 0;
1923
1924 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1925 if (!pdata)
1926 return -ENOMEM;
1927
1928 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1929 pdata->enable_arbiter = 1;
1930 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1931 pdata->keep_config = 1;
1932 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia5b3e5072014-05-14 14:58:08 -03001933
Daniel Mack1e7ba632012-07-22 19:51:02 +02001934 pdev->dev.platform_data = pdata;
1935
1936 return 0;
1937}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001938
Lei Wene353a202011-03-03 11:08:30 +08001939static int pxa3xx_nand_probe(struct platform_device *pdev)
1940{
1941 struct pxa3xx_nand_platform_data *pdata;
1942 struct pxa3xx_nand_info *info;
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001943 int ret, cs, probe_success, dma_available;
Lei Wene353a202011-03-03 11:08:30 +08001944
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001945 dma_available = IS_ENABLED(CONFIG_ARM) &&
1946 (IS_ENABLED(CONFIG_ARCH_PXA) || IS_ENABLED(CONFIG_ARCH_MMP));
1947 if (use_dma && !dma_available) {
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001948 use_dma = 0;
1949 dev_warn(&pdev->dev,
1950 "This platform can't do DMA on this device\n");
1951 }
Robert Jarzmik8f5ba312015-09-06 15:12:47 +02001952
Daniel Mack1e7ba632012-07-22 19:51:02 +02001953 ret = pxa3xx_nand_probe_dt(pdev);
1954 if (ret)
1955 return ret;
1956
Jingoo Han453810b2013-07-30 17:18:33 +09001957 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001958 if (!pdata) {
1959 dev_err(&pdev->dev, "no platform data defined\n");
1960 return -ENODEV;
1961 }
1962
Lei Wend4568822011-07-14 20:44:32 -07001963 ret = alloc_nand_resource(pdev);
1964 if (ret) {
1965 dev_err(&pdev->dev, "alloc nand resource failed\n");
1966 return ret;
1967 }
Lei Wene353a202011-03-03 11:08:30 +08001968
Lei Wend4568822011-07-14 20:44:32 -07001969 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001970 probe_success = 0;
1971 for (cs = 0; cs < pdata->num_cs; cs++) {
Boris BREZILLON063294a2015-12-10 09:00:20 +01001972 struct mtd_info *mtd = nand_to_mtd(&info->host[cs]->chip);
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001973
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001974 /*
1975 * The mtd name matches the one used in 'mtdparts' kernel
1976 * parameter. This name cannot be changed or otherwise
1977 * user's mtd partitions configuration would get broken.
1978 */
1979 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001980 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001981 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001982 if (ret) {
1983 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1984 cs);
1985 continue;
1986 }
1987
Brian Norrisa61ae812015-10-30 20:33:25 -07001988 ret = mtd_device_register(mtd, pdata->parts[cs],
1989 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001990 if (!ret)
1991 probe_success = 1;
1992 }
1993
1994 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001995 pxa3xx_nand_remove(pdev);
1996 return -ENODEV;
1997 }
1998
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001999 return 0;
Lei Wene353a202011-03-03 11:08:30 +08002000}
2001
eric miaofe69af02008-02-14 15:48:23 +08002002#ifdef CONFIG_PM
Brian Norrisd3e94f32015-10-12 14:07:41 -07002003static int pxa3xx_nand_suspend(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08002004{
Brian Norrisd3e94f32015-10-12 14:07:41 -07002005 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
eric miaofe69af02008-02-14 15:48:23 +08002006
Lei Wenf8155a42011-02-28 10:32:11 +08002007 if (info->state) {
Brian Norrisd3e94f32015-10-12 14:07:41 -07002008 dev_err(dev, "driver busy, state = %d\n", info->state);
eric miaofe69af02008-02-14 15:48:23 +08002009 return -EAGAIN;
2010 }
2011
Ezequiel Garcíad55d31a2015-11-04 13:13:46 -03002012 clk_disable(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08002013 return 0;
2014}
2015
Brian Norrisd3e94f32015-10-12 14:07:41 -07002016static int pxa3xx_nand_resume(struct device *dev)
eric miaofe69af02008-02-14 15:48:23 +08002017{
Brian Norrisd3e94f32015-10-12 14:07:41 -07002018 struct pxa3xx_nand_info *info = dev_get_drvdata(dev);
Ezequiel Garcíad55d31a2015-11-04 13:13:46 -03002019 int ret;
2020
2021 ret = clk_enable(info->clk);
2022 if (ret < 0)
2023 return ret;
Lei Wen051fc412011-07-14 20:44:30 -07002024
2025 /* We don't want to handle interrupt without calling mtd routine */
2026 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08002027
Lei Wenf3c8cfc2011-07-14 20:44:33 -07002028 /*
2029 * Directly set the chip select to a invalid value,
2030 * then the driver would reset the timing according
2031 * to current chip select at the beginning of cmdfunc
2032 */
2033 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08002034
Lei Wen051fc412011-07-14 20:44:30 -07002035 /*
2036 * As the spec says, the NDSR would be updated to 0x1800 when
2037 * doing the nand_clk disable/enable.
2038 * To prevent it damaging state machine of the driver, clear
2039 * all status before resume
2040 */
2041 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07002042
Lei Wen18c81b12010-08-17 17:25:57 +08002043 return 0;
eric miaofe69af02008-02-14 15:48:23 +08002044}
2045#else
2046#define pxa3xx_nand_suspend NULL
2047#define pxa3xx_nand_resume NULL
2048#endif
2049
Brian Norrisd3e94f32015-10-12 14:07:41 -07002050static const struct dev_pm_ops pxa3xx_nand_pm_ops = {
2051 .suspend = pxa3xx_nand_suspend,
2052 .resume = pxa3xx_nand_resume,
2053};
2054
eric miaofe69af02008-02-14 15:48:23 +08002055static struct platform_driver pxa3xx_nand_driver = {
2056 .driver = {
2057 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05302058 .of_match_table = pxa3xx_nand_dt_ids,
Brian Norrisd3e94f32015-10-12 14:07:41 -07002059 .pm = &pxa3xx_nand_pm_ops,
eric miaofe69af02008-02-14 15:48:23 +08002060 },
2061 .probe = pxa3xx_nand_probe,
2062 .remove = pxa3xx_nand_remove,
eric miaofe69af02008-02-14 15:48:23 +08002063};
2064
Axel Linf99640d2011-11-27 20:45:03 +08002065module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08002066
2067MODULE_LICENSE("GPL");
2068MODULE_DESCRIPTION("PXA3xx NAND controller driver");