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/termios.h> 43 #include <sys/tty.h> 44 #include <machine/resource.h> 45 #include <machine/stdarg.h> 46 47 #include <dev/uart/uart.h> 48 #include <dev/uart/uart_bus.h> 49 #include <dev/uart/uart_cpu.h> 50 51 #include "uart_if.h" 52 53 static cn_probe_t uart_cnprobe; 54 static cn_init_t uart_cninit; 55 static cn_term_t uart_cnterm; 56 static cn_getc_t uart_cngetc; 57 static cn_checkc_t uart_cncheckc; 58 static cn_putc_t uart_cnputc; 59 60 CONS_DRIVER(uart, uart_cnprobe, uart_cninit, uart_cnterm, uart_cngetc, 61 uart_cncheckc, uart_cnputc, NULL); 62 63 static struct uart_devinfo uart_console; 64 65 static void 66 uart_cnprobe(struct consdev *cp) 67 { 68 69 cp->cn_pri = CN_DEAD; 70 71 KASSERT(uart_console.cookie == NULL, ("foo")); 72 73 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console)) 74 return; 75 76 if (uart_probe(&uart_console)) 77 return; 78 79 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL; 80 cp->cn_arg = &uart_console; 81 } 82 83 static void 84 uart_cninit(struct consdev *cp) 85 { 86 struct uart_devinfo *di; 87 88 /* 89 * Yedi trick: we need to be able to define cn_dev before we go 90 * single- or multi-user. The problem is that we don't know at 91 * this time what the device will be. Hence, we need to link from 92 * the uart_devinfo to the consdev that corresponds to it so that 93 * we can define cn_dev in uart_bus_attach() when we find the 94 * device during bus enumeration. That's when we'll know what the 95 * the unit number will be. 96 */ 97 di = cp->cn_arg; 98 KASSERT(di->cookie == NULL, ("foo")); 99 di->cookie = cp; 100 di->type = UART_DEV_CONSOLE; 101 uart_add_sysdev(di); 102 uart_init(di); 103 } 104 105 static void 106 uart_cnterm(struct consdev *cp) 107 { 108 109 uart_term(cp->cn_arg); 110 } 111 112 static void 113 uart_cnputc(struct consdev *cp, int c) 114 { 115 116 uart_putc(cp->cn_arg, c); 117 } 118 119 static int 120 uart_cncheckc(struct consdev *cp) 121 { 122 123 return (uart_poll(cp->cn_arg)); 124 } 125 126 static int 127 uart_cngetc(struct consdev *cp) 128 { 129 130 return (uart_getc(cp->cn_arg)); 131 } 132 133 static int 134 uart_tty_open(struct tty *tp, struct cdev *dev) 135 { 136 struct uart_softc *sc; 137 138 sc = tp->t_sc; 139 sc->sc_opened = 1; 140 return (0); 141 } 142 143 static void 144 uart_tty_close(struct tty *tp) 145 { 146 struct uart_softc *sc; 147 148 sc = tp->t_sc; 149 if (sc == NULL || sc->sc_leaving || !sc->sc_opened) 150 return; 151 152 if (sc->sc_hwiflow) 153 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0); 154 if (sc->sc_hwoflow) 155 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0); 156 if (sc->sc_sysdev == NULL) 157 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 158 159 wakeup(sc); 160 sc->sc_opened = 0; 161 return; 162 } 163 164 static void 165 uart_tty_oproc(struct tty *tp) 166 { 167 struct uart_softc *sc; 168 169 sc = tp->t_sc; 170 if (sc == NULL || sc->sc_leaving) 171 return; 172 173 /* 174 * Handle input flow control. Note that if we have hardware support, 175 * we don't do anything here. We continue to receive until our buffer 176 * is full. At that time we cannot empty the UART itself and it will 177 * de-assert RTS for us. In that situation we're completely stuffed. 178 * Without hardware support, we need to toggle RTS ourselves. 179 */ 180 if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) { 181 if ((tp->t_state & TS_TBLOCK) && 182 (sc->sc_hwsig & SER_RTS)) 183 UART_SETSIG(sc, SER_DRTS); 184 else if (!(tp->t_state & TS_TBLOCK) && 185 !(sc->sc_hwsig & SER_RTS)) 186 UART_SETSIG(sc, SER_DRTS|SER_RTS); 187 } 188 189 if (tp->t_state & TS_TTSTOP) 190 return; 191 192 if ((tp->t_state & TS_BUSY) || sc->sc_txbusy) 193 return; 194 195 if (tp->t_outq.c_cc == 0) { 196 ttwwakeup(tp); 197 return; 198 } 199 200 sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz); 201 tp->t_state |= TS_BUSY; 202 UART_TRANSMIT(sc); 203 ttwwakeup(tp); 204 } 205 206 static int 207 uart_tty_param(struct tty *tp, struct termios *t) 208 { 209 struct uart_softc *sc; 210 int databits, parity, stopbits; 211 212 sc = tp->t_sc; 213 if (sc == NULL || sc->sc_leaving) 214 return (ENODEV); 215 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0) 216 return (EINVAL); 217 /* Fixate certain parameters for system devices. */ 218 if (sc->sc_sysdev != NULL) { 219 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate; 220 t->c_cflag |= CLOCAL; 221 t->c_cflag &= ~HUPCL; 222 } 223 if (t->c_ospeed == 0) { 224 UART_SETSIG(sc, SER_DDTR | SER_DRTS); 225 return (0); 226 } 227 switch (t->c_cflag & CSIZE) { 228 case CS5: databits = 5; break; 229 case CS6: databits = 6; break; 230 case CS7: databits = 7; break; 231 default: databits = 8; break; 232 } 233 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1; 234 if (t->c_cflag & PARENB) 235 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD 236 : UART_PARITY_EVEN; 237 else 238 parity = UART_PARITY_NONE; 239 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0) 240 return (EINVAL); 241 UART_SETSIG(sc, SER_DDTR | SER_DTR); 242 /* Set input flow control state. */ 243 if (!sc->sc_hwiflow) { 244 if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK)) 245 UART_SETSIG(sc, SER_DRTS); 246 else 247 UART_SETSIG(sc, SER_DRTS | SER_RTS); 248 } else 249 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW)); 250 /* Set output flow control state. */ 251 if (sc->sc_hwoflow) 252 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW)); 253 ttsetwater(tp); 254 return (0); 255 } 256 257 static int 258 uart_tty_modem(struct tty *tp, int biton, int bitoff) 259 { 260 struct uart_softc *sc; 261 262 sc = tp->t_sc; 263 if (biton != 0 || bitoff != 0) 264 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton); 265 return (sc->sc_hwsig); 266 } 267 268 static void 269 uart_tty_break(struct tty *tp, int state) 270 { 271 struct uart_softc *sc; 272 273 sc = tp->t_sc; 274 UART_IOCTL(sc, UART_IOCTL_BREAK, state); 275 } 276 277 static void 278 uart_tty_stop(struct tty *tp, int rw) 279 { 280 struct uart_softc *sc; 281 282 sc = tp->t_sc; 283 if (sc == NULL || sc->sc_leaving) 284 return; 285 if (rw & FWRITE) { 286 if (sc->sc_txbusy) { 287 sc->sc_txbusy = 0; 288 UART_FLUSH(sc, UART_FLUSH_TRANSMITTER); 289 } 290 tp->t_state &= ~TS_BUSY; 291 } 292 if (rw & FREAD) { 293 UART_FLUSH(sc, UART_FLUSH_RECEIVER); 294 sc->sc_rxget = sc->sc_rxput = 0; 295 } 296 } 297 298 void 299 uart_tty_intr(void *arg) 300 { 301 struct uart_softc *sc = arg; 302 struct tty *tp; 303 int c, pend, sig, xc; 304 305 if (sc->sc_leaving) 306 return; 307 308 pend = atomic_readandclear_32(&sc->sc_ttypend); 309 if (!(pend & UART_IPEND_MASK)) 310 return; 311 312 tp = sc->sc_u.u_tty.tp; 313 314 if (pend & UART_IPEND_RXREADY) { 315 while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) { 316 xc = uart_rx_get(sc); 317 c = xc & 0xff; 318 if (xc & UART_STAT_FRAMERR) 319 c |= TTY_FE; 320 if (xc & UART_STAT_PARERR) 321 c |= TTY_PE; 322 ttyld_rint(tp, c); 323 } 324 } 325 326 if (pend & UART_IPEND_BREAK) { 327 if (tp != NULL && !(tp->t_iflag & IGNBRK)) 328 ttyld_rint(tp, 0); 329 } 330 331 if (pend & UART_IPEND_SIGCHG) { 332 sig = pend & UART_IPEND_SIGMASK; 333 if (sig & SER_DDCD) 334 ttyld_modem(tp, sig & SER_DCD); 335 if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) && 336 !sc->sc_hwoflow) { 337 if (sig & SER_CTS) { 338 tp->t_state &= ~TS_TTSTOP; 339 ttyld_start(tp); 340 } else 341 tp->t_state |= TS_TTSTOP; 342 } 343 } 344 345 if (pend & UART_IPEND_TXIDLE) { 346 tp->t_state &= ~TS_BUSY; 347 ttyld_start(tp); 348 } 349 } 350 351 int 352 uart_tty_attach(struct uart_softc *sc) 353 { 354 struct tty *tp; 355 int unit; 356 357 tp = ttyalloc(); 358 sc->sc_u.u_tty.tp = tp; 359 tp->t_sc = sc; 360 361 unit = device_get_unit(sc->sc_dev); 362 363 tp->t_oproc = uart_tty_oproc; 364 tp->t_param = uart_tty_param; 365 tp->t_stop = uart_tty_stop; 366 tp->t_modem = uart_tty_modem; 367 tp->t_break = uart_tty_break; 368 tp->t_open = uart_tty_open; 369 tp->t_close = uart_tty_close; 370 371 tp->t_pps = &sc->sc_pps; 372 373 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) { 374 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name, 375 "ttyu%r", unit); 376 ttyconsolemode(tp, 0); 377 } 378 379 swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY, 380 INTR_TYPE_TTY, &sc->sc_softih); 381 382 ttycreate(tp, NULL, 0, MINOR_CALLOUT, "u%r", unit); 383 384 return (0); 385 } 386 387 int uart_tty_detach(struct uart_softc *sc) 388 { 389 struct tty *tp; 390 391 tp = sc->sc_u.u_tty.tp; 392 tp->t_pps = NULL; 393 ttygone(tp); 394 ithread_remove_handler(sc->sc_softih); 395 ttyfree(tp); 396 397 return (0); 398 } 399