map.h 13 KB

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