xref: /openbmc/linux/drivers/tty/serial/8250/8250_dwlib.c (revision 5ff33917faca773e051d48ef37a6e95b2f8e4c5a)
1136e0ab9SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0+
2136e0ab9SAndy Shevchenko /* Synopsys DesignWare 8250 library. */
3136e0ab9SAndy Shevchenko 
4136e0ab9SAndy Shevchenko #include <linux/bitops.h>
5642aa760SIlpo Järvinen #include <linux/bitfield.h>
6136e0ab9SAndy Shevchenko #include <linux/device.h>
7136e0ab9SAndy Shevchenko #include <linux/kernel.h>
8642aa760SIlpo Järvinen #include <linux/property.h>
9136e0ab9SAndy Shevchenko #include <linux/serial_8250.h>
10136e0ab9SAndy Shevchenko #include <linux/serial_core.h>
11136e0ab9SAndy Shevchenko 
12136e0ab9SAndy Shevchenko #include "8250_dwlib.h"
13136e0ab9SAndy Shevchenko 
14136e0ab9SAndy Shevchenko /* Offsets for the DesignWare specific registers */
15642aa760SIlpo Järvinen #define DW_UART_TCR	0xac /* Transceiver Control Register (RS485) */
16642aa760SIlpo Järvinen #define DW_UART_DE_EN	0xb0 /* Driver Output Enable Register */
17642aa760SIlpo Järvinen #define DW_UART_RE_EN	0xb4 /* Receiver Output Enable Register */
18136e0ab9SAndy Shevchenko #define DW_UART_DLF	0xc0 /* Divisor Latch Fraction Register */
19136e0ab9SAndy Shevchenko #define DW_UART_CPR	0xf4 /* Component Parameter Register */
20136e0ab9SAndy Shevchenko #define DW_UART_UCV	0xf8 /* UART Component Version */
21136e0ab9SAndy Shevchenko 
22642aa760SIlpo Järvinen /* Transceiver Control Register bits */
23642aa760SIlpo Järvinen #define DW_UART_TCR_RS485_EN		BIT(0)
24642aa760SIlpo Järvinen #define DW_UART_TCR_RE_POL		BIT(1)
25642aa760SIlpo Järvinen #define DW_UART_TCR_DE_POL		BIT(2)
26642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE		GENMASK(4, 3)
27642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_DE_DURING_RE	FIELD_PREP(DW_UART_TCR_XFER_MODE, 0)
28642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_SW_DE_OR_RE	FIELD_PREP(DW_UART_TCR_XFER_MODE, 1)
29642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_DE_OR_RE		FIELD_PREP(DW_UART_TCR_XFER_MODE, 2)
30642aa760SIlpo Järvinen 
31136e0ab9SAndy Shevchenko /* Component Parameter Register bits */
32136e0ab9SAndy Shevchenko #define DW_UART_CPR_ABP_DATA_WIDTH	(3 << 0)
33136e0ab9SAndy Shevchenko #define DW_UART_CPR_AFCE_MODE		(1 << 4)
34136e0ab9SAndy Shevchenko #define DW_UART_CPR_THRE_MODE		(1 << 5)
35136e0ab9SAndy Shevchenko #define DW_UART_CPR_SIR_MODE		(1 << 6)
36136e0ab9SAndy Shevchenko #define DW_UART_CPR_SIR_LP_MODE		(1 << 7)
37136e0ab9SAndy Shevchenko #define DW_UART_CPR_ADDITIONAL_FEATURES	(1 << 8)
38136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_ACCESS		(1 << 9)
39136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_STAT		(1 << 10)
40136e0ab9SAndy Shevchenko #define DW_UART_CPR_SHADOW		(1 << 11)
41136e0ab9SAndy Shevchenko #define DW_UART_CPR_ENCODED_PARMS	(1 << 12)
42136e0ab9SAndy Shevchenko #define DW_UART_CPR_DMA_EXTRA		(1 << 13)
43136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_MODE		(0xff << 16)
44136e0ab9SAndy Shevchenko 
45136e0ab9SAndy Shevchenko /* Helper for FIFO size calculation */
46136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_SIZE(a)	(((a >> 16) & 0xff) * 16)
47136e0ab9SAndy Shevchenko 
48136e0ab9SAndy Shevchenko /*
49136e0ab9SAndy Shevchenko  * divisor = div(I) + div(F)
50136e0ab9SAndy Shevchenko  * "I" means integer, "F" means fractional
51136e0ab9SAndy Shevchenko  * quot = div(I) = clk / (16 * baud)
52136e0ab9SAndy Shevchenko  * frac = div(F) * 2^dlf_size
53136e0ab9SAndy Shevchenko  *
54136e0ab9SAndy Shevchenko  * let rem = clk % (16 * baud)
55136e0ab9SAndy Shevchenko  * we have: div(F) * (16 * baud) = rem
56136e0ab9SAndy Shevchenko  * so frac = 2^dlf_size * rem / (16 * baud) = (rem << dlf_size) / (16 * baud)
57136e0ab9SAndy Shevchenko  */
58136e0ab9SAndy Shevchenko static unsigned int dw8250_get_divisor(struct uart_port *p, unsigned int baud,
59136e0ab9SAndy Shevchenko 				       unsigned int *frac)
60136e0ab9SAndy Shevchenko {
61136e0ab9SAndy Shevchenko 	unsigned int quot, rem, base_baud = baud * 16;
62136e0ab9SAndy Shevchenko 	struct dw8250_port_data *d = p->private_data;
63136e0ab9SAndy Shevchenko 
64136e0ab9SAndy Shevchenko 	quot = p->uartclk / base_baud;
65136e0ab9SAndy Shevchenko 	rem = p->uartclk % base_baud;
66136e0ab9SAndy Shevchenko 	*frac = DIV_ROUND_CLOSEST(rem << d->dlf_size, base_baud);
67136e0ab9SAndy Shevchenko 
68136e0ab9SAndy Shevchenko 	return quot;
69136e0ab9SAndy Shevchenko }
70136e0ab9SAndy Shevchenko 
71136e0ab9SAndy Shevchenko static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
72136e0ab9SAndy Shevchenko 			       unsigned int quot, unsigned int quot_frac)
73136e0ab9SAndy Shevchenko {
74136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, quot_frac);
75136e0ab9SAndy Shevchenko 	serial8250_do_set_divisor(p, baud, quot, quot_frac);
76136e0ab9SAndy Shevchenko }
77136e0ab9SAndy Shevchenko 
787c4fc082SAndy Shevchenko void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios, struct ktermios *old)
797c4fc082SAndy Shevchenko {
807c4fc082SAndy Shevchenko 	p->status &= ~UPSTAT_AUTOCTS;
817c4fc082SAndy Shevchenko 	if (termios->c_cflag & CRTSCTS)
827c4fc082SAndy Shevchenko 		p->status |= UPSTAT_AUTOCTS;
837c4fc082SAndy Shevchenko 
847c4fc082SAndy Shevchenko 	serial8250_do_set_termios(p, termios, old);
857c4fc082SAndy Shevchenko }
867c4fc082SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_do_set_termios);
877c4fc082SAndy Shevchenko 
88642aa760SIlpo Järvinen static int dw8250_rs485_config(struct uart_port *p, struct serial_rs485 *rs485)
89642aa760SIlpo Järvinen {
90642aa760SIlpo Järvinen 	u32 tcr;
91642aa760SIlpo Järvinen 
92642aa760SIlpo Järvinen 	tcr = dw8250_readl_ext(p, DW_UART_TCR);
93642aa760SIlpo Järvinen 	tcr &= ~DW_UART_TCR_XFER_MODE;
94642aa760SIlpo Järvinen 
95642aa760SIlpo Järvinen 	if (rs485->flags & SER_RS485_ENABLED) {
96642aa760SIlpo Järvinen 		/* Clear unsupported flags. */
97642aa760SIlpo Järvinen 		rs485->flags &= SER_RS485_ENABLED | SER_RS485_RX_DURING_TX |
98642aa760SIlpo Järvinen 				SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND;
99642aa760SIlpo Järvinen 		tcr |= DW_UART_TCR_RS485_EN;
100642aa760SIlpo Järvinen 
101642aa760SIlpo Järvinen 		if (rs485->flags & SER_RS485_RX_DURING_TX) {
102642aa760SIlpo Järvinen 			tcr |= DW_UART_TCR_XFER_MODE_DE_DURING_RE;
103642aa760SIlpo Järvinen 		} else {
104642aa760SIlpo Järvinen 			/* HW does not support same DE level for tx and rx */
105642aa760SIlpo Järvinen 			if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
106642aa760SIlpo Järvinen 			    !(rs485->flags & SER_RS485_RTS_AFTER_SEND))
107642aa760SIlpo Järvinen 				return -EINVAL;
108642aa760SIlpo Järvinen 
109642aa760SIlpo Järvinen 			tcr |= DW_UART_TCR_XFER_MODE_DE_OR_RE;
110642aa760SIlpo Järvinen 		}
111642aa760SIlpo Järvinen 		dw8250_writel_ext(p, DW_UART_DE_EN, 1);
112642aa760SIlpo Järvinen 		dw8250_writel_ext(p, DW_UART_RE_EN, 1);
113642aa760SIlpo Järvinen 	} else {
114642aa760SIlpo Järvinen 		rs485->flags = 0;
115642aa760SIlpo Järvinen 
116642aa760SIlpo Järvinen 		tcr &= ~DW_UART_TCR_RS485_EN;
117642aa760SIlpo Järvinen 	}
118642aa760SIlpo Järvinen 
119642aa760SIlpo Järvinen 	/* Reset to default polarity */
120642aa760SIlpo Järvinen 	tcr |= DW_UART_TCR_DE_POL;
121642aa760SIlpo Järvinen 	tcr &= ~DW_UART_TCR_RE_POL;
122642aa760SIlpo Järvinen 
123642aa760SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_RTS_ON_SEND))
124642aa760SIlpo Järvinen 		tcr &= ~DW_UART_TCR_DE_POL;
125642aa760SIlpo Järvinen 	if (device_property_read_bool(p->dev, "rs485-rx-active-high"))
126642aa760SIlpo Järvinen 		tcr |= DW_UART_TCR_RE_POL;
127642aa760SIlpo Järvinen 
128642aa760SIlpo Järvinen 	dw8250_writel_ext(p, DW_UART_TCR, tcr);
129642aa760SIlpo Järvinen 
130642aa760SIlpo Järvinen 	rs485->delay_rts_before_send = 0;
131642aa760SIlpo Järvinen 	rs485->delay_rts_after_send = 0;
132642aa760SIlpo Järvinen 
133642aa760SIlpo Järvinen 	p->rs485 = *rs485;
134642aa760SIlpo Järvinen 
135642aa760SIlpo Järvinen 	return 0;
136642aa760SIlpo Järvinen }
137642aa760SIlpo Järvinen 
138642aa760SIlpo Järvinen /*
139642aa760SIlpo Järvinen  * Tests if RE_EN register can have non-zero value to see if RS-485 HW support
140642aa760SIlpo Järvinen  * is present.
141642aa760SIlpo Järvinen  */
142642aa760SIlpo Järvinen static bool dw8250_detect_rs485_hw(struct uart_port *p)
143642aa760SIlpo Järvinen {
144642aa760SIlpo Järvinen 	u32 reg;
145642aa760SIlpo Järvinen 
146642aa760SIlpo Järvinen 	dw8250_writel_ext(p, DW_UART_RE_EN, 1);
147642aa760SIlpo Järvinen 	reg = dw8250_readl_ext(p, DW_UART_RE_EN);
148642aa760SIlpo Järvinen 	dw8250_writel_ext(p, DW_UART_RE_EN, 0);
149642aa760SIlpo Järvinen 	return reg;
150642aa760SIlpo Järvinen }
151642aa760SIlpo Järvinen 
152136e0ab9SAndy Shevchenko void dw8250_setup_port(struct uart_port *p)
153136e0ab9SAndy Shevchenko {
154593dea00SMiquel Raynal 	struct dw8250_port_data *pd = p->private_data;
155593dea00SMiquel Raynal 	struct dw8250_data *data = to_dw8250_data(pd);
156136e0ab9SAndy Shevchenko 	struct uart_8250_port *up = up_to_u8250p(p);
157136e0ab9SAndy Shevchenko 	u32 reg;
158136e0ab9SAndy Shevchenko 
159642aa760SIlpo Järvinen 	pd->hw_rs485_support = dw8250_detect_rs485_hw(p);
160*5ff33917SIlpo Järvinen 	if (pd->hw_rs485_support) {
161642aa760SIlpo Järvinen 		p->rs485_config = dw8250_rs485_config;
162*5ff33917SIlpo Järvinen 	} else {
163*5ff33917SIlpo Järvinen 		p->rs485_config = serial8250_em485_config;
164*5ff33917SIlpo Järvinen 		up->rs485_start_tx = serial8250_em485_start_tx;
165*5ff33917SIlpo Järvinen 		up->rs485_stop_tx = serial8250_em485_stop_tx;
166*5ff33917SIlpo Järvinen 	}
167*5ff33917SIlpo Järvinen 	up->capabilities |= UART_CAP_NOTEMT;
168642aa760SIlpo Järvinen 
169136e0ab9SAndy Shevchenko 	/*
170136e0ab9SAndy Shevchenko 	 * If the Component Version Register returns zero, we know that
171136e0ab9SAndy Shevchenko 	 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
172136e0ab9SAndy Shevchenko 	 */
173136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_UCV);
174136e0ab9SAndy Shevchenko 	if (!reg)
175136e0ab9SAndy Shevchenko 		return;
176136e0ab9SAndy Shevchenko 
177136e0ab9SAndy Shevchenko 	dev_dbg(p->dev, "Designware UART version %c.%c%c\n",
178136e0ab9SAndy Shevchenko 		(reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
179136e0ab9SAndy Shevchenko 
180136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, ~0U);
181136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_DLF);
182136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, 0);
183136e0ab9SAndy Shevchenko 
184136e0ab9SAndy Shevchenko 	if (reg) {
185593dea00SMiquel Raynal 		pd->dlf_size = fls(reg);
186136e0ab9SAndy Shevchenko 		p->get_divisor = dw8250_get_divisor;
187136e0ab9SAndy Shevchenko 		p->set_divisor = dw8250_set_divisor;
188136e0ab9SAndy Shevchenko 	}
189136e0ab9SAndy Shevchenko 
190136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_CPR);
191593dea00SMiquel Raynal 	if (!reg) {
192593dea00SMiquel Raynal 		reg = data->pdata->cpr_val;
193593dea00SMiquel Raynal 		dev_dbg(p->dev, "CPR is not available, using 0x%08x instead\n", reg);
194593dea00SMiquel Raynal 	}
195136e0ab9SAndy Shevchenko 	if (!reg)
196136e0ab9SAndy Shevchenko 		return;
197136e0ab9SAndy Shevchenko 
198136e0ab9SAndy Shevchenko 	/* Select the type based on FIFO */
199136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_FIFO_MODE) {
200136e0ab9SAndy Shevchenko 		p->type = PORT_16550A;
201136e0ab9SAndy Shevchenko 		p->flags |= UPF_FIXED_TYPE;
202136e0ab9SAndy Shevchenko 		p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
203*5ff33917SIlpo Järvinen 		up->capabilities = UART_CAP_FIFO | UART_CAP_NOTEMT;
204136e0ab9SAndy Shevchenko 	}
205136e0ab9SAndy Shevchenko 
206136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_AFCE_MODE)
207136e0ab9SAndy Shevchenko 		up->capabilities |= UART_CAP_AFE;
208136e0ab9SAndy Shevchenko 
209136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_SIR_MODE)
210136e0ab9SAndy Shevchenko 		up->capabilities |= UART_CAP_IRDA;
211136e0ab9SAndy Shevchenko }
212136e0ab9SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_setup_port);
213