if_fxp_cardbus.c revision 1.4 1 /* $NetBSD: if_fxp_cardbus.c,v 1.4 1999/11/01 09:59:23 haya 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
8 * The NetBSD Foundation 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 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * 3. Neither the name of The NetBSD Foundation nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38
39 #include "opt_inet.h"
40 #include "opt_ns.h"
41 #include "bpfilter.h"
42 #include "rnd.h"
43
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/mbuf.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/ioctl.h>
51 #include <sys/errno.h>
52 #include <sys/device.h>
53
54 #if NRND > 0
55 #include <sys/rnd.h>
56 #endif
57
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_ether.h>
62
63 #if NBPFILTER > 0
64 #include <net/bpf.h>
65 #endif
66
67 #ifdef INET
68 #include <netinet/in.h>
69 #include <netinet/if_inarp.h>
70 #endif
71
72 #ifdef NS
73 #include <netns/ns.h>
74 #include <netns/ns_if.h>
75 #endif
76
77 #include <machine/bus.h>
78 #include <machine/intr.h>
79
80 #include <dev/mii/miivar.h>
81
82 #include <dev/ic/i82557reg.h>
83 #include <dev/ic/i82557var.h>
84
85 #include <dev/pci/pcivar.h>
86 #include <dev/pci/pcireg.h>
87 #include <dev/pci/pcidevs.h>
88
89 #include <dev/cardbus/cardbusvar.h>
90 #include <dev/cardbus/cardbusdevs.h>
91
92 static int fxp_cardbus_match __P((struct device *, struct cfdata *, void *));
93 static void fxp_cardbus_attach __P((struct device *, struct device *, void *));
94 static void fxp_cardbus_setup __P((struct fxp_softc *sc));
95 static int fxp_cardbus_enable __P((struct fxp_softc *sc));
96 static void fxp_cardbus_disable __P((struct fxp_softc *sc));
97
98 struct fxp_cardbus_softc {
99 struct fxp_softc sc;
100 cardbus_devfunc_t ct;
101 pcireg_t base0_reg;
102 pcireg_t base1_reg;
103 bus_size_t size;
104 };
105
106 struct cfattach fxp_cardbus_ca = {
107 sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach
108 };
109
110 #ifdef CBB_DEBUG
111 #define DPRINTF(X) printf X
112 #else
113 #define DPRINTF(X)
114 #endif
115
116 static int
117 fxp_cardbus_match(parent, match, aux)
118 struct device *parent;
119 struct cfdata *match;
120 void *aux;
121 {
122 /* should check CIS */
123 struct cardbus_attach_args *ca = aux;
124
125 if (CARDBUS_VENDOR(ca->ca_id) != CARDBUS_VENDOR_INTEL)
126 return (0);
127
128 switch (CARDBUS_PRODUCT(ca->ca_id)) {
129 case CARDBUS_PRODUCT_INTEL_82557:
130 return (1);
131 }
132
133 return (0);
134 }
135
136 static void
137 fxp_cardbus_attach(parent, self, aux)
138 struct device *parent, *self;
139 void *aux;
140 {
141 struct fxp_softc *sc = (struct fxp_softc*)self;
142 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)self;
143 struct cardbus_attach_args *ca = aux;
144 bus_space_tag_t iot, memt;
145 bus_space_handle_t ioh, memh;
146
147 bus_addr_t adr;
148 bus_size_t size;
149
150 csc->ct = ca->ca_ct;
151
152 /*
153 * Map control/status registers.
154 */
155 if(Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
156 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
157 0, &memt, &memh, &adr, &size) == 0) {
158 csc->base0_reg = adr;
159 sc->sc_st = memt;
160 sc->sc_sh = memh;
161 csc->size = size;
162 } else if(Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
163 PCI_MAPREG_TYPE_IO,
164 0, &iot, &ioh, &adr, &size) == 0) {
165 csc->base1_reg = adr | 1;
166 sc->sc_st = iot;
167 sc->sc_sh = ioh;
168 csc->size = size;
169 } else
170 panic("%s: failed to allocate mem and io space", __FUNCTION__);
171
172
173 sc->sc_dmat = ca->ca_dmat;
174 sc->sc_enable = fxp_cardbus_enable;
175 sc->sc_disable = fxp_cardbus_disable;
176 sc->sc_enabled = 0;
177
178 fxp_enable(sc);
179 fxp_attach(sc);
180 fxp_disable(sc);
181 }
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 = (struct cardbus_softc *)sc->sc_dev.dv_parent;
189 cardbus_chipset_tag_t cc = psc->sc_cc;
190 cardbus_function_tag_t cf = psc->sc_cf;
191 pcireg_t command;
192
193 cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
194 csc->ct->ct_dev, csc->ct->ct_func);
195
196 command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
197 if(csc->base0_reg) {
198 Cardbus_conf_write(csc->ct, tag,
199 CARDBUS_BASE0_REG, csc->base0_reg);
200 (cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
201 command |= CARDBUS_COMMAND_MEM_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE;
202
203 } else if(csc->base1_reg) {
204 Cardbus_conf_write(csc->ct, tag,
205 CARDBUS_BASE1_REG, csc->base1_reg);
206 (cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
207 command |= (CARDBUS_COMMAND_IO_ENABLE | CARDBUS_COMMAND_MASTER_ENABLE);
208 }
209 /* enable the card */
210 Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
211 }
212
213 static int
214 fxp_cardbus_enable(struct fxp_softc *sc)
215 {
216 struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc*)sc;
217 struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
218 cardbus_chipset_tag_t cc = psc->sc_cc;
219 cardbus_function_tag_t cf = psc->sc_cf;
220
221 Cardbus_function_enable(csc->ct);
222
223 fxp_cardbus_setup(sc);
224
225 /* Map and establish the interrupt. */
226
227 sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
228 fxp_intr, sc);
229 if (NULL == sc->sc_ih) {
230 printf("%s: couldn't establish interrupt\n", sc->sc_dev.dv_xname);
231 return 1;
232 }
233
234 printf("%s: interrupting at %d\n", sc->sc_dev.dv_xname, psc->sc_intrline);
235
236 return 0;
237 }
238
239 static void
240 fxp_cardbus_disable(struct fxp_softc *sc)
241 {
242 struct cardbus_softc *psc = (struct cardbus_softc *)sc->sc_dev.dv_parent;
243 cardbus_chipset_tag_t cc = psc->sc_cc;
244 cardbus_function_tag_t cf = psc->sc_cf;
245
246 cardbus_intr_disestablish(cc, cf, sc->sc_ih); /* remove intr handler */
247
248 Cardbus_function_disable(((struct fxp_cardbus_softc*)sc)->ct);
249 }
250