com_pcmcia.c revision 1.13 1 /* $NetBSD: com_pcmcia.c,v 1.13 1998/08/15 17:47:17 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. 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 int manufacturer;
104 int product;
105 char *cis1_info[4];
106 };
107
108 static struct com_dev com_devs[] = {
109 { PCMCIA_STR_3COM_3C562,
110 PCMCIA_VENDOR_3COM, PCMCIA_PRODUCT_3COM_3C562,
111 PCMCIA_CIS_3COM_3C562 },
112
113 { PCMCIA_STR_MOTOROLA_POWER144,
114 PCMCIA_VENDOR_MOTOROLA, PCMCIA_PRODUCT_MOTOROLA_POWER144,
115 PCMCIA_CIS_MOTOROLA_POWER144 },
116
117 { PCMCIA_STR_IBM_HOME_AND_AWAY,
118 PCMCIA_VENDOR_IBM, PCMCIA_PRODUCT_IBM_HOME_AND_AWAY,
119 PCMCIA_CIS_IBM_HOME_AND_AWAY },
120
121 { PCMCIA_STR_MEGAHERTZ_XJ4288,
122 PCMCIA_VENDOR_MEGAHERTZ, PCMCIA_PRODUCT_MEGAHERTZ_XJ4288,
123 PCMCIA_CIS_MEGAHERTZ_XJ4288 },
124
125 { PCMCIA_STR_MEGAHERTZ_XJ4288,
126 PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_MEGAHERTZ_XJ4288,
127 PCMCIA_CIS_MEGAHERTZ_XJ4288 },
128
129 { PCMCIA_STR_USROBOTICS_WORLDPORT144,
130 PCMCIA_VENDOR_USROBOTICS, PCMCIA_PRODUCT_USROBOTICS_WORLDPORT144,
131 PCMCIA_CIS_USROBOTICS_WORLDPORT144 },
132
133 { PCMCIA_STR_SOCKET_PAGECARD,
134 PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_PAGECARD,
135 PCMCIA_CIS_SOCKET_PAGECARD },
136
137 { PCMCIA_STR_MOTOROLA_PM100C,
138 PCMCIA_VENDOR_MOTOROLA, PCMCIA_PRODUCT_MOTOROLA_PM100C,
139 PCMCIA_CIS_MOTOROLA_PM100C },
140
141 { PCMCIA_STR_SIMPLETECH_COMMUNICATOR288,
142 PCMCIA_VENDOR_SIMPLETECH, PCMCIA_PRODUCT_SIMPLETECH_COMMUNICATOR288,
143 PCMCIA_CIS_SIMPLETECH_COMMUNICATOR288 },
144 };
145
146
147 static int com_devs_size = sizeof(com_devs) / sizeof(com_devs[0]);
148 static struct com_dev *com_dev_match __P((struct pcmcia_card *));
149
150 int com_pcmcia_match __P((struct device *, struct cfdata *, void *));
151 void com_pcmcia_attach __P((struct device *, struct device *, void *));
152 void com_pcmcia_cleanup __P((void *));
153
154 int com_pcmcia_enable __P((struct com_softc *));
155 void com_pcmcia_disable __P((struct com_softc *));
156 int com_pcmcia_enable1 __P((struct com_softc *));
157 void com_pcmcia_disable1 __P((struct com_softc *));
158
159 struct com_pcmcia_softc {
160 struct com_softc sc_com; /* real "com" softc */
161
162 /* PCMCIA-specific goo */
163 struct pcmcia_io_handle sc_pcioh; /* PCMCIA i/o space info */
164 int sc_io_window; /* our i/o window */
165 struct pcmcia_function *sc_pf; /* our PCMCIA function */
166 void *sc_ih; /* interrupt handler */
167 };
168
169 struct cfattach com_pcmcia_ca = {
170 sizeof(struct com_pcmcia_softc), com_pcmcia_match, com_pcmcia_attach
171 };
172
173 static struct com_dev *
174 com_dev_match(card)
175 struct pcmcia_card *card;
176 {
177 int i;
178
179 /* 1. check our table */
180 for (i = 0; i < com_devs_size; i++) {
181 if (com_devs[i].manufacturer != PCMCIA_VENDOR_INVALID) {
182 /* manufacturer/product match */
183 if (com_devs[i].manufacturer == card->manufacturer &&
184 com_devs[i].product == card->product)
185 return &com_devs[i];
186 } else {
187 /* cis strings match */
188 int j;
189
190 if (com_devs[i].cis1_info[0] == NULL)
191 continue;
192 for (j = 0; j < 4; j++)
193 if (com_devs[i].cis1_info[j] &&
194 strcmp(com_devs[i].cis1_info[j],
195 card->cis1_info[j]))
196 break;
197 if (j == 4)
198 return &com_devs[i];
199 }
200 }
201
202 return NULL;
203 }
204
205
206 int
207 com_pcmcia_match(parent, match, aux)
208 struct device *parent;
209 struct cfdata *match;
210 void *aux;
211 {
212 int comportmask;
213 struct pcmcia_attach_args *pa = aux;
214 struct pcmcia_config_entry *cfe;
215
216 /* 1. Does it claim to be a serial device? */
217 if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
218 return 1;
219
220 /* 2. Does it have all four 'standard' port ranges? */
221 comportmask = 0;
222 for (cfe = pa->pf->cfe_head.sqh_first; cfe;
223 cfe = cfe->cfe_list.sqe_next) {
224 switch (cfe->iospace[0].start) {
225 case IO_COM1:
226 comportmask |= 1;
227 break;
228 case IO_COM2:
229 comportmask |= 2;
230 break;
231 case IO_COM3:
232 comportmask |= 4;
233 break;
234 case IO_COM4:
235 comportmask |= 8;
236 break;
237 }
238 }
239
240 if (comportmask == 15)
241 return 1;
242
243 /* 3. Is this a card we know about? */
244 if (com_dev_match(pa->card) != NULL)
245 return 1;
246
247 return 0;
248 }
249
250 void
251 com_pcmcia_attach(parent, self, aux)
252 struct device *parent, *self;
253 void *aux;
254 {
255 struct com_pcmcia_softc *psc = (void *) self;
256 struct com_softc *sc = &psc->sc_com;
257 struct pcmcia_attach_args *pa = aux;
258 struct pcmcia_config_entry *cfe;
259 struct com_dev *comdev;
260
261 psc->sc_pf = pa->pf;
262
263 /* find a cfe we can use */
264
265 for (cfe = pa->pf->cfe_head.sqh_first; cfe;
266 cfe = cfe->cfe_list.sqe_next) {
267 #if 0
268 /*
269 * Some modem cards (e.g. Xircom CM33) also have
270 * mem space. Don't bother with this check.
271 */
272 if (cfe->num_memspace != 0)
273 continue;
274 #endif
275
276 if (cfe->num_iospace != 1)
277 continue;
278
279 if (cfe->iomask == 3) {
280 if (pcmcia_io_alloc(pa->pf, 0, cfe->iospace[0].length,
281 cfe->iospace[0].length, &psc->sc_pcioh)) {
282 continue;
283 }
284 } else {
285 if (pcmcia_io_alloc(pa->pf, cfe->iospace[0].start,
286 cfe->iospace[0].length, 0, &psc->sc_pcioh)) {
287 continue;
288 }
289 }
290
291 break;
292 }
293
294 if (!cfe) {
295 printf(": can't allocate i/o space\n");
296 return;
297 }
298
299 sc->sc_iot = psc->sc_pcioh.iot;
300 sc->sc_ioh = psc->sc_pcioh.ioh;
301
302 /* Enable the card. */
303 pcmcia_function_init(pa->pf, cfe);
304 if (com_pcmcia_enable1(sc))
305 printf(": function enable failed\n");
306
307 sc->enabled = 1;
308
309 /* map in the io space */
310
311 if (pcmcia_io_map(pa->pf, ((cfe->flags & PCMCIA_CFE_IO16) ?
312 PCMCIA_WIDTH_IO16 : PCMCIA_WIDTH_IO8), 0, psc->sc_pcioh.size,
313 &psc->sc_pcioh, &psc->sc_io_window)) {
314 printf(": can't map i/o space\n");
315 return;
316 }
317
318 sc->sc_iobase = -1;
319 sc->sc_frequency = COM_FREQ;
320
321 sc->enable = com_pcmcia_enable;
322 sc->disable = com_pcmcia_disable;
323
324 if ((comdev = com_dev_match(pa->card)) != NULL)
325 printf(": %s", comdev->name);
326 else
327 printf(": Generic serial device");
328
329 com_attach_subr(sc);
330
331 sc->enabled = 0;
332
333 com_pcmcia_disable1(sc);
334 }
335
336 int
337 com_pcmcia_enable(sc)
338 struct com_softc *sc;
339 {
340 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
341 struct pcmcia_function *pf = psc->sc_pf;
342
343 /* establish the interrupt. */
344 psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
345 if (psc->sc_ih == NULL) {
346 printf("%s: couldn't establish interrupt\n",
347 sc->sc_dev.dv_xname);
348 return (1);
349 }
350 return com_pcmcia_enable1(sc);
351 }
352
353 int
354 com_pcmcia_enable1(sc)
355 struct com_softc *sc;
356 {
357 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
358 struct pcmcia_function *pf = psc->sc_pf;
359 int ret;
360
361 if ((ret = pcmcia_function_enable(pf)))
362 return(ret);
363
364 if (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) {
365 int reg;
366
367 /* turn off the ethernet-disable bit */
368
369 reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
370 if (reg & 0x08) {
371 reg &= ~0x08;
372 pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
373 }
374 }
375
376 return(ret);
377 }
378
379 void
380 com_pcmcia_disable(sc)
381 struct com_softc *sc;
382 {
383 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
384
385 com_pcmcia_disable1(sc);
386 pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
387 }
388
389 void
390 com_pcmcia_disable1(sc)
391 struct com_softc *sc;
392 {
393 struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
394
395 pcmcia_function_disable(psc->sc_pf);
396 }
397