map.h 11 KB

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