Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Implement the default iomap interfaces |
| 3 | * |
| 4 | * (C) Copyright 2004 Linus Torvalds |
| 5 | */ |
| 6 | #include <linux/pci.h> |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 7 | #include <linux/io.h> |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | |
| 11 | /* |
| 12 | * Read/write from/to an (offsettable) iomem cookie. It might be a PIO |
| 13 | * access or a MMIO access, these functions don't care. The info is |
| 14 | * encoded in the hardware mapping set up by the mapping functions |
| 15 | * (or the cookie itself, depending on implementation and hw). |
| 16 | * |
| 17 | * The generic routines don't assume any hardware mappings, and just |
| 18 | * encode the PIO/MMIO as part of the cookie. They coldly assume that |
| 19 | * the MMIO IO mappings are not in the low address range. |
| 20 | * |
| 21 | * Architectures for which this is not true can't use this generic |
| 22 | * implementation and should do their own copy. |
| 23 | */ |
| 24 | |
| 25 | #ifndef HAVE_ARCH_PIO_SIZE |
| 26 | /* |
| 27 | * We encode the physical PIO addresses (0-0xffff) into the |
| 28 | * pointer by offsetting them with a constant (0x10000) and |
| 29 | * assuming that all the low addresses are always PIO. That means |
| 30 | * we can do some sanity checks on the low bits, and don't |
| 31 | * need to just take things for granted. |
| 32 | */ |
| 33 | #define PIO_OFFSET 0x10000UL |
| 34 | #define PIO_MASK 0x0ffffUL |
| 35 | #define PIO_RESERVED 0x40000UL |
| 36 | #endif |
| 37 | |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 38 | static void bad_io_access(unsigned long port, const char *access) |
| 39 | { |
| 40 | static int count = 10; |
| 41 | if (count) { |
| 42 | count--; |
| 43 | printk(KERN_ERR "Bad IO access at port %lx (%s)\n", port, access); |
| 44 | WARN_ON(1); |
| 45 | } |
| 46 | } |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | /* |
| 49 | * Ugly macros are a way of life. |
| 50 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #define IO_COND(addr, is_pio, is_mmio) do { \ |
| 52 | unsigned long port = (unsigned long __force)addr; \ |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 53 | if (port >= PIO_RESERVED) { \ |
| 54 | is_mmio; \ |
| 55 | } else if (port > PIO_OFFSET) { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | port &= PIO_MASK; \ |
| 57 | is_pio; \ |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 58 | } else \ |
| 59 | bad_io_access(port, #is_pio ); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } while (0) |
| 61 | |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 62 | #ifndef pio_read16be |
| 63 | #define pio_read16be(port) swab16(inw(port)) |
| 64 | #define pio_read32be(port) swab32(inl(port)) |
| 65 | #endif |
| 66 | |
| 67 | #ifndef mmio_read16be |
| 68 | #define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr)) |
| 69 | #define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr)) |
| 70 | #endif |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | unsigned int fastcall ioread8(void __iomem *addr) |
| 73 | { |
| 74 | IO_COND(addr, return inb(port), return readb(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 75 | return 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | } |
| 77 | unsigned int fastcall ioread16(void __iomem *addr) |
| 78 | { |
| 79 | IO_COND(addr, return inw(port), return readw(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 80 | return 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 82 | unsigned int fastcall ioread16be(void __iomem *addr) |
| 83 | { |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 84 | IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 85 | return 0xffff; |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 86 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | unsigned int fastcall ioread32(void __iomem *addr) |
| 88 | { |
| 89 | IO_COND(addr, return inl(port), return readl(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 90 | return 0xffffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 92 | unsigned int fastcall ioread32be(void __iomem *addr) |
| 93 | { |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 94 | IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame^] | 95 | return 0xffffffff; |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | EXPORT_SYMBOL(ioread8); |
| 98 | EXPORT_SYMBOL(ioread16); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 99 | EXPORT_SYMBOL(ioread16be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | EXPORT_SYMBOL(ioread32); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL(ioread32be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 103 | #ifndef pio_write16be |
| 104 | #define pio_write16be(val,port) outw(swab16(val),port) |
| 105 | #define pio_write32be(val,port) outl(swab32(val),port) |
| 106 | #endif |
| 107 | |
| 108 | #ifndef mmio_write16be |
| 109 | #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port) |
| 110 | #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port) |
| 111 | #endif |
| 112 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | void fastcall iowrite8(u8 val, void __iomem *addr) |
| 114 | { |
| 115 | IO_COND(addr, outb(val,port), writeb(val, addr)); |
| 116 | } |
| 117 | void fastcall iowrite16(u16 val, void __iomem *addr) |
| 118 | { |
| 119 | IO_COND(addr, outw(val,port), writew(val, addr)); |
| 120 | } |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 121 | void fastcall iowrite16be(u16 val, void __iomem *addr) |
| 122 | { |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 123 | IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr)); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | void fastcall iowrite32(u32 val, void __iomem *addr) |
| 126 | { |
| 127 | IO_COND(addr, outl(val,port), writel(val, addr)); |
| 128 | } |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 129 | void fastcall iowrite32be(u32 val, void __iomem *addr) |
| 130 | { |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 131 | IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr)); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 132 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | EXPORT_SYMBOL(iowrite8); |
| 134 | EXPORT_SYMBOL(iowrite16); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 135 | EXPORT_SYMBOL(iowrite16be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | EXPORT_SYMBOL(iowrite32); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 137 | EXPORT_SYMBOL(iowrite32be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | /* |
| 140 | * These are the "repeat MMIO read/write" functions. |
| 141 | * Note the "__raw" accesses, since we don't want to |
| 142 | * convert to CPU byte order. We write in "IO byte |
| 143 | * order" (we also don't have IO barriers). |
| 144 | */ |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 145 | #ifndef mmio_insb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | static inline void mmio_insb(void __iomem *addr, u8 *dst, int count) |
| 147 | { |
| 148 | while (--count >= 0) { |
| 149 | u8 data = __raw_readb(addr); |
| 150 | *dst = data; |
| 151 | dst++; |
| 152 | } |
| 153 | } |
| 154 | static inline void mmio_insw(void __iomem *addr, u16 *dst, int count) |
| 155 | { |
| 156 | while (--count >= 0) { |
| 157 | u16 data = __raw_readw(addr); |
| 158 | *dst = data; |
| 159 | dst++; |
| 160 | } |
| 161 | } |
| 162 | static inline void mmio_insl(void __iomem *addr, u32 *dst, int count) |
| 163 | { |
| 164 | while (--count >= 0) { |
| 165 | u32 data = __raw_readl(addr); |
| 166 | *dst = data; |
| 167 | dst++; |
| 168 | } |
| 169 | } |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 170 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 172 | #ifndef mmio_outsb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count) |
| 174 | { |
| 175 | while (--count >= 0) { |
| 176 | __raw_writeb(*src, addr); |
| 177 | src++; |
| 178 | } |
| 179 | } |
| 180 | static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count) |
| 181 | { |
| 182 | while (--count >= 0) { |
| 183 | __raw_writew(*src, addr); |
| 184 | src++; |
| 185 | } |
| 186 | } |
| 187 | static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count) |
| 188 | { |
| 189 | while (--count >= 0) { |
| 190 | __raw_writel(*src, addr); |
| 191 | src++; |
| 192 | } |
| 193 | } |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 194 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count) |
| 197 | { |
| 198 | IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count)); |
| 199 | } |
| 200 | void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count) |
| 201 | { |
| 202 | IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count)); |
| 203 | } |
| 204 | void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count) |
| 205 | { |
| 206 | IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count)); |
| 207 | } |
| 208 | EXPORT_SYMBOL(ioread8_rep); |
| 209 | EXPORT_SYMBOL(ioread16_rep); |
| 210 | EXPORT_SYMBOL(ioread32_rep); |
| 211 | |
| 212 | void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count) |
| 213 | { |
| 214 | IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count)); |
| 215 | } |
| 216 | void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count) |
| 217 | { |
| 218 | IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count)); |
| 219 | } |
| 220 | void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count) |
| 221 | { |
| 222 | IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count)); |
| 223 | } |
| 224 | EXPORT_SYMBOL(iowrite8_rep); |
| 225 | EXPORT_SYMBOL(iowrite16_rep); |
| 226 | EXPORT_SYMBOL(iowrite32_rep); |
| 227 | |
| 228 | /* Create a virtual mapping cookie for an IO port range */ |
| 229 | void __iomem *ioport_map(unsigned long port, unsigned int nr) |
| 230 | { |
| 231 | if (port > PIO_MASK) |
| 232 | return NULL; |
| 233 | return (void __iomem *) (unsigned long) (port + PIO_OFFSET); |
| 234 | } |
| 235 | |
| 236 | void ioport_unmap(void __iomem *addr) |
| 237 | { |
| 238 | /* Nothing to do */ |
| 239 | } |
| 240 | EXPORT_SYMBOL(ioport_map); |
| 241 | EXPORT_SYMBOL(ioport_unmap); |
| 242 | |
| 243 | /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */ |
| 244 | void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen) |
| 245 | { |
| 246 | unsigned long start = pci_resource_start(dev, bar); |
| 247 | unsigned long len = pci_resource_len(dev, bar); |
| 248 | unsigned long flags = pci_resource_flags(dev, bar); |
| 249 | |
| 250 | if (!len || !start) |
| 251 | return NULL; |
| 252 | if (maxlen && len > maxlen) |
| 253 | len = maxlen; |
| 254 | if (flags & IORESOURCE_IO) |
| 255 | return ioport_map(start, len); |
| 256 | if (flags & IORESOURCE_MEM) { |
| 257 | if (flags & IORESOURCE_CACHEABLE) |
| 258 | return ioremap(start, len); |
| 259 | return ioremap_nocache(start, len); |
| 260 | } |
| 261 | /* What? */ |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | void pci_iounmap(struct pci_dev *dev, void __iomem * addr) |
| 266 | { |
| 267 | IO_COND(addr, /* nothing */, iounmap(addr)); |
| 268 | } |
| 269 | EXPORT_SYMBOL(pci_iomap); |
| 270 | EXPORT_SYMBOL(pci_iounmap); |