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