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