pci_machdep.c revision 1.26
11.26Stsutsui/*	$NetBSD: pci_machdep.c,v 1.26 2008/03/22 18:32:20 tsutsui Exp $	*/
21.1Ssoren
31.1Ssoren/*
41.1Ssoren * Copyright (c) 2000 Soren S. Jorvang.  All rights reserved.
51.1Ssoren *
61.1Ssoren * Redistribution and use in source and binary forms, with or without
71.1Ssoren * modification, are permitted provided that the following conditions
81.1Ssoren * are met:
91.1Ssoren * 1. Redistributions of source code must retain the above copyright
101.1Ssoren *    notice, this list of conditions, and the following disclaimer.
111.1Ssoren * 2. Redistributions in binary form must reproduce the above copyright
121.1Ssoren *    notice, this list of conditions and the following disclaimer in the
131.1Ssoren *    documentation and/or other materials provided with the distribution.
141.1Ssoren *
151.1Ssoren * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
161.1Ssoren * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
171.1Ssoren * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
181.1Ssoren * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
191.1Ssoren * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201.1Ssoren * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
211.1Ssoren * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
221.1Ssoren * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
231.1Ssoren * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
241.1Ssoren * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
251.1Ssoren * SUCH DAMAGE.
261.1Ssoren */
271.13Slukem
281.13Slukem#include <sys/cdefs.h>
291.26Stsutsui__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.26 2008/03/22 18:32:20 tsutsui Exp $");
301.1Ssoren
311.1Ssoren#include <sys/types.h>
321.1Ssoren#include <sys/param.h>
331.1Ssoren#include <sys/time.h>
341.1Ssoren#include <sys/systm.h>
351.1Ssoren#include <sys/errno.h>
361.1Ssoren#include <sys/device.h>
371.17Stsutsui#include <sys/extent.h>
381.1Ssoren
391.1Ssoren#define _COBALT_BUS_DMA_PRIVATE
401.1Ssoren#include <machine/bus.h>
411.4Ssoren#include <machine/intr.h>
421.1Ssoren
431.1Ssoren#include <dev/pci/pcivar.h>
441.1Ssoren#include <dev/pci/pcireg.h>
451.1Ssoren#include <dev/pci/pcidevs.h>
461.17Stsutsui#include <dev/pci/pciconf.h>
471.23Stsutsui#include <dev/pci/pciide_apollo_reg.h>
481.1Ssoren
491.16Stsutsui#include <cobalt/dev/gtreg.h>
501.16Stsutsui
511.1Ssoren/*
521.1Ssoren * PCI doesn't have any special needs; just use
531.1Ssoren * the generic versions of these functions.
541.1Ssoren */
551.1Ssorenstruct cobalt_bus_dma_tag pci_bus_dma_tag = {
561.14Stsutsui	_bus_dmamap_create,
571.1Ssoren	_bus_dmamap_destroy,
581.1Ssoren	_bus_dmamap_load,
591.1Ssoren	_bus_dmamap_load_mbuf,
601.1Ssoren	_bus_dmamap_load_uio,
611.1Ssoren	_bus_dmamap_load_raw,
621.1Ssoren	_bus_dmamap_unload,
631.1Ssoren	_bus_dmamap_sync,
641.1Ssoren	_bus_dmamem_alloc,
651.1Ssoren	_bus_dmamem_free,
661.1Ssoren	_bus_dmamem_map,
671.1Ssoren	_bus_dmamem_unmap,
681.1Ssoren	_bus_dmamem_mmap,
691.1Ssoren};
701.1Ssoren
711.1Ssorenvoid
721.20Stsutsuipci_attach_hook(struct device *parent, struct device *self,
731.20Stsutsui    struct pcibus_attach_args *pba)
741.1Ssoren{
751.1Ssoren	/* XXX */
761.1Ssoren
771.1Ssoren	return;
781.1Ssoren}
791.1Ssoren
801.1Ssorenint
811.20Stsutsuipci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
821.1Ssoren{
831.20Stsutsui
841.6Ssoren	return 32;
851.1Ssoren}
861.1Ssoren
871.1Ssorenpcitag_t
881.20Stsutsuipci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
891.1Ssoren{
901.20Stsutsui
911.1Ssoren	return (bus << 16) | (device << 11) | (function << 8);
921.1Ssoren}
931.1Ssoren
941.1Ssorenvoid
951.20Stsutsuipci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
961.1Ssoren{
971.20Stsutsui
981.1Ssoren	if (bp != NULL)
991.1Ssoren		*bp = (tag >> 16) & 0xff;
1001.1Ssoren	if (dp != NULL)
1011.1Ssoren		*dp = (tag >> 11) & 0x1f;
1021.1Ssoren	if (fp != NULL)
1031.1Ssoren		*fp = (tag >> 8) & 0x07;
1041.1Ssoren}
1051.1Ssoren
1061.1Ssorenpcireg_t
1071.20Stsutsuipci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
1081.1Ssoren{
1091.1Ssoren	pcireg_t data;
1101.6Ssoren	int bus, dev, func;
1111.14Stsutsui
1121.6Ssoren	pci_decompose_tag(pc, tag, &bus, &dev, &func);
1131.6Ssoren
1141.6Ssoren	/*
1151.6Ssoren	 * 2700 hardware wedges on accesses to device 6.
1161.6Ssoren	 */
1171.6Ssoren	if (bus == 0 && dev == 6)
1181.6Ssoren		return 0;
1191.6Ssoren	/*
1201.6Ssoren	 * 2800 hardware wedges on accesses to device 31.
1211.6Ssoren	 */
1221.6Ssoren	if (bus == 0 && dev == 31)
1231.6Ssoren		return 0;
1241.1Ssoren
1251.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR,
1261.21Stsutsui	    PCICFG_ENABLE | tag | reg);
1271.16Stsutsui	data = bus_space_read_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_DATA);
1281.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR, 0);
1291.1Ssoren
1301.1Ssoren	return data;
1311.1Ssoren}
1321.1Ssoren
1331.1Ssorenvoid
1341.20Stsutsuipci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
1351.1Ssoren{
1361.1Ssoren
1371.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR,
1381.21Stsutsui	    PCICFG_ENABLE | tag | reg);
1391.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_DATA, data);
1401.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR, 0);
1411.1Ssoren}
1421.1Ssoren
1431.1Ssorenint
1441.20Stsutsuipci_intr_map(struct pci_attach_args *pa, pci_intr_handle_t *ihp)
1451.1Ssoren{
1461.10Ssommerfe	pci_chipset_tag_t pc = pa->pa_pc;
1471.11Stsutsui	pcitag_t intrtag = pa->pa_intrtag;
1481.10Ssommerfe	int pin = pa->pa_intrpin;
1491.10Ssommerfe	int line = pa->pa_intrline;
1501.5Ssoren	int bus, dev, func;
1511.1Ssoren
1521.5Ssoren	pci_decompose_tag(pc, intrtag, &bus, &dev, &func);
1531.5Ssoren
1541.5Ssoren	/*
1551.26Stsutsui	 * The interrupt lines of the internal Tulips are connected
1561.5Ssoren	 * directly to the CPU.
1571.5Ssoren	 */
1581.25Stsutsui	if (cobalt_id == COBALT_ID_QUBE2700) {
1591.26Stsutsui		if (bus == 0 && dev == 7 && pin == PCI_INTERRUPT_PIN_A) {
1601.26Stsutsui			/* tulip is connected to CPU INT2 on Qube2700 */
1611.26Stsutsui			*ihp = NICU_INT + 2;
1621.26Stsutsui			return 0;
1631.26Stsutsui		}
1641.25Stsutsui	} else {
1651.26Stsutsui		if (bus == 0 && dev == 7 && pin == PCI_INTERRUPT_PIN_A) {
1661.26Stsutsui			/* the primary tulip is connected to CPU INT1 */
1671.26Stsutsui			*ihp = NICU_INT + 1;
1681.26Stsutsui			return 0;
1691.26Stsutsui		}
1701.26Stsutsui		if (bus == 0 && dev == 12 && pin == PCI_INTERRUPT_PIN_A) {
1711.26Stsutsui			/* the secondary tulip is connected to CPU INT2 */
1721.26Stsutsui			*ihp = NICU_INT + 2;
1731.26Stsutsui			return 0;
1741.26Stsutsui		}
1751.25Stsutsui	}
1761.1Ssoren
1771.26Stsutsui	/* sanity check */
1781.26Stsutsui	if (line == 0 || line >= NICU_INT)
1791.26Stsutsui		return -1;
1801.26Stsutsui
1811.26Stsutsui	*ihp = line;
1821.1Ssoren	return 0;
1831.1Ssoren}
1841.1Ssoren
1851.1Ssorenconst char *
1861.20Stsutsuipci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
1871.1Ssoren{
1881.4Ssoren	static char irqstr[8];
1891.4Ssoren
1901.26Stsutsui	if (ih >= NICU_INT)
1911.26Stsutsui		sprintf(irqstr, "level %d", ih - NICU_INT);
1921.4Ssoren	else
1931.4Ssoren		sprintf(irqstr, "irq %d", ih);
1941.1Ssoren
1951.1Ssoren	return irqstr;
1961.7Scgd}
1971.7Scgd
1981.7Scgdconst struct evcnt *
1991.20Stsutsuipci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
2001.7Scgd{
2011.7Scgd
2021.7Scgd	/* XXX for now, no evcnt parent reported */
2031.7Scgd	return NULL;
2041.1Ssoren}
2051.1Ssoren
2061.1Ssorenvoid *
2071.20Stsutsuipci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
2081.20Stsutsui    int (*func)(void *), void *arg)
2091.1Ssoren{
2101.20Stsutsui
2111.26Stsutsui	if (ih >= NICU_INT)
2121.26Stsutsui		return cpu_intr_establish(ih - NICU_INT, level, func, arg);
2131.4Ssoren	else
2141.4Ssoren		return icu_intr_establish(ih, IST_LEVEL, level, func, arg);
2151.1Ssoren}
2161.1Ssoren
2171.1Ssorenvoid
2181.20Stsutsuipci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
2191.1Ssoren{
2201.20Stsutsui
2211.12Saugustss	/* Try both, only the valid one will disestablish. */
2221.12Saugustss	cpu_intr_disestablish(cookie);
2231.12Saugustss	icu_intr_disestablish(cookie);
2241.1Ssoren}
2251.17Stsutsui
2261.17Stsutsuivoid
2271.17Stsutsuipci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin, int swiz,
2281.17Stsutsui    int *iline)
2291.17Stsutsui{
2301.17Stsutsui
2311.26Stsutsui	/*
2321.26Stsutsui	 * Use irq 9 on all devices on the Qube's PCI slot.
2331.26Stsutsui	 * XXX doesn't handle devices over PCI-PCI bridges
2341.26Stsutsui	 */
2351.26Stsutsui	if (bus == 0 && dev == 10 && pin != PCI_INTERRUPT_PIN_NONE)
2361.26Stsutsui		*iline = 9;
2371.17Stsutsui}
2381.17Stsutsui
2391.17Stsutsuiint
2401.17Stsutsuipci_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func, pcireg_t id)
2411.17Stsutsui{
2421.17Stsutsui
2431.22Stsutsui	/* ignore bogus IDs */
2441.22Stsutsui	if (PCI_VENDOR(id) == 0)
2451.22Stsutsui		return 0;
2461.22Stsutsui
2471.22Stsutsui	/* 2700 hardware wedges on accesses to device 6. */
2481.22Stsutsui	if (bus == 0 && dev == 6)
2491.22Stsutsui		return 0;
2501.22Stsutsui
2511.22Stsutsui	/* 2800 hardware wedges on accesses to device 31. */
2521.22Stsutsui	if (bus == 0 && dev == 31)
2531.22Stsutsui		return 0;
2541.22Stsutsui
2551.17Stsutsui	/* Don't configure the bridge and PCI probe. */
2561.24Sriz	if (PCI_VENDOR(id) == PCI_VENDOR_MARVELL &&
2571.24Sriz	    PCI_PRODUCT(id) == PCI_PRODUCT_MARVELL_GT64011)
2581.17Stsutsui	        return 0;
2591.17Stsutsui
2601.23Stsutsui	/* Don't configure on-board VIA VT82C586 (pcib, uhci) */
2611.23Stsutsui	if (bus == 0 && dev == 9 && (func == 0 || func == 2))
2621.17Stsutsui		return 0;
2631.17Stsutsui
2641.23Stsutsui	/* Enable viaide secondary port. Some firmware doesn't enable it. */
2651.23Stsutsui	if (bus == 0 && dev == 9 && func == 1) {
2661.23Stsutsui		pcitag_t tag;
2671.23Stsutsui		pcireg_t csr;
2681.23Stsutsui
2691.23Stsutsui#define	APO_VIAIDECONF	(APO_VIA_REGBASE + 0x00)
2701.23Stsutsui
2711.23Stsutsui		tag = pci_make_tag(pc, bus, dev, func);
2721.23Stsutsui		csr = pci_conf_read(pc, tag, APO_VIAIDECONF);
2731.23Stsutsui		pci_conf_write(pc, tag, APO_VIAIDECONF,
2741.23Stsutsui		    csr | APO_IDECONF_EN(1));
2751.23Stsutsui	}
2761.19Sgdamore	return PCI_CONF_DEFAULT & ~(PCI_COMMAND_SERR_ENABLE |
2771.19Sgdamore	    PCI_COMMAND_PARITY_ENABLE);
2781.17Stsutsui}
279