xref: /openbsd/sys/dev/pci/if_gem_pci.c (revision db3296cf)
1 /*	$OpenBSD: if_gem_pci.c,v 1.12 2002/11/20 00:31:03 jason Exp $	*/
2 /*	$NetBSD: if_gem_pci.c,v 1.1 2001/09/16 00:11:42 eeh Exp $ */
3 
4 /*
5  *
6  * Copyright (C) 2001 Eduardo Horvath.
7  * All rights reserved.
8  *
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR  ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR  BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  */
32 
33 /*
34  * PCI bindings for Sun GEM ethernet controllers.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/socket.h>
42 #include <sys/errno.h>
43 #include <sys/device.h>
44 
45 #include <machine/endian.h>
46 
47 #include <uvm/uvm_extern.h>
48 
49 #include <net/if.h>
50 #include <net/if_dl.h>
51 #include <net/if_media.h>
52 
53 #ifdef INET
54 #include <netinet/in.h>
55 #include <netinet/if_ether.h>
56 #endif
57 
58 #if NBPFILTER > 0
59 #include <net/bpf.h>
60 #endif
61 
62 #include <machine/bus.h>
63 #include <machine/intr.h>
64 
65 #ifdef __sparc64__
66 #include <dev/ofw/openfirm.h>
67 #endif
68 
69 #include <dev/mii/mii.h>
70 #include <dev/mii/miivar.h>
71 #include <dev/mii/mii_bitbang.h>
72 
73 #include <dev/ic/gemreg.h>
74 #include <dev/ic/gemvar.h>
75 
76 #include <dev/pci/pcivar.h>
77 #include <dev/pci/pcireg.h>
78 #include <dev/pci/pcidevs.h>
79 
80 struct gem_pci_softc {
81 	struct	gem_softc	gsc_gem;	/* GEM device */
82 	bus_space_tag_t		gsc_memt;
83 	bus_space_handle_t	gsc_memh;
84 	void			*gsc_ih;
85 };
86 
87 int	gem_match_pci(struct device *, void *, void *);
88 void	gem_attach_pci(struct device *, struct device *, void *);
89 
90 struct cfattach gem_pci_ca = {
91 	sizeof(struct gem_pci_softc), gem_match_pci, gem_attach_pci
92 };
93 
94 /*
95  * Attach routines need to be split out to different bus-specific files.
96  */
97 
98 const struct pci_matchid gem_pci_devices[] = {
99 	{ PCI_VENDOR_SUN, PCI_PRODUCT_SUN_ERINETWORK },
100 	{ PCI_VENDOR_SUN, PCI_PRODUCT_SUN_GEMNETWORK },
101 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_GMAC },
102 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_GMAC2 },
103 	{ PCI_VENDOR_APPLE, PCI_PRODUCT_APPLE_GMAC3 },
104 };
105 
106 int
107 gem_match_pci(parent, cf, aux)
108 	struct device *parent;
109 	void *cf;
110 	void *aux;
111 {
112 	return (pci_matchbyid((struct pci_attach_args *)aux, gem_pci_devices,
113 	    sizeof(gem_pci_devices)/sizeof(gem_pci_devices[0])));
114 }
115 
116 void
117 gem_attach_pci(parent, self, aux)
118 	struct device *parent, *self;
119 	void *aux;
120 {
121 	struct pci_attach_args *pa = aux;
122 	struct gem_pci_softc *gsc = (void *)self;
123 	struct gem_softc *sc = &gsc->gsc_gem;
124 	pci_intr_handle_t intrhandle;
125 #ifdef __sparc__
126 	/* XXX the following declarations should be elsewhere */
127 	extern void myetheraddr(u_char *);
128 #endif
129 	const char *intrstr;
130 	int type;
131 
132 	if (pa->pa_memt) {
133 		type = PCI_MAPREG_TYPE_MEM;
134 		sc->sc_bustag = pa->pa_memt;
135 	} else {
136 		type = PCI_MAPREG_TYPE_IO;
137 		sc->sc_bustag = pa->pa_iot;
138 	}
139 
140 	sc->sc_dmatag = pa->pa_dmat;
141 
142 	sc->sc_pci = 1; /* XXXXX should all be done in bus_dma. */
143 
144 #define PCI_GEM_BASEADDR	0x10
145 	if (pci_mapreg_map(pa, PCI_GEM_BASEADDR, type, 0,
146 	    &gsc->gsc_memt, &gsc->gsc_memh, NULL, NULL, 0) != 0)
147 	{
148 		printf(": could not map gem registers\n");
149 		return;
150 	}
151 
152 	sc->sc_bustag = gsc->gsc_memt;
153 	sc->sc_h = gsc->gsc_memh;
154 
155 #ifdef __sparc__
156 	if (OF_getprop(PCITAG_NODE(pa->pa_tag), "local-mac-address",
157 	    sc->sc_enaddr, ETHER_ADDR_LEN) <= 0)
158 		myetheraddr(sc->sc_enaddr);
159 #endif
160 #ifdef __powerpc__
161         pci_ether_hw_addr(pa->pa_pc, sc->sc_enaddr);
162 #endif
163 
164 	sc->sc_burst = 16;	/* XXX */
165 
166 	printf("\n");
167 	/*
168 	 * call the main configure
169 	 */
170 	gem_config(sc);
171 
172 	if (pci_intr_map(pa, &intrhandle) != 0) {
173 		printf("%s: couldn't map interrupt\n",
174 		    sc->sc_dev.dv_xname);
175 		return;	/* bus_unmap ? */
176 	}
177 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
178 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc,
179 	    intrhandle, IPL_NET, gem_intr, sc, self->dv_xname);
180 	if (gsc->gsc_ih != NULL) {
181 		printf("%s: using %s for interrupt\n",
182 		    sc->sc_dev.dv_xname,
183 		    intrstr ? intrstr : "unknown interrupt");
184 	} else {
185 		printf("%s: couldn't establish interrupt",
186 		    sc->sc_dev.dv_xname);
187 		if (intrstr != NULL)
188 			printf(" at %s", intrstr);
189 		printf("\n");
190 		return;	/* bus_unmap ? */
191 	}
192 }
193