obio.c revision 1.27
11.27Stsutsui/*	$NetBSD: obio.c,v 1.27 2006/10/01 03:53:27 tsutsui 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 * 3. All advertising materials mentioning features or use of this software
191.1Sgwr *    must display the following acknowledgement:
201.1Sgwr *        This product includes software developed by the NetBSD
211.1Sgwr *        Foundation, Inc. and its contributors.
221.1Sgwr * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Sgwr *    contributors may be used to endorse or promote products derived
241.1Sgwr *    from this software without specific prior written permission.
251.1Sgwr *
261.1Sgwr * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Sgwr * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Sgwr * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Sgwr * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Sgwr * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Sgwr * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Sgwr * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Sgwr * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Sgwr * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Sgwr * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Sgwr * POSSIBILITY OF SUCH DAMAGE.
371.1Sgwr */
381.22Slukem
391.22Slukem#include <sys/cdefs.h>
401.27Stsutsui__KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.27 2006/10/01 03:53:27 tsutsui Exp $");
411.1Sgwr
421.1Sgwr#include <sys/param.h>
431.1Sgwr#include <sys/systm.h>
441.1Sgwr#include <sys/device.h>
451.1Sgwr
461.21Sthorpej#include <uvm/uvm_extern.h>
471.21Sthorpej
481.1Sgwr#include <machine/autoconf.h>
491.27Stsutsui#include <machine/bus.h>
501.1Sgwr#include <machine/mon.h>
511.3Sgwr#include <machine/pte.h>
521.2Sgwr
531.9Sgwr#include <sun3/sun3/machdep.h>
541.9Sgwr#include <sun3/sun3x/obio.h>
551.1Sgwr
561.23Schsstatic int	obio_match(struct device *, struct cfdata *, void *);
571.23Schsstatic void	obio_attach(struct device *, struct device *, void *);
581.23Schsstatic int	obio_print(void *, const char *);
591.24Sdrochnerstatic int	obio_submatch(struct device *, struct cfdata *,
601.25Sdrochner			      const int *, void *);
611.1Sgwr
621.27Stsutsuistruct obio_softc {
631.27Stsutsui	struct device	sc_dev;
641.27Stsutsui	bus_space_tag_t	sc_bustag;
651.27Stsutsui	bus_dma_tag_t	sc_dmatag;
661.27Stsutsui};
671.27StsutsuiCFATTACH_DECL(obio, sizeof(struct obio_softc),
681.20Sthorpej    obio_match, obio_attach, NULL, NULL);
691.1Sgwr
701.27Stsutsuistatic int obio_attached;
711.27Stsutsui
721.27Stsutsuistatic int obio_bus_map(bus_space_tag_t, bus_type_t, bus_addr_t, bus_size_t,
731.27Stsutsui    int, vaddr_t, bus_space_handle_t *);
741.27Stsutsuistatic paddr_t obio_bus_mmap(bus_space_tag_t, bus_type_t, bus_addr_t,
751.27Stsutsui    off_t, int, int);
761.27Stsutsui
771.27Stsutsuistatic struct sun68k_bus_space_tag obio_space_tag = {
781.27Stsutsui	NULL,				/* cookie */
791.27Stsutsui	NULL,				/* parent bus space tag */
801.27Stsutsui	obio_bus_map,			/* bus_space_map */
811.27Stsutsui	NULL,				/* bus_space_unmap */
821.27Stsutsui	NULL,				/* bus_space_subregion */
831.27Stsutsui	NULL,				/* bus_space_barrier */
841.27Stsutsui	obio_bus_mmap,			/* bus_space_mmap */
851.27Stsutsui	NULL,				/* bus_intr_establish */
861.27Stsutsui	NULL,				/* bus_space_peek_N */
871.27Stsutsui	NULL				/* bus_space_poke_N */
881.27Stsutsui};
891.27Stsutsui
901.23Schsstatic int
911.23Schsobio_match(struct device *parent, struct cfdata *cf, void *aux)
921.1Sgwr{
931.1Sgwr	struct confargs *ca = aux;
941.1Sgwr
951.27Stsutsui	if (obio_attached)
961.27Stsutsui		return 0;
971.27Stsutsui
981.1Sgwr	if (ca->ca_bustype != BUS_OBIO)
991.27Stsutsui		return 0;
1001.27Stsutsui
1011.27Stsutsui	if (ca->ca_name != NULL && strcmp(cf->cf_name, ca->ca_name) != 0)
1021.27Stsutsui		return 0;
1031.27Stsutsui
1041.27Stsutsui	return 1;
1051.1Sgwr}
1061.1Sgwr
1071.1Sgwr/*
1081.10Sgwr * We need control over the order of attachment on OBIO,
1091.10Sgwr * so do "direct" style autoconfiguration with addresses
1101.10Sgwr * from the list below.  OBIO addresses are fixed forever.
1111.5Sgwr *
1121.5Sgwr * Warning: This whole list is very carefully ordered!
1131.5Sgwr * In general, anything not already shown here should
1141.5Sgwr * be added at or near the end.
1151.1Sgwr */
1161.27Stsutsuistatic paddr_t obio_alist[] = {
1171.5Sgwr
1181.10Sgwr	/* This is used by the Ethernet and SCSI drivers. */
1191.10Sgwr	OBIO_IOMMU,
1201.10Sgwr
1211.5Sgwr	/* Misc. registers - needed by many things */
1221.5Sgwr	OBIO_ENABLEREG,
1231.5Sgwr	OBIO_BUSERRREG,
1241.5Sgwr	OBIO_DIAGREG,	/* leds.c */
1251.5Sgwr	OBIO_IDPROM1,	/* idprom.c (3/470) */
1261.5Sgwr	OBIO_MEMREG,	/* memerr.c */
1271.5Sgwr	OBIO_INTERREG,	/* intreg.c */
1281.5Sgwr
1291.5Sgwr	/* Zilog UARTs */
1301.1Sgwr	OBIO_ZS_KBD_MS,
1311.1Sgwr	OBIO_ZS_TTY_AB,
1321.1Sgwr
1331.5Sgwr	/* eeprom.c */
1341.5Sgwr	OBIO_EEPROM,
1351.1Sgwr
1361.5Sgwr	/* Note: This must come after OBIO_IDPROM1. */
1371.5Sgwr	OBIO_IDPROM2,	/* idprom.c (3/80) */
1381.1Sgwr
1391.5Sgwr	/* Note: Must probe for the Intersil first! */
1401.5Sgwr	OBIO_CLOCK1,	/* clock.c (3/470) */
1411.5Sgwr	OBIO_CLOCK2,	/* clock.c (3/80) */
1421.4Sgwr
1431.1Sgwr	OBIO_INTEL_ETHER,
1441.1Sgwr	OBIO_LANCE_ETHER,
1451.1Sgwr
1461.11Sgwr	/* Need esp DMA before SCSI. */
1471.11Sgwr	OBIO_EMULEX_DMA,  /* 3/80 only */
1481.4Sgwr	OBIO_EMULEX_SCSI, /* 3/80 only */
1491.4Sgwr
1501.10Sgwr	/* Memory subsystem */
1511.10Sgwr	OBIO_PCACHE_TAGS,
1521.10Sgwr	OBIO_ECCPARREG,
1531.10Sgwr	OBIO_IOC_TAGS,
1541.10Sgwr	OBIO_IOC_FLUSH,
1551.10Sgwr
1561.10Sgwr	OBIO_FDC,	/* floppy disk (3/80) */
1571.12Stsutsui	OBIO_PRINTER_PORT, /* printer port (3/80) */
1581.1Sgwr};
1591.4Sgwr#define OBIO_ALIST_LEN (sizeof(obio_alist) / \
1601.4Sgwr                        sizeof(obio_alist[0]))
1611.1Sgwr
1621.23Schsstatic void
1631.23Schsobio_attach(struct device *parent, struct device *self, void *aux)
1641.1Sgwr{
1651.1Sgwr	struct confargs *ca = aux;
1661.27Stsutsui	struct obio_softc *sc = (void *)self;
1671.27Stsutsui	struct confargs oba;
1681.27Stsutsui	int i;
1691.27Stsutsui
1701.27Stsutsui	obio_attached = 1;
1711.1Sgwr
1721.1Sgwr	printf("\n");
1731.1Sgwr
1741.27Stsutsui	sc->sc_bustag = ca->ca_bustag;
1751.27Stsutsui	sc->sc_dmatag = ca->ca_dmatag;
1761.27Stsutsui
1771.27Stsutsui	obio_space_tag.cookie = sc;
1781.27Stsutsui	obio_space_tag.parent = sc->sc_bustag;
1791.27Stsutsui
1801.27Stsutsui	oba = *ca;
1811.27Stsutsui	oba.ca_bustag = &obio_space_tag;
1821.27Stsutsui
1831.5Sgwr	/* Configure these in the order listed above. */
1841.1Sgwr	for (i = 0; i < OBIO_ALIST_LEN; i++) {
1851.5Sgwr		/* Our parent set ca->ca_bustype already. */
1861.27Stsutsui		oba.ca_paddr = obio_alist[i];
1871.5Sgwr		/* These are filled-in by obio_submatch. */
1881.27Stsutsui		oba.ca_intpri = -1;
1891.27Stsutsui		oba.ca_intvec = -1;
1901.27Stsutsui		(void)config_found_sm_loc(self, "obio", NULL, &oba, obio_print,
1911.27Stsutsui		    obio_submatch);
1921.1Sgwr	}
1931.1Sgwr}
1941.1Sgwr
1951.1Sgwr/*
1961.1Sgwr * Print out the confargs.  The (parent) name is non-NULL
1971.1Sgwr * when there was no match found by config_found().
1981.1Sgwr */
1991.23Schsstatic int
2001.23Schsobio_print(void *args, const char *name)
2011.1Sgwr{
2021.1Sgwr
2031.1Sgwr	/* Be quiet about empty OBIO locations. */
2041.1Sgwr	if (name)
2051.1Sgwr		return(QUIET);
2061.1Sgwr
2071.5Sgwr	/* Otherwise do the usual. */
2081.5Sgwr	return(bus_print(args, name));
2091.1Sgwr}
2101.1Sgwr
2111.23Schsint
2121.24Sdrochnerobio_submatch(struct device *parent, struct cfdata *cf,
2131.25Sdrochner	      const int *ldesc, void *aux)
2141.1Sgwr{
2151.1Sgwr	struct confargs *ca = aux;
2161.1Sgwr
2171.1Sgwr	/*
2181.5Sgwr	 * Note that a defaulted address locator can never match
2191.5Sgwr	 * the value of ca->ca_paddr set by the obio_attach loop.
2201.5Sgwr	 * Without this diagnostic, any device with a defaulted
2211.5Sgwr	 * address locator would always be silently unmatched.
2221.5Sgwr	 * Therefore, just disallow default addresses on OBIO.
2231.1Sgwr	 */
2241.1Sgwr#ifdef	DIAGNOSTIC
2251.1Sgwr	if (cf->cf_paddr == -1)
2261.17Sprovos		panic("obio_submatch: invalid address for: %s%d",
2271.15Sthorpej			cf->cf_name, cf->cf_unit);
2281.1Sgwr#endif
2291.1Sgwr
2301.5Sgwr	/*
2311.5Sgwr	 * Note that obio_attach calls config_found_sm() with
2321.5Sgwr	 * this function as the "submatch" and ca->ca_paddr
2331.5Sgwr	 * set to each of the possible OBIO locations, so we
2341.5Sgwr	 * want to reject any unmatched address here.
2351.5Sgwr	 */
2361.1Sgwr	if (cf->cf_paddr != ca->ca_paddr)
2371.1Sgwr		return 0;
2381.1Sgwr
2391.5Sgwr	/*
2401.5Sgwr	 * Copy the locators into our confargs for the child.
2411.5Sgwr	 * Note: ca->ca_bustype was set by our parent driver
2421.5Sgwr	 * (mainbus) and ca->ca_paddr was set by obio_attach.
2431.5Sgwr	 */
2441.5Sgwr	ca->ca_intpri = cf->cf_intpri;
2451.5Sgwr	ca->ca_intvec = cf->cf_intvec;
2461.5Sgwr
2471.1Sgwr	/* Now call the match function of the potential child. */
2481.16Sthorpej	return (config_match(parent, cf, aux));
2491.1Sgwr}
2501.1Sgwr
2511.1Sgwr
2521.1Sgwr/*****************************************************************/
2531.1Sgwr
2541.1Sgwr/*
2551.1Sgwr * This is our record of "interesting" OBIO mappings that
2561.1Sgwr * the PROM has left in the virtual space reserved for it.
2571.10Sgwr * Each row of the array holds a virtual address and the
2581.10Sgwr * physical address it maps to (if found).
2591.1Sgwr */
2601.1Sgwrstatic struct prom_map {
2611.13Stsutsui	paddr_t pa;
2621.13Stsutsui	vaddr_t va;
2631.1Sgwr} prom_mappings[] = {
2641.5Sgwr	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
2651.1Sgwr	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
2661.1Sgwr	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
2671.1Sgwr	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
2681.1Sgwr};
2691.1Sgwr#define PROM_MAP_CNT (sizeof(prom_mappings) / \
2701.1Sgwr		      sizeof(prom_mappings[0]))
2711.1Sgwr
2721.1Sgwr/*
2731.1Sgwr * Find a virtual address for a device at physical address 'pa'.
2741.1Sgwr * If one is found among the mappings already made by the PROM
2751.1Sgwr * at power-up time, use it.  Otherwise return 0 as a sign that
2761.1Sgwr * a mapping will have to be created.
2771.1Sgwr */
2781.1Sgwrcaddr_t
2791.14Stsutsuiobio_find_mapping(paddr_t pa, psize_t sz)
2801.1Sgwr{
2811.14Stsutsui	int i;
2821.14Stsutsui	vsize_t off;
2831.1Sgwr
2841.1Sgwr	off = pa & PGOFSET;
2851.1Sgwr	pa -= off;
2861.10Sgwr	sz += off;
2871.10Sgwr
2881.10Sgwr	/* The saved mappings are all one page long. */
2891.21Sthorpej	if (sz > PAGE_SIZE)
2901.10Sgwr		return (caddr_t)0;
2911.1Sgwr
2921.10Sgwr	/* Linear search for it.  The list is short. */
2931.1Sgwr	for (i = 0; i < PROM_MAP_CNT; i++) {
2941.1Sgwr		if (pa == prom_mappings[i].pa) {
2951.1Sgwr			return ((caddr_t)(prom_mappings[i].va + off));
2961.1Sgwr		}
2971.1Sgwr	}
2981.1Sgwr	return (caddr_t)0;
2991.1Sgwr}
3001.1Sgwr
3011.1Sgwr/*
3021.27Stsutsui * The similar function with the above but used by bus_space(9)
3031.27Stsutsui */
3041.27Stsutsuiint
3051.27Stsutsuifind_prom_map(bus_addr_t pa, bus_type_t iospace, int sz,
3061.27Stsutsui    bus_space_handle_t *hp)
3071.27Stsutsui{
3081.27Stsutsui	int i;
3091.27Stsutsui	vsize_t off;
3101.27Stsutsui
3111.27Stsutsui	off = pa & PGOFSET;
3121.27Stsutsui	pa -= off;
3131.27Stsutsui	sz += off;
3141.27Stsutsui
3151.27Stsutsui	/* The saved mappings are all one page long. */
3161.27Stsutsui	if (sz > PAGE_SIZE)
3171.27Stsutsui		return EINVAL;
3181.27Stsutsui
3191.27Stsutsui	/* Linear search for it.  The list is short. */
3201.27Stsutsui	for (i = 0; i < PROM_MAP_CNT; i++) {
3211.27Stsutsui		if (pa == prom_mappings[i].pa) {
3221.27Stsutsui			*hp = prom_mappings[i].va + off;
3231.27Stsutsui			return 0;
3241.27Stsutsui		}
3251.27Stsutsui	}
3261.27Stsutsui	return ENOENT;
3271.27Stsutsui}
3281.27Stsutsui
3291.27Stsutsui/*
3301.1Sgwr * Search the PROM page tables for OBIO mappings that
3311.1Sgwr * we would like to borrow.
3321.1Sgwr */
3331.1Sgwrstatic void
3341.23Schssave_prom_mappings(void)
3351.1Sgwr{
3361.1Sgwr	int *mon_pte;
3371.13Stsutsui	vaddr_t va;
3381.13Stsutsui	paddr_t pa;
3391.1Sgwr	int i;
3401.1Sgwr
3411.9Sgwr	/* Note: mon_ctbl[0] maps SUN3X_MON_KDB_BASE */
3421.1Sgwr	mon_pte = *romVectorPtr->monptaddr;
3431.1Sgwr
3441.9Sgwr	for (va = SUN3X_MON_KDB_BASE; va < SUN3X_MONEND;
3451.21Sthorpej		 va += PAGE_SIZE, mon_pte++)
3461.1Sgwr	{
3471.1Sgwr		/* Is this a valid mapping to OBIO? */
3481.1Sgwr		/* XXX - Some macros would be nice... */
3491.1Sgwr		if ((*mon_pte & 0xF0000003) != 0x60000001)
3501.1Sgwr			continue;
3511.1Sgwr
3521.1Sgwr		/* Yes it is.  Is this a mapping we want? */
3531.1Sgwr		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
3541.1Sgwr		for (i = 0; i < PROM_MAP_CNT; i++) {
3551.1Sgwr			if (pa != prom_mappings[i].pa)
3561.1Sgwr				continue;
3571.1Sgwr			/* Yes, we want it.  Save the va? */
3581.1Sgwr			if (prom_mappings[i].va == 0) {
3591.1Sgwr				prom_mappings[i].va = va;
3601.1Sgwr			}
3611.1Sgwr		}
3621.1Sgwr	}
3631.1Sgwr
3641.1Sgwr}
3651.1Sgwr
3661.1Sgwr/*
3671.1Sgwr * These are all the OBIO address that are required early in
3681.1Sgwr * the life of the kernel.  All are less than one page long.
3691.1Sgwr * This function should make any required mappings that we
3701.1Sgwr * were not able to find among the PROM monitor's mappings.
3711.1Sgwr */
3721.1Sgwrstatic void
3731.23Schsmake_required_mappings(void)
3741.1Sgwr{
3751.1Sgwr	int i;
3761.1Sgwr
3771.1Sgwr	for (i = 0; i < PROM_MAP_CNT; i++) {
3781.1Sgwr		if (prom_mappings[i].va == 0) {
3791.1Sgwr			/*
3801.1Sgwr			 * Actually, the PROM always has all the
3811.1Sgwr			 * "required" mappings we need, (smile)
3821.1Sgwr			 * but this makes sure that is true.
3831.1Sgwr			 */
3841.2Sgwr			mon_printf("obio: no mapping for pa=0x%x\n",
3851.1Sgwr			    prom_mappings[i].pa);
3861.2Sgwr			sunmon_abort();  /* Ancient PROM? */
3871.1Sgwr		}
3881.1Sgwr	}
3891.1Sgwr}
3901.1Sgwr
3911.1Sgwr
3921.1Sgwr/*
3931.1Sgwr * Find mappings for devices that are needed before autoconfiguration.
3941.1Sgwr * We first look for and record any useful PROM mappings, then call
3951.1Sgwr * the "init" functions for drivers that we need to use before the
3961.1Sgwr * normal autoconfiguration calls configure().  Warning: this is
3971.1Sgwr * called before pmap_bootstrap, so no allocation allowed!
3981.1Sgwr */
3991.23Schsvoid
4001.23Schsobio_init(void)
4011.1Sgwr{
4021.1Sgwr	save_prom_mappings();
4031.1Sgwr	make_required_mappings();
4041.1Sgwr
4051.9Sgwr	enable_init();
4061.1Sgwr
4071.5Sgwr	/*
4081.5Sgwr	 * Find the interrupt reg mapping and turn off the
4091.5Sgwr	 * interrupts, otherwise the PROM clock interrupt
4101.5Sgwr	 * would poll the zs and toggle some LEDs...
4111.5Sgwr	 */
4121.1Sgwr	intreg_init();
4131.1Sgwr}
4141.27Stsutsui
4151.27Stsutsuiint
4161.27Stsutsuiobio_bus_map(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr,
4171.27Stsutsui    bus_size_t size, int flags, vaddr_t vaddr, bus_space_handle_t *hp)
4181.27Stsutsui{
4191.27Stsutsui	struct obio_softc *sc = t->cookie;
4201.27Stsutsui
4211.27Stsutsui	return bus_space_map2(sc->sc_bustag, PMAP_OBIO, paddr, size,
4221.27Stsutsui	    flags | _SUN68K_BUS_MAP_USE_PROM, vaddr, hp);
4231.27Stsutsui}
4241.27Stsutsui
4251.27Stsutsuipaddr_t
4261.27Stsutsuiobio_bus_mmap(bus_space_tag_t t, bus_type_t btype, bus_addr_t paddr, off_t off,
4271.27Stsutsui    int prot, int flags)
4281.27Stsutsui{
4291.27Stsutsui	struct obio_softc *sc = t->cookie;
4301.27Stsutsui
4311.27Stsutsui	return bus_space_mmap2(sc->sc_bustag, PMAP_OBIO, paddr, off, prot,
4321.27Stsutsui	    flags);
4331.27Stsutsui}
434