1 /*
2  * Copyright (c) 2008 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #pragma once
9 
10 #include <stdint.h>
11 
12 /* low level macros for accessing memory mapped hardware registers */
13 #define REG64(addr) ((volatile uint64_t *)(uintptr_t)(addr))
14 #define REG32(addr) ((volatile uint32_t *)(uintptr_t)(addr))
15 #define REG16(addr) ((volatile uint16_t *)(uintptr_t)(addr))
16 #define REG8(addr) ((volatile uint8_t *)(uintptr_t)(addr))
17 
18 #define RMWREG64(addr, startbit, width, val) *REG64(addr) = (*REG64(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
19 #define RMWREG32(addr, startbit, width, val) *REG32(addr) = (*REG32(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
20 #define RMWREG16(addr, startbit, width, val) *REG16(addr) = (*REG16(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
21 #define RMWREG8(addr, startbit, width, val) *REG8(addr) = (*REG8(addr) & ~(((1<<(width)) - 1) << (startbit))) | ((val) << (startbit))
22 
23 #define writel(v, a) (*REG32(a) = (v))
24 #define readl(a) (*REG32(a))
25 #define writeb(v, a) (*REG8(a) = (v))
26 #define readb(a) (*REG8(a))
27