io_uring: ensure finish_wait() is always called in __io_uring_task_cancel()
[linux/fpc-iii.git] / drivers / tty / serial / sifive.c
blob1066eebe3b28b046deb69348b1357c77eed90185
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
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.
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.
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
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/
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.
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>
58 * Register offsets
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)
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)
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)
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)
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)
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)
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)
111 * Config macros
115 * SIFIVE_SERIAL_MAX_PORTS: maximum number of UARTs on a device that can
116 * host a serial console
118 #define SIFIVE_SERIAL_MAX_PORTS 8
121 * SIFIVE_DEFAULT_BAUD_RATE: default baud rate that the driver should
122 * configure itself to use
124 #define SIFIVE_DEFAULT_BAUD_RATE 115200
126 /* SIFIVE_SERIAL_NAME: our driver's name that we pass to the operating system */
127 #define SIFIVE_SERIAL_NAME "sifive-serial"
129 /* SIFIVE_TTY_PREFIX: tty name prefix for SiFive serial ports */
130 #define SIFIVE_TTY_PREFIX "ttySIF"
132 /* SIFIVE_TX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
133 #define SIFIVE_TX_FIFO_DEPTH 8
135 /* SIFIVE_RX_FIFO_DEPTH: depth of the TX FIFO (in bytes) */
136 #define SIFIVE_RX_FIFO_DEPTH 8
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
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 * @clkin_rate: input clock to the UART IP block.
152 * @baud_rate: UART serial line rate (e.g., 115200 baud)
153 * @clk: reference to this device's clock
154 * @clk_notifier: clock rate change notifier for upstream clock changes
156 * Configuration data specific to this SiFive UART.
158 struct sifive_serial_port {
159 struct uart_port port;
160 struct device *dev;
161 unsigned char ier;
162 unsigned long clkin_rate;
163 unsigned long baud_rate;
164 struct clk *clk;
165 struct notifier_block clk_notifier;
169 * Structure container-of macros
172 #define port_to_sifive_serial_port(p) (container_of((p), \
173 struct sifive_serial_port, \
174 port))
176 #define notifier_to_sifive_serial_port(nb) (container_of((nb), \
177 struct sifive_serial_port, \
178 clk_notifier))
181 * Forward declarations
183 static void sifive_serial_stop_tx(struct uart_port *port);
186 * Internal functions
190 * __ssp_early_writel() - write to a SiFive serial port register (early)
191 * @port: pointer to a struct uart_port record
192 * @offs: register address offset from the IP block base address
193 * @v: value to write to the register
195 * Given a pointer @port to a struct uart_port record, write the value
196 * @v to the IP block register address offset @offs. This function is
197 * intended for early console use.
199 * Context: Intended to be used only by the earlyconsole code.
201 static void __ssp_early_writel(u32 v, u16 offs, struct uart_port *port)
203 writel_relaxed(v, port->membase + offs);
207 * __ssp_early_readl() - read from a SiFive serial port register (early)
208 * @port: pointer to a struct uart_port record
209 * @offs: register address offset from the IP block base address
211 * Given a pointer @port to a struct uart_port record, read the
212 * contents of the IP block register located at offset @offs from the
213 * IP block base and return it. This function is intended for early
214 * console use.
216 * Context: Intended to be called only by the earlyconsole code or by
217 * __ssp_readl() or __ssp_writel() (in this driver)
219 * Returns: the register value read from the UART.
221 static u32 __ssp_early_readl(struct uart_port *port, u16 offs)
223 return readl_relaxed(port->membase + offs);
227 * __ssp_writel() - write to a SiFive serial port register
228 * @v: value to write to the register
229 * @offs: register address offset from the IP block base address
230 * @ssp: pointer to a struct sifive_serial_port record
232 * Write the value @v to the IP block register located at offset @offs from the
233 * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
235 * Context: Any context.
237 static void __ssp_writel(u32 v, u16 offs, struct sifive_serial_port *ssp)
239 __ssp_early_writel(v, offs, &ssp->port);
243 * __ssp_readl() - read from a SiFive serial port register
244 * @ssp: pointer to a struct sifive_serial_port record
245 * @offs: register address offset from the IP block base address
247 * Read the contents of the IP block register located at offset @offs from the
248 * IP block base, given a pointer @ssp to a struct sifive_serial_port record.
250 * Context: Any context.
252 * Returns: the value of the UART register
254 static u32 __ssp_readl(struct sifive_serial_port *ssp, u16 offs)
256 return __ssp_early_readl(&ssp->port, offs);
260 * sifive_serial_is_txfifo_full() - is the TXFIFO full?
261 * @ssp: pointer to a struct sifive_serial_port
263 * Read the transmit FIFO "full" bit, returning a non-zero value if the
264 * TX FIFO is full, or zero if space remains. Intended to be used to prevent
265 * writes to the TX FIFO when it's full.
267 * Returns: SIFIVE_SERIAL_TXDATA_FULL_MASK (non-zero) if the transmit FIFO
268 * is full, or 0 if space remains.
270 static int sifive_serial_is_txfifo_full(struct sifive_serial_port *ssp)
272 return __ssp_readl(ssp, SIFIVE_SERIAL_TXDATA_OFFS) &
273 SIFIVE_SERIAL_TXDATA_FULL_MASK;
277 * __ssp_transmit_char() - enqueue a byte to transmit onto the TX FIFO
278 * @ssp: pointer to a struct sifive_serial_port
279 * @ch: character to transmit
281 * Enqueue a byte @ch onto the transmit FIFO, given a pointer @ssp to the
282 * struct sifive_serial_port * to transmit on. Caller should first check to
283 * ensure that the TXFIFO has space; see sifive_serial_is_txfifo_full().
285 * Context: Any context.
287 static void __ssp_transmit_char(struct sifive_serial_port *ssp, int ch)
289 __ssp_writel(ch, SIFIVE_SERIAL_TXDATA_OFFS, ssp);
293 * __ssp_transmit_chars() - enqueue multiple bytes onto the TX FIFO
294 * @ssp: pointer to a struct sifive_serial_port
296 * Transfer up to a TX FIFO size's worth of characters from the Linux serial
297 * transmit buffer to the SiFive UART TX FIFO.
299 * Context: Any context. Expects @ssp->port.lock to be held by caller.
301 static void __ssp_transmit_chars(struct sifive_serial_port *ssp)
303 struct circ_buf *xmit = &ssp->port.state->xmit;
304 int count;
306 if (ssp->port.x_char) {
307 __ssp_transmit_char(ssp, ssp->port.x_char);
308 ssp->port.icount.tx++;
309 ssp->port.x_char = 0;
310 return;
312 if (uart_circ_empty(xmit) || uart_tx_stopped(&ssp->port)) {
313 sifive_serial_stop_tx(&ssp->port);
314 return;
316 count = SIFIVE_TX_FIFO_DEPTH;
317 do {
318 __ssp_transmit_char(ssp, xmit->buf[xmit->tail]);
319 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
320 ssp->port.icount.tx++;
321 if (uart_circ_empty(xmit))
322 break;
323 } while (--count > 0);
325 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
326 uart_write_wakeup(&ssp->port);
328 if (uart_circ_empty(xmit))
329 sifive_serial_stop_tx(&ssp->port);
333 * __ssp_enable_txwm() - enable transmit watermark interrupts
334 * @ssp: pointer to a struct sifive_serial_port
336 * Enable interrupt generation when the transmit FIFO watermark is reached
337 * on the SiFive UART referred to by @ssp.
339 static void __ssp_enable_txwm(struct sifive_serial_port *ssp)
341 if (ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK)
342 return;
344 ssp->ier |= SIFIVE_SERIAL_IE_TXWM_MASK;
345 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
349 * __ssp_enable_rxwm() - enable receive watermark interrupts
350 * @ssp: pointer to a struct sifive_serial_port
352 * Enable interrupt generation when the receive FIFO watermark is reached
353 * on the SiFive UART referred to by @ssp.
355 static void __ssp_enable_rxwm(struct sifive_serial_port *ssp)
357 if (ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK)
358 return;
360 ssp->ier |= SIFIVE_SERIAL_IE_RXWM_MASK;
361 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
365 * __ssp_disable_txwm() - disable transmit watermark interrupts
366 * @ssp: pointer to a struct sifive_serial_port
368 * Disable interrupt generation when the transmit FIFO watermark is reached
369 * on the UART referred to by @ssp.
371 static void __ssp_disable_txwm(struct sifive_serial_port *ssp)
373 if (!(ssp->ier & SIFIVE_SERIAL_IE_TXWM_MASK))
374 return;
376 ssp->ier &= ~SIFIVE_SERIAL_IE_TXWM_MASK;
377 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
381 * __ssp_disable_rxwm() - disable receive watermark interrupts
382 * @ssp: pointer to a struct sifive_serial_port
384 * Disable interrupt generation when the receive FIFO watermark is reached
385 * on the UART referred to by @ssp.
387 static void __ssp_disable_rxwm(struct sifive_serial_port *ssp)
389 if (!(ssp->ier & SIFIVE_SERIAL_IE_RXWM_MASK))
390 return;
392 ssp->ier &= ~SIFIVE_SERIAL_IE_RXWM_MASK;
393 __ssp_writel(ssp->ier, SIFIVE_SERIAL_IE_OFFS, ssp);
397 * __ssp_receive_char() - receive a byte from the UART
398 * @ssp: pointer to a struct sifive_serial_port
399 * @is_empty: char pointer to return whether the RX FIFO is empty
401 * Try to read a byte from the SiFive UART RX FIFO, referenced by
402 * @ssp, and to return it. Also returns the RX FIFO empty bit in
403 * the char pointed to by @ch. The caller must pass the byte back to the
404 * Linux serial layer if needed.
406 * Returns: the byte read from the UART RX FIFO.
408 static char __ssp_receive_char(struct sifive_serial_port *ssp, char *is_empty)
410 u32 v;
411 u8 ch;
413 v = __ssp_readl(ssp, SIFIVE_SERIAL_RXDATA_OFFS);
415 if (!is_empty)
416 WARN_ON(1);
417 else
418 *is_empty = (v & SIFIVE_SERIAL_RXDATA_EMPTY_MASK) >>
419 SIFIVE_SERIAL_RXDATA_EMPTY_SHIFT;
421 ch = (v & SIFIVE_SERIAL_RXDATA_DATA_MASK) >>
422 SIFIVE_SERIAL_RXDATA_DATA_SHIFT;
424 return ch;
428 * __ssp_receive_chars() - receive multiple bytes from the UART
429 * @ssp: pointer to a struct sifive_serial_port
431 * Receive up to an RX FIFO's worth of bytes from the SiFive UART referred
432 * to by @ssp and pass them up to the Linux serial layer.
434 * Context: Expects ssp->port.lock to be held by caller.
436 static void __ssp_receive_chars(struct sifive_serial_port *ssp)
438 unsigned char ch;
439 char is_empty;
440 int c;
442 for (c = SIFIVE_RX_FIFO_DEPTH; c > 0; --c) {
443 ch = __ssp_receive_char(ssp, &is_empty);
444 if (is_empty)
445 break;
447 ssp->port.icount.rx++;
448 uart_insert_char(&ssp->port, 0, 0, ch, TTY_NORMAL);
451 spin_unlock(&ssp->port.lock);
452 tty_flip_buffer_push(&ssp->port.state->port);
453 spin_lock(&ssp->port.lock);
457 * __ssp_update_div() - calculate the divisor setting by the line rate
458 * @ssp: pointer to a struct sifive_serial_port
460 * Calculate the appropriate value of the clock divisor for the UART
461 * and target line rate referred to by @ssp and write it into the
462 * hardware.
464 static void __ssp_update_div(struct sifive_serial_port *ssp)
466 u16 div;
468 div = DIV_ROUND_UP(ssp->clkin_rate, ssp->baud_rate) - 1;
470 __ssp_writel(div, SIFIVE_SERIAL_DIV_OFFS, ssp);
474 * __ssp_update_baud_rate() - set the UART "baud rate"
475 * @ssp: pointer to a struct sifive_serial_port
476 * @rate: new target bit rate
478 * Calculate the UART divisor value for the target bit rate @rate for the
479 * SiFive UART described by @ssp and program it into the UART. There may
480 * be some error between the target bit rate and the actual bit rate implemented
481 * by the UART due to clock ratio granularity.
483 static void __ssp_update_baud_rate(struct sifive_serial_port *ssp,
484 unsigned int rate)
486 if (ssp->baud_rate == rate)
487 return;
489 ssp->baud_rate = rate;
490 __ssp_update_div(ssp);
494 * __ssp_set_stop_bits() - set the number of stop bits
495 * @ssp: pointer to a struct sifive_serial_port
496 * @nstop: 1 or 2 (stop bits)
498 * Program the SiFive UART referred to by @ssp to use @nstop stop bits.
500 static void __ssp_set_stop_bits(struct sifive_serial_port *ssp, char nstop)
502 u32 v;
504 if (nstop < 1 || nstop > 2) {
505 WARN_ON(1);
506 return;
509 v = __ssp_readl(ssp, SIFIVE_SERIAL_TXCTRL_OFFS);
510 v &= ~SIFIVE_SERIAL_TXCTRL_NSTOP_MASK;
511 v |= (nstop - 1) << SIFIVE_SERIAL_TXCTRL_NSTOP_SHIFT;
512 __ssp_writel(v, SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
516 * __ssp_wait_for_xmitr() - wait for an empty slot on the TX FIFO
517 * @ssp: pointer to a struct sifive_serial_port
519 * Delay while the UART TX FIFO referred to by @ssp is marked as full.
521 * Context: Any context.
523 static void __maybe_unused __ssp_wait_for_xmitr(struct sifive_serial_port *ssp)
525 while (sifive_serial_is_txfifo_full(ssp))
526 udelay(1); /* XXX Could probably be more intelligent here */
530 * Linux serial API functions
533 static void sifive_serial_stop_tx(struct uart_port *port)
535 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
537 __ssp_disable_txwm(ssp);
540 static void sifive_serial_stop_rx(struct uart_port *port)
542 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
544 __ssp_disable_rxwm(ssp);
547 static void sifive_serial_start_tx(struct uart_port *port)
549 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
551 __ssp_enable_txwm(ssp);
554 static irqreturn_t sifive_serial_irq(int irq, void *dev_id)
556 struct sifive_serial_port *ssp = dev_id;
557 u32 ip;
559 spin_lock(&ssp->port.lock);
561 ip = __ssp_readl(ssp, SIFIVE_SERIAL_IP_OFFS);
562 if (!ip) {
563 spin_unlock(&ssp->port.lock);
564 return IRQ_NONE;
567 if (ip & SIFIVE_SERIAL_IP_RXWM_MASK)
568 __ssp_receive_chars(ssp);
569 if (ip & SIFIVE_SERIAL_IP_TXWM_MASK)
570 __ssp_transmit_chars(ssp);
572 spin_unlock(&ssp->port.lock);
574 return IRQ_HANDLED;
577 static unsigned int sifive_serial_tx_empty(struct uart_port *port)
579 return TIOCSER_TEMT;
582 static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
584 return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
587 static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
589 /* IP block does not support these signals */
592 static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
594 /* IP block does not support sending a break */
597 static int sifive_serial_startup(struct uart_port *port)
599 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
601 __ssp_enable_rxwm(ssp);
603 return 0;
606 static void sifive_serial_shutdown(struct uart_port *port)
608 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
610 __ssp_disable_rxwm(ssp);
611 __ssp_disable_txwm(ssp);
615 * sifive_serial_clk_notifier() - clock post-rate-change notifier
616 * @nb: pointer to the struct notifier_block, from the notifier code
617 * @event: event mask from the notifier code
618 * @data: pointer to the struct clk_notifier_data from the notifier code
620 * On the V0 SoC, the UART IP block is derived from the CPU clock source
621 * after a synchronous divide-by-two divider, so any CPU clock rate change
622 * requires the UART baud rate to be updated. This presumably corrupts any
623 * serial word currently being transmitted or received. In order to avoid
624 * corrupting the output data stream, we drain the transmit queue before
625 * allowing the clock's rate to be changed.
627 static int sifive_serial_clk_notifier(struct notifier_block *nb,
628 unsigned long event, void *data)
630 struct clk_notifier_data *cnd = data;
631 struct sifive_serial_port *ssp = notifier_to_sifive_serial_port(nb);
633 if (event == PRE_RATE_CHANGE) {
635 * The TX watermark is always set to 1 by this driver, which
636 * means that the TX busy bit will lower when there are 0 bytes
637 * left in the TX queue -- in other words, when the TX FIFO is
638 * empty.
640 __ssp_wait_for_xmitr(ssp);
642 * On the cycle the TX FIFO goes empty there is still a full
643 * UART frame left to be transmitted in the shift register.
644 * The UART provides no way for software to directly determine
645 * when that last frame has been transmitted, so we just sleep
646 * here instead. As we're not tracking the number of stop bits
647 * they're just worst cased here. The rest of the serial
648 * framing parameters aren't configurable by software.
650 udelay(DIV_ROUND_UP(12 * 1000 * 1000, ssp->baud_rate));
653 if (event == POST_RATE_CHANGE && ssp->clkin_rate != cnd->new_rate) {
654 ssp->clkin_rate = cnd->new_rate;
655 __ssp_update_div(ssp);
658 return NOTIFY_OK;
661 static void sifive_serial_set_termios(struct uart_port *port,
662 struct ktermios *termios,
663 struct ktermios *old)
665 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
666 unsigned long flags;
667 u32 v, old_v;
668 int rate;
669 char nstop;
671 if ((termios->c_cflag & CSIZE) != CS8)
672 dev_err_once(ssp->port.dev, "only 8-bit words supported\n");
673 if (termios->c_iflag & (INPCK | PARMRK))
674 dev_err_once(ssp->port.dev, "parity checking not supported\n");
675 if (termios->c_iflag & BRKINT)
676 dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
678 /* Set number of stop bits */
679 nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
680 __ssp_set_stop_bits(ssp, nstop);
682 /* Set line rate */
683 rate = uart_get_baud_rate(port, termios, old, 0, ssp->clkin_rate / 16);
684 __ssp_update_baud_rate(ssp, rate);
686 spin_lock_irqsave(&ssp->port.lock, flags);
688 /* Update the per-port timeout */
689 uart_update_timeout(port, termios->c_cflag, rate);
691 ssp->port.read_status_mask = 0;
693 /* Ignore all characters if CREAD is not set */
694 v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
695 old_v = v;
696 if ((termios->c_cflag & CREAD) == 0)
697 v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
698 else
699 v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
700 if (v != old_v)
701 __ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
703 spin_unlock_irqrestore(&ssp->port.lock, flags);
706 static void sifive_serial_release_port(struct uart_port *port)
710 static int sifive_serial_request_port(struct uart_port *port)
712 return 0;
715 static void sifive_serial_config_port(struct uart_port *port, int flags)
717 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
719 ssp->port.type = PORT_SIFIVE_V0;
722 static int sifive_serial_verify_port(struct uart_port *port,
723 struct serial_struct *ser)
725 return -EINVAL;
728 static const char *sifive_serial_type(struct uart_port *port)
730 return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
733 #ifdef CONFIG_CONSOLE_POLL
734 static int sifive_serial_poll_get_char(struct uart_port *port)
736 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
737 char is_empty, ch;
739 ch = __ssp_receive_char(ssp, &is_empty);
740 if (is_empty)
741 return NO_POLL_CHAR;
743 return ch;
746 static void sifive_serial_poll_put_char(struct uart_port *port,
747 unsigned char c)
749 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
751 __ssp_wait_for_xmitr(ssp);
752 __ssp_transmit_char(ssp, c);
754 #endif /* CONFIG_CONSOLE_POLL */
757 * Early console support
760 #ifdef CONFIG_SERIAL_EARLYCON
761 static void early_sifive_serial_putc(struct uart_port *port, int c)
763 while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
764 SIFIVE_SERIAL_TXDATA_FULL_MASK)
765 cpu_relax();
767 __ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
770 static void early_sifive_serial_write(struct console *con, const char *s,
771 unsigned int n)
773 struct earlycon_device *dev = con->data;
774 struct uart_port *port = &dev->port;
776 uart_console_write(port, s, n, early_sifive_serial_putc);
779 static int __init early_sifive_serial_setup(struct earlycon_device *dev,
780 const char *options)
782 struct uart_port *port = &dev->port;
784 if (!port->membase)
785 return -ENODEV;
787 dev->con->write = early_sifive_serial_write;
789 return 0;
792 OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
793 OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0",
794 early_sifive_serial_setup);
795 #endif /* CONFIG_SERIAL_EARLYCON */
798 * Linux console interface
801 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
803 static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
805 static void sifive_serial_console_putchar(struct uart_port *port, int ch)
807 struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
809 __ssp_wait_for_xmitr(ssp);
810 __ssp_transmit_char(ssp, ch);
813 static void sifive_serial_console_write(struct console *co, const char *s,
814 unsigned int count)
816 struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
817 unsigned long flags;
818 unsigned int ier;
819 int locked = 1;
821 if (!ssp)
822 return;
824 local_irq_save(flags);
825 if (ssp->port.sysrq)
826 locked = 0;
827 else if (oops_in_progress)
828 locked = spin_trylock(&ssp->port.lock);
829 else
830 spin_lock(&ssp->port.lock);
832 ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
833 __ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
835 uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
837 __ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
839 if (locked)
840 spin_unlock(&ssp->port.lock);
841 local_irq_restore(flags);
844 static int __init sifive_serial_console_setup(struct console *co, char *options)
846 struct sifive_serial_port *ssp;
847 int baud = SIFIVE_DEFAULT_BAUD_RATE;
848 int bits = 8;
849 int parity = 'n';
850 int flow = 'n';
852 if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
853 return -ENODEV;
855 ssp = sifive_serial_console_ports[co->index];
856 if (!ssp)
857 return -ENODEV;
859 if (options)
860 uart_parse_options(options, &baud, &parity, &bits, &flow);
862 return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
865 static struct uart_driver sifive_serial_uart_driver;
867 static struct console sifive_serial_console = {
868 .name = SIFIVE_TTY_PREFIX,
869 .write = sifive_serial_console_write,
870 .device = uart_console_device,
871 .setup = sifive_serial_console_setup,
872 .flags = CON_PRINTBUFFER,
873 .index = -1,
874 .data = &sifive_serial_uart_driver,
877 static int __init sifive_console_init(void)
879 register_console(&sifive_serial_console);
880 return 0;
883 console_initcall(sifive_console_init);
885 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
887 sifive_serial_console_ports[ssp->port.line] = ssp;
890 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
892 sifive_serial_console_ports[ssp->port.line] = 0;
895 #define SIFIVE_SERIAL_CONSOLE (&sifive_serial_console)
897 #else
899 #define SIFIVE_SERIAL_CONSOLE NULL
901 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
903 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
906 #endif
908 static const struct uart_ops sifive_serial_uops = {
909 .tx_empty = sifive_serial_tx_empty,
910 .set_mctrl = sifive_serial_set_mctrl,
911 .get_mctrl = sifive_serial_get_mctrl,
912 .stop_tx = sifive_serial_stop_tx,
913 .start_tx = sifive_serial_start_tx,
914 .stop_rx = sifive_serial_stop_rx,
915 .break_ctl = sifive_serial_break_ctl,
916 .startup = sifive_serial_startup,
917 .shutdown = sifive_serial_shutdown,
918 .set_termios = sifive_serial_set_termios,
919 .type = sifive_serial_type,
920 .release_port = sifive_serial_release_port,
921 .request_port = sifive_serial_request_port,
922 .config_port = sifive_serial_config_port,
923 .verify_port = sifive_serial_verify_port,
924 #ifdef CONFIG_CONSOLE_POLL
925 .poll_get_char = sifive_serial_poll_get_char,
926 .poll_put_char = sifive_serial_poll_put_char,
927 #endif
930 static struct uart_driver sifive_serial_uart_driver = {
931 .owner = THIS_MODULE,
932 .driver_name = SIFIVE_SERIAL_NAME,
933 .dev_name = SIFIVE_TTY_PREFIX,
934 .nr = SIFIVE_SERIAL_MAX_PORTS,
935 .cons = SIFIVE_SERIAL_CONSOLE,
938 static int sifive_serial_probe(struct platform_device *pdev)
940 struct sifive_serial_port *ssp;
941 struct resource *mem;
942 struct clk *clk;
943 void __iomem *base;
944 int irq, id, r;
946 irq = platform_get_irq(pdev, 0);
947 if (irq < 0)
948 return -EPROBE_DEFER;
950 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
951 base = devm_ioremap_resource(&pdev->dev, mem);
952 if (IS_ERR(base)) {
953 dev_err(&pdev->dev, "could not acquire device memory\n");
954 return PTR_ERR(base);
957 clk = devm_clk_get(&pdev->dev, NULL);
958 if (IS_ERR(clk)) {
959 dev_err(&pdev->dev, "unable to find controller clock\n");
960 return PTR_ERR(clk);
963 id = of_alias_get_id(pdev->dev.of_node, "serial");
964 if (id < 0) {
965 dev_err(&pdev->dev, "missing aliases entry\n");
966 return id;
969 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
970 if (id > SIFIVE_SERIAL_MAX_PORTS) {
971 dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
972 return -EINVAL;
974 #endif
976 ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
977 if (!ssp)
978 return -ENOMEM;
980 ssp->port.dev = &pdev->dev;
981 ssp->port.type = PORT_SIFIVE_V0;
982 ssp->port.iotype = UPIO_MEM;
983 ssp->port.irq = irq;
984 ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
985 ssp->port.ops = &sifive_serial_uops;
986 ssp->port.line = id;
987 ssp->port.mapbase = mem->start;
988 ssp->port.membase = base;
989 ssp->dev = &pdev->dev;
990 ssp->clk = clk;
991 ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
993 r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
994 if (r) {
995 dev_err(&pdev->dev, "could not register clock notifier: %d\n",
997 goto probe_out1;
1000 /* Set up clock divider */
1001 ssp->clkin_rate = clk_get_rate(ssp->clk);
1002 ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
1003 __ssp_update_div(ssp);
1005 platform_set_drvdata(pdev, ssp);
1007 /* Enable transmits and set the watermark level to 1 */
1008 __ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
1009 SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
1010 SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
1012 /* Enable receives and set the watermark level to 0 */
1013 __ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
1014 SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
1015 SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
1017 r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
1018 dev_name(&pdev->dev), ssp);
1019 if (r) {
1020 dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
1021 goto probe_out2;
1024 __ssp_add_console_port(ssp);
1026 r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
1027 if (r != 0) {
1028 dev_err(&pdev->dev, "could not add uart: %d\n", r);
1029 goto probe_out3;
1032 return 0;
1034 probe_out3:
1035 __ssp_remove_console_port(ssp);
1036 free_irq(ssp->port.irq, ssp);
1037 probe_out2:
1038 clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1039 probe_out1:
1040 return r;
1043 static int sifive_serial_remove(struct platform_device *dev)
1045 struct sifive_serial_port *ssp = platform_get_drvdata(dev);
1047 __ssp_remove_console_port(ssp);
1048 uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
1049 free_irq(ssp->port.irq, ssp);
1050 clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1052 return 0;
1055 static const struct of_device_id sifive_serial_of_match[] = {
1056 { .compatible = "sifive,fu540-c000-uart0" },
1057 { .compatible = "sifive,uart0" },
1060 MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
1062 static struct platform_driver sifive_serial_platform_driver = {
1063 .probe = sifive_serial_probe,
1064 .remove = sifive_serial_remove,
1065 .driver = {
1066 .name = SIFIVE_SERIAL_NAME,
1067 .of_match_table = of_match_ptr(sifive_serial_of_match),
1071 static int __init sifive_serial_init(void)
1073 int r;
1075 r = uart_register_driver(&sifive_serial_uart_driver);
1076 if (r)
1077 goto init_out1;
1079 r = platform_driver_register(&sifive_serial_platform_driver);
1080 if (r)
1081 goto init_out2;
1083 return 0;
1085 init_out2:
1086 uart_unregister_driver(&sifive_serial_uart_driver);
1087 init_out1:
1088 return r;
1091 static void __exit sifive_serial_exit(void)
1093 platform_driver_unregister(&sifive_serial_platform_driver);
1094 uart_unregister_driver(&sifive_serial_uart_driver);
1097 module_init(sifive_serial_init);
1098 module_exit(sifive_serial_exit);
1100 MODULE_DESCRIPTION("SiFive UART serial driver");
1101 MODULE_LICENSE("GPL");
1102 MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");