if_re_pci.c revision 1.8
164562Sgshapiro/*	$OpenBSD: if_re_pci.c,v 1.8 2006/05/23 00:41:50 brad Exp $	*/
2261363Sgshapiro
364562Sgshapiro/*
464562Sgshapiro * Copyright (c) 2005 Peter Valchev <pvalchev@openbsd.org>
564562Sgshapiro *
664562Sgshapiro * Permission to use, copy, modify, and distribute this software for any
764562Sgshapiro * purpose with or without fee is hereby granted, provided that the above
864562Sgshapiro * copyright notice and this permission notice appear in all copies.
964562Sgshapiro *
1064562Sgshapiro * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1164562Sgshapiro * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1264562Sgshapiro * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13266692Sgshapiro * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1464562Sgshapiro * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1590792Sgshapiro * 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/socket.h>
32
33#include <net/if.h>
34#include <net/if_dl.h>
35#include <net/if_media.h>
36
37#ifdef INET
38#include <netinet/in.h>
39#include <netinet/in_systm.h>
40#include <netinet/in_var.h>
41#include <netinet/ip.h>
42#include <netinet/if_ether.h>
43#endif
44
45#include <dev/mii/mii.h>
46#include <dev/mii/miivar.h>
47
48#include <dev/pci/pcireg.h>
49#include <dev/pci/pcivar.h>
50#include <dev/pci/pcidevs.h>
51
52#include <dev/ic/rtl81x9reg.h>
53#include <dev/ic/revar.h>
54
55struct re_pci_softc {
56	/* General */
57	struct rl_softc sc_rl;
58
59	/* PCI-specific data */
60	void *sc_ih;
61	pci_chipset_tag_t sc_pc;
62	pcitag_t sc_pcitag;
63};
64
65const struct pci_matchid re_pci_devices[] = {
66	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8111B },
67	{ PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169 },
68	{ PCI_VENDOR_COREGA, PCI_PRODUCT_COREGA_CGLAPCIGT },
69	{ PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DGE528T },
70	{ PCI_VENDOR_USR2, PCI_PRODUCT_USR2_USR997902 },
71	{ PCI_VENDOR_TTTECH, PCI_PRODUCT_TTTECH_MC322 }
72};
73
74#define RE_LINKSYS_EG1032_SUBID 0x00241737
75
76int re_pci_probe(struct device *, void *, void *);
77void re_pci_attach(struct device *, struct device *, void *);
78
79/*
80 * PCI autoconfig definitions
81 */
82struct cfattach re_pci_ca = {
83	sizeof(struct re_pci_softc),
84	re_pci_probe,
85	re_pci_attach
86};
87
88/*
89 * Probe for a Realtek 8169/8110 chip. Check the PCI vendor and device
90 * IDs against our list and return a device name if we find a match.
91 */
92int
93re_pci_probe(struct device *parent, void *match, void *aux)
94{
95	struct pci_attach_args *pa = aux;
96	pci_chipset_tag_t pc = pa->pa_pc;
97	pcireg_t subid;
98
99	subid = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
100
101	/* C+ mode 8139's */
102	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK &&
103	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139 &&
104	    PCI_REVISION(pa->pa_class) == 0x20)
105		return (1);
106
107	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_LINKSYS &&
108	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_LINKSYS_EG1032 &&
109	    subid == RE_LINKSYS_EG1032_SUBID)
110		return (1);
111
112	return (pci_matchbyid((struct pci_attach_args *)aux, re_pci_devices,
113	    sizeof(re_pci_devices)/sizeof(re_pci_devices[0])));
114}
115
116/*
117 * PCI-specific attach routine
118 */
119void
120re_pci_attach(struct device *parent, struct device *self, void *aux)
121{
122	struct re_pci_softc	*psc = (struct re_pci_softc *)self;
123	struct rl_softc		*sc = &psc->sc_rl;
124	struct pci_attach_args	*pa = aux;
125	pci_chipset_tag_t	pc = pa->pa_pc;
126	pci_intr_handle_t	ih;
127	const char		*intrstr = NULL;
128	bus_size_t		iosize;
129	pcireg_t		command;
130
131	/*
132	 * Handle power management nonsense.
133	 */
134
135	command = pci_conf_read(pc, pa->pa_tag, RL_PCI_CAPID) & 0x000000FF;
136
137	if (command == 0x01) {
138		u_int32_t		iobase, membase, irq;
139
140		/* Save important PCI config data. */
141		iobase = pci_conf_read(pc, pa->pa_tag,  RL_PCI_LOIO);
142		membase = pci_conf_read(pc, pa->pa_tag, RL_PCI_LOMEM);
143		irq = pci_conf_read(pc, pa->pa_tag, RL_PCI_INTLINE);
144
145		/* Reset the power state. */
146		printf("%s: chip is is in D%d power mode "
147		    "-- setting to D0\n", sc->sc_dev.dv_xname,
148		    command & RL_PSTATE_MASK);
149		command &= 0xFFFFFFFC;
150
151		/* Restore PCI config data. */
152		pci_conf_write(pc, pa->pa_tag, RL_PCI_LOIO, iobase);
153		pci_conf_write(pc, pa->pa_tag, RL_PCI_LOMEM, membase);
154		pci_conf_write(pc, pa->pa_tag, RL_PCI_INTLINE, irq);
155	}
156
157	/*
158	 * Map control/status registers.
159	 */
160	command = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
161
162	if ((command & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) == 0) {
163		printf(": neither i/o nor mem enabled\n");
164		return;
165	}
166
167	if (command & PCI_COMMAND_MEM_ENABLE) {
168		if (pci_mapreg_map(pa, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
169		    &sc->rl_btag, &sc->rl_bhandle, NULL, &iosize, 0)) {
170			printf(": can't map mem space\n");
171			return;
172		}
173	} else {
174		if (pci_mapreg_map(pa, RL_PCI_LOIO, PCI_MAPREG_TYPE_IO, 0,
175		    &sc->rl_btag, &sc->rl_bhandle, NULL, &iosize, 0)) {
176			printf(": can't map i/o space\n");
177			return;
178		}
179	}
180
181	/* Allocate interrupt */
182	if (pci_intr_map(pa, &ih)) {
183		printf(": couldn't map interrupt\n");
184		return;
185	}
186	intrstr = pci_intr_string(pc, ih);
187	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, re_intr, sc,
188	    sc->sc_dev.dv_xname);
189	if (psc->sc_ih == NULL) {
190		printf(": couldn't establish interrupt");
191		if (intrstr != NULL)
192			printf(" at %s", intrstr);
193		return;
194	}
195	printf(": %s", intrstr);
196
197	sc->sc_dmat = pa->pa_dmat;
198	sc->sc_flags |= RL_ENABLED;
199
200	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_REALTEK) {
201		if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_REALTEK_RT8139)
202			sc->rl_type = RL_8139;
203		else
204			sc->rl_type = RL_8169;
205	} else if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TTTECH &&
206		   PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TTTECH_MC322)
207		sc->rl_type = RL_8139;
208	else
209		sc->rl_type = RL_8169;
210
211	/* Call bus-independent attach routine */
212	re_attach_common(sc);
213}
214