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