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