isa_machdep.c revision 1.33
11.33Sad/*	$NetBSD: isa_machdep.c,v 1.33 2008/01/04 22:13:56 ad Exp $	*/
21.1Stakemura
31.16Such/*-
41.16Such * Copyright (c) 1999 The NetBSD Foundation, Inc.
51.1Stakemura * All rights reserved.
61.1Stakemura *
71.16Such * This code is derived from software contributed to The NetBSD Foundation
81.16Such * by UCHIYAMA Yasushi.
91.16Such *
101.1Stakemura * Redistribution and use in source and binary forms, with or without
111.1Stakemura * modification, are permitted provided that the following conditions
121.1Stakemura * are met:
131.1Stakemura * 1. Redistributions of source code must retain the above copyright
141.1Stakemura *    notice, this list of conditions and the following disclaimer.
151.16Such * 2. Redistributions in binary form must reproduce the above copyright
161.16Such *    notice, this list of conditions and the following disclaimer in the
171.16Such *    documentation and/or other materials provided with the distribution.
181.16Such * 3. All advertising materials mentioning features or use of this software
191.16Such *    must display the following acknowledgement:
201.16Such *        This product includes software developed by the NetBSD
211.16Such *        Foundation, Inc. and its contributors.
221.16Such * 4. Neither the name of The NetBSD Foundation nor the names of its
231.16Such *    contributors may be used to endorse or promote products derived
241.16Such *    from this software without specific prior written permission.
251.1Stakemura *
261.16Such * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.16Such * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.16Such * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.16Such * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.16Such * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.16Such * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.16Such * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.16Such * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.16Such * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.16Such * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.16Such * POSSIBILITY OF SUCH DAMAGE.
371.1Stakemura */
381.28Slukem
391.28Slukem#include <sys/cdefs.h>
401.33Sad__KERNEL_RCSID(0, "$NetBSD: isa_machdep.c,v 1.33 2008/01/04 22:13:56 ad Exp $");
411.16Such
421.16Such#include "opt_vr41xx.h"
431.16Such
441.1Stakemura#include <sys/param.h>
451.1Stakemura#include <sys/systm.h>
461.10Ssato#include <sys/reboot.h>
471.33Sad#include <sys/device.h>
481.1Stakemura
491.1Stakemura#include <dev/isa/isavar.h>
501.1Stakemura#include <dev/isa/isareg.h>
511.1Stakemura
521.1Stakemura#include <machine/platid.h>
531.1Stakemura#include <machine/platid_mask.h>
541.18Stakemura#include <machine/bus.h>
551.18Stakemura#include <machine/bus_space_hpcmips.h>
561.23Such#include <machine/debug.h>
571.1Stakemura
581.13Stakemura#include <dev/hpc/hpciovar.h>
591.13Stakemura
601.22Stakemura#include <hpcmips/vr/vripif.h>
611.1Stakemura
621.1Stakemura#include "locators.h"
631.1Stakemura
641.4Ssato#define VRISADEBUG
651.4Ssato
661.4Ssato#ifdef VRISADEBUG
671.4Ssato#ifndef VRISADEBUG_CONF
681.4Ssato#define VRISADEBUG_CONF 0
691.4Ssato#endif /* VRISADEBUG_CONF */
701.4Ssatoint vrisa_debug = VRISADEBUG_CONF;
711.4Ssato#define DPRINTF(arg) if (vrisa_debug) printf arg;
721.23Such#define DBITDISP(mask) if (vrisa_debug) dbg_bit_print(mask);
731.8Sjdolecek#define VPRINTF(arg) if (bootverbose || vrisa_debug) printf arg;
741.4Ssato#else /* VRISADEBUG */
751.4Ssato#define DPRINTF(arg)
761.23Such#define DBITDISP(mask)
771.8Sjdolecek#define VPRINTF(arg) if (bootverbose) printf arg;
781.4Ssato#endif /* VRISADEBUG */
791.4Ssato
801.13Stakemura/*
811.13Stakemura * intrrupt no. encoding:
821.13Stakemura *
831.13Stakemura * 0x0000000f ISA IRQ#
841.13Stakemura * 0x00ff0000 GPIO port#
851.13Stakemura * 0x01000000 interrupt signal hold/through	(1:hold/0:though)
861.13Stakemura * 0x02000000 interrupt detection level		(1:low /0:high	)
871.13Stakemura * 0x04000000 interrupt detection trigger	(1:edge/0:level	)
881.13Stakemura */
891.13Stakemura#define INTR_IRQ(i)	(((i)>> 0) & 0x0f)
901.13Stakemura#define INTR_PORT(i)	(((i)>>16) & 0xff)
911.13Stakemura#define INTR_MODE(i)	(((i)>>24) & 0x07)
921.13Stakemura#define INTR_NIRQS	16
931.13Stakemura
941.16Suchint	vrisabprint(void *, const char *);
951.16Suchint	vrisabmatch(struct device *, struct cfdata *, void *);
961.16Suchvoid	vrisabattach(struct device *, struct device *, void *);
971.1Stakemura
981.1Stakemurastruct vrisab_softc {
991.1Stakemura	struct device sc_dev;
1001.13Stakemura	hpcio_chip_t sc_hc;
1011.13Stakemura	int sc_intr_map[INTR_NIRQS]; /* ISA <-> GIU inerrupt line mapping */
1021.3Stakemura	struct hpcmips_isa_chipset sc_isa_ic;
1031.1Stakemura};
1041.1Stakemura
1051.27SthorpejCFATTACH_DECL(vrisab, sizeof(struct vrisab_softc),
1061.27Sthorpej    vrisabmatch, vrisabattach, NULL, NULL);
1071.1Stakemura
1081.1Stakemura#ifdef DEBUG_FIND_PCIC
1091.1Stakemura#include <mips/cpuregs.h>
1101.1Stakemura#warning DEBUG_FIND_PCIC
1111.16Suchstatic void __find_pcic(void);
1121.1Stakemura#endif
1131.1Stakemura
1141.11Ssato#ifdef DEBUG_FIND_COMPORT
1151.11Ssato#include <mips/cpuregs.h>
1161.11Ssato#include <dev/ic/ns16550reg.h>
1171.11Ssato#include <dev/ic/comreg.h>
1181.11Ssato#warning DEBUG_FIND_COMPORT
1191.16Suchstatic void __find_comport(void);
1201.11Ssato#endif
1211.11Ssato
1221.1Stakemuraint
1231.16Suchvrisabmatch(struct device *parent, struct cfdata *match, void *aux)
1241.1Stakemura{
1251.13Stakemura	struct hpcio_attach_args *haa = aux;
1261.1Stakemura	platid_mask_t mask;
1271.20Stakemura	int n;
1281.20Stakemura
1291.25Sthorpej	if (strcmp(haa->haa_busname, match->cf_name))
1301.16Such		return (0);
1311.16Such
1321.13Stakemura	if (match->cf_loc[HPCIOIFCF_PLATFORM] == HPCIOIFCF_PLATFORM_DEFAULT)
1331.16Such		return (1);
1341.16Such
1351.13Stakemura	mask = PLATID_DEREF(match->cf_loc[HPCIOIFCF_PLATFORM]);
1361.20Stakemura	if ((n = platid_match(&platid, &mask)) != 0)
1371.20Stakemura		return (n + 2);
1381.16Such
1391.16Such	return (0);
1401.1Stakemura}
1411.1Stakemura
1421.1Stakemuravoid
1431.16Suchvrisabattach(struct device *parent, struct device *self, void *aux)
1441.1Stakemura{
1451.13Stakemura	struct hpcio_attach_args *haa = aux;
1461.1Stakemura	struct vrisab_softc *sc = (void*)self;
1471.1Stakemura	struct isabus_attach_args iba;
1481.18Stakemura	struct bus_space_tag_hpcmips *iot, *memt;
1491.1Stakemura	bus_addr_t offset;
1501.1Stakemura	int i;
1511.1Stakemura
1521.13Stakemura	sc->sc_hc = (*haa->haa_getchip)(haa->haa_sc, VRIP_IOCHIP_VRGIU);
1531.3Stakemura	sc->sc_isa_ic.ic_sc = sc;
1541.1Stakemura
1551.3Stakemura	iba.iba_ic	= &sc->sc_isa_ic;
1561.1Stakemura	iba.iba_dmat    = 0; /* XXX not yet */
1571.1Stakemura
1581.1Stakemura	/* Allocate ISA memory space */
1591.19Stakemura	memt = hpcmips_alloc_bus_space_tag();
1601.32Sthorpej	offset = device_cfdata(&sc->sc_dev)->cf_loc[VRISABIFCF_ISAMEMOFFSET];
1611.19Stakemura	hpcmips_init_bus_space(memt,
1621.19Stakemura	    (struct bus_space_tag_hpcmips *)haa->haa_iot, "ISA mem",
1631.18Stakemura	    VR_ISA_MEM_BASE + offset, VR_ISA_MEM_SIZE - offset);
1641.19Stakemura	iba.iba_memt = &memt->bst;
1651.1Stakemura
1661.1Stakemura	/* Allocate ISA port space */
1671.19Stakemura	iot = hpcmips_alloc_bus_space_tag();
1681.32Sthorpej	offset = device_cfdata(&sc->sc_dev)->cf_loc[VRISABIFCF_ISAPORTOFFSET];
1691.19Stakemura	hpcmips_init_bus_space(iot,
1701.19Stakemura	    (struct bus_space_tag_hpcmips *)haa->haa_iot, "ISA port",
1711.18Stakemura	    VR_ISA_PORT_BASE + offset, VR_ISA_PORT_SIZE - offset);
1721.19Stakemura	iba.iba_iot = &iot->bst;
1731.1Stakemura
1741.1Stakemura#ifdef DEBUG_FIND_PCIC
1751.1Stakemura#warning DEBUG_FIND_PCIC
1761.1Stakemura	__find_pcic();
1771.1Stakemura#else
1781.1Stakemura	/* Initialize ISA IRQ <-> GPIO mapping */
1791.13Stakemura	for (i = 0; i < INTR_NIRQS; i++)
1801.1Stakemura		sc->sc_intr_map[i] = -1;
1811.15Senami	printf(": ISA port %#x-%#x mem %#x-%#x\n",
1821.18Stakemura	    iot->base, iot->base + iot->size,
1831.18Stakemura	    memt->base, memt->base + memt->size);
1841.29Sdrochner	config_found_ia(self, "isabus", &iba, vrisabprint);
1851.1Stakemura#endif
1861.11Ssato
1871.11Ssato#ifdef DEBUG_FIND_COMPORT
1881.11Ssato#warning DEBUG_FIND_COMPORT
1891.11Ssato	__find_comport();
1901.11Ssato#endif
1911.1Stakemura}
1921.1Stakemura
1931.1Stakemuraint
1941.16Suchvrisabprint(void *aux, const char *pnp)
1951.1Stakemura{
1961.1Stakemura	if (pnp)
1971.1Stakemura		return (QUIET);
1981.16Such
1991.1Stakemura	return (UNCONF);
2001.1Stakemura}
2011.1Stakemura
2021.1Stakemuravoid
2031.16Suchisa_attach_hook(struct device *parent, struct device *self,
2041.16Such    struct isabus_attach_args *iba)
2051.1Stakemura{
2061.16Such
2071.6Scgd}
2081.6Scgd
2091.6Scgdconst struct evcnt *
2101.6Scgdisa_intr_evcnt(isa_chipset_tag_t ic, int irq)
2111.6Scgd{
2121.6Scgd
2131.6Scgd	/* XXX for now, no evcnt parent reported */
2141.16Such	return (NULL);
2151.1Stakemura}
2161.1Stakemura
2171.1Stakemuravoid *
2181.16Suchisa_intr_establish(isa_chipset_tag_t ic, int intr, int type, int level,
2191.16Such    int (*ih_fun)(void*), void *ih_arg)
2201.1Stakemura{
2211.3Stakemura	struct vrisab_softc *sc = ic->ic_sc;
2221.3Stakemura	int port, irq, mode;
2231.2Stakemura
2241.2Stakemura	static int intr_modes[8] = {
2251.13Stakemura		HPCIO_INTR_LEVEL_HIGH_THROUGH,
2261.13Stakemura		HPCIO_INTR_LEVEL_HIGH_HOLD,
2271.13Stakemura		HPCIO_INTR_LEVEL_LOW_THROUGH,
2281.13Stakemura		HPCIO_INTR_LEVEL_LOW_HOLD,
2291.13Stakemura		HPCIO_INTR_EDGE_THROUGH,
2301.13Stakemura		HPCIO_INTR_EDGE_HOLD,
2311.13Stakemura		HPCIO_INTR_EDGE_THROUGH,
2321.13Stakemura		HPCIO_INTR_EDGE_HOLD,
2331.2Stakemura	};
2341.4Ssato#ifdef VRISADEBUG
2351.30She	static const char* intr_mode_names[8] = {
2361.2Stakemura		"level high through",
2371.2Stakemura		"level high hold",
2381.2Stakemura		"level low through",
2391.2Stakemura		"level low hold",
2401.2Stakemura		"edge through",
2411.2Stakemura		"edge hold",
2421.2Stakemura		"edge through",
2431.2Stakemura		"edge hold",
2441.2Stakemura	};
2451.4Ssato#endif /* VRISADEBUG */
2461.1Stakemura	/*
2471.1Stakemura	 * ISA IRQ <-> GPIO port mapping
2481.1Stakemura	 */
2491.2Stakemura	irq = INTR_IRQ(intr);
2501.3Stakemura	if (sc->sc_intr_map[irq] != -1) {
2511.3Stakemura		/* already mapped */
2521.3Stakemura		intr = sc->sc_intr_map[irq];
2531.2Stakemura	} else {
2541.3Stakemura		/* not mapped yet */
2551.3Stakemura		sc->sc_intr_map[irq] = intr; /* Register it */
2561.1Stakemura	}
2571.3Stakemura	mode = INTR_MODE(intr);
2581.3Stakemura	port = INTR_PORT(intr);
2591.3Stakemura
2601.14Stakemura	VPRINTF(("ISA IRQ %d -> %s port %d, %s\n",
2611.16Such	    irq, sc->sc_hc->hc_name, port, intr_mode_names[mode]));
2621.3Stakemura
2631.1Stakemura	/* Call Vr routine */
2641.16Such	return (hpcio_intr_establish(sc->sc_hc, port, intr_modes[mode],
2651.16Such	    ih_fun, ih_arg));
2661.1Stakemura}
2671.1Stakemura
2681.1Stakemuravoid
2691.1Stakemuraisa_intr_disestablish(ic, arg)
2701.1Stakemura	isa_chipset_tag_t ic;
2711.1Stakemura	void *arg;
2721.1Stakemura{
2731.3Stakemura	struct vrisab_softc *sc = ic->ic_sc;
2741.1Stakemura	/* Call Vr routine */
2751.13Stakemura	hpcio_intr_disestablish(sc->sc_hc, arg);
2761.1Stakemura}
2771.1Stakemura
2781.1Stakemuraint
2791.16Suchisa_intr_alloc(isa_chipset_tag_t ic, int mask, int type, int *irq)
2801.1Stakemura{
2811.1Stakemura	/* XXX not coded yet. this is temporary XXX */
2821.4Ssato	DPRINTF(("isa_intr_alloc:"));
2831.23Such	DBITDISP(mask);
2841.3Stakemura	*irq = (ffs(mask) -1); /* XXX */
2851.16Such
2861.16Such	return (0);
2871.1Stakemura}
2881.1Stakemura
2891.1Stakemura#ifdef DEBUG_FIND_PCIC
2901.1Stakemura#warning DEBUG_FIND_PCIC
2911.1Stakemurastatic void
2921.1Stakemura__find_pcic(void)
2931.1Stakemura{
2941.1Stakemura	int i, j, step, found;
2951.1Stakemura	u_int32_t addr;
2961.1Stakemura	u_int8_t reg;
2971.1Stakemura	int __read_revid (u_int32_t port)
2981.16Such	    {
2991.16Such		    addr = MIPS_PHYS_TO_KSEG1(i + port);
3001.16Such		    printf("%#x\r", i);
3011.16Such		    for (found = 0, j = 0; j < 0x100; j += 0x40) {
3021.16Such			    *((volatile u_int8_t *)addr) = j;
3031.16Such			    reg = *((volatile u_int8_t *)(addr + 1));
3041.1Stakemura#ifdef DEBUG_FIND_PCIC_I82365SL_ONLY
3051.16Such			    if (reg == 0x82 || reg == 0x83) {
3061.1Stakemura#else
3071.17Sshin			    if ((reg & 0xc0) == 0x80) {
3081.1Stakemura#endif
3091.16Such					    found++;
3101.16Such			    }
3111.16Such			    if (found)
3121.16Such				    printf("\nfound %d socket at %#x"
3131.16Such					"(base from %#x)\n", found, addr,
3141.16Such					i + port - VR_ISA_PORT_BASE);
3151.17Sshin		    }
3161.16Such	    }
3171.1Stakemura	step = 0x1000000;
3181.1Stakemura	printf("\nFinding PCIC. Trying ISA port %#x-%#x step %#x\n",
3191.16Such	    VR_ISA_PORT_BASE, VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE, step);
3201.16Such	for (i = VR_ISA_PORT_BASE; i < VR_ISA_PORT_BASE+VR_ISA_PORT_SIZE;
3211.16Such	    i+= step) {
3221.1Stakemura		__read_revid (0x3e0);
3231.1Stakemura		__read_revid (0x3e2);
3241.11Ssato	}
3251.11Ssato}
3261.16Such#endif /* DEBUG_FIND_PCIC */
3271.11Ssato
3281.11Ssato
3291.11Ssato#ifdef DEBUG_FIND_COMPORT
3301.11Ssato#warning DEBUG_FIND_COMPORT
3311.11Ssato
3321.16Suchstatic int probe_com(u_int32_t);
3331.11Ssato
3341.16Suchstatic int
3351.16Suchprobe_com(u_int32_t port_addr)
3361.11Ssato{
3371.16Such	u_int32_t addr;
3381.16Such	u_int8_t ubtmp1, ubtmp2;
3391.11Ssato
3401.16Such	addr = MIPS_PHYS_TO_KSEG1(port_addr);
3411.11Ssato
3421.16Such	*((volatile u_int8_t *)(addr + com_cfcr)) = LCR_8BITS;
3431.16Such	*((volatile u_int8_t *)(addr + com_iir)) = 0;
3441.11Ssato
3451.16Such	ubtmp1 = *((volatile u_int8_t *)(addr + com_cfcr));
3461.16Such	ubtmp2 = *((volatile u_int8_t *)(addr + com_iir));
3471.11Ssato
3481.16Such	if ((ubtmp1 != LCR_8BITS) || ((ubtmp2 & 0x38) != 0)) {
3491.16Such		return (0);
3501.11Ssato	}
3511.11Ssato
3521.16Such	return (1);
3531.11Ssato}
3541.11Ssato
3551.11Ssatostatic void
3561.16Such__find_comport()
3571.11Ssato{
3581.16Such	int found;
3591.16Such	u_int32_t port, step;
3601.11Ssato
3611.11Ssato	found = 0;
3621.11Ssato	step = 0x08;
3631.11Ssato
3641.11Ssato	printf("Searching COM port. Trying ISA port %#x-%#x step %#x\n",
3651.16Such	    VR_ISA_PORT_BASE, VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE - 1, step );
3661.11Ssato
3671.16Such	for (port = VR_ISA_PORT_BASE;
3681.16Such	    port < (VR_ISA_PORT_BASE + VR_ISA_PORT_SIZE); port += step){
3691.16Such		if (probe_com(port)) {
3701.11Ssato			found++;
3711.16Such			printf("found %d at %#x\n", found, port);
3721.11Ssato		}
3731.1Stakemura	}
3741.1Stakemura}
3751.16Such#endif /* DEBUG_FIND_COMPORT */
376