if_fxp_cardbus.c revision 1.10 1 /* $NetBSD: if_fxp_cardbus.c,v 1.10 2000/03/12 17:05:23 veego Exp $ */
2
3 /*
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Johan Danielsson.
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 * CardBus front-end for the Intel i82557 family of Ethernet chips.
41 */
42
43 #include "opt_inet.h"
44 #include "opt_ns.h"
45 #include "bpfilter.h"
46 #include "rnd.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/mbuf.h>
51 #include <sys/malloc.h>
52 #include <sys/kernel.h>
53 #include <sys/socket.h>
54 #include <sys/ioctl.h>
55 #include <sys/errno.h>
56 #include <sys/device.h>
57
58 #if NRND > 0
59 #include <sys/rnd.h>
60 #endif
61
62 #include <net/if.h>
63 #include <net/if_dl.h>
64 #include <net/if_media.h>
65 #include <net/if_ether.h>
66
67 #include <machine/endian.h>
68
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/if_inarp.h>
76 #endif
77
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82
83 #include <machine/bus.h>
84 #include <machine/intr.h>
85
86 #include <dev/mii/miivar.h>
87
88 #include <dev/ic/i82557reg.h>
89 #include <dev/ic/i82557var.h>
90
91 #include <dev/pci/pcivar.h>
92 #include <dev/pci/pcireg.h>
93 #include <dev/pci/pcidevs.h>
94
95 #include <dev/cardbus/cardbusvar.h>
96 #include <dev/cardbus/cardbusdevs.h>
97
98 static int fxp_cardbus_match __P((struct device *, struct cfdata *, void *));
99 static void fxp_cardbus_attach __P((struct device *, struct device *, void *));
100 static int fxp_cardbus_detach __P((struct device * self, int flags));
101 static void fxp_cardbus_setup __P((struct fxp_softc * sc));
102 static int fxp_cardbus_enable __P((struct fxp_softc * sc));
103 static void fxp_cardbus_disable __P((struct fxp_softc * sc));
104
105 struct fxp_cardbus_softc {
106 struct fxp_softc sc;
107 cardbus_devfunc_t ct;
108 pcireg_t base0_reg;
109 pcireg_t base1_reg;
110 bus_size_t size;
111 };
112
113 struct cfattach fxp_cardbus_ca = {
114 sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach,
115 fxp_cardbus_detach, fxp_activate
116 };
117
118 #ifdef CBB_DEBUG
119 #define DPRINTF(X) printf X
120 #else
121 #define DPRINTF(X)
122 #endif
123
124 static int
125 fxp_cardbus_match(parent, match, aux)
126 struct device *parent;
127 struct cfdata *match;
128 void *aux;
129 {
130 struct cardbus_attach_args *ca = aux;
131
132 if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_INTEL &&
133 CARDBUS_PRODUCT(ca->ca_id) != CARDBUS_PRODUCT_INTEL_82557)
134 return 0;
135 return 1;
136 }
137
138 static void
139 fxp_cardbus_attach(parent, self, aux)
140 struct device *parent, *self;
141 void *aux;
142 {
143 static const char __func__[] = "fxp_cardbus_attach";
144
145 struct fxp_softc *sc = (struct fxp_softc *) self;
146 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
147 struct cardbus_attach_args *ca = aux;
148 bus_space_tag_t iot, memt;
149 bus_space_handle_t ioh, memh;
150
151 bus_addr_t adr;
152 bus_size_t size;
153
154 csc->ct = ca->ca_ct;
155
156 /*
157 * Map control/status registers.
158 */
159 if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
160 PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
161 csc->base1_reg = adr | 1;
162 sc->sc_st = iot;
163 sc->sc_sh = ioh;
164 csc->size = size;
165 } else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
166 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
167 0, &memt, &memh, &adr, &size) == 0) {
168 csc->base0_reg = adr;
169 sc->sc_st = memt;
170 sc->sc_sh = memh;
171 csc->size = size;
172 } else
173 panic("%s: failed to allocate mem and io space", __func__);
174
175
176 if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
177 printf(": %s %s\n", ca->ca_cis.cis1_info[0],
178 ca->ca_cis.cis1_info[1]);
179 else
180 printf("\n");
181
182 sc->sc_dmat = ca->ca_dmat;
183 sc->sc_enable = fxp_cardbus_enable;
184 sc->sc_disable = fxp_cardbus_disable;
185 sc->sc_enabled = 0;
186
187 fxp_enable(sc);
188 fxp_attach(sc);
189 fxp_disable(sc);
190 }
191
192 static void
193 fxp_cardbus_setup(struct fxp_softc * sc)
194 {
195 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
196 struct cardbus_softc *psc =
197 (struct cardbus_softc *) sc->sc_dev.dv_parent;
198 cardbus_chipset_tag_t cc = psc->sc_cc;
199 cardbus_function_tag_t cf = psc->sc_cf;
200 pcireg_t command;
201
202 cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
203 csc->ct->ct_dev, csc->ct->ct_func);
204
205 command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
206 if (csc->base0_reg) {
207 Cardbus_conf_write(csc->ct, tag,
208 CARDBUS_BASE0_REG, csc->base0_reg);
209 (cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
210 command |= CARDBUS_COMMAND_MEM_ENABLE |
211 CARDBUS_COMMAND_MASTER_ENABLE;
212 } else if (csc->base1_reg) {
213 Cardbus_conf_write(csc->ct, tag,
214 CARDBUS_BASE1_REG, csc->base1_reg);
215 (cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
216 command |= (CARDBUS_COMMAND_IO_ENABLE |
217 CARDBUS_COMMAND_MASTER_ENABLE);
218 }
219
220 (cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
221
222 /* enable the card */
223 Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
224 }
225
226 static int
227 fxp_cardbus_enable(struct fxp_softc * sc)
228 {
229 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
230 struct cardbus_softc *psc =
231 (struct cardbus_softc *) sc->sc_dev.dv_parent;
232 cardbus_chipset_tag_t cc = psc->sc_cc;
233 cardbus_function_tag_t cf = psc->sc_cf;
234
235 Cardbus_function_enable(csc->ct);
236
237 fxp_cardbus_setup(sc);
238
239 /* Map and establish the interrupt. */
240
241 sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
242 fxp_intr, sc);
243 if (NULL == sc->sc_ih) {
244 printf("%s: couldn't establish interrupt\n",
245 sc->sc_dev.dv_xname);
246 return 1;
247 }
248
249 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
250 psc->sc_intrline);
251
252 return 0;
253 }
254
255 static void
256 fxp_cardbus_disable(struct fxp_softc * sc)
257 {
258 struct cardbus_softc *psc =
259 (struct cardbus_softc *) sc->sc_dev.dv_parent;
260 cardbus_chipset_tag_t cc = psc->sc_cc;
261 cardbus_function_tag_t cf = psc->sc_cf;
262
263 /* Remove interrupt handler. */
264 cardbus_intr_disestablish(cc, cf, sc->sc_ih);
265
266 Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
267 }
268
269 static int
270 fxp_cardbus_detach(self, flags)
271 struct device *self;
272 int flags;
273 {
274 struct fxp_softc *sc = (struct fxp_softc *) self;
275 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
276 struct cardbus_devfunc *ct = csc->ct;
277 int rv, reg;
278
279 #ifdef DIAGNOSTIC
280 if (ct == NULL)
281 panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
282 #endif
283
284 rv = fxp_detach(sc);
285 if (rv == 0) {
286 /*
287 * Unhook the interrupt handler.
288 */
289 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
290
291 /*
292 * release bus space and close window
293 */
294 if (csc->base0_reg)
295 reg = CARDBUS_BASE0_REG;
296 else
297 reg = CARDBUS_BASE1_REG;
298 Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
299 }
300 return (rv);
301 }
302