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 snps_uart_attach(struct uart_softc *uart_sc)
67 struct snps_softc *sc;
69 sc = (struct snps_softc *)uart_sc;
71 /* UART requires to read USR reg when IIR_BUSY */
72 sc->ns8250.busy_detect = 1;
74 return (ns8250_bus_attach(uart_sc));
77 static kobj_method_t snps_methods[] = {
78 KOBJMETHOD(uart_probe, ns8250_bus_probe),
79 KOBJMETHOD(uart_attach, snps_uart_attach),
80 KOBJMETHOD(uart_detach, ns8250_bus_detach),
81 KOBJMETHOD(uart_flush, ns8250_bus_flush),
82 KOBJMETHOD(uart_getsig, ns8250_bus_getsig),
83 KOBJMETHOD(uart_ioctl, ns8250_bus_ioctl),
84 KOBJMETHOD(uart_ipend, ns8250_bus_ipend),
85 KOBJMETHOD(uart_param, ns8250_bus_param),
86 KOBJMETHOD(uart_receive, ns8250_bus_receive),
87 KOBJMETHOD(uart_setsig, ns8250_bus_setsig),
88 KOBJMETHOD(uart_transmit, ns8250_bus_transmit),
89 KOBJMETHOD(uart_grab, ns8250_bus_grab),
90 KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab),
94 struct uart_class uart_snps_class = {
97 sizeof(struct snps_softc),
98 .uc_ops = &uart_ns8250_ops,
103 static struct ofw_compat_data compat_data[] = {
104 { "snps,dw-apb-uart", (uintptr_t)&uart_snps_class },
105 { NULL, (uintptr_t)NULL }
107 UART_FDT_CLASS(compat_data);
111 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
113 struct snps_softc *sc;
115 sc = device_get_softc(dev);
119 /* Baud clock is either named "baudclk", or there is a single
122 if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
123 clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
126 /* APB peripheral clock is optional */
127 (void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
134 snps_probe(device_t dev)
136 struct snps_softc *sc;
137 struct uart_class *uart_class;
139 uint32_t shift, iowidth, clock;
143 clk_t baudclk, apb_pclk;
147 if (!ofw_bus_status_okay(dev))
150 uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
151 compat_data)->ocd_data;
152 if (uart_class == NULL)
156 sc = device_get_softc(dev);
157 sc->ns8250.base.sc_class = uart_class;
159 node = ofw_bus_get_node(dev);
160 if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
162 if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
164 if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
168 if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
169 error = hwreset_deassert(reset);
171 device_printf(dev, "cannot de-assert reset\n");
176 if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
177 error = clk_enable(baudclk);
179 device_printf(dev, "cannot enable baud clock\n");
182 if (apb_pclk != NULL) {
183 error = clk_enable(apb_pclk);
186 "cannot enable peripheral clock\n");
192 error = clk_get_freq(baudclk, &freq);
194 device_printf(dev, "cannot get frequency\n");
197 clock = (uint32_t)freq;
202 if (bootverbose && clock == 0)
203 device_printf(dev, "could not determine frequency\n");
205 error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0);
210 /* XXX uart_bus_probe has changed the softc, so refresh it */
211 sc = device_get_softc(dev);
213 /* Store clock and reset handles for detach */
214 sc->baudclk = baudclk;
215 sc->apb_pclk = apb_pclk;
223 snps_detach(device_t dev)
226 struct snps_softc *sc;
227 clk_t baudclk, apb_pclk;
233 sc = device_get_softc(dev);
234 baudclk = sc->baudclk;
235 apb_pclk = sc->apb_pclk;
239 error = uart_bus_detach(dev);
245 error = hwreset_assert(reset);
247 device_printf(dev, "cannot assert reset\n");
250 hwreset_release(reset);
252 if (apb_pclk != NULL) {
253 error = clk_release(apb_pclk);
255 device_printf(dev, "cannot release peripheral clock\n");
259 if (baudclk != NULL) {
260 error = clk_release(baudclk);
262 device_printf(dev, "cannot release baud clock\n");
271 static device_method_t snps_bus_methods[] = {
272 /* Device interface */
273 DEVMETHOD(device_probe, snps_probe),
274 DEVMETHOD(device_attach, uart_bus_attach),
275 DEVMETHOD(device_detach, snps_detach),
279 static driver_t snps_uart_driver = {
282 sizeof(struct snps_softc)
285 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0);