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