blob: 77a02e5f5241fb8f5f7fe5328fbf9f49a3f6c2e6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassb4a905e2011-10-10 08:22:14 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glassb4a905e2011-10-10 08:22:14 +00004 */
5
Simon Glass62cf9122013-04-26 02:53:43 +00006#ifndef __SANDBOX_ASM_IO_H
7#define __SANDBOX_ASM_IO_H
8
Simon Glass0a341a72019-09-25 08:56:09 -06009enum sandboxio_size_t {
10 SB_SIZE_8,
11 SB_SIZE_16,
12 SB_SIZE_32,
13 SB_SIZE_64,
14};
15
Paul Burton51369252017-09-14 15:05:13 -070016void *phys_to_virt(phys_addr_t paddr);
17#define phys_to_virt phys_to_virt
18
19phys_addr_t virt_to_phys(void *vaddr);
20#define virt_to_phys virt_to_phys
Simon Glassb4a905e2011-10-10 08:22:14 +000021
22void *map_physmem(phys_addr_t paddr, unsigned long len, unsigned long flags);
Paul Burton51369252017-09-14 15:05:13 -070023#define map_physmem map_physmem
Simon Glassb4a905e2011-10-10 08:22:14 +000024
25/*
26 * Take down a mapping set up by map_physmem().
27 */
Simon Glass70778bc2015-03-05 12:25:26 -070028void unmap_physmem(const void *vaddr, unsigned long flags);
Paul Burton51369252017-09-14 15:05:13 -070029#define unmap_physmem unmap_physmem
30
Simon Glasse23d2932013-04-20 08:42:37 +000031/* Map from a pointer to our RAM buffer */
Simon Glass656bfc42013-11-07 09:31:58 -070032phys_addr_t map_to_sysmem(const void *ptr);
Simon Glass62cf9122013-04-26 02:53:43 +000033
Abdellatif El Khlifi32f72952023-04-17 10:11:54 +010034unsigned long sandbox_read(const void *addr, enum sandboxio_size_t size);
Simon Glassd80cda22019-10-11 16:16:47 -060035void sandbox_write(void *addr, unsigned int val, enum sandboxio_size_t size);
Simon Glass0a341a72019-09-25 08:56:09 -060036
37#define readb(addr) sandbox_read((const void *)addr, SB_SIZE_8)
38#define readw(addr) sandbox_read((const void *)addr, SB_SIZE_16)
39#define readl(addr) sandbox_read((const void *)addr, SB_SIZE_32)
Mario Six61efece2018-02-12 08:05:57 +010040#ifdef CONFIG_SANDBOX64
Simon Glass0a341a72019-09-25 08:56:09 -060041#define readq(addr) sandbox_read((const void *)addr, SB_SIZE_64)
Mario Six61efece2018-02-12 08:05:57 +010042#endif
Simon Glassd80cda22019-10-11 16:16:47 -060043#define writeb(v, addr) sandbox_write((void *)addr, v, SB_SIZE_8)
44#define writew(v, addr) sandbox_write((void *)addr, v, SB_SIZE_16)
45#define writel(v, addr) sandbox_write((void *)addr, v, SB_SIZE_32)
Mario Six61efece2018-02-12 08:05:57 +010046#ifdef CONFIG_SANDBOX64
Simon Glassd80cda22019-10-11 16:16:47 -060047#define writeq(v, addr) sandbox_write((void *)addr, v, SB_SIZE_64)
Mario Six61efece2018-02-12 08:05:57 +010048#endif
Simon Glass125a27b2014-06-11 23:29:43 -060049
Tom Rini36629472017-09-29 08:53:53 -040050/*
51 * Clear and set bits in one shot. These macros can be used to clear and
52 * set multiple bits in a register using a single call. These macros can
53 * also be used to set a multiple-bit bit pattern using a mask, by
54 * specifying the mask in the 'clear' parameter and the new bit pattern
55 * in the 'set' parameter.
56 */
57
58#define out_arch(type,endian,a,v) write##type(cpu_to_##endian(v),a)
59#define in_arch(type,endian,a) endian##_to_cpu(read##type(a))
60
61#define out_le64(a,v) out_arch(q,le64,a,v)
62#define out_le32(a,v) out_arch(l,le32,a,v)
63#define out_le16(a,v) out_arch(w,le16,a,v)
64
65#define in_le64(a) in_arch(q,le64,a)
66#define in_le32(a) in_arch(l,le32,a)
67#define in_le16(a) in_arch(w,le16,a)
68
69#define out_be32(a,v) out_arch(l,be32,a,v)
70#define out_be16(a,v) out_arch(w,be16,a,v)
71
72#define in_be32(a) in_arch(l,be32,a)
73#define in_be16(a) in_arch(w,be16,a)
74
75#define out_8(a,v) writeb(v,a)
76#define in_8(a) readb(a)
77
78#define clrbits(type, addr, clear) \
79 out_##type((addr), in_##type(addr) & ~(clear))
80
81#define setbits(type, addr, set) \
82 out_##type((addr), in_##type(addr) | (set))
83
84#define clrsetbits(type, addr, clear, set) \
85 out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
86
87#define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
88#define setbits_be32(addr, set) setbits(be32, addr, set)
89#define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
90
91#define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
92#define setbits_le32(addr, set) setbits(le32, addr, set)
93#define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
94
95#define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
96#define setbits_be16(addr, set) setbits(be16, addr, set)
97#define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
98
99#define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
100#define setbits_le16(addr, set) setbits(le16, addr, set)
101#define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
102
103#define clrbits_8(addr, clear) clrbits(8, addr, clear)
104#define setbits_8(addr, set) setbits(8, addr, set)
105#define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
106
Simon Glass70778bc2015-03-05 12:25:26 -0700107/* I/O access functions */
Simon Glassca6f1212019-09-25 08:56:02 -0600108int _inl(unsigned int addr);
109int _inw(unsigned int addr);
110int _inb(unsigned int addr);
Simon Glass70778bc2015-03-05 12:25:26 -0700111
Simon Glassca6f1212019-09-25 08:56:02 -0600112void _outl(unsigned int value, unsigned int addr);
113void _outw(unsigned int value, unsigned int addr);
114void _outb(unsigned int value, unsigned int addr);
115
116#define inb(port) _inb((uintptr_t)(port))
117#define inw(port) _inw((uintptr_t)(port))
118#define inl(port) _inl((uintptr_t)(port))
119
120#define outb(val, port) _outb(val, (uintptr_t)(port))
121#define outw(val, port) _outw(val, (uintptr_t)(port))
122#define outl(val, port) _outl(val, (uintptr_t)(port))
Simon Glass70778bc2015-03-05 12:25:26 -0700123
Maxime Ripardf768aa82017-10-02 15:25:56 +0200124#define out_arch(type,endian,a,v) write##type(cpu_to_##endian(v),a)
125#define in_arch(type,endian,a) endian##_to_cpu(read##type(a))
126
127#define out_le32(a,v) out_arch(l,le32,a,v)
128#define out_le16(a,v) out_arch(w,le16,a,v)
129
130#define in_le32(a) in_arch(l,le32,a)
131#define in_le16(a) in_arch(w,le16,a)
132
133#define out_be32(a,v) out_arch(l,be32,a,v)
134#define out_be16(a,v) out_arch(w,be16,a,v)
135
136#define in_be32(a) in_arch(l,be32,a)
137#define in_be16(a) in_arch(w,be16,a)
138
139#define out_8(a,v) writeb(v,a)
140#define in_8(a) readb(a)
141
142#define clrbits(type, addr, clear) \
143 out_##type((addr), in_##type(addr) & ~(clear))
144
145#define setbits(type, addr, set) \
146 out_##type((addr), in_##type(addr) | (set))
147
148#define clrsetbits(type, addr, clear, set) \
149 out_##type((addr), (in_##type(addr) & ~(clear)) | (set))
150
151#define clrbits_be32(addr, clear) clrbits(be32, addr, clear)
152#define setbits_be32(addr, set) setbits(be32, addr, set)
153#define clrsetbits_be32(addr, clear, set) clrsetbits(be32, addr, clear, set)
154
155#define clrbits_le32(addr, clear) clrbits(le32, addr, clear)
156#define setbits_le32(addr, set) setbits(le32, addr, set)
157#define clrsetbits_le32(addr, clear, set) clrsetbits(le32, addr, clear, set)
158
159#define clrbits_be16(addr, clear) clrbits(be16, addr, clear)
160#define setbits_be16(addr, set) setbits(be16, addr, set)
161#define clrsetbits_be16(addr, clear, set) clrsetbits(be16, addr, clear, set)
162
163#define clrbits_le16(addr, clear) clrbits(le16, addr, clear)
164#define setbits_le16(addr, set) setbits(le16, addr, set)
165#define clrsetbits_le16(addr, clear, set) clrsetbits(le16, addr, clear, set)
166
167#define clrbits_8(addr, clear) clrbits(8, addr, clear)
168#define setbits_8(addr, set) setbits(8, addr, set)
169#define clrsetbits_8(addr, clear, set) clrsetbits(8, addr, clear, set)
170
Simon Glass8d5e2452016-05-01 11:35:54 -0600171static inline void _insw(volatile u16 *port, void *buf, int ns)
172{
173}
174
175static inline void _outsw(volatile u16 *port, const void *buf, int ns)
176{
177}
178
Christian GMEINER048b2f42018-12-04 20:35:24 +0100179static inline void memset_io(volatile void *addr, unsigned char val, int count)
180{
181}
182
183static inline void memcpy_fromio(void *dst, const volatile void *src, int count)
184{
185}
186
187static inline void memcpy_toio(volatile void *dst, const void *src, int count)
188{
189}
190
Simon Glass8d5e2452016-05-01 11:35:54 -0600191#define insw(port, buf, ns) _insw((u16 *)port, buf, ns)
192#define outsw(port, buf, ns) _outsw((u16 *)port, buf, ns)
193
Simon Glassca6f1212019-09-25 08:56:02 -0600194/* IO space accessors */
195#define clrio(type, addr, clear) \
196 out##type(in##type(addr) & ~(clear), (addr))
197
198#define setio(type, addr, set) \
199 out##type(in##type(addr) | (set), (addr))
200
201#define clrsetio(type, addr, clear, set) \
202 out##type((in##type(addr) & ~(clear)) | (set), (addr))
203
204#define clrio_32(addr, clear) clrio(l, addr, clear)
205#define clrio_16(addr, clear) clrio(w, addr, clear)
206#define clrio_8(addr, clear) clrio(b, addr, clear)
207
208#define setio_32(addr, set) setio(l, addr, set)
209#define setio_16(addr, set) setio(w, addr, set)
210#define setio_8(addr, set) setio(b, addr, set)
211
212#define clrsetio_32(addr, clear, set) clrsetio(l, addr, clear, set)
213#define clrsetio_16(addr, clear, set) clrsetio(w, addr, clear, set)
214#define clrsetio_8(addr, clear, set) clrsetio(b, addr, clear, set)
215
Simon Glass125a27b2014-06-11 23:29:43 -0600216#include <iotrace.h>
Tom Rini22e42682016-03-15 13:20:23 -0400217#include <asm/types.h>
Igor Prusov91341f82023-11-14 14:02:45 +0300218#include <asm-generic/io.h>
219
220/* For sandbox, we want addresses to point into our RAM buffer */
221static inline void *map_sysmem(phys_addr_t paddr, unsigned long len)
222{
223 return map_physmem(paddr, len, MAP_WRBACK);
224}
225
226/* Remove a previous mapping */
227static inline void unmap_sysmem(const void *vaddr)
228{
229 unmap_physmem(vaddr, MAP_WRBACK);
230}
231
Simon Glass125a27b2014-06-11 23:29:43 -0600232
Simon Glass62cf9122013-04-26 02:53:43 +0000233#endif