bitops.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #ifndef _H8300_BITOPS_H
  2. #define _H8300_BITOPS_H
  3. /*
  4. * Copyright 1992, Linus Torvalds.
  5. * Copyright 2002, Yoshinori Sato
  6. */
  7. #include <linux/compiler.h>
  8. #ifdef __KERNEL__
  9. #ifndef _LINUX_BITOPS_H
  10. #error only <linux/bitops.h> can be included directly
  11. #endif
  12. /*
  13. * Function prototypes to keep gcc -Wall happy
  14. */
  15. /*
  16. * ffz = Find First Zero in word. Undefined if no zero exists,
  17. * so code should check against ~0UL first..
  18. */
  19. static __inline__ unsigned long ffz(unsigned long word)
  20. {
  21. unsigned long result;
  22. result = -1;
  23. __asm__("1:\n\t"
  24. "shlr.l %2\n\t"
  25. "adds #1,%0\n\t"
  26. "bcs 1b"
  27. : "=r" (result)
  28. : "0" (result),"r" (word));
  29. return result;
  30. }
  31. #define H8300_GEN_BITOP_CONST(OP,BIT) \
  32. case BIT: \
  33. __asm__(OP " #" #BIT ",@%0"::"r"(b_addr):"memory"); \
  34. break;
  35. #define H8300_GEN_BITOP(FNAME,OP) \
  36. static __inline__ void FNAME(int nr, volatile unsigned long* addr) \
  37. { \
  38. volatile unsigned char *b_addr; \
  39. b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
  40. if (__builtin_constant_p(nr)) { \
  41. switch(nr & 7) { \
  42. H8300_GEN_BITOP_CONST(OP,0) \
  43. H8300_GEN_BITOP_CONST(OP,1) \
  44. H8300_GEN_BITOP_CONST(OP,2) \
  45. H8300_GEN_BITOP_CONST(OP,3) \
  46. H8300_GEN_BITOP_CONST(OP,4) \
  47. H8300_GEN_BITOP_CONST(OP,5) \
  48. H8300_GEN_BITOP_CONST(OP,6) \
  49. H8300_GEN_BITOP_CONST(OP,7) \
  50. } \
  51. } else { \
  52. __asm__(OP " %w0,@%1"::"r"(nr),"r"(b_addr):"memory"); \
  53. } \
  54. }
  55. /*
  56. * clear_bit() doesn't provide any barrier for the compiler.
  57. */
  58. #define smp_mb__before_clear_bit() barrier()
  59. #define smp_mb__after_clear_bit() barrier()
  60. H8300_GEN_BITOP(set_bit ,"bset")
  61. H8300_GEN_BITOP(clear_bit ,"bclr")
  62. H8300_GEN_BITOP(change_bit,"bnot")
  63. #define __set_bit(nr,addr) set_bit((nr),(addr))
  64. #define __clear_bit(nr,addr) clear_bit((nr),(addr))
  65. #define __change_bit(nr,addr) change_bit((nr),(addr))
  66. #undef H8300_GEN_BITOP
  67. #undef H8300_GEN_BITOP_CONST
  68. static __inline__ int test_bit(int nr, const unsigned long* addr)
  69. {
  70. return (*((volatile unsigned char *)addr +
  71. ((nr >> 3) ^ 3)) & (1UL << (nr & 7))) != 0;
  72. }
  73. #define __test_bit(nr, addr) test_bit(nr, addr)
  74. #define H8300_GEN_TEST_BITOP_CONST_INT(OP,BIT) \
  75. case BIT: \
  76. __asm__("stc ccr,%w1\n\t" \
  77. "orc #0x80,ccr\n\t" \
  78. "bld #" #BIT ",@%4\n\t" \
  79. OP " #" #BIT ",@%4\n\t" \
  80. "rotxl.l %0\n\t" \
  81. "ldc %w1,ccr" \
  82. : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
  83. : "0" (retval),"r" (b_addr) \
  84. : "memory"); \
  85. break;
  86. #define H8300_GEN_TEST_BITOP_CONST(OP,BIT) \
  87. case BIT: \
  88. __asm__("bld #" #BIT ",@%3\n\t" \
  89. OP " #" #BIT ",@%3\n\t" \
  90. "rotxl.l %0\n\t" \
  91. : "=r"(retval),"=m"(*b_addr) \
  92. : "0" (retval),"r" (b_addr) \
  93. : "memory"); \
  94. break;
  95. #define H8300_GEN_TEST_BITOP(FNNAME,OP) \
  96. static __inline__ int FNNAME(int nr, volatile void * addr) \
  97. { \
  98. int retval = 0; \
  99. char ccrsave; \
  100. volatile unsigned char *b_addr; \
  101. b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
  102. if (__builtin_constant_p(nr)) { \
  103. switch(nr & 7) { \
  104. H8300_GEN_TEST_BITOP_CONST_INT(OP,0) \
  105. H8300_GEN_TEST_BITOP_CONST_INT(OP,1) \
  106. H8300_GEN_TEST_BITOP_CONST_INT(OP,2) \
  107. H8300_GEN_TEST_BITOP_CONST_INT(OP,3) \
  108. H8300_GEN_TEST_BITOP_CONST_INT(OP,4) \
  109. H8300_GEN_TEST_BITOP_CONST_INT(OP,5) \
  110. H8300_GEN_TEST_BITOP_CONST_INT(OP,6) \
  111. H8300_GEN_TEST_BITOP_CONST_INT(OP,7) \
  112. } \
  113. } else { \
  114. __asm__("stc ccr,%w1\n\t" \
  115. "orc #0x80,ccr\n\t" \
  116. "btst %w5,@%4\n\t" \
  117. OP " %w5,@%4\n\t" \
  118. "beq 1f\n\t" \
  119. "inc.l #1,%0\n" \
  120. "1:\n\t" \
  121. "ldc %w1,ccr" \
  122. : "=r"(retval),"=&r"(ccrsave),"=m"(*b_addr) \
  123. : "0" (retval),"r" (b_addr),"r"(nr) \
  124. : "memory"); \
  125. } \
  126. return retval; \
  127. } \
  128. \
  129. static __inline__ int __ ## FNNAME(int nr, volatile void * addr) \
  130. { \
  131. int retval = 0; \
  132. volatile unsigned char *b_addr; \
  133. b_addr = (volatile unsigned char *)addr + ((nr >> 3) ^ 3); \
  134. if (__builtin_constant_p(nr)) { \
  135. switch(nr & 7) { \
  136. H8300_GEN_TEST_BITOP_CONST(OP,0) \
  137. H8300_GEN_TEST_BITOP_CONST(OP,1) \
  138. H8300_GEN_TEST_BITOP_CONST(OP,2) \
  139. H8300_GEN_TEST_BITOP_CONST(OP,3) \
  140. H8300_GEN_TEST_BITOP_CONST(OP,4) \
  141. H8300_GEN_TEST_BITOP_CONST(OP,5) \
  142. H8300_GEN_TEST_BITOP_CONST(OP,6) \
  143. H8300_GEN_TEST_BITOP_CONST(OP,7) \
  144. } \
  145. } else { \
  146. __asm__("btst %w4,@%3\n\t" \
  147. OP " %w4,@%3\n\t" \
  148. "beq 1f\n\t" \
  149. "inc.l #1,%0\n" \
  150. "1:" \
  151. : "=r"(retval),"=m"(*b_addr) \
  152. : "0" (retval),"r" (b_addr),"r"(nr) \
  153. : "memory"); \
  154. } \
  155. return retval; \
  156. }
  157. H8300_GEN_TEST_BITOP(test_and_set_bit, "bset")
  158. H8300_GEN_TEST_BITOP(test_and_clear_bit, "bclr")
  159. H8300_GEN_TEST_BITOP(test_and_change_bit,"bnot")
  160. #undef H8300_GEN_TEST_BITOP_CONST
  161. #undef H8300_GEN_TEST_BITOP_CONST_INT
  162. #undef H8300_GEN_TEST_BITOP
  163. #include <asm-generic/bitops/ffs.h>
  164. static __inline__ unsigned long __ffs(unsigned long word)
  165. {
  166. unsigned long result;
  167. result = -1;
  168. __asm__("1:\n\t"
  169. "shlr.l %2\n\t"
  170. "adds #1,%0\n\t"
  171. "bcc 1b"
  172. : "=r" (result)
  173. : "0"(result),"r"(word));
  174. return result;
  175. }
  176. #include <asm-generic/bitops/find.h>
  177. #include <asm-generic/bitops/sched.h>
  178. #include <asm-generic/bitops/hweight.h>
  179. #include <asm-generic/bitops/lock.h>
  180. #include <asm-generic/bitops/le.h>
  181. #include <asm-generic/bitops/ext2-atomic.h>
  182. #endif /* __KERNEL__ */
  183. #include <asm-generic/bitops/fls.h>
  184. #include <asm-generic/bitops/__fls.h>
  185. #include <asm-generic/bitops/fls64.h>
  186. #endif /* _H8300_BITOPS_H */