xref: /openbmc/linux/drivers/tty/serial/8250/8250_dwlib.c (revision 136e0ab99b22378e3ff7d54f799a3a329316e869)
1*136e0ab9SAndy Shevchenko // SPDX-License-Identifier: GPL-2.0+
2*136e0ab9SAndy Shevchenko /* Synopsys DesignWare 8250 library. */
3*136e0ab9SAndy Shevchenko 
4*136e0ab9SAndy Shevchenko #include <linux/bitops.h>
5*136e0ab9SAndy Shevchenko #include <linux/device.h>
6*136e0ab9SAndy Shevchenko #include <linux/io.h>
7*136e0ab9SAndy Shevchenko #include <linux/kernel.h>
8*136e0ab9SAndy Shevchenko #include <linux/serial_8250.h>
9*136e0ab9SAndy Shevchenko #include <linux/serial_core.h>
10*136e0ab9SAndy Shevchenko 
11*136e0ab9SAndy Shevchenko #include "8250_dwlib.h"
12*136e0ab9SAndy Shevchenko 
13*136e0ab9SAndy Shevchenko /* Offsets for the DesignWare specific registers */
14*136e0ab9SAndy Shevchenko #define DW_UART_DLF	0xc0 /* Divisor Latch Fraction Register */
15*136e0ab9SAndy Shevchenko #define DW_UART_CPR	0xf4 /* Component Parameter Register */
16*136e0ab9SAndy Shevchenko #define DW_UART_UCV	0xf8 /* UART Component Version */
17*136e0ab9SAndy Shevchenko 
18*136e0ab9SAndy Shevchenko /* Component Parameter Register bits */
19*136e0ab9SAndy Shevchenko #define DW_UART_CPR_ABP_DATA_WIDTH	(3 << 0)
20*136e0ab9SAndy Shevchenko #define DW_UART_CPR_AFCE_MODE		(1 << 4)
21*136e0ab9SAndy Shevchenko #define DW_UART_CPR_THRE_MODE		(1 << 5)
22*136e0ab9SAndy Shevchenko #define DW_UART_CPR_SIR_MODE		(1 << 6)
23*136e0ab9SAndy Shevchenko #define DW_UART_CPR_SIR_LP_MODE		(1 << 7)
24*136e0ab9SAndy Shevchenko #define DW_UART_CPR_ADDITIONAL_FEATURES	(1 << 8)
25*136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_ACCESS		(1 << 9)
26*136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_STAT		(1 << 10)
27*136e0ab9SAndy Shevchenko #define DW_UART_CPR_SHADOW		(1 << 11)
28*136e0ab9SAndy Shevchenko #define DW_UART_CPR_ENCODED_PARMS	(1 << 12)
29*136e0ab9SAndy Shevchenko #define DW_UART_CPR_DMA_EXTRA		(1 << 13)
30*136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_MODE		(0xff << 16)
31*136e0ab9SAndy Shevchenko 
32*136e0ab9SAndy Shevchenko /* Helper for FIFO size calculation */
33*136e0ab9SAndy Shevchenko #define DW_UART_CPR_FIFO_SIZE(a)	(((a >> 16) & 0xff) * 16)
34*136e0ab9SAndy Shevchenko 
35*136e0ab9SAndy Shevchenko static inline u32 dw8250_readl_ext(struct uart_port *p, int offset)
36*136e0ab9SAndy Shevchenko {
37*136e0ab9SAndy Shevchenko 	if (p->iotype == UPIO_MEM32BE)
38*136e0ab9SAndy Shevchenko 		return ioread32be(p->membase + offset);
39*136e0ab9SAndy Shevchenko 	return readl(p->membase + offset);
40*136e0ab9SAndy Shevchenko }
41*136e0ab9SAndy Shevchenko 
42*136e0ab9SAndy Shevchenko static inline void dw8250_writel_ext(struct uart_port *p, int offset, u32 reg)
43*136e0ab9SAndy Shevchenko {
44*136e0ab9SAndy Shevchenko 	if (p->iotype == UPIO_MEM32BE)
45*136e0ab9SAndy Shevchenko 		iowrite32be(reg, p->membase + offset);
46*136e0ab9SAndy Shevchenko 	else
47*136e0ab9SAndy Shevchenko 		writel(reg, p->membase + offset);
48*136e0ab9SAndy Shevchenko }
49*136e0ab9SAndy Shevchenko 
50*136e0ab9SAndy Shevchenko /*
51*136e0ab9SAndy Shevchenko  * divisor = div(I) + div(F)
52*136e0ab9SAndy Shevchenko  * "I" means integer, "F" means fractional
53*136e0ab9SAndy Shevchenko  * quot = div(I) = clk / (16 * baud)
54*136e0ab9SAndy Shevchenko  * frac = div(F) * 2^dlf_size
55*136e0ab9SAndy Shevchenko  *
56*136e0ab9SAndy Shevchenko  * let rem = clk % (16 * baud)
57*136e0ab9SAndy Shevchenko  * we have: div(F) * (16 * baud) = rem
58*136e0ab9SAndy Shevchenko  * so frac = 2^dlf_size * rem / (16 * baud) = (rem << dlf_size) / (16 * baud)
59*136e0ab9SAndy Shevchenko  */
60*136e0ab9SAndy Shevchenko static unsigned int dw8250_get_divisor(struct uart_port *p, unsigned int baud,
61*136e0ab9SAndy Shevchenko 				       unsigned int *frac)
62*136e0ab9SAndy Shevchenko {
63*136e0ab9SAndy Shevchenko 	unsigned int quot, rem, base_baud = baud * 16;
64*136e0ab9SAndy Shevchenko 	struct dw8250_port_data *d = p->private_data;
65*136e0ab9SAndy Shevchenko 
66*136e0ab9SAndy Shevchenko 	quot = p->uartclk / base_baud;
67*136e0ab9SAndy Shevchenko 	rem = p->uartclk % base_baud;
68*136e0ab9SAndy Shevchenko 	*frac = DIV_ROUND_CLOSEST(rem << d->dlf_size, base_baud);
69*136e0ab9SAndy Shevchenko 
70*136e0ab9SAndy Shevchenko 	return quot;
71*136e0ab9SAndy Shevchenko }
72*136e0ab9SAndy Shevchenko 
73*136e0ab9SAndy Shevchenko static void dw8250_set_divisor(struct uart_port *p, unsigned int baud,
74*136e0ab9SAndy Shevchenko 			       unsigned int quot, unsigned int quot_frac)
75*136e0ab9SAndy Shevchenko {
76*136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, quot_frac);
77*136e0ab9SAndy Shevchenko 	serial8250_do_set_divisor(p, baud, quot, quot_frac);
78*136e0ab9SAndy Shevchenko }
79*136e0ab9SAndy Shevchenko 
80*136e0ab9SAndy Shevchenko void dw8250_setup_port(struct uart_port *p)
81*136e0ab9SAndy Shevchenko {
82*136e0ab9SAndy Shevchenko 	struct uart_8250_port *up = up_to_u8250p(p);
83*136e0ab9SAndy Shevchenko 	u32 reg;
84*136e0ab9SAndy Shevchenko 
85*136e0ab9SAndy Shevchenko 	/*
86*136e0ab9SAndy Shevchenko 	 * If the Component Version Register returns zero, we know that
87*136e0ab9SAndy Shevchenko 	 * ADDITIONAL_FEATURES are not enabled. No need to go any further.
88*136e0ab9SAndy Shevchenko 	 */
89*136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_UCV);
90*136e0ab9SAndy Shevchenko 	if (!reg)
91*136e0ab9SAndy Shevchenko 		return;
92*136e0ab9SAndy Shevchenko 
93*136e0ab9SAndy Shevchenko 	dev_dbg(p->dev, "Designware UART version %c.%c%c\n",
94*136e0ab9SAndy Shevchenko 		(reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff);
95*136e0ab9SAndy Shevchenko 
96*136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, ~0U);
97*136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_DLF);
98*136e0ab9SAndy Shevchenko 	dw8250_writel_ext(p, DW_UART_DLF, 0);
99*136e0ab9SAndy Shevchenko 
100*136e0ab9SAndy Shevchenko 	if (reg) {
101*136e0ab9SAndy Shevchenko 		struct dw8250_port_data *d = p->private_data;
102*136e0ab9SAndy Shevchenko 
103*136e0ab9SAndy Shevchenko 		d->dlf_size = fls(reg);
104*136e0ab9SAndy Shevchenko 		p->get_divisor = dw8250_get_divisor;
105*136e0ab9SAndy Shevchenko 		p->set_divisor = dw8250_set_divisor;
106*136e0ab9SAndy Shevchenko 	}
107*136e0ab9SAndy Shevchenko 
108*136e0ab9SAndy Shevchenko 	reg = dw8250_readl_ext(p, DW_UART_CPR);
109*136e0ab9SAndy Shevchenko 	if (!reg)
110*136e0ab9SAndy Shevchenko 		return;
111*136e0ab9SAndy Shevchenko 
112*136e0ab9SAndy Shevchenko 	/* Select the type based on FIFO */
113*136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_FIFO_MODE) {
114*136e0ab9SAndy Shevchenko 		p->type = PORT_16550A;
115*136e0ab9SAndy Shevchenko 		p->flags |= UPF_FIXED_TYPE;
116*136e0ab9SAndy Shevchenko 		p->fifosize = DW_UART_CPR_FIFO_SIZE(reg);
117*136e0ab9SAndy Shevchenko 		up->capabilities = UART_CAP_FIFO;
118*136e0ab9SAndy Shevchenko 	}
119*136e0ab9SAndy Shevchenko 
120*136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_AFCE_MODE)
121*136e0ab9SAndy Shevchenko 		up->capabilities |= UART_CAP_AFE;
122*136e0ab9SAndy Shevchenko 
123*136e0ab9SAndy Shevchenko 	if (reg & DW_UART_CPR_SIR_MODE)
124*136e0ab9SAndy Shevchenko 		up->capabilities |= UART_CAP_IRDA;
125*136e0ab9SAndy Shevchenko }
126*136e0ab9SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_setup_port);
127