pcmcom.c revision 1.21 1 1.21 perry /* $NetBSD: pcmcom.c,v 1.21 2005/02/04 02:10:45 perry Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.17 mycroft * Copyright (c) 1998, 2000, 2004 The NetBSD Foundation, Inc.
5 1.1 thorpej * All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.1 thorpej * by RedBack Networks, Inc.
9 1.1 thorpej *
10 1.1 thorpej * Redistribution and use in source and binary forms, with or without
11 1.1 thorpej * modification, are permitted provided that the following conditions
12 1.1 thorpej * are met:
13 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
17 1.1 thorpej * documentation and/or other materials provided with the distribution.
18 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
19 1.1 thorpej * must display the following acknowledgement:
20 1.1 thorpej * This product includes software developed by the NetBSD
21 1.1 thorpej * Foundation, Inc. and its contributors.
22 1.1 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 thorpej * contributors may be used to endorse or promote products derived
24 1.1 thorpej * from this software without specific prior written permission.
25 1.1 thorpej *
26 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 thorpej * POSSIBILITY OF SUCH DAMAGE.
37 1.1 thorpej */
38 1.1 thorpej
39 1.1 thorpej /*
40 1.1 thorpej * Device driver for multi-port PCMCIA serial cards, written by
41 1.1 thorpej * Jason R. Thorpe for RedBack Networks, Inc.
42 1.1 thorpej *
43 1.1 thorpej * Most of these cards are simply multiple UARTs sharing a single interrupt
44 1.1 thorpej * line, and rely on the fact that PCMCIA level-triggered interrupts can
45 1.1 thorpej * be shared. There are no special interrupt registers on them, as there
46 1.1 thorpej * are on most ISA multi-port serial cards.
47 1.1 thorpej *
48 1.1 thorpej * If there are other cards that have interrupt registers, they should not
49 1.1 thorpej * be glued into this driver. Rather, separate drivers should be written
50 1.1 thorpej * for those devices, as we have in the ISA multi-port serial card case.
51 1.1 thorpej */
52 1.7 lukem
53 1.7 lukem #include <sys/cdefs.h>
54 1.21 perry __KERNEL_RCSID(0, "$NetBSD: pcmcom.c,v 1.21 2005/02/04 02:10:45 perry Exp $");
55 1.1 thorpej
56 1.1 thorpej #include <sys/param.h>
57 1.1 thorpej #include <sys/systm.h>
58 1.1 thorpej #include <sys/device.h>
59 1.1 thorpej #include <sys/termios.h>
60 1.1 thorpej #include <sys/malloc.h>
61 1.1 thorpej
62 1.1 thorpej #include <machine/bus.h>
63 1.1 thorpej #include <machine/intr.h>
64 1.1 thorpej
65 1.1 thorpej #include <dev/ic/comreg.h>
66 1.1 thorpej #include <dev/ic/comvar.h>
67 1.1 thorpej
68 1.1 thorpej #include <dev/pcmcia/pcmciavar.h>
69 1.1 thorpej #include <dev/pcmcia/pcmciareg.h>
70 1.1 thorpej #include <dev/pcmcia/pcmciadevs.h>
71 1.1 thorpej
72 1.1 thorpej #include "com.h"
73 1.1 thorpej #include "pcmcom.h"
74 1.1 thorpej
75 1.1 thorpej #include "locators.h"
76 1.1 thorpej
77 1.1 thorpej struct pcmcom_softc {
78 1.1 thorpej struct device sc_dev; /* generic device glue */
79 1.1 thorpej
80 1.1 thorpej struct pcmcia_function *sc_pf; /* our PCMCIA function */
81 1.1 thorpej void *sc_ih; /* interrupt handle */
82 1.1 thorpej int sc_enabled_count; /* enabled count */
83 1.1 thorpej
84 1.19 mycroft #define NSLAVES 8
85 1.19 mycroft struct device *sc_slaves[NSLAVES]; /* slave info */
86 1.1 thorpej int sc_nslaves; /* slave count */
87 1.19 mycroft
88 1.19 mycroft int sc_state;
89 1.19 mycroft #define PCMCOM_ATTACHED 3
90 1.1 thorpej };
91 1.1 thorpej
92 1.1 thorpej struct pcmcom_attach_args {
93 1.1 thorpej bus_space_tag_t pca_iot; /* I/O tag */
94 1.1 thorpej bus_space_handle_t pca_ioh; /* I/O handle */
95 1.1 thorpej int pca_slave; /* slave # */
96 1.1 thorpej };
97 1.1 thorpej
98 1.21 perry int pcmcom_match(struct device *, struct cfdata *, void *);
99 1.21 perry int pcmcom_validate_config(struct pcmcia_config_entry *);
100 1.21 perry void pcmcom_attach(struct device *, struct device *, void *);
101 1.21 perry int pcmcom_detach(struct device *, int);
102 1.21 perry int pcmcom_activate(struct device *, enum devact);
103 1.1 thorpej
104 1.12 thorpej CFATTACH_DECL(pcmcom, sizeof(struct pcmcom_softc),
105 1.13 thorpej pcmcom_match, pcmcom_attach, pcmcom_detach, pcmcom_activate);
106 1.1 thorpej
107 1.19 mycroft const struct pcmcia_product pcmcom_products[] = {
108 1.19 mycroft { PCMCIA_VENDOR_SOCKET, PCMCIA_PRODUCT_SOCKET_DUAL_RS232,
109 1.19 mycroft PCMCIA_CIS_INVALID },
110 1.1 thorpej };
111 1.17 mycroft const size_t pcmcom_nproducts =
112 1.17 mycroft sizeof(pcmcom_products) / sizeof(pcmcom_products[0]);
113 1.1 thorpej
114 1.21 perry int pcmcom_print(void *, const char *);
115 1.21 perry int pcmcom_submatch(struct device *, struct cfdata *,
116 1.21 perry const locdesc_t *, void *);
117 1.1 thorpej
118 1.21 perry int pcmcom_enable(struct pcmcom_softc *);
119 1.21 perry void pcmcom_disable(struct pcmcom_softc *);
120 1.1 thorpej
121 1.21 perry int pcmcom_intr(void *);
122 1.1 thorpej
123 1.1 thorpej int
124 1.1 thorpej pcmcom_match(parent, cf, aux)
125 1.1 thorpej struct device *parent;
126 1.1 thorpej struct cfdata *cf;
127 1.1 thorpej void *aux;
128 1.1 thorpej {
129 1.1 thorpej struct pcmcia_attach_args *pa = aux;
130 1.1 thorpej
131 1.17 mycroft if (pcmcia_product_lookup(pa, pcmcom_products, pcmcom_nproducts,
132 1.17 mycroft sizeof(pcmcom_products[0]), NULL))
133 1.17 mycroft return (2); /* beat com_pcmcia */
134 1.1 thorpej return (0);
135 1.1 thorpej }
136 1.1 thorpej
137 1.19 mycroft int
138 1.19 mycroft pcmcom_validate_config(cfe)
139 1.19 mycroft struct pcmcia_config_entry *cfe;
140 1.19 mycroft {
141 1.19 mycroft if (cfe->iftype != PCMCIA_IFTYPE_IO ||
142 1.19 mycroft cfe->num_iospace < 1 || cfe->num_iospace > NSLAVES)
143 1.19 mycroft return (EINVAL);
144 1.19 mycroft return (0);
145 1.19 mycroft }
146 1.19 mycroft
147 1.1 thorpej void
148 1.1 thorpej pcmcom_attach(parent, self, aux)
149 1.1 thorpej struct device *parent, *self;
150 1.1 thorpej void *aux;
151 1.1 thorpej {
152 1.19 mycroft struct pcmcom_softc *sc = (void *)self;
153 1.1 thorpej struct pcmcia_attach_args *pa = aux;
154 1.1 thorpej struct pcmcia_config_entry *cfe;
155 1.19 mycroft int slave;
156 1.19 mycroft int error;
157 1.20 drochner int help[2];
158 1.20 drochner locdesc_t *ldesc = (void *)help; /* XXX */
159 1.1 thorpej
160 1.1 thorpej sc->sc_pf = pa->pf;
161 1.1 thorpej
162 1.19 mycroft error = pcmcia_function_configure(pa->pf, pcmcom_validate_config);
163 1.19 mycroft if (error) {
164 1.19 mycroft aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
165 1.19 mycroft error);
166 1.1 thorpej return;
167 1.1 thorpej }
168 1.1 thorpej
169 1.19 mycroft cfe = pa->pf->cfe;
170 1.19 mycroft sc->sc_nslaves = cfe->num_iospace;
171 1.1 thorpej
172 1.19 mycroft error = pcmcom_enable(sc);
173 1.19 mycroft if (error)
174 1.19 mycroft goto fail;
175 1.1 thorpej
176 1.1 thorpej /* Attach the children. */
177 1.1 thorpej for (slave = 0; slave < sc->sc_nslaves; slave++) {
178 1.19 mycroft struct pcmcom_attach_args pca;
179 1.1 thorpej
180 1.19 mycroft printf("%s: slave %d\n", self->dv_xname, slave);
181 1.1 thorpej
182 1.19 mycroft pca.pca_iot = cfe->iospace[slave].handle.iot;
183 1.19 mycroft pca.pca_ioh = cfe->iospace[slave].handle.ioh;
184 1.19 mycroft pca.pca_slave = slave;
185 1.1 thorpej
186 1.20 drochner ldesc->len = 1;
187 1.20 drochner ldesc->locs[PCMCOMCF_SLAVE] = slave;
188 1.20 drochner
189 1.20 drochner sc->sc_slaves[slave] = config_found_sm_loc(&sc->sc_dev,
190 1.20 drochner "pcmcom", ldesc,
191 1.20 drochner &pca, pcmcom_print, pcmcom_submatch);
192 1.1 thorpej }
193 1.1 thorpej
194 1.19 mycroft pcmcom_disable(sc);
195 1.19 mycroft sc->sc_state = PCMCOM_ATTACHED;
196 1.19 mycroft return;
197 1.1 thorpej
198 1.19 mycroft fail:
199 1.19 mycroft pcmcia_function_unconfigure(pa->pf);
200 1.1 thorpej }
201 1.1 thorpej
202 1.1 thorpej int
203 1.3 thorpej pcmcom_detach(self, flags)
204 1.3 thorpej struct device *self;
205 1.3 thorpej int flags;
206 1.3 thorpej {
207 1.19 mycroft struct pcmcom_softc *sc = (void *)self;
208 1.3 thorpej int slave, error;
209 1.3 thorpej
210 1.19 mycroft if (sc->sc_state != PCMCOM_ATTACHED)
211 1.19 mycroft return (0);
212 1.19 mycroft
213 1.4 thorpej for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
214 1.19 mycroft if (sc->sc_slaves[slave]) {
215 1.19 mycroft /* Detach the child. */
216 1.19 mycroft error = config_detach(sc->sc_slaves[slave], flags);
217 1.19 mycroft if (error)
218 1.19 mycroft return (error);
219 1.19 mycroft sc->sc_slaves[slave] = 0;
220 1.19 mycroft }
221 1.19 mycroft }
222 1.3 thorpej
223 1.19 mycroft pcmcia_function_unconfigure(sc->sc_pf);
224 1.3 thorpej
225 1.3 thorpej return (0);
226 1.3 thorpej }
227 1.3 thorpej
228 1.3 thorpej int
229 1.3 thorpej pcmcom_activate(self, act)
230 1.3 thorpej struct device *self;
231 1.3 thorpej enum devact act;
232 1.3 thorpej {
233 1.19 mycroft struct pcmcom_softc *sc = (void *)self;
234 1.3 thorpej int slave, error = 0, s;
235 1.3 thorpej
236 1.3 thorpej s = splserial();
237 1.3 thorpej switch (act) {
238 1.3 thorpej case DVACT_ACTIVATE:
239 1.3 thorpej error = EOPNOTSUPP;
240 1.3 thorpej break;
241 1.3 thorpej
242 1.3 thorpej case DVACT_DEACTIVATE:
243 1.4 thorpej for (slave = sc->sc_nslaves - 1; slave >= 0; slave--) {
244 1.19 mycroft if (sc->sc_slaves[slave]) {
245 1.19 mycroft /*
246 1.19 mycroft * Deactivate the child. Doing so will cause
247 1.19 mycroft * our own enabled count to drop to 0, once all
248 1.19 mycroft * children are deactivated.
249 1.19 mycroft */
250 1.19 mycroft error = config_deactivate(sc->sc_slaves[slave]);
251 1.19 mycroft if (error)
252 1.19 mycroft break;
253 1.19 mycroft }
254 1.3 thorpej }
255 1.3 thorpej break;
256 1.3 thorpej }
257 1.3 thorpej splx(s);
258 1.3 thorpej return (error);
259 1.3 thorpej }
260 1.3 thorpej
261 1.3 thorpej int
262 1.1 thorpej pcmcom_print(aux, pnp)
263 1.1 thorpej void *aux;
264 1.1 thorpej const char *pnp;
265 1.1 thorpej {
266 1.1 thorpej struct pcmcom_attach_args *pca = aux;
267 1.1 thorpej
268 1.1 thorpej /* only com's can attach to pcmcom's; easy... */
269 1.1 thorpej if (pnp)
270 1.14 thorpej aprint_normal("com at %s", pnp);
271 1.1 thorpej
272 1.14 thorpej aprint_normal(" slave %d", pca->pca_slave);
273 1.1 thorpej
274 1.1 thorpej return (UNCONF);
275 1.1 thorpej }
276 1.1 thorpej
277 1.1 thorpej int
278 1.20 drochner pcmcom_submatch(parent, cf, ldesc, aux)
279 1.1 thorpej struct device *parent;
280 1.1 thorpej struct cfdata *cf;
281 1.20 drochner const locdesc_t *ldesc;
282 1.1 thorpej void *aux;
283 1.1 thorpej {
284 1.1 thorpej
285 1.20 drochner if (cf->cf_loc[PCMCOMCF_SLAVE] != PCMCOMCF_SLAVE_DEFAULT &&
286 1.20 drochner cf->cf_loc[PCMCOMCF_SLAVE] != ldesc->locs[PCMCOMCF_SLAVE]);
287 1.1 thorpej return (0);
288 1.1 thorpej
289 1.10 thorpej return (config_match(parent, cf, aux));
290 1.1 thorpej }
291 1.1 thorpej
292 1.1 thorpej int
293 1.1 thorpej pcmcom_intr(arg)
294 1.1 thorpej void *arg;
295 1.1 thorpej {
296 1.1 thorpej #if NCOM > 0
297 1.1 thorpej struct pcmcom_softc *sc = arg;
298 1.1 thorpej int i, rval = 0;
299 1.1 thorpej
300 1.1 thorpej if (sc->sc_enabled_count == 0)
301 1.1 thorpej return (0);
302 1.1 thorpej
303 1.1 thorpej for (i = 0; i < sc->sc_nslaves; i++) {
304 1.19 mycroft if (sc->sc_slaves[i])
305 1.19 mycroft rval |= comintr(sc->sc_slaves[i]);
306 1.1 thorpej }
307 1.1 thorpej
308 1.1 thorpej return (rval);
309 1.1 thorpej #else
310 1.1 thorpej return (0);
311 1.1 thorpej #endif /* NCOM > 0 */
312 1.1 thorpej }
313 1.1 thorpej
314 1.1 thorpej int
315 1.1 thorpej pcmcom_enable(sc)
316 1.1 thorpej struct pcmcom_softc *sc;
317 1.1 thorpej {
318 1.16 mycroft int error;
319 1.1 thorpej
320 1.16 mycroft if (sc->sc_enabled_count++ != 0)
321 1.1 thorpej return (0);
322 1.1 thorpej
323 1.1 thorpej /* Establish the interrupt. */
324 1.1 thorpej sc->sc_ih = pcmcia_intr_establish(sc->sc_pf, IPL_SERIAL,
325 1.1 thorpej pcmcom_intr, sc);
326 1.19 mycroft if (!sc->sc_ih)
327 1.19 mycroft return (EIO);
328 1.1 thorpej
329 1.16 mycroft error = pcmcia_function_enable(sc->sc_pf);
330 1.19 mycroft if (error) {
331 1.16 mycroft pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
332 1.19 mycroft sc->sc_ih = 0;
333 1.19 mycroft }
334 1.16 mycroft
335 1.16 mycroft return (error);
336 1.1 thorpej }
337 1.1 thorpej
338 1.1 thorpej void
339 1.1 thorpej pcmcom_disable(sc)
340 1.1 thorpej struct pcmcom_softc *sc;
341 1.1 thorpej {
342 1.1 thorpej
343 1.16 mycroft if (--sc->sc_enabled_count != 0)
344 1.1 thorpej return;
345 1.1 thorpej
346 1.1 thorpej pcmcia_function_disable(sc->sc_pf);
347 1.1 thorpej pcmcia_intr_disestablish(sc->sc_pf, sc->sc_ih);
348 1.19 mycroft sc->sc_ih = 0;
349 1.1 thorpej }
350 1.1 thorpej
351 1.1 thorpej /****** Here begins the com attachment code. ******/
352 1.1 thorpej
353 1.1 thorpej #if NCOM_PCMCOM > 0
354 1.21 perry int com_pcmcom_match(struct device *, struct cfdata *, void *);
355 1.21 perry void com_pcmcom_attach(struct device *, struct device *, void *);
356 1.1 thorpej
357 1.1 thorpej /* No pcmcom-specific goo in the softc; it's all in the parent. */
358 1.12 thorpej CFATTACH_DECL(com_pcmcom, sizeof(struct com_softc),
359 1.13 thorpej com_pcmcom_match, com_pcmcom_attach, com_detach, com_activate);
360 1.1 thorpej
361 1.21 perry int com_pcmcom_enable(struct com_softc *);
362 1.21 perry void com_pcmcom_disable(struct com_softc *);
363 1.1 thorpej
364 1.1 thorpej int
365 1.1 thorpej com_pcmcom_match(parent, cf, aux)
366 1.1 thorpej struct device *parent;
367 1.1 thorpej struct cfdata *cf;
368 1.1 thorpej void *aux;
369 1.1 thorpej {
370 1.1 thorpej
371 1.1 thorpej /* Device is always present. */
372 1.1 thorpej return (1);
373 1.1 thorpej }
374 1.1 thorpej
375 1.1 thorpej void
376 1.1 thorpej com_pcmcom_attach(parent, self, aux)
377 1.1 thorpej struct device *parent, *self;
378 1.1 thorpej void *aux;
379 1.1 thorpej {
380 1.1 thorpej struct com_softc *sc = (struct com_softc *)self;
381 1.1 thorpej struct pcmcom_attach_args *pca = aux;
382 1.1 thorpej
383 1.1 thorpej sc->sc_iot = pca->pca_iot;
384 1.1 thorpej sc->sc_ioh = pca->pca_ioh;
385 1.1 thorpej
386 1.1 thorpej sc->enabled = 1;
387 1.1 thorpej
388 1.1 thorpej sc->sc_iobase = -1;
389 1.1 thorpej sc->sc_frequency = COM_FREQ;
390 1.1 thorpej
391 1.1 thorpej sc->enable = com_pcmcom_enable;
392 1.1 thorpej sc->disable = com_pcmcom_disable;
393 1.1 thorpej
394 1.1 thorpej com_attach_subr(sc);
395 1.1 thorpej
396 1.1 thorpej sc->enabled = 0;
397 1.1 thorpej }
398 1.1 thorpej
399 1.1 thorpej int
400 1.1 thorpej com_pcmcom_enable(sc)
401 1.1 thorpej struct com_softc *sc;
402 1.1 thorpej {
403 1.1 thorpej
404 1.1 thorpej return (pcmcom_enable((struct pcmcom_softc *)sc->sc_dev.dv_parent));
405 1.1 thorpej }
406 1.1 thorpej
407 1.1 thorpej void
408 1.1 thorpej com_pcmcom_disable(sc)
409 1.1 thorpej struct com_softc *sc;
410 1.1 thorpej {
411 1.1 thorpej
412 1.1 thorpej pcmcom_disable((struct pcmcom_softc *)sc->sc_dev.dv_parent);
413 1.1 thorpej }
414 1.1 thorpej #endif /* NCOM_PCMCOM > 0 */
415