1 /*
2  * Copyright (c) 2018 - 2019 Antmicro <www.antmicro.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __RISCV32_LITEX_VEXRISCV_SOC_H_
8 #define __RISCV32_LITEX_VEXRISCV_SOC_H_
9 
10 #include <zephyr/devicetree.h>
11 #include <zephyr/arch/riscv/sys_io.h>
12 
13 #ifndef _ASMLANGUAGE
14 /* CSR access helpers */
15 
litex_read8(unsigned long addr)16 static inline unsigned char litex_read8(unsigned long addr)
17 {
18 #if CONFIG_LITEX_CSR_DATA_WIDTH >= 8
19 	return sys_read8(addr);
20 #else
21 #error CSR data width less than 8
22 #endif
23 }
24 
litex_read16(unsigned long addr)25 static inline unsigned short litex_read16(unsigned long addr)
26 {
27 #if CONFIG_LITEX_CSR_DATA_WIDTH == 8
28 	return (sys_read8(addr) << 8)
29 		| sys_read8(addr + 0x4);
30 #elif CONFIG_LITEX_CSR_DATA_WIDTH >= 16
31 	return sys_read16(addr);
32 #else
33 #error Unsupported CSR data width
34 #endif
35 }
36 
litex_read32(unsigned long addr)37 static inline unsigned int litex_read32(unsigned long addr)
38 {
39 #if CONFIG_LITEX_CSR_DATA_WIDTH == 8
40 	return (sys_read8(addr) << 24)
41 		| (sys_read8(addr + 0x4) << 16)
42 		| (sys_read8(addr + 0x8) << 8)
43 		| sys_read8(addr + 0xc);
44 #elif CONFIG_LITEX_CSR_DATA_WIDTH >= 32
45 	return sys_read32(addr);
46 #else
47 #error Unsupported CSR data width
48 #endif
49 }
50 
litex_read64(unsigned long addr)51 static inline uint64_t litex_read64(unsigned long addr)
52 {
53 #if CONFIG_LITEX_CSR_DATA_WIDTH == 8
54 	return (((uint64_t)sys_read8(addr)) << 56)
55 		| ((uint64_t)sys_read8(addr + 0x4) << 48)
56 		| ((uint64_t)sys_read8(addr + 0x8) << 40)
57 		| ((uint64_t)sys_read8(addr + 0xc) << 32)
58 		| ((uint64_t)sys_read8(addr + 0x10) << 24)
59 		| ((uint64_t)sys_read8(addr + 0x14) << 16)
60 		| ((uint64_t)sys_read8(addr + 0x18) << 8)
61 		| (uint64_t)sys_read8(addr + 0x1c);
62 #elif CONFIG_LITEX_CSR_DATA_WIDTH == 32
63 	return ((uint64_t)sys_read32(addr) << 32) | (uint64_t)sys_read32(addr + 0x4);
64 #elif CONFIG_LITEX_CSR_DATA_WIDTH >= 64
65 	return sys_read64(addr);
66 #else
67 #error Unsupported CSR data width
68 #endif
69 }
70 
litex_write8(unsigned char value,unsigned long addr)71 static inline void litex_write8(unsigned char value, unsigned long addr)
72 {
73 #if CONFIG_LITEX_CSR_DATA_WIDTH >= 8
74 	sys_write8(value, addr);
75 #else
76 #error CSR data width less than 8
77 #endif
78 }
79 
litex_write16(unsigned short value,unsigned long addr)80 static inline void litex_write16(unsigned short value, unsigned long addr)
81 {
82 #if CONFIG_LITEX_CSR_DATA_WIDTH == 8
83 	sys_write8(value >> 8, addr);
84 	sys_write8(value, addr + 0x4);
85 #elif CONFIG_LITEX_CSR_DATA_WIDTH >= 16
86 	sys_write16(value, addr);
87 #else
88 #error Unsupported CSR data width
89 #endif
90 }
91 
litex_write32(unsigned int value,unsigned long addr)92 static inline void litex_write32(unsigned int value, unsigned long addr)
93 {
94 #if CONFIG_LITEX_CSR_DATA_WIDTH == 8
95 	sys_write8(value >> 24, addr);
96 	sys_write8(value >> 16, addr + 0x4);
97 	sys_write8(value >> 8, addr + 0x8);
98 	sys_write8(value, addr + 0xC);
99 #elif CONFIG_LITEX_CSR_DATA_WIDTH >= 32
100 	sys_write32(value, addr);
101 #else
102 #error Unsupported CSR data width
103 #endif
104 }
105 
106 /*
107  * Operates on uint32_t values only
108  * Size is in bytes and meaningful are 1, 2 or 4
109  * Address must be aligned to 4 bytes
110  */
litex_write(uint32_t addr,uint32_t size,uint32_t value)111 static inline void litex_write(uint32_t addr, uint32_t size, uint32_t value)
112 {
113 	switch (size) {
114 	case 1:
115 		litex_write8(value, addr);
116 		break;
117 	case 2:
118 		litex_write16(value, addr);
119 		break;
120 	case 4:
121 		litex_write32(value, addr);
122 		break;
123 	default:
124 		break;
125 	}
126 }
127 
128 /*
129  * Operates on uint32_t values only
130  * Size is in bytes and meaningful are 1, 2 or 4
131  * Address must be aligned to 4 bytes
132  */
litex_read(uint32_t addr,uint32_t size)133 static inline uint32_t litex_read(uint32_t addr, uint32_t size)
134 {
135 	switch (size) {
136 	case 1:
137 		return litex_read8(addr);
138 	case 2:
139 		return litex_read16(addr);
140 	case 4:
141 		return litex_read32(addr);
142 	default:
143 		return 0;
144 	}
145 }
146 
147 #endif /* _ASMLANGUAGE */
148 
149 #endif /* __RISCV32_LITEX_VEXRISCV_SOC_H_ */
150