2 * drivers/mtd/nand/au1550nd.c
4 * Copyright (C) 2004 Embedded Edge, LLC
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/slab.h>
13 #include <linux/gpio.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/interrupt.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/nand.h>
19 #include <linux/mtd/partitions.h>
22 #include <asm/mach-au1x00/au1xxx.h>
23 #include <asm/mach-db1x00/bcsr.h>
26 * MTD structure for NAND controller
28 static struct mtd_info
*au1550_mtd
= NULL
;
29 static void __iomem
*p_nand
;
30 static int nand_width
= 1; /* default x8 */
31 static void (*au1550_write_byte
)(struct mtd_info
*, u_char
);
34 * Define partitions for flash device
36 static const struct mtd_partition partition_info
[] = {
40 .size
= 8 * 1024 * 1024},
43 .offset
= MTDPART_OFS_APPEND
,
44 .size
= MTDPART_SIZ_FULL
}
48 * au_read_byte - read one byte from the chip
49 * @mtd: MTD device structure
51 * read function for 8bit buswith
53 static u_char
au_read_byte(struct mtd_info
*mtd
)
55 struct nand_chip
*this = mtd
->priv
;
56 u_char ret
= readb(this->IO_ADDR_R
);
62 * au_write_byte - write one byte to the chip
63 * @mtd: MTD device structure
64 * @byte: pointer to data byte to write
66 * write function for 8it buswith
68 static void au_write_byte(struct mtd_info
*mtd
, u_char byte
)
70 struct nand_chip
*this = mtd
->priv
;
71 writeb(byte
, this->IO_ADDR_W
);
76 * au_read_byte16 - read one byte endianess aware from the chip
77 * @mtd: MTD device structure
79 * read function for 16bit buswith with
80 * endianess conversion
82 static u_char
au_read_byte16(struct mtd_info
*mtd
)
84 struct nand_chip
*this = mtd
->priv
;
85 u_char ret
= (u_char
) cpu_to_le16(readw(this->IO_ADDR_R
));
91 * au_write_byte16 - write one byte endianess aware to the chip
92 * @mtd: MTD device structure
93 * @byte: pointer to data byte to write
95 * write function for 16bit buswith with
96 * endianess conversion
98 static void au_write_byte16(struct mtd_info
*mtd
, u_char byte
)
100 struct nand_chip
*this = mtd
->priv
;
101 writew(le16_to_cpu((u16
) byte
), this->IO_ADDR_W
);
106 * au_read_word - read one word from the chip
107 * @mtd: MTD device structure
109 * read function for 16bit buswith without
110 * endianess conversion
112 static u16
au_read_word(struct mtd_info
*mtd
)
114 struct nand_chip
*this = mtd
->priv
;
115 u16 ret
= readw(this->IO_ADDR_R
);
121 * au_write_buf - write buffer to chip
122 * @mtd: MTD device structure
124 * @len: number of bytes to write
126 * write function for 8bit buswith
128 static void au_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
131 struct nand_chip
*this = mtd
->priv
;
133 for (i
= 0; i
< len
; i
++) {
134 writeb(buf
[i
], this->IO_ADDR_W
);
140 * au_read_buf - read chip data into buffer
141 * @mtd: MTD device structure
142 * @buf: buffer to store date
143 * @len: number of bytes to read
145 * read function for 8bit buswith
147 static void au_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
150 struct nand_chip
*this = mtd
->priv
;
152 for (i
= 0; i
< len
; i
++) {
153 buf
[i
] = readb(this->IO_ADDR_R
);
159 * au_verify_buf - Verify chip data against buffer
160 * @mtd: MTD device structure
161 * @buf: buffer containing the data to compare
162 * @len: number of bytes to compare
164 * verify function for 8bit buswith
166 static int au_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
169 struct nand_chip
*this = mtd
->priv
;
171 for (i
= 0; i
< len
; i
++) {
172 if (buf
[i
] != readb(this->IO_ADDR_R
))
181 * au_write_buf16 - write buffer to chip
182 * @mtd: MTD device structure
184 * @len: number of bytes to write
186 * write function for 16bit buswith
188 static void au_write_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
191 struct nand_chip
*this = mtd
->priv
;
192 u16
*p
= (u16
*) buf
;
195 for (i
= 0; i
< len
; i
++) {
196 writew(p
[i
], this->IO_ADDR_W
);
203 * au_read_buf16 - read chip data into buffer
204 * @mtd: MTD device structure
205 * @buf: buffer to store date
206 * @len: number of bytes to read
208 * read function for 16bit buswith
210 static void au_read_buf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
213 struct nand_chip
*this = mtd
->priv
;
214 u16
*p
= (u16
*) buf
;
217 for (i
= 0; i
< len
; i
++) {
218 p
[i
] = readw(this->IO_ADDR_R
);
224 * au_verify_buf16 - Verify chip data against buffer
225 * @mtd: MTD device structure
226 * @buf: buffer containing the data to compare
227 * @len: number of bytes to compare
229 * verify function for 16bit buswith
231 static int au_verify_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
234 struct nand_chip
*this = mtd
->priv
;
235 u16
*p
= (u16
*) buf
;
238 for (i
= 0; i
< len
; i
++) {
239 if (p
[i
] != readw(this->IO_ADDR_R
))
246 /* Select the chip by setting nCE to low */
247 #define NAND_CTL_SETNCE 1
248 /* Deselect the chip by setting nCE to high */
249 #define NAND_CTL_CLRNCE 2
250 /* Select the command latch by setting CLE to high */
251 #define NAND_CTL_SETCLE 3
252 /* Deselect the command latch by setting CLE to low */
253 #define NAND_CTL_CLRCLE 4
254 /* Select the address latch by setting ALE to high */
255 #define NAND_CTL_SETALE 5
256 /* Deselect the address latch by setting ALE to low */
257 #define NAND_CTL_CLRALE 6
259 static void au1550_hwcontrol(struct mtd_info
*mtd
, int cmd
)
261 register struct nand_chip
*this = mtd
->priv
;
265 case NAND_CTL_SETCLE
:
266 this->IO_ADDR_W
= p_nand
+ MEM_STNAND_CMD
;
269 case NAND_CTL_CLRCLE
:
270 this->IO_ADDR_W
= p_nand
+ MEM_STNAND_DATA
;
273 case NAND_CTL_SETALE
:
274 this->IO_ADDR_W
= p_nand
+ MEM_STNAND_ADDR
;
277 case NAND_CTL_CLRALE
:
278 this->IO_ADDR_W
= p_nand
+ MEM_STNAND_DATA
;
279 /* FIXME: Nobody knows why this is necessary,
280 * but it works only that way */
284 case NAND_CTL_SETNCE
:
285 /* assert (force assert) chip enable */
286 au_writel((1 << (4 + NAND_CS
)), MEM_STNDCTL
);
289 case NAND_CTL_CLRNCE
:
290 /* deassert chip enable */
291 au_writel(0, MEM_STNDCTL
);
295 this->IO_ADDR_R
= this->IO_ADDR_W
;
297 /* Drain the writebuffer */
301 int au1550_device_ready(struct mtd_info
*mtd
)
303 int ret
= (au_readl(MEM_STSTAT
) & 0x1) ? 1 : 0;
309 * au1550_select_chip - control -CE line
310 * Forbid driving -CE manually permitting the NAND controller to do this.
311 * Keeping -CE asserted during the whole sector reads interferes with the
312 * NOR flash and PCMCIA drivers as it causes contention on the static bus.
313 * We only have to hold -CE low for the NAND read commands since the flash
314 * chip needs it to be asserted during chip not ready time but the NAND
315 * controller keeps it released.
317 * @mtd: MTD device structure
318 * @chip: chipnumber to select, -1 for deselect
320 static void au1550_select_chip(struct mtd_info
*mtd
, int chip
)
325 * au1550_command - Send command to NAND device
326 * @mtd: MTD device structure
327 * @command: the command to be sent
328 * @column: the column address for this command, -1 if none
329 * @page_addr: the page address for this command, -1 if none
331 static void au1550_command(struct mtd_info
*mtd
, unsigned command
, int column
, int page_addr
)
333 register struct nand_chip
*this = mtd
->priv
;
334 int ce_override
= 0, i
;
337 /* Begin command latch cycle */
338 au1550_hwcontrol(mtd
, NAND_CTL_SETCLE
);
340 * Write out the command to the device.
342 if (command
== NAND_CMD_SEQIN
) {
345 if (column
>= mtd
->writesize
) {
347 column
-= mtd
->writesize
;
348 readcmd
= NAND_CMD_READOOB
;
349 } else if (column
< 256) {
350 /* First 256 bytes --> READ0 */
351 readcmd
= NAND_CMD_READ0
;
354 readcmd
= NAND_CMD_READ1
;
356 au1550_write_byte(mtd
, readcmd
);
358 au1550_write_byte(mtd
, command
);
360 /* Set ALE and clear CLE to start address cycle */
361 au1550_hwcontrol(mtd
, NAND_CTL_CLRCLE
);
363 if (column
!= -1 || page_addr
!= -1) {
364 au1550_hwcontrol(mtd
, NAND_CTL_SETALE
);
366 /* Serially input address */
368 /* Adjust columns for 16 bit buswidth */
369 if (this->options
& NAND_BUSWIDTH_16
)
371 au1550_write_byte(mtd
, column
);
373 if (page_addr
!= -1) {
374 au1550_write_byte(mtd
, (u8
)(page_addr
& 0xff));
376 if (command
== NAND_CMD_READ0
||
377 command
== NAND_CMD_READ1
||
378 command
== NAND_CMD_READOOB
) {
380 * NAND controller will release -CE after
381 * the last address byte is written, so we'll
382 * have to forcibly assert it. No interrupts
383 * are allowed while we do this as we don't
384 * want the NOR flash or PCMCIA drivers to
385 * steal our precious bytes of data...
388 local_irq_save(flags
);
389 au1550_hwcontrol(mtd
, NAND_CTL_SETNCE
);
392 au1550_write_byte(mtd
, (u8
)(page_addr
>> 8));
394 /* One more address cycle for devices > 32MiB */
395 if (this->chipsize
> (32 << 20))
396 au1550_write_byte(mtd
, (u8
)((page_addr
>> 16) & 0x0f));
398 /* Latch in address */
399 au1550_hwcontrol(mtd
, NAND_CTL_CLRALE
);
403 * Program and erase have their own busy handlers.
404 * Status and sequential in need no delay.
408 case NAND_CMD_PAGEPROG
:
409 case NAND_CMD_ERASE1
:
410 case NAND_CMD_ERASE2
:
412 case NAND_CMD_STATUS
:
420 case NAND_CMD_READOOB
:
421 /* Check if we're really driving -CE low (just in case) */
422 if (unlikely(!ce_override
))
425 /* Apply a short delay always to ensure that we do wait tWB. */
427 /* Wait for a chip to become ready... */
428 for (i
= this->chip_delay
; !this->dev_ready(mtd
) && i
> 0; --i
)
431 /* Release -CE and re-enable interrupts. */
432 au1550_hwcontrol(mtd
, NAND_CTL_CLRNCE
);
433 local_irq_restore(flags
);
436 /* Apply this short delay always to ensure that we do wait tWB. */
439 while(!this->dev_ready(mtd
));
444 * Main initialization routine
446 static int __init
au1xxx_nand_init(void)
448 struct nand_chip
*this;
449 u16 boot_swapboot
= 0; /* default value */
454 /* Allocate memory for MTD device structure and private data */
455 au1550_mtd
= kzalloc(sizeof(struct mtd_info
) + sizeof(struct nand_chip
), GFP_KERNEL
);
457 printk("Unable to allocate NAND MTD dev structure.\n");
461 /* Get pointer to private data */
462 this = (struct nand_chip
*)(&au1550_mtd
[1]);
464 /* Link the private data with the MTD structure */
465 au1550_mtd
->priv
= this;
466 au1550_mtd
->owner
= THIS_MODULE
;
469 /* MEM_STNDCTL: disable ints, disable nand boot */
470 au_writel(0, MEM_STNDCTL
);
472 #ifdef CONFIG_MIPS_PB1550
473 /* set gpio206 high */
474 gpio_direction_input(206);
476 boot_swapboot
= (au_readl(MEM_STSTAT
) & (0x7 << 1)) | ((bcsr_read(BCSR_STATUS
) >> 6) & 0x1);
478 switch (boot_swapboot
) {
496 printk("Pb1550 NAND: bad boot:swap\n");
502 /* Configure chip-select; normally done by boot code, e.g. YAMON */
505 au_writel(NAND_STCFG
, MEM_STCFG0
);
506 au_writel(NAND_STTIME
, MEM_STTIME0
);
507 au_writel(NAND_STADDR
, MEM_STADDR0
);
510 au_writel(NAND_STCFG
, MEM_STCFG1
);
511 au_writel(NAND_STTIME
, MEM_STTIME1
);
512 au_writel(NAND_STADDR
, MEM_STADDR1
);
515 au_writel(NAND_STCFG
, MEM_STCFG2
);
516 au_writel(NAND_STTIME
, MEM_STTIME2
);
517 au_writel(NAND_STADDR
, MEM_STADDR2
);
520 au_writel(NAND_STCFG
, MEM_STCFG3
);
521 au_writel(NAND_STTIME
, MEM_STTIME3
);
522 au_writel(NAND_STADDR
, MEM_STADDR3
);
526 /* Locate NAND chip-select in order to determine NAND phys address */
527 mem_staddr
= 0x00000000;
528 if (((au_readl(MEM_STCFG0
) & 0x7) == 0x5) && (NAND_CS
== 0))
529 mem_staddr
= au_readl(MEM_STADDR0
);
530 else if (((au_readl(MEM_STCFG1
) & 0x7) == 0x5) && (NAND_CS
== 1))
531 mem_staddr
= au_readl(MEM_STADDR1
);
532 else if (((au_readl(MEM_STCFG2
) & 0x7) == 0x5) && (NAND_CS
== 2))
533 mem_staddr
= au_readl(MEM_STADDR2
);
534 else if (((au_readl(MEM_STCFG3
) & 0x7) == 0x5) && (NAND_CS
== 3))
535 mem_staddr
= au_readl(MEM_STADDR3
);
537 if (mem_staddr
== 0x00000000) {
538 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
542 nand_phys
= (mem_staddr
<< 4) & 0xFFFC0000;
544 p_nand
= ioremap(nand_phys
, 0x1000);
546 /* make controller and MTD agree */
548 nand_width
= au_readl(MEM_STCFG0
) & (1 << 22);
550 nand_width
= au_readl(MEM_STCFG1
) & (1 << 22);
552 nand_width
= au_readl(MEM_STCFG2
) & (1 << 22);
554 nand_width
= au_readl(MEM_STCFG3
) & (1 << 22);
556 /* Set address of hardware control function */
557 this->dev_ready
= au1550_device_ready
;
558 this->select_chip
= au1550_select_chip
;
559 this->cmdfunc
= au1550_command
;
561 /* 30 us command delay time */
562 this->chip_delay
= 30;
563 this->ecc
.mode
= NAND_ECC_SOFT
;
565 this->options
= NAND_NO_AUTOINCR
;
568 this->options
|= NAND_BUSWIDTH_16
;
570 this->read_byte
= (!nand_width
) ? au_read_byte16
: au_read_byte
;
571 au1550_write_byte
= (!nand_width
) ? au_write_byte16
: au_write_byte
;
572 this->read_word
= au_read_word
;
573 this->write_buf
= (!nand_width
) ? au_write_buf16
: au_write_buf
;
574 this->read_buf
= (!nand_width
) ? au_read_buf16
: au_read_buf
;
575 this->verify_buf
= (!nand_width
) ? au_verify_buf16
: au_verify_buf
;
577 /* Scan to find existence of the device */
578 if (nand_scan(au1550_mtd
, 1)) {
583 /* Register the partitions */
584 mtd_device_register(au1550_mtd
, partition_info
,
585 ARRAY_SIZE(partition_info
));
597 module_init(au1xxx_nand_init
);
602 static void __exit
au1550_cleanup(void)
604 /* Release resources, unregister device */
605 nand_release(au1550_mtd
);
607 /* Free the MTD device structure */
614 module_exit(au1550_cleanup
);
616 MODULE_LICENSE("GPL");
617 MODULE_AUTHOR("Embedded Edge, LLC");
618 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");