pci_machdep.c revision 1.33
11.33Schs/*	$NetBSD: pci_machdep.c,v 1.33 2012/10/27 17:17:44 chs 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.33Schs__KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.33 2012/10/27 17:17:44 chs Exp $");
301.32Smatt
311.32Smatt#define _COBALT_BUS_DMA_PRIVATE
321.1Ssoren
331.1Ssoren#include <sys/param.h>
341.32Smatt#include <sys/bus.h>
351.1Ssoren#include <sys/errno.h>
361.1Ssoren#include <sys/device.h>
371.17Stsutsui#include <sys/extent.h>
381.32Smatt#include <sys/intr.h>
391.32Smatt#include <sys/systm.h>
401.32Smatt#include <sys/time.h>
411.1Ssoren
421.1Ssoren#include <dev/pci/pcivar.h>
431.1Ssoren#include <dev/pci/pcireg.h>
441.1Ssoren#include <dev/pci/pcidevs.h>
451.17Stsutsui#include <dev/pci/pciconf.h>
461.23Stsutsui#include <dev/pci/pciide_apollo_reg.h>
471.1Ssoren
481.16Stsutsui#include <cobalt/dev/gtreg.h>
491.16Stsutsui
501.1Ssoren/*
511.1Ssoren * PCI doesn't have any special needs; just use
521.1Ssoren * the generic versions of these functions.
531.1Ssoren */
541.1Ssorenstruct cobalt_bus_dma_tag pci_bus_dma_tag = {
551.14Stsutsui	_bus_dmamap_create,
561.1Ssoren	_bus_dmamap_destroy,
571.1Ssoren	_bus_dmamap_load,
581.1Ssoren	_bus_dmamap_load_mbuf,
591.1Ssoren	_bus_dmamap_load_uio,
601.1Ssoren	_bus_dmamap_load_raw,
611.1Ssoren	_bus_dmamap_unload,
621.1Ssoren	_bus_dmamap_sync,
631.1Ssoren	_bus_dmamem_alloc,
641.1Ssoren	_bus_dmamem_free,
651.1Ssoren	_bus_dmamem_map,
661.1Ssoren	_bus_dmamem_unmap,
671.1Ssoren	_bus_dmamem_mmap,
681.1Ssoren};
691.1Ssoren
701.1Ssorenvoid
711.33Schspci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba)
721.1Ssoren{
731.1Ssoren	/* XXX */
741.1Ssoren
751.1Ssoren	return;
761.1Ssoren}
771.1Ssoren
781.1Ssorenint
791.20Stsutsuipci_bus_maxdevs(pci_chipset_tag_t pc, int busno)
801.1Ssoren{
811.20Stsutsui
821.6Ssoren	return 32;
831.1Ssoren}
841.1Ssoren
851.1Ssorenpcitag_t
861.20Stsutsuipci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function)
871.1Ssoren{
881.20Stsutsui
891.1Ssoren	return (bus << 16) | (device << 11) | (function << 8);
901.1Ssoren}
911.1Ssoren
921.1Ssorenvoid
931.20Stsutsuipci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, int *bp, int *dp, int *fp)
941.1Ssoren{
951.20Stsutsui
961.1Ssoren	if (bp != NULL)
971.1Ssoren		*bp = (tag >> 16) & 0xff;
981.1Ssoren	if (dp != NULL)
991.1Ssoren		*dp = (tag >> 11) & 0x1f;
1001.1Ssoren	if (fp != NULL)
1011.1Ssoren		*fp = (tag >> 8) & 0x07;
1021.1Ssoren}
1031.1Ssoren
1041.1Ssorenpcireg_t
1051.20Stsutsuipci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg)
1061.1Ssoren{
1071.1Ssoren	pcireg_t data;
1081.6Ssoren	int bus, dev, func;
1091.14Stsutsui
1101.28Smatt	KASSERT(pc != NULL);
1111.28Smatt
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.29Sdyoungpci_intr_map(const 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.27Sadint
2071.27Sadpci_intr_setattr(pci_chipset_tag_t pc, pci_intr_handle_t *ih,
2081.27Sad		 int attr, uint64_t data)
2091.27Sad{
2101.27Sad
2111.27Sad	switch (attr) {
2121.27Sad	case PCI_INTR_MPSAFE:
2131.27Sad		return 0;
2141.27Sad	default:
2151.27Sad		return ENODEV;
2161.27Sad	}
2171.27Sad}
2181.27Sad
2191.1Ssorenvoid *
2201.20Stsutsuipci_intr_establish(pci_chipset_tag_t pc, pci_intr_handle_t ih, int level,
2211.20Stsutsui    int (*func)(void *), void *arg)
2221.1Ssoren{
2231.20Stsutsui
2241.26Stsutsui	if (ih >= NICU_INT)
2251.26Stsutsui		return cpu_intr_establish(ih - NICU_INT, level, func, arg);
2261.4Ssoren	else
2271.4Ssoren		return icu_intr_establish(ih, IST_LEVEL, level, func, arg);
2281.1Ssoren}
2291.1Ssoren
2301.1Ssorenvoid
2311.20Stsutsuipci_intr_disestablish(pci_chipset_tag_t pc, void *cookie)
2321.1Ssoren{
2331.20Stsutsui
2341.12Saugustss	/* Try both, only the valid one will disestablish. */
2351.12Saugustss	cpu_intr_disestablish(cookie);
2361.12Saugustss	icu_intr_disestablish(cookie);
2371.1Ssoren}
2381.17Stsutsui
2391.17Stsutsuivoid
2401.17Stsutsuipci_conf_interrupt(pci_chipset_tag_t pc, int bus, int dev, int pin, int swiz,
2411.17Stsutsui    int *iline)
2421.17Stsutsui{
2431.17Stsutsui
2441.26Stsutsui	/*
2451.26Stsutsui	 * Use irq 9 on all devices on the Qube's PCI slot.
2461.26Stsutsui	 * XXX doesn't handle devices over PCI-PCI bridges
2471.26Stsutsui	 */
2481.26Stsutsui	if (bus == 0 && dev == 10 && pin != PCI_INTERRUPT_PIN_NONE)
2491.26Stsutsui		*iline = 9;
2501.17Stsutsui}
2511.17Stsutsui
2521.17Stsutsuiint
2531.17Stsutsuipci_conf_hook(pci_chipset_tag_t pc, int bus, int dev, int func, pcireg_t id)
2541.17Stsutsui{
2551.17Stsutsui
2561.22Stsutsui	/* ignore bogus IDs */
2571.22Stsutsui	if (PCI_VENDOR(id) == 0)
2581.22Stsutsui		return 0;
2591.22Stsutsui
2601.22Stsutsui	/* 2700 hardware wedges on accesses to device 6. */
2611.22Stsutsui	if (bus == 0 && dev == 6)
2621.22Stsutsui		return 0;
2631.22Stsutsui
2641.22Stsutsui	/* 2800 hardware wedges on accesses to device 31. */
2651.22Stsutsui	if (bus == 0 && dev == 31)
2661.22Stsutsui		return 0;
2671.22Stsutsui
2681.30Stsutsui	/* Don't configure the bridge and PCI probe. */
2691.24Sriz	if (PCI_VENDOR(id) == PCI_VENDOR_MARVELL &&
2701.24Sriz	    PCI_PRODUCT(id) == PCI_PRODUCT_MARVELL_GT64011)
2711.17Stsutsui	        return 0;
2721.17Stsutsui
2731.23Stsutsui	/* Don't configure on-board VIA VT82C586 (pcib, uhci) */
2741.23Stsutsui	if (bus == 0 && dev == 9 && (func == 0 || func == 2))
2751.17Stsutsui		return 0;
2761.17Stsutsui
2771.23Stsutsui	/* Enable viaide secondary port. Some firmware doesn't enable it. */
2781.23Stsutsui	if (bus == 0 && dev == 9 && func == 1) {
2791.23Stsutsui		pcitag_t tag;
2801.23Stsutsui		pcireg_t csr;
2811.23Stsutsui
2821.23Stsutsui#define	APO_VIAIDECONF	(APO_VIA_REGBASE + 0x00)
2831.23Stsutsui
2841.23Stsutsui		tag = pci_make_tag(pc, bus, dev, func);
2851.23Stsutsui		csr = pci_conf_read(pc, tag, APO_VIAIDECONF);
2861.23Stsutsui		pci_conf_write(pc, tag, APO_VIAIDECONF,
2871.23Stsutsui		    csr | APO_IDECONF_EN(1));
2881.23Stsutsui	}
2891.19Sgdamore	return PCI_CONF_DEFAULT & ~(PCI_COMMAND_SERR_ENABLE |
2901.19Sgdamore	    PCI_COMMAND_PARITY_ENABLE);
2911.17Stsutsui}
292