xref: /netbsd-src/sys/dev/pci/if_rtk_pci.c (revision 2c6fc41c810f5088457889d00eba558e8bc74d9e)
1 /*	$NetBSD: if_rtk_pci.c,v 1.45 2014/03/29 19:28:25 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1997, 1998
5  *	Bill Paul <wpaul@ctr.columbia.edu>.  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  *	FreeBSD Id: if_rl.c,v 1.17 1999/06/19 20:17:37 wpaul Exp
35  */
36 
37 /*
38  * Realtek 8129/8139 PCI NIC driver
39  *
40  * Supports several extremely cheap PCI 10/100 adapters based on
41  * the Realtek chipset. Datasheets can be obtained from
42  * www.realtek.com.tw.
43  *
44  * Written by Bill Paul <wpaul@ctr.columbia.edu>
45  * Electrical Engineering Department
46  * Columbia University, New York City
47  */
48 
49 #include <sys/cdefs.h>
50 __KERNEL_RCSID(0, "$NetBSD: if_rtk_pci.c,v 1.45 2014/03/29 19:28:25 christos Exp $");
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/callout.h>
55 #include <sys/device.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 
62 #include <net/if.h>
63 #include <net/if_arp.h>
64 #include <net/if_ether.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 
68 #include <sys/bus.h>
69 
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcidevs.h>
73 
74 #include <dev/mii/mii.h>
75 #include <dev/mii/miivar.h>
76 
77 #include <dev/ic/rtl81x9reg.h>
78 #include <dev/ic/rtl81x9var.h>
79 
80 struct rtk_pci_softc {
81 	struct rtk_softc sc_rtk;	/* real rtk softc */
82 
83 	/* PCI-specific goo.*/
84 	void *sc_ih;
85 	pci_chipset_tag_t sc_pc;
86 };
87 
88 static const struct rtk_type rtk_pci_devs[] = {
89 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8129,
90 		RTK_8129, "Realtek 8129 10/100BaseTX" },
91 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139,
92 		RTK_8139, "Realtek 8139 10/100BaseTX" },
93 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8138,
94 		RTK_8139, "Realtek 8138 10/100BaseTX" },
95 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8139D,
96 		RTK_8139, "Realtek 8139D 10/100BaseTX" },
97 	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8100,
98 		RTK_8139, "Realtek 8100 10/100BaseTX" },
99 	{ PCI_VENDOR_ACCTON, PCI_PRODUCT_ACCTON_MPX5030,
100 		RTK_8139, "Accton MPX 5030/5038 10/100BaseTX" },
101 	{ PCI_VENDOR_DELTA, PCI_PRODUCT_DELTA_8139,
102 		RTK_8139, "Delta Electronics 8139 10/100BaseTX" },
103 	{ PCI_VENDOR_ADDTRON, PCI_PRODUCT_ADDTRON_8139,
104 		RTK_8139, "Addtron Technology 8139 10/100BaseTX" },
105 	{ PCI_VENDOR_SEGA, PCI_PRODUCT_SEGA_BROADBAND,
106 		RTK_8139, "SEGA Broadband Adapter" },
107 	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DFE530TXPLUS,
108 		RTK_8139, "D-Link Systems DFE 530TX+" },
109 	{ PCI_VENDOR_NORTEL, PCI_PRODUCT_NORTEL_BAYSTACK_21,
110 		RTK_8139, "Baystack 21 (MPX EN5038) 10/100BaseTX" },
111 };
112 
113 static int	rtk_pci_match(device_t, cfdata_t, void *);
114 static void	rtk_pci_attach(device_t, device_t, void *);
115 static int	rtk_pci_detach(device_t, int);
116 
117 CFATTACH_DECL_NEW(rtk_pci, sizeof(struct rtk_pci_softc),
118     rtk_pci_match, rtk_pci_attach, rtk_pci_detach, NULL);
119 
120 static const struct rtk_type *
121 rtk_pci_lookup(const struct pci_attach_args *pa)
122 {
123 	int i;
124 
125 	for (i = 0; i < __arraycount(rtk_pci_devs); i++) {
126 		if (PCI_VENDOR(pa->pa_id) != rtk_pci_devs[i].rtk_vid)
127 			continue;
128 		if (PCI_PRODUCT(pa->pa_id) == rtk_pci_devs[i].rtk_did)
129 			return &rtk_pci_devs[i];
130 	}
131 
132 	return NULL;
133 }
134 
135 static int
136 rtk_pci_match(device_t parent, cfdata_t cf, void *aux)
137 {
138 	struct pci_attach_args *pa = aux;
139 
140 	if (rtk_pci_lookup(pa) != NULL)
141 		return 1;
142 
143 	return 0;
144 }
145 
146 /*
147  * Attach the interface. Allocate softc structures, do ifmedia
148  * setup and ethernet/BPF attach.
149  */
150 static void
151 rtk_pci_attach(device_t parent, device_t self, void *aux)
152 {
153 	struct rtk_pci_softc *psc = device_private(self);
154 	struct rtk_softc *sc = &psc->sc_rtk;
155 	struct pci_attach_args *pa = aux;
156 	pci_chipset_tag_t pc = pa->pa_pc;
157 	pci_intr_handle_t ih;
158 	bus_space_tag_t iot, memt;
159 	bus_space_handle_t ioh, memh;
160 	bus_size_t iosize, memsize;
161 	pcireg_t csr;
162 	const char *intrstr = NULL;
163 	const struct rtk_type *t;
164 	bool ioh_valid, memh_valid;
165 	char intrbuf[PCI_INTRSTR_LEN];
166 
167 	sc->sc_dev = self;
168 	psc->sc_pc = pa->pa_pc;
169 
170 	t = rtk_pci_lookup(pa);
171 	KASSERT(t != NULL);
172 
173 	pci_aprint_devinfo_fancy(pa, NULL, t->rtk_name, 1);
174 
175 	/*
176 	 * Map control/status registers.
177 	 *
178 	 * The original FreeBSD's driver has the following comment:
179 	 *
180 	 *   Default to using PIO access for this driver. On SMP systems,
181 	 *   there appear to be problems with memory mapped mode:
182 	 *   it looks like doing too many memory mapped access
183 	 *   back to back in rapid succession can hang the bus.
184 	 *   I'm inclined to blame this on crummy design/construction
185 	 *   on the part of Realtek. Memory mapped mode does appear to
186 	 *   work on uniprocessor systems though.
187 	 *
188 	 * On NetBSD, some ports don't support PCI I/O space properly,
189 	 * so we try to map both and prefer I/O space to mem space.
190 	 */
191 	ioh_valid = (pci_mapreg_map(pa, RTK_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
192 	    &iot, &ioh, NULL, &iosize) == 0);
193 	memh_valid = (pci_mapreg_map(pa, RTK_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
194 	    &memt, &memh, NULL, &memsize) == 0);
195 	if (ioh_valid) {
196 		sc->rtk_btag = iot;
197 		sc->rtk_bhandle = ioh;
198 		sc->rtk_bsize = iosize;
199 		if (memh_valid)
200 			bus_space_unmap(memt, memh, memsize);
201 	} else if (memh_valid) {
202 		sc->rtk_btag = memt;
203 		sc->rtk_bhandle = memh;
204 		sc->rtk_bsize = memsize;
205 	} else {
206 		aprint_error_dev(self, "can't map registers\n");
207 		return;
208 	}
209 
210 	/* Allocate interrupt */
211 	if (pci_intr_map(pa, &ih)) {
212 		aprint_error_dev(self, "couldn't map interrupt\n");
213 		return;
214 	}
215 	intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
216 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, rtk_intr, sc);
217 	if (psc->sc_ih == NULL) {
218 		aprint_error_dev(self, "couldn't establish interrupt");
219 		if (intrstr != NULL)
220 			aprint_error(" at %s", intrstr);
221 		aprint_error("\n");
222 		return;
223 	}
224 
225 	if (t->rtk_basetype == RTK_8129)
226 		sc->sc_quirk |= RTKQ_8129;
227 
228 	aprint_normal_dev(self, "interrupting at %s\n", intrstr);
229 
230 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
231 	csr |= PCI_COMMAND_MASTER_ENABLE;
232 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, csr);
233 
234 	sc->sc_dmat = pa->pa_dmat;
235 	sc->sc_flags |= RTK_ENABLED;
236 
237 	if (pmf_device_register(self, NULL, NULL))
238 		pmf_class_network_register(self, &sc->ethercom.ec_if);
239 	else
240 		aprint_error_dev(self, "couldn't establish power handler\n");
241 
242 	rtk_attach(sc);
243 }
244 
245 static int
246 rtk_pci_detach(device_t self, int flags)
247 {
248 	struct rtk_pci_softc *psc = device_private(self);
249 	struct rtk_softc *sc = &psc->sc_rtk;
250 	int rv;
251 
252 	/* rtk_stop() */
253 	sc->ethercom.ec_if.if_stop(&sc->ethercom.ec_if, 0);
254 
255 	rv = rtk_detach(sc);
256 	if (rv)
257 		return rv;
258 
259 	if (psc->sc_ih != NULL) {
260 		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
261 		psc->sc_ih = NULL;
262 	}
263 
264 	if (sc->rtk_bsize != 0) {
265 		bus_space_unmap(sc->rtk_btag, sc->rtk_bhandle, sc->rtk_bsize);
266 		sc->rtk_bsize = 0;
267 	}
268 
269 	return 0;
270 }
271