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