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 957c4fc082SAndy Shevchenko void dw8250_do_set_termios(struct uart_port *p, struct ktermios *termios, struct ktermios *old) 967c4fc082SAndy Shevchenko { 977c4fc082SAndy Shevchenko p->status &= ~UPSTAT_AUTOCTS; 987c4fc082SAndy Shevchenko if (termios->c_cflag & CRTSCTS) 997c4fc082SAndy Shevchenko p->status |= UPSTAT_AUTOCTS; 1007c4fc082SAndy Shevchenko 1017c4fc082SAndy Shevchenko serial8250_do_set_termios(p, termios, old); 102f287f971SIlpo Järvinen 103f287f971SIlpo Järvinen /* Filter addresses which have 9th bit set */ 104f287f971SIlpo Järvinen p->ignore_status_mask |= DW_UART_LSR_ADDR_RCVD; 105f287f971SIlpo Järvinen p->read_status_mask |= DW_UART_LSR_ADDR_RCVD; 1067c4fc082SAndy Shevchenko } 1077c4fc082SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_do_set_termios); 1087c4fc082SAndy Shevchenko 109f287f971SIlpo Järvinen /* 110f287f971SIlpo Järvinen * Wait until re is de-asserted for sure. An ongoing receive will keep 111f287f971SIlpo Järvinen * re asserted until end of frame. Without BUSY indication available, 112f287f971SIlpo Järvinen * only available course of action is to wait for the time it takes to 113f287f971SIlpo Järvinen * receive one frame (there might nothing to receive but w/o BUSY the 114f287f971SIlpo Järvinen * driver cannot know). 115f287f971SIlpo Järvinen */ 116f287f971SIlpo Järvinen static void dw8250_wait_re_deassert(struct uart_port *p) 117f287f971SIlpo Järvinen { 118f287f971SIlpo Järvinen ndelay(p->frame_time); 119f287f971SIlpo Järvinen } 120f287f971SIlpo Järvinen 121f287f971SIlpo Järvinen static void dw8250_update_rar(struct uart_port *p, u32 addr) 122f287f971SIlpo Järvinen { 123f287f971SIlpo Järvinen u32 re_en = dw8250_readl_ext(p, DW_UART_RE_EN); 124f287f971SIlpo Järvinen 125f287f971SIlpo Järvinen /* 126f287f971SIlpo Järvinen * RAR shouldn't be changed while receiving. Thus, de-assert RE_EN 127f287f971SIlpo Järvinen * if asserted and wait. 128f287f971SIlpo Järvinen */ 129f287f971SIlpo Järvinen if (re_en) 130f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 0); 131f287f971SIlpo Järvinen dw8250_wait_re_deassert(p); 132f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RAR, addr); 133f287f971SIlpo Järvinen if (re_en) 134f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, re_en); 135f287f971SIlpo Järvinen } 136f287f971SIlpo Järvinen 137f287f971SIlpo Järvinen static void dw8250_rs485_set_addr(struct uart_port *p, struct serial_rs485 *rs485, 138f287f971SIlpo Järvinen struct ktermios *termios) 139f287f971SIlpo Järvinen { 140f287f971SIlpo Järvinen u32 lcr = dw8250_readl_ext(p, DW_UART_LCR_EXT); 141f287f971SIlpo Järvinen 142f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ADDRB) { 143f287f971SIlpo Järvinen lcr |= DW_UART_LCR_EXT_DLS_E; 144f287f971SIlpo Järvinen if (termios) 145f287f971SIlpo Järvinen termios->c_cflag |= ADDRB; 146f287f971SIlpo Järvinen 147f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ADDR_RECV) { 148f287f971SIlpo Järvinen u32 delta = p->rs485.flags ^ rs485->flags; 149f287f971SIlpo Järvinen 150f287f971SIlpo Järvinen /* 151f287f971SIlpo Järvinen * rs485 (param) is equal to uart_port's rs485 only during init 152f287f971SIlpo Järvinen * (during init, delta is not yet applicable). 153f287f971SIlpo Järvinen */ 154f287f971SIlpo Järvinen if (unlikely(&p->rs485 == rs485)) 155f287f971SIlpo Järvinen delta = rs485->flags; 156f287f971SIlpo Järvinen 157f287f971SIlpo Järvinen if ((delta & SER_RS485_ADDR_RECV) || 158f287f971SIlpo Järvinen (p->rs485.addr_recv != rs485->addr_recv)) 159f287f971SIlpo Järvinen dw8250_update_rar(p, rs485->addr_recv); 160f287f971SIlpo Järvinen lcr |= DW_UART_LCR_EXT_ADDR_MATCH; 161f287f971SIlpo Järvinen } else { 162f287f971SIlpo Järvinen lcr &= ~DW_UART_LCR_EXT_ADDR_MATCH; 163f287f971SIlpo Järvinen } 164f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ADDR_DEST) { 165f287f971SIlpo Järvinen /* 166f287f971SIlpo Järvinen * Don't skip writes here as another endpoint could 167f287f971SIlpo Järvinen * have changed communication line's destination 168f287f971SIlpo Järvinen * address in between. 169f287f971SIlpo Järvinen */ 170f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_TAR, rs485->addr_dest); 171f287f971SIlpo Järvinen lcr |= DW_UART_LCR_EXT_SEND_ADDR; 172f287f971SIlpo Järvinen } 173f287f971SIlpo Järvinen } else { 174f287f971SIlpo Järvinen lcr = 0; 175f287f971SIlpo Järvinen } 176f287f971SIlpo Järvinen dw8250_writel_ext(p, DW_UART_LCR_EXT, lcr); 177f287f971SIlpo Järvinen } 178f287f971SIlpo Järvinen 179ae50bb27SIlpo Järvinen static int dw8250_rs485_config(struct uart_port *p, struct ktermios *termios, 180ae50bb27SIlpo Järvinen struct serial_rs485 *rs485) 181642aa760SIlpo Järvinen { 182642aa760SIlpo Järvinen u32 tcr; 183642aa760SIlpo Järvinen 184642aa760SIlpo Järvinen tcr = dw8250_readl_ext(p, DW_UART_TCR); 185642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_XFER_MODE; 186642aa760SIlpo Järvinen 187642aa760SIlpo Järvinen if (rs485->flags & SER_RS485_ENABLED) { 188642aa760SIlpo Järvinen tcr |= DW_UART_TCR_RS485_EN; 189642aa760SIlpo Järvinen 190642aa760SIlpo Järvinen if (rs485->flags & SER_RS485_RX_DURING_TX) { 191642aa760SIlpo Järvinen tcr |= DW_UART_TCR_XFER_MODE_DE_DURING_RE; 192642aa760SIlpo Järvinen } else { 193642aa760SIlpo Järvinen /* HW does not support same DE level for tx and rx */ 194642aa760SIlpo Järvinen if (!(rs485->flags & SER_RS485_RTS_ON_SEND) == 195642aa760SIlpo Järvinen !(rs485->flags & SER_RS485_RTS_AFTER_SEND)) 196642aa760SIlpo Järvinen return -EINVAL; 197642aa760SIlpo Järvinen 198642aa760SIlpo Järvinen tcr |= DW_UART_TCR_XFER_MODE_DE_OR_RE; 199642aa760SIlpo Järvinen } 200642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_DE_EN, 1); 201642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 1); 202642aa760SIlpo Järvinen } else { 203f287f971SIlpo Järvinen if (termios) 204f287f971SIlpo Järvinen termios->c_cflag &= ~ADDRB; 205f287f971SIlpo Järvinen 206642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_RS485_EN; 207642aa760SIlpo Järvinen } 208642aa760SIlpo Järvinen 209642aa760SIlpo Järvinen /* Reset to default polarity */ 210642aa760SIlpo Järvinen tcr |= DW_UART_TCR_DE_POL; 211642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_RE_POL; 212642aa760SIlpo Järvinen 213642aa760SIlpo Järvinen if (!(rs485->flags & SER_RS485_RTS_ON_SEND)) 214642aa760SIlpo Järvinen tcr &= ~DW_UART_TCR_DE_POL; 215642aa760SIlpo Järvinen if (device_property_read_bool(p->dev, "rs485-rx-active-high")) 216642aa760SIlpo Järvinen tcr |= DW_UART_TCR_RE_POL; 217642aa760SIlpo Järvinen 218642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_TCR, tcr); 219642aa760SIlpo Järvinen 220f287f971SIlpo Järvinen /* Addressing mode can only be set up after TCR */ 221f287f971SIlpo Järvinen if (rs485->flags & SER_RS485_ENABLED) 222f287f971SIlpo Järvinen dw8250_rs485_set_addr(p, rs485, termios); 223f287f971SIlpo Järvinen 224642aa760SIlpo Järvinen return 0; 225642aa760SIlpo Järvinen } 226642aa760SIlpo Järvinen 227642aa760SIlpo Järvinen /* 228642aa760SIlpo Järvinen * Tests if RE_EN register can have non-zero value to see if RS-485 HW support 229642aa760SIlpo Järvinen * is present. 230642aa760SIlpo Järvinen */ 231642aa760SIlpo Järvinen static bool dw8250_detect_rs485_hw(struct uart_port *p) 232642aa760SIlpo Järvinen { 233642aa760SIlpo Järvinen u32 reg; 234642aa760SIlpo Järvinen 235642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 1); 236642aa760SIlpo Järvinen reg = dw8250_readl_ext(p, DW_UART_RE_EN); 237642aa760SIlpo Järvinen dw8250_writel_ext(p, DW_UART_RE_EN, 0); 238642aa760SIlpo Järvinen return reg; 239642aa760SIlpo Järvinen } 240642aa760SIlpo Järvinen 24162a4b3d2SIlpo Järvinen static const struct serial_rs485 dw8250_rs485_supported = { 24262a4b3d2SIlpo Järvinen .flags = SER_RS485_ENABLED | SER_RS485_RX_DURING_TX | SER_RS485_RTS_ON_SEND | 243f287f971SIlpo Järvinen SER_RS485_RTS_AFTER_SEND | SER_RS485_ADDRB | SER_RS485_ADDR_RECV | 244f287f971SIlpo Järvinen SER_RS485_ADDR_DEST, 24562a4b3d2SIlpo Järvinen }; 24662a4b3d2SIlpo Järvinen 247136e0ab9SAndy Shevchenko void dw8250_setup_port(struct uart_port *p) 248136e0ab9SAndy Shevchenko { 249593dea00SMiquel Raynal struct dw8250_port_data *pd = p->private_data; 250593dea00SMiquel Raynal struct dw8250_data *data = to_dw8250_data(pd); 251136e0ab9SAndy Shevchenko struct uart_8250_port *up = up_to_u8250p(p); 252136e0ab9SAndy Shevchenko u32 reg; 253136e0ab9SAndy Shevchenko 254642aa760SIlpo Järvinen pd->hw_rs485_support = dw8250_detect_rs485_hw(p); 2555ff33917SIlpo Järvinen if (pd->hw_rs485_support) { 256642aa760SIlpo Järvinen p->rs485_config = dw8250_rs485_config; 257f287f971SIlpo Järvinen up->lsr_save_mask = LSR_SAVE_FLAGS | DW_UART_LSR_ADDR_RCVD; 258*0139da50SIlpo Järvinen p->rs485_supported = dw8250_rs485_supported; 2595ff33917SIlpo Järvinen } else { 2605ff33917SIlpo Järvinen p->rs485_config = serial8250_em485_config; 261*0139da50SIlpo Järvinen p->rs485_supported = serial8250_em485_supported; 2625ff33917SIlpo Järvinen up->rs485_start_tx = serial8250_em485_start_tx; 2635ff33917SIlpo Järvinen up->rs485_stop_tx = serial8250_em485_stop_tx; 2645ff33917SIlpo Järvinen } 2655ff33917SIlpo Järvinen up->capabilities |= UART_CAP_NOTEMT; 266642aa760SIlpo Järvinen 267136e0ab9SAndy Shevchenko /* 268136e0ab9SAndy Shevchenko * If the Component Version Register returns zero, we know that 269136e0ab9SAndy Shevchenko * ADDITIONAL_FEATURES are not enabled. No need to go any further. 270136e0ab9SAndy Shevchenko */ 271136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_UCV); 272136e0ab9SAndy Shevchenko if (!reg) 273136e0ab9SAndy Shevchenko return; 274136e0ab9SAndy Shevchenko 275136e0ab9SAndy Shevchenko dev_dbg(p->dev, "Designware UART version %c.%c%c\n", 276136e0ab9SAndy Shevchenko (reg >> 24) & 0xff, (reg >> 16) & 0xff, (reg >> 8) & 0xff); 277136e0ab9SAndy Shevchenko 278136e0ab9SAndy Shevchenko dw8250_writel_ext(p, DW_UART_DLF, ~0U); 279136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_DLF); 280136e0ab9SAndy Shevchenko dw8250_writel_ext(p, DW_UART_DLF, 0); 281136e0ab9SAndy Shevchenko 282136e0ab9SAndy Shevchenko if (reg) { 283593dea00SMiquel Raynal pd->dlf_size = fls(reg); 284136e0ab9SAndy Shevchenko p->get_divisor = dw8250_get_divisor; 285136e0ab9SAndy Shevchenko p->set_divisor = dw8250_set_divisor; 286136e0ab9SAndy Shevchenko } 287136e0ab9SAndy Shevchenko 288136e0ab9SAndy Shevchenko reg = dw8250_readl_ext(p, DW_UART_CPR); 289593dea00SMiquel Raynal if (!reg) { 290593dea00SMiquel Raynal reg = data->pdata->cpr_val; 291593dea00SMiquel Raynal dev_dbg(p->dev, "CPR is not available, using 0x%08x instead\n", reg); 292593dea00SMiquel Raynal } 293136e0ab9SAndy Shevchenko if (!reg) 294136e0ab9SAndy Shevchenko return; 295136e0ab9SAndy Shevchenko 296136e0ab9SAndy Shevchenko /* Select the type based on FIFO */ 297136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_FIFO_MODE) { 298136e0ab9SAndy Shevchenko p->type = PORT_16550A; 299136e0ab9SAndy Shevchenko p->flags |= UPF_FIXED_TYPE; 300136e0ab9SAndy Shevchenko p->fifosize = DW_UART_CPR_FIFO_SIZE(reg); 3015ff33917SIlpo Järvinen up->capabilities = UART_CAP_FIFO | UART_CAP_NOTEMT; 302136e0ab9SAndy Shevchenko } 303136e0ab9SAndy Shevchenko 304136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_AFCE_MODE) 305136e0ab9SAndy Shevchenko up->capabilities |= UART_CAP_AFE; 306136e0ab9SAndy Shevchenko 307136e0ab9SAndy Shevchenko if (reg & DW_UART_CPR_SIR_MODE) 308136e0ab9SAndy Shevchenko up->capabilities |= UART_CAP_IRDA; 309136e0ab9SAndy Shevchenko } 310136e0ab9SAndy Shevchenko EXPORT_SYMBOL_GPL(dw8250_setup_port); 311