xref: /openbmc/linux/drivers/tty/serial/8250/8250.h (revision 874c8ca1)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  *  Driver for 8250/16550-type serial ports
4  *
5  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
6  *
7  *  Copyright (C) 2001 Russell King.
8  */
9 
10 #include <linux/bits.h>
11 #include <linux/serial_8250.h>
12 #include <linux/serial_reg.h>
13 #include <linux/dmaengine.h>
14 
15 #include "../serial_mctrl_gpio.h"
16 
17 struct uart_8250_dma {
18 	int (*tx_dma)(struct uart_8250_port *p);
19 	int (*rx_dma)(struct uart_8250_port *p);
20 	void (*prepare_tx_dma)(struct uart_8250_port *p);
21 	void (*prepare_rx_dma)(struct uart_8250_port *p);
22 
23 	/* Filter function */
24 	dma_filter_fn		fn;
25 	/* Parameter to the filter function */
26 	void			*rx_param;
27 	void			*tx_param;
28 
29 	struct dma_slave_config	rxconf;
30 	struct dma_slave_config	txconf;
31 
32 	struct dma_chan		*rxchan;
33 	struct dma_chan		*txchan;
34 
35 	/* Device address base for DMA operations */
36 	phys_addr_t		rx_dma_addr;
37 	phys_addr_t		tx_dma_addr;
38 
39 	/* DMA address of the buffer in memory */
40 	dma_addr_t		rx_addr;
41 	dma_addr_t		tx_addr;
42 
43 	dma_cookie_t		rx_cookie;
44 	dma_cookie_t		tx_cookie;
45 
46 	void			*rx_buf;
47 
48 	size_t			rx_size;
49 	size_t			tx_size;
50 
51 	unsigned char		tx_running;
52 	unsigned char		tx_err;
53 	unsigned char		rx_running;
54 };
55 
56 struct old_serial_port {
57 	unsigned int uart;
58 	unsigned int baud_base;
59 	unsigned int port;
60 	unsigned int irq;
61 	upf_t        flags;
62 	unsigned char io_type;
63 	unsigned char __iomem *iomem_base;
64 	unsigned short iomem_reg_shift;
65 };
66 
67 struct serial8250_config {
68 	const char	*name;
69 	unsigned short	fifo_size;
70 	unsigned short	tx_loadsz;
71 	unsigned char	fcr;
72 	unsigned char	rxtrig_bytes[UART_FCR_R_TRIG_MAX_STATE];
73 	unsigned int	flags;
74 };
75 
76 #define UART_CAP_FIFO	BIT(8)	/* UART has FIFO */
77 #define UART_CAP_EFR	BIT(9)	/* UART has EFR */
78 #define UART_CAP_SLEEP	BIT(10)	/* UART has IER sleep */
79 #define UART_CAP_AFE	BIT(11)	/* MCR-based hw flow control */
80 #define UART_CAP_UUE	BIT(12)	/* UART needs IER bit 6 set (Xscale) */
81 #define UART_CAP_RTOIE	BIT(13)	/* UART needs IER bit 4 set (Xscale, Tegra) */
82 #define UART_CAP_HFIFO	BIT(14)	/* UART has a "hidden" FIFO */
83 #define UART_CAP_RPM	BIT(15)	/* Runtime PM is active while idle */
84 #define UART_CAP_IRDA	BIT(16)	/* UART supports IrDA line discipline */
85 #define UART_CAP_MINI	BIT(17)	/* Mini UART on BCM283X family lacks:
86 					 * STOP PARITY EPAR SPAR WLEN5 WLEN6
87 					 */
88 #define UART_CAP_NOTEMT	BIT(18)	/* UART without interrupt on TEMT available */
89 
90 #define UART_BUG_QUOT	BIT(0)	/* UART has buggy quot LSB */
91 #define UART_BUG_TXEN	BIT(1)	/* UART has buggy TX IIR status */
92 #define UART_BUG_NOMSR	BIT(2)	/* UART has buggy MSR status bits (Au1x00) */
93 #define UART_BUG_THRE	BIT(3)	/* UART has buggy THRE reassertion */
94 #define UART_BUG_PARITY	BIT(4)	/* UART mishandles parity if FIFO enabled */
95 #define UART_BUG_TXRACE	BIT(5)	/* UART Tx fails to set remote DR */
96 
97 
98 #ifdef CONFIG_SERIAL_8250_SHARE_IRQ
99 #define SERIAL8250_SHARE_IRQS 1
100 #else
101 #define SERIAL8250_SHARE_IRQS 0
102 #endif
103 
104 #define SERIAL8250_PORT_FLAGS(_base, _irq, _flags)		\
105 	{							\
106 		.iobase		= _base,			\
107 		.irq		= _irq,				\
108 		.uartclk	= 1843200,			\
109 		.iotype		= UPIO_PORT,			\
110 		.flags		= UPF_BOOT_AUTOCONF | (_flags),	\
111 	}
112 
113 #define SERIAL8250_PORT(_base, _irq) SERIAL8250_PORT_FLAGS(_base, _irq, 0)
114 
115 
116 static inline int serial_in(struct uart_8250_port *up, int offset)
117 {
118 	return up->port.serial_in(&up->port, offset);
119 }
120 
121 static inline void serial_out(struct uart_8250_port *up, int offset, int value)
122 {
123 	up->port.serial_out(&up->port, offset, value);
124 }
125 
126 /*
127  * For the 16C950
128  */
129 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
130 {
131 	serial_out(up, UART_SCR, offset);
132 	serial_out(up, UART_ICR, value);
133 }
134 
135 static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
136 						   int offset)
137 {
138 	unsigned int value;
139 
140 	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
141 	serial_out(up, UART_SCR, offset);
142 	value = serial_in(up, UART_ICR);
143 	serial_icr_write(up, UART_ACR, up->acr);
144 
145 	return value;
146 }
147 
148 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
149 
150 static inline int serial_dl_read(struct uart_8250_port *up)
151 {
152 	return up->dl_read(up);
153 }
154 
155 static inline void serial_dl_write(struct uart_8250_port *up, int value)
156 {
157 	up->dl_write(up, value);
158 }
159 
160 static inline bool serial8250_set_THRI(struct uart_8250_port *up)
161 {
162 	if (up->ier & UART_IER_THRI)
163 		return false;
164 	up->ier |= UART_IER_THRI;
165 	serial_out(up, UART_IER, up->ier);
166 	return true;
167 }
168 
169 static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
170 {
171 	if (!(up->ier & UART_IER_THRI))
172 		return false;
173 	up->ier &= ~UART_IER_THRI;
174 	serial_out(up, UART_IER, up->ier);
175 	return true;
176 }
177 
178 struct uart_8250_port *serial8250_get_port(int line);
179 
180 void serial8250_rpm_get(struct uart_8250_port *p);
181 void serial8250_rpm_put(struct uart_8250_port *p);
182 
183 void serial8250_rpm_get_tx(struct uart_8250_port *p);
184 void serial8250_rpm_put_tx(struct uart_8250_port *p);
185 
186 int serial8250_em485_config(struct uart_port *port, struct serial_rs485 *rs485);
187 void serial8250_em485_start_tx(struct uart_8250_port *p);
188 void serial8250_em485_stop_tx(struct uart_8250_port *p);
189 void serial8250_em485_destroy(struct uart_8250_port *p);
190 
191 /* MCR <-> TIOCM conversion */
192 static inline int serial8250_TIOCM_to_MCR(int tiocm)
193 {
194 	int mcr = 0;
195 
196 	if (tiocm & TIOCM_RTS)
197 		mcr |= UART_MCR_RTS;
198 	if (tiocm & TIOCM_DTR)
199 		mcr |= UART_MCR_DTR;
200 	if (tiocm & TIOCM_OUT1)
201 		mcr |= UART_MCR_OUT1;
202 	if (tiocm & TIOCM_OUT2)
203 		mcr |= UART_MCR_OUT2;
204 	if (tiocm & TIOCM_LOOP)
205 		mcr |= UART_MCR_LOOP;
206 
207 	return mcr;
208 }
209 
210 static inline int serial8250_MCR_to_TIOCM(int mcr)
211 {
212 	int tiocm = 0;
213 
214 	if (mcr & UART_MCR_RTS)
215 		tiocm |= TIOCM_RTS;
216 	if (mcr & UART_MCR_DTR)
217 		tiocm |= TIOCM_DTR;
218 	if (mcr & UART_MCR_OUT1)
219 		tiocm |= TIOCM_OUT1;
220 	if (mcr & UART_MCR_OUT2)
221 		tiocm |= TIOCM_OUT2;
222 	if (mcr & UART_MCR_LOOP)
223 		tiocm |= TIOCM_LOOP;
224 
225 	return tiocm;
226 }
227 
228 /* MSR <-> TIOCM conversion */
229 static inline int serial8250_MSR_to_TIOCM(int msr)
230 {
231 	int tiocm = 0;
232 
233 	if (msr & UART_MSR_DCD)
234 		tiocm |= TIOCM_CAR;
235 	if (msr & UART_MSR_RI)
236 		tiocm |= TIOCM_RNG;
237 	if (msr & UART_MSR_DSR)
238 		tiocm |= TIOCM_DSR;
239 	if (msr & UART_MSR_CTS)
240 		tiocm |= TIOCM_CTS;
241 
242 	return tiocm;
243 }
244 
245 static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
246 {
247 	serial_out(up, UART_MCR, value);
248 
249 	if (up->gpios)
250 		mctrl_gpio_set(up->gpios, serial8250_MCR_to_TIOCM(value));
251 }
252 
253 static inline int serial8250_in_MCR(struct uart_8250_port *up)
254 {
255 	int mctrl;
256 
257 	mctrl = serial_in(up, UART_MCR);
258 
259 	if (up->gpios) {
260 		unsigned int mctrl_gpio = 0;
261 
262 		mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
263 		mctrl |= serial8250_TIOCM_to_MCR(mctrl_gpio);
264 	}
265 
266 	return mctrl;
267 }
268 
269 bool alpha_jensen(void);
270 void alpha_jensen_set_mctrl(struct uart_port *port, unsigned int mctrl);
271 
272 #ifdef CONFIG_SERIAL_8250_PNP
273 int serial8250_pnp_init(void);
274 void serial8250_pnp_exit(void);
275 #else
276 static inline int serial8250_pnp_init(void) { return 0; }
277 static inline void serial8250_pnp_exit(void) { }
278 #endif
279 
280 #ifdef CONFIG_SERIAL_8250_FINTEK
281 int fintek_8250_probe(struct uart_8250_port *uart);
282 #else
283 static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
284 #endif
285 
286 #ifdef CONFIG_ARCH_OMAP1
287 #include <linux/soc/ti/omap1-soc.h>
288 static inline int is_omap1_8250(struct uart_8250_port *pt)
289 {
290 	int res;
291 
292 	switch (pt->port.mapbase) {
293 	case OMAP1_UART1_BASE:
294 	case OMAP1_UART2_BASE:
295 	case OMAP1_UART3_BASE:
296 		res = 1;
297 		break;
298 	default:
299 		res = 0;
300 		break;
301 	}
302 
303 	return res;
304 }
305 
306 static inline int is_omap1510_8250(struct uart_8250_port *pt)
307 {
308 	if (!cpu_is_omap1510())
309 		return 0;
310 
311 	return is_omap1_8250(pt);
312 }
313 #else
314 static inline int is_omap1_8250(struct uart_8250_port *pt)
315 {
316 	return 0;
317 }
318 static inline int is_omap1510_8250(struct uart_8250_port *pt)
319 {
320 	return 0;
321 }
322 #endif
323 
324 #ifdef CONFIG_SERIAL_8250_DMA
325 extern int serial8250_tx_dma(struct uart_8250_port *);
326 extern int serial8250_rx_dma(struct uart_8250_port *);
327 extern void serial8250_rx_dma_flush(struct uart_8250_port *);
328 extern int serial8250_request_dma(struct uart_8250_port *);
329 extern void serial8250_release_dma(struct uart_8250_port *);
330 
331 static inline void serial8250_do_prepare_tx_dma(struct uart_8250_port *p)
332 {
333 	struct uart_8250_dma *dma = p->dma;
334 
335 	if (dma->prepare_tx_dma)
336 		dma->prepare_tx_dma(p);
337 }
338 
339 static inline void serial8250_do_prepare_rx_dma(struct uart_8250_port *p)
340 {
341 	struct uart_8250_dma *dma = p->dma;
342 
343 	if (dma->prepare_rx_dma)
344 		dma->prepare_rx_dma(p);
345 }
346 #else
347 static inline int serial8250_tx_dma(struct uart_8250_port *p)
348 {
349 	return -1;
350 }
351 static inline int serial8250_rx_dma(struct uart_8250_port *p)
352 {
353 	return -1;
354 }
355 static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
356 static inline int serial8250_request_dma(struct uart_8250_port *p)
357 {
358 	return -1;
359 }
360 static inline void serial8250_release_dma(struct uart_8250_port *p) { }
361 #endif
362 
363 static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
364 {
365 	unsigned char status;
366 
367 	status = serial_in(up, 0x04); /* EXCR2 */
368 #define PRESL(x) ((x) & 0x30)
369 	if (PRESL(status) == 0x10) {
370 		/* already in high speed mode */
371 		return 0;
372 	} else {
373 		status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
374 		status |= 0x10;  /* 1.625 divisor for baud_base --> 921600 */
375 		serial_out(up, 0x04, status);
376 	}
377 	return 1;
378 }
379 
380 static inline int serial_index(struct uart_port *port)
381 {
382 	return port->minor - 64;
383 }
384