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