vserver 1.9.3
[linux-2.6.git] / include / linux / mtd / map.h
1
2 /* Overhauled routines for dealing with different mmap regions of flash */
3 /* $Id: map.h,v 1.43 2004/07/14 13:30:27 dwmw2 Exp $ */
4
5 #ifndef __LINUX_MTD_MAP_H__
6 #define __LINUX_MTD_MAP_H__
7
8 #include <linux/config.h>
9 #include <linux/types.h>
10 #include <linux/list.h>
11 #include <asm/unaligned.h>
12 #include <asm/system.h>
13 #include <asm/io.h>
14 #include <asm/bug.h>
15
16 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
17 #define map_bankwidth(map) 1
18 #define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
19 #define map_bankwidth_is_large(map) (0)
20 #define map_words(map) (1)
21 #define MAX_MAP_BANKWIDTH 1
22 #else
23 #define map_bankwidth_is_1(map) (0)
24 #endif
25
26 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
27 # ifdef map_bankwidth
28 #  undef map_bankwidth
29 #  define map_bankwidth(map) ((map)->bankwidth)
30 # else
31 #  define map_bankwidth(map) 2
32 #  define map_bankwidth_is_large(map) (0)
33 #  define map_words(map) (1)
34 # endif
35 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
36 #undef MAX_MAP_BANKWIDTH
37 #define MAX_MAP_BANKWIDTH 2
38 #else
39 #define map_bankwidth_is_2(map) (0)
40 #endif
41
42 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
43 # ifdef map_bankwidth
44 #  undef map_bankwidth
45 #  define map_bankwidth(map) ((map)->bankwidth)
46 # else
47 #  define map_bankwidth(map) 4
48 #  define map_bankwidth_is_large(map) (0)
49 #  define map_words(map) (1)
50 # endif
51 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
52 #undef MAX_MAP_BANKWIDTH
53 #define MAX_MAP_BANKWIDTH 4
54 #else
55 #define map_bankwidth_is_4(map) (0)
56 #endif
57
58 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
59 # ifdef map_bankwidth
60 #  undef map_bankwidth
61 #  define map_bankwidth(map) ((map)->bankwidth)
62 #  if BITS_PER_LONG < 64
63 #   undef map_bankwidth_is_large
64 #   define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
65 #   undef map_words
66 #   define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
67 #  endif
68 # else
69 #  define map_bankwidth(map) 8
70 #  define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
71 #  define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
72 # endif
73 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
74 #undef MAX_MAP_BANKWIDTH
75 #define MAX_MAP_BANKWIDTH 8
76 #else
77 #define map_bankwidth_is_8(map) (0)
78 #endif
79
80 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
81 # ifdef map_bankwidth
82 #  undef map_bankwidth
83 #  define map_bankwidth(map) ((map)->bankwidth)
84 #  undef map_bankwidth_is_large
85 #  define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
86 #  undef map_words
87 #  define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
88 # else
89 #  define map_bankwidth(map) 16
90 #  define map_bankwidth_is_large(map) (1)
91 #  define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
92 # endif
93 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
94 #undef MAX_MAP_BANKWIDTH
95 #define MAX_MAP_BANKWIDTH 16
96 #else
97 #define map_bankwidth_is_16(map) (0)
98 #endif
99
100 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
101 # ifdef map_bankwidth
102 #  undef map_bankwidth
103 #  define map_bankwidth(map) ((map)->bankwidth)
104 #  undef map_bankwidth_is_large
105 #  define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
106 #  undef map_words
107 #  define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
108 # else
109 #  define map_bankwidth(map) 32
110 #  define map_bankwidth_is_large(map) (1)
111 #  define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
112 # endif
113 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
114 #undef MAX_MAP_BANKWIDTH
115 #define MAX_MAP_BANKWIDTH 32
116 #else
117 #define map_bankwidth_is_32(map) (0)
118 #endif
119
120 #ifndef map_bankwidth
121 #error "No bus width supported. What's the point?"
122 #endif
123
124 static inline int map_bankwidth_supported(int w)
125 {
126         switch (w) {
127 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
128         case 1:
129 #endif
130 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
131         case 2:
132 #endif
133 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
134         case 4:
135 #endif
136 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
137         case 8:
138 #endif
139 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
140         case 16:
141 #endif
142 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
143         case 32:
144 #endif
145                 return 1;
146
147         default:
148                 return 0;
149         }
150 }
151
152 #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
153
154 typedef union {
155         unsigned long x[MAX_MAP_LONGS];
156 } map_word;
157
158 /* The map stuff is very simple. You fill in your struct map_info with
159    a handful of routines for accessing the device, making sure they handle
160    paging etc. correctly if your device needs it. Then you pass it off
161    to a chip probe routine -- either JEDEC or CFI probe or both -- via
162    do_map_probe(). If a chip is recognised, the probe code will invoke the
163    appropriate chip driver (if present) and return a struct mtd_info.
164    At which point, you fill in the mtd->module with your own module 
165    address, and register it with the MTD core code. Or you could partition
166    it and register the partitions instead, or keep it for your own private
167    use; whatever.
168    
169    The mtd->priv field will point to the struct map_info, and any further
170    private data required by the chip driver is linked from the 
171    mtd->priv->fldrv_priv field. This allows the map driver to get at 
172    the destructor function map->fldrv_destroy() when it's tired
173    of living.
174 */
175
176 struct map_info {
177         char *name;
178         unsigned long size;
179         unsigned long phys;
180 #define NO_XIP (-1UL)
181
182         void __iomem *virt;
183         void *cached;
184
185         int bankwidth; /* in octets. This isn't necessarily the width
186                        of actual bus cycles -- it's the repeat interval
187                       in bytes, before you are talking to the first chip again.
188                       */
189
190 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
191         map_word (*read)(struct map_info *, unsigned long);
192         void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
193
194         void (*write)(struct map_info *, const map_word, unsigned long);
195         void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
196
197         /* We can perhaps put in 'point' and 'unpoint' methods, if we really
198            want to enable XIP for non-linear mappings. Not yet though. */
199 #endif
200         /* It's possible for the map driver to use cached memory in its
201            copy_from implementation (and _only_ with copy_from).  However,
202            when the chip driver knows some flash area has changed contents,
203            it will signal it to the map driver through this routine to let
204            the map driver invalidate the corresponding cache as needed.
205            If there is no cache to care about this can be set to NULL. */
206         void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
207
208         /* set_vpp() must handle being reentered -- enable, enable, disable 
209            must leave it enabled. */
210         void (*set_vpp)(struct map_info *, int);
211
212         unsigned long map_priv_1;
213         unsigned long map_priv_2;
214         void *fldrv_priv;
215         struct mtd_chip_driver *fldrv;
216 };
217
218 struct mtd_chip_driver {
219         struct mtd_info *(*probe)(struct map_info *map);
220         void (*destroy)(struct mtd_info *);
221         struct module *module;
222         char *name;
223         struct list_head list;
224 };
225
226 void register_mtd_chip_driver(struct mtd_chip_driver *);
227 void unregister_mtd_chip_driver(struct mtd_chip_driver *);
228
229 struct mtd_info *do_map_probe(const char *name, struct map_info *map);
230 void map_destroy(struct mtd_info *mtd);
231
232 #define ENABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 1); } while(0)
233 #define DISABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 0); } while(0)
234
235 #define INVALIDATE_CACHED_RANGE(map, from, size) \
236         do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
237
238
239 static inline int map_word_equal(struct map_info *map, map_word val1, map_word val2)
240 {
241         int i;
242         for (i=0; i<map_words(map); i++) {
243                 if (val1.x[i] != val2.x[i])
244                         return 0;
245         }
246         return 1;
247 }
248
249 static inline map_word map_word_and(struct map_info *map, map_word val1, map_word val2)
250 {
251         map_word r;
252         int i;
253
254         for (i=0; i<map_words(map); i++) {
255                 r.x[i] = val1.x[i] & val2.x[i];
256         }
257         return r;
258 }
259
260 static inline map_word map_word_or(struct map_info *map, map_word val1, map_word val2)
261 {
262         map_word r;
263         int i;
264
265         for (i=0; i<map_words(map); i++) {
266                 r.x[i] = val1.x[i] | val2.x[i];
267         }
268         return r;
269 }
270 #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
271
272 static inline int map_word_bitsset(struct map_info *map, map_word val1, map_word val2)
273 {
274         int i;
275
276         for (i=0; i<map_words(map); i++) {
277                 if (val1.x[i] & val2.x[i])
278                         return 1;
279         }
280         return 0;
281 }
282
283 static inline map_word map_word_load(struct map_info *map, const void *ptr)
284 {
285         map_word r;
286
287         if (map_bankwidth_is_1(map))
288                 r.x[0] = *(unsigned char *)ptr;
289         else if (map_bankwidth_is_2(map))
290                 r.x[0] = get_unaligned((uint16_t *)ptr);
291         else if (map_bankwidth_is_4(map))
292                 r.x[0] = get_unaligned((uint32_t *)ptr);
293 #if BITS_PER_LONG >= 64
294         else if (map_bankwidth_is_8(map))
295                 r.x[0] = get_unaligned((uint64_t *)ptr);
296 #endif
297         else if (map_bankwidth_is_large(map))
298                 memcpy(r.x, ptr, map->bankwidth);
299
300         return r;
301 }
302
303 static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
304 {
305         int i;
306
307         if (map_bankwidth_is_large(map)) {
308                 char *dest = (char *)&orig;
309                 memcpy(dest+start, buf, len);
310         } else {
311                 for (i=start; i < start+len; i++) {
312                         int bitpos;
313 #ifdef __LITTLE_ENDIAN
314                         bitpos = i*8;
315 #else /* __BIG_ENDIAN */
316                         bitpos = (map_bankwidth(map)-1-i)*8;
317 #endif
318                         orig.x[0] &= ~(0xff << bitpos);
319                         orig.x[0] |= buf[i] << bitpos;
320                 }
321         }
322         return orig;
323 }
324
325 static inline map_word map_word_ff(struct map_info *map)
326 {
327         map_word r;
328         int i;
329
330         for (i=0; i<map_words(map); i++) {
331                 r.x[i] = ~0UL;
332         }
333         return r;
334 }
335 static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
336 {
337         map_word r;
338
339         if (map_bankwidth_is_1(map))
340                 r.x[0] = __raw_readb(map->virt + ofs);
341         else if (map_bankwidth_is_2(map))
342                 r.x[0] = __raw_readw(map->virt + ofs);
343         else if (map_bankwidth_is_4(map))
344                 r.x[0] = __raw_readl(map->virt + ofs);
345 #if BITS_PER_LONG >= 64
346         else if (map_bankwidth_is_8(map))
347                 r.x[0] = __raw_readq(map->virt + ofs);
348 #endif
349         else if (map_bankwidth_is_large(map))
350                 memcpy_fromio(r.x, map->virt+ofs, map->bankwidth);
351
352         return r;
353 }
354
355 static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
356 {
357         if (map_bankwidth_is_1(map))
358                 __raw_writeb(datum.x[0], map->virt + ofs);
359         else if (map_bankwidth_is_2(map))
360                 __raw_writew(datum.x[0], map->virt + ofs);
361         else if (map_bankwidth_is_4(map))
362                 __raw_writel(datum.x[0], map->virt + ofs);
363 #if BITS_PER_LONG >= 64
364         else if (map_bankwidth_is_8(map))
365                 __raw_writeq(datum.x[0], map->virt + ofs);
366 #endif
367         else if (map_bankwidth_is_large(map))
368                 memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
369         mb();
370 }
371
372 static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
373 {
374         if (map->cached)
375                 memcpy(to, (char *)map->cached + from, len);
376         else
377                 memcpy_fromio(to, map->virt + from, len);
378 }
379
380 static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
381 {
382         memcpy_toio(map->virt + to, from, len);
383 }
384
385 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
386 #define map_read(map, ofs) (map)->read(map, ofs)
387 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
388 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
389 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
390
391 extern void simple_map_init(struct map_info *);
392 #define map_is_linear(map) (map->phys != NO_XIP)
393
394 #else
395 #define map_read(map, ofs) inline_map_read(map, ofs)
396 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
397 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
398 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
399
400
401 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
402 #define map_is_linear(map) (1)
403
404 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
405
406 #endif /* __LINUX_MTD_MAP_H__ */