io.h 8.36 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

#define readb __raw_readb
56 57 58 59 60 61 62 63 64 65 66 67

#define readw readw
static inline u16 readw(const volatile void __iomem *addr)
{
	return __le16_to_cpu(__raw_readw(addr));
}

#define readl readl
static inline u32 readl(const volatile void __iomem *addr)
{
	return __le32_to_cpu(__raw_readl(addr));
}
68

69
#ifndef __raw_writeb
70 71 72 73
static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
{
	*(volatile u8 __force *) addr = b;
}
74
#endif
75

76
#ifndef __raw_writew
77 78 79 80
static inline void __raw_writew(u16 b, volatile void __iomem *addr)
{
	*(volatile u16 __force *) addr = b;
}
81
#endif
82

83
#ifndef __raw_writel
84 85 86 87
static inline void __raw_writel(u32 b, volatile void __iomem *addr)
{
	*(volatile u32 __force *) addr = b;
}
88
#endif
89 90 91 92 93 94

#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
95
#ifndef __raw_readq
96 97 98 99
static inline u64 __raw_readq(const volatile void __iomem *addr)
{
	return *(const volatile u64 __force *) addr;
}
Jan Glauber's avatar
Jan Glauber committed
100 101
#endif

102 103 104 105 106
#define readq readq
static inline u64 readq(const volatile void __iomem *addr)
{
	return __le64_to_cpu(__raw_readq(addr));
}
107

Jan Glauber's avatar
Jan Glauber committed
108
#ifndef __raw_writeq
109 110 111 112 113 114
static inline void __raw_writeq(u64 b, volatile void __iomem *addr)
{
	*(volatile u64 __force *) addr = b;
}
#endif

Jan Glauber's avatar
Jan Glauber committed
115 116 117
#define writeq(b, addr) __raw_writeq(__cpu_to_le64(b), addr)
#endif /* CONFIG_64BIT */

118 119 120 121
#ifndef PCI_IOBASE
#define PCI_IOBASE ((void __iomem *) 0)
#endif

122 123 124 125 126 127 128
/*****************************************************************************/
/*
 * traditional input/output functions
 */

static inline u8 inb(unsigned long addr)
{
129
	return readb(addr + PCI_IOBASE);
130 131 132 133
}

static inline u16 inw(unsigned long addr)
{
134
	return readw(addr + PCI_IOBASE);
135 136 137 138
}

static inline u32 inl(unsigned long addr)
{
139
	return readl(addr + PCI_IOBASE);
140 141 142 143
}

static inline void outb(u8 b, unsigned long addr)
{
144
	writeb(b, addr + PCI_IOBASE);
145 146 147 148
}

static inline void outw(u16 b, unsigned long addr)
{
149
	writew(b, addr + PCI_IOBASE);
150 151 152 153
}

static inline void outl(u32 b, unsigned long addr)
{
154
	writel(b, addr + PCI_IOBASE);
155 156 157 158 159 160 161 162 163
}

#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))

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

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

190
#ifndef insl
191 192 193 194 195
static inline void insl(unsigned long addr, void *buffer, int count)
{
	if (count) {
		u32 *buf = buffer;
		do {
196
			u32 x = __raw_readl(addr + PCI_IOBASE);
197 198 199 200
			*buf++ = x;
		} while (--count);
	}
}
201
#endif
202

203
#ifndef outsb
204 205 206 207 208
static inline void outsb(unsigned long addr, const void *buffer, int count)
{
	if (count) {
		const u8 *buf = buffer;
		do {
209
			__raw_writeb(*buf++, addr + PCI_IOBASE);
210 211 212
		} while (--count);
	}
}
213
#endif
214

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

227
#ifndef outsl
228 229 230 231 232
static inline void outsl(unsigned long addr, const void *buffer, int count)
{
	if (count) {
		const u32 *buf = buffer;
		do {
233
			__raw_writel(*buf++, addr + PCI_IOBASE);
234 235 236
		} while (--count);
	}
}
237
#endif
238 239 240 241

#ifndef CONFIG_GENERIC_IOMAP
#define ioread8(addr)		readb(addr)
#define ioread16(addr)		readw(addr)
242
#define ioread16be(addr)	__be16_to_cpu(__raw_readw(addr))
243
#define ioread32(addr)		readl(addr)
244
#define ioread32be(addr)	__be32_to_cpu(__raw_readl(addr))
245 246 247

#define iowrite8(v, addr)	writeb((v), (addr))
#define iowrite16(v, addr)	writew((v), (addr))
248
#define iowrite16be(v, addr)	__raw_writew(__cpu_to_be16(v), addr)
249
#define iowrite32(v, addr)	writel((v), (addr))
250
#define iowrite32be(v, addr)	__raw_writel(__cpu_to_be32(v), addr)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

#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 */

267 268 269
#ifndef IO_SPACE_LIMIT
#define IO_SPACE_LIMIT 0xffff
#endif
270 271 272 273 274 275 276 277

#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
278 279 280
extern void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long max);

#ifndef pci_iounmap
281 282 283
static inline void pci_iounmap(struct pci_dev *dev, void __iomem *p)
{
}
Jan Glauber's avatar
Jan Glauber committed
284
#endif
285 286 287 288 289 290
#endif /* CONFIG_GENERIC_IOMAP */

/*
 * Change virtual addresses to physical addresses and vv.
 * These are pretty trivial
 */
Jan Glauber's avatar
Jan Glauber committed
291
#ifndef virt_to_phys
292 293 294 295 296 297 298 299 300
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
301
#endif
302 303 304

/*
 * Change "struct page" to physical address.
305 306 307
 *
 * This implementation is for the no-MMU case only... if you have an MMU
 * you'll need to provide your own definitions.
308
 */
309
#ifndef CONFIG_MMU
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
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

325
static inline void iounmap(void __iomem *addr)
326 327
{
}
328
#endif /* CONFIG_MMU */
329

330
#ifdef CONFIG_HAS_IOPORT_MAP
331 332 333 334 335 336 337 338 339 340 341 342 343
#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 */
344
#endif /* CONFIG_HAS_IOPORT_MAP */
345

346
#ifndef xlate_dev_kmem_ptr
347
#define xlate_dev_kmem_ptr(p)	p
348 349
#endif
#ifndef xlate_dev_mem_ptr
350
#define xlate_dev_mem_ptr(p)	__va(p)
351
#endif
352

353
#ifdef CONFIG_VIRT_TO_BUS
354 355 356 357 358 359 360 361 362 363 364
#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
365
#endif
366

Jan Glauber's avatar
Jan Glauber committed
367
#ifndef memset_io
368
#define memset_io(a, b, c)	memset(__io_virt(a), (b), (c))
Jan Glauber's avatar
Jan Glauber committed
369 370 371
#endif

#ifndef memcpy_fromio
372
#define memcpy_fromio(a, b, c)	memcpy((a), __io_virt(b), (c))
Jan Glauber's avatar
Jan Glauber committed
373 374
#endif
#ifndef memcpy_toio
375
#define memcpy_toio(a, b, c)	memcpy(__io_virt(a), (b), (c))
Jan Glauber's avatar
Jan Glauber committed
376
#endif
377 378 379 380

#endif /* __KERNEL__ */

#endif /* __ASM_GENERIC_IO_H */