com_pcmcia.c revision 1.38 1 /* $NetBSD: com_pcmcia.c,v 1.38 2004/08/07 04:55:25 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
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 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 1991 The Regents of the University of California.
41 * All rights reserved.
42 *
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
45 * are met:
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
54 *
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
66 *
67 * @(#)com.c 7.5 (Berkeley) 5/16/91
68 */
69
70 #include <sys/cdefs.h>
71 __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.38 2004/08/07 04:55:25 mycroft Exp $");
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/ioctl.h>
76 #include <sys/select.h>
77 #include <sys/tty.h>
78 #include <sys/proc.h>
79 #include <sys/user.h>
80 #include <sys/conf.h>
81 #include <sys/file.h>
82 #include <sys/uio.h>
83 #include <sys/kernel.h>
84 #include <sys/syslog.h>
85 #include <sys/device.h>
86
87 #include <machine/intr.h>
88 #include <machine/bus.h>
89
90 #include <dev/pcmcia/pcmciavar.h>
91 #include <dev/pcmcia/pcmciareg.h>
92 #include <dev/pcmcia/pcmciadevs.h>
93
94 #include <dev/ic/comreg.h>
95 #include <dev/ic/comvar.h>
96
97 #include <dev/isa/isareg.h>
98
99 struct com_dev {
100 char *cis1_info[4];
101 };
102
103 /* Devices that we need to match by CIS strings */
104 static struct com_dev com_devs[] = {
105 { PCMCIA_CIS_MEGAHERTZ_XJ2288 },
106 };
107
108
109 static int com_devs_size = sizeof(com_devs) / sizeof(com_devs[0]);
110 static struct com_dev *com_dev_match __P((struct pcmcia_card *));
111
112 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
113 void com_pcmcia_attach __P((struct device *, struct device *, void *));
114 int com_pcmcia_detach __P((struct device *, int));
115 void com_pcmcia_cleanup __P((void *));
116
117 int com_pcmcia_enable __P((struct com_softc *));
118 void com_pcmcia_disable __P((struct com_softc *));
119 int com_pcmcia_enable1 __P((struct com_softc *));
120 void com_pcmcia_disable1 __P((struct com_softc *));
121
122 struct com_pcmcia_softc {
123 struct com_softc sc_com; /* real "com" softc */
124
125 /* PCMCIA-specific goo */
126 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
127 int sc_io_window; /* our i/o window */
128 struct pcmcia_function *sc_pf; /* our PCMCIA function */
129 void *sc_ih; /* interrupt handler */
130 };
131
132 CFATTACH_DECL(com_pcmcia, sizeof(struct com_pcmcia_softc),
133 com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
134
135 /* Look for pcmcia cards with particular CIS strings */
136 static struct com_dev *
137 com_dev_match(card)
138 struct pcmcia_card *card;
139 {
140 int i, j;
141
142 for (i = 0; i < com_devs_size; i++) {
143 for (j = 0; j < 4; j++)
144 if (com_devs[i].cis1_info[j] &&
145 strcmp(com_devs[i].cis1_info[j],
146 card->cis1_info[j]) != 0)
147 break;
148 if (j == 4)
149 return &com_devs[i];
150 }
151
152 return NULL;
153 }
154
155
156 int
157 com_pcmcia_match(parent, match, aux)
158 struct device *parent;
159 struct cfdata *match;
160 void *aux;
161 {
162 int comportmask;
163 struct pcmcia_attach_args *pa = aux;
164 struct pcmcia_config_entry *cfe;
165
166 /* 1. Does it claim to be a serial device? */
167 if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
168 return 1;
169
170 /* 2. Does it have all four 'standard' port ranges? */
171 comportmask = 0;
172 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
173 switch (cfe->iospace[0].start) {
174 case IO_COM1:
175 comportmask |= 1;
176 break;
177 case IO_COM2:
178 comportmask |= 2;
179 break;
180 case IO_COM3:
181 comportmask |= 4;
182 break;
183 case IO_COM4:
184 comportmask |= 8;
185 break;
186 }
187 }
188
189 if (comportmask == 15)
190 return 1;
191
192 /* 3. Is this a card we know about? */
193 if (com_dev_match(pa->card) != NULL)
194 return 1;
195
196 return 0;
197 }
198
199 void
200 com_pcmcia_attach(parent, self, aux)
201 struct device *parent, *self;
202 void *aux;
203 {
204 struct com_pcmcia_softc *psc = (void *) self;
205 struct com_softc *sc = &psc->sc_com;
206 struct pcmcia_attach_args *pa = aux;
207 struct pcmcia_config_entry *cfe;
208 int autoalloc = 0;
209
210 aprint_normal("\n");
211
212 psc->sc_pf = pa->pf;
213
214 psc->sc_io_window = -1;
215
216 retry:
217 /* find a cfe we can use */
218
219 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
220 #if 0
221 /*
222 * Some modem cards (e.g. Xircom CM33) also have
223 * mem space. Don't bother with this check.
224 */
225 if (cfe->num_memspace != 0)
226 continue;
227 #endif
228
229 if (cfe->num_iospace != 1)
230 continue;
231
232 if (autoalloc == 0) {
233 /*
234 * cfe->iomask == 3 is our test for the "generic"
235 * config table entry, which we want to avoid on the
236 * first pass and use exclusively on the second pass.
237 */
238 if ((cfe->iomask != 3) &&
239 (cfe->iospace[0].start != 0)) {
240 if (!pcmcia_io_alloc(pa->pf,
241 cfe->iospace[0].start,
242 cfe->iospace[0].length,
243 cfe->iospace[0].length, &psc->sc_pcioh)) {
244 goto found;
245 }
246 }
247 } else {
248 if (cfe->iomask == 3) {
249 if (!pcmcia_io_alloc(pa->pf, 0,
250 cfe->iospace[0].length,
251 cfe->iospace[0].length, &psc->sc_pcioh)) {
252 goto found;
253 }
254 }
255 }
256 }
257 if (autoalloc == 0) {
258 autoalloc = 1;
259 goto retry;
260 } else if (!cfe) {
261 aprint_error("%s: can't allocate i/o space\n",
262 sc->sc_dev.dv_xname);
263 return;
264 }
265 found:
266 /* Enable the card. */
267 pcmcia_function_init(pa->pf, cfe);
268 if (com_pcmcia_enable1(sc))
269 aprint_error("%s: function enable failed\n",
270 sc->sc_dev.dv_xname);
271
272 sc->enabled = 1;
273
274 /* map in the io space */
275
276 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
277 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
278 &psc->sc_pcioh, &psc->sc_io_window)) {
279 aprint_error("%s: can't map i/o space\n", sc->sc_dev.dv_xname);
280 return;
281 }
282 sc->sc_iot = psc->sc_pcioh.iot;
283 sc->sc_ioh = psc->sc_pcioh.ioh;
284
285 sc->sc_iobase = -1;
286 sc->sc_frequency = COM_FREQ;
287
288 sc->enable = com_pcmcia_enable;
289 sc->disable = com_pcmcia_disable;
290
291 aprint_normal("%s", sc->sc_dev.dv_xname);
292
293 com_attach_subr(sc);
294
295 sc->enabled = 0;
296
297 com_pcmcia_disable1(sc);
298 }
299
300 int
301 com_pcmcia_detach(self, flags)
302 struct device *self;
303 int flags;
304 {
305 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) self;
306 int error;
307
308 /* Unmap our i/o window. */
309 if (psc->sc_io_window == -1) {
310 printf("%s: I/O window not allocated.\n",
311 psc->sc_com.sc_dev.dv_xname);
312 return 0;
313 }
314
315 if ((error = com_detach(self, flags)) != 0)
316 return error;
317
318 /* Unmap our i/o window. */
319 pcmcia_io_unmap(psc->sc_pf, psc->sc_io_window);
320
321 /* Free our i/o space. */
322 pcmcia_io_free(psc->sc_pf, &psc->sc_pcioh);
323
324 return 0;
325 }
326
327 int
328 com_pcmcia_enable(sc)
329 struct com_softc *sc;
330 {
331 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
332 struct pcmcia_function *pf = psc->sc_pf;
333 int error;
334
335 if ((error = com_pcmcia_enable1(sc)) != 0)
336 return error;
337
338 /* establish the interrupt. */
339 psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
340 if (psc->sc_ih == NULL) {
341 printf("%s: couldn't establish interrupt\n",
342 sc->sc_dev.dv_xname);
343 com_pcmcia_disable1(sc);
344 return 1;
345 }
346 return 0;
347 }
348
349 int
350 com_pcmcia_enable1(sc)
351 struct com_softc *sc;
352 {
353 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
354 struct pcmcia_function *pf = psc->sc_pf;
355 int ret;
356
357 if ((ret = pcmcia_function_enable(pf)) != 0)
358 return ret;
359
360 if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
361 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
362 (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
363 int reg;
364
365 /* turn off the ethernet-disable bit */
366
367 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
368 if (reg & 0x08) {
369 reg &= ~0x08;
370 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
371 }
372 }
373 return ret;
374 }
375
376 void
377 com_pcmcia_disable(sc)
378 struct com_softc *sc;
379 {
380 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
381
382 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
383 com_pcmcia_disable1(sc);
384 }
385
386 void
387 com_pcmcia_disable1(sc)
388 struct com_softc *sc;
389 {
390 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
391
392 pcmcia_function_disable(psc->sc_pf);
393 }
394