11.20Sjdolecek/*	$NetBSD: iha_pci.c,v 1.20 2018/12/09 11:14:02 jdolecek Exp $ */
21.16Stsutsui
31.16Stsutsui/*-
41.16Stsutsui * Copyright (c) 2001 Izumi Tsutsui.  All rights reserved.
51.16Stsutsui *
61.16Stsutsui * Redistribution and use in source and binary forms, with or without
71.16Stsutsui * modification, are permitted provided that the following conditions
81.16Stsutsui * are met:
91.16Stsutsui * 1. Redistributions of source code must retain the above copyright
101.16Stsutsui *    notice, this list of conditions and the following disclaimer.
111.16Stsutsui * 2. Redistributions in binary form must reproduce the above copyright
121.16Stsutsui *    notice, this list of conditions and the following disclaimer in the
131.16Stsutsui *    documentation and/or other materials provided with the distribution.
141.16Stsutsui *
151.16Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
161.16Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
171.16Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
181.16Stsutsui * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
191.16Stsutsui * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
201.16Stsutsui * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
211.16Stsutsui * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
221.16Stsutsui * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
231.16Stsutsui * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
241.16Stsutsui * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
251.16Stsutsui */
261.7Stsutsui
271.7Stsutsui/*-
281.7Stsutsui * Device driver for the INI-9XXXU/UW or INIC-940/950  PCI SCSI Controller.
291.7Stsutsui *
301.7Stsutsui *  Written for 386bsd and FreeBSD by
311.7Stsutsui *	Winston Hung		<winstonh@initio.com>
321.1Stsutsui *
331.7Stsutsui * Copyright (c) 1997-1999 Initio Corp.
341.1Stsutsui * Copyright (c) 2000 Ken Westerback
351.1Stsutsui * All rights reserved.
361.1Stsutsui *
371.1Stsutsui * Redistribution and use in source and binary forms, with or without
381.1Stsutsui * modification, are permitted provided that the following conditions
391.1Stsutsui * are met:
401.1Stsutsui * 1. Redistributions of source code must retain the above copyright
411.1Stsutsui *    notice, this list of conditions and the following disclaimer,
421.1Stsutsui *    without modification, immediately at the beginning of the file.
431.1Stsutsui * 2. The name of the author may not be used to endorse or promote products
441.1Stsutsui *    derived from this software without specific prior written permission.
451.1Stsutsui *
461.1Stsutsui * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
471.1Stsutsui * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
481.1Stsutsui * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
491.1Stsutsui * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
501.1Stsutsui * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
511.1Stsutsui * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
521.1Stsutsui * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
531.1Stsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
541.1Stsutsui * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
551.1Stsutsui * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
561.1Stsutsui * THE POSSIBILITY OF SUCH DAMAGE.
571.1Stsutsui */
581.1Stsutsui
591.1Stsutsui/*
601.15Stsutsui * Ported to NetBSD by Izumi Tsutsui <tsutsui@NetBSD.org> from OpenBSD:
611.1Stsutsui * $OpenBSD: iha_pci.c,v 1.2 2001/03/29 23:53:38 krw Exp $
621.1Stsutsui */
631.3Slukem
641.3Slukem#include <sys/cdefs.h>
651.20Sjdolecek__KERNEL_RCSID(0, "$NetBSD: iha_pci.c,v 1.20 2018/12/09 11:14:02 jdolecek Exp $");
661.1Stsutsui
671.1Stsutsui#include <sys/param.h>
681.1Stsutsui#include <sys/systm.h>
691.1Stsutsui#include <sys/device.h>
701.2Stsutsui
711.1Stsutsui#include <dev/pci/pcidevs.h>
721.1Stsutsui#include <dev/pci/pcivar.h>
731.1Stsutsui
741.1Stsutsui#include <dev/scsipi/scsipi_all.h>
751.1Stsutsui#include <dev/scsipi/scsi_all.h>
761.1Stsutsui#include <dev/scsipi/scsiconf.h>
771.1Stsutsui
781.1Stsutsui#include <dev/ic/ihavar.h>
791.1Stsutsui
801.15Stsutsuistatic int  iha_pci_match(device_t, cfdata_t, void *);
811.15Stsutsuistatic void iha_pci_attach(device_t, device_t, void *);
821.1Stsutsui
831.15StsutsuiCFATTACH_DECL_NEW(iha_pci, sizeof(struct iha_softc),
841.10Stsutsui    iha_pci_match, iha_pci_attach, NULL, NULL);
851.1Stsutsui
861.1Stsutsuistatic int
871.15Stsutsuiiha_pci_match(device_t parent, cfdata_t cf, void *aux)
881.1Stsutsui{
891.1Stsutsui	struct pci_attach_args *pa = aux;
901.1Stsutsui
911.1Stsutsui	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INITIO)
921.15Stsutsui		return 0;
931.1Stsutsui
941.1Stsutsui	switch (PCI_PRODUCT(pa->pa_id)) {
951.1Stsutsui	case PCI_PRODUCT_INITIO_I940:
961.1Stsutsui	case PCI_PRODUCT_INITIO_I950:
971.15Stsutsui		return 1;
981.1Stsutsui	}
991.1Stsutsui
1001.15Stsutsui	return 0;
1011.1Stsutsui}
1021.1Stsutsui
1031.1Stsutsuistatic void
1041.15Stsutsuiiha_pci_attach(device_t parent, device_t self, void *aux)
1051.1Stsutsui{
1061.15Stsutsui	struct iha_softc *sc = device_private(self);
1071.1Stsutsui	struct pci_attach_args *pa = aux;
1081.1Stsutsui	bus_space_tag_t iot;
1091.1Stsutsui	bus_space_handle_t ioh;
1101.1Stsutsui	pci_intr_handle_t ih;
1111.1Stsutsui	const char *intrstr;
1121.1Stsutsui	pcireg_t command;
1131.1Stsutsui	int ioh_valid;
1141.19Schristos	char intrbuf[PCI_INTRSTR_LEN];
1151.1Stsutsui
1161.15Stsutsui	sc->sc_dev = self;
1171.15Stsutsui
1181.18Sdrochner	pci_aprint_devinfo(pa, NULL);
1191.1Stsutsui
1201.1Stsutsui	command  = pci_conf_read(pa->pa_pc,pa->pa_tag,PCI_COMMAND_STATUS_REG);
1211.1Stsutsui	command |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_PARITY_ENABLE;
1221.1Stsutsui
1231.1Stsutsui	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
1241.1Stsutsui
1251.1Stsutsui	/*
1261.1Stsutsui	 * XXX - Tried memory mapping (using code from adw and ahc)
1271.1Stsutsui	 *	 rather that IO mapping, but it didn't work at all..
1281.1Stsutsui	 */
1291.1Stsutsui	ioh_valid = pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
1301.1Stsutsui	    &iot, &ioh, NULL, NULL);
1311.1Stsutsui
1321.1Stsutsui	if (ioh_valid != 0) {
1331.15Stsutsui		aprint_error_dev(self, "unable to map registers\n");
1341.1Stsutsui		return;
1351.1Stsutsui	}
1361.1Stsutsui
1371.1Stsutsui	sc->sc_iot  = iot;
1381.1Stsutsui	sc->sc_ioh  = ioh;
1391.1Stsutsui	sc->sc_dmat = pa->pa_dmat;
1401.1Stsutsui
1411.1Stsutsui	if (pci_intr_map(pa, &ih)) {
1421.15Stsutsui		aprint_error_dev(self, "couldn't map interrupt\n");
1431.1Stsutsui		return;
1441.1Stsutsui	}
1451.19Schristos	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
1461.1Stsutsui
1471.20Sjdolecek	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_BIO, iha_intr,
1481.20Sjdolecek	    sc, device_xname(self));
1491.1Stsutsui
1501.1Stsutsui	if (sc->sc_ih == NULL) {
1511.15Stsutsui		aprint_error_dev(self, "couldn't establish interrupt");
1521.1Stsutsui		if (intrstr != NULL)
1531.15Stsutsui			aprint_error(" at %s", intrstr);
1541.15Stsutsui		aprint_error("\n");
1551.1Stsutsui		return;
1561.1Stsutsui	}
1571.15Stsutsui	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
1581.1Stsutsui
1591.1Stsutsui	iha_attach(sc);
1601.1Stsutsui}
161