2 * Copyright (c) 2013 Ian Lepore
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "opt_platform.h"
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/sysctl.h>
39 #include <machine/bus.h>
41 #include <dev/extres/clk/clk.h>
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/uart/uart.h>
46 #include <dev/uart/uart_cpu.h>
47 #include <dev/uart/uart_cpu_fdt.h>
48 #include <dev/uart/uart_bus.h>
49 #include <dev/uart/uart_dev_ns8250.h>
50 #include <dev/ic/ns16550.h>
54 * High-level UART interface.
56 struct jz4780_uart_softc {
57 struct ns8250_softc ns8250_base;
63 jz4780_bus_attach(struct uart_softc *sc)
65 struct ns8250_softc *ns8250;
69 ns8250 = (struct ns8250_softc *)sc;
72 rv = ns8250_bus_attach(sc);
76 /* Configure uart to use extra IER_RXTMOUT bit */
77 ns8250->ier_rxbits = IER_RXTMOUT | IER_EMSC | IER_ERLS | IER_ERXRDY;
78 ns8250->ier_mask = ~(ns8250->ier_rxbits);
79 ns8250->ier = uart_getreg(bas, REG_IER) & ns8250->ier_mask;
80 ns8250->ier |= ns8250->ier_rxbits;
81 uart_setreg(bas, REG_IER, ns8250->ier);
86 static kobj_method_t jz4780_uart_methods[] = {
87 KOBJMETHOD(uart_probe, ns8250_bus_probe),
88 KOBJMETHOD(uart_attach, jz4780_bus_attach),
89 KOBJMETHOD(uart_detach, ns8250_bus_detach),
90 KOBJMETHOD(uart_flush, ns8250_bus_flush),
91 KOBJMETHOD(uart_getsig, ns8250_bus_getsig),
92 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl),
93 KOBJMETHOD(uart_ipend, ns8250_bus_ipend),
94 KOBJMETHOD(uart_param, ns8250_bus_param),
95 KOBJMETHOD(uart_receive, ns8250_bus_receive),
96 KOBJMETHOD(uart_setsig, ns8250_bus_setsig),
97 KOBJMETHOD(uart_transmit, ns8250_bus_transmit),
98 KOBJMETHOD(uart_grab, ns8250_bus_grab),
99 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab),
103 static struct uart_class jz4780_uart_class = {
106 sizeof(struct jz4780_uart_softc),
107 .uc_ops = &uart_ns8250_ops,
112 /* Compatible devices. */
113 static struct ofw_compat_data compat_data[] = {
114 {"ingenic,jz4780-uart", (uintptr_t)&jz4780_uart_class},
115 {NULL, (uintptr_t)NULL},
118 UART_FDT_CLASS(compat_data);
121 * UART Driver interface.
124 jz4780_uart_get_shift(device_t dev)
129 node = ofw_bus_get_node(dev);
130 if ((OF_getencprop(node, "reg-shift", &shift, sizeof(shift))) <= 0)
136 jz4780_uart_probe(device_t dev)
138 struct jz4780_uart_softc *sc;
142 const struct ofw_compat_data *cd;
144 sc = device_get_softc(dev);
145 if (!ofw_bus_status_okay(dev))
147 cd = ofw_bus_search_compatible(dev, compat_data);
148 if (cd->ocd_data == 0)
151 /* Figure out clock setup */
152 rv = clk_get_by_ofw_name(dev, 0, "module", &sc->clk_mod);
154 device_printf(dev, "Cannot get UART clock: %d\n", rv);
157 rv = clk_enable(sc->clk_mod);
159 device_printf(dev, "Cannot enable UART clock: %d\n", rv);
162 rv = clk_get_by_ofw_name(dev, 0, "baud", &sc->clk_baud);
164 device_printf(dev, "Cannot get UART clock: %d\n", rv);
167 rv = clk_enable(sc->clk_baud);
169 device_printf(dev, "Cannot enable UART clock: %d\n", rv);
172 rv = clk_get_freq(sc->clk_baud, &freq);
174 device_printf(dev, "Cannot determine UART clock frequency: %d\n", rv);
179 device_printf(dev, "got UART clock: %lld\n", freq);
180 sc->ns8250_base.base.sc_class = (struct uart_class *)cd->ocd_data;
181 shift = jz4780_uart_get_shift(dev);
182 return (uart_bus_probe(dev, shift, (int)freq, 0, 0));
186 jz4780_uart_detach(device_t dev)
188 struct jz4780_uart_softc *sc;
191 rv = uart_bus_detach(dev);
195 sc = device_get_softc(dev);
196 if (sc->clk_mod != NULL) {
197 clk_release(sc->clk_mod);
199 if (sc->clk_baud != NULL) {
200 clk_release(sc->clk_baud);
205 static device_method_t jz4780_uart_bus_methods[] = {
206 /* Device interface */
207 DEVMETHOD(device_probe, jz4780_uart_probe),
208 DEVMETHOD(device_attach, uart_bus_attach),
209 DEVMETHOD(device_detach, jz4780_uart_detach),
213 static driver_t jz4780_uart_driver = {
215 jz4780_uart_bus_methods,
216 sizeof(struct jz4780_uart_softc),
219 DRIVER_MODULE(jz4780_uart, simplebus, jz4780_uart_driver, uart_devclass,