Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 1 | /* Generic I/O port emulation, based on MN10300 code |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | #ifndef __ASM_GENERIC_IO_H |
| 12 | #define __ASM_GENERIC_IO_H |
| 13 | |
| 14 | #include <asm/page.h> /* I/O is all done through memory accesses */ |
| 15 | #include <asm/cacheflush.h> |
| 16 | #include <linux/types.h> |
| 17 | |
| 18 | #ifdef CONFIG_GENERIC_IOMAP |
| 19 | #include <asm-generic/iomap.h> |
| 20 | #endif |
| 21 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 22 | #ifndef mmiowb |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 23 | #define mmiowb() do {} while (0) |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 24 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 25 | |
| 26 | /*****************************************************************************/ |
| 27 | /* |
| 28 | * readX/writeX() are used to access memory mapped devices. On some |
| 29 | * architectures the memory mapped IO stuff needs to be accessed |
| 30 | * differently. On the simple architectures, we just read/write the |
| 31 | * memory location directly. |
| 32 | */ |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 33 | #ifndef __raw_readb |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 34 | static inline u8 __raw_readb(const volatile void __iomem *addr) |
| 35 | { |
| 36 | return *(const volatile u8 __force *) addr; |
| 37 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 38 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 39 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 40 | #ifndef __raw_readw |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 41 | static inline u16 __raw_readw(const volatile void __iomem *addr) |
| 42 | { |
| 43 | return *(const volatile u16 __force *) addr; |
| 44 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 45 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 46 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 47 | #ifndef __raw_readl |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 48 | static inline u32 __raw_readl(const volatile void __iomem *addr) |
| 49 | { |
| 50 | return *(const volatile u32 __force *) addr; |
| 51 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 52 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 53 | |
| 54 | #define readb __raw_readb |
| 55 | #define readw(addr) __le16_to_cpu(__raw_readw(addr)) |
| 56 | #define readl(addr) __le32_to_cpu(__raw_readl(addr)) |
| 57 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 58 | #ifndef __raw_writeb |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 59 | static inline void __raw_writeb(u8 b, volatile void __iomem *addr) |
| 60 | { |
| 61 | *(volatile u8 __force *) addr = b; |
| 62 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 63 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 64 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 65 | #ifndef __raw_writew |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 66 | static inline void __raw_writew(u16 b, volatile void __iomem *addr) |
| 67 | { |
| 68 | *(volatile u16 __force *) addr = b; |
| 69 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 70 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 71 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 72 | #ifndef __raw_writel |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 73 | static inline void __raw_writel(u32 b, volatile void __iomem *addr) |
| 74 | { |
| 75 | *(volatile u32 __force *) addr = b; |
| 76 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 77 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 78 | |
| 79 | #define writeb __raw_writeb |
| 80 | #define writew(b,addr) __raw_writew(__cpu_to_le16(b),addr) |
| 81 | #define writel(b,addr) __raw_writel(__cpu_to_le32(b),addr) |
| 82 | |
| 83 | #ifdef CONFIG_64BIT |
| 84 | static inline u64 __raw_readq(const volatile void __iomem *addr) |
| 85 | { |
| 86 | return *(const volatile u64 __force *) addr; |
| 87 | } |
| 88 | #define readq(addr) __le64_to_cpu(__raw_readq(addr)) |
| 89 | |
| 90 | static inline void __raw_writeq(u64 b, volatile void __iomem *addr) |
| 91 | { |
| 92 | *(volatile u64 __force *) addr = b; |
| 93 | } |
| 94 | #define writeq(b,addr) __raw_writeq(__cpu_to_le64(b),addr) |
| 95 | #endif |
| 96 | |
| 97 | /*****************************************************************************/ |
| 98 | /* |
| 99 | * traditional input/output functions |
| 100 | */ |
| 101 | |
| 102 | static inline u8 inb(unsigned long addr) |
| 103 | { |
| 104 | return readb((volatile void __iomem *) addr); |
| 105 | } |
| 106 | |
| 107 | static inline u16 inw(unsigned long addr) |
| 108 | { |
| 109 | return readw((volatile void __iomem *) addr); |
| 110 | } |
| 111 | |
| 112 | static inline u32 inl(unsigned long addr) |
| 113 | { |
| 114 | return readl((volatile void __iomem *) addr); |
| 115 | } |
| 116 | |
| 117 | static inline void outb(u8 b, unsigned long addr) |
| 118 | { |
| 119 | writeb(b, (volatile void __iomem *) addr); |
| 120 | } |
| 121 | |
| 122 | static inline void outw(u16 b, unsigned long addr) |
| 123 | { |
| 124 | writew(b, (volatile void __iomem *) addr); |
| 125 | } |
| 126 | |
| 127 | static inline void outl(u32 b, unsigned long addr) |
| 128 | { |
| 129 | writel(b, (volatile void __iomem *) addr); |
| 130 | } |
| 131 | |
| 132 | #define inb_p(addr) inb(addr) |
| 133 | #define inw_p(addr) inw(addr) |
| 134 | #define inl_p(addr) inl(addr) |
| 135 | #define outb_p(x, addr) outb((x), (addr)) |
| 136 | #define outw_p(x, addr) outw((x), (addr)) |
| 137 | #define outl_p(x, addr) outl((x), (addr)) |
| 138 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 139 | #ifndef insb |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 140 | static inline void insb(unsigned long addr, void *buffer, int count) |
| 141 | { |
| 142 | if (count) { |
| 143 | u8 *buf = buffer; |
| 144 | do { |
| 145 | u8 x = inb(addr); |
| 146 | *buf++ = x; |
| 147 | } while (--count); |
| 148 | } |
| 149 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 150 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 151 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 152 | #ifndef insw |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 153 | static inline void insw(unsigned long addr, void *buffer, int count) |
| 154 | { |
| 155 | if (count) { |
| 156 | u16 *buf = buffer; |
| 157 | do { |
| 158 | u16 x = inw(addr); |
| 159 | *buf++ = x; |
| 160 | } while (--count); |
| 161 | } |
| 162 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 163 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 164 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 165 | #ifndef insl |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 166 | static inline void insl(unsigned long addr, void *buffer, int count) |
| 167 | { |
| 168 | if (count) { |
| 169 | u32 *buf = buffer; |
| 170 | do { |
| 171 | u32 x = inl(addr); |
| 172 | *buf++ = x; |
| 173 | } while (--count); |
| 174 | } |
| 175 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 176 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 177 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 178 | #ifndef outsb |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 179 | static inline void outsb(unsigned long addr, const void *buffer, int count) |
| 180 | { |
| 181 | if (count) { |
| 182 | const u8 *buf = buffer; |
| 183 | do { |
| 184 | outb(*buf++, addr); |
| 185 | } while (--count); |
| 186 | } |
| 187 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 188 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 189 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 190 | #ifndef outsw |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 191 | static inline void outsw(unsigned long addr, const void *buffer, int count) |
| 192 | { |
| 193 | if (count) { |
| 194 | const u16 *buf = buffer; |
| 195 | do { |
| 196 | outw(*buf++, addr); |
| 197 | } while (--count); |
| 198 | } |
| 199 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 200 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 201 | |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 202 | #ifndef outsl |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 203 | static inline void outsl(unsigned long addr, const void *buffer, int count) |
| 204 | { |
| 205 | if (count) { |
| 206 | const u32 *buf = buffer; |
| 207 | do { |
| 208 | outl(*buf++, addr); |
| 209 | } while (--count); |
| 210 | } |
| 211 | } |
Mike Frysinger | 35dbc0e | 2010-10-18 03:09:39 -0400 | [diff] [blame] | 212 | #endif |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 213 | |
| 214 | #ifndef CONFIG_GENERIC_IOMAP |
| 215 | #define ioread8(addr) readb(addr) |
| 216 | #define ioread16(addr) readw(addr) |
Mike Frysinger | 7387be3 | 2010-08-09 17:20:17 -0700 | [diff] [blame] | 217 | #define ioread16be(addr) be16_to_cpu(ioread16(addr)) |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 218 | #define ioread32(addr) readl(addr) |
Mike Frysinger | 7387be3 | 2010-08-09 17:20:17 -0700 | [diff] [blame] | 219 | #define ioread32be(addr) be32_to_cpu(ioread32(addr)) |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 220 | |
| 221 | #define iowrite8(v, addr) writeb((v), (addr)) |
| 222 | #define iowrite16(v, addr) writew((v), (addr)) |
Mike Frysinger | 7387be3 | 2010-08-09 17:20:17 -0700 | [diff] [blame] | 223 | #define iowrite16be(v, addr) iowrite16(be16_to_cpu(v), (addr)) |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 224 | #define iowrite32(v, addr) writel((v), (addr)) |
Mike Frysinger | 7387be3 | 2010-08-09 17:20:17 -0700 | [diff] [blame] | 225 | #define iowrite32be(v, addr) iowrite32(be32_to_cpu(v), (addr)) |
Arnd Bergmann | 3f7e212 | 2009-05-13 22:56:35 +0000 | [diff] [blame] | 226 | |
| 227 | #define ioread8_rep(p, dst, count) \ |
| 228 | insb((unsigned long) (p), (dst), (count)) |
| 229 | #define ioread16_rep(p, dst, count) \ |
| 230 | insw((unsigned long) (p), (dst), (count)) |
| 231 | #define ioread32_rep(p, dst, count) \ |
| 232 | insl((unsigned long) (p), (dst), (count)) |
| 233 | |
| 234 | #define iowrite8_rep(p, src, count) \ |
| 235 | outsb((unsigned long) (p), (src), (count)) |
| 236 | #define iowrite16_rep(p, src, count) \ |
| 237 | outsw((unsigned long) (p), (src), (count)) |
| 238 | #define iowrite32_rep(p, src, count) \ |
| 239 | outsl((unsigned long) (p), (src), (count)) |
| 240 | #endif /* CONFIG_GENERIC_IOMAP */ |
| 241 | |
| 242 | |
| 243 | #define IO_SPACE_LIMIT 0xffffffff |
| 244 | |
| 245 | #ifdef __KERNEL__ |
| 246 | |
| 247 | #include <linux/vmalloc.h> |
| 248 | #define __io_virt(x) ((void __force *) (x)) |
| 249 | |
| 250 | #ifndef CONFIG_GENERIC_IOMAP |
| 251 | /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */ |
| 252 | struct pci_dev; |
| 253 | extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max); |
| 254 | static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p) |
| 255 | { |
| 256 | } |
| 257 | #endif /* CONFIG_GENERIC_IOMAP */ |
| 258 | |
| 259 | /* |
| 260 | * Change virtual addresses to physical addresses and vv. |
| 261 | * These are pretty trivial |
| 262 | */ |
| 263 | static inline unsigned long virt_to_phys(volatile void *address) |
| 264 | { |
| 265 | return __pa((unsigned long)address); |
| 266 | } |
| 267 | |
| 268 | static inline void *phys_to_virt(unsigned long address) |
| 269 | { |
| 270 | return __va(address); |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * Change "struct page" to physical address. |
| 275 | */ |
| 276 | static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size) |
| 277 | { |
| 278 | return (void __iomem*) (unsigned long)offset; |
| 279 | } |
| 280 | |
| 281 | #define __ioremap(offset, size, flags) ioremap(offset, size) |
| 282 | |
| 283 | #ifndef ioremap_nocache |
| 284 | #define ioremap_nocache ioremap |
| 285 | #endif |
| 286 | |
| 287 | #ifndef ioremap_wc |
| 288 | #define ioremap_wc ioremap_nocache |
| 289 | #endif |
| 290 | |
| 291 | static inline void iounmap(void *addr) |
| 292 | { |
| 293 | } |
| 294 | |
| 295 | #ifndef CONFIG_GENERIC_IOMAP |
| 296 | static inline void __iomem *ioport_map(unsigned long port, unsigned int nr) |
| 297 | { |
| 298 | return (void __iomem *) port; |
| 299 | } |
| 300 | |
| 301 | static inline void ioport_unmap(void __iomem *p) |
| 302 | { |
| 303 | } |
| 304 | #else /* CONFIG_GENERIC_IOMAP */ |
| 305 | extern void __iomem *ioport_map(unsigned long port, unsigned int nr); |
| 306 | extern void ioport_unmap(void __iomem *p); |
| 307 | #endif /* CONFIG_GENERIC_IOMAP */ |
| 308 | |
| 309 | #define xlate_dev_kmem_ptr(p) p |
| 310 | #define xlate_dev_mem_ptr(p) ((void *) (p)) |
| 311 | |
| 312 | #ifndef virt_to_bus |
| 313 | static inline unsigned long virt_to_bus(volatile void *address) |
| 314 | { |
| 315 | return ((unsigned long) address); |
| 316 | } |
| 317 | |
| 318 | static inline void *bus_to_virt(unsigned long address) |
| 319 | { |
| 320 | return (void *) address; |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | #define memset_io(a, b, c) memset(__io_virt(a), (b), (c)) |
| 325 | #define memcpy_fromio(a, b, c) memcpy((a), __io_virt(b), (c)) |
| 326 | #define memcpy_toio(a, b, c) memcpy(__io_virt(a), (b), (c)) |
| 327 | |
| 328 | #endif /* __KERNEL__ */ |
| 329 | |
| 330 | #endif /* __ASM_GENERIC_IO_H */ |