import of upstream 2.4.34.4 from kernel.org
[linux-2.4.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.13 2002/02/13 15:30:20 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 seperate
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/partitions.h>
44
45 #define WINDOW_START 0xb0000
46 /* Number of bits in offset. */
47 #define WINDOW_SHIFT 15
48 #define WINDOW_LENGTH (1 << WINDOW_SHIFT)
49 /* The bits for the offset into the window. */
50 #define WINDOW_MASK (WINDOW_LENGTH-1)
51 #define PAGE_IO 0x22
52 #define PAGE_IO_SIZE 2
53
54 static volatile int page_in_window = -1; // Current page in window.
55 static unsigned long iomapadr;
56 static spinlock_t elan_104nc_spin = SPIN_LOCK_UNLOCKED;
57
58 /* partition_info gives details on the logical partitions that the split the 
59  * single flash device into. If the size if zero we use up to the end of the
60  * device. */
61 static struct mtd_partition partition_info[]={
62     { name: "ELAN-104NC flash boot partition", 
63       offset: 0, 
64       size: 640*1024 },
65     { name: "ELAN-104NC flash partition 1", 
66       offset: 640*1024, 
67       size: 896*1024 },
68     { name: "ELAN-104NC flash partition 2", 
69       offset: (640+896)*1024 }
70 };
71 #define NUM_PARTITIONS (sizeof(partition_info)/sizeof(partition_info[0]))
72
73 /*
74  * If no idea what is going on here.  This is taken from the FlashFX stuff.
75  */
76 #define ROMCS 1
77
78 static inline void elan_104nc_setup(void)
79 {
80     u16 t;
81
82     outw( 0x0023 + ROMCS*2, PAGE_IO );
83     t=inb( PAGE_IO+1 );
84
85     t=(t & 0xf9) | 0x04;
86
87     outw( ((0x0023 + ROMCS*2) | (t << 8)), PAGE_IO );
88 }
89
90 static inline void elan_104nc_page(struct map_info *map, unsigned long ofs)
91 {
92         unsigned long page = ofs >> WINDOW_SHIFT;
93        
94         if( page!=page_in_window ) {
95                 int cmd1;
96                 int cmd2;
97
98                 cmd1=(page & 0x700) + 0x0833 + ROMCS*0x4000;
99                 cmd2=((page & 0xff) << 8) + 0x0032;
100
101                 outw( cmd1, PAGE_IO );
102                 outw( cmd2, PAGE_IO );
103
104                 page_in_window = page;
105         }
106 }
107
108
109 static __u8 elan_104nc_read8(struct map_info *map, unsigned long ofs)
110 {
111         __u8 ret;
112         spin_lock(&elan_104nc_spin);
113         elan_104nc_page(map, ofs);
114         ret = readb(iomapadr + (ofs & WINDOW_MASK));
115         spin_unlock(&elan_104nc_spin);
116         return ret;
117 }
118
119 static __u16 elan_104nc_read16(struct map_info *map, unsigned long ofs)
120 {
121         __u16 ret;
122         spin_lock(&elan_104nc_spin);
123         elan_104nc_page(map, ofs);
124         ret = readw(iomapadr + (ofs & WINDOW_MASK));
125         spin_unlock(&elan_104nc_spin);
126         return ret;
127 }
128
129 static __u32 elan_104nc_read32(struct map_info *map, unsigned long ofs)
130 {
131         __u32 ret;
132         spin_lock(&elan_104nc_spin);
133         elan_104nc_page(map, ofs);
134         ret = readl(iomapadr + (ofs & WINDOW_MASK));
135         spin_unlock(&elan_104nc_spin);
136         return ret;
137 }
138
139 static void elan_104nc_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
140 {
141         while(len) {
142                 unsigned long thislen = len;
143                 if (len > (WINDOW_LENGTH - (from & WINDOW_MASK)))
144                         thislen = WINDOW_LENGTH-(from & WINDOW_MASK);
145                 
146                 spin_lock(&elan_104nc_spin);
147                 elan_104nc_page(map, from);
148                 memcpy_fromio(to, iomapadr + (from & WINDOW_MASK), thislen);
149                 spin_unlock(&elan_104nc_spin);
150                 to += thislen;
151                 from += thislen;
152                 len -= thislen;
153         }
154 }
155
156 static void elan_104nc_write8(struct map_info *map, __u8 d, unsigned long adr)
157 {
158         spin_lock(&elan_104nc_spin);
159         elan_104nc_page(map, adr);
160         writeb(d, iomapadr + (adr & WINDOW_MASK));
161         spin_unlock(&elan_104nc_spin);
162 }
163
164 static void elan_104nc_write16(struct map_info *map, __u16 d, unsigned long adr)
165 {
166         spin_lock(&elan_104nc_spin);
167         elan_104nc_page(map, adr);
168         writew(d, iomapadr + (adr & WINDOW_MASK));
169         spin_unlock(&elan_104nc_spin);
170 }
171
172 static void elan_104nc_write32(struct map_info *map, __u32 d, unsigned long adr)
173 {
174         spin_lock(&elan_104nc_spin);
175         elan_104nc_page(map, adr);
176         writel(d, iomapadr + (adr & WINDOW_MASK));
177         spin_unlock(&elan_104nc_spin);
178 }
179
180 static void elan_104nc_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
181 {       
182         while(len) {
183                 unsigned long thislen = len;
184                 if (len > (WINDOW_LENGTH - (to & WINDOW_MASK)))
185                         thislen = WINDOW_LENGTH-(to & WINDOW_MASK);
186                 
187                 spin_lock(&elan_104nc_spin);
188                 elan_104nc_page(map, to);
189                 memcpy_toio(iomapadr + (to & WINDOW_MASK), from, thislen);
190                 spin_unlock(&elan_104nc_spin);
191                 to += thislen;
192                 from += thislen;
193                 len -= thislen;
194         }
195 }
196
197 static struct map_info elan_104nc_map = {
198         name: "ELAN-104NC flash",
199         size: 8*1024*1024, /* this must be set to a maximum possible amount
200                         of flash so the cfi probe routines find all
201                         the chips */
202         buswidth: 2,
203         read8: elan_104nc_read8,
204         read16: elan_104nc_read16,
205         read32: elan_104nc_read32,
206         copy_from: elan_104nc_copy_from,
207         write8: elan_104nc_write8,
208         write16: elan_104nc_write16,
209         write32: elan_104nc_write32,
210         copy_to: elan_104nc_copy_to
211 };
212
213 /* MTD device for all of the flash. */
214 static struct mtd_info *all_mtd;
215
216 static void cleanup_elan_104nc(void)
217 {
218         if( all_mtd ) {
219                 del_mtd_partitions( all_mtd );
220                 map_destroy( all_mtd );
221         }
222
223         iounmap((void *)iomapadr);
224         release_region(PAGE_IO,PAGE_IO_SIZE);
225 }
226
227 int __init init_elan_104nc(void)
228 {
229         /* Urg! We use I/O port 0x22 without request_region()ing it */
230         /*
231         if (check_region(PAGE_IO,PAGE_IO_SIZE) != 0) {
232                 printk( KERN_ERR"%s: IO ports 0x%x-0x%x in use\n",
233                         elan_104nc_map.name,
234                         PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1 );
235                 return -EAGAIN;
236         }
237         */
238         iomapadr = (unsigned long)ioremap(WINDOW_START, WINDOW_LENGTH);
239         if (!iomapadr) {
240                 printk( KERN_ERR"%s: failed to ioremap memory region\n",
241                         elan_104nc_map.name );
242                 return -EIO;
243         }
244
245         /*
246         request_region( PAGE_IO, PAGE_IO_SIZE, "ELAN-104NC flash" );
247         */
248
249         printk( KERN_INFO"%s: IO:0x%x-0x%x MEM:0x%x-0x%x\n",
250                 elan_104nc_map.name,
251                 PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1,
252                 WINDOW_START, WINDOW_START+WINDOW_LENGTH-1 );
253
254         elan_104nc_setup();
255
256         /* Probe for chip. */
257         all_mtd = do_map_probe("cfi_probe",  &elan_104nc_map );
258         if( !all_mtd ) {
259                 cleanup_elan_104nc();
260                 return -ENXIO;
261         }
262         
263         all_mtd->module=THIS_MODULE;
264
265         /* Create MTD devices for each partition. */
266         add_mtd_partitions( all_mtd, partition_info, NUM_PARTITIONS );
267
268         return 0;
269 }
270
271 module_init(init_elan_104nc);
272 module_exit(cleanup_elan_104nc);
273
274
275 MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("Arcom Control Systems Ltd.");
277 MODULE_DESCRIPTION("MTD map driver for Arcom Control Systems ELAN-104NC");