Merge tag 'u-boot-atmel-fixes-2019.07-a' of git://git.denx.de/u-boot-atmel
[oweals/u-boot.git] / arch / arm / include / asm / bitops.h
index 597dafbf9d26fadcb27dce69cf03df2dd08ca44b..b02c454d04c14ae755b38016ad067040be557c0a 100644 (file)
@@ -15,6 +15,8 @@
 #ifndef __ASM_ARM_BITOPS_H
 #define __ASM_ARM_BITOPS_H
 
+#include <asm-generic/bitops/__ffs.h>
+
 #ifdef __KERNEL__
 
 #include <asm/proc-armv/system.h>
@@ -51,7 +53,7 @@ static inline int __test_and_set_bit(int nr, volatile void *addr)
 
 static inline int test_and_set_bit(int nr, volatile void * addr)
 {
-       unsigned long flags;
+       unsigned long flags = 0;
        int out;
 
        local_irq_save(flags);
@@ -73,7 +75,7 @@ static inline int __test_and_clear_bit(int nr, volatile void *addr)
 
 static inline int test_and_clear_bit(int nr, volatile void * addr)
 {
-       unsigned long flags;
+       unsigned long flags = 0;
        int out;
 
        local_irq_save(flags);
@@ -95,9 +97,6 @@ static inline int __test_and_change_bit(int nr, volatile void *addr)
        return (old & mask) != 0;
 }
 
-extern int find_first_zero_bit(void * addr, unsigned size);
-extern int find_next_zero_bit(void * addr, int size, int offset);
-
 /*
  * This routine doesn't need to be atomic.
  */
@@ -111,22 +110,43 @@ static inline int __ilog2(unsigned int x)
        return generic_fls(x) - 1;
 }
 
-/*
- * ffz = Find First Zero in word. Undefined if no zero exists,
- * so code should check against ~0UL first..
- */
-static inline unsigned long ffz(unsigned long word)
+#define ffz(x)  __ffs(~(x))
+
+static inline int find_next_zero_bit(void *addr, int size, int offset)
 {
-       int k;
-
-       word = ~word;
-       k = 31;
-       if (word & 0x0000ffff) { k -= 16; word <<= 16; }
-       if (word & 0x00ff0000) { k -= 8;  word <<= 8;  }
-       if (word & 0x0f000000) { k -= 4;  word <<= 4;  }
-       if (word & 0x30000000) { k -= 2;  word <<= 2;  }
-       if (word & 0x40000000) { k -= 1; }
-       return k;
+       unsigned long *p = ((unsigned long *)addr) + (offset / BITS_PER_LONG);
+       unsigned long result = offset & ~(BITS_PER_LONG - 1);
+       unsigned long tmp;
+
+       if (offset >= size)
+               return size;
+       size -= result;
+       offset &= (BITS_PER_LONG - 1);
+       if (offset) {
+               tmp = *(p++);
+               tmp |= ~0UL >> (BITS_PER_LONG - offset);
+               if (size < BITS_PER_LONG)
+                       goto found_first;
+               if (~tmp)
+                       goto found_middle;
+               size -= BITS_PER_LONG;
+               result += BITS_PER_LONG;
+       }
+       while (size & ~(BITS_PER_LONG - 1)) {
+               tmp = *(p++);
+               if (~tmp)
+                       goto found_middle;
+               result += BITS_PER_LONG;
+               size -= BITS_PER_LONG;
+       }
+       if (!size)
+               return result;
+       tmp = *p;
+
+found_first:
+       tmp |= ~0UL << size;
+found_middle:
+       return result + ffz(tmp);
 }
 
 /*
@@ -138,6 +158,9 @@ static inline unsigned long ffz(unsigned long word)
 #define hweight16(x) generic_hweight16(x)
 #define hweight8(x) generic_hweight8(x)
 
+#define find_first_zero_bit(addr, size) \
+       find_next_zero_bit((addr), (size), 0)
+
 #define ext2_set_bit                   test_and_set_bit
 #define ext2_clear_bit                 test_and_clear_bit
 #define ext2_test_bit                  test_bit
@@ -153,4 +176,8 @@ static inline unsigned long ffz(unsigned long word)
 
 #endif /* __KERNEL__ */
 
+#include <asm-generic/bitops/__fls.h>
+#include <asm-generic/bitops/fls.h>
+#include <asm-generic/bitops/fls64.h>
+
 #endif /* _ARM_BITOPS_H */