xref: /netbsd-src/sys/dev/pci/if_sf_pci.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: if_sf_pci.c,v 1.13 2007/10/19 12:00:47 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 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.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * PCI bus front-end for the Adaptec AIC-6915 (``Starfire'')
41  * 10/100 Ethernet controller.
42  */
43 
44 #include <sys/cdefs.h>
45 __KERNEL_RCSID(0, "$NetBSD: if_sf_pci.c,v 1.13 2007/10/19 12:00:47 ad Exp $");
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/socket.h>
53 #include <sys/ioctl.h>
54 #include <sys/errno.h>
55 #include <sys/device.h>
56 
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_ether.h>
61 
62 #include <sys/bus.h>
63 #include <sys/intr.h>
64 
65 #include <dev/mii/miivar.h>
66 
67 #include <dev/ic/aic6915reg.h>
68 #include <dev/ic/aic6915var.h>
69 
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcidevs.h>
73 
74 struct sf_pci_softc {
75 	struct sf_softc sc_starfire;	/* read Starfire softc */
76 
77 	/* PCI-specific goo. */
78 	void	*sc_ih;			/* interrupt handle */
79 };
80 
81 static int	sf_pci_match(struct device *, struct cfdata *, void *);
82 static void	sf_pci_attach(struct device *, struct device *, void *);
83 
84 CFATTACH_DECL(sf_pci, sizeof(struct sf_pci_softc),
85     sf_pci_match, sf_pci_attach, NULL, NULL);
86 
87 struct sf_pci_product {
88 	uint32_t	spp_vendor;	/* PCI vendor ID */
89 	uint32_t	spp_product;	/* PCI product ID */
90 	const char	*spp_name;	/* product name */
91 	const struct sf_pci_product *spp_subsys; /* subsystm IDs */
92 };
93 
94 static const struct sf_pci_product sf_subsys_adaptec[] = {
95 	/* ANA-62011 (rev 0) Single port 10/100 64-bit */
96 	{ PCI_VENDOR_ADP,			0x0008,
97 	  "ANA-62011 (rev 0) 10/100 Ethernet",	NULL },
98 
99 	/* ANA-62011 (rev 1) Single port 10/100 64-bit */
100 	{ PCI_VENDOR_ADP,			0x0009,
101 	  "ANA-62011 (rev 1) 10/100 Ethernet",	NULL },
102 
103 	/* ANA-62022 Dual port 10/100 64-bit */
104 	{ PCI_VENDOR_ADP,			0x0010,
105 	  "ANA-62022 10/100 Ethernet",		NULL },
106 
107 	/* ANA-62044 (rev 0) Quad port 10/100 64-bit */
108 	{ PCI_VENDOR_ADP,			0x0018,
109 	  "ANA-62044 (rev 0) 10/100 Ethernet",	NULL },
110 
111 	/* ANA-62044 (rev 1) Quad port 10/100 64-bit */
112 	{ PCI_VENDOR_ADP,			0x0019,
113 	  "ANA-62044 (rev 1) 10/100 Ethernet",	NULL },
114 
115 	/* ANA-62020 Single port 100baseFX 64-bit */
116 	{ PCI_VENDOR_ADP,			0x0020,
117 	  "ANA-62020 100baseFX Ethernet",	NULL },
118 
119 	/* ANA-69011 Single port 10/100 32-bit */
120 	{ PCI_VENDOR_ADP,			0x0028,
121 	  "ANA-69011 10/100 Ethernet",		NULL },
122 
123 	{ 0, 					0,
124 	  NULL,					NULL },
125 };
126 
127 static const struct sf_pci_product sf_pci_products[] = {
128 	{ PCI_VENDOR_ADP,			PCI_PRODUCT_ADP_AIC6915,
129 	  "AIC-6915 10/100 Ethernet",		sf_subsys_adaptec },
130 
131 	{ 0,					0,
132 	  NULL,					NULL },
133 };
134 
135 static const struct sf_pci_product *
136 sf_pci_lookup(const struct pci_attach_args *pa)
137 {
138 	const struct sf_pci_product *spp, *subspp;
139 	pcireg_t subsysid;
140 
141 	for (spp = sf_pci_products; spp->spp_name != NULL; spp++) {
142 		if (PCI_VENDOR(pa->pa_id) == spp->spp_vendor &&
143 		    PCI_PRODUCT(pa->pa_id) == spp->spp_product) {
144 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
145 			    PCI_SUBSYS_ID_REG);
146 			for (subspp = spp->spp_subsys;
147 			     subspp->spp_name != NULL; subspp++) {
148 				if (PCI_VENDOR(subsysid) ==
149 					subspp->spp_vendor ||
150 				    PCI_PRODUCT(subsysid) ==
151 					subspp->spp_product) {
152 					return (subspp);
153 				}
154 			}
155 			return (spp);
156 		}
157 	}
158 
159 	return (NULL);
160 }
161 
162 static int
163 sf_pci_match(struct device *parent, struct cfdata *match,
164     void *aux)
165 {
166 	struct pci_attach_args *pa = aux;
167 
168 	if (sf_pci_lookup(pa) != NULL)
169 		return (1);
170 
171 	return (0);
172 }
173 
174 static void
175 sf_pci_attach(struct device *parent, struct device *self, void *aux)
176 {
177 	struct sf_pci_softc *psc = (void *) self;
178 	struct sf_softc *sc = &psc->sc_starfire;
179 	struct pci_attach_args *pa = aux;
180 	pci_intr_handle_t ih;
181 	const char *intrstr = NULL;
182 	const struct sf_pci_product *spp;
183 	bus_space_tag_t iot, memt;
184 	bus_space_handle_t ioh, memh;
185 	pcireg_t reg;
186 	int error, ioh_valid, memh_valid;
187 
188 	spp = sf_pci_lookup(pa);
189 	if (spp == NULL) {
190 		printf("\n");
191 		panic("sf_pci_attach: impossible");
192 	}
193 
194 	printf(": %s, rev. %d\n", spp->spp_name, PCI_REVISION(pa->pa_class));
195 
196 	/* power up chip */
197 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
198 	    NULL)) && error != EOPNOTSUPP) {
199 		aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
200 		    error);
201 		return;
202 	}
203 
204 	/*
205 	 * Map the device.
206 	 */
207 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
208 	switch (reg) {
209 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
210 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
211 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
212 		    reg, 0, &memt, &memh, NULL, NULL) == 0);
213 		break;
214 	default:
215 		memh_valid = 0;
216 	}
217 
218 	ioh_valid = (pci_mapreg_map(pa,
219 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
220 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
221 	    PCI_MAPREG_TYPE_IO, 0,
222 	    &iot, &ioh, NULL, NULL) == 0);
223 
224 	if (memh_valid) {
225 		sc->sc_st = memt;
226 		sc->sc_sh = memh;
227 		sc->sc_iomapped = 0;
228 	} else if (ioh_valid) {
229 		sc->sc_st = iot;
230 		sc->sc_sh = ioh;
231 		sc->sc_iomapped = 1;
232 	} else {
233 		printf("%s: unable to map device registers\n",
234 		    sc->sc_dev.dv_xname);
235 		return;
236 	}
237 
238 	sc->sc_dmat = pa->pa_dmat;
239 
240 	/* Make sure bus mastering is enabled. */
241 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
242 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
243 	    PCI_COMMAND_MASTER_ENABLE);
244 
245 	/*
246 	 * Map and establish our interrupt.
247 	 */
248 	if (pci_intr_map(pa, &ih)) {
249 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
250 		return;
251 	}
252 	intrstr = pci_intr_string(pa->pa_pc, ih);
253 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, sf_intr, sc);
254 	if (psc->sc_ih == NULL) {
255 		printf("%s: unable to establish interrupt",
256 		    sc->sc_dev.dv_xname);
257 		if (intrstr != NULL)
258 			printf(" at %s", intrstr);
259 		printf("\n");
260 		return;
261 	}
262 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
263 
264 	/*
265 	 * Finish off the attach.
266 	 */
267 	sf_attach(sc);
268 }
269