forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-02-19 1c055e55a242a33e574e48be530e06770a210dcd
kernel/drivers/tty/serial/ar933x_uart.c
....@@ -13,6 +13,7 @@
1313 #include <linux/console.h>
1414 #include <linux/sysrq.h>
1515 #include <linux/delay.h>
16
+#include <linux/gpio/consumer.h>
1617 #include <linux/platform_device.h>
1718 #include <linux/of.h>
1819 #include <linux/of_platform.h>
....@@ -28,6 +29,8 @@
2829 #include <asm/div64.h>
2930
3031 #include <asm/mach-ath79/ar933x_uart.h>
32
+
33
+#include "serial_mctrl_gpio.h"
3134
3235 #define DRIVER_NAME "ar933x-uart"
3336
....@@ -47,6 +50,8 @@
4750 unsigned int min_baud;
4851 unsigned int max_baud;
4952 struct clk *clk;
53
+ struct mctrl_gpios *gpios;
54
+ struct gpio_desc *rts_gpiod;
5055 };
5156
5257 static inline unsigned int ar933x_uart_read(struct ar933x_uart_port *up,
....@@ -100,6 +105,18 @@
100105 ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
101106 }
102107
108
+static inline void ar933x_uart_start_rx_interrupt(struct ar933x_uart_port *up)
109
+{
110
+ up->ier |= AR933X_UART_INT_RX_VALID;
111
+ ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
112
+}
113
+
114
+static inline void ar933x_uart_stop_rx_interrupt(struct ar933x_uart_port *up)
115
+{
116
+ up->ier &= ~AR933X_UART_INT_RX_VALID;
117
+ ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
118
+}
119
+
103120 static inline void ar933x_uart_putc(struct ar933x_uart_port *up, int ch)
104121 {
105122 unsigned int rdata;
....@@ -125,11 +142,21 @@
125142
126143 static unsigned int ar933x_uart_get_mctrl(struct uart_port *port)
127144 {
128
- return TIOCM_CAR;
145
+ struct ar933x_uart_port *up =
146
+ container_of(port, struct ar933x_uart_port, port);
147
+ int ret = TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
148
+
149
+ mctrl_gpio_get(up->gpios, &ret);
150
+
151
+ return ret;
129152 }
130153
131154 static void ar933x_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
132155 {
156
+ struct ar933x_uart_port *up =
157
+ container_of(port, struct ar933x_uart_port, port);
158
+
159
+ mctrl_gpio_set(up->gpios, mctrl);
133160 }
134161
135162 static void ar933x_uart_start_tx(struct uart_port *port)
....@@ -138,6 +165,37 @@
138165 container_of(port, struct ar933x_uart_port, port);
139166
140167 ar933x_uart_start_tx_interrupt(up);
168
+}
169
+
170
+static void ar933x_uart_wait_tx_complete(struct ar933x_uart_port *up)
171
+{
172
+ unsigned int status;
173
+ unsigned int timeout = 60000;
174
+
175
+ /* Wait up to 60ms for the character(s) to be sent. */
176
+ do {
177
+ status = ar933x_uart_read(up, AR933X_UART_CS_REG);
178
+ if (--timeout == 0)
179
+ break;
180
+ udelay(1);
181
+ } while (status & AR933X_UART_CS_TX_BUSY);
182
+
183
+ if (timeout == 0)
184
+ dev_err(up->port.dev, "waiting for TX timed out\n");
185
+}
186
+
187
+static void ar933x_uart_rx_flush(struct ar933x_uart_port *up)
188
+{
189
+ unsigned int status;
190
+
191
+ /* clear RX_VALID interrupt */
192
+ ar933x_uart_write(up, AR933X_UART_INT_REG, AR933X_UART_INT_RX_VALID);
193
+
194
+ /* remove characters from the RX FIFO */
195
+ do {
196
+ ar933x_uart_write(up, AR933X_UART_DATA_REG, AR933X_UART_DATA_RX_CSR);
197
+ status = ar933x_uart_read(up, AR933X_UART_DATA_REG);
198
+ } while (status & AR933X_UART_DATA_RX_CSR);
141199 }
142200
143201 static void ar933x_uart_stop_tx(struct uart_port *port)
....@@ -153,8 +211,7 @@
153211 struct ar933x_uart_port *up =
154212 container_of(port, struct ar933x_uart_port, port);
155213
156
- up->ier &= ~AR933X_UART_INT_RX_VALID;
157
- ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
214
+ ar933x_uart_stop_rx_interrupt(up);
158215 }
159216
160217 static void ar933x_uart_break_ctl(struct uart_port *port, int break_state)
....@@ -336,10 +393,19 @@
336393 static void ar933x_uart_tx_chars(struct ar933x_uart_port *up)
337394 {
338395 struct circ_buf *xmit = &up->port.state->xmit;
396
+ struct serial_rs485 *rs485conf = &up->port.rs485;
339397 int count;
398
+ bool half_duplex_send = false;
340399
341400 if (uart_tx_stopped(&up->port))
342401 return;
402
+
403
+ if ((rs485conf->flags & SER_RS485_ENABLED) &&
404
+ (up->port.x_char || !uart_circ_empty(xmit))) {
405
+ ar933x_uart_stop_rx_interrupt(up);
406
+ gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_ON_SEND));
407
+ half_duplex_send = true;
408
+ }
343409
344410 count = up->port.fifosize;
345411 do {
....@@ -368,8 +434,14 @@
368434 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
369435 uart_write_wakeup(&up->port);
370436
371
- if (!uart_circ_empty(xmit))
437
+ if (!uart_circ_empty(xmit)) {
372438 ar933x_uart_start_tx_interrupt(up);
439
+ } else if (half_duplex_send) {
440
+ ar933x_uart_wait_tx_complete(up);
441
+ ar933x_uart_rx_flush(up);
442
+ ar933x_uart_start_rx_interrupt(up);
443
+ gpiod_set_value(up->rts_gpiod, !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND));
444
+ }
373445 }
374446
375447 static irqreturn_t ar933x_uart_interrupt(int irq, void *dev_id)
....@@ -427,8 +499,7 @@
427499 AR933X_UART_CS_TX_READY_ORIDE | AR933X_UART_CS_RX_READY_ORIDE);
428500
429501 /* Enable RX interrupts */
430
- up->ier = AR933X_UART_INT_RX_VALID;
431
- ar933x_uart_write(up, AR933X_UART_INT_EN_REG, up->ier);
502
+ ar933x_uart_start_rx_interrupt(up);
432503
433504 spin_unlock_irqrestore(&up->port.lock, flags);
434505
....@@ -510,6 +581,26 @@
510581 .config_port = ar933x_uart_config_port,
511582 .verify_port = ar933x_uart_verify_port,
512583 };
584
+
585
+static int ar933x_config_rs485(struct uart_port *port,
586
+ struct serial_rs485 *rs485conf)
587
+{
588
+ struct ar933x_uart_port *up =
589
+ container_of(port, struct ar933x_uart_port, port);
590
+
591
+ if ((rs485conf->flags & SER_RS485_ENABLED) &&
592
+ !up->rts_gpiod) {
593
+ dev_err(port->dev, "RS485 needs rts-gpio\n");
594
+ return 1;
595
+ }
596
+
597
+ if (rs485conf->flags & SER_RS485_ENABLED)
598
+ gpiod_set_value(up->rts_gpiod,
599
+ !!(rs485conf->flags & SER_RS485_RTS_AFTER_SEND));
600
+
601
+ port->rs485 = *rs485conf;
602
+ return 0;
603
+}
513604
514605 #ifdef CONFIG_SERIAL_AR933X_CONSOLE
515606 static struct ar933x_uart_port *
....@@ -690,6 +781,7 @@
690781 port->regshift = 2;
691782 port->fifosize = AR933X_UART_FIFO_SIZE;
692783 port->ops = &ar933x_uart_ops;
784
+ port->rs485_config = ar933x_config_rs485;
693785
694786 baud = ar933x_uart_get_baud(port->uartclk, AR933X_UART_MAX_SCALE, 1);
695787 up->min_baud = max_t(unsigned int, baud, AR933X_UART_MIN_BAUD);
....@@ -697,6 +789,24 @@
697789 baud = ar933x_uart_get_baud(port->uartclk, 0, AR933X_UART_MAX_STEP);
698790 up->max_baud = min_t(unsigned int, baud, AR933X_UART_MAX_BAUD);
699791
792
+ ret = uart_get_rs485_mode(port);
793
+ if (ret)
794
+ goto err_disable_clk;
795
+
796
+ up->gpios = mctrl_gpio_init(port, 0);
797
+ if (IS_ERR(up->gpios) && PTR_ERR(up->gpios) != -ENOSYS) {
798
+ ret = PTR_ERR(up->gpios);
799
+ goto err_disable_clk;
800
+ }
801
+
802
+ up->rts_gpiod = mctrl_gpio_to_gpiod(up->gpios, UART_GPIO_RTS);
803
+
804
+ if ((port->rs485.flags & SER_RS485_ENABLED) &&
805
+ !up->rts_gpiod) {
806
+ dev_err(&pdev->dev, "lacking rts-gpio, disabling RS485\n");
807
+ port->rs485.flags &= ~SER_RS485_ENABLED;
808
+ }
809
+
700810 #ifdef CONFIG_SERIAL_AR933X_CONSOLE
701811 ar933x_console_ports[up->port.line] = up;
702812 #endif