obio.c revision 1.4
1/*	$NetBSD: obio.c,v 1.4 1997/02/21 20:33:09 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 */
83static int obio_alist[] = {
84	OBIO_ZS_KBD_MS,
85	OBIO_ZS_TTY_AB,
86
87	OBIO_EEPROM,	/* the next two are part of the eeprom */
88	OBIO_IDPROM2,	/* 3/80 only */
89	OBIO_CLOCK2,	/* 3/80 only */
90	OBIO_CLOCK1,	/* 3/470 only */
91
92	/* These are all in the same page - could be just one driver... */
93	OBIO_ENABLEREG,
94	OBIO_BUSERRREG,
95	OBIO_DIAGREG,
96	OBIO_IDPROM1,
97	OBIO_MEMREG,
98	OBIO_INTERREG,
99
100	/*
101	 * The addresses listed before this point were already found
102	 * and mapped in by the startup code, so keep those in the
103	 * order shown, and separated from what follows.
104	 * (Just to be honest about attach order.)
105	 *
106	 * The following are carefully ordered.
107	 */
108
109	/* This is used by video board drivers... */
110	OBIO_P4_REG,
111
112	/* This is used by the Ethernet and SCSI drivers. */
113	OBIO_IOMMU,
114
115	OBIO_INTEL_ETHER,
116	OBIO_LANCE_ETHER,
117
118	OBIO_EMULEX_SCSI, /* 3/80 only */
119
120	/* ...todo... */
121	OBIO_FDC,
122	OBIO_PRINTER_PORT,
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 order of address. */
139	for (i = 0; i < OBIO_ALIST_LEN; i++) {
140		/* We know ca_bustype == BUS_OBIO */
141		ca->ca_paddr = obio_alist[i];
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	struct confargs *ca = args;
158
159	/* Be quiet about empty OBIO locations. */
160	if (name)
161		return(QUIET);
162
163	if (ca->ca_paddr != -1)
164		printf(" addr 0x%x", ca->ca_paddr);
165	if (ca->ca_intpri != -1)
166		printf(" level %d", ca->ca_intpri);
167
168	return(UNCONF);
169}
170
171int
172obio_submatch(parent, cf, aux)
173	struct device *parent;
174	struct cfdata *cf;
175	void *aux;
176{
177	struct confargs *ca = aux;
178	cfmatch_t submatch;
179
180	/*
181	 * Default addresses are mostly useless for OBIO.
182	 * The address assignments are fixed for all time,
183	 * so our config files might as well reflect that.
184	 */
185#ifdef	DIAGNOSTIC
186	if (cf->cf_paddr == -1)
187		panic("obio_submatch: invalid address for: %s%d\n",
188			cf->cf_driver->cd_name, cf->cf_unit);
189#endif
190
191	/* This enforces exact address match. */
192	if (cf->cf_paddr != ca->ca_paddr)
193		return 0;
194
195	/* Now call the match function of the potential child. */
196	submatch = cf->cf_attach->ca_match;
197	if (submatch == NULL)
198		panic("obio_submatch: no match function for: %s\n",
199			  cf->cf_driver->cd_name);
200
201	return ((*submatch)(parent, cf, aux));
202}
203
204
205/*****************************************************************/
206
207/*
208 * This is our record of "interesting" OBIO mappings that
209 * the PROM has left in the virtual space reserved for it.
210 * Each non-null array element holds the virtual address
211 * of an OBIO mapping where the OBIO address mapped is:
212 *     (array_index * SAVE_INCR)
213 * and the length of the mapping is one page.
214 */
215static struct prom_map {
216	vm_offset_t pa, va;
217} prom_mappings[] = {
218	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
219	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
220	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
221	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
222};
223#define PROM_MAP_CNT (sizeof(prom_mappings) / \
224		      sizeof(prom_mappings[0]))
225
226/*
227 * Find a virtual address for a device at physical address 'pa'.
228 * If one is found among the mappings already made by the PROM
229 * at power-up time, use it.  Otherwise return 0 as a sign that
230 * a mapping will have to be created.
231 */
232caddr_t
233obio_find_mapping(int pa, int size)
234{
235	int i, off;
236
237	if (size >= NBPG)
238		return (caddr_t)0;
239
240	off = pa & PGOFSET;
241	pa -= off;
242
243	for (i = 0; i < PROM_MAP_CNT; i++) {
244		if (pa == prom_mappings[i].pa) {
245			return ((caddr_t)(prom_mappings[i].va + off));
246		}
247	}
248	return (caddr_t)0;
249}
250
251/*
252 * Search the PROM page tables for OBIO mappings that
253 * we would like to borrow.
254 */
255static void
256save_prom_mappings __P((void))
257{
258	int *mon_pte;
259	vm_offset_t va, pa;
260	int i;
261
262	/* Note: mon_ctbl[0] maps MON_KDB_START */
263	mon_pte = *romVectorPtr->monptaddr;
264
265	for (va = MON_KDB_START; va < MONEND;
266		 va += NBPG, mon_pte++)
267	{
268		/* Is this a valid mapping to OBIO? */
269		/* XXX - Some macros would be nice... */
270		if ((*mon_pte & 0xF0000003) != 0x60000001)
271			continue;
272
273		/* Yes it is.  Is this a mapping we want? */
274		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
275		for (i = 0; i < PROM_MAP_CNT; i++) {
276			if (pa != prom_mappings[i].pa)
277				continue;
278			/* Yes, we want it.  Save the va? */
279			if (prom_mappings[i].va == 0) {
280				prom_mappings[i].va = va;
281			}
282		}
283	}
284
285}
286
287/*
288 * These are all the OBIO address that are required early in
289 * the life of the kernel.  All are less than one page long.
290 * This function should make any required mappings that we
291 * were not able to find among the PROM monitor's mappings.
292 */
293static void
294make_required_mappings __P((void))
295{
296	int i;
297
298	for (i = 0; i < PROM_MAP_CNT; i++) {
299		if (prom_mappings[i].va == 0) {
300			/*
301			 * Actually, the PROM always has all the
302			 * "required" mappings we need, (smile)
303			 * but this makes sure that is true.
304			 */
305			mon_printf("obio: no mapping for pa=0x%x\n",
306			    prom_mappings[i].pa);
307			sunmon_abort();  /* Ancient PROM? */
308		}
309	}
310}
311
312
313/*
314 * Find mappings for devices that are needed before autoconfiguration.
315 * We first look for and record any useful PROM mappings, then call
316 * the "init" functions for drivers that we need to use before the
317 * normal autoconfiguration calls configure().  Warning: this is
318 * called before pmap_bootstrap, so no allocation allowed!
319 */
320void
321obio_init()
322{
323	save_prom_mappings();
324	make_required_mappings();
325
326	/* Init drivers that use the required OBIO mappings. */
327	idprom_init();
328	eeprom_init();
329	zs_init();
330
331	enable_reg = (short*) obio_find_mapping(OBIO_ENABLEREG, 2);
332	intreg_init();
333	clock_init();
334}
335
336caddr_t
337obio_alloc(obio_addr, obio_size)
338	int obio_addr, obio_size;
339{
340	caddr_t cp;
341
342	cp = obio_find_mapping((vm_offset_t)obio_addr, obio_size);
343	if (cp) return (cp);
344
345	cp = bus_mapin(BUS_OBIO, obio_addr, obio_size);
346	return (cp);
347}
348