xref: /freebsd/sys/dev/uart/uart_core.c (revision 2b743a9e9ddc6736208dc8ca1ce06ce64ad20a19)
1 /*-
2  * Copyright (c) 2003 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  *
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.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #ifndef KLD_MODULE
31 #include "opt_comconsole.h"
32 #endif
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/bus.h>
37 #include <sys/conf.h>
38 #include <sys/cons.h>
39 #include <sys/fcntl.h>
40 #include <sys/interrupt.h>
41 #include <sys/kdb.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/queue.h>
45 #include <sys/reboot.h>
46 #include <machine/bus.h>
47 #include <sys/rman.h>
48 #include <sys/termios.h>
49 #include <sys/tty.h>
50 #include <machine/resource.h>
51 #include <machine/stdarg.h>
52 
53 #include <dev/uart/uart.h>
54 #include <dev/uart/uart_bus.h>
55 #include <dev/uart/uart_cpu.h>
56 
57 #include "uart_if.h"
58 
59 devclass_t uart_devclass;
60 char uart_driver_name[] = "uart";
61 
62 SLIST_HEAD(uart_devinfo_list, uart_devinfo) uart_sysdevs =
63     SLIST_HEAD_INITIALIZER(uart_sysdevs);
64 
65 MALLOC_DEFINE(M_UART, "UART", "UART driver");
66 
67 void
68 uart_add_sysdev(struct uart_devinfo *di)
69 {
70 	SLIST_INSERT_HEAD(&uart_sysdevs, di, next);
71 }
72 
73 /*
74  * Schedule a soft interrupt. We do this on the 0 to !0 transition
75  * of the TTY pending interrupt status.
76  */
77 static void
78 uart_sched_softih(struct uart_softc *sc, uint32_t ipend)
79 {
80 	uint32_t new, old;
81 
82 	do {
83 		old = sc->sc_ttypend;
84 		new = old | ipend;
85 	} while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
86 
87 	if ((old & SER_INT_MASK) == 0)
88 		swi_sched(sc->sc_softih, 0);
89 }
90 
91 /*
92  * A break condition has been detected. We treat the break condition as
93  * a special case that should not happen during normal operation. When
94  * the break condition is to be passed to higher levels in the form of
95  * a NUL character, we really want the break to be in the right place in
96  * the input stream. The overhead to achieve that is not in relation to
97  * the exceptional nature of the break condition, so we permit ourselves
98  * to be sloppy.
99  */
100 static __inline int
101 uart_intr_break(void *arg)
102 {
103 	struct uart_softc *sc = arg;
104 
105 #if defined(KDB) && defined(BREAK_TO_DEBUGGER)
106 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
107 		kdb_enter("Line break on console");
108 		return (0);
109 	}
110 #endif
111 	if (sc->sc_opened)
112 		uart_sched_softih(sc, SER_INT_BREAK);
113 	return (0);
114 }
115 
116 /*
117  * Handle a receiver overrun situation. We lost at least 1 byte in the
118  * input stream and it's our job to contain the situation. We grab as
119  * much of the data we can, but otherwise flush the receiver FIFO to
120  * create some breathing room. The net effect is that we avoid the
121  * overrun condition to happen for the next X characters, where X is
122  * related to the FIFO size at the cost of loosing data right away.
123  * So, instead of having multiple overrun interrupts in close proximity
124  * to each other and possibly pessimizing UART interrupt latency for
125  * other UARTs in a multiport configuration, we create a longer segment
126  * of missing characters by freeing up the FIFO.
127  * Each overrun condition is marked in the input buffer by a token. The
128  * token represents the loss of at least one, but possible more bytes in
129  * the input stream.
130  */
131 static __inline int
132 uart_intr_overrun(void *arg)
133 {
134 	struct uart_softc *sc = arg;
135 
136 	if (sc->sc_opened) {
137 		UART_RECEIVE(sc);
138 		if (uart_rx_put(sc, UART_STAT_OVERRUN))
139 			sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
140 		uart_sched_softih(sc, SER_INT_RXREADY);
141 	}
142 	UART_FLUSH(sc, UART_FLUSH_RECEIVER);
143 	return (0);
144 }
145 
146 /*
147  * Received data ready.
148  */
149 static __inline int
150 uart_intr_rxready(void *arg)
151 {
152 	struct uart_softc *sc = arg;
153 	int rxp;
154 
155 	rxp = sc->sc_rxput;
156 	UART_RECEIVE(sc);
157 #if defined(KDB) && defined(ALT_BREAK_TO_DEBUGGER)
158 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->type == UART_DEV_CONSOLE) {
159 		while (rxp != sc->sc_rxput) {
160 			if (kdb_alt_break(sc->sc_rxbuf[rxp++], &sc->sc_altbrk))
161 				kdb_enter("Break sequence on console");
162 			if (rxp == sc->sc_rxbufsz)
163 				rxp = 0;
164 		}
165 	}
166 #endif
167 	if (sc->sc_opened)
168 		uart_sched_softih(sc, SER_INT_RXREADY);
169 	else
170 		sc->sc_rxput = sc->sc_rxget;	/* Ignore received data. */
171 	return (1);
172 }
173 
174 /*
175  * Line or modem status change (OOB signalling).
176  * We pass the signals to the software interrupt handler for further
177  * processing. Note that we merge the delta bits, but set the state
178  * bits. This is to avoid loosing state transitions due to having more
179  * than 1 hardware interrupt between software interrupts.
180  */
181 static __inline int
182 uart_intr_sigchg(void *arg)
183 {
184 	struct uart_softc *sc = arg;
185 	int new, old, sig;
186 
187 	sig = UART_GETSIG(sc);
188 
189 	if (sc->sc_pps.ppsparam.mode & PPS_CAPTUREBOTH) {
190 		if (sig & UART_SIG_DPPS) {
191 			pps_capture(&sc->sc_pps);
192 			pps_event(&sc->sc_pps, (sig & UART_SIG_PPS) ?
193 			    PPS_CAPTUREASSERT : PPS_CAPTURECLEAR);
194 		}
195 	}
196 
197 	/*
198 	 * Keep track of signal changes, even when the device is not
199 	 * opened. This allows us to inform upper layers about a
200 	 * possible loss of DCD and thus the existence of a (possibly)
201 	 * different connection when we have DCD back, during the time
202 	 * that the device was closed.
203 	 */
204 	do {
205 		old = sc->sc_ttypend;
206 		new = old & ~SER_MASK_STATE;
207 		new |= sig & SER_INT_SIGMASK;
208 	} while (!atomic_cmpset_32(&sc->sc_ttypend, old, new));
209 
210 	if (sc->sc_opened)
211 		uart_sched_softih(sc, SER_INT_SIGCHG);
212 	return (1);
213 }
214 
215 /*
216  * The transmitter can accept more data.
217  */
218 static __inline int
219 uart_intr_txidle(void *arg)
220 {
221 	struct uart_softc *sc = arg;
222 
223 	if (sc->sc_txbusy) {
224 		sc->sc_txbusy = 0;
225 		uart_sched_softih(sc, SER_INT_TXIDLE);
226 	}
227 	return (0);
228 }
229 
230 static int
231 uart_intr(void *arg)
232 {
233 	struct uart_softc *sc = arg;
234 	int flag = 0, ipend;
235 
236 	while (!sc->sc_leaving && (ipend = UART_IPEND(sc)) != 0) {
237 		flag = 1;
238 		if (ipend & SER_INT_OVERRUN)
239 			uart_intr_overrun(sc);
240 		if (ipend & SER_INT_BREAK)
241 			uart_intr_break(sc);
242 		if (ipend & SER_INT_RXREADY)
243 			uart_intr_rxready(sc);
244 		if (ipend & SER_INT_SIGCHG)
245 			uart_intr_sigchg(sc);
246 		if (ipend & SER_INT_TXIDLE)
247 			uart_intr_txidle(sc);
248 	}
249 	return((flag)?FILTER_HANDLED:FILTER_STRAY);
250 }
251 
252 serdev_intr_t *
253 uart_bus_ihand(device_t dev, int ipend)
254 {
255 
256 	switch (ipend) {
257 	case SER_INT_BREAK:
258 		return (uart_intr_break);
259 	case SER_INT_OVERRUN:
260 		return (uart_intr_overrun);
261 	case SER_INT_RXREADY:
262 		return (uart_intr_rxready);
263 	case SER_INT_SIGCHG:
264 		return (uart_intr_sigchg);
265 	case SER_INT_TXIDLE:
266 		return (uart_intr_txidle);
267 	}
268 	return (NULL);
269 }
270 
271 int
272 uart_bus_ipend(device_t dev)
273 {
274 	struct uart_softc *sc;
275 
276 	sc = device_get_softc(dev);
277 	return (UART_IPEND(sc));
278 }
279 
280 int
281 uart_bus_sysdev(device_t dev)
282 {
283 	struct uart_softc *sc;
284 
285 	sc = device_get_softc(dev);
286 	return ((sc->sc_sysdev != NULL) ? 1 : 0);
287 }
288 
289 int
290 uart_bus_probe(device_t dev, int regshft, int rclk, int rid, int chan)
291 {
292 	struct uart_softc *sc;
293 	struct uart_devinfo *sysdev;
294 	int error;
295 
296 	/*
297 	 * Initialize the instance. Note that the instance (=softc) does
298 	 * not necessarily match the hardware specific softc. We can't do
299 	 * anything about it now, because we may not attach to the device.
300 	 * Hardware drivers cannot use any of the class specific fields
301 	 * while probing.
302 	 */
303 	sc = device_get_softc(dev);
304 	kobj_init((kobj_t)sc, (kobj_class_t)sc->sc_class);
305 	sc->sc_dev = dev;
306 	if (device_get_desc(dev) == NULL)
307 		device_set_desc(dev, sc->sc_class->name);
308 
309 	/*
310 	 * Allocate the register resource. We assume that all UARTs have
311 	 * a single register window in either I/O port space or memory
312 	 * mapped I/O space. Any UART that needs multiple windows will
313 	 * consequently not be supported by this driver as-is. We try I/O
314 	 * port space first because that's the common case.
315 	 */
316 	sc->sc_rrid = rid;
317 	sc->sc_rtype = SYS_RES_IOPORT;
318 	sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
319 	    0, ~0, sc->sc_class->uc_range, RF_ACTIVE);
320 	if (sc->sc_rres == NULL) {
321 		sc->sc_rrid = rid;
322 		sc->sc_rtype = SYS_RES_MEMORY;
323 		sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype,
324 		    &sc->sc_rrid, 0, ~0, sc->sc_class->uc_range, RF_ACTIVE);
325 		if (sc->sc_rres == NULL)
326 			return (ENXIO);
327 	}
328 
329 	/*
330 	 * Fill in the bus access structure and compare this device with
331 	 * a possible console device and/or a debug port. We set the flags
332 	 * in the softc so that the hardware dependent probe can adjust
333 	 * accordingly. In general, you don't want to permanently disrupt
334 	 * console I/O.
335 	 */
336 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
337 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
338 	sc->sc_bas.chan = chan;
339 	sc->sc_bas.regshft = regshft;
340 	sc->sc_bas.rclk = (rclk == 0) ? sc->sc_class->uc_rclk : rclk;
341 
342 	SLIST_FOREACH(sysdev, &uart_sysdevs, next) {
343 		if (chan == sysdev->bas.chan &&
344 		    uart_cpu_eqres(&sc->sc_bas, &sysdev->bas)) {
345 			/* XXX check if ops matches class. */
346 			sc->sc_sysdev = sysdev;
347 			break;
348 		}
349 	}
350 
351 	error = UART_PROBE(sc);
352 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
353 	return ((error) ? error : BUS_PROBE_DEFAULT);
354 }
355 
356 int
357 uart_bus_attach(device_t dev)
358 {
359 	struct uart_softc *sc, *sc0;
360 	const char *sep;
361 	int error;
362 
363 	/*
364 	 * The sc_class field defines the type of UART we're going to work
365 	 * with and thus the size of the softc. Replace the generic softc
366 	 * with one that matches the UART now that we're certain we handle
367 	 * the device.
368 	 */
369 	sc0 = device_get_softc(dev);
370 	if (sc0->sc_class->size > sizeof(*sc)) {
371 		sc = malloc(sc0->sc_class->size, M_UART, M_WAITOK|M_ZERO);
372 		bcopy(sc0, sc, sizeof(*sc));
373 		device_set_softc(dev, sc);
374 	} else
375 		sc = sc0;
376 
377 	/*
378 	 * Protect ourselves against interrupts while we're not completely
379 	 * finished attaching and initializing. We don't expect interrupts
380 	 * until after UART_ATTACH() though.
381 	 */
382 	sc->sc_leaving = 1;
383 
384 	mtx_init(&sc->sc_hwmtx_s, "uart_hwmtx", NULL, MTX_SPIN);
385 	if (sc->sc_hwmtx == NULL)
386 		sc->sc_hwmtx = &sc->sc_hwmtx_s;
387 
388 	/*
389 	 * Re-allocate. We expect that the softc contains the information
390 	 * collected by uart_bus_probe() intact.
391 	 */
392 	sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid,
393 	    0, ~0, sc->sc_class->uc_range, RF_ACTIVE);
394 	if (sc->sc_rres == NULL) {
395 		mtx_destroy(&sc->sc_hwmtx_s);
396 		return (ENXIO);
397 	}
398 	sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres);
399 	sc->sc_bas.bst = rman_get_bustag(sc->sc_rres);
400 
401 	sc->sc_irid = 0;
402 	sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid,
403 	    RF_ACTIVE | RF_SHAREABLE);
404 	if (sc->sc_ires != NULL) {
405 		error = bus_setup_intr(dev,
406 		    sc->sc_ires, INTR_TYPE_TTY,
407 		    uart_intr, NULL, sc, &sc->sc_icookie);
408 		if (error)
409 			error = bus_setup_intr(dev,
410 			    sc->sc_ires, INTR_TYPE_TTY | INTR_MPSAFE,
411 			    NULL, (driver_intr_t *)uart_intr, sc, &sc->sc_icookie);
412 		else
413 			sc->sc_fastintr = 1;
414 
415 		if (error) {
416 			device_printf(dev, "could not activate interrupt\n");
417 			bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
418 			    sc->sc_ires);
419 			sc->sc_ires = NULL;
420 		}
421 	}
422 	if (sc->sc_ires == NULL) {
423 		/* XXX no interrupt resource. Force polled mode. */
424 		sc->sc_polled = 1;
425 	}
426 
427 	sc->sc_rxbufsz = IBUFSIZ;
428 	sc->sc_rxbuf = malloc(sc->sc_rxbufsz * sizeof(*sc->sc_rxbuf),
429 	    M_UART, M_WAITOK);
430 	sc->sc_txbuf = malloc(sc->sc_txfifosz * sizeof(*sc->sc_txbuf),
431 	    M_UART, M_WAITOK);
432 
433 	error = UART_ATTACH(sc);
434 	if (error)
435 		goto fail;
436 
437 	if (sc->sc_hwiflow || sc->sc_hwoflow) {
438 		sep = "";
439 		device_print_prettyname(dev);
440 		if (sc->sc_hwiflow) {
441 			printf("%sRTS iflow", sep);
442 			sep = ", ";
443 		}
444 		if (sc->sc_hwoflow) {
445 			printf("%sCTS oflow", sep);
446 			sep = ", ";
447 		}
448 		printf("\n");
449 	}
450 
451 	if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) {
452 		sep = "";
453 		device_print_prettyname(dev);
454 		if (sc->sc_fastintr) {
455 			printf("%sfast interrupt", sep);
456 			sep = ", ";
457 		}
458 		if (sc->sc_polled) {
459 			printf("%spolled mode", sep);
460 			sep = ", ";
461 		}
462 		printf("\n");
463 	}
464 
465 	if (sc->sc_sysdev != NULL) {
466 		if (sc->sc_sysdev->baudrate == 0) {
467 			if (UART_IOCTL(sc, UART_IOCTL_BAUD,
468 			    (intptr_t)&sc->sc_sysdev->baudrate) != 0)
469 				sc->sc_sysdev->baudrate = -1;
470 		}
471 		switch (sc->sc_sysdev->type) {
472 		case UART_DEV_CONSOLE:
473 			device_printf(dev, "console");
474 			break;
475 		case UART_DEV_DBGPORT:
476 			device_printf(dev, "debug port");
477 			break;
478 		case UART_DEV_KEYBOARD:
479 			device_printf(dev, "keyboard");
480 			break;
481 		default:
482 			device_printf(dev, "unknown system device");
483 			break;
484 		}
485 		printf(" (%d,%c,%d,%d)\n", sc->sc_sysdev->baudrate,
486 		    "noems"[sc->sc_sysdev->parity], sc->sc_sysdev->databits,
487 		    sc->sc_sysdev->stopbits);
488 	}
489 
490 	sc->sc_pps.ppscap = PPS_CAPTUREBOTH;
491 	pps_init(&sc->sc_pps);
492 
493 	error = (sc->sc_sysdev != NULL && sc->sc_sysdev->attach != NULL)
494 	    ? (*sc->sc_sysdev->attach)(sc) : uart_tty_attach(sc);
495 	if (error)
496 		goto fail;
497 
498 	if (sc->sc_sysdev != NULL)
499 		sc->sc_sysdev->hwmtx = sc->sc_hwmtx;
500 
501 	sc->sc_leaving = 0;
502 	uart_intr(sc);
503 	return (0);
504 
505  fail:
506 	free(sc->sc_txbuf, M_UART);
507 	free(sc->sc_rxbuf, M_UART);
508 
509 	if (sc->sc_ires != NULL) {
510 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
511 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
512 		    sc->sc_ires);
513 	}
514 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
515 
516 	mtx_destroy(&sc->sc_hwmtx_s);
517 
518 	return (error);
519 }
520 
521 int
522 uart_bus_detach(device_t dev)
523 {
524 	struct uart_softc *sc;
525 
526 	sc = device_get_softc(dev);
527 
528 	sc->sc_leaving = 1;
529 
530 	if (sc->sc_sysdev != NULL)
531 		sc->sc_sysdev->hwmtx = NULL;
532 
533 	UART_DETACH(sc);
534 
535 	if (sc->sc_sysdev != NULL && sc->sc_sysdev->detach != NULL)
536 		(*sc->sc_sysdev->detach)(sc);
537 	else
538 		uart_tty_detach(sc);
539 
540 	free(sc->sc_txbuf, M_UART);
541 	free(sc->sc_rxbuf, M_UART);
542 
543 	if (sc->sc_ires != NULL) {
544 		bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie);
545 		bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid,
546 		    sc->sc_ires);
547 	}
548 	bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres);
549 
550 	mtx_destroy(&sc->sc_hwmtx_s);
551 
552 	if (sc->sc_class->size > sizeof(*sc)) {
553 		device_set_softc(dev, NULL);
554 		free(sc, M_UART);
555 	} else
556 		device_set_softc(dev, NULL);
557 
558 	return (0);
559 }
560