PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/include/linux/mtd/map.h

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