Merge to Fedora kernel-2.6.18-1.2224_FC5 patched with stable patch-2.6.18.1-vs2.0...
[linux-2.6.git] / drivers / mtd / nand / au1550nd.c
1 /*
2  *  drivers/mtd/nand/au1550nd.c
3  *
4  *  Copyright (C) 2004 Embedded Edge, LLC
5  *
6  * $Id: au1550nd.c,v 1.13 2005/11/07 11:14:30 gleixner Exp $
7  *
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/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>
22 #include <asm/io.h>
23
24 /* fixme: this is ugly */
25 #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 0)
26 #include <asm/mach-au1x00/au1xxx.h>
27 #else
28 #include <asm/au1000.h>
29 #ifdef CONFIG_MIPS_PB1550
30 #include <asm/pb1550.h>
31 #endif
32 #ifdef CONFIG_MIPS_DB1550
33 #include <asm/db1x00.h>
34 #endif
35 #endif
36
37 /*
38  * MTD structure for NAND controller
39  */
40 static struct mtd_info *au1550_mtd = NULL;
41 static void __iomem *p_nand;
42 static int nand_width = 1;      /* default x8 */
43 static void (*au1550_write_byte)(struct mtd_info *, u_char);
44
45 /*
46  * Define partitions for flash device
47  */
48 static const struct mtd_partition partition_info[] = {
49         {
50          .name = "NAND FS 0",
51          .offset = 0,
52          .size = 8 * 1024 * 1024},
53         {
54          .name = "NAND FS 1",
55          .offset = MTDPART_OFS_APPEND,
56          .size = MTDPART_SIZ_FULL}
57 };
58
59 /**
60  * au_read_byte -  read one byte from the chip
61  * @mtd:        MTD device structure
62  *
63  *  read function for 8bit buswith
64  */
65 static 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  */
80 static 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  *
91  *  read function for 16bit buswith with
92  * endianess conversion
93  */
94 static 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  */
110 static 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  *
121  *  read function for 16bit buswith without
122  * endianess conversion
123  */
124 static 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_buf -  write buffer to chip
134  * @mtd:        MTD device structure
135  * @buf:        data buffer
136  * @len:        number of bytes to write
137  *
138  *  write function for 8bit buswith
139  */
140 static void au_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
141 {
142         int i;
143         struct nand_chip *this = mtd->priv;
144
145         for (i = 0; i < len; i++) {
146                 writeb(buf[i], this->IO_ADDR_W);
147                 au_sync();
148         }
149 }
150
151 /**
152  * au_read_buf -  read chip data into buffer
153  * @mtd:        MTD device structure
154  * @buf:        buffer to store date
155  * @len:        number of bytes to read
156  *
157  *  read function for 8bit buswith
158  */
159 static void au_read_buf(struct mtd_info *mtd, u_char *buf, int len)
160 {
161         int i;
162         struct nand_chip *this = mtd->priv;
163
164         for (i = 0; i < len; i++) {
165                 buf[i] = readb(this->IO_ADDR_R);
166                 au_sync();
167         }
168 }
169
170 /**
171  * au_verify_buf -  Verify chip data against buffer
172  * @mtd:        MTD device structure
173  * @buf:        buffer containing the data to compare
174  * @len:        number of bytes to compare
175  *
176  *  verify function for 8bit buswith
177  */
178 static int au_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
179 {
180         int i;
181         struct nand_chip *this = mtd->priv;
182
183         for (i = 0; i < len; i++) {
184                 if (buf[i] != readb(this->IO_ADDR_R))
185                         return -EFAULT;
186                 au_sync();
187         }
188
189         return 0;
190 }
191
192 /**
193  * au_write_buf16 -  write buffer to chip
194  * @mtd:        MTD device structure
195  * @buf:        data buffer
196  * @len:        number of bytes to write
197  *
198  *  write function for 16bit buswith
199  */
200 static void au_write_buf16(struct mtd_info *mtd, const u_char *buf, int len)
201 {
202         int i;
203         struct nand_chip *this = mtd->priv;
204         u16 *p = (u16 *) buf;
205         len >>= 1;
206
207         for (i = 0; i < len; i++) {
208                 writew(p[i], this->IO_ADDR_W);
209                 au_sync();
210         }
211
212 }
213
214 /**
215  * au_read_buf16 -  read chip data into buffer
216  * @mtd:        MTD device structure
217  * @buf:        buffer to store date
218  * @len:        number of bytes to read
219  *
220  *  read function for 16bit buswith
221  */
222 static void au_read_buf16(struct mtd_info *mtd, u_char *buf, int len)
223 {
224         int i;
225         struct nand_chip *this = mtd->priv;
226         u16 *p = (u16 *) buf;
227         len >>= 1;
228
229         for (i = 0; i < len; i++) {
230                 p[i] = readw(this->IO_ADDR_R);
231                 au_sync();
232         }
233 }
234
235 /**
236  * au_verify_buf16 -  Verify chip data against buffer
237  * @mtd:        MTD device structure
238  * @buf:        buffer containing the data to compare
239  * @len:        number of bytes to compare
240  *
241  *  verify function for 16bit buswith
242  */
243 static int au_verify_buf16(struct mtd_info *mtd, const u_char *buf, int len)
244 {
245         int i;
246         struct nand_chip *this = mtd->priv;
247         u16 *p = (u16 *) buf;
248         len >>= 1;
249
250         for (i = 0; i < len; i++) {
251                 if (p[i] != readw(this->IO_ADDR_R))
252                         return -EFAULT;
253                 au_sync();
254         }
255         return 0;
256 }
257
258 /* Select the chip by setting nCE to low */
259 #define NAND_CTL_SETNCE         1
260 /* Deselect the chip by setting nCE to high */
261 #define NAND_CTL_CLRNCE         2
262 /* Select the command latch by setting CLE to high */
263 #define NAND_CTL_SETCLE         3
264 /* Deselect the command latch by setting CLE to low */
265 #define NAND_CTL_CLRCLE         4
266 /* Select the address latch by setting ALE to high */
267 #define NAND_CTL_SETALE         5
268 /* Deselect the address latch by setting ALE to low */
269 #define NAND_CTL_CLRALE         6
270
271 static void au1550_hwcontrol(struct mtd_info *mtd, int cmd)
272 {
273         register struct nand_chip *this = mtd->priv;
274
275         switch (cmd) {
276
277         case NAND_CTL_SETCLE:
278                 this->IO_ADDR_W = p_nand + MEM_STNAND_CMD;
279                 break;
280
281         case NAND_CTL_CLRCLE:
282                 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
283                 break;
284
285         case NAND_CTL_SETALE:
286                 this->IO_ADDR_W = p_nand + MEM_STNAND_ADDR;
287                 break;
288
289         case NAND_CTL_CLRALE:
290                 this->IO_ADDR_W = p_nand + MEM_STNAND_DATA;
291                 /* FIXME: Nobody knows why this is necessary,
292                  * but it works only that way */
293                 udelay(1);
294                 break;
295
296         case NAND_CTL_SETNCE:
297                 /* assert (force assert) chip enable */
298                 au_writel((1 << (4 + NAND_CS)), MEM_STNDCTL);
299                 break;
300
301         case NAND_CTL_CLRNCE:
302                 /* deassert chip enable */
303                 au_writel(0, MEM_STNDCTL);
304                 break;
305         }
306
307         this->IO_ADDR_R = this->IO_ADDR_W;
308
309         /* Drain the writebuffer */
310         au_sync();
311 }
312
313 int au1550_device_ready(struct mtd_info *mtd)
314 {
315         int ret = (au_readl(MEM_STSTAT) & 0x1) ? 1 : 0;
316         au_sync();
317         return ret;
318 }
319
320 /**
321  * au1550_select_chip - control -CE line
322  *      Forbid driving -CE manually permitting the NAND controller to do this.
323  *      Keeping -CE asserted during the whole sector reads interferes with the
324  *      NOR flash and PCMCIA drivers as it causes contention on the static bus.
325  *      We only have to hold -CE low for the NAND read commands since the flash
326  *      chip needs it to be asserted during chip not ready time but the NAND
327  *      controller keeps it released.
328  *
329  * @mtd:        MTD device structure
330  * @chip:       chipnumber to select, -1 for deselect
331  */
332 static void au1550_select_chip(struct mtd_info *mtd, int chip)
333 {
334 }
335
336 /**
337  * au1550_command - Send command to NAND device
338  * @mtd:        MTD device structure
339  * @command:    the command to be sent
340  * @column:     the column address for this command, -1 if none
341  * @page_addr:  the page address for this command, -1 if none
342  */
343 static void au1550_command(struct mtd_info *mtd, unsigned command, int column, int page_addr)
344 {
345         register struct nand_chip *this = mtd->priv;
346         int ce_override = 0, i;
347         ulong flags;
348
349         /* Begin command latch cycle */
350         au1550_hwcontrol(mtd, NAND_CTL_SETCLE);
351         /*
352          * Write out the command to the device.
353          */
354         if (command == NAND_CMD_SEQIN) {
355                 int readcmd;
356
357                 if (column >= mtd->writesize) {
358                         /* OOB area */
359                         column -= mtd->writesize;
360                         readcmd = NAND_CMD_READOOB;
361                 } else if (column < 256) {
362                         /* First 256 bytes --> READ0 */
363                         readcmd = NAND_CMD_READ0;
364                 } else {
365                         column -= 256;
366                         readcmd = NAND_CMD_READ1;
367                 }
368                 au1550_write_byte(mtd, readcmd);
369         }
370         au1550_write_byte(mtd, command);
371
372         /* Set ALE and clear CLE to start address cycle */
373         au1550_hwcontrol(mtd, NAND_CTL_CLRCLE);
374
375         if (column != -1 || page_addr != -1) {
376                 au1550_hwcontrol(mtd, NAND_CTL_SETALE);
377
378                 /* Serially input address */
379                 if (column != -1) {
380                         /* Adjust columns for 16 bit buswidth */
381                         if (this->options & NAND_BUSWIDTH_16)
382                                 column >>= 1;
383                         au1550_write_byte(mtd, column);
384                 }
385                 if (page_addr != -1) {
386                         au1550_write_byte(mtd, (u8)(page_addr & 0xff));
387
388                         if (command == NAND_CMD_READ0 ||
389                             command == NAND_CMD_READ1 ||
390                             command == NAND_CMD_READOOB) {
391                                 /*
392                                  * NAND controller will release -CE after
393                                  * the last address byte is written, so we'll
394                                  * have to forcibly assert it. No interrupts
395                                  * are allowed while we do this as we don't
396                                  * want the NOR flash or PCMCIA drivers to
397                                  * steal our precious bytes of data...
398                                  */
399                                 ce_override = 1;
400                                 local_irq_save(flags);
401                                 au1550_hwcontrol(mtd, NAND_CTL_SETNCE);
402                         }
403
404                         au1550_write_byte(mtd, (u8)(page_addr >> 8));
405
406                         /* One more address cycle for devices > 32MiB */
407                         if (this->chipsize > (32 << 20))
408                                 au1550_write_byte(mtd, (u8)((page_addr >> 16) & 0x0f));
409                 }
410                 /* Latch in address */
411                 au1550_hwcontrol(mtd, NAND_CTL_CLRALE);
412         }
413
414         /*
415          * Program and erase have their own busy handlers.
416          * Status and sequential in need no delay.
417          */
418         switch (command) {
419
420         case NAND_CMD_PAGEPROG:
421         case NAND_CMD_ERASE1:
422         case NAND_CMD_ERASE2:
423         case NAND_CMD_SEQIN:
424         case NAND_CMD_STATUS:
425                 return;
426
427         case NAND_CMD_RESET:
428                 break;
429
430         case NAND_CMD_READ0:
431         case NAND_CMD_READ1:
432         case NAND_CMD_READOOB:
433                 /* Check if we're really driving -CE low (just in case) */
434                 if (unlikely(!ce_override))
435                         break;
436
437                 /* Apply a short delay always to ensure that we do wait tWB. */
438                 ndelay(100);
439                 /* Wait for a chip to become ready... */
440                 for (i = this->chip_delay; !this->dev_ready(mtd) && i > 0; --i)
441                         udelay(1);
442
443                 /* Release -CE and re-enable interrupts. */
444                 au1550_hwcontrol(mtd, NAND_CTL_CLRNCE);
445                 local_irq_restore(flags);
446                 return;
447         }
448         /* Apply this short delay always to ensure that we do wait tWB. */
449         ndelay(100);
450
451         while(!this->dev_ready(mtd));
452 }
453
454
455 /*
456  * Main initialization routine
457  */
458 static int __init au1xxx_nand_init(void)
459 {
460         struct nand_chip *this;
461         u16 boot_swapboot = 0;  /* default value */
462         int retval;
463         u32 mem_staddr;
464         u32 nand_phys;
465
466         /* Allocate memory for MTD device structure and private data */
467         au1550_mtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip), GFP_KERNEL);
468         if (!au1550_mtd) {
469                 printk("Unable to allocate NAND MTD dev structure.\n");
470                 return -ENOMEM;
471         }
472
473         /* Get pointer to private data */
474         this = (struct nand_chip *)(&au1550_mtd[1]);
475
476         /* Initialize structures */
477         memset(au1550_mtd, 0, sizeof(struct mtd_info));
478         memset(this, 0, sizeof(struct nand_chip));
479
480         /* Link the private data with the MTD structure */
481         au1550_mtd->priv = this;
482         au1550_mtd->owner = THIS_MODULE;
483
484
485         /* MEM_STNDCTL: disable ints, disable nand boot */
486         au_writel(0, MEM_STNDCTL);
487
488 #ifdef CONFIG_MIPS_PB1550
489         /* set gpio206 high */
490         au_writel(au_readl(GPIO2_DIR) & ~(1 << 6), GPIO2_DIR);
491
492         boot_swapboot = (au_readl(MEM_STSTAT) & (0x7 << 1)) | ((bcsr->status >> 6) & 0x1);
493         switch (boot_swapboot) {
494         case 0:
495         case 2:
496         case 8:
497         case 0xC:
498         case 0xD:
499                 /* x16 NAND Flash */
500                 nand_width = 0;
501                 break;
502         case 1:
503         case 9:
504         case 3:
505         case 0xE:
506         case 0xF:
507                 /* x8 NAND Flash */
508                 nand_width = 1;
509                 break;
510         default:
511                 printk("Pb1550 NAND: bad boot:swap\n");
512                 retval = -EINVAL;
513                 goto outmem;
514         }
515 #endif
516
517         /* Configure chip-select; normally done by boot code, e.g. YAMON */
518 #ifdef NAND_STCFG
519         if (NAND_CS == 0) {
520                 au_writel(NAND_STCFG,  MEM_STCFG0);
521                 au_writel(NAND_STTIME, MEM_STTIME0);
522                 au_writel(NAND_STADDR, MEM_STADDR0);
523         }
524         if (NAND_CS == 1) {
525                 au_writel(NAND_STCFG,  MEM_STCFG1);
526                 au_writel(NAND_STTIME, MEM_STTIME1);
527                 au_writel(NAND_STADDR, MEM_STADDR1);
528         }
529         if (NAND_CS == 2) {
530                 au_writel(NAND_STCFG,  MEM_STCFG2);
531                 au_writel(NAND_STTIME, MEM_STTIME2);
532                 au_writel(NAND_STADDR, MEM_STADDR2);
533         }
534         if (NAND_CS == 3) {
535                 au_writel(NAND_STCFG,  MEM_STCFG3);
536                 au_writel(NAND_STTIME, MEM_STTIME3);
537                 au_writel(NAND_STADDR, MEM_STADDR3);
538         }
539 #endif
540
541         /* Locate NAND chip-select in order to determine NAND phys address */
542         mem_staddr = 0x00000000;
543         if (((au_readl(MEM_STCFG0) & 0x7) == 0x5) && (NAND_CS == 0))
544                 mem_staddr = au_readl(MEM_STADDR0);
545         else if (((au_readl(MEM_STCFG1) & 0x7) == 0x5) && (NAND_CS == 1))
546                 mem_staddr = au_readl(MEM_STADDR1);
547         else if (((au_readl(MEM_STCFG2) & 0x7) == 0x5) && (NAND_CS == 2))
548                 mem_staddr = au_readl(MEM_STADDR2);
549         else if (((au_readl(MEM_STCFG3) & 0x7) == 0x5) && (NAND_CS == 3))
550                 mem_staddr = au_readl(MEM_STADDR3);
551
552         if (mem_staddr == 0x00000000) {
553                 printk("Au1xxx NAND: ERROR WITH NAND CHIP-SELECT\n");
554                 kfree(au1550_mtd);
555                 return 1;
556         }
557         nand_phys = (mem_staddr << 4) & 0xFFFC0000;
558
559         p_nand = (void __iomem *)ioremap(nand_phys, 0x1000);
560
561         /* make controller and MTD agree */
562         if (NAND_CS == 0)
563                 nand_width = au_readl(MEM_STCFG0) & (1 << 22);
564         if (NAND_CS == 1)
565                 nand_width = au_readl(MEM_STCFG1) & (1 << 22);
566         if (NAND_CS == 2)
567                 nand_width = au_readl(MEM_STCFG2) & (1 << 22);
568         if (NAND_CS == 3)
569                 nand_width = au_readl(MEM_STCFG3) & (1 << 22);
570
571         /* Set address of hardware control function */
572         this->dev_ready = au1550_device_ready;
573         this->select_chip = au1550_select_chip;
574         this->cmdfunc = au1550_command;
575
576         /* 30 us command delay time */
577         this->chip_delay = 30;
578         this->ecc.mode = NAND_ECC_SOFT;
579
580         this->options = NAND_NO_AUTOINCR;
581
582         if (!nand_width)
583                 this->options |= NAND_BUSWIDTH_16;
584
585         this->read_byte = (!nand_width) ? au_read_byte16 : au_read_byte;
586         au1550_write_byte = (!nand_width) ? au_write_byte16 : au_write_byte;
587         this->read_word = au_read_word;
588         this->write_buf = (!nand_width) ? au_write_buf16 : au_write_buf;
589         this->read_buf = (!nand_width) ? au_read_buf16 : au_read_buf;
590         this->verify_buf = (!nand_width) ? au_verify_buf16 : au_verify_buf;
591
592         /* Scan to find existence of the device */
593         if (nand_scan(au1550_mtd, 1)) {
594                 retval = -ENXIO;
595                 goto outio;
596         }
597
598         /* Register the partitions */
599         add_mtd_partitions(au1550_mtd, partition_info, ARRAY_SIZE(partition_info));
600
601         return 0;
602
603  outio:
604         iounmap((void *)p_nand);
605
606  outmem:
607         kfree(au1550_mtd);
608         return retval;
609 }
610
611 module_init(au1xxx_nand_init);
612
613 /*
614  * Clean up routine
615  */
616 static void __exit au1550_cleanup(void)
617 {
618         struct nand_chip *this = (struct nand_chip *)&au1550_mtd[1];
619
620         /* Release resources, unregister device */
621         nand_release(au1550_mtd);
622
623         /* Free the MTD device structure */
624         kfree(au1550_mtd);
625
626         /* Unmap */
627         iounmap((void *)p_nand);
628 }
629
630 module_exit(au1550_cleanup);
631
632 MODULE_LICENSE("GPL");
633 MODULE_AUTHOR("Embedded Edge, LLC");
634 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on Pb1550 board");