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