VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[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.20 2004/07/20 02:44: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  *   autronix autcpu12 board, which is a SmartMediaCard. It supports 
18  *   16MiB, 32MiB and 64MiB 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 static int autcpu12_io_base = CS89712_VIRT_BASE;
48 static int autcpu12_fio_pbase = AUTCPU12_PHYS_SMC;
49 static int autcpu12_fio_ctrl = AUTCPU12_SMC_SELECT_OFFSET;
50 static int autcpu12_pedr = AUTCPU12_SMC_PORT_OFFSET;
51 static int autcpu12_fio_base;
52
53 #ifdef MODULE
54 MODULE_PARM(autcpu12_fio_pbase, "i");
55 MODULE_PARM(autcpu12_fio_ctrl, "i");
56 MODULE_PARM(autcpu12_pedr, "i");
57
58 __setup("autcpu12_fio_pbase=",autcpu12_fio_pbase);
59 __setup("autcpu12_fio_ctrl=",autcpu12_fio_ctrl);
60 __setup("autcpu12_pedr=",autcpu12_pedr);
61 #endif
62
63 /*
64  * Define partitions for flash devices
65  */
66 static struct mtd_partition partition_info16k[] = {
67         { .name         = "AUTCPU12 flash partition 1",
68           .offset       = 0,
69           .size         = 8 * SZ_1M },
70         { .name         = "AUTCPU12 flash partition 2",
71           .offset       = 8 * SZ_1M,
72           .size         = 8 * SZ_1M },
73 };
74
75 static struct mtd_partition partition_info32k[] = {
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         = 24 * SZ_1M },
82 };
83
84 static struct mtd_partition partition_info64k[] = {
85         { .name         = "AUTCPU12 flash partition 1",
86           .offset       = 0,
87           .size         = 16 * SZ_1M },
88         { .name         = "AUTCPU12 flash partition 2",
89           .offset       = 16 * SZ_1M,
90           .size         = 48 * SZ_1M },
91 };
92
93 static struct mtd_partition partition_info128k[] = {
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         = 112 * SZ_1M },
100 };
101
102 #define NUM_PARTITIONS16K 2
103 #define NUM_PARTITIONS32K 2
104 #define NUM_PARTITIONS64K 2
105 #define NUM_PARTITIONS128K 2
106 /* 
107  *      hardware specific access to control-lines
108 */
109 static void autcpu12_hwcontrol(struct mtd_info *mtd, int cmd)
110 {
111
112         switch(cmd){
113
114                 case NAND_CTL_SETCLE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) |=  AUTCPU12_SMC_CLE; break;
115                 case NAND_CTL_CLRCLE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) &= ~AUTCPU12_SMC_CLE; break;
116
117                 case NAND_CTL_SETALE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) |=  AUTCPU12_SMC_ALE; break;
118                 case NAND_CTL_CLRALE: (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) &= ~AUTCPU12_SMC_ALE; break;
119
120                 case NAND_CTL_SETNCE: (*(volatile unsigned char *) (autcpu12_fio_base + autcpu12_fio_ctrl)) = 0x01; break;
121                 case NAND_CTL_CLRNCE: (*(volatile unsigned char *) (autcpu12_fio_base + autcpu12_fio_ctrl)) = 0x00; break;
122         }
123 }
124
125 /*
126 *       read device ready pin
127 */
128 int autcpu12_device_ready(struct mtd_info *mtd)
129 {
130
131         return ( (*(volatile unsigned char *) (autcpu12_io_base + autcpu12_pedr)) & AUTCPU12_SMC_RDY) ? 1 : 0;
132
133 }
134
135 /*
136  * Main initialization routine
137  */
138 int __init autcpu12_init (void)
139 {
140         struct nand_chip *this;
141         int err = 0;
142
143         /* Allocate memory for MTD device structure and private data */
144         autcpu12_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
145                                 GFP_KERNEL);
146         if (!autcpu12_mtd) {
147                 printk ("Unable to allocate AUTCPU12 NAND MTD device structure.\n");
148                 err = -ENOMEM;
149                 goto out;
150         }
151
152         /* map physical adress */
153         autcpu12_fio_base=(unsigned long)ioremap(autcpu12_fio_pbase,SZ_1K);
154         if(!autcpu12_fio_base){
155                 printk("Ioremap autcpu12 SmartMedia Card failed\n");
156                 err = -EIO;
157                 goto out_mtd;
158         }
159
160         /* Get pointer to private data */
161         this = (struct nand_chip *) (&autcpu12_mtd[1]);
162
163         /* Initialize structures */
164         memset((char *) autcpu12_mtd, 0, sizeof(struct mtd_info));
165         memset((char *) this, 0, sizeof(struct nand_chip));
166
167         /* Link the private data with the MTD structure */
168         autcpu12_mtd->priv = this;
169
170         /* Set address of NAND IO lines */
171         this->IO_ADDR_R = autcpu12_fio_base;
172         this->IO_ADDR_W = autcpu12_fio_base;
173         this->hwcontrol = autcpu12_hwcontrol;
174         this->dev_ready = autcpu12_device_ready;
175         /* 20 us command delay time */
176         this->chip_delay = 20;          
177         this->eccmode = NAND_ECC_SOFT;
178
179         /* Enable the following for a flash based bad block table */
180         /*
181         this->options = NAND_USE_FLASH_BBT;
182         */
183         this->options = NAND_USE_FLASH_BBT;
184         
185         /* Scan to find existance of the device */
186         if (nand_scan (autcpu12_mtd, 1)) {
187                 err = -ENXIO;
188                 goto out_ior;
189         }
190         
191         /* Register the partitions */
192         switch(autcpu12_mtd->size){
193                 case SZ_16M: add_mtd_partitions(autcpu12_mtd, partition_info16k, NUM_PARTITIONS16K); break;
194                 case SZ_32M: add_mtd_partitions(autcpu12_mtd, partition_info32k, NUM_PARTITIONS32K); break;
195                 case SZ_64M: add_mtd_partitions(autcpu12_mtd, partition_info64k, NUM_PARTITIONS64K); break; 
196                 case SZ_128M: add_mtd_partitions(autcpu12_mtd, partition_info128k, NUM_PARTITIONS128K); break; 
197                 default: {
198                         printk ("Unsupported SmartMedia device\n"); 
199                         err = -ENXIO;
200                         goto out_ior;
201                 }
202         }
203         goto out;
204
205 out_ior:
206         iounmap((void *)autcpu12_fio_base);
207 out_mtd:
208         kfree (autcpu12_mtd);
209 out:
210         return err;
211 }
212
213 module_init(autcpu12_init);
214
215 /*
216  * Clean up routine
217  */
218 #ifdef MODULE
219 static void __exit autcpu12_cleanup (void)
220 {
221         /* Release resources, unregister device */
222         nand_release (autcpu12_mtd);
223
224         /* unmap physical adress */
225         iounmap((void *)autcpu12_fio_base);
226         
227         /* Free the MTD device structure */
228         kfree (autcpu12_mtd);
229 }
230 module_exit(autcpu12_cleanup);
231 #endif
232
233 MODULE_LICENSE("GPL");
234 MODULE_AUTHOR("Thomas Gleixner <tglx@linutronix.de>");
235 MODULE_DESCRIPTION("Glue layer for SmartMediaCard on autronix autcpu12");