2 Common I/O Library routines.
4 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5 Copyright (c) 2017, AMD Incorporated. All rights reserved.<BR>
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
18 #include "BaseIoLibIntrinsicInternal.h"
19 #include <Library/PcdLib.h>
21 #define MAP_PORT_BASE_TO_MEM(_Port) \
22 ((((_Port) & 0xfffc) << 10) | ((_Port) & 0x0fff))
25 Translates I/O port address to memory address.
27 This function translates I/O port address to memory address by adding the 64MB
28 aligned I/O Port space to the I/O address.
29 If I/O Port space base is not 64MB aligned, then ASSERT ().
31 @param Port The I/O port to read.
33 @return The memory address.
37 InternalGetMemoryMapAddress (
42 UINTN IoBlockBaseAddress
;
44 Address
= MAP_PORT_BASE_TO_MEM (Port
);
45 IoBlockBaseAddress
= PcdGet64(PcdIoBlockBaseAddressForIpf
);
48 // Make sure that the I/O Port space base is 64MB aligned.
50 ASSERT ((IoBlockBaseAddress
& 0x3ffffff) == 0);
51 Address
+= IoBlockBaseAddress
;
57 Reads an 8-bit I/O port.
59 Reads the 8-bit I/O port specified by Port. The 8-bit read value is returned.
60 This function must guarantee that all I/O read and write operations are
63 If 8-bit I/O port operations are not supported, then ASSERT().
65 @param Port The I/O port to read.
67 @return The value read.
76 return MmioRead8 (InternalGetMemoryMapAddress (Port
));
80 Reads a 16-bit I/O port.
82 Reads the 16-bit I/O port specified by Port. The 16-bit read value is returned.
83 This function must guarantee that all I/O read and write operations are
86 If 16-bit I/O port operations are not supported, then ASSERT().
87 If Port is not aligned on a 16-bit boundary, then ASSERT().
89 @param Port The I/O port to read.
91 @return The value read.
100 return MmioRead16 (InternalGetMemoryMapAddress (Port
));
104 Reads a 32-bit I/O port.
106 Reads the 32-bit I/O port specified by Port. The 32-bit read value is returned.
107 This function must guarantee that all I/O read and write operations are
110 If 32-bit I/O port operations are not supported, then ASSERT().
111 If Port is not aligned on a 32-bit boundary, then ASSERT().
113 @param Port The I/O port to read.
115 @return The value read.
124 return MmioRead32 (InternalGetMemoryMapAddress (Port
));
128 Reads a 64-bit I/O port.
130 Reads the 64-bit I/O port specified by Port. The 64-bit read value is returned.
131 This function must guarantee that all I/O read and write operations are
134 If 64-bit I/O port operations are not supported, then ASSERT().
135 If Port is not aligned on a 64-bit boundary, then ASSERT().
137 @param Port The I/O port to read.
139 @return The value read.
154 Writes an 8-bit I/O port.
156 Writes the 8-bit I/O port specified by Port with the value specified by Value
157 and returns Value. This function must guarantee that all I/O read and write
158 operations are serialized.
160 If 8-bit I/O port operations are not supported, then ASSERT().
162 @param Port The I/O port to write.
163 @param Value The value to write to the I/O port.
165 @return The value written the I/O port.
175 return MmioWrite8 (InternalGetMemoryMapAddress (Port
), Value
);
179 Writes a 16-bit I/O port.
181 Writes the 16-bit I/O port specified by Port with the value specified by Value
182 and returns Value. This function must guarantee that all I/O read and write
183 operations are serialized.
185 If 16-bit I/O port operations are not supported, then ASSERT().
186 If Port is not aligned on a 16-bit boundary, then ASSERT().
188 @param Port The I/O port to write.
189 @param Value The value to write to the I/O port.
191 @return The value written the I/O port.
201 return MmioWrite16 (InternalGetMemoryMapAddress (Port
), Value
);
205 Writes a 32-bit I/O port.
207 Writes the 32-bit I/O port specified by Port with the value specified by Value
208 and returns Value. This function must guarantee that all I/O read and write
209 operations are serialized.
211 If 32-bit I/O port operations are not supported, then ASSERT().
212 If Port is not aligned on a 32-bit boundary, then ASSERT().
214 @param Port The I/O port to write.
215 @param Value The value to write to the I/O port.
217 @return The value written the I/O port.
227 return MmioWrite32 (InternalGetMemoryMapAddress (Port
), Value
);
231 Writes a 64-bit I/O port.
233 Writes the 64-bit I/O port specified by Port with the value specified by Value
234 and returns Value. This function must guarantee that all I/O read and write
235 operations are serialized.
237 If 64-bit I/O port operations are not supported, then ASSERT().
238 If Port is not aligned on a 64-bit boundary, then ASSERT().
240 @param Port The I/O port to write.
241 @param Value The value to write to the I/O port.
243 @return The value written the I/O port.
258 Reads an 8-bit I/O port fifo into a block of memory.
260 Reads the 8-bit I/O fifo port specified by Port.
261 The port is read Count times, and the read data is
262 stored in the provided Buffer.
264 This function must guarantee that all I/O read and write operations are
267 If 8-bit I/O port operations are not supported, then ASSERT().
269 @param Port The I/O port to read.
270 @param Count The number of times to read I/O port.
271 @param Buffer The buffer to store the read data into.
284 Buffer8
= (UINT8
*)Buffer
;
285 while (Count
-- > 0) {
286 *Buffer8
++ = IoRead8 (Port
);
291 Reads a 16-bit I/O port fifo into a block of memory.
293 Reads the 16-bit I/O fifo port specified by Port.
294 The port is read Count times, and the read data is
295 stored in the provided Buffer.
297 This function must guarantee that all I/O read and write operations are
300 If 16-bit I/O port operations are not supported, then ASSERT().
302 @param Port The I/O port to read.
303 @param Count The number of times to read I/O port.
304 @param Buffer The buffer to store the read data into.
317 Buffer16
= (UINT16
*)Buffer
;
318 while (Count
-- > 0) {
319 *Buffer16
++ = IoRead16 (Port
);
324 Reads a 32-bit I/O port fifo into a block of memory.
326 Reads the 32-bit I/O fifo port specified by Port.
327 The port is read Count times, and the read data is
328 stored in the provided Buffer.
330 This function must guarantee that all I/O read and write operations are
333 If 32-bit I/O port operations are not supported, then ASSERT().
335 @param Port The I/O port to read.
336 @param Count The number of times to read I/O port.
337 @param Buffer The buffer to store the read data into.
350 Buffer32
= (UINT32
*)Buffer
;
351 while (Count
-- > 0) {
352 *Buffer32
++ = IoRead32 (Port
);
357 Writes a block of memory into an 8-bit I/O port fifo.
359 Writes the 8-bit I/O fifo port specified by Port.
360 The port is written Count times, and the write data is
361 retrieved from the provided Buffer.
363 This function must guarantee that all I/O write and write operations are
366 If 8-bit I/O port operations are not supported, then ASSERT().
368 @param Port The I/O port to write.
369 @param Count The number of times to write I/O port.
370 @param Buffer The buffer to retrieve the write data from.
383 Buffer8
= (UINT8
*)Buffer
;
384 while (Count
-- > 0) {
385 IoWrite8 (Port
, *Buffer8
++);
390 Writes a block of memory into a 16-bit I/O port fifo.
392 Writes the 16-bit I/O fifo port specified by Port.
393 The port is written Count times, and the write data is
394 retrieved from the provided Buffer.
396 This function must guarantee that all I/O write and write operations are
399 If 16-bit I/O port operations are not supported, then ASSERT().
401 @param Port The I/O port to write.
402 @param Count The number of times to write I/O port.
403 @param Buffer The buffer to retrieve the write data from.
416 Buffer16
= (UINT16
*)Buffer
;
417 while (Count
-- > 0) {
418 IoWrite16 (Port
, *Buffer16
++);
423 Writes a block of memory into a 32-bit I/O port fifo.
425 Writes the 32-bit I/O fifo port specified by Port.
426 The port is written Count times, and the write data is
427 retrieved from the provided Buffer.
429 This function must guarantee that all I/O write and write operations are
432 If 32-bit I/O port operations are not supported, then ASSERT().
434 @param Port The I/O port to write.
435 @param Count The number of times to write I/O port.
436 @param Buffer The buffer to retrieve the write data from.
449 Buffer32
= (UINT32
*)Buffer
;
450 while (Count
-- > 0) {
451 IoWrite32 (Port
, *Buffer32
++);
456 Reads an 8-bit MMIO register.
458 Reads the 8-bit MMIO register specified by Address. The 8-bit read value is
459 returned. This function must guarantee that all MMIO read and write
460 operations are serialized.
462 If 8-bit MMIO register operations are not supported, then ASSERT().
464 @param Address The MMIO register to read.
466 @return The value read.
480 Data
= *((volatile UINT8
*) Address
);
487 Reads a 16-bit MMIO register.
489 Reads the 16-bit MMIO register specified by Address. The 16-bit read value is
490 returned. This function must guarantee that all MMIO read and write
491 operations are serialized.
493 If 16-bit MMIO register operations are not supported, then ASSERT().
494 If Address is not aligned on a 16-bit boundary, then ASSERT().
496 @param Address The MMIO register to read.
498 @return The value read.
510 // Make sure that Address is 16-bit aligned.
512 ASSERT ((Address
& 1) == 0);
517 Data
= *((volatile UINT16
*) Address
);
524 Reads a 32-bit MMIO register.
526 Reads the 32-bit MMIO register specified by Address. The 32-bit read value is
527 returned. This function must guarantee that all MMIO read and write
528 operations are serialized.
530 If 32-bit MMIO register operations are not supported, then ASSERT().
531 If Address is not aligned on a 32-bit boundary, then ASSERT().
533 @param Address The MMIO register to read.
535 @return The value read.
547 // Make sure that Address is 32-bit aligned.
549 ASSERT ((Address
& 3) == 0);
554 Data
= *((volatile UINT32
*) Address
);
561 Reads a 64-bit MMIO register.
563 Reads the 64-bit MMIO register specified by Address. The 64-bit read value is
564 returned. This function must guarantee that all MMIO read and write
565 operations are serialized.
567 If 64-bit MMIO register operations are not supported, then ASSERT().
568 If Address is not aligned on a 64-bit boundary, then ASSERT().
570 @param Address The MMIO register to read.
572 @return The value read.
584 // Make sure that Address is 64-bit aligned.
586 ASSERT ((Address
& 7) == 0);
591 Data
= *((volatile UINT64
*) Address
);
599 Writes an 8-bit MMIO register.
601 Writes the 8-bit MMIO register specified by Address with the value specified
602 by Value and returns Value. This function must guarantee that all MMIO read
603 and write operations are serialized.
605 If 8-bit MMIO register operations are not supported, then ASSERT().
607 @param Address The MMIO register to write.
608 @param Value The value to write to the MMIO register.
623 *((volatile UINT8
*) Address
) = Value
;
630 Writes a 16-bit MMIO register.
632 Writes the 16-bit MMIO register specified by Address with the value specified
633 by Value and returns Value. This function must guarantee that all MMIO read
634 and write operations are serialized.
636 If 16-bit MMIO register operations are not supported, then ASSERT().
637 If Address is not aligned on a 16-bit boundary, then ASSERT().
639 @param Address The MMIO register to write.
640 @param Value The value to write to the MMIO register.
653 // Make sure that Address is 16-bit aligned.
655 ASSERT ((Address
& 1) == 0);
660 *((volatile UINT16
*) Address
) = Value
;
667 Writes a 32-bit MMIO register.
669 Writes the 32-bit MMIO register specified by Address with the value specified
670 by Value and returns Value. This function must guarantee that all MMIO read
671 and write operations are serialized.
673 If 32-bit MMIO register operations are not supported, then ASSERT().
674 If Address is not aligned on a 32-bit boundary, then ASSERT().
676 @param Address The MMIO register to write.
677 @param Value The value to write to the MMIO register.
690 // Make sure that Address is 32-bit aligned.
692 ASSERT ((Address
& 3) == 0);
697 *((volatile UINT32
*) Address
) = Value
;
704 Writes a 64-bit MMIO register.
706 Writes the 64-bit MMIO register specified by Address with the value specified
707 by Value and returns Value. This function must guarantee that all MMIO read
708 and write operations are serialized.
710 If 64-bit MMIO register operations are not supported, then ASSERT().
711 If Address is not aligned on a 64-bit boundary, then ASSERT().
713 @param Address The MMIO register to write.
714 @param Value The value to write to the MMIO register.
725 // Make sure that Address is 64-bit aligned.
727 ASSERT ((Address
& 7) == 0);
732 *((volatile UINT64
*) Address
) = Value
;