blob: b29a91e63f01c252dc16b732629a737426506494 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Implement the default iomap interfaces
3 *
4 * (C) Copyright 2004 Linus Torvalds
5 */
6#include <linux/pci.h>
Tejun Heo9ac78492007-01-20 16:00:26 +09007#include <linux/io.h>
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -07008#include <linux/msm_rtb.h>
Tejun Heo9ac78492007-01-20 16:00:26 +09009
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050010#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
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 Torvalds6cbf0c72007-05-04 20:44:23 -070039static void bad_io_access(unsigned long port, const char *access)
40{
41 static int count = 10;
42 if (count) {
43 count--;
Arjan van de Ven5cd2b452008-07-25 19:45:39 -070044 WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070045 }
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Ugly macros are a way of life.
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define IO_COND(addr, is_pio, is_mmio) do { \
52 unsigned long port = (unsigned long __force)addr; \
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070053 if (port >= PIO_RESERVED) { \
54 is_mmio; \
55 } else if (port > PIO_OFFSET) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 port &= PIO_MASK; \
57 is_pio; \
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070058 } else \
59 bad_io_access(port, #is_pio ); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070060} while (0)
61
Linus Torvalds34ba8a52006-11-11 17:24:46 +110062#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 Harrison9f741cb2008-02-08 04:19:55 -080072unsigned int ioread8(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -070074 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
75 IO_COND(addr, return inb(port), return readb_no_log(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070076 return 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080078unsigned int ioread16(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -070080 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
81 IO_COND(addr, return inw(port), return readw_no_log(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070082 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080084unsigned int ioread16be(void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -070085{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -070086 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +110087 IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070088 return 0xffff;
James Bottomleydae409a2005-04-16 15:25:54 -070089}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080090unsigned int ioread32(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -070092 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
93 IO_COND(addr, return inl(port), return readl_no_log(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070094 return 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080096unsigned int ioread32be(void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -070097{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -070098 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +110099 IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -0700100 return 0xffffffff;
James Bottomleydae409a2005-04-16 15:25:54 -0700101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102EXPORT_SYMBOL(ioread8);
103EXPORT_SYMBOL(ioread16);
James Bottomleydae409a2005-04-16 15:25:54 -0700104EXPORT_SYMBOL(ioread16be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105EXPORT_SYMBOL(ioread32);
James Bottomleydae409a2005-04-16 15:25:54 -0700106EXPORT_SYMBOL(ioread32be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100108#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 Harrison9f741cb2008-02-08 04:19:55 -0800118void iowrite8(u8 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -0700120 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
121 IO_COND(addr, outb(val, port), writeb_no_log(val, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800123void iowrite16(u16 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -0700125 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
126 IO_COND(addr, outw(val, port), writew_no_log(val, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800128void iowrite16be(u16 val, void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -0700129{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -0700130 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100131 IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
James Bottomleydae409a2005-04-16 15:25:54 -0700132}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800133void iowrite32(u32 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -0700135 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
136 IO_COND(addr, outl(val, port), writel_no_log(val, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800138void iowrite32be(u32 val, void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -0700139{
Rohit Vaswani6aa4ad22014-08-29 08:56:22 -0700140 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100141 IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
James Bottomleydae409a2005-04-16 15:25:54 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143EXPORT_SYMBOL(iowrite8);
144EXPORT_SYMBOL(iowrite16);
James Bottomleydae409a2005-04-16 15:25:54 -0700145EXPORT_SYMBOL(iowrite16be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146EXPORT_SYMBOL(iowrite32);
James Bottomleydae409a2005-04-16 15:25:54 -0700147EXPORT_SYMBOL(iowrite32be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
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 Torvalds34ba8a52006-11-11 17:24:46 +1100155#ifndef mmio_insb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static 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}
164static 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}
172static 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 Torvalds34ba8a52006-11-11 17:24:46 +1100180#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100182#ifndef mmio_outsb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static 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}
190static 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}
197static 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 Torvalds34ba8a52006-11-11 17:24:46 +1100204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800206void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
209}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800210void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
213}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800214void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
217}
218EXPORT_SYMBOL(ioread8_rep);
219EXPORT_SYMBOL(ioread16_rep);
220EXPORT_SYMBOL(ioread32_rep);
221
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800222void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
225}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800226void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
229}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800230void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
233}
234EXPORT_SYMBOL(iowrite8_rep);
235EXPORT_SYMBOL(iowrite16_rep);
236EXPORT_SYMBOL(iowrite32_rep);
237
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700238#ifdef CONFIG_HAS_IOPORT_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239/* Create a virtual mapping cookie for an IO port range */
240void __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
247void ioport_unmap(void __iomem *addr)
248{
249 /* Nothing to do */
250}
251EXPORT_SYMBOL(ioport_map);
252EXPORT_SYMBOL(ioport_unmap);
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700253#endif /* CONFIG_HAS_IOPORT_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Jonas Bonn82ed2232011-07-02 17:23:29 +0200255#ifdef CONFIG_PCI
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +0200256/* Hide the details if this is a MMIO or PIO address space and just do what
257 * you expect in the correct way. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
259{
260 IO_COND(addr, /* nothing */, iounmap(addr));
261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262EXPORT_SYMBOL(pci_iounmap);
Jonas Bonn82ed2232011-07-02 17:23:29 +0200263#endif /* CONFIG_PCI */