blob: 044a574830277901d812569af427719c669a3766 [file] [log] [blame]
Sascha Hauer34f6e152008-09-02 17:16:59 +02001/*
2 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
3 * Copyright 2008 Sascha Hauer, kernel@pengutronix.de
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 * MA 02110-1301, USA.
18 */
19
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23#include <linux/module.h>
24#include <linux/mtd/mtd.h>
25#include <linux/mtd/nand.h>
26#include <linux/mtd/partitions.h>
27#include <linux/interrupt.h>
28#include <linux/device.h>
29#include <linux/platform_device.h>
30#include <linux/clk.h>
31#include <linux/err.h>
32#include <linux/io.h>
33
34#include <asm/mach/flash.h>
35#include <mach/mxc_nand.h>
Sascha Hauer94671142009-10-05 12:14:21 +020036#include <mach/hardware.h>
Sascha Hauer34f6e152008-09-02 17:16:59 +020037
38#define DRIVER_NAME "mxc_nand"
39
Sascha Hauer94671142009-10-05 12:14:21 +020040#define nfc_is_v21() (cpu_is_mx25() || cpu_is_mx35())
41#define nfc_is_v1() (cpu_is_mx31() || cpu_is_mx27())
42
Sascha Hauer34f6e152008-09-02 17:16:59 +020043/* Addresses for NFC registers */
44#define NFC_BUF_SIZE 0xE00
45#define NFC_BUF_ADDR 0xE04
46#define NFC_FLASH_ADDR 0xE06
47#define NFC_FLASH_CMD 0xE08
48#define NFC_CONFIG 0xE0A
49#define NFC_ECC_STATUS_RESULT 0xE0C
50#define NFC_RSLTMAIN_AREA 0xE0E
51#define NFC_RSLTSPARE_AREA 0xE10
52#define NFC_WRPROT 0xE12
Sascha Hauer94671142009-10-05 12:14:21 +020053#define NFC_V1_UNLOCKSTART_BLKADDR 0xe14
54#define NFC_V1_UNLOCKEND_BLKADDR 0xe16
55#define NFC_V21_UNLOCKSTART_BLKADDR 0xe20
56#define NFC_V21_UNLOCKEND_BLKADDR 0xe22
Sascha Hauer34f6e152008-09-02 17:16:59 +020057#define NFC_NF_WRPRST 0xE18
58#define NFC_CONFIG1 0xE1A
59#define NFC_CONFIG2 0xE1C
60
Sascha Hauer34f6e152008-09-02 17:16:59 +020061/* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
62 * for Command operation */
63#define NFC_CMD 0x1
64
65/* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
66 * for Address operation */
67#define NFC_ADDR 0x2
68
69/* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
70 * for Input operation */
71#define NFC_INPUT 0x4
72
73/* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
74 * for Data Output operation */
75#define NFC_OUTPUT 0x8
76
77/* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
78 * for Read ID operation */
79#define NFC_ID 0x10
80
81/* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
82 * for Read Status operation */
83#define NFC_STATUS 0x20
84
85/* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
86 * Status operation */
87#define NFC_INT 0x8000
88
89#define NFC_SP_EN (1 << 2)
90#define NFC_ECC_EN (1 << 3)
91#define NFC_INT_MSK (1 << 4)
92#define NFC_BIG (1 << 5)
93#define NFC_RST (1 << 6)
94#define NFC_CE (1 << 7)
95#define NFC_ONE_CYCLE (1 << 8)
96
97struct mxc_nand_host {
98 struct mtd_info mtd;
99 struct nand_chip nand;
100 struct mtd_partition *parts;
101 struct device *dev;
102
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200103 void *spare0;
104 void *main_area0;
105 void *main_area1;
106
107 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200108 void __iomem *regs;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200109 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200110 struct clk *clk;
111 int clk_act;
112 int irq;
113
114 wait_queue_head_t irq_waitq;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200115
116 uint8_t *data_buf;
117 unsigned int buf_start;
118 int spare_len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200119};
120
121/* Define delays in microsec for NAND device operations */
122#define TROP_US_DELAY 2000
Sascha Hauer34f6e152008-09-02 17:16:59 +0200123
124/* OOB placement block for use with hardware ecc generation */
Sascha Hauer94671142009-10-05 12:14:21 +0200125static struct nand_ecclayout nandv1_hw_eccoob_smallpage = {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200126 .eccbytes = 5,
127 .eccpos = {6, 7, 8, 9, 10},
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200128 .oobfree = {{0, 5}, {12, 4}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200129};
130
Sascha Hauer94671142009-10-05 12:14:21 +0200131static struct nand_ecclayout nandv1_hw_eccoob_largepage = {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400132 .eccbytes = 20,
133 .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
134 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
135 .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200136};
137
Sascha Hauer94671142009-10-05 12:14:21 +0200138/* OOB description for 512 byte pages with 16 byte OOB */
139static struct nand_ecclayout nandv2_hw_eccoob_smallpage = {
140 .eccbytes = 1 * 9,
141 .eccpos = {
142 7, 8, 9, 10, 11, 12, 13, 14, 15
143 },
144 .oobfree = {
145 {.offset = 0, .length = 5}
146 }
147};
148
149/* OOB description for 2048 byte pages with 64 byte OOB */
150static struct nand_ecclayout nandv2_hw_eccoob_largepage = {
151 .eccbytes = 4 * 9,
152 .eccpos = {
153 7, 8, 9, 10, 11, 12, 13, 14, 15,
154 23, 24, 25, 26, 27, 28, 29, 30, 31,
155 39, 40, 41, 42, 43, 44, 45, 46, 47,
156 55, 56, 57, 58, 59, 60, 61, 62, 63
157 },
158 .oobfree = {
159 {.offset = 2, .length = 4},
160 {.offset = 16, .length = 7},
161 {.offset = 32, .length = 7},
162 {.offset = 48, .length = 7}
163 }
164};
165
Sascha Hauer34f6e152008-09-02 17:16:59 +0200166#ifdef CONFIG_MTD_PARTITIONS
167static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
168#endif
169
170static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
171{
172 struct mxc_nand_host *host = dev_id;
173
174 uint16_t tmp;
175
176 tmp = readw(host->regs + NFC_CONFIG1);
177 tmp |= NFC_INT_MSK; /* Disable interrupt */
178 writew(tmp, host->regs + NFC_CONFIG1);
179
180 wake_up(&host->irq_waitq);
181
182 return IRQ_HANDLED;
183}
184
185/* This function polls the NANDFC to wait for the basic operation to
186 * complete by checking the INT bit of config2 register.
187 */
188static void wait_op_done(struct mxc_nand_host *host, int max_retries,
Sascha Hauer62465492009-06-04 15:57:20 +0200189 int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200190{
191 uint32_t tmp;
192
193 if (useirq) {
194 if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) {
195
196 tmp = readw(host->regs + NFC_CONFIG1);
197 tmp &= ~NFC_INT_MSK; /* Enable interrupt */
198 writew(tmp, host->regs + NFC_CONFIG1);
199
200 wait_event(host->irq_waitq,
201 readw(host->regs + NFC_CONFIG2) & NFC_INT);
202
203 tmp = readw(host->regs + NFC_CONFIG2);
204 tmp &= ~NFC_INT;
205 writew(tmp, host->regs + NFC_CONFIG2);
206 }
207 } else {
208 while (max_retries-- > 0) {
209 if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
210 tmp = readw(host->regs + NFC_CONFIG2);
211 tmp &= ~NFC_INT;
212 writew(tmp, host->regs + NFC_CONFIG2);
213 break;
214 }
215 udelay(1);
216 }
Roel Kluin43950a62009-06-04 16:24:59 +0200217 if (max_retries < 0)
Sascha Hauer62465492009-06-04 15:57:20 +0200218 DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
219 __func__);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200220 }
221}
222
223/* This function issues the specified command to the NAND device and
224 * waits for completion. */
225static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
226{
227 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
228
229 writew(cmd, host->regs + NFC_FLASH_CMD);
230 writew(NFC_CMD, host->regs + NFC_CONFIG2);
231
232 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200233 wait_op_done(host, TROP_US_DELAY, useirq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200234}
235
236/* This function sends an address (or partial address) to the
237 * NAND device. The address is used to select the source/destination for
238 * a NAND command. */
239static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast)
240{
241 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
242
243 writew(addr, host->regs + NFC_FLASH_ADDR);
244 writew(NFC_ADDR, host->regs + NFC_CONFIG2);
245
246 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200247 wait_op_done(host, TROP_US_DELAY, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200248}
249
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200250static void send_page(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200251{
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200252 struct nand_chip *nand_chip = mtd->priv;
253 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200254 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200255
Sascha Hauer94671142009-10-05 12:14:21 +0200256 if (nfc_is_v1() && mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200257 bufs = 4;
258 else
259 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200260
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200261 for (i = 0; i < bufs; i++) {
262
263 /* NANDFC buffer 0 is used for page read/write */
264 writew(i, host->regs + NFC_BUF_ADDR);
265
266 writew(ops, host->regs + NFC_CONFIG2);
267
268 /* Wait for operation to complete */
269 wait_op_done(host, TROP_US_DELAY, true);
270 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200271}
272
273/* Request the NANDFC to perform a read of the NAND device ID. */
274static void send_read_id(struct mxc_nand_host *host)
275{
276 struct nand_chip *this = &host->nand;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200277
278 /* NANDFC buffer 0 is used for device ID output */
279 writew(0x0, host->regs + NFC_BUF_ADDR);
280
Sascha Hauer34f6e152008-09-02 17:16:59 +0200281 writew(NFC_ID, host->regs + NFC_CONFIG2);
282
283 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200284 wait_op_done(host, TROP_US_DELAY, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200285
286 if (this->options & NAND_BUSWIDTH_16) {
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200287 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200288 /* compress the ID info */
289 writeb(readb(main_buf + 2), main_buf + 1);
290 writeb(readb(main_buf + 4), main_buf + 2);
291 writeb(readb(main_buf + 6), main_buf + 3);
292 writeb(readb(main_buf + 8), main_buf + 4);
293 writeb(readb(main_buf + 10), main_buf + 5);
294 }
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200295 memcpy(host->data_buf, host->main_area0, 16);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200296}
297
298/* This function requests the NANDFC to perform a read of the
299 * NAND device status and returns the current status. */
300static uint16_t get_dev_status(struct mxc_nand_host *host)
301{
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200302 void __iomem *main_buf = host->main_area1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200303 uint32_t store;
Sascha Hauerf06368f2009-10-05 17:18:42 +0200304 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200305 /* Issue status request to NAND device */
306
307 /* store the main area1 first word, later do recovery */
308 store = readl(main_buf);
309 /* NANDFC buffer 1 is used for device status to prevent
310 * corruption of read/write buffer on status requests. */
311 writew(1, host->regs + NFC_BUF_ADDR);
312
Sascha Hauer34f6e152008-09-02 17:16:59 +0200313 writew(NFC_STATUS, host->regs + NFC_CONFIG2);
314
315 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200316 wait_op_done(host, TROP_US_DELAY, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200317
318 /* Status is placed in first word of main buffer */
319 /* get status, then recovery area 1 data */
320 ret = readw(main_buf);
321 writel(store, main_buf);
322
323 return ret;
324}
325
326/* This functions is used by upper layer to checks if device is ready */
327static int mxc_nand_dev_ready(struct mtd_info *mtd)
328{
329 /*
330 * NFC handles R/B internally. Therefore, this function
331 * always returns status as ready.
332 */
333 return 1;
334}
335
336static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
337{
338 /*
339 * If HW ECC is enabled, we turn it on during init. There is
340 * no need to enable again here.
341 */
342}
343
344static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
345 u_char *read_ecc, u_char *calc_ecc)
346{
347 struct nand_chip *nand_chip = mtd->priv;
348 struct mxc_nand_host *host = nand_chip->priv;
349
350 /*
351 * 1-Bit errors are automatically corrected in HW. No need for
352 * additional correction. 2-Bit errors cannot be corrected by
353 * HW ECC, so we need to return failure
354 */
355 uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
356
357 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
358 DEBUG(MTD_DEBUG_LEVEL0,
359 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
360 return -1;
361 }
362
363 return 0;
364}
365
366static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
367 u_char *ecc_code)
368{
369 return 0;
370}
371
372static u_char mxc_nand_read_byte(struct mtd_info *mtd)
373{
374 struct nand_chip *nand_chip = mtd->priv;
375 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200376 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200377
378 /* Check for status request */
379 if (host->status_request)
380 return get_dev_status(host) & 0xFF;
381
Sascha Hauerf8f96082009-06-04 17:12:26 +0200382 ret = *(uint8_t *)(host->data_buf + host->buf_start);
383 host->buf_start++;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200384
385 return ret;
386}
387
388static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
389{
390 struct nand_chip *nand_chip = mtd->priv;
391 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200392 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200393
Sascha Hauerf8f96082009-06-04 17:12:26 +0200394 ret = *(uint16_t *)(host->data_buf + host->buf_start);
395 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200396
397 return ret;
398}
399
400/* Write data of length len to buffer buf. The data to be
401 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
402 * Operation by the NFC, the data is written to NAND Flash */
403static void mxc_nand_write_buf(struct mtd_info *mtd,
404 const u_char *buf, int len)
405{
406 struct nand_chip *nand_chip = mtd->priv;
407 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200408 u16 col = host->buf_start;
409 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200410
Sascha Hauerf8f96082009-06-04 17:12:26 +0200411 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200412
Sascha Hauerf8f96082009-06-04 17:12:26 +0200413 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200414
Sascha Hauerf8f96082009-06-04 17:12:26 +0200415 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200416}
417
418/* Read the data buffer from the NAND Flash. To read the data from NAND
419 * Flash first the data output cycle is initiated by the NFC, which copies
420 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
421 */
422static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
423{
424 struct nand_chip *nand_chip = mtd->priv;
425 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200426 u16 col = host->buf_start;
427 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200428
Sascha Hauerf8f96082009-06-04 17:12:26 +0200429 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200430
Sascha Hauerf8f96082009-06-04 17:12:26 +0200431 memcpy(buf, host->data_buf + col, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200432
Sascha Hauerf8f96082009-06-04 17:12:26 +0200433 host->buf_start += len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200434}
435
436/* Used by the upper layer to verify the data in NAND Flash
437 * with the data in the buf. */
438static int mxc_nand_verify_buf(struct mtd_info *mtd,
439 const u_char *buf, int len)
440{
441 return -EFAULT;
442}
443
444/* This function is used by upper layer for select and
445 * deselect of the NAND chip */
446static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
447{
448 struct nand_chip *nand_chip = mtd->priv;
449 struct mxc_nand_host *host = nand_chip->priv;
450
Sascha Hauer34f6e152008-09-02 17:16:59 +0200451 switch (chip) {
452 case -1:
453 /* Disable the NFC clock */
454 if (host->clk_act) {
455 clk_disable(host->clk);
456 host->clk_act = 0;
457 }
458 break;
459 case 0:
460 /* Enable the NFC clock */
461 if (!host->clk_act) {
462 clk_enable(host->clk);
463 host->clk_act = 1;
464 }
465 break;
466
467 default:
468 break;
469 }
470}
471
Sascha Hauerf8f96082009-06-04 17:12:26 +0200472/*
473 * Function to transfer data to/from spare area.
474 */
475static void copy_spare(struct mtd_info *mtd, bool bfrom)
476{
477 struct nand_chip *this = mtd->priv;
478 struct mxc_nand_host *host = this->priv;
479 u16 i, j;
480 u16 n = mtd->writesize >> 9;
481 u8 *d = host->data_buf + mtd->writesize;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200482 u8 *s = host->spare0;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200483 u16 t = host->spare_len;
484
485 j = (mtd->oobsize / n >> 1) << 1;
486
487 if (bfrom) {
488 for (i = 0; i < n - 1; i++)
489 memcpy(d + i * j, s + i * t, j);
490
491 /* the last section */
492 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
493 } else {
494 for (i = 0; i < n - 1; i++)
495 memcpy(&s[i * t], &d[i * j], j);
496
497 /* the last section */
498 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
499 }
500}
501
Sascha Hauera3e65b62009-06-02 11:47:59 +0200502static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
503{
504 struct nand_chip *nand_chip = mtd->priv;
505 struct mxc_nand_host *host = nand_chip->priv;
506
507 /* Write out column address, if necessary */
508 if (column != -1) {
509 /*
510 * MXC NANDFC can only perform full page+spare or
511 * spare-only read/write. When the upper layers
512 * layers perform a read/write buf operation,
513 * we will used the saved column adress to index into
514 * the full page.
515 */
516 send_addr(host, 0, page_addr == -1);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200517 if (mtd->writesize > 512)
Sascha Hauera3e65b62009-06-02 11:47:59 +0200518 /* another col addr cycle for 2k page */
519 send_addr(host, 0, false);
520 }
521
522 /* Write out page address, if necessary */
523 if (page_addr != -1) {
524 /* paddr_0 - p_addr_7 */
525 send_addr(host, (page_addr & 0xff), false);
526
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200527 if (mtd->writesize > 512) {
Sascha Hauera3e65b62009-06-02 11:47:59 +0200528 if (mtd->size >= 0x10000000) {
529 /* paddr_8 - paddr_15 */
530 send_addr(host, (page_addr >> 8) & 0xff, false);
531 send_addr(host, (page_addr >> 16) & 0xff, true);
532 } else
533 /* paddr_8 - paddr_15 */
534 send_addr(host, (page_addr >> 8) & 0xff, true);
535 } else {
536 /* One more address cycle for higher density devices */
537 if (mtd->size >= 0x4000000) {
538 /* paddr_8 - paddr_15 */
539 send_addr(host, (page_addr >> 8) & 0xff, false);
540 send_addr(host, (page_addr >> 16) & 0xff, true);
541 } else
542 /* paddr_8 - paddr_15 */
543 send_addr(host, (page_addr >> 8) & 0xff, true);
544 }
545 }
546}
547
Sascha Hauer34f6e152008-09-02 17:16:59 +0200548/* Used by the upper layer to write command to NAND Flash for
549 * different operations to be carried out on NAND Flash */
550static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
551 int column, int page_addr)
552{
553 struct nand_chip *nand_chip = mtd->priv;
554 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200555
556 DEBUG(MTD_DEBUG_LEVEL3,
557 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
558 command, column, page_addr);
559
560 /* Reset command state information */
561 host->status_request = false;
562
563 /* Command pre-processing step */
564 switch (command) {
565
566 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200567 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200568 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +0200569
570 send_cmd(host, command, true);
571 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200572 break;
573
574 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200575 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +0200576 if (command == NAND_CMD_READ0)
577 host->buf_start = column;
578 else
579 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200580
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200581 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200582 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +0200583
584 send_cmd(host, command, false);
585 mxc_do_addr_cycle(mtd, column, page_addr);
586
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200587 if (mtd->writesize > 512)
Sascha Hauer89121a62009-06-04 17:18:01 +0200588 send_cmd(host, NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200589
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200590 send_page(mtd, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200591
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200592 memcpy(host->data_buf, host->main_area0, mtd->writesize);
Sascha Hauer89121a62009-06-04 17:18:01 +0200593 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200594 break;
595
596 case NAND_CMD_SEQIN:
597 if (column >= mtd->writesize) {
598 /*
599 * FIXME: before send SEQIN command for write OOB,
600 * We must read one page out.
601 * For K9F1GXX has no READ1 command to set current HW
602 * pointer to spare area, we must write the whole page
603 * including OOB together.
604 */
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200605 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200606 /* call ourself to read a page */
607 mxc_nand_command(mtd, NAND_CMD_READ0, 0,
608 page_addr);
609
Sascha Hauerf8f96082009-06-04 17:12:26 +0200610 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200611
612 /* Set program pointer to spare region */
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200613 if (mtd->writesize == 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200614 send_cmd(host, NAND_CMD_READOOB, false);
615 } else {
Sascha Hauerf8f96082009-06-04 17:12:26 +0200616 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200617
618 /* Set program pointer to page start */
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200619 if (mtd->writesize == 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200620 send_cmd(host, NAND_CMD_READ0, false);
621 }
Sascha Hauer89121a62009-06-04 17:18:01 +0200622
623 send_cmd(host, command, false);
624 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200625 break;
626
627 case NAND_CMD_PAGEPROG:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200628 memcpy(host->main_area0, host->data_buf, mtd->writesize);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200629 copy_spare(mtd, false);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200630 send_page(mtd, NFC_INPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200631 send_cmd(host, command, true);
632 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200633 break;
634
635 case NAND_CMD_READID:
Sascha Hauer89121a62009-06-04 17:18:01 +0200636 send_cmd(host, command, true);
637 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200638 send_read_id(host);
Sascha Hauer94671142009-10-05 12:14:21 +0200639 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200640 break;
641
Sascha Hauer89121a62009-06-04 17:18:01 +0200642 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200643 case NAND_CMD_ERASE2:
Sascha Hauer89121a62009-06-04 17:18:01 +0200644 send_cmd(host, command, false);
645 mxc_do_addr_cycle(mtd, column, page_addr);
646
Sascha Hauer34f6e152008-09-02 17:16:59 +0200647 break;
648 }
649}
650
Sascha Hauerf1372052009-10-21 14:25:27 +0200651/*
652 * The generic flash bbt decriptors overlap with our ecc
653 * hardware, so define some i.MX specific ones.
654 */
655static uint8_t bbt_pattern[] = { 'B', 'b', 't', '0' };
656static uint8_t mirror_pattern[] = { '1', 't', 'b', 'B' };
657
658static struct nand_bbt_descr bbt_main_descr = {
659 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
660 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
661 .offs = 0,
662 .len = 4,
663 .veroffs = 4,
664 .maxblocks = 4,
665 .pattern = bbt_pattern,
666};
667
668static struct nand_bbt_descr bbt_mirror_descr = {
669 .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
670 | NAND_BBT_2BIT | NAND_BBT_VERSION | NAND_BBT_PERCHIP,
671 .offs = 0,
672 .len = 4,
673 .veroffs = 4,
674 .maxblocks = 4,
675 .pattern = mirror_pattern,
676};
677
Sascha Hauer34f6e152008-09-02 17:16:59 +0200678static int __init mxcnd_probe(struct platform_device *pdev)
679{
680 struct nand_chip *this;
681 struct mtd_info *mtd;
682 struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
683 struct mxc_nand_host *host;
684 struct resource *res;
685 uint16_t tmp;
686 int err = 0, nr_parts = 0;
Sascha Hauer94671142009-10-05 12:14:21 +0200687 struct nand_ecclayout *oob_smallpage, *oob_largepage;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200688
689 /* Allocate memory for MTD device structure and private data */
Sascha Hauerf8f96082009-06-04 17:12:26 +0200690 host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
691 NAND_MAX_OOBSIZE, GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200692 if (!host)
693 return -ENOMEM;
694
Sascha Hauerf8f96082009-06-04 17:12:26 +0200695 host->data_buf = (uint8_t *)(host + 1);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200696
Sascha Hauer34f6e152008-09-02 17:16:59 +0200697 host->dev = &pdev->dev;
698 /* structures must be linked */
699 this = &host->nand;
700 mtd = &host->mtd;
701 mtd->priv = this;
702 mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700703 mtd->dev.parent = &pdev->dev;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400704 mtd->name = "mxc_nand";
Sascha Hauer34f6e152008-09-02 17:16:59 +0200705
706 /* 50 us command delay time */
707 this->chip_delay = 5;
708
709 this->priv = host;
710 this->dev_ready = mxc_nand_dev_ready;
711 this->cmdfunc = mxc_nand_command;
712 this->select_chip = mxc_nand_select_chip;
713 this->read_byte = mxc_nand_read_byte;
714 this->read_word = mxc_nand_read_word;
715 this->write_buf = mxc_nand_write_buf;
716 this->read_buf = mxc_nand_read_buf;
717 this->verify_buf = mxc_nand_verify_buf;
718
Sascha Hauere65fb002009-02-16 14:29:10 +0100719 host->clk = clk_get(&pdev->dev, "nfc");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400720 if (IS_ERR(host->clk)) {
721 err = PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200722 goto eclk;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400723 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200724
725 clk_enable(host->clk);
726 host->clk_act = 1;
727
728 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
729 if (!res) {
730 err = -ENODEV;
731 goto eres;
732 }
733
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200734 host->base = ioremap(res->start, resource_size(res));
735 if (!host->base) {
Vladimir Barinov8541c112009-04-23 15:47:22 +0400736 err = -ENOMEM;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200737 goto eres;
738 }
739
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200740 host->main_area0 = host->base;
741 host->main_area1 = host->base + 0x200;
Sascha Hauer94671142009-10-05 12:14:21 +0200742
743 if (nfc_is_v21()) {
744 host->regs = host->base + 0x1000;
745 host->spare0 = host->base + 0x1000;
746 host->spare_len = 64;
747 oob_smallpage = &nandv2_hw_eccoob_smallpage;
748 oob_largepage = &nandv2_hw_eccoob_largepage;
749 } else if (nfc_is_v1()) {
750 host->regs = host->base;
751 host->spare0 = host->base + 0x800;
752 host->spare_len = 16;
753 oob_smallpage = &nandv1_hw_eccoob_smallpage;
754 oob_largepage = &nandv1_hw_eccoob_largepage;
755 } else
756 BUG();
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200757
Sascha Hauerf06368f2009-10-05 17:18:42 +0200758 /* disable interrupt and spare enable */
Sascha Hauer34f6e152008-09-02 17:16:59 +0200759 tmp = readw(host->regs + NFC_CONFIG1);
760 tmp |= NFC_INT_MSK;
Sascha Hauerf06368f2009-10-05 17:18:42 +0200761 tmp &= ~NFC_SP_EN;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200762 writew(tmp, host->regs + NFC_CONFIG1);
763
764 init_waitqueue_head(&host->irq_waitq);
765
766 host->irq = platform_get_irq(pdev, 0);
767
768 err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
769 if (err)
770 goto eirq;
771
Sascha Hauer34f6e152008-09-02 17:16:59 +0200772 /* Reset NAND */
773 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
774
775 /* preset operation */
776 /* Unlock the internal RAM Buffer */
777 writew(0x2, host->regs + NFC_CONFIG);
778
779 /* Blocks to be unlocked */
Sascha Hauer94671142009-10-05 12:14:21 +0200780 if (nfc_is_v21()) {
781 writew(0x0, host->regs + NFC_V21_UNLOCKSTART_BLKADDR);
782 writew(0xffff, host->regs + NFC_V21_UNLOCKEND_BLKADDR);
783 this->ecc.bytes = 9;
784 } else if (nfc_is_v1()) {
785 writew(0x0, host->regs + NFC_V1_UNLOCKSTART_BLKADDR);
786 writew(0x4000, host->regs + NFC_V1_UNLOCKEND_BLKADDR);
787 this->ecc.bytes = 3;
788 } else
789 BUG();
Sascha Hauer34f6e152008-09-02 17:16:59 +0200790
791 /* Unlock Block Command for given address range */
792 writew(0x4, host->regs + NFC_WRPROT);
793
Sascha Hauer13e1add2009-10-21 10:39:05 +0200794 this->ecc.size = 512;
Sascha Hauer94671142009-10-05 12:14:21 +0200795 this->ecc.layout = oob_smallpage;
Sascha Hauer13e1add2009-10-21 10:39:05 +0200796
797 if (pdata->hw_ecc) {
798 this->ecc.calculate = mxc_nand_calculate_ecc;
799 this->ecc.hwctl = mxc_nand_enable_hwecc;
800 this->ecc.correct = mxc_nand_correct_data;
801 this->ecc.mode = NAND_ECC_HW;
802 tmp = readw(host->regs + NFC_CONFIG1);
803 tmp |= NFC_ECC_EN;
804 writew(tmp, host->regs + NFC_CONFIG1);
805 } else {
806 this->ecc.mode = NAND_ECC_SOFT;
807 tmp = readw(host->regs + NFC_CONFIG1);
808 tmp &= ~NFC_ECC_EN;
809 writew(tmp, host->regs + NFC_CONFIG1);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200810 }
811
Sascha Hauer13e1add2009-10-21 10:39:05 +0200812 /* NAND bus width determines access funtions used by upper layer */
813 if (pdata->width == 2)
814 this->options |= NAND_BUSWIDTH_16;
815
Sascha Hauerf1372052009-10-21 14:25:27 +0200816 if (pdata->flash_bbt) {
817 this->bbt_td = &bbt_main_descr;
818 this->bbt_md = &bbt_mirror_descr;
819 /* update flash based bbt */
820 this->options |= NAND_USE_FLASH_BBT;
821 }
822
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400823 /* first scan to find the device and get the page size */
824 if (nand_scan_ident(mtd, 1)) {
825 err = -ENXIO;
826 goto escan;
827 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200828
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200829 if (mtd->writesize == 2048)
Sascha Hauer94671142009-10-05 12:14:21 +0200830 this->ecc.layout = oob_largepage;
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400831
832 /* second phase scan */
833 if (nand_scan_tail(mtd)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200834 err = -ENXIO;
835 goto escan;
836 }
837
838 /* Register the partitions */
839#ifdef CONFIG_MTD_PARTITIONS
840 nr_parts =
841 parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
842 if (nr_parts > 0)
843 add_mtd_partitions(mtd, host->parts, nr_parts);
844 else
845#endif
846 {
847 pr_info("Registering %s as whole device\n", mtd->name);
848 add_mtd_device(mtd);
849 }
850
851 platform_set_drvdata(pdev, host);
852
853 return 0;
854
855escan:
Magnus Liljab258fd82009-05-08 21:57:47 +0200856 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200857eirq:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200858 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200859eres:
860 clk_put(host->clk);
861eclk:
862 kfree(host);
863
864 return err;
865}
866
Uwe Kleine-König82613b02009-10-01 10:28:21 +0200867static int __exit mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200868{
869 struct mxc_nand_host *host = platform_get_drvdata(pdev);
870
871 clk_put(host->clk);
872
873 platform_set_drvdata(pdev, NULL);
874
875 nand_release(&host->mtd);
Magnus Liljab258fd82009-05-08 21:57:47 +0200876 free_irq(host->irq, host);
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200877 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200878 kfree(host);
879
880 return 0;
881}
882
883#ifdef CONFIG_PM
884static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
885{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400886 struct mtd_info *mtd = platform_get_drvdata(pdev);
887 struct nand_chip *nand_chip = mtd->priv;
888 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200889 int ret = 0;
890
891 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400892 if (mtd) {
893 ret = mtd->suspend(mtd);
894 /* Disable the NFC clock */
895 clk_disable(host->clk);
896 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200897
898 return ret;
899}
900
901static int mxcnd_resume(struct platform_device *pdev)
902{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400903 struct mtd_info *mtd = platform_get_drvdata(pdev);
904 struct nand_chip *nand_chip = mtd->priv;
905 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200906 int ret = 0;
907
908 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
Sascha Hauer34f6e152008-09-02 17:16:59 +0200909
Vladimir Barinov8541c112009-04-23 15:47:22 +0400910 if (mtd) {
911 /* Enable the NFC clock */
912 clk_enable(host->clk);
913 mtd->resume(mtd);
914 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200915
Sascha Hauer34f6e152008-09-02 17:16:59 +0200916 return ret;
917}
918
919#else
920# define mxcnd_suspend NULL
921# define mxcnd_resume NULL
922#endif /* CONFIG_PM */
923
924static struct platform_driver mxcnd_driver = {
925 .driver = {
926 .name = DRIVER_NAME,
927 },
928 .remove = __exit_p(mxcnd_remove),
929 .suspend = mxcnd_suspend,
930 .resume = mxcnd_resume,
931};
932
933static int __init mxc_nd_init(void)
934{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400935 return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200936}
937
938static void __exit mxc_nd_cleanup(void)
939{
940 /* Unregister the device structure */
941 platform_driver_unregister(&mxcnd_driver);
942}
943
944module_init(mxc_nd_init);
945module_exit(mxc_nd_cleanup);
946
947MODULE_AUTHOR("Freescale Semiconductor, Inc.");
948MODULE_DESCRIPTION("MXC NAND MTD driver");
949MODULE_LICENSE("GPL");