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