blob: 70d050590de4d4b544cd69dfb41b9b4975d9cf29 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Alexey Brodkin544c5f52014-02-04 12:56:13 +04002/*
Alexey Brodkin5af2aa82020-01-20 13:37:38 +03003 * Copyright (C) 2013-2014, 2020 Synopsys, Inc. All rights reserved.
Alexey Brodkin544c5f52014-02-04 12:56:13 +04004 */
5
6#ifndef __ASM_ARC_IO_H
7#define __ASM_ARC_IO_H
8
9#include <linux/types.h>
10#include <asm/byteorder.h>
11
Alexey Brodkin0f184182018-02-21 12:58:00 +030012#ifdef __ARCHS__
Alexey Brodkinb686ce72016-06-08 08:24:54 +030013
14/*
15 * ARCv2 based HS38 cores are in-order issue, but still weakly ordered
16 * due to micro-arch buffering/queuing of load/store, cache hit vs. miss ...
17 *
18 * Explicit barrier provided by DMB instruction
19 * - Operand supports fine grained load/store/load+store semantics
20 * - Ensures that selected memory operation issued before it will complete
21 * before any subsequent memory operation of same type
22 * - DMB guarantees SMP as well as local barrier semantics
23 * (asm-generic/barrier.h ensures sane smp_*mb if not defined here, i.e.
24 * UP: barrier(), SMP: smp_*mb == *mb)
25 * - DSYNC provides DMB+completion_of_cache_bpu_maintenance_ops hence not needed
26 * in the general case. Plus it only provides full barrier.
27 */
28
29#define mb() asm volatile("dmb 3\n" : : : "memory")
30#define rmb() asm volatile("dmb 1\n" : : : "memory")
31#define wmb() asm volatile("dmb 2\n" : : : "memory")
32
33#else
34
35/*
36 * ARCompact based cores (ARC700) only have SYNC instruction which is super
37 * heavy weight as it flushes the pipeline as well.
38 * There are no real SMP implementations of such cores.
39 */
40
41#define mb() asm volatile("sync\n" : : : "memory")
42#endif
43
Alexey Brodkin0f184182018-02-21 12:58:00 +030044#ifdef __ARCHS__
Alexey Brodkinb686ce72016-06-08 08:24:54 +030045#define __iormb() rmb()
46#define __iowmb() wmb()
47#else
Alexey Brodkin0f184182018-02-21 12:58:00 +030048#define __iormb() asm volatile("" : : : "memory")
49#define __iowmb() asm volatile("" : : : "memory")
Alexey Brodkinb686ce72016-06-08 08:24:54 +030050#endif
51
Alexey Brodkin544c5f52014-02-04 12:56:13 +040052static inline void sync(void)
53{
54 /* Not yet implemented */
55}
56
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030057#define __arch_getb(a) (*(unsigned char *)(a))
58#define __arch_getw(a) (*(unsigned short *)(a))
59#define __arch_getl(a) (*(unsigned int *)(a))
60#define __arch_getq(a) (*(unsigned long long *)(a))
Alexey Brodkin544c5f52014-02-04 12:56:13 +040061
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030062#define __arch_putb(v, a) (*(unsigned char *)(a) = (v))
63#define __arch_putw(v, a) (*(unsigned short *)(a) = (v))
64#define __arch_putl(v, a) (*(unsigned int *)(a) = (v))
65#define __arch_putq(v, a) (*(unsigned long long *)(a) = (v))
Alexey Brodkin544c5f52014-02-04 12:56:13 +040066
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030067#define __raw_writeb(v, a) __arch_putb(v, a)
68#define __raw_writew(v, a) __arch_putw(v, a)
69#define __raw_writel(v, a) __arch_putl(v, a)
70#define __raw_writeq(v, a) __arch_putq(v, a)
Alexey Brodkin544c5f52014-02-04 12:56:13 +040071
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030072#define __raw_readb(a) __arch_getb(a)
73#define __raw_readw(a) __arch_getw(a)
74#define __raw_readl(a) __arch_getl(a)
75#define __raw_readq(a) __arch_getq(a)
Alexey Brodkin544c5f52014-02-04 12:56:13 +040076
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030077static inline void __raw_writesb(unsigned long addr, const void *data,
78 int bytelen)
Alexey Brodkin544c5f52014-02-04 12:56:13 +040079{
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030080 u8 *buf = (uint8_t *)data;
Alexey Brodkin544c5f52014-02-04 12:56:13 +040081
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030082 while (bytelen--)
83 __arch_putb(*buf++, addr);
Alexey Brodkin544c5f52014-02-04 12:56:13 +040084}
85
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030086static inline void __raw_writesw(unsigned long addr, const void *data,
87 int wordlen)
Alexey Brodkin544c5f52014-02-04 12:56:13 +040088{
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030089 u16 *buf = (uint16_t *)data;
Alexey Brodkin544c5f52014-02-04 12:56:13 +040090
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030091 while (wordlen--)
92 __arch_putw(*buf++, addr);
Alexey Brodkin544c5f52014-02-04 12:56:13 +040093}
94
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030095static inline void __raw_writesl(unsigned long addr, const void *data,
96 int longlen)
Alexey Brodkin544c5f52014-02-04 12:56:13 +040097{
Alexey Brodkin5af2aa82020-01-20 13:37:38 +030098 u32 *buf = (uint32_t *)data;
Alexey Brodkin544c5f52014-02-04 12:56:13 +040099
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300100 while (longlen--)
101 __arch_putl(*buf++, addr);
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400102}
103
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300104static inline void __raw_readsb(unsigned long addr, void *data, int bytelen)
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400105{
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300106 u8 *buf = (uint8_t *)data;
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400107
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300108 while (bytelen--)
109 *buf++ = __arch_getb(addr);
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400110}
111
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300112static inline void __raw_readsw(unsigned long addr, void *data, int wordlen)
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400113{
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300114 u16 *buf = (uint16_t *)data;
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400115
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300116 while (wordlen--)
117 *buf++ = __arch_getw(addr);
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400118}
119
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300120static inline void __raw_readsl(unsigned long addr, void *data, int longlen)
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400121{
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300122 u32 *buf = (uint32_t *)data;
123
124 while (longlen--)
125 *buf++ = __arch_getl(addr);
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400126}
127
Alexey Brodkinb686ce72016-06-08 08:24:54 +0300128/*
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300129 * Relaxed I/O memory access primitives. These follow the Device memory
130 * ordering rules but do not guarantee any ordering relative to Normal memory
131 * accesses.
132 */
133#define readb_relaxed(c) ({ u8 __r = __raw_readb(c); __r; })
134#define readw_relaxed(c) ({ u16 __r = le16_to_cpu((__force __le16) \
135 __raw_readw(c)); __r; })
136#define readl_relaxed(c) ({ u32 __r = le32_to_cpu((__force __le32) \
137 __raw_readl(c)); __r; })
138#define readq_relaxed(c) ({ u64 __r = le64_to_cpu((__force __le64) \
139 __raw_readq(c)); __r; })
140
141#define writeb_relaxed(v, c) ((void)__raw_writeb((v), (c)))
142#define writew_relaxed(v, c) ((void)__raw_writew((__force u16) \
143 cpu_to_le16(v), (c)))
144#define writel_relaxed(v, c) ((void)__raw_writel((__force u32) \
145 cpu_to_le32(v), (c)))
146#define writeq_relaxed(v, c) ((void)__raw_writeq((__force u64) \
147 cpu_to_le64(v), (c)))
148
149/*
Alexey Brodkinb686ce72016-06-08 08:24:54 +0300150 * MMIO can also get buffered/optimized in micro-arch, so barriers needed
151 * Based on ARM model for the typical use case
152 *
153 * <ST [DMA buffer]>
154 * <writel MMIO "go" reg>
155 * or:
156 * <readl MMIO "status" reg>
157 * <LD [DMA buffer]>
158 *
159 * http://lkml.kernel.org/r/20150622133656.GG1583@arm.com
160 */
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300161#define readb(c) ({ u8 __v = readb_relaxed(c); __iormb(); __v; })
162#define readw(c) ({ u16 __v = readw_relaxed(c); __iormb(); __v; })
163#define readl(c) ({ u32 __v = readl_relaxed(c); __iormb(); __v; })
164#define readq(c) ({ u64 __v = readq_relaxed(c); __iormb(); __v; })
Alexey Brodkinb686ce72016-06-08 08:24:54 +0300165
Alexey Brodkin5af2aa82020-01-20 13:37:38 +0300166#define writeb(v, c) ({ __iowmb(); writeb_relaxed(v, c); })
167#define writew(v, c) ({ __iowmb(); writew_relaxed(v, c); })
168#define writel(v, c) ({ __iowmb(); writel_relaxed(v, c); })
169#define writeq(v, c) ({ __iowmb(); writeq_relaxed(v, c); })
Alexey Brodkinb686ce72016-06-08 08:24:54 +0300170
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400171#define out_arch(type, endian, a, v) __raw_write##type(cpu_to_##endian(v), a)
172#define in_arch(type, endian, a) endian##_to_cpu(__raw_read##type(a))
173
174#define out_le32(a, v) out_arch(l, le32, a, v)
175#define out_le16(a, v) out_arch(w, le16, a, v)
176
177#define in_le32(a) in_arch(l, le32, a)
178#define in_le16(a) in_arch(w, le16, a)
179
180#define out_be32(a, v) out_arch(l, be32, a, v)
181#define out_be16(a, v) out_arch(w, be16, a, v)
182
183#define in_be32(a) in_arch(l, be32, a)
184#define in_be16(a) in_arch(w, be16, a)
185
186#define out_8(a, v) __raw_writeb(v, a)
187#define in_8(a) __raw_readb(a)
188
189/*
190 * Clear and set bits in one shot. These macros can be used to clear and
191 * set multiple bits in a register using a single call. These macros can
192 * also be used to set a multiple-bit bit pattern using a mask, by
193 * specifying the mask in the 'clear' parameter and the new bit pattern
194 * in the 'set' parameter.
195 */
196
197#define clrbits(type, addr, clear) \
198 out_##type((addr), in_##type(addr) & ~(clear))
199
200#define setbits(type, addr, set) \
201 out_##type((addr), in_##type(addr) | (set))
202
203#define clrsetbits(type, addr, clear, set) \
204 out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
205
206#define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
207#define setbits_be32(addr, set) setbits(be32, addr, set)
208#define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
209
210#define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
211#define setbits_le32(addr, set) setbits(le32, addr, set)
212#define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
213
214#define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
215#define setbits_be16(addr, set) setbits(be16, addr, set)
216#define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
217
218#define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
219#define setbits_le16(addr, set) setbits(le16, addr, set)
220#define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
221
222#define clrbits_8(addr, clear) clrbits(8, addr, clear)
223#define setbits_8(addr, set) setbits(8, addr, set)
224#define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
225
Paul Burton1fdceda2017-09-14 15:05:02 -0700226#include <asm-generic/io.h>
Alexey Brodkincc2ea6c2016-04-08 09:21:12 -0700227
Alexey Brodkin544c5f52014-02-04 12:56:13 +0400228#endif /* __ASM_ARC_IO_H */