obio.c revision 1.2
11.2Sgwr/*	$NetBSD: obio.c,v 1.2 1997/01/25 21:48:49 gwr 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.1Sgwr
391.1Sgwr#include <sys/param.h>
401.1Sgwr#include <sys/systm.h>
411.1Sgwr#include <sys/device.h>
421.1Sgwr
431.1Sgwr#include <machine/autoconf.h>
441.1Sgwr#include <machine/pte.h>
451.1Sgwr#include <machine/mon.h>
461.1Sgwr#include <machine/obio.h>
471.1Sgwr
481.2Sgwr#include <sun3/sun3/sunmon.h>
491.2Sgwr
501.1Sgwrshort *enable_reg;
511.1Sgwr
521.1Sgwrstatic int  obio_match __P((struct device *, struct cfdata *, void *));
531.1Sgwrstatic void obio_attach __P((struct device *, struct device *, void *));
541.1Sgwrstatic int  obio_print __P((void *, const char *parentname));
551.1Sgwrstatic int	obio_submatch __P((struct device *, struct cfdata *, void *));
561.1Sgwr
571.1Sgwrstruct cfattach obio_ca = {
581.1Sgwr	sizeof(struct device), obio_match, obio_attach
591.1Sgwr};
601.1Sgwr
611.1Sgwrstruct cfdriver obio_cd = {
621.1Sgwr	NULL, "obio", DV_DULL
631.1Sgwr};
641.1Sgwr
651.1Sgwrstatic int
661.1Sgwrobio_match(parent, cf, aux)
671.1Sgwr	struct device *parent;
681.1Sgwr	struct cfdata *cf;
691.1Sgwr	void *aux;
701.1Sgwr{
711.1Sgwr	struct confargs *ca = aux;
721.1Sgwr
731.1Sgwr	if (ca->ca_bustype != BUS_OBIO)
741.1Sgwr		return (0);
751.1Sgwr	return(1);
761.1Sgwr}
771.1Sgwr
781.1Sgwr/*
791.1Sgwr * We need some control over the order of attachment on OBIO,
801.1Sgwr * and all OBIO device addresses are known and fixed foerver.
811.1Sgwr * Therefore, this uses a list of addresses to attach.
821.1Sgwr * XXX - Any other way to control search/attach order?
831.1Sgwr */
841.1Sgwrstatic int obio_alist[] = {
851.1Sgwr	OBIO_ZS_KBD_MS,
861.1Sgwr	OBIO_ZS_TTY_AB,
871.1Sgwr
881.1Sgwr	OBIO_EEPROM,	/* the next two are part of the eeprom */
891.1Sgwr	OBIO_IDPROM2,	/* 3/80 only */
901.1Sgwr	OBIO_CLOCK2,	/* 3/80 only */
911.1Sgwr
921.1Sgwr	OBIO_CLOCK1,	/* 3/470 only */
931.1Sgwr
941.1Sgwr	/* These are all in the same page - could be just one driver... */
951.1Sgwr	OBIO_ENABLEREG,
961.1Sgwr	OBIO_BUSERRREG,
971.1Sgwr	OBIO_DIAGREG,
981.1Sgwr	OBIO_IDPROM1,
991.1Sgwr	OBIO_MEMREG,
1001.1Sgwr	OBIO_INTERREG,
1011.1Sgwr
1021.1Sgwr	OBIO_P4_REG,
1031.1Sgwr	OBIO_IOMMU,
1041.1Sgwr
1051.1Sgwr	OBIO_INTEL_ETHER,
1061.1Sgwr	OBIO_LANCE_ETHER,
1071.1Sgwr
1081.1Sgwr	/* ...todo... */
1091.1Sgwr	OBIO_FDC,
1101.1Sgwr	OBIO_PRINTER_PORT,
1111.1Sgwr};
1121.1Sgwr#define	OBIO_ALIST_LEN (sizeof(obio_alist) / \
1131.1Sgwr						sizeof(obio_alist[0]))
1141.1Sgwr
1151.1Sgwrstatic void
1161.1Sgwrobio_attach(parent, self, aux)
1171.1Sgwr	struct device *parent;
1181.1Sgwr	struct device *self;
1191.1Sgwr	void *aux;
1201.1Sgwr{
1211.1Sgwr	struct confargs *ca = aux;
1221.1Sgwr	int	i;
1231.1Sgwr
1241.1Sgwr	printf("\n");
1251.1Sgwr
1261.1Sgwr	/* Configure these in order of address. */
1271.1Sgwr	for (i = 0; i < OBIO_ALIST_LEN; i++) {
1281.1Sgwr		/* We know ca_bustype == BUS_OBIO */
1291.1Sgwr		ca->ca_paddr = obio_alist[i];
1301.1Sgwr		ca->ca_intpri = -1;
1311.1Sgwr		ca->ca_intvec = -1;
1321.1Sgwr		(void) config_found_sm(self, ca, obio_print, obio_submatch);
1331.1Sgwr	}
1341.1Sgwr}
1351.1Sgwr
1361.1Sgwr/*
1371.1Sgwr * Print out the confargs.  The (parent) name is non-NULL
1381.1Sgwr * when there was no match found by config_found().
1391.1Sgwr */
1401.1Sgwrstatic int
1411.1Sgwrobio_print(args, name)
1421.1Sgwr	void *args;
1431.1Sgwr	const char *name;
1441.1Sgwr{
1451.1Sgwr	struct confargs *ca = args;
1461.1Sgwr
1471.1Sgwr	/* Be quiet about empty OBIO locations. */
1481.1Sgwr	if (name)
1491.1Sgwr		return(QUIET);
1501.1Sgwr
1511.1Sgwr	if (ca->ca_paddr != -1)
1521.1Sgwr		printf(" addr 0x%x", ca->ca_paddr);
1531.1Sgwr	if (ca->ca_intpri != -1)
1541.1Sgwr		printf(" level %d", ca->ca_intpri);
1551.1Sgwr
1561.1Sgwr	return(UNCONF);
1571.1Sgwr}
1581.1Sgwr
1591.1Sgwrint
1601.1Sgwrobio_submatch(parent, cf, aux)
1611.1Sgwr	struct device *parent;
1621.1Sgwr	struct cfdata *cf;
1631.1Sgwr	void *aux;
1641.1Sgwr{
1651.1Sgwr	struct confargs *ca = aux;
1661.1Sgwr	cfmatch_t submatch;
1671.1Sgwr
1681.1Sgwr	/*
1691.1Sgwr	 * Default addresses are mostly useless for OBIO.
1701.1Sgwr	 * The address assignments are fixed for all time,
1711.1Sgwr	 * so our config files might as well reflect that.
1721.1Sgwr	 */
1731.1Sgwr#ifdef	DIAGNOSTIC
1741.1Sgwr	if (cf->cf_paddr == -1)
1751.1Sgwr		panic("obio_submatch: invalid address for: %s%d\n",
1761.1Sgwr			cf->cf_driver->cd_name, cf->cf_unit);
1771.1Sgwr#endif
1781.1Sgwr
1791.1Sgwr	/* This enforces exact address match. */
1801.1Sgwr	if (cf->cf_paddr != ca->ca_paddr)
1811.1Sgwr		return 0;
1821.1Sgwr
1831.1Sgwr	/* Now call the match function of the potential child. */
1841.1Sgwr	submatch = cf->cf_attach->ca_match;
1851.1Sgwr	if (submatch == NULL)
1861.1Sgwr		panic("obio_submatch: no match function for: %s\n",
1871.1Sgwr			  cf->cf_driver->cd_name);
1881.1Sgwr
1891.1Sgwr	return ((*submatch)(parent, cf, aux));
1901.1Sgwr}
1911.1Sgwr
1921.1Sgwr
1931.1Sgwr/*****************************************************************/
1941.1Sgwr
1951.1Sgwr/*
1961.1Sgwr * This is our record of "interesting" OBIO mappings that
1971.1Sgwr * the PROM has left in the virtual space reserved for it.
1981.1Sgwr * Each non-null array element holds the virtual address
1991.1Sgwr * of an OBIO mapping where the OBIO address mapped is:
2001.1Sgwr *     (array_index * SAVE_INCR)
2011.1Sgwr * and the length of the mapping is one page.
2021.1Sgwr */
2031.1Sgwrstatic struct prom_map {
2041.1Sgwr	vm_offset_t pa, va;
2051.1Sgwr} prom_mappings[] = {
2061.1Sgwr	{ OBIO_ZS_KBD_MS, 0 },	/* Keyboard and Mouse */
2071.1Sgwr	{ OBIO_ZS_TTY_AB, 0 },	/* Serial Ports */
2081.1Sgwr	{ OBIO_EEPROM,    0 },	/* EEPROM/IDPROM/clock */
2091.1Sgwr	{ OBIO_ENABLEREG, 0 },	/* regs: Sys ENA, Bus ERR, etc. */
2101.1Sgwr};
2111.1Sgwr#define PROM_MAP_CNT (sizeof(prom_mappings) / \
2121.1Sgwr		      sizeof(prom_mappings[0]))
2131.1Sgwr
2141.1Sgwr/*
2151.1Sgwr * Find a virtual address for a device at physical address 'pa'.
2161.1Sgwr * If one is found among the mappings already made by the PROM
2171.1Sgwr * at power-up time, use it.  Otherwise return 0 as a sign that
2181.1Sgwr * a mapping will have to be created.
2191.1Sgwr */
2201.1Sgwrcaddr_t
2211.1Sgwrobio_find_mapping(int pa, int size)
2221.1Sgwr{
2231.1Sgwr	int i, off;
2241.1Sgwr
2251.1Sgwr	if (size >= NBPG)
2261.1Sgwr		return (caddr_t)0;
2271.1Sgwr
2281.1Sgwr	off = pa & PGOFSET;
2291.1Sgwr	pa -= off;
2301.1Sgwr
2311.1Sgwr	for (i = 0; i < PROM_MAP_CNT; i++) {
2321.1Sgwr		if (pa == prom_mappings[i].pa) {
2331.1Sgwr			return ((caddr_t)(prom_mappings[i].va + off));
2341.1Sgwr		}
2351.1Sgwr	}
2361.1Sgwr	return (caddr_t)0;
2371.1Sgwr}
2381.1Sgwr
2391.1Sgwr/*
2401.1Sgwr * Search the PROM page tables for OBIO mappings that
2411.1Sgwr * we would like to borrow.
2421.1Sgwr */
2431.1Sgwrstatic void
2441.1Sgwrsave_prom_mappings __P((void))
2451.1Sgwr{
2461.1Sgwr	int *mon_pte;
2471.1Sgwr	vm_offset_t va, pa;
2481.1Sgwr	int i;
2491.1Sgwr
2501.1Sgwr	/* Note: mon_ctbl[0] maps MON_KDB_START */
2511.1Sgwr	mon_pte = *romVectorPtr->monptaddr;
2521.1Sgwr
2531.1Sgwr	for (va = MON_KDB_START; va < MONEND;
2541.1Sgwr		 va += NBPG, mon_pte++)
2551.1Sgwr	{
2561.1Sgwr		/* Is this a valid mapping to OBIO? */
2571.1Sgwr		/* XXX - Some macros would be nice... */
2581.1Sgwr		if ((*mon_pte & 0xF0000003) != 0x60000001)
2591.1Sgwr			continue;
2601.1Sgwr
2611.1Sgwr		/* Yes it is.  Is this a mapping we want? */
2621.1Sgwr		pa = *mon_pte & MMU_SHORT_PTE_BASEADDR;
2631.1Sgwr		for (i = 0; i < PROM_MAP_CNT; i++) {
2641.1Sgwr			if (pa != prom_mappings[i].pa)
2651.1Sgwr				continue;
2661.1Sgwr			/* Yes, we want it.  Save the va? */
2671.1Sgwr			if (prom_mappings[i].va == 0) {
2681.1Sgwr				prom_mappings[i].va = va;
2691.1Sgwr			}
2701.1Sgwr		}
2711.1Sgwr	}
2721.1Sgwr
2731.1Sgwr}
2741.1Sgwr
2751.1Sgwr/*
2761.1Sgwr * These are all the OBIO address that are required early in
2771.1Sgwr * the life of the kernel.  All are less than one page long.
2781.1Sgwr * This function should make any required mappings that we
2791.1Sgwr * were not able to find among the PROM monitor's mappings.
2801.1Sgwr */
2811.1Sgwrstatic void
2821.1Sgwrmake_required_mappings __P((void))
2831.1Sgwr{
2841.1Sgwr	int i;
2851.1Sgwr
2861.1Sgwr	for (i = 0; i < PROM_MAP_CNT; i++) {
2871.1Sgwr		if (prom_mappings[i].va == 0) {
2881.1Sgwr			/*
2891.1Sgwr			 * Actually, the PROM always has all the
2901.1Sgwr			 * "required" mappings we need, (smile)
2911.1Sgwr			 * but this makes sure that is true.
2921.1Sgwr			 */
2931.2Sgwr			mon_printf("obio: no mapping for pa=0x%x\n",
2941.1Sgwr			    prom_mappings[i].pa);
2951.2Sgwr			sunmon_abort();  /* Ancient PROM? */
2961.1Sgwr		}
2971.1Sgwr	}
2981.1Sgwr}
2991.1Sgwr
3001.1Sgwr
3011.1Sgwr/*
3021.1Sgwr * Find mappings for devices that are needed before autoconfiguration.
3031.1Sgwr * We first look for and record any useful PROM mappings, then call
3041.1Sgwr * the "init" functions for drivers that we need to use before the
3051.1Sgwr * normal autoconfiguration calls configure().  Warning: this is
3061.1Sgwr * called before pmap_bootstrap, so no allocation allowed!
3071.1Sgwr */
3081.1Sgwrvoid
3091.1Sgwrobio_init()
3101.1Sgwr{
3111.1Sgwr	save_prom_mappings();
3121.1Sgwr	make_required_mappings();
3131.1Sgwr
3141.1Sgwr	/* Init drivers that use the required OBIO mappings. */
3151.1Sgwr	idprom_init();
3161.1Sgwr	eeprom_init();
3171.1Sgwr	zs_init();
3181.1Sgwr
3191.1Sgwr	enable_reg = (short*) obio_find_mapping(OBIO_ENABLEREG, 2);
3201.1Sgwr	intreg_init();
3211.1Sgwr	clock_init();
3221.1Sgwr}
3231.1Sgwr
3241.1Sgwrcaddr_t
3251.1Sgwrobio_alloc(obio_addr, obio_size)
3261.1Sgwr	int obio_addr, obio_size;
3271.1Sgwr{
3281.1Sgwr	caddr_t cp;
3291.1Sgwr
3301.1Sgwr	cp = obio_find_mapping((vm_offset_t)obio_addr, obio_size);
3311.1Sgwr	if (cp) return (cp);
3321.1Sgwr
3331.1Sgwr	cp = bus_mapin(BUS_OBIO, obio_addr, obio_size);
3341.1Sgwr	return (cp);
3351.1Sgwr}
336