1 /*- 2 * Copyright (c) 2003 Marcel Moolenaar 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/cons.h> 35 #include <sys/fcntl.h> 36 #include <sys/interrupt.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/reboot.h> 40 #include <machine/bus.h> 41 #include <sys/rman.h> 42 #include <sys/tty.h> 43 #include <machine/resource.h> 44 #include <machine/stdarg.h> 45 46 #include <dev/uart/uart.h> 47 #include <dev/uart/uart_bus.h> 48 #include <dev/uart/uart_cpu.h> 49 50 #include "uart_if.h" 51 52 static cn_probe_t uart_cnprobe; 53 static cn_init_t uart_cninit; 54 static cn_term_t uart_cnterm; 55 static cn_getc_t uart_cngetc; 56 static cn_putc_t uart_cnputc; 57 58 CONSOLE_DRIVER(uart); 59 60 static struct uart_devinfo uart_console; 61 62 static void 63 uart_cnprobe(struct consdev *cp) 64 { 65 66 cp->cn_pri = CN_DEAD; 67 68 KASSERT(uart_console.cookie == NULL, ("foo")); 69 70 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console)) 71 return; 72 73 if (uart_probe(&uart_console)) 74 return; 75 76 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name)); 77 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL; 78 cp->cn_arg = &uart_console; 79 } 80 81 static void 82 uart_cninit(struct consdev *cp) 83 { 84 struct uart_devinfo *di; 85 86 /* 87 * Yedi trick: we need to be able to define cn_dev before we go 88 * single- or multi-user. The problem is that we don't know at 89 * this time what the device will be. Hence, we need to link from 90 * the uart_devinfo to the consdev that corresponds to it so that 91 * we can define cn_dev in uart_bus_attach() when we find the 92 * device during bus enumeration. That's when we'll know what the 93 * the unit number will be. 94 */ 95 di = cp->cn_arg; 96 KASSERT(di->cookie == NULL, ("foo")); 97 di->cookie = cp; 98 di->type = UART_DEV_CONSOLE; 99 uart_add_sysdev(di); 100 uart_init(di); 101 } 102 103 static void 104 uart_cnterm(struct consdev *cp) 105 { 106 107 uart_term(cp->cn_arg); 108 } 109 110 static void 111 uart_cnputc(struct consdev *cp, int c) 112 { 113 114 uart_putc(cp->cn_arg, c); 115 } 116 117 static int 118 uart_cngetc(struct consdev *cp) 119 { 120 121 return (uart_poll(cp->cn_arg)); 122 } 123 124 static int 125 uart_tty_open(struct tty *tp) 126 { 127 struct uart_softc *sc; 128 129 sc = tty_softc(tp); 130 131 if (sc == NULL || sc->sc_leaving) 132 return (ENXIO); 133 134 sc->sc_opened = 1; 135 return (0); 136 } 137 138 static void 139 uart_tty_close(struct tty *tp) 140 { 141 struct uart_softc *sc; 142 143 sc = tty_softc(tp); 144 if (sc == NULL || sc->sc_leaving || !sc->sc_opened) 145 return; 146 147 if (sc->sc_hwiflow) 148 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0); 149 if (sc->sc_hwoflow) 150 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0); 151 if (sc->sc_sysdev == NULL) 152 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 153 154 wakeup(sc); 155 sc->sc_opened = 0; 156 return; 157 } 158 159 static void 160 uart_tty_outwakeup(struct tty *tp) 161 { 162 struct uart_softc *sc; 163 164 sc = tty_softc(tp); 165 if (sc == NULL || sc->sc_leaving) 166 return; 167 168 if (sc->sc_txbusy) 169 return; 170 171 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz); 172 if (sc->sc_txdatasz != 0) 173 UART_TRANSMIT(sc); 174 } 175 176 static void 177 uart_tty_inwakeup(struct tty *tp) 178 { 179 struct uart_softc *sc; 180 181 sc = tty_softc(tp); 182 if (sc == NULL || sc->sc_leaving) 183 return; 184 185 if (sc->sc_isquelch) { 186 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) 187 UART_SETSIG(sc, SER_DRTS|SER_RTS); 188 sc->sc_isquelch = 0; 189 uart_sched_softih(sc, SER_INT_RXREADY); 190 } 191 } 192 193 static int 194 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td) 195 { 196 struct uart_softc *sc; 197 198 sc = tty_softc(tp); 199 200 switch (cmd) { 201 case TIOCSBRK: 202 UART_IOCTL(sc, UART_IOCTL_BREAK, 1); 203 return (0); 204 case TIOCCBRK: 205 UART_IOCTL(sc, UART_IOCTL_BREAK, 0); 206 return (0); 207 default: 208 return pps_ioctl(cmd, data, &sc->sc_pps); 209 } 210 } 211 212 static int 213 uart_tty_param(struct tty *tp, struct termios *t) 214 { 215 struct uart_softc *sc; 216 int databits, parity, stopbits; 217 218 sc = tty_softc(tp); 219 if (sc == NULL || sc->sc_leaving) 220 return (ENODEV); 221 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0) 222 return (EINVAL); 223 /* Fixate certain parameters for system devices. */ 224 if (sc->sc_sysdev != NULL) { 225 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate; 226 t->c_cflag |= CLOCAL; 227 t->c_cflag &= ~HUPCL; 228 } 229 if (t->c_ospeed == 0) { 230 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 231 return (0); 232 } 233 switch (t->c_cflag & CSIZE) { 234 case CS5: databits = 5; break; 235 case CS6: databits = 6; break; 236 case CS7: databits = 7; break; 237 default: databits = 8; break; 238 } 239 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1; 240 if (t->c_cflag & PARENB) 241 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD 242 : UART_PARITY_EVEN; 243 else 244 parity = UART_PARITY_NONE; 245 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0) 246 return (EINVAL); 247 UART_SETSIG(sc, SER_DDTR | SER_DTR); 248 /* Set input flow control state. */ 249 if (!sc->sc_hwiflow) { 250 if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch) 251 UART_SETSIG(sc, SER_DRTS); 252 else 253 UART_SETSIG(sc, SER_DRTS | SER_RTS); 254 } else 255 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW)); 256 /* Set output flow control state. */ 257 if (sc->sc_hwoflow) 258 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW)); 259 260 return (0); 261 } 262 263 static int 264 uart_tty_modem(struct tty *tp, int biton, int bitoff) 265 { 266 struct uart_softc *sc; 267 268 sc = tty_softc(tp); 269 if (biton != 0 || bitoff != 0) 270 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton); 271 return (sc->sc_hwsig); 272 } 273 274 void 275 uart_tty_intr(void *arg) 276 { 277 struct uart_softc *sc = arg; 278 struct tty *tp; 279 int c, err = 0, pend, sig, xc; 280 281 if (sc->sc_leaving) 282 return; 283 284 pend = atomic_readandclear_32(&sc->sc_ttypend); 285 if (!(pend & SER_INT_MASK)) 286 return; 287 288 tp = sc->sc_u.u_tty.tp; 289 tty_lock(tp); 290 291 if (pend & SER_INT_RXREADY) { 292 while (!uart_rx_empty(sc) && !sc->sc_isquelch) { 293 xc = uart_rx_peek(sc); 294 c = xc & 0xff; 295 if (xc & UART_STAT_FRAMERR) 296 err |= TRE_FRAMING; 297 if (xc & UART_STAT_OVERRUN) 298 err |= TRE_OVERRUN; 299 if (xc & UART_STAT_PARERR) 300 err |= TRE_PARITY; 301 if (ttydisc_rint(tp, c, err) != 0) { 302 sc->sc_isquelch = 1; 303 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && 304 !sc->sc_hwiflow) 305 UART_SETSIG(sc, SER_DRTS); 306 } else 307 uart_rx_next(sc); 308 } 309 } 310 311 if (pend & SER_INT_BREAK) 312 ttydisc_rint(tp, 0, TRE_BREAK); 313 314 if (pend & SER_INT_SIGCHG) { 315 sig = pend & SER_INT_SIGMASK; 316 if (sig & SER_DDCD) 317 ttydisc_modem(tp, sig & SER_DCD); 318 if ((sig & SER_DCTS) && (tp->t_termios.c_cflag & CCTS_OFLOW) && 319 !sc->sc_hwoflow) { 320 if (sig & SER_CTS) 321 uart_tty_outwakeup(tp); 322 } 323 } 324 325 if (pend & SER_INT_TXIDLE) 326 uart_tty_outwakeup(tp); 327 ttydisc_rint_done(tp); 328 tty_unlock(tp); 329 } 330 331 static void 332 uart_tty_free(void *arg) 333 { 334 335 /* 336 * XXX: uart(4) could reuse the device unit number before it is 337 * being freed by the TTY layer. We should use this hook to free 338 * the device unit number, but unfortunately newbus does not 339 * seem to support such a construct. 340 */ 341 } 342 343 static struct ttydevsw uart_tty_class = { 344 .tsw_flags = TF_INITLOCK|TF_CALLOUT, 345 .tsw_open = uart_tty_open, 346 .tsw_close = uart_tty_close, 347 .tsw_outwakeup = uart_tty_outwakeup, 348 .tsw_inwakeup = uart_tty_inwakeup, 349 .tsw_ioctl = uart_tty_ioctl, 350 .tsw_param = uart_tty_param, 351 .tsw_modem = uart_tty_modem, 352 .tsw_free = uart_tty_free, 353 }; 354 355 int 356 uart_tty_attach(struct uart_softc *sc) 357 { 358 struct tty *tp; 359 int unit; 360 361 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc); 362 363 unit = device_get_unit(sc->sc_dev); 364 365 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 366 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name, 367 "ttyu%r", unit); 368 tty_init_console(tp, 0); 369 } 370 371 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY, 372 INTR_TYPE_TTY, &sc->sc_softih); 373 374 tty_makedev(tp, NULL, "u%r", unit); 375 376 return (0); 377 } 378 379 int 380 uart_tty_detach(struct uart_softc *sc) 381 { 382 struct tty *tp; 383 384 tp = sc->sc_u.u_tty.tp; 385 386 tty_lock(tp); 387 swi_remove(sc->sc_softih); 388 tty_rel_gone(tp); 389 390 return (0); 391 } 392