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