xref: /netbsd-src/sys/dev/pcmcia/com_pcmcia.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /*	$NetBSD: com_pcmcia.c,v 1.59 2008/08/27 05:39:01 christos Exp $	 */
2 
3 /*-
4  * Copyright (c) 1998, 2004 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1991 The Regents of the University of California.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. Neither the name of the University nor the names of its contributors
45  *    may be used to endorse or promote products derived from this software
46  *    without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  *
60  *	@(#)com.c	7.5 (Berkeley) 5/16/91
61  */
62 
63 #include <sys/cdefs.h>
64 __KERNEL_RCSID(0, "$NetBSD: com_pcmcia.c,v 1.59 2008/08/27 05:39:01 christos Exp $");
65 
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/ioctl.h>
69 #include <sys/select.h>
70 #include <sys/tty.h>
71 #include <sys/proc.h>
72 #include <sys/user.h>
73 #include <sys/conf.h>
74 #include <sys/file.h>
75 #include <sys/uio.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 #include <sys/device.h>
79 
80 #include <sys/intr.h>
81 #include <sys/bus.h>
82 
83 #include <dev/pcmcia/pcmciavar.h>
84 #include <dev/pcmcia/pcmciareg.h>
85 #include <dev/pcmcia/pcmciadevs.h>
86 
87 #include <dev/ic/comreg.h>
88 #include <dev/ic/comvar.h>
89 
90 #include <dev/isa/isareg.h>
91 
92 int com_pcmcia_match(device_t, cfdata_t , void *);
93 int com_pcmcia_validate_config(struct pcmcia_config_entry *);
94 void com_pcmcia_attach(device_t, device_t, void *);
95 int com_pcmcia_detach(device_t, int);
96 
97 int com_pcmcia_enable(struct com_softc *);
98 void com_pcmcia_disable(struct com_softc *);
99 
100 struct com_pcmcia_softc {
101 	struct com_softc sc_com;		/* real "com" softc */
102 
103 	/* PCMCIA-specific goo */
104 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
105 	void *sc_ih;				/* interrupt handler */
106 	int sc_attached;
107 };
108 
109 CFATTACH_DECL_NEW(com_pcmcia, sizeof(struct com_pcmcia_softc),
110     com_pcmcia_match, com_pcmcia_attach, com_pcmcia_detach, com_activate);
111 
112 static const struct pcmcia_product com_pcmcia_products[] = {
113 	{ PCMCIA_VENDOR_INVALID, PCMCIA_PRODUCT_INVALID,
114 	  PCMCIA_CIS_MEGAHERTZ_XJ2288 },
115 	{ PCMCIA_VENDOR_TDK, PCMCIA_PRODUCT_TDK_BLUETOOTH_PCCARD,
116 	  PCMCIA_CIS_INVALID },
117 };
118 static const size_t com_pcmcia_nproducts =
119     sizeof(com_pcmcia_products) / sizeof(com_pcmcia_products[0]);
120 
121 int
122 com_pcmcia_match(device_t parent, cfdata_t match, void *aux)
123 {
124 	int comportmask;
125 	struct pcmcia_attach_args *pa = aux;
126 	struct pcmcia_config_entry *cfe;
127 
128 	/* 1. Does it claim to be a serial device? */
129 	if (pa->pf->function == PCMCIA_FUNCTION_SERIAL)
130 		return 1;
131 
132 	/* 2. Does it have all four 'standard' port ranges? */
133 	comportmask = 0;
134 	SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
135 		switch (cfe->iospace[0].start) {
136 		case IO_COM1:
137 			comportmask |= 1;
138 			break;
139 		case IO_COM2:
140 			comportmask |= 2;
141 			break;
142 		case IO_COM3:
143 			comportmask |= 4;
144 			break;
145 		case IO_COM4:
146 			comportmask |= 8;
147 			break;
148 		}
149 	}
150 
151 	if (comportmask == 15)
152 		return 1;
153 
154 	/* 3. Is this a card we know about? */
155 	if (pcmcia_product_lookup(pa, com_pcmcia_products, com_pcmcia_nproducts,
156 	    sizeof(com_pcmcia_products[0]), NULL))
157 		return 1;
158 
159 	return 0;
160 }
161 
162 int
163 com_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
164 {
165 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
166 	    cfe->num_iospace != 1)
167 		return (EINVAL);
168 	/* Some cards have a memory space, but we don't use it. */
169 	cfe->num_memspace = 0;
170 	return (0);
171 }
172 
173 void
174 com_pcmcia_attach(device_t parent, device_t self, void *aux)
175 {
176 	struct com_pcmcia_softc *psc = device_private(self);
177 	struct com_softc *sc = &psc->sc_com;
178 	struct pcmcia_attach_args *pa = aux;
179 	struct pcmcia_config_entry *cfe;
180 	int error;
181 
182 	sc->sc_dev = self;
183 	psc->sc_pf = pa->pf;
184 
185 	error = pcmcia_function_configure(pa->pf, com_pcmcia_validate_config);
186 	if (error) {
187 		aprint_error_dev(self, "configure failed, error=%d\n", error);
188 		return;
189 	}
190 
191 	cfe = pa->pf->cfe;
192 	COM_INIT_REGS(sc->sc_regs, cfe->iospace[0].handle.iot,
193 	    cfe->iospace[0].handle.ioh, -1);
194 
195 	error = com_pcmcia_enable(sc);
196 	if (error)
197 		goto fail;
198 
199 	sc->enabled = 1;
200 
201 	sc->sc_frequency = COM_FREQ;
202 
203 	sc->enable = com_pcmcia_enable;
204 	sc->disable = com_pcmcia_disable;
205 
206 	aprint_normal("%s", device_xname(self));
207 
208 	com_attach_subr(sc);
209 
210 	if (!pmf_device_register1(self, com_suspend, com_resume,
211 	    com_cleanup))
212 		aprint_error_dev(self, "couldn't establish power handler\n");
213 
214 	sc->enabled = 0;
215 
216 	psc->sc_attached = 1;
217 	com_pcmcia_disable(sc);
218 	return;
219 
220 fail:
221 	pcmcia_function_unconfigure(pa->pf);
222 }
223 
224 int
225 com_pcmcia_detach(device_t self, int flags)
226 {
227 	struct com_pcmcia_softc *psc = device_private(self);
228 	int error;
229 
230 	if (!psc->sc_attached)
231 		return (0);
232 
233 	pmf_device_deregister(self);
234 
235 	if ((error = com_detach(self, flags)) != 0)
236 		return error;
237 
238 	pcmcia_function_unconfigure(psc->sc_pf);
239 	return (0);
240 }
241 
242 int
243 com_pcmcia_enable(struct com_softc *sc)
244 {
245 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
246 	struct pcmcia_function *pf = psc->sc_pf;
247 	int error;
248 
249 	/* establish the interrupt. */
250 	psc->sc_ih = pcmcia_intr_establish(pf, IPL_SERIAL, comintr, sc);
251 	if (psc->sc_ih == NULL) {
252 		aprint_error_dev(sc->sc_dev, "couldn't establish interrupt\n");
253 		return (1);
254 	}
255 
256 	if ((error = pcmcia_function_enable(pf)) != 0) {
257 		pcmcia_intr_disestablish(pf, psc->sc_ih);
258 		psc->sc_ih = 0;
259 		return (error);
260 	}
261 
262 	if ((psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3C562) ||
263 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556) ||
264 	    (psc->sc_pf->sc->card.product == PCMCIA_PRODUCT_3COM_3CXEM556INT)) {
265 		int reg;
266 
267 		/* turn off the ethernet-disable bit */
268 		reg = pcmcia_ccr_read(pf, PCMCIA_CCR_OPTION);
269 		if (reg & 0x08) {
270 			reg &= ~0x08;
271 			pcmcia_ccr_write(pf, PCMCIA_CCR_OPTION, reg);
272 		}
273 	}
274 
275 	return (0);
276 }
277 
278 void
279 com_pcmcia_disable(struct com_softc *sc)
280 {
281 	struct com_pcmcia_softc *psc = (struct com_pcmcia_softc *) sc;
282 
283 	pcmcia_function_disable(psc->sc_pf);
284 	pcmcia_intr_disestablish(psc->sc_pf, psc->sc_ih);
285 	psc->sc_ih = 0;
286 }
287