2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Marcel Moolenaar
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
37 #include <sys/fcntl.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/reboot.h>
42 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <machine/stdarg.h>
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_bus.h>
50 #include <dev/uart/uart_cpu.h>
54 static cn_probe_t uart_cnprobe;
55 static cn_init_t uart_cninit;
56 static cn_term_t uart_cnterm;
57 static cn_getc_t uart_cngetc;
58 static cn_putc_t uart_cnputc;
59 static cn_grab_t uart_cngrab;
60 static cn_ungrab_t uart_cnungrab;
62 static tsw_open_t uart_tty_open;
63 static tsw_close_t uart_tty_close;
64 static tsw_outwakeup_t uart_tty_outwakeup;
65 static tsw_inwakeup_t uart_tty_inwakeup;
66 static tsw_ioctl_t uart_tty_ioctl;
67 static tsw_param_t uart_tty_param;
68 static tsw_modem_t uart_tty_modem;
69 static tsw_free_t uart_tty_free;
70 static tsw_busy_t uart_tty_busy;
74 static struct uart_devinfo uart_console;
77 uart_cnprobe(struct consdev *cp)
82 KASSERT(uart_console.cookie == NULL, ("foo"));
84 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
87 if (uart_probe(&uart_console))
90 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
91 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
92 cp->cn_arg = &uart_console;
96 uart_cninit(struct consdev *cp)
98 struct uart_devinfo *di;
101 * Yedi trick: we need to be able to define cn_dev before we go
102 * single- or multi-user. The problem is that we don't know at
103 * this time what the device will be. Hence, we need to link from
104 * the uart_devinfo to the consdev that corresponds to it so that
105 * we can define cn_dev in uart_bus_attach() when we find the
106 * device during bus enumeration. That's when we'll know what the
107 * the unit number will be.
110 KASSERT(di->cookie == NULL, ("foo"));
112 di->type = UART_DEV_CONSOLE;
118 uart_cnterm(struct consdev *cp)
121 uart_term(cp->cn_arg);
125 uart_cngrab(struct consdev *cp)
128 uart_grab(cp->cn_arg);
132 uart_cnungrab(struct consdev *cp)
135 uart_ungrab(cp->cn_arg);
139 uart_cnputc(struct consdev *cp, int c)
142 uart_putc(cp->cn_arg, c);
146 uart_cngetc(struct consdev *cp)
149 return (uart_poll(cp->cn_arg));
153 uart_tty_open(struct tty *tp)
155 struct uart_softc *sc;
159 if (sc == NULL || sc->sc_leaving)
167 uart_tty_close(struct tty *tp)
169 struct uart_softc *sc;
172 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
176 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
178 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
179 if (sc->sc_sysdev == NULL)
180 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
187 uart_tty_outwakeup(struct tty *tp)
189 struct uart_softc *sc;
192 if (sc == NULL || sc->sc_leaving)
199 * Respect RTS/CTS (output) flow control if enabled and not already
200 * handled by hardware.
202 if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow &&
203 !(sc->sc_hwsig & SER_CTS))
206 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
207 if (sc->sc_txdatasz != 0)
212 uart_tty_inwakeup(struct tty *tp)
214 struct uart_softc *sc;
217 if (sc == NULL || sc->sc_leaving)
220 if (sc->sc_isquelch) {
221 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow)
222 UART_SETSIG(sc, SER_DRTS|SER_RTS);
224 uart_sched_softih(sc, SER_INT_RXREADY);
229 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data,
230 struct thread *td __unused)
232 struct uart_softc *sc;
238 UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
241 UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
244 return pps_ioctl(cmd, data, &sc->sc_pps);
249 uart_tty_param(struct tty *tp, struct termios *t)
251 struct uart_softc *sc;
252 int databits, parity, stopbits;
255 if (sc == NULL || sc->sc_leaving)
257 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
259 if (t->c_ospeed == 0) {
260 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
263 switch (t->c_cflag & CSIZE) {
264 case CS5: databits = 5; break;
265 case CS6: databits = 6; break;
266 case CS7: databits = 7; break;
267 default: databits = 8; break;
269 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
270 if (t->c_cflag & PARENB)
271 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD :
274 parity = UART_PARITY_NONE;
275 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
277 UART_SETSIG(sc, SER_DDTR | SER_DTR);
278 /* Set input flow control state. */
279 if (!sc->sc_hwiflow) {
280 if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch)
281 UART_SETSIG(sc, SER_DRTS);
283 UART_SETSIG(sc, SER_DRTS | SER_RTS);
285 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
286 /* Set output flow control state. */
288 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
294 uart_tty_modem(struct tty *tp, int biton, int bitoff)
296 struct uart_softc *sc;
299 if (biton != 0 || bitoff != 0)
300 UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton);
301 return (sc->sc_hwsig);
305 uart_tty_intr(void *arg)
307 struct uart_softc *sc = arg;
309 int c, err = 0, pend, sig, xc;
314 pend = atomic_readandclear_32(&sc->sc_ttypend);
315 if (!(pend & SER_INT_MASK))
318 tp = sc->sc_u.u_tty.tp;
321 if (pend & SER_INT_RXREADY) {
322 while (!uart_rx_empty(sc) && !sc->sc_isquelch) {
323 xc = uart_rx_peek(sc);
325 if (xc & UART_STAT_FRAMERR)
327 if (xc & UART_STAT_OVERRUN)
329 if (xc & UART_STAT_PARERR)
331 if (ttydisc_rint(tp, c, err) != 0) {
333 if ((tp->t_termios.c_cflag & CRTS_IFLOW) &&
335 UART_SETSIG(sc, SER_DRTS);
341 if (pend & SER_INT_BREAK)
342 ttydisc_rint(tp, 0, TRE_BREAK);
344 if (pend & SER_INT_SIGCHG) {
345 sig = pend & SER_INT_SIGMASK;
347 ttydisc_modem(tp, sig & SER_DCD);
349 uart_tty_outwakeup(tp);
352 if (pend & SER_INT_TXIDLE)
353 uart_tty_outwakeup(tp);
354 ttydisc_rint_done(tp);
359 uart_tty_free(void *arg __unused)
363 * XXX: uart(4) could reuse the device unit number before it is
364 * being freed by the TTY layer. We should use this hook to free
365 * the device unit number, but unfortunately newbus does not
366 * seem to support such a construct.
371 uart_tty_busy(struct tty *tp)
373 struct uart_softc *sc;
376 if (sc == NULL || sc->sc_leaving)
379 return (sc->sc_txbusy);
382 static struct ttydevsw uart_tty_class = {
383 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
384 .tsw_open = uart_tty_open,
385 .tsw_close = uart_tty_close,
386 .tsw_outwakeup = uart_tty_outwakeup,
387 .tsw_inwakeup = uart_tty_inwakeup,
388 .tsw_ioctl = uart_tty_ioctl,
389 .tsw_param = uart_tty_param,
390 .tsw_modem = uart_tty_modem,
391 .tsw_free = uart_tty_free,
392 .tsw_busy = uart_tty_busy,
396 uart_tty_attach(struct uart_softc *sc)
401 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc);
403 unit = device_get_unit(sc->sc_dev);
405 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
406 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
408 tty_init_console(tp, sc->sc_sysdev->baudrate);
411 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
412 INTR_TYPE_TTY, &sc->sc_softih);
414 tty_makedev(tp, NULL, "u%r", unit);
420 uart_tty_detach(struct uart_softc *sc)
424 tp = sc->sc_u.u_tty.tp;
427 swi_remove(sc->sc_softih);
434 uart_tty_getlock(struct uart_softc *sc)
437 if (sc->sc_u.u_tty.tp != NULL)
438 return (tty_getlock(sc->sc_u.u_tty.tp));