Wolfgang Denk | 97caf67 | 2006-03-12 02:12:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * U-boot - bitops.h Routines for bit operations |
| 3 | * |
| 4 | * Copyright (c) 2005 blackfin.uclinux.org |
| 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., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #ifndef _BLACKFIN_BITOPS_H |
| 26 | #define _BLACKFIN_BITOPS_H |
| 27 | |
| 28 | /* |
| 29 | * Copyright 1992, Linus Torvalds. |
| 30 | */ |
| 31 | |
| 32 | #include <linux/config.h> |
| 33 | #include <asm/byteorder.h> |
| 34 | #include <asm/system.h> |
| 35 | |
| 36 | #ifdef __KERNEL__ |
| 37 | /* |
| 38 | * Function prototypes to keep gcc -Wall happy |
| 39 | */ |
| 40 | |
| 41 | /* |
| 42 | * The __ functions are not atomic |
| 43 | */ |
| 44 | |
| 45 | /* |
| 46 | * ffz = Find First Zero in word. Undefined if no zero exists, |
| 47 | * so code should check against ~0UL first.. |
| 48 | */ |
| 49 | static __inline__ unsigned long ffz(unsigned long word) |
| 50 | { |
| 51 | unsigned long result = 0; |
| 52 | |
| 53 | while (word & 1) { |
| 54 | result++; |
| 55 | word >>= 1; |
| 56 | } |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | static __inline__ void set_bit(int nr, volatile void *addr) |
| 61 | { |
| 62 | int *a = (int *) addr; |
| 63 | int mask; |
| 64 | unsigned long flags; |
| 65 | |
| 66 | a += nr >> 5; |
| 67 | mask = 1 << (nr & 0x1f); |
| 68 | save_and_cli(flags); |
| 69 | *a |= mask; |
| 70 | restore_flags(flags); |
| 71 | } |
| 72 | |
| 73 | static __inline__ void __set_bit(int nr, volatile void *addr) |
| 74 | { |
| 75 | int *a = (int *) addr; |
| 76 | int mask; |
| 77 | |
| 78 | a += nr >> 5; |
| 79 | mask = 1 << (nr & 0x1f); |
| 80 | *a |= mask; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * clear_bit() doesn't provide any barrier for the compiler. |
| 85 | */ |
| 86 | #define smp_mb__before_clear_bit() barrier() |
| 87 | #define smp_mb__after_clear_bit() barrier() |
| 88 | |
| 89 | static __inline__ void clear_bit(int nr, volatile void *addr) |
| 90 | { |
| 91 | int *a = (int *) addr; |
| 92 | int mask; |
| 93 | unsigned long flags; |
| 94 | |
| 95 | a += nr >> 5; |
| 96 | mask = 1 << (nr & 0x1f); |
| 97 | save_and_cli(flags); |
| 98 | *a &= ~mask; |
| 99 | restore_flags(flags); |
| 100 | } |
| 101 | |
| 102 | static __inline__ void change_bit(int nr, volatile void *addr) |
| 103 | { |
| 104 | int mask, flags; |
| 105 | unsigned long *ADDR = (unsigned long *) addr; |
| 106 | |
| 107 | ADDR += nr >> 5; |
| 108 | mask = 1 << (nr & 31); |
| 109 | save_and_cli(flags); |
| 110 | *ADDR ^= mask; |
| 111 | restore_flags(flags); |
| 112 | } |
| 113 | |
| 114 | static __inline__ void __change_bit(int nr, volatile void *addr) |
| 115 | { |
| 116 | int mask; |
| 117 | unsigned long *ADDR = (unsigned long *) addr; |
| 118 | |
| 119 | ADDR += nr >> 5; |
| 120 | mask = 1 << (nr & 31); |
| 121 | *ADDR ^= mask; |
| 122 | } |
| 123 | |
| 124 | static __inline__ int test_and_set_bit(int nr, volatile void *addr) |
| 125 | { |
| 126 | int mask, retval; |
| 127 | volatile unsigned int *a = (volatile unsigned int *) addr; |
| 128 | unsigned long flags; |
| 129 | |
| 130 | a += nr >> 5; |
| 131 | mask = 1 << (nr & 0x1f); |
| 132 | save_and_cli(flags); |
| 133 | retval = (mask & *a) != 0; |
| 134 | *a |= mask; |
| 135 | restore_flags(flags); |
| 136 | |
| 137 | return retval; |
| 138 | } |
| 139 | |
| 140 | static __inline__ int __test_and_set_bit(int nr, volatile void *addr) |
| 141 | { |
| 142 | int mask, retval; |
| 143 | volatile unsigned int *a = (volatile unsigned int *) addr; |
| 144 | |
| 145 | a += nr >> 5; |
| 146 | mask = 1 << (nr & 0x1f); |
| 147 | retval = (mask & *a) != 0; |
| 148 | *a |= mask; |
| 149 | return retval; |
| 150 | } |
| 151 | |
| 152 | static __inline__ int test_and_clear_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 | |
| 158 | a += nr >> 5; |
| 159 | mask = 1 << (nr & 0x1f); |
| 160 | save_and_cli(flags); |
| 161 | retval = (mask & *a) != 0; |
| 162 | *a &= ~mask; |
| 163 | restore_flags(flags); |
| 164 | |
| 165 | return retval; |
| 166 | } |
| 167 | |
| 168 | static __inline__ int __test_and_clear_bit(int nr, volatile void *addr) |
| 169 | { |
| 170 | int mask, retval; |
| 171 | volatile unsigned int *a = (volatile unsigned int *) addr; |
| 172 | |
| 173 | a += nr >> 5; |
| 174 | mask = 1 << (nr & 0x1f); |
| 175 | retval = (mask & *a) != 0; |
| 176 | *a &= ~mask; |
| 177 | return retval; |
| 178 | } |
| 179 | |
| 180 | static __inline__ int test_and_change_bit(int nr, volatile void *addr) |
| 181 | { |
| 182 | int mask, retval; |
| 183 | volatile unsigned int *a = (volatile unsigned int *) addr; |
| 184 | unsigned long flags; |
| 185 | |
| 186 | a += nr >> 5; |
| 187 | mask = 1 << (nr & 0x1f); |
| 188 | save_and_cli(flags); |
| 189 | retval = (mask & *a) != 0; |
| 190 | *a ^= mask; |
| 191 | restore_flags(flags); |
| 192 | |
| 193 | return retval; |
| 194 | } |
| 195 | |
| 196 | static __inline__ int __test_and_change_bit(int nr, volatile void *addr) |
| 197 | { |
| 198 | int mask, retval; |
| 199 | volatile unsigned int *a = (volatile unsigned int *) addr; |
| 200 | |
| 201 | a += nr >> 5; |
| 202 | mask = 1 << (nr & 0x1f); |
| 203 | retval = (mask & *a) != 0; |
| 204 | *a ^= mask; |
| 205 | return retval; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * This routine doesn't need to be atomic. |
| 210 | */ |
| 211 | static __inline__ int __constant_test_bit(int nr, |
| 212 | const volatile void *addr) |
| 213 | { |
| 214 | return ((1UL << (nr & 31)) & |
| 215 | (((const volatile unsigned int *) addr)[nr >> 5])) != 0; |
| 216 | } |
| 217 | |
| 218 | static __inline__ int __test_bit(int nr, volatile void *addr) |
| 219 | { |
| 220 | int *a = (int *) addr; |
| 221 | int mask; |
| 222 | |
| 223 | a += nr >> 5; |
| 224 | mask = 1 << (nr & 0x1f); |
| 225 | return ((mask & *a) != 0); |
| 226 | } |
| 227 | |
| 228 | #define test_bit(nr,addr) \ |
| 229 | (__builtin_constant_p(nr) ? \ |
| 230 | __constant_test_bit((nr),(addr)) : \ |
| 231 | __test_bit((nr),(addr))) |
| 232 | |
| 233 | #define find_first_zero_bit(addr, size) \ |
| 234 | find_next_zero_bit((addr), (size), 0) |
| 235 | |
| 236 | static __inline__ int find_next_zero_bit(void *addr, int size, int offset) |
| 237 | { |
| 238 | unsigned long *p = ((unsigned long *) addr) + (offset >> 5); |
| 239 | unsigned long result = offset & ~31UL; |
| 240 | unsigned long tmp; |
| 241 | |
| 242 | if (offset >= size) |
| 243 | return size; |
| 244 | size -= result; |
| 245 | offset &= 31UL; |
| 246 | if (offset) { |
| 247 | tmp = *(p++); |
| 248 | tmp |= ~0UL >> (32 - offset); |
| 249 | if (size < 32) |
| 250 | goto found_first; |
| 251 | if (~tmp) |
| 252 | goto found_middle; |
| 253 | size -= 32; |
| 254 | result += 32; |
| 255 | } |
| 256 | while (size & ~31UL) { |
| 257 | if (~(tmp = *(p++))) |
| 258 | goto found_middle; |
| 259 | result += 32; |
| 260 | size -= 32; |
| 261 | } |
| 262 | if (!size) |
| 263 | return result; |
| 264 | tmp = *p; |
| 265 | |
| 266 | found_first: |
| 267 | tmp |= ~0UL >> size; |
| 268 | found_middle: |
| 269 | return result + ffz(tmp); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * ffs: find first bit set. This is defined the same way as |
| 274 | * the libc and compiler builtin ffs routines, therefore |
| 275 | * differs in spirit from the above ffz (man ffs). |
| 276 | */ |
| 277 | |
| 278 | #define ffs(x) generic_ffs(x) |
| 279 | |
| 280 | /* |
| 281 | * hweightN: returns the hamming weight (i.e. the number |
| 282 | * of bits set) of a N-bit word |
| 283 | */ |
| 284 | |
| 285 | #define hweight32(x) generic_hweight32(x) |
| 286 | #define hweight16(x) generic_hweight16(x) |
| 287 | #define hweight8(x) generic_hweight8(x) |
| 288 | |
| 289 | static __inline__ int ext2_set_bit(int nr, volatile void *addr) |
| 290 | { |
| 291 | int mask, retval; |
| 292 | unsigned long flags; |
| 293 | volatile unsigned char *ADDR = (unsigned char *) addr; |
| 294 | |
| 295 | ADDR += nr >> 3; |
| 296 | mask = 1 << (nr & 0x07); |
| 297 | save_and_cli(flags); |
| 298 | retval = (mask & *ADDR) != 0; |
| 299 | *ADDR |= mask; |
| 300 | restore_flags(flags); |
| 301 | return retval; |
| 302 | } |
| 303 | |
| 304 | static __inline__ int ext2_clear_bit(int nr, volatile void *addr) |
| 305 | { |
| 306 | int mask, retval; |
| 307 | unsigned long flags; |
| 308 | volatile unsigned char *ADDR = (unsigned char *) addr; |
| 309 | |
| 310 | ADDR += nr >> 3; |
| 311 | mask = 1 << (nr & 0x07); |
| 312 | save_and_cli(flags); |
| 313 | retval = (mask & *ADDR) != 0; |
| 314 | *ADDR &= ~mask; |
| 315 | restore_flags(flags); |
| 316 | return retval; |
| 317 | } |
| 318 | |
| 319 | static __inline__ int ext2_test_bit(int nr, const volatile void *addr) |
| 320 | { |
| 321 | int mask; |
| 322 | const volatile unsigned char *ADDR = (const unsigned char *) addr; |
| 323 | |
| 324 | ADDR += nr >> 3; |
| 325 | mask = 1 << (nr & 0x07); |
| 326 | return ((mask & *ADDR) != 0); |
| 327 | } |
| 328 | |
| 329 | #define ext2_find_first_zero_bit(addr, size) \ |
| 330 | ext2_find_next_zero_bit((addr), (size), 0) |
| 331 | |
| 332 | static __inline__ unsigned long ext2_find_next_zero_bit(void *addr, |
| 333 | unsigned long size, |
| 334 | unsigned long |
| 335 | offset) |
| 336 | { |
| 337 | unsigned long *p = ((unsigned long *) addr) + (offset >> 5); |
| 338 | unsigned long result = offset & ~31UL; |
| 339 | unsigned long tmp; |
| 340 | |
| 341 | if (offset >= size) |
| 342 | return size; |
| 343 | size -= result; |
| 344 | offset &= 31UL; |
| 345 | if (offset) { |
| 346 | tmp = *(p++); |
| 347 | tmp |= ~0UL >> (32 - offset); |
| 348 | if (size < 32) |
| 349 | goto found_first; |
| 350 | if (~tmp) |
| 351 | goto found_middle; |
| 352 | size -= 32; |
| 353 | result += 32; |
| 354 | } |
| 355 | while (size & ~31UL) { |
| 356 | if (~(tmp = *(p++))) |
| 357 | goto found_middle; |
| 358 | result += 32; |
| 359 | size -= 32; |
| 360 | } |
| 361 | if (!size) |
| 362 | return result; |
| 363 | tmp = *p; |
| 364 | |
| 365 | found_first: |
| 366 | tmp |= ~0UL >> size; |
| 367 | found_middle: |
| 368 | return result + ffz(tmp); |
| 369 | } |
| 370 | |
| 371 | /* Bitmap functions for the minix filesystem. */ |
| 372 | #define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr) |
| 373 | #define minix_set_bit(nr,addr) set_bit(nr,addr) |
| 374 | #define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr) |
| 375 | #define minix_test_bit(nr,addr) test_bit(nr,addr) |
| 376 | #define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size) |
| 377 | |
| 378 | #endif |
| 379 | |
| 380 | #endif |