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