2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
20 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
21 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <machine/bus.h>
38 #include <dev/uart/uart.h>
39 #include <dev/uart/uart_bus.h>
40 #include <dev/uart/uart_cpu_fdt.h>
41 #include <dev/uart/uart_dev_ns8250.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/extres/clk/clk.h>
48 #include <dev/extres/hwreset/hwreset.h>
54 struct ns8250_softc ns8250;
64 * To use early printf on 64 bits Allwinner SoC, add to kernel config
65 * options SOCDEV_PA=0x0
66 * options SOCDEV_VA=0x40000000
67 * options EARLY_PRINTF
69 * To use early printf on 32 bits Allwinner SoC, add to kernel config
70 * options SOCDEV_PA=0x01C00000
71 * options SOCDEV_VA=0x10000000
72 * options EARLY_PRINTF
79 uart_snps_early_putc(int c)
81 volatile uint32_t *stat;
82 volatile uint32_t *tx;
85 stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C);
86 tx = (uint32_t *) (SOCDEV_VA + 0x1C28000);
89 stat = (uint32_t *) (SOCDEV_VA + 0x2807C);
90 tx = (uint32_t *) (SOCDEV_VA + 0x28000);
93 while ((*stat & (1 << 2)) == 0)
97 early_putc_t *early_putc = uart_snps_early_putc;
98 #endif /* EARLY_PRINTF */
101 static kobj_method_t snps_methods[] = {
102 KOBJMETHOD(uart_probe, ns8250_bus_probe),
103 KOBJMETHOD(uart_attach, ns8250_bus_attach),
104 KOBJMETHOD(uart_detach, ns8250_bus_detach),
105 KOBJMETHOD(uart_flush, ns8250_bus_flush),
106 KOBJMETHOD(uart_getsig, ns8250_bus_getsig),
107 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl),
108 KOBJMETHOD(uart_ipend, ns8250_bus_ipend),
109 KOBJMETHOD(uart_param, ns8250_bus_param),
110 KOBJMETHOD(uart_receive, ns8250_bus_receive),
111 KOBJMETHOD(uart_setsig, ns8250_bus_setsig),
112 KOBJMETHOD(uart_transmit, ns8250_bus_transmit),
113 KOBJMETHOD(uart_grab, ns8250_bus_grab),
114 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab),
118 struct uart_class uart_snps_class = {
121 sizeof(struct snps_softc),
122 .uc_ops = &uart_ns8250_ops,
127 static struct ofw_compat_data compat_data[] = {
128 { "snps,dw-apb-uart", (uintptr_t)&uart_snps_class },
129 { "marvell,armada-38x-uart", (uintptr_t)&uart_snps_class },
130 { NULL, (uintptr_t)NULL }
132 UART_FDT_CLASS(compat_data);
136 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
142 /* Baud clock is either named "baudclk", or there is a single
145 if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
146 clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
149 /* APB peripheral clock is optional */
150 (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
157 snps_probe(device_t dev)
159 struct snps_softc *sc;
160 struct uart_class *uart_class;
162 uint32_t shift, iowidth, clock;
166 clk_t baudclk, apb_pclk;
170 if (!ofw_bus_status_okay(dev))
173 uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
174 compat_data)->ocd_data;
175 if (uart_class == NULL)
179 sc = device_get_softc(dev);
180 sc->ns8250.base.sc_class = uart_class;
182 node = ofw_bus_get_node(dev);
183 if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
185 if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
187 if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
191 if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
192 error = hwreset_deassert(reset);
194 device_printf(dev, "cannot de-assert reset\n");
199 if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
200 error = clk_enable(baudclk);
202 device_printf(dev, "cannot enable baud clock\n");
205 if (apb_pclk != NULL) {
206 error = clk_enable(apb_pclk);
209 "cannot enable peripheral clock\n");
215 error = clk_get_freq(baudclk, &freq);
217 device_printf(dev, "cannot get frequency\n");
220 clock = (uint32_t)freq;
225 if (bootverbose && clock == 0)
226 device_printf(dev, "could not determine frequency\n");
228 error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
233 /* XXX uart_bus_probe has changed the softc, so refresh it */
234 sc = device_get_softc(dev);
236 /* Store clock and reset handles for detach */
237 sc->baudclk = baudclk;
238 sc->apb_pclk = apb_pclk;
246 snps_detach(device_t dev)
249 struct snps_softc *sc;
250 clk_t baudclk, apb_pclk;
256 sc = device_get_softc(dev);
257 baudclk = sc->baudclk;
258 apb_pclk = sc->apb_pclk;
262 error = uart_bus_detach(dev);
268 error = hwreset_assert(reset);
270 device_printf(dev, "cannot assert reset\n");
273 hwreset_release(reset);
275 if (apb_pclk != NULL) {
276 error = clk_release(apb_pclk);
278 device_printf(dev, "cannot release peripheral clock\n");
282 if (baudclk != NULL) {
283 error = clk_release(baudclk);
285 device_printf(dev, "cannot release baud clock\n");
294 static device_method_t snps_bus_methods[] = {
295 /* Device interface */
296 DEVMETHOD(device_probe, snps_probe),
297 DEVMETHOD(device_attach, uart_bus_attach),
298 DEVMETHOD(device_detach, snps_detach),
302 static driver_t snps_uart_driver = {
305 sizeof(struct snps_softc)
308 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0);