obio.c revision 1.13
1/*	$NetBSD: obio.c,v 1.13 2001/09/05 12:37:25 tsutsui 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/mon.h>
45#include <machine/pte.h>
46
47#include <sun3/sun3/machdep.h>
48#include <sun3/sun3x/obio.h>
49
50static int  obio_match __P((struct device *, struct cfdata *, void *));
51static void obio_attach __P((struct device *, struct device *, void *));
52static int  obio_print __P((void *, const char *parentname));
53static int	obio_submatch __P((struct device *, struct cfdata *, void *));
54
55struct cfattach obio_ca = {
56	sizeof(struct device), obio_match, obio_attach
57};
58
59static int
60obio_match(parent, cf, aux)
61	struct device *parent;
62	struct cfdata *cf;
63	void *aux;
64{
65	struct confargs *ca = aux;
66
67	if (ca->ca_bustype != BUS_OBIO)
68		return (0);
69	return(1);
70}
71
72/*
73 * We need control over the order of attachment on OBIO,
74 * so do "direct" style autoconfiguration with addresses
75 * from the list below.  OBIO addresses are fixed forever.
76 *
77 * Warning: This whole list is very carefully ordered!
78 * In general, anything not already shown here should
79 * be added at or near the end.
80 */
81static int obio_alist[] = {
82
83	/* This is used by the Ethernet and SCSI drivers. */
84	OBIO_IOMMU,
85
86	/* Misc. registers - needed by many things */
87	OBIO_ENABLEREG,
88	OBIO_BUSERRREG,
89	OBIO_DIAGREG,	/* leds.c */
90	OBIO_IDPROM1,	/* idprom.c (3/470) */
91	OBIO_MEMREG,	/* memerr.c */
92	OBIO_INTERREG,	/* intreg.c */
93
94	/* Zilog UARTs */
95	OBIO_ZS_KBD_MS,
96	OBIO_ZS_TTY_AB,
97
98	/* eeprom.c */
99	OBIO_EEPROM,
100
101	/* Note: This must come after OBIO_IDPROM1. */
102	OBIO_IDPROM2,	/* idprom.c (3/80) */
103
104	/* Note: Must probe for the Intersil first! */
105	OBIO_CLOCK1,	/* clock.c (3/470) */
106	OBIO_CLOCK2,	/* clock.c (3/80) */
107
108	OBIO_INTEL_ETHER,
109	OBIO_LANCE_ETHER,
110
111	/* Need esp DMA before SCSI. */
112	OBIO_EMULEX_DMA,  /* 3/80 only */
113	OBIO_EMULEX_SCSI, /* 3/80 only */
114
115	/* Memory subsystem */
116	OBIO_PCACHE_TAGS,
117	OBIO_ECCPARREG,
118	OBIO_IOC_TAGS,
119	OBIO_IOC_FLUSH,
120
121	OBIO_FDC,	/* floppy disk (3/80) */
122	OBIO_PRINTER_PORT, /* printer port (3/80) */
123};
124#define OBIO_ALIST_LEN (sizeof(obio_alist) / \
125                        sizeof(obio_alist[0]))
126
127static void
128obio_attach(parent, self, aux)
129	struct device *parent;
130	struct device *self;
131	void *aux;
132{
133	struct confargs *ca = aux;
134	int	i;
135
136	printf("\n");
137
138	/* Configure these in the order listed above. */
139	for (i = 0; i < OBIO_ALIST_LEN; i++) {
140		/* Our parent set ca->ca_bustype already. */
141		ca->ca_paddr = obio_alist[i];
142		/* These are filled-in by obio_submatch. */
143		ca->ca_intpri = -1;
144		ca->ca_intvec = -1;
145		(void) config_found_sm(self, ca, obio_print, obio_submatch);
146	}
147}
148
149/*
150 * Print out the confargs.  The (parent) name is non-NULL
151 * when there was no match found by config_found().
152 */
153static int
154obio_print(args, name)
155	void *args;
156	const char *name;
157{
158
159	/* Be quiet about empty OBIO locations. */
160	if (name)
161		return(QUIET);
162
163	/* Otherwise do the usual. */
164	return(bus_print(args, name));
165}
166
167int
168obio_submatch(parent, cf, aux)
169	struct device *parent;
170	struct cfdata *cf;
171	void *aux;
172{
173	struct confargs *ca = aux;
174	cfmatch_t submatch;
175
176	/*
177	 * Note that a defaulted address locator can never match
178	 * the value of ca->ca_paddr set by the obio_attach loop.
179	 * Without this diagnostic, any device with a defaulted
180	 * address locator would always be silently unmatched.
181	 * Therefore, just disallow default addresses on OBIO.
182	 */
183#ifdef	DIAGNOSTIC
184	if (cf->cf_paddr == -1)
185		panic("obio_submatch: invalid address for: %s%d\n",
186			cf->cf_driver->cd_name, cf->cf_unit);
187#endif
188
189	/*
190	 * Note that obio_attach calls config_found_sm() with
191	 * this function as the "submatch" and ca->ca_paddr
192	 * set to each of the possible OBIO locations, so we
193	 * want to reject any unmatched address here.
194	 */
195	if (cf->cf_paddr != ca->ca_paddr)
196		return 0;
197
198	/*
199	 * Copy the locators into our confargs for the child.
200	 * Note: ca->ca_bustype was set by our parent driver
201	 * (mainbus) and ca->ca_paddr was set by obio_attach.
202	 */
203	ca->ca_intpri = cf->cf_intpri;
204	ca->ca_intvec = cf->cf_intvec;
205
206	/* Now call the match function of the potential child. */
207	submatch = cf->cf_attach->ca_match;
208	if (submatch == NULL)
209		panic("obio_submatch: no match function for: %s\n",
210			  cf->cf_driver->cd_name);
211
212	return ((*submatch)(parent, cf, aux));
213}
214
215
216/*****************************************************************/
217
218/*
219 * This is our record of "interesting" OBIO mappings that
220 * the PROM has left in the virtual space reserved for it.
221 * Each row of the array holds a virtual address and the
222 * physical address it maps to (if found).
223 */
224static struct prom_map {
225	paddr_t pa;
226	vaddr_t 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 sz)
244{
245	int i, off;
246
247	off = pa & PGOFSET;
248	pa -= off;
249	sz += off;
250
251	/* The saved mappings are all one page long. */
252	if (sz > NBPG)
253		return (caddr_t)0;
254
255	/* Linear search for it.  The list is short. */
256	for (i = 0; i < PROM_MAP_CNT; i++) {
257		if (pa == prom_mappings[i].pa) {
258			return ((caddr_t)(prom_mappings[i].va + off));
259		}
260	}
261	return (caddr_t)0;
262}
263
264/*
265 * Search the PROM page tables for OBIO mappings that
266 * we would like to borrow.
267 */
268static void
269save_prom_mappings __P((void))
270{
271	int *mon_pte;
272	vaddr_t va;
273	paddr_t pa;
274	int i;
275
276	/* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */
277	mon_pte = *romVectorPtr->monptaddr;
278
279	for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND;
280		 va += NBPG, mon_pte++)
281	{
282		/* Is this a valid mapping to OBIO? */
283		/* XXX - Some macros would be nice... */
284		if ((*mon_pte & 0xF0000003) != 0x60000001)
285			continue;
286
287		/* Yes it is.  Is this a mapping we want? */
288		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
289		for (i = 0; i < PROM_MAP_CNT; i++) {
290			if (pa != prom_mappings[i].pa)
291				continue;
292			/* Yes, we want it.  Save the va? */
293			if (prom_mappings[i].va == 0) {
294				prom_mappings[i].va = va;
295			}
296		}
297	}
298
299}
300
301/*
302 * These are all the OBIO address that are required early in
303 * the life of the kernel.  All are less than one page long.
304 * This function should make any required mappings that we
305 * were not able to find among the PROM monitor's mappings.
306 */
307static void
308make_required_mappings __P((void))
309{
310	int i;
311
312	for (i = 0; i < PROM_MAP_CNT; i++) {
313		if (prom_mappings[i].va == 0) {
314			/*
315			 * Actually, the PROM always has all the
316			 * "required" mappings we need, (smile)
317			 * but this makes sure that is true.
318			 */
319			mon_printf("obio: no mapping for pa=0x%x\n",
320			    prom_mappings[i].pa);
321			sunmon_abort();  /* Ancient PROM? */
322		}
323	}
324}
325
326
327/*
328 * Find mappings for devices that are needed before autoconfiguration.
329 * We first look for and record any useful PROM mappings, then call
330 * the "init" functions for drivers that we need to use before the
331 * normal autoconfiguration calls configure().  Warning: this is
332 * called before pmap_bootstrap, so no allocation allowed!
333 */
334void
335obio_init()
336{
337	save_prom_mappings();
338	make_required_mappings();
339
340	enable_init();
341
342	/*
343	 * Find the interrupt reg mapping and turn off the
344	 * interrupts, otherwise the PROM clock interrupt
345	 * would poll the zs and toggle some LEDs...
346	 */
347	intreg_init();
348}
349