xref: /netbsd-src/sys/arch/sun3/sun3x/obio.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: obio.c,v 1.35 2021/04/24 23:36:50 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.35 2021/04/24 23:36:50 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 
39 #include <uvm/uvm_extern.h>
40 
41 #define _SUN68K_BUS_DMA_PRIVATE
42 #include <machine/autoconf.h>
43 #include <machine/bus.h>
44 #include <machine/dvma.h>
45 #include <machine/mon.h>
46 #include <machine/pte.h>
47 
48 #include <sun3/sun3/machdep.h>
49 #include <sun3/sun3x/obio.h>
50 
51 static int	obio_match(device_t, cfdata_t, void *);
52 static void	obio_attach(device_t, device_t, void *);
53 static int	obio_print(void *, const char *);
54 static int	obio_submatch(device_t, cfdata_t, const int *, void *);
55 
56 struct obio_softc {
57 	device_t	sc_dev;
58 	bus_space_tag_t	sc_bustag;
59 	bus_dma_tag_t	sc_dmatag;
60 };
61 CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
62     obio_match, obio_attach, NULL, NULL);
63 
64 static int obio_attached;
65 
66 static int obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
67     int, vaddr_t, bus_space_handle_t *);
68 static paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
69     off_t, int, int);
70 static int obio_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
71     struct proc *, int);
72 
73 static struct sun68k_bus_space_tag obio_space_tag = {
74 	NULL,				/* cookie */
75 	NULL,				/* parent bus space tag */
76 	obio_bus_map,			/* bus_space_map */
77 	NULL,				/* bus_space_unmap */
78 	NULL,				/* bus_space_subregion */
79 	NULL,				/* bus_space_barrier */
80 	obio_bus_mmap,			/* bus_space_mmap */
81 	NULL,				/* bus_intr_establish */
82 	NULL,				/* bus_space_peek_N */
83 	NULL				/* bus_space_poke_N */
84 };
85 
86 static struct sun68k_bus_dma_tag obio_dma_tag;
87 
88 static int
89 obio_match(device_t parent, cfdata_t cf, void *aux)
90 {
91 	struct confargs *ca = aux;
92 
93 	if (obio_attached)
94 		return 0;
95 
96 	if (ca->ca_bustype != BUS_OBIO)
97 		return 0;
98 
99 	if (ca->ca_name != NULL && strcmp(cf->cf_name, ca->ca_name) != 0)
100 		return 0;
101 
102 	return 1;
103 }
104 
105 /*
106  * We need control over the order of attachment on OBIO,
107  * so do "direct" style autoconfiguration with addresses
108  * from the list below.  OBIO addresses are fixed forever.
109  *
110  * Warning: This whole list is very carefully ordered!
111  * In general, anything not already shown here should
112  * be added at or near the end.
113  */
114 static paddr_t obio_alist[] = {
115 
116 	/* This is used by the Ethernet and SCSI drivers. */
117 	OBIO_IOMMU,
118 
119 	/* Misc. registers - needed by many things */
120 	OBIO_ENABLEREG,
121 	OBIO_BUSERRREG,
122 	OBIO_DIAGREG,	/* leds.c */
123 	OBIO_IDPROM1,	/* idprom.c (3/470) */
124 	OBIO_MEMREG,	/* memerr.c */
125 	OBIO_INTERREG,	/* intreg.c */
126 
127 	/* Zilog UARTs */
128 	OBIO_ZS_KBD_MS,
129 	OBIO_ZS_TTY_AB,
130 
131 	/* eeprom.c */
132 	OBIO_EEPROM,
133 
134 	/* Note: This must come after OBIO_IDPROM1. */
135 	OBIO_IDPROM2,	/* idprom.c (3/80) */
136 
137 	/* Note: Must probe for the Intersil first! */
138 	OBIO_CLOCK1,	/* clock.c (3/470) */
139 	OBIO_CLOCK2,	/* clock.c (3/80) */
140 
141 	OBIO_INTEL_ETHER,
142 	OBIO_LANCE_ETHER,
143 
144 	/* Need esp DMA before SCSI. */
145 	OBIO_EMULEX_DMA,  /* 3/80 only */
146 	OBIO_EMULEX_SCSI, /* 3/80 only */
147 
148 	/* Memory subsystem */
149 	OBIO_PCACHE_TAGS,
150 	OBIO_ECCPARREG,
151 	OBIO_IOC_TAGS,
152 	OBIO_IOC_FLUSH,
153 
154 	OBIO_FDC,	/* floppy disk (3/80) */
155 	OBIO_PRINTER_PORT, /* printer port (3/80) */
156 };
157 #define OBIO_ALIST_LEN	__arraycount(obio_alist)
158 
159 static void
160 obio_attach(device_t parent, device_t self, void *aux)
161 {
162 	struct confargs *ca = aux;
163 	struct obio_softc *sc = device_private(self);
164 	struct confargs oba;
165 	int i;
166 
167 	obio_attached = 1;
168 	sc->sc_dev = self;
169 
170 	aprint_normal("\n");
171 
172 	sc->sc_bustag = ca->ca_bustag;
173 	sc->sc_dmatag = ca->ca_dmatag;
174 
175 	obio_space_tag.cookie = sc;
176 	obio_space_tag.parent = sc->sc_bustag;
177 
178 	obio_dma_tag = *sc->sc_dmatag;
179 	obio_dma_tag._cookie = sc;
180 	obio_dma_tag._dmamap_load = obio_dmamap_load;
181 
182 	oba = *ca;
183 	oba.ca_bustag = &obio_space_tag;
184 	oba.ca_dmatag = &obio_dma_tag;
185 
186 	/* Configure these in the order listed above. */
187 	for (i = 0; i < OBIO_ALIST_LEN; i++) {
188 		/* Our parent set ca->ca_bustype already. */
189 		oba.ca_paddr = obio_alist[i];
190 		/* These are filled-in by obio_submatch. */
191 		oba.ca_intpri = -1;
192 		oba.ca_intvec = -1;
193 		config_found(self, &oba, obio_print,
194 		    CFARG_SUBMATCH, obio_submatch,
195 		    CFARG_EOL);
196 	}
197 }
198 
199 /*
200  * Print out the confargs.  The (parent) name is non-NULL
201  * when there was no match found by config_found().
202  */
203 static int
204 obio_print(void *args, const char *name)
205 {
206 
207 	/* Be quiet about empty OBIO locations. */
208 	if (name)
209 		return QUIET;
210 
211 	/* Otherwise do the usual. */
212 	return bus_print(args, name);
213 }
214 
215 int
216 obio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
217 {
218 	struct confargs *ca = aux;
219 
220 	/*
221 	 * Note that a defaulted address locator can never match
222 	 * the value of ca->ca_paddr set by the obio_attach loop.
223 	 * Without this diagnostic, any device with a defaulted
224 	 * address locator would always be silently unmatched.
225 	 * Therefore, just disallow default addresses on OBIO.
226 	 */
227 #ifdef	DIAGNOSTIC
228 	if (cf->cf_paddr == -1)
229 		panic("%s: invalid address for: %s%d",
230 		    __func__, cf->cf_name, cf->cf_unit);
231 #endif
232 
233 	/*
234 	 * Note that obio_attach calls config_found_sm() with
235 	 * this function as the "submatch" and ca->ca_paddr
236 	 * set to each of the possible OBIO locations, so we
237 	 * want to reject any unmatched address here.
238 	 */
239 	if (cf->cf_paddr != ca->ca_paddr)
240 		return 0;
241 
242 	/*
243 	 * Copy the locators into our confargs for the child.
244 	 * Note: ca->ca_bustype was set by our parent driver
245 	 * (mainbus) and ca->ca_paddr was set by obio_attach.
246 	 */
247 	ca->ca_intpri = cf->cf_intpri;
248 	ca->ca_intvec = cf->cf_intvec;
249 
250 	/* Now call the match function of the potential child. */
251 	return config_match(parent, cf, aux);
252 }
253 
254 
255 /*****************************************************************/
256 
257 /*
258  * This is our record of "interesting" OBIO mappings that
259  * the PROM has left in the virtual space reserved for it.
260  * Each row of the array holds a virtual address and the
261  * physical address it maps to (if found).
262  */
263 static struct prom_map {
264 	paddr_t pa;
265 	vaddr_t va;
266 } prom_mappings[] = {
267 	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
268 	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
269 	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
270 	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
271 };
272 #define PROM_MAP_CNT	__arraycount(prom_mappings)
273 
274 /*
275  * Find a virtual address for a device at physical address 'pa'.
276  * If one is found among the mappings already made by the PROM
277  * at power-up time, use it and return 0. Otherwise return errno
278  * as a sign that a mapping will have to be created.
279  */
280 int
281 find_prom_map(paddr_t pa, bus_type_t iospace, int sz, vaddr_t *vap)
282 {
283 	int i;
284 	vsize_t off;
285 
286 	off = pa & PGOFSET;
287 	pa -= off;
288 	sz += off;
289 
290 	/* The saved mappings are all one page long. */
291 	if (sz > PAGE_SIZE)
292 		return EINVAL;
293 
294 	/* Linear search for it.  The list is short. */
295 	for (i = 0; i < PROM_MAP_CNT; i++) {
296 		if (pa == prom_mappings[i].pa) {
297 			*vap = prom_mappings[i].va + off;
298 			return 0;
299 		}
300 	}
301 	return ENOENT;
302 }
303 
304 /*
305  * Search the PROM page tables for OBIO mappings that
306  * we would like to borrow.
307  */
308 static void
309 save_prom_mappings(void)
310 {
311 	int *mon_pte;
312 	vaddr_t va;
313 	paddr_t pa;
314 	int i;
315 
316 	/* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */
317 	mon_pte = *romVectorPtr->monptaddr;
318 
319 	for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND;
320 	    va += PAGE_SIZE, mon_pte++) {
321 		/* Is this a valid mapping to OBIO? */
322 		/* XXX - Some macros would be nice... */
323 		if ((*mon_pte & 0xF0000003) != 0x60000001)
324 			continue;
325 
326 		/* Yes it is.  Is this a mapping we want? */
327 		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
328 		for (i = 0; i < PROM_MAP_CNT; i++) {
329 			if (pa != prom_mappings[i].pa)
330 				continue;
331 			/* Yes, we want it.  Save the va? */
332 			if (prom_mappings[i].va == 0) {
333 				prom_mappings[i].va = va;
334 			}
335 		}
336 	}
337 }
338 
339 /*
340  * These are all the OBIO address that are required early in
341  * the life of the kernel.  All are less than one page long.
342  * This function should make any required mappings that we
343  * were not able to find among the PROM monitor's mappings.
344  */
345 static void
346 make_required_mappings(void)
347 {
348 	int i;
349 
350 	for (i = 0; i < PROM_MAP_CNT; i++) {
351 		if (prom_mappings[i].va == 0) {
352 			/*
353 			 * Actually, the PROM always has all the
354 			 * "required" mappings we need, (smile)
355 			 * but this makes sure that is true.
356 			 */
357 			mon_printf("obio: no mapping for pa=0x%x\n",
358 			    prom_mappings[i].pa);
359 			sunmon_abort();  /* Ancient PROM? */
360 		}
361 	}
362 }
363 
364 
365 /*
366  * Find mappings for devices that are needed before autoconfiguration.
367  * We first look for and record any useful PROM mappings, then call
368  * the "init" functions for drivers that we need to use before the
369  * normal autoconfiguration calls configure().  Warning: this is
370  * called before pmap_bootstrap, so no allocation allowed!
371  */
372 void
373 obio_init(void)
374 {
375 
376 	save_prom_mappings();
377 	make_required_mappings();
378 
379 	enable_init();
380 
381 	/*
382 	 * Find the interrupt reg mapping and turn off the
383 	 * interrupts, otherwise the PROM clock interrupt
384 	 * would poll the zs and toggle some LEDs...
385 	 */
386 	intreg_init();
387 }
388 
389 int
390 obio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
391     bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
392 {
393 	struct obio_softc *sc = t->cookie;
394 
395 	return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
396 	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
397 }
398 
399 paddr_t
400 obio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
401     int prot, int flags)
402 {
403 	struct obio_softc *sc = t->cookie;
404 
405 	return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off, prot,
406 	    flags);
407 }
408 
409 static int
410 obio_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
411     bus_size_t buflen, struct proc *p, int flags)
412 {
413 	int error;
414 
415 	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
416 	if (error == 0)
417 		map->dm_segs[0].ds_addr &= DVMA_OBIO_SLAVE_MASK;
418 	return error;
419 }
420