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 88*ae50bb27SIlpo Järvinen static int dw8250_rs485_config(struct uart_port *p, struct ktermios *termios, 89*ae50bb27SIlpo Järvinen struct serial_rs485 *rs485) 90642aa760SIlpo Järvinen { 91642aa760SIlpo Järvinen u32 tcr; 92642aa760SIlpo Järvinen 93642aa760SIlpo Järvinen tcr = dw8250_readl_ext(p, DW_UART_TCR); 94642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_XFER_MODE; 95642aa760SIlpo Järvinen 96642aa760SIlpo Järvinen if (rs485->flags & SER_RS485_ENABLED) { 97642aa760SIlpo Järvinen tcr |= DW_UART_TCR_RS485_EN; 98642aa760SIlpo Järvinen 99642aa760SIlpo Järvinen if (rs485->flags & SER_RS485_RX_DURING_TX) { 100642aa760SIlpo Järvinen tcr |= DW_UART_TCR_XFER_MODE_DE_DURING_RE; 101642aa760SIlpo Järvinen } else { 102642aa760SIlpo Järvinen /* HW does not support same DE level for tx and rx */ 103642aa760SIlpo Järvinen if (!(rs485->flags & SER_RS485_RTS_ON_SEND) == 104642aa760SIlpo Järvinen !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) 105642aa760SIlpo Järvinen return -EINVAL; 106642aa760SIlpo Järvinen 107642aa760SIlpo Järvinen tcr |= DW_UART_TCR_XFER_MODE_DE_OR_RE; 108642aa760SIlpo Järvinen } 109642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_DE_EN, 1); 110642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 1); 111642aa760SIlpo Järvinen } else { 112642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_RS485_EN; 113642aa760SIlpo Järvinen } 114642aa760SIlpo Järvinen 115642aa760SIlpo Järvinen /* Reset to default polarity */ 116642aa760SIlpo Järvinen tcr |= DW_UART_TCR_DE_POL; 117642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_RE_POL; 118642aa760SIlpo Järvinen 119642aa760SIlpo Järvinen if (!(rs485->flags & SER_RS485_RTS_ON_SEND)) 120642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_DE_POL; 121642aa760SIlpo Järvinen if (device_property_read_bool(p->dev, "rs485-rx-active-high")) 122642aa760SIlpo Järvinen tcr |= DW_UART_TCR_RE_POL; 123642aa760SIlpo Järvinen 124642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_TCR, tcr); 125642aa760SIlpo Järvinen 126642aa760SIlpo Järvinen return 0; 127642aa760SIlpo Järvinen } 128642aa760SIlpo Järvinen 129642aa760SIlpo Järvinen /* 130642aa760SIlpo Järvinen * Tests if RE_EN register can have non-zero value to see if RS-485 HW support 131642aa760SIlpo Järvinen * is present. 132642aa760SIlpo Järvinen */ 133642aa760SIlpo Järvinen static bool dw8250_detect_rs485_hw(struct uart_port *p) 134642aa760SIlpo Järvinen { 135642aa760SIlpo Järvinen u32 reg; 136642aa760SIlpo Järvinen 137642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 1); 138642aa760SIlpo Järvinen reg = dw8250_readl_ext(p, DW_UART_RE_EN); 139642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 0); 140642aa760SIlpo Järvinen return reg; 141642aa760SIlpo Järvinen } 142642aa760SIlpo Järvinen 14362a4b3d2SIlpo Järvinen static const struct serial_rs485 dw8250_rs485_supported = { 14462a4b3d2SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_RTS_ON_SEND | 14562a4b3d2SIlpo Järvinen SER_RS485_RTS_AFTER_SEND, 14662a4b3d2SIlpo Järvinen }; 14762a4b3d2SIlpo Järvinen 148136e0ab9SAndy Shevchenko void dw8250_setup_port(struct uart_port *p) 149136e0ab9SAndy Shevchenko { 150593dea00SMiquel Raynal struct dw8250_port_data *pd = p->private_data; 151593dea00SMiquel Raynal struct dw8250_data *data = to_dw8250_data(pd); 152136e0ab9SAndy Shevchenko struct uart_8250_port *up = up_to_u8250p(p); 153136e0ab9SAndy Shevchenko u32 reg; 154136e0ab9SAndy Shevchenko 155642aa760SIlpo Järvinen pd->hw_rs485_support = dw8250_detect_rs485_hw(p); 1565ff33917SIlpo Järvinen if (pd->hw_rs485_support) { 157642aa760SIlpo Järvinen p->rs485_config = dw8250_rs485_config; 15862a4b3d2SIlpo Järvinen p->rs485_supported = &dw8250_rs485_supported; 1595ff33917SIlpo Järvinen } else { 1605ff33917SIlpo Järvinen p->rs485_config = serial8250_em485_config; 16162a4b3d2SIlpo Järvinen p->rs485_supported = &serial8250_em485_supported; 1625ff33917SIlpo Järvinen up->rs485_start_tx = serial8250_em485_start_tx; 1635ff33917SIlpo Järvinen up->rs485_stop_tx = serial8250_em485_stop_tx; 1645ff33917SIlpo Järvinen } 1655ff33917SIlpo Järvinen up->capabilities |= UART_CAP_NOTEMT; 166642aa760SIlpo Järvinen 167136e0ab9SAndy Shevchenko /* 168136e0ab9SAndy Shevchenko * If the Component Version Register returns zero, we know that 169136e0ab9SAndy Shevchenko * ADDITIONAL_FEATURES are not enabled. No need to go any further. 170136e0ab9SAndy Shevchenko */ 171136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_UCV); 172136e0ab9SAndy Shevchenko if (!reg) 173136e0ab9SAndy Shevchenko return; 174136e0ab9SAndy Shevchenko 175136e0ab9SAndy Shevchenko dev_dbg(p->dev, "Designware UART version %c.%c%c\n", 176136e0ab9SAndy Shevchenko (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff); 177136e0ab9SAndy Shevchenko 178136e0ab9SAndy Shevchenko dw8250_writel_ext(p, DW_UART_DLF, ~0U); 179136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_DLF); 180136e0ab9SAndy Shevchenko dw8250_writel_ext(p, DW_UART_DLF, 0); 181136e0ab9SAndy Shevchenko 182136e0ab9SAndy Shevchenko if (reg) { 183593dea00SMiquel Raynal pd->dlf_size = fls(reg); 184136e0ab9SAndy Shevchenko p->get_divisor = dw8250_get_divisor; 185136e0ab9SAndy Shevchenko p->set_divisor = dw8250_set_divisor; 186136e0ab9SAndy Shevchenko } 187136e0ab9SAndy Shevchenko 188136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_CPR); 189593dea00SMiquel Raynal if (!reg) { 190593dea00SMiquel Raynal reg = data->pdata->cpr_val; 191593dea00SMiquel Raynal dev_dbg(p->dev, "CPR is not available, using 0x%08x instead\n", reg); 192593dea00SMiquel Raynal } 193136e0ab9SAndy Shevchenko if (!reg) 194136e0ab9SAndy Shevchenko return; 195136e0ab9SAndy Shevchenko 196136e0ab9SAndy Shevchenko /* Select the type based on FIFO */ 197136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_FIFO_MODE) { 198136e0ab9SAndy Shevchenko p->type = PORT_16550A; 199136e0ab9SAndy Shevchenko p->flags |= UPF_FIXED_TYPE; 200136e0ab9SAndy Shevchenko p->fifosize = DW_UART_CPR_FIFO_SIZE(reg); 2015ff33917SIlpo Järvinen up->capabilities = UART_CAP_FIFO | UART_CAP_NOTEMT; 202136e0ab9SAndy Shevchenko } 203136e0ab9SAndy Shevchenko 204136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_AFCE_MODE) 205136e0ab9SAndy Shevchenko up->capabilities |= UART_CAP_AFE; 206136e0ab9SAndy Shevchenko 207136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_SIR_MODE) 208136e0ab9SAndy Shevchenko up->capabilities |= UART_CAP_IRDA; 209136e0ab9SAndy Shevchenko } 210136e0ab9SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_setup_port); 211