Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 1 | /* |
yang.li | 5bef2f6 | 2022-01-11 14:08:06 +0800 | [diff] [blame] | 2 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 3 | * |
yang.li | 5bef2f6 | 2022-01-11 14:08:06 +0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: MIT |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 7 | #ifndef __COMMON_H |
| 8 | #define __COMMON_H |
| 9 | |
| 10 | #ifdef __cplusplus |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | #include <stdint.h> |
| 14 | #include <errno.h> |
| 15 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 16 | /* Macros to access registers */ |
| 17 | #define REG32_ADDR(addr) ((volatile uint32_t *)(uintptr_t)(addr)) |
| 18 | #define REG16_ADDR(addr) ((volatile uint16_t *)(uintptr_t)(addr)) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 19 | #define REG8_ADDR(addr) ((volatile uint8_t *)(uintptr_t)(addr)) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 20 | |
| 21 | #define REG32(addr) (*REG32_ADDR(addr)) |
| 22 | #define REG16(addr) (*REG16_ADDR(addr)) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 23 | #define REG8(addr) (*REG8_ADDR(addr)) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 24 | |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 25 | #define BIT(nr) (1UL << (nr)) |
| 26 | #define REG32_UPDATE_BITS(addr, mask, val) \ |
| 27 | do { \ |
| 28 | uint32_t _v = REG32((unsigned long)addr); \ |
| 29 | _v &= (~(mask)); \ |
| 30 | _v |= ((val) & (mask)); \ |
| 31 | REG32((unsigned long)addr) = _v; \ |
| 32 | } while (0) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 33 | |
xiaohu.huang | 0a90955 | 2022-09-19 14:43:08 +0800 | [diff] [blame] | 34 | static inline int generic_ffs(int x) |
| 35 | { |
| 36 | int r = 1; |
| 37 | |
| 38 | if (!x) |
| 39 | return 0; |
| 40 | if (!(x & 0xffff)) { |
| 41 | x >>= 16; |
| 42 | r += 16; |
| 43 | } |
| 44 | if (!(x & 0xff)) { |
| 45 | x >>= 8; |
| 46 | r += 8; |
| 47 | } |
| 48 | if (!(x & 0xf)) { |
| 49 | x >>= 4; |
| 50 | r += 4; |
| 51 | } |
| 52 | if (!(x & 3)) { |
| 53 | x >>= 2; |
| 54 | r += 2; |
| 55 | } |
| 56 | if (!(x & 1)) { |
| 57 | x >>= 1; |
| 58 | r += 1; |
| 59 | } |
| 60 | return r; |
| 61 | } |
| 62 | #define ffs(x) generic_ffs(x) |
| 63 | |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 64 | #ifndef FIELD_PREP |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 65 | #define FIELD_PREP(_mask, _val) (((typeof(_mask))(_val) << (ffs(_mask) - 1)) & (_mask)) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 66 | #endif |
| 67 | |
| 68 | #ifndef FIELD_GET |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 69 | #define FIELD_GET(_mask, _reg) ((typeof(_mask))(((_reg) & (_mask)) >> (ffs(_mask) - 1))) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 70 | #endif |
| 71 | |
| 72 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) |
| 73 | |
| 74 | #define BITS_PER_LONG (sizeof(unsigned long) == 8 ? 64 : 32) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 75 | #define GENMASK(h, l) (((~0UL) << (l)) & (~0UL >> (BITS_PER_LONG - 1 - (h)))) |
| 76 | #define IS_ALIGNED(x, a) (((unsigned long)(x) & ((unsigned long)(a)-1)) == 0) |
| 77 | #define _RET_IP_ ((unsigned long)__builtin_return_address(0)) |
| 78 | #define _THIS_IP_ \ |
| 79 | ({ \ |
| 80 | __label__ __here; \ |
| 81 | __here: \ |
| 82 | (unsigned long)&&__here; \ |
| 83 | }) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 84 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
xiaohu.huang | e7678d1 | 2022-05-10 00:56:48 +0800 | [diff] [blame] | 85 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y)) + 1) |
Xiaohu.Huang | 83a0b70 | 2021-12-28 11:06:24 +0800 | [diff] [blame] | 86 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) |
| 87 | |
| 88 | typedef uint64_t u64; |
| 89 | typedef uint32_t u32; |
| 90 | typedef uint16_t u16; |
| 91 | typedef uint8_t u8; |
| 92 | typedef int64_t s64; |
| 93 | typedef int32_t s32; |
| 94 | typedef int16_t s16; |
| 95 | typedef int8_t s8; |
| 96 | |
| 97 | #ifndef BIT |
| 98 | #define BIT(x) (1 << (x)) |
| 99 | #endif |
| 100 | |
| 101 | #ifdef __cplusplus |
| 102 | } |
| 103 | #endif |
| 104 | #endif |