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