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