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