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_core.h>
13#include <linux/dmaengine.h>
14
15#include "../serial_mctrl_gpio.h"
16
17struct 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
56struct 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
67struct 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_TXRACE BIT(5) /* UART Tx fails to set remote DR */
95
96/* Module parameters */
97#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
98
99extern unsigned int nr_uarts;
100
101#ifdef CONFIG_SERIAL_8250_SHARE_IRQ
102#define SERIAL8250_SHARE_IRQS 1
103#else
104#define SERIAL8250_SHARE_IRQS 0
105#endif
106
107extern unsigned int share_irqs;
108extern unsigned int skip_txen_test;
109
110#define SERIAL8250_PORT_FLAGS(_base, _irq, _flags) \
111 { \
112 .iobase = _base, \
113 .irq = _irq, \
114 .uartclk = 1843200, \
115 .iotype = UPIO_PORT, \
116 .flags = UPF_BOOT_AUTOCONF | (_flags), \
117 }
118
119#define SERIAL8250_PORT(_base, _irq) SERIAL8250_PORT_FLAGS(_base, _irq, 0)
120
121extern struct uart_driver serial8250_reg;
122void serial8250_register_ports(struct uart_driver *drv, struct device *dev);
123
124/* Legacy ISA bus related APIs */
125typedef void (*serial8250_isa_config_fn)(int, struct uart_port *, u32 *);
126extern serial8250_isa_config_fn serial8250_isa_config;
127
128void serial8250_isa_init_ports(void);
129
130extern struct platform_device *serial8250_isa_devs;
131
132extern const struct uart_ops *univ8250_port_base_ops;
133extern struct uart_ops univ8250_port_ops;
134
135static inline int serial_in(struct uart_8250_port *up, int offset)
136{
137 return up->port.serial_in(&up->port, offset);
138}
139
140static inline void serial_out(struct uart_8250_port *up, int offset, int value)
141{
142 up->port.serial_out(&up->port, offset, value);
143}
144
145/**
146 * serial_lsr_in - Read LSR register and preserve flags across reads
147 * @up: uart 8250 port
148 *
149 * Read LSR register and handle saving non-preserved flags across reads.
150 * The flags that are not preserved across reads are stored into
151 * up->lsr_saved_flags.
152 *
153 * Returns LSR value or'ed with the preserved flags (if any).
154 */
155static inline u16 serial_lsr_in(struct uart_8250_port *up)
156{
157 u16 lsr = up->lsr_saved_flags;
158
159 lsr |= serial_in(up, UART_LSR);
160 up->lsr_saved_flags = lsr & up->lsr_save_mask;
161
162 return lsr;
163}
164
165/*
166 * For the 16C950
167 */
168static void serial_icr_write(struct uart_8250_port *up, int offset, int value)
169{
170 serial_out(up, UART_SCR, value: offset);
171 serial_out(up, UART_ICR, value);
172}
173
174static unsigned int __maybe_unused serial_icr_read(struct uart_8250_port *up,
175 int offset)
176{
177 unsigned int value;
178
179 serial_icr_write(up, UART_ACR, value: up->acr | UART_ACR_ICRRD);
180 serial_out(up, UART_SCR, value: offset);
181 value = serial_in(up, UART_ICR);
182 serial_icr_write(up, UART_ACR, value: up->acr);
183
184 return value;
185}
186
187void serial8250_clear_and_reinit_fifos(struct uart_8250_port *p);
188
189void serial8250_rpm_get(struct uart_8250_port *p);
190void serial8250_rpm_put(struct uart_8250_port *p);
191DEFINE_GUARD(serial8250_rpm, struct uart_8250_port *,
192 serial8250_rpm_get(_T), serial8250_rpm_put(_T));
193
194static inline u32 serial_dl_read(struct uart_8250_port *up)
195{
196 return up->dl_read(up);
197}
198
199static inline void serial_dl_write(struct uart_8250_port *up, u32 value)
200{
201 up->dl_write(up, value);
202}
203
204static inline bool serial8250_set_THRI(struct uart_8250_port *up)
205{
206 /* Port locked to synchronize UART_IER access against the console. */
207 lockdep_assert_held_once(&up->port.lock);
208
209 if (up->ier & UART_IER_THRI)
210 return false;
211 up->ier |= UART_IER_THRI;
212 serial_out(up, UART_IER, value: up->ier);
213 return true;
214}
215
216static inline bool serial8250_clear_THRI(struct uart_8250_port *up)
217{
218 /* Port locked to synchronize UART_IER access against the console. */
219 lockdep_assert_held_once(&up->port.lock);
220
221 if (!(up->ier & UART_IER_THRI))
222 return false;
223 up->ier &= ~UART_IER_THRI;
224 serial_out(up, UART_IER, value: up->ier);
225 return true;
226}
227
228struct uart_8250_port *serial8250_setup_port(int index);
229struct uart_8250_port *serial8250_get_port(int line);
230
231int serial8250_em485_config(struct uart_port *port, struct ktermios *termios,
232 struct serial_rs485 *rs485);
233void serial8250_em485_start_tx(struct uart_8250_port *p, bool toggle_ier);
234void serial8250_em485_stop_tx(struct uart_8250_port *p, bool toggle_ier);
235void serial8250_em485_destroy(struct uart_8250_port *p);
236extern struct serial_rs485 serial8250_em485_supported;
237
238/* MCR <-> TIOCM conversion */
239static inline int serial8250_TIOCM_to_MCR(int tiocm)
240{
241 int mcr = 0;
242
243 if (tiocm & TIOCM_RTS)
244 mcr |= UART_MCR_RTS;
245 if (tiocm & TIOCM_DTR)
246 mcr |= UART_MCR_DTR;
247 if (tiocm & TIOCM_OUT1)
248 mcr |= UART_MCR_OUT1;
249 if (tiocm & TIOCM_OUT2)
250 mcr |= UART_MCR_OUT2;
251 if (tiocm & TIOCM_LOOP)
252 mcr |= UART_MCR_LOOP;
253
254 return mcr;
255}
256
257static inline int serial8250_MCR_to_TIOCM(int mcr)
258{
259 int tiocm = 0;
260
261 if (mcr & UART_MCR_RTS)
262 tiocm |= TIOCM_RTS;
263 if (mcr & UART_MCR_DTR)
264 tiocm |= TIOCM_DTR;
265 if (mcr & UART_MCR_OUT1)
266 tiocm |= TIOCM_OUT1;
267 if (mcr & UART_MCR_OUT2)
268 tiocm |= TIOCM_OUT2;
269 if (mcr & UART_MCR_LOOP)
270 tiocm |= TIOCM_LOOP;
271
272 return tiocm;
273}
274
275/* MSR <-> TIOCM conversion */
276static inline int serial8250_MSR_to_TIOCM(int msr)
277{
278 int tiocm = 0;
279
280 if (msr & UART_MSR_DCD)
281 tiocm |= TIOCM_CAR;
282 if (msr & UART_MSR_RI)
283 tiocm |= TIOCM_RNG;
284 if (msr & UART_MSR_DSR)
285 tiocm |= TIOCM_DSR;
286 if (msr & UART_MSR_CTS)
287 tiocm |= TIOCM_CTS;
288
289 return tiocm;
290}
291
292static inline void serial8250_out_MCR(struct uart_8250_port *up, int value)
293{
294 serial_out(up, UART_MCR, value);
295
296 if (up->gpios)
297 mctrl_gpio_set(gpios: up->gpios, mctrl: serial8250_MCR_to_TIOCM(mcr: value));
298}
299
300static inline int serial8250_in_MCR(struct uart_8250_port *up)
301{
302 int mctrl;
303
304 mctrl = serial_in(up, UART_MCR);
305
306 if (up->gpios) {
307 unsigned int mctrl_gpio = 0;
308
309 mctrl_gpio = mctrl_gpio_get_outputs(gpios: up->gpios, mctrl: &mctrl_gpio);
310 mctrl |= serial8250_TIOCM_to_MCR(tiocm: mctrl_gpio);
311 }
312
313 return mctrl;
314}
315
316#ifdef CONFIG_SERIAL_8250_PNP
317int serial8250_pnp_init(void);
318void serial8250_pnp_exit(void);
319#else
320static inline int serial8250_pnp_init(void) { return 0; }
321static inline void serial8250_pnp_exit(void) { }
322#endif
323
324#ifdef CONFIG_SERIAL_8250_RSA
325void univ8250_rsa_support(struct uart_ops *ops);
326void rsa_enable(struct uart_8250_port *up);
327void rsa_disable(struct uart_8250_port *up);
328void rsa_autoconfig(struct uart_8250_port *up);
329void rsa_reset(struct uart_8250_port *up);
330#else
331static inline void univ8250_rsa_support(struct uart_ops *ops) { }
332static inline void rsa_enable(struct uart_8250_port *up) {}
333static inline void rsa_disable(struct uart_8250_port *up) {}
334static inline void rsa_autoconfig(struct uart_8250_port *up) {}
335static inline void rsa_reset(struct uart_8250_port *up) {}
336#endif
337
338#ifdef CONFIG_SERIAL_8250_FINTEK
339int fintek_8250_probe(struct uart_8250_port *uart);
340#else
341static inline int fintek_8250_probe(struct uart_8250_port *uart) { return 0; }
342#endif
343
344#ifdef CONFIG_ARCH_OMAP1
345#include <linux/soc/ti/omap1-soc.h>
346static inline int is_omap1_8250(struct uart_8250_port *pt)
347{
348 int res;
349
350 switch (pt->port.mapbase) {
351 case OMAP1_UART1_BASE:
352 case OMAP1_UART2_BASE:
353 case OMAP1_UART3_BASE:
354 res = 1;
355 break;
356 default:
357 res = 0;
358 break;
359 }
360
361 return res;
362}
363
364static inline int is_omap1510_8250(struct uart_8250_port *pt)
365{
366 if (!cpu_is_omap1510())
367 return 0;
368
369 return is_omap1_8250(pt);
370}
371#else
372static inline int is_omap1_8250(struct uart_8250_port *pt)
373{
374 return 0;
375}
376static inline int is_omap1510_8250(struct uart_8250_port *pt)
377{
378 return 0;
379}
380#endif
381
382#ifdef CONFIG_SERIAL_8250_DMA
383extern int serial8250_tx_dma(struct uart_8250_port *);
384extern void serial8250_tx_dma_flush(struct uart_8250_port *);
385extern int serial8250_rx_dma(struct uart_8250_port *);
386extern void serial8250_rx_dma_flush(struct uart_8250_port *);
387extern int serial8250_request_dma(struct uart_8250_port *);
388extern void serial8250_release_dma(struct uart_8250_port *);
389
390static inline void serial8250_do_prepare_tx_dma(struct uart_8250_port *p)
391{
392 struct uart_8250_dma *dma = p->dma;
393
394 if (dma->prepare_tx_dma)
395 dma->prepare_tx_dma(p);
396}
397
398static inline void serial8250_do_prepare_rx_dma(struct uart_8250_port *p)
399{
400 struct uart_8250_dma *dma = p->dma;
401
402 if (dma->prepare_rx_dma)
403 dma->prepare_rx_dma(p);
404}
405
406static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
407{
408 struct uart_8250_dma *dma = p->dma;
409
410 return dma && dma->tx_running;
411}
412#else
413static inline int serial8250_tx_dma(struct uart_8250_port *p)
414{
415 return -1;
416}
417static inline void serial8250_tx_dma_flush(struct uart_8250_port *p) { }
418static inline int serial8250_rx_dma(struct uart_8250_port *p)
419{
420 return -1;
421}
422static inline void serial8250_rx_dma_flush(struct uart_8250_port *p) { }
423static inline int serial8250_request_dma(struct uart_8250_port *p)
424{
425 return -1;
426}
427static inline void serial8250_release_dma(struct uart_8250_port *p) { }
428
429static inline bool serial8250_tx_dma_running(struct uart_8250_port *p)
430{
431 return false;
432}
433#endif
434
435static inline int ns16550a_goto_highspeed(struct uart_8250_port *up)
436{
437 unsigned char status;
438
439 status = serial_in(up, offset: 0x04); /* EXCR2 */
440#define PRESL(x) ((x) & 0x30)
441 if (PRESL(status) == 0x10) {
442 /* already in high speed mode */
443 return 0;
444 } else {
445 status &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */
446 status |= 0x10; /* 1.625 divisor for baud_base --> 921600 */
447 serial_out(up, offset: 0x04, value: status);
448 }
449 return 1;
450}
451
452static inline int serial_index(struct uart_port *port)
453{
454 return port->minor - 64;
455}
456