obio.c revision 1.70 1 /* $NetBSD: obio.c,v 1.70 2008/04/28 20:23:35 martin 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.70 2008/04/28 20:23:35 martin 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 <machine/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 struct device 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 device sc_dev; /* base device */
67 struct obio4_softc sc_obio; /* sun4 obio */
68 struct sbus_softc sc_sbus; /* sun4m obio is another sbus slot */
69 };
70
71
72 /* autoconfiguration driver */
73 static int obiomatch(struct device *, struct cfdata *, void *);
74 static void obioattach(struct device *, struct device *, void *);
75
76 CFATTACH_DECL(obio, sizeof(union obio_softc),
77 obiomatch, obioattach, NULL, NULL);
78
79 static int obio_attached;
80
81 /*
82 * This `obio4_busattachargs' data structure only exists to pass down
83 * to obiosearch() the name of a device that must be configured early.
84 */
85 struct obio4_busattachargs {
86 struct mainbus_attach_args *ma;
87 const char *name;
88 };
89
90 #if defined(SUN4)
91 static int obioprint(void *, const char *);
92 static int obiosearch(struct device *, struct cfdata *, const int *, void *);
93 static paddr_t obio_bus_mmap(bus_space_tag_t, bus_addr_t, off_t, int, int);
94 static int _obio_bus_map(bus_space_tag_t, bus_addr_t, bus_size_t, int,
95 vaddr_t, bus_space_handle_t *);
96
97 /* There's at most one obio bus, so we can allocate the bus tag statically */
98 static struct sparc_bus_space_tag obio_space_tag;
99 #endif
100
101 /*
102 * Translate obio `interrupts' property value to processor IPL (see sbus.c)
103 * Apparently, the `interrupts' property on obio devices is just
104 * the processor IPL.
105 */
106 static int intr_obio2ipl[] = {
107 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
108 };
109
110 static int
111 obiomatch(struct device *parent, struct cfdata *cf, void *aux)
112 {
113 struct mainbus_attach_args *ma = aux;
114
115 if (obio_attached)
116 return 0;
117
118 return (strcmp(cf->cf_name, ma->ma_name) == 0);
119 }
120
121 static void
122 obioattach(struct device *parent, struct device *self, void *aux)
123 {
124 struct mainbus_attach_args *ma = aux;
125
126 obio_attached = 1;
127
128 printf("\n");
129
130 if (CPU_ISSUN4) {
131 #if defined(SUN4)
132 struct obio4_softc *sc = &((union obio_softc *)self)->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_bustag = ma->ma_bustag;
143 sc->sc_dmatag = ma->ma_dmatag;
144
145 memcpy(&obio_space_tag, sc->sc_bustag, sizeof(obio_space_tag));
146 obio_space_tag.cookie = sc;
147 obio_space_tag.parent = sc->sc_bustag;
148 obio_space_tag.sparc_bus_map = _obio_bus_map;
149 obio_space_tag.sparc_bus_mmap = obio_bus_mmap;
150
151 oa.ma = ma;
152
153 /* Find all `early' obio devices */
154 for (cpp = special4; *cpp != NULL; cpp++) {
155 oa.name = *cpp;
156 config_search_ia(obiosearch, self, "obio", &oa);
157 }
158
159 /* Find all other obio devices */
160 oa.name = NULL;
161 config_search_ia(obiosearch, self, "obio", &oa);
162 #endif
163 return;
164 } else if (CPU_ISSUN4M) {
165 /*
166 * Attach the on-board I/O bus at on a sun4m.
167 * In this case we treat the obio bus as another sbus slot.
168 */
169 struct sbus_softc *sc = &((union obio_softc *)self)->sc_sbus;
170
171 static const char *const special4m[] = {
172 /* find these first */
173 "eeprom",
174 "counter",
175 #if 0 /* Not all sun4m's have an `auxio' */
176 "auxio",
177 #endif
178 "",
179 /* place device to ignore here */
180 "interrupt",
181 NULL
182 };
183
184 sc->sc_bustag = ma->ma_bustag;
185 sc->sc_dmatag = ma->ma_dmatag;
186 sc->sc_intr2ipl = intr_obio2ipl;
187
188 sbus_attach_common(sc, "obio", ma->ma_node, special4m);
189 } else {
190 printf("obio on this machine?\n");
191 }
192 }
193
194 #if defined(SUN4)
195 static int
196 obioprint(void *args, const char *busname)
197 {
198 union obio_attach_args *uoba = args;
199 struct obio4_attach_args *oba = &uoba->uoba_oba4;
200
201 aprint_normal(" addr 0x%lx", (u_long)BUS_ADDR_PADDR(oba->oba_paddr));
202 if (oba->oba_pri != -1)
203 aprint_normal(" level %d", oba->oba_pri);
204
205 return (UNCONF);
206 }
207
208 static int
209 _obio_bus_map(bus_space_tag_t t, bus_addr_t ba, bus_size_t size, int flags,
210 vaddr_t va, bus_space_handle_t *hp)
211 {
212
213 if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
214 obio_find_rom_map(ba, size, hp) == 0)
215 return (0);
216
217 return (bus_space_map2(t->parent, ba, size, flags, va, hp));
218 }
219
220 static paddr_t
221 obio_bus_mmap(bus_space_tag_t t, bus_addr_t ba, off_t off, int prot, int flags)
222 {
223
224 return (bus_space_mmap(t->parent, ba, off, prot, flags));
225 }
226
227 static int
228 obiosearch(struct device *parent, struct cfdata *cf, const int *ldesc,
229 void *aux)
230 {
231 struct obio4_busattachargs *oap = aux;
232 union obio_attach_args uoba;
233 struct obio4_attach_args *oba = &uoba.uoba_oba4;
234 int addr;
235
236 /* Check whether we're looking for a specifically named device */
237 if (oap->name != NULL && strcmp(oap->name, cf->cf_name) != 0)
238 return (0);
239
240 /*
241 * Avoid sun4m entries which don't have valid PAs.
242 * no point in even probing them.
243 */
244 addr = cf->cf_loc[OBIOCF_ADDR];
245 if (addr == -1)
246 return (0);
247
248 /*
249 * On the 4/100 obio addresses must be mapped at
250 * 0x0YYYYYYY, but alias higher up (we avoid the
251 * alias condition because it causes pmap difficulties)
252 * XXX: We also assume that 4/[23]00 obio addresses
253 * must be 0xZYYYYYYY, where (Z != 0)
254 */
255 if (cpuinfo.cpu_type == CPUTYP_4_100 && (addr & 0xf0000000))
256 return (0);
257 if (cpuinfo.cpu_type != CPUTYP_4_100 && !(addr & 0xf0000000))
258 return (0);
259
260 uoba.uoba_isobio4 = 1;
261 oba->oba_bustag = &obio_space_tag;
262 oba->oba_dmatag = oap->ma->ma_dmatag;
263 oba->oba_paddr = BUS_ADDR(PMAP_OBIO, addr);
264 oba->oba_pri = cf->cf_loc[OBIOCF_LEVEL];
265
266 if (config_match(parent, cf, &uoba) == 0)
267 return (0);
268
269 config_attach(parent, cf, &uoba, obioprint);
270 return (1);
271 }
272
273
274 /*
275 * If we can find a mapping that was established by the rom, use it.
276 * Else, create a new mapping.
277 */
278 int
279 obio_find_rom_map(bus_addr_t ba, int len, bus_space_handle_t *hp)
280 {
281 #define getpte(va) lda(va, ASI_PTE)
282
283 u_long pa, pf;
284 int pgtype;
285 u_long va, pte;
286
287 if (len > PAGE_SIZE)
288 return (EINVAL);
289
290 pa = BUS_ADDR_PADDR(ba);
291 pf = pa >> PGSHIFT;
292 pgtype = PMAP_T2PTE_4(PMAP_OBIO);
293
294 for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += PAGE_SIZE) {
295 pte = getpte(va);
296 if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
297 (pte & PG_PFNUM) != pf)
298 continue;
299
300 /*
301 * Found entry in PROM's pagetable
302 * note: preserve page offset
303 */
304 *hp = (bus_space_handle_t)(va | (pa & PGOFSET));
305 return (0);
306 }
307
308 return (ENOENT);
309 }
310 #endif /* SUN4 */
311