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 $ */
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 <asm/unaligned.h>
12 #include <asm/system.h>
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
23 #define map_bankwidth_is_1(map) (0)
26 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
29 # define map_bankwidth(map) ((map)->bankwidth)
31 # define map_bankwidth(map) 2
32 # define map_bankwidth_is_large(map) (0)
33 # define map_words(map) (1)
35 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
36 #undef MAX_MAP_BANKWIDTH
37 #define MAX_MAP_BANKWIDTH 2
39 #define map_bankwidth_is_2(map) (0)
42 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
45 # define map_bankwidth(map) ((map)->bankwidth)
47 # define map_bankwidth(map) 4
48 # define map_bankwidth_is_large(map) (0)
49 # define map_words(map) (1)
51 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
52 #undef MAX_MAP_BANKWIDTH
53 #define MAX_MAP_BANKWIDTH 4
55 #define map_bankwidth_is_4(map) (0)
58 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
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)
66 # define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
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))
73 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
74 #undef MAX_MAP_BANKWIDTH
75 #define MAX_MAP_BANKWIDTH 8
77 #define map_bankwidth_is_8(map) (0)
80 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
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)
87 # define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
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))
93 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
94 #undef MAX_MAP_BANKWIDTH
95 #define MAX_MAP_BANKWIDTH 16
97 #define map_bankwidth_is_16(map) (0)
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)
107 # define map_words(map) (map_bankwidth(map) / sizeof(unsigned long))
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))
113 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
114 #undef MAX_MAP_BANKWIDTH
115 #define MAX_MAP_BANKWIDTH 32
117 #define map_bankwidth_is_32(map) (0)
120 #ifndef map_bankwidth
121 #error "No bus width supported. What's the point?"
124 static inline int map_bankwidth_supported(int w
)
127 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
130 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
133 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
136 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
139 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
142 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
152 #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
155 unsigned long x
[MAX_MAP_LONGS
];
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
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
180 #define NO_XIP (-1UL)
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.
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
);
194 void (*write
)(struct map_info
*, const map_word
, unsigned long);
195 void (*copy_to
)(struct map_info
*, unsigned long, const void *, ssize_t
);
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. */
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
);
208 /* set_vpp() must handle being reentered -- enable, enable, disable
209 must leave it enabled. */
210 void (*set_vpp
)(struct map_info
*, int);
212 unsigned long map_priv_1
;
213 unsigned long map_priv_2
;
215 struct mtd_chip_driver
*fldrv
;
218 struct mtd_chip_driver
{
219 struct mtd_info
*(*probe
)(struct map_info
*map
);
220 void (*destroy
)(struct mtd_info
*);
221 struct module
*module
;
223 struct list_head list
;
226 void register_mtd_chip_driver(struct mtd_chip_driver
*);
227 void unregister_mtd_chip_driver(struct mtd_chip_driver
*);
229 struct mtd_info
*do_map_probe(const char *name
, struct map_info
*map
);
230 void map_destroy(struct mtd_info
*mtd
);
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)
235 #define INVALIDATE_CACHED_RANGE(map, from, size) \
236 do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
239 static inline int map_word_equal(struct map_info
*map
, map_word val1
, map_word val2
)
242 for (i
=0; i
<map_words(map
); i
++) {
243 if (val1
.x
[i
] != val2
.x
[i
])
249 static inline map_word
map_word_and(struct map_info
*map
, map_word val1
, map_word val2
)
254 for (i
=0; i
<map_words(map
); i
++) {
255 r
.x
[i
] = val1
.x
[i
] & val2
.x
[i
];
260 static inline map_word
map_word_or(struct map_info
*map
, map_word val1
, map_word val2
)
265 for (i
=0; i
<map_words(map
); i
++) {
266 r
.x
[i
] = val1
.x
[i
] | val2
.x
[i
];
270 #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
272 static inline int map_word_bitsset(struct map_info
*map
, map_word val1
, map_word val2
)
276 for (i
=0; i
<map_words(map
); i
++) {
277 if (val1
.x
[i
] & val2
.x
[i
])
283 static inline map_word
map_word_load(struct map_info
*map
, const void *ptr
)
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
);
297 else if (map_bankwidth_is_large(map
))
298 memcpy(r
.x
, ptr
, map
->bankwidth
);
303 static inline map_word
map_word_load_partial(struct map_info
*map
, map_word orig
, const unsigned char *buf
, int start
, int len
)
307 if (map_bankwidth_is_large(map
)) {
308 char *dest
= (char *)&orig
;
309 memcpy(dest
+start
, buf
, len
);
311 for (i
=start
; i
< start
+len
; i
++) {
313 #ifdef __LITTLE_ENDIAN
315 #else /* __BIG_ENDIAN */
316 bitpos
= (map_bankwidth(map
)-1-i
)*8;
318 orig
.x
[0] &= ~(0xff << bitpos
);
319 orig
.x
[0] |= buf
[i
] << bitpos
;
325 static inline map_word
map_word_ff(struct map_info
*map
)
330 for (i
=0; i
<map_words(map
); i
++) {
335 static inline map_word
inline_map_read(struct map_info
*map
, unsigned long ofs
)
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
);
349 else if (map_bankwidth_is_large(map
))
350 memcpy_fromio(r
.x
, map
->virt
+ofs
, map
->bankwidth
);
355 static inline void inline_map_write(struct map_info
*map
, const map_word datum
, unsigned long ofs
)
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
);
367 else if (map_bankwidth_is_large(map
))
368 memcpy_toio(map
->virt
+ofs
, datum
.x
, map
->bankwidth
);
372 static inline void inline_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
375 memcpy(to
, (char *)map
->cached
+ from
, len
);
377 memcpy_fromio(to
, map
->virt
+ from
, len
);
380 static inline void inline_map_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
382 memcpy_toio(map
->virt
+ to
, from
, len
);
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)
391 extern void simple_map_init(struct map_info
*);
392 #define map_is_linear(map) (map->phys != NO_XIP)
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)
401 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
402 #define map_is_linear(map) (1)
404 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
406 #endif /* __LINUX_MTD_MAP_H__ */