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