obio.c revision 1.43 1 1.43 pk /* $NetBSD: obio.c,v 1.43 1998/04/07 20:20:03 pk Exp $ */
2 1.39 pk
3 1.39 pk /*-
4 1.43 pk * Copyright (c) 1997,1998 The NetBSD Foundation, Inc.
5 1.39 pk * All rights reserved.
6 1.39 pk *
7 1.39 pk * This code is derived from software contributed to The NetBSD Foundation
8 1.39 pk * by Paul Kranenburg.
9 1.39 pk *
10 1.39 pk * Redistribution and use in source and binary forms, with or without
11 1.39 pk * modification, are permitted provided that the following conditions
12 1.39 pk * are met:
13 1.39 pk * 1. Redistributions of source code must retain the above copyright
14 1.39 pk * notice, this list of conditions and the following disclaimer.
15 1.39 pk * 2. Redistributions in binary form must reproduce the above copyright
16 1.39 pk * notice, this list of conditions and the following disclaimer in the
17 1.39 pk * documentation and/or other materials provided with the distribution.
18 1.39 pk * 3. All advertising materials mentioning features or use of this software
19 1.39 pk * must display the following acknowledgement:
20 1.39 pk * This product includes software developed by the NetBSD
21 1.39 pk * Foundation, Inc. and its contributors.
22 1.39 pk * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.39 pk * contributors may be used to endorse or promote products derived
24 1.39 pk * from this software without specific prior written permission.
25 1.39 pk *
26 1.39 pk * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.39 pk * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.39 pk * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.39 pk * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.39 pk * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.39 pk * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.39 pk * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.39 pk * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.39 pk * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.39 pk * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.39 pk * POSSIBILITY OF SUCH DAMAGE.
37 1.39 pk */
38 1.1 deraadt
39 1.1 deraadt
40 1.1 deraadt #include <sys/param.h>
41 1.20 christos #include <sys/systm.h>
42 1.1 deraadt #include <sys/device.h>
43 1.1 deraadt #include <sys/malloc.h>
44 1.1 deraadt
45 1.1 deraadt #ifdef DEBUG
46 1.1 deraadt #include <sys/proc.h>
47 1.1 deraadt #include <sys/syslog.h>
48 1.1 deraadt #endif
49 1.1 deraadt
50 1.1 deraadt #include <vm/vm.h>
51 1.1 deraadt
52 1.40 pk #include <machine/bus.h>
53 1.40 pk #include <sparc/dev/sbusvar.h>
54 1.1 deraadt #include <machine/autoconf.h>
55 1.1 deraadt #include <machine/pmap.h>
56 1.2 deraadt #include <machine/oldmon.h>
57 1.5 deraadt #include <machine/cpu.h>
58 1.2 deraadt #include <machine/ctlreg.h>
59 1.2 deraadt #include <sparc/sparc/asm.h>
60 1.2 deraadt #include <sparc/sparc/vaddrs.h>
61 1.30 pk #include <sparc/sparc/cpuvar.h>
62 1.1 deraadt
63 1.40 pk struct obio4_softc {
64 1.40 pk struct device sc_dev; /* base device */
65 1.40 pk bus_space_tag_t sc_bustag; /* parent bus tag */
66 1.40 pk bus_dma_tag_t sc_dmatag; /* parent bus dma tag */
67 1.40 pk };
68 1.40 pk
69 1.40 pk union obio_softc {
70 1.40 pk struct device sc_dev; /* base device */
71 1.40 pk struct obio4_softc sc_obio; /* sun4 obio */
72 1.40 pk struct sbus_softc sc_sbus; /* sun4m obio is another sbus slot */
73 1.1 deraadt };
74 1.1 deraadt
75 1.32 pk
76 1.1 deraadt /* autoconfiguration driver */
77 1.40 pk static int obiomatch __P((struct device *, struct cfdata *, void *));
78 1.40 pk static void obioattach __P((struct device *, struct device *, void *));
79 1.39 pk
80 1.42 pk struct cfattach obio_ca = {
81 1.42 pk sizeof(union obio_softc), obiomatch, obioattach
82 1.42 pk };
83 1.42 pk
84 1.39 pk #if defined(SUN4)
85 1.40 pk static int obioprint __P((void *, const char *));
86 1.40 pk static int obiosearch __P((struct device *, struct cfdata *, void *));
87 1.43 pk static int obio_bus_mmap __P((bus_space_tag_t, bus_type_t, bus_addr_t,
88 1.43 pk int, bus_space_handle_t *));
89 1.43 pk static int _obio_bus_map __P((bus_space_tag_t, bus_type_t, bus_addr_t,
90 1.43 pk bus_size_t, int,
91 1.43 pk vm_offset_t, bus_space_handle_t *));
92 1.40 pk
93 1.40 pk static struct sparc_bus_space_tag obio_space_tag = {
94 1.40 pk NULL, /* cookie */
95 1.43 pk NULL, /* parent bus tag */
96 1.42 pk _obio_bus_map, /* bus_space_map */
97 1.40 pk NULL, /* bus_space_unmap */
98 1.40 pk NULL, /* bus_space_subregion */
99 1.40 pk NULL, /* bus_space_barrier */
100 1.40 pk obio_bus_mmap, /* bus_space_mmap */
101 1.40 pk NULL /* bus_intr_establish */
102 1.40 pk };
103 1.39 pk #endif
104 1.4 deraadt
105 1.40 pk /*
106 1.40 pk * Translate obio `interrupts' property value to processor IPL (see sbus.c)
107 1.40 pk * Apparently, the `interrupts' property on obio devices is just
108 1.40 pk * the processor IPL.
109 1.40 pk */
110 1.40 pk static int intr_obio2ipl[] = {
111 1.40 pk 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
112 1.4 deraadt };
113 1.21 thorpej
114 1.1 deraadt int
115 1.39 pk obiomatch(parent, cf, aux)
116 1.1 deraadt struct device *parent;
117 1.28 pk struct cfdata *cf;
118 1.28 pk void *aux;
119 1.1 deraadt {
120 1.40 pk struct mainbus_attach_args *ma = aux;
121 1.3 deraadt
122 1.40 pk return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
123 1.1 deraadt }
124 1.1 deraadt
125 1.39 pk void
126 1.40 pk obioattach(parent, self, aux)
127 1.39 pk struct device *parent, *self;
128 1.40 pk void *aux;
129 1.39 pk {
130 1.40 pk struct mainbus_attach_args *ma = aux;
131 1.1 deraadt
132 1.40 pk /*
133 1.40 pk * There is only one obio bus
134 1.40 pk */
135 1.39 pk if (self->dv_unit > 0) {
136 1.39 pk printf(" unsupported\n");
137 1.39 pk return;
138 1.39 pk }
139 1.40 pk printf("\n");
140 1.32 pk
141 1.40 pk if (CPU_ISSUN4) {
142 1.40 pk #if defined(SUN4)
143 1.42 pk struct obio4_softc *sc = &((union obio_softc *)self)->sc_obio;
144 1.42 pk
145 1.42 pk sc->sc_bustag = ma->ma_bustag;
146 1.42 pk sc->sc_dmatag = ma->ma_dmatag;
147 1.42 pk
148 1.42 pk obio_space_tag.cookie = sc;
149 1.43 pk obio_space_tag.parent = sc->sc_bustag;
150 1.40 pk
151 1.40 pk (void)config_search(obiosearch, self, aux);
152 1.39 pk #endif
153 1.40 pk return;
154 1.40 pk } else if (CPU_ISSUN4M) {
155 1.40 pk /*
156 1.40 pk * Attach the on-board I/O bus at on a sun4m.
157 1.40 pk * In this case we treat the obio bus as another sbus slot.
158 1.40 pk */
159 1.40 pk struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus;
160 1.40 pk
161 1.40 pk static const char *const special4m[] = {
162 1.40 pk /* find these first */
163 1.40 pk "eeprom",
164 1.40 pk "counter",
165 1.29 pk #if 0 /* Not all sun4m's have an `auxio' */
166 1.40 pk "auxio",
167 1.29 pk #endif
168 1.40 pk "",
169 1.40 pk /* place device to ignore here */
170 1.40 pk "interrupt",
171 1.40 pk NULL
172 1.40 pk };
173 1.40 pk
174 1.40 pk sc->sc_bustag = ma->ma_bustag;
175 1.40 pk sc->sc_dmatag = ma->ma_dmatag;
176 1.40 pk sc->sc_intr2ipl = intr_obio2ipl;
177 1.40 pk
178 1.40 pk sbus_attach(sc, "obio", ma->ma_node, ma->ma_bp, special4m);
179 1.40 pk } else {
180 1.40 pk printf("obio on this machine?\n");
181 1.40 pk }
182 1.40 pk }
183 1.22 pk
184 1.42 pk #if defined(SUN4)
185 1.40 pk int
186 1.42 pk obioprint(args, busname)
187 1.42 pk void *args;
188 1.42 pk const char *busname;
189 1.40 pk {
190 1.42 pk union obio_attach_args *uoba = args;
191 1.42 pk struct obio4_attach_args *oba = &uoba->uoba_oba4;
192 1.22 pk
193 1.42 pk printf(" addr 0x%lx", (long)oba->oba_paddr);
194 1.42 pk if (oba->oba_pri != -1)
195 1.42 pk printf(" level %d", oba->oba_pri);
196 1.22 pk
197 1.42 pk return (UNCONF);
198 1.40 pk }
199 1.22 pk
200 1.40 pk int
201 1.43 pk _obio_bus_map(t, btype, paddr, size, flags, vaddr, hp)
202 1.43 pk bus_space_tag_t t;
203 1.42 pk bus_type_t btype;
204 1.42 pk bus_addr_t paddr;
205 1.42 pk bus_size_t size;
206 1.40 pk int flags;
207 1.42 pk vm_offset_t vaddr;
208 1.40 pk bus_space_handle_t *hp;
209 1.40 pk {
210 1.43 pk struct obio4_softc *sc = t->cookie;
211 1.22 pk
212 1.40 pk if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
213 1.40 pk obio_find_rom_map(paddr, PMAP_OBIO, size, hp) == 0)
214 1.40 pk return (0);
215 1.22 pk
216 1.42 pk return (bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr,
217 1.42 pk size, flags, vaddr, hp));
218 1.40 pk }
219 1.22 pk
220 1.40 pk int
221 1.43 pk obio_bus_mmap(t, btype, paddr, flags, hp)
222 1.43 pk bus_space_tag_t t;
223 1.40 pk bus_type_t btype;
224 1.40 pk bus_addr_t paddr;
225 1.40 pk int flags;
226 1.43 pk bus_space_handle_t *hp;
227 1.40 pk {
228 1.43 pk struct obio4_softc *sc = t->cookie;
229 1.22 pk
230 1.43 pk return (bus_space_mmap(sc->sc_bustag, PMAP_OBIO, paddr, flags, hp));
231 1.22 pk }
232 1.35 pk
233 1.14 pk int
234 1.40 pk obiosearch(parent, cf, aux)
235 1.14 pk struct device *parent;
236 1.28 pk struct cfdata *cf;
237 1.40 pk void *aux;
238 1.1 deraadt {
239 1.40 pk struct mainbus_attach_args *ma = aux;
240 1.40 pk union obio_attach_args uoba;
241 1.40 pk struct obio4_attach_args *oba = &uoba.uoba_oba4;
242 1.40 pk struct bootpath *bp;
243 1.1 deraadt
244 1.23 chuck
245 1.39 pk /*
246 1.39 pk * Avoid sun4m entries which don't have valid PAs.
247 1.39 pk * no point in even probing them.
248 1.39 pk */
249 1.39 pk if (cf->cf_loc[0] == -1)
250 1.39 pk return (0);
251 1.39 pk
252 1.39 pk /*
253 1.39 pk * On the 4/100 obio addresses must be mapped at
254 1.39 pk * 0x0YYYYYYY, but alias higher up (we avoid the
255 1.39 pk * alias condition because it causes pmap difficulties)
256 1.39 pk * XXX: We also assume that 4/[23]00 obio addresses
257 1.39 pk * must be 0xZYYYYYYY, where (Z != 0)
258 1.39 pk */
259 1.40 pk if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000))
260 1.39 pk return (0);
261 1.40 pk if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000))
262 1.39 pk return (0);
263 1.14 pk
264 1.40 pk uoba.uoba_isobio4 = 1;
265 1.40 pk oba->oba_bustag = &obio_space_tag;
266 1.40 pk oba->oba_dmatag = ma->ma_dmatag;
267 1.42 pk oba->oba_paddr = cf->cf_loc[0];
268 1.40 pk oba->oba_pri = cf->cf_loc[1];
269 1.40 pk
270 1.40 pk bp = ma->ma_bp;
271 1.40 pk if (bp != NULL && strcmp(bp->name, "obio") == 0)
272 1.40 pk oba->oba_bp = bp + 1;
273 1.15 pk else
274 1.40 pk oba->oba_bp = NULL;
275 1.39 pk
276 1.40 pk if ((*cf->cf_attach->ca_match)(parent, cf, &uoba) == 0)
277 1.39 pk return (0);
278 1.14 pk
279 1.40 pk config_attach(parent, cf, &uoba, obioprint);
280 1.39 pk return (1);
281 1.5 deraadt }
282 1.4 deraadt
283 1.2 deraadt
284 1.2 deraadt /*
285 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
286 1.2 deraadt * Else, create a new mapping.
287 1.2 deraadt */
288 1.40 pk int
289 1.40 pk obio_find_rom_map(pa, iospace, len, hp)
290 1.40 pk bus_addr_t pa;
291 1.40 pk bus_type_t iospace;
292 1.40 pk int len;
293 1.40 pk bus_space_handle_t *hp;
294 1.2 deraadt {
295 1.40 pk #define getpte(va) lda(va, ASI_PTE)
296 1.40 pk
297 1.40 pk u_long pf;
298 1.40 pk int pgtype;
299 1.40 pk u_long va, pte;
300 1.40 pk
301 1.42 pk if (len > NBPG)
302 1.40 pk return (EINVAL);
303 1.40 pk
304 1.40 pk pf = pa >> PGSHIFT;
305 1.40 pk pgtype = PMAP_T2PTE_4(iospace);
306 1.40 pk
307 1.40 pk for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
308 1.40 pk pte = getpte(va);
309 1.40 pk if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
310 1.40 pk (pte & PG_PFNUM) != pf)
311 1.40 pk continue;
312 1.2 deraadt
313 1.40 pk /*
314 1.40 pk * Found entry in PROM's pagetable
315 1.40 pk * note: preserve page offset
316 1.40 pk */
317 1.40 pk *hp = (bus_space_handle_t)(va | ((u_long)pa & PGOFSET));
318 1.40 pk return (0);
319 1.2 deraadt }
320 1.32 pk
321 1.40 pk return (ENOENT);
322 1.2 deraadt }
323 1.39 pk #endif /* SUN4 */
324