moxa_isa.c revision 1.1.2.2 1 1.1.2.2 bouyer /* $NetBSD: moxa_isa.c,v 1.1.2.2 2000/11/22 16:03:47 bouyer Exp $ */
2 1.1.2.2 bouyer
3 1.1.2.2 bouyer /*
4 1.1.2.2 bouyer * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.1.2.2 bouyer * Copyright (c) 1995 Charles M. Hannum. All rights reserved.
6 1.1.2.2 bouyer *
7 1.1.2.2 bouyer * This code is derived from public-domain software written by
8 1.1.2.2 bouyer * Roland McGrath.
9 1.1.2.2 bouyer *
10 1.1.2.2 bouyer * Redistribution and use in source and binary forms, with or without
11 1.1.2.2 bouyer * modification, are permitted provided that the following conditions
12 1.1.2.2 bouyer * are met:
13 1.1.2.2 bouyer * 1. Redistributions of source code must retain the above copyright
14 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer.
15 1.1.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
17 1.1.2.2 bouyer * documentation and/or other materials provided with the distribution.
18 1.1.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
19 1.1.2.2 bouyer * must display the following acknowledgement:
20 1.1.2.2 bouyer * This product includes software developed by Charles M. Hannum.
21 1.1.2.2 bouyer * 4. The name of the author may not be used to endorse or promote products
22 1.1.2.2 bouyer * derived from this software without specific prior written permission.
23 1.1.2.2 bouyer *
24 1.1.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.1.2.2 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1.2.2 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1.2.2 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1.2.2 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1.2.2 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1.2.2 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1.2.2 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1.2.2 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1.2.2 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1.2.2 bouyer */
35 1.1.2.2 bouyer
36 1.1.2.2 bouyer #include <sys/param.h>
37 1.1.2.2 bouyer #include <sys/systm.h>
38 1.1.2.2 bouyer #include <sys/device.h>
39 1.1.2.2 bouyer #include <sys/termios.h>
40 1.1.2.2 bouyer
41 1.1.2.2 bouyer #include <machine/bus.h>
42 1.1.2.2 bouyer #include <machine/intr.h>
43 1.1.2.2 bouyer
44 1.1.2.2 bouyer #include <dev/ic/comreg.h>
45 1.1.2.2 bouyer #include <dev/ic/comvar.h>
46 1.1.2.2 bouyer
47 1.1.2.2 bouyer #include <dev/isa/isavar.h>
48 1.1.2.2 bouyer #include <dev/isa/com_multi.h>
49 1.1.2.2 bouyer
50 1.1.2.2 bouyer #define NSLAVES 8
51 1.1.2.2 bouyer
52 1.1.2.2 bouyer struct moxa_isa_softc {
53 1.1.2.2 bouyer struct device sc_dev;
54 1.1.2.2 bouyer void *sc_ih;
55 1.1.2.2 bouyer
56 1.1.2.2 bouyer bus_space_tag_t sc_iot;
57 1.1.2.2 bouyer int sc_iobase;
58 1.1.2.2 bouyer
59 1.1.2.2 bouyer int sc_alive; /* mask of slave units attached */
60 1.1.2.2 bouyer void *sc_slaves[NSLAVES]; /* com device unit numbers */
61 1.1.2.2 bouyer bus_space_handle_t sc_slaveioh[NSLAVES];
62 1.1.2.2 bouyer };
63 1.1.2.2 bouyer
64 1.1.2.2 bouyer int moxa_isaprobe __P((struct device *, struct cfdata *, void *));
65 1.1.2.2 bouyer void moxa_isaattach __P((struct device *, struct device *, void *));
66 1.1.2.2 bouyer int moxa_isaintr __P((void *));
67 1.1.2.2 bouyer int moxa_isaprint __P((void *, const char *));
68 1.1.2.2 bouyer
69 1.1.2.2 bouyer struct cfattach moxa_isa_ca = {
70 1.1.2.2 bouyer sizeof(struct moxa_isa_softc), moxa_isaprobe, moxa_isaattach
71 1.1.2.2 bouyer };
72 1.1.2.2 bouyer
73 1.1.2.2 bouyer int
74 1.1.2.2 bouyer moxa_isaprobe(parent, self, aux)
75 1.1.2.2 bouyer struct device *parent;
76 1.1.2.2 bouyer struct cfdata *self;
77 1.1.2.2 bouyer void *aux;
78 1.1.2.2 bouyer {
79 1.1.2.2 bouyer struct isa_attach_args *ia = aux;
80 1.1.2.2 bouyer int iobase = ia->ia_iobase;
81 1.1.2.2 bouyer bus_space_tag_t iot = ia->ia_iot;
82 1.1.2.2 bouyer bus_space_handle_t ioh;
83 1.1.2.2 bouyer int i, rv = 1;
84 1.1.2.2 bouyer
85 1.1.2.2 bouyer /*
86 1.1.2.2 bouyer * Do the normal com probe for the first UART and assume
87 1.1.2.2 bouyer * its presence, and the ability to map the other UARTS,
88 1.1.2.2 bouyer * means there is a multiport board there.
89 1.1.2.2 bouyer * XXX Needs more robustness.
90 1.1.2.2 bouyer */
91 1.1.2.2 bouyer
92 1.1.2.2 bouyer /* Disallow wildcarded i/o address. */
93 1.1.2.2 bouyer if (ia->ia_iobase == ISACF_PORT_DEFAULT)
94 1.1.2.2 bouyer return (0);
95 1.1.2.2 bouyer
96 1.1.2.2 bouyer /* if the first port is in use as console, then it. */
97 1.1.2.2 bouyer if (com_is_console(iot, iobase, 0))
98 1.1.2.2 bouyer goto checkmappings;
99 1.1.2.2 bouyer
100 1.1.2.2 bouyer if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
101 1.1.2.2 bouyer rv = 0;
102 1.1.2.2 bouyer goto out;
103 1.1.2.2 bouyer }
104 1.1.2.2 bouyer rv = comprobe1(iot, ioh);
105 1.1.2.2 bouyer bus_space_unmap(iot, ioh, COM_NPORTS);
106 1.1.2.2 bouyer if (rv == 0)
107 1.1.2.2 bouyer goto out;
108 1.1.2.2 bouyer
109 1.1.2.2 bouyer checkmappings:
110 1.1.2.2 bouyer for (i = 1; i < NSLAVES; i++) {
111 1.1.2.2 bouyer iobase += COM_NPORTS;
112 1.1.2.2 bouyer
113 1.1.2.2 bouyer if (com_is_console(iot, iobase, 0))
114 1.1.2.2 bouyer continue;
115 1.1.2.2 bouyer
116 1.1.2.2 bouyer if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) {
117 1.1.2.2 bouyer rv = 0;
118 1.1.2.2 bouyer goto out;
119 1.1.2.2 bouyer }
120 1.1.2.2 bouyer bus_space_unmap(iot, ioh, COM_NPORTS);
121 1.1.2.2 bouyer }
122 1.1.2.2 bouyer
123 1.1.2.2 bouyer out:
124 1.1.2.2 bouyer if (rv)
125 1.1.2.2 bouyer ia->ia_iosize = NSLAVES * COM_NPORTS;
126 1.1.2.2 bouyer return (rv);
127 1.1.2.2 bouyer }
128 1.1.2.2 bouyer
129 1.1.2.2 bouyer int
130 1.1.2.2 bouyer moxa_isaprint(aux, pnp)
131 1.1.2.2 bouyer void *aux;
132 1.1.2.2 bouyer const char *pnp;
133 1.1.2.2 bouyer {
134 1.1.2.2 bouyer struct commulti_attach_args *ca = aux;
135 1.1.2.2 bouyer
136 1.1.2.2 bouyer if (pnp)
137 1.1.2.2 bouyer printf("com at %s", pnp);
138 1.1.2.2 bouyer printf(" slave %d", ca->ca_slave);
139 1.1.2.2 bouyer return (UNCONF);
140 1.1.2.2 bouyer }
141 1.1.2.2 bouyer
142 1.1.2.2 bouyer void
143 1.1.2.2 bouyer moxa_isaattach(parent, self, aux)
144 1.1.2.2 bouyer struct device *parent, *self;
145 1.1.2.2 bouyer void *aux;
146 1.1.2.2 bouyer {
147 1.1.2.2 bouyer struct moxa_isa_softc *sc = (void *)self;
148 1.1.2.2 bouyer struct isa_attach_args *ia = aux;
149 1.1.2.2 bouyer struct commulti_attach_args ca;
150 1.1.2.2 bouyer bus_space_tag_t iot = ia->ia_iot;
151 1.1.2.2 bouyer int i, iobase;
152 1.1.2.2 bouyer
153 1.1.2.2 bouyer printf("\n");
154 1.1.2.2 bouyer
155 1.1.2.2 bouyer sc->sc_iot = ia->ia_iot;
156 1.1.2.2 bouyer sc->sc_iobase = ia->ia_iobase;
157 1.1.2.2 bouyer
158 1.1.2.2 bouyer for (i = 0; i < NSLAVES; i++) {
159 1.1.2.2 bouyer iobase = sc->sc_iobase + i * COM_NPORTS;
160 1.1.2.2 bouyer if (!com_is_console(iot, iobase, &sc->sc_slaveioh[i]) &&
161 1.1.2.2 bouyer bus_space_map(iot, iobase, COM_NPORTS, 0,
162 1.1.2.2 bouyer &sc->sc_slaveioh[i])) {
163 1.1.2.2 bouyer printf("%s: can't map i/o space for slave %d\n",
164 1.1.2.2 bouyer sc->sc_dev.dv_xname, i);
165 1.1.2.2 bouyer return;
166 1.1.2.2 bouyer }
167 1.1.2.2 bouyer }
168 1.1.2.2 bouyer
169 1.1.2.2 bouyer for (i = 0; i < NSLAVES; i++) {
170 1.1.2.2 bouyer ca.ca_slave = i;
171 1.1.2.2 bouyer ca.ca_iot = sc->sc_iot;
172 1.1.2.2 bouyer ca.ca_ioh = sc->sc_slaveioh[i];
173 1.1.2.2 bouyer ca.ca_iobase = sc->sc_iobase + i * COM_NPORTS;
174 1.1.2.2 bouyer ca.ca_noien = 1;
175 1.1.2.2 bouyer
176 1.1.2.2 bouyer sc->sc_slaves[i] = config_found(self, &ca, moxa_isaprint);
177 1.1.2.2 bouyer if (sc->sc_slaves[i] != NULL)
178 1.1.2.2 bouyer sc->sc_alive |= 1 << i;
179 1.1.2.2 bouyer }
180 1.1.2.2 bouyer
181 1.1.2.2 bouyer sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
182 1.1.2.2 bouyer IPL_SERIAL, moxa_isaintr, sc);
183 1.1.2.2 bouyer }
184 1.1.2.2 bouyer
185 1.1.2.2 bouyer int
186 1.1.2.2 bouyer moxa_isaintr(arg)
187 1.1.2.2 bouyer void *arg;
188 1.1.2.2 bouyer {
189 1.1.2.2 bouyer struct moxa_isa_softc *sc = arg;
190 1.1.2.2 bouyer int bits;
191 1.1.2.2 bouyer int rv;
192 1.1.2.2 bouyer
193 1.1.2.2 bouyer bits = sc->sc_alive;
194 1.1.2.2 bouyer if (bits == 0)
195 1.1.2.2 bouyer return (0);
196 1.1.2.2 bouyer
197 1.1.2.2 bouyer for (;;rv = 0) {
198 1.1.2.2 bouyer #define TRY(n) \
199 1.1.2.2 bouyer if (bits & (1 << (n))) \
200 1.1.2.2 bouyer rv += comintr(sc->sc_slaves[n]);
201 1.1.2.2 bouyer TRY(0);
202 1.1.2.2 bouyer TRY(1);
203 1.1.2.2 bouyer TRY(2);
204 1.1.2.2 bouyer TRY(3);
205 1.1.2.2 bouyer TRY(4);
206 1.1.2.2 bouyer TRY(5);
207 1.1.2.2 bouyer TRY(6);
208 1.1.2.2 bouyer TRY(7);
209 1.1.2.2 bouyer #undef TRY
210 1.1.2.2 bouyer if (rv == 0)
211 1.1.2.2 bouyer return (1);
212 1.1.2.2 bouyer }
213 1.1.2.2 bouyer }
214