xref: /netbsd-src/sys/dev/pci/if_epic_pci.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_epic_pci.c,v 1.33 2007/10/19 12:00:45 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * PCI bus front-end for the Standard Microsystems Corp. 83C170
42  * Ethernet PCI Integrated Controller (EPIC/100) driver.
43  */
44 
45 #include <sys/cdefs.h>
46 __KERNEL_RCSID(0, "$NetBSD: if_epic_pci.c,v 1.33 2007/10/19 12:00:45 ad Exp $");
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/errno.h>
56 #include <sys/device.h>
57 
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_ether.h>
62 
63 #include <sys/bus.h>
64 #include <sys/intr.h>
65 
66 #include <dev/mii/miivar.h>
67 
68 #include <dev/ic/smc83c170reg.h>
69 #include <dev/ic/smc83c170var.h>
70 
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/pci/pcidevs.h>
74 
75 /*
76  * PCI configuration space registers used by the EPIC.
77  */
78 #define	EPIC_PCI_IOBA		0x10	/* i/o mapped base */
79 #define	EPIC_PCI_MMBA		0x14	/* memory mapped base */
80 
81 struct epic_pci_softc {
82 	struct epic_softc sc_epic;	/* real EPIC softc */
83 
84 	/* PCI-specific goo. */
85 	void	*sc_ih;			/* interrupt handle */
86 };
87 
88 static int	epic_pci_match(struct device *, struct cfdata *, void *);
89 static void	epic_pci_attach(struct device *, struct device *, void *);
90 
91 CFATTACH_DECL(epic_pci, sizeof(struct epic_pci_softc),
92     epic_pci_match, epic_pci_attach, NULL, NULL);
93 
94 static const struct epic_pci_product {
95 	u_int32_t	epp_prodid;	/* PCI product ID */
96 	const char	*epp_name;	/* device name */
97 } epic_pci_products[] = {
98 	{ PCI_PRODUCT_SMC_83C170,	"SMC 83c170 Fast Ethernet" },
99 	{ PCI_PRODUCT_SMC_83C175,	"SMC 83c175 Fast Ethernet" },
100 	{ 0,				NULL },
101 };
102 
103 static const struct epic_pci_product *
104 epic_pci_lookup(const struct pci_attach_args *pa)
105 {
106 	const struct epic_pci_product *epp;
107 
108 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_SMC)
109 		return (NULL);
110 
111 	for (epp = epic_pci_products; epp->epp_name != NULL; epp++)
112 		if (PCI_PRODUCT(pa->pa_id) == epp->epp_prodid)
113 			return (epp);
114 
115 	return (NULL);
116 }
117 
118 static const struct epic_pci_subsys_info {
119 	pcireg_t subsysid;
120 	int flags;
121 } epic_pci_subsys_info[] = {
122 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa015), /* SMC9432BTX */
123 	  EPIC_HAS_BNC },
124 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa024), /* SMC9432BTX1 */
125 	  EPIC_HAS_BNC },
126 	{ PCI_ID_CODE(PCI_VENDOR_SMC, 0xa016), /* SMC9432FTX */
127 	  EPIC_HAS_MII_FIBER | EPIC_DUPLEXLED_ON_694 },
128 	{ 0xffffffff,
129 	  0 }
130 };
131 
132 static const struct epic_pci_subsys_info *
133 epic_pci_subsys_lookup(const struct pci_attach_args *pa)
134 {
135 	pci_chipset_tag_t pc = pa->pa_pc;
136 	pcireg_t reg;
137 	const struct epic_pci_subsys_info *esp;
138 
139 	reg = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
140 
141 	for (esp = epic_pci_subsys_info; esp->subsysid != 0xffffffff; esp++)
142 		if (esp->subsysid == reg)
143 			return (esp);
144 
145 	return (NULL);
146 }
147 
148 static int
149 epic_pci_match(struct device *parent, struct cfdata *match,
150     void *aux)
151 {
152 	struct pci_attach_args *pa = aux;
153 
154 	if (epic_pci_lookup(pa) != NULL)
155 		return (1);
156 
157 	return (0);
158 }
159 
160 static void
161 epic_pci_attach(struct device *parent, struct device *self, void *aux)
162 {
163 	struct epic_pci_softc *psc = (struct epic_pci_softc *)self;
164 	struct epic_softc *sc = &psc->sc_epic;
165 	struct pci_attach_args *pa = aux;
166 	pci_chipset_tag_t pc = pa->pa_pc;
167 	pci_intr_handle_t ih;
168 	const char *intrstr = NULL;
169 	const struct epic_pci_product *epp;
170 	const struct epic_pci_subsys_info *esp;
171 	bus_space_tag_t iot, memt;
172 	bus_space_handle_t ioh, memh;
173 	int ioh_valid, memh_valid;
174 	int error;
175 
176 	aprint_naive(": Ethernet controller\n");
177 
178 	epp = epic_pci_lookup(pa);
179 	if (epp == NULL) {
180 		printf("\n");
181 		panic("epic_pci_attach: impossible");
182 	}
183 
184 	aprint_normal(": %s, rev. %d\n", epp->epp_name,
185 	    PCI_REVISION(pa->pa_class));
186 
187 	/* power up chip */
188 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
189 	    NULL)) && error != EOPNOTSUPP) {
190 		aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
191 		    error);
192 		return;
193 	}
194 
195 	/*
196 	 * Map the device.
197 	 */
198 	ioh_valid = (pci_mapreg_map(pa, EPIC_PCI_IOBA,
199 	    PCI_MAPREG_TYPE_IO, 0,
200 	    &iot, &ioh, NULL, NULL) == 0);
201 	memh_valid = (pci_mapreg_map(pa, EPIC_PCI_MMBA,
202 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
203 	    &memt, &memh, NULL, NULL) == 0);
204 
205 	if (memh_valid) {
206 		sc->sc_st = memt;
207 		sc->sc_sh = memh;
208 	} else if (ioh_valid) {
209 		sc->sc_st = iot;
210 		sc->sc_sh = ioh;
211 	} else {
212 		aprint_error("%s: unable to map device registers\n",
213 		    sc->sc_dev.dv_xname);
214 		return;
215 	}
216 
217 	sc->sc_dmat = pa->pa_dmat;
218 
219 	/* Make sure bus mastering is enabled. */
220 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
221 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
222 	    PCI_COMMAND_MASTER_ENABLE);
223 
224 	/*
225 	 * Map and establish our interrupt.
226 	 */
227 	if (pci_intr_map(pa, &ih)) {
228 		aprint_error("%s: unable to map interrupt\n",
229 		    sc->sc_dev.dv_xname);
230 		return;
231 	}
232 	intrstr = pci_intr_string(pc, ih);
233 	psc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, epic_intr, sc);
234 	if (psc->sc_ih == NULL) {
235 		aprint_error("%s: unable to establish interrupt",
236 		    sc->sc_dev.dv_xname);
237 		if (intrstr != NULL)
238 			aprint_normal(" at %s", intrstr);
239 		aprint_normal("\n");
240 		return;
241 	}
242 	aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
243 
244 	esp = epic_pci_subsys_lookup(pa);
245 	if (esp)
246 		sc->sc_hwflags = esp->flags;
247 
248 	/*
249 	 * Finish off the attach.
250 	 */
251 	epic_attach(sc);
252 }
253