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 */
103 snps_uart_attach(struct uart_softc *uart_sc)
105 struct snps_softc *sc;
107 sc = (struct snps_softc *)uart_sc;
109 /* UART requires to read USR reg when IIR_BUSY */
110 uart_sc->sc_bas.busy_detect = 1;
112 return (ns8250_bus_attach(uart_sc));
115 static kobj_method_t snps_methods[] = {
116 KOBJMETHOD(uart_probe, ns8250_bus_probe),
117 KOBJMETHOD(uart_attach, snps_uart_attach),
118 KOBJMETHOD(uart_detach, ns8250_bus_detach),
119 KOBJMETHOD(uart_flush, ns8250_bus_flush),
120 KOBJMETHOD(uart_getsig, ns8250_bus_getsig),
121 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl),
122 KOBJMETHOD(uart_ipend, ns8250_bus_ipend),
123 KOBJMETHOD(uart_param, ns8250_bus_param),
124 KOBJMETHOD(uart_receive, ns8250_bus_receive),
125 KOBJMETHOD(uart_setsig, ns8250_bus_setsig),
126 KOBJMETHOD(uart_transmit, ns8250_bus_transmit),
127 KOBJMETHOD(uart_grab, ns8250_bus_grab),
128 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab),
132 struct uart_class uart_snps_class = {
135 sizeof(struct snps_softc),
136 .uc_ops = &uart_ns8250_ops,
141 static struct ofw_compat_data compat_data[] = {
142 { "snps,dw-apb-uart", (uintptr_t)&uart_snps_class },
143 { NULL, (uintptr_t)NULL }
145 UART_FDT_CLASS(compat_data);
149 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
155 /* Baud clock is either named "baudclk", or there is a single
158 if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
159 clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
162 /* APB peripheral clock is optional */
163 (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
170 snps_probe(device_t dev)
172 struct snps_softc *sc;
173 struct uart_class *uart_class;
175 uint32_t shift, iowidth, clock;
179 clk_t baudclk, apb_pclk;
183 if (!ofw_bus_status_okay(dev))
186 uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
187 compat_data)->ocd_data;
188 if (uart_class == NULL)
192 sc = device_get_softc(dev);
193 sc->ns8250.base.sc_class = uart_class;
195 node = ofw_bus_get_node(dev);
196 if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
198 if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
200 if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
204 if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
205 error = hwreset_deassert(reset);
207 device_printf(dev, "cannot de-assert reset\n");
212 if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
213 error = clk_enable(baudclk);
215 device_printf(dev, "cannot enable baud clock\n");
218 if (apb_pclk != NULL) {
219 error = clk_enable(apb_pclk);
222 "cannot enable peripheral clock\n");
228 error = clk_get_freq(baudclk, &freq);
230 device_printf(dev, "cannot get frequency\n");
233 clock = (uint32_t)freq;
238 if (bootverbose && clock == 0)
239 device_printf(dev, "could not determine frequency\n");
241 error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0);
246 /* XXX uart_bus_probe has changed the softc, so refresh it */
247 sc = device_get_softc(dev);
249 /* Store clock and reset handles for detach */
250 sc->baudclk = baudclk;
251 sc->apb_pclk = apb_pclk;
259 snps_detach(device_t dev)
262 struct snps_softc *sc;
263 clk_t baudclk, apb_pclk;
269 sc = device_get_softc(dev);
270 baudclk = sc->baudclk;
271 apb_pclk = sc->apb_pclk;
275 error = uart_bus_detach(dev);
281 error = hwreset_assert(reset);
283 device_printf(dev, "cannot assert reset\n");
286 hwreset_release(reset);
288 if (apb_pclk != NULL) {
289 error = clk_release(apb_pclk);
291 device_printf(dev, "cannot release peripheral clock\n");
295 if (baudclk != NULL) {
296 error = clk_release(baudclk);
298 device_printf(dev, "cannot release baud clock\n");
307 static device_method_t snps_bus_methods[] = {
308 /* Device interface */
309 DEVMETHOD(device_probe, snps_probe),
310 DEVMETHOD(device_attach, uart_bus_attach),
311 DEVMETHOD(device_detach, snps_detach),
315 static driver_t snps_uart_driver = {
318 sizeof(struct snps_softc)
321 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0);