blob: a30116bfff4d393216878fdfc18496258b2999b0 [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>
eric miaofe69af02008-02-14 15:48:23 +080029
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030030#if defined(CONFIG_ARCH_PXA) || defined(CONFIG_ARCH_MMP)
31#define ARCH_HAS_DMA
32#endif
33
34#ifdef ARCH_HAS_DMA
Eric Miaoafb5b5c2008-12-01 11:43:08 +080035#include <mach/dma.h>
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -030036#endif
37
Arnd Bergmann293b2da2012-08-24 15:16:48 +020038#include <linux/platform_data/mtd-nand-pxa3xx.h>
eric miaofe69af02008-02-14 15:48:23 +080039
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -030040#define NAND_DEV_READY_TIMEOUT 50
eric miaofe69af02008-02-14 15:48:23 +080041#define CHIP_DELAY_TIMEOUT (2 * HZ/10)
Lei Wenf8155a42011-02-28 10:32:11 +080042#define NAND_STOP_DELAY (2 * HZ/50)
Lei Wen4eb2da82011-02-28 10:32:13 +080043#define PAGE_CHUNK_SIZE (2048)
eric miaofe69af02008-02-14 15:48:23 +080044
Ezequiel Garcia62e8b852013-10-04 15:30:38 -030045/*
46 * Define a buffer size for the initial command that detects the flash device:
47 * STATUS, READID and PARAM. The largest of these is the PARAM command,
48 * needing 256 bytes.
49 */
50#define INIT_BUFFER_SIZE 256
51
eric miaofe69af02008-02-14 15:48:23 +080052/* registers and bit definitions */
53#define NDCR (0x00) /* Control register */
54#define NDTR0CS0 (0x04) /* Timing Parameter 0 for CS0 */
55#define NDTR1CS0 (0x0C) /* Timing Parameter 1 for CS0 */
56#define NDSR (0x14) /* Status Register */
57#define NDPCR (0x18) /* Page Count Register */
58#define NDBDR0 (0x1C) /* Bad Block Register 0 */
59#define NDBDR1 (0x20) /* Bad Block Register 1 */
60#define NDDB (0x40) /* Data Buffer */
61#define NDCB0 (0x48) /* Command Buffer0 */
62#define NDCB1 (0x4C) /* Command Buffer1 */
63#define NDCB2 (0x50) /* Command Buffer2 */
64
65#define NDCR_SPARE_EN (0x1 << 31)
66#define NDCR_ECC_EN (0x1 << 30)
67#define NDCR_DMA_EN (0x1 << 29)
68#define NDCR_ND_RUN (0x1 << 28)
69#define NDCR_DWIDTH_C (0x1 << 27)
70#define NDCR_DWIDTH_M (0x1 << 26)
71#define NDCR_PAGE_SZ (0x1 << 24)
72#define NDCR_NCSX (0x1 << 23)
73#define NDCR_ND_MODE (0x3 << 21)
74#define NDCR_NAND_MODE (0x0)
75#define NDCR_CLR_PG_CNT (0x1 << 20)
Lei Wenf8155a42011-02-28 10:32:11 +080076#define NDCR_STOP_ON_UNCOR (0x1 << 19)
eric miaofe69af02008-02-14 15:48:23 +080077#define NDCR_RD_ID_CNT_MASK (0x7 << 16)
78#define NDCR_RD_ID_CNT(x) (((x) << 16) & NDCR_RD_ID_CNT_MASK)
79
80#define NDCR_RA_START (0x1 << 15)
81#define NDCR_PG_PER_BLK (0x1 << 14)
82#define NDCR_ND_ARB_EN (0x1 << 12)
Lei Wenf8155a42011-02-28 10:32:11 +080083#define NDCR_INT_MASK (0xFFF)
eric miaofe69af02008-02-14 15:48:23 +080084
85#define NDSR_MASK (0xfff)
Lei Wenf8155a42011-02-28 10:32:11 +080086#define NDSR_RDY (0x1 << 12)
87#define NDSR_FLASH_RDY (0x1 << 11)
eric miaofe69af02008-02-14 15:48:23 +080088#define NDSR_CS0_PAGED (0x1 << 10)
89#define NDSR_CS1_PAGED (0x1 << 9)
90#define NDSR_CS0_CMDD (0x1 << 8)
91#define NDSR_CS1_CMDD (0x1 << 7)
92#define NDSR_CS0_BBD (0x1 << 6)
93#define NDSR_CS1_BBD (0x1 << 5)
94#define NDSR_DBERR (0x1 << 4)
95#define NDSR_SBERR (0x1 << 3)
96#define NDSR_WRDREQ (0x1 << 2)
97#define NDSR_RDDREQ (0x1 << 1)
98#define NDSR_WRCMDREQ (0x1)
99
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300100#define NDCB0_LEN_OVRD (0x1 << 28)
Lei Wen4eb2da82011-02-28 10:32:13 +0800101#define NDCB0_ST_ROW_EN (0x1 << 26)
eric miaofe69af02008-02-14 15:48:23 +0800102#define NDCB0_AUTO_RS (0x1 << 25)
103#define NDCB0_CSEL (0x1 << 24)
104#define NDCB0_CMD_TYPE_MASK (0x7 << 21)
105#define NDCB0_CMD_TYPE(x) (((x) << 21) & NDCB0_CMD_TYPE_MASK)
106#define NDCB0_NC (0x1 << 20)
107#define NDCB0_DBC (0x1 << 19)
108#define NDCB0_ADDR_CYC_MASK (0x7 << 16)
109#define NDCB0_ADDR_CYC(x) (((x) << 16) & NDCB0_ADDR_CYC_MASK)
110#define NDCB0_CMD2_MASK (0xff << 8)
111#define NDCB0_CMD1_MASK (0xff)
112#define NDCB0_ADDR_CYC_SHIFT (16)
113
eric miaofe69af02008-02-14 15:48:23 +0800114/* macros for registers read/write */
115#define nand_writel(info, off, val) \
116 __raw_writel((val), (info)->mmio_base + (off))
117
118#define nand_readl(info, off) \
119 __raw_readl((info)->mmio_base + (off))
120
121/* error code and state */
122enum {
123 ERR_NONE = 0,
124 ERR_DMABUSERR = -1,
125 ERR_SENDCMD = -2,
126 ERR_DBERR = -3,
127 ERR_BBERR = -4,
Yeasah Pell223cf6c2009-07-01 18:11:35 +0300128 ERR_SBERR = -5,
eric miaofe69af02008-02-14 15:48:23 +0800129};
130
131enum {
Lei Wenf8155a42011-02-28 10:32:11 +0800132 STATE_IDLE = 0,
Lei Wend4568822011-07-14 20:44:32 -0700133 STATE_PREPARED,
eric miaofe69af02008-02-14 15:48:23 +0800134 STATE_CMD_HANDLE,
135 STATE_DMA_READING,
136 STATE_DMA_WRITING,
137 STATE_DMA_DONE,
138 STATE_PIO_READING,
139 STATE_PIO_WRITING,
Lei Wenf8155a42011-02-28 10:32:11 +0800140 STATE_CMD_DONE,
141 STATE_READY,
eric miaofe69af02008-02-14 15:48:23 +0800142};
143
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300144enum pxa3xx_nand_variant {
145 PXA3XX_NAND_VARIANT_PXA,
146 PXA3XX_NAND_VARIANT_ARMADA370,
147};
148
Lei Wend4568822011-07-14 20:44:32 -0700149struct pxa3xx_nand_host {
150 struct nand_chip chip;
Lei Wend4568822011-07-14 20:44:32 -0700151 struct mtd_info *mtd;
152 void *info_data;
eric miaofe69af02008-02-14 15:48:23 +0800153
Lei Wend4568822011-07-14 20:44:32 -0700154 /* page size of attached chip */
Lei Wend4568822011-07-14 20:44:32 -0700155 int use_ecc;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700156 int cs;
Lei Wend4568822011-07-14 20:44:32 -0700157
158 /* calculated from pxa3xx_nand_flash data */
159 unsigned int col_addr_cycles;
160 unsigned int row_addr_cycles;
161 size_t read_id_bytes;
162
Lei Wend4568822011-07-14 20:44:32 -0700163};
164
165struct pxa3xx_nand_info {
Lei Wen401e67e2011-02-28 10:32:14 +0800166 struct nand_hw_control controller;
eric miaofe69af02008-02-14 15:48:23 +0800167 struct platform_device *pdev;
eric miaofe69af02008-02-14 15:48:23 +0800168
169 struct clk *clk;
170 void __iomem *mmio_base;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800171 unsigned long mmio_phys;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300172 struct completion cmd_complete, dev_ready;
eric miaofe69af02008-02-14 15:48:23 +0800173
174 unsigned int buf_start;
175 unsigned int buf_count;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300176 unsigned int buf_size;
eric miaofe69af02008-02-14 15:48:23 +0800177
178 /* DMA information */
179 int drcmr_dat;
180 int drcmr_cmd;
181
182 unsigned char *data_buff;
Lei Wen18c81b12010-08-17 17:25:57 +0800183 unsigned char *oob_buff;
eric miaofe69af02008-02-14 15:48:23 +0800184 dma_addr_t data_buff_phys;
eric miaofe69af02008-02-14 15:48:23 +0800185 int data_dma_ch;
186 struct pxa_dma_desc *data_desc;
187 dma_addr_t data_desc_addr;
188
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700189 struct pxa3xx_nand_host *host[NUM_CHIP_SELECT];
eric miaofe69af02008-02-14 15:48:23 +0800190 unsigned int state;
191
Ezequiel Garciac0f3b862013-08-10 16:34:52 -0300192 /*
193 * This driver supports NFCv1 (as found in PXA SoC)
194 * and NFCv2 (as found in Armada 370/XP SoC).
195 */
196 enum pxa3xx_nand_variant variant;
197
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700198 int cs;
eric miaofe69af02008-02-14 15:48:23 +0800199 int use_ecc; /* use HW ECC ? */
200 int use_dma; /* use DMA ? */
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300201 int use_spare; /* use spare ? */
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300202 int need_wait;
eric miaofe69af02008-02-14 15:48:23 +0800203
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300204 unsigned int fifo_size; /* max. data size in the FIFO */
205 unsigned int data_size; /* data to be read from FIFO */
Lei Wend4568822011-07-14 20:44:32 -0700206 unsigned int oob_size;
eric miaofe69af02008-02-14 15:48:23 +0800207 int retcode;
eric miaofe69af02008-02-14 15:48:23 +0800208
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300209 /* cached register value */
210 uint32_t reg_ndcr;
211 uint32_t ndtr0cs0;
212 uint32_t ndtr1cs0;
213
eric miaofe69af02008-02-14 15:48:23 +0800214 /* generated NDCBx register values */
215 uint32_t ndcb0;
216 uint32_t ndcb1;
217 uint32_t ndcb2;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300218 uint32_t ndcb3;
eric miaofe69af02008-02-14 15:48:23 +0800219};
220
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030221static bool use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800222module_param(use_dma, bool, 0444);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300223MODULE_PARM_DESC(use_dma, "enable DMA for data transferring to/from NAND HW");
eric miaofe69af02008-02-14 15:48:23 +0800224
Lei Wenc1f82472010-08-17 13:50:23 +0800225static struct pxa3xx_nand_timing timing[] = {
Lei Wen227a8862010-08-18 18:00:03 +0800226 { 40, 80, 60, 100, 80, 100, 90000, 400, 40, },
227 { 10, 0, 20, 40, 30, 40, 11123, 110, 10, },
228 { 10, 25, 15, 25, 15, 30, 25000, 60, 10, },
229 { 10, 35, 15, 25, 15, 25, 25000, 60, 10, },
eric miaofe69af02008-02-14 15:48:23 +0800230};
231
Lei Wenc1f82472010-08-17 13:50:23 +0800232static struct pxa3xx_nand_flash builtin_flash_types[] = {
Lei Wen4332c112011-03-03 11:27:01 +0800233{ "DEFAULT FLASH", 0, 0, 2048, 8, 8, 0, &timing[0] },
234{ "64MiB 16-bit", 0x46ec, 32, 512, 16, 16, 4096, &timing[1] },
235{ "256MiB 8-bit", 0xdaec, 64, 2048, 8, 8, 2048, &timing[1] },
236{ "4GiB 8-bit", 0xd7ec, 128, 4096, 8, 8, 8192, &timing[1] },
237{ "128MiB 8-bit", 0xa12c, 64, 2048, 8, 8, 1024, &timing[2] },
238{ "128MiB 16-bit", 0xb12c, 64, 2048, 16, 16, 1024, &timing[2] },
239{ "512MiB 8-bit", 0xdc2c, 64, 2048, 8, 8, 4096, &timing[2] },
240{ "512MiB 16-bit", 0xcc2c, 64, 2048, 16, 16, 4096, &timing[2] },
241{ "256MiB 16-bit", 0xba20, 64, 2048, 16, 16, 2048, &timing[3] },
eric miaofe69af02008-02-14 15:48:23 +0800242};
243
Lei Wen227a8862010-08-18 18:00:03 +0800244/* Define a default flash type setting serve as flash detecting only */
245#define DEFAULT_FLASH_TYPE (&builtin_flash_types[0])
246
eric miaofe69af02008-02-14 15:48:23 +0800247#define NDTR0_tCH(c) (min((c), 7) << 19)
248#define NDTR0_tCS(c) (min((c), 7) << 16)
249#define NDTR0_tWH(c) (min((c), 7) << 11)
250#define NDTR0_tWP(c) (min((c), 7) << 8)
251#define NDTR0_tRH(c) (min((c), 7) << 3)
252#define NDTR0_tRP(c) (min((c), 7) << 0)
253
254#define NDTR1_tR(c) (min((c), 65535) << 16)
255#define NDTR1_tWHR(c) (min((c), 15) << 4)
256#define NDTR1_tAR(c) (min((c), 15) << 0)
257
258/* convert nano-seconds to nand flash controller clock cycles */
Axel Lin93b352f2010-08-16 16:09:09 +0800259#define ns2cycle(ns, clk) (int)((ns) * (clk / 1000000) / 1000)
eric miaofe69af02008-02-14 15:48:23 +0800260
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -0300261static struct of_device_id pxa3xx_nand_dt_ids[] = {
262 {
263 .compatible = "marvell,pxa3xx-nand",
264 .data = (void *)PXA3XX_NAND_VARIANT_PXA,
265 },
266 {}
267};
268MODULE_DEVICE_TABLE(of, pxa3xx_nand_dt_ids);
269
270static enum pxa3xx_nand_variant
271pxa3xx_nand_get_variant(struct platform_device *pdev)
272{
273 const struct of_device_id *of_id =
274 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
275 if (!of_id)
276 return PXA3XX_NAND_VARIANT_PXA;
277 return (enum pxa3xx_nand_variant)of_id->data;
278}
279
Lei Wend4568822011-07-14 20:44:32 -0700280static void pxa3xx_nand_set_timing(struct pxa3xx_nand_host *host,
Enrico Scholz7dad4822008-08-29 12:59:50 +0200281 const struct pxa3xx_nand_timing *t)
eric miaofe69af02008-02-14 15:48:23 +0800282{
Lei Wend4568822011-07-14 20:44:32 -0700283 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800284 unsigned long nand_clk = clk_get_rate(info->clk);
285 uint32_t ndtr0, ndtr1;
286
287 ndtr0 = NDTR0_tCH(ns2cycle(t->tCH, nand_clk)) |
288 NDTR0_tCS(ns2cycle(t->tCS, nand_clk)) |
289 NDTR0_tWH(ns2cycle(t->tWH, nand_clk)) |
290 NDTR0_tWP(ns2cycle(t->tWP, nand_clk)) |
291 NDTR0_tRH(ns2cycle(t->tRH, nand_clk)) |
292 NDTR0_tRP(ns2cycle(t->tRP, nand_clk));
293
294 ndtr1 = NDTR1_tR(ns2cycle(t->tR, nand_clk)) |
295 NDTR1_tWHR(ns2cycle(t->tWHR, nand_clk)) |
296 NDTR1_tAR(ns2cycle(t->tAR, nand_clk));
297
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300298 info->ndtr0cs0 = ndtr0;
299 info->ndtr1cs0 = ndtr1;
eric miaofe69af02008-02-14 15:48:23 +0800300 nand_writel(info, NDTR0CS0, ndtr0);
301 nand_writel(info, NDTR1CS0, ndtr1);
302}
303
Ezequiel Garcia6a3e4862013-11-07 12:17:18 -0300304/*
305 * Set the data and OOB size, depending on the selected
306 * spare and ECC configuration.
307 * Only applicable to READ0, READOOB and PAGEPROG commands.
308 */
Lei Wen18c81b12010-08-17 17:25:57 +0800309static void pxa3xx_set_datasize(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800310{
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300311 int oob_enable = info->reg_ndcr & NDCR_SPARE_EN;
Lei Wen9d8b1042010-08-17 14:09:30 +0800312
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300313 info->data_size = info->fifo_size;
Lei Wen9d8b1042010-08-17 14:09:30 +0800314 if (!oob_enable) {
315 info->oob_size = 0;
316 return;
317 }
318
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300319 switch (info->fifo_size) {
eric miaofe69af02008-02-14 15:48:23 +0800320 case 2048:
Lei Wen9d8b1042010-08-17 14:09:30 +0800321 info->oob_size = (info->use_ecc) ? 40 : 64;
eric miaofe69af02008-02-14 15:48:23 +0800322 break;
323 case 512:
Lei Wen9d8b1042010-08-17 14:09:30 +0800324 info->oob_size = (info->use_ecc) ? 8 : 16;
eric miaofe69af02008-02-14 15:48:23 +0800325 break;
eric miaofe69af02008-02-14 15:48:23 +0800326 }
Lei Wen18c81b12010-08-17 17:25:57 +0800327}
328
Lei Wenf8155a42011-02-28 10:32:11 +0800329/**
330 * NOTE: it is a must to set ND_RUN firstly, then write
331 * command buffer, otherwise, it does not work.
332 * We enable all the interrupt at the same time, and
333 * let pxa3xx_nand_irq to handle all logic.
334 */
335static void pxa3xx_nand_start(struct pxa3xx_nand_info *info)
336{
337 uint32_t ndcr;
338
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300339 ndcr = info->reg_ndcr;
Ezequiel Garciacd9d1182013-08-12 14:14:48 -0300340
341 if (info->use_ecc)
342 ndcr |= NDCR_ECC_EN;
343 else
344 ndcr &= ~NDCR_ECC_EN;
345
346 if (info->use_dma)
347 ndcr |= NDCR_DMA_EN;
348 else
349 ndcr &= ~NDCR_DMA_EN;
350
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300351 if (info->use_spare)
352 ndcr |= NDCR_SPARE_EN;
353 else
354 ndcr &= ~NDCR_SPARE_EN;
355
Lei Wenf8155a42011-02-28 10:32:11 +0800356 ndcr |= NDCR_ND_RUN;
357
358 /* clear status bits and run */
359 nand_writel(info, NDCR, 0);
360 nand_writel(info, NDSR, NDSR_MASK);
361 nand_writel(info, NDCR, ndcr);
362}
363
364static void pxa3xx_nand_stop(struct pxa3xx_nand_info *info)
365{
366 uint32_t ndcr;
367 int timeout = NAND_STOP_DELAY;
368
369 /* wait RUN bit in NDCR become 0 */
370 ndcr = nand_readl(info, NDCR);
371 while ((ndcr & NDCR_ND_RUN) && (timeout-- > 0)) {
372 ndcr = nand_readl(info, NDCR);
373 udelay(1);
374 }
375
376 if (timeout <= 0) {
377 ndcr &= ~NDCR_ND_RUN;
378 nand_writel(info, NDCR, ndcr);
379 }
380 /* clear status bits */
381 nand_writel(info, NDSR, NDSR_MASK);
382}
383
Ezequiel Garcia57ff88f2013-08-12 14:14:57 -0300384static void __maybe_unused
385enable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
eric miaofe69af02008-02-14 15:48:23 +0800386{
387 uint32_t ndcr;
388
389 ndcr = nand_readl(info, NDCR);
390 nand_writel(info, NDCR, ndcr & ~int_mask);
391}
392
393static void disable_int(struct pxa3xx_nand_info *info, uint32_t int_mask)
394{
395 uint32_t ndcr;
396
397 ndcr = nand_readl(info, NDCR);
398 nand_writel(info, NDCR, ndcr | int_mask);
399}
400
Lei Wenf8155a42011-02-28 10:32:11 +0800401static void handle_data_pio(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800402{
eric miaofe69af02008-02-14 15:48:23 +0800403 switch (info->state) {
404 case STATE_PIO_WRITING:
405 __raw_writesl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800406 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800407 if (info->oob_size > 0)
408 __raw_writesl(info->mmio_base + NDDB, info->oob_buff,
409 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800410 break;
411 case STATE_PIO_READING:
412 __raw_readsl(info->mmio_base + NDDB, info->data_buff,
Haojian Zhuanga88bdbb2009-09-11 19:33:58 +0800413 DIV_ROUND_UP(info->data_size, 4));
Lei Wen9d8b1042010-08-17 14:09:30 +0800414 if (info->oob_size > 0)
415 __raw_readsl(info->mmio_base + NDDB, info->oob_buff,
416 DIV_ROUND_UP(info->oob_size, 4));
eric miaofe69af02008-02-14 15:48:23 +0800417 break;
418 default:
Lei Wenda675b42011-07-14 20:44:31 -0700419 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
eric miaofe69af02008-02-14 15:48:23 +0800420 info->state);
Lei Wenf8155a42011-02-28 10:32:11 +0800421 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800422 }
eric miaofe69af02008-02-14 15:48:23 +0800423}
424
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300425#ifdef ARCH_HAS_DMA
Lei Wenf8155a42011-02-28 10:32:11 +0800426static void start_data_dma(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +0800427{
428 struct pxa_dma_desc *desc = info->data_desc;
Lei Wen9d8b1042010-08-17 14:09:30 +0800429 int dma_len = ALIGN(info->data_size + info->oob_size, 32);
eric miaofe69af02008-02-14 15:48:23 +0800430
431 desc->ddadr = DDADR_STOP;
432 desc->dcmd = DCMD_ENDIRQEN | DCMD_WIDTH4 | DCMD_BURST32 | dma_len;
433
Lei Wenf8155a42011-02-28 10:32:11 +0800434 switch (info->state) {
435 case STATE_DMA_WRITING:
eric miaofe69af02008-02-14 15:48:23 +0800436 desc->dsadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800437 desc->dtadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800438 desc->dcmd |= DCMD_INCSRCADDR | DCMD_FLOWTRG;
Lei Wenf8155a42011-02-28 10:32:11 +0800439 break;
440 case STATE_DMA_READING:
eric miaofe69af02008-02-14 15:48:23 +0800441 desc->dtadr = info->data_buff_phys;
Haojian Zhuang8638fac2009-09-10 14:11:44 +0800442 desc->dsadr = info->mmio_phys + NDDB;
eric miaofe69af02008-02-14 15:48:23 +0800443 desc->dcmd |= DCMD_INCTRGADDR | DCMD_FLOWSRC;
Lei Wenf8155a42011-02-28 10:32:11 +0800444 break;
445 default:
Lei Wenda675b42011-07-14 20:44:31 -0700446 dev_err(&info->pdev->dev, "%s: invalid state %d\n", __func__,
Lei Wenf8155a42011-02-28 10:32:11 +0800447 info->state);
448 BUG();
eric miaofe69af02008-02-14 15:48:23 +0800449 }
450
451 DRCMR(info->drcmr_dat) = DRCMR_MAPVLD | info->data_dma_ch;
452 DDADR(info->data_dma_ch) = info->data_desc_addr;
453 DCSR(info->data_dma_ch) |= DCSR_RUN;
454}
455
456static void pxa3xx_nand_data_dma_irq(int channel, void *data)
457{
458 struct pxa3xx_nand_info *info = data;
459 uint32_t dcsr;
460
461 dcsr = DCSR(channel);
462 DCSR(channel) = dcsr;
463
464 if (dcsr & DCSR_BUSERR) {
465 info->retcode = ERR_DMABUSERR;
eric miaofe69af02008-02-14 15:48:23 +0800466 }
467
Lei Wenf8155a42011-02-28 10:32:11 +0800468 info->state = STATE_DMA_DONE;
469 enable_int(info, NDCR_INT_MASK);
470 nand_writel(info, NDSR, NDSR_WRDREQ | NDSR_RDDREQ);
eric miaofe69af02008-02-14 15:48:23 +0800471}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300472#else
473static void start_data_dma(struct pxa3xx_nand_info *info)
474{}
475#endif
eric miaofe69af02008-02-14 15:48:23 +0800476
477static irqreturn_t pxa3xx_nand_irq(int irq, void *devid)
478{
479 struct pxa3xx_nand_info *info = devid;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300480 unsigned int status, is_completed = 0, is_ready = 0;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700481 unsigned int ready, cmd_done;
482
483 if (info->cs == 0) {
484 ready = NDSR_FLASH_RDY;
485 cmd_done = NDSR_CS0_CMDD;
486 } else {
487 ready = NDSR_RDY;
488 cmd_done = NDSR_CS1_CMDD;
489 }
eric miaofe69af02008-02-14 15:48:23 +0800490
491 status = nand_readl(info, NDSR);
492
Lei Wenf8155a42011-02-28 10:32:11 +0800493 if (status & NDSR_DBERR)
494 info->retcode = ERR_DBERR;
495 if (status & NDSR_SBERR)
496 info->retcode = ERR_SBERR;
497 if (status & (NDSR_RDDREQ | NDSR_WRDREQ)) {
498 /* whether use dma to transfer data */
eric miaofe69af02008-02-14 15:48:23 +0800499 if (info->use_dma) {
Lei Wenf8155a42011-02-28 10:32:11 +0800500 disable_int(info, NDCR_INT_MASK);
501 info->state = (status & NDSR_RDDREQ) ?
502 STATE_DMA_READING : STATE_DMA_WRITING;
503 start_data_dma(info);
504 goto NORMAL_IRQ_EXIT;
eric miaofe69af02008-02-14 15:48:23 +0800505 } else {
Lei Wenf8155a42011-02-28 10:32:11 +0800506 info->state = (status & NDSR_RDDREQ) ?
507 STATE_PIO_READING : STATE_PIO_WRITING;
508 handle_data_pio(info);
eric miaofe69af02008-02-14 15:48:23 +0800509 }
Lei Wenf8155a42011-02-28 10:32:11 +0800510 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700511 if (status & cmd_done) {
Lei Wenf8155a42011-02-28 10:32:11 +0800512 info->state = STATE_CMD_DONE;
513 is_completed = 1;
514 }
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700515 if (status & ready) {
eric miaofe69af02008-02-14 15:48:23 +0800516 info->state = STATE_READY;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300517 is_ready = 1;
Lei Wen401e67e2011-02-28 10:32:14 +0800518 }
Lei Wenf8155a42011-02-28 10:32:11 +0800519
520 if (status & NDSR_WRCMDREQ) {
521 nand_writel(info, NDSR, NDSR_WRCMDREQ);
522 status &= ~NDSR_WRCMDREQ;
523 info->state = STATE_CMD_HANDLE;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300524
525 /*
526 * Command buffer registers NDCB{0-2} (and optionally NDCB3)
527 * must be loaded by writing directly either 12 or 16
528 * bytes directly to NDCB0, four bytes at a time.
529 *
530 * Direct write access to NDCB1, NDCB2 and NDCB3 is ignored
531 * but each NDCBx register can be read.
532 */
Lei Wenf8155a42011-02-28 10:32:11 +0800533 nand_writel(info, NDCB0, info->ndcb0);
534 nand_writel(info, NDCB0, info->ndcb1);
535 nand_writel(info, NDCB0, info->ndcb2);
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300536
537 /* NDCB3 register is available in NFCv2 (Armada 370/XP SoC) */
538 if (info->variant == PXA3XX_NAND_VARIANT_ARMADA370)
539 nand_writel(info, NDCB0, info->ndcb3);
eric miaofe69af02008-02-14 15:48:23 +0800540 }
Lei Wenf8155a42011-02-28 10:32:11 +0800541
542 /* clear NDSR to let the controller exit the IRQ */
eric miaofe69af02008-02-14 15:48:23 +0800543 nand_writel(info, NDSR, status);
Lei Wenf8155a42011-02-28 10:32:11 +0800544 if (is_completed)
545 complete(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300546 if (is_ready)
547 complete(&info->dev_ready);
Lei Wenf8155a42011-02-28 10:32:11 +0800548NORMAL_IRQ_EXIT:
eric miaofe69af02008-02-14 15:48:23 +0800549 return IRQ_HANDLED;
550}
551
eric miaofe69af02008-02-14 15:48:23 +0800552static inline int is_buf_blank(uint8_t *buf, size_t len)
553{
554 for (; len > 0; len--)
555 if (*buf++ != 0xff)
556 return 0;
557 return 1;
558}
559
Lei Wen4eb2da82011-02-28 10:32:13 +0800560static int prepare_command_pool(struct pxa3xx_nand_info *info, int command,
561 uint16_t column, int page_addr)
562{
Lei Wend4568822011-07-14 20:44:32 -0700563 int addr_cycle, exec_cmd;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700564 struct pxa3xx_nand_host *host;
565 struct mtd_info *mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800566
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700567 host = info->host[info->cs];
568 mtd = host->mtd;
Lei Wen4eb2da82011-02-28 10:32:13 +0800569 addr_cycle = 0;
570 exec_cmd = 1;
571
572 /* reset data and oob column point to handle data */
Lei Wen401e67e2011-02-28 10:32:14 +0800573 info->buf_start = 0;
574 info->buf_count = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800575 info->oob_size = 0;
576 info->use_ecc = 0;
Ezequiel Garcia5bb653e2013-08-12 14:14:49 -0300577 info->use_spare = 1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800578 info->retcode = ERR_NONE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700579 if (info->cs != 0)
580 info->ndcb0 = NDCB0_CSEL;
581 else
582 info->ndcb0 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800583
584 switch (command) {
585 case NAND_CMD_READ0:
586 case NAND_CMD_PAGEPROG:
587 info->use_ecc = 1;
588 case NAND_CMD_READOOB:
589 pxa3xx_set_datasize(info);
590 break;
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300591 case NAND_CMD_PARAM:
592 info->use_spare = 0;
593 break;
Lei Wen4eb2da82011-02-28 10:32:13 +0800594 case NAND_CMD_SEQIN:
595 exec_cmd = 0;
596 break;
597 default:
598 info->ndcb1 = 0;
599 info->ndcb2 = 0;
Ezequiel Garcia3a1a3442013-08-12 14:14:50 -0300600 info->ndcb3 = 0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800601 break;
602 }
603
Lei Wend4568822011-07-14 20:44:32 -0700604 addr_cycle = NDCB0_ADDR_CYC(host->row_addr_cycles
605 + host->col_addr_cycles);
Lei Wen4eb2da82011-02-28 10:32:13 +0800606
607 switch (command) {
608 case NAND_CMD_READOOB:
609 case NAND_CMD_READ0:
Ezequiel Garciaec821352013-08-12 14:14:54 -0300610 info->buf_start = column;
611 info->ndcb0 |= NDCB0_CMD_TYPE(0)
612 | addr_cycle
613 | NAND_CMD_READ0;
Lei Wen4eb2da82011-02-28 10:32:13 +0800614
Ezequiel Garciaec821352013-08-12 14:14:54 -0300615 if (command == NAND_CMD_READOOB)
616 info->buf_start += mtd->writesize;
617
618 /* Second command setting for large pages */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300619 if (mtd->writesize >= PAGE_CHUNK_SIZE)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300620 info->ndcb0 |= NDCB0_DBC | (NAND_CMD_READSTART << 8);
Lei Wen4eb2da82011-02-28 10:32:13 +0800621
622 case NAND_CMD_SEQIN:
623 /* small page addr setting */
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -0300624 if (unlikely(mtd->writesize < PAGE_CHUNK_SIZE)) {
Lei Wen4eb2da82011-02-28 10:32:13 +0800625 info->ndcb1 = ((page_addr & 0xFFFFFF) << 8)
626 | (column & 0xFF);
627
628 info->ndcb2 = 0;
629 } else {
630 info->ndcb1 = ((page_addr & 0xFFFF) << 16)
631 | (column & 0xFFFF);
632
633 if (page_addr & 0xFF0000)
634 info->ndcb2 = (page_addr & 0xFF0000) >> 16;
635 else
636 info->ndcb2 = 0;
637 }
638
639 info->buf_count = mtd->writesize + mtd->oobsize;
640 memset(info->data_buff, 0xFF, info->buf_count);
641
642 break;
643
644 case NAND_CMD_PAGEPROG:
645 if (is_buf_blank(info->data_buff,
646 (mtd->writesize + mtd->oobsize))) {
647 exec_cmd = 0;
648 break;
649 }
650
Lei Wen4eb2da82011-02-28 10:32:13 +0800651 info->ndcb0 |= NDCB0_CMD_TYPE(0x1)
652 | NDCB0_AUTO_RS
653 | NDCB0_ST_ROW_EN
654 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300655 | (NAND_CMD_PAGEPROG << 8)
656 | NAND_CMD_SEQIN
Lei Wen4eb2da82011-02-28 10:32:13 +0800657 | addr_cycle;
658 break;
659
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300660 case NAND_CMD_PARAM:
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300661 info->buf_count = 256;
662 info->ndcb0 |= NDCB0_CMD_TYPE(0)
663 | NDCB0_ADDR_CYC(1)
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300664 | NDCB0_LEN_OVRD
Ezequiel Garciaec821352013-08-12 14:14:54 -0300665 | command;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300666 info->ndcb1 = (column & 0xFF);
Ezequiel Garcia41a63432013-08-12 14:14:51 -0300667 info->ndcb3 = 256;
Ezequiel Garciace0268f2013-05-14 08:15:25 -0300668 info->data_size = 256;
669 break;
670
Lei Wen4eb2da82011-02-28 10:32:13 +0800671 case NAND_CMD_READID:
Lei Wend4568822011-07-14 20:44:32 -0700672 info->buf_count = host->read_id_bytes;
Lei Wen4eb2da82011-02-28 10:32:13 +0800673 info->ndcb0 |= NDCB0_CMD_TYPE(3)
674 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300675 | command;
Ezequiel Garciad14231f2013-05-14 08:15:24 -0300676 info->ndcb1 = (column & 0xFF);
Lei Wen4eb2da82011-02-28 10:32:13 +0800677
678 info->data_size = 8;
679 break;
680 case NAND_CMD_STATUS:
Lei Wen4eb2da82011-02-28 10:32:13 +0800681 info->buf_count = 1;
682 info->ndcb0 |= NDCB0_CMD_TYPE(4)
683 | NDCB0_ADDR_CYC(1)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300684 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800685
686 info->data_size = 8;
687 break;
688
689 case NAND_CMD_ERASE1:
Lei Wen4eb2da82011-02-28 10:32:13 +0800690 info->ndcb0 |= NDCB0_CMD_TYPE(2)
691 | NDCB0_AUTO_RS
692 | NDCB0_ADDR_CYC(3)
693 | NDCB0_DBC
Ezequiel Garciaec821352013-08-12 14:14:54 -0300694 | (NAND_CMD_ERASE2 << 8)
695 | NAND_CMD_ERASE1;
Lei Wen4eb2da82011-02-28 10:32:13 +0800696 info->ndcb1 = page_addr;
697 info->ndcb2 = 0;
698
699 break;
700 case NAND_CMD_RESET:
Lei Wen4eb2da82011-02-28 10:32:13 +0800701 info->ndcb0 |= NDCB0_CMD_TYPE(5)
Ezequiel Garciaec821352013-08-12 14:14:54 -0300702 | command;
Lei Wen4eb2da82011-02-28 10:32:13 +0800703
704 break;
705
706 case NAND_CMD_ERASE2:
707 exec_cmd = 0;
708 break;
709
710 default:
711 exec_cmd = 0;
Lei Wenda675b42011-07-14 20:44:31 -0700712 dev_err(&info->pdev->dev, "non-supported command %x\n",
713 command);
Lei Wen4eb2da82011-02-28 10:32:13 +0800714 break;
715 }
716
717 return exec_cmd;
718}
719
eric miaofe69af02008-02-14 15:48:23 +0800720static void pxa3xx_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
David Woodhousea1c06ee2008-04-22 20:39:43 +0100721 int column, int page_addr)
eric miaofe69af02008-02-14 15:48:23 +0800722{
Lei Wend4568822011-07-14 20:44:32 -0700723 struct pxa3xx_nand_host *host = mtd->priv;
724 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen4eb2da82011-02-28 10:32:13 +0800725 int ret, exec_cmd;
eric miaofe69af02008-02-14 15:48:23 +0800726
Lei Wen4eb2da82011-02-28 10:32:13 +0800727 /*
728 * if this is a x16 device ,then convert the input
729 * "byte" address into a "word" address appropriate
730 * for indexing a word-oriented device
731 */
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300732 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wen4eb2da82011-02-28 10:32:13 +0800733 column /= 2;
eric miaofe69af02008-02-14 15:48:23 +0800734
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700735 /*
736 * There may be different NAND chip hooked to
737 * different chip select, so check whether
738 * chip select has been changed, if yes, reset the timing
739 */
740 if (info->cs != host->cs) {
741 info->cs = host->cs;
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300742 nand_writel(info, NDTR0CS0, info->ndtr0cs0);
743 nand_writel(info, NDTR1CS0, info->ndtr1cs0);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700744 }
745
Lei Wend4568822011-07-14 20:44:32 -0700746 info->state = STATE_PREPARED;
Lei Wen4eb2da82011-02-28 10:32:13 +0800747 exec_cmd = prepare_command_pool(info, command, column, page_addr);
Lei Wenf8155a42011-02-28 10:32:11 +0800748 if (exec_cmd) {
749 init_completion(&info->cmd_complete);
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300750 init_completion(&info->dev_ready);
751 info->need_wait = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800752 pxa3xx_nand_start(info);
753
754 ret = wait_for_completion_timeout(&info->cmd_complete,
755 CHIP_DELAY_TIMEOUT);
756 if (!ret) {
Lei Wenda675b42011-07-14 20:44:31 -0700757 dev_err(&info->pdev->dev, "Wait time out!!!\n");
Lei Wenf8155a42011-02-28 10:32:11 +0800758 /* Stop State Machine for next command cycle */
759 pxa3xx_nand_stop(info);
760 }
eric miaofe69af02008-02-14 15:48:23 +0800761 }
Lei Wend4568822011-07-14 20:44:32 -0700762 info->state = STATE_IDLE;
eric miaofe69af02008-02-14 15:48:23 +0800763}
764
Josh Wufdbad98d2012-06-25 18:07:45 +0800765static int pxa3xx_nand_write_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700766 struct nand_chip *chip, const uint8_t *buf, int oob_required)
Lei Wenf8155a42011-02-28 10:32:11 +0800767{
768 chip->write_buf(mtd, buf, mtd->writesize);
769 chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
Josh Wufdbad98d2012-06-25 18:07:45 +0800770
771 return 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800772}
773
774static int pxa3xx_nand_read_page_hwecc(struct mtd_info *mtd,
Brian Norris1fbb9382012-05-02 10:14:55 -0700775 struct nand_chip *chip, uint8_t *buf, int oob_required,
776 int page)
Lei Wenf8155a42011-02-28 10:32:11 +0800777{
Lei Wend4568822011-07-14 20:44:32 -0700778 struct pxa3xx_nand_host *host = mtd->priv;
779 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300780 int max_bitflips = 0;
Lei Wenf8155a42011-02-28 10:32:11 +0800781
782 chip->read_buf(mtd, buf, mtd->writesize);
783 chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
784
785 if (info->retcode == ERR_SBERR) {
786 switch (info->use_ecc) {
787 case 1:
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300788 max_bitflips = 1;
Lei Wenf8155a42011-02-28 10:32:11 +0800789 mtd->ecc_stats.corrected++;
790 break;
791 case 0:
792 default:
793 break;
794 }
795 } else if (info->retcode == ERR_DBERR) {
796 /*
797 * for blank page (all 0xff), HW will calculate its ECC as
798 * 0, which is different from the ECC information within
799 * OOB, ignore such double bit errors
800 */
801 if (is_buf_blank(buf, mtd->writesize))
Daniel Mack543e32d2011-06-07 03:01:07 -0700802 info->retcode = ERR_NONE;
803 else
Lei Wenf8155a42011-02-28 10:32:11 +0800804 mtd->ecc_stats.failed++;
805 }
806
Ezequiel Garcia4e86fd22013-11-07 12:17:13 -0300807 return max_bitflips;
Lei Wenf8155a42011-02-28 10:32:11 +0800808}
809
eric miaofe69af02008-02-14 15:48:23 +0800810static uint8_t pxa3xx_nand_read_byte(struct mtd_info *mtd)
811{
Lei Wend4568822011-07-14 20:44:32 -0700812 struct pxa3xx_nand_host *host = mtd->priv;
813 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800814 char retval = 0xFF;
815
816 if (info->buf_start < info->buf_count)
817 /* Has just send a new command? */
818 retval = info->data_buff[info->buf_start++];
819
820 return retval;
821}
822
823static u16 pxa3xx_nand_read_word(struct mtd_info *mtd)
824{
Lei Wend4568822011-07-14 20:44:32 -0700825 struct pxa3xx_nand_host *host = mtd->priv;
826 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800827 u16 retval = 0xFFFF;
828
829 if (!(info->buf_start & 0x01) && info->buf_start < info->buf_count) {
830 retval = *((u16 *)(info->data_buff+info->buf_start));
831 info->buf_start += 2;
832 }
833 return retval;
834}
835
836static void pxa3xx_nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
837{
Lei Wend4568822011-07-14 20:44:32 -0700838 struct pxa3xx_nand_host *host = mtd->priv;
839 struct pxa3xx_nand_info *info = host->info_data;
eric miaofe69af02008-02-14 15:48:23 +0800840 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
841
842 memcpy(buf, info->data_buff + info->buf_start, real_len);
843 info->buf_start += real_len;
844}
845
846static void pxa3xx_nand_write_buf(struct mtd_info *mtd,
847 const uint8_t *buf, int len)
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 int real_len = min_t(size_t, len, info->buf_count - info->buf_start);
852
853 memcpy(info->data_buff + info->buf_start, buf, real_len);
854 info->buf_start += real_len;
855}
856
eric miaofe69af02008-02-14 15:48:23 +0800857static void pxa3xx_nand_select_chip(struct mtd_info *mtd, int chip)
858{
859 return;
860}
861
862static int pxa3xx_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
863{
Lei Wend4568822011-07-14 20:44:32 -0700864 struct pxa3xx_nand_host *host = mtd->priv;
865 struct pxa3xx_nand_info *info = host->info_data;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300866 int ret;
867
868 if (info->need_wait) {
869 ret = wait_for_completion_timeout(&info->dev_ready,
870 CHIP_DELAY_TIMEOUT);
871 info->need_wait = 0;
872 if (!ret) {
873 dev_err(&info->pdev->dev, "Ready time out!!!\n");
874 return NAND_STATUS_FAIL;
875 }
876 }
eric miaofe69af02008-02-14 15:48:23 +0800877
878 /* pxa3xx_nand_send_command has waited for command complete */
879 if (this->state == FL_WRITING || this->state == FL_ERASING) {
880 if (info->retcode == ERR_NONE)
881 return 0;
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300882 else
883 return NAND_STATUS_FAIL;
eric miaofe69af02008-02-14 15:48:23 +0800884 }
885
Ezequiel Garcia55d9fd62013-11-14 18:25:26 -0300886 return NAND_STATUS_READY;
eric miaofe69af02008-02-14 15:48:23 +0800887}
888
eric miaofe69af02008-02-14 15:48:23 +0800889static int pxa3xx_nand_config_flash(struct pxa3xx_nand_info *info,
Enrico Scholzc8c17c82008-08-29 12:59:51 +0200890 const struct pxa3xx_nand_flash *f)
eric miaofe69af02008-02-14 15:48:23 +0800891{
892 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +0900893 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700894 struct pxa3xx_nand_host *host = info->host[info->cs];
Lei Wenf8155a42011-02-28 10:32:11 +0800895 uint32_t ndcr = 0x0; /* enable all interrupts */
eric miaofe69af02008-02-14 15:48:23 +0800896
Lei Wenda675b42011-07-14 20:44:31 -0700897 if (f->page_size != 2048 && f->page_size != 512) {
898 dev_err(&pdev->dev, "Current only support 2048 and 512 size\n");
eric miaofe69af02008-02-14 15:48:23 +0800899 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700900 }
eric miaofe69af02008-02-14 15:48:23 +0800901
Lei Wenda675b42011-07-14 20:44:31 -0700902 if (f->flash_width != 16 && f->flash_width != 8) {
903 dev_err(&pdev->dev, "Only support 8bit and 16 bit!\n");
eric miaofe69af02008-02-14 15:48:23 +0800904 return -EINVAL;
Lei Wenda675b42011-07-14 20:44:31 -0700905 }
eric miaofe69af02008-02-14 15:48:23 +0800906
907 /* calculate flash information */
Lei Wend4568822011-07-14 20:44:32 -0700908 host->read_id_bytes = (f->page_size == 2048) ? 4 : 2;
eric miaofe69af02008-02-14 15:48:23 +0800909
910 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -0700911 host->col_addr_cycles = (f->page_size == 2048) ? 2 : 1;
eric miaofe69af02008-02-14 15:48:23 +0800912
913 if (f->num_blocks * f->page_per_block > 65536)
Lei Wend4568822011-07-14 20:44:32 -0700914 host->row_addr_cycles = 3;
eric miaofe69af02008-02-14 15:48:23 +0800915 else
Lei Wend4568822011-07-14 20:44:32 -0700916 host->row_addr_cycles = 2;
eric miaofe69af02008-02-14 15:48:23 +0800917
918 ndcr |= (pdata->enable_arbiter) ? NDCR_ND_ARB_EN : 0;
Lei Wend4568822011-07-14 20:44:32 -0700919 ndcr |= (host->col_addr_cycles == 2) ? NDCR_RA_START : 0;
eric miaofe69af02008-02-14 15:48:23 +0800920 ndcr |= (f->page_per_block == 64) ? NDCR_PG_PER_BLK : 0;
921 ndcr |= (f->page_size == 2048) ? NDCR_PAGE_SZ : 0;
922 ndcr |= (f->flash_width == 16) ? NDCR_DWIDTH_M : 0;
923 ndcr |= (f->dfc_width == 16) ? NDCR_DWIDTH_C : 0;
924
Lei Wend4568822011-07-14 20:44:32 -0700925 ndcr |= NDCR_RD_ID_CNT(host->read_id_bytes);
eric miaofe69af02008-02-14 15:48:23 +0800926 ndcr |= NDCR_SPARE_EN; /* enable spare by default */
927
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300928 info->reg_ndcr = ndcr;
eric miaofe69af02008-02-14 15:48:23 +0800929
Lei Wend4568822011-07-14 20:44:32 -0700930 pxa3xx_nand_set_timing(host, f->timing);
eric miaofe69af02008-02-14 15:48:23 +0800931 return 0;
932}
933
Mike Rapoportf2710492009-02-17 13:54:47 +0200934static int pxa3xx_nand_detect_config(struct pxa3xx_nand_info *info)
935{
Lei Wenf3c8cfc2011-07-14 20:44:33 -0700936 /*
937 * We set 0 by hard coding here, for we don't support keep_config
938 * when there is more than one chip attached to the controller
939 */
940 struct pxa3xx_nand_host *host = info->host[0];
Mike Rapoportf2710492009-02-17 13:54:47 +0200941 uint32_t ndcr = nand_readl(info, NDCR);
Mike Rapoportf2710492009-02-17 13:54:47 +0200942
Lei Wend4568822011-07-14 20:44:32 -0700943 if (ndcr & NDCR_PAGE_SZ) {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300944 /* Controller's FIFO size */
945 info->fifo_size = 2048;
Lei Wend4568822011-07-14 20:44:32 -0700946 host->read_id_bytes = 4;
947 } else {
Ezequiel Garcia2128b082013-11-07 12:17:16 -0300948 info->fifo_size = 512;
Lei Wend4568822011-07-14 20:44:32 -0700949 host->read_id_bytes = 2;
950 }
951
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -0300952 info->reg_ndcr = ndcr & ~NDCR_INT_MASK;
953 info->ndtr0cs0 = nand_readl(info, NDTR0CS0);
954 info->ndtr1cs0 = nand_readl(info, NDTR1CS0);
Mike Rapoportf2710492009-02-17 13:54:47 +0200955 return 0;
956}
957
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -0300958#ifdef ARCH_HAS_DMA
eric miaofe69af02008-02-14 15:48:23 +0800959static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
960{
961 struct platform_device *pdev = info->pdev;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300962 int data_desc_offset = info->buf_size - sizeof(struct pxa_dma_desc);
eric miaofe69af02008-02-14 15:48:23 +0800963
964 if (use_dma == 0) {
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300965 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
eric miaofe69af02008-02-14 15:48:23 +0800966 if (info->data_buff == NULL)
967 return -ENOMEM;
968 return 0;
969 }
970
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300971 info->data_buff = dma_alloc_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800972 &info->data_buff_phys, GFP_KERNEL);
973 if (info->data_buff == NULL) {
974 dev_err(&pdev->dev, "failed to allocate dma buffer\n");
975 return -ENOMEM;
976 }
977
eric miaofe69af02008-02-14 15:48:23 +0800978 info->data_desc = (void *)info->data_buff + data_desc_offset;
979 info->data_desc_addr = info->data_buff_phys + data_desc_offset;
980
981 info->data_dma_ch = pxa_request_dma("nand-data", DMA_PRIO_LOW,
982 pxa3xx_nand_data_dma_irq, info);
983 if (info->data_dma_ch < 0) {
984 dev_err(&pdev->dev, "failed to request data dma\n");
Ezequiel Garcia62e8b852013-10-04 15:30:38 -0300985 dma_free_coherent(&pdev->dev, info->buf_size,
eric miaofe69af02008-02-14 15:48:23 +0800986 info->data_buff, info->data_buff_phys);
987 return info->data_dma_ch;
988 }
989
Ezequiel Garcia95b26562013-10-04 15:30:37 -0300990 /*
991 * Now that DMA buffers are allocated we turn on
992 * DMA proper for I/O operations.
993 */
994 info->use_dma = 1;
eric miaofe69af02008-02-14 15:48:23 +0800995 return 0;
996}
997
Ezequiel Garcia498b6142013-04-17 13:38:14 -0300998static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
999{
1000 struct platform_device *pdev = info->pdev;
Ezequiel Garcia15b540c2013-12-10 09:57:15 -03001001 if (info->use_dma) {
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001002 pxa_free_dma(info->data_dma_ch);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001003 dma_free_coherent(&pdev->dev, info->buf_size,
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001004 info->data_buff, info->data_buff_phys);
1005 } else {
1006 kfree(info->data_buff);
1007 }
1008}
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001009#else
1010static int pxa3xx_nand_init_buff(struct pxa3xx_nand_info *info)
1011{
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001012 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001013 if (info->data_buff == NULL)
1014 return -ENOMEM;
1015 return 0;
1016}
1017
1018static void pxa3xx_nand_free_buff(struct pxa3xx_nand_info *info)
1019{
1020 kfree(info->data_buff);
1021}
1022#endif
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001023
Lei Wen401e67e2011-02-28 10:32:14 +08001024static int pxa3xx_nand_sensing(struct pxa3xx_nand_info *info)
eric miaofe69af02008-02-14 15:48:23 +08001025{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001026 struct mtd_info *mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001027 struct nand_chip *chip;
Lei Wend4568822011-07-14 20:44:32 -07001028 int ret;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001029
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001030 mtd = info->host[info->cs]->mtd;
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001031 chip = mtd->priv;
1032
Lei Wen401e67e2011-02-28 10:32:14 +08001033 /* use the common timing to make a try */
Lei Wend4568822011-07-14 20:44:32 -07001034 ret = pxa3xx_nand_config_flash(info, &builtin_flash_types[0]);
1035 if (ret)
1036 return ret;
1037
Ezequiel Garcia2d79ab12013-11-07 12:17:15 -03001038 chip->cmdfunc(mtd, NAND_CMD_RESET, 0, 0);
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001039 ret = chip->waitfunc(mtd, chip);
1040 if (ret & NAND_STATUS_FAIL)
1041 return -ENODEV;
Lei Wend4568822011-07-14 20:44:32 -07001042
Ezequiel Garcia56704d82013-11-14 18:25:27 -03001043 return 0;
Lei Wen401e67e2011-02-28 10:32:14 +08001044}
eric miaofe69af02008-02-14 15:48:23 +08001045
Lei Wen401e67e2011-02-28 10:32:14 +08001046static int pxa3xx_nand_scan(struct mtd_info *mtd)
1047{
Lei Wend4568822011-07-14 20:44:32 -07001048 struct pxa3xx_nand_host *host = mtd->priv;
1049 struct pxa3xx_nand_info *info = host->info_data;
Lei Wen401e67e2011-02-28 10:32:14 +08001050 struct platform_device *pdev = info->pdev;
Jingoo Han453810b2013-07-30 17:18:33 +09001051 struct pxa3xx_nand_platform_data *pdata = dev_get_platdata(&pdev->dev);
Lei Wen0fab0282011-06-07 03:01:06 -07001052 struct nand_flash_dev pxa3xx_flash_ids[2], *def = NULL;
Lei Wen401e67e2011-02-28 10:32:14 +08001053 const struct pxa3xx_nand_flash *f = NULL;
1054 struct nand_chip *chip = mtd->priv;
1055 uint32_t id = -1;
Lei Wen4332c112011-03-03 11:27:01 +08001056 uint64_t chipsize;
Lei Wen401e67e2011-02-28 10:32:14 +08001057 int i, ret, num;
1058
1059 if (pdata->keep_config && !pxa3xx_nand_detect_config(info))
Lei Wen4332c112011-03-03 11:27:01 +08001060 goto KEEP_CONFIG;
Lei Wen401e67e2011-02-28 10:32:14 +08001061
1062 ret = pxa3xx_nand_sensing(info);
Lei Wend4568822011-07-14 20:44:32 -07001063 if (ret) {
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001064 dev_info(&info->pdev->dev, "There is no chip on cs %d!\n",
1065 info->cs);
Lei Wen401e67e2011-02-28 10:32:14 +08001066
Lei Wend4568822011-07-14 20:44:32 -07001067 return ret;
Lei Wen401e67e2011-02-28 10:32:14 +08001068 }
1069
1070 chip->cmdfunc(mtd, NAND_CMD_READID, 0, 0);
1071 id = *((uint16_t *)(info->data_buff));
1072 if (id != 0)
Lei Wenda675b42011-07-14 20:44:31 -07001073 dev_info(&info->pdev->dev, "Detect a flash id %x\n", id);
Lei Wen401e67e2011-02-28 10:32:14 +08001074 else {
Lei Wenda675b42011-07-14 20:44:31 -07001075 dev_warn(&info->pdev->dev,
1076 "Read out ID 0, potential timing set wrong!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001077
1078 return -EINVAL;
1079 }
1080
1081 num = ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1;
1082 for (i = 0; i < num; i++) {
1083 if (i < pdata->num_flash)
1084 f = pdata->flash + i;
1085 else
1086 f = &builtin_flash_types[i - pdata->num_flash + 1];
1087
1088 /* find the chip in default list */
Lei Wen4332c112011-03-03 11:27:01 +08001089 if (f->chip_id == id)
Lei Wen401e67e2011-02-28 10:32:14 +08001090 break;
Lei Wen401e67e2011-02-28 10:32:14 +08001091 }
1092
Lei Wen4332c112011-03-03 11:27:01 +08001093 if (i >= (ARRAY_SIZE(builtin_flash_types) + pdata->num_flash - 1)) {
Lei Wenda675b42011-07-14 20:44:31 -07001094 dev_err(&info->pdev->dev, "ERROR!! flash not defined!!!\n");
Lei Wen401e67e2011-02-28 10:32:14 +08001095
1096 return -EINVAL;
1097 }
1098
Lei Wend4568822011-07-14 20:44:32 -07001099 ret = pxa3xx_nand_config_flash(info, f);
1100 if (ret) {
1101 dev_err(&info->pdev->dev, "ERROR! Configure failed\n");
1102 return ret;
1103 }
1104
Lei Wen4332c112011-03-03 11:27:01 +08001105 pxa3xx_flash_ids[0].name = f->name;
Artem Bityutskiy68aa352de2013-03-04 16:05:00 +02001106 pxa3xx_flash_ids[0].dev_id = (f->chip_id >> 8) & 0xffff;
Lei Wen4332c112011-03-03 11:27:01 +08001107 pxa3xx_flash_ids[0].pagesize = f->page_size;
1108 chipsize = (uint64_t)f->num_blocks * f->page_per_block * f->page_size;
1109 pxa3xx_flash_ids[0].chipsize = chipsize >> 20;
1110 pxa3xx_flash_ids[0].erasesize = f->page_size * f->page_per_block;
1111 if (f->flash_width == 16)
1112 pxa3xx_flash_ids[0].options = NAND_BUSWIDTH_16;
Lei Wen0fab0282011-06-07 03:01:06 -07001113 pxa3xx_flash_ids[1].name = NULL;
1114 def = pxa3xx_flash_ids;
Lei Wen4332c112011-03-03 11:27:01 +08001115KEEP_CONFIG:
Lei Wend4568822011-07-14 20:44:32 -07001116 chip->ecc.mode = NAND_ECC_HW;
Ezequiel Garcia0a3f3a12013-11-07 12:17:17 -03001117 chip->ecc.size = info->fifo_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -07001118 chip->ecc.strength = 1;
Lei Wend4568822011-07-14 20:44:32 -07001119
Ezequiel Garcia48cf7ef2013-08-12 14:14:55 -03001120 if (info->reg_ndcr & NDCR_DWIDTH_M)
Lei Wend4568822011-07-14 20:44:32 -07001121 chip->options |= NAND_BUSWIDTH_16;
1122
Lei Wen0fab0282011-06-07 03:01:06 -07001123 if (nand_scan_ident(mtd, 1, def))
Lei Wen4332c112011-03-03 11:27:01 +08001124 return -ENODEV;
1125 /* calculate addressing information */
Lei Wend4568822011-07-14 20:44:32 -07001126 if (mtd->writesize >= 2048)
1127 host->col_addr_cycles = 2;
1128 else
1129 host->col_addr_cycles = 1;
1130
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001131 /* release the initial buffer */
1132 kfree(info->data_buff);
1133
1134 /* allocate the real data + oob buffer */
1135 info->buf_size = mtd->writesize + mtd->oobsize;
1136 ret = pxa3xx_nand_init_buff(info);
1137 if (ret)
1138 return ret;
Lei Wen4332c112011-03-03 11:27:01 +08001139 info->oob_buff = info->data_buff + mtd->writesize;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001140
Lei Wen4332c112011-03-03 11:27:01 +08001141 if ((mtd->size >> chip->page_shift) > 65536)
Lei Wend4568822011-07-14 20:44:32 -07001142 host->row_addr_cycles = 3;
Lei Wen4332c112011-03-03 11:27:01 +08001143 else
Lei Wend4568822011-07-14 20:44:32 -07001144 host->row_addr_cycles = 2;
Lei Wen401e67e2011-02-28 10:32:14 +08001145 return nand_scan_tail(mtd);
eric miaofe69af02008-02-14 15:48:23 +08001146}
1147
Lei Wend4568822011-07-14 20:44:32 -07001148static int alloc_nand_resource(struct platform_device *pdev)
eric miaofe69af02008-02-14 15:48:23 +08001149{
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001150 struct pxa3xx_nand_platform_data *pdata;
eric miaofe69af02008-02-14 15:48:23 +08001151 struct pxa3xx_nand_info *info;
Lei Wend4568822011-07-14 20:44:32 -07001152 struct pxa3xx_nand_host *host;
Haojian Zhuang6e308f82012-08-20 13:40:31 +08001153 struct nand_chip *chip = NULL;
eric miaofe69af02008-02-14 15:48:23 +08001154 struct mtd_info *mtd;
1155 struct resource *r;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001156 int ret, irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001157
Jingoo Han453810b2013-07-30 17:18:33 +09001158 pdata = dev_get_platdata(&pdev->dev);
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001159 info = devm_kzalloc(&pdev->dev, sizeof(*info) + (sizeof(*mtd) +
1160 sizeof(*host)) * pdata->num_cs, GFP_KERNEL);
1161 if (!info)
Lei Wend4568822011-07-14 20:44:32 -07001162 return -ENOMEM;
eric miaofe69af02008-02-14 15:48:23 +08001163
eric miaofe69af02008-02-14 15:48:23 +08001164 info->pdev = pdev;
Ezequiel Garciac7e9c7e2013-11-07 12:17:14 -03001165 info->variant = pxa3xx_nand_get_variant(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001166 for (cs = 0; cs < pdata->num_cs; cs++) {
1167 mtd = (struct mtd_info *)((unsigned int)&info[1] +
1168 (sizeof(*mtd) + sizeof(*host)) * cs);
1169 chip = (struct nand_chip *)(&mtd[1]);
1170 host = (struct pxa3xx_nand_host *)chip;
1171 info->host[cs] = host;
1172 host->mtd = mtd;
1173 host->cs = cs;
1174 host->info_data = info;
1175 mtd->priv = host;
1176 mtd->owner = THIS_MODULE;
eric miaofe69af02008-02-14 15:48:23 +08001177
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001178 chip->ecc.read_page = pxa3xx_nand_read_page_hwecc;
1179 chip->ecc.write_page = pxa3xx_nand_write_page_hwecc;
1180 chip->controller = &info->controller;
1181 chip->waitfunc = pxa3xx_nand_waitfunc;
1182 chip->select_chip = pxa3xx_nand_select_chip;
1183 chip->cmdfunc = pxa3xx_nand_cmdfunc;
1184 chip->read_word = pxa3xx_nand_read_word;
1185 chip->read_byte = pxa3xx_nand_read_byte;
1186 chip->read_buf = pxa3xx_nand_read_buf;
1187 chip->write_buf = pxa3xx_nand_write_buf;
Ezequiel Garcia664c7f52013-11-07 12:17:12 -03001188 chip->options |= NAND_NO_SUBPAGE_WRITE;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001189 }
Lei Wen401e67e2011-02-28 10:32:14 +08001190
1191 spin_lock_init(&chip->controller->lock);
1192 init_waitqueue_head(&chip->controller->wq);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001193 info->clk = devm_clk_get(&pdev->dev, NULL);
eric miaofe69af02008-02-14 15:48:23 +08001194 if (IS_ERR(info->clk)) {
1195 dev_err(&pdev->dev, "failed to get nand clock\n");
Ezequiel Garcia4c073cd2013-04-17 13:38:09 -03001196 return PTR_ERR(info->clk);
eric miaofe69af02008-02-14 15:48:23 +08001197 }
Ezequiel Garcia1f8eaff2013-04-17 13:38:13 -03001198 ret = clk_prepare_enable(info->clk);
1199 if (ret < 0)
1200 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001201
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001202 if (use_dma) {
1203 /*
1204 * This is a dirty hack to make this driver work from
1205 * devicetree bindings. It can be removed once we have
1206 * a prober DMA controller framework for DT.
1207 */
1208 if (pdev->dev.of_node &&
1209 of_machine_is_compatible("marvell,pxa3xx")) {
1210 info->drcmr_dat = 97;
1211 info->drcmr_cmd = 99;
1212 } else {
1213 r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1214 if (r == NULL) {
1215 dev_err(&pdev->dev,
1216 "no resource defined for data DMA\n");
1217 ret = -ENXIO;
1218 goto fail_disable_clk;
1219 }
1220 info->drcmr_dat = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001221
Ezequiel Garcia6b45c1e2013-08-12 14:14:58 -03001222 r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1223 if (r == NULL) {
1224 dev_err(&pdev->dev,
1225 "no resource defined for cmd DMA\n");
1226 ret = -ENXIO;
1227 goto fail_disable_clk;
1228 }
1229 info->drcmr_cmd = r->start;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001230 }
eric miaofe69af02008-02-14 15:48:23 +08001231 }
eric miaofe69af02008-02-14 15:48:23 +08001232
1233 irq = platform_get_irq(pdev, 0);
1234 if (irq < 0) {
1235 dev_err(&pdev->dev, "no IRQ resource defined\n");
1236 ret = -ENXIO;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001237 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001238 }
1239
1240 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ezequiel Garcia0ddd8462013-04-17 13:38:10 -03001241 info->mmio_base = devm_ioremap_resource(&pdev->dev, r);
1242 if (IS_ERR(info->mmio_base)) {
1243 ret = PTR_ERR(info->mmio_base);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001244 goto fail_disable_clk;
eric miaofe69af02008-02-14 15:48:23 +08001245 }
Haojian Zhuang8638fac2009-09-10 14:11:44 +08001246 info->mmio_phys = r->start;
eric miaofe69af02008-02-14 15:48:23 +08001247
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001248 /* Allocate a buffer to allow flash detection */
1249 info->buf_size = INIT_BUFFER_SIZE;
1250 info->data_buff = kmalloc(info->buf_size, GFP_KERNEL);
1251 if (info->data_buff == NULL) {
1252 ret = -ENOMEM;
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001253 goto fail_disable_clk;
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001254 }
eric miaofe69af02008-02-14 15:48:23 +08001255
Haojian Zhuang346e1252009-09-10 14:27:23 +08001256 /* initialize all interrupts to be disabled */
1257 disable_int(info, NDSR_MASK);
1258
Michael Opdenackerb1eb2342013-10-13 08:21:32 +02001259 ret = request_irq(irq, pxa3xx_nand_irq, 0, pdev->name, info);
eric miaofe69af02008-02-14 15:48:23 +08001260 if (ret < 0) {
1261 dev_err(&pdev->dev, "failed to request IRQ\n");
1262 goto fail_free_buf;
1263 }
1264
Lei Wene353a202011-03-03 11:08:30 +08001265 platform_set_drvdata(pdev, info);
eric miaofe69af02008-02-14 15:48:23 +08001266
Lei Wend4568822011-07-14 20:44:32 -07001267 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001268
eric miaofe69af02008-02-14 15:48:23 +08001269fail_free_buf:
Lei Wen401e67e2011-02-28 10:32:14 +08001270 free_irq(irq, info);
Ezequiel Garcia62e8b852013-10-04 15:30:38 -03001271 kfree(info->data_buff);
Ezequiel Garcia9ca79442013-04-17 13:38:11 -03001272fail_disable_clk:
Ezequiel Garciafb320612013-04-17 13:38:12 -03001273 clk_disable_unprepare(info->clk);
Lei Wend4568822011-07-14 20:44:32 -07001274 return ret;
eric miaofe69af02008-02-14 15:48:23 +08001275}
1276
1277static int pxa3xx_nand_remove(struct platform_device *pdev)
1278{
Lei Wene353a202011-03-03 11:08:30 +08001279 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001280 struct pxa3xx_nand_platform_data *pdata;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001281 int irq, cs;
eric miaofe69af02008-02-14 15:48:23 +08001282
Lei Wend4568822011-07-14 20:44:32 -07001283 if (!info)
1284 return 0;
1285
Jingoo Han453810b2013-07-30 17:18:33 +09001286 pdata = dev_get_platdata(&pdev->dev);
eric miaofe69af02008-02-14 15:48:23 +08001287
Haojian Zhuangdbf59862009-09-10 14:22:55 +08001288 irq = platform_get_irq(pdev, 0);
1289 if (irq >= 0)
1290 free_irq(irq, info);
Ezequiel Garcia498b6142013-04-17 13:38:14 -03001291 pxa3xx_nand_free_buff(info);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001292
Ezequiel Garciafb320612013-04-17 13:38:12 -03001293 clk_disable_unprepare(info->clk);
Mike Rapoport82a72d12009-02-17 13:54:46 +02001294
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001295 for (cs = 0; cs < pdata->num_cs; cs++)
1296 nand_release(info->host[cs]->mtd);
eric miaofe69af02008-02-14 15:48:23 +08001297 return 0;
1298}
1299
Daniel Mack1e7ba632012-07-22 19:51:02 +02001300static int pxa3xx_nand_probe_dt(struct platform_device *pdev)
1301{
1302 struct pxa3xx_nand_platform_data *pdata;
1303 struct device_node *np = pdev->dev.of_node;
1304 const struct of_device_id *of_id =
1305 of_match_device(pxa3xx_nand_dt_ids, &pdev->dev);
1306
1307 if (!of_id)
1308 return 0;
1309
1310 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1311 if (!pdata)
1312 return -ENOMEM;
1313
1314 if (of_get_property(np, "marvell,nand-enable-arbiter", NULL))
1315 pdata->enable_arbiter = 1;
1316 if (of_get_property(np, "marvell,nand-keep-config", NULL))
1317 pdata->keep_config = 1;
1318 of_property_read_u32(np, "num-cs", &pdata->num_cs);
1319
1320 pdev->dev.platform_data = pdata;
1321
1322 return 0;
1323}
Daniel Mack1e7ba632012-07-22 19:51:02 +02001324
Lei Wene353a202011-03-03 11:08:30 +08001325static int pxa3xx_nand_probe(struct platform_device *pdev)
1326{
1327 struct pxa3xx_nand_platform_data *pdata;
Daniel Mack1e7ba632012-07-22 19:51:02 +02001328 struct mtd_part_parser_data ppdata = {};
Lei Wene353a202011-03-03 11:08:30 +08001329 struct pxa3xx_nand_info *info;
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001330 int ret, cs, probe_success;
Lei Wene353a202011-03-03 11:08:30 +08001331
Ezequiel Garciaf4db2e32013-08-12 14:14:56 -03001332#ifndef ARCH_HAS_DMA
1333 if (use_dma) {
1334 use_dma = 0;
1335 dev_warn(&pdev->dev,
1336 "This platform can't do DMA on this device\n");
1337 }
1338#endif
Daniel Mack1e7ba632012-07-22 19:51:02 +02001339 ret = pxa3xx_nand_probe_dt(pdev);
1340 if (ret)
1341 return ret;
1342
Jingoo Han453810b2013-07-30 17:18:33 +09001343 pdata = dev_get_platdata(&pdev->dev);
Lei Wene353a202011-03-03 11:08:30 +08001344 if (!pdata) {
1345 dev_err(&pdev->dev, "no platform data defined\n");
1346 return -ENODEV;
1347 }
1348
Lei Wend4568822011-07-14 20:44:32 -07001349 ret = alloc_nand_resource(pdev);
1350 if (ret) {
1351 dev_err(&pdev->dev, "alloc nand resource failed\n");
1352 return ret;
1353 }
Lei Wene353a202011-03-03 11:08:30 +08001354
Lei Wend4568822011-07-14 20:44:32 -07001355 info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001356 probe_success = 0;
1357 for (cs = 0; cs < pdata->num_cs; cs++) {
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001358 struct mtd_info *mtd = info->host[cs]->mtd;
Ezequiel Garciaf4555782013-08-12 14:14:53 -03001359
Ezequiel Garcia18a84e92013-10-19 18:19:25 -03001360 /*
1361 * The mtd name matches the one used in 'mtdparts' kernel
1362 * parameter. This name cannot be changed or otherwise
1363 * user's mtd partitions configuration would get broken.
1364 */
1365 mtd->name = "pxa3xx_nand-0";
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001366 info->cs = cs;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001367 ret = pxa3xx_nand_scan(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001368 if (ret) {
1369 dev_warn(&pdev->dev, "failed to scan nand at cs %d\n",
1370 cs);
1371 continue;
1372 }
1373
Daniel Mack1e7ba632012-07-22 19:51:02 +02001374 ppdata.of_node = pdev->dev.of_node;
Ezequiel Garciab7655bc2013-08-12 14:14:52 -03001375 ret = mtd_device_parse_register(mtd, NULL,
Daniel Mack1e7ba632012-07-22 19:51:02 +02001376 &ppdata, pdata->parts[cs],
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001377 pdata->nr_parts[cs]);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001378 if (!ret)
1379 probe_success = 1;
1380 }
1381
1382 if (!probe_success) {
Lei Wene353a202011-03-03 11:08:30 +08001383 pxa3xx_nand_remove(pdev);
1384 return -ENODEV;
1385 }
1386
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001387 return 0;
Lei Wene353a202011-03-03 11:08:30 +08001388}
1389
eric miaofe69af02008-02-14 15:48:23 +08001390#ifdef CONFIG_PM
1391static int pxa3xx_nand_suspend(struct platform_device *pdev, pm_message_t state)
1392{
Lei Wene353a202011-03-03 11:08:30 +08001393 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001394 struct pxa3xx_nand_platform_data *pdata;
1395 struct mtd_info *mtd;
1396 int cs;
eric miaofe69af02008-02-14 15:48:23 +08001397
Jingoo Han453810b2013-07-30 17:18:33 +09001398 pdata = dev_get_platdata(&pdev->dev);
Lei Wenf8155a42011-02-28 10:32:11 +08001399 if (info->state) {
eric miaofe69af02008-02-14 15:48:23 +08001400 dev_err(&pdev->dev, "driver busy, state = %d\n", info->state);
1401 return -EAGAIN;
1402 }
1403
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001404 for (cs = 0; cs < pdata->num_cs; cs++) {
1405 mtd = info->host[cs]->mtd;
Artem Bityutskiy3fe4bae2011-12-23 19:25:16 +02001406 mtd_suspend(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001407 }
1408
eric miaofe69af02008-02-14 15:48:23 +08001409 return 0;
1410}
1411
1412static int pxa3xx_nand_resume(struct platform_device *pdev)
1413{
Lei Wene353a202011-03-03 11:08:30 +08001414 struct pxa3xx_nand_info *info = platform_get_drvdata(pdev);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001415 struct pxa3xx_nand_platform_data *pdata;
1416 struct mtd_info *mtd;
1417 int cs;
Lei Wen051fc412011-07-14 20:44:30 -07001418
Jingoo Han453810b2013-07-30 17:18:33 +09001419 pdata = dev_get_platdata(&pdev->dev);
Lei Wen051fc412011-07-14 20:44:30 -07001420 /* We don't want to handle interrupt without calling mtd routine */
1421 disable_int(info, NDCR_INT_MASK);
eric miaofe69af02008-02-14 15:48:23 +08001422
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001423 /*
1424 * Directly set the chip select to a invalid value,
1425 * then the driver would reset the timing according
1426 * to current chip select at the beginning of cmdfunc
1427 */
1428 info->cs = 0xff;
eric miaofe69af02008-02-14 15:48:23 +08001429
Lei Wen051fc412011-07-14 20:44:30 -07001430 /*
1431 * As the spec says, the NDSR would be updated to 0x1800 when
1432 * doing the nand_clk disable/enable.
1433 * To prevent it damaging state machine of the driver, clear
1434 * all status before resume
1435 */
1436 nand_writel(info, NDSR, NDSR_MASK);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001437 for (cs = 0; cs < pdata->num_cs; cs++) {
1438 mtd = info->host[cs]->mtd;
Artem Bityutskiyead995f2011-12-23 19:31:25 +02001439 mtd_resume(mtd);
Lei Wenf3c8cfc2011-07-14 20:44:33 -07001440 }
1441
Lei Wen18c81b12010-08-17 17:25:57 +08001442 return 0;
eric miaofe69af02008-02-14 15:48:23 +08001443}
1444#else
1445#define pxa3xx_nand_suspend NULL
1446#define pxa3xx_nand_resume NULL
1447#endif
1448
1449static struct platform_driver pxa3xx_nand_driver = {
1450 .driver = {
1451 .name = "pxa3xx-nand",
Sachin Kamat5576bc72013-09-30 15:10:24 +05301452 .of_match_table = pxa3xx_nand_dt_ids,
eric miaofe69af02008-02-14 15:48:23 +08001453 },
1454 .probe = pxa3xx_nand_probe,
1455 .remove = pxa3xx_nand_remove,
1456 .suspend = pxa3xx_nand_suspend,
1457 .resume = pxa3xx_nand_resume,
1458};
1459
Axel Linf99640d2011-11-27 20:45:03 +08001460module_platform_driver(pxa3xx_nand_driver);
eric miaofe69af02008-02-14 15:48:23 +08001461
1462MODULE_LICENSE("GPL");
1463MODULE_DESCRIPTION("PXA3xx NAND controller driver");