cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

sifive.c (30694B)


      1// SPDX-License-Identifier: GPL-2.0+
      2/*
      3 * SiFive UART driver
      4 * Copyright (C) 2018 Paul Walmsley <paul@pwsan.com>
      5 * Copyright (C) 2018-2019 SiFive
      6 *
      7 * This program is free software; you can redistribute it and/or modify
      8 * it under the terms of the GNU General Public License as published by
      9 * the Free Software Foundation; either version 2 of the License, or
     10 * (at your option) any later version.
     11 *
     12 * This program is distributed in the hope that it will be useful,
     13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15 * GNU General Public License for more details.
     16 *
     17 * Based partially on:
     18 * - drivers/tty/serial/pxa.c
     19 * - drivers/tty/serial/amba-pl011.c
     20 * - drivers/tty/serial/uartlite.c
     21 * - drivers/tty/serial/omap-serial.c
     22 * - drivers/pwm/pwm-sifive.c
     23 *
     24 * See the following sources for further documentation:
     25 * - Chapter 19 "Universal Asynchronous Receiver/Transmitter (UART)" of
     26 *   SiFive FE310-G000 v2p3
     27 * - The tree/master/src/main/scala/devices/uart directory of
     28 *   https://github.com/sifive/sifive-blocks/
     29 *
     30 * The SiFive UART design is not 8250-compatible.  The following common
     31 * features are not supported:
     32 * - Word lengths other than 8 bits
     33 * - Break handling
     34 * - Parity
     35 * - Flow control
     36 * - Modem signals (DSR, RI, etc.)
     37 * On the other hand, the design is free from the baggage of the 8250
     38 * programming model.
     39 */
     40
     41#include <linux/clk.h>
     42#include <linux/console.h>
     43#include <linux/delay.h>
     44#include <linux/init.h>
     45#include <linux/io.h>
     46#include <linux/irq.h>
     47#include <linux/module.h>
     48#include <linux/of.h>
     49#include <linux/of_irq.h>
     50#include <linux/platform_device.h>
     51#include <linux/serial_core.h>
     52#include <linux/serial_reg.h>
     53#include <linux/slab.h>
     54#include <linux/tty.h>
     55#include <linux/tty_flip.h>
     56
     57/*
     58 * Register offsets
     59 */
     60
     61/* TXDATA */
     62#define SIFIVE_SERIAL_TXDATA_OFFS		0x0
     63#define SIFIVE_SERIAL_TXDATA_FULL_SHIFT		31
     64#define SIFIVE_SERIAL_TXDATA_FULL_MASK		(1 << SIFIVE_SERIAL_TXDATA_FULL_SHIFT)
     65#define SIFIVE_SERIAL_TXDATA_DATA_SHIFT		0
     66#define SIFIVE_SERIAL_TXDATA_DATA_MASK		(0xff << SIFIVE_SERIAL_TXDATA_DATA_SHIFT)
     67
     68/* RXDATA */
     69#define SIFIVE_SERIAL_RXDATA_OFFS		0x4
     70#define SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT	31
     71#define SIFIVE_SERIAL_RXDATA_EMPTY_MASK		(1 << SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT)
     72#define SIFIVE_SERIAL_RXDATA_DATA_SHIFT		0
     73#define SIFIVE_SERIAL_RXDATA_DATA_MASK		(0xff << SIFIVE_SERIAL_RXDATA_DATA_SHIFT)
     74
     75/* TXCTRL */
     76#define SIFIVE_SERIAL_TXCTRL_OFFS		0x8
     77#define SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT	16
     78#define SIFIVE_SERIAL_TXCTRL_TXCNT_MASK		(0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
     79#define SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT	1
     80#define SIFIVE_SERIAL_TXCTRL_NSTOP_MASK		(1 << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT)
     81#define SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT		0
     82#define SIFIVE_SERIAL_TXCTRL_TXEN_MASK		(1 << SIFIVE_SERIAL_TXCTRL_TXEN_SHIFT)
     83
     84/* RXCTRL */
     85#define SIFIVE_SERIAL_RXCTRL_OFFS		0xC
     86#define SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT	16
     87#define SIFIVE_SERIAL_RXCTRL_RXCNT_MASK		(0x7 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT)
     88#define SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT		0
     89#define SIFIVE_SERIAL_RXCTRL_RXEN_MASK		(1 << SIFIVE_SERIAL_RXCTRL_RXEN_SHIFT)
     90
     91/* IE */
     92#define SIFIVE_SERIAL_IE_OFFS			0x10
     93#define SIFIVE_SERIAL_IE_RXWM_SHIFT		1
     94#define SIFIVE_SERIAL_IE_RXWM_MASK		(1 << SIFIVE_SERIAL_IE_RXWM_SHIFT)
     95#define SIFIVE_SERIAL_IE_TXWM_SHIFT		0
     96#define SIFIVE_SERIAL_IE_TXWM_MASK		(1 << SIFIVE_SERIAL_IE_TXWM_SHIFT)
     97
     98/* IP */
     99#define SIFIVE_SERIAL_IP_OFFS			0x14
    100#define SIFIVE_SERIAL_IP_RXWM_SHIFT		1
    101#define SIFIVE_SERIAL_IP_RXWM_MASK		(1 << SIFIVE_SERIAL_IP_RXWM_SHIFT)
    102#define SIFIVE_SERIAL_IP_TXWM_SHIFT		0
    103#define SIFIVE_SERIAL_IP_TXWM_MASK		(1 << SIFIVE_SERIAL_IP_TXWM_SHIFT)
    104
    105/* DIV */
    106#define SIFIVE_SERIAL_DIV_OFFS			0x18
    107#define SIFIVE_SERIAL_DIV_DIV_SHIFT		0
    108#define SIFIVE_SERIAL_DIV_DIV_MASK		(0xffff << SIFIVE_SERIAL_IP_DIV_SHIFT)
    109
    110/*
    111 * Config macros
    112 */
    113
    114/*
    115 * SIFIVE_SERIAL_MAX_PORTS: maximum number of UARTs on a device that can
    116 *                          host a serial console
    117 */
    118#define SIFIVE_SERIAL_MAX_PORTS			8
    119
    120/*
    121 * SIFIVE_DEFAULT_BAUD_RATE: default baud rate that the driver should
    122 *                           configure itself to use
    123 */
    124#define SIFIVE_DEFAULT_BAUD_RATE		115200
    125
    126/* SIFIVE_SERIAL_NAME: our driver's name that we pass to the operating system */
    127#define SIFIVE_SERIAL_NAME			"sifive-serial"
    128
    129/* SIFIVE_TTY_PREFIX: tty name prefix for SiFive serial ports */
    130#define SIFIVE_TTY_PREFIX			"ttySIF"
    131
    132/* SIFIVE_TX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
    133#define SIFIVE_TX_FIFO_DEPTH			8
    134
    135/* SIFIVE_RX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
    136#define SIFIVE_RX_FIFO_DEPTH			8
    137
    138#if (SIFIVE_TX_FIFO_DEPTH != SIFIVE_RX_FIFO_DEPTH)
    139#error Driver does not support configurations with different TX, RX FIFO sizes
    140#endif
    141
    142/*
    143 *
    144 */
    145
    146/**
    147 * struct sifive_serial_port - driver-specific data extension to struct uart_port
    148 * @port: struct uart_port embedded in this struct
    149 * @dev: struct device *
    150 * @ier: shadowed copy of the interrupt enable register
    151 * @baud_rate: UART serial line rate (e.g., 115200 baud)
    152 * @clk: reference to this device's clock
    153 * @clk_notifier: clock rate change notifier for upstream clock changes
    154 *
    155 * Configuration data specific to this SiFive UART.
    156 */
    157struct sifive_serial_port {
    158	struct uart_port	port;
    159	struct device		*dev;
    160	unsigned char		ier;
    161	unsigned long		baud_rate;
    162	struct clk		*clk;
    163	struct notifier_block	clk_notifier;
    164};
    165
    166/*
    167 * Structure container-of macros
    168 */
    169
    170#define port_to_sifive_serial_port(p) (container_of((p), \
    171						    struct sifive_serial_port, \
    172						    port))
    173
    174#define notifier_to_sifive_serial_port(nb) (container_of((nb), \
    175							 struct sifive_serial_port, \
    176							 clk_notifier))
    177
    178/*
    179 * Forward declarations
    180 */
    181static void sifive_serial_stop_tx(struct uart_port *port);
    182
    183/*
    184 * Internal functions
    185 */
    186
    187/**
    188 * __ssp_early_writel() - write to a SiFive serial port register (early)
    189 * @port: pointer to a struct uart_port record
    190 * @offs: register address offset from the IP block base address
    191 * @v: value to write to the register
    192 *
    193 * Given a pointer @port to a struct uart_port record, write the value
    194 * @v to the IP block register address offset @offs.  This function is
    195 * intended for early console use.
    196 *
    197 * Context: Intended to be used only by the earlyconsole code.
    198 */
    199static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port)
    200{
    201	writel_relaxed(v, port->membase + offs);
    202}
    203
    204/**
    205 * __ssp_early_readl() - read from a SiFive serial port register (early)
    206 * @port: pointer to a struct uart_port record
    207 * @offs: register address offset from the IP block base address
    208 *
    209 * Given a pointer @port to a struct uart_port record, read the
    210 * contents of the IP block register located at offset @offs from the
    211 * IP block base and return it.  This function is intended for early
    212 * console use.
    213 *
    214 * Context: Intended to be called only by the earlyconsole code or by
    215 *          __ssp_readl() or __ssp_writel() (in this driver)
    216 *
    217 * Returns: the register value read from the UART.
    218 */
    219static u32 __ssp_early_readl(struct uart_port *port, u16 offs)
    220{
    221	return readl_relaxed(port->membase + offs);
    222}
    223
    224/**
    225 * __ssp_writel() - write to a SiFive serial port register
    226 * @v: value to write to the register
    227 * @offs: register address offset from the IP block base address
    228 * @ssp: pointer to a struct sifive_serial_port record
    229 *
    230 * Write the value @v to the IP block register located at offset @offs from the
    231 * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
    232 *
    233 * Context: Any context.
    234 */
    235static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp)
    236{
    237	__ssp_early_writel(v, offs, &ssp->port);
    238}
    239
    240/**
    241 * __ssp_readl() - read from a SiFive serial port register
    242 * @ssp: pointer to a struct sifive_serial_port record
    243 * @offs: register address offset from the IP block base address
    244 *
    245 * Read the contents of the IP block register located at offset @offs from the
    246 * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
    247 *
    248 * Context: Any context.
    249 *
    250 * Returns: the value of the UART register
    251 */
    252static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs)
    253{
    254	return __ssp_early_readl(&ssp->port, offs);
    255}
    256
    257/**
    258 * sifive_serial_is_txfifo_full() - is the TXFIFO full?
    259 * @ssp: pointer to a struct sifive_serial_port
    260 *
    261 * Read the transmit FIFO "full" bit, returning a non-zero value if the
    262 * TX FIFO is full, or zero if space remains.  Intended to be used to prevent
    263 * writes to the TX FIFO when it's full.
    264 *
    265 * Returns: SIFIVE_SERIAL_TXDATA_FULL_MASK (non-zero) if the transmit FIFO
    266 * is full, or 0 if space remains.
    267 */
    268static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp)
    269{
    270	return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) &
    271		SIFIVE_SERIAL_TXDATA_FULL_MASK;
    272}
    273
    274/**
    275 * __ssp_transmit_char() - enqueue a byte to transmit onto the TX FIFO
    276 * @ssp: pointer to a struct sifive_serial_port
    277 * @ch: character to transmit
    278 *
    279 * Enqueue a byte @ch onto the transmit FIFO, given a pointer @ssp to the
    280 * struct sifive_serial_port * to transmit on.  Caller should first check to
    281 * ensure that the TXFIFO has space; see sifive_serial_is_txfifo_full().
    282 *
    283 * Context: Any context.
    284 */
    285static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch)
    286{
    287	__ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp);
    288}
    289
    290/**
    291 * __ssp_transmit_chars() - enqueue multiple bytes onto the TX FIFO
    292 * @ssp: pointer to a struct sifive_serial_port
    293 *
    294 * Transfer up to a TX FIFO size's worth of characters from the Linux serial
    295 * transmit buffer to the SiFive UART TX FIFO.
    296 *
    297 * Context: Any context.  Expects @ssp->port.lock to be held by caller.
    298 */
    299static void __ssp_transmit_chars(struct sifive_serial_port *ssp)
    300{
    301	struct circ_buf *xmit = &ssp->port.state->xmit;
    302	int count;
    303
    304	if (ssp->port.x_char) {
    305		__ssp_transmit_char(ssp, ssp->port.x_char);
    306		ssp->port.icount.tx++;
    307		ssp->port.x_char = 0;
    308		return;
    309	}
    310	if (uart_circ_empty(xmit) || uart_tx_stopped(&ssp->port)) {
    311		sifive_serial_stop_tx(&ssp->port);
    312		return;
    313	}
    314	count = SIFIVE_TX_FIFO_DEPTH;
    315	do {
    316		__ssp_transmit_char(ssp, xmit->buf[xmit->tail]);
    317		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
    318		ssp->port.icount.tx++;
    319		if (uart_circ_empty(xmit))
    320			break;
    321	} while (--count > 0);
    322
    323	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
    324		uart_write_wakeup(&ssp->port);
    325
    326	if (uart_circ_empty(xmit))
    327		sifive_serial_stop_tx(&ssp->port);
    328}
    329
    330/**
    331 * __ssp_enable_txwm() - enable transmit watermark interrupts
    332 * @ssp: pointer to a struct sifive_serial_port
    333 *
    334 * Enable interrupt generation when the transmit FIFO watermark is reached
    335 * on the SiFive UART referred to by @ssp.
    336 */
    337static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
    338{
    339	if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
    340		return;
    341
    342	ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
    343	__ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
    344}
    345
    346/**
    347 * __ssp_enable_rxwm() - enable receive watermark interrupts
    348 * @ssp: pointer to a struct sifive_serial_port
    349 *
    350 * Enable interrupt generation when the receive FIFO watermark is reached
    351 * on the SiFive UART referred to by @ssp.
    352 */
    353static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
    354{
    355	if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
    356		return;
    357
    358	ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
    359	__ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
    360}
    361
    362/**
    363 * __ssp_disable_txwm() - disable transmit watermark interrupts
    364 * @ssp: pointer to a struct sifive_serial_port
    365 *
    366 * Disable interrupt generation when the transmit FIFO watermark is reached
    367 * on the UART referred to by @ssp.
    368 */
    369static void __ssp_disable_txwm(struct sifive_serial_port *ssp)
    370{
    371	if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK))
    372		return;
    373
    374	ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK;
    375	__ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
    376}
    377
    378/**
    379 * __ssp_disable_rxwm() - disable receive watermark interrupts
    380 * @ssp: pointer to a struct sifive_serial_port
    381 *
    382 * Disable interrupt generation when the receive FIFO watermark is reached
    383 * on the UART referred to by @ssp.
    384 */
    385static void __ssp_disable_rxwm(struct sifive_serial_port *ssp)
    386{
    387	if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK))
    388		return;
    389
    390	ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK;
    391	__ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
    392}
    393
    394/**
    395 * __ssp_receive_char() - receive a byte from the UART
    396 * @ssp: pointer to a struct sifive_serial_port
    397 * @is_empty: char pointer to return whether the RX FIFO is empty
    398 *
    399 * Try to read a byte from the SiFive UART RX FIFO, referenced by
    400 * @ssp, and to return it.  Also returns the RX FIFO empty bit in
    401 * the char pointed to by @ch.  The caller must pass the byte back to the
    402 * Linux serial layer if needed.
    403 *
    404 * Returns: the byte read from the UART RX FIFO.
    405 */
    406static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
    407{
    408	u32 v;
    409	u8 ch;
    410
    411	v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
    412
    413	if (!is_empty)
    414		WARN_ON(1);
    415	else
    416		*is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
    417			SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
    418
    419	ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
    420		SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
    421
    422	return ch;
    423}
    424
    425/**
    426 * __ssp_receive_chars() - receive multiple bytes from the UART
    427 * @ssp: pointer to a struct sifive_serial_port
    428 *
    429 * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred
    430 * to by @ssp and pass them up to the Linux serial layer.
    431 *
    432 * Context: Expects ssp->port.lock to be held by caller.
    433 */
    434static void __ssp_receive_chars(struct sifive_serial_port *ssp)
    435{
    436	unsigned char ch;
    437	char is_empty;
    438	int c;
    439
    440	for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
    441		ch = __ssp_receive_char(ssp, &is_empty);
    442		if (is_empty)
    443			break;
    444
    445		ssp->port.icount.rx++;
    446		uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
    447	}
    448
    449	tty_flip_buffer_push(&ssp->port.state->port);
    450}
    451
    452/**
    453 * __ssp_update_div() - calculate the divisor setting by the line rate
    454 * @ssp: pointer to a struct sifive_serial_port
    455 *
    456 * Calculate the appropriate value of the clock divisor for the UART
    457 * and target line rate referred to by @ssp and write it into the
    458 * hardware.
    459 */
    460static void __ssp_update_div(struct sifive_serial_port *ssp)
    461{
    462	u16 div;
    463
    464	div = DIV_ROUND_UP(ssp->port.uartclk, ssp->baud_rate) - 1;
    465
    466	__ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
    467}
    468
    469/**
    470 * __ssp_update_baud_rate() - set the UART "baud rate"
    471 * @ssp: pointer to a struct sifive_serial_port
    472 * @rate: new target bit rate
    473 *
    474 * Calculate the UART divisor value for the target bit rate @rate for the
    475 * SiFive UART described by @ssp and program it into the UART.  There may
    476 * be some error between the target bit rate and the actual bit rate implemented
    477 * by the UART due to clock ratio granularity.
    478 */
    479static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
    480				   unsigned int rate)
    481{
    482	if (ssp->baud_rate == rate)
    483		return;
    484
    485	ssp->baud_rate = rate;
    486	__ssp_update_div(ssp);
    487}
    488
    489/**
    490 * __ssp_set_stop_bits() - set the number of stop bits
    491 * @ssp: pointer to a struct sifive_serial_port
    492 * @nstop: 1 or 2 (stop bits)
    493 *
    494 * Program the SiFive UART referred to by @ssp to use @nstop stop bits.
    495 */
    496static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
    497{
    498	u32 v;
    499
    500	if (nstop < 1 || nstop > 2) {
    501		WARN_ON(1);
    502		return;
    503	}
    504
    505	v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
    506	v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
    507	v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
    508	__ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
    509}
    510
    511/**
    512 * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO
    513 * @ssp: pointer to a struct sifive_serial_port
    514 *
    515 * Delay while the UART TX FIFO referred to by @ssp is marked as full.
    516 *
    517 * Context: Any context.
    518 */
    519static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
    520{
    521	while (sifive_serial_is_txfifo_full(ssp))
    522		udelay(1); /* XXX Could probably be more intelligent here */
    523}
    524
    525/*
    526 * Linux serial API functions
    527 */
    528
    529static void sifive_serial_stop_tx(struct uart_port *port)
    530{
    531	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    532
    533	__ssp_disable_txwm(ssp);
    534}
    535
    536static void sifive_serial_stop_rx(struct uart_port *port)
    537{
    538	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    539
    540	__ssp_disable_rxwm(ssp);
    541}
    542
    543static void sifive_serial_start_tx(struct uart_port *port)
    544{
    545	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    546
    547	__ssp_enable_txwm(ssp);
    548}
    549
    550static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
    551{
    552	struct sifive_serial_port *ssp = dev_id;
    553	u32 ip;
    554
    555	spin_lock(&ssp->port.lock);
    556
    557	ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
    558	if (!ip) {
    559		spin_unlock(&ssp->port.lock);
    560		return IRQ_NONE;
    561	}
    562
    563	if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
    564		__ssp_receive_chars(ssp);
    565	if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
    566		__ssp_transmit_chars(ssp);
    567
    568	spin_unlock(&ssp->port.lock);
    569
    570	return IRQ_HANDLED;
    571}
    572
    573static unsigned int sifive_serial_tx_empty(struct uart_port *port)
    574{
    575	return TIOCSER_TEMT;
    576}
    577
    578static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
    579{
    580	return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
    581}
    582
    583static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
    584{
    585	/* IP block does not support these signals */
    586}
    587
    588static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
    589{
    590	/* IP block does not support sending a break */
    591}
    592
    593static int sifive_serial_startup(struct uart_port *port)
    594{
    595	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    596
    597	__ssp_enable_rxwm(ssp);
    598
    599	return 0;
    600}
    601
    602static void sifive_serial_shutdown(struct uart_port *port)
    603{
    604	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    605
    606	__ssp_disable_rxwm(ssp);
    607	__ssp_disable_txwm(ssp);
    608}
    609
    610/**
    611 * sifive_serial_clk_notifier() - clock post-rate-change notifier
    612 * @nb: pointer to the struct notifier_block, from the notifier code
    613 * @event: event mask from the notifier code
    614 * @data: pointer to the struct clk_notifier_data from the notifier code
    615 *
    616 * On the V0 SoC, the UART IP block is derived from the CPU clock source
    617 * after a synchronous divide-by-two divider, so any CPU clock rate change
    618 * requires the UART baud rate to be updated.  This presumably corrupts any
    619 * serial word currently being transmitted or received.  In order to avoid
    620 * corrupting the output data stream, we drain the transmit queue before
    621 * allowing the clock's rate to be changed.
    622 */
    623static int sifive_serial_clk_notifier(struct notifier_block *nb,
    624				      unsigned long event, void *data)
    625{
    626	struct clk_notifier_data *cnd = data;
    627	struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
    628
    629	if (event == PRE_RATE_CHANGE) {
    630		/*
    631		 * The TX watermark is always set to 1 by this driver, which
    632		 * means that the TX busy bit will lower when there are 0 bytes
    633		 * left in the TX queue -- in other words, when the TX FIFO is
    634		 * empty.
    635		 */
    636		__ssp_wait_for_xmitr(ssp);
    637		/*
    638		 * On the cycle the TX FIFO goes empty there is still a full
    639		 * UART frame left to be transmitted in the shift register.
    640		 * The UART provides no way for software to directly determine
    641		 * when that last frame has been transmitted, so we just sleep
    642		 * here instead.  As we're not tracking the number of stop bits
    643		 * they're just worst cased here.  The rest of the serial
    644		 * framing parameters aren't configurable by software.
    645		 */
    646		udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
    647	}
    648
    649	if (event == POST_RATE_CHANGE && ssp->port.uartclk != cnd->new_rate) {
    650		ssp->port.uartclk = cnd->new_rate;
    651		__ssp_update_div(ssp);
    652	}
    653
    654	return NOTIFY_OK;
    655}
    656
    657static void sifive_serial_set_termios(struct uart_port *port,
    658				      struct ktermios *termios,
    659				      struct ktermios *old)
    660{
    661	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    662	unsigned long flags;
    663	u32 v, old_v;
    664	int rate;
    665	char nstop;
    666
    667	if ((termios->c_cflag & CSIZE) != CS8) {
    668		dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
    669		termios->c_cflag &= ~CSIZE;
    670		termios->c_cflag |= CS8;
    671	}
    672	if (termios->c_iflag & (INPCK | PARMRK))
    673		dev_err_once(ssp->port.dev, "parity checking not supported\n");
    674	if (termios->c_iflag & BRKINT)
    675		dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
    676	termios->c_iflag &= ~(INPCK|PARMRK|BRKINT);
    677
    678	/* Set number of stop bits */
    679	nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
    680	__ssp_set_stop_bits(ssp, nstop);
    681
    682	/* Set line rate */
    683	rate = uart_get_baud_rate(port, termios, old, 0,
    684				  ssp->port.uartclk / 16);
    685	__ssp_update_baud_rate(ssp, rate);
    686
    687	spin_lock_irqsave(&ssp->port.lock, flags);
    688
    689	/* Update the per-port timeout */
    690	uart_update_timeout(port, termios->c_cflag, rate);
    691
    692	ssp->port.read_status_mask = 0;
    693
    694	/* Ignore all characters if CREAD is not set */
    695	v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
    696	old_v = v;
    697	if ((termios->c_cflag & CREAD) == 0)
    698		v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
    699	else
    700		v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
    701	if (v != old_v)
    702		__ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
    703
    704	spin_unlock_irqrestore(&ssp->port.lock, flags);
    705}
    706
    707static void sifive_serial_release_port(struct uart_port *port)
    708{
    709}
    710
    711static int sifive_serial_request_port(struct uart_port *port)
    712{
    713	return 0;
    714}
    715
    716static void sifive_serial_config_port(struct uart_port *port, int flags)
    717{
    718	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    719
    720	ssp->port.type = PORT_SIFIVE_V0;
    721}
    722
    723static int sifive_serial_verify_port(struct uart_port *port,
    724				     struct serial_struct *ser)
    725{
    726	return -EINVAL;
    727}
    728
    729static const char *sifive_serial_type(struct uart_port *port)
    730{
    731	return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
    732}
    733
    734#ifdef CONFIG_CONSOLE_POLL
    735static int sifive_serial_poll_get_char(struct uart_port *port)
    736{
    737	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    738	char is_empty, ch;
    739
    740	ch = __ssp_receive_char(ssp, &is_empty);
    741	if (is_empty)
    742		return NO_POLL_CHAR;
    743
    744	return ch;
    745}
    746
    747static void sifive_serial_poll_put_char(struct uart_port *port,
    748					unsigned char c)
    749{
    750	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    751
    752	__ssp_wait_for_xmitr(ssp);
    753	__ssp_transmit_char(ssp, c);
    754}
    755#endif /* CONFIG_CONSOLE_POLL */
    756
    757/*
    758 * Early console support
    759 */
    760
    761#ifdef CONFIG_SERIAL_EARLYCON
    762static void early_sifive_serial_putc(struct uart_port *port, unsigned char c)
    763{
    764	while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
    765	       SIFIVE_SERIAL_TXDATA_FULL_MASK)
    766		cpu_relax();
    767
    768	__ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
    769}
    770
    771static void early_sifive_serial_write(struct console *con, const char *s,
    772				      unsigned int n)
    773{
    774	struct earlycon_device *dev = con->data;
    775	struct uart_port *port = &dev->port;
    776
    777	uart_console_write(port, s, n, early_sifive_serial_putc);
    778}
    779
    780static int __init early_sifive_serial_setup(struct earlycon_device *dev,
    781					    const char *options)
    782{
    783	struct uart_port *port = &dev->port;
    784
    785	if (!port->membase)
    786		return -ENODEV;
    787
    788	dev->con->write = early_sifive_serial_write;
    789
    790	return 0;
    791}
    792
    793OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
    794OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0",
    795		    early_sifive_serial_setup);
    796#endif /* CONFIG_SERIAL_EARLYCON */
    797
    798/*
    799 * Linux console interface
    800 */
    801
    802#ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
    803
    804static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
    805
    806static void sifive_serial_console_putchar(struct uart_port *port, unsigned char ch)
    807{
    808	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
    809
    810	__ssp_wait_for_xmitr(ssp);
    811	__ssp_transmit_char(ssp, ch);
    812}
    813
    814static void sifive_serial_console_write(struct console *co, const char *s,
    815					unsigned int count)
    816{
    817	struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
    818	unsigned long flags;
    819	unsigned int ier;
    820	int locked = 1;
    821
    822	if (!ssp)
    823		return;
    824
    825	local_irq_save(flags);
    826	if (ssp->port.sysrq)
    827		locked = 0;
    828	else if (oops_in_progress)
    829		locked = spin_trylock(&ssp->port.lock);
    830	else
    831		spin_lock(&ssp->port.lock);
    832
    833	ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
    834	__ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
    835
    836	uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
    837
    838	__ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
    839
    840	if (locked)
    841		spin_unlock(&ssp->port.lock);
    842	local_irq_restore(flags);
    843}
    844
    845static int __init sifive_serial_console_setup(struct console *co, char *options)
    846{
    847	struct sifive_serial_port *ssp;
    848	int baud = SIFIVE_DEFAULT_BAUD_RATE;
    849	int bits = 8;
    850	int parity = 'n';
    851	int flow = 'n';
    852
    853	if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
    854		return -ENODEV;
    855
    856	ssp = sifive_serial_console_ports[co->index];
    857	if (!ssp)
    858		return -ENODEV;
    859
    860	if (options)
    861		uart_parse_options(options, &baud, &parity, &bits, &flow);
    862
    863	return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
    864}
    865
    866static struct uart_driver sifive_serial_uart_driver;
    867
    868static struct console sifive_serial_console = {
    869	.name		= SIFIVE_TTY_PREFIX,
    870	.write		= sifive_serial_console_write,
    871	.device		= uart_console_device,
    872	.setup		= sifive_serial_console_setup,
    873	.flags		= CON_PRINTBUFFER,
    874	.index		= -1,
    875	.data		= &sifive_serial_uart_driver,
    876};
    877
    878static int __init sifive_console_init(void)
    879{
    880	register_console(&sifive_serial_console);
    881	return 0;
    882}
    883
    884console_initcall(sifive_console_init);
    885
    886static void __ssp_add_console_port(struct sifive_serial_port *ssp)
    887{
    888	sifive_serial_console_ports[ssp->port.line] = ssp;
    889}
    890
    891static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
    892{
    893	sifive_serial_console_ports[ssp->port.line] = NULL;
    894}
    895
    896#define SIFIVE_SERIAL_CONSOLE	(&sifive_serial_console)
    897
    898#else
    899
    900#define SIFIVE_SERIAL_CONSOLE	NULL
    901
    902static void __ssp_add_console_port(struct sifive_serial_port *ssp)
    903{}
    904static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
    905{}
    906
    907#endif
    908
    909static const struct uart_ops sifive_serial_uops = {
    910	.tx_empty	= sifive_serial_tx_empty,
    911	.set_mctrl	= sifive_serial_set_mctrl,
    912	.get_mctrl	= sifive_serial_get_mctrl,
    913	.stop_tx	= sifive_serial_stop_tx,
    914	.start_tx	= sifive_serial_start_tx,
    915	.stop_rx	= sifive_serial_stop_rx,
    916	.break_ctl	= sifive_serial_break_ctl,
    917	.startup	= sifive_serial_startup,
    918	.shutdown	= sifive_serial_shutdown,
    919	.set_termios	= sifive_serial_set_termios,
    920	.type		= sifive_serial_type,
    921	.release_port	= sifive_serial_release_port,
    922	.request_port	= sifive_serial_request_port,
    923	.config_port	= sifive_serial_config_port,
    924	.verify_port	= sifive_serial_verify_port,
    925#ifdef CONFIG_CONSOLE_POLL
    926	.poll_get_char	= sifive_serial_poll_get_char,
    927	.poll_put_char	= sifive_serial_poll_put_char,
    928#endif
    929};
    930
    931static struct uart_driver sifive_serial_uart_driver = {
    932	.owner		= THIS_MODULE,
    933	.driver_name	= SIFIVE_SERIAL_NAME,
    934	.dev_name	= SIFIVE_TTY_PREFIX,
    935	.nr		= SIFIVE_SERIAL_MAX_PORTS,
    936	.cons		= SIFIVE_SERIAL_CONSOLE,
    937};
    938
    939static int sifive_serial_probe(struct platform_device *pdev)
    940{
    941	struct sifive_serial_port *ssp;
    942	struct resource *mem;
    943	struct clk *clk;
    944	void __iomem *base;
    945	int irq, id, r;
    946
    947	irq = platform_get_irq(pdev, 0);
    948	if (irq < 0)
    949		return -EPROBE_DEFER;
    950
    951	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
    952	base = devm_ioremap_resource(&pdev->dev, mem);
    953	if (IS_ERR(base)) {
    954		dev_err(&pdev->dev, "could not acquire device memory\n");
    955		return PTR_ERR(base);
    956	}
    957
    958	clk = devm_clk_get(&pdev->dev, NULL);
    959	if (IS_ERR(clk)) {
    960		dev_err(&pdev->dev, "unable to find controller clock\n");
    961		return PTR_ERR(clk);
    962	}
    963
    964	id = of_alias_get_id(pdev->dev.of_node, "serial");
    965	if (id < 0) {
    966		dev_err(&pdev->dev, "missing aliases entry\n");
    967		return id;
    968	}
    969
    970#ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
    971	if (id > SIFIVE_SERIAL_MAX_PORTS) {
    972		dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
    973		return -EINVAL;
    974	}
    975#endif
    976
    977	ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
    978	if (!ssp)
    979		return -ENOMEM;
    980
    981	ssp->port.dev = &pdev->dev;
    982	ssp->port.type = PORT_SIFIVE_V0;
    983	ssp->port.iotype = UPIO_MEM;
    984	ssp->port.irq = irq;
    985	ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
    986	ssp->port.ops = &sifive_serial_uops;
    987	ssp->port.line = id;
    988	ssp->port.mapbase = mem->start;
    989	ssp->port.membase = base;
    990	ssp->dev = &pdev->dev;
    991	ssp->clk = clk;
    992	ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
    993
    994	r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
    995	if (r) {
    996		dev_err(&pdev->dev, "could not register clock notifier: %d\n",
    997			r);
    998		goto probe_out1;
    999	}
   1000
   1001	/* Set up clock divider */
   1002	ssp->port.uartclk = clk_get_rate(ssp->clk);
   1003	ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
   1004	__ssp_update_div(ssp);
   1005
   1006	platform_set_drvdata(pdev, ssp);
   1007
   1008	/* Enable transmits and set the watermark level to 1 */
   1009	__ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
   1010		     SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
   1011		     SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
   1012
   1013	/* Enable receives and set the watermark level to 0 */
   1014	__ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
   1015		     SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
   1016		     SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
   1017
   1018	r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
   1019			dev_name(&pdev->dev), ssp);
   1020	if (r) {
   1021		dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
   1022		goto probe_out2;
   1023	}
   1024
   1025	__ssp_add_console_port(ssp);
   1026
   1027	r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
   1028	if (r != 0) {
   1029		dev_err(&pdev->dev, "could not add uart: %d\n", r);
   1030		goto probe_out3;
   1031	}
   1032
   1033	return 0;
   1034
   1035probe_out3:
   1036	__ssp_remove_console_port(ssp);
   1037	free_irq(ssp->port.irq, ssp);
   1038probe_out2:
   1039	clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
   1040probe_out1:
   1041	return r;
   1042}
   1043
   1044static int sifive_serial_remove(struct platform_device *dev)
   1045{
   1046	struct sifive_serial_port *ssp = platform_get_drvdata(dev);
   1047
   1048	__ssp_remove_console_port(ssp);
   1049	uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
   1050	free_irq(ssp->port.irq, ssp);
   1051	clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
   1052
   1053	return 0;
   1054}
   1055
   1056static const struct of_device_id sifive_serial_of_match[] = {
   1057	{ .compatible = "sifive,fu540-c000-uart0" },
   1058	{ .compatible = "sifive,uart0" },
   1059	{},
   1060};
   1061MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
   1062
   1063static struct platform_driver sifive_serial_platform_driver = {
   1064	.probe		= sifive_serial_probe,
   1065	.remove		= sifive_serial_remove,
   1066	.driver		= {
   1067		.name	= SIFIVE_SERIAL_NAME,
   1068		.of_match_table = of_match_ptr(sifive_serial_of_match),
   1069	},
   1070};
   1071
   1072static int __init sifive_serial_init(void)
   1073{
   1074	int r;
   1075
   1076	r = uart_register_driver(&sifive_serial_uart_driver);
   1077	if (r)
   1078		goto init_out1;
   1079
   1080	r = platform_driver_register(&sifive_serial_platform_driver);
   1081	if (r)
   1082		goto init_out2;
   1083
   1084	return 0;
   1085
   1086init_out2:
   1087	uart_unregister_driver(&sifive_serial_uart_driver);
   1088init_out1:
   1089	return r;
   1090}
   1091
   1092static void __exit sifive_serial_exit(void)
   1093{
   1094	platform_driver_unregister(&sifive_serial_platform_driver);
   1095	uart_unregister_driver(&sifive_serial_uart_driver);
   1096}
   1097
   1098module_init(sifive_serial_init);
   1099module_exit(sifive_serial_exit);
   1100
   1101MODULE_DESCRIPTION("SiFive UART serial driver");
   1102MODULE_LICENSE("GPL");
   1103MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");