obio.c revision 1.40
1/*	$NetBSD: obio.c,v 1.40 1998/03/21 19:55:31 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
80#if defined(SUN4)
81static	int obioprint  __P((void *, const char *));
82static	int obiosearch   __P((struct device *, struct cfdata *, void *));
83static	int obio_bus_mmap __P((void *, bus_type_t, bus_addr_t, int));
84static	int obio_find_rom_map __P((bus_addr_t, bus_type_t, int,
85				   bus_space_handle_t *));
86#endif
87
88struct cfattach obio_ca = {
89	sizeof(union obio_softc), obiomatch, obioattach
90};
91
92#if defined(SUN4)
93static struct sparc_bus_space_tag obio_space_tag = {
94	NULL,				/* cookie */
95	NULL,				/* bus_space_map */
96	NULL,				/* bus_space_unmap */
97	NULL,				/* bus_space_subregion */
98	NULL,				/* bus_space_barrier */
99	obio_bus_mmap,			/* bus_space_mmap */
100	NULL				/* bus_intr_establish */
101};
102#endif
103
104/*
105 * Translate obio `interrupts' property value to processor IPL (see sbus.c)
106 * Apparently, the `interrupts' property on obio devices is just
107 * the processor IPL.
108 */
109static int intr_obio2ipl[] = {
110	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
111};
112
113int
114obiomatch(parent, cf, aux)
115	struct device *parent;
116	struct cfdata *cf;
117	void *aux;
118{
119	struct mainbus_attach_args *ma = aux;
120
121	return (strcmp(cf->cf_driver->cd_name, ma->ma_name) == 0);
122}
123
124#if defined(SUN4)
125int
126obioprint(args, busname)
127	void *args;
128	const char *busname;
129{
130	union obio_attach_args *uoba = args;
131	struct obio4_attach_args *oba = &uoba->uoba_oba4;
132
133	printf(" addr %p pri %d", oba->oba_paddr, oba->oba_pri);
134
135	return (UNCONF);
136}
137#endif
138
139void
140obioattach(parent, self, aux)
141	struct device *parent, *self;
142	void *aux;
143{
144	struct mainbus_attach_args *ma = aux;
145
146	/*
147	 * There is only one obio bus
148	 */
149	if (self->dv_unit > 0) {
150		printf(" unsupported\n");
151		return;
152	}
153	printf("\n");
154
155	if (CPU_ISSUN4) {
156#if defined(SUN4)
157		/* Propagate interrupt establish function */
158		obio_space_tag.sparc_intr_establish =
159			ma->ma_bustag->sparc_intr_establish;
160
161		(void)config_search(obiosearch, self, aux);
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(sc, "obio", ma->ma_node, ma->ma_bp, special4m);
189	} else {
190		printf("obio on this machine?\n");
191	}
192}
193
194
195int
196obio_bus_probe(tag, addr, offset, size, callback, arg)
197	bus_space_tag_t tag;
198	void	*addr;
199	int	offset;
200	size_t	size;
201	int	(*callback) __P((void *, void *));
202	void	*arg;
203{
204#if defined(SUN4)
205	bus_addr_t paddr = (long)addr + offset;
206	bus_space_handle_t bh;
207	caddr_t tmp;
208	int result;
209
210	if (sparc_bus_map(0, PMAP_OBIO, paddr, size, 0, TMPMAP_VA, &bh) != 0)
211		return (0);
212
213	tmp = (caddr_t)bh;
214	result = probeget(tmp + offset, size) != -1;
215	if (result && callback != NULL)
216		result = (*callback)(tmp, arg);
217	pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
218	return (result);
219#else
220	return (ENXIO);
221#endif /*SUN4*/
222}
223
224int
225obio_bus_map(tag, addr, offset, size, flags, vaddr, hp)
226	bus_space_tag_t tag;
227	void	*addr;
228	int	offset;
229	size_t	size;
230	int	flags;
231	void	*vaddr;
232	bus_space_handle_t *hp;
233{
234#if defined(SUN4)
235	bus_addr_t paddr = (long)addr + offset;
236
237	if ((flags & OBIO_BUS_MAP_USE_ROM) != 0 &&
238	     obio_find_rom_map(paddr, PMAP_OBIO, size, hp) == 0)
239		return (0);
240
241	return (sparc_bus_map(0, PMAP_OBIO, paddr, size, flags,
242			      (vm_offset_t)vaddr, hp));
243#else
244	return (ENXIO);
245#endif /*SUN4*/
246}
247
248#if defined(SUN4)
249int
250obio_bus_mmap(cookie, btype, paddr, flags)
251	void *cookie;
252	bus_type_t btype;
253	bus_addr_t paddr;
254	int flags;
255{
256
257	return (sparc_bus_mmap(0, PMAP_OBIO, paddr, flags));
258}
259
260int
261obiosearch(parent, cf, aux)
262	struct device *parent;
263	struct cfdata *cf;
264	void *aux;
265{
266	struct mainbus_attach_args *ma = aux;
267	union obio_attach_args uoba;
268	struct obio4_attach_args *oba = &uoba.uoba_oba4;
269	struct bootpath *bp;
270
271
272	/*
273	 * Avoid sun4m entries which don't have valid PAs.
274	 * no point in even probing them.
275	 */
276	if (cf->cf_loc[0] == -1)
277		return (0);
278
279	/*
280	 * On the 4/100 obio addresses must be mapped at
281	 * 0x0YYYYYYY, but alias higher up (we avoid the
282	 * alias condition because it causes pmap difficulties)
283	 * XXX: We also assume that 4/[23]00 obio addresses
284	 * must be 0xZYYYYYYY, where (Z != 0)
285	 */
286	if (cpuinfo.cpu_type == CPUTYP_4_100 && (cf->cf_loc[0] & 0xf0000000))
287		return (0);
288	if (cpuinfo.cpu_type != CPUTYP_4_100 && !(cf->cf_loc[0] & 0xf0000000))
289		return (0);
290
291	uoba.uoba_isobio4 = 1;
292	oba->oba_bustag = &obio_space_tag;
293	oba->oba_dmatag = ma->ma_dmatag;
294	oba->oba_paddr = (void *)cf->cf_loc[0];
295	oba->oba_pri = cf->cf_loc[1];
296
297	bp = ma->ma_bp;
298	if (bp != NULL && strcmp(bp->name, "obio") == 0)
299		oba->oba_bp = bp + 1;
300	else
301		oba->oba_bp = NULL;
302
303	if ((*cf->cf_attach->ca_match)(parent, cf, &uoba) == 0)
304		return (0);
305
306	config_attach(parent, cf, &uoba, obioprint);
307	return (1);
308}
309
310
311/*
312 * If we can find a mapping that was established by the rom, use it.
313 * Else, create a new mapping.
314 */
315int
316obio_find_rom_map(pa, iospace, len, hp)
317	bus_addr_t	pa;
318	bus_type_t	iospace;
319	int		len;
320	bus_space_handle_t *hp;
321{
322#define	getpte(va)		lda(va, ASI_PTE)
323
324	u_long	pf;
325	int	pgtype;
326	u_long	va, pte;
327
328	if (len <= NBPG)
329		return (EINVAL);
330
331	pf = pa >> PGSHIFT;
332	pgtype = PMAP_T2PTE_4(iospace);
333
334	for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
335		pte = getpte(va);
336		if ((pte & PG_V) == 0 || (pte & PG_TYPE) != pgtype ||
337		    (pte & PG_PFNUM) != pf)
338			continue;
339
340		/*
341		 * Found entry in PROM's pagetable
342		 * note: preserve page offset
343		 */
344		*hp = (bus_space_handle_t)(va | ((u_long)pa & PGOFSET));
345		return (0);
346	}
347
348	return (ENOENT);
349}
350#endif /* SUN4 */
351