plcom.c revision 1.36 1 1.36 skrll /* $NetBSD: plcom.c,v 1.36 2012/05/20 07:54:40 skrll Exp $ */
2 1.1 rearnsha
3 1.1 rearnsha /*-
4 1.1 rearnsha * Copyright (c) 2001 ARM Ltd
5 1.1 rearnsha * All rights reserved.
6 1.1 rearnsha *
7 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
8 1.1 rearnsha * modification, are permitted provided that the following conditions
9 1.1 rearnsha * are met:
10 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
11 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
12 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
14 1.1 rearnsha * documentation and/or other materials provided with the distribution.
15 1.1 rearnsha * 3. The name of the company may not be used to endorse or promote
16 1.1 rearnsha * products derived from this software without specific prior written
17 1.1 rearnsha * permission.
18 1.1 rearnsha *
19 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20 1.1 rearnsha * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 1.1 rearnsha * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 1.1 rearnsha * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 1.1 rearnsha * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 1.1 rearnsha * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 1.1 rearnsha * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 rearnsha * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 rearnsha * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 rearnsha * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 rearnsha * SUCH DAMAGE.
30 1.1 rearnsha *
31 1.1 rearnsha * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
32 1.1 rearnsha * All rights reserved.
33 1.1 rearnsha *
34 1.1 rearnsha * This code is derived from software contributed to The NetBSD Foundation
35 1.1 rearnsha * by Charles M. Hannum.
36 1.1 rearnsha *
37 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
38 1.1 rearnsha * modification, are permitted provided that the following conditions
39 1.1 rearnsha * are met:
40 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
41 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
42 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
44 1.1 rearnsha * documentation and/or other materials provided with the distribution.
45 1.1 rearnsha *
46 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
47 1.1 rearnsha * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
48 1.1 rearnsha * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
49 1.1 rearnsha * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
50 1.1 rearnsha * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 1.1 rearnsha * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 1.1 rearnsha * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 1.1 rearnsha * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 1.1 rearnsha * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 1.1 rearnsha * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
56 1.1 rearnsha * POSSIBILITY OF SUCH DAMAGE.
57 1.1 rearnsha */
58 1.1 rearnsha
59 1.1 rearnsha /*
60 1.1 rearnsha * Copyright (c) 1991 The Regents of the University of California.
61 1.1 rearnsha * All rights reserved.
62 1.1 rearnsha *
63 1.1 rearnsha * Redistribution and use in source and binary forms, with or without
64 1.1 rearnsha * modification, are permitted provided that the following conditions
65 1.1 rearnsha * are met:
66 1.1 rearnsha * 1. Redistributions of source code must retain the above copyright
67 1.1 rearnsha * notice, this list of conditions and the following disclaimer.
68 1.1 rearnsha * 2. Redistributions in binary form must reproduce the above copyright
69 1.1 rearnsha * notice, this list of conditions and the following disclaimer in the
70 1.1 rearnsha * documentation and/or other materials provided with the distribution.
71 1.9 agc * 3. Neither the name of the University nor the names of its contributors
72 1.1 rearnsha * may be used to endorse or promote products derived from this software
73 1.1 rearnsha * without specific prior written permission.
74 1.1 rearnsha *
75 1.1 rearnsha * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
76 1.1 rearnsha * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77 1.1 rearnsha * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
78 1.1 rearnsha * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
79 1.1 rearnsha * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
80 1.1 rearnsha * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
81 1.1 rearnsha * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
82 1.1 rearnsha * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
83 1.1 rearnsha * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
84 1.1 rearnsha * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85 1.1 rearnsha * SUCH DAMAGE.
86 1.1 rearnsha *
87 1.1 rearnsha * @(#)com.c 7.5 (Berkeley) 5/16/91
88 1.1 rearnsha */
89 1.1 rearnsha
90 1.1 rearnsha /*
91 1.1 rearnsha * COM driver for the Prime Cell PL010 UART, which is similar to the 16C550,
92 1.1 rearnsha * but has a completely different programmer's model.
93 1.1 rearnsha * Derived from the NS16550AF com driver.
94 1.1 rearnsha */
95 1.8 lukem
96 1.8 lukem #include <sys/cdefs.h>
97 1.36 skrll __KERNEL_RCSID(0, "$NetBSD: plcom.c,v 1.36 2012/05/20 07:54:40 skrll Exp $");
98 1.1 rearnsha
99 1.1 rearnsha #include "opt_plcom.h"
100 1.1 rearnsha #include "opt_ddb.h"
101 1.1 rearnsha #include "opt_kgdb.h"
102 1.7 martin #include "opt_lockdebug.h"
103 1.7 martin #include "opt_multiprocessor.h"
104 1.1 rearnsha
105 1.1 rearnsha #include "rnd.h"
106 1.33 tls #ifdef RND_COM
107 1.1 rearnsha #include <sys/rnd.h>
108 1.1 rearnsha #endif
109 1.1 rearnsha
110 1.1 rearnsha /*
111 1.1 rearnsha * Override cnmagic(9) macro before including <sys/systm.h>.
112 1.1 rearnsha * We need to know if cn_check_magic triggered debugger, so set a flag.
113 1.1 rearnsha * Callers of cn_check_magic must declare int cn_trapped = 0;
114 1.1 rearnsha * XXX: this is *ugly*!
115 1.1 rearnsha */
116 1.1 rearnsha #define cn_trap() \
117 1.1 rearnsha do { \
118 1.1 rearnsha console_debugger(); \
119 1.1 rearnsha cn_trapped = 1; \
120 1.1 rearnsha } while (/* CONSTCOND */ 0)
121 1.1 rearnsha
122 1.1 rearnsha #include <sys/param.h>
123 1.1 rearnsha #include <sys/systm.h>
124 1.1 rearnsha #include <sys/ioctl.h>
125 1.1 rearnsha #include <sys/select.h>
126 1.1 rearnsha #include <sys/tty.h>
127 1.1 rearnsha #include <sys/proc.h>
128 1.1 rearnsha #include <sys/conf.h>
129 1.1 rearnsha #include <sys/file.h>
130 1.1 rearnsha #include <sys/uio.h>
131 1.1 rearnsha #include <sys/kernel.h>
132 1.1 rearnsha #include <sys/syslog.h>
133 1.1 rearnsha #include <sys/types.h>
134 1.1 rearnsha #include <sys/device.h>
135 1.1 rearnsha #include <sys/malloc.h>
136 1.1 rearnsha #include <sys/timepps.h>
137 1.1 rearnsha #include <sys/vnode.h>
138 1.16 elad #include <sys/kauth.h>
139 1.25 ad #include <sys/intr.h>
140 1.25 ad #include <sys/bus.h>
141 1.1 rearnsha
142 1.1 rearnsha #include <evbarm/dev/plcomreg.h>
143 1.1 rearnsha #include <evbarm/dev/plcomvar.h>
144 1.1 rearnsha
145 1.1 rearnsha #include <dev/cons.h>
146 1.1 rearnsha
147 1.1 rearnsha static void plcom_enable_debugport (struct plcom_softc *);
148 1.1 rearnsha
149 1.1 rearnsha void plcom_config (struct plcom_softc *);
150 1.1 rearnsha void plcom_shutdown (struct plcom_softc *);
151 1.1 rearnsha int plcomspeed (long, long);
152 1.1 rearnsha static u_char cflag2lcr (tcflag_t);
153 1.1 rearnsha int plcomparam (struct tty *, struct termios *);
154 1.1 rearnsha void plcomstart (struct tty *);
155 1.1 rearnsha int plcomhwiflow (struct tty *, int);
156 1.1 rearnsha
157 1.1 rearnsha void plcom_loadchannelregs (struct plcom_softc *);
158 1.1 rearnsha void plcom_hwiflow (struct plcom_softc *);
159 1.1 rearnsha void plcom_break (struct plcom_softc *, int);
160 1.1 rearnsha void plcom_modem (struct plcom_softc *, int);
161 1.1 rearnsha void tiocm_to_plcom (struct plcom_softc *, u_long, int);
162 1.1 rearnsha int plcom_to_tiocm (struct plcom_softc *);
163 1.1 rearnsha void plcom_iflush (struct plcom_softc *);
164 1.1 rearnsha
165 1.1 rearnsha int plcom_common_getc (dev_t, bus_space_tag_t, bus_space_handle_t);
166 1.1 rearnsha void plcom_common_putc (dev_t, bus_space_tag_t, bus_space_handle_t, int);
167 1.1 rearnsha
168 1.1 rearnsha int plcominit (bus_space_tag_t, bus_addr_t, int, int, tcflag_t,
169 1.1 rearnsha bus_space_handle_t *);
170 1.1 rearnsha
171 1.4 gehenna dev_type_open(plcomopen);
172 1.4 gehenna dev_type_close(plcomclose);
173 1.4 gehenna dev_type_read(plcomread);
174 1.4 gehenna dev_type_write(plcomwrite);
175 1.4 gehenna dev_type_ioctl(plcomioctl);
176 1.4 gehenna dev_type_stop(plcomstop);
177 1.4 gehenna dev_type_tty(plcomtty);
178 1.4 gehenna dev_type_poll(plcompoll);
179 1.1 rearnsha
180 1.1 rearnsha int plcomcngetc (dev_t);
181 1.1 rearnsha void plcomcnputc (dev_t, int);
182 1.1 rearnsha void plcomcnpollc (dev_t, int);
183 1.1 rearnsha
184 1.1 rearnsha #define integrate static inline
185 1.1 rearnsha void plcomsoft (void *);
186 1.1 rearnsha integrate void plcom_rxsoft (struct plcom_softc *, struct tty *);
187 1.1 rearnsha integrate void plcom_txsoft (struct plcom_softc *, struct tty *);
188 1.1 rearnsha integrate void plcom_stsoft (struct plcom_softc *, struct tty *);
189 1.1 rearnsha integrate void plcom_schedrx (struct plcom_softc *);
190 1.1 rearnsha void plcomdiag (void *);
191 1.1 rearnsha
192 1.1 rearnsha extern struct cfdriver plcom_cd;
193 1.1 rearnsha
194 1.4 gehenna const struct cdevsw plcom_cdevsw = {
195 1.4 gehenna plcomopen, plcomclose, plcomread, plcomwrite, plcomioctl,
196 1.5 jdolecek plcomstop, plcomtty, plcompoll, nommap, ttykqfilter, D_TTY
197 1.4 gehenna };
198 1.4 gehenna
199 1.1 rearnsha /*
200 1.1 rearnsha * Make this an option variable one can patch.
201 1.1 rearnsha * But be warned: this must be a power of 2!
202 1.1 rearnsha */
203 1.1 rearnsha u_int plcom_rbuf_size = PLCOM_RING_SIZE;
204 1.1 rearnsha
205 1.1 rearnsha /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
206 1.1 rearnsha u_int plcom_rbuf_hiwat = (PLCOM_RING_SIZE * 1) / 4;
207 1.1 rearnsha u_int plcom_rbuf_lowat = (PLCOM_RING_SIZE * 3) / 4;
208 1.1 rearnsha
209 1.1 rearnsha static int plcomconsunit = -1;
210 1.1 rearnsha static bus_space_tag_t plcomconstag;
211 1.1 rearnsha static bus_space_handle_t plcomconsioh;
212 1.1 rearnsha static int plcomconsattached;
213 1.1 rearnsha static int plcomconsrate;
214 1.1 rearnsha static tcflag_t plcomconscflag;
215 1.1 rearnsha static struct cnm_state plcom_cnm_state;
216 1.1 rearnsha
217 1.1 rearnsha static int ppscap =
218 1.1 rearnsha PPS_TSFMT_TSPEC |
219 1.1 rearnsha PPS_CAPTUREASSERT |
220 1.1 rearnsha PPS_CAPTURECLEAR |
221 1.1 rearnsha #ifdef PPS_SYNC
222 1.1 rearnsha PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR |
223 1.1 rearnsha #endif /* PPS_SYNC */
224 1.1 rearnsha PPS_OFFSETASSERT | PPS_OFFSETCLEAR;
225 1.1 rearnsha
226 1.1 rearnsha #ifdef KGDB
227 1.1 rearnsha #include <sys/kgdb.h>
228 1.1 rearnsha
229 1.1 rearnsha static int plcom_kgdb_unit;
230 1.1 rearnsha static bus_space_tag_t plcom_kgdb_iot;
231 1.1 rearnsha static bus_space_handle_t plcom_kgdb_ioh;
232 1.1 rearnsha static int plcom_kgdb_attached;
233 1.1 rearnsha
234 1.1 rearnsha int plcom_kgdb_getc (void *);
235 1.1 rearnsha void plcom_kgdb_putc (void *, int);
236 1.1 rearnsha #endif /* KGDB */
237 1.1 rearnsha
238 1.1 rearnsha #define PLCOMUNIT_MASK 0x7ffff
239 1.1 rearnsha #define PLCOMDIALOUT_MASK 0x80000
240 1.1 rearnsha
241 1.1 rearnsha #define PLCOMUNIT(x) (minor(x) & PLCOMUNIT_MASK)
242 1.1 rearnsha #define PLCOMDIALOUT(x) (minor(x) & PLCOMDIALOUT_MASK)
243 1.1 rearnsha
244 1.1 rearnsha #define PLCOM_ISALIVE(sc) ((sc)->enabled != 0 && \
245 1.14 thorpej device_is_active(&(sc)->sc_dev))
246 1.1 rearnsha
247 1.1 rearnsha #define BR BUS_SPACE_BARRIER_READ
248 1.1 rearnsha #define BW BUS_SPACE_BARRIER_WRITE
249 1.1 rearnsha #define PLCOM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, PLCOM_UART_SIZE, (f))
250 1.1 rearnsha
251 1.1 rearnsha int
252 1.1 rearnsha plcomspeed(long speed, long frequency)
253 1.1 rearnsha {
254 1.1 rearnsha #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */
255 1.1 rearnsha
256 1.1 rearnsha int x, err;
257 1.1 rearnsha
258 1.1 rearnsha #if 0
259 1.1 rearnsha if (speed == 0)
260 1.1 rearnsha return 0;
261 1.1 rearnsha #endif
262 1.1 rearnsha if (speed <= 0)
263 1.1 rearnsha return -1;
264 1.1 rearnsha x = divrnd(frequency / 16, speed);
265 1.1 rearnsha if (x <= 0)
266 1.1 rearnsha return -1;
267 1.1 rearnsha err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000;
268 1.1 rearnsha if (err < 0)
269 1.1 rearnsha err = -err;
270 1.1 rearnsha if (err > PLCOM_TOLERANCE)
271 1.1 rearnsha return -1;
272 1.1 rearnsha return x;
273 1.1 rearnsha
274 1.1 rearnsha #undef divrnd
275 1.1 rearnsha }
276 1.1 rearnsha
277 1.1 rearnsha #ifdef PLCOM_DEBUG
278 1.1 rearnsha int plcom_debug = 0;
279 1.1 rearnsha
280 1.34 bsh void plcomstatus (struct plcom_softc *, const char *);
281 1.1 rearnsha void
282 1.34 bsh plcomstatus(struct plcom_softc *sc, const char *str)
283 1.1 rearnsha {
284 1.1 rearnsha struct tty *tp = sc->sc_tty;
285 1.1 rearnsha
286 1.1 rearnsha printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n",
287 1.1 rearnsha sc->sc_dev.dv_xname, str,
288 1.1 rearnsha ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
289 1.35 skrll ISSET(sc->sc_msr, PL01X_MSR_DCD) ? "+" : "-",
290 1.1 rearnsha ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
291 1.35 skrll ISSET(sc->sc_mcr, PL01X_MCR_DTR) ? "+" : "-",
292 1.1 rearnsha sc->sc_tx_stopped ? "+" : "-");
293 1.1 rearnsha
294 1.1 rearnsha printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n",
295 1.1 rearnsha sc->sc_dev.dv_xname, str,
296 1.1 rearnsha ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
297 1.35 skrll ISSET(sc->sc_msr, PL01X_MSR_CTS) ? "+" : "-",
298 1.1 rearnsha ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
299 1.35 skrll ISSET(sc->sc_mcr, PL01X_MCR_RTS) ? "+" : "-",
300 1.1 rearnsha sc->sc_rx_flags);
301 1.1 rearnsha }
302 1.1 rearnsha #endif
303 1.1 rearnsha
304 1.34 bsh /* XXX this function is not used? */
305 1.1 rearnsha int
306 1.1 rearnsha plcomprobe1(bus_space_tag_t iot, bus_space_handle_t ioh)
307 1.1 rearnsha {
308 1.1 rearnsha int data;
309 1.1 rearnsha
310 1.1 rearnsha /* Disable the UART. */
311 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, 0);
312 1.1 rearnsha /* Make sure the FIFO is off. */
313 1.35 skrll bus_space_write_1(iot, ioh, plcom_lcr, PL01X_LCR_8BITS);
314 1.1 rearnsha /* Disable interrupts. */
315 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_iir, 0);
316 1.1 rearnsha
317 1.1 rearnsha /* Make sure we swallow anything in the receiving register. */
318 1.1 rearnsha data = bus_space_read_1(iot, ioh, plcom_dr);
319 1.1 rearnsha
320 1.35 skrll if (bus_space_read_1(iot, ioh, plcom_lcr) != PL01X_LCR_8BITS)
321 1.1 rearnsha return 0;
322 1.1 rearnsha
323 1.35 skrll data = bus_space_read_1(iot, ioh, plcom_fr) & (PL01X_FR_RXFF | PL01X_FR_RXFE);
324 1.1 rearnsha
325 1.35 skrll if (data != PL01X_FR_RXFE)
326 1.1 rearnsha return 0;
327 1.1 rearnsha
328 1.1 rearnsha return 1;
329 1.1 rearnsha }
330 1.1 rearnsha
331 1.36 skrll /*
332 1.36 skrll * No locking in this routine; it is only called during attach,
333 1.36 skrll * or with the port already locked.
334 1.36 skrll */
335 1.1 rearnsha static void
336 1.1 rearnsha plcom_enable_debugport(struct plcom_softc *sc)
337 1.1 rearnsha {
338 1.1 rearnsha
339 1.1 rearnsha /* Turn on line break interrupt, set carrier. */
340 1.35 skrll sc->sc_cr = PL010_CR_RIE | PL010_CR_RTIE | PL01X_CR_UARTEN;
341 1.1 rearnsha bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
342 1.35 skrll SET(sc->sc_mcr, PL01X_MCR_DTR | PL01X_MCR_RTS);
343 1.15 thorpej /* XXX device_unit() abuse */
344 1.15 thorpej sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
345 1.15 thorpej sc->sc_mcr);
346 1.1 rearnsha }
347 1.1 rearnsha
348 1.1 rearnsha void
349 1.1 rearnsha plcom_attach_subr(struct plcom_softc *sc)
350 1.1 rearnsha {
351 1.1 rearnsha int unit = sc->sc_iounit;
352 1.1 rearnsha bus_space_tag_t iot = sc->sc_iot;
353 1.1 rearnsha bus_space_handle_t ioh = sc->sc_ioh;
354 1.1 rearnsha struct tty *tp;
355 1.1 rearnsha
356 1.21 ad callout_init(&sc->sc_diag_callout, 0);
357 1.36 skrll mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH);
358 1.1 rearnsha
359 1.1 rearnsha /* Disable interrupts before configuring the device. */
360 1.1 rearnsha sc->sc_cr = 0;
361 1.1 rearnsha
362 1.1 rearnsha if (plcomconstag && unit == plcomconsunit) {
363 1.1 rearnsha plcomconsattached = 1;
364 1.1 rearnsha
365 1.1 rearnsha plcomconstag = iot;
366 1.1 rearnsha plcomconsioh = ioh;
367 1.1 rearnsha
368 1.1 rearnsha /* Make sure the console is always "hardwired". */
369 1.1 rearnsha delay(1000); /* wait for output to finish */
370 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_CONSOLE);
371 1.1 rearnsha SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
372 1.1 rearnsha /* Must re-enable the console immediately, or we will
373 1.1 rearnsha hang when trying to print. */
374 1.35 skrll sc->sc_cr = PL01X_CR_UARTEN;
375 1.1 rearnsha }
376 1.1 rearnsha
377 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
378 1.1 rearnsha
379 1.1 rearnsha /* The PL010 has a 16-byte fifo, but the tx interrupt triggers when
380 1.1 rearnsha there is space for 8 more bytes. */
381 1.1 rearnsha sc->sc_fifolen = 8;
382 1.1 rearnsha printf("\n");
383 1.1 rearnsha
384 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_TXFIFO_DISABLE)) {
385 1.1 rearnsha sc->sc_fifolen = 1;
386 1.1 rearnsha printf("%s: txfifo disabled\n", sc->sc_dev.dv_xname);
387 1.1 rearnsha }
388 1.1 rearnsha
389 1.1 rearnsha if (sc->sc_fifolen > 1)
390 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_FIFO);
391 1.1 rearnsha
392 1.32 rmind tp = tty_alloc();
393 1.1 rearnsha tp->t_oproc = plcomstart;
394 1.1 rearnsha tp->t_param = plcomparam;
395 1.1 rearnsha tp->t_hwiflow = plcomhwiflow;
396 1.1 rearnsha
397 1.1 rearnsha sc->sc_tty = tp;
398 1.1 rearnsha sc->sc_rbuf = malloc(plcom_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
399 1.1 rearnsha sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
400 1.1 rearnsha sc->sc_rbavail = plcom_rbuf_size;
401 1.1 rearnsha if (sc->sc_rbuf == NULL) {
402 1.1 rearnsha printf("%s: unable to allocate ring buffer\n",
403 1.1 rearnsha sc->sc_dev.dv_xname);
404 1.1 rearnsha return;
405 1.1 rearnsha }
406 1.1 rearnsha sc->sc_ebuf = sc->sc_rbuf + (plcom_rbuf_size << 1);
407 1.1 rearnsha
408 1.1 rearnsha tty_attach(tp);
409 1.1 rearnsha
410 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
411 1.1 rearnsha int maj;
412 1.1 rearnsha
413 1.1 rearnsha /* locate the major number */
414 1.4 gehenna maj = cdevsw_lookup_major(&plcom_cdevsw);
415 1.1 rearnsha
416 1.15 thorpej cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
417 1.1 rearnsha
418 1.1 rearnsha printf("%s: console\n", sc->sc_dev.dv_xname);
419 1.1 rearnsha }
420 1.1 rearnsha
421 1.1 rearnsha #ifdef KGDB
422 1.1 rearnsha /*
423 1.1 rearnsha * Allow kgdb to "take over" this port. If this is
424 1.1 rearnsha * the kgdb device, it has exclusive use.
425 1.1 rearnsha */
426 1.1 rearnsha if (iot == plcom_kgdb_iot && unit == plcom_kgdb_unit) {
427 1.1 rearnsha plcom_kgdb_attached = 1;
428 1.1 rearnsha
429 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_KGDB);
430 1.1 rearnsha printf("%s: kgdb\n", sc->sc_dev.dv_xname);
431 1.1 rearnsha }
432 1.1 rearnsha #endif
433 1.1 rearnsha
434 1.25 ad sc->sc_si = softint_establish(SOFTINT_SERIAL, plcomsoft, sc);
435 1.1 rearnsha
436 1.33 tls #ifdef RND_COM
437 1.1 rearnsha rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
438 1.1 rearnsha RND_TYPE_TTY, 0);
439 1.1 rearnsha #endif
440 1.1 rearnsha
441 1.1 rearnsha /* if there are no enable/disable functions, assume the device
442 1.1 rearnsha is always enabled */
443 1.1 rearnsha if (!sc->enable)
444 1.1 rearnsha sc->enabled = 1;
445 1.1 rearnsha
446 1.1 rearnsha plcom_config(sc);
447 1.1 rearnsha
448 1.1 rearnsha SET(sc->sc_hwflags, PLCOM_HW_DEV_OK);
449 1.1 rearnsha }
450 1.1 rearnsha
451 1.1 rearnsha void
452 1.1 rearnsha plcom_config(struct plcom_softc *sc)
453 1.1 rearnsha {
454 1.1 rearnsha bus_space_tag_t iot = sc->sc_iot;
455 1.1 rearnsha bus_space_handle_t ioh = sc->sc_ioh;
456 1.1 rearnsha
457 1.1 rearnsha /* Disable interrupts before configuring the device. */
458 1.1 rearnsha sc->sc_cr = 0;
459 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
460 1.1 rearnsha
461 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
462 1.1 rearnsha plcom_enable_debugport(sc);
463 1.1 rearnsha }
464 1.1 rearnsha
465 1.1 rearnsha int
466 1.29 dsl plcom_detach(struct device *self, int flags)
467 1.1 rearnsha {
468 1.1 rearnsha struct plcom_softc *sc = (struct plcom_softc *)self;
469 1.1 rearnsha int maj, mn;
470 1.1 rearnsha
471 1.31 dyoung if (sc->sc_hwflags & (PLCOM_HW_CONSOLE|PLCOM_HW_KGDB))
472 1.31 dyoung return EBUSY;
473 1.31 dyoung
474 1.31 dyoung if (sc->disable != NULL && sc->enabled != 0) {
475 1.31 dyoung (*sc->disable)(sc);
476 1.31 dyoung sc->enabled = 0;
477 1.31 dyoung }
478 1.31 dyoung
479 1.1 rearnsha /* locate the major number */
480 1.4 gehenna maj = cdevsw_lookup_major(&plcom_cdevsw);
481 1.1 rearnsha
482 1.1 rearnsha /* Nuke the vnodes for any open instances. */
483 1.15 thorpej mn = device_unit(self);
484 1.1 rearnsha vdevgone(maj, mn, mn, VCHR);
485 1.1 rearnsha
486 1.1 rearnsha mn |= PLCOMDIALOUT_MASK;
487 1.1 rearnsha vdevgone(maj, mn, mn, VCHR);
488 1.1 rearnsha
489 1.1 rearnsha /* Free the receive buffer. */
490 1.1 rearnsha free(sc->sc_rbuf, M_DEVBUF);
491 1.1 rearnsha
492 1.1 rearnsha /* Detach and free the tty. */
493 1.1 rearnsha tty_detach(sc->sc_tty);
494 1.32 rmind tty_free(sc->sc_tty);
495 1.1 rearnsha
496 1.1 rearnsha /* Unhook the soft interrupt handler. */
497 1.25 ad softint_disestablish(sc->sc_si);
498 1.1 rearnsha
499 1.33 tls #ifdef RND_COM
500 1.1 rearnsha /* Unhook the entropy source. */
501 1.1 rearnsha rnd_detach_source(&sc->rnd_source);
502 1.1 rearnsha #endif
503 1.1 rearnsha
504 1.36 skrll /* Destroy the lock. */
505 1.36 skrll mutex_destroy(&sc->sc_lock);
506 1.36 skrll
507 1.1 rearnsha return 0;
508 1.1 rearnsha }
509 1.1 rearnsha
510 1.1 rearnsha int
511 1.31 dyoung plcom_activate(device_t self, enum devact act)
512 1.1 rearnsha {
513 1.31 dyoung struct plcom_softc *sc = device_private(self);
514 1.1 rearnsha
515 1.1 rearnsha switch (act) {
516 1.1 rearnsha case DVACT_DEACTIVATE:
517 1.31 dyoung sc->enabled = 0;
518 1.31 dyoung return 0;
519 1.31 dyoung default:
520 1.31 dyoung return EOPNOTSUPP;
521 1.1 rearnsha }
522 1.1 rearnsha }
523 1.1 rearnsha
524 1.1 rearnsha void
525 1.1 rearnsha plcom_shutdown(struct plcom_softc *sc)
526 1.1 rearnsha {
527 1.1 rearnsha struct tty *tp = sc->sc_tty;
528 1.36 skrll mutex_spin_enter(&sc->sc_lock);
529 1.1 rearnsha
530 1.1 rearnsha /* If we were asserting flow control, then deassert it. */
531 1.1 rearnsha SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
532 1.1 rearnsha plcom_hwiflow(sc);
533 1.1 rearnsha
534 1.1 rearnsha /* Clear any break condition set with TIOCSBRK. */
535 1.1 rearnsha plcom_break(sc, 0);
536 1.1 rearnsha
537 1.1 rearnsha /* Turn off PPS capture on last close. */
538 1.26 ad mutex_spin_enter(&timecounter_lock);
539 1.1 rearnsha sc->sc_ppsmask = 0;
540 1.1 rearnsha sc->ppsparam.mode = 0;
541 1.26 ad mutex_spin_exit(&timecounter_lock);
542 1.1 rearnsha
543 1.1 rearnsha /*
544 1.1 rearnsha * Hang up if necessary. Wait a bit, so the other side has time to
545 1.1 rearnsha * notice even if we immediately open the port again.
546 1.1 rearnsha * Avoid tsleeping above splhigh().
547 1.1 rearnsha */
548 1.1 rearnsha if (ISSET(tp->t_cflag, HUPCL)) {
549 1.1 rearnsha plcom_modem(sc, 0);
550 1.36 skrll mutex_spin_exit(&sc->sc_lock);
551 1.36 skrll /* XXX will only timeout */
552 1.36 skrll (void) kpause(ttclos, false, hz, NULL);
553 1.36 skrll mutex_spin_enter(&sc->sc_lock);
554 1.1 rearnsha }
555 1.1 rearnsha
556 1.1 rearnsha /* Turn off interrupts. */
557 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE))
558 1.1 rearnsha /* interrupt on break */
559 1.35 skrll sc->sc_cr = PL010_CR_RIE | PL010_CR_RTIE | PL01X_CR_UARTEN;
560 1.1 rearnsha else
561 1.1 rearnsha sc->sc_cr = 0;
562 1.1 rearnsha bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
563 1.1 rearnsha
564 1.36 skrll mutex_spin_exit(&sc->sc_lock);
565 1.1 rearnsha if (sc->disable) {
566 1.1 rearnsha #ifdef DIAGNOSTIC
567 1.1 rearnsha if (!sc->enabled)
568 1.1 rearnsha panic("plcom_shutdown: not enabled?");
569 1.1 rearnsha #endif
570 1.1 rearnsha (*sc->disable)(sc);
571 1.1 rearnsha sc->enabled = 0;
572 1.1 rearnsha }
573 1.1 rearnsha }
574 1.1 rearnsha
575 1.1 rearnsha int
576 1.12 christos plcomopen(dev_t dev, int flag, int mode, struct lwp *l)
577 1.1 rearnsha {
578 1.1 rearnsha struct plcom_softc *sc;
579 1.1 rearnsha struct tty *tp;
580 1.36 skrll int s;
581 1.1 rearnsha int error;
582 1.1 rearnsha
583 1.28 cegger sc = device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
584 1.1 rearnsha if (sc == NULL || !ISSET(sc->sc_hwflags, PLCOM_HW_DEV_OK) ||
585 1.1 rearnsha sc->sc_rbuf == NULL)
586 1.1 rearnsha return ENXIO;
587 1.1 rearnsha
588 1.14 thorpej if (!device_is_active(&sc->sc_dev))
589 1.1 rearnsha return ENXIO;
590 1.1 rearnsha
591 1.1 rearnsha #ifdef KGDB
592 1.1 rearnsha /*
593 1.1 rearnsha * If this is the kgdb port, no other use is permitted.
594 1.1 rearnsha */
595 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_KGDB))
596 1.1 rearnsha return EBUSY;
597 1.1 rearnsha #endif
598 1.1 rearnsha
599 1.1 rearnsha tp = sc->sc_tty;
600 1.1 rearnsha
601 1.18 elad if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
602 1.18 elad return (EBUSY);
603 1.1 rearnsha
604 1.1 rearnsha s = spltty();
605 1.1 rearnsha
606 1.1 rearnsha /*
607 1.1 rearnsha * Do the following iff this is a first open.
608 1.1 rearnsha */
609 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
610 1.1 rearnsha struct termios t;
611 1.1 rearnsha
612 1.1 rearnsha tp->t_dev = dev;
613 1.1 rearnsha
614 1.1 rearnsha if (sc->enable) {
615 1.1 rearnsha if ((*sc->enable)(sc)) {
616 1.1 rearnsha splx(s);
617 1.1 rearnsha printf("%s: device enable failed\n",
618 1.1 rearnsha sc->sc_dev.dv_xname);
619 1.1 rearnsha return EIO;
620 1.1 rearnsha }
621 1.36 skrll mutex_spin_enter(&sc->sc_lock);
622 1.1 rearnsha sc->enabled = 1;
623 1.1 rearnsha plcom_config(sc);
624 1.36 skrll } else {
625 1.36 skrll mutex_spin_enter(&sc->sc_lock);
626 1.1 rearnsha }
627 1.1 rearnsha
628 1.1 rearnsha /* Turn on interrupts. */
629 1.1 rearnsha /* IER_ERXRDY | IER_ERLS | IER_EMSC; */
630 1.35 skrll sc->sc_cr = PL010_CR_RIE | PL010_CR_RTIE | PL010_CR_MSIE | PL01X_CR_UARTEN;
631 1.1 rearnsha bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr);
632 1.1 rearnsha
633 1.1 rearnsha /* Fetch the current modem control status, needed later. */
634 1.1 rearnsha sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, plcom_fr);
635 1.1 rearnsha
636 1.1 rearnsha /* Clear PPS capture state on first open. */
637 1.26 ad
638 1.26 ad mutex_spin_enter(&timecounter_lock);
639 1.1 rearnsha sc->sc_ppsmask = 0;
640 1.1 rearnsha sc->ppsparam.mode = 0;
641 1.26 ad mutex_spin_exit(&timecounter_lock);
642 1.1 rearnsha
643 1.36 skrll mutex_spin_enter(&sc->sc_lock);
644 1.1 rearnsha
645 1.1 rearnsha /*
646 1.1 rearnsha * Initialize the termios status to the defaults. Add in the
647 1.1 rearnsha * sticky bits from TIOCSFLAGS.
648 1.1 rearnsha */
649 1.1 rearnsha t.c_ispeed = 0;
650 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
651 1.1 rearnsha t.c_ospeed = plcomconsrate;
652 1.1 rearnsha t.c_cflag = plcomconscflag;
653 1.1 rearnsha } else {
654 1.1 rearnsha t.c_ospeed = TTYDEF_SPEED;
655 1.1 rearnsha t.c_cflag = TTYDEF_CFLAG;
656 1.1 rearnsha }
657 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
658 1.1 rearnsha SET(t.c_cflag, CLOCAL);
659 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
660 1.1 rearnsha SET(t.c_cflag, CRTSCTS);
661 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
662 1.1 rearnsha SET(t.c_cflag, MDMBUF);
663 1.1 rearnsha /* Make sure plcomparam() will do something. */
664 1.1 rearnsha tp->t_ospeed = 0;
665 1.1 rearnsha (void) plcomparam(tp, &t);
666 1.1 rearnsha tp->t_iflag = TTYDEF_IFLAG;
667 1.1 rearnsha tp->t_oflag = TTYDEF_OFLAG;
668 1.1 rearnsha tp->t_lflag = TTYDEF_LFLAG;
669 1.1 rearnsha ttychars(tp);
670 1.1 rearnsha ttsetwater(tp);
671 1.1 rearnsha
672 1.36 skrll mutex_spin_enter(&sc->sc_lock);
673 1.1 rearnsha
674 1.1 rearnsha /*
675 1.1 rearnsha * Turn on DTR. We must always do this, even if carrier is not
676 1.1 rearnsha * present, because otherwise we'd have to use TIOCSDTR
677 1.1 rearnsha * immediately after setting CLOCAL, which applications do not
678 1.1 rearnsha * expect. We always assert DTR while the device is open
679 1.1 rearnsha * unless explicitly requested to deassert it.
680 1.1 rearnsha */
681 1.1 rearnsha plcom_modem(sc, 1);
682 1.1 rearnsha
683 1.1 rearnsha /* Clear the input ring, and unblock. */
684 1.1 rearnsha sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
685 1.1 rearnsha sc->sc_rbavail = plcom_rbuf_size;
686 1.1 rearnsha plcom_iflush(sc);
687 1.1 rearnsha CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
688 1.1 rearnsha plcom_hwiflow(sc);
689 1.1 rearnsha
690 1.1 rearnsha #ifdef PLCOM_DEBUG
691 1.1 rearnsha if (plcom_debug)
692 1.1 rearnsha plcomstatus(sc, "plcomopen ");
693 1.1 rearnsha #endif
694 1.1 rearnsha
695 1.36 skrll mutex_spin_exit(&sc->sc_lock);
696 1.1 rearnsha }
697 1.1 rearnsha
698 1.1 rearnsha splx(s);
699 1.1 rearnsha
700 1.1 rearnsha error = ttyopen(tp, PLCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
701 1.1 rearnsha if (error)
702 1.1 rearnsha goto bad;
703 1.1 rearnsha
704 1.1 rearnsha error = (*tp->t_linesw->l_open)(dev, tp);
705 1.1 rearnsha if (error)
706 1.1 rearnsha goto bad;
707 1.1 rearnsha
708 1.1 rearnsha return 0;
709 1.1 rearnsha
710 1.1 rearnsha bad:
711 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
712 1.1 rearnsha /*
713 1.1 rearnsha * We failed to open the device, and nobody else had it opened.
714 1.1 rearnsha * Clean up the state as appropriate.
715 1.1 rearnsha */
716 1.1 rearnsha plcom_shutdown(sc);
717 1.1 rearnsha }
718 1.1 rearnsha
719 1.1 rearnsha return error;
720 1.1 rearnsha }
721 1.1 rearnsha
722 1.1 rearnsha int
723 1.12 christos plcomclose(dev_t dev, int flag, int mode, struct lwp *l)
724 1.1 rearnsha {
725 1.28 cegger struct plcom_softc *sc =
726 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
727 1.1 rearnsha struct tty *tp = sc->sc_tty;
728 1.1 rearnsha
729 1.1 rearnsha /* XXX This is for cons.c. */
730 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN))
731 1.1 rearnsha return 0;
732 1.1 rearnsha
733 1.1 rearnsha (*tp->t_linesw->l_close)(tp, flag);
734 1.1 rearnsha ttyclose(tp);
735 1.1 rearnsha
736 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
737 1.1 rearnsha return 0;
738 1.1 rearnsha
739 1.1 rearnsha if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
740 1.1 rearnsha /*
741 1.1 rearnsha * Although we got a last close, the device may still be in
742 1.1 rearnsha * use; e.g. if this was the dialout node, and there are still
743 1.1 rearnsha * processes waiting for carrier on the non-dialout node.
744 1.1 rearnsha */
745 1.1 rearnsha plcom_shutdown(sc);
746 1.1 rearnsha }
747 1.1 rearnsha
748 1.1 rearnsha return 0;
749 1.1 rearnsha }
750 1.1 rearnsha
751 1.1 rearnsha int
752 1.1 rearnsha plcomread(dev_t dev, struct uio *uio, int flag)
753 1.1 rearnsha {
754 1.28 cegger struct plcom_softc *sc =
755 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
756 1.1 rearnsha struct tty *tp = sc->sc_tty;
757 1.1 rearnsha
758 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
759 1.1 rearnsha return EIO;
760 1.1 rearnsha
761 1.1 rearnsha return (*tp->t_linesw->l_read)(tp, uio, flag);
762 1.1 rearnsha }
763 1.1 rearnsha
764 1.1 rearnsha int
765 1.1 rearnsha plcomwrite(dev_t dev, struct uio *uio, int flag)
766 1.1 rearnsha {
767 1.28 cegger struct plcom_softc *sc =
768 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
769 1.1 rearnsha struct tty *tp = sc->sc_tty;
770 1.1 rearnsha
771 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
772 1.1 rearnsha return EIO;
773 1.1 rearnsha
774 1.1 rearnsha return (*tp->t_linesw->l_write)(tp, uio, flag);
775 1.1 rearnsha }
776 1.1 rearnsha
777 1.1 rearnsha int
778 1.12 christos plcompoll(dev_t dev, int events, struct lwp *l)
779 1.1 rearnsha {
780 1.28 cegger struct plcom_softc *sc =
781 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
782 1.1 rearnsha struct tty *tp = sc->sc_tty;
783 1.1 rearnsha
784 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
785 1.1 rearnsha return EIO;
786 1.1 rearnsha
787 1.12 christos return (*tp->t_linesw->l_poll)(tp, events, l);
788 1.1 rearnsha }
789 1.1 rearnsha
790 1.1 rearnsha struct tty *
791 1.1 rearnsha plcomtty(dev_t dev)
792 1.1 rearnsha {
793 1.28 cegger struct plcom_softc *sc =
794 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
795 1.1 rearnsha struct tty *tp = sc->sc_tty;
796 1.1 rearnsha
797 1.1 rearnsha return tp;
798 1.1 rearnsha }
799 1.1 rearnsha
800 1.1 rearnsha int
801 1.20 christos plcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
802 1.1 rearnsha {
803 1.28 cegger struct plcom_softc *sc =
804 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(dev));
805 1.1 rearnsha struct tty *tp = sc->sc_tty;
806 1.1 rearnsha int error;
807 1.1 rearnsha
808 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
809 1.1 rearnsha return EIO;
810 1.1 rearnsha
811 1.12 christos error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
812 1.3 atatat if (error != EPASSTHROUGH)
813 1.1 rearnsha return error;
814 1.1 rearnsha
815 1.12 christos error = ttioctl(tp, cmd, data, flag, l);
816 1.3 atatat if (error != EPASSTHROUGH)
817 1.1 rearnsha return error;
818 1.1 rearnsha
819 1.1 rearnsha error = 0;
820 1.1 rearnsha
821 1.36 skrll mutex_spin_enter(&sc->sc_lock);
822 1.1 rearnsha switch (cmd) {
823 1.1 rearnsha case TIOCSBRK:
824 1.1 rearnsha plcom_break(sc, 1);
825 1.1 rearnsha break;
826 1.1 rearnsha
827 1.1 rearnsha case TIOCCBRK:
828 1.1 rearnsha plcom_break(sc, 0);
829 1.1 rearnsha break;
830 1.1 rearnsha
831 1.1 rearnsha case TIOCSDTR:
832 1.1 rearnsha plcom_modem(sc, 1);
833 1.1 rearnsha break;
834 1.1 rearnsha
835 1.1 rearnsha case TIOCCDTR:
836 1.1 rearnsha plcom_modem(sc, 0);
837 1.1 rearnsha break;
838 1.1 rearnsha
839 1.1 rearnsha case TIOCGFLAGS:
840 1.1 rearnsha *(int *)data = sc->sc_swflags;
841 1.1 rearnsha break;
842 1.1 rearnsha
843 1.1 rearnsha case TIOCSFLAGS:
844 1.19 elad error = kauth_authorize_device_tty(l->l_cred,
845 1.19 elad KAUTH_DEVICE_TTY_PRIVSET, tp);
846 1.1 rearnsha if (error)
847 1.1 rearnsha break;
848 1.1 rearnsha sc->sc_swflags = *(int *)data;
849 1.1 rearnsha break;
850 1.1 rearnsha
851 1.1 rearnsha case TIOCMSET:
852 1.1 rearnsha case TIOCMBIS:
853 1.1 rearnsha case TIOCMBIC:
854 1.1 rearnsha tiocm_to_plcom(sc, cmd, *(int *)data);
855 1.1 rearnsha break;
856 1.1 rearnsha
857 1.1 rearnsha case TIOCMGET:
858 1.1 rearnsha *(int *)data = plcom_to_tiocm(sc);
859 1.1 rearnsha break;
860 1.1 rearnsha
861 1.1 rearnsha case PPS_IOC_CREATE:
862 1.1 rearnsha break;
863 1.1 rearnsha
864 1.1 rearnsha case PPS_IOC_DESTROY:
865 1.1 rearnsha break;
866 1.1 rearnsha
867 1.1 rearnsha case PPS_IOC_GETPARAMS: {
868 1.1 rearnsha pps_params_t *pp;
869 1.1 rearnsha pp = (pps_params_t *)data;
870 1.26 ad mutex_spin_enter(&timecounter_lock);
871 1.1 rearnsha *pp = sc->ppsparam;
872 1.26 ad mutex_spin_exit(&timecounter_lock);
873 1.1 rearnsha break;
874 1.1 rearnsha }
875 1.1 rearnsha
876 1.1 rearnsha case PPS_IOC_SETPARAMS: {
877 1.1 rearnsha pps_params_t *pp;
878 1.1 rearnsha int mode;
879 1.1 rearnsha pp = (pps_params_t *)data;
880 1.26 ad mutex_spin_enter(&timecounter_lock);
881 1.1 rearnsha if (pp->mode & ~ppscap) {
882 1.1 rearnsha error = EINVAL;
883 1.26 ad mutex_spin_exit(&timecounter_lock);
884 1.1 rearnsha break;
885 1.1 rearnsha }
886 1.1 rearnsha sc->ppsparam = *pp;
887 1.1 rearnsha /*
888 1.1 rearnsha * Compute msr masks from user-specified timestamp state.
889 1.1 rearnsha */
890 1.1 rearnsha mode = sc->ppsparam.mode;
891 1.1 rearnsha #ifdef PPS_SYNC
892 1.1 rearnsha if (mode & PPS_HARDPPSONASSERT) {
893 1.1 rearnsha mode |= PPS_CAPTUREASSERT;
894 1.1 rearnsha /* XXX revoke any previous HARDPPS source */
895 1.1 rearnsha }
896 1.1 rearnsha if (mode & PPS_HARDPPSONCLEAR) {
897 1.1 rearnsha mode |= PPS_CAPTURECLEAR;
898 1.1 rearnsha /* XXX revoke any previous HARDPPS source */
899 1.1 rearnsha }
900 1.1 rearnsha #endif /* PPS_SYNC */
901 1.1 rearnsha switch (mode & PPS_CAPTUREBOTH) {
902 1.1 rearnsha case 0:
903 1.1 rearnsha sc->sc_ppsmask = 0;
904 1.1 rearnsha break;
905 1.1 rearnsha
906 1.1 rearnsha case PPS_CAPTUREASSERT:
907 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
908 1.35 skrll sc->sc_ppsassert = PL01X_MSR_DCD;
909 1.1 rearnsha sc->sc_ppsclear = -1;
910 1.1 rearnsha break;
911 1.1 rearnsha
912 1.1 rearnsha case PPS_CAPTURECLEAR:
913 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
914 1.1 rearnsha sc->sc_ppsassert = -1;
915 1.1 rearnsha sc->sc_ppsclear = 0;
916 1.1 rearnsha break;
917 1.1 rearnsha
918 1.1 rearnsha case PPS_CAPTUREBOTH:
919 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
920 1.35 skrll sc->sc_ppsassert = PL01X_MSR_DCD;
921 1.1 rearnsha sc->sc_ppsclear = 0;
922 1.1 rearnsha break;
923 1.1 rearnsha
924 1.1 rearnsha default:
925 1.1 rearnsha error = EINVAL;
926 1.1 rearnsha break;
927 1.1 rearnsha }
928 1.26 ad mutex_spin_exit(&timecounter_lock);
929 1.1 rearnsha break;
930 1.1 rearnsha }
931 1.1 rearnsha
932 1.1 rearnsha case PPS_IOC_GETCAP:
933 1.1 rearnsha *(int*)data = ppscap;
934 1.1 rearnsha break;
935 1.1 rearnsha
936 1.1 rearnsha case PPS_IOC_FETCH: {
937 1.1 rearnsha pps_info_t *pi;
938 1.1 rearnsha pi = (pps_info_t *)data;
939 1.26 ad mutex_spin_enter(&timecounter_lock);
940 1.1 rearnsha *pi = sc->ppsinfo;
941 1.26 ad mutex_spin_exit(&timecounter_lock);
942 1.1 rearnsha break;
943 1.1 rearnsha }
944 1.1 rearnsha
945 1.1 rearnsha case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
946 1.1 rearnsha /*
947 1.1 rearnsha * Some GPS clocks models use the falling rather than
948 1.1 rearnsha * rising edge as the on-the-second signal.
949 1.1 rearnsha * The old API has no way to specify PPS polarity.
950 1.1 rearnsha */
951 1.26 ad mutex_spin_enter(&timecounter_lock);
952 1.35 skrll sc->sc_ppsmask = PL01X_MSR_DCD;
953 1.1 rearnsha #ifndef PPS_TRAILING_EDGE
954 1.35 skrll sc->sc_ppsassert = PL01X_MSR_DCD;
955 1.1 rearnsha sc->sc_ppsclear = -1;
956 1.1 rearnsha TIMESPEC_TO_TIMEVAL((struct timeval *)data,
957 1.1 rearnsha &sc->ppsinfo.assert_timestamp);
958 1.1 rearnsha #else
959 1.1 rearnsha sc->sc_ppsassert = -1
960 1.1 rearnsha sc->sc_ppsclear = 0;
961 1.1 rearnsha TIMESPEC_TO_TIMEVAL((struct timeval *)data,
962 1.1 rearnsha &sc->ppsinfo.clear_timestamp);
963 1.1 rearnsha #endif
964 1.26 ad mutex_spin_exit(&timecounter_lock);
965 1.1 rearnsha break;
966 1.1 rearnsha
967 1.1 rearnsha default:
968 1.3 atatat error = EPASSTHROUGH;
969 1.1 rearnsha break;
970 1.1 rearnsha }
971 1.1 rearnsha
972 1.36 skrll mutex_spin_exit(&sc->sc_lock);
973 1.1 rearnsha
974 1.1 rearnsha #ifdef PLCOM_DEBUG
975 1.1 rearnsha if (plcom_debug)
976 1.1 rearnsha plcomstatus(sc, "plcomioctl ");
977 1.1 rearnsha #endif
978 1.1 rearnsha
979 1.1 rearnsha return error;
980 1.1 rearnsha }
981 1.1 rearnsha
982 1.1 rearnsha integrate void
983 1.1 rearnsha plcom_schedrx(struct plcom_softc *sc)
984 1.1 rearnsha {
985 1.1 rearnsha
986 1.1 rearnsha sc->sc_rx_ready = 1;
987 1.1 rearnsha
988 1.1 rearnsha /* Wake up the poller. */
989 1.25 ad softint_schedule(sc->sc_si);
990 1.1 rearnsha }
991 1.1 rearnsha
992 1.1 rearnsha void
993 1.1 rearnsha plcom_break(struct plcom_softc *sc, int onoff)
994 1.1 rearnsha {
995 1.1 rearnsha
996 1.1 rearnsha if (onoff)
997 1.35 skrll SET(sc->sc_lcr, PL01X_LCR_BRK);
998 1.1 rearnsha else
999 1.35 skrll CLR(sc->sc_lcr, PL01X_LCR_BRK);
1000 1.1 rearnsha
1001 1.1 rearnsha if (!sc->sc_heldchange) {
1002 1.1 rearnsha if (sc->sc_tx_busy) {
1003 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1004 1.1 rearnsha sc->sc_tbc = 0;
1005 1.1 rearnsha sc->sc_heldchange = 1;
1006 1.1 rearnsha } else
1007 1.1 rearnsha plcom_loadchannelregs(sc);
1008 1.1 rearnsha }
1009 1.1 rearnsha }
1010 1.1 rearnsha
1011 1.1 rearnsha void
1012 1.1 rearnsha plcom_modem(struct plcom_softc *sc, int onoff)
1013 1.1 rearnsha {
1014 1.1 rearnsha
1015 1.1 rearnsha if (sc->sc_mcr_dtr == 0)
1016 1.1 rearnsha return;
1017 1.1 rearnsha
1018 1.1 rearnsha if (onoff)
1019 1.1 rearnsha SET(sc->sc_mcr, sc->sc_mcr_dtr);
1020 1.1 rearnsha else
1021 1.1 rearnsha CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1022 1.1 rearnsha
1023 1.1 rearnsha if (!sc->sc_heldchange) {
1024 1.1 rearnsha if (sc->sc_tx_busy) {
1025 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1026 1.1 rearnsha sc->sc_tbc = 0;
1027 1.1 rearnsha sc->sc_heldchange = 1;
1028 1.1 rearnsha } else
1029 1.1 rearnsha plcom_loadchannelregs(sc);
1030 1.1 rearnsha }
1031 1.1 rearnsha }
1032 1.1 rearnsha
1033 1.1 rearnsha void
1034 1.1 rearnsha tiocm_to_plcom(struct plcom_softc *sc, u_long how, int ttybits)
1035 1.1 rearnsha {
1036 1.1 rearnsha u_char plcombits;
1037 1.1 rearnsha
1038 1.1 rearnsha plcombits = 0;
1039 1.1 rearnsha if (ISSET(ttybits, TIOCM_DTR))
1040 1.35 skrll SET(plcombits, PL01X_MCR_DTR);
1041 1.1 rearnsha if (ISSET(ttybits, TIOCM_RTS))
1042 1.35 skrll SET(plcombits, PL01X_MCR_RTS);
1043 1.1 rearnsha
1044 1.1 rearnsha switch (how) {
1045 1.1 rearnsha case TIOCMBIC:
1046 1.1 rearnsha CLR(sc->sc_mcr, plcombits);
1047 1.1 rearnsha break;
1048 1.1 rearnsha
1049 1.1 rearnsha case TIOCMBIS:
1050 1.1 rearnsha SET(sc->sc_mcr, plcombits);
1051 1.1 rearnsha break;
1052 1.1 rearnsha
1053 1.1 rearnsha case TIOCMSET:
1054 1.35 skrll CLR(sc->sc_mcr, PL01X_MCR_DTR | PL01X_MCR_RTS);
1055 1.1 rearnsha SET(sc->sc_mcr, plcombits);
1056 1.1 rearnsha break;
1057 1.1 rearnsha }
1058 1.1 rearnsha
1059 1.1 rearnsha if (!sc->sc_heldchange) {
1060 1.1 rearnsha if (sc->sc_tx_busy) {
1061 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1062 1.1 rearnsha sc->sc_tbc = 0;
1063 1.1 rearnsha sc->sc_heldchange = 1;
1064 1.1 rearnsha } else
1065 1.1 rearnsha plcom_loadchannelregs(sc);
1066 1.1 rearnsha }
1067 1.1 rearnsha }
1068 1.1 rearnsha
1069 1.1 rearnsha int
1070 1.1 rearnsha plcom_to_tiocm(struct plcom_softc *sc)
1071 1.1 rearnsha {
1072 1.1 rearnsha u_char plcombits;
1073 1.1 rearnsha int ttybits = 0;
1074 1.1 rearnsha
1075 1.1 rearnsha plcombits = sc->sc_mcr;
1076 1.35 skrll if (ISSET(plcombits, PL01X_MCR_DTR))
1077 1.1 rearnsha SET(ttybits, TIOCM_DTR);
1078 1.35 skrll if (ISSET(plcombits, PL01X_MCR_RTS))
1079 1.1 rearnsha SET(ttybits, TIOCM_RTS);
1080 1.1 rearnsha
1081 1.1 rearnsha plcombits = sc->sc_msr;
1082 1.35 skrll if (ISSET(plcombits, PL01X_MSR_DCD))
1083 1.1 rearnsha SET(ttybits, TIOCM_CD);
1084 1.35 skrll if (ISSET(plcombits, PL01X_MSR_CTS))
1085 1.1 rearnsha SET(ttybits, TIOCM_CTS);
1086 1.35 skrll if (ISSET(plcombits, PL01X_MSR_DSR))
1087 1.1 rearnsha SET(ttybits, TIOCM_DSR);
1088 1.1 rearnsha
1089 1.1 rearnsha if (sc->sc_cr != 0)
1090 1.1 rearnsha SET(ttybits, TIOCM_LE);
1091 1.1 rearnsha
1092 1.1 rearnsha return ttybits;
1093 1.1 rearnsha }
1094 1.1 rearnsha
1095 1.1 rearnsha static u_char
1096 1.1 rearnsha cflag2lcr(tcflag_t cflag)
1097 1.1 rearnsha {
1098 1.1 rearnsha u_char lcr = 0;
1099 1.1 rearnsha
1100 1.1 rearnsha switch (ISSET(cflag, CSIZE)) {
1101 1.1 rearnsha case CS5:
1102 1.35 skrll SET(lcr, PL01X_LCR_5BITS);
1103 1.1 rearnsha break;
1104 1.1 rearnsha case CS6:
1105 1.35 skrll SET(lcr, PL01X_LCR_6BITS);
1106 1.1 rearnsha break;
1107 1.1 rearnsha case CS7:
1108 1.35 skrll SET(lcr, PL01X_LCR_7BITS);
1109 1.1 rearnsha break;
1110 1.1 rearnsha case CS8:
1111 1.35 skrll SET(lcr, PL01X_LCR_8BITS);
1112 1.1 rearnsha break;
1113 1.1 rearnsha }
1114 1.1 rearnsha if (ISSET(cflag, PARENB)) {
1115 1.35 skrll SET(lcr, PL01X_LCR_PEN);
1116 1.1 rearnsha if (!ISSET(cflag, PARODD))
1117 1.35 skrll SET(lcr, PL01X_LCR_EPS);
1118 1.1 rearnsha }
1119 1.1 rearnsha if (ISSET(cflag, CSTOPB))
1120 1.35 skrll SET(lcr, PL01X_LCR_STP2);
1121 1.1 rearnsha
1122 1.1 rearnsha return lcr;
1123 1.1 rearnsha }
1124 1.1 rearnsha
1125 1.1 rearnsha int
1126 1.1 rearnsha plcomparam(struct tty *tp, struct termios *t)
1127 1.1 rearnsha {
1128 1.28 cegger struct plcom_softc *sc =
1129 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1130 1.1 rearnsha int ospeed;
1131 1.1 rearnsha u_char lcr;
1132 1.1 rearnsha
1133 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1134 1.1 rearnsha return EIO;
1135 1.1 rearnsha
1136 1.1 rearnsha ospeed = plcomspeed(t->c_ospeed, sc->sc_frequency);
1137 1.1 rearnsha
1138 1.1 rearnsha /* Check requested parameters. */
1139 1.1 rearnsha if (ospeed < 0)
1140 1.1 rearnsha return EINVAL;
1141 1.1 rearnsha if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
1142 1.1 rearnsha return EINVAL;
1143 1.1 rearnsha
1144 1.1 rearnsha /*
1145 1.1 rearnsha * For the console, always force CLOCAL and !HUPCL, so that the port
1146 1.1 rearnsha * is always active.
1147 1.1 rearnsha */
1148 1.1 rearnsha if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
1149 1.1 rearnsha ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) {
1150 1.1 rearnsha SET(t->c_cflag, CLOCAL);
1151 1.1 rearnsha CLR(t->c_cflag, HUPCL);
1152 1.1 rearnsha }
1153 1.1 rearnsha
1154 1.1 rearnsha /*
1155 1.1 rearnsha * If there were no changes, don't do anything. This avoids dropping
1156 1.1 rearnsha * input and improves performance when all we did was frob things like
1157 1.1 rearnsha * VMIN and VTIME.
1158 1.1 rearnsha */
1159 1.1 rearnsha if (tp->t_ospeed == t->c_ospeed &&
1160 1.1 rearnsha tp->t_cflag == t->c_cflag)
1161 1.1 rearnsha return 0;
1162 1.1 rearnsha
1163 1.35 skrll lcr = ISSET(sc->sc_lcr, PL01X_LCR_BRK) | cflag2lcr(t->c_cflag);
1164 1.1 rearnsha
1165 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1166 1.1 rearnsha
1167 1.1 rearnsha sc->sc_lcr = lcr;
1168 1.1 rearnsha
1169 1.1 rearnsha /*
1170 1.1 rearnsha * PL010 has a fixed-length FIFO trigger point.
1171 1.1 rearnsha */
1172 1.1 rearnsha if (ISSET(sc->sc_hwflags, PLCOM_HW_FIFO))
1173 1.1 rearnsha sc->sc_fifo = 1;
1174 1.1 rearnsha else
1175 1.1 rearnsha sc->sc_fifo = 0;
1176 1.1 rearnsha
1177 1.1 rearnsha if (sc->sc_fifo)
1178 1.35 skrll SET(sc->sc_lcr, PL01X_LCR_FEN);
1179 1.1 rearnsha
1180 1.1 rearnsha /*
1181 1.1 rearnsha * If we're not in a mode that assumes a connection is present, then
1182 1.1 rearnsha * ignore carrier changes.
1183 1.1 rearnsha */
1184 1.1 rearnsha if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1185 1.1 rearnsha sc->sc_msr_dcd = 0;
1186 1.1 rearnsha else
1187 1.35 skrll sc->sc_msr_dcd = PL01X_MSR_DCD;
1188 1.1 rearnsha /*
1189 1.1 rearnsha * Set the flow control pins depending on the current flow control
1190 1.1 rearnsha * mode.
1191 1.1 rearnsha */
1192 1.1 rearnsha if (ISSET(t->c_cflag, CRTSCTS)) {
1193 1.35 skrll sc->sc_mcr_dtr = PL01X_MCR_DTR;
1194 1.35 skrll sc->sc_mcr_rts = PL01X_MCR_RTS;
1195 1.35 skrll sc->sc_msr_cts = PL01X_MSR_CTS;
1196 1.1 rearnsha } else if (ISSET(t->c_cflag, MDMBUF)) {
1197 1.1 rearnsha /*
1198 1.1 rearnsha * For DTR/DCD flow control, make sure we don't toggle DTR for
1199 1.1 rearnsha * carrier detection.
1200 1.1 rearnsha */
1201 1.1 rearnsha sc->sc_mcr_dtr = 0;
1202 1.35 skrll sc->sc_mcr_rts = PL01X_MCR_DTR;
1203 1.35 skrll sc->sc_msr_cts = PL01X_MSR_DCD;
1204 1.1 rearnsha } else {
1205 1.1 rearnsha /*
1206 1.1 rearnsha * If no flow control, then always set RTS. This will make
1207 1.1 rearnsha * the other side happy if it mistakenly thinks we're doing
1208 1.1 rearnsha * RTS/CTS flow control.
1209 1.1 rearnsha */
1210 1.35 skrll sc->sc_mcr_dtr = PL01X_MCR_DTR | PL01X_MCR_RTS;
1211 1.1 rearnsha sc->sc_mcr_rts = 0;
1212 1.1 rearnsha sc->sc_msr_cts = 0;
1213 1.35 skrll if (ISSET(sc->sc_mcr, PL01X_MCR_DTR))
1214 1.35 skrll SET(sc->sc_mcr, PL01X_MCR_RTS);
1215 1.1 rearnsha else
1216 1.35 skrll CLR(sc->sc_mcr, PL01X_MCR_RTS);
1217 1.1 rearnsha }
1218 1.1 rearnsha sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd;
1219 1.1 rearnsha
1220 1.1 rearnsha #if 0
1221 1.1 rearnsha if (ospeed == 0)
1222 1.1 rearnsha CLR(sc->sc_mcr, sc->sc_mcr_dtr);
1223 1.1 rearnsha else
1224 1.1 rearnsha SET(sc->sc_mcr, sc->sc_mcr_dtr);
1225 1.1 rearnsha #endif
1226 1.1 rearnsha
1227 1.1 rearnsha sc->sc_dlbl = ospeed;
1228 1.1 rearnsha sc->sc_dlbh = ospeed >> 8;
1229 1.1 rearnsha
1230 1.1 rearnsha /* And copy to tty. */
1231 1.1 rearnsha tp->t_ispeed = 0;
1232 1.1 rearnsha tp->t_ospeed = t->c_ospeed;
1233 1.1 rearnsha tp->t_cflag = t->c_cflag;
1234 1.1 rearnsha
1235 1.1 rearnsha if (!sc->sc_heldchange) {
1236 1.1 rearnsha if (sc->sc_tx_busy) {
1237 1.1 rearnsha sc->sc_heldtbc = sc->sc_tbc;
1238 1.1 rearnsha sc->sc_tbc = 0;
1239 1.1 rearnsha sc->sc_heldchange = 1;
1240 1.1 rearnsha } else
1241 1.1 rearnsha plcom_loadchannelregs(sc);
1242 1.1 rearnsha }
1243 1.1 rearnsha
1244 1.1 rearnsha if (!ISSET(t->c_cflag, CHWFLOW)) {
1245 1.1 rearnsha /* Disable the high water mark. */
1246 1.1 rearnsha sc->sc_r_hiwat = 0;
1247 1.1 rearnsha sc->sc_r_lowat = 0;
1248 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1249 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1250 1.1 rearnsha plcom_schedrx(sc);
1251 1.1 rearnsha }
1252 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1253 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1254 1.1 rearnsha plcom_hwiflow(sc);
1255 1.1 rearnsha }
1256 1.1 rearnsha } else {
1257 1.1 rearnsha sc->sc_r_hiwat = plcom_rbuf_hiwat;
1258 1.1 rearnsha sc->sc_r_lowat = plcom_rbuf_lowat;
1259 1.1 rearnsha }
1260 1.1 rearnsha
1261 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1262 1.1 rearnsha
1263 1.1 rearnsha /*
1264 1.1 rearnsha * Update the tty layer's idea of the carrier bit, in case we changed
1265 1.1 rearnsha * CLOCAL or MDMBUF. We don't hang up here; we only do that by
1266 1.1 rearnsha * explicit request.
1267 1.1 rearnsha */
1268 1.35 skrll (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, PL01X_MSR_DCD));
1269 1.1 rearnsha
1270 1.1 rearnsha #ifdef PLCOM_DEBUG
1271 1.1 rearnsha if (plcom_debug)
1272 1.1 rearnsha plcomstatus(sc, "plcomparam ");
1273 1.1 rearnsha #endif
1274 1.1 rearnsha
1275 1.1 rearnsha if (!ISSET(t->c_cflag, CHWFLOW)) {
1276 1.1 rearnsha if (sc->sc_tx_stopped) {
1277 1.1 rearnsha sc->sc_tx_stopped = 0;
1278 1.1 rearnsha plcomstart(tp);
1279 1.1 rearnsha }
1280 1.1 rearnsha }
1281 1.1 rearnsha
1282 1.1 rearnsha return 0;
1283 1.1 rearnsha }
1284 1.1 rearnsha
1285 1.1 rearnsha void
1286 1.1 rearnsha plcom_iflush(struct plcom_softc *sc)
1287 1.1 rearnsha {
1288 1.1 rearnsha bus_space_tag_t iot = sc->sc_iot;
1289 1.1 rearnsha bus_space_handle_t ioh = sc->sc_ioh;
1290 1.1 rearnsha #ifdef DIAGNOSTIC
1291 1.1 rearnsha int reg;
1292 1.1 rearnsha #endif
1293 1.1 rearnsha int timo;
1294 1.1 rearnsha
1295 1.1 rearnsha #ifdef DIAGNOSTIC
1296 1.1 rearnsha reg = 0xffff;
1297 1.1 rearnsha #endif
1298 1.1 rearnsha timo = 50000;
1299 1.1 rearnsha /* flush any pending I/O */
1300 1.35 skrll while (! ISSET(bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_RXFE)
1301 1.1 rearnsha && --timo)
1302 1.1 rearnsha #ifdef DIAGNOSTIC
1303 1.1 rearnsha reg =
1304 1.1 rearnsha #else
1305 1.1 rearnsha (void)
1306 1.1 rearnsha #endif
1307 1.1 rearnsha bus_space_read_1(iot, ioh, plcom_dr);
1308 1.1 rearnsha #ifdef DIAGNOSTIC
1309 1.1 rearnsha if (!timo)
1310 1.1 rearnsha printf("%s: plcom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
1311 1.1 rearnsha reg);
1312 1.1 rearnsha #endif
1313 1.1 rearnsha }
1314 1.1 rearnsha
1315 1.1 rearnsha void
1316 1.1 rearnsha plcom_loadchannelregs(struct plcom_softc *sc)
1317 1.1 rearnsha {
1318 1.1 rearnsha bus_space_tag_t iot = sc->sc_iot;
1319 1.1 rearnsha bus_space_handle_t ioh = sc->sc_ioh;
1320 1.1 rearnsha
1321 1.1 rearnsha /* XXXXX necessary? */
1322 1.1 rearnsha plcom_iflush(sc);
1323 1.1 rearnsha
1324 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, 0);
1325 1.1 rearnsha
1326 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_dlbl, sc->sc_dlbl);
1327 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_dlbh, sc->sc_dlbh);
1328 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_lcr, sc->sc_lcr);
1329 1.15 thorpej /* XXX device_unit() abuse */
1330 1.15 thorpej sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
1331 1.1 rearnsha sc->sc_mcr_active = sc->sc_mcr);
1332 1.1 rearnsha
1333 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
1334 1.1 rearnsha }
1335 1.1 rearnsha
1336 1.1 rearnsha int
1337 1.1 rearnsha plcomhwiflow(struct tty *tp, int block)
1338 1.1 rearnsha {
1339 1.28 cegger struct plcom_softc *sc =
1340 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1341 1.1 rearnsha
1342 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1343 1.1 rearnsha return 0;
1344 1.1 rearnsha
1345 1.1 rearnsha if (sc->sc_mcr_rts == 0)
1346 1.1 rearnsha return 0;
1347 1.1 rearnsha
1348 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1349 1.1 rearnsha
1350 1.1 rearnsha if (block) {
1351 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1352 1.1 rearnsha SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1353 1.1 rearnsha plcom_hwiflow(sc);
1354 1.1 rearnsha }
1355 1.1 rearnsha } else {
1356 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1357 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1358 1.1 rearnsha plcom_schedrx(sc);
1359 1.1 rearnsha }
1360 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1361 1.1 rearnsha CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1362 1.1 rearnsha plcom_hwiflow(sc);
1363 1.1 rearnsha }
1364 1.1 rearnsha }
1365 1.1 rearnsha
1366 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1367 1.1 rearnsha return 1;
1368 1.1 rearnsha }
1369 1.1 rearnsha
1370 1.1 rearnsha /*
1371 1.1 rearnsha * (un)block input via hw flowcontrol
1372 1.1 rearnsha */
1373 1.1 rearnsha void
1374 1.1 rearnsha plcom_hwiflow(struct plcom_softc *sc)
1375 1.1 rearnsha {
1376 1.1 rearnsha if (sc->sc_mcr_rts == 0)
1377 1.1 rearnsha return;
1378 1.1 rearnsha
1379 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) {
1380 1.1 rearnsha CLR(sc->sc_mcr, sc->sc_mcr_rts);
1381 1.1 rearnsha CLR(sc->sc_mcr_active, sc->sc_mcr_rts);
1382 1.1 rearnsha } else {
1383 1.1 rearnsha SET(sc->sc_mcr, sc->sc_mcr_rts);
1384 1.1 rearnsha SET(sc->sc_mcr_active, sc->sc_mcr_rts);
1385 1.1 rearnsha }
1386 1.15 thorpej /* XXX device_unit() abuse */
1387 1.15 thorpej sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
1388 1.1 rearnsha sc->sc_mcr_active);
1389 1.1 rearnsha }
1390 1.1 rearnsha
1391 1.1 rearnsha
1392 1.1 rearnsha void
1393 1.1 rearnsha plcomstart(struct tty *tp)
1394 1.1 rearnsha {
1395 1.28 cegger struct plcom_softc *sc =
1396 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1397 1.1 rearnsha bus_space_tag_t iot = sc->sc_iot;
1398 1.1 rearnsha bus_space_handle_t ioh = sc->sc_ioh;
1399 1.1 rearnsha int s;
1400 1.1 rearnsha
1401 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1402 1.1 rearnsha return;
1403 1.1 rearnsha
1404 1.1 rearnsha s = spltty();
1405 1.1 rearnsha if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1406 1.1 rearnsha goto out;
1407 1.1 rearnsha if (sc->sc_tx_stopped)
1408 1.1 rearnsha goto out;
1409 1.1 rearnsha
1410 1.24 ad if (!ttypull(tp))
1411 1.24 ad goto out;
1412 1.1 rearnsha
1413 1.1 rearnsha /* Grab the first contiguous region of buffer space. */
1414 1.1 rearnsha {
1415 1.1 rearnsha u_char *tba;
1416 1.1 rearnsha int tbc;
1417 1.1 rearnsha
1418 1.1 rearnsha tba = tp->t_outq.c_cf;
1419 1.1 rearnsha tbc = ndqb(&tp->t_outq, 0);
1420 1.1 rearnsha
1421 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1422 1.1 rearnsha
1423 1.1 rearnsha sc->sc_tba = tba;
1424 1.1 rearnsha sc->sc_tbc = tbc;
1425 1.1 rearnsha }
1426 1.1 rearnsha
1427 1.1 rearnsha SET(tp->t_state, TS_BUSY);
1428 1.1 rearnsha sc->sc_tx_busy = 1;
1429 1.1 rearnsha
1430 1.1 rearnsha /* Enable transmit completion interrupts if necessary. */
1431 1.35 skrll if (!ISSET(sc->sc_cr, PL010_CR_TIE)) {
1432 1.35 skrll SET(sc->sc_cr, PL010_CR_TIE);
1433 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr);
1434 1.1 rearnsha }
1435 1.1 rearnsha
1436 1.1 rearnsha /* Output the first chunk of the contiguous buffer. */
1437 1.1 rearnsha {
1438 1.1 rearnsha int n;
1439 1.1 rearnsha
1440 1.1 rearnsha n = sc->sc_tbc;
1441 1.1 rearnsha if (n > sc->sc_fifolen)
1442 1.1 rearnsha n = sc->sc_fifolen;
1443 1.1 rearnsha bus_space_write_multi_1(iot, ioh, plcom_dr, sc->sc_tba, n);
1444 1.1 rearnsha sc->sc_tbc -= n;
1445 1.1 rearnsha sc->sc_tba += n;
1446 1.1 rearnsha }
1447 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1448 1.1 rearnsha out:
1449 1.1 rearnsha splx(s);
1450 1.1 rearnsha return;
1451 1.1 rearnsha }
1452 1.1 rearnsha
1453 1.1 rearnsha /*
1454 1.1 rearnsha * Stop output on a line.
1455 1.1 rearnsha */
1456 1.1 rearnsha void
1457 1.1 rearnsha plcomstop(struct tty *tp, int flag)
1458 1.1 rearnsha {
1459 1.28 cegger struct plcom_softc *sc =
1460 1.28 cegger device_lookup_private(&plcom_cd, PLCOMUNIT(tp->t_dev));
1461 1.1 rearnsha
1462 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1463 1.1 rearnsha if (ISSET(tp->t_state, TS_BUSY)) {
1464 1.1 rearnsha /* Stop transmitting at the next chunk. */
1465 1.1 rearnsha sc->sc_tbc = 0;
1466 1.1 rearnsha sc->sc_heldtbc = 0;
1467 1.1 rearnsha if (!ISSET(tp->t_state, TS_TTSTOP))
1468 1.1 rearnsha SET(tp->t_state, TS_FLUSH);
1469 1.1 rearnsha }
1470 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1471 1.1 rearnsha }
1472 1.1 rearnsha
1473 1.1 rearnsha void
1474 1.1 rearnsha plcomdiag(void *arg)
1475 1.1 rearnsha {
1476 1.1 rearnsha struct plcom_softc *sc = arg;
1477 1.1 rearnsha int overflows, floods;
1478 1.1 rearnsha
1479 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1480 1.1 rearnsha overflows = sc->sc_overflows;
1481 1.1 rearnsha sc->sc_overflows = 0;
1482 1.1 rearnsha floods = sc->sc_floods;
1483 1.1 rearnsha sc->sc_floods = 0;
1484 1.1 rearnsha sc->sc_errors = 0;
1485 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1486 1.1 rearnsha
1487 1.1 rearnsha log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1488 1.1 rearnsha sc->sc_dev.dv_xname,
1489 1.1 rearnsha overflows, overflows == 1 ? "" : "s",
1490 1.1 rearnsha floods, floods == 1 ? "" : "s");
1491 1.1 rearnsha }
1492 1.1 rearnsha
1493 1.1 rearnsha integrate void
1494 1.1 rearnsha plcom_rxsoft(struct plcom_softc *sc, struct tty *tp)
1495 1.1 rearnsha {
1496 1.1 rearnsha int (*rint) (int, struct tty *) = tp->t_linesw->l_rint;
1497 1.1 rearnsha u_char *get, *end;
1498 1.1 rearnsha u_int cc, scc;
1499 1.1 rearnsha u_char rsr;
1500 1.1 rearnsha int code;
1501 1.1 rearnsha
1502 1.1 rearnsha end = sc->sc_ebuf;
1503 1.1 rearnsha get = sc->sc_rbget;
1504 1.1 rearnsha scc = cc = plcom_rbuf_size - sc->sc_rbavail;
1505 1.1 rearnsha
1506 1.1 rearnsha if (cc == plcom_rbuf_size) {
1507 1.1 rearnsha sc->sc_floods++;
1508 1.1 rearnsha if (sc->sc_errors++ == 0)
1509 1.1 rearnsha callout_reset(&sc->sc_diag_callout, 60 * hz,
1510 1.1 rearnsha plcomdiag, sc);
1511 1.1 rearnsha }
1512 1.1 rearnsha
1513 1.1 rearnsha while (cc) {
1514 1.1 rearnsha code = get[0];
1515 1.1 rearnsha rsr = get[1];
1516 1.35 skrll if (ISSET(rsr, PL01X_RSR_ERROR)) {
1517 1.35 skrll if (ISSET(rsr, PL01X_RSR_OE)) {
1518 1.1 rearnsha sc->sc_overflows++;
1519 1.1 rearnsha if (sc->sc_errors++ == 0)
1520 1.1 rearnsha callout_reset(&sc->sc_diag_callout,
1521 1.1 rearnsha 60 * hz, plcomdiag, sc);
1522 1.1 rearnsha }
1523 1.35 skrll if (ISSET(rsr, PL01X_RSR_BE | PL01X_RSR_FE))
1524 1.1 rearnsha SET(code, TTY_FE);
1525 1.35 skrll if (ISSET(rsr, PL01X_RSR_PE))
1526 1.1 rearnsha SET(code, TTY_PE);
1527 1.1 rearnsha }
1528 1.1 rearnsha if ((*rint)(code, tp) == -1) {
1529 1.1 rearnsha /*
1530 1.1 rearnsha * The line discipline's buffer is out of space.
1531 1.1 rearnsha */
1532 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1533 1.1 rearnsha /*
1534 1.1 rearnsha * We're either not using flow control, or the
1535 1.1 rearnsha * line discipline didn't tell us to block for
1536 1.1 rearnsha * some reason. Either way, we have no way to
1537 1.1 rearnsha * know when there's more space available, so
1538 1.1 rearnsha * just drop the rest of the data.
1539 1.1 rearnsha */
1540 1.1 rearnsha get += cc << 1;
1541 1.1 rearnsha if (get >= end)
1542 1.1 rearnsha get -= plcom_rbuf_size << 1;
1543 1.1 rearnsha cc = 0;
1544 1.1 rearnsha } else {
1545 1.1 rearnsha /*
1546 1.1 rearnsha * Don't schedule any more receive processing
1547 1.1 rearnsha * until the line discipline tells us there's
1548 1.1 rearnsha * space available (through plcomhwiflow()).
1549 1.1 rearnsha * Leave the rest of the data in the input
1550 1.1 rearnsha * buffer.
1551 1.1 rearnsha */
1552 1.1 rearnsha SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1553 1.1 rearnsha }
1554 1.1 rearnsha break;
1555 1.1 rearnsha }
1556 1.1 rearnsha get += 2;
1557 1.1 rearnsha if (get >= end)
1558 1.1 rearnsha get = sc->sc_rbuf;
1559 1.1 rearnsha cc--;
1560 1.1 rearnsha }
1561 1.1 rearnsha
1562 1.1 rearnsha if (cc != scc) {
1563 1.1 rearnsha sc->sc_rbget = get;
1564 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1565 1.36 skrll
1566 1.1 rearnsha cc = sc->sc_rbavail += scc - cc;
1567 1.1 rearnsha /* Buffers should be ok again, release possible block. */
1568 1.1 rearnsha if (cc >= sc->sc_r_lowat) {
1569 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1570 1.1 rearnsha CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1571 1.35 skrll SET(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
1572 1.1 rearnsha }
1573 1.1 rearnsha if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1574 1.1 rearnsha CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1575 1.1 rearnsha plcom_hwiflow(sc);
1576 1.1 rearnsha }
1577 1.1 rearnsha }
1578 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1579 1.1 rearnsha }
1580 1.1 rearnsha }
1581 1.1 rearnsha
1582 1.1 rearnsha integrate void
1583 1.1 rearnsha plcom_txsoft(struct plcom_softc *sc, struct tty *tp)
1584 1.1 rearnsha {
1585 1.1 rearnsha
1586 1.1 rearnsha CLR(tp->t_state, TS_BUSY);
1587 1.1 rearnsha if (ISSET(tp->t_state, TS_FLUSH))
1588 1.1 rearnsha CLR(tp->t_state, TS_FLUSH);
1589 1.1 rearnsha else
1590 1.1 rearnsha ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1591 1.1 rearnsha (*tp->t_linesw->l_start)(tp);
1592 1.1 rearnsha }
1593 1.1 rearnsha
1594 1.1 rearnsha integrate void
1595 1.1 rearnsha plcom_stsoft(struct plcom_softc *sc, struct tty *tp)
1596 1.1 rearnsha {
1597 1.1 rearnsha u_char msr, delta;
1598 1.1 rearnsha
1599 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1600 1.1 rearnsha msr = sc->sc_msr;
1601 1.1 rearnsha delta = sc->sc_msr_delta;
1602 1.1 rearnsha sc->sc_msr_delta = 0;
1603 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1604 1.1 rearnsha
1605 1.1 rearnsha if (ISSET(delta, sc->sc_msr_dcd)) {
1606 1.1 rearnsha /*
1607 1.1 rearnsha * Inform the tty layer that carrier detect changed.
1608 1.1 rearnsha */
1609 1.35 skrll (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, PL01X_MSR_DCD));
1610 1.1 rearnsha }
1611 1.1 rearnsha
1612 1.1 rearnsha if (ISSET(delta, sc->sc_msr_cts)) {
1613 1.1 rearnsha /* Block or unblock output according to flow control. */
1614 1.1 rearnsha if (ISSET(msr, sc->sc_msr_cts)) {
1615 1.1 rearnsha sc->sc_tx_stopped = 0;
1616 1.1 rearnsha (*tp->t_linesw->l_start)(tp);
1617 1.1 rearnsha } else {
1618 1.1 rearnsha sc->sc_tx_stopped = 1;
1619 1.1 rearnsha }
1620 1.1 rearnsha }
1621 1.1 rearnsha
1622 1.1 rearnsha #ifdef PLCOM_DEBUG
1623 1.1 rearnsha if (plcom_debug)
1624 1.1 rearnsha plcomstatus(sc, "plcom_stsoft");
1625 1.1 rearnsha #endif
1626 1.1 rearnsha }
1627 1.1 rearnsha
1628 1.1 rearnsha void
1629 1.1 rearnsha plcomsoft(void *arg)
1630 1.1 rearnsha {
1631 1.1 rearnsha struct plcom_softc *sc = arg;
1632 1.1 rearnsha struct tty *tp;
1633 1.1 rearnsha
1634 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1635 1.1 rearnsha return;
1636 1.1 rearnsha
1637 1.21 ad tp = sc->sc_tty;
1638 1.1 rearnsha
1639 1.21 ad if (sc->sc_rx_ready) {
1640 1.21 ad sc->sc_rx_ready = 0;
1641 1.21 ad plcom_rxsoft(sc, tp);
1642 1.21 ad }
1643 1.1 rearnsha
1644 1.21 ad if (sc->sc_st_check) {
1645 1.21 ad sc->sc_st_check = 0;
1646 1.21 ad plcom_stsoft(sc, tp);
1647 1.21 ad }
1648 1.1 rearnsha
1649 1.21 ad if (sc->sc_tx_done) {
1650 1.21 ad sc->sc_tx_done = 0;
1651 1.21 ad plcom_txsoft(sc, tp);
1652 1.1 rearnsha }
1653 1.1 rearnsha }
1654 1.1 rearnsha
1655 1.1 rearnsha int
1656 1.1 rearnsha plcomintr(void *arg)
1657 1.1 rearnsha {
1658 1.1 rearnsha struct plcom_softc *sc = arg;
1659 1.1 rearnsha bus_space_tag_t iot = sc->sc_iot;
1660 1.1 rearnsha bus_space_handle_t ioh = sc->sc_ioh;
1661 1.1 rearnsha u_char *put, *end;
1662 1.1 rearnsha u_int cc;
1663 1.1 rearnsha u_char rsr, iir;
1664 1.1 rearnsha
1665 1.1 rearnsha if (PLCOM_ISALIVE(sc) == 0)
1666 1.1 rearnsha return 0;
1667 1.1 rearnsha
1668 1.36 skrll mutex_spin_enter(&sc->sc_lock);
1669 1.1 rearnsha iir = bus_space_read_1(iot, ioh, plcom_iir);
1670 1.35 skrll if (! ISSET(iir, PL010_IIR_IMASK)) {
1671 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1672 1.1 rearnsha return 0;
1673 1.1 rearnsha }
1674 1.1 rearnsha
1675 1.1 rearnsha end = sc->sc_ebuf;
1676 1.1 rearnsha put = sc->sc_rbput;
1677 1.1 rearnsha cc = sc->sc_rbavail;
1678 1.1 rearnsha
1679 1.1 rearnsha do {
1680 1.1 rearnsha u_char msr, delta, fr;
1681 1.1 rearnsha
1682 1.1 rearnsha fr = bus_space_read_1(iot, ioh, plcom_fr);
1683 1.1 rearnsha
1684 1.35 skrll if (!ISSET(fr, PL01X_FR_RXFE) &&
1685 1.1 rearnsha !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1686 1.1 rearnsha while (cc > 0) {
1687 1.1 rearnsha int cn_trapped = 0;
1688 1.1 rearnsha put[0] = bus_space_read_1(iot, ioh,
1689 1.1 rearnsha plcom_dr);
1690 1.1 rearnsha rsr = bus_space_read_1(iot, ioh, plcom_rsr);
1691 1.1 rearnsha /* Clear any error status. */
1692 1.35 skrll if (ISSET(rsr, PL01X_RSR_ERROR))
1693 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_ecr,
1694 1.1 rearnsha 0);
1695 1.35 skrll if (ISSET(rsr, PL01X_RSR_BE)) {
1696 1.10 rearnsha cn_trapped = 0;
1697 1.1 rearnsha cn_check_magic(sc->sc_tty->t_dev,
1698 1.1 rearnsha CNC_BREAK, plcom_cnm_state);
1699 1.1 rearnsha if (cn_trapped)
1700 1.1 rearnsha continue;
1701 1.1 rearnsha #if defined(KGDB)
1702 1.1 rearnsha if (ISSET(sc->sc_hwflags,
1703 1.1 rearnsha PLCOM_HW_KGDB)) {
1704 1.1 rearnsha kgdb_connect(1);
1705 1.1 rearnsha continue;
1706 1.1 rearnsha }
1707 1.1 rearnsha #endif
1708 1.1 rearnsha }
1709 1.1 rearnsha
1710 1.1 rearnsha put[1] = rsr;
1711 1.10 rearnsha cn_trapped = 0;
1712 1.1 rearnsha cn_check_magic(sc->sc_tty->t_dev,
1713 1.1 rearnsha put[0], plcom_cnm_state);
1714 1.1 rearnsha if (cn_trapped) {
1715 1.1 rearnsha fr = bus_space_read_1(iot, ioh,
1716 1.1 rearnsha plcom_fr);
1717 1.35 skrll if (ISSET(fr, PL01X_FR_RXFE))
1718 1.1 rearnsha break;
1719 1.1 rearnsha
1720 1.1 rearnsha continue;
1721 1.1 rearnsha }
1722 1.1 rearnsha put += 2;
1723 1.1 rearnsha if (put >= end)
1724 1.1 rearnsha put = sc->sc_rbuf;
1725 1.1 rearnsha cc--;
1726 1.1 rearnsha
1727 1.1 rearnsha fr = bus_space_read_1(iot, ioh, plcom_fr);
1728 1.35 skrll if (ISSET(fr, PL01X_FR_RXFE))
1729 1.1 rearnsha break;
1730 1.1 rearnsha }
1731 1.1 rearnsha
1732 1.1 rearnsha /*
1733 1.1 rearnsha * Current string of incoming characters ended because
1734 1.1 rearnsha * no more data was available or we ran out of space.
1735 1.1 rearnsha * Schedule a receive event if any data was received.
1736 1.1 rearnsha * If we're out of space, turn off receive interrupts.
1737 1.1 rearnsha */
1738 1.1 rearnsha sc->sc_rbput = put;
1739 1.1 rearnsha sc->sc_rbavail = cc;
1740 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1741 1.1 rearnsha sc->sc_rx_ready = 1;
1742 1.1 rearnsha
1743 1.1 rearnsha /*
1744 1.1 rearnsha * See if we are in danger of overflowing a buffer. If
1745 1.1 rearnsha * so, use hardware flow control to ease the pressure.
1746 1.1 rearnsha */
1747 1.1 rearnsha if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1748 1.1 rearnsha cc < sc->sc_r_hiwat) {
1749 1.1 rearnsha SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1750 1.1 rearnsha plcom_hwiflow(sc);
1751 1.1 rearnsha }
1752 1.1 rearnsha
1753 1.1 rearnsha /*
1754 1.1 rearnsha * If we're out of space, disable receive interrupts
1755 1.1 rearnsha * until the queue has drained a bit.
1756 1.1 rearnsha */
1757 1.1 rearnsha if (!cc) {
1758 1.1 rearnsha SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1759 1.35 skrll CLR(sc->sc_cr, PL010_CR_RIE | PL010_CR_RTIE);
1760 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr,
1761 1.1 rearnsha sc->sc_cr);
1762 1.1 rearnsha }
1763 1.1 rearnsha } else {
1764 1.35 skrll if (ISSET(iir, PL010_IIR_RIS)) {
1765 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, 0);
1766 1.1 rearnsha delay(10);
1767 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr,
1768 1.1 rearnsha sc->sc_cr);
1769 1.1 rearnsha continue;
1770 1.1 rearnsha }
1771 1.1 rearnsha }
1772 1.1 rearnsha
1773 1.1 rearnsha msr = bus_space_read_1(iot, ioh, plcom_fr);
1774 1.1 rearnsha delta = msr ^ sc->sc_msr;
1775 1.1 rearnsha sc->sc_msr = msr;
1776 1.1 rearnsha /* Clear any pending modem status interrupt. */
1777 1.35 skrll if (iir & PL010_IIR_MIS)
1778 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_icr, 0);
1779 1.1 rearnsha /*
1780 1.1 rearnsha * Pulse-per-second (PSS) signals on edge of DCD?
1781 1.1 rearnsha * Process these even if line discipline is ignoring DCD.
1782 1.1 rearnsha */
1783 1.1 rearnsha if (delta & sc->sc_ppsmask) {
1784 1.1 rearnsha struct timeval tv;
1785 1.26 ad mutex_spin_enter(&timecounter_lock);
1786 1.1 rearnsha if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) {
1787 1.1 rearnsha /* XXX nanotime() */
1788 1.1 rearnsha microtime(&tv);
1789 1.1 rearnsha TIMEVAL_TO_TIMESPEC(&tv,
1790 1.1 rearnsha &sc->ppsinfo.assert_timestamp);
1791 1.1 rearnsha if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1792 1.1 rearnsha timespecadd(&sc->ppsinfo.assert_timestamp,
1793 1.1 rearnsha &sc->ppsparam.assert_offset,
1794 1.1 rearnsha &sc->ppsinfo.assert_timestamp);
1795 1.1 rearnsha }
1796 1.1 rearnsha
1797 1.1 rearnsha #ifdef PPS_SYNC
1798 1.1 rearnsha if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1799 1.1 rearnsha hardpps(&tv, tv.tv_usec);
1800 1.1 rearnsha #endif
1801 1.1 rearnsha sc->ppsinfo.assert_sequence++;
1802 1.1 rearnsha sc->ppsinfo.current_mode = sc->ppsparam.mode;
1803 1.1 rearnsha
1804 1.1 rearnsha } else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) {
1805 1.1 rearnsha /* XXX nanotime() */
1806 1.1 rearnsha microtime(&tv);
1807 1.1 rearnsha TIMEVAL_TO_TIMESPEC(&tv,
1808 1.1 rearnsha &sc->ppsinfo.clear_timestamp);
1809 1.1 rearnsha if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1810 1.1 rearnsha timespecadd(&sc->ppsinfo.clear_timestamp,
1811 1.1 rearnsha &sc->ppsparam.clear_offset,
1812 1.1 rearnsha &sc->ppsinfo.clear_timestamp);
1813 1.1 rearnsha }
1814 1.1 rearnsha
1815 1.1 rearnsha #ifdef PPS_SYNC
1816 1.1 rearnsha if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1817 1.1 rearnsha hardpps(&tv, tv.tv_usec);
1818 1.1 rearnsha #endif
1819 1.1 rearnsha sc->ppsinfo.clear_sequence++;
1820 1.1 rearnsha sc->ppsinfo.current_mode = sc->ppsparam.mode;
1821 1.1 rearnsha }
1822 1.26 ad mutex_spin_exit(&timecounter_lock);
1823 1.1 rearnsha }
1824 1.1 rearnsha
1825 1.1 rearnsha /*
1826 1.1 rearnsha * Process normal status changes
1827 1.1 rearnsha */
1828 1.1 rearnsha if (ISSET(delta, sc->sc_msr_mask)) {
1829 1.1 rearnsha SET(sc->sc_msr_delta, delta);
1830 1.1 rearnsha
1831 1.1 rearnsha /*
1832 1.1 rearnsha * Stop output immediately if we lose the output
1833 1.1 rearnsha * flow control signal or carrier detect.
1834 1.1 rearnsha */
1835 1.1 rearnsha if (ISSET(~msr, sc->sc_msr_mask)) {
1836 1.1 rearnsha sc->sc_tbc = 0;
1837 1.1 rearnsha sc->sc_heldtbc = 0;
1838 1.1 rearnsha #ifdef PLCOM_DEBUG
1839 1.1 rearnsha if (plcom_debug)
1840 1.1 rearnsha plcomstatus(sc, "plcomintr ");
1841 1.1 rearnsha #endif
1842 1.1 rearnsha }
1843 1.1 rearnsha
1844 1.1 rearnsha sc->sc_st_check = 1;
1845 1.1 rearnsha }
1846 1.1 rearnsha
1847 1.1 rearnsha /*
1848 1.1 rearnsha * Done handling any receive interrupts. See if data
1849 1.1 rearnsha * can be * transmitted as well. Schedule tx done
1850 1.1 rearnsha * event if no data left * and tty was marked busy.
1851 1.1 rearnsha */
1852 1.35 skrll if (ISSET(iir, PL010_IIR_TIS)) {
1853 1.1 rearnsha /*
1854 1.1 rearnsha * If we've delayed a parameter change, do it
1855 1.1 rearnsha * now, and restart * output.
1856 1.1 rearnsha */
1857 1.1 rearnsha if (sc->sc_heldchange) {
1858 1.1 rearnsha plcom_loadchannelregs(sc);
1859 1.1 rearnsha sc->sc_heldchange = 0;
1860 1.1 rearnsha sc->sc_tbc = sc->sc_heldtbc;
1861 1.1 rearnsha sc->sc_heldtbc = 0;
1862 1.1 rearnsha }
1863 1.1 rearnsha
1864 1.1 rearnsha /*
1865 1.1 rearnsha * Output the next chunk of the contiguous
1866 1.1 rearnsha * buffer, if any.
1867 1.1 rearnsha */
1868 1.1 rearnsha if (sc->sc_tbc > 0) {
1869 1.1 rearnsha int n;
1870 1.1 rearnsha
1871 1.1 rearnsha n = sc->sc_tbc;
1872 1.1 rearnsha if (n > sc->sc_fifolen)
1873 1.1 rearnsha n = sc->sc_fifolen;
1874 1.1 rearnsha bus_space_write_multi_1(iot, ioh, plcom_dr,
1875 1.1 rearnsha sc->sc_tba, n);
1876 1.1 rearnsha sc->sc_tbc -= n;
1877 1.1 rearnsha sc->sc_tba += n;
1878 1.1 rearnsha } else {
1879 1.1 rearnsha /*
1880 1.1 rearnsha * Disable transmit plcompletion
1881 1.1 rearnsha * interrupts if necessary.
1882 1.1 rearnsha */
1883 1.35 skrll if (ISSET(sc->sc_cr, PL010_CR_TIE)) {
1884 1.35 skrll CLR(sc->sc_cr, PL010_CR_TIE);
1885 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr,
1886 1.1 rearnsha sc->sc_cr);
1887 1.1 rearnsha }
1888 1.1 rearnsha if (sc->sc_tx_busy) {
1889 1.1 rearnsha sc->sc_tx_busy = 0;
1890 1.1 rearnsha sc->sc_tx_done = 1;
1891 1.1 rearnsha }
1892 1.1 rearnsha }
1893 1.1 rearnsha }
1894 1.1 rearnsha } while (ISSET((iir = bus_space_read_1(iot, ioh, plcom_iir)),
1895 1.35 skrll PL010_IIR_IMASK));
1896 1.1 rearnsha
1897 1.36 skrll mutex_spin_exit(&sc->sc_lock);
1898 1.1 rearnsha
1899 1.1 rearnsha /* Wake up the poller. */
1900 1.25 ad softint_schedule(sc->sc_si);
1901 1.1 rearnsha
1902 1.33 tls #ifdef RND_COM
1903 1.1 rearnsha rnd_add_uint32(&sc->rnd_source, iir | rsr);
1904 1.1 rearnsha #endif
1905 1.1 rearnsha
1906 1.1 rearnsha return 1;
1907 1.1 rearnsha }
1908 1.1 rearnsha
1909 1.1 rearnsha /*
1910 1.1 rearnsha * The following functions are polled getc and putc routines, shared
1911 1.1 rearnsha * by the console and kgdb glue.
1912 1.1 rearnsha *
1913 1.1 rearnsha * The read-ahead code is so that you can detect pending in-band
1914 1.1 rearnsha * cn_magic in polled mode while doing output rather than having to
1915 1.1 rearnsha * wait until the kernel decides it needs input.
1916 1.1 rearnsha */
1917 1.1 rearnsha
1918 1.1 rearnsha #define MAX_READAHEAD 20
1919 1.1 rearnsha static int plcom_readahead[MAX_READAHEAD];
1920 1.1 rearnsha static int plcom_readaheadcount = 0;
1921 1.1 rearnsha
1922 1.1 rearnsha int
1923 1.1 rearnsha plcom_common_getc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh)
1924 1.1 rearnsha {
1925 1.1 rearnsha int s = splserial();
1926 1.1 rearnsha u_char stat, c;
1927 1.1 rearnsha
1928 1.1 rearnsha /* got a character from reading things earlier */
1929 1.1 rearnsha if (plcom_readaheadcount > 0) {
1930 1.1 rearnsha int i;
1931 1.1 rearnsha
1932 1.1 rearnsha c = plcom_readahead[0];
1933 1.1 rearnsha for (i = 1; i < plcom_readaheadcount; i++) {
1934 1.1 rearnsha plcom_readahead[i-1] = plcom_readahead[i];
1935 1.1 rearnsha }
1936 1.1 rearnsha plcom_readaheadcount--;
1937 1.1 rearnsha splx(s);
1938 1.1 rearnsha return c;
1939 1.1 rearnsha }
1940 1.1 rearnsha
1941 1.1 rearnsha /* block until a character becomes available */
1942 1.35 skrll while (ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_RXFE))
1943 1.1 rearnsha ;
1944 1.1 rearnsha
1945 1.1 rearnsha c = bus_space_read_1(iot, ioh, plcom_dr);
1946 1.1 rearnsha stat = bus_space_read_1(iot, ioh, plcom_iir);
1947 1.1 rearnsha {
1948 1.1 rearnsha int cn_trapped = 0; /* unused */
1949 1.1 rearnsha #ifdef DDB
1950 1.1 rearnsha extern int db_active;
1951 1.1 rearnsha if (!db_active)
1952 1.1 rearnsha #endif
1953 1.1 rearnsha cn_check_magic(dev, c, plcom_cnm_state);
1954 1.1 rearnsha }
1955 1.1 rearnsha splx(s);
1956 1.1 rearnsha return c;
1957 1.1 rearnsha }
1958 1.1 rearnsha
1959 1.1 rearnsha void
1960 1.1 rearnsha plcom_common_putc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh,
1961 1.1 rearnsha int c)
1962 1.1 rearnsha {
1963 1.1 rearnsha int s = splserial();
1964 1.1 rearnsha int timo;
1965 1.1 rearnsha
1966 1.1 rearnsha int cin, stat;
1967 1.1 rearnsha if (plcom_readaheadcount < MAX_READAHEAD
1968 1.35 skrll && !ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_RXFE)) {
1969 1.1 rearnsha int cn_trapped = 0;
1970 1.1 rearnsha cin = bus_space_read_1(iot, ioh, plcom_dr);
1971 1.1 rearnsha stat = bus_space_read_1(iot, ioh, plcom_iir);
1972 1.1 rearnsha cn_check_magic(dev, cin, plcom_cnm_state);
1973 1.1 rearnsha plcom_readahead[plcom_readaheadcount++] = cin;
1974 1.1 rearnsha }
1975 1.1 rearnsha
1976 1.1 rearnsha /* wait for any pending transmission to finish */
1977 1.1 rearnsha timo = 150000;
1978 1.35 skrll while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_TXFE) && --timo)
1979 1.1 rearnsha continue;
1980 1.1 rearnsha
1981 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_dr, c);
1982 1.1 rearnsha PLCOM_BARRIER(iot, ioh, BR | BW);
1983 1.1 rearnsha
1984 1.1 rearnsha /* wait for this transmission to complete */
1985 1.1 rearnsha timo = 1500000;
1986 1.35 skrll while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), PL01X_FR_TXFE) && --timo)
1987 1.1 rearnsha continue;
1988 1.1 rearnsha
1989 1.1 rearnsha splx(s);
1990 1.1 rearnsha }
1991 1.1 rearnsha
1992 1.1 rearnsha /*
1993 1.1 rearnsha * Initialize UART for use as console or KGDB line.
1994 1.1 rearnsha */
1995 1.1 rearnsha int
1996 1.1 rearnsha plcominit(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
1997 1.1 rearnsha tcflag_t cflag, bus_space_handle_t *iohp)
1998 1.1 rearnsha {
1999 1.1 rearnsha bus_space_handle_t ioh;
2000 1.1 rearnsha
2001 1.1 rearnsha if (bus_space_map(iot, iobase, PLCOM_UART_SIZE, 0, &ioh))
2002 1.1 rearnsha return ENOMEM; /* ??? */
2003 1.1 rearnsha
2004 1.1 rearnsha rate = plcomspeed(rate, frequency);
2005 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_cr, 0);
2006 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_dlbl, rate);
2007 1.1 rearnsha bus_space_write_1(iot, ioh, plcom_dlbh, rate >> 8);
2008 1.35 skrll bus_space_write_1(iot, ioh, plcom_lcr, cflag2lcr(cflag) | PL01X_LCR_FEN);
2009 1.35 skrll bus_space_write_1(iot, ioh, plcom_cr, PL01X_CR_UARTEN);
2010 1.1 rearnsha
2011 1.1 rearnsha #if 0
2012 1.1 rearnsha /* Ought to do something like this, but we have no sc to
2013 1.1 rearnsha dereference. */
2014 1.15 thorpej /* XXX device_unit() abuse */
2015 1.15 thorpej sc->sc_set_mcr(sc->sc_set_mcr_arg, device_unit(&sc->sc_dev),
2016 1.35 skrll PL01X_MCR_DTR | PL01X_MCR_RTS);
2017 1.1 rearnsha #endif
2018 1.1 rearnsha
2019 1.1 rearnsha *iohp = ioh;
2020 1.1 rearnsha return 0;
2021 1.1 rearnsha }
2022 1.1 rearnsha
2023 1.1 rearnsha /*
2024 1.1 rearnsha * Following are all routines needed for PLCOM to act as console
2025 1.1 rearnsha */
2026 1.1 rearnsha struct consdev plcomcons = {
2027 1.1 rearnsha NULL, NULL, plcomcngetc, plcomcnputc, plcomcnpollc, NULL,
2028 1.6 he NULL, NULL, NODEV, CN_NORMAL
2029 1.1 rearnsha };
2030 1.1 rearnsha
2031 1.1 rearnsha
2032 1.1 rearnsha int
2033 1.1 rearnsha plcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency,
2034 1.1 rearnsha tcflag_t cflag, int unit)
2035 1.1 rearnsha {
2036 1.1 rearnsha int res;
2037 1.1 rearnsha
2038 1.1 rearnsha res = plcominit(iot, iobase, rate, frequency, cflag, &plcomconsioh);
2039 1.1 rearnsha if (res)
2040 1.1 rearnsha return res;
2041 1.1 rearnsha
2042 1.1 rearnsha cn_tab = &plcomcons;
2043 1.1 rearnsha cn_init_magic(&plcom_cnm_state);
2044 1.1 rearnsha cn_set_magic("\047\001"); /* default magic is BREAK */
2045 1.1 rearnsha
2046 1.1 rearnsha plcomconstag = iot;
2047 1.1 rearnsha plcomconsunit = unit;
2048 1.1 rearnsha plcomconsrate = rate;
2049 1.1 rearnsha plcomconscflag = cflag;
2050 1.1 rearnsha
2051 1.1 rearnsha return 0;
2052 1.1 rearnsha }
2053 1.1 rearnsha
2054 1.1 rearnsha void
2055 1.1 rearnsha plcomcndetach(void)
2056 1.1 rearnsha {
2057 1.1 rearnsha bus_space_unmap(plcomconstag, plcomconsioh, PLCOM_UART_SIZE);
2058 1.1 rearnsha plcomconstag = NULL;
2059 1.1 rearnsha
2060 1.1 rearnsha cn_tab = NULL;
2061 1.1 rearnsha }
2062 1.1 rearnsha
2063 1.1 rearnsha int
2064 1.1 rearnsha plcomcngetc(dev_t dev)
2065 1.1 rearnsha {
2066 1.1 rearnsha return plcom_common_getc(dev, plcomconstag, plcomconsioh);
2067 1.1 rearnsha }
2068 1.1 rearnsha
2069 1.1 rearnsha /*
2070 1.1 rearnsha * Console kernel output character routine.
2071 1.1 rearnsha */
2072 1.1 rearnsha void
2073 1.1 rearnsha plcomcnputc(dev_t dev, int c)
2074 1.1 rearnsha {
2075 1.1 rearnsha plcom_common_putc(dev, plcomconstag, plcomconsioh, c);
2076 1.1 rearnsha }
2077 1.1 rearnsha
2078 1.1 rearnsha void
2079 1.1 rearnsha plcomcnpollc(dev_t dev, int on)
2080 1.1 rearnsha {
2081 1.1 rearnsha
2082 1.1 rearnsha }
2083 1.1 rearnsha
2084 1.1 rearnsha #ifdef KGDB
2085 1.1 rearnsha int
2086 1.1 rearnsha plcom_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate,
2087 1.1 rearnsha int frequency, tcflag_t cflag, int unit)
2088 1.1 rearnsha {
2089 1.1 rearnsha int res;
2090 1.1 rearnsha
2091 1.1 rearnsha if (iot == plcomconstag && iobase == plcomconsunit)
2092 1.1 rearnsha return EBUSY; /* cannot share with console */
2093 1.1 rearnsha
2094 1.1 rearnsha res = plcominit(iot, iobase, rate, frequency, cflag, &plcom_kgdb_ioh);
2095 1.1 rearnsha if (res)
2096 1.1 rearnsha return res;
2097 1.1 rearnsha
2098 1.1 rearnsha kgdb_attach(plcom_kgdb_getc, plcom_kgdb_putc, NULL);
2099 1.1 rearnsha kgdb_dev = 123; /* unneeded, only to satisfy some tests */
2100 1.1 rearnsha
2101 1.1 rearnsha plcom_kgdb_iot = iot;
2102 1.1 rearnsha plcom_kgdb_unit = unit;
2103 1.1 rearnsha
2104 1.1 rearnsha return 0;
2105 1.1 rearnsha }
2106 1.1 rearnsha
2107 1.1 rearnsha /* ARGSUSED */
2108 1.1 rearnsha int
2109 1.1 rearnsha plcom_kgdb_getc(void *arg)
2110 1.1 rearnsha {
2111 1.1 rearnsha return plcom_common_getc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh);
2112 1.1 rearnsha }
2113 1.1 rearnsha
2114 1.1 rearnsha /* ARGSUSED */
2115 1.1 rearnsha void
2116 1.1 rearnsha plcom_kgdb_putc(void *arg, int c)
2117 1.1 rearnsha {
2118 1.1 rearnsha plcom_common_putc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh, c);
2119 1.1 rearnsha }
2120 1.1 rearnsha #endif /* KGDB */
2121 1.1 rearnsha
2122 1.1 rearnsha /* helper function to identify the plcom ports used by
2123 1.1 rearnsha console or KGDB (and not yet autoconf attached) */
2124 1.1 rearnsha int
2125 1.1 rearnsha plcom_is_console(bus_space_tag_t iot, int unit,
2126 1.1 rearnsha bus_space_handle_t *ioh)
2127 1.1 rearnsha {
2128 1.1 rearnsha bus_space_handle_t help;
2129 1.1 rearnsha
2130 1.1 rearnsha if (!plcomconsattached &&
2131 1.1 rearnsha iot == plcomconstag && unit == plcomconsunit)
2132 1.1 rearnsha help = plcomconsioh;
2133 1.1 rearnsha #ifdef KGDB
2134 1.1 rearnsha else if (!plcom_kgdb_attached &&
2135 1.1 rearnsha iot == plcom_kgdb_iot && unit == plcom_kgdb_unit)
2136 1.1 rearnsha help = plcom_kgdb_ioh;
2137 1.1 rearnsha #endif
2138 1.1 rearnsha else
2139 1.1 rearnsha return 0;
2140 1.1 rearnsha
2141 1.1 rearnsha if (ioh)
2142 1.1 rearnsha *ioh = help;
2143 1.1 rearnsha return 1;
2144 1.1 rearnsha }
2145