blob: dd36d9433f000a35510ef32bab2fe8146759cf61 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/* Overhauled routines for dealing with different mmap regions of flash */
Thomas Gleixner3a3ab482005-05-24 20:50:18 +02003/* $Id: map.h,v 1.49 2005/05/24 18:45:15 gleixner Exp $ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5#ifndef __LINUX_MTD_MAP_H__
6#define __LINUX_MTD_MAP_H__
7
8#include <linux/config.h>
9#include <linux/types.h>
10#include <linux/list.h>
11#include <linux/mtd/compatmac.h>
12#include <asm/unaligned.h>
13#include <asm/system.h>
14#include <asm/io.h>
15#include <asm/bug.h>
16
17#ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
18#define map_bankwidth(map) 1
19#define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
20#define map_bankwidth_is_large(map) (0)
21#define map_words(map) (1)
22#define MAX_MAP_BANKWIDTH 1
23#else
24#define map_bankwidth_is_1(map) (0)
25#endif
26
27#ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
28# ifdef map_bankwidth
29# undef map_bankwidth
30# define map_bankwidth(map) ((map)->bankwidth)
31# else
32# define map_bankwidth(map) 2
33# define map_bankwidth_is_large(map) (0)
34# define map_words(map) (1)
35# endif
36#define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
37#undef MAX_MAP_BANKWIDTH
38#define MAX_MAP_BANKWIDTH 2
39#else
40#define map_bankwidth_is_2(map) (0)
41#endif
42
43#ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
44# ifdef map_bankwidth
45# undef map_bankwidth
46# define map_bankwidth(map) ((map)->bankwidth)
47# else
48# define map_bankwidth(map) 4
49# define map_bankwidth_is_large(map) (0)
50# define map_words(map) (1)
51# endif
52#define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
53#undef MAX_MAP_BANKWIDTH
54#define MAX_MAP_BANKWIDTH 4
55#else
56#define map_bankwidth_is_4(map) (0)
57#endif
58
59/* ensure we never evaluate anything shorted than an unsigned long
60 * to zero, and ensure we'll never miss the end of an comparison (bjd) */
61
62#define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1))/ sizeof(unsigned long))
63
64#ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
65# ifdef map_bankwidth
66# undef map_bankwidth
67# define map_bankwidth(map) ((map)->bankwidth)
68# if BITS_PER_LONG < 64
69# undef map_bankwidth_is_large
70# define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
71# undef map_words
72# define map_words(map) map_calc_words(map)
73# endif
74# else
75# define map_bankwidth(map) 8
76# define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
77# define map_words(map) map_calc_words(map)
78# endif
79#define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
80#undef MAX_MAP_BANKWIDTH
81#define MAX_MAP_BANKWIDTH 8
82#else
83#define map_bankwidth_is_8(map) (0)
84#endif
85
86#ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
87# ifdef map_bankwidth
88# undef map_bankwidth
89# define map_bankwidth(map) ((map)->bankwidth)
90# undef map_bankwidth_is_large
91# define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
92# undef map_words
93# define map_words(map) map_calc_words(map)
94# else
95# define map_bankwidth(map) 16
96# define map_bankwidth_is_large(map) (1)
97# define map_words(map) map_calc_words(map)
98# endif
99#define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
100#undef MAX_MAP_BANKWIDTH
101#define MAX_MAP_BANKWIDTH 16
102#else
103#define map_bankwidth_is_16(map) (0)
104#endif
105
106#ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
107# ifdef map_bankwidth
108# undef map_bankwidth
109# define map_bankwidth(map) ((map)->bankwidth)
110# undef map_bankwidth_is_large
111# define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
112# undef map_words
113# define map_words(map) map_calc_words(map)
114# else
115# define map_bankwidth(map) 32
116# define map_bankwidth_is_large(map) (1)
117# define map_words(map) map_calc_words(map)
118# endif
119#define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
120#undef MAX_MAP_BANKWIDTH
121#define MAX_MAP_BANKWIDTH 32
122#else
123#define map_bankwidth_is_32(map) (0)
124#endif
125
126#ifndef map_bankwidth
127#error "No bus width supported. What's the point?"
128#endif
129
130static inline int map_bankwidth_supported(int w)
131{
132 switch (w) {
133#ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
134 case 1:
135#endif
136#ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
137 case 2:
138#endif
139#ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
140 case 4:
141#endif
142#ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
143 case 8:
144#endif
145#ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
146 case 16:
147#endif
148#ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
149 case 32:
150#endif
151 return 1;
152
153 default:
154 return 0;
155 }
156}
157
158#define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
159
160typedef union {
161 unsigned long x[MAX_MAP_LONGS];
162} map_word;
163
164/* The map stuff is very simple. You fill in your struct map_info with
165 a handful of routines for accessing the device, making sure they handle
166 paging etc. correctly if your device needs it. Then you pass it off
167 to a chip probe routine -- either JEDEC or CFI probe or both -- via
168 do_map_probe(). If a chip is recognised, the probe code will invoke the
169 appropriate chip driver (if present) and return a struct mtd_info.
170 At which point, you fill in the mtd->module with your own module
171 address, and register it with the MTD core code. Or you could partition
172 it and register the partitions instead, or keep it for your own private
173 use; whatever.
174
175 The mtd->priv field will point to the struct map_info, and any further
176 private data required by the chip driver is linked from the
177 mtd->priv->fldrv_priv field. This allows the map driver to get at
178 the destructor function map->fldrv_destroy() when it's tired
179 of living.
180*/
181
182struct map_info {
183 char *name;
184 unsigned long size;
185 unsigned long phys;
186#define NO_XIP (-1UL)
187
188 void __iomem *virt;
189 void *cached;
190
191 int bankwidth; /* in octets. This isn't necessarily the width
192 of actual bus cycles -- it's the repeat interval
193 in bytes, before you are talking to the first chip again.
194 */
195
196#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
197 map_word (*read)(struct map_info *, unsigned long);
198 void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
199
200 void (*write)(struct map_info *, const map_word, unsigned long);
201 void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
202
203 /* We can perhaps put in 'point' and 'unpoint' methods, if we really
204 want to enable XIP for non-linear mappings. Not yet though. */
205#endif
206 /* It's possible for the map driver to use cached memory in its
207 copy_from implementation (and _only_ with copy_from). However,
208 when the chip driver knows some flash area has changed contents,
209 it will signal it to the map driver through this routine to let
210 the map driver invalidate the corresponding cache as needed.
211 If there is no cache to care about this can be set to NULL. */
212 void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
213
214 /* set_vpp() must handle being reentered -- enable, enable, disable
215 must leave it enabled. */
216 void (*set_vpp)(struct map_info *, int);
217
218 unsigned long map_priv_1;
219 unsigned long map_priv_2;
220 void *fldrv_priv;
221 struct mtd_chip_driver *fldrv;
222};
223
224struct mtd_chip_driver {
225 struct mtd_info *(*probe)(struct map_info *map);
226 void (*destroy)(struct mtd_info *);
227 struct module *module;
228 char *name;
229 struct list_head list;
230};
231
232void register_mtd_chip_driver(struct mtd_chip_driver *);
233void unregister_mtd_chip_driver(struct mtd_chip_driver *);
234
235struct mtd_info *do_map_probe(const char *name, struct map_info *map);
236void map_destroy(struct mtd_info *mtd);
237
238#define ENABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 1); } while(0)
239#define DISABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 0); } while(0)
240
241#define INVALIDATE_CACHED_RANGE(map, from, size) \
242 do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
243
244
245static inline int map_word_equal(struct map_info *map, map_word val1, map_word val2)
246{
247 int i;
248 for (i=0; i<map_words(map); i++) {
249 if (val1.x[i] != val2.x[i])
250 return 0;
251 }
252 return 1;
253}
254
255static inline map_word map_word_and(struct map_info *map, map_word val1, map_word val2)
256{
257 map_word r;
258 int i;
259
260 for (i=0; i<map_words(map); i++) {
261 r.x[i] = val1.x[i] & val2.x[i];
262 }
263 return r;
264}
265
Nicolas Pitref77814d2005-02-08 17:11:19 +0000266static inline map_word map_word_clr(struct map_info *map, map_word val1, map_word val2)
267{
268 map_word r;
269 int i;
270
271 for (i=0; i<map_words(map); i++) {
272 r.x[i] = val1.x[i] & ~val2.x[i];
273 }
274 return r;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static inline map_word map_word_or(struct map_info *map, map_word val1, map_word val2)
278{
279 map_word r;
280 int i;
281
282 for (i=0; i<map_words(map); i++) {
283 r.x[i] = val1.x[i] | val2.x[i];
284 }
285 return r;
286}
Nicolas Pitref77814d2005-02-08 17:11:19 +0000287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288#define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
289
290static inline int map_word_bitsset(struct map_info *map, map_word val1, map_word val2)
291{
292 int i;
293
294 for (i=0; i<map_words(map); i++) {
295 if (val1.x[i] & val2.x[i])
296 return 1;
297 }
298 return 0;
299}
300
301static inline map_word map_word_load(struct map_info *map, const void *ptr)
302{
303 map_word r;
304
305 if (map_bankwidth_is_1(map))
306 r.x[0] = *(unsigned char *)ptr;
307 else if (map_bankwidth_is_2(map))
308 r.x[0] = get_unaligned((uint16_t *)ptr);
309 else if (map_bankwidth_is_4(map))
310 r.x[0] = get_unaligned((uint32_t *)ptr);
311#if BITS_PER_LONG >= 64
312 else if (map_bankwidth_is_8(map))
313 r.x[0] = get_unaligned((uint64_t *)ptr);
314#endif
315 else if (map_bankwidth_is_large(map))
316 memcpy(r.x, ptr, map->bankwidth);
317
318 return r;
319}
320
321static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
322{
323 int i;
324
325 if (map_bankwidth_is_large(map)) {
326 char *dest = (char *)&orig;
327 memcpy(dest+start, buf, len);
328 } else {
329 for (i=start; i < start+len; i++) {
330 int bitpos;
331#ifdef __LITTLE_ENDIAN
332 bitpos = i*8;
333#else /* __BIG_ENDIAN */
334 bitpos = (map_bankwidth(map)-1-i)*8;
335#endif
336 orig.x[0] &= ~(0xff << bitpos);
337 orig.x[0] |= buf[i-start] << bitpos;
338 }
339 }
340 return orig;
341}
342
Thomas Gleixner3a3ab482005-05-24 20:50:18 +0200343#if BITS_PER_LONG < 64
344#define MAP_FF_LIMIT 4
345#else
346#define MAP_FF_LIMIT 8
347#endif
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349static inline map_word map_word_ff(struct map_info *map)
350{
351 map_word r;
352 int i;
Thomas Gleixner3a3ab482005-05-24 20:50:18 +0200353
354 if (map_bank_width(map) < MAP_FF_LIMIT) {
355 r.x[0] = (1 << (8*map_bank_width(map))) - 1;
356 } else {
357 for (i=0; i<map_words(map); i++)
358 r.x[i] = ~0UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
360 return r;
361}
Nicolas Pitref77814d2005-02-08 17:11:19 +0000362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
364{
365 map_word r;
366
367 if (map_bankwidth_is_1(map))
368 r.x[0] = __raw_readb(map->virt + ofs);
369 else if (map_bankwidth_is_2(map))
370 r.x[0] = __raw_readw(map->virt + ofs);
371 else if (map_bankwidth_is_4(map))
372 r.x[0] = __raw_readl(map->virt + ofs);
373#if BITS_PER_LONG >= 64
374 else if (map_bankwidth_is_8(map))
375 r.x[0] = __raw_readq(map->virt + ofs);
376#endif
377 else if (map_bankwidth_is_large(map))
378 memcpy_fromio(r.x, map->virt+ofs, map->bankwidth);
379
380 return r;
381}
382
383static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
384{
385 if (map_bankwidth_is_1(map))
386 __raw_writeb(datum.x[0], map->virt + ofs);
387 else if (map_bankwidth_is_2(map))
388 __raw_writew(datum.x[0], map->virt + ofs);
389 else if (map_bankwidth_is_4(map))
390 __raw_writel(datum.x[0], map->virt + ofs);
391#if BITS_PER_LONG >= 64
392 else if (map_bankwidth_is_8(map))
393 __raw_writeq(datum.x[0], map->virt + ofs);
394#endif
395 else if (map_bankwidth_is_large(map))
396 memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
397 mb();
398}
399
400static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
401{
402 if (map->cached)
403 memcpy(to, (char *)map->cached + from, len);
404 else
405 memcpy_fromio(to, map->virt + from, len);
406}
407
408static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
409{
410 memcpy_toio(map->virt + to, from, len);
411}
412
413#ifdef CONFIG_MTD_COMPLEX_MAPPINGS
414#define map_read(map, ofs) (map)->read(map, ofs)
415#define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
416#define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
417#define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
418
419extern void simple_map_init(struct map_info *);
420#define map_is_linear(map) (map->phys != NO_XIP)
421
422#else
423#define map_read(map, ofs) inline_map_read(map, ofs)
424#define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
425#define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
426#define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
427
428
429#define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
Nicolas Pitref16407d2005-02-16 15:55:03 +0000430#define map_is_linear(map) ({ (void)(map); 1; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432#endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
433
434#endif /* __LINUX_MTD_MAP_H__ */