Bin Meng | 091e51d | 2017-08-03 22:52:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2000-2002 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 8 | #ifndef _ASM_IO_H |
| 9 | #define _ASM_IO_H |
| 10 | |
Masahiro Yamada | afc366f | 2014-11-26 16:00:58 +0900 | [diff] [blame] | 11 | #include <linux/compiler.h> |
Gabe Black | ec516c4 | 2012-10-23 18:04:44 +0000 | [diff] [blame] | 12 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 13 | /* |
| 14 | * This file contains the definitions for the x86 IO instructions |
| 15 | * inb/inw/inl/outb/outw/outl and the "string versions" of the same |
| 16 | * (insb/insw/insl/outsb/outsw/outsl). You can also use "pausing" |
| 17 | * versions of the single-IO instructions (inb_p/inw_p/..). |
| 18 | * |
| 19 | * This file is not meant to be obfuscating: it's just complicated |
| 20 | * to (a) handle it all in a way that makes gcc able to optimize it |
| 21 | * as well as possible and (b) trying to avoid writing the same thing |
| 22 | * over and over again with slight variations and possibly making a |
| 23 | * mistake somewhere. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 27 | * Thanks to James van Artsdalen for a better timing-fix than |
| 28 | * the two short jumps: using outb's to a nonexistent port seems |
| 29 | * to guarantee better timings even on fast machines. |
| 30 | * |
| 31 | * On the other hand, I'd like to be sure of a non-existent port: |
| 32 | * I feel a bit unsafe about using 0x80 (should be safe, though) |
| 33 | * |
| 34 | * Linus |
| 35 | */ |
| 36 | |
| 37 | /* |
| 38 | * Bit simplified and optimized by Jan Hubicka |
| 39 | * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999. |
| 40 | * |
| 41 | * isa_memset_io, isa_memcpy_fromio, isa_memcpy_toio added, |
| 42 | * isa_read[wl] and isa_write[wl] fixed |
| 43 | * - Arnaldo Carvalho de Melo <acme@conectiva.com.br> |
| 44 | */ |
| 45 | |
| 46 | #define IO_SPACE_LIMIT 0xffff |
| 47 | |
Gabe Black | 687c108 | 2012-10-20 12:33:13 +0000 | [diff] [blame] | 48 | #include <asm/types.h> |
| 49 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 50 | |
| 51 | #ifdef __KERNEL__ |
| 52 | |
| 53 | |
| 54 | /* |
| 55 | * readX/writeX() are used to access memory mapped devices. On some |
| 56 | * architectures the memory mapped IO stuff needs to be accessed |
| 57 | * differently. On the x86 architecture, we just read/write the |
| 58 | * memory location directly. |
| 59 | */ |
| 60 | |
| 61 | #define readb(addr) (*(volatile unsigned char *) (addr)) |
| 62 | #define readw(addr) (*(volatile unsigned short *) (addr)) |
| 63 | #define readl(addr) (*(volatile unsigned int *) (addr)) |
Ivan Gorinov | 53cabe3 | 2018-04-06 14:43:04 -0700 | [diff] [blame] | 64 | #define readq(addr) (*(volatile unsigned long long *) (addr)) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 65 | #define __raw_readb readb |
| 66 | #define __raw_readw readw |
| 67 | #define __raw_readl readl |
Ivan Gorinov | 53cabe3 | 2018-04-06 14:43:04 -0700 | [diff] [blame] | 68 | #define __raw_readq readq |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 69 | |
| 70 | #define writeb(b,addr) (*(volatile unsigned char *) (addr) = (b)) |
| 71 | #define writew(b,addr) (*(volatile unsigned short *) (addr) = (b)) |
| 72 | #define writel(b,addr) (*(volatile unsigned int *) (addr) = (b)) |
Ivan Gorinov | 53cabe3 | 2018-04-06 14:43:04 -0700 | [diff] [blame] | 73 | #define writeq(b,addr) (*(volatile unsigned long long *) (addr) = (b)) |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 74 | #define __raw_writeb writeb |
| 75 | #define __raw_writew writew |
| 76 | #define __raw_writel writel |
Ivan Gorinov | 53cabe3 | 2018-04-06 14:43:04 -0700 | [diff] [blame] | 77 | #define __raw_writeq writeq |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 78 | |
| 79 | #define memset_io(a,b,c) memset((a),(b),(c)) |
| 80 | #define memcpy_fromio(a,b,c) memcpy((a),(b),(c)) |
| 81 | #define memcpy_toio(a,b,c) memcpy((a),(b),(c)) |
| 82 | |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 83 | #define out_arch(type, endian, a, v) __raw_write##type(cpu_to_##endian(v), a) |
| 84 | #define in_arch(type, endian, a) endian##_to_cpu(__raw_read##type(a)) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 85 | |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 86 | #define out_le64(a, v) out_arch(q, le64, a, v) |
| 87 | #define out_le32(a, v) out_arch(l, le32, a, v) |
| 88 | #define out_le16(a, v) out_arch(w, le16, a, v) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 89 | |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 90 | #define in_le64(a) in_arch(q, le64, a) |
| 91 | #define in_le32(a) in_arch(l, le32, a) |
| 92 | #define in_le16(a) in_arch(w, le16, a) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 93 | |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 94 | #define out_be32(a, v) out_arch(l, be32, a, v) |
| 95 | #define out_be16(a, v) out_arch(w, be16, a, v) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 96 | |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 97 | #define in_be32(a) in_arch(l, be32, a) |
| 98 | #define in_be16(a) in_arch(w, be16, a) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 99 | |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 100 | #define out_8(a, v) __raw_writeb(v, a) |
| 101 | #define in_8(a) __raw_readb(a) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 102 | |
| 103 | #define clrbits(type, addr, clear) \ |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 104 | out_##type((addr), in_##type(addr) & ~(clear)) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 105 | |
| 106 | #define setbits(type, addr, set) \ |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 107 | out_##type((addr), in_##type(addr) | (set)) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 108 | |
| 109 | #define clrsetbits(type, addr, clear, set) \ |
Lukasz Majewski | 014d7b1 | 2018-03-29 16:41:11 +0200 | [diff] [blame] | 110 | out_##type((addr), (in_##type(addr) & ~(clear)) | (set)) |
Simon Glass | 3f70a6f | 2014-11-12 22:42:17 -0700 | [diff] [blame] | 111 | |
| 112 | #define clrbits_be32(addr, clear) clrbits(be32, addr, clear) |
| 113 | #define setbits_be32(addr, set) setbits(be32, addr, set) |
| 114 | #define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set) |
| 115 | |
| 116 | #define clrbits_le32(addr, clear) clrbits(le32, addr, clear) |
| 117 | #define setbits_le32(addr, set) setbits(le32, addr, set) |
| 118 | #define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set) |
| 119 | |
| 120 | #define clrbits_be16(addr, clear) clrbits(be16, addr, clear) |
| 121 | #define setbits_be16(addr, set) setbits(be16, addr, set) |
| 122 | #define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set) |
| 123 | |
| 124 | #define clrbits_le16(addr, clear) clrbits(le16, addr, clear) |
| 125 | #define setbits_le16(addr, set) setbits(le16, addr, set) |
| 126 | #define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set) |
| 127 | |
| 128 | #define clrbits_8(addr, clear) clrbits(8, addr, clear) |
| 129 | #define setbits_8(addr, set) setbits(8, addr, set) |
| 130 | #define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set) |
| 131 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 132 | #endif /* __KERNEL__ */ |
| 133 | |
| 134 | #ifdef SLOW_IO_BY_JUMPING |
| 135 | #define __SLOW_DOWN_IO "\njmp 1f\n1:\tjmp 1f\n1:" |
| 136 | #else |
Stefan Reinauer | 339c511 | 2012-10-20 12:33:16 +0000 | [diff] [blame] | 137 | #define __SLOW_DOWN_IO "\noutb %%al,$0xed" |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 138 | #endif |
| 139 | |
| 140 | #ifdef REALLY_SLOW_IO |
| 141 | #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO __SLOW_DOWN_IO |
| 142 | #else |
| 143 | #define __FULL_SLOW_DOWN_IO __SLOW_DOWN_IO |
| 144 | #endif |
| 145 | |
| 146 | |
| 147 | /* |
| 148 | * Talk about misusing macros.. |
| 149 | */ |
| 150 | #define __OUT1(s,x) \ |
Simon Glass | 005174d | 2016-03-11 22:07:07 -0700 | [diff] [blame] | 151 | static inline void _out##s(unsigned x value, unsigned short port) { |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 152 | |
| 153 | #define __OUT2(s,s1,s2) \ |
| 154 | __asm__ __volatile__ ("out" #s " %" s1 "0,%" s2 "1" |
| 155 | |
| 156 | |
| 157 | #define __OUT(s,s1,x) \ |
| 158 | __OUT1(s,x) __OUT2(s,s1,"w") : : "a" (value), "Nd" (port)); } \ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | __OUT1(s##_p,x) __OUT2(s,s1,"w") __FULL_SLOW_DOWN_IO : : "a" (value), "Nd" (port));} |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 160 | |
| 161 | #define __IN1(s) \ |
Simon Glass | 005174d | 2016-03-11 22:07:07 -0700 | [diff] [blame] | 162 | static inline RETURN_TYPE _in##s(unsigned short port) { RETURN_TYPE _v; |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 163 | |
| 164 | #define __IN2(s,s1,s2) \ |
| 165 | __asm__ __volatile__ ("in" #s " %" s2 "1,%" s1 "0" |
| 166 | |
| 167 | #define __IN(s,s1,i...) \ |
| 168 | __IN1(s) __IN2(s,s1,"w") : "=a" (_v) : "Nd" (port) ,##i ); return _v; } \ |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 169 | __IN1(s##_p) __IN2(s,s1,"w") __FULL_SLOW_DOWN_IO : "=a" (_v) : "Nd" (port) ,##i ); return _v; } |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 170 | |
| 171 | #define __INS(s) \ |
| 172 | static inline void ins##s(unsigned short port, void * addr, unsigned long count) \ |
| 173 | { __asm__ __volatile__ ("rep ; ins" #s \ |
| 174 | : "=D" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); } |
| 175 | |
| 176 | #define __OUTS(s) \ |
| 177 | static inline void outs##s(unsigned short port, const void * addr, unsigned long count) \ |
| 178 | { __asm__ __volatile__ ("rep ; outs" #s \ |
| 179 | : "=S" (addr), "=c" (count) : "d" (port),"0" (addr),"1" (count)); } |
| 180 | |
| 181 | #define RETURN_TYPE unsigned char |
| 182 | __IN(b,"") |
| 183 | #undef RETURN_TYPE |
| 184 | #define RETURN_TYPE unsigned short |
| 185 | __IN(w,"") |
| 186 | #undef RETURN_TYPE |
| 187 | #define RETURN_TYPE unsigned int |
| 188 | __IN(l,"") |
| 189 | #undef RETURN_TYPE |
| 190 | |
Simon Glass | 005174d | 2016-03-11 22:07:07 -0700 | [diff] [blame] | 191 | #define inb(port) _inb((uintptr_t)(port)) |
| 192 | #define inw(port) _inw((uintptr_t)(port)) |
| 193 | #define inl(port) _inl((uintptr_t)(port)) |
| 194 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 195 | __OUT(b,"b",char) |
| 196 | __OUT(w,"w",short) |
| 197 | __OUT(l,,int) |
| 198 | |
Simon Glass | 005174d | 2016-03-11 22:07:07 -0700 | [diff] [blame] | 199 | #define outb(val, port) _outb(val, (uintptr_t)(port)) |
| 200 | #define outw(val, port) _outw(val, (uintptr_t)(port)) |
| 201 | #define outl(val, port) _outl(val, (uintptr_t)(port)) |
| 202 | |
wdenk | 2262cfe | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 203 | __INS(b) |
| 204 | __INS(w) |
| 205 | __INS(l) |
| 206 | |
| 207 | __OUTS(b) |
| 208 | __OUTS(w) |
| 209 | __OUTS(l) |
| 210 | |
Simon Glass | 30928c1 | 2016-03-11 22:07:06 -0700 | [diff] [blame] | 211 | /* IO space accessors */ |
| 212 | #define clrio(type, addr, clear) \ |
| 213 | out##type(in##type(addr) & ~(clear), (addr)) |
| 214 | |
| 215 | #define setio(type, addr, set) \ |
| 216 | out##type(in##type(addr) | (set), (addr)) |
| 217 | |
| 218 | #define clrsetio(type, addr, clear, set) \ |
| 219 | out##type((in##type(addr) & ~(clear)) | (set), (addr)) |
| 220 | |
| 221 | #define clrio_32(addr, clear) clrio(l, addr, clear) |
| 222 | #define clrio_16(addr, clear) clrio(w, addr, clear) |
| 223 | #define clrio_8(addr, clear) clrio(b, addr, clear) |
| 224 | |
| 225 | #define setio_32(addr, set) setio(l, addr, set) |
| 226 | #define setio_16(addr, set) setio(w, addr, set) |
| 227 | #define setio_8(addr, set) setio(b, addr, set) |
| 228 | |
| 229 | #define clrsetio_32(addr, clear, set) clrsetio(l, addr, clear, set) |
| 230 | #define clrsetio_16(addr, clear, set) clrsetio(w, addr, clear, set) |
| 231 | #define clrsetio_8(addr, clear, set) clrsetio(b, addr, clear, set) |
| 232 | |
Haiying Wang | 3a197b2 | 2007-02-21 16:52:31 +0100 | [diff] [blame] | 233 | static inline void sync(void) |
| 234 | { |
| 235 | } |
| 236 | |
Haavard Skinnemoen | 4d7d693 | 2007-12-13 12:56:33 +0100 | [diff] [blame] | 237 | /* |
Simon Glass | 8a487a4 | 2012-10-10 13:12:53 +0000 | [diff] [blame] | 238 | * TODO: The kernel offers some more advanced versions of barriers, it might |
| 239 | * have some advantages to use them instead of the simple one here. |
| 240 | */ |
| 241 | #define dmb() __asm__ __volatile__ ("" : : : "memory") |
| 242 | #define __iormb() dmb() |
| 243 | #define __iowmb() dmb() |
| 244 | |
Paul Burton | 6f01316 | 2017-09-14 15:05:08 -0700 | [diff] [blame] | 245 | #include <asm-generic/io.h> |
| 246 | |
Bin Meng | 091e51d | 2017-08-03 22:52:44 -0700 | [diff] [blame] | 247 | #endif /* _ASM_IO_H */ |