obio.c revision 1.73 1 /* $NetBSD: obio.c,v 1.73 2012/10/27 17:18:11 chs 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.73 2012/10/27 17:18:11 chs Exp $");
34
35 #include "locators.h"
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/malloc.h>
41
42 #ifdef DEBUG
43 #include <sys/proc.h>
44 #include <sys/syslog.h>
45 #endif
46
47 #include <uvm/uvm_extern.h>
48
49 #include <sys/bus.h>
50 #include <sparc/dev/sbusvar.h>
51 #include <machine/autoconf.h>
52 #include <machine/oldmon.h>
53 #include <machine/cpu.h>
54 #include <machine/ctlreg.h>
55 #include <sparc/sparc/asm.h>
56 #include <sparc/sparc/vaddrs.h>
57 #include <sparc/sparc/cpuvar.h>
58
59 struct obio4_softc {
60 device_t sc_dev; /* base device */
61 bus_space_tag_t sc_bustag; /* parent bus tag */
62 bus_dma_tag_t sc_dmatag; /* parent bus DMA tag */
63 };
64
65 union obio_softc {
66 struct obio4_softc sc_obio; /* sun4 obio */
67 struct sbus_softc sc_sbus; /* sun4m obio is another sbus slot */
68 };
69
70
71 /* autoconfiguration driver */
72 static int obiomatch(device_t, cfdata_t, void *);
73 static void obioattach(device_t, device_t, void *);
74
75 CFATTACH_DECL_NEW(obio, sizeof(union obio_softc),
76 obiomatch, obioattach, NULL, NULL);
77
78 static int obio_attached;
79
80 /*
81 * This `obio4_busattachargs' data structure only exists to pass down
82 * to obiosearch() the name of a device that must be configured early.
83 */
84 struct obio4_busattachargs {
85 struct mainbus_attach_args *ma;
86 const char *name;
87 };
88
89 #if defined(SUN4)
90 static int obioprint(void *, const char *);
91 static int obiosearch(device_t, struct cfdata *, const int *, void *);
92 static paddr_t obio_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
93 static int _obio_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
94 vaddr_t, bus_space_handle_t *);
95
96 /* There's at most one obio bus, so we can allocate the bus tag statically */
97 static struct sparc_bus_space_tag obio_space_tag;
98 #endif
99
100 /*
101 * Translate obio `interrupts' property value to processor IPL (see sbus.c)
102 * Apparently, the `interrupts' property on obio devices is just
103 * the processor IPL.
104 */
105 static int intr_obio2ipl[] = {
106 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
107 };
108
109 static int
110 obiomatch(device_t parent, struct cfdata *cf, void *aux)
111 {
112 struct mainbus_attach_args *ma = aux;
113
114 if (obio_attached)
115 return 0;
116
117 return (strcmp(cf->cf_name, ma->ma_name) == 0);
118 }
119
120 static void
121 obioattach(device_t parent, device_t self, void *aux)
122 {
123 struct mainbus_attach_args *ma = aux;
124
125 obio_attached = 1;
126
127 printf("\n");
128
129 if (CPU_ISSUN4) {
130 #if defined(SUN4)
131 union obio_softc *usc = device_private(self);
132 struct obio4_softc *sc = &usc->sc_obio;
133 struct obio4_busattachargs oa;
134 const char *const *cpp;
135 static const char *const special4[] = {
136 /* find these first */
137 "timer",
138 "dma", /* need this before `esp', if any */
139 NULL
140 };
141
142 sc->sc_dev = self;
143 sc->sc_bustag = ma->ma_bustag;
144 sc->sc_dmatag = ma->ma_dmatag;
145
146 memcpy(&obio_space_tag, sc->sc_bustag, sizeof(obio_space_tag));
147 obio_space_tag.cookie = sc;
148 obio_space_tag.parent = sc->sc_bustag;
149 obio_space_tag.sparc_bus_map = _obio_bus_map;
150 obio_space_tag.sparc_bus_mmap = obio_bus_mmap;
151
152 oa.ma = ma;
153
154 /* Find all `early' obio devices */
155 for (cpp = special4; *cpp != NULL; cpp++) {
156 oa.name = *cpp;
157 config_search_ia(obiosearch, self, "obio", &oa);
158 }
159
160 /* Find all other obio devices */
161 oa.name = NULL;
162 config_search_ia(obiosearch, self, "obio", &oa);
163 #endif
164 return;
165 } else if (CPU_ISSUN4M) {
166 /*
167 * Attach the on-board I/O bus at on a sun4m.
168 * In this case we treat the obio bus as another sbus slot.
169 */
170 union obio_softc *usc = device_private(self);
171 struct sbus_softc *sc = &usc->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_dev = self;
187 sc->sc_bustag = ma->ma_bustag;
188 sc->sc_dmatag = ma->ma_dmatag;
189 sc->sc_intr2ipl = intr_obio2ipl;
190
191 sbus_attach_common(sc, "obio", ma->ma_node, special4m);
192 } else {
193 printf("obio on this machine?\n");
194 }
195 }
196
197 #if defined(SUN4)
198 static int
199 obioprint(void *args, const char *busname)
200 {
201 union obio_attach_args *uoba = args;
202 struct obio4_attach_args *oba = &uoba->uoba_oba4;
203
204 aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
205 if (oba->oba_pri != -1)
206 aprint_normal(" level %d", oba->oba_pri);
207
208 return (UNCONF);
209 }
210
211 static int
212 _obio_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
213 vaddr_t va, bus_space_handle_t *hp)
214 {
215
216 if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
217 obio_find_rom_map(ba, size, hp) == 0)
218 return (0);
219
220 return (bus_space_map2(t->parent, ba, size, flags, va, hp));
221 }
222
223 static paddr_t
224 obio_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
225 {
226
227 return (bus_space_mmap(t->parent, ba, off, prot, flags));
228 }
229
230 static int
231 obiosearch(device_t parent, struct cfdata *cf, const int *ldesc,
232 void *aux)
233 {
234 struct obio4_busattachargs *oap = aux;
235 union obio_attach_args uoba;
236 struct obio4_attach_args *oba = &uoba.uoba_oba4;
237 int addr;
238
239 /* Check whether we're looking for a specifically named device */
240 if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
241 return (0);
242
243 /*
244 * Avoid sun4m entries which don't have valid PAs.
245 * no point in even probing them.
246 */
247 addr = cf->cf_loc[OBIOCF_ADDR];
248 if (addr == -1)
249 return (0);
250
251 /*
252 * On the 4/100 obio addresses must be mapped at
253 * 0x0YYYYYYY, but alias higher up (we avoid the
254 * alias condition because it causes pmap difficulties)
255 * XXX: We also assume that 4/[23]00 obio addresses
256 * must be 0xZYYYYYYY, where (Z != 0)
257 */
258 if (cpuinfo.cpu_type == CPUTYP_4_100 && (addr & 0xf0000000))
259 return (0);
260 if (cpuinfo.cpu_type != CPUTYP_4_100 && !(addr & 0xf0000000))
261 return (0);
262
263 uoba.uoba_isobio4 = 1;
264 oba->oba_bustag = &obio_space_tag;
265 oba->oba_dmatag = oap->ma->ma_dmatag;
266 oba->oba_paddr = BUS_ADDR(PMAP_OBIO, addr);
267 oba->oba_pri = cf->cf_loc[OBIOCF_LEVEL];
268
269 if (config_match(parent, cf, &uoba) == 0)
270 return (0);
271
272 config_attach(parent, cf, &uoba, obioprint);
273 return (1);
274 }
275
276
277 /*
278 * If we can find a mapping that was established by the rom, use it.
279 * Else, create a new mapping.
280 */
281 int
282 obio_find_rom_map(bus_addr_t ba, int len, bus_space_handle_t *hp)
283 {
284 #define getpte(va) lda(va, ASI_PTE)
285
286 u_long pa, pf;
287 int pgtype;
288 u_long va, pte;
289
290 if (len > PAGE_SIZE)
291 return (EINVAL);
292
293 pa = BUS_ADDR_PADDR(ba);
294 pf = pa >> PGSHIFT;
295 pgtype = PMAP_T2PTE_4(PMAP_OBIO);
296
297 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
298 pte = getpte(va);
299 if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
300 (pte & PG_PFNUM) != pf)
301 continue;
302
303 /*
304 * Found entry in PROM's pagetable
305 * note: preserve page offset
306 */
307 *hp = (bus_space_handle_t)(va | (pa & PGOFSET));
308 return (0);
309 }
310
311 return (ENOENT);
312 }
313 #endif /* SUN4 */
314