initial commit with v2.6.9
[linux-2.6.9-moxart.git] / lib / iomap.c
blobd30ff1a8edee2737638e77ae32d2dc32f06d45c9
1 /*
2 * Implement the default iomap interfaces
3 */
4 #include <linux/pci.h>
5 #include <linux/module.h>
6 #include <asm/io.h>
8 /*
9 * Read/write from/to an (offsettable) iomem cookie. It might be a PIO
10 * access or a MMIO access, these functions don't care. The info is
11 * encoded in the hardware mapping set up by the mapping functions
12 * (or the cookie itself, depending on implementation and hw).
14 * The generic routines don't assume any hardware mappings, and just
15 * encode the PIO/MMIO as part of the cookie. They coldly assume that
16 * the MMIO IO mappings are not in the low address range.
18 * Architectures for which this is not true can't use this generic
19 * implementation and should do their own copy.
21 * We encode the physical PIO addresses (0-0xffff) into the
22 * pointer by offsetting them with a constant (0x10000) and
23 * assuming that all the low addresses are always PIO. That means
24 * we can do some sanity checks on the low bits, and don't
25 * need to just take things for granted.
27 #define PIO_OFFSET 0x10000UL
28 #define PIO_MASK 0x0ffffUL
29 #define PIO_RESERVED 0x40000UL
32 * Ugly macros are a way of life.
34 #define VERIFY_PIO(port) BUG_ON((port & ~PIO_MASK) != PIO_OFFSET)
36 #define IO_COND(addr, is_pio, is_mmio) do { \
37 unsigned long port = (unsigned long __force)addr; \
38 if (port < PIO_RESERVED) { \
39 VERIFY_PIO(port); \
40 port &= PIO_MASK; \
41 is_pio; \
42 } else { \
43 is_mmio; \
44 } \
45 } while (0)
47 unsigned int fastcall ioread8(void __iomem *addr)
49 IO_COND(addr, return inb(port), return readb(addr));
51 unsigned int fastcall ioread16(void __iomem *addr)
53 IO_COND(addr, return inw(port), return readw(addr));
55 unsigned int fastcall ioread32(void __iomem *addr)
57 IO_COND(addr, return inl(port), return readl(addr));
59 EXPORT_SYMBOL(ioread8);
60 EXPORT_SYMBOL(ioread16);
61 EXPORT_SYMBOL(ioread32);
63 void fastcall iowrite8(u8 val, void __iomem *addr)
65 IO_COND(addr, outb(val,port), writeb(val, addr));
67 void fastcall iowrite16(u16 val, void __iomem *addr)
69 IO_COND(addr, outw(val,port), writew(val, addr));
71 void fastcall iowrite32(u32 val, void __iomem *addr)
73 IO_COND(addr, outl(val,port), writel(val, addr));
75 EXPORT_SYMBOL(iowrite8);
76 EXPORT_SYMBOL(iowrite16);
77 EXPORT_SYMBOL(iowrite32);
80 * These are the "repeat MMIO read/write" functions.
81 * Note the "__raw" accesses, since we don't want to
82 * convert to CPU byte order. We write in "IO byte
83 * order" (we also don't have IO barriers).
85 static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
87 while (--count >= 0) {
88 u8 data = __raw_readb(addr);
89 *dst = data;
90 dst++;
93 static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
95 while (--count >= 0) {
96 u16 data = __raw_readw(addr);
97 *dst = data;
98 dst++;
101 static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
103 while (--count >= 0) {
104 u32 data = __raw_readl(addr);
105 *dst = data;
106 dst++;
110 static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
112 while (--count >= 0) {
113 __raw_writeb(*src, addr);
114 src++;
117 static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
119 while (--count >= 0) {
120 __raw_writew(*src, addr);
121 src++;
124 static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
126 while (--count >= 0) {
127 __raw_writel(*src, addr);
128 src++;
132 void fastcall ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
134 IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
136 void fastcall ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
138 IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
140 void fastcall ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
142 IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
144 EXPORT_SYMBOL(ioread8_rep);
145 EXPORT_SYMBOL(ioread16_rep);
146 EXPORT_SYMBOL(ioread32_rep);
148 void fastcall iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
150 IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
152 void fastcall iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
154 IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
156 void fastcall iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
158 IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
160 EXPORT_SYMBOL(iowrite8_rep);
161 EXPORT_SYMBOL(iowrite16_rep);
162 EXPORT_SYMBOL(iowrite32_rep);
164 /* Create a virtual mapping cookie for an IO port range */
165 void __iomem *ioport_map(unsigned long port, unsigned int nr)
167 if (port > PIO_MASK)
168 return NULL;
169 return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
172 void ioport_unmap(void __iomem *addr)
174 /* Nothing to do */
176 EXPORT_SYMBOL(ioport_map);
177 EXPORT_SYMBOL(ioport_unmap);
179 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
180 void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
182 unsigned long start = pci_resource_start(dev, bar);
183 unsigned long len = pci_resource_len(dev, bar);
184 unsigned long flags = pci_resource_flags(dev, bar);
186 if (!len || !start)
187 return NULL;
188 if (maxlen && len > maxlen)
189 len = maxlen;
190 if (flags & IORESOURCE_IO)
191 return ioport_map(start, len);
192 if (flags & IORESOURCE_MEM) {
193 if (flags & IORESOURCE_CACHEABLE)
194 return ioremap(start, len);
195 return ioremap_nocache(start, len);
197 /* What? */
198 return NULL;
201 void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
203 IO_COND(addr, /* nothing */, iounmap(addr));
205 EXPORT_SYMBOL(pci_iomap);
206 EXPORT_SYMBOL(pci_iounmap);