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