Add et(4), which supports Agere ET1310 based Ethernet chips (PCIe only)
[dragonfly.git] / sys / dev / netif / et / bitops.h
1 /*
2  * Copyright (c) 2004, 2005 David Young.  All rights reserved.
3  *
4  * Programmed for NetBSD by David Young.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of David Young may not be used to endorse or promote
15  *    products derived from this software without specific prior
16  *    written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL David
22  * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
24  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29  * OF SUCH DAMAGE.
30  *
31  * $DragonFly: src/sys/dev/netif/et/Attic/bitops.h,v 1.1 2007/10/12 14:12:42 sephe Exp $
32  */
33
34 #ifndef _BITOPS_H
35 #define _BITOPS_H
36
37 /*
38  * __BIT(n): Return a bitmask with bit m set, where the least
39  *           significant bit is bit 0.
40  *
41  * __BITS(m, n): Return a bitmask with bits m through n, inclusive,
42  *               set.  It does not matter whether m>n or m<=n.  The
43  *               least significant bit is bit 0.
44  *
45  * A "bitfield" is a span of consecutive bits defined by a bitmask,
46  * where 1s select the bits in the bitfield.  __SHIFTIN, __SHIFTOUT,
47  * and SHIFTOUT_MASK help read and write bitfields from device registers.
48  *
49  * __SHIFTIN(v, mask): Left-shift bits `v' into the bitfield
50  *                     defined by `mask', and return them.  No
51  *                     side-effects.
52  *
53  * __SHIFTOUT(v, mask): Extract and return the bitfield selected
54  *                      by `mask' from `v', right-shifting the
55  *                      bits so that the rightmost selected bit
56  *                      is at bit 0.  No side-effects.
57  *
58  * __SHIFTOUT_MASK(mask): Right-shift the bits in `mask' so that
59  *                        the rightmost non-zero bit is at bit
60  *                        0.  This is useful for finding the
61  *                        greatest unsigned value that a bitfield
62  *                        can hold.  No side-effects.  Note that
63  *                        SHIFTOUT_MASK(m) = SHIFTOUT(m, m).
64  */
65
66 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
67 #define __BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n)))
68
69 /* __BITS(m, n): bits m through n, m < n. */
70 #define __BITS(__m, __n)        \
71         ((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))
72
73 /* Find least significant bit that is set */
74 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
75
76 #define __SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
77 #define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
78 #define __SHIFTOUT_MASK(__mask) __SHIFTOUT((__mask), (__mask))
79
80 #endif  /* !_BITOPS_H */