map.h 12 KB

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