obio.c revision 1.40 1 1.40 pk /* $NetBSD: obio.c,v 1.40 1998/03/21 19:55:31 pk Exp $ */
2 1.39 pk
3 1.39 pk /*-
4 1.39 pk * Copyright (c) 1997 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.39 pk #if defined(SUN4)
81 1.40 pk static int obioprint __P((void *, const char *));
82 1.40 pk static int obiosearch __P((struct device *, struct cfdata *, void *));
83 1.40 pk static int obio_bus_mmap __P((void *, bus_type_t, bus_addr_t, int));
84 1.40 pk static int obio_find_rom_map __P((bus_addr_t, bus_type_t, int,
85 1.40 pk bus_space_handle_t *));
86 1.39 pk #endif
87 1.40 pk
88 1.40 pk struct cfattach obio_ca = {
89 1.40 pk sizeof(union obio_softc), obiomatch, obioattach
90 1.40 pk };
91 1.40 pk
92 1.40 pk #if defined(SUN4)
93 1.40 pk static struct sparc_bus_space_tag obio_space_tag = {
94 1.40 pk NULL, /* cookie */
95 1.40 pk NULL, /* bus_space_map */
96 1.40 pk NULL, /* bus_space_unmap */
97 1.40 pk NULL, /* bus_space_subregion */
98 1.40 pk NULL, /* bus_space_barrier */
99 1.40 pk obio_bus_mmap, /* bus_space_mmap */
100 1.40 pk NULL /* bus_intr_establish */
101 1.40 pk };
102 1.39 pk #endif
103 1.4 deraadt
104 1.40 pk /*
105 1.40 pk * Translate obio `interrupts' property value to processor IPL (see sbus.c)
106 1.40 pk * Apparently, the `interrupts' property on obio devices is just
107 1.40 pk * the processor IPL.
108 1.40 pk */
109 1.40 pk static int intr_obio2ipl[] = {
110 1.40 pk 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
111 1.4 deraadt };
112 1.21 thorpej
113 1.1 deraadt int
114 1.39 pk obiomatch(parent, cf, aux)
115 1.1 deraadt struct device *parent;
116 1.28 pk struct cfdata *cf;
117 1.28 pk void *aux;
118 1.1 deraadt {
119 1.40 pk struct mainbus_attach_args *ma = aux;
120 1.3 deraadt
121 1.40 pk return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
122 1.1 deraadt }
123 1.1 deraadt
124 1.40 pk #if defined(SUN4)
125 1.1 deraadt int
126 1.40 pk obioprint(args, busname)
127 1.1 deraadt void *args;
128 1.40 pk const char *busname;
129 1.1 deraadt {
130 1.40 pk union obio_attach_args *uoba = args;
131 1.40 pk struct obio4_attach_args *oba = &uoba->uoba_oba4;
132 1.2 deraadt
133 1.40 pk printf(" addr %p pri %d", oba->oba_paddr, oba->oba_pri);
134 1.22 pk
135 1.39 pk return (UNCONF);
136 1.39 pk }
137 1.40 pk #endif
138 1.22 pk
139 1.39 pk void
140 1.40 pk obioattach(parent, self, aux)
141 1.39 pk struct device *parent, *self;
142 1.40 pk void *aux;
143 1.39 pk {
144 1.40 pk struct mainbus_attach_args *ma = aux;
145 1.1 deraadt
146 1.40 pk /*
147 1.40 pk * There is only one obio bus
148 1.40 pk */
149 1.39 pk if (self->dv_unit > 0) {
150 1.39 pk printf(" unsupported\n");
151 1.39 pk return;
152 1.39 pk }
153 1.40 pk printf("\n");
154 1.32 pk
155 1.40 pk if (CPU_ISSUN4) {
156 1.40 pk #if defined(SUN4)
157 1.40 pk /* Propagate interrupt establish function */
158 1.40 pk obio_space_tag.sparc_intr_establish =
159 1.40 pk ma->ma_bustag->sparc_intr_establish;
160 1.40 pk
161 1.40 pk (void)config_search(obiosearch, self, aux);
162 1.39 pk #endif
163 1.40 pk return;
164 1.40 pk } else if (CPU_ISSUN4M) {
165 1.40 pk /*
166 1.40 pk * Attach the on-board I/O bus at on a sun4m.
167 1.40 pk * In this case we treat the obio bus as another sbus slot.
168 1.40 pk */
169 1.40 pk struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus;
170 1.40 pk
171 1.40 pk static const char *const special4m[] = {
172 1.40 pk /* find these first */
173 1.40 pk "eeprom",
174 1.40 pk "counter",
175 1.29 pk #if 0 /* Not all sun4m's have an `auxio' */
176 1.40 pk "auxio",
177 1.29 pk #endif
178 1.40 pk "",
179 1.40 pk /* place device to ignore here */
180 1.40 pk "interrupt",
181 1.40 pk NULL
182 1.40 pk };
183 1.40 pk
184 1.40 pk sc->sc_bustag = ma->ma_bustag;
185 1.40 pk sc->sc_dmatag = ma->ma_dmatag;
186 1.40 pk sc->sc_intr2ipl = intr_obio2ipl;
187 1.40 pk
188 1.40 pk sbus_attach(sc, "obio", ma->ma_node, ma->ma_bp, special4m);
189 1.40 pk } else {
190 1.40 pk printf("obio on this machine?\n");
191 1.40 pk }
192 1.40 pk }
193 1.22 pk
194 1.22 pk
195 1.40 pk int
196 1.40 pk obio_bus_probe(tag, addr, offset, size, callback, arg)
197 1.40 pk bus_space_tag_t tag;
198 1.40 pk void *addr;
199 1.40 pk int offset;
200 1.40 pk size_t size;
201 1.40 pk int (*callback) __P((void *, void *));
202 1.40 pk void *arg;
203 1.40 pk {
204 1.40 pk #if defined(SUN4)
205 1.40 pk bus_addr_t paddr = (long)addr + offset;
206 1.40 pk bus_space_handle_t bh;
207 1.40 pk caddr_t tmp;
208 1.40 pk int result;
209 1.22 pk
210 1.40 pk if (sparc_bus_map(0, PMAP_OBIO, paddr, size, 0, TMPMAP_VA, &bh) != 0)
211 1.40 pk return (0);
212 1.22 pk
213 1.40 pk tmp = (caddr_t)bh;
214 1.40 pk result = probeget(tmp + offset, size) != -1;
215 1.40 pk if (result && callback != NULL)
216 1.40 pk result = (*callback)(tmp, arg);
217 1.40 pk pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
218 1.40 pk return (result);
219 1.40 pk #else
220 1.40 pk return (ENXIO);
221 1.40 pk #endif /*SUN4*/
222 1.40 pk }
223 1.22 pk
224 1.40 pk int
225 1.40 pk obio_bus_map(tag, addr, offset, size, flags, vaddr, hp)
226 1.40 pk bus_space_tag_t tag;
227 1.40 pk void *addr;
228 1.40 pk int offset;
229 1.40 pk size_t size;
230 1.40 pk int flags;
231 1.40 pk void *vaddr;
232 1.40 pk bus_space_handle_t *hp;
233 1.40 pk {
234 1.40 pk #if defined(SUN4)
235 1.40 pk bus_addr_t paddr = (long)addr + offset;
236 1.22 pk
237 1.40 pk if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
238 1.40 pk obio_find_rom_map(paddr, PMAP_OBIO, size, hp) == 0)
239 1.40 pk return (0);
240 1.22 pk
241 1.40 pk return (sparc_bus_map(0, PMAP_OBIO, paddr, size, flags,
242 1.40 pk (vm_offset_t)vaddr, hp));
243 1.40 pk #else
244 1.40 pk return (ENXIO);
245 1.40 pk #endif /*SUN4*/
246 1.40 pk }
247 1.22 pk
248 1.40 pk #if defined(SUN4)
249 1.40 pk int
250 1.40 pk obio_bus_mmap(cookie, btype, paddr, flags)
251 1.40 pk void *cookie;
252 1.40 pk bus_type_t btype;
253 1.40 pk bus_addr_t paddr;
254 1.40 pk int flags;
255 1.40 pk {
256 1.22 pk
257 1.40 pk return (sparc_bus_mmap(0, PMAP_OBIO, paddr, flags));
258 1.22 pk }
259 1.35 pk
260 1.14 pk int
261 1.40 pk obiosearch(parent, cf, aux)
262 1.14 pk struct device *parent;
263 1.28 pk struct cfdata *cf;
264 1.40 pk void *aux;
265 1.1 deraadt {
266 1.40 pk struct mainbus_attach_args *ma = aux;
267 1.40 pk union obio_attach_args uoba;
268 1.40 pk struct obio4_attach_args *oba = &uoba.uoba_oba4;
269 1.40 pk struct bootpath *bp;
270 1.1 deraadt
271 1.23 chuck
272 1.39 pk /*
273 1.39 pk * Avoid sun4m entries which don't have valid PAs.
274 1.39 pk * no point in even probing them.
275 1.39 pk */
276 1.39 pk if (cf->cf_loc[0] == -1)
277 1.39 pk return (0);
278 1.39 pk
279 1.39 pk /*
280 1.39 pk * On the 4/100 obio addresses must be mapped at
281 1.39 pk * 0x0YYYYYYY, but alias higher up (we avoid the
282 1.39 pk * alias condition because it causes pmap difficulties)
283 1.39 pk * XXX: We also assume that 4/[23]00 obio addresses
284 1.39 pk * must be 0xZYYYYYYY, where (Z != 0)
285 1.39 pk */
286 1.40 pk if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000))
287 1.39 pk return (0);
288 1.40 pk if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000))
289 1.39 pk return (0);
290 1.14 pk
291 1.40 pk uoba.uoba_isobio4 = 1;
292 1.40 pk oba->oba_bustag = &obio_space_tag;
293 1.40 pk oba->oba_dmatag = ma->ma_dmatag;
294 1.40 pk oba->oba_paddr = (void *)cf->cf_loc[0];
295 1.40 pk oba->oba_pri = cf->cf_loc[1];
296 1.40 pk
297 1.40 pk bp = ma->ma_bp;
298 1.40 pk if (bp != NULL && strcmp(bp->name, "obio") == 0)
299 1.40 pk oba->oba_bp = bp + 1;
300 1.15 pk else
301 1.40 pk oba->oba_bp = NULL;
302 1.39 pk
303 1.40 pk if ((*cf->cf_attach->ca_match)(parent, cf, &uoba) == 0)
304 1.39 pk return (0);
305 1.14 pk
306 1.40 pk config_attach(parent, cf, &uoba, obioprint);
307 1.39 pk return (1);
308 1.5 deraadt }
309 1.4 deraadt
310 1.2 deraadt
311 1.2 deraadt /*
312 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
313 1.2 deraadt * Else, create a new mapping.
314 1.2 deraadt */
315 1.40 pk int
316 1.40 pk obio_find_rom_map(pa, iospace, len, hp)
317 1.40 pk bus_addr_t pa;
318 1.40 pk bus_type_t iospace;
319 1.40 pk int len;
320 1.40 pk bus_space_handle_t *hp;
321 1.2 deraadt {
322 1.40 pk #define getpte(va) lda(va, ASI_PTE)
323 1.40 pk
324 1.40 pk u_long pf;
325 1.40 pk int pgtype;
326 1.40 pk u_long va, pte;
327 1.40 pk
328 1.40 pk if (len <= NBPG)
329 1.40 pk return (EINVAL);
330 1.40 pk
331 1.40 pk pf = pa >> PGSHIFT;
332 1.40 pk pgtype = PMAP_T2PTE_4(iospace);
333 1.40 pk
334 1.40 pk for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
335 1.40 pk pte = getpte(va);
336 1.40 pk if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
337 1.40 pk (pte & PG_PFNUM) != pf)
338 1.40 pk continue;
339 1.2 deraadt
340 1.40 pk /*
341 1.40 pk * Found entry in PROM's pagetable
342 1.40 pk * note: preserve page offset
343 1.40 pk */
344 1.40 pk *hp = (bus_space_handle_t)(va | ((u_long)pa & PGOFSET));
345 1.40 pk return (0);
346 1.2 deraadt }
347 1.32 pk
348 1.40 pk return (ENOENT);
349 1.2 deraadt }
350 1.39 pk #endif /* SUN4 */
351