blob: bde3550910a2e12626b7255e23a92982776a8668 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/mtd/nand/au1550nd.c
3 *
4 * Copyright (C) 2004 Embedded Edge, LLC
5 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 * $Id: au1550nd.c,v 1.13 2005/11/07 11:14:30 gleixner Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
11 *
12 */
13
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>
Olaf Hering733482e2005-11-08 21:34:55 -080020#include <linux/version.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/io.h>
22
23/* fixme: this is ugly */
24#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
Pete Popovef6f0d12005-09-23 02:44:58 +010025#include <asm/mach-au1x00/au1xxx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#else
27#include <asm/au1000.h>
28#ifdef CONFIG_MIPS_PB1550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000029#include <asm/pb1550.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#endif
31#ifdef CONFIG_MIPS_DB1550
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000032#include <asm/db1x00.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34#endif
35
36/*
37 * MTD structure for NAND controller
38 */
39static struct mtd_info *au1550_mtd = NULL;
40static void __iomem *p_nand;
41static int nand_width = 1; /* default x8*/
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Define partitions for flash device
45 */
Jesper Juhl3c6bee12006-01-09 20:54:01 -080046static const struct mtd_partition partition_info[] = {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000047 {
Pete Popovef6f0d12005-09-23 02:44:58 +010048 .name = "NAND FS 0",
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .offset = 0,
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000050 .size = 8*1024*1024
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 },
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000052 {
Pete Popovef6f0d12005-09-23 02:44:58 +010053 .name = "NAND FS 1",
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 .offset = MTDPART_OFS_APPEND,
Pete Popovef6f0d12005-09-23 02:44:58 +010055 .size = MTDPART_SIZ_FULL
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59/**
60 * au_read_byte - read one byte from the chip
61 * @mtd: MTD device structure
62 *
63 * read function for 8bit buswith
64 */
65static u_char au_read_byte(struct mtd_info *mtd)
66{
67 struct nand_chip *this = mtd->priv;
68 u_char ret = readb(this->IO_ADDR_R);
69 au_sync();
70 return ret;
71}
72
73/**
74 * au_write_byte - write one byte to the chip
75 * @mtd: MTD device structure
76 * @byte: pointer to data byte to write
77 *
78 * write function for 8it buswith
79 */
80static void au_write_byte(struct mtd_info *mtd, u_char byte)
81{
82 struct nand_chip *this = mtd->priv;
83 writeb(byte, this->IO_ADDR_W);
84 au_sync();
85}
86
87/**
88 * au_read_byte16 - read one byte endianess aware from the chip
89 * @mtd: MTD device structure
90 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +000091 * read function for 16bit buswith with
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * endianess conversion
93 */
94static u_char au_read_byte16(struct mtd_info *mtd)
95{
96 struct nand_chip *this = mtd->priv;
97 u_char ret = (u_char) cpu_to_le16(readw(this->IO_ADDR_R));
98 au_sync();
99 return ret;
100}
101
102/**
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
106 *
107 * write function for 16bit buswith with
108 * endianess conversion
109 */
110static void au_write_byte16(struct mtd_info *mtd, u_char byte)
111{
112 struct nand_chip *this = mtd->priv;
113 writew(le16_to_cpu((u16) byte), this->IO_ADDR_W);
114 au_sync();
115}
116
117/**
118 * au_read_word - read one word from the chip
119 * @mtd: MTD device structure
120 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000121 * read function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * endianess conversion
123 */
124static u16 au_read_word(struct mtd_info *mtd)
125{
126 struct nand_chip *this = mtd->priv;
127 u16 ret = readw(this->IO_ADDR_R);
128 au_sync();
129 return ret;
130}
131
132/**
133 * au_write_word - write one word to the chip
134 * @mtd: MTD device structure
135 * @word: data word to write
136 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000137 * write function for 16bit buswith without
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 * endianess conversion
139 */
140static void au_write_word(struct mtd_info *mtd, u16 word)
141{
142 struct nand_chip *this = mtd->priv;
143 writew(word, this->IO_ADDR_W);
144 au_sync();
145}
146
147/**
148 * au_write_buf - write buffer to chip
149 * @mtd: MTD device structure
150 * @buf: data buffer
151 * @len: number of bytes to write
152 *
153 * write function for 8bit buswith
154 */
155static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
156{
157 int i;
158 struct nand_chip *this = mtd->priv;
159
160 for (i=0; i<len; i++) {
161 writeb(buf[i], this->IO_ADDR_W);
162 au_sync();
163 }
164}
165
166/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000167 * au_read_buf - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * @mtd: MTD device structure
169 * @buf: buffer to store date
170 * @len: number of bytes to read
171 *
172 * read function for 8bit buswith
173 */
174static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
175{
176 int i;
177 struct nand_chip *this = mtd->priv;
178
179 for (i=0; i<len; i++) {
180 buf[i] = readb(this->IO_ADDR_R);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000181 au_sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183}
184
185/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000186 * au_verify_buf - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * @mtd: MTD device structure
188 * @buf: buffer containing the data to compare
189 * @len: number of bytes to compare
190 *
191 * verify function for 8bit buswith
192 */
193static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
194{
195 int i;
196 struct nand_chip *this = mtd->priv;
197
198 for (i=0; i<len; i++) {
199 if (buf[i] != readb(this->IO_ADDR_R))
200 return -EFAULT;
201 au_sync();
202 }
203
204 return 0;
205}
206
207/**
208 * au_write_buf16 - write buffer to chip
209 * @mtd: MTD device structure
210 * @buf: data buffer
211 * @len: number of bytes to write
212 *
213 * write function for 16bit buswith
214 */
215static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
216{
217 int i;
218 struct nand_chip *this = mtd->priv;
219 u16 *p = (u16 *) buf;
220 len >>= 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 for (i=0; i<len; i++) {
223 writew(p[i], this->IO_ADDR_W);
224 au_sync();
225 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
229/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000230 * au_read_buf16 - read chip data into buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 * @mtd: MTD device structure
232 * @buf: buffer to store date
233 * @len: number of bytes to read
234 *
235 * read function for 16bit buswith
236 */
237static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
238{
239 int i;
240 struct nand_chip *this = mtd->priv;
241 u16 *p = (u16 *) buf;
242 len >>= 1;
243
244 for (i=0; i<len; i++) {
245 p[i] = readw(this->IO_ADDR_R);
246 au_sync();
247 }
248}
249
250/**
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000251 * au_verify_buf16 - Verify chip data against buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 * @mtd: MTD device structure
253 * @buf: buffer containing the data to compare
254 * @len: number of bytes to compare
255 *
256 * verify function for 16bit buswith
257 */
258static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
259{
260 int i;
261 struct nand_chip *this = mtd->priv;
262 u16 *p = (u16 *) buf;
263 len >>= 1;
264
265 for (i=0; i<len; i++) {
266 if (p[i] != readw(this->IO_ADDR_R))
267 return -EFAULT;
268 au_sync();
269 }
270 return 0;
271}
272
273
274static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
275{
276 register struct nand_chip *this = mtd->priv;
277
278 switch(cmd){
279
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;
282
283 case NAND_CTL_SETALE: this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR; break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000284 case NAND_CTL_CLRALE:
285 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
286 /* FIXME: Nobody knows why this is neccecary,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * but it works only that way */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000288 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 break;
290
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000291 case NAND_CTL_SETNCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* assert (force assert) chip enable */
293 au_writel((1<<(4+NAND_CS)) , MEM_STNDCTL); break;
294 break;
295
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000296 case NAND_CTL_CLRNCE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 /* deassert chip enable */
298 au_writel(0, MEM_STNDCTL); break;
299 break;
300 }
301
302 this->IO_ADDR_R = this->IO_ADDR_W;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 /* Drain the writebuffer */
305 au_sync();
306}
307
308int au1550_device_ready(struct mtd_info *mtd)
309{
310 int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
311 au_sync();
312 return ret;
313}
314
315/*
316 * Main initialization routine
317 */
Pete Popovef6f0d12005-09-23 02:44:58 +0100318int __init au1xxx_nand_init (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
320 struct nand_chip *this;
321 u16 boot_swapboot = 0; /* default value */
322 int retval;
Pete Popovef6f0d12005-09-23 02:44:58 +0100323 u32 mem_staddr;
324 u32 nand_phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Allocate memory for MTD device structure and private data */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000327 au1550_mtd = kmalloc (sizeof(struct mtd_info) +
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sizeof (struct nand_chip), GFP_KERNEL);
329 if (!au1550_mtd) {
330 printk ("Unable to allocate NAND MTD dev structure.\n");
331 return -ENOMEM;
332 }
333
334 /* Get pointer to private data */
335 this = (struct nand_chip *) (&au1550_mtd[1]);
336
337 /* Initialize structures */
338 memset((char *) au1550_mtd, 0, sizeof(struct mtd_info));
339 memset((char *) this, 0, sizeof(struct nand_chip));
340
341 /* Link the private data with the MTD structure */
342 au1550_mtd->priv = this;
343
344
Pete Popovef6f0d12005-09-23 02:44:58 +0100345 /* disable interrupts */
346 au_writel(au_readl(MEM_STNDCTL) & ~(1<<8), MEM_STNDCTL);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000347
Pete Popovef6f0d12005-09-23 02:44:58 +0100348 /* disable NAND boot */
349 au_writel(au_readl(MEM_STNDCTL) & ~(1<<0), MEM_STNDCTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351#ifdef CONFIG_MIPS_PB1550
352 /* set gpio206 high */
353 au_writel(au_readl(GPIO2_DIR) & ~(1<<6), GPIO2_DIR);
354
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000355 boot_swapboot = (au_readl(MEM_STSTAT) & (0x7<<1)) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 ((bcsr->status >> 6) & 0x1);
357 switch (boot_swapboot) {
358 case 0:
359 case 2:
360 case 8:
361 case 0xC:
362 case 0xD:
363 /* x16 NAND Flash */
364 nand_width = 0;
365 break;
366 case 1:
367 case 9:
368 case 3:
369 case 0xE:
370 case 0xF:
371 /* x8 NAND Flash */
372 nand_width = 1;
373 break;
374 default:
375 printk("Pb1550 NAND: bad boot:swap\n");
376 retval = -EINVAL;
377 goto outmem;
378 }
379#endif
380
Pete Popovef6f0d12005-09-23 02:44:58 +0100381 /* Configure chip-select; normally done by boot code, e.g. YAMON */
382#ifdef NAND_STCFG
383 if (NAND_CS == 0) {
384 au_writel(NAND_STCFG, MEM_STCFG0);
385 au_writel(NAND_STTIME, MEM_STTIME0);
386 au_writel(NAND_STADDR, MEM_STADDR0);
387 }
388 if (NAND_CS == 1) {
389 au_writel(NAND_STCFG, MEM_STCFG1);
390 au_writel(NAND_STTIME, MEM_STTIME1);
391 au_writel(NAND_STADDR, MEM_STADDR1);
392 }
393 if (NAND_CS == 2) {
394 au_writel(NAND_STCFG, MEM_STCFG2);
395 au_writel(NAND_STTIME, MEM_STTIME2);
396 au_writel(NAND_STADDR, MEM_STADDR2);
397 }
398 if (NAND_CS == 3) {
399 au_writel(NAND_STCFG, MEM_STCFG3);
400 au_writel(NAND_STTIME, MEM_STTIME3);
401 au_writel(NAND_STADDR, MEM_STADDR3);
402 }
403#endif
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000404
Pete Popovef6f0d12005-09-23 02:44:58 +0100405 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Pete Popovef6f0d12005-09-23 02:44:58 +0100416 if (mem_staddr == 0x00000000) {
417 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
418 kfree(au1550_mtd);
419 return 1;
420 }
421 nand_phys = (mem_staddr << 4) & 0xFFFC0000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Pete Popovef6f0d12005-09-23 02:44:58 +0100423 p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
424
425 /* make controller and MTD agree */
426 if (NAND_CS == 0)
427 nand_width = au_readl(MEM_STCFG0) & (1<<22);
428 if (NAND_CS == 1)
429 nand_width = au_readl(MEM_STCFG1) & (1<<22);
430 if (NAND_CS == 2)
431 nand_width = au_readl(MEM_STCFG2) & (1<<22);
432 if (NAND_CS == 3)
433 nand_width = au_readl(MEM_STCFG3) & (1<<22);
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
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 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000440 this->chip_delay = 30;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 this->eccmode = NAND_ECC_SOFT;
442
443 this->options = NAND_NO_AUTOINCR;
444
445 if (!nand_width)
446 this->options |= NAND_BUSWIDTH_16;
447
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;
455
456 /* Scan to find existence of the device */
457 if (nand_scan (au1550_mtd, 1)) {
458 retval = -ENXIO;
459 goto outio;
460 }
461
462 /* Register the partitions */
Tobias Klauser87d10f32006-03-31 02:29:45 -0800463 add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return 0;
466
467 outio:
468 iounmap ((void *)p_nand);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 outmem:
471 kfree (au1550_mtd);
472 return retval;
473}
474
Pete Popovef6f0d12005-09-23 02:44:58 +0100475module_init(au1xxx_nand_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477/*
478 * Clean up routine
479 */
480#ifdef MODULE
481static void __exit au1550_cleanup (void)
482{
483 struct nand_chip *this = (struct nand_chip *) &au1550_mtd[1];
484
485 /* Release resources, unregister device */
486 nand_release (au1550_mtd);
487
488 /* Free the MTD device structure */
489 kfree (au1550_mtd);
490
491 /* Unmap */
492 iounmap ((void *)p_nand);
493}
494module_exit(au1550_cleanup);
495#endif
496
497MODULE_LICENSE("GPL");
498MODULE_AUTHOR("Embedded Edge, LLC");
499MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");