11.36Sthorpej/*	$NetBSD: obio.c,v 1.36 2021/08/07 16:19:06 thorpej Exp $	*/
21.1Sgwr
31.1Sgwr/*-
41.1Sgwr * Copyright (c) 1996 The NetBSD Foundation, Inc.
51.1Sgwr * All rights reserved.
61.1Sgwr *
71.1Sgwr * This code is derived from software contributed to The NetBSD Foundation
81.1Sgwr * by Adam Glass and Gordon W. Ross.
91.1Sgwr *
101.1Sgwr * Redistribution and use in source and binary forms, with or without
111.1Sgwr * modification, are permitted provided that the following conditions
121.1Sgwr * are met:
131.1Sgwr * 1. Redistributions of source code must retain the above copyright
141.1Sgwr *    notice, this list of conditions and the following disclaimer.
151.1Sgwr * 2. Redistributions in binary form must reproduce the above copyright
161.1Sgwr *    notice, this list of conditions and the following disclaimer in the
171.1Sgwr *    documentation and/or other materials provided with the distribution.
181.1Sgwr *
191.1Sgwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Sgwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Sgwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Sgwr * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Sgwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Sgwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Sgwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Sgwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Sgwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Sgwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Sgwr * POSSIBILITY OF SUCH DAMAGE.
301.1Sgwr */
311.22Slukem
321.22Slukem#include <sys/cdefs.h>
331.36Sthorpej__KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.36 2021/08/07 16:19:06 thorpej Exp $");
341.1Sgwr
351.1Sgwr#include <sys/param.h>
361.1Sgwr#include <sys/systm.h>
371.1Sgwr#include <sys/device.h>
381.1Sgwr
391.21Sthorpej#include <uvm/uvm_extern.h>
401.21Sthorpej
411.29Stsutsui#define _SUN68K_BUS_DMA_PRIVATE
421.1Sgwr#include <machine/autoconf.h>
431.27Stsutsui#include <machine/bus.h>
441.29Stsutsui#include <machine/dvma.h>
451.1Sgwr#include <machine/mon.h>
461.3Sgwr#include <machine/pte.h>
471.2Sgwr
481.9Sgwr#include <sun3/sun3/machdep.h>
491.9Sgwr#include <sun3/sun3x/obio.h>
501.1Sgwr
511.32Stsutsuistatic int	obio_match(device_t, cfdata_t, void *);
521.32Stsutsuistatic void	obio_attach(device_t, device_t, void *);
531.23Schsstatic int	obio_print(void *, const char *);
541.32Stsutsuistatic int	obio_submatch(device_t, cfdata_t, const int *, void *);
551.1Sgwr
561.27Stsutsuistruct obio_softc {
571.32Stsutsui	device_t	sc_dev;
581.27Stsutsui	bus_space_tag_t	sc_bustag;
591.27Stsutsui	bus_dma_tag_t	sc_dmatag;
601.27Stsutsui};
611.32StsutsuiCFATTACH_DECL_NEW(obio, sizeof(struct obio_softc),
621.20Sthorpej    obio_match, obio_attach, NULL, NULL);
631.1Sgwr
641.27Stsutsuistatic int obio_attached;
651.27Stsutsui
661.27Stsutsuistatic int obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
671.27Stsutsui    int, vaddr_t, bus_space_handle_t *);
681.27Stsutsuistatic paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
691.27Stsutsui    off_t, int, int);
701.29Stsutsuistatic int obio_dmamap_load(bus_dma_tag_t, bus_dmamap_t, void *, bus_size_t,
711.29Stsutsui    struct proc *, int);
721.27Stsutsui
731.27Stsutsuistatic struct sun68k_bus_space_tag obio_space_tag = {
741.27Stsutsui	NULL,				/* cookie */
751.27Stsutsui	NULL,				/* parent bus space tag */
761.27Stsutsui	obio_bus_map,			/* bus_space_map */
771.27Stsutsui	NULL,				/* bus_space_unmap */
781.27Stsutsui	NULL,				/* bus_space_subregion */
791.27Stsutsui	NULL,				/* bus_space_barrier */
801.27Stsutsui	obio_bus_mmap,			/* bus_space_mmap */
811.27Stsutsui	NULL,				/* bus_intr_establish */
821.27Stsutsui	NULL,				/* bus_space_peek_N */
831.27Stsutsui	NULL				/* bus_space_poke_N */
841.27Stsutsui};
851.27Stsutsui
861.29Stsutsuistatic struct sun68k_bus_dma_tag obio_dma_tag;
871.29Stsutsui
881.34Stsutsuistatic int
891.32Stsutsuiobio_match(device_t parent, cfdata_t cf, void *aux)
901.1Sgwr{
911.1Sgwr	struct confargs *ca = aux;
921.1Sgwr
931.27Stsutsui	if (obio_attached)
941.27Stsutsui		return 0;
951.27Stsutsui
961.1Sgwr	if (ca->ca_bustype != BUS_OBIO)
971.27Stsutsui		return 0;
981.27Stsutsui
991.27Stsutsui	if (ca->ca_name != NULL && strcmp(cf->cf_name, ca->ca_name) != 0)
1001.27Stsutsui		return 0;
1011.27Stsutsui
1021.27Stsutsui	return 1;
1031.1Sgwr}
1041.1Sgwr
1051.1Sgwr/*
1061.10Sgwr * We need control over the order of attachment on OBIO,
1071.10Sgwr * so do "direct" style autoconfiguration with addresses
1081.10Sgwr * from the list below.  OBIO addresses are fixed forever.
1091.5Sgwr *
1101.5Sgwr * Warning: This whole list is very carefully ordered!
1111.5Sgwr * In general, anything not already shown here should
1121.5Sgwr * be added at or near the end.
1131.1Sgwr */
1141.27Stsutsuistatic paddr_t obio_alist[] = {
1151.5Sgwr
1161.10Sgwr	/* This is used by the Ethernet and SCSI drivers. */
1171.10Sgwr	OBIO_IOMMU,
1181.10Sgwr
1191.5Sgwr	/* Misc. registers - needed by many things */
1201.5Sgwr	OBIO_ENABLEREG,
1211.5Sgwr	OBIO_BUSERRREG,
1221.5Sgwr	OBIO_DIAGREG,	/* leds.c */
1231.5Sgwr	OBIO_IDPROM1,	/* idprom.c (3/470) */
1241.5Sgwr	OBIO_MEMREG,	/* memerr.c */
1251.5Sgwr	OBIO_INTERREG,	/* intreg.c */
1261.5Sgwr
1271.5Sgwr	/* Zilog UARTs */
1281.1Sgwr	OBIO_ZS_KBD_MS,
1291.1Sgwr	OBIO_ZS_TTY_AB,
1301.1Sgwr
1311.5Sgwr	/* eeprom.c */
1321.5Sgwr	OBIO_EEPROM,
1331.1Sgwr
1341.5Sgwr	/* Note: This must come after OBIO_IDPROM1. */
1351.5Sgwr	OBIO_IDPROM2,	/* idprom.c (3/80) */
1361.1Sgwr
1371.5Sgwr	/* Note: Must probe for the Intersil first! */
1381.5Sgwr	OBIO_CLOCK1,	/* clock.c (3/470) */
1391.5Sgwr	OBIO_CLOCK2,	/* clock.c (3/80) */
1401.4Sgwr
1411.1Sgwr	OBIO_INTEL_ETHER,
1421.1Sgwr	OBIO_LANCE_ETHER,
1431.1Sgwr
1441.11Sgwr	/* Need esp DMA before SCSI. */
1451.11Sgwr	OBIO_EMULEX_DMA,  /* 3/80 only */
1461.4Sgwr	OBIO_EMULEX_SCSI, /* 3/80 only */
1471.4Sgwr
1481.10Sgwr	/* Memory subsystem */
1491.10Sgwr	OBIO_PCACHE_TAGS,
1501.10Sgwr	OBIO_ECCPARREG,
1511.10Sgwr	OBIO_IOC_TAGS,
1521.10Sgwr	OBIO_IOC_FLUSH,
1531.10Sgwr
1541.10Sgwr	OBIO_FDC,	/* floppy disk (3/80) */
1551.12Stsutsui	OBIO_PRINTER_PORT, /* printer port (3/80) */
1561.1Sgwr};
1571.30Stsutsui#define OBIO_ALIST_LEN	__arraycount(obio_alist)
1581.1Sgwr
1591.34Stsutsuistatic void
1601.33Stsutsuiobio_attach(device_t parent, device_t self, void *aux)
1611.1Sgwr{
1621.1Sgwr	struct confargs *ca = aux;
1631.32Stsutsui	struct obio_softc *sc = device_private(self);
1641.27Stsutsui	struct confargs oba;
1651.27Stsutsui	int i;
1661.27Stsutsui
1671.27Stsutsui	obio_attached = 1;
1681.32Stsutsui	sc->sc_dev = self;
1691.1Sgwr
1701.32Stsutsui	aprint_normal("\n");
1711.1Sgwr
1721.27Stsutsui	sc->sc_bustag = ca->ca_bustag;
1731.27Stsutsui	sc->sc_dmatag = ca->ca_dmatag;
1741.27Stsutsui
1751.27Stsutsui	obio_space_tag.cookie = sc;
1761.27Stsutsui	obio_space_tag.parent = sc->sc_bustag;
1771.27Stsutsui
1781.29Stsutsui	obio_dma_tag = *sc->sc_dmatag;
1791.29Stsutsui	obio_dma_tag._cookie = sc;
1801.29Stsutsui	obio_dma_tag._dmamap_load = obio_dmamap_load;
1811.29Stsutsui
1821.27Stsutsui	oba = *ca;
1831.27Stsutsui	oba.ca_bustag = &obio_space_tag;
1841.29Stsutsui	oba.ca_dmatag = &obio_dma_tag;
1851.27Stsutsui
1861.5Sgwr	/* Configure these in the order listed above. */
1871.1Sgwr	for (i = 0; i < OBIO_ALIST_LEN; i++) {
1881.5Sgwr		/* Our parent set ca->ca_bustype already. */
1891.27Stsutsui		oba.ca_paddr = obio_alist[i];
1901.5Sgwr		/* These are filled-in by obio_submatch. */
1911.27Stsutsui		oba.ca_intpri = -1;
1921.27Stsutsui		oba.ca_intvec = -1;
1931.35Sthorpej		config_found(self, &oba, obio_print,
1941.36Sthorpej		    CFARGS(.submatch = obio_submatch));
1951.1Sgwr	}
1961.1Sgwr}
1971.1Sgwr
1981.1Sgwr/*
1991.1Sgwr * Print out the confargs.  The (parent) name is non-NULL
2001.1Sgwr * when there was no match found by config_found().
2011.1Sgwr */
2021.34Stsutsuistatic int
2031.23Schsobio_print(void *args, const char *name)
2041.1Sgwr{
2051.1Sgwr
2061.1Sgwr	/* Be quiet about empty OBIO locations. */
2071.1Sgwr	if (name)
2081.30Stsutsui		return QUIET;
2091.1Sgwr
2101.5Sgwr	/* Otherwise do the usual. */
2111.30Stsutsui	return bus_print(args, name);
2121.1Sgwr}
2131.1Sgwr
2141.34Stsutsuiint
2151.32Stsutsuiobio_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
2161.1Sgwr{
2171.1Sgwr	struct confargs *ca = aux;
2181.1Sgwr
2191.1Sgwr	/*
2201.5Sgwr	 * Note that a defaulted address locator can never match
2211.5Sgwr	 * the value of ca->ca_paddr set by the obio_attach loop.
2221.5Sgwr	 * Without this diagnostic, any device with a defaulted
2231.5Sgwr	 * address locator would always be silently unmatched.
2241.5Sgwr	 * Therefore, just disallow default addresses on OBIO.
2251.1Sgwr	 */
2261.1Sgwr#ifdef	DIAGNOSTIC
2271.1Sgwr	if (cf->cf_paddr == -1)
2281.30Stsutsui		panic("%s: invalid address for: %s%d",
2291.30Stsutsui		    __func__, cf->cf_name, cf->cf_unit);
2301.1Sgwr#endif
2311.1Sgwr
2321.5Sgwr	/*
2331.5Sgwr	 * Note that obio_attach calls config_found_sm() with
2341.5Sgwr	 * this function as the "submatch" and ca->ca_paddr
2351.5Sgwr	 * set to each of the possible OBIO locations, so we
2361.5Sgwr	 * want to reject any unmatched address here.
2371.5Sgwr	 */
2381.1Sgwr	if (cf->cf_paddr != ca->ca_paddr)
2391.1Sgwr		return 0;
2401.1Sgwr
2411.5Sgwr	/*
2421.5Sgwr	 * Copy the locators into our confargs for the child.
2431.5Sgwr	 * Note: ca->ca_bustype was set by our parent driver
2441.5Sgwr	 * (mainbus) and ca->ca_paddr was set by obio_attach.
2451.5Sgwr	 */
2461.5Sgwr	ca->ca_intpri = cf->cf_intpri;
2471.5Sgwr	ca->ca_intvec = cf->cf_intvec;
2481.5Sgwr
2491.1Sgwr	/* Now call the match function of the potential child. */
2501.30Stsutsui	return config_match(parent, cf, aux);
2511.1Sgwr}
2521.1Sgwr
2531.1Sgwr
2541.1Sgwr/*****************************************************************/
2551.1Sgwr
2561.1Sgwr/*
2571.1Sgwr * This is our record of "interesting" OBIO mappings that
2581.1Sgwr * the PROM has left in the virtual space reserved for it.
2591.10Sgwr * Each row of the array holds a virtual address and the
2601.10Sgwr * physical address it maps to (if found).
2611.1Sgwr */
2621.1Sgwrstatic struct prom_map {
2631.13Stsutsui	paddr_t pa;
2641.13Stsutsui	vaddr_t va;
2651.1Sgwr} prom_mappings[] = {
2661.5Sgwr	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
2671.1Sgwr	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
2681.1Sgwr	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
2691.1Sgwr	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
2701.1Sgwr};
2711.30Stsutsui#define PROM_MAP_CNT	__arraycount(prom_mappings)
2721.1Sgwr
2731.1Sgwr/*
2741.1Sgwr * Find a virtual address for a device at physical address 'pa'.
2751.1Sgwr * If one is found among the mappings already made by the PROM
2761.28Stsutsui * at power-up time, use it and return 0. Otherwise return errno
2771.28Stsutsui * as a sign that a mapping will have to be created.
2781.27Stsutsui */
2791.27Stsutsuiint
2801.28Stsutsuifind_prom_map(paddr_t pa, bus_type_t iospace, int sz, vaddr_t *vap)
2811.27Stsutsui{
2821.27Stsutsui	int i;
2831.27Stsutsui	vsize_t off;
2841.27Stsutsui
2851.27Stsutsui	off = pa & PGOFSET;
2861.27Stsutsui	pa -= off;
2871.27Stsutsui	sz += off;
2881.27Stsutsui
2891.27Stsutsui	/* The saved mappings are all one page long. */
2901.27Stsutsui	if (sz > PAGE_SIZE)
2911.27Stsutsui		return EINVAL;
2921.27Stsutsui
2931.27Stsutsui	/* Linear search for it.  The list is short. */
2941.27Stsutsui	for (i = 0; i < PROM_MAP_CNT; i++) {
2951.27Stsutsui		if (pa == prom_mappings[i].pa) {
2961.28Stsutsui			*vap = prom_mappings[i].va + off;
2971.27Stsutsui			return 0;
2981.27Stsutsui		}
2991.27Stsutsui	}
3001.27Stsutsui	return ENOENT;
3011.27Stsutsui}
3021.27Stsutsui
3031.27Stsutsui/*
3041.1Sgwr * Search the PROM page tables for OBIO mappings that
3051.1Sgwr * we would like to borrow.
3061.1Sgwr */
3071.1Sgwrstatic void
3081.23Schssave_prom_mappings(void)
3091.1Sgwr{
3101.1Sgwr	int *mon_pte;
3111.13Stsutsui	vaddr_t va;
3121.13Stsutsui	paddr_t pa;
3131.1Sgwr	int i;
3141.1Sgwr
3151.9Sgwr	/* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */
3161.1Sgwr	mon_pte = *romVectorPtr->monptaddr;
3171.1Sgwr
3181.9Sgwr	for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND;
3191.30Stsutsui	    va += PAGE_SIZE, mon_pte++) {
3201.1Sgwr		/* Is this a valid mapping to OBIO? */
3211.1Sgwr		/* XXX - Some macros would be nice... */
3221.1Sgwr		if ((*mon_pte & 0xF0000003) != 0x60000001)
3231.1Sgwr			continue;
3241.1Sgwr
3251.1Sgwr		/* Yes it is.  Is this a mapping we want? */
3261.1Sgwr		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
3271.1Sgwr		for (i = 0; i < PROM_MAP_CNT; i++) {
3281.1Sgwr			if (pa != prom_mappings[i].pa)
3291.1Sgwr				continue;
3301.1Sgwr			/* Yes, we want it.  Save the va? */
3311.1Sgwr			if (prom_mappings[i].va == 0) {
3321.1Sgwr				prom_mappings[i].va = va;
3331.1Sgwr			}
3341.1Sgwr		}
3351.1Sgwr	}
3361.1Sgwr}
3371.1Sgwr
3381.1Sgwr/*
3391.1Sgwr * These are all the OBIO address that are required early in
3401.1Sgwr * the life of the kernel.  All are less than one page long.
3411.1Sgwr * This function should make any required mappings that we
3421.1Sgwr * were not able to find among the PROM monitor's mappings.
3431.1Sgwr */
3441.1Sgwrstatic void
3451.23Schsmake_required_mappings(void)
3461.1Sgwr{
3471.1Sgwr	int i;
3481.1Sgwr
3491.1Sgwr	for (i = 0; i < PROM_MAP_CNT; i++) {
3501.1Sgwr		if (prom_mappings[i].va == 0) {
3511.1Sgwr			/*
3521.1Sgwr			 * Actually, the PROM always has all the
3531.1Sgwr			 * "required" mappings we need, (smile)
3541.1Sgwr			 * but this makes sure that is true.
3551.1Sgwr			 */
3561.2Sgwr			mon_printf("obio: no mapping for pa=0x%x\n",
3571.1Sgwr			    prom_mappings[i].pa);
3581.2Sgwr			sunmon_abort();  /* Ancient PROM? */
3591.1Sgwr		}
3601.1Sgwr	}
3611.1Sgwr}
3621.1Sgwr
3631.1Sgwr
3641.1Sgwr/*
3651.1Sgwr * Find mappings for devices that are needed before autoconfiguration.
3661.1Sgwr * We first look for and record any useful PROM mappings, then call
3671.1Sgwr * the "init" functions for drivers that we need to use before the
3681.1Sgwr * normal autoconfiguration calls configure().  Warning: this is
3691.1Sgwr * called before pmap_bootstrap, so no allocation allowed!
3701.1Sgwr */
3711.34Stsutsuivoid
3721.23Schsobio_init(void)
3731.1Sgwr{
3741.30Stsutsui
3751.1Sgwr	save_prom_mappings();
3761.1Sgwr	make_required_mappings();
3771.1Sgwr
3781.9Sgwr	enable_init();
3791.1Sgwr
3801.5Sgwr	/*
3811.5Sgwr	 * Find the interrupt reg mapping and turn off the
3821.5Sgwr	 * interrupts, otherwise the PROM clock interrupt
3831.5Sgwr	 * would poll the zs and toggle some LEDs...
3841.5Sgwr	 */
3851.1Sgwr	intreg_init();
3861.1Sgwr}
3871.27Stsutsui
3881.27Stsutsuiint
3891.27Stsutsuiobio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
3901.27Stsutsui    bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
3911.27Stsutsui{
3921.27Stsutsui	struct obio_softc *sc = t->cookie;
3931.27Stsutsui
3941.27Stsutsui	return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
3951.27Stsutsui	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
3961.27Stsutsui}
3971.27Stsutsui
3981.27Stsutsuipaddr_t
3991.27Stsutsuiobio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
4001.27Stsutsui    int prot, int flags)
4011.27Stsutsui{
4021.27Stsutsui	struct obio_softc *sc = t->cookie;
4031.27Stsutsui
4041.27Stsutsui	return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off, prot,
4051.27Stsutsui	    flags);
4061.27Stsutsui}
4071.29Stsutsui
4081.29Stsutsuistatic int
4091.29Stsutsuiobio_dmamap_load(bus_dma_tag_t t, bus_dmamap_t map, void *buf,
4101.29Stsutsui    bus_size_t buflen, struct proc *p, int flags)
4111.29Stsutsui{
4121.29Stsutsui	int error;
4131.29Stsutsui
4141.29Stsutsui	error = _bus_dmamap_load(t, map, buf, buflen, p, flags);
4151.29Stsutsui	if (error == 0)
4161.29Stsutsui		map->dm_segs[0].ds_addr &= DVMA_OBIO_SLAVE_MASK;
4171.29Stsutsui	return error;
4181.29Stsutsui}
419