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/mtd/mtd.h>
18 #include <linux/mtd/nand.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/version.h>
23 /* fixme: this is ugly */
24 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
25 #include <asm/mach-au1x00/au1xxx.h>
27 #include <asm/au1000.h>
28 #ifdef CONFIG_MIPS_PB1550
29 #include <asm/pb1550.h>
31 #ifdef CONFIG_MIPS_DB1550
32 #include <asm/db1x00.h>
37 * MTD structure for NAND controller
39 static struct mtd_info
*au1550_mtd
= NULL
;
40 static void __iomem
*p_nand
;
41 static int nand_width
= 1; /* default x8*/
44 * Define partitions for flash device
46 static const struct mtd_partition partition_info
[] = {
54 .offset
= MTDPART_OFS_APPEND
,
55 .size
= MTDPART_SIZ_FULL
60 * au_read_byte - read one byte from the chip
61 * @mtd: MTD device structure
63 * read function for 8bit buswith
65 static u_char
au_read_byte(struct mtd_info
*mtd
)
67 struct nand_chip
*this = mtd
->priv
;
68 u_char ret
= readb(this->IO_ADDR_R
);
74 * au_write_byte - write one byte to the chip
75 * @mtd: MTD device structure
76 * @byte: pointer to data byte to write
78 * write function for 8it buswith
80 static void au_write_byte(struct mtd_info
*mtd
, u_char byte
)
82 struct nand_chip
*this = mtd
->priv
;
83 writeb(byte
, this->IO_ADDR_W
);
88 * au_read_byte16 - read one byte endianess aware from the chip
89 * @mtd: MTD device structure
91 * read function for 16bit buswith with
92 * endianess conversion
94 static u_char
au_read_byte16(struct mtd_info
*mtd
)
96 struct nand_chip
*this = mtd
->priv
;
97 u_char ret
= (u_char
) cpu_to_le16(readw(this->IO_ADDR_R
));
103 * au_write_byte16 - write one byte endianess aware to the chip
104 * @mtd: MTD device structure
105 * @byte: pointer to data byte to write
107 * write function for 16bit buswith with
108 * endianess conversion
110 static void au_write_byte16(struct mtd_info
*mtd
, u_char byte
)
112 struct nand_chip
*this = mtd
->priv
;
113 writew(le16_to_cpu((u16
) byte
), this->IO_ADDR_W
);
118 * au_read_word - read one word from the chip
119 * @mtd: MTD device structure
121 * read function for 16bit buswith without
122 * endianess conversion
124 static u16
au_read_word(struct mtd_info
*mtd
)
126 struct nand_chip
*this = mtd
->priv
;
127 u16 ret
= readw(this->IO_ADDR_R
);
133 * au_write_word - write one word to the chip
134 * @mtd: MTD device structure
135 * @word: data word to write
137 * write function for 16bit buswith without
138 * endianess conversion
140 static void au_write_word(struct mtd_info
*mtd
, u16 word
)
142 struct nand_chip
*this = mtd
->priv
;
143 writew(word
, this->IO_ADDR_W
);
148 * au_write_buf - write buffer to chip
149 * @mtd: MTD device structure
151 * @len: number of bytes to write
153 * write function for 8bit buswith
155 static void au_write_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
158 struct nand_chip
*this = mtd
->priv
;
160 for (i
=0; i
<len
; i
++) {
161 writeb(buf
[i
], this->IO_ADDR_W
);
167 * au_read_buf - read chip data into buffer
168 * @mtd: MTD device structure
169 * @buf: buffer to store date
170 * @len: number of bytes to read
172 * read function for 8bit buswith
174 static void au_read_buf(struct mtd_info
*mtd
, u_char
*buf
, int len
)
177 struct nand_chip
*this = mtd
->priv
;
179 for (i
=0; i
<len
; i
++) {
180 buf
[i
] = readb(this->IO_ADDR_R
);
186 * au_verify_buf - Verify chip data against buffer
187 * @mtd: MTD device structure
188 * @buf: buffer containing the data to compare
189 * @len: number of bytes to compare
191 * verify function for 8bit buswith
193 static int au_verify_buf(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
196 struct nand_chip
*this = mtd
->priv
;
198 for (i
=0; i
<len
; i
++) {
199 if (buf
[i
] != readb(this->IO_ADDR_R
))
208 * au_write_buf16 - write buffer to chip
209 * @mtd: MTD device structure
211 * @len: number of bytes to write
213 * write function for 16bit buswith
215 static void au_write_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
218 struct nand_chip
*this = mtd
->priv
;
219 u16
*p
= (u16
*) buf
;
222 for (i
=0; i
<len
; i
++) {
223 writew(p
[i
], this->IO_ADDR_W
);
230 * au_read_buf16 - read chip data into buffer
231 * @mtd: MTD device structure
232 * @buf: buffer to store date
233 * @len: number of bytes to read
235 * read function for 16bit buswith
237 static void au_read_buf16(struct mtd_info
*mtd
, u_char
*buf
, int len
)
240 struct nand_chip
*this = mtd
->priv
;
241 u16
*p
= (u16
*) buf
;
244 for (i
=0; i
<len
; i
++) {
245 p
[i
] = readw(this->IO_ADDR_R
);
251 * au_verify_buf16 - Verify chip data against buffer
252 * @mtd: MTD device structure
253 * @buf: buffer containing the data to compare
254 * @len: number of bytes to compare
256 * verify function for 16bit buswith
258 static int au_verify_buf16(struct mtd_info
*mtd
, const u_char
*buf
, int len
)
261 struct nand_chip
*this = mtd
->priv
;
262 u16
*p
= (u16
*) buf
;
265 for (i
=0; i
<len
; i
++) {
266 if (p
[i
] != readw(this->IO_ADDR_R
))
274 static void au1550_hwcontrol(struct mtd_info
*mtd
, int cmd
)
276 register struct nand_chip
*this = mtd
->priv
;
280 case NAND_CTL_SETCLE
: this->IO_ADDR_W
= p_nand
+ MEM_STNAND_CMD
; break;
281 case NAND_CTL_CLRCLE
: this->IO_ADDR_W
= p_nand
+ MEM_STNAND_DATA
; break;
283 case NAND_CTL_SETALE
: this->IO_ADDR_W
= p_nand
+ MEM_STNAND_ADDR
; break;
284 case NAND_CTL_CLRALE
:
285 this->IO_ADDR_W
= p_nand
+ MEM_STNAND_DATA
;
286 /* FIXME: Nobody knows why this is neccecary,
287 * but it works only that way */
291 case NAND_CTL_SETNCE
:
292 /* assert (force assert) chip enable */
293 au_writel((1<<(4+NAND_CS
)) , MEM_STNDCTL
); break;
296 case NAND_CTL_CLRNCE
:
297 /* deassert chip enable */
298 au_writel(0, MEM_STNDCTL
); break;
302 this->IO_ADDR_R
= this->IO_ADDR_W
;
304 /* Drain the writebuffer */
308 int au1550_device_ready(struct mtd_info
*mtd
)
310 int ret
= (au_readl(MEM_STSTAT
) & 0x1) ? 1 : 0;
316 * Main initialization routine
318 int __init
au1xxx_nand_init (void)
320 struct nand_chip
*this;
321 u16 boot_swapboot
= 0; /* default value */
326 /* Allocate memory for MTD device structure and private data */
327 au1550_mtd
= kmalloc (sizeof(struct mtd_info
) +
328 sizeof (struct nand_chip
), GFP_KERNEL
);
330 printk ("Unable to allocate NAND MTD dev structure.\n");
334 /* Get pointer to private data */
335 this = (struct nand_chip
*) (&au1550_mtd
[1]);
337 /* Initialize structures */
338 memset((char *) au1550_mtd
, 0, sizeof(struct mtd_info
));
339 memset((char *) this, 0, sizeof(struct nand_chip
));
341 /* Link the private data with the MTD structure */
342 au1550_mtd
->priv
= this;
345 /* disable interrupts */
346 au_writel(au_readl(MEM_STNDCTL
) & ~(1<<8), MEM_STNDCTL
);
348 /* disable NAND boot */
349 au_writel(au_readl(MEM_STNDCTL
) & ~(1<<0), MEM_STNDCTL
);
351 #ifdef CONFIG_MIPS_PB1550
352 /* set gpio206 high */
353 au_writel(au_readl(GPIO2_DIR
) & ~(1<<6), GPIO2_DIR
);
355 boot_swapboot
= (au_readl(MEM_STSTAT
) & (0x7<<1)) |
356 ((bcsr
->status
>> 6) & 0x1);
357 switch (boot_swapboot
) {
375 printk("Pb1550 NAND: bad boot:swap\n");
381 /* Configure chip-select; normally done by boot code, e.g. YAMON */
384 au_writel(NAND_STCFG
, MEM_STCFG0
);
385 au_writel(NAND_STTIME
, MEM_STTIME0
);
386 au_writel(NAND_STADDR
, MEM_STADDR0
);
389 au_writel(NAND_STCFG
, MEM_STCFG1
);
390 au_writel(NAND_STTIME
, MEM_STTIME1
);
391 au_writel(NAND_STADDR
, MEM_STADDR1
);
394 au_writel(NAND_STCFG
, MEM_STCFG2
);
395 au_writel(NAND_STTIME
, MEM_STTIME2
);
396 au_writel(NAND_STADDR
, MEM_STADDR2
);
399 au_writel(NAND_STCFG
, MEM_STCFG3
);
400 au_writel(NAND_STTIME
, MEM_STTIME3
);
401 au_writel(NAND_STADDR
, MEM_STADDR3
);
405 /* Locate NAND chip-select in order to determine NAND phys address */
406 mem_staddr
= 0x00000000;
407 if (((au_readl(MEM_STCFG0
) & 0x7) == 0x5) && (NAND_CS
== 0))
408 mem_staddr
= au_readl(MEM_STADDR0
);
409 else if (((au_readl(MEM_STCFG1
) & 0x7) == 0x5) && (NAND_CS
== 1))
410 mem_staddr
= au_readl(MEM_STADDR1
);
411 else if (((au_readl(MEM_STCFG2
) & 0x7) == 0x5) && (NAND_CS
== 2))
412 mem_staddr
= au_readl(MEM_STADDR2
);
413 else if (((au_readl(MEM_STCFG3
) & 0x7) == 0x5) && (NAND_CS
== 3))
414 mem_staddr
= au_readl(MEM_STADDR3
);
416 if (mem_staddr
== 0x00000000) {
417 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
421 nand_phys
= (mem_staddr
<< 4) & 0xFFFC0000;
423 p_nand
= (void __iomem
*)ioremap(nand_phys
, 0x1000);
425 /* make controller and MTD agree */
427 nand_width
= au_readl(MEM_STCFG0
) & (1<<22);
429 nand_width
= au_readl(MEM_STCFG1
) & (1<<22);
431 nand_width
= au_readl(MEM_STCFG2
) & (1<<22);
433 nand_width
= au_readl(MEM_STCFG3
) & (1<<22);
436 /* Set address of hardware control function */
437 this->hwcontrol
= au1550_hwcontrol
;
438 this->dev_ready
= au1550_device_ready
;
439 /* 30 us command delay time */
440 this->chip_delay
= 30;
441 this->eccmode
= NAND_ECC_SOFT
;
443 this->options
= NAND_NO_AUTOINCR
;
446 this->options
|= NAND_BUSWIDTH_16
;
448 this->read_byte
= (!nand_width
) ? au_read_byte16
: au_read_byte
;
449 this->write_byte
= (!nand_width
) ? au_write_byte16
: au_write_byte
;
450 this->write_word
= au_write_word
;
451 this->read_word
= au_read_word
;
452 this->write_buf
= (!nand_width
) ? au_write_buf16
: au_write_buf
;
453 this->read_buf
= (!nand_width
) ? au_read_buf16
: au_read_buf
;
454 this->verify_buf
= (!nand_width
) ? au_verify_buf16
: au_verify_buf
;
456 /* Scan to find existence of the device */
457 if (nand_scan (au1550_mtd
, 1)) {
462 /* Register the partitions */
463 add_mtd_partitions(au1550_mtd
, partition_info
, ARRAY_SIZE(partition_info
));
468 iounmap ((void *)p_nand
);
475 module_init(au1xxx_nand_init
);
481 static void __exit
au1550_cleanup (void)
483 struct nand_chip
*this = (struct nand_chip
*) &au1550_mtd
[1];
485 /* Release resources, unregister device */
486 nand_release (au1550_mtd
);
488 /* Free the MTD device structure */
492 iounmap ((void *)p_nand
);
494 module_exit(au1550_cleanup
);
497 MODULE_LICENSE("GPL");
498 MODULE_AUTHOR("Embedded Edge, LLC");
499 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");