2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <machine/bus.h>
39 #include <dev/uart/uart.h>
40 #include <dev/uart/uart_bus.h>
41 #include <dev/uart/uart_cpu_fdt.h>
42 #include <dev/uart/uart_dev_ns8250.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
55 struct ns8250_softc ns8250;
65 * To use early printf on 64 bits Allwinner SoC, add to kernel config
66 * options SOCDEV_PA=0x0
67 * options SOCDEV_VA=0x40000000
68 * options EARLY_PRINTF
70 * To use early printf on 32 bits Allwinner SoC, add to kernel config
71 * options SOCDEV_PA=0x01C00000
72 * options SOCDEV_VA=0x10000000
73 * options EARLY_PRINTF
80 uart_snps_early_putc(int c)
82 volatile uint32_t *stat;
83 volatile uint32_t *tx;
86 stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C);
87 tx = (uint32_t *) (SOCDEV_VA + 0x1C28000);
90 stat = (uint32_t *) (SOCDEV_VA + 0x2807C);
91 tx = (uint32_t *) (SOCDEV_VA + 0x28000);
94 while ((*stat & (1 << 2)) == 0)
98 early_putc_t *early_putc = uart_snps_early_putc;
99 #endif /* EARLY_PRINTF */
102 static kobj_method_t snps_methods[] = {
103 KOBJMETHOD(uart_probe, ns8250_bus_probe),
104 KOBJMETHOD(uart_attach, ns8250_bus_attach),
105 KOBJMETHOD(uart_detach, ns8250_bus_detach),
106 KOBJMETHOD(uart_flush, ns8250_bus_flush),
107 KOBJMETHOD(uart_getsig, ns8250_bus_getsig),
108 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl),
109 KOBJMETHOD(uart_ipend, ns8250_bus_ipend),
110 KOBJMETHOD(uart_param, ns8250_bus_param),
111 KOBJMETHOD(uart_receive, ns8250_bus_receive),
112 KOBJMETHOD(uart_setsig, ns8250_bus_setsig),
113 KOBJMETHOD(uart_transmit, ns8250_bus_transmit),
114 KOBJMETHOD(uart_grab, ns8250_bus_grab),
115 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab),
119 struct uart_class uart_snps_class = {
122 sizeof(struct snps_softc),
123 .uc_ops = &uart_ns8250_ops,
128 static struct ofw_compat_data compat_data[] = {
129 { "snps,dw-apb-uart", (uintptr_t)&uart_snps_class },
130 { "marvell,armada-38x-uart", (uintptr_t)&uart_snps_class },
131 { NULL, (uintptr_t)NULL }
133 UART_FDT_CLASS(compat_data);
137 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
143 /* Baud clock is either named "baudclk", or there is a single
146 if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
147 clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
150 /* APB peripheral clock is optional */
151 (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
158 snps_probe(device_t dev)
160 struct snps_softc *sc;
161 struct uart_class *uart_class;
163 uint32_t shift, iowidth, clock;
167 clk_t baudclk, apb_pclk;
171 if (!ofw_bus_status_okay(dev))
174 uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
175 compat_data)->ocd_data;
176 if (uart_class == NULL)
180 sc = device_get_softc(dev);
181 sc->ns8250.base.sc_class = uart_class;
183 node = ofw_bus_get_node(dev);
184 if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
186 if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
188 if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
192 if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
193 error = hwreset_deassert(reset);
195 device_printf(dev, "cannot de-assert reset\n");
200 if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
201 error = clk_enable(baudclk);
203 device_printf(dev, "cannot enable baud clock\n");
206 if (apb_pclk != NULL) {
207 error = clk_enable(apb_pclk);
210 "cannot enable peripheral clock\n");
216 error = clk_get_freq(baudclk, &freq);
218 device_printf(dev, "cannot get frequency\n");
221 clock = (uint32_t)freq;
226 if (bootverbose && clock == 0)
227 device_printf(dev, "could not determine frequency\n");
229 error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
234 /* XXX uart_bus_probe has changed the softc, so refresh it */
235 sc = device_get_softc(dev);
237 /* Store clock and reset handles for detach */
238 sc->baudclk = baudclk;
239 sc->apb_pclk = apb_pclk;
247 snps_detach(device_t dev)
250 struct snps_softc *sc;
251 clk_t baudclk, apb_pclk;
257 sc = device_get_softc(dev);
258 baudclk = sc->baudclk;
259 apb_pclk = sc->apb_pclk;
263 error = uart_bus_detach(dev);
269 error = hwreset_assert(reset);
271 device_printf(dev, "cannot assert reset\n");
274 hwreset_release(reset);
276 if (apb_pclk != NULL) {
277 error = clk_release(apb_pclk);
279 device_printf(dev, "cannot release peripheral clock\n");
283 if (baudclk != NULL) {
284 error = clk_release(baudclk);
286 device_printf(dev, "cannot release baud clock\n");
295 static device_method_t snps_bus_methods[] = {
296 /* Device interface */
297 DEVMETHOD(device_probe, snps_probe),
298 DEVMETHOD(device_attach, uart_bus_attach),
299 DEVMETHOD(device_detach, snps_detach),
303 static driver_t snps_uart_driver = {
306 sizeof(struct snps_softc)
309 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0);