Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / include / linux / mtd / map.h
blobb5b43f94f311626ee364157515c4342aba976e6f
1 /*
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>
30 #include <linux/io.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
41 #else
42 #define map_bankwidth_is_1(map) (0)
43 #endif
45 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
46 # ifdef map_bankwidth
47 # undef map_bankwidth
48 # define map_bankwidth(map) ((map)->bankwidth)
49 # else
50 # define map_bankwidth(map) 2
51 # define map_bankwidth_is_large(map) (0)
52 # define map_words(map) (1)
53 # endif
54 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
55 #undef MAX_MAP_BANKWIDTH
56 #define MAX_MAP_BANKWIDTH 2
57 #else
58 #define map_bankwidth_is_2(map) (0)
59 #endif
61 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
62 # ifdef map_bankwidth
63 # undef map_bankwidth
64 # define map_bankwidth(map) ((map)->bankwidth)
65 # else
66 # define map_bankwidth(map) 4
67 # define map_bankwidth_is_large(map) (0)
68 # define map_words(map) (1)
69 # endif
70 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
71 #undef MAX_MAP_BANKWIDTH
72 #define MAX_MAP_BANKWIDTH 4
73 #else
74 #define map_bankwidth_is_4(map) (0)
75 #endif
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
83 # ifdef map_bankwidth
84 # undef map_bankwidth
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)
89 # undef map_words
90 # define map_words(map) map_calc_words(map)
91 # endif
92 # else
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)
96 # endif
97 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
98 #undef MAX_MAP_BANKWIDTH
99 #define MAX_MAP_BANKWIDTH 8
100 #else
101 #define map_bankwidth_is_8(map) (0)
102 #endif
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)
110 # undef map_words
111 # define map_words(map) map_calc_words(map)
112 # else
113 # define map_bankwidth(map) 16
114 # define map_bankwidth_is_large(map) (1)
115 # define map_words(map) map_calc_words(map)
116 # endif
117 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
118 #undef MAX_MAP_BANKWIDTH
119 #define MAX_MAP_BANKWIDTH 16
120 #else
121 #define map_bankwidth_is_16(map) (0)
122 #endif
124 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
125 /* always use indirect access for 256-bit to preserve kernel stack */
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)
130 # undef map_words
131 # define map_words(map) map_calc_words(map)
132 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
133 #undef MAX_MAP_BANKWIDTH
134 #define MAX_MAP_BANKWIDTH 32
135 #else
136 #define map_bankwidth_is_32(map) (0)
137 #endif
139 #ifndef map_bankwidth
140 #ifdef CONFIG_MTD
141 #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
142 #endif
143 static inline int map_bankwidth(void *map)
145 BUG();
146 return 0;
148 #define map_bankwidth_is_large(map) (0)
149 #define map_words(map) (0)
150 #define MAX_MAP_BANKWIDTH 1
151 #endif
153 static inline int map_bankwidth_supported(int w)
155 switch (w) {
156 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
157 case 1:
158 #endif
159 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
160 case 2:
161 #endif
162 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
163 case 4:
164 #endif
165 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
166 case 8:
167 #endif
168 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
169 case 16:
170 #endif
171 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
172 case 32:
173 #endif
174 return 1;
176 default:
177 return 0;
181 #define MAX_MAP_LONGS (((MAX_MAP_BANKWIDTH * 8) + BITS_PER_LONG - 1) / BITS_PER_LONG)
183 typedef union {
184 unsigned long x[MAX_MAP_LONGS];
185 } map_word;
187 /* The map stuff is very simple. You fill in your struct map_info with
188 a handful of routines for accessing the device, making sure they handle
189 paging etc. correctly if your device needs it. Then you pass it off
190 to a chip probe routine -- either JEDEC or CFI probe or both -- via
191 do_map_probe(). If a chip is recognised, the probe code will invoke the
192 appropriate chip driver (if present) and return a struct mtd_info.
193 At which point, you fill in the mtd->module with your own module
194 address, and register it with the MTD core code. Or you could partition
195 it and register the partitions instead, or keep it for your own private
196 use; whatever.
198 The mtd->priv field will point to the struct map_info, and any further
199 private data required by the chip driver is linked from the
200 mtd->priv->fldrv_priv field. This allows the map driver to get at
201 the destructor function map->fldrv_destroy() when it's tired
202 of living.
205 struct map_info {
206 const char *name;
207 unsigned long size;
208 resource_size_t phys;
209 #define NO_XIP (-1UL)
211 void __iomem *virt;
212 void *cached;
214 int swap; /* this mapping's byte-swapping requirement */
215 int bankwidth; /* in octets. This isn't necessarily the width
216 of actual bus cycles -- it's the repeat interval
217 in bytes, before you are talking to the first chip again.
220 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
221 map_word (*read)(struct map_info *, unsigned long);
222 void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
224 void (*write)(struct map_info *, const map_word, unsigned long);
225 void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
227 /* We can perhaps put in 'point' and 'unpoint' methods, if we really
228 want to enable XIP for non-linear mappings. Not yet though. */
229 #endif
230 /* It's possible for the map driver to use cached memory in its
231 copy_from implementation (and _only_ with copy_from). However,
232 when the chip driver knows some flash area has changed contents,
233 it will signal it to the map driver through this routine to let
234 the map driver invalidate the corresponding cache as needed.
235 If there is no cache to care about this can be set to NULL. */
236 void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
238 /* This will be called with 1 as parameter when the first map user
239 * needs VPP, and called with 0 when the last user exits. The map
240 * core maintains a reference counter, and assumes that VPP is a
241 * global resource applying to all mapped flash chips on the system.
243 void (*set_vpp)(struct map_info *, int);
245 unsigned long pfow_base;
246 unsigned long map_priv_1;
247 unsigned long map_priv_2;
248 struct device_node *device_node;
249 void *fldrv_priv;
250 struct mtd_chip_driver *fldrv;
253 struct mtd_chip_driver {
254 struct mtd_info *(*probe)(struct map_info *map);
255 void (*destroy)(struct mtd_info *);
256 struct module *module;
257 char *name;
258 struct list_head list;
261 void register_mtd_chip_driver(struct mtd_chip_driver *);
262 void unregister_mtd_chip_driver(struct mtd_chip_driver *);
264 struct mtd_info *do_map_probe(const char *name, struct map_info *map);
265 void map_destroy(struct mtd_info *mtd);
267 #define ENABLE_VPP(map) do { if (map->set_vpp) map->set_vpp(map, 1); } while (0)
268 #define DISABLE_VPP(map) do { if (map->set_vpp) map->set_vpp(map, 0); } while (0)
270 #define INVALIDATE_CACHED_RANGE(map, from, size) \
271 do { if (map->inval_cache) map->inval_cache(map, from, size); } while (0)
273 #define map_word_equal(map, val1, val2) \
274 ({ \
275 int i, ret = 1; \
276 for (i = 0; i < map_words(map); i++) \
277 if ((val1).x[i] != (val2).x[i]) { \
278 ret = 0; \
279 break; \
281 ret; \
284 #define map_word_and(map, val1, val2) \
285 ({ \
286 map_word r; \
287 int i; \
288 for (i = 0; i < map_words(map); i++) \
289 r.x[i] = (val1).x[i] & (val2).x[i]; \
290 r; \
293 #define map_word_clr(map, val1, val2) \
294 ({ \
295 map_word r; \
296 int i; \
297 for (i = 0; i < map_words(map); i++) \
298 r.x[i] = (val1).x[i] & ~(val2).x[i]; \
299 r; \
302 #define map_word_or(map, val1, val2) \
303 ({ \
304 map_word r; \
305 int i; \
306 for (i = 0; i < map_words(map); i++) \
307 r.x[i] = (val1).x[i] | (val2).x[i]; \
308 r; \
311 #define map_word_andequal(map, val1, val2, val3) \
312 ({ \
313 int i, ret = 1; \
314 for (i = 0; i < map_words(map); i++) { \
315 if (((val1).x[i] & (val2).x[i]) != (val2).x[i]) { \
316 ret = 0; \
317 break; \
320 ret; \
323 #define map_word_bitsset(map, val1, val2) \
324 ({ \
325 int i, ret = 0; \
326 for (i = 0; i < map_words(map); i++) { \
327 if ((val1).x[i] & (val2).x[i]) { \
328 ret = 1; \
329 break; \
332 ret; \
335 static inline map_word map_word_load(struct map_info *map, const void *ptr)
337 map_word r;
339 if (map_bankwidth_is_1(map))
340 r.x[0] = *(unsigned char *)ptr;
341 else if (map_bankwidth_is_2(map))
342 r.x[0] = get_unaligned((uint16_t *)ptr);
343 else if (map_bankwidth_is_4(map))
344 r.x[0] = get_unaligned((uint32_t *)ptr);
345 #if BITS_PER_LONG >= 64
346 else if (map_bankwidth_is_8(map))
347 r.x[0] = get_unaligned((uint64_t *)ptr);
348 #endif
349 else if (map_bankwidth_is_large(map))
350 memcpy(r.x, ptr, map->bankwidth);
351 else
352 BUG();
354 return r;
357 static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
359 int i;
361 if (map_bankwidth_is_large(map)) {
362 char *dest = (char *)&orig;
364 memcpy(dest+start, buf, len);
365 } else {
366 for (i = start; i < start+len; i++) {
367 int bitpos;
369 #ifdef __LITTLE_ENDIAN
370 bitpos = i * 8;
371 #else /* __BIG_ENDIAN */
372 bitpos = (map_bankwidth(map) - 1 - i) * 8;
373 #endif
374 orig.x[0] &= ~(0xff << bitpos);
375 orig.x[0] |= (unsigned long)buf[i-start] << bitpos;
378 return orig;
381 #if BITS_PER_LONG < 64
382 #define MAP_FF_LIMIT 4
383 #else
384 #define MAP_FF_LIMIT 8
385 #endif
387 static inline map_word map_word_ff(struct map_info *map)
389 map_word r;
390 int i;
392 if (map_bankwidth(map) < MAP_FF_LIMIT) {
393 int bw = 8 * map_bankwidth(map);
395 r.x[0] = (1UL << bw) - 1;
396 } else {
397 for (i = 0; i < map_words(map); i++)
398 r.x[i] = ~0UL;
400 return r;
403 static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
405 map_word r;
407 if (map_bankwidth_is_1(map))
408 r.x[0] = __raw_readb(map->virt + ofs);
409 else if (map_bankwidth_is_2(map))
410 r.x[0] = __raw_readw(map->virt + ofs);
411 else if (map_bankwidth_is_4(map))
412 r.x[0] = __raw_readl(map->virt + ofs);
413 #if BITS_PER_LONG >= 64
414 else if (map_bankwidth_is_8(map))
415 r.x[0] = __raw_readq(map->virt + ofs);
416 #endif
417 else if (map_bankwidth_is_large(map))
418 memcpy_fromio(r.x, map->virt + ofs, map->bankwidth);
419 else
420 BUG();
422 return r;
425 static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
427 if (map_bankwidth_is_1(map))
428 __raw_writeb(datum.x[0], map->virt + ofs);
429 else if (map_bankwidth_is_2(map))
430 __raw_writew(datum.x[0], map->virt + ofs);
431 else if (map_bankwidth_is_4(map))
432 __raw_writel(datum.x[0], map->virt + ofs);
433 #if BITS_PER_LONG >= 64
434 else if (map_bankwidth_is_8(map))
435 __raw_writeq(datum.x[0], map->virt + ofs);
436 #endif
437 else if (map_bankwidth_is_large(map))
438 memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
439 else
440 BUG();
441 mb();
444 static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
446 if (map->cached)
447 memcpy(to, (char *)map->cached + from, len);
448 else
449 memcpy_fromio(to, map->virt + from, len);
452 static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
454 memcpy_toio(map->virt + to, from, len);
457 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
458 #define map_read(map, ofs) (map)->read(map, ofs)
459 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
460 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
461 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
463 extern void simple_map_init(struct map_info *);
464 #define map_is_linear(map) (map->phys != NO_XIP)
466 #else
467 #define map_read(map, ofs) inline_map_read(map, ofs)
468 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
469 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
470 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
473 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
474 #define map_is_linear(map) ({ (void)(map); 1; })
476 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
478 #endif /* __LINUX_MTD_MAP_H__ */