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