map.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Overhauled routines for dealing with different mmap regions of flash */
  2. #ifndef __LINUX_MTD_MAP_H__
  3. #define __LINUX_MTD_MAP_H__
  4. #include <linux/types.h>
  5. #include <linux/list.h>
  6. #include <linux/string.h>
  7. #include <linux/mtd/compatmac.h>
  8. #include <asm/unaligned.h>
  9. #include <asm/system.h>
  10. #include <asm/io.h>
  11. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
  12. #define map_bankwidth(map) 1
  13. #define map_bankwidth_is_1(map) (map_bankwidth(map) == 1)
  14. #define map_bankwidth_is_large(map) (0)
  15. #define map_words(map) (1)
  16. #define MAX_MAP_BANKWIDTH 1
  17. #else
  18. #define map_bankwidth_is_1(map) (0)
  19. #endif
  20. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
  21. # ifdef map_bankwidth
  22. # undef map_bankwidth
  23. # define map_bankwidth(map) ((map)->bankwidth)
  24. # else
  25. # define map_bankwidth(map) 2
  26. # define map_bankwidth_is_large(map) (0)
  27. # define map_words(map) (1)
  28. # endif
  29. #define map_bankwidth_is_2(map) (map_bankwidth(map) == 2)
  30. #undef MAX_MAP_BANKWIDTH
  31. #define MAX_MAP_BANKWIDTH 2
  32. #else
  33. #define map_bankwidth_is_2(map) (0)
  34. #endif
  35. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
  36. # ifdef map_bankwidth
  37. # undef map_bankwidth
  38. # define map_bankwidth(map) ((map)->bankwidth)
  39. # else
  40. # define map_bankwidth(map) 4
  41. # define map_bankwidth_is_large(map) (0)
  42. # define map_words(map) (1)
  43. # endif
  44. #define map_bankwidth_is_4(map) (map_bankwidth(map) == 4)
  45. #undef MAX_MAP_BANKWIDTH
  46. #define MAX_MAP_BANKWIDTH 4
  47. #else
  48. #define map_bankwidth_is_4(map) (0)
  49. #endif
  50. /* ensure we never evaluate anything shorted than an unsigned long
  51. * to zero, and ensure we'll never miss the end of an comparison (bjd) */
  52. #define map_calc_words(map) ((map_bankwidth(map) + (sizeof(unsigned long)-1))/ sizeof(unsigned long))
  53. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
  54. # ifdef map_bankwidth
  55. # undef map_bankwidth
  56. # define map_bankwidth(map) ((map)->bankwidth)
  57. # if BITS_PER_LONG < 64
  58. # undef map_bankwidth_is_large
  59. # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
  60. # undef map_words
  61. # define map_words(map) map_calc_words(map)
  62. # endif
  63. # else
  64. # define map_bankwidth(map) 8
  65. # define map_bankwidth_is_large(map) (BITS_PER_LONG < 64)
  66. # define map_words(map) map_calc_words(map)
  67. # endif
  68. #define map_bankwidth_is_8(map) (map_bankwidth(map) == 8)
  69. #undef MAX_MAP_BANKWIDTH
  70. #define MAX_MAP_BANKWIDTH 8
  71. #else
  72. #define map_bankwidth_is_8(map) (0)
  73. #endif
  74. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
  75. # ifdef map_bankwidth
  76. # undef map_bankwidth
  77. # define map_bankwidth(map) ((map)->bankwidth)
  78. # undef map_bankwidth_is_large
  79. # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
  80. # undef map_words
  81. # define map_words(map) map_calc_words(map)
  82. # else
  83. # define map_bankwidth(map) 16
  84. # define map_bankwidth_is_large(map) (1)
  85. # define map_words(map) map_calc_words(map)
  86. # endif
  87. #define map_bankwidth_is_16(map) (map_bankwidth(map) == 16)
  88. #undef MAX_MAP_BANKWIDTH
  89. #define MAX_MAP_BANKWIDTH 16
  90. #else
  91. #define map_bankwidth_is_16(map) (0)
  92. #endif
  93. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
  94. # ifdef map_bankwidth
  95. # undef map_bankwidth
  96. # define map_bankwidth(map) ((map)->bankwidth)
  97. # undef map_bankwidth_is_large
  98. # define map_bankwidth_is_large(map) (map_bankwidth(map) > BITS_PER_LONG/8)
  99. # undef map_words
  100. # define map_words(map) map_calc_words(map)
  101. # else
  102. # define map_bankwidth(map) 32
  103. # define map_bankwidth_is_large(map) (1)
  104. # define map_words(map) map_calc_words(map)
  105. # endif
  106. #define map_bankwidth_is_32(map) (map_bankwidth(map) == 32)
  107. #undef MAX_MAP_BANKWIDTH
  108. #define MAX_MAP_BANKWIDTH 32
  109. #else
  110. #define map_bankwidth_is_32(map) (0)
  111. #endif
  112. #ifndef map_bankwidth
  113. #warning "No CONFIG_MTD_MAP_BANK_WIDTH_xx selected. No NOR chip support can work"
  114. static inline int map_bankwidth(void *map)
  115. {
  116. BUG();
  117. return 0;
  118. }
  119. #define map_bankwidth_is_large(map) (0)
  120. #define map_words(map) (0)
  121. #define MAX_MAP_BANKWIDTH 1
  122. #endif
  123. static inline int map_bankwidth_supported(int w)
  124. {
  125. switch (w) {
  126. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_1
  127. case 1:
  128. #endif
  129. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_2
  130. case 2:
  131. #endif
  132. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_4
  133. case 4:
  134. #endif
  135. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_8
  136. case 8:
  137. #endif
  138. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_16
  139. case 16:
  140. #endif
  141. #ifdef CONFIG_MTD_MAP_BANK_WIDTH_32
  142. case 32:
  143. #endif
  144. return 1;
  145. default:
  146. return 0;
  147. }
  148. }
  149. #define MAX_MAP_LONGS ( ((MAX_MAP_BANKWIDTH*8) + BITS_PER_LONG - 1) / BITS_PER_LONG )
  150. typedef union {
  151. unsigned long x[MAX_MAP_LONGS];
  152. } map_word;
  153. /* The map stuff is very simple. You fill in your struct map_info with
  154. a handful of routines for accessing the device, making sure they handle
  155. paging etc. correctly if your device needs it. Then you pass it off
  156. to a chip probe routine -- either JEDEC or CFI probe or both -- via
  157. do_map_probe(). If a chip is recognised, the probe code will invoke the
  158. appropriate chip driver (if present) and return a struct mtd_info.
  159. At which point, you fill in the mtd->module with your own module
  160. address, and register it with the MTD core code. Or you could partition
  161. it and register the partitions instead, or keep it for your own private
  162. use; whatever.
  163. The mtd->priv field will point to the struct map_info, and any further
  164. private data required by the chip driver is linked from the
  165. mtd->priv->fldrv_priv field. This allows the map driver to get at
  166. the destructor function map->fldrv_destroy() when it's tired
  167. of living.
  168. */
  169. struct map_info {
  170. const char *name;
  171. unsigned long size;
  172. resource_size_t phys;
  173. #define NO_XIP (-1UL)
  174. void __iomem *virt;
  175. void *cached;
  176. int bankwidth; /* in octets. This isn't necessarily the width
  177. of actual bus cycles -- it's the repeat interval
  178. in bytes, before you are talking to the first chip again.
  179. */
  180. #ifdef CONFIG_MTD_COMPLEX_MAPPINGS
  181. map_word (*read)(struct map_info *, unsigned long);
  182. void (*copy_from)(struct map_info *, void *, unsigned long, ssize_t);
  183. void (*write)(struct map_info *, const map_word, unsigned long);
  184. void (*copy_to)(struct map_info *, unsigned long, const void *, ssize_t);
  185. /* We can perhaps put in 'point' and 'unpoint' methods, if we really
  186. want to enable XIP for non-linear mappings. Not yet though. */
  187. #endif
  188. /* It's possible for the map driver to use cached memory in its
  189. copy_from implementation (and _only_ with copy_from). However,
  190. when the chip driver knows some flash area has changed contents,
  191. it will signal it to the map driver through this routine to let
  192. the map driver invalidate the corresponding cache as needed.
  193. If there is no cache to care about this can be set to NULL. */
  194. void (*inval_cache)(struct map_info *, unsigned long, ssize_t);
  195. /* set_vpp() must handle being reentered -- enable, enable, disable
  196. must leave it enabled. */
  197. void (*set_vpp)(struct map_info *, int);
  198. unsigned long pfow_base;
  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__ */