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 $ */
5 #ifndef __LINUX_MTD_MAP_H__
6 #define __LINUX_MTD_MAP_H__
8 #include <linux/config.h>
9 #include <linux/types.h>
10 #include <linux/list.h>
11 #include <linux/string.h>
13 #include <linux/mtd/compatmac.h>
15 #include <asm/unaligned.h>
16 #include <asm/system.h>
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
26 #define map_bankwidth_is_1(map) (0)
29 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
32 # define map_bankwidth(map) ((map)->bankwidth)
34 # define map_bankwidth(map) 2
35 # define map_bankwidth_is_large(map) (0)
36 # define map_words(map) (1)
38 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
39 #undef MAX_MAP_BANKWIDTH
40 #define MAX_MAP_BANKWIDTH 2
42 #define map_bankwidth_is_2(map) (0)
45 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
48 # define map_bankwidth(map) ((map)->bankwidth)
50 # define map_bankwidth(map) 4
51 # define map_bankwidth_is_large(map) (0)
52 # define map_words(map) (1)
54 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
55 #undef MAX_MAP_BANKWIDTH
56 #define MAX_MAP_BANKWIDTH 4
58 #define map_bankwidth_is_4(map) (0)
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) */
64 #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1))/ sizeof(unsigned long))
66 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
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)
74 # define map_words(map) map_calc_words(map)
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)
81 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
82 #undef MAX_MAP_BANKWIDTH
83 #define MAX_MAP_BANKWIDTH 8
85 #define map_bankwidth_is_8(map) (0)
88 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
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)
95 # define map_words(map) map_calc_words(map)
97 # define map_bankwidth(map) 16
98 # define map_bankwidth_is_large(map) (1)
99 # define map_words(map) map_calc_words(map)
101 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
102 #undef MAX_MAP_BANKWIDTH
103 #define MAX_MAP_BANKWIDTH 16
105 #define map_bankwidth_is_16(map) (0)
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)
115 # define map_words(map) map_calc_words(map)
117 # define map_bankwidth(map) 32
118 # define map_bankwidth_is_large(map) (1)
119 # define map_words(map) map_calc_words(map)
121 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
122 #undef MAX_MAP_BANKWIDTH
123 #define MAX_MAP_BANKWIDTH 32
125 #define map_bankwidth_is_32(map) (0)
128 #ifndef map_bankwidth
129 #error "No bus width supported. What's the point?"
132 static inline int map_bankwidth_supported(int w)
135 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
138 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
141 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
144 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
147 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
150 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
160 #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
163 unsigned long x[MAX_MAP_LONGS];
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
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
188 #define NO_XIP (-1UL)
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.
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);
202 void (*write)(struct map_info *, const map_word, unsigned long);
203 void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
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. */
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);
216 /* set_vpp() must handle being reentered -- enable, enable, disable
217 must leave it enabled. */
218 void (*set_vpp)(struct map_info *, int);
220 unsigned long map_priv_1;
221 unsigned long map_priv_2;
223 struct mtd_chip_driver *fldrv;
226 struct mtd_chip_driver {
227 struct mtd_info *(*probe)(struct map_info *map);
228 void (*destroy)(struct mtd_info *);
229 struct module *module;
231 struct list_head list;
234 void register_mtd_chip_driver(struct mtd_chip_driver *);
235 void unregister_mtd_chip_driver(struct mtd_chip_driver *);
237 struct mtd_info *do_map_probe(const char *name, struct map_info *map);
238 void map_destroy(struct mtd_info *mtd);
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)
243 #define INVALIDATE_CACHED_RANGE(map, from, size) \
244 do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
247 static inline int map_word_equal(struct map_info *map, map_word val1, map_word val2)
250 for (i=0; i<map_words(map); i++) {
251 if (val1.x[i] != val2.x[i])
257 static inline map_word map_word_and(struct map_info *map, map_word val1, map_word val2)
262 for (i=0; i<map_words(map); i++) {
263 r.x[i] = val1.x[i] & val2.x[i];
268 static inline map_word map_word_clr(struct map_info *map, map_word val1, map_word val2)
273 for (i=0; i<map_words(map); i++) {
274 r.x[i] = val1.x[i] & ~val2.x[i];
279 static inline map_word map_word_or(struct map_info *map, map_word val1, map_word val2)
284 for (i=0; i<map_words(map); i++) {
285 r.x[i] = val1.x[i] | val2.x[i];
290 #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
292 static inline int map_word_bitsset(struct map_info *map, map_word val1, map_word val2)
296 for (i=0; i<map_words(map); i++) {
297 if (val1.x[i] & val2.x[i])
303 static inline map_word map_word_load(struct map_info *map, const void *ptr)
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);
317 else if (map_bankwidth_is_large(map))
318 memcpy(r.x, ptr, map->bankwidth);
323 static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
327 if (map_bankwidth_is_large(map)) {
328 char *dest = (char *)&orig;
329 memcpy(dest+start, buf, len);
331 for (i=start; i < start+len; i++) {
333 #ifdef __LITTLE_ENDIAN
335 #else /* __BIG_ENDIAN */
336 bitpos = (map_bankwidth(map)-1-i)*8;
338 orig.x[0] &= ~(0xff << bitpos);
339 orig.x[0] |= buf[i-start] << bitpos;
345 #if BITS_PER_LONG < 64
346 #define MAP_FF_LIMIT 4
348 #define MAP_FF_LIMIT 8
351 static inline map_word map_word_ff(struct map_info *map)
356 if (map_bankwidth(map) < MAP_FF_LIMIT) {
357 int bw = 8 * map_bankwidth(map);
358 r.x[0] = (1 << bw) - 1;
360 for (i=0; i<map_words(map); i++)
366 static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
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);
380 else if (map_bankwidth_is_large(map))
381 memcpy_fromio(r.x, map->virt+ofs, map->bankwidth);
386 static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
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);
398 else if (map_bankwidth_is_large(map))
399 memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
403 static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
406 memcpy(to, (char *)map->cached + from, len);
408 memcpy_fromio(to, map->virt + from, len);
411 static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
413 memcpy_toio(map->virt + to, from, len);
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)
422 extern void simple_map_init(struct map_info *);
423 #define map_is_linear(map) (map->phys != NO_XIP)
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)
432 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
433 #define map_is_linear(map) ({ (void)(map); 1; })
435 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
437 #endif /* __LINUX_MTD_MAP_H__ */