bitops.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. local_irq_save(flags);
  60. *a |= mask;
  61. local_irq_restore(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. #define PLATFORM__SET_BIT
  72. /*
  73. * clear_bit() doesn't provide any barrier for the compiler.
  74. */
  75. #define smp_mb__before_clear_bit() barrier()
  76. #define smp_mb__after_clear_bit() barrier()
  77. static __inline__ void clear_bit(int nr, volatile void *addr)
  78. {
  79. int *a = (int *)addr;
  80. int mask;
  81. unsigned long flags;
  82. a += nr >> 5;
  83. mask = 1 << (nr & 0x1f);
  84. local_irq_save(flags);
  85. *a &= ~mask;
  86. local_irq_restore(flags);
  87. }
  88. static __inline__ void change_bit(int nr, volatile void *addr)
  89. {
  90. int mask, flags;
  91. unsigned long *ADDR = (unsigned long *)addr;
  92. ADDR += nr >> 5;
  93. mask = 1 << (nr & 31);
  94. local_irq_save(flags);
  95. *ADDR ^= mask;
  96. local_irq_restore(flags);
  97. }
  98. static __inline__ void __change_bit(int nr, volatile void *addr)
  99. {
  100. int mask;
  101. unsigned long *ADDR = (unsigned long *)addr;
  102. ADDR += nr >> 5;
  103. mask = 1 << (nr & 31);
  104. *ADDR ^= mask;
  105. }
  106. static __inline__ int test_and_set_bit(int nr, volatile void *addr)
  107. {
  108. int mask, retval;
  109. volatile unsigned int *a = (volatile unsigned int *)addr;
  110. unsigned long flags;
  111. a += nr >> 5;
  112. mask = 1 << (nr & 0x1f);
  113. local_irq_save(flags);
  114. retval = (mask & *a) != 0;
  115. *a |= mask;
  116. local_irq_restore(flags);
  117. return retval;
  118. }
  119. static __inline__ int __test_and_set_bit(int nr, volatile void *addr)
  120. {
  121. int mask, retval;
  122. volatile unsigned int *a = (volatile unsigned int *)addr;
  123. a += nr >> 5;
  124. mask = 1 << (nr & 0x1f);
  125. retval = (mask & *a) != 0;
  126. *a |= mask;
  127. return retval;
  128. }
  129. static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
  130. {
  131. int mask, retval;
  132. volatile unsigned int *a = (volatile unsigned int *)addr;
  133. unsigned long flags;
  134. a += nr >> 5;
  135. mask = 1 << (nr & 0x1f);
  136. local_irq_save(flags);
  137. retval = (mask & *a) != 0;
  138. *a &= ~mask;
  139. local_irq_restore(flags);
  140. return retval;
  141. }
  142. static __inline__ int __test_and_clear_bit(int nr, volatile void *addr)
  143. {
  144. int mask, retval;
  145. volatile unsigned int *a = (volatile unsigned int *)addr;
  146. a += nr >> 5;
  147. mask = 1 << (nr & 0x1f);
  148. retval = (mask & *a) != 0;
  149. *a &= ~mask;
  150. return retval;
  151. }
  152. static __inline__ int test_and_change_bit(int nr, volatile void *addr)
  153. {
  154. int mask, retval;
  155. volatile unsigned int *a = (volatile unsigned int *)addr;
  156. unsigned long flags;
  157. a += nr >> 5;
  158. mask = 1 << (nr & 0x1f);
  159. local_irq_save(flags);
  160. retval = (mask & *a) != 0;
  161. *a ^= mask;
  162. local_irq_restore(flags);
  163. return retval;
  164. }
  165. static __inline__ int __test_and_change_bit(int nr, volatile void *addr)
  166. {
  167. int mask, retval;
  168. volatile unsigned int *a = (volatile unsigned int *)addr;
  169. a += nr >> 5;
  170. mask = 1 << (nr & 0x1f);
  171. retval = (mask & *a) != 0;
  172. *a ^= mask;
  173. return retval;
  174. }
  175. /*
  176. * This routine doesn't need to be atomic.
  177. */
  178. static __inline__ int __constant_test_bit(int nr, const volatile void *addr)
  179. {
  180. return ((1UL << (nr & 31)) &
  181. (((const volatile unsigned int *)addr)[nr >> 5])) != 0;
  182. }
  183. static __inline__ int __test_bit(int nr, volatile void *addr)
  184. {
  185. int *a = (int *)addr;
  186. int mask;
  187. a += nr >> 5;
  188. mask = 1 << (nr & 0x1f);
  189. return ((mask & *a) != 0);
  190. }
  191. #define test_bit(nr,addr) \
  192. (__builtin_constant_p(nr) ? \
  193. __constant_test_bit((nr),(addr)) : \
  194. __test_bit((nr),(addr)))
  195. #define find_first_zero_bit(addr, size) \
  196. find_next_zero_bit((addr), (size), 0)
  197. static __inline__ int find_next_zero_bit(void *addr, int size, int offset)
  198. {
  199. unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
  200. unsigned long result = offset & ~31UL;
  201. unsigned long tmp;
  202. if (offset >= size)
  203. return size;
  204. size -= result;
  205. offset &= 31UL;
  206. if (offset) {
  207. tmp = *(p++);
  208. tmp |= ~0UL >> (32 - offset);
  209. if (size < 32)
  210. goto found_first;
  211. if (~tmp)
  212. goto found_middle;
  213. size -= 32;
  214. result += 32;
  215. }
  216. while (size & ~31UL) {
  217. if (~(tmp = *(p++)))
  218. goto found_middle;
  219. result += 32;
  220. size -= 32;
  221. }
  222. if (!size)
  223. return result;
  224. tmp = *p;
  225. found_first:
  226. tmp |= ~0UL >> size;
  227. found_middle:
  228. return result + ffz(tmp);
  229. }
  230. /*
  231. * hweightN: returns the hamming weight (i.e. the number
  232. * of bits set) of a N-bit word
  233. */
  234. #define hweight32(x) generic_hweight32(x)
  235. #define hweight16(x) generic_hweight16(x)
  236. #define hweight8(x) generic_hweight8(x)
  237. static __inline__ int ext2_set_bit(int nr, volatile void *addr)
  238. {
  239. int mask, retval;
  240. unsigned long flags;
  241. volatile unsigned char *ADDR = (unsigned char *)addr;
  242. ADDR += nr >> 3;
  243. mask = 1 << (nr & 0x07);
  244. local_irq_save(flags);
  245. retval = (mask & *ADDR) != 0;
  246. *ADDR |= mask;
  247. local_irq_restore(flags);
  248. return retval;
  249. }
  250. static __inline__ int ext2_clear_bit(int nr, volatile void *addr)
  251. {
  252. int mask, retval;
  253. unsigned long flags;
  254. volatile unsigned char *ADDR = (unsigned char *)addr;
  255. ADDR += nr >> 3;
  256. mask = 1 << (nr & 0x07);
  257. local_irq_save(flags);
  258. retval = (mask & *ADDR) != 0;
  259. *ADDR &= ~mask;
  260. local_irq_restore(flags);
  261. return retval;
  262. }
  263. static __inline__ int ext2_test_bit(int nr, const volatile void *addr)
  264. {
  265. int mask;
  266. const volatile unsigned char *ADDR = (const unsigned char *)addr;
  267. ADDR += nr >> 3;
  268. mask = 1 << (nr & 0x07);
  269. return ((mask & *ADDR) != 0);
  270. }
  271. #define ext2_find_first_zero_bit(addr, size) \
  272. ext2_find_next_zero_bit((addr), (size), 0)
  273. static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
  274. unsigned long size,
  275. unsigned long offset)
  276. {
  277. unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
  278. unsigned long result = offset & ~31UL;
  279. unsigned long tmp;
  280. if (offset >= size)
  281. return size;
  282. size -= result;
  283. offset &= 31UL;
  284. if (offset) {
  285. tmp = *(p++);
  286. tmp |= ~0UL >> (32 - offset);
  287. if (size < 32)
  288. goto found_first;
  289. if (~tmp)
  290. goto found_middle;
  291. size -= 32;
  292. result += 32;
  293. }
  294. while (size & ~31UL) {
  295. if (~(tmp = *(p++)))
  296. goto found_middle;
  297. result += 32;
  298. size -= 32;
  299. }
  300. if (!size)
  301. return result;
  302. tmp = *p;
  303. found_first:
  304. tmp |= ~0UL >> size;
  305. found_middle:
  306. return result + ffz(tmp);
  307. }
  308. /* Bitmap functions for the minix filesystem. */
  309. #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
  310. #define minix_set_bit(nr,addr) set_bit(nr,addr)
  311. #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
  312. #define minix_test_bit(nr,addr) test_bit(nr,addr)
  313. #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)
  314. #endif
  315. #endif