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/types.h>
9 #include <linux/list.h>
10 #include <linux/string.h>
12 #include <linux/mtd/compatmac.h>
14 #include <asm/unaligned.h>
15 #include <asm/system.h>
18 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
19 #define map_bankwidth(map) 1
20 #define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
21 #define map_bankwidth_is_large(map) (0)
22 #define map_words(map) (1)
23 #define MAX_MAP_BANKWIDTH 1
25 #define map_bankwidth_is_1(map) (0)
28 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
31 # define map_bankwidth(map) ((map)->bankwidth)
33 # define map_bankwidth(map) 2
34 # define map_bankwidth_is_large(map) (0)
35 # define map_words(map) (1)
37 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
38 #undef MAX_MAP_BANKWIDTH
39 #define MAX_MAP_BANKWIDTH 2
41 #define map_bankwidth_is_2(map) (0)
44 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
47 # define map_bankwidth(map) ((map)->bankwidth)
49 # define map_bankwidth(map) 4
50 # define map_bankwidth_is_large(map) (0)
51 # define map_words(map) (1)
53 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
54 #undef MAX_MAP_BANKWIDTH
55 #define MAX_MAP_BANKWIDTH 4
57 #define map_bankwidth_is_4(map) (0)
60 /* ensure we never evaluate anything shorted than an unsigned long
61 * to zero, and ensure we'll never miss the end of an comparison (bjd) */
63 #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1))/ sizeof(unsigned long))
65 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
68 # define map_bankwidth(map) ((map)->bankwidth)
69 # if BITS_PER_LONG < 64
70 # undef map_bankwidth_is_large
71 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
73 # define map_words(map) map_calc_words(map)
76 # define map_bankwidth(map) 8
77 # define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
78 # define map_words(map) map_calc_words(map)
80 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
81 #undef MAX_MAP_BANKWIDTH
82 #define MAX_MAP_BANKWIDTH 8
84 #define map_bankwidth_is_8(map) (0)
87 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
90 # define map_bankwidth(map) ((map)->bankwidth)
91 # undef map_bankwidth_is_large
92 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
94 # define map_words(map) map_calc_words(map)
96 # define map_bankwidth(map) 16
97 # define map_bankwidth_is_large(map) (1)
98 # define map_words(map) map_calc_words(map)
100 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
101 #undef MAX_MAP_BANKWIDTH
102 #define MAX_MAP_BANKWIDTH 16
104 #define map_bankwidth_is_16(map) (0)
107 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
108 # ifdef map_bankwidth
109 # undef map_bankwidth
110 # define map_bankwidth(map) ((map)->bankwidth)
111 # undef map_bankwidth_is_large
112 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
114 # define map_words(map) map_calc_words(map)
116 # define map_bankwidth(map) 32
117 # define map_bankwidth_is_large(map) (1)
118 # define map_words(map) map_calc_words(map)
120 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
121 #undef MAX_MAP_BANKWIDTH
122 #define MAX_MAP_BANKWIDTH 32
124 #define map_bankwidth_is_32(map) (0)
127 #ifndef map_bankwidth
128 #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
129 static inline int map_bankwidth(void *map
)
134 #define map_bankwidth_is_large(map) (0)
135 #define map_words(map) (0)
136 #define MAX_MAP_BANKWIDTH 1
139 static inline int map_bankwidth_supported(int w
)
142 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
145 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
148 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
151 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
154 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
157 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
167 #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
170 unsigned long x
[MAX_MAP_LONGS
];
173 /* The map stuff is very simple. You fill in your struct map_info with
174 a handful of routines for accessing the device, making sure they handle
175 paging etc. correctly if your device needs it. Then you pass it off
176 to a chip probe routine -- either JEDEC or CFI probe or both -- via
177 do_map_probe(). If a chip is recognised, the probe code will invoke the
178 appropriate chip driver (if present) and return a struct mtd_info.
179 At which point, you fill in the mtd->module with your own module
180 address, and register it with the MTD core code. Or you could partition
181 it and register the partitions instead, or keep it for your own private
184 The mtd->priv field will point to the struct map_info, and any further
185 private data required by the chip driver is linked from the
186 mtd->priv->fldrv_priv field. This allows the map driver to get at
187 the destructor function map->fldrv_destroy() when it's tired
194 resource_size_t phys
;
195 #define NO_XIP (-1UL)
200 int bankwidth
; /* in octets. This isn't necessarily the width
201 of actual bus cycles -- it's the repeat interval
202 in bytes, before you are talking to the first chip again.
205 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
206 map_word (*read
)(struct map_info
*, unsigned long);
207 void (*copy_from
)(struct map_info
*, void *, unsigned long, ssize_t
);
209 void (*write
)(struct map_info
*, const map_word
, unsigned long);
210 void (*copy_to
)(struct map_info
*, unsigned long, const void *, ssize_t
);
212 /* We can perhaps put in 'point' and 'unpoint' methods, if we really
213 want to enable XIP for non-linear mappings. Not yet though. */
215 /* It's possible for the map driver to use cached memory in its
216 copy_from implementation (and _only_ with copy_from). However,
217 when the chip driver knows some flash area has changed contents,
218 it will signal it to the map driver through this routine to let
219 the map driver invalidate the corresponding cache as needed.
220 If there is no cache to care about this can be set to NULL. */
221 void (*inval_cache
)(struct map_info
*, unsigned long, ssize_t
);
223 /* set_vpp() must handle being reentered -- enable, enable, disable
224 must leave it enabled. */
225 void (*set_vpp
)(struct map_info
*, int);
227 unsigned long map_priv_1
;
228 unsigned long map_priv_2
;
230 struct mtd_chip_driver
*fldrv
;
233 struct mtd_chip_driver
{
234 struct mtd_info
*(*probe
)(struct map_info
*map
);
235 void (*destroy
)(struct mtd_info
*);
236 struct module
*module
;
238 struct list_head list
;
241 void register_mtd_chip_driver(struct mtd_chip_driver
*);
242 void unregister_mtd_chip_driver(struct mtd_chip_driver
*);
244 struct mtd_info
*do_map_probe(const char *name
, struct map_info
*map
);
245 void map_destroy(struct mtd_info
*mtd
);
247 #define ENABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 1); } while(0)
248 #define DISABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 0); } while(0)
250 #define INVALIDATE_CACHED_RANGE(map, from, size) \
251 do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
254 static inline int map_word_equal(struct map_info
*map
, map_word val1
, map_word val2
)
257 for (i
=0; i
<map_words(map
); i
++) {
258 if (val1
.x
[i
] != val2
.x
[i
])
264 static inline map_word
map_word_and(struct map_info
*map
, map_word val1
, map_word val2
)
269 for (i
=0; i
<map_words(map
); i
++) {
270 r
.x
[i
] = val1
.x
[i
] & val2
.x
[i
];
275 static inline map_word
map_word_clr(struct map_info
*map
, map_word val1
, map_word val2
)
280 for (i
=0; i
<map_words(map
); i
++) {
281 r
.x
[i
] = val1
.x
[i
] & ~val2
.x
[i
];
286 static inline map_word
map_word_or(struct map_info
*map
, map_word val1
, map_word val2
)
291 for (i
=0; i
<map_words(map
); i
++) {
292 r
.x
[i
] = val1
.x
[i
] | val2
.x
[i
];
297 #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
299 static inline int map_word_bitsset(struct map_info
*map
, map_word val1
, map_word val2
)
303 for (i
=0; i
<map_words(map
); i
++) {
304 if (val1
.x
[i
] & val2
.x
[i
])
310 static inline map_word
map_word_load(struct map_info
*map
, const void *ptr
)
314 if (map_bankwidth_is_1(map
))
315 r
.x
[0] = *(unsigned char *)ptr
;
316 else if (map_bankwidth_is_2(map
))
317 r
.x
[0] = get_unaligned((uint16_t *)ptr
);
318 else if (map_bankwidth_is_4(map
))
319 r
.x
[0] = get_unaligned((uint32_t *)ptr
);
320 #if BITS_PER_LONG >= 64
321 else if (map_bankwidth_is_8(map
))
322 r
.x
[0] = get_unaligned((uint64_t *)ptr
);
324 else if (map_bankwidth_is_large(map
))
325 memcpy(r
.x
, ptr
, map
->bankwidth
);
330 static inline map_word
map_word_load_partial(struct map_info
*map
, map_word orig
, const unsigned char *buf
, int start
, int len
)
334 if (map_bankwidth_is_large(map
)) {
335 char *dest
= (char *)&orig
;
336 memcpy(dest
+start
, buf
, len
);
338 for (i
=start
; i
< start
+len
; i
++) {
340 #ifdef __LITTLE_ENDIAN
342 #else /* __BIG_ENDIAN */
343 bitpos
= (map_bankwidth(map
)-1-i
)*8;
345 orig
.x
[0] &= ~(0xff << bitpos
);
346 orig
.x
[0] |= buf
[i
-start
] << bitpos
;
352 #if BITS_PER_LONG < 64
353 #define MAP_FF_LIMIT 4
355 #define MAP_FF_LIMIT 8
358 static inline map_word
map_word_ff(struct map_info
*map
)
363 if (map_bankwidth(map
) < MAP_FF_LIMIT
) {
364 int bw
= 8 * map_bankwidth(map
);
365 r
.x
[0] = (1 << bw
) - 1;
367 for (i
=0; i
<map_words(map
); i
++)
373 static inline map_word
inline_map_read(struct map_info
*map
, unsigned long ofs
)
377 if (map_bankwidth_is_1(map
))
378 r
.x
[0] = __raw_readb(map
->virt
+ ofs
);
379 else if (map_bankwidth_is_2(map
))
380 r
.x
[0] = __raw_readw(map
->virt
+ ofs
);
381 else if (map_bankwidth_is_4(map
))
382 r
.x
[0] = __raw_readl(map
->virt
+ ofs
);
383 #if BITS_PER_LONG >= 64
384 else if (map_bankwidth_is_8(map
))
385 r
.x
[0] = __raw_readq(map
->virt
+ ofs
);
387 else if (map_bankwidth_is_large(map
))
388 memcpy_fromio(r
.x
, map
->virt
+ofs
, map
->bankwidth
);
393 static inline void inline_map_write(struct map_info
*map
, const map_word datum
, unsigned long ofs
)
395 if (map_bankwidth_is_1(map
))
396 __raw_writeb(datum
.x
[0], map
->virt
+ ofs
);
397 else if (map_bankwidth_is_2(map
))
398 __raw_writew(datum
.x
[0], map
->virt
+ ofs
);
399 else if (map_bankwidth_is_4(map
))
400 __raw_writel(datum
.x
[0], map
->virt
+ ofs
);
401 #if BITS_PER_LONG >= 64
402 else if (map_bankwidth_is_8(map
))
403 __raw_writeq(datum
.x
[0], map
->virt
+ ofs
);
405 else if (map_bankwidth_is_large(map
))
406 memcpy_toio(map
->virt
+ofs
, datum
.x
, map
->bankwidth
);
410 static inline void inline_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
413 memcpy(to
, (char *)map
->cached
+ from
, len
);
415 memcpy_fromio(to
, map
->virt
+ from
, len
);
418 static inline void inline_map_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
420 memcpy_toio(map
->virt
+ to
, from
, len
);
423 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
424 #define map_read(map, ofs) (map)->read(map, ofs)
425 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
426 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
427 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
429 extern void simple_map_init(struct map_info
*);
430 #define map_is_linear(map) (map->phys != NO_XIP)
433 #define map_read(map, ofs) inline_map_read(map, ofs)
434 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
435 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
436 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
439 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
440 #define map_is_linear(map) ({ (void)(map); 1; })
442 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
444 #endif /* __LINUX_MTD_MAP_H__ */