1 /** IoAccessLib.c
2 
3   Provide MMIO APIs for BE modules.
4 
5   Copyright 2017-2020 NXP
6 
7   SPDX-License-Identifier: BSD-2-Clause-Patent
8 
9 **/
10 
11 #include <Base.h>
12 #include <Library/BaseLib.h>
13 #include <Library/IoAccessLib.h>
14 #include <Library/IoLib.h>
15 
16 /**
17   MmioRead16 for Big-Endian modules.
18 
19   @param  Address The MMIO register to read.
20 
21   @return The value read.
22 
23 **/
24 STATIC
25 UINT16
26 EFIAPI
SwapMmioRead16(IN UINTN Address)27 SwapMmioRead16 (
28   IN  UINTN     Address
29   )
30 {
31   return SwapBytes16 (MmioRead16 (Address));
32 }
33 
34 /**
35   MmioRead32 for Big-Endian modules.
36 
37   @param  Address The MMIO register to read.
38 
39   @return The value read.
40 
41 **/
42 STATIC
43 UINT32
44 EFIAPI
SwapMmioRead32(IN UINTN Address)45 SwapMmioRead32 (
46   IN  UINTN     Address
47   )
48 {
49   return SwapBytes32 (MmioRead32 (Address));
50 }
51 
52 /**
53   MmioRead64 for Big-Endian modules.
54 
55   @param  Address The MMIO register to read.
56 
57   @return The value read.
58 
59 **/
60 STATIC
61 UINT64
62 EFIAPI
SwapMmioRead64(IN UINTN Address)63 SwapMmioRead64 (
64   IN  UINTN     Address
65   )
66 {
67   return SwapBytes64 (MmioRead64 (Address));
68 }
69 
70 /**
71   MmioWrite16 for Big-Endian modules.
72 
73   @param  Address The MMIO register to write.
74   @param  Value   The value to write to the MMIO register.
75 
76 **/
77 STATIC
78 UINT16
79 EFIAPI
SwapMmioWrite16(IN UINTN Address,IN UINT16 Value)80 SwapMmioWrite16 (
81   IN  UINTN     Address,
82   IN  UINT16    Value
83   )
84 {
85   return MmioWrite16 (Address, SwapBytes16 (Value));
86 }
87 
88 /**
89   MmioWrite32 for Big-Endian modules.
90 
91   @param  Address The MMIO register to write.
92   @param  Value   The value to write to the MMIO register.
93 
94 **/
95 STATIC
96 UINT32
97 EFIAPI
SwapMmioWrite32(IN UINTN Address,IN UINT32 Value)98 SwapMmioWrite32 (
99   IN  UINTN     Address,
100   IN  UINT32    Value
101   )
102 {
103   return MmioWrite32 (Address, SwapBytes32 (Value));
104 }
105 
106 /**
107   MmioWrite64 for Big-Endian modules.
108 
109   @param  Address The MMIO register to write.
110   @param  Value   The value to write to the MMIO register.
111 
112 **/
113 STATIC
114 UINT64
115 EFIAPI
SwapMmioWrite64(IN UINTN Address,IN UINT64 Value)116 SwapMmioWrite64 (
117   IN  UINTN     Address,
118   IN  UINT64    Value
119   )
120 {
121   return MmioWrite64 (Address, SwapBytes64 (Value));
122 }
123 
124 /**
125   MmioAndThenOr16 for Big-Endian modules.
126 
127   @param  Address The MMIO register to write.
128   @param  AndData The value to AND with the read value from the MMIO register.
129   @param  OrData  The value to OR with the result of the AND operation.
130 
131   @return The value written back to the MMIO register.
132 
133 **/
134 STATIC
135 UINT16
136 EFIAPI
SwapMmioAndThenOr16(IN UINTN Address,IN UINT16 AndData,IN UINT16 OrData)137 SwapMmioAndThenOr16 (
138   IN  UINTN     Address,
139   IN  UINT16    AndData,
140   IN  UINT16    OrData
141   )
142 {
143   AndData = SwapBytes16 (AndData);
144   OrData = SwapBytes16 (OrData);
145 
146   return MmioAndThenOr16 (Address, AndData, OrData);
147 }
148 
149 /**
150   MmioAndThenOr32 for Big-Endian modules.
151 
152   @param  Address The MMIO register to write.
153   @param  AndData The value to AND with the read value from the MMIO register.
154   @param  OrData  The value to OR with the result of the AND operation.
155 
156   @return The value written back to the MMIO register.
157 
158 **/
159 STATIC
160 UINT32
161 EFIAPI
SwapMmioAndThenOr32(IN UINTN Address,IN UINT32 AndData,IN UINT32 OrData)162 SwapMmioAndThenOr32 (
163   IN  UINTN     Address,
164   IN  UINT32    AndData,
165   IN  UINT32    OrData
166   )
167 {
168   AndData = SwapBytes32 (AndData);
169   OrData = SwapBytes32 (OrData);
170 
171   return MmioAndThenOr32 (Address, AndData, OrData);
172 }
173 
174 /**
175   MmioAndThenOr64 for Big-Endian modules.
176 
177   @param  Address The MMIO register to write.
178   @param  AndData The value to AND with the read value from the MMIO register.
179   @param  OrData  The value to OR with the result of the AND operation.
180 
181   @return The value written back to the MMIO register.
182 
183 **/
184 STATIC
185 UINT64
186 EFIAPI
SwapMmioAndThenOr64(IN UINTN Address,IN UINT64 AndData,IN UINT64 OrData)187 SwapMmioAndThenOr64 (
188   IN  UINTN     Address,
189   IN  UINT64    AndData,
190   IN  UINT64    OrData
191   )
192 {
193   AndData = SwapBytes64 (AndData);
194   OrData = SwapBytes64 (OrData);
195 
196   return MmioAndThenOr64 (Address, AndData, OrData);
197 }
198 
199 /**
200   MmioOr16 for Big-Endian modules.
201 
202   @param  Address The MMIO register to write.
203   @param  OrData  The value to OR with the read value from the MMIO register.
204 
205   @return The value written back to the MMIO register.
206 
207 **/
208 STATIC
209 UINT16
210 EFIAPI
SwapMmioOr16(IN UINTN Address,IN UINT16 OrData)211 SwapMmioOr16 (
212   IN  UINTN     Address,
213   IN  UINT16    OrData
214   )
215 {
216   return MmioOr16 (Address, SwapBytes16 (OrData));
217 }
218 
219 /**
220   MmioOr32 for Big-Endian modules.
221 
222   @param  Address The MMIO register to write.
223   @param  OrData  The value to OR with the read value from the MMIO register.
224 
225   @return The value written back to the MMIO register.
226 
227 **/
228 STATIC
229 UINT32
230 EFIAPI
SwapMmioOr32(IN UINTN Address,IN UINT32 OrData)231 SwapMmioOr32 (
232   IN  UINTN     Address,
233   IN  UINT32    OrData
234   )
235 {
236   return MmioOr32 (Address, SwapBytes32 (OrData));
237 }
238 
239 /**
240   MmioOr64 for Big-Endian modules.
241 
242   @param  Address The MMIO register to write.
243   @param  OrData  The value to OR with the read value from the MMIO register.
244 
245   @return The value written back to the MMIO register.
246 
247 **/
248 STATIC
249 UINT64
250 EFIAPI
SwapMmioOr64(IN UINTN Address,IN UINT64 OrData)251 SwapMmioOr64 (
252   IN  UINTN     Address,
253   IN  UINT64    OrData
254   )
255 {
256   return MmioOr64 (Address, SwapBytes64 (OrData));
257 }
258 
259 /**
260   MmioAnd16 for Big-Endian modules.
261 
262   @param  Address The MMIO register to write.
263   @param  AndData The value to AND with the read value from the MMIO register.
264 
265   @return The value written back to the MMIO register.
266 
267 **/
268 STATIC
269 UINT16
270 EFIAPI
SwapMmioAnd16(IN UINTN Address,IN UINT16 AndData)271 SwapMmioAnd16 (
272   IN  UINTN     Address,
273   IN  UINT16    AndData
274   )
275 {
276   return MmioAnd16 (Address, SwapBytes16 (AndData));
277 }
278 
279 /**
280   MmioAnd32 for Big-Endian modules.
281 
282   @param  Address The MMIO register to write.
283   @param  AndData The value to AND with the read value from the MMIO register.
284 
285   @return The value written back to the MMIO register.
286 
287 **/
288 STATIC
289 UINT32
290 EFIAPI
SwapMmioAnd32(IN UINTN Address,IN UINT32 AndData)291 SwapMmioAnd32 (
292   IN  UINTN     Address,
293   IN  UINT32    AndData
294   )
295 {
296   return MmioAnd32 (Address, SwapBytes32 (AndData));
297 }
298 
299 /**
300   MmioAnd64 for Big-Endian modules.
301 
302   @param  Address The MMIO register to write.
303   @param  AndData The value to AND with the read value from the MMIO register.
304 
305   @return The value written back to the MMIO register.
306 
307 **/
308 STATIC
309 UINT64
310 EFIAPI
SwapMmioAnd64(IN UINTN Address,IN UINT64 AndData)311 SwapMmioAnd64 (
312   IN  UINTN     Address,
313   IN  UINT64    AndData
314   )
315 {
316   return MmioAnd64 (Address, SwapBytes64 (AndData));
317 }
318 
319 STATIC MMIO_OPERATIONS SwappingFunctions = {
320   SwapMmioRead16,
321   SwapMmioWrite16,
322   SwapMmioOr16,
323   SwapMmioAnd16,
324   SwapMmioAndThenOr16,
325   SwapMmioRead32,
326   SwapMmioWrite32,
327   SwapMmioOr32,
328   SwapMmioAnd32,
329   SwapMmioAndThenOr32,
330   SwapMmioRead64,
331   SwapMmioWrite64,
332   SwapMmioOr64,
333   SwapMmioAnd64,
334   SwapMmioAndThenOr64,
335 };
336 
337 STATIC MMIO_OPERATIONS NonSwappingFunctions = {
338   MmioRead16,
339   MmioWrite16,
340   MmioOr16,
341   MmioAnd16,
342   MmioAndThenOr16,
343   MmioRead32,
344   MmioWrite32,
345   MmioOr32,
346   MmioAnd32,
347   MmioAndThenOr32,
348   MmioRead64,
349   MmioWrite64,
350   MmioOr64,
351   MmioAnd64,
352   MmioAndThenOr64,
353 };
354 
355 /**
356   Function to return pointer to Mmio operations.
357 
358   @param  Swap  Flag to tell if Swap is needed or not
359                 on Mmio Operations.
360 
361   @return       Pointer to Mmio Operations.
362 
363 **/
364 MMIO_OPERATIONS *
GetMmioOperations(BOOLEAN Swap)365 GetMmioOperations (BOOLEAN Swap) {
366   if (Swap) {
367     return &SwappingFunctions;
368   } else {
369     return &NonSwappingFunctions;
370   }
371 }
372