1 /*
2  * Copyright 2024 Microchip Technology Inc. and its subsidiaries.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef _MEC_DEFS_H
7 #define _MEC_DEFS_H
8 
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 /* General Constants */
14 
15 #define BIT_n_MASK(n)   (1U << (n))
16 #define BIT_0_MASK  (1U << 0)
17 #define BIT_1_MASK  (1U << 1)
18 #define BIT_2_MASK  (1U << 2)
19 #define BIT_3_MASK  (1U << 3)
20 #define BIT_4_MASK  (1U << 4)
21 #define BIT_5_MASK  (1U << 5)
22 #define BIT_6_MASK  (1U << 6)
23 #define BIT_7_MASK  (1U << 7)
24 #define BIT_8_MASK  (1U << 8)
25 #define BIT_9_MASK  (1U << 9)
26 #define BIT_10_MASK (1U << 10)
27 #define BIT_11_MASK (1U << 11)
28 #define BIT_12_MASK (1U << 12)
29 #define BIT_13_MASK (1U << 13)
30 #define BIT_14_MASK (1U << 14)
31 #define BIT_15_MASK (1U << 15)
32 #define BIT_16_MASK (1U << 16)
33 #define BIT_17_MASK (1U << 17)
34 #define BIT_18_MASK (1U << 18)
35 #define BIT_19_MASK (1U << 19)
36 #define BIT_20_MASK (1U << 20)
37 #define BIT_21_MASK (1U << 21)
38 #define BIT_22_MASK (1U << 22)
39 #define BIT_23_MASK (1U << 23)
40 #define BIT_24_MASK (1U << 24)
41 #define BIT_25_MASK (1U << 25)
42 #define BIT_26_MASK (1U << 26)
43 #define BIT_27_MASK (1U << 27)
44 #define BIT_28_MASK (1U << 28)
45 #define BIT_29_MASK (1U << 29)
46 #define BIT_30_MASK (1U << 30)
47 #define BIT_31_MASK (1U << 31)
48 
49 #ifndef MEC_BIT
50 #define MEC_BIT(n)    (1ul << (n))
51 #endif
52 
53 #ifndef MEC_BIT32
54 #define MEC_BIT32(n)  (1ul << (n))
55 #endif
56 
57 #ifndef MEC_BIT64
58 #define MEC_BIT64(n)  (1ULL << (n))
59 #endif
60 
61 #ifndef MEC_BIT_SET
62 #define MEC_BIT_SET(d, pos) ((d) |= BIT(pos))
63 #endif
64 
65 #ifndef MEC_BIT_CLR
66 #define MEC_BIT_CLR(d, pos) ((d) &= ~BIT(pos))
67 #endif
68 
69 #ifndef MEC_LSHFT
70 #define MEC_LSHFT(v, s) (((uint32_t)(v)) << ((s) & 0x1Fu))
71 #endif
72 
73 #ifndef MEC_FIELD_VAL
74 #define MEC_FIELD_VAL(val, pos) ((uint32_t)(val) << (pos))
75 #endif
76 
77 #ifndef MEC_IS_PTR_ALIGNED16
78 #define MEC_IS_PTR_ALIGNED16(ptr) ((((uintptr_t)(ptr)) & 0x01U) == 0)
79 #endif
80 
81 #ifndef MEC_IS_PTR_ALIGNED32
82 #define MEC_IS_PTR_ALIGNED32(ptr) ((((uintptr_t)(ptr)) & 0x03U) == 0)
83 #endif
84 
85 #ifndef MEC_IS_PTR_ALIGNED64
86 #define MEC_IS_PTR_ALIGNED64(ptr) ((((uintptr_t)(ptr)) & 0x07U) == 0)
87 #endif
88 
89 #ifndef MEC_IS_PTR_ALIGNED128
90 #define MEC_IS_PTR_ALIGNED128(ptr) ((((uintptr_t)(ptr)) & 0x0fU) == 0)
91 #endif
92 
93 #ifndef MEC_IS_PTR_ALIGNED4K
94 #define MEC_IS_PTR_ALIGNED4K(ptr) ((((uintptr_t)(ptr)) & 0xfffU) == 0)
95 #endif
96 
97 /* Align pointer in memory region. NOTE: memory region must be large
98  * enough to move pointer forward to an aligned address.
99  */
100 #ifndef MEC_PTR_ALIGN4
101 #define MEC_PTR_ALIGN4(ptr)     (((uintptr_t)(ptr) + 4U) & ~0x3u)
102 #endif
103 
104 #ifndef MEC_PTR_ALIGN8
105 #define MEC_PTR_ALIGN8(ptr)     (((uintptr_t)(ptr) + 8U) & ~0x7u)
106 #endif
107 
108 #ifndef MEC_PTR_ALIGN16
109 #define MEC_PTR_ALIGN16(ptr)    (((uintptr_t)(ptr) + 16U) & ~0xFu)
110 #endif
111 
112 #ifndef MEC_PTR_ALIGN4K
113 #define MEC_PTR_ALIGN4K(ptr)    (((uintptr_t)(ptr) + 0x1000U) & ~0xFFFu)
114 #endif
115 
116 #define MEC_MMCR8(a)  *(volatile uint8_t *)(a)
117 #define MEC_MMCR16(a) *(volatile uint16_t *)(a)
118 #define MEC_MMCR32(a) *(volatile uint32_t *)(a)
119 
120 #define MEC_MMCR8_WR(a, b)  *(volatile uint8_t *)(a) = (uint8_t)(b)
121 #define MEC_MMCR8_RD(a)     *(volatile uint8_t *)(a)
122 #define MEC_MMCR16_WR(a, b) *(volatile uint16_t *)(a) = (uint16_t)(b)
123 #define MEC_MMCR16_RD(a)    *(volatile uint16_t *)(a)
124 #define MEC_MMCR32_WR(a, b) *(volatile uint32_t *)(a) = (uint32_t)(b)
125 #define MEC_MMCR32_RD(a)    *(volatile uint32_t *)(a)
126 
127 #ifndef MEC_DIV_ROUND_UP
128 #define MEC_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
129 #endif
130 
131 #ifndef MEC_BITS_PER_LONG
132 #define MEC_BITS_PER_LONG (__CHAR_BIT__ * __SIZEOF_LONG__)
133 #endif
134 
135 #ifndef MEC_BITS_PER_LONG_LONG
136 #define MEC_BITS_PER_LONG_LONG (__CHAR_BIT__ * __SIZEOF_LONG_LONG__)
137 #endif
138 
139 #ifndef MEC_GENMASK
140 #define MEC_GENMASK(h, l) \
141     (((~0UL) - (1UL << (l)) + 1) & (~0UL >> (MEC_BITS_PER_LONG - 1 - (h))))
142 #endif
143 
144 #ifndef MEC_GENMASK64
145 #define MEC_GENMASK64(h, l) \
146     (((~0ULL) - (1ULL << (l)) + 1) & (~0ULL >> (MEC_BITS_PER_LONG_LONG - 1 - (h))))
147 #endif
148 
149 struct mec_buf {
150     void *data;
151     size_t len;
152 };
153 
154 struct mec_buf_set {
155     struct mchp_buf *bufptr;
156     size_t count;
157 };
158 
159 struct mec_buf_link {
160     void *data;
161     size_t len;
162     struct mec_buf_link *next;
163 };
164 
165 #endif /* #ifndef _MEC_DEFS_H */
166