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> |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 8 | #include <linux/msm_rtb.h> |
Tejun Heo | 9ac7849 | 2007-01-20 16:00:26 +0900 | [diff] [blame] | 9 | |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 10 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * Read/write from/to an (offsettable) iomem cookie. It might be a PIO |
| 14 | * access or a MMIO access, these functions don't care. The info is |
| 15 | * encoded in the hardware mapping set up by the mapping functions |
| 16 | * (or the cookie itself, depending on implementation and hw). |
| 17 | * |
| 18 | * The generic routines don't assume any hardware mappings, and just |
| 19 | * encode the PIO/MMIO as part of the cookie. They coldly assume that |
| 20 | * the MMIO IO mappings are not in the low address range. |
| 21 | * |
| 22 | * Architectures for which this is not true can't use this generic |
| 23 | * implementation and should do their own copy. |
| 24 | */ |
| 25 | |
| 26 | #ifndef HAVE_ARCH_PIO_SIZE |
| 27 | /* |
| 28 | * We encode the physical PIO addresses (0-0xffff) into the |
| 29 | * pointer by offsetting them with a constant (0x10000) and |
| 30 | * assuming that all the low addresses are always PIO. That means |
| 31 | * we can do some sanity checks on the low bits, and don't |
| 32 | * need to just take things for granted. |
| 33 | */ |
| 34 | #define PIO_OFFSET 0x10000UL |
| 35 | #define PIO_MASK 0x0ffffUL |
| 36 | #define PIO_RESERVED 0x40000UL |
| 37 | #endif |
| 38 | |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 39 | static void bad_io_access(unsigned long port, const char *access) |
| 40 | { |
| 41 | static int count = 10; |
| 42 | if (count) { |
| 43 | count--; |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 44 | WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 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 | |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 72 | unsigned int ioread8(void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 74 | uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr); |
| 75 | IO_COND(addr, return inb(port), return readb_no_log(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 76 | return 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 78 | unsigned int ioread16(void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 80 | uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr); |
| 81 | IO_COND(addr, return inw(port), return readw_no_log(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 82 | return 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 84 | unsigned int ioread16be(void __iomem *addr) |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 85 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 86 | uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr); |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 87 | IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 88 | return 0xffff; |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 89 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 90 | unsigned int ioread32(void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 92 | uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr); |
| 93 | IO_COND(addr, return inl(port), return readl_no_log(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 94 | return 0xffffffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 96 | unsigned int ioread32be(void __iomem *addr) |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 97 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 98 | uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr); |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 99 | IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr)); |
Linus Torvalds | 6cbf0c7 | 2007-05-04 20:44:23 -0700 | [diff] [blame] | 100 | return 0xffffffff; |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | EXPORT_SYMBOL(ioread8); |
| 103 | EXPORT_SYMBOL(ioread16); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 104 | EXPORT_SYMBOL(ioread16be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | EXPORT_SYMBOL(ioread32); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 106 | EXPORT_SYMBOL(ioread32be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 108 | #ifndef pio_write16be |
| 109 | #define pio_write16be(val,port) outw(swab16(val),port) |
| 110 | #define pio_write32be(val,port) outl(swab32(val),port) |
| 111 | #endif |
| 112 | |
| 113 | #ifndef mmio_write16be |
| 114 | #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port) |
| 115 | #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port) |
| 116 | #endif |
| 117 | |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 118 | void iowrite8(u8 val, void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 120 | uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr); |
| 121 | IO_COND(addr, outb(val, port), writeb_no_log(val, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 123 | void iowrite16(u16 val, void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 125 | uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr); |
| 126 | IO_COND(addr, outw(val, port), writew_no_log(val, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 128 | void iowrite16be(u16 val, void __iomem *addr) |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 129 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 130 | uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr); |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 131 | IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr)); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 132 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 133 | void iowrite32(u32 val, void __iomem *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 135 | uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr); |
| 136 | IO_COND(addr, outl(val, port), writel_no_log(val, addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 138 | void iowrite32be(u32 val, void __iomem *addr) |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 139 | { |
Rohit Vaswani | 6aa4ad2 | 2014-08-29 08:56:22 -0700 | [diff] [blame] | 140 | uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr); |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 141 | IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr)); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 142 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | EXPORT_SYMBOL(iowrite8); |
| 144 | EXPORT_SYMBOL(iowrite16); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 145 | EXPORT_SYMBOL(iowrite16be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | EXPORT_SYMBOL(iowrite32); |
James Bottomley | dae409a | 2005-04-16 15:25:54 -0700 | [diff] [blame] | 147 | EXPORT_SYMBOL(iowrite32be); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | /* |
| 150 | * These are the "repeat MMIO read/write" functions. |
| 151 | * Note the "__raw" accesses, since we don't want to |
| 152 | * convert to CPU byte order. We write in "IO byte |
| 153 | * order" (we also don't have IO barriers). |
| 154 | */ |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 155 | #ifndef mmio_insb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | static inline void mmio_insb(void __iomem *addr, u8 *dst, int count) |
| 157 | { |
| 158 | while (--count >= 0) { |
| 159 | u8 data = __raw_readb(addr); |
| 160 | *dst = data; |
| 161 | dst++; |
| 162 | } |
| 163 | } |
| 164 | static inline void mmio_insw(void __iomem *addr, u16 *dst, int count) |
| 165 | { |
| 166 | while (--count >= 0) { |
| 167 | u16 data = __raw_readw(addr); |
| 168 | *dst = data; |
| 169 | dst++; |
| 170 | } |
| 171 | } |
| 172 | static inline void mmio_insl(void __iomem *addr, u32 *dst, int count) |
| 173 | { |
| 174 | while (--count >= 0) { |
| 175 | u32 data = __raw_readl(addr); |
| 176 | *dst = data; |
| 177 | dst++; |
| 178 | } |
| 179 | } |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 180 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 182 | #ifndef mmio_outsb |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count) |
| 184 | { |
| 185 | while (--count >= 0) { |
| 186 | __raw_writeb(*src, addr); |
| 187 | src++; |
| 188 | } |
| 189 | } |
| 190 | static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count) |
| 191 | { |
| 192 | while (--count >= 0) { |
| 193 | __raw_writew(*src, addr); |
| 194 | src++; |
| 195 | } |
| 196 | } |
| 197 | static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count) |
| 198 | { |
| 199 | while (--count >= 0) { |
| 200 | __raw_writel(*src, addr); |
| 201 | src++; |
| 202 | } |
| 203 | } |
Linus Torvalds | 34ba8a5 | 2006-11-11 17:24:46 +1100 | [diff] [blame] | 204 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 206 | void ioread8_rep(void __iomem *addr, void *dst, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
| 208 | IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count)); |
| 209 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 210 | void ioread16_rep(void __iomem *addr, void *dst, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
| 212 | IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count)); |
| 213 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 214 | void ioread32_rep(void __iomem *addr, void *dst, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | { |
| 216 | IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count)); |
| 217 | } |
| 218 | EXPORT_SYMBOL(ioread8_rep); |
| 219 | EXPORT_SYMBOL(ioread16_rep); |
| 220 | EXPORT_SYMBOL(ioread32_rep); |
| 221 | |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 222 | void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { |
| 224 | IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count)); |
| 225 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 226 | void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
| 228 | IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count)); |
| 229 | } |
Harvey Harrison | 9f741cb | 2008-02-08 04:19:55 -0800 | [diff] [blame] | 230 | void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | { |
| 232 | IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count)); |
| 233 | } |
| 234 | EXPORT_SYMBOL(iowrite8_rep); |
| 235 | EXPORT_SYMBOL(iowrite16_rep); |
| 236 | EXPORT_SYMBOL(iowrite32_rep); |
| 237 | |
Uwe Kleine-König | ce816fa | 2014-04-07 15:39:19 -0700 | [diff] [blame] | 238 | #ifdef CONFIG_HAS_IOPORT_MAP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | /* Create a virtual mapping cookie for an IO port range */ |
| 240 | void __iomem *ioport_map(unsigned long port, unsigned int nr) |
| 241 | { |
| 242 | if (port > PIO_MASK) |
| 243 | return NULL; |
| 244 | return (void __iomem *) (unsigned long) (port + PIO_OFFSET); |
| 245 | } |
| 246 | |
| 247 | void ioport_unmap(void __iomem *addr) |
| 248 | { |
| 249 | /* Nothing to do */ |
| 250 | } |
| 251 | EXPORT_SYMBOL(ioport_map); |
| 252 | EXPORT_SYMBOL(ioport_unmap); |
Uwe Kleine-König | ce816fa | 2014-04-07 15:39:19 -0700 | [diff] [blame] | 253 | #endif /* CONFIG_HAS_IOPORT_MAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
Jonas Bonn | 82ed223 | 2011-07-02 17:23:29 +0200 | [diff] [blame] | 255 | #ifdef CONFIG_PCI |
Michael S. Tsirkin | 66eab4d | 2011-11-24 20:45:20 +0200 | [diff] [blame] | 256 | /* Hide the details if this is a MMIO or PIO address space and just do what |
| 257 | * you expect in the correct way. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | void pci_iounmap(struct pci_dev *dev, void __iomem * addr) |
| 259 | { |
| 260 | IO_COND(addr, /* nothing */, iounmap(addr)); |
| 261 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | EXPORT_SYMBOL(pci_iounmap); |
Jonas Bonn | 82ed223 | 2011-07-02 17:23:29 +0200 | [diff] [blame] | 263 | #endif /* CONFIG_PCI */ |