blob: 233034e46ec646dc5226e97b6285f8c33c89b88b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/io.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1996-2000 Russell King
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Modifications:
11 * 16-Sep-1996 RMK Inlined the inx/outx functions & optimised for both
12 * constant addresses and variable addresses.
13 * 04-Dec-1997 RMK Moved a lot of this stuff to the new architecture
14 * specific IO header files.
15 * 27-Mar-1999 PJB Second parameter of memcpy_toio is const..
16 * 04-Apr-1999 PJB Added check_signature.
17 * 12-Dec-1999 RMK More cleanups
18 * 18-Jun-2000 RMK Removed virt_to_* and friends definitions
19 * 05-Oct-2004 BJD Moved memory string functions to use void __iomem
20 */
21#ifndef __ASM_ARM_IO_H
22#define __ASM_ARM_IO_H
23
24#ifdef __KERNEL__
25
26#include <linux/types.h>
27#include <asm/byteorder.h>
28#include <asm/memory.h>
Catalin Marinas79f64db2010-07-28 22:01:55 +010029#include <asm/system.h>
Michael S. Tsirkine5bfb722011-11-24 20:57:23 +020030#include <asm-generic/pci_iomap.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/*
33 * ISA I/O bus memory addresses are 1:1 with the physical address.
34 */
35#define isa_virt_to_bus virt_to_phys
36#define isa_page_to_bus page_to_phys
37#define isa_bus_to_virt phys_to_virt
38
39/*
40 * Generic IO read/write. These perform native-endian accesses. Note
41 * that some architectures will want to re-define __raw_{read,write}w.
42 */
43extern void __raw_writesb(void __iomem *addr, const void *data, int bytelen);
44extern void __raw_writesw(void __iomem *addr, const void *data, int wordlen);
45extern void __raw_writesl(void __iomem *addr, const void *data, int longlen);
46
Deepak Saxenaa0d95af2005-12-05 10:54:59 +000047extern void __raw_readsb(const void __iomem *addr, void *data, int bytelen);
48extern void __raw_readsw(const void __iomem *addr, void *data, int wordlen);
49extern void __raw_readsl(const void __iomem *addr, void *data, int longlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#define __raw_writeb(v,a) (__chk_io_ptr(a), *(volatile unsigned char __force *)(a) = (v))
52#define __raw_writew(v,a) (__chk_io_ptr(a), *(volatile unsigned short __force *)(a) = (v))
53#define __raw_writel(v,a) (__chk_io_ptr(a), *(volatile unsigned int __force *)(a) = (v))
54
55#define __raw_readb(a) (__chk_io_ptr(a), *(volatile unsigned char __force *)(a))
56#define __raw_readw(a) (__chk_io_ptr(a), *(volatile unsigned short __force *)(a))
57#define __raw_readl(a) (__chk_io_ptr(a), *(volatile unsigned int __force *)(a))
58
59/*
Russell King67a19012005-11-17 16:48:00 +000060 * Architecture ioremap implementation.
61 */
Russell King3603ab22007-05-05 20:59:27 +010062#define MT_DEVICE 0
63#define MT_DEVICE_NONSHARED 1
64#define MT_DEVICE_CACHED 2
Russell Kingdb5b7162008-09-07 12:42:51 +010065#define MT_DEVICE_WC 3
Russell King3603ab22007-05-05 20:59:27 +010066/*
Russell Kingdb5b7162008-09-07 12:42:51 +010067 * types 4 onwards can be found in asm/mach/map.h and are undefined
Russell King3603ab22007-05-05 20:59:27 +010068 * for ioremap
69 */
70
71/*
72 * __arm_ioremap takes CPU physical address.
73 * __arm_ioremap_pfn takes a Page Frame Number and an offset into that page
Russell King31aa8fd2009-12-18 11:10:03 +000074 * The _caller variety takes a __builtin_return_address(0) value for
75 * /proc/vmalloc to use - and should only be used in non-inline functions.
Russell King3603ab22007-05-05 20:59:27 +010076 */
Russell King31aa8fd2009-12-18 11:10:03 +000077extern void __iomem *__arm_ioremap_pfn_caller(unsigned long, unsigned long,
78 size_t, unsigned int, void *);
79extern void __iomem *__arm_ioremap_caller(unsigned long, size_t, unsigned int,
80 void *);
81
82extern void __iomem *__arm_ioremap_pfn(unsigned long, unsigned long, size_t, unsigned int);
83extern void __iomem *__arm_ioremap(unsigned long, size_t, unsigned int);
Tony Lindgren6c5482d2011-10-12 01:02:50 +010084extern void __iomem *__arm_ioremap_exec(unsigned long, size_t, bool cached);
Al Viro16226052006-10-09 02:09:49 +010085extern void __iounmap(volatile void __iomem *addr);
Rob Herring4fe7ef32012-02-10 17:05:13 -060086extern void __arm_iounmap(volatile void __iomem *addr);
87
88extern void __iomem * (*arch_ioremap_caller)(unsigned long, size_t,
89 unsigned int, void *);
90extern void (*arch_iounmap)(volatile void __iomem *);
Russell King67a19012005-11-17 16:48:00 +000091
92/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * Bad read/write accesses...
94 */
95extern void __readwrite_bug(const char *fn);
96
97/*
Russell King0560cf52008-11-30 11:45:54 +000098 * A typesafe __io() helper
99 */
100static inline void __iomem *__typesafe_io(unsigned long addr)
101{
102 return (void __iomem *)addr;
103}
104
Russell Kingc1928022011-01-30 11:29:40 +0000105/* IO barriers */
106#ifdef CONFIG_ARM_DMA_MEM_BUFFERABLE
107#define __iormb() rmb()
108#define __iowmb() wmb()
109#else
110#define __iormb() do { } while (0)
111#define __iowmb() do { } while (0)
112#endif
113
Russell King0560cf52008-11-30 11:45:54 +0000114/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * Now, pick up the machine-defined IO definitions
116 */
Rob Herringc334bc12012-03-04 22:03:33 -0600117#ifdef CONFIG_NEED_MACH_IO_H
Russell Kinga09e64f2008-08-05 16:14:15 +0100118#include <mach/io.h>
Rob Herringc334bc12012-03-04 22:03:33 -0600119#else
120#define __io(a) ({ (void)(a); __typesafe_io(0); })
121#define __mem_pci(a) (a)
122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124/*
Russell King04e1c832011-07-06 12:49:59 +0100125 * This is the limit of PC card/PCI/ISA IO space, which is by default
126 * 64K if we have PC card, PCI or ISA support. Otherwise, default to
127 * zero to prevent ISA/PCI drivers claiming IO space (and potentially
128 * oopsing.)
129 *
130 * Only set this larger if you really need inb() et.al. to operate over
131 * a larger address space. Note that SOC_COMMON ioremaps each sockets
132 * IO space area, and so inb() et.al. must be defined to operate as per
133 * readb() et.al. on such platforms.
134 */
135#ifndef IO_SPACE_LIMIT
136#if defined(CONFIG_PCMCIA_SOC_COMMON) || defined(CONFIG_PCMCIA_SOC_COMMON_MODULE)
137#define IO_SPACE_LIMIT ((resource_size_t)0xffffffff)
138#elif defined(CONFIG_PCI) || defined(CONFIG_ISA) || defined(CONFIG_PCCARD)
139#define IO_SPACE_LIMIT ((resource_size_t)0xffff)
140#else
141#define IO_SPACE_LIMIT ((resource_size_t)0)
142#endif
143#endif
144
145/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 * IO port access primitives
147 * -------------------------
148 *
149 * The ARM doesn't have special IO access instructions; all IO is memory
150 * mapped. Note that these are defined to perform little endian accesses
151 * only. Their primary purpose is to access PCI and ISA peripherals.
152 *
153 * Note that for a big endian machine, this implies that the following
Russell Kingc79ebfa2005-06-27 14:23:38 +0100154 * big endian mode connectivity is in place, as described by numerous
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * ARM documents:
156 *
157 * PCI: D0-D7 D8-D15 D16-D23 D24-D31
158 * ARM: D24-D31 D16-D23 D8-D15 D0-D7
159 *
160 * The machine specific io.h include defines __io to translate an "IO"
161 * address to a memory address.
162 *
163 * Note that we prevent GCC re-ordering or caching values in expressions
164 * by introducing sequence points into the in*() definitions. Note that
165 * __raw_* do not guarantee this behaviour.
166 *
167 * The {in,out}[bwl] macros are for emulating x86-style PCI/ISA IO space.
168 */
169#ifdef __io
Russell Kingc1928022011-01-30 11:29:40 +0000170#define outb(v,p) ({ __iowmb(); __raw_writeb(v,__io(p)); })
171#define outw(v,p) ({ __iowmb(); __raw_writew((__force __u16) \
172 cpu_to_le16(v),__io(p)); })
173#define outl(v,p) ({ __iowmb(); __raw_writel((__force __u32) \
174 cpu_to_le32(v),__io(p)); })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Russell Kingc1928022011-01-30 11:29:40 +0000176#define inb(p) ({ __u8 __v = __raw_readb(__io(p)); __iormb(); __v; })
Olav Kongas05f98692005-04-29 22:08:34 +0100177#define inw(p) ({ __u16 __v = le16_to_cpu((__force __le16) \
Russell Kingc1928022011-01-30 11:29:40 +0000178 __raw_readw(__io(p))); __iormb(); __v; })
Olav Kongas05f98692005-04-29 22:08:34 +0100179#define inl(p) ({ __u32 __v = le32_to_cpu((__force __le32) \
Russell Kingc1928022011-01-30 11:29:40 +0000180 __raw_readl(__io(p))); __iormb(); __v; })
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182#define outsb(p,d,l) __raw_writesb(__io(p),d,l)
183#define outsw(p,d,l) __raw_writesw(__io(p),d,l)
184#define outsl(p,d,l) __raw_writesl(__io(p),d,l)
185
186#define insb(p,d,l) __raw_readsb(__io(p),d,l)
187#define insw(p,d,l) __raw_readsw(__io(p),d,l)
188#define insl(p,d,l) __raw_readsl(__io(p),d,l)
189#endif
190
191#define outb_p(val,port) outb((val),(port))
192#define outw_p(val,port) outw((val),(port))
193#define outl_p(val,port) outl((val),(port))
194#define inb_p(port) inb((port))
195#define inw_p(port) inw((port))
196#define inl_p(port) inl((port))
197
198#define outsb_p(port,from,len) outsb(port,from,len)
199#define outsw_p(port,from,len) outsw(port,from,len)
200#define outsl_p(port,from,len) outsl(port,from,len)
201#define insb_p(port,to,len) insb(port,to,len)
202#define insw_p(port,to,len) insw(port,to,len)
203#define insl_p(port,to,len) insl(port,to,len)
204
205/*
206 * String version of IO memory access ops:
207 */
Russell Kingd2f60742005-09-24 10:42:06 +0100208extern void _memcpy_fromio(void *, const volatile void __iomem *, size_t);
209extern void _memcpy_toio(volatile void __iomem *, const void *, size_t);
210extern void _memset_io(volatile void __iomem *, int, size_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212#define mmiowb()
213
214/*
215 * Memory access primitives
216 * ------------------------
217 *
218 * These perform PCI memory accesses via an ioremap region. They don't
219 * take an address as such, but a cookie.
220 *
221 * Again, this are defined to perform little endian accesses. See the
222 * IO port primitives for more information.
223 */
224#ifdef __mem_pci
Olof Johanssonb0c12642011-10-04 03:44:07 +0100225#define readb_relaxed(c) ({ u8 __r = __raw_readb(__mem_pci(c)); __r; })
226#define readw_relaxed(c) ({ u16 __r = le16_to_cpu((__force __le16) \
227 __raw_readw(__mem_pci(c))); __r; })
228#define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32) \
229 __raw_readl(__mem_pci(c))); __r; })
Catalin Marinase9367712010-07-28 22:00:54 +0100230
231#define writeb_relaxed(v,c) ((void)__raw_writeb(v,__mem_pci(c)))
232#define writew_relaxed(v,c) ((void)__raw_writew((__force u16) \
233 cpu_to_le16(v),__mem_pci(c)))
234#define writel_relaxed(v,c) ((void)__raw_writel((__force u32) \
235 cpu_to_le32(v),__mem_pci(c)))
236
Russell Kingb92b3612010-07-29 11:38:05 +0100237#define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; })
238#define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; })
239#define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; })
240
241#define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); })
242#define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); })
243#define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); })
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245#define readsb(p,d,l) __raw_readsb(__mem_pci(p),d,l)
246#define readsw(p,d,l) __raw_readsw(__mem_pci(p),d,l)
247#define readsl(p,d,l) __raw_readsl(__mem_pci(p),d,l)
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249#define writesb(p,d,l) __raw_writesb(__mem_pci(p),d,l)
250#define writesw(p,d,l) __raw_writesw(__mem_pci(p),d,l)
251#define writesl(p,d,l) __raw_writesl(__mem_pci(p),d,l)
252
253#define memset_io(c,v,l) _memset_io(__mem_pci(c),(v),(l))
254#define memcpy_fromio(a,c,l) _memcpy_fromio((a),__mem_pci(c),(l))
255#define memcpy_toio(c,a,l) _memcpy_toio(__mem_pci(c),(a),(l))
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257#elif !defined(readb)
258
259#define readb(c) (__readwrite_bug("readb"),0)
260#define readw(c) (__readwrite_bug("readw"),0)
261#define readl(c) (__readwrite_bug("readl"),0)
262#define writeb(v,c) __readwrite_bug("writeb")
263#define writew(v,c) __readwrite_bug("writew")
264#define writel(v,c) __readwrite_bug("writel")
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#define check_signature(io,sig,len) (0)
267
268#endif /* __mem_pci */
269
270/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 * ioremap and friends.
272 *
273 * ioremap takes a PCI memory address, as specified in
Paul Bolle395cf962011-08-15 02:02:26 +0200274 * Documentation/io-mapping.txt.
Deepak Saxena9d4ae722006-01-09 19:23:11 +0000275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Rob Herring21a53652012-03-06 15:21:45 -0600277#define ioremap(cookie,size) __arm_ioremap((cookie), (size), MT_DEVICE)
278#define ioremap_nocache(cookie,size) __arm_ioremap((cookie), (size), MT_DEVICE)
279#define ioremap_cached(cookie,size) __arm_ioremap((cookie), (size), MT_DEVICE_CACHED)
280#define ioremap_wc(cookie,size) __arm_ioremap((cookie), (size), MT_DEVICE_WC)
281#define iounmap __arm_iounmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283/*
Russell King09f05512005-06-20 18:44:37 +0100284 * io{read,write}{8,16,32} macros
285 */
Lennert Buytenhek7533fca2005-06-24 23:11:31 +0100286#ifndef ioread8
Russell Kingb92b3612010-07-29 11:38:05 +0100287#define ioread8(p) ({ unsigned int __v = __raw_readb(p); __iormb(); __v; })
288#define ioread16(p) ({ unsigned int __v = le16_to_cpu((__force __le16)__raw_readw(p)); __iormb(); __v; })
289#define ioread32(p) ({ unsigned int __v = le32_to_cpu((__force __le32)__raw_readl(p)); __iormb(); __v; })
Russell King09f05512005-06-20 18:44:37 +0100290
Arnd Bergmann06901bd2011-09-03 17:54:44 +0200291#define ioread16be(p) ({ unsigned int __v = be16_to_cpu((__force __be16)__raw_readw(p)); __iormb(); __v; })
292#define ioread32be(p) ({ unsigned int __v = be32_to_cpu((__force __be32)__raw_readl(p)); __iormb(); __v; })
293
Russell Kingb92b3612010-07-29 11:38:05 +0100294#define iowrite8(v,p) ({ __iowmb(); (void)__raw_writeb(v, p); })
295#define iowrite16(v,p) ({ __iowmb(); (void)__raw_writew((__force __u16)cpu_to_le16(v), p); })
296#define iowrite32(v,p) ({ __iowmb(); (void)__raw_writel((__force __u32)cpu_to_le32(v), p); })
Russell King09f05512005-06-20 18:44:37 +0100297
Arnd Bergmann06901bd2011-09-03 17:54:44 +0200298#define iowrite16be(v,p) ({ __iowmb(); (void)__raw_writew((__force __u16)cpu_to_be16(v), p); })
299#define iowrite32be(v,p) ({ __iowmb(); (void)__raw_writel((__force __u32)cpu_to_be32(v), p); })
300
Russell King09f05512005-06-20 18:44:37 +0100301#define ioread8_rep(p,d,c) __raw_readsb(p,d,c)
302#define ioread16_rep(p,d,c) __raw_readsw(p,d,c)
303#define ioread32_rep(p,d,c) __raw_readsl(p,d,c)
304
305#define iowrite8_rep(p,s,c) __raw_writesb(p,s,c)
306#define iowrite16_rep(p,s,c) __raw_writesw(p,s,c)
307#define iowrite32_rep(p,s,c) __raw_writesl(p,s,c)
308
309extern void __iomem *ioport_map(unsigned long port, unsigned int nr);
310extern void ioport_unmap(void __iomem *addr);
Lennert Buytenhek7533fca2005-06-24 23:11:31 +0100311#endif
Russell King09f05512005-06-20 18:44:37 +0100312
313struct pci_dev;
314
Russell King09f05512005-06-20 18:44:37 +0100315extern void pci_iounmap(struct pci_dev *dev, void __iomem *addr);
316
317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * can the hardware map this into one segment or not, given no other
319 * constraints.
320 */
321#define BIOVEC_MERGEABLE(vec1, vec2) \
322 ((bvec_to_phys((vec1)) + (vec1)->bv_len) == bvec_to_phys((vec2)))
323
Greg Ungerer95ba71f2007-05-17 06:22:41 +0100324#ifdef CONFIG_MMU
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100325#define ARCH_HAS_VALID_PHYS_ADDR_RANGE
326extern int valid_phys_addr_range(unsigned long addr, size_t size);
327extern int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
Nicolas Pitre087aaff2010-09-22 18:34:36 -0400328extern int devmem_is_allowed(unsigned long pfn);
Greg Ungerer95ba71f2007-05-17 06:22:41 +0100329#endif
Lennert Buytenhek51635ad2006-09-16 10:50:22 +0100330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331/*
332 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
333 * access
334 */
335#define xlate_dev_mem_ptr(p) __va(p)
336
337/*
338 * Convert a virtual cached pointer to an uncached pointer
339 */
340#define xlate_dev_kmem_ptr(p) p
341
Russell King1645f202006-08-28 12:45:16 +0100342/*
343 * Register ISA memory and port locations for glibc iopl/inb/outb
344 * emulation.
345 */
346extern void register_isa_ports(unsigned int mmio, unsigned int io,
347 unsigned int io_shift);
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349#endif /* __KERNEL__ */
350#endif /* __ASM_ARM_IO_H */