xref: /openbmc/linux/drivers/tty/serial/sifive.c (revision 7b0364ea)
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  */
157 struct 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  */
181 static 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  */
199 static 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  */
219 static 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  */
235 static 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  */
252 static 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  */
268 static 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  */
285 static 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  */
299 static 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  */
337 static 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  */
353 static 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  */
369 static 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  */
385 static 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  */
406 static 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  */
434 static 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  */
460 static 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  */
479 static 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  */
496 static 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  */
519 static 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 
529 static 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 
536 static 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 
543 static 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 
550 static 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 
573 static unsigned int sifive_serial_tx_empty(struct uart_port *port)
574 {
575 	return TIOCSER_TEMT;
576 }
577 
578 static unsigned int sifive_serial_get_mctrl(struct uart_port *port)
579 {
580 	return TIOCM_CAR | TIOCM_CTS | TIOCM_DSR;
581 }
582 
583 static void sifive_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
584 {
585 	/* IP block does not support these signals */
586 }
587 
588 static void sifive_serial_break_ctl(struct uart_port *port, int break_state)
589 {
590 	/* IP block does not support sending a break */
591 }
592 
593 static 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 
602 static 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  */
623 static 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 
657 static 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 	if (termios->c_iflag & (INPCK | PARMRK))
670 		dev_err_once(ssp->port.dev, "parity checking not supported\n");
671 	if (termios->c_iflag & BRKINT)
672 		dev_err_once(ssp->port.dev, "BREAK detection not supported\n");
673 
674 	/* Set number of stop bits */
675 	nstop = (termios->c_cflag & CSTOPB) ? 2 : 1;
676 	__ssp_set_stop_bits(ssp, nstop);
677 
678 	/* Set line rate */
679 	rate = uart_get_baud_rate(port, termios, old, 0,
680 				  ssp->port.uartclk / 16);
681 	__ssp_update_baud_rate(ssp, rate);
682 
683 	spin_lock_irqsave(&ssp->port.lock, flags);
684 
685 	/* Update the per-port timeout */
686 	uart_update_timeout(port, termios->c_cflag, rate);
687 
688 	ssp->port.read_status_mask = 0;
689 
690 	/* Ignore all characters if CREAD is not set */
691 	v = __ssp_readl(ssp, SIFIVE_SERIAL_RXCTRL_OFFS);
692 	old_v = v;
693 	if ((termios->c_cflag & CREAD) == 0)
694 		v &= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
695 	else
696 		v |= SIFIVE_SERIAL_RXCTRL_RXEN_MASK;
697 	if (v != old_v)
698 		__ssp_writel(v, SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
699 
700 	spin_unlock_irqrestore(&ssp->port.lock, flags);
701 }
702 
703 static void sifive_serial_release_port(struct uart_port *port)
704 {
705 }
706 
707 static int sifive_serial_request_port(struct uart_port *port)
708 {
709 	return 0;
710 }
711 
712 static void sifive_serial_config_port(struct uart_port *port, int flags)
713 {
714 	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
715 
716 	ssp->port.type = PORT_SIFIVE_V0;
717 }
718 
719 static int sifive_serial_verify_port(struct uart_port *port,
720 				     struct serial_struct *ser)
721 {
722 	return -EINVAL;
723 }
724 
725 static const char *sifive_serial_type(struct uart_port *port)
726 {
727 	return port->type == PORT_SIFIVE_V0 ? "SiFive UART v0" : NULL;
728 }
729 
730 #ifdef CONFIG_CONSOLE_POLL
731 static int sifive_serial_poll_get_char(struct uart_port *port)
732 {
733 	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
734 	char is_empty, ch;
735 
736 	ch = __ssp_receive_char(ssp, &is_empty);
737 	if (is_empty)
738 		return NO_POLL_CHAR;
739 
740 	return ch;
741 }
742 
743 static void sifive_serial_poll_put_char(struct uart_port *port,
744 					unsigned char c)
745 {
746 	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
747 
748 	__ssp_wait_for_xmitr(ssp);
749 	__ssp_transmit_char(ssp, c);
750 }
751 #endif /* CONFIG_CONSOLE_POLL */
752 
753 /*
754  * Early console support
755  */
756 
757 #ifdef CONFIG_SERIAL_EARLYCON
758 static void early_sifive_serial_putc(struct uart_port *port, unsigned char c)
759 {
760 	while (__ssp_early_readl(port, SIFIVE_SERIAL_TXDATA_OFFS) &
761 	       SIFIVE_SERIAL_TXDATA_FULL_MASK)
762 		cpu_relax();
763 
764 	__ssp_early_writel(c, SIFIVE_SERIAL_TXDATA_OFFS, port);
765 }
766 
767 static void early_sifive_serial_write(struct console *con, const char *s,
768 				      unsigned int n)
769 {
770 	struct earlycon_device *dev = con->data;
771 	struct uart_port *port = &dev->port;
772 
773 	uart_console_write(port, s, n, early_sifive_serial_putc);
774 }
775 
776 static int __init early_sifive_serial_setup(struct earlycon_device *dev,
777 					    const char *options)
778 {
779 	struct uart_port *port = &dev->port;
780 
781 	if (!port->membase)
782 		return -ENODEV;
783 
784 	dev->con->write = early_sifive_serial_write;
785 
786 	return 0;
787 }
788 
789 OF_EARLYCON_DECLARE(sifive, "sifive,uart0", early_sifive_serial_setup);
790 OF_EARLYCON_DECLARE(sifive, "sifive,fu540-c000-uart0",
791 		    early_sifive_serial_setup);
792 #endif /* CONFIG_SERIAL_EARLYCON */
793 
794 /*
795  * Linux console interface
796  */
797 
798 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
799 
800 static struct sifive_serial_port *sifive_serial_console_ports[SIFIVE_SERIAL_MAX_PORTS];
801 
802 static void sifive_serial_console_putchar(struct uart_port *port, unsigned char ch)
803 {
804 	struct sifive_serial_port *ssp = port_to_sifive_serial_port(port);
805 
806 	__ssp_wait_for_xmitr(ssp);
807 	__ssp_transmit_char(ssp, ch);
808 }
809 
810 static void sifive_serial_console_write(struct console *co, const char *s,
811 					unsigned int count)
812 {
813 	struct sifive_serial_port *ssp = sifive_serial_console_ports[co->index];
814 	unsigned long flags;
815 	unsigned int ier;
816 	int locked = 1;
817 
818 	if (!ssp)
819 		return;
820 
821 	local_irq_save(flags);
822 	if (ssp->port.sysrq)
823 		locked = 0;
824 	else if (oops_in_progress)
825 		locked = spin_trylock(&ssp->port.lock);
826 	else
827 		spin_lock(&ssp->port.lock);
828 
829 	ier = __ssp_readl(ssp, SIFIVE_SERIAL_IE_OFFS);
830 	__ssp_writel(0, SIFIVE_SERIAL_IE_OFFS, ssp);
831 
832 	uart_console_write(&ssp->port, s, count, sifive_serial_console_putchar);
833 
834 	__ssp_writel(ier, SIFIVE_SERIAL_IE_OFFS, ssp);
835 
836 	if (locked)
837 		spin_unlock(&ssp->port.lock);
838 	local_irq_restore(flags);
839 }
840 
841 static int __init sifive_serial_console_setup(struct console *co, char *options)
842 {
843 	struct sifive_serial_port *ssp;
844 	int baud = SIFIVE_DEFAULT_BAUD_RATE;
845 	int bits = 8;
846 	int parity = 'n';
847 	int flow = 'n';
848 
849 	if (co->index < 0 || co->index >= SIFIVE_SERIAL_MAX_PORTS)
850 		return -ENODEV;
851 
852 	ssp = sifive_serial_console_ports[co->index];
853 	if (!ssp)
854 		return -ENODEV;
855 
856 	if (options)
857 		uart_parse_options(options, &baud, &parity, &bits, &flow);
858 
859 	return uart_set_options(&ssp->port, co, baud, parity, bits, flow);
860 }
861 
862 static struct uart_driver sifive_serial_uart_driver;
863 
864 static struct console sifive_serial_console = {
865 	.name		= SIFIVE_TTY_PREFIX,
866 	.write		= sifive_serial_console_write,
867 	.device		= uart_console_device,
868 	.setup		= sifive_serial_console_setup,
869 	.flags		= CON_PRINTBUFFER,
870 	.index		= -1,
871 	.data		= &sifive_serial_uart_driver,
872 };
873 
874 static int __init sifive_console_init(void)
875 {
876 	register_console(&sifive_serial_console);
877 	return 0;
878 }
879 
880 console_initcall(sifive_console_init);
881 
882 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
883 {
884 	sifive_serial_console_ports[ssp->port.line] = ssp;
885 }
886 
887 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
888 {
889 	sifive_serial_console_ports[ssp->port.line] = NULL;
890 }
891 
892 #define SIFIVE_SERIAL_CONSOLE	(&sifive_serial_console)
893 
894 #else
895 
896 #define SIFIVE_SERIAL_CONSOLE	NULL
897 
898 static void __ssp_add_console_port(struct sifive_serial_port *ssp)
899 {}
900 static void __ssp_remove_console_port(struct sifive_serial_port *ssp)
901 {}
902 
903 #endif
904 
905 static const struct uart_ops sifive_serial_uops = {
906 	.tx_empty	= sifive_serial_tx_empty,
907 	.set_mctrl	= sifive_serial_set_mctrl,
908 	.get_mctrl	= sifive_serial_get_mctrl,
909 	.stop_tx	= sifive_serial_stop_tx,
910 	.start_tx	= sifive_serial_start_tx,
911 	.stop_rx	= sifive_serial_stop_rx,
912 	.break_ctl	= sifive_serial_break_ctl,
913 	.startup	= sifive_serial_startup,
914 	.shutdown	= sifive_serial_shutdown,
915 	.set_termios	= sifive_serial_set_termios,
916 	.type		= sifive_serial_type,
917 	.release_port	= sifive_serial_release_port,
918 	.request_port	= sifive_serial_request_port,
919 	.config_port	= sifive_serial_config_port,
920 	.verify_port	= sifive_serial_verify_port,
921 #ifdef CONFIG_CONSOLE_POLL
922 	.poll_get_char	= sifive_serial_poll_get_char,
923 	.poll_put_char	= sifive_serial_poll_put_char,
924 #endif
925 };
926 
927 static struct uart_driver sifive_serial_uart_driver = {
928 	.owner		= THIS_MODULE,
929 	.driver_name	= SIFIVE_SERIAL_NAME,
930 	.dev_name	= SIFIVE_TTY_PREFIX,
931 	.nr		= SIFIVE_SERIAL_MAX_PORTS,
932 	.cons		= SIFIVE_SERIAL_CONSOLE,
933 };
934 
935 static int sifive_serial_probe(struct platform_device *pdev)
936 {
937 	struct sifive_serial_port *ssp;
938 	struct resource *mem;
939 	struct clk *clk;
940 	void __iomem *base;
941 	int irq, id, r;
942 
943 	irq = platform_get_irq(pdev, 0);
944 	if (irq < 0)
945 		return -EPROBE_DEFER;
946 
947 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
948 	base = devm_ioremap_resource(&pdev->dev, mem);
949 	if (IS_ERR(base)) {
950 		dev_err(&pdev->dev, "could not acquire device memory\n");
951 		return PTR_ERR(base);
952 	}
953 
954 	clk = devm_clk_get(&pdev->dev, NULL);
955 	if (IS_ERR(clk)) {
956 		dev_err(&pdev->dev, "unable to find controller clock\n");
957 		return PTR_ERR(clk);
958 	}
959 
960 	id = of_alias_get_id(pdev->dev.of_node, "serial");
961 	if (id < 0) {
962 		dev_err(&pdev->dev, "missing aliases entry\n");
963 		return id;
964 	}
965 
966 #ifdef CONFIG_SERIAL_SIFIVE_CONSOLE
967 	if (id > SIFIVE_SERIAL_MAX_PORTS) {
968 		dev_err(&pdev->dev, "too many UARTs (%d)\n", id);
969 		return -EINVAL;
970 	}
971 #endif
972 
973 	ssp = devm_kzalloc(&pdev->dev, sizeof(*ssp), GFP_KERNEL);
974 	if (!ssp)
975 		return -ENOMEM;
976 
977 	ssp->port.dev = &pdev->dev;
978 	ssp->port.type = PORT_SIFIVE_V0;
979 	ssp->port.iotype = UPIO_MEM;
980 	ssp->port.irq = irq;
981 	ssp->port.fifosize = SIFIVE_TX_FIFO_DEPTH;
982 	ssp->port.ops = &sifive_serial_uops;
983 	ssp->port.line = id;
984 	ssp->port.mapbase = mem->start;
985 	ssp->port.membase = base;
986 	ssp->dev = &pdev->dev;
987 	ssp->clk = clk;
988 	ssp->clk_notifier.notifier_call = sifive_serial_clk_notifier;
989 
990 	r = clk_notifier_register(ssp->clk, &ssp->clk_notifier);
991 	if (r) {
992 		dev_err(&pdev->dev, "could not register clock notifier: %d\n",
993 			r);
994 		goto probe_out1;
995 	}
996 
997 	/* Set up clock divider */
998 	ssp->port.uartclk = clk_get_rate(ssp->clk);
999 	ssp->baud_rate = SIFIVE_DEFAULT_BAUD_RATE;
1000 	__ssp_update_div(ssp);
1001 
1002 	platform_set_drvdata(pdev, ssp);
1003 
1004 	/* Enable transmits and set the watermark level to 1 */
1005 	__ssp_writel((1 << SIFIVE_SERIAL_TXCTRL_TXCNT_SHIFT) |
1006 		     SIFIVE_SERIAL_TXCTRL_TXEN_MASK,
1007 		     SIFIVE_SERIAL_TXCTRL_OFFS, ssp);
1008 
1009 	/* Enable receives and set the watermark level to 0 */
1010 	__ssp_writel((0 << SIFIVE_SERIAL_RXCTRL_RXCNT_SHIFT) |
1011 		     SIFIVE_SERIAL_RXCTRL_RXEN_MASK,
1012 		     SIFIVE_SERIAL_RXCTRL_OFFS, ssp);
1013 
1014 	r = request_irq(ssp->port.irq, sifive_serial_irq, ssp->port.irqflags,
1015 			dev_name(&pdev->dev), ssp);
1016 	if (r) {
1017 		dev_err(&pdev->dev, "could not attach interrupt: %d\n", r);
1018 		goto probe_out2;
1019 	}
1020 
1021 	__ssp_add_console_port(ssp);
1022 
1023 	r = uart_add_one_port(&sifive_serial_uart_driver, &ssp->port);
1024 	if (r != 0) {
1025 		dev_err(&pdev->dev, "could not add uart: %d\n", r);
1026 		goto probe_out3;
1027 	}
1028 
1029 	return 0;
1030 
1031 probe_out3:
1032 	__ssp_remove_console_port(ssp);
1033 	free_irq(ssp->port.irq, ssp);
1034 probe_out2:
1035 	clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1036 probe_out1:
1037 	return r;
1038 }
1039 
1040 static int sifive_serial_remove(struct platform_device *dev)
1041 {
1042 	struct sifive_serial_port *ssp = platform_get_drvdata(dev);
1043 
1044 	__ssp_remove_console_port(ssp);
1045 	uart_remove_one_port(&sifive_serial_uart_driver, &ssp->port);
1046 	free_irq(ssp->port.irq, ssp);
1047 	clk_notifier_unregister(ssp->clk, &ssp->clk_notifier);
1048 
1049 	return 0;
1050 }
1051 
1052 static const struct of_device_id sifive_serial_of_match[] = {
1053 	{ .compatible = "sifive,fu540-c000-uart0" },
1054 	{ .compatible = "sifive,uart0" },
1055 	{},
1056 };
1057 MODULE_DEVICE_TABLE(of, sifive_serial_of_match);
1058 
1059 static struct platform_driver sifive_serial_platform_driver = {
1060 	.probe		= sifive_serial_probe,
1061 	.remove		= sifive_serial_remove,
1062 	.driver		= {
1063 		.name	= SIFIVE_SERIAL_NAME,
1064 		.of_match_table = of_match_ptr(sifive_serial_of_match),
1065 	},
1066 };
1067 
1068 static int __init sifive_serial_init(void)
1069 {
1070 	int r;
1071 
1072 	r = uart_register_driver(&sifive_serial_uart_driver);
1073 	if (r)
1074 		goto init_out1;
1075 
1076 	r = platform_driver_register(&sifive_serial_platform_driver);
1077 	if (r)
1078 		goto init_out2;
1079 
1080 	return 0;
1081 
1082 init_out2:
1083 	uart_unregister_driver(&sifive_serial_uart_driver);
1084 init_out1:
1085 	return r;
1086 }
1087 
1088 static void __exit sifive_serial_exit(void)
1089 {
1090 	platform_driver_unregister(&sifive_serial_platform_driver);
1091 	uart_unregister_driver(&sifive_serial_uart_driver);
1092 }
1093 
1094 module_init(sifive_serial_init);
1095 module_exit(sifive_serial_exit);
1096 
1097 MODULE_DESCRIPTION("SiFive UART serial driver");
1098 MODULE_LICENSE("GPL");
1099 MODULE_AUTHOR("Paul Walmsley <paul@pwsan.com>");
1100