Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
Simon Glass | b4a905e | 2011-10-10 08:22:14 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2011 The Chromium OS Authors. |
Simon Glass | b4a905e | 2011-10-10 08:22:14 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 62cf912 | 2013-04-26 02:53:43 +0000 | [diff] [blame] | 6 | #ifndef __SANDBOX_ASM_IO_H |
| 7 | #define __SANDBOX_ASM_IO_H |
| 8 | |
Tom Rini | ada64ac | 2023-12-14 13:16:46 -0500 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 11 | enum sandboxio_size_t { |
| 12 | SB_SIZE_8, |
| 13 | SB_SIZE_16, |
| 14 | SB_SIZE_32, |
| 15 | SB_SIZE_64, |
| 16 | }; |
| 17 | |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 18 | void *phys_to_virt(phys_addr_t paddr); |
| 19 | #define phys_to_virt phys_to_virt |
| 20 | |
| 21 | phys_addr_t virt_to_phys(void *vaddr); |
| 22 | #define virt_to_phys virt_to_phys |
Simon Glass | b4a905e | 2011-10-10 08:22:14 +0000 | [diff] [blame] | 23 | |
| 24 | void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags); |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 25 | #define map_physmem map_physmem |
Simon Glass | b4a905e | 2011-10-10 08:22:14 +0000 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * Take down a mapping set up by map_physmem(). |
| 29 | */ |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 30 | void unmap_physmem(const void *vaddr, unsigned long flags); |
Paul Burton | 5136925 | 2017-09-14 15:05:13 -0700 | [diff] [blame] | 31 | #define unmap_physmem unmap_physmem |
| 32 | |
Simon Glass | e23d293 | 2013-04-20 08:42:37 +0000 | [diff] [blame] | 33 | /* Map from a pointer to our RAM buffer */ |
Simon Glass | 656bfc4 | 2013-11-07 09:31:58 -0700 | [diff] [blame] | 34 | phys_addr_t map_to_sysmem(const void *ptr); |
Simon Glass | 62cf912 | 2013-04-26 02:53:43 +0000 | [diff] [blame] | 35 | |
Abdellatif El Khlifi | 32f7295 | 2023-04-17 10:11:54 +0100 | [diff] [blame] | 36 | unsigned long sandbox_read(const void *addr, enum sandboxio_size_t size); |
Simon Glass | d80cda2 | 2019-10-11 16:16:47 -0600 | [diff] [blame] | 37 | void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size); |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 38 | |
| 39 | #define readb(addr) sandbox_read((const void *)addr, SB_SIZE_8) |
| 40 | #define readw(addr) sandbox_read((const void *)addr, SB_SIZE_16) |
| 41 | #define readl(addr) sandbox_read((const void *)addr, SB_SIZE_32) |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 42 | #ifdef CONFIG_SANDBOX64 |
Simon Glass | 0a341a7 | 2019-09-25 08:56:09 -0600 | [diff] [blame] | 43 | #define readq(addr) sandbox_read((const void *)addr, SB_SIZE_64) |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 44 | #endif |
Simon Glass | d80cda2 | 2019-10-11 16:16:47 -0600 | [diff] [blame] | 45 | #define writeb(v, addr) sandbox_write((void *)addr, v, SB_SIZE_8) |
| 46 | #define writew(v, addr) sandbox_write((void *)addr, v, SB_SIZE_16) |
| 47 | #define writel(v, addr) sandbox_write((void *)addr, v, SB_SIZE_32) |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 48 | #ifdef CONFIG_SANDBOX64 |
Simon Glass | d80cda2 | 2019-10-11 16:16:47 -0600 | [diff] [blame] | 49 | #define writeq(v, addr) sandbox_write((void *)addr, v, SB_SIZE_64) |
Mario Six | 61efece | 2018-02-12 08:05:57 +0100 | [diff] [blame] | 50 | #endif |
Simon Glass | 125a27b | 2014-06-11 23:29:43 -0600 | [diff] [blame] | 51 | |
Tom Rini | 3662947 | 2017-09-29 08:53:53 -0400 | [diff] [blame] | 52 | /* |
| 53 | * Clear and set bits in one shot. These macros can be used to clear and |
| 54 | * set multiple bits in a register using a single call. These macros can |
| 55 | * also be used to set a multiple-bit bit pattern using a mask, by |
| 56 | * specifying the mask in the 'clear' parameter and the new bit pattern |
| 57 | * in the 'set' parameter. |
| 58 | */ |
| 59 | |
| 60 | #define out_arch(type,endian,a,v) write##type(cpu_to_##endian(v),a) |
| 61 | #define in_arch(type,endian,a) endian##_to_cpu(read##type(a)) |
| 62 | |
| 63 | #define out_le64(a,v) out_arch(q,le64,a,v) |
| 64 | #define out_le32(a,v) out_arch(l,le32,a,v) |
| 65 | #define out_le16(a,v) out_arch(w,le16,a,v) |
| 66 | |
| 67 | #define in_le64(a) in_arch(q,le64,a) |
| 68 | #define in_le32(a) in_arch(l,le32,a) |
| 69 | #define in_le16(a) in_arch(w,le16,a) |
| 70 | |
| 71 | #define out_be32(a,v) out_arch(l,be32,a,v) |
| 72 | #define out_be16(a,v) out_arch(w,be16,a,v) |
| 73 | |
| 74 | #define in_be32(a) in_arch(l,be32,a) |
| 75 | #define in_be16(a) in_arch(w,be16,a) |
| 76 | |
| 77 | #define out_8(a,v) writeb(v,a) |
| 78 | #define in_8(a) readb(a) |
| 79 | |
| 80 | #define clrbits(type, addr, clear) \ |
| 81 | out_##type((addr), in_##type(addr) & ~(clear)) |
| 82 | |
| 83 | #define setbits(type, addr, set) \ |
| 84 | out_##type((addr), in_##type(addr) | (set)) |
| 85 | |
| 86 | #define clrsetbits(type, addr, clear, set) \ |
| 87 | out_##type((addr), (in_##type(addr) & ~(clear)) | (set)) |
| 88 | |
| 89 | #define clrbits_be32(addr, clear) clrbits(be32, addr, clear) |
| 90 | #define setbits_be32(addr, set) setbits(be32, addr, set) |
| 91 | #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set) |
| 92 | |
| 93 | #define clrbits_le32(addr, clear) clrbits(le32, addr, clear) |
| 94 | #define setbits_le32(addr, set) setbits(le32, addr, set) |
| 95 | #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set) |
| 96 | |
| 97 | #define clrbits_be16(addr, clear) clrbits(be16, addr, clear) |
| 98 | #define setbits_be16(addr, set) setbits(be16, addr, set) |
| 99 | #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set) |
| 100 | |
| 101 | #define clrbits_le16(addr, clear) clrbits(le16, addr, clear) |
| 102 | #define setbits_le16(addr, set) setbits(le16, addr, set) |
| 103 | #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set) |
| 104 | |
| 105 | #define clrbits_8(addr, clear) clrbits(8, addr, clear) |
| 106 | #define setbits_8(addr, set) setbits(8, addr, set) |
| 107 | #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set) |
| 108 | |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 109 | /* I/O access functions */ |
Simon Glass | ca6f121 | 2019-09-25 08:56:02 -0600 | [diff] [blame] | 110 | int _inl(unsigned int addr); |
| 111 | int _inw(unsigned int addr); |
| 112 | int _inb(unsigned int addr); |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 113 | |
Simon Glass | ca6f121 | 2019-09-25 08:56:02 -0600 | [diff] [blame] | 114 | void _outl(unsigned int value, unsigned int addr); |
| 115 | void _outw(unsigned int value, unsigned int addr); |
| 116 | void _outb(unsigned int value, unsigned int addr); |
| 117 | |
| 118 | #define inb(port) _inb((uintptr_t)(port)) |
| 119 | #define inw(port) _inw((uintptr_t)(port)) |
| 120 | #define inl(port) _inl((uintptr_t)(port)) |
| 121 | |
| 122 | #define outb(val, port) _outb(val, (uintptr_t)(port)) |
| 123 | #define outw(val, port) _outw(val, (uintptr_t)(port)) |
| 124 | #define outl(val, port) _outl(val, (uintptr_t)(port)) |
Simon Glass | 70778bc | 2015-03-05 12:25:26 -0700 | [diff] [blame] | 125 | |
Maxime Ripard | f768aa8 | 2017-10-02 15:25:56 +0200 | [diff] [blame] | 126 | #define out_arch(type,endian,a,v) write##type(cpu_to_##endian(v),a) |
| 127 | #define in_arch(type,endian,a) endian##_to_cpu(read##type(a)) |
| 128 | |
| 129 | #define out_le32(a,v) out_arch(l,le32,a,v) |
| 130 | #define out_le16(a,v) out_arch(w,le16,a,v) |
| 131 | |
| 132 | #define in_le32(a) in_arch(l,le32,a) |
| 133 | #define in_le16(a) in_arch(w,le16,a) |
| 134 | |
| 135 | #define out_be32(a,v) out_arch(l,be32,a,v) |
| 136 | #define out_be16(a,v) out_arch(w,be16,a,v) |
| 137 | |
| 138 | #define in_be32(a) in_arch(l,be32,a) |
| 139 | #define in_be16(a) in_arch(w,be16,a) |
| 140 | |
| 141 | #define out_8(a,v) writeb(v,a) |
| 142 | #define in_8(a) readb(a) |
| 143 | |
| 144 | #define clrbits(type, addr, clear) \ |
| 145 | out_##type((addr), in_##type(addr) & ~(clear)) |
| 146 | |
| 147 | #define setbits(type, addr, set) \ |
| 148 | out_##type((addr), in_##type(addr) | (set)) |
| 149 | |
| 150 | #define clrsetbits(type, addr, clear, set) \ |
| 151 | out_##type((addr), (in_##type(addr) & ~(clear)) | (set)) |
| 152 | |
| 153 | #define clrbits_be32(addr, clear) clrbits(be32, addr, clear) |
| 154 | #define setbits_be32(addr, set) setbits(be32, addr, set) |
| 155 | #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set) |
| 156 | |
| 157 | #define clrbits_le32(addr, clear) clrbits(le32, addr, clear) |
| 158 | #define setbits_le32(addr, set) setbits(le32, addr, set) |
| 159 | #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set) |
| 160 | |
| 161 | #define clrbits_be16(addr, clear) clrbits(be16, addr, clear) |
| 162 | #define setbits_be16(addr, set) setbits(be16, addr, set) |
| 163 | #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set) |
| 164 | |
| 165 | #define clrbits_le16(addr, clear) clrbits(le16, addr, clear) |
| 166 | #define setbits_le16(addr, set) setbits(le16, addr, set) |
| 167 | #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set) |
| 168 | |
| 169 | #define clrbits_8(addr, clear) clrbits(8, addr, clear) |
| 170 | #define setbits_8(addr, set) setbits(8, addr, set) |
| 171 | #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set) |
| 172 | |
Simon Glass | 8d5e245 | 2016-05-01 11:35:54 -0600 | [diff] [blame] | 173 | static inline void _insw(volatile u16 *port, void *buf, int ns) |
| 174 | { |
| 175 | } |
| 176 | |
| 177 | static inline void _outsw(volatile u16 *port, const void *buf, int ns) |
| 178 | { |
| 179 | } |
| 180 | |
Christian GMEINER | 048b2f4 | 2018-12-04 20:35:24 +0100 | [diff] [blame] | 181 | static inline void memset_io(volatile void *addr, unsigned char val, int count) |
| 182 | { |
| 183 | } |
| 184 | |
| 185 | static inline void memcpy_fromio(void *dst, const volatile void *src, int count) |
| 186 | { |
| 187 | } |
| 188 | |
| 189 | static inline void memcpy_toio(volatile void *dst, const void *src, int count) |
| 190 | { |
| 191 | } |
| 192 | |
Simon Glass | 8d5e245 | 2016-05-01 11:35:54 -0600 | [diff] [blame] | 193 | #define insw(port, buf, ns) _insw((u16 *)port, buf, ns) |
| 194 | #define outsw(port, buf, ns) _outsw((u16 *)port, buf, ns) |
| 195 | |
Simon Glass | ca6f121 | 2019-09-25 08:56:02 -0600 | [diff] [blame] | 196 | /* IO space accessors */ |
| 197 | #define clrio(type, addr, clear) \ |
| 198 | out##type(in##type(addr) & ~(clear), (addr)) |
| 199 | |
| 200 | #define setio(type, addr, set) \ |
| 201 | out##type(in##type(addr) | (set), (addr)) |
| 202 | |
| 203 | #define clrsetio(type, addr, clear, set) \ |
| 204 | out##type((in##type(addr) & ~(clear)) | (set), (addr)) |
| 205 | |
| 206 | #define clrio_32(addr, clear) clrio(l, addr, clear) |
| 207 | #define clrio_16(addr, clear) clrio(w, addr, clear) |
| 208 | #define clrio_8(addr, clear) clrio(b, addr, clear) |
| 209 | |
| 210 | #define setio_32(addr, set) setio(l, addr, set) |
| 211 | #define setio_16(addr, set) setio(w, addr, set) |
| 212 | #define setio_8(addr, set) setio(b, addr, set) |
| 213 | |
| 214 | #define clrsetio_32(addr, clear, set) clrsetio(l, addr, clear, set) |
| 215 | #define clrsetio_16(addr, clear, set) clrsetio(w, addr, clear, set) |
| 216 | #define clrsetio_8(addr, clear, set) clrsetio(b, addr, clear, set) |
| 217 | |
Simon Glass | 125a27b | 2014-06-11 23:29:43 -0600 | [diff] [blame] | 218 | #include <iotrace.h> |
Tom Rini | 22e4268 | 2016-03-15 13:20:23 -0400 | [diff] [blame] | 219 | #include <asm/types.h> |
Igor Prusov | 91341f8 | 2023-11-14 14:02:45 +0300 | [diff] [blame] | 220 | #include <asm-generic/io.h> |
| 221 | |
| 222 | /* For sandbox, we want addresses to point into our RAM buffer */ |
| 223 | static inline void *map_sysmem(phys_addr_t paddr, unsigned long len) |
| 224 | { |
| 225 | return map_physmem(paddr, len, MAP_WRBACK); |
| 226 | } |
| 227 | |
| 228 | /* Remove a previous mapping */ |
| 229 | static inline void unmap_sysmem(const void *vaddr) |
| 230 | { |
| 231 | unmap_physmem(vaddr, MAP_WRBACK); |
| 232 | } |
| 233 | |
Simon Glass | 125a27b | 2014-06-11 23:29:43 -0600 | [diff] [blame] | 234 | |
Simon Glass | 62cf912 | 2013-04-26 02:53:43 +0000 | [diff] [blame] | 235 | #endif |