xref: /netbsd/sys/dev/pci/if_sf_pci.c (revision bf9ec67e)
1 /*	$NetBSD: if_sf_pci.c,v 1.3 2001/11/13 07:48:44 lukem 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.3 2001/11/13 07:48:44 lukem 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 int	sf_pci_match(struct device *, struct cfdata *, void *);
82 void	sf_pci_attach(struct device *, struct device *, void *);
83 
84 struct cfattach sf_pci_ca = {
85 	sizeof(struct sf_pci_softc), sf_pci_match, sf_pci_attach,
86 };
87 
88 struct sf_pci_product {
89 	uint32_t	spp_vendor;	/* PCI vendor ID */
90 	uint32_t	spp_product;	/* PCI product ID */
91 	const char	*spp_name;	/* product name */
92 	const struct sf_pci_product *spp_subsys; /* subsystm IDs */
93 };
94 
95 const struct sf_pci_product sf_subsys_adaptec[] = {
96 	/* ANA-62011 (rev 0) Single port 10/100 64-bit */
97 	{ PCI_VENDOR_ADP,			0x0008,
98 	  "ANA-62011 (rev 0) 10/100 Ethernet",	NULL },
99 
100 	/* ANA-62011 (rev 1) Single port 10/100 64-bit */
101 	{ PCI_VENDOR_ADP,			0x0009,
102 	  "ANA-62011 (rev 1) 10/100 Ethernet",	NULL },
103 
104 	/* ANA-62022 Dual port 10/100 64-bit */
105 	{ PCI_VENDOR_ADP,			0x0010,
106 	  "ANA-62022 10/100 Ethernet",		NULL },
107 
108 	/* ANA-62044 (rev 0) Quad port 10/100 64-bit */
109 	{ PCI_VENDOR_ADP,			0x0018,
110 	  "ANA-62044 (rev 0) 10/100 Ethernet",	NULL },
111 
112 	/* ANA-62044 (rev 1) Quad port 10/100 64-bit */
113 	{ PCI_VENDOR_ADP,			0x0019,
114 	  "ANA-62044 (rev 1) 10/100 Ethernet",	NULL },
115 
116 	/* ANA-62020 Single port 100baseFX 64-bit */
117 	{ PCI_VENDOR_ADP,			0x0020,
118 	  "ANA-62020 100baseFX Ethernet",	NULL },
119 
120 	/* ANA-69011 Single port 10/100 32-bit */
121 	{ PCI_VENDOR_ADP,			0x0028,
122 	  "ANA-69011 10/100 Ethernet",		NULL },
123 
124 	{ 0, 					0,
125 	  NULL,					NULL },
126 };
127 
128 const struct sf_pci_product sf_pci_products[] = {
129 	{ PCI_VENDOR_ADP,			PCI_PRODUCT_ADP_AIC6915,
130 	  "AIC-6915 10/100 Ethernet",		sf_subsys_adaptec },
131 
132 	{ 0,					0,
133 	  NULL,					NULL },
134 };
135 
136 static const struct sf_pci_product *
137 sf_pci_lookup(const struct pci_attach_args *pa)
138 {
139 	const struct sf_pci_product *spp, *subspp;
140 	pcireg_t subsysid;
141 
142 	for (spp = sf_pci_products; spp->spp_name != NULL; spp++) {
143 		if (PCI_VENDOR(pa->pa_id) == spp->spp_vendor &&
144 		    PCI_PRODUCT(pa->pa_id) == spp->spp_product) {
145 			subsysid = pci_conf_read(pa->pa_pc, pa->pa_tag,
146 			    PCI_SUBSYS_ID_REG);
147 			for (subspp = spp->spp_subsys;
148 			     subspp->spp_name != NULL; subspp++) {
149 				if (PCI_VENDOR(subsysid) ==
150 					subspp->spp_vendor ||
151 				    PCI_PRODUCT(subsysid) ==
152 					subspp->spp_product) {
153 					return (subspp);
154 				}
155 			}
156 			return (spp);
157 		}
158 	}
159 
160 	return (NULL);
161 }
162 
163 int
164 sf_pci_match(struct device *parent, struct cfdata *match, 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 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 pmreg, 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 	if (pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_PWRMGMT,
197 	    &pmreg, 0)) {
198 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, pmreg + 4);
199 		switch (reg & PCI_PMCSR_STATE_MASK) {
200 		case PCI_PMCSR_STATE_D1:
201 		case PCI_PMCSR_STATE_D2:
202 			printf(": waking up from power state D%d\n%s",
203 			    reg & PCI_PMCSR_STATE_MASK, sc->sc_dev.dv_xname);
204 			pci_conf_write(pa->pa_pc, pa->pa_tag, pmreg + 4,
205 			    (reg & ~PCI_PMCSR_STATE_MASK) |
206 			    PCI_PMCSR_STATE_D0);
207 			break;
208 
209 		case PCI_PMCSR_STATE_D3:
210 			printf("%s: unable to wake up from power state D3\n",
211 			    sc->sc_dev.dv_xname);
212 			pci_conf_write(pa->pa_pc, pa->pa_tag, pmreg + 4,
213 			    (reg & ~PCI_PMCSR_STATE_MASK) |
214 			    PCI_PMCSR_STATE_D0);
215 			return;
216 		}
217 	}
218 
219 	/*
220 	 * Map the device.
221 	 */
222 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
223 	switch (reg) {
224 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
225 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
226 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
227 		    reg, 0, &memt, &memh, NULL, NULL) == 0);
228 		break;
229 	default:
230 		memh_valid = 0;
231 	}
232 
233 	ioh_valid = (pci_mapreg_map(pa,
234 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
235 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
236 	    PCI_MAPREG_TYPE_IO, 0,
237 	    &iot, &ioh, NULL, NULL) == 0);
238 
239 	if (memh_valid) {
240 		sc->sc_st = memt;
241 		sc->sc_sh = memh;
242 		sc->sc_iomapped = 0;
243 	} else if (ioh_valid) {
244 		sc->sc_st = iot;
245 		sc->sc_sh = ioh;
246 		sc->sc_iomapped = 1;
247 	} else {
248 		printf("%s: unable to map device registers\n",
249 		    sc->sc_dev.dv_xname);
250 		return;
251 	}
252 
253 	sc->sc_dmat = pa->pa_dmat;
254 
255 	/* Make sure bus mastering is enabled. */
256 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
257 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
258 	    PCI_COMMAND_MASTER_ENABLE);
259 
260 	/*
261 	 * Map and establish our interrupt.
262 	 */
263 	if (pci_intr_map(pa, &ih)) {
264 		printf("%s: unable to map interrupt\n", sc->sc_dev.dv_xname);
265 		return;
266 	}
267 	intrstr = pci_intr_string(pa->pa_pc, ih);
268 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, sf_intr, sc);
269 	if (psc->sc_ih == NULL) {
270 		printf("%s: unable to establish interrupt",
271 		    sc->sc_dev.dv_xname);
272 		if (intrstr != NULL)
273 			printf(" at %s", intrstr);
274 		printf("\n");
275 		return;
276 	}
277 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
278 
279 	/*
280 	 * Finish off the attach.
281 	 */
282 	sf_attach(sc);
283 }
284