1 /* $NetBSD: bus_space.c,v 1.11 2023/12/20 00:40:44 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Steve C. Woodford and Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Implementation of bus_space mapping for the mvme68k.
34 * Derived from the hp300 bus_space implementation by Jason Thorpe.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: bus_space.c,v 1.11 2023/12/20 00:40:44 thorpej Exp $");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42
43 #include <machine/cpu.h>
44 #include <machine/pte.h>
45 #define _MVME68K_BUS_DMA_PRIVATE /* For _bus_dmamem_map/_bus_dmamem_unmap */
46 #define _MVME68K_BUS_SPACE_PRIVATE
47 #include <machine/bus.h>
48 #undef _MVME68K_BUS_DMA_PRIVATE
49 #undef _MVME68K_BUS_SPACE_PRIVATE
50
51 static void peek1(void *, void *);
52 static void peek2(void *, void *);
53 static void peek4(void *, void *);
54 static void poke1(void *, u_int);
55 static void poke2(void *, u_int);
56 static void poke4(void *, u_int);
57 static int do_peek(void (*)(void *, void *), void *, void *);
58 static int do_poke(void (*)(void *, u_int), void *, u_int);
59
60 /*
61 * Used in locore.s/trap.c to determine if faults are being trapped.
62 */
63 label_t *nofault;
64
65
66 /* ARGSUSED */
67 int
_bus_space_map(void * cookie,bus_addr_t addr,bus_size_t size,int flags,bus_space_handle_t * bushp)68 _bus_space_map(void *cookie, bus_addr_t addr, bus_size_t size, int flags,
69 bus_space_handle_t *bushp)
70 {
71 bus_dma_segment_t seg;
72 void *va;
73
74 if (addr >= intiobase_phys && addr < intiotop_phys) {
75 #ifdef DEBUG
76 if ((addr + size) >= intiotop_phys)
77 panic("%s: Invalid INTIO range!", __func__);
78 #endif
79 /*
80 * Intio space is direct-mapped in pmap_bootstrap(); just
81 * do the translation.
82 */
83 addr -= intiobase_phys;
84 *bushp = (bus_space_handle_t)&(intiobase[addr]);
85 return 0;
86 }
87
88 /*
89 * Otherwise, set up a VM mapping for the requested address range
90 */
91 seg.ds_addr = seg._ds_cpuaddr = m68k_trunc_page(addr);
92 seg.ds_len = m68k_round_page(size);
93
94 if (_bus_dmamem_map(NULL, &seg, 1, seg.ds_len, &va,
95 flags | BUS_DMA_COHERENT))
96 return EIO;
97
98 /*
99 * The handle is really the virtual address we just mapped
100 */
101 *bushp = (bus_space_handle_t)((char *)va + m68k_page_offset(addr));
102
103 return 0;
104 }
105
106 /* ARGSUSED */
107 void
_bus_space_unmap(void * cookie,bus_space_handle_t bush,bus_size_t size)108 _bus_space_unmap(void *cookie, bus_space_handle_t bush, bus_size_t size)
109 {
110 /* Nothing to do for INTIO space */
111 if ((char *)bush >= intiobase && (char *)bush < intiolimit)
112 return;
113
114 bush = m68k_trunc_page(bush);
115 size = m68k_round_page(size);
116
117 _bus_dmamem_unmap(NULL, (void *) bush, size);
118 }
119
120 /* ARGSUSED */
121 int
_bus_space_peek_1(void * cookie,bus_space_handle_t bush,bus_size_t offset,uint8_t * valuep)122 _bus_space_peek_1(void *cookie, bus_space_handle_t bush, bus_size_t offset,
123 uint8_t *valuep)
124 {
125 uint8_t v;
126
127 if (valuep == NULL)
128 valuep = &v;
129
130 return do_peek(&peek1, (void *)(bush + offset), (void *)valuep);
131 }
132
133 /* ARGSUSED */
134 int
_bus_space_peek_2(void * cookie,bus_space_handle_t bush,bus_size_t offset,uint16_t * valuep)135 _bus_space_peek_2(void *cookie, bus_space_handle_t bush, bus_size_t offset,
136 uint16_t *valuep)
137 {
138 uint16_t v;
139
140 if (valuep == NULL)
141 valuep = &v;
142
143 return do_peek(&peek2, (void *)(bush + offset), (void *)valuep);
144 }
145
146 /* ARGSUSED */
147 int
_bus_space_peek_4(void * cookie,bus_space_handle_t bush,bus_size_t offset,uint32_t * valuep)148 _bus_space_peek_4(void *cookie, bus_space_handle_t bush, bus_size_t offset,
149 uint32_t *valuep)
150 {
151 uint32_t v;
152
153 if (valuep == NULL)
154 valuep = &v;
155
156 return do_peek(&peek4, (void *)(bush + offset), (void *)valuep);
157 }
158
159 /* ARGSUSED */
160 int
_bus_space_poke_1(void * cookie,bus_space_handle_t bush,bus_size_t offset,uint8_t value)161 _bus_space_poke_1(void *cookie, bus_space_handle_t bush, bus_size_t offset,
162 uint8_t value)
163 {
164
165 return do_poke(&poke1, (void *)(bush + offset), (u_int)value);
166 }
167
168 /* ARGSUSED */
169 int
_bus_space_poke_2(void * cookie,bus_space_handle_t bush,bus_size_t offset,uint16_t value)170 _bus_space_poke_2(void *cookie, bus_space_handle_t bush, bus_size_t offset,
171 uint16_t value)
172 {
173
174 return do_poke(&poke2, (void *)(bush + offset), (u_int)value);
175 }
176
177 /* ARGSUSED */
178 int
_bus_space_poke_4(void * cookie,bus_space_handle_t bush,bus_size_t offset,uint32_t value)179 _bus_space_poke_4(void *cookie, bus_space_handle_t bush, bus_size_t offset,
180 uint32_t value)
181 {
182
183 return do_poke(&poke4, (void *)(bush + offset), (u_int)value);
184 }
185
186 static void
peek1(void * addr,void * vp)187 peek1(void *addr, void *vp)
188 {
189
190 *((uint8_t *)vp) = *((uint8_t *)addr);
191 }
192
193 static void
peek2(void * addr,void * vp)194 peek2(void *addr, void *vp)
195 {
196
197 *((uint16_t *)vp) = *((uint16_t *)addr);
198 }
199
200 static void
peek4(void * addr,void * vp)201 peek4(void *addr, void *vp)
202 {
203
204 *((uint32_t *)vp) = *((uint32_t *)addr);
205 }
206
207 static void
poke1(void * addr,u_int value)208 poke1(void *addr, u_int value)
209 {
210
211 *((uint8_t *)addr) = value;
212 }
213
214 static void
poke2(void * addr,u_int value)215 poke2(void *addr, u_int value)
216 {
217
218 *((uint16_t *)addr) = value;
219 }
220
221 static void
poke4(void * addr,u_int value)222 poke4(void *addr, u_int value)
223 {
224
225 *((uint32_t *)addr) = value;
226 }
227
228 static int
do_peek(void (* peekfn)(void *,void *),void * addr,void * valuep)229 do_peek(void (*peekfn)(void *, void *), void *addr, void *valuep)
230 {
231 label_t faultbuf;
232
233 nofault = &faultbuf;
234 if (setjmp(&faultbuf)) {
235 nofault = NULL;
236 return 1;
237 }
238
239 (*peekfn)(addr, valuep);
240
241 nofault = NULL;
242 return 0;
243 }
244
245 static int
do_poke(void (* pokefn)(void *,u_int),void * addr,u_int value)246 do_poke(void (*pokefn)(void *, u_int), void *addr, u_int value)
247 {
248 label_t faultbuf;
249
250 nofault = &faultbuf;
251 if (setjmp(&faultbuf)) {
252 nofault = NULL;
253 return 1;
254 }
255
256 (*pokefn)(addr, value);
257
258 nofault = NULL;
259 return 0;
260 }
261