VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / mtd / nand / edb7312.c
1 /*
2  *  drivers/mtd/nand/edb7312.c
3  *
4  *  Copyright (C) 2002 Marius Gröger (mag@sysgo.de)
5  *
6  *  Derived from drivers/mtd/nand/autcpu12.c
7  *       Copyright (c) 2001 Thomas Gleixner (gleixner@autronix.de)
8  *
9  * $Id: edb7312.c,v 1.8 2004/07/12 15:03:26 dwmw2 Exp $
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  *
15  *  Overview:
16  *   This is a device driver for the NAND flash device found on the
17  *   CLEP7312 board which utilizes the Toshiba TC58V64AFT part. This is
18  *   a 64Mibit (8MiB x 8 bits) NAND flash device.
19  */
20
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24 #include <linux/mtd/mtd.h>
25 #include <linux/mtd/nand.h>
26 #include <linux/mtd/partitions.h>
27 #include <asm/io.h>
28 #include <asm/arch/hardware.h> /* for CLPS7111_VIRT_BASE */
29 #include <asm/sizes.h>
30 #include <asm/hardware/clps7111.h>
31
32 /*
33  * MTD structure for EDB7312 board
34  */
35 static struct mtd_info *ep7312_mtd = NULL;
36
37 /*
38  * Values specific to the EDB7312 board (used with EP7312 processor)
39  */
40 #define EP7312_FIO_PBASE 0x10000000     /* Phys address of flash */
41 #define EP7312_PXDR     0x0001  /*
42                                  * IO offset to Port B data register
43                                  * where the CLE, ALE and NCE pins
44                                  * are wired to.
45                                  */
46 #define EP7312_PXDDR    0x0041  /*
47                                  * IO offset to Port B data direction
48                                  * register so we can control the IO
49                                  * lines.
50                                  */
51
52 /*
53  * Module stuff
54  */
55
56 static int ep7312_fio_pbase = EP7312_FIO_PBASE;
57 static int ep7312_pxdr = EP7312_PXDR;
58 static int ep7312_pxddr = EP7312_PXDDR;
59
60 #ifdef MODULE
61 MODULE_PARM(ep7312_fio_pbase, "i");
62 MODULE_PARM(ep7312_pxdr, "i");
63 MODULE_PARM(ep7312_pxddr, "i");
64
65 __setup("ep7312_fio_pbase=",ep7312_fio_pbase);
66 __setup("ep7312_pxdr=",ep7312_pxdr);
67 __setup("ep7312_pxddr=",ep7312_pxddr);
68 #endif
69
70 #ifdef CONFIG_MTD_PARTITIONS
71 /*
72  * Define static partitions for flash device
73  */
74 static struct mtd_partition partition_info[] = {
75         { .name = "EP7312 Nand Flash",
76                   .offset = 0,
77                   .size = 8*1024*1024 }
78 };
79 #define NUM_PARTITIONS 1
80
81 #endif
82
83
84 /* 
85  *      hardware specific access to control-lines
86  */
87 static void ep7312_hwcontrol(struct mtd_info *mtd, int cmd) 
88 {
89         switch(cmd) {
90                 
91         case NAND_CTL_SETCLE: 
92                 clps_writeb(clps_readb(ep7312_pxdr) | 0x10, ep7312_pxdr); 
93                 break;
94         case NAND_CTL_CLRCLE: 
95                 clps_writeb(clps_readb(ep7312_pxdr) & ~0x10, ep7312_pxdr);
96                 break;
97                 
98         case NAND_CTL_SETALE:
99                 clps_writeb(clps_readb(ep7312_pxdr) | 0x20, ep7312_pxdr);
100                 break;
101         case NAND_CTL_CLRALE:
102                 clps_writeb(clps_readb(ep7312_pxdr) & ~0x20, ep7312_pxdr);
103                 break;
104                 
105         case NAND_CTL_SETNCE:
106                 clps_writeb((clps_readb(ep7312_pxdr) | 0x80) & ~0x40, ep7312_pxdr);
107                 break;
108         case NAND_CTL_CLRNCE:
109                 clps_writeb((clps_readb(ep7312_pxdr) | 0x80) | 0x40, ep7312_pxdr);
110                 break;
111         }
112 }
113
114 /*
115  *      read device ready pin
116  */
117 static int ep7312_device_ready(struct mtd_info *mtd)
118 {
119         return 1;
120 }
121 #ifdef CONFIG_MTD_PARTITIONS
122 const char *part_probes[] = { "cmdlinepart", NULL };
123 #endif
124
125 /*
126  * Main initialization routine
127  */
128 static int __init ep7312_init (void)
129 {
130         struct nand_chip *this;
131         const char *part_type = 0;
132         int mtd_parts_nb = 0;
133         struct mtd_partition *mtd_parts = 0;
134         int ep7312_fio_base;
135         
136         /* Allocate memory for MTD device structure and private data */
137         ep7312_mtd = kmalloc(sizeof(struct mtd_info) + 
138                              sizeof(struct nand_chip),
139                              GFP_KERNEL);
140         if (!ep7312_mtd) {
141                 printk("Unable to allocate EDB7312 NAND MTD device structure.\n");
142                 return -ENOMEM;
143         }
144         
145         /* map physical adress */
146         ep7312_fio_base = (unsigned long)ioremap(ep7312_fio_pbase, SZ_1K);
147         if(!ep7312_fio_base) {
148                 printk("ioremap EDB7312 NAND flash failed\n");
149                 kfree(ep7312_mtd);
150                 return -EIO;
151         }
152         
153         /* Get pointer to private data */
154         this = (struct nand_chip *) (&ep7312_mtd[1]);
155         
156         /* Initialize structures */
157         memset((char *) ep7312_mtd, 0, sizeof(struct mtd_info));
158         memset((char *) this, 0, sizeof(struct nand_chip));
159         
160         /* Link the private data with the MTD structure */
161         ep7312_mtd->priv = this;
162         
163         /*
164          * Set GPIO Port B control register so that the pins are configured
165          * to be outputs for controlling the NAND flash.
166          */
167         clps_writeb(0xf0, ep7312_pxddr);
168         
169         /* insert callbacks */
170         this->IO_ADDR_R = ep7312_fio_base;
171         this->IO_ADDR_W = ep7312_fio_base;
172         this->hwcontrol = ep7312_hwcontrol;
173         this->dev_ready = ep7312_device_ready;
174         /* 15 us command delay time */
175         this->chip_delay = 15;
176         
177         /* Scan to find existence of the device */
178         if (nand_scan (ep7312_mtd, 1)) {
179                 iounmap((void *)ep7312_fio_base);
180                 kfree (ep7312_mtd);
181                 return -ENXIO;
182         }
183         
184         /* Allocate memory for internal data buffer */
185         this->data_buf = kmalloc (sizeof(u_char) * (ep7312_mtd->oobblock + ep7312_mtd->oobsize), GFP_KERNEL);
186         if (!this->data_buf) {
187                 printk("Unable to allocate NAND data buffer for EDB7312.\n");
188                 iounmap((void *)ep7312_fio_base);
189                 kfree (ep7312_mtd);
190                 return -ENOMEM;
191         }
192         
193 #ifdef CONFIG_PARTITIONS
194         ep7312_mtd->name = "edb7312-nand";
195         mtd_parts_nb = parse_mtd_partitions(ep7312_mtd, part_probes,
196                                             &mtd_parts, 0);
197         if (mtd_parts_nb > 0)
198                 part_type = "command line";
199         else
200                 mtd_parts_nb = 0;
201 #endif
202         if (mtd_parts_nb == 0) {
203                 mtd_parts = partition_info;
204                 mtd_parts_nb = NUM_PARTITIONS;
205                 part_type = "static";
206         }
207         
208         /* Register the partitions */
209         printk(KERN_NOTICE "Using %s partition definition\n", part_type);
210         add_mtd_partitions(ep7312_mtd, mtd_parts, mtd_parts_nb);
211         
212         /* Return happy */
213         return 0;
214 }
215 module_init(ep7312_init);
216
217 /*
218  * Clean up routine
219  */
220 static void __exit ep7312_cleanup (void)
221 {
222         struct nand_chip *this = (struct nand_chip *) &ep7312_mtd[1];
223         
224         /* Unregister the device */
225         del_mtd_device (ep7312_mtd);
226         
227         /* Free internal data buffer */
228         kfree (this->data_buf);
229         
230         /* Free the MTD device structure */
231         kfree (ep7312_mtd);
232 }
233 module_exit(ep7312_cleanup);
234
235 MODULE_LICENSE("GPL");
236 MODULE_AUTHOR("Marius Groeger <mag@sysgo.de>");
237 MODULE_DESCRIPTION("MTD map driver for Cogent EDB7312 board");