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 
serial_in(struct uart_8250_port * up,int offset)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 
serial_out(struct uart_8250_port * up,int offset,int value)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  *	serial_lsr_in - Read LSR register and preserve flags across reads
128  *	@up:	uart 8250 port
129  *
130  *	Read LSR register and handle saving non-preserved flags across reads.
131  *	The flags that are not preserved across reads are stored into
132  *	up->lsr_saved_flags.
133  *
134  *	Returns LSR value or'ed with the preserved flags (if any).
135  */
serial_lsr_in(struct uart_8250_port * up)136 static inline unsigned int serial_lsr_in(struct uart_8250_port *up)
137 {
138 	unsigned int lsr = up->lsr_saved_flags;
139 
140 	lsr |= serial_in(up, UART_LSR);
141 	up->lsr_saved_flags = lsr & LSR_SAVE_FLAGS;
142 
143 	return lsr;
144 }
145 
146 /*
147  * For the 16C950
148  */
serial_icr_write(struct uart_8250_port * up,int offset,int value)149 static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
150 {
151 	serial_out(up, UART_SCR, offset);
152 	serial_out(up, UART_ICR, value);
153 }
154 
serial_icr_read(struct uart_8250_port * up,int offset)155 static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
156 						   int offset)
157 {
158 	unsigned int value;
159 
160 	serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD);
161 	serial_out(up, UART_SCR, offset);
162 	value = serial_in(up, UART_ICR);
163 	serial_icr_write(up, UART_ACR, up->acr);
164 
165 	return value;
166 }
167 
168 void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
169 
serial_dl_read(struct uart_8250_port * up)170 static inline int serial_dl_read(struct uart_8250_port *up)
171 {
172 	return up->dl_read(up);
173 }
174 
serial_dl_write(struct uart_8250_port * up,int value)175 static inline void serial_dl_write(struct uart_8250_port *up, int value)
176 {
177 	up->dl_write(up, value);
178 }
179 
serial8250_set_THRI(struct uart_8250_port * up)180 static inline bool serial8250_set_THRI(struct uart_8250_port *up)
181 {
182 	if (up->ier & UART_IER_THRI)
183 		return false;
184 	up->ier |= UART_IER_THRI;
185 	serial_out(up, UART_IER, up->ier);
186 	return true;
187 }
188 
serial8250_clear_THRI(struct uart_8250_port * up)189 static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
190 {
191 	if (!(up->ier & UART_IER_THRI))
192 		return false;
193 	up->ier &= ~UART_IER_THRI;
194 	serial_out(up, UART_IER, up->ier);
195 	return true;
196 }
197 
198 struct uart_8250_port *serial8250_get_port(int line);
199 
200 void serial8250_rpm_get(struct uart_8250_port *p);
201 void serial8250_rpm_put(struct uart_8250_port *p);
202 
203 void serial8250_rpm_get_tx(struct uart_8250_port *p);
204 void serial8250_rpm_put_tx(struct uart_8250_port *p);
205 
206 int serial8250_em485_config(struct uart_port *port, struct serial_rs485 *rs485);
207 void serial8250_em485_start_tx(struct uart_8250_port *p);
208 void serial8250_em485_stop_tx(struct uart_8250_port *p);
209 void serial8250_em485_destroy(struct uart_8250_port *p);
210 
211 /* MCR <-> TIOCM conversion */
serial8250_TIOCM_to_MCR(int tiocm)212 static inline int serial8250_TIOCM_to_MCR(int tiocm)
213 {
214 	int mcr = 0;
215 
216 	if (tiocm & TIOCM_RTS)
217 		mcr |= UART_MCR_RTS;
218 	if (tiocm & TIOCM_DTR)
219 		mcr |= UART_MCR_DTR;
220 	if (tiocm & TIOCM_OUT1)
221 		mcr |= UART_MCR_OUT1;
222 	if (tiocm & TIOCM_OUT2)
223 		mcr |= UART_MCR_OUT2;
224 	if (tiocm & TIOCM_LOOP)
225 		mcr |= UART_MCR_LOOP;
226 
227 	return mcr;
228 }
229 
serial8250_MCR_to_TIOCM(int mcr)230 static inline int serial8250_MCR_to_TIOCM(int mcr)
231 {
232 	int tiocm = 0;
233 
234 	if (mcr & UART_MCR_RTS)
235 		tiocm |= TIOCM_RTS;
236 	if (mcr & UART_MCR_DTR)
237 		tiocm |= TIOCM_DTR;
238 	if (mcr & UART_MCR_OUT1)
239 		tiocm |= TIOCM_OUT1;
240 	if (mcr & UART_MCR_OUT2)
241 		tiocm |= TIOCM_OUT2;
242 	if (mcr & UART_MCR_LOOP)
243 		tiocm |= TIOCM_LOOP;
244 
245 	return tiocm;
246 }
247 
248 /* MSR <-> TIOCM conversion */
serial8250_MSR_to_TIOCM(int msr)249 static inline int serial8250_MSR_to_TIOCM(int msr)
250 {
251 	int tiocm = 0;
252 
253 	if (msr & UART_MSR_DCD)
254 		tiocm |= TIOCM_CAR;
255 	if (msr & UART_MSR_RI)
256 		tiocm |= TIOCM_RNG;
257 	if (msr & UART_MSR_DSR)
258 		tiocm |= TIOCM_DSR;
259 	if (msr & UART_MSR_CTS)
260 		tiocm |= TIOCM_CTS;
261 
262 	return tiocm;
263 }
264 
serial8250_out_MCR(struct uart_8250_port * up,int value)265 static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
266 {
267 	serial_out(up, UART_MCR, value);
268 
269 	if (up->gpios)
270 		mctrl_gpio_set(up->gpios, serial8250_MCR_to_TIOCM(value));
271 }
272 
serial8250_in_MCR(struct uart_8250_port * up)273 static inline int serial8250_in_MCR(struct uart_8250_port *up)
274 {
275 	int mctrl;
276 
277 	mctrl = serial_in(up, UART_MCR);
278 
279 	if (up->gpios) {
280 		unsigned int mctrl_gpio = 0;
281 
282 		mctrl_gpio = mctrl_gpio_get_outputs(up->gpios, &mctrl_gpio);
283 		mctrl |= serial8250_TIOCM_to_MCR(mctrl_gpio);
284 	}
285 
286 	return mctrl;
287 }
288 
289 bool alpha_jensen(void);
290 void alpha_jensen_set_mctrl(struct uart_port *port, unsigned int mctrl);
291 
292 #ifdef CONFIG_SERIAL_8250_PNP
293 int serial8250_pnp_init(void);
294 void serial8250_pnp_exit(void);
295 #else
serial8250_pnp_init(void)296 static inline int serial8250_pnp_init(void) { return 0; }
serial8250_pnp_exit(void)297 static inline void serial8250_pnp_exit(void) { }
298 #endif
299 
300 #ifdef CONFIG_SERIAL_8250_FINTEK
301 int fintek_8250_probe(struct uart_8250_port *uart);
302 #else
fintek_8250_probe(struct uart_8250_port * uart)303 static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
304 #endif
305 
306 #ifdef CONFIG_ARCH_OMAP1
307 #include <linux/soc/ti/omap1-soc.h>
is_omap1_8250(struct uart_8250_port * pt)308 static inline int is_omap1_8250(struct uart_8250_port *pt)
309 {
310 	int res;
311 
312 	switch (pt->port.mapbase) {
313 	case OMAP1_UART1_BASE:
314 	case OMAP1_UART2_BASE:
315 	case OMAP1_UART3_BASE:
316 		res = 1;
317 		break;
318 	default:
319 		res = 0;
320 		break;
321 	}
322 
323 	return res;
324 }
325 
is_omap1510_8250(struct uart_8250_port * pt)326 static inline int is_omap1510_8250(struct uart_8250_port *pt)
327 {
328 	if (!cpu_is_omap1510())
329 		return 0;
330 
331 	return is_omap1_8250(pt);
332 }
333 #else
is_omap1_8250(struct uart_8250_port * pt)334 static inline int is_omap1_8250(struct uart_8250_port *pt)
335 {
336 	return 0;
337 }
is_omap1510_8250(struct uart_8250_port * pt)338 static inline int is_omap1510_8250(struct uart_8250_port *pt)
339 {
340 	return 0;
341 }
342 #endif
343 
344 #ifdef CONFIG_SERIAL_8250_DMA
345 extern int serial8250_tx_dma(struct uart_8250_port *);
346 extern int serial8250_rx_dma(struct uart_8250_port *);
347 extern void serial8250_rx_dma_flush(struct uart_8250_port *);
348 extern int serial8250_request_dma(struct uart_8250_port *);
349 extern void serial8250_release_dma(struct uart_8250_port *);
350 
serial8250_do_prepare_tx_dma(struct uart_8250_port * p)351 static inline void serial8250_do_prepare_tx_dma(struct uart_8250_port *p)
352 {
353 	struct uart_8250_dma *dma = p->dma;
354 
355 	if (dma->prepare_tx_dma)
356 		dma->prepare_tx_dma(p);
357 }
358 
serial8250_do_prepare_rx_dma(struct uart_8250_port * p)359 static inline void serial8250_do_prepare_rx_dma(struct uart_8250_port *p)
360 {
361 	struct uart_8250_dma *dma = p->dma;
362 
363 	if (dma->prepare_rx_dma)
364 		dma->prepare_rx_dma(p);
365 }
366 #else
serial8250_tx_dma(struct uart_8250_port * p)367 static inline int serial8250_tx_dma(struct uart_8250_port *p)
368 {
369 	return -1;
370 }
serial8250_rx_dma(struct uart_8250_port * p)371 static inline int serial8250_rx_dma(struct uart_8250_port *p)
372 {
373 	return -1;
374 }
serial8250_rx_dma_flush(struct uart_8250_port * p)375 static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
serial8250_request_dma(struct uart_8250_port * p)376 static inline int serial8250_request_dma(struct uart_8250_port *p)
377 {
378 	return -1;
379 }
serial8250_release_dma(struct uart_8250_port * p)380 static inline void serial8250_release_dma(struct uart_8250_port *p) { }
381 #endif
382 
ns16550a_goto_highspeed(struct uart_8250_port * up)383 static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
384 {
385 	unsigned char status;
386 
387 	status = serial_in(up, 0x04); /* EXCR2 */
388 #define PRESL(x) ((x) & 0x30)
389 	if (PRESL(status) == 0x10) {
390 		/* already in high speed mode */
391 		return 0;
392 	} else {
393 		status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
394 		status |= 0x10;  /* 1.625 divisor for baud_base --> 921600 */
395 		serial_out(up, 0x04, status);
396 	}
397 	return 1;
398 }
399 
serial_index(struct uart_port * port)400 static inline int serial_index(struct uart_port *port)
401 {
402 	return port->minor - 64;
403 }
404