blob: 0fdcc91af1e25a02e0fa8f75a13f707b1ad72f04 [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>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
20#include <linux/clk.h>
21#include <linux/mtd/mtd.h>
22#include <linux/mtd/nand.h>
23#include <linux/mtd/partitions.h>
David Woodhousea1c06ee2008-04-22 20:39:43 +010024#include <linux/io.h>
25#include <linux/irq.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Daniel Mack1e7ba632012-07-22 19:51:02 +020027#include <linux/of.h>
28#include <linux/of_device.h>
Ezequiel Garcia776f2652013-11-14 18:25:28 -030029#include <linux/of_mtd.h>
eric miaofe69af02008-02-14 15:48:23 +080030
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030031#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
32#define ARCH_HAS_DMA
33#endif
34
35#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080036#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030037#endif
38
Arnd Bergmann293b2da2012-08-24 15:16:48 +020039#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080040
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -030041#define NAND_DEV_READY_TIMEOUT 50
eric miaofe69af02008-02-14 15:48:23 +080042#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080043#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080044#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080045
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030046/*
47 * Define a buffer size for the initial command that detects the flash device:
48 * STATUS, READID and PARAM. The largest of these is the PARAM command,
49 * needing 256 bytes.
50 */
51#define INIT_BUFFER_SIZE 256
52
eric miaofe69af02008-02-14 15:48:23 +080053/* registers and bit definitions */
54#define NDCR (0x00) /* Control register */
55#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
56#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
57#define NDSR (0x14) /* Status Register */
58#define NDPCR (0x18) /* Page Count Register */
59#define NDBDR0 (0x1C) /* Bad Block Register 0 */
60#define NDBDR1 (0x20) /* Bad Block Register 1 */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -030061#define NDECCCTRL (0x28) /* ECC control */
eric miaofe69af02008-02-14 15:48:23 +080062#define NDDB (0x40) /* Data Buffer */
63#define NDCB0 (0x48) /* Command Buffer0 */
64#define NDCB1 (0x4C) /* Command Buffer1 */
65#define NDCB2 (0x50) /* Command Buffer2 */
66
67#define NDCR_SPARE_EN (0x1 << 31)
68#define NDCR_ECC_EN (0x1 << 30)
69#define NDCR_DMA_EN (0x1 << 29)
70#define NDCR_ND_RUN (0x1 << 28)
71#define NDCR_DWIDTH_C (0x1 << 27)
72#define NDCR_DWIDTH_M (0x1 << 26)
73#define NDCR_PAGE_SZ (0x1 << 24)
74#define NDCR_NCSX (0x1 << 23)
75#define NDCR_ND_MODE (0x3 << 21)
76#define NDCR_NAND_MODE (0x0)
77#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080078#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080079#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
80#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
81
82#define NDCR_RA_START (0x1 << 15)
83#define NDCR_PG_PER_BLK (0x1 << 14)
84#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080085#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080086
87#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080088#define NDSR_RDY (0x1 << 12)
89#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080090#define NDSR_CS0_PAGED (0x1 << 10)
91#define NDSR_CS1_PAGED (0x1 << 9)
92#define NDSR_CS0_CMDD (0x1 << 8)
93#define NDSR_CS1_CMDD (0x1 << 7)
94#define NDSR_CS0_BBD (0x1 << 6)
95#define NDSR_CS1_BBD (0x1 << 5)
96#define NDSR_DBERR (0x1 << 4)
97#define NDSR_SBERR (0x1 << 3)
98#define NDSR_WRDREQ (0x1 << 2)
99#define NDSR_RDDREQ (0x1 << 1)
100#define NDSR_WRCMDREQ (0x1)
101
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300102#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800103#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800104#define NDCB0_AUTO_RS (0x1 << 25)
105#define NDCB0_CSEL (0x1 << 24)
106#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
107#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
108#define NDCB0_NC (0x1 << 20)
109#define NDCB0_DBC (0x1 << 19)
110#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
111#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
112#define NDCB0_CMD2_MASK (0xff << 8)
113#define NDCB0_CMD1_MASK (0xff)
114#define NDCB0_ADDR_CYC_SHIFT (16)
115
eric miaofe69af02008-02-14 15:48:23 +0800116/* macros for registers read/write */
117#define nand_writel(info, off, val) \
118 __raw_writel((val), (info)->mmio_base + (off))
119
120#define nand_readl(info, off) \
121 __raw_readl((info)->mmio_base + (off))
122
123/* error code and state */
124enum {
125 ERR_NONE = 0,
126 ERR_DMABUSERR = -1,
127 ERR_SENDCMD = -2,
128 ERR_DBERR = -3,
129 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300130 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800131};
132
133enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800134 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700135 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800136 STATE_CMD_HANDLE,
137 STATE_DMA_READING,
138 STATE_DMA_WRITING,
139 STATE_DMA_DONE,
140 STATE_PIO_READING,
141 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800142 STATE_CMD_DONE,
143 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800144};
145
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300146enum pxa3xx_nand_variant {
147 PXA3XX_NAND_VARIANT_PXA,
148 PXA3XX_NAND_VARIANT_ARMADA370,
149};
150
Lei Wend4568822011-07-14 20:44:32 -0700151struct pxa3xx_nand_host {
152 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700153 struct mtd_info *mtd;
154 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800155
Lei Wend4568822011-07-14 20:44:32 -0700156 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700157 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700158 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700159
160 /* calculated from pxa3xx_nand_flash data */
161 unsigned int col_addr_cycles;
162 unsigned int row_addr_cycles;
163 size_t read_id_bytes;
164
Lei Wend4568822011-07-14 20:44:32 -0700165};
166
167struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800168 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800169 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800170
171 struct clk *clk;
172 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800173 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300174 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800175
176 unsigned int buf_start;
177 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300178 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800179
180 /* DMA information */
181 int drcmr_dat;
182 int drcmr_cmd;
183
184 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800185 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800186 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800187 int data_dma_ch;
188 struct pxa_dma_desc *data_desc;
189 dma_addr_t data_desc_addr;
190
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700191 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800192 unsigned int state;
193
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300194 /*
195 * This driver supports NFCv1 (as found in PXA SoC)
196 * and NFCv2 (as found in Armada 370/XP SoC).
197 */
198 enum pxa3xx_nand_variant variant;
199
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700200 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800201 int use_ecc; /* use HW ECC ? */
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300202 int ecc_bch; /* using BCH ECC? */
eric miaofe69af02008-02-14 15:48:23 +0800203 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300204 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300205 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800206
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300207 unsigned int fifo_size; /* max. data size in the FIFO */
208 unsigned int data_size; /* data to be read from FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700209 unsigned int oob_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300210 unsigned int spare_size;
211 unsigned int ecc_size;
eric miaofe69af02008-02-14 15:48:23 +0800212 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800213
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300214 /* cached register value */
215 uint32_t reg_ndcr;
216 uint32_t ndtr0cs0;
217 uint32_t ndtr1cs0;
218
eric miaofe69af02008-02-14 15:48:23 +0800219 /* generated NDCBx register values */
220 uint32_t ndcb0;
221 uint32_t ndcb1;
222 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300223 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800224};
225
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030226static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800227module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300228MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800229
Lei Wenc1f82472010-08-17 13:50:23 +0800230static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800231 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
232 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
233 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
234 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800235};
236
Lei Wenc1f82472010-08-17 13:50:23 +0800237static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800238{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
239{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
240{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
241{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
242{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
243{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
244{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
245{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
246{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800247};
248
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300249static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
250static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
251
252static struct nand_bbt_descr bbt_main_descr = {
253 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
254 | NAND_BBT_2BIT | NAND_BBT_VERSION,
255 .offs = 8,
256 .len = 6,
257 .veroffs = 14,
258 .maxblocks = 8, /* Last 8 blocks in each chip */
259 .pattern = bbt_pattern
260};
261
262static struct nand_bbt_descr bbt_mirror_descr = {
263 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
264 | NAND_BBT_2BIT | NAND_BBT_VERSION,
265 .offs = 8,
266 .len = 6,
267 .veroffs = 14,
268 .maxblocks = 8, /* Last 8 blocks in each chip */
269 .pattern = bbt_mirror_pattern
270};
271
Lei Wen227a8862010-08-18 18:00:03 +0800272/* Define a default flash type setting serve as flash detecting only */
273#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
274
eric miaofe69af02008-02-14 15:48:23 +0800275#define NDTR0_tCH(c) (min((c), 7) << 19)
276#define NDTR0_tCS(c) (min((c), 7) << 16)
277#define NDTR0_tWH(c) (min((c), 7) << 11)
278#define NDTR0_tWP(c) (min((c), 7) << 8)
279#define NDTR0_tRH(c) (min((c), 7) << 3)
280#define NDTR0_tRP(c) (min((c), 7) << 0)
281
282#define NDTR1_tR(c) (min((c), 65535) << 16)
283#define NDTR1_tWHR(c) (min((c), 15) << 4)
284#define NDTR1_tAR(c) (min((c), 15) << 0)
285
286/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800287#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800288
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300289static struct of_device_id pxa3xx_nand_dt_ids[] = {
290 {
291 .compatible = "marvell,pxa3xx-nand",
292 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
293 },
294 {}
295};
296MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
297
298static enum pxa3xx_nand_variant
299pxa3xx_nand_get_variant(struct platform_device *pdev)
300{
301 const struct of_device_id *of_id =
302 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
303 if (!of_id)
304 return PXA3XX_NAND_VARIANT_PXA;
305 return (enum pxa3xx_nand_variant)of_id->data;
306}
307
Lei Wend4568822011-07-14 20:44:32 -0700308static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200309 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800310{
Lei Wend4568822011-07-14 20:44:32 -0700311 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800312 unsigned long nand_clk = clk_get_rate(info->clk);
313 uint32_t ndtr0, ndtr1;
314
315 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
316 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
317 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
318 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
319 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
320 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
321
322 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
323 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
324 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
325
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300326 info->ndtr0cs0 = ndtr0;
327 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800328 nand_writel(info, NDTR0CS0, ndtr0);
329 nand_writel(info, NDTR1CS0, ndtr1);
330}
331
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300332/*
333 * Set the data and OOB size, depending on the selected
334 * spare and ECC configuration.
335 * Only applicable to READ0, READOOB and PAGEPROG commands.
336 */
Lei Wen18c81b12010-08-17 17:25:57 +0800337static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800338{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300339 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800340
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300341 info->data_size = info->fifo_size;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300342 if (!oob_enable)
Lei Wen9d8b1042010-08-17 14:09:30 +0800343 return;
Lei Wen9d8b1042010-08-17 14:09:30 +0800344
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300345 info->oob_size = info->spare_size;
346 if (!info->use_ecc)
347 info->oob_size += info->ecc_size;
Lei Wen18c81b12010-08-17 17:25:57 +0800348}
349
Lei Wenf8155a42011-02-28 10:32:11 +0800350/**
351 * NOTE: it is a must to set ND_RUN firstly, then write
352 * command buffer, otherwise, it does not work.
353 * We enable all the interrupt at the same time, and
354 * let pxa3xx_nand_irq to handle all logic.
355 */
356static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
357{
358 uint32_t ndcr;
359
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300360 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300361
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300362 if (info->use_ecc) {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300363 ndcr |= NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300364 if (info->ecc_bch)
365 nand_writel(info, NDECCCTRL, 0x1);
366 } else {
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300367 ndcr &= ~NDCR_ECC_EN;
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -0300368 if (info->ecc_bch)
369 nand_writel(info, NDECCCTRL, 0x0);
370 }
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300371
372 if (info->use_dma)
373 ndcr |= NDCR_DMA_EN;
374 else
375 ndcr &= ~NDCR_DMA_EN;
376
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300377 if (info->use_spare)
378 ndcr |= NDCR_SPARE_EN;
379 else
380 ndcr &= ~NDCR_SPARE_EN;
381
Lei Wenf8155a42011-02-28 10:32:11 +0800382 ndcr |= NDCR_ND_RUN;
383
384 /* clear status bits and run */
385 nand_writel(info, NDCR, 0);
386 nand_writel(info, NDSR, NDSR_MASK);
387 nand_writel(info, NDCR, ndcr);
388}
389
390static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
391{
392 uint32_t ndcr;
393 int timeout = NAND_STOP_DELAY;
394
395 /* wait RUN bit in NDCR become 0 */
396 ndcr = nand_readl(info, NDCR);
397 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
398 ndcr = nand_readl(info, NDCR);
399 udelay(1);
400 }
401
402 if (timeout <= 0) {
403 ndcr &= ~NDCR_ND_RUN;
404 nand_writel(info, NDCR, ndcr);
405 }
406 /* clear status bits */
407 nand_writel(info, NDSR, NDSR_MASK);
408}
409
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300410static void __maybe_unused
411enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800412{
413 uint32_t ndcr;
414
415 ndcr = nand_readl(info, NDCR);
416 nand_writel(info, NDCR, ndcr & ~int_mask);
417}
418
419static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
420{
421 uint32_t ndcr;
422
423 ndcr = nand_readl(info, NDCR);
424 nand_writel(info, NDCR, ndcr | int_mask);
425}
426
Lei Wenf8155a42011-02-28 10:32:11 +0800427static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800428{
eric miaofe69af02008-02-14 15:48:23 +0800429 switch (info->state) {
430 case STATE_PIO_WRITING:
431 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800432 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800433 if (info->oob_size > 0)
434 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
435 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800436 break;
437 case STATE_PIO_READING:
438 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800439 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800440 if (info->oob_size > 0)
441 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
442 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800443 break;
444 default:
Lei Wenda675b42011-07-14 20:44:31 -0700445 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800446 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800447 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800448 }
eric miaofe69af02008-02-14 15:48:23 +0800449}
450
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300451#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800452static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800453{
454 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800455 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800456
457 desc->ddadr = DDADR_STOP;
458 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
459
Lei Wenf8155a42011-02-28 10:32:11 +0800460 switch (info->state) {
461 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800462 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800463 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800464 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800465 break;
466 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800467 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800468 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800469 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800470 break;
471 default:
Lei Wenda675b42011-07-14 20:44:31 -0700472 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800473 info->state);
474 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800475 }
476
477 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
478 DDADR(info->data_dma_ch) = info->data_desc_addr;
479 DCSR(info->data_dma_ch) |= DCSR_RUN;
480}
481
482static void pxa3xx_nand_data_dma_irq(int channel, void *data)
483{
484 struct pxa3xx_nand_info *info = data;
485 uint32_t dcsr;
486
487 dcsr = DCSR(channel);
488 DCSR(channel) = dcsr;
489
490 if (dcsr & DCSR_BUSERR) {
491 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800492 }
493
Lei Wenf8155a42011-02-28 10:32:11 +0800494 info->state = STATE_DMA_DONE;
495 enable_int(info, NDCR_INT_MASK);
496 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800497}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300498#else
499static void start_data_dma(struct pxa3xx_nand_info *info)
500{}
501#endif
eric miaofe69af02008-02-14 15:48:23 +0800502
503static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
504{
505 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300506 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700507 unsigned int ready, cmd_done;
508
509 if (info->cs == 0) {
510 ready = NDSR_FLASH_RDY;
511 cmd_done = NDSR_CS0_CMDD;
512 } else {
513 ready = NDSR_RDY;
514 cmd_done = NDSR_CS1_CMDD;
515 }
eric miaofe69af02008-02-14 15:48:23 +0800516
517 status = nand_readl(info, NDSR);
518
Lei Wenf8155a42011-02-28 10:32:11 +0800519 if (status & NDSR_DBERR)
520 info->retcode = ERR_DBERR;
521 if (status & NDSR_SBERR)
522 info->retcode = ERR_SBERR;
523 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
524 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800525 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800526 disable_int(info, NDCR_INT_MASK);
527 info->state = (status & NDSR_RDDREQ) ?
528 STATE_DMA_READING : STATE_DMA_WRITING;
529 start_data_dma(info);
530 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800531 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800532 info->state = (status & NDSR_RDDREQ) ?
533 STATE_PIO_READING : STATE_PIO_WRITING;
534 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800535 }
Lei Wenf8155a42011-02-28 10:32:11 +0800536 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700537 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800538 info->state = STATE_CMD_DONE;
539 is_completed = 1;
540 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700541 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800542 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300543 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800544 }
Lei Wenf8155a42011-02-28 10:32:11 +0800545
546 if (status & NDSR_WRCMDREQ) {
547 nand_writel(info, NDSR, NDSR_WRCMDREQ);
548 status &= ~NDSR_WRCMDREQ;
549 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300550
551 /*
552 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
553 * must be loaded by writing directly either 12 or 16
554 * bytes directly to NDCB0, four bytes at a time.
555 *
556 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
557 * but each NDCBx register can be read.
558 */
Lei Wenf8155a42011-02-28 10:32:11 +0800559 nand_writel(info, NDCB0, info->ndcb0);
560 nand_writel(info, NDCB0, info->ndcb1);
561 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300562
563 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
564 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
565 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800566 }
Lei Wenf8155a42011-02-28 10:32:11 +0800567
568 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800569 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800570 if (is_completed)
571 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300572 if (is_ready)
573 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800574NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800575 return IRQ_HANDLED;
576}
577
eric miaofe69af02008-02-14 15:48:23 +0800578static inline int is_buf_blank(uint8_t *buf, size_t len)
579{
580 for (; len > 0; len--)
581 if (*buf++ != 0xff)
582 return 0;
583 return 1;
584}
585
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300586static void set_command_address(struct pxa3xx_nand_info *info,
587 unsigned int page_size, uint16_t column, int page_addr)
588{
589 /* small page addr setting */
590 if (page_size < PAGE_CHUNK_SIZE) {
591 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
592 | (column & 0xFF);
593
594 info->ndcb2 = 0;
595 } else {
596 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
597 | (column & 0xFFFF);
598
599 if (page_addr & 0xFF0000)
600 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
601 else
602 info->ndcb2 = 0;
603 }
604}
605
Lei Wen4eb2da82011-02-28 10:32:13 +0800606static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
607 uint16_t column, int page_addr)
608{
Lei Wend4568822011-07-14 20:44:32 -0700609 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700610 struct pxa3xx_nand_host *host;
611 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800612
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700613 host = info->host[info->cs];
614 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800615 addr_cycle = 0;
616 exec_cmd = 1;
617
618 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800619 info->buf_start = 0;
620 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800621 info->oob_size = 0;
622 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300623 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800624 info->retcode = ERR_NONE;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300625 info->ndcb3 = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700626 if (info->cs != 0)
627 info->ndcb0 = NDCB0_CSEL;
628 else
629 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800630
631 switch (command) {
632 case NAND_CMD_READ0:
633 case NAND_CMD_PAGEPROG:
634 info->use_ecc = 1;
635 case NAND_CMD_READOOB:
636 pxa3xx_set_datasize(info);
637 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300638 case NAND_CMD_PARAM:
639 info->use_spare = 0;
640 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800641 case NAND_CMD_SEQIN:
642 exec_cmd = 0;
643 break;
644 default:
645 info->ndcb1 = 0;
646 info->ndcb2 = 0;
647 break;
648 }
649
Lei Wend4568822011-07-14 20:44:32 -0700650 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
651 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800652
653 switch (command) {
654 case NAND_CMD_READOOB:
655 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300656 info->buf_start = column;
657 info->ndcb0 |= NDCB0_CMD_TYPE(0)
658 | addr_cycle
659 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800660
Ezequiel Garciaec821352013-08-12 14:14:54 -0300661 if (command == NAND_CMD_READOOB)
662 info->buf_start += mtd->writesize;
663
664 /* Second command setting for large pages */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300665 if (mtd->writesize >= PAGE_CHUNK_SIZE)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300666 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800667
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300668 set_command_address(info, mtd->writesize, column, page_addr);
669 info->buf_count = mtd->writesize + mtd->oobsize;
670 memset(info->data_buff, 0xFF, info->buf_count);
671 break;
672
Lei Wen4eb2da82011-02-28 10:32:13 +0800673 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800674
Ezequiel Garcia86beeba2013-11-14 18:25:31 -0300675 set_command_address(info, mtd->writesize, column, page_addr);
Lei Wen4eb2da82011-02-28 10:32:13 +0800676 info->buf_count = mtd->writesize + mtd->oobsize;
677 memset(info->data_buff, 0xFF, info->buf_count);
678
679 break;
680
681 case NAND_CMD_PAGEPROG:
682 if (is_buf_blank(info->data_buff,
683 (mtd->writesize + mtd->oobsize))) {
684 exec_cmd = 0;
685 break;
686 }
687
Lei Wen4eb2da82011-02-28 10:32:13 +0800688 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
689 | NDCB0_AUTO_RS
690 | NDCB0_ST_ROW_EN
691 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300692 | (NAND_CMD_PAGEPROG << 8)
693 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800694 | addr_cycle;
695 break;
696
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300697 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300698 info->buf_count = 256;
699 info->ndcb0 |= NDCB0_CMD_TYPE(0)
700 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300701 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300702 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300703 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300704 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300705 info->data_size = 256;
706 break;
707
Lei Wen4eb2da82011-02-28 10:32:13 +0800708 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700709 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800710 info->ndcb0 |= NDCB0_CMD_TYPE(3)
711 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300712 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300713 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800714
715 info->data_size = 8;
716 break;
717 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800718 info->buf_count = 1;
719 info->ndcb0 |= NDCB0_CMD_TYPE(4)
720 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300721 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800722
723 info->data_size = 8;
724 break;
725
726 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800727 info->ndcb0 |= NDCB0_CMD_TYPE(2)
728 | NDCB0_AUTO_RS
729 | NDCB0_ADDR_CYC(3)
730 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300731 | (NAND_CMD_ERASE2 << 8)
732 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800733 info->ndcb1 = page_addr;
734 info->ndcb2 = 0;
735
736 break;
737 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800738 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300739 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800740
741 break;
742
743 case NAND_CMD_ERASE2:
744 exec_cmd = 0;
745 break;
746
747 default:
748 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700749 dev_err(&info->pdev->dev, "non-supported command %x\n",
750 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800751 break;
752 }
753
754 return exec_cmd;
755}
756
eric miaofe69af02008-02-14 15:48:23 +0800757static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100758 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800759{
Lei Wend4568822011-07-14 20:44:32 -0700760 struct pxa3xx_nand_host *host = mtd->priv;
761 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800762 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800763
Lei Wen4eb2da82011-02-28 10:32:13 +0800764 /*
765 * if this is a x16 device ,then convert the input
766 * "byte" address into a "word" address appropriate
767 * for indexing a word-oriented device
768 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300769 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800770 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800771
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700772 /*
773 * There may be different NAND chip hooked to
774 * different chip select, so check whether
775 * chip select has been changed, if yes, reset the timing
776 */
777 if (info->cs != host->cs) {
778 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300779 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
780 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700781 }
782
Lei Wend4568822011-07-14 20:44:32 -0700783 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800784 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800785 if (exec_cmd) {
786 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300787 init_completion(&info->dev_ready);
788 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800789 pxa3xx_nand_start(info);
790
791 ret = wait_for_completion_timeout(&info->cmd_complete,
792 CHIP_DELAY_TIMEOUT);
793 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700794 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800795 /* Stop State Machine for next command cycle */
796 pxa3xx_nand_stop(info);
797 }
eric miaofe69af02008-02-14 15:48:23 +0800798 }
Lei Wend4568822011-07-14 20:44:32 -0700799 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800800}
801
Josh Wufdbad98d2012-06-25 18:07:45 +0800802static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700803 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800804{
805 chip->write_buf(mtd, buf, mtd->writesize);
806 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800807
808 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800809}
810
811static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700812 struct nand_chip *chip, uint8_t *buf, int oob_required,
813 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800814{
Lei Wend4568822011-07-14 20:44:32 -0700815 struct pxa3xx_nand_host *host = mtd->priv;
816 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300817 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800818
819 chip->read_buf(mtd, buf, mtd->writesize);
820 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
821
822 if (info->retcode == ERR_SBERR) {
823 switch (info->use_ecc) {
824 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300825 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800826 mtd->ecc_stats.corrected++;
827 break;
828 case 0:
829 default:
830 break;
831 }
832 } else if (info->retcode == ERR_DBERR) {
833 /*
834 * for blank page (all 0xff), HW will calculate its ECC as
835 * 0, which is different from the ECC information within
836 * OOB, ignore such double bit errors
837 */
838 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700839 info->retcode = ERR_NONE;
840 else
Lei Wenf8155a42011-02-28 10:32:11 +0800841 mtd->ecc_stats.failed++;
842 }
843
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300844 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800845}
846
eric miaofe69af02008-02-14 15:48:23 +0800847static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
848{
Lei Wend4568822011-07-14 20:44:32 -0700849 struct pxa3xx_nand_host *host = mtd->priv;
850 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800851 char retval = 0xFF;
852
853 if (info->buf_start < info->buf_count)
854 /* Has just send a new command? */
855 retval = info->data_buff[info->buf_start++];
856
857 return retval;
858}
859
860static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
861{
Lei Wend4568822011-07-14 20:44:32 -0700862 struct pxa3xx_nand_host *host = mtd->priv;
863 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800864 u16 retval = 0xFFFF;
865
866 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
867 retval = *((u16 *)(info->data_buff+info->buf_start));
868 info->buf_start += 2;
869 }
870 return retval;
871}
872
873static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
874{
Lei Wend4568822011-07-14 20:44:32 -0700875 struct pxa3xx_nand_host *host = mtd->priv;
876 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800877 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
878
879 memcpy(buf, info->data_buff + info->buf_start, real_len);
880 info->buf_start += real_len;
881}
882
883static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
884 const uint8_t *buf, int len)
885{
Lei Wend4568822011-07-14 20:44:32 -0700886 struct pxa3xx_nand_host *host = mtd->priv;
887 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800888 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
889
890 memcpy(info->data_buff + info->buf_start, buf, real_len);
891 info->buf_start += real_len;
892}
893
eric miaofe69af02008-02-14 15:48:23 +0800894static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
895{
896 return;
897}
898
899static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
900{
Lei Wend4568822011-07-14 20:44:32 -0700901 struct pxa3xx_nand_host *host = mtd->priv;
902 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300903 int ret;
904
905 if (info->need_wait) {
906 ret = wait_for_completion_timeout(&info->dev_ready,
907 CHIP_DELAY_TIMEOUT);
908 info->need_wait = 0;
909 if (!ret) {
910 dev_err(&info->pdev->dev, "Ready time out!!!\n");
911 return NAND_STATUS_FAIL;
912 }
913 }
eric miaofe69af02008-02-14 15:48:23 +0800914
915 /* pxa3xx_nand_send_command has waited for command complete */
916 if (this->state == FL_WRITING || this->state == FL_ERASING) {
917 if (info->retcode == ERR_NONE)
918 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300919 else
920 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +0800921 }
922
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300923 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +0800924}
925
eric miaofe69af02008-02-14 15:48:23 +0800926static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200927 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800928{
929 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900930 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700931 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800932 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800933
Lei Wenda675b42011-07-14 20:44:31 -0700934 if (f->page_size != 2048 && f->page_size != 512) {
935 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800936 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700937 }
eric miaofe69af02008-02-14 15:48:23 +0800938
Lei Wenda675b42011-07-14 20:44:31 -0700939 if (f->flash_width != 16 && f->flash_width != 8) {
940 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800941 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700942 }
eric miaofe69af02008-02-14 15:48:23 +0800943
944 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700945 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800946
947 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700948 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800949
950 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700951 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800952 else
Lei Wend4568822011-07-14 20:44:32 -0700953 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800954
955 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700956 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800957 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
958 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
959 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
960 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
961
Lei Wend4568822011-07-14 20:44:32 -0700962 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800963 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
964
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300965 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800966
Lei Wend4568822011-07-14 20:44:32 -0700967 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800968 return 0;
969}
970
Mike Rapoportf2710492009-02-17 13:54:47 +0200971static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
972{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700973 /*
974 * We set 0 by hard coding here, for we don't support keep_config
975 * when there is more than one chip attached to the controller
976 */
977 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200978 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200979
Lei Wend4568822011-07-14 20:44:32 -0700980 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300981 /* Controller's FIFO size */
982 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -0700983 host->read_id_bytes = 4;
984 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300985 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -0700986 host->read_id_bytes = 2;
987 }
988
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300989 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
990 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
991 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200992 return 0;
993}
994
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300995#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800996static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
997{
998 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300999 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001000
1001 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001002 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001003 if (info->data_buff == NULL)
1004 return -ENOMEM;
1005 return 0;
1006 }
1007
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001008 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001009 &info->data_buff_phys, GFP_KERNEL);
1010 if (info->data_buff == NULL) {
1011 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1012 return -ENOMEM;
1013 }
1014
eric miaofe69af02008-02-14 15:48:23 +08001015 info->data_desc = (void *)info->data_buff + data_desc_offset;
1016 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1017
1018 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1019 pxa3xx_nand_data_dma_irq, info);
1020 if (info->data_dma_ch < 0) {
1021 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001022 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001023 info->data_buff, info->data_buff_phys);
1024 return info->data_dma_ch;
1025 }
1026
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001027 /*
1028 * Now that DMA buffers are allocated we turn on
1029 * DMA proper for I/O operations.
1030 */
1031 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001032 return 0;
1033}
1034
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001035static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1036{
1037 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001038 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001039 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001040 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001041 info->data_buff, info->data_buff_phys);
1042 } else {
1043 kfree(info->data_buff);
1044 }
1045}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001046#else
1047static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1048{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001049 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001050 if (info->data_buff == NULL)
1051 return -ENOMEM;
1052 return 0;
1053}
1054
1055static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1056{
1057 kfree(info->data_buff);
1058}
1059#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001060
Lei Wen401e67e2011-02-28 10:32:14 +08001061static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001062{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001063 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001064 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001065 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001066
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001067 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001068 chip = mtd->priv;
1069
Lei Wen401e67e2011-02-28 10:32:14 +08001070 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001071 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1072 if (ret)
1073 return ret;
1074
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001075 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001076 ret = chip->waitfunc(mtd, chip);
1077 if (ret & NAND_STATUS_FAIL)
1078 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001079
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001080 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001081}
eric miaofe69af02008-02-14 15:48:23 +08001082
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001083static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1084 struct nand_ecc_ctrl *ecc,
1085 int strength, int page_size)
1086{
1087 /*
1088 * We don't use strength here as the PXA variant
1089 * is used with non-ONFI compliant devices.
1090 */
1091 if (page_size == 2048) {
1092 info->spare_size = 40;
1093 info->ecc_size = 24;
1094 ecc->mode = NAND_ECC_HW;
1095 ecc->size = 512;
1096 ecc->strength = 1;
1097 return 1;
1098
1099 } else if (page_size == 512) {
1100 info->spare_size = 8;
1101 info->ecc_size = 8;
1102 ecc->mode = NAND_ECC_HW;
1103 ecc->size = 512;
1104 ecc->strength = 1;
1105 return 1;
1106 }
1107 return 0;
1108}
1109
1110static int armada370_ecc_init(struct pxa3xx_nand_info *info,
1111 struct nand_ecc_ctrl *ecc,
1112 int strength, int page_size)
1113{
1114 /* Unimplemented yet */
1115 return 0;
1116}
1117
Lei Wen401e67e2011-02-28 10:32:14 +08001118static int pxa3xx_nand_scan(struct mtd_info *mtd)
1119{
Lei Wend4568822011-07-14 20:44:32 -07001120 struct pxa3xx_nand_host *host = mtd->priv;
1121 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001122 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001123 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001124 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001125 const struct pxa3xx_nand_flash *f = NULL;
1126 struct nand_chip *chip = mtd->priv;
1127 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001128 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001129 int i, ret, num;
1130
1131 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001132 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001133
1134 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001135 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001136 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1137 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001138
Lei Wend4568822011-07-14 20:44:32 -07001139 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001140 }
1141
1142 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1143 id = *((uint16_t *)(info->data_buff));
1144 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001145 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001146 else {
Lei Wenda675b42011-07-14 20:44:31 -07001147 dev_warn(&info->pdev->dev,
1148 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001149
1150 return -EINVAL;
1151 }
1152
1153 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1154 for (i = 0; i < num; i++) {
1155 if (i < pdata->num_flash)
1156 f = pdata->flash + i;
1157 else
1158 f = &builtin_flash_types[i - pdata->num_flash + 1];
1159
1160 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001161 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001162 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001163 }
1164
Lei Wen4332c112011-03-03 11:27:01 +08001165 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001166 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001167
1168 return -EINVAL;
1169 }
1170
Lei Wend4568822011-07-14 20:44:32 -07001171 ret = pxa3xx_nand_config_flash(info, f);
1172 if (ret) {
1173 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1174 return ret;
1175 }
1176
Lei Wen4332c112011-03-03 11:27:01 +08001177 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001178 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001179 pxa3xx_flash_ids[0].pagesize = f->page_size;
1180 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1181 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1182 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1183 if (f->flash_width == 16)
1184 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001185 pxa3xx_flash_ids[1].name = NULL;
1186 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001187KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001188 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001189 chip->options |= NAND_BUSWIDTH_16;
1190
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001191 /* Device detection must be done with ECC disabled */
1192 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1193 nand_writel(info, NDECCCTRL, 0x0);
1194
Lei Wen0fab0282011-06-07 03:01:06 -07001195 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001196 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001197
1198 if (pdata->flash_bbt) {
1199 /*
1200 * We'll use a bad block table stored in-flash and don't
1201 * allow writing the bad block marker to the flash.
1202 */
1203 chip->bbt_options |= NAND_BBT_USE_FLASH |
1204 NAND_BBT_NO_OOB_BBM;
1205 chip->bbt_td = &bbt_main_descr;
1206 chip->bbt_md = &bbt_mirror_descr;
1207 }
1208
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001209 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1210 ret = armada370_ecc_init(info, &chip->ecc,
1211 chip->ecc_strength_ds,
1212 mtd->writesize);
1213 else
1214 ret = pxa_ecc_init(info, &chip->ecc,
1215 chip->ecc_strength_ds,
1216 mtd->writesize);
1217 if (!ret) {
1218 dev_err(&info->pdev->dev,
1219 "ECC strength %d at page size %d is not supported\n",
1220 chip->ecc_strength_ds, mtd->writesize);
1221 return -ENODEV;
1222 }
1223
Lei Wen4332c112011-03-03 11:27:01 +08001224 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001225 if (mtd->writesize >= 2048)
1226 host->col_addr_cycles = 2;
1227 else
1228 host->col_addr_cycles = 1;
1229
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001230 /* release the initial buffer */
1231 kfree(info->data_buff);
1232
1233 /* allocate the real data + oob buffer */
1234 info->buf_size = mtd->writesize + mtd->oobsize;
1235 ret = pxa3xx_nand_init_buff(info);
1236 if (ret)
1237 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001238 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001239
Lei Wen4332c112011-03-03 11:27:01 +08001240 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001241 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001242 else
Lei Wend4568822011-07-14 20:44:32 -07001243 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001244 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001245}
1246
Lei Wend4568822011-07-14 20:44:32 -07001247static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001248{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001249 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001250 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001251 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001252 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001253 struct mtd_info *mtd;
1254 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001255 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001256
Jingoo Han453810b2013-07-30 17:18:33 +09001257 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001258 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1259 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1260 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001261 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001262
eric miaofe69af02008-02-14 15:48:23 +08001263 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001264 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001265 for (cs = 0; cs < pdata->num_cs; cs++) {
1266 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1267 (sizeof(*mtd) + sizeof(*host)) * cs);
1268 chip = (struct nand_chip *)(&mtd[1]);
1269 host = (struct pxa3xx_nand_host *)chip;
1270 info->host[cs] = host;
1271 host->mtd = mtd;
1272 host->cs = cs;
1273 host->info_data = info;
1274 mtd->priv = host;
1275 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001276
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001277 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1278 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1279 chip->controller = &info->controller;
1280 chip->waitfunc = pxa3xx_nand_waitfunc;
1281 chip->select_chip = pxa3xx_nand_select_chip;
1282 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1283 chip->read_word = pxa3xx_nand_read_word;
1284 chip->read_byte = pxa3xx_nand_read_byte;
1285 chip->read_buf = pxa3xx_nand_read_buf;
1286 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001287 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001288 }
Lei Wen401e67e2011-02-28 10:32:14 +08001289
1290 spin_lock_init(&chip->controller->lock);
1291 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001292 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001293 if (IS_ERR(info->clk)) {
1294 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001295 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001296 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001297 ret = clk_prepare_enable(info->clk);
1298 if (ret < 0)
1299 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001300
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001301 if (use_dma) {
1302 /*
1303 * This is a dirty hack to make this driver work from
1304 * devicetree bindings. It can be removed once we have
1305 * a prober DMA controller framework for DT.
1306 */
1307 if (pdev->dev.of_node &&
1308 of_machine_is_compatible("marvell,pxa3xx")) {
1309 info->drcmr_dat = 97;
1310 info->drcmr_cmd = 99;
1311 } else {
1312 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1313 if (r == NULL) {
1314 dev_err(&pdev->dev,
1315 "no resource defined for data DMA\n");
1316 ret = -ENXIO;
1317 goto fail_disable_clk;
1318 }
1319 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001320
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001321 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1322 if (r == NULL) {
1323 dev_err(&pdev->dev,
1324 "no resource defined for cmd DMA\n");
1325 ret = -ENXIO;
1326 goto fail_disable_clk;
1327 }
1328 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001329 }
eric miaofe69af02008-02-14 15:48:23 +08001330 }
eric miaofe69af02008-02-14 15:48:23 +08001331
1332 irq = platform_get_irq(pdev, 0);
1333 if (irq < 0) {
1334 dev_err(&pdev->dev, "no IRQ resource defined\n");
1335 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001336 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001337 }
1338
1339 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001340 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1341 if (IS_ERR(info->mmio_base)) {
1342 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001343 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001344 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001345 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001346
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001347 /* Allocate a buffer to allow flash detection */
1348 info->buf_size = INIT_BUFFER_SIZE;
1349 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1350 if (info->data_buff == NULL) {
1351 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001352 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001353 }
eric miaofe69af02008-02-14 15:48:23 +08001354
Haojian Zhuang346e1252009-09-10 14:27:23 +08001355 /* initialize all interrupts to be disabled */
1356 disable_int(info, NDSR_MASK);
1357
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001358 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001359 if (ret < 0) {
1360 dev_err(&pdev->dev, "failed to request IRQ\n");
1361 goto fail_free_buf;
1362 }
1363
Lei Wene353a202011-03-03 11:08:30 +08001364 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001365
Lei Wend4568822011-07-14 20:44:32 -07001366 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001367
eric miaofe69af02008-02-14 15:48:23 +08001368fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001369 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001370 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001371fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001372 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001373 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001374}
1375
1376static int pxa3xx_nand_remove(struct platform_device *pdev)
1377{
Lei Wene353a202011-03-03 11:08:30 +08001378 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001379 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001380 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001381
Lei Wend4568822011-07-14 20:44:32 -07001382 if (!info)
1383 return 0;
1384
Jingoo Han453810b2013-07-30 17:18:33 +09001385 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001386
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001387 irq = platform_get_irq(pdev, 0);
1388 if (irq >= 0)
1389 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001390 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001391
Ezequiel Garciafb320612013-04-17 13:38:12 -03001392 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001393
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001394 for (cs = 0; cs < pdata->num_cs; cs++)
1395 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001396 return 0;
1397}
1398
Daniel Mack1e7ba632012-07-22 19:51:02 +02001399static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1400{
1401 struct pxa3xx_nand_platform_data *pdata;
1402 struct device_node *np = pdev->dev.of_node;
1403 const struct of_device_id *of_id =
1404 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1405
1406 if (!of_id)
1407 return 0;
1408
1409 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1410 if (!pdata)
1411 return -ENOMEM;
1412
1413 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1414 pdata->enable_arbiter = 1;
1415 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1416 pdata->keep_config = 1;
1417 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001418 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001419
1420 pdev->dev.platform_data = pdata;
1421
1422 return 0;
1423}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001424
Lei Wene353a202011-03-03 11:08:30 +08001425static int pxa3xx_nand_probe(struct platform_device *pdev)
1426{
1427 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001428 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001429 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001430 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001431
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001432#ifndef ARCH_HAS_DMA
1433 if (use_dma) {
1434 use_dma = 0;
1435 dev_warn(&pdev->dev,
1436 "This platform can't do DMA on this device\n");
1437 }
1438#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001439 ret = pxa3xx_nand_probe_dt(pdev);
1440 if (ret)
1441 return ret;
1442
Jingoo Han453810b2013-07-30 17:18:33 +09001443 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001444 if (!pdata) {
1445 dev_err(&pdev->dev, "no platform data defined\n");
1446 return -ENODEV;
1447 }
1448
Lei Wend4568822011-07-14 20:44:32 -07001449 ret = alloc_nand_resource(pdev);
1450 if (ret) {
1451 dev_err(&pdev->dev, "alloc nand resource failed\n");
1452 return ret;
1453 }
Lei Wene353a202011-03-03 11:08:30 +08001454
Lei Wend4568822011-07-14 20:44:32 -07001455 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001456 probe_success = 0;
1457 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001458 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001459
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001460 /*
1461 * The mtd name matches the one used in 'mtdparts' kernel
1462 * parameter. This name cannot be changed or otherwise
1463 * user's mtd partitions configuration would get broken.
1464 */
1465 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001466 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001467 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001468 if (ret) {
1469 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1470 cs);
1471 continue;
1472 }
1473
Daniel Mack1e7ba632012-07-22 19:51:02 +02001474 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001475 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001476 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001477 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001478 if (!ret)
1479 probe_success = 1;
1480 }
1481
1482 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001483 pxa3xx_nand_remove(pdev);
1484 return -ENODEV;
1485 }
1486
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001487 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001488}
1489
eric miaofe69af02008-02-14 15:48:23 +08001490#ifdef CONFIG_PM
1491static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1492{
Lei Wene353a202011-03-03 11:08:30 +08001493 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001494 struct pxa3xx_nand_platform_data *pdata;
1495 struct mtd_info *mtd;
1496 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001497
Jingoo Han453810b2013-07-30 17:18:33 +09001498 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001499 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001500 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1501 return -EAGAIN;
1502 }
1503
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001504 for (cs = 0; cs < pdata->num_cs; cs++) {
1505 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001506 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001507 }
1508
eric miaofe69af02008-02-14 15:48:23 +08001509 return 0;
1510}
1511
1512static int pxa3xx_nand_resume(struct platform_device *pdev)
1513{
Lei Wene353a202011-03-03 11:08:30 +08001514 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001515 struct pxa3xx_nand_platform_data *pdata;
1516 struct mtd_info *mtd;
1517 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001518
Jingoo Han453810b2013-07-30 17:18:33 +09001519 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001520 /* We don't want to handle interrupt without calling mtd routine */
1521 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001522
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001523 /*
1524 * Directly set the chip select to a invalid value,
1525 * then the driver would reset the timing according
1526 * to current chip select at the beginning of cmdfunc
1527 */
1528 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001529
Lei Wen051fc412011-07-14 20:44:30 -07001530 /*
1531 * As the spec says, the NDSR would be updated to 0x1800 when
1532 * doing the nand_clk disable/enable.
1533 * To prevent it damaging state machine of the driver, clear
1534 * all status before resume
1535 */
1536 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001537 for (cs = 0; cs < pdata->num_cs; cs++) {
1538 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001539 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001540 }
1541
Lei Wen18c81b12010-08-17 17:25:57 +08001542 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001543}
1544#else
1545#define pxa3xx_nand_suspend NULL
1546#define pxa3xx_nand_resume NULL
1547#endif
1548
1549static struct platform_driver pxa3xx_nand_driver = {
1550 .driver = {
1551 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301552 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001553 },
1554 .probe = pxa3xx_nand_probe,
1555 .remove = pxa3xx_nand_remove,
1556 .suspend = pxa3xx_nand_suspend,
1557 .resume = pxa3xx_nand_resume,
1558};
1559
Axel Linf99640d2011-11-27 20:45:03 +08001560module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001561
1562MODULE_LICENSE("GPL");
1563MODULE_DESCRIPTION("PXA3xx NAND controller driver");