Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * st-asc.c: ST Asynchronous serial controller (ASC) driver
0004  *
0005  * Copyright (C) 2003-2013 STMicroelectronics (R&D) Limited
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/serial.h>
0010 #include <linux/console.h>
0011 #include <linux/sysrq.h>
0012 #include <linux/pinctrl/consumer.h>
0013 #include <linux/platform_device.h>
0014 #include <linux/io.h>
0015 #include <linux/irq.h>
0016 #include <linux/tty.h>
0017 #include <linux/tty_flip.h>
0018 #include <linux/delay.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/of.h>
0021 #include <linux/of_platform.h>
0022 #include <linux/serial_core.h>
0023 #include <linux/clk.h>
0024 #include <linux/gpio/consumer.h>
0025 
0026 #define DRIVER_NAME "st-asc"
0027 #define ASC_SERIAL_NAME "ttyAS"
0028 #define ASC_FIFO_SIZE 16
0029 #define ASC_MAX_PORTS 8
0030 
0031 /* Pinctrl states */
0032 #define DEFAULT     0
0033 #define NO_HW_FLOWCTRL  1
0034 
0035 struct asc_port {
0036     struct uart_port port;
0037     struct gpio_desc *rts;
0038     struct clk *clk;
0039     struct pinctrl *pinctrl;
0040     struct pinctrl_state *states[2];
0041     unsigned int hw_flow_control:1;
0042     unsigned int force_m1:1;
0043 };
0044 
0045 static struct asc_port asc_ports[ASC_MAX_PORTS];
0046 static struct uart_driver asc_uart_driver;
0047 
0048 /*---- UART Register definitions ------------------------------*/
0049 
0050 /* Register offsets */
0051 
0052 #define ASC_BAUDRATE            0x00
0053 #define ASC_TXBUF           0x04
0054 #define ASC_RXBUF           0x08
0055 #define ASC_CTL             0x0C
0056 #define ASC_INTEN           0x10
0057 #define ASC_STA             0x14
0058 #define ASC_GUARDTIME           0x18
0059 #define ASC_TIMEOUT         0x1C
0060 #define ASC_TXRESET         0x20
0061 #define ASC_RXRESET         0x24
0062 #define ASC_RETRIES         0x28
0063 
0064 /* ASC_RXBUF */
0065 #define ASC_RXBUF_PE            0x100
0066 #define ASC_RXBUF_FE            0x200
0067 /*
0068  * Some of status comes from higher bits of the character and some come from
0069  * the status register. Combining both of them in to single status using dummy
0070  * bits.
0071  */
0072 #define ASC_RXBUF_DUMMY_RX      0x10000
0073 #define ASC_RXBUF_DUMMY_BE      0x20000
0074 #define ASC_RXBUF_DUMMY_OE      0x40000
0075 
0076 /* ASC_CTL */
0077 
0078 #define ASC_CTL_MODE_MSK        0x0007
0079 #define  ASC_CTL_MODE_8BIT      0x0001
0080 #define  ASC_CTL_MODE_7BIT_PAR      0x0003
0081 #define  ASC_CTL_MODE_9BIT      0x0004
0082 #define  ASC_CTL_MODE_8BIT_WKUP     0x0005
0083 #define  ASC_CTL_MODE_8BIT_PAR      0x0007
0084 #define ASC_CTL_STOP_MSK        0x0018
0085 #define  ASC_CTL_STOP_HALFBIT       0x0000
0086 #define  ASC_CTL_STOP_1BIT      0x0008
0087 #define  ASC_CTL_STOP_1_HALFBIT     0x0010
0088 #define  ASC_CTL_STOP_2BIT      0x0018
0089 #define ASC_CTL_PARITYODD       0x0020
0090 #define ASC_CTL_LOOPBACK        0x0040
0091 #define ASC_CTL_RUN         0x0080
0092 #define ASC_CTL_RXENABLE        0x0100
0093 #define ASC_CTL_SCENABLE        0x0200
0094 #define ASC_CTL_FIFOENABLE      0x0400
0095 #define ASC_CTL_CTSENABLE       0x0800
0096 #define ASC_CTL_BAUDMODE        0x1000
0097 
0098 /* ASC_GUARDTIME */
0099 
0100 #define ASC_GUARDTIME_MSK       0x00FF
0101 
0102 /* ASC_INTEN */
0103 
0104 #define ASC_INTEN_RBE           0x0001
0105 #define ASC_INTEN_TE            0x0002
0106 #define ASC_INTEN_THE           0x0004
0107 #define ASC_INTEN_PE            0x0008
0108 #define ASC_INTEN_FE            0x0010
0109 #define ASC_INTEN_OE            0x0020
0110 #define ASC_INTEN_TNE           0x0040
0111 #define ASC_INTEN_TOI           0x0080
0112 #define ASC_INTEN_RHF           0x0100
0113 
0114 /* ASC_RETRIES */
0115 
0116 #define ASC_RETRIES_MSK         0x00FF
0117 
0118 /* ASC_RXBUF */
0119 
0120 #define ASC_RXBUF_MSK           0x03FF
0121 
0122 /* ASC_STA */
0123 
0124 #define ASC_STA_RBF         0x0001
0125 #define ASC_STA_TE          0x0002
0126 #define ASC_STA_THE         0x0004
0127 #define ASC_STA_PE          0x0008
0128 #define ASC_STA_FE          0x0010
0129 #define ASC_STA_OE          0x0020
0130 #define ASC_STA_TNE         0x0040
0131 #define ASC_STA_TOI         0x0080
0132 #define ASC_STA_RHF         0x0100
0133 #define ASC_STA_TF          0x0200
0134 #define ASC_STA_NKD         0x0400
0135 
0136 /* ASC_TIMEOUT */
0137 
0138 #define ASC_TIMEOUT_MSK         0x00FF
0139 
0140 /* ASC_TXBUF */
0141 
0142 #define ASC_TXBUF_MSK           0x01FF
0143 
0144 /*---- Inline function definitions ---------------------------*/
0145 
0146 static inline struct asc_port *to_asc_port(struct uart_port *port)
0147 {
0148     return container_of(port, struct asc_port, port);
0149 }
0150 
0151 static inline u32 asc_in(struct uart_port *port, u32 offset)
0152 {
0153 #ifdef readl_relaxed
0154     return readl_relaxed(port->membase + offset);
0155 #else
0156     return readl(port->membase + offset);
0157 #endif
0158 }
0159 
0160 static inline void asc_out(struct uart_port *port, u32 offset, u32 value)
0161 {
0162 #ifdef writel_relaxed
0163     writel_relaxed(value, port->membase + offset);
0164 #else
0165     writel(value, port->membase + offset);
0166 #endif
0167 }
0168 
0169 /*
0170  * Some simple utility functions to enable and disable interrupts.
0171  * Note that these need to be called with interrupts disabled.
0172  */
0173 static inline void asc_disable_tx_interrupts(struct uart_port *port)
0174 {
0175     u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_THE;
0176     asc_out(port, ASC_INTEN, intenable);
0177     (void)asc_in(port, ASC_INTEN);  /* Defeat bus write posting */
0178 }
0179 
0180 static inline void asc_enable_tx_interrupts(struct uart_port *port)
0181 {
0182     u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_THE;
0183     asc_out(port, ASC_INTEN, intenable);
0184 }
0185 
0186 static inline void asc_disable_rx_interrupts(struct uart_port *port)
0187 {
0188     u32 intenable = asc_in(port, ASC_INTEN) & ~ASC_INTEN_RBE;
0189     asc_out(port, ASC_INTEN, intenable);
0190     (void)asc_in(port, ASC_INTEN);  /* Defeat bus write posting */
0191 }
0192 
0193 static inline void asc_enable_rx_interrupts(struct uart_port *port)
0194 {
0195     u32 intenable = asc_in(port, ASC_INTEN) | ASC_INTEN_RBE;
0196     asc_out(port, ASC_INTEN, intenable);
0197 }
0198 
0199 static inline u32 asc_txfifo_is_empty(struct uart_port *port)
0200 {
0201     return asc_in(port, ASC_STA) & ASC_STA_TE;
0202 }
0203 
0204 static inline u32 asc_txfifo_is_half_empty(struct uart_port *port)
0205 {
0206     return asc_in(port, ASC_STA) & ASC_STA_THE;
0207 }
0208 
0209 static inline const char *asc_port_name(struct uart_port *port)
0210 {
0211     return to_platform_device(port->dev)->name;
0212 }
0213 
0214 /*----------------------------------------------------------------------*/
0215 
0216 /*
0217  * This section contains code to support the use of the ASC as a
0218  * generic serial port.
0219  */
0220 
0221 static inline unsigned asc_hw_txroom(struct uart_port *port)
0222 {
0223     u32 status = asc_in(port, ASC_STA);
0224 
0225     if (status & ASC_STA_THE)
0226         return port->fifosize / 2;
0227     else if (!(status & ASC_STA_TF))
0228         return 1;
0229 
0230     return 0;
0231 }
0232 
0233 /*
0234  * Start transmitting chars.
0235  * This is called from both interrupt and task level.
0236  * Either way interrupts are disabled.
0237  */
0238 static void asc_transmit_chars(struct uart_port *port)
0239 {
0240     struct circ_buf *xmit = &port->state->xmit;
0241     int txroom;
0242     unsigned char c;
0243 
0244     txroom = asc_hw_txroom(port);
0245 
0246     if ((txroom != 0) && port->x_char) {
0247         c = port->x_char;
0248         port->x_char = 0;
0249         asc_out(port, ASC_TXBUF, c);
0250         port->icount.tx++;
0251         txroom = asc_hw_txroom(port);
0252     }
0253 
0254     if (uart_tx_stopped(port)) {
0255         /*
0256          * We should try and stop the hardware here, but I
0257          * don't think the ASC has any way to do that.
0258          */
0259         asc_disable_tx_interrupts(port);
0260         return;
0261     }
0262 
0263     if (uart_circ_empty(xmit)) {
0264         asc_disable_tx_interrupts(port);
0265         return;
0266     }
0267 
0268     if (txroom == 0)
0269         return;
0270 
0271     do {
0272         c = xmit->buf[xmit->tail];
0273         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0274         asc_out(port, ASC_TXBUF, c);
0275         port->icount.tx++;
0276         txroom--;
0277     } while ((txroom > 0) && (!uart_circ_empty(xmit)));
0278 
0279     if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0280         uart_write_wakeup(port);
0281 
0282     if (uart_circ_empty(xmit))
0283         asc_disable_tx_interrupts(port);
0284 }
0285 
0286 static void asc_receive_chars(struct uart_port *port)
0287 {
0288     struct tty_port *tport = &port->state->port;
0289     unsigned long status, mode;
0290     unsigned long c = 0;
0291     char flag;
0292     bool ignore_pe = false;
0293 
0294     /*
0295      * Datasheet states: If the MODE field selects an 8-bit frame then
0296      * this [parity error] bit is undefined. Software should ignore this
0297      * bit when reading 8-bit frames.
0298      */
0299     mode = asc_in(port, ASC_CTL) & ASC_CTL_MODE_MSK;
0300     if (mode == ASC_CTL_MODE_8BIT || mode == ASC_CTL_MODE_8BIT_PAR)
0301         ignore_pe = true;
0302 
0303     if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
0304         pm_wakeup_event(tport->tty->dev, 0);
0305 
0306     while ((status = asc_in(port, ASC_STA)) & ASC_STA_RBF) {
0307         c = asc_in(port, ASC_RXBUF) | ASC_RXBUF_DUMMY_RX;
0308         flag = TTY_NORMAL;
0309         port->icount.rx++;
0310 
0311         if (status & ASC_STA_OE || c & ASC_RXBUF_FE ||
0312             (c & ASC_RXBUF_PE && !ignore_pe)) {
0313 
0314             if (c & ASC_RXBUF_FE) {
0315                 if (c == (ASC_RXBUF_FE | ASC_RXBUF_DUMMY_RX)) {
0316                     port->icount.brk++;
0317                     if (uart_handle_break(port))
0318                         continue;
0319                     c |= ASC_RXBUF_DUMMY_BE;
0320                 } else {
0321                     port->icount.frame++;
0322                 }
0323             } else if (c & ASC_RXBUF_PE) {
0324                 port->icount.parity++;
0325             }
0326             /*
0327              * Reading any data from the RX FIFO clears the
0328              * overflow error condition.
0329              */
0330             if (status & ASC_STA_OE) {
0331                 port->icount.overrun++;
0332                 c |= ASC_RXBUF_DUMMY_OE;
0333             }
0334 
0335             c &= port->read_status_mask;
0336 
0337             if (c & ASC_RXBUF_DUMMY_BE)
0338                 flag = TTY_BREAK;
0339             else if (c & ASC_RXBUF_PE)
0340                 flag = TTY_PARITY;
0341             else if (c & ASC_RXBUF_FE)
0342                 flag = TTY_FRAME;
0343         }
0344 
0345         if (uart_handle_sysrq_char(port, c & 0xff))
0346             continue;
0347 
0348         uart_insert_char(port, c, ASC_RXBUF_DUMMY_OE, c & 0xff, flag);
0349     }
0350 
0351     /* Tell the rest of the system the news. New characters! */
0352     tty_flip_buffer_push(tport);
0353 }
0354 
0355 static irqreturn_t asc_interrupt(int irq, void *ptr)
0356 {
0357     struct uart_port *port = ptr;
0358     u32 status;
0359 
0360     spin_lock(&port->lock);
0361 
0362     status = asc_in(port, ASC_STA);
0363 
0364     if (status & ASC_STA_RBF) {
0365         /* Receive FIFO not empty */
0366         asc_receive_chars(port);
0367     }
0368 
0369     if ((status & ASC_STA_THE) &&
0370         (asc_in(port, ASC_INTEN) & ASC_INTEN_THE)) {
0371         /* Transmitter FIFO at least half empty */
0372         asc_transmit_chars(port);
0373     }
0374 
0375     spin_unlock(&port->lock);
0376 
0377     return IRQ_HANDLED;
0378 }
0379 
0380 /*----------------------------------------------------------------------*/
0381 
0382 /*
0383  * UART Functions
0384  */
0385 
0386 static unsigned int asc_tx_empty(struct uart_port *port)
0387 {
0388     return asc_txfifo_is_empty(port) ? TIOCSER_TEMT : 0;
0389 }
0390 
0391 static void asc_set_mctrl(struct uart_port *port, unsigned int mctrl)
0392 {
0393     struct asc_port *ascport = to_asc_port(port);
0394 
0395     /*
0396      * This routine is used for seting signals of: DTR, DCD, CTS and RTS.
0397      * We use ASC's hardware for CTS/RTS when hardware flow-control is
0398      * enabled, however if the RTS line is required for another purpose,
0399      * commonly controlled using HUP from userspace, then we need to toggle
0400      * it manually, using GPIO.
0401      *
0402      * Some boards also have DTR and DCD implemented using PIO pins, code to
0403      * do this should be hooked in here.
0404      */
0405 
0406     if (!ascport->rts)
0407         return;
0408 
0409     /* If HW flow-control is enabled, we can't fiddle with the RTS line */
0410     if (asc_in(port, ASC_CTL) & ASC_CTL_CTSENABLE)
0411         return;
0412 
0413     gpiod_set_value(ascport->rts, mctrl & TIOCM_RTS);
0414 }
0415 
0416 static unsigned int asc_get_mctrl(struct uart_port *port)
0417 {
0418     /*
0419      * This routine is used for geting signals of: DTR, DCD, DSR, RI,
0420      * and CTS/RTS
0421      */
0422     return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
0423 }
0424 
0425 /* There are probably characters waiting to be transmitted. */
0426 static void asc_start_tx(struct uart_port *port)
0427 {
0428     struct circ_buf *xmit = &port->state->xmit;
0429 
0430     if (!uart_circ_empty(xmit))
0431         asc_enable_tx_interrupts(port);
0432 }
0433 
0434 /* Transmit stop */
0435 static void asc_stop_tx(struct uart_port *port)
0436 {
0437     asc_disable_tx_interrupts(port);
0438 }
0439 
0440 /* Receive stop */
0441 static void asc_stop_rx(struct uart_port *port)
0442 {
0443     asc_disable_rx_interrupts(port);
0444 }
0445 
0446 /* Handle breaks - ignored by us */
0447 static void asc_break_ctl(struct uart_port *port, int break_state)
0448 {
0449     /* Nothing here yet .. */
0450 }
0451 
0452 /*
0453  * Enable port for reception.
0454  */
0455 static int asc_startup(struct uart_port *port)
0456 {
0457     if (request_irq(port->irq, asc_interrupt, 0,
0458             asc_port_name(port), port)) {
0459         dev_err(port->dev, "cannot allocate irq.\n");
0460         return -ENODEV;
0461     }
0462 
0463     asc_transmit_chars(port);
0464     asc_enable_rx_interrupts(port);
0465 
0466     return 0;
0467 }
0468 
0469 static void asc_shutdown(struct uart_port *port)
0470 {
0471     asc_disable_tx_interrupts(port);
0472     asc_disable_rx_interrupts(port);
0473     free_irq(port->irq, port);
0474 }
0475 
0476 static void asc_pm(struct uart_port *port, unsigned int state,
0477         unsigned int oldstate)
0478 {
0479     struct asc_port *ascport = to_asc_port(port);
0480     unsigned long flags;
0481     u32 ctl;
0482 
0483     switch (state) {
0484     case UART_PM_STATE_ON:
0485         clk_prepare_enable(ascport->clk);
0486         break;
0487     case UART_PM_STATE_OFF:
0488         /*
0489          * Disable the ASC baud rate generator, which is as close as
0490          * we can come to turning it off. Note this is not called with
0491          * the port spinlock held.
0492          */
0493         spin_lock_irqsave(&port->lock, flags);
0494         ctl = asc_in(port, ASC_CTL) & ~ASC_CTL_RUN;
0495         asc_out(port, ASC_CTL, ctl);
0496         spin_unlock_irqrestore(&port->lock, flags);
0497         clk_disable_unprepare(ascport->clk);
0498         break;
0499     }
0500 }
0501 
0502 static void asc_set_termios(struct uart_port *port, struct ktermios *termios,
0503                 struct ktermios *old)
0504 {
0505     struct asc_port *ascport = to_asc_port(port);
0506     struct gpio_desc *gpiod;
0507     unsigned int baud;
0508     u32 ctrl_val;
0509     tcflag_t cflag;
0510     unsigned long flags;
0511 
0512     /* Update termios to reflect hardware capabilities */
0513     termios->c_cflag &= ~(CMSPAR |
0514              (ascport->hw_flow_control ? 0 : CRTSCTS));
0515 
0516     port->uartclk = clk_get_rate(ascport->clk);
0517 
0518     baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
0519     cflag = termios->c_cflag;
0520 
0521     spin_lock_irqsave(&port->lock, flags);
0522 
0523     /* read control register */
0524     ctrl_val = asc_in(port, ASC_CTL);
0525 
0526     /* stop serial port and reset value */
0527     asc_out(port, ASC_CTL, (ctrl_val & ~ASC_CTL_RUN));
0528     ctrl_val = ASC_CTL_RXENABLE | ASC_CTL_FIFOENABLE;
0529 
0530     /* reset fifo rx & tx */
0531     asc_out(port, ASC_TXRESET, 1);
0532     asc_out(port, ASC_RXRESET, 1);
0533 
0534     /* set character length */
0535     if ((cflag & CSIZE) == CS7) {
0536         ctrl_val |= ASC_CTL_MODE_7BIT_PAR;
0537         cflag |= PARENB;
0538     } else {
0539         ctrl_val |= (cflag & PARENB) ?  ASC_CTL_MODE_8BIT_PAR :
0540                         ASC_CTL_MODE_8BIT;
0541         cflag &= ~CSIZE;
0542         cflag |= CS8;
0543     }
0544     termios->c_cflag = cflag;
0545 
0546     /* set stop bit */
0547     ctrl_val |= (cflag & CSTOPB) ? ASC_CTL_STOP_2BIT : ASC_CTL_STOP_1BIT;
0548 
0549     /* odd parity */
0550     if (cflag & PARODD)
0551         ctrl_val |= ASC_CTL_PARITYODD;
0552 
0553     /* hardware flow control */
0554     if ((cflag & CRTSCTS)) {
0555         ctrl_val |= ASC_CTL_CTSENABLE;
0556 
0557         /* If flow-control selected, stop handling RTS manually */
0558         if (ascport->rts) {
0559             devm_gpiod_put(port->dev, ascport->rts);
0560             ascport->rts = NULL;
0561 
0562             pinctrl_select_state(ascport->pinctrl,
0563                          ascport->states[DEFAULT]);
0564         }
0565     } else {
0566         /* If flow-control disabled, it's safe to handle RTS manually */
0567         if (!ascport->rts && ascport->states[NO_HW_FLOWCTRL]) {
0568             pinctrl_select_state(ascport->pinctrl,
0569                          ascport->states[NO_HW_FLOWCTRL]);
0570 
0571             gpiod = devm_gpiod_get(port->dev, "rts", GPIOD_OUT_LOW);
0572             if (!IS_ERR(gpiod)) {
0573                 gpiod_set_consumer_name(gpiod,
0574                         port->dev->of_node->name);
0575                 ascport->rts = gpiod;
0576             }
0577         }
0578     }
0579 
0580     if ((baud < 19200) && !ascport->force_m1) {
0581         asc_out(port, ASC_BAUDRATE, (port->uartclk / (16 * baud)));
0582     } else {
0583         /*
0584          * MODE 1: recommended for high bit rates (above 19.2K)
0585          *
0586          *                   baudrate * 16 * 2^16
0587          * ASCBaudRate =   ------------------------
0588          *                          inputclock
0589          *
0590          * To keep maths inside 64bits, we divide inputclock by 16.
0591          */
0592         u64 dividend = (u64)baud * (1 << 16);
0593 
0594         do_div(dividend, port->uartclk / 16);
0595         asc_out(port, ASC_BAUDRATE, dividend);
0596         ctrl_val |= ASC_CTL_BAUDMODE;
0597     }
0598 
0599     uart_update_timeout(port, cflag, baud);
0600 
0601     ascport->port.read_status_mask = ASC_RXBUF_DUMMY_OE;
0602     if (termios->c_iflag & INPCK)
0603         ascport->port.read_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
0604     if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
0605         ascport->port.read_status_mask |= ASC_RXBUF_DUMMY_BE;
0606 
0607     /*
0608      * Characters to ignore
0609      */
0610     ascport->port.ignore_status_mask = 0;
0611     if (termios->c_iflag & IGNPAR)
0612         ascport->port.ignore_status_mask |= ASC_RXBUF_FE | ASC_RXBUF_PE;
0613     if (termios->c_iflag & IGNBRK) {
0614         ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_BE;
0615         /*
0616          * If we're ignoring parity and break indicators,
0617          * ignore overruns too (for real raw support).
0618          */
0619         if (termios->c_iflag & IGNPAR)
0620             ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_OE;
0621     }
0622 
0623     /*
0624      * Ignore all characters if CREAD is not set.
0625      */
0626     if (!(termios->c_cflag & CREAD))
0627         ascport->port.ignore_status_mask |= ASC_RXBUF_DUMMY_RX;
0628 
0629     /* Set the timeout */
0630     asc_out(port, ASC_TIMEOUT, 20);
0631 
0632     /* write final value and enable port */
0633     asc_out(port, ASC_CTL, (ctrl_val | ASC_CTL_RUN));
0634 
0635     spin_unlock_irqrestore(&port->lock, flags);
0636 }
0637 
0638 static const char *asc_type(struct uart_port *port)
0639 {
0640     return (port->type == PORT_ASC) ? DRIVER_NAME : NULL;
0641 }
0642 
0643 static void asc_release_port(struct uart_port *port)
0644 {
0645 }
0646 
0647 static int asc_request_port(struct uart_port *port)
0648 {
0649     return 0;
0650 }
0651 
0652 /*
0653  * Called when the port is opened, and UPF_BOOT_AUTOCONF flag is set
0654  * Set type field if successful
0655  */
0656 static void asc_config_port(struct uart_port *port, int flags)
0657 {
0658     if ((flags & UART_CONFIG_TYPE))
0659         port->type = PORT_ASC;
0660 }
0661 
0662 static int
0663 asc_verify_port(struct uart_port *port, struct serial_struct *ser)
0664 {
0665     /* No user changeable parameters */
0666     return -EINVAL;
0667 }
0668 
0669 #ifdef CONFIG_CONSOLE_POLL
0670 /*
0671  * Console polling routines for writing and reading from the uart while
0672  * in an interrupt or debug context (i.e. kgdb).
0673  */
0674 
0675 static int asc_get_poll_char(struct uart_port *port)
0676 {
0677     if (!(asc_in(port, ASC_STA) & ASC_STA_RBF))
0678         return NO_POLL_CHAR;
0679 
0680     return asc_in(port, ASC_RXBUF);
0681 }
0682 
0683 static void asc_put_poll_char(struct uart_port *port, unsigned char c)
0684 {
0685     while (!asc_txfifo_is_half_empty(port))
0686         cpu_relax();
0687     asc_out(port, ASC_TXBUF, c);
0688 }
0689 
0690 #endif /* CONFIG_CONSOLE_POLL */
0691 
0692 /*---------------------------------------------------------------------*/
0693 
0694 static const struct uart_ops asc_uart_ops = {
0695     .tx_empty   = asc_tx_empty,
0696     .set_mctrl  = asc_set_mctrl,
0697     .get_mctrl  = asc_get_mctrl,
0698     .start_tx   = asc_start_tx,
0699     .stop_tx    = asc_stop_tx,
0700     .stop_rx    = asc_stop_rx,
0701     .break_ctl  = asc_break_ctl,
0702     .startup    = asc_startup,
0703     .shutdown   = asc_shutdown,
0704     .set_termios    = asc_set_termios,
0705     .type       = asc_type,
0706     .release_port   = asc_release_port,
0707     .request_port   = asc_request_port,
0708     .config_port    = asc_config_port,
0709     .verify_port    = asc_verify_port,
0710     .pm     = asc_pm,
0711 #ifdef CONFIG_CONSOLE_POLL
0712     .poll_get_char = asc_get_poll_char,
0713     .poll_put_char = asc_put_poll_char,
0714 #endif /* CONFIG_CONSOLE_POLL */
0715 };
0716 
0717 static int asc_init_port(struct asc_port *ascport,
0718               struct platform_device *pdev)
0719 {
0720     struct uart_port *port = &ascport->port;
0721     struct resource *res;
0722     int ret;
0723 
0724     port->iotype    = UPIO_MEM;
0725     port->flags = UPF_BOOT_AUTOCONF;
0726     port->ops   = &asc_uart_ops;
0727     port->fifosize  = ASC_FIFO_SIZE;
0728     port->dev   = &pdev->dev;
0729     port->irq   = platform_get_irq(pdev, 0);
0730     port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_ST_ASC_CONSOLE);
0731 
0732     res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0733     port->membase = devm_ioremap_resource(&pdev->dev, res);
0734     if (IS_ERR(port->membase))
0735         return PTR_ERR(port->membase);
0736     port->mapbase = res->start;
0737 
0738     spin_lock_init(&port->lock);
0739 
0740     ascport->clk = devm_clk_get(&pdev->dev, NULL);
0741 
0742     if (WARN_ON(IS_ERR(ascport->clk)))
0743         return -EINVAL;
0744     /* ensure that clk rate is correct by enabling the clk */
0745     clk_prepare_enable(ascport->clk);
0746     ascport->port.uartclk = clk_get_rate(ascport->clk);
0747     WARN_ON(ascport->port.uartclk == 0);
0748     clk_disable_unprepare(ascport->clk);
0749 
0750     ascport->pinctrl = devm_pinctrl_get(&pdev->dev);
0751     if (IS_ERR(ascport->pinctrl)) {
0752         ret = PTR_ERR(ascport->pinctrl);
0753         dev_err(&pdev->dev, "Failed to get Pinctrl: %d\n", ret);
0754         return ret;
0755     }
0756 
0757     ascport->states[DEFAULT] =
0758         pinctrl_lookup_state(ascport->pinctrl, "default");
0759     if (IS_ERR(ascport->states[DEFAULT])) {
0760         ret = PTR_ERR(ascport->states[DEFAULT]);
0761         dev_err(&pdev->dev,
0762             "Failed to look up Pinctrl state 'default': %d\n", ret);
0763         return ret;
0764     }
0765 
0766     /* "no-hw-flowctrl" state is optional */
0767     ascport->states[NO_HW_FLOWCTRL] =
0768         pinctrl_lookup_state(ascport->pinctrl, "no-hw-flowctrl");
0769     if (IS_ERR(ascport->states[NO_HW_FLOWCTRL]))
0770         ascport->states[NO_HW_FLOWCTRL] = NULL;
0771 
0772     return 0;
0773 }
0774 
0775 static struct asc_port *asc_of_get_asc_port(struct platform_device *pdev)
0776 {
0777     struct device_node *np = pdev->dev.of_node;
0778     int id;
0779 
0780     if (!np)
0781         return NULL;
0782 
0783     id = of_alias_get_id(np, "serial");
0784     if (id < 0)
0785         id = of_alias_get_id(np, ASC_SERIAL_NAME);
0786 
0787     if (id < 0)
0788         id = 0;
0789 
0790     if (WARN_ON(id >= ASC_MAX_PORTS))
0791         return NULL;
0792 
0793     asc_ports[id].hw_flow_control = of_property_read_bool(np,
0794                             "uart-has-rtscts");
0795     asc_ports[id].force_m1 =  of_property_read_bool(np, "st,force_m1");
0796     asc_ports[id].port.line = id;
0797     asc_ports[id].rts = NULL;
0798 
0799     return &asc_ports[id];
0800 }
0801 
0802 #ifdef CONFIG_OF
0803 static const struct of_device_id asc_match[] = {
0804     { .compatible = "st,asc", },
0805     {},
0806 };
0807 
0808 MODULE_DEVICE_TABLE(of, asc_match);
0809 #endif
0810 
0811 static int asc_serial_probe(struct platform_device *pdev)
0812 {
0813     int ret;
0814     struct asc_port *ascport;
0815 
0816     ascport = asc_of_get_asc_port(pdev);
0817     if (!ascport)
0818         return -ENODEV;
0819 
0820     ret = asc_init_port(ascport, pdev);
0821     if (ret)
0822         return ret;
0823 
0824     ret = uart_add_one_port(&asc_uart_driver, &ascport->port);
0825     if (ret)
0826         return ret;
0827 
0828     platform_set_drvdata(pdev, &ascport->port);
0829 
0830     return 0;
0831 }
0832 
0833 static int asc_serial_remove(struct platform_device *pdev)
0834 {
0835     struct uart_port *port = platform_get_drvdata(pdev);
0836 
0837     return uart_remove_one_port(&asc_uart_driver, port);
0838 }
0839 
0840 #ifdef CONFIG_PM_SLEEP
0841 static int asc_serial_suspend(struct device *dev)
0842 {
0843     struct uart_port *port = dev_get_drvdata(dev);
0844 
0845     return uart_suspend_port(&asc_uart_driver, port);
0846 }
0847 
0848 static int asc_serial_resume(struct device *dev)
0849 {
0850     struct uart_port *port = dev_get_drvdata(dev);
0851 
0852     return uart_resume_port(&asc_uart_driver, port);
0853 }
0854 
0855 #endif /* CONFIG_PM_SLEEP */
0856 
0857 /*----------------------------------------------------------------------*/
0858 
0859 #ifdef CONFIG_SERIAL_ST_ASC_CONSOLE
0860 static void asc_console_putchar(struct uart_port *port, unsigned char ch)
0861 {
0862     unsigned int timeout = 1000000;
0863 
0864     /* Wait for upto 1 second in case flow control is stopping us. */
0865     while (--timeout && !asc_txfifo_is_half_empty(port))
0866         udelay(1);
0867 
0868     asc_out(port, ASC_TXBUF, ch);
0869 }
0870 
0871 /*
0872  *  Print a string to the serial port trying not to disturb
0873  *  any possible real use of the port...
0874  */
0875 
0876 static void asc_console_write(struct console *co, const char *s, unsigned count)
0877 {
0878     struct uart_port *port = &asc_ports[co->index].port;
0879     unsigned long flags;
0880     unsigned long timeout = 1000000;
0881     int locked = 1;
0882     u32 intenable;
0883 
0884     if (port->sysrq)
0885         locked = 0; /* asc_interrupt has already claimed the lock */
0886     else if (oops_in_progress)
0887         locked = spin_trylock_irqsave(&port->lock, flags);
0888     else
0889         spin_lock_irqsave(&port->lock, flags);
0890 
0891     /*
0892      * Disable interrupts so we don't get the IRQ line bouncing
0893      * up and down while interrupts are disabled.
0894      */
0895     intenable = asc_in(port, ASC_INTEN);
0896     asc_out(port, ASC_INTEN, 0);
0897     (void)asc_in(port, ASC_INTEN);  /* Defeat bus write posting */
0898 
0899     uart_console_write(port, s, count, asc_console_putchar);
0900 
0901     while (--timeout && !asc_txfifo_is_empty(port))
0902         udelay(1);
0903 
0904     asc_out(port, ASC_INTEN, intenable);
0905 
0906     if (locked)
0907         spin_unlock_irqrestore(&port->lock, flags);
0908 }
0909 
0910 static int asc_console_setup(struct console *co, char *options)
0911 {
0912     struct asc_port *ascport;
0913     int baud = 115200;
0914     int bits = 8;
0915     int parity = 'n';
0916     int flow = 'n';
0917 
0918     if (co->index >= ASC_MAX_PORTS)
0919         return -ENODEV;
0920 
0921     ascport = &asc_ports[co->index];
0922 
0923     /*
0924      * This driver does not support early console initialization
0925      * (use ARM early printk support instead), so we only expect
0926      * this to be called during the uart port registration when the
0927      * driver gets probed and the port should be mapped at that point.
0928      */
0929     if (ascport->port.mapbase == 0 || ascport->port.membase == NULL)
0930         return -ENXIO;
0931 
0932     if (options)
0933         uart_parse_options(options, &baud, &parity, &bits, &flow);
0934 
0935     return uart_set_options(&ascport->port, co, baud, parity, bits, flow);
0936 }
0937 
0938 static struct console asc_console = {
0939     .name       = ASC_SERIAL_NAME,
0940     .device     = uart_console_device,
0941     .write      = asc_console_write,
0942     .setup      = asc_console_setup,
0943     .flags      = CON_PRINTBUFFER,
0944     .index      = -1,
0945     .data       = &asc_uart_driver,
0946 };
0947 
0948 #define ASC_SERIAL_CONSOLE (&asc_console)
0949 
0950 #else
0951 #define ASC_SERIAL_CONSOLE NULL
0952 #endif /* CONFIG_SERIAL_ST_ASC_CONSOLE */
0953 
0954 static struct uart_driver asc_uart_driver = {
0955     .owner      = THIS_MODULE,
0956     .driver_name    = DRIVER_NAME,
0957     .dev_name   = ASC_SERIAL_NAME,
0958     .major      = 0,
0959     .minor      = 0,
0960     .nr     = ASC_MAX_PORTS,
0961     .cons       = ASC_SERIAL_CONSOLE,
0962 };
0963 
0964 static const struct dev_pm_ops asc_serial_pm_ops = {
0965     SET_SYSTEM_SLEEP_PM_OPS(asc_serial_suspend, asc_serial_resume)
0966 };
0967 
0968 static struct platform_driver asc_serial_driver = {
0969     .probe      = asc_serial_probe,
0970     .remove     = asc_serial_remove,
0971     .driver = {
0972         .name   = DRIVER_NAME,
0973         .pm = &asc_serial_pm_ops,
0974         .of_match_table = of_match_ptr(asc_match),
0975     },
0976 };
0977 
0978 static int __init asc_init(void)
0979 {
0980     int ret;
0981     static const char banner[] __initconst =
0982         KERN_INFO "STMicroelectronics ASC driver initialized\n";
0983 
0984     printk(banner);
0985 
0986     ret = uart_register_driver(&asc_uart_driver);
0987     if (ret)
0988         return ret;
0989 
0990     ret = platform_driver_register(&asc_serial_driver);
0991     if (ret)
0992         uart_unregister_driver(&asc_uart_driver);
0993 
0994     return ret;
0995 }
0996 
0997 static void __exit asc_exit(void)
0998 {
0999     platform_driver_unregister(&asc_serial_driver);
1000     uart_unregister_driver(&asc_uart_driver);
1001 }
1002 
1003 module_init(asc_init);
1004 module_exit(asc_exit);
1005 
1006 MODULE_ALIAS("platform:" DRIVER_NAME);
1007 MODULE_AUTHOR("STMicroelectronics (R&D) Limited");
1008 MODULE_DESCRIPTION("STMicroelectronics ASC serial port driver");
1009 MODULE_LICENSE("GPL");