2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2019 Axiado Corporation
7 * This software was developed in part by Kristof Provost under contract for
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
42 #include <machine/bus.h>
43 #include <machine/cpu.h>
45 #include <dev/extres/clk/clk.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/ofw/openfirm.h>
51 #include <dev/uart/uart.h>
52 #include <dev/uart/uart_bus.h>
53 #include <dev/uart/uart_cpu.h>
54 #include <dev/uart/uart_cpu_fdt.h>
58 #define SFUART_TXDATA 0x00
59 #define SFUART_TXDATA_FULL (1 << 31)
60 #define SFUART_RXDATA 0x04
61 #define SFUART_RXDATA_EMPTY (1 << 31)
62 #define SFUART_TXCTRL 0x08
63 #define SFUART_TXCTRL_ENABLE 0x01
64 #define SFUART_TXCTRL_NSTOP 0x02
65 #define SFUART_TXCTRL_TXCNT 0x70000
66 #define SFUART_TXCTRL_TXCNT_SHIFT 16
67 #define SFUART_RXCTRL 0x0c
68 #define SFUART_RXCTRL_ENABLE 0x01
69 #define SFUART_RXCTRL_RXCNT 0x70000
70 #define SFUART_RXCTRL_RXCNT_SHIFT 16
71 #define SFUART_IRQ_ENABLE 0x10
72 #define SFUART_IRQ_ENABLE_TXWM 0x01
73 #define SFUART_IRQ_ENABLE_RXWM 0x02
74 #define SFUART_IRQ_PENDING 0x14
75 #define SFUART_IRQ_PENDING_TXWM 0x01
76 #define SFUART_IRQ_PENDING_RXQM 0x02
77 #define SFUART_DIV 0x18
78 #define SFUART_REGS_SIZE 0x1c
80 #define SFUART_RX_FIFO_DEPTH 8
81 #define SFUART_TX_FIFO_DEPTH 8
84 struct uart_softc uart_softc;
89 sfuart_probe(struct uart_bas *bas)
98 sfuart_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
103 uart_setreg(bas, SFUART_IRQ_ENABLE, 0);
105 /* Enable RX and configure the watermark so that we get an interrupt
106 * when a single character arrives (if interrupts are enabled). */
107 reg = SFUART_RXCTRL_ENABLE;
108 reg |= (0 << SFUART_RXCTRL_RXCNT_SHIFT);
109 uart_setreg(bas, SFUART_RXCTRL, reg);
111 /* Enable TX and configure the watermark so that we get an interrupt
112 * when there's room for one more character in the TX fifo (if
113 * interrupts are enabled). */
114 reg = SFUART_TXCTRL_ENABLE;
115 reg |= (1 << SFUART_TXCTRL_TXCNT_SHIFT);
117 reg |= SFUART_TXCTRL_NSTOP;
118 uart_setreg(bas, SFUART_TXCTRL, reg);
120 /* Don't touch DIV. Assume that's set correctly until we can
125 sfuart_putc(struct uart_bas *bas, int c)
128 while ((uart_getreg(bas, SFUART_TXDATA) & SFUART_TXDATA_FULL)
132 uart_setreg(bas, SFUART_TXDATA, c);
136 sfuart_rxready(struct uart_bas *bas)
139 * Unfortunately the FIFO empty flag is in the FIFO data register so
140 * reading it would dequeue the character. Instead, rely on the fact
141 * we've configured the watermark to be 0 and that interrupts are off
142 * when using the low-level console function, and read the interrupt
143 * pending state instead.
145 return ((uart_getreg(bas, SFUART_IRQ_PENDING) &
146 SFUART_IRQ_PENDING_RXQM) != 0);
150 sfuart_getc(struct uart_bas *bas, struct mtx *hwmtx)
156 while (((c = uart_getreg(bas, SFUART_RXDATA)) &
157 SFUART_RXDATA_EMPTY) != 0) {
169 sfuart_bus_probe(struct uart_softc *sc)
173 error = sfuart_probe(&sc->sc_bas);
177 sc->sc_rxfifosz = SFUART_RX_FIFO_DEPTH;
178 sc->sc_txfifosz = SFUART_TX_FIFO_DEPTH;
182 device_set_desc(sc->sc_dev, "SiFive UART");
188 sfuart_bus_attach(struct uart_softc *sc)
190 struct uart_bas *bas;
191 struct sfuart_softc *sfsc;
196 sfsc = (struct sfuart_softc *)sc;
199 error = clk_get_by_ofw_index(sc->sc_dev, 0, 0, &sfsc->clk);
201 device_printf(sc->sc_dev, "couldn't allocate clock\n");
205 error = clk_enable(sfsc->clk);
207 device_printf(sc->sc_dev, "couldn't enable clock\n");
211 error = clk_get_freq(sfsc->clk, &freq);
212 if (error || freq == 0) {
213 clk_disable(sfsc->clk);
214 device_printf(sc->sc_dev, "couldn't get clock frequency\n");
221 reg = SFUART_RXCTRL_ENABLE;
222 reg |= (0 << SFUART_RXCTRL_RXCNT_SHIFT);
223 uart_setreg(bas, SFUART_RXCTRL, reg);
225 reg = SFUART_TXCTRL_ENABLE;
226 reg |= (1 << SFUART_TXCTRL_TXCNT_SHIFT);
227 uart_setreg(bas, SFUART_TXCTRL, reg);
229 /* Enable RX interrupt */
230 uart_setreg(bas, SFUART_IRQ_ENABLE, SFUART_IRQ_ENABLE_RXWM);
236 sfuart_bus_detach(struct uart_softc *sc)
238 struct sfuart_softc *sfsc;
239 struct uart_bas *bas;
241 sfsc = (struct sfuart_softc *)sc;
245 uart_setreg(bas, SFUART_RXCTRL, 0);
246 uart_setreg(bas, SFUART_TXCTRL, 0);
248 /* Disable interrupts */
249 uart_setreg(bas, SFUART_IRQ_ENABLE, 0);
251 clk_disable(sfsc->clk);
257 sfuart_bus_flush(struct uart_softc *sc, int what)
259 struct uart_bas *bas;
263 uart_lock(sc->sc_hwmtx);
265 if (what & UART_FLUSH_TRANSMITTER) {
267 reg = uart_getreg(bas, SFUART_TXDATA);
268 } while ((reg & SFUART_TXDATA_FULL) != 0);
271 if (what & UART_FLUSH_RECEIVER) {
273 reg = uart_getreg(bas, SFUART_RXDATA);
274 } while ((reg & SFUART_RXDATA_EMPTY) == 0);
276 uart_unlock(sc->sc_hwmtx);
281 #define SIGCHG(c, i, s, d) \
284 i |= ((i) & (s)) ? (s) : (s) | (d); \
286 i = ((i) & (s)) ? ((i) & ~(s)) | (d) : (i); \
290 sfuart_bus_getsig(struct uart_softc *sc)
292 uint32_t new, old, sig;
297 SIGCHG(1, sig, SER_DSR, SER_DDSR);
298 SIGCHG(1, sig, SER_DCD, SER_DDCD);
299 SIGCHG(1, sig, SER_CTS, SER_DCTS);
300 new = sig & ~SER_MASK_DELTA;
301 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
307 sfuart_bus_setsig(struct uart_softc *sc, int sig)
314 if (sig & SER_DDTR) {
315 SIGCHG(sig & SER_DTR, new, SER_DTR, SER_DDTR);
317 if (sig & SER_DRTS) {
318 SIGCHG(sig & SER_RTS, new, SER_RTS, SER_DRTS);
320 } while (!atomic_cmpset_32(&sc->sc_hwsig, old, new));
326 sfuart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
328 struct uart_bas *bas;
334 uart_lock(sc->sc_hwmtx);
337 case UART_IOCTL_BAUD:
338 reg = uart_getreg(bas, SFUART_DIV);
340 /* Possible if the divisor hasn't been set up yet. */
344 *(int*)data = bas->rclk / (reg + 1);
352 uart_unlock(sc->sc_hwmtx);
358 sfuart_bus_ipend(struct uart_softc *sc)
360 struct uart_bas *bas;
365 uart_lock(sc->sc_hwmtx);
368 reg = uart_getreg(bas, SFUART_IRQ_PENDING);
369 ie = uart_getreg(bas, SFUART_IRQ_ENABLE);
371 if ((reg & SFUART_IRQ_PENDING_TXWM) != 0 &&
372 (ie & SFUART_IRQ_ENABLE_TXWM) != 0) {
373 ipend |= SER_INT_TXIDLE;
375 /* Disable TX interrupt */
376 ie &= ~(SFUART_IRQ_ENABLE_TXWM);
377 uart_setreg(bas, SFUART_IRQ_ENABLE, ie);
380 if ((reg & SFUART_IRQ_PENDING_RXQM) != 0)
381 ipend |= SER_INT_RXREADY;
383 uart_unlock(sc->sc_hwmtx);
389 sfuart_bus_param(struct uart_softc *sc, int baudrate, int databits,
390 int stopbits, int parity)
392 struct uart_bas *bas;
400 if (parity != UART_PARITY_NONE)
403 uart_lock(sc->sc_hwmtx);
405 reg = uart_getreg(bas, SFUART_TXCTRL);
407 reg |= SFUART_TXCTRL_NSTOP;
408 } else if (stopbits == 1) {
409 reg &= ~SFUART_TXCTRL_NSTOP;
411 uart_unlock(sc->sc_hwmtx);
415 if (baudrate > 0 && bas->rclk != 0) {
416 reg = (bas->rclk / baudrate) - 1;
417 uart_setreg(bas, SFUART_DIV, reg);
420 uart_unlock(sc->sc_hwmtx);
425 sfuart_bus_receive(struct uart_softc *sc)
427 struct uart_bas *bas;
431 uart_lock(sc->sc_hwmtx);
433 reg = uart_getreg(bas, SFUART_RXDATA);
434 while ((reg & SFUART_RXDATA_EMPTY) == 0) {
435 if (uart_rx_full(sc)) {
436 sc->sc_rxbuf[sc->sc_rxput] = UART_STAT_OVERRUN;
440 uart_rx_put(sc, reg & 0xff);
442 reg = uart_getreg(bas, SFUART_RXDATA);
445 uart_unlock(sc->sc_hwmtx);
451 sfuart_bus_transmit(struct uart_softc *sc)
453 struct uart_bas *bas;
458 uart_lock(sc->sc_hwmtx);
460 reg = uart_getreg(bas, SFUART_IRQ_ENABLE);
461 reg |= SFUART_IRQ_ENABLE_TXWM;
462 uart_setreg(bas, SFUART_IRQ_ENABLE, reg);
464 for (i = 0; i < sc->sc_txdatasz; i++)
465 sfuart_putc(bas, sc->sc_txbuf[i]);
469 uart_unlock(sc->sc_hwmtx);
475 sfuart_bus_grab(struct uart_softc *sc)
477 struct uart_bas *bas;
481 uart_lock(sc->sc_hwmtx);
483 reg = uart_getreg(bas, SFUART_IRQ_ENABLE);
484 reg &= ~(SFUART_IRQ_ENABLE_TXWM | SFUART_IRQ_PENDING_RXQM);
485 uart_setreg(bas, SFUART_IRQ_ENABLE, reg);
487 uart_unlock(sc->sc_hwmtx);
491 sfuart_bus_ungrab(struct uart_softc *sc)
493 struct uart_bas *bas;
497 uart_lock(sc->sc_hwmtx);
499 reg = uart_getreg(bas, SFUART_IRQ_ENABLE);
500 reg |= SFUART_IRQ_ENABLE_TXWM | SFUART_IRQ_PENDING_RXQM;
501 uart_setreg(bas, SFUART_IRQ_ENABLE, reg);
503 uart_unlock(sc->sc_hwmtx);
506 static kobj_method_t sfuart_methods[] = {
507 KOBJMETHOD(uart_probe, sfuart_bus_probe),
508 KOBJMETHOD(uart_attach, sfuart_bus_attach),
509 KOBJMETHOD(uart_detach, sfuart_bus_detach),
510 KOBJMETHOD(uart_flush, sfuart_bus_flush),
511 KOBJMETHOD(uart_getsig, sfuart_bus_getsig),
512 KOBJMETHOD(uart_setsig, sfuart_bus_setsig),
513 KOBJMETHOD(uart_ioctl, sfuart_bus_ioctl),
514 KOBJMETHOD(uart_ipend, sfuart_bus_ipend),
515 KOBJMETHOD(uart_param, sfuart_bus_param),
516 KOBJMETHOD(uart_receive, sfuart_bus_receive),
517 KOBJMETHOD(uart_transmit, sfuart_bus_transmit),
518 KOBJMETHOD(uart_grab, sfuart_bus_grab),
519 KOBJMETHOD(uart_ungrab, sfuart_bus_ungrab),
523 static struct uart_ops sfuart_ops = {
524 .probe = sfuart_probe,
528 .rxready = sfuart_rxready,
532 struct uart_class sfuart_class = {
535 sizeof(struct sfuart_softc),
536 .uc_ops = &sfuart_ops,
537 .uc_range = SFUART_REGS_SIZE,
542 static struct ofw_compat_data compat_data[] = {
543 { "sifive,uart0", (uintptr_t)&sfuart_class },
544 { NULL, (uintptr_t)NULL }
547 UART_FDT_CLASS_AND_DEVICE(compat_data);