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>
42 #include <sys/termios.h>
44 #include <machine/resource.h>
45 #include <machine/stdarg.h>
47 #include <dev/uart/uart.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_cpu.h>
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_putc_t uart_cnputc;
61 static struct uart_devinfo uart_console;
64 uart_cnprobe(struct consdev *cp)
69 KASSERT(uart_console.cookie == NULL, ("foo"));
71 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
74 if (uart_probe(&uart_console))
77 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
78 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
79 cp->cn_arg = &uart_console;
83 uart_cninit(struct consdev *cp)
85 struct uart_devinfo *di;
88 * Yedi trick: we need to be able to define cn_dev before we go
89 * single- or multi-user. The problem is that we don't know at
90 * this time what the device will be. Hence, we need to link from
91 * the uart_devinfo to the consdev that corresponds to it so that
92 * we can define cn_dev in uart_bus_attach() when we find the
93 * device during bus enumeration. That's when we'll know what the
94 * the unit number will be.
97 KASSERT(di->cookie == NULL, ("foo"));
99 di->type = UART_DEV_CONSOLE;
105 uart_cnterm(struct consdev *cp)
108 uart_term(cp->cn_arg);
112 uart_cnputc(struct consdev *cp, int c)
115 uart_putc(cp->cn_arg, c);
119 uart_cngetc(struct consdev *cp)
122 return (uart_poll(cp->cn_arg));
126 uart_tty_open(struct tty *tp)
128 struct uart_softc *sc;
132 if (sc == NULL || sc->sc_leaving)
140 uart_tty_close(struct tty *tp)
142 struct uart_softc *sc;
145 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
149 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
151 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
152 if (sc->sc_sysdev == NULL)
153 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
161 uart_tty_outwakeup(struct tty *tp)
163 struct uart_softc *sc;
166 if (sc == NULL || sc->sc_leaving)
170 * Handle input flow control. Note that if we have hardware support,
171 * we don't do anything here. We continue to receive until our buffer
172 * is full. At that time we cannot empty the UART itself and it will
173 * de-assert RTS for us. In that situation we're completely stuffed.
174 * Without hardware support, we need to toggle RTS ourselves.
176 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) {
178 /*if ((tp->t_state & TS_TBLOCK) &&
179 (sc->sc_hwsig & SER_RTS))
180 UART_SETSIG(sc, SER_DRTS);
181 else */ if (/*!(tp->t_state & TS_TBLOCK) &&*/
182 !(sc->sc_hwsig & SER_RTS))
183 UART_SETSIG(sc, SER_DRTS|SER_RTS);
185 /* XXX: we should use inwakeup to implement this! */
186 if (!(sc->sc_hwsig & SER_RTS))
187 UART_SETSIG(sc, SER_DRTS|SER_RTS);
193 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
194 if (sc->sc_txdatasz != 0)
199 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
201 struct uart_softc *sc;
207 UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
210 UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
213 return pps_ioctl(cmd, data, &sc->sc_pps);
218 uart_tty_param(struct tty *tp, struct termios *t)
220 struct uart_softc *sc;
221 int databits, parity, stopbits;
224 if (sc == NULL || sc->sc_leaving)
226 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
228 /* Fixate certain parameters for system devices. */
229 if (sc->sc_sysdev != NULL) {
230 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
231 t->c_cflag |= CLOCAL;
232 t->c_cflag &= ~HUPCL;
234 if (t->c_ospeed == 0) {
235 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
238 switch (t->c_cflag & CSIZE) {
239 case CS5: databits = 5; break;
240 case CS6: databits = 6; break;
241 case CS7: databits = 7; break;
242 default: databits = 8; break;
244 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
245 if (t->c_cflag & PARENB)
246 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
249 parity = UART_PARITY_NONE;
250 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
252 UART_SETSIG(sc, SER_DDTR | SER_DTR);
253 /* Set input flow control state. */
254 if (!sc->sc_hwiflow) {
255 /* if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK))
256 UART_SETSIG(sc, SER_DRTS);
258 UART_SETSIG(sc, SER_DRTS | SER_RTS);
260 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
261 /* Set output flow control state. */
263 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
269 uart_tty_modem(struct tty *tp, int biton, int bitoff)
271 struct uart_softc *sc;
274 if (biton != 0 || bitoff != 0)
275 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton);
276 return (sc->sc_hwsig);
280 uart_tty_intr(void *arg)
282 struct uart_softc *sc = arg;
284 int c, err = 0, pend, sig, xc;
289 pend = atomic_readandclear_32(&sc->sc_ttypend);
290 if (!(pend & SER_INT_MASK))
293 tp = sc->sc_u.u_tty.tp;
296 if (pend & SER_INT_RXREADY) {
297 while (!uart_rx_empty(sc) /* && !(tp->t_state & TS_TBLOCK)*/) {
298 xc = uart_rx_get(sc);
300 if (xc & UART_STAT_FRAMERR)
302 if (xc & UART_STAT_OVERRUN)
304 if (xc & UART_STAT_PARERR)
306 ttydisc_rint(tp, c, err);
310 if (pend & SER_INT_BREAK)
311 ttydisc_rint(tp, 0, TRE_BREAK);
313 if (pend & SER_INT_SIGCHG) {
314 sig = pend & SER_INT_SIGMASK;
316 ttydisc_modem(tp, sig & SER_DCD);
317 if ((sig & SER_DCTS) && (tp->t_termios.c_cflag & CCTS_OFLOW) &&
320 uart_tty_outwakeup(tp);
324 if (pend & SER_INT_TXIDLE)
325 uart_tty_outwakeup(tp);
326 ttydisc_rint_done(tp);
330 static struct ttydevsw uart_tty_class = {
331 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
332 .tsw_open = uart_tty_open,
333 .tsw_close = uart_tty_close,
334 .tsw_outwakeup = uart_tty_outwakeup,
335 .tsw_ioctl = uart_tty_ioctl,
336 .tsw_param = uart_tty_param,
337 .tsw_modem = uart_tty_modem,
341 uart_tty_attach(struct uart_softc *sc)
346 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc, NULL);
348 unit = device_get_unit(sc->sc_dev);
350 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
351 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
353 tty_init_console(tp, 0);
356 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
357 INTR_TYPE_TTY, &sc->sc_softih);
359 tty_makedev(tp, NULL, "u%r", unit);
364 int uart_tty_detach(struct uart_softc *sc)
368 tp = sc->sc_u.u_tty.tp;
371 swi_remove(sc->sc_softih);