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