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