if_fxp_cardbus.c revision 1.9 1 1.9 thorpej /* $NetBSD: if_fxp_cardbus.c,v 1.9 2000/03/10 07:11:47 thorpej Exp $ */
2 1.1 haya
3 1.1 haya /*
4 1.3 joda * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.3 joda * All rights reserved.
6 1.1 haya *
7 1.9 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.9 thorpej * by Johan Danielsson.
9 1.1 haya *
10 1.9 thorpej * Redistribution and use in source and binary forms, with or without
11 1.9 thorpej * modification, are permitted provided that the following conditions
12 1.9 thorpej * are met:
13 1.9 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.9 thorpej * notice, this list of conditions and the following disclaimer.
15 1.9 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.9 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.9 thorpej * documentation and/or other materials provided with the distribution.
18 1.9 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.9 thorpej * must display the following acknowledgement:
20 1.9 thorpej * This product includes software developed by the NetBSD
21 1.9 thorpej * Foundation, Inc. and its contributors.
22 1.9 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3 joda * contributors may be used to endorse or promote products derived
24 1.3 joda * from this software without specific prior written permission.
25 1.3 joda *
26 1.3 joda * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3 joda * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3 joda * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3 joda * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3 joda * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3 joda * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3 joda * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3 joda * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3 joda * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3 joda * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 haya * POSSIBILITY OF SUCH DAMAGE.
37 1.1 haya */
38 1.1 haya
39 1.9 thorpej /*
40 1.9 thorpej * CardBus front-end for the Intel i82557 family of Ethernet chips.
41 1.9 thorpej */
42 1.1 haya
43 1.1 haya #include "opt_inet.h"
44 1.1 haya #include "opt_ns.h"
45 1.1 haya #include "bpfilter.h"
46 1.1 haya #include "rnd.h"
47 1.1 haya
48 1.1 haya #include <sys/param.h>
49 1.1 haya #include <sys/systm.h>
50 1.1 haya #include <sys/mbuf.h>
51 1.1 haya #include <sys/malloc.h>
52 1.1 haya #include <sys/kernel.h>
53 1.1 haya #include <sys/socket.h>
54 1.1 haya #include <sys/ioctl.h>
55 1.1 haya #include <sys/errno.h>
56 1.1 haya #include <sys/device.h>
57 1.1 haya
58 1.1 haya #if NRND > 0
59 1.1 haya #include <sys/rnd.h>
60 1.1 haya #endif
61 1.1 haya
62 1.1 haya #include <net/if.h>
63 1.1 haya #include <net/if_dl.h>
64 1.1 haya #include <net/if_media.h>
65 1.1 haya #include <net/if_ether.h>
66 1.5 thorpej
67 1.5 thorpej #include <machine/endian.h>
68 1.1 haya
69 1.1 haya #if NBPFILTER > 0
70 1.1 haya #include <net/bpf.h>
71 1.1 haya #endif
72 1.1 haya
73 1.1 haya #ifdef INET
74 1.1 haya #include <netinet/in.h>
75 1.1 haya #include <netinet/if_inarp.h>
76 1.1 haya #endif
77 1.1 haya
78 1.1 haya #ifdef NS
79 1.1 haya #include <netns/ns.h>
80 1.1 haya #include <netns/ns_if.h>
81 1.1 haya #endif
82 1.1 haya
83 1.1 haya #include <machine/bus.h>
84 1.1 haya #include <machine/intr.h>
85 1.1 haya
86 1.1 haya #include <dev/mii/miivar.h>
87 1.1 haya
88 1.3 joda #include <dev/ic/i82557reg.h>
89 1.3 joda #include <dev/ic/i82557var.h>
90 1.1 haya
91 1.1 haya #include <dev/pci/pcivar.h>
92 1.1 haya #include <dev/pci/pcireg.h>
93 1.1 haya #include <dev/pci/pcidevs.h>
94 1.1 haya
95 1.1 haya #include <dev/cardbus/cardbusvar.h>
96 1.1 haya #include <dev/cardbus/cardbusdevs.h>
97 1.1 haya
98 1.1 haya static int fxp_cardbus_match __P((struct device *, struct cfdata *, void *));
99 1.1 haya static void fxp_cardbus_attach __P((struct device *, struct device *, void *));
100 1.9 thorpej static int fxp_cardbus_detach __P((struct device * self, int flags));
101 1.9 thorpej static void fxp_cardbus_setup __P((struct fxp_softc * sc));
102 1.9 thorpej static int fxp_cardbus_enable __P((struct fxp_softc * sc));
103 1.9 thorpej static void fxp_cardbus_disable __P((struct fxp_softc * sc));
104 1.1 haya
105 1.1 haya struct fxp_cardbus_softc {
106 1.9 thorpej struct fxp_softc sc;
107 1.9 thorpej cardbus_devfunc_t ct;
108 1.9 thorpej pcireg_t base0_reg;
109 1.9 thorpej pcireg_t base1_reg;
110 1.9 thorpej bus_size_t size;
111 1.1 haya };
112 1.1 haya
113 1.1 haya struct cfattach fxp_cardbus_ca = {
114 1.9 thorpej sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach,
115 1.9 thorpej fxp_cardbus_detach, fxp_activate
116 1.1 haya };
117 1.1 haya
118 1.1 haya #ifdef CBB_DEBUG
119 1.1 haya #define DPRINTF(X) printf X
120 1.1 haya #else
121 1.1 haya #define DPRINTF(X)
122 1.1 haya #endif
123 1.1 haya
124 1.1 haya static int
125 1.1 haya fxp_cardbus_match(parent, match, aux)
126 1.9 thorpej struct device *parent;
127 1.9 thorpej struct cfdata *match;
128 1.9 thorpej void *aux;
129 1.1 haya {
130 1.9 thorpej struct cardbus_attach_args *ca = aux;
131 1.9 thorpej
132 1.9 thorpej if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_INTEL &&
133 1.9 thorpej CARDBUS_PRODUCT(ca->ca_id) != CARDBUS_PRODUCT_INTEL_82557)
134 1.9 thorpej return 0;
135 1.9 thorpej return 1;
136 1.1 haya }
137 1.1 haya
138 1.1 haya static void
139 1.1 haya fxp_cardbus_attach(parent, self, aux)
140 1.9 thorpej struct device *parent, *self;
141 1.9 thorpej void *aux;
142 1.1 haya {
143 1.9 thorpej static const char __func__[] = "fxp_cardbus_attach";
144 1.6 joda
145 1.9 thorpej struct fxp_softc *sc = (struct fxp_softc *) self;
146 1.9 thorpej struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
147 1.9 thorpej struct cardbus_attach_args *ca = aux;
148 1.9 thorpej bus_space_tag_t iot, memt;
149 1.9 thorpej bus_space_handle_t ioh, memh;
150 1.9 thorpej
151 1.9 thorpej bus_addr_t adr;
152 1.9 thorpej bus_size_t size;
153 1.9 thorpej
154 1.9 thorpej csc->ct = ca->ca_ct;
155 1.9 thorpej
156 1.9 thorpej /*
157 1.9 thorpej * Map control/status registers.
158 1.9 thorpej */
159 1.9 thorpej if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
160 1.9 thorpej PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
161 1.9 thorpej 0, &memt, &memh, &adr, &size) == 0) {
162 1.9 thorpej csc->base0_reg = adr;
163 1.9 thorpej sc->sc_st = memt;
164 1.9 thorpej sc->sc_sh = memh;
165 1.9 thorpej csc->size = size;
166 1.9 thorpej } else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
167 1.9 thorpej PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
168 1.9 thorpej csc->base1_reg = adr | 1;
169 1.9 thorpej sc->sc_st = iot;
170 1.9 thorpej sc->sc_sh = ioh;
171 1.9 thorpej csc->size = size;
172 1.9 thorpej } else
173 1.9 thorpej panic("%s: failed to allocate mem and io space", __func__);
174 1.9 thorpej
175 1.9 thorpej
176 1.9 thorpej if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
177 1.9 thorpej printf(": %s %s\n", ca->ca_cis.cis1_info[0],
178 1.9 thorpej ca->ca_cis.cis1_info[1]);
179 1.9 thorpej else
180 1.9 thorpej printf("\n");
181 1.9 thorpej
182 1.9 thorpej sc->sc_dmat = ca->ca_dmat;
183 1.9 thorpej sc->sc_enable = fxp_cardbus_enable;
184 1.9 thorpej sc->sc_disable = fxp_cardbus_disable;
185 1.9 thorpej sc->sc_enabled = 0;
186 1.9 thorpej
187 1.9 thorpej fxp_enable(sc);
188 1.9 thorpej fxp_attach(sc);
189 1.9 thorpej fxp_disable(sc);
190 1.1 haya }
191 1.1 haya
192 1.1 haya static void
193 1.9 thorpej fxp_cardbus_setup(struct fxp_softc * sc)
194 1.1 haya {
195 1.9 thorpej struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
196 1.9 thorpej struct cardbus_softc *psc =
197 1.9 thorpej (struct cardbus_softc *) sc->sc_dev.dv_parent;
198 1.9 thorpej cardbus_chipset_tag_t cc = psc->sc_cc;
199 1.9 thorpej cardbus_function_tag_t cf = psc->sc_cf;
200 1.9 thorpej pcireg_t command;
201 1.9 thorpej
202 1.9 thorpej cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
203 1.9 thorpej csc->ct->ct_dev, csc->ct->ct_func);
204 1.9 thorpej
205 1.9 thorpej command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
206 1.9 thorpej if (csc->base0_reg) {
207 1.9 thorpej Cardbus_conf_write(csc->ct, tag,
208 1.9 thorpej CARDBUS_BASE0_REG, csc->base0_reg);
209 1.9 thorpej (cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
210 1.9 thorpej command |= CARDBUS_COMMAND_MEM_ENABLE |
211 1.9 thorpej CARDBUS_COMMAND_MASTER_ENABLE;
212 1.9 thorpej } else if (csc->base1_reg) {
213 1.9 thorpej Cardbus_conf_write(csc->ct, tag,
214 1.9 thorpej CARDBUS_BASE1_REG, csc->base1_reg);
215 1.9 thorpej (cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
216 1.9 thorpej command |= (CARDBUS_COMMAND_IO_ENABLE |
217 1.9 thorpej CARDBUS_COMMAND_MASTER_ENABLE);
218 1.9 thorpej }
219 1.9 thorpej
220 1.9 thorpej (cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
221 1.9 thorpej
222 1.9 thorpej /* enable the card */
223 1.9 thorpej Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
224 1.1 haya }
225 1.1 haya
226 1.1 haya static int
227 1.9 thorpej fxp_cardbus_enable(struct fxp_softc * sc)
228 1.1 haya {
229 1.9 thorpej struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
230 1.9 thorpej struct cardbus_softc *psc =
231 1.9 thorpej (struct cardbus_softc *) sc->sc_dev.dv_parent;
232 1.9 thorpej cardbus_chipset_tag_t cc = psc->sc_cc;
233 1.9 thorpej cardbus_function_tag_t cf = psc->sc_cf;
234 1.9 thorpej
235 1.9 thorpej Cardbus_function_enable(csc->ct);
236 1.9 thorpej
237 1.9 thorpej fxp_cardbus_setup(sc);
238 1.9 thorpej
239 1.9 thorpej /* Map and establish the interrupt. */
240 1.9 thorpej
241 1.9 thorpej sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
242 1.9 thorpej fxp_intr, sc);
243 1.9 thorpej if (NULL == sc->sc_ih) {
244 1.9 thorpej printf("%s: couldn't establish interrupt\n",
245 1.9 thorpej sc->sc_dev.dv_xname);
246 1.9 thorpej return 1;
247 1.9 thorpej }
248 1.1 haya
249 1.9 thorpej printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname,
250 1.9 thorpej psc->sc_intrline);
251 1.1 haya
252 1.9 thorpej return 0;
253 1.1 haya }
254 1.1 haya
255 1.1 haya static void
256 1.9 thorpej fxp_cardbus_disable(struct fxp_softc * sc)
257 1.1 haya {
258 1.9 thorpej struct cardbus_softc *psc =
259 1.9 thorpej (struct cardbus_softc *) sc->sc_dev.dv_parent;
260 1.9 thorpej cardbus_chipset_tag_t cc = psc->sc_cc;
261 1.9 thorpej cardbus_function_tag_t cf = psc->sc_cf;
262 1.9 thorpej
263 1.9 thorpej /* Remove interrupt handler. */
264 1.9 thorpej cardbus_intr_disestablish(cc, cf, sc->sc_ih);
265 1.9 thorpej
266 1.9 thorpej Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
267 1.6 joda }
268 1.6 joda
269 1.6 joda static int
270 1.6 joda fxp_cardbus_detach(self, flags)
271 1.6 joda struct device *self;
272 1.6 joda int flags;
273 1.6 joda {
274 1.9 thorpej struct fxp_softc *sc = (struct fxp_softc *) self;
275 1.9 thorpej struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
276 1.6 joda struct cardbus_devfunc *ct = csc->ct;
277 1.9 thorpej int rv, reg;
278 1.6 joda
279 1.6 joda #ifdef DIAGNOSTIC
280 1.9 thorpej if (ct == NULL)
281 1.6 joda panic("%s: data structure lacks\n", sc->sc_dev.dv_xname);
282 1.6 joda #endif
283 1.6 joda
284 1.6 joda rv = fxp_detach(sc);
285 1.6 joda if (rv == 0) {
286 1.6 joda /*
287 1.6 joda * Unhook the interrupt handler.
288 1.6 joda */
289 1.6 joda cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
290 1.6 joda
291 1.6 joda /*
292 1.6 joda * release bus space and close window
293 1.6 joda */
294 1.9 thorpej if (csc->base0_reg)
295 1.6 joda reg = CARDBUS_BASE0_REG;
296 1.9 thorpej else
297 1.6 joda reg = CARDBUS_BASE1_REG;
298 1.6 joda Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
299 1.6 joda }
300 1.6 joda return (rv);
301 1.1 haya }
302