2 /* Overhauled routines for dealing with different mmap regions of flash */
3 /* $Id: map.h,v 1.52 2005/05/25 10:29:41 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/mtd/compatmac.h>
12 #include <asm/unaligned.h>
13 #include <asm/system.h>
17 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
18 #define map_bankwidth(map) 1
19 #define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
20 #define map_bankwidth_is_large(map) (0)
21 #define map_words(map) (1)
22 #define MAX_MAP_BANKWIDTH 1
24 #define map_bankwidth_is_1(map) (0)
27 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
30 # define map_bankwidth(map) ((map)->bankwidth)
32 # define map_bankwidth(map) 2
33 # define map_bankwidth_is_large(map) (0)
34 # define map_words(map) (1)
36 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
37 #undef MAX_MAP_BANKWIDTH
38 #define MAX_MAP_BANKWIDTH 2
40 #define map_bankwidth_is_2(map) (0)
43 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
46 # define map_bankwidth(map) ((map)->bankwidth)
48 # define map_bankwidth(map) 4
49 # define map_bankwidth_is_large(map) (0)
50 # define map_words(map) (1)
52 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
53 #undef MAX_MAP_BANKWIDTH
54 #define MAX_MAP_BANKWIDTH 4
56 #define map_bankwidth_is_4(map) (0)
59 /* ensure we never evaluate anything shorted than an unsigned long
60 * to zero, and ensure we'll never miss the end of an comparison (bjd) */
62 #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1))/ sizeof(unsigned long))
64 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
67 # define map_bankwidth(map) ((map)->bankwidth)
68 # if BITS_PER_LONG < 64
69 # undef map_bankwidth_is_large
70 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
72 # define map_words(map) map_calc_words(map)
75 # define map_bankwidth(map) 8
76 # define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
77 # define map_words(map) map_calc_words(map)
79 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
80 #undef MAX_MAP_BANKWIDTH
81 #define MAX_MAP_BANKWIDTH 8
83 #define map_bankwidth_is_8(map) (0)
86 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
89 # define map_bankwidth(map) ((map)->bankwidth)
90 # undef map_bankwidth_is_large
91 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
93 # define map_words(map) map_calc_words(map)
95 # define map_bankwidth(map) 16
96 # define map_bankwidth_is_large(map) (1)
97 # define map_words(map) map_calc_words(map)
99 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
100 #undef MAX_MAP_BANKWIDTH
101 #define MAX_MAP_BANKWIDTH 16
103 #define map_bankwidth_is_16(map) (0)
106 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
107 # ifdef map_bankwidth
108 # undef map_bankwidth
109 # define map_bankwidth(map) ((map)->bankwidth)
110 # undef map_bankwidth_is_large
111 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
113 # define map_words(map) map_calc_words(map)
115 # define map_bankwidth(map) 32
116 # define map_bankwidth_is_large(map) (1)
117 # define map_words(map) map_calc_words(map)
119 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
120 #undef MAX_MAP_BANKWIDTH
121 #define MAX_MAP_BANKWIDTH 32
123 #define map_bankwidth_is_32(map) (0)
126 #ifndef map_bankwidth
127 #error "No bus width supported. What's the point?"
130 static inline int map_bankwidth_supported(int w
)
133 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
136 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
139 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
142 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
145 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
148 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
158 #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
161 unsigned long x
[MAX_MAP_LONGS
];
164 /* The map stuff is very simple. You fill in your struct map_info with
165 a handful of routines for accessing the device, making sure they handle
166 paging etc. correctly if your device needs it. Then you pass it off
167 to a chip probe routine -- either JEDEC or CFI probe or both -- via
168 do_map_probe(). If a chip is recognised, the probe code will invoke the
169 appropriate chip driver (if present) and return a struct mtd_info.
170 At which point, you fill in the mtd->module with your own module
171 address, and register it with the MTD core code. Or you could partition
172 it and register the partitions instead, or keep it for your own private
175 The mtd->priv field will point to the struct map_info, and any further
176 private data required by the chip driver is linked from the
177 mtd->priv->fldrv_priv field. This allows the map driver to get at
178 the destructor function map->fldrv_destroy() when it's tired
186 #define NO_XIP (-1UL)
191 int bankwidth
; /* in octets. This isn't necessarily the width
192 of actual bus cycles -- it's the repeat interval
193 in bytes, before you are talking to the first chip again.
196 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
197 map_word (*read
)(struct map_info
*, unsigned long);
198 void (*copy_from
)(struct map_info
*, void *, unsigned long, ssize_t
);
200 void (*write
)(struct map_info
*, const map_word
, unsigned long);
201 void (*copy_to
)(struct map_info
*, unsigned long, const void *, ssize_t
);
203 /* We can perhaps put in 'point' and 'unpoint' methods, if we really
204 want to enable XIP for non-linear mappings. Not yet though. */
206 /* It's possible for the map driver to use cached memory in its
207 copy_from implementation (and _only_ with copy_from). However,
208 when the chip driver knows some flash area has changed contents,
209 it will signal it to the map driver through this routine to let
210 the map driver invalidate the corresponding cache as needed.
211 If there is no cache to care about this can be set to NULL. */
212 void (*inval_cache
)(struct map_info
*, unsigned long, ssize_t
);
214 /* set_vpp() must handle being reentered -- enable, enable, disable
215 must leave it enabled. */
216 void (*set_vpp
)(struct map_info
*, int);
218 unsigned long map_priv_1
;
219 unsigned long map_priv_2
;
221 struct mtd_chip_driver
*fldrv
;
224 struct mtd_chip_driver
{
225 struct mtd_info
*(*probe
)(struct map_info
*map
);
226 void (*destroy
)(struct mtd_info
*);
227 struct module
*module
;
229 struct list_head list
;
232 void register_mtd_chip_driver(struct mtd_chip_driver
*);
233 void unregister_mtd_chip_driver(struct mtd_chip_driver
*);
235 struct mtd_info
*do_map_probe(const char *name
, struct map_info
*map
);
236 void map_destroy(struct mtd_info
*mtd
);
238 #define ENABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 1); } while(0)
239 #define DISABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 0); } while(0)
241 #define INVALIDATE_CACHED_RANGE(map, from, size) \
242 do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
245 static inline int map_word_equal(struct map_info
*map
, map_word val1
, map_word val2
)
248 for (i
=0; i
<map_words(map
); i
++) {
249 if (val1
.x
[i
] != val2
.x
[i
])
255 static inline map_word
map_word_and(struct map_info
*map
, map_word val1
, map_word val2
)
260 for (i
=0; i
<map_words(map
); i
++) {
261 r
.x
[i
] = val1
.x
[i
] & val2
.x
[i
];
266 static inline map_word
map_word_clr(struct map_info
*map
, map_word val1
, map_word val2
)
271 for (i
=0; i
<map_words(map
); i
++) {
272 r
.x
[i
] = val1
.x
[i
] & ~val2
.x
[i
];
277 static inline map_word
map_word_or(struct map_info
*map
, map_word val1
, map_word val2
)
282 for (i
=0; i
<map_words(map
); i
++) {
283 r
.x
[i
] = val1
.x
[i
] | val2
.x
[i
];
288 #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
290 static inline int map_word_bitsset(struct map_info
*map
, map_word val1
, map_word val2
)
294 for (i
=0; i
<map_words(map
); i
++) {
295 if (val1
.x
[i
] & val2
.x
[i
])
301 static inline map_word
map_word_load(struct map_info
*map
, const void *ptr
)
305 if (map_bankwidth_is_1(map
))
306 r
.x
[0] = *(unsigned char *)ptr
;
307 else if (map_bankwidth_is_2(map
))
308 r
.x
[0] = get_unaligned((uint16_t *)ptr
);
309 else if (map_bankwidth_is_4(map
))
310 r
.x
[0] = get_unaligned((uint32_t *)ptr
);
311 #if BITS_PER_LONG >= 64
312 else if (map_bankwidth_is_8(map
))
313 r
.x
[0] = get_unaligned((uint64_t *)ptr
);
315 else if (map_bankwidth_is_large(map
))
316 memcpy(r
.x
, ptr
, map
->bankwidth
);
321 static inline map_word
map_word_load_partial(struct map_info
*map
, map_word orig
, const unsigned char *buf
, int start
, int len
)
325 if (map_bankwidth_is_large(map
)) {
326 char *dest
= (char *)&orig
;
327 memcpy(dest
+start
, buf
, len
);
329 for (i
=start
; i
< start
+len
; i
++) {
331 #ifdef __LITTLE_ENDIAN
333 #else /* __BIG_ENDIAN */
334 bitpos
= (map_bankwidth(map
)-1-i
)*8;
336 orig
.x
[0] &= ~(0xff << bitpos
);
337 orig
.x
[0] |= buf
[i
-start
] << bitpos
;
343 #if BITS_PER_LONG < 64
344 #define MAP_FF_LIMIT 4
346 #define MAP_FF_LIMIT 8
349 static inline map_word
map_word_ff(struct map_info
*map
)
354 if (map_bankwidth(map
) < MAP_FF_LIMIT
) {
355 int bw
= 8 * map_bankwidth(map
);
356 r
.x
[0] = (1 << bw
) - 1;
358 for (i
=0; i
<map_words(map
); i
++)
364 static inline map_word
inline_map_read(struct map_info
*map
, unsigned long ofs
)
368 if (map_bankwidth_is_1(map
))
369 r
.x
[0] = __raw_readb(map
->virt
+ ofs
);
370 else if (map_bankwidth_is_2(map
))
371 r
.x
[0] = __raw_readw(map
->virt
+ ofs
);
372 else if (map_bankwidth_is_4(map
))
373 r
.x
[0] = __raw_readl(map
->virt
+ ofs
);
374 #if BITS_PER_LONG >= 64
375 else if (map_bankwidth_is_8(map
))
376 r
.x
[0] = __raw_readq(map
->virt
+ ofs
);
378 else if (map_bankwidth_is_large(map
))
379 memcpy_fromio(r
.x
, map
->virt
+ofs
, map
->bankwidth
);
384 static inline void inline_map_write(struct map_info
*map
, const map_word datum
, unsigned long ofs
)
386 if (map_bankwidth_is_1(map
))
387 __raw_writeb(datum
.x
[0], map
->virt
+ ofs
);
388 else if (map_bankwidth_is_2(map
))
389 __raw_writew(datum
.x
[0], map
->virt
+ ofs
);
390 else if (map_bankwidth_is_4(map
))
391 __raw_writel(datum
.x
[0], map
->virt
+ ofs
);
392 #if BITS_PER_LONG >= 64
393 else if (map_bankwidth_is_8(map
))
394 __raw_writeq(datum
.x
[0], map
->virt
+ ofs
);
396 else if (map_bankwidth_is_large(map
))
397 memcpy_toio(map
->virt
+ofs
, datum
.x
, map
->bankwidth
);
401 static inline void inline_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
404 memcpy(to
, (char *)map
->cached
+ from
, len
);
406 memcpy_fromio(to
, map
->virt
+ from
, len
);
409 static inline void inline_map_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
411 memcpy_toio(map
->virt
+ to
, from
, len
);
414 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
415 #define map_read(map, ofs) (map)->read(map, ofs)
416 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
417 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
418 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
420 extern void simple_map_init(struct map_info
*);
421 #define map_is_linear(map) (map->phys != NO_XIP)
424 #define map_read(map, ofs) inline_map_read(map, ofs)
425 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
426 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
427 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
430 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
431 #define map_is_linear(map) ({ (void)(map); 1; })
433 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
435 #endif /* __LINUX_MTD_MAP_H__ */