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> 6f287f971SIlpo Järvinen #include <linux/delay.h> 7136e0ab9SAndy Shevchenko #include <linux/device.h> 8136e0ab9SAndy Shevchenko #include <linux/kernel.h> 9f287f971SIlpo Järvinen #include <linux/math.h> 10642aa760SIlpo Järvinen #include <linux/property.h> 11136e0ab9SAndy Shevchenko #include <linux/serial_8250.h> 12136e0ab9SAndy Shevchenko #include <linux/serial_core.h> 13136e0ab9SAndy Shevchenko 14136e0ab9SAndy Shevchenko #include "8250_dwlib.h" 15136e0ab9SAndy Shevchenko 16136e0ab9SAndy Shevchenko /* Offsets for the DesignWare specific registers */ 17642aa760SIlpo Järvinen #define DW_UART_TCR 0xac /* Transceiver Control Register (RS485) */ 18642aa760SIlpo Järvinen #define DW_UART_DE_EN 0xb0 /* Driver Output Enable Register */ 19642aa760SIlpo Järvinen #define DW_UART_RE_EN 0xb4 /* Receiver Output Enable Register */ 20136e0ab9SAndy Shevchenko #define DW_UART_DLF 0xc0 /* Divisor Latch Fraction Register */ 21f287f971SIlpo Järvinen #define DW_UART_RAR 0xc4 /* Receive Address Register */ 22f287f971SIlpo Järvinen #define DW_UART_TAR 0xc8 /* Transmit Address Register */ 23f287f971SIlpo Järvinen #define DW_UART_LCR_EXT 0xcc /* Line Extended Control Register */ 24136e0ab9SAndy Shevchenko #define DW_UART_CPR 0xf4 /* Component Parameter Register */ 25136e0ab9SAndy Shevchenko #define DW_UART_UCV 0xf8 /* UART Component Version */ 26136e0ab9SAndy Shevchenko 27f287f971SIlpo Järvinen /* Receive / Transmit Address Register bits */ 28f287f971SIlpo Järvinen #define DW_UART_ADDR_MASK GENMASK(7, 0) 29f287f971SIlpo Järvinen 30f287f971SIlpo Järvinen /* Line Status Register bits */ 31f287f971SIlpo Järvinen #define DW_UART_LSR_ADDR_RCVD BIT(8) 32f287f971SIlpo Järvinen 33642aa760SIlpo Järvinen /* Transceiver Control Register bits */ 34642aa760SIlpo Järvinen #define DW_UART_TCR_RS485_EN BIT(0) 35642aa760SIlpo Järvinen #define DW_UART_TCR_RE_POL BIT(1) 36642aa760SIlpo Järvinen #define DW_UART_TCR_DE_POL BIT(2) 37642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE GENMASK(4, 3) 38642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_DE_DURING_RE FIELD_PREP(DW_UART_TCR_XFER_MODE, 0) 39642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_SW_DE_OR_RE FIELD_PREP(DW_UART_TCR_XFER_MODE, 1) 40642aa760SIlpo Järvinen #define DW_UART_TCR_XFER_MODE_DE_OR_RE FIELD_PREP(DW_UART_TCR_XFER_MODE, 2) 41642aa760SIlpo Järvinen 42f287f971SIlpo Järvinen /* Line Extended Control Register bits */ 43f287f971SIlpo Järvinen #define DW_UART_LCR_EXT_DLS_E BIT(0) 44f287f971SIlpo Järvinen #define DW_UART_LCR_EXT_ADDR_MATCH BIT(1) 45f287f971SIlpo Järvinen #define DW_UART_LCR_EXT_SEND_ADDR BIT(2) 46f287f971SIlpo Järvinen #define DW_UART_LCR_EXT_TRANSMIT_MODE BIT(3) 47f287f971SIlpo Järvinen 48136e0ab9SAndy Shevchenko /* Component Parameter Register bits */ 49e9f97366SIlpo Järvinen #define DW_UART_CPR_ABP_DATA_WIDTH GENMASK(1, 0) 50e9f97366SIlpo Järvinen #define DW_UART_CPR_AFCE_MODE BIT(4) 51e9f97366SIlpo Järvinen #define DW_UART_CPR_THRE_MODE BIT(5) 52e9f97366SIlpo Järvinen #define DW_UART_CPR_SIR_MODE BIT(6) 53e9f97366SIlpo Järvinen #define DW_UART_CPR_SIR_LP_MODE BIT(7) 54e9f97366SIlpo Järvinen #define DW_UART_CPR_ADDITIONAL_FEATURES BIT(8) 55e9f97366SIlpo Järvinen #define DW_UART_CPR_FIFO_ACCESS BIT(9) 56e9f97366SIlpo Järvinen #define DW_UART_CPR_FIFO_STAT BIT(10) 57e9f97366SIlpo Järvinen #define DW_UART_CPR_SHADOW BIT(11) 58e9f97366SIlpo Järvinen #define DW_UART_CPR_ENCODED_PARMS BIT(12) 59e9f97366SIlpo Järvinen #define DW_UART_CPR_DMA_EXTRA BIT(13) 60e9f97366SIlpo Järvinen #define DW_UART_CPR_FIFO_MODE GENMASK(23, 16) 61136e0ab9SAndy Shevchenko 62136e0ab9SAndy Shevchenko /* Helper for FIFO size calculation */ 63e9f97366SIlpo Järvinen #define DW_UART_CPR_FIFO_SIZE(a) (FIELD_GET(DW_UART_CPR_FIFO_MODE, (a)) * 16) 64136e0ab9SAndy Shevchenko 65136e0ab9SAndy Shevchenko /* 66136e0ab9SAndy Shevchenko * divisor = div(I) + div(F) 67136e0ab9SAndy Shevchenko * "I" means integer, "F" means fractional 68136e0ab9SAndy Shevchenko * quot = div(I) = clk / (16 * baud) 69136e0ab9SAndy Shevchenko * frac = div(F) * 2^dlf_size 70136e0ab9SAndy Shevchenko * 71136e0ab9SAndy Shevchenko * let rem = clk % (16 * baud) 72136e0ab9SAndy Shevchenko * we have: div(F) * (16 * baud) = rem 73136e0ab9SAndy Shevchenko * so frac = 2^dlf_size * rem / (16 * baud) = (rem << dlf_size) / (16 * baud) 74136e0ab9SAndy Shevchenko */ 75136e0ab9SAndy Shevchenko static unsigned int dw8250_get_divisor(struct uart_port *p, unsigned int baud, 76136e0ab9SAndy Shevchenko unsigned int *frac) 77136e0ab9SAndy Shevchenko { 78136e0ab9SAndy Shevchenko unsigned int quot, rem, base_baud = baud * 16; 79136e0ab9SAndy Shevchenko struct dw8250_port_data *d = p->private_data; 80136e0ab9SAndy Shevchenko 81136e0ab9SAndy Shevchenko quot = p->uartclk / base_baud; 82136e0ab9SAndy Shevchenko rem = p->uartclk % base_baud; 83136e0ab9SAndy Shevchenko *frac = DIV_ROUND_CLOSEST(rem << d->dlf_size, base_baud); 84136e0ab9SAndy Shevchenko 85136e0ab9SAndy Shevchenko return quot; 86136e0ab9SAndy Shevchenko } 87136e0ab9SAndy Shevchenko 88136e0ab9SAndy Shevchenko static void dw8250_set_divisor(struct uart_port *p, unsigned int baud, 89136e0ab9SAndy Shevchenko unsigned int quot, unsigned int quot_frac) 90136e0ab9SAndy Shevchenko { 91136e0ab9SAndy Shevchenko dw8250_writel_ext(p, DW_UART_DLF, quot_frac); 92136e0ab9SAndy Shevchenko serial8250_do_set_divisor(p, baud, quot, quot_frac); 93136e0ab9SAndy Shevchenko } 94136e0ab9SAndy Shevchenko 95bec5b814SIlpo Järvinen void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios, 96bec5b814SIlpo Järvinen const struct ktermios *old) 977c4fc082SAndy Shevchenko { 987c4fc082SAndy Shevchenko p->status &= ~UPSTAT_AUTOCTS; 997c4fc082SAndy Shevchenko if (termios->c_cflag & CRTSCTS) 1007c4fc082SAndy Shevchenko p->status |= UPSTAT_AUTOCTS; 1017c4fc082SAndy Shevchenko 1027c4fc082SAndy Shevchenko serial8250_do_set_termios(p, termios, old); 103f287f971SIlpo Järvinen 104f287f971SIlpo Järvinen /* Filter addresses which have 9th bit set */ 105f287f971SIlpo Järvinen p->ignore_status_mask |= DW_UART_LSR_ADDR_RCVD; 106f287f971SIlpo Järvinen p->read_status_mask |= DW_UART_LSR_ADDR_RCVD; 1077c4fc082SAndy Shevchenko } 1087c4fc082SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_do_set_termios); 1097c4fc082SAndy Shevchenko 110f287f971SIlpo Järvinen /* 111f287f971SIlpo Järvinen * Wait until re is de-asserted for sure. An ongoing receive will keep 112f287f971SIlpo Järvinen * re asserted until end of frame. Without BUSY indication available, 113f287f971SIlpo Järvinen * only available course of action is to wait for the time it takes to 114f287f971SIlpo Järvinen * receive one frame (there might nothing to receive but w/o BUSY the 115f287f971SIlpo Järvinen * driver cannot know). 116f287f971SIlpo Järvinen */ 117f287f971SIlpo Järvinen static void dw8250_wait_re_deassert(struct uart_port *p) 118f287f971SIlpo Järvinen { 119f287f971SIlpo Järvinen ndelay(p->frame_time); 120f287f971SIlpo Järvinen } 121f287f971SIlpo Järvinen 122f287f971SIlpo Järvinen static void dw8250_update_rar(struct uart_port *p, u32 addr) 123f287f971SIlpo Järvinen { 124f287f971SIlpo Järvinen u32 re_en = dw8250_readl_ext(p, DW_UART_RE_EN); 125f287f971SIlpo Järvinen 126f287f971SIlpo Järvinen /* 127f287f971SIlpo Järvinen * RAR shouldn't be changed while receiving. Thus, de-assert RE_EN 128f287f971SIlpo Järvinen * if asserted and wait. 129f287f971SIlpo Järvinen */ 130f287f971SIlpo Järvinen if (re_en) 131f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 0); 132f287f971SIlpo Järvinen dw8250_wait_re_deassert(p); 133f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RAR, addr); 134f287f971SIlpo Järvinen if (re_en) 135f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, re_en); 136f287f971SIlpo Järvinen } 137f287f971SIlpo Järvinen 138f287f971SIlpo Järvinen static void dw8250_rs485_set_addr(struct uart_port *p, struct serial_rs485 *rs485, 139f287f971SIlpo Järvinen struct ktermios *termios) 140f287f971SIlpo Järvinen { 141f287f971SIlpo Järvinen u32 lcr = dw8250_readl_ext(p, DW_UART_LCR_EXT); 142f287f971SIlpo Järvinen 143f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ADDRB) { 144f287f971SIlpo Järvinen lcr |= DW_UART_LCR_EXT_DLS_E; 145f287f971SIlpo Järvinen if (termios) 146f287f971SIlpo Järvinen termios->c_cflag |= ADDRB; 147f287f971SIlpo Järvinen 148f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ADDR_RECV) { 149f287f971SIlpo Järvinen u32 delta = p->rs485.flags ^ rs485->flags; 150f287f971SIlpo Järvinen 151f287f971SIlpo Järvinen /* 152f287f971SIlpo Järvinen * rs485 (param) is equal to uart_port's rs485 only during init 153f287f971SIlpo Järvinen * (during init, delta is not yet applicable). 154f287f971SIlpo Järvinen */ 155f287f971SIlpo Järvinen if (unlikely(&p->rs485 == rs485)) 156f287f971SIlpo Järvinen delta = rs485->flags; 157f287f971SIlpo Järvinen 158f287f971SIlpo Järvinen if ((delta & SER_RS485_ADDR_RECV) || 159f287f971SIlpo Järvinen (p->rs485.addr_recv != rs485->addr_recv)) 160f287f971SIlpo Järvinen dw8250_update_rar(p, rs485->addr_recv); 161f287f971SIlpo Järvinen lcr |= DW_UART_LCR_EXT_ADDR_MATCH; 162f287f971SIlpo Järvinen } else { 163f287f971SIlpo Järvinen lcr &= ~DW_UART_LCR_EXT_ADDR_MATCH; 164f287f971SIlpo Järvinen } 165f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ADDR_DEST) { 166f287f971SIlpo Järvinen /* 167f287f971SIlpo Järvinen * Don't skip writes here as another endpoint could 168f287f971SIlpo Järvinen * have changed communication line's destination 169f287f971SIlpo Järvinen * address in between. 170f287f971SIlpo Järvinen */ 171f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_TAR, rs485->addr_dest); 172f287f971SIlpo Järvinen lcr |= DW_UART_LCR_EXT_SEND_ADDR; 173f287f971SIlpo Järvinen } 174f287f971SIlpo Järvinen } else { 175f287f971SIlpo Järvinen lcr = 0; 176f287f971SIlpo Järvinen } 177f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_LCR_EXT, lcr); 178f287f971SIlpo Järvinen } 179f287f971SIlpo Järvinen 180ae50bb27SIlpo Järvinen static int dw8250_rs485_config(struct uart_port *p, struct ktermios *termios, 181ae50bb27SIlpo Järvinen struct serial_rs485 *rs485) 182642aa760SIlpo Järvinen { 183642aa760SIlpo Järvinen u32 tcr; 184642aa760SIlpo Järvinen 185642aa760SIlpo Järvinen tcr = dw8250_readl_ext(p, DW_UART_TCR); 186642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_XFER_MODE; 187642aa760SIlpo Järvinen 188642aa760SIlpo Järvinen if (rs485->flags & SER_RS485_ENABLED) { 189642aa760SIlpo Järvinen tcr |= DW_UART_TCR_RS485_EN; 190642aa760SIlpo Järvinen 191c64e1758SLino Sanfilippo if (rs485->flags & SER_RS485_RX_DURING_TX) 192642aa760SIlpo Järvinen tcr |= DW_UART_TCR_XFER_MODE_DE_DURING_RE; 193c64e1758SLino Sanfilippo else 194642aa760SIlpo Järvinen tcr |= DW_UART_TCR_XFER_MODE_DE_OR_RE; 195642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_DE_EN, 1); 196642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 1); 197642aa760SIlpo Järvinen } else { 198f287f971SIlpo Järvinen if (termios) 199f287f971SIlpo Järvinen termios->c_cflag &= ~ADDRB; 200f287f971SIlpo Järvinen 201642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_RS485_EN; 202642aa760SIlpo Järvinen } 203642aa760SIlpo Järvinen 204642aa760SIlpo Järvinen /* Reset to default polarity */ 205642aa760SIlpo Järvinen tcr |= DW_UART_TCR_DE_POL; 206642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_RE_POL; 207642aa760SIlpo Järvinen 208642aa760SIlpo Järvinen if (!(rs485->flags & SER_RS485_RTS_ON_SEND)) 209642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_DE_POL; 210642aa760SIlpo Järvinen if (device_property_read_bool(p->dev, "rs485-rx-active-high")) 211642aa760SIlpo Järvinen tcr |= DW_UART_TCR_RE_POL; 212642aa760SIlpo Järvinen 213642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_TCR, tcr); 214642aa760SIlpo Järvinen 215f287f971SIlpo Järvinen /* Addressing mode can only be set up after TCR */ 216f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ENABLED) 217f287f971SIlpo Järvinen dw8250_rs485_set_addr(p, rs485, termios); 218f287f971SIlpo Järvinen 219642aa760SIlpo Järvinen return 0; 220642aa760SIlpo Järvinen } 221642aa760SIlpo Järvinen 222642aa760SIlpo Järvinen /* 223642aa760SIlpo Järvinen * Tests if RE_EN register can have non-zero value to see if RS-485 HW support 224642aa760SIlpo Järvinen * is present. 225642aa760SIlpo Järvinen */ 226642aa760SIlpo Järvinen static bool dw8250_detect_rs485_hw(struct uart_port *p) 227642aa760SIlpo Järvinen { 228642aa760SIlpo Järvinen u32 reg; 229642aa760SIlpo Järvinen 230642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 1); 231642aa760SIlpo Järvinen reg = dw8250_readl_ext(p, DW_UART_RE_EN); 232642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 0); 233642aa760SIlpo Järvinen return reg; 234642aa760SIlpo Järvinen } 235642aa760SIlpo Järvinen 23662a4b3d2SIlpo Järvinen static const struct serial_rs485 dw8250_rs485_supported = { 23762a4b3d2SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_RTS_ON_SEND | 238f287f971SIlpo Järvinen SER_RS485_RTS_AFTER_SEND | SER_RS485_ADDRB | SER_RS485_ADDR_RECV | 239f287f971SIlpo Järvinen SER_RS485_ADDR_DEST, 24062a4b3d2SIlpo Järvinen }; 24162a4b3d2SIlpo Järvinen 242136e0ab9SAndy Shevchenko void dw8250_setup_port(struct uart_port *p) 243136e0ab9SAndy Shevchenko { 244593dea00SMiquel Raynal struct dw8250_port_data *pd = p->private_data; 245593dea00SMiquel Raynal struct dw8250_data *data = to_dw8250_data(pd); 246136e0ab9SAndy Shevchenko struct uart_8250_port *up = up_to_u8250p(p); 247*748c5ea8SRuihong Luo u32 reg, old_dlf; 248136e0ab9SAndy Shevchenko 249642aa760SIlpo Järvinen pd->hw_rs485_support = dw8250_detect_rs485_hw(p); 2505ff33917SIlpo Järvinen if (pd->hw_rs485_support) { 251642aa760SIlpo Järvinen p->rs485_config = dw8250_rs485_config; 252f287f971SIlpo Järvinen up->lsr_save_mask = LSR_SAVE_FLAGS | DW_UART_LSR_ADDR_RCVD; 2530139da50SIlpo Järvinen p->rs485_supported = dw8250_rs485_supported; 2545ff33917SIlpo Järvinen } else { 2555ff33917SIlpo Järvinen p->rs485_config = serial8250_em485_config; 2560139da50SIlpo Järvinen p->rs485_supported = serial8250_em485_supported; 2575ff33917SIlpo Järvinen up->rs485_start_tx = serial8250_em485_start_tx; 2585ff33917SIlpo Järvinen up->rs485_stop_tx = serial8250_em485_stop_tx; 2595ff33917SIlpo Järvinen } 2605ff33917SIlpo Järvinen up->capabilities |= UART_CAP_NOTEMT; 261642aa760SIlpo Järvinen 262136e0ab9SAndy Shevchenko /* 263136e0ab9SAndy Shevchenko * If the Component Version Register returns zero, we know that 264136e0ab9SAndy Shevchenko * ADDITIONAL_FEATURES are not enabled. No need to go any further. 265136e0ab9SAndy Shevchenko */ 266136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_UCV); 267136e0ab9SAndy Shevchenko if (!reg) 268136e0ab9SAndy Shevchenko return; 269136e0ab9SAndy Shevchenko 270136e0ab9SAndy Shevchenko dev_dbg(p->dev, "Designware UART version %c.%c%c\n", 271136e0ab9SAndy Shevchenko (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff); 272136e0ab9SAndy Shevchenko 273*748c5ea8SRuihong Luo /* Preserve value written by firmware or bootloader */ 274*748c5ea8SRuihong Luo old_dlf = dw8250_readl_ext(p, DW_UART_DLF); 275136e0ab9SAndy Shevchenko dw8250_writel_ext(p, DW_UART_DLF, ~0U); 276136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_DLF); 277*748c5ea8SRuihong Luo dw8250_writel_ext(p, DW_UART_DLF, old_dlf); 278136e0ab9SAndy Shevchenko 279136e0ab9SAndy Shevchenko if (reg) { 280593dea00SMiquel Raynal pd->dlf_size = fls(reg); 281136e0ab9SAndy Shevchenko p->get_divisor = dw8250_get_divisor; 282136e0ab9SAndy Shevchenko p->set_divisor = dw8250_set_divisor; 283136e0ab9SAndy Shevchenko } 284136e0ab9SAndy Shevchenko 285136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_CPR); 286593dea00SMiquel Raynal if (!reg) { 287593dea00SMiquel Raynal reg = data->pdata->cpr_val; 288593dea00SMiquel Raynal dev_dbg(p->dev, "CPR is not available, using 0x%08x instead\n", reg); 289593dea00SMiquel Raynal } 290136e0ab9SAndy Shevchenko if (!reg) 291136e0ab9SAndy Shevchenko return; 292136e0ab9SAndy Shevchenko 293136e0ab9SAndy Shevchenko /* Select the type based on FIFO */ 294136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_FIFO_MODE) { 295136e0ab9SAndy Shevchenko p->type = PORT_16550A; 296136e0ab9SAndy Shevchenko p->flags |= UPF_FIXED_TYPE; 297136e0ab9SAndy Shevchenko p->fifosize = DW_UART_CPR_FIFO_SIZE(reg); 2985ff33917SIlpo Järvinen up->capabilities = UART_CAP_FIFO | UART_CAP_NOTEMT; 299136e0ab9SAndy Shevchenko } 300136e0ab9SAndy Shevchenko 301136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_AFCE_MODE) 302136e0ab9SAndy Shevchenko up->capabilities |= UART_CAP_AFE; 303136e0ab9SAndy Shevchenko 304136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_SIR_MODE) 305136e0ab9SAndy Shevchenko up->capabilities |= UART_CAP_IRDA; 306136e0ab9SAndy Shevchenko } 307136e0ab9SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_setup_port); 308