1 /* $NetBSD: if_re_pci.c,v 1.37 2009/08/29 14:18:34 tsutsui Exp $ */
4 * Copyright (c) 1997, 1998-2003
5 * Bill Paul <wpaul@windriver.com>. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
35 /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
38 * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
40 * Written by Bill Paul <wpaul@windriver.com>
41 * Senior Networking Software Engineer
44 * NetBSD bus-specific frontends for written by
45 * Jonathan Stone <jonathan@netbsd.org>
48 #include <sys/cdefs.h>
49 __KERNEL_RCSID(0, "$NetBSD: if_re_pci.c,v 1.37 2009/08/29 14:18:34 tsutsui Exp $");
51 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/device.h>
58 #include <net/if_ether.h>
59 #include <net/if_media.h>
63 #include <dev/mii/mii.h>
64 #include <dev/mii/miivar.h>
66 #include <dev/pci/pcireg.h>
67 #include <dev/pci/pcivar.h>
68 #include <dev/pci/pcidevs.h>
70 #include <dev/ic/rtl81x9reg.h>
71 #include <dev/ic/rtl81x9var.h>
72 #include <dev/ic/rtl8169var.h>
75 struct rtk_softc sc_rtk
;
78 pci_chipset_tag_t sc_pc
;
82 static int re_pci_match(device_t
, cfdata_t
, void *);
83 static void re_pci_attach(device_t
, device_t
, void *);
85 CFATTACH_DECL_NEW(re_pci
, sizeof(struct re_pci_softc
),
86 re_pci_match
, re_pci_attach
, NULL
, NULL
);
89 * Various supported device vendors/types and their names.
91 static const struct rtk_type re_devs
[] = {
92 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8139
,
94 "RealTek 8139C+ 10/100BaseTX" },
95 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8101E
,
97 "RealTek 8100E/8101E/8102E/8102EL PCIe 10/100BaseTX" },
98 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8168
,
100 "RealTek 8168/8111 PCIe Gigabit Ethernet" },
101 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8169
,
103 "RealTek 8169/8110 Gigabit Ethernet" },
104 { PCI_VENDOR_REALTEK
, PCI_PRODUCT_REALTEK_RT8169SC
,
106 "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
107 { PCI_VENDOR_COREGA
, PCI_PRODUCT_COREGA_LAPCIGT
,
109 "Corega CG-LAPCIGT Gigabit Ethernet" },
110 { PCI_VENDOR_DLINK
, PCI_PRODUCT_DLINK_DGE528T
,
112 "D-Link DGE-528T Gigabit Ethernet" },
113 { PCI_VENDOR_USR2
, PCI_PRODUCT_USR2_USR997902
,
115 "US Robotics (3Com) USR997902 Gigabit Ethernet" },
116 { PCI_VENDOR_LINKSYS
, PCI_PRODUCT_LINKSYS_EG1032
,
118 "Linksys EG1032 rev. 3 Gigabit Ethernet" },
122 #define RE_LINKSYS_EG1032_SUBID 0x00241737
125 * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
126 * IDs against our list and return a device name if we find a match.
129 re_pci_match(device_t parent
, cfdata_t cf
, void *aux
)
131 const struct rtk_type
*t
;
132 struct pci_attach_args
*pa
= aux
;
135 subid
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_SUBSYS_ID_REG
);
137 /* special-case Linksys EG1032, since rev 2 uses sk(4) */
138 if (PCI_VENDOR(pa
->pa_id
) == PCI_VENDOR_LINKSYS
&&
139 PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_LINKSYS_EG1032
) {
140 if (subid
!= RE_LINKSYS_EG1032_SUBID
)
143 /* Don't match 8139 other than C-PLUS */
144 if (PCI_VENDOR(pa
->pa_id
) == PCI_VENDOR_REALTEK
&&
145 PCI_PRODUCT(pa
->pa_id
) == PCI_PRODUCT_REALTEK_RT8139
) {
146 if (PCI_REVISION(pa
->pa_class
) != 0x20)
152 while (t
->rtk_name
!= NULL
) {
153 if ((PCI_VENDOR(pa
->pa_id
) == t
->rtk_vid
) &&
154 (PCI_PRODUCT(pa
->pa_id
) == t
->rtk_did
)) {
155 return 2; /* defect rtk(4) */
164 re_pci_attach(device_t parent
, device_t self
, void *aux
)
166 struct re_pci_softc
*psc
= device_private(self
);
167 struct rtk_softc
*sc
= &psc
->sc_rtk
;
168 struct pci_attach_args
*pa
= aux
;
169 pci_chipset_tag_t pc
= pa
->pa_pc
;
170 pci_intr_handle_t ih
;
171 const char *intrstr
= NULL
;
172 const struct rtk_type
*t
;
175 pcireg_t command
, memtype
;
176 bool ioh_valid
, memh_valid
;
177 bus_space_tag_t iot
, memt
;
178 bus_space_handle_t ioh
, memh
;
179 bus_size_t iosize
, memsize
, bsize
;
184 * Map control/status registers.
186 command
= pci_conf_read(pa
->pa_pc
, pa
->pa_tag
, PCI_COMMAND_STATUS_REG
);
187 command
|= PCI_COMMAND_MASTER_ENABLE
;
188 pci_conf_write(pa
->pa_pc
, pa
->pa_tag
, PCI_COMMAND_STATUS_REG
, command
);
190 ioh_valid
= (pci_mapreg_map(pa
, RTK_PCI_LOIO
, PCI_MAPREG_TYPE_IO
, 0,
191 &iot
, &ioh
, NULL
, &iosize
) == 0);
192 memtype
= pci_mapreg_type(pa
->pa_pc
, pa
->pa_tag
, RTK_PCI_LOMEM
);
194 case PCI_MAPREG_TYPE_MEM
| PCI_MAPREG_MEM_TYPE_32BIT
:
195 case PCI_MAPREG_TYPE_MEM
| PCI_MAPREG_MEM_TYPE_64BIT
:
196 memh_valid
= (pci_mapreg_map(pa
, RTK_PCI_LOMEM
,
197 memtype
, 0, &memt
, &memh
, NULL
, &memsize
) == 0);
206 sc
->rtk_bhandle
= ioh
;
208 } else if (memh_valid
) {
210 sc
->rtk_bhandle
= memh
;
213 aprint_error(": can't map registers\n");
218 hwrev
= CSR_READ_4(sc
, RTK_TXCFG
) & RTK_TXCFG_HWREV
;
220 while (t
->rtk_name
!= NULL
) {
221 if ((PCI_VENDOR(pa
->pa_id
) == t
->rtk_vid
) &&
222 (PCI_PRODUCT(pa
->pa_id
) == t
->rtk_did
)) {
228 aprint_normal(": %s (rev. 0x%02x)\n",
229 t
->rtk_name
, PCI_REVISION(pa
->pa_class
));
231 if (t
->rtk_basetype
== RTK_8139CPLUS
)
232 sc
->sc_quirk
|= RTKQ_8139CPLUS
;
234 if (t
->rtk_basetype
== RTK_8168
||
235 t
->rtk_basetype
== RTK_8101E
)
236 sc
->sc_quirk
|= RTKQ_PCIE
;
238 sc
->sc_dmat
= pa
->pa_dmat
;
241 * No power/enable/disable machinery for PCI attach;
242 * mark the card enabled now.
244 sc
->sc_flags
|= RTK_ENABLED
;
246 /* Hook interrupt last to avoid having to lock softc */
247 /* Allocate interrupt */
248 if (pci_intr_map(pa
, &ih
)) {
249 aprint_error_dev(self
, "couldn't map interrupt\n");
252 intrstr
= pci_intr_string(pc
, ih
);
253 psc
->sc_ih
= pci_intr_establish(pc
, ih
, IPL_NET
, re_intr
, sc
);
254 if (psc
->sc_ih
== NULL
) {
255 aprint_error_dev(self
, "couldn't establish interrupt");
257 aprint_error(" at %s", intrstr
);
261 aprint_normal_dev(self
, "interrupting at %s\n", intrstr
);
266 * Perform hardware diagnostic on the original RTL8169.
267 * Some 32-bit cards were incorrectly wired and would
268 * malfunction if plugged into a 64-bit slot.
270 if (hwrev
== RTK_HWREV_8169
) {
273 aprint_error_dev(self
,
274 "attach aborted due to hardware diag failure\n");
278 if (psc
->sc_ih
!= NULL
) {
279 pci_intr_disestablish(pc
, psc
->sc_ih
);
284 bus_space_unmap(iot
, ioh
, iosize
);
286 bus_space_unmap(memt
, memh
, memsize
);