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_init_t uart_cnresume;
57 static cn_term_t uart_cnterm;
58 static cn_getc_t uart_cngetc;
59 static cn_putc_t uart_cnputc;
60 static cn_grab_t uart_cngrab;
61 static cn_ungrab_t uart_cnungrab;
63 static tsw_open_t uart_tty_open;
64 static tsw_close_t uart_tty_close;
65 static tsw_outwakeup_t uart_tty_outwakeup;
66 static tsw_inwakeup_t uart_tty_inwakeup;
67 static tsw_ioctl_t uart_tty_ioctl;
68 static tsw_param_t uart_tty_param;
69 static tsw_modem_t uart_tty_modem;
70 static tsw_free_t uart_tty_free;
71 static tsw_busy_t uart_tty_busy;
75 .cn_resume = uart_cnresume,
78 static struct uart_devinfo uart_console;
81 uart_cnprobe(struct consdev *cp)
86 KASSERT(uart_console.cookie == NULL, ("foo"));
88 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
91 if (uart_probe(&uart_console))
94 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
95 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
96 cp->cn_arg = &uart_console;
100 uart_cninit(struct consdev *cp)
102 struct uart_devinfo *di;
105 * Yedi trick: we need to be able to define cn_dev before we go
106 * single- or multi-user. The problem is that we don't know at
107 * this time what the device will be. Hence, we need to link from
108 * the uart_devinfo to the consdev that corresponds to it so that
109 * we can define cn_dev in uart_bus_attach() when we find the
110 * device during bus enumeration. That's when we'll know what the
111 * the unit number will be.
114 KASSERT(di->cookie == NULL, ("foo"));
116 di->type = UART_DEV_CONSOLE;
122 uart_cnresume(struct consdev *cp)
125 uart_init(cp->cn_arg);
129 uart_cnterm(struct consdev *cp)
132 uart_term(cp->cn_arg);
136 uart_cngrab(struct consdev *cp)
139 uart_grab(cp->cn_arg);
143 uart_cnungrab(struct consdev *cp)
146 uart_ungrab(cp->cn_arg);
150 uart_cnputc(struct consdev *cp, int c)
153 uart_putc(cp->cn_arg, c);
157 uart_cngetc(struct consdev *cp)
160 return (uart_poll(cp->cn_arg));
164 uart_tty_open(struct tty *tp)
166 struct uart_softc *sc;
170 if (sc == NULL || sc->sc_leaving)
178 uart_tty_close(struct tty *tp)
180 struct uart_softc *sc;
183 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
187 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
189 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
190 if (sc->sc_sysdev == NULL)
191 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
198 uart_tty_outwakeup(struct tty *tp)
200 struct uart_softc *sc;
203 if (sc == NULL || sc->sc_leaving)
210 * Respect RTS/CTS (output) flow control if enabled and not already
211 * handled by hardware.
213 if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow &&
214 !(sc->sc_hwsig & SER_CTS))
217 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
218 if (sc->sc_txdatasz != 0)
223 uart_tty_inwakeup(struct tty *tp)
225 struct uart_softc *sc;
228 if (sc == NULL || sc->sc_leaving)
231 if (sc->sc_isquelch) {
232 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow)
233 UART_SETSIG(sc, SER_DRTS|SER_RTS);
235 uart_sched_softih(sc, SER_INT_RXREADY);
240 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data,
241 struct thread *td __unused)
243 struct uart_softc *sc;
249 UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
252 UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
255 return pps_ioctl(cmd, data, &sc->sc_pps);
260 uart_tty_param(struct tty *tp, struct termios *t)
262 struct uart_softc *sc;
263 int databits, parity, stopbits;
266 if (sc == NULL || sc->sc_leaving)
268 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
270 if (t->c_ospeed == 0) {
271 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
274 switch (t->c_cflag & CSIZE) {
275 case CS5: databits = 5; break;
276 case CS6: databits = 6; break;
277 case CS7: databits = 7; break;
278 default: databits = 8; break;
280 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
281 if (t->c_cflag & PARENB)
282 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD :
285 parity = UART_PARITY_NONE;
286 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
288 UART_SETSIG(sc, SER_DDTR | SER_DTR);
289 /* Set input flow control state. */
290 if (!sc->sc_hwiflow) {
291 if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch)
292 UART_SETSIG(sc, SER_DRTS);
294 UART_SETSIG(sc, SER_DRTS | SER_RTS);
296 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
297 /* Set output flow control state. */
299 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
305 uart_tty_modem(struct tty *tp, int biton, int bitoff)
307 struct uart_softc *sc;
310 if (biton != 0 || bitoff != 0)
311 UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton);
312 return (sc->sc_hwsig);
316 uart_tty_intr(void *arg)
318 struct uart_softc *sc = arg;
320 int c, err = 0, pend, sig, xc;
325 pend = atomic_readandclear_32(&sc->sc_ttypend);
326 if (!(pend & SER_INT_MASK))
329 tp = sc->sc_u.u_tty.tp;
332 if (pend & SER_INT_RXREADY) {
333 while (!uart_rx_empty(sc) && !sc->sc_isquelch) {
334 xc = uart_rx_peek(sc);
336 if (xc & UART_STAT_FRAMERR)
338 if (xc & UART_STAT_OVERRUN)
340 if (xc & UART_STAT_PARERR)
342 if (ttydisc_rint(tp, c, err) != 0) {
344 if ((tp->t_termios.c_cflag & CRTS_IFLOW) &&
346 UART_SETSIG(sc, SER_DRTS);
352 if (pend & SER_INT_BREAK)
353 ttydisc_rint(tp, 0, TRE_BREAK);
355 if (pend & SER_INT_SIGCHG) {
356 sig = pend & SER_INT_SIGMASK;
358 ttydisc_modem(tp, sig & SER_DCD);
360 uart_tty_outwakeup(tp);
363 if (pend & SER_INT_TXIDLE)
364 uart_tty_outwakeup(tp);
365 ttydisc_rint_done(tp);
370 uart_tty_free(void *arg __unused)
374 * XXX: uart(4) could reuse the device unit number before it is
375 * being freed by the TTY layer. We should use this hook to free
376 * the device unit number, but unfortunately newbus does not
377 * seem to support such a construct.
382 uart_tty_busy(struct tty *tp)
384 struct uart_softc *sc;
387 if (sc == NULL || sc->sc_leaving)
390 return (sc->sc_txbusy);
393 static struct ttydevsw uart_tty_class = {
394 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
395 .tsw_open = uart_tty_open,
396 .tsw_close = uart_tty_close,
397 .tsw_outwakeup = uart_tty_outwakeup,
398 .tsw_inwakeup = uart_tty_inwakeup,
399 .tsw_ioctl = uart_tty_ioctl,
400 .tsw_param = uart_tty_param,
401 .tsw_modem = uart_tty_modem,
402 .tsw_free = uart_tty_free,
403 .tsw_busy = uart_tty_busy,
407 uart_tty_attach(struct uart_softc *sc)
412 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc);
414 unit = device_get_unit(sc->sc_dev);
416 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
417 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
419 tty_init_console(tp, sc->sc_sysdev->baudrate);
422 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
423 INTR_TYPE_TTY, &sc->sc_softih);
425 tty_makedev(tp, NULL, "u%r", unit);
431 uart_tty_detach(struct uart_softc *sc)
435 tp = sc->sc_u.u_tty.tp;
438 swi_remove(sc->sc_softih);
445 uart_tty_getlock(struct uart_softc *sc)
448 if (sc->sc_u.u_tty.tp != NULL)
449 return (tty_getlock(sc->sc_u.u_tty.tp));