xref: /netbsd-src/sys/dev/pci/if_sf_pci.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: if_sf_pci.c,v 1.10 2006/06/17 23:34:27 christos 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.10 2006/06/17 23:34:27 christos 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 <machine/bus.h>
63 #include <machine/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, void *aux)
164 {
165 	struct pci_attach_args *pa = aux;
166 
167 	if (sf_pci_lookup(pa) != NULL)
168 		return (1);
169 
170 	return (0);
171 }
172 
173 static void
174 sf_pci_attach(struct device *parent, struct device *self, void *aux)
175 {
176 	struct sf_pci_softc *psc = (void *) self;
177 	struct sf_softc *sc = &psc->sc_starfire;
178 	struct pci_attach_args *pa = aux;
179 	pci_intr_handle_t ih;
180 	const char *intrstr = NULL;
181 	const struct sf_pci_product *spp;
182 	bus_space_tag_t iot, memt;
183 	bus_space_handle_t ioh, memh;
184 	pcireg_t reg;
185 	int error, ioh_valid, memh_valid;
186 
187 	spp = sf_pci_lookup(pa);
188 	if (spp == NULL) {
189 		printf("\n");
190 		panic("sf_pci_attach: impossible");
191 	}
192 
193 	printf(": %s, rev. %d\n", spp->spp_name, PCI_REVISION(pa->pa_class));
194 
195 	/* power up chip */
196 	if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc,
197 	    NULL)) && error != EOPNOTSUPP) {
198 		aprint_error("%s: cannot activate %d\n", sc->sc_dev.dv_xname,
199 		    error);
200 		return;
201 	}
202 
203 	/*
204 	 * Map the device.
205 	 */
206 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
207 	switch (reg) {
208 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
209 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
210 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
211 		    reg, 0, &memt, &memh, NULL, NULL) == 0);
212 		break;
213 	default:
214 		memh_valid = 0;
215 	}
216 
217 	ioh_valid = (pci_mapreg_map(pa,
218 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
219 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
220 	    PCI_MAPREG_TYPE_IO, 0,
221 	    &iot, &ioh, NULL, NULL) == 0);
222 
223 	if (memh_valid) {
224 		sc->sc_st = memt;
225 		sc->sc_sh = memh;
226 		sc->sc_iomapped = 0;
227 	} else if (ioh_valid) {
228 		sc->sc_st = iot;
229 		sc->sc_sh = ioh;
230 		sc->sc_iomapped = 1;
231 	} else {
232 		printf("%s: unable to map device registers\n",
233 		    sc->sc_dev.dv_xname);
234 		return;
235 	}
236 
237 	sc->sc_dmat = pa->pa_dmat;
238 
239 	/* Make sure bus mastering is enabled. */
240 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
241 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
242 	    PCI_COMMAND_MASTER_ENABLE);
243 
244 	/*
245 	 * Map and establish our interrupt.
246 	 */
247 	if (pci_intr_map(pa, &ih)) {
248 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
249 		return;
250 	}
251 	intrstr = pci_intr_string(pa->pa_pc, ih);
252 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, sf_intr, sc);
253 	if (psc->sc_ih == NULL) {
254 		printf("%s: unable to establish interrupt",
255 		    sc->sc_dev.dv_xname);
256 		if (intrstr != NULL)
257 			printf(" at %s", intrstr);
258 		printf("\n");
259 		return;
260 	}
261 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
262 
263 	/*
264 	 * Finish off the attach.
265 	 */
266 	sf_attach(sc);
267 }
268