1 // SPDX-License-Identifier: GPL-2.0 2 // Copyright (c) 2017-2018, The Linux foundation. All rights reserved. 3 4 #include <linux/clk.h> 5 #include <linux/console.h> 6 #include <linux/io.h> 7 #include <linux/iopoll.h> 8 #include <linux/module.h> 9 #include <linux/of.h> 10 #include <linux/of_device.h> 11 #include <linux/platform_device.h> 12 #include <linux/qcom-geni-se.h> 13 #include <linux/serial.h> 14 #include <linux/serial_core.h> 15 #include <linux/slab.h> 16 #include <linux/tty.h> 17 #include <linux/tty_flip.h> 18 19 /* UART specific GENI registers */ 20 #define SE_UART_LOOPBACK_CFG 0x22c 21 #define SE_UART_TX_TRANS_CFG 0x25c 22 #define SE_UART_TX_WORD_LEN 0x268 23 #define SE_UART_TX_STOP_BIT_LEN 0x26c 24 #define SE_UART_TX_TRANS_LEN 0x270 25 #define SE_UART_RX_TRANS_CFG 0x280 26 #define SE_UART_RX_WORD_LEN 0x28c 27 #define SE_UART_RX_STALE_CNT 0x294 28 #define SE_UART_TX_PARITY_CFG 0x2a4 29 #define SE_UART_RX_PARITY_CFG 0x2a8 30 #define SE_UART_MANUAL_RFR 0x2ac 31 32 /* SE_UART_TRANS_CFG */ 33 #define UART_TX_PAR_EN BIT(0) 34 #define UART_CTS_MASK BIT(1) 35 36 /* SE_UART_TX_WORD_LEN */ 37 #define TX_WORD_LEN_MSK GENMASK(9, 0) 38 39 /* SE_UART_TX_STOP_BIT_LEN */ 40 #define TX_STOP_BIT_LEN_MSK GENMASK(23, 0) 41 #define TX_STOP_BIT_LEN_1 0 42 #define TX_STOP_BIT_LEN_1_5 1 43 #define TX_STOP_BIT_LEN_2 2 44 45 /* SE_UART_TX_TRANS_LEN */ 46 #define TX_TRANS_LEN_MSK GENMASK(23, 0) 47 48 /* SE_UART_RX_TRANS_CFG */ 49 #define UART_RX_INS_STATUS_BIT BIT(2) 50 #define UART_RX_PAR_EN BIT(3) 51 52 /* SE_UART_RX_WORD_LEN */ 53 #define RX_WORD_LEN_MASK GENMASK(9, 0) 54 55 /* SE_UART_RX_STALE_CNT */ 56 #define RX_STALE_CNT GENMASK(23, 0) 57 58 /* SE_UART_TX_PARITY_CFG/RX_PARITY_CFG */ 59 #define PAR_CALC_EN BIT(0) 60 #define PAR_MODE_MSK GENMASK(2, 1) 61 #define PAR_MODE_SHFT 1 62 #define PAR_EVEN 0x00 63 #define PAR_ODD 0x01 64 #define PAR_SPACE 0x10 65 #define PAR_MARK 0x11 66 67 /* SE_UART_MANUAL_RFR register fields */ 68 #define UART_MANUAL_RFR_EN BIT(31) 69 #define UART_RFR_NOT_READY BIT(1) 70 #define UART_RFR_READY BIT(0) 71 72 /* UART M_CMD OP codes */ 73 #define UART_START_TX 0x1 74 #define UART_START_BREAK 0x4 75 #define UART_STOP_BREAK 0x5 76 /* UART S_CMD OP codes */ 77 #define UART_START_READ 0x1 78 #define UART_PARAM 0x1 79 80 #define UART_OVERSAMPLING 32 81 #define STALE_TIMEOUT 16 82 #define DEFAULT_BITS_PER_CHAR 10 83 #define GENI_UART_CONS_PORTS 1 84 #define GENI_UART_PORTS 3 85 #define DEF_FIFO_DEPTH_WORDS 16 86 #define DEF_TX_WM 2 87 #define DEF_FIFO_WIDTH_BITS 32 88 #define UART_CONSOLE_RX_WM 2 89 #define MAX_LOOPBACK_CFG 3 90 91 #ifdef CONFIG_CONSOLE_POLL 92 #define RX_BYTES_PW 1 93 #else 94 #define RX_BYTES_PW 4 95 #endif 96 97 struct qcom_geni_serial_port { 98 struct uart_port uport; 99 struct geni_se se; 100 char name[20]; 101 u32 tx_fifo_depth; 102 u32 tx_fifo_width; 103 u32 rx_fifo_depth; 104 u32 tx_wm; 105 u32 rx_wm; 106 u32 rx_rfr; 107 enum geni_se_xfer_mode xfer_mode; 108 bool setup; 109 int (*handle_rx)(struct uart_port *uport, u32 bytes, bool drop); 110 unsigned int baud; 111 unsigned int tx_bytes_pw; 112 unsigned int rx_bytes_pw; 113 u32 *rx_fifo; 114 u32 loopback; 115 bool brk; 116 }; 117 118 static const struct uart_ops qcom_geni_console_pops; 119 static const struct uart_ops qcom_geni_uart_pops; 120 static struct uart_driver qcom_geni_console_driver; 121 static struct uart_driver qcom_geni_uart_driver; 122 static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop); 123 static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop); 124 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *port); 125 static void qcom_geni_serial_stop_rx(struct uart_port *uport); 126 127 static const unsigned long root_freq[] = {7372800, 14745600, 19200000, 29491200, 128 32000000, 48000000, 64000000, 80000000, 129 96000000, 100000000, 102400000, 130 112000000, 120000000, 128000000}; 131 132 #define to_dev_port(ptr, member) \ 133 container_of(ptr, struct qcom_geni_serial_port, member) 134 135 static struct qcom_geni_serial_port qcom_geni_uart_ports[GENI_UART_PORTS] = { 136 [0] = { 137 .uport = { 138 .iotype = UPIO_MEM, 139 .ops = &qcom_geni_uart_pops, 140 .flags = UPF_BOOT_AUTOCONF, 141 .line = 0, 142 }, 143 }, 144 [1] = { 145 .uport = { 146 .iotype = UPIO_MEM, 147 .ops = &qcom_geni_uart_pops, 148 .flags = UPF_BOOT_AUTOCONF, 149 .line = 1, 150 }, 151 }, 152 [2] = { 153 .uport = { 154 .iotype = UPIO_MEM, 155 .ops = &qcom_geni_uart_pops, 156 .flags = UPF_BOOT_AUTOCONF, 157 .line = 2, 158 }, 159 }, 160 }; 161 162 static ssize_t loopback_show(struct device *dev, 163 struct device_attribute *attr, char *buf) 164 { 165 struct platform_device *pdev = to_platform_device(dev); 166 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev); 167 168 return snprintf(buf, sizeof(u32), "%d\n", port->loopback); 169 } 170 171 static ssize_t loopback_store(struct device *dev, 172 struct device_attribute *attr, const char *buf, 173 size_t size) 174 { 175 struct platform_device *pdev = to_platform_device(dev); 176 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev); 177 u32 loopback; 178 179 if (kstrtoint(buf, 0, &loopback) || loopback > MAX_LOOPBACK_CFG) { 180 dev_err(dev, "Invalid input\n"); 181 return -EINVAL; 182 } 183 port->loopback = loopback; 184 return size; 185 } 186 static DEVICE_ATTR_RW(loopback); 187 188 static struct qcom_geni_serial_port qcom_geni_console_port = { 189 .uport = { 190 .iotype = UPIO_MEM, 191 .ops = &qcom_geni_console_pops, 192 .flags = UPF_BOOT_AUTOCONF, 193 .line = 0, 194 }, 195 }; 196 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, uport); 201 struct resource *res; 202 203 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 204 uport->membase = devm_ioremap_resource(&pdev->dev, res); 205 if (IS_ERR(uport->membase)) 206 return PTR_ERR(uport->membase); 207 port->se.base = uport->membase; 208 return 0; 209 } 210 211 static void qcom_geni_serial_config_port(struct uart_port *uport, int cfg_flags) 212 { 213 if (cfg_flags & UART_CONFIG_TYPE) { 214 uport->type = PORT_MSM; 215 qcom_geni_serial_request_port(uport); 216 } 217 } 218 219 static unsigned int qcom_geni_serial_get_mctrl(struct uart_port *uport) 220 { 221 unsigned int mctrl = TIOCM_DSR | TIOCM_CAR; 222 u32 geni_ios; 223 224 if (uart_console(uport) || !uart_cts_enabled(uport)) { 225 mctrl |= TIOCM_CTS; 226 } else { 227 geni_ios = readl_relaxed(uport->membase + SE_GENI_IOS); 228 if (!(geni_ios & IO2_DATA_IN)) 229 mctrl |= TIOCM_CTS; 230 } 231 232 return mctrl; 233 } 234 235 static void qcom_geni_serial_set_mctrl(struct uart_port *uport, 236 unsigned int mctrl) 237 { 238 u32 uart_manual_rfr = 0; 239 240 if (uart_console(uport) || !uart_cts_enabled(uport)) 241 return; 242 243 if (!(mctrl & TIOCM_RTS)) 244 uart_manual_rfr = UART_MANUAL_RFR_EN | UART_RFR_NOT_READY; 245 writel_relaxed(uart_manual_rfr, uport->membase + SE_UART_MANUAL_RFR); 246 } 247 248 static const char *qcom_geni_serial_get_type(struct uart_port *uport) 249 { 250 return "MSM"; 251 } 252 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 (line < 0 || line >= nr_ports) 259 return ERR_PTR(-ENXIO); 260 261 port = console ? &qcom_geni_console_port : &qcom_geni_uart_ports[line]; 262 return port; 263 } 264 265 static bool qcom_geni_serial_poll_bit(struct uart_port *uport, 266 int offset, int field, bool set) 267 { 268 u32 reg; 269 struct qcom_geni_serial_port *port; 270 unsigned int baud; 271 unsigned int fifo_bits; 272 unsigned long timeout_us = 20000; 273 274 /* Ensure polling is not re-ordered before the prior writes/reads */ 275 mb(); 276 277 if (uport->private_data) { 278 port = to_dev_port(uport, uport); 279 baud = port->baud; 280 if (!baud) 281 baud = 115200; 282 fifo_bits = port->tx_fifo_depth * port->tx_fifo_width; 283 /* 284 * Total polling iterations based on FIFO worth of bytes to be 285 * sent at current baud. Add a little fluff to the wait. 286 */ 287 timeout_us = ((fifo_bits * USEC_PER_SEC) / baud) + 500; 288 } 289 290 /* 291 * Use custom implementation instead of readl_poll_atomic since ktimer 292 * is not ready at the time of early console. 293 */ 294 timeout_us = DIV_ROUND_UP(timeout_us, 10) * 10; 295 while (timeout_us) { 296 reg = readl_relaxed(uport->membase + offset); 297 if ((bool)(reg & field) == set) 298 return true; 299 udelay(10); 300 timeout_us -= 10; 301 } 302 return false; 303 } 304 305 static void qcom_geni_serial_setup_tx(struct uart_port *uport, u32 xmit_size) 306 { 307 u32 m_cmd; 308 309 writel_relaxed(xmit_size, uport->membase + SE_UART_TX_TRANS_LEN); 310 m_cmd = UART_START_TX << M_OPCODE_SHFT; 311 writel(m_cmd, uport->membase + SE_GENI_M_CMD0); 312 } 313 314 static void qcom_geni_serial_poll_tx_done(struct uart_port *uport) 315 { 316 int done; 317 u32 irq_clear = M_CMD_DONE_EN; 318 319 done = qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 320 M_CMD_DONE_EN, true); 321 if (!done) { 322 writel_relaxed(M_GENI_CMD_ABORT, uport->membase + 323 SE_GENI_M_CMD_CTRL_REG); 324 irq_clear |= M_CMD_ABORT_EN; 325 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 326 M_CMD_ABORT_EN, true); 327 } 328 writel_relaxed(irq_clear, uport->membase + SE_GENI_M_IRQ_CLEAR); 329 } 330 331 static void qcom_geni_serial_abort_rx(struct uart_port *uport) 332 { 333 u32 irq_clear = S_CMD_DONE_EN | S_CMD_ABORT_EN; 334 335 writel(S_GENI_CMD_ABORT, uport->membase + SE_GENI_S_CMD_CTRL_REG); 336 qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG, 337 S_GENI_CMD_ABORT, false); 338 writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR); 339 writel_relaxed(FORCE_DEFAULT, uport->membase + GENI_FORCE_DEFAULT_REG); 340 } 341 342 #ifdef CONFIG_CONSOLE_POLL 343 static int qcom_geni_serial_get_char(struct uart_port *uport) 344 { 345 u32 rx_fifo; 346 u32 status; 347 348 status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS); 349 writel_relaxed(status, uport->membase + SE_GENI_M_IRQ_CLEAR); 350 351 status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS); 352 writel_relaxed(status, uport->membase + SE_GENI_S_IRQ_CLEAR); 353 354 /* 355 * Ensure the writes to clear interrupts is not re-ordered after 356 * reading the data. 357 */ 358 mb(); 359 360 status = readl_relaxed(uport->membase + SE_GENI_RX_FIFO_STATUS); 361 if (!(status & RX_FIFO_WC_MSK)) 362 return NO_POLL_CHAR; 363 364 rx_fifo = readl(uport->membase + SE_GENI_RX_FIFOn); 365 return rx_fifo & 0xff; 366 } 367 368 static void qcom_geni_serial_poll_put_char(struct uart_port *uport, 369 unsigned char c) 370 { 371 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 372 373 writel_relaxed(port->tx_wm, uport->membase + SE_GENI_TX_WATERMARK_REG); 374 qcom_geni_serial_setup_tx(uport, 1); 375 WARN_ON(!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 376 M_TX_FIFO_WATERMARK_EN, true)); 377 writel_relaxed(c, uport->membase + SE_GENI_TX_FIFOn); 378 writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase + 379 SE_GENI_M_IRQ_CLEAR); 380 qcom_geni_serial_poll_tx_done(uport); 381 } 382 #endif 383 384 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE 385 static void qcom_geni_serial_wr_char(struct uart_port *uport, int ch) 386 { 387 writel_relaxed(ch, uport->membase + SE_GENI_TX_FIFOn); 388 } 389 390 static void 391 __qcom_geni_serial_console_write(struct uart_port *uport, const char *s, 392 unsigned int count) 393 { 394 int i; 395 u32 bytes_to_send = count; 396 397 for (i = 0; i < count; i++) { 398 /* 399 * uart_console_write() adds a carriage return for each newline. 400 * Account for additional bytes to be written. 401 */ 402 if (s[i] == '\n') 403 bytes_to_send++; 404 } 405 406 writel_relaxed(DEF_TX_WM, uport->membase + SE_GENI_TX_WATERMARK_REG); 407 qcom_geni_serial_setup_tx(uport, bytes_to_send); 408 for (i = 0; i < count; ) { 409 size_t chars_to_write = 0; 410 size_t avail = DEF_FIFO_DEPTH_WORDS - DEF_TX_WM; 411 412 /* 413 * If the WM bit never set, then the Tx state machine is not 414 * in a valid state, so break, cancel/abort any existing 415 * command. Unfortunately the current data being written is 416 * lost. 417 */ 418 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 419 M_TX_FIFO_WATERMARK_EN, true)) 420 break; 421 chars_to_write = min_t(size_t, count - i, avail / 2); 422 uart_console_write(uport, s + i, chars_to_write, 423 qcom_geni_serial_wr_char); 424 writel_relaxed(M_TX_FIFO_WATERMARK_EN, uport->membase + 425 SE_GENI_M_IRQ_CLEAR); 426 i += chars_to_write; 427 } 428 qcom_geni_serial_poll_tx_done(uport); 429 } 430 431 static void qcom_geni_serial_console_write(struct console *co, const char *s, 432 unsigned int count) 433 { 434 struct uart_port *uport; 435 struct qcom_geni_serial_port *port; 436 bool locked = true; 437 unsigned long flags; 438 439 WARN_ON(co->index < 0 || co->index >= GENI_UART_CONS_PORTS); 440 441 port = get_port_from_line(co->index, true); 442 if (IS_ERR(port)) 443 return; 444 445 uport = &port->uport; 446 if (oops_in_progress) 447 locked = spin_trylock_irqsave(&uport->lock, flags); 448 else 449 spin_lock_irqsave(&uport->lock, flags); 450 451 /* Cancel the current write to log the fault */ 452 if (!locked) { 453 geni_se_cancel_m_cmd(&port->se); 454 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 455 M_CMD_CANCEL_EN, true)) { 456 geni_se_abort_m_cmd(&port->se); 457 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 458 M_CMD_ABORT_EN, true); 459 writel_relaxed(M_CMD_ABORT_EN, uport->membase + 460 SE_GENI_M_IRQ_CLEAR); 461 } 462 writel_relaxed(M_CMD_CANCEL_EN, uport->membase + 463 SE_GENI_M_IRQ_CLEAR); 464 } 465 466 __qcom_geni_serial_console_write(uport, s, count); 467 if (locked) 468 spin_unlock_irqrestore(&uport->lock, flags); 469 } 470 471 static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop) 472 { 473 u32 i; 474 unsigned char buf[sizeof(u32)]; 475 struct tty_port *tport; 476 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 477 478 tport = &uport->state->port; 479 for (i = 0; i < bytes; ) { 480 int c; 481 int chunk = min_t(int, bytes - i, port->rx_bytes_pw); 482 483 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, buf, 1); 484 i += chunk; 485 if (drop) 486 continue; 487 488 for (c = 0; c < chunk; c++) { 489 int sysrq; 490 491 uport->icount.rx++; 492 if (port->brk && buf[c] == 0) { 493 port->brk = false; 494 if (uart_handle_break(uport)) 495 continue; 496 } 497 498 sysrq = uart_handle_sysrq_char(uport, buf[c]); 499 if (!sysrq) 500 tty_insert_flip_char(tport, buf[c], TTY_NORMAL); 501 } 502 } 503 if (!drop) 504 tty_flip_buffer_push(tport); 505 return 0; 506 } 507 #else 508 static int handle_rx_console(struct uart_port *uport, u32 bytes, bool drop) 509 { 510 return -EPERM; 511 } 512 513 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */ 514 515 static int handle_rx_uart(struct uart_port *uport, u32 bytes, bool drop) 516 { 517 unsigned char *buf; 518 struct tty_port *tport; 519 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 520 u32 num_bytes_pw = port->tx_fifo_width / BITS_PER_BYTE; 521 u32 words = ALIGN(bytes, num_bytes_pw) / num_bytes_pw; 522 int ret; 523 524 tport = &uport->state->port; 525 ioread32_rep(uport->membase + SE_GENI_RX_FIFOn, port->rx_fifo, words); 526 if (drop) 527 return 0; 528 529 buf = (unsigned char *)port->rx_fifo; 530 ret = tty_insert_flip_string(tport, buf, bytes); 531 if (ret != bytes) { 532 dev_err(uport->dev, "%s:Unable to push data ret %d_bytes %d\n", 533 __func__, ret, bytes); 534 WARN_ON_ONCE(1); 535 } 536 uport->icount.rx += ret; 537 tty_flip_buffer_push(tport); 538 return ret; 539 } 540 541 static void qcom_geni_serial_start_tx(struct uart_port *uport) 542 { 543 u32 irq_en; 544 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 545 u32 status; 546 547 if (port->xfer_mode == GENI_SE_FIFO) { 548 /* 549 * readl ensures reading & writing of IRQ_EN register 550 * is not re-ordered before checking the status of the 551 * Serial Engine. 552 */ 553 status = readl(uport->membase + SE_GENI_STATUS); 554 if (status & M_GENI_CMD_ACTIVE) 555 return; 556 557 if (!qcom_geni_serial_tx_empty(uport)) 558 return; 559 560 irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN); 561 irq_en |= M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN; 562 563 writel_relaxed(port->tx_wm, uport->membase + 564 SE_GENI_TX_WATERMARK_REG); 565 writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN); 566 } 567 } 568 569 static void qcom_geni_serial_stop_tx(struct uart_port *uport) 570 { 571 u32 irq_en; 572 u32 status; 573 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 574 575 irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN); 576 irq_en &= ~M_CMD_DONE_EN; 577 if (port->xfer_mode == GENI_SE_FIFO) { 578 irq_en &= ~M_TX_FIFO_WATERMARK_EN; 579 writel_relaxed(0, uport->membase + 580 SE_GENI_TX_WATERMARK_REG); 581 } 582 writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN); 583 status = readl_relaxed(uport->membase + SE_GENI_STATUS); 584 /* Possible stop tx is called multiple times. */ 585 if (!(status & M_GENI_CMD_ACTIVE)) 586 return; 587 588 /* 589 * Ensure cancel command write is not re-ordered before checking 590 * the status of the Primary Sequencer. 591 */ 592 mb(); 593 594 geni_se_cancel_m_cmd(&port->se); 595 if (!qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 596 M_CMD_CANCEL_EN, true)) { 597 geni_se_abort_m_cmd(&port->se); 598 qcom_geni_serial_poll_bit(uport, SE_GENI_M_IRQ_STATUS, 599 M_CMD_ABORT_EN, true); 600 writel_relaxed(M_CMD_ABORT_EN, uport->membase + 601 SE_GENI_M_IRQ_CLEAR); 602 } 603 writel_relaxed(M_CMD_CANCEL_EN, uport->membase + SE_GENI_M_IRQ_CLEAR); 604 } 605 606 static void qcom_geni_serial_start_rx(struct uart_port *uport) 607 { 608 u32 irq_en; 609 u32 status; 610 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 611 612 status = readl_relaxed(uport->membase + SE_GENI_STATUS); 613 if (status & S_GENI_CMD_ACTIVE) 614 qcom_geni_serial_stop_rx(uport); 615 616 /* 617 * Ensure setup command write is not re-ordered before checking 618 * the status of the Secondary Sequencer. 619 */ 620 mb(); 621 622 geni_se_setup_s_cmd(&port->se, UART_START_READ, 0); 623 624 if (port->xfer_mode == GENI_SE_FIFO) { 625 irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN); 626 irq_en |= S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN; 627 writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN); 628 629 irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN); 630 irq_en |= M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN; 631 writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN); 632 } 633 } 634 635 static void qcom_geni_serial_stop_rx(struct uart_port *uport) 636 { 637 u32 irq_en; 638 u32 status; 639 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 640 u32 irq_clear = S_CMD_DONE_EN; 641 642 if (port->xfer_mode == GENI_SE_FIFO) { 643 irq_en = readl_relaxed(uport->membase + SE_GENI_S_IRQ_EN); 644 irq_en &= ~(S_RX_FIFO_WATERMARK_EN | S_RX_FIFO_LAST_EN); 645 writel_relaxed(irq_en, uport->membase + SE_GENI_S_IRQ_EN); 646 647 irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN); 648 irq_en &= ~(M_RX_FIFO_WATERMARK_EN | M_RX_FIFO_LAST_EN); 649 writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN); 650 } 651 652 status = readl_relaxed(uport->membase + SE_GENI_STATUS); 653 /* Possible stop rx is called multiple times. */ 654 if (!(status & S_GENI_CMD_ACTIVE)) 655 return; 656 657 /* 658 * Ensure cancel command write is not re-ordered before checking 659 * the status of the Secondary Sequencer. 660 */ 661 mb(); 662 663 geni_se_cancel_s_cmd(&port->se); 664 qcom_geni_serial_poll_bit(uport, SE_GENI_S_CMD_CTRL_REG, 665 S_GENI_CMD_CANCEL, false); 666 status = readl_relaxed(uport->membase + SE_GENI_STATUS); 667 writel_relaxed(irq_clear, uport->membase + SE_GENI_S_IRQ_CLEAR); 668 if (status & S_GENI_CMD_ACTIVE) 669 qcom_geni_serial_abort_rx(uport); 670 } 671 672 static void qcom_geni_serial_handle_rx(struct uart_port *uport, bool drop) 673 { 674 u32 status; 675 u32 word_cnt; 676 u32 last_word_byte_cnt; 677 u32 last_word_partial; 678 u32 total_bytes; 679 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 680 681 status = readl_relaxed(uport->membase + SE_GENI_RX_FIFO_STATUS); 682 word_cnt = status & RX_FIFO_WC_MSK; 683 last_word_partial = status & RX_LAST; 684 last_word_byte_cnt = (status & RX_LAST_BYTE_VALID_MSK) >> 685 RX_LAST_BYTE_VALID_SHFT; 686 687 if (!word_cnt) 688 return; 689 total_bytes = port->rx_bytes_pw * (word_cnt - 1); 690 if (last_word_partial && last_word_byte_cnt) 691 total_bytes += last_word_byte_cnt; 692 else 693 total_bytes += port->rx_bytes_pw; 694 port->handle_rx(uport, total_bytes, drop); 695 } 696 697 static void qcom_geni_serial_handle_tx(struct uart_port *uport) 698 { 699 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 700 struct circ_buf *xmit = &uport->state->xmit; 701 size_t avail; 702 size_t remaining; 703 int i; 704 u32 status; 705 unsigned int chunk; 706 int tail; 707 u32 irq_en; 708 709 chunk = uart_circ_chars_pending(xmit); 710 status = readl_relaxed(uport->membase + SE_GENI_TX_FIFO_STATUS); 711 /* Both FIFO and framework buffer are drained */ 712 if (!chunk && !status) { 713 qcom_geni_serial_stop_tx(uport); 714 goto out_write_wakeup; 715 } 716 717 if (!uart_console(uport)) { 718 irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN); 719 irq_en &= ~(M_TX_FIFO_WATERMARK_EN); 720 writel_relaxed(0, uport->membase + SE_GENI_TX_WATERMARK_REG); 721 writel_relaxed(irq_en, uport->membase + SE_GENI_M_IRQ_EN); 722 } 723 724 avail = (port->tx_fifo_depth - port->tx_wm) * port->tx_bytes_pw; 725 tail = xmit->tail; 726 chunk = min3((size_t)chunk, (size_t)(UART_XMIT_SIZE - tail), avail); 727 if (!chunk) 728 goto out_write_wakeup; 729 730 qcom_geni_serial_setup_tx(uport, chunk); 731 732 remaining = chunk; 733 for (i = 0; i < chunk; ) { 734 unsigned int tx_bytes; 735 u8 buf[sizeof(u32)]; 736 int c; 737 738 memset(buf, 0, ARRAY_SIZE(buf)); 739 tx_bytes = min_t(size_t, remaining, port->tx_bytes_pw); 740 for (c = 0; c < tx_bytes ; c++) 741 buf[c] = xmit->buf[tail + c]; 742 743 iowrite32_rep(uport->membase + SE_GENI_TX_FIFOn, buf, 1); 744 745 i += tx_bytes; 746 tail += tx_bytes; 747 uport->icount.tx += tx_bytes; 748 remaining -= tx_bytes; 749 } 750 751 xmit->tail = tail & (UART_XMIT_SIZE - 1); 752 if (uart_console(uport)) 753 qcom_geni_serial_poll_tx_done(uport); 754 out_write_wakeup: 755 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 756 uart_write_wakeup(uport); 757 } 758 759 static irqreturn_t qcom_geni_serial_isr(int isr, void *dev) 760 { 761 unsigned int m_irq_status; 762 unsigned int s_irq_status; 763 struct uart_port *uport = dev; 764 unsigned long flags; 765 unsigned int m_irq_en; 766 bool drop_rx = false; 767 struct tty_port *tport = &uport->state->port; 768 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 769 770 if (uport->suspended) 771 return IRQ_NONE; 772 773 spin_lock_irqsave(&uport->lock, flags); 774 m_irq_status = readl_relaxed(uport->membase + SE_GENI_M_IRQ_STATUS); 775 s_irq_status = readl_relaxed(uport->membase + SE_GENI_S_IRQ_STATUS); 776 m_irq_en = readl_relaxed(uport->membase + SE_GENI_M_IRQ_EN); 777 writel_relaxed(m_irq_status, uport->membase + SE_GENI_M_IRQ_CLEAR); 778 writel_relaxed(s_irq_status, uport->membase + SE_GENI_S_IRQ_CLEAR); 779 780 if (WARN_ON(m_irq_status & M_ILLEGAL_CMD_EN)) 781 goto out_unlock; 782 783 if (s_irq_status & S_RX_FIFO_WR_ERR_EN) { 784 uport->icount.overrun++; 785 tty_insert_flip_char(tport, 0, TTY_OVERRUN); 786 } 787 788 if (m_irq_status & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN) && 789 m_irq_en & (M_TX_FIFO_WATERMARK_EN | M_CMD_DONE_EN)) 790 qcom_geni_serial_handle_tx(uport); 791 792 if (s_irq_status & S_GP_IRQ_0_EN || s_irq_status & S_GP_IRQ_1_EN) { 793 if (s_irq_status & S_GP_IRQ_0_EN) 794 uport->icount.parity++; 795 drop_rx = true; 796 } else if (s_irq_status & S_GP_IRQ_2_EN || 797 s_irq_status & S_GP_IRQ_3_EN) { 798 uport->icount.brk++; 799 port->brk = true; 800 } 801 802 if (s_irq_status & S_RX_FIFO_WATERMARK_EN || 803 s_irq_status & S_RX_FIFO_LAST_EN) 804 qcom_geni_serial_handle_rx(uport, drop_rx); 805 806 out_unlock: 807 spin_unlock_irqrestore(&uport->lock, flags); 808 return IRQ_HANDLED; 809 } 810 811 static void get_tx_fifo_size(struct qcom_geni_serial_port *port) 812 { 813 struct uart_port *uport; 814 815 uport = &port->uport; 816 port->tx_fifo_depth = geni_se_get_tx_fifo_depth(&port->se); 817 port->tx_fifo_width = geni_se_get_tx_fifo_width(&port->se); 818 port->rx_fifo_depth = geni_se_get_rx_fifo_depth(&port->se); 819 uport->fifosize = 820 (port->tx_fifo_depth * port->tx_fifo_width) / BITS_PER_BYTE; 821 } 822 823 static void set_rfr_wm(struct qcom_geni_serial_port *port) 824 { 825 /* 826 * Set RFR (Flow off) to FIFO_DEPTH - 2. 827 * RX WM level at 10% RX_FIFO_DEPTH. 828 * TX WM level at 10% TX_FIFO_DEPTH. 829 */ 830 port->rx_rfr = port->rx_fifo_depth - 2; 831 port->rx_wm = UART_CONSOLE_RX_WM; 832 port->tx_wm = DEF_TX_WM; 833 } 834 835 static void qcom_geni_serial_shutdown(struct uart_port *uport) 836 { 837 unsigned long flags; 838 839 /* Stop the console before stopping the current tx */ 840 if (uart_console(uport)) 841 console_stop(uport->cons); 842 843 free_irq(uport->irq, uport); 844 spin_lock_irqsave(&uport->lock, flags); 845 qcom_geni_serial_stop_tx(uport); 846 qcom_geni_serial_stop_rx(uport); 847 spin_unlock_irqrestore(&uport->lock, flags); 848 } 849 850 static int qcom_geni_serial_port_setup(struct uart_port *uport) 851 { 852 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 853 unsigned int rxstale = DEFAULT_BITS_PER_CHAR * STALE_TIMEOUT; 854 u32 proto; 855 856 if (uart_console(uport)) 857 port->tx_bytes_pw = 1; 858 else 859 port->tx_bytes_pw = 4; 860 port->rx_bytes_pw = RX_BYTES_PW; 861 862 proto = geni_se_read_proto(&port->se); 863 if (proto != GENI_SE_UART) { 864 dev_err(uport->dev, "Invalid FW loaded, proto: %d\n", proto); 865 return -ENXIO; 866 } 867 868 qcom_geni_serial_stop_rx(uport); 869 870 get_tx_fifo_size(port); 871 872 set_rfr_wm(port); 873 writel_relaxed(rxstale, uport->membase + SE_UART_RX_STALE_CNT); 874 /* 875 * Make an unconditional cancel on the main sequencer to reset 876 * it else we could end up in data loss scenarios. 877 */ 878 port->xfer_mode = GENI_SE_FIFO; 879 if (uart_console(uport)) 880 qcom_geni_serial_poll_tx_done(uport); 881 geni_se_config_packing(&port->se, BITS_PER_BYTE, port->tx_bytes_pw, 882 false, true, false); 883 geni_se_config_packing(&port->se, BITS_PER_BYTE, port->rx_bytes_pw, 884 false, false, true); 885 geni_se_init(&port->se, port->rx_wm, port->rx_rfr); 886 geni_se_select_mode(&port->se, port->xfer_mode); 887 if (!uart_console(uport)) { 888 port->rx_fifo = devm_kcalloc(uport->dev, 889 port->rx_fifo_depth, sizeof(u32), GFP_KERNEL); 890 if (!port->rx_fifo) 891 return -ENOMEM; 892 } 893 port->setup = true; 894 895 return 0; 896 } 897 898 static int qcom_geni_serial_startup(struct uart_port *uport) 899 { 900 int ret; 901 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 902 903 scnprintf(port->name, sizeof(port->name), 904 "qcom_serial_%s%d", 905 (uart_console(uport) ? "console" : "uart"), uport->line); 906 907 if (!port->setup) { 908 ret = qcom_geni_serial_port_setup(uport); 909 if (ret) 910 return ret; 911 } 912 913 ret = request_irq(uport->irq, qcom_geni_serial_isr, IRQF_TRIGGER_HIGH, 914 port->name, uport); 915 if (ret) 916 dev_err(uport->dev, "Failed to get IRQ ret %d\n", ret); 917 return ret; 918 } 919 920 static unsigned long get_clk_cfg(unsigned long clk_freq) 921 { 922 int i; 923 924 for (i = 0; i < ARRAY_SIZE(root_freq); i++) { 925 if (!(root_freq[i] % clk_freq)) 926 return root_freq[i]; 927 } 928 return 0; 929 } 930 931 static unsigned long get_clk_div_rate(unsigned int baud, unsigned int *clk_div) 932 { 933 unsigned long ser_clk; 934 unsigned long desired_clk; 935 936 desired_clk = baud * UART_OVERSAMPLING; 937 ser_clk = get_clk_cfg(desired_clk); 938 if (!ser_clk) { 939 pr_err("%s: Can't find matching DFS entry for baud %d\n", 940 __func__, baud); 941 return ser_clk; 942 } 943 944 *clk_div = ser_clk / desired_clk; 945 return ser_clk; 946 } 947 948 static void qcom_geni_serial_set_termios(struct uart_port *uport, 949 struct ktermios *termios, struct ktermios *old) 950 { 951 unsigned int baud; 952 unsigned int bits_per_char; 953 unsigned int tx_trans_cfg; 954 unsigned int tx_parity_cfg; 955 unsigned int rx_trans_cfg; 956 unsigned int rx_parity_cfg; 957 unsigned int stop_bit_len; 958 unsigned int clk_div; 959 unsigned long ser_clk_cfg; 960 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 961 unsigned long clk_rate; 962 963 qcom_geni_serial_stop_rx(uport); 964 /* baud rate */ 965 baud = uart_get_baud_rate(uport, termios, old, 300, 4000000); 966 port->baud = baud; 967 clk_rate = get_clk_div_rate(baud, &clk_div); 968 if (!clk_rate) 969 goto out_restart_rx; 970 971 uport->uartclk = clk_rate; 972 clk_set_rate(port->se.clk, clk_rate); 973 ser_clk_cfg = SER_CLK_EN; 974 ser_clk_cfg |= clk_div << CLK_DIV_SHFT; 975 976 /* parity */ 977 tx_trans_cfg = readl_relaxed(uport->membase + SE_UART_TX_TRANS_CFG); 978 tx_parity_cfg = readl_relaxed(uport->membase + SE_UART_TX_PARITY_CFG); 979 rx_trans_cfg = readl_relaxed(uport->membase + SE_UART_RX_TRANS_CFG); 980 rx_parity_cfg = readl_relaxed(uport->membase + SE_UART_RX_PARITY_CFG); 981 if (termios->c_cflag & PARENB) { 982 tx_trans_cfg |= UART_TX_PAR_EN; 983 rx_trans_cfg |= UART_RX_PAR_EN; 984 tx_parity_cfg |= PAR_CALC_EN; 985 rx_parity_cfg |= PAR_CALC_EN; 986 if (termios->c_cflag & PARODD) { 987 tx_parity_cfg |= PAR_ODD; 988 rx_parity_cfg |= PAR_ODD; 989 } else if (termios->c_cflag & CMSPAR) { 990 tx_parity_cfg |= PAR_SPACE; 991 rx_parity_cfg |= PAR_SPACE; 992 } else { 993 tx_parity_cfg |= PAR_EVEN; 994 rx_parity_cfg |= PAR_EVEN; 995 } 996 } else { 997 tx_trans_cfg &= ~UART_TX_PAR_EN; 998 rx_trans_cfg &= ~UART_RX_PAR_EN; 999 tx_parity_cfg &= ~PAR_CALC_EN; 1000 rx_parity_cfg &= ~PAR_CALC_EN; 1001 } 1002 1003 /* bits per char */ 1004 switch (termios->c_cflag & CSIZE) { 1005 case CS5: 1006 bits_per_char = 5; 1007 break; 1008 case CS6: 1009 bits_per_char = 6; 1010 break; 1011 case CS7: 1012 bits_per_char = 7; 1013 break; 1014 case CS8: 1015 default: 1016 bits_per_char = 8; 1017 break; 1018 } 1019 1020 /* stop bits */ 1021 if (termios->c_cflag & CSTOPB) 1022 stop_bit_len = TX_STOP_BIT_LEN_2; 1023 else 1024 stop_bit_len = TX_STOP_BIT_LEN_1; 1025 1026 /* flow control, clear the CTS_MASK bit if using flow control. */ 1027 if (termios->c_cflag & CRTSCTS) 1028 tx_trans_cfg &= ~UART_CTS_MASK; 1029 else 1030 tx_trans_cfg |= UART_CTS_MASK; 1031 1032 if (baud) 1033 uart_update_timeout(uport, termios->c_cflag, baud); 1034 1035 if (!uart_console(uport)) 1036 writel_relaxed(port->loopback, 1037 uport->membase + SE_UART_LOOPBACK_CFG); 1038 writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG); 1039 writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG); 1040 writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG); 1041 writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG); 1042 writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN); 1043 writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN); 1044 writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN); 1045 writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_M_CLK_CFG); 1046 writel_relaxed(ser_clk_cfg, uport->membase + GENI_SER_S_CLK_CFG); 1047 out_restart_rx: 1048 qcom_geni_serial_start_rx(uport); 1049 } 1050 1051 static unsigned int qcom_geni_serial_tx_empty(struct uart_port *uport) 1052 { 1053 return !readl(uport->membase + SE_GENI_TX_FIFO_STATUS); 1054 } 1055 1056 #ifdef CONFIG_SERIAL_QCOM_GENI_CONSOLE 1057 static int __init qcom_geni_console_setup(struct console *co, char *options) 1058 { 1059 struct uart_port *uport; 1060 struct qcom_geni_serial_port *port; 1061 int baud; 1062 int bits = 8; 1063 int parity = 'n'; 1064 int flow = 'n'; 1065 int ret; 1066 1067 if (co->index >= GENI_UART_CONS_PORTS || co->index < 0) 1068 return -ENXIO; 1069 1070 port = get_port_from_line(co->index, true); 1071 if (IS_ERR(port)) { 1072 pr_err("Invalid line %d\n", co->index); 1073 return PTR_ERR(port); 1074 } 1075 1076 uport = &port->uport; 1077 1078 if (unlikely(!uport->membase)) 1079 return -ENXIO; 1080 1081 if (!port->setup) { 1082 ret = qcom_geni_serial_port_setup(uport); 1083 if (ret) 1084 return ret; 1085 } 1086 1087 if (options) 1088 uart_parse_options(options, &baud, &parity, &bits, &flow); 1089 1090 return uart_set_options(uport, co, baud, parity, bits, flow); 1091 } 1092 1093 static void qcom_geni_serial_earlycon_write(struct console *con, 1094 const char *s, unsigned int n) 1095 { 1096 struct earlycon_device *dev = con->data; 1097 1098 __qcom_geni_serial_console_write(&dev->port, s, n); 1099 } 1100 1101 static int __init qcom_geni_serial_earlycon_setup(struct earlycon_device *dev, 1102 const char *opt) 1103 { 1104 struct uart_port *uport = &dev->port; 1105 u32 tx_trans_cfg; 1106 u32 tx_parity_cfg = 0; /* Disable Tx Parity */ 1107 u32 rx_trans_cfg = 0; 1108 u32 rx_parity_cfg = 0; /* Disable Rx Parity */ 1109 u32 stop_bit_len = 0; /* Default stop bit length - 1 bit */ 1110 u32 bits_per_char; 1111 struct geni_se se; 1112 1113 if (!uport->membase) 1114 return -EINVAL; 1115 1116 memset(&se, 0, sizeof(se)); 1117 se.base = uport->membase; 1118 if (geni_se_read_proto(&se) != GENI_SE_UART) 1119 return -ENXIO; 1120 /* 1121 * Ignore Flow control. 1122 * n = 8. 1123 */ 1124 tx_trans_cfg = UART_CTS_MASK; 1125 bits_per_char = BITS_PER_BYTE; 1126 1127 /* 1128 * Make an unconditional cancel on the main sequencer to reset 1129 * it else we could end up in data loss scenarios. 1130 */ 1131 qcom_geni_serial_poll_tx_done(uport); 1132 qcom_geni_serial_abort_rx(uport); 1133 geni_se_config_packing(&se, BITS_PER_BYTE, 1, false, true, false); 1134 geni_se_init(&se, DEF_FIFO_DEPTH_WORDS / 2, DEF_FIFO_DEPTH_WORDS - 2); 1135 geni_se_select_mode(&se, GENI_SE_FIFO); 1136 1137 writel_relaxed(tx_trans_cfg, uport->membase + SE_UART_TX_TRANS_CFG); 1138 writel_relaxed(tx_parity_cfg, uport->membase + SE_UART_TX_PARITY_CFG); 1139 writel_relaxed(rx_trans_cfg, uport->membase + SE_UART_RX_TRANS_CFG); 1140 writel_relaxed(rx_parity_cfg, uport->membase + SE_UART_RX_PARITY_CFG); 1141 writel_relaxed(bits_per_char, uport->membase + SE_UART_TX_WORD_LEN); 1142 writel_relaxed(bits_per_char, uport->membase + SE_UART_RX_WORD_LEN); 1143 writel_relaxed(stop_bit_len, uport->membase + SE_UART_TX_STOP_BIT_LEN); 1144 1145 dev->con->write = qcom_geni_serial_earlycon_write; 1146 dev->con->setup = NULL; 1147 return 0; 1148 } 1149 OF_EARLYCON_DECLARE(qcom_geni, "qcom,geni-debug-uart", 1150 qcom_geni_serial_earlycon_setup); 1151 1152 static int __init console_register(struct uart_driver *drv) 1153 { 1154 return uart_register_driver(drv); 1155 } 1156 1157 static void console_unregister(struct uart_driver *drv) 1158 { 1159 uart_unregister_driver(drv); 1160 } 1161 1162 static struct console cons_ops = { 1163 .name = "ttyMSM", 1164 .write = qcom_geni_serial_console_write, 1165 .device = uart_console_device, 1166 .setup = qcom_geni_console_setup, 1167 .flags = CON_PRINTBUFFER, 1168 .index = -1, 1169 .data = &qcom_geni_console_driver, 1170 }; 1171 1172 static struct uart_driver qcom_geni_console_driver = { 1173 .owner = THIS_MODULE, 1174 .driver_name = "qcom_geni_console", 1175 .dev_name = "ttyMSM", 1176 .nr = GENI_UART_CONS_PORTS, 1177 .cons = &cons_ops, 1178 }; 1179 #else 1180 static int console_register(struct uart_driver *drv) 1181 { 1182 return 0; 1183 } 1184 1185 static void console_unregister(struct uart_driver *drv) 1186 { 1187 } 1188 #endif /* CONFIG_SERIAL_QCOM_GENI_CONSOLE */ 1189 1190 static struct uart_driver qcom_geni_uart_driver = { 1191 .owner = THIS_MODULE, 1192 .driver_name = "qcom_geni_uart", 1193 .dev_name = "ttyHS", 1194 .nr = GENI_UART_PORTS, 1195 }; 1196 1197 static void qcom_geni_serial_pm(struct uart_port *uport, 1198 unsigned int new_state, unsigned int old_state) 1199 { 1200 struct qcom_geni_serial_port *port = to_dev_port(uport, uport); 1201 1202 /* If we've never been called, treat it as off */ 1203 if (old_state == UART_PM_STATE_UNDEFINED) 1204 old_state = UART_PM_STATE_OFF; 1205 1206 if (new_state == UART_PM_STATE_ON && old_state == UART_PM_STATE_OFF) 1207 geni_se_resources_on(&port->se); 1208 else if (new_state == UART_PM_STATE_OFF && 1209 old_state == UART_PM_STATE_ON) 1210 geni_se_resources_off(&port->se); 1211 } 1212 1213 static const struct uart_ops qcom_geni_console_pops = { 1214 .tx_empty = qcom_geni_serial_tx_empty, 1215 .stop_tx = qcom_geni_serial_stop_tx, 1216 .start_tx = qcom_geni_serial_start_tx, 1217 .stop_rx = qcom_geni_serial_stop_rx, 1218 .set_termios = qcom_geni_serial_set_termios, 1219 .startup = qcom_geni_serial_startup, 1220 .request_port = qcom_geni_serial_request_port, 1221 .config_port = qcom_geni_serial_config_port, 1222 .shutdown = qcom_geni_serial_shutdown, 1223 .type = qcom_geni_serial_get_type, 1224 .set_mctrl = qcom_geni_serial_set_mctrl, 1225 .get_mctrl = qcom_geni_serial_get_mctrl, 1226 #ifdef CONFIG_CONSOLE_POLL 1227 .poll_get_char = qcom_geni_serial_get_char, 1228 .poll_put_char = qcom_geni_serial_poll_put_char, 1229 #endif 1230 .pm = qcom_geni_serial_pm, 1231 }; 1232 1233 static const struct uart_ops qcom_geni_uart_pops = { 1234 .tx_empty = qcom_geni_serial_tx_empty, 1235 .stop_tx = qcom_geni_serial_stop_tx, 1236 .start_tx = qcom_geni_serial_start_tx, 1237 .stop_rx = qcom_geni_serial_stop_rx, 1238 .set_termios = qcom_geni_serial_set_termios, 1239 .startup = qcom_geni_serial_startup, 1240 .request_port = qcom_geni_serial_request_port, 1241 .config_port = qcom_geni_serial_config_port, 1242 .shutdown = qcom_geni_serial_shutdown, 1243 .type = qcom_geni_serial_get_type, 1244 .set_mctrl = qcom_geni_serial_set_mctrl, 1245 .get_mctrl = qcom_geni_serial_get_mctrl, 1246 .pm = qcom_geni_serial_pm, 1247 }; 1248 1249 static int qcom_geni_serial_probe(struct platform_device *pdev) 1250 { 1251 int ret = 0; 1252 int line = -1; 1253 struct qcom_geni_serial_port *port; 1254 struct uart_port *uport; 1255 struct resource *res; 1256 int irq; 1257 bool console = false; 1258 struct uart_driver *drv; 1259 1260 if (of_device_is_compatible(pdev->dev.of_node, "qcom,geni-debug-uart")) 1261 console = true; 1262 1263 if (console) { 1264 drv = &qcom_geni_console_driver; 1265 line = of_alias_get_id(pdev->dev.of_node, "serial"); 1266 } else { 1267 drv = &qcom_geni_uart_driver; 1268 line = of_alias_get_id(pdev->dev.of_node, "hsuart"); 1269 } 1270 1271 port = get_port_from_line(line, console); 1272 if (IS_ERR(port)) { 1273 dev_err(&pdev->dev, "Invalid line %d\n", line); 1274 return PTR_ERR(port); 1275 } 1276 1277 uport = &port->uport; 1278 /* Don't allow 2 drivers to access the same port */ 1279 if (uport->private_data) 1280 return -ENODEV; 1281 1282 uport->dev = &pdev->dev; 1283 port->se.dev = &pdev->dev; 1284 port->se.wrapper = dev_get_drvdata(pdev->dev.parent); 1285 port->se.clk = devm_clk_get(&pdev->dev, "se"); 1286 if (IS_ERR(port->se.clk)) { 1287 ret = PTR_ERR(port->se.clk); 1288 dev_err(&pdev->dev, "Err getting SE Core clk %d\n", ret); 1289 return ret; 1290 } 1291 1292 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1293 if (!res) 1294 return -EINVAL; 1295 uport->mapbase = res->start; 1296 1297 port->tx_fifo_depth = DEF_FIFO_DEPTH_WORDS; 1298 port->rx_fifo_depth = DEF_FIFO_DEPTH_WORDS; 1299 port->tx_fifo_width = DEF_FIFO_WIDTH_BITS; 1300 1301 irq = platform_get_irq(pdev, 0); 1302 if (irq < 0) { 1303 dev_err(&pdev->dev, "Failed to get IRQ %d\n", irq); 1304 return irq; 1305 } 1306 uport->irq = irq; 1307 1308 uport->private_data = drv; 1309 platform_set_drvdata(pdev, port); 1310 port->handle_rx = console ? handle_rx_console : handle_rx_uart; 1311 if (!console) 1312 device_create_file(uport->dev, &dev_attr_loopback); 1313 return uart_add_one_port(drv, uport); 1314 } 1315 1316 static int qcom_geni_serial_remove(struct platform_device *pdev) 1317 { 1318 struct qcom_geni_serial_port *port = platform_get_drvdata(pdev); 1319 struct uart_driver *drv = port->uport.private_data; 1320 1321 uart_remove_one_port(drv, &port->uport); 1322 return 0; 1323 } 1324 1325 static int __maybe_unused qcom_geni_serial_sys_suspend_noirq(struct device *dev) 1326 { 1327 struct qcom_geni_serial_port *port = dev_get_drvdata(dev); 1328 struct uart_port *uport = &port->uport; 1329 1330 if (uart_console(uport)) { 1331 uart_suspend_port(uport->private_data, uport); 1332 } else { 1333 struct uart_state *state = uport->state; 1334 /* 1335 * If the port is open, deny system suspend. 1336 */ 1337 if (state->pm_state == UART_PM_STATE_ON) 1338 return -EBUSY; 1339 } 1340 1341 return 0; 1342 } 1343 1344 static int __maybe_unused qcom_geni_serial_sys_resume_noirq(struct device *dev) 1345 { 1346 struct qcom_geni_serial_port *port = dev_get_drvdata(dev); 1347 struct uart_port *uport = &port->uport; 1348 1349 if (uart_console(uport) && 1350 console_suspend_enabled && uport->suspended) { 1351 uart_resume_port(uport->private_data, uport); 1352 /* 1353 * uart_suspend_port() invokes port shutdown which in turn 1354 * frees the irq. uart_resume_port invokes port startup which 1355 * performs request_irq. The request_irq auto-enables the IRQ. 1356 * In addition, resume_noirq implicitly enables the IRQ and 1357 * leads to an unbalanced IRQ enable warning. Disable the IRQ 1358 * before returning so that the warning is suppressed. 1359 */ 1360 disable_irq(uport->irq); 1361 } 1362 return 0; 1363 } 1364 1365 static const struct dev_pm_ops qcom_geni_serial_pm_ops = { 1366 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(qcom_geni_serial_sys_suspend_noirq, 1367 qcom_geni_serial_sys_resume_noirq) 1368 }; 1369 1370 static const struct of_device_id qcom_geni_serial_match_table[] = { 1371 { .compatible = "qcom,geni-debug-uart", }, 1372 { .compatible = "qcom,geni-uart", }, 1373 {} 1374 }; 1375 MODULE_DEVICE_TABLE(of, qcom_geni_serial_match_table); 1376 1377 static struct platform_driver qcom_geni_serial_platform_driver = { 1378 .remove = qcom_geni_serial_remove, 1379 .probe = qcom_geni_serial_probe, 1380 .driver = { 1381 .name = "qcom_geni_serial", 1382 .of_match_table = qcom_geni_serial_match_table, 1383 .pm = &qcom_geni_serial_pm_ops, 1384 }, 1385 }; 1386 1387 static int __init qcom_geni_serial_init(void) 1388 { 1389 int ret; 1390 1391 ret = console_register(&qcom_geni_console_driver); 1392 if (ret) 1393 return ret; 1394 1395 ret = uart_register_driver(&qcom_geni_uart_driver); 1396 if (ret) { 1397 console_unregister(&qcom_geni_console_driver); 1398 return ret; 1399 } 1400 1401 ret = platform_driver_register(&qcom_geni_serial_platform_driver); 1402 if (ret) { 1403 console_unregister(&qcom_geni_console_driver); 1404 uart_unregister_driver(&qcom_geni_uart_driver); 1405 } 1406 return ret; 1407 } 1408 module_init(qcom_geni_serial_init); 1409 1410 static void __exit qcom_geni_serial_exit(void) 1411 { 1412 platform_driver_unregister(&qcom_geni_serial_platform_driver); 1413 console_unregister(&qcom_geni_console_driver); 1414 uart_unregister_driver(&qcom_geni_uart_driver); 1415 } 1416 module_exit(qcom_geni_serial_exit); 1417 1418 MODULE_DESCRIPTION("Serial driver for GENI based QUP cores"); 1419 MODULE_LICENSE("GPL v2"); 1420