1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2012-2015 Spreadtrum Communications Inc. 4 * 5 * This software is licensed under the terms of the GNU General Public 6 * License version 2, as published by the Free Software Foundation, and 7 * may be copied, distributed, and modified under those terms. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #if defined(CONFIG_SERIAL_SPRD_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) 16 #define SUPPORT_SYSRQ 17 #endif 18 19 #include <linux/clk.h> 20 #include <linux/console.h> 21 #include <linux/delay.h> 22 #include <linux/io.h> 23 #include <linux/ioport.h> 24 #include <linux/kernel.h> 25 #include <linux/module.h> 26 #include <linux/of.h> 27 #include <linux/platform_device.h> 28 #include <linux/serial_core.h> 29 #include <linux/serial.h> 30 #include <linux/slab.h> 31 #include <linux/tty.h> 32 #include <linux/tty_flip.h> 33 34 /* device name */ 35 #define UART_NR_MAX 8 36 #define SPRD_TTY_NAME "ttyS" 37 #define SPRD_FIFO_SIZE 128 38 #define SPRD_DEF_RATE 26000000 39 #define SPRD_BAUD_IO_LIMIT 3000000 40 #define SPRD_TIMEOUT 256000 41 42 /* the offset of serial registers and BITs for them */ 43 /* data registers */ 44 #define SPRD_TXD 0x0000 45 #define SPRD_RXD 0x0004 46 47 /* line status register and its BITs */ 48 #define SPRD_LSR 0x0008 49 #define SPRD_LSR_OE BIT(4) 50 #define SPRD_LSR_FE BIT(3) 51 #define SPRD_LSR_PE BIT(2) 52 #define SPRD_LSR_BI BIT(7) 53 #define SPRD_LSR_TX_OVER BIT(15) 54 55 /* data number in TX and RX fifo */ 56 #define SPRD_STS1 0x000C 57 58 /* interrupt enable register and its BITs */ 59 #define SPRD_IEN 0x0010 60 #define SPRD_IEN_RX_FULL BIT(0) 61 #define SPRD_IEN_TX_EMPTY BIT(1) 62 #define SPRD_IEN_BREAK_DETECT BIT(7) 63 #define SPRD_IEN_TIMEOUT BIT(13) 64 65 /* interrupt clear register */ 66 #define SPRD_ICLR 0x0014 67 #define SPRD_ICLR_TIMEOUT BIT(13) 68 69 /* line control register */ 70 #define SPRD_LCR 0x0018 71 #define SPRD_LCR_STOP_1BIT 0x10 72 #define SPRD_LCR_STOP_2BIT 0x30 73 #define SPRD_LCR_DATA_LEN (BIT(2) | BIT(3)) 74 #define SPRD_LCR_DATA_LEN5 0x0 75 #define SPRD_LCR_DATA_LEN6 0x4 76 #define SPRD_LCR_DATA_LEN7 0x8 77 #define SPRD_LCR_DATA_LEN8 0xc 78 #define SPRD_LCR_PARITY (BIT(0) | BIT(1)) 79 #define SPRD_LCR_PARITY_EN 0x2 80 #define SPRD_LCR_EVEN_PAR 0x0 81 #define SPRD_LCR_ODD_PAR 0x1 82 83 /* control register 1 */ 84 #define SPRD_CTL1 0x001C 85 #define RX_HW_FLOW_CTL_THLD BIT(6) 86 #define RX_HW_FLOW_CTL_EN BIT(7) 87 #define TX_HW_FLOW_CTL_EN BIT(8) 88 #define RX_TOUT_THLD_DEF 0x3E00 89 #define RX_HFC_THLD_DEF 0x40 90 91 /* fifo threshold register */ 92 #define SPRD_CTL2 0x0020 93 #define THLD_TX_EMPTY 0x40 94 #define THLD_RX_FULL 0x40 95 96 /* config baud rate register */ 97 #define SPRD_CLKD0 0x0024 98 #define SPRD_CLKD1 0x0028 99 100 /* interrupt mask status register */ 101 #define SPRD_IMSR 0x002C 102 #define SPRD_IMSR_RX_FIFO_FULL BIT(0) 103 #define SPRD_IMSR_TX_FIFO_EMPTY BIT(1) 104 #define SPRD_IMSR_BREAK_DETECT BIT(7) 105 #define SPRD_IMSR_TIMEOUT BIT(13) 106 107 struct reg_backup { 108 u32 ien; 109 u32 ctrl0; 110 u32 ctrl1; 111 u32 ctrl2; 112 u32 clkd0; 113 u32 clkd1; 114 u32 dspwait; 115 }; 116 117 struct sprd_uart_port { 118 struct uart_port port; 119 struct reg_backup reg_bak; 120 char name[16]; 121 }; 122 123 static struct sprd_uart_port *sprd_port[UART_NR_MAX]; 124 static int sprd_ports_num; 125 126 static inline unsigned int serial_in(struct uart_port *port, int offset) 127 { 128 return readl_relaxed(port->membase + offset); 129 } 130 131 static inline void serial_out(struct uart_port *port, int offset, int value) 132 { 133 writel_relaxed(value, port->membase + offset); 134 } 135 136 static unsigned int sprd_tx_empty(struct uart_port *port) 137 { 138 if (serial_in(port, SPRD_STS1) & 0xff00) 139 return 0; 140 else 141 return TIOCSER_TEMT; 142 } 143 144 static unsigned int sprd_get_mctrl(struct uart_port *port) 145 { 146 return TIOCM_DSR | TIOCM_CTS; 147 } 148 149 static void sprd_set_mctrl(struct uart_port *port, unsigned int mctrl) 150 { 151 /* nothing to do */ 152 } 153 154 static void sprd_stop_tx(struct uart_port *port) 155 { 156 unsigned int ien, iclr; 157 158 iclr = serial_in(port, SPRD_ICLR); 159 ien = serial_in(port, SPRD_IEN); 160 161 iclr |= SPRD_IEN_TX_EMPTY; 162 ien &= ~SPRD_IEN_TX_EMPTY; 163 164 serial_out(port, SPRD_ICLR, iclr); 165 serial_out(port, SPRD_IEN, ien); 166 } 167 168 static void sprd_start_tx(struct uart_port *port) 169 { 170 unsigned int ien; 171 172 ien = serial_in(port, SPRD_IEN); 173 if (!(ien & SPRD_IEN_TX_EMPTY)) { 174 ien |= SPRD_IEN_TX_EMPTY; 175 serial_out(port, SPRD_IEN, ien); 176 } 177 } 178 179 static void sprd_stop_rx(struct uart_port *port) 180 { 181 unsigned int ien, iclr; 182 183 iclr = serial_in(port, SPRD_ICLR); 184 ien = serial_in(port, SPRD_IEN); 185 186 ien &= ~(SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT); 187 iclr |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT; 188 189 serial_out(port, SPRD_IEN, ien); 190 serial_out(port, SPRD_ICLR, iclr); 191 } 192 193 /* The Sprd serial does not support this function. */ 194 static void sprd_break_ctl(struct uart_port *port, int break_state) 195 { 196 /* nothing to do */ 197 } 198 199 static int handle_lsr_errors(struct uart_port *port, 200 unsigned int *flag, 201 unsigned int *lsr) 202 { 203 int ret = 0; 204 205 /* statistics */ 206 if (*lsr & SPRD_LSR_BI) { 207 *lsr &= ~(SPRD_LSR_FE | SPRD_LSR_PE); 208 port->icount.brk++; 209 ret = uart_handle_break(port); 210 if (ret) 211 return ret; 212 } else if (*lsr & SPRD_LSR_PE) 213 port->icount.parity++; 214 else if (*lsr & SPRD_LSR_FE) 215 port->icount.frame++; 216 if (*lsr & SPRD_LSR_OE) 217 port->icount.overrun++; 218 219 /* mask off conditions which should be ignored */ 220 *lsr &= port->read_status_mask; 221 if (*lsr & SPRD_LSR_BI) 222 *flag = TTY_BREAK; 223 else if (*lsr & SPRD_LSR_PE) 224 *flag = TTY_PARITY; 225 else if (*lsr & SPRD_LSR_FE) 226 *flag = TTY_FRAME; 227 228 return ret; 229 } 230 231 static inline void sprd_rx(struct uart_port *port) 232 { 233 struct tty_port *tty = &port->state->port; 234 unsigned int ch, flag, lsr, max_count = SPRD_TIMEOUT; 235 236 while ((serial_in(port, SPRD_STS1) & 0x00ff) && max_count--) { 237 lsr = serial_in(port, SPRD_LSR); 238 ch = serial_in(port, SPRD_RXD); 239 flag = TTY_NORMAL; 240 port->icount.rx++; 241 242 if (lsr & (SPRD_LSR_BI | SPRD_LSR_PE | 243 SPRD_LSR_FE | SPRD_LSR_OE)) 244 if (handle_lsr_errors(port, &lsr, &flag)) 245 continue; 246 if (uart_handle_sysrq_char(port, ch)) 247 continue; 248 249 uart_insert_char(port, lsr, SPRD_LSR_OE, ch, flag); 250 } 251 252 tty_flip_buffer_push(tty); 253 } 254 255 static inline void sprd_tx(struct uart_port *port) 256 { 257 struct circ_buf *xmit = &port->state->xmit; 258 int count; 259 260 if (port->x_char) { 261 serial_out(port, SPRD_TXD, port->x_char); 262 port->icount.tx++; 263 port->x_char = 0; 264 return; 265 } 266 267 if (uart_circ_empty(xmit) || uart_tx_stopped(port)) { 268 sprd_stop_tx(port); 269 return; 270 } 271 272 count = THLD_TX_EMPTY; 273 do { 274 serial_out(port, SPRD_TXD, xmit->buf[xmit->tail]); 275 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); 276 port->icount.tx++; 277 if (uart_circ_empty(xmit)) 278 break; 279 } while (--count > 0); 280 281 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 282 uart_write_wakeup(port); 283 284 if (uart_circ_empty(xmit)) 285 sprd_stop_tx(port); 286 } 287 288 /* this handles the interrupt from one port */ 289 static irqreturn_t sprd_handle_irq(int irq, void *dev_id) 290 { 291 struct uart_port *port = dev_id; 292 unsigned int ims; 293 294 spin_lock(&port->lock); 295 296 ims = serial_in(port, SPRD_IMSR); 297 298 if (!ims) { 299 spin_unlock(&port->lock); 300 return IRQ_NONE; 301 } 302 303 if (ims & SPRD_IMSR_TIMEOUT) 304 serial_out(port, SPRD_ICLR, SPRD_ICLR_TIMEOUT); 305 306 if (ims & (SPRD_IMSR_RX_FIFO_FULL | 307 SPRD_IMSR_BREAK_DETECT | SPRD_IMSR_TIMEOUT)) 308 sprd_rx(port); 309 310 if (ims & SPRD_IMSR_TX_FIFO_EMPTY) 311 sprd_tx(port); 312 313 spin_unlock(&port->lock); 314 315 return IRQ_HANDLED; 316 } 317 318 static int sprd_startup(struct uart_port *port) 319 { 320 int ret = 0; 321 unsigned int ien, fc; 322 unsigned int timeout; 323 struct sprd_uart_port *sp; 324 unsigned long flags; 325 326 serial_out(port, SPRD_CTL2, ((THLD_TX_EMPTY << 8) | THLD_RX_FULL)); 327 328 /* clear rx fifo */ 329 timeout = SPRD_TIMEOUT; 330 while (timeout-- && serial_in(port, SPRD_STS1) & 0x00ff) 331 serial_in(port, SPRD_RXD); 332 333 /* clear tx fifo */ 334 timeout = SPRD_TIMEOUT; 335 while (timeout-- && serial_in(port, SPRD_STS1) & 0xff00) 336 cpu_relax(); 337 338 /* clear interrupt */ 339 serial_out(port, SPRD_IEN, 0); 340 serial_out(port, SPRD_ICLR, ~0); 341 342 /* allocate irq */ 343 sp = container_of(port, struct sprd_uart_port, port); 344 snprintf(sp->name, sizeof(sp->name), "sprd_serial%d", port->line); 345 ret = devm_request_irq(port->dev, port->irq, sprd_handle_irq, 346 IRQF_SHARED, sp->name, port); 347 if (ret) { 348 dev_err(port->dev, "fail to request serial irq %d, ret=%d\n", 349 port->irq, ret); 350 return ret; 351 } 352 fc = serial_in(port, SPRD_CTL1); 353 fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF; 354 serial_out(port, SPRD_CTL1, fc); 355 356 /* enable interrupt */ 357 spin_lock_irqsave(&port->lock, flags); 358 ien = serial_in(port, SPRD_IEN); 359 ien |= SPRD_IEN_RX_FULL | SPRD_IEN_BREAK_DETECT | SPRD_IEN_TIMEOUT; 360 serial_out(port, SPRD_IEN, ien); 361 spin_unlock_irqrestore(&port->lock, flags); 362 363 return 0; 364 } 365 366 static void sprd_shutdown(struct uart_port *port) 367 { 368 serial_out(port, SPRD_IEN, 0); 369 serial_out(port, SPRD_ICLR, ~0); 370 devm_free_irq(port->dev, port->irq, port); 371 } 372 373 static void sprd_set_termios(struct uart_port *port, 374 struct ktermios *termios, 375 struct ktermios *old) 376 { 377 unsigned int baud, quot; 378 unsigned int lcr = 0, fc; 379 unsigned long flags; 380 381 /* ask the core to calculate the divisor for us */ 382 baud = uart_get_baud_rate(port, termios, old, 0, SPRD_BAUD_IO_LIMIT); 383 384 quot = (unsigned int)((port->uartclk + baud / 2) / baud); 385 386 /* set data length */ 387 switch (termios->c_cflag & CSIZE) { 388 case CS5: 389 lcr |= SPRD_LCR_DATA_LEN5; 390 break; 391 case CS6: 392 lcr |= SPRD_LCR_DATA_LEN6; 393 break; 394 case CS7: 395 lcr |= SPRD_LCR_DATA_LEN7; 396 break; 397 case CS8: 398 default: 399 lcr |= SPRD_LCR_DATA_LEN8; 400 break; 401 } 402 403 /* calculate stop bits */ 404 lcr &= ~(SPRD_LCR_STOP_1BIT | SPRD_LCR_STOP_2BIT); 405 if (termios->c_cflag & CSTOPB) 406 lcr |= SPRD_LCR_STOP_2BIT; 407 else 408 lcr |= SPRD_LCR_STOP_1BIT; 409 410 /* calculate parity */ 411 lcr &= ~SPRD_LCR_PARITY; 412 termios->c_cflag &= ~CMSPAR; /* no support mark/space */ 413 if (termios->c_cflag & PARENB) { 414 lcr |= SPRD_LCR_PARITY_EN; 415 if (termios->c_cflag & PARODD) 416 lcr |= SPRD_LCR_ODD_PAR; 417 else 418 lcr |= SPRD_LCR_EVEN_PAR; 419 } 420 421 spin_lock_irqsave(&port->lock, flags); 422 423 /* update the per-port timeout */ 424 uart_update_timeout(port, termios->c_cflag, baud); 425 426 port->read_status_mask = SPRD_LSR_OE; 427 if (termios->c_iflag & INPCK) 428 port->read_status_mask |= SPRD_LSR_FE | SPRD_LSR_PE; 429 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK)) 430 port->read_status_mask |= SPRD_LSR_BI; 431 432 /* characters to ignore */ 433 port->ignore_status_mask = 0; 434 if (termios->c_iflag & IGNPAR) 435 port->ignore_status_mask |= SPRD_LSR_PE | SPRD_LSR_FE; 436 if (termios->c_iflag & IGNBRK) { 437 port->ignore_status_mask |= SPRD_LSR_BI; 438 /* 439 * If we're ignoring parity and break indicators, 440 * ignore overruns too (for real raw support). 441 */ 442 if (termios->c_iflag & IGNPAR) 443 port->ignore_status_mask |= SPRD_LSR_OE; 444 } 445 446 /* flow control */ 447 fc = serial_in(port, SPRD_CTL1); 448 fc &= ~(RX_HW_FLOW_CTL_THLD | RX_HW_FLOW_CTL_EN | TX_HW_FLOW_CTL_EN); 449 if (termios->c_cflag & CRTSCTS) { 450 fc |= RX_HW_FLOW_CTL_THLD; 451 fc |= RX_HW_FLOW_CTL_EN; 452 fc |= TX_HW_FLOW_CTL_EN; 453 } 454 455 /* clock divider bit0~bit15 */ 456 serial_out(port, SPRD_CLKD0, quot & 0xffff); 457 458 /* clock divider bit16~bit20 */ 459 serial_out(port, SPRD_CLKD1, (quot & 0x1f0000) >> 16); 460 serial_out(port, SPRD_LCR, lcr); 461 fc |= RX_TOUT_THLD_DEF | RX_HFC_THLD_DEF; 462 serial_out(port, SPRD_CTL1, fc); 463 464 spin_unlock_irqrestore(&port->lock, flags); 465 466 /* Don't rewrite B0 */ 467 if (tty_termios_baud_rate(termios)) 468 tty_termios_encode_baud_rate(termios, baud, baud); 469 } 470 471 static const char *sprd_type(struct uart_port *port) 472 { 473 return "SPX"; 474 } 475 476 static void sprd_release_port(struct uart_port *port) 477 { 478 /* nothing to do */ 479 } 480 481 static int sprd_request_port(struct uart_port *port) 482 { 483 return 0; 484 } 485 486 static void sprd_config_port(struct uart_port *port, int flags) 487 { 488 if (flags & UART_CONFIG_TYPE) 489 port->type = PORT_SPRD; 490 } 491 492 static int sprd_verify_port(struct uart_port *port, 493 struct serial_struct *ser) 494 { 495 if (ser->type != PORT_SPRD) 496 return -EINVAL; 497 if (port->irq != ser->irq) 498 return -EINVAL; 499 if (port->iotype != ser->io_type) 500 return -EINVAL; 501 return 0; 502 } 503 504 static const struct uart_ops serial_sprd_ops = { 505 .tx_empty = sprd_tx_empty, 506 .get_mctrl = sprd_get_mctrl, 507 .set_mctrl = sprd_set_mctrl, 508 .stop_tx = sprd_stop_tx, 509 .start_tx = sprd_start_tx, 510 .stop_rx = sprd_stop_rx, 511 .break_ctl = sprd_break_ctl, 512 .startup = sprd_startup, 513 .shutdown = sprd_shutdown, 514 .set_termios = sprd_set_termios, 515 .type = sprd_type, 516 .release_port = sprd_release_port, 517 .request_port = sprd_request_port, 518 .config_port = sprd_config_port, 519 .verify_port = sprd_verify_port, 520 }; 521 522 #ifdef CONFIG_SERIAL_SPRD_CONSOLE 523 static void wait_for_xmitr(struct uart_port *port) 524 { 525 unsigned int status, tmout = 10000; 526 527 /* wait up to 10ms for the character(s) to be sent */ 528 do { 529 status = serial_in(port, SPRD_STS1); 530 if (--tmout == 0) 531 break; 532 udelay(1); 533 } while (status & 0xff00); 534 } 535 536 static void sprd_console_putchar(struct uart_port *port, int ch) 537 { 538 wait_for_xmitr(port); 539 serial_out(port, SPRD_TXD, ch); 540 } 541 542 static void sprd_console_write(struct console *co, const char *s, 543 unsigned int count) 544 { 545 struct uart_port *port = &sprd_port[co->index]->port; 546 int locked = 1; 547 unsigned long flags; 548 549 if (port->sysrq) 550 locked = 0; 551 else if (oops_in_progress) 552 locked = spin_trylock_irqsave(&port->lock, flags); 553 else 554 spin_lock_irqsave(&port->lock, flags); 555 556 uart_console_write(port, s, count, sprd_console_putchar); 557 558 /* wait for transmitter to become empty */ 559 wait_for_xmitr(port); 560 561 if (locked) 562 spin_unlock_irqrestore(&port->lock, flags); 563 } 564 565 static int __init sprd_console_setup(struct console *co, char *options) 566 { 567 struct uart_port *port; 568 int baud = 115200; 569 int bits = 8; 570 int parity = 'n'; 571 int flow = 'n'; 572 573 if (co->index >= UART_NR_MAX || co->index < 0) 574 co->index = 0; 575 576 port = &sprd_port[co->index]->port; 577 if (port == NULL) { 578 pr_info("serial port %d not yet initialized\n", co->index); 579 return -ENODEV; 580 } 581 if (options) 582 uart_parse_options(options, &baud, &parity, &bits, &flow); 583 584 return uart_set_options(port, co, baud, parity, bits, flow); 585 } 586 587 static struct uart_driver sprd_uart_driver; 588 static struct console sprd_console = { 589 .name = SPRD_TTY_NAME, 590 .write = sprd_console_write, 591 .device = uart_console_device, 592 .setup = sprd_console_setup, 593 .flags = CON_PRINTBUFFER, 594 .index = -1, 595 .data = &sprd_uart_driver, 596 }; 597 598 #define SPRD_CONSOLE (&sprd_console) 599 600 /* Support for earlycon */ 601 static void sprd_putc(struct uart_port *port, int c) 602 { 603 unsigned int timeout = SPRD_TIMEOUT; 604 605 while (timeout-- && 606 !(readl(port->membase + SPRD_LSR) & SPRD_LSR_TX_OVER)) 607 cpu_relax(); 608 609 writeb(c, port->membase + SPRD_TXD); 610 } 611 612 static void sprd_early_write(struct console *con, const char *s, 613 unsigned n) 614 { 615 struct earlycon_device *dev = con->data; 616 617 uart_console_write(&dev->port, s, n, sprd_putc); 618 } 619 620 static int __init sprd_early_console_setup( 621 struct earlycon_device *device, 622 const char *opt) 623 { 624 if (!device->port.membase) 625 return -ENODEV; 626 627 device->con->write = sprd_early_write; 628 return 0; 629 } 630 OF_EARLYCON_DECLARE(sprd_serial, "sprd,sc9836-uart", 631 sprd_early_console_setup); 632 633 #else /* !CONFIG_SERIAL_SPRD_CONSOLE */ 634 #define SPRD_CONSOLE NULL 635 #endif 636 637 static struct uart_driver sprd_uart_driver = { 638 .owner = THIS_MODULE, 639 .driver_name = "sprd_serial", 640 .dev_name = SPRD_TTY_NAME, 641 .major = 0, 642 .minor = 0, 643 .nr = UART_NR_MAX, 644 .cons = SPRD_CONSOLE, 645 }; 646 647 static int sprd_probe_dt_alias(int index, struct device *dev) 648 { 649 struct device_node *np; 650 int ret = index; 651 652 if (!IS_ENABLED(CONFIG_OF)) 653 return ret; 654 655 np = dev->of_node; 656 if (!np) 657 return ret; 658 659 ret = of_alias_get_id(np, "serial"); 660 if (ret < 0) 661 ret = index; 662 else if (ret >= ARRAY_SIZE(sprd_port) || sprd_port[ret] != NULL) { 663 dev_warn(dev, "requested serial port %d not available.\n", ret); 664 ret = index; 665 } 666 667 return ret; 668 } 669 670 static int sprd_remove(struct platform_device *dev) 671 { 672 struct sprd_uart_port *sup = platform_get_drvdata(dev); 673 674 if (sup) { 675 uart_remove_one_port(&sprd_uart_driver, &sup->port); 676 sprd_port[sup->port.line] = NULL; 677 sprd_ports_num--; 678 } 679 680 if (!sprd_ports_num) 681 uart_unregister_driver(&sprd_uart_driver); 682 683 return 0; 684 } 685 686 static int sprd_probe(struct platform_device *pdev) 687 { 688 struct resource *res; 689 struct uart_port *up; 690 struct clk *clk; 691 int irq; 692 int index; 693 int ret; 694 695 for (index = 0; index < ARRAY_SIZE(sprd_port); index++) 696 if (sprd_port[index] == NULL) 697 break; 698 699 if (index == ARRAY_SIZE(sprd_port)) 700 return -EBUSY; 701 702 index = sprd_probe_dt_alias(index, &pdev->dev); 703 704 sprd_port[index] = devm_kzalloc(&pdev->dev, 705 sizeof(*sprd_port[index]), GFP_KERNEL); 706 if (!sprd_port[index]) 707 return -ENOMEM; 708 709 up = &sprd_port[index]->port; 710 up->dev = &pdev->dev; 711 up->line = index; 712 up->type = PORT_SPRD; 713 up->iotype = UPIO_MEM; 714 up->uartclk = SPRD_DEF_RATE; 715 up->fifosize = SPRD_FIFO_SIZE; 716 up->ops = &serial_sprd_ops; 717 up->flags = UPF_BOOT_AUTOCONF; 718 719 clk = devm_clk_get(&pdev->dev, NULL); 720 if (!IS_ERR_OR_NULL(clk)) 721 up->uartclk = clk_get_rate(clk); 722 723 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 724 if (!res) { 725 dev_err(&pdev->dev, "not provide mem resource\n"); 726 return -ENODEV; 727 } 728 up->mapbase = res->start; 729 up->membase = devm_ioremap_resource(&pdev->dev, res); 730 if (IS_ERR(up->membase)) 731 return PTR_ERR(up->membase); 732 733 irq = platform_get_irq(pdev, 0); 734 if (irq < 0) { 735 dev_err(&pdev->dev, "not provide irq resource: %d\n", irq); 736 return irq; 737 } 738 up->irq = irq; 739 740 if (!sprd_ports_num) { 741 ret = uart_register_driver(&sprd_uart_driver); 742 if (ret < 0) { 743 pr_err("Failed to register SPRD-UART driver\n"); 744 return ret; 745 } 746 } 747 sprd_ports_num++; 748 749 ret = uart_add_one_port(&sprd_uart_driver, up); 750 if (ret) { 751 sprd_port[index] = NULL; 752 sprd_remove(pdev); 753 } 754 755 platform_set_drvdata(pdev, up); 756 757 return ret; 758 } 759 760 #ifdef CONFIG_PM_SLEEP 761 static int sprd_suspend(struct device *dev) 762 { 763 struct sprd_uart_port *sup = dev_get_drvdata(dev); 764 765 uart_suspend_port(&sprd_uart_driver, &sup->port); 766 767 return 0; 768 } 769 770 static int sprd_resume(struct device *dev) 771 { 772 struct sprd_uart_port *sup = dev_get_drvdata(dev); 773 774 uart_resume_port(&sprd_uart_driver, &sup->port); 775 776 return 0; 777 } 778 #endif 779 780 static SIMPLE_DEV_PM_OPS(sprd_pm_ops, sprd_suspend, sprd_resume); 781 782 static const struct of_device_id serial_ids[] = { 783 {.compatible = "sprd,sc9836-uart",}, 784 {} 785 }; 786 MODULE_DEVICE_TABLE(of, serial_ids); 787 788 static struct platform_driver sprd_platform_driver = { 789 .probe = sprd_probe, 790 .remove = sprd_remove, 791 .driver = { 792 .name = "sprd_serial", 793 .of_match_table = of_match_ptr(serial_ids), 794 .pm = &sprd_pm_ops, 795 }, 796 }; 797 798 module_platform_driver(sprd_platform_driver); 799 800 MODULE_LICENSE("GPL v2"); 801 MODULE_DESCRIPTION("Spreadtrum SoC serial driver series"); 802