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