Cleanup of config/includes.
[fw/openocd] / src / helper / bits.h
1 /*
2  * Copyright (C) 2018, STMicroelectronics - All Rights Reserved
3  * Author(s): Antonio Borneo <borneo.antonio@gmail.com> for STMicroelectronics
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /*
20  * The content of this file is mainly copied/inspired from Linux kernel
21  * code in include/linux/types.h include/linux/bitmap.h include/linux/bitops.h
22  */
23
24 #ifndef OPENOCD_HELPER_BITS_H
25 #define OPENOCD_HELPER_BITS_H
26
27 #include <helper/replacements.h>
28 #include <helper/types.h>
29
30 #define BIT(nr)                     (1UL << (nr))
31 #define BITS_PER_BYTE               8
32 #define BITS_PER_LONG               (BITS_PER_BYTE * sizeof(long))
33 #define BITS_TO_LONGS(nr)           DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
34 #define BIT_MASK(nr)                (1UL << ((nr) % BITS_PER_LONG))
35 #define BIT_WORD(nr)                ((nr) / BITS_PER_LONG)
36 #define DECLARE_BITMAP(name, bits)  unsigned long name[BITS_TO_LONGS(bits)]
37
38 /**
39  * bitmap_zero - Clears all the bits in memory
40  * @param dst the address of the bitmap
41  * @param nbits the number of bits to clear
42  */
43 static inline void bitmap_zero(unsigned long *dst, unsigned int nbits)
44 {
45         unsigned int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
46         memset(dst, 0, len);
47 }
48
49 /**
50  * clear_bit - Clear a bit in memory
51  * @param nr the bit to set
52  * @param addr the address to start counting from
53  */
54 static inline void clear_bit(unsigned int nr, volatile unsigned long *addr)
55 {
56         unsigned long mask = BIT_MASK(nr);
57         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
58
59         *p &= ~mask;
60 }
61
62 /**
63  * set_bit - Set a bit in memory
64  * @param nr the bit to set
65  * @param addr the address to start counting from
66  */
67 static inline void set_bit(unsigned int nr, volatile unsigned long *addr)
68 {
69         unsigned long mask = BIT_MASK(nr);
70         unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
71
72         *p |= mask;
73 }
74
75 /**
76  * test_bit - Determine whether a bit is set
77  * @param nr bit number to test
78  * @param addr Address to start counting from
79  */
80 static inline int test_bit(unsigned int nr, const volatile unsigned long *addr)
81 {
82         return 1UL & (addr[BIT_WORD(nr)] >> (nr % BITS_PER_LONG));
83 }
84
85 #endif /* OPENOCD_HELPER_BITS_H */