if_re_pci.c revision 1.12.8.1 1 /* $NetBSD: if_re_pci.c,v 1.12.8.1 2006/05/24 10:58:00 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1997, 1998-2003
5 * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Bill Paul.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
36
37 /*
38 * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
39 *
40 * Written by Bill Paul <wpaul (at) windriver.com>
41 * Senior Networking Software Engineer
42 * Wind River Systems
43 *
44 * Netbsd bus-specific frontends for written by
45 * Jonathan Stone <jonathan (at) netbsd.org>
46 */
47
48 #include "bpfilter.h"
49 #include "vlan.h"
50
51 #include <sys/types.h>
52
53 #include <sys/param.h>
54 #include <sys/endian.h>
55 #include <sys/systm.h>
56 #include <sys/sockio.h>
57 #include <sys/mbuf.h>
58 #include <sys/malloc.h>
59 #include <sys/kernel.h>
60 #include <sys/socket.h>
61 #include <sys/device.h>
62
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_ether.h>
67 #include <net/if_media.h>
68 #include <net/if_vlanvar.h>
69
70 #include <machine/bus.h>
71
72 #include <dev/mii/mii.h>
73 #include <dev/mii/miivar.h>
74
75 #include <dev/pci/pcireg.h>
76 #include <dev/pci/pcivar.h>
77 #include <dev/pci/pcidevs.h>
78
79 /*
80 * Default to using PIO access for this driver.
81 */
82 /*#define RE_USEIOSPACE */
83 #undef RE_USEIOSPACE
84
85 #include <dev/ic/rtl81x9reg.h>
86 #include <dev/ic/rtl81x9var.h>
87 #include <dev/ic/rtl8169var.h>
88
89 struct re_pci_softc {
90 struct rtk_softc sc_rtk;
91
92 void *sc_ih;
93 pci_chipset_tag_t sc_pc;
94 pcitag_t sc_pcitag;
95 };
96
97 static int re_pci_probe(struct device *, struct cfdata *, void *);
98 static void re_pci_attach(struct device *, struct device *, void *);
99
100 /*
101 * Various supported device vendors/types and their names.
102 */
103 static const struct rtk_type re_devs[] = {
104 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139, RTK_HWREV_8139CPLUS,
105 "RealTek 8139C+ 10/100BaseTX" },
106 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169,
107 "RealTek 8169 Gigabit Ethernet" },
108 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169S,
109 "RealTek 8169S Single-chip Gigabit Ethernet" },
110 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8169SB,
111 "RealTek 8169SB Single-chip Gigabit Ethernet" },
112 { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT, RTK_HWREV_8169S,
113 "Corega CG-LAPCIGT Gigabit Ethernet" },
114 { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169, RTK_HWREV_8110S,
115 "RealTek 8110S Single-chip Gigabit Ethernet" },
116 { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T, RTK_HWREV_8169S,
117 "D-Link DGE-528T Gigabit Ethernet" },
118 { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902, RTK_HWREV_8169S,
119 "US Robotics (3Com) USR997902 Gigabit Ethernet" },
120 { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032, RTK_HWREV_8169S,
121 "Linksys EG1032 rev. 3 Gigabit Ethernet" },
122 { 0, 0, 0, NULL }
123 };
124
125 static const struct rtk_hwrev re_hwrevs[] = {
126 { RTK_HWREV_8139, RTK_8139, "" },
127 { RTK_HWREV_8139A, RTK_8139, "A" },
128 { RTK_HWREV_8139AG, RTK_8139, "A-G" },
129 { RTK_HWREV_8139B, RTK_8139, "B" },
130 { RTK_HWREV_8130, RTK_8139, "8130" },
131 { RTK_HWREV_8139C, RTK_8139, "C" },
132 { RTK_HWREV_8139D, RTK_8139, "8139D/8100B/8100C" },
133 { RTK_HWREV_8139CPLUS, RTK_8139CPLUS, "C+"},
134 { RTK_HWREV_8169, RTK_8169, "8169"},
135 { RTK_HWREV_8169S, RTK_8169, "8169S"},
136 { RTK_HWREV_8169SB, RTK_8169, "8169SB"},
137 { RTK_HWREV_8110S, RTK_8169, "8110S"},
138 { RTK_HWREV_8100, RTK_8139, "8100"},
139 { RTK_HWREV_8101, RTK_8139, "8101"},
140 { 0, 0, NULL }
141 };
142
143 #define RE_LINKSYS_EG1032_SUBID 0x00241737
144
145 CFATTACH_DECL(re_pci, sizeof(struct re_pci_softc), re_pci_probe, re_pci_attach,
146 NULL, NULL);
147
148 /*
149 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
150 * IDs against our list and return a device name if we find a match.
151 */
152 static int
153 re_pci_probe(struct device *parent, struct cfdata *match, void *aux)
154 {
155 const struct rtk_type *t;
156 struct pci_attach_args *pa = aux;
157 bus_space_tag_t rtk_btag;
158 bus_space_handle_t rtk_bhandle;
159 bus_size_t bsize;
160 u_int32_t hwrev;
161 pcireg_t subid;
162
163 subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
164
165 /* special-case Linksys EG1032, since rev 2 uses sk(4) */
166 if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
167 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032 &&
168 subid == RE_LINKSYS_EG1032_SUBID)
169 return 1;
170
171 t = re_devs;
172
173 while (t->rtk_name != NULL) {
174 if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
175 (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
176
177 /*
178 * Temporarily map the I/O space
179 * so we can read the chip ID register.
180 */
181 #ifdef RE_USEIOSPACE
182 if (pci_mapreg_map(pa, RTK_PCI_LOIO,
183 PCI_MAPREG_TYPE_IO, 0, &rtk_btag,
184 &rtk_bhandle, NULL, &bsize)) {
185 aprint_error("can't map i/o space\n");
186 return 0;
187 }
188 #else
189 if (pci_mapreg_map(pa, RTK_PCI_LOMEM,
190 PCI_MAPREG_TYPE_MEM, 0, &rtk_btag,
191 &rtk_bhandle, NULL, &bsize)) {
192 aprint_error("can't map mem space\n");
193 return 0;
194 }
195 #endif
196 hwrev = bus_space_read_4(rtk_btag, rtk_bhandle,
197 RTK_TXCFG) & RTK_TXCFG_HWREV;
198 bus_space_unmap(rtk_btag, rtk_bhandle, bsize);
199 if (t->rtk_basetype == hwrev)
200 return 2; /* defeat rtk(4) */
201 }
202 t++;
203 }
204
205 return 0;
206 }
207
208 static void
209 re_pci_attach(struct device *parent, struct device *self, void *aux)
210 {
211 struct re_pci_softc *psc = (void *)self;
212 struct rtk_softc *sc = &psc->sc_rtk;
213 struct pci_attach_args *pa = aux;
214 pci_chipset_tag_t pc = pa->pa_pc;
215 pci_intr_handle_t ih;
216 const char *intrstr = NULL;
217 const struct rtk_type *t;
218 const struct rtk_hwrev *hw_rev;
219 int hwrev;
220 int error = 0;
221 int pmreg;
222 pcireg_t command;
223 bus_size_t bsize;
224
225
226 /*
227 * Handle power management nonsense.
228 */
229 if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
230 command = pci_conf_read(pc, pa->pa_tag, pmreg + PCI_PMCSR);
231 if (command & RTK_PSTATE_MASK) {
232 u_int32_t iobase, membase, irq;
233
234 /* Save important PCI config data. */
235 iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
236 membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
237 irq = pci_conf_read(pc, pa->pa_tag, PCI_INTERRUPT_REG);
238
239 /* Reset the power state. */
240 aprint_normal("%s: chip is is in D%d power mode "
241 "-- setting to D0\n", sc->sc_dev.dv_xname,
242 command & RTK_PSTATE_MASK);
243
244 command &= ~RTK_PSTATE_MASK;
245 pci_conf_write(pc, pa->pa_tag,
246 pmreg + PCI_PMCSR, command);
247
248 /* Restore PCI config data. */
249 pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
250 pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
251 pci_conf_write(pc, pa->pa_tag, PCI_INTERRUPT_REG, irq);
252 }
253 }
254
255 /*
256 * Map control/status registers.
257 */
258 command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
259 command |= PCI_COMMAND_MASTER_ENABLE;
260 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
261
262 #ifdef RE_USEIOSPACE
263 if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
264 &sc->rtk_btag, &sc->rtk_bhandle, NULL, &bsize)) {
265 aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
266 return;
267 }
268 #else
269 if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
270 &sc->rtk_btag, &sc->rtk_bhandle, NULL, &bsize)) {
271 aprint_error("%s: can't map mem space\n", sc->sc_dev.dv_xname);
272 return;
273 }
274 #endif
275 t = re_devs;
276 hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
277
278 while (t->rtk_name != NULL) {
279 if ((PCI_VENDOR(pa->pa_id) == t->rtk_vid) &&
280 (PCI_PRODUCT(pa->pa_id) == t->rtk_did)) {
281
282 if (t->rtk_basetype == hwrev)
283 break;
284 }
285 t++;
286 }
287 aprint_normal(": %s\n", t->rtk_name);
288
289 hw_rev = re_hwrevs;
290 hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
291 while (hw_rev->rtk_desc != NULL) {
292 if (hw_rev->rtk_rev == hwrev) {
293 sc->rtk_type = hw_rev->rtk_type;
294 break;
295 }
296 hw_rev++;
297 }
298
299 sc->sc_dmat = pa->pa_dmat;
300
301 /*
302 * No power/enable/disable machinery for PCI attac;
303 * mark the card enabled now.
304 */
305 sc->sc_flags |= RTK_ENABLED;
306
307 /* Hook interrupt last to avoid having to lock softc */
308 /* Allocate interrupt */
309 if (pci_intr_map(pa, &ih)) {
310 aprint_error("%s: couldn't map interrupt\n",
311 sc->sc_dev.dv_xname);
312 return;
313 }
314 intrstr = pci_intr_string(pc, ih);
315 psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
316 if (psc->sc_ih == NULL) {
317 aprint_error("%s: couldn't establish interrupt",
318 sc->sc_dev.dv_xname);
319 if (intrstr != NULL)
320 aprint_error(" at %s", intrstr);
321 aprint_error("\n");
322 return;
323 }
324 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
325
326 re_attach(sc);
327
328 /*
329 * Perform hardware diagnostic.
330 * XXX: this diagnostic only makes sense for attachemnts with 64-bit
331 * busses: PCI, but not CardBus.
332 */
333 error = re_diag(sc);
334 if (error) {
335 aprint_error(
336 "%s: attach aborted due to hardware diag failure\n",
337 sc->sc_dev.dv_xname);
338
339 re_detach(sc);
340
341 if (psc->sc_ih != NULL) {
342 pci_intr_disestablish(pc, psc->sc_ih);
343 psc->sc_ih = NULL;
344 }
345
346 if (bsize)
347 bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, bsize);
348 }
349 }
350