ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / mtd / maps / vmax301.c
1 // $Id: vmax301.c,v 1.28 2003/05/21 15:15:08 dwmw2 Exp $
2 /* ######################################################################
3
4    Tempustech VMAX SBC301 MTD Driver.
5   
6    The VMAx 301 is a SBC based on . It
7    comes with three builtin AMD 29F016B flash chips and a socket for SRAM or
8    more flash. Each unit has it's own 8k mapping into a settable region 
9    (0xD8000). There are two 8k mappings for each MTD, the first is always set
10    to the lower 8k of the device the second is paged. Writing a 16 bit page
11    value to anywhere in the first 8k will cause the second 8k to page around.
12
13    To boot the device a bios extension must be installed into the first 8k 
14    of flash that is smart enough to copy itself down, page in the rest of 
15    itself and begin executing.
16    
17    ##################################################################### */
18
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/ioport.h>
22 #include <linux/init.h>
23 #include <linux/spinlock.h>
24 #include <asm/io.h>
25
26 #include <linux/mtd/map.h>
27 #include <linux/mtd/mtd.h>
28
29
30 #define WINDOW_START 0xd8000
31 #define WINDOW_LENGTH 0x2000
32 #define WINDOW_SHIFT 25
33 #define WINDOW_MASK 0x1FFF
34
35 /* Actually we could use two spinlocks, but we'd have to have
36    more private space in the struct map_info. We lose a little
37    performance like this, but we'd probably lose more by having
38    the extra indirection from having one of the map->map_priv 
39    fields pointing to yet another private struct.
40 */
41 static spinlock_t vmax301_spin = SPIN_LOCK_UNLOCKED;
42
43 static void __vmax301_page(struct map_info *map, unsigned long page)
44 {
45         writew(page, map->map_priv_2 - WINDOW_LENGTH);
46         map->map_priv_1 = page;
47 }
48
49 static inline void vmax301_page(struct map_info *map,
50                                   unsigned long ofs)
51 {
52         unsigned long page = (ofs >> WINDOW_SHIFT);
53         if (map->map_priv_1 != page)
54                 __vmax301_page(map, page);
55 }
56
57 static __u8 vmax301_read8(struct map_info *map, unsigned long ofs)
58 {
59         __u8 ret;
60         spin_lock(&vmax301_spin);
61         vmax301_page(map, ofs);
62         ret = readb(map->map_priv_2 + (ofs & WINDOW_MASK));
63         spin_unlock(&vmax301_spin);
64         return ret;
65 }
66
67 static __u16 vmax301_read16(struct map_info *map, unsigned long ofs)
68 {
69         __u16 ret;
70         spin_lock(&vmax301_spin);
71         vmax301_page(map, ofs);
72         ret = readw(map->map_priv_2 + (ofs & WINDOW_MASK));
73         spin_unlock(&vmax301_spin);
74         return ret;
75 }
76
77 static __u32 vmax301_read32(struct map_info *map, unsigned long ofs)
78 {
79         __u32 ret;
80         spin_lock(&vmax301_spin);
81         vmax301_page(map, ofs);
82         ret =  readl(map->map_priv_2 + (ofs & WINDOW_MASK));
83         spin_unlock(&vmax301_spin);
84         return ret;
85 }
86
87 static void vmax301_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
88 {
89         while(len) {
90                 unsigned long thislen = len;
91                 if (len > (WINDOW_LENGTH - (from & WINDOW_MASK)))
92                         thislen = WINDOW_LENGTH-(from & WINDOW_MASK);
93                 spin_lock(&vmax301_spin);
94                 vmax301_page(map, from);
95                 memcpy_fromio(to, map->map_priv_2 + from, thislen);
96                 spin_unlock(&vmax301_spin);
97                 to += thislen;
98                 from += thislen;
99                 len -= thislen;
100         }
101 }
102
103 static void vmax301_write8(struct map_info *map, __u8 d, unsigned long adr)
104 {
105         spin_lock(&vmax301_spin);
106         vmax301_page(map, adr);
107         writeb(d, map->map_priv_2 + (adr & WINDOW_MASK));
108         spin_unlock(&vmax301_spin);
109 }
110
111 static void vmax301_write16(struct map_info *map, __u16 d, unsigned long adr)
112 {
113         spin_lock(&vmax301_spin);
114         vmax301_page(map, adr);
115         writew(d, map->map_priv_2 + (adr & WINDOW_MASK));
116         spin_unlock(&vmax301_spin);
117 }
118
119 static void vmax301_write32(struct map_info *map, __u32 d, unsigned long adr)
120 {
121         spin_lock(&vmax301_spin);
122         vmax301_page(map, adr);
123         writel(d, map->map_priv_2 + (adr & WINDOW_MASK));
124         spin_unlock(&vmax301_spin);
125 }
126
127 static void vmax301_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
128 {
129         while(len) {
130                 unsigned long thislen = len;
131                 if (len > (WINDOW_LENGTH - (to & WINDOW_MASK)))
132                         thislen = WINDOW_LENGTH-(to & WINDOW_MASK);
133
134                 spin_lock(&vmax301_spin);
135                 vmax301_page(map, to);
136                 memcpy_toio(map->map_priv_2 + to, from, thislen);
137                 spin_unlock(&vmax301_spin);             
138                 to += thislen;
139                 from += thislen;
140                 len -= thislen;
141         }
142 }
143
144 static struct map_info vmax_map[2] = {
145         {
146                 .name = "VMAX301 Internal Flash",
147                 .phys = NO_XIP,
148                 .size = 3*2*1024*1024,
149                 .buswidth = 1,
150                 .read8 = vmax301_read8,
151                 .read16 = vmax301_read16,
152                 .read32 = vmax301_read32,
153                 .copy_from = vmax301_copy_from,
154                 .write8 = vmax301_write8,
155                 .write16 = vmax301_write16,
156                 .write32 = vmax301_write32,
157                 .copy_to = vmax301_copy_to,
158                 .map_priv_1 = WINDOW_START + WINDOW_LENGTH,
159                 .map_priv_2 = 0xFFFFFFFF
160         },
161         {
162                 .name = "VMAX301 Socket",
163                 .phys = NO_XIP,
164                 .size = 0,
165                 .buswidth = 1,
166                 .read8 = vmax301_read8,
167                 .read16 = vmax301_read16,
168                 .read32 = vmax301_read32,
169                 .copy_from = vmax301_copy_from,
170                 .write8 = vmax301_write8,
171                 .write16 = vmax301_write16,
172                 .write32 = vmax301_write32,
173                 .copy_to = vmax301_copy_to,
174                 .map_priv_1 = WINDOW_START + (3*WINDOW_LENGTH),
175                 .map_priv_2 = 0xFFFFFFFF
176         }
177 };
178
179 static struct mtd_info *vmax_mtd[2] = {NULL, NULL};
180
181 static void __exit cleanup_vmax301(void)
182 {
183         int i;
184         
185         for (i=0; i<2; i++) {
186                 if (vmax_mtd[i]) {
187                         del_mtd_device(vmax_mtd[i]);
188                         map_destroy(vmax_mtd[i]);
189                 }
190         }
191         iounmap((void *)vmax_map[0].map_priv_1 - WINDOW_START);
192 }
193
194 int __init init_vmax301(void)
195 {
196         int i;
197         unsigned long iomapadr;
198         // Print out our little header..
199         printk("Tempustech VMAX 301 MEM:0x%x-0x%x\n",WINDOW_START,
200                WINDOW_START+4*WINDOW_LENGTH);
201
202         iomapadr = (unsigned long)ioremap(WINDOW_START, WINDOW_LENGTH*4);
203         if (!iomapadr) {
204                 printk("Failed to ioremap memory region\n");
205                 return -EIO;
206         }
207         /* Put the address in the map's private data area.
208            We store the actual MTD IO address rather than the 
209            address of the first half, because it's used more
210            often. 
211         */
212         vmax_map[0].map_priv_2 = iomapadr + WINDOW_START;
213         vmax_map[1].map_priv_2 = iomapadr + (3*WINDOW_START);
214         
215         for (i=0; i<2; i++) {
216                 vmax_mtd[i] = do_map_probe("cfi_probe", &vmax_map[i]);
217                 if (!vmax_mtd[i])
218                         vmax_mtd[i] = do_map_probe("jedec", &vmax_map[i]);
219                 if (!vmax_mtd[i])
220                         vmax_mtd[i] = do_map_probe("map_ram", &vmax_map[i]);
221                 if (!vmax_mtd[i])
222                         vmax_mtd[i] = do_map_probe("map_rom", &vmax_map[i]);
223                 if (vmax_mtd[i]) {
224                         vmax_mtd[i]->owner = THIS_MODULE;
225                         add_mtd_device(vmax_mtd[i]);
226                 }
227         }
228
229         if (!vmax_mtd[1] && !vmax_mtd[2]) {
230                 iounmap((void *)iomapadr);
231                 return -ENXIO;
232         }
233
234         return 0;
235 }
236
237 module_init(init_vmax301);
238 module_exit(cleanup_vmax301);
239
240 MODULE_LICENSE("GPL");
241 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
242 MODULE_DESCRIPTION("MTD map driver for Tempustech VMAX SBC301 board");