1 /* $NetBSD: obio.c,v 1.21 2021/08/07 16:19:06 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass, Gordon W. Ross, and Matthew Fredette.
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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.21 2021/08/07 16:19:06 thorpej Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38
39 #include <uvm/uvm_extern.h>
40
41 #include <machine/autoconf.h>
42 #include <machine/pmap.h>
43 #include <machine/pte.h>
44
45 #include <sun2/sun2/control.h>
46 #include <sun2/sun2/machdep.h>
47
48 /* Does this machine have a Multibus? */
49 extern int cpu_has_multibus;
50
51 static int obio_match(device_t, cfdata_t, void *);
52 static void obio_attach(device_t, device_t, void *);
53
54 struct obio_softc {
55 device_t sc_dev; /* base device */
56 bus_space_tag_t sc_bustag; /* parent bus tag */
57 bus_dma_tag_t sc_dmatag; /* parent bus dma tag */
58 };
59
60 CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
61 obio_match, obio_attach, NULL, NULL);
62
63 static int obio_attached;
64
65 static paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t, off_t,
66 int, int);
67 static int _obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
68 int, vaddr_t, bus_space_handle_t *);
69 static int _obio_addr_bad(bus_space_tag_t, bus_space_handle_t, bus_size_t,
70 size_t);
71 static int _obio_bus_peek(bus_space_tag_t, bus_space_handle_t, bus_size_t,
72 size_t, void *);
73 static int _obio_bus_poke(bus_space_tag_t, bus_space_handle_t, bus_size_t,
74 size_t, uint32_t);
75
76 static struct sun68k_bus_space_tag obio_space_tag = {
77 NULL, /* cookie */
78 NULL, /* parent bus tag */
79 _obio_bus_map, /* bus_space_map */
80 NULL, /* bus_space_unmap */
81 NULL, /* bus_space_subregion */
82 NULL, /* bus_space_barrier */
83 obio_bus_mmap, /* bus_space_mmap */
84 NULL, /* bus_intr_establish */
85 _obio_bus_peek, /* bus_space_peek_N */
86 _obio_bus_poke /* bus_space_poke_N */
87 };
88
89 static int
obio_match(device_t parent,cfdata_t cf,void * aux)90 obio_match(device_t parent, cfdata_t cf, void *aux)
91 {
92 struct mainbus_attach_args *ma = aux;
93
94 if (obio_attached)
95 return 0;
96
97 return ma->ma_name == NULL || strcmp(cf->cf_name, ma->ma_name) == 0;
98 }
99
100 static void
obio_attach(device_t parent,device_t self,void * aux)101 obio_attach(device_t parent, device_t self, void *aux)
102 {
103 struct mainbus_attach_args *ma = aux;
104 struct obio_softc *sc = device_private(self);
105 struct obio_attach_args oba;
106 const char *const *cpp;
107 static const char *const special[] = {
108 /* find these first */
109 NULL
110 };
111
112 obio_attached = 1;
113
114 sc->sc_dev = self;
115 aprint_normal("\n");
116
117 sc->sc_bustag = ma->ma_bustag;
118 sc->sc_dmatag = ma->ma_dmatag;
119
120 obio_space_tag.cookie = sc;
121 obio_space_tag.parent = sc->sc_bustag;
122
123 /*
124 * Prepare the skeleton attach arguments for our devices.
125 * The values we give in the locators are indications to
126 * sun68k_bus_search about which locators must and must not
127 * be defined.
128 */
129 oba = *ma;
130 oba.oba_bustag = &obio_space_tag;
131 oba.oba_paddr = LOCATOR_REQUIRED;
132 oba.oba_pri = LOCATOR_OPTIONAL;
133
134 /* Find all `early' obio devices */
135 for (cpp = special; *cpp != NULL; cpp++) {
136 oba.oba_name = *cpp;
137 config_search(self, &oba,
138 CFARGS(.search = sun68k_bus_search));
139 }
140
141 /* Find all other obio devices */
142 oba.oba_name = NULL;
143 config_search(self, &oba,
144 CFARGS(.search = sun68k_bus_search));
145 }
146
147 int
_obio_bus_map(bus_space_tag_t t,bus_type_t btype,bus_addr_t paddr,bus_size_t size,int flags,vaddr_t vaddr,bus_space_handle_t * hp)148 _obio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
149 bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
150 {
151 struct obio_softc *sc = t->cookie;
152
153 return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
154 flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
155 }
156
157 paddr_t
obio_bus_mmap(bus_space_tag_t t,bus_type_t btype,bus_addr_t paddr,off_t off,int prot,int flags)158 obio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
159 int prot, int flags)
160 {
161 struct obio_softc *sc = t->cookie;
162
163 return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off,
164 prot, flags);
165 }
166
167 /*
168 * The sun2 obio bus doesn't give bus errors, so we check on
169 * probed obio physical addresses to make sure they're ok.
170 */
171 int
_obio_addr_bad(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,size_t s)172 _obio_addr_bad(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s)
173 {
174 u_int pte;
175 paddr_t pa;
176
177 /* Get the physical address for this page. */
178 pte = get_pte((vaddr_t) (h + o));
179 if ((pte & PG_VALID) == 0)
180 return -1;
181 pa = PG_PA(pte);
182
183 /*
184 * Return nonzero if it's bad. All sun2 Multibus
185 * machines have all obio devices between 0x2000
186 * and 0x4000, and all sun2 VME machines have all
187 * obio devices outside of this range.
188 */
189 return (!!cpu_has_multibus) != (pa >= 0x2000 && pa < 0x4000);
190 }
191
192 int
_obio_bus_peek(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,size_t s,void * vp)193 _obio_bus_peek(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s,
194 void *vp)
195 {
196 struct obio_softc *sc = t->cookie;
197
198 return _obio_addr_bad(t, h, o, s) ||
199 _bus_space_peek(sc->sc_bustag, h, o, s, vp);
200 }
201
202 int
_obio_bus_poke(bus_space_tag_t t,bus_space_handle_t h,bus_size_t o,size_t s,uint32_t v)203 _obio_bus_poke(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o, size_t s,
204 uint32_t v)
205 {
206 struct obio_softc *sc = t->cookie;
207
208 return _obio_addr_bad(t, h, o, s) ||
209 _bus_space_poke(sc->sc_bustag, h, o, s, v);
210 }
211