if_cs_pcmcia.c revision 1.4 1 1.4 thorpej /* $NetBSD: if_cs_pcmcia.c,v 1.4 2002/09/30 22:27:00 thorpej Exp $ */
2 1.1 yamt
3 1.1 yamt /*-
4 1.1 yamt * Copyright (c)2001 YAMAMOTO Takashi,
5 1.1 yamt * All rights reserved.
6 1.1 yamt *
7 1.1 yamt * Redistribution and use in source and binary forms, with or without
8 1.1 yamt * modification, are permitted provided that the following conditions
9 1.1 yamt * are met:
10 1.1 yamt * 1. Redistributions of source code must retain the above copyright
11 1.1 yamt * notice, this list of conditions and the following disclaimer.
12 1.1 yamt * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 yamt * notice, this list of conditions and the following disclaimer in the
14 1.1 yamt * documentation and/or other materials provided with the distribution.
15 1.1 yamt *
16 1.1 yamt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 yamt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 yamt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 yamt * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 yamt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 yamt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 yamt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 yamt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 yamt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 yamt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 yamt * SUCH DAMAGE.
27 1.1 yamt */
28 1.1 yamt
29 1.1 yamt #include <sys/cdefs.h>
30 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: if_cs_pcmcia.c,v 1.4 2002/09/30 22:27:00 thorpej Exp $");
31 1.1 yamt
32 1.1 yamt #include <sys/param.h>
33 1.1 yamt #include <sys/systm.h>
34 1.1 yamt #include <sys/device.h>
35 1.1 yamt #include <sys/socket.h>
36 1.1 yamt #include <sys/queue.h>
37 1.1 yamt
38 1.1 yamt #include "rnd.h"
39 1.1 yamt #if NRND > 0
40 1.1 yamt #include <sys/rnd.h>
41 1.1 yamt #endif
42 1.1 yamt
43 1.1 yamt #include <net/if.h>
44 1.1 yamt #include <net/if_ether.h>
45 1.1 yamt #include <net/if_media.h>
46 1.1 yamt
47 1.1 yamt #include <machine/bus.h>
48 1.1 yamt #include <machine/intr.h>
49 1.1 yamt
50 1.1 yamt #include <dev/pcmcia/pcmciareg.h>
51 1.1 yamt #include <dev/pcmcia/pcmciavar.h>
52 1.1 yamt #include <dev/pcmcia/pcmciadevs.h>
53 1.1 yamt
54 1.2 yamt #include <dev/ic/cs89x0reg.h>
55 1.2 yamt #include <dev/ic/cs89x0var.h>
56 1.1 yamt
57 1.1 yamt #define DEVNAME(sc) ((sc)->sc_dev.dv_xname)
58 1.1 yamt
59 1.1 yamt struct cs_pcmcia_softc;
60 1.1 yamt
61 1.1 yamt static int cs_pcmcia_match(struct device *, struct cfdata *, void *);
62 1.1 yamt static void cs_pcmcia_attach(struct device *, struct device *, void *);
63 1.1 yamt static int cs_pcmcia_detach(struct device *, int);
64 1.1 yamt static int cs_pcmcia_enable(struct cs_softc *);
65 1.1 yamt static void cs_pcmcia_disable(struct cs_softc *);
66 1.1 yamt
67 1.1 yamt struct cs_pcmcia_softc {
68 1.1 yamt struct cs_softc sc_cs; /* real "cs" softc */
69 1.1 yamt
70 1.1 yamt struct pcmcia_io_handle sc_pcioh;
71 1.1 yamt struct pcmcia_function *sc_pf;
72 1.1 yamt int sc_io_window;
73 1.1 yamt int sc_flags;
74 1.1 yamt };
75 1.1 yamt
76 1.1 yamt #define CS_PCMCIA_FLAGS_IO_ALLOCATED 1
77 1.1 yamt #define CS_PCMCIA_FLAGS_IO_MAPPED 2
78 1.1 yamt
79 1.4 thorpej CFATTACH_DECL(cs_pcmcia, sizeof(struct cs_pcmcia_softc),
80 1.4 thorpej cs_pcmcia_match, cs_pcmcia_attach, cs_pcmcia_detach, cs_activate)
81 1.1 yamt
82 1.1 yamt static int
83 1.1 yamt cs_pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
84 1.1 yamt {
85 1.1 yamt struct pcmcia_attach_args *pa = aux;
86 1.1 yamt
87 1.1 yamt if (pa->card->manufacturer == PCMCIA_VENDOR_IBM
88 1.1 yamt && pa->card->product == PCMCIA_PRODUCT_IBM_ETHERJET)
89 1.1 yamt return 1;
90 1.1 yamt
91 1.1 yamt return 0;
92 1.1 yamt }
93 1.1 yamt
94 1.1 yamt static void
95 1.1 yamt cs_pcmcia_attach(struct device *parent, struct device *self, void *aux)
96 1.1 yamt {
97 1.1 yamt struct cs_pcmcia_softc *psc = (void *)self;
98 1.1 yamt struct cs_softc *sc = (void *)&psc->sc_cs;
99 1.1 yamt struct pcmcia_attach_args *pa = aux;
100 1.1 yamt struct pcmcia_config_entry *cfe;
101 1.1 yamt struct pcmcia_function *pf;
102 1.1 yamt char devinfo[256];
103 1.1 yamt
104 1.1 yamt /* Print out what we are. */
105 1.1 yamt pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
106 1.1 yamt printf(": %s\n", devinfo);
107 1.1 yamt
108 1.1 yamt pf = psc->sc_pf = pa->pf;
109 1.1 yamt
110 1.1 yamt cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
111 1.1 yamt
112 1.1 yamt if (cfe->num_iospace != 1) {
113 1.1 yamt printf("%s: unexpected number of iospace(%d)\n",
114 1.1 yamt DEVNAME(sc), cfe->num_iospace);
115 1.1 yamt goto fail;
116 1.1 yamt }
117 1.1 yamt
118 1.1 yamt if (cfe->iospace[0].length < CS8900_IOSIZE) {
119 1.1 yamt printf("%s: unexpected iosize(%lu)\n",
120 1.1 yamt DEVNAME(sc), cfe->iospace[0].length);
121 1.1 yamt goto fail;
122 1.1 yamt }
123 1.1 yamt
124 1.1 yamt if (cfe->num_memspace != 0) {
125 1.1 yamt printf("%s: unexpected number of memspace(%d)\n",
126 1.1 yamt DEVNAME(sc), cfe->num_memspace);
127 1.1 yamt goto fail;
128 1.1 yamt }
129 1.1 yamt
130 1.1 yamt if (pcmcia_io_alloc(pf, cfe->iospace[0].start,
131 1.1 yamt cfe->iospace[0].length, cfe->iospace[0].length,
132 1.1 yamt &psc->sc_pcioh) != 0) {
133 1.1 yamt printf("%s: can't allocate i/o space %lx:%lx\n", DEVNAME(sc),
134 1.1 yamt cfe->iospace[0].start, cfe->iospace[0].length);
135 1.1 yamt goto fail;
136 1.1 yamt }
137 1.1 yamt psc->sc_flags |= CS_PCMCIA_FLAGS_IO_ALLOCATED;
138 1.1 yamt
139 1.1 yamt sc->sc_iot = psc->sc_pcioh.iot;
140 1.1 yamt sc->sc_ioh = psc->sc_pcioh.ioh;
141 1.1 yamt sc->sc_irq = -1;
142 1.1 yamt #define CS_PCMCIA_HACK_FOR_CARDBUS
143 1.1 yamt #ifdef CS_PCMCIA_HACK_FOR_CARDBUS
144 1.1 yamt /*
145 1.1 yamt * XXX is there a generic way to know if it's a cardbus or not?
146 1.1 yamt */
147 1.1 yamt sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK;
148 1.1 yamt #endif
149 1.1 yamt sc->sc_enable = cs_pcmcia_enable;
150 1.1 yamt sc->sc_disable = cs_pcmcia_disable;
151 1.1 yamt
152 1.1 yamt pcmcia_function_init(pa->pf, cfe);
153 1.1 yamt if (cs_pcmcia_enable(sc))
154 1.1 yamt goto fail;
155 1.1 yamt
156 1.1 yamt /* chip attach */
157 1.1 yamt if (cs_attach(sc, 0, 0, 0, 0))
158 1.1 yamt goto fail;
159 1.1 yamt
160 1.1 yamt cs_pcmcia_disable(sc);
161 1.1 yamt
162 1.1 yamt return;
163 1.1 yamt
164 1.1 yamt fail:
165 1.1 yamt cs_pcmcia_detach((struct device *)psc, 0);
166 1.1 yamt
167 1.1 yamt return;
168 1.1 yamt }
169 1.1 yamt
170 1.1 yamt static int
171 1.1 yamt cs_pcmcia_detach(struct device *self, int flags)
172 1.1 yamt {
173 1.1 yamt struct cs_pcmcia_softc *psc = (void *)self;
174 1.1 yamt struct cs_softc *sc = &psc->sc_cs;
175 1.1 yamt struct pcmcia_function *pf = psc->sc_pf;
176 1.1 yamt int rv;
177 1.1 yamt
178 1.1 yamt rv = cs_detach(sc);
179 1.1 yamt if (rv)
180 1.1 yamt return rv;
181 1.1 yamt
182 1.1 yamt cs_pcmcia_disable(sc);
183 1.1 yamt
184 1.1 yamt if (psc->sc_flags & CS_PCMCIA_FLAGS_IO_ALLOCATED) {
185 1.1 yamt pcmcia_io_free(pf, &psc->sc_pcioh);
186 1.1 yamt psc->sc_flags &= ~CS_PCMCIA_FLAGS_IO_ALLOCATED;
187 1.1 yamt }
188 1.1 yamt
189 1.1 yamt return 0;
190 1.1 yamt }
191 1.1 yamt
192 1.1 yamt static int
193 1.1 yamt cs_pcmcia_enable(struct cs_softc *sc)
194 1.1 yamt {
195 1.1 yamt struct cs_pcmcia_softc *psc = (void *)sc;
196 1.1 yamt struct pcmcia_function *pf = psc->sc_pf;
197 1.1 yamt
198 1.1 yamt if (pcmcia_io_map(pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
199 1.1 yamt &psc->sc_pcioh, &psc->sc_io_window) != 0) {
200 1.1 yamt printf("%s: can't map i/o space\n", DEVNAME(sc));
201 1.1 yamt goto fail;
202 1.1 yamt }
203 1.1 yamt psc->sc_flags |= CS_PCMCIA_FLAGS_IO_MAPPED;
204 1.1 yamt
205 1.1 yamt if (pcmcia_function_enable(pf)) {
206 1.1 yamt printf("%s: can't enable function\n", DEVNAME(sc));
207 1.1 yamt goto fail;
208 1.1 yamt }
209 1.1 yamt
210 1.1 yamt sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, cs_intr, sc);
211 1.1 yamt if (sc->sc_ih == 0) {
212 1.1 yamt printf("%s: can't establish interrupt\n", DEVNAME(sc));
213 1.1 yamt goto fail;
214 1.1 yamt }
215 1.1 yamt
216 1.1 yamt return 0;
217 1.1 yamt
218 1.1 yamt fail:
219 1.1 yamt return EIO;
220 1.1 yamt }
221 1.1 yamt
222 1.1 yamt static void
223 1.1 yamt cs_pcmcia_disable(struct cs_softc *sc)
224 1.1 yamt {
225 1.1 yamt struct cs_pcmcia_softc *psc = (void *)sc;
226 1.1 yamt struct pcmcia_function *pf = psc->sc_pf;
227 1.1 yamt
228 1.1 yamt if (sc->sc_ih != 0) {
229 1.1 yamt pcmcia_intr_disestablish(pf, sc->sc_ih);
230 1.1 yamt sc->sc_ih = 0;
231 1.1 yamt }
232 1.1 yamt
233 1.1 yamt pcmcia_function_disable(pf);
234 1.1 yamt
235 1.1 yamt if (psc->sc_flags & CS_PCMCIA_FLAGS_IO_MAPPED) {
236 1.1 yamt pcmcia_io_unmap(pf, psc->sc_io_window);
237 1.1 yamt psc->sc_flags &= ~CS_PCMCIA_FLAGS_IO_MAPPED;
238 1.1 yamt }
239 1.1 yamt }
240