blob: f45ea96c77a2462c93d2fea949e27113c043a5a0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Implement the default iomap interfaces
4 *
5 * (C) Copyright 2004 Linus Torvalds
6 */
7#include <linux/pci.h>
Tejun Heo9ac78492007-01-20 16:00:26 +09008#include <linux/io.h>
Xiaogang Cuicef94392013-12-16 16:53:13 +08009#include <linux/msm_rtb.h>
Tejun Heo9ac78492007-01-20 16:00:26 +090010
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050011#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13/*
14 * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
15 * access or a MMIO access, these functions don't care. The info is
16 * encoded in the hardware mapping set up by the mapping functions
17 * (or the cookie itself, depending on implementation and hw).
18 *
19 * The generic routines don't assume any hardware mappings, and just
20 * encode the PIO/MMIO as part of the cookie. They coldly assume that
21 * the MMIO IO mappings are not in the low address range.
22 *
23 * Architectures for which this is not true can't use this generic
24 * implementation and should do their own copy.
25 */
26
27#ifndef HAVE_ARCH_PIO_SIZE
28/*
29 * We encode the physical PIO addresses (0-0xffff) into the
30 * pointer by offsetting them with a constant (0x10000) and
31 * assuming that all the low addresses are always PIO. That means
32 * we can do some sanity checks on the low bits, and don't
33 * need to just take things for granted.
34 */
35#define PIO_OFFSET 0x10000UL
36#define PIO_MASK 0x0ffffUL
37#define PIO_RESERVED 0x40000UL
38#endif
39
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070040static void bad_io_access(unsigned long port, const char *access)
41{
42 static int count = 10;
43 if (count) {
44 count--;
Arjan van de Ven5cd2b452008-07-25 19:45:39 -070045 WARN(1, KERN_ERR "Bad IO access at port %#lx (%s)\n", port, access);
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070046 }
47}
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/*
50 * Ugly macros are a way of life.
51 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define IO_COND(addr, is_pio, is_mmio) do { \
53 unsigned long port = (unsigned long __force)addr; \
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070054 if (port >= PIO_RESERVED) { \
55 is_mmio; \
56 } else if (port > PIO_OFFSET) { \
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 port &= PIO_MASK; \
58 is_pio; \
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070059 } else \
60 bad_io_access(port, #is_pio ); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061} while (0)
62
Linus Torvalds34ba8a52006-11-11 17:24:46 +110063#ifndef pio_read16be
64#define pio_read16be(port) swab16(inw(port))
65#define pio_read32be(port) swab32(inl(port))
66#endif
67
68#ifndef mmio_read16be
69#define mmio_read16be(addr) be16_to_cpu(__raw_readw(addr))
70#define mmio_read32be(addr) be32_to_cpu(__raw_readl(addr))
71#endif
72
Harvey Harrison9f741cb2008-02-08 04:19:55 -080073unsigned int ioread8(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Xiaogang Cuicef94392013-12-16 16:53:13 +080075 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
76 IO_COND(addr, return inb(port), return readb_no_log(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070077 return 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080079unsigned int ioread16(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Xiaogang Cuicef94392013-12-16 16:53:13 +080081 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
82 IO_COND(addr, return inw(port), return readw_no_log(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070083 return 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080085unsigned int ioread16be(void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -070086{
Xiaogang Cuicef94392013-12-16 16:53:13 +080087 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +110088 IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070089 return 0xffff;
James Bottomleydae409a2005-04-16 15:25:54 -070090}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080091unsigned int ioread32(void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Xiaogang Cuicef94392013-12-16 16:53:13 +080093 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
94 IO_COND(addr, return inl(port), return readl_no_log(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -070095 return 0xffffffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Harvey Harrison9f741cb2008-02-08 04:19:55 -080097unsigned int ioread32be(void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -070098{
Xiaogang Cuicef94392013-12-16 16:53:13 +080099 uncached_logk_pc(LOGK_READL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100100 IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
Linus Torvalds6cbf0c72007-05-04 20:44:23 -0700101 return 0xffffffff;
James Bottomleydae409a2005-04-16 15:25:54 -0700102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103EXPORT_SYMBOL(ioread8);
104EXPORT_SYMBOL(ioread16);
James Bottomleydae409a2005-04-16 15:25:54 -0700105EXPORT_SYMBOL(ioread16be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106EXPORT_SYMBOL(ioread32);
James Bottomleydae409a2005-04-16 15:25:54 -0700107EXPORT_SYMBOL(ioread32be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100109#ifndef pio_write16be
110#define pio_write16be(val,port) outw(swab16(val),port)
111#define pio_write32be(val,port) outl(swab32(val),port)
112#endif
113
114#ifndef mmio_write16be
115#define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
116#define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
117#endif
118
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800119void iowrite8(u8 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Xiaogang Cuicef94392013-12-16 16:53:13 +0800121 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
122 IO_COND(addr, outb(val, port), writeb_no_log(val, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800124void iowrite16(u16 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Xiaogang Cuicef94392013-12-16 16:53:13 +0800126 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
127 IO_COND(addr, outw(val, port), writew_no_log(val, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800129void iowrite16be(u16 val, void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -0700130{
Xiaogang Cuicef94392013-12-16 16:53:13 +0800131 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100132 IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
James Bottomleydae409a2005-04-16 15:25:54 -0700133}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800134void iowrite32(u32 val, void __iomem *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Xiaogang Cuicef94392013-12-16 16:53:13 +0800136 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
137 IO_COND(addr, outl(val, port), writel_no_log(val, addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800139void iowrite32be(u32 val, void __iomem *addr)
James Bottomleydae409a2005-04-16 15:25:54 -0700140{
Xiaogang Cuicef94392013-12-16 16:53:13 +0800141 uncached_logk_pc(LOGK_WRITEL, __builtin_return_address(0), addr);
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100142 IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
James Bottomleydae409a2005-04-16 15:25:54 -0700143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144EXPORT_SYMBOL(iowrite8);
145EXPORT_SYMBOL(iowrite16);
James Bottomleydae409a2005-04-16 15:25:54 -0700146EXPORT_SYMBOL(iowrite16be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147EXPORT_SYMBOL(iowrite32);
James Bottomleydae409a2005-04-16 15:25:54 -0700148EXPORT_SYMBOL(iowrite32be);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150/*
151 * These are the "repeat MMIO read/write" functions.
152 * Note the "__raw" accesses, since we don't want to
153 * convert to CPU byte order. We write in "IO byte
154 * order" (we also don't have IO barriers).
155 */
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100156#ifndef mmio_insb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
158{
159 while (--count >= 0) {
160 u8 data = __raw_readb(addr);
161 *dst = data;
162 dst++;
163 }
164}
165static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
166{
167 while (--count >= 0) {
168 u16 data = __raw_readw(addr);
169 *dst = data;
170 dst++;
171 }
172}
173static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
174{
175 while (--count >= 0) {
176 u32 data = __raw_readl(addr);
177 *dst = data;
178 dst++;
179 }
180}
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100183#ifndef mmio_outsb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
185{
186 while (--count >= 0) {
187 __raw_writeb(*src, addr);
188 src++;
189 }
190}
191static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
192{
193 while (--count >= 0) {
194 __raw_writew(*src, addr);
195 src++;
196 }
197}
198static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
199{
200 while (--count >= 0) {
201 __raw_writel(*src, addr);
202 src++;
203 }
204}
Linus Torvalds34ba8a52006-11-11 17:24:46 +1100205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800207void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
210}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800211void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
214}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800215void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
218}
219EXPORT_SYMBOL(ioread8_rep);
220EXPORT_SYMBOL(ioread16_rep);
221EXPORT_SYMBOL(ioread32_rep);
222
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800223void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
226}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800227void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
230}
Harvey Harrison9f741cb2008-02-08 04:19:55 -0800231void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
233 IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
234}
235EXPORT_SYMBOL(iowrite8_rep);
236EXPORT_SYMBOL(iowrite16_rep);
237EXPORT_SYMBOL(iowrite32_rep);
238
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700239#ifdef CONFIG_HAS_IOPORT_MAP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/* Create a virtual mapping cookie for an IO port range */
241void __iomem *ioport_map(unsigned long port, unsigned int nr)
242{
243 if (port > PIO_MASK)
244 return NULL;
245 return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
246}
247
248void ioport_unmap(void __iomem *addr)
249{
250 /* Nothing to do */
251}
252EXPORT_SYMBOL(ioport_map);
253EXPORT_SYMBOL(ioport_unmap);
Uwe Kleine-Königce816fa2014-04-07 15:39:19 -0700254#endif /* CONFIG_HAS_IOPORT_MAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Jonas Bonn82ed2232011-07-02 17:23:29 +0200256#ifdef CONFIG_PCI
Michael S. Tsirkin66eab4d2011-11-24 20:45:20 +0200257/* Hide the details if this is a MMIO or PIO address space and just do what
258 * you expect in the correct way. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
260{
261 IO_COND(addr, /* nothing */, iounmap(addr));
262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263EXPORT_SYMBOL(pci_iounmap);
Jonas Bonn82ed2232011-07-02 17:23:29 +0200264#endif /* CONFIG_PCI */