1 /* $OpenBSD: bus_space.c,v 1.2 2020/09/01 18:46:59 kettenis Exp $ */
2
3 /*
4 * Copyright (c) 2001-2003 Opsycon AB (www.opsycon.se / www.opsycon.com)
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 */
28
29 /*
30 * Simple generic bus access primitives.
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35
36 #include <machine/bus.h>
37 #include <uvm/uvm_extern.h>
38
39 uint8_t
generic_space_read_1(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)40 generic_space_read_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
41 {
42 return *(volatile uint8_t *)(h + o);
43 }
44
45 uint16_t
generic_space_read_2(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)46 generic_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
47 {
48 return *(volatile uint16_t *)(h + o);
49 }
50
51 uint32_t
generic_space_read_4(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)52 generic_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
53 {
54 return *(volatile uint32_t *)(h + o);
55 }
56
57 uint64_t
generic_space_read_8(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)58 generic_space_read_8(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
59 {
60 return *(volatile uint64_t *)(h + o);
61 }
62
63 void
generic_space_write_1(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint8_t v)64 generic_space_write_1(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
65 uint8_t v)
66 {
67 *(volatile uint8_t *)(h + o) = v;
68 }
69
70 void
generic_space_write_2(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint16_t v)71 generic_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
72 uint16_t v)
73 {
74 *(volatile uint16_t *)(h + o) = v;
75 }
76
77 void
generic_space_write_4(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint32_t v)78 generic_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
79 uint32_t v)
80 {
81 *(volatile uint32_t *)(h + o) = v;
82 }
83
84 void
generic_space_write_8(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint64_t v)85 generic_space_write_8(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
86 uint64_t v)
87 {
88 *(volatile uint64_t *)(h + o) = v;
89 }
90
91 void
generic_space_read_raw_2(bus_space_tag_t t,bus_space_handle_t h,bus_addr_t o,uint8_t * buf,bus_size_t len)92 generic_space_read_raw_2(bus_space_tag_t t, bus_space_handle_t h, bus_addr_t o,
93 uint8_t *buf, bus_size_t len)
94 {
95 volatile uint16_t *addr = (volatile uint16_t *)(h + o);
96
97 len >>= 1;
98 while (len-- != 0) {
99 *(uint16_t *)buf = *addr;
100 buf += 2;
101 }
102 }
103
104 void
generic_space_write_raw_2(bus_space_tag_t t,bus_space_handle_t h,bus_addr_t o,const uint8_t * buf,bus_size_t len)105 generic_space_write_raw_2(bus_space_tag_t t, bus_space_handle_t h, bus_addr_t o,
106 const uint8_t *buf, bus_size_t len)
107 {
108 volatile uint16_t *addr = (volatile uint16_t *)(h + o);
109
110 len >>= 1;
111 while (len-- != 0) {
112 *addr = *(uint16_t *)buf;
113 buf += 2;
114 }
115 }
116
117 void
generic_space_read_raw_4(bus_space_tag_t t,bus_space_handle_t h,bus_addr_t o,uint8_t * buf,bus_size_t len)118 generic_space_read_raw_4(bus_space_tag_t t, bus_space_handle_t h, bus_addr_t o,
119 uint8_t *buf, bus_size_t len)
120 {
121 volatile uint32_t *addr = (volatile uint32_t *)(h + o);
122
123 len >>= 2;
124 while (len-- != 0) {
125 *(uint32_t *)buf = *addr;
126 buf += 4;
127 }
128 }
129
130 void
generic_space_write_raw_4(bus_space_tag_t t,bus_space_handle_t h,bus_addr_t o,const uint8_t * buf,bus_size_t len)131 generic_space_write_raw_4(bus_space_tag_t t, bus_space_handle_t h, bus_addr_t o,
132 const uint8_t *buf, bus_size_t len)
133 {
134 volatile uint32_t *addr = (volatile uint32_t *)(h + o);
135
136 len >>= 2;
137 while (len-- != 0) {
138 *addr = *(uint32_t *)buf;
139 buf += 4;
140 }
141 }
142
143 void
generic_space_read_raw_8(bus_space_tag_t t,bus_space_handle_t h,bus_addr_t o,uint8_t * buf,bus_size_t len)144 generic_space_read_raw_8(bus_space_tag_t t, bus_space_handle_t h, bus_addr_t o,
145 uint8_t *buf, bus_size_t len)
146 {
147 volatile uint64_t *addr = (volatile uint64_t *)(h + o);
148
149 len >>= 3;
150 while (len-- != 0) {
151 *(uint64_t *)buf = *addr;
152 buf += 8;
153 }
154 }
155
156 void
generic_space_write_raw_8(bus_space_tag_t t,bus_space_handle_t h,bus_addr_t o,const uint8_t * buf,bus_size_t len)157 generic_space_write_raw_8(bus_space_tag_t t, bus_space_handle_t h, bus_addr_t o,
158 const uint8_t *buf, bus_size_t len)
159 {
160 volatile uint64_t *addr = (volatile uint64_t *)(h + o);
161
162 len >>= 3;
163 while (len-- != 0) {
164 *addr = *(uint64_t *)buf;
165 buf += 8;
166 }
167 }
168
169 int
generic_space_map(bus_space_tag_t t,bus_addr_t offs,bus_size_t size,int flags,bus_space_handle_t * bshp)170 generic_space_map(bus_space_tag_t t, bus_addr_t offs, bus_size_t size,
171 int flags, bus_space_handle_t *bshp)
172 {
173 paddr_t startpa, endpa, pa;
174 vaddr_t va;
175 int cache = (flags & BUS_SPACE_MAP_CACHEABLE) ? 0 : PMAP_NOCACHE;
176
177 startpa = trunc_page(offs);
178 endpa = round_page(offs + size);
179
180 va = (vaddr_t)km_alloc(endpa - startpa, &kv_any, &kp_none, &kd_nowait);
181 if (va == 0)
182 return ENOMEM;
183
184 *bshp = (bus_space_handle_t)(va + (offs - startpa));
185
186 for (pa = startpa; pa < endpa; pa += PAGE_SIZE, va += PAGE_SIZE)
187 pmap_kenter_pa(va, pa | cache, PROT_READ | PROT_WRITE);
188 pmap_update(pmap_kernel());
189
190 return 0;
191 }
192
193 void
generic_space_unmap(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t size)194 generic_space_unmap(bus_space_tag_t t, bus_space_handle_t bsh, bus_size_t size)
195 {
196 vaddr_t va, endva;
197
198 va = trunc_page((vaddr_t)bsh);
199 endva = round_page((vaddr_t)bsh + size);
200
201 pmap_kremove(va, endva - va);
202 pmap_update(pmap_kernel());
203
204 km_free((void *)va, endva - va, &kv_any, &kp_none);
205 }
206
207 int
generic_space_region(bus_space_tag_t t,bus_space_handle_t bsh,bus_size_t offset,bus_size_t size,bus_space_handle_t * nbshp)208 generic_space_region(bus_space_tag_t t, bus_space_handle_t bsh,
209 bus_size_t offset, bus_size_t size, bus_space_handle_t *nbshp)
210 {
211 *nbshp = bsh + offset;
212 return 0;
213 }
214
215 void *
generic_space_vaddr(bus_space_tag_t t,bus_space_handle_t h)216 generic_space_vaddr(bus_space_tag_t t, bus_space_handle_t h)
217 {
218 return (void *)h;
219 }
220
221 paddr_t
generic_space_mmap(bus_space_tag_t t,bus_addr_t addr,off_t off,int prot,int flags)222 generic_space_mmap(bus_space_tag_t t, bus_addr_t addr, off_t off,
223 int prot, int flags)
224 {
225 return (addr + off) | PMAP_NOCACHE;
226 }
227
228 uint16_t
little_space_read_2(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)229 little_space_read_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
230 {
231 return lemtoh16((volatile uint16_t *)(h + o));
232 }
233
234 uint32_t
little_space_read_4(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)235 little_space_read_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
236 {
237 return lemtoh32((volatile uint32_t *)(h + o));
238 }
239
240 uint64_t
little_space_read_8(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o)241 little_space_read_8(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o)
242 {
243 return lemtoh64((volatile uint64_t *)(h + o));
244 }
245
246 void
little_space_write_2(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint16_t v)247 little_space_write_2(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
248 uint16_t v)
249 {
250 htolem16((volatile uint16_t *)(h + o), v);
251 }
252
253 void
little_space_write_4(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint32_t v)254 little_space_write_4(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
255 uint32_t v)
256 {
257 htolem32((volatile uint32_t *)(h + o) , v);
258 }
259
260 void
little_space_write_8(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,uint64_t v)261 little_space_write_8(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
262 uint64_t v)
263 {
264 htolem64((volatile uint64_t *)(h + o), v);
265 }
266