obio.c revision 1.36
1/*	$NetBSD: obio.c,v 1.36 2021/08/07 16:19:06 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.36 2021/08/07 16:19:06 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
51static int	obio_match(device_t, cfdata_t, void *);
52static void	obio_attach(device_t, device_t, void *);
53static int	obio_print(void *, const char *);
54static int	obio_submatch(device_t, cfdata_t, const int *, void *);
55
56struct obio_softc {
57	device_t	sc_dev;
58	bus_space_tag_t	sc_bustag;
59	bus_dma_tag_t	sc_dmatag;
60};
61CFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
62    obio_match, obio_attach, NULL, NULL);
63
64static int obio_attached;
65
66static 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 *);
68static paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
69    off_t, int, int);
70static int obio_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
71    struct proc *, int);
72
73static 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
86static struct sun68k_bus_dma_tag obio_dma_tag;
87
88static int
89obio_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 */
114static 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
159static void
160obio_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		    CFARGS(.submatch = obio_submatch));
195	}
196}
197
198/*
199 * Print out the confargs.  The (parent) name is non-NULL
200 * when there was no match found by config_found().
201 */
202static int
203obio_print(void *args, const char *name)
204{
205
206	/* Be quiet about empty OBIO locations. */
207	if (name)
208		return QUIET;
209
210	/* Otherwise do the usual. */
211	return bus_print(args, name);
212}
213
214int
215obio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
216{
217	struct confargs *ca = aux;
218
219	/*
220	 * Note that a defaulted address locator can never match
221	 * the value of ca->ca_paddr set by the obio_attach loop.
222	 * Without this diagnostic, any device with a defaulted
223	 * address locator would always be silently unmatched.
224	 * Therefore, just disallow default addresses on OBIO.
225	 */
226#ifdef	DIAGNOSTIC
227	if (cf->cf_paddr == -1)
228		panic("%s: invalid address for: %s%d",
229		    __func__, cf->cf_name, cf->cf_unit);
230#endif
231
232	/*
233	 * Note that obio_attach calls config_found_sm() with
234	 * this function as the "submatch" and ca->ca_paddr
235	 * set to each of the possible OBIO locations, so we
236	 * want to reject any unmatched address here.
237	 */
238	if (cf->cf_paddr != ca->ca_paddr)
239		return 0;
240
241	/*
242	 * Copy the locators into our confargs for the child.
243	 * Note: ca->ca_bustype was set by our parent driver
244	 * (mainbus) and ca->ca_paddr was set by obio_attach.
245	 */
246	ca->ca_intpri = cf->cf_intpri;
247	ca->ca_intvec = cf->cf_intvec;
248
249	/* Now call the match function of the potential child. */
250	return config_match(parent, cf, aux);
251}
252
253
254/*****************************************************************/
255
256/*
257 * This is our record of "interesting" OBIO mappings that
258 * the PROM has left in the virtual space reserved for it.
259 * Each row of the array holds a virtual address and the
260 * physical address it maps to (if found).
261 */
262static struct prom_map {
263	paddr_t pa;
264	vaddr_t va;
265} prom_mappings[] = {
266	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
267	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
268	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
269	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
270};
271#define PROM_MAP_CNT	__arraycount(prom_mappings)
272
273/*
274 * Find a virtual address for a device at physical address 'pa'.
275 * If one is found among the mappings already made by the PROM
276 * at power-up time, use it and return 0. Otherwise return errno
277 * as a sign that a mapping will have to be created.
278 */
279int
280find_prom_map(paddr_t pa, bus_type_t iospace, int sz, vaddr_t *vap)
281{
282	int i;
283	vsize_t off;
284
285	off = pa & PGOFSET;
286	pa -= off;
287	sz += off;
288
289	/* The saved mappings are all one page long. */
290	if (sz > PAGE_SIZE)
291		return EINVAL;
292
293	/* Linear search for it.  The list is short. */
294	for (i = 0; i < PROM_MAP_CNT; i++) {
295		if (pa == prom_mappings[i].pa) {
296			*vap = prom_mappings[i].va + off;
297			return 0;
298		}
299	}
300	return ENOENT;
301}
302
303/*
304 * Search the PROM page tables for OBIO mappings that
305 * we would like to borrow.
306 */
307static void
308save_prom_mappings(void)
309{
310	int *mon_pte;
311	vaddr_t va;
312	paddr_t pa;
313	int i;
314
315	/* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */
316	mon_pte = *romVectorPtr->monptaddr;
317
318	for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND;
319	    va += PAGE_SIZE, mon_pte++) {
320		/* Is this a valid mapping to OBIO? */
321		/* XXX - Some macros would be nice... */
322		if ((*mon_pte & 0xF0000003) != 0x60000001)
323			continue;
324
325		/* Yes it is.  Is this a mapping we want? */
326		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
327		for (i = 0; i < PROM_MAP_CNT; i++) {
328			if (pa != prom_mappings[i].pa)
329				continue;
330			/* Yes, we want it.  Save the va? */
331			if (prom_mappings[i].va == 0) {
332				prom_mappings[i].va = va;
333			}
334		}
335	}
336}
337
338/*
339 * These are all the OBIO address that are required early in
340 * the life of the kernel.  All are less than one page long.
341 * This function should make any required mappings that we
342 * were not able to find among the PROM monitor's mappings.
343 */
344static void
345make_required_mappings(void)
346{
347	int i;
348
349	for (i = 0; i < PROM_MAP_CNT; i++) {
350		if (prom_mappings[i].va == 0) {
351			/*
352			 * Actually, the PROM always has all the
353			 * "required" mappings we need, (smile)
354			 * but this makes sure that is true.
355			 */
356			mon_printf("obio: no mapping for pa=0x%x\n",
357			    prom_mappings[i].pa);
358			sunmon_abort();  /* Ancient PROM? */
359		}
360	}
361}
362
363
364/*
365 * Find mappings for devices that are needed before autoconfiguration.
366 * We first look for and record any useful PROM mappings, then call
367 * the "init" functions for drivers that we need to use before the
368 * normal autoconfiguration calls configure().  Warning: this is
369 * called before pmap_bootstrap, so no allocation allowed!
370 */
371void
372obio_init(void)
373{
374
375	save_prom_mappings();
376	make_required_mappings();
377
378	enable_init();
379
380	/*
381	 * Find the interrupt reg mapping and turn off the
382	 * interrupts, otherwise the PROM clock interrupt
383	 * would poll the zs and toggle some LEDs...
384	 */
385	intreg_init();
386}
387
388int
389obio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
390    bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
391{
392	struct obio_softc *sc = t->cookie;
393
394	return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
395	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
396}
397
398paddr_t
399obio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
400    int prot, int flags)
401{
402	struct obio_softc *sc = t->cookie;
403
404	return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off, prot,
405	    flags);
406}
407
408static int
409obio_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
410    bus_size_t buflen, struct proc *p, int flags)
411{
412	int error;
413
414	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
415	if (error == 0)
416		map->dm_segs[0].ds_addr &= DVMA_OBIO_SLAVE_MASK;
417	return error;
418}
419