1*433d6423SLionel Sambuc #ifndef __MMIO_H__
2*433d6423SLionel Sambuc #define __MMIO_H__
3*433d6423SLionel Sambuc
4*433d6423SLionel Sambuc #define REG16(x)(*((volatile uint16_t *)(x)))
5*433d6423SLionel Sambuc #define REG(x)(*((volatile uint32_t *)(x)))
6*433d6423SLionel Sambuc #define BIT(x)(0x1 << x)
7*433d6423SLionel Sambuc
8*433d6423SLionel Sambuc /* Write a uint32_t value to a memory address. */
9*433d6423SLionel Sambuc static inline void
write32(uint32_t address,uint32_t value)10*433d6423SLionel Sambuc write32(uint32_t address, uint32_t value)
11*433d6423SLionel Sambuc {
12*433d6423SLionel Sambuc REG(address) = value;
13*433d6423SLionel Sambuc }
14*433d6423SLionel Sambuc
15*433d6423SLionel Sambuc /* Read an uint32_t from a memory address */
16*433d6423SLionel Sambuc static inline uint32_t
read32(uint32_t address)17*433d6423SLionel Sambuc read32(uint32_t address)
18*433d6423SLionel Sambuc {
19*433d6423SLionel Sambuc return REG(address);
20*433d6423SLionel Sambuc }
21*433d6423SLionel Sambuc
22*433d6423SLionel Sambuc /* Set a 32 bits value depending on a mask */
23*433d6423SLionel Sambuc static inline void
set32(uint32_t address,uint32_t mask,uint32_t value)24*433d6423SLionel Sambuc set32(uint32_t address, uint32_t mask, uint32_t value)
25*433d6423SLionel Sambuc {
26*433d6423SLionel Sambuc uint32_t val;
27*433d6423SLionel Sambuc val = read32(address);
28*433d6423SLionel Sambuc /* clear the bits */
29*433d6423SLionel Sambuc val &= ~(mask);
30*433d6423SLionel Sambuc /* apply the value using the mask */
31*433d6423SLionel Sambuc val |= (value & mask);
32*433d6423SLionel Sambuc write32(address, val);
33*433d6423SLionel Sambuc }
34*433d6423SLionel Sambuc
35*433d6423SLionel Sambuc /* Write a uint16_t value to a memory address. */
36*433d6423SLionel Sambuc static inline void
write16(uint32_t address,uint16_t value)37*433d6423SLionel Sambuc write16(uint32_t address, uint16_t value)
38*433d6423SLionel Sambuc {
39*433d6423SLionel Sambuc REG16(address) = value;
40*433d6423SLionel Sambuc }
41*433d6423SLionel Sambuc
42*433d6423SLionel Sambuc /* Read an uint16_t from a memory address */
43*433d6423SLionel Sambuc static inline uint16_t
read16(uint32_t address)44*433d6423SLionel Sambuc read16(uint32_t address)
45*433d6423SLionel Sambuc {
46*433d6423SLionel Sambuc return REG16(address);
47*433d6423SLionel Sambuc }
48*433d6423SLionel Sambuc
49*433d6423SLionel Sambuc /* Set a 16 bits value depending on a mask */
50*433d6423SLionel Sambuc static inline void
set16(uint32_t address,uint16_t mask,uint16_t value)51*433d6423SLionel Sambuc set16(uint32_t address, uint16_t mask, uint16_t value)
52*433d6423SLionel Sambuc {
53*433d6423SLionel Sambuc uint16_t val;
54*433d6423SLionel Sambuc val = read16(address);
55*433d6423SLionel Sambuc /* clear the bits */
56*433d6423SLionel Sambuc val &= ~(mask);
57*433d6423SLionel Sambuc /* apply the value using the mask */
58*433d6423SLionel Sambuc val |= (value & mask);
59*433d6423SLionel Sambuc write16(address, val);
60*433d6423SLionel Sambuc }
61*433d6423SLionel Sambuc
62*433d6423SLionel Sambuc #endif /* __MMIO_H__ */
63