blob: 84a6c649100c163390c8bc2c3235f7b5fd083bbf [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 */
61#define NDDB (0x40) /* Data Buffer */
62#define NDCB0 (0x48) /* Command Buffer0 */
63#define NDCB1 (0x4C) /* Command Buffer1 */
64#define NDCB2 (0x50) /* Command Buffer2 */
65
66#define NDCR_SPARE_EN (0x1 << 31)
67#define NDCR_ECC_EN (0x1 << 30)
68#define NDCR_DMA_EN (0x1 << 29)
69#define NDCR_ND_RUN (0x1 << 28)
70#define NDCR_DWIDTH_C (0x1 << 27)
71#define NDCR_DWIDTH_M (0x1 << 26)
72#define NDCR_PAGE_SZ (0x1 << 24)
73#define NDCR_NCSX (0x1 << 23)
74#define NDCR_ND_MODE (0x3 << 21)
75#define NDCR_NAND_MODE (0x0)
76#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080077#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080078#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
79#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
80
81#define NDCR_RA_START (0x1 << 15)
82#define NDCR_PG_PER_BLK (0x1 << 14)
83#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080084#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080085
86#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080087#define NDSR_RDY (0x1 << 12)
88#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080089#define NDSR_CS0_PAGED (0x1 << 10)
90#define NDSR_CS1_PAGED (0x1 << 9)
91#define NDSR_CS0_CMDD (0x1 << 8)
92#define NDSR_CS1_CMDD (0x1 << 7)
93#define NDSR_CS0_BBD (0x1 << 6)
94#define NDSR_CS1_BBD (0x1 << 5)
95#define NDSR_DBERR (0x1 << 4)
96#define NDSR_SBERR (0x1 << 3)
97#define NDSR_WRDREQ (0x1 << 2)
98#define NDSR_RDDREQ (0x1 << 1)
99#define NDSR_WRCMDREQ (0x1)
100
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300101#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800102#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800103#define NDCB0_AUTO_RS (0x1 << 25)
104#define NDCB0_CSEL (0x1 << 24)
105#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
106#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
107#define NDCB0_NC (0x1 << 20)
108#define NDCB0_DBC (0x1 << 19)
109#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
110#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
111#define NDCB0_CMD2_MASK (0xff << 8)
112#define NDCB0_CMD1_MASK (0xff)
113#define NDCB0_ADDR_CYC_SHIFT (16)
114
eric miaofe69af02008-02-14 15:48:23 +0800115/* macros for registers read/write */
116#define nand_writel(info, off, val) \
117 __raw_writel((val), (info)->mmio_base + (off))
118
119#define nand_readl(info, off) \
120 __raw_readl((info)->mmio_base + (off))
121
122/* error code and state */
123enum {
124 ERR_NONE = 0,
125 ERR_DMABUSERR = -1,
126 ERR_SENDCMD = -2,
127 ERR_DBERR = -3,
128 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300129 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800130};
131
132enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800133 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700134 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800135 STATE_CMD_HANDLE,
136 STATE_DMA_READING,
137 STATE_DMA_WRITING,
138 STATE_DMA_DONE,
139 STATE_PIO_READING,
140 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800141 STATE_CMD_DONE,
142 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800143};
144
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300145enum pxa3xx_nand_variant {
146 PXA3XX_NAND_VARIANT_PXA,
147 PXA3XX_NAND_VARIANT_ARMADA370,
148};
149
Lei Wend4568822011-07-14 20:44:32 -0700150struct pxa3xx_nand_host {
151 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700152 struct mtd_info *mtd;
153 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800154
Lei Wend4568822011-07-14 20:44:32 -0700155 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700156 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700157 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700158
159 /* calculated from pxa3xx_nand_flash data */
160 unsigned int col_addr_cycles;
161 unsigned int row_addr_cycles;
162 size_t read_id_bytes;
163
Lei Wend4568822011-07-14 20:44:32 -0700164};
165
166struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800167 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800168 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800169
170 struct clk *clk;
171 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800172 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300173 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800174
175 unsigned int buf_start;
176 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300177 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800178
179 /* DMA information */
180 int drcmr_dat;
181 int drcmr_cmd;
182
183 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800184 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800185 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800186 int data_dma_ch;
187 struct pxa_dma_desc *data_desc;
188 dma_addr_t data_desc_addr;
189
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700190 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800191 unsigned int state;
192
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300193 /*
194 * This driver supports NFCv1 (as found in PXA SoC)
195 * and NFCv2 (as found in Armada 370/XP SoC).
196 */
197 enum pxa3xx_nand_variant variant;
198
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700199 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800200 int use_ecc; /* use HW ECC ? */
201 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300202 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300203 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800204
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300205 unsigned int fifo_size; /* max. data size in the FIFO */
206 unsigned int data_size; /* data to be read from FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700207 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800208 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800209
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300210 /* cached register value */
211 uint32_t reg_ndcr;
212 uint32_t ndtr0cs0;
213 uint32_t ndtr1cs0;
214
eric miaofe69af02008-02-14 15:48:23 +0800215 /* generated NDCBx register values */
216 uint32_t ndcb0;
217 uint32_t ndcb1;
218 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300219 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800220};
221
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030222static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800223module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300224MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800225
Lei Wenc1f82472010-08-17 13:50:23 +0800226static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800227 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
228 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
229 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
230 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800231};
232
Lei Wenc1f82472010-08-17 13:50:23 +0800233static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800234{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
235{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
236{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
237{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
238{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
239{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
240{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
241{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
242{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800243};
244
Ezequiel Garcia776f2652013-11-14 18:25:28 -0300245static u8 bbt_pattern[] = {'M', 'V', 'B', 'b', 't', '0' };
246static u8 bbt_mirror_pattern[] = {'1', 't', 'b', 'B', 'V', 'M' };
247
248static struct nand_bbt_descr bbt_main_descr = {
249 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
250 | NAND_BBT_2BIT | NAND_BBT_VERSION,
251 .offs = 8,
252 .len = 6,
253 .veroffs = 14,
254 .maxblocks = 8, /* Last 8 blocks in each chip */
255 .pattern = bbt_pattern
256};
257
258static struct nand_bbt_descr bbt_mirror_descr = {
259 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
260 | NAND_BBT_2BIT | NAND_BBT_VERSION,
261 .offs = 8,
262 .len = 6,
263 .veroffs = 14,
264 .maxblocks = 8, /* Last 8 blocks in each chip */
265 .pattern = bbt_mirror_pattern
266};
267
Lei Wen227a8862010-08-18 18:00:03 +0800268/* Define a default flash type setting serve as flash detecting only */
269#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
270
eric miaofe69af02008-02-14 15:48:23 +0800271#define NDTR0_tCH(c) (min((c), 7) << 19)
272#define NDTR0_tCS(c) (min((c), 7) << 16)
273#define NDTR0_tWH(c) (min((c), 7) << 11)
274#define NDTR0_tWP(c) (min((c), 7) << 8)
275#define NDTR0_tRH(c) (min((c), 7) << 3)
276#define NDTR0_tRP(c) (min((c), 7) << 0)
277
278#define NDTR1_tR(c) (min((c), 65535) << 16)
279#define NDTR1_tWHR(c) (min((c), 15) << 4)
280#define NDTR1_tAR(c) (min((c), 15) << 0)
281
282/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800283#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800284
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300285static struct of_device_id pxa3xx_nand_dt_ids[] = {
286 {
287 .compatible = "marvell,pxa3xx-nand",
288 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
289 },
290 {}
291};
292MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
293
294static enum pxa3xx_nand_variant
295pxa3xx_nand_get_variant(struct platform_device *pdev)
296{
297 const struct of_device_id *of_id =
298 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
299 if (!of_id)
300 return PXA3XX_NAND_VARIANT_PXA;
301 return (enum pxa3xx_nand_variant)of_id->data;
302}
303
Lei Wend4568822011-07-14 20:44:32 -0700304static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200305 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800306{
Lei Wend4568822011-07-14 20:44:32 -0700307 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800308 unsigned long nand_clk = clk_get_rate(info->clk);
309 uint32_t ndtr0, ndtr1;
310
311 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
312 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
313 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
314 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
315 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
316 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
317
318 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
319 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
320 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
321
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300322 info->ndtr0cs0 = ndtr0;
323 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800324 nand_writel(info, NDTR0CS0, ndtr0);
325 nand_writel(info, NDTR1CS0, ndtr1);
326}
327
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300328/*
329 * Set the data and OOB size, depending on the selected
330 * spare and ECC configuration.
331 * Only applicable to READ0, READOOB and PAGEPROG commands.
332 */
Lei Wen18c81b12010-08-17 17:25:57 +0800333static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800334{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300335 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800336
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300337 info->data_size = info->fifo_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800338 if (!oob_enable) {
339 info->oob_size = 0;
340 return;
341 }
342
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300343 switch (info->fifo_size) {
eric miaofe69af02008-02-14 15:48:23 +0800344 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800345 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800346 break;
347 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800348 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800349 break;
eric miaofe69af02008-02-14 15:48:23 +0800350 }
Lei Wen18c81b12010-08-17 17:25:57 +0800351}
352
Lei Wenf8155a42011-02-28 10:32:11 +0800353/**
354 * NOTE: it is a must to set ND_RUN firstly, then write
355 * command buffer, otherwise, it does not work.
356 * We enable all the interrupt at the same time, and
357 * let pxa3xx_nand_irq to handle all logic.
358 */
359static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
360{
361 uint32_t ndcr;
362
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300363 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300364
365 if (info->use_ecc)
366 ndcr |= NDCR_ECC_EN;
367 else
368 ndcr &= ~NDCR_ECC_EN;
369
370 if (info->use_dma)
371 ndcr |= NDCR_DMA_EN;
372 else
373 ndcr &= ~NDCR_DMA_EN;
374
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300375 if (info->use_spare)
376 ndcr |= NDCR_SPARE_EN;
377 else
378 ndcr &= ~NDCR_SPARE_EN;
379
Lei Wenf8155a42011-02-28 10:32:11 +0800380 ndcr |= NDCR_ND_RUN;
381
382 /* clear status bits and run */
383 nand_writel(info, NDCR, 0);
384 nand_writel(info, NDSR, NDSR_MASK);
385 nand_writel(info, NDCR, ndcr);
386}
387
388static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
389{
390 uint32_t ndcr;
391 int timeout = NAND_STOP_DELAY;
392
393 /* wait RUN bit in NDCR become 0 */
394 ndcr = nand_readl(info, NDCR);
395 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
396 ndcr = nand_readl(info, NDCR);
397 udelay(1);
398 }
399
400 if (timeout <= 0) {
401 ndcr &= ~NDCR_ND_RUN;
402 nand_writel(info, NDCR, ndcr);
403 }
404 /* clear status bits */
405 nand_writel(info, NDSR, NDSR_MASK);
406}
407
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300408static void __maybe_unused
409enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800410{
411 uint32_t ndcr;
412
413 ndcr = nand_readl(info, NDCR);
414 nand_writel(info, NDCR, ndcr & ~int_mask);
415}
416
417static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
418{
419 uint32_t ndcr;
420
421 ndcr = nand_readl(info, NDCR);
422 nand_writel(info, NDCR, ndcr | int_mask);
423}
424
Lei Wenf8155a42011-02-28 10:32:11 +0800425static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800426{
eric miaofe69af02008-02-14 15:48:23 +0800427 switch (info->state) {
428 case STATE_PIO_WRITING:
429 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800430 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800431 if (info->oob_size > 0)
432 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
433 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800434 break;
435 case STATE_PIO_READING:
436 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800437 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800438 if (info->oob_size > 0)
439 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
440 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800441 break;
442 default:
Lei Wenda675b42011-07-14 20:44:31 -0700443 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800444 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800445 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800446 }
eric miaofe69af02008-02-14 15:48:23 +0800447}
448
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300449#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800450static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800451{
452 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800453 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800454
455 desc->ddadr = DDADR_STOP;
456 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
457
Lei Wenf8155a42011-02-28 10:32:11 +0800458 switch (info->state) {
459 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800460 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800461 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800462 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800463 break;
464 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800465 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800466 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800467 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800468 break;
469 default:
Lei Wenda675b42011-07-14 20:44:31 -0700470 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800471 info->state);
472 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800473 }
474
475 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
476 DDADR(info->data_dma_ch) = info->data_desc_addr;
477 DCSR(info->data_dma_ch) |= DCSR_RUN;
478}
479
480static void pxa3xx_nand_data_dma_irq(int channel, void *data)
481{
482 struct pxa3xx_nand_info *info = data;
483 uint32_t dcsr;
484
485 dcsr = DCSR(channel);
486 DCSR(channel) = dcsr;
487
488 if (dcsr & DCSR_BUSERR) {
489 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800490 }
491
Lei Wenf8155a42011-02-28 10:32:11 +0800492 info->state = STATE_DMA_DONE;
493 enable_int(info, NDCR_INT_MASK);
494 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800495}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300496#else
497static void start_data_dma(struct pxa3xx_nand_info *info)
498{}
499#endif
eric miaofe69af02008-02-14 15:48:23 +0800500
501static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
502{
503 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300504 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700505 unsigned int ready, cmd_done;
506
507 if (info->cs == 0) {
508 ready = NDSR_FLASH_RDY;
509 cmd_done = NDSR_CS0_CMDD;
510 } else {
511 ready = NDSR_RDY;
512 cmd_done = NDSR_CS1_CMDD;
513 }
eric miaofe69af02008-02-14 15:48:23 +0800514
515 status = nand_readl(info, NDSR);
516
Lei Wenf8155a42011-02-28 10:32:11 +0800517 if (status & NDSR_DBERR)
518 info->retcode = ERR_DBERR;
519 if (status & NDSR_SBERR)
520 info->retcode = ERR_SBERR;
521 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
522 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800523 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800524 disable_int(info, NDCR_INT_MASK);
525 info->state = (status & NDSR_RDDREQ) ?
526 STATE_DMA_READING : STATE_DMA_WRITING;
527 start_data_dma(info);
528 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800529 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800530 info->state = (status & NDSR_RDDREQ) ?
531 STATE_PIO_READING : STATE_PIO_WRITING;
532 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800533 }
Lei Wenf8155a42011-02-28 10:32:11 +0800534 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700535 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800536 info->state = STATE_CMD_DONE;
537 is_completed = 1;
538 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700539 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800540 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300541 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800542 }
Lei Wenf8155a42011-02-28 10:32:11 +0800543
544 if (status & NDSR_WRCMDREQ) {
545 nand_writel(info, NDSR, NDSR_WRCMDREQ);
546 status &= ~NDSR_WRCMDREQ;
547 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300548
549 /*
550 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
551 * must be loaded by writing directly either 12 or 16
552 * bytes directly to NDCB0, four bytes at a time.
553 *
554 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
555 * but each NDCBx register can be read.
556 */
Lei Wenf8155a42011-02-28 10:32:11 +0800557 nand_writel(info, NDCB0, info->ndcb0);
558 nand_writel(info, NDCB0, info->ndcb1);
559 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300560
561 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
562 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
563 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800564 }
Lei Wenf8155a42011-02-28 10:32:11 +0800565
566 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800567 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800568 if (is_completed)
569 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300570 if (is_ready)
571 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800572NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800573 return IRQ_HANDLED;
574}
575
eric miaofe69af02008-02-14 15:48:23 +0800576static inline int is_buf_blank(uint8_t *buf, size_t len)
577{
578 for (; len > 0; len--)
579 if (*buf++ != 0xff)
580 return 0;
581 return 1;
582}
583
Lei Wen4eb2da82011-02-28 10:32:13 +0800584static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
585 uint16_t column, int page_addr)
586{
Lei Wend4568822011-07-14 20:44:32 -0700587 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700588 struct pxa3xx_nand_host *host;
589 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800590
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700591 host = info->host[info->cs];
592 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800593 addr_cycle = 0;
594 exec_cmd = 1;
595
596 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800597 info->buf_start = 0;
598 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800599 info->oob_size = 0;
600 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300601 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800602 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700603 if (info->cs != 0)
604 info->ndcb0 = NDCB0_CSEL;
605 else
606 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800607
608 switch (command) {
609 case NAND_CMD_READ0:
610 case NAND_CMD_PAGEPROG:
611 info->use_ecc = 1;
612 case NAND_CMD_READOOB:
613 pxa3xx_set_datasize(info);
614 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300615 case NAND_CMD_PARAM:
616 info->use_spare = 0;
617 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800618 case NAND_CMD_SEQIN:
619 exec_cmd = 0;
620 break;
621 default:
622 info->ndcb1 = 0;
623 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300624 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800625 break;
626 }
627
Lei Wend4568822011-07-14 20:44:32 -0700628 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
629 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800630
631 switch (command) {
632 case NAND_CMD_READOOB:
633 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300634 info->buf_start = column;
635 info->ndcb0 |= NDCB0_CMD_TYPE(0)
636 | addr_cycle
637 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800638
Ezequiel Garciaec821352013-08-12 14:14:54 -0300639 if (command == NAND_CMD_READOOB)
640 info->buf_start += mtd->writesize;
641
642 /* Second command setting for large pages */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300643 if (mtd->writesize >= PAGE_CHUNK_SIZE)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300644 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800645
646 case NAND_CMD_SEQIN:
647 /* small page addr setting */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300648 if (unlikely(mtd->writesize < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800649 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
650 | (column & 0xFF);
651
652 info->ndcb2 = 0;
653 } else {
654 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
655 | (column & 0xFFFF);
656
657 if (page_addr & 0xFF0000)
658 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
659 else
660 info->ndcb2 = 0;
661 }
662
663 info->buf_count = mtd->writesize + mtd->oobsize;
664 memset(info->data_buff, 0xFF, info->buf_count);
665
666 break;
667
668 case NAND_CMD_PAGEPROG:
669 if (is_buf_blank(info->data_buff,
670 (mtd->writesize + mtd->oobsize))) {
671 exec_cmd = 0;
672 break;
673 }
674
Lei Wen4eb2da82011-02-28 10:32:13 +0800675 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
676 | NDCB0_AUTO_RS
677 | NDCB0_ST_ROW_EN
678 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300679 | (NAND_CMD_PAGEPROG << 8)
680 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800681 | addr_cycle;
682 break;
683
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300684 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300685 info->buf_count = 256;
686 info->ndcb0 |= NDCB0_CMD_TYPE(0)
687 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300688 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300689 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300690 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300691 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300692 info->data_size = 256;
693 break;
694
Lei Wen4eb2da82011-02-28 10:32:13 +0800695 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700696 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800697 info->ndcb0 |= NDCB0_CMD_TYPE(3)
698 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300699 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300700 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800701
702 info->data_size = 8;
703 break;
704 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800705 info->buf_count = 1;
706 info->ndcb0 |= NDCB0_CMD_TYPE(4)
707 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300708 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800709
710 info->data_size = 8;
711 break;
712
713 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800714 info->ndcb0 |= NDCB0_CMD_TYPE(2)
715 | NDCB0_AUTO_RS
716 | NDCB0_ADDR_CYC(3)
717 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300718 | (NAND_CMD_ERASE2 << 8)
719 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800720 info->ndcb1 = page_addr;
721 info->ndcb2 = 0;
722
723 break;
724 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800725 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300726 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800727
728 break;
729
730 case NAND_CMD_ERASE2:
731 exec_cmd = 0;
732 break;
733
734 default:
735 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700736 dev_err(&info->pdev->dev, "non-supported command %x\n",
737 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800738 break;
739 }
740
741 return exec_cmd;
742}
743
eric miaofe69af02008-02-14 15:48:23 +0800744static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100745 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800746{
Lei Wend4568822011-07-14 20:44:32 -0700747 struct pxa3xx_nand_host *host = mtd->priv;
748 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800749 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800750
Lei Wen4eb2da82011-02-28 10:32:13 +0800751 /*
752 * if this is a x16 device ,then convert the input
753 * "byte" address into a "word" address appropriate
754 * for indexing a word-oriented device
755 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300756 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800757 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800758
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700759 /*
760 * There may be different NAND chip hooked to
761 * different chip select, so check whether
762 * chip select has been changed, if yes, reset the timing
763 */
764 if (info->cs != host->cs) {
765 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300766 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
767 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700768 }
769
Lei Wend4568822011-07-14 20:44:32 -0700770 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800771 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800772 if (exec_cmd) {
773 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300774 init_completion(&info->dev_ready);
775 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800776 pxa3xx_nand_start(info);
777
778 ret = wait_for_completion_timeout(&info->cmd_complete,
779 CHIP_DELAY_TIMEOUT);
780 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700781 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800782 /* Stop State Machine for next command cycle */
783 pxa3xx_nand_stop(info);
784 }
eric miaofe69af02008-02-14 15:48:23 +0800785 }
Lei Wend4568822011-07-14 20:44:32 -0700786 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800787}
788
Josh Wufdbad98d2012-06-25 18:07:45 +0800789static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700790 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800791{
792 chip->write_buf(mtd, buf, mtd->writesize);
793 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800794
795 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800796}
797
798static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700799 struct nand_chip *chip, uint8_t *buf, int oob_required,
800 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800801{
Lei Wend4568822011-07-14 20:44:32 -0700802 struct pxa3xx_nand_host *host = mtd->priv;
803 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300804 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800805
806 chip->read_buf(mtd, buf, mtd->writesize);
807 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
808
809 if (info->retcode == ERR_SBERR) {
810 switch (info->use_ecc) {
811 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300812 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800813 mtd->ecc_stats.corrected++;
814 break;
815 case 0:
816 default:
817 break;
818 }
819 } else if (info->retcode == ERR_DBERR) {
820 /*
821 * for blank page (all 0xff), HW will calculate its ECC as
822 * 0, which is different from the ECC information within
823 * OOB, ignore such double bit errors
824 */
825 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700826 info->retcode = ERR_NONE;
827 else
Lei Wenf8155a42011-02-28 10:32:11 +0800828 mtd->ecc_stats.failed++;
829 }
830
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300831 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800832}
833
eric miaofe69af02008-02-14 15:48:23 +0800834static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
835{
Lei Wend4568822011-07-14 20:44:32 -0700836 struct pxa3xx_nand_host *host = mtd->priv;
837 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800838 char retval = 0xFF;
839
840 if (info->buf_start < info->buf_count)
841 /* Has just send a new command? */
842 retval = info->data_buff[info->buf_start++];
843
844 return retval;
845}
846
847static u16 pxa3xx_nand_read_word(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 u16 retval = 0xFFFF;
852
853 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
854 retval = *((u16 *)(info->data_buff+info->buf_start));
855 info->buf_start += 2;
856 }
857 return retval;
858}
859
860static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
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 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
865
866 memcpy(buf, info->data_buff + info->buf_start, real_len);
867 info->buf_start += real_len;
868}
869
870static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
871 const uint8_t *buf, int len)
872{
Lei Wend4568822011-07-14 20:44:32 -0700873 struct pxa3xx_nand_host *host = mtd->priv;
874 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800875 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
876
877 memcpy(info->data_buff + info->buf_start, buf, real_len);
878 info->buf_start += real_len;
879}
880
eric miaofe69af02008-02-14 15:48:23 +0800881static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
882{
883 return;
884}
885
886static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
887{
Lei Wend4568822011-07-14 20:44:32 -0700888 struct pxa3xx_nand_host *host = mtd->priv;
889 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300890 int ret;
891
892 if (info->need_wait) {
893 ret = wait_for_completion_timeout(&info->dev_ready,
894 CHIP_DELAY_TIMEOUT);
895 info->need_wait = 0;
896 if (!ret) {
897 dev_err(&info->pdev->dev, "Ready time out!!!\n");
898 return NAND_STATUS_FAIL;
899 }
900 }
eric miaofe69af02008-02-14 15:48:23 +0800901
902 /* pxa3xx_nand_send_command has waited for command complete */
903 if (this->state == FL_WRITING || this->state == FL_ERASING) {
904 if (info->retcode == ERR_NONE)
905 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300906 else
907 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +0800908 }
909
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300910 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +0800911}
912
eric miaofe69af02008-02-14 15:48:23 +0800913static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200914 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800915{
916 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900917 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700918 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800919 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800920
Lei Wenda675b42011-07-14 20:44:31 -0700921 if (f->page_size != 2048 && f->page_size != 512) {
922 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800923 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700924 }
eric miaofe69af02008-02-14 15:48:23 +0800925
Lei Wenda675b42011-07-14 20:44:31 -0700926 if (f->flash_width != 16 && f->flash_width != 8) {
927 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800928 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700929 }
eric miaofe69af02008-02-14 15:48:23 +0800930
931 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700932 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800933
934 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700935 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800936
937 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700938 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800939 else
Lei Wend4568822011-07-14 20:44:32 -0700940 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800941
942 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700943 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800944 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
945 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
946 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
947 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
948
Lei Wend4568822011-07-14 20:44:32 -0700949 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800950 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
951
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300952 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800953
Lei Wend4568822011-07-14 20:44:32 -0700954 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800955 return 0;
956}
957
Mike Rapoportf2710492009-02-17 13:54:47 +0200958static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
959{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700960 /*
961 * We set 0 by hard coding here, for we don't support keep_config
962 * when there is more than one chip attached to the controller
963 */
964 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200965 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200966
Lei Wend4568822011-07-14 20:44:32 -0700967 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300968 /* Controller's FIFO size */
969 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -0700970 host->read_id_bytes = 4;
971 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300972 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -0700973 host->read_id_bytes = 2;
974 }
975
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300976 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
977 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
978 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200979 return 0;
980}
981
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300982#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800983static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
984{
985 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300986 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800987
988 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300989 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800990 if (info->data_buff == NULL)
991 return -ENOMEM;
992 return 0;
993 }
994
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300995 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800996 &info->data_buff_phys, GFP_KERNEL);
997 if (info->data_buff == NULL) {
998 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
999 return -ENOMEM;
1000 }
1001
eric miaofe69af02008-02-14 15:48:23 +08001002 info->data_desc = (void *)info->data_buff + data_desc_offset;
1003 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
1004
1005 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
1006 pxa3xx_nand_data_dma_irq, info);
1007 if (info->data_dma_ch < 0) {
1008 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001009 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +08001010 info->data_buff, info->data_buff_phys);
1011 return info->data_dma_ch;
1012 }
1013
Ezequiel Garcia95b26562013-10-04 15:30:37 -03001014 /*
1015 * Now that DMA buffers are allocated we turn on
1016 * DMA proper for I/O operations.
1017 */
1018 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +08001019 return 0;
1020}
1021
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001022static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1023{
1024 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001025 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001026 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001027 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001028 info->data_buff, info->data_buff_phys);
1029 } else {
1030 kfree(info->data_buff);
1031 }
1032}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001033#else
1034static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1035{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001036 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001037 if (info->data_buff == NULL)
1038 return -ENOMEM;
1039 return 0;
1040}
1041
1042static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1043{
1044 kfree(info->data_buff);
1045}
1046#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001047
Lei Wen401e67e2011-02-28 10:32:14 +08001048static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001049{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001050 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001051 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001052 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001053
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001054 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001055 chip = mtd->priv;
1056
Lei Wen401e67e2011-02-28 10:32:14 +08001057 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001058 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1059 if (ret)
1060 return ret;
1061
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001062 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001063 ret = chip->waitfunc(mtd, chip);
1064 if (ret & NAND_STATUS_FAIL)
1065 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001066
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001067 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001068}
eric miaofe69af02008-02-14 15:48:23 +08001069
Lei Wen401e67e2011-02-28 10:32:14 +08001070static int pxa3xx_nand_scan(struct mtd_info *mtd)
1071{
Lei Wend4568822011-07-14 20:44:32 -07001072 struct pxa3xx_nand_host *host = mtd->priv;
1073 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001074 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001075 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001076 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001077 const struct pxa3xx_nand_flash *f = NULL;
1078 struct nand_chip *chip = mtd->priv;
1079 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001080 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001081 int i, ret, num;
1082
1083 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001084 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001085
1086 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001087 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001088 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1089 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001090
Lei Wend4568822011-07-14 20:44:32 -07001091 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001092 }
1093
1094 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1095 id = *((uint16_t *)(info->data_buff));
1096 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001097 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001098 else {
Lei Wenda675b42011-07-14 20:44:31 -07001099 dev_warn(&info->pdev->dev,
1100 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001101
1102 return -EINVAL;
1103 }
1104
1105 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1106 for (i = 0; i < num; i++) {
1107 if (i < pdata->num_flash)
1108 f = pdata->flash + i;
1109 else
1110 f = &builtin_flash_types[i - pdata->num_flash + 1];
1111
1112 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001113 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001114 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001115 }
1116
Lei Wen4332c112011-03-03 11:27:01 +08001117 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001118 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001119
1120 return -EINVAL;
1121 }
1122
Lei Wend4568822011-07-14 20:44:32 -07001123 ret = pxa3xx_nand_config_flash(info, f);
1124 if (ret) {
1125 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1126 return ret;
1127 }
1128
Lei Wen4332c112011-03-03 11:27:01 +08001129 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001130 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001131 pxa3xx_flash_ids[0].pagesize = f->page_size;
1132 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1133 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1134 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1135 if (f->flash_width == 16)
1136 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001137 pxa3xx_flash_ids[1].name = NULL;
1138 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001139KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001140 chip->ecc.mode = NAND_ECC_HW;
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -03001141 chip->ecc.size = info->fifo_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001142 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001143
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001144 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001145 chip->options |= NAND_BUSWIDTH_16;
1146
Lei Wen0fab0282011-06-07 03:01:06 -07001147 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001148 return -ENODEV;
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001149
1150 if (pdata->flash_bbt) {
1151 /*
1152 * We'll use a bad block table stored in-flash and don't
1153 * allow writing the bad block marker to the flash.
1154 */
1155 chip->bbt_options |= NAND_BBT_USE_FLASH |
1156 NAND_BBT_NO_OOB_BBM;
1157 chip->bbt_td = &bbt_main_descr;
1158 chip->bbt_md = &bbt_mirror_descr;
1159 }
1160
Lei Wen4332c112011-03-03 11:27:01 +08001161 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001162 if (mtd->writesize >= 2048)
1163 host->col_addr_cycles = 2;
1164 else
1165 host->col_addr_cycles = 1;
1166
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001167 /* release the initial buffer */
1168 kfree(info->data_buff);
1169
1170 /* allocate the real data + oob buffer */
1171 info->buf_size = mtd->writesize + mtd->oobsize;
1172 ret = pxa3xx_nand_init_buff(info);
1173 if (ret)
1174 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001175 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001176
Lei Wen4332c112011-03-03 11:27:01 +08001177 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001178 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001179 else
Lei Wend4568822011-07-14 20:44:32 -07001180 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001181 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001182}
1183
Lei Wend4568822011-07-14 20:44:32 -07001184static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001185{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001186 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001187 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001188 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001189 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001190 struct mtd_info *mtd;
1191 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001192 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001193
Jingoo Han453810b2013-07-30 17:18:33 +09001194 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001195 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1196 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1197 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001198 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001199
eric miaofe69af02008-02-14 15:48:23 +08001200 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001201 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001202 for (cs = 0; cs < pdata->num_cs; cs++) {
1203 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1204 (sizeof(*mtd) + sizeof(*host)) * cs);
1205 chip = (struct nand_chip *)(&mtd[1]);
1206 host = (struct pxa3xx_nand_host *)chip;
1207 info->host[cs] = host;
1208 host->mtd = mtd;
1209 host->cs = cs;
1210 host->info_data = info;
1211 mtd->priv = host;
1212 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001213
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001214 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1215 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1216 chip->controller = &info->controller;
1217 chip->waitfunc = pxa3xx_nand_waitfunc;
1218 chip->select_chip = pxa3xx_nand_select_chip;
1219 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1220 chip->read_word = pxa3xx_nand_read_word;
1221 chip->read_byte = pxa3xx_nand_read_byte;
1222 chip->read_buf = pxa3xx_nand_read_buf;
1223 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001224 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001225 }
Lei Wen401e67e2011-02-28 10:32:14 +08001226
1227 spin_lock_init(&chip->controller->lock);
1228 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001229 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001230 if (IS_ERR(info->clk)) {
1231 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001232 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001233 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001234 ret = clk_prepare_enable(info->clk);
1235 if (ret < 0)
1236 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001237
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001238 if (use_dma) {
1239 /*
1240 * This is a dirty hack to make this driver work from
1241 * devicetree bindings. It can be removed once we have
1242 * a prober DMA controller framework for DT.
1243 */
1244 if (pdev->dev.of_node &&
1245 of_machine_is_compatible("marvell,pxa3xx")) {
1246 info->drcmr_dat = 97;
1247 info->drcmr_cmd = 99;
1248 } else {
1249 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1250 if (r == NULL) {
1251 dev_err(&pdev->dev,
1252 "no resource defined for data DMA\n");
1253 ret = -ENXIO;
1254 goto fail_disable_clk;
1255 }
1256 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001257
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001258 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1259 if (r == NULL) {
1260 dev_err(&pdev->dev,
1261 "no resource defined for cmd DMA\n");
1262 ret = -ENXIO;
1263 goto fail_disable_clk;
1264 }
1265 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001266 }
eric miaofe69af02008-02-14 15:48:23 +08001267 }
eric miaofe69af02008-02-14 15:48:23 +08001268
1269 irq = platform_get_irq(pdev, 0);
1270 if (irq < 0) {
1271 dev_err(&pdev->dev, "no IRQ resource defined\n");
1272 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001273 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001274 }
1275
1276 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001277 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1278 if (IS_ERR(info->mmio_base)) {
1279 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001280 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001281 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001282 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001283
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001284 /* Allocate a buffer to allow flash detection */
1285 info->buf_size = INIT_BUFFER_SIZE;
1286 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1287 if (info->data_buff == NULL) {
1288 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001289 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001290 }
eric miaofe69af02008-02-14 15:48:23 +08001291
Haojian Zhuang346e1252009-09-10 14:27:23 +08001292 /* initialize all interrupts to be disabled */
1293 disable_int(info, NDSR_MASK);
1294
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001295 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001296 if (ret < 0) {
1297 dev_err(&pdev->dev, "failed to request IRQ\n");
1298 goto fail_free_buf;
1299 }
1300
Lei Wene353a202011-03-03 11:08:30 +08001301 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001302
Lei Wend4568822011-07-14 20:44:32 -07001303 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001304
eric miaofe69af02008-02-14 15:48:23 +08001305fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001306 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001307 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001308fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001309 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001310 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001311}
1312
1313static int pxa3xx_nand_remove(struct platform_device *pdev)
1314{
Lei Wene353a202011-03-03 11:08:30 +08001315 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001316 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001317 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001318
Lei Wend4568822011-07-14 20:44:32 -07001319 if (!info)
1320 return 0;
1321
Jingoo Han453810b2013-07-30 17:18:33 +09001322 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001323
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001324 irq = platform_get_irq(pdev, 0);
1325 if (irq >= 0)
1326 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001327 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001328
Ezequiel Garciafb320612013-04-17 13:38:12 -03001329 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001330
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001331 for (cs = 0; cs < pdata->num_cs; cs++)
1332 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001333 return 0;
1334}
1335
Daniel Mack1e7ba632012-07-22 19:51:02 +02001336static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1337{
1338 struct pxa3xx_nand_platform_data *pdata;
1339 struct device_node *np = pdev->dev.of_node;
1340 const struct of_device_id *of_id =
1341 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1342
1343 if (!of_id)
1344 return 0;
1345
1346 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1347 if (!pdata)
1348 return -ENOMEM;
1349
1350 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1351 pdata->enable_arbiter = 1;
1352 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1353 pdata->keep_config = 1;
1354 of_property_read_u32(np, "num-cs", &pdata->num_cs);
Ezequiel Garcia776f2652013-11-14 18:25:28 -03001355 pdata->flash_bbt = of_get_nand_on_flash_bbt(np);
Daniel Mack1e7ba632012-07-22 19:51:02 +02001356
1357 pdev->dev.platform_data = pdata;
1358
1359 return 0;
1360}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001361
Lei Wene353a202011-03-03 11:08:30 +08001362static int pxa3xx_nand_probe(struct platform_device *pdev)
1363{
1364 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001365 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001366 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001367 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001368
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001369#ifndef ARCH_HAS_DMA
1370 if (use_dma) {
1371 use_dma = 0;
1372 dev_warn(&pdev->dev,
1373 "This platform can't do DMA on this device\n");
1374 }
1375#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001376 ret = pxa3xx_nand_probe_dt(pdev);
1377 if (ret)
1378 return ret;
1379
Jingoo Han453810b2013-07-30 17:18:33 +09001380 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001381 if (!pdata) {
1382 dev_err(&pdev->dev, "no platform data defined\n");
1383 return -ENODEV;
1384 }
1385
Lei Wend4568822011-07-14 20:44:32 -07001386 ret = alloc_nand_resource(pdev);
1387 if (ret) {
1388 dev_err(&pdev->dev, "alloc nand resource failed\n");
1389 return ret;
1390 }
Lei Wene353a202011-03-03 11:08:30 +08001391
Lei Wend4568822011-07-14 20:44:32 -07001392 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001393 probe_success = 0;
1394 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001395 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001396
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001397 /*
1398 * The mtd name matches the one used in 'mtdparts' kernel
1399 * parameter. This name cannot be changed or otherwise
1400 * user's mtd partitions configuration would get broken.
1401 */
1402 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001403 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001404 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001405 if (ret) {
1406 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1407 cs);
1408 continue;
1409 }
1410
Daniel Mack1e7ba632012-07-22 19:51:02 +02001411 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001412 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001413 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001414 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001415 if (!ret)
1416 probe_success = 1;
1417 }
1418
1419 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001420 pxa3xx_nand_remove(pdev);
1421 return -ENODEV;
1422 }
1423
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001424 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001425}
1426
eric miaofe69af02008-02-14 15:48:23 +08001427#ifdef CONFIG_PM
1428static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1429{
Lei Wene353a202011-03-03 11:08:30 +08001430 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001431 struct pxa3xx_nand_platform_data *pdata;
1432 struct mtd_info *mtd;
1433 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001434
Jingoo Han453810b2013-07-30 17:18:33 +09001435 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001436 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001437 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1438 return -EAGAIN;
1439 }
1440
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001441 for (cs = 0; cs < pdata->num_cs; cs++) {
1442 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001443 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001444 }
1445
eric miaofe69af02008-02-14 15:48:23 +08001446 return 0;
1447}
1448
1449static int pxa3xx_nand_resume(struct platform_device *pdev)
1450{
Lei Wene353a202011-03-03 11:08:30 +08001451 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001452 struct pxa3xx_nand_platform_data *pdata;
1453 struct mtd_info *mtd;
1454 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001455
Jingoo Han453810b2013-07-30 17:18:33 +09001456 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001457 /* We don't want to handle interrupt without calling mtd routine */
1458 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001459
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001460 /*
1461 * Directly set the chip select to a invalid value,
1462 * then the driver would reset the timing according
1463 * to current chip select at the beginning of cmdfunc
1464 */
1465 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001466
Lei Wen051fc412011-07-14 20:44:30 -07001467 /*
1468 * As the spec says, the NDSR would be updated to 0x1800 when
1469 * doing the nand_clk disable/enable.
1470 * To prevent it damaging state machine of the driver, clear
1471 * all status before resume
1472 */
1473 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001474 for (cs = 0; cs < pdata->num_cs; cs++) {
1475 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001476 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001477 }
1478
Lei Wen18c81b12010-08-17 17:25:57 +08001479 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001480}
1481#else
1482#define pxa3xx_nand_suspend NULL
1483#define pxa3xx_nand_resume NULL
1484#endif
1485
1486static struct platform_driver pxa3xx_nand_driver = {
1487 .driver = {
1488 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301489 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001490 },
1491 .probe = pxa3xx_nand_probe,
1492 .remove = pxa3xx_nand_remove,
1493 .suspend = pxa3xx_nand_suspend,
1494 .resume = pxa3xx_nand_resume,
1495};
1496
Axel Linf99640d2011-11-27 20:45:03 +08001497module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001498
1499MODULE_LICENSE("GPL");
1500MODULE_DESCRIPTION("PXA3xx NAND controller driver");