1 /*
2 * Copyright (c) 2003-2017 Cavium Inc. ([email protected]). All rights
3 * reserved.
4 * Copyright 2018-present Facebook, Inc.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * access.h: Wrappers for memory access
9 */
10
11 #ifndef __BDK_BDK_COREBOOT_H
12 #define __BDK_BDK_COREBOOT_H
13
14 #include <device/mmio.h>
15 #include <delay.h>
16
17 /**
18 * Convert a memory pointer (void*) into a hardware compatible
19 * memory address (uint64_t). Cavium hardware widgets don't
20 * understand logical addresses.
21 *
22 * @param ptr C style memory pointer
23 * @return Hardware physical address
24 */
bdk_ptr_to_phys(void * ptr)25 static inline uint64_t bdk_ptr_to_phys(void *ptr)
26 {
27 /* PA = VA for coreboot's purposes */
28 return (uint64_t)ptr;
29 }
30
31 /**
32 * Convert a hardware physical address (uint64_t) into a
33 * memory pointer (void *).
34 *
35 * @param physical_address
36 * Hardware physical address to memory
37 * @return Pointer to memory
38 */
bdk_phys_to_ptr(uint64_t physical_address)39 static inline void *bdk_phys_to_ptr(uint64_t physical_address)
40 {
41 /* PA = VA for coreboot's purposes */
42 return (void *)physical_address;
43 }
44
bdk_write64_int64(uint64_t address,int64_t value)45 static inline void bdk_write64_int64(uint64_t address, int64_t value)
46 {
47 dmb();
48 *(volatile int64_t *)address = value;
49 dmb();
50 }
51
bdk_write64_uint64(uint64_t address,uint64_t value)52 static inline void bdk_write64_uint64(uint64_t address, uint64_t value)
53 {
54 write64(bdk_phys_to_ptr(address), value);
55 }
56
bdk_write64_int32(uint64_t address,int32_t value)57 static inline void bdk_write64_int32(uint64_t address, int32_t value)
58 {
59 dmb();
60 *(volatile int32_t *)address = value;
61 dmb();
62 }
63
bdk_write64_uint32(uint64_t address,uint32_t value)64 static inline void bdk_write64_uint32(uint64_t address, uint32_t value)
65 {
66 write32(bdk_phys_to_ptr(address), value);
67 }
68
bdk_write64_int16(uint64_t address,int16_t value)69 static inline void bdk_write64_int16(uint64_t address, int16_t value)
70 {
71 dmb();
72 *(volatile int16_t *)address = value;
73 dmb();
74 }
75
bdk_write64_uint16(uint64_t address,uint16_t value)76 static inline void bdk_write64_uint16(uint64_t address, uint16_t value)
77 {
78 write16(bdk_phys_to_ptr(address), value);
79 }
80
bdk_write64_int8(uint64_t address,int8_t value)81 static inline void bdk_write64_int8(uint64_t address, int8_t value)
82 {
83 dmb();
84 *(volatile int8_t *)address = value;
85 dmb();
86 }
87
bdk_write64_uint8(uint64_t address,uint8_t value)88 static inline void bdk_write64_uint8(uint64_t address, uint8_t value)
89 {
90 write8(bdk_phys_to_ptr(address), value);
91 }
92
bdk_read64_int64(uint64_t address)93 static inline int64_t bdk_read64_int64(uint64_t address)
94 {
95 return *(volatile int64_t *)bdk_phys_to_ptr(address);
96 }
97
bdk_read64_uint64(uint64_t address)98 static inline uint64_t bdk_read64_uint64(uint64_t address)
99 {
100 return read64(bdk_phys_to_ptr(address));
101 }
102
bdk_read64_int32(uint64_t address)103 static inline int32_t bdk_read64_int32(uint64_t address)
104 {
105 return *(volatile int32_t *)bdk_phys_to_ptr(address);
106 }
107
bdk_read64_uint32(uint64_t address)108 static inline uint32_t bdk_read64_uint32(uint64_t address)
109 {
110 return read32(bdk_phys_to_ptr(address));
111 }
112
bdk_read64_int16(uint64_t address)113 static inline int16_t bdk_read64_int16(uint64_t address)
114 {
115 return *(volatile int16_t *)bdk_phys_to_ptr(address);
116 }
117
bdk_read64_uint16(uint64_t address)118 static inline uint16_t bdk_read64_uint16(uint64_t address)
119 {
120 return read16(bdk_phys_to_ptr(address));
121 }
122
bdk_read64_int8(uint64_t address)123 static inline int8_t bdk_read64_int8(uint64_t address)
124 {
125 return *(volatile int8_t *)bdk_phys_to_ptr(address);
126 }
127
bdk_read64_uint8(uint64_t address)128 static inline uint8_t bdk_read64_uint8(uint64_t address)
129 {
130 return read8(bdk_phys_to_ptr(address));
131 }
132
133 /**
134 * Returns the number of bits set in the provided value.
135 * Simple wrapper for POP instruction.
136 *
137 * @param val 32 bit value to count set bits in
138 *
139 * @return Number of bits set
140 */
bdk_pop(uint32_t v)141 static inline uint32_t bdk_pop(uint32_t v)
142 {
143 /* Use parallel SWAR algorithm */
144 v = v - ((v >> 1) & 0x55555555);
145 v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
146 return (((v + (v >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
147 }
148
149 /**
150 * Returns the number of bits set in the provided value.
151 * Simple wrapper for DPOP instruction.
152 *
153 * @param val 64 bit value to count set bits in
154 *
155 * @return Number of bits set
156 */
bdk_dpop(uint64_t val)157 static inline int bdk_dpop(uint64_t val)
158 {
159 return bdk_pop(val & 0xffffffff) + bdk_pop(val >> 32);
160 }
161
162 /**
163 * Wait for the specified number of micro seconds
164 *
165 * @param usec micro seconds to wait
166 */
bdk_wait_usec(uint64_t usec)167 static inline void bdk_wait_usec(uint64_t usec)
168 {
169 udelay((unsigned int)usec);
170 }
171
172 #endif /* !__BDK_BDK_COREBOOT_H */
173