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