if_rtk_pci.c revision 1.8.2.4 1 1.8.2.4 jdolecek /* $NetBSD: if_rtk_pci.c,v 1.8.2.4 2002/10/10 18:40:45 jdolecek Exp $ */
2 1.1 haya
3 1.1 haya /*
4 1.1 haya * Copyright (c) 1997, 1998
5 1.1 haya * Bill Paul <wpaul (at) ctr.columbia.edu>. All rights reserved.
6 1.1 haya *
7 1.1 haya * Redistribution and use in source and binary forms, with or without
8 1.1 haya * modification, are permitted provided that the following conditions
9 1.1 haya * are met:
10 1.1 haya * 1. Redistributions of source code must retain the above copyright
11 1.1 haya * notice, this list of conditions and the following disclaimer.
12 1.1 haya * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 haya * notice, this list of conditions and the following disclaimer in the
14 1.1 haya * documentation and/or other materials provided with the distribution.
15 1.1 haya * 3. All advertising materials mentioning features or use of this software
16 1.1 haya * must display the following acknowledgement:
17 1.1 haya * This product includes software developed by Bill Paul.
18 1.1 haya * 4. Neither the name of the author nor the names of any co-contributors
19 1.1 haya * may be used to endorse or promote products derived from this software
20 1.1 haya * without specific prior written permission.
21 1.1 haya *
22 1.1 haya * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23 1.1 haya * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.1 haya * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.1 haya * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26 1.1 haya * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 1.1 haya * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 1.1 haya * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 1.1 haya * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 1.1 haya * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 1.1 haya * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 1.1 haya * THE POSSIBILITY OF SUCH DAMAGE.
33 1.1 haya *
34 1.1 haya * FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
35 1.1 haya */
36 1.1 haya
37 1.1 haya /*
38 1.1 haya * RealTek 8129/8139 PCI NIC driver
39 1.1 haya *
40 1.1 haya * Supports several extremely cheap PCI 10/100 adapters based on
41 1.1 haya * the RealTek chipset. Datasheets can be obtained from
42 1.1 haya * www.realtek.com.tw.
43 1.1 haya *
44 1.1 haya * Written by Bill Paul <wpaul (at) ctr.columbia.edu>
45 1.1 haya * Electrical Engineering Department
46 1.1 haya * Columbia University, New York City
47 1.1 haya */
48 1.1 haya
49 1.8.2.2 thorpej #include <sys/cdefs.h>
50 1.8.2.4 jdolecek __KERNEL_RCSID(0, "$NetBSD: if_rtk_pci.c,v 1.8.2.4 2002/10/10 18:40:45 jdolecek Exp $");
51 1.1 haya
52 1.1 haya #include <sys/param.h>
53 1.1 haya #include <sys/systm.h>
54 1.1 haya #include <sys/callout.h>
55 1.1 haya #include <sys/device.h>
56 1.1 haya #include <sys/sockio.h>
57 1.1 haya #include <sys/mbuf.h>
58 1.1 haya #include <sys/malloc.h>
59 1.1 haya #include <sys/kernel.h>
60 1.1 haya #include <sys/socket.h>
61 1.1 haya
62 1.1 haya #include <net/if.h>
63 1.1 haya #include <net/if_arp.h>
64 1.1 haya #include <net/if_ether.h>
65 1.1 haya #include <net/if_dl.h>
66 1.1 haya #include <net/if_media.h>
67 1.1 haya
68 1.1 haya #include <machine/bus.h>
69 1.1 haya
70 1.1 haya #include <dev/pci/pcireg.h>
71 1.1 haya #include <dev/pci/pcivar.h>
72 1.1 haya #include <dev/pci/pcidevs.h>
73 1.1 haya
74 1.1 haya #include <dev/mii/mii.h>
75 1.1 haya #include <dev/mii/miivar.h>
76 1.1 haya
77 1.1 haya /*
78 1.1 haya * Default to using PIO access for this driver. On SMP systems,
79 1.1 haya * there appear to be problems with memory mapped mode: it looks like
80 1.1 haya * doing too many memory mapped access back to back in rapid succession
81 1.1 haya * can hang the bus. I'm inclined to blame this on crummy design/construction
82 1.1 haya * on the part of RealTek. Memory mapped mode does appear to work on
83 1.1 haya * uniprocessor systems though.
84 1.1 haya */
85 1.5 thorpej #ifndef dreamcast /* XXX */
86 1.3 tsutsui #define RTK_USEIOSPACE
87 1.5 thorpej #endif
88 1.1 haya
89 1.1 haya #include <dev/ic/rtl81x9reg.h>
90 1.1 haya #include <dev/ic/rtl81x9var.h>
91 1.1 haya
92 1.2 thorpej struct rtk_pci_softc {
93 1.2 thorpej struct rtk_softc sc_rtk; /* real rtk softc */
94 1.8.2.3 jdolecek
95 1.1 haya /* PCI-specific goo.*/
96 1.1 haya void *sc_ih;
97 1.1 haya pci_chipset_tag_t sc_pc; /* PCI chipset */
98 1.1 haya pcitag_t sc_pcitag; /* PCI tag */
99 1.1 haya };
100 1.1 haya
101 1.2 thorpej static const struct rtk_type rtk_pci_devs[] = {
102 1.1 haya { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
103 1.2 thorpej "RealTek 8129 10/100BaseTX",
104 1.3 tsutsui RTK_8129 },
105 1.1 haya { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
106 1.2 thorpej "RealTek 8139 10/100BaseTX",
107 1.3 tsutsui RTK_8139 },
108 1.1 haya { PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
109 1.2 thorpej "Accton MPX 5030/5038 10/100BaseTX",
110 1.3 tsutsui RTK_8139 },
111 1.1 haya { PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
112 1.2 thorpej "Delta Electronics 8139 10/100BaseTX",
113 1.3 tsutsui RTK_8139 },
114 1.1 haya { PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
115 1.2 thorpej "Addtron Technology 8139 10/100BaseTX",
116 1.3 tsutsui RTK_8139 },
117 1.5 thorpej { PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
118 1.5 thorpej "SEGA Broadband Adapter",
119 1.6 tron RTK_8139 },
120 1.8.2.1 lukem { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
121 1.6 tron "D-Link Systems DFE 530TX+",
122 1.5 thorpej RTK_8139 },
123 1.2 thorpej { 0, 0, NULL, 0 }
124 1.1 haya };
125 1.1 haya
126 1.2 thorpej const struct rtk_type *rtk_pci_lookup __P((const struct pci_attach_args *));
127 1.2 thorpej
128 1.2 thorpej int rtk_pci_match __P((struct device *, struct cfdata *, void *));
129 1.2 thorpej void rtk_pci_attach __P((struct device *, struct device *, void *));
130 1.1 haya
131 1.8.2.4 jdolecek CFATTACH_DECL(rtk_pci, sizeof(struct rtk_pci_softc),
132 1.8.2.4 jdolecek rtk_pci_match, rtk_pci_attach, NULL, NULL);
133 1.1 haya
134 1.2 thorpej const struct rtk_type *
135 1.2 thorpej rtk_pci_lookup(pa)
136 1.1 haya const struct pci_attach_args *pa;
137 1.1 haya {
138 1.2 thorpej const struct rtk_type *t;
139 1.1 haya
140 1.8.2.3 jdolecek for (t = rtk_pci_devs; t->rtk_name != NULL; t++) {
141 1.2 thorpej if (PCI_VENDOR(pa->pa_id) == t->rtk_vid &&
142 1.2 thorpej PCI_PRODUCT(pa->pa_id) == t->rtk_did) {
143 1.1 haya return (t);
144 1.1 haya }
145 1.1 haya }
146 1.1 haya return (NULL);
147 1.1 haya }
148 1.1 haya
149 1.1 haya int
150 1.2 thorpej rtk_pci_match(parent, match, aux)
151 1.1 haya struct device *parent;
152 1.1 haya struct cfdata *match;
153 1.1 haya void *aux;
154 1.1 haya {
155 1.1 haya struct pci_attach_args *pa = aux;
156 1.1 haya
157 1.2 thorpej if (rtk_pci_lookup(pa) != NULL)
158 1.1 haya return (1);
159 1.2 thorpej
160 1.1 haya return (0);
161 1.1 haya }
162 1.1 haya
163 1.1 haya /*
164 1.1 haya * Attach the interface. Allocate softc structures, do ifmedia
165 1.1 haya * setup and ethernet/BPF attach.
166 1.1 haya */
167 1.1 haya void
168 1.2 thorpej rtk_pci_attach(parent, self, aux)
169 1.1 haya struct device *parent, *self;
170 1.1 haya void *aux;
171 1.1 haya {
172 1.2 thorpej struct rtk_pci_softc *psc = (struct rtk_pci_softc *)self;
173 1.2 thorpej struct rtk_softc *sc = &psc->sc_rtk;
174 1.2 thorpej pcireg_t command;
175 1.1 haya struct pci_attach_args *pa = aux;
176 1.1 haya pci_chipset_tag_t pc = pa->pa_pc;
177 1.1 haya pci_intr_handle_t ih;
178 1.1 haya const char *intrstr = NULL;
179 1.2 thorpej const struct rtk_type *t;
180 1.2 thorpej int pmreg;
181 1.1 haya
182 1.1 haya psc->sc_pc = pa->pa_pc;
183 1.1 haya psc->sc_pcitag = pa->pa_tag;
184 1.1 haya
185 1.2 thorpej t = rtk_pci_lookup(pa);
186 1.1 haya if (t == NULL) {
187 1.1 haya printf("\n");
188 1.2 thorpej panic("rtk_pci_attach: impossible");
189 1.1 haya }
190 1.2 thorpej printf(": %s\n", t->rtk_name);
191 1.1 haya
192 1.1 haya /*
193 1.1 haya * Handle power management nonsense.
194 1.1 haya */
195 1.1 haya
196 1.1 haya if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PWRMGMT, &pmreg, 0)) {
197 1.1 haya command = pci_conf_read(pc, pa->pa_tag, pmreg + 4);
198 1.3 tsutsui if (command & RTK_PSTATE_MASK) {
199 1.2 thorpej pcireg_t iobase, membase, irq;
200 1.1 haya
201 1.1 haya /* Save important PCI config data. */
202 1.3 tsutsui iobase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOIO);
203 1.3 tsutsui membase = pci_conf_read(pc, pa->pa_tag, RTK_PCI_LOMEM);
204 1.1 haya irq = pci_conf_read(pc, pa->pa_tag,
205 1.8.2.3 jdolecek PCI_PRODUCT_DELTA_8139);
206 1.1 haya
207 1.1 haya /* Reset the power state. */
208 1.1 haya printf("%s: chip is is in D%d power mode "
209 1.8.2.3 jdolecek "-- setting to D0\n", sc->sc_dev.dv_xname,
210 1.8.2.3 jdolecek command & RTK_PSTATE_MASK);
211 1.1 haya command &= 0xFFFFFFFC;
212 1.1 haya pci_conf_write(pc, pa->pa_tag, pmreg + 4, command);
213 1.1 haya
214 1.1 haya /* Restore PCI config data. */
215 1.3 tsutsui pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOIO, iobase);
216 1.3 tsutsui pci_conf_write(pc, pa->pa_tag, RTK_PCI_LOMEM, membase);
217 1.1 haya pci_conf_write(pc, pa->pa_tag,
218 1.8.2.3 jdolecek PCI_PRODUCT_DELTA_8139, irq);
219 1.1 haya }
220 1.1 haya }
221 1.1 haya
222 1.1 haya /*
223 1.1 haya * Map control/status registers.
224 1.1 haya */
225 1.3 tsutsui #ifdef RTK_USEIOSPACE
226 1.3 tsutsui if (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
227 1.2 thorpej &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
228 1.1 haya printf("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
229 1.2 thorpej return;
230 1.1 haya }
231 1.1 haya #else
232 1.3 tsutsui if (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
233 1.2 thorpej &sc->rtk_btag, &sc->rtk_bhandle, NULL, NULL)) {
234 1.5 thorpej printf("%s: can't map mem space\n", sc->sc_dev.dv_xname);
235 1.2 thorpej return;
236 1.1 haya }
237 1.1 haya #endif
238 1.1 haya
239 1.1 haya /* Allocate interrupt */
240 1.4 sommerfe if (pci_intr_map(pa, &ih)) {
241 1.1 haya printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
242 1.2 thorpej return;
243 1.1 haya }
244 1.1 haya intrstr = pci_intr_string(pc, ih);
245 1.2 thorpej psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
246 1.1 haya if (psc->sc_ih == NULL) {
247 1.1 haya printf("%s: couldn't establish interrupt",
248 1.1 haya sc->sc_dev.dv_xname);
249 1.1 haya if (intrstr != NULL)
250 1.1 haya printf(" at %s", intrstr);
251 1.1 haya printf("\n");
252 1.2 thorpej return;
253 1.1 haya }
254 1.1 haya
255 1.2 thorpej sc->rtk_type = t->rtk_type;
256 1.2 thorpej
257 1.1 haya printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
258 1.1 haya
259 1.1 haya sc->sc_dmat = pa->pa_dmat;
260 1.8.2.3 jdolecek
261 1.2 thorpej rtk_attach(sc);
262 1.1 haya }
263