io.h 8.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Generic I/O port emulation, based on MN10300 code
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public Licence
 * as published by the Free Software Foundation; either version
 * 2 of the Licence, or (at your option) any later version.
 */
#ifndef __ASM_GENERIC_IO_H
#define __ASM_GENERIC_IO_H

#include <asm/page.h> /* I/O is all done through memory accesses */
#include <linux/types.h>

#ifdef CONFIG_GENERIC_IOMAP
#include <asm-generic/iomap.h>
#endif

21 22
#include <asm-generic/pci_iomap.h>

23
#ifndef mmiowb
24
#define mmiowb() do {} while (0)
25
#endif
26 27 28 29 30 31 32 33

/*****************************************************************************/
/*
 * readX/writeX() are used to access memory mapped devices. On some
 * architectures the memory mapped IO stuff needs to be accessed
 * differently. On the simple architectures, we just read/write the
 * memory location directly.
 */
34
#ifndef __raw_readb
35 36 37 38
static inline u8 __raw_readb(const volatile void __iomem *addr)
{
	return *(const volatile u8 __force *) addr;
}
39
#endif
40

41
#ifndef __raw_readw
42 43 44 45
static inline u16 __raw_readw(const volatile void __iomem *addr)
{
	return *(const volatile u16 __force *) addr;
}
46
#endif
47

48
#ifndef __raw_readl
49 50 51 52
static inline u32 __raw_readl(const volatile void __iomem *addr)
{
	return *(const volatile u32 __force *) addr;
}
53
#endif
54 55 56 57 58

#define readb __raw_readb
#define readw(addr) __le16_to_cpu(__raw_readw(addr))
#define readl(addr) __le32_to_cpu(__raw_readl(addr))

59
#ifndef __raw_writeb
60 61 62 63
static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
{
	*(volatile u8 __force *) addr = b;
}
64
#endif
65

66
#ifndef __raw_writew
67 68 69 70
static inline void __raw_writew(u16 b, volatile void __iomem *addr)
{
	*(volatile u16 __force *) addr = b;
}
71
#endif
72

73
#ifndef __raw_writel
74 75 76 77
static inline void __raw_writel(u32 b, volatile void __iomem *addr)
{
	*(volatile u32 __force *) addr = b;
}
78
#endif
79 80 81 82 83 84

#define writeb __raw_writeb
#define writew(b,addr) __raw_writew(__cpu_to_le16(b),addr)
#define writel(b,addr) __raw_writel(__cpu_to_le32(b),addr)

#ifdef CONFIG_64BIT
Jan Glauber's avatar
Jan Glauber committed
85
#ifndef __raw_readq
86 87 88 89
static inline u64 __raw_readq(const volatile void __iomem *addr)
{
	return *(const volatile u64 __force *) addr;
}
Jan Glauber's avatar
Jan Glauber committed
90 91
#endif

92 93
#define readq(addr) __le64_to_cpu(__raw_readq(addr))

Jan Glauber's avatar
Jan Glauber committed
94
#ifndef __raw_writeq
95 96 97 98 99 100
static inline void __raw_writeq(u64 b, volatile void __iomem *addr)
{
	*(volatile u64 __force *) addr = b;
}
#endif

Jan Glauber's avatar
Jan Glauber committed
101 102 103
#define writeq(b, addr) __raw_writeq(__cpu_to_le64(b), addr)
#endif /* CONFIG_64BIT */

104 105 106 107
#ifndef PCI_IOBASE
#define PCI_IOBASE ((void __iomem *) 0)
#endif

108 109 110 111 112 113 114
/*****************************************************************************/
/*
 * traditional input/output functions
 */

static inline u8 inb(unsigned long addr)
{
115
	return readb(addr + PCI_IOBASE);
116 117 118 119
}

static inline u16 inw(unsigned long addr)
{
120
	return readw(addr + PCI_IOBASE);
121 122 123 124
}

static inline u32 inl(unsigned long addr)
{
125
	return readl(addr + PCI_IOBASE);
126 127 128 129
}

static inline void outb(u8 b, unsigned long addr)
{
130
	writeb(b, addr + PCI_IOBASE);
131 132 133 134
}

static inline void outw(u16 b, unsigned long addr)
{
135
	writew(b, addr + PCI_IOBASE);
136 137 138 139
}

static inline void outl(u32 b, unsigned long addr)
{
140
	writel(b, addr + PCI_IOBASE);
141 142 143 144 145 146 147 148 149
}

#define inb_p(addr)	inb(addr)
#define inw_p(addr)	inw(addr)
#define inl_p(addr)	inl(addr)
#define outb_p(x, addr)	outb((x), (addr))
#define outw_p(x, addr)	outw((x), (addr))
#define outl_p(x, addr)	outl((x), (addr))

150
#ifndef insb
151 152 153 154 155
static inline void insb(unsigned long addr, void *buffer, int count)
{
	if (count) {
		u8 *buf = buffer;
		do {
156
			u8 x = __raw_readb(addr + PCI_IOBASE);
157 158 159 160
			*buf++ = x;
		} while (--count);
	}
}
161
#endif
162

163
#ifndef insw
164 165 166 167 168
static inline void insw(unsigned long addr, void *buffer, int count)
{
	if (count) {
		u16 *buf = buffer;
		do {
169
			u16 x = __raw_readw(addr + PCI_IOBASE);
170 171 172 173
			*buf++ = x;
		} while (--count);
	}
}
174
#endif
175

176
#ifndef insl
177 178 179 180 181
static inline void insl(unsigned long addr, void *buffer, int count)
{
	if (count) {
		u32 *buf = buffer;
		do {
182
			u32 x = __raw_readl(addr + PCI_IOBASE);
183 184 185 186
			*buf++ = x;
		} while (--count);
	}
}
187
#endif
188

189
#ifndef outsb
190 191 192 193 194
static inline void outsb(unsigned long addr, const void *buffer, int count)
{
	if (count) {
		const u8 *buf = buffer;
		do {
195
			__raw_writeb(*buf++, addr + PCI_IOBASE);
196 197 198
		} while (--count);
	}
}
199
#endif
200

201
#ifndef outsw
202 203 204 205 206
static inline void outsw(unsigned long addr, const void *buffer, int count)
{
	if (count) {
		const u16 *buf = buffer;
		do {
207
			__raw_writew(*buf++, addr + PCI_IOBASE);
208 209 210
		} while (--count);
	}
}
211
#endif
212

213
#ifndef outsl
214 215 216 217 218
static inline void outsl(unsigned long addr, const void *buffer, int count)
{
	if (count) {
		const u32 *buf = buffer;
		do {
219
			__raw_writel(*buf++, addr + PCI_IOBASE);
220 221 222
		} while (--count);
	}
}
223
#endif
224 225 226 227

#ifndef CONFIG_GENERIC_IOMAP
#define ioread8(addr)		readb(addr)
#define ioread16(addr)		readw(addr)
228
#define ioread16be(addr)	be16_to_cpu(ioread16(addr))
229
#define ioread32(addr)		readl(addr)
230
#define ioread32be(addr)	be32_to_cpu(ioread32(addr))
231 232 233

#define iowrite8(v, addr)	writeb((v), (addr))
#define iowrite16(v, addr)	writew((v), (addr))
234
#define iowrite16be(v, addr)	iowrite16(be16_to_cpu(v), (addr))
235
#define iowrite32(v, addr)	writel((v), (addr))
236
#define iowrite32be(v, addr)	iowrite32(be32_to_cpu(v), (addr))
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

#define ioread8_rep(p, dst, count) \
	insb((unsigned long) (p), (dst), (count))
#define ioread16_rep(p, dst, count) \
	insw((unsigned long) (p), (dst), (count))
#define ioread32_rep(p, dst, count) \
	insl((unsigned long) (p), (dst), (count))

#define iowrite8_rep(p, src, count) \
	outsb((unsigned long) (p), (src), (count))
#define iowrite16_rep(p, src, count) \
	outsw((unsigned long) (p), (src), (count))
#define iowrite32_rep(p, src, count) \
	outsl((unsigned long) (p), (src), (count))
#endif /* CONFIG_GENERIC_IOMAP */

253 254 255
#ifndef IO_SPACE_LIMIT
#define IO_SPACE_LIMIT 0xffff
#endif
256 257 258 259 260 261 262 263

#ifdef __KERNEL__

#include <linux/vmalloc.h>
#define __io_virt(x) ((void __force *) (x))

#ifndef CONFIG_GENERIC_IOMAP
struct pci_dev;
Jan Glauber's avatar
Jan Glauber committed
264 265 266
extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);

#ifndef pci_iounmap
267 268 269
static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
{
}
Jan Glauber's avatar
Jan Glauber committed
270
#endif
271 272 273 274 275 276
#endif /* CONFIG_GENERIC_IOMAP */

/*
 * Change virtual addresses to physical addresses and vv.
 * These are pretty trivial
 */
Jan Glauber's avatar
Jan Glauber committed
277
#ifndef virt_to_phys
278 279 280 281 282 283 284 285 286
static inline unsigned long virt_to_phys(volatile void *address)
{
	return __pa((unsigned long)address);
}

static inline void *phys_to_virt(unsigned long address)
{
	return __va(address);
}
Jan Glauber's avatar
Jan Glauber committed
287
#endif
288 289 290

/*
 * Change "struct page" to physical address.
291 292 293
 *
 * This implementation is for the no-MMU case only... if you have an MMU
 * you'll need to provide your own definitions.
294
 */
295
#ifndef CONFIG_MMU
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
{
	return (void __iomem*) (unsigned long)offset;
}

#define __ioremap(offset, size, flags)	ioremap(offset, size)

#ifndef ioremap_nocache
#define ioremap_nocache ioremap
#endif

#ifndef ioremap_wc
#define ioremap_wc ioremap_nocache
#endif

311
static inline void iounmap(void __iomem *addr)
312 313
{
}
314
#endif /* CONFIG_MMU */
315

316
#ifdef CONFIG_HAS_IOPORT
317 318 319 320 321 322 323 324 325 326 327 328 329
#ifndef CONFIG_GENERIC_IOMAP
static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
{
	return (void __iomem *) port;
}

static inline void ioport_unmap(void __iomem *p)
{
}
#else /* CONFIG_GENERIC_IOMAP */
extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
extern void ioport_unmap(void __iomem *p);
#endif /* CONFIG_GENERIC_IOMAP */
330
#endif /* CONFIG_HAS_IOPORT */
331 332

#define xlate_dev_kmem_ptr(p)	p
333
#define xlate_dev_mem_ptr(p)	__va(p)
334 335 336 337 338 339 340 341 342 343 344 345 346

#ifndef virt_to_bus
static inline unsigned long virt_to_bus(volatile void *address)
{
	return ((unsigned long) address);
}

static inline void *bus_to_virt(unsigned long address)
{
	return (void *) address;
}
#endif

Jan Glauber's avatar
Jan Glauber committed
347
#ifndef memset_io
348
#define memset_io(a, b, c)	memset(__io_virt(a), (b), (c))
Jan Glauber's avatar
Jan Glauber committed
349 350 351
#endif

#ifndef memcpy_fromio
352
#define memcpy_fromio(a, b, c)	memcpy((a), __io_virt(b), (c))
Jan Glauber's avatar
Jan Glauber committed
353 354
#endif
#ifndef memcpy_toio
355
#define memcpy_toio(a, b, c)	memcpy(__io_virt(a), (b), (c))
Jan Glauber's avatar
Jan Glauber committed
356
#endif
357 358 359 360

#endif /* __KERNEL__ */

#endif /* __ASM_GENERIC_IO_H */