]>
git.ipfire.org Git - people/ms/u-boot.git/blob - arch/mips/include/asm/io.h
b8ac5a5ac5417b68c481c5019438587e907ca6f2
2 * Copyright (C) 1994, 1995 Waldorf GmbH
3 * Copyright (C) 1994 - 2000, 06 Ralf Baechle
4 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
5 * Copyright (C) 2004, 2005 MIPS Technologies, Inc. All rights reserved.
6 * Author: Maciej W. Rozycki <macro@mips.com>
8 * SPDX-License-Identifier: GPL-2.0
13 #include <linux/compiler.h>
14 #include <linux/types.h>
16 #include <asm/addrspace.h>
17 #include <asm/byteorder.h>
18 #include <asm/cpu-features.h>
19 #include <asm/pgtable-bits.h>
20 #include <asm/processor.h>
21 #include <asm/string.h>
24 #include <mangle-port.h>
28 * Slowdown I/O port space accesses for antique hardware.
30 #undef CONF_SLOWDOWN_IO
33 * 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
38 # 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)
44 /* ioswab[bwlq], __mem_ioswab[bwlq] are defined in mangle-port.h */
46 #define IO_SPACE_LIMIT 0xffff
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.
56 extern const unsigned long mips_io_port_base
;
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.
67 static inline void set_io_port_base(unsigned long base
)
69 * (unsigned long *) &mips_io_port_base
= base
;
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.
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)
85 #define __SLOW_DOWN_IO \
86 __asm__ __volatile__( \
88 : : "r" (mips_io_port_base));
90 #ifdef CONF_SLOWDOWN_IO
92 #define SLOW_DOWN_IO { __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; __SLOW_DOWN_IO; }
94 #define SLOW_DOWN_IO __SLOW_DOWN_IO
101 * virt_to_phys - map virtual addresses to physical
102 * @address: address to remap
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.
108 * This function does not give bus mappings for DMA transfers. In
109 * almost all conceivable cases a device driver should not be using
112 static inline unsigned long virt_to_phys(volatile const void *address
)
114 unsigned long addr
= (unsigned long)address
;
116 /* this corresponds to kernel implementation of __pa() */
119 return XPHYSADDR(addr
);
121 return CPHYSADDR(addr
);
123 return addr
- PAGE_OFFSET
+ PHYS_OFFSET
;
128 * phys_to_virt - map physical address to virtual
129 * @address: address to remap
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
135 * This function does not handle bus mappings for DMA transfers. In
136 * almost all conceivable cases a device driver should not be using
139 static inline void *phys_to_virt(unsigned long address
)
141 return (void *)(address
+ PAGE_OFFSET
- PHYS_OFFSET
);
145 * ISA I/O bus memory addresses are 1:1 with the physical address.
147 static inline unsigned long isa_virt_to_bus(volatile void *address
)
149 return (unsigned long)address
- PAGE_OFFSET
;
152 static inline void *isa_bus_to_virt(unsigned long address
)
154 return (void *)(address
+ PAGE_OFFSET
);
157 #define isa_page_to_bus page_to_phys
160 * However PCI ones are not necessarily 1:1 and therefore these interfaces
161 * are forbidden in portable PCI drivers.
163 * Allow them for x86 for legacy drivers, though.
165 #define virt_to_bus virt_to_phys
166 #define bus_to_virt phys_to_virt
168 static inline void __iomem
*__ioremap_mode(phys_addr_t offset
, unsigned long size
,
172 phys_addr_t phys_addr
;
174 addr
= plat_ioremap(offset
, size
, flags
);
178 phys_addr
= fixup_bigphys_addr(offset
, size
);
179 return (void __iomem
*)(unsigned long)CKSEG1ADDR(phys_addr
);
183 * ioremap - map bus memory into CPU space
184 * @offset: bus address of the memory
185 * @size: size of the resource to map
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
193 #define ioremap(offset, size) \
194 __ioremap_mode((offset), (size), _CACHE_UNCACHED)
197 * ioremap_nocache - map bus memory into CPU space
198 * @offset: bus address of the memory
199 * @size: size of the resource to map
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
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
212 * It's useful if some control registers are in such an area and
213 * write combining or read caching is not desirable:
215 #define ioremap_nocache(offset, size) \
216 __ioremap_mode((offset), (size), _CACHE_UNCACHED)
217 #define ioremap_uc ioremap_nocache
220 * ioremap_cachable - map bus memory into CPU space
221 * @offset: bus address of the memory
222 * @size: size of the resource to map
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
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.
234 #define ioremap_cachable(offset, size) \
235 __ioremap_mode((offset), (size), _page_cachable_default)
238 * 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
243 #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)
248 static inline void iounmap(const volatile void __iomem
*addr
)
253 #ifdef CONFIG_CPU_CAVIUM_OCTEON
254 #define war_octeon_io_reorder_wmb() wmb()
256 #define war_octeon_io_reorder_wmb() do { } while (0)
259 #define __BUILD_MEMORY_SINGLE(pfx, bwlq, type, irq) \
261 static inline void pfx##write##bwlq(type val, \
262 volatile void __iomem *mem) \
264 volatile type *__mem; \
267 war_octeon_io_reorder_wmb(); \
269 __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
271 __val = pfx##ioswab##bwlq(__mem, val); \
273 if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
275 else if (cpu_has_64bits) { \
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" \
287 : "0" (__val), "m" (*__mem)); \
292 static inline type pfx##read##bwlq(const volatile void __iomem *mem) \
294 volatile type *__mem; \
297 __mem = (void *)__swizzle_addr_##bwlq((unsigned long)(mem)); \
299 if (sizeof(type) != sizeof(u64) || sizeof(u64) == sizeof(long)) \
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" \
315 return pfx##ioswab##bwlq(__mem, __val); \
318 #define __BUILD_IOPORT_SINGLE(pfx, bwlq, type, p, slow) \
320 static inline void pfx##out##bwlq##p(type val, unsigned long port) \
322 volatile type *__addr; \
325 war_octeon_io_reorder_wmb(); \
327 __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \
329 __val = pfx##ioswab##bwlq(__addr, val); \
331 /* Really, we want this to be atomic */ \
332 BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
338 static inline type pfx##in##bwlq##p(unsigned long port) \
340 volatile type *__addr; \
343 __addr = (void *)__swizzle_addr_##bwlq(mips_io_port_base + port); \
345 BUILD_BUG_ON(sizeof(type) > sizeof(unsigned long)); \
350 return pfx##ioswab##bwlq(__addr, __val); \
353 #define __BUILD_MEMORY_PFX(bus, bwlq, type) \
355 __BUILD_MEMORY_SINGLE(bus, bwlq, type, 1)
357 #define BUILDIO_MEM(bwlq, type) \
359 __BUILD_MEMORY_PFX(__raw_, bwlq, type) \
360 __BUILD_MEMORY_PFX(, bwlq, type) \
361 __BUILD_MEMORY_PFX(__mem_, bwlq, type) \
368 #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)
372 #define BUILDIO_IOPORT(bwlq, type) \
373 __BUILD_IOPORT_PFX(, bwlq, type) \
374 __BUILD_IOPORT_PFX(__mem_, bwlq, type)
376 BUILDIO_IOPORT(b
, u8
)
377 BUILDIO_IOPORT(w
, u16
)
378 BUILDIO_IOPORT(l
, u32
)
380 BUILDIO_IOPORT(q
, u64
)
383 #define __BUILDIO(bwlq, type) \
385 __BUILD_MEMORY_SINGLE(____raw_, bwlq, type, 0)
389 #define readb_relaxed readb
390 #define readw_relaxed readw
391 #define readl_relaxed readl
392 #define readq_relaxed readq
394 #define writeb_relaxed writeb
395 #define writew_relaxed writew
396 #define writel_relaxed writel
397 #define writeq_relaxed writeq
399 #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)))
408 #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))
418 * Some code tests for these symbols
421 #define writeq writeq
423 #define __BUILD_MEMORY_STRING(bwlq, type) \
425 static inline void writes##bwlq(volatile void __iomem *mem, \
426 const void *addr, unsigned int count) \
428 const volatile type *__addr = addr; \
431 __mem_write##bwlq(*__addr, mem); \
436 static inline void reads##bwlq(volatile void __iomem *mem, void *addr, \
437 unsigned int count) \
439 volatile type *__addr = addr; \
442 *__addr = __mem_read##bwlq(mem); \
447 #define __BUILD_IOPORT_STRING(bwlq, type) \
449 static inline void outs##bwlq(unsigned long port, const void *addr, \
450 unsigned int count) \
452 const volatile type *__addr = addr; \
455 __mem_out##bwlq(*__addr, port); \
460 static inline void ins##bwlq(unsigned long port, void *addr, \
461 unsigned int count) \
463 volatile type *__addr = addr; \
466 *__addr = __mem_in##bwlq(port); \
471 #define BUILDSTRING(bwlq, type) \
473 __BUILD_MEMORY_STRING(bwlq, type) \
474 __BUILD_IOPORT_STRING(bwlq, type)
484 #ifdef CONFIG_CPU_CAVIUM_OCTEON
485 #define mmiowb() wmb()
487 /* Depends on MIPS II instruction set */
488 #define mmiowb() asm volatile ("sync" ::: "memory")
491 static inline void memset_io(volatile void __iomem
*addr
, unsigned char val
, int count
)
493 memset((void __force
*)addr
, val
, count
);
495 static inline void memcpy_fromio(void *dst
, const volatile void __iomem
*src
, int count
)
497 memcpy(dst
, (void __force
*)src
, count
);
499 static inline void memcpy_toio(volatile void __iomem
*dst
, const void *src
, int count
)
501 memcpy((void __force
*)dst
, src
, count
);
505 * 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.
510 #define __CSR_32_ADJUST 4
512 #define __CSR_32_ADJUST 0
515 #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))
521 #define sync() mmiowb()
523 #define MAP_NOCACHE (1)
524 #define MAP_WRCOMBINE (0)
525 #define MAP_WRBACK (0)
526 #define MAP_WRTHROUGH (0)
529 map_physmem(phys_addr_t paddr
, unsigned long len
, unsigned long flags
)
531 if (flags
== MAP_NOCACHE
)
532 return ioremap(paddr
, len
);
534 return (void *)paddr
;
538 * Take down a mapping set up by map_physmem().
540 static inline void unmap_physmem(void *vaddr
, unsigned long flags
)
544 #define __BUILD_CLRBITS(bwlq, sfx, end, type) \
546 static inline void clrbits_##sfx(volatile void __iomem *mem, type clr) \
548 type __val = __raw_read##bwlq(mem); \
549 __val = end##_to_cpu(__val); \
551 __val = cpu_to_##end(__val); \
552 __raw_write##bwlq(__val, mem); \
555 #define __BUILD_SETBITS(bwlq, sfx, end, type) \
557 static inline void setbits_##sfx(volatile void __iomem *mem, type set) \
559 type __val = __raw_read##bwlq(mem); \
560 __val = end##_to_cpu(__val); \
562 __val = cpu_to_##end(__val); \
563 __raw_write##bwlq(__val, mem); \
566 #define __BUILD_CLRSETBITS(bwlq, sfx, end, type) \
568 static inline void clrsetbits_##sfx(volatile void __iomem *mem, \
569 type clr, type set) \
571 type __val = __raw_read##bwlq(mem); \
572 __val = end##_to_cpu(__val); \
575 __val = cpu_to_##end(__val); \
576 __raw_write##bwlq(__val, mem); \
579 #define BUILD_CLRSETBITS(bwlq, sfx, end, type) \
581 __BUILD_CLRBITS(bwlq, sfx, end, type) \
582 __BUILD_SETBITS(bwlq, sfx, end, type) \
583 __BUILD_CLRSETBITS(bwlq, sfx, end, type)
585 #define __to_cpu(v) (v)
586 #define cpu_to__(v) (v)
588 BUILD_CLRSETBITS(b
, 8, _
, u8
)
589 BUILD_CLRSETBITS(w
, le16
, le16
, u16
)
590 BUILD_CLRSETBITS(w
, be16
, be16
, u16
)
591 BUILD_CLRSETBITS(w
, 16, _
, u16
)
592 BUILD_CLRSETBITS(l
, le32
, le32
, u32
)
593 BUILD_CLRSETBITS(l
, be32
, be32
, u32
)
594 BUILD_CLRSETBITS(l
, 32, _
, u32
)
595 BUILD_CLRSETBITS(q
, le64
, le64
, u64
)
596 BUILD_CLRSETBITS(q
, be64
, be64
, u64
)
597 BUILD_CLRSETBITS(q
, 64, _
, u64
)
599 #endif /* _ASM_IO_H */