map.h 13 KB

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