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_checkc_t uart_cncheckc;
58 static cn_putc_t uart_cnputc;
60 CONS_DRIVER(uart, uart_cnprobe, uart_cninit, uart_cnterm, uart_cngetc,
61 uart_cncheckc, uart_cnputc, NULL);
63 static struct uart_devinfo uart_console;
66 uart_cnprobe(struct consdev *cp)
71 KASSERT(uart_console.cookie == NULL, ("foo"));
73 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
76 if (uart_probe(&uart_console))
79 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
80 cp->cn_arg = &uart_console;
84 uart_cninit(struct consdev *cp)
86 struct uart_devinfo *di;
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.
98 KASSERT(di->cookie == NULL, ("foo"));
100 di->type = UART_DEV_CONSOLE;
106 uart_cnterm(struct consdev *cp)
109 uart_term(cp->cn_arg);
113 uart_cnputc(struct consdev *cp, int c)
116 uart_putc(cp->cn_arg, c);
120 uart_cncheckc(struct consdev *cp)
123 return (uart_poll(cp->cn_arg));
127 uart_cngetc(struct consdev *cp)
130 return (uart_getc(cp->cn_arg));
134 uart_tty_open(struct tty *tp, struct cdev *dev)
136 struct uart_softc *sc;
144 uart_tty_close(struct tty *tp)
146 struct uart_softc *sc;
149 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
153 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
155 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
156 if (sc->sc_sysdev == NULL)
157 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
165 uart_tty_oproc(struct tty *tp)
167 struct uart_softc *sc;
170 if (sc == NULL || sc->sc_leaving)
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.
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);
189 if (tp->t_state & TS_TTSTOP)
192 if ((tp->t_state & TS_BUSY) || sc->sc_txbusy)
195 if (tp->t_outq.c_cc == 0) {
200 sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz);
201 tp->t_state |= TS_BUSY;
207 uart_tty_param(struct tty *tp, struct termios *t)
209 struct uart_softc *sc;
210 int databits, parity, stopbits;
213 if (sc == NULL || sc->sc_leaving)
215 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
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;
223 if (t->c_ospeed == 0) {
224 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
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;
233 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
234 if (t->c_cflag & PARENB)
235 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
238 parity = UART_PARITY_NONE;
239 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
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);
247 UART_SETSIG(sc, SER_DRTS | SER_RTS);
249 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
250 /* Set output flow control state. */
252 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
258 uart_tty_modem(struct tty *tp, int biton, int bitoff)
260 struct uart_softc *sc;
263 if (biton != 0 || bitoff != 0)
264 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton);
265 return (sc->sc_hwsig);
269 uart_tty_break(struct tty *tp, int state)
271 struct uart_softc *sc;
274 UART_IOCTL(sc, UART_IOCTL_BREAK, state);
278 uart_tty_stop(struct tty *tp, int rw)
280 struct uart_softc *sc;
283 if (sc == NULL || sc->sc_leaving)
288 UART_FLUSH(sc, UART_FLUSH_TRANSMITTER);
290 tp->t_state &= ~TS_BUSY;
293 UART_FLUSH(sc, UART_FLUSH_RECEIVER);
294 sc->sc_rxget = sc->sc_rxput = 0;
299 uart_tty_intr(void *arg)
301 struct uart_softc *sc = arg;
303 int c, pend, sig, xc;
308 pend = atomic_readandclear_32(&sc->sc_ttypend);
309 if (!(pend & UART_IPEND_MASK))
312 tp = sc->sc_u.u_tty.tp;
314 if (pend & UART_IPEND_RXREADY) {
315 while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) {
316 xc = uart_rx_get(sc);
318 if (xc & UART_STAT_FRAMERR)
320 if (xc & UART_STAT_PARERR)
326 if (pend & UART_IPEND_BREAK) {
327 if (tp != NULL && !(tp->t_iflag & IGNBRK))
331 if (pend & UART_IPEND_SIGCHG) {
332 sig = pend & UART_IPEND_SIGMASK;
334 ttyld_modem(tp, sig & SER_DCD);
335 if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) &&
338 tp->t_state &= ~TS_TTSTOP;
341 tp->t_state |= TS_TTSTOP;
345 if (pend & UART_IPEND_TXIDLE) {
346 tp->t_state &= ~TS_BUSY;
352 uart_tty_attach(struct uart_softc *sc)
358 sc->sc_u.u_tty.tp = tp;
361 unit = device_get_unit(sc->sc_dev);
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;
371 tp->t_pps = &sc->sc_pps;
373 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
374 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
376 ttyconsolemode(tp, 0);
379 swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
380 INTR_TYPE_TTY, &sc->sc_softih);
382 ttycreate(tp, NULL, 0, MINOR_CALLOUT, "u%r", unit);
387 int uart_tty_detach(struct uart_softc *sc)
391 tp = sc->sc_u.u_tty.tp;
394 ithread_remove_handler(sc->sc_softih);