ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / nand / spia.c
1 /*
2  *  drivers/mtd/nand/spia.c
3  *
4  *  Copyright (C) 2000 Steven J. Hill (sjhill@realitydiluted.com)
5  *
6  *
7  *      10-29-2001 TG   change to support hardwarespecific access
8  *                      to controllines (due to change in nand.c)
9  *                      page_cache added
10  *
11  * $Id: spia.c,v 1.19 2003/04/20 07:24:40 gleixner Exp $
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  *
17  *  Overview:
18  *   This is a device driver for the NAND flash device found on the
19  *   SPIA board which utilizes the Toshiba TC58V64AFT part. This is
20  *   a 64Mibit (8MiB x 8 bits) NAND flash device.
21  */
22
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/nand.h>
27 #include <linux/mtd/partitions.h>
28 #include <asm/io.h>
29
30 /*
31  * MTD structure for SPIA board
32  */
33 static struct mtd_info *spia_mtd = NULL;
34
35 /*
36  * Values specific to the SPIA board (used with EP7212 processor)
37  */
38 #define SPIA_IO_ADDR    = 0xd0000000    /* Start of EP7212 IO address space */
39 #define SPIA_FIO_ADDR   = 0xf0000000    /* Address where flash is mapped */
40 #define SPIA_PEDR       = 0x0080        /*
41                                          * IO offset to Port E data register
42                                          * where the CLE, ALE and NCE pins
43                                          * are wired to.
44                                          */
45 #define SPIA_PEDDR      = 0x00c0        /*
46                                          * IO offset to Port E data direction
47                                          * register so we can control the IO
48                                          * lines.
49                                          */
50
51 /*
52  * Module stuff
53  */
54
55 static int spia_io_base = SPIA_IO_BASE;
56 static int spia_fio_base = SPIA_FIO_BASE;
57 static int spia_pedr = SPIA_PEDR;
58 static int spia_peddr = SPIA_PEDDR;
59
60 MODULE_PARM(spia_io_base, "i");
61 MODULE_PARM(spia_fio_base, "i");
62 MODULE_PARM(spia_pedr, "i");
63 MODULE_PARM(spia_peddr, "i");
64
65 __setup("spia_io_base=",spia_io_base);
66 __setup("spia_fio_base=",spia_fio_base);
67 __setup("spia_pedr=",spia_pedr);
68 __setup("spia_peddr=",spia_peddr);
69
70 /*
71  * Define partitions for flash device
72  */
73 const static struct mtd_partition partition_info[] = {
74         {
75                 .name   = "SPIA flash partition 1",
76                 .offset = 0,
77                 .size   = 2*1024*1024
78         },
79         {
80                 .name   = "SPIA flash partition 2",
81                 .offset = 2*1024*1024,
82                 .size   = 6*1024*1024
83         }
84 };
85 #define NUM_PARTITIONS 2
86
87
88 /* 
89  *      hardware specific access to control-lines
90 */
91 void spia_hwcontrol(int cmd){
92
93     switch(cmd){
94
95         case NAND_CTL_SETCLE: (*(volatile unsigned char *) (spia_io_base + spia_pedr)) |=  0x01; break;
96         case NAND_CTL_CLRCLE: (*(volatile unsigned char *) (spia_io_base + spia_pedr)) &= ~0x01; break;
97
98         case NAND_CTL_SETALE: (*(volatile unsigned char *) (spia_io_base + spia_pedr)) |=  0x02; break;
99         case NAND_CTL_CLRALE: (*(volatile unsigned char *) (spia_io_base + spia_pedr)) &= ~0x02; break;
100
101         case NAND_CTL_SETNCE: (*(volatile unsigned char *) (spia_io_base + spia_pedr)) &= ~0x04; break;
102         case NAND_CTL_CLRNCE: (*(volatile unsigned char *) (spia_io_base + spia_pedr)) |=  0x04; break;
103     }
104 }
105
106 /*
107  * Main initialization routine
108  */
109 int __init spia_init (void)
110 {
111         struct nand_chip *this;
112
113         /* Allocate memory for MTD device structure and private data */
114         spia_mtd = kmalloc (sizeof(struct mtd_info) + sizeof (struct nand_chip),
115                                 GFP_KERNEL);
116         if (!spia_mtd) {
117                 printk ("Unable to allocate SPIA NAND MTD device structure.\n");
118                 return -ENOMEM;
119         }
120
121         /* Get pointer to private data */
122         this = (struct nand_chip *) (&spia_mtd[1]);
123
124         /* Initialize structures */
125         memset((char *) spia_mtd, 0, sizeof(struct mtd_info));
126         memset((char *) this, 0, sizeof(struct nand_chip));
127
128         /* Link the private data with the MTD structure */
129         spia_mtd->priv = this;
130
131         /*
132          * Set GPIO Port E control register so that the pins are configured
133          * to be outputs for controlling the NAND flash.
134          */
135         (*(volatile unsigned char *) (spia_io_base + spia_peddr)) = 0x07;
136
137         /* Set address of NAND IO lines */
138         this->IO_ADDR_R = spia_fio_base;
139         this->IO_ADDR_W = spia_fio_base;
140         /* Set address of hardware control function */
141         this->hwcontrol = spia_hwcontrol;
142         /* 15 us command delay time */
143         this->chip_delay = 15;          
144
145         /* Scan to find existence of the device */
146         if (nand_scan (spia_mtd)) {
147                 kfree (spia_mtd);
148                 return -ENXIO;
149         }
150
151         /* Allocate memory for internal data buffer */
152         this->data_buf = kmalloc (sizeof(u_char) * (spia_mtd->oobblock + spia_mtd->oobsize), GFP_KERNEL);
153         if (!this->data_buf) {
154                 printk ("Unable to allocate NAND data buffer for SPIA.\n");
155                 kfree (spia_mtd);
156                 return -ENOMEM;
157         }
158
159         /* Register the partitions */
160         add_mtd_partitions(spia_mtd, partition_info, NUM_PARTITIONS);
161
162         /* Return happy */
163         return 0;
164 }
165 module_init(spia_init);
166
167 /*
168  * Clean up routine
169  */
170 #ifdef MODULE
171 static void __exit spia_cleanup (void)
172 {
173         struct nand_chip *this = (struct nand_chip *) &spia_mtd[1];
174
175         /* Unregister the device */
176         del_mtd_device (spia_mtd);
177
178         /* Free internal data buffer */
179         kfree (this->data_buf);
180
181         /* Free the MTD device structure */
182         kfree (spia_mtd);
183 }
184 module_exit(spia_cleanup);
185 #endif
186
187 MODULE_LICENSE("GPL");
188 MODULE_AUTHOR("Steven J. Hill <sjhill@realitydiluted.com");
189 MODULE_DESCRIPTION("Board-specific glue layer for NAND flash on SPIA board");