if_re_pci.c revision 1.38.4.1 1 1.38.4.1 rmind /* $NetBSD: if_re_pci.c,v 1.38.4.1 2011/03/05 20:53:43 rmind Exp $ */
2 1.4 yamt
3 1.4 yamt /*
4 1.4 yamt * Copyright (c) 1997, 1998-2003
5 1.4 yamt * Bill Paul <wpaul (at) windriver.com>. All rights reserved.
6 1.4 yamt *
7 1.4 yamt * Redistribution and use in source and binary forms, with or without
8 1.4 yamt * modification, are permitted provided that the following conditions
9 1.4 yamt * are met:
10 1.4 yamt * 1. Redistributions of source code must retain the above copyright
11 1.4 yamt * notice, this list of conditions and the following disclaimer.
12 1.4 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.4 yamt * notice, this list of conditions and the following disclaimer in the
14 1.4 yamt * documentation and/or other materials provided with the distribution.
15 1.4 yamt * 3. All advertising materials mentioning features or use of this software
16 1.4 yamt * must display the following acknowledgement:
17 1.4 yamt * This product includes software developed by Bill Paul.
18 1.4 yamt * 4. Neither the name of the author nor the names of any co-contributors
19 1.4 yamt * may be used to endorse or promote products derived from this software
20 1.4 yamt * without specific prior written permission.
21 1.4 yamt *
22 1.4 yamt * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 1.4 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.4 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.4 yamt * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 1.4 yamt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.4 yamt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.4 yamt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.4 yamt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.4 yamt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.4 yamt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.4 yamt * THE POSSIBILITY OF SUCH DAMAGE.
33 1.4 yamt */
34 1.4 yamt
35 1.4 yamt /* $FreeBSD: /repoman/r/ncvs/src/sys/dev/re/if_re.c,v 1.20 2004/04/11 20:34:08 ru Exp $ */
36 1.4 yamt
37 1.1 jonathan /*
38 1.1 jonathan * RealTek 8139C+/8169/8169S/8110S PCI NIC driver
39 1.1 jonathan *
40 1.1 jonathan * Written by Bill Paul <wpaul (at) windriver.com>
41 1.1 jonathan * Senior Networking Software Engineer
42 1.1 jonathan * Wind River Systems
43 1.1 jonathan *
44 1.21 christos * NetBSD bus-specific frontends for written by
45 1.1 jonathan * Jonathan Stone <jonathan (at) netbsd.org>
46 1.1 jonathan */
47 1.1 jonathan
48 1.28 dsl #include <sys/cdefs.h>
49 1.38.4.1 rmind __KERNEL_RCSID(0, "$NetBSD: if_re_pci.c,v 1.38.4.1 2011/03/05 20:53:43 rmind Exp $");
50 1.1 jonathan
51 1.1 jonathan #include <sys/types.h>
52 1.1 jonathan
53 1.1 jonathan #include <sys/param.h>
54 1.1 jonathan #include <sys/systm.h>
55 1.1 jonathan #include <sys/device.h>
56 1.1 jonathan
57 1.1 jonathan #include <net/if.h>
58 1.1 jonathan #include <net/if_ether.h>
59 1.1 jonathan #include <net/if_media.h>
60 1.1 jonathan
61 1.30 ad #include <sys/bus.h>
62 1.5 briggs
63 1.1 jonathan #include <dev/mii/mii.h>
64 1.1 jonathan #include <dev/mii/miivar.h>
65 1.1 jonathan
66 1.5 briggs #include <dev/pci/pcireg.h>
67 1.5 briggs #include <dev/pci/pcivar.h>
68 1.5 briggs #include <dev/pci/pcidevs.h>
69 1.5 briggs
70 1.1 jonathan #include <dev/ic/rtl81x9reg.h>
71 1.1 jonathan #include <dev/ic/rtl81x9var.h>
72 1.1 jonathan #include <dev/ic/rtl8169var.h>
73 1.1 jonathan
74 1.1 jonathan struct re_pci_softc {
75 1.1 jonathan struct rtk_softc sc_rtk;
76 1.1 jonathan
77 1.1 jonathan void *sc_ih;
78 1.1 jonathan pci_chipset_tag_t sc_pc;
79 1.1 jonathan };
80 1.1 jonathan
81 1.34 tsutsui static int re_pci_match(device_t, cfdata_t, void *);
82 1.34 tsutsui static void re_pci_attach(device_t, device_t, void *);
83 1.38.4.1 rmind static int re_pci_detach(device_t, int);
84 1.34 tsutsui
85 1.34 tsutsui CFATTACH_DECL_NEW(re_pci, sizeof(struct re_pci_softc),
86 1.38.4.1 rmind re_pci_match, re_pci_attach, re_pci_detach, NULL);
87 1.1 jonathan
88 1.1 jonathan /*
89 1.1 jonathan * Various supported device vendors/types and their names.
90 1.1 jonathan */
91 1.6 jdolecek static const struct rtk_type re_devs[] = {
92 1.19 tsutsui { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
93 1.27 tsutsui RTK_8139CPLUS,
94 1.19 tsutsui "RealTek 8139C+ 10/100BaseTX" },
95 1.19 tsutsui { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E,
96 1.27 tsutsui RTK_8101E,
97 1.35 tnn "RealTek 8100E/8101E/8102E/8102EL PCIe 10/100BaseTX" },
98 1.19 tsutsui { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168,
99 1.27 tsutsui RTK_8168,
100 1.36 tsutsui "RealTek 8168/8111 PCIe Gigabit Ethernet" },
101 1.19 tsutsui { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169,
102 1.27 tsutsui RTK_8169,
103 1.27 tsutsui "RealTek 8169/8110 Gigabit Ethernet" },
104 1.19 tsutsui { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC,
105 1.27 tsutsui RTK_8169,
106 1.19 tsutsui "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
107 1.19 tsutsui { PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_LAPCIGT,
108 1.27 tsutsui RTK_8169,
109 1.19 tsutsui "Corega CG-LAPCIGT Gigabit Ethernet" },
110 1.19 tsutsui { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T,
111 1.27 tsutsui RTK_8169,
112 1.19 tsutsui "D-Link DGE-528T Gigabit Ethernet" },
113 1.19 tsutsui { PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902,
114 1.27 tsutsui RTK_8169,
115 1.19 tsutsui "US Robotics (3Com) USR997902 Gigabit Ethernet" },
116 1.19 tsutsui { PCI_VENDOR_LINKSYS, PCI_PRODUCT_LINKSYS_EG1032,
117 1.27 tsutsui RTK_8169,
118 1.19 tsutsui "Linksys EG1032 rev. 3 Gigabit Ethernet" },
119 1.1 jonathan };
120 1.1 jonathan
121 1.38.4.1 rmind static const struct rtk_type *
122 1.38.4.1 rmind re_pci_lookup(const struct pci_attach_args * pa)
123 1.38.4.1 rmind {
124 1.38.4.1 rmind int i;
125 1.38.4.1 rmind
126 1.38.4.1 rmind for(i = 0; i < __arraycount(re_devs); i++) {
127 1.38.4.1 rmind if (PCI_VENDOR(pa->pa_id) != re_devs[i].rtk_vid)
128 1.38.4.1 rmind continue;
129 1.38.4.1 rmind if (PCI_PRODUCT(pa->pa_id) == re_devs[i].rtk_did)
130 1.38.4.1 rmind return &re_devs[i];
131 1.38.4.1 rmind }
132 1.38.4.1 rmind
133 1.38.4.1 rmind return NULL;
134 1.38.4.1 rmind }
135 1.38.4.1 rmind
136 1.11 riz #define RE_LINKSYS_EG1032_SUBID 0x00241737
137 1.11 riz
138 1.1 jonathan /*
139 1.1 jonathan * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
140 1.1 jonathan * IDs against our list and return a device name if we find a match.
141 1.1 jonathan */
142 1.1 jonathan static int
143 1.34 tsutsui re_pci_match(device_t parent, cfdata_t cf, void *aux)
144 1.1 jonathan {
145 1.34 tsutsui struct pci_attach_args *pa = aux;
146 1.11 riz pcireg_t subid;
147 1.11 riz
148 1.11 riz subid = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
149 1.11 riz
150 1.11 riz /* special-case Linksys EG1032, since rev 2 uses sk(4) */
151 1.11 riz if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
152 1.27 tsutsui PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032) {
153 1.27 tsutsui if (subid != RE_LINKSYS_EG1032_SUBID)
154 1.27 tsutsui return 0;
155 1.27 tsutsui }
156 1.38.4.1 rmind
157 1.27 tsutsui /* Don't match 8139 other than C-PLUS */
158 1.27 tsutsui if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
159 1.27 tsutsui PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139) {
160 1.27 tsutsui if (PCI_REVISION(pa->pa_class) != 0x20)
161 1.27 tsutsui return 0;
162 1.27 tsutsui }
163 1.1 jonathan
164 1.38.4.1 rmind if (re_pci_lookup(pa) != NULL)
165 1.38.4.1 rmind return 2; /* defeat rtk(4) */
166 1.1 jonathan
167 1.1 jonathan return 0;
168 1.1 jonathan }
169 1.1 jonathan
170 1.1 jonathan static void
171 1.34 tsutsui re_pci_attach(device_t parent, device_t self, void *aux)
172 1.1 jonathan {
173 1.34 tsutsui struct re_pci_softc *psc = device_private(self);
174 1.34 tsutsui struct rtk_softc *sc = &psc->sc_rtk;
175 1.34 tsutsui struct pci_attach_args *pa = aux;
176 1.1 jonathan pci_chipset_tag_t pc = pa->pa_pc;
177 1.1 jonathan pci_intr_handle_t ih;
178 1.1 jonathan const char *intrstr = NULL;
179 1.34 tsutsui const struct rtk_type *t;
180 1.34 tsutsui uint32_t hwrev;
181 1.34 tsutsui pcireg_t command, memtype;
182 1.34 tsutsui bool ioh_valid, memh_valid;
183 1.34 tsutsui bus_space_tag_t iot, memt;
184 1.34 tsutsui bus_space_handle_t ioh, memh;
185 1.38.4.1 rmind bus_size_t iosize, memsize;
186 1.34 tsutsui
187 1.34 tsutsui sc->sc_dev = self;
188 1.38.4.1 rmind psc->sc_pc = pa->pa_pc;
189 1.1 jonathan
190 1.1 jonathan command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
191 1.1 jonathan command |= PCI_COMMAND_MASTER_ENABLE;
192 1.1 jonathan pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
193 1.1 jonathan
194 1.38.4.1 rmind /*
195 1.38.4.1 rmind * Map control/status registers.
196 1.38.4.1 rmind */
197 1.22 tsutsui ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
198 1.22 tsutsui &iot, &ioh, NULL, &iosize) == 0);
199 1.27 tsutsui memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, RTK_PCI_LOMEM);
200 1.27 tsutsui switch (memtype) {
201 1.27 tsutsui case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
202 1.27 tsutsui case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
203 1.27 tsutsui memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM,
204 1.27 tsutsui memtype, 0, &memt, &memh, NULL, &memsize) == 0);
205 1.27 tsutsui break;
206 1.27 tsutsui default:
207 1.27 tsutsui memh_valid = 0;
208 1.27 tsutsui break;
209 1.27 tsutsui }
210 1.27 tsutsui
211 1.22 tsutsui if (ioh_valid) {
212 1.22 tsutsui sc->rtk_btag = iot;
213 1.22 tsutsui sc->rtk_bhandle = ioh;
214 1.38.4.1 rmind sc->rtk_bsize = iosize;
215 1.38.4.1 rmind if (memh_valid)
216 1.38.4.1 rmind bus_space_unmap(memt, memh, memsize);
217 1.22 tsutsui } else if (memh_valid) {
218 1.22 tsutsui sc->rtk_btag = memt;
219 1.22 tsutsui sc->rtk_bhandle = memh;
220 1.38.4.1 rmind sc->rtk_bsize = memsize;
221 1.22 tsutsui } else {
222 1.34 tsutsui aprint_error(": can't map registers\n");
223 1.2 kanaoka return;
224 1.1 jonathan }
225 1.22 tsutsui
226 1.38.4.1 rmind t = re_pci_lookup(pa);
227 1.38.4.1 rmind KASSERT(t != NULL);
228 1.1 jonathan
229 1.34 tsutsui aprint_normal(": %s (rev. 0x%02x)\n",
230 1.34 tsutsui t->rtk_name, PCI_REVISION(pa->pa_class));
231 1.34 tsutsui
232 1.27 tsutsui if (t->rtk_basetype == RTK_8139CPLUS)
233 1.27 tsutsui sc->sc_quirk |= RTKQ_8139CPLUS;
234 1.27 tsutsui
235 1.27 tsutsui if (t->rtk_basetype == RTK_8168 ||
236 1.27 tsutsui t->rtk_basetype == RTK_8101E)
237 1.27 tsutsui sc->sc_quirk |= RTKQ_PCIE;
238 1.27 tsutsui
239 1.38.4.1 rmind if (pci_dma64_available(pa) && (sc->sc_quirk & RTKQ_PCIE))
240 1.38.4.1 rmind sc->sc_dmat = pa->pa_dmat64;
241 1.38.4.1 rmind else
242 1.38.4.1 rmind sc->sc_dmat = pa->pa_dmat;
243 1.1 jonathan
244 1.1 jonathan /*
245 1.29 simonb * No power/enable/disable machinery for PCI attach;
246 1.1 jonathan * mark the card enabled now.
247 1.1 jonathan */
248 1.1 jonathan sc->sc_flags |= RTK_ENABLED;
249 1.8 perry
250 1.1 jonathan /* Hook interrupt last to avoid having to lock softc */
251 1.1 jonathan /* Allocate interrupt */
252 1.1 jonathan if (pci_intr_map(pa, &ih)) {
253 1.34 tsutsui aprint_error_dev(self, "couldn't map interrupt\n");
254 1.2 kanaoka return;
255 1.1 jonathan }
256 1.1 jonathan intrstr = pci_intr_string(pc, ih);
257 1.1 jonathan psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc);
258 1.1 jonathan if (psc->sc_ih == NULL) {
259 1.34 tsutsui aprint_error_dev(self, "couldn't establish interrupt");
260 1.1 jonathan if (intrstr != NULL)
261 1.3 kanaoka aprint_error(" at %s", intrstr);
262 1.3 kanaoka aprint_error("\n");
263 1.1 jonathan return;
264 1.1 jonathan }
265 1.34 tsutsui aprint_normal_dev(self, "interrupting at %s\n", intrstr);
266 1.1 jonathan
267 1.2 kanaoka re_attach(sc);
268 1.2 kanaoka
269 1.38.4.1 rmind hwrev = CSR_READ_4(sc, RTK_TXCFG) & RTK_TXCFG_HWREV;
270 1.38.4.1 rmind
271 1.2 kanaoka /*
272 1.17 tsutsui * Perform hardware diagnostic on the original RTL8169.
273 1.17 tsutsui * Some 32-bit cards were incorrectly wired and would
274 1.17 tsutsui * malfunction if plugged into a 64-bit slot.
275 1.2 kanaoka */
276 1.17 tsutsui if (hwrev == RTK_HWREV_8169) {
277 1.38.4.1 rmind if (re_diag(sc)) {
278 1.38.4.1 rmind re_pci_detach(self, 0);
279 1.38.4.1 rmind aprint_error_dev(self, "disabled\n");
280 1.2 kanaoka }
281 1.2 kanaoka }
282 1.1 jonathan }
283 1.38.4.1 rmind
284 1.38.4.1 rmind static int
285 1.38.4.1 rmind re_pci_detach(device_t self, int flags)
286 1.38.4.1 rmind {
287 1.38.4.1 rmind struct re_pci_softc *psc = device_private(self);
288 1.38.4.1 rmind struct rtk_softc *sc = &psc->sc_rtk;
289 1.38.4.1 rmind int rv;
290 1.38.4.1 rmind
291 1.38.4.1 rmind if ((sc->sc_flags & RTK_ATTACHED) != 0)
292 1.38.4.1 rmind /* re_stop() */
293 1.38.4.1 rmind sc->ethercom.ec_if.if_stop(&sc->ethercom.ec_if, 0);
294 1.38.4.1 rmind
295 1.38.4.1 rmind rv = re_detach(sc);
296 1.38.4.1 rmind if (rv)
297 1.38.4.1 rmind return rv;
298 1.38.4.1 rmind
299 1.38.4.1 rmind if (psc->sc_ih != NULL) {
300 1.38.4.1 rmind pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
301 1.38.4.1 rmind psc->sc_ih = NULL;
302 1.38.4.1 rmind }
303 1.38.4.1 rmind
304 1.38.4.1 rmind if (sc->rtk_bsize != 0) {
305 1.38.4.1 rmind bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize);
306 1.38.4.1 rmind sc->rtk_bsize = 0;
307 1.38.4.1 rmind }
308 1.38.4.1 rmind
309 1.38.4.1 rmind return 0;
310 1.38.4.1 rmind }
311