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