2 * Copyright (c) 2003 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.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>
43 #include <machine/resource.h>
44 #include <machine/stdarg.h>
46 #include <dev/uart/uart.h>
47 #include <dev/uart/uart_bus.h>
48 #include <dev/uart/uart_cpu.h>
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 static cn_grab_t uart_cngrab;
58 static cn_ungrab_t uart_cnungrab;
60 static tsw_open_t uart_tty_open;
61 static tsw_close_t uart_tty_close;
62 static tsw_outwakeup_t uart_tty_outwakeup;
63 static tsw_inwakeup_t uart_tty_inwakeup;
64 static tsw_ioctl_t uart_tty_ioctl;
65 static tsw_param_t uart_tty_param;
66 static tsw_modem_t uart_tty_modem;
67 static tsw_free_t uart_tty_free;
68 static tsw_busy_t uart_tty_busy;
72 static struct uart_devinfo uart_console;
75 uart_cnprobe(struct consdev *cp)
80 KASSERT(uart_console.cookie == NULL, ("foo"));
82 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
85 if (uart_probe(&uart_console))
88 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
89 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
90 cp->cn_arg = &uart_console;
94 uart_cninit(struct consdev *cp)
96 struct uart_devinfo *di;
99 * Yedi trick: we need to be able to define cn_dev before we go
100 * single- or multi-user. The problem is that we don't know at
101 * this time what the device will be. Hence, we need to link from
102 * the uart_devinfo to the consdev that corresponds to it so that
103 * we can define cn_dev in uart_bus_attach() when we find the
104 * device during bus enumeration. That's when we'll know what the
105 * the unit number will be.
108 KASSERT(di->cookie == NULL, ("foo"));
110 di->type = UART_DEV_CONSOLE;
116 uart_cnterm(struct consdev *cp)
119 uart_term(cp->cn_arg);
123 uart_cngrab(struct consdev *cp)
126 uart_grab(cp->cn_arg);
130 uart_cnungrab(struct consdev *cp)
133 uart_ungrab(cp->cn_arg);
137 uart_cnputc(struct consdev *cp, int c)
140 uart_putc(cp->cn_arg, c);
144 uart_cngetc(struct consdev *cp)
147 return (uart_poll(cp->cn_arg));
151 uart_tty_open(struct tty *tp)
153 struct uart_softc *sc;
157 if (sc == NULL || sc->sc_leaving)
165 uart_tty_close(struct tty *tp)
167 struct uart_softc *sc;
170 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
174 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
176 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
177 if (sc->sc_sysdev == NULL)
178 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
185 uart_tty_outwakeup(struct tty *tp)
187 struct uart_softc *sc;
190 if (sc == NULL || sc->sc_leaving)
197 * Respect RTS/CTS (output) flow control if enabled and not already
198 * handled by hardware.
200 if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow &&
201 !(sc->sc_hwsig & SER_CTS))
204 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
205 if (sc->sc_txdatasz != 0)
210 uart_tty_inwakeup(struct tty *tp)
212 struct uart_softc *sc;
215 if (sc == NULL || sc->sc_leaving)
218 if (sc->sc_isquelch) {
219 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow)
220 UART_SETSIG(sc, SER_DRTS|SER_RTS);
222 uart_sched_softih(sc, SER_INT_RXREADY);
227 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data,
228 struct thread *td __unused)
230 struct uart_softc *sc;
236 UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
239 UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
242 return pps_ioctl(cmd, data, &sc->sc_pps);
247 uart_tty_param(struct tty *tp, struct termios *t)
249 struct uart_softc *sc;
250 int databits, parity, stopbits;
253 if (sc == NULL || sc->sc_leaving)
255 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
257 /* Fixate certain parameters for system devices. */
258 if (sc->sc_sysdev != NULL) {
259 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
260 t->c_cflag |= CLOCAL;
261 t->c_cflag &= ~HUPCL;
263 if (t->c_ospeed == 0) {
264 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
267 switch (t->c_cflag & CSIZE) {
268 case CS5: databits = 5; break;
269 case CS6: databits = 6; break;
270 case CS7: databits = 7; break;
271 default: databits = 8; break;
273 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
274 if (t->c_cflag & PARENB)
275 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD :
278 parity = UART_PARITY_NONE;
279 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
281 UART_SETSIG(sc, SER_DDTR | SER_DTR);
282 /* Set input flow control state. */
283 if (!sc->sc_hwiflow) {
284 if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch)
285 UART_SETSIG(sc, SER_DRTS);
287 UART_SETSIG(sc, SER_DRTS | SER_RTS);
289 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
290 /* Set output flow control state. */
292 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
298 uart_tty_modem(struct tty *tp, int biton, int bitoff)
300 struct uart_softc *sc;
303 if (biton != 0 || bitoff != 0)
304 UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton);
305 return (sc->sc_hwsig);
309 uart_tty_intr(void *arg)
311 struct uart_softc *sc = arg;
313 int c, err = 0, pend, sig, xc;
318 pend = atomic_readandclear_32(&sc->sc_ttypend);
319 if (!(pend & SER_INT_MASK))
322 tp = sc->sc_u.u_tty.tp;
325 if (pend & SER_INT_RXREADY) {
326 while (!uart_rx_empty(sc) && !sc->sc_isquelch) {
327 xc = uart_rx_peek(sc);
329 if (xc & UART_STAT_FRAMERR)
331 if (xc & UART_STAT_OVERRUN)
333 if (xc & UART_STAT_PARERR)
335 if (ttydisc_rint(tp, c, err) != 0) {
337 if ((tp->t_termios.c_cflag & CRTS_IFLOW) &&
339 UART_SETSIG(sc, SER_DRTS);
345 if (pend & SER_INT_BREAK)
346 ttydisc_rint(tp, 0, TRE_BREAK);
348 if (pend & SER_INT_SIGCHG) {
349 sig = pend & SER_INT_SIGMASK;
351 ttydisc_modem(tp, sig & SER_DCD);
353 uart_tty_outwakeup(tp);
356 if (pend & SER_INT_TXIDLE)
357 uart_tty_outwakeup(tp);
358 ttydisc_rint_done(tp);
363 uart_tty_free(void *arg __unused)
367 * XXX: uart(4) could reuse the device unit number before it is
368 * being freed by the TTY layer. We should use this hook to free
369 * the device unit number, but unfortunately newbus does not
370 * seem to support such a construct.
375 uart_tty_busy(struct tty *tp)
377 struct uart_softc *sc;
380 if (sc == NULL || sc->sc_leaving)
383 return (sc->sc_txbusy);
386 static struct ttydevsw uart_tty_class = {
387 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
388 .tsw_open = uart_tty_open,
389 .tsw_close = uart_tty_close,
390 .tsw_outwakeup = uart_tty_outwakeup,
391 .tsw_inwakeup = uart_tty_inwakeup,
392 .tsw_ioctl = uart_tty_ioctl,
393 .tsw_param = uart_tty_param,
394 .tsw_modem = uart_tty_modem,
395 .tsw_free = uart_tty_free,
396 .tsw_busy = uart_tty_busy,
400 uart_tty_attach(struct uart_softc *sc)
405 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc);
407 unit = device_get_unit(sc->sc_dev);
409 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
410 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
412 tty_init_console(tp, 0);
415 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
416 INTR_TYPE_TTY, &sc->sc_softih);
418 tty_makedev(tp, NULL, "u%r", unit);
424 uart_tty_detach(struct uart_softc *sc)
428 tp = sc->sc_u.u_tty.tp;
431 swi_remove(sc->sc_softih);
438 uart_tty_getlock(struct uart_softc *sc)
441 if (sc->sc_u.u_tty.tp != NULL)
442 return (tty_getlock(sc->sc_u.u_tty.tp));