blob: 579347bc254d37da3251c8040ebb9cce8cb3bd61 [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
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300606static void prepare_start_command(struct pxa3xx_nand_info *info, int command)
Lei Wen4eb2da82011-02-28 10:32:13 +0800607{
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300608 struct pxa3xx_nand_host *host = info->host[info->cs];
609 struct mtd_info *mtd = host->mtd;
610
Lei Wen4eb2da82011-02-28 10:32:13 +0800611 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800612 info->buf_start = 0;
613 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800614 info->oob_size = 0;
615 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300616 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800617 info->retcode = ERR_NONE;
Ezequiel Garciaf0e6a32e2013-11-14 18:25:30 -0300618 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800619
620 switch (command) {
621 case NAND_CMD_READ0:
622 case NAND_CMD_PAGEPROG:
623 info->use_ecc = 1;
624 case NAND_CMD_READOOB:
625 pxa3xx_set_datasize(info);
626 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300627 case NAND_CMD_PARAM:
628 info->use_spare = 0;
629 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800630 default:
631 info->ndcb1 = 0;
632 info->ndcb2 = 0;
633 break;
634 }
Ezequiel Garcia39f83d12013-11-14 18:25:34 -0300635
636 /*
637 * If we are about to issue a read command, or about to set
638 * the write address, then clean the data buffer.
639 */
640 if (command == NAND_CMD_READ0 ||
641 command == NAND_CMD_READOOB ||
642 command == NAND_CMD_SEQIN) {
643
644 info->buf_count = mtd->writesize + mtd->oobsize;
645 memset(info->data_buff, 0xFF, info->buf_count);
646 }
647
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300648}
649
650static int prepare_set_command(struct pxa3xx_nand_info *info, int command,
651 uint16_t column, int page_addr)
652{
653 int addr_cycle, exec_cmd;
654 struct pxa3xx_nand_host *host;
655 struct mtd_info *mtd;
656
657 host = info->host[info->cs];
658 mtd = host->mtd;
659 addr_cycle = 0;
660 exec_cmd = 1;
661
662 if (info->cs != 0)
663 info->ndcb0 = NDCB0_CSEL;
664 else
665 info->ndcb0 = 0;
666
667 if (command == NAND_CMD_SEQIN)
668 exec_cmd = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800669
Lei Wend4568822011-07-14 20:44:32 -0700670 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
671 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800672
673 switch (command) {
674 case NAND_CMD_READOOB:
675 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300676 info->buf_start = column;
677 info->ndcb0 |= NDCB0_CMD_TYPE(0)
678 | addr_cycle
679 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800680
Ezequiel Garciaec821352013-08-12 14:14:54 -0300681 if (command == NAND_CMD_READOOB)
682 info->buf_start += mtd->writesize;
683
684 /* Second command setting for large pages */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300685 if (mtd->writesize >= PAGE_CHUNK_SIZE)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300686 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800687
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300688 set_command_address(info, mtd->writesize, column, page_addr);
Ezequiel Garcia01d99472013-11-14 18:25:32 -0300689 break;
690
Lei Wen4eb2da82011-02-28 10:32:13 +0800691 case NAND_CMD_SEQIN:
Lei Wen4eb2da82011-02-28 10:32:13 +0800692
Ezequiel Garciae7f9a6a2013-11-14 18:25:35 -0300693 info->buf_start = column;
694 set_command_address(info, mtd->writesize, 0, page_addr);
Lei Wen4eb2da82011-02-28 10:32:13 +0800695 break;
696
697 case NAND_CMD_PAGEPROG:
698 if (is_buf_blank(info->data_buff,
699 (mtd->writesize + mtd->oobsize))) {
700 exec_cmd = 0;
701 break;
702 }
703
Lei Wen4eb2da82011-02-28 10:32:13 +0800704 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
705 | NDCB0_AUTO_RS
706 | NDCB0_ST_ROW_EN
707 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300708 | (NAND_CMD_PAGEPROG << 8)
709 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800710 | addr_cycle;
711 break;
712
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300713 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300714 info->buf_count = 256;
715 info->ndcb0 |= NDCB0_CMD_TYPE(0)
716 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300717 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300718 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300719 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300720 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300721 info->data_size = 256;
722 break;
723
Lei Wen4eb2da82011-02-28 10:32:13 +0800724 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700725 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800726 info->ndcb0 |= NDCB0_CMD_TYPE(3)
727 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300728 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300729 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800730
731 info->data_size = 8;
732 break;
733 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800734 info->buf_count = 1;
735 info->ndcb0 |= NDCB0_CMD_TYPE(4)
736 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300737 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800738
739 info->data_size = 8;
740 break;
741
742 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800743 info->ndcb0 |= NDCB0_CMD_TYPE(2)
744 | NDCB0_AUTO_RS
745 | NDCB0_ADDR_CYC(3)
746 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300747 | (NAND_CMD_ERASE2 << 8)
748 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800749 info->ndcb1 = page_addr;
750 info->ndcb2 = 0;
751
752 break;
753 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800754 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300755 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800756
757 break;
758
759 case NAND_CMD_ERASE2:
760 exec_cmd = 0;
761 break;
762
763 default:
764 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700765 dev_err(&info->pdev->dev, "non-supported command %x\n",
766 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800767 break;
768 }
769
770 return exec_cmd;
771}
772
eric miaofe69af02008-02-14 15:48:23 +0800773static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100774 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800775{
Lei Wend4568822011-07-14 20:44:32 -0700776 struct pxa3xx_nand_host *host = mtd->priv;
777 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800778 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800779
Lei Wen4eb2da82011-02-28 10:32:13 +0800780 /*
781 * if this is a x16 device ,then convert the input
782 * "byte" address into a "word" address appropriate
783 * for indexing a word-oriented device
784 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300785 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800786 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800787
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700788 /*
789 * There may be different NAND chip hooked to
790 * different chip select, so check whether
791 * chip select has been changed, if yes, reset the timing
792 */
793 if (info->cs != host->cs) {
794 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300795 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
796 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700797 }
798
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300799 prepare_start_command(info, command);
800
Lei Wend4568822011-07-14 20:44:32 -0700801 info->state = STATE_PREPARED;
Ezequiel Garciac39ff032013-11-14 18:25:33 -0300802 exec_cmd = prepare_set_command(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800803 if (exec_cmd) {
804 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300805 init_completion(&info->dev_ready);
806 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800807 pxa3xx_nand_start(info);
808
809 ret = wait_for_completion_timeout(&info->cmd_complete,
810 CHIP_DELAY_TIMEOUT);
811 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700812 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800813 /* Stop State Machine for next command cycle */
814 pxa3xx_nand_stop(info);
815 }
eric miaofe69af02008-02-14 15:48:23 +0800816 }
Lei Wend4568822011-07-14 20:44:32 -0700817 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800818}
819
Josh Wufdbad98d2012-06-25 18:07:45 +0800820static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700821 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800822{
823 chip->write_buf(mtd, buf, mtd->writesize);
824 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800825
826 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800827}
828
829static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700830 struct nand_chip *chip, uint8_t *buf, int oob_required,
831 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800832{
Lei Wend4568822011-07-14 20:44:32 -0700833 struct pxa3xx_nand_host *host = mtd->priv;
834 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300835 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800836
837 chip->read_buf(mtd, buf, mtd->writesize);
838 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
839
840 if (info->retcode == ERR_SBERR) {
841 switch (info->use_ecc) {
842 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300843 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800844 mtd->ecc_stats.corrected++;
845 break;
846 case 0:
847 default:
848 break;
849 }
850 } else if (info->retcode == ERR_DBERR) {
851 /*
852 * for blank page (all 0xff), HW will calculate its ECC as
853 * 0, which is different from the ECC information within
854 * OOB, ignore such double bit errors
855 */
856 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700857 info->retcode = ERR_NONE;
858 else
Lei Wenf8155a42011-02-28 10:32:11 +0800859 mtd->ecc_stats.failed++;
860 }
861
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300862 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800863}
864
eric miaofe69af02008-02-14 15:48:23 +0800865static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
866{
Lei Wend4568822011-07-14 20:44:32 -0700867 struct pxa3xx_nand_host *host = mtd->priv;
868 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800869 char retval = 0xFF;
870
871 if (info->buf_start < info->buf_count)
872 /* Has just send a new command? */
873 retval = info->data_buff[info->buf_start++];
874
875 return retval;
876}
877
878static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
879{
Lei Wend4568822011-07-14 20:44:32 -0700880 struct pxa3xx_nand_host *host = mtd->priv;
881 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800882 u16 retval = 0xFFFF;
883
884 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
885 retval = *((u16 *)(info->data_buff+info->buf_start));
886 info->buf_start += 2;
887 }
888 return retval;
889}
890
891static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
892{
Lei Wend4568822011-07-14 20:44:32 -0700893 struct pxa3xx_nand_host *host = mtd->priv;
894 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800895 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
896
897 memcpy(buf, info->data_buff + info->buf_start, real_len);
898 info->buf_start += real_len;
899}
900
901static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
902 const uint8_t *buf, int len)
903{
Lei Wend4568822011-07-14 20:44:32 -0700904 struct pxa3xx_nand_host *host = mtd->priv;
905 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800906 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
907
908 memcpy(info->data_buff + info->buf_start, buf, real_len);
909 info->buf_start += real_len;
910}
911
eric miaofe69af02008-02-14 15:48:23 +0800912static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
913{
914 return;
915}
916
917static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
918{
Lei Wend4568822011-07-14 20:44:32 -0700919 struct pxa3xx_nand_host *host = mtd->priv;
920 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300921 int ret;
922
923 if (info->need_wait) {
924 ret = wait_for_completion_timeout(&info->dev_ready,
925 CHIP_DELAY_TIMEOUT);
926 info->need_wait = 0;
927 if (!ret) {
928 dev_err(&info->pdev->dev, "Ready time out!!!\n");
929 return NAND_STATUS_FAIL;
930 }
931 }
eric miaofe69af02008-02-14 15:48:23 +0800932
933 /* pxa3xx_nand_send_command has waited for command complete */
934 if (this->state == FL_WRITING || this->state == FL_ERASING) {
935 if (info->retcode == ERR_NONE)
936 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300937 else
938 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +0800939 }
940
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300941 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +0800942}
943
eric miaofe69af02008-02-14 15:48:23 +0800944static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200945 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800946{
947 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900948 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700949 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800950 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800951
Lei Wenda675b42011-07-14 20:44:31 -0700952 if (f->page_size != 2048 && f->page_size != 512) {
953 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800954 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700955 }
eric miaofe69af02008-02-14 15:48:23 +0800956
Lei Wenda675b42011-07-14 20:44:31 -0700957 if (f->flash_width != 16 && f->flash_width != 8) {
958 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800959 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700960 }
eric miaofe69af02008-02-14 15:48:23 +0800961
962 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700963 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800964
965 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700966 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800967
968 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700969 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800970 else
Lei Wend4568822011-07-14 20:44:32 -0700971 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800972
973 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700974 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800975 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
976 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
977 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
978 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
979
Lei Wend4568822011-07-14 20:44:32 -0700980 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800981 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
982
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300983 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800984
Lei Wend4568822011-07-14 20:44:32 -0700985 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800986 return 0;
987}
988
Mike Rapoportf2710492009-02-17 13:54:47 +0200989static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
990{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700991 /*
992 * We set 0 by hard coding here, for we don't support keep_config
993 * when there is more than one chip attached to the controller
994 */
995 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200996 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200997
Lei Wend4568822011-07-14 20:44:32 -0700998 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300999 /* Controller's FIFO size */
1000 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -07001001 host->read_id_bytes = 4;
1002 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -03001003 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -07001004 host->read_id_bytes = 2;
1005 }
1006
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001007 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
1008 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
1009 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +02001010 return 0;
1011}
1012
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001013#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +08001014static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1015{
1016 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001017 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +08001018
1019 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001020 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +08001021 if (info->data_buff == NULL)
1022 return -ENOMEM;
1023 return 0;
1024 }
1025
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001026 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001027 &info->data_buff_phys, GFP_KERNEL);
1028 if (info->data_buff == NULL) {
1029 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
1030 return -ENOMEM;
1031 }
1032
eric miaofe69af02008-02-14 15:48:23 +08001033 info->data_desc = (void *)info->data_buff + data_desc_offset;
1034 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1035
1036 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1037 pxa3xx_nand_data_dma_irq, info);
1038 if (info->data_dma_ch < 0) {
1039 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001040 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001041 info->data_buff, info->data_buff_phys);
1042 return info->data_dma_ch;
1043 }
1044
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001045 /*
1046 * Now that DMA buffers are allocated we turn on
1047 * DMA proper for I/O operations.
1048 */
1049 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001050 return 0;
1051}
1052
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001053static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1054{
1055 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001056 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001057 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001058 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001059 info->data_buff, info->data_buff_phys);
1060 } else {
1061 kfree(info->data_buff);
1062 }
1063}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001064#else
1065static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1066{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001067 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001068 if (info->data_buff == NULL)
1069 return -ENOMEM;
1070 return 0;
1071}
1072
1073static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1074{
1075 kfree(info->data_buff);
1076}
1077#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001078
Lei Wen401e67e2011-02-28 10:32:14 +08001079static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001080{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001081 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001082 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001083 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001084
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001085 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001086 chip = mtd->priv;
1087
Lei Wen401e67e2011-02-28 10:32:14 +08001088 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001089 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1090 if (ret)
1091 return ret;
1092
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001093 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001094 ret = chip->waitfunc(mtd, chip);
1095 if (ret & NAND_STATUS_FAIL)
1096 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001097
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001098 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001099}
eric miaofe69af02008-02-14 15:48:23 +08001100
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001101static int pxa_ecc_init(struct pxa3xx_nand_info *info,
1102 struct nand_ecc_ctrl *ecc,
1103 int strength, int page_size)
1104{
1105 /*
1106 * We don't use strength here as the PXA variant
1107 * is used with non-ONFI compliant devices.
1108 */
1109 if (page_size == 2048) {
1110 info->spare_size = 40;
1111 info->ecc_size = 24;
1112 ecc->mode = NAND_ECC_HW;
1113 ecc->size = 512;
1114 ecc->strength = 1;
1115 return 1;
1116
1117 } else if (page_size == 512) {
1118 info->spare_size = 8;
1119 info->ecc_size = 8;
1120 ecc->mode = NAND_ECC_HW;
1121 ecc->size = 512;
1122 ecc->strength = 1;
1123 return 1;
1124 }
1125 return 0;
1126}
1127
1128static int armada370_ecc_init(struct pxa3xx_nand_info *info,
1129 struct nand_ecc_ctrl *ecc,
1130 int strength, int page_size)
1131{
1132 /* Unimplemented yet */
1133 return 0;
1134}
1135
Lei Wen401e67e2011-02-28 10:32:14 +08001136static int pxa3xx_nand_scan(struct mtd_info *mtd)
1137{
Lei Wend4568822011-07-14 20:44:32 -07001138 struct pxa3xx_nand_host *host = mtd->priv;
1139 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001140 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001141 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001142 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001143 const struct pxa3xx_nand_flash *f = NULL;
1144 struct nand_chip *chip = mtd->priv;
1145 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001146 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001147 int i, ret, num;
1148
1149 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001150 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001151
1152 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001153 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001154 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1155 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001156
Lei Wend4568822011-07-14 20:44:32 -07001157 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001158 }
1159
1160 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1161 id = *((uint16_t *)(info->data_buff));
1162 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001163 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001164 else {
Lei Wenda675b42011-07-14 20:44:31 -07001165 dev_warn(&info->pdev->dev,
1166 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001167
1168 return -EINVAL;
1169 }
1170
1171 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1172 for (i = 0; i < num; i++) {
1173 if (i < pdata->num_flash)
1174 f = pdata->flash + i;
1175 else
1176 f = &builtin_flash_types[i - pdata->num_flash + 1];
1177
1178 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001179 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001180 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001181 }
1182
Lei Wen4332c112011-03-03 11:27:01 +08001183 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001184 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001185
1186 return -EINVAL;
1187 }
1188
Lei Wend4568822011-07-14 20:44:32 -07001189 ret = pxa3xx_nand_config_flash(info, f);
1190 if (ret) {
1191 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1192 return ret;
1193 }
1194
Lei Wen4332c112011-03-03 11:27:01 +08001195 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001196 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001197 pxa3xx_flash_ids[0].pagesize = f->page_size;
1198 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1199 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1200 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1201 if (f->flash_width == 16)
1202 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001203 pxa3xx_flash_ids[1].name = NULL;
1204 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001205KEEP_CONFIG:
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001206 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001207 chip->options |= NAND_BUSWIDTH_16;
1208
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001209 /* Device detection must be done with ECC disabled */
1210 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1211 nand_writel(info, NDECCCTRL, 0x0);
1212
Lei Wen0fab0282011-06-07 03:01:06 -07001213 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001214 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001215
1216 if (pdata->flash_bbt) {
1217 /*
1218 * We'll use a bad block table stored in-flash and don't
1219 * allow writing the bad block marker to the flash.
1220 */
1221 chip->bbt_options |= NAND_BBT_USE_FLASH |
1222 NAND_BBT_NO_OOB_BBM;
1223 chip->bbt_td = &bbt_main_descr;
1224 chip->bbt_md = &bbt_mirror_descr;
1225 }
1226
Ezequiel Garcia43bcfd22013-11-14 18:25:29 -03001227 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
1228 ret = armada370_ecc_init(info, &chip->ecc,
1229 chip->ecc_strength_ds,
1230 mtd->writesize);
1231 else
1232 ret = pxa_ecc_init(info, &chip->ecc,
1233 chip->ecc_strength_ds,
1234 mtd->writesize);
1235 if (!ret) {
1236 dev_err(&info->pdev->dev,
1237 "ECC strength %d at page size %d is not supported\n",
1238 chip->ecc_strength_ds, mtd->writesize);
1239 return -ENODEV;
1240 }
1241
Lei Wen4332c112011-03-03 11:27:01 +08001242 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001243 if (mtd->writesize >= 2048)
1244 host->col_addr_cycles = 2;
1245 else
1246 host->col_addr_cycles = 1;
1247
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001248 /* release the initial buffer */
1249 kfree(info->data_buff);
1250
1251 /* allocate the real data + oob buffer */
1252 info->buf_size = mtd->writesize + mtd->oobsize;
1253 ret = pxa3xx_nand_init_buff(info);
1254 if (ret)
1255 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001256 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001257
Lei Wen4332c112011-03-03 11:27:01 +08001258 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001259 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001260 else
Lei Wend4568822011-07-14 20:44:32 -07001261 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001262 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001263}
1264
Lei Wend4568822011-07-14 20:44:32 -07001265static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001266{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001267 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001268 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001269 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001270 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001271 struct mtd_info *mtd;
1272 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001273 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001274
Jingoo Han453810b2013-07-30 17:18:33 +09001275 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001276 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1277 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1278 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001279 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001280
eric miaofe69af02008-02-14 15:48:23 +08001281 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001282 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001283 for (cs = 0; cs < pdata->num_cs; cs++) {
1284 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1285 (sizeof(*mtd) + sizeof(*host)) * cs);
1286 chip = (struct nand_chip *)(&mtd[1]);
1287 host = (struct pxa3xx_nand_host *)chip;
1288 info->host[cs] = host;
1289 host->mtd = mtd;
1290 host->cs = cs;
1291 host->info_data = info;
1292 mtd->priv = host;
1293 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001294
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001295 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1296 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1297 chip->controller = &info->controller;
1298 chip->waitfunc = pxa3xx_nand_waitfunc;
1299 chip->select_chip = pxa3xx_nand_select_chip;
1300 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1301 chip->read_word = pxa3xx_nand_read_word;
1302 chip->read_byte = pxa3xx_nand_read_byte;
1303 chip->read_buf = pxa3xx_nand_read_buf;
1304 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001305 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001306 }
Lei Wen401e67e2011-02-28 10:32:14 +08001307
1308 spin_lock_init(&chip->controller->lock);
1309 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001310 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001311 if (IS_ERR(info->clk)) {
1312 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001313 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001314 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001315 ret = clk_prepare_enable(info->clk);
1316 if (ret < 0)
1317 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001318
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001319 if (use_dma) {
1320 /*
1321 * This is a dirty hack to make this driver work from
1322 * devicetree bindings. It can be removed once we have
1323 * a prober DMA controller framework for DT.
1324 */
1325 if (pdev->dev.of_node &&
1326 of_machine_is_compatible("marvell,pxa3xx")) {
1327 info->drcmr_dat = 97;
1328 info->drcmr_cmd = 99;
1329 } else {
1330 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1331 if (r == NULL) {
1332 dev_err(&pdev->dev,
1333 "no resource defined for data DMA\n");
1334 ret = -ENXIO;
1335 goto fail_disable_clk;
1336 }
1337 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001338
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001339 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1340 if (r == NULL) {
1341 dev_err(&pdev->dev,
1342 "no resource defined for cmd DMA\n");
1343 ret = -ENXIO;
1344 goto fail_disable_clk;
1345 }
1346 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001347 }
eric miaofe69af02008-02-14 15:48:23 +08001348 }
eric miaofe69af02008-02-14 15:48:23 +08001349
1350 irq = platform_get_irq(pdev, 0);
1351 if (irq < 0) {
1352 dev_err(&pdev->dev, "no IRQ resource defined\n");
1353 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001354 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001355 }
1356
1357 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001358 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1359 if (IS_ERR(info->mmio_base)) {
1360 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001361 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001362 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001363 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001364
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001365 /* Allocate a buffer to allow flash detection */
1366 info->buf_size = INIT_BUFFER_SIZE;
1367 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1368 if (info->data_buff == NULL) {
1369 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001370 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001371 }
eric miaofe69af02008-02-14 15:48:23 +08001372
Haojian Zhuang346e1252009-09-10 14:27:23 +08001373 /* initialize all interrupts to be disabled */
1374 disable_int(info, NDSR_MASK);
1375
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001376 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001377 if (ret < 0) {
1378 dev_err(&pdev->dev, "failed to request IRQ\n");
1379 goto fail_free_buf;
1380 }
1381
Lei Wene353a202011-03-03 11:08:30 +08001382 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001383
Lei Wend4568822011-07-14 20:44:32 -07001384 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001385
eric miaofe69af02008-02-14 15:48:23 +08001386fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001387 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001388 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001389fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001390 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001391 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001392}
1393
1394static int pxa3xx_nand_remove(struct platform_device *pdev)
1395{
Lei Wene353a202011-03-03 11:08:30 +08001396 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001397 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001398 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001399
Lei Wend4568822011-07-14 20:44:32 -07001400 if (!info)
1401 return 0;
1402
Jingoo Han453810b2013-07-30 17:18:33 +09001403 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001404
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001405 irq = platform_get_irq(pdev, 0);
1406 if (irq >= 0)
1407 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001408 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001409
Ezequiel Garciafb320612013-04-17 13:38:12 -03001410 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001411
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001412 for (cs = 0; cs < pdata->num_cs; cs++)
1413 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001414 return 0;
1415}
1416
Daniel Mack1e7ba632012-07-22 19:51:02 +02001417static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1418{
1419 struct pxa3xx_nand_platform_data *pdata;
1420 struct device_node *np = pdev->dev.of_node;
1421 const struct of_device_id *of_id =
1422 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1423
1424 if (!of_id)
1425 return 0;
1426
1427 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1428 if (!pdata)
1429 return -ENOMEM;
1430
1431 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1432 pdata->enable_arbiter = 1;
1433 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1434 pdata->keep_config = 1;
1435 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001436 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001437
1438 pdev->dev.platform_data = pdata;
1439
1440 return 0;
1441}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001442
Lei Wene353a202011-03-03 11:08:30 +08001443static int pxa3xx_nand_probe(struct platform_device *pdev)
1444{
1445 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001446 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001447 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001448 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001449
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001450#ifndef ARCH_HAS_DMA
1451 if (use_dma) {
1452 use_dma = 0;
1453 dev_warn(&pdev->dev,
1454 "This platform can't do DMA on this device\n");
1455 }
1456#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001457 ret = pxa3xx_nand_probe_dt(pdev);
1458 if (ret)
1459 return ret;
1460
Jingoo Han453810b2013-07-30 17:18:33 +09001461 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001462 if (!pdata) {
1463 dev_err(&pdev->dev, "no platform data defined\n");
1464 return -ENODEV;
1465 }
1466
Lei Wend4568822011-07-14 20:44:32 -07001467 ret = alloc_nand_resource(pdev);
1468 if (ret) {
1469 dev_err(&pdev->dev, "alloc nand resource failed\n");
1470 return ret;
1471 }
Lei Wene353a202011-03-03 11:08:30 +08001472
Lei Wend4568822011-07-14 20:44:32 -07001473 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001474 probe_success = 0;
1475 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001476 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001477
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001478 /*
1479 * The mtd name matches the one used in 'mtdparts' kernel
1480 * parameter. This name cannot be changed or otherwise
1481 * user's mtd partitions configuration would get broken.
1482 */
1483 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001484 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001485 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001486 if (ret) {
1487 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1488 cs);
1489 continue;
1490 }
1491
Daniel Mack1e7ba632012-07-22 19:51:02 +02001492 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001493 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001494 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001495 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001496 if (!ret)
1497 probe_success = 1;
1498 }
1499
1500 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001501 pxa3xx_nand_remove(pdev);
1502 return -ENODEV;
1503 }
1504
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001505 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001506}
1507
eric miaofe69af02008-02-14 15:48:23 +08001508#ifdef CONFIG_PM
1509static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1510{
Lei Wene353a202011-03-03 11:08:30 +08001511 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001512 struct pxa3xx_nand_platform_data *pdata;
1513 struct mtd_info *mtd;
1514 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001515
Jingoo Han453810b2013-07-30 17:18:33 +09001516 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001517 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001518 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1519 return -EAGAIN;
1520 }
1521
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001522 for (cs = 0; cs < pdata->num_cs; cs++) {
1523 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001524 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001525 }
1526
eric miaofe69af02008-02-14 15:48:23 +08001527 return 0;
1528}
1529
1530static int pxa3xx_nand_resume(struct platform_device *pdev)
1531{
Lei Wene353a202011-03-03 11:08:30 +08001532 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001533 struct pxa3xx_nand_platform_data *pdata;
1534 struct mtd_info *mtd;
1535 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001536
Jingoo Han453810b2013-07-30 17:18:33 +09001537 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001538 /* We don't want to handle interrupt without calling mtd routine */
1539 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001540
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001541 /*
1542 * Directly set the chip select to a invalid value,
1543 * then the driver would reset the timing according
1544 * to current chip select at the beginning of cmdfunc
1545 */
1546 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001547
Lei Wen051fc412011-07-14 20:44:30 -07001548 /*
1549 * As the spec says, the NDSR would be updated to 0x1800 when
1550 * doing the nand_clk disable/enable.
1551 * To prevent it damaging state machine of the driver, clear
1552 * all status before resume
1553 */
1554 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001555 for (cs = 0; cs < pdata->num_cs; cs++) {
1556 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001557 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001558 }
1559
Lei Wen18c81b12010-08-17 17:25:57 +08001560 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001561}
1562#else
1563#define pxa3xx_nand_suspend NULL
1564#define pxa3xx_nand_resume NULL
1565#endif
1566
1567static struct platform_driver pxa3xx_nand_driver = {
1568 .driver = {
1569 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301570 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001571 },
1572 .probe = pxa3xx_nand_probe,
1573 .remove = pxa3xx_nand_remove,
1574 .suspend = pxa3xx_nand_suspend,
1575 .resume = pxa3xx_nand_resume,
1576};
1577
Axel Linf99640d2011-11-27 20:45:03 +08001578module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001579
1580MODULE_LICENSE("GPL");
1581MODULE_DESCRIPTION("PXA3xx NAND controller driver");