ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / maps / elan-104nc.c
1 /* elan-104nc.c -- MTD map driver for Arcom Control Systems ELAN-104NC
2  
3    Copyright (C) 2000 Arcom Control System Ltd
4  
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9  
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18
19    $Id: elan-104nc.c,v 1.18 2003/06/23 07:37:02 dwmw2 Exp $
20
21 The ELAN-104NC has up to 8 Mibyte of Intel StrataFlash (28F320/28F640) in x16
22 mode.  This drivers uses the CFI probe and Intel Extended Command Set drivers.
23
24 The flash is accessed as follows:
25
26    32 kbyte memory window at 0xb0000-0xb7fff
27    
28    16 bit I/O port (0x22) for some sort of paging.
29
30 The single flash device is divided into 3 partition which appear as separate
31 MTD devices.
32
33 Linux thinks that the I/O port is used by the PIC and hence check_region() will
34 always fail.  So we don't do it.  I just hope it doesn't break anything.
35 */
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/ioport.h>
39 #include <linux/init.h>
40 #include <asm/io.h>
41
42 #include <linux/mtd/map.h>
43 #include <linux/mtd/mtd.h>
44 #include <linux/mtd/partitions.h>
45
46 #define WINDOW_START 0xb0000
47 /* Number of bits in offset. */
48 #define WINDOW_SHIFT 15
49 #define WINDOW_LENGTH (1 << WINDOW_SHIFT)
50 /* The bits for the offset into the window. */
51 #define WINDOW_MASK (WINDOW_LENGTH-1)
52 #define PAGE_IO 0x22
53 #define PAGE_IO_SIZE 2
54
55 static volatile int page_in_window = -1; // Current page in window.
56 static unsigned long iomapadr;
57 static spinlock_t elan_104nc_spin = SPIN_LOCK_UNLOCKED;
58
59 /* partition_info gives details on the logical partitions that the split the 
60  * single flash device into. If the size if zero we use up to the end of the
61  * device. */
62 static struct mtd_partition partition_info[]={
63     { .name = "ELAN-104NC flash boot partition", 
64       .offset = 0, 
65       .size = 640*1024 },
66     { .name = "ELAN-104NC flash partition 1", 
67       .offset = 640*1024, 
68       .size = 896*1024 },
69     { .name = "ELAN-104NC flash partition 2", 
70       .offset = (640+896)*1024 }
71 };
72 #define NUM_PARTITIONS (sizeof(partition_info)/sizeof(partition_info[0]))
73
74 /*
75  * If no idea what is going on here.  This is taken from the FlashFX stuff.
76  */
77 #define ROMCS 1
78
79 static inline void elan_104nc_setup(void)
80 {
81     u16 t;
82
83     outw( 0x0023 + ROMCS*2, PAGE_IO );
84     t=inb( PAGE_IO+1 );
85
86     t=(t & 0xf9) | 0x04;
87
88     outw( ((0x0023 + ROMCS*2) | (t << 8)), PAGE_IO );
89 }
90
91 static inline void elan_104nc_page(struct map_info *map, unsigned long ofs)
92 {
93         unsigned long page = ofs >> WINDOW_SHIFT;
94        
95         if( page!=page_in_window ) {
96                 int cmd1;
97                 int cmd2;
98
99                 cmd1=(page & 0x700) + 0x0833 + ROMCS*0x4000;
100                 cmd2=((page & 0xff) << 8) + 0x0032;
101
102                 outw( cmd1, PAGE_IO );
103                 outw( cmd2, PAGE_IO );
104
105                 page_in_window = page;
106         }
107 }
108
109
110 static __u8 elan_104nc_read8(struct map_info *map, unsigned long ofs)
111 {
112         __u8 ret;
113         spin_lock(&elan_104nc_spin);
114         elan_104nc_page(map, ofs);
115         ret = readb(iomapadr + (ofs & WINDOW_MASK));
116         spin_unlock(&elan_104nc_spin);
117         return ret;
118 }
119
120 static __u16 elan_104nc_read16(struct map_info *map, unsigned long ofs)
121 {
122         __u16 ret;
123         spin_lock(&elan_104nc_spin);
124         elan_104nc_page(map, ofs);
125         ret = readw(iomapadr + (ofs & WINDOW_MASK));
126         spin_unlock(&elan_104nc_spin);
127         return ret;
128 }
129
130 static __u32 elan_104nc_read32(struct map_info *map, unsigned long ofs)
131 {
132         __u32 ret;
133         spin_lock(&elan_104nc_spin);
134         elan_104nc_page(map, ofs);
135         ret = readl(iomapadr + (ofs & WINDOW_MASK));
136         spin_unlock(&elan_104nc_spin);
137         return ret;
138 }
139
140 static void elan_104nc_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
141 {
142         while(len) {
143                 unsigned long thislen = len;
144                 if (len > (WINDOW_LENGTH - (from & WINDOW_MASK)))
145                         thislen = WINDOW_LENGTH-(from & WINDOW_MASK);
146                 
147                 spin_lock(&elan_104nc_spin);
148                 elan_104nc_page(map, from);
149                 memcpy_fromio(to, iomapadr + (from & WINDOW_MASK), thislen);
150                 spin_unlock(&elan_104nc_spin);
151                 to += thislen;
152                 from += thislen;
153                 len -= thislen;
154         }
155 }
156
157 static void elan_104nc_write8(struct map_info *map, __u8 d, unsigned long adr)
158 {
159         spin_lock(&elan_104nc_spin);
160         elan_104nc_page(map, adr);
161         writeb(d, iomapadr + (adr & WINDOW_MASK));
162         spin_unlock(&elan_104nc_spin);
163 }
164
165 static void elan_104nc_write16(struct map_info *map, __u16 d, unsigned long adr)
166 {
167         spin_lock(&elan_104nc_spin);
168         elan_104nc_page(map, adr);
169         writew(d, iomapadr + (adr & WINDOW_MASK));
170         spin_unlock(&elan_104nc_spin);
171 }
172
173 static void elan_104nc_write32(struct map_info *map, __u32 d, unsigned long adr)
174 {
175         spin_lock(&elan_104nc_spin);
176         elan_104nc_page(map, adr);
177         writel(d, iomapadr + (adr & WINDOW_MASK));
178         spin_unlock(&elan_104nc_spin);
179 }
180
181 static void elan_104nc_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
182 {       
183         while(len) {
184                 unsigned long thislen = len;
185                 if (len > (WINDOW_LENGTH - (to & WINDOW_MASK)))
186                         thislen = WINDOW_LENGTH-(to & WINDOW_MASK);
187                 
188                 spin_lock(&elan_104nc_spin);
189                 elan_104nc_page(map, to);
190                 memcpy_toio(iomapadr + (to & WINDOW_MASK), from, thislen);
191                 spin_unlock(&elan_104nc_spin);
192                 to += thislen;
193                 from += thislen;
194                 len -= thislen;
195         }
196 }
197
198 static struct map_info elan_104nc_map = {
199         .name = "ELAN-104NC flash",
200         .phys = NO_XIP,
201         .size = 8*1024*1024, /* this must be set to a maximum possible amount
202                         of flash so the cfi probe routines find all
203                         the chips */
204         .buswidth = 2,
205         .read8 = elan_104nc_read8,
206         .read16 = elan_104nc_read16,
207         .read32 = elan_104nc_read32,
208         .copy_from = elan_104nc_copy_from,
209         .write8 = elan_104nc_write8,
210         .write16 = elan_104nc_write16,
211         .write32 = elan_104nc_write32,
212         .copy_to = elan_104nc_copy_to
213 };
214
215 /* MTD device for all of the flash. */
216 static struct mtd_info *all_mtd;
217
218 static void cleanup_elan_104nc(void)
219 {
220         if( all_mtd ) {
221                 del_mtd_partitions( all_mtd );
222                 map_destroy( all_mtd );
223         }
224
225         iounmap((void *)iomapadr);
226 }
227
228 int __init init_elan_104nc(void)
229 {
230         /* Urg! We use I/O port 0x22 without request_region()ing it,
231            because it's already allocated to the PIC. */
232
233         iomapadr = (unsigned long)ioremap(WINDOW_START, WINDOW_LENGTH);
234         if (!iomapadr) {
235                 printk( KERN_ERR"%s: failed to ioremap memory region\n",
236                         elan_104nc_map.name );
237                 return -EIO;
238         }
239
240         printk( KERN_INFO"%s: IO:0x%x-0x%x MEM:0x%x-0x%x\n",
241                 elan_104nc_map.name,
242                 PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1,
243                 WINDOW_START, WINDOW_START+WINDOW_LENGTH-1 );
244
245         elan_104nc_setup();
246
247         /* Probe for chip. */
248         all_mtd = do_map_probe("cfi_probe",  &elan_104nc_map );
249         if( !all_mtd ) {
250                 cleanup_elan_104nc();
251                 return -ENXIO;
252         }
253         
254         all_mtd->owner = THIS_MODULE;
255
256         /* Create MTD devices for each partition. */
257         add_mtd_partitions( all_mtd, partition_info, NUM_PARTITIONS );
258
259         return 0;
260 }
261
262 module_init(init_elan_104nc);
263 module_exit(cleanup_elan_104nc);
264
265
266 MODULE_LICENSE("GPL");
267 MODULE_AUTHOR("Arcom Control Systems Ltd.");
268 MODULE_DESCRIPTION("MTD map driver for Arcom Control Systems ELAN-104NC");