1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3
4 #include <linux/clk.h>
5 #include <linux/console.h>
6 #include <linux/io.h>
7 #include <linux/iopoll.h>
8 #include <linux/irq.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/of_device.h>
12 #include <linux/pm_opp.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_wakeirq.h>
16 #include <linux/qcom-geni-se.h>
17 #include <linux/serial.h>
18 #include <linux/serial_core.h>
19 #include <linux/slab.h>
20 #include <linux/tty.h>
21 #include <linux/tty_flip.h>
22
23 /* UART specific GENI registers */
24 #define SE_UART_LOOPBACK_CFG 0x22c
25 #define SE_UART_IO_MACRO_CTRL 0x240
26 #define SE_UART_TX_TRANS_CFG 0x25c
27 #define SE_UART_TX_WORD_LEN 0x268
28 #define SE_UART_TX_STOP_BIT_LEN 0x26c
29 #define SE_UART_TX_TRANS_LEN 0x270
30 #define SE_UART_RX_TRANS_CFG 0x280
31 #define SE_UART_RX_WORD_LEN 0x28c
32 #define SE_UART_RX_STALE_CNT 0x294
33 #define SE_UART_TX_PARITY_CFG 0x2a4
34 #define SE_UART_RX_PARITY_CFG 0x2a8
35 #define SE_UART_MANUAL_RFR 0x2ac
36
37 /* SE_UART_TRANS_CFG */
38 #define UART_TX_PAR_EN BIT(0)
39 #define UART_CTS_MASK BIT(1)
40
41 /* SE_UART_TX_WORD_LEN */
42 #define TX_WORD_LEN_MSK GENMASK(9, 0)
43
44 /* SE_UART_TX_STOP_BIT_LEN */
45 #define TX_STOP_BIT_LEN_MSK GENMASK(23, 0)
46 #define TX_STOP_BIT_LEN_1 0
47 #define TX_STOP_BIT_LEN_1_5 1
48 #define TX_STOP_BIT_LEN_2 2
49
50 /* SE_UART_TX_TRANS_LEN */
51 #define TX_TRANS_LEN_MSK GENMASK(23, 0)
52
53 /* SE_UART_RX_TRANS_CFG */
54 #define UART_RX_INS_STATUS_BIT BIT(2)
55 #define UART_RX_PAR_EN BIT(3)
56
57 /* SE_UART_RX_WORD_LEN */
58 #define RX_WORD_LEN_MASK GENMASK(9, 0)
59
60 /* SE_UART_RX_STALE_CNT */
61 #define RX_STALE_CNT GENMASK(23, 0)
62
63 /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */
64 #define PAR_CALC_EN BIT(0)
65 #define PAR_MODE_MSK GENMASK(2, 1)
66 #define PAR_MODE_SHFT 1
67 #define PAR_EVEN 0x00
68 #define PAR_ODD 0x01
69 #define PAR_SPACE 0x10
70 #define PAR_MARK 0x11
71
72 /* SE_UART_MANUAL_RFR register fields */
73 #define UART_MANUAL_RFR_EN BIT(31)
74 #define UART_RFR_NOT_READY BIT(1)
75 #define UART_RFR_READY BIT(0)
76
77 /* UART M_CMD OP codes */
78 #define UART_START_TX 0x1
79 #define UART_START_BREAK 0x4
80 #define UART_STOP_BREAK 0x5
81 /* UART S_CMD OP codes */
82 #define UART_START_READ 0x1
83 #define UART_PARAM 0x1
84
85 #define UART_OVERSAMPLING 32
86 #define STALE_TIMEOUT 16
87 #define DEFAULT_BITS_PER_CHAR 10
88 #define GENI_UART_CONS_PORTS 1
89 #define GENI_UART_PORTS 3
90 #define DEF_FIFO_DEPTH_WORDS 16
91 #define DEF_TX_WM 2
92 #define DEF_FIFO_WIDTH_BITS 32
93 #define UART_RX_WM 2
94
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 };
119
120 struct qcom_geni_serial_port {
121 struct uart_port uport;
122 struct geni_se se;
123 const char *name;
124 u32 tx_fifo_depth;
125 u32 tx_fifo_width;
126 u32 rx_fifo_depth;
127 bool setup;
128 int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop);
129 unsigned int baud;
130 void *rx_fifo;
131 u32 loopback;
132 bool brk;
133
134 unsigned int tx_remaining;
135 int wakeup_irq;
136 bool rx_tx_swap;
137 bool cts_rts_swap;
138
139 struct qcom_geni_private_data private_data;
140 };
141
142 static const struct uart_ops qcom_geni_console_pops;
143 static const struct uart_ops qcom_geni_uart_pops;
144 static struct uart_driver qcom_geni_console_driver;
145 static struct uart_driver qcom_geni_uart_driver;
146 static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop);
147 static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop);
148 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port);
149 static void qcom_geni_serial_stop_rx(struct uart_port *uport);
150 static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop);
151
152 #define to_dev_port(ptr, member) \
153 container_of(ptr, struct qcom_geni_serial_port, member)
154
155 static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = {
156 [0] = {
157 .uport = {
158 .iotype = UPIO_MEM,
159 .ops = &qcom_geni_uart_pops,
160 .flags = UPF_BOOT_AUTOCONF,
161 .line = 0,
162 },
163 },
164 [1] = {
165 .uport = {
166 .iotype = UPIO_MEM,
167 .ops = &qcom_geni_uart_pops,
168 .flags = UPF_BOOT_AUTOCONF,
169 .line = 1,
170 },
171 },
172 [2] = {
173 .uport = {
174 .iotype = UPIO_MEM,
175 .ops = &qcom_geni_uart_pops,
176 .flags = UPF_BOOT_AUTOCONF,
177 .line = 2,
178 },
179 },
180 };
181
182 static struct qcom_geni_serial_port qcom_geni_console_port = {
183 .uport = {
184 .iotype = UPIO_MEM,
185 .ops = &qcom_geni_console_pops,
186 .flags = UPF_BOOT_AUTOCONF,
187 .line = 0,
188 },
189 };
190
qcom_geni_serial_request_port(struct uart_port * uport)191 static int qcom_geni_serial_request_port(struct uart_port *uport)
192 {
193 struct platform_device *pdev = to_platform_device(uport->dev);
194 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
195
196 uport->membase = devm_platform_ioremap_resource(pdev, 0);
197 if (IS_ERR(uport->membase))
198 return PTR_ERR(uport->membase);
199 port->se.base = uport->membase;
200 return 0;
201 }
202
qcom_geni_serial_config_port(struct uart_port * uport,int cfg_flags)203 static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags)
204 {
205 if (cfg_flags & UART_CONFIG_TYPE) {
206 uport->type = PORT_MSM;
207 qcom_geni_serial_request_port(uport);
208 }
209 }
210
qcom_geni_serial_get_mctrl(struct uart_port * uport)211 static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport)
212 {
213 unsigned int mctrl = TIOCM_DSR | TIOCM_CAR;
214 u32 geni_ios;
215
216 if (uart_console(uport)) {
217 mctrl |= TIOCM_CTS;
218 } else {
219 geni_ios = readl(uport->membase + SE_GENI_IOS);
220 if (!(geni_ios & IO2_DATA_IN))
221 mctrl |= TIOCM_CTS;
222 }
223
224 return mctrl;
225 }
226
qcom_geni_serial_set_mctrl(struct uart_port * uport,unsigned int mctrl)227 static void qcom_geni_serial_set_mctrl(struct uart_port *uport,
228 unsigned int mctrl)
229 {
230 u32 uart_manual_rfr = 0;
231 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
232
233 if (uart_console(uport))
234 return;
235
236 if (mctrl & TIOCM_LOOP)
237 port->loopback = RX_TX_CTS_RTS_SORTED;
238
239 if (!(mctrl & TIOCM_RTS) && !uport->suspended)
240 uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY;
241 writel(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR);
242 }
243
qcom_geni_serial_get_type(struct uart_port * uport)244 static const char *qcom_geni_serial_get_type(struct uart_port *uport)
245 {
246 return "MSM";
247 }
248
get_port_from_line(int line,bool console)249 static struct qcom_geni_serial_port *get_port_from_line(int line, bool console)
250 {
251 struct qcom_geni_serial_port *port;
252 int nr_ports = console ? GENI_UART_CONS_PORTS : GENI_UART_PORTS;
253
254 if (line < 0 || line >= nr_ports)
255 return ERR_PTR(-ENXIO);
256
257 port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line];
258 return port;
259 }
260
qcom_geni_serial_poll_bit(struct uart_port * uport,int offset,int field,bool set)261 static bool qcom_geni_serial_poll_bit(struct uart_port *uport,
262 int offset, int field, bool set)
263 {
264 u32 reg;
265 struct qcom_geni_serial_port *port;
266 unsigned int baud;
267 unsigned int fifo_bits;
268 unsigned long timeout_us = 20000;
269 struct qcom_geni_private_data *private_data = uport->private_data;
270
271 if (private_data->drv) {
272 port = to_dev_port(uport, uport);
273 baud = port->baud;
274 if (!baud)
275 baud = 115200;
276 fifo_bits = port->tx_fifo_depth * port->tx_fifo_width;
277 /*
278 * Total polling iterations based on FIFO worth of bytes to be
279 * sent at current baud. Add a little fluff to the wait.
280 */
281 timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500;
282 }
283
284 /*
285 * Use custom implementation instead of readl_poll_atomic since ktimer
286 * is not ready at the time of early console.
287 */
288 timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10;
289 while (timeout_us) {
290 reg = readl(uport->membase + offset);
291 if ((bool)(reg & field) == set)
292 return true;
293 udelay(10);
294 timeout_us -= 10;
295 }
296 return false;
297 }
298
qcom_geni_serial_setup_tx(struct uart_port * uport,u32 xmit_size)299 static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size)
300 {
301 u32 m_cmd;
302
303 writel(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN);
304 m_cmd = UART_START_TX << M_OPCODE_SHFT;
305 writel(m_cmd, uport->membase + SE_GENI_M_CMD0);
306 }
307
qcom_geni_serial_poll_tx_done(struct uart_port * uport)308 static void qcom_geni_serial_poll_tx_done(struct uart_port *uport)
309 {
310 int done;
311 u32 irq_clear = M_CMD_DONE_EN;
312
313 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
314 M_CMD_DONE_EN, true);
315 if (!done) {
316 writel(M_GENI_CMD_ABORT, uport->membase +
317 SE_GENI_M_CMD_CTRL_REG);
318 irq_clear |= M_CMD_ABORT_EN;
319 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
320 M_CMD_ABORT_EN, true);
321 }
322 writel(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR);
323 }
324
qcom_geni_serial_abort_rx(struct uart_port * uport)325 static void qcom_geni_serial_abort_rx(struct uart_port *uport)
326 {
327 u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN;
328
329 writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG);
330 qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG,
331 S_GENI_CMD_ABORT, false);
332 writel(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR);
333 writel(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG);
334 }
335
336 #ifdef CONFIG_CONSOLE_POLL
337
qcom_geni_serial_get_char(struct uart_port * uport)338 static int qcom_geni_serial_get_char(struct uart_port *uport)
339 {
340 struct qcom_geni_private_data *private_data = uport->private_data;
341 u32 status;
342 u32 word_cnt;
343 int ret;
344
345 if (!private_data->poll_cached_bytes_cnt) {
346 status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
347 writel(status, uport->membase + SE_GENI_M_IRQ_CLEAR);
348
349 status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
350 writel(status, uport->membase + SE_GENI_S_IRQ_CLEAR);
351
352 status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
353 word_cnt = status & RX_FIFO_WC_MSK;
354 if (!word_cnt)
355 return NO_POLL_CHAR;
356
357 if (word_cnt == 1 && (status & RX_LAST))
358 /*
359 * NOTE: If RX_LAST_BYTE_VALID is 0 it needs to be
360 * treated as if it was BYTES_PER_FIFO_WORD.
361 */
362 private_data->poll_cached_bytes_cnt =
363 (status & RX_LAST_BYTE_VALID_MSK) >>
364 RX_LAST_BYTE_VALID_SHFT;
365
366 if (private_data->poll_cached_bytes_cnt == 0)
367 private_data->poll_cached_bytes_cnt = BYTES_PER_FIFO_WORD;
368
369 private_data->poll_cached_bytes =
370 readl(uport->membase + SE_GENI_RX_FIFOn);
371 }
372
373 private_data->poll_cached_bytes_cnt--;
374 ret = private_data->poll_cached_bytes & 0xff;
375 private_data->poll_cached_bytes >>= 8;
376
377 return ret;
378 }
379
qcom_geni_serial_poll_put_char(struct uart_port * uport,unsigned char c)380 static void qcom_geni_serial_poll_put_char(struct uart_port *uport,
381 unsigned char c)
382 {
383 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
384 qcom_geni_serial_setup_tx(uport, 1);
385 WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
386 M_TX_FIFO_WATERMARK_EN, true));
387 writel(c, uport->membase + SE_GENI_TX_FIFOn);
388 writel(M_TX_FIFO_WATERMARK_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
389 qcom_geni_serial_poll_tx_done(uport);
390 }
391 #endif
392
393 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
qcom_geni_serial_wr_char(struct uart_port * uport,unsigned char ch)394 static void qcom_geni_serial_wr_char(struct uart_port *uport, unsigned char ch)
395 {
396 struct qcom_geni_private_data *private_data = uport->private_data;
397
398 private_data->write_cached_bytes =
399 (private_data->write_cached_bytes >> 8) | (ch << 24);
400 private_data->write_cached_bytes_cnt++;
401
402 if (private_data->write_cached_bytes_cnt == BYTES_PER_FIFO_WORD) {
403 writel(private_data->write_cached_bytes,
404 uport->membase + SE_GENI_TX_FIFOn);
405 private_data->write_cached_bytes_cnt = 0;
406 }
407 }
408
409 static void
__qcom_geni_serial_console_write(struct uart_port * uport,const char * s,unsigned int count)410 __qcom_geni_serial_console_write(struct uart_port *uport, const char *s,
411 unsigned int count)
412 {
413 struct qcom_geni_private_data *private_data = uport->private_data;
414
415 int i;
416 u32 bytes_to_send = count;
417
418 for (i = 0; i < count; i++) {
419 /*
420 * uart_console_write() adds a carriage return for each newline.
421 * Account for additional bytes to be written.
422 */
423 if (s[i] == '\n')
424 bytes_to_send++;
425 }
426
427 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
428 qcom_geni_serial_setup_tx(uport, bytes_to_send);
429 for (i = 0; i < count; ) {
430 size_t chars_to_write = 0;
431 size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM;
432
433 /*
434 * If the WM bit never set, then the Tx state machine is not
435 * in a valid state, so break, cancel/abort any existing
436 * command. Unfortunately the current data being written is
437 * lost.
438 */
439 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
440 M_TX_FIFO_WATERMARK_EN, true))
441 break;
442 chars_to_write = min_t(size_t, count - i, avail / 2);
443 uart_console_write(uport, s + i, chars_to_write,
444 qcom_geni_serial_wr_char);
445 writel(M_TX_FIFO_WATERMARK_EN, uport->membase +
446 SE_GENI_M_IRQ_CLEAR);
447 i += chars_to_write;
448 }
449
450 if (private_data->write_cached_bytes_cnt) {
451 private_data->write_cached_bytes >>= BITS_PER_BYTE *
452 (BYTES_PER_FIFO_WORD - private_data->write_cached_bytes_cnt);
453 writel(private_data->write_cached_bytes,
454 uport->membase + SE_GENI_TX_FIFOn);
455 private_data->write_cached_bytes_cnt = 0;
456 }
457
458 qcom_geni_serial_poll_tx_done(uport);
459 }
460
qcom_geni_serial_console_write(struct console * co,const char * s,unsigned int count)461 static void qcom_geni_serial_console_write(struct console *co, const char *s,
462 unsigned int count)
463 {
464 struct uart_port *uport;
465 struct qcom_geni_serial_port *port;
466 bool locked = true;
467 unsigned long flags;
468 u32 geni_status;
469 u32 irq_en;
470
471 WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS);
472
473 port = get_port_from_line(co->index, true);
474 if (IS_ERR(port))
475 return;
476
477 uport = &port->uport;
478 if (oops_in_progress)
479 locked = spin_trylock_irqsave(&uport->lock, flags);
480 else
481 spin_lock_irqsave(&uport->lock, flags);
482
483 geni_status = readl(uport->membase + SE_GENI_STATUS);
484
485 /* Cancel the current write to log the fault */
486 if (!locked) {
487 geni_se_cancel_m_cmd(&port->se);
488 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
489 M_CMD_CANCEL_EN, true)) {
490 geni_se_abort_m_cmd(&port->se);
491 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
492 M_CMD_ABORT_EN, true);
493 writel(M_CMD_ABORT_EN, uport->membase +
494 SE_GENI_M_IRQ_CLEAR);
495 }
496 writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
497 } else if ((geni_status & M_GENI_CMD_ACTIVE) && !port->tx_remaining) {
498 /*
499 * It seems we can't interrupt existing transfers if all data
500 * has been sent, in which case we need to look for done first.
501 */
502 qcom_geni_serial_poll_tx_done(uport);
503
504 if (!uart_circ_empty(&uport->state->xmit)) {
505 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
506 writel(irq_en | M_TX_FIFO_WATERMARK_EN,
507 uport->membase + SE_GENI_M_IRQ_EN);
508 }
509 }
510
511 __qcom_geni_serial_console_write(uport, s, count);
512
513 if (port->tx_remaining)
514 qcom_geni_serial_setup_tx(uport, port->tx_remaining);
515
516 if (locked)
517 spin_unlock_irqrestore(&uport->lock, flags);
518 }
519
handle_rx_console(struct uart_port * uport,u32 bytes,bool drop)520 static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
521 {
522 u32 i;
523 unsigned char buf[sizeof(u32)];
524 struct tty_port *tport;
525 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
526
527 tport = &uport->state->port;
528 for (i = 0; i < bytes; ) {
529 int c;
530 int chunk = min_t(int, bytes - i, BYTES_PER_FIFO_WORD);
531
532 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1);
533 i += chunk;
534 if (drop)
535 continue;
536
537 for (c = 0; c < chunk; c++) {
538 int sysrq;
539
540 uport->icount.rx++;
541 if (port->brk && buf[c] == 0) {
542 port->brk = false;
543 if (uart_handle_break(uport))
544 continue;
545 }
546
547 sysrq = uart_prepare_sysrq_char(uport, buf[c]);
548
549 if (!sysrq)
550 tty_insert_flip_char(tport, buf[c], TTY_NORMAL);
551 }
552 }
553 if (!drop)
554 tty_flip_buffer_push(tport);
555 return 0;
556 }
557 #else
handle_rx_console(struct uart_port * uport,u32 bytes,bool drop)558 static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop)
559 {
560 return -EPERM;
561 }
562
563 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
564
handle_rx_uart(struct uart_port * uport,u32 bytes,bool drop)565 static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop)
566 {
567 struct tty_port *tport;
568 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
569 u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE;
570 u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw;
571 int ret;
572
573 tport = &uport->state->port;
574 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words);
575 if (drop)
576 return 0;
577
578 ret = tty_insert_flip_string(tport, port->rx_fifo, bytes);
579 if (ret != bytes) {
580 dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n",
581 __func__, ret, bytes);
582 WARN_ON_ONCE(1);
583 }
584 uport->icount.rx += ret;
585 tty_flip_buffer_push(tport);
586 return ret;
587 }
588
qcom_geni_serial_start_tx(struct uart_port * uport)589 static void qcom_geni_serial_start_tx(struct uart_port *uport)
590 {
591 u32 irq_en;
592 u32 status;
593
594 status = readl(uport->membase + SE_GENI_STATUS);
595 if (status & M_GENI_CMD_ACTIVE)
596 return;
597
598 if (!qcom_geni_serial_tx_empty(uport))
599 return;
600
601 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
602 irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN;
603
604 writel(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG);
605 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
606 }
607
qcom_geni_serial_stop_tx(struct uart_port * uport)608 static void qcom_geni_serial_stop_tx(struct uart_port *uport)
609 {
610 u32 irq_en;
611 u32 status;
612 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
613
614 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
615 irq_en &= ~(M_CMD_DONE_EN | M_TX_FIFO_WATERMARK_EN);
616 writel(0, uport->membase + SE_GENI_TX_WATERMARK_REG);
617 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
618 status = readl(uport->membase + SE_GENI_STATUS);
619 /* Possible stop tx is called multiple times. */
620 if (!(status & M_GENI_CMD_ACTIVE))
621 return;
622
623 geni_se_cancel_m_cmd(&port->se);
624 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
625 M_CMD_CANCEL_EN, true)) {
626 geni_se_abort_m_cmd(&port->se);
627 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS,
628 M_CMD_ABORT_EN, true);
629 writel(M_CMD_ABORT_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
630 }
631 writel(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR);
632 }
633
qcom_geni_serial_start_rx(struct uart_port * uport)634 static void qcom_geni_serial_start_rx(struct uart_port *uport)
635 {
636 u32 irq_en;
637 u32 status;
638 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
639
640 status = readl(uport->membase + SE_GENI_STATUS);
641 if (status & S_GENI_CMD_ACTIVE)
642 qcom_geni_serial_stop_rx(uport);
643
644 geni_se_setup_s_cmd(&port->se, UART_START_READ, 0);
645
646 irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
647 irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN;
648 writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
649
650 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
651 irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN;
652 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
653 }
654
qcom_geni_serial_stop_rx(struct uart_port * uport)655 static void qcom_geni_serial_stop_rx(struct uart_port *uport)
656 {
657 u32 irq_en;
658 u32 status;
659 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
660 u32 s_irq_status;
661
662 irq_en = readl(uport->membase + SE_GENI_S_IRQ_EN);
663 irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN);
664 writel(irq_en, uport->membase + SE_GENI_S_IRQ_EN);
665
666 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
667 irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN);
668 writel(irq_en, uport->membase + SE_GENI_M_IRQ_EN);
669
670 status = readl(uport->membase + SE_GENI_STATUS);
671 /* Possible stop rx is called multiple times. */
672 if (!(status & S_GENI_CMD_ACTIVE))
673 return;
674
675 geni_se_cancel_s_cmd(&port->se);
676 qcom_geni_serial_poll_bit(uport, SE_GENI_S_IRQ_STATUS,
677 S_CMD_CANCEL_EN, true);
678 /*
679 * If timeout occurs secondary engine remains active
680 * and Abort sequence is executed.
681 */
682 s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
683 /* Flush the Rx buffer */
684 if (s_irq_status & S_RX_FIFO_LAST_EN)
685 qcom_geni_serial_handle_rx(uport, true);
686 writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
687
688 status = readl(uport->membase + SE_GENI_STATUS);
689 if (status & S_GENI_CMD_ACTIVE)
690 qcom_geni_serial_abort_rx(uport);
691 }
692
qcom_geni_serial_handle_rx(struct uart_port * uport,bool drop)693 static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop)
694 {
695 u32 status;
696 u32 word_cnt;
697 u32 last_word_byte_cnt;
698 u32 last_word_partial;
699 u32 total_bytes;
700 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
701
702 status = readl(uport->membase + SE_GENI_RX_FIFO_STATUS);
703 word_cnt = status & RX_FIFO_WC_MSK;
704 last_word_partial = status & RX_LAST;
705 last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >>
706 RX_LAST_BYTE_VALID_SHFT;
707
708 if (!word_cnt)
709 return;
710 total_bytes = BYTES_PER_FIFO_WORD * (word_cnt - 1);
711 if (last_word_partial && last_word_byte_cnt)
712 total_bytes += last_word_byte_cnt;
713 else
714 total_bytes += BYTES_PER_FIFO_WORD;
715 port->handle_rx(uport, total_bytes, drop);
716 }
717
qcom_geni_serial_handle_tx(struct uart_port * uport,bool done,bool active)718 static void qcom_geni_serial_handle_tx(struct uart_port *uport, bool done,
719 bool active)
720 {
721 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
722 struct circ_buf *xmit = &uport->state->xmit;
723 size_t avail;
724 size_t remaining;
725 size_t pending;
726 int i;
727 u32 status;
728 u32 irq_en;
729 unsigned int chunk;
730 int tail;
731
732 status = readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
733
734 /* Complete the current tx command before taking newly added data */
735 if (active)
736 pending = port->tx_remaining;
737 else
738 pending = uart_circ_chars_pending(xmit);
739
740 /* All data has been transmitted and acknowledged as received */
741 if (!pending && !status && done) {
742 qcom_geni_serial_stop_tx(uport);
743 goto out_write_wakeup;
744 }
745
746 avail = port->tx_fifo_depth - (status & TX_FIFO_WC);
747 avail *= BYTES_PER_FIFO_WORD;
748
749 tail = xmit->tail;
750 chunk = min(avail, pending);
751 if (!chunk)
752 goto out_write_wakeup;
753
754 if (!port->tx_remaining) {
755 qcom_geni_serial_setup_tx(uport, pending);
756 port->tx_remaining = pending;
757
758 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
759 if (!(irq_en & M_TX_FIFO_WATERMARK_EN))
760 writel(irq_en | M_TX_FIFO_WATERMARK_EN,
761 uport->membase + SE_GENI_M_IRQ_EN);
762 }
763
764 remaining = chunk;
765 for (i = 0; i < chunk; ) {
766 unsigned int tx_bytes;
767 u8 buf[sizeof(u32)];
768 int c;
769
770 memset(buf, 0, sizeof(buf));
771 tx_bytes = min_t(size_t, remaining, BYTES_PER_FIFO_WORD);
772
773 for (c = 0; c < tx_bytes ; c++) {
774 buf[c] = xmit->buf[tail++];
775 tail &= UART_XMIT_SIZE - 1;
776 }
777
778 iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1);
779
780 i += tx_bytes;
781 uport->icount.tx += tx_bytes;
782 remaining -= tx_bytes;
783 port->tx_remaining -= tx_bytes;
784 }
785
786 xmit->tail = tail;
787
788 /*
789 * The tx fifo watermark is level triggered and latched. Though we had
790 * cleared it in qcom_geni_serial_isr it will have already reasserted
791 * so we must clear it again here after our writes.
792 */
793 writel(M_TX_FIFO_WATERMARK_EN,
794 uport->membase + SE_GENI_M_IRQ_CLEAR);
795
796 out_write_wakeup:
797 if (!port->tx_remaining) {
798 irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
799 if (irq_en & M_TX_FIFO_WATERMARK_EN)
800 writel(irq_en & ~M_TX_FIFO_WATERMARK_EN,
801 uport->membase + SE_GENI_M_IRQ_EN);
802 }
803
804 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
805 uart_write_wakeup(uport);
806 }
807
qcom_geni_serial_isr(int isr,void * dev)808 static irqreturn_t qcom_geni_serial_isr(int isr, void *dev)
809 {
810 u32 m_irq_en;
811 u32 m_irq_status;
812 u32 s_irq_status;
813 u32 geni_status;
814 struct uart_port *uport = dev;
815 bool drop_rx = false;
816 struct tty_port *tport = &uport->state->port;
817 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
818
819 if (uport->suspended)
820 return IRQ_NONE;
821
822 spin_lock(&uport->lock);
823
824 m_irq_status = readl(uport->membase + SE_GENI_M_IRQ_STATUS);
825 s_irq_status = readl(uport->membase + SE_GENI_S_IRQ_STATUS);
826 geni_status = readl(uport->membase + SE_GENI_STATUS);
827 m_irq_en = readl(uport->membase + SE_GENI_M_IRQ_EN);
828 writel(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR);
829 writel(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR);
830
831 if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN))
832 goto out_unlock;
833
834 if (s_irq_status & S_RX_FIFO_WR_ERR_EN) {
835 uport->icount.overrun++;
836 tty_insert_flip_char(tport, 0, TTY_OVERRUN);
837 }
838
839 if (m_irq_status & m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN))
840 qcom_geni_serial_handle_tx(uport, m_irq_status & M_CMD_DONE_EN,
841 geni_status & M_GENI_CMD_ACTIVE);
842
843 if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) {
844 if (s_irq_status & S_GP_IRQ_0_EN)
845 uport->icount.parity++;
846 drop_rx = true;
847 } else if (s_irq_status & S_GP_IRQ_2_EN ||
848 s_irq_status & S_GP_IRQ_3_EN) {
849 uport->icount.brk++;
850 port->brk = true;
851 }
852
853 if (s_irq_status & S_RX_FIFO_WATERMARK_EN ||
854 s_irq_status & S_RX_FIFO_LAST_EN)
855 qcom_geni_serial_handle_rx(uport, drop_rx);
856
857 out_unlock:
858 uart_unlock_and_check_sysrq(uport);
859
860 return IRQ_HANDLED;
861 }
862
get_tx_fifo_size(struct qcom_geni_serial_port * port)863 static void get_tx_fifo_size(struct qcom_geni_serial_port *port)
864 {
865 struct uart_port *uport;
866
867 uport = &port->uport;
868 port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se);
869 port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se);
870 port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se);
871 uport->fifosize =
872 (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE;
873 }
874
875
qcom_geni_serial_shutdown(struct uart_port * uport)876 static void qcom_geni_serial_shutdown(struct uart_port *uport)
877 {
878 disable_irq(uport->irq);
879 }
880
qcom_geni_serial_port_setup(struct uart_port * uport)881 static int qcom_geni_serial_port_setup(struct uart_port *uport)
882 {
883 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
884 u32 rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT;
885 u32 proto;
886 u32 pin_swap;
887
888 proto = geni_se_read_proto(&port->se);
889 if (proto != GENI_SE_UART) {
890 dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto);
891 return -ENXIO;
892 }
893
894 qcom_geni_serial_stop_rx(uport);
895
896 get_tx_fifo_size(port);
897
898 writel(rxstale, uport->membase + SE_UART_RX_STALE_CNT);
899
900 pin_swap = readl(uport->membase + SE_UART_IO_MACRO_CTRL);
901 if (port->rx_tx_swap) {
902 pin_swap &= ~DEFAULT_IO_MACRO_IO2_IO3_MASK;
903 pin_swap |= IO_MACRO_IO2_IO3_SWAP;
904 }
905 if (port->cts_rts_swap) {
906 pin_swap &= ~DEFAULT_IO_MACRO_IO0_IO1_MASK;
907 pin_swap |= IO_MACRO_IO0_SEL;
908 }
909 /* Configure this register if RX-TX, CTS-RTS pins are swapped */
910 if (port->rx_tx_swap || port->cts_rts_swap)
911 writel(pin_swap, uport->membase + SE_UART_IO_MACRO_CTRL);
912
913 /*
914 * Make an unconditional cancel on the main sequencer to reset
915 * it else we could end up in data loss scenarios.
916 */
917 if (uart_console(uport))
918 qcom_geni_serial_poll_tx_done(uport);
919 geni_se_config_packing(&port->se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
920 false, true, true);
921 geni_se_init(&port->se, UART_RX_WM, port->rx_fifo_depth - 2);
922 geni_se_select_mode(&port->se, GENI_SE_FIFO);
923 port->setup = true;
924
925 return 0;
926 }
927
qcom_geni_serial_startup(struct uart_port * uport)928 static int qcom_geni_serial_startup(struct uart_port *uport)
929 {
930 int ret;
931 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
932
933 if (!port->setup) {
934 ret = qcom_geni_serial_port_setup(uport);
935 if (ret)
936 return ret;
937 }
938 enable_irq(uport->irq);
939
940 return 0;
941 }
942
find_clk_rate_in_tol(struct clk * clk,unsigned int desired_clk,unsigned int * clk_div,unsigned int percent_tol)943 static unsigned long find_clk_rate_in_tol(struct clk *clk, unsigned int desired_clk,
944 unsigned int *clk_div, unsigned int percent_tol)
945 {
946 unsigned long freq;
947 unsigned long div, maxdiv;
948 u64 mult;
949 unsigned long offset, abs_tol, achieved;
950
951 abs_tol = div_u64((u64)desired_clk * percent_tol, 100);
952 maxdiv = CLK_DIV_MSK >> CLK_DIV_SHFT;
953 div = 1;
954 while (div <= maxdiv) {
955 mult = (u64)div * desired_clk;
956 if (mult != (unsigned long)mult)
957 break;
958
959 offset = div * abs_tol;
960 freq = clk_round_rate(clk, mult - offset);
961
962 /* Can only get lower if we're done */
963 if (freq < mult - offset)
964 break;
965
966 /*
967 * Re-calculate div in case rounding skipped rates but we
968 * ended up at a good one, then check for a match.
969 */
970 div = DIV_ROUND_CLOSEST(freq, desired_clk);
971 achieved = DIV_ROUND_CLOSEST(freq, div);
972 if (achieved <= desired_clk + abs_tol &&
973 achieved >= desired_clk - abs_tol) {
974 *clk_div = div;
975 return freq;
976 }
977
978 div = DIV_ROUND_UP(freq, desired_clk);
979 }
980
981 return 0;
982 }
983
get_clk_div_rate(struct clk * clk,unsigned int baud,unsigned int sampling_rate,unsigned int * clk_div)984 static unsigned long get_clk_div_rate(struct clk *clk, unsigned int baud,
985 unsigned int sampling_rate, unsigned int *clk_div)
986 {
987 unsigned long ser_clk;
988 unsigned long desired_clk;
989
990 desired_clk = baud * sampling_rate;
991 if (!desired_clk)
992 return 0;
993
994 /*
995 * try to find a clock rate within 2% tolerance, then within 5%
996 */
997 ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 2);
998 if (!ser_clk)
999 ser_clk = find_clk_rate_in_tol(clk, desired_clk, clk_div, 5);
1000
1001 return ser_clk;
1002 }
1003
qcom_geni_serial_set_termios(struct uart_port * uport,struct ktermios * termios,struct ktermios * old)1004 static void qcom_geni_serial_set_termios(struct uart_port *uport,
1005 struct ktermios *termios, struct ktermios *old)
1006 {
1007 unsigned int baud;
1008 u32 bits_per_char;
1009 u32 tx_trans_cfg;
1010 u32 tx_parity_cfg;
1011 u32 rx_trans_cfg;
1012 u32 rx_parity_cfg;
1013 u32 stop_bit_len;
1014 unsigned int clk_div;
1015 u32 ser_clk_cfg;
1016 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1017 unsigned long clk_rate;
1018 u32 ver, sampling_rate;
1019 unsigned int avg_bw_core;
1020
1021 qcom_geni_serial_stop_rx(uport);
1022 /* baud rate */
1023 baud = uart_get_baud_rate(uport, termios, old, 300, 4000000);
1024 port->baud = baud;
1025
1026 sampling_rate = UART_OVERSAMPLING;
1027 /* Sampling rate is halved for IP versions >= 2.5 */
1028 ver = geni_se_get_qup_hw_version(&port->se);
1029 if (ver >= QUP_SE_VERSION_2_5)
1030 sampling_rate /= 2;
1031
1032 clk_rate = get_clk_div_rate(port->se.clk, baud,
1033 sampling_rate, &clk_div);
1034 if (!clk_rate) {
1035 dev_err(port->se.dev,
1036 "Couldn't find suitable clock rate for %u\n",
1037 baud * sampling_rate);
1038 goto out_restart_rx;
1039 }
1040
1041 dev_dbg(port->se.dev, "desired_rate-%u, clk_rate-%lu, clk_div-%u\n",
1042 baud * sampling_rate, clk_rate, clk_div);
1043
1044 uport->uartclk = clk_rate;
1045 dev_pm_opp_set_rate(uport->dev, clk_rate);
1046 ser_clk_cfg = SER_CLK_EN;
1047 ser_clk_cfg |= clk_div << CLK_DIV_SHFT;
1048
1049 /*
1050 * Bump up BW vote on CPU and CORE path as driver supports FIFO mode
1051 * only.
1052 */
1053 avg_bw_core = (baud > 115200) ? Bps_to_icc(CORE_2X_50_MHZ)
1054 : GENI_DEFAULT_BW;
1055 port->se.icc_paths[GENI_TO_CORE].avg_bw = avg_bw_core;
1056 port->se.icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(baud);
1057 geni_icc_set_bw(&port->se);
1058
1059 /* parity */
1060 tx_trans_cfg = readl(uport->membase + SE_UART_TX_TRANS_CFG);
1061 tx_parity_cfg = readl(uport->membase + SE_UART_TX_PARITY_CFG);
1062 rx_trans_cfg = readl(uport->membase + SE_UART_RX_TRANS_CFG);
1063 rx_parity_cfg = readl(uport->membase + SE_UART_RX_PARITY_CFG);
1064 if (termios->c_cflag & PARENB) {
1065 tx_trans_cfg |= UART_TX_PAR_EN;
1066 rx_trans_cfg |= UART_RX_PAR_EN;
1067 tx_parity_cfg |= PAR_CALC_EN;
1068 rx_parity_cfg |= PAR_CALC_EN;
1069 if (termios->c_cflag & PARODD) {
1070 tx_parity_cfg |= PAR_ODD;
1071 rx_parity_cfg |= PAR_ODD;
1072 } else if (termios->c_cflag & CMSPAR) {
1073 tx_parity_cfg |= PAR_SPACE;
1074 rx_parity_cfg |= PAR_SPACE;
1075 } else {
1076 tx_parity_cfg |= PAR_EVEN;
1077 rx_parity_cfg |= PAR_EVEN;
1078 }
1079 } else {
1080 tx_trans_cfg &= ~UART_TX_PAR_EN;
1081 rx_trans_cfg &= ~UART_RX_PAR_EN;
1082 tx_parity_cfg &= ~PAR_CALC_EN;
1083 rx_parity_cfg &= ~PAR_CALC_EN;
1084 }
1085
1086 /* bits per char */
1087 bits_per_char = tty_get_char_size(termios->c_cflag);
1088
1089 /* stop bits */
1090 if (termios->c_cflag & CSTOPB)
1091 stop_bit_len = TX_STOP_BIT_LEN_2;
1092 else
1093 stop_bit_len = TX_STOP_BIT_LEN_1;
1094
1095 /* flow control, clear the CTS_MASK bit if using flow control. */
1096 if (termios->c_cflag & CRTSCTS)
1097 tx_trans_cfg &= ~UART_CTS_MASK;
1098 else
1099 tx_trans_cfg |= UART_CTS_MASK;
1100
1101 if (baud)
1102 uart_update_timeout(uport, termios->c_cflag, baud);
1103
1104 if (!uart_console(uport))
1105 writel(port->loopback,
1106 uport->membase + SE_UART_LOOPBACK_CFG);
1107 writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1108 writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1109 writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1110 writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1111 writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1112 writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1113 writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1114 writel(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG);
1115 writel(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG);
1116 out_restart_rx:
1117 qcom_geni_serial_start_rx(uport);
1118 }
1119
qcom_geni_serial_tx_empty(struct uart_port * uport)1120 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport)
1121 {
1122 return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS);
1123 }
1124
1125 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE
qcom_geni_console_setup(struct console * co,char * options)1126 static int qcom_geni_console_setup(struct console *co, char *options)
1127 {
1128 struct uart_port *uport;
1129 struct qcom_geni_serial_port *port;
1130 int baud = 115200;
1131 int bits = 8;
1132 int parity = 'n';
1133 int flow = 'n';
1134 int ret;
1135
1136 if (co->index >= GENI_UART_CONS_PORTS || co->index < 0)
1137 return -ENXIO;
1138
1139 port = get_port_from_line(co->index, true);
1140 if (IS_ERR(port)) {
1141 pr_err("Invalid line %d\n", co->index);
1142 return PTR_ERR(port);
1143 }
1144
1145 uport = &port->uport;
1146
1147 if (unlikely(!uport->membase))
1148 return -ENXIO;
1149
1150 if (!port->setup) {
1151 ret = qcom_geni_serial_port_setup(uport);
1152 if (ret)
1153 return ret;
1154 }
1155
1156 if (options)
1157 uart_parse_options(options, &baud, &parity, &bits, &flow);
1158
1159 return uart_set_options(uport, co, baud, parity, bits, flow);
1160 }
1161
qcom_geni_serial_earlycon_write(struct console * con,const char * s,unsigned int n)1162 static void qcom_geni_serial_earlycon_write(struct console *con,
1163 const char *s, unsigned int n)
1164 {
1165 struct earlycon_device *dev = con->data;
1166
1167 __qcom_geni_serial_console_write(&dev->port, s, n);
1168 }
1169
1170 #ifdef CONFIG_CONSOLE_POLL
qcom_geni_serial_earlycon_read(struct console * con,char * s,unsigned int n)1171 static int qcom_geni_serial_earlycon_read(struct console *con,
1172 char *s, unsigned int n)
1173 {
1174 struct earlycon_device *dev = con->data;
1175 struct uart_port *uport = &dev->port;
1176 int num_read = 0;
1177 int ch;
1178
1179 while (num_read < n) {
1180 ch = qcom_geni_serial_get_char(uport);
1181 if (ch == NO_POLL_CHAR)
1182 break;
1183 s[num_read++] = ch;
1184 }
1185
1186 return num_read;
1187 }
1188
qcom_geni_serial_enable_early_read(struct geni_se * se,struct console * con)1189 static void __init qcom_geni_serial_enable_early_read(struct geni_se *se,
1190 struct console *con)
1191 {
1192 geni_se_setup_s_cmd(se, UART_START_READ, 0);
1193 con->read = qcom_geni_serial_earlycon_read;
1194 }
1195 #else
qcom_geni_serial_enable_early_read(struct geni_se * se,struct console * con)1196 static inline void qcom_geni_serial_enable_early_read(struct geni_se *se,
1197 struct console *con) { }
1198 #endif
1199
1200 static struct qcom_geni_private_data earlycon_private_data;
1201
qcom_geni_serial_earlycon_setup(struct earlycon_device * dev,const char * opt)1202 static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev,
1203 const char *opt)
1204 {
1205 struct uart_port *uport = &dev->port;
1206 u32 tx_trans_cfg;
1207 u32 tx_parity_cfg = 0; /* Disable Tx Parity */
1208 u32 rx_trans_cfg = 0;
1209 u32 rx_parity_cfg = 0; /* Disable Rx Parity */
1210 u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */
1211 u32 bits_per_char;
1212 struct geni_se se;
1213
1214 if (!uport->membase)
1215 return -EINVAL;
1216
1217 uport->private_data = &earlycon_private_data;
1218
1219 memset(&se, 0, sizeof(se));
1220 se.base = uport->membase;
1221 if (geni_se_read_proto(&se) != GENI_SE_UART)
1222 return -ENXIO;
1223 /*
1224 * Ignore Flow control.
1225 * n = 8.
1226 */
1227 tx_trans_cfg = UART_CTS_MASK;
1228 bits_per_char = BITS_PER_BYTE;
1229
1230 /*
1231 * Make an unconditional cancel on the main sequencer to reset
1232 * it else we could end up in data loss scenarios.
1233 */
1234 qcom_geni_serial_poll_tx_done(uport);
1235 qcom_geni_serial_abort_rx(uport);
1236 geni_se_config_packing(&se, BITS_PER_BYTE, BYTES_PER_FIFO_WORD,
1237 false, true, true);
1238 geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2);
1239 geni_se_select_mode(&se, GENI_SE_FIFO);
1240
1241 writel(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG);
1242 writel(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG);
1243 writel(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG);
1244 writel(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG);
1245 writel(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN);
1246 writel(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN);
1247 writel(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN);
1248
1249 dev->con->write = qcom_geni_serial_earlycon_write;
1250 dev->con->setup = NULL;
1251 qcom_geni_serial_enable_early_read(&se, dev->con);
1252
1253 return 0;
1254 }
1255 OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart",
1256 qcom_geni_serial_earlycon_setup);
1257
console_register(struct uart_driver * drv)1258 static int __init console_register(struct uart_driver *drv)
1259 {
1260 return uart_register_driver(drv);
1261 }
1262
console_unregister(struct uart_driver * drv)1263 static void console_unregister(struct uart_driver *drv)
1264 {
1265 uart_unregister_driver(drv);
1266 }
1267
1268 static struct console cons_ops = {
1269 .name = "ttyMSM",
1270 .write = qcom_geni_serial_console_write,
1271 .device = uart_console_device,
1272 .setup = qcom_geni_console_setup,
1273 .flags = CON_PRINTBUFFER,
1274 .index = -1,
1275 .data = &qcom_geni_console_driver,
1276 };
1277
1278 static struct uart_driver qcom_geni_console_driver = {
1279 .owner = THIS_MODULE,
1280 .driver_name = "qcom_geni_console",
1281 .dev_name = "ttyMSM",
1282 .nr = GENI_UART_CONS_PORTS,
1283 .cons = &cons_ops,
1284 };
1285 #else
console_register(struct uart_driver * drv)1286 static int console_register(struct uart_driver *drv)
1287 {
1288 return 0;
1289 }
1290
console_unregister(struct uart_driver * drv)1291 static void console_unregister(struct uart_driver *drv)
1292 {
1293 }
1294 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */
1295
1296 static struct uart_driver qcom_geni_uart_driver = {
1297 .owner = THIS_MODULE,
1298 .driver_name = "qcom_geni_uart",
1299 .dev_name = "ttyHS",
1300 .nr = GENI_UART_PORTS,
1301 };
1302
qcom_geni_serial_pm(struct uart_port * uport,unsigned int new_state,unsigned int old_state)1303 static void qcom_geni_serial_pm(struct uart_port *uport,
1304 unsigned int new_state, unsigned int old_state)
1305 {
1306 struct qcom_geni_serial_port *port = to_dev_port(uport, uport);
1307
1308 /* If we've never been called, treat it as off */
1309 if (old_state == UART_PM_STATE_UNDEFINED)
1310 old_state = UART_PM_STATE_OFF;
1311
1312 if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) {
1313 geni_icc_enable(&port->se);
1314 geni_se_resources_on(&port->se);
1315 } else if (new_state == UART_PM_STATE_OFF &&
1316 old_state == UART_PM_STATE_ON) {
1317 geni_se_resources_off(&port->se);
1318 geni_icc_disable(&port->se);
1319 }
1320 }
1321
1322 static const struct uart_ops qcom_geni_console_pops = {
1323 .tx_empty = qcom_geni_serial_tx_empty,
1324 .stop_tx = qcom_geni_serial_stop_tx,
1325 .start_tx = qcom_geni_serial_start_tx,
1326 .stop_rx = qcom_geni_serial_stop_rx,
1327 .start_rx = qcom_geni_serial_start_rx,
1328 .set_termios = qcom_geni_serial_set_termios,
1329 .startup = qcom_geni_serial_startup,
1330 .request_port = qcom_geni_serial_request_port,
1331 .config_port = qcom_geni_serial_config_port,
1332 .shutdown = qcom_geni_serial_shutdown,
1333 .type = qcom_geni_serial_get_type,
1334 .set_mctrl = qcom_geni_serial_set_mctrl,
1335 .get_mctrl = qcom_geni_serial_get_mctrl,
1336 #ifdef CONFIG_CONSOLE_POLL
1337 .poll_get_char = qcom_geni_serial_get_char,
1338 .poll_put_char = qcom_geni_serial_poll_put_char,
1339 #endif
1340 .pm = qcom_geni_serial_pm,
1341 };
1342
1343 static const struct uart_ops qcom_geni_uart_pops = {
1344 .tx_empty = qcom_geni_serial_tx_empty,
1345 .stop_tx = qcom_geni_serial_stop_tx,
1346 .start_tx = qcom_geni_serial_start_tx,
1347 .stop_rx = qcom_geni_serial_stop_rx,
1348 .set_termios = qcom_geni_serial_set_termios,
1349 .startup = qcom_geni_serial_startup,
1350 .request_port = qcom_geni_serial_request_port,
1351 .config_port = qcom_geni_serial_config_port,
1352 .shutdown = qcom_geni_serial_shutdown,
1353 .type = qcom_geni_serial_get_type,
1354 .set_mctrl = qcom_geni_serial_set_mctrl,
1355 .get_mctrl = qcom_geni_serial_get_mctrl,
1356 .pm = qcom_geni_serial_pm,
1357 };
1358
qcom_geni_serial_probe(struct platform_device * pdev)1359 static int qcom_geni_serial_probe(struct platform_device *pdev)
1360 {
1361 int ret = 0;
1362 int line;
1363 struct qcom_geni_serial_port *port;
1364 struct uart_port *uport;
1365 struct resource *res;
1366 int irq;
1367 bool console = false;
1368 struct uart_driver *drv;
1369
1370 if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart"))
1371 console = true;
1372
1373 if (console) {
1374 drv = &qcom_geni_console_driver;
1375 line = of_alias_get_id(pdev->dev.of_node, "serial");
1376 } else {
1377 drv = &qcom_geni_uart_driver;
1378 line = of_alias_get_id(pdev->dev.of_node, "serial");
1379 if (line == -ENODEV) /* compat with non-standard aliases */
1380 line = of_alias_get_id(pdev->dev.of_node, "hsuart");
1381 }
1382
1383 port = get_port_from_line(line, console);
1384 if (IS_ERR(port)) {
1385 dev_err(&pdev->dev, "Invalid line %d\n", line);
1386 return PTR_ERR(port);
1387 }
1388
1389 uport = &port->uport;
1390 /* Don't allow 2 drivers to access the same port */
1391 if (uport->private_data)
1392 return -ENODEV;
1393
1394 uport->dev = &pdev->dev;
1395 port->se.dev = &pdev->dev;
1396 port->se.wrapper = dev_get_drvdata(pdev->dev.parent);
1397 port->se.clk = devm_clk_get(&pdev->dev, "se");
1398 if (IS_ERR(port->se.clk)) {
1399 ret = PTR_ERR(port->se.clk);
1400 dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret);
1401 return ret;
1402 }
1403
1404 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1405 if (!res)
1406 return -EINVAL;
1407 uport->mapbase = res->start;
1408
1409 port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1410 port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS;
1411 port->tx_fifo_width = DEF_FIFO_WIDTH_BITS;
1412
1413 if (!console) {
1414 port->rx_fifo = devm_kcalloc(uport->dev,
1415 port->rx_fifo_depth, sizeof(u32), GFP_KERNEL);
1416 if (!port->rx_fifo)
1417 return -ENOMEM;
1418 }
1419
1420 ret = geni_icc_get(&port->se, NULL);
1421 if (ret)
1422 return ret;
1423 port->se.icc_paths[GENI_TO_CORE].avg_bw = GENI_DEFAULT_BW;
1424 port->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
1425
1426 /* Set BW for register access */
1427 ret = geni_icc_set_bw(&port->se);
1428 if (ret)
1429 return ret;
1430
1431 port->name = devm_kasprintf(uport->dev, GFP_KERNEL,
1432 "qcom_geni_serial_%s%d",
1433 uart_console(uport) ? "console" : "uart", uport->line);
1434 if (!port->name)
1435 return -ENOMEM;
1436
1437 irq = platform_get_irq(pdev, 0);
1438 if (irq < 0)
1439 return irq;
1440 uport->irq = irq;
1441 uport->has_sysrq = IS_ENABLED(CONFIG_SERIAL_QCOM_GENI_CONSOLE);
1442
1443 if (!console)
1444 port->wakeup_irq = platform_get_irq_optional(pdev, 1);
1445
1446 if (of_property_read_bool(pdev->dev.of_node, "rx-tx-swap"))
1447 port->rx_tx_swap = true;
1448
1449 if (of_property_read_bool(pdev->dev.of_node, "cts-rts-swap"))
1450 port->cts_rts_swap = true;
1451
1452 ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
1453 if (ret)
1454 return ret;
1455 /* OPP table is optional */
1456 ret = devm_pm_opp_of_add_table(&pdev->dev);
1457 if (ret && ret != -ENODEV) {
1458 dev_err(&pdev->dev, "invalid OPP table in device tree\n");
1459 return ret;
1460 }
1461
1462 port->private_data.drv = drv;
1463 uport->private_data = &port->private_data;
1464 platform_set_drvdata(pdev, port);
1465 port->handle_rx = console ? handle_rx_console : handle_rx_uart;
1466
1467 ret = uart_add_one_port(drv, uport);
1468 if (ret)
1469 return ret;
1470
1471 irq_set_status_flags(uport->irq, IRQ_NOAUTOEN);
1472 ret = devm_request_irq(uport->dev, uport->irq, qcom_geni_serial_isr,
1473 IRQF_TRIGGER_HIGH, port->name, uport);
1474 if (ret) {
1475 dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret);
1476 uart_remove_one_port(drv, uport);
1477 return ret;
1478 }
1479
1480 /*
1481 * Set pm_runtime status as ACTIVE so that wakeup_irq gets
1482 * enabled/disabled from dev_pm_arm_wake_irq during system
1483 * suspend/resume respectively.
1484 */
1485 pm_runtime_set_active(&pdev->dev);
1486
1487 if (port->wakeup_irq > 0) {
1488 device_init_wakeup(&pdev->dev, true);
1489 ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
1490 port->wakeup_irq);
1491 if (ret) {
1492 device_init_wakeup(&pdev->dev, false);
1493 uart_remove_one_port(drv, uport);
1494 return ret;
1495 }
1496 }
1497
1498 return 0;
1499 }
1500
qcom_geni_serial_remove(struct platform_device * pdev)1501 static int qcom_geni_serial_remove(struct platform_device *pdev)
1502 {
1503 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev);
1504 struct uart_driver *drv = port->private_data.drv;
1505
1506 dev_pm_clear_wake_irq(&pdev->dev);
1507 device_init_wakeup(&pdev->dev, false);
1508 uart_remove_one_port(drv, &port->uport);
1509
1510 return 0;
1511 }
1512
qcom_geni_serial_sys_suspend(struct device * dev)1513 static int __maybe_unused qcom_geni_serial_sys_suspend(struct device *dev)
1514 {
1515 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1516 struct uart_port *uport = &port->uport;
1517 struct qcom_geni_private_data *private_data = uport->private_data;
1518
1519 /*
1520 * This is done so we can hit the lowest possible state in suspend
1521 * even with no_console_suspend
1522 */
1523 if (uart_console(uport)) {
1524 geni_icc_set_tag(&port->se, 0x3);
1525 geni_icc_set_bw(&port->se);
1526 }
1527 return uart_suspend_port(private_data->drv, uport);
1528 }
1529
qcom_geni_serial_sys_resume(struct device * dev)1530 static int __maybe_unused qcom_geni_serial_sys_resume(struct device *dev)
1531 {
1532 int ret;
1533 struct qcom_geni_serial_port *port = dev_get_drvdata(dev);
1534 struct uart_port *uport = &port->uport;
1535 struct qcom_geni_private_data *private_data = uport->private_data;
1536
1537 ret = uart_resume_port(private_data->drv, uport);
1538 if (uart_console(uport)) {
1539 geni_icc_set_tag(&port->se, 0x7);
1540 geni_icc_set_bw(&port->se);
1541 }
1542 return ret;
1543 }
1544
1545 static const struct dev_pm_ops qcom_geni_serial_pm_ops = {
1546 SET_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend,
1547 qcom_geni_serial_sys_resume)
1548 };
1549
1550 static const struct of_device_id qcom_geni_serial_match_table[] = {
1551 { .compatible = "qcom,geni-debug-uart", },
1552 { .compatible = "qcom,geni-uart", },
1553 {}
1554 };
1555 MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table);
1556
1557 static struct platform_driver qcom_geni_serial_platform_driver = {
1558 .remove = qcom_geni_serial_remove,
1559 .probe = qcom_geni_serial_probe,
1560 .driver = {
1561 .name = "qcom_geni_serial",
1562 .of_match_table = qcom_geni_serial_match_table,
1563 .pm = &qcom_geni_serial_pm_ops,
1564 },
1565 };
1566
qcom_geni_serial_init(void)1567 static int __init qcom_geni_serial_init(void)
1568 {
1569 int ret;
1570
1571 ret = console_register(&qcom_geni_console_driver);
1572 if (ret)
1573 return ret;
1574
1575 ret = uart_register_driver(&qcom_geni_uart_driver);
1576 if (ret) {
1577 console_unregister(&qcom_geni_console_driver);
1578 return ret;
1579 }
1580
1581 ret = platform_driver_register(&qcom_geni_serial_platform_driver);
1582 if (ret) {
1583 console_unregister(&qcom_geni_console_driver);
1584 uart_unregister_driver(&qcom_geni_uart_driver);
1585 }
1586 return ret;
1587 }
1588 module_init(qcom_geni_serial_init);
1589
qcom_geni_serial_exit(void)1590 static void __exit qcom_geni_serial_exit(void)
1591 {
1592 platform_driver_unregister(&qcom_geni_serial_platform_driver);
1593 console_unregister(&qcom_geni_console_driver);
1594 uart_unregister_driver(&qcom_geni_uart_driver);
1595 }
1596 module_exit(qcom_geni_serial_exit);
1597
1598 MODULE_DESCRIPTION("Serial driver for GENI based QUP cores");
1599 MODULE_LICENSE("GPL v2");
1600