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