blob: 5ae1d9ee2cf1aa96c28c31ccb77ab476bd255074 [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())
Sascha Hauer71ec5152010-08-06 15:53:11 +020044#define nfc_is_v3_2() cpu_is_mx51()
45#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)
59#define NFC_V21_UNLOCKSTART_BLKADDR (host->regs + 0x20)
60#define NFC_V21_UNLOCKEND_BLKADDR (host->regs + 0x22)
61#define NFC_V1_V2_NF_WRPRST (host->regs + 0x18)
62#define NFC_V1_V2_CONFIG1 (host->regs + 0x1a)
63#define NFC_V1_V2_CONFIG2 (host->regs + 0x1c)
Sascha Hauer34f6e152008-09-02 17:16:59 +020064
Sascha Hauer6e85dfd2010-08-06 15:53:10 +020065#define NFC_V2_CONFIG1_ECC_MODE_4 (1 << 0)
Sascha Hauer1bc99182010-08-06 15:53:08 +020066#define NFC_V1_V2_CONFIG1_SP_EN (1 << 2)
67#define NFC_V1_V2_CONFIG1_ECC_EN (1 << 3)
68#define NFC_V1_V2_CONFIG1_INT_MSK (1 << 4)
69#define NFC_V1_V2_CONFIG1_BIG (1 << 5)
70#define NFC_V1_V2_CONFIG1_RST (1 << 6)
71#define NFC_V1_V2_CONFIG1_CE (1 << 7)
Sascha Hauerb8db2f52010-08-09 15:04:19 +020072#define NFC_V2_CONFIG1_ONE_CYCLE (1 << 8)
73#define NFC_V2_CONFIG1_PPB(x) (((x) & 0x3) << 9)
74#define NFC_V2_CONFIG1_FP_INT (1 << 11)
Sascha Hauer34f6e152008-09-02 17:16:59 +020075
Sascha Hauer1bc99182010-08-06 15:53:08 +020076#define NFC_V1_V2_CONFIG2_INT (1 << 15)
Sascha Hauer34f6e152008-09-02 17:16:59 +020077
Sascha Hauer1bc99182010-08-06 15:53:08 +020078/*
79 * Operation modes for the NFC. Valid for v1, v2 and v3
80 * type controllers.
81 */
82#define NFC_CMD (1 << 0)
83#define NFC_ADDR (1 << 1)
84#define NFC_INPUT (1 << 2)
85#define NFC_OUTPUT (1 << 3)
86#define NFC_ID (1 << 4)
87#define NFC_STATUS (1 << 5)
Sascha Hauer34f6e152008-09-02 17:16:59 +020088
Sascha Hauer71ec5152010-08-06 15:53:11 +020089#define NFC_V3_FLASH_CMD (host->regs_axi + 0x00)
90#define NFC_V3_FLASH_ADDR0 (host->regs_axi + 0x04)
Sascha Hauer34f6e152008-09-02 17:16:59 +020091
Sascha Hauer71ec5152010-08-06 15:53:11 +020092#define NFC_V3_CONFIG1 (host->regs_axi + 0x34)
93#define NFC_V3_CONFIG1_SP_EN (1 << 0)
94#define NFC_V3_CONFIG1_RBA(x) (((x) & 0x7 ) << 4)
Sascha Hauer34f6e152008-09-02 17:16:59 +020095
Sascha Hauer71ec5152010-08-06 15:53:11 +020096#define NFC_V3_ECC_STATUS_RESULT (host->regs_axi + 0x38)
Sascha Hauer34f6e152008-09-02 17:16:59 +020097
Sascha Hauer71ec5152010-08-06 15:53:11 +020098#define NFC_V3_LAUNCH (host->regs_axi + 0x40)
Sascha Hauer34f6e152008-09-02 17:16:59 +020099
Sascha Hauer71ec5152010-08-06 15:53:11 +0200100#define NFC_V3_WRPROT (host->regs_ip + 0x0)
101#define NFC_V3_WRPROT_LOCK_TIGHT (1 << 0)
102#define NFC_V3_WRPROT_LOCK (1 << 1)
103#define NFC_V3_WRPROT_UNLOCK (1 << 2)
104#define NFC_V3_WRPROT_BLS_UNLOCK (2 << 6)
105
106#define NFC_V3_WRPROT_UNLOCK_BLK_ADD0 (host->regs_ip + 0x04)
107
108#define NFC_V3_CONFIG2 (host->regs_ip + 0x24)
109#define NFC_V3_CONFIG2_PS_512 (0 << 0)
110#define NFC_V3_CONFIG2_PS_2048 (1 << 0)
111#define NFC_V3_CONFIG2_PS_4096 (2 << 0)
112#define NFC_V3_CONFIG2_ONE_CYCLE (1 << 2)
113#define NFC_V3_CONFIG2_ECC_EN (1 << 3)
114#define NFC_V3_CONFIG2_2CMD_PHASES (1 << 4)
115#define NFC_V3_CONFIG2_NUM_ADDR_PHASE0 (1 << 5)
116#define NFC_V3_CONFIG2_ECC_MODE_8 (1 << 6)
117#define NFC_V3_CONFIG2_PPB(x) (((x) & 0x3) << 7)
118#define NFC_V3_CONFIG2_NUM_ADDR_PHASE1(x) (((x) & 0x3) << 12)
119#define NFC_V3_CONFIG2_INT_MSK (1 << 15)
120#define NFC_V3_CONFIG2_ST_CMD(x) (((x) & 0xff) << 24)
121#define NFC_V3_CONFIG2_SPAS(x) (((x) & 0xff) << 16)
122
123#define NFC_V3_CONFIG3 (host->regs_ip + 0x28)
124#define NFC_V3_CONFIG3_ADD_OP(x) (((x) & 0x3) << 0)
125#define NFC_V3_CONFIG3_FW8 (1 << 3)
126#define NFC_V3_CONFIG3_SBB(x) (((x) & 0x7) << 8)
127#define NFC_V3_CONFIG3_NUM_OF_DEVICES(x) (((x) & 0x7) << 12)
128#define NFC_V3_CONFIG3_RBB_MODE (1 << 15)
129#define NFC_V3_CONFIG3_NO_SDMA (1 << 20)
130
131#define NFC_V3_IPC (host->regs_ip + 0x2C)
132#define NFC_V3_IPC_CREQ (1 << 0)
133#define NFC_V3_IPC_INT (1 << 31)
134
135#define NFC_V3_DELAY_LINE (host->regs_ip + 0x34)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200136
137struct mxc_nand_host {
138 struct mtd_info mtd;
139 struct nand_chip nand;
140 struct mtd_partition *parts;
141 struct device *dev;
142
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200143 void *spare0;
144 void *main_area0;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200145
146 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200147 void __iomem *regs;
Sascha Hauer71ec5152010-08-06 15:53:11 +0200148 void __iomem *regs_axi;
149 void __iomem *regs_ip;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200150 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200151 struct clk *clk;
152 int clk_act;
153 int irq;
Sascha Hauer94f77e52010-08-06 15:53:09 +0200154 int eccsize;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200155
Sascha Hauer63f14742010-10-18 10:16:26 +0200156 struct completion op_completion;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200157
158 uint8_t *data_buf;
159 unsigned int buf_start;
160 int spare_len;
Sascha Hauer5f973042010-08-06 15:53:06 +0200161
162 void (*preset)(struct mtd_info *);
163 void (*send_cmd)(struct mxc_nand_host *, uint16_t, int);
164 void (*send_addr)(struct mxc_nand_host *, uint16_t, int);
165 void (*send_page)(struct mtd_info *, unsigned int);
166 void (*send_read_id)(struct mxc_nand_host *);
167 uint16_t (*get_dev_status)(struct mxc_nand_host *);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200168 int (*check_int)(struct mxc_nand_host *);
Sascha Hauer63f14742010-10-18 10:16:26 +0200169 void (*irq_control)(struct mxc_nand_host *, int);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200170};
171
Sascha Hauer34f6e152008-09-02 17:16:59 +0200172/* OOB placement block for use with hardware ecc generation */
Sascha Hauer94671142009-10-05 12:14:21 +0200173static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200174 .eccbytes = 5,
175 .eccpos = {6, 7, 8, 9, 10},
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200176 .oobfree = {{0, 5}, {12, 4}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200177};
178
Sascha Hauer94671142009-10-05 12:14:21 +0200179static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400180 .eccbytes = 20,
181 .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
182 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
183 .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200184};
185
Sascha Hauer94671142009-10-05 12:14:21 +0200186/* OOB description for 512 byte pages with 16 byte OOB */
187static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
188 .eccbytes = 1 * 9,
189 .eccpos = {
190 7, 8, 9, 10, 11, 12, 13, 14, 15
191 },
192 .oobfree = {
193 {.offset = 0, .length = 5}
194 }
195};
196
197/* OOB description for 2048 byte pages with 64 byte OOB */
198static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
199 .eccbytes = 4 * 9,
200 .eccpos = {
201 7, 8, 9, 10, 11, 12, 13, 14, 15,
202 23, 24, 25, 26, 27, 28, 29, 30, 31,
203 39, 40, 41, 42, 43, 44, 45, 46, 47,
204 55, 56, 57, 58, 59, 60, 61, 62, 63
205 },
206 .oobfree = {
207 {.offset = 2, .length = 4},
208 {.offset = 16, .length = 7},
209 {.offset = 32, .length = 7},
210 {.offset = 48, .length = 7}
211 }
212};
213
Sascha Hauer34f6e152008-09-02 17:16:59 +0200214#ifdef CONFIG_MTD_PARTITIONS
215static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
216#endif
217
218static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
219{
220 struct mxc_nand_host *host = dev_id;
221
Sascha Hauer63f14742010-10-18 10:16:26 +0200222 if (!host->check_int(host))
223 return IRQ_NONE;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200224
Sascha Hauer63f14742010-10-18 10:16:26 +0200225 host->irq_control(host, 0);
226
227 complete(&host->op_completion);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200228
229 return IRQ_HANDLED;
230}
231
Sascha Hauer71ec5152010-08-06 15:53:11 +0200232static int check_int_v3(struct mxc_nand_host *host)
233{
234 uint32_t tmp;
235
236 tmp = readl(NFC_V3_IPC);
237 if (!(tmp & NFC_V3_IPC_INT))
238 return 0;
239
240 tmp &= ~NFC_V3_IPC_INT;
241 writel(tmp, NFC_V3_IPC);
242
243 return 1;
244}
245
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200246static int check_int_v1_v2(struct mxc_nand_host *host)
247{
248 uint32_t tmp;
249
Sascha Hauer1bc99182010-08-06 15:53:08 +0200250 tmp = readw(NFC_V1_V2_CONFIG2);
251 if (!(tmp & NFC_V1_V2_CONFIG2_INT))
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200252 return 0;
253
Sascha Hauer63f14742010-10-18 10:16:26 +0200254 if (!cpu_is_mx21())
255 writew(tmp & ~NFC_V1_V2_CONFIG2_INT, NFC_V1_V2_CONFIG2);
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200256
257 return 1;
258}
259
Sascha Hauer63f14742010-10-18 10:16:26 +0200260/*
261 * It has been observed that the i.MX21 cannot read the CONFIG2:INT bit
262 * if interrupts are masked (CONFIG1:INT_MSK is set). To handle this, the
263 * driver can enable/disable the irq line rather than simply masking the
264 * interrupts.
265 */
266static void irq_control_mx21(struct mxc_nand_host *host, int activate)
267{
268 if (activate)
269 enable_irq(host->irq);
270 else
271 disable_irq_nosync(host->irq);
272}
273
274static void irq_control_v1_v2(struct mxc_nand_host *host, int activate)
275{
276 uint16_t tmp;
277
278 tmp = readw(NFC_V1_V2_CONFIG1);
279
280 if (activate)
281 tmp &= ~NFC_V1_V2_CONFIG1_INT_MSK;
282 else
283 tmp |= NFC_V1_V2_CONFIG1_INT_MSK;
284
285 writew(tmp, NFC_V1_V2_CONFIG1);
286}
287
288static void irq_control_v3(struct mxc_nand_host *host, int activate)
289{
290 uint32_t tmp;
291
292 tmp = readl(NFC_V3_CONFIG2);
293
294 if (activate)
295 tmp &= ~NFC_V3_CONFIG2_INT_MSK;
296 else
297 tmp |= NFC_V3_CONFIG2_INT_MSK;
298
299 writel(tmp, NFC_V3_CONFIG2);
300}
301
Sascha Hauer34f6e152008-09-02 17:16:59 +0200302/* This function polls the NANDFC to wait for the basic operation to
303 * complete by checking the INT bit of config2 register.
304 */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200305static void wait_op_done(struct mxc_nand_host *host, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200306{
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200307 int max_retries = 8000;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200308
309 if (useirq) {
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200310 if (!host->check_int(host)) {
Sascha Hauer63f14742010-10-18 10:16:26 +0200311 INIT_COMPLETION(host->op_completion);
312 host->irq_control(host, 1);
313 wait_for_completion(&host->op_completion);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200314 }
315 } else {
316 while (max_retries-- > 0) {
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200317 if (host->check_int(host))
Sascha Hauer34f6e152008-09-02 17:16:59 +0200318 break;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +0200319
Sascha Hauer34f6e152008-09-02 17:16:59 +0200320 udelay(1);
321 }
Roel Kluin43950a62009-06-04 16:24:59 +0200322 if (max_retries < 0)
Sascha Hauer62465492009-06-04 15:57:20 +0200323 DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
324 __func__);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200325 }
326}
327
Sascha Hauer71ec5152010-08-06 15:53:11 +0200328static void send_cmd_v3(struct mxc_nand_host *host, uint16_t cmd, int useirq)
329{
330 /* fill command */
331 writel(cmd, NFC_V3_FLASH_CMD);
332
333 /* send out command */
334 writel(NFC_CMD, NFC_V3_LAUNCH);
335
336 /* Wait for operation to complete */
337 wait_op_done(host, useirq);
338}
339
Sascha Hauer34f6e152008-09-02 17:16:59 +0200340/* This function issues the specified command to the NAND device and
341 * waits for completion. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200342static void send_cmd_v1_v2(struct mxc_nand_host *host, uint16_t cmd, int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200343{
344 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
345
Sascha Hauer1bc99182010-08-06 15:53:08 +0200346 writew(cmd, NFC_V1_V2_FLASH_CMD);
347 writew(NFC_CMD, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200348
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200349 if (cpu_is_mx21() && (cmd == NAND_CMD_RESET)) {
350 int max_retries = 100;
351 /* Reset completion is indicated by NFC_CONFIG2 */
352 /* being set to 0 */
353 while (max_retries-- > 0) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200354 if (readw(NFC_V1_V2_CONFIG2) == 0) {
Ivo Claryssea47bfd22010-04-08 16:16:51 +0200355 break;
356 }
357 udelay(1);
358 }
359 if (max_retries < 0)
360 DEBUG(MTD_DEBUG_LEVEL0, "%s: RESET failed\n",
361 __func__);
362 } else {
363 /* Wait for operation to complete */
364 wait_op_done(host, useirq);
365 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200366}
367
Sascha Hauer71ec5152010-08-06 15:53:11 +0200368static void send_addr_v3(struct mxc_nand_host *host, uint16_t addr, int islast)
369{
370 /* fill address */
371 writel(addr, NFC_V3_FLASH_ADDR0);
372
373 /* send out address */
374 writel(NFC_ADDR, NFC_V3_LAUNCH);
375
376 wait_op_done(host, 0);
377}
378
Sascha Hauer34f6e152008-09-02 17:16:59 +0200379/* This function sends an address (or partial address) to the
380 * NAND device. The address is used to select the source/destination for
381 * a NAND command. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200382static void send_addr_v1_v2(struct mxc_nand_host *host, uint16_t addr, int islast)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200383{
384 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
385
Sascha Hauer1bc99182010-08-06 15:53:08 +0200386 writew(addr, NFC_V1_V2_FLASH_ADDR);
387 writew(NFC_ADDR, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200388
389 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200390 wait_op_done(host, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200391}
392
Sascha Hauer71ec5152010-08-06 15:53:11 +0200393static void send_page_v3(struct mtd_info *mtd, unsigned int ops)
394{
395 struct nand_chip *nand_chip = mtd->priv;
396 struct mxc_nand_host *host = nand_chip->priv;
397 uint32_t tmp;
398
399 tmp = readl(NFC_V3_CONFIG1);
400 tmp &= ~(7 << 4);
401 writel(tmp, NFC_V3_CONFIG1);
402
403 /* transfer data from NFC ram to nand */
404 writel(ops, NFC_V3_LAUNCH);
405
406 wait_op_done(host, false);
407}
408
Sascha Hauer5f973042010-08-06 15:53:06 +0200409static void send_page_v1_v2(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200410{
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200411 struct nand_chip *nand_chip = mtd->priv;
412 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200413 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200414
Sascha Hauer94671142009-10-05 12:14:21 +0200415 if (nfc_is_v1() && mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200416 bufs = 4;
417 else
418 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200419
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200420 for (i = 0; i < bufs; i++) {
421
422 /* NANDFC buffer 0 is used for page read/write */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200423 writew(i, NFC_V1_V2_BUF_ADDR);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200424
Sascha Hauer1bc99182010-08-06 15:53:08 +0200425 writew(ops, NFC_V1_V2_CONFIG2);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200426
427 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200428 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200429 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200430}
431
Sascha Hauer71ec5152010-08-06 15:53:11 +0200432static void send_read_id_v3(struct mxc_nand_host *host)
433{
434 /* Read ID into main buffer */
435 writel(NFC_ID, NFC_V3_LAUNCH);
436
437 wait_op_done(host, true);
438
439 memcpy(host->data_buf, host->main_area0, 16);
440}
441
Sascha Hauer34f6e152008-09-02 17:16:59 +0200442/* Request the NANDFC to perform a read of the NAND device ID. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200443static void send_read_id_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200444{
445 struct nand_chip *this = &host->nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200446
447 /* NANDFC buffer 0 is used for device ID output */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200448 writew(0x0, NFC_V1_V2_BUF_ADDR);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200449
Sascha Hauer1bc99182010-08-06 15:53:08 +0200450 writew(NFC_ID, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200451
452 /* Wait for operation to complete */
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200453 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200454
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200455 memcpy(host->data_buf, host->main_area0, 16);
John Ognessf7b66e52010-06-18 18:59:47 +0200456
457 if (this->options & NAND_BUSWIDTH_16) {
458 /* compress the ID info */
459 host->data_buf[1] = host->data_buf[2];
460 host->data_buf[2] = host->data_buf[4];
461 host->data_buf[3] = host->data_buf[6];
462 host->data_buf[4] = host->data_buf[8];
463 host->data_buf[5] = host->data_buf[10];
464 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200465}
466
Sascha Hauer71ec5152010-08-06 15:53:11 +0200467static uint16_t get_dev_status_v3(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200468{
Sascha Hauer71ec5152010-08-06 15:53:11 +0200469 writew(NFC_STATUS, NFC_V3_LAUNCH);
Sascha Hauerc110eaf2009-10-21 16:01:02 +0200470 wait_op_done(host, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200471
Sascha Hauer71ec5152010-08-06 15:53:11 +0200472 return readl(NFC_V3_CONFIG1) >> 16;
473}
474
Sascha Hauer34f6e152008-09-02 17:16:59 +0200475/* This function requests the NANDFC to perform a read of the
476 * NAND device status and returns the current status. */
Sascha Hauer5f973042010-08-06 15:53:06 +0200477static uint16_t get_dev_status_v1_v2(struct mxc_nand_host *host)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200478{
Sascha Hauerc29c6072010-08-06 15:53:05 +0200479 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200480 uint32_t store;
481 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200482
Sascha Hauerc29c6072010-08-06 15:53:05 +0200483 writew(0x0, NFC_V1_V2_BUF_ADDR);
484
485 /*
486 * The device status is stored in main_area0. To
487 * prevent corruption of the buffer save the value
488 * and restore it afterwards.
489 */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200490 store = readl(main_buf);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200491
Sascha Hauer1bc99182010-08-06 15:53:08 +0200492 writew(NFC_STATUS, NFC_V1_V2_CONFIG2);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200493 wait_op_done(host, true);
494
Sascha Hauer34f6e152008-09-02 17:16:59 +0200495 ret = readw(main_buf);
Sascha Hauerc29c6072010-08-06 15:53:05 +0200496
Sascha Hauer34f6e152008-09-02 17:16:59 +0200497 writel(store, main_buf);
498
499 return ret;
500}
501
502/* This functions is used by upper layer to checks if device is ready */
503static int mxc_nand_dev_ready(struct mtd_info *mtd)
504{
505 /*
506 * NFC handles R/B internally. Therefore, this function
507 * always returns status as ready.
508 */
509 return 1;
510}
511
512static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
513{
514 /*
515 * If HW ECC is enabled, we turn it on during init. There is
516 * no need to enable again here.
517 */
518}
519
Sascha Hauer94f77e52010-08-06 15:53:09 +0200520static int mxc_nand_correct_data_v1(struct mtd_info *mtd, u_char *dat,
Sascha Hauer34f6e152008-09-02 17:16:59 +0200521 u_char *read_ecc, u_char *calc_ecc)
522{
523 struct nand_chip *nand_chip = mtd->priv;
524 struct mxc_nand_host *host = nand_chip->priv;
525
526 /*
527 * 1-Bit errors are automatically corrected in HW. No need for
528 * additional correction. 2-Bit errors cannot be corrected by
529 * HW ECC, so we need to return failure
530 */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200531 uint16_t ecc_status = readw(NFC_V1_V2_ECC_STATUS_RESULT);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200532
533 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
534 DEBUG(MTD_DEBUG_LEVEL0,
535 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
536 return -1;
537 }
538
539 return 0;
540}
541
Sascha Hauer94f77e52010-08-06 15:53:09 +0200542static int mxc_nand_correct_data_v2_v3(struct mtd_info *mtd, u_char *dat,
543 u_char *read_ecc, u_char *calc_ecc)
544{
545 struct nand_chip *nand_chip = mtd->priv;
546 struct mxc_nand_host *host = nand_chip->priv;
547 u32 ecc_stat, err;
548 int no_subpages = 1;
549 int ret = 0;
550 u8 ecc_bit_mask, err_limit;
551
552 ecc_bit_mask = (host->eccsize == 4) ? 0x7 : 0xf;
553 err_limit = (host->eccsize == 4) ? 0x4 : 0x8;
554
555 no_subpages = mtd->writesize >> 9;
556
Sascha Hauer71ec5152010-08-06 15:53:11 +0200557 if (nfc_is_v21())
558 ecc_stat = readl(NFC_V1_V2_ECC_STATUS_RESULT);
559 else
560 ecc_stat = readl(NFC_V3_ECC_STATUS_RESULT);
Sascha Hauer94f77e52010-08-06 15:53:09 +0200561
562 do {
563 err = ecc_stat & ecc_bit_mask;
564 if (err > err_limit) {
565 printk(KERN_WARNING "UnCorrectable RS-ECC Error\n");
566 return -1;
567 } else {
568 ret += err;
569 }
570 ecc_stat >>= 4;
571 } while (--no_subpages);
572
573 mtd->ecc_stats.corrected += ret;
574 pr_debug("%d Symbol Correctable RS-ECC Error\n", ret);
575
576 return ret;
577}
578
Sascha Hauer34f6e152008-09-02 17:16:59 +0200579static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
580 u_char *ecc_code)
581{
582 return 0;
583}
584
585static u_char mxc_nand_read_byte(struct mtd_info *mtd)
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 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200590
591 /* Check for status request */
592 if (host->status_request)
Sascha Hauer5f973042010-08-06 15:53:06 +0200593 return host->get_dev_status(host) & 0xFF;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200594
Sascha Hauerf8f96082009-06-04 17:12:26 +0200595 ret = *(uint8_t *)(host->data_buf + host->buf_start);
596 host->buf_start++;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200597
598 return ret;
599}
600
601static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
602{
603 struct nand_chip *nand_chip = mtd->priv;
604 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200605 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200606
Sascha Hauerf8f96082009-06-04 17:12:26 +0200607 ret = *(uint16_t *)(host->data_buf + host->buf_start);
608 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200609
610 return ret;
611}
612
613/* Write data of length len to buffer buf. The data to be
614 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
615 * Operation by the NFC, the data is written to NAND Flash */
616static void mxc_nand_write_buf(struct mtd_info *mtd,
617 const u_char *buf, int len)
618{
619 struct nand_chip *nand_chip = mtd->priv;
620 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200621 u16 col = host->buf_start;
622 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200623
Sascha Hauerf8f96082009-06-04 17:12:26 +0200624 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200625
Sascha Hauerf8f96082009-06-04 17:12:26 +0200626 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200627
Sascha Hauerf8f96082009-06-04 17:12:26 +0200628 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200629}
630
631/* Read the data buffer from the NAND Flash. To read the data from NAND
632 * Flash first the data output cycle is initiated by the NFC, which copies
633 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
634 */
635static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
636{
637 struct nand_chip *nand_chip = mtd->priv;
638 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200639 u16 col = host->buf_start;
640 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200641
Sascha Hauerf8f96082009-06-04 17:12:26 +0200642 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200643
Sascha Hauerf8f96082009-06-04 17:12:26 +0200644 memcpy(buf, host->data_buf + col, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200645
Sascha Hauerf8f96082009-06-04 17:12:26 +0200646 host->buf_start += len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200647}
648
649/* Used by the upper layer to verify the data in NAND Flash
650 * with the data in the buf. */
651static int mxc_nand_verify_buf(struct mtd_info *mtd,
652 const u_char *buf, int len)
653{
654 return -EFAULT;
655}
656
657/* This function is used by upper layer for select and
658 * deselect of the NAND chip */
659static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
660{
661 struct nand_chip *nand_chip = mtd->priv;
662 struct mxc_nand_host *host = nand_chip->priv;
663
Sascha Hauer34f6e152008-09-02 17:16:59 +0200664 switch (chip) {
665 case -1:
666 /* Disable the NFC clock */
667 if (host->clk_act) {
668 clk_disable(host->clk);
669 host->clk_act = 0;
670 }
671 break;
672 case 0:
673 /* Enable the NFC clock */
674 if (!host->clk_act) {
675 clk_enable(host->clk);
676 host->clk_act = 1;
677 }
678 break;
679
680 default:
681 break;
682 }
683}
684
Sascha Hauerf8f96082009-06-04 17:12:26 +0200685/*
686 * Function to transfer data to/from spare area.
687 */
688static void copy_spare(struct mtd_info *mtd, bool bfrom)
689{
690 struct nand_chip *this = mtd->priv;
691 struct mxc_nand_host *host = this->priv;
692 u16 i, j;
693 u16 n = mtd->writesize >> 9;
694 u8 *d = host->data_buf + mtd->writesize;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200695 u8 *s = host->spare0;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200696 u16 t = host->spare_len;
697
698 j = (mtd->oobsize / n >> 1) << 1;
699
700 if (bfrom) {
701 for (i = 0; i < n - 1; i++)
702 memcpy(d + i * j, s + i * t, j);
703
704 /* the last section */
705 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
706 } else {
707 for (i = 0; i < n - 1; i++)
708 memcpy(&s[i * t], &d[i * j], j);
709
710 /* the last section */
711 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
712 }
713}
714
Sascha Hauera3e65b62009-06-02 11:47:59 +0200715static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200716{
717 struct nand_chip *nand_chip = mtd->priv;
718 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200719
720 /* Write out column address, if necessary */
721 if (column != -1) {
722 /*
723 * MXC NANDFC can only perform full page+spare or
724 * spare-only read/write. When the upper layers
Gilles Espinasse177b2412011-01-09 08:59:49 +0100725 * perform a read/write buf operation, the saved column
726 * address is used to index into the full page.
Sascha Hauer34f6e152008-09-02 17:16:59 +0200727 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200728 host->send_addr(host, 0, page_addr == -1);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200729 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200730 /* another col addr cycle for 2k page */
Sascha Hauer5f973042010-08-06 15:53:06 +0200731 host->send_addr(host, 0, false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200732 }
733
734 /* Write out page address, if necessary */
735 if (page_addr != -1) {
736 /* paddr_0 - p_addr_7 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200737 host->send_addr(host, (page_addr & 0xff), false);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200738
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200739 if (mtd->writesize > 512) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400740 if (mtd->size >= 0x10000000) {
741 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200742 host->send_addr(host, (page_addr >> 8) & 0xff, false);
743 host->send_addr(host, (page_addr >> 16) & 0xff, true);
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400744 } else
745 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200746 host->send_addr(host, (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200747 } else {
748 /* One more address cycle for higher density devices */
749 if (mtd->size >= 0x4000000) {
750 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200751 host->send_addr(host, (page_addr >> 8) & 0xff, false);
752 host->send_addr(host, (page_addr >> 16) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200753 } else
754 /* paddr_8 - paddr_15 */
Sascha Hauer5f973042010-08-06 15:53:06 +0200755 host->send_addr(host, (page_addr >> 8) & 0xff, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200756 }
757 }
Sascha Hauera3e65b62009-06-02 11:47:59 +0200758}
Sascha Hauer34f6e152008-09-02 17:16:59 +0200759
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200760/*
761 * v2 and v3 type controllers can do 4bit or 8bit ecc depending
762 * on how much oob the nand chip has. For 8bit ecc we need at least
763 * 26 bytes of oob data per 512 byte block.
764 */
765static int get_eccsize(struct mtd_info *mtd)
766{
767 int oobbytes_per_512 = 0;
768
769 oobbytes_per_512 = mtd->oobsize * 512 / mtd->writesize;
770
771 if (oobbytes_per_512 < 26)
772 return 4;
773 else
774 return 8;
775}
776
Sascha Hauer5f973042010-08-06 15:53:06 +0200777static void preset_v1_v2(struct mtd_info *mtd)
Ivo Claryssed4840182010-04-08 16:14:44 +0200778{
779 struct nand_chip *nand_chip = mtd->priv;
780 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200781 uint16_t config1 = 0;
Ivo Claryssed4840182010-04-08 16:14:44 +0200782
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200783 if (nand_chip->ecc.mode == NAND_ECC_HW)
784 config1 |= NFC_V1_V2_CONFIG1_ECC_EN;
785
786 if (nfc_is_v21())
787 config1 |= NFC_V2_CONFIG1_FP_INT;
788
789 if (!cpu_is_mx21())
790 config1 |= NFC_V1_V2_CONFIG1_INT_MSK;
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200791
792 if (nfc_is_v21() && mtd->writesize) {
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200793 uint16_t pages_per_block = mtd->erasesize / mtd->writesize;
794
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200795 host->eccsize = get_eccsize(mtd);
796 if (host->eccsize == 4)
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200797 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
798
799 config1 |= NFC_V2_CONFIG1_PPB(ffs(pages_per_block) - 6);
Sascha Hauer6e85dfd2010-08-06 15:53:10 +0200800 } else {
801 host->eccsize = 1;
802 }
803
Sascha Hauerb8db2f52010-08-09 15:04:19 +0200804 writew(config1, NFC_V1_V2_CONFIG1);
Ivo Claryssed4840182010-04-08 16:14:44 +0200805 /* preset operation */
806
807 /* Unlock the internal RAM Buffer */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200808 writew(0x2, NFC_V1_V2_CONFIG);
Ivo Claryssed4840182010-04-08 16:14:44 +0200809
810 /* Blocks to be unlocked */
811 if (nfc_is_v21()) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200812 writew(0x0, NFC_V21_UNLOCKSTART_BLKADDR);
813 writew(0xffff, NFC_V21_UNLOCKEND_BLKADDR);
Ivo Claryssed4840182010-04-08 16:14:44 +0200814 } else if (nfc_is_v1()) {
Sascha Hauer1bc99182010-08-06 15:53:08 +0200815 writew(0x0, NFC_V1_UNLOCKSTART_BLKADDR);
816 writew(0x4000, NFC_V1_UNLOCKEND_BLKADDR);
Ivo Claryssed4840182010-04-08 16:14:44 +0200817 } else
818 BUG();
819
820 /* Unlock Block Command for given address range */
Sascha Hauer1bc99182010-08-06 15:53:08 +0200821 writew(0x4, NFC_V1_V2_WRPROT);
Ivo Claryssed4840182010-04-08 16:14:44 +0200822}
823
Sascha Hauer71ec5152010-08-06 15:53:11 +0200824static void preset_v3(struct mtd_info *mtd)
825{
826 struct nand_chip *chip = mtd->priv;
827 struct mxc_nand_host *host = chip->priv;
828 uint32_t config2, config3;
829 int i, addr_phases;
830
831 writel(NFC_V3_CONFIG1_RBA(0), NFC_V3_CONFIG1);
832 writel(NFC_V3_IPC_CREQ, NFC_V3_IPC);
833
834 /* Unlock the internal RAM Buffer */
835 writel(NFC_V3_WRPROT_BLS_UNLOCK | NFC_V3_WRPROT_UNLOCK,
836 NFC_V3_WRPROT);
837
838 /* Blocks to be unlocked */
839 for (i = 0; i < NAND_MAX_CHIPS; i++)
840 writel(0x0 | (0xffff << 16),
841 NFC_V3_WRPROT_UNLOCK_BLK_ADD0 + (i << 2));
842
843 writel(0, NFC_V3_IPC);
844
845 config2 = NFC_V3_CONFIG2_ONE_CYCLE |
846 NFC_V3_CONFIG2_2CMD_PHASES |
847 NFC_V3_CONFIG2_SPAS(mtd->oobsize >> 1) |
848 NFC_V3_CONFIG2_ST_CMD(0x70) |
Sascha Hauer63f14742010-10-18 10:16:26 +0200849 NFC_V3_CONFIG2_INT_MSK |
Sascha Hauer71ec5152010-08-06 15:53:11 +0200850 NFC_V3_CONFIG2_NUM_ADDR_PHASE0;
851
852 if (chip->ecc.mode == NAND_ECC_HW)
853 config2 |= NFC_V3_CONFIG2_ECC_EN;
854
855 addr_phases = fls(chip->pagemask) >> 3;
856
857 if (mtd->writesize == 2048) {
858 config2 |= NFC_V3_CONFIG2_PS_2048;
859 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
860 } else if (mtd->writesize == 4096) {
861 config2 |= NFC_V3_CONFIG2_PS_4096;
862 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases);
863 } else {
864 config2 |= NFC_V3_CONFIG2_PS_512;
865 config2 |= NFC_V3_CONFIG2_NUM_ADDR_PHASE1(addr_phases - 1);
866 }
867
868 if (mtd->writesize) {
869 config2 |= NFC_V3_CONFIG2_PPB(ffs(mtd->erasesize / mtd->writesize) - 6);
870 host->eccsize = get_eccsize(mtd);
871 if (host->eccsize == 8)
872 config2 |= NFC_V3_CONFIG2_ECC_MODE_8;
873 }
874
875 writel(config2, NFC_V3_CONFIG2);
876
877 config3 = NFC_V3_CONFIG3_NUM_OF_DEVICES(0) |
878 NFC_V3_CONFIG3_NO_SDMA |
879 NFC_V3_CONFIG3_RBB_MODE |
880 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
881 NFC_V3_CONFIG3_ADD_OP(0);
882
883 if (!(chip->options & NAND_BUSWIDTH_16))
884 config3 |= NFC_V3_CONFIG3_FW8;
885
886 writel(config3, NFC_V3_CONFIG3);
887
888 writel(0, NFC_V3_DELAY_LINE);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200889}
890
Sascha Hauer34f6e152008-09-02 17:16:59 +0200891/* Used by the upper layer to write command to NAND Flash for
892 * different operations to be carried out on NAND Flash */
893static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
894 int column, int page_addr)
895{
896 struct nand_chip *nand_chip = mtd->priv;
897 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200898
899 DEBUG(MTD_DEBUG_LEVEL3,
900 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
901 command, column, page_addr);
902
903 /* Reset command state information */
904 host->status_request = false;
905
906 /* Command pre-processing step */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200907 switch (command) {
Ivo Claryssed4840182010-04-08 16:14:44 +0200908 case NAND_CMD_RESET:
Sascha Hauer5f973042010-08-06 15:53:06 +0200909 host->preset(mtd);
910 host->send_cmd(host, command, false);
Ivo Claryssed4840182010-04-08 16:14:44 +0200911 break;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200912
Sascha Hauer34f6e152008-09-02 17:16:59 +0200913 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200914 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200915 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +0200916
Sascha Hauer5f973042010-08-06 15:53:06 +0200917 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200918 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200919 break;
920
Sascha Hauer34f6e152008-09-02 17:16:59 +0200921 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200922 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +0200923 if (command == NAND_CMD_READ0)
924 host->buf_start = column;
925 else
926 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200927
Sascha Hauer5ea32022010-04-27 15:24:01 +0200928 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +0200929
Sascha Hauer5f973042010-08-06 15:53:06 +0200930 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200931 mxc_do_addr_cycle(mtd, column, page_addr);
932
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200933 if (mtd->writesize > 512)
Sascha Hauer5f973042010-08-06 15:53:06 +0200934 host->send_cmd(host, NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200935
Sascha Hauer5f973042010-08-06 15:53:06 +0200936 host->send_page(mtd, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200937
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200938 memcpy(host->data_buf, host->main_area0, mtd->writesize);
Sascha Hauer89121a62009-06-04 17:18:01 +0200939 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200940 break;
941
Sascha Hauer34f6e152008-09-02 17:16:59 +0200942 case NAND_CMD_SEQIN:
Sascha Hauer5ea32022010-04-27 15:24:01 +0200943 if (column >= mtd->writesize)
944 /* call ourself to read a page */
945 mxc_nand_command(mtd, NAND_CMD_READ0, 0, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200946
Sascha Hauer5ea32022010-04-27 15:24:01 +0200947 host->buf_start = column;
Sascha Hauer89121a62009-06-04 17:18:01 +0200948
Sascha Hauer5f973042010-08-06 15:53:06 +0200949 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200950 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200951 break;
952
953 case NAND_CMD_PAGEPROG:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200954 memcpy(host->main_area0, host->data_buf, mtd->writesize);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200955 copy_spare(mtd, false);
Sascha Hauer5f973042010-08-06 15:53:06 +0200956 host->send_page(mtd, NFC_INPUT);
957 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200958 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200959 break;
960
Sascha Hauer34f6e152008-09-02 17:16:59 +0200961 case NAND_CMD_READID:
Sascha Hauer5f973042010-08-06 15:53:06 +0200962 host->send_cmd(host, command, true);
Sascha Hauer89121a62009-06-04 17:18:01 +0200963 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer5f973042010-08-06 15:53:06 +0200964 host->send_read_id(host);
Sascha Hauer94671142009-10-05 12:14:21 +0200965 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200966 break;
967
Sascha Hauer89121a62009-06-04 17:18:01 +0200968 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200969 case NAND_CMD_ERASE2:
Sascha Hauer5f973042010-08-06 15:53:06 +0200970 host->send_cmd(host, command, false);
Sascha Hauer89121a62009-06-04 17:18:01 +0200971 mxc_do_addr_cycle(mtd, column, page_addr);
972
Sascha Hauer34f6e152008-09-02 17:16:59 +0200973 break;
974 }
975}
976
Sascha Hauerf1372052009-10-21 14:25:27 +0200977/*
978 * The generic flash bbt decriptors overlap with our ecc
979 * hardware, so define some i.MX specific ones.
980 */
981static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
982static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
983
984static struct nand_bbt_descr bbt_main_descr = {
985 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
986 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
987 .offs = 0,
988 .len = 4,
989 .veroffs = 4,
990 .maxblocks = 4,
991 .pattern = bbt_pattern,
992};
993
994static struct nand_bbt_descr bbt_mirror_descr = {
995 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
996 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
997 .offs = 0,
998 .len = 4,
999 .veroffs = 4,
1000 .maxblocks = 4,
1001 .pattern = mirror_pattern,
1002};
1003
Sascha Hauer34f6e152008-09-02 17:16:59 +02001004static int __init mxcnd_probe(struct platform_device *pdev)
1005{
1006 struct nand_chip *this;
1007 struct mtd_info *mtd;
1008 struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
1009 struct mxc_nand_host *host;
1010 struct resource *res;
Fabio Estevam2ebf0622010-11-23 17:02:13 -02001011 int err = 0, __maybe_unused nr_parts = 0;
Sascha Hauer94671142009-10-05 12:14:21 +02001012 struct nand_ecclayout *oob_smallpage, *oob_largepage;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001013
1014 /* Allocate memory for MTD device structure and private data */
Sascha Hauerf8f96082009-06-04 17:12:26 +02001015 host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
1016 NAND_MAX_OOBSIZE, GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001017 if (!host)
1018 return -ENOMEM;
1019
Sascha Hauerf8f96082009-06-04 17:12:26 +02001020 host->data_buf = (uint8_t *)(host + 1);
Sascha Hauerf8f96082009-06-04 17:12:26 +02001021
Sascha Hauer34f6e152008-09-02 17:16:59 +02001022 host->dev = &pdev->dev;
1023 /* structures must be linked */
1024 this = &host->nand;
1025 mtd = &host->mtd;
1026 mtd->priv = this;
1027 mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -07001028 mtd->dev.parent = &pdev->dev;
Sascha Hauer1fbff0a2009-10-21 16:06:27 +02001029 mtd->name = DRIVER_NAME;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001030
1031 /* 50 us command delay time */
1032 this->chip_delay = 5;
1033
1034 this->priv = host;
1035 this->dev_ready = mxc_nand_dev_ready;
1036 this->cmdfunc = mxc_nand_command;
1037 this->select_chip = mxc_nand_select_chip;
1038 this->read_byte = mxc_nand_read_byte;
1039 this->read_word = mxc_nand_read_word;
1040 this->write_buf = mxc_nand_write_buf;
1041 this->read_buf = mxc_nand_read_buf;
1042 this->verify_buf = mxc_nand_verify_buf;
1043
Sascha Hauere65fb002009-02-16 14:29:10 +01001044 host->clk = clk_get(&pdev->dev, "nfc");
Vladimir Barinov8541c112009-04-23 15:47:22 +04001045 if (IS_ERR(host->clk)) {
1046 err = PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001047 goto eclk;
Vladimir Barinov8541c112009-04-23 15:47:22 +04001048 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001049
1050 clk_enable(host->clk);
1051 host->clk_act = 1;
1052
1053 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1054 if (!res) {
1055 err = -ENODEV;
1056 goto eres;
1057 }
1058
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001059 host->base = ioremap(res->start, resource_size(res));
1060 if (!host->base) {
Vladimir Barinov8541c112009-04-23 15:47:22 +04001061 err = -ENOMEM;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001062 goto eres;
1063 }
1064
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001065 host->main_area0 = host->base;
Sascha Hauer94671142009-10-05 12:14:21 +02001066
Sascha Hauer5f973042010-08-06 15:53:06 +02001067 if (nfc_is_v1() || nfc_is_v21()) {
1068 host->preset = preset_v1_v2;
1069 host->send_cmd = send_cmd_v1_v2;
1070 host->send_addr = send_addr_v1_v2;
1071 host->send_page = send_page_v1_v2;
1072 host->send_read_id = send_read_id_v1_v2;
1073 host->get_dev_status = get_dev_status_v1_v2;
Sascha Hauer7aaf28a2010-08-06 15:53:07 +02001074 host->check_int = check_int_v1_v2;
Sascha Hauer63f14742010-10-18 10:16:26 +02001075 if (cpu_is_mx21())
1076 host->irq_control = irq_control_mx21;
1077 else
1078 host->irq_control = irq_control_v1_v2;
Sascha Hauer5f973042010-08-06 15:53:06 +02001079 }
Sascha Hauer94671142009-10-05 12:14:21 +02001080
1081 if (nfc_is_v21()) {
Sascha Hauer938cf992010-08-06 15:53:04 +02001082 host->regs = host->base + 0x1e00;
Sascha Hauer94671142009-10-05 12:14:21 +02001083 host->spare0 = host->base + 0x1000;
1084 host->spare_len = 64;
1085 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1086 oob_largepage = &nandv2_hw_eccoob_largepage;
Ivo Claryssed4840182010-04-08 16:14:44 +02001087 this->ecc.bytes = 9;
Sascha Hauer94671142009-10-05 12:14:21 +02001088 } else if (nfc_is_v1()) {
Sascha Hauer938cf992010-08-06 15:53:04 +02001089 host->regs = host->base + 0xe00;
Sascha Hauer94671142009-10-05 12:14:21 +02001090 host->spare0 = host->base + 0x800;
1091 host->spare_len = 16;
1092 oob_smallpage = &nandv1_hw_eccoob_smallpage;
1093 oob_largepage = &nandv1_hw_eccoob_largepage;
Sascha Hauer94671142009-10-05 12:14:21 +02001094 this->ecc.bytes = 3;
Sascha Hauer71ec5152010-08-06 15:53:11 +02001095 host->eccsize = 1;
1096 } else if (nfc_is_v3_2()) {
1097 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1098 if (!res) {
1099 err = -ENODEV;
1100 goto eirq;
1101 }
1102 host->regs_ip = ioremap(res->start, resource_size(res));
1103 if (!host->regs_ip) {
1104 err = -ENOMEM;
1105 goto eirq;
1106 }
1107 host->regs_axi = host->base + 0x1e00;
1108 host->spare0 = host->base + 0x1000;
1109 host->spare_len = 64;
1110 host->preset = preset_v3;
1111 host->send_cmd = send_cmd_v3;
1112 host->send_addr = send_addr_v3;
1113 host->send_page = send_page_v3;
1114 host->send_read_id = send_read_id_v3;
1115 host->check_int = check_int_v3;
1116 host->get_dev_status = get_dev_status_v3;
Sascha Hauer63f14742010-10-18 10:16:26 +02001117 host->irq_control = irq_control_v3;
Sascha Hauer71ec5152010-08-06 15:53:11 +02001118 oob_smallpage = &nandv2_hw_eccoob_smallpage;
1119 oob_largepage = &nandv2_hw_eccoob_largepage;
Sascha Hauer94671142009-10-05 12:14:21 +02001120 } else
1121 BUG();
Sascha Hauer34f6e152008-09-02 17:16:59 +02001122
Sascha Hauer13e1add2009-10-21 10:39:05 +02001123 this->ecc.size = 512;
Sascha Hauer94671142009-10-05 12:14:21 +02001124 this->ecc.layout = oob_smallpage;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001125
1126 if (pdata->hw_ecc) {
1127 this->ecc.calculate = mxc_nand_calculate_ecc;
1128 this->ecc.hwctl = mxc_nand_enable_hwecc;
Sascha Hauer94f77e52010-08-06 15:53:09 +02001129 if (nfc_is_v1())
1130 this->ecc.correct = mxc_nand_correct_data_v1;
1131 else
1132 this->ecc.correct = mxc_nand_correct_data_v2_v3;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001133 this->ecc.mode = NAND_ECC_HW;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001134 } else {
1135 this->ecc.mode = NAND_ECC_SOFT;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001136 }
1137
Sascha Hauer34f6e152008-09-02 17:16:59 +02001138 /* NAND bus width determines access funtions used by upper layer */
Sascha Hauer13e1add2009-10-21 10:39:05 +02001139 if (pdata->width == 2)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001140 this->options |= NAND_BUSWIDTH_16;
Sascha Hauer13e1add2009-10-21 10:39:05 +02001141
Sascha Hauerf1372052009-10-21 14:25:27 +02001142 if (pdata->flash_bbt) {
1143 this->bbt_td = &bbt_main_descr;
1144 this->bbt_md = &bbt_mirror_descr;
1145 /* update flash based bbt */
1146 this->options |= NAND_USE_FLASH_BBT;
Sascha Hauer34f6e152008-09-02 17:16:59 +02001147 }
1148
Sascha Hauer63f14742010-10-18 10:16:26 +02001149 init_completion(&host->op_completion);
Ivo Claryssed4840182010-04-08 16:14:44 +02001150
1151 host->irq = platform_get_irq(pdev, 0);
1152
Sascha Hauer63f14742010-10-18 10:16:26 +02001153 /*
1154 * mask the interrupt. For i.MX21 explicitely call
1155 * irq_control_v1_v2 to use the mask bit. We can't call
1156 * disable_irq_nosync() for an interrupt we do not own yet.
1157 */
1158 if (cpu_is_mx21())
1159 irq_control_v1_v2(host, 0);
1160 else
1161 host->irq_control(host, 0);
1162
Ivo Claryssea47bfd22010-04-08 16:16:51 +02001163 err = request_irq(host->irq, mxc_nfc_irq, IRQF_DISABLED, DRIVER_NAME, host);
Ivo Claryssed4840182010-04-08 16:14:44 +02001164 if (err)
1165 goto eirq;
1166
Sascha Hauer63f14742010-10-18 10:16:26 +02001167 host->irq_control(host, 0);
1168
1169 /*
1170 * Now that the interrupt is disabled make sure the interrupt
1171 * mask bit is cleared on i.MX21. Otherwise we can't read
1172 * the interrupt status bit on this machine.
1173 */
1174 if (cpu_is_mx21())
1175 irq_control_v1_v2(host, 1);
1176
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001177 /* first scan to find the device and get the page size */
David Woodhouse5e81e882010-02-26 18:32:56 +00001178 if (nand_scan_ident(mtd, 1, NULL)) {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001179 err = -ENXIO;
1180 goto escan;
1181 }
Sascha Hauer34f6e152008-09-02 17:16:59 +02001182
Sascha Hauer6e85dfd2010-08-06 15:53:10 +02001183 /* Call preset again, with correct writesize this time */
1184 host->preset(mtd);
1185
Sascha Hauer2d69c7f2009-10-05 11:24:02 +02001186 if (mtd->writesize == 2048)
Sascha Hauer94671142009-10-05 12:14:21 +02001187 this->ecc.layout = oob_largepage;
Vladimir Barinovbd3fd622009-05-25 13:06:17 +04001188
1189 /* second phase scan */
1190 if (nand_scan_tail(mtd)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +02001191 err = -ENXIO;
1192 goto escan;
1193 }
1194
1195 /* Register the partitions */
1196#ifdef CONFIG_MTD_PARTITIONS
1197 nr_parts =
1198 parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
1199 if (nr_parts > 0)
1200 add_mtd_partitions(mtd, host->parts, nr_parts);
Baruch Siachcce02462010-05-31 08:49:40 +03001201 else if (pdata->parts)
1202 add_mtd_partitions(mtd, pdata->parts, pdata->nr_parts);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001203 else
1204#endif
1205 {
1206 pr_info("Registering %s as whole device\n", mtd->name);
1207 add_mtd_device(mtd);
1208 }
1209
1210 platform_set_drvdata(pdev, host);
1211
1212 return 0;
1213
1214escan:
Magnus Liljab258fd82009-05-08 21:57:47 +02001215 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001216eirq:
Sascha Hauer71ec5152010-08-06 15:53:11 +02001217 if (host->regs_ip)
1218 iounmap(host->regs_ip);
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001219 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001220eres:
1221 clk_put(host->clk);
1222eclk:
1223 kfree(host);
1224
1225 return err;
1226}
1227
Uwe Kleine-König51eeb872009-12-07 09:44:05 +00001228static int __devexit mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +02001229{
1230 struct mxc_nand_host *host = platform_get_drvdata(pdev);
1231
1232 clk_put(host->clk);
1233
1234 platform_set_drvdata(pdev, NULL);
1235
1236 nand_release(&host->mtd);
Magnus Liljab258fd82009-05-08 21:57:47 +02001237 free_irq(host->irq, host);
Sascha Hauer71ec5152010-08-06 15:53:11 +02001238 if (host->regs_ip)
1239 iounmap(host->regs_ip);
Sascha Hauerc6de7e12009-10-05 11:14:35 +02001240 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001241 kfree(host);
1242
1243 return 0;
1244}
1245
Sascha Hauer34f6e152008-09-02 17:16:59 +02001246static struct platform_driver mxcnd_driver = {
1247 .driver = {
1248 .name = DRIVER_NAME,
Eric Bénard04dd0d32010-06-17 20:59:04 +02001249 },
Uwe Kleine-Königdaa0f152009-11-24 22:07:08 +01001250 .remove = __devexit_p(mxcnd_remove),
Sascha Hauer34f6e152008-09-02 17:16:59 +02001251};
1252
1253static int __init mxc_nd_init(void)
1254{
Vladimir Barinov8541c112009-04-23 15:47:22 +04001255 return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
Sascha Hauer34f6e152008-09-02 17:16:59 +02001256}
1257
1258static void __exit mxc_nd_cleanup(void)
1259{
1260 /* Unregister the device structure */
1261 platform_driver_unregister(&mxcnd_driver);
1262}
1263
1264module_init(mxc_nd_init);
1265module_exit(mxc_nd_cleanup);
1266
1267MODULE_AUTHOR("Freescale Semiconductor, Inc.");
1268MODULE_DESCRIPTION("MXC NAND MTD driver");
1269MODULE_LICENSE("GPL");