1 /* elan-104nc.c -- MTD map driver for Arcom Control Systems ELAN-104NC
3 Copyright (C) 2000 Arcom Control System Ltd
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.
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.
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
19 $Id: elan-104nc.c,v 1.13 2002/02/13 15:30:20 dwmw2 Exp $
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.
24 The flash is accessed as follows:
26 32 kbyte memory window at 0xb0000-0xb7fff
28 16 bit I/O port (0x22) for some sort of paging.
30 The single flash device is divided into 3 partition which appear as seperate
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.
36 #include <linux/module.h>
37 #include <linux/slab.h>
38 #include <linux/ioport.h>
39 #include <linux/init.h>
42 #include <linux/mtd/map.h>
43 #include <linux/mtd/partitions.h>
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)
52 #define PAGE_IO_SIZE 2
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;
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
61 static struct mtd_partition partition_info[]={
62 { name: "ELAN-104NC flash boot partition",
65 { name: "ELAN-104NC flash partition 1",
68 { name: "ELAN-104NC flash partition 2",
69 offset: (640+896)*1024 }
71 #define NUM_PARTITIONS (sizeof(partition_info)/sizeof(partition_info[0]))
74 * If no idea what is going on here. This is taken from the FlashFX stuff.
78 static inline void elan_104nc_setup(void)
82 outw( 0x0023 + ROMCS*2, PAGE_IO );
87 outw( ((0x0023 + ROMCS*2) | (t << 8)), PAGE_IO );
90 static inline void elan_104nc_page(struct map_info *map, unsigned long ofs)
92 unsigned long page = ofs >> WINDOW_SHIFT;
94 if( page!=page_in_window ) {
98 cmd1=(page & 0x700) + 0x0833 + ROMCS*0x4000;
99 cmd2=((page & 0xff) << 8) + 0x0032;
101 outw( cmd1, PAGE_IO );
102 outw( cmd2, PAGE_IO );
104 page_in_window = page;
109 static __u8 elan_104nc_read8(struct map_info *map, unsigned long ofs)
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);
119 static __u16 elan_104nc_read16(struct map_info *map, unsigned long ofs)
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);
129 static __u32 elan_104nc_read32(struct map_info *map, unsigned long ofs)
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);
139 static void elan_104nc_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
142 unsigned long thislen = len;
143 if (len > (WINDOW_LENGTH - (from & WINDOW_MASK)))
144 thislen = WINDOW_LENGTH-(from & WINDOW_MASK);
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);
156 static void elan_104nc_write8(struct map_info *map, __u8 d, unsigned long adr)
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);
164 static void elan_104nc_write16(struct map_info *map, __u16 d, unsigned long adr)
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);
172 static void elan_104nc_write32(struct map_info *map, __u32 d, unsigned long adr)
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);
180 static void elan_104nc_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
183 unsigned long thislen = len;
184 if (len > (WINDOW_LENGTH - (to & WINDOW_MASK)))
185 thislen = WINDOW_LENGTH-(to & WINDOW_MASK);
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);
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
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
213 /* MTD device for all of the flash. */
214 static struct mtd_info *all_mtd;
216 static void cleanup_elan_104nc(void)
219 del_mtd_partitions( all_mtd );
220 map_destroy( all_mtd );
223 iounmap((void *)iomapadr);
224 release_region(PAGE_IO,PAGE_IO_SIZE);
227 int __init init_elan_104nc(void)
229 /* Urg! We use I/O port 0x22 without request_region()ing it */
231 if (check_region(PAGE_IO,PAGE_IO_SIZE) != 0) {
232 printk( KERN_ERR"%s: IO ports 0x%x-0x%x in use\n",
234 PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1 );
238 iomapadr = (unsigned long)ioremap(WINDOW_START, WINDOW_LENGTH);
240 printk( KERN_ERR"%s: failed to ioremap memory region\n",
241 elan_104nc_map.name );
246 request_region( PAGE_IO, PAGE_IO_SIZE, "ELAN-104NC flash" );
249 printk( KERN_INFO"%s: IO:0x%x-0x%x MEM:0x%x-0x%x\n",
251 PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1,
252 WINDOW_START, WINDOW_START+WINDOW_LENGTH-1 );
256 /* Probe for chip. */
257 all_mtd = do_map_probe("cfi_probe", &elan_104nc_map );
259 cleanup_elan_104nc();
263 all_mtd->module=THIS_MODULE;
265 /* Create MTD devices for each partition. */
266 add_mtd_partitions( all_mtd, partition_info, NUM_PARTITIONS );
271 module_init(init_elan_104nc);
272 module_exit(cleanup_elan_104nc);
275 MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("Arcom Control Systems Ltd.");
277 MODULE_DESCRIPTION("MTD map driver for Arcom Control Systems ELAN-104NC");