map.h 12 KB

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