blob: b8ac5a5ac5417b68c481c5019438587e907ca6f2 [file] [log] [blame]
wdenk4fc95692003-02-28 00:49:47 +00001/*
wdenk4fc95692003-02-28 00:49:47 +00002 * Copyright (C) 1994, 1995 Waldorf GmbH
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +01003 * Copyright (C) 1994 - 2000, 06 Ralf Baechle
wdenk4fc95692003-02-28 00:49:47 +00004 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +01005 * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
6 * Author: Maciej W. Rozycki <macro@mips.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0
wdenk4fc95692003-02-28 00:49:47 +00009 */
10#ifndef _ASM_IO_H
11#define _ASM_IO_H
12
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010013#include <linux/compiler.h>
14#include <linux/types.h>
15
wdenk4fc95692003-02-28 00:49:47 +000016#include <asm/addrspace.h>
17#include <asm/byteorder.h>
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010018#include <asm/cpu-features.h>
19#include <asm/pgtable-bits.h>
20#include <asm/processor.h>
21#include <asm/string.h>
22
23#include <ioremap.h>
24#include <mangle-port.h>
25#include <spaces.h>
wdenk4fc95692003-02-28 00:49:47 +000026
27/*
28 * Slowdown I/O port space accesses for antique hardware.
29 */
30#undef CONF_SLOWDOWN_IO
31
32/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010033 * Raw operations are never swapped in software. OTOH values that raw
34 * operations are working on may or may not have been swapped by the bus
35 * hardware. An example use would be for flash memory that's used for
36 * execute in place.
wdenk4fc95692003-02-28 00:49:47 +000037 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010038# define __raw_ioswabb(a, x) (x)
39# define __raw_ioswabw(a, x) (x)
40# define __raw_ioswabl(a, x) (x)
41# define __raw_ioswabq(a, x) (x)
42# define ____raw_ioswabq(a, x) (x)
wdenk4fc95692003-02-28 00:49:47 +000043
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010044/* ioswab[bwlq], __mem_ioswab[bwlq] are defined in mangle-port.h */
wdenk4fc95692003-02-28 00:49:47 +000045
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010046#define IO_SPACE_LIMIT 0xffff
wdenk4fc95692003-02-28 00:49:47 +000047
48/*
49 * On MIPS I/O ports are memory mapped, so we access them using normal
50 * load/store instructions. mips_io_port_base is the virtual address to
51 * which all ports are being mapped. For sake of efficiency some code
52 * assumes that this is an address that can be loaded with a single lui
53 * instruction, so the lower 16 bits must be zero. Should be true on
54 * on any sane architecture; generic code does not use this assumption.
55 */
Jean-Christophe PLAGNIOL-VILLARD089dbb72007-11-13 09:11:05 +010056extern const unsigned long mips_io_port_base;
57
58/*
59 * Gcc will generate code to load the value of mips_io_port_base after each
60 * function call which may be fairly wasteful in some cases. So we don't
61 * play quite by the book. We tell gcc mips_io_port_base is a long variable
62 * which solves the code generation issue. Now we need to violate the
63 * aliasing rules a little to make initialization possible and finally we
64 * will need the barrier() to fight side effects of the aliasing chat.
65 * This trickery will eventually collapse under gcc's optimizer. Oh well.
66 */
67static inline void set_io_port_base(unsigned long base)
68{
69 * (unsigned long *) &mips_io_port_base = base;
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +010070 barrier();
Jean-Christophe PLAGNIOL-VILLARD089dbb72007-11-13 09:11:05 +010071}
wdenk4fc95692003-02-28 00:49:47 +000072
73/*
74 * Thanks to James van Artsdalen for a better timing-fix than
75 * the two short jumps: using outb's to a nonexistent port seems
76 * to guarantee better timings even on fast machines.
77 *
78 * On the other hand, I'd like to be sure of a non-existent port:
79 * I feel a bit unsafe about using 0x80 (should be safe, though)
80 *
81 * Linus
82 *
83 */
84
85#define __SLOW_DOWN_IO \
86 __asm__ __volatile__( \
87 "sb\t$0,0x80(%0)" \
88 : : "r" (mips_io_port_base));
89
90#ifdef CONF_SLOWDOWN_IO
91#ifdef REALLY_SLOW_IO
92#define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
93#else
94#define SLOW_DOWN_IO __SLOW_DOWN_IO
95#endif
96#else
97#define SLOW_DOWN_IO
98#endif
99
100/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100101 * virt_to_phys - map virtual addresses to physical
102 * @address: address to remap
103 *
104 * The returned physical address is the physical (CPU) mapping for
105 * the memory address given. It is only valid to use this function on
106 * addresses directly mapped or allocated via kmalloc.
107 *
108 * This function does not give bus mappings for DMA transfers. In
109 * almost all conceivable cases a device driver should not be using
110 * this function
wdenk4fc95692003-02-28 00:49:47 +0000111 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100112static inline unsigned long virt_to_phys(volatile const void *address)
wdenk4fc95692003-02-28 00:49:47 +0000113{
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100114 unsigned long addr = (unsigned long)address;
wdenk4fc95692003-02-28 00:49:47 +0000115
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100116 /* this corresponds to kernel implementation of __pa() */
117#ifdef CONFIG_64BIT
118 if (addr < CKSEG0)
119 return XPHYSADDR(addr);
120
121 return CPHYSADDR(addr);
Zhi-zhou Zhangdc3c2512012-10-16 15:02:08 +0200122#else
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100123 return addr - PAGE_OFFSET + PHYS_OFFSET;
Zhi-zhou Zhangdc3c2512012-10-16 15:02:08 +0200124#endif
wdenk4fc95692003-02-28 00:49:47 +0000125}
126
127/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100128 * phys_to_virt - map physical address to virtual
129 * @address: address to remap
130 *
131 * The returned virtual address is a current CPU mapping for
132 * the memory address given. It is only valid to use this function on
133 * addresses that have a kernel mapping
134 *
135 * This function does not handle bus mappings for DMA transfers. In
136 * almost all conceivable cases a device driver should not be using
137 * this function
wdenk4fc95692003-02-28 00:49:47 +0000138 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100139static inline void *phys_to_virt(unsigned long address)
wdenk4fc95692003-02-28 00:49:47 +0000140{
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100141 return (void *)(address + PAGE_OFFSET - PHYS_OFFSET);
wdenk4fc95692003-02-28 00:49:47 +0000142}
143
144/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100145 * ISA I/O bus memory addresses are 1:1 with the physical address.
wdenk4fc95692003-02-28 00:49:47 +0000146 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100147static inline unsigned long isa_virt_to_bus(volatile void *address)
wdenk4fc95692003-02-28 00:49:47 +0000148{
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100149 return (unsigned long)address - PAGE_OFFSET;
wdenk4fc95692003-02-28 00:49:47 +0000150}
151
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100152static inline void *isa_bus_to_virt(unsigned long address)
wdenk4fc95692003-02-28 00:49:47 +0000153{
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100154 return (void *)(address + PAGE_OFFSET);
wdenk4fc95692003-02-28 00:49:47 +0000155}
156
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100157#define isa_page_to_bus page_to_phys
wdenk4fc95692003-02-28 00:49:47 +0000158
159/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100160 * However PCI ones are not necessarily 1:1 and therefore these interfaces
161 * are forbidden in portable PCI drivers.
162 *
163 * Allow them for x86 for legacy drivers, though.
wdenk4fc95692003-02-28 00:49:47 +0000164 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100165#define virt_to_bus virt_to_phys
166#define bus_to_virt phys_to_virt
wdenk4fc95692003-02-28 00:49:47 +0000167
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100168static inline void __iomem *__ioremap_mode(phys_addr_t offset, unsigned long size,
169 unsigned long flags)
170{
171 void __iomem *addr;
172 phys_addr_t phys_addr;
wdenk4fc95692003-02-28 00:49:47 +0000173
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100174 addr = plat_ioremap(offset, size, flags);
175 if (addr)
176 return addr;
wdenk4fc95692003-02-28 00:49:47 +0000177
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100178 phys_addr = fixup_bigphys_addr(offset, size);
179 return (void __iomem *)(unsigned long)CKSEG1ADDR(phys_addr);
180}
wdenk4fc95692003-02-28 00:49:47 +0000181
182/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100183 * ioremap - map bus memory into CPU space
184 * @offset: bus address of the memory
185 * @size: size of the resource to map
186 *
187 * ioremap performs a platform specific sequence of operations to
188 * make bus memory CPU accessible via the readb/readw/readl/writeb/
189 * writew/writel functions and the other mmio helpers. The returned
190 * address is not guaranteed to be usable directly as a virtual
191 * address.
wdenk4fc95692003-02-28 00:49:47 +0000192 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100193#define ioremap(offset, size) \
194 __ioremap_mode((offset), (size), _CACHE_UNCACHED)
wdenk4fc95692003-02-28 00:49:47 +0000195
196/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100197 * ioremap_nocache - map bus memory into CPU space
198 * @offset: bus address of the memory
199 * @size: size of the resource to map
200 *
201 * ioremap_nocache performs a platform specific sequence of operations to
202 * make bus memory CPU accessible via the readb/readw/readl/writeb/
203 * writew/writel functions and the other mmio helpers. The returned
204 * address is not guaranteed to be usable directly as a virtual
205 * address.
206 *
207 * This version of ioremap ensures that the memory is marked uncachable
208 * on the CPU as well as honouring existing caching rules from things like
209 * the PCI bus. Note that there are other caches and buffers on many
210 * busses. In particular driver authors should read up on PCI writes
211 *
212 * It's useful if some control registers are in such an area and
213 * write combining or read caching is not desirable:
wdenk4fc95692003-02-28 00:49:47 +0000214 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100215#define ioremap_nocache(offset, size) \
216 __ioremap_mode((offset), (size), _CACHE_UNCACHED)
217#define ioremap_uc ioremap_nocache
wdenk4fc95692003-02-28 00:49:47 +0000218
219/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100220 * ioremap_cachable - map bus memory into CPU space
221 * @offset: bus address of the memory
222 * @size: size of the resource to map
223 *
224 * ioremap_nocache performs a platform specific sequence of operations to
225 * make bus memory CPU accessible via the readb/readw/readl/writeb/
226 * writew/writel functions and the other mmio helpers. The returned
227 * address is not guaranteed to be usable directly as a virtual
228 * address.
229 *
230 * This version of ioremap ensures that the memory is marked cachable by
231 * the CPU. Also enables full write-combining. Useful for some
232 * memory-like regions on I/O busses.
wdenk4fc95692003-02-28 00:49:47 +0000233 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100234#define ioremap_cachable(offset, size) \
235 __ioremap_mode((offset), (size), _page_cachable_default)
wdenk4fc95692003-02-28 00:49:47 +0000236
237/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100238 * These two are MIPS specific ioremap variant. ioremap_cacheable_cow
239 * requests a cachable mapping, ioremap_uncached_accelerated requests a
240 * mapping using the uncached accelerated mode which isn't supported on
241 * all processors.
wdenk4fc95692003-02-28 00:49:47 +0000242 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100243#define ioremap_cacheable_cow(offset, size) \
244 __ioremap_mode((offset), (size), _CACHE_CACHABLE_COW)
245#define ioremap_uncached_accelerated(offset, size) \
246 __ioremap_mode((offset), (size), _CACHE_UNCACHED_ACCELERATED)
wdenk4fc95692003-02-28 00:49:47 +0000247
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100248static inline void iounmap(const volatile void __iomem *addr)
249{
250 plat_iounmap(addr);
251}
wdenk4fc95692003-02-28 00:49:47 +0000252
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100253#ifdef CONFIG_CPU_CAVIUM_OCTEON
254#define war_octeon_io_reorder_wmb() wmb()
255#else
256#define war_octeon_io_reorder_wmb() do { } while (0)
257#endif
wdenk4fc95692003-02-28 00:49:47 +0000258
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100259#define __BUILD_MEMORY_SINGLE(pfx, bwlq, type, irq) \
260 \
261static inline void pfx##write##bwlq(type val, \
262 volatile void __iomem *mem) \
263{ \
264 volatile type *__mem; \
265 type __val; \
266 \
267 war_octeon_io_reorder_wmb(); \
268 \
269 __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
270 \
271 __val = pfx##ioswab##bwlq(__mem, val); \
272 \
273 if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
274 *__mem = __val; \
275 else if (cpu_has_64bits) { \
276 type __tmp; \
277 \
278 __asm__ __volatile__( \
279 ".set arch=r4000" "\t\t# __writeq""\n\t" \
280 "dsll32 %L0, %L0, 0" "\n\t" \
281 "dsrl32 %L0, %L0, 0" "\n\t" \
282 "dsll32 %M0, %M0, 0" "\n\t" \
283 "or %L0, %L0, %M0" "\n\t" \
284 "sd %L0, %2" "\n\t" \
285 ".set mips0" "\n" \
286 : "=r" (__tmp) \
287 : "0" (__val), "m" (*__mem)); \
288 } else \
289 BUG(); \
290} \
291 \
292static inline type pfx##read##bwlq(const volatile void __iomem *mem) \
293{ \
294 volatile type *__mem; \
295 type __val; \
296 \
297 __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
298 \
299 if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
300 __val = *__mem; \
301 else if (cpu_has_64bits) { \
302 __asm__ __volatile__( \
303 ".set arch=r4000" "\t\t# __readq" "\n\t" \
304 "ld %L0, %1" "\n\t" \
305 "dsra32 %M0, %L0, 0" "\n\t" \
306 "sll %L0, %L0, 0" "\n\t" \
307 ".set mips0" "\n" \
308 : "=r" (__val) \
309 : "m" (*__mem)); \
310 } else { \
311 __val = 0; \
312 BUG(); \
313 } \
314 \
315 return pfx##ioswab##bwlq(__mem, __val); \
316}
wdenk4fc95692003-02-28 00:49:47 +0000317
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100318#define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow) \
319 \
320static inline void pfx##out##bwlq##p(type val, unsigned long port) \
321{ \
322 volatile type *__addr; \
323 type __val; \
324 \
325 war_octeon_io_reorder_wmb(); \
326 \
327 __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \
328 \
329 __val = pfx##ioswab##bwlq(__addr, val); \
330 \
331 /* Really, we want this to be atomic */ \
332 BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
333 \
334 *__addr = __val; \
335 slow; \
336} \
337 \
338static inline type pfx##in##bwlq##p(unsigned long port) \
339{ \
340 volatile type *__addr; \
341 type __val; \
342 \
343 __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \
344 \
345 BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
346 \
347 __val = *__addr; \
348 slow; \
349 \
350 return pfx##ioswab##bwlq(__addr, __val); \
351}
wdenk4fc95692003-02-28 00:49:47 +0000352
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100353#define __BUILD_MEMORY_PFX(bus, bwlq, type) \
354 \
355__BUILD_MEMORY_SINGLE(bus, bwlq, type, 1)
wdenk4fc95692003-02-28 00:49:47 +0000356
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100357#define BUILDIO_MEM(bwlq, type) \
358 \
359__BUILD_MEMORY_PFX(__raw_, bwlq, type) \
360__BUILD_MEMORY_PFX(, bwlq, type) \
361__BUILD_MEMORY_PFX(__mem_, bwlq, type) \
wdenk4fc95692003-02-28 00:49:47 +0000362
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100363BUILDIO_MEM(b, u8)
364BUILDIO_MEM(w, u16)
365BUILDIO_MEM(l, u32)
366BUILDIO_MEM(q, u64)
wdenk4fc95692003-02-28 00:49:47 +0000367
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100368#define __BUILD_IOPORT_PFX(bus, bwlq, type) \
369 __BUILD_IOPORT_SINGLE(bus, bwlq, type, ,) \
370 __BUILD_IOPORT_SINGLE(bus, bwlq, type, _p, SLOW_DOWN_IO)
wdenk4fc95692003-02-28 00:49:47 +0000371
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100372#define BUILDIO_IOPORT(bwlq, type) \
373 __BUILD_IOPORT_PFX(, bwlq, type) \
374 __BUILD_IOPORT_PFX(__mem_, bwlq, type)
wdenk4fc95692003-02-28 00:49:47 +0000375
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100376BUILDIO_IOPORT(b, u8)
377BUILDIO_IOPORT(w, u16)
378BUILDIO_IOPORT(l, u32)
379#ifdef CONFIG_64BIT
380BUILDIO_IOPORT(q, u64)
381#endif
wdenk4fc95692003-02-28 00:49:47 +0000382
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100383#define __BUILDIO(bwlq, type) \
384 \
385__BUILD_MEMORY_SINGLE(____raw_, bwlq, type, 0)
wdenk4fc95692003-02-28 00:49:47 +0000386
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100387__BUILDIO(q, u64)
wdenk4fc95692003-02-28 00:49:47 +0000388
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100389#define readb_relaxed readb
390#define readw_relaxed readw
391#define readl_relaxed readl
392#define readq_relaxed readq
wdenk4fc95692003-02-28 00:49:47 +0000393
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100394#define writeb_relaxed writeb
395#define writew_relaxed writew
396#define writel_relaxed writel
397#define writeq_relaxed writeq
wdenk4fc95692003-02-28 00:49:47 +0000398
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100399#define readb_be(addr) \
400 __raw_readb((__force unsigned *)(addr))
401#define readw_be(addr) \
402 be16_to_cpu(__raw_readw((__force unsigned *)(addr)))
403#define readl_be(addr) \
404 be32_to_cpu(__raw_readl((__force unsigned *)(addr)))
405#define readq_be(addr) \
406 be64_to_cpu(__raw_readq((__force unsigned *)(addr)))
wdenk4fc95692003-02-28 00:49:47 +0000407
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100408#define writeb_be(val, addr) \
409 __raw_writeb((val), (__force unsigned *)(addr))
410#define writew_be(val, addr) \
411 __raw_writew(cpu_to_be16((val)), (__force unsigned *)(addr))
412#define writel_be(val, addr) \
413 __raw_writel(cpu_to_be32((val)), (__force unsigned *)(addr))
414#define writeq_be(val, addr) \
415 __raw_writeq(cpu_to_be64((val)), (__force unsigned *)(addr))
wdenk4fc95692003-02-28 00:49:47 +0000416
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100417/*
418 * Some code tests for these symbols
419 */
420#define readq readq
421#define writeq writeq
wdenk4fc95692003-02-28 00:49:47 +0000422
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100423#define __BUILD_MEMORY_STRING(bwlq, type) \
424 \
425static inline void writes##bwlq(volatile void __iomem *mem, \
426 const void *addr, unsigned int count) \
427{ \
428 const volatile type *__addr = addr; \
429 \
430 while (count--) { \
431 __mem_write##bwlq(*__addr, mem); \
432 __addr++; \
433 } \
434} \
435 \
436static inline void reads##bwlq(volatile void __iomem *mem, void *addr, \
437 unsigned int count) \
438{ \
439 volatile type *__addr = addr; \
440 \
441 while (count--) { \
442 *__addr = __mem_read##bwlq(mem); \
443 __addr++; \
444 } \
445}
wdenk4fc95692003-02-28 00:49:47 +0000446
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100447#define __BUILD_IOPORT_STRING(bwlq, type) \
448 \
449static inline void outs##bwlq(unsigned long port, const void *addr, \
450 unsigned int count) \
451{ \
452 const volatile type *__addr = addr; \
453 \
454 while (count--) { \
455 __mem_out##bwlq(*__addr, port); \
456 __addr++; \
457 } \
458} \
459 \
460static inline void ins##bwlq(unsigned long port, void *addr, \
461 unsigned int count) \
462{ \
463 volatile type *__addr = addr; \
464 \
465 while (count--) { \
466 *__addr = __mem_in##bwlq(port); \
467 __addr++; \
468 } \
469}
wdenk4fc95692003-02-28 00:49:47 +0000470
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100471#define BUILDSTRING(bwlq, type) \
472 \
473__BUILD_MEMORY_STRING(bwlq, type) \
474__BUILD_IOPORT_STRING(bwlq, type)
wdenk4fc95692003-02-28 00:49:47 +0000475
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100476BUILDSTRING(b, u8)
477BUILDSTRING(w, u16)
478BUILDSTRING(l, u32)
479#ifdef CONFIG_64BIT
480BUILDSTRING(q, u64)
481#endif
wdenk4fc95692003-02-28 00:49:47 +0000482
wdenk4fc95692003-02-28 00:49:47 +0000483
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100484#ifdef CONFIG_CPU_CAVIUM_OCTEON
485#define mmiowb() wmb()
486#else
487/* Depends on MIPS II instruction set */
488#define mmiowb() asm volatile ("sync" ::: "memory")
489#endif
wdenk4fc95692003-02-28 00:49:47 +0000490
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100491static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
492{
493 memset((void __force *)addr, val, count);
494}
495static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
496{
497 memcpy(dst, (void __force *)src, count);
498}
499static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
500{
501 memcpy((void __force *)dst, src, count);
502}
wdenk4fc95692003-02-28 00:49:47 +0000503
504/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100505 * Read a 32-bit register that requires a 64-bit read cycle on the bus.
506 * Avoid interrupt mucking, just adjust the address for 4-byte access.
507 * Assume the addresses are 8-byte aligned.
wdenk4fc95692003-02-28 00:49:47 +0000508 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100509#ifdef __MIPSEB__
510#define __CSR_32_ADJUST 4
511#else
512#define __CSR_32_ADJUST 0
513#endif
wdenk4fc95692003-02-28 00:49:47 +0000514
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100515#define csr_out32(v, a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST) = (v))
516#define csr_in32(a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST))
Haiying Wangc123a382007-02-21 16:52:31 +0100517
Haavard Skinnemoenf9855512007-12-13 12:56:33 +0100518/*
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100519 * U-Boot specific
Haavard Skinnemoenf9855512007-12-13 12:56:33 +0100520 */
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100521#define sync() mmiowb()
522
523#define MAP_NOCACHE (1)
Haavard Skinnemoenf9855512007-12-13 12:56:33 +0100524#define MAP_WRCOMBINE (0)
525#define MAP_WRBACK (0)
526#define MAP_WRTHROUGH (0)
527
528static inline void *
529map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
530{
Daniel Schwierzeckb01d3e12016-01-12 21:48:25 +0100531 if (flags == MAP_NOCACHE)
532 return ioremap(paddr, len);
533
Haavard Skinnemoenf9855512007-12-13 12:56:33 +0100534 return (void *)paddr;
535}
536
537/*
538 * Take down a mapping set up by map_physmem().
539 */
540static inline void unmap_physmem(void *vaddr, unsigned long flags)
541{
Haavard Skinnemoenf9855512007-12-13 12:56:33 +0100542}
543
Daniel Schwierzeck650f3f22016-01-15 15:54:48 +0100544#define __BUILD_CLRBITS(bwlq, sfx, end, type) \
545 \
546static inline void clrbits_##sfx(volatile void __iomem *mem, type clr) \
547{ \
548 type __val = __raw_read##bwlq(mem); \
549 __val = end##_to_cpu(__val); \
550 __val &= ~clr; \
551 __val = cpu_to_##end(__val); \
552 __raw_write##bwlq(__val, mem); \
553}
554
555#define __BUILD_SETBITS(bwlq, sfx, end, type) \
556 \
557static inline void setbits_##sfx(volatile void __iomem *mem, type set) \
558{ \
559 type __val = __raw_read##bwlq(mem); \
560 __val = end##_to_cpu(__val); \
561 __val |= set; \
562 __val = cpu_to_##end(__val); \
563 __raw_write##bwlq(__val, mem); \
564}
565
566#define __BUILD_CLRSETBITS(bwlq, sfx, end, type) \
567 \
568static inline void clrsetbits_##sfx(volatile void __iomem *mem, \
569 type clr, type set) \
570{ \
571 type __val = __raw_read##bwlq(mem); \
572 __val = end##_to_cpu(__val); \
573 __val &= ~clr; \
574 __val |= set; \
575 __val = cpu_to_##end(__val); \
576 __raw_write##bwlq(__val, mem); \
577}
578
579#define BUILD_CLRSETBITS(bwlq, sfx, end, type) \
580 \
581__BUILD_CLRBITS(bwlq, sfx, end, type) \
582__BUILD_SETBITS(bwlq, sfx, end, type) \
583__BUILD_CLRSETBITS(bwlq, sfx, end, type)
584
585#define __to_cpu(v) (v)
586#define cpu_to__(v) (v)
587
588BUILD_CLRSETBITS(b, 8, _, u8)
589BUILD_CLRSETBITS(w, le16, le16, u16)
590BUILD_CLRSETBITS(w, be16, be16, u16)
591BUILD_CLRSETBITS(w, 16, _, u16)
592BUILD_CLRSETBITS(l, le32, le32, u32)
593BUILD_CLRSETBITS(l, be32, be32, u32)
594BUILD_CLRSETBITS(l, 32, _, u32)
595BUILD_CLRSETBITS(q, le64, le64, u64)
596BUILD_CLRSETBITS(q, be64, be64, u64)
597BUILD_CLRSETBITS(q, 64, _, u64)
598
wdenk4fc95692003-02-28 00:49:47 +0000599#endif /* _ASM_IO_H */