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