xref: /linux-6.15/include/linux/mtd/map.h (revision 96f951ed)
1 /*
2  * Copyright © 2000-2010 David Woodhouse <[email protected]> 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 
31 #include <asm/unaligned.h>
32 #include <asm/system.h>
33 #include <asm/io.h>
34 #include <asm/barrier.h>
35 
36 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
37 #define map_bankwidth(map) 1
38 #define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
39 #define map_bankwidth_is_large(map) (0)
40 #define map_words(map) (1)
41 #define MAX_MAP_BANKWIDTH 1
42 #else
43 #define map_bankwidth_is_1(map) (0)
44 #endif
45 
46 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
47 # ifdef map_bankwidth
48 #  undef map_bankwidth
49 #  define map_bankwidth(map) ((map)->bankwidth)
50 # else
51 #  define map_bankwidth(map) 2
52 #  define map_bankwidth_is_large(map) (0)
53 #  define map_words(map) (1)
54 # endif
55 #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
56 #undef MAX_MAP_BANKWIDTH
57 #define MAX_MAP_BANKWIDTH 2
58 #else
59 #define map_bankwidth_is_2(map) (0)
60 #endif
61 
62 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
63 # ifdef map_bankwidth
64 #  undef map_bankwidth
65 #  define map_bankwidth(map) ((map)->bankwidth)
66 # else
67 #  define map_bankwidth(map) 4
68 #  define map_bankwidth_is_large(map) (0)
69 #  define map_words(map) (1)
70 # endif
71 #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
72 #undef MAX_MAP_BANKWIDTH
73 #define MAX_MAP_BANKWIDTH 4
74 #else
75 #define map_bankwidth_is_4(map) (0)
76 #endif
77 
78 /* ensure we never evaluate anything shorted than an unsigned long
79  * to zero, and ensure we'll never miss the end of an comparison (bjd) */
80 
81 #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1))/ sizeof(unsigned long))
82 
83 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
84 # ifdef map_bankwidth
85 #  undef map_bankwidth
86 #  define map_bankwidth(map) ((map)->bankwidth)
87 #  if BITS_PER_LONG < 64
88 #   undef map_bankwidth_is_large
89 #   define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
90 #   undef map_words
91 #   define map_words(map) map_calc_words(map)
92 #  endif
93 # else
94 #  define map_bankwidth(map) 8
95 #  define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
96 #  define map_words(map) map_calc_words(map)
97 # endif
98 #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
99 #undef MAX_MAP_BANKWIDTH
100 #define MAX_MAP_BANKWIDTH 8
101 #else
102 #define map_bankwidth_is_8(map) (0)
103 #endif
104 
105 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
106 # ifdef map_bankwidth
107 #  undef map_bankwidth
108 #  define map_bankwidth(map) ((map)->bankwidth)
109 #  undef map_bankwidth_is_large
110 #  define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
111 #  undef map_words
112 #  define map_words(map) map_calc_words(map)
113 # else
114 #  define map_bankwidth(map) 16
115 #  define map_bankwidth_is_large(map) (1)
116 #  define map_words(map) map_calc_words(map)
117 # endif
118 #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
119 #undef MAX_MAP_BANKWIDTH
120 #define MAX_MAP_BANKWIDTH 16
121 #else
122 #define map_bankwidth_is_16(map) (0)
123 #endif
124 
125 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
126 # ifdef map_bankwidth
127 #  undef map_bankwidth
128 #  define map_bankwidth(map) ((map)->bankwidth)
129 #  undef map_bankwidth_is_large
130 #  define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
131 #  undef map_words
132 #  define map_words(map) map_calc_words(map)
133 # else
134 #  define map_bankwidth(map) 32
135 #  define map_bankwidth_is_large(map) (1)
136 #  define map_words(map) map_calc_words(map)
137 # endif
138 #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
139 #undef MAX_MAP_BANKWIDTH
140 #define MAX_MAP_BANKWIDTH 32
141 #else
142 #define map_bankwidth_is_32(map) (0)
143 #endif
144 
145 #ifndef map_bankwidth
146 #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
147 static inline int map_bankwidth(void *map)
148 {
149 	BUG();
150 	return 0;
151 }
152 #define map_bankwidth_is_large(map) (0)
153 #define map_words(map) (0)
154 #define MAX_MAP_BANKWIDTH 1
155 #endif
156 
157 static inline int map_bankwidth_supported(int w)
158 {
159 	switch (w) {
160 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
161 	case 1:
162 #endif
163 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
164 	case 2:
165 #endif
166 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
167 	case 4:
168 #endif
169 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
170 	case 8:
171 #endif
172 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
173 	case 16:
174 #endif
175 #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
176 	case 32:
177 #endif
178 		return 1;
179 
180 	default:
181 		return 0;
182 	}
183 }
184 
185 #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
186 
187 typedef union {
188 	unsigned long x[MAX_MAP_LONGS];
189 } map_word;
190 
191 /* The map stuff is very simple. You fill in your struct map_info with
192    a handful of routines for accessing the device, making sure they handle
193    paging etc. correctly if your device needs it. Then you pass it off
194    to a chip probe routine -- either JEDEC or CFI probe or both -- via
195    do_map_probe(). If a chip is recognised, the probe code will invoke the
196    appropriate chip driver (if present) and return a struct mtd_info.
197    At which point, you fill in the mtd->module with your own module
198    address, and register it with the MTD core code. Or you could partition
199    it and register the partitions instead, or keep it for your own private
200    use; whatever.
201 
202    The mtd->priv field will point to the struct map_info, and any further
203    private data required by the chip driver is linked from the
204    mtd->priv->fldrv_priv field. This allows the map driver to get at
205    the destructor function map->fldrv_destroy() when it's tired
206    of living.
207 */
208 
209 struct map_info {
210 	const char *name;
211 	unsigned long size;
212 	resource_size_t phys;
213 #define NO_XIP (-1UL)
214 
215 	void __iomem *virt;
216 	void *cached;
217 
218 	int swap; /* this mapping's byte-swapping requirement */
219 	int bankwidth; /* in octets. This isn't necessarily the width
220 		       of actual bus cycles -- it's the repeat interval
221 		      in bytes, before you are talking to the first chip again.
222 		      */
223 
224 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
225 	map_word (*read)(struct map_info *, unsigned long);
226 	void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
227 
228 	void (*write)(struct map_info *, const map_word, unsigned long);
229 	void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
230 
231 	/* We can perhaps put in 'point' and 'unpoint' methods, if we really
232 	   want to enable XIP for non-linear mappings. Not yet though. */
233 #endif
234 	/* It's possible for the map driver to use cached memory in its
235 	   copy_from implementation (and _only_ with copy_from).  However,
236 	   when the chip driver knows some flash area has changed contents,
237 	   it will signal it to the map driver through this routine to let
238 	   the map driver invalidate the corresponding cache as needed.
239 	   If there is no cache to care about this can be set to NULL. */
240 	void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
241 
242 	/* set_vpp() must handle being reentered -- enable, enable, disable
243 	   must leave it enabled. */
244 	void (*set_vpp)(struct map_info *, int);
245 
246 	unsigned long pfow_base;
247 	unsigned long map_priv_1;
248 	unsigned long map_priv_2;
249 	void *fldrv_priv;
250 	struct mtd_chip_driver *fldrv;
251 };
252 
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;
259 };
260 
261 void register_mtd_chip_driver(struct mtd_chip_driver *);
262 void unregister_mtd_chip_driver(struct mtd_chip_driver *);
263 
264 struct mtd_info *do_map_probe(const char *name, struct map_info *map);
265 void map_destroy(struct mtd_info *mtd);
266 
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)
269 
270 #define INVALIDATE_CACHED_RANGE(map, from, size) \
271 	do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
272 
273 
274 static inline int map_word_equal(struct map_info *map, map_word val1, map_word val2)
275 {
276 	int i;
277 	for (i=0; i<map_words(map); i++) {
278 		if (val1.x[i] != val2.x[i])
279 			return 0;
280 	}
281 	return 1;
282 }
283 
284 static inline map_word map_word_and(struct map_info *map, map_word val1, map_word val2)
285 {
286 	map_word r;
287 	int i;
288 
289 	for (i=0; i<map_words(map); i++) {
290 		r.x[i] = val1.x[i] & val2.x[i];
291 	}
292 	return r;
293 }
294 
295 static inline map_word map_word_clr(struct map_info *map, map_word val1, map_word val2)
296 {
297 	map_word r;
298 	int i;
299 
300 	for (i=0; i<map_words(map); i++) {
301 		r.x[i] = val1.x[i] & ~val2.x[i];
302 	}
303 	return r;
304 }
305 
306 static inline map_word map_word_or(struct map_info *map, map_word val1, map_word val2)
307 {
308 	map_word r;
309 	int i;
310 
311 	for (i=0; i<map_words(map); i++) {
312 		r.x[i] = val1.x[i] | val2.x[i];
313 	}
314 	return r;
315 }
316 
317 #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
318 
319 static inline int map_word_bitsset(struct map_info *map, map_word val1, map_word val2)
320 {
321 	int i;
322 
323 	for (i=0; i<map_words(map); i++) {
324 		if (val1.x[i] & val2.x[i])
325 			return 1;
326 	}
327 	return 0;
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 
347 	return r;
348 }
349 
350 static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
351 {
352 	int i;
353 
354 	if (map_bankwidth_is_large(map)) {
355 		char *dest = (char *)&orig;
356 		memcpy(dest+start, buf, len);
357 	} else {
358 		for (i=start; i < start+len; i++) {
359 			int bitpos;
360 #ifdef __LITTLE_ENDIAN
361 			bitpos = i*8;
362 #else /* __BIG_ENDIAN */
363 			bitpos = (map_bankwidth(map)-1-i)*8;
364 #endif
365 			orig.x[0] &= ~(0xff << bitpos);
366 			orig.x[0] |= buf[i-start] << bitpos;
367 		}
368 	}
369 	return orig;
370 }
371 
372 #if BITS_PER_LONG < 64
373 #define MAP_FF_LIMIT 4
374 #else
375 #define MAP_FF_LIMIT 8
376 #endif
377 
378 static inline map_word map_word_ff(struct map_info *map)
379 {
380 	map_word r;
381 	int i;
382 
383 	if (map_bankwidth(map) < MAP_FF_LIMIT) {
384 		int bw = 8 * map_bankwidth(map);
385 		r.x[0] = (1 << bw) - 1;
386 	} else {
387 		for (i=0; i<map_words(map); i++)
388 			r.x[i] = ~0UL;
389 	}
390 	return r;
391 }
392 
393 static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
394 {
395 	map_word r;
396 
397 	if (map_bankwidth_is_1(map))
398 		r.x[0] = __raw_readb(map->virt + ofs);
399 	else if (map_bankwidth_is_2(map))
400 		r.x[0] = __raw_readw(map->virt + ofs);
401 	else if (map_bankwidth_is_4(map))
402 		r.x[0] = __raw_readl(map->virt + ofs);
403 #if BITS_PER_LONG >= 64
404 	else if (map_bankwidth_is_8(map))
405 		r.x[0] = __raw_readq(map->virt + ofs);
406 #endif
407 	else if (map_bankwidth_is_large(map))
408 		memcpy_fromio(r.x, map->virt+ofs, map->bankwidth);
409 	else
410 		BUG();
411 
412 	return r;
413 }
414 
415 static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
416 {
417 	if (map_bankwidth_is_1(map))
418 		__raw_writeb(datum.x[0], map->virt + ofs);
419 	else if (map_bankwidth_is_2(map))
420 		__raw_writew(datum.x[0], map->virt + ofs);
421 	else if (map_bankwidth_is_4(map))
422 		__raw_writel(datum.x[0], map->virt + ofs);
423 #if BITS_PER_LONG >= 64
424 	else if (map_bankwidth_is_8(map))
425 		__raw_writeq(datum.x[0], map->virt + ofs);
426 #endif
427 	else if (map_bankwidth_is_large(map))
428 		memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
429 	mb();
430 }
431 
432 static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
433 {
434 	if (map->cached)
435 		memcpy(to, (char *)map->cached + from, len);
436 	else
437 		memcpy_fromio(to, map->virt + from, len);
438 }
439 
440 static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
441 {
442 	memcpy_toio(map->virt + to, from, len);
443 }
444 
445 #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
446 #define map_read(map, ofs) (map)->read(map, ofs)
447 #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
448 #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
449 #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
450 
451 extern void simple_map_init(struct map_info *);
452 #define map_is_linear(map) (map->phys != NO_XIP)
453 
454 #else
455 #define map_read(map, ofs) inline_map_read(map, ofs)
456 #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
457 #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
458 #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
459 
460 
461 #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
462 #define map_is_linear(map) ({ (void)(map); 1; })
463 
464 #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
465 
466 #endif /* __LINUX_MTD_MAP_H__ */
467