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);
82
#define PLATFORM__SET_BIT
85
* clear_bit() doesn't provide any barrier for the compiler.
87
#define smp_mb__before_clear_bit() barrier()
88
#define smp_mb__after_clear_bit() barrier()
90
static __inline__ void clear_bit(int nr, volatile void *addr)
97
mask = 1 << (nr & 0x1f);
98
local_irq_save(flags);
100
local_irq_restore(flags);
103
static __inline__ void change_bit(int nr, volatile void *addr)
106
unsigned long *ADDR = (unsigned long *)addr;
109
mask = 1 << (nr & 31);
110
local_irq_save(flags);
112
local_irq_restore(flags);
115
static __inline__ void __change_bit(int nr, volatile void *addr)
118
unsigned long *ADDR = (unsigned long *)addr;
121
mask = 1 << (nr & 31);
125
static __inline__ int test_and_set_bit(int nr, volatile void *addr)
128
volatile unsigned int *a = (volatile unsigned int *)addr;
132
mask = 1 << (nr & 0x1f);
133
local_irq_save(flags);
134
retval = (mask & *a) != 0;
136
local_irq_restore(flags);
141
static __inline__ int __test_and_set_bit(int nr, volatile void *addr)
144
volatile unsigned int *a = (volatile unsigned int *)addr;
147
mask = 1 << (nr & 0x1f);
148
retval = (mask & *a) != 0;
153
static __inline__ int test_and_clear_bit(int nr, volatile void *addr)
156
volatile unsigned int *a = (volatile unsigned int *)addr;
160
mask = 1 << (nr & 0x1f);
161
local_irq_save(flags);
162
retval = (mask & *a) != 0;
164
local_irq_restore(flags);
169
static __inline__ int __test_and_clear_bit(int nr, volatile void *addr)
172
volatile unsigned int *a = (volatile unsigned int *)addr;
175
mask = 1 << (nr & 0x1f);
176
retval = (mask & *a) != 0;
181
static __inline__ int test_and_change_bit(int nr, volatile void *addr)
184
volatile unsigned int *a = (volatile unsigned int *)addr;
188
mask = 1 << (nr & 0x1f);
189
local_irq_save(flags);
190
retval = (mask & *a) != 0;
192
local_irq_restore(flags);
197
static __inline__ int __test_and_change_bit(int nr, volatile void *addr)
200
volatile unsigned int *a = (volatile unsigned int *)addr;
203
mask = 1 << (nr & 0x1f);
204
retval = (mask & *a) != 0;
210
* This routine doesn't need to be atomic.
212
static __inline__ int __constant_test_bit(int nr, const volatile void *addr)
214
return ((1UL << (nr & 31)) &
215
(((const volatile unsigned int *)addr)[nr >> 5])) != 0;
218
static __inline__ int __test_bit(int nr, volatile void *addr)
220
int *a = (int *)addr;
224
mask = 1 << (nr & 0x1f);
225
return ((mask & *a) != 0);
228
#define test_bit(nr,addr) \
229
(__builtin_constant_p(nr) ? \
230
__constant_test_bit((nr),(addr)) : \
231
__test_bit((nr),(addr)))
233
#define find_first_zero_bit(addr, size) \
234
find_next_zero_bit((addr), (size), 0)
236
static __inline__ int find_next_zero_bit(void *addr, int size, int offset)
238
unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
239
unsigned long result = offset & ~31UL;
248
tmp |= ~0UL >> (32 - offset);
256
while (size & ~31UL) {
269
return result + ffz(tmp);
273
* hweightN: returns the hamming weight (i.e. the number
274
* of bits set) of a N-bit word
277
#define hweight32(x) generic_hweight32(x)
278
#define hweight16(x) generic_hweight16(x)
279
#define hweight8(x) generic_hweight8(x)
281
static __inline__ int ext2_set_bit(int nr, volatile void *addr)
285
volatile unsigned char *ADDR = (unsigned char *)addr;
288
mask = 1 << (nr & 0x07);
289
local_irq_save(flags);
290
retval = (mask & *ADDR) != 0;
292
local_irq_restore(flags);
296
static __inline__ int ext2_clear_bit(int nr, volatile void *addr)
300
volatile unsigned char *ADDR = (unsigned char *)addr;
303
mask = 1 << (nr & 0x07);
304
local_irq_save(flags);
305
retval = (mask & *ADDR) != 0;
307
local_irq_restore(flags);
311
static __inline__ int ext2_test_bit(int nr, const volatile void *addr)
314
const volatile unsigned char *ADDR = (const unsigned char *)addr;
317
mask = 1 << (nr & 0x07);
318
return ((mask & *ADDR) != 0);
321
#define ext2_find_first_zero_bit(addr, size) \
322
ext2_find_next_zero_bit((addr), (size), 0)
324
static __inline__ unsigned long ext2_find_next_zero_bit(void *addr,
326
unsigned long offset)
328
unsigned long *p = ((unsigned long *)addr) + (offset >> 5);
329
unsigned long result = offset & ~31UL;
338
tmp |= ~0UL >> (32 - offset);
346
while (size & ~31UL) {
359
return result + ffz(tmp);
362
/* Bitmap functions for the minix filesystem. */
363
#define minix_test_and_set_bit(nr,addr) test_and_set_bit(nr,addr)
364
#define minix_set_bit(nr,addr) set_bit(nr,addr)
365
#define minix_test_and_clear_bit(nr,addr) test_and_clear_bit(nr,addr)
366
#define minix_test_bit(nr,addr) test_bit(nr,addr)
367
#define minix_find_first_zero_bit(addr,size) find_first_zero_bit(addr,size)