1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003 Marcel Moolenaar
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
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/tty.h>
43 #include <machine/resource.h>
44 #include <machine/stdarg.h>
45
46 #include <dev/uart/uart.h>
47 #include <dev/uart/uart_bus.h>
48 #include <dev/uart/uart_cpu.h>
49
50 #include "uart_if.h"
51
52 static cn_probe_t uart_cnprobe;
53 static cn_init_t uart_cninit;
54 static cn_init_t uart_cnresume;
55 static cn_term_t uart_cnterm;
56 static cn_getc_t uart_cngetc;
57 static cn_putc_t uart_cnputc;
58 static cn_grab_t uart_cngrab;
59 static cn_ungrab_t uart_cnungrab;
60
61 static tsw_open_t uart_tty_open;
62 static tsw_close_t uart_tty_close;
63 static tsw_outwakeup_t uart_tty_outwakeup;
64 static tsw_inwakeup_t uart_tty_inwakeup;
65 static tsw_ioctl_t uart_tty_ioctl;
66 static tsw_param_t uart_tty_param;
67 static tsw_modem_t uart_tty_modem;
68 static tsw_free_t uart_tty_free;
69 static tsw_busy_t uart_tty_busy;
70
71 CONSOLE_DRIVER(
72 uart,
73 .cn_resume = uart_cnresume,
74 );
75
76 static struct uart_devinfo uart_console;
77
78 /* TTY swi(9) event. Allows all uart soft handlers to share one ithread. */
79 static struct intr_event *tty_intr_event;
80
81 static void
uart_cnprobe(struct consdev * cp)82 uart_cnprobe(struct consdev *cp)
83 {
84
85 cp->cn_pri = CN_DEAD;
86
87 KASSERT(uart_console.cookie == NULL, ("foo"));
88
89 if (uart_cpu_getdev(UART_DEV_CONSOLE, &uart_console))
90 return;
91
92 if (uart_probe(&uart_console))
93 return;
94
95 strlcpy(cp->cn_name, uart_driver_name, sizeof(cp->cn_name));
96 cp->cn_pri = (boothowto & RB_SERIAL) ? CN_REMOTE : CN_NORMAL;
97 cp->cn_arg = &uart_console;
98 }
99
100 static void
uart_cninit(struct consdev * cp)101 uart_cninit(struct consdev *cp)
102 {
103 struct uart_devinfo *di;
104
105 /*
106 * Yedi trick: we need to be able to define cn_dev before we go
107 * single- or multi-user. The problem is that we don't know at
108 * this time what the device will be. Hence, we need to link from
109 * the uart_devinfo to the consdev that corresponds to it so that
110 * we can define cn_dev in uart_bus_attach() when we find the
111 * device during bus enumeration. That's when we'll know what the
112 * the unit number will be.
113 */
114 di = cp->cn_arg;
115 KASSERT(di->cookie == NULL, ("foo"));
116 di->cookie = cp;
117 di->type = UART_DEV_CONSOLE;
118 uart_add_sysdev(di);
119 uart_init(di);
120 }
121
122 static void
uart_cnresume(struct consdev * cp)123 uart_cnresume(struct consdev *cp)
124 {
125
126 uart_init(cp->cn_arg);
127 }
128
129 static void
uart_cnterm(struct consdev * cp)130 uart_cnterm(struct consdev *cp)
131 {
132
133 uart_term(cp->cn_arg);
134 }
135
136 static void
uart_cngrab(struct consdev * cp)137 uart_cngrab(struct consdev *cp)
138 {
139
140 uart_grab(cp->cn_arg);
141 }
142
143 static void
uart_cnungrab(struct consdev * cp)144 uart_cnungrab(struct consdev *cp)
145 {
146
147 uart_ungrab(cp->cn_arg);
148 }
149
150 static void
uart_cnputc(struct consdev * cp,int c)151 uart_cnputc(struct consdev *cp, int c)
152 {
153
154 uart_putc(cp->cn_arg, c);
155 }
156
157 static int
uart_cngetc(struct consdev * cp)158 uart_cngetc(struct consdev *cp)
159 {
160
161 return (uart_poll(cp->cn_arg));
162 }
163
164 static int
uart_tty_open(struct tty * tp)165 uart_tty_open(struct tty *tp)
166 {
167 struct uart_softc *sc;
168
169 sc = tty_softc(tp);
170
171 if (sc == NULL || sc->sc_leaving)
172 return (ENXIO);
173
174 sc->sc_opened = 1;
175 return (0);
176 }
177
178 static void
uart_tty_close(struct tty * tp)179 uart_tty_close(struct tty *tp)
180 {
181 struct uart_softc *sc;
182
183 sc = tty_softc(tp);
184 if (sc == NULL || sc->sc_leaving || !sc->sc_opened)
185 return;
186
187 if (sc->sc_hwiflow)
188 UART_IOCTL(sc, UART_IOCTL_IFLOW, 0);
189 if (sc->sc_hwoflow)
190 UART_IOCTL(sc, UART_IOCTL_OFLOW, 0);
191 if (sc->sc_sysdev == NULL)
192 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
193
194 wakeup(sc);
195 sc->sc_opened = 0;
196 }
197
198 static void
uart_tty_outwakeup(struct tty * tp)199 uart_tty_outwakeup(struct tty *tp)
200 {
201 struct uart_softc *sc;
202
203 sc = tty_softc(tp);
204 if (sc == NULL || sc->sc_leaving)
205 return;
206
207 if (sc->sc_txbusy)
208 return;
209
210 /*
211 * Respect RTS/CTS (output) flow control if enabled and not already
212 * handled by hardware.
213 */
214 if ((tp->t_termios.c_cflag & CCTS_OFLOW) && !sc->sc_hwoflow &&
215 !(sc->sc_hwsig & SER_CTS))
216 return;
217
218 sc->sc_txdatasz = ttydisc_getc(tp, sc->sc_txbuf, sc->sc_txfifosz);
219 if (sc->sc_txdatasz != 0)
220 UART_TRANSMIT(sc);
221 }
222
223 static void
uart_tty_inwakeup(struct tty * tp)224 uart_tty_inwakeup(struct tty *tp)
225 {
226 struct uart_softc *sc;
227
228 sc = tty_softc(tp);
229 if (sc == NULL || sc->sc_leaving)
230 return;
231
232 if (sc->sc_isquelch) {
233 if ((tp->t_termios.c_cflag & CRTS_IFLOW) && !sc->sc_hwiflow)
234 UART_SETSIG(sc, SER_DRTS|SER_RTS);
235 sc->sc_isquelch = 0;
236 uart_sched_softih(sc, SER_INT_RXREADY);
237 }
238 }
239
240 static int
uart_tty_ioctl(struct tty * tp,u_long cmd,caddr_t data,struct thread * td __unused)241 uart_tty_ioctl(struct tty *tp, u_long cmd, caddr_t data,
242 struct thread *td __unused)
243 {
244 struct uart_softc *sc;
245
246 sc = tty_softc(tp);
247
248 switch (cmd) {
249 case TIOCSBRK:
250 UART_IOCTL(sc, UART_IOCTL_BREAK, 1);
251 return (0);
252 case TIOCCBRK:
253 UART_IOCTL(sc, UART_IOCTL_BREAK, 0);
254 return (0);
255 default:
256 return pps_ioctl(cmd, data, &sc->sc_pps);
257 }
258 }
259
260 static int
uart_tty_param(struct tty * tp,struct termios * t)261 uart_tty_param(struct tty *tp, struct termios *t)
262 {
263 struct uart_softc *sc;
264 int databits, parity, stopbits;
265
266 sc = tty_softc(tp);
267 if (sc == NULL || sc->sc_leaving)
268 return (ENODEV);
269 if (t->c_ispeed != t->c_ospeed && t->c_ospeed != 0)
270 return (EINVAL);
271 if (t->c_ospeed == 0) {
272 UART_SETSIG(sc, SER_DDTR | SER_DRTS);
273 return (0);
274 }
275 switch (t->c_cflag & CSIZE) {
276 case CS5: databits = 5; break;
277 case CS6: databits = 6; break;
278 case CS7: databits = 7; break;
279 default: databits = 8; break;
280 }
281 stopbits = (t->c_cflag & CSTOPB) ? 2 : 1;
282 if (t->c_cflag & PARENB)
283 parity = (t->c_cflag & PARODD) ? UART_PARITY_ODD :
284 UART_PARITY_EVEN;
285 else
286 parity = UART_PARITY_NONE;
287 if (UART_PARAM(sc, t->c_ospeed, databits, stopbits, parity) != 0)
288 return (EINVAL);
289 if ((t->c_cflag & CNO_RTSDTR) == 0)
290 UART_SETSIG(sc, SER_DDTR | SER_DTR);
291 /* Set input flow control state. */
292 if (!sc->sc_hwiflow) {
293 if ((t->c_cflag & CRTS_IFLOW) && sc->sc_isquelch)
294 UART_SETSIG(sc, SER_DRTS);
295 else {
296 if ((t->c_cflag & CNO_RTSDTR) == 0)
297 UART_SETSIG(sc, SER_DRTS | SER_RTS);
298 }
299 } else
300 UART_IOCTL(sc, UART_IOCTL_IFLOW, (t->c_cflag & CRTS_IFLOW));
301 /* Set output flow control state. */
302 if (sc->sc_hwoflow)
303 UART_IOCTL(sc, UART_IOCTL_OFLOW, (t->c_cflag & CCTS_OFLOW));
304
305 return (0);
306 }
307
308 static int
uart_tty_modem(struct tty * tp,int biton,int bitoff)309 uart_tty_modem(struct tty *tp, int biton, int bitoff)
310 {
311 struct uart_softc *sc;
312
313 sc = tty_softc(tp);
314 if (biton != 0 || bitoff != 0)
315 UART_SETSIG(sc, SER_DELTA(bitoff | biton) | biton);
316 return (sc->sc_hwsig);
317 }
318
319 void
uart_tty_intr(void * arg)320 uart_tty_intr(void *arg)
321 {
322 struct uart_softc *sc = arg;
323 struct tty *tp;
324 int c, err = 0, pend, sig, xc;
325
326 if (sc->sc_leaving)
327 return;
328
329 pend = atomic_readandclear_32(&sc->sc_ttypend);
330 if (!(pend & SER_INT_MASK))
331 return;
332
333 tp = sc->sc_u.u_tty.tp;
334 tty_lock(tp);
335
336 if (pend & SER_INT_RXREADY) {
337 while (!uart_rx_empty(sc) && !sc->sc_isquelch) {
338 xc = uart_rx_peek(sc);
339 c = xc & 0xff;
340 if (xc & UART_STAT_FRAMERR)
341 err |= TRE_FRAMING;
342 if (xc & UART_STAT_OVERRUN)
343 err |= TRE_OVERRUN;
344 if (xc & UART_STAT_PARERR)
345 err |= TRE_PARITY;
346 if (ttydisc_rint(tp, c, err) != 0) {
347 sc->sc_isquelch = 1;
348 if ((tp->t_termios.c_cflag & CRTS_IFLOW) &&
349 !sc->sc_hwiflow)
350 UART_SETSIG(sc, SER_DRTS);
351 } else
352 uart_rx_next(sc);
353 }
354 }
355
356 if (pend & SER_INT_BREAK)
357 ttydisc_rint(tp, 0, TRE_BREAK);
358
359 if (pend & SER_INT_SIGCHG) {
360 sig = pend & SER_INT_SIGMASK;
361 if (sig & SER_DDCD)
362 ttydisc_modem(tp, sig & SER_DCD);
363 if (sig & SER_DCTS)
364 uart_tty_outwakeup(tp);
365 }
366
367 if (pend & SER_INT_TXIDLE)
368 uart_tty_outwakeup(tp);
369 ttydisc_rint_done(tp);
370 tty_unlock(tp);
371 }
372
373 static void
uart_tty_free(void * arg __unused)374 uart_tty_free(void *arg __unused)
375 {
376
377 /*
378 * XXX: uart(4) could reuse the device unit number before it is
379 * being freed by the TTY layer. We should use this hook to free
380 * the device unit number, but unfortunately newbus does not
381 * seem to support such a construct.
382 */
383 }
384
385 static bool
uart_tty_busy(struct tty * tp)386 uart_tty_busy(struct tty *tp)
387 {
388 struct uart_softc *sc;
389
390 sc = tty_softc(tp);
391 if (sc == NULL || sc->sc_leaving)
392 return (false);
393
394 /*
395 * The tty locking is sufficient here; we may lose the race against
396 * uart_bus_ihand()/uart_intr() clearing sc_txbusy underneath us, in
397 * which case we will incorrectly but non-fatally report a busy Tx
398 * path upward. However, tty locking ensures that no additional output
399 * is enqueued before UART_TXBUSY() returns, which means that there
400 * are no Tx interrupts to be lost.
401 */
402 if (sc->sc_txbusy)
403 return (true);
404 return (UART_TXBUSY(sc));
405 }
406
407 static struct ttydevsw uart_tty_class = {
408 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
409 .tsw_open = uart_tty_open,
410 .tsw_close = uart_tty_close,
411 .tsw_outwakeup = uart_tty_outwakeup,
412 .tsw_inwakeup = uart_tty_inwakeup,
413 .tsw_ioctl = uart_tty_ioctl,
414 .tsw_param = uart_tty_param,
415 .tsw_modem = uart_tty_modem,
416 .tsw_free = uart_tty_free,
417 .tsw_busy = uart_tty_busy,
418 };
419
420 int
uart_tty_attach(struct uart_softc * sc)421 uart_tty_attach(struct uart_softc *sc)
422 {
423 struct tty *tp;
424 int unit;
425
426 sc->sc_u.u_tty.tp = tp = tty_alloc(&uart_tty_class, sc);
427
428 unit = device_get_unit(sc->sc_dev);
429
430 if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
431 sprintf(((struct consdev *)sc->sc_sysdev->cookie)->cn_name,
432 "ttyu%r", unit);
433 tty_init_console(tp, sc->sc_sysdev->baudrate);
434 }
435
436 swi_add(&tty_intr_event, uart_driver_name, uart_tty_intr, sc, SWI_TTY,
437 INTR_TYPE_TTY, &sc->sc_softih);
438
439 tty_makedev(tp, NULL, "u%r", unit);
440
441 return (0);
442 }
443
444 int
uart_tty_detach(struct uart_softc * sc)445 uart_tty_detach(struct uart_softc *sc)
446 {
447 struct tty *tp;
448
449 tp = sc->sc_u.u_tty.tp;
450
451 tty_lock(tp);
452 swi_remove(sc->sc_softih);
453 tty_rel_gone(tp);
454
455 return (0);
456 }
457
458 struct mtx *
uart_tty_getlock(struct uart_softc * sc)459 uart_tty_getlock(struct uart_softc *sc)
460 {
461
462 if (sc->sc_u.u_tty.tp != NULL)
463 return (tty_getlock(sc->sc_u.u_tty.tp));
464 else
465 return (NULL);
466 }
467