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