Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* io.h: FRV I/O operations |
| 2 | * |
| 3 | * Copyright (C) 2004 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 License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * This gets interesting when talking to the PCI bus - the CPU is in big endian |
| 12 | * mode, the PCI bus is little endian and the hardware in the middle can do |
| 13 | * byte swapping |
| 14 | */ |
| 15 | #ifndef _ASM_IO_H |
| 16 | #define _ASM_IO_H |
| 17 | |
| 18 | #ifdef __KERNEL__ |
| 19 | |
| 20 | #include <linux/config.h> |
David Howells | a90a72c | 2006-01-08 01:01:20 -0800 | [diff] [blame] | 21 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <asm/virtconvert.h> |
| 23 | #include <asm/string.h> |
| 24 | #include <asm/mb-regs.h> |
| 25 | #include <linux/delay.h> |
| 26 | |
| 27 | /* |
| 28 | * swap functions are sometimes needed to interface little-endian hardware |
| 29 | */ |
| 30 | |
| 31 | static inline unsigned short _swapw(unsigned short v) |
| 32 | { |
| 33 | return ((v << 8) | (v >> 8)); |
| 34 | } |
| 35 | |
| 36 | static inline unsigned long _swapl(unsigned long v) |
| 37 | { |
| 38 | return ((v << 24) | ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8) | (v >> 24)); |
| 39 | } |
| 40 | |
| 41 | //#define __iormb() asm volatile("membar") |
| 42 | //#define __iowmb() asm volatile("membar") |
| 43 | |
| 44 | #define __raw_readb(addr) __builtin_read8((void *) (addr)) |
| 45 | #define __raw_readw(addr) __builtin_read16((void *) (addr)) |
| 46 | #define __raw_readl(addr) __builtin_read32((void *) (addr)) |
| 47 | |
| 48 | #define __raw_writeb(datum, addr) __builtin_write8((void *) (addr), datum) |
| 49 | #define __raw_writew(datum, addr) __builtin_write16((void *) (addr), datum) |
| 50 | #define __raw_writel(datum, addr) __builtin_write32((void *) (addr), datum) |
| 51 | |
| 52 | static inline void io_outsb(unsigned int addr, const void *buf, int len) |
| 53 | { |
| 54 | unsigned long __ioaddr = (unsigned long) addr; |
| 55 | const uint8_t *bp = buf; |
| 56 | |
| 57 | while (len--) |
| 58 | __builtin_write8((volatile void __iomem *) __ioaddr, *bp++); |
| 59 | } |
| 60 | |
| 61 | static inline void io_outsw(unsigned int addr, const void *buf, int len) |
| 62 | { |
| 63 | unsigned long __ioaddr = (unsigned long) addr; |
| 64 | const uint16_t *bp = buf; |
| 65 | |
| 66 | while (len--) |
| 67 | __builtin_write16((volatile void __iomem *) __ioaddr, (*bp++)); |
| 68 | } |
| 69 | |
| 70 | extern void __outsl_ns(unsigned int addr, const void *buf, int len); |
| 71 | extern void __outsl_sw(unsigned int addr, const void *buf, int len); |
| 72 | static inline void __outsl(unsigned int addr, const void *buf, int len, int swap) |
| 73 | { |
| 74 | unsigned long __ioaddr = (unsigned long) addr; |
| 75 | |
| 76 | if (!swap) |
| 77 | __outsl_ns(__ioaddr, buf, len); |
| 78 | else |
| 79 | __outsl_sw(__ioaddr, buf, len); |
| 80 | } |
| 81 | |
| 82 | static inline void io_insb(unsigned long addr, void *buf, int len) |
| 83 | { |
| 84 | uint8_t *bp = buf; |
| 85 | |
| 86 | while (len--) |
| 87 | *bp++ = __builtin_read8((volatile void __iomem *) addr); |
| 88 | } |
| 89 | |
| 90 | static inline void io_insw(unsigned long addr, void *buf, int len) |
| 91 | { |
| 92 | uint16_t *bp = buf; |
| 93 | |
| 94 | while (len--) |
| 95 | *bp++ = __builtin_read16((volatile void __iomem *) addr); |
| 96 | } |
| 97 | |
| 98 | extern void __insl_ns(unsigned long addr, void *buf, int len); |
| 99 | extern void __insl_sw(unsigned long addr, void *buf, int len); |
| 100 | static inline void __insl(unsigned long addr, void *buf, int len, int swap) |
| 101 | { |
| 102 | if (!swap) |
| 103 | __insl_ns(addr, buf, len); |
| 104 | else |
| 105 | __insl_sw(addr, buf, len); |
| 106 | } |
| 107 | |
David Howells | a90a72c | 2006-01-08 01:01:20 -0800 | [diff] [blame] | 108 | #define mmiowb() mb() |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* |
| 111 | * make the short names macros so specific devices |
| 112 | * can override them as required |
| 113 | */ |
| 114 | |
| 115 | static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count) |
| 116 | { |
| 117 | memset((void __force *) addr, val, count); |
| 118 | } |
| 119 | |
| 120 | static inline void memcpy_fromio(void *dst, volatile void __iomem *src, int count) |
| 121 | { |
| 122 | memcpy(dst, (void __force *) src, count); |
| 123 | } |
| 124 | |
| 125 | static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count) |
| 126 | { |
| 127 | memcpy((void __force *) dst, src, count); |
| 128 | } |
| 129 | |
| 130 | static inline uint8_t inb(unsigned long addr) |
| 131 | { |
| 132 | return __builtin_read8((void *)addr); |
| 133 | } |
| 134 | |
| 135 | static inline uint16_t inw(unsigned long addr) |
| 136 | { |
| 137 | uint16_t ret = __builtin_read16((void *)addr); |
| 138 | |
| 139 | if (__is_PCI_IO(addr)) |
| 140 | ret = _swapw(ret); |
| 141 | |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static inline uint32_t inl(unsigned long addr) |
| 146 | { |
| 147 | uint32_t ret = __builtin_read32((void *)addr); |
| 148 | |
| 149 | if (__is_PCI_IO(addr)) |
| 150 | ret = _swapl(ret); |
| 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | static inline void outb(uint8_t datum, unsigned long addr) |
| 156 | { |
| 157 | __builtin_write8((void *)addr, datum); |
| 158 | } |
| 159 | |
| 160 | static inline void outw(uint16_t datum, unsigned long addr) |
| 161 | { |
| 162 | if (__is_PCI_IO(addr)) |
| 163 | datum = _swapw(datum); |
| 164 | __builtin_write16((void *)addr, datum); |
| 165 | } |
| 166 | |
| 167 | static inline void outl(uint32_t datum, unsigned long addr) |
| 168 | { |
| 169 | if (__is_PCI_IO(addr)) |
| 170 | datum = _swapl(datum); |
| 171 | __builtin_write32((void *)addr, datum); |
| 172 | } |
| 173 | |
| 174 | #define inb_p(addr) inb(addr) |
| 175 | #define inw_p(addr) inw(addr) |
| 176 | #define inl_p(addr) inl(addr) |
| 177 | #define outb_p(x,addr) outb(x,addr) |
| 178 | #define outw_p(x,addr) outw(x,addr) |
| 179 | #define outl_p(x,addr) outl(x,addr) |
| 180 | |
| 181 | #define outsb(a,b,l) io_outsb(a,b,l) |
| 182 | #define outsw(a,b,l) io_outsw(a,b,l) |
| 183 | #define outsl(a,b,l) __outsl(a,b,l,0) |
| 184 | |
| 185 | #define insb(a,b,l) io_insb(a,b,l) |
| 186 | #define insw(a,b,l) io_insw(a,b,l) |
| 187 | #define insl(a,b,l) __insl(a,b,l,0) |
| 188 | |
| 189 | #define IO_SPACE_LIMIT 0xffffffff |
| 190 | |
| 191 | static inline uint8_t readb(const volatile void __iomem *addr) |
| 192 | { |
| 193 | return __builtin_read8((volatile uint8_t __force *) addr); |
| 194 | } |
| 195 | |
| 196 | static inline uint16_t readw(const volatile void __iomem *addr) |
| 197 | { |
| 198 | uint16_t ret = __builtin_read16((volatile uint16_t __force *)addr); |
| 199 | |
| 200 | if (__is_PCI_MEM(addr)) |
| 201 | ret = _swapw(ret); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static inline uint32_t readl(const volatile void __iomem *addr) |
| 206 | { |
| 207 | uint32_t ret = __builtin_read32((volatile uint32_t __force *)addr); |
| 208 | |
| 209 | if (__is_PCI_MEM(addr)) |
| 210 | ret = _swapl(ret); |
| 211 | |
| 212 | return ret; |
| 213 | } |
| 214 | |
David Howells | a90a72c | 2006-01-08 01:01:20 -0800 | [diff] [blame] | 215 | #define readb_relaxed readb |
| 216 | #define readw_relaxed readw |
| 217 | #define readl_relaxed readl |
| 218 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | static inline void writeb(uint8_t datum, volatile void __iomem *addr) |
| 220 | { |
| 221 | __builtin_write8((volatile uint8_t __force *) addr, datum); |
| 222 | if (__is_PCI_MEM(addr)) |
| 223 | __flush_PCI_writes(); |
| 224 | } |
| 225 | |
| 226 | static inline void writew(uint16_t datum, volatile void __iomem *addr) |
| 227 | { |
| 228 | if (__is_PCI_MEM(addr)) |
| 229 | datum = _swapw(datum); |
| 230 | |
| 231 | __builtin_write16((volatile uint16_t __force *) addr, datum); |
| 232 | if (__is_PCI_MEM(addr)) |
| 233 | __flush_PCI_writes(); |
| 234 | } |
| 235 | |
| 236 | static inline void writel(uint32_t datum, volatile void __iomem *addr) |
| 237 | { |
| 238 | if (__is_PCI_MEM(addr)) |
| 239 | datum = _swapl(datum); |
| 240 | |
| 241 | __builtin_write32((volatile uint32_t __force *) addr, datum); |
| 242 | if (__is_PCI_MEM(addr)) |
| 243 | __flush_PCI_writes(); |
| 244 | } |
| 245 | |
| 246 | |
| 247 | /* Values for nocacheflag and cmode */ |
| 248 | #define IOMAP_FULL_CACHING 0 |
| 249 | #define IOMAP_NOCACHE_SER 1 |
| 250 | #define IOMAP_NOCACHE_NONSER 2 |
| 251 | #define IOMAP_WRITETHROUGH 3 |
| 252 | |
| 253 | extern void __iomem *__ioremap(unsigned long physaddr, unsigned long size, int cacheflag); |
| 254 | extern void __iounmap(void __iomem *addr, unsigned long size); |
| 255 | |
| 256 | static inline void __iomem *ioremap(unsigned long physaddr, unsigned long size) |
| 257 | { |
| 258 | return __ioremap(physaddr, size, IOMAP_NOCACHE_SER); |
| 259 | } |
| 260 | |
| 261 | static inline void __iomem *ioremap_nocache(unsigned long physaddr, unsigned long size) |
| 262 | { |
| 263 | return __ioremap(physaddr, size, IOMAP_NOCACHE_SER); |
| 264 | } |
| 265 | |
| 266 | static inline void __iomem *ioremap_writethrough(unsigned long physaddr, unsigned long size) |
| 267 | { |
| 268 | return __ioremap(physaddr, size, IOMAP_WRITETHROUGH); |
| 269 | } |
| 270 | |
| 271 | static inline void __iomem *ioremap_fullcache(unsigned long physaddr, unsigned long size) |
| 272 | { |
| 273 | return __ioremap(physaddr, size, IOMAP_FULL_CACHING); |
| 274 | } |
| 275 | |
| 276 | extern void iounmap(void __iomem *addr); |
| 277 | |
David Howells | a90a72c | 2006-01-08 01:01:20 -0800 | [diff] [blame] | 278 | static inline void __iomem *ioport_map(unsigned long port, unsigned int nr) |
| 279 | { |
| 280 | return (void __iomem *) port; |
| 281 | } |
| 282 | |
| 283 | static inline void ioport_unmap(void __iomem *p) |
| 284 | { |
| 285 | } |
| 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | static inline void flush_write_buffers(void) |
| 288 | { |
| 289 | __asm__ __volatile__ ("membar" : : :"memory"); |
| 290 | } |
| 291 | |
David Howells | a90a72c | 2006-01-08 01:01:20 -0800 | [diff] [blame] | 292 | /* |
| 293 | * do appropriate I/O accesses for token type |
| 294 | */ |
| 295 | static inline unsigned int ioread8(void __iomem *p) |
| 296 | { |
| 297 | return __builtin_read8(p); |
| 298 | } |
| 299 | |
| 300 | static inline unsigned int ioread16(void __iomem *p) |
| 301 | { |
| 302 | uint16_t ret = __builtin_read16(p); |
| 303 | if (__is_PCI_addr(p)) |
| 304 | ret = _swapw(ret); |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static inline unsigned int ioread32(void __iomem *p) |
| 309 | { |
| 310 | uint32_t ret = __builtin_read32(p); |
| 311 | if (__is_PCI_addr(p)) |
| 312 | ret = _swapl(ret); |
| 313 | return ret; |
| 314 | } |
| 315 | |
| 316 | static inline void iowrite8(u8 val, void __iomem *p) |
| 317 | { |
| 318 | __builtin_write8(p, val); |
| 319 | if (__is_PCI_MEM(p)) |
| 320 | __flush_PCI_writes(); |
| 321 | } |
| 322 | |
| 323 | static inline void iowrite16(u16 val, void __iomem *p) |
| 324 | { |
| 325 | if (__is_PCI_addr(p)) |
| 326 | val = _swapw(val); |
| 327 | __builtin_write16(p, val); |
| 328 | if (__is_PCI_MEM(p)) |
| 329 | __flush_PCI_writes(); |
| 330 | } |
| 331 | |
| 332 | static inline void iowrite32(u32 val, void __iomem *p) |
| 333 | { |
| 334 | if (__is_PCI_addr(p)) |
| 335 | val = _swapl(val); |
| 336 | __builtin_write32(p, val); |
| 337 | if (__is_PCI_MEM(p)) |
| 338 | __flush_PCI_writes(); |
| 339 | } |
| 340 | |
| 341 | static inline void ioread8_rep(void __iomem *p, void *dst, unsigned long count) |
| 342 | { |
| 343 | io_insb((unsigned long) p, dst, count); |
| 344 | } |
| 345 | |
| 346 | static inline void ioread16_rep(void __iomem *p, void *dst, unsigned long count) |
| 347 | { |
| 348 | io_insw((unsigned long) p, dst, count); |
| 349 | } |
| 350 | |
| 351 | static inline void ioread32_rep(void __iomem *p, void *dst, unsigned long count) |
| 352 | { |
| 353 | __insl_ns((unsigned long) p, dst, count); |
| 354 | } |
| 355 | |
| 356 | static inline void iowrite8_rep(void __iomem *p, const void *src, unsigned long count) |
| 357 | { |
| 358 | io_outsb((unsigned long) p, src, count); |
| 359 | } |
| 360 | |
| 361 | static inline void iowrite16_rep(void __iomem *p, const void *src, unsigned long count) |
| 362 | { |
| 363 | io_outsw((unsigned long) p, src, count); |
| 364 | } |
| 365 | |
| 366 | static inline void iowrite32_rep(void __iomem *p, const void *src, unsigned long count) |
| 367 | { |
| 368 | __outsl_ns((unsigned long) p, src, count); |
| 369 | } |
| 370 | |
| 371 | /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */ |
| 372 | struct pci_dev; |
| 373 | extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max); |
| 374 | static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p) |
| 375 | { |
| 376 | } |
| 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
| 379 | /* |
| 380 | * Convert a physical pointer to a virtual kernel pointer for /dev/mem |
| 381 | * access |
| 382 | */ |
| 383 | #define xlate_dev_mem_ptr(p) __va(p) |
| 384 | |
| 385 | /* |
| 386 | * Convert a virtual cached pointer to an uncached pointer |
| 387 | */ |
| 388 | #define xlate_dev_kmem_ptr(p) p |
| 389 | |
David Howells | a90a72c | 2006-01-08 01:01:20 -0800 | [diff] [blame] | 390 | /* |
| 391 | * Check BIOS signature |
| 392 | */ |
| 393 | static inline int check_signature(volatile void __iomem *io_addr, |
| 394 | const unsigned char *signature, int length) |
| 395 | { |
| 396 | int retval = 0; |
| 397 | |
| 398 | do { |
| 399 | if (readb(io_addr) != *signature) |
| 400 | goto out; |
| 401 | io_addr++; |
| 402 | signature++; |
| 403 | length--; |
| 404 | } while (length); |
| 405 | |
| 406 | retval = 1; |
| 407 | out: |
| 408 | return retval; |
| 409 | } |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | #endif /* __KERNEL__ */ |
| 412 | |
| 413 | #endif /* _ASM_IO_H */ |