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