2 * U-Boot - bitops.h Routines for bit operations
4 * Copyright (c) 2005-2007 Analog Devices Inc.
6 * SPDX-License-Identifier: GPL-2.0+
9 #ifndef _BLACKFIN_BITOPS_H
10 #define _BLACKFIN_BITOPS_H
13 * Copyright 1992, Linus Torvalds.
16 #include <asm/byteorder.h>
17 #include <asm/system.h>
18 #include <asm-generic/bitops/fls.h>
19 #include <asm-generic/bitops/__fls.h>
20 #include <asm-generic/bitops/fls64.h>
21 #include <asm-generic/bitops/__ffs.h>
25 * Function prototypes to keep gcc -Wall happy
29 * The __ functions are not atomic
33 * ffz = Find First Zero in word. Undefined if no zero exists,
34 * so code should check against ~0UL first..
36 static __inline__ unsigned long ffz(unsigned long word)
38 unsigned long result = 0;
47 static __inline__ void set_bit(int nr, volatile void *addr)
54 mask = 1 << (nr & 0x1f);
55 local_irq_save(flags);
57 local_irq_restore(flags);
60 static __inline__ void __set_bit(int nr, volatile void *addr)
66 mask = 1 << (nr & 0x1f);
69 #define PLATFORM__SET_BIT
72 * clear_bit() doesn't provide any barrier for the compiler.
74 #define smp_mb__before_clear_bit() barrier()
75 #define smp_mb__after_clear_bit() barrier()
77 static __inline__ void clear_bit(int nr, volatile void *addr)
84 mask = 1 << (nr & 0x1f);
85 local_irq_save(flags);
87 local_irq_restore(flags);
90 static __inline__ void change_bit(int nr, volatile void *addr)
93 unsigned long *ADDR = (unsigned long *)addr;
96 mask = 1 << (nr & 31);
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);
112 static __inline__ int test_and_set_bit(int nr, volatile void *addr)
115 volatile unsigned int *a = (volatile unsigned int *)addr;
119 mask = 1 << (nr & 0x1f);
120 local_irq_save(flags);
121 retval = (mask & *a) != 0;
123 local_irq_restore(flags);
128 static __inline__ int __test_and_set_bit(int nr, volatile void *addr)
131 volatile unsigned int *a = (volatile unsigned int *)addr;
134 mask = 1 << (nr & 0x1f);
135 retval = (mask & *a) != 0;
140 static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
143 volatile unsigned int *a = (volatile unsigned int *)addr;
147 mask = 1 << (nr & 0x1f);
148 local_irq_save(flags);
149 retval = (mask & *a) != 0;
151 local_irq_restore(flags);
156 static __inline__ int __test_and_clear_bit(int nr, volatile void *addr)
159 volatile unsigned int *a = (volatile unsigned int *)addr;
162 mask = 1 << (nr & 0x1f);
163 retval = (mask & *a) != 0;
168 static __inline__ int test_and_change_bit(int nr, volatile void *addr)
171 volatile unsigned int *a = (volatile unsigned int *)addr;
175 mask = 1 << (nr & 0x1f);
176 local_irq_save(flags);
177 retval = (mask & *a) != 0;
179 local_irq_restore(flags);
184 static __inline__ int __test_and_change_bit(int nr, volatile void *addr)
187 volatile unsigned int *a = (volatile unsigned int *)addr;
190 mask = 1 << (nr & 0x1f);
191 retval = (mask & *a) != 0;
197 * This routine doesn't need to be atomic.
199 static __inline__ int __constant_test_bit(int nr, const volatile void *addr)
201 return ((1UL << (nr & 31)) &
202 (((const volatile unsigned int *)addr)[nr >> 5])) != 0;
205 static __inline__ int __test_bit(int nr, volatile void *addr)
207 int *a = (int *)addr;
211 mask = 1 << (nr & 0x1f);
212 return ((mask & *a) != 0);
215 #define test_bit(nr,addr) \
216 (__builtin_constant_p(nr) ? \
217 __constant_test_bit((nr),(addr)) : \
218 __test_bit((nr),(addr)))
220 #define find_first_zero_bit(addr, size) \
221 find_next_zero_bit((addr), (size), 0)
223 static __inline__ int find_next_zero_bit(void *addr, int size, int offset)
225 unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
226 unsigned long result = offset & ~31UL;
235 tmp |= ~0UL >> (32 - offset);
243 while (size & ~31UL) {
256 return result + ffz(tmp);
260 * hweightN: returns the hamming weight (i.e. the number
261 * of bits set) of a N-bit word
264 #define hweight32(x) generic_hweight32(x)
265 #define hweight16(x) generic_hweight16(x)
266 #define hweight8(x) generic_hweight8(x)
268 static __inline__ int ext2_set_bit(int nr, volatile void *addr)
272 volatile unsigned char *ADDR = (unsigned char *)addr;
275 mask = 1 << (nr & 0x07);
276 local_irq_save(flags);
277 retval = (mask & *ADDR) != 0;
279 local_irq_restore(flags);
283 static __inline__ int ext2_clear_bit(int nr, volatile void *addr)
287 volatile unsigned char *ADDR = (unsigned char *)addr;
290 mask = 1 << (nr & 0x07);
291 local_irq_save(flags);
292 retval = (mask & *ADDR) != 0;
294 local_irq_restore(flags);
298 static __inline__ int ext2_test_bit(int nr, const volatile void *addr)
301 const volatile unsigned char *ADDR = (const unsigned char *)addr;
304 mask = 1 << (nr & 0x07);
305 return ((mask & *ADDR) != 0);
308 #define ext2_find_first_zero_bit(addr, size) \
309 ext2_find_next_zero_bit((addr), (size), 0)
311 static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
313 unsigned long offset)
315 unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
316 unsigned long result = offset & ~31UL;
325 tmp |= ~0UL >> (32 - offset);
333 while (size & ~31UL) {
346 return result + ffz(tmp);
349 /* Bitmap functions for the minix filesystem. */
350 #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
351 #define minix_set_bit(nr,addr) set_bit(nr,addr)
352 #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
353 #define minix_test_bit(nr,addr) test_bit(nr,addr)
354 #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)