map.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
  115. static inline int map_bankwidth(void *map)
  116. {
  117. BUG();
  118. return 0;
  119. }
  120. #define map_bankwidth_is_large(map) (0)
  121. #define map_words(map) (0)
  122. #define MAX_MAP_BANKWIDTH 1
  123. #endif
  124. static inline int map_bankwidth_supported(int w)
  125. {
  126. switch (w) {
  127. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
  128. case 1:
  129. #endif
  130. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
  131. case 2:
  132. #endif
  133. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
  134. case 4:
  135. #endif
  136. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
  137. case 8:
  138. #endif
  139. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
  140. case 16:
  141. #endif
  142. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
  143. case 32:
  144. #endif
  145. return 1;
  146. default:
  147. return 0;
  148. }
  149. }
  150. #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
  151. typedef union {
  152. unsigned long x[MAX_MAP_LONGS];
  153. } map_word;
  154. /* The map stuff is very simple. You fill in your struct map_info with
  155. a handful of routines for accessing the device, making sure they handle
  156. paging etc. correctly if your device needs it. Then you pass it off
  157. to a chip probe routine -- either JEDEC or CFI probe or both -- via
  158. do_map_probe(). If a chip is recognised, the probe code will invoke the
  159. appropriate chip driver (if present) and return a struct mtd_info.
  160. At which point, you fill in the mtd->module with your own module
  161. address, and register it with the MTD core code. Or you could partition
  162. it and register the partitions instead, or keep it for your own private
  163. use; whatever.
  164. The mtd->priv field will point to the struct map_info, and any further
  165. private data required by the chip driver is linked from the
  166. mtd->priv->fldrv_priv field. This allows the map driver to get at
  167. the destructor function map->fldrv_destroy() when it's tired
  168. of living.
  169. */
  170. struct map_info {
  171. const char *name;
  172. unsigned long size;
  173. resource_size_t phys;
  174. #define NO_XIP (-1UL)
  175. void __iomem *virt;
  176. void *cached;
  177. int bankwidth; /* in octets. This isn't necessarily the width
  178. of actual bus cycles -- it's the repeat interval
  179. in bytes, before you are talking to the first chip again.
  180. */
  181. #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
  182. map_word (*read)(struct map_info *, unsigned long);
  183. void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
  184. void (*write)(struct map_info *, const map_word, unsigned long);
  185. void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
  186. /* We can perhaps put in 'point' and 'unpoint' methods, if we really
  187. want to enable XIP for non-linear mappings. Not yet though. */
  188. #endif
  189. /* It's possible for the map driver to use cached memory in its
  190. copy_from implementation (and _only_ with copy_from). However,
  191. when the chip driver knows some flash area has changed contents,
  192. it will signal it to the map driver through this routine to let
  193. the map driver invalidate the corresponding cache as needed.
  194. If there is no cache to care about this can be set to NULL. */
  195. void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
  196. /* set_vpp() must handle being reentered -- enable, enable, disable
  197. must leave it enabled. */
  198. void (*set_vpp)(struct map_info *, int);
  199. unsigned long map_priv_1;
  200. unsigned long map_priv_2;
  201. void *fldrv_priv;
  202. struct mtd_chip_driver *fldrv;
  203. };
  204. struct mtd_chip_driver {
  205. struct mtd_info *(*probe)(struct map_info *map);
  206. void (*destroy)(struct mtd_info *);
  207. struct module *module;
  208. char *name;
  209. struct list_head list;
  210. };
  211. void register_mtd_chip_driver(struct mtd_chip_driver *);
  212. void unregister_mtd_chip_driver(struct mtd_chip_driver *);
  213. struct mtd_info *do_map_probe(const char *name, struct map_info *map);
  214. void map_destroy(struct mtd_info *mtd);
  215. #define ENABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 1); } while(0)
  216. #define DISABLE_VPP(map) do { if(map->set_vpp) map->set_vpp(map, 0); } while(0)
  217. #define INVALIDATE_CACHED_RANGE(map, from, size) \
  218. do { if(map->inval_cache) map->inval_cache(map, from, size); } while(0)
  219. static inline int map_word_equal(struct map_info *map, map_word val1, map_word val2)
  220. {
  221. int i;
  222. for (i=0; i<map_words(map); i++) {
  223. if (val1.x[i] != val2.x[i])
  224. return 0;
  225. }
  226. return 1;
  227. }
  228. static inline map_word map_word_and(struct map_info *map, map_word val1, map_word val2)
  229. {
  230. map_word r;
  231. int i;
  232. for (i=0; i<map_words(map); i++) {
  233. r.x[i] = val1.x[i] & val2.x[i];
  234. }
  235. return r;
  236. }
  237. static inline map_word map_word_clr(struct map_info *map, map_word val1, map_word val2)
  238. {
  239. map_word r;
  240. int i;
  241. for (i=0; i<map_words(map); i++) {
  242. r.x[i] = val1.x[i] & ~val2.x[i];
  243. }
  244. return r;
  245. }
  246. static inline map_word map_word_or(struct map_info *map, map_word val1, map_word val2)
  247. {
  248. map_word r;
  249. int i;
  250. for (i=0; i<map_words(map); i++) {
  251. r.x[i] = val1.x[i] | val2.x[i];
  252. }
  253. return r;
  254. }
  255. #define map_word_andequal(m, a, b, z) map_word_equal(m, z, map_word_and(m, a, b))
  256. static inline int map_word_bitsset(struct map_info *map, map_word val1, map_word val2)
  257. {
  258. int i;
  259. for (i=0; i<map_words(map); i++) {
  260. if (val1.x[i] & val2.x[i])
  261. return 1;
  262. }
  263. return 0;
  264. }
  265. static inline map_word map_word_load(struct map_info *map, const void *ptr)
  266. {
  267. map_word r;
  268. if (map_bankwidth_is_1(map))
  269. r.x[0] = *(unsigned char *)ptr;
  270. else if (map_bankwidth_is_2(map))
  271. r.x[0] = get_unaligned((uint16_t *)ptr);
  272. else if (map_bankwidth_is_4(map))
  273. r.x[0] = get_unaligned((uint32_t *)ptr);
  274. #if BITS_PER_LONG >= 64
  275. else if (map_bankwidth_is_8(map))
  276. r.x[0] = get_unaligned((uint64_t *)ptr);
  277. #endif
  278. else if (map_bankwidth_is_large(map))
  279. memcpy(r.x, ptr, map->bankwidth);
  280. return r;
  281. }
  282. static inline map_word map_word_load_partial(struct map_info *map, map_word orig, const unsigned char *buf, int start, int len)
  283. {
  284. int i;
  285. if (map_bankwidth_is_large(map)) {
  286. char *dest = (char *)&orig;
  287. memcpy(dest+start, buf, len);
  288. } else {
  289. for (i=start; i < start+len; i++) {
  290. int bitpos;
  291. #ifdef __LITTLE_ENDIAN
  292. bitpos = i*8;
  293. #else /* __BIG_ENDIAN */
  294. bitpos = (map_bankwidth(map)-1-i)*8;
  295. #endif
  296. orig.x[0] &= ~(0xff << bitpos);
  297. orig.x[0] |= buf[i-start] << bitpos;
  298. }
  299. }
  300. return orig;
  301. }
  302. #if BITS_PER_LONG < 64
  303. #define MAP_FF_LIMIT 4
  304. #else
  305. #define MAP_FF_LIMIT 8
  306. #endif
  307. static inline map_word map_word_ff(struct map_info *map)
  308. {
  309. map_word r;
  310. int i;
  311. if (map_bankwidth(map) < MAP_FF_LIMIT) {
  312. int bw = 8 * map_bankwidth(map);
  313. r.x[0] = (1 << bw) - 1;
  314. } else {
  315. for (i=0; i<map_words(map); i++)
  316. r.x[i] = ~0UL;
  317. }
  318. return r;
  319. }
  320. static inline map_word inline_map_read(struct map_info *map, unsigned long ofs)
  321. {
  322. map_word r;
  323. if (map_bankwidth_is_1(map))
  324. r.x[0] = __raw_readb(map->virt + ofs);
  325. else if (map_bankwidth_is_2(map))
  326. r.x[0] = __raw_readw(map->virt + ofs);
  327. else if (map_bankwidth_is_4(map))
  328. r.x[0] = __raw_readl(map->virt + ofs);
  329. #if BITS_PER_LONG >= 64
  330. else if (map_bankwidth_is_8(map))
  331. r.x[0] = __raw_readq(map->virt + ofs);
  332. #endif
  333. else if (map_bankwidth_is_large(map))
  334. memcpy_fromio(r.x, map->virt+ofs, map->bankwidth);
  335. return r;
  336. }
  337. static inline void inline_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
  338. {
  339. if (map_bankwidth_is_1(map))
  340. __raw_writeb(datum.x[0], map->virt + ofs);
  341. else if (map_bankwidth_is_2(map))
  342. __raw_writew(datum.x[0], map->virt + ofs);
  343. else if (map_bankwidth_is_4(map))
  344. __raw_writel(datum.x[0], map->virt + ofs);
  345. #if BITS_PER_LONG >= 64
  346. else if (map_bankwidth_is_8(map))
  347. __raw_writeq(datum.x[0], map->virt + ofs);
  348. #endif
  349. else if (map_bankwidth_is_large(map))
  350. memcpy_toio(map->virt+ofs, datum.x, map->bankwidth);
  351. mb();
  352. }
  353. static inline void inline_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
  354. {
  355. if (map->cached)
  356. memcpy(to, (char *)map->cached + from, len);
  357. else
  358. memcpy_fromio(to, map->virt + from, len);
  359. }
  360. static inline void inline_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
  361. {
  362. memcpy_toio(map->virt + to, from, len);
  363. }
  364. #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
  365. #define map_read(map, ofs) (map)->read(map, ofs)
  366. #define map_copy_from(map, to, from, len) (map)->copy_from(map, to, from, len)
  367. #define map_write(map, datum, ofs) (map)->write(map, datum, ofs)
  368. #define map_copy_to(map, to, from, len) (map)->copy_to(map, to, from, len)
  369. extern void simple_map_init(struct map_info *);
  370. #define map_is_linear(map) (map->phys != NO_XIP)
  371. #else
  372. #define map_read(map, ofs) inline_map_read(map, ofs)
  373. #define map_copy_from(map, to, from, len) inline_map_copy_from(map, to, from, len)
  374. #define map_write(map, datum, ofs) inline_map_write(map, datum, ofs)
  375. #define map_copy_to(map, to, from, len) inline_map_copy_to(map, to, from, len)
  376. #define simple_map_init(map) BUG_ON(!map_bankwidth_supported((map)->bankwidth))
  377. #define map_is_linear(map) ({ (void)(map); 1; })
  378. #endif /* !CONFIG_MTD_COMPLEX_MAPPINGS */
  379. #endif /* __LINUX_MTD_MAP_H__ */