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