obio.c revision 1.7
1/*	$NetBSD: obio.c,v 1.7 1997/10/04 19:48:36 gwr Exp $	*/
2
3/*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Adam Glass and Gordon W. Ross.
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/param.h>
40#include <sys/systm.h>
41#include <sys/device.h>
42
43#include <machine/autoconf.h>
44#include <machine/machdep.h>
45#include <machine/mon.h>
46#include <machine/obio.h>
47#include <machine/pte.h>
48
49short *enable_reg;
50
51static int  obio_match __P((struct device *, struct cfdata *, void *));
52static void obio_attach __P((struct device *, struct device *, void *));
53static int  obio_print __P((void *, const char *parentname));
54static int	obio_submatch __P((struct device *, struct cfdata *, void *));
55
56struct cfattach obio_ca = {
57	sizeof(struct device), obio_match, obio_attach
58};
59
60struct cfdriver obio_cd = {
61	NULL, "obio", DV_DULL
62};
63
64static int
65obio_match(parent, cf, aux)
66	struct device *parent;
67	struct cfdata *cf;
68	void *aux;
69{
70	struct confargs *ca = aux;
71
72	if (ca->ca_bustype != BUS_OBIO)
73		return (0);
74	return(1);
75}
76
77/*
78 * We need some control over the order of attachment on OBIO,
79 * and all OBIO device addresses are known and fixed foerver.
80 * Therefore, this uses a list of addresses to attach.
81 * XXX - Any other way to control search/attach order?
82 *
83 * Warning: This whole list is very carefully ordered!
84 * In general, anything not already shown here should
85 * be added at or near the end.
86 */
87static int obio_alist[] = {
88
89	/* Misc. registers - needed by many things */
90	OBIO_ENABLEREG,
91	OBIO_BUSERRREG,
92	OBIO_DIAGREG,	/* leds.c */
93	OBIO_IDPROM1,	/* idprom.c (3/470) */
94	OBIO_MEMREG,	/* memerr.c */
95	OBIO_INTERREG,	/* intreg.c */
96
97	/* Zilog UARTs */
98	OBIO_ZS_KBD_MS,
99	OBIO_ZS_TTY_AB,
100
101	/* eeprom.c */
102	OBIO_EEPROM,
103
104	/* Note: This must come after OBIO_IDPROM1. */
105	OBIO_IDPROM2,	/* idprom.c (3/80) */
106
107	/* Note: Must probe for the Intersil first! */
108	OBIO_CLOCK1,	/* clock.c (3/470) */
109	OBIO_CLOCK2,	/* clock.c (3/80) */
110
111	/* This is used by the Ethernet and SCSI drivers. */
112	OBIO_IOMMU,
113
114	OBIO_INTEL_ETHER,
115	OBIO_LANCE_ETHER,
116
117	OBIO_EMULEX_SCSI, /* 3/80 only */
118
119	/* ...todo... */
120	OBIO_FDC,
121	OBIO_PRINTER_PORT,
122};
123#define OBIO_ALIST_LEN (sizeof(obio_alist) / \
124                        sizeof(obio_alist[0]))
125
126static void
127obio_attach(parent, self, aux)
128	struct device *parent;
129	struct device *self;
130	void *aux;
131{
132	struct confargs *ca = aux;
133	int	i;
134
135	printf("\n");
136
137	/* Configure these in the order listed above. */
138	for (i = 0; i < OBIO_ALIST_LEN; i++) {
139		/* Our parent set ca->ca_bustype already. */
140		ca->ca_paddr = obio_alist[i];
141		/* These are filled-in by obio_submatch. */
142		ca->ca_intpri = -1;
143		ca->ca_intvec = -1;
144		(void) config_found_sm(self, ca, obio_print, obio_submatch);
145	}
146}
147
148/*
149 * Print out the confargs.  The (parent) name is non-NULL
150 * when there was no match found by config_found().
151 */
152static int
153obio_print(args, name)
154	void *args;
155	const char *name;
156{
157
158	/* Be quiet about empty OBIO locations. */
159	if (name)
160		return(QUIET);
161
162	/* Otherwise do the usual. */
163	return(bus_print(args, name));
164}
165
166int
167obio_submatch(parent, cf, aux)
168	struct device *parent;
169	struct cfdata *cf;
170	void *aux;
171{
172	struct confargs *ca = aux;
173	cfmatch_t submatch;
174
175	/*
176	 * Note that a defaulted address locator can never match
177	 * the value of ca->ca_paddr set by the obio_attach loop.
178	 * Without this diagnostic, any device with a defaulted
179	 * address locator would always be silently unmatched.
180	 * Therefore, just disallow default addresses on OBIO.
181	 */
182#ifdef	DIAGNOSTIC
183	if (cf->cf_paddr == -1)
184		panic("obio_submatch: invalid address for: %s%d\n",
185			cf->cf_driver->cd_name, cf->cf_unit);
186#endif
187
188	/*
189	 * Note that obio_attach calls config_found_sm() with
190	 * this function as the "submatch" and ca->ca_paddr
191	 * set to each of the possible OBIO locations, so we
192	 * want to reject any unmatched address here.
193	 */
194	if (cf->cf_paddr != ca->ca_paddr)
195		return 0;
196
197	/*
198	 * Copy the locators into our confargs for the child.
199	 * Note: ca->ca_bustype was set by our parent driver
200	 * (mainbus) and ca->ca_paddr was set by obio_attach.
201	 */
202	ca->ca_intpri = cf->cf_intpri;
203	ca->ca_intvec = cf->cf_intvec;
204
205	/* Now call the match function of the potential child. */
206	submatch = cf->cf_attach->ca_match;
207	if (submatch == NULL)
208		panic("obio_submatch: no match function for: %s\n",
209			  cf->cf_driver->cd_name);
210
211	return ((*submatch)(parent, cf, aux));
212}
213
214
215/*****************************************************************/
216
217/*
218 * This is our record of "interesting" OBIO mappings that
219 * the PROM has left in the virtual space reserved for it.
220 * Each non-null array element holds the virtual address
221 * of an OBIO mapping where the OBIO address mapped is:
222 *     (array_index * SAVE_INCR)
223 * and the length of the mapping is one page.
224 */
225static struct prom_map {
226	vm_offset_t pa, va;
227} prom_mappings[] = {
228	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
229	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
230	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
231	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
232};
233#define PROM_MAP_CNT (sizeof(prom_mappings) / \
234		      sizeof(prom_mappings[0]))
235
236/*
237 * Find a virtual address for a device at physical address 'pa'.
238 * If one is found among the mappings already made by the PROM
239 * at power-up time, use it.  Otherwise return 0 as a sign that
240 * a mapping will have to be created.
241 */
242caddr_t
243obio_find_mapping(int pa, int size)
244{
245	int i, off;
246
247	if (size >= NBPG)
248		return (caddr_t)0;
249
250	off = pa & PGOFSET;
251	pa -= off;
252
253	for (i = 0; i < PROM_MAP_CNT; i++) {
254		if (pa == prom_mappings[i].pa) {
255			return ((caddr_t)(prom_mappings[i].va + off));
256		}
257	}
258	return (caddr_t)0;
259}
260
261/*
262 * Search the PROM page tables for OBIO mappings that
263 * we would like to borrow.
264 */
265static void
266save_prom_mappings __P((void))
267{
268	int *mon_pte;
269	vm_offset_t va, pa;
270	int i;
271
272	/* Note: mon_ctbl[0] maps MON_KDB_START */
273	mon_pte = *romVectorPtr->monptaddr;
274
275	for (va = MON_KDB_START; va < MONEND;
276		 va += NBPG, mon_pte++)
277	{
278		/* Is this a valid mapping to OBIO? */
279		/* XXX - Some macros would be nice... */
280		if ((*mon_pte & 0xF0000003) != 0x60000001)
281			continue;
282
283		/* Yes it is.  Is this a mapping we want? */
284		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
285		for (i = 0; i < PROM_MAP_CNT; i++) {
286			if (pa != prom_mappings[i].pa)
287				continue;
288			/* Yes, we want it.  Save the va? */
289			if (prom_mappings[i].va == 0) {
290				prom_mappings[i].va = va;
291			}
292		}
293	}
294
295}
296
297/*
298 * These are all the OBIO address that are required early in
299 * the life of the kernel.  All are less than one page long.
300 * This function should make any required mappings that we
301 * were not able to find among the PROM monitor's mappings.
302 */
303static void
304make_required_mappings __P((void))
305{
306	int i;
307
308	for (i = 0; i < PROM_MAP_CNT; i++) {
309		if (prom_mappings[i].va == 0) {
310			/*
311			 * Actually, the PROM always has all the
312			 * "required" mappings we need, (smile)
313			 * but this makes sure that is true.
314			 */
315			mon_printf("obio: no mapping for pa=0x%x\n",
316			    prom_mappings[i].pa);
317			sunmon_abort();  /* Ancient PROM? */
318		}
319	}
320}
321
322
323/*
324 * Find mappings for devices that are needed before autoconfiguration.
325 * We first look for and record any useful PROM mappings, then call
326 * the "init" functions for drivers that we need to use before the
327 * normal autoconfiguration calls configure().  Warning: this is
328 * called before pmap_bootstrap, so no allocation allowed!
329 */
330void
331obio_init()
332{
333	save_prom_mappings();
334	make_required_mappings();
335
336	enable_reg = (short*) obio_find_mapping(OBIO_ENABLEREG, 2);
337
338	/*
339	 * Find the interrupt reg mapping and turn off the
340	 * interrupts, otherwise the PROM clock interrupt
341	 * would poll the zs and toggle some LEDs...
342	 */
343	intreg_init();
344
345	/* Turn on the LEDs so we know power is on. */
346	leds_init();
347
348	/* Make the zs driver ready for console duty. */
349	zs_init();
350}
351
352/*
353 * This function is used by some OBIO drivers to conserve
354 * kernel virtual space by sharing mappings made by the
355 * PROM monitor.  If we could not find any mapping made by
356 * the PROM monitor, then make our own as usual.
357 */
358caddr_t
359obio_mapin(obio_addr, obio_size)
360	int obio_addr, obio_size;
361{
362	caddr_t cp;
363
364	cp = obio_find_mapping((vm_offset_t)obio_addr, obio_size);
365	if (cp)
366		return (cp);
367
368	cp = bus_mapin(BUS_OBIO, obio_addr, obio_size);
369	return (cp);
370}
371