pci_machdep.c revision 1.29
11.29Sdyoung/*	$NetBSD: pci_machdep.c,v 1.29 2011/04/04 20:37:47 dyoung 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.29Sdyoung__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.29 2011/04/04 20:37:47 dyoung 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.28Smatt	KASSERT(pc != NULL);
1131.28Smatt
1141.6Ssoren	pci_decompose_tag(pc, tag, &bus, &dev, &func);
1151.6Ssoren
1161.6Ssoren	/*
1171.6Ssoren	 * 2700 hardware wedges on accesses to device 6.
1181.6Ssoren	 */
1191.6Ssoren	if (bus == 0 && dev == 6)
1201.6Ssoren		return 0;
1211.6Ssoren	/*
1221.6Ssoren	 * 2800 hardware wedges on accesses to device 31.
1231.6Ssoren	 */
1241.6Ssoren	if (bus == 0 && dev == 31)
1251.6Ssoren		return 0;
1261.1Ssoren
1271.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR,
1281.21Stsutsui	    PCICFG_ENABLE | tag | reg);
1291.16Stsutsui	data = bus_space_read_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_DATA);
1301.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR, 0);
1311.1Ssoren
1321.1Ssoren	return data;
1331.1Ssoren}
1341.1Ssoren
1351.1Ssorenvoid
1361.20Stsutsuipci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
1371.1Ssoren{
1381.1Ssoren
1391.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR,
1401.21Stsutsui	    PCICFG_ENABLE | tag | reg);
1411.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_DATA, data);
1421.16Stsutsui	bus_space_write_4(pc->pc_bst, pc->pc_bsh, GT_PCICFG_ADDR, 0);
1431.1Ssoren}
1441.1Ssoren
1451.1Ssorenint
1461.29Sdyoungpci_intr_map(const struct pci_attach_args *pa, pci_intr_handle_t *ihp)
1471.1Ssoren{
1481.10Ssommerfe	pci_chipset_tag_t pc = pa->pa_pc;
1491.11Stsutsui	pcitag_t intrtag = pa->pa_intrtag;
1501.10Ssommerfe	int pin = pa->pa_intrpin;
1511.10Ssommerfe	int line = pa->pa_intrline;
1521.5Ssoren	int bus, dev, func;
1531.1Ssoren
1541.5Ssoren	pci_decompose_tag(pc, intrtag, &bus, &dev, &func);
1551.5Ssoren
1561.5Ssoren	/*
1571.26Stsutsui	 * The interrupt lines of the internal Tulips are connected
1581.5Ssoren	 * directly to the CPU.
1591.5Ssoren	 */
1601.25Stsutsui	if (cobalt_id == COBALT_ID_QUBE2700) {
1611.26Stsutsui		if (bus == 0 && dev == 7 && pin == PCI_INTERRUPT_PIN_A) {
1621.26Stsutsui			/* tulip is connected to CPU INT2 on Qube2700 */
1631.26Stsutsui			*ihp = NICU_INT + 2;
1641.26Stsutsui			return 0;
1651.26Stsutsui		}
1661.25Stsutsui	} else {
1671.26Stsutsui		if (bus == 0 && dev == 7 && pin == PCI_INTERRUPT_PIN_A) {
1681.26Stsutsui			/* the primary tulip is connected to CPU INT1 */
1691.26Stsutsui			*ihp = NICU_INT + 1;
1701.26Stsutsui			return 0;
1711.26Stsutsui		}
1721.26Stsutsui		if (bus == 0 && dev == 12 && pin == PCI_INTERRUPT_PIN_A) {
1731.26Stsutsui			/* the secondary tulip is connected to CPU INT2 */
1741.26Stsutsui			*ihp = NICU_INT + 2;
1751.26Stsutsui			return 0;
1761.26Stsutsui		}
1771.25Stsutsui	}
1781.1Ssoren
1791.26Stsutsui	/* sanity check */
1801.26Stsutsui	if (line == 0 || line >= NICU_INT)
1811.26Stsutsui		return -1;
1821.26Stsutsui
1831.26Stsutsui	*ihp = line;
1841.1Ssoren	return 0;
1851.1Ssoren}
1861.1Ssoren
1871.1Ssorenconst char *
1881.20Stsutsuipci_intr_string(pci_chipset_tag_t pc, pci_intr_handle_t ih)
1891.1Ssoren{
1901.4Ssoren	static char irqstr[8];
1911.4Ssoren
1921.26Stsutsui	if (ih >= NICU_INT)
1931.26Stsutsui		sprintf(irqstr, "level %d", ih - NICU_INT);
1941.4Ssoren	else
1951.4Ssoren		sprintf(irqstr, "irq %d", ih);
1961.1Ssoren
1971.1Ssoren	return irqstr;
1981.7Scgd}
1991.7Scgd
2001.7Scgdconst struct evcnt *
2011.20Stsutsuipci_intr_evcnt(pci_chipset_tag_t pc, pci_intr_handle_t ih)
2021.7Scgd{
2031.7Scgd
2041.7Scgd	/* XXX for now, no evcnt parent reported */
2051.7Scgd	return NULL;
2061.1Ssoren}
2071.1Ssoren
2081.27Sadint
2091.27Sadpci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
2101.27Sad		 int attr, uint64_t data)
2111.27Sad{
2121.27Sad
2131.27Sad	switch (attr) {
2141.27Sad	case PCI_INTR_MPSAFE:
2151.27Sad		return 0;
2161.27Sad	default:
2171.27Sad		return ENODEV;
2181.27Sad	}
2191.27Sad}
2201.27Sad
2211.1Ssorenvoid *
2221.20Stsutsuipci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
2231.20Stsutsui    int (*func)(void *), void *arg)
2241.1Ssoren{
2251.20Stsutsui
2261.26Stsutsui	if (ih >= NICU_INT)
2271.26Stsutsui		return cpu_intr_establish(ih - NICU_INT, level, func, arg);
2281.4Ssoren	else
2291.4Ssoren		return icu_intr_establish(ih, IST_LEVEL, level, func, arg);
2301.1Ssoren}
2311.1Ssoren
2321.1Ssorenvoid
2331.20Stsutsuipci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
2341.1Ssoren{
2351.20Stsutsui
2361.12Saugustss	/* Try both, only the valid one will disestablish. */
2371.12Saugustss	cpu_intr_disestablish(cookie);
2381.12Saugustss	icu_intr_disestablish(cookie);
2391.1Ssoren}
2401.17Stsutsui
2411.17Stsutsuivoid
2421.17Stsutsuipci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin, int swiz,
2431.17Stsutsui    int *iline)
2441.17Stsutsui{
2451.17Stsutsui
2461.26Stsutsui	/*
2471.26Stsutsui	 * Use irq 9 on all devices on the Qube's PCI slot.
2481.26Stsutsui	 * XXX doesn't handle devices over PCI-PCI bridges
2491.26Stsutsui	 */
2501.26Stsutsui	if (bus == 0 && dev == 10 && pin != PCI_INTERRUPT_PIN_NONE)
2511.26Stsutsui		*iline = 9;
2521.17Stsutsui}
2531.17Stsutsui
2541.17Stsutsuiint
2551.17Stsutsuipci_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func, pcireg_t id)
2561.17Stsutsui{
2571.17Stsutsui
2581.22Stsutsui	/* ignore bogus IDs */
2591.22Stsutsui	if (PCI_VENDOR(id) == 0)
2601.22Stsutsui		return 0;
2611.22Stsutsui
2621.22Stsutsui	/* 2700 hardware wedges on accesses to device 6. */
2631.22Stsutsui	if (bus == 0 && dev == 6)
2641.22Stsutsui		return 0;
2651.22Stsutsui
2661.22Stsutsui	/* 2800 hardware wedges on accesses to device 31. */
2671.22Stsutsui	if (bus == 0 && dev == 31)
2681.22Stsutsui		return 0;
2691.22Stsutsui
2701.17Stsutsui	/* Don't configure the bridge and PCI probe. */
2711.24Sriz	if (PCI_VENDOR(id) == PCI_VENDOR_MARVELL &&
2721.24Sriz	    PCI_PRODUCT(id) == PCI_PRODUCT_MARVELL_GT64011)
2731.17Stsutsui	        return 0;
2741.17Stsutsui
2751.23Stsutsui	/* Don't configure on-board VIA VT82C586 (pcib, uhci) */
2761.23Stsutsui	if (bus == 0 && dev == 9 && (func == 0 || func == 2))
2771.17Stsutsui		return 0;
2781.17Stsutsui
2791.23Stsutsui	/* Enable viaide secondary port. Some firmware doesn't enable it. */
2801.23Stsutsui	if (bus == 0 && dev == 9 && func == 1) {
2811.23Stsutsui		pcitag_t tag;
2821.23Stsutsui		pcireg_t csr;
2831.23Stsutsui
2841.23Stsutsui#define	APO_VIAIDECONF	(APO_VIA_REGBASE + 0x00)
2851.23Stsutsui
2861.23Stsutsui		tag = pci_make_tag(pc, bus, dev, func);
2871.23Stsutsui		csr = pci_conf_read(pc, tag, APO_VIAIDECONF);
2881.23Stsutsui		pci_conf_write(pc, tag, APO_VIAIDECONF,
2891.23Stsutsui		    csr | APO_IDECONF_EN(1));
2901.23Stsutsui	}
2911.19Sgdamore	return PCI_CONF_DEFAULT & ~(PCI_COMMAND_SERR_ENABLE |
2921.19Sgdamore	    PCI_COMMAND_PARITY_ENABLE);
2931.17Stsutsui}
294