1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2003 Marcel Moolenaar 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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. 16 * 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. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 #include <sys/cons.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> 43 #include <sys/rman.h> 44 #include <sys/tty.h> 45 #include <machine/resource.h> 46 #include <machine/stdarg.h> 47 48 #include <dev/uart/uart.h> 49 #include <dev/uart/uart_bus.h> 50 #include <dev/uart/uart_cpu.h> 51 52 #include "uart_if.h" 53 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; 62 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; 72 73 CONSOLE_DRIVER( 74 uart, 75 .cn_resume = uart_cnresume, 76 ); 77 78 static struct uart_devinfo uart_console; 79 80 /* TTY swi(9) event. Allows all uart soft handlers to share one ithread. */ 81 static struct intr_event *tty_intr_event; 82 83 static void 84 uart_cnprobe(struct consdev *cp) 85 { 86 87 cp->cn_pri = CN_DEAD; 88 89 KASSERT(uart_console.cookie == NULL, ("foo")); 90 91 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console)) 92 return; 93 94 if (uart_probe(&uart_console)) 95 return; 96 97 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name)); 98 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL; 99 cp->cn_arg = &uart_console; 100 } 101 102 static void 103 uart_cninit(struct consdev *cp) 104 { 105 struct uart_devinfo *di; 106 107 /* 108 * Yedi trick: we need to be able to define cn_dev before we go 109 * single- or multi-user. The problem is that we don't know at 110 * this time what the device will be. Hence, we need to link from 111 * the uart_devinfo to the consdev that corresponds to it so that 112 * we can define cn_dev in uart_bus_attach() when we find the 113 * device during bus enumeration. That's when we'll know what the 114 * the unit number will be. 115 */ 116 di = cp->cn_arg; 117 KASSERT(di->cookie == NULL, ("foo")); 118 di->cookie = cp; 119 di->type = UART_DEV_CONSOLE; 120 uart_add_sysdev(di); 121 uart_init(di); 122 } 123 124 static void 125 uart_cnresume(struct consdev *cp) 126 { 127 128 uart_init(cp->cn_arg); 129 } 130 131 static void 132 uart_cnterm(struct consdev *cp) 133 { 134 135 uart_term(cp->cn_arg); 136 } 137 138 static void 139 uart_cngrab(struct consdev *cp) 140 { 141 142 uart_grab(cp->cn_arg); 143 } 144 145 static void 146 uart_cnungrab(struct consdev *cp) 147 { 148 149 uart_ungrab(cp->cn_arg); 150 } 151 152 static void 153 uart_cnputc(struct consdev *cp, int c) 154 { 155 156 uart_putc(cp->cn_arg, c); 157 } 158 159 static int 160 uart_cngetc(struct consdev *cp) 161 { 162 163 return (uart_poll(cp->cn_arg)); 164 } 165 166 static int 167 uart_tty_open(struct tty *tp) 168 { 169 struct uart_softc *sc; 170 171 sc = tty_softc(tp); 172 173 if (sc == NULL || sc->sc_leaving) 174 return (ENXIO); 175 176 sc->sc_opened = 1; 177 return (0); 178 } 179 180 static void 181 uart_tty_close(struct tty *tp) 182 { 183 struct uart_softc *sc; 184 185 sc = tty_softc(tp); 186 if (sc == NULL || sc->sc_leaving || !sc->sc_opened) 187 return; 188 189 if (sc->sc_hwiflow) 190 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0); 191 if (sc->sc_hwoflow) 192 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0); 193 if (sc->sc_sysdev == NULL) 194 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 195 196 wakeup(sc); 197 sc->sc_opened = 0; 198 } 199 200 static void 201 uart_tty_outwakeup(struct tty *tp) 202 { 203 struct uart_softc *sc; 204 205 sc = tty_softc(tp); 206 if (sc == NULL || sc->sc_leaving) 207 return; 208 209 if (sc->sc_txbusy) 210 return; 211 212 /* 213 * Respect RTS/CTS (output) flow control if enabled and not already 214 * handled by hardware. 215 */ 216 if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow && 217 !(sc->sc_hwsig & SER_CTS)) 218 return; 219 220 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz); 221 if (sc->sc_txdatasz != 0) 222 UART_TRANSMIT(sc); 223 } 224 225 static void 226 uart_tty_inwakeup(struct tty *tp) 227 { 228 struct uart_softc *sc; 229 230 sc = tty_softc(tp); 231 if (sc == NULL || sc->sc_leaving) 232 return; 233 234 if (sc->sc_isquelch) { 235 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) 236 UART_SETSIG(sc, SER_DRTS|SER_RTS); 237 sc->sc_isquelch = 0; 238 uart_sched_softih(sc, SER_INT_RXREADY); 239 } 240 } 241 242 static int 243 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data, 244 struct thread *td __unused) 245 { 246 struct uart_softc *sc; 247 248 sc = tty_softc(tp); 249 250 switch (cmd) { 251 case TIOCSBRK: 252 UART_IOCTL(sc, UART_IOCTL_BREAK, 1); 253 return (0); 254 case TIOCCBRK: 255 UART_IOCTL(sc, UART_IOCTL_BREAK, 0); 256 return (0); 257 default: 258 return pps_ioctl(cmd, data, &sc->sc_pps); 259 } 260 } 261 262 static int 263 uart_tty_param(struct tty *tp, struct termios *t) 264 { 265 struct uart_softc *sc; 266 int databits, parity, stopbits; 267 268 sc = tty_softc(tp); 269 if (sc == NULL || sc->sc_leaving) 270 return (ENODEV); 271 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0) 272 return (EINVAL); 273 if (t->c_ospeed == 0) { 274 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 275 return (0); 276 } 277 switch (t->c_cflag & CSIZE) { 278 case CS5: databits = 5; break; 279 case CS6: databits = 6; break; 280 case CS7: databits = 7; break; 281 default: databits = 8; break; 282 } 283 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1; 284 if (t->c_cflag & PARENB) 285 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD : 286 UART_PARITY_EVEN; 287 else 288 parity = UART_PARITY_NONE; 289 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0) 290 return (EINVAL); 291 if ((t->c_cflag & CNO_RTSDTR) == 0) 292 UART_SETSIG(sc, SER_DDTR | SER_DTR); 293 /* Set input flow control state. */ 294 if (!sc->sc_hwiflow) { 295 if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch) 296 UART_SETSIG(sc, SER_DRTS); 297 else { 298 if ((t->c_cflag & CNO_RTSDTR) == 0) 299 UART_SETSIG(sc, SER_DRTS | SER_RTS); 300 } 301 } else 302 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW)); 303 /* Set output flow control state. */ 304 if (sc->sc_hwoflow) 305 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW)); 306 307 return (0); 308 } 309 310 static int 311 uart_tty_modem(struct tty *tp, int biton, int bitoff) 312 { 313 struct uart_softc *sc; 314 315 sc = tty_softc(tp); 316 if (biton != 0 || bitoff != 0) 317 UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton); 318 return (sc->sc_hwsig); 319 } 320 321 void 322 uart_tty_intr(void *arg) 323 { 324 struct uart_softc *sc = arg; 325 struct tty *tp; 326 int c, err = 0, pend, sig, xc; 327 328 if (sc->sc_leaving) 329 return; 330 331 pend = atomic_readandclear_32(&sc->sc_ttypend); 332 if (!(pend & SER_INT_MASK)) 333 return; 334 335 tp = sc->sc_u.u_tty.tp; 336 tty_lock(tp); 337 338 if (pend & SER_INT_RXREADY) { 339 while (!uart_rx_empty(sc) && !sc->sc_isquelch) { 340 xc = uart_rx_peek(sc); 341 c = xc & 0xff; 342 if (xc & UART_STAT_FRAMERR) 343 err |= TRE_FRAMING; 344 if (xc & UART_STAT_OVERRUN) 345 err |= TRE_OVERRUN; 346 if (xc & UART_STAT_PARERR) 347 err |= TRE_PARITY; 348 if (ttydisc_rint(tp, c, err) != 0) { 349 sc->sc_isquelch = 1; 350 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && 351 !sc->sc_hwiflow) 352 UART_SETSIG(sc, SER_DRTS); 353 } else 354 uart_rx_next(sc); 355 } 356 } 357 358 if (pend & SER_INT_BREAK) 359 ttydisc_rint(tp, 0, TRE_BREAK); 360 361 if (pend & SER_INT_SIGCHG) { 362 sig = pend & SER_INT_SIGMASK; 363 if (sig & SER_DDCD) 364 ttydisc_modem(tp, sig & SER_DCD); 365 if (sig & SER_DCTS) 366 uart_tty_outwakeup(tp); 367 } 368 369 if (pend & SER_INT_TXIDLE) 370 uart_tty_outwakeup(tp); 371 ttydisc_rint_done(tp); 372 tty_unlock(tp); 373 } 374 375 static void 376 uart_tty_free(void *arg __unused) 377 { 378 379 /* 380 * XXX: uart(4) could reuse the device unit number before it is 381 * being freed by the TTY layer. We should use this hook to free 382 * the device unit number, but unfortunately newbus does not 383 * seem to support such a construct. 384 */ 385 } 386 387 static bool 388 uart_tty_busy(struct tty *tp) 389 { 390 struct uart_softc *sc; 391 392 sc = tty_softc(tp); 393 if (sc == NULL || sc->sc_leaving) 394 return (FALSE); 395 396 return (sc->sc_txbusy); 397 } 398 399 static struct ttydevsw uart_tty_class = { 400 .tsw_flags = TF_INITLOCK|TF_CALLOUT, 401 .tsw_open = uart_tty_open, 402 .tsw_close = uart_tty_close, 403 .tsw_outwakeup = uart_tty_outwakeup, 404 .tsw_inwakeup = uart_tty_inwakeup, 405 .tsw_ioctl = uart_tty_ioctl, 406 .tsw_param = uart_tty_param, 407 .tsw_modem = uart_tty_modem, 408 .tsw_free = uart_tty_free, 409 .tsw_busy = uart_tty_busy, 410 }; 411 412 int 413 uart_tty_attach(struct uart_softc *sc) 414 { 415 struct tty *tp; 416 int unit; 417 418 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc); 419 420 unit = device_get_unit(sc->sc_dev); 421 422 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 423 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name, 424 "ttyu%r", unit); 425 tty_init_console(tp, sc->sc_sysdev->baudrate); 426 } 427 428 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY, 429 INTR_TYPE_TTY, &sc->sc_softih); 430 431 tty_makedev(tp, NULL, "u%r", unit); 432 433 return (0); 434 } 435 436 int 437 uart_tty_detach(struct uart_softc *sc) 438 { 439 struct tty *tp; 440 441 tp = sc->sc_u.u_tty.tp; 442 443 tty_lock(tp); 444 swi_remove(sc->sc_softih); 445 tty_rel_gone(tp); 446 447 return (0); 448 } 449 450 struct mtx * 451 uart_tty_getlock(struct uart_softc *sc) 452 { 453 454 if (sc->sc_u.u_tty.tp != NULL) 455 return (tty_getlock(sc->sc_u.u_tty.tp)); 456 else 457 return (NULL); 458 } 459