2 * Implement the default iomap interfaces
4 * (C) Copyright 2004 Linus Torvalds
7 #include <linux/module.h>
11 * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
12 * access or a MMIO access, these functions don't care. The info is
13 * encoded in the hardware mapping set up by the mapping functions
14 * (or the cookie itself, depending on implementation and hw).
16 * The generic routines don't assume any hardware mappings, and just
17 * encode the PIO/MMIO as part of the cookie. They coldly assume that
18 * the MMIO IO mappings are not in the low address range.
20 * Architectures for which this is not true can't use this generic
21 * implementation and should do their own copy.
23 * We encode the physical PIO addresses (0-0xffff) into the
24 * pointer by offsetting them with a constant (0x10000) and
25 * assuming that all the low addresses are always PIO. That means
26 * we can do some sanity checks on the low bits, and don't
27 * need to just take things for granted.
29 #define PIO_OFFSET 0x10000UL
30 #define PIO_MASK 0x0ffffUL
31 #define PIO_RESERVED 0x40000UL
34 * Ugly macros are a way of life.
36 #define VERIFY_PIO(port) BUG_ON((port & ~PIO_MASK) != PIO_OFFSET)
38 #define IO_COND(addr, is_pio, is_mmio) do { \
39 unsigned long port = (unsigned long __force)addr; \
40 if (port < PIO_RESERVED) { \
49 unsigned int fastcall
ioread8(void __iomem
*addr
)
51 IO_COND(addr
, return inb(port
), return readb(addr
));
53 unsigned int fastcall
ioread16(void __iomem
*addr
)
55 IO_COND(addr
, return inw(port
), return readw(addr
));
57 unsigned int fastcall
ioread32(void __iomem
*addr
)
59 IO_COND(addr
, return inl(port
), return readl(addr
));
61 EXPORT_SYMBOL(ioread8
);
62 EXPORT_SYMBOL(ioread16
);
63 EXPORT_SYMBOL(ioread32
);
65 void fastcall
iowrite8(u8 val
, void __iomem
*addr
)
67 IO_COND(addr
, outb(val
,port
), writeb(val
, addr
));
69 void fastcall
iowrite16(u16 val
, void __iomem
*addr
)
71 IO_COND(addr
, outw(val
,port
), writew(val
, addr
));
73 void fastcall
iowrite32(u32 val
, void __iomem
*addr
)
75 IO_COND(addr
, outl(val
,port
), writel(val
, addr
));
77 EXPORT_SYMBOL(iowrite8
);
78 EXPORT_SYMBOL(iowrite16
);
79 EXPORT_SYMBOL(iowrite32
);
82 * These are the "repeat MMIO read/write" functions.
83 * Note the "__raw" accesses, since we don't want to
84 * convert to CPU byte order. We write in "IO byte
85 * order" (we also don't have IO barriers).
87 static inline void mmio_insb(void __iomem
*addr
, u8
*dst
, int count
)
89 while (--count
>= 0) {
90 u8 data
= __raw_readb(addr
);
95 static inline void mmio_insw(void __iomem
*addr
, u16
*dst
, int count
)
97 while (--count
>= 0) {
98 u16 data
= __raw_readw(addr
);
103 static inline void mmio_insl(void __iomem
*addr
, u32
*dst
, int count
)
105 while (--count
>= 0) {
106 u32 data
= __raw_readl(addr
);
112 static inline void mmio_outsb(void __iomem
*addr
, const u8
*src
, int count
)
114 while (--count
>= 0) {
115 __raw_writeb(*src
, addr
);
119 static inline void mmio_outsw(void __iomem
*addr
, const u16
*src
, int count
)
121 while (--count
>= 0) {
122 __raw_writew(*src
, addr
);
126 static inline void mmio_outsl(void __iomem
*addr
, const u32
*src
, int count
)
128 while (--count
>= 0) {
129 __raw_writel(*src
, addr
);
134 void fastcall
ioread8_rep(void __iomem
*addr
, void *dst
, unsigned long count
)
136 IO_COND(addr
, insb(port
,dst
,count
), mmio_insb(addr
, dst
, count
));
138 void fastcall
ioread16_rep(void __iomem
*addr
, void *dst
, unsigned long count
)
140 IO_COND(addr
, insw(port
,dst
,count
), mmio_insw(addr
, dst
, count
));
142 void fastcall
ioread32_rep(void __iomem
*addr
, void *dst
, unsigned long count
)
144 IO_COND(addr
, insl(port
,dst
,count
), mmio_insl(addr
, dst
, count
));
146 EXPORT_SYMBOL(ioread8_rep
);
147 EXPORT_SYMBOL(ioread16_rep
);
148 EXPORT_SYMBOL(ioread32_rep
);
150 void fastcall
iowrite8_rep(void __iomem
*addr
, const void *src
, unsigned long count
)
152 IO_COND(addr
, outsb(port
, src
, count
), mmio_outsb(addr
, src
, count
));
154 void fastcall
iowrite16_rep(void __iomem
*addr
, const void *src
, unsigned long count
)
156 IO_COND(addr
, outsw(port
, src
, count
), mmio_outsw(addr
, src
, count
));
158 void fastcall
iowrite32_rep(void __iomem
*addr
, const void *src
, unsigned long count
)
160 IO_COND(addr
, outsl(port
, src
,count
), mmio_outsl(addr
, src
, count
));
162 EXPORT_SYMBOL(iowrite8_rep
);
163 EXPORT_SYMBOL(iowrite16_rep
);
164 EXPORT_SYMBOL(iowrite32_rep
);
166 /* Create a virtual mapping cookie for an IO port range */
167 void __iomem
*ioport_map(unsigned long port
, unsigned int nr
)
171 return (void __iomem
*) (unsigned long) (port
+ PIO_OFFSET
);
174 void ioport_unmap(void __iomem
*addr
)
178 EXPORT_SYMBOL(ioport_map
);
179 EXPORT_SYMBOL(ioport_unmap
);
181 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
182 void __iomem
*pci_iomap(struct pci_dev
*dev
, int bar
, unsigned long maxlen
)
184 unsigned long start
= pci_resource_start(dev
, bar
);
185 unsigned long len
= pci_resource_len(dev
, bar
);
186 unsigned long flags
= pci_resource_flags(dev
, bar
);
190 if (maxlen
&& len
> maxlen
)
192 if (flags
& IORESOURCE_IO
)
193 return ioport_map(start
, len
);
194 if (flags
& IORESOURCE_MEM
) {
195 if (flags
& IORESOURCE_CACHEABLE
)
196 return ioremap(start
, len
);
197 return ioremap_nocache(start
, len
);
203 void pci_iounmap(struct pci_dev
*dev
, void __iomem
* addr
)
205 IO_COND(addr
, /* nothing */, iounmap(addr
));
207 EXPORT_SYMBOL(pci_iomap
);
208 EXPORT_SYMBOL(pci_iounmap
);