xref: /netbsd-src/sys/dev/pci/if_sf_pci.c (revision 17306b8fd0952c7489f93f0230818481e5a1e2c9)
1 /*	$NetBSD: if_sf_pci.c,v 1.1 2001/06/18 22:05:37 thorpej 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 "bpfilter.h"
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/mbuf.h>
49 #include <sys/malloc.h>
50 #include <sys/kernel.h>
51 #include <sys/socket.h>
52 #include <sys/ioctl.h>
53 #include <sys/errno.h>
54 #include <sys/device.h>
55 
56 #include <net/if.h>
57 #include <net/if_dl.h>
58 #include <net/if_media.h>
59 #include <net/if_ether.h>
60 
61 #if NBPFILTER > 0
62 #include <net/bpf.h>
63 #endif
64 
65 #include <machine/bus.h>
66 #include <machine/intr.h>
67 
68 #include <dev/mii/miivar.h>
69 
70 #include <dev/ic/aic6915reg.h>
71 #include <dev/ic/aic6915var.h>
72 
73 #include <dev/pci/pcireg.h>
74 #include <dev/pci/pcivar.h>
75 #include <dev/pci/pcidevs.h>
76 
77 struct sf_pci_softc {
78 	struct sf_softc sc_starfire;	/* read Starfire softc */
79 
80 	/* PCI-specific goo. */
81 	void	*sc_ih;			/* interrupt handle */
82 };
83 
84 int	sf_pci_match(struct device *, struct cfdata *, void *);
85 void	sf_pci_attach(struct device *, struct device *, void *);
86 
87 struct cfattach sf_pci_ca = {
88 	sizeof(struct sf_pci_softc), sf_pci_match, sf_pci_attach,
89 };
90 
91 struct sf_pci_product {
92 	uint32_t	spp_vendor;	/* PCI vendor ID */
93 	uint32_t	spp_product;	/* PCI product ID */
94 	const char	*spp_name;	/* product name */
95 	const struct sf_pci_product *spp_subsys; /* subsystm IDs */
96 };
97 
98 const struct sf_pci_product sf_subsys_adaptec[] = {
99 	/* ANA-62011 (rev 0) Single port 10/100 64-bit */
100 	{ PCI_VENDOR_ADP,			0x0008,
101 	  "ANA-62011 (rev 0) 10/100 Ethernet",	NULL },
102 
103 	/* ANA-62011 (rev 1) Single port 10/100 64-bit */
104 	{ PCI_VENDOR_ADP,			0x0009,
105 	  "ANA-62011 (rev 1) 10/100 Ethernet",	NULL },
106 
107 	/* ANA-62022 Dual port 10/100 64-bit */
108 	{ PCI_VENDOR_ADP,			0x0010,
109 	  "ANA-62022 10/100 Ethernet",		NULL },
110 
111 	/* ANA-62044 (rev 0) Quad port 10/100 64-bit */
112 	{ PCI_VENDOR_ADP,			0x0018,
113 	  "ANA-62044 (rev 0) 10/100 Ethernet",	NULL },
114 
115 	/* ANA-62044 (rev 1) Quad port 10/100 64-bit */
116 	{ PCI_VENDOR_ADP,			0x0019,
117 	  "ANA-62044 (rev 1) 10/100 Ethernet",	NULL },
118 
119 	/* ANA-62020 Single port 100baseFX 64-bit */
120 	{ PCI_VENDOR_ADP,			0x0020,
121 	  "ANA-62020 100baseFX Ethernet",	NULL },
122 
123 	/* ANA-69011 Single port 10/100 32-bit */
124 	{ PCI_VENDOR_ADP,			0x0028,
125 	  "ANA-69011 10/100 Ethernet",		NULL },
126 
127 	{ 0, 					0,
128 	  NULL,					NULL },
129 };
130 
131 const struct sf_pci_product sf_pci_products[] = {
132 	{ PCI_VENDOR_ADP,			PCI_PRODUCT_ADP_AIC6915,
133 	  "AIC-6915 10/100 Ethernet",		sf_subsys_adaptec },
134 
135 	{ 0,					0,
136 	  NULL,					NULL },
137 };
138 
139 static const struct sf_pci_product *
140 sf_pci_lookup(const struct pci_attach_args *pa)
141 {
142 	const struct sf_pci_product *spp, *subspp;
143 	pcireg_t subsysid;
144 
145 	for (spp = sf_pci_products; spp->spp_name != NULL; spp++) {
146 		if (PCI_VENDOR(pa->pa_id) == spp->spp_vendor &&
147 		    PCI_PRODUCT(pa->pa_id) == spp->spp_product) {
148 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
149 			    PCI_SUBSYS_ID_REG);
150 			for (subspp = spp->spp_subsys;
151 			     subspp->spp_name != NULL; subspp++) {
152 				if (PCI_VENDOR(subsysid) ==
153 					subspp->spp_vendor ||
154 				    PCI_PRODUCT(subsysid) ==
155 					subspp->spp_product) {
156 					return (subspp);
157 				}
158 			}
159 			return (spp);
160 		}
161 	}
162 
163 	return (NULL);
164 }
165 
166 int
167 sf_pci_match(struct device *parent, struct cfdata *match, void *aux)
168 {
169 	struct pci_attach_args *pa = aux;
170 
171 	if (sf_pci_lookup(pa) != NULL)
172 		return (1);
173 
174 	return (0);
175 }
176 
177 void
178 sf_pci_attach(struct device *parent, struct device *self, void *aux)
179 {
180 	struct sf_pci_softc *psc = (void *) self;
181 	struct sf_softc *sc = &psc->sc_starfire;
182 	struct pci_attach_args *pa = aux;
183 	pci_intr_handle_t ih;
184 	const char *intrstr = NULL;
185 	const struct sf_pci_product *spp;
186 	bus_space_tag_t iot, memt;
187 	bus_space_handle_t ioh, memh;
188 	pcireg_t reg;
189 	int pmreg, ioh_valid, memh_valid;
190 
191 	spp = sf_pci_lookup(pa);
192 	if (spp == NULL) {
193 		printf("\n");
194 		panic("sf_pci_attach: impossible");
195 	}
196 
197 	printf(": %s, rev. %d\n", spp->spp_name, PCI_REVISION(pa->pa_class));
198 
199 	if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PWRMGMT,
200 	    &pmreg, 0)) {
201 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, pmreg + 4);
202 		switch (reg & PCI_PMCSR_STATE_MASK) {
203 		case PCI_PMCSR_STATE_D1:
204 		case PCI_PMCSR_STATE_D2:
205 			printf(": waking up from power state D%d\n%s",
206 			    reg & PCI_PMCSR_STATE_MASK, sc->sc_dev.dv_xname);
207 			pci_conf_write(pa->pa_pc, pa->pa_tag, pmreg + 4,
208 			    (reg & ~PCI_PMCSR_STATE_MASK) |
209 			    PCI_PMCSR_STATE_D0);
210 			break;
211 
212 		case PCI_PMCSR_STATE_D3:
213 			printf("%s: unable to wake up from power state D3\n",
214 			    sc->sc_dev.dv_xname);
215 			pci_conf_write(pa->pa_pc, pa->pa_tag, pmreg + 4,
216 			    (reg & ~PCI_PMCSR_STATE_MASK) |
217 			    PCI_PMCSR_STATE_D0);
218 			return;
219 		}
220 	}
221 
222 	/*
223 	 * Map the device.
224 	 */
225 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
226 	switch (reg) {
227 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
228 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
229 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
230 		    reg, 0, &memt, &memh, NULL, NULL) == 0);
231 		break;
232 	default:
233 		memh_valid = 0;
234 	}
235 
236 	ioh_valid = (pci_mapreg_map(pa,
237 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
238 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
239 	    PCI_MAPREG_TYPE_IO, 0,
240 	    &iot, &ioh, NULL, NULL) == 0);
241 
242 	if (memh_valid) {
243 		sc->sc_st = memt;
244 		sc->sc_sh = memh;
245 		sc->sc_iomapped = 0;
246 	} else if (ioh_valid) {
247 		sc->sc_st = iot;
248 		sc->sc_sh = ioh;
249 		sc->sc_iomapped = 1;
250 	} else {
251 		printf("%s: unable to map device registers\n",
252 		    sc->sc_dev.dv_xname);
253 		return;
254 	}
255 
256 	sc->sc_dmat = pa->pa_dmat;
257 
258 	/* Make sure bus mastering is enabled. */
259 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
260 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
261 	    PCI_COMMAND_MASTER_ENABLE);
262 
263 	/*
264 	 * Map and establish our interrupt.
265 	 */
266 	if (pci_intr_map(pa, &ih)) {
267 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
268 		return;
269 	}
270 	intrstr = pci_intr_string(pa->pa_pc, ih);
271 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, sf_intr, sc);
272 	if (psc->sc_ih == NULL) {
273 		printf("%s: unable to establish interrupt",
274 		    sc->sc_dev.dv_xname);
275 		if (intrstr != NULL)
276 			printf(" at %s", intrstr);
277 		printf("\n");
278 		return;
279 	}
280 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
281 
282 	/*
283 	 * Finish off the attach.
284 	 */
285 	sf_attach(sc);
286 }
287