2 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> et al.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 /* Overhauled routines for dealing with different mmap regions of flash */
22 #ifndef __LINUX_MTD_MAP_H__
23 #define __LINUX_MTD_MAP_H__
25 #include <linux/types.h>
26 #include <linux/list.h>
27 #include <linux/string.h>
28 #include <linux/bug.h>
29 #include <linux/kernel.h>
32 #include <asm/unaligned.h>
33 #include <asm/barrier.h>
35 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
36 #define map_bankwidth(map) 1
37 #define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
38 #define map_bankwidth_is_large(map) (0)
39 #define map_words(map) (1)
40 #define MAX_MAP_BANKWIDTH 1
42 #define map_bankwidth_is_1(map) (0)
45 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
48 # define map_bankwidth(map) ((map)->bankwidth)
50 # define map_bankwidth(map) 2
51 # define map_bankwidth_is_large(map) (0)
52 # define map_words(map) (1)
54 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
55 #undef MAX_MAP_BANKWIDTH
56 #define MAX_MAP_BANKWIDTH 2
58 #define map_bankwidth_is_2(map) (0)
61 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
64 # define map_bankwidth(map) ((map)->bankwidth)
66 # define map_bankwidth(map) 4
67 # define map_bankwidth_is_large(map) (0)
68 # define map_words(map) (1)
70 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
71 #undef MAX_MAP_BANKWIDTH
72 #define MAX_MAP_BANKWIDTH 4
74 #define map_bankwidth_is_4(map) (0)
77 /* ensure we never evaluate anything shorted than an unsigned long
78 * to zero, and ensure we'll never miss the end of an comparison (bjd) */
80 #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1)) / sizeof(unsigned long))
82 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
85 # define map_bankwidth(map) ((map)->bankwidth)
86 # if BITS_PER_LONG < 64
87 # undef map_bankwidth_is_large
88 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
90 # define map_words(map) map_calc_words(map)
93 # define map_bankwidth(map) 8
94 # define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
95 # define map_words(map) map_calc_words(map)
97 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
98 #undef MAX_MAP_BANKWIDTH
99 #define MAX_MAP_BANKWIDTH 8
101 #define map_bankwidth_is_8(map) (0)
104 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
105 # ifdef map_bankwidth
106 # undef map_bankwidth
107 # define map_bankwidth(map) ((map)->bankwidth)
108 # undef map_bankwidth_is_large
109 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
111 # define map_words(map) map_calc_words(map)
113 # define map_bankwidth(map) 16
114 # define map_bankwidth_is_large(map) (1)
115 # define map_words(map) map_calc_words(map)
117 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
118 #undef MAX_MAP_BANKWIDTH
119 #define MAX_MAP_BANKWIDTH 16
121 #define map_bankwidth_is_16(map) (0)
124 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
125 # ifdef map_bankwidth
126 # undef map_bankwidth
127 # define map_bankwidth(map) ((map)->bankwidth)
128 # undef map_bankwidth_is_large
129 # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
131 # define map_words(map) map_calc_words(map)
133 # define map_bankwidth(map) 32
134 # define map_bankwidth_is_large(map) (1)
135 # define map_words(map) map_calc_words(map)
137 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
138 #undef MAX_MAP_BANKWIDTH
139 #define MAX_MAP_BANKWIDTH 32
141 #define map_bankwidth_is_32(map) (0)
144 #ifndef map_bankwidth
146 #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
148 static inline int map_bankwidth(void *map
)
153 #define map_bankwidth_is_large(map) (0)
154 #define map_words(map) (0)
155 #define MAX_MAP_BANKWIDTH 1
158 static inline int map_bankwidth_supported(int w
)
161 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
164 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
167 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
170 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
173 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
176 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
186 #define MAX_MAP_LONGS (((MAX_MAP_BANKWIDTH * 8) + BITS_PER_LONG - 1) / BITS_PER_LONG)
189 unsigned long x
[MAX_MAP_LONGS
];
192 /* The map stuff is very simple. You fill in your struct map_info with
193 a handful of routines for accessing the device, making sure they handle
194 paging etc. correctly if your device needs it. Then you pass it off
195 to a chip probe routine -- either JEDEC or CFI probe or both -- via
196 do_map_probe(). If a chip is recognised, the probe code will invoke the
197 appropriate chip driver (if present) and return a struct mtd_info.
198 At which point, you fill in the mtd->module with your own module
199 address, and register it with the MTD core code. Or you could partition
200 it and register the partitions instead, or keep it for your own private
203 The mtd->priv field will point to the struct map_info, and any further
204 private data required by the chip driver is linked from the
205 mtd->priv->fldrv_priv field. This allows the map driver to get at
206 the destructor function map->fldrv_destroy() when it's tired
213 resource_size_t phys
;
214 #define NO_XIP (-1UL)
219 int swap
; /* this mapping's byte-swapping requirement */
220 int bankwidth
; /* in octets. This isn't necessarily the width
221 of actual bus cycles -- it's the repeat interval
222 in bytes, before you are talking to the first chip again.
225 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
226 map_word (*read
)(struct map_info
*, unsigned long);
227 void (*copy_from
)(struct map_info
*, void *, unsigned long, ssize_t
);
229 void (*write
)(struct map_info
*, const map_word
, unsigned long);
230 void (*copy_to
)(struct map_info
*, unsigned long, const void *, ssize_t
);
232 /* We can perhaps put in 'point' and 'unpoint' methods, if we really
233 want to enable XIP for non-linear mappings. Not yet though. */
235 /* It's possible for the map driver to use cached memory in its
236 copy_from implementation (and _only_ with copy_from). However,
237 when the chip driver knows some flash area has changed contents,
238 it will signal it to the map driver through this routine to let
239 the map driver invalidate the corresponding cache as needed.
240 If there is no cache to care about this can be set to NULL. */
241 void (*inval_cache
)(struct map_info
*, unsigned long, ssize_t
);
243 /* This will be called with 1 as parameter when the first map user
244 * needs VPP, and called with 0 when the last user exits. The map
245 * core maintains a reference counter, and assumes that VPP is a
246 * global resource applying to all mapped flash chips on the system.
248 void (*set_vpp
)(struct map_info
*, int);
250 unsigned long pfow_base
;
251 unsigned long map_priv_1
;
252 unsigned long map_priv_2
;
253 struct device_node
*device_node
;
255 struct mtd_chip_driver
*fldrv
;
258 struct mtd_chip_driver
{
259 struct mtd_info
*(*probe
)(struct map_info
*map
);
260 void (*destroy
)(struct mtd_info
*);
261 struct module
*module
;
263 struct list_head list
;
266 void register_mtd_chip_driver(struct mtd_chip_driver
*);
267 void unregister_mtd_chip_driver(struct mtd_chip_driver
*);
269 struct mtd_info
*do_map_probe(const char *name
, struct map_info
*map
);
270 void map_destroy(struct mtd_info
*mtd
);
272 #define ENABLE_VPP(map) do { if (map->set_vpp) map->set_vpp(map, 1); } while (0)
273 #define DISABLE_VPP(map) do { if (map->set_vpp) map->set_vpp(map, 0); } while (0)
275 #define INVALIDATE_CACHED_RANGE(map, from, size) \
276 do { if (map->inval_cache) map->inval_cache(map, from, size); } while (0)
279 static inline int map_word_equal(struct map_info
*map
, map_word val1
, map_word val2
)
283 for (i
= 0; i
< map_words(map
); i
++) {
284 if (val1
.x
[i
] != val2
.x
[i
])
291 static inline map_word
map_word_and(struct map_info
*map
, map_word val1
, map_word val2
)
296 for (i
= 0; i
< map_words(map
); i
++)
297 r
.x
[i
] = val1
.x
[i
] & val2
.x
[i
];
302 static inline map_word
map_word_clr(struct map_info
*map
, map_word val1
, map_word val2
)
307 for (i
= 0; i
< map_words(map
); i
++)
308 r
.x
[i
] = val1
.x
[i
] & ~val2
.x
[i
];
313 static inline map_word
map_word_or(struct map_info
*map
, map_word val1
, map_word val2
)
318 for (i
= 0; i
< map_words(map
); i
++)
319 r
.x
[i
] = val1
.x
[i
] | val2
.x
[i
];
324 static inline int map_word_andequal(struct map_info
*map
, map_word val1
, map_word val2
, map_word val3
)
328 for (i
= 0; i
< map_words(map
); i
++) {
329 if ((val1
.x
[i
] & val2
.x
[i
]) != val3
.x
[i
])
336 static inline int map_word_bitsset(struct map_info
*map
, map_word val1
, map_word val2
)
340 for (i
= 0; i
< map_words(map
); i
++) {
341 if (val1
.x
[i
] & val2
.x
[i
])
348 static inline map_word
map_word_load(struct map_info
*map
, const void *ptr
)
352 if (map_bankwidth_is_1(map
))
353 r
.x
[0] = *(unsigned char *)ptr
;
354 else if (map_bankwidth_is_2(map
))
355 r
.x
[0] = get_unaligned((uint16_t *)ptr
);
356 else if (map_bankwidth_is_4(map
))
357 r
.x
[0] = get_unaligned((uint32_t *)ptr
);
358 #if BITS_PER_LONG >= 64
359 else if (map_bankwidth_is_8(map
))
360 r
.x
[0] = get_unaligned((uint64_t *)ptr
);
362 else if (map_bankwidth_is_large(map
))
363 memcpy(r
.x
, ptr
, map
->bankwidth
);
370 static inline map_word
map_word_load_partial(struct map_info
*map
, map_word orig
, const unsigned char *buf
, int start
, int len
)
374 if (map_bankwidth_is_large(map
)) {
375 char *dest
= (char *)&orig
;
377 memcpy(dest
+start
, buf
, len
);
379 for (i
= start
; i
< start
+len
; i
++) {
382 #ifdef __LITTLE_ENDIAN
384 #else /* __BIG_ENDIAN */
385 bitpos
= (map_bankwidth(map
) - 1 - i
) * 8;
387 orig
.x
[0] &= ~(0xff << bitpos
);
388 orig
.x
[0] |= (unsigned long)buf
[i
-start
] << bitpos
;
394 #if BITS_PER_LONG < 64
395 #define MAP_FF_LIMIT 4
397 #define MAP_FF_LIMIT 8
400 static inline map_word
map_word_ff(struct map_info
*map
)
405 if (map_bankwidth(map
) < MAP_FF_LIMIT
) {
406 int bw
= 8 * map_bankwidth(map
);
408 r
.x
[0] = (1UL << bw
) - 1;
410 for (i
= 0; i
< map_words(map
); i
++)
416 static inline map_word
inline_map_read(struct map_info
*map
, unsigned long ofs
)
420 if (map_bankwidth_is_1(map
))
421 r
.x
[0] = __raw_readb(map
->virt
+ ofs
);
422 else if (map_bankwidth_is_2(map
))
423 r
.x
[0] = __raw_readw(map
->virt
+ ofs
);
424 else if (map_bankwidth_is_4(map
))
425 r
.x
[0] = __raw_readl(map
->virt
+ ofs
);
426 #if BITS_PER_LONG >= 64
427 else if (map_bankwidth_is_8(map
))
428 r
.x
[0] = __raw_readq(map
->virt
+ ofs
);
430 else if (map_bankwidth_is_large(map
))
431 memcpy_fromio(r
.x
, map
->virt
+ ofs
, map
->bankwidth
);
438 static inline void inline_map_write(struct map_info
*map
, const map_word datum
, unsigned long ofs
)
440 if (map_bankwidth_is_1(map
))
441 __raw_writeb(datum
.x
[0], map
->virt
+ ofs
);
442 else if (map_bankwidth_is_2(map
))
443 __raw_writew(datum
.x
[0], map
->virt
+ ofs
);
444 else if (map_bankwidth_is_4(map
))
445 __raw_writel(datum
.x
[0], map
->virt
+ ofs
);
446 #if BITS_PER_LONG >= 64
447 else if (map_bankwidth_is_8(map
))
448 __raw_writeq(datum
.x
[0], map
->virt
+ ofs
);
450 else if (map_bankwidth_is_large(map
))
451 memcpy_toio(map
->virt
+ofs
, datum
.x
, map
->bankwidth
);
457 static inline void inline_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
460 memcpy(to
, (char *)map
->cached
+ from
, len
);
462 memcpy_fromio(to
, map
->virt
+ from
, len
);
465 static inline void inline_map_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
467 memcpy_toio(map
->virt
+ to
, from
, len
);
470 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
471 #define map_read(map, ofs) (map)->read(map, ofs)
472 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
473 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
474 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
476 extern void simple_map_init(struct map_info
*);
477 #define map_is_linear(map) (map->phys != NO_XIP)
480 #define map_read(map, ofs) inline_map_read(map, ofs)
481 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
482 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
483 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
486 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
487 #define map_is_linear(map) ({ (void)(map); 1; })
489 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
491 #endif /* __LINUX_MTD_MAP_H__ */
This page took 0.048612 seconds and 5 git commands to generate.