1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 1994, 1995 Waldorf GmbH
3*4882a593Smuzhiyun * Copyright (C) 1994 - 2000, 06 Ralf Baechle
4*4882a593Smuzhiyun * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
5*4882a593Smuzhiyun * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
6*4882a593Smuzhiyun * Author: Maciej W. Rozycki <macro@mips.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun #ifndef _ASM_IO_H
11*4882a593Smuzhiyun #define _ASM_IO_H
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/bug.h>
14*4882a593Smuzhiyun #include <linux/compiler.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun #include <asm/addrspace.h>
18*4882a593Smuzhiyun #include <asm/byteorder.h>
19*4882a593Smuzhiyun #include <asm/cpu-features.h>
20*4882a593Smuzhiyun #include <asm/pgtable-bits.h>
21*4882a593Smuzhiyun #include <asm/processor.h>
22*4882a593Smuzhiyun #include <asm/string.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #include <ioremap.h>
25*4882a593Smuzhiyun #include <mangle-port.h>
26*4882a593Smuzhiyun #include <spaces.h>
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun /*
29*4882a593Smuzhiyun * Raw operations are never swapped in software. OTOH values that raw
30*4882a593Smuzhiyun * operations are working on may or may not have been swapped by the bus
31*4882a593Smuzhiyun * hardware. An example use would be for flash memory that's used for
32*4882a593Smuzhiyun * execute in place.
33*4882a593Smuzhiyun */
34*4882a593Smuzhiyun # define __raw_ioswabb(a, x) (x)
35*4882a593Smuzhiyun # define __raw_ioswabw(a, x) (x)
36*4882a593Smuzhiyun # define __raw_ioswabl(a, x) (x)
37*4882a593Smuzhiyun # define __raw_ioswabq(a, x) (x)
38*4882a593Smuzhiyun # define ____raw_ioswabq(a, x) (x)
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /* ioswab[bwlq], __mem_ioswab[bwlq] are defined in mangle-port.h */
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun #define IO_SPACE_LIMIT 0xffff
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun #ifdef CONFIG_DYNAMIC_IO_PORT_BASE
45*4882a593Smuzhiyun
mips_io_port_base(void)46*4882a593Smuzhiyun static inline ulong mips_io_port_base(void)
47*4882a593Smuzhiyun {
48*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun return gd->arch.io_port_base;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
set_io_port_base(unsigned long base)53*4882a593Smuzhiyun static inline void set_io_port_base(unsigned long base)
54*4882a593Smuzhiyun {
55*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun gd->arch.io_port_base = base;
58*4882a593Smuzhiyun barrier();
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun #else /* !CONFIG_DYNAMIC_IO_PORT_BASE */
62*4882a593Smuzhiyun
mips_io_port_base(void)63*4882a593Smuzhiyun static inline ulong mips_io_port_base(void)
64*4882a593Smuzhiyun {
65*4882a593Smuzhiyun return 0;
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun
set_io_port_base(unsigned long base)68*4882a593Smuzhiyun static inline void set_io_port_base(unsigned long base)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun BUG_ON(base);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun #endif /* !CONFIG_DYNAMIC_IO_PORT_BASE */
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /*
76*4882a593Smuzhiyun * virt_to_phys - map virtual addresses to physical
77*4882a593Smuzhiyun * @address: address to remap
78*4882a593Smuzhiyun *
79*4882a593Smuzhiyun * The returned physical address is the physical (CPU) mapping for
80*4882a593Smuzhiyun * the memory address given. It is only valid to use this function on
81*4882a593Smuzhiyun * addresses directly mapped or allocated via kmalloc.
82*4882a593Smuzhiyun *
83*4882a593Smuzhiyun * This function does not give bus mappings for DMA transfers. In
84*4882a593Smuzhiyun * almost all conceivable cases a device driver should not be using
85*4882a593Smuzhiyun * this function
86*4882a593Smuzhiyun */
virt_to_phys(volatile const void * address)87*4882a593Smuzhiyun static inline unsigned long virt_to_phys(volatile const void *address)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun unsigned long addr = (unsigned long)address;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* this corresponds to kernel implementation of __pa() */
92*4882a593Smuzhiyun #ifdef CONFIG_64BIT
93*4882a593Smuzhiyun if (addr < CKSEG0)
94*4882a593Smuzhiyun return XPHYSADDR(addr);
95*4882a593Smuzhiyun #endif
96*4882a593Smuzhiyun return CPHYSADDR(addr);
97*4882a593Smuzhiyun }
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /*
100*4882a593Smuzhiyun * phys_to_virt - map physical address to virtual
101*4882a593Smuzhiyun * @address: address to remap
102*4882a593Smuzhiyun *
103*4882a593Smuzhiyun * The returned virtual address is a current CPU mapping for
104*4882a593Smuzhiyun * the memory address given. It is only valid to use this function on
105*4882a593Smuzhiyun * addresses that have a kernel mapping
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * This function does not handle bus mappings for DMA transfers. In
108*4882a593Smuzhiyun * almost all conceivable cases a device driver should not be using
109*4882a593Smuzhiyun * this function
110*4882a593Smuzhiyun */
phys_to_virt(unsigned long address)111*4882a593Smuzhiyun static inline void *phys_to_virt(unsigned long address)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun return (void *)(address + PAGE_OFFSET - PHYS_OFFSET);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * ISA I/O bus memory addresses are 1:1 with the physical address.
118*4882a593Smuzhiyun */
isa_virt_to_bus(volatile void * address)119*4882a593Smuzhiyun static inline unsigned long isa_virt_to_bus(volatile void *address)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun return (unsigned long)address - PAGE_OFFSET;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
isa_bus_to_virt(unsigned long address)124*4882a593Smuzhiyun static inline void *isa_bus_to_virt(unsigned long address)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun return (void *)(address + PAGE_OFFSET);
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun #define isa_page_to_bus page_to_phys
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun /*
132*4882a593Smuzhiyun * However PCI ones are not necessarily 1:1 and therefore these interfaces
133*4882a593Smuzhiyun * are forbidden in portable PCI drivers.
134*4882a593Smuzhiyun *
135*4882a593Smuzhiyun * Allow them for x86 for legacy drivers, though.
136*4882a593Smuzhiyun */
137*4882a593Smuzhiyun #define virt_to_bus virt_to_phys
138*4882a593Smuzhiyun #define bus_to_virt phys_to_virt
139*4882a593Smuzhiyun
__ioremap_mode(phys_addr_t offset,unsigned long size,unsigned long flags)140*4882a593Smuzhiyun static inline void __iomem *__ioremap_mode(phys_addr_t offset, unsigned long size,
141*4882a593Smuzhiyun unsigned long flags)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun void __iomem *addr;
144*4882a593Smuzhiyun phys_addr_t phys_addr;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun addr = plat_ioremap(offset, size, flags);
147*4882a593Smuzhiyun if (addr)
148*4882a593Smuzhiyun return addr;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun phys_addr = fixup_bigphys_addr(offset, size);
151*4882a593Smuzhiyun return (void __iomem *)(unsigned long)CKSEG1ADDR(phys_addr);
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /*
155*4882a593Smuzhiyun * ioremap - map bus memory into CPU space
156*4882a593Smuzhiyun * @offset: bus address of the memory
157*4882a593Smuzhiyun * @size: size of the resource to map
158*4882a593Smuzhiyun *
159*4882a593Smuzhiyun * ioremap performs a platform specific sequence of operations to
160*4882a593Smuzhiyun * make bus memory CPU accessible via the readb/readw/readl/writeb/
161*4882a593Smuzhiyun * writew/writel functions and the other mmio helpers. The returned
162*4882a593Smuzhiyun * address is not guaranteed to be usable directly as a virtual
163*4882a593Smuzhiyun * address.
164*4882a593Smuzhiyun */
165*4882a593Smuzhiyun #define ioremap(offset, size) \
166*4882a593Smuzhiyun __ioremap_mode((offset), (size), _CACHE_UNCACHED)
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /*
169*4882a593Smuzhiyun * ioremap_nocache - map bus memory into CPU space
170*4882a593Smuzhiyun * @offset: bus address of the memory
171*4882a593Smuzhiyun * @size: size of the resource to map
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * ioremap_nocache performs a platform specific sequence of operations to
174*4882a593Smuzhiyun * make bus memory CPU accessible via the readb/readw/readl/writeb/
175*4882a593Smuzhiyun * writew/writel functions and the other mmio helpers. The returned
176*4882a593Smuzhiyun * address is not guaranteed to be usable directly as a virtual
177*4882a593Smuzhiyun * address.
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * This version of ioremap ensures that the memory is marked uncachable
180*4882a593Smuzhiyun * on the CPU as well as honouring existing caching rules from things like
181*4882a593Smuzhiyun * the PCI bus. Note that there are other caches and buffers on many
182*4882a593Smuzhiyun * busses. In particular driver authors should read up on PCI writes
183*4882a593Smuzhiyun *
184*4882a593Smuzhiyun * It's useful if some control registers are in such an area and
185*4882a593Smuzhiyun * write combining or read caching is not desirable:
186*4882a593Smuzhiyun */
187*4882a593Smuzhiyun #define ioremap_nocache(offset, size) \
188*4882a593Smuzhiyun __ioremap_mode((offset), (size), _CACHE_UNCACHED)
189*4882a593Smuzhiyun #define ioremap_uc ioremap_nocache
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun * ioremap_cachable - map bus memory into CPU space
193*4882a593Smuzhiyun * @offset: bus address of the memory
194*4882a593Smuzhiyun * @size: size of the resource to map
195*4882a593Smuzhiyun *
196*4882a593Smuzhiyun * ioremap_nocache performs a platform specific sequence of operations to
197*4882a593Smuzhiyun * make bus memory CPU accessible via the readb/readw/readl/writeb/
198*4882a593Smuzhiyun * writew/writel functions and the other mmio helpers. The returned
199*4882a593Smuzhiyun * address is not guaranteed to be usable directly as a virtual
200*4882a593Smuzhiyun * address.
201*4882a593Smuzhiyun *
202*4882a593Smuzhiyun * This version of ioremap ensures that the memory is marked cachable by
203*4882a593Smuzhiyun * the CPU. Also enables full write-combining. Useful for some
204*4882a593Smuzhiyun * memory-like regions on I/O busses.
205*4882a593Smuzhiyun */
206*4882a593Smuzhiyun #define ioremap_cachable(offset, size) \
207*4882a593Smuzhiyun __ioremap_mode((offset), (size), _page_cachable_default)
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /*
210*4882a593Smuzhiyun * These two are MIPS specific ioremap variant. ioremap_cacheable_cow
211*4882a593Smuzhiyun * requests a cachable mapping, ioremap_uncached_accelerated requests a
212*4882a593Smuzhiyun * mapping using the uncached accelerated mode which isn't supported on
213*4882a593Smuzhiyun * all processors.
214*4882a593Smuzhiyun */
215*4882a593Smuzhiyun #define ioremap_cacheable_cow(offset, size) \
216*4882a593Smuzhiyun __ioremap_mode((offset), (size), _CACHE_CACHABLE_COW)
217*4882a593Smuzhiyun #define ioremap_uncached_accelerated(offset, size) \
218*4882a593Smuzhiyun __ioremap_mode((offset), (size), _CACHE_UNCACHED_ACCELERATED)
219*4882a593Smuzhiyun
iounmap(const volatile void __iomem * addr)220*4882a593Smuzhiyun static inline void iounmap(const volatile void __iomem *addr)
221*4882a593Smuzhiyun {
222*4882a593Smuzhiyun plat_iounmap(addr);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun #ifdef CONFIG_CPU_CAVIUM_OCTEON
226*4882a593Smuzhiyun #define war_octeon_io_reorder_wmb() wmb()
227*4882a593Smuzhiyun #else
228*4882a593Smuzhiyun #define war_octeon_io_reorder_wmb() do { } while (0)
229*4882a593Smuzhiyun #endif
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun #define __BUILD_MEMORY_SINGLE(pfx, bwlq, type, irq) \
232*4882a593Smuzhiyun \
233*4882a593Smuzhiyun static inline void pfx##write##bwlq(type val, \
234*4882a593Smuzhiyun volatile void __iomem *mem) \
235*4882a593Smuzhiyun { \
236*4882a593Smuzhiyun volatile type *__mem; \
237*4882a593Smuzhiyun type __val; \
238*4882a593Smuzhiyun \
239*4882a593Smuzhiyun war_octeon_io_reorder_wmb(); \
240*4882a593Smuzhiyun \
241*4882a593Smuzhiyun __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
242*4882a593Smuzhiyun \
243*4882a593Smuzhiyun __val = pfx##ioswab##bwlq(__mem, val); \
244*4882a593Smuzhiyun \
245*4882a593Smuzhiyun if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
246*4882a593Smuzhiyun *__mem = __val; \
247*4882a593Smuzhiyun else if (cpu_has_64bits) { \
248*4882a593Smuzhiyun type __tmp; \
249*4882a593Smuzhiyun \
250*4882a593Smuzhiyun __asm__ __volatile__( \
251*4882a593Smuzhiyun ".set arch=r4000" "\t\t# __writeq""\n\t" \
252*4882a593Smuzhiyun "dsll32 %L0, %L0, 0" "\n\t" \
253*4882a593Smuzhiyun "dsrl32 %L0, %L0, 0" "\n\t" \
254*4882a593Smuzhiyun "dsll32 %M0, %M0, 0" "\n\t" \
255*4882a593Smuzhiyun "or %L0, %L0, %M0" "\n\t" \
256*4882a593Smuzhiyun "sd %L0, %2" "\n\t" \
257*4882a593Smuzhiyun ".set mips0" "\n" \
258*4882a593Smuzhiyun : "=r" (__tmp) \
259*4882a593Smuzhiyun : "0" (__val), "m" (*__mem)); \
260*4882a593Smuzhiyun } else \
261*4882a593Smuzhiyun BUG(); \
262*4882a593Smuzhiyun } \
263*4882a593Smuzhiyun \
264*4882a593Smuzhiyun static inline type pfx##read##bwlq(const volatile void __iomem *mem) \
265*4882a593Smuzhiyun { \
266*4882a593Smuzhiyun volatile type *__mem; \
267*4882a593Smuzhiyun type __val; \
268*4882a593Smuzhiyun \
269*4882a593Smuzhiyun __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
270*4882a593Smuzhiyun \
271*4882a593Smuzhiyun if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
272*4882a593Smuzhiyun __val = *__mem; \
273*4882a593Smuzhiyun else if (cpu_has_64bits) { \
274*4882a593Smuzhiyun __asm__ __volatile__( \
275*4882a593Smuzhiyun ".set arch=r4000" "\t\t# __readq" "\n\t" \
276*4882a593Smuzhiyun "ld %L0, %1" "\n\t" \
277*4882a593Smuzhiyun "dsra32 %M0, %L0, 0" "\n\t" \
278*4882a593Smuzhiyun "sll %L0, %L0, 0" "\n\t" \
279*4882a593Smuzhiyun ".set mips0" "\n" \
280*4882a593Smuzhiyun : "=r" (__val) \
281*4882a593Smuzhiyun : "m" (*__mem)); \
282*4882a593Smuzhiyun } else { \
283*4882a593Smuzhiyun __val = 0; \
284*4882a593Smuzhiyun BUG(); \
285*4882a593Smuzhiyun } \
286*4882a593Smuzhiyun \
287*4882a593Smuzhiyun return pfx##ioswab##bwlq(__mem, __val); \
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun #define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p) \
291*4882a593Smuzhiyun \
292*4882a593Smuzhiyun static inline void pfx##out##bwlq##p(type val, unsigned long port) \
293*4882a593Smuzhiyun { \
294*4882a593Smuzhiyun volatile type *__addr; \
295*4882a593Smuzhiyun type __val; \
296*4882a593Smuzhiyun \
297*4882a593Smuzhiyun war_octeon_io_reorder_wmb(); \
298*4882a593Smuzhiyun \
299*4882a593Smuzhiyun __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base() + port); \
300*4882a593Smuzhiyun \
301*4882a593Smuzhiyun __val = pfx##ioswab##bwlq(__addr, val); \
302*4882a593Smuzhiyun \
303*4882a593Smuzhiyun /* Really, we want this to be atomic */ \
304*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
305*4882a593Smuzhiyun \
306*4882a593Smuzhiyun *__addr = __val; \
307*4882a593Smuzhiyun } \
308*4882a593Smuzhiyun \
309*4882a593Smuzhiyun static inline type pfx##in##bwlq##p(unsigned long port) \
310*4882a593Smuzhiyun { \
311*4882a593Smuzhiyun volatile type *__addr; \
312*4882a593Smuzhiyun type __val; \
313*4882a593Smuzhiyun \
314*4882a593Smuzhiyun __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base() + port); \
315*4882a593Smuzhiyun \
316*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
317*4882a593Smuzhiyun \
318*4882a593Smuzhiyun __val = *__addr; \
319*4882a593Smuzhiyun \
320*4882a593Smuzhiyun return pfx##ioswab##bwlq(__addr, __val); \
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun
323*4882a593Smuzhiyun #define __BUILD_MEMORY_PFX(bus, bwlq, type) \
324*4882a593Smuzhiyun \
325*4882a593Smuzhiyun __BUILD_MEMORY_SINGLE(bus, bwlq, type, 1)
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun #define BUILDIO_MEM(bwlq, type) \
328*4882a593Smuzhiyun \
329*4882a593Smuzhiyun __BUILD_MEMORY_PFX(__raw_, bwlq, type) \
330*4882a593Smuzhiyun __BUILD_MEMORY_PFX(, bwlq, type) \
331*4882a593Smuzhiyun __BUILD_MEMORY_PFX(__mem_, bwlq, type) \
332*4882a593Smuzhiyun
BUILDIO_MEM(b,u8)333*4882a593Smuzhiyun BUILDIO_MEM(b, u8)
334*4882a593Smuzhiyun BUILDIO_MEM(w, u16)
335*4882a593Smuzhiyun BUILDIO_MEM(l, u32)
336*4882a593Smuzhiyun BUILDIO_MEM(q, u64)
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun #define __BUILD_IOPORT_PFX(bus, bwlq, type) \
339*4882a593Smuzhiyun __BUILD_IOPORT_SINGLE(bus, bwlq, type, ) \
340*4882a593Smuzhiyun __BUILD_IOPORT_SINGLE(bus, bwlq, type, _p)
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun #define BUILDIO_IOPORT(bwlq, type) \
343*4882a593Smuzhiyun __BUILD_IOPORT_PFX(, bwlq, type) \
344*4882a593Smuzhiyun __BUILD_IOPORT_PFX(__mem_, bwlq, type)
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun BUILDIO_IOPORT(b, u8)
347*4882a593Smuzhiyun BUILDIO_IOPORT(w, u16)
348*4882a593Smuzhiyun BUILDIO_IOPORT(l, u32)
349*4882a593Smuzhiyun #ifdef CONFIG_64BIT
350*4882a593Smuzhiyun BUILDIO_IOPORT(q, u64)
351*4882a593Smuzhiyun #endif
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun #define __BUILDIO(bwlq, type) \
354*4882a593Smuzhiyun \
355*4882a593Smuzhiyun __BUILD_MEMORY_SINGLE(____raw_, bwlq, type, 0)
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun __BUILDIO(q, u64)
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun #define readb_relaxed readb
360*4882a593Smuzhiyun #define readw_relaxed readw
361*4882a593Smuzhiyun #define readl_relaxed readl
362*4882a593Smuzhiyun #define readq_relaxed readq
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun #define writeb_relaxed writeb
365*4882a593Smuzhiyun #define writew_relaxed writew
366*4882a593Smuzhiyun #define writel_relaxed writel
367*4882a593Smuzhiyun #define writeq_relaxed writeq
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun #define readb_be(addr) \
370*4882a593Smuzhiyun __raw_readb((__force unsigned *)(addr))
371*4882a593Smuzhiyun #define readw_be(addr) \
372*4882a593Smuzhiyun be16_to_cpu(__raw_readw((__force unsigned *)(addr)))
373*4882a593Smuzhiyun #define readl_be(addr) \
374*4882a593Smuzhiyun be32_to_cpu(__raw_readl((__force unsigned *)(addr)))
375*4882a593Smuzhiyun #define readq_be(addr) \
376*4882a593Smuzhiyun be64_to_cpu(__raw_readq((__force unsigned *)(addr)))
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun #define writeb_be(val, addr) \
379*4882a593Smuzhiyun __raw_writeb((val), (__force unsigned *)(addr))
380*4882a593Smuzhiyun #define writew_be(val, addr) \
381*4882a593Smuzhiyun __raw_writew(cpu_to_be16((val)), (__force unsigned *)(addr))
382*4882a593Smuzhiyun #define writel_be(val, addr) \
383*4882a593Smuzhiyun __raw_writel(cpu_to_be32((val)), (__force unsigned *)(addr))
384*4882a593Smuzhiyun #define writeq_be(val, addr) \
385*4882a593Smuzhiyun __raw_writeq(cpu_to_be64((val)), (__force unsigned *)(addr))
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun /*
388*4882a593Smuzhiyun * Some code tests for these symbols
389*4882a593Smuzhiyun */
390*4882a593Smuzhiyun #define readq readq
391*4882a593Smuzhiyun #define writeq writeq
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun #define __BUILD_MEMORY_STRING(bwlq, type) \
394*4882a593Smuzhiyun \
395*4882a593Smuzhiyun static inline void writes##bwlq(volatile void __iomem *mem, \
396*4882a593Smuzhiyun const void *addr, unsigned int count) \
397*4882a593Smuzhiyun { \
398*4882a593Smuzhiyun const volatile type *__addr = addr; \
399*4882a593Smuzhiyun \
400*4882a593Smuzhiyun while (count--) { \
401*4882a593Smuzhiyun __mem_write##bwlq(*__addr, mem); \
402*4882a593Smuzhiyun __addr++; \
403*4882a593Smuzhiyun } \
404*4882a593Smuzhiyun } \
405*4882a593Smuzhiyun \
406*4882a593Smuzhiyun static inline void reads##bwlq(volatile void __iomem *mem, void *addr, \
407*4882a593Smuzhiyun unsigned int count) \
408*4882a593Smuzhiyun { \
409*4882a593Smuzhiyun volatile type *__addr = addr; \
410*4882a593Smuzhiyun \
411*4882a593Smuzhiyun while (count--) { \
412*4882a593Smuzhiyun *__addr = __mem_read##bwlq(mem); \
413*4882a593Smuzhiyun __addr++; \
414*4882a593Smuzhiyun } \
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun #define __BUILD_IOPORT_STRING(bwlq, type) \
418*4882a593Smuzhiyun \
419*4882a593Smuzhiyun static inline void outs##bwlq(unsigned long port, const void *addr, \
420*4882a593Smuzhiyun unsigned int count) \
421*4882a593Smuzhiyun { \
422*4882a593Smuzhiyun const volatile type *__addr = addr; \
423*4882a593Smuzhiyun \
424*4882a593Smuzhiyun while (count--) { \
425*4882a593Smuzhiyun __mem_out##bwlq(*__addr, port); \
426*4882a593Smuzhiyun __addr++; \
427*4882a593Smuzhiyun } \
428*4882a593Smuzhiyun } \
429*4882a593Smuzhiyun \
430*4882a593Smuzhiyun static inline void ins##bwlq(unsigned long port, void *addr, \
431*4882a593Smuzhiyun unsigned int count) \
432*4882a593Smuzhiyun { \
433*4882a593Smuzhiyun volatile type *__addr = addr; \
434*4882a593Smuzhiyun \
435*4882a593Smuzhiyun while (count--) { \
436*4882a593Smuzhiyun *__addr = __mem_in##bwlq(port); \
437*4882a593Smuzhiyun __addr++; \
438*4882a593Smuzhiyun } \
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun #define BUILDSTRING(bwlq, type) \
442*4882a593Smuzhiyun \
443*4882a593Smuzhiyun __BUILD_MEMORY_STRING(bwlq, type) \
444*4882a593Smuzhiyun __BUILD_IOPORT_STRING(bwlq, type)
445*4882a593Smuzhiyun
446*4882a593Smuzhiyun BUILDSTRING(b, u8)
447*4882a593Smuzhiyun BUILDSTRING(w, u16)
448*4882a593Smuzhiyun BUILDSTRING(l, u32)
449*4882a593Smuzhiyun #ifdef CONFIG_64BIT
450*4882a593Smuzhiyun BUILDSTRING(q, u64)
451*4882a593Smuzhiyun #endif
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun
454*4882a593Smuzhiyun #ifdef CONFIG_CPU_CAVIUM_OCTEON
455*4882a593Smuzhiyun #define mmiowb() wmb()
456*4882a593Smuzhiyun #else
457*4882a593Smuzhiyun /* Depends on MIPS II instruction set */
458*4882a593Smuzhiyun #define mmiowb() asm volatile ("sync" ::: "memory")
459*4882a593Smuzhiyun #endif
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun static inline void memset_io(volatile void __iomem *addr, unsigned char val, int count)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun memset((void __force *)addr, val, count);
464*4882a593Smuzhiyun }
memcpy_fromio(void * dst,const volatile void __iomem * src,int count)465*4882a593Smuzhiyun static inline void memcpy_fromio(void *dst, const volatile void __iomem *src, int count)
466*4882a593Smuzhiyun {
467*4882a593Smuzhiyun memcpy(dst, (void __force *)src, count);
468*4882a593Smuzhiyun }
memcpy_toio(volatile void __iomem * dst,const void * src,int count)469*4882a593Smuzhiyun static inline void memcpy_toio(volatile void __iomem *dst, const void *src, int count)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun memcpy((void __force *)dst, src, count);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun /*
475*4882a593Smuzhiyun * Read a 32-bit register that requires a 64-bit read cycle on the bus.
476*4882a593Smuzhiyun * Avoid interrupt mucking, just adjust the address for 4-byte access.
477*4882a593Smuzhiyun * Assume the addresses are 8-byte aligned.
478*4882a593Smuzhiyun */
479*4882a593Smuzhiyun #ifdef __MIPSEB__
480*4882a593Smuzhiyun #define __CSR_32_ADJUST 4
481*4882a593Smuzhiyun #else
482*4882a593Smuzhiyun #define __CSR_32_ADJUST 0
483*4882a593Smuzhiyun #endif
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun #define csr_out32(v, a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST) = (v))
486*4882a593Smuzhiyun #define csr_in32(a) (*(volatile u32 *)((unsigned long)(a) + __CSR_32_ADJUST))
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun /*
489*4882a593Smuzhiyun * U-Boot specific
490*4882a593Smuzhiyun */
491*4882a593Smuzhiyun #define sync() mmiowb()
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun #define MAP_NOCACHE (1)
494*4882a593Smuzhiyun #define MAP_WRCOMBINE (0)
495*4882a593Smuzhiyun #define MAP_WRBACK (0)
496*4882a593Smuzhiyun #define MAP_WRTHROUGH (0)
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun static inline void *
map_physmem(phys_addr_t paddr,unsigned long len,unsigned long flags)499*4882a593Smuzhiyun map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun if (flags == MAP_NOCACHE)
502*4882a593Smuzhiyun return ioremap(paddr, len);
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun return (void *)CKSEG0ADDR(paddr);
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun /*
508*4882a593Smuzhiyun * Take down a mapping set up by map_physmem().
509*4882a593Smuzhiyun */
unmap_physmem(void * vaddr,unsigned long flags)510*4882a593Smuzhiyun static inline void unmap_physmem(void *vaddr, unsigned long flags)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun
514*4882a593Smuzhiyun #define __BUILD_CLRBITS(bwlq, sfx, end, type) \
515*4882a593Smuzhiyun \
516*4882a593Smuzhiyun static inline void clrbits_##sfx(volatile void __iomem *mem, type clr) \
517*4882a593Smuzhiyun { \
518*4882a593Smuzhiyun type __val = __raw_read##bwlq(mem); \
519*4882a593Smuzhiyun __val = end##_to_cpu(__val); \
520*4882a593Smuzhiyun __val &= ~clr; \
521*4882a593Smuzhiyun __val = cpu_to_##end(__val); \
522*4882a593Smuzhiyun __raw_write##bwlq(__val, mem); \
523*4882a593Smuzhiyun }
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun #define __BUILD_SETBITS(bwlq, sfx, end, type) \
526*4882a593Smuzhiyun \
527*4882a593Smuzhiyun static inline void setbits_##sfx(volatile void __iomem *mem, type set) \
528*4882a593Smuzhiyun { \
529*4882a593Smuzhiyun type __val = __raw_read##bwlq(mem); \
530*4882a593Smuzhiyun __val = end##_to_cpu(__val); \
531*4882a593Smuzhiyun __val |= set; \
532*4882a593Smuzhiyun __val = cpu_to_##end(__val); \
533*4882a593Smuzhiyun __raw_write##bwlq(__val, mem); \
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun #define __BUILD_CLRSETBITS(bwlq, sfx, end, type) \
537*4882a593Smuzhiyun \
538*4882a593Smuzhiyun static inline void clrsetbits_##sfx(volatile void __iomem *mem, \
539*4882a593Smuzhiyun type clr, type set) \
540*4882a593Smuzhiyun { \
541*4882a593Smuzhiyun type __val = __raw_read##bwlq(mem); \
542*4882a593Smuzhiyun __val = end##_to_cpu(__val); \
543*4882a593Smuzhiyun __val &= ~clr; \
544*4882a593Smuzhiyun __val |= set; \
545*4882a593Smuzhiyun __val = cpu_to_##end(__val); \
546*4882a593Smuzhiyun __raw_write##bwlq(__val, mem); \
547*4882a593Smuzhiyun }
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun #define BUILD_CLRSETBITS(bwlq, sfx, end, type) \
550*4882a593Smuzhiyun \
551*4882a593Smuzhiyun __BUILD_CLRBITS(bwlq, sfx, end, type) \
552*4882a593Smuzhiyun __BUILD_SETBITS(bwlq, sfx, end, type) \
553*4882a593Smuzhiyun __BUILD_CLRSETBITS(bwlq, sfx, end, type)
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun #define __to_cpu(v) (v)
556*4882a593Smuzhiyun #define cpu_to__(v) (v)
557*4882a593Smuzhiyun
558*4882a593Smuzhiyun BUILD_CLRSETBITS(b, 8, _, u8)
559*4882a593Smuzhiyun BUILD_CLRSETBITS(w, le16, le16, u16)
560*4882a593Smuzhiyun BUILD_CLRSETBITS(w, be16, be16, u16)
561*4882a593Smuzhiyun BUILD_CLRSETBITS(w, 16, _, u16)
562*4882a593Smuzhiyun BUILD_CLRSETBITS(l, le32, le32, u32)
563*4882a593Smuzhiyun BUILD_CLRSETBITS(l, be32, be32, u32)
564*4882a593Smuzhiyun BUILD_CLRSETBITS(l, 32, _, u32)
565*4882a593Smuzhiyun BUILD_CLRSETBITS(q, le64, le64, u64)
566*4882a593Smuzhiyun BUILD_CLRSETBITS(q, be64, be64, u64)
567*4882a593Smuzhiyun BUILD_CLRSETBITS(q, 64, _, u64)
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun #endif /* _ASM_IO_H */
570