2 * drivers/mtd/nand/au1550nd.c
4 * Copyright (C) 2004 Embedded Edge, LLC
6 * $Id: au1550nd.c,v 1.13 2005/11/07 11:14:30 gleixner Exp $
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include <linux/slab.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/interrupt.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/nand.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/version.h>
24 /* fixme: this is ugly */
25 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
26 #include <asm/mach-au1x00/au1xxx.h>
28 #include <asm/au1000.h>
29 #ifdef CONFIG_MIPS_PB1550
30 #include <asm/pb1550.h>
32 #ifdef CONFIG_MIPS_DB1550
33 #include <asm/db1x00.h>
38 * MTD structure for NAND controller
40 static struct mtd_info *au1550_mtd = NULL;
41 static void __iomem *p_nand;
42 static int nand_width = 1; /* default x8 */
45 * Define partitions for flash device
47 static const struct mtd_partition partition_info[] = {
51 .size = 8 * 1024 * 1024},
54 .offset = MTDPART_OFS_APPEND,
55 .size = MTDPART_SIZ_FULL}
59 * au_read_byte - read one byte from the chip
60 * @mtd: MTD device structure
62 * read function for 8bit buswith
64 static u_char au_read_byte(struct mtd_info *mtd)
66 struct nand_chip *this = mtd->priv;
67 u_char ret = readb(this->IO_ADDR_R);
73 * au_write_byte - write one byte to the chip
74 * @mtd: MTD device structure
75 * @byte: pointer to data byte to write
77 * write function for 8it buswith
79 static void au_write_byte(struct mtd_info *mtd, u_char byte)
81 struct nand_chip *this = mtd->priv;
82 writeb(byte, this->IO_ADDR_W);
87 * au_read_byte16 - read one byte endianess aware from the chip
88 * @mtd: MTD device structure
90 * read function for 16bit buswith with
91 * endianess conversion
93 static u_char au_read_byte16(struct mtd_info *mtd)
95 struct nand_chip *this = mtd->priv;
96 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
102 * au_write_byte16 - write one byte endianess aware to the chip
103 * @mtd: MTD device structure
104 * @byte: pointer to data byte to write
106 * write function for 16bit buswith with
107 * endianess conversion
109 static void au_write_byte16(struct mtd_info *mtd, u_char byte)
111 struct nand_chip *this = mtd->priv;
112 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
117 * au_read_word - read one word from the chip
118 * @mtd: MTD device structure
120 * read function for 16bit buswith without
121 * endianess conversion
123 static u16 au_read_word(struct mtd_info *mtd)
125 struct nand_chip *this = mtd->priv;
126 u16 ret = readw(this->IO_ADDR_R);
132 * au_write_word - write one word to the chip
133 * @mtd: MTD device structure
134 * @word: data word to write
136 * write function for 16bit buswith without
137 * endianess conversion
139 static void au_write_word(struct mtd_info *mtd, u16 word)
141 struct nand_chip *this = mtd->priv;
142 writew(word, this->IO_ADDR_W);
147 * au_write_buf - write buffer to chip
148 * @mtd: MTD device structure
150 * @len: number of bytes to write
152 * write function for 8bit buswith
154 static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
157 struct nand_chip *this = mtd->priv;
159 for (i = 0; i < len; i++) {
160 writeb(buf[i], this->IO_ADDR_W);
166 * au_read_buf - read chip data into buffer
167 * @mtd: MTD device structure
168 * @buf: buffer to store date
169 * @len: number of bytes to read
171 * read function for 8bit buswith
173 static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
176 struct nand_chip *this = mtd->priv;
178 for (i = 0; i < len; i++) {
179 buf[i] = readb(this->IO_ADDR_R);
185 * au_verify_buf - Verify chip data against buffer
186 * @mtd: MTD device structure
187 * @buf: buffer containing the data to compare
188 * @len: number of bytes to compare
190 * verify function for 8bit buswith
192 static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
195 struct nand_chip *this = mtd->priv;
197 for (i = 0; i < len; i++) {
198 if (buf[i] != readb(this->IO_ADDR_R))
207 * au_write_buf16 - write buffer to chip
208 * @mtd: MTD device structure
210 * @len: number of bytes to write
212 * write function for 16bit buswith
214 static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
217 struct nand_chip *this = mtd->priv;
218 u16 *p = (u16 *) buf;
221 for (i = 0; i < len; i++) {
222 writew(p[i], this->IO_ADDR_W);
229 * au_read_buf16 - read chip data into buffer
230 * @mtd: MTD device structure
231 * @buf: buffer to store date
232 * @len: number of bytes to read
234 * read function for 16bit buswith
236 static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
239 struct nand_chip *this = mtd->priv;
240 u16 *p = (u16 *) buf;
243 for (i = 0; i < len; i++) {
244 p[i] = readw(this->IO_ADDR_R);
250 * au_verify_buf16 - Verify chip data against buffer
251 * @mtd: MTD device structure
252 * @buf: buffer containing the data to compare
253 * @len: number of bytes to compare
255 * verify function for 16bit buswith
257 static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
260 struct nand_chip *this = mtd->priv;
261 u16 *p = (u16 *) buf;
264 for (i = 0; i < len; i++) {
265 if (p[i] != readw(this->IO_ADDR_R))
273 static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
275 register struct nand_chip *this = mtd->priv;
279 case NAND_CTL_SETCLE:
280 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
283 case NAND_CTL_CLRCLE:
284 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
287 case NAND_CTL_SETALE:
288 this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
291 case NAND_CTL_CLRALE:
292 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
293 /* FIXME: Nobody knows why this is necessary,
294 * but it works only that way */
298 case NAND_CTL_SETNCE:
299 /* assert (force assert) chip enable */
300 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
303 case NAND_CTL_CLRNCE:
304 /* deassert chip enable */
305 au_writel(0, MEM_STNDCTL);
309 this->IO_ADDR_R = this->IO_ADDR_W;
311 /* Drain the writebuffer */
315 int au1550_device_ready(struct mtd_info *mtd)
317 int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
323 * au1550_select_chip - control -CE line
324 * Forbid driving -CE manually permitting the NAND controller to do this.
325 * Keeping -CE asserted during the whole sector reads interferes with the
326 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
327 * We only have to hold -CE low for the NAND read commands since the flash
328 * chip needs it to be asserted during chip not ready time but the NAND
329 * controller keeps it released.
331 * @mtd: MTD device structure
332 * @chip: chipnumber to select, -1 for deselect
334 static void au1550_select_chip(struct mtd_info *mtd, int chip)
339 * au1550_command - Send command to NAND device
340 * @mtd: MTD device structure
341 * @command: the command to be sent
342 * @column: the column address for this command, -1 if none
343 * @page_addr: the page address for this command, -1 if none
345 static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
347 register struct nand_chip *this = mtd->priv;
348 int ce_override = 0, i;
351 /* Begin command latch cycle */
352 this->hwcontrol(mtd, NAND_CTL_SETCLE);
354 * Write out the command to the device.
356 if (command == NAND_CMD_SEQIN) {
359 if (column >= mtd->writesize) {
361 column -= mtd->writesize;
362 readcmd = NAND_CMD_READOOB;
363 } else if (column < 256) {
364 /* First 256 bytes --> READ0 */
365 readcmd = NAND_CMD_READ0;
368 readcmd = NAND_CMD_READ1;
370 this->write_byte(mtd, readcmd);
372 this->write_byte(mtd, command);
374 /* Set ALE and clear CLE to start address cycle */
375 this->hwcontrol(mtd, NAND_CTL_CLRCLE);
377 if (column != -1 || page_addr != -1) {
378 this->hwcontrol(mtd, NAND_CTL_SETALE);
380 /* Serially input address */
382 /* Adjust columns for 16 bit buswidth */
383 if (this->options & NAND_BUSWIDTH_16)
385 this->write_byte(mtd, column);
387 if (page_addr != -1) {
388 this->write_byte(mtd, (u8)(page_addr & 0xff));
390 if (command == NAND_CMD_READ0 ||
391 command == NAND_CMD_READ1 ||
392 command == NAND_CMD_READOOB) {
394 * NAND controller will release -CE after
395 * the last address byte is written, so we'll
396 * have to forcibly assert it. No interrupts
397 * are allowed while we do this as we don't
398 * want the NOR flash or PCMCIA drivers to
399 * steal our precious bytes of data...
402 local_irq_save(flags);
403 this->hwcontrol(mtd, NAND_CTL_SETNCE);
406 this->write_byte(mtd, (u8)(page_addr >> 8));
408 /* One more address cycle for devices > 32MiB */
409 if (this->chipsize > (32 << 20))
410 this->write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
412 /* Latch in address */
413 this->hwcontrol(mtd, NAND_CTL_CLRALE);
417 * Program and erase have their own busy handlers.
418 * Status and sequential in need no delay.
422 case NAND_CMD_PAGEPROG:
423 case NAND_CMD_ERASE1:
424 case NAND_CMD_ERASE2:
426 case NAND_CMD_STATUS:
434 case NAND_CMD_READOOB:
435 /* Check if we're really driving -CE low (just in case) */
436 if (unlikely(!ce_override))
439 /* Apply a short delay always to ensure that we do wait tWB. */
441 /* Wait for a chip to become ready... */
442 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
445 /* Release -CE and re-enable interrupts. */
446 this->hwcontrol(mtd, NAND_CTL_CLRNCE);
447 local_irq_restore(flags);
450 /* Apply this short delay always to ensure that we do wait tWB. */
453 while(!this->dev_ready(mtd));
458 * Main initialization routine
460 static int __init au1xxx_nand_init(void)
462 struct nand_chip *this;
463 u16 boot_swapboot = 0; /* default value */
468 /* Allocate memory for MTD device structure and private data */
469 au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
471 printk("Unable to allocate NAND MTD dev structure.\n");
475 /* Get pointer to private data */
476 this = (struct nand_chip *)(&au1550_mtd[1]);
478 /* Initialize structures */
479 memset(au1550_mtd, 0, sizeof(struct mtd_info));
480 memset(this, 0, sizeof(struct nand_chip));
482 /* Link the private data with the MTD structure */
483 au1550_mtd->priv = this;
484 au1550_mtd->owner = THIS_MODULE;
487 /* MEM_STNDCTL: disable ints, disable nand boot */
488 au_writel(0, MEM_STNDCTL);
490 #ifdef CONFIG_MIPS_PB1550
491 /* set gpio206 high */
492 au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
494 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
495 switch (boot_swapboot) {
513 printk("Pb1550 NAND: bad boot:swap\n");
519 /* Configure chip-select; normally done by boot code, e.g. YAMON */
522 au_writel(NAND_STCFG, MEM_STCFG0);
523 au_writel(NAND_STTIME, MEM_STTIME0);
524 au_writel(NAND_STADDR, MEM_STADDR0);
527 au_writel(NAND_STCFG, MEM_STCFG1);
528 au_writel(NAND_STTIME, MEM_STTIME1);
529 au_writel(NAND_STADDR, MEM_STADDR1);
532 au_writel(NAND_STCFG, MEM_STCFG2);
533 au_writel(NAND_STTIME, MEM_STTIME2);
534 au_writel(NAND_STADDR, MEM_STADDR2);
537 au_writel(NAND_STCFG, MEM_STCFG3);
538 au_writel(NAND_STTIME, MEM_STTIME3);
539 au_writel(NAND_STADDR, MEM_STADDR3);
543 /* Locate NAND chip-select in order to determine NAND phys address */
544 mem_staddr = 0x00000000;
545 if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
546 mem_staddr = au_readl(MEM_STADDR0);
547 else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
548 mem_staddr = au_readl(MEM_STADDR1);
549 else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
550 mem_staddr = au_readl(MEM_STADDR2);
551 else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
552 mem_staddr = au_readl(MEM_STADDR3);
554 if (mem_staddr == 0x00000000) {
555 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
559 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
561 p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
563 /* make controller and MTD agree */
565 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
567 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
569 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
571 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
573 /* Set address of hardware control function */
574 this->hwcontrol = au1550_hwcontrol;
575 this->dev_ready = au1550_device_ready;
576 this->select_chip = au1550_select_chip;
577 this->cmdfunc = au1550_command;
579 /* 30 us command delay time */
580 this->chip_delay = 30;
581 this->ecc.mode = NAND_ECC_SOFT;
583 this->options = NAND_NO_AUTOINCR;
586 this->options |= NAND_BUSWIDTH_16;
588 this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
589 this->write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
590 this->write_word = au_write_word;
591 this->read_word = au_read_word;
592 this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
593 this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
594 this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
596 /* Scan to find existence of the device */
597 if (nand_scan(au1550_mtd, 1)) {
602 /* Register the partitions */
603 add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
608 iounmap((void *)p_nand);
615 module_init(au1xxx_nand_init);
620 static void __exit au1550_cleanup(void)
622 struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
624 /* Release resources, unregister device */
625 nand_release(au1550_mtd);
627 /* Free the MTD device structure */
631 iounmap((void *)p_nand);
634 module_exit(au1550_cleanup);
636 MODULE_LICENSE("GPL");
637 MODULE_AUTHOR("Embedded Edge, LLC");
638 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");