xref: /openbmc/linux/drivers/tty/serial/8250/8250_dwlib.c (revision 642aa7603e95d0fdfc3e3fb8482561e89ca92a33)
1136e0ab9SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0+
2136e0ab9SAndy Shevchenko /* Synopsys DesignWare 8250 library. */
3136e0ab9SAndy Shevchenko 
4136e0ab9SAndy Shevchenko #include <linux/bitops.h>
5*642aa760SIlpo Järvinen #include <linux/bitfield.h>
6136e0ab9SAndy Shevchenko #include <linux/device.h>
7136e0ab9SAndy Shevchenko #include <linux/kernel.h>
8*642aa760SIlpo 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 */
15*642aa760SIlpo Järvinen #define DW_UART_TCR	0xac /* Transceiver Control Register (RS485) */
16*642aa760SIlpo Järvinen #define DW_UART_DE_EN	0xb0 /* Driver Output Enable Register */
17*642aa760SIlpo 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 
22*642aa760SIlpo Järvinen /* Transceiver Control Register bits */
23*642aa760SIlpo Järvinen #define DW_UART_TCR_RS485_EN		BIT(0)
24*642aa760SIlpo Järvinen #define DW_UART_TCR_RE_POL		BIT(1)
25*642aa760SIlpo Järvinen #define DW_UART_TCR_DE_POL		BIT(2)
26*642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE		GENMASK(4, 3)
27*642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_DE_DURING_RE	FIELD_PREP(DW_UART_TCR_XFER_MODE, 0)
28*642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_SW_DE_OR_RE	FIELD_PREP(DW_UART_TCR_XFER_MODE, 1)
29*642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_DE_OR_RE		FIELD_PREP(DW_UART_TCR_XFER_MODE, 2)
30*642aa760SIlpo 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 
88*642aa760SIlpo Järvinen static int dw8250_rs485_config(struct uart_port *p, struct serial_rs485 *rs485)
89*642aa760SIlpo Järvinen {
90*642aa760SIlpo Järvinen 	u32 tcr;
91*642aa760SIlpo Järvinen 
92*642aa760SIlpo Järvinen 	tcr = dw8250_readl_ext(p, DW_UART_TCR);
93*642aa760SIlpo Järvinen 	tcr &= ~DW_UART_TCR_XFER_MODE;
94*642aa760SIlpo Järvinen 
95*642aa760SIlpo Järvinen 	if (rs485->flags & SER_RS485_ENABLED) {
96*642aa760SIlpo Järvinen 		/* Clear unsupported flags. */
97*642aa760SIlpo Järvinen 		rs485->flags &= SER_RS485_ENABLED | SER_RS485_RX_DURING_TX |
98*642aa760SIlpo Järvinen 				SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND;
99*642aa760SIlpo Järvinen 		tcr |= DW_UART_TCR_RS485_EN;
100*642aa760SIlpo Järvinen 
101*642aa760SIlpo Järvinen 		if (rs485->flags & SER_RS485_RX_DURING_TX) {
102*642aa760SIlpo Järvinen 			tcr |= DW_UART_TCR_XFER_MODE_DE_DURING_RE;
103*642aa760SIlpo Järvinen 		} else {
104*642aa760SIlpo Järvinen 			/* HW does not support same DE level for tx and rx */
105*642aa760SIlpo Järvinen 			if (!(rs485->flags & SER_RS485_RTS_ON_SEND) ==
106*642aa760SIlpo Järvinen 			    !(rs485->flags & SER_RS485_RTS_AFTER_SEND))
107*642aa760SIlpo Järvinen 				return -EINVAL;
108*642aa760SIlpo Järvinen 
109*642aa760SIlpo Järvinen 			tcr |= DW_UART_TCR_XFER_MODE_DE_OR_RE;
110*642aa760SIlpo Järvinen 		}
111*642aa760SIlpo Järvinen 		dw8250_writel_ext(p, DW_UART_DE_EN, 1);
112*642aa760SIlpo Järvinen 		dw8250_writel_ext(p, DW_UART_RE_EN, 1);
113*642aa760SIlpo Järvinen 	} else {
114*642aa760SIlpo Järvinen 		rs485->flags = 0;
115*642aa760SIlpo Järvinen 
116*642aa760SIlpo Järvinen 		tcr &= ~DW_UART_TCR_RS485_EN;
117*642aa760SIlpo Järvinen 	}
118*642aa760SIlpo Järvinen 
119*642aa760SIlpo Järvinen 	/* Reset to default polarity */
120*642aa760SIlpo Järvinen 	tcr |= DW_UART_TCR_DE_POL;
121*642aa760SIlpo Järvinen 	tcr &= ~DW_UART_TCR_RE_POL;
122*642aa760SIlpo Järvinen 
123*642aa760SIlpo Järvinen 	if (!(rs485->flags & SER_RS485_RTS_ON_SEND))
124*642aa760SIlpo Järvinen 		tcr &= ~DW_UART_TCR_DE_POL;
125*642aa760SIlpo Järvinen 	if (device_property_read_bool(p->dev, "rs485-rx-active-high"))
126*642aa760SIlpo Järvinen 		tcr |= DW_UART_TCR_RE_POL;
127*642aa760SIlpo Järvinen 
128*642aa760SIlpo Järvinen 	dw8250_writel_ext(p, DW_UART_TCR, tcr);
129*642aa760SIlpo Järvinen 
130*642aa760SIlpo Järvinen 	rs485->delay_rts_before_send = 0;
131*642aa760SIlpo Järvinen 	rs485->delay_rts_after_send = 0;
132*642aa760SIlpo Järvinen 
133*642aa760SIlpo Järvinen 	p->rs485 = *rs485;
134*642aa760SIlpo Järvinen 
135*642aa760SIlpo Järvinen 	return 0;
136*642aa760SIlpo Järvinen }
137*642aa760SIlpo Järvinen 
138*642aa760SIlpo Järvinen /*
139*642aa760SIlpo Järvinen  * Tests if RE_EN register can have non-zero value to see if RS-485 HW support
140*642aa760SIlpo Järvinen  * is present.
141*642aa760SIlpo Järvinen  */
142*642aa760SIlpo Järvinen static bool dw8250_detect_rs485_hw(struct uart_port *p)
143*642aa760SIlpo Järvinen {
144*642aa760SIlpo Järvinen 	u32 reg;
145*642aa760SIlpo Järvinen 
146*642aa760SIlpo Järvinen 	dw8250_writel_ext(p, DW_UART_RE_EN, 1);
147*642aa760SIlpo Järvinen 	reg = dw8250_readl_ext(p, DW_UART_RE_EN);
148*642aa760SIlpo Järvinen 	dw8250_writel_ext(p, DW_UART_RE_EN, 0);
149*642aa760SIlpo Järvinen 	return reg;
150*642aa760SIlpo Järvinen }
151*642aa760SIlpo 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 
159*642aa760SIlpo Järvinen 	pd->hw_rs485_support = dw8250_detect_rs485_hw(p);
160*642aa760SIlpo Järvinen 	if (pd->hw_rs485_support)
161*642aa760SIlpo Järvinen 		p->rs485_config = dw8250_rs485_config;
162*642aa760SIlpo Järvinen 
163136e0ab9SAndy Shevchenko 	/*
164136e0ab9SAndy Shevchenko 	 * If the Component Version Register returns zero, we know that
165136e0ab9SAndy Shevchenko 	 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
166136e0ab9SAndy Shevchenko 	 */
167136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_UCV);
168136e0ab9SAndy Shevchenko 	if (!reg)
169136e0ab9SAndy Shevchenko 		return;
170136e0ab9SAndy Shevchenko 
171136e0ab9SAndy Shevchenko 	dev_dbg(p->dev, "Designware UART version %c.%c%c\n",
172136e0ab9SAndy Shevchenko 		(reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
173136e0ab9SAndy Shevchenko 
174136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, ~0U);
175136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_DLF);
176136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, 0);
177136e0ab9SAndy Shevchenko 
178136e0ab9SAndy Shevchenko 	if (reg) {
179593dea00SMiquel Raynal 		pd->dlf_size = fls(reg);
180136e0ab9SAndy Shevchenko 		p->get_divisor = dw8250_get_divisor;
181136e0ab9SAndy Shevchenko 		p->set_divisor = dw8250_set_divisor;
182136e0ab9SAndy Shevchenko 	}
183136e0ab9SAndy Shevchenko 
184136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_CPR);
185593dea00SMiquel Raynal 	if (!reg) {
186593dea00SMiquel Raynal 		reg = data->pdata->cpr_val;
187593dea00SMiquel Raynal 		dev_dbg(p->dev, "CPR is not available, using 0x%08x instead\n", reg);
188593dea00SMiquel Raynal 	}
189136e0ab9SAndy Shevchenko 	if (!reg)
190136e0ab9SAndy Shevchenko 		return;
191136e0ab9SAndy Shevchenko 
192136e0ab9SAndy Shevchenko 	/* Select the type based on FIFO */
193136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_FIFO_MODE) {
194136e0ab9SAndy Shevchenko 		p->type = PORT_16550A;
195136e0ab9SAndy Shevchenko 		p->flags |= UPF_FIXED_TYPE;
196136e0ab9SAndy Shevchenko 		p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
197136e0ab9SAndy Shevchenko 		up->capabilities = UART_CAP_FIFO;
198136e0ab9SAndy Shevchenko 	}
199136e0ab9SAndy Shevchenko 
200136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_AFCE_MODE)
201136e0ab9SAndy Shevchenko 		up->capabilities |= UART_CAP_AFE;
202136e0ab9SAndy Shevchenko 
203136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_SIR_MODE)
204136e0ab9SAndy Shevchenko 		up->capabilities |= UART_CAP_IRDA;
205136e0ab9SAndy Shevchenko }
206136e0ab9SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_setup_port);
207