obio.c revision 1.2
1/*	$NetBSD: obio.c,v 1.2 1997/01/25 21:48:49 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/pte.h>
45#include <machine/mon.h>
46#include <machine/obio.h>
47
48#include <sun3/sun3/sunmon.h>
49
50short *enable_reg;
51
52static int  obio_match __P((struct device *, struct cfdata *, void *));
53static void obio_attach __P((struct device *, struct device *, void *));
54static int  obio_print __P((void *, const char *parentname));
55static int	obio_submatch __P((struct device *, struct cfdata *, void *));
56
57struct cfattach obio_ca = {
58	sizeof(struct device), obio_match, obio_attach
59};
60
61struct cfdriver obio_cd = {
62	NULL, "obio", DV_DULL
63};
64
65static int
66obio_match(parent, cf, aux)
67	struct device *parent;
68	struct cfdata *cf;
69	void *aux;
70{
71	struct confargs *ca = aux;
72
73	if (ca->ca_bustype != BUS_OBIO)
74		return (0);
75	return(1);
76}
77
78/*
79 * We need some control over the order of attachment on OBIO,
80 * and all OBIO device addresses are known and fixed foerver.
81 * Therefore, this uses a list of addresses to attach.
82 * XXX - Any other way to control search/attach order?
83 */
84static int obio_alist[] = {
85	OBIO_ZS_KBD_MS,
86	OBIO_ZS_TTY_AB,
87
88	OBIO_EEPROM,	/* the next two are part of the eeprom */
89	OBIO_IDPROM2,	/* 3/80 only */
90	OBIO_CLOCK2,	/* 3/80 only */
91
92	OBIO_CLOCK1,	/* 3/470 only */
93
94	/* These are all in the same page - could be just one driver... */
95	OBIO_ENABLEREG,
96	OBIO_BUSERRREG,
97	OBIO_DIAGREG,
98	OBIO_IDPROM1,
99	OBIO_MEMREG,
100	OBIO_INTERREG,
101
102	OBIO_P4_REG,
103	OBIO_IOMMU,
104
105	OBIO_INTEL_ETHER,
106	OBIO_LANCE_ETHER,
107
108	/* ...todo... */
109	OBIO_FDC,
110	OBIO_PRINTER_PORT,
111};
112#define	OBIO_ALIST_LEN (sizeof(obio_alist) / \
113						sizeof(obio_alist[0]))
114
115static void
116obio_attach(parent, self, aux)
117	struct device *parent;
118	struct device *self;
119	void *aux;
120{
121	struct confargs *ca = aux;
122	int	i;
123
124	printf("\n");
125
126	/* Configure these in order of address. */
127	for (i = 0; i < OBIO_ALIST_LEN; i++) {
128		/* We know ca_bustype == BUS_OBIO */
129		ca->ca_paddr = obio_alist[i];
130		ca->ca_intpri = -1;
131		ca->ca_intvec = -1;
132		(void) config_found_sm(self, ca, obio_print, obio_submatch);
133	}
134}
135
136/*
137 * Print out the confargs.  The (parent) name is non-NULL
138 * when there was no match found by config_found().
139 */
140static int
141obio_print(args, name)
142	void *args;
143	const char *name;
144{
145	struct confargs *ca = args;
146
147	/* Be quiet about empty OBIO locations. */
148	if (name)
149		return(QUIET);
150
151	if (ca->ca_paddr != -1)
152		printf(" addr 0x%x", ca->ca_paddr);
153	if (ca->ca_intpri != -1)
154		printf(" level %d", ca->ca_intpri);
155
156	return(UNCONF);
157}
158
159int
160obio_submatch(parent, cf, aux)
161	struct device *parent;
162	struct cfdata *cf;
163	void *aux;
164{
165	struct confargs *ca = aux;
166	cfmatch_t submatch;
167
168	/*
169	 * Default addresses are mostly useless for OBIO.
170	 * The address assignments are fixed for all time,
171	 * so our config files might as well reflect that.
172	 */
173#ifdef	DIAGNOSTIC
174	if (cf->cf_paddr == -1)
175		panic("obio_submatch: invalid address for: %s%d\n",
176			cf->cf_driver->cd_name, cf->cf_unit);
177#endif
178
179	/* This enforces exact address match. */
180	if (cf->cf_paddr != ca->ca_paddr)
181		return 0;
182
183	/* Now call the match function of the potential child. */
184	submatch = cf->cf_attach->ca_match;
185	if (submatch == NULL)
186		panic("obio_submatch: no match function for: %s\n",
187			  cf->cf_driver->cd_name);
188
189	return ((*submatch)(parent, cf, aux));
190}
191
192
193/*****************************************************************/
194
195/*
196 * This is our record of "interesting" OBIO mappings that
197 * the PROM has left in the virtual space reserved for it.
198 * Each non-null array element holds the virtual address
199 * of an OBIO mapping where the OBIO address mapped is:
200 *     (array_index * SAVE_INCR)
201 * and the length of the mapping is one page.
202 */
203static struct prom_map {
204	vm_offset_t pa, va;
205} prom_mappings[] = {
206	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
207	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
208	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
209	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
210};
211#define PROM_MAP_CNT (sizeof(prom_mappings) / \
212		      sizeof(prom_mappings[0]))
213
214/*
215 * Find a virtual address for a device at physical address 'pa'.
216 * If one is found among the mappings already made by the PROM
217 * at power-up time, use it.  Otherwise return 0 as a sign that
218 * a mapping will have to be created.
219 */
220caddr_t
221obio_find_mapping(int pa, int size)
222{
223	int i, off;
224
225	if (size >= NBPG)
226		return (caddr_t)0;
227
228	off = pa & PGOFSET;
229	pa -= off;
230
231	for (i = 0; i < PROM_MAP_CNT; i++) {
232		if (pa == prom_mappings[i].pa) {
233			return ((caddr_t)(prom_mappings[i].va + off));
234		}
235	}
236	return (caddr_t)0;
237}
238
239/*
240 * Search the PROM page tables for OBIO mappings that
241 * we would like to borrow.
242 */
243static void
244save_prom_mappings __P((void))
245{
246	int *mon_pte;
247	vm_offset_t va, pa;
248	int i;
249
250	/* Note: mon_ctbl[0] maps MON_KDB_START */
251	mon_pte = *romVectorPtr->monptaddr;
252
253	for (va = MON_KDB_START; va < MONEND;
254		 va += NBPG, mon_pte++)
255	{
256		/* Is this a valid mapping to OBIO? */
257		/* XXX - Some macros would be nice... */
258		if ((*mon_pte & 0xF0000003) != 0x60000001)
259			continue;
260
261		/* Yes it is.  Is this a mapping we want? */
262		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
263		for (i = 0; i < PROM_MAP_CNT; i++) {
264			if (pa != prom_mappings[i].pa)
265				continue;
266			/* Yes, we want it.  Save the va? */
267			if (prom_mappings[i].va == 0) {
268				prom_mappings[i].va = va;
269			}
270		}
271	}
272
273}
274
275/*
276 * These are all the OBIO address that are required early in
277 * the life of the kernel.  All are less than one page long.
278 * This function should make any required mappings that we
279 * were not able to find among the PROM monitor's mappings.
280 */
281static void
282make_required_mappings __P((void))
283{
284	int i;
285
286	for (i = 0; i < PROM_MAP_CNT; i++) {
287		if (prom_mappings[i].va == 0) {
288			/*
289			 * Actually, the PROM always has all the
290			 * "required" mappings we need, (smile)
291			 * but this makes sure that is true.
292			 */
293			mon_printf("obio: no mapping for pa=0x%x\n",
294			    prom_mappings[i].pa);
295			sunmon_abort();  /* Ancient PROM? */
296		}
297	}
298}
299
300
301/*
302 * Find mappings for devices that are needed before autoconfiguration.
303 * We first look for and record any useful PROM mappings, then call
304 * the "init" functions for drivers that we need to use before the
305 * normal autoconfiguration calls configure().  Warning: this is
306 * called before pmap_bootstrap, so no allocation allowed!
307 */
308void
309obio_init()
310{
311	save_prom_mappings();
312	make_required_mappings();
313
314	/* Init drivers that use the required OBIO mappings. */
315	idprom_init();
316	eeprom_init();
317	zs_init();
318
319	enable_reg = (short*) obio_find_mapping(OBIO_ENABLEREG, 2);
320	intreg_init();
321	clock_init();
322}
323
324caddr_t
325obio_alloc(obio_addr, obio_size)
326	int obio_addr, obio_size;
327{
328	caddr_t cp;
329
330	cp = obio_find_mapping((vm_offset_t)obio_addr, obio_size);
331	if (cp) return (cp);
332
333	cp = bus_mapin(BUS_OBIO, obio_addr, obio_size);
334	return (cp);
335}
336