blob: fc3dcb4b238eca1a483c17de5b5706f536d3d444 [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>
8
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -05009#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
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 Torvalds6cbf0c72007-05-04 20:44:23 -070038static void bad_io_access(unsigned long port, const char *access)
39{
40 static int count = 10;
41 if (count) {
42 count--;
Arjan van de Ven5cd2b452008-07-25 19:45:39 -070043 WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070044 }
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * Ugly macros are a way of life.
49 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define IO_COND(addr, is_pio, is_mmio) do { \
51 unsigned long port = (unsigned long __force)addr; \
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070052 if (port >= PIO_RESERVED) { \
53 is_mmio; \
54 } else if (port > PIO_OFFSET) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 port &= PIO_MASK; \
56 is_pio; \
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070057 } else \
58 bad_io_access(port, #is_pio ); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070059} while (0)
60
Linus Torvalds34ba8a52006-11-11 17:24:46 +110061#ifndef pio_read16be
62#define pio_read16be(port) swab16(inw(port))
63#define pio_read32be(port) swab32(inl(port))
64#endif
65
66#ifndef mmio_read16be
67#define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
68#define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
69#endif
70
Harvey Harrison9f741cb2008-02-08 04:19:55 -080071unsigned int ioread8(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
73 IO_COND(addr, return inb(port), return readb(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070074 return 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080076unsigned int ioread16(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 IO_COND(addr, return inw(port), return readw(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070079 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080081unsigned int ioread16be(void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -070082{
Linus Torvalds34ba8a52006-11-11 17:24:46 +110083 IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070084 return 0xffff;
James Bottomleydae409a2005-04-16 15:25:54 -070085}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080086unsigned int ioread32(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
88 IO_COND(addr, return inl(port), return readl(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070089 return 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080091unsigned int ioread32be(void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -070092{
Linus Torvalds34ba8a52006-11-11 17:24:46 +110093 IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070094 return 0xffffffff;
James Bottomleydae409a2005-04-16 15:25:54 -070095}
Linus Torvalds1da177e2005-04-16 15:20:36 -070096EXPORT_SYMBOL(ioread8);
97EXPORT_SYMBOL(ioread16);
James Bottomleydae409a2005-04-16 15:25:54 -070098EXPORT_SYMBOL(ioread16be);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099EXPORT_SYMBOL(ioread32);
James Bottomleydae409a2005-04-16 15:25:54 -0700100EXPORT_SYMBOL(ioread32be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100102#ifndef pio_write16be
103#define pio_write16be(val,port) outw(swab16(val),port)
104#define pio_write32be(val,port) outl(swab32(val),port)
105#endif
106
107#ifndef mmio_write16be
108#define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
109#define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
110#endif
111
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800112void iowrite8(u8 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 IO_COND(addr, outb(val,port), writeb(val, addr));
115}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800116void iowrite16(u16 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 IO_COND(addr, outw(val,port), writew(val, addr));
119}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800120void iowrite16be(u16 val, void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -0700121{
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100122 IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
James Bottomleydae409a2005-04-16 15:25:54 -0700123}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800124void iowrite32(u32 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 IO_COND(addr, outl(val,port), writel(val, addr));
127}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800128void iowrite32be(u32 val, void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -0700129{
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100130 IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
James Bottomleydae409a2005-04-16 15:25:54 -0700131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132EXPORT_SYMBOL(iowrite8);
133EXPORT_SYMBOL(iowrite16);
James Bottomleydae409a2005-04-16 15:25:54 -0700134EXPORT_SYMBOL(iowrite16be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135EXPORT_SYMBOL(iowrite32);
James Bottomleydae409a2005-04-16 15:25:54 -0700136EXPORT_SYMBOL(iowrite32be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138/*
139 * These are the "repeat MMIO read/write" functions.
140 * Note the "__raw" accesses, since we don't want to
141 * convert to CPU byte order. We write in "IO byte
142 * order" (we also don't have IO barriers).
143 */
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100144#ifndef mmio_insb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
146{
147 while (--count >= 0) {
148 u8 data = __raw_readb(addr);
149 *dst = data;
150 dst++;
151 }
152}
153static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
154{
155 while (--count >= 0) {
156 u16 data = __raw_readw(addr);
157 *dst = data;
158 dst++;
159 }
160}
161static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
162{
163 while (--count >= 0) {
164 u32 data = __raw_readl(addr);
165 *dst = data;
166 dst++;
167 }
168}
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100169#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100171#ifndef mmio_outsb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
173{
174 while (--count >= 0) {
175 __raw_writeb(*src, addr);
176 src++;
177 }
178}
179static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
180{
181 while (--count >= 0) {
182 __raw_writew(*src, addr);
183 src++;
184 }
185}
186static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
187{
188 while (--count >= 0) {
189 __raw_writel(*src, addr);
190 src++;
191 }
192}
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100193#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800195void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
198}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800199void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
202}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800203void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
206}
207EXPORT_SYMBOL(ioread8_rep);
208EXPORT_SYMBOL(ioread16_rep);
209EXPORT_SYMBOL(ioread32_rep);
210
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800211void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
214}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800215void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
218}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800219void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
222}
223EXPORT_SYMBOL(iowrite8_rep);
224EXPORT_SYMBOL(iowrite16_rep);
225EXPORT_SYMBOL(iowrite32_rep);
226
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700227#ifdef CONFIG_HAS_IOPORT_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* Create a virtual mapping cookie for an IO port range */
229void __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
236void ioport_unmap(void __iomem *addr)
237{
238 /* Nothing to do */
239}
240EXPORT_SYMBOL(ioport_map);
241EXPORT_SYMBOL(ioport_unmap);
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700242#endif /* CONFIG_HAS_IOPORT_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Jonas Bonn82ed2232011-07-02 17:23:29 +0200244#ifdef CONFIG_PCI
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +0200245/* Hide the details if this is a MMIO or PIO address space and just do what
246 * you expect in the correct way. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
248{
249 IO_COND(addr, /* nothing */, iounmap(addr));
250}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251EXPORT_SYMBOL(pci_iounmap);
Jonas Bonn82ed2232011-07-02 17:23:29 +0200252#endif /* CONFIG_PCI */