blob: 6f6068043faddb39d769e685f6b821fea4b610c2 [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>
Sascha Hauer63f14742010-10-18 10:16:26 +020033#include <linux/irq.h>
34#include <linux/completion.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020035
36#include <asm/mach/flash.h>
37#include <mach/mxc_nand.h>
Sascha Hauer94671142009-10-05 12:14:21 +020038#include <mach/hardware.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020039
40#define DRIVER_NAME "mxc_nand"
41
Sascha Hauer94671142009-10-05 12:14:21 +020042#define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
Ivo Claryssea47bfd22010-04-08 16:16:51 +020043#define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27() || cpu_is_mx21())
Jason Liuc97926d2011-08-22 14:13:17 +080044#define nfc_is_v3_2() (cpu_is_mx51() || cpu_is_mx53())
Sascha Hauer71ec5152010-08-06 15:53:11 +020045#define nfc_is_v3() nfc_is_v3_2()
Sascha Hauer94671142009-10-05 12:14:21 +020046
Sascha Hauer34f6e152008-09-02 17:16:59 +020047/* Addresses for NFC registers */
Sascha Hauer1bc99182010-08-06 15:53:08 +020048#define NFC_V1_V2_BUF_SIZE (host->regs + 0x00)
49#define NFC_V1_V2_BUF_ADDR (host->regs + 0x04)
50#define NFC_V1_V2_FLASH_ADDR (host->regs + 0x06)
51#define NFC_V1_V2_FLASH_CMD (host->regs + 0x08)
52#define NFC_V1_V2_CONFIG (host->regs + 0x0a)
53#define NFC_V1_V2_ECC_STATUS_RESULT (host->regs + 0x0c)
54#define NFC_V1_V2_RSLTMAIN_AREA (host->regs + 0x0e)
55#define NFC_V1_V2_RSLTSPARE_AREA (host->regs + 0x10)
56#define NFC_V1_V2_WRPROT (host->regs + 0x12)
57#define NFC_V1_UNLOCKSTART_BLKADDR (host->regs + 0x14)
58#define NFC_V1_UNLOCKEND_BLKADDR (host->regs + 0x16)
Baruch Siachd178e3e2011-03-14 09:01:56 +020059#define NFC_V21_UNLOCKSTART_BLKADDR0 (host->regs + 0x20)
60#define NFC_V21_UNLOCKSTART_BLKADDR1 (host->regs + 0x24)
61#define NFC_V21_UNLOCKSTART_BLKADDR2 (host->regs + 0x28)
62#define NFC_V21_UNLOCKSTART_BLKADDR3 (host->regs + 0x2c)
63#define NFC_V21_UNLOCKEND_BLKADDR0 (host->regs + 0x22)
64#define NFC_V21_UNLOCKEND_BLKADDR1 (host->regs + 0x26)
65#define NFC_V21_UNLOCKEND_BLKADDR2 (host->regs + 0x2a)
66#define NFC_V21_UNLOCKEND_BLKADDR3 (host->regs + 0x2e)
Sascha Hauer1bc99182010-08-06 15:53:08 +020067#define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
68#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
69#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
Sascha Hauer34f6e152008-09-02 17:16:59 +020070
Sascha Hauer6e85dfd2010-08-06 15:53:10 +020071#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
Sascha Hauer1bc99182010-08-06 15:53:08 +020072#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
73#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
74#define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
75#define NFC_V1_V2_CONFIG1_BIG (1 << 5)
76#define NFC_V1_V2_CONFIG1_RST (1 << 6)
77#define NFC_V1_V2_CONFIG1_CE (1 << 7)
Sascha Hauerb8db2f52010-08-09 15:04:19 +020078#define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
79#define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
80#define NFC_V2_CONFIG1_FP_INT (1 << 11)
Sascha Hauer34f6e152008-09-02 17:16:59 +020081
Sascha Hauer1bc99182010-08-06 15:53:08 +020082#define NFC_V1_V2_CONFIG2_INT (1 << 15)
Sascha Hauer34f6e152008-09-02 17:16:59 +020083
Sascha Hauer1bc99182010-08-06 15:53:08 +020084/*
85 * Operation modes for the NFC. Valid for v1, v2 and v3
86 * type controllers.
87 */
88#define NFC_CMD (1 << 0)
89#define NFC_ADDR (1 << 1)
90#define NFC_INPUT (1 << 2)
91#define NFC_OUTPUT (1 << 3)
92#define NFC_ID (1 << 4)
93#define NFC_STATUS (1 << 5)
Sascha Hauer34f6e152008-09-02 17:16:59 +020094
Sascha Hauer71ec5152010-08-06 15:53:11 +020095#define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
96#define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
Sascha Hauer34f6e152008-09-02 17:16:59 +020097
Sascha Hauer71ec5152010-08-06 15:53:11 +020098#define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
99#define NFC_V3_CONFIG1_SP_EN (1 << 0)
100#define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200101
Sascha Hauer71ec5152010-08-06 15:53:11 +0200102#define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200103
Sascha Hauer71ec5152010-08-06 15:53:11 +0200104#define NFC_V3_LAUNCH (host->regs_axi + 0x40)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200105
Sascha Hauer71ec5152010-08-06 15:53:11 +0200106#define NFC_V3_WRPROT (host->regs_ip + 0x0)
107#define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
108#define NFC_V3_WRPROT_LOCK (1 << 1)
109#define NFC_V3_WRPROT_UNLOCK (1 << 2)
110#define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
111
112#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
113
114#define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
115#define NFC_V3_CONFIG2_PS_512 (0 << 0)
116#define NFC_V3_CONFIG2_PS_2048 (1 << 0)
117#define NFC_V3_CONFIG2_PS_4096 (2 << 0)
118#define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
119#define NFC_V3_CONFIG2_ECC_EN (1 << 3)
120#define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
121#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
122#define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
123#define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7)
124#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
125#define NFC_V3_CONFIG2_INT_MSK (1 << 15)
126#define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
127#define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
128
129#define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
130#define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
131#define NFC_V3_CONFIG3_FW8 (1 << 3)
132#define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
133#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
134#define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
135#define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
136
137#define NFC_V3_IPC (host->regs_ip + 0x2C)
138#define NFC_V3_IPC_CREQ (1 << 0)
139#define NFC_V3_IPC_INT (1 << 31)
140
141#define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200142
143struct mxc_nand_host {
144 struct mtd_info mtd;
145 struct nand_chip nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200146 struct device *dev;
147
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200148 void *spare0;
149 void *main_area0;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200150
151 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200152 void __iomem *regs;
Sascha Hauer71ec5152010-08-06 15:53:11 +0200153 void __iomem *regs_axi;
154 void __iomem *regs_ip;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200155 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200156 struct clk *clk;
157 int clk_act;
158 int irq;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200159 int eccsize;
Baruch Siachd178e3e2011-03-14 09:01:56 +0200160 int active_cs;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200161
Sascha Hauer63f14742010-10-18 10:16:26 +0200162 struct completion op_completion;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200163
164 uint8_t *data_buf;
165 unsigned int buf_start;
166 int spare_len;
Sascha Hauer5f973042010-08-06 15:53:06 +0200167
168 void (*preset)(struct mtd_info *);
169 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
170 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
171 void (*send_page)(struct mtd_info *, unsigned int);
172 void (*send_read_id)(struct mxc_nand_host *);
173 uint16_t (*get_dev_status)(struct mxc_nand_host *);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200174 int (*check_int)(struct mxc_nand_host *);
Sascha Hauer63f14742010-10-18 10:16:26 +0200175 void (*irq_control)(struct mxc_nand_host *, int);
Uwe Kleine-König85569582012-04-23 11:23:34 +0200176
177 /*
178 * On i.MX21 the CONFIG2:INT bit cannot be read if interrupts are masked
179 * (CONFIG1:INT_MSK is set). To handle this the driver uses
180 * enable_irq/disable_irq_nosync instead of CONFIG1:INT_MSK
181 */
182 int irqpending_quirk;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200183};
184
Sascha Hauer34f6e152008-09-02 17:16:59 +0200185/* OOB placement block for use with hardware ecc generation */
Sascha Hauer94671142009-10-05 12:14:21 +0200186static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200187 .eccbytes = 5,
188 .eccpos = {6, 7, 8, 9, 10},
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200189 .oobfree = {{0, 5}, {12, 4}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200190};
191
Sascha Hauer94671142009-10-05 12:14:21 +0200192static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400193 .eccbytes = 20,
194 .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
195 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
196 .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200197};
198
Sascha Hauer94671142009-10-05 12:14:21 +0200199/* OOB description for 512 byte pages with 16 byte OOB */
200static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
201 .eccbytes = 1 * 9,
202 .eccpos = {
203 7, 8, 9, 10, 11, 12, 13, 14, 15
204 },
205 .oobfree = {
206 {.offset = 0, .length = 5}
207 }
208};
209
210/* OOB description for 2048 byte pages with 64 byte OOB */
211static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
212 .eccbytes = 4 * 9,
213 .eccpos = {
214 7, 8, 9, 10, 11, 12, 13, 14, 15,
215 23, 24, 25, 26, 27, 28, 29, 30, 31,
216 39, 40, 41, 42, 43, 44, 45, 46, 47,
217 55, 56, 57, 58, 59, 60, 61, 62, 63
218 },
219 .oobfree = {
220 {.offset = 2, .length = 4},
221 {.offset = 16, .length = 7},
222 {.offset = 32, .length = 7},
223 {.offset = 48, .length = 7}
224 }
225};
226
Baruch Siach2c1c5f12011-03-09 16:12:20 +0200227/* OOB description for 4096 byte pages with 128 byte OOB */
228static struct nand_ecclayout nandv2_hw_eccoob_4k = {
229 .eccbytes = 8 * 9,
230 .eccpos = {
231 7, 8, 9, 10, 11, 12, 13, 14, 15,
232 23, 24, 25, 26, 27, 28, 29, 30, 31,
233 39, 40, 41, 42, 43, 44, 45, 46, 47,
234 55, 56, 57, 58, 59, 60, 61, 62, 63,
235 71, 72, 73, 74, 75, 76, 77, 78, 79,
236 87, 88, 89, 90, 91, 92, 93, 94, 95,
237 103, 104, 105, 106, 107, 108, 109, 110, 111,
238 119, 120, 121, 122, 123, 124, 125, 126, 127,
239 },
240 .oobfree = {
241 {.offset = 2, .length = 4},
242 {.offset = 16, .length = 7},
243 {.offset = 32, .length = 7},
244 {.offset = 48, .length = 7},
245 {.offset = 64, .length = 7},
246 {.offset = 80, .length = 7},
247 {.offset = 96, .length = 7},
248 {.offset = 112, .length = 7},
249 }
250};
251
Sascha Hauer34f6e152008-09-02 17:16:59 +0200252static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
Sascha Hauer34f6e152008-09-02 17:16:59 +0200253
Sascha Hauer71ec5152010-08-06 15:53:11 +0200254static int check_int_v3(struct mxc_nand_host *host)
255{
256 uint32_t tmp;
257
258 tmp = readl(NFC_V3_IPC);
259 if (!(tmp & NFC_V3_IPC_INT))
260 return 0;
261
262 tmp &= ~NFC_V3_IPC_INT;
263 writel(tmp, NFC_V3_IPC);
264
265 return 1;
266}
267
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200268static int check_int_v1_v2(struct mxc_nand_host *host)
269{
270 uint32_t tmp;
271
Sascha Hauer1bc99182010-08-06 15:53:08 +0200272 tmp = readw(NFC_V1_V2_CONFIG2);
273 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200274 return 0;
275
Uwe Kleine-König85569582012-04-23 11:23:34 +0200276 if (!host->irqpending_quirk)
Sascha Hauer63f14742010-10-18 10:16:26 +0200277 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200278
279 return 1;
280}
281
Sascha Hauer63f14742010-10-18 10:16:26 +0200282static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
283{
284 uint16_t tmp;
285
286 tmp = readw(NFC_V1_V2_CONFIG1);
287
288 if (activate)
289 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
290 else
291 tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
292
293 writew(tmp, NFC_V1_V2_CONFIG1);
294}
295
296static void irq_control_v3(struct mxc_nand_host *host, int activate)
297{
298 uint32_t tmp;
299
300 tmp = readl(NFC_V3_CONFIG2);
301
302 if (activate)
303 tmp &= ~NFC_V3_CONFIG2_INT_MSK;
304 else
305 tmp |= NFC_V3_CONFIG2_INT_MSK;
306
307 writel(tmp, NFC_V3_CONFIG2);
308}
309
Uwe Kleine-König85569582012-04-23 11:23:34 +0200310static void irq_control(struct mxc_nand_host *host, int activate)
311{
312 if (host->irqpending_quirk) {
313 if (activate)
314 enable_irq(host->irq);
315 else
316 disable_irq_nosync(host->irq);
317 } else {
318 host->irq_control(host, activate);
319 }
320}
321
322static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
323{
324 struct mxc_nand_host *host = dev_id;
325
326 if (!host->check_int(host))
327 return IRQ_NONE;
328
329 irq_control(host, 0);
330
331 complete(&host->op_completion);
332
333 return IRQ_HANDLED;
334}
335
Sascha Hauer34f6e152008-09-02 17:16:59 +0200336/* This function polls the NANDFC to wait for the basic operation to
337 * complete by checking the INT bit of config2 register.
338 */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200339static void wait_op_done(struct mxc_nand_host *host, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200340{
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200341 int max_retries = 8000;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200342
343 if (useirq) {
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200344 if (!host->check_int(host)) {
Sascha Hauer63f14742010-10-18 10:16:26 +0200345 INIT_COMPLETION(host->op_completion);
Uwe Kleine-König85569582012-04-23 11:23:34 +0200346 irq_control(host, 1);
Sascha Hauer63f14742010-10-18 10:16:26 +0200347 wait_for_completion(&host->op_completion);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200348 }
349 } else {
350 while (max_retries-- > 0) {
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200351 if (host->check_int(host))
Sascha Hauer34f6e152008-09-02 17:16:59 +0200352 break;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200353
Sascha Hauer34f6e152008-09-02 17:16:59 +0200354 udelay(1);
355 }
Roel Kluin43950a62009-06-04 16:24:59 +0200356 if (max_retries < 0)
Brian Norris0a32a102011-07-19 10:06:10 -0700357 pr_debug("%s: INT not set\n", __func__);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200358 }
359}
360
Sascha Hauer71ec5152010-08-06 15:53:11 +0200361static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
362{
363 /* fill command */
364 writel(cmd, NFC_V3_FLASH_CMD);
365
366 /* send out command */
367 writel(NFC_CMD, NFC_V3_LAUNCH);
368
369 /* Wait for operation to complete */
370 wait_op_done(host, useirq);
371}
372
Sascha Hauer34f6e152008-09-02 17:16:59 +0200373/* This function issues the specified command to the NAND device and
374 * waits for completion. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200375static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200376{
Brian Norris289c0522011-07-19 10:06:09 -0700377 pr_debug("send_cmd(host, 0x%x, %d)\n", cmd, useirq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200378
Sascha Hauer1bc99182010-08-06 15:53:08 +0200379 writew(cmd, NFC_V1_V2_FLASH_CMD);
380 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200381
Uwe Kleine-König85569582012-04-23 11:23:34 +0200382 if (host->irqpending_quirk && (cmd == NAND_CMD_RESET)) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200383 int max_retries = 100;
384 /* Reset completion is indicated by NFC_CONFIG2 */
385 /* being set to 0 */
386 while (max_retries-- > 0) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200387 if (readw(NFC_V1_V2_CONFIG2) == 0) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200388 break;
389 }
390 udelay(1);
391 }
392 if (max_retries < 0)
Brian Norris0a32a102011-07-19 10:06:10 -0700393 pr_debug("%s: RESET failed\n", __func__);
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200394 } else {
395 /* Wait for operation to complete */
396 wait_op_done(host, useirq);
397 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200398}
399
Sascha Hauer71ec5152010-08-06 15:53:11 +0200400static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
401{
402 /* fill address */
403 writel(addr, NFC_V3_FLASH_ADDR0);
404
405 /* send out address */
406 writel(NFC_ADDR, NFC_V3_LAUNCH);
407
408 wait_op_done(host, 0);
409}
410
Sascha Hauer34f6e152008-09-02 17:16:59 +0200411/* This function sends an address (or partial address) to the
412 * NAND device. The address is used to select the source/destination for
413 * a NAND command. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200414static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200415{
Brian Norris289c0522011-07-19 10:06:09 -0700416 pr_debug("send_addr(host, 0x%x %d)\n", addr, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200417
Sascha Hauer1bc99182010-08-06 15:53:08 +0200418 writew(addr, NFC_V1_V2_FLASH_ADDR);
419 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200420
421 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200422 wait_op_done(host, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200423}
424
Sascha Hauer71ec5152010-08-06 15:53:11 +0200425static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
426{
427 struct nand_chip *nand_chip = mtd->priv;
428 struct mxc_nand_host *host = nand_chip->priv;
429 uint32_t tmp;
430
431 tmp = readl(NFC_V3_CONFIG1);
432 tmp &= ~(7 << 4);
433 writel(tmp, NFC_V3_CONFIG1);
434
435 /* transfer data from NFC ram to nand */
436 writel(ops, NFC_V3_LAUNCH);
437
438 wait_op_done(host, false);
439}
440
Sascha Hauer5f973042010-08-06 15:53:06 +0200441static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200442{
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200443 struct nand_chip *nand_chip = mtd->priv;
444 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200445 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200446
Sascha Hauer94671142009-10-05 12:14:21 +0200447 if (nfc_is_v1() && mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200448 bufs = 4;
449 else
450 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200451
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200452 for (i = 0; i < bufs; i++) {
453
454 /* NANDFC buffer 0 is used for page read/write */
Baruch Siachd178e3e2011-03-14 09:01:56 +0200455 writew((host->active_cs << 4) | i, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200456
Sascha Hauer1bc99182010-08-06 15:53:08 +0200457 writew(ops, NFC_V1_V2_CONFIG2);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200458
459 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200460 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200461 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200462}
463
Sascha Hauer71ec5152010-08-06 15:53:11 +0200464static void send_read_id_v3(struct mxc_nand_host *host)
465{
466 /* Read ID into main buffer */
467 writel(NFC_ID, NFC_V3_LAUNCH);
468
469 wait_op_done(host, true);
470
471 memcpy(host->data_buf, host->main_area0, 16);
472}
473
Sascha Hauer34f6e152008-09-02 17:16:59 +0200474/* Request the NANDFC to perform a read of the NAND device ID. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200475static void send_read_id_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200476{
477 struct nand_chip *this = &host->nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200478
479 /* NANDFC buffer 0 is used for device ID output */
Baruch Siachd178e3e2011-03-14 09:01:56 +0200480 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200481
Sascha Hauer1bc99182010-08-06 15:53:08 +0200482 writew(NFC_ID, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200483
484 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200485 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200486
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200487 memcpy(host->data_buf, host->main_area0, 16);
John Ognessf7b66e52010-06-18 18:59:47 +0200488
489 if (this->options & NAND_BUSWIDTH_16) {
490 /* compress the ID info */
491 host->data_buf[1] = host->data_buf[2];
492 host->data_buf[2] = host->data_buf[4];
493 host->data_buf[3] = host->data_buf[6];
494 host->data_buf[4] = host->data_buf[8];
495 host->data_buf[5] = host->data_buf[10];
496 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200497}
498
Sascha Hauer71ec5152010-08-06 15:53:11 +0200499static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200500{
Sascha Hauer71ec5152010-08-06 15:53:11 +0200501 writew(NFC_STATUS, NFC_V3_LAUNCH);
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200502 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200503
Sascha Hauer71ec5152010-08-06 15:53:11 +0200504 return readl(NFC_V3_CONFIG1) >> 16;
505}
506
Sascha Hauer34f6e152008-09-02 17:16:59 +0200507/* This function requests the NANDFC to perform a read of the
508 * NAND device status and returns the current status. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200509static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200510{
Sascha Hauerc29c6072010-08-06 15:53:05 +0200511 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200512 uint32_t store;
513 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200514
Baruch Siachd178e3e2011-03-14 09:01:56 +0200515 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200516
517 /*
518 * The device status is stored in main_area0. To
519 * prevent corruption of the buffer save the value
520 * and restore it afterwards.
521 */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200522 store = readl(main_buf);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200523
Sascha Hauer1bc99182010-08-06 15:53:08 +0200524 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200525 wait_op_done(host, true);
526
Sascha Hauer34f6e152008-09-02 17:16:59 +0200527 ret = readw(main_buf);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200528
Sascha Hauer34f6e152008-09-02 17:16:59 +0200529 writel(store, main_buf);
530
531 return ret;
532}
533
534/* This functions is used by upper layer to checks if device is ready */
535static int mxc_nand_dev_ready(struct mtd_info *mtd)
536{
537 /*
538 * NFC handles R/B internally. Therefore, this function
539 * always returns status as ready.
540 */
541 return 1;
542}
543
544static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
545{
546 /*
547 * If HW ECC is enabled, we turn it on during init. There is
548 * no need to enable again here.
549 */
550}
551
Sascha Hauer94f77e52010-08-06 15:53:09 +0200552static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
Sascha Hauer34f6e152008-09-02 17:16:59 +0200553 u_char *read_ecc, u_char *calc_ecc)
554{
555 struct nand_chip *nand_chip = mtd->priv;
556 struct mxc_nand_host *host = nand_chip->priv;
557
558 /*
559 * 1-Bit errors are automatically corrected in HW. No need for
560 * additional correction. 2-Bit errors cannot be corrected by
561 * HW ECC, so we need to return failure
562 */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200563 uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200564
565 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
Brian Norris289c0522011-07-19 10:06:09 -0700566 pr_debug("MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
Sascha Hauer34f6e152008-09-02 17:16:59 +0200567 return -1;
568 }
569
570 return 0;
571}
572
Sascha Hauer94f77e52010-08-06 15:53:09 +0200573static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
574 u_char *read_ecc, u_char *calc_ecc)
575{
576 struct nand_chip *nand_chip = mtd->priv;
577 struct mxc_nand_host *host = nand_chip->priv;
578 u32 ecc_stat, err;
579 int no_subpages = 1;
580 int ret = 0;
581 u8 ecc_bit_mask, err_limit;
582
583 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
584 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
585
586 no_subpages = mtd->writesize >> 9;
587
Sascha Hauer71ec5152010-08-06 15:53:11 +0200588 if (nfc_is_v21())
589 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
590 else
591 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200592
593 do {
594 err = ecc_stat & ecc_bit_mask;
595 if (err > err_limit) {
596 printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
597 return -1;
598 } else {
599 ret += err;
600 }
601 ecc_stat >>= 4;
602 } while (--no_subpages);
603
604 mtd->ecc_stats.corrected += ret;
605 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
606
607 return ret;
608}
609
Sascha Hauer34f6e152008-09-02 17:16:59 +0200610static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
611 u_char *ecc_code)
612{
613 return 0;
614}
615
616static u_char mxc_nand_read_byte(struct mtd_info *mtd)
617{
618 struct nand_chip *nand_chip = mtd->priv;
619 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200620 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200621
622 /* Check for status request */
623 if (host->status_request)
Sascha Hauer5f973042010-08-06 15:53:06 +0200624 return host->get_dev_status(host) & 0xFF;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200625
Sascha Hauerf8f96082009-06-04 17:12:26 +0200626 ret = *(uint8_t *)(host->data_buf + host->buf_start);
627 host->buf_start++;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200628
629 return ret;
630}
631
632static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
633{
634 struct nand_chip *nand_chip = mtd->priv;
635 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200636 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200637
Sascha Hauerf8f96082009-06-04 17:12:26 +0200638 ret = *(uint16_t *)(host->data_buf + host->buf_start);
639 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200640
641 return ret;
642}
643
644/* Write data of length len to buffer buf. The data to be
645 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
646 * Operation by the NFC, the data is written to NAND Flash */
647static void mxc_nand_write_buf(struct mtd_info *mtd,
648 const u_char *buf, int len)
649{
650 struct nand_chip *nand_chip = mtd->priv;
651 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200652 u16 col = host->buf_start;
653 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200654
Sascha Hauerf8f96082009-06-04 17:12:26 +0200655 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200656
Sascha Hauerf8f96082009-06-04 17:12:26 +0200657 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200658
Sascha Hauerf8f96082009-06-04 17:12:26 +0200659 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200660}
661
662/* Read the data buffer from the NAND Flash. To read the data from NAND
663 * Flash first the data output cycle is initiated by the NFC, which copies
664 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
665 */
666static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
667{
668 struct nand_chip *nand_chip = mtd->priv;
669 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200670 u16 col = host->buf_start;
671 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200672
Sascha Hauerf8f96082009-06-04 17:12:26 +0200673 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200674
Baruch Siach5d9d9932011-03-02 16:47:55 +0200675 memcpy(buf, host->data_buf + col, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200676
Baruch Siach5d9d9932011-03-02 16:47:55 +0200677 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200678}
679
680/* Used by the upper layer to verify the data in NAND Flash
681 * with the data in the buf. */
682static int mxc_nand_verify_buf(struct mtd_info *mtd,
683 const u_char *buf, int len)
684{
685 return -EFAULT;
686}
687
688/* This function is used by upper layer for select and
689 * deselect of the NAND chip */
690static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
691{
692 struct nand_chip *nand_chip = mtd->priv;
693 struct mxc_nand_host *host = nand_chip->priv;
694
Baruch Siachd178e3e2011-03-14 09:01:56 +0200695 if (chip == -1) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200696 /* Disable the NFC clock */
697 if (host->clk_act) {
698 clk_disable(host->clk);
699 host->clk_act = 0;
700 }
Baruch Siachd178e3e2011-03-14 09:01:56 +0200701 return;
702 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200703
Baruch Siachd178e3e2011-03-14 09:01:56 +0200704 if (!host->clk_act) {
705 /* Enable the NFC clock */
706 clk_enable(host->clk);
707 host->clk_act = 1;
708 }
709
710 if (nfc_is_v21()) {
711 host->active_cs = chip;
712 writew(host->active_cs << 4, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200713 }
714}
715
Sascha Hauerf8f96082009-06-04 17:12:26 +0200716/*
717 * Function to transfer data to/from spare area.
718 */
719static void copy_spare(struct mtd_info *mtd, bool bfrom)
720{
721 struct nand_chip *this = mtd->priv;
722 struct mxc_nand_host *host = this->priv;
723 u16 i, j;
724 u16 n = mtd->writesize >> 9;
725 u8 *d = host->data_buf + mtd->writesize;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200726 u8 *s = host->spare0;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200727 u16 t = host->spare_len;
728
729 j = (mtd->oobsize / n >> 1) << 1;
730
731 if (bfrom) {
732 for (i = 0; i < n - 1; i++)
733 memcpy(d + i * j, s + i * t, j);
734
735 /* the last section */
736 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
737 } else {
738 for (i = 0; i < n - 1; i++)
739 memcpy(&s[i * t], &d[i * j], j);
740
741 /* the last section */
742 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
743 }
744}
745
Sascha Hauera3e65b62009-06-02 11:47:59 +0200746static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200747{
748 struct nand_chip *nand_chip = mtd->priv;
749 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200750
751 /* Write out column address, if necessary */
752 if (column != -1) {
753 /*
754 * MXC NANDFC can only perform full page+spare or
755 * spare-only read/write. When the upper layers
Gilles Espinasse177b2412011-01-09 08:59:49 +0100756 * perform a read/write buf operation, the saved column
757 * address is used to index into the full page.
Sascha Hauer34f6e152008-09-02 17:16:59 +0200758 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200759 host->send_addr(host, 0, page_addr == -1);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200760 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200761 /* another col addr cycle for 2k page */
Sascha Hauer5f973042010-08-06 15:53:06 +0200762 host->send_addr(host, 0, false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200763 }
764
765 /* Write out page address, if necessary */
766 if (page_addr != -1) {
767 /* paddr_0 - p_addr_7 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200768 host->send_addr(host, (page_addr & 0xff), false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200769
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200770 if (mtd->writesize > 512) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400771 if (mtd->size >= 0x10000000) {
772 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200773 host->send_addr(host, (page_addr >> 8) & 0xff, false);
774 host->send_addr(host, (page_addr >> 16) & 0xff, true);
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400775 } else
776 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200777 host->send_addr(host, (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200778 } else {
779 /* One more address cycle for higher density devices */
780 if (mtd->size >= 0x4000000) {
781 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200782 host->send_addr(host, (page_addr >> 8) & 0xff, false);
783 host->send_addr(host, (page_addr >> 16) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200784 } else
785 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200786 host->send_addr(host, (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200787 }
788 }
Sascha Hauera3e65b62009-06-02 11:47:59 +0200789}
Sascha Hauer34f6e152008-09-02 17:16:59 +0200790
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200791/*
792 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
793 * on how much oob the nand chip has. For 8bit ecc we need at least
794 * 26 bytes of oob data per 512 byte block.
795 */
796static int get_eccsize(struct mtd_info *mtd)
797{
798 int oobbytes_per_512 = 0;
799
800 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
801
802 if (oobbytes_per_512 < 26)
803 return 4;
804 else
805 return 8;
806}
807
Sascha Hauer5f973042010-08-06 15:53:06 +0200808static void preset_v1_v2(struct mtd_info *mtd)
Ivo Claryssed4840182010-04-08 16:14:44 +0200809{
810 struct nand_chip *nand_chip = mtd->priv;
811 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200812 uint16_t config1 = 0;
Ivo Claryssed4840182010-04-08 16:14:44 +0200813
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200814 if (nand_chip->ecc.mode == NAND_ECC_HW)
815 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
816
817 if (nfc_is_v21())
818 config1 |= NFC_V2_CONFIG1_FP_INT;
819
Uwe Kleine-König85569582012-04-23 11:23:34 +0200820 if (!host->irqpending_quirk)
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200821 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200822
823 if (nfc_is_v21() && mtd->writesize) {
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200824 uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
825
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200826 host->eccsize = get_eccsize(mtd);
827 if (host->eccsize == 4)
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200828 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
829
830 config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200831 } else {
832 host->eccsize = 1;
833 }
834
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200835 writew(config1, NFC_V1_V2_CONFIG1);
Ivo Claryssed4840182010-04-08 16:14:44 +0200836 /* preset operation */
837
838 /* Unlock the internal RAM Buffer */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200839 writew(0x2, NFC_V1_V2_CONFIG);
Ivo Claryssed4840182010-04-08 16:14:44 +0200840
841 /* Blocks to be unlocked */
842 if (nfc_is_v21()) {
Baruch Siachd178e3e2011-03-14 09:01:56 +0200843 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR0);
844 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR1);
845 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR2);
846 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR3);
847 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR0);
848 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR1);
849 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR2);
850 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR3);
Ivo Claryssed4840182010-04-08 16:14:44 +0200851 } else if (nfc_is_v1()) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200852 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
Wolfram Stering5172ac12011-09-23 13:53:44 +0200853 writew(0xffff, NFC_V1_UNLOCKEND_BLKADDR);
Ivo Claryssed4840182010-04-08 16:14:44 +0200854 } else
855 BUG();
856
857 /* Unlock Block Command for given address range */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200858 writew(0x4, NFC_V1_V2_WRPROT);
Ivo Claryssed4840182010-04-08 16:14:44 +0200859}
860
Sascha Hauer71ec5152010-08-06 15:53:11 +0200861static void preset_v3(struct mtd_info *mtd)
862{
863 struct nand_chip *chip = mtd->priv;
864 struct mxc_nand_host *host = chip->priv;
865 uint32_t config2, config3;
866 int i, addr_phases;
867
868 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
869 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
870
871 /* Unlock the internal RAM Buffer */
872 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
873 NFC_V3_WRPROT);
874
875 /* Blocks to be unlocked */
876 for (i = 0; i < NAND_MAX_CHIPS; i++)
877 writel(0x0 | (0xffff << 16),
878 NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
879
880 writel(0, NFC_V3_IPC);
881
882 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
883 NFC_V3_CONFIG2_2CMD_PHASES |
884 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
885 NFC_V3_CONFIG2_ST_CMD(0x70) |
Sascha Hauer63f14742010-10-18 10:16:26 +0200886 NFC_V3_CONFIG2_INT_MSK |
Sascha Hauer71ec5152010-08-06 15:53:11 +0200887 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
888
889 if (chip->ecc.mode == NAND_ECC_HW)
890 config2 |= NFC_V3_CONFIG2_ECC_EN;
891
892 addr_phases = fls(chip->pagemask) >> 3;
893
894 if (mtd->writesize == 2048) {
895 config2 |= NFC_V3_CONFIG2_PS_2048;
896 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
897 } else if (mtd->writesize == 4096) {
898 config2 |= NFC_V3_CONFIG2_PS_4096;
899 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
900 } else {
901 config2 |= NFC_V3_CONFIG2_PS_512;
902 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
903 }
904
905 if (mtd->writesize) {
906 config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
907 host->eccsize = get_eccsize(mtd);
908 if (host->eccsize == 8)
909 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
910 }
911
912 writel(config2, NFC_V3_CONFIG2);
913
914 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
915 NFC_V3_CONFIG3_NO_SDMA |
916 NFC_V3_CONFIG3_RBB_MODE |
917 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
918 NFC_V3_CONFIG3_ADD_OP(0);
919
920 if (!(chip->options & NAND_BUSWIDTH_16))
921 config3 |= NFC_V3_CONFIG3_FW8;
922
923 writel(config3, NFC_V3_CONFIG3);
924
925 writel(0, NFC_V3_DELAY_LINE);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200926}
927
Sascha Hauer34f6e152008-09-02 17:16:59 +0200928/* Used by the upper layer to write command to NAND Flash for
929 * different operations to be carried out on NAND Flash */
930static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
931 int column, int page_addr)
932{
933 struct nand_chip *nand_chip = mtd->priv;
934 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200935
Brian Norris289c0522011-07-19 10:06:09 -0700936 pr_debug("mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
Sascha Hauer34f6e152008-09-02 17:16:59 +0200937 command, column, page_addr);
938
939 /* Reset command state information */
940 host->status_request = false;
941
942 /* Command pre-processing step */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200943 switch (command) {
Ivo Claryssed4840182010-04-08 16:14:44 +0200944 case NAND_CMD_RESET:
Sascha Hauer5f973042010-08-06 15:53:06 +0200945 host->preset(mtd);
946 host->send_cmd(host, command, false);
Ivo Claryssed4840182010-04-08 16:14:44 +0200947 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200948
Sascha Hauer34f6e152008-09-02 17:16:59 +0200949 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200950 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200951 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +0200952
Sascha Hauer5f973042010-08-06 15:53:06 +0200953 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200954 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200955 break;
956
Sascha Hauer34f6e152008-09-02 17:16:59 +0200957 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200958 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +0200959 if (command == NAND_CMD_READ0)
960 host->buf_start = column;
961 else
962 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200963
Sascha Hauer5ea32022010-04-27 15:24:01 +0200964 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +0200965
Sascha Hauer5f973042010-08-06 15:53:06 +0200966 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200967 mxc_do_addr_cycle(mtd, column, page_addr);
968
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200969 if (mtd->writesize > 512)
Sascha Hauer5f973042010-08-06 15:53:06 +0200970 host->send_cmd(host, NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200971
Sascha Hauer5f973042010-08-06 15:53:06 +0200972 host->send_page(mtd, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200973
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200974 memcpy(host->data_buf, host->main_area0, mtd->writesize);
Sascha Hauer89121a62009-06-04 17:18:01 +0200975 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200976 break;
977
Sascha Hauer34f6e152008-09-02 17:16:59 +0200978 case NAND_CMD_SEQIN:
Sascha Hauer5ea32022010-04-27 15:24:01 +0200979 if (column >= mtd->writesize)
980 /* call ourself to read a page */
981 mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200982
Sascha Hauer5ea32022010-04-27 15:24:01 +0200983 host->buf_start = column;
Sascha Hauer89121a62009-06-04 17:18:01 +0200984
Sascha Hauer5f973042010-08-06 15:53:06 +0200985 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200986 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200987 break;
988
989 case NAND_CMD_PAGEPROG:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200990 memcpy(host->main_area0, host->data_buf, mtd->writesize);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200991 copy_spare(mtd, false);
Sascha Hauer5f973042010-08-06 15:53:06 +0200992 host->send_page(mtd, NFC_INPUT);
993 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200994 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200995 break;
996
Sascha Hauer34f6e152008-09-02 17:16:59 +0200997 case NAND_CMD_READID:
Sascha Hauer5f973042010-08-06 15:53:06 +0200998 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200999 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer5f973042010-08-06 15:53:06 +02001000 host->send_read_id(host);
Sascha Hauer94671142009-10-05 12:14:21 +02001001 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001002 break;
1003
Sascha Hauer89121a62009-06-04 17:18:01 +02001004 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +02001005 case NAND_CMD_ERASE2:
Sascha Hauer5f973042010-08-06 15:53:06 +02001006 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +02001007 mxc_do_addr_cycle(mtd, column, page_addr);
1008
Sascha Hauer34f6e152008-09-02 17:16:59 +02001009 break;
1010 }
1011}
1012
Sascha Hauerf1372052009-10-21 14:25:27 +02001013/*
1014 * The generic flash bbt decriptors overlap with our ecc
1015 * hardware, so define some i.MX specific ones.
1016 */
1017static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
1018static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
1019
1020static struct nand_bbt_descr bbt_main_descr = {
1021 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1022 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1023 .offs = 0,
1024 .len = 4,
1025 .veroffs = 4,
1026 .maxblocks = 4,
1027 .pattern = bbt_pattern,
1028};
1029
1030static struct nand_bbt_descr bbt_mirror_descr = {
1031 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
1032 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
1033 .offs = 0,
1034 .len = 4,
1035 .veroffs = 4,
1036 .maxblocks = 4,
1037 .pattern = mirror_pattern,
1038};
1039
Sascha Hauer34f6e152008-09-02 17:16:59 +02001040static int __init mxcnd_probe(struct platform_device *pdev)
1041{
1042 struct nand_chip *this;
1043 struct mtd_info *mtd;
1044 struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
1045 struct mxc_nand_host *host;
1046 struct resource *res;
Dmitry Eremin-Solenikovd4ed8f12011-06-02 18:00:43 +04001047 int err = 0;
Sascha Hauer94671142009-10-05 12:14:21 +02001048 struct nand_ecclayout *oob_smallpage, *oob_largepage;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001049
1050 /* Allocate memory for MTD device structure and private data */
Sascha Hauerf8f96082009-06-04 17:12:26 +02001051 host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
1052 NAND_MAX_OOBSIZE, GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001053 if (!host)
1054 return -ENOMEM;
1055
Sascha Hauerf8f96082009-06-04 17:12:26 +02001056 host->data_buf = (uint8_t *)(host + 1);
Sascha Hauerf8f96082009-06-04 17:12:26 +02001057
Sascha Hauer34f6e152008-09-02 17:16:59 +02001058 host->dev = &pdev->dev;
1059 /* structures must be linked */
1060 this = &host->nand;
1061 mtd = &host->mtd;
1062 mtd->priv = this;
1063 mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -07001064 mtd->dev.parent = &pdev->dev;
Sascha Hauer1fbff0a2009-10-21 16:06:27 +02001065 mtd->name = DRIVER_NAME;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001066
1067 /* 50 us command delay time */
1068 this->chip_delay = 5;
1069
1070 this->priv = host;
1071 this->dev_ready = mxc_nand_dev_ready;
1072 this->cmdfunc = mxc_nand_command;
1073 this->select_chip = mxc_nand_select_chip;
1074 this->read_byte = mxc_nand_read_byte;
1075 this->read_word = mxc_nand_read_word;
1076 this->write_buf = mxc_nand_write_buf;
1077 this->read_buf = mxc_nand_read_buf;
1078 this->verify_buf = mxc_nand_verify_buf;
1079
Sascha Hauere65fb002009-02-16 14:29:10 +01001080 host->clk = clk_get(&pdev->dev, "nfc");
Vladimir Barinov8541c112009-04-23 15:47:22 +04001081 if (IS_ERR(host->clk)) {
1082 err = PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001083 goto eclk;
Vladimir Barinov8541c112009-04-23 15:47:22 +04001084 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001085
1086 clk_enable(host->clk);
1087 host->clk_act = 1;
1088
1089 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1090 if (!res) {
1091 err = -ENODEV;
1092 goto eres;
1093 }
1094
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001095 host->base = ioremap(res->start, resource_size(res));
1096 if (!host->base) {
Vladimir Barinov8541c112009-04-23 15:47:22 +04001097 err = -ENOMEM;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001098 goto eres;
1099 }
1100
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001101 host->main_area0 = host->base;
Sascha Hauer94671142009-10-05 12:14:21 +02001102
Sascha Hauer5f973042010-08-06 15:53:06 +02001103 if (nfc_is_v1() || nfc_is_v21()) {
1104 host->preset = preset_v1_v2;
1105 host->send_cmd = send_cmd_v1_v2;
1106 host->send_addr = send_addr_v1_v2;
1107 host->send_page = send_page_v1_v2;
1108 host->send_read_id = send_read_id_v1_v2;
1109 host->get_dev_status = get_dev_status_v1_v2;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +02001110 host->check_int = check_int_v1_v2;
Uwe Kleine-König85569582012-04-23 11:23:34 +02001111 host->irq_control = irq_control_v1_v2;
Sascha Hauer63f14742010-10-18 10:16:26 +02001112 if (cpu_is_mx21())
Uwe Kleine-König85569582012-04-23 11:23:34 +02001113 host->irqpending_quirk = 1;
Sascha Hauer5f973042010-08-06 15:53:06 +02001114 }
Sascha Hauer94671142009-10-05 12:14:21 +02001115
1116 if (nfc_is_v21()) {
Sascha Hauer938cf992010-08-06 15:53:04 +02001117 host->regs = host->base + 0x1e00;
Sascha Hauer94671142009-10-05 12:14:21 +02001118 host->spare0 = host->base + 0x1000;
1119 host->spare_len = 64;
1120 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1121 oob_largepage = &nandv2_hw_eccoob_largepage;
Ivo Claryssed4840182010-04-08 16:14:44 +02001122 this->ecc.bytes = 9;
Sascha Hauer94671142009-10-05 12:14:21 +02001123 } else if (nfc_is_v1()) {
Sascha Hauer938cf992010-08-06 15:53:04 +02001124 host->regs = host->base + 0xe00;
Sascha Hauer94671142009-10-05 12:14:21 +02001125 host->spare0 = host->base + 0x800;
1126 host->spare_len = 16;
1127 oob_smallpage = &nandv1_hw_eccoob_smallpage;
1128 oob_largepage = &nandv1_hw_eccoob_largepage;
Sascha Hauer94671142009-10-05 12:14:21 +02001129 this->ecc.bytes = 3;
Sascha Hauer71ec5152010-08-06 15:53:11 +02001130 host->eccsize = 1;
1131 } else if (nfc_is_v3_2()) {
1132 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1133 if (!res) {
1134 err = -ENODEV;
1135 goto eirq;
1136 }
1137 host->regs_ip = ioremap(res->start, resource_size(res));
1138 if (!host->regs_ip) {
1139 err = -ENOMEM;
1140 goto eirq;
1141 }
1142 host->regs_axi = host->base + 0x1e00;
1143 host->spare0 = host->base + 0x1000;
1144 host->spare_len = 64;
1145 host->preset = preset_v3;
1146 host->send_cmd = send_cmd_v3;
1147 host->send_addr = send_addr_v3;
1148 host->send_page = send_page_v3;
1149 host->send_read_id = send_read_id_v3;
1150 host->check_int = check_int_v3;
1151 host->get_dev_status = get_dev_status_v3;
Sascha Hauer63f14742010-10-18 10:16:26 +02001152 host->irq_control = irq_control_v3;
Sascha Hauer71ec5152010-08-06 15:53:11 +02001153 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1154 oob_largepage = &nandv2_hw_eccoob_largepage;
Sascha Hauer94671142009-10-05 12:14:21 +02001155 } else
1156 BUG();
Sascha Hauer34f6e152008-09-02 17:16:59 +02001157
Sascha Hauer13e1add2009-10-21 10:39:05 +02001158 this->ecc.size = 512;
Sascha Hauer94671142009-10-05 12:14:21 +02001159 this->ecc.layout = oob_smallpage;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001160
1161 if (pdata->hw_ecc) {
1162 this->ecc.calculate = mxc_nand_calculate_ecc;
1163 this->ecc.hwctl = mxc_nand_enable_hwecc;
Sascha Hauer94f77e52010-08-06 15:53:09 +02001164 if (nfc_is_v1())
1165 this->ecc.correct = mxc_nand_correct_data_v1;
1166 else
1167 this->ecc.correct = mxc_nand_correct_data_v2_v3;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001168 this->ecc.mode = NAND_ECC_HW;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001169 } else {
1170 this->ecc.mode = NAND_ECC_SOFT;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001171 }
1172
Sascha Hauer34f6e152008-09-02 17:16:59 +02001173 /* NAND bus width determines access funtions used by upper layer */
Sascha Hauer13e1add2009-10-21 10:39:05 +02001174 if (pdata->width == 2)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001175 this->options |= NAND_BUSWIDTH_16;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001176
Sascha Hauerf1372052009-10-21 14:25:27 +02001177 if (pdata->flash_bbt) {
1178 this->bbt_td = &bbt_main_descr;
1179 this->bbt_md = &bbt_mirror_descr;
1180 /* update flash based bbt */
Brian Norrisbb9ebd42011-05-31 16:31:23 -07001181 this->bbt_options |= NAND_BBT_USE_FLASH;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001182 }
1183
Sascha Hauer63f14742010-10-18 10:16:26 +02001184 init_completion(&host->op_completion);
Ivo Claryssed4840182010-04-08 16:14:44 +02001185
1186 host->irq = platform_get_irq(pdev, 0);
1187
Sascha Hauer63f14742010-10-18 10:16:26 +02001188 /*
Uwe Kleine-König85569582012-04-23 11:23:34 +02001189 * Use host->irq_control here instead of irq_control because we must not
1190 * disable_irq_nosync without having requested the irq
Sascha Hauer63f14742010-10-18 10:16:26 +02001191 */
Uwe Kleine-König85569582012-04-23 11:23:34 +02001192 host->irq_control(host, 0);
Sascha Hauer63f14742010-10-18 10:16:26 +02001193
Ivo Claryssea47bfd22010-04-08 16:16:51 +02001194 err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
Ivo Claryssed4840182010-04-08 16:14:44 +02001195 if (err)
1196 goto eirq;
1197
Sascha Hauer63f14742010-10-18 10:16:26 +02001198 /*
Uwe Kleine-König85569582012-04-23 11:23:34 +02001199 * Now that we "own" the interrupt make sure the interrupt mask bit is
1200 * cleared on i.MX21. Otherwise we can't read the interrupt status bit
1201 * on this machine.
Sascha Hauer63f14742010-10-18 10:16:26 +02001202 */
Uwe Kleine-König85569582012-04-23 11:23:34 +02001203 if (host->irqpending_quirk) {
1204 disable_irq_nosync(host->irq);
1205 host->irq_control(host, 1);
1206 }
Sascha Hauer63f14742010-10-18 10:16:26 +02001207
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001208 /* first scan to find the device and get the page size */
Baruch Siachd178e3e2011-03-14 09:01:56 +02001209 if (nand_scan_ident(mtd, nfc_is_v21() ? 4 : 1, NULL)) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001210 err = -ENXIO;
1211 goto escan;
1212 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001213
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001214 /* Call preset again, with correct writesize this time */
1215 host->preset(mtd);
1216
Sascha Hauer2d69c7f2009-10-05 11:24:02 +02001217 if (mtd->writesize == 2048)
Sascha Hauer94671142009-10-05 12:14:21 +02001218 this->ecc.layout = oob_largepage;
Baruch Siach2c1c5f12011-03-09 16:12:20 +02001219 if (nfc_is_v21() && mtd->writesize == 4096)
1220 this->ecc.layout = &nandv2_hw_eccoob_4k;
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001221
1222 /* second phase scan */
1223 if (nand_scan_tail(mtd)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +02001224 err = -ENXIO;
1225 goto escan;
1226 }
1227
Mike Dunn6a918ba2012-03-11 14:21:11 -07001228 if (this->ecc.mode == NAND_ECC_HW) {
1229 if (nfc_is_v1())
1230 this->ecc.strength = 1;
1231 else
1232 this->ecc.strength = (host->eccsize == 4) ? 4 : 8;
1233 }
1234
Sascha Hauer34f6e152008-09-02 17:16:59 +02001235 /* Register the partitions */
Artem Bityutskiy42d7fbe2012-03-09 19:24:26 +02001236 mtd_device_parse_register(mtd, part_probes, NULL, pdata->parts,
1237 pdata->nr_parts);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001238
1239 platform_set_drvdata(pdev, host);
1240
1241 return 0;
1242
1243escan:
Magnus Liljab258fd82009-05-08 21:57:47 +02001244 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001245eirq:
Sascha Hauer71ec5152010-08-06 15:53:11 +02001246 if (host->regs_ip)
1247 iounmap(host->regs_ip);
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001248 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001249eres:
1250 clk_put(host->clk);
1251eclk:
1252 kfree(host);
1253
1254 return err;
1255}
1256
Uwe Kleine-König51eeb872009-12-07 09:44:05 +00001257static int __devexit mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001258{
1259 struct mxc_nand_host *host = platform_get_drvdata(pdev);
1260
1261 clk_put(host->clk);
1262
1263 platform_set_drvdata(pdev, NULL);
1264
1265 nand_release(&host->mtd);
Magnus Liljab258fd82009-05-08 21:57:47 +02001266 free_irq(host->irq, host);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001267 if (host->regs_ip)
1268 iounmap(host->regs_ip);
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001269 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001270 kfree(host);
1271
1272 return 0;
1273}
1274
Sascha Hauer34f6e152008-09-02 17:16:59 +02001275static struct platform_driver mxcnd_driver = {
1276 .driver = {
1277 .name = DRIVER_NAME,
Uwe Kleine-König8d1fd162012-04-23 11:23:33 +02001278 .owner = THIS_MODULE,
Eric Bénard04dd0d32010-06-17 20:59:04 +02001279 },
Uwe Kleine-Königdaa0f152009-11-24 22:07:08 +01001280 .remove = __devexit_p(mxcnd_remove),
Sascha Hauer34f6e152008-09-02 17:16:59 +02001281};
1282
1283static int __init mxc_nd_init(void)
1284{
Vladimir Barinov8541c112009-04-23 15:47:22 +04001285 return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001286}
1287
1288static void __exit mxc_nd_cleanup(void)
1289{
1290 /* Unregister the device structure */
1291 platform_driver_unregister(&mxcnd_driver);
1292}
1293
1294module_init(mxc_nd_init);
1295module_exit(mxc_nd_cleanup);
1296
1297MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1298MODULE_DESCRIPTION("MXC NAND MTD driver");
1299MODULE_LICENSE("GPL");