Home | History | Annotate | Line # | Download | only in pci
      1  1.23   thorpej /*	$NetBSD: if_sf_pci.c,v 1.23 2022/09/24 18:12:42 thorpej Exp $	*/
      2   1.1   thorpej 
      3   1.1   thorpej /*-
      4   1.1   thorpej  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5   1.1   thorpej  * All rights reserved.
      6   1.1   thorpej  *
      7   1.1   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1   thorpej  * by Jason R. Thorpe.
      9   1.1   thorpej  *
     10   1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     11   1.1   thorpej  * modification, are permitted provided that the following conditions
     12   1.1   thorpej  * are met:
     13   1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     14   1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     15   1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17   1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     18   1.1   thorpej  *
     19   1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1   thorpej  */
     31   1.1   thorpej 
     32   1.1   thorpej /*
     33   1.1   thorpej  * PCI bus front-end for the Adaptec AIC-6915 (``Starfire'')
     34   1.1   thorpej  * 10/100 Ethernet controller.
     35   1.1   thorpej  */
     36   1.3     lukem 
     37   1.3     lukem #include <sys/cdefs.h>
     38  1.23   thorpej __KERNEL_RCSID(0, "$NetBSD: if_sf_pci.c,v 1.23 2022/09/24 18:12:42 thorpej Exp $");
     39   1.1   thorpej 
     40   1.1   thorpej #include <sys/param.h>
     41   1.1   thorpej #include <sys/systm.h>
     42   1.1   thorpej #include <sys/mbuf.h>
     43   1.1   thorpej #include <sys/kernel.h>
     44   1.1   thorpej #include <sys/socket.h>
     45   1.1   thorpej #include <sys/ioctl.h>
     46   1.1   thorpej #include <sys/errno.h>
     47   1.1   thorpej #include <sys/device.h>
     48   1.1   thorpej 
     49   1.1   thorpej #include <net/if.h>
     50   1.1   thorpej #include <net/if_dl.h>
     51   1.1   thorpej #include <net/if_media.h>
     52   1.1   thorpej #include <net/if_ether.h>
     53   1.1   thorpej 
     54  1.13        ad #include <sys/bus.h>
     55  1.13        ad #include <sys/intr.h>
     56   1.1   thorpej 
     57   1.1   thorpej #include <dev/mii/miivar.h>
     58   1.1   thorpej 
     59   1.1   thorpej #include <dev/ic/aic6915reg.h>
     60   1.1   thorpej #include <dev/ic/aic6915var.h>
     61   1.1   thorpej 
     62   1.1   thorpej #include <dev/pci/pcireg.h>
     63   1.1   thorpej #include <dev/pci/pcivar.h>
     64   1.1   thorpej #include <dev/pci/pcidevs.h>
     65   1.1   thorpej 
     66   1.1   thorpej struct sf_pci_softc {
     67   1.1   thorpej 	struct sf_softc sc_starfire;	/* read Starfire softc */
     68   1.1   thorpej 
     69   1.1   thorpej 	/* PCI-specific goo. */
     70   1.1   thorpej 	void	*sc_ih;			/* interrupt handle */
     71   1.1   thorpej };
     72   1.1   thorpej 
     73  1.17    cegger static int	sf_pci_match(device_t, cfdata_t, void *);
     74  1.14    dyoung static void	sf_pci_attach(device_t, device_t, void *);
     75   1.1   thorpej 
     76  1.19       chs CFATTACH_DECL_NEW(sf_pci, sizeof(struct sf_pci_softc),
     77   1.6   thorpej     sf_pci_match, sf_pci_attach, NULL, NULL);
     78   1.1   thorpej 
     79   1.1   thorpej struct sf_pci_product {
     80   1.1   thorpej 	uint32_t	spp_vendor;	/* PCI vendor ID */
     81   1.1   thorpej 	uint32_t	spp_product;	/* PCI product ID */
     82   1.1   thorpej 	const char	*spp_name;	/* product name */
     83   1.1   thorpej 	const struct sf_pci_product *spp_subsys; /* subsystm IDs */
     84   1.1   thorpej };
     85   1.1   thorpej 
     86   1.8   thorpej static const struct sf_pci_product sf_subsys_adaptec[] = {
     87   1.1   thorpej 	/* ANA-62011 (rev 0) Single port 10/100 64-bit */
     88   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0008,
     89   1.1   thorpej 	  "ANA-62011 (rev 0) 10/100 Ethernet",	NULL },
     90   1.1   thorpej 
     91   1.1   thorpej 	/* ANA-62011 (rev 1) Single port 10/100 64-bit */
     92   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0009,
     93   1.1   thorpej 	  "ANA-62011 (rev 1) 10/100 Ethernet",	NULL },
     94   1.1   thorpej 
     95   1.1   thorpej 	/* ANA-62022 Dual port 10/100 64-bit */
     96   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0010,
     97   1.1   thorpej 	  "ANA-62022 10/100 Ethernet",		NULL },
     98   1.1   thorpej 
     99   1.1   thorpej 	/* ANA-62044 (rev 0) Quad port 10/100 64-bit */
    100   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0018,
    101   1.1   thorpej 	  "ANA-62044 (rev 0) 10/100 Ethernet",	NULL },
    102   1.1   thorpej 
    103   1.1   thorpej 	/* ANA-62044 (rev 1) Quad port 10/100 64-bit */
    104   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0019,
    105   1.1   thorpej 	  "ANA-62044 (rev 1) 10/100 Ethernet",	NULL },
    106   1.1   thorpej 
    107   1.1   thorpej 	/* ANA-62020 Single port 100baseFX 64-bit */
    108   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0020,
    109   1.1   thorpej 	  "ANA-62020 100baseFX Ethernet",	NULL },
    110   1.1   thorpej 
    111   1.1   thorpej 	/* ANA-69011 Single port 10/100 32-bit */
    112   1.1   thorpej 	{ PCI_VENDOR_ADP,			0x0028,
    113   1.1   thorpej 	  "ANA-69011 10/100 Ethernet",		NULL },
    114   1.1   thorpej 
    115   1.1   thorpej 	{ 0, 					0,
    116   1.1   thorpej 	  NULL,					NULL },
    117   1.1   thorpej };
    118   1.1   thorpej 
    119   1.8   thorpej static const struct sf_pci_product sf_pci_products[] = {
    120   1.1   thorpej 	{ PCI_VENDOR_ADP,			PCI_PRODUCT_ADP_AIC6915,
    121   1.1   thorpej 	  "AIC-6915 10/100 Ethernet",		sf_subsys_adaptec },
    122   1.1   thorpej 
    123   1.1   thorpej 	{ 0,					0,
    124   1.1   thorpej 	  NULL,					NULL },
    125   1.1   thorpej };
    126   1.1   thorpej 
    127   1.1   thorpej static const struct sf_pci_product *
    128   1.1   thorpej sf_pci_lookup(const struct pci_attach_args *pa)
    129   1.1   thorpej {
    130   1.1   thorpej 	const struct sf_pci_product *spp, *subspp;
    131   1.1   thorpej 	pcireg_t subsysid;
    132   1.1   thorpej 
    133   1.1   thorpej 	for (spp = sf_pci_products; spp->spp_name != NULL; spp++) {
    134   1.1   thorpej 		if (PCI_VENDOR(pa->pa_id) == spp->spp_vendor &&
    135   1.1   thorpej 		    PCI_PRODUCT(pa->pa_id) == spp->spp_product) {
    136   1.1   thorpej 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
    137   1.1   thorpej 			    PCI_SUBSYS_ID_REG);
    138   1.1   thorpej 			for (subspp = spp->spp_subsys;
    139   1.1   thorpej 			     subspp->spp_name != NULL; subspp++) {
    140   1.1   thorpej 				if (PCI_VENDOR(subsysid) ==
    141   1.1   thorpej 					subspp->spp_vendor ||
    142   1.1   thorpej 				    PCI_PRODUCT(subsysid) ==
    143   1.1   thorpej 					subspp->spp_product) {
    144   1.1   thorpej 					return (subspp);
    145   1.1   thorpej 				}
    146   1.1   thorpej 			}
    147   1.1   thorpej 			return (spp);
    148   1.1   thorpej 		}
    149   1.1   thorpej 	}
    150   1.1   thorpej 
    151   1.1   thorpej 	return (NULL);
    152   1.1   thorpej }
    153   1.1   thorpej 
    154   1.8   thorpej static int
    155  1.17    cegger sf_pci_match(device_t parent, cfdata_t match, void *aux)
    156   1.1   thorpej {
    157   1.1   thorpej 	struct pci_attach_args *pa = aux;
    158   1.1   thorpej 
    159   1.1   thorpej 	if (sf_pci_lookup(pa) != NULL)
    160   1.1   thorpej 		return (1);
    161   1.1   thorpej 
    162   1.1   thorpej 	return (0);
    163   1.1   thorpej }
    164   1.1   thorpej 
    165   1.8   thorpej static void
    166  1.14    dyoung sf_pci_attach(device_t parent, device_t self, void *aux)
    167   1.1   thorpej {
    168  1.14    dyoung 	struct sf_pci_softc *psc = device_private(self);
    169   1.1   thorpej 	struct sf_softc *sc = &psc->sc_starfire;
    170   1.1   thorpej 	struct pci_attach_args *pa = aux;
    171   1.1   thorpej 	pci_intr_handle_t ih;
    172   1.1   thorpej 	const char *intrstr = NULL;
    173   1.1   thorpej 	const struct sf_pci_product *spp;
    174   1.1   thorpej 	bus_space_tag_t iot, memt;
    175   1.1   thorpej 	bus_space_handle_t ioh, memh;
    176   1.1   thorpej 	pcireg_t reg;
    177  1.10  christos 	int error, ioh_valid, memh_valid;
    178  1.20  christos 	char intrbuf[PCI_INTRSTR_LEN];
    179   1.1   thorpej 
    180  1.19       chs 	sc->sc_dev = self;
    181   1.1   thorpej 	spp = sf_pci_lookup(pa);
    182   1.1   thorpej 	if (spp == NULL) {
    183   1.1   thorpej 		printf("\n");
    184   1.1   thorpej 		panic("sf_pci_attach: impossible");
    185   1.1   thorpej 	}
    186   1.1   thorpej 
    187   1.1   thorpej 	printf(": %s, rev. %d\n", spp->spp_name, PCI_REVISION(pa->pa_class));
    188   1.1   thorpej 
    189  1.10  christos 	/* power up chip */
    190  1.14    dyoung 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, self, NULL)) &&
    191  1.14    dyoung 	    error != EOPNOTSUPP) {
    192  1.21   msaitoh 		aprint_error_dev(self, "cannot activate %d\n", error);
    193  1.10  christos 		return;
    194   1.1   thorpej 	}
    195   1.1   thorpej 
    196   1.1   thorpej 	/*
    197   1.1   thorpej 	 * Map the device.
    198   1.1   thorpej 	 */
    199   1.1   thorpej 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
    200   1.1   thorpej 	switch (reg) {
    201   1.1   thorpej 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
    202   1.1   thorpej 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
    203   1.1   thorpej 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
    204   1.1   thorpej 		    reg, 0, &memt, &memh, NULL, NULL) == 0);
    205   1.1   thorpej 		break;
    206   1.1   thorpej 	default:
    207   1.1   thorpej 		memh_valid = 0;
    208   1.1   thorpej 	}
    209   1.1   thorpej 
    210   1.1   thorpej 	ioh_valid = (pci_mapreg_map(pa,
    211   1.1   thorpej 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
    212   1.1   thorpej 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
    213  1.21   msaitoh 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, NULL, NULL) == 0);
    214   1.1   thorpej 
    215   1.1   thorpej 	if (memh_valid) {
    216   1.1   thorpej 		sc->sc_st = memt;
    217   1.1   thorpej 		sc->sc_sh = memh;
    218   1.1   thorpej 		sc->sc_iomapped = 0;
    219   1.1   thorpej 	} else if (ioh_valid) {
    220   1.1   thorpej 		sc->sc_st = iot;
    221   1.1   thorpej 		sc->sc_sh = ioh;
    222   1.1   thorpej 		sc->sc_iomapped = 1;
    223   1.1   thorpej 	} else {
    224  1.19       chs 		aprint_error_dev(self, "unable to map device registers\n");
    225   1.1   thorpej 		return;
    226   1.1   thorpej 	}
    227   1.1   thorpej 
    228   1.1   thorpej 	sc->sc_dmat = pa->pa_dmat;
    229   1.1   thorpej 
    230   1.1   thorpej 	/* Make sure bus mastering is enabled. */
    231   1.1   thorpej 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    232   1.1   thorpej 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    233   1.1   thorpej 	    PCI_COMMAND_MASTER_ENABLE);
    234   1.1   thorpej 
    235   1.1   thorpej 	/*
    236   1.1   thorpej 	 * Map and establish our interrupt.
    237   1.1   thorpej 	 */
    238   1.1   thorpej 	if (pci_intr_map(pa, &ih)) {
    239  1.19       chs 		aprint_error_dev(self, "unable to map interrupt\n");
    240   1.1   thorpej 		return;
    241   1.1   thorpej 	}
    242  1.20  christos 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
    243  1.22  jdolecek 	psc->sc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_NET, sf_intr,
    244  1.22  jdolecek 	    sc, device_xname(self));
    245   1.1   thorpej 	if (psc->sc_ih == NULL) {
    246  1.19       chs 		aprint_error_dev(self, "unable to establish interrupt");
    247   1.1   thorpej 		if (intrstr != NULL)
    248  1.18     njoly 			aprint_error(" at %s", intrstr);
    249  1.18     njoly 		aprint_error("\n");
    250   1.1   thorpej 		return;
    251   1.1   thorpej 	}
    252  1.19       chs 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    253   1.1   thorpej 
    254   1.1   thorpej 	/*
    255   1.1   thorpej 	 * Finish off the attach.
    256   1.1   thorpej 	 */
    257   1.1   thorpej 	sf_attach(sc);
    258   1.1   thorpej }
    259