if_fxp_cardbus.c revision 1.29.10.2 1 /* $NetBSD: if_fxp_cardbus.c,v 1.29.10.2 2008/06/29 09:33:05 mjf 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * CardBus front-end for the Intel i82557 family of Ethernet chips.
34 */
35
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: if_fxp_cardbus.c,v 1.29.10.2 2008/06/29 09:33:05 mjf Exp $");
38
39 #include "opt_inet.h"
40 #include "bpfilter.h"
41 #include "rnd.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/mbuf.h>
46 #include <sys/malloc.h>
47 #include <sys/kernel.h>
48 #include <sys/socket.h>
49 #include <sys/ioctl.h>
50 #include <sys/errno.h>
51 #include <sys/device.h>
52
53 #if NRND > 0
54 #include <sys/rnd.h>
55 #endif
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/endian.h>
63
64 #if NBPFILTER > 0
65 #include <net/bpf.h>
66 #endif
67
68 #ifdef INET
69 #include <netinet/in.h>
70 #include <netinet/if_inarp.h>
71 #endif
72
73
74 #include <sys/bus.h>
75 #include <sys/intr.h>
76
77 #include <dev/mii/miivar.h>
78
79 #include <dev/ic/i82557reg.h>
80 #include <dev/ic/i82557var.h>
81
82 #include <dev/pci/pcivar.h>
83 #include <dev/pci/pcireg.h>
84 #include <dev/pci/pcidevs.h>
85
86 #include <dev/cardbus/cardbusvar.h>
87 #include <dev/pci/pcidevs.h>
88
89 static int fxp_cardbus_match(struct device *, struct cfdata *, void *);
90 static void fxp_cardbus_attach(struct device *, struct device *, void *);
91 static int fxp_cardbus_detach(struct device * self, int flags);
92 static void fxp_cardbus_setup(struct fxp_softc * sc);
93 static int fxp_cardbus_enable(struct fxp_softc * sc);
94 static void fxp_cardbus_disable(struct fxp_softc * sc);
95
96 struct fxp_cardbus_softc {
97 struct fxp_softc sc;
98 cardbus_devfunc_t ct;
99 pcireg_t base0_reg;
100 pcireg_t base1_reg;
101 bus_size_t size;
102 };
103
104 CFATTACH_DECL(fxp_cardbus, sizeof(struct fxp_cardbus_softc),
105 fxp_cardbus_match, fxp_cardbus_attach, fxp_cardbus_detach, fxp_activate);
106
107 #ifdef CBB_DEBUG
108 #define DPRINTF(X) printf X
109 #else
110 #define DPRINTF(X)
111 #endif
112
113 static int
114 fxp_cardbus_match(struct device *parent, struct cfdata *match,
115 void *aux)
116 {
117 struct cardbus_attach_args *ca = aux;
118
119 if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_INTEL &&
120 CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_INTEL_82557)
121 return (1);
122
123 return (0);
124 }
125
126 static void
127 fxp_cardbus_attach(struct device *parent, struct device *self,
128 void *aux)
129 {
130 static const char thisfunc[] = "fxp_cardbus_attach";
131
132 struct fxp_softc *sc = device_private(self);
133 struct fxp_cardbus_softc *csc = device_private(self);
134 struct cardbus_attach_args *ca = aux;
135 bus_space_tag_t iot, memt;
136 bus_space_handle_t ioh, memh;
137
138 bus_addr_t adr;
139 bus_size_t size;
140
141 csc->ct = ca->ca_ct;
142
143 /*
144 * Map control/status registers.
145 */
146 if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
147 PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
148 csc->base1_reg = adr | 1;
149 sc->sc_st = iot;
150 sc->sc_sh = ioh;
151 csc->size = size;
152 } else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
153 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
154 0, &memt, &memh, &adr, &size) == 0) {
155 csc->base0_reg = adr;
156 sc->sc_st = memt;
157 sc->sc_sh = memh;
158 csc->size = size;
159 } else
160 panic("%s: failed to allocate mem and io space", thisfunc);
161
162
163 if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
164 printf(": %s %s\n", ca->ca_cis.cis1_info[0],
165 ca->ca_cis.cis1_info[1]);
166 else
167 printf("\n");
168
169 sc->sc_dmat = ca->ca_dmat;
170 sc->sc_enable = fxp_cardbus_enable;
171 sc->sc_disable = fxp_cardbus_disable;
172 sc->sc_enabled = 0;
173
174 fxp_enable(sc);
175 fxp_attach(sc);
176 fxp_disable(sc);
177
178 if (!pmf_device_register(self, NULL, NULL))
179 aprint_error_dev(self, "couldn't establish power handler\n");
180 else
181 pmf_class_network_register(self, &sc->sc_ethercom.ec_if);
182 }
183
184 static void
185 fxp_cardbus_setup(struct fxp_softc * sc)
186 {
187 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
188 struct cardbus_softc *psc =
189 (struct cardbus_softc *) device_parent(&sc->sc_dev);
190 cardbus_chipset_tag_t cc = psc->sc_cc;
191 cardbus_function_tag_t cf = psc->sc_cf;
192 pcireg_t command;
193
194 cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
195 csc->ct->ct_func);
196
197 command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
198 if (csc->base0_reg) {
199 Cardbus_conf_write(csc->ct, tag,
200 CARDBUS_BASE0_REG, csc->base0_reg);
201 (cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
202 command |= CARDBUS_COMMAND_MEM_ENABLE |
203 CARDBUS_COMMAND_MASTER_ENABLE;
204 } else if (csc->base1_reg) {
205 Cardbus_conf_write(csc->ct, tag,
206 CARDBUS_BASE1_REG, csc->base1_reg);
207 (cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
208 command |= (CARDBUS_COMMAND_IO_ENABLE |
209 CARDBUS_COMMAND_MASTER_ENABLE);
210 }
211
212 (cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
213
214 /* enable the card */
215 Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
216 }
217
218 static int
219 fxp_cardbus_enable(struct fxp_softc * sc)
220 {
221 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
222 struct cardbus_softc *psc =
223 (struct cardbus_softc *) device_parent(&sc->sc_dev);
224 cardbus_chipset_tag_t cc = psc->sc_cc;
225 cardbus_function_tag_t cf = psc->sc_cf;
226
227 Cardbus_function_enable(csc->ct);
228
229 fxp_cardbus_setup(sc);
230
231 /* Map and establish the interrupt. */
232
233 sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
234 fxp_intr, sc);
235 if (NULL == sc->sc_ih) {
236 aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt\n");
237 return 1;
238 }
239
240 return 0;
241 }
242
243 static void
244 fxp_cardbus_disable(struct fxp_softc * sc)
245 {
246 struct cardbus_softc *psc =
247 (struct cardbus_softc *) device_parent(&sc->sc_dev);
248 cardbus_chipset_tag_t cc = psc->sc_cc;
249 cardbus_function_tag_t cf = psc->sc_cf;
250
251 /* Remove interrupt handler. */
252 cardbus_intr_disestablish(cc, cf, sc->sc_ih);
253
254 Cardbus_function_disable(((struct fxp_cardbus_softc *) sc)->ct);
255 }
256
257 static int
258 fxp_cardbus_detach(struct device *self, int flags)
259 {
260 struct fxp_softc *sc = device_private(self);
261 struct fxp_cardbus_softc *csc = device_private(self);
262 struct cardbus_devfunc *ct = csc->ct;
263 int rv, reg;
264
265 #ifdef DIAGNOSTIC
266 if (ct == NULL)
267 panic("%s: data structure lacks", device_xname(&sc->sc_dev));
268 #endif
269
270 rv = fxp_detach(sc);
271 if (rv == 0) {
272 /*
273 * Unhook the interrupt handler.
274 */
275 cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
276
277 /*
278 * release bus space and close window
279 */
280 if (csc->base0_reg)
281 reg = CARDBUS_BASE0_REG;
282 else
283 reg = CARDBUS_BASE1_REG;
284 Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
285 }
286 return (rv);
287 }
288