GNU Linux-libre 4.4.285-gnu1
[releases.git] / include / linux / mtd / map.h
1 /*
2  * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> et al.
3  *
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.
8  *
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.
13  *
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
17  *
18  */
19
20 /* Overhauled routines for dealing with different mmap regions of flash */
21
22 #ifndef __LINUX_MTD_MAP_H__
23 #define __LINUX_MTD_MAP_H__
24
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>
31
32 #include <asm/unaligned.h>
33 #include <asm/barrier.h>
34
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
44
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
60
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
76
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) */
79
80 #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1)) / sizeof(unsigned long))
81
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
103
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
123
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
138
139 #ifndef map_bankwidth
140 #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
141 static inline int map_bankwidth(void *map)
142 {
143         BUG();
144         return 0;
145 }
146 #define map_bankwidth_is_large(map) (0)
147 #define map_words(map) (0)
148 #define MAX_MAP_BANKWIDTH 1
149 #endif
150
151 static inline int map_bankwidth_supported(int w)
152 {
153         switch (w) {
154 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
155         case 1:
156 #endif
157 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
158         case 2:
159 #endif
160 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
161         case 4:
162 #endif
163 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
164         case 8:
165 #endif
166 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
167         case 16:
168 #endif
169 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
170         case 32:
171 #endif
172                 return 1;
173
174         default:
175                 return 0;
176         }
177 }
178
179 #define MAX_MAP_LONGS (((MAX_MAP_BANKWIDTH * 8) + BITS_PER_LONG - 1) / BITS_PER_LONG)
180
181 typedef union {
182         unsigned long x[MAX_MAP_LONGS];
183 } map_word;
184
185 /* The map stuff is very simple. You fill in your struct map_info with
186    a handful of routines for accessing the device, making sure they handle
187    paging etc. correctly if your device needs it. Then you pass it off
188    to a chip probe routine -- either JEDEC or CFI probe or both -- via
189    do_map_probe(). If a chip is recognised, the probe code will invoke the
190    appropriate chip driver (if present) and return a struct mtd_info.
191    At which point, you fill in the mtd->module with your own module
192    address, and register it with the MTD core code. Or you could partition
193    it and register the partitions instead, or keep it for your own private
194    use; whatever.
195
196    The mtd->priv field will point to the struct map_info, and any further
197    private data required by the chip driver is linked from the
198    mtd->priv->fldrv_priv field. This allows the map driver to get at
199    the destructor function map->fldrv_destroy() when it's tired
200    of living.
201 */
202
203 struct map_info {
204         const char *name;
205         unsigned long size;
206         resource_size_t phys;
207 #define NO_XIP (-1UL)
208
209         void __iomem *virt;
210         void *cached;
211
212         int swap; /* this mapping's byte-swapping requirement */
213         int bankwidth; /* in octets. This isn't necessarily the width
214                        of actual bus cycles -- it's the repeat interval
215                       in bytes, before you are talking to the first chip again.
216                       */
217
218 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
219         map_word (*read)(struct map_info *, unsigned long);
220         void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
221
222         void (*write)(struct map_info *, const map_word, unsigned long);
223         void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
224
225         /* We can perhaps put in 'point' and 'unpoint' methods, if we really
226            want to enable XIP for non-linear mappings. Not yet though. */
227 #endif
228         /* It's possible for the map driver to use cached memory in its
229            copy_from implementation (and _only_ with copy_from).  However,
230            when the chip driver knows some flash area has changed contents,
231            it will signal it to the map driver through this routine to let
232            the map driver invalidate the corresponding cache as needed.
233            If there is no cache to care about this can be set to NULL. */
234         void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
235
236         /* set_vpp() must handle being reentered -- enable, enable, disable
237            must leave it enabled. */
238         void (*set_vpp)(struct map_info *, int);
239
240         unsigned long pfow_base;
241         unsigned long map_priv_1;
242         unsigned long map_priv_2;
243         struct device_node *device_node;
244         void *fldrv_priv;
245         struct mtd_chip_driver *fldrv;
246 };
247
248 struct mtd_chip_driver {
249         struct mtd_info *(*probe)(struct map_info *map);
250         void (*destroy)(struct mtd_info *);
251         struct module *module;
252         char *name;
253         struct list_head list;
254 };
255
256 void register_mtd_chip_driver(struct mtd_chip_driver *);
257 void unregister_mtd_chip_driver(struct mtd_chip_driver *);
258
259 struct mtd_info *do_map_probe(const char *name, struct map_info *map);
260 void map_destroy(struct mtd_info *mtd);
261
262 #define ENABLE_VPP(map) do { if (map->set_vpp) map->set_vpp(map, 1); } while (0)
263 #define DISABLE_VPP(map) do { if (map->set_vpp) map->set_vpp(map, 0); } while (0)
264
265 #define INVALIDATE_CACHED_RANGE(map, from, size) \
266         do { if (map->inval_cache) map->inval_cache(map, from, size); } while (0)
267
268 #define map_word_equal(map, val1, val2)                                 \
269 ({                                                                      \
270         int i, ret = 1;                                                 \
271         for (i = 0; i < map_words(map); i++)                            \
272                 if ((val1).x[i] != (val2).x[i]) {                       \
273                         ret = 0;                                        \
274                         break;                                          \
275                 }                                                       \
276         ret;                                                            \
277 })
278
279 #define map_word_and(map, val1, val2)                                   \
280 ({                                                                      \
281         map_word r;                                                     \
282         int i;                                                          \
283         for (i = 0; i < map_words(map); i++)                            \
284                 r.x[i] = (val1).x[i] & (val2).x[i];                     \
285         r;                                                              \
286 })
287
288 #define map_word_clr(map, val1, val2)                                   \
289 ({                                                                      \
290         map_word r;                                                     \
291         int i;                                                          \
292         for (i = 0; i < map_words(map); i++)                            \
293                 r.x[i] = (val1).x[i] & ~(val2).x[i];                    \
294         r;                                                              \
295 })
296
297 #define map_word_or(map, val1, val2)                                    \
298 ({                                                                      \
299         map_word r;                                                     \
300         int i;                                                          \
301         for (i = 0; i < map_words(map); i++)                            \
302                 r.x[i] = (val1).x[i] | (val2).x[i];                     \
303         r;                                                              \
304 })
305
306 #define map_word_andequal(map, val1, val2, val3)                        \
307 ({                                                                      \
308         int i, ret = 1;                                                 \
309         for (i = 0; i < map_words(map); i++) {                          \
310                 if (((val1).x[i] & (val2).x[i]) != (val3).x[i]) {       \
311                         ret = 0;                                        \
312                         break;                                          \
313                 }                                                       \
314         }                                                               \
315         ret;                                                            \
316 })
317
318 #define map_word_bitsset(map, val1, val2)                               \
319 ({                                                                      \
320         int i, ret = 0;                                                 \
321         for (i = 0; i < map_words(map); i++) {                          \
322                 if ((val1).x[i] & (val2).x[i]) {                        \
323                         ret = 1;                                        \
324                         break;                                          \
325                 }                                                       \
326         }                                                               \
327         ret;                                                            \
328 })
329
330 static inline map_word map_word_load(struct map_info *map, const void *ptr)
331 {
332         map_word r;
333
334         if (map_bankwidth_is_1(map))
335                 r.x[0] = *(unsigned char *)ptr;
336         else if (map_bankwidth_is_2(map))
337                 r.x[0] = get_unaligned((uint16_t *)ptr);
338         else if (map_bankwidth_is_4(map))
339                 r.x[0] = get_unaligned((uint32_t *)ptr);
340 #if BITS_PER_LONG >= 64
341         else if (map_bankwidth_is_8(map))
342                 r.x[0] = get_unaligned((uint64_t *)ptr);
343 #endif
344         else if (map_bankwidth_is_large(map))
345                 memcpy(r.x, ptr, map->bankwidth);
346         else
347                 BUG();
348
349         return r;
350 }
351
352 static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
353 {
354         int i;
355
356         if (map_bankwidth_is_large(map)) {
357                 char *dest = (char *)&orig;
358
359                 memcpy(dest+start, buf, len);
360         } else {
361                 for (i = start; i < start+len; i++) {
362                         int bitpos;
363
364 #ifdef __LITTLE_ENDIAN
365                         bitpos = i * 8;
366 #else /* __BIG_ENDIAN */
367                         bitpos = (map_bankwidth(map) - 1 - i) * 8;
368 #endif
369                         orig.x[0] &= ~(0xff << bitpos);
370                         orig.x[0] |= (unsigned long)buf[i-start] << bitpos;
371                 }
372         }
373         return orig;
374 }
375
376 #if BITS_PER_LONG < 64
377 #define MAP_FF_LIMIT 4
378 #else
379 #define MAP_FF_LIMIT 8
380 #endif
381
382 static inline map_word map_word_ff(struct map_info *map)
383 {
384         map_word r;
385         int i;
386
387         if (map_bankwidth(map) < MAP_FF_LIMIT) {
388                 int bw = 8 * map_bankwidth(map);
389
390                 r.x[0] = (1UL << bw) - 1;
391         } else {
392                 for (i = 0; i < map_words(map); i++)
393                         r.x[i] = ~0UL;
394         }
395         return r;
396 }
397
398 static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
399 {
400         map_word r;
401
402         if (map_bankwidth_is_1(map))
403                 r.x[0] = __raw_readb(map->virt + ofs);
404         else if (map_bankwidth_is_2(map))
405                 r.x[0] = __raw_readw(map->virt + ofs);
406         else if (map_bankwidth_is_4(map))
407                 r.x[0] = __raw_readl(map->virt + ofs);
408 #if BITS_PER_LONG >= 64
409         else if (map_bankwidth_is_8(map))
410                 r.x[0] = __raw_readq(map->virt + ofs);
411 #endif
412         else if (map_bankwidth_is_large(map))
413                 memcpy_fromio(r.x, map->virt + ofs, map->bankwidth);
414         else
415                 BUG();
416
417         return r;
418 }
419
420 static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
421 {
422         if (map_bankwidth_is_1(map))
423                 __raw_writeb(datum.x[0], map->virt + ofs);
424         else if (map_bankwidth_is_2(map))
425                 __raw_writew(datum.x[0], map->virt + ofs);
426         else if (map_bankwidth_is_4(map))
427                 __raw_writel(datum.x[0], map->virt + ofs);
428 #if BITS_PER_LONG >= 64
429         else if (map_bankwidth_is_8(map))
430                 __raw_writeq(datum.x[0], map->virt + ofs);
431 #endif
432         else if (map_bankwidth_is_large(map))
433                 memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
434         else
435                 BUG();
436         mb();
437 }
438
439 static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
440 {
441         if (map->cached)
442                 memcpy(to, (char *)map->cached + from, len);
443         else
444                 memcpy_fromio(to, map->virt + from, len);
445 }
446
447 static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
448 {
449         memcpy_toio(map->virt + to, from, len);
450 }
451
452 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
453 #define map_read(map, ofs) (map)->read(map, ofs)
454 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
455 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
456 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
457
458 extern void simple_map_init(struct map_info *);
459 #define map_is_linear(map) (map->phys != NO_XIP)
460
461 #else
462 #define map_read(map, ofs) inline_map_read(map, ofs)
463 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
464 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
465 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
466
467
468 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
469 #define map_is_linear(map) ({ (void)(map); 1; })
470
471 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
472
473 #endif /* __LINUX_MTD_MAP_H__ */