blob: 09b9aab6823d50c978877cfb9c8f0fb07d298410 [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
Sascha Hauer34f6e152008-09-02 17:16:59 +020055/* Set INT to 0, FCMD to 1, rest to 0 in NFC_CONFIG2 Register
56 * for Command operation */
57#define NFC_CMD 0x1
58
59/* Set INT to 0, FADD to 1, rest to 0 in NFC_CONFIG2 Register
60 * for Address operation */
61#define NFC_ADDR 0x2
62
63/* Set INT to 0, FDI to 1, rest to 0 in NFC_CONFIG2 Register
64 * for Input operation */
65#define NFC_INPUT 0x4
66
67/* Set INT to 0, FDO to 001, rest to 0 in NFC_CONFIG2 Register
68 * for Data Output operation */
69#define NFC_OUTPUT 0x8
70
71/* Set INT to 0, FD0 to 010, rest to 0 in NFC_CONFIG2 Register
72 * for Read ID operation */
73#define NFC_ID 0x10
74
75/* Set INT to 0, FDO to 100, rest to 0 in NFC_CONFIG2 Register
76 * for Read Status operation */
77#define NFC_STATUS 0x20
78
79/* Set INT to 1, rest to 0 in NFC_CONFIG2 Register for Read
80 * Status operation */
81#define NFC_INT 0x8000
82
83#define NFC_SP_EN (1 << 2)
84#define NFC_ECC_EN (1 << 3)
85#define NFC_INT_MSK (1 << 4)
86#define NFC_BIG (1 << 5)
87#define NFC_RST (1 << 6)
88#define NFC_CE (1 << 7)
89#define NFC_ONE_CYCLE (1 << 8)
90
91struct mxc_nand_host {
92 struct mtd_info mtd;
93 struct nand_chip nand;
94 struct mtd_partition *parts;
95 struct device *dev;
96
Sascha Hauerc6de7e12009-10-05 11:14:35 +020097 void *spare0;
98 void *main_area0;
99 void *main_area1;
100
101 void __iomem *base;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200102 void __iomem *regs;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200103 int status_request;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200104 struct clk *clk;
105 int clk_act;
106 int irq;
107
108 wait_queue_head_t irq_waitq;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200109
110 uint8_t *data_buf;
111 unsigned int buf_start;
112 int spare_len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200113};
114
115/* Define delays in microsec for NAND device operations */
116#define TROP_US_DELAY 2000
Sascha Hauer34f6e152008-09-02 17:16:59 +0200117
118/* OOB placement block for use with hardware ecc generation */
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200119static struct nand_ecclayout nand_hw_eccoob_smallpage = {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200120 .eccbytes = 5,
121 .eccpos = {6, 7, 8, 9, 10},
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200122 .oobfree = {{0, 5}, {12, 4}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200123};
124
Sascha Hauer8c1fd892009-10-21 10:22:01 +0200125static struct nand_ecclayout nand_hw_eccoob_largepage = {
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400126 .eccbytes = 20,
127 .eccpos = {6, 7, 8, 9, 10, 22, 23, 24, 25, 26,
128 38, 39, 40, 41, 42, 54, 55, 56, 57, 58},
129 .oobfree = {{2, 4}, {11, 10}, {27, 10}, {43, 10}, {59, 5}, }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200130};
131
132#ifdef CONFIG_MTD_PARTITIONS
133static const char *part_probes[] = { "RedBoot", "cmdlinepart", NULL };
134#endif
135
136static irqreturn_t mxc_nfc_irq(int irq, void *dev_id)
137{
138 struct mxc_nand_host *host = dev_id;
139
140 uint16_t tmp;
141
142 tmp = readw(host->regs + NFC_CONFIG1);
143 tmp |= NFC_INT_MSK; /* Disable interrupt */
144 writew(tmp, host->regs + NFC_CONFIG1);
145
146 wake_up(&host->irq_waitq);
147
148 return IRQ_HANDLED;
149}
150
151/* This function polls the NANDFC to wait for the basic operation to
152 * complete by checking the INT bit of config2 register.
153 */
154static void wait_op_done(struct mxc_nand_host *host, int max_retries,
Sascha Hauer62465492009-06-04 15:57:20 +0200155 int useirq)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200156{
157 uint32_t tmp;
158
159 if (useirq) {
160 if ((readw(host->regs + NFC_CONFIG2) & NFC_INT) == 0) {
161
162 tmp = readw(host->regs + NFC_CONFIG1);
163 tmp &= ~NFC_INT_MSK; /* Enable interrupt */
164 writew(tmp, host->regs + NFC_CONFIG1);
165
166 wait_event(host->irq_waitq,
167 readw(host->regs + NFC_CONFIG2) & NFC_INT);
168
169 tmp = readw(host->regs + NFC_CONFIG2);
170 tmp &= ~NFC_INT;
171 writew(tmp, host->regs + NFC_CONFIG2);
172 }
173 } else {
174 while (max_retries-- > 0) {
175 if (readw(host->regs + NFC_CONFIG2) & NFC_INT) {
176 tmp = readw(host->regs + NFC_CONFIG2);
177 tmp &= ~NFC_INT;
178 writew(tmp, host->regs + NFC_CONFIG2);
179 break;
180 }
181 udelay(1);
182 }
Roel Kluin43950a62009-06-04 16:24:59 +0200183 if (max_retries < 0)
Sascha Hauer62465492009-06-04 15:57:20 +0200184 DEBUG(MTD_DEBUG_LEVEL0, "%s: INT not set\n",
185 __func__);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200186 }
187}
188
189/* This function issues the specified command to the NAND device and
190 * waits for completion. */
191static void send_cmd(struct mxc_nand_host *host, uint16_t cmd, int useirq)
192{
193 DEBUG(MTD_DEBUG_LEVEL3, "send_cmd(host, 0x%x, %d)\n", cmd, useirq);
194
195 writew(cmd, host->regs + NFC_FLASH_CMD);
196 writew(NFC_CMD, host->regs + NFC_CONFIG2);
197
198 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200199 wait_op_done(host, TROP_US_DELAY, useirq);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200200}
201
202/* This function sends an address (or partial address) to the
203 * NAND device. The address is used to select the source/destination for
204 * a NAND command. */
205static void send_addr(struct mxc_nand_host *host, uint16_t addr, int islast)
206{
207 DEBUG(MTD_DEBUG_LEVEL3, "send_addr(host, 0x%x %d)\n", addr, islast);
208
209 writew(addr, host->regs + NFC_FLASH_ADDR);
210 writew(NFC_ADDR, host->regs + NFC_CONFIG2);
211
212 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200213 wait_op_done(host, TROP_US_DELAY, islast);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200214}
215
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200216static void send_page(struct mtd_info *mtd, unsigned int ops)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200217{
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200218 struct nand_chip *nand_chip = mtd->priv;
219 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200220 int bufs, i;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200221
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200222 if (mtd->writesize > 512)
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200223 bufs = 4;
224 else
225 bufs = 1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200226
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200227 for (i = 0; i < bufs; i++) {
228
229 /* NANDFC buffer 0 is used for page read/write */
230 writew(i, host->regs + NFC_BUF_ADDR);
231
232 writew(ops, host->regs + NFC_CONFIG2);
233
234 /* Wait for operation to complete */
235 wait_op_done(host, TROP_US_DELAY, true);
236 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200237}
238
239/* Request the NANDFC to perform a read of the NAND device ID. */
240static void send_read_id(struct mxc_nand_host *host)
241{
242 struct nand_chip *this = &host->nand;
243 uint16_t tmp;
244
245 /* NANDFC buffer 0 is used for device ID output */
246 writew(0x0, host->regs + NFC_BUF_ADDR);
247
248 /* Read ID into main buffer */
249 tmp = readw(host->regs + NFC_CONFIG1);
250 tmp &= ~NFC_SP_EN;
251 writew(tmp, host->regs + NFC_CONFIG1);
252
253 writew(NFC_ID, host->regs + NFC_CONFIG2);
254
255 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200256 wait_op_done(host, TROP_US_DELAY, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200257
258 if (this->options & NAND_BUSWIDTH_16) {
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200259 void __iomem *main_buf = host->main_area0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200260 /* compress the ID info */
261 writeb(readb(main_buf + 2), main_buf + 1);
262 writeb(readb(main_buf + 4), main_buf + 2);
263 writeb(readb(main_buf + 6), main_buf + 3);
264 writeb(readb(main_buf + 8), main_buf + 4);
265 writeb(readb(main_buf + 10), main_buf + 5);
266 }
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200267 memcpy(host->data_buf, host->main_area0, 16);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200268}
269
270/* This function requests the NANDFC to perform a read of the
271 * NAND device status and returns the current status. */
272static uint16_t get_dev_status(struct mxc_nand_host *host)
273{
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200274 void __iomem *main_buf = host->main_area1;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200275 uint32_t store;
276 uint16_t ret, tmp;
277 /* Issue status request to NAND device */
278
279 /* store the main area1 first word, later do recovery */
280 store = readl(main_buf);
281 /* NANDFC buffer 1 is used for device status to prevent
282 * corruption of read/write buffer on status requests. */
283 writew(1, host->regs + NFC_BUF_ADDR);
284
285 /* Read status into main buffer */
286 tmp = readw(host->regs + NFC_CONFIG1);
287 tmp &= ~NFC_SP_EN;
288 writew(tmp, host->regs + NFC_CONFIG1);
289
290 writew(NFC_STATUS, host->regs + NFC_CONFIG2);
291
292 /* Wait for operation to complete */
Sascha Hauer62465492009-06-04 15:57:20 +0200293 wait_op_done(host, TROP_US_DELAY, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200294
295 /* Status is placed in first word of main buffer */
296 /* get status, then recovery area 1 data */
297 ret = readw(main_buf);
298 writel(store, main_buf);
299
300 return ret;
301}
302
303/* This functions is used by upper layer to checks if device is ready */
304static int mxc_nand_dev_ready(struct mtd_info *mtd)
305{
306 /*
307 * NFC handles R/B internally. Therefore, this function
308 * always returns status as ready.
309 */
310 return 1;
311}
312
313static void mxc_nand_enable_hwecc(struct mtd_info *mtd, int mode)
314{
315 /*
316 * If HW ECC is enabled, we turn it on during init. There is
317 * no need to enable again here.
318 */
319}
320
321static int mxc_nand_correct_data(struct mtd_info *mtd, u_char *dat,
322 u_char *read_ecc, u_char *calc_ecc)
323{
324 struct nand_chip *nand_chip = mtd->priv;
325 struct mxc_nand_host *host = nand_chip->priv;
326
327 /*
328 * 1-Bit errors are automatically corrected in HW. No need for
329 * additional correction. 2-Bit errors cannot be corrected by
330 * HW ECC, so we need to return failure
331 */
332 uint16_t ecc_status = readw(host->regs + NFC_ECC_STATUS_RESULT);
333
334 if (((ecc_status & 0x3) == 2) || ((ecc_status >> 2) == 2)) {
335 DEBUG(MTD_DEBUG_LEVEL0,
336 "MXC_NAND: HWECC uncorrectable 2-bit ECC error\n");
337 return -1;
338 }
339
340 return 0;
341}
342
343static int mxc_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
344 u_char *ecc_code)
345{
346 return 0;
347}
348
349static u_char mxc_nand_read_byte(struct mtd_info *mtd)
350{
351 struct nand_chip *nand_chip = mtd->priv;
352 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200353 uint8_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200354
355 /* Check for status request */
356 if (host->status_request)
357 return get_dev_status(host) & 0xFF;
358
Sascha Hauerf8f96082009-06-04 17:12:26 +0200359 ret = *(uint8_t *)(host->data_buf + host->buf_start);
360 host->buf_start++;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200361
362 return ret;
363}
364
365static uint16_t mxc_nand_read_word(struct mtd_info *mtd)
366{
367 struct nand_chip *nand_chip = mtd->priv;
368 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200369 uint16_t ret;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200370
Sascha Hauerf8f96082009-06-04 17:12:26 +0200371 ret = *(uint16_t *)(host->data_buf + host->buf_start);
372 host->buf_start += 2;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200373
374 return ret;
375}
376
377/* Write data of length len to buffer buf. The data to be
378 * written on NAND Flash is first copied to RAMbuffer. After the Data Input
379 * Operation by the NFC, the data is written to NAND Flash */
380static void mxc_nand_write_buf(struct mtd_info *mtd,
381 const u_char *buf, int len)
382{
383 struct nand_chip *nand_chip = mtd->priv;
384 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200385 u16 col = host->buf_start;
386 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200387
Sascha Hauerf8f96082009-06-04 17:12:26 +0200388 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200389
Sascha Hauerf8f96082009-06-04 17:12:26 +0200390 memcpy(host->data_buf + col, buf, n);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200391
Sascha Hauerf8f96082009-06-04 17:12:26 +0200392 host->buf_start += n;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200393}
394
395/* Read the data buffer from the NAND Flash. To read the data from NAND
396 * Flash first the data output cycle is initiated by the NFC, which copies
397 * the data to RAMbuffer. This data of length len is then copied to buffer buf.
398 */
399static void mxc_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
400{
401 struct nand_chip *nand_chip = mtd->priv;
402 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200403 u16 col = host->buf_start;
404 int n = mtd->oobsize + mtd->writesize - col;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200405
Sascha Hauerf8f96082009-06-04 17:12:26 +0200406 n = min(n, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200407
Sascha Hauerf8f96082009-06-04 17:12:26 +0200408 memcpy(buf, host->data_buf + col, len);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200409
Sascha Hauerf8f96082009-06-04 17:12:26 +0200410 host->buf_start += len;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200411}
412
413/* Used by the upper layer to verify the data in NAND Flash
414 * with the data in the buf. */
415static int mxc_nand_verify_buf(struct mtd_info *mtd,
416 const u_char *buf, int len)
417{
418 return -EFAULT;
419}
420
421/* This function is used by upper layer for select and
422 * deselect of the NAND chip */
423static void mxc_nand_select_chip(struct mtd_info *mtd, int chip)
424{
425 struct nand_chip *nand_chip = mtd->priv;
426 struct mxc_nand_host *host = nand_chip->priv;
427
Sascha Hauer34f6e152008-09-02 17:16:59 +0200428 switch (chip) {
429 case -1:
430 /* Disable the NFC clock */
431 if (host->clk_act) {
432 clk_disable(host->clk);
433 host->clk_act = 0;
434 }
435 break;
436 case 0:
437 /* Enable the NFC clock */
438 if (!host->clk_act) {
439 clk_enable(host->clk);
440 host->clk_act = 1;
441 }
442 break;
443
444 default:
445 break;
446 }
447}
448
Sascha Hauerf8f96082009-06-04 17:12:26 +0200449/*
450 * Function to transfer data to/from spare area.
451 */
452static void copy_spare(struct mtd_info *mtd, bool bfrom)
453{
454 struct nand_chip *this = mtd->priv;
455 struct mxc_nand_host *host = this->priv;
456 u16 i, j;
457 u16 n = mtd->writesize >> 9;
458 u8 *d = host->data_buf + mtd->writesize;
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200459 u8 *s = host->spare0;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200460 u16 t = host->spare_len;
461
462 j = (mtd->oobsize / n >> 1) << 1;
463
464 if (bfrom) {
465 for (i = 0; i < n - 1; i++)
466 memcpy(d + i * j, s + i * t, j);
467
468 /* the last section */
469 memcpy(d + i * j, s + i * t, mtd->oobsize - i * j);
470 } else {
471 for (i = 0; i < n - 1; i++)
472 memcpy(&s[i * t], &d[i * j], j);
473
474 /* the last section */
475 memcpy(&s[i * t], &d[i * j], mtd->oobsize - i * j);
476 }
477}
478
Sascha Hauera3e65b62009-06-02 11:47:59 +0200479static void mxc_do_addr_cycle(struct mtd_info *mtd, int column, int page_addr)
480{
481 struct nand_chip *nand_chip = mtd->priv;
482 struct mxc_nand_host *host = nand_chip->priv;
483
484 /* Write out column address, if necessary */
485 if (column != -1) {
486 /*
487 * MXC NANDFC can only perform full page+spare or
488 * spare-only read/write. When the upper layers
489 * layers perform a read/write buf operation,
490 * we will used the saved column adress to index into
491 * the full page.
492 */
493 send_addr(host, 0, page_addr == -1);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200494 if (mtd->writesize > 512)
Sascha Hauera3e65b62009-06-02 11:47:59 +0200495 /* another col addr cycle for 2k page */
496 send_addr(host, 0, false);
497 }
498
499 /* Write out page address, if necessary */
500 if (page_addr != -1) {
501 /* paddr_0 - p_addr_7 */
502 send_addr(host, (page_addr & 0xff), false);
503
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200504 if (mtd->writesize > 512) {
Sascha Hauera3e65b62009-06-02 11:47:59 +0200505 if (mtd->size >= 0x10000000) {
506 /* paddr_8 - paddr_15 */
507 send_addr(host, (page_addr >> 8) & 0xff, false);
508 send_addr(host, (page_addr >> 16) & 0xff, true);
509 } else
510 /* paddr_8 - paddr_15 */
511 send_addr(host, (page_addr >> 8) & 0xff, true);
512 } else {
513 /* One more address cycle for higher density devices */
514 if (mtd->size >= 0x4000000) {
515 /* paddr_8 - paddr_15 */
516 send_addr(host, (page_addr >> 8) & 0xff, false);
517 send_addr(host, (page_addr >> 16) & 0xff, true);
518 } else
519 /* paddr_8 - paddr_15 */
520 send_addr(host, (page_addr >> 8) & 0xff, true);
521 }
522 }
523}
524
Sascha Hauer34f6e152008-09-02 17:16:59 +0200525/* Used by the upper layer to write command to NAND Flash for
526 * different operations to be carried out on NAND Flash */
527static void mxc_nand_command(struct mtd_info *mtd, unsigned command,
528 int column, int page_addr)
529{
530 struct nand_chip *nand_chip = mtd->priv;
531 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200532
533 DEBUG(MTD_DEBUG_LEVEL3,
534 "mxc_nand_command (cmd = 0x%x, col = 0x%x, page = 0x%x)\n",
535 command, column, page_addr);
536
537 /* Reset command state information */
538 host->status_request = false;
539
540 /* Command pre-processing step */
541 switch (command) {
542
543 case NAND_CMD_STATUS:
Sascha Hauerf8f96082009-06-04 17:12:26 +0200544 host->buf_start = 0;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200545 host->status_request = true;
Sascha Hauer89121a62009-06-04 17:18:01 +0200546
547 send_cmd(host, command, true);
548 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200549 break;
550
551 case NAND_CMD_READ0:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200552 case NAND_CMD_READOOB:
Sascha Hauer89121a62009-06-04 17:18:01 +0200553 if (command == NAND_CMD_READ0)
554 host->buf_start = column;
555 else
556 host->buf_start = column + mtd->writesize;
Sascha Hauerf8f96082009-06-04 17:12:26 +0200557
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200558 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200559 command = NAND_CMD_READ0; /* only READ0 is valid */
Sascha Hauer89121a62009-06-04 17:18:01 +0200560
561 send_cmd(host, command, false);
562 mxc_do_addr_cycle(mtd, column, page_addr);
563
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200564 if (mtd->writesize > 512)
Sascha Hauer89121a62009-06-04 17:18:01 +0200565 send_cmd(host, NAND_CMD_READSTART, true);
Sascha Hauerc5d23f12009-06-04 17:25:53 +0200566
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200567 send_page(mtd, NFC_OUTPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200568
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200569 memcpy(host->data_buf, host->main_area0, mtd->writesize);
Sascha Hauer89121a62009-06-04 17:18:01 +0200570 copy_spare(mtd, true);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200571 break;
572
573 case NAND_CMD_SEQIN:
574 if (column >= mtd->writesize) {
575 /*
576 * FIXME: before send SEQIN command for write OOB,
577 * We must read one page out.
578 * For K9F1GXX has no READ1 command to set current HW
579 * pointer to spare area, we must write the whole page
580 * including OOB together.
581 */
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200582 if (mtd->writesize > 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200583 /* call ourself to read a page */
584 mxc_nand_command(mtd, NAND_CMD_READ0, 0,
585 page_addr);
586
Sascha Hauerf8f96082009-06-04 17:12:26 +0200587 host->buf_start = column;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200588
589 /* Set program pointer to spare region */
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200590 if (mtd->writesize == 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200591 send_cmd(host, NAND_CMD_READOOB, false);
592 } else {
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 page start */
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200596 if (mtd->writesize == 512)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200597 send_cmd(host, NAND_CMD_READ0, false);
598 }
Sascha Hauer89121a62009-06-04 17:18:01 +0200599
600 send_cmd(host, command, false);
601 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200602 break;
603
604 case NAND_CMD_PAGEPROG:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200605 memcpy(host->main_area0, host->data_buf, mtd->writesize);
Sascha Hauerf8f96082009-06-04 17:12:26 +0200606 copy_spare(mtd, false);
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200607 send_page(mtd, NFC_INPUT);
Sascha Hauer89121a62009-06-04 17:18:01 +0200608 send_cmd(host, command, true);
609 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200610 break;
611
612 case NAND_CMD_READID:
Sascha Hauer89121a62009-06-04 17:18:01 +0200613 send_cmd(host, command, true);
614 mxc_do_addr_cycle(mtd, column, page_addr);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200615 send_read_id(host);
616 break;
617
Sascha Hauer89121a62009-06-04 17:18:01 +0200618 case NAND_CMD_ERASE1:
Sascha Hauer34f6e152008-09-02 17:16:59 +0200619 case NAND_CMD_ERASE2:
Sascha Hauer89121a62009-06-04 17:18:01 +0200620 send_cmd(host, command, false);
621 mxc_do_addr_cycle(mtd, column, page_addr);
622
Sascha Hauer34f6e152008-09-02 17:16:59 +0200623 break;
624 }
625}
626
627static int __init mxcnd_probe(struct platform_device *pdev)
628{
629 struct nand_chip *this;
630 struct mtd_info *mtd;
631 struct mxc_nand_platform_data *pdata = pdev->dev.platform_data;
632 struct mxc_nand_host *host;
633 struct resource *res;
634 uint16_t tmp;
635 int err = 0, nr_parts = 0;
636
637 /* Allocate memory for MTD device structure and private data */
Sascha Hauerf8f96082009-06-04 17:12:26 +0200638 host = kzalloc(sizeof(struct mxc_nand_host) + NAND_MAX_PAGESIZE +
639 NAND_MAX_OOBSIZE, GFP_KERNEL);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200640 if (!host)
641 return -ENOMEM;
642
Sascha Hauerf8f96082009-06-04 17:12:26 +0200643 host->data_buf = (uint8_t *)(host + 1);
644 host->spare_len = 16;
645
Sascha Hauer34f6e152008-09-02 17:16:59 +0200646 host->dev = &pdev->dev;
647 /* structures must be linked */
648 this = &host->nand;
649 mtd = &host->mtd;
650 mtd->priv = this;
651 mtd->owner = THIS_MODULE;
David Brownell87f39f02009-03-26 00:42:50 -0700652 mtd->dev.parent = &pdev->dev;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400653 mtd->name = "mxc_nand";
Sascha Hauer34f6e152008-09-02 17:16:59 +0200654
655 /* 50 us command delay time */
656 this->chip_delay = 5;
657
658 this->priv = host;
659 this->dev_ready = mxc_nand_dev_ready;
660 this->cmdfunc = mxc_nand_command;
661 this->select_chip = mxc_nand_select_chip;
662 this->read_byte = mxc_nand_read_byte;
663 this->read_word = mxc_nand_read_word;
664 this->write_buf = mxc_nand_write_buf;
665 this->read_buf = mxc_nand_read_buf;
666 this->verify_buf = mxc_nand_verify_buf;
667
Sascha Hauere65fb002009-02-16 14:29:10 +0100668 host->clk = clk_get(&pdev->dev, "nfc");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400669 if (IS_ERR(host->clk)) {
670 err = PTR_ERR(host->clk);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200671 goto eclk;
Vladimir Barinov8541c112009-04-23 15:47:22 +0400672 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200673
674 clk_enable(host->clk);
675 host->clk_act = 1;
676
677 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
678 if (!res) {
679 err = -ENODEV;
680 goto eres;
681 }
682
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200683 host->base = ioremap(res->start, resource_size(res));
684 if (!host->base) {
Vladimir Barinov8541c112009-04-23 15:47:22 +0400685 err = -ENOMEM;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200686 goto eres;
687 }
688
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200689 host->regs = host->base;
690 host->main_area0 = host->base;
691 host->main_area1 = host->base + 0x200;
692 host->spare0 = host->base + 0x800;
693
Sascha Hauer34f6e152008-09-02 17:16:59 +0200694 tmp = readw(host->regs + NFC_CONFIG1);
695 tmp |= NFC_INT_MSK;
696 writew(tmp, host->regs + NFC_CONFIG1);
697
698 init_waitqueue_head(&host->irq_waitq);
699
700 host->irq = platform_get_irq(pdev, 0);
701
702 err = request_irq(host->irq, mxc_nfc_irq, 0, "mxc_nd", host);
703 if (err)
704 goto eirq;
705
Sascha Hauer34f6e152008-09-02 17:16:59 +0200706 /* Reset NAND */
707 this->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
708
709 /* preset operation */
710 /* Unlock the internal RAM Buffer */
711 writew(0x2, host->regs + NFC_CONFIG);
712
713 /* Blocks to be unlocked */
714 writew(0x0, host->regs + NFC_UNLOCKSTART_BLKADDR);
715 writew(0x4000, host->regs + NFC_UNLOCKEND_BLKADDR);
716
717 /* Unlock Block Command for given address range */
718 writew(0x4, host->regs + NFC_WRPROT);
719
Sascha Hauer13e1add2009-10-21 10:39:05 +0200720 this->ecc.size = 512;
721 this->ecc.bytes = 3;
722 this->ecc.layout = &nand_hw_eccoob_smallpage;
723
724 if (pdata->hw_ecc) {
725 this->ecc.calculate = mxc_nand_calculate_ecc;
726 this->ecc.hwctl = mxc_nand_enable_hwecc;
727 this->ecc.correct = mxc_nand_correct_data;
728 this->ecc.mode = NAND_ECC_HW;
729 tmp = readw(host->regs + NFC_CONFIG1);
730 tmp |= NFC_ECC_EN;
731 writew(tmp, host->regs + NFC_CONFIG1);
732 } else {
733 this->ecc.mode = NAND_ECC_SOFT;
734 tmp = readw(host->regs + NFC_CONFIG1);
735 tmp &= ~NFC_ECC_EN;
736 writew(tmp, host->regs + NFC_CONFIG1);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200737 }
738
Sascha Hauer13e1add2009-10-21 10:39:05 +0200739 /* NAND bus width determines access funtions used by upper layer */
740 if (pdata->width == 2)
741 this->options |= NAND_BUSWIDTH_16;
742
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400743 /* first scan to find the device and get the page size */
744 if (nand_scan_ident(mtd, 1)) {
745 err = -ENXIO;
746 goto escan;
747 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200748
Sascha Hauer2d69c7f2009-10-05 11:24:02 +0200749 if (mtd->writesize == 2048)
Sascha Hauer13e1add2009-10-21 10:39:05 +0200750 this->ecc.layout = &nand_hw_eccoob_largepage;
Vladimir Barinovbd3fd622009-05-25 13:06:17 +0400751
752 /* second phase scan */
753 if (nand_scan_tail(mtd)) {
Sascha Hauer34f6e152008-09-02 17:16:59 +0200754 err = -ENXIO;
755 goto escan;
756 }
757
758 /* Register the partitions */
759#ifdef CONFIG_MTD_PARTITIONS
760 nr_parts =
761 parse_mtd_partitions(mtd, part_probes, &host->parts, 0);
762 if (nr_parts > 0)
763 add_mtd_partitions(mtd, host->parts, nr_parts);
764 else
765#endif
766 {
767 pr_info("Registering %s as whole device\n", mtd->name);
768 add_mtd_device(mtd);
769 }
770
771 platform_set_drvdata(pdev, host);
772
773 return 0;
774
775escan:
Magnus Liljab258fd82009-05-08 21:57:47 +0200776 free_irq(host->irq, host);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200777eirq:
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200778 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200779eres:
780 clk_put(host->clk);
781eclk:
782 kfree(host);
783
784 return err;
785}
786
Uwe Kleine-König82613b02009-10-01 10:28:21 +0200787static int __exit mxcnd_remove(struct platform_device *pdev)
Sascha Hauer34f6e152008-09-02 17:16:59 +0200788{
789 struct mxc_nand_host *host = platform_get_drvdata(pdev);
790
791 clk_put(host->clk);
792
793 platform_set_drvdata(pdev, NULL);
794
795 nand_release(&host->mtd);
Magnus Liljab258fd82009-05-08 21:57:47 +0200796 free_irq(host->irq, host);
Sascha Hauerc6de7e12009-10-05 11:14:35 +0200797 iounmap(host->base);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200798 kfree(host);
799
800 return 0;
801}
802
803#ifdef CONFIG_PM
804static int mxcnd_suspend(struct platform_device *pdev, pm_message_t state)
805{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400806 struct mtd_info *mtd = platform_get_drvdata(pdev);
807 struct nand_chip *nand_chip = mtd->priv;
808 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200809 int ret = 0;
810
811 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND suspend\n");
Vladimir Barinov8541c112009-04-23 15:47:22 +0400812 if (mtd) {
813 ret = mtd->suspend(mtd);
814 /* Disable the NFC clock */
815 clk_disable(host->clk);
816 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200817
818 return ret;
819}
820
821static int mxcnd_resume(struct platform_device *pdev)
822{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400823 struct mtd_info *mtd = platform_get_drvdata(pdev);
824 struct nand_chip *nand_chip = mtd->priv;
825 struct mxc_nand_host *host = nand_chip->priv;
Sascha Hauer34f6e152008-09-02 17:16:59 +0200826 int ret = 0;
827
828 DEBUG(MTD_DEBUG_LEVEL0, "MXC_ND : NAND resume\n");
Sascha Hauer34f6e152008-09-02 17:16:59 +0200829
Vladimir Barinov8541c112009-04-23 15:47:22 +0400830 if (mtd) {
831 /* Enable the NFC clock */
832 clk_enable(host->clk);
833 mtd->resume(mtd);
834 }
Sascha Hauer34f6e152008-09-02 17:16:59 +0200835
Sascha Hauer34f6e152008-09-02 17:16:59 +0200836 return ret;
837}
838
839#else
840# define mxcnd_suspend NULL
841# define mxcnd_resume NULL
842#endif /* CONFIG_PM */
843
844static struct platform_driver mxcnd_driver = {
845 .driver = {
846 .name = DRIVER_NAME,
847 },
848 .remove = __exit_p(mxcnd_remove),
849 .suspend = mxcnd_suspend,
850 .resume = mxcnd_resume,
851};
852
853static int __init mxc_nd_init(void)
854{
Vladimir Barinov8541c112009-04-23 15:47:22 +0400855 return platform_driver_probe(&mxcnd_driver, mxcnd_probe);
Sascha Hauer34f6e152008-09-02 17:16:59 +0200856}
857
858static void __exit mxc_nd_cleanup(void)
859{
860 /* Unregister the device structure */
861 platform_driver_unregister(&mxcnd_driver);
862}
863
864module_init(mxc_nd_init);
865module_exit(mxc_nd_cleanup);
866
867MODULE_AUTHOR("Freescale Semiconductor, Inc.");
868MODULE_DESCRIPTION("MXC NAND MTD driver");
869MODULE_LICENSE("GPL");