xref: /netbsd-src/sys/dev/pcmcia/pcmcom.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: pcmcom.c,v 1.28 2006/10/12 01:31:50 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by RedBack Networks, Inc.
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  * Device driver for multi-port PCMCIA serial cards, written by
41  * Jason R. Thorpe for RedBack Networks, Inc.
42  *
43  * Most of these cards are simply multiple UARTs sharing a single interrupt
44  * line, and rely on the fact that PCMCIA level-triggered interrupts can
45  * be shared.  There are no special interrupt registers on them, as there
46  * are on most ISA multi-port serial cards.
47  *
48  * If there are other cards that have interrupt registers, they should not
49  * be glued into this driver.  Rather, separate drivers should be written
50  * for those devices, as we have in the ISA multi-port serial card case.
51  */
52 
53 #include <sys/cdefs.h>
54 __KERNEL_RCSID(0, "$NetBSD: pcmcom.c,v 1.28 2006/10/12 01:31:50 christos Exp $");
55 
56 #include <sys/param.h>
57 #include <sys/systm.h>
58 #include <sys/device.h>
59 #include <sys/termios.h>
60 #include <sys/malloc.h>
61 
62 #include <machine/bus.h>
63 #include <machine/intr.h>
64 
65 #include <dev/ic/comreg.h>
66 #include <dev/ic/comvar.h>
67 
68 #include <dev/pcmcia/pcmciavar.h>
69 #include <dev/pcmcia/pcmciareg.h>
70 #include <dev/pcmcia/pcmciadevs.h>
71 
72 #include "com.h"
73 #include "pcmcom.h"
74 
75 #include "locators.h"
76 
77 struct pcmcom_softc {
78 	struct device sc_dev;			/* generic device glue */
79 
80 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
81 	void *sc_ih;				/* interrupt handle */
82 	int sc_enabled_count;			/* enabled count */
83 
84 #define	NSLAVES			8
85 	struct device *sc_slaves[NSLAVES];	/* slave info */
86 	int sc_nslaves;				/* slave count */
87 
88 	int sc_state;
89 #define	PCMCOM_ATTACHED		3
90 };
91 
92 struct pcmcom_attach_args {
93 	bus_space_tag_t pca_iot;		/* I/O tag */
94 	bus_space_handle_t pca_ioh;		/* I/O handle */
95 	int pca_slave;				/* slave # */
96 };
97 
98 int	pcmcom_match(struct device *, struct cfdata *, void *);
99 int	pcmcom_validate_config(struct pcmcia_config_entry *);
100 void	pcmcom_attach(struct device *, struct device *, void *);
101 int	pcmcom_detach(struct device *, int);
102 int	pcmcom_activate(struct device *, enum devact);
103 
104 CFATTACH_DECL(pcmcom, sizeof(struct pcmcom_softc),
105     pcmcom_match, pcmcom_attach, pcmcom_detach, pcmcom_activate);
106 
107 const struct pcmcia_product pcmcom_products[] = {
108 	{ PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232,
109 	  PCMCIA_CIS_INVALID },
110 };
111 const size_t pcmcom_nproducts =
112     sizeof(pcmcom_products) / sizeof(pcmcom_products[0]);
113 
114 int	pcmcom_print(void *, const char *);
115 
116 int	pcmcom_enable(struct pcmcom_softc *);
117 void	pcmcom_disable(struct pcmcom_softc *);
118 
119 int	pcmcom_intr(void *);
120 
121 int
122 pcmcom_match(struct device *parent __unused, struct cfdata *cf __unused,
123     void *aux)
124 {
125 	struct pcmcia_attach_args *pa = aux;
126 
127 	if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
128 	    sizeof(pcmcom_products[0]), NULL))
129 		return (2);	/* beat com_pcmcia */
130 	return (0);
131 }
132 
133 int
134 pcmcom_validate_config(cfe)
135 	struct pcmcia_config_entry *cfe;
136 {
137 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
138 	    cfe->num_iospace < 1 || cfe->num_iospace > NSLAVES)
139 		return (EINVAL);
140 	return (0);
141 }
142 
143 void
144 pcmcom_attach(struct device *parent __unused, struct device *self, void *aux)
145 {
146 	struct pcmcom_softc *sc = (void *)self;
147 	struct pcmcia_attach_args *pa = aux;
148 	struct pcmcia_config_entry *cfe;
149 	int slave;
150 	int error;
151 	int locs[PCMCOMCF_NLOCS];
152 
153 	sc->sc_pf = pa->pf;
154 
155 	error = pcmcia_function_configure(pa->pf, pcmcom_validate_config);
156 	if (error) {
157 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
158 		    error);
159 		return;
160 	}
161 
162 	cfe = pa->pf->cfe;
163 	sc->sc_nslaves = cfe->num_iospace;
164 
165 	error = pcmcom_enable(sc);
166 	if (error)
167 		goto fail;
168 
169 	/* Attach the children. */
170 	for (slave = 0; slave < sc->sc_nslaves; slave++) {
171 		struct pcmcom_attach_args pca;
172 
173 		printf("%s: slave %d\n", self->dv_xname, slave);
174 
175 		pca.pca_iot = cfe->iospace[slave].handle.iot;
176 		pca.pca_ioh = cfe->iospace[slave].handle.ioh;
177 		pca.pca_slave = slave;
178 
179 		locs[PCMCOMCF_SLAVE] = slave;
180 
181 		sc->sc_slaves[slave] = config_found_sm_loc(&sc->sc_dev,
182 			"pcmcom", locs,
183 			&pca, pcmcom_print, config_stdsubmatch);
184 	}
185 
186 	pcmcom_disable(sc);
187 	sc->sc_state = PCMCOM_ATTACHED;
188 	return;
189 
190 fail:
191 	pcmcia_function_unconfigure(pa->pf);
192 }
193 
194 int
195 pcmcom_detach(self, flags)
196 	struct device *self;
197 	int flags;
198 {
199 	struct pcmcom_softc *sc = (void *)self;
200 	int slave, error;
201 
202 	if (sc->sc_state != PCMCOM_ATTACHED)
203 		return (0);
204 
205 	for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
206 		if (sc->sc_slaves[slave]) {
207 			/* Detach the child. */
208 			error = config_detach(sc->sc_slaves[slave], flags);
209 			if (error)
210 				return (error);
211 			sc->sc_slaves[slave] = 0;
212 		}
213 	}
214 
215 	pcmcia_function_unconfigure(sc->sc_pf);
216 
217 	return (0);
218 }
219 
220 int
221 pcmcom_activate(self, act)
222 	struct device *self;
223 	enum devact act;
224 {
225 	struct pcmcom_softc *sc = (void *)self;
226 	int slave, error = 0, s;
227 
228 	s = splserial();
229 	switch (act) {
230 	case DVACT_ACTIVATE:
231 		error = EOPNOTSUPP;
232 		break;
233 
234 	case DVACT_DEACTIVATE:
235 		for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
236 			if (sc->sc_slaves[slave]) {
237 				/*
238 				 * Deactivate the child.  Doing so will cause
239 				 * our own enabled count to drop to 0, once all
240 				 * children are deactivated.
241 				 */
242 				error = config_deactivate(sc->sc_slaves[slave]);
243 				if (error)
244 					break;
245 			}
246 		}
247 		break;
248 	}
249 	splx(s);
250 	return (error);
251 }
252 
253 int
254 pcmcom_print(aux, pnp)
255 	void *aux;
256 	const char *pnp;
257 {
258 	struct pcmcom_attach_args *pca = aux;
259 
260 	/* only com's can attach to pcmcom's; easy... */
261 	if (pnp)
262 		aprint_normal("com at %s", pnp);
263 
264 	aprint_normal(" slave %d", pca->pca_slave);
265 
266 	return (UNCONF);
267 }
268 
269 int
270 pcmcom_intr(arg)
271 	void *arg;
272 {
273 #if NCOM > 0
274 	struct pcmcom_softc *sc = arg;
275 	int i, rval = 0;
276 
277 	if (sc->sc_enabled_count == 0)
278 		return (0);
279 
280 	for (i = 0; i < sc->sc_nslaves; i++) {
281 		if (sc->sc_slaves[i])
282 			rval |= comintr(sc->sc_slaves[i]);
283 	}
284 
285 	return (rval);
286 #else
287 	return (0);
288 #endif /* NCOM > 0 */
289 }
290 
291 int
292 pcmcom_enable(sc)
293 	struct pcmcom_softc *sc;
294 {
295 	int error;
296 
297 	if (sc->sc_enabled_count++ != 0)
298 		return (0);
299 
300 	/* Establish the interrupt. */
301 	sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
302 	    pcmcom_intr, sc);
303 	if (!sc->sc_ih)
304 		return (EIO);
305 
306 	error = pcmcia_function_enable(sc->sc_pf);
307 	if (error) {
308 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
309 		sc->sc_ih = 0;
310 	}
311 
312 	return (error);
313 }
314 
315 void
316 pcmcom_disable(sc)
317 	struct pcmcom_softc *sc;
318 {
319 
320 	if (--sc->sc_enabled_count != 0)
321 		return;
322 
323 	pcmcia_function_disable(sc->sc_pf);
324 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
325 	sc->sc_ih = 0;
326 }
327 
328 /****** Here begins the com attachment code. ******/
329 
330 #if NCOM_PCMCOM > 0
331 int	com_pcmcom_match(struct device *, struct cfdata *, void *);
332 void	com_pcmcom_attach(struct device *, struct device *, void *);
333 
334 /* No pcmcom-specific goo in the softc; it's all in the parent. */
335 CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
336     com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);
337 
338 int	com_pcmcom_enable(struct com_softc *);
339 void	com_pcmcom_disable(struct com_softc *);
340 
341 int
342 com_pcmcom_match(struct device *parent __unused, struct cfdata *cf __unused,
343     void *aux __unused)
344 {
345 
346 	/* Device is always present. */
347 	return (1);
348 }
349 
350 void
351 com_pcmcom_attach(struct device *parent __unused, struct device *self,
352     void *aux)
353 {
354 	struct com_softc *sc = (struct com_softc *)self;
355 	struct pcmcom_attach_args *pca = aux;
356 
357 	COM_INIT_REGS(sc->sc_regs, pca->pca_iot, pca->pca_ioh, -1);
358 	sc->enabled = 1;
359 
360 	sc->sc_frequency = COM_FREQ;
361 
362 	sc->enable = com_pcmcom_enable;
363 	sc->disable = com_pcmcom_disable;
364 
365 	com_attach_subr(sc);
366 
367 	sc->enabled = 0;
368 }
369 
370 int
371 com_pcmcom_enable(sc)
372 	struct com_softc *sc;
373 {
374 
375 	return (pcmcom_enable((struct pcmcom_softc *)device_parent(&sc->sc_dev)));
376 }
377 
378 void
379 com_pcmcom_disable(sc)
380 	struct com_softc *sc;
381 {
382 
383 	pcmcom_disable((struct pcmcom_softc *)device_parent(&sc->sc_dev));
384 }
385 #endif /* NCOM_PCMCOM > 0 */
386