obio.c revision 1.23
1/*	$NetBSD: obio.c,v 1.23 1996/04/05 21:50:05 chuck Exp $	*/
2
3/*
4 * Copyright (c) 1993, 1994 Theo de Raadt
5 * Copyright (c) 1995 Paul Kranenburg
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by Theo de Raadt.
19 * 4. The name of the author may not be used to endorse or promote products
20 *    derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/device.h>
37#include <sys/malloc.h>
38
39#ifdef DEBUG
40#include <sys/proc.h>
41#include <sys/syslog.h>
42#endif
43
44#include <vm/vm.h>
45
46#include <machine/autoconf.h>
47#include <machine/pmap.h>
48#include <machine/oldmon.h>
49#include <machine/cpu.h>
50#include <machine/ctlreg.h>
51#include <sparc/sparc/asm.h>
52#include <sparc/sparc/vaddrs.h>
53#include <sparc/dev/sbusvar.h>
54
55struct bus_softc {
56	union {
57		struct	device scu_dev;		/* base device */
58		struct	sbus_softc scu_sbus;	/* obio is another sbus slot */
59	} bu;
60#define sc_dev	bu.scu_dev
61};
62
63/* autoconfiguration driver */
64static int	busmatch __P((struct device *, void *, void *));
65static void	obioattach __P((struct device *, struct device *, void *));
66static void	vmesattach __P((struct device *, struct device *, void *));
67static void	vmelattach __P((struct device *, struct device *, void *));
68
69int		busprint __P((void *, char *));
70static int	busattach __P((struct device *, void *, void *, int));
71void *		bus_map __P((struct rom_reg *, int, int));
72int		obio_scan __P((struct device *, void *, void *));
73int 		vmes_scan __P((struct device *, void *, void *));
74int 		vmel_scan __P((struct device *, void *, void *));
75int 		vmeintr __P((void *));
76
77struct cfattach obio_ca = {
78	sizeof(struct bus_softc), busmatch, obioattach
79};
80
81struct cfdriver obio_cd = {
82	NULL, "obio", DV_DULL
83};
84
85struct cfattach vmel_ca = {
86	sizeof(struct bus_softc), busmatch, vmelattach
87};
88
89struct cfdriver vmel_cd = {
90	NULL, "vmel", DV_DULL
91};
92
93struct cfattach vmes_ca = {
94	sizeof(struct bus_softc), busmatch, vmesattach
95};
96
97struct cfdriver vmes_cd = {
98	NULL, "vmes", DV_DULL
99};
100
101struct intrhand **vmeints;
102
103
104int
105busmatch(parent, vcf, aux)
106	struct device *parent;
107	void *vcf, *aux;
108{
109	struct cfdata *cf = vcf;
110	register struct confargs *ca = aux;
111	register struct romaux *ra = &ca->ca_ra;
112
113	if (CPU_ISSUN4M)
114		return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
115
116	if (!CPU_ISSUN4)
117		return (0);
118
119	return (strcmp(cf->cf_driver->cd_name, ra->ra_name) == 0);
120}
121
122int
123busprint(args, obio)
124	void *args;
125	char *obio;
126{
127	register struct confargs *ca = args;
128
129	if (ca->ca_ra.ra_name == NULL)
130		ca->ca_ra.ra_name = "<unknown>";
131
132	if (obio)
133		printf("[%s at %s]", ca->ca_ra.ra_name, obio);
134
135	printf(" addr %p", ca->ca_ra.ra_paddr);
136
137	if (CPU_ISSUN4 && ca->ca_ra.ra_intr[0].int_vec != -1)
138		printf(" vec 0x%x", ca->ca_ra.ra_intr[0].int_vec);
139
140	return (UNCONF);
141}
142
143
144void
145obioattach(parent, self, args)
146	struct device *parent, *self;
147	void *args;
148{
149#if defined(SUN4M)
150	register struct bus_softc *sc = (struct bus_softc *)self;
151	struct confargs oca, *ca = args;
152	register struct romaux *ra = &ca->ca_ra;
153	register int node0, node;
154	register char *name;
155	register const char *sp;
156	const char *const *ssp;
157	extern int autoconf_nzs;
158
159	static const char *const special4m[] = {
160		/* find these first */
161		"eeprom",
162		"counter",
163		"auxio",
164		"",
165		/* place device to ignore here */
166		"interrupt",
167		NULL
168	};
169#endif
170
171	if (CPU_ISSUN4) {
172		if (self->dv_unit > 0) {
173			printf(" unsupported\n");
174			return;
175		}
176		printf("\n");
177
178		(void)config_search(obio_scan, self, args);
179		bus_untmp();
180	}
181
182#if defined(SUN4M)
183	if (!CPU_ISSUN4M)
184		return;
185
186	/*
187	 * There is only one obio bus (it is in fact one of the Sbus slots)
188	 * How about VME?
189	 */
190	if (sc->sc_dev.dv_unit > 0) {
191		printf(" unsupported\n");
192		return;
193	}
194
195	printf("\n");
196
197	if (ra->ra_bp != NULL && strcmp(ra->ra_bp->name, "obio") == 0)
198		oca.ca_ra.ra_bp = ra->ra_bp + 1;
199	else
200		oca.ca_ra.ra_bp = NULL;
201
202	sc->bu.scu_sbus.sc_range = ra->ra_range;
203	sc->bu.scu_sbus.sc_nrange = ra->ra_nrange;
204
205	/*
206	 * Loop through ROM children, fixing any relative addresses
207	 * and then configuring each device.
208	 * We first do the crucial ones, such as eeprom, etc.
209	 */
210	node0 = firstchild(ra->ra_node);
211	for (ssp = special4m ; *(sp = *ssp) != 0; ssp++) {
212		if ((node = findnode(node0, sp)) == 0) {
213			printf("could not find %s amongst obio devices\n", sp);
214			panic(sp);
215		}
216		if (!romprop(&oca.ca_ra, sp, node))
217			continue;
218
219		sbus_translate(self, &oca);
220		oca.ca_bustype = BUS_OBIO;
221		(void) config_found(&sc->sc_dev, (void *)&oca, busprint);
222	}
223
224	for (node = node0; node; node = nextsibling(node)) {
225		name = getpropstring(node, "name");
226		for (ssp = special4m ; (sp = *ssp) != NULL; ssp++)
227			if (strcmp(name, sp) == 0)
228				break;
229
230		if (sp != NULL || !romprop(&oca.ca_ra, name, node))
231			continue;
232
233		if (strcmp(name, "zs") == 0)
234			/* XXX - see autoconf.c for this hack */
235			autoconf_nzs++;
236
237		/* Translate into parent address spaces */
238		sbus_translate(self, &oca);
239		oca.ca_bustype = BUS_OBIO;
240		(void) config_found(&sc->sc_dev, (void *)&oca, busprint);
241	}
242#endif
243}
244
245void
246vmesattach(parent, self, args)
247	struct device *parent, *self;
248	void *args;
249{
250	if (CPU_ISSUN4M || self->dv_unit > 0) {
251		printf(" unsupported\n");
252		return;
253	}
254	printf("\n");
255
256	if (vmeints == NULL) {
257		vmeints = (struct intrhand **)malloc(256 *
258		    sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
259		bzero(vmeints, 256 * sizeof(struct intrhand *));
260	}
261	(void)config_search(vmes_scan, self, args);
262	bus_untmp();
263}
264
265void
266vmelattach(parent, self, args)
267	struct device *parent, *self;
268	void *args;
269{
270	if (CPU_ISSUN4M || self->dv_unit > 0) {
271		printf(" unsupported\n");
272		return;
273	}
274	printf("\n");
275
276	if (vmeints == NULL) {
277		vmeints = (struct intrhand **)malloc(256 *
278		    sizeof(struct intrhand *), M_TEMP, M_NOWAIT);
279		bzero(vmeints, 256 * sizeof(struct intrhand *));
280	}
281	(void)config_search(vmel_scan, self, args);
282	bus_untmp();
283}
284
285int
286busattach(parent, child, args, bustype)
287	struct device *parent;
288	void *args, *child;
289	int bustype;
290{
291#if defined(SUN4)
292	struct cfdata *cf = child;
293	register struct confargs *ca = args;
294	struct confargs oca;
295	caddr_t tmp;
296
297	if (bustype == BUS_OBIO && CPU_ISSUN4) {
298
299		/*
300		 * avoid sun4m entries which don't have valid PA's.
301		 * no point in even probing them.
302		 */
303		if (cf->cf_loc[0] == -1) return 0;
304
305		/*
306		 * On the 4/100 obio addresses must be mapped at
307		 * 0x0YYYYYYY, but alias higher up (we avoid the
308		 * alias condition because it causes pmap difficulties)
309		 * XXX: We also assume that 4/[23]00 obio addresses
310		 * must be 0xZYYYYYYY, where (Z != 0)
311		 */
312		if (cpumod == SUN4_100 && (cf->cf_loc[0] & 0xf0000000))
313			return 0;
314		if (cpumod != SUN4_100 && !(cf->cf_loc[0] & 0xf0000000))
315			return 0;
316	}
317
318	if (parent->dv_cfdata->cf_driver->cd_indirect) {
319		printf(" indirect devices not supported\n");
320		return 0;
321	}
322
323	oca.ca_ra.ra_iospace = -1;
324	oca.ca_ra.ra_paddr = (void *)cf->cf_loc[0];
325	oca.ca_ra.ra_len = 0;
326	oca.ca_ra.ra_nreg = 1;
327	tmp = NULL;
328	if (oca.ca_ra.ra_paddr)
329		tmp = bus_tmp(oca.ca_ra.ra_paddr,
330		    bustype);
331	oca.ca_ra.ra_vaddr = tmp;
332	oca.ca_ra.ra_intr[0].int_pri = cf->cf_loc[1];
333	if (bustype == BUS_VME16 || bustype == BUS_VME32)
334		oca.ca_ra.ra_intr[0].int_vec = cf->cf_loc[2];
335	else
336		oca.ca_ra.ra_intr[0].int_vec = -1;
337	oca.ca_ra.ra_nintr = 1;
338	oca.ca_ra.ra_name = cf->cf_driver->cd_name;
339	if (ca->ca_ra.ra_bp != NULL &&
340	  ((bustype == BUS_VME16 && strcmp(ca->ca_ra.ra_bp->name,"vmes") ==0) ||
341	   (bustype == BUS_VME32 && strcmp(ca->ca_ra.ra_bp->name,"vmel") ==0) ||
342	   (bustype == BUS_OBIO && strcmp(ca->ca_ra.ra_bp->name,"obio") == 0)))
343		oca.ca_ra.ra_bp = ca->ca_ra.ra_bp + 1;
344	else
345		oca.ca_ra.ra_bp = NULL;
346	oca.ca_bustype = bustype;
347
348	if ((*cf->cf_attach->ca_match)(parent, cf, &oca) == 0)
349		return 0;
350
351	/*
352	 * check if XXmatch routine replaced the temporary mapping with
353	 * a real mapping.   If not, then make sure we don't pass the
354	 * tmp mapping to the attach routine.
355	 */
356	if (oca.ca_ra.ra_vaddr == tmp)
357		oca.ca_ra.ra_vaddr = NULL; /* wipe out tmp address */
358	/*
359	 * the match routine will set "ra_len" if it wants us to
360	 * establish a mapping for it.
361	 * (which won't be seen on future XXmatch calls,
362	 * so not as useful as it seems.)
363	 */
364	if (oca.ca_ra.ra_len)
365		oca.ca_ra.ra_vaddr =
366		    bus_map(oca.ca_ra.ra_reg,
367		    oca.ca_ra.ra_len, oca.ca_bustype);
368
369	config_attach(parent, cf, &oca, busprint);
370	return 1;
371#else
372	return 0;
373#endif
374}
375
376int
377obio_scan(parent, child, args)
378	struct device *parent;
379	void *child, *args;
380{
381	return busattach(parent, child, args, BUS_OBIO);
382}
383
384int
385vmes_scan(parent, child, args)
386	struct device *parent;
387	void *child, *args;
388{
389	return busattach(parent, child, args, BUS_VME16);
390}
391
392int
393vmel_scan(parent, child, args)
394	struct device *parent;
395	void *child, *args;
396{
397	return busattach(parent, child, args, BUS_VME32);
398}
399
400int pil_to_vme[] = {
401	-1,	/* pil 0 */
402	-1,	/* pil 1 */
403	1,	/* pil 2 */
404	2,	/* pil 3 */
405	-1,	/* pil 4 */
406	3,	/* pil 5 */
407	-1,	/* pil 6 */
408	4,	/* pil 7 */
409	-1,	/* pil 8 */
410	5,	/* pil 9 */
411	-1,	/* pil 10 */
412	6,	/* pil 11 */
413	-1,	/* pil 12 */
414	7,	/* pil 13 */
415	-1,	/* pil 14 */
416	-1,	/* pil 15 */
417};
418
419int
420vmeintr(arg)
421	void *arg;
422{
423	int level = (int)arg, vec;
424	struct intrhand *ih;
425	int i = 0;
426
427#ifdef DIAGNOSTIC
428	if (!CPU_ISSUN4) {
429		panic("vme: spurious interrupt");
430	}
431#endif
432
433	vec = ldcontrolb((caddr_t)
434	    (AC_VMEINTVEC | (pil_to_vme[level] << 1) | 1));
435	if (vec == -1) {
436		printf("vme: spurious interrupt\n");
437		return 0;
438	}
439
440	for (ih = vmeints[vec]; ih; ih = ih->ih_next)
441		if (ih->ih_fun)
442			i += (ih->ih_fun)(ih->ih_arg);
443	return (i);
444}
445
446void
447vmeintr_establish(vec, level, ih)
448	int vec, level;
449	struct intrhand *ih;
450{
451	struct intrhand *ihs;
452
453	if (!CPU_ISSUN4) {
454		panic("vmeintr_establish: not supported on cpu-type %d",
455		      cputyp);
456	}
457
458	if (vec == -1)
459		panic("vmeintr_establish: uninitialized vec\n");
460
461	if (vmeints[vec] == NULL)
462		vmeints[vec] = ih;
463	else {
464		for (ihs = vmeints[vec]; ihs->ih_next; ihs = ihs->ih_next)
465			;
466		ihs->ih_next = ih;
467	}
468
469	/* ensure the interrupt subsystem will call us at this level */
470	for (ihs = intrhand[level]; ihs; ihs = ihs->ih_next)
471		if (ihs->ih_fun == vmeintr)
472			return;
473
474	ihs = (struct intrhand *)malloc(sizeof(struct intrhand),
475	    M_TEMP, M_NOWAIT);
476	if (ihs == NULL)
477		panic("vme_addirq");
478	bzero(ihs, sizeof *ihs);
479	ihs->ih_fun = vmeintr;
480	ihs->ih_arg = (void *)level;
481	intr_establish(level, ihs);
482}
483
484#define	getpte(va)		lda(va, ASI_PTE)
485
486/*
487 * If we can find a mapping that was established by the rom, use it.
488 * Else, create a new mapping.
489 */
490void *
491bus_map(pa, len, bustype)
492	struct rom_reg *pa;
493	int len;
494	int bustype;
495{
496	u_long	pf = (u_long)(pa->rr_paddr) >> PGSHIFT;
497	u_long	va, pte;
498	int pgtype = -1;
499
500	switch (bt2pmt[bustype]) {
501	case PMAP_OBIO:
502		pgtype = PG_OBIO;
503		break;
504	case PMAP_VME32:
505		pgtype = PG_VME32;
506		break;
507	case PMAP_VME16:
508		pgtype = PG_VME16;
509		break;
510	}
511
512	if (len <= NBPG) {
513		for (va = OLDMON_STARTVADDR; va < OLDMON_ENDVADDR; va += NBPG) {
514			pte = getpte(va);
515			if ((pte & PG_V) != 0 && (pte & PG_TYPE) == pgtype &&
516			    (pte & PG_PFNUM) == pf)
517				return ((void *)
518				    (va | ((u_long)pa->rr_paddr & PGOFSET)) );
519					/* note: preserve page offset */
520		}
521	}
522	return mapiodev(pa, 0, len, bustype);
523}
524
525void *
526bus_tmp(pa, bustype)
527	void *pa;
528	int bustype;
529{
530	vm_offset_t addr = (vm_offset_t)pa & ~PGOFSET;
531	int pmtype = bt2pmt[bustype];
532
533	pmap_enter(pmap_kernel(), TMPMAP_VA,
534		   addr | pmtype | PMAP_NC,
535		   VM_PROT_READ | VM_PROT_WRITE, 1);
536	return ((void *)(TMPMAP_VA | ((u_long) pa & PGOFSET)) );
537}
538
539void
540bus_untmp()
541{
542	pmap_remove(pmap_kernel(), TMPMAP_VA, TMPMAP_VA+NBPG);
543}
544