blob: ab6db698cbd8cbbf494b819675fe85a92a1b60f8 [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>
36
37#define DRIVER_NAME "mxc_nand"
38
39/* Addresses for NFC registers */
40#define NFC_BUF_SIZE 0xE00
41#define NFC_BUF_ADDR 0xE04
42#define NFC_FLASH_ADDR 0xE06
43#define NFC_FLASH_CMD 0xE08
44#define NFC_CONFIG 0xE0A
45#define NFC_ECC_STATUS_RESULT 0xE0C
46#define NFC_RSLTMAIN_AREA 0xE0E
47#define NFC_RSLTSPARE_AREA 0xE10
48#define NFC_WRPROT 0xE12
49#define NFC_UNLOCKSTART_BLKADDR 0xE14
50#define NFC_UNLOCKEND_BLKADDR 0xE16
51#define NFC_NF_WRPRST 0xE18
52#define NFC_CONFIG1 0xE1A
53#define NFC_CONFIG2 0xE1C
54
55/* Addresses for NFC RAM BUFFER Main area 0 */
56#define MAIN_AREA0 0x000
57#define MAIN_AREA1 0x200
58#define MAIN_AREA2 0x400
59#define MAIN_AREA3 0x600
60
61/* Addresses for NFC SPARE BUFFER Spare area 0 */
62#define SPARE_AREA0 0x800
63#define SPARE_AREA1 0x810
64#define SPARE_AREA2 0x820
65#define SPARE_AREA3 0x830
66
67/* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
68 * for Command operation */
69#define NFC_CMD 0x1
70
71/* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
72 * for Address operation */
73#define NFC_ADDR 0x2
74
75/* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
76 * for Input operation */
77#define NFC_INPUT 0x4
78
79/* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
80 * for Data Output operation */
81#define NFC_OUTPUT 0x8
82
83/* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
84 * for Read ID operation */
85#define NFC_ID 0x10
86
87/* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
88 * for Read Status operation */
89#define NFC_STATUS 0x20
90
91/* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
92 * Status operation */
93#define NFC_INT 0x8000
94
95#define NFC_SP_EN (1 << 2)
96#define NFC_ECC_EN (1 << 3)
97#define NFC_INT_MSK (1 << 4)
98#define NFC_BIG (1 << 5)
99#define NFC_RST (1 << 6)
100#define NFC_CE (1 << 7)
101#define NFC_ONE_CYCLE (1 << 8)
102
103struct mxc_nand_host {
104 struct mtd_info mtd;
105 struct nand_chip nand;
106 struct mtd_partition *parts;
107 struct device *dev;
108
109 void __iomem *regs;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200110 int status_request;
111 int pagesize_2k;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200112 struct clk *clk;
113 int clk_act;
114 int irq;
115
116 wait_queue_head_t irq_waitq;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200117
118 uint8_t *data_buf;
119 unsigned int buf_start;
120 int spare_len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200121};
122
123/* Define delays in microsec for NAND device operations */
124#define TROP_US_DELAY 2000
Sascha Hauer34f6e152008-09-02 17:16:59 +0200125
126/* OOB placement block for use with hardware ecc generation */
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200127static struct nand_ecclayout nand_hw_eccoob_smallpage = {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200128 .eccbytes = 5,
129 .eccpos = {6, 7, 8, 9, 10},
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200130 .oobfree = {{0, 5}, {12, 4}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200131};
132
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200133static struct nand_ecclayout nand_hw_eccoob_largepage = {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400134 .eccbytes = 20,
135 .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
136 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
137 .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200138};
139
140#ifdef CONFIG_MTD_PARTITIONS
141static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
142#endif
143
144static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
145{
146 struct mxc_nand_host *host = dev_id;
147
148 uint16_t tmp;
149
150 tmp = readw(host->regs + NFC_CONFIG1);
151 tmp |= NFC_INT_MSK; /* Disable interrupt */
152 writew(tmp, host->regs + NFC_CONFIG1);
153
154 wake_up(&host->irq_waitq);
155
156 return IRQ_HANDLED;
157}
158
159/* This function polls the NANDFC to wait for the basic operation to
160 * complete by checking the INT bit of config2 register.
161 */
162static void wait_op_done(struct mxc_nand_host *host, int max_retries,
Sascha Hauer62465492009-06-04 15:57:20 +0200163 int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200164{
165 uint32_t tmp;
166
167 if (useirq) {
168 if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) {
169
170 tmp = readw(host->regs + NFC_CONFIG1);
171 tmp &= ~NFC_INT_MSK; /* Enable interrupt */
172 writew(tmp, host->regs + NFC_CONFIG1);
173
174 wait_event(host->irq_waitq,
175 readw(host->regs + NFC_CONFIG2) & NFC_INT);
176
177 tmp = readw(host->regs + NFC_CONFIG2);
178 tmp &= ~NFC_INT;
179 writew(tmp, host->regs + NFC_CONFIG2);
180 }
181 } else {
182 while (max_retries-- > 0) {
183 if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
184 tmp = readw(host->regs + NFC_CONFIG2);
185 tmp &= ~NFC_INT;
186 writew(tmp, host->regs + NFC_CONFIG2);
187 break;
188 }
189 udelay(1);
190 }
Roel Kluin43950a62009-06-04 16:24:59 +0200191 if (max_retries < 0)
Sascha Hauer62465492009-06-04 15:57:20 +0200192 DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
193 __func__);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200194 }
195}
196
197/* This function issues the specified command to the NAND device and
198 * waits for completion. */
199static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
200{
201 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
202
203 writew(cmd, host->regs + NFC_FLASH_CMD);
204 writew(NFC_CMD, host->regs + NFC_CONFIG2);
205
206 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200207 wait_op_done(host, TROP_US_DELAY, useirq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200208}
209
210/* This function sends an address (or partial address) to the
211 * NAND device. The address is used to select the source/destination for
212 * a NAND command. */
213static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast)
214{
215 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
216
217 writew(addr, host->regs + NFC_FLASH_ADDR);
218 writew(NFC_ADDR, host->regs + NFC_CONFIG2);
219
220 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200221 wait_op_done(host, TROP_US_DELAY, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200222}
223
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200224static void send_page(struct mxc_nand_host *host, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200225{
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200226 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200227
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200228 if (host->pagesize_2k)
229 bufs = 4;
230 else
231 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200232
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200233 for (i = 0; i < bufs; i++) {
234
235 /* NANDFC buffer 0 is used for page read/write */
236 writew(i, host->regs + NFC_BUF_ADDR);
237
238 writew(ops, host->regs + NFC_CONFIG2);
239
240 /* Wait for operation to complete */
241 wait_op_done(host, TROP_US_DELAY, true);
242 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200243}
244
245/* Request the NANDFC to perform a read of the NAND device ID. */
246static void send_read_id(struct mxc_nand_host *host)
247{
248 struct nand_chip *this = &host->nand;
249 uint16_t tmp;
250
251 /* NANDFC buffer 0 is used for device ID output */
252 writew(0x0, host->regs + NFC_BUF_ADDR);
253
254 /* Read ID into main buffer */
255 tmp = readw(host->regs + NFC_CONFIG1);
256 tmp &= ~NFC_SP_EN;
257 writew(tmp, host->regs + NFC_CONFIG1);
258
259 writew(NFC_ID, host->regs + NFC_CONFIG2);
260
261 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200262 wait_op_done(host, TROP_US_DELAY, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200263
264 if (this->options & NAND_BUSWIDTH_16) {
265 void __iomem *main_buf = host->regs + MAIN_AREA0;
266 /* compress the ID info */
267 writeb(readb(main_buf + 2), main_buf + 1);
268 writeb(readb(main_buf + 4), main_buf + 2);
269 writeb(readb(main_buf + 6), main_buf + 3);
270 writeb(readb(main_buf + 8), main_buf + 4);
271 writeb(readb(main_buf + 10), main_buf + 5);
272 }
Sascha Hauerf8f96082009-06-04 17:12:26 +0200273 memcpy(host->data_buf, host->regs + MAIN_AREA0, 16);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200274}
275
276/* This function requests the NANDFC to perform a read of the
277 * NAND device status and returns the current status. */
278static uint16_t get_dev_status(struct mxc_nand_host *host)
279{
280 void __iomem *main_buf = host->regs + MAIN_AREA1;
281 uint32_t store;
282 uint16_t ret, tmp;
283 /* Issue status request to NAND device */
284
285 /* store the main area1 first word, later do recovery */
286 store = readl(main_buf);
287 /* NANDFC buffer 1 is used for device status to prevent
288 * corruption of read/write buffer on status requests. */
289 writew(1, host->regs + NFC_BUF_ADDR);
290
291 /* Read status into main buffer */
292 tmp = readw(host->regs + NFC_CONFIG1);
293 tmp &= ~NFC_SP_EN;
294 writew(tmp, host->regs + NFC_CONFIG1);
295
296 writew(NFC_STATUS, host->regs + NFC_CONFIG2);
297
298 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200299 wait_op_done(host, TROP_US_DELAY, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200300
301 /* Status is placed in first word of main buffer */
302 /* get status, then recovery area 1 data */
303 ret = readw(main_buf);
304 writel(store, main_buf);
305
306 return ret;
307}
308
309/* This functions is used by upper layer to checks if device is ready */
310static int mxc_nand_dev_ready(struct mtd_info *mtd)
311{
312 /*
313 * NFC handles R/B internally. Therefore, this function
314 * always returns status as ready.
315 */
316 return 1;
317}
318
319static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
320{
321 /*
322 * If HW ECC is enabled, we turn it on during init. There is
323 * no need to enable again here.
324 */
325}
326
327static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
328 u_char *read_ecc, u_char *calc_ecc)
329{
330 struct nand_chip *nand_chip = mtd->priv;
331 struct mxc_nand_host *host = nand_chip->priv;
332
333 /*
334 * 1-Bit errors are automatically corrected in HW. No need for
335 * additional correction. 2-Bit errors cannot be corrected by
336 * HW ECC, so we need to return failure
337 */
338 uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
339
340 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
341 DEBUG(MTD_DEBUG_LEVEL0,
342 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
343 return -1;
344 }
345
346 return 0;
347}
348
349static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
350 u_char *ecc_code)
351{
352 return 0;
353}
354
355static u_char mxc_nand_read_byte(struct mtd_info *mtd)
356{
357 struct nand_chip *nand_chip = mtd->priv;
358 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200359 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200360
361 /* Check for status request */
362 if (host->status_request)
363 return get_dev_status(host) & 0xFF;
364
Sascha Hauerf8f96082009-06-04 17:12:26 +0200365 ret = *(uint8_t *)(host->data_buf + host->buf_start);
366 host->buf_start++;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200367
368 return ret;
369}
370
371static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
372{
373 struct nand_chip *nand_chip = mtd->priv;
374 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200375 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200376
Sascha Hauerf8f96082009-06-04 17:12:26 +0200377 ret = *(uint16_t *)(host->data_buf + host->buf_start);
378 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200379
380 return ret;
381}
382
383/* Write data of length len to buffer buf. The data to be
384 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
385 * Operation by the NFC, the data is written to NAND Flash */
386static void mxc_nand_write_buf(struct mtd_info *mtd,
387 const u_char *buf, int len)
388{
389 struct nand_chip *nand_chip = mtd->priv;
390 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200391 u16 col = host->buf_start;
392 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200393
Sascha Hauerf8f96082009-06-04 17:12:26 +0200394 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200395
Sascha Hauerf8f96082009-06-04 17:12:26 +0200396 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200397
Sascha Hauerf8f96082009-06-04 17:12:26 +0200398 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200399}
400
401/* Read the data buffer from the NAND Flash. To read the data from NAND
402 * Flash first the data output cycle is initiated by the NFC, which copies
403 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
404 */
405static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
406{
407 struct nand_chip *nand_chip = mtd->priv;
408 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200409 u16 col = host->buf_start;
410 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200411
Sascha Hauerf8f96082009-06-04 17:12:26 +0200412 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200413
Sascha Hauerf8f96082009-06-04 17:12:26 +0200414 memcpy(buf, host->data_buf + col, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200415
Sascha Hauerf8f96082009-06-04 17:12:26 +0200416 host->buf_start += len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200417}
418
419/* Used by the upper layer to verify the data in NAND Flash
420 * with the data in the buf. */
421static int mxc_nand_verify_buf(struct mtd_info *mtd,
422 const u_char *buf, int len)
423{
424 return -EFAULT;
425}
426
427/* This function is used by upper layer for select and
428 * deselect of the NAND chip */
429static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
430{
431 struct nand_chip *nand_chip = mtd->priv;
432 struct mxc_nand_host *host = nand_chip->priv;
433
Sascha Hauer34f6e152008-09-02 17:16:59 +0200434 switch (chip) {
435 case -1:
436 /* Disable the NFC clock */
437 if (host->clk_act) {
438 clk_disable(host->clk);
439 host->clk_act = 0;
440 }
441 break;
442 case 0:
443 /* Enable the NFC clock */
444 if (!host->clk_act) {
445 clk_enable(host->clk);
446 host->clk_act = 1;
447 }
448 break;
449
450 default:
451 break;
452 }
453}
454
Sascha Hauerf8f96082009-06-04 17:12:26 +0200455/*
456 * Function to transfer data to/from spare area.
457 */
458static void copy_spare(struct mtd_info *mtd, bool bfrom)
459{
460 struct nand_chip *this = mtd->priv;
461 struct mxc_nand_host *host = this->priv;
462 u16 i, j;
463 u16 n = mtd->writesize >> 9;
464 u8 *d = host->data_buf + mtd->writesize;
465 u8 *s = host->regs + SPARE_AREA0;
466 u16 t = host->spare_len;
467
468 j = (mtd->oobsize / n >> 1) << 1;
469
470 if (bfrom) {
471 for (i = 0; i < n - 1; i++)
472 memcpy(d + i * j, s + i * t, j);
473
474 /* the last section */
475 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
476 } else {
477 for (i = 0; i < n - 1; i++)
478 memcpy(&s[i * t], &d[i * j], j);
479
480 /* the last section */
481 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
482 }
483}
484
Sascha Hauera3e65b62009-06-02 11:47:59 +0200485static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
486{
487 struct nand_chip *nand_chip = mtd->priv;
488 struct mxc_nand_host *host = nand_chip->priv;
489
490 /* Write out column address, if necessary */
491 if (column != -1) {
492 /*
493 * MXC NANDFC can only perform full page+spare or
494 * spare-only read/write. When the upper layers
495 * layers perform a read/write buf operation,
496 * we will used the saved column adress to index into
497 * the full page.
498 */
499 send_addr(host, 0, page_addr == -1);
500 if (host->pagesize_2k)
501 /* another col addr cycle for 2k page */
502 send_addr(host, 0, false);
503 }
504
505 /* Write out page address, if necessary */
506 if (page_addr != -1) {
507 /* paddr_0 - p_addr_7 */
508 send_addr(host, (page_addr & 0xff), false);
509
510 if (host->pagesize_2k) {
511 if (mtd->size >= 0x10000000) {
512 /* paddr_8 - paddr_15 */
513 send_addr(host, (page_addr >> 8) & 0xff, false);
514 send_addr(host, (page_addr >> 16) & 0xff, true);
515 } else
516 /* paddr_8 - paddr_15 */
517 send_addr(host, (page_addr >> 8) & 0xff, true);
518 } else {
519 /* One more address cycle for higher density devices */
520 if (mtd->size >= 0x4000000) {
521 /* paddr_8 - paddr_15 */
522 send_addr(host, (page_addr >> 8) & 0xff, false);
523 send_addr(host, (page_addr >> 16) & 0xff, true);
524 } else
525 /* paddr_8 - paddr_15 */
526 send_addr(host, (page_addr >> 8) & 0xff, true);
527 }
528 }
529}
530
Sascha Hauer34f6e152008-09-02 17:16:59 +0200531/* Used by the upper layer to write command to NAND Flash for
532 * different operations to be carried out on NAND Flash */
533static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
534 int column, int page_addr)
535{
536 struct nand_chip *nand_chip = mtd->priv;
537 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200538
539 DEBUG(MTD_DEBUG_LEVEL3,
540 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
541 command, column, page_addr);
542
543 /* Reset command state information */
544 host->status_request = false;
545
546 /* Command pre-processing step */
547 switch (command) {
548
549 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200550 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200551 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +0200552
553 send_cmd(host, command, true);
554 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200555 break;
556
557 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200558 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +0200559 if (command == NAND_CMD_READ0)
560 host->buf_start = column;
561 else
562 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200563
Sascha Hauer34f6e152008-09-02 17:16:59 +0200564 if (host->pagesize_2k)
565 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +0200566
567 send_cmd(host, command, false);
568 mxc_do_addr_cycle(mtd, column, page_addr);
569
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200570 if (host->pagesize_2k)
Sascha Hauer89121a62009-06-04 17:18:01 +0200571 send_cmd(host, NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200572
573 send_page(host, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200574
575 memcpy(host->data_buf, host->regs + MAIN_AREA0, mtd->writesize);
576 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200577 break;
578
579 case NAND_CMD_SEQIN:
580 if (column >= mtd->writesize) {
581 /*
582 * FIXME: before send SEQIN command for write OOB,
583 * We must read one page out.
584 * For K9F1GXX has no READ1 command to set current HW
585 * pointer to spare area, we must write the whole page
586 * including OOB together.
587 */
588 if (host->pagesize_2k)
589 /* call ourself to read a page */
590 mxc_nand_command(mtd, NAND_CMD_READ0, 0,
591 page_addr);
592
Sascha Hauerf8f96082009-06-04 17:12:26 +0200593 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200594
595 /* Set program pointer to spare region */
596 if (!host->pagesize_2k)
597 send_cmd(host, NAND_CMD_READOOB, false);
598 } else {
Sascha Hauerf8f96082009-06-04 17:12:26 +0200599 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200600
601 /* Set program pointer to page start */
602 if (!host->pagesize_2k)
603 send_cmd(host, NAND_CMD_READ0, false);
604 }
Sascha Hauer89121a62009-06-04 17:18:01 +0200605
606 send_cmd(host, command, false);
607 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200608 break;
609
610 case NAND_CMD_PAGEPROG:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200611 memcpy(host->regs + MAIN_AREA0, host->data_buf, mtd->writesize);
612 copy_spare(mtd, false);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200613 send_page(host, NFC_INPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200614 send_cmd(host, command, true);
615 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200616 break;
617
618 case NAND_CMD_READID:
Sascha Hauer89121a62009-06-04 17:18:01 +0200619 send_cmd(host, command, true);
620 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200621 send_read_id(host);
622 break;
623
Sascha Hauer89121a62009-06-04 17:18:01 +0200624 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200625 case NAND_CMD_ERASE2:
Sascha Hauer89121a62009-06-04 17:18:01 +0200626 send_cmd(host, command, false);
627 mxc_do_addr_cycle(mtd, column, page_addr);
628
Sascha Hauer34f6e152008-09-02 17:16:59 +0200629 break;
630 }
631}
632
633static int __init mxcnd_probe(struct platform_device *pdev)
634{
635 struct nand_chip *this;
636 struct mtd_info *mtd;
637 struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
638 struct mxc_nand_host *host;
639 struct resource *res;
640 uint16_t tmp;
641 int err = 0, nr_parts = 0;
642
643 /* Allocate memory for MTD device structure and private data */
Sascha Hauerf8f96082009-06-04 17:12:26 +0200644 host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
645 NAND_MAX_OOBSIZE, GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200646 if (!host)
647 return -ENOMEM;
648
Sascha Hauerf8f96082009-06-04 17:12:26 +0200649 host->data_buf = (uint8_t *)(host + 1);
650 host->spare_len = 16;
651
Sascha Hauer34f6e152008-09-02 17:16:59 +0200652 host->dev = &pdev->dev;
653 /* structures must be linked */
654 this = &host->nand;
655 mtd = &host->mtd;
656 mtd->priv = this;
657 mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700658 mtd->dev.parent = &pdev->dev;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400659 mtd->name = "mxc_nand";
Sascha Hauer34f6e152008-09-02 17:16:59 +0200660
661 /* 50 us command delay time */
662 this->chip_delay = 5;
663
664 this->priv = host;
665 this->dev_ready = mxc_nand_dev_ready;
666 this->cmdfunc = mxc_nand_command;
667 this->select_chip = mxc_nand_select_chip;
668 this->read_byte = mxc_nand_read_byte;
669 this->read_word = mxc_nand_read_word;
670 this->write_buf = mxc_nand_write_buf;
671 this->read_buf = mxc_nand_read_buf;
672 this->verify_buf = mxc_nand_verify_buf;
673
Sascha Hauere65fb002009-02-16 14:29:10 +0100674 host->clk = clk_get(&pdev->dev, "nfc");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400675 if (IS_ERR(host->clk)) {
676 err = PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200677 goto eclk;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400678 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200679
680 clk_enable(host->clk);
681 host->clk_act = 1;
682
683 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
684 if (!res) {
685 err = -ENODEV;
686 goto eres;
687 }
688
Sascha Hauerd970a072009-06-04 16:16:01 +0200689 host->regs = ioremap(res->start, resource_size(res));
Sascha Hauer34f6e152008-09-02 17:16:59 +0200690 if (!host->regs) {
Vladimir Barinov8541c112009-04-23 15:47:22 +0400691 err = -ENOMEM;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200692 goto eres;
693 }
694
695 tmp = readw(host->regs + NFC_CONFIG1);
696 tmp |= NFC_INT_MSK;
697 writew(tmp, host->regs + NFC_CONFIG1);
698
699 init_waitqueue_head(&host->irq_waitq);
700
701 host->irq = platform_get_irq(pdev, 0);
702
703 err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
704 if (err)
705 goto eirq;
706
Sascha Hauer34f6e152008-09-02 17:16:59 +0200707 /* Reset NAND */
708 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
709
710 /* preset operation */
711 /* Unlock the internal RAM Buffer */
712 writew(0x2, host->regs + NFC_CONFIG);
713
714 /* Blocks to be unlocked */
715 writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
716 writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
717
718 /* Unlock Block Command for given address range */
719 writew(0x4, host->regs + NFC_WRPROT);
720
Sascha Hauer13e1add2009-10-21 10:39:05 +0200721 this->ecc.size = 512;
722 this->ecc.bytes = 3;
723 this->ecc.layout = &nand_hw_eccoob_smallpage;
724
725 if (pdata->hw_ecc) {
726 this->ecc.calculate = mxc_nand_calculate_ecc;
727 this->ecc.hwctl = mxc_nand_enable_hwecc;
728 this->ecc.correct = mxc_nand_correct_data;
729 this->ecc.mode = NAND_ECC_HW;
730 tmp = readw(host->regs + NFC_CONFIG1);
731 tmp |= NFC_ECC_EN;
732 writew(tmp, host->regs + NFC_CONFIG1);
733 } else {
734 this->ecc.mode = NAND_ECC_SOFT;
735 tmp = readw(host->regs + NFC_CONFIG1);
736 tmp &= ~NFC_ECC_EN;
737 writew(tmp, host->regs + NFC_CONFIG1);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200738 }
739
Sascha Hauer13e1add2009-10-21 10:39:05 +0200740 /* NAND bus width determines access funtions used by upper layer */
741 if (pdata->width == 2)
742 this->options |= NAND_BUSWIDTH_16;
743
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400744 /* first scan to find the device and get the page size */
745 if (nand_scan_ident(mtd, 1)) {
746 err = -ENXIO;
747 goto escan;
748 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200749
Sascha Hauer13e1add2009-10-21 10:39:05 +0200750 if (mtd->writesize == 2048) {
751 host->pagesize_2k = 1;
752 this->ecc.layout = &nand_hw_eccoob_largepage;
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400753 }
754
755 /* second phase scan */
756 if (nand_scan_tail(mtd)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200757 err = -ENXIO;
758 goto escan;
759 }
760
761 /* Register the partitions */
762#ifdef CONFIG_MTD_PARTITIONS
763 nr_parts =
764 parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
765 if (nr_parts > 0)
766 add_mtd_partitions(mtd, host->parts, nr_parts);
767 else
768#endif
769 {
770 pr_info("Registering %s as whole device\n", mtd->name);
771 add_mtd_device(mtd);
772 }
773
774 platform_set_drvdata(pdev, host);
775
776 return 0;
777
778escan:
Magnus Liljab258fd82009-05-08 21:57:47 +0200779 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200780eirq:
781 iounmap(host->regs);
782eres:
783 clk_put(host->clk);
784eclk:
785 kfree(host);
786
787 return err;
788}
789
Uwe Kleine-König82613b02009-10-01 10:28:21 +0200790static int __exit mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200791{
792 struct mxc_nand_host *host = platform_get_drvdata(pdev);
793
794 clk_put(host->clk);
795
796 platform_set_drvdata(pdev, NULL);
797
798 nand_release(&host->mtd);
Magnus Liljab258fd82009-05-08 21:57:47 +0200799 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200800 iounmap(host->regs);
801 kfree(host);
802
803 return 0;
804}
805
806#ifdef CONFIG_PM
807static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
808{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400809 struct mtd_info *mtd = platform_get_drvdata(pdev);
810 struct nand_chip *nand_chip = mtd->priv;
811 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200812 int ret = 0;
813
814 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400815 if (mtd) {
816 ret = mtd->suspend(mtd);
817 /* Disable the NFC clock */
818 clk_disable(host->clk);
819 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200820
821 return ret;
822}
823
824static int mxcnd_resume(struct platform_device *pdev)
825{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400826 struct mtd_info *mtd = platform_get_drvdata(pdev);
827 struct nand_chip *nand_chip = mtd->priv;
828 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200829 int ret = 0;
830
831 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
Sascha Hauer34f6e152008-09-02 17:16:59 +0200832
Vladimir Barinov8541c112009-04-23 15:47:22 +0400833 if (mtd) {
834 /* Enable the NFC clock */
835 clk_enable(host->clk);
836 mtd->resume(mtd);
837 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200838
Sascha Hauer34f6e152008-09-02 17:16:59 +0200839 return ret;
840}
841
842#else
843# define mxcnd_suspend NULL
844# define mxcnd_resume NULL
845#endif /* CONFIG_PM */
846
847static struct platform_driver mxcnd_driver = {
848 .driver = {
849 .name = DRIVER_NAME,
850 },
851 .remove = __exit_p(mxcnd_remove),
852 .suspend = mxcnd_suspend,
853 .resume = mxcnd_resume,
854};
855
856static int __init mxc_nd_init(void)
857{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400858 return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200859}
860
861static void __exit mxc_nd_cleanup(void)
862{
863 /* Unregister the device structure */
864 platform_driver_unregister(&mxcnd_driver);
865}
866
867module_init(mxc_nd_init);
868module_exit(mxc_nd_cleanup);
869
870MODULE_AUTHOR("Freescale Semiconductor, Inc.");
871MODULE_DESCRIPTION("MXC NAND MTD driver");
872MODULE_LICENSE("GPL");