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