2 * U-boot - bitops.h Routines for bit operations
4 * Copyright (c) 2005-2007 Analog Devices Inc.
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 #ifndef _BLACKFIN_BITOPS_H
26 #define _BLACKFIN_BITOPS_H
29 * Copyright 1992, Linus Torvalds.
32 #include <linux/config.h>
33 #include <asm/byteorder.h>
34 #include <asm/system.h>
38 * Function prototypes to keep gcc -Wall happy
42 * The __ functions are not atomic
46 * ffz = Find First Zero in word. Undefined if no zero exists,
47 * so code should check against ~0UL first..
49 static __inline__
unsigned long ffz(unsigned long word
)
51 unsigned long result
= 0;
60 static __inline__
void set_bit(int nr
, volatile void *addr
)
67 mask
= 1 << (nr
& 0x1f);
68 local_irq_save(flags
);
70 local_irq_restore(flags
);
73 static __inline__
void __set_bit(int nr
, volatile void *addr
)
79 mask
= 1 << (nr
& 0x1f);
84 * clear_bit() doesn't provide any barrier for the compiler.
86 #define smp_mb__before_clear_bit() barrier()
87 #define smp_mb__after_clear_bit() barrier()
89 static __inline__
void clear_bit(int nr
, volatile void *addr
)
96 mask
= 1 << (nr
& 0x1f);
97 local_irq_save(flags
);
99 local_irq_restore(flags
);
102 static __inline__
void change_bit(int nr
, volatile void *addr
)
105 unsigned long *ADDR
= (unsigned long *)addr
;
108 mask
= 1 << (nr
& 31);
109 local_irq_save(flags
);
111 local_irq_restore(flags
);
114 static __inline__
void __change_bit(int nr
, volatile void *addr
)
117 unsigned long *ADDR
= (unsigned long *)addr
;
120 mask
= 1 << (nr
& 31);
124 static __inline__
int test_and_set_bit(int nr
, volatile void *addr
)
127 volatile unsigned int *a
= (volatile unsigned int *)addr
;
131 mask
= 1 << (nr
& 0x1f);
132 local_irq_save(flags
);
133 retval
= (mask
& *a
) != 0;
135 local_irq_restore(flags
);
140 static __inline__
int __test_and_set_bit(int nr
, volatile void *addr
)
143 volatile unsigned int *a
= (volatile unsigned int *)addr
;
146 mask
= 1 << (nr
& 0x1f);
147 retval
= (mask
& *a
) != 0;
152 static __inline__
int test_and_clear_bit(int nr
, volatile void *addr
)
155 volatile unsigned int *a
= (volatile unsigned int *)addr
;
159 mask
= 1 << (nr
& 0x1f);
160 local_irq_save(flags
);
161 retval
= (mask
& *a
) != 0;
163 local_irq_restore(flags
);
168 static __inline__
int __test_and_clear_bit(int nr
, volatile void *addr
)
171 volatile unsigned int *a
= (volatile unsigned int *)addr
;
174 mask
= 1 << (nr
& 0x1f);
175 retval
= (mask
& *a
) != 0;
180 static __inline__
int test_and_change_bit(int nr
, volatile void *addr
)
183 volatile unsigned int *a
= (volatile unsigned int *)addr
;
187 mask
= 1 << (nr
& 0x1f);
188 local_irq_save(flags
);
189 retval
= (mask
& *a
) != 0;
191 local_irq_restore(flags
);
196 static __inline__
int __test_and_change_bit(int nr
, volatile void *addr
)
199 volatile unsigned int *a
= (volatile unsigned int *)addr
;
202 mask
= 1 << (nr
& 0x1f);
203 retval
= (mask
& *a
) != 0;
209 * This routine doesn't need to be atomic.
211 static __inline__
int __constant_test_bit(int nr
, const volatile void *addr
)
213 return ((1UL << (nr
& 31)) &
214 (((const volatile unsigned int *)addr
)[nr
>> 5])) != 0;
217 static __inline__
int __test_bit(int nr
, volatile void *addr
)
219 int *a
= (int *)addr
;
223 mask
= 1 << (nr
& 0x1f);
224 return ((mask
& *a
) != 0);
227 #define test_bit(nr,addr) \
228 (__builtin_constant_p(nr) ? \
229 __constant_test_bit((nr),(addr)) : \
230 __test_bit((nr),(addr)))
232 #define find_first_zero_bit(addr, size) \
233 find_next_zero_bit((addr), (size), 0)
235 static __inline__
int find_next_zero_bit(void *addr
, int size
, int offset
)
237 unsigned long *p
= ((unsigned long *)addr
) + (offset
>> 5);
238 unsigned long result
= offset
& ~31UL;
247 tmp
|= ~0UL >> (32 - offset
);
255 while (size
& ~31UL) {
268 return result
+ ffz(tmp
);
272 * ffs: find first bit set. This is defined the same way as
273 * the libc and compiler builtin ffs routines, therefore
274 * differs in spirit from the above ffz (man ffs).
277 #define ffs(x) generic_ffs(x)
280 * hweightN: returns the hamming weight (i.e. the number
281 * of bits set) of a N-bit word
284 #define hweight32(x) generic_hweight32(x)
285 #define hweight16(x) generic_hweight16(x)
286 #define hweight8(x) generic_hweight8(x)
288 static __inline__
int ext2_set_bit(int nr
, volatile void *addr
)
292 volatile unsigned char *ADDR
= (unsigned char *)addr
;
295 mask
= 1 << (nr
& 0x07);
296 local_irq_save(flags
);
297 retval
= (mask
& *ADDR
) != 0;
299 local_irq_restore(flags
);
303 static __inline__
int ext2_clear_bit(int nr
, volatile void *addr
)
307 volatile unsigned char *ADDR
= (unsigned char *)addr
;
310 mask
= 1 << (nr
& 0x07);
311 local_irq_save(flags
);
312 retval
= (mask
& *ADDR
) != 0;
314 local_irq_restore(flags
);
318 static __inline__
int ext2_test_bit(int nr
, const volatile void *addr
)
321 const volatile unsigned char *ADDR
= (const unsigned char *)addr
;
324 mask
= 1 << (nr
& 0x07);
325 return ((mask
& *ADDR
) != 0);
328 #define ext2_find_first_zero_bit(addr, size) \
329 ext2_find_next_zero_bit((addr), (size), 0)
331 static __inline__
unsigned long ext2_find_next_zero_bit(void *addr
,
333 unsigned long offset
)
335 unsigned long *p
= ((unsigned long *)addr
) + (offset
>> 5);
336 unsigned long result
= offset
& ~31UL;
345 tmp
|= ~0UL >> (32 - offset
);
353 while (size
& ~31UL) {
366 return result
+ ffz(tmp
);
369 /* Bitmap functions for the minix filesystem. */
370 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
371 #define minix_set_bit(nr,addr) set_bit(nr,addr)
372 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
373 #define minix_test_bit(nr,addr) test_bit(nr,addr)
374 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)