File: /usr/src/linux/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.10 2001/06/02 14:30:44 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     		(__u8*)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     #if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
217     #define init_elan_104nc init_module
218     #define cleanup_elan_104nc cleanup_module
219     #endif
220     
221     mod_exit_t cleanup_elan_104nc(void)
222     {
223     	if( all_mtd ) {
224     		del_mtd_partitions( all_mtd );
225     		map_destroy( all_mtd );
226     	}
227     
228     	iounmap((void *)iomapadr);
229     	release_region(PAGE_IO,PAGE_IO_SIZE);
230     }
231     
232     mod_init_t init_elan_104nc(void)
233     {
234     	/* Urg! We use I/O port 0x22 without request_region()ing it */
235     	/*
236     	if (check_region(PAGE_IO,PAGE_IO_SIZE) != 0) {
237     		printk( KERN_ERR"%s: IO ports 0x%x-0x%x in use\n",
238     			elan_104nc_map.name,
239     			PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1 );
240     		return -EAGAIN;
241     	}
242     	*/
243       	iomapadr = (unsigned long)ioremap(WINDOW_START, WINDOW_LENGTH);
244     	if (!iomapadr) {
245     		printk( KERN_ERR"%s: failed to ioremap memory region\n",
246     			elan_104nc_map.name );
247     		return -EIO;
248     	}
249     
250     	/*
251     	request_region( PAGE_IO, PAGE_IO_SIZE, "ELAN-104NC flash" );
252     	*/
253     
254     	printk( KERN_INFO"%s: IO:0x%x-0x%x MEM:0x%x-0x%x\n",
255     		elan_104nc_map.name,
256     		PAGE_IO, PAGE_IO+PAGE_IO_SIZE-1,
257     		WINDOW_START, WINDOW_START+WINDOW_LENGTH-1 );
258     
259     	elan_104nc_setup();
260     
261     	/* Probe for chip. */
262     	all_mtd = do_map_probe("cfi",  &elan_104nc_map );
263     	if( !all_mtd ) {
264     		cleanup_elan_104nc();
265     		return -ENXIO;
266     	}
267     	
268     	all_mtd->module=THIS_MODULE;
269     
270     	/* Create MTD devices for each partition. */
271     	add_mtd_partitions( all_mtd, partition_info, NUM_PARTITIONS );
272     
273     	return 0;
274     }
275     
276     module_init(init_elan_104nc);
277     module_exit(cleanup_elan_104nc);
278