1 /* compatibility layer for iomap functions used by mfgpt.
2 *
3 * This file has been extracted and inlined from linux-2.6.22/lib/iomap.c with
4 * the following original header and copyright :
5 *
6 * Implement the default iomap interfaces
7 *
8 * (C) Copyright 2004 Linus Torvalds
9 */
10 #include <linux/pci.h>
11
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
bad_io_access(unsigned long port,const char * access)39 static inline void bad_io_access(unsigned long port, const char *access)
40 {
41 static int count = 10;
42 if (count) {
43 count--;
44 printk(KERN_ERR "Bad IO access at port %lx (%s)\n", port, access);
45 WARN_ON(1);
46 }
47 }
48
49 /*
50 * Ugly macros are a way of life.
51 */
52 #define IO_COND(addr, is_pio, is_mmio) do { \
53 unsigned long port = (unsigned long)addr; \
54 if (port >= PIO_RESERVED) { \
55 is_mmio; \
56 } else if (port > PIO_OFFSET) { \
57 port &= PIO_MASK; \
58 is_pio; \
59 } else \
60 bad_io_access(port, #is_pio ); \
61 } while (0)
62
63 #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
ioread8(void __iomem * addr)73 static inline unsigned int ioread8(void __iomem *addr)
74 {
75 IO_COND(addr, return inb(port), return readb(addr));
76 return 0xff;
77 }
ioread16(void __iomem * addr)78 static inline unsigned int ioread16(void __iomem *addr)
79 {
80 IO_COND(addr, return inw(port), return readw(addr));
81 return 0xffff;
82 }
ioread16be(void __iomem * addr)83 static inline unsigned int ioread16be(void __iomem *addr)
84 {
85 IO_COND(addr, return pio_read16be(port), return mmio_read16be(addr));
86 return 0xffff;
87 }
ioread32(void __iomem * addr)88 static inline unsigned int ioread32(void __iomem *addr)
89 {
90 IO_COND(addr, return inl(port), return readl(addr));
91 return 0xffffffff;
92 }
ioread32be(void __iomem * addr)93 static inline unsigned int ioread32be(void __iomem *addr)
94 {
95 IO_COND(addr, return pio_read32be(port), return mmio_read32be(addr));
96 return 0xffffffff;
97 }
98
99 #ifndef pio_write16be
100 #define pio_write16be(val,port) outw(swab16(val),port)
101 #define pio_write32be(val,port) outl(swab32(val),port)
102 #endif
103
104 #ifndef mmio_write16be
105 #define mmio_write16be(val,port) __raw_writew(be16_to_cpu(val),port)
106 #define mmio_write32be(val,port) __raw_writel(be32_to_cpu(val),port)
107 #endif
108
iowrite8(u8 val,void __iomem * addr)109 static inline void iowrite8(u8 val, void __iomem *addr)
110 {
111 IO_COND(addr, outb(val,port), writeb(val, addr));
112 }
iowrite16(u16 val,void __iomem * addr)113 static inline void iowrite16(u16 val, void __iomem *addr)
114 {
115 IO_COND(addr, outw(val,port), writew(val, addr));
116 }
iowrite16be(u16 val,void __iomem * addr)117 static inline void iowrite16be(u16 val, void __iomem *addr)
118 {
119 IO_COND(addr, pio_write16be(val,port), mmio_write16be(val, addr));
120 }
iowrite32(u32 val,void __iomem * addr)121 static inline void iowrite32(u32 val, void __iomem *addr)
122 {
123 IO_COND(addr, outl(val,port), writel(val, addr));
124 }
iowrite32be(u32 val,void __iomem * addr)125 static inline void iowrite32be(u32 val, void __iomem *addr)
126 {
127 IO_COND(addr, pio_write32be(val,port), mmio_write32be(val, addr));
128 }
129
130 /*
131 * These are the "repeat MMIO read/write" functions.
132 * Note the "__raw" accesses, since we don't want to
133 * convert to CPU byte order. We write in "IO byte
134 * order" (we also don't have IO barriers).
135 */
136 #ifndef mmio_insb
mmio_insb(void __iomem * addr,u8 * dst,int count)137 static inline void mmio_insb(void __iomem *addr, u8 *dst, int count)
138 {
139 while (--count >= 0) {
140 u8 data = __raw_readb(addr);
141 *dst = data;
142 dst++;
143 }
144 }
mmio_insw(void __iomem * addr,u16 * dst,int count)145 static inline void mmio_insw(void __iomem *addr, u16 *dst, int count)
146 {
147 while (--count >= 0) {
148 u16 data = __raw_readw(addr);
149 *dst = data;
150 dst++;
151 }
152 }
mmio_insl(void __iomem * addr,u32 * dst,int count)153 static inline void mmio_insl(void __iomem *addr, u32 *dst, int count)
154 {
155 while (--count >= 0) {
156 u32 data = __raw_readl(addr);
157 *dst = data;
158 dst++;
159 }
160 }
161 #endif
162
163 #ifndef mmio_outsb
mmio_outsb(void __iomem * addr,const u8 * src,int count)164 static inline void mmio_outsb(void __iomem *addr, const u8 *src, int count)
165 {
166 while (--count >= 0) {
167 __raw_writeb(*src, addr);
168 src++;
169 }
170 }
mmio_outsw(void __iomem * addr,const u16 * src,int count)171 static inline void mmio_outsw(void __iomem *addr, const u16 *src, int count)
172 {
173 while (--count >= 0) {
174 __raw_writew(*src, addr);
175 src++;
176 }
177 }
mmio_outsl(void __iomem * addr,const u32 * src,int count)178 static inline void mmio_outsl(void __iomem *addr, const u32 *src, int count)
179 {
180 while (--count >= 0) {
181 __raw_writel(*src, addr);
182 src++;
183 }
184 }
185 #endif
186
ioread8_rep(void __iomem * addr,void * dst,unsigned long count)187 static inline void ioread8_rep(void __iomem *addr, void *dst, unsigned long count)
188 {
189 IO_COND(addr, insb(port,dst,count), mmio_insb(addr, dst, count));
190 }
ioread16_rep(void __iomem * addr,void * dst,unsigned long count)191 static inline void ioread16_rep(void __iomem *addr, void *dst, unsigned long count)
192 {
193 IO_COND(addr, insw(port,dst,count), mmio_insw(addr, dst, count));
194 }
ioread32_rep(void __iomem * addr,void * dst,unsigned long count)195 static inline void ioread32_rep(void __iomem *addr, void *dst, unsigned long count)
196 {
197 IO_COND(addr, insl(port,dst,count), mmio_insl(addr, dst, count));
198 }
199
iowrite8_rep(void __iomem * addr,const void * src,unsigned long count)200 static inline void iowrite8_rep(void __iomem *addr, const void *src, unsigned long count)
201 {
202 IO_COND(addr, outsb(port, src, count), mmio_outsb(addr, src, count));
203 }
iowrite16_rep(void __iomem * addr,const void * src,unsigned long count)204 static inline void iowrite16_rep(void __iomem *addr, const void *src, unsigned long count)
205 {
206 IO_COND(addr, outsw(port, src, count), mmio_outsw(addr, src, count));
207 }
iowrite32_rep(void __iomem * addr,const void * src,unsigned long count)208 static inline void iowrite32_rep(void __iomem *addr, const void *src, unsigned long count)
209 {
210 IO_COND(addr, outsl(port, src,count), mmio_outsl(addr, src, count));
211 }
212
213 /* Create a virtual mapping cookie for an IO port range */
ioport_map(unsigned long port,unsigned int nr)214 static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
215 {
216 if (port > PIO_MASK)
217 return NULL;
218 return (void __iomem *) (unsigned long) (port + PIO_OFFSET);
219 }
220
ioport_unmap(void __iomem * addr)221 static inline void ioport_unmap(void __iomem *addr)
222 {
223 /* Nothing to do */
224 }
225
226 /* Create a virtual mapping cookie for a PCI BAR (memory or IO) */
pci_iomap(struct pci_dev * dev,int bar,unsigned long maxlen)227 static inline void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
228 {
229 unsigned long start = pci_resource_start(dev, bar);
230 unsigned long len = pci_resource_len(dev, bar);
231 unsigned long flags = pci_resource_flags(dev, bar);
232
233 if (!len || !start)
234 return NULL;
235 if (maxlen && len > maxlen)
236 len = maxlen;
237 if (flags & IORESOURCE_IO)
238 return ioport_map(start, len);
239 if (flags & IORESOURCE_MEM) {
240 if (flags & IORESOURCE_CACHEABLE)
241 return ioremap(start, len);
242 return ioremap_nocache(start, len);
243 }
244 /* What? */
245 return NULL;
246 }
247
pci_iounmap(struct pci_dev * dev,void __iomem * addr)248 static inline void pci_iounmap(struct pci_dev *dev, void __iomem * addr)
249 {
250 IO_COND(addr, /* nothing */, iounmap(addr));
251 }
252