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