Home | History | Annotate | Line # | Download | only in pci
if_hme_pci.c revision 1.3.4.2
      1  1.3.4.2  thorpej /*	$NetBSD: if_hme_pci.c,v 1.3.4.2 2002/01/10 19:56:40 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.1      mrg 
     35  1.3.4.2  thorpej #include <sys/cdefs.h>
     36  1.3.4.2  thorpej __KERNEL_RCSID(0, "$NetBSD: if_hme_pci.c,v 1.3.4.2 2002/01/10 19:56:40 thorpej Exp $");
     37  1.3.4.2  thorpej 
     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.4.1  thorpej #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.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.1      mrg int	hmematch_pci __P((struct device *, struct cfdata *, void *));
     69      1.1      mrg void	hmeattach_pci __P((struct device *, struct device *, void *));
     70      1.1      mrg 
     71      1.1      mrg struct cfattach hme_pci_ca = {
     72      1.1      mrg 	sizeof(struct hme_pci_softc), hmematch_pci, hmeattach_pci
     73      1.1      mrg };
     74      1.1      mrg 
     75      1.1      mrg int
     76      1.1      mrg hmematch_pci(parent, cf, aux)
     77      1.1      mrg 	struct device *parent;
     78      1.1      mrg 	struct cfdata *cf;
     79      1.1      mrg 	void *aux;
     80      1.1      mrg {
     81      1.1      mrg 	struct pci_attach_args *pa = aux;
     82      1.1      mrg 
     83      1.1      mrg 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
     84      1.1      mrg 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_HMENETWORK)
     85      1.1      mrg 		return (1);
     86      1.1      mrg 
     87      1.1      mrg 	return (0);
     88      1.1      mrg }
     89      1.1      mrg 
     90      1.1      mrg void
     91      1.1      mrg hmeattach_pci(parent, self, aux)
     92      1.1      mrg 	struct device *parent, *self;
     93      1.1      mrg 	void *aux;
     94      1.1      mrg {
     95      1.1      mrg 	struct pci_attach_args *pa = aux;
     96      1.1      mrg 	struct hme_pci_softc *hsc = (void *)self;
     97      1.1      mrg 	struct hme_softc *sc = &hsc->hsc_hme;
     98  1.3.4.2  thorpej 	pci_intr_handle_t ih;
     99      1.1      mrg 	pcireg_t csr;
    100      1.1      mrg 	const char *intrstr;
    101      1.1      mrg 	int type;
    102      1.1      mrg 
    103  1.3.4.2  thorpej 	/* XXX the following declarations should be elsewhere */
    104  1.3.4.2  thorpej 	extern void myetheraddr __P((u_char *));
    105  1.3.4.2  thorpej 
    106  1.3.4.2  thorpej 	printf(": Sun Happy Meal Ethernet, rev. %d\n",
    107  1.3.4.2  thorpej 	    PCI_REVISION(pa->pa_class));
    108  1.3.4.2  thorpej 
    109      1.1      mrg 	/*
    110      1.1      mrg 	 * enable io/memory-space accesses.  this is kinda of gross; but
    111      1.1      mrg 	 # the hme comes up with neither IO space enabled, or memory space.
    112      1.1      mrg 	 */
    113      1.1      mrg 	if (pa->pa_memt)
    114      1.1      mrg 		pa->pa_flags |= PCI_FLAGS_MEM_ENABLED;
    115      1.1      mrg 	if (pa->pa_iot)
    116      1.1      mrg 		pa->pa_flags |= PCI_FLAGS_IO_ENABLED;
    117      1.1      mrg 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    118      1.1      mrg 	if (pa->pa_memt) {
    119      1.1      mrg 		type = PCI_MAPREG_TYPE_MEM;
    120      1.1      mrg 		csr |= PCI_COMMAND_MEM_ENABLE;
    121      1.1      mrg 		sc->sc_bustag = pa->pa_memt;
    122      1.1      mrg 	} else {
    123      1.1      mrg 		type = PCI_MAPREG_TYPE_IO;
    124      1.1      mrg 		csr |= PCI_COMMAND_IO_ENABLE;
    125      1.1      mrg 		sc->sc_bustag = pa->pa_iot;
    126      1.1      mrg 	}
    127      1.1      mrg 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    128      1.1      mrg 	    csr | PCI_COMMAND_MEM_ENABLE);
    129      1.1      mrg 
    130      1.1      mrg 	sc->sc_dmatag = pa->pa_dmat;
    131      1.1      mrg 
    132      1.2      eeh 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
    133      1.1      mrg 	/*
    134      1.1      mrg 	 * Map five register banks:
    135      1.1      mrg 	 *
    136      1.1      mrg 	 *	bank 0: HME SEB registers:	+0x0000
    137      1.1      mrg 	 *	bank 1: HME ETX registers:	+0x2000
    138      1.1      mrg 	 *	bank 2: HME ERX registers:	+0x4000
    139      1.1      mrg 	 *	bank 3: HME MAC registers:	+0x6000
    140      1.1      mrg 	 *	bank 4: HME MIF registers:	+0x7000
    141      1.1      mrg 	 *
    142      1.1      mrg 	 */
    143      1.1      mrg 
    144      1.1      mrg #define PCI_HME_BASEADDR	0x10
    145      1.1      mrg 	if (pci_mapreg_map(pa, PCI_HME_BASEADDR, type, 0,
    146      1.1      mrg 	    &hsc->hsc_memt, &hsc->hsc_memh, NULL, NULL) != 0)
    147      1.1      mrg 	{
    148  1.3.4.2  thorpej 		printf("%s: unable to map device registers\n",
    149  1.3.4.2  thorpej 		    sc->sc_dev.dv_xname);
    150      1.1      mrg 		return;
    151      1.1      mrg 	}
    152      1.1      mrg 	sc->sc_seb = hsc->hsc_memh;
    153  1.3.4.2  thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x2000,
    154  1.3.4.2  thorpej 	    0x1000, &sc->sc_etx)) {
    155  1.3.4.2  thorpej 		printf("%s: unable to subregion ETX registers\n",
    156  1.3.4.2  thorpej 		    sc->sc_dev.dv_xname);
    157  1.3.4.2  thorpej 		return;
    158  1.3.4.2  thorpej 	}
    159  1.3.4.2  thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x4000,
    160  1.3.4.2  thorpej 	    0x1000, &sc->sc_erx)) {
    161  1.3.4.2  thorpej 		printf("%s: unable to subregion ERX registers\n",
    162  1.3.4.2  thorpej 		    sc->sc_dev.dv_xname);
    163  1.3.4.2  thorpej 		return;
    164  1.3.4.2  thorpej 	}
    165  1.3.4.2  thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x6000,
    166  1.3.4.2  thorpej 	    0x1000, &sc->sc_mac)) {
    167  1.3.4.2  thorpej 		printf("%s: unable to subregion MAC registers\n",
    168  1.3.4.2  thorpej 		    sc->sc_dev.dv_xname);
    169  1.3.4.2  thorpej 		return;
    170  1.3.4.2  thorpej 	}
    171  1.3.4.2  thorpej 	if (bus_space_subregion(hsc->hsc_memt, hsc->hsc_memh, 0x7000,
    172  1.3.4.2  thorpej 	    0x1000, &sc->sc_mif)) {
    173  1.3.4.2  thorpej 		printf("%s: unable to subregion MIF registers\n",
    174  1.3.4.2  thorpej 		    sc->sc_dev.dv_xname);
    175  1.3.4.2  thorpej 		return;
    176  1.3.4.2  thorpej 	}
    177      1.1      mrg 
    178      1.1      mrg 	myetheraddr(sc->sc_enaddr);
    179      1.1      mrg 
    180      1.1      mrg 	/*
    181  1.3.4.2  thorpej 	 * Map and establish our interrupt.
    182      1.1      mrg 	 */
    183  1.3.4.2  thorpej 	if (pci_intr_map(pa, &ih) != 0) {
    184  1.3.4.2  thorpej 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
    185  1.3.4.2  thorpej 		return;
    186      1.1      mrg 	}
    187  1.3.4.2  thorpej 	intrstr = pci_intr_string(pa->pa_pc, ih);
    188  1.3.4.2  thorpej 	hsc->hsc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, hme_intr, sc);
    189  1.3.4.2  thorpej 	if (hsc->hsc_ih == NULL) {
    190  1.3.4.2  thorpej 		printf("%s: unable to establish interrupt",
    191      1.1      mrg 		    sc->sc_dev.dv_xname);
    192      1.1      mrg 		if (intrstr != NULL)
    193      1.1      mrg 			printf(" at %s", intrstr);
    194      1.1      mrg 		printf("\n");
    195  1.3.4.2  thorpej 		return;
    196      1.1      mrg 	}
    197  1.3.4.2  thorpej 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
    198  1.3.4.2  thorpej 
    199  1.3.4.2  thorpej 	sc->sc_burst = 16;	/* XXX */
    200  1.3.4.2  thorpej 
    201  1.3.4.2  thorpej 	/* Finish off the attach. */
    202  1.3.4.2  thorpej 	hme_config(sc);
    203      1.1      mrg }
    204