obio.c revision 1.64 1 1.64 lukem /* $NetBSD: obio.c,v 1.64 2003/07/15 00:04:55 lukem 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.64 lukem #include <sys/cdefs.h>
40 1.64 lukem __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.64 2003/07/15 00:04:55 lukem Exp $");
41 1.1 deraadt
42 1.1 deraadt #include <sys/param.h>
43 1.20 christos #include <sys/systm.h>
44 1.1 deraadt #include <sys/device.h>
45 1.1 deraadt #include <sys/malloc.h>
46 1.1 deraadt
47 1.1 deraadt #ifdef DEBUG
48 1.1 deraadt #include <sys/proc.h>
49 1.1 deraadt #include <sys/syslog.h>
50 1.1 deraadt #endif
51 1.1 deraadt
52 1.48 mrg #include <uvm/uvm_extern.h>
53 1.1 deraadt
54 1.40 pk #include <machine/bus.h>
55 1.40 pk #include <sparc/dev/sbusvar.h>
56 1.1 deraadt #include <machine/autoconf.h>
57 1.2 deraadt #include <machine/oldmon.h>
58 1.5 deraadt #include <machine/cpu.h>
59 1.2 deraadt #include <machine/ctlreg.h>
60 1.2 deraadt #include <sparc/sparc/asm.h>
61 1.2 deraadt #include <sparc/sparc/vaddrs.h>
62 1.30 pk #include <sparc/sparc/cpuvar.h>
63 1.1 deraadt
64 1.40 pk struct obio4_softc {
65 1.40 pk struct device sc_dev; /* base device */
66 1.40 pk bus_space_tag_t sc_bustag; /* parent bus tag */
67 1.63 wiz bus_dma_tag_t sc_dmatag; /* parent bus DMA tag */
68 1.40 pk };
69 1.40 pk
70 1.40 pk union obio_softc {
71 1.40 pk struct device sc_dev; /* base device */
72 1.40 pk struct obio4_softc sc_obio; /* sun4 obio */
73 1.40 pk struct sbus_softc sc_sbus; /* sun4m obio is another sbus slot */
74 1.1 deraadt };
75 1.1 deraadt
76 1.32 pk
77 1.1 deraadt /* autoconfiguration driver */
78 1.40 pk static int obiomatch __P((struct device *, struct cfdata *, void *));
79 1.40 pk static void obioattach __P((struct device *, struct device *, void *));
80 1.39 pk
81 1.57 thorpej CFATTACH_DECL(obio, sizeof(union obio_softc),
82 1.58 thorpej obiomatch, obioattach, NULL, NULL);
83 1.46 pk
84 1.46 pk /*
85 1.46 pk * This `obio4_busattachargs' data structure only exists to pass down
86 1.46 pk * to obiosearch() the name of a device that must be configured early.
87 1.46 pk */
88 1.46 pk struct obio4_busattachargs {
89 1.46 pk struct mainbus_attach_args *ma;
90 1.46 pk const char *name;
91 1.46 pk };
92 1.46 pk
93 1.39 pk #if defined(SUN4)
94 1.40 pk static int obioprint __P((void *, const char *));
95 1.40 pk static int obiosearch __P((struct device *, struct cfdata *, void *));
96 1.50 eeh static paddr_t obio_bus_mmap __P((bus_space_tag_t, bus_addr_t, off_t,
97 1.50 eeh int, int));
98 1.51 pk static int _obio_bus_map __P((bus_space_tag_t, bus_addr_t,
99 1.43 pk bus_size_t, int,
100 1.44 pk vaddr_t, bus_space_handle_t *));
101 1.40 pk
102 1.40 pk static struct sparc_bus_space_tag obio_space_tag = {
103 1.40 pk NULL, /* cookie */
104 1.43 pk NULL, /* parent bus tag */
105 1.53 thorpej NULL, /* ranges */
106 1.53 thorpej 0, /* nranges */
107 1.42 pk _obio_bus_map, /* bus_space_map */
108 1.40 pk NULL, /* bus_space_unmap */
109 1.40 pk NULL, /* bus_space_subregion */
110 1.40 pk NULL, /* bus_space_barrier */
111 1.40 pk obio_bus_mmap, /* bus_space_mmap */
112 1.62 martin NULL, /* bus_intr_establish */
113 1.60 mrg #if __FULL_SPARC_BUS_SPACE
114 1.60 mrg NULL, /* read_1 */
115 1.60 mrg NULL, /* read_2 */
116 1.60 mrg NULL, /* read_4 */
117 1.60 mrg NULL, /* read_8 */
118 1.60 mrg NULL, /* write_1 */
119 1.60 mrg NULL, /* write_2 */
120 1.60 mrg NULL, /* write_4 */
121 1.60 mrg NULL, /* write_8 */
122 1.60 mrg #endif
123 1.40 pk };
124 1.39 pk #endif
125 1.4 deraadt
126 1.40 pk /*
127 1.40 pk * Translate obio `interrupts' property value to processor IPL (see sbus.c)
128 1.40 pk * Apparently, the `interrupts' property on obio devices is just
129 1.40 pk * the processor IPL.
130 1.40 pk */
131 1.40 pk static int intr_obio2ipl[] = {
132 1.40 pk 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
133 1.4 deraadt };
134 1.21 thorpej
135 1.1 deraadt int
136 1.39 pk obiomatch(parent, cf, aux)
137 1.1 deraadt struct device *parent;
138 1.28 pk struct cfdata *cf;
139 1.28 pk void *aux;
140 1.1 deraadt {
141 1.40 pk struct mainbus_attach_args *ma = aux;
142 1.3 deraadt
143 1.54 thorpej return (strcmp(cf->cf_name, ma->ma_name) == 0);
144 1.1 deraadt }
145 1.1 deraadt
146 1.39 pk void
147 1.40 pk obioattach(parent, self, aux)
148 1.39 pk struct device *parent, *self;
149 1.40 pk void *aux;
150 1.39 pk {
151 1.40 pk struct mainbus_attach_args *ma = aux;
152 1.1 deraadt
153 1.40 pk /*
154 1.40 pk * There is only one obio bus
155 1.40 pk */
156 1.39 pk if (self->dv_unit > 0) {
157 1.39 pk printf(" unsupported\n");
158 1.39 pk return;
159 1.39 pk }
160 1.40 pk printf("\n");
161 1.32 pk
162 1.40 pk if (CPU_ISSUN4) {
163 1.40 pk #if defined(SUN4)
164 1.42 pk struct obio4_softc *sc = &((union obio_softc *)self)->sc_obio;
165 1.46 pk struct obio4_busattachargs oa;
166 1.46 pk const char *const *cpp;
167 1.46 pk static const char *const special4[] = {
168 1.46 pk /* find these first */
169 1.46 pk "timer",
170 1.49 pk "dma", /* need this before `esp', if any */
171 1.46 pk NULL
172 1.46 pk };
173 1.42 pk
174 1.42 pk sc->sc_bustag = ma->ma_bustag;
175 1.42 pk sc->sc_dmatag = ma->ma_dmatag;
176 1.42 pk
177 1.42 pk obio_space_tag.cookie = sc;
178 1.43 pk obio_space_tag.parent = sc->sc_bustag;
179 1.40 pk
180 1.46 pk oa.ma = ma;
181 1.46 pk
182 1.46 pk /* Find all `early' obio devices */
183 1.46 pk for (cpp = special4; *cpp != NULL; cpp++) {
184 1.46 pk oa.name = *cpp;
185 1.46 pk (void)config_search(obiosearch, self, &oa);
186 1.46 pk }
187 1.46 pk
188 1.46 pk /* Find all other obio devices */
189 1.46 pk oa.name = NULL;
190 1.46 pk (void)config_search(obiosearch, self, &oa);
191 1.39 pk #endif
192 1.40 pk return;
193 1.40 pk } else if (CPU_ISSUN4M) {
194 1.40 pk /*
195 1.40 pk * Attach the on-board I/O bus at on a sun4m.
196 1.40 pk * In this case we treat the obio bus as another sbus slot.
197 1.40 pk */
198 1.40 pk struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus;
199 1.40 pk
200 1.40 pk static const char *const special4m[] = {
201 1.40 pk /* find these first */
202 1.40 pk "eeprom",
203 1.40 pk "counter",
204 1.29 pk #if 0 /* Not all sun4m's have an `auxio' */
205 1.40 pk "auxio",
206 1.29 pk #endif
207 1.40 pk "",
208 1.40 pk /* place device to ignore here */
209 1.40 pk "interrupt",
210 1.40 pk NULL
211 1.40 pk };
212 1.40 pk
213 1.40 pk sc->sc_bustag = ma->ma_bustag;
214 1.40 pk sc->sc_dmatag = ma->ma_dmatag;
215 1.40 pk sc->sc_intr2ipl = intr_obio2ipl;
216 1.40 pk
217 1.47 pk sbus_attach_common(sc, "obio", ma->ma_node, special4m);
218 1.40 pk } else {
219 1.40 pk printf("obio on this machine?\n");
220 1.40 pk }
221 1.40 pk }
222 1.22 pk
223 1.42 pk #if defined(SUN4)
224 1.40 pk int
225 1.42 pk obioprint(args, busname)
226 1.42 pk void *args;
227 1.42 pk const char *busname;
228 1.40 pk {
229 1.42 pk union obio_attach_args *uoba = args;
230 1.42 pk struct obio4_attach_args *oba = &uoba->uoba_oba4;
231 1.22 pk
232 1.59 thorpej aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
233 1.42 pk if (oba->oba_pri != -1)
234 1.59 thorpej aprint_normal(" level %d", oba->oba_pri);
235 1.22 pk
236 1.42 pk return (UNCONF);
237 1.40 pk }
238 1.22 pk
239 1.40 pk int
240 1.51 pk _obio_bus_map(t, ba, size, flags, va, hp)
241 1.43 pk bus_space_tag_t t;
242 1.51 pk bus_addr_t ba;
243 1.42 pk bus_size_t size;
244 1.40 pk int flags;
245 1.51 pk vaddr_t va;
246 1.40 pk bus_space_handle_t *hp;
247 1.40 pk {
248 1.43 pk struct obio4_softc *sc = t->cookie;
249 1.22 pk
250 1.40 pk if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
251 1.51 pk obio_find_rom_map(ba, size, hp) == 0)
252 1.40 pk return (0);
253 1.22 pk
254 1.52 pk return (bus_space_map2(sc->sc_bustag, ba, size, flags, va, hp));
255 1.40 pk }
256 1.22 pk
257 1.50 eeh paddr_t
258 1.51 pk obio_bus_mmap(t, ba, off, prot, flags)
259 1.43 pk bus_space_tag_t t;
260 1.51 pk bus_addr_t ba;
261 1.50 eeh off_t off;
262 1.50 eeh int prot;
263 1.40 pk int flags;
264 1.40 pk {
265 1.43 pk struct obio4_softc *sc = t->cookie;
266 1.22 pk
267 1.52 pk return (bus_space_mmap(sc->sc_bustag, ba, off, prot, flags));
268 1.22 pk }
269 1.35 pk
270 1.14 pk int
271 1.40 pk obiosearch(parent, cf, aux)
272 1.14 pk struct device *parent;
273 1.28 pk struct cfdata *cf;
274 1.40 pk void *aux;
275 1.1 deraadt {
276 1.46 pk struct obio4_busattachargs *oap = aux;
277 1.40 pk union obio_attach_args uoba;
278 1.40 pk struct obio4_attach_args *oba = &uoba.uoba_oba4;
279 1.1 deraadt
280 1.46 pk /* Check whether we're looking for a specifically named device */
281 1.54 thorpej if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
282 1.46 pk return (0);
283 1.23 chuck
284 1.39 pk /*
285 1.39 pk * Avoid sun4m entries which don't have valid PAs.
286 1.39 pk * no point in even probing them.
287 1.39 pk */
288 1.39 pk if (cf->cf_loc[0] == -1)
289 1.39 pk return (0);
290 1.39 pk
291 1.39 pk /*
292 1.39 pk * On the 4/100 obio addresses must be mapped at
293 1.39 pk * 0x0YYYYYYY, but alias higher up (we avoid the
294 1.39 pk * alias condition because it causes pmap difficulties)
295 1.39 pk * XXX: We also assume that 4/[23]00 obio addresses
296 1.39 pk * must be 0xZYYYYYYY, where (Z != 0)
297 1.39 pk */
298 1.40 pk if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000))
299 1.39 pk return (0);
300 1.40 pk if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000))
301 1.39 pk return (0);
302 1.14 pk
303 1.40 pk uoba.uoba_isobio4 = 1;
304 1.40 pk oba->oba_bustag = &obio_space_tag;
305 1.46 pk oba->oba_dmatag = oap->ma->ma_dmatag;
306 1.52 pk oba->oba_paddr = BUS_ADDR(PMAP_OBIO, cf->cf_loc[0]);
307 1.40 pk oba->oba_pri = cf->cf_loc[1];
308 1.39 pk
309 1.55 thorpej if (config_match(parent, cf, &uoba) == 0)
310 1.39 pk return (0);
311 1.14 pk
312 1.40 pk config_attach(parent, cf, &uoba, obioprint);
313 1.39 pk return (1);
314 1.5 deraadt }
315 1.4 deraadt
316 1.2 deraadt
317 1.2 deraadt /*
318 1.2 deraadt * If we can find a mapping that was established by the rom, use it.
319 1.2 deraadt * Else, create a new mapping.
320 1.2 deraadt */
321 1.40 pk int
322 1.52 pk obio_find_rom_map(ba, len, hp)
323 1.52 pk bus_addr_t ba;
324 1.40 pk int len;
325 1.40 pk bus_space_handle_t *hp;
326 1.2 deraadt {
327 1.40 pk #define getpte(va) lda(va, ASI_PTE)
328 1.40 pk
329 1.52 pk u_long pa, pf;
330 1.40 pk int pgtype;
331 1.40 pk u_long va, pte;
332 1.40 pk
333 1.61 thorpej if (len > PAGE_SIZE)
334 1.40 pk return (EINVAL);
335 1.40 pk
336 1.52 pk pa = BUS_ADDR_PADDR(ba);
337 1.40 pk pf = pa >> PGSHIFT;
338 1.51 pk pgtype = PMAP_T2PTE_4(PMAP_OBIO);
339 1.40 pk
340 1.61 thorpej for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
341 1.40 pk pte = getpte(va);
342 1.40 pk if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
343 1.40 pk (pte & PG_PFNUM) != pf)
344 1.40 pk continue;
345 1.2 deraadt
346 1.40 pk /*
347 1.40 pk * Found entry in PROM's pagetable
348 1.40 pk * note: preserve page offset
349 1.40 pk */
350 1.52 pk *hp = (bus_space_handle_t)(va | (pa & PGOFSET));
351 1.40 pk return (0);
352 1.2 deraadt }
353 1.32 pk
354 1.40 pk return (ENOENT);
355 1.2 deraadt }
356 1.39 pk #endif /* SUN4 */
357