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 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
80 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
81 cp->cn_arg = &uart_console;
85 uart_cninit(struct consdev *cp)
87 struct uart_devinfo *di;
90 * Yedi trick: we need to be able to define cn_dev before we go
91 * single- or multi-user. The problem is that we don't know at
92 * this time what the device will be. Hence, we need to link from
93 * the uart_devinfo to the consdev that corresponds to it so that
94 * we can define cn_dev in uart_bus_attach() when we find the
95 * device during bus enumeration. That's when we'll know what the
96 * the unit number will be.
99 KASSERT(di->cookie == NULL, ("foo"));
101 di->type = UART_DEV_CONSOLE;
107 uart_cnterm(struct consdev *cp)
110 uart_term(cp->cn_arg);
114 uart_cnputc(struct consdev *cp, int c)
117 uart_putc(cp->cn_arg, c);
121 uart_cncheckc(struct consdev *cp)
124 return (uart_poll(cp->cn_arg));
128 uart_cngetc(struct consdev *cp)
131 return (uart_getc(cp->cn_arg));
135 uart_tty_open(struct tty *tp, struct cdev *dev)
137 struct uart_softc *sc;
145 uart_tty_close(struct tty *tp)
147 struct uart_softc *sc;
150 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
154 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
156 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
157 if (sc->sc_sysdev == NULL)
158 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
166 uart_tty_oproc(struct tty *tp)
168 struct uart_softc *sc;
171 if (sc == NULL || sc->sc_leaving)
175 * Handle input flow control. Note that if we have hardware support,
176 * we don't do anything here. We continue to receive until our buffer
177 * is full. At that time we cannot empty the UART itself and it will
178 * de-assert RTS for us. In that situation we're completely stuffed.
179 * Without hardware support, we need to toggle RTS ourselves.
181 if ((tp->t_cflag & CRTS_IFLOW) && !sc->sc_hwiflow) {
182 if ((tp->t_state & TS_TBLOCK) &&
183 (sc->sc_hwsig & SER_RTS))
184 UART_SETSIG(sc, SER_DRTS);
185 else if (!(tp->t_state & TS_TBLOCK) &&
186 !(sc->sc_hwsig & SER_RTS))
187 UART_SETSIG(sc, SER_DRTS|SER_RTS);
190 if (tp->t_state & TS_TTSTOP)
193 if ((tp->t_state & TS_BUSY) || sc->sc_txbusy)
196 if (tp->t_outq.c_cc == 0) {
201 sc->sc_txdatasz = q_to_b(&tp->t_outq, sc->sc_txbuf, sc->sc_txfifosz);
202 tp->t_state |= TS_BUSY;
208 uart_tty_param(struct tty *tp, struct termios *t)
210 struct uart_softc *sc;
211 int databits, parity, stopbits;
214 if (sc == NULL || sc->sc_leaving)
216 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
218 /* Fixate certain parameters for system devices. */
219 if (sc->sc_sysdev != NULL) {
220 t->c_ispeed = t->c_ospeed = sc->sc_sysdev->baudrate;
221 t->c_cflag |= CLOCAL;
222 t->c_cflag &= ~HUPCL;
224 if (t->c_ospeed == 0) {
225 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
228 switch (t->c_cflag & CSIZE) {
229 case CS5: databits = 5; break;
230 case CS6: databits = 6; break;
231 case CS7: databits = 7; break;
232 default: databits = 8; break;
234 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
235 if (t->c_cflag & PARENB)
236 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD
239 parity = UART_PARITY_NONE;
240 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
242 UART_SETSIG(sc, SER_DDTR | SER_DTR);
243 /* Set input flow control state. */
244 if (!sc->sc_hwiflow) {
245 if ((t->c_cflag & CRTS_IFLOW) && (tp->t_state & TS_TBLOCK))
246 UART_SETSIG(sc, SER_DRTS);
248 UART_SETSIG(sc, SER_DRTS | SER_RTS);
250 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
251 /* Set output flow control state. */
253 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
259 uart_tty_modem(struct tty *tp, int biton, int bitoff)
261 struct uart_softc *sc;
264 if (biton != 0 || bitoff != 0)
265 UART_SETSIG(sc, SER_DELTA(bitoff|biton) | biton);
266 return (sc->sc_hwsig);
270 uart_tty_break(struct tty *tp, int state)
272 struct uart_softc *sc;
275 UART_IOCTL(sc, UART_IOCTL_BREAK, state);
279 uart_tty_stop(struct tty *tp, int rw)
281 struct uart_softc *sc;
284 if (sc == NULL || sc->sc_leaving)
289 UART_FLUSH(sc, UART_FLUSH_TRANSMITTER);
291 tp->t_state &= ~TS_BUSY;
294 UART_FLUSH(sc, UART_FLUSH_RECEIVER);
295 sc->sc_rxget = sc->sc_rxput = 0;
300 uart_tty_intr(void *arg)
302 struct uart_softc *sc = arg;
304 int c, pend, sig, xc;
309 pend = atomic_readandclear_32(&sc->sc_ttypend);
310 if (!(pend & UART_IPEND_MASK))
313 tp = sc->sc_u.u_tty.tp;
315 if (pend & UART_IPEND_RXREADY) {
316 while (!uart_rx_empty(sc) && !(tp->t_state & TS_TBLOCK)) {
317 xc = uart_rx_get(sc);
319 if (xc & UART_STAT_FRAMERR)
321 if (xc & UART_STAT_PARERR)
327 if (pend & UART_IPEND_BREAK) {
328 if (tp != NULL && !(tp->t_iflag & IGNBRK))
332 if (pend & UART_IPEND_SIGCHG) {
333 sig = pend & UART_IPEND_SIGMASK;
335 ttyld_modem(tp, sig & SER_DCD);
336 if ((sig & SER_DCTS) && (tp->t_cflag & CCTS_OFLOW) &&
339 tp->t_state &= ~TS_TTSTOP;
342 tp->t_state |= TS_TTSTOP;
346 if (pend & UART_IPEND_TXIDLE) {
347 tp->t_state &= ~TS_BUSY;
353 uart_tty_attach(struct uart_softc *sc)
359 sc->sc_u.u_tty.tp = tp;
362 unit = device_get_unit(sc->sc_dev);
364 tp->t_oproc = uart_tty_oproc;
365 tp->t_param = uart_tty_param;
366 tp->t_stop = uart_tty_stop;
367 tp->t_modem = uart_tty_modem;
368 tp->t_break = uart_tty_break;
369 tp->t_open = uart_tty_open;
370 tp->t_close = uart_tty_close;
372 tp->t_pps = &sc->sc_pps;
374 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
375 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
377 ttyconsolemode(tp, 0);
380 swi_add(&tty_ithd, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
381 INTR_TYPE_TTY, &sc->sc_softih);
383 ttycreate(tp, NULL, 0, MINOR_CALLOUT, "u%r", unit);
388 int uart_tty_detach(struct uart_softc *sc)
392 tp = sc->sc_u.u_tty.tp;
395 ithread_remove_handler(sc->sc_softih);