hc
2024-01-03 2f7c68cb55ecb7331f2381deb497c27155f32faf
kernel/drivers/tty/serial/qcom_geni_serial.c
....@@ -5,10 +5,14 @@
55 #include <linux/console.h>
66 #include <linux/io.h>
77 #include <linux/iopoll.h>
8
+#include <linux/irq.h>
89 #include <linux/module.h>
910 #include <linux/of.h>
1011 #include <linux/of_device.h>
12
+#include <linux/pm_opp.h>
1113 #include <linux/platform_device.h>
14
+#include <linux/pm_runtime.h>
15
+#include <linux/pm_wakeirq.h>
1216 #include <linux/qcom-geni-se.h>
1317 #include <linux/serial.h>
1418 #include <linux/serial_core.h>
....@@ -18,6 +22,7 @@
1822
1923 /* UART specific GENI registers */
2024 #define SE_UART_LOOPBACK_CFG 0x22c
25
+#define SE_UART_IO_MACRO_CTRL 0x240
2126 #define SE_UART_TX_TRANS_CFG 0x25c
2227 #define SE_UART_TX_WORD_LEN 0x268
2328 #define SE_UART_TX_STOP_BIT_LEN 0x26c
....@@ -86,31 +91,53 @@
8691 #define DEF_TX_WM 2
8792 #define DEF_FIFO_WIDTH_BITS 32
8893 #define UART_RX_WM 2
89
-#define MAX_LOOPBACK_CFG 3
9094
91
-#ifdef CONFIG_CONSOLE_POLL
92
-#define RX_BYTES_PW 1
93
-#else
94
-#define RX_BYTES_PW 4
95
-#endif
95
+/* SE_UART_LOOPBACK_CFG */
96
+#define RX_TX_SORTED BIT(0)
97
+#define CTS_RTS_SORTED BIT(1)
98
+#define RX_TX_CTS_RTS_SORTED (RX_TX_SORTED | CTS_RTS_SORTED)
99
+
100
+/* UART pin swap value */
101
+#define DEFAULT_IO_MACRO_IO0_IO1_MASK GENMASK(3, 0)
102
+#define IO_MACRO_IO0_SEL 0x3
103
+#define DEFAULT_IO_MACRO_IO2_IO3_MASK GENMASK(15, 4)
104
+#define IO_MACRO_IO2_IO3_SWAP 0x4640
105
+
106
+/* We always configure 4 bytes per FIFO word */
107
+#define BYTES_PER_FIFO_WORD 4
108
+
109
+struct qcom_geni_private_data {
110
+ /* NOTE: earlycon port will have NULL here */
111
+ struct uart_driver *drv;
112
+
113
+ u32 poll_cached_bytes;
114
+ unsigned int poll_cached_bytes_cnt;
115
+
116
+ u32 write_cached_bytes;
117
+ unsigned int write_cached_bytes_cnt;
118
+};
96119
97120 struct qcom_geni_serial_port {
98121 struct uart_port uport;
99122 struct geni_se se;
100
- char name[20];
123
+ const char *name;
101124 u32 tx_fifo_depth;
102125 u32 tx_fifo_width;
103126 u32 rx_fifo_depth;
104127 bool setup;
128
+ unsigned long clk_rate;
105129 int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
106130 unsigned int baud;
107
- unsigned int tx_bytes_pw;
108
- unsigned int rx_bytes_pw;
109
- u32 *rx_fifo;
131
+ void *rx_fifo;
110132 u32 loopback;
111133 bool brk;
112134
113135 unsigned int tx_remaining;
136
+ int wakeup_irq;
137
+ bool rx_tx_swap;
138
+ bool cts_rts_swap;
139
+
140
+ struct qcom_geni_private_data private_data;
114141 };
115142
116143 static const struct uart_ops qcom_geni_console_pops;
....@@ -124,9 +151,10 @@
124151 static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
125152
126153 static const unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200,
127
- 32000000, 48000000, 64000000, 80000000,
128
- 96000000, 100000000, 102400000,
129
- 112000000, 120000000, 128000000};
154
+ 32000000, 48000000, 51200000, 64000000,
155
+ 80000000, 96000000, 100000000,
156
+ 102400000, 112000000, 120000000,
157
+ 128000000};
130158
131159 #define to_dev_port(ptr, member) \
132160 container_of(ptr, struct qcom_geni_serial_port, member)
....@@ -158,32 +186,6 @@
158186 },
159187 };
160188
161
-static ssize_t loopback_show(struct device *dev,
162
- struct device_attribute *attr, char *buf)
163
-{
164
- struct platform_device *pdev = to_platform_device(dev);
165
- struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
166
-
167
- return snprintf(buf, sizeof(u32), "%d\n", port->loopback);
168
-}
169
-
170
-static ssize_t loopback_store(struct device *dev,
171
- struct device_attribute *attr, const char *buf,
172
- size_t size)
173
-{
174
- struct platform_device *pdev = to_platform_device(dev);
175
- struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
176
- u32 loopback;
177
-
178
- if (kstrtoint(buf, 0, &loopback) || loopback > MAX_LOOPBACK_CFG) {
179
- dev_err(dev, "Invalid input\n");
180
- return -EINVAL;
181
- }
182
- port->loopback = loopback;
183
- return size;
184
-}
185
-static DEVICE_ATTR_RW(loopback);
186
-
187189 static struct qcom_geni_serial_port qcom_geni_console_port = {
188190 .uport = {
189191 .iotype = UPIO_MEM,
....@@ -197,10 +199,8 @@
197199 {
198200 struct platform_device *pdev = to_platform_device(uport->dev);
199201 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
200
- struct resource *res;
201202
202
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203
- uport->membase = devm_ioremap_resource(&pdev->dev, res);
203
+ uport->membase = devm_platform_ioremap_resource(pdev, 0);
204204 if (IS_ERR(uport->membase))
205205 return PTR_ERR(uport->membase);
206206 port->se.base = uport->membase;
....@@ -235,11 +235,15 @@
235235 unsigned int mctrl)
236236 {
237237 u32 uart_manual_rfr = 0;
238
+ struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
238239
239240 if (uart_console(uport))
240241 return;
241242
242
- if (!(mctrl & TIOCM_RTS))
243
+ if (mctrl & TIOCM_LOOP)
244
+ port->loopback = RX_TX_CTS_RTS_SORTED;
245
+
246
+ if (!(mctrl & TIOCM_RTS) && !uport->suspended)
243247 uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
244248 writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
245249 }
....@@ -269,8 +273,9 @@
269273 unsigned int baud;
270274 unsigned int fifo_bits;
271275 unsigned long timeout_us = 20000;
276
+ struct qcom_geni_private_data *private_data = uport->private_data;
272277
273
- if (uport->private_data) {
278
+ if (private_data->drv) {
274279 port = to_dev_port(uport, uport);
275280 baud = port->baud;
276281 if (!baud)
....@@ -336,23 +341,47 @@
336341 }
337342
338343 #ifdef CONFIG_CONSOLE_POLL
344
+
339345 static int qcom_geni_serial_get_char(struct uart_port *uport)
340346 {
341
- u32 rx_fifo;
347
+ struct qcom_geni_private_data *private_data = uport->private_data;
342348 u32 status;
349
+ u32 word_cnt;
350
+ int ret;
343351
344
- status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
345
- writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
352
+ if (!private_data->poll_cached_bytes_cnt) {
353
+ status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
354
+ writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
346355
347
- status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
348
- writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
356
+ status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
357
+ writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
349358
350
- status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
351
- if (!(status & RX_FIFO_WC_MSK))
352
- return NO_POLL_CHAR;
359
+ status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
360
+ word_cnt = status & RX_FIFO_WC_MSK;
361
+ if (!word_cnt)
362
+ return NO_POLL_CHAR;
353363
354
- rx_fifo = readl(uport->membase + SE_GENI_RX_FIFOn);
355
- return rx_fifo & 0xff;
364
+ if (word_cnt == 1 && (status & RX_LAST))
365
+ /*
366
+ * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
367
+ * treated as if it was BYTES_PER_FIFO_WORD.
368
+ */
369
+ private_data->poll_cached_bytes_cnt =
370
+ (status & RX_LAST_BYTE_VALID_MSK) >>
371
+ RX_LAST_BYTE_VALID_SHFT;
372
+
373
+ if (private_data->poll_cached_bytes_cnt == 0)
374
+ private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
375
+
376
+ private_data->poll_cached_bytes =
377
+ readl(uport->membase + SE_GENI_RX_FIFOn);
378
+ }
379
+
380
+ private_data->poll_cached_bytes_cnt--;
381
+ ret = private_data->poll_cached_bytes & 0xff;
382
+ private_data->poll_cached_bytes >>= 8;
383
+
384
+ return ret;
356385 }
357386
358387 static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
....@@ -371,13 +400,25 @@
371400 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
372401 static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch)
373402 {
374
- writel(ch, uport->membase + SE_GENI_TX_FIFOn);
403
+ struct qcom_geni_private_data *private_data = uport->private_data;
404
+
405
+ private_data->write_cached_bytes =
406
+ (private_data->write_cached_bytes >> 8) | (ch << 24);
407
+ private_data->write_cached_bytes_cnt++;
408
+
409
+ if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
410
+ writel(private_data->write_cached_bytes,
411
+ uport->membase + SE_GENI_TX_FIFOn);
412
+ private_data->write_cached_bytes_cnt = 0;
413
+ }
375414 }
376415
377416 static void
378417 __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
379418 unsigned int count)
380419 {
420
+ struct qcom_geni_private_data *private_data = uport->private_data;
421
+
381422 int i;
382423 u32 bytes_to_send = count;
383424
....@@ -412,6 +453,15 @@
412453 SE_GENI_M_IRQ_CLEAR);
413454 i += chars_to_write;
414455 }
456
+
457
+ if (private_data->write_cached_bytes_cnt) {
458
+ private_data->write_cached_bytes >>= BITS_PER_BYTE *
459
+ (BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
460
+ writel(private_data->write_cached_bytes,
461
+ uport->membase + SE_GENI_TX_FIFOn);
462
+ private_data->write_cached_bytes_cnt = 0;
463
+ }
464
+
415465 qcom_geni_serial_poll_tx_done(uport);
416466 }
417467
....@@ -484,7 +534,7 @@
484534 tport = &uport->state->port;
485535 for (i = 0; i < bytes; ) {
486536 int c;
487
- int chunk = min_t(int, bytes - i, port->rx_bytes_pw);
537
+ int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
488538
489539 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
490540 i += chunk;
....@@ -501,7 +551,8 @@
501551 continue;
502552 }
503553
504
- sysrq = uart_handle_sysrq_char(uport, buf[c]);
554
+ sysrq = uart_prepare_sysrq_char(uport, buf[c]);
555
+
505556 if (!sysrq)
506557 tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
507558 }
....@@ -520,7 +571,6 @@
520571
521572 static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
522573 {
523
- unsigned char *buf;
524574 struct tty_port *tport;
525575 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
526576 u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
....@@ -532,8 +582,7 @@
532582 if (drop)
533583 return 0;
534584
535
- buf = (unsigned char *)port->rx_fifo;
536
- ret = tty_insert_flip_string(tport, buf, bytes);
585
+ ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
537586 if (ret != bytes) {
538587 dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
539588 __func__, ret, bytes);
....@@ -665,11 +714,11 @@
665714
666715 if (!word_cnt)
667716 return;
668
- total_bytes = port->rx_bytes_pw * (word_cnt - 1);
717
+ total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
669718 if (last_word_partial && last_word_byte_cnt)
670719 total_bytes += last_word_byte_cnt;
671720 else
672
- total_bytes += port->rx_bytes_pw;
721
+ total_bytes += BYTES_PER_FIFO_WORD;
673722 port->handle_rx(uport, total_bytes, drop);
674723 }
675724
....@@ -702,7 +751,7 @@
702751 }
703752
704753 avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
705
- avail *= port->tx_bytes_pw;
754
+ avail *= BYTES_PER_FIFO_WORD;
706755
707756 tail = xmit->tail;
708757 chunk = min(avail, pending);
....@@ -725,8 +774,8 @@
725774 u8 buf[sizeof(u32)];
726775 int c;
727776
728
- memset(buf, 0, ARRAY_SIZE(buf));
729
- tx_bytes = min_t(size_t, remaining, port->tx_bytes_pw);
777
+ memset(buf, 0, sizeof(buf));
778
+ tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
730779
731780 for (c = 0; c < tx_bytes ; c++) {
732781 buf[c] = xmit->buf[tail++];
....@@ -765,12 +814,12 @@
765814
766815 static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
767816 {
768
- unsigned int m_irq_status;
769
- unsigned int s_irq_status;
770
- unsigned int geni_status;
817
+ u32 m_irq_en;
818
+ u32 m_irq_status;
819
+ u32 s_irq_status;
820
+ u32 geni_status;
771821 struct uart_port *uport = dev;
772822 unsigned long flags;
773
- unsigned int m_irq_en;
774823 bool drop_rx = false;
775824 struct tty_port *tport = &uport->state->port;
776825 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
....@@ -813,13 +862,15 @@
813862 qcom_geni_serial_handle_rx(uport, drop_rx);
814863
815864 out_unlock:
816
- spin_unlock_irqrestore(&uport->lock, flags);
865
+ uart_unlock_and_check_sysrq(uport, flags);
866
+
817867 return IRQ_HANDLED;
818868 }
819869
820
-static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
870
+static int setup_fifos(struct qcom_geni_serial_port *port)
821871 {
822872 struct uart_port *uport;
873
+ u32 old_rx_fifo_depth = port->rx_fifo_depth;
823874
824875 uport = &port->uport;
825876 port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
....@@ -827,35 +878,31 @@
827878 port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
828879 uport->fifosize =
829880 (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
881
+
882
+ if (port->rx_fifo && (old_rx_fifo_depth != port->rx_fifo_depth) && port->rx_fifo_depth) {
883
+ port->rx_fifo = devm_krealloc(uport->dev, port->rx_fifo,
884
+ port->rx_fifo_depth * sizeof(u32),
885
+ GFP_KERNEL);
886
+ if (!port->rx_fifo)
887
+ return -ENOMEM;
888
+ }
889
+
890
+ return 0;
830891 }
831892
832893
833894 static void qcom_geni_serial_shutdown(struct uart_port *uport)
834895 {
835
- unsigned long flags;
836
-
837
- /* Stop the console before stopping the current tx */
838
- if (uart_console(uport))
839
- console_stop(uport->cons);
840
-
841
- free_irq(uport->irq, uport);
842
- spin_lock_irqsave(&uport->lock, flags);
843
- qcom_geni_serial_stop_tx(uport);
844
- qcom_geni_serial_stop_rx(uport);
845
- spin_unlock_irqrestore(&uport->lock, flags);
896
+ disable_irq(uport->irq);
846897 }
847898
848899 static int qcom_geni_serial_port_setup(struct uart_port *uport)
849900 {
850901 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
851
- unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
902
+ u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
852903 u32 proto;
853
-
854
- if (uart_console(uport))
855
- port->tx_bytes_pw = 1;
856
- else
857
- port->tx_bytes_pw = 4;
858
- port->rx_bytes_pw = RX_BYTES_PW;
904
+ u32 pin_swap;
905
+ int ret;
859906
860907 proto = geni_se_read_proto(&port->se);
861908 if (proto != GENI_SE_UART) {
....@@ -865,27 +912,35 @@
865912
866913 qcom_geni_serial_stop_rx(uport);
867914
868
- get_tx_fifo_size(port);
915
+ ret = setup_fifos(port);
916
+ if (ret)
917
+ return ret;
869918
870919 writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
920
+
921
+ pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
922
+ if (port->rx_tx_swap) {
923
+ pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
924
+ pin_swap |= IO_MACRO_IO2_IO3_SWAP;
925
+ }
926
+ if (port->cts_rts_swap) {
927
+ pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
928
+ pin_swap |= IO_MACRO_IO0_SEL;
929
+ }
930
+ /* Configure this register if RX-TX, CTS-RTS pins are swapped */
931
+ if (port->rx_tx_swap || port->cts_rts_swap)
932
+ writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
933
+
871934 /*
872935 * Make an unconditional cancel on the main sequencer to reset
873936 * it else we could end up in data loss scenarios.
874937 */
875938 if (uart_console(uport))
876939 qcom_geni_serial_poll_tx_done(uport);
877
- geni_se_config_packing(&port->se, BITS_PER_BYTE, port->tx_bytes_pw,
878
- false, true, false);
879
- geni_se_config_packing(&port->se, BITS_PER_BYTE, port->rx_bytes_pw,
880
- false, false, true);
940
+ geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
941
+ false, true, true);
881942 geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
882943 geni_se_select_mode(&port->se, GENI_SE_FIFO);
883
- if (!uart_console(uport)) {
884
- port->rx_fifo = devm_kcalloc(uport->dev,
885
- port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
886
- if (!port->rx_fifo)
887
- return -ENOMEM;
888
- }
889944 port->setup = true;
890945
891946 return 0;
....@@ -896,21 +951,14 @@
896951 int ret;
897952 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
898953
899
- scnprintf(port->name, sizeof(port->name),
900
- "qcom_serial_%s%d",
901
- (uart_console(uport) ? "console" : "uart"), uport->line);
902
-
903954 if (!port->setup) {
904955 ret = qcom_geni_serial_port_setup(uport);
905956 if (ret)
906957 return ret;
907958 }
959
+ enable_irq(uport->irq);
908960
909
- ret = request_irq(uport->irq, qcom_geni_serial_isr, IRQF_TRIGGER_HIGH,
910
- port->name, uport);
911
- if (ret)
912
- dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
913
- return ret;
961
+ return 0;
914962 }
915963
916964 static unsigned long get_clk_cfg(unsigned long clk_freq)
....@@ -924,12 +972,13 @@
924972 return 0;
925973 }
926974
927
-static unsigned long get_clk_div_rate(unsigned int baud, unsigned int *clk_div)
975
+static unsigned long get_clk_div_rate(unsigned int baud,
976
+ unsigned int sampling_rate, unsigned int *clk_div)
928977 {
929978 unsigned long ser_clk;
930979 unsigned long desired_clk;
931980
932
- desired_clk = baud * UART_OVERSAMPLING;
981
+ desired_clk = baud * sampling_rate;
933982 ser_clk = get_clk_cfg(desired_clk);
934983 if (!ser_clk) {
935984 pr_err("%s: Can't find matching DFS entry for baud %d\n",
....@@ -945,29 +994,49 @@
945994 struct ktermios *termios, struct ktermios *old)
946995 {
947996 unsigned int baud;
948
- unsigned int bits_per_char;
949
- unsigned int tx_trans_cfg;
950
- unsigned int tx_parity_cfg;
951
- unsigned int rx_trans_cfg;
952
- unsigned int rx_parity_cfg;
953
- unsigned int stop_bit_len;
997
+ u32 bits_per_char;
998
+ u32 tx_trans_cfg;
999
+ u32 tx_parity_cfg;
1000
+ u32 rx_trans_cfg;
1001
+ u32 rx_parity_cfg;
1002
+ u32 stop_bit_len;
9541003 unsigned int clk_div;
955
- unsigned long ser_clk_cfg;
1004
+ u32 ser_clk_cfg;
9561005 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
9571006 unsigned long clk_rate;
1007
+ u32 ver, sampling_rate;
1008
+ unsigned int avg_bw_core;
9581009
9591010 qcom_geni_serial_stop_rx(uport);
9601011 /* baud rate */
9611012 baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
9621013 port->baud = baud;
963
- clk_rate = get_clk_div_rate(baud, &clk_div);
1014
+
1015
+ sampling_rate = UART_OVERSAMPLING;
1016
+ /* Sampling rate is halved for IP versions >= 2.5 */
1017
+ ver = geni_se_get_qup_hw_version(&port->se);
1018
+ if (ver >= QUP_SE_VERSION_2_5)
1019
+ sampling_rate /= 2;
1020
+
1021
+ clk_rate = get_clk_div_rate(baud, sampling_rate, &clk_div);
9641022 if (!clk_rate)
9651023 goto out_restart_rx;
9661024
9671025 uport->uartclk = clk_rate;
968
- clk_set_rate(port->se.clk, clk_rate);
1026
+ port->clk_rate = clk_rate;
1027
+ dev_pm_opp_set_rate(uport->dev, clk_rate);
9691028 ser_clk_cfg = SER_CLK_EN;
9701029 ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1030
+
1031
+ /*
1032
+ * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1033
+ * only.
1034
+ */
1035
+ avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
1036
+ : GENI_DEFAULT_BW;
1037
+ port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
1038
+ port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1039
+ geni_icc_set_bw(&port->se);
9711040
9721041 /* parity */
9731042 tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
....@@ -1054,7 +1123,7 @@
10541123 {
10551124 struct uart_port *uport;
10561125 struct qcom_geni_serial_port *port;
1057
- int baud = 9600;
1126
+ int baud = 115200;
10581127 int bits = 8;
10591128 int parity = 'n';
10601129 int flow = 'n';
....@@ -1094,6 +1163,38 @@
10941163 __qcom_geni_serial_console_write(&dev->port, s, n);
10951164 }
10961165
1166
+#ifdef CONFIG_CONSOLE_POLL
1167
+static int qcom_geni_serial_earlycon_read(struct console *con,
1168
+ char *s, unsigned int n)
1169
+{
1170
+ struct earlycon_device *dev = con->data;
1171
+ struct uart_port *uport = &dev->port;
1172
+ int num_read = 0;
1173
+ int ch;
1174
+
1175
+ while (num_read < n) {
1176
+ ch = qcom_geni_serial_get_char(uport);
1177
+ if (ch == NO_POLL_CHAR)
1178
+ break;
1179
+ s[num_read++] = ch;
1180
+ }
1181
+
1182
+ return num_read;
1183
+}
1184
+
1185
+static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1186
+ struct console *con)
1187
+{
1188
+ geni_se_setup_s_cmd(se, UART_START_READ, 0);
1189
+ con->read = qcom_geni_serial_earlycon_read;
1190
+}
1191
+#else
1192
+static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1193
+ struct console *con) { }
1194
+#endif
1195
+
1196
+static struct qcom_geni_private_data earlycon_private_data;
1197
+
10971198 static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
10981199 const char *opt)
10991200 {
....@@ -1108,6 +1209,8 @@
11081209
11091210 if (!uport->membase)
11101211 return -EINVAL;
1212
+
1213
+ uport->private_data = &earlycon_private_data;
11111214
11121215 memset(&se, 0, sizeof(se));
11131216 se.base = uport->membase;
....@@ -1126,7 +1229,8 @@
11261229 */
11271230 qcom_geni_serial_poll_tx_done(uport);
11281231 qcom_geni_serial_abort_rx(uport);
1129
- geni_se_config_packing(&se, BITS_PER_BYTE, 1, false, true, false);
1232
+ geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1233
+ false, true, true);
11301234 geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
11311235 geni_se_select_mode(&se, GENI_SE_FIFO);
11321236
....@@ -1140,6 +1244,8 @@
11401244
11411245 dev->con->write = qcom_geni_serial_earlycon_write;
11421246 dev->con->setup = NULL;
1247
+ qcom_geni_serial_enable_early_read(&se, dev->con);
1248
+
11431249 return 0;
11441250 }
11451251 OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
....@@ -1199,11 +1305,17 @@
11991305 if (old_state == UART_PM_STATE_UNDEFINED)
12001306 old_state = UART_PM_STATE_OFF;
12011307
1202
- if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF)
1308
+ if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
1309
+ geni_icc_enable(&port->se);
1310
+ if (port->clk_rate)
1311
+ dev_pm_opp_set_rate(uport->dev, port->clk_rate);
12031312 geni_se_resources_on(&port->se);
1204
- else if (new_state == UART_PM_STATE_OFF &&
1205
- old_state == UART_PM_STATE_ON)
1313
+ } else if (new_state == UART_PM_STATE_OFF &&
1314
+ old_state == UART_PM_STATE_ON) {
12061315 geni_se_resources_off(&port->se);
1316
+ dev_pm_opp_set_rate(uport->dev, 0);
1317
+ geni_icc_disable(&port->se);
1318
+ }
12071319 }
12081320
12091321 static const struct uart_ops qcom_geni_console_pops = {
....@@ -1256,14 +1368,12 @@
12561368 if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
12571369 console = true;
12581370
1259
- if (pdev->dev.of_node) {
1260
- if (console) {
1261
- drv = &qcom_geni_console_driver;
1262
- line = of_alias_get_id(pdev->dev.of_node, "serial");
1263
- } else {
1264
- drv = &qcom_geni_uart_driver;
1265
- line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1266
- }
1371
+ if (console) {
1372
+ drv = &qcom_geni_console_driver;
1373
+ line = of_alias_get_id(pdev->dev.of_node, "serial");
1374
+ } else {
1375
+ drv = &qcom_geni_uart_driver;
1376
+ line = of_alias_get_id(pdev->dev.of_node, "hsuart");
12671377 }
12681378
12691379 port = get_port_from_line(line, console);
....@@ -1296,73 +1406,141 @@
12961406 port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
12971407 port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
12981408
1299
- irq = platform_get_irq(pdev, 0);
1300
- if (irq < 0) {
1301
- dev_err(&pdev->dev, "Failed to get IRQ %d\n", irq);
1302
- return irq;
1409
+ if (!console) {
1410
+ port->rx_fifo = devm_kcalloc(uport->dev,
1411
+ port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
1412
+ if (!port->rx_fifo)
1413
+ return -ENOMEM;
13031414 }
1304
- uport->irq = irq;
13051415
1306
- uport->private_data = drv;
1416
+ ret = geni_icc_get(&port->se, NULL);
1417
+ if (ret)
1418
+ return ret;
1419
+ port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
1420
+ port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
1421
+
1422
+ /* Set BW for register access */
1423
+ ret = geni_icc_set_bw(&port->se);
1424
+ if (ret)
1425
+ return ret;
1426
+
1427
+ port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1428
+ "qcom_geni_serial_%s%d",
1429
+ uart_console(uport) ? "console" : "uart", uport->line);
1430
+ if (!port->name)
1431
+ return -ENOMEM;
1432
+
1433
+ irq = platform_get_irq(pdev, 0);
1434
+ if (irq < 0)
1435
+ return irq;
1436
+ uport->irq = irq;
1437
+ uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1438
+
1439
+ if (!console)
1440
+ port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1441
+
1442
+ if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1443
+ port->rx_tx_swap = true;
1444
+
1445
+ if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1446
+ port->cts_rts_swap = true;
1447
+
1448
+ port->se.opp_table = dev_pm_opp_set_clkname(&pdev->dev, "se");
1449
+ if (IS_ERR(port->se.opp_table))
1450
+ return PTR_ERR(port->se.opp_table);
1451
+ /* OPP table is optional */
1452
+ ret = dev_pm_opp_of_add_table(&pdev->dev);
1453
+ if (ret && ret != -ENODEV) {
1454
+ dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1455
+ goto put_clkname;
1456
+ }
1457
+
1458
+ port->private_data.drv = drv;
1459
+ uport->private_data = &port->private_data;
13071460 platform_set_drvdata(pdev, port);
13081461 port->handle_rx = console ? handle_rx_console : handle_rx_uart;
1309
- if (!console)
1310
- device_create_file(uport->dev, &dev_attr_loopback);
1311
- return uart_add_one_port(drv, uport);
1462
+
1463
+ ret = uart_add_one_port(drv, uport);
1464
+ if (ret)
1465
+ goto err;
1466
+
1467
+ irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1468
+ ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1469
+ IRQF_TRIGGER_HIGH, port->name, uport);
1470
+ if (ret) {
1471
+ dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1472
+ uart_remove_one_port(drv, uport);
1473
+ goto err;
1474
+ }
1475
+
1476
+ if (port->wakeup_irq > 0) {
1477
+ device_init_wakeup(&pdev->dev, true);
1478
+ ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1479
+ port->wakeup_irq);
1480
+ if (ret) {
1481
+ device_init_wakeup(&pdev->dev, false);
1482
+ uart_remove_one_port(drv, uport);
1483
+ goto err;
1484
+ }
1485
+ }
1486
+
1487
+ return 0;
1488
+err:
1489
+ dev_pm_opp_of_remove_table(&pdev->dev);
1490
+put_clkname:
1491
+ dev_pm_opp_put_clkname(port->se.opp_table);
1492
+ return ret;
13121493 }
13131494
13141495 static int qcom_geni_serial_remove(struct platform_device *pdev)
13151496 {
13161497 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1317
- struct uart_driver *drv = port->uport.private_data;
1498
+ struct uart_driver *drv = port->private_data.drv;
13181499
1500
+ dev_pm_opp_of_remove_table(&pdev->dev);
1501
+ dev_pm_opp_put_clkname(port->se.opp_table);
1502
+ dev_pm_clear_wake_irq(&pdev->dev);
1503
+ device_init_wakeup(&pdev->dev, false);
13191504 uart_remove_one_port(drv, &port->uport);
1505
+
13201506 return 0;
13211507 }
13221508
1323
-static int __maybe_unused qcom_geni_serial_sys_suspend_noirq(struct device *dev)
1509
+static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
13241510 {
13251511 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
13261512 struct uart_port *uport = &port->uport;
1513
+ struct qcom_geni_private_data *private_data = uport->private_data;
13271514
1515
+ /*
1516
+ * This is done so we can hit the lowest possible state in suspend
1517
+ * even with no_console_suspend
1518
+ */
13281519 if (uart_console(uport)) {
1329
- uart_suspend_port(uport->private_data, uport);
1330
- } else {
1331
- struct uart_state *state = uport->state;
1332
- /*
1333
- * If the port is open, deny system suspend.
1334
- */
1335
- if (state->pm_state == UART_PM_STATE_ON)
1336
- return -EBUSY;
1520
+ geni_icc_set_tag(&port->se, 0x3);
1521
+ geni_icc_set_bw(&port->se);
13371522 }
1338
-
1339
- return 0;
1523
+ return uart_suspend_port(private_data->drv, uport);
13401524 }
13411525
1342
-static int __maybe_unused qcom_geni_serial_sys_resume_noirq(struct device *dev)
1526
+static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
13431527 {
1528
+ int ret;
13441529 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
13451530 struct uart_port *uport = &port->uport;
1531
+ struct qcom_geni_private_data *private_data = uport->private_data;
13461532
1347
- if (uart_console(uport) &&
1348
- console_suspend_enabled && uport->suspended) {
1349
- uart_resume_port(uport->private_data, uport);
1350
- /*
1351
- * uart_suspend_port() invokes port shutdown which in turn
1352
- * frees the irq. uart_resume_port invokes port startup which
1353
- * performs request_irq. The request_irq auto-enables the IRQ.
1354
- * In addition, resume_noirq implicitly enables the IRQ and
1355
- * leads to an unbalanced IRQ enable warning. Disable the IRQ
1356
- * before returning so that the warning is suppressed.
1357
- */
1358
- disable_irq(uport->irq);
1533
+ ret = uart_resume_port(private_data->drv, uport);
1534
+ if (uart_console(uport)) {
1535
+ geni_icc_set_tag(&port->se, 0x7);
1536
+ geni_icc_set_bw(&port->se);
13591537 }
1360
- return 0;
1538
+ return ret;
13611539 }
13621540
13631541 static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1364
- SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend_noirq,
1365
- qcom_geni_serial_sys_resume_noirq)
1542
+ SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1543
+ qcom_geni_serial_sys_resume)
13661544 };
13671545
13681546 static const struct of_device_id qcom_geni_serial_match_table[] = {