xref: /openbsd-src/sys/dev/pci/if_gem_pci.c (revision 33b792a3c1c87b47219fdf9a73548c4003214de3)
1 /*	$OpenBSD: if_gem_pci.c,v 1.6 2002/01/28 01:04:02 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 #include <dev/mii/mii.h>
66 #include <dev/mii/miivar.h>
67 #include <dev/mii/mii_bitbang.h>
68 
69 #include <dev/ic/gemreg.h>
70 #include <dev/ic/gemvar.h>
71 
72 #include <dev/pci/pcivar.h>
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcidevs.h>
75 
76 struct gem_pci_softc {
77 	struct	gem_softc	gsc_gem;	/* GEM device */
78 	bus_space_tag_t		gsc_memt;
79 	bus_space_handle_t	gsc_memh;
80 	void			*gsc_ih;
81 };
82 
83 int	gem_match_pci __P((struct device *, void *, void *));
84 void	gem_attach_pci __P((struct device *, struct device *, void *));
85 
86 struct cfattach gem_pci_ca = {
87 	sizeof(struct gem_pci_softc), gem_match_pci, gem_attach_pci
88 };
89 
90 /*
91  * Attach routines need to be split out to different bus-specific files.
92  */
93 
94 int
95 gem_match_pci(parent, cf, aux)
96 	struct device *parent;
97 	void *cf;
98 	void *aux;
99 {
100 	struct pci_attach_args *pa = aux;
101 
102 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_SUN &&
103 	       (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_ERINETWORK ||
104 		PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_SUN_GEMNETWORK))
105 		return (1);
106 
107 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_APPLE &&
108 	       (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC ||
109 		PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_APPLE_GMAC2))
110 		return (1);
111 
112 	return (0);
113 }
114 
115 void
116 gem_attach_pci(parent, self, aux)
117 	struct device *parent, *self;
118 	void *aux;
119 {
120 	struct pci_attach_args *pa = aux;
121 	struct gem_pci_softc *gsc = (void *)self;
122 	struct gem_softc *sc = &gsc->gsc_gem;
123 	pci_intr_handle_t intrhandle;
124 #ifdef __sparc__
125 	/* XXX the following declarations should be elsewhere */
126 	extern void myetheraddr __P((u_char *));
127 #endif
128 	const char *intrstr;
129 	int type;
130 	char enaddr[ETHER_ADDR_LEN];
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 	myetheraddr(enaddr);
157 #endif
158 #ifdef __powerpc__
159         pci_ether_hw_addr(pa->pa_pc, enaddr);
160 #endif
161 
162 	printf("\n");
163 	/*
164 	 * call the main configure
165 	 */
166 	gem_attach(sc, enaddr);
167 
168 	if (pci_intr_map(pa, &intrhandle) != 0) {
169 		printf("%s: couldn't map interrupt\n",
170 		    sc->sc_dev.dv_xname);
171 		return;	/* bus_unmap ? */
172 	}
173 	intrstr = pci_intr_string(pa->pa_pc, intrhandle);
174 	gsc->gsc_ih = pci_intr_establish(pa->pa_pc,
175 	    intrhandle, IPL_NET, gem_intr, sc, self->dv_xname);
176 	if (gsc->gsc_ih != NULL) {
177 		printf("%s: using %s for interrupt\n",
178 		    sc->sc_dev.dv_xname,
179 		    intrstr ? intrstr : "unknown interrupt");
180 	} else {
181 		printf("%s: couldn't establish interrupt",
182 		    sc->sc_dev.dv_xname);
183 		if (intrstr != NULL)
184 			printf(" at %s", intrstr);
185 		printf("\n");
186 		return;	/* bus_unmap ? */
187 	}
188 }
189