pcmcom.c revision 1.17 1 /* $NetBSD: pcmcom.c,v 1.17 2004/08/10 18:39:08 mycroft 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.17 2004/08/10 18:39:08 mycroft 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_slave_info {
78 struct pcmcia_io_handle psi_pcioh; /* PCMCIA i/o space info */
79 int psi_io_window; /* our i/o window */
80 struct device *psi_child; /* child's device */
81 };
82
83 struct pcmcom_softc {
84 struct device sc_dev; /* generic device glue */
85
86 struct pcmcia_function *sc_pf; /* our PCMCIA function */
87 void *sc_ih; /* interrupt handle */
88 int sc_enabled_count; /* enabled count */
89
90 struct pcmcom_slave_info *sc_slaves; /* slave info */
91 int sc_nslaves; /* slave count */
92 };
93
94 struct pcmcom_attach_args {
95 bus_space_tag_t pca_iot; /* I/O tag */
96 bus_space_handle_t pca_ioh; /* I/O handle */
97 int pca_slave; /* slave # */
98 };
99
100 int pcmcom_match __P((struct device *, struct cfdata *, void *));
101 void pcmcom_attach __P((struct device *, struct device *, void *));
102 int pcmcom_detach __P((struct device *, int));
103 int pcmcom_activate __P((struct device *, enum devact));
104
105 CFATTACH_DECL(pcmcom, sizeof(struct pcmcom_softc),
106 pcmcom_match, pcmcom_attach, pcmcom_detach, pcmcom_activate);
107
108 const struct pcmcom_product {
109 struct pcmcia_product pp_product;
110 int pp_nslaves; /* number of slaves */
111 } pcmcom_products[] = {
112 { { PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232,
113 PCMCIA_CIS_INVALID },
114 2 },
115 };
116 const size_t pcmcom_nproducts =
117 sizeof(pcmcom_products) / sizeof(pcmcom_products[0]);
118
119 int pcmcom_print __P((void *, const char *));
120 int pcmcom_submatch __P((struct device *, struct cfdata *, void *));
121
122 int pcmcom_check_cfe __P((struct pcmcom_softc *,
123 struct pcmcia_config_entry *));
124 void pcmcom_attach_slave __P((struct pcmcom_softc *, int));
125
126 int pcmcom_enable __P((struct pcmcom_softc *));
127 void pcmcom_disable __P((struct pcmcom_softc *));
128
129 int pcmcom_intr __P((void *));
130
131 int
132 pcmcom_match(parent, cf, aux)
133 struct device *parent;
134 struct cfdata *cf;
135 void *aux;
136 {
137 struct pcmcia_attach_args *pa = aux;
138
139 if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
140 sizeof(pcmcom_products[0]), NULL))
141 return (2); /* beat com_pcmcia */
142 return (0);
143 }
144
145 void
146 pcmcom_attach(parent, self, aux)
147 struct device *parent, *self;
148 void *aux;
149 {
150 struct pcmcom_softc *sc = (struct pcmcom_softc *)self;
151 struct pcmcia_attach_args *pa = aux;
152 struct pcmcia_config_entry *cfe;
153 const struct pcmcom_product *pp;
154 size_t size;
155 int i;
156
157 printf("\n");
158 sc->sc_pf = pa->pf;
159
160 pp = pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
161 sizeof(pcmcom_products[0]), NULL);
162 if (!pp) {
163 printf("\n");
164 panic("pcmcom_attach: impossible");
165 }
166
167 /* Allocate the slave info. */
168 sc->sc_nslaves = pp->pp_nslaves;
169 size = sizeof(struct pcmcom_slave_info) * sc->sc_nslaves;
170 sc->sc_slaves = malloc(size, M_DEVBUF, M_NOWAIT|M_ZERO);
171 if (sc->sc_slaves == NULL) {
172 printf("%s: unable to allocate slave info\n",
173 sc->sc_dev.dv_xname);
174 return;
175 }
176
177 /*
178 * The address decoders on these cards are stupid. They decode
179 * (usually) 10 bits of address, so you need to allocate the
180 * regions they request in order for the card to differentiate
181 * between serial ports.
182 */
183 SIMPLEQ_FOREACH(cfe, &pa->pf->cfe_head, cfe_list) {
184 if (pcmcom_check_cfe(sc, cfe)) {
185 /* Found one! */
186 break;
187 }
188 }
189 if (cfe == NULL) {
190 printf("%s: unable to find a suitable config table entry\n",
191 sc->sc_dev.dv_xname);
192 return;
193 }
194
195 /* Enable the card. */
196 pcmcia_function_init(pa->pf, cfe);
197 if (pcmcia_function_enable(sc->sc_pf)) {
198 printf("%s: function enable failed\n", sc->sc_dev.dv_xname);
199 return;
200 }
201
202 sc->sc_enabled_count = 1;
203
204 /* Attach the children. */
205 for (i = 0; i < sc->sc_nslaves; i++)
206 pcmcom_attach_slave(sc, i);
207
208 sc->sc_enabled_count = 0;
209 pcmcia_function_disable(sc->sc_pf);
210 }
211
212 int
213 pcmcom_check_cfe(sc, cfe)
214 struct pcmcom_softc *sc;
215 struct pcmcia_config_entry *cfe;
216 {
217 struct pcmcom_slave_info *psi;
218 int slave;
219
220 /* We need to have the same number of I/O spaces as we do slaves. */
221 if (cfe->num_iospace != sc->sc_nslaves)
222 return (0);
223
224 for (slave = 0; slave < sc->sc_nslaves; slave++) {
225 psi = &sc->sc_slaves[slave];
226 if (pcmcia_io_alloc(sc->sc_pf,
227 cfe->iospace[slave].start,
228 cfe->iospace[slave].length,
229 cfe->iospace[slave].length,
230 &psi->psi_pcioh))
231 goto release;
232 }
233
234 /* If we got here, we were able to allocate space for all slaves! */
235 return (1);
236
237 release:
238 /* Release the i/o spaces we've allocated. */
239 for (slave--; slave >= 0; slave--) {
240 psi = &sc->sc_slaves[slave];
241 pcmcia_io_free(sc->sc_pf, &psi->psi_pcioh);
242 }
243 return (0);
244 }
245
246 void
247 pcmcom_attach_slave(sc, slave)
248 struct pcmcom_softc *sc;
249 int slave;
250 {
251 struct pcmcom_slave_info *psi = &sc->sc_slaves[slave];
252 struct pcmcom_attach_args pca;
253
254 printf("%s: slave %d", sc->sc_dev.dv_xname, slave);
255
256 if (pcmcia_io_map(sc->sc_pf, PCMCIA_WIDTH_IO8, &psi->psi_pcioh,
257 &psi->psi_io_window)) {
258 printf(": can't map i/o space\n");
259 return;
260 }
261
262 printf("\n");
263
264 pca.pca_iot = psi->psi_pcioh.iot;
265 pca.pca_ioh = psi->psi_pcioh.ioh;
266 pca.pca_slave = slave;
267
268 psi->psi_child = config_found_sm(&sc->sc_dev, &pca, pcmcom_print,
269 pcmcom_submatch);
270 }
271
272 int
273 pcmcom_detach(self, flags)
274 struct device *self;
275 int flags;
276 {
277 struct pcmcom_softc *sc = (struct pcmcom_softc *)self;
278 struct pcmcom_slave_info *psi;
279 int slave, error;
280
281 for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
282 psi = &sc->sc_slaves[slave];
283 if (psi->psi_child == NULL)
284 continue;
285
286 /* Detach the child. */
287 if ((error = config_detach(psi->psi_child, flags)) != 0)
288 return (error);
289 psi->psi_child = NULL;
290
291 /* Unmap the i/o window. */
292 pcmcia_io_unmap(sc->sc_pf, psi->psi_io_window);
293
294 /* Free the i/o space. */
295 pcmcia_io_free(sc->sc_pf, &psi->psi_pcioh);
296 }
297 return (0);
298 }
299
300 int
301 pcmcom_activate(self, act)
302 struct device *self;
303 enum devact act;
304 {
305 struct pcmcom_softc *sc = (struct pcmcom_softc *)self;
306 struct pcmcom_slave_info *psi;
307 int slave, error = 0, s;
308
309 s = splserial();
310 switch (act) {
311 case DVACT_ACTIVATE:
312 error = EOPNOTSUPP;
313 break;
314
315 case DVACT_DEACTIVATE:
316 for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
317 psi = &sc->sc_slaves[slave];
318 if (psi->psi_child == NULL)
319 continue;
320
321 /*
322 * Deactivate the child. Doing so will cause our
323 * own enabled count to drop to 0, once all children
324 * are deactivated.
325 */
326 if ((error = config_deactivate(psi->psi_child)) != 0)
327 break;
328 }
329 break;
330 }
331 splx(s);
332 return (error);
333 }
334
335 int
336 pcmcom_print(aux, pnp)
337 void *aux;
338 const char *pnp;
339 {
340 struct pcmcom_attach_args *pca = aux;
341
342 /* only com's can attach to pcmcom's; easy... */
343 if (pnp)
344 aprint_normal("com at %s", pnp);
345
346 aprint_normal(" slave %d", pca->pca_slave);
347
348 return (UNCONF);
349 }
350
351 int
352 pcmcom_submatch(parent, cf, aux)
353 struct device *parent;
354 struct cfdata *cf;
355 void *aux;
356 {
357 struct pcmcom_attach_args *pca = aux;
358
359 if (cf->cf_loc[PCMCOMCF_SLAVE] != pca->pca_slave &&
360 cf->cf_loc[PCMCOMCF_SLAVE] != PCMCOMCF_SLAVE_DEFAULT)
361 return (0);
362
363 return (config_match(parent, cf, aux));
364 }
365
366 int
367 pcmcom_intr(arg)
368 void *arg;
369 {
370 #if NCOM > 0
371 struct pcmcom_softc *sc = arg;
372 int i, rval = 0;
373
374 if (sc->sc_enabled_count == 0)
375 return (0);
376
377 for (i = 0; i < sc->sc_nslaves; i++) {
378 if (sc->sc_slaves[i].psi_child != NULL)
379 rval |= comintr(sc->sc_slaves[i].psi_child);
380 }
381
382 return (rval);
383 #else
384 return (0);
385 #endif /* NCOM > 0 */
386 }
387
388 int
389 pcmcom_enable(sc)
390 struct pcmcom_softc *sc;
391 {
392 int error;
393
394 if (sc->sc_enabled_count++ != 0)
395 return (0);
396
397 /* Establish the interrupt. */
398 sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
399 pcmcom_intr, sc);
400 if (sc->sc_ih == NULL) {
401 printf("%s: couldn't establish interrupt\n",
402 sc->sc_dev.dv_xname);
403 return (1);
404 }
405
406 error = pcmcia_function_enable(sc->sc_pf);
407 if (error)
408 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
409
410 return (error);
411 }
412
413 void
414 pcmcom_disable(sc)
415 struct pcmcom_softc *sc;
416 {
417
418 if (--sc->sc_enabled_count != 0)
419 return;
420
421 pcmcia_function_disable(sc->sc_pf);
422 pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
423 }
424
425 /****** Here begins the com attachment code. ******/
426
427 #if NCOM_PCMCOM > 0
428 int com_pcmcom_match __P((struct device *, struct cfdata *, void *));
429 void com_pcmcom_attach __P((struct device *, struct device *, void *));
430
431 /* No pcmcom-specific goo in the softc; it's all in the parent. */
432 CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
433 com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);
434
435 int com_pcmcom_enable __P((struct com_softc *));
436 void com_pcmcom_disable __P((struct com_softc *));
437
438 int
439 com_pcmcom_match(parent, cf, aux)
440 struct device *parent;
441 struct cfdata *cf;
442 void *aux;
443 {
444
445 /* Device is always present. */
446 return (1);
447 }
448
449 void
450 com_pcmcom_attach(parent, self, aux)
451 struct device *parent, *self;
452 void *aux;
453 {
454 struct com_softc *sc = (struct com_softc *)self;
455 struct pcmcom_attach_args *pca = aux;
456
457 sc->sc_iot = pca->pca_iot;
458 sc->sc_ioh = pca->pca_ioh;
459
460 sc->enabled = 1;
461
462 sc->sc_iobase = -1;
463 sc->sc_frequency = COM_FREQ;
464
465 sc->enable = com_pcmcom_enable;
466 sc->disable = com_pcmcom_disable;
467
468 com_attach_subr(sc);
469
470 sc->enabled = 0;
471 }
472
473 int
474 com_pcmcom_enable(sc)
475 struct com_softc *sc;
476 {
477
478 return (pcmcom_enable((struct pcmcom_softc *)sc->sc_dev.dv_parent));
479 }
480
481 void
482 com_pcmcom_disable(sc)
483 struct com_softc *sc;
484 {
485
486 pcmcom_disable((struct pcmcom_softc *)sc->sc_dev.dv_parent);
487 }
488 #endif /* NCOM_PCMCOM > 0 */
489