if_re_pci.c revision 1.47
1/*	$OpenBSD: if_re_pci.c,v 1.47 2015/03/08 01:54:04 tobiasu Exp $	*/
2
3/*
4 * Copyright (c) 2005 Peter Valchev <pvalchev@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19/*
20 * PCI front-end for the Realtek 8169
21 */
22
23#include <sys/param.h>
24#include <sys/endian.h>
25#include <sys/systm.h>
26#include <sys/sockio.h>
27#include <sys/mbuf.h>
28#include <sys/malloc.h>
29#include <sys/kernel.h>
30#include <sys/device.h>
31#include <sys/timeout.h>
32#include <sys/socket.h>
33
34#include <net/if.h>
35#include <net/if_dl.h>
36#include <net/if_media.h>
37
38#include <netinet/in.h>
39#include <netinet/if_ether.h>
40
41#include <dev/mii/mii.h>
42#include <dev/mii/miivar.h>
43
44#include <dev/pci/pcireg.h>
45#include <dev/pci/pcivar.h>
46#include <dev/pci/pcidevs.h>
47
48#include <dev/ic/rtl81x9reg.h>
49#include <dev/ic/revar.h>
50
51struct re_pci_softc {
52	/* General */
53	struct rl_softc sc_rl;
54
55	/* PCI-specific data */
56	void *sc_ih;
57	pci_chipset_tag_t sc_pc;
58	pcitag_t sc_pcitag;
59
60	bus_size_t sc_iosize;
61};
62
63const struct pci_matchid re_pci_devices[] = {
64	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CGLAPCIGT },
65	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T },
66	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE530T_C1 },
67	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8101E },
68	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8168 },
69	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169 },
70	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169SC },
71	{ PCI_VENDOR_TTTECH, PCI_PRODUCT_TTTECH_MC322 },
72	{ PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902 }
73};
74
75#define RE_LINKSYS_EG1032_SUBID 0x00241737
76
77int	re_pci_probe(struct device *, void *, void *);
78void	re_pci_attach(struct device *, struct device *, void *);
79int	re_pci_detach(struct device *, int);
80int	re_pci_activate(struct device *, int);
81
82/*
83 * PCI autoconfig definitions
84 */
85struct cfattach re_pci_ca = {
86	sizeof(struct re_pci_softc),
87	re_pci_probe,
88	re_pci_attach,
89	re_pci_detach,
90	re_pci_activate
91};
92
93/*
94 * Probe for a Realtek 8169/8110 chip. Check the PCI vendor and device
95 * IDs against our list and return a device name if we find a match.
96 */
97int
98re_pci_probe(struct device *parent, void *match, void *aux)
99{
100	struct pci_attach_args *pa = aux;
101	pci_chipset_tag_t pc = pa->pa_pc;
102	pcireg_t subid;
103
104	subid = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
105
106	/* C+ mode 8139's */
107	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
108	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139 &&
109	    PCI_REVISION(pa->pa_class) == 0x20)
110		return (1);
111
112	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
113	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032 &&
114	    subid == RE_LINKSYS_EG1032_SUBID)
115		return (1);
116
117	return (pci_matchbyid((struct pci_attach_args *)aux, re_pci_devices,
118	    nitems(re_pci_devices)));
119}
120
121/*
122 * PCI-specific attach routine
123 */
124void
125re_pci_attach(struct device *parent, struct device *self, void *aux)
126{
127	struct re_pci_softc	*psc = (struct re_pci_softc *)self;
128	struct rl_softc		*sc = &psc->sc_rl;
129	struct pci_attach_args	*pa = aux;
130	pci_chipset_tag_t	pc = pa->pa_pc;
131	pci_intr_handle_t	ih;
132	const char		*intrstr = NULL;
133
134	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
135
136#ifndef SMALL_KERNEL
137	/* Enable power management for wake on lan. */
138	pci_conf_write(pc, pa->pa_tag, RL_PCI_PMCSR, RL_PME_EN);
139#endif
140
141	/*
142	 * Map control/status registers.
143	 */
144	if (pci_mapreg_map(pa, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
145	    &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->sc_iosize, 0)) {
146		if (pci_mapreg_map(pa, RL_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
147		    &sc->rl_btag, &sc->rl_bhandle, NULL, &psc->sc_iosize, 0)) {
148			printf(": can't map mem or i/o space\n");
149			return;
150		}
151	}
152
153	/* Allocate interrupt */
154	if (pci_intr_map_msi(pa, &ih) == 0)
155		sc->rl_flags |= RL_FLAG_MSI;
156	else if (pci_intr_map(pa, &ih) != 0) {
157		printf(": couldn't map interrupt\n");
158		return;
159	}
160	intrstr = pci_intr_string(pc, ih);
161	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc,
162	    sc->sc_dev.dv_xname);
163	if (psc->sc_ih == NULL) {
164		printf(": couldn't establish interrupt");
165		if (intrstr != NULL)
166			printf(" at %s", intrstr);
167		return;
168	}
169
170	sc->sc_dmat = pa->pa_dmat;
171	psc->sc_pc = pc;
172
173	/*
174	 * PCI Express check.
175	 */
176	if (pci_get_capability(pc, pa->pa_tag, PCI_CAP_PCIEXPRESS,
177	    NULL, NULL))
178		sc->rl_flags |= RL_FLAG_PCIE;
179
180	if (!(PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
181	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139)) {
182		u_int8_t	cfg;
183
184		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
185		cfg = CSR_READ_1(sc, RL_CFG2);
186		if (sc->rl_flags & RL_FLAG_MSI) {
187			cfg |= RL_CFG2_MSI;
188			CSR_WRITE_1(sc, RL_CFG2, cfg);
189		} else {
190			if ((cfg & RL_CFG2_MSI) != 0) {
191				cfg &= ~RL_CFG2_MSI;
192				CSR_WRITE_1(sc, RL_CFG2, cfg);
193			}
194		}
195		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
196	}
197
198	sc->sc_product = PCI_PRODUCT(pa->pa_id);
199
200	/* Call bus-independent attach routine */
201	if (re_attach(sc, intrstr)) {
202		pci_intr_disestablish(pc, psc->sc_ih);
203		bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->sc_iosize);
204	}
205}
206
207int
208re_pci_detach(struct device *self, int flags)
209{
210	struct re_pci_softc	*psc = (struct re_pci_softc *)self;
211	struct rl_softc		*sc = &psc->sc_rl;
212	struct ifnet		*ifp = &sc->sc_arpcom.ac_if;
213
214	/* Remove timeout handler */
215	timeout_del(&sc->timer_handle);
216
217	/* Detach PHY */
218	if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL)
219		mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
220
221	/* Delete media stuff */
222	ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
223	ether_ifdetach(ifp);
224	if_detach(ifp);
225
226	/* Disable interrupts */
227	if (psc->sc_ih != NULL)
228		pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
229
230	/* Free pci resources */
231	bus_space_unmap(sc->rl_btag, sc->rl_bhandle, psc->sc_iosize);
232
233	return (0);
234}
235
236int
237re_pci_activate(struct device *self, int act)
238{
239	struct re_pci_softc	*psc = (struct re_pci_softc *)self;
240	struct rl_softc		*sc = &psc->sc_rl;
241	struct ifnet 		*ifp = &sc->sc_arpcom.ac_if;
242
243	switch (act) {
244	case DVACT_SUSPEND:
245		if (ifp->if_flags & IFF_RUNNING)
246			re_stop(ifp);
247		break;
248	case DVACT_RESUME:
249		if (ifp->if_flags & IFF_UP)
250			re_init(ifp);
251		break;
252	}
253
254	return (0);
255}
256