blob: ec464a6b95fe2cbd790bd63c17302a533f239a6e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __ASM_SH_IO_H
2#define __ASM_SH_IO_H
Paul Mundt37b7a972010-11-01 09:49:04 -04003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004/*
5 * Convention:
Paul Mundt14866542008-10-04 05:25:52 +09006 * read{b,w,l,q}/write{b,w,l,q} are for PCI,
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * while in{b,w,l}/out{b,w,l} are for ISA
Paul Mundt14866542008-10-04 05:25:52 +09008 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * In addition we have 'pausing' versions: in{b,w,l}_p/out{b,w,l}_p
10 * and 'string' versions: ins{b,w,l}/outs{b,w,l}
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Paul Mundt14866542008-10-04 05:25:52 +090012 * While read{b,w,l,q} and write{b,w,l,q} contain memory barriers
13 * automatically, there are also __raw versions, which do not.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
Paul Mundt4f744af2010-01-18 21:30:29 +090015#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <asm/addrspace.h>
18#include <asm/machvec.h>
Paul Mundtb66c1a32006-01-16 22:14:15 -080019#include <asm/pgtable.h>
20#include <asm-generic/iomap.h>
21
22#ifdef __KERNEL__
Paul Mundt37b7a972010-11-01 09:49:04 -040023#define __IO_PREFIX generic
Paul Mundtb66c1a32006-01-16 22:14:15 -080024#include <asm/io_generic.h>
Magnus Damme7cc9a72008-02-07 20:18:21 +090025#include <asm/io_trapped.h>
Paul Mundtb7e68d62012-03-29 16:05:10 +090026#include <mach/mangle-port.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Paul Mundt14866542008-10-04 05:25:52 +090028#define __raw_writeb(v,a) (__chk_io_ptr(a), *(volatile u8 __force *)(a) = (v))
29#define __raw_writew(v,a) (__chk_io_ptr(a), *(volatile u16 __force *)(a) = (v))
30#define __raw_writel(v,a) (__chk_io_ptr(a), *(volatile u32 __force *)(a) = (v))
31#define __raw_writeq(v,a) (__chk_io_ptr(a), *(volatile u64 __force *)(a) = (v))
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Paul Mundt14866542008-10-04 05:25:52 +090033#define __raw_readb(a) (__chk_io_ptr(a), *(volatile u8 __force *)(a))
34#define __raw_readw(a) (__chk_io_ptr(a), *(volatile u16 __force *)(a))
35#define __raw_readl(a) (__chk_io_ptr(a), *(volatile u32 __force *)(a))
36#define __raw_readq(a) (__chk_io_ptr(a), *(volatile u64 __force *)(a))
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Paul Mundtb7e68d62012-03-29 16:05:10 +090038#define readb_relaxed(c) ({ u8 __v = ioswabb(__raw_readb(c)); __v; })
39#define readw_relaxed(c) ({ u16 __v = ioswabw(__raw_readw(c)); __v; })
40#define readl_relaxed(c) ({ u32 __v = ioswabl(__raw_readl(c)); __v; })
41#define readq_relaxed(c) ({ u64 __v = ioswabq(__raw_readq(c)); __v; })
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Paul Mundtb7e68d62012-03-29 16:05:10 +090043#define writeb_relaxed(v,c) ((void)__raw_writeb((__force u8)ioswabb(v),c))
44#define writew_relaxed(v,c) ((void)__raw_writew((__force u16)ioswabw(v),c))
45#define writel_relaxed(v,c) ((void)__raw_writel((__force u32)ioswabl(v),c))
46#define writeq_relaxed(v,c) ((void)__raw_writeq((__force u64)ioswabq(v),c))
Paul Mundt37b7a972010-11-01 09:49:04 -040047
48#define readb(a) ({ u8 r_ = readb_relaxed(a); rmb(); r_; })
49#define readw(a) ({ u16 r_ = readw_relaxed(a); rmb(); r_; })
50#define readl(a) ({ u32 r_ = readl_relaxed(a); rmb(); r_; })
51#define readq(a) ({ u64 r_ = readq_relaxed(a); rmb(); r_; })
52
53#define writeb(v,a) ({ wmb(); writeb_relaxed((v),(a)); })
54#define writew(v,a) ({ wmb(); writew_relaxed((v),(a)); })
55#define writel(v,a) ({ wmb(); writel_relaxed((v),(a)); })
56#define writeq(v,a) ({ wmb(); writeq_relaxed((v),(a)); })
57
58#define readsb(p,d,l) __raw_readsb(p,d,l)
59#define readsw(p,d,l) __raw_readsw(p,d,l)
60#define readsl(p,d,l) __raw_readsl(p,d,l)
61
62#define writesb(p,d,l) __raw_writesb(p,d,l)
63#define writesw(p,d,l) __raw_writesw(p,d,l)
64#define writesl(p,d,l) __raw_writesl(p,d,l)
65
66#define __BUILD_UNCACHED_IO(bwlq, type) \
67static inline type read##bwlq##_uncached(unsigned long addr) \
68{ \
69 type ret; \
70 jump_to_uncached(); \
71 ret = __raw_read##bwlq(addr); \
72 back_to_cached(); \
73 return ret; \
74} \
75 \
76static inline void write##bwlq##_uncached(type v, unsigned long addr) \
77{ \
78 jump_to_uncached(); \
79 __raw_write##bwlq(v, addr); \
80 back_to_cached(); \
81}
82
83__BUILD_UNCACHED_IO(b, u8)
84__BUILD_UNCACHED_IO(w, u16)
85__BUILD_UNCACHED_IO(l, u32)
86__BUILD_UNCACHED_IO(q, u64)
87
88#define __BUILD_MEMORY_STRING(pfx, bwlq, type) \
89 \
90static inline void \
91pfx##writes##bwlq(volatile void __iomem *mem, const void *addr, \
92 unsigned int count) \
93{ \
94 const volatile type *__addr = addr; \
95 \
96 while (count--) { \
97 __raw_write##bwlq(*__addr, mem); \
98 __addr++; \
99 } \
100} \
101 \
102static inline void pfx##reads##bwlq(volatile void __iomem *mem, \
103 void *addr, unsigned int count) \
104{ \
105 volatile type *__addr = addr; \
106 \
107 while (count--) { \
108 *__addr = __raw_read##bwlq(mem); \
109 __addr++; \
110 } \
111}
112
113__BUILD_MEMORY_STRING(__raw_, b, u8)
114__BUILD_MEMORY_STRING(__raw_, w, u16)
115
116#ifdef CONFIG_SUPERH32
117void __raw_writesl(void __iomem *addr, const void *data, int longlen);
118void __raw_readsl(const void __iomem *addr, void *data, int longlen);
119#else
120__BUILD_MEMORY_STRING(__raw_, l, u32)
121#endif
122
123__BUILD_MEMORY_STRING(__raw_, q, u64)
124
125#ifdef CONFIG_HAS_IOPORT
126
127/*
128 * Slowdown I/O port space accesses for antique hardware.
129 */
130#undef CONF_SLOWDOWN_IO
131
132/*
133 * On SuperH I/O ports are memory mapped, so we access them using normal
134 * load/store instructions. sh_io_port_base is the virtual address to
135 * which all ports are being mapped.
136 */
137extern const unsigned long sh_io_port_base;
138
139static inline void __set_io_port_base(unsigned long pbase)
140{
141 *(unsigned long *)&sh_io_port_base = pbase;
142 barrier();
143}
144
145#ifdef CONFIG_GENERIC_IOMAP
146#define __ioport_map ioport_map
147#else
148extern void __iomem *__ioport_map(unsigned long addr, unsigned int size);
149#endif
150
151#ifdef CONF_SLOWDOWN_IO
152#define SLOW_DOWN_IO __raw_readw(sh_io_port_base)
153#else
154#define SLOW_DOWN_IO
155#endif
156
157#define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow) \
158 \
159static inline void pfx##out##bwlq##p(type val, unsigned long port) \
160{ \
161 volatile type *__addr; \
162 \
163 __addr = __ioport_map(port, sizeof(type)); \
164 *__addr = val; \
165 slow; \
166} \
167 \
168static inline type pfx##in##bwlq##p(unsigned long port) \
169{ \
170 volatile type *__addr; \
171 type __val; \
172 \
173 __addr = __ioport_map(port, sizeof(type)); \
174 __val = *__addr; \
175 slow; \
176 \
177 return __val; \
178}
179
180#define __BUILD_IOPORT_PFX(bus, bwlq, type) \
181 __BUILD_IOPORT_SINGLE(bus, bwlq, type, ,) \
182 __BUILD_IOPORT_SINGLE(bus, bwlq, type, _p, SLOW_DOWN_IO)
183
184#define BUILDIO_IOPORT(bwlq, type) \
185 __BUILD_IOPORT_PFX(, bwlq, type)
186
187BUILDIO_IOPORT(b, u8)
188BUILDIO_IOPORT(w, u16)
189BUILDIO_IOPORT(l, u32)
190BUILDIO_IOPORT(q, u64)
191
192#define __BUILD_IOPORT_STRING(bwlq, type) \
193 \
194static inline void outs##bwlq(unsigned long port, const void *addr, \
195 unsigned int count) \
196{ \
197 const volatile type *__addr = addr; \
198 \
199 while (count--) { \
200 out##bwlq(*__addr, port); \
201 __addr++; \
202 } \
203} \
204 \
205static inline void ins##bwlq(unsigned long port, void *addr, \
206 unsigned int count) \
207{ \
208 volatile type *__addr = addr; \
209 \
210 while (count--) { \
211 *__addr = in##bwlq(port); \
212 __addr++; \
213 } \
214}
215
216__BUILD_IOPORT_STRING(b, u8)
217__BUILD_IOPORT_STRING(w, u16)
218__BUILD_IOPORT_STRING(l, u32)
219__BUILD_IOPORT_STRING(q, u64)
220
221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Paul Mundt37b7a972010-11-01 09:49:04 -0400223#define IO_SPACE_LIMIT 0xffffffff
Paul Mundtb66c1a32006-01-16 22:14:15 -0800224
Paul Mundt14866542008-10-04 05:25:52 +0900225/* synco on SH-4A, otherwise a nop */
226#define mmiowb() wmb()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228/* We really want to try and get these to memcpy etc */
Paul Mundt14866542008-10-04 05:25:52 +0900229void memcpy_fromio(void *, const volatile void __iomem *, unsigned long);
230void memcpy_toio(volatile void __iomem *, const void *, unsigned long);
231void memset_io(volatile void __iomem *, int, unsigned long);
Paul Mundt959f85f2006-09-27 16:43:28 +0900232
Paul Mundtac490a42007-11-20 18:26:28 +0900233/* Quad-word real-mode I/O, don't ask.. */
234unsigned long long peek_real_address_q(unsigned long long addr);
235unsigned long long poke_real_address_q(unsigned long long addr,
236 unsigned long long val);
237
Paul Mundtda06b8d2007-11-09 12:58:12 +0900238#if !defined(CONFIG_MMU)
239#define virt_to_phys(address) ((unsigned long)(address))
240#define phys_to_virt(address) ((void *)(address))
Stuart Menefyd02b08f2007-11-30 17:52:53 +0900241#else
Paul Mundtda06b8d2007-11-09 12:58:12 +0900242#define virt_to_phys(address) (__pa(address))
243#define phys_to_virt(address) (__va(address))
Yoshinori Satoa2d1a5f2006-09-27 17:25:07 +0900244#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246/*
Paul Mundtda06b8d2007-11-09 12:58:12 +0900247 * On 32-bit SH, we traditionally have the whole physical address space
248 * mapped at all times (as MIPS does), so "ioremap()" and "iounmap()" do
249 * not need to do anything but place the address in the proper segment.
250 * This is true for P1 and P2 addresses, as well as some P3 ones.
251 * However, most of the P3 addresses and newer cores using extended
252 * addressing need to map through page tables, so the ioremap()
253 * implementation becomes a bit more complicated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 *
Paul Mundtda06b8d2007-11-09 12:58:12 +0900255 * See arch/sh/mm/ioremap.c for additional notes on this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 *
257 * We cheat a bit and always return uncachable areas until we've fixed
Paul Mundtb66c1a32006-01-16 22:14:15 -0800258 * the drivers to handle caching properly.
Paul Mundtda06b8d2007-11-09 12:58:12 +0900259 *
260 * On the SH-5 the concept of segmentation in the 1:1 PXSEG sense simply
261 * doesn't exist, so everything must go through page tables.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 */
Paul Mundtb66c1a32006-01-16 22:14:15 -0800263#ifdef CONFIG_MMU
Paul Mundt90e7d642010-02-23 16:20:53 +0900264void __iomem *__ioremap_caller(phys_addr_t offset, unsigned long size,
Paul Mundtd57d6402010-01-19 13:34:38 +0900265 pgprot_t prot, void *caller);
Paul Mundtb66c1a32006-01-16 22:14:15 -0800266void __iounmap(void __iomem *addr);
Paul Mundtccd80582008-04-25 12:58:40 +0900267
Paul Mundtb66c1a32006-01-16 22:14:15 -0800268static inline void __iomem *
Paul Mundt90e7d642010-02-23 16:20:53 +0900269__ioremap(phys_addr_t offset, unsigned long size, pgprot_t prot)
Paul Mundtbf3cded2009-12-14 14:23:41 +0900270{
Paul Mundtd57d6402010-01-19 13:34:38 +0900271 return __ioremap_caller(offset, size, prot, __builtin_return_address(0));
Paul Mundtbf3cded2009-12-14 14:23:41 +0900272}
273
274static inline void __iomem *
Paul Mundt90e7d642010-02-23 16:20:53 +0900275__ioremap_29bit(phys_addr_t offset, unsigned long size, pgprot_t prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Paul Mundta0ab3662010-01-13 18:31:48 +0900277#ifdef CONFIG_29BIT
Paul Mundt90e7d642010-02-23 16:20:53 +0900278 phys_addr_t last_addr = offset + size - 1;
Paul Mundtb66c1a32006-01-16 22:14:15 -0800279
280 /*
281 * For P1 and P2 space this is trivial, as everything is already
282 * mapped. Uncached access for P1 addresses are done through P2.
283 * In the P3 case or for addresses outside of the 29-bit space,
284 * mapping must be done by the PMB or by using page tables.
285 */
286 if (likely(PXSEG(offset) < P3SEG && PXSEG(last_addr) < P3SEG)) {
Paul Mundtefb3e342011-01-11 15:02:59 +0900287 u64 flags = pgprot_val(prot);
288
289 /*
290 * Anything using the legacy PTEA space attributes needs
291 * to be kicked down to page table mappings.
292 */
293 if (unlikely(flags & _PAGE_PCC_MASK))
294 return NULL;
295 if (unlikely(flags & _PAGE_CACHABLE))
Paul Mundtb66c1a32006-01-16 22:14:15 -0800296 return (void __iomem *)P1SEGADDR(offset);
297
298 return (void __iomem *)P2SEGADDR(offset);
299 }
Magnus Damm716777d2008-11-25 21:57:29 +0900300
301 /* P4 above the store queues are always mapped. */
302 if (unlikely(offset >= P3_ADDR_MAX))
303 return (void __iomem *)P4SEGADDR(offset);
Paul Mundtda06b8d2007-11-09 12:58:12 +0900304#endif
Paul Mundtb66c1a32006-01-16 22:14:15 -0800305
Paul Mundta0ab3662010-01-13 18:31:48 +0900306 return NULL;
307}
308
309static inline void __iomem *
Paul Mundt90e7d642010-02-23 16:20:53 +0900310__ioremap_mode(phys_addr_t offset, unsigned long size, pgprot_t prot)
Paul Mundta0ab3662010-01-13 18:31:48 +0900311{
312 void __iomem *ret;
313
314 ret = __ioremap_trapped(offset, size);
315 if (ret)
316 return ret;
317
Paul Mundtd57d6402010-01-19 13:34:38 +0900318 ret = __ioremap_29bit(offset, size, prot);
Paul Mundta0ab3662010-01-13 18:31:48 +0900319 if (ret)
320 return ret;
321
Paul Mundtd57d6402010-01-19 13:34:38 +0900322 return __ioremap(offset, size, prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
Magnus Damme6be3a22009-04-30 12:56:37 +0900324#else
Paul Mundtd57d6402010-01-19 13:34:38 +0900325#define __ioremap(offset, size, prot) ((void __iomem *)(offset))
326#define __ioremap_mode(offset, size, prot) ((void __iomem *)(offset))
Magnus Damme6be3a22009-04-30 12:56:37 +0900327#define __iounmap(addr) do { } while (0)
328#endif /* CONFIG_MMU */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Paul Mundt90e7d642010-02-23 16:20:53 +0900330static inline void __iomem *ioremap(phys_addr_t offset, unsigned long size)
Paul Mundtd57d6402010-01-19 13:34:38 +0900331{
332 return __ioremap_mode(offset, size, PAGE_KERNEL_NOCACHE);
333}
334
335static inline void __iomem *
Paul Mundt90e7d642010-02-23 16:20:53 +0900336ioremap_cache(phys_addr_t offset, unsigned long size)
Paul Mundtd57d6402010-01-19 13:34:38 +0900337{
338 return __ioremap_mode(offset, size, PAGE_KERNEL);
339}
340
Paul Mundt6d63e732010-01-19 14:00:14 +0900341#ifdef CONFIG_HAVE_IOREMAP_PROT
Paul Mundtd57d6402010-01-19 13:34:38 +0900342static inline void __iomem *
Paul Mundt90e7d642010-02-23 16:20:53 +0900343ioremap_prot(phys_addr_t offset, unsigned long size, unsigned long flags)
Paul Mundtd57d6402010-01-19 13:34:38 +0900344{
345 return __ioremap_mode(offset, size, __pgprot(flags));
346}
Paul Mundt6d63e732010-01-19 14:00:14 +0900347#endif
Paul Mundtd57d6402010-01-19 13:34:38 +0900348
Paul Mundtd627a2e2010-01-28 18:17:29 +0900349#ifdef CONFIG_IOREMAP_FIXED
Paul Mundt90e7d642010-02-23 16:20:53 +0900350extern void __iomem *ioremap_fixed(phys_addr_t, unsigned long, pgprot_t);
Paul Mundtd627a2e2010-01-28 18:17:29 +0900351extern int iounmap_fixed(void __iomem *);
352extern void ioremap_fixed_init(void);
353#else
354static inline void __iomem *
Paul Mundt90e7d642010-02-23 16:20:53 +0900355ioremap_fixed(phys_addr_t phys_addr, unsigned long size, pgprot_t prot)
Paul Mundtd627a2e2010-01-28 18:17:29 +0900356{
357 BUG();
358 return NULL;
359}
360
361static inline void ioremap_fixed_init(void) { }
362static inline int iounmap_fixed(void __iomem *addr) { return -EINVAL; }
363#endif
364
Paul Mundtd57d6402010-01-19 13:34:38 +0900365#define ioremap_nocache ioremap
Paul Mundtd57d6402010-01-19 13:34:38 +0900366#define iounmap __iounmap
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * Convert a physical pointer to a virtual kernel pointer for /dev/mem
370 * access
371 */
372#define xlate_dev_mem_ptr(p) __va(p)
373
374/*
375 * Convert a virtual cached pointer to an uncached pointer
376 */
377#define xlate_dev_kmem_ptr(p) p
378
Paul Mundt185aed72008-11-12 12:53:48 +0900379#define ARCH_HAS_VALID_PHYS_ADDR_RANGE
380int valid_phys_addr_range(unsigned long addr, size_t size);
381int valid_mmap_phys_addr_range(unsigned long pfn, size_t size);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#endif /* __KERNEL__ */
384
385#endif /* __ASM_SH_IO_H */