if_epic_pci.c revision 1.17 1 /* $NetBSD: if_epic_pci.c,v 1.17 2001/06/12 22:28:15 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * PCI bus front-end for the Standard Microsystems Corp. 83C170
42 * Ethernet PCI Integrated Controller (EPIC/100) driver.
43 */
44
45 #include "bpfilter.h"
46
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/errno.h>
55 #include <sys/device.h>
56
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
61
62 #if NBPFILTER > 0
63 #include <net/bpf.h>
64 #endif
65
66 #include <machine/bus.h>
67 #include <machine/intr.h>
68
69 #include <dev/mii/miivar.h>
70
71 #include <dev/ic/smc83c170reg.h>
72 #include <dev/ic/smc83c170var.h>
73
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcidevs.h>
77
78 /*
79 * PCI configuration space registers used by the EPIC.
80 */
81 #define EPIC_PCI_IOBA 0x10 /* i/o mapped base */
82 #define EPIC_PCI_MMBA 0x14 /* memory mapped base */
83
84 struct epic_pci_softc {
85 struct epic_softc sc_epic; /* real EPIC softc */
86
87 /* PCI-specific goo. */
88 void *sc_ih; /* interrupt handle */
89 };
90
91 int epic_pci_match(struct device *, struct cfdata *, void *);
92 void epic_pci_attach(struct device *, struct device *, void *);
93
94 struct cfattach epic_pci_ca = {
95 sizeof(struct epic_pci_softc), epic_pci_match, epic_pci_attach,
96 };
97
98 const struct epic_pci_product {
99 u_int32_t epp_prodid; /* PCI product ID */
100 const char *epp_name; /* device name */
101 } epic_pci_products[] = {
102 { PCI_PRODUCT_SMC_83C170, "SMC 83c170 Fast Ethernet" },
103 { PCI_PRODUCT_SMC_83C175, "SMC 83c175 Fast Ethernet" },
104 { 0, NULL },
105 };
106
107 const struct epic_pci_product *epic_pci_lookup(const struct pci_attach_args *);
108
109 const struct epic_pci_product *
110 epic_pci_lookup(pa)
111 const struct pci_attach_args *pa;
112 {
113 const struct epic_pci_product *epp;
114
115 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SMC)
116 return (NULL);
117
118 for (epp = epic_pci_products; epp->epp_name != NULL; epp++)
119 if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
120 return (epp);
121
122 return (NULL);
123 }
124
125 const struct epic_pci_subsys_info {
126 pcireg_t subsysid;
127 int flags;
128 } epic_pci_subsys_info[] = {
129 { PCI_ID_CODE(PCI_VENDOR_SMC, 0xa024), /* SMC9432BTX1 */
130 EPIC_HAS_BNC },
131 { PCI_ID_CODE(PCI_VENDOR_SMC, 0xa016), /* SMC9432FTX */
132 EPIC_HAS_MII_FIBER | EPIC_DUPLEXLED_ON_694 },
133 { 0xffffffff,
134 0 }
135 };
136
137 const struct epic_pci_subsys_info *
138 epic_pci_subsys_lookup(const struct pci_attach_args *);
139
140 const struct epic_pci_subsys_info *
141 epic_pci_subsys_lookup(pa)
142 const struct pci_attach_args *pa;
143 {
144 pci_chipset_tag_t pc = pa->pa_pc;
145 pcireg_t reg;
146 const struct epic_pci_subsys_info *esp;
147
148 reg = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
149
150 for (esp = epic_pci_subsys_info; esp->subsysid != 0xffffffff; esp++)
151 if (esp->subsysid == reg)
152 return (esp);
153
154 return (NULL);
155 }
156
157 int
158 epic_pci_match(parent, match, aux)
159 struct device *parent;
160 struct cfdata *match;
161 void *aux;
162 {
163 struct pci_attach_args *pa = aux;
164
165 if (epic_pci_lookup(pa) != NULL)
166 return (1);
167
168 return (0);
169 }
170
171 void
172 epic_pci_attach(parent, self, aux)
173 struct device *parent, *self;
174 void *aux;
175 {
176 struct epic_pci_softc *psc = (struct epic_pci_softc *)self;
177 struct epic_softc *sc = &psc->sc_epic;
178 struct pci_attach_args *pa = aux;
179 pci_chipset_tag_t pc = pa->pa_pc;
180 pci_intr_handle_t ih;
181 const char *intrstr = NULL;
182 const struct epic_pci_product *epp;
183 const struct epic_pci_subsys_info *esp;
184 bus_space_tag_t iot, memt;
185 bus_space_handle_t ioh, memh;
186 pcireg_t reg;
187 int pmreg, ioh_valid, memh_valid;
188
189 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
190 reg = pci_conf_read(pc, pa->pa_tag, pmreg + 4);
191 switch (reg & PCI_PMCSR_STATE_MASK) {
192 case PCI_PMCSR_STATE_D1:
193 case PCI_PMCSR_STATE_D2:
194 printf(": waking up from power state D%d\n%s",
195 reg & PCI_PMCSR_STATE_MASK, sc->sc_dev.dv_xname);
196 pci_conf_write(pc, pa->pa_tag, pmreg + 4,
197 (reg & ~PCI_PMCSR_STATE_MASK) |
198 PCI_PMCSR_STATE_D0);
199 break;
200 case PCI_PMCSR_STATE_D3:
201 /*
202 * IO and MEM are disabled. We can't enable
203 * the card because the BARs might be invalid.
204 */
205 printf(": unable to wake up from power state D3, "
206 "reboot required.\n");
207 pci_conf_write(pc, pa->pa_tag, pmreg + 4,
208 (reg & ~PCI_PMCSR_STATE_MASK) |
209 PCI_PMCSR_STATE_D0);
210 return;
211 }
212 }
213
214 /*
215 * Map the device.
216 */
217 ioh_valid = (pci_mapreg_map(pa, EPIC_PCI_IOBA,
218 PCI_MAPREG_TYPE_IO, 0,
219 &iot, &ioh, NULL, NULL) == 0);
220 memh_valid = (pci_mapreg_map(pa, EPIC_PCI_MMBA,
221 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
222 &memt, &memh, NULL, NULL) == 0);
223
224 if (memh_valid) {
225 sc->sc_st = memt;
226 sc->sc_sh = memh;
227 } else if (ioh_valid) {
228 sc->sc_st = iot;
229 sc->sc_sh = ioh;
230 } else {
231 printf(": unable to map device registers\n");
232 return;
233 }
234
235 sc->sc_dmat = pa->pa_dmat;
236
237 epp = epic_pci_lookup(pa);
238 if (epp == NULL) {
239 printf("\n");
240 panic("epic_pci_attach: impossible");
241 }
242
243 printf(": %s, rev. %d\n", epp->epp_name, PCI_REVISION(pa->pa_class));
244
245 /* Make sure bus mastering is enabled. */
246 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
247 pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
248 PCI_COMMAND_MASTER_ENABLE);
249
250 /*
251 * Map and establish our interrupt.
252 */
253 if (pci_intr_map(pa, &ih)) {
254 printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
255 return;
256 }
257 intrstr = pci_intr_string(pc, ih);
258 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, epic_intr, sc);
259 if (psc->sc_ih == NULL) {
260 printf("%s: unable to establish interrupt",
261 sc->sc_dev.dv_xname);
262 if (intrstr != NULL)
263 printf(" at %s", intrstr);
264 printf("\n");
265 return;
266 }
267 printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
268
269 esp = epic_pci_subsys_lookup(pa);
270 if (esp)
271 sc->sc_hwflags = esp->flags;
272
273 /*
274 * Finish off the attach.
275 */
276 epic_attach(sc);
277 }
278