1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * I/O device access primitives. Simplified based on related U-Boot code,
5 * which is in turn based on early versions from the Linux kernel:
6 *
7 * Copyright (C) 1996-2000 Russell King
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14 #ifndef __ARCH_GENERIC_IO_H__
15 #define __ARCH_GENERIC_IO_H__
16
17 #include <stdint.h>
18 #include <stddef.h>
19 #include <endian.h>
20 #include <arch/mmio.h>
21
22 #define __io(a) (void *)(uintptr_t)(CONFIG_PCI_IOBASE + a)
23
outb(uint8_t value,uint16_t port)24 static inline void outb(uint8_t value, uint16_t port)
25 {
26 write8(__io(port), value);
27 }
28
outw(uint16_t value,uint16_t port)29 static inline void outw(uint16_t value, uint16_t port)
30 {
31 write16(__io(port), cpu_to_le16(value));
32 }
33
outl(uint32_t value,uint16_t port)34 static inline void outl(uint32_t value, uint16_t port)
35 {
36 write32(__io(port), cpu_to_le32(value));
37 }
38
inb(uint16_t port)39 static inline uint8_t inb(uint16_t port)
40 {
41 return read8(__io(port));
42 }
43
inw(uint16_t port)44 static inline uint16_t inw(uint16_t port)
45 {
46 return le16_to_cpu(read16(__io(port)));
47 }
48
inl(uint16_t port)49 static inline uint32_t inl(uint16_t port)
50 {
51 return le32_to_cpu(read32(__io(port)));
52 }
53
outsb(uint16_t port,const void * addr,unsigned long count)54 static inline void outsb(uint16_t port, const void *addr, unsigned long count)
55 {
56 uint8_t *buf = (uint8_t *)addr;
57 while (count--)
58 write8(__io(port), *buf++);
59 }
60
outsw(uint16_t port,const void * addr,unsigned long count)61 static inline void outsw(uint16_t port, const void *addr, unsigned long count)
62 {
63 uint16_t *buf = (uint16_t *)addr;
64 while (count--)
65 write16(__io(port), *buf++);
66 }
67
outsl(uint16_t port,const void * addr,unsigned long count)68 static inline void outsl(uint16_t port, const void *addr, unsigned long count)
69 {
70 uint32_t *buf = (uint32_t *)addr;
71 while (count--)
72 write32(__io(port), *buf++);
73 }
74
insb(uint16_t port,void * addr,unsigned long count)75 static inline void insb(uint16_t port, void *addr, unsigned long count)
76 {
77 uint8_t *buf = (uint8_t *)addr;
78 while (count--)
79 *buf++ = read8(__io(port));
80 }
81
insw(uint16_t port,void * addr,unsigned long count)82 static inline void insw(uint16_t port, void *addr, unsigned long count)
83 {
84 uint16_t *buf = (uint16_t *)addr;
85 while (count--)
86 *buf++ = read16(__io(port));
87 }
88
insl(uint16_t port,void * addr,unsigned long count)89 static inline void insl(uint16_t port, void *addr, unsigned long count)
90 {
91 uint32_t *buf = (uint32_t *)addr;
92 while (count--)
93 *buf++ = read32(__io(port));
94 }
95
96 #endif
97