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