Home | History | Annotate | Line # | Download | only in pci
if_hme_pci.c revision 1.37.30.1
      1  1.37.30.1  christos /*	$NetBSD: if_hme_pci.c,v 1.37.30.1 2019/06/10 22:07:16 christos Exp $	*/
      2        1.1       mrg 
      3        1.1       mrg /*
      4        1.1       mrg  * Copyright (c) 2000 Matthew R. Green
      5        1.1       mrg  * All rights reserved.
      6        1.1       mrg  *
      7        1.1       mrg  * Redistribution and use in source and binary forms, with or without
      8        1.1       mrg  * modification, are permitted provided that the following conditions
      9        1.1       mrg  * are met:
     10        1.1       mrg  * 1. Redistributions of source code must retain the above copyright
     11        1.1       mrg  *    notice, this list of conditions and the following disclaimer.
     12        1.1       mrg  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       mrg  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       mrg  *    documentation and/or other materials provided with the distribution.
     15        1.1       mrg  *
     16        1.1       mrg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17        1.1       mrg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18        1.1       mrg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19        1.1       mrg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20        1.1       mrg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21        1.1       mrg  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22        1.1       mrg  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23        1.1       mrg  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24        1.1       mrg  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1       mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1       mrg  * SUCH DAMAGE.
     27        1.1       mrg  */
     28        1.1       mrg 
     29        1.1       mrg /*
     30        1.1       mrg  * PCI front-end device driver for the HME ethernet device.
     31        1.1       mrg  */
     32        1.9     lukem 
     33        1.9     lukem #include <sys/cdefs.h>
     34  1.37.30.1  christos __KERNEL_RCSID(0, "$NetBSD: if_hme_pci.c,v 1.37.30.1 2019/06/10 22:07:16 christos Exp $");
     35        1.1       mrg 
     36        1.1       mrg #include <sys/param.h>
     37        1.1       mrg #include <sys/systm.h>
     38        1.1       mrg #include <sys/syslog.h>
     39        1.1       mrg #include <sys/device.h>
     40        1.1       mrg #include <sys/malloc.h>
     41        1.1       mrg #include <sys/socket.h>
     42        1.1       mrg 
     43        1.1       mrg #include <net/if.h>
     44        1.1       mrg #include <net/if_dl.h>
     45        1.1       mrg #include <net/if_ether.h>
     46        1.1       mrg #include <net/if_media.h>
     47        1.1       mrg 
     48        1.1       mrg #include <dev/mii/mii.h>
     49        1.1       mrg #include <dev/mii/miivar.h>
     50        1.1       mrg 
     51       1.23        ad #include <sys/intr.h>
     52        1.1       mrg 
     53        1.1       mrg #include <dev/pci/pcivar.h>
     54        1.1       mrg #include <dev/pci/pcireg.h>
     55        1.1       mrg #include <dev/pci/pcidevs.h>
     56        1.1       mrg 
     57        1.1       mrg #include <dev/ic/hmevar.h>
     58       1.13     itohy 
     59       1.35    dyoung #define PCI_HME_BASEADDR	PCI_BAR(0)
     60       1.34    dyoung 
     61        1.1       mrg struct hme_pci_softc {
     62        1.1       mrg 	struct	hme_softc	hsc_hme;	/* HME device */
     63        1.1       mrg 	bus_space_tag_t		hsc_memt;
     64        1.1       mrg 	bus_space_handle_t	hsc_memh;
     65        1.1       mrg 	void			*hsc_ih;
     66        1.1       mrg };
     67        1.1       mrg 
     68       1.27    cegger int	hmematch_pci(device_t, cfdata_t, void *);
     69       1.27    cegger void	hmeattach_pci(device_t, device_t, void *);
     70        1.1       mrg 
     71       1.29   tsutsui CFATTACH_DECL_NEW(hme_pci, sizeof(struct hme_pci_softc),
     72       1.12   thorpej     hmematch_pci, hmeattach_pci, NULL, NULL);
     73        1.1       mrg 
     74        1.1       mrg int
     75       1.27    cegger hmematch_pci(device_t parent, cfdata_t cf, void *aux)
     76        1.1       mrg {
     77        1.1       mrg 	struct pci_attach_args *pa = aux;
     78        1.1       mrg 
     79       1.16     perry 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
     80        1.1       mrg 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HMENETWORK)
     81        1.1       mrg 		return (1);
     82        1.1       mrg 
     83        1.1       mrg 	return (0);
     84        1.1       mrg }
     85        1.1       mrg 
     86       1.18  christos static inline int
     87       1.30   tsutsui hmepromvalid(uint8_t* buf)
     88       1.18  christos {
     89       1.18  christos 	return buf[0] == 0x18 && buf[1] == 0x00 &&	/* structure length */
     90       1.18  christos 	    buf[2] == 0x00 &&				/* revision */
     91       1.18  christos 	    (buf[3] == 0x00 ||				/* hme */
     92       1.18  christos 	     buf[3] == 0x80) &&				/* qfe */
     93       1.18  christos 	    buf[4] == PCI_SUBCLASS_NETWORK_ETHERNET &&	/* subclass code */
     94       1.18  christos 	    buf[5] == PCI_CLASS_NETWORK;		/* class code */
     95       1.18  christos }
     96       1.18  christos 
     97       1.18  christos static inline int
     98       1.19    atatat hmevpdoff(bus_space_tag_t romt, bus_space_handle_t romh, int vpdoff, int dev)
     99       1.18  christos {
    100       1.18  christos #define VPDLEN (3 + sizeof(struct pci_vpd) + ETHER_ADDR_LEN)
    101       1.18  christos 	if (bus_space_read_1(romt, romh, vpdoff + VPDLEN) != 0x79 &&
    102       1.18  christos 	    bus_space_read_1(romt, romh, vpdoff + 4 * VPDLEN) == 0x79) {
    103       1.18  christos 		/*
    104       1.18  christos 		 * Use the Nth NA for the Nth HME on
    105       1.18  christos 		 * this SUNW,qfe.
    106       1.18  christos 		 */
    107       1.18  christos 		vpdoff += dev * VPDLEN;
    108       1.18  christos 	}
    109       1.18  christos 	return vpdoff;
    110       1.18  christos }
    111       1.18  christos 
    112        1.1       mrg void
    113       1.27    cegger hmeattach_pci(device_t parent, device_t self, void *aux)
    114        1.1       mrg {
    115        1.1       mrg 	struct pci_attach_args *pa = aux;
    116       1.28    cegger 	struct hme_pci_softc *hsc = device_private(self);
    117        1.1       mrg 	struct hme_softc *sc = &hsc->hsc_hme;
    118        1.7   thorpej 	pci_intr_handle_t ih;
    119        1.1       mrg 	pcireg_t csr;
    120        1.1       mrg 	const char *intrstr;
    121        1.1       mrg 	int type;
    122       1.13     itohy 	struct pci_attach_args	ebus_pa;
    123       1.31    martin 	prop_data_t		eaddrprop;
    124       1.13     itohy 	pcireg_t		ebus_cl, ebus_id;
    125       1.30   tsutsui 	uint8_t			*enaddr;
    126       1.13     itohy 	bus_space_tag_t		romt;
    127       1.13     itohy 	bus_space_handle_t	romh;
    128       1.13     itohy 	bus_size_t		romsize;
    129       1.30   tsutsui 	uint8_t			buf[64];
    130       1.13     itohy 	int			dataoff, vpdoff;
    131       1.13     itohy 	struct pci_vpd		*vpd;
    132       1.30   tsutsui 	static const uint8_t promhdr[] = { 0x55, 0xaa };
    133       1.13     itohy #define PROMHDR_PTR_DATA	0x18
    134       1.30   tsutsui 	static const uint8_t promdat[] = {
    135       1.13     itohy 		0x50, 0x43, 0x49, 0x52,		/* "PCIR" */
    136       1.13     itohy 		PCI_VENDOR_SUN & 0xff, PCI_VENDOR_SUN >> 8,
    137       1.13     itohy 		PCI_PRODUCT_SUN_HMENETWORK & 0xff,
    138       1.13     itohy 		PCI_PRODUCT_SUN_HMENETWORK >> 8
    139       1.13     itohy 	};
    140       1.13     itohy #define PROMDATA_PTR_VPD	0x08
    141       1.16     perry #define PROMDATA_DATA2		0x0a
    142       1.37  christos 	char intrbuf[PCI_INTRSTR_LEN];
    143        1.7   thorpej 
    144       1.29   tsutsui 	sc->sc_dev = self;
    145       1.29   tsutsui 
    146       1.29   tsutsui 	aprint_normal(": Sun Happy Meal Ethernet, rev. %d\n",
    147        1.7   thorpej 	    PCI_REVISION(pa->pa_class));
    148       1.32       mrg 	aprint_naive(": Ethernet controller\n");
    149        1.7   thorpej 
    150       1.33    dyoung 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    151       1.34    dyoung 	type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, PCI_HME_BASEADDR);
    152       1.33    dyoung 
    153        1.1       mrg 	/*
    154        1.1       mrg 	 * enable io/memory-space accesses.  this is kinda of gross; but
    155       1.33    dyoung 	 * the hme comes up with neither IO space enabled, or memory space.
    156        1.1       mrg 	 */
    157       1.33    dyoung 	switch (type) {
    158       1.33    dyoung 	case PCI_MAPREG_TYPE_MEM:
    159        1.1       mrg 		csr |= PCI_COMMAND_MEM_ENABLE;
    160        1.1       mrg 		sc->sc_bustag = pa->pa_memt;
    161       1.33    dyoung 		break;
    162       1.33    dyoung 	case PCI_MAPREG_TYPE_IO:
    163        1.1       mrg 		csr |= PCI_COMMAND_IO_ENABLE;
    164        1.1       mrg 		sc->sc_bustag = pa->pa_iot;
    165       1.33    dyoung 		break;
    166        1.1       mrg 	}
    167        1.1       mrg 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    168        1.1       mrg 	    csr | PCI_COMMAND_MEM_ENABLE);
    169        1.1       mrg 
    170        1.1       mrg 	sc->sc_dmatag = pa->pa_dmat;
    171        1.1       mrg 
    172        1.2       eeh 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
    173        1.1       mrg 	/*
    174        1.1       mrg 	 * Map five register banks:
    175        1.1       mrg 	 *
    176        1.1       mrg 	 *	bank 0: HME SEB registers:	+0x0000
    177        1.1       mrg 	 *	bank 1: HME ETX registers:	+0x2000
    178        1.1       mrg 	 *	bank 2: HME ERX registers:	+0x4000
    179        1.1       mrg 	 *	bank 3: HME MAC registers:	+0x6000
    180        1.1       mrg 	 *	bank 4: HME MIF registers:	+0x7000
    181        1.1       mrg 	 *
    182        1.1       mrg 	 */
    183        1.1       mrg 
    184       1.34    dyoung 	if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0,
    185       1.29   tsutsui 	    &hsc->hsc_memt, &hsc->hsc_memh, NULL, NULL) != 0) {
    186       1.29   tsutsui 		aprint_error_dev(self, "unable to map device registers\n");
    187        1.1       mrg 		return;
    188        1.1       mrg 	}
    189        1.1       mrg 	sc->sc_seb = hsc->hsc_memh;
    190        1.7   thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000,
    191        1.7   thorpej 	    0x1000, &sc->sc_etx)) {
    192       1.29   tsutsui 		aprint_error_dev(self, "unable to subregion ETX registers\n");
    193        1.7   thorpej 		return;
    194        1.7   thorpej 	}
    195        1.7   thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000,
    196        1.7   thorpej 	    0x1000, &sc->sc_erx)) {
    197       1.29   tsutsui 		aprint_error_dev(self, "unable to subregion ERX registers\n");
    198        1.7   thorpej 		return;
    199        1.7   thorpej 	}
    200        1.7   thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000,
    201        1.7   thorpej 	    0x1000, &sc->sc_mac)) {
    202       1.29   tsutsui 		aprint_error_dev(self, "unable to subregion MAC registers\n");
    203        1.7   thorpej 		return;
    204        1.7   thorpej 	}
    205        1.7   thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000,
    206        1.7   thorpej 	    0x1000, &sc->sc_mif)) {
    207       1.29   tsutsui 		aprint_error_dev(self, "unable to subregion MIF registers\n");
    208        1.7   thorpej 		return;
    209        1.7   thorpej 	}
    210        1.1       mrg 
    211       1.31    martin 
    212       1.31    martin 	/*
    213       1.31    martin 	 * Check if we got a mac-address property passed
    214       1.31    martin 	 */
    215       1.31    martin 	eaddrprop = prop_dictionary_get(device_properties(self), "mac-address");
    216       1.31    martin 
    217       1.31    martin 	if (eaddrprop != NULL && prop_data_size(eaddrprop) == ETHER_ADDR_LEN) {
    218       1.31    martin 		memcpy(&sc->sc_enaddr, prop_data_data_nocopy(eaddrprop),
    219       1.31    martin 			    ETHER_ADDR_LEN);
    220       1.31    martin 		goto got_eaddr;
    221       1.31    martin 	}
    222       1.31    martin 
    223       1.13     itohy 	/*
    224       1.13     itohy 	 * Dig out VPD (vital product data) and acquire Ethernet address.
    225       1.13     itohy 	 * The VPD of hme resides in the Boot PROM (PCI FCode) attached
    226       1.13     itohy 	 * to the EBus interface.
    227       1.13     itohy 	 */
    228       1.13     itohy 	/*
    229       1.13     itohy 	 * ``Writing FCode 3.x Programs'' (newer ones, dated 1997 and later)
    230       1.13     itohy 	 * chapter 2 describes the data structure.
    231       1.13     itohy 	 */
    232       1.13     itohy 
    233       1.13     itohy 	enaddr = NULL;
    234       1.13     itohy 
    235       1.13     itohy 	/* get a PCI tag for the EBus bridge (function 0 of the same device) */
    236       1.13     itohy 	ebus_pa = *pa;
    237       1.13     itohy 	ebus_pa.pa_tag = pci_make_tag(pa->pa_pc, pa->pa_bus, pa->pa_device, 0);
    238       1.13     itohy 
    239       1.13     itohy 	ebus_cl = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_CLASS_REG);
    240       1.13     itohy 	ebus_id = pci_conf_read(ebus_pa.pa_pc, ebus_pa.pa_tag, PCI_ID_REG);
    241       1.13     itohy 
    242       1.13     itohy #define PCI_EBUS2_BOOTROM	0x10
    243       1.13     itohy 	if (PCI_CLASS(ebus_cl) == PCI_CLASS_BRIDGE &&
    244       1.13     itohy 	    PCI_PRODUCT(ebus_id) == PCI_PRODUCT_SUN_EBUS &&
    245       1.13     itohy 	    pci_mapreg_map(&ebus_pa, PCI_EBUS2_BOOTROM, PCI_MAPREG_TYPE_MEM,
    246       1.13     itohy 		BUS_SPACE_MAP_CACHEABLE | BUS_SPACE_MAP_PREFETCHABLE,
    247       1.13     itohy 		&romt, &romh, 0, &romsize) == 0) {
    248       1.13     itohy 
    249       1.13     itohy 		/* read PCI Expansion PROM Header */
    250       1.13     itohy 		bus_space_read_region_1(romt, romh, 0, buf, sizeof buf);
    251       1.13     itohy 		if (memcmp(buf, promhdr, sizeof promhdr) == 0 &&
    252       1.13     itohy 		    (dataoff = (buf[PROMHDR_PTR_DATA] |
    253       1.13     itohy 			(buf[PROMHDR_PTR_DATA + 1] << 8))) >= 0x1c) {
    254       1.13     itohy 
    255       1.13     itohy 			/* read PCI Expansion PROM Data */
    256       1.13     itohy 			bus_space_read_region_1(romt, romh, dataoff,
    257       1.13     itohy 			    buf, sizeof buf);
    258       1.13     itohy 			if (memcmp(buf, promdat, sizeof promdat) == 0 &&
    259       1.18  christos 			    hmepromvalid(buf + PROMDATA_DATA2) &&
    260       1.13     itohy 			    (vpdoff = (buf[PROMDATA_PTR_VPD] |
    261       1.13     itohy 				(buf[PROMDATA_PTR_VPD + 1] << 8))) >= 0x1c) {
    262       1.13     itohy 
    263       1.13     itohy 				/*
    264       1.13     itohy 				 * The VPD of hme is not in PCI 2.2 standard
    265       1.13     itohy 				 * format.  The length in the resource header
    266       1.13     itohy 				 * is in big endian, and resources are not
    267       1.13     itohy 				 * properly terminated (only one resource
    268       1.13     itohy 				 * and no end tag).
    269       1.13     itohy 				 */
    270       1.18  christos 				vpdoff = hmevpdoff(romt, romh, vpdoff,
    271       1.18  christos 				    pa->pa_device);
    272       1.13     itohy 				/* read PCI VPD */
    273       1.13     itohy 				bus_space_read_region_1(romt, romh,
    274       1.13     itohy 				    vpdoff, buf, sizeof buf);
    275       1.13     itohy 				vpd = (void *)(buf + 3);
    276       1.13     itohy 				if (PCI_VPDRES_ISLARGE(buf[0]) &&
    277       1.13     itohy 				    PCI_VPDRES_LARGE_NAME(buf[0])
    278       1.13     itohy 					== PCI_VPDRES_TYPE_VPD &&
    279       1.13     itohy 				    /* buf[1] == 0 && buf[2] == 9 && */ /*len*/
    280       1.13     itohy 				    vpd->vpd_key0 == 0x4e /* N */ &&
    281       1.13     itohy 				    vpd->vpd_key1 == 0x41 /* A */ &&
    282       1.13     itohy 				    vpd->vpd_len == ETHER_ADDR_LEN) {
    283       1.13     itohy 					/*
    284       1.13     itohy 					 * Ethernet address found
    285       1.13     itohy 					 */
    286       1.13     itohy 					enaddr = buf + 6;
    287       1.13     itohy 				}
    288       1.13     itohy 			}
    289       1.13     itohy 		}
    290       1.13     itohy 		bus_space_unmap(romt, romh, romsize);
    291       1.13     itohy 	}
    292       1.13     itohy 
    293       1.31    martin 	if (enaddr) {
    294       1.13     itohy 		memcpy(sc->sc_enaddr, enaddr, ETHER_ADDR_LEN);
    295       1.31    martin 		goto got_eaddr;
    296       1.31    martin 	}
    297       1.31    martin 
    298       1.31    martin 	aprint_error_dev(self, "no Ethernet address found\n");
    299       1.31    martin got_eaddr:
    300        1.1       mrg 
    301        1.7   thorpej 	/*
    302        1.7   thorpej 	 * Map and establish our interrupt.
    303        1.7   thorpej 	 */
    304        1.7   thorpej 	if (pci_intr_map(pa, &ih) != 0) {
    305       1.29   tsutsui 		aprint_error_dev(self, "unable to map interrupt\n");
    306        1.7   thorpej 		return;
    307       1.16     perry 	}
    308       1.37  christos 	intrstr = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
    309  1.37.30.1  christos 	hsc->hsc_ih = pci_intr_establish_xname(pa->pa_pc, ih, IPL_NET, hme_intr,
    310  1.37.30.1  christos 	    sc, device_xname(self));
    311        1.7   thorpej 	if (hsc->hsc_ih == NULL) {
    312       1.29   tsutsui 		aprint_error_dev(self, "unable to establish interrupt");
    313        1.7   thorpej 		if (intrstr != NULL)
    314       1.29   tsutsui 			aprint_error(" at %s", intrstr);
    315       1.29   tsutsui 		aprint_error("\n");
    316        1.8   thorpej 		return;
    317        1.7   thorpej 	}
    318       1.29   tsutsui 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
    319        1.1       mrg 
    320        1.1       mrg 	sc->sc_burst = 16;	/* XXX */
    321        1.1       mrg 
    322        1.7   thorpej 	/* Finish off the attach. */
    323        1.1       mrg 	hme_config(sc);
    324        1.1       mrg }
    325