|
| 1 | +/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 | +/* |
| 3 | + * Copyright (c) 2021-2023 HPMicro |
| 4 | + */ |
| 5 | + |
| 6 | +#ifndef _HPM_COMMON_H |
| 7 | +#define _HPM_COMMON_H |
| 8 | + |
| 9 | +#include <assert.h> |
| 10 | +#include <stdbool.h> |
| 11 | +#include <stdint.h> |
| 12 | +#include <string.h> |
| 13 | +#include <stdlib.h> |
| 14 | + |
| 15 | +/** |
| 16 | + * |
| 17 | + * @brief COMMON driver APIs |
| 18 | + * @defgroup common_interface COMMON driver APIs |
| 19 | + * @{ |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +#define __R volatile const /* Define "read-only" permission */ |
| 24 | +#define __RW volatile /* Define "read-write" permission */ |
| 25 | +#define __W volatile /* Define "write-only" permission */ |
| 26 | + |
| 27 | +#ifndef __I |
| 28 | +#define __I __R |
| 29 | +#endif |
| 30 | + |
| 31 | +#ifndef __IO |
| 32 | +#define __IO __RW |
| 33 | +#endif |
| 34 | + |
| 35 | +#ifndef __O |
| 36 | +#define __O __W |
| 37 | +#endif |
| 38 | + |
| 39 | +#ifndef ARRAY_SIZE |
| 40 | +#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 41 | +#endif |
| 42 | + |
| 43 | +#define HPM_BITSMASK(val, offset) ((uint32_t)(val) << (offset)) |
| 44 | +#define IS_HPM_BITMASK_SET(val, mask) (((uint32_t)(val) & (uint32_t)(mask)) != 0U) |
| 45 | +#define IS_HPM_BIT_SET(val, offset) (((uint32_t)(val) & (1UL << (offset))) != 0U) |
| 46 | +#define IS_HPM_BITMASK_CLR(val, mask) (((uint32_t)(val) & (uint32_t)(mask)) == 0U) |
| 47 | +#define IS_HPM_BIT_CLR(val, offset) (((uint32_t)(val) & (1UL << (offset))) == 0U) |
| 48 | + |
| 49 | +#define HPM_BREAK_IF(cond) do {if (cond) break; } while (0) |
| 50 | +#define HPM_CONTINUE_IF(cond) do {if (cond) continue; } while (0) |
| 51 | + |
| 52 | +#define HPM_CHECK_RET(x) \ |
| 53 | + do { \ |
| 54 | + stat = (x); \ |
| 55 | + if (status_success != stat) { \ |
| 56 | + return stat; \ |
| 57 | + } \ |
| 58 | + } while (false) |
| 59 | + |
| 60 | +#define SIZE_1KB (1024UL) |
| 61 | +#define SIZE_1MB (1048576UL) |
| 62 | + |
| 63 | +typedef uint32_t hpm_stat_t; |
| 64 | + |
| 65 | +/* @brief Enum definition for the Status group |
| 66 | + * Rule: |
| 67 | + * [Group] 0-999 for the SoC driver and the corresponding components |
| 68 | + * 1000 or above for the application status group |
| 69 | + * [Code] Valid value: 0-999 |
| 70 | + * |
| 71 | + */ |
| 72 | +#define MAKE_STATUS(group, code) ((uint32_t)(group) * 1000U + (uint32_t)(code)) |
| 73 | +/* @brief System status group definitions */ |
| 74 | +enum { |
| 75 | + status_group_common = 0, |
| 76 | + status_group_uart = 1, |
| 77 | + status_group_i2c = 2, |
| 78 | + status_group_spi = 3, |
| 79 | + status_group_usb = 4, |
| 80 | + status_group_i2s = 5, |
| 81 | + status_group_xpi = 6, |
| 82 | + status_group_l1c, |
| 83 | + status_group_dma, |
| 84 | + status_group_femc, |
| 85 | + status_group_sdp, |
| 86 | + status_group_xpi_nor, |
| 87 | + status_group_otp, |
| 88 | + status_group_lcdc, |
| 89 | + status_group_mbx, |
| 90 | + status_group_rng, |
| 91 | + status_group_pdma, |
| 92 | + status_group_wdg, |
| 93 | + status_group_pmic_sec, |
| 94 | + status_group_can, |
| 95 | + status_group_sdxc, |
| 96 | + status_group_pcfg, |
| 97 | + status_group_clk, |
| 98 | + status_group_pllctl, |
| 99 | + status_group_pllctlv2, |
| 100 | + status_group_ffa, |
| 101 | + status_group_mcan, |
| 102 | + |
| 103 | + status_group_middleware_start = 500, |
| 104 | + status_group_sdmmc = status_group_middleware_start, |
| 105 | + status_group_audio_codec, |
| 106 | + status_group_dma_manager, |
| 107 | +}; |
| 108 | + |
| 109 | +/* @brief Common status code definitions */ |
| 110 | +enum { |
| 111 | + status_success = MAKE_STATUS(status_group_common, 0), |
| 112 | + status_fail = MAKE_STATUS(status_group_common, 1), |
| 113 | + status_invalid_argument = MAKE_STATUS(status_group_common, 2), |
| 114 | + status_timeout = MAKE_STATUS(status_group_common, 3), |
| 115 | +}; |
| 116 | + |
| 117 | +#if defined(__GNUC__) |
| 118 | + |
| 119 | +/* alway_inline */ |
| 120 | +#define ATTR_ALWAYS_INLINE __attribute__((always_inline)) |
| 121 | + |
| 122 | +/* weak */ |
| 123 | +#define ATTR_WEAK __attribute__((weak)) |
| 124 | + |
| 125 | +/* alignment */ |
| 126 | +#define ATTR_ALIGN(alignment) __attribute__((aligned(alignment))) |
| 127 | + |
| 128 | +/* place var_declare at section_name, e.x. PLACE_AT(".target_section", var); */ |
| 129 | +#define ATTR_PLACE_AT(section_name) __attribute__((section(section_name))) |
| 130 | + |
| 131 | +#define ATTR_PLACE_AT_WITH_ALIGNMENT(section_name, alignment) \ |
| 132 | +ATTR_PLACE_AT(section_name) ATTR_ALIGN(alignment) |
| 133 | + |
| 134 | +#define ATTR_PLACE_AT_NONCACHEABLE ATTR_PLACE_AT(".noncacheable.bss") |
| 135 | +#define ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(alignment) \ |
| 136 | + ATTR_PLACE_AT_NONCACHEABLE ATTR_ALIGN(alignment) |
| 137 | + |
| 138 | +#define ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_PLACE_AT(".noncacheable.bss") |
| 139 | +#define ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(alignment) \ |
| 140 | + ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_ALIGN(alignment) |
| 141 | + |
| 142 | +/* initialize variable x with y using PLACE_AT_NONCACHEABLE_INIT(x) = {y}; */ |
| 143 | +#define ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_PLACE_AT(".noncacheable.init") |
| 144 | +#define ATTR_PLACE_AT_NONCACHEABLE_INIT_WITH_ALIGNMENT(alignment) \ |
| 145 | + ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_ALIGN(alignment) |
| 146 | + |
| 147 | +#define ATTR_RAMFUNC ATTR_PLACE_AT(".fast") |
| 148 | +#define ATTR_RAMFUNC_WITH_ALIGNMENT(alignment) \ |
| 149 | + ATTR_RAMFUNC ATTR_ALIGN(alignment) |
| 150 | + |
| 151 | +#define ATTR_SHARE_MEM ATTR_PLACE_AT(".sh_mem") |
| 152 | + |
| 153 | +#define NOP() __asm volatile("nop") |
| 154 | +#define WFI() __asm volatile("wfi") |
| 155 | + |
| 156 | +#define HPM_ATTR_MACHINE_INTERRUPT __attribute__ ((section(".isr_vector"), interrupt("machine"), aligned(4))) |
| 157 | + |
| 158 | +#elif defined(__ICCRISCV__) |
| 159 | + |
| 160 | + |
| 161 | + /* alway_inline */ |
| 162 | +#define ATTR_ALWAYS_INLINE __attribute__((always_inline)) |
| 163 | + |
| 164 | +/* weak */ |
| 165 | +#define ATTR_WEAK __weak |
| 166 | + |
| 167 | +/* alignment */ |
| 168 | +#define ATTR_ALIGN(alignment) __attribute__((aligned(alignment))) |
| 169 | + |
| 170 | +/* place var_declare at section_name, e.x. PLACE_AT(".target_section", var); */ |
| 171 | +#define ATTR_PLACE_AT(section_name) __attribute__((section(section_name))) |
| 172 | + |
| 173 | +#define ATTR_PLACE_AT_WITH_ALIGNMENT(section_name, alignment) \ |
| 174 | +ATTR_PLACE_AT(section_name) ATTR_ALIGN(alignment) |
| 175 | + |
| 176 | +#define ATTR_PLACE_AT_NONCACHEABLE ATTR_PLACE_AT(".noncacheable.bss") |
| 177 | +#define ATTR_PLACE_AT_NONCACHEABLE_WITH_ALIGNMENT(alignment) \ |
| 178 | + ATTR_PLACE_AT_NONCACHEABLE ATTR_ALIGN(alignment) |
| 179 | + |
| 180 | +#define ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_PLACE_AT(".noncacheable.bss") |
| 181 | +#define ATTR_PLACE_AT_NONCACHEABLE_BSS_WITH_ALIGNMENT(alignment) \ |
| 182 | + ATTR_PLACE_AT_NONCACHEABLE_BSS ATTR_ALIGN(alignment) |
| 183 | + |
| 184 | +/* initialize variable x with y using PLACE_AT_NONCACHEABLE_INIT(x) = {y}; */ |
| 185 | +#define ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_PLACE_AT(".noncacheable.init") |
| 186 | +#define ATTR_PLACE_AT_NONCACHEABLE_INIT_WITH_ALIGNMENT(alignment) \ |
| 187 | + ATTR_PLACE_AT_NONCACHEABLE_INIT ATTR_ALIGN(alignment) |
| 188 | + |
| 189 | +#define ATTR_RAMFUNC ATTR_PLACE_AT(".fast") |
| 190 | +#define ATTR_RAMFUNC_WITH_ALIGNMENT(alignment) \ |
| 191 | + ATTR_RAMFUNC ATTR_ALIGN(alignment) |
| 192 | + |
| 193 | +#define ATTR_SHARE_MEM ATTR_PLACE_AT(".sh_mem") |
| 194 | + |
| 195 | +#define NOP() __asm volatile("nop") |
| 196 | +#define WFI() __asm volatile("wfi") |
| 197 | + |
| 198 | +#define HPM_ATTR_MACHINE_INTERRUPT __machine __interrupt |
| 199 | + |
| 200 | +#else |
| 201 | +#error Unknown toolchain |
| 202 | +#endif |
| 203 | + |
| 204 | +#ifdef __cplusplus |
| 205 | +extern "C" { |
| 206 | +#endif |
| 207 | + |
| 208 | + |
| 209 | +/** |
| 210 | + * @brief Count bits set to 1 |
| 211 | + * |
| 212 | + * @param value Data to be counted |
| 213 | + * |
| 214 | + * @return number of bits set to 1 |
| 215 | + */ |
| 216 | +static inline uint32_t count_set_bits(uint32_t value) |
| 217 | +{ |
| 218 | + if (value == 0) |
| 219 | + return 0; |
| 220 | + return 1 + count_set_bits(value & (value - 1)); |
| 221 | +} |
| 222 | + |
| 223 | +/** |
| 224 | + * @brief Count bits set to 1 from least significant bit |
| 225 | + * |
| 226 | + * @param value Data to be counted |
| 227 | + * |
| 228 | + * @return number of bits set to 1 |
| 229 | + * @return 0xFFFFFFFF if no bit was set to 1 |
| 230 | + */ |
| 231 | +static inline uint32_t get_first_set_bit_from_lsb(uint32_t value) |
| 232 | +{ |
| 233 | + uint32_t i = 0; |
| 234 | + if (!value) |
| 235 | + return 0xFFFFFFFFUL; |
| 236 | + while (value && !(value & 0x1)) { |
| 237 | + value >>= 1; |
| 238 | + i++; |
| 239 | + } |
| 240 | + return i; |
| 241 | +} |
| 242 | + |
| 243 | +/** |
| 244 | + * @brief Count bits set to 1 from most significant bit |
| 245 | + * |
| 246 | + * @param value Data to be counted |
| 247 | + * |
| 248 | + * @return number of bits set to 1 |
| 249 | + * @return 0xFFFFFFFF if no bit was set to 1 |
| 250 | + */ |
| 251 | +static inline uint32_t get_first_set_bit_from_msb(uint32_t value) |
| 252 | +{ |
| 253 | + uint32_t i = 31; |
| 254 | + if (!value) |
| 255 | + return 0xFFFFFFFFUL; |
| 256 | + while (value && !(value & 0x80000000)) { |
| 257 | + value <<= 1; |
| 258 | + value &= ~1; |
| 259 | + i--; |
| 260 | + } |
| 261 | + return i; |
| 262 | +} |
| 263 | + |
| 264 | +#ifdef __cplusplus |
| 265 | +} |
| 266 | +#endif |
| 267 | + |
| 268 | +/** |
| 269 | + * @} |
| 270 | + */ |
| 271 | +#endif /* _HPM_COMMON_H */ |
0 commit comments