xref: /linux-6.15/include/linux/serial_core.h (revision 7c7e6c89)
11a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/drivers/char/serial_core.h
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Copyright (C) 2000 Deep Blue Solutions Ltd.
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #ifndef LINUX_SERIAL_CORE_H
81da177e4SLinus Torvalds #define LINUX_SERIAL_CORE_H
91da177e4SLinus Torvalds 
10c7ac15ceSAndy Shevchenko #include <linux/bitops.h>
11661f83a6SRussell King #include <linux/compiler.h>
123e6f8806SDouglas Anderson #include <linux/console.h>
131da177e4SLinus Torvalds #include <linux/interrupt.h>
144126f149SJohn Ogness #include <linux/lockdep.h>
154126f149SJohn Ogness #include <linux/printk.h>
161da177e4SLinus Torvalds #include <linux/spinlock.h>
171da177e4SLinus Torvalds #include <linux/sched.h>
181da177e4SLinus Torvalds #include <linux/tty.h>
19e2862f6aSIngo Molnar #include <linux/mutex.h>
20b11115c1SMaciej W. Rozycki #include <linux/sysrq.h>
21607ca46eSDavid Howells #include <uapi/linux/serial_core.h>
221da177e4SLinus Torvalds 
23cf0ebee0SSourav Poddar #ifdef CONFIG_SERIAL_CORE_CONSOLE
24cf0ebee0SSourav Poddar #define uart_console(port) \
25cf0ebee0SSourav Poddar 	((port)->cons && (port)->cons->index == (port)->line)
26cf0ebee0SSourav Poddar #else
276b3cddccSPeter Hurley #define uart_console(port)      ({ (void)port; 0; })
28cf0ebee0SSourav Poddar #endif
29cf0ebee0SSourav Poddar 
301da177e4SLinus Torvalds struct uart_port;
311da177e4SLinus Torvalds struct serial_struct;
3284a9582fSTony Lindgren struct serial_port_device;
331da177e4SLinus Torvalds struct device;
34167cbce2SJohan Hovold struct gpio_desc;
351da177e4SLinus Torvalds 
36e60a7233SJiri Slaby /**
37e60a7233SJiri Slaby  * struct uart_ops -- interface between serial_core and the driver
38e60a7233SJiri Slaby  *
39e759d7c5SKevin Cernekee  * This structure describes all the operations that can be done on the
40e60a7233SJiri Slaby  * physical hardware.
41e60a7233SJiri Slaby  *
42e60a7233SJiri Slaby  * @tx_empty: ``unsigned int ()(struct uart_port *port)``
43e60a7233SJiri Slaby  *
44e60a7233SJiri Slaby  *	This function tests whether the transmitter fifo and shifter for the
45e60a7233SJiri Slaby  *	@port is empty. If it is empty, this function should return
46e60a7233SJiri Slaby  *	%TIOCSER_TEMT, otherwise return 0. If the port does not support this
47e60a7233SJiri Slaby  *	operation, then it should return %TIOCSER_TEMT.
48e60a7233SJiri Slaby  *
49e60a7233SJiri Slaby  *	Locking: none.
50e60a7233SJiri Slaby  *	Interrupts: caller dependent.
51e60a7233SJiri Slaby  *	This call must not sleep
52e60a7233SJiri Slaby  *
53e60a7233SJiri Slaby  * @set_mctrl: ``void ()(struct uart_port *port, unsigned int mctrl)``
54e60a7233SJiri Slaby  *
55e60a7233SJiri Slaby  *	This function sets the modem control lines for @port to the state
56e60a7233SJiri Slaby  *	described by @mctrl. The relevant bits of @mctrl are:
57e60a7233SJiri Slaby  *
58e60a7233SJiri Slaby  *		- %TIOCM_RTS	RTS signal.
59e60a7233SJiri Slaby  *		- %TIOCM_DTR	DTR signal.
60e60a7233SJiri Slaby  *		- %TIOCM_OUT1	OUT1 signal.
61e60a7233SJiri Slaby  *		- %TIOCM_OUT2	OUT2 signal.
62e60a7233SJiri Slaby  *		- %TIOCM_LOOP	Set the port into loopback mode.
63e60a7233SJiri Slaby  *
64e60a7233SJiri Slaby  *	If the appropriate bit is set, the signal should be driven
65e60a7233SJiri Slaby  *	active.  If the bit is clear, the signal should be driven
66e60a7233SJiri Slaby  *	inactive.
67e60a7233SJiri Slaby  *
68e60a7233SJiri Slaby  *	Locking: @port->lock taken.
69e60a7233SJiri Slaby  *	Interrupts: locally disabled.
70e60a7233SJiri Slaby  *	This call must not sleep
71e60a7233SJiri Slaby  *
72e60a7233SJiri Slaby  * @get_mctrl: ``unsigned int ()(struct uart_port *port)``
73e60a7233SJiri Slaby  *
74e60a7233SJiri Slaby  *	Returns the current state of modem control inputs of @port. The state
75e60a7233SJiri Slaby  *	of the outputs should not be returned, since the core keeps track of
76e60a7233SJiri Slaby  *	their state. The state information should include:
77e60a7233SJiri Slaby  *
78e60a7233SJiri Slaby  *		- %TIOCM_CAR	state of DCD signal
79e60a7233SJiri Slaby  *		- %TIOCM_CTS	state of CTS signal
80e60a7233SJiri Slaby  *		- %TIOCM_DSR	state of DSR signal
81e60a7233SJiri Slaby  *		- %TIOCM_RI	state of RI signal
82e60a7233SJiri Slaby  *
83e60a7233SJiri Slaby  *	The bit is set if the signal is currently driven active.  If
84e60a7233SJiri Slaby  *	the port does not support CTS, DCD or DSR, the driver should
85e60a7233SJiri Slaby  *	indicate that the signal is permanently active. If RI is
86e60a7233SJiri Slaby  *	not available, the signal should not be indicated as active.
87e60a7233SJiri Slaby  *
88e60a7233SJiri Slaby  *	Locking: @port->lock taken.
89e60a7233SJiri Slaby  *	Interrupts: locally disabled.
90e60a7233SJiri Slaby  *	This call must not sleep
91e60a7233SJiri Slaby  *
92e60a7233SJiri Slaby  * @stop_tx: ``void ()(struct uart_port *port)``
93e60a7233SJiri Slaby  *
94e60a7233SJiri Slaby  *	Stop transmitting characters. This might be due to the CTS line
95e60a7233SJiri Slaby  *	becoming inactive or the tty layer indicating we want to stop
96e60a7233SJiri Slaby  *	transmission due to an %XOFF character.
97e60a7233SJiri Slaby  *
98e60a7233SJiri Slaby  *	The driver should stop transmitting characters as soon as possible.
99e60a7233SJiri Slaby  *
100e60a7233SJiri Slaby  *	Locking: @port->lock taken.
101e60a7233SJiri Slaby  *	Interrupts: locally disabled.
102e60a7233SJiri Slaby  *	This call must not sleep
103e60a7233SJiri Slaby  *
104e60a7233SJiri Slaby  * @start_tx: ``void ()(struct uart_port *port)``
105e60a7233SJiri Slaby  *
106e60a7233SJiri Slaby  *	Start transmitting characters.
107e60a7233SJiri Slaby  *
108e60a7233SJiri Slaby  *	Locking: @port->lock taken.
109e60a7233SJiri Slaby  *	Interrupts: locally disabled.
110e60a7233SJiri Slaby  *	This call must not sleep
111e60a7233SJiri Slaby  *
112e60a7233SJiri Slaby  * @throttle: ``void ()(struct uart_port *port)``
113e60a7233SJiri Slaby  *
114e60a7233SJiri Slaby  *	Notify the serial driver that input buffers for the line discipline are
115e60a7233SJiri Slaby  *	close to full, and it should somehow signal that no more characters
116e60a7233SJiri Slaby  *	should be sent to the serial port.
117e60a7233SJiri Slaby  *	This will be called only if hardware assisted flow control is enabled.
118e60a7233SJiri Slaby  *
119e60a7233SJiri Slaby  *	Locking: serialized with @unthrottle() and termios modification by the
120e60a7233SJiri Slaby  *	tty layer.
121e60a7233SJiri Slaby  *
122e60a7233SJiri Slaby  * @unthrottle: ``void ()(struct uart_port *port)``
123e60a7233SJiri Slaby  *
124e60a7233SJiri Slaby  *	Notify the serial driver that characters can now be sent to the serial
125e60a7233SJiri Slaby  *	port without fear of overrunning the input buffers of the line
126e60a7233SJiri Slaby  *	disciplines.
127e60a7233SJiri Slaby  *
128e60a7233SJiri Slaby  *	This will be called only if hardware assisted flow control is enabled.
129e60a7233SJiri Slaby  *
130e60a7233SJiri Slaby  *	Locking: serialized with @throttle() and termios modification by the
131e60a7233SJiri Slaby  *	tty layer.
132e60a7233SJiri Slaby  *
133e60a7233SJiri Slaby  * @send_xchar: ``void ()(struct uart_port *port, char ch)``
134e60a7233SJiri Slaby  *
135e60a7233SJiri Slaby  *	Transmit a high priority character, even if the port is stopped. This
136e60a7233SJiri Slaby  *	is used to implement XON/XOFF flow control and tcflow(). If the serial
137e60a7233SJiri Slaby  *	driver does not implement this function, the tty core will append the
138e60a7233SJiri Slaby  *	character to the circular buffer and then call start_tx() / stop_tx()
139e60a7233SJiri Slaby  *	to flush the data out.
140e60a7233SJiri Slaby  *
141e60a7233SJiri Slaby  *	Do not transmit if @ch == '\0' (%__DISABLED_CHAR).
142e60a7233SJiri Slaby  *
143e60a7233SJiri Slaby  *	Locking: none.
144e60a7233SJiri Slaby  *	Interrupts: caller dependent.
145e60a7233SJiri Slaby  *
146b5a5b9d5SMauro Carvalho Chehab  * @start_rx: ``void ()(struct uart_port *port)``
147b5a5b9d5SMauro Carvalho Chehab  *
148b5a5b9d5SMauro Carvalho Chehab  *	Start receiving characters.
149b5a5b9d5SMauro Carvalho Chehab  *
150b5a5b9d5SMauro Carvalho Chehab  *	Locking: @port->lock taken.
151b5a5b9d5SMauro Carvalho Chehab  *	Interrupts: locally disabled.
152b5a5b9d5SMauro Carvalho Chehab  *	This call must not sleep
153b5a5b9d5SMauro Carvalho Chehab  *
154e60a7233SJiri Slaby  * @stop_rx: ``void ()(struct uart_port *port)``
155e60a7233SJiri Slaby  *
156e60a7233SJiri Slaby  *	Stop receiving characters; the @port is in the process of being closed.
157e60a7233SJiri Slaby  *
158e60a7233SJiri Slaby  *	Locking: @port->lock taken.
159e60a7233SJiri Slaby  *	Interrupts: locally disabled.
160e60a7233SJiri Slaby  *	This call must not sleep
161e60a7233SJiri Slaby  *
162e60a7233SJiri Slaby  * @enable_ms: ``void ()(struct uart_port *port)``
163e60a7233SJiri Slaby  *
164e60a7233SJiri Slaby  *	Enable the modem status interrupts.
165e60a7233SJiri Slaby  *
166e60a7233SJiri Slaby  *	This method may be called multiple times. Modem status interrupts
167e60a7233SJiri Slaby  *	should be disabled when the @shutdown() method is called.
168e60a7233SJiri Slaby  *
169e60a7233SJiri Slaby  *	Locking: @port->lock taken.
170e60a7233SJiri Slaby  *	Interrupts: locally disabled.
171e60a7233SJiri Slaby  *	This call must not sleep
172e60a7233SJiri Slaby  *
173e60a7233SJiri Slaby  * @break_ctl: ``void ()(struct uart_port *port, int ctl)``
174e60a7233SJiri Slaby  *
175e60a7233SJiri Slaby  *	Control the transmission of a break signal. If @ctl is nonzero, the
176e60a7233SJiri Slaby  *	break signal should be transmitted. The signal should be terminated
177e60a7233SJiri Slaby  *	when another call is made with a zero @ctl.
178e60a7233SJiri Slaby  *
179e60a7233SJiri Slaby  *	Locking: caller holds tty_port->mutex
180e60a7233SJiri Slaby  *
181e60a7233SJiri Slaby  * @startup: ``int ()(struct uart_port *port)``
182e60a7233SJiri Slaby  *
183e60a7233SJiri Slaby  *	Grab any interrupt resources and initialise any low level driver state.
184e60a7233SJiri Slaby  *	Enable the port for reception. It should not activate RTS nor DTR;
185e60a7233SJiri Slaby  *	this will be done via a separate call to @set_mctrl().
186e60a7233SJiri Slaby  *
187e60a7233SJiri Slaby  *	This method will only be called when the port is initially opened.
188e60a7233SJiri Slaby  *
189e60a7233SJiri Slaby  *	Locking: port_sem taken.
190e60a7233SJiri Slaby  *	Interrupts: globally disabled.
191e60a7233SJiri Slaby  *
192e60a7233SJiri Slaby  * @shutdown: ``void ()(struct uart_port *port)``
193e60a7233SJiri Slaby  *
194e60a7233SJiri Slaby  *	Disable the @port, disable any break condition that may be in effect,
195e60a7233SJiri Slaby  *	and free any interrupt resources. It should not disable RTS nor DTR;
196e60a7233SJiri Slaby  *	this will have already been done via a separate call to @set_mctrl().
197e60a7233SJiri Slaby  *
198e60a7233SJiri Slaby  *	Drivers must not access @port->state once this call has completed.
199e60a7233SJiri Slaby  *
200e60a7233SJiri Slaby  *	This method will only be called when there are no more users of this
201e60a7233SJiri Slaby  *	@port.
202e60a7233SJiri Slaby  *
203e60a7233SJiri Slaby  *	Locking: port_sem taken.
204e60a7233SJiri Slaby  *	Interrupts: caller dependent.
205e60a7233SJiri Slaby  *
206e60a7233SJiri Slaby  * @flush_buffer: ``void ()(struct uart_port *port)``
207e60a7233SJiri Slaby  *
208e60a7233SJiri Slaby  *	Flush any write buffers, reset any DMA state and stop any ongoing DMA
209e60a7233SJiri Slaby  *	transfers.
210e60a7233SJiri Slaby  *
211e60a7233SJiri Slaby  *	This will be called whenever the @port->state->xmit circular buffer is
212e60a7233SJiri Slaby  *	cleared.
213e60a7233SJiri Slaby  *
214e60a7233SJiri Slaby  *	Locking: @port->lock taken.
215e60a7233SJiri Slaby  *	Interrupts: locally disabled.
216e60a7233SJiri Slaby  *	This call must not sleep
217e60a7233SJiri Slaby  *
218e60a7233SJiri Slaby  * @set_termios: ``void ()(struct uart_port *port, struct ktermios *new,
219e60a7233SJiri Slaby  *			struct ktermios *old)``
220e60a7233SJiri Slaby  *
221e60a7233SJiri Slaby  *	Change the @port parameters, including word length, parity, stop bits.
222e60a7233SJiri Slaby  *	Update @port->read_status_mask and @port->ignore_status_mask to
223e60a7233SJiri Slaby  *	indicate the types of events we are interested in receiving. Relevant
224e60a7233SJiri Slaby  *	ktermios::c_cflag bits are:
225e60a7233SJiri Slaby  *
226e60a7233SJiri Slaby  *	- %CSIZE - word size
227e60a7233SJiri Slaby  *	- %CSTOPB - 2 stop bits
228e60a7233SJiri Slaby  *	- %PARENB - parity enable
229e60a7233SJiri Slaby  *	- %PARODD - odd parity (when %PARENB is in force)
230e60a7233SJiri Slaby  *	- %ADDRB - address bit (changed through uart_port::rs485_config()).
231e60a7233SJiri Slaby  *	- %CREAD - enable reception of characters (if not set, still receive
232e60a7233SJiri Slaby  *	  characters from the port, but throw them away).
233e60a7233SJiri Slaby  *	- %CRTSCTS - if set, enable CTS status change reporting.
234e60a7233SJiri Slaby  *	- %CLOCAL - if not set, enable modem status change reporting.
235e60a7233SJiri Slaby  *
236e60a7233SJiri Slaby  *	Relevant ktermios::c_iflag bits are:
237e60a7233SJiri Slaby  *
238e60a7233SJiri Slaby  *	- %INPCK - enable frame and parity error events to be passed to the TTY
239e60a7233SJiri Slaby  *	  layer.
240e60a7233SJiri Slaby  *	- %BRKINT / %PARMRK - both of these enable break events to be passed to
241e60a7233SJiri Slaby  *	  the TTY layer.
242e60a7233SJiri Slaby  *	- %IGNPAR - ignore parity and framing errors.
243e60a7233SJiri Slaby  *	- %IGNBRK - ignore break errors. If %IGNPAR is also set, ignore overrun
244e60a7233SJiri Slaby  *	  errors as well.
245e60a7233SJiri Slaby  *
246e60a7233SJiri Slaby  *	The interaction of the ktermios::c_iflag bits is as follows (parity
247e60a7233SJiri Slaby  *	error given as an example):
248e60a7233SJiri Slaby  *
249e60a7233SJiri Slaby  *	============ ======= ======= =========================================
250e60a7233SJiri Slaby  *	Parity error INPCK   IGNPAR
251e60a7233SJiri Slaby  *	============ ======= ======= =========================================
252e60a7233SJiri Slaby  *	n/a	     0	     n/a     character received, marked as %TTY_NORMAL
253e60a7233SJiri Slaby  *	None	     1	     n/a     character received, marked as %TTY_NORMAL
254e60a7233SJiri Slaby  *	Yes	     1	     0	     character received, marked as %TTY_PARITY
255e60a7233SJiri Slaby  *	Yes	     1	     1	     character discarded
256e60a7233SJiri Slaby  *	============ ======= ======= =========================================
257e60a7233SJiri Slaby  *
258e60a7233SJiri Slaby  *	Other flags may be used (eg, xon/xoff characters) if your hardware
259e60a7233SJiri Slaby  *	supports hardware "soft" flow control.
260e60a7233SJiri Slaby  *
261e60a7233SJiri Slaby  *	Locking: caller holds tty_port->mutex
262e60a7233SJiri Slaby  *	Interrupts: caller dependent.
263e60a7233SJiri Slaby  *	This call must not sleep
264e60a7233SJiri Slaby  *
265e60a7233SJiri Slaby  * @set_ldisc: ``void ()(struct uart_port *port, struct ktermios *termios)``
266e60a7233SJiri Slaby  *
267e60a7233SJiri Slaby  *	Notifier for discipline change. See
268e60a7233SJiri Slaby  *	Documentation/driver-api/tty/tty_ldisc.rst.
269e60a7233SJiri Slaby  *
270e60a7233SJiri Slaby  *	Locking: caller holds tty_port->mutex
271e60a7233SJiri Slaby  *
272e60a7233SJiri Slaby  * @pm: ``void ()(struct uart_port *port, unsigned int state,
273e60a7233SJiri Slaby  *		 unsigned int oldstate)``
274e60a7233SJiri Slaby  *
275e60a7233SJiri Slaby  *	Perform any power management related activities on the specified @port.
276e60a7233SJiri Slaby  *	@state indicates the new state (defined by enum uart_pm_state),
277e60a7233SJiri Slaby  *	@oldstate indicates the previous state.
278e60a7233SJiri Slaby  *
279e60a7233SJiri Slaby  *	This function should not be used to grab any resources.
280e60a7233SJiri Slaby  *
281e60a7233SJiri Slaby  *	This will be called when the @port is initially opened and finally
282e60a7233SJiri Slaby  *	closed, except when the @port is also the system console. This will
283e60a7233SJiri Slaby  *	occur even if %CONFIG_PM is not set.
284e60a7233SJiri Slaby  *
285e60a7233SJiri Slaby  *	Locking: none.
286e60a7233SJiri Slaby  *	Interrupts: caller dependent.
287e60a7233SJiri Slaby  *
288e60a7233SJiri Slaby  * @type: ``const char *()(struct uart_port *port)``
289e60a7233SJiri Slaby  *
290e60a7233SJiri Slaby  *	Return a pointer to a string constant describing the specified @port,
291e60a7233SJiri Slaby  *	or return %NULL, in which case the string 'unknown' is substituted.
292e60a7233SJiri Slaby  *
293e60a7233SJiri Slaby  *	Locking: none.
294e60a7233SJiri Slaby  *	Interrupts: caller dependent.
295e60a7233SJiri Slaby  *
296e60a7233SJiri Slaby  * @release_port: ``void ()(struct uart_port *port)``
297e60a7233SJiri Slaby  *
298e60a7233SJiri Slaby  *	Release any memory and IO region resources currently in use by the
299e60a7233SJiri Slaby  *	@port.
300e60a7233SJiri Slaby  *
301e60a7233SJiri Slaby  *	Locking: none.
302e60a7233SJiri Slaby  *	Interrupts: caller dependent.
303e60a7233SJiri Slaby  *
304e60a7233SJiri Slaby  * @request_port: ``int ()(struct uart_port *port)``
305e60a7233SJiri Slaby  *
306e60a7233SJiri Slaby  *	Request any memory and IO region resources required by the port. If any
307e60a7233SJiri Slaby  *	fail, no resources should be registered when this function returns, and
308e60a7233SJiri Slaby  *	it should return -%EBUSY on failure.
309e60a7233SJiri Slaby  *
310e60a7233SJiri Slaby  *	Locking: none.
311e60a7233SJiri Slaby  *	Interrupts: caller dependent.
312e60a7233SJiri Slaby  *
313e60a7233SJiri Slaby  * @config_port: ``void ()(struct uart_port *port, int type)``
314e60a7233SJiri Slaby  *
315e60a7233SJiri Slaby  *	Perform any autoconfiguration steps required for the @port. @type
316e60a7233SJiri Slaby  *	contains a bit mask of the required configuration. %UART_CONFIG_TYPE
317e60a7233SJiri Slaby  *	indicates that the port requires detection and identification.
318e60a7233SJiri Slaby  *	@port->type should be set to the type found, or %PORT_UNKNOWN if no
319e60a7233SJiri Slaby  *	port was detected.
320e60a7233SJiri Slaby  *
321e60a7233SJiri Slaby  *	%UART_CONFIG_IRQ indicates autoconfiguration of the interrupt signal,
322e60a7233SJiri Slaby  *	which should be probed using standard kernel autoprobing techniques.
323e60a7233SJiri Slaby  *	This is not necessary on platforms where ports have interrupts
324e60a7233SJiri Slaby  *	internally hard wired (eg, system on a chip implementations).
325e60a7233SJiri Slaby  *
326e60a7233SJiri Slaby  *	Locking: none.
327e60a7233SJiri Slaby  *	Interrupts: caller dependent.
328e60a7233SJiri Slaby  *
329e60a7233SJiri Slaby  * @verify_port: ``int ()(struct uart_port *port,
330e60a7233SJiri Slaby  *			struct serial_struct *serinfo)``
331e60a7233SJiri Slaby  *
332e60a7233SJiri Slaby  *	Verify the new serial port information contained within @serinfo is
333e60a7233SJiri Slaby  *	suitable for this port type.
334e60a7233SJiri Slaby  *
335e60a7233SJiri Slaby  *	Locking: none.
336e60a7233SJiri Slaby  *	Interrupts: caller dependent.
337e60a7233SJiri Slaby  *
338e60a7233SJiri Slaby  * @ioctl: ``int ()(struct uart_port *port, unsigned int cmd,
339e60a7233SJiri Slaby  *		unsigned long arg)``
340e60a7233SJiri Slaby  *
341e60a7233SJiri Slaby  *	Perform any port specific IOCTLs. IOCTL commands must be defined using
342e60a7233SJiri Slaby  *	the standard numbering system found in <asm/ioctl.h>.
343e60a7233SJiri Slaby  *
344e60a7233SJiri Slaby  *	Locking: none.
345e60a7233SJiri Slaby  *	Interrupts: caller dependent.
346e60a7233SJiri Slaby  *
347e60a7233SJiri Slaby  * @poll_init: ``int ()(struct uart_port *port)``
348e60a7233SJiri Slaby  *
349e60a7233SJiri Slaby  *	Called by kgdb to perform the minimal hardware initialization needed to
350e60a7233SJiri Slaby  *	support @poll_put_char() and @poll_get_char(). Unlike @startup(), this
351e60a7233SJiri Slaby  *	should not request interrupts.
352e60a7233SJiri Slaby  *
353e60a7233SJiri Slaby  *	Locking: %tty_mutex and tty_port->mutex taken.
354e60a7233SJiri Slaby  *	Interrupts: n/a.
355e60a7233SJiri Slaby  *
356e60a7233SJiri Slaby  * @poll_put_char: ``void ()(struct uart_port *port, unsigned char ch)``
357e60a7233SJiri Slaby  *
358e60a7233SJiri Slaby  *	Called by kgdb to write a single character @ch directly to the serial
359e60a7233SJiri Slaby  *	@port. It can and should block until there is space in the TX FIFO.
360e60a7233SJiri Slaby  *
361e60a7233SJiri Slaby  *	Locking: none.
362e60a7233SJiri Slaby  *	Interrupts: caller dependent.
363e60a7233SJiri Slaby  *	This call must not sleep
364e60a7233SJiri Slaby  *
365e60a7233SJiri Slaby  * @poll_get_char: ``int ()(struct uart_port *port)``
366e60a7233SJiri Slaby  *
367e60a7233SJiri Slaby  *	Called by kgdb to read a single character directly from the serial
368e60a7233SJiri Slaby  *	port. If data is available, it should be returned; otherwise the
369e60a7233SJiri Slaby  *	function should return %NO_POLL_CHAR immediately.
370e60a7233SJiri Slaby  *
371e60a7233SJiri Slaby  *	Locking: none.
372e60a7233SJiri Slaby  *	Interrupts: caller dependent.
373e60a7233SJiri Slaby  *	This call must not sleep
3741da177e4SLinus Torvalds  */
3751da177e4SLinus Torvalds struct uart_ops {
3761da177e4SLinus Torvalds 	unsigned int	(*tx_empty)(struct uart_port *);
3771da177e4SLinus Torvalds 	void		(*set_mctrl)(struct uart_port *, unsigned int mctrl);
3781da177e4SLinus Torvalds 	unsigned int	(*get_mctrl)(struct uart_port *);
379b129a8ccSRussell King 	void		(*stop_tx)(struct uart_port *);
380b129a8ccSRussell King 	void		(*start_tx)(struct uart_port *);
3819aba8d5bSRussell King 	void		(*throttle)(struct uart_port *);
3829aba8d5bSRussell King 	void		(*unthrottle)(struct uart_port *);
3831da177e4SLinus Torvalds 	void		(*send_xchar)(struct uart_port *, char ch);
3841da177e4SLinus Torvalds 	void		(*stop_rx)(struct uart_port *);
385cfab87c2SVijaya Krishna Nivarthi 	void		(*start_rx)(struct uart_port *);
3861da177e4SLinus Torvalds 	void		(*enable_ms)(struct uart_port *);
3871da177e4SLinus Torvalds 	void		(*break_ctl)(struct uart_port *, int ctl);
3881da177e4SLinus Torvalds 	int		(*startup)(struct uart_port *);
3891da177e4SLinus Torvalds 	void		(*shutdown)(struct uart_port *);
3906bb0e3a5SHaavard Skinnemoen 	void		(*flush_buffer)(struct uart_port *);
391606d099cSAlan Cox 	void		(*set_termios)(struct uart_port *, struct ktermios *new,
392bec5b814SIlpo Järvinen 				       const struct ktermios *old);
393732a84a0SPeter Hurley 	void		(*set_ldisc)(struct uart_port *, struct ktermios *);
3941da177e4SLinus Torvalds 	void		(*pm)(struct uart_port *, unsigned int state,
3951da177e4SLinus Torvalds 			      unsigned int oldstate);
3961da177e4SLinus Torvalds 	const char	*(*type)(struct uart_port *);
3971da177e4SLinus Torvalds 	void		(*release_port)(struct uart_port *);
3981da177e4SLinus Torvalds 	int		(*request_port)(struct uart_port *);
3991da177e4SLinus Torvalds 	void		(*config_port)(struct uart_port *, int);
4001da177e4SLinus Torvalds 	int		(*verify_port)(struct uart_port *, struct serial_struct *);
4011da177e4SLinus Torvalds 	int		(*ioctl)(struct uart_port *, unsigned int, unsigned long);
402f2d937f3SJason Wessel #ifdef CONFIG_CONSOLE_POLL
403c7f3e708SAnton Vorontsov 	int		(*poll_init)(struct uart_port *);
404f2d937f3SJason Wessel 	void		(*poll_put_char)(struct uart_port *, unsigned char);
405f2d937f3SJason Wessel 	int		(*poll_get_char)(struct uart_port *);
406f2d937f3SJason Wessel #endif
4071da177e4SLinus Torvalds };
4081da177e4SLinus Torvalds 
409f5316b4aSJason Wessel #define NO_POLL_CHAR		0x00ff0000
4101da177e4SLinus Torvalds #define UART_CONFIG_TYPE	(1 << 0)
4111da177e4SLinus Torvalds #define UART_CONFIG_IRQ		(1 << 1)
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds struct uart_icount {
4141da177e4SLinus Torvalds 	__u32	cts;
4151da177e4SLinus Torvalds 	__u32	dsr;
4161da177e4SLinus Torvalds 	__u32	rng;
4171da177e4SLinus Torvalds 	__u32	dcd;
4181da177e4SLinus Torvalds 	__u32	rx;
4191da177e4SLinus Torvalds 	__u32	tx;
4201da177e4SLinus Torvalds 	__u32	frame;
4211da177e4SLinus Torvalds 	__u32	overrun;
4221da177e4SLinus Torvalds 	__u32	parity;
4231da177e4SLinus Torvalds 	__u32	brk;
4241da177e4SLinus Torvalds 	__u32	buf_overrun;
4251da177e4SLinus Torvalds };
4261da177e4SLinus Torvalds 
4279906890cSMaciej W. Rozycki typedef u64 __bitwise upf_t;
4289efeccacSMichael S. Tsirkin typedef unsigned int __bitwise upstat_t;
4290077d45eSRussell King 
4301da177e4SLinus Torvalds struct uart_port {
4311da177e4SLinus Torvalds 	spinlock_t		lock;			/* port lock */
4320c8946d9SDavid Miller 	unsigned long		iobase;			/* in/out[bwl] */
4331da177e4SLinus Torvalds 	unsigned char __iomem	*membase;		/* read/write[bwl] */
4347d6a07d1SDavid Daney 	unsigned int		(*serial_in)(struct uart_port *, int);
4357d6a07d1SDavid Daney 	void			(*serial_out)(struct uart_port *, int, int);
436235dae5dSPhilippe Langlais 	void			(*set_termios)(struct uart_port *,
437235dae5dSPhilippe Langlais 				               struct ktermios *new,
438bec5b814SIlpo Järvinen 				               const struct ktermios *old);
439db405a8fSEd Blake 	void			(*set_ldisc)(struct uart_port *,
440db405a8fSEd Blake 					     struct ktermios *);
441144ef5c2SWan Ahmad Zainie 	unsigned int		(*get_mctrl)(struct uart_port *);
4424bf4ea9dSPeter Hurley 	void			(*set_mctrl)(struct uart_port *, unsigned int);
4430238d2b4SJisheng Zhang 	unsigned int		(*get_divisor)(struct uart_port *,
4440238d2b4SJisheng Zhang 					       unsigned int baud,
4450238d2b4SJisheng Zhang 					       unsigned int *frac);
4460238d2b4SJisheng Zhang 	void			(*set_divisor)(struct uart_port *,
4470238d2b4SJisheng Zhang 					       unsigned int baud,
4480238d2b4SJisheng Zhang 					       unsigned int quot,
4490238d2b4SJisheng Zhang 					       unsigned int quot_frac);
450b99b121bSSebastian Andrzej Siewior 	int			(*startup)(struct uart_port *port);
451b99b121bSSebastian Andrzej Siewior 	void			(*shutdown)(struct uart_port *port);
452234abab1SSebastian Andrzej Siewior 	void			(*throttle)(struct uart_port *port);
453234abab1SSebastian Andrzej Siewior 	void			(*unthrottle)(struct uart_port *port);
454a74036f5SJamie Iles 	int			(*handle_irq)(struct uart_port *);
455c161afe9SManuel Lauss 	void			(*pm)(struct uart_port *, unsigned int state,
456c161afe9SManuel Lauss 				      unsigned int old);
457bf03f65bSDan Williams 	void			(*handle_break)(struct uart_port *);
458a5f276f1SRicardo Ribalda Delgado 	int			(*rs485_config)(struct uart_port *,
459ae50bb27SIlpo Järvinen 						struct ktermios *termios,
460a5f276f1SRicardo Ribalda Delgado 						struct serial_rs485 *rs485);
461ad8c0eaaSNicolas Ferre 	int			(*iso7816_config)(struct uart_port *,
462ad8c0eaaSNicolas Ferre 						  struct serial_iso7816 *iso7816);
46383c35180STony Lindgren 	unsigned int		ctrl_id;		/* optional serial core controller id */
464d962de6aSTony Lindgren 	unsigned int		port_id;		/* optional serial core port id */
4651da177e4SLinus Torvalds 	unsigned int		irq;			/* irq number */
4661c2f0493SVikram Pandita 	unsigned long		irqflags;		/* irq flags  */
4671da177e4SLinus Torvalds 	unsigned int		uartclk;		/* base uart clock */
468947deee8SRussell King 	unsigned int		fifosize;		/* tx fifo size */
4691da177e4SLinus Torvalds 	unsigned char		x_char;			/* xon/xoff char */
4701da177e4SLinus Torvalds 	unsigned char		regshift;		/* reg offset shift */
47135c822a3SAndy Shevchenko 
4721da177e4SLinus Torvalds 	unsigned char		iotype;			/* io access style */
4731da177e4SLinus Torvalds 
47479d713baSAndy Shevchenko #define UPIO_UNKNOWN		((unsigned char)~0U)	/* UCHAR_MAX */
475647f162bSPeter Hurley #define UPIO_PORT		(SERIAL_IO_PORT)	/* 8b I/O port access */
476647f162bSPeter Hurley #define UPIO_HUB6		(SERIAL_IO_HUB6)	/* Hub6 ISA card */
477858965d9SPeter Hurley #define UPIO_MEM		(SERIAL_IO_MEM)		/* driver-specific */
478647f162bSPeter Hurley #define UPIO_MEM32		(SERIAL_IO_MEM32)	/* 32b little endian */
479647f162bSPeter Hurley #define UPIO_AU			(SERIAL_IO_AU)		/* Au1x00 and RT288x type IO */
480647f162bSPeter Hurley #define UPIO_TSI		(SERIAL_IO_TSI)		/* Tsi108/109 type IO */
481647f162bSPeter Hurley #define UPIO_MEM32BE		(SERIAL_IO_MEM32BE)	/* 32b big endian */
482bd94c407SMasahiro Yamada #define UPIO_MEM16		(SERIAL_IO_MEM16)	/* 16b little endian */
4831da177e4SLinus Torvalds 
48435c822a3SAndy Shevchenko 	unsigned char		quirks;			/* internal quirks */
48535c822a3SAndy Shevchenko 
48635c822a3SAndy Shevchenko 	/* internal quirks must be updated while holding port mutex */
487c7ac15ceSAndy Shevchenko #define UPQ_NO_TXEN_TEST	BIT(0)
488c7ac15ceSAndy Shevchenko 
4891da177e4SLinus Torvalds 	unsigned int		read_status_mask;	/* driver specific */
4901da177e4SLinus Torvalds 	unsigned int		ignore_status_mask;	/* driver specific */
491ebd2c8f6SAlan Cox 	struct uart_state	*state;			/* pointer to parent state */
4921da177e4SLinus Torvalds 	struct uart_icount	icount;			/* statistics */
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	struct console		*cons;			/* struct console, if any */
4958a949b07SPeter Hurley 	/* flags must be updated while holding port mutex */
4960077d45eSRussell King 	upf_t			flags;
4971da177e4SLinus Torvalds 
498904326ecSPeter Hurley 	/*
499904326ecSPeter Hurley 	 * These flags must be equivalent to the flags defined in
500904326ecSPeter Hurley 	 * include/uapi/linux/tty_flags.h which are the userspace definitions
501904326ecSPeter Hurley 	 * assigned from the serial_struct flags in uart_set_info()
502904326ecSPeter Hurley 	 * [for bit definitions in the UPF_CHANGE_MASK]
503904326ecSPeter Hurley 	 *
504916acbf6SAndy Shevchenko 	 * Bits [0..ASYNCB_LAST_USER] are userspace defined/visible/changeable
505904326ecSPeter Hurley 	 * The remaining bits are serial-core specific and not modifiable by
506904326ecSPeter Hurley 	 * userspace.
507904326ecSPeter Hurley 	 */
508*7c7e6c89SNiklas Schnelle #ifdef CONFIG_HAS_IOPORT
509904326ecSPeter Hurley #define UPF_FOURPORT		((__force upf_t) ASYNC_FOURPORT       /* 1  */ )
510*7c7e6c89SNiklas Schnelle #else
511*7c7e6c89SNiklas Schnelle #define UPF_FOURPORT		0
512*7c7e6c89SNiklas Schnelle #endif
513904326ecSPeter Hurley #define UPF_SAK			((__force upf_t) ASYNC_SAK            /* 2  */ )
514904326ecSPeter Hurley #define UPF_SPD_HI		((__force upf_t) ASYNC_SPD_HI         /* 4  */ )
515904326ecSPeter Hurley #define UPF_SPD_VHI		((__force upf_t) ASYNC_SPD_VHI        /* 5  */ )
516904326ecSPeter Hurley #define UPF_SPD_CUST		((__force upf_t) ASYNC_SPD_CUST   /* 0x0030 */ )
517904326ecSPeter Hurley #define UPF_SPD_WARP		((__force upf_t) ASYNC_SPD_WARP   /* 0x1010 */ )
518904326ecSPeter Hurley #define UPF_SPD_MASK		((__force upf_t) ASYNC_SPD_MASK   /* 0x1030 */ )
519904326ecSPeter Hurley #define UPF_SKIP_TEST		((__force upf_t) ASYNC_SKIP_TEST      /* 6  */ )
520904326ecSPeter Hurley #define UPF_AUTO_IRQ		((__force upf_t) ASYNC_AUTO_IRQ       /* 7  */ )
521904326ecSPeter Hurley #define UPF_HARDPPS_CD		((__force upf_t) ASYNC_HARDPPS_CD     /* 11 */ )
522904326ecSPeter Hurley #define UPF_SPD_SHI		((__force upf_t) ASYNC_SPD_SHI        /* 12 */ )
523904326ecSPeter Hurley #define UPF_LOW_LATENCY		((__force upf_t) ASYNC_LOW_LATENCY    /* 13 */ )
524904326ecSPeter Hurley #define UPF_BUGGY_UART		((__force upf_t) ASYNC_BUGGY_UART     /* 14 */ )
525904326ecSPeter Hurley #define UPF_MAGIC_MULTIPLIER	((__force upf_t) ASYNC_MAGIC_MULTIPLIER /* 16 */ )
526904326ecSPeter Hurley 
52746a8973cSMaciej W. Rozycki #define UPF_NO_THRE_TEST	((__force upf_t) BIT_ULL(19))
528391f93f2SPeter Hurley /* Port has hardware-assisted h/w flow control */
52946a8973cSMaciej W. Rozycki #define UPF_AUTO_CTS		((__force upf_t) BIT_ULL(20))
53046a8973cSMaciej W. Rozycki #define UPF_AUTO_RTS		((__force upf_t) BIT_ULL(21))
531391f93f2SPeter Hurley #define UPF_HARD_FLOW		((__force upf_t) (UPF_AUTO_CTS | UPF_AUTO_RTS))
5322cbacafdSRussell King /* Port has hardware-assisted s/w flow control */
53346a8973cSMaciej W. Rozycki #define UPF_SOFT_FLOW		((__force upf_t) BIT_ULL(22))
53446a8973cSMaciej W. Rozycki #define UPF_CONS_FLOW		((__force upf_t) BIT_ULL(23))
53546a8973cSMaciej W. Rozycki #define UPF_SHARE_IRQ		((__force upf_t) BIT_ULL(24))
53646a8973cSMaciej W. Rozycki #define UPF_EXAR_EFR		((__force upf_t) BIT_ULL(25))
53746a8973cSMaciej W. Rozycki #define UPF_BUG_THRE		((__force upf_t) BIT_ULL(26))
5388e23fcc8SDavid Daney /* The exact UART type is known and should not be probed.  */
53946a8973cSMaciej W. Rozycki #define UPF_FIXED_TYPE		((__force upf_t) BIT_ULL(27))
54046a8973cSMaciej W. Rozycki #define UPF_BOOT_AUTOCONF	((__force upf_t) BIT_ULL(28))
54146a8973cSMaciej W. Rozycki #define UPF_FIXED_PORT		((__force upf_t) BIT_ULL(29))
54246a8973cSMaciej W. Rozycki #define UPF_DEAD		((__force upf_t) BIT_ULL(30))
54346a8973cSMaciej W. Rozycki #define UPF_IOREMAP		((__force upf_t) BIT_ULL(31))
54446a8973cSMaciej W. Rozycki #define UPF_FULL_PROBE		((__force upf_t) BIT_ULL(32))
5451da177e4SLinus Torvalds 
546904326ecSPeter Hurley #define __UPF_CHANGE_MASK	0x17fff
547904326ecSPeter Hurley #define UPF_CHANGE_MASK		((__force upf_t) __UPF_CHANGE_MASK)
5480077d45eSRussell King #define UPF_USR_MASK		((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
5491da177e4SLinus Torvalds 
550904326ecSPeter Hurley #if __UPF_CHANGE_MASK > ASYNC_FLAGS
551904326ecSPeter Hurley #error Change mask not equivalent to userspace-visible bit defines
552904326ecSPeter Hurley #endif
553904326ecSPeter Hurley 
554391f93f2SPeter Hurley 	/*
555391f93f2SPeter Hurley 	 * Must hold termios_rwsem, port mutex and port lock to change;
556391f93f2SPeter Hurley 	 * can hold any one lock to read.
557391f93f2SPeter Hurley 	 */
558299245a1SPeter Hurley 	upstat_t		status;
559299245a1SPeter Hurley 
560299245a1SPeter Hurley #define UPSTAT_CTS_ENABLE	((__force upstat_t) (1 << 0))
561299245a1SPeter Hurley #define UPSTAT_DCD_ENABLE	((__force upstat_t) (1 << 1))
562391f93f2SPeter Hurley #define UPSTAT_AUTORTS		((__force upstat_t) (1 << 2))
563391f93f2SPeter Hurley #define UPSTAT_AUTOCTS		((__force upstat_t) (1 << 3))
564391f93f2SPeter Hurley #define UPSTAT_AUTOXOFF		((__force upstat_t) (1 << 4))
565c5f78b1fSJeremy Kerr #define UPSTAT_SYNC_FIFO	((__force upstat_t) (1 << 5))
566299245a1SPeter Hurley 
567b5def43aSIlpo Järvinen 	bool			hw_stopped;		/* sw-assisted CTS flow state */
5681da177e4SLinus Torvalds 	unsigned int		mctrl;			/* current modem ctrl settings */
56931f6bd7fSIlpo Järvinen 	unsigned int		frame_time;		/* frame timing in ns */
5701da177e4SLinus Torvalds 	unsigned int		type;			/* port type */
571ba899dbcSRussell King 	const struct uart_ops	*ops;
5721da177e4SLinus Torvalds 	unsigned int		custom_divisor;
5731da177e4SLinus Torvalds 	unsigned int		line;			/* port index */
574959801feSPeter Hurley 	unsigned int		minor;
5754f640efbSJosh Boyer 	resource_size_t		mapbase;		/* for ioremap */
576ee97d0e3SMans Rullgard 	resource_size_t		mapsize;
57784a9582fSTony Lindgren 	struct device		*dev;			/* serial port physical parent device */
57884a9582fSTony Lindgren 	struct serial_port_device *port_dev;		/* serial core port device */
5797e5ed9f5SDmitry Safonov 
5807e5ed9f5SDmitry Safonov 	unsigned long		sysrq;			/* sysrq timeout */
58112ae2359SJiri Slaby 	u8			sysrq_ch;		/* char for sysrq */
5821997e9dfSDmitry Safonov 	unsigned char		has_sysrq;
58368af4317SDmitry Safonov 	unsigned char		sysrq_seq;		/* index in sysrq_toggle_seq */
5847e5ed9f5SDmitry Safonov 
5851da177e4SLinus Torvalds 	unsigned char		hub6;			/* this should be in the 8250 driver */
586b3b708faSGuennadi Liakhovetski 	unsigned char		suspended;
587e0830dbfSJohan Hovold 	unsigned char		console_reinit;
5882e94d5aeSAndy Shevchenko 	const char		*name;			/* port name */
589266dcff0SGreg Kroah-Hartman 	struct attribute_group	*attr_group;		/* port specific attributes */
590266dcff0SGreg Kroah-Hartman 	const struct attribute_group **tty_groups;	/* all attributes (serial core use only) */
591a5f276f1SRicardo Ribalda Delgado 	struct serial_rs485     rs485;
5920139da50SIlpo Järvinen 	struct serial_rs485	rs485_supported;	/* Supported mask for serial_rs485 */
593d58a2df3SLukas Wunner 	struct gpio_desc	*rs485_term_gpio;	/* enable RS485 bus termination */
594163f080eSChristoph Niedermaier 	struct gpio_desc	*rs485_rx_during_tx_gpio; /* Output GPIO that sets the state of RS485 RX during TX */
595ad8c0eaaSNicolas Ferre 	struct serial_iso7816   iso7816;
596beab697aSMarc St-Jean 	void			*private_data;		/* generic platform data pointer */
5971da177e4SLinus Torvalds };
5981da177e4SLinus Torvalds 
59977e73c06SJohn Ogness /*
60077e73c06SJohn Ogness  * Only for console->device_lock()/_unlock() callbacks and internal
60177e73c06SJohn Ogness  * port lock wrapper synchronization.
60277e73c06SJohn Ogness  */
__uart_port_lock_irqsave(struct uart_port * up,unsigned long * flags)60377e73c06SJohn Ogness static inline void __uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
60477e73c06SJohn Ogness {
60577e73c06SJohn Ogness 	spin_lock_irqsave(&up->lock, *flags);
60677e73c06SJohn Ogness }
60777e73c06SJohn Ogness 
60877e73c06SJohn Ogness /*
60977e73c06SJohn Ogness  * Only for console->device_lock()/_unlock() callbacks and internal
61077e73c06SJohn Ogness  * port lock wrapper synchronization.
61177e73c06SJohn Ogness  */
__uart_port_unlock_irqrestore(struct uart_port * up,unsigned long flags)61277e73c06SJohn Ogness static inline void __uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
61377e73c06SJohn Ogness {
61477e73c06SJohn Ogness 	spin_unlock_irqrestore(&up->lock, flags);
61577e73c06SJohn Ogness }
61677e73c06SJohn Ogness 
617b0af4bcbSThomas Gleixner /**
618eabd4600SJohn Ogness  * uart_port_set_cons - Safely set the @cons field for a uart
619eabd4600SJohn Ogness  * @up:		The uart port to set
620eabd4600SJohn Ogness  * @con:	The new console to set to
621eabd4600SJohn Ogness  *
622eabd4600SJohn Ogness  * This function must be used to set @up->cons. It uses the port lock to
623eabd4600SJohn Ogness  * synchronize with the port lock wrappers in order to ensure that the console
624eabd4600SJohn Ogness  * cannot change or disappear while another context is holding the port lock.
625eabd4600SJohn Ogness  */
uart_port_set_cons(struct uart_port * up,struct console * con)626eabd4600SJohn Ogness static inline void uart_port_set_cons(struct uart_port *up, struct console *con)
627eabd4600SJohn Ogness {
628eabd4600SJohn Ogness 	unsigned long flags;
629eabd4600SJohn Ogness 
630eabd4600SJohn Ogness 	__uart_port_lock_irqsave(up, &flags);
631eabd4600SJohn Ogness 	up->cons = con;
632eabd4600SJohn Ogness 	__uart_port_unlock_irqrestore(up, flags);
633eabd4600SJohn Ogness }
6344126f149SJohn Ogness 
6354126f149SJohn Ogness /* Only for internal port lock wrapper usage. */
__uart_port_using_nbcon(struct uart_port * up)6364126f149SJohn Ogness static inline bool __uart_port_using_nbcon(struct uart_port *up)
6374126f149SJohn Ogness {
6384126f149SJohn Ogness 	lockdep_assert_held_once(&up->lock);
6394126f149SJohn Ogness 
6404126f149SJohn Ogness 	if (likely(!uart_console(up)))
6414126f149SJohn Ogness 		return false;
6424126f149SJohn Ogness 
6434126f149SJohn Ogness 	/*
6444126f149SJohn Ogness 	 * @up->cons is only modified under the port lock. Therefore it is
6454126f149SJohn Ogness 	 * certain that it cannot disappear here.
6464126f149SJohn Ogness 	 *
6474126f149SJohn Ogness 	 * @up->cons->node is added/removed from the console list under the
6484126f149SJohn Ogness 	 * port lock. Therefore it is certain that the registration status
6494126f149SJohn Ogness 	 * cannot change here, thus @up->cons->flags can be read directly.
6504126f149SJohn Ogness 	 */
6514126f149SJohn Ogness 	if (hlist_unhashed_lockless(&up->cons->node) ||
6524126f149SJohn Ogness 	    !(up->cons->flags & CON_NBCON) ||
6534126f149SJohn Ogness 	    !up->cons->write_atomic) {
6544126f149SJohn Ogness 		return false;
6554126f149SJohn Ogness 	}
6564126f149SJohn Ogness 
6574126f149SJohn Ogness 	return true;
6584126f149SJohn Ogness }
6594126f149SJohn Ogness 
6604126f149SJohn Ogness /* Only for internal port lock wrapper usage. */
__uart_port_nbcon_try_acquire(struct uart_port * up)6614126f149SJohn Ogness static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up)
6624126f149SJohn Ogness {
6634126f149SJohn Ogness 	if (!__uart_port_using_nbcon(up))
6644126f149SJohn Ogness 		return true;
6654126f149SJohn Ogness 
6664126f149SJohn Ogness 	return nbcon_device_try_acquire(up->cons);
6674126f149SJohn Ogness }
6684126f149SJohn Ogness 
6694126f149SJohn Ogness /* Only for internal port lock wrapper usage. */
__uart_port_nbcon_acquire(struct uart_port * up)6704126f149SJohn Ogness static inline void __uart_port_nbcon_acquire(struct uart_port *up)
6714126f149SJohn Ogness {
6724126f149SJohn Ogness 	if (!__uart_port_using_nbcon(up))
6734126f149SJohn Ogness 		return;
6744126f149SJohn Ogness 
6754126f149SJohn Ogness 	while (!nbcon_device_try_acquire(up->cons))
6764126f149SJohn Ogness 		cpu_relax();
6774126f149SJohn Ogness }
6784126f149SJohn Ogness 
6794126f149SJohn Ogness /* Only for internal port lock wrapper usage. */
__uart_port_nbcon_release(struct uart_port * up)6804126f149SJohn Ogness static inline void __uart_port_nbcon_release(struct uart_port *up)
6814126f149SJohn Ogness {
6824126f149SJohn Ogness 	if (!__uart_port_using_nbcon(up))
6834126f149SJohn Ogness 		return;
6844126f149SJohn Ogness 
6854126f149SJohn Ogness 	nbcon_device_release(up->cons);
6864126f149SJohn Ogness }
6874126f149SJohn Ogness 
688eabd4600SJohn Ogness /**
689b0af4bcbSThomas Gleixner  * uart_port_lock - Lock the UART port
690b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
691b0af4bcbSThomas Gleixner  */
uart_port_lock(struct uart_port * up)692b0af4bcbSThomas Gleixner static inline void uart_port_lock(struct uart_port *up)
693b0af4bcbSThomas Gleixner {
694b0af4bcbSThomas Gleixner 	spin_lock(&up->lock);
6954126f149SJohn Ogness 	__uart_port_nbcon_acquire(up);
696b0af4bcbSThomas Gleixner }
697b0af4bcbSThomas Gleixner 
698b0af4bcbSThomas Gleixner /**
699b0af4bcbSThomas Gleixner  * uart_port_lock_irq - Lock the UART port and disable interrupts
700b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
701b0af4bcbSThomas Gleixner  */
uart_port_lock_irq(struct uart_port * up)702b0af4bcbSThomas Gleixner static inline void uart_port_lock_irq(struct uart_port *up)
703b0af4bcbSThomas Gleixner {
704b0af4bcbSThomas Gleixner 	spin_lock_irq(&up->lock);
7054126f149SJohn Ogness 	__uart_port_nbcon_acquire(up);
706b0af4bcbSThomas Gleixner }
707b0af4bcbSThomas Gleixner 
708b0af4bcbSThomas Gleixner /**
709b0af4bcbSThomas Gleixner  * uart_port_lock_irqsave - Lock the UART port, save and disable interrupts
710b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
711b0af4bcbSThomas Gleixner  * @flags:	Pointer to interrupt flags storage
712b0af4bcbSThomas Gleixner  */
uart_port_lock_irqsave(struct uart_port * up,unsigned long * flags)713b0af4bcbSThomas Gleixner static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned long *flags)
714b0af4bcbSThomas Gleixner {
715b0af4bcbSThomas Gleixner 	spin_lock_irqsave(&up->lock, *flags);
7164126f149SJohn Ogness 	__uart_port_nbcon_acquire(up);
717b0af4bcbSThomas Gleixner }
718b0af4bcbSThomas Gleixner 
719b0af4bcbSThomas Gleixner /**
720b0af4bcbSThomas Gleixner  * uart_port_trylock - Try to lock the UART port
721b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
722b0af4bcbSThomas Gleixner  *
723b0af4bcbSThomas Gleixner  * Returns: True if lock was acquired, false otherwise
724b0af4bcbSThomas Gleixner  */
uart_port_trylock(struct uart_port * up)725b0af4bcbSThomas Gleixner static inline bool uart_port_trylock(struct uart_port *up)
726b0af4bcbSThomas Gleixner {
7274126f149SJohn Ogness 	if (!spin_trylock(&up->lock))
7284126f149SJohn Ogness 		return false;
7294126f149SJohn Ogness 
7304126f149SJohn Ogness 	if (!__uart_port_nbcon_try_acquire(up)) {
7314126f149SJohn Ogness 		spin_unlock(&up->lock);
7324126f149SJohn Ogness 		return false;
7334126f149SJohn Ogness 	}
7344126f149SJohn Ogness 
7354126f149SJohn Ogness 	return true;
736b0af4bcbSThomas Gleixner }
737b0af4bcbSThomas Gleixner 
738b0af4bcbSThomas Gleixner /**
739b0af4bcbSThomas Gleixner  * uart_port_trylock_irqsave - Try to lock the UART port, save and disable interrupts
740b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
741b0af4bcbSThomas Gleixner  * @flags:	Pointer to interrupt flags storage
742b0af4bcbSThomas Gleixner  *
743b0af4bcbSThomas Gleixner  * Returns: True if lock was acquired, false otherwise
744b0af4bcbSThomas Gleixner  */
uart_port_trylock_irqsave(struct uart_port * up,unsigned long * flags)745b0af4bcbSThomas Gleixner static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigned long *flags)
746b0af4bcbSThomas Gleixner {
7474126f149SJohn Ogness 	if (!spin_trylock_irqsave(&up->lock, *flags))
7484126f149SJohn Ogness 		return false;
7494126f149SJohn Ogness 
7504126f149SJohn Ogness 	if (!__uart_port_nbcon_try_acquire(up)) {
7514126f149SJohn Ogness 		spin_unlock_irqrestore(&up->lock, *flags);
7524126f149SJohn Ogness 		return false;
7534126f149SJohn Ogness 	}
7544126f149SJohn Ogness 
7554126f149SJohn Ogness 	return true;
756b0af4bcbSThomas Gleixner }
757b0af4bcbSThomas Gleixner 
758b0af4bcbSThomas Gleixner /**
759b0af4bcbSThomas Gleixner  * uart_port_unlock - Unlock the UART port
760b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
761b0af4bcbSThomas Gleixner  */
uart_port_unlock(struct uart_port * up)762b0af4bcbSThomas Gleixner static inline void uart_port_unlock(struct uart_port *up)
763b0af4bcbSThomas Gleixner {
7644126f149SJohn Ogness 	__uart_port_nbcon_release(up);
765b0af4bcbSThomas Gleixner 	spin_unlock(&up->lock);
766b0af4bcbSThomas Gleixner }
767b0af4bcbSThomas Gleixner 
768b0af4bcbSThomas Gleixner /**
769b0af4bcbSThomas Gleixner  * uart_port_unlock_irq - Unlock the UART port and re-enable interrupts
770b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
771b0af4bcbSThomas Gleixner  */
uart_port_unlock_irq(struct uart_port * up)772b0af4bcbSThomas Gleixner static inline void uart_port_unlock_irq(struct uart_port *up)
773b0af4bcbSThomas Gleixner {
7744126f149SJohn Ogness 	__uart_port_nbcon_release(up);
775b0af4bcbSThomas Gleixner 	spin_unlock_irq(&up->lock);
776b0af4bcbSThomas Gleixner }
777b0af4bcbSThomas Gleixner 
778b0af4bcbSThomas Gleixner /**
77929bff582SRandy Dunlap  * uart_port_unlock_irqrestore - Unlock the UART port, restore interrupts
780b0af4bcbSThomas Gleixner  * @up:		Pointer to UART port structure
781b0af4bcbSThomas Gleixner  * @flags:	The saved interrupt flags for restore
782b0af4bcbSThomas Gleixner  */
uart_port_unlock_irqrestore(struct uart_port * up,unsigned long flags)783b0af4bcbSThomas Gleixner static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsigned long flags)
784b0af4bcbSThomas Gleixner {
7854126f149SJohn Ogness 	__uart_port_nbcon_release(up);
786b0af4bcbSThomas Gleixner 	spin_unlock_irqrestore(&up->lock, flags);
787b0af4bcbSThomas Gleixner }
788b0af4bcbSThomas Gleixner 
serial_port_in(struct uart_port * up,int offset)789927353a7SPaul Gortmaker static inline int serial_port_in(struct uart_port *up, int offset)
790927353a7SPaul Gortmaker {
791927353a7SPaul Gortmaker 	return up->serial_in(up, offset);
792927353a7SPaul Gortmaker }
793927353a7SPaul Gortmaker 
serial_port_out(struct uart_port * up,int offset,int value)794927353a7SPaul Gortmaker static inline void serial_port_out(struct uart_port *up, int offset, int value)
795927353a7SPaul Gortmaker {
796927353a7SPaul Gortmaker 	up->serial_out(up, offset, value);
797927353a7SPaul Gortmaker }
798927353a7SPaul Gortmaker 
7996f538fe3SLinus Walleij /**
8006f538fe3SLinus Walleij  * enum uart_pm_state - power states for UARTs
8016f538fe3SLinus Walleij  * @UART_PM_STATE_ON: UART is powered, up and operational
8026f538fe3SLinus Walleij  * @UART_PM_STATE_OFF: UART is powered off
8036f538fe3SLinus Walleij  * @UART_PM_STATE_UNDEFINED: sentinel
8046f538fe3SLinus Walleij  */
8056f538fe3SLinus Walleij enum uart_pm_state {
8066f538fe3SLinus Walleij 	UART_PM_STATE_ON = 0,
8076f538fe3SLinus Walleij 	UART_PM_STATE_OFF = 3, /* number taken from ACPI */
8086f538fe3SLinus Walleij 	UART_PM_STATE_UNDEFINED,
8096f538fe3SLinus Walleij };
8106f538fe3SLinus Walleij 
8111da177e4SLinus Torvalds /*
812ebd2c8f6SAlan Cox  * This is the state information which is persistent across opens.
813ebd2c8f6SAlan Cox  */
814ebd2c8f6SAlan Cox struct uart_state {
815df4f4dd4SAlan Cox 	struct tty_port		port;
816ebd2c8f6SAlan Cox 
8176f538fe3SLinus Walleij 	enum uart_pm_state	pm_state;
8181da177e4SLinus Torvalds 
8199ed19428SPeter Hurley 	atomic_t		refcount;
8209ed19428SPeter Hurley 	wait_queue_head_t	remove_wait;
821ebd2c8f6SAlan Cox 	struct uart_port	*uart_port;
822f751928eSAlan Cox };
823f751928eSAlan Cox 
824f751928eSAlan Cox #define UART_XMIT_SIZE	PAGE_SIZE
825f751928eSAlan Cox 
826f751928eSAlan Cox 
8271da177e4SLinus Torvalds /* number of characters left in xmit buffer before we ask for more */
8281da177e4SLinus Torvalds #define WAKEUP_CHARS		256
8291da177e4SLinus Torvalds 
830e77cab77SIlpo Järvinen /**
831e77cab77SIlpo Järvinen  * uart_xmit_advance - Advance xmit buffer and account Tx'ed chars
832e77cab77SIlpo Järvinen  * @up: uart_port structure describing the port
833e77cab77SIlpo Järvinen  * @chars: number of characters sent
834e77cab77SIlpo Järvinen  *
835e77cab77SIlpo Järvinen  * This function advances the tail of circular xmit buffer by the number of
836e77cab77SIlpo Järvinen  * @chars transmitted and handles accounting of transmitted bytes (into
837e77cab77SIlpo Järvinen  * @up's icount.tx).
838e77cab77SIlpo Järvinen  */
uart_xmit_advance(struct uart_port * up,unsigned int chars)839e77cab77SIlpo Järvinen static inline void uart_xmit_advance(struct uart_port *up, unsigned int chars)
840e77cab77SIlpo Järvinen {
8411788cf6aSJiri Slaby (SUSE) 	struct tty_port *tport = &up->state->port;
842e77cab77SIlpo Järvinen 
8431788cf6aSJiri Slaby (SUSE) 	kfifo_skip_count(&tport->xmit_fifo, chars);
844e77cab77SIlpo Järvinen 	up->icount.tx += chars;
845e77cab77SIlpo Järvinen }
846e77cab77SIlpo Järvinen 
uart_fifo_out(struct uart_port * up,unsigned char * buf,unsigned int chars)8471788cf6aSJiri Slaby (SUSE) static inline unsigned int uart_fifo_out(struct uart_port *up,
8481788cf6aSJiri Slaby (SUSE) 		unsigned char *buf, unsigned int chars)
8491788cf6aSJiri Slaby (SUSE) {
8501788cf6aSJiri Slaby (SUSE) 	struct tty_port *tport = &up->state->port;
8511788cf6aSJiri Slaby (SUSE) 
8521788cf6aSJiri Slaby (SUSE) 	chars = kfifo_out(&tport->xmit_fifo, buf, chars);
8531788cf6aSJiri Slaby (SUSE) 	up->icount.tx += chars;
8541788cf6aSJiri Slaby (SUSE) 
8551788cf6aSJiri Slaby (SUSE) 	return chars;
8561788cf6aSJiri Slaby (SUSE) }
8571788cf6aSJiri Slaby (SUSE) 
uart_fifo_get(struct uart_port * up,unsigned char * ch)8581788cf6aSJiri Slaby (SUSE) static inline unsigned int uart_fifo_get(struct uart_port *up,
8591788cf6aSJiri Slaby (SUSE) 		unsigned char *ch)
8601788cf6aSJiri Slaby (SUSE) {
8611788cf6aSJiri Slaby (SUSE) 	struct tty_port *tport = &up->state->port;
8621788cf6aSJiri Slaby (SUSE) 	unsigned int chars;
8631788cf6aSJiri Slaby (SUSE) 
8641788cf6aSJiri Slaby (SUSE) 	chars = kfifo_get(&tport->xmit_fifo, ch);
8651788cf6aSJiri Slaby (SUSE) 	up->icount.tx += chars;
8661788cf6aSJiri Slaby (SUSE) 
8671788cf6aSJiri Slaby (SUSE) 	return chars;
8681788cf6aSJiri Slaby (SUSE) }
8691788cf6aSJiri Slaby (SUSE) 
8701da177e4SLinus Torvalds struct module;
8711da177e4SLinus Torvalds struct tty_driver;
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds struct uart_driver {
8741da177e4SLinus Torvalds 	struct module		*owner;
8751da177e4SLinus Torvalds 	const char		*driver_name;
8761da177e4SLinus Torvalds 	const char		*dev_name;
8771da177e4SLinus Torvalds 	int			 major;
8781da177e4SLinus Torvalds 	int			 minor;
8791da177e4SLinus Torvalds 	int			 nr;
8801da177e4SLinus Torvalds 	struct console		*cons;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	/*
8831da177e4SLinus Torvalds 	 * these are private; the low level driver should not
8841da177e4SLinus Torvalds 	 * touch these; they should be initialised to NULL
8851da177e4SLinus Torvalds 	 */
8861da177e4SLinus Torvalds 	struct uart_state	*state;
8871da177e4SLinus Torvalds 	struct tty_driver	*tty_driver;
8881da177e4SLinus Torvalds };
8891da177e4SLinus Torvalds 
8901da177e4SLinus Torvalds void uart_write_wakeup(struct uart_port *port);
8911da177e4SLinus Torvalds 
8923ee07964SJiri Slaby (SUSE) /**
8933ee07964SJiri Slaby (SUSE)  * enum UART_TX_FLAGS -- flags for uart_port_tx_flags()
8943ee07964SJiri Slaby (SUSE)  *
8953ee07964SJiri Slaby (SUSE)  * @UART_TX_NOSTOP: don't call port->ops->stop_tx() on empty buffer
8963ee07964SJiri Slaby (SUSE)  */
8973ee07964SJiri Slaby (SUSE) enum UART_TX_FLAGS {
8983ee07964SJiri Slaby (SUSE) 	UART_TX_NOSTOP = BIT(0),
8993ee07964SJiri Slaby (SUSE) };
9003ee07964SJiri Slaby (SUSE) 
9013ee07964SJiri Slaby (SUSE) #define __uart_port_tx(uport, ch, flags, tx_ready, put_char, tx_done,	      \
9023ee07964SJiri Slaby (SUSE) 		       for_test, for_post)				      \
9038275b48bSJiri Slaby (SUSE) ({									      \
9048275b48bSJiri Slaby (SUSE) 	struct uart_port *__port = (uport);				      \
9051788cf6aSJiri Slaby (SUSE) 	struct tty_port *__tport = &__port->state->port;		      \
9068275b48bSJiri Slaby (SUSE) 	unsigned int pending;						      \
9078275b48bSJiri Slaby (SUSE) 									      \
9088275b48bSJiri Slaby (SUSE) 	for (; (for_test) && (tx_ready); (for_post), __port->icount.tx++) {   \
9098275b48bSJiri Slaby (SUSE) 		if (__port->x_char) {					      \
9108275b48bSJiri Slaby (SUSE) 			(ch) = __port->x_char;				      \
9118275b48bSJiri Slaby (SUSE) 			(put_char);					      \
9128275b48bSJiri Slaby (SUSE) 			__port->x_char = 0;				      \
9138275b48bSJiri Slaby (SUSE) 			continue;					      \
9148275b48bSJiri Slaby (SUSE) 		}							      \
9158275b48bSJiri Slaby (SUSE) 									      \
9161788cf6aSJiri Slaby (SUSE) 		if (uart_tx_stopped(__port))				      \
9178275b48bSJiri Slaby (SUSE) 			break;						      \
9188275b48bSJiri Slaby (SUSE) 									      \
9191788cf6aSJiri Slaby (SUSE) 		if (!kfifo_get(&__tport->xmit_fifo, &(ch)))		      \
9201788cf6aSJiri Slaby (SUSE) 			break;						      \
9211788cf6aSJiri Slaby (SUSE) 									      \
9228275b48bSJiri Slaby (SUSE) 		(put_char);						      \
9238275b48bSJiri Slaby (SUSE) 	}								      \
9248275b48bSJiri Slaby (SUSE) 									      \
9258275b48bSJiri Slaby (SUSE) 	(tx_done);							      \
9268275b48bSJiri Slaby (SUSE) 									      \
9271788cf6aSJiri Slaby (SUSE) 	pending = kfifo_len(&__tport->xmit_fifo);			      \
9288275b48bSJiri Slaby (SUSE) 	if (pending < WAKEUP_CHARS) {					      \
9298275b48bSJiri Slaby (SUSE) 		uart_write_wakeup(__port);				      \
9308275b48bSJiri Slaby (SUSE) 									      \
931c5603e2aSDoug Brown 		if (!((flags) & UART_TX_NOSTOP) && pending == 0)	      \
9328275b48bSJiri Slaby (SUSE) 			__port->ops->stop_tx(__port);			      \
9338275b48bSJiri Slaby (SUSE) 	}								      \
9348275b48bSJiri Slaby (SUSE) 									      \
9358275b48bSJiri Slaby (SUSE) 	pending;							      \
9368275b48bSJiri Slaby (SUSE) })
9378275b48bSJiri Slaby (SUSE) 
9388275b48bSJiri Slaby (SUSE) /**
9398275b48bSJiri Slaby (SUSE)  * uart_port_tx_limited -- transmit helper for uart_port with count limiting
9408275b48bSJiri Slaby (SUSE)  * @port: uart port
9418275b48bSJiri Slaby (SUSE)  * @ch: variable to store a character to be written to the HW
9428275b48bSJiri Slaby (SUSE)  * @count: a limit of characters to send
9438275b48bSJiri Slaby (SUSE)  * @tx_ready: can HW accept more data function
9448275b48bSJiri Slaby (SUSE)  * @put_char: function to write a character
9458275b48bSJiri Slaby (SUSE)  * @tx_done: function to call after the loop is done
9468275b48bSJiri Slaby (SUSE)  *
9478275b48bSJiri Slaby (SUSE)  * This helper transmits characters from the xmit buffer to the hardware using
9488275b48bSJiri Slaby (SUSE)  * @put_char(). It does so until @count characters are sent and while @tx_ready
9498275b48bSJiri Slaby (SUSE)  * evaluates to true.
9508275b48bSJiri Slaby (SUSE)  *
9518275b48bSJiri Slaby (SUSE)  * Returns: the number of characters in the xmit buffer when done.
9528275b48bSJiri Slaby (SUSE)  *
9538275b48bSJiri Slaby (SUSE)  * The expression in macro parameters shall be designed as follows:
9548275b48bSJiri Slaby (SUSE)  *  * **tx_ready:** should evaluate to true if the HW can accept more data to
9558275b48bSJiri Slaby (SUSE)  *    be sent. This parameter can be %true, which means the HW is always ready.
9568275b48bSJiri Slaby (SUSE)  *  * **put_char:** shall write @ch to the device of @port.
9578275b48bSJiri Slaby (SUSE)  *  * **tx_done:** when the write loop is done, this can perform arbitrary
9588275b48bSJiri Slaby (SUSE)  *    action before potential invocation of ops->stop_tx() happens. If the
9598275b48bSJiri Slaby (SUSE)  *    driver does not need to do anything, use e.g. ({}).
9608275b48bSJiri Slaby (SUSE)  *
9618275b48bSJiri Slaby (SUSE)  * For all of them, @port->lock is held, interrupts are locally disabled and
9628275b48bSJiri Slaby (SUSE)  * the expressions must not sleep.
9638275b48bSJiri Slaby (SUSE)  */
9648275b48bSJiri Slaby (SUSE) #define uart_port_tx_limited(port, ch, count, tx_ready, put_char, tx_done) ({ \
9658275b48bSJiri Slaby (SUSE) 	unsigned int __count = (count);					      \
9663ee07964SJiri Slaby (SUSE) 	__uart_port_tx(port, ch, 0, tx_ready, put_char, tx_done, __count,     \
9678275b48bSJiri Slaby (SUSE) 			__count--);					      \
9688275b48bSJiri Slaby (SUSE) })
9698275b48bSJiri Slaby (SUSE) 
9708275b48bSJiri Slaby (SUSE) /**
9719bb43b9eSJonas Gorski  * uart_port_tx_limited_flags -- transmit helper for uart_port with count limiting with flags
9729bb43b9eSJonas Gorski  * @port: uart port
9739bb43b9eSJonas Gorski  * @ch: variable to store a character to be written to the HW
9749bb43b9eSJonas Gorski  * @flags: %UART_TX_NOSTOP or similar
9759bb43b9eSJonas Gorski  * @count: a limit of characters to send
9769bb43b9eSJonas Gorski  * @tx_ready: can HW accept more data function
9779bb43b9eSJonas Gorski  * @put_char: function to write a character
9789bb43b9eSJonas Gorski  * @tx_done: function to call after the loop is done
9799bb43b9eSJonas Gorski  *
9809bb43b9eSJonas Gorski  * See uart_port_tx_limited() for more details.
9819bb43b9eSJonas Gorski  */
9829bb43b9eSJonas Gorski #define uart_port_tx_limited_flags(port, ch, flags, count, tx_ready, put_char, tx_done) ({ \
9839bb43b9eSJonas Gorski 	unsigned int __count = (count);							   \
9849bb43b9eSJonas Gorski 	__uart_port_tx(port, ch, flags, tx_ready, put_char, tx_done, __count,		   \
9859bb43b9eSJonas Gorski 			__count--);							   \
9869bb43b9eSJonas Gorski })
9879bb43b9eSJonas Gorski 
9889bb43b9eSJonas Gorski /**
9898275b48bSJiri Slaby (SUSE)  * uart_port_tx -- transmit helper for uart_port
9908275b48bSJiri Slaby (SUSE)  * @port: uart port
9918275b48bSJiri Slaby (SUSE)  * @ch: variable to store a character to be written to the HW
9928275b48bSJiri Slaby (SUSE)  * @tx_ready: can HW accept more data function
9938275b48bSJiri Slaby (SUSE)  * @put_char: function to write a character
9948275b48bSJiri Slaby (SUSE)  *
9958275b48bSJiri Slaby (SUSE)  * See uart_port_tx_limited() for more details.
9968275b48bSJiri Slaby (SUSE)  */
9978275b48bSJiri Slaby (SUSE) #define uart_port_tx(port, ch, tx_ready, put_char)			\
9983ee07964SJiri Slaby (SUSE) 	__uart_port_tx(port, ch, 0, tx_ready, put_char, ({}), true, ({}))
9998275b48bSJiri Slaby (SUSE) 
10003ee07964SJiri Slaby (SUSE) 
10013ee07964SJiri Slaby (SUSE) /**
10023ee07964SJiri Slaby (SUSE)  * uart_port_tx_flags -- transmit helper for uart_port with flags
10033ee07964SJiri Slaby (SUSE)  * @port: uart port
10043ee07964SJiri Slaby (SUSE)  * @ch: variable to store a character to be written to the HW
10053ee07964SJiri Slaby (SUSE)  * @flags: %UART_TX_NOSTOP or similar
10063ee07964SJiri Slaby (SUSE)  * @tx_ready: can HW accept more data function
10073ee07964SJiri Slaby (SUSE)  * @put_char: function to write a character
10083ee07964SJiri Slaby (SUSE)  *
10093ee07964SJiri Slaby (SUSE)  * See uart_port_tx_limited() for more details.
10103ee07964SJiri Slaby (SUSE)  */
10113ee07964SJiri Slaby (SUSE) #define uart_port_tx_flags(port, ch, flags, tx_ready, put_char)		\
10123ee07964SJiri Slaby (SUSE) 	__uart_port_tx(port, ch, flags, tx_ready, put_char, ({}), true, ({}))
10131da177e4SLinus Torvalds /*
10141da177e4SLinus Torvalds  * Baud rate helpers.
10151da177e4SLinus Torvalds  */
10161da177e4SLinus Torvalds void uart_update_timeout(struct uart_port *port, unsigned int cflag,
10171da177e4SLinus Torvalds 			 unsigned int baud);
1018606d099cSAlan Cox unsigned int uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
1019bec5b814SIlpo Järvinen 				const struct ktermios *old, unsigned int min,
10201da177e4SLinus Torvalds 				unsigned int max);
10211da177e4SLinus Torvalds unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);
10221da177e4SLinus Torvalds 
1023f9008285SIlpo Järvinen /*
1024f9008285SIlpo Järvinen  * Calculates FIFO drain time.
1025f9008285SIlpo Järvinen  */
uart_fifo_timeout(struct uart_port * port)1026f9008285SIlpo Järvinen static inline unsigned long uart_fifo_timeout(struct uart_port *port)
1027f9008285SIlpo Järvinen {
1028f9008285SIlpo Järvinen 	u64 fifo_timeout = (u64)READ_ONCE(port->frame_time) * port->fifosize;
1029f9008285SIlpo Järvinen 
1030f9008285SIlpo Järvinen 	/* Add .02 seconds of slop */
1031f9008285SIlpo Järvinen 	fifo_timeout += 20 * NSEC_PER_MSEC;
1032f9008285SIlpo Järvinen 
1033f9008285SIlpo Järvinen 	return max(nsecs_to_jiffies(fifo_timeout), 1UL);
1034f9008285SIlpo Järvinen }
1035f9008285SIlpo Järvinen 
103654381067SAnton Vorontsov /* Base timer interval for polling */
uart_poll_timeout(struct uart_port * port)1037cb86a338SVamshi Gajjela static inline unsigned long uart_poll_timeout(struct uart_port *port)
103854381067SAnton Vorontsov {
1039cb86a338SVamshi Gajjela 	unsigned long timeout = uart_fifo_timeout(port);
104054381067SAnton Vorontsov 
104154381067SAnton Vorontsov 	return timeout > 6 ? (timeout / 2 - 2) : 1;
104254381067SAnton Vorontsov }
104354381067SAnton Vorontsov 
10441da177e4SLinus Torvalds /*
10451da177e4SLinus Torvalds  * Console helpers.
10461da177e4SLinus Torvalds  */
10479aac5887SRob Herring struct earlycon_device {
10489aac5887SRob Herring 	struct console *con;
10499aac5887SRob Herring 	struct uart_port port;
105041000b03SRicardo Ribalda 	char options[32];		/* e.g., 115200n8 */
10519aac5887SRob Herring 	unsigned int baud;
10529aac5887SRob Herring };
10539aac5887SRob Herring 
1054470ca0deSPeter Hurley struct earlycon_id {
1055c1c734cbSDouglas Anderson 	char	name[15];
1056c1c734cbSDouglas Anderson 	char	name_term;	/* In case compiler didn't '\0' term name */
10572eaa7909SPeter Hurley 	char	compatible[128];
1058470ca0deSPeter Hurley 	int	(*setup)(struct earlycon_device *, const char *options);
1059dd709e72SDaniel Kurtz };
1060470ca0deSPeter Hurley 
106162dcd9c5SJohan Hovold extern const struct earlycon_id __earlycon_table[];
106262dcd9c5SJohan Hovold extern const struct earlycon_id __earlycon_table_end[];
10632eaa7909SPeter Hurley 
1064f8ba3647SMasahiro Yamada #if defined(CONFIG_SERIAL_EARLYCON) && !defined(MODULE)
1065f8ba3647SMasahiro Yamada #define EARLYCON_USED_OR_UNUSED	__used
1066f8ba3647SMasahiro Yamada #else
1067f8ba3647SMasahiro Yamada #define EARLYCON_USED_OR_UNUSED	__maybe_unused
1068f8ba3647SMasahiro Yamada #endif
1069f8ba3647SMasahiro Yamada 
107062dcd9c5SJohan Hovold #define OF_EARLYCON_DECLARE(_name, compat, fn)				\
107162dcd9c5SJohan Hovold 	static const struct earlycon_id __UNIQUE_ID(__earlycon_##_name) \
107262dcd9c5SJohan Hovold 		EARLYCON_USED_OR_UNUSED  __section("__earlycon_table")  \
107362dcd9c5SJohan Hovold 		__aligned(__alignof__(struct earlycon_id))		\
10742eaa7909SPeter Hurley 		= { .name = __stringify(_name),				\
10752eaa7909SPeter Hurley 		    .compatible = compat,				\
107676437b34SJohan Hovold 		    .setup = fn }
10772eaa7909SPeter Hurley 
10782eaa7909SPeter Hurley #define EARLYCON_DECLARE(_name, fn)	OF_EARLYCON_DECLARE(_name, "", fn)
10792eaa7909SPeter Hurley 
10806229ad99SIlpo Järvinen int of_setup_earlycon(const struct earlycon_id *match, unsigned long node,
10814d118c9aSPeter Hurley 		      const char *options);
1082b0b6abd3SRob Herring 
1083ad1696f6SAleksey Makarov #ifdef CONFIG_SERIAL_EARLYCON
10840231d000SPrarit Bhargava extern bool earlycon_acpi_spcr_enable __initdata;
1085ad1696f6SAleksey Makarov int setup_earlycon(char *buf);
1086ad1696f6SAleksey Makarov #else
1087219c7b06SMathieu Malaterre static const bool earlycon_acpi_spcr_enable EARLYCON_USED_OR_UNUSED;
setup_earlycon(char * buf)1088ad1696f6SAleksey Makarov static inline int setup_earlycon(char *buf) { return 0; }
1089ad1696f6SAleksey Makarov #endif
1090ad1696f6SAleksey Makarov 
1091452b9b24SJohn Ogness /* Variant of uart_console_registered() when the console_list_lock is held. */
uart_console_registered_locked(struct uart_port * port)1092452b9b24SJohn Ogness static inline bool uart_console_registered_locked(struct uart_port *port)
1093f9b11229SIlpo Järvinen {
1094452b9b24SJohn Ogness 	return uart_console(port) && console_is_registered_locked(port->cons);
1095452b9b24SJohn Ogness }
1096452b9b24SJohn Ogness 
uart_console_registered(struct uart_port * port)1097452b9b24SJohn Ogness static inline bool uart_console_registered(struct uart_port *port)
1098452b9b24SJohn Ogness {
1099452b9b24SJohn Ogness 	return uart_console(port) && console_is_registered(port->cons);
1100f9b11229SIlpo Järvinen }
1101f9b11229SIlpo Järvinen 
11021da177e4SLinus Torvalds struct uart_port *uart_get_console(struct uart_port *ports, int nr,
11031da177e4SLinus Torvalds 				   struct console *c);
110446e36683SAlexander Sverdlin int uart_parse_earlycon(char *p, unsigned char *iotype, resource_size_t *addr,
110573abaf87SPeter Hurley 			char **options);
11060f646b63SPaul Cercueil void uart_parse_options(const char *options, int *baud, int *parity, int *bits,
11071da177e4SLinus Torvalds 			int *flow);
11081da177e4SLinus Torvalds int uart_set_options(struct uart_port *port, struct console *co, int baud,
11091da177e4SLinus Torvalds 		     int parity, int bits, int flow);
11101da177e4SLinus Torvalds struct tty_driver *uart_console_device(struct console *co, int *index);
1111d358788fSRussell King void uart_console_write(struct uart_port *port, const char *s,
1112d358788fSRussell King 			unsigned int count,
11133f8bab17SJiri Slaby 			void (*putchar)(struct uart_port *, unsigned char));
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds /*
11161da177e4SLinus Torvalds  * Port/driver registration/removal
11171da177e4SLinus Torvalds  */
11181da177e4SLinus Torvalds int uart_register_driver(struct uart_driver *uart);
11191da177e4SLinus Torvalds void uart_unregister_driver(struct uart_driver *uart);
11201da177e4SLinus Torvalds int uart_add_one_port(struct uart_driver *reg, struct uart_port *port);
1121d5b3d02dSUwe Kleine-König void uart_remove_one_port(struct uart_driver *reg, struct uart_port *port);
1122e894b600SAndy Shevchenko int uart_read_port_properties(struct uart_port *port);
1123e894b600SAndy Shevchenko int uart_read_and_validate_port_properties(struct uart_port *port);
1124b8be5db5SJiri Slaby bool uart_match_port(const struct uart_port *port1,
1125b8be5db5SJiri Slaby 		const struct uart_port *port2);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds /*
11281da177e4SLinus Torvalds  * Power Management
11291da177e4SLinus Torvalds  */
11301da177e4SLinus Torvalds int uart_suspend_port(struct uart_driver *reg, struct uart_port *port);
11311da177e4SLinus Torvalds int uart_resume_port(struct uart_driver *reg, struct uart_port *port);
11321da177e4SLinus Torvalds 
uart_tx_stopped(struct uart_port * port)1133f751928eSAlan Cox static inline int uart_tx_stopped(struct uart_port *port)
1134f751928eSAlan Cox {
1135ebd2c8f6SAlan Cox 	struct tty_struct *tty = port->state->port.tty;
11366e94dbc7SJiri Slaby 	if ((tty && tty->flow.stopped) || port->hw_stopped)
1137f751928eSAlan Cox 		return 1;
1138f751928eSAlan Cox 	return 0;
1139f751928eSAlan Cox }
11401da177e4SLinus Torvalds 
uart_cts_enabled(struct uart_port * uport)1141299245a1SPeter Hurley static inline bool uart_cts_enabled(struct uart_port *uport)
1142299245a1SPeter Hurley {
1143d4260b51SPeter Hurley 	return !!(uport->status & UPSTAT_CTS_ENABLE);
1144299245a1SPeter Hurley }
1145299245a1SPeter Hurley 
uart_softcts_mode(struct uart_port * uport)1146391f93f2SPeter Hurley static inline bool uart_softcts_mode(struct uart_port *uport)
1147391f93f2SPeter Hurley {
1148391f93f2SPeter Hurley 	upstat_t mask = UPSTAT_CTS_ENABLE | UPSTAT_AUTOCTS;
1149391f93f2SPeter Hurley 
1150391f93f2SPeter Hurley 	return ((uport->status & mask) == UPSTAT_CTS_ENABLE);
1151391f93f2SPeter Hurley }
1152391f93f2SPeter Hurley 
11531da177e4SLinus Torvalds /*
11541da177e4SLinus Torvalds  * The following are helper functions for the low level drivers.
11551da177e4SLinus Torvalds  */
1156027d7dacSJiri Slaby 
11576229ad99SIlpo Järvinen void uart_handle_dcd_change(struct uart_port *uport, bool active);
11586229ad99SIlpo Järvinen void uart_handle_cts_change(struct uart_port *uport, bool active);
1159027d7dacSJiri Slaby 
11606229ad99SIlpo Järvinen void uart_insert_char(struct uart_port *port, unsigned int status,
1161df007fa0SJiri Slaby 		      unsigned int overrun, u8 ch, u8 flag);
1162027d7dacSJiri Slaby 
1163f58c252eSIlpo Järvinen void uart_xchar_out(struct uart_port *uport, int offset);
1164f58c252eSIlpo Järvinen 
116508d54703SJohan Hovold #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
116608d54703SJohan Hovold #define SYSRQ_TIMEOUT	(HZ * 5)
116708d54703SJohan Hovold 
116812ae2359SJiri Slaby bool uart_try_toggle_sysrq(struct uart_port *port, u8 ch);
116908d54703SJohan Hovold 
uart_handle_sysrq_char(struct uart_port * port,u8 ch)117012ae2359SJiri Slaby static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
117108d54703SJohan Hovold {
117222538565SJohan Hovold 	if (!port->sysrq)
117308d54703SJohan Hovold 		return 0;
117408d54703SJohan Hovold 
117508d54703SJohan Hovold 	if (ch && time_before(jiffies, port->sysrq)) {
117608d54703SJohan Hovold 		if (sysrq_mask()) {
117708d54703SJohan Hovold 			handle_sysrq(ch);
117808d54703SJohan Hovold 			port->sysrq = 0;
117908d54703SJohan Hovold 			return 1;
118008d54703SJohan Hovold 		}
118108d54703SJohan Hovold 		if (uart_try_toggle_sysrq(port, ch))
118208d54703SJohan Hovold 			return 1;
118308d54703SJohan Hovold 	}
118408d54703SJohan Hovold 	port->sysrq = 0;
118508d54703SJohan Hovold 
118608d54703SJohan Hovold 	return 0;
118708d54703SJohan Hovold }
118808d54703SJohan Hovold 
uart_prepare_sysrq_char(struct uart_port * port,u8 ch)118912ae2359SJiri Slaby static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
119008d54703SJohan Hovold {
119122538565SJohan Hovold 	if (!port->sysrq)
119208d54703SJohan Hovold 		return 0;
119308d54703SJohan Hovold 
119408d54703SJohan Hovold 	if (ch && time_before(jiffies, port->sysrq)) {
119508d54703SJohan Hovold 		if (sysrq_mask()) {
119608d54703SJohan Hovold 			port->sysrq_ch = ch;
119708d54703SJohan Hovold 			port->sysrq = 0;
119808d54703SJohan Hovold 			return 1;
119908d54703SJohan Hovold 		}
120008d54703SJohan Hovold 		if (uart_try_toggle_sysrq(port, ch))
120108d54703SJohan Hovold 			return 1;
120208d54703SJohan Hovold 	}
120308d54703SJohan Hovold 	port->sysrq = 0;
120408d54703SJohan Hovold 
120508d54703SJohan Hovold 	return 0;
120608d54703SJohan Hovold }
120708d54703SJohan Hovold 
uart_unlock_and_check_sysrq(struct uart_port * port)120875f4e830SJohan Hovold static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
120908d54703SJohan Hovold {
121012ae2359SJiri Slaby 	u8 sysrq_ch;
121108d54703SJohan Hovold 
121208d54703SJohan Hovold 	if (!port->has_sysrq) {
1213c5cbdb76SThomas Gleixner 		uart_port_unlock(port);
121408d54703SJohan Hovold 		return;
121508d54703SJohan Hovold 	}
121608d54703SJohan Hovold 
121708d54703SJohan Hovold 	sysrq_ch = port->sysrq_ch;
121808d54703SJohan Hovold 	port->sysrq_ch = 0;
121908d54703SJohan Hovold 
1220c5cbdb76SThomas Gleixner 	uart_port_unlock(port);
122108d54703SJohan Hovold 
122208d54703SJohan Hovold 	if (sysrq_ch)
122308d54703SJohan Hovold 		handle_sysrq(sysrq_ch);
122408d54703SJohan Hovold }
1225853a9ae2SJohan Hovold 
uart_unlock_and_check_sysrq_irqrestore(struct uart_port * port,unsigned long flags)1226853a9ae2SJohan Hovold static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1227853a9ae2SJohan Hovold 		unsigned long flags)
1228853a9ae2SJohan Hovold {
122912ae2359SJiri Slaby 	u8 sysrq_ch;
1230853a9ae2SJohan Hovold 
1231853a9ae2SJohan Hovold 	if (!port->has_sysrq) {
1232c5cbdb76SThomas Gleixner 		uart_port_unlock_irqrestore(port, flags);
1233853a9ae2SJohan Hovold 		return;
1234853a9ae2SJohan Hovold 	}
1235853a9ae2SJohan Hovold 
1236853a9ae2SJohan Hovold 	sysrq_ch = port->sysrq_ch;
1237853a9ae2SJohan Hovold 	port->sysrq_ch = 0;
1238853a9ae2SJohan Hovold 
1239c5cbdb76SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
1240853a9ae2SJohan Hovold 
1241853a9ae2SJohan Hovold 	if (sysrq_ch)
1242853a9ae2SJohan Hovold 		handle_sysrq(sysrq_ch);
1243853a9ae2SJohan Hovold }
124408d54703SJohan Hovold #else	/* CONFIG_MAGIC_SYSRQ_SERIAL */
uart_handle_sysrq_char(struct uart_port * port,u8 ch)124512ae2359SJiri Slaby static inline int uart_handle_sysrq_char(struct uart_port *port, u8 ch)
124608d54703SJohan Hovold {
124708d54703SJohan Hovold 	return 0;
124808d54703SJohan Hovold }
uart_prepare_sysrq_char(struct uart_port * port,u8 ch)124912ae2359SJiri Slaby static inline int uart_prepare_sysrq_char(struct uart_port *port, u8 ch)
125008d54703SJohan Hovold {
125108d54703SJohan Hovold 	return 0;
125208d54703SJohan Hovold }
uart_unlock_and_check_sysrq(struct uart_port * port)125375f4e830SJohan Hovold static inline void uart_unlock_and_check_sysrq(struct uart_port *port)
125408d54703SJohan Hovold {
1255c5cbdb76SThomas Gleixner 	uart_port_unlock(port);
125608d54703SJohan Hovold }
uart_unlock_and_check_sysrq_irqrestore(struct uart_port * port,unsigned long flags)1257853a9ae2SJohan Hovold static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port *port,
1258853a9ae2SJohan Hovold 		unsigned long flags)
1259853a9ae2SJohan Hovold {
1260c5cbdb76SThomas Gleixner 	uart_port_unlock_irqrestore(port, flags);
1261853a9ae2SJohan Hovold }
126208d54703SJohan Hovold #endif	/* CONFIG_MAGIC_SYSRQ_SERIAL */
126308d54703SJohan Hovold 
126408d54703SJohan Hovold /*
126508d54703SJohan Hovold  * We do the SysRQ and SAK checking like this...
126608d54703SJohan Hovold  */
uart_handle_break(struct uart_port * port)126708d54703SJohan Hovold static inline int uart_handle_break(struct uart_port *port)
126808d54703SJohan Hovold {
126908d54703SJohan Hovold 	struct uart_state *state = port->state;
127008d54703SJohan Hovold 
127108d54703SJohan Hovold 	if (port->handle_break)
127208d54703SJohan Hovold 		port->handle_break(port);
127308d54703SJohan Hovold 
127408d54703SJohan Hovold #ifdef CONFIG_MAGIC_SYSRQ_SERIAL
127508d54703SJohan Hovold 	if (port->has_sysrq && uart_console(port)) {
127608d54703SJohan Hovold 		if (!port->sysrq) {
127708d54703SJohan Hovold 			port->sysrq = jiffies + SYSRQ_TIMEOUT;
127808d54703SJohan Hovold 			return 1;
127908d54703SJohan Hovold 		}
128008d54703SJohan Hovold 		port->sysrq = 0;
128108d54703SJohan Hovold 	}
128208d54703SJohan Hovold #endif
128308d54703SJohan Hovold 	if (port->flags & UPF_SAK)
128408d54703SJohan Hovold 		do_SAK(state->port.tty);
128508d54703SJohan Hovold 	return 0;
128608d54703SJohan Hovold }
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds /*
12891da177e4SLinus Torvalds  *	UART_ENABLE_MS - determine if port should enable modem status irqs
12901da177e4SLinus Torvalds  */
12911da177e4SLinus Torvalds #define UART_ENABLE_MS(port,cflag)	((port)->flags & UPF_HARDPPS_CD || \
12921da177e4SLinus Torvalds 					 (cflag) & CRTSCTS || \
12931da177e4SLinus Torvalds 					 !((cflag) & CLOCAL))
12941da177e4SLinus Torvalds 
1295c150c0f3SLukas Wunner int uart_get_rs485_mode(struct uart_port *port);
12961da177e4SLinus Torvalds #endif /* LINUX_SERIAL_CORE_H */
1297