xref: /openbmc/linux/include/linux/serial_core.h (revision f59e2391)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  linux/drivers/char/serial_core.h
4  *
5  *  Copyright (C) 2000 Deep Blue Solutions Ltd.
6  */
7 #ifndef LINUX_SERIAL_CORE_H
8 #define LINUX_SERIAL_CORE_H
9 
10 #include <linux/bitops.h>
11 #include <linux/compiler.h>
12 #include <linux/console.h>
13 #include <linux/interrupt.h>
14 #include <linux/circ_buf.h>
15 #include <linux/spinlock.h>
16 #include <linux/sched.h>
17 #include <linux/tty.h>
18 #include <linux/mutex.h>
19 #include <linux/sysrq.h>
20 #include <uapi/linux/serial_core.h>
21 
22 #ifdef CONFIG_SERIAL_CORE_CONSOLE
23 #define uart_console(port) \
24 	((port)->cons && (port)->cons->index == (port)->line)
25 #else
26 #define uart_console(port)      ({ (void)port; 0; })
27 #endif
28 
29 struct uart_port;
30 struct serial_struct;
31 struct serial_port_device;
32 struct device;
33 struct gpio_desc;
34 
35 /**
36  * struct uart_ops -- interface between serial_core and the driver
37  *
38  * This structure describes all the operations that can be done on the
39  * physical hardware.
40  *
41  * @tx_empty: ``unsigned int ()(struct uart_port *port)``
42  *
43  *	This function tests whether the transmitter fifo and shifter for the
44  *	@port is empty. If it is empty, this function should return
45  *	%TIOCSER_TEMT, otherwise return 0. If the port does not support this
46  *	operation, then it should return %TIOCSER_TEMT.
47  *
48  *	Locking: none.
49  *	Interrupts: caller dependent.
50  *	This call must not sleep
51  *
52  * @set_mctrl: ``void ()(struct uart_port *port, unsigned int mctrl)``
53  *
54  *	This function sets the modem control lines for @port to the state
55  *	described by @mctrl. The relevant bits of @mctrl are:
56  *
57  *		- %TIOCM_RTS	RTS signal.
58  *		- %TIOCM_DTR	DTR signal.
59  *		- %TIOCM_OUT1	OUT1 signal.
60  *		- %TIOCM_OUT2	OUT2 signal.
61  *		- %TIOCM_LOOP	Set the port into loopback mode.
62  *
63  *	If the appropriate bit is set, the signal should be driven
64  *	active.  If the bit is clear, the signal should be driven
65  *	inactive.
66  *
67  *	Locking: @port->lock taken.
68  *	Interrupts: locally disabled.
69  *	This call must not sleep
70  *
71  * @get_mctrl: ``unsigned int ()(struct uart_port *port)``
72  *
73  *	Returns the current state of modem control inputs of @port. The state
74  *	of the outputs should not be returned, since the core keeps track of
75  *	their state. The state information should include:
76  *
77  *		- %TIOCM_CAR	state of DCD signal
78  *		- %TIOCM_CTS	state of CTS signal
79  *		- %TIOCM_DSR	state of DSR signal
80  *		- %TIOCM_RI	state of RI signal
81  *
82  *	The bit is set if the signal is currently driven active.  If
83  *	the port does not support CTS, DCD or DSR, the driver should
84  *	indicate that the signal is permanently active. If RI is
85  *	not available, the signal should not be indicated as active.
86  *
87  *	Locking: @port->lock taken.
88  *	Interrupts: locally disabled.
89  *	This call must not sleep
90  *
91  * @stop_tx: ``void ()(struct uart_port *port)``
92  *
93  *	Stop transmitting characters. This might be due to the CTS line
94  *	becoming inactive or the tty layer indicating we want to stop
95  *	transmission due to an %XOFF character.
96  *
97  *	The driver should stop transmitting characters as soon as possible.
98  *
99  *	Locking: @port->lock taken.
100  *	Interrupts: locally disabled.
101  *	This call must not sleep
102  *
103  * @start_tx: ``void ()(struct uart_port *port)``
104  *
105  *	Start transmitting characters.
106  *
107  *	Locking: @port->lock taken.
108  *	Interrupts: locally disabled.
109  *	This call must not sleep
110  *
111  * @throttle: ``void ()(struct uart_port *port)``
112  *
113  *	Notify the serial driver that input buffers for the line discipline are
114  *	close to full, and it should somehow signal that no more characters
115  *	should be sent to the serial port.
116  *	This will be called only if hardware assisted flow control is enabled.
117  *
118  *	Locking: serialized with @unthrottle() and termios modification by the
119  *	tty layer.
120  *
121  * @unthrottle: ``void ()(struct uart_port *port)``
122  *
123  *	Notify the serial driver that characters can now be sent to the serial
124  *	port without fear of overrunning the input buffers of the line
125  *	disciplines.
126  *
127  *	This will be called only if hardware assisted flow control is enabled.
128  *
129  *	Locking: serialized with @throttle() and termios modification by the
130  *	tty layer.
131  *
132  * @send_xchar: ``void ()(struct uart_port *port, char ch)``
133  *
134  *	Transmit a high priority character, even if the port is stopped. This
135  *	is used to implement XON/XOFF flow control and tcflow(). If the serial
136  *	driver does not implement this function, the tty core will append the
137  *	character to the circular buffer and then call start_tx() / stop_tx()
138  *	to flush the data out.
139  *
140  *	Do not transmit if @ch == '\0' (%__DISABLED_CHAR).
141  *
142  *	Locking: none.
143  *	Interrupts: caller dependent.
144  *
145  * @start_rx: ``void ()(struct uart_port *port)``
146  *
147  *	Start receiving characters.
148  *
149  *	Locking: @port->lock taken.
150  *	Interrupts: locally disabled.
151  *	This call must not sleep
152  *
153  * @stop_rx: ``void ()(struct uart_port *port)``
154  *
155  *	Stop receiving characters; the @port is in the process of being closed.
156  *
157  *	Locking: @port->lock taken.
158  *	Interrupts: locally disabled.
159  *	This call must not sleep
160  *
161  * @enable_ms: ``void ()(struct uart_port *port)``
162  *
163  *	Enable the modem status interrupts.
164  *
165  *	This method may be called multiple times. Modem status interrupts
166  *	should be disabled when the @shutdown() method is called.
167  *
168  *	Locking: @port->lock taken.
169  *	Interrupts: locally disabled.
170  *	This call must not sleep
171  *
172  * @break_ctl: ``void ()(struct uart_port *port, int ctl)``
173  *
174  *	Control the transmission of a break signal. If @ctl is nonzero, the
175  *	break signal should be transmitted. The signal should be terminated
176  *	when another call is made with a zero @ctl.
177  *
178  *	Locking: caller holds tty_port->mutex
179  *
180  * @startup: ``int ()(struct uart_port *port)``
181  *
182  *	Grab any interrupt resources and initialise any low level driver state.
183  *	Enable the port for reception. It should not activate RTS nor DTR;
184  *	this will be done via a separate call to @set_mctrl().
185  *
186  *	This method will only be called when the port is initially opened.
187  *
188  *	Locking: port_sem taken.
189  *	Interrupts: globally disabled.
190  *
191  * @shutdown: ``void ()(struct uart_port *port)``
192  *
193  *	Disable the @port, disable any break condition that may be in effect,
194  *	and free any interrupt resources. It should not disable RTS nor DTR;
195  *	this will have already been done via a separate call to @set_mctrl().
196  *
197  *	Drivers must not access @port->state once this call has completed.
198  *
199  *	This method will only be called when there are no more users of this
200  *	@port.
201  *
202  *	Locking: port_sem taken.
203  *	Interrupts: caller dependent.
204  *
205  * @flush_buffer: ``void ()(struct uart_port *port)``
206  *
207  *	Flush any write buffers, reset any DMA state and stop any ongoing DMA
208  *	transfers.
209  *
210  *	This will be called whenever the @port->state->xmit circular buffer is
211  *	cleared.
212  *
213  *	Locking: @port->lock taken.
214  *	Interrupts: locally disabled.
215  *	This call must not sleep
216  *
217  * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new,
218  *			struct ktermios *old)``
219  *
220  *	Change the @port parameters, including word length, parity, stop bits.
221  *	Update @port->read_status_mask and @port->ignore_status_mask to
222  *	indicate the types of events we are interested in receiving. Relevant
223  *	ktermios::c_cflag bits are:
224  *
225  *	- %CSIZE - word size
226  *	- %CSTOPB - 2 stop bits
227  *	- %PARENB - parity enable
228  *	- %PARODD - odd parity (when %PARENB is in force)
229  *	- %ADDRB - address bit (changed through uart_port::rs485_config()).
230  *	- %CREAD - enable reception of characters (if not set, still receive
231  *	  characters from the port, but throw them away).
232  *	- %CRTSCTS - if set, enable CTS status change reporting.
233  *	- %CLOCAL - if not set, enable modem status change reporting.
234  *
235  *	Relevant ktermios::c_iflag bits are:
236  *
237  *	- %INPCK - enable frame and parity error events to be passed to the TTY
238  *	  layer.
239  *	- %BRKINT / %PARMRK - both of these enable break events to be passed to
240  *	  the TTY layer.
241  *	- %IGNPAR - ignore parity and framing errors.
242  *	- %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun
243  *	  errors as well.
244  *
245  *	The interaction of the ktermios::c_iflag bits is as follows (parity
246  *	error given as an example):
247  *
248  *	============ ======= ======= =========================================
249  *	Parity error INPCK   IGNPAR
250  *	============ ======= ======= =========================================
251  *	n/a	     0	     n/a     character received, marked as %TTY_NORMAL
252  *	None	     1	     n/a     character received, marked as %TTY_NORMAL
253  *	Yes	     1	     0	     character received, marked as %TTY_PARITY
254  *	Yes	     1	     1	     character discarded
255  *	============ ======= ======= =========================================
256  *
257  *	Other flags may be used (eg, xon/xoff characters) if your hardware
258  *	supports hardware "soft" flow control.
259  *
260  *	Locking: caller holds tty_port->mutex
261  *	Interrupts: caller dependent.
262  *	This call must not sleep
263  *
264  * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)``
265  *
266  *	Notifier for discipline change. See
267  *	Documentation/driver-api/tty/tty_ldisc.rst.
268  *
269  *	Locking: caller holds tty_port->mutex
270  *
271  * @pm: ``void ()(struct uart_port *port, unsigned int state,
272  *		 unsigned int oldstate)``
273  *
274  *	Perform any power management related activities on the specified @port.
275  *	@state indicates the new state (defined by enum uart_pm_state),
276  *	@oldstate indicates the previous state.
277  *
278  *	This function should not be used to grab any resources.
279  *
280  *	This will be called when the @port is initially opened and finally
281  *	closed, except when the @port is also the system console. This will
282  *	occur even if %CONFIG_PM is not set.
283  *
284  *	Locking: none.
285  *	Interrupts: caller dependent.
286  *
287  * @type: ``const char *()(struct uart_port *port)``
288  *
289  *	Return a pointer to a string constant describing the specified @port,
290  *	or return %NULL, in which case the string 'unknown' is substituted.
291  *
292  *	Locking: none.
293  *	Interrupts: caller dependent.
294  *
295  * @release_port: ``void ()(struct uart_port *port)``
296  *
297  *	Release any memory and IO region resources currently in use by the
298  *	@port.
299  *
300  *	Locking: none.
301  *	Interrupts: caller dependent.
302  *
303  * @request_port: ``int ()(struct uart_port *port)``
304  *
305  *	Request any memory and IO region resources required by the port. If any
306  *	fail, no resources should be registered when this function returns, and
307  *	it should return -%EBUSY on failure.
308  *
309  *	Locking: none.
310  *	Interrupts: caller dependent.
311  *
312  * @config_port: ``void ()(struct uart_port *port, int type)``
313  *
314  *	Perform any autoconfiguration steps required for the @port. @type
315  *	contains a bit mask of the required configuration. %UART_CONFIG_TYPE
316  *	indicates that the port requires detection and identification.
317  *	@port->type should be set to the type found, or %PORT_UNKNOWN if no
318  *	port was detected.
319  *
320  *	%UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
321  *	which should be probed using standard kernel autoprobing techniques.
322  *	This is not necessary on platforms where ports have interrupts
323  *	internally hard wired (eg, system on a chip implementations).
324  *
325  *	Locking: none.
326  *	Interrupts: caller dependent.
327  *
328  * @verify_port: ``int ()(struct uart_port *port,
329  *			struct serial_struct *serinfo)``
330  *
331  *	Verify the new serial port information contained within @serinfo is
332  *	suitable for this port type.
333  *
334  *	Locking: none.
335  *	Interrupts: caller dependent.
336  *
337  * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd,
338  *		unsigned long arg)``
339  *
340  *	Perform any port specific IOCTLs. IOCTL commands must be defined using
341  *	the standard numbering system found in <asm/ioctl.h>.
342  *
343  *	Locking: none.
344  *	Interrupts: caller dependent.
345  *
346  * @poll_init: ``int ()(struct uart_port *port)``
347  *
348  *	Called by kgdb to perform the minimal hardware initialization needed to
349  *	support @poll_put_char() and @poll_get_char(). Unlike @startup(), this
350  *	should not request interrupts.
351  *
352  *	Locking: %tty_mutex and tty_port->mutex taken.
353  *	Interrupts: n/a.
354  *
355  * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)``
356  *
357  *	Called by kgdb to write a single character @ch directly to the serial
358  *	@port. It can and should block until there is space in the TX FIFO.
359  *
360  *	Locking: none.
361  *	Interrupts: caller dependent.
362  *	This call must not sleep
363  *
364  * @poll_get_char: ``int ()(struct uart_port *port)``
365  *
366  *	Called by kgdb to read a single character directly from the serial
367  *	port. If data is available, it should be returned; otherwise the
368  *	function should return %NO_POLL_CHAR immediately.
369  *
370  *	Locking: none.
371  *	Interrupts: caller dependent.
372  *	This call must not sleep
373  */
374 struct uart_ops {
375 	unsigned int	(*tx_empty)(struct uart_port *);
376 	void		(*set_mctrl)(struct uart_port *, unsigned int mctrl);
377 	unsigned int	(*get_mctrl)(struct uart_port *);
378 	void		(*stop_tx)(struct uart_port *);
379 	void		(*start_tx)(struct uart_port *);
380 	void		(*throttle)(struct uart_port *);
381 	void		(*unthrottle)(struct uart_port *);
382 	void		(*send_xchar)(struct uart_port *, char ch);
383 	void		(*stop_rx)(struct uart_port *);
384 	void		(*start_rx)(struct uart_port *);
385 	void		(*enable_ms)(struct uart_port *);
386 	void		(*break_ctl)(struct uart_port *, int ctl);
387 	int		(*startup)(struct uart_port *);
388 	void		(*shutdown)(struct uart_port *);
389 	void		(*flush_buffer)(struct uart_port *);
390 	void		(*set_termios)(struct uart_port *, struct ktermios *new,
391 				       const struct ktermios *old);
392 	void		(*set_ldisc)(struct uart_port *, struct ktermios *);
393 	void		(*pm)(struct uart_port *, unsigned int state,
394 			      unsigned int oldstate);
395 	const char	*(*type)(struct uart_port *);
396 	void		(*release_port)(struct uart_port *);
397 	int		(*request_port)(struct uart_port *);
398 	void		(*config_port)(struct uart_port *, int);
399 	int		(*verify_port)(struct uart_port *, struct serial_struct *);
400 	int		(*ioctl)(struct uart_port *, unsigned int, unsigned long);
401 #ifdef CONFIG_CONSOLE_POLL
402 	int		(*poll_init)(struct uart_port *);
403 	void		(*poll_put_char)(struct uart_port *, unsigned char);
404 	int		(*poll_get_char)(struct uart_port *);
405 #endif
406 };
407 
408 #define NO_POLL_CHAR		0x00ff0000
409 #define UART_CONFIG_TYPE	(1 << 0)
410 #define UART_CONFIG_IRQ		(1 << 1)
411 
412 struct uart_icount {
413 	__u32	cts;
414 	__u32	dsr;
415 	__u32	rng;
416 	__u32	dcd;
417 	__u32	rx;
418 	__u32	tx;
419 	__u32	frame;
420 	__u32	overrun;
421 	__u32	parity;
422 	__u32	brk;
423 	__u32	buf_overrun;
424 };
425 
426 typedef u64 __bitwise upf_t;
427 typedef unsigned int __bitwise upstat_t;
428 
429 struct uart_port {
430 	spinlock_t		lock;			/* port lock */
431 	unsigned long		iobase;			/* in/out[bwl] */
432 	unsigned char __iomem	*membase;		/* read/write[bwl] */
433 	unsigned int		(*serial_in)(struct uart_port *, int);
434 	void			(*serial_out)(struct uart_port *, int, int);
435 	void			(*set_termios)(struct uart_port *,
436 				               struct ktermios *new,
437 				               const struct ktermios *old);
438 	void			(*set_ldisc)(struct uart_port *,
439 					     struct ktermios *);
440 	unsigned int		(*get_mctrl)(struct uart_port *);
441 	void			(*set_mctrl)(struct uart_port *, unsigned int);
442 	unsigned int		(*get_divisor)(struct uart_port *,
443 					       unsigned int baud,
444 					       unsigned int *frac);
445 	void			(*set_divisor)(struct uart_port *,
446 					       unsigned int baud,
447 					       unsigned int quot,
448 					       unsigned int quot_frac);
449 	int			(*startup)(struct uart_port *port);
450 	void			(*shutdown)(struct uart_port *port);
451 	void			(*throttle)(struct uart_port *port);
452 	void			(*unthrottle)(struct uart_port *port);
453 	int			(*handle_irq)(struct uart_port *);
454 	void			(*pm)(struct uart_port *, unsigned int state,
455 				      unsigned int old);
456 	void			(*handle_break)(struct uart_port *);
457 	int			(*rs485_config)(struct uart_port *,
458 						struct ktermios *termios,
459 						struct serial_rs485 *rs485);
460 	int			(*iso7816_config)(struct uart_port *,
461 						  struct serial_iso7816 *iso7816);
462 	unsigned int		ctrl_id;		/* optional serial core controller id */
463 	unsigned int		port_id;		/* optional serial core port id */
464 	unsigned int		irq;			/* irq number */
465 	unsigned long		irqflags;		/* irq flags  */
466 	unsigned int		uartclk;		/* base uart clock */
467 	unsigned int		fifosize;		/* tx fifo size */
468 	unsigned char		x_char;			/* xon/xoff char */
469 	unsigned char		regshift;		/* reg offset shift */
470 	unsigned char		iotype;			/* io access style */
471 	unsigned char		quirks;			/* internal quirks */
472 
473 #define UPIO_UNKNOWN		((unsigned char)~0U)	/* UCHAR_MAX */
474 #define UPIO_PORT		(SERIAL_IO_PORT)	/* 8b I/O port access */
475 #define UPIO_HUB6		(SERIAL_IO_HUB6)	/* Hub6 ISA card */
476 #define UPIO_MEM		(SERIAL_IO_MEM)		/* driver-specific */
477 #define UPIO_MEM32		(SERIAL_IO_MEM32)	/* 32b little endian */
478 #define UPIO_AU			(SERIAL_IO_AU)		/* Au1x00 and RT288x type IO */
479 #define UPIO_TSI		(SERIAL_IO_TSI)		/* Tsi108/109 type IO */
480 #define UPIO_MEM32BE		(SERIAL_IO_MEM32BE)	/* 32b big endian */
481 #define UPIO_MEM16		(SERIAL_IO_MEM16)	/* 16b little endian */
482 
483 	/* quirks must be updated while holding port mutex */
484 #define UPQ_NO_TXEN_TEST	BIT(0)
485 
486 	unsigned int		read_status_mask;	/* driver specific */
487 	unsigned int		ignore_status_mask;	/* driver specific */
488 	struct uart_state	*state;			/* pointer to parent state */
489 	struct uart_icount	icount;			/* statistics */
490 
491 	struct console		*cons;			/* struct console, if any */
492 	/* flags must be updated while holding port mutex */
493 	upf_t			flags;
494 
495 	/*
496 	 * These flags must be equivalent to the flags defined in
497 	 * include/uapi/linux/tty_flags.h which are the userspace definitions
498 	 * assigned from the serial_struct flags in uart_set_info()
499 	 * [for bit definitions in the UPF_CHANGE_MASK]
500 	 *
501 	 * Bits [0..ASYNCB_LAST_USER] are userspace defined/visible/changeable
502 	 * The remaining bits are serial-core specific and not modifiable by
503 	 * userspace.
504 	 */
505 #define UPF_FOURPORT		((__force upf_t) ASYNC_FOURPORT       /* 1  */ )
506 #define UPF_SAK			((__force upf_t) ASYNC_SAK            /* 2  */ )
507 #define UPF_SPD_HI		((__force upf_t) ASYNC_SPD_HI         /* 4  */ )
508 #define UPF_SPD_VHI		((__force upf_t) ASYNC_SPD_VHI        /* 5  */ )
509 #define UPF_SPD_CUST		((__force upf_t) ASYNC_SPD_CUST   /* 0x0030 */ )
510 #define UPF_SPD_WARP		((__force upf_t) ASYNC_SPD_WARP   /* 0x1010 */ )
511 #define UPF_SPD_MASK		((__force upf_t) ASYNC_SPD_MASK   /* 0x1030 */ )
512 #define UPF_SKIP_TEST		((__force upf_t) ASYNC_SKIP_TEST      /* 6  */ )
513 #define UPF_AUTO_IRQ		((__force upf_t) ASYNC_AUTO_IRQ       /* 7  */ )
514 #define UPF_HARDPPS_CD		((__force upf_t) ASYNC_HARDPPS_CD     /* 11 */ )
515 #define UPF_SPD_SHI		((__force upf_t) ASYNC_SPD_SHI        /* 12 */ )
516 #define UPF_LOW_LATENCY		((__force upf_t) ASYNC_LOW_LATENCY    /* 13 */ )
517 #define UPF_BUGGY_UART		((__force upf_t) ASYNC_BUGGY_UART     /* 14 */ )
518 #define UPF_MAGIC_MULTIPLIER	((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ )
519 
520 #define UPF_NO_THRE_TEST	((__force upf_t) BIT_ULL(19))
521 /* Port has hardware-assisted h/w flow control */
522 #define UPF_AUTO_CTS		((__force upf_t) BIT_ULL(20))
523 #define UPF_AUTO_RTS		((__force upf_t) BIT_ULL(21))
524 #define UPF_HARD_FLOW		((__force upf_t) (UPF_AUTO_CTS | UPF_AUTO_RTS))
525 /* Port has hardware-assisted s/w flow control */
526 #define UPF_SOFT_FLOW		((__force upf_t) BIT_ULL(22))
527 #define UPF_CONS_FLOW		((__force upf_t) BIT_ULL(23))
528 #define UPF_SHARE_IRQ		((__force upf_t) BIT_ULL(24))
529 #define UPF_EXAR_EFR		((__force upf_t) BIT_ULL(25))
530 #define UPF_BUG_THRE		((__force upf_t) BIT_ULL(26))
531 /* The exact UART type is known and should not be probed.  */
532 #define UPF_FIXED_TYPE		((__force upf_t) BIT_ULL(27))
533 #define UPF_BOOT_AUTOCONF	((__force upf_t) BIT_ULL(28))
534 #define UPF_FIXED_PORT		((__force upf_t) BIT_ULL(29))
535 #define UPF_DEAD		((__force upf_t) BIT_ULL(30))
536 #define UPF_IOREMAP		((__force upf_t) BIT_ULL(31))
537 #define UPF_FULL_PROBE		((__force upf_t) BIT_ULL(32))
538 
539 #define __UPF_CHANGE_MASK	0x17fff
540 #define UPF_CHANGE_MASK		((__force upf_t) __UPF_CHANGE_MASK)
541 #define UPF_USR_MASK		((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
542 
543 #if __UPF_CHANGE_MASK > ASYNC_FLAGS
544 #error Change mask not equivalent to userspace-visible bit defines
545 #endif
546 
547 	/*
548 	 * Must hold termios_rwsem, port mutex and port lock to change;
549 	 * can hold any one lock to read.
550 	 */
551 	upstat_t		status;
552 
553 #define UPSTAT_CTS_ENABLE	((__force upstat_t) (1 << 0))
554 #define UPSTAT_DCD_ENABLE	((__force upstat_t) (1 << 1))
555 #define UPSTAT_AUTORTS		((__force upstat_t) (1 << 2))
556 #define UPSTAT_AUTOCTS		((__force upstat_t) (1 << 3))
557 #define UPSTAT_AUTOXOFF		((__force upstat_t) (1 << 4))
558 #define UPSTAT_SYNC_FIFO	((__force upstat_t) (1 << 5))
559 
560 	bool			hw_stopped;		/* sw-assisted CTS flow state */
561 	unsigned int		mctrl;			/* current modem ctrl settings */
562 	unsigned int		frame_time;		/* frame timing in ns */
563 	unsigned int		type;			/* port type */
564 	const struct uart_ops	*ops;
565 	unsigned int		custom_divisor;
566 	unsigned int		line;			/* port index */
567 	unsigned int		minor;
568 	resource_size_t		mapbase;		/* for ioremap */
569 	resource_size_t		mapsize;
570 	struct device		*dev;			/* serial port physical parent device */
571 	struct serial_port_device *port_dev;		/* serial core port device */
572 
573 	unsigned long		sysrq;			/* sysrq timeout */
574 	u8			sysrq_ch;		/* char for sysrq */
575 	unsigned char		has_sysrq;
576 	unsigned char		sysrq_seq;		/* index in sysrq_toggle_seq */
577 
578 	unsigned char		hub6;			/* this should be in the 8250 driver */
579 	unsigned char		suspended;
580 	unsigned char		console_reinit;
581 	const char		*name;			/* port name */
582 	struct attribute_group	*attr_group;		/* port specific attributes */
583 	const struct attribute_group **tty_groups;	/* all attributes (serial core use only) */
584 	struct serial_rs485     rs485;
585 	struct serial_rs485	rs485_supported;	/* Supported mask for serial_rs485 */
586 	struct gpio_desc	*rs485_term_gpio;	/* enable RS485 bus termination */
587 	struct gpio_desc	*rs485_rx_during_tx_gpio; /* Output GPIO that sets the state of RS485 RX during TX */
588 	struct serial_iso7816   iso7816;
589 	void			*private_data;		/* generic platform data pointer */
590 };
591 
592 /**
593  * uart_port_lock - Lock the UART port
594  * @up:		Pointer to UART port structure
595  */
uart_port_lock(struct uart_port * up)596 static inline void uart_port_lock(struct uart_port *up)
597 {
598 	spin_lock(&up->lock);
599 }
600 
601 /**
602  * uart_port_lock_irq - Lock the UART port and disable interrupts
603  * @up:		Pointer to UART port structure
604  */
uart_port_lock_irq(struct uart_port * up)605 static inline void uart_port_lock_irq(struct uart_port *up)
606 {
607 	spin_lock_irq(&up->lock);
608 }
609 
610 /**
611  * uart_port_lock_irqsave - Lock the UART port, save and disable interrupts
612  * @up:		Pointer to UART port structure
613  * @flags:	Pointer to interrupt flags storage
614  */
uart_port_lock_irqsave(struct uart_port * up,unsigned long * flags)615 static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
616 {
617 	spin_lock_irqsave(&up->lock, *flags);
618 }
619 
620 /**
621  * uart_port_trylock - Try to lock the UART port
622  * @up:		Pointer to UART port structure
623  *
624  * Returns: True if lock was acquired, false otherwise
625  */
uart_port_trylock(struct uart_port * up)626 static inline bool uart_port_trylock(struct uart_port *up)
627 {
628 	return spin_trylock(&up->lock);
629 }
630 
631 /**
632  * uart_port_trylock_irqsave - Try to lock the UART port, save and disable interrupts
633  * @up:		Pointer to UART port structure
634  * @flags:	Pointer to interrupt flags storage
635  *
636  * Returns: True if lock was acquired, false otherwise
637  */
uart_port_trylock_irqsave(struct uart_port * up,unsigned long * flags)638 static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long *flags)
639 {
640 	return spin_trylock_irqsave(&up->lock, *flags);
641 }
642 
643 /**
644  * uart_port_unlock - Unlock the UART port
645  * @up:		Pointer to UART port structure
646  */
uart_port_unlock(struct uart_port * up)647 static inline void uart_port_unlock(struct uart_port *up)
648 {
649 	spin_unlock(&up->lock);
650 }
651 
652 /**
653  * uart_port_unlock_irq - Unlock the UART port and re-enable interrupts
654  * @up:		Pointer to UART port structure
655  */
uart_port_unlock_irq(struct uart_port * up)656 static inline void uart_port_unlock_irq(struct uart_port *up)
657 {
658 	spin_unlock_irq(&up->lock);
659 }
660 
661 /**
662  * uart_port_unlock_irqrestore - Unlock the UART port, restore interrupts
663  * @up:		Pointer to UART port structure
664  * @flags:	The saved interrupt flags for restore
665  */
uart_port_unlock_irqrestore(struct uart_port * up,unsigned long flags)666 static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
667 {
668 	spin_unlock_irqrestore(&up->lock, flags);
669 }
670 
serial_port_in(struct uart_port * up,int offset)671 static inline int serial_port_in(struct uart_port *up, int offset)
672 {
673 	return up->serial_in(up, offset);
674 }
675 
serial_port_out(struct uart_port * up,int offset,int value)676 static inline void serial_port_out(struct uart_port *up, int offset, int value)
677 {
678 	up->serial_out(up, offset, value);
679 }
680 
681 /**
682  * enum uart_pm_state - power states for UARTs
683  * @UART_PM_STATE_ON: UART is powered, up and operational
684  * @UART_PM_STATE_OFF: UART is powered off
685  * @UART_PM_STATE_UNDEFINED: sentinel
686  */
687 enum uart_pm_state {
688 	UART_PM_STATE_ON = 0,
689 	UART_PM_STATE_OFF = 3, /* number taken from ACPI */
690 	UART_PM_STATE_UNDEFINED,
691 };
692 
693 /*
694  * This is the state information which is persistent across opens.
695  */
696 struct uart_state {
697 	struct tty_port		port;
698 
699 	enum uart_pm_state	pm_state;
700 	struct circ_buf		xmit;
701 
702 	atomic_t		refcount;
703 	wait_queue_head_t	remove_wait;
704 	struct uart_port	*uart_port;
705 };
706 
707 #define UART_XMIT_SIZE	PAGE_SIZE
708 
709 
710 /* number of characters left in xmit buffer before we ask for more */
711 #define WAKEUP_CHARS		256
712 
713 /**
714  * uart_xmit_advance - Advance xmit buffer and account Tx'ed chars
715  * @up: uart_port structure describing the port
716  * @chars: number of characters sent
717  *
718  * This function advances the tail of circular xmit buffer by the number of
719  * @chars transmitted and handles accounting of transmitted bytes (into
720  * @up's icount.tx).
721  */
uart_xmit_advance(struct uart_port * up,unsigned int chars)722 static inline void uart_xmit_advance(struct uart_port *up, unsigned int chars)
723 {
724 	struct circ_buf *xmit = &up->state->xmit;
725 
726 	xmit->tail = (xmit->tail + chars) & (UART_XMIT_SIZE - 1);
727 	up->icount.tx += chars;
728 }
729 
730 struct module;
731 struct tty_driver;
732 
733 struct uart_driver {
734 	struct module		*owner;
735 	const char		*driver_name;
736 	const char		*dev_name;
737 	int			 major;
738 	int			 minor;
739 	int			 nr;
740 	struct console		*cons;
741 
742 	/*
743 	 * these are private; the low level driver should not
744 	 * touch these; they should be initialised to NULL
745 	 */
746 	struct uart_state	*state;
747 	struct tty_driver	*tty_driver;
748 };
749 
750 void uart_write_wakeup(struct uart_port *port);
751 
752 /**
753  * enum UART_TX_FLAGS -- flags for uart_port_tx_flags()
754  *
755  * @UART_TX_NOSTOP: don't call port->ops->stop_tx() on empty buffer
756  */
757 enum UART_TX_FLAGS {
758 	UART_TX_NOSTOP = BIT(0),
759 };
760 
761 #define __uart_port_tx(uport, ch, flags, tx_ready, put_char, tx_done,	      \
762 		       for_test, for_post)				      \
763 ({									      \
764 	struct uart_port *__port = (uport);				      \
765 	struct circ_buf *xmit = &__port->state->xmit;			      \
766 	unsigned int pending;						      \
767 									      \
768 	for (; (for_test) && (tx_ready); (for_post), __port->icount.tx++) {   \
769 		if (__port->x_char) {					      \
770 			(ch) = __port->x_char;				      \
771 			(put_char);					      \
772 			__port->x_char = 0;				      \
773 			continue;					      \
774 		}							      \
775 									      \
776 		if (uart_circ_empty(xmit) || uart_tx_stopped(__port))	      \
777 			break;						      \
778 									      \
779 		(ch) = xmit->buf[xmit->tail];				      \
780 		(put_char);						      \
781 		xmit->tail = (xmit->tail + 1) % UART_XMIT_SIZE;		      \
782 	}								      \
783 									      \
784 	(tx_done);							      \
785 									      \
786 	pending = uart_circ_chars_pending(xmit);			      \
787 	if (pending < WAKEUP_CHARS) {					      \
788 		uart_write_wakeup(__port);				      \
789 									      \
790 		if (!((flags) & UART_TX_NOSTOP) && pending == 0 &&	      \
791 		    __port->ops->tx_empty(__port))			      \
792 			__port->ops->stop_tx(__port);			      \
793 	}								      \
794 									      \
795 	pending;							      \
796 })
797 
798 /**
799  * uart_port_tx_limited -- transmit helper for uart_port with count limiting
800  * @port: uart port
801  * @ch: variable to store a character to be written to the HW
802  * @count: a limit of characters to send
803  * @tx_ready: can HW accept more data function
804  * @put_char: function to write a character
805  * @tx_done: function to call after the loop is done
806  *
807  * This helper transmits characters from the xmit buffer to the hardware using
808  * @put_char(). It does so until @count characters are sent and while @tx_ready
809  * evaluates to true.
810  *
811  * Returns: the number of characters in the xmit buffer when done.
812  *
813  * The expression in macro parameters shall be designed as follows:
814  *  * **tx_ready:** should evaluate to true if the HW can accept more data to
815  *    be sent. This parameter can be %true, which means the HW is always ready.
816  *  * **put_char:** shall write @ch to the device of @port.
817  *  * **tx_done:** when the write loop is done, this can perform arbitrary
818  *    action before potential invocation of ops->stop_tx() happens. If the
819  *    driver does not need to do anything, use e.g. ({}).
820  *
821  * For all of them, @port->lock is held, interrupts are locally disabled and
822  * the expressions must not sleep.
823  */
824 #define uart_port_tx_limited(port, ch, count, tx_ready, put_char, tx_done) ({ \
825 	unsigned int __count = (count);					      \
826 	__uart_port_tx(port, ch, 0, tx_ready, put_char, tx_done, __count,     \
827 			__count--);					      \
828 })
829 
830 /**
831  * uart_port_tx -- transmit helper for uart_port
832  * @port: uart port
833  * @ch: variable to store a character to be written to the HW
834  * @tx_ready: can HW accept more data function
835  * @put_char: function to write a character
836  *
837  * See uart_port_tx_limited() for more details.
838  */
839 #define uart_port_tx(port, ch, tx_ready, put_char)			\
840 	__uart_port_tx(port, ch, 0, tx_ready, put_char, ({}), true, ({}))
841 
842 
843 /**
844  * uart_port_tx_flags -- transmit helper for uart_port with flags
845  * @port: uart port
846  * @ch: variable to store a character to be written to the HW
847  * @flags: %UART_TX_NOSTOP or similar
848  * @tx_ready: can HW accept more data function
849  * @put_char: function to write a character
850  *
851  * See uart_port_tx_limited() for more details.
852  */
853 #define uart_port_tx_flags(port, ch, flags, tx_ready, put_char)		\
854 	__uart_port_tx(port, ch, flags, tx_ready, put_char, ({}), true, ({}))
855 /*
856  * Baud rate helpers.
857  */
858 void uart_update_timeout(struct uart_port *port, unsigned int cflag,
859 			 unsigned int baud);
860 unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
861 				const struct ktermios *old, unsigned int min,
862 				unsigned int max);
863 unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);
864 
865 /*
866  * Calculates FIFO drain time.
867  */
uart_fifo_timeout(struct uart_port * port)868 static inline unsigned long uart_fifo_timeout(struct uart_port *port)
869 {
870 	u64 fifo_timeout = (u64)READ_ONCE(port->frame_time) * port->fifosize;
871 
872 	/* Add .02 seconds of slop */
873 	fifo_timeout += 20 * NSEC_PER_MSEC;
874 
875 	return max(nsecs_to_jiffies(fifo_timeout), 1UL);
876 }
877 
878 /* Base timer interval for polling */
uart_poll_timeout(struct uart_port * port)879 static inline int uart_poll_timeout(struct uart_port *port)
880 {
881 	int timeout = uart_fifo_timeout(port);
882 
883 	return timeout > 6 ? (timeout / 2 - 2) : 1;
884 }
885 
886 /*
887  * Console helpers.
888  */
889 struct earlycon_device {
890 	struct console *con;
891 	struct uart_port port;
892 	char options[32];		/* e.g., 115200n8 */
893 	unsigned int baud;
894 };
895 
896 struct earlycon_id {
897 	char	name[15];
898 	char	name_term;	/* In case compiler didn't '\0' term name */
899 	char	compatible[128];
900 	int	(*setup)(struct earlycon_device *, const char *options);
901 };
902 
903 extern const struct earlycon_id __earlycon_table[];
904 extern const struct earlycon_id __earlycon_table_end[];
905 
906 #if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE)
907 #define EARLYCON_USED_OR_UNUSED	__used
908 #else
909 #define EARLYCON_USED_OR_UNUSED	__maybe_unused
910 #endif
911 
912 #define OF_EARLYCON_DECLARE(_name, compat, fn)				\
913 	static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \
914 		EARLYCON_USED_OR_UNUSED  __section("__earlycon_table")  \
915 		__aligned(__alignof__(struct earlycon_id))		\
916 		= { .name = __stringify(_name),				\
917 		    .compatible = compat,				\
918 		    .setup = fn }
919 
920 #define EARLYCON_DECLARE(_name, fn)	OF_EARLYCON_DECLARE(_name, "", fn)
921 
922 int of_setup_earlycon(const struct earlycon_id *match, unsigned long node,
923 		      const char *options);
924 
925 #ifdef CONFIG_SERIAL_EARLYCON
926 extern bool earlycon_acpi_spcr_enable __initdata;
927 int setup_earlycon(char *buf);
928 #else
929 static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
setup_earlycon(char * buf)930 static inline int setup_earlycon(char *buf) { return 0; }
931 #endif
932 
933 /* Variant of uart_console_registered() when the console_list_lock is held. */
uart_console_registered_locked(struct uart_port * port)934 static inline bool uart_console_registered_locked(struct uart_port *port)
935 {
936 	return uart_console(port) && console_is_registered_locked(port->cons);
937 }
938 
uart_console_registered(struct uart_port * port)939 static inline bool uart_console_registered(struct uart_port *port)
940 {
941 	return uart_console(port) && console_is_registered(port->cons);
942 }
943 
944 struct uart_port *uart_get_console(struct uart_port *ports, int nr,
945 				   struct console *c);
946 int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
947 			char **options);
948 void uart_parse_options(const char *options, int *baud, int *parity, int *bits,
949 			int *flow);
950 int uart_set_options(struct uart_port *port, struct console *co, int baud,
951 		     int parity, int bits, int flow);
952 struct tty_driver *uart_console_device(struct console *co, int *index);
953 void uart_console_write(struct uart_port *port, const char *s,
954 			unsigned int count,
955 			void (*putchar)(struct uart_port *, unsigned char));
956 
957 /*
958  * Port/driver registration/removal
959  */
960 int uart_register_driver(struct uart_driver *uart);
961 void uart_unregister_driver(struct uart_driver *uart);
962 int uart_add_one_port(struct uart_driver *reg, struct uart_port *port);
963 void uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
964 int uart_read_port_properties(struct uart_port *port);
965 int uart_read_and_validate_port_properties(struct uart_port *port);
966 bool uart_match_port(const struct uart_port *port1,
967 		const struct uart_port *port2);
968 
969 /*
970  * Power Management
971  */
972 int uart_suspend_port(struct uart_driver *reg, struct uart_port *port);
973 int uart_resume_port(struct uart_driver *reg, struct uart_port *port);
974 
975 #define uart_circ_empty(circ)		((circ)->head == (circ)->tail)
976 #define uart_circ_clear(circ)		((circ)->head = (circ)->tail = 0)
977 
978 #define uart_circ_chars_pending(circ)	\
979 	(CIRC_CNT((circ)->head, (circ)->tail, UART_XMIT_SIZE))
980 
981 #define uart_circ_chars_free(circ)	\
982 	(CIRC_SPACE((circ)->head, (circ)->tail, UART_XMIT_SIZE))
983 
uart_tx_stopped(struct uart_port * port)984 static inline int uart_tx_stopped(struct uart_port *port)
985 {
986 	struct tty_struct *tty = port->state->port.tty;
987 	if ((tty && tty->flow.stopped) || port->hw_stopped)
988 		return 1;
989 	return 0;
990 }
991 
uart_cts_enabled(struct uart_port * uport)992 static inline bool uart_cts_enabled(struct uart_port *uport)
993 {
994 	return !!(uport->status & UPSTAT_CTS_ENABLE);
995 }
996 
uart_softcts_mode(struct uart_port * uport)997 static inline bool uart_softcts_mode(struct uart_port *uport)
998 {
999 	upstat_t mask = UPSTAT_CTS_ENABLE | UPSTAT_AUTOCTS;
1000 
1001 	return ((uport->status & mask) == UPSTAT_CTS_ENABLE);
1002 }
1003 
1004 /*
1005  * The following are helper functions for the low level drivers.
1006  */
1007 
1008 void uart_handle_dcd_change(struct uart_port *uport, bool active);
1009 void uart_handle_cts_change(struct uart_port *uport, bool active);
1010 
1011 void uart_insert_char(struct uart_port *port, unsigned int status,
1012 		      unsigned int overrun, u8 ch, u8 flag);
1013 
1014 void uart_xchar_out(struct uart_port *uport, int offset);
1015 
1016 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
1017 #define SYSRQ_TIMEOUT	(HZ * 5)
1018 
1019 bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch);
1020 
uart_handle_sysrq_char(struct uart_port * port,u8 ch)1021 static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
1022 {
1023 	if (!port->sysrq)
1024 		return 0;
1025 
1026 	if (ch && time_before(jiffies, port->sysrq)) {
1027 		if (sysrq_mask()) {
1028 			handle_sysrq(ch);
1029 			port->sysrq = 0;
1030 			return 1;
1031 		}
1032 		if (uart_try_toggle_sysrq(port, ch))
1033 			return 1;
1034 	}
1035 	port->sysrq = 0;
1036 
1037 	return 0;
1038 }
1039 
uart_prepare_sysrq_char(struct uart_port * port,u8 ch)1040 static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
1041 {
1042 	if (!port->sysrq)
1043 		return 0;
1044 
1045 	if (ch && time_before(jiffies, port->sysrq)) {
1046 		if (sysrq_mask()) {
1047 			port->sysrq_ch = ch;
1048 			port->sysrq = 0;
1049 			return 1;
1050 		}
1051 		if (uart_try_toggle_sysrq(port, ch))
1052 			return 1;
1053 	}
1054 	port->sysrq = 0;
1055 
1056 	return 0;
1057 }
1058 
uart_unlock_and_check_sysrq(struct uart_port * port)1059 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
1060 {
1061 	u8 sysrq_ch;
1062 
1063 	if (!port->has_sysrq) {
1064 		spin_unlock(&port->lock);
1065 		return;
1066 	}
1067 
1068 	sysrq_ch = port->sysrq_ch;
1069 	port->sysrq_ch = 0;
1070 
1071 	spin_unlock(&port->lock);
1072 
1073 	if (sysrq_ch)
1074 		handle_sysrq(sysrq_ch);
1075 }
1076 
uart_unlock_and_check_sysrq_irqrestore(struct uart_port * port,unsigned long flags)1077 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1078 		unsigned long flags)
1079 {
1080 	u8 sysrq_ch;
1081 
1082 	if (!port->has_sysrq) {
1083 		spin_unlock_irqrestore(&port->lock, flags);
1084 		return;
1085 	}
1086 
1087 	sysrq_ch = port->sysrq_ch;
1088 	port->sysrq_ch = 0;
1089 
1090 	spin_unlock_irqrestore(&port->lock, flags);
1091 
1092 	if (sysrq_ch)
1093 		handle_sysrq(sysrq_ch);
1094 }
1095 #else	/* CONFIG_MAGIC_SYSRQ_SERIAL */
uart_handle_sysrq_char(struct uart_port * port,u8 ch)1096 static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
1097 {
1098 	return 0;
1099 }
uart_prepare_sysrq_char(struct uart_port * port,u8 ch)1100 static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
1101 {
1102 	return 0;
1103 }
uart_unlock_and_check_sysrq(struct uart_port * port)1104 static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
1105 {
1106 	spin_unlock(&port->lock);
1107 }
uart_unlock_and_check_sysrq_irqrestore(struct uart_port * port,unsigned long flags)1108 static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1109 		unsigned long flags)
1110 {
1111 	spin_unlock_irqrestore(&port->lock, flags);
1112 }
1113 #endif	/* CONFIG_MAGIC_SYSRQ_SERIAL */
1114 
1115 /*
1116  * We do the SysRQ and SAK checking like this...
1117  */
uart_handle_break(struct uart_port * port)1118 static inline int uart_handle_break(struct uart_port *port)
1119 {
1120 	struct uart_state *state = port->state;
1121 
1122 	if (port->handle_break)
1123 		port->handle_break(port);
1124 
1125 #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
1126 	if (port->has_sysrq && uart_console(port)) {
1127 		if (!port->sysrq) {
1128 			port->sysrq = jiffies + SYSRQ_TIMEOUT;
1129 			return 1;
1130 		}
1131 		port->sysrq = 0;
1132 	}
1133 #endif
1134 	if (port->flags & UPF_SAK)
1135 		do_SAK(state->port.tty);
1136 	return 0;
1137 }
1138 
1139 /*
1140  *	UART_ENABLE_MS - determine if port should enable modem status irqs
1141  */
1142 #define UART_ENABLE_MS(port,cflag)	((port)->flags & UPF_HARDPPS_CD || \
1143 					 (cflag) & CRTSCTS || \
1144 					 !((cflag) & CLOCAL))
1145 
1146 int uart_get_rs485_mode(struct uart_port *port);
1147 #endif /* LINUX_SERIAL_CORE_H */
1148