blob: 26caa01e34a66321a3d2409e4295c3dcc1ea956f [file] [log] [blame]
Sascha Hauer34f6e152008-09-02 17:16:59 +02001/*
2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
18 */
19
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/nand.h>
26#include <linux/mtd/partitions.h>
27#include <linux/interrupt.h>
28#include <linux/device.h>
29#include <linux/platform_device.h>
30#include <linux/clk.h>
31#include <linux/err.h>
32#include <linux/io.h>
33
34#include <asm/mach/flash.h>
35#include <mach/mxc_nand.h>
Sascha Hauer94671142009-10-05 12:14:21 +020036#include <mach/hardware.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020037
38#define DRIVER_NAME "mxc_nand"
39
Sascha Hauer94671142009-10-05 12:14:21 +020040#define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
Ivo Claryssea47bfd22010-04-08 16:16:51 +020041#define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
Sascha Hauer71ec5152010-08-06 15:53:11 +020042#define nfc_is_v3_2() cpu_is_mx51()
43#define nfc_is_v3() nfc_is_v3_2()
Sascha Hauer94671142009-10-05 12:14:21 +020044
Sascha Hauer34f6e152008-09-02 17:16:59 +020045/* Addresses for NFC registers */
Sascha Hauer1bc99182010-08-06 15:53:08 +020046#define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
47#define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
48#define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
49#define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
50#define NFC_V1_V2_CONFIG (host->regs + 0x0a)
51#define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
52#define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
53#define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
54#define NFC_V1_V2_WRPROT (host->regs + 0x12)
55#define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
56#define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
57#define NFC_V21_UNLOCKSTART_BLKADDR (host->regs + 0x20)
58#define NFC_V21_UNLOCKEND_BLKADDR (host->regs + 0x22)
59#define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
60#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
61#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
Sascha Hauer34f6e152008-09-02 17:16:59 +020062
Sascha Hauer6e85dfd2010-08-06 15:53:10 +020063#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
Sascha Hauer1bc99182010-08-06 15:53:08 +020064#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
65#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
66#define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
67#define NFC_V1_V2_CONFIG1_BIG (1 << 5)
68#define NFC_V1_V2_CONFIG1_RST (1 << 6)
69#define NFC_V1_V2_CONFIG1_CE (1 << 7)
70#define NFC_V1_V2_CONFIG1_ONE_CYCLE (1 << 8)
Sascha Hauer34f6e152008-09-02 17:16:59 +020071
Sascha Hauer1bc99182010-08-06 15:53:08 +020072#define NFC_V1_V2_CONFIG2_INT (1 << 15)
Sascha Hauer34f6e152008-09-02 17:16:59 +020073
Sascha Hauer1bc99182010-08-06 15:53:08 +020074/*
75 * Operation modes for the NFC. Valid for v1, v2 and v3
76 * type controllers.
77 */
78#define NFC_CMD (1 << 0)
79#define NFC_ADDR (1 << 1)
80#define NFC_INPUT (1 << 2)
81#define NFC_OUTPUT (1 << 3)
82#define NFC_ID (1 << 4)
83#define NFC_STATUS (1 << 5)
Sascha Hauer34f6e152008-09-02 17:16:59 +020084
Sascha Hauer71ec5152010-08-06 15:53:11 +020085#define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
86#define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
Sascha Hauer34f6e152008-09-02 17:16:59 +020087
Sascha Hauer71ec5152010-08-06 15:53:11 +020088#define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
89#define NFC_V3_CONFIG1_SP_EN (1 << 0)
90#define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
Sascha Hauer34f6e152008-09-02 17:16:59 +020091
Sascha Hauer71ec5152010-08-06 15:53:11 +020092#define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
Sascha Hauer34f6e152008-09-02 17:16:59 +020093
Sascha Hauer71ec5152010-08-06 15:53:11 +020094#define NFC_V3_LAUNCH (host->regs_axi + 0x40)
Sascha Hauer34f6e152008-09-02 17:16:59 +020095
Sascha Hauer71ec5152010-08-06 15:53:11 +020096#define NFC_V3_WRPROT (host->regs_ip + 0x0)
97#define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
98#define NFC_V3_WRPROT_LOCK (1 << 1)
99#define NFC_V3_WRPROT_UNLOCK (1 << 2)
100#define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
101
102#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
103
104#define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
105#define NFC_V3_CONFIG2_PS_512 (0 << 0)
106#define NFC_V3_CONFIG2_PS_2048 (1 << 0)
107#define NFC_V3_CONFIG2_PS_4096 (2 << 0)
108#define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
109#define NFC_V3_CONFIG2_ECC_EN (1 << 3)
110#define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
111#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
112#define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
113#define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7)
114#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
115#define NFC_V3_CONFIG2_INT_MSK (1 << 15)
116#define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
117#define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
118
119#define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
120#define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
121#define NFC_V3_CONFIG3_FW8 (1 << 3)
122#define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
123#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
124#define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
125#define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
126
127#define NFC_V3_IPC (host->regs_ip + 0x2C)
128#define NFC_V3_IPC_CREQ (1 << 0)
129#define NFC_V3_IPC_INT (1 << 31)
130
131#define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200132
133struct mxc_nand_host {
134 struct mtd_info mtd;
135 struct nand_chip nand;
136 struct mtd_partition *parts;
137 struct device *dev;
138
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200139 void *spare0;
140 void *main_area0;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200141
142 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200143 void __iomem *regs;
Sascha Hauer71ec5152010-08-06 15:53:11 +0200144 void __iomem *regs_axi;
145 void __iomem *regs_ip;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200146 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200147 struct clk *clk;
148 int clk_act;
149 int irq;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200150 int eccsize;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200151
152 wait_queue_head_t irq_waitq;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200153
154 uint8_t *data_buf;
155 unsigned int buf_start;
156 int spare_len;
Sascha Hauer5f973042010-08-06 15:53:06 +0200157
158 void (*preset)(struct mtd_info *);
159 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
160 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
161 void (*send_page)(struct mtd_info *, unsigned int);
162 void (*send_read_id)(struct mxc_nand_host *);
163 uint16_t (*get_dev_status)(struct mxc_nand_host *);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200164 int (*check_int)(struct mxc_nand_host *);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200165};
166
Sascha Hauer34f6e152008-09-02 17:16:59 +0200167/* OOB placement block for use with hardware ecc generation */
Sascha Hauer94671142009-10-05 12:14:21 +0200168static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200169 .eccbytes = 5,
170 .eccpos = {6, 7, 8, 9, 10},
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200171 .oobfree = {{0, 5}, {12, 4}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200172};
173
Sascha Hauer94671142009-10-05 12:14:21 +0200174static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400175 .eccbytes = 20,
176 .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
177 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
178 .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200179};
180
Sascha Hauer94671142009-10-05 12:14:21 +0200181/* OOB description for 512 byte pages with 16 byte OOB */
182static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
183 .eccbytes = 1 * 9,
184 .eccpos = {
185 7, 8, 9, 10, 11, 12, 13, 14, 15
186 },
187 .oobfree = {
188 {.offset = 0, .length = 5}
189 }
190};
191
192/* OOB description for 2048 byte pages with 64 byte OOB */
193static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
194 .eccbytes = 4 * 9,
195 .eccpos = {
196 7, 8, 9, 10, 11, 12, 13, 14, 15,
197 23, 24, 25, 26, 27, 28, 29, 30, 31,
198 39, 40, 41, 42, 43, 44, 45, 46, 47,
199 55, 56, 57, 58, 59, 60, 61, 62, 63
200 },
201 .oobfree = {
202 {.offset = 2, .length = 4},
203 {.offset = 16, .length = 7},
204 {.offset = 32, .length = 7},
205 {.offset = 48, .length = 7}
206 }
207};
208
Sascha Hauer34f6e152008-09-02 17:16:59 +0200209#ifdef CONFIG_MTD_PARTITIONS
210static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
211#endif
212
213static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
214{
215 struct mxc_nand_host *host = dev_id;
216
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200217 disable_irq_nosync(irq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200218
219 wake_up(&host->irq_waitq);
220
221 return IRQ_HANDLED;
222}
223
Sascha Hauer71ec5152010-08-06 15:53:11 +0200224static int check_int_v3(struct mxc_nand_host *host)
225{
226 uint32_t tmp;
227
228 tmp = readl(NFC_V3_IPC);
229 if (!(tmp & NFC_V3_IPC_INT))
230 return 0;
231
232 tmp &= ~NFC_V3_IPC_INT;
233 writel(tmp, NFC_V3_IPC);
234
235 return 1;
236}
237
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200238static int check_int_v1_v2(struct mxc_nand_host *host)
239{
240 uint32_t tmp;
241
Sascha Hauer1bc99182010-08-06 15:53:08 +0200242 tmp = readw(NFC_V1_V2_CONFIG2);
243 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200244 return 0;
245
Sascha Hauer1bc99182010-08-06 15:53:08 +0200246 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200247
248 return 1;
249}
250
Sascha Hauer34f6e152008-09-02 17:16:59 +0200251/* This function polls the NANDFC to wait for the basic operation to
252 * complete by checking the INT bit of config2 register.
253 */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200254static void wait_op_done(struct mxc_nand_host *host, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200255{
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200256 int max_retries = 8000;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200257
258 if (useirq) {
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200259 if (!host->check_int(host)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200260
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200261 enable_irq(host->irq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200262
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200263 wait_event(host->irq_waitq, host->check_int(host));
Sascha Hauer34f6e152008-09-02 17:16:59 +0200264 }
265 } else {
266 while (max_retries-- > 0) {
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200267 if (host->check_int(host))
Sascha Hauer34f6e152008-09-02 17:16:59 +0200268 break;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200269
Sascha Hauer34f6e152008-09-02 17:16:59 +0200270 udelay(1);
271 }
Roel Kluin43950a62009-06-04 16:24:59 +0200272 if (max_retries < 0)
Sascha Hauer62465492009-06-04 15:57:20 +0200273 DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
274 __func__);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200275 }
276}
277
Sascha Hauer71ec5152010-08-06 15:53:11 +0200278static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
279{
280 /* fill command */
281 writel(cmd, NFC_V3_FLASH_CMD);
282
283 /* send out command */
284 writel(NFC_CMD, NFC_V3_LAUNCH);
285
286 /* Wait for operation to complete */
287 wait_op_done(host, useirq);
288}
289
Sascha Hauer34f6e152008-09-02 17:16:59 +0200290/* This function issues the specified command to the NAND device and
291 * waits for completion. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200292static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200293{
294 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
295
Sascha Hauer1bc99182010-08-06 15:53:08 +0200296 writew(cmd, NFC_V1_V2_FLASH_CMD);
297 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200298
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200299 if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
300 int max_retries = 100;
301 /* Reset completion is indicated by NFC_CONFIG2 */
302 /* being set to 0 */
303 while (max_retries-- > 0) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200304 if (readw(NFC_V1_V2_CONFIG2) == 0) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200305 break;
306 }
307 udelay(1);
308 }
309 if (max_retries < 0)
310 DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n",
311 __func__);
312 } else {
313 /* Wait for operation to complete */
314 wait_op_done(host, useirq);
315 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200316}
317
Sascha Hauer71ec5152010-08-06 15:53:11 +0200318static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
319{
320 /* fill address */
321 writel(addr, NFC_V3_FLASH_ADDR0);
322
323 /* send out address */
324 writel(NFC_ADDR, NFC_V3_LAUNCH);
325
326 wait_op_done(host, 0);
327}
328
Sascha Hauer34f6e152008-09-02 17:16:59 +0200329/* This function sends an address (or partial address) to the
330 * NAND device. The address is used to select the source/destination for
331 * a NAND command. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200332static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200333{
334 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
335
Sascha Hauer1bc99182010-08-06 15:53:08 +0200336 writew(addr, NFC_V1_V2_FLASH_ADDR);
337 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200338
339 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200340 wait_op_done(host, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200341}
342
Sascha Hauer71ec5152010-08-06 15:53:11 +0200343static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
344{
345 struct nand_chip *nand_chip = mtd->priv;
346 struct mxc_nand_host *host = nand_chip->priv;
347 uint32_t tmp;
348
349 tmp = readl(NFC_V3_CONFIG1);
350 tmp &= ~(7 << 4);
351 writel(tmp, NFC_V3_CONFIG1);
352
353 /* transfer data from NFC ram to nand */
354 writel(ops, NFC_V3_LAUNCH);
355
356 wait_op_done(host, false);
357}
358
Sascha Hauer5f973042010-08-06 15:53:06 +0200359static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200360{
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200361 struct nand_chip *nand_chip = mtd->priv;
362 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200363 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200364
Sascha Hauer94671142009-10-05 12:14:21 +0200365 if (nfc_is_v1() && mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200366 bufs = 4;
367 else
368 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200369
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200370 for (i = 0; i < bufs; i++) {
371
372 /* NANDFC buffer 0 is used for page read/write */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200373 writew(i, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200374
Sascha Hauer1bc99182010-08-06 15:53:08 +0200375 writew(ops, NFC_V1_V2_CONFIG2);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200376
377 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200378 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200379 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200380}
381
Sascha Hauer71ec5152010-08-06 15:53:11 +0200382static void send_read_id_v3(struct mxc_nand_host *host)
383{
384 /* Read ID into main buffer */
385 writel(NFC_ID, NFC_V3_LAUNCH);
386
387 wait_op_done(host, true);
388
389 memcpy(host->data_buf, host->main_area0, 16);
390}
391
Sascha Hauer34f6e152008-09-02 17:16:59 +0200392/* Request the NANDFC to perform a read of the NAND device ID. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200393static void send_read_id_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200394{
395 struct nand_chip *this = &host->nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200396
397 /* NANDFC buffer 0 is used for device ID output */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200398 writew(0x0, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200399
Sascha Hauer1bc99182010-08-06 15:53:08 +0200400 writew(NFC_ID, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200401
402 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200403 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200404
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200405 memcpy(host->data_buf, host->main_area0, 16);
John Ognessf7b66e52010-06-18 18:59:47 +0200406
407 if (this->options & NAND_BUSWIDTH_16) {
408 /* compress the ID info */
409 host->data_buf[1] = host->data_buf[2];
410 host->data_buf[2] = host->data_buf[4];
411 host->data_buf[3] = host->data_buf[6];
412 host->data_buf[4] = host->data_buf[8];
413 host->data_buf[5] = host->data_buf[10];
414 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200415}
416
Sascha Hauer71ec5152010-08-06 15:53:11 +0200417static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200418{
Sascha Hauer71ec5152010-08-06 15:53:11 +0200419 writew(NFC_STATUS, NFC_V3_LAUNCH);
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200420 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200421
Sascha Hauer71ec5152010-08-06 15:53:11 +0200422 return readl(NFC_V3_CONFIG1) >> 16;
423}
424
Sascha Hauer34f6e152008-09-02 17:16:59 +0200425/* This function requests the NANDFC to perform a read of the
426 * NAND device status and returns the current status. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200427static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200428{
Sascha Hauerc29c6072010-08-06 15:53:05 +0200429 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200430 uint32_t store;
431 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200432
Sascha Hauerc29c6072010-08-06 15:53:05 +0200433 writew(0x0, NFC_V1_V2_BUF_ADDR);
434
435 /*
436 * The device status is stored in main_area0. To
437 * prevent corruption of the buffer save the value
438 * and restore it afterwards.
439 */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200440 store = readl(main_buf);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200441
Sascha Hauer1bc99182010-08-06 15:53:08 +0200442 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200443 wait_op_done(host, true);
444
Sascha Hauer34f6e152008-09-02 17:16:59 +0200445 ret = readw(main_buf);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200446
Sascha Hauer34f6e152008-09-02 17:16:59 +0200447 writel(store, main_buf);
448
449 return ret;
450}
451
452/* This functions is used by upper layer to checks if device is ready */
453static int mxc_nand_dev_ready(struct mtd_info *mtd)
454{
455 /*
456 * NFC handles R/B internally. Therefore, this function
457 * always returns status as ready.
458 */
459 return 1;
460}
461
462static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
463{
464 /*
465 * If HW ECC is enabled, we turn it on during init. There is
466 * no need to enable again here.
467 */
468}
469
Sascha Hauer94f77e52010-08-06 15:53:09 +0200470static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
Sascha Hauer34f6e152008-09-02 17:16:59 +0200471 u_char *read_ecc, u_char *calc_ecc)
472{
473 struct nand_chip *nand_chip = mtd->priv;
474 struct mxc_nand_host *host = nand_chip->priv;
475
476 /*
477 * 1-Bit errors are automatically corrected in HW. No need for
478 * additional correction. 2-Bit errors cannot be corrected by
479 * HW ECC, so we need to return failure
480 */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200481 uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200482
483 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
484 DEBUG(MTD_DEBUG_LEVEL0,
485 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
486 return -1;
487 }
488
489 return 0;
490}
491
Sascha Hauer94f77e52010-08-06 15:53:09 +0200492static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
493 u_char *read_ecc, u_char *calc_ecc)
494{
495 struct nand_chip *nand_chip = mtd->priv;
496 struct mxc_nand_host *host = nand_chip->priv;
497 u32 ecc_stat, err;
498 int no_subpages = 1;
499 int ret = 0;
500 u8 ecc_bit_mask, err_limit;
501
502 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
503 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
504
505 no_subpages = mtd->writesize >> 9;
506
Sascha Hauer71ec5152010-08-06 15:53:11 +0200507 if (nfc_is_v21())
508 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
509 else
510 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200511
512 do {
513 err = ecc_stat & ecc_bit_mask;
514 if (err > err_limit) {
515 printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
516 return -1;
517 } else {
518 ret += err;
519 }
520 ecc_stat >>= 4;
521 } while (--no_subpages);
522
523 mtd->ecc_stats.corrected += ret;
524 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
525
526 return ret;
527}
528
Sascha Hauer34f6e152008-09-02 17:16:59 +0200529static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
530 u_char *ecc_code)
531{
532 return 0;
533}
534
535static u_char mxc_nand_read_byte(struct mtd_info *mtd)
536{
537 struct nand_chip *nand_chip = mtd->priv;
538 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200539 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200540
541 /* Check for status request */
542 if (host->status_request)
Sascha Hauer5f973042010-08-06 15:53:06 +0200543 return host->get_dev_status(host) & 0xFF;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200544
Sascha Hauerf8f96082009-06-04 17:12:26 +0200545 ret = *(uint8_t *)(host->data_buf + host->buf_start);
546 host->buf_start++;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200547
548 return ret;
549}
550
551static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
552{
553 struct nand_chip *nand_chip = mtd->priv;
554 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200555 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200556
Sascha Hauerf8f96082009-06-04 17:12:26 +0200557 ret = *(uint16_t *)(host->data_buf + host->buf_start);
558 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200559
560 return ret;
561}
562
563/* Write data of length len to buffer buf. The data to be
564 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
565 * Operation by the NFC, the data is written to NAND Flash */
566static void mxc_nand_write_buf(struct mtd_info *mtd,
567 const u_char *buf, int len)
568{
569 struct nand_chip *nand_chip = mtd->priv;
570 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200571 u16 col = host->buf_start;
572 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200573
Sascha Hauerf8f96082009-06-04 17:12:26 +0200574 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200575
Sascha Hauerf8f96082009-06-04 17:12:26 +0200576 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200577
Sascha Hauerf8f96082009-06-04 17:12:26 +0200578 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200579}
580
581/* Read the data buffer from the NAND Flash. To read the data from NAND
582 * Flash first the data output cycle is initiated by the NFC, which copies
583 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
584 */
585static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
586{
587 struct nand_chip *nand_chip = mtd->priv;
588 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200589 u16 col = host->buf_start;
590 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200591
Sascha Hauerf8f96082009-06-04 17:12:26 +0200592 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200593
Sascha Hauerf8f96082009-06-04 17:12:26 +0200594 memcpy(buf, host->data_buf + col, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200595
Sascha Hauerf8f96082009-06-04 17:12:26 +0200596 host->buf_start += len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200597}
598
599/* Used by the upper layer to verify the data in NAND Flash
600 * with the data in the buf. */
601static int mxc_nand_verify_buf(struct mtd_info *mtd,
602 const u_char *buf, int len)
603{
604 return -EFAULT;
605}
606
607/* This function is used by upper layer for select and
608 * deselect of the NAND chip */
609static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
610{
611 struct nand_chip *nand_chip = mtd->priv;
612 struct mxc_nand_host *host = nand_chip->priv;
613
Sascha Hauer34f6e152008-09-02 17:16:59 +0200614 switch (chip) {
615 case -1:
616 /* Disable the NFC clock */
617 if (host->clk_act) {
618 clk_disable(host->clk);
619 host->clk_act = 0;
620 }
621 break;
622 case 0:
623 /* Enable the NFC clock */
624 if (!host->clk_act) {
625 clk_enable(host->clk);
626 host->clk_act = 1;
627 }
628 break;
629
630 default:
631 break;
632 }
633}
634
Sascha Hauerf8f96082009-06-04 17:12:26 +0200635/*
636 * Function to transfer data to/from spare area.
637 */
638static void copy_spare(struct mtd_info *mtd, bool bfrom)
639{
640 struct nand_chip *this = mtd->priv;
641 struct mxc_nand_host *host = this->priv;
642 u16 i, j;
643 u16 n = mtd->writesize >> 9;
644 u8 *d = host->data_buf + mtd->writesize;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200645 u8 *s = host->spare0;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200646 u16 t = host->spare_len;
647
648 j = (mtd->oobsize / n >> 1) << 1;
649
650 if (bfrom) {
651 for (i = 0; i < n - 1; i++)
652 memcpy(d + i * j, s + i * t, j);
653
654 /* the last section */
655 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
656 } else {
657 for (i = 0; i < n - 1; i++)
658 memcpy(&s[i * t], &d[i * j], j);
659
660 /* the last section */
661 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
662 }
663}
664
Sascha Hauera3e65b62009-06-02 11:47:59 +0200665static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200666{
667 struct nand_chip *nand_chip = mtd->priv;
668 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200669
670 /* Write out column address, if necessary */
671 if (column != -1) {
672 /*
673 * MXC NANDFC can only perform full page+spare or
674 * spare-only read/write. When the upper layers
675 * layers perform a read/write buf operation,
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800676 * we will used the saved column address to index into
Sascha Hauer34f6e152008-09-02 17:16:59 +0200677 * the full page.
678 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200679 host->send_addr(host, 0, page_addr == -1);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200680 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200681 /* another col addr cycle for 2k page */
Sascha Hauer5f973042010-08-06 15:53:06 +0200682 host->send_addr(host, 0, false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200683 }
684
685 /* Write out page address, if necessary */
686 if (page_addr != -1) {
687 /* paddr_0 - p_addr_7 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200688 host->send_addr(host, (page_addr & 0xff), false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200689
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200690 if (mtd->writesize > 512) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400691 if (mtd->size >= 0x10000000) {
692 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200693 host->send_addr(host, (page_addr >> 8) & 0xff, false);
694 host->send_addr(host, (page_addr >> 16) & 0xff, true);
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400695 } else
696 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200697 host->send_addr(host, (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200698 } else {
699 /* One more address cycle for higher density devices */
700 if (mtd->size >= 0x4000000) {
701 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200702 host->send_addr(host, (page_addr >> 8) & 0xff, false);
703 host->send_addr(host, (page_addr >> 16) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200704 } else
705 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200706 host->send_addr(host, (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200707 }
708 }
Sascha Hauera3e65b62009-06-02 11:47:59 +0200709}
Sascha Hauer34f6e152008-09-02 17:16:59 +0200710
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200711/*
712 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
713 * on how much oob the nand chip has. For 8bit ecc we need at least
714 * 26 bytes of oob data per 512 byte block.
715 */
716static int get_eccsize(struct mtd_info *mtd)
717{
718 int oobbytes_per_512 = 0;
719
720 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
721
722 if (oobbytes_per_512 < 26)
723 return 4;
724 else
725 return 8;
726}
727
Sascha Hauer5f973042010-08-06 15:53:06 +0200728static void preset_v1_v2(struct mtd_info *mtd)
Ivo Claryssed4840182010-04-08 16:14:44 +0200729{
730 struct nand_chip *nand_chip = mtd->priv;
731 struct mxc_nand_host *host = nand_chip->priv;
732 uint16_t tmp;
733
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200734 /* enable interrupt, disable spare enable */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200735 tmp = readw(NFC_V1_V2_CONFIG1);
736 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
737 tmp &= ~NFC_V1_V2_CONFIG1_SP_EN;
Ivo Claryssed4840182010-04-08 16:14:44 +0200738 if (nand_chip->ecc.mode == NAND_ECC_HW) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200739 tmp |= NFC_V1_V2_CONFIG1_ECC_EN;
Ivo Claryssed4840182010-04-08 16:14:44 +0200740 } else {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200741 tmp &= ~NFC_V1_V2_CONFIG1_ECC_EN;
Ivo Claryssed4840182010-04-08 16:14:44 +0200742 }
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200743
744 if (nfc_is_v21() && mtd->writesize) {
745 host->eccsize = get_eccsize(mtd);
746 if (host->eccsize == 4)
747 tmp |= NFC_V2_CONFIG1_ECC_MODE_4;
748 } else {
749 host->eccsize = 1;
750 }
751
Sascha Hauer1bc99182010-08-06 15:53:08 +0200752 writew(tmp, NFC_V1_V2_CONFIG1);
Ivo Claryssed4840182010-04-08 16:14:44 +0200753 /* preset operation */
754
755 /* Unlock the internal RAM Buffer */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200756 writew(0x2, NFC_V1_V2_CONFIG);
Ivo Claryssed4840182010-04-08 16:14:44 +0200757
758 /* Blocks to be unlocked */
759 if (nfc_is_v21()) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200760 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
761 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
Ivo Claryssed4840182010-04-08 16:14:44 +0200762 } else if (nfc_is_v1()) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200763 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
764 writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
Ivo Claryssed4840182010-04-08 16:14:44 +0200765 } else
766 BUG();
767
768 /* Unlock Block Command for given address range */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200769 writew(0x4, NFC_V1_V2_WRPROT);
Ivo Claryssed4840182010-04-08 16:14:44 +0200770}
771
Sascha Hauer71ec5152010-08-06 15:53:11 +0200772static void preset_v3(struct mtd_info *mtd)
773{
774 struct nand_chip *chip = mtd->priv;
775 struct mxc_nand_host *host = chip->priv;
776 uint32_t config2, config3;
777 int i, addr_phases;
778
779 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
780 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
781
782 /* Unlock the internal RAM Buffer */
783 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
784 NFC_V3_WRPROT);
785
786 /* Blocks to be unlocked */
787 for (i = 0; i < NAND_MAX_CHIPS; i++)
788 writel(0x0 | (0xffff << 16),
789 NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
790
791 writel(0, NFC_V3_IPC);
792
793 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
794 NFC_V3_CONFIG2_2CMD_PHASES |
795 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
796 NFC_V3_CONFIG2_ST_CMD(0x70) |
797 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
798
799 if (chip->ecc.mode == NAND_ECC_HW)
800 config2 |= NFC_V3_CONFIG2_ECC_EN;
801
802 addr_phases = fls(chip->pagemask) >> 3;
803
804 if (mtd->writesize == 2048) {
805 config2 |= NFC_V3_CONFIG2_PS_2048;
806 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
807 } else if (mtd->writesize == 4096) {
808 config2 |= NFC_V3_CONFIG2_PS_4096;
809 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
810 } else {
811 config2 |= NFC_V3_CONFIG2_PS_512;
812 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
813 }
814
815 if (mtd->writesize) {
816 config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
817 host->eccsize = get_eccsize(mtd);
818 if (host->eccsize == 8)
819 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
820 }
821
822 writel(config2, NFC_V3_CONFIG2);
823
824 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
825 NFC_V3_CONFIG3_NO_SDMA |
826 NFC_V3_CONFIG3_RBB_MODE |
827 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
828 NFC_V3_CONFIG3_ADD_OP(0);
829
830 if (!(chip->options & NAND_BUSWIDTH_16))
831 config3 |= NFC_V3_CONFIG3_FW8;
832
833 writel(config3, NFC_V3_CONFIG3);
834
835 writel(0, NFC_V3_DELAY_LINE);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200836}
837
Sascha Hauer34f6e152008-09-02 17:16:59 +0200838/* Used by the upper layer to write command to NAND Flash for
839 * different operations to be carried out on NAND Flash */
840static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
841 int column, int page_addr)
842{
843 struct nand_chip *nand_chip = mtd->priv;
844 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200845
846 DEBUG(MTD_DEBUG_LEVEL3,
847 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
848 command, column, page_addr);
849
850 /* Reset command state information */
851 host->status_request = false;
852
853 /* Command pre-processing step */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200854 switch (command) {
Ivo Claryssed4840182010-04-08 16:14:44 +0200855 case NAND_CMD_RESET:
Sascha Hauer5f973042010-08-06 15:53:06 +0200856 host->preset(mtd);
857 host->send_cmd(host, command, false);
Ivo Claryssed4840182010-04-08 16:14:44 +0200858 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200859
Sascha Hauer34f6e152008-09-02 17:16:59 +0200860 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200861 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200862 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +0200863
Sascha Hauer5f973042010-08-06 15:53:06 +0200864 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200865 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200866 break;
867
Sascha Hauer34f6e152008-09-02 17:16:59 +0200868 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200869 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +0200870 if (command == NAND_CMD_READ0)
871 host->buf_start = column;
872 else
873 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200874
Sascha Hauer5ea32022010-04-27 15:24:01 +0200875 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +0200876
Sascha Hauer5f973042010-08-06 15:53:06 +0200877 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200878 mxc_do_addr_cycle(mtd, column, page_addr);
879
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200880 if (mtd->writesize > 512)
Sascha Hauer5f973042010-08-06 15:53:06 +0200881 host->send_cmd(host, NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200882
Sascha Hauer5f973042010-08-06 15:53:06 +0200883 host->send_page(mtd, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200884
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200885 memcpy(host->data_buf, host->main_area0, mtd->writesize);
Sascha Hauer89121a62009-06-04 17:18:01 +0200886 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200887 break;
888
Sascha Hauer34f6e152008-09-02 17:16:59 +0200889 case NAND_CMD_SEQIN:
Sascha Hauer5ea32022010-04-27 15:24:01 +0200890 if (column >= mtd->writesize)
891 /* call ourself to read a page */
892 mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200893
Sascha Hauer5ea32022010-04-27 15:24:01 +0200894 host->buf_start = column;
Sascha Hauer89121a62009-06-04 17:18:01 +0200895
Sascha Hauer5f973042010-08-06 15:53:06 +0200896 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200897 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200898 break;
899
900 case NAND_CMD_PAGEPROG:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200901 memcpy(host->main_area0, host->data_buf, mtd->writesize);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200902 copy_spare(mtd, false);
Sascha Hauer5f973042010-08-06 15:53:06 +0200903 host->send_page(mtd, NFC_INPUT);
904 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200905 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200906 break;
907
Sascha Hauer34f6e152008-09-02 17:16:59 +0200908 case NAND_CMD_READID:
Sascha Hauer5f973042010-08-06 15:53:06 +0200909 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200910 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer5f973042010-08-06 15:53:06 +0200911 host->send_read_id(host);
Sascha Hauer94671142009-10-05 12:14:21 +0200912 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200913 break;
914
Sascha Hauer89121a62009-06-04 17:18:01 +0200915 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200916 case NAND_CMD_ERASE2:
Sascha Hauer5f973042010-08-06 15:53:06 +0200917 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200918 mxc_do_addr_cycle(mtd, column, page_addr);
919
Sascha Hauer34f6e152008-09-02 17:16:59 +0200920 break;
921 }
922}
923
Sascha Hauerf1372052009-10-21 14:25:27 +0200924/*
925 * The generic flash bbt decriptors overlap with our ecc
926 * hardware, so define some i.MX specific ones.
927 */
928static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
929static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
930
931static struct nand_bbt_descr bbt_main_descr = {
932 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
933 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
934 .offs = 0,
935 .len = 4,
936 .veroffs = 4,
937 .maxblocks = 4,
938 .pattern = bbt_pattern,
939};
940
941static struct nand_bbt_descr bbt_mirror_descr = {
942 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
943 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
944 .offs = 0,
945 .len = 4,
946 .veroffs = 4,
947 .maxblocks = 4,
948 .pattern = mirror_pattern,
949};
950
Sascha Hauer34f6e152008-09-02 17:16:59 +0200951static int __init mxcnd_probe(struct platform_device *pdev)
952{
953 struct nand_chip *this;
954 struct mtd_info *mtd;
955 struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
956 struct mxc_nand_host *host;
957 struct resource *res;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200958 int err = 0, nr_parts = 0;
Sascha Hauer94671142009-10-05 12:14:21 +0200959 struct nand_ecclayout *oob_smallpage, *oob_largepage;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200960
961 /* Allocate memory for MTD device structure and private data */
Sascha Hauerf8f96082009-06-04 17:12:26 +0200962 host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
963 NAND_MAX_OOBSIZE, GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200964 if (!host)
965 return -ENOMEM;
966
Sascha Hauerf8f96082009-06-04 17:12:26 +0200967 host->data_buf = (uint8_t *)(host + 1);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200968
Sascha Hauer34f6e152008-09-02 17:16:59 +0200969 host->dev = &pdev->dev;
970 /* structures must be linked */
971 this = &host->nand;
972 mtd = &host->mtd;
973 mtd->priv = this;
974 mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700975 mtd->dev.parent = &pdev->dev;
Sascha Hauer1fbff0a2009-10-21 16:06:27 +0200976 mtd->name = DRIVER_NAME;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200977
978 /* 50 us command delay time */
979 this->chip_delay = 5;
980
981 this->priv = host;
982 this->dev_ready = mxc_nand_dev_ready;
983 this->cmdfunc = mxc_nand_command;
984 this->select_chip = mxc_nand_select_chip;
985 this->read_byte = mxc_nand_read_byte;
986 this->read_word = mxc_nand_read_word;
987 this->write_buf = mxc_nand_write_buf;
988 this->read_buf = mxc_nand_read_buf;
989 this->verify_buf = mxc_nand_verify_buf;
990
Sascha Hauere65fb002009-02-16 14:29:10 +0100991 host->clk = clk_get(&pdev->dev, "nfc");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400992 if (IS_ERR(host->clk)) {
993 err = PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200994 goto eclk;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400995 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200996
997 clk_enable(host->clk);
998 host->clk_act = 1;
999
1000 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1001 if (!res) {
1002 err = -ENODEV;
1003 goto eres;
1004 }
1005
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001006 host->base = ioremap(res->start, resource_size(res));
1007 if (!host->base) {
Vladimir Barinov8541c112009-04-23 15:47:22 +04001008 err = -ENOMEM;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001009 goto eres;
1010 }
1011
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001012 host->main_area0 = host->base;
Sascha Hauer94671142009-10-05 12:14:21 +02001013
Sascha Hauer5f973042010-08-06 15:53:06 +02001014 if (nfc_is_v1() || nfc_is_v21()) {
1015 host->preset = preset_v1_v2;
1016 host->send_cmd = send_cmd_v1_v2;
1017 host->send_addr = send_addr_v1_v2;
1018 host->send_page = send_page_v1_v2;
1019 host->send_read_id = send_read_id_v1_v2;
1020 host->get_dev_status = get_dev_status_v1_v2;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +02001021 host->check_int = check_int_v1_v2;
Sascha Hauer5f973042010-08-06 15:53:06 +02001022 }
Sascha Hauer94671142009-10-05 12:14:21 +02001023
1024 if (nfc_is_v21()) {
Sascha Hauer938cf992010-08-06 15:53:04 +02001025 host->regs = host->base + 0x1e00;
Sascha Hauer94671142009-10-05 12:14:21 +02001026 host->spare0 = host->base + 0x1000;
1027 host->spare_len = 64;
1028 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1029 oob_largepage = &nandv2_hw_eccoob_largepage;
Ivo Claryssed4840182010-04-08 16:14:44 +02001030 this->ecc.bytes = 9;
Sascha Hauer94671142009-10-05 12:14:21 +02001031 } else if (nfc_is_v1()) {
Sascha Hauer938cf992010-08-06 15:53:04 +02001032 host->regs = host->base + 0xe00;
Sascha Hauer94671142009-10-05 12:14:21 +02001033 host->spare0 = host->base + 0x800;
1034 host->spare_len = 16;
1035 oob_smallpage = &nandv1_hw_eccoob_smallpage;
1036 oob_largepage = &nandv1_hw_eccoob_largepage;
Sascha Hauer94671142009-10-05 12:14:21 +02001037 this->ecc.bytes = 3;
Sascha Hauer71ec5152010-08-06 15:53:11 +02001038 host->eccsize = 1;
1039 } else if (nfc_is_v3_2()) {
1040 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1041 if (!res) {
1042 err = -ENODEV;
1043 goto eirq;
1044 }
1045 host->regs_ip = ioremap(res->start, resource_size(res));
1046 if (!host->regs_ip) {
1047 err = -ENOMEM;
1048 goto eirq;
1049 }
1050 host->regs_axi = host->base + 0x1e00;
1051 host->spare0 = host->base + 0x1000;
1052 host->spare_len = 64;
1053 host->preset = preset_v3;
1054 host->send_cmd = send_cmd_v3;
1055 host->send_addr = send_addr_v3;
1056 host->send_page = send_page_v3;
1057 host->send_read_id = send_read_id_v3;
1058 host->check_int = check_int_v3;
1059 host->get_dev_status = get_dev_status_v3;
1060 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1061 oob_largepage = &nandv2_hw_eccoob_largepage;
Sascha Hauer94671142009-10-05 12:14:21 +02001062 } else
1063 BUG();
Sascha Hauer34f6e152008-09-02 17:16:59 +02001064
Sascha Hauer13e1add2009-10-21 10:39:05 +02001065 this->ecc.size = 512;
Sascha Hauer94671142009-10-05 12:14:21 +02001066 this->ecc.layout = oob_smallpage;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001067
1068 if (pdata->hw_ecc) {
1069 this->ecc.calculate = mxc_nand_calculate_ecc;
1070 this->ecc.hwctl = mxc_nand_enable_hwecc;
Sascha Hauer94f77e52010-08-06 15:53:09 +02001071 if (nfc_is_v1())
1072 this->ecc.correct = mxc_nand_correct_data_v1;
1073 else
1074 this->ecc.correct = mxc_nand_correct_data_v2_v3;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001075 this->ecc.mode = NAND_ECC_HW;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001076 } else {
1077 this->ecc.mode = NAND_ECC_SOFT;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001078 }
1079
Sascha Hauer34f6e152008-09-02 17:16:59 +02001080 /* NAND bus width determines access funtions used by upper layer */
Sascha Hauer13e1add2009-10-21 10:39:05 +02001081 if (pdata->width == 2)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001082 this->options |= NAND_BUSWIDTH_16;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001083
Sascha Hauerf1372052009-10-21 14:25:27 +02001084 if (pdata->flash_bbt) {
1085 this->bbt_td = &bbt_main_descr;
1086 this->bbt_md = &bbt_mirror_descr;
1087 /* update flash based bbt */
1088 this->options |= NAND_USE_FLASH_BBT;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001089 }
1090
Ivo Claryssed4840182010-04-08 16:14:44 +02001091 init_waitqueue_head(&host->irq_waitq);
1092
1093 host->irq = platform_get_irq(pdev, 0);
1094
Ivo Claryssea47bfd22010-04-08 16:16:51 +02001095 err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
Ivo Claryssed4840182010-04-08 16:14:44 +02001096 if (err)
1097 goto eirq;
1098
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001099 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001100 if (nand_scan_ident(mtd, 1, NULL)) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001101 err = -ENXIO;
1102 goto escan;
1103 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001104
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001105 /* Call preset again, with correct writesize this time */
1106 host->preset(mtd);
1107
Sascha Hauer2d69c7f2009-10-05 11:24:02 +02001108 if (mtd->writesize == 2048)
Sascha Hauer94671142009-10-05 12:14:21 +02001109 this->ecc.layout = oob_largepage;
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001110
1111 /* second phase scan */
1112 if (nand_scan_tail(mtd)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +02001113 err = -ENXIO;
1114 goto escan;
1115 }
1116
1117 /* Register the partitions */
1118#ifdef CONFIG_MTD_PARTITIONS
1119 nr_parts =
1120 parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
1121 if (nr_parts > 0)
1122 add_mtd_partitions(mtd, host->parts, nr_parts);
Baruch Siachcce02462010-05-31 08:49:40 +03001123 else if (pdata->parts)
1124 add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001125 else
1126#endif
1127 {
1128 pr_info("Registering %s as whole device\n", mtd->name);
1129 add_mtd_device(mtd);
1130 }
1131
1132 platform_set_drvdata(pdev, host);
1133
1134 return 0;
1135
1136escan:
Magnus Liljab258fd82009-05-08 21:57:47 +02001137 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001138eirq:
Sascha Hauer71ec5152010-08-06 15:53:11 +02001139 if (host->regs_ip)
1140 iounmap(host->regs_ip);
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001141 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001142eres:
1143 clk_put(host->clk);
1144eclk:
1145 kfree(host);
1146
1147 return err;
1148}
1149
Uwe Kleine-König51eeb872009-12-07 09:44:05 +00001150static int __devexit mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001151{
1152 struct mxc_nand_host *host = platform_get_drvdata(pdev);
1153
1154 clk_put(host->clk);
1155
1156 platform_set_drvdata(pdev, NULL);
1157
1158 nand_release(&host->mtd);
Magnus Liljab258fd82009-05-08 21:57:47 +02001159 free_irq(host->irq, host);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001160 if (host->regs_ip)
1161 iounmap(host->regs_ip);
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001162 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001163 kfree(host);
1164
1165 return 0;
1166}
1167
Sascha Hauer34f6e152008-09-02 17:16:59 +02001168static struct platform_driver mxcnd_driver = {
1169 .driver = {
1170 .name = DRIVER_NAME,
Eric Bénard04dd0d32010-06-17 20:59:04 +02001171 },
Uwe Kleine-Königdaa0f152009-11-24 22:07:08 +01001172 .remove = __devexit_p(mxcnd_remove),
Sascha Hauer34f6e152008-09-02 17:16:59 +02001173};
1174
1175static int __init mxc_nd_init(void)
1176{
Vladimir Barinov8541c112009-04-23 15:47:22 +04001177 return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001178}
1179
1180static void __exit mxc_nd_cleanup(void)
1181{
1182 /* Unregister the device structure */
1183 platform_driver_unregister(&mxcnd_driver);
1184}
1185
1186module_init(mxc_nd_init);
1187module_exit(mxc_nd_cleanup);
1188
1189MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1190MODULE_DESCRIPTION("MXC NAND MTD driver");
1191MODULE_LICENSE("GPL");