Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * SiFive UART driver
0004  * Copyright (C) 2018 Paul Walmsley <paul@pwsan.com>
0005  * Copyright (C) 2018-2019 SiFive
0006  *
0007  * Based partially on:
0008  * - drivers/tty/serial/pxa.c
0009  * - drivers/tty/serial/amba-pl011.c
0010  * - drivers/tty/serial/uartlite.c
0011  * - drivers/tty/serial/omap-serial.c
0012  * - drivers/pwm/pwm-sifive.c
0013  *
0014  * See the following sources for further documentation:
0015  * - Chapter 19 "Universal Asynchronous Receiver/Transmitter (UART)" of
0016  *   SiFive FE310-G000 v2p3
0017  * - The tree/master/src/main/scala/devices/uart directory of
0018  *   https://github.com/sifive/sifive-blocks/
0019  *
0020  * The SiFive UART design is not 8250-compatible.  The following common
0021  * features are not supported:
0022  * - Word lengths other than 8 bits
0023  * - Break handling
0024  * - Parity
0025  * - Flow control
0026  * - Modem signals (DSR, RI, etc.)
0027  * On the other hand, the design is free from the baggage of the 8250
0028  * programming model.
0029  */
0030 
0031 #include <linux/clk.h>
0032 #include <linux/console.h>
0033 #include <linux/delay.h>
0034 #include <linux/init.h>
0035 #include <linux/io.h>
0036 #include <linux/irq.h>
0037 #include <linux/module.h>
0038 #include <linux/of.h>
0039 #include <linux/of_irq.h>
0040 #include <linux/platform_device.h>
0041 #include <linux/serial_core.h>
0042 #include <linux/serial_reg.h>
0043 #include <linux/slab.h>
0044 #include <linux/tty.h>
0045 #include <linux/tty_flip.h>
0046 
0047 /*
0048  * Register offsets
0049  */
0050 
0051 /* TXDATA */
0052 #define SIFIVE_SERIAL_TXDATA_OFFS       0x0
0053 #define SIFIVE_SERIAL_TXDATA_FULL_SHIFT     31
0054 #define SIFIVE_SERIAL_TXDATA_FULL_MASK      (1 << SIFIVE_SERIAL_TXDATA_FULL_SHIFT)
0055 #define SIFIVE_SERIAL_TXDATA_DATA_SHIFT     0
0056 #define SIFIVE_SERIAL_TXDATA_DATA_MASK      (0xff << SIFIVE_SERIAL_TXDATA_DATA_SHIFT)
0057 
0058 /* RXDATA */
0059 #define SIFIVE_SERIAL_RXDATA_OFFS       0x4
0060 #define SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT    31
0061 #define SIFIVE_SERIAL_RXDATA_EMPTY_MASK     (1 << SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT)
0062 #define SIFIVE_SERIAL_RXDATA_DATA_SHIFT     0
0063 #define SIFIVE_SERIAL_RXDATA_DATA_MASK      (0xff << SIFIVE_SERIAL_RXDATA_DATA_SHIFT)
0064 
0065 /* TXCTRL */
0066 #define SIFIVE_SERIAL_TXCTRL_OFFS       0x8
0067 #define SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT    16
0068 #define SIFIVE_SERIAL_TXCTRL_TXCNT_MASK     (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
0069 #define SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT    1
0070 #define SIFIVE_SERIAL_TXCTRL_NSTOP_MASK     (1 << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT)
0071 #define SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT     0
0072 #define SIFIVE_SERIAL_TXCTRL_TXEN_MASK      (1 << SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT)
0073 
0074 /* RXCTRL */
0075 #define SIFIVE_SERIAL_RXCTRL_OFFS       0xC
0076 #define SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT    16
0077 #define SIFIVE_SERIAL_RXCTRL_RXCNT_MASK     (0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
0078 #define SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT     0
0079 #define SIFIVE_SERIAL_RXCTRL_RXEN_MASK      (1 << SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT)
0080 
0081 /* IE */
0082 #define SIFIVE_SERIAL_IE_OFFS           0x10
0083 #define SIFIVE_SERIAL_IE_RXWM_SHIFT     1
0084 #define SIFIVE_SERIAL_IE_RXWM_MASK      (1 << SIFIVE_SERIAL_IE_RXWM_SHIFT)
0085 #define SIFIVE_SERIAL_IE_TXWM_SHIFT     0
0086 #define SIFIVE_SERIAL_IE_TXWM_MASK      (1 << SIFIVE_SERIAL_IE_TXWM_SHIFT)
0087 
0088 /* IP */
0089 #define SIFIVE_SERIAL_IP_OFFS           0x14
0090 #define SIFIVE_SERIAL_IP_RXWM_SHIFT     1
0091 #define SIFIVE_SERIAL_IP_RXWM_MASK      (1 << SIFIVE_SERIAL_IP_RXWM_SHIFT)
0092 #define SIFIVE_SERIAL_IP_TXWM_SHIFT     0
0093 #define SIFIVE_SERIAL_IP_TXWM_MASK      (1 << SIFIVE_SERIAL_IP_TXWM_SHIFT)
0094 
0095 /* DIV */
0096 #define SIFIVE_SERIAL_DIV_OFFS          0x18
0097 #define SIFIVE_SERIAL_DIV_DIV_SHIFT     0
0098 #define SIFIVE_SERIAL_DIV_DIV_MASK      (0xffff << SIFIVE_SERIAL_IP_DIV_SHIFT)
0099 
0100 /*
0101  * Config macros
0102  */
0103 
0104 /*
0105  * SIFIVE_SERIAL_MAX_PORTS: maximum number of UARTs on a device that can
0106  *                          host a serial console
0107  */
0108 #define SIFIVE_SERIAL_MAX_PORTS         8
0109 
0110 /*
0111  * SIFIVE_DEFAULT_BAUD_RATE: default baud rate that the driver should
0112  *                           configure itself to use
0113  */
0114 #define SIFIVE_DEFAULT_BAUD_RATE        115200
0115 
0116 /* SIFIVE_SERIAL_NAME: our driver's name that we pass to the operating system */
0117 #define SIFIVE_SERIAL_NAME          "sifive-serial"
0118 
0119 /* SIFIVE_TTY_PREFIX: tty name prefix for SiFive serial ports */
0120 #define SIFIVE_TTY_PREFIX           "ttySIF"
0121 
0122 /* SIFIVE_TX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
0123 #define SIFIVE_TX_FIFO_DEPTH            8
0124 
0125 /* SIFIVE_RX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
0126 #define SIFIVE_RX_FIFO_DEPTH            8
0127 
0128 #if (SIFIVE_TX_FIFO_DEPTH != SIFIVE_RX_FIFO_DEPTH)
0129 #error Driver does not support configurations with different TX, RX FIFO sizes
0130 #endif
0131 
0132 /*
0133  *
0134  */
0135 
0136 /**
0137  * struct sifive_serial_port - driver-specific data extension to struct uart_port
0138  * @port: struct uart_port embedded in this struct
0139  * @dev: struct device *
0140  * @ier: shadowed copy of the interrupt enable register
0141  * @baud_rate: UART serial line rate (e.g., 115200 baud)
0142  * @clk: reference to this device's clock
0143  * @clk_notifier: clock rate change notifier for upstream clock changes
0144  *
0145  * Configuration data specific to this SiFive UART.
0146  */
0147 struct sifive_serial_port {
0148     struct uart_port    port;
0149     struct device       *dev;
0150     unsigned char       ier;
0151     unsigned long       baud_rate;
0152     struct clk      *clk;
0153     struct notifier_block   clk_notifier;
0154 };
0155 
0156 /*
0157  * Structure container-of macros
0158  */
0159 
0160 #define port_to_sifive_serial_port(p) (container_of((p), \
0161                             struct sifive_serial_port, \
0162                             port))
0163 
0164 #define notifier_to_sifive_serial_port(nb) (container_of((nb), \
0165                              struct sifive_serial_port, \
0166                              clk_notifier))
0167 
0168 /*
0169  * Forward declarations
0170  */
0171 static void sifive_serial_stop_tx(struct uart_port *port);
0172 
0173 /*
0174  * Internal functions
0175  */
0176 
0177 /**
0178  * __ssp_early_writel() - write to a SiFive serial port register (early)
0179  * @port: pointer to a struct uart_port record
0180  * @offs: register address offset from the IP block base address
0181  * @v: value to write to the register
0182  *
0183  * Given a pointer @port to a struct uart_port record, write the value
0184  * @v to the IP block register address offset @offs.  This function is
0185  * intended for early console use.
0186  *
0187  * Context: Intended to be used only by the earlyconsole code.
0188  */
0189 static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port)
0190 {
0191     writel_relaxed(v, port->membase + offs);
0192 }
0193 
0194 /**
0195  * __ssp_early_readl() - read from a SiFive serial port register (early)
0196  * @port: pointer to a struct uart_port record
0197  * @offs: register address offset from the IP block base address
0198  *
0199  * Given a pointer @port to a struct uart_port record, read the
0200  * contents of the IP block register located at offset @offs from the
0201  * IP block base and return it.  This function is intended for early
0202  * console use.
0203  *
0204  * Context: Intended to be called only by the earlyconsole code or by
0205  *          __ssp_readl() or __ssp_writel() (in this driver)
0206  *
0207  * Returns: the register value read from the UART.
0208  */
0209 static u32 __ssp_early_readl(struct uart_port *port, u16 offs)
0210 {
0211     return readl_relaxed(port->membase + offs);
0212 }
0213 
0214 /**
0215  * __ssp_writel() - write to a SiFive serial port register
0216  * @v: value to write to the register
0217  * @offs: register address offset from the IP block base address
0218  * @ssp: pointer to a struct sifive_serial_port record
0219  *
0220  * Write the value @v to the IP block register located at offset @offs from the
0221  * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
0222  *
0223  * Context: Any context.
0224  */
0225 static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp)
0226 {
0227     __ssp_early_writel(v, offs, &ssp->port);
0228 }
0229 
0230 /**
0231  * __ssp_readl() - read from a SiFive serial port register
0232  * @ssp: pointer to a struct sifive_serial_port record
0233  * @offs: register address offset from the IP block base address
0234  *
0235  * Read the contents of the IP block register located at offset @offs from the
0236  * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
0237  *
0238  * Context: Any context.
0239  *
0240  * Returns: the value of the UART register
0241  */
0242 static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs)
0243 {
0244     return __ssp_early_readl(&ssp->port, offs);
0245 }
0246 
0247 /**
0248  * sifive_serial_is_txfifo_full() - is the TXFIFO full?
0249  * @ssp: pointer to a struct sifive_serial_port
0250  *
0251  * Read the transmit FIFO "full" bit, returning a non-zero value if the
0252  * TX FIFO is full, or zero if space remains.  Intended to be used to prevent
0253  * writes to the TX FIFO when it's full.
0254  *
0255  * Returns: SIFIVE_SERIAL_TXDATA_FULL_MASK (non-zero) if the transmit FIFO
0256  * is full, or 0 if space remains.
0257  */
0258 static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp)
0259 {
0260     return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) &
0261         SIFIVE_SERIAL_TXDATA_FULL_MASK;
0262 }
0263 
0264 /**
0265  * __ssp_transmit_char() - enqueue a byte to transmit onto the TX FIFO
0266  * @ssp: pointer to a struct sifive_serial_port
0267  * @ch: character to transmit
0268  *
0269  * Enqueue a byte @ch onto the transmit FIFO, given a pointer @ssp to the
0270  * struct sifive_serial_port * to transmit on.  Caller should first check to
0271  * ensure that the TXFIFO has space; see sifive_serial_is_txfifo_full().
0272  *
0273  * Context: Any context.
0274  */
0275 static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch)
0276 {
0277     __ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp);
0278 }
0279 
0280 /**
0281  * __ssp_transmit_chars() - enqueue multiple bytes onto the TX FIFO
0282  * @ssp: pointer to a struct sifive_serial_port
0283  *
0284  * Transfer up to a TX FIFO size's worth of characters from the Linux serial
0285  * transmit buffer to the SiFive UART TX FIFO.
0286  *
0287  * Context: Any context.  Expects @ssp->port.lock to be held by caller.
0288  */
0289 static void __ssp_transmit_chars(struct sifive_serial_port *ssp)
0290 {
0291     struct circ_buf *xmit = &ssp->port.state->xmit;
0292     int count;
0293 
0294     if (ssp->port.x_char) {
0295         __ssp_transmit_char(ssp, ssp->port.x_char);
0296         ssp->port.icount.tx++;
0297         ssp->port.x_char = 0;
0298         return;
0299     }
0300     if (uart_circ_empty(xmit) || uart_tx_stopped(&ssp->port)) {
0301         sifive_serial_stop_tx(&ssp->port);
0302         return;
0303     }
0304     count = SIFIVE_TX_FIFO_DEPTH;
0305     do {
0306         __ssp_transmit_char(ssp, xmit->buf[xmit->tail]);
0307         xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
0308         ssp->port.icount.tx++;
0309         if (uart_circ_empty(xmit))
0310             break;
0311     } while (--count > 0);
0312 
0313     if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
0314         uart_write_wakeup(&ssp->port);
0315 
0316     if (uart_circ_empty(xmit))
0317         sifive_serial_stop_tx(&ssp->port);
0318 }
0319 
0320 /**
0321  * __ssp_enable_txwm() - enable transmit watermark interrupts
0322  * @ssp: pointer to a struct sifive_serial_port
0323  *
0324  * Enable interrupt generation when the transmit FIFO watermark is reached
0325  * on the SiFive UART referred to by @ssp.
0326  */
0327 static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
0328 {
0329     if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
0330         return;
0331 
0332     ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
0333     __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0334 }
0335 
0336 /**
0337  * __ssp_enable_rxwm() - enable receive watermark interrupts
0338  * @ssp: pointer to a struct sifive_serial_port
0339  *
0340  * Enable interrupt generation when the receive FIFO watermark is reached
0341  * on the SiFive UART referred to by @ssp.
0342  */
0343 static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
0344 {
0345     if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
0346         return;
0347 
0348     ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
0349     __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0350 }
0351 
0352 /**
0353  * __ssp_disable_txwm() - disable transmit watermark interrupts
0354  * @ssp: pointer to a struct sifive_serial_port
0355  *
0356  * Disable interrupt generation when the transmit FIFO watermark is reached
0357  * on the UART referred to by @ssp.
0358  */
0359 static void __ssp_disable_txwm(struct sifive_serial_port *ssp)
0360 {
0361     if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK))
0362         return;
0363 
0364     ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK;
0365     __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0366 }
0367 
0368 /**
0369  * __ssp_disable_rxwm() - disable receive watermark interrupts
0370  * @ssp: pointer to a struct sifive_serial_port
0371  *
0372  * Disable interrupt generation when the receive FIFO watermark is reached
0373  * on the UART referred to by @ssp.
0374  */
0375 static void __ssp_disable_rxwm(struct sifive_serial_port *ssp)
0376 {
0377     if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK))
0378         return;
0379 
0380     ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK;
0381     __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0382 }
0383 
0384 /**
0385  * __ssp_receive_char() - receive a byte from the UART
0386  * @ssp: pointer to a struct sifive_serial_port
0387  * @is_empty: char pointer to return whether the RX FIFO is empty
0388  *
0389  * Try to read a byte from the SiFive UART RX FIFO, referenced by
0390  * @ssp, and to return it.  Also returns the RX FIFO empty bit in
0391  * the char pointed to by @ch.  The caller must pass the byte back to the
0392  * Linux serial layer if needed.
0393  *
0394  * Returns: the byte read from the UART RX FIFO.
0395  */
0396 static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
0397 {
0398     u32 v;
0399     u8 ch;
0400 
0401     v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
0402 
0403     if (!is_empty)
0404         WARN_ON(1);
0405     else
0406         *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
0407             SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
0408 
0409     ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
0410         SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
0411 
0412     return ch;
0413 }
0414 
0415 /**
0416  * __ssp_receive_chars() - receive multiple bytes from the UART
0417  * @ssp: pointer to a struct sifive_serial_port
0418  *
0419  * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred
0420  * to by @ssp and pass them up to the Linux serial layer.
0421  *
0422  * Context: Expects ssp->port.lock to be held by caller.
0423  */
0424 static void __ssp_receive_chars(struct sifive_serial_port *ssp)
0425 {
0426     unsigned char ch;
0427     char is_empty;
0428     int c;
0429 
0430     for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
0431         ch = __ssp_receive_char(ssp, &is_empty);
0432         if (is_empty)
0433             break;
0434 
0435         ssp->port.icount.rx++;
0436         uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
0437     }
0438 
0439     tty_flip_buffer_push(&ssp->port.state->port);
0440 }
0441 
0442 /**
0443  * __ssp_update_div() - calculate the divisor setting by the line rate
0444  * @ssp: pointer to a struct sifive_serial_port
0445  *
0446  * Calculate the appropriate value of the clock divisor for the UART
0447  * and target line rate referred to by @ssp and write it into the
0448  * hardware.
0449  */
0450 static void __ssp_update_div(struct sifive_serial_port *ssp)
0451 {
0452     u16 div;
0453 
0454     div = DIV_ROUND_UP(ssp->port.uartclk, ssp->baud_rate) - 1;
0455 
0456     __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
0457 }
0458 
0459 /**
0460  * __ssp_update_baud_rate() - set the UART "baud rate"
0461  * @ssp: pointer to a struct sifive_serial_port
0462  * @rate: new target bit rate
0463  *
0464  * Calculate the UART divisor value for the target bit rate @rate for the
0465  * SiFive UART described by @ssp and program it into the UART.  There may
0466  * be some error between the target bit rate and the actual bit rate implemented
0467  * by the UART due to clock ratio granularity.
0468  */
0469 static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
0470                    unsigned int rate)
0471 {
0472     if (ssp->baud_rate == rate)
0473         return;
0474 
0475     ssp->baud_rate = rate;
0476     __ssp_update_div(ssp);
0477 }
0478 
0479 /**
0480  * __ssp_set_stop_bits() - set the number of stop bits
0481  * @ssp: pointer to a struct sifive_serial_port
0482  * @nstop: 1 or 2 (stop bits)
0483  *
0484  * Program the SiFive UART referred to by @ssp to use @nstop stop bits.
0485  */
0486 static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
0487 {
0488     u32 v;
0489 
0490     if (nstop < 1 || nstop > 2) {
0491         WARN_ON(1);
0492         return;
0493     }
0494 
0495     v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
0496     v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
0497     v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
0498     __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
0499 }
0500 
0501 /**
0502  * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO
0503  * @ssp: pointer to a struct sifive_serial_port
0504  *
0505  * Delay while the UART TX FIFO referred to by @ssp is marked as full.
0506  *
0507  * Context: Any context.
0508  */
0509 static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
0510 {
0511     while (sifive_serial_is_txfifo_full(ssp))
0512         udelay(1); /* XXX Could probably be more intelligent here */
0513 }
0514 
0515 /*
0516  * Linux serial API functions
0517  */
0518 
0519 static void sifive_serial_stop_tx(struct uart_port *port)
0520 {
0521     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0522 
0523     __ssp_disable_txwm(ssp);
0524 }
0525 
0526 static void sifive_serial_stop_rx(struct uart_port *port)
0527 {
0528     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0529 
0530     __ssp_disable_rxwm(ssp);
0531 }
0532 
0533 static void sifive_serial_start_tx(struct uart_port *port)
0534 {
0535     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0536 
0537     __ssp_enable_txwm(ssp);
0538 }
0539 
0540 static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
0541 {
0542     struct sifive_serial_port *ssp = dev_id;
0543     u32 ip;
0544 
0545     spin_lock(&ssp->port.lock);
0546 
0547     ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
0548     if (!ip) {
0549         spin_unlock(&ssp->port.lock);
0550         return IRQ_NONE;
0551     }
0552 
0553     if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
0554         __ssp_receive_chars(ssp);
0555     if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
0556         __ssp_transmit_chars(ssp);
0557 
0558     spin_unlock(&ssp->port.lock);
0559 
0560     return IRQ_HANDLED;
0561 }
0562 
0563 static unsigned int sifive_serial_tx_empty(struct uart_port *port)
0564 {
0565     return TIOCSER_TEMT;
0566 }
0567 
0568 static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
0569 {
0570     return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
0571 }
0572 
0573 static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
0574 {
0575     /* IP block does not support these signals */
0576 }
0577 
0578 static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
0579 {
0580     /* IP block does not support sending a break */
0581 }
0582 
0583 static int sifive_serial_startup(struct uart_port *port)
0584 {
0585     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0586 
0587     __ssp_enable_rxwm(ssp);
0588 
0589     return 0;
0590 }
0591 
0592 static void sifive_serial_shutdown(struct uart_port *port)
0593 {
0594     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0595 
0596     __ssp_disable_rxwm(ssp);
0597     __ssp_disable_txwm(ssp);
0598 }
0599 
0600 /**
0601  * sifive_serial_clk_notifier() - clock post-rate-change notifier
0602  * @nb: pointer to the struct notifier_block, from the notifier code
0603  * @event: event mask from the notifier code
0604  * @data: pointer to the struct clk_notifier_data from the notifier code
0605  *
0606  * On the V0 SoC, the UART IP block is derived from the CPU clock source
0607  * after a synchronous divide-by-two divider, so any CPU clock rate change
0608  * requires the UART baud rate to be updated.  This presumably corrupts any
0609  * serial word currently being transmitted or received.  In order to avoid
0610  * corrupting the output data stream, we drain the transmit queue before
0611  * allowing the clock's rate to be changed.
0612  */
0613 static int sifive_serial_clk_notifier(struct notifier_block *nb,
0614                       unsigned long event, void *data)
0615 {
0616     struct clk_notifier_data *cnd = data;
0617     struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
0618 
0619     if (event == PRE_RATE_CHANGE) {
0620         /*
0621          * The TX watermark is always set to 1 by this driver, which
0622          * means that the TX busy bit will lower when there are 0 bytes
0623          * left in the TX queue -- in other words, when the TX FIFO is
0624          * empty.
0625          */
0626         __ssp_wait_for_xmitr(ssp);
0627         /*
0628          * On the cycle the TX FIFO goes empty there is still a full
0629          * UART frame left to be transmitted in the shift register.
0630          * The UART provides no way for software to directly determine
0631          * when that last frame has been transmitted, so we just sleep
0632          * here instead.  As we're not tracking the number of stop bits
0633          * they're just worst cased here.  The rest of the serial
0634          * framing parameters aren't configurable by software.
0635          */
0636         udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
0637     }
0638 
0639     if (event == POST_RATE_CHANGE && ssp->port.uartclk != cnd->new_rate) {
0640         ssp->port.uartclk = cnd->new_rate;
0641         __ssp_update_div(ssp);
0642     }
0643 
0644     return NOTIFY_OK;
0645 }
0646 
0647 static void sifive_serial_set_termios(struct uart_port *port,
0648                       struct ktermios *termios,
0649                       struct ktermios *old)
0650 {
0651     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0652     unsigned long flags;
0653     u32 v, old_v;
0654     int rate;
0655     char nstop;
0656 
0657     if ((termios->c_cflag & CSIZE) != CS8) {
0658         dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
0659         termios->c_cflag &= ~CSIZE;
0660         termios->c_cflag |= CS8;
0661     }
0662     if (termios->c_iflag & (INPCK | PARMRK))
0663         dev_err_once(ssp->port.dev, "parity checking not supported\n");
0664     if (termios->c_iflag & BRKINT)
0665         dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
0666     termios->c_iflag &= ~(INPCK|PARMRK|BRKINT);
0667 
0668     /* Set number of stop bits */
0669     nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
0670     __ssp_set_stop_bits(ssp, nstop);
0671 
0672     /* Set line rate */
0673     rate = uart_get_baud_rate(port, termios, old, 0,
0674                   ssp->port.uartclk / 16);
0675     __ssp_update_baud_rate(ssp, rate);
0676 
0677     spin_lock_irqsave(&ssp->port.lock, flags);
0678 
0679     /* Update the per-port timeout */
0680     uart_update_timeout(port, termios->c_cflag, rate);
0681 
0682     ssp->port.read_status_mask = 0;
0683 
0684     /* Ignore all characters if CREAD is not set */
0685     v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
0686     old_v = v;
0687     if ((termios->c_cflag & CREAD) == 0)
0688         v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
0689     else
0690         v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
0691     if (v != old_v)
0692         __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
0693 
0694     spin_unlock_irqrestore(&ssp->port.lock, flags);
0695 }
0696 
0697 static void sifive_serial_release_port(struct uart_port *port)
0698 {
0699 }
0700 
0701 static int sifive_serial_request_port(struct uart_port *port)
0702 {
0703     return 0;
0704 }
0705 
0706 static void sifive_serial_config_port(struct uart_port *port, int flags)
0707 {
0708     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0709 
0710     ssp->port.type = PORT_SIFIVE_V0;
0711 }
0712 
0713 static int sifive_serial_verify_port(struct uart_port *port,
0714                      struct serial_struct *ser)
0715 {
0716     return -EINVAL;
0717 }
0718 
0719 static const char *sifive_serial_type(struct uart_port *port)
0720 {
0721     return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
0722 }
0723 
0724 #ifdef CONFIG_CONSOLE_POLL
0725 static int sifive_serial_poll_get_char(struct uart_port *port)
0726 {
0727     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0728     char is_empty, ch;
0729 
0730     ch = __ssp_receive_char(ssp, &is_empty);
0731     if (is_empty)
0732         return NO_POLL_CHAR;
0733 
0734     return ch;
0735 }
0736 
0737 static void sifive_serial_poll_put_char(struct uart_port *port,
0738                     unsigned char c)
0739 {
0740     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0741 
0742     __ssp_wait_for_xmitr(ssp);
0743     __ssp_transmit_char(ssp, c);
0744 }
0745 #endif /* CONFIG_CONSOLE_POLL */
0746 
0747 /*
0748  * Early console support
0749  */
0750 
0751 #ifdef CONFIG_SERIAL_EARLYCON
0752 static void early_sifive_serial_putc(struct uart_port *port, unsigned char c)
0753 {
0754     while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
0755            SIFIVE_SERIAL_TXDATA_FULL_MASK)
0756         cpu_relax();
0757 
0758     __ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
0759 }
0760 
0761 static void early_sifive_serial_write(struct console *con, const char *s,
0762                       unsigned int n)
0763 {
0764     struct earlycon_device *dev = con->data;
0765     struct uart_port *port = &dev->port;
0766 
0767     uart_console_write(port, s, n, early_sifive_serial_putc);
0768 }
0769 
0770 static int __init early_sifive_serial_setup(struct earlycon_device *dev,
0771                         const char *options)
0772 {
0773     struct uart_port *port = &dev->port;
0774 
0775     if (!port->membase)
0776         return -ENODEV;
0777 
0778     dev->con->write = early_sifive_serial_write;
0779 
0780     return 0;
0781 }
0782 
0783 OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
0784 OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0",
0785             early_sifive_serial_setup);
0786 #endif /* CONFIG_SERIAL_EARLYCON */
0787 
0788 /*
0789  * Linux console interface
0790  */
0791 
0792 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
0793 
0794 static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
0795 
0796 static void sifive_serial_console_putchar(struct uart_port *port, unsigned char ch)
0797 {
0798     struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
0799 
0800     __ssp_wait_for_xmitr(ssp);
0801     __ssp_transmit_char(ssp, ch);
0802 }
0803 
0804 static void sifive_serial_console_write(struct console *co, const char *s,
0805                     unsigned int count)
0806 {
0807     struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
0808     unsigned long flags;
0809     unsigned int ier;
0810     int locked = 1;
0811 
0812     if (!ssp)
0813         return;
0814 
0815     local_irq_save(flags);
0816     if (ssp->port.sysrq)
0817         locked = 0;
0818     else if (oops_in_progress)
0819         locked = spin_trylock(&ssp->port.lock);
0820     else
0821         spin_lock(&ssp->port.lock);
0822 
0823     ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
0824     __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
0825 
0826     uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
0827 
0828     __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
0829 
0830     if (locked)
0831         spin_unlock(&ssp->port.lock);
0832     local_irq_restore(flags);
0833 }
0834 
0835 static int __init sifive_serial_console_setup(struct console *co, char *options)
0836 {
0837     struct sifive_serial_port *ssp;
0838     int baud = SIFIVE_DEFAULT_BAUD_RATE;
0839     int bits = 8;
0840     int parity = 'n';
0841     int flow = 'n';
0842 
0843     if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
0844         return -ENODEV;
0845 
0846     ssp = sifive_serial_console_ports[co->index];
0847     if (!ssp)
0848         return -ENODEV;
0849 
0850     if (options)
0851         uart_parse_options(options, &baud, &parity, &bits, &flow);
0852 
0853     return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
0854 }
0855 
0856 static struct uart_driver sifive_serial_uart_driver;
0857 
0858 static struct console sifive_serial_console = {
0859     .name       = SIFIVE_TTY_PREFIX,
0860     .write      = sifive_serial_console_write,
0861     .device     = uart_console_device,
0862     .setup      = sifive_serial_console_setup,
0863     .flags      = CON_PRINTBUFFER,
0864     .index      = -1,
0865     .data       = &sifive_serial_uart_driver,
0866 };
0867 
0868 static int __init sifive_console_init(void)
0869 {
0870     register_console(&sifive_serial_console);
0871     return 0;
0872 }
0873 
0874 console_initcall(sifive_console_init);
0875 
0876 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
0877 {
0878     sifive_serial_console_ports[ssp->port.line] = ssp;
0879 }
0880 
0881 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
0882 {
0883     sifive_serial_console_ports[ssp->port.line] = NULL;
0884 }
0885 
0886 #define SIFIVE_SERIAL_CONSOLE   (&sifive_serial_console)
0887 
0888 #else
0889 
0890 #define SIFIVE_SERIAL_CONSOLE   NULL
0891 
0892 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
0893 {}
0894 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
0895 {}
0896 
0897 #endif
0898 
0899 static const struct uart_ops sifive_serial_uops = {
0900     .tx_empty   = sifive_serial_tx_empty,
0901     .set_mctrl  = sifive_serial_set_mctrl,
0902     .get_mctrl  = sifive_serial_get_mctrl,
0903     .stop_tx    = sifive_serial_stop_tx,
0904     .start_tx   = sifive_serial_start_tx,
0905     .stop_rx    = sifive_serial_stop_rx,
0906     .break_ctl  = sifive_serial_break_ctl,
0907     .startup    = sifive_serial_startup,
0908     .shutdown   = sifive_serial_shutdown,
0909     .set_termios    = sifive_serial_set_termios,
0910     .type       = sifive_serial_type,
0911     .release_port   = sifive_serial_release_port,
0912     .request_port   = sifive_serial_request_port,
0913     .config_port    = sifive_serial_config_port,
0914     .verify_port    = sifive_serial_verify_port,
0915 #ifdef CONFIG_CONSOLE_POLL
0916     .poll_get_char  = sifive_serial_poll_get_char,
0917     .poll_put_char  = sifive_serial_poll_put_char,
0918 #endif
0919 };
0920 
0921 static struct uart_driver sifive_serial_uart_driver = {
0922     .owner      = THIS_MODULE,
0923     .driver_name    = SIFIVE_SERIAL_NAME,
0924     .dev_name   = SIFIVE_TTY_PREFIX,
0925     .nr     = SIFIVE_SERIAL_MAX_PORTS,
0926     .cons       = SIFIVE_SERIAL_CONSOLE,
0927 };
0928 
0929 static int sifive_serial_probe(struct platform_device *pdev)
0930 {
0931     struct sifive_serial_port *ssp;
0932     struct resource *mem;
0933     struct clk *clk;
0934     void __iomem *base;
0935     int irq, id, r;
0936 
0937     irq = platform_get_irq(pdev, 0);
0938     if (irq < 0)
0939         return -EPROBE_DEFER;
0940 
0941     mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0942     base = devm_ioremap_resource(&pdev->dev, mem);
0943     if (IS_ERR(base)) {
0944         dev_err(&pdev->dev, "could not acquire device memory\n");
0945         return PTR_ERR(base);
0946     }
0947 
0948     clk = devm_clk_get_enabled(&pdev->dev, NULL);
0949     if (IS_ERR(clk)) {
0950         dev_err(&pdev->dev, "unable to find controller clock\n");
0951         return PTR_ERR(clk);
0952     }
0953 
0954     id = of_alias_get_id(pdev->dev.of_node, "serial");
0955     if (id < 0) {
0956         dev_err(&pdev->dev, "missing aliases entry\n");
0957         return id;
0958     }
0959 
0960 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
0961     if (id > SIFIVE_SERIAL_MAX_PORTS) {
0962         dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
0963         return -EINVAL;
0964     }
0965 #endif
0966 
0967     ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
0968     if (!ssp)
0969         return -ENOMEM;
0970 
0971     ssp->port.dev = &pdev->dev;
0972     ssp->port.type = PORT_SIFIVE_V0;
0973     ssp->port.iotype = UPIO_MEM;
0974     ssp->port.irq = irq;
0975     ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
0976     ssp->port.ops = &sifive_serial_uops;
0977     ssp->port.line = id;
0978     ssp->port.mapbase = mem->start;
0979     ssp->port.membase = base;
0980     ssp->dev = &pdev->dev;
0981     ssp->clk = clk;
0982     ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
0983 
0984     r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
0985     if (r) {
0986         dev_err(&pdev->dev, "could not register clock notifier: %d\n",
0987             r);
0988         goto probe_out1;
0989     }
0990 
0991     /* Set up clock divider */
0992     ssp->port.uartclk = clk_get_rate(ssp->clk);
0993     ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
0994     __ssp_update_div(ssp);
0995 
0996     platform_set_drvdata(pdev, ssp);
0997 
0998     /* Enable transmits and set the watermark level to 1 */
0999     __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
1000              SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
1001              SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
1002 
1003     /* Enable receives and set the watermark level to 0 */
1004     __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
1005              SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
1006              SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
1007 
1008     r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
1009             dev_name(&pdev->dev), ssp);
1010     if (r) {
1011         dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
1012         goto probe_out2;
1013     }
1014 
1015     __ssp_add_console_port(ssp);
1016 
1017     r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
1018     if (r != 0) {
1019         dev_err(&pdev->dev, "could not add uart: %d\n", r);
1020         goto probe_out3;
1021     }
1022 
1023     return 0;
1024 
1025 probe_out3:
1026     __ssp_remove_console_port(ssp);
1027     free_irq(ssp->port.irq, ssp);
1028 probe_out2:
1029     clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1030 probe_out1:
1031     return r;
1032 }
1033 
1034 static int sifive_serial_remove(struct platform_device *dev)
1035 {
1036     struct sifive_serial_port *ssp = platform_get_drvdata(dev);
1037 
1038     __ssp_remove_console_port(ssp);
1039     uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
1040     free_irq(ssp->port.irq, ssp);
1041     clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1042 
1043     return 0;
1044 }
1045 
1046 static const struct of_device_id sifive_serial_of_match[] = {
1047     { .compatible = "sifive,fu540-c000-uart0" },
1048     { .compatible = "sifive,uart0" },
1049     {},
1050 };
1051 MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
1052 
1053 static struct platform_driver sifive_serial_platform_driver = {
1054     .probe      = sifive_serial_probe,
1055     .remove     = sifive_serial_remove,
1056     .driver     = {
1057         .name   = SIFIVE_SERIAL_NAME,
1058         .of_match_table = of_match_ptr(sifive_serial_of_match),
1059     },
1060 };
1061 
1062 static int __init sifive_serial_init(void)
1063 {
1064     int r;
1065 
1066     r = uart_register_driver(&sifive_serial_uart_driver);
1067     if (r)
1068         goto init_out1;
1069 
1070     r = platform_driver_register(&sifive_serial_platform_driver);
1071     if (r)
1072         goto init_out2;
1073 
1074     return 0;
1075 
1076 init_out2:
1077     uart_unregister_driver(&sifive_serial_uart_driver);
1078 init_out1:
1079     return r;
1080 }
1081 
1082 static void __exit sifive_serial_exit(void)
1083 {
1084     platform_driver_unregister(&sifive_serial_platform_driver);
1085     uart_unregister_driver(&sifive_serial_uart_driver);
1086 }
1087 
1088 module_init(sifive_serial_init);
1089 module_exit(sifive_serial_exit);
1090 
1091 MODULE_DESCRIPTION("SiFive UART serial driver");
1092 MODULE_LICENSE("GPL");
1093 MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");