bitops.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * U-boot - bitops.h Routines for bit operations
  3. *
  4. * Copyright (c) 2005-2007 Analog Devices Inc.
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  22. * MA 02110-1301 USA
  23. */
  24. #ifndef _BLACKFIN_BITOPS_H
  25. #define _BLACKFIN_BITOPS_H
  26. /*
  27. * Copyright 1992, Linus Torvalds.
  28. */
  29. #include <linux/config.h>
  30. #include <asm/byteorder.h>
  31. #include <asm/system.h>
  32. #ifdef __KERNEL__
  33. /*
  34. * Function prototypes to keep gcc -Wall happy
  35. */
  36. /*
  37. * The __ functions are not atomic
  38. */
  39. /*
  40. * ffz = Find First Zero in word. Undefined if no zero exists,
  41. * so code should check against ~0UL first..
  42. */
  43. static __inline__ unsigned long ffz(unsigned long word)
  44. {
  45. unsigned long result = 0;
  46. while (word & 1) {
  47. result++;
  48. word >>= 1;
  49. }
  50. return result;
  51. }
  52. static __inline__ void set_bit(int nr, volatile void *addr)
  53. {
  54. int *a = (int *)addr;
  55. int mask;
  56. unsigned long flags;
  57. a += nr >> 5;
  58. mask = 1 << (nr & 0x1f);
  59. save_and_cli(flags);
  60. *a |= mask;
  61. restore_flags(flags);
  62. }
  63. static __inline__ void __set_bit(int nr, volatile void *addr)
  64. {
  65. int *a = (int *)addr;
  66. int mask;
  67. a += nr >> 5;
  68. mask = 1 << (nr & 0x1f);
  69. *a |= mask;
  70. }
  71. /*
  72. * clear_bit() doesn't provide any barrier for the compiler.
  73. */
  74. #define smp_mb__before_clear_bit() barrier()
  75. #define smp_mb__after_clear_bit() barrier()
  76. static __inline__ void clear_bit(int nr, volatile void *addr)
  77. {
  78. int *a = (int *)addr;
  79. int mask;
  80. unsigned long flags;
  81. a += nr >> 5;
  82. mask = 1 << (nr & 0x1f);
  83. save_and_cli(flags);
  84. *a &= ~mask;
  85. restore_flags(flags);
  86. }
  87. static __inline__ void change_bit(int nr, volatile void *addr)
  88. {
  89. int mask, flags;
  90. unsigned long *ADDR = (unsigned long *)addr;
  91. ADDR += nr >> 5;
  92. mask = 1 << (nr & 31);
  93. save_and_cli(flags);
  94. *ADDR ^= mask;
  95. restore_flags(flags);
  96. }
  97. static __inline__ void __change_bit(int nr, volatile void *addr)
  98. {
  99. int mask;
  100. unsigned long *ADDR = (unsigned long *)addr;
  101. ADDR += nr >> 5;
  102. mask = 1 << (nr & 31);
  103. *ADDR ^= mask;
  104. }
  105. static __inline__ int test_and_set_bit(int nr, volatile void *addr)
  106. {
  107. int mask, retval;
  108. volatile unsigned int *a = (volatile unsigned int *)addr;
  109. unsigned long flags;
  110. a += nr >> 5;
  111. mask = 1 << (nr & 0x1f);
  112. save_and_cli(flags);
  113. retval = (mask & *a) != 0;
  114. *a |= mask;
  115. restore_flags(flags);
  116. return retval;
  117. }
  118. static __inline__ int __test_and_set_bit(int nr, volatile void *addr)
  119. {
  120. int mask, retval;
  121. volatile unsigned int *a = (volatile unsigned int *)addr;
  122. a += nr >> 5;
  123. mask = 1 << (nr & 0x1f);
  124. retval = (mask & *a) != 0;
  125. *a |= mask;
  126. return retval;
  127. }
  128. static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
  129. {
  130. int mask, retval;
  131. volatile unsigned int *a = (volatile unsigned int *)addr;
  132. unsigned long flags;
  133. a += nr >> 5;
  134. mask = 1 << (nr & 0x1f);
  135. save_and_cli(flags);
  136. retval = (mask & *a) != 0;
  137. *a &= ~mask;
  138. restore_flags(flags);
  139. return retval;
  140. }
  141. static __inline__ int __test_and_clear_bit(int nr, volatile void *addr)
  142. {
  143. int mask, retval;
  144. volatile unsigned int *a = (volatile unsigned int *)addr;
  145. a += nr >> 5;
  146. mask = 1 << (nr & 0x1f);
  147. retval = (mask & *a) != 0;
  148. *a &= ~mask;
  149. return retval;
  150. }
  151. static __inline__ int test_and_change_bit(int nr, volatile void *addr)
  152. {
  153. int mask, retval;
  154. volatile unsigned int *a = (volatile unsigned int *)addr;
  155. unsigned long flags;
  156. a += nr >> 5;
  157. mask = 1 << (nr & 0x1f);
  158. save_and_cli(flags);
  159. retval = (mask & *a) != 0;
  160. *a ^= mask;
  161. restore_flags(flags);
  162. return retval;
  163. }
  164. static __inline__ int __test_and_change_bit(int nr, volatile void *addr)
  165. {
  166. int mask, retval;
  167. volatile unsigned int *a = (volatile unsigned int *)addr;
  168. a += nr >> 5;
  169. mask = 1 << (nr & 0x1f);
  170. retval = (mask & *a) != 0;
  171. *a ^= mask;
  172. return retval;
  173. }
  174. /*
  175. * This routine doesn't need to be atomic.
  176. */
  177. static __inline__ int __constant_test_bit(int nr, const volatile void *addr)
  178. {
  179. return ((1UL << (nr & 31)) &
  180. (((const volatile unsigned int *)addr)[nr >> 5])) != 0;
  181. }
  182. static __inline__ int __test_bit(int nr, volatile void *addr)
  183. {
  184. int *a = (int *)addr;
  185. int mask;
  186. a += nr >> 5;
  187. mask = 1 << (nr & 0x1f);
  188. return ((mask & *a) != 0);
  189. }
  190. #define test_bit(nr,addr) \
  191. (__builtin_constant_p(nr) ? \
  192. __constant_test_bit((nr),(addr)) : \
  193. __test_bit((nr),(addr)))
  194. #define find_first_zero_bit(addr, size) \
  195. find_next_zero_bit((addr), (size), 0)
  196. static __inline__ int find_next_zero_bit(void *addr, int size, int offset)
  197. {
  198. unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
  199. unsigned long result = offset & ~31UL;
  200. unsigned long tmp;
  201. if (offset >= size)
  202. return size;
  203. size -= result;
  204. offset &= 31UL;
  205. if (offset) {
  206. tmp = *(p++);
  207. tmp |= ~0UL >> (32 - offset);
  208. if (size < 32)
  209. goto found_first;
  210. if (~tmp)
  211. goto found_middle;
  212. size -= 32;
  213. result += 32;
  214. }
  215. while (size & ~31UL) {
  216. if (~(tmp = *(p++)))
  217. goto found_middle;
  218. result += 32;
  219. size -= 32;
  220. }
  221. if (!size)
  222. return result;
  223. tmp = *p;
  224. found_first:
  225. tmp |= ~0UL >> size;
  226. found_middle:
  227. return result + ffz(tmp);
  228. }
  229. /*
  230. * ffs: find first bit set. This is defined the same way as
  231. * the libc and compiler builtin ffs routines, therefore
  232. * differs in spirit from the above ffz (man ffs).
  233. */
  234. #define ffs(x) generic_ffs(x)
  235. /*
  236. * hweightN: returns the hamming weight (i.e. the number
  237. * of bits set) of a N-bit word
  238. */
  239. #define hweight32(x) generic_hweight32(x)
  240. #define hweight16(x) generic_hweight16(x)
  241. #define hweight8(x) generic_hweight8(x)
  242. static __inline__ int ext2_set_bit(int nr, volatile void *addr)
  243. {
  244. int mask, retval;
  245. unsigned long flags;
  246. volatile unsigned char *ADDR = (unsigned char *)addr;
  247. ADDR += nr >> 3;
  248. mask = 1 << (nr & 0x07);
  249. save_and_cli(flags);
  250. retval = (mask & *ADDR) != 0;
  251. *ADDR |= mask;
  252. restore_flags(flags);
  253. return retval;
  254. }
  255. static __inline__ int ext2_clear_bit(int nr, volatile void *addr)
  256. {
  257. int mask, retval;
  258. unsigned long flags;
  259. volatile unsigned char *ADDR = (unsigned char *)addr;
  260. ADDR += nr >> 3;
  261. mask = 1 << (nr & 0x07);
  262. save_and_cli(flags);
  263. retval = (mask & *ADDR) != 0;
  264. *ADDR &= ~mask;
  265. restore_flags(flags);
  266. return retval;
  267. }
  268. static __inline__ int ext2_test_bit(int nr, const volatile void *addr)
  269. {
  270. int mask;
  271. const volatile unsigned char *ADDR = (const unsigned char *)addr;
  272. ADDR += nr >> 3;
  273. mask = 1 << (nr & 0x07);
  274. return ((mask & *ADDR) != 0);
  275. }
  276. #define ext2_find_first_zero_bit(addr, size) \
  277. ext2_find_next_zero_bit((addr), (size), 0)
  278. static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
  279. unsigned long size,
  280. unsigned long offset)
  281. {
  282. unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
  283. unsigned long result = offset & ~31UL;
  284. unsigned long tmp;
  285. if (offset >= size)
  286. return size;
  287. size -= result;
  288. offset &= 31UL;
  289. if (offset) {
  290. tmp = *(p++);
  291. tmp |= ~0UL >> (32 - offset);
  292. if (size < 32)
  293. goto found_first;
  294. if (~tmp)
  295. goto found_middle;
  296. size -= 32;
  297. result += 32;
  298. }
  299. while (size & ~31UL) {
  300. if (~(tmp = *(p++)))
  301. goto found_middle;
  302. result += 32;
  303. size -= 32;
  304. }
  305. if (!size)
  306. return result;
  307. tmp = *p;
  308. found_first:
  309. tmp |= ~0UL >> size;
  310. found_middle:
  311. return result + ffz(tmp);
  312. }
  313. /* Bitmap functions for the minix filesystem. */
  314. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  315. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  316. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  317. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  318. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  319. #endif
  320. #endif