1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
21162b070SVineet Gupta /*
31162b070SVineet Gupta * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
41162b070SVineet Gupta */
51162b070SVineet Gupta
61162b070SVineet Gupta #ifndef _ASM_ARC_IO_H
71162b070SVineet Gupta #define _ASM_ARC_IO_H
81162b070SVineet Gupta
91162b070SVineet Gupta #include <linux/types.h>
101162b070SVineet Gupta #include <asm/byteorder.h>
111162b070SVineet Gupta #include <asm/page.h>
1210d44343SJose Abreu #include <asm/unaligned.h>
131162b070SVineet Gupta
14e5bc0478SVineet Gupta #ifdef CONFIG_ISA_ARCV2
15e5bc0478SVineet Gupta #include <asm/barrier.h>
16e5bc0478SVineet Gupta #define __iormb() rmb()
17e5bc0478SVineet Gupta #define __iowmb() wmb()
18e5bc0478SVineet Gupta #else
19e5bc0478SVineet Gupta #define __iormb() do { } while (0)
20e5bc0478SVineet Gupta #define __iowmb() do { } while (0)
21e5bc0478SVineet Gupta #endif
22e5bc0478SVineet Gupta
23f5db19e9SVineet Gupta extern void __iomem *ioremap(phys_addr_t paddr, unsigned long size);
24*06dfae39SBaoquan He #define ioremap ioremap
25*06dfae39SBaoquan He #define ioremap_prot ioremap_prot
26*06dfae39SBaoquan He #define iounmap iounmap
ioport_map(unsigned long port,unsigned int nr)27c1678ffcSJoao Pinto static inline void __iomem *ioport_map(unsigned long port, unsigned int nr)
28c1678ffcSJoao Pinto {
29c1678ffcSJoao Pinto return (void __iomem *)port;
30c1678ffcSJoao Pinto }
31c1678ffcSJoao Pinto
ioport_unmap(void __iomem * addr)32c1678ffcSJoao Pinto static inline void ioport_unmap(void __iomem *addr)
33c1678ffcSJoao Pinto {
34c1678ffcSJoao Pinto }
35c1678ffcSJoao Pinto
36e5bc0478SVineet Gupta /*
37e5bc0478SVineet Gupta * io{read,write}{16,32}be() macros
38e5bc0478SVineet Gupta */
39e5bc0478SVineet Gupta #define ioread16be(p) ({ u16 __v = be16_to_cpu((__force __be16)__raw_readw(p)); __iormb(); __v; })
40e5bc0478SVineet Gupta #define ioread32be(p) ({ u32 __v = be32_to_cpu((__force __be32)__raw_readl(p)); __iormb(); __v; })
41e5bc0478SVineet Gupta
42e5bc0478SVineet Gupta #define iowrite16be(v,p) ({ __iowmb(); __raw_writew((__force u16)cpu_to_be16(v), p); })
43e5bc0478SVineet Gupta #define iowrite32be(v,p) ({ __iowmb(); __raw_writel((__force u32)cpu_to_be32(v), p); })
44e5bc0478SVineet Gupta
451162b070SVineet Gupta /* Change struct page to physical address */
461162b070SVineet Gupta #define page_to_phys(page) (page_to_pfn(page) << PAGE_SHIFT)
471162b070SVineet Gupta
481162b070SVineet Gupta #define __raw_readb __raw_readb
__raw_readb(const volatile void __iomem * addr)491162b070SVineet Gupta static inline u8 __raw_readb(const volatile void __iomem *addr)
501162b070SVineet Gupta {
511162b070SVineet Gupta u8 b;
521162b070SVineet Gupta
531162b070SVineet Gupta __asm__ __volatile__(
541162b070SVineet Gupta " ldb%U1 %0, %1 \n"
551162b070SVineet Gupta : "=r" (b)
561162b070SVineet Gupta : "m" (*(volatile u8 __force *)addr)
571162b070SVineet Gupta : "memory");
581162b070SVineet Gupta
591162b070SVineet Gupta return b;
601162b070SVineet Gupta }
611162b070SVineet Gupta
621162b070SVineet Gupta #define __raw_readw __raw_readw
__raw_readw(const volatile void __iomem * addr)631162b070SVineet Gupta static inline u16 __raw_readw(const volatile void __iomem *addr)
641162b070SVineet Gupta {
651162b070SVineet Gupta u16 s;
661162b070SVineet Gupta
671162b070SVineet Gupta __asm__ __volatile__(
681162b070SVineet Gupta " ldw%U1 %0, %1 \n"
691162b070SVineet Gupta : "=r" (s)
701162b070SVineet Gupta : "m" (*(volatile u16 __force *)addr)
711162b070SVineet Gupta : "memory");
721162b070SVineet Gupta
731162b070SVineet Gupta return s;
741162b070SVineet Gupta }
751162b070SVineet Gupta
761162b070SVineet Gupta #define __raw_readl __raw_readl
__raw_readl(const volatile void __iomem * addr)771162b070SVineet Gupta static inline u32 __raw_readl(const volatile void __iomem *addr)
781162b070SVineet Gupta {
791162b070SVineet Gupta u32 w;
801162b070SVineet Gupta
811162b070SVineet Gupta __asm__ __volatile__(
821162b070SVineet Gupta " ld%U1 %0, %1 \n"
831162b070SVineet Gupta : "=r" (w)
841162b070SVineet Gupta : "m" (*(volatile u32 __force *)addr)
851162b070SVineet Gupta : "memory");
861162b070SVineet Gupta
871162b070SVineet Gupta return w;
881162b070SVineet Gupta }
891162b070SVineet Gupta
9010d44343SJose Abreu /*
9110d44343SJose Abreu * {read,write}s{b,w,l}() repeatedly access the same IO address in
9210d44343SJose Abreu * native endianness in 8-, 16-, 32-bit chunks {into,from} memory,
9310d44343SJose Abreu * @count times
9410d44343SJose Abreu */
9510d44343SJose Abreu #define __raw_readsx(t,f) \
9610d44343SJose Abreu static inline void __raw_reads##f(const volatile void __iomem *addr, \
9710d44343SJose Abreu void *ptr, unsigned int count) \
9810d44343SJose Abreu { \
9910d44343SJose Abreu bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \
10010d44343SJose Abreu u##t *buf = ptr; \
10110d44343SJose Abreu \
10210d44343SJose Abreu if (!count) \
10310d44343SJose Abreu return; \
10410d44343SJose Abreu \
10510d44343SJose Abreu /* Some ARC CPU's don't support unaligned accesses */ \
10610d44343SJose Abreu if (is_aligned) { \
10710d44343SJose Abreu do { \
10810d44343SJose Abreu u##t x = __raw_read##f(addr); \
10910d44343SJose Abreu *buf++ = x; \
11010d44343SJose Abreu } while (--count); \
11110d44343SJose Abreu } else { \
11210d44343SJose Abreu do { \
11310d44343SJose Abreu u##t x = __raw_read##f(addr); \
11410d44343SJose Abreu put_unaligned(x, buf++); \
11510d44343SJose Abreu } while (--count); \
11610d44343SJose Abreu } \
11710d44343SJose Abreu }
11810d44343SJose Abreu
11910d44343SJose Abreu #define __raw_readsb __raw_readsb
12010d44343SJose Abreu __raw_readsx(8, b)
12110d44343SJose Abreu #define __raw_readsw __raw_readsw
12210d44343SJose Abreu __raw_readsx(16, w)
12310d44343SJose Abreu #define __raw_readsl __raw_readsl
12410d44343SJose Abreu __raw_readsx(32, l)
12510d44343SJose Abreu
1261162b070SVineet Gupta #define __raw_writeb __raw_writeb
__raw_writeb(u8 b,volatile void __iomem * addr)1271162b070SVineet Gupta static inline void __raw_writeb(u8 b, volatile void __iomem *addr)
1281162b070SVineet Gupta {
1291162b070SVineet Gupta __asm__ __volatile__(
1301162b070SVineet Gupta " stb%U1 %0, %1 \n"
1311162b070SVineet Gupta :
1321162b070SVineet Gupta : "r" (b), "m" (*(volatile u8 __force *)addr)
1331162b070SVineet Gupta : "memory");
1341162b070SVineet Gupta }
1351162b070SVineet Gupta
1361162b070SVineet Gupta #define __raw_writew __raw_writew
__raw_writew(u16 s,volatile void __iomem * addr)1371162b070SVineet Gupta static inline void __raw_writew(u16 s, volatile void __iomem *addr)
1381162b070SVineet Gupta {
1391162b070SVineet Gupta __asm__ __volatile__(
1401162b070SVineet Gupta " stw%U1 %0, %1 \n"
1411162b070SVineet Gupta :
1421162b070SVineet Gupta : "r" (s), "m" (*(volatile u16 __force *)addr)
1431162b070SVineet Gupta : "memory");
1441162b070SVineet Gupta
1451162b070SVineet Gupta }
1461162b070SVineet Gupta
1471162b070SVineet Gupta #define __raw_writel __raw_writel
__raw_writel(u32 w,volatile void __iomem * addr)1481162b070SVineet Gupta static inline void __raw_writel(u32 w, volatile void __iomem *addr)
1491162b070SVineet Gupta {
1501162b070SVineet Gupta __asm__ __volatile__(
1511162b070SVineet Gupta " st%U1 %0, %1 \n"
1521162b070SVineet Gupta :
1531162b070SVineet Gupta : "r" (w), "m" (*(volatile u32 __force *)addr)
1541162b070SVineet Gupta : "memory");
1551162b070SVineet Gupta
1561162b070SVineet Gupta }
1571162b070SVineet Gupta
15810d44343SJose Abreu #define __raw_writesx(t,f) \
15910d44343SJose Abreu static inline void __raw_writes##f(volatile void __iomem *addr, \
16010d44343SJose Abreu const void *ptr, unsigned int count) \
16110d44343SJose Abreu { \
16210d44343SJose Abreu bool is_aligned = ((unsigned long)ptr % ((t) / 8)) == 0; \
16310d44343SJose Abreu const u##t *buf = ptr; \
16410d44343SJose Abreu \
16510d44343SJose Abreu if (!count) \
16610d44343SJose Abreu return; \
16710d44343SJose Abreu \
16810d44343SJose Abreu /* Some ARC CPU's don't support unaligned accesses */ \
16910d44343SJose Abreu if (is_aligned) { \
17010d44343SJose Abreu do { \
17110d44343SJose Abreu __raw_write##f(*buf++, addr); \
17210d44343SJose Abreu } while (--count); \
17310d44343SJose Abreu } else { \
17410d44343SJose Abreu do { \
17510d44343SJose Abreu __raw_write##f(get_unaligned(buf++), addr); \
17610d44343SJose Abreu } while (--count); \
17710d44343SJose Abreu } \
17810d44343SJose Abreu }
17910d44343SJose Abreu
18010d44343SJose Abreu #define __raw_writesb __raw_writesb
18110d44343SJose Abreu __raw_writesx(8, b)
18210d44343SJose Abreu #define __raw_writesw __raw_writesw
18310d44343SJose Abreu __raw_writesx(16, w)
18410d44343SJose Abreu #define __raw_writesl __raw_writesl
18510d44343SJose Abreu __raw_writesx(32, l)
18610d44343SJose Abreu
187b8a03302SVineet Gupta /*
188b8a03302SVineet Gupta * MMIO can also get buffered/optimized in micro-arch, so barriers needed
189b8a03302SVineet Gupta * Based on ARM model for the typical use case
190b8a03302SVineet Gupta *
191b8a03302SVineet Gupta * <ST [DMA buffer]>
192b8a03302SVineet Gupta * <writel MMIO "go" reg>
193b8a03302SVineet Gupta * or:
194b8a03302SVineet Gupta * <readl MMIO "status" reg>
195b8a03302SVineet Gupta * <LD [DMA buffer]>
196b8a03302SVineet Gupta *
197b8a03302SVineet Gupta * http://lkml.kernel.org/r/20150622133656.GG1583@arm.com
198b8a03302SVineet Gupta */
199b8a03302SVineet Gupta #define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; })
200b8a03302SVineet Gupta #define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; })
201b8a03302SVineet Gupta #define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; })
20210d44343SJose Abreu #define readsb(p,d,l) ({ __raw_readsb(p,d,l); __iormb(); })
20310d44343SJose Abreu #define readsw(p,d,l) ({ __raw_readsw(p,d,l); __iormb(); })
20410d44343SJose Abreu #define readsl(p,d,l) ({ __raw_readsl(p,d,l); __iormb(); })
205b8a03302SVineet Gupta
206b8a03302SVineet Gupta #define writeb(v,c) ({ __iowmb(); writeb_relaxed(v,c); })
207b8a03302SVineet Gupta #define writew(v,c) ({ __iowmb(); writew_relaxed(v,c); })
208b8a03302SVineet Gupta #define writel(v,c) ({ __iowmb(); writel_relaxed(v,c); })
20910d44343SJose Abreu #define writesb(p,d,l) ({ __iowmb(); __raw_writesb(p,d,l); })
21010d44343SJose Abreu #define writesw(p,d,l) ({ __iowmb(); __raw_writesw(p,d,l); })
21110d44343SJose Abreu #define writesl(p,d,l) ({ __iowmb(); __raw_writesl(p,d,l); })
212b8a03302SVineet Gupta
213b8a03302SVineet Gupta /*
214f778cc65SLada Trimasova * Relaxed API for drivers which can handle barrier ordering themselves
215f778cc65SLada Trimasova *
216f778cc65SLada Trimasova * Also these are defined to perform little endian accesses.
217f778cc65SLada Trimasova * To provide the typical device register semantics of fixed endian,
218f778cc65SLada Trimasova * swap the byte order for Big Endian
219f778cc65SLada Trimasova *
220f778cc65SLada Trimasova * http://lkml.kernel.org/r/201603100845.30602.arnd@arndb.de
221b8a03302SVineet Gupta */
222b8a03302SVineet Gupta #define readb_relaxed(c) __raw_readb(c)
223f778cc65SLada Trimasova #define readw_relaxed(c) ({ u16 __r = le16_to_cpu((__force __le16) \
224f778cc65SLada Trimasova __raw_readw(c)); __r; })
225f778cc65SLada Trimasova #define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32) \
226f778cc65SLada Trimasova __raw_readl(c)); __r; })
227b8a03302SVineet Gupta
228b8a03302SVineet Gupta #define writeb_relaxed(v,c) __raw_writeb(v,c)
229f778cc65SLada Trimasova #define writew_relaxed(v,c) __raw_writew((__force u16) cpu_to_le16(v),c)
230f778cc65SLada Trimasova #define writel_relaxed(v,c) __raw_writel((__force u32) cpu_to_le32(v),c)
2316532b02fSMischa Jonker
2321162b070SVineet Gupta #include <asm-generic/io.h>
2331162b070SVineet Gupta
2341162b070SVineet Gupta #endif /* _ASM_ARC_IO_H */
235