ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / nand / autcpu12.c
1 /*
2  *  drivers/mtd/autcpu12.c
3  *
4  *  Copyright (c) 2002 Thomas Gleixner <tgxl@linutronix.de>
5  *
6  *  Derived from drivers/mtd/spia.c
7  *       Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
8  * 
9  * $Id: autcpu12.c,v 1.11 2003/06/04 17:04:09 gleixner 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  *   autronix autcpu12 board, which is a SmartMediaCard. It supports 
18  *   16MB, 32MB and 64MB cards.
19  *
20  *
21  *      02-12-2002 TG   Cleanup of module params
22  *
23  *      02-20-2002 TG   adjusted for different rd/wr adress support
24  *                      added support for read device ready/busy line
25  *                      added page_cache
26  *
27  *      10-06-2002 TG   128K card support added
28  */
29
30 #include <linux/version.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/nand.h>
36 #include <linux/mtd/partitions.h>
37 #include <asm/io.h>
38 #include <asm/arch/hardware.h>
39 #include <asm/sizes.h>
40 #include <asm/arch/autcpu12.h>
41
42 /*
43  * MTD structure for AUTCPU12 board
44  */
45 static struct mtd_info *autcpu12_mtd = NULL;
46
47 /*
48  * Module stuff
49  */
50 #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
51 #define autcpu12_init init_module
52 #define autcpu12_cleanup cleanup_module
53 #endif
54
55 static int autcpu12_io_base = CS89712_VIRT_BASE;
56 static int autcpu12_fio_pbase = AUTCPU12_PHYS_SMC;
57 static int autcpu12_fio_ctrl = AUTCPU12_SMC_SELECT_OFFSET;
58 static int autcpu12_pedr = AUTCPU12_SMC_PORT_OFFSET;
59 static int autcpu12_fio_base;
60
61 #ifdef MODULE
62 MODULE_PARM(autcpu12_fio_pbase, "i");
63 MODULE_PARM(autcpu12_fio_ctrl, "i");
64 MODULE_PARM(autcpu12_pedr, "i");
65
66 __setup("autcpu12_fio_pbase=",autcpu12_fio_pbase);
67 __setup("autcpu12_fio_ctrl=",autcpu12_fio_ctrl);
68 __setup("autcpu12_pedr=",autcpu12_pedr);
69 #endif
70
71 /*
72  * Define partitions for flash devices
73  */
74 extern struct nand_oobinfo jffs2_oobinfo;
75
76 static struct mtd_partition partition_info16k[] = {
77         { .name = "AUTCPU12 flash partition 1",
78           .offset  = 0,
79           .size =    8 * SZ_1M },
80         { .name = "AUTCPU12 flash partition 2",
81           .offset =  8 * SZ_1M,
82           .size =    8 * SZ_1M },
83 };
84
85 static struct mtd_partition partition_info32k[] = {
86         { .name = "AUTCPU12 flash partition 1",
87           .offset  = 0,
88           .size =    8 * SZ_1M },
89         { .name = "AUTCPU12 flash partition 2",
90           .offset =  8 * SZ_1M,
91           .size =   24 * SZ_1M },
92 };
93
94 static struct mtd_partition partition_info64k[] = {
95         { .name = "AUTCPU12 flash partition 1",
96           .offset  = 0,
97           .size =   16 * SZ_1M },
98         { .name = "AUTCPU12 flash partition 2",
99           .offset = 16 * SZ_1M,
100           .size =   48 * SZ_1M },
101 };
102
103 static struct mtd_partition partition_info128k[] = {
104         { .name = "AUTCPU12 flash partition 1",
105           .offset  = 0,
106           .size =   16 * SZ_1M },
107         { .name = "AUTCPU12 flash partition 2",
108           .offset = 16 * SZ_1M,
109           .size =   112 * SZ_1M },
110 };
111
112 #define NUM_PARTITIONS16K 2
113 #define NUM_PARTITIONS32K 2
114 #define NUM_PARTITIONS64K 2
115 #define NUM_PARTITIONS128K 2
116 /* 
117  *      hardware specific access to control-lines
118 */
119 void autcpu12_hwcontrol(int cmd)
120 {
121
122         switch(cmd){
123
124                 case NAND_CTL_SETCLE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) |=  AUTCPU12_SMC_CLE; break;
125                 case NAND_CTL_CLRCLE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) &= ~AUTCPU12_SMC_CLE; break;
126
127                 case NAND_CTL_SETALE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) |=  AUTCPU12_SMC_ALE; break;
128                 case NAND_CTL_CLRALE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) &= ~AUTCPU12_SMC_ALE; break;
129
130                 case NAND_CTL_SETNCE: (*(volatile unsigned char *) (autcpu12_fio_base + autcpu12_fio_ctrl)) = 0x01; break;
131                 case NAND_CTL_CLRNCE: (*(volatile unsigned char *) (autcpu12_fio_base + autcpu12_fio_ctrl)) = 0x00; break;
132         }
133 }
134
135 /*
136 *       read device ready pin
137 */
138 int autcpu12_device_ready(void)
139 {
140
141         return ( (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) & AUTCPU12_SMC_RDY) ? 1 : 0;
142
143 }
144 /*
145  * Main initialization routine
146  */
147 int __init autcpu12_init (void)
148 {
149         struct nand_chip *this;
150         int err = 0;
151
152         /* Allocate memory for MTD device structure and private data */
153         autcpu12_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
154                                 GFP_KERNEL);
155         if (!autcpu12_mtd) {
156                 printk ("Unable to allocate AUTCPU12 NAND MTD device structure.\n");
157                 err = -ENOMEM;
158                 goto out;
159         }
160
161         /* map physical adress */
162         autcpu12_fio_base=(unsigned long)ioremap(autcpu12_fio_pbase,SZ_1K);
163         if(!autcpu12_fio_base){
164                 printk("Ioremap autcpu12 SmartMedia Card failed\n");
165                 err = -EIO;
166                 goto out_mtd;
167         }
168
169         /* Get pointer to private data */
170         this = (struct nand_chip *) (&autcpu12_mtd[1]);
171
172         /* Initialize structures */
173         memset((char *) autcpu12_mtd, 0, sizeof(struct mtd_info));
174         memset((char *) this, 0, sizeof(struct nand_chip));
175
176         /* Link the private data with the MTD structure */
177         autcpu12_mtd->priv = this;
178
179         /* Set address of NAND IO lines */
180         this->IO_ADDR_R = autcpu12_fio_base;
181         this->IO_ADDR_W = autcpu12_fio_base;
182         this->hwcontrol = autcpu12_hwcontrol;
183         this->dev_ready = autcpu12_device_ready;
184         /* 20 us command delay time */
185         this->chip_delay = 20;          
186         this->eccmode = NAND_ECC_SOFT;
187
188         /* Scan to find existance of the device */
189         if (nand_scan (autcpu12_mtd)) {
190                 err = -ENXIO;
191                 goto out_ior;
192         }
193
194         /* Allocate memory for internal data buffer */
195         this->data_buf = kmalloc (sizeof(u_char) * (autcpu12_mtd->oobblock + autcpu12_mtd->oobsize), GFP_KERNEL);
196         if (!this->data_buf) {
197                 printk ("Unable to allocate NAND data buffer for AUTCPU12.\n");
198                 err = -ENOMEM;
199                 goto out_ior;
200         }
201
202         /* Register the partitions */
203         switch(autcpu12_mtd->size){
204                 case SZ_16M: add_mtd_partitions(autcpu12_mtd, partition_info16k, NUM_PARTITIONS16K); break;
205                 case SZ_32M: add_mtd_partitions(autcpu12_mtd, partition_info32k, NUM_PARTITIONS32K); break;
206                 case SZ_64M: add_mtd_partitions(autcpu12_mtd, partition_info64k, NUM_PARTITIONS64K); break; 
207                 case SZ_128M: add_mtd_partitions(autcpu12_mtd, partition_info128k, NUM_PARTITIONS128K); break; 
208                 default: {
209                         printk ("Unsupported SmartMedia device\n"); 
210                         err = -ENXIO;
211                         goto out_buf;
212                 }
213         }
214         goto out;
215
216 out_buf:
217         kfree (this->data_buf);    
218 out_ior:
219         iounmap((void *)autcpu12_fio_base);
220 out_mtd:
221         kfree (autcpu12_mtd);
222 out:
223         return err;
224 }
225
226 module_init(autcpu12_init);
227
228 /*
229  * Clean up routine
230  */
231 #ifdef MODULE
232 static void __exit autcpu12_cleanup (void)
233 {
234         struct nand_chip *this = (struct nand_chip *) &autcpu12_mtd[1];
235
236         /* Unregister partitions */
237         del_mtd_partitions(autcpu12_mtd);
238         
239         /* Unregister the device */
240         del_mtd_device (autcpu12_mtd);
241
242         /* Free internal data buffers */
243         kfree (this->data_buf);
244
245         /* unmap physical adress */
246         iounmap((void *)autcpu12_fio_base);
247
248         /* Free the MTD device structure */
249         kfree (autcpu12_mtd);
250 }
251 module_exit(autcpu12_cleanup);
252 #endif
253
254 MODULE_LICENSE("GPL");
255 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
256 MODULE_DESCRIPTION("Glue layer for SmartMediaCard on autronix autcpu12");