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