xref: /freebsd/sys/dev/uart/uart_dev_snps.c (revision 6de1c50e78a826abc08660e4c3ac10f1ddba9ba2)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/bus.h>
29 #include <sys/kernel.h>
30 #include <sys/module.h>
31 #include <machine/bus.h>
32 
33 #include <dev/uart/uart.h>
34 #include <dev/uart/uart_bus.h>
35 #include <dev/uart/uart_cpu_fdt.h>
36 #include <dev/uart/uart_dev_ns8250.h>
37 
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 
41 #include <dev/clk/clk.h>
42 #include <dev/hwreset/hwreset.h>
43 
44 #include "uart_if.h"
45 
46 struct snps_softc {
47 	struct ns8250_softc	ns8250;
48 
49 	clk_t			baudclk;
50 	clk_t			apb_pclk;
51 	hwreset_t		reset;
52 };
53 
54 /*
55  * To use early printf on 64 bits Allwinner SoC, add to kernel config
56  * options SOCDEV_PA=0x0
57  * options EARLY_PRINTF=snps
58  *
59  * To use early printf on 32 bits Allwinner SoC, add to kernel config
60  * options SOCDEV_PA=0x01C00000
61  * options SOCDEV_VA=0x10000000
62  * options EARLY_PRINTF=snps
63  *
64  * remove the if 0
65 */
66 #if CHECK_EARLY_PRINTF(snps)
67 static void
uart_snps_early_putc(int c)68 uart_snps_early_putc(int c)
69 {
70 	volatile uint32_t *stat;
71 	volatile uint32_t *tx;
72 
73 #ifdef ALLWINNER_64
74 	stat = (uint32_t *) (socdev_va + 0x1C2807C);
75 	tx = (uint32_t *) (socdev_va + 0x1C28000);
76 #endif
77 #ifdef ALLWINNER_32
78 	stat = (uint32_t *) (socdev_va + 0x2807C);
79 	tx = (uint32_t *) (socdev_va + 0x28000);
80 #endif
81 
82 	while ((*stat & (1 << 2)) == 0)
83 		continue;
84 	*tx = c;
85 }
86 early_putc_t *early_putc = uart_snps_early_putc;
87 #endif /* CHECK_EARLY_PRINTF */
88 
89 static kobj_method_t snps_methods[] = {
90 	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
91 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
92 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
93 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
94 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
95 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
96 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
97 	KOBJMETHOD(uart_param,		ns8250_bus_param),
98 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
99 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
100 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
101 	KOBJMETHOD(uart_txbusy,		ns8250_bus_txbusy),
102 	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
103 	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
104 	KOBJMETHOD_END
105 };
106 
107 struct uart_class uart_snps_class = {
108 	"snps",
109 	snps_methods,
110 	sizeof(struct snps_softc),
111 	.uc_ops = &uart_ns8250_ops,
112 	.uc_range = 8,
113 	.uc_rclk = 0,
114 };
115 
116 struct uart_class uart_snps_jh7110_class = {
117 	"snps",
118 	snps_methods,
119 	sizeof(struct snps_softc),
120 	.uc_ops = &uart_ns8250_ops,
121 	.uc_range = 8,
122 	.uc_rclk = 24000000,
123 };
124 
125 static struct ofw_compat_data compat_data[] = {
126 	{ "starfive,jh7110-uart",	(uintptr_t)&uart_snps_jh7110_class },
127 	{ "snps,dw-apb-uart",		(uintptr_t)&uart_snps_class },
128 	{ "marvell,armada-38x-uart",	(uintptr_t)&uart_snps_class },
129 	{ NULL,				(uintptr_t)NULL }
130 };
131 UART_FDT_CLASS(compat_data);
132 
133 static int
snps_get_clocks(device_t dev,clk_t * baudclk,clk_t * apb_pclk)134 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
135 {
136 
137 	*baudclk = NULL;
138 	*apb_pclk = NULL;
139 
140 	/* Baud clock is either named "baudclk", or there is a single
141 	 * unnamed clock.
142 	 */
143 	if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
144 	    clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
145 		return (ENOENT);
146 
147 	/* APB peripheral clock is optional */
148 	(void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
149 
150 	return (0);
151 }
152 
153 static int
snps_probe(device_t dev)154 snps_probe(device_t dev)
155 {
156 	struct snps_softc *sc;
157 	struct uart_class *uart_class;
158 	phandle_t node;
159 	uint32_t shift, iowidth, clock;
160 	uint64_t freq;
161 	int error;
162 	clk_t baudclk, apb_pclk;
163 	hwreset_t reset;
164 
165 	if (!ofw_bus_status_okay(dev))
166 		return (ENXIO);
167 
168 	uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
169 	    compat_data)->ocd_data;
170 	if (uart_class == NULL)
171 		return (ENXIO);
172 
173 	freq = 0;
174 	sc = device_get_softc(dev);
175 	sc->ns8250.base.sc_class = uart_class;
176 
177 	node = ofw_bus_get_node(dev);
178 	if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
179 		shift = 0;
180 	if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
181 		iowidth = 1;
182 	if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
183 		clock = 0;
184 
185 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
186 		error = hwreset_deassert(reset);
187 		if (error != 0) {
188 			device_printf(dev, "cannot de-assert reset\n");
189 			return (error);
190 		}
191 	}
192 
193 	if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
194 		error = clk_enable(baudclk);
195 		if (error != 0) {
196 			device_printf(dev, "cannot enable baud clock\n");
197 			return (error);
198 		}
199 		if (apb_pclk != NULL) {
200 			error = clk_enable(apb_pclk);
201 			if (error != 0) {
202 				device_printf(dev,
203 				    "cannot enable peripheral clock\n");
204 				return (error);
205 			}
206 		}
207 
208 		if (clock == 0) {
209 			error = clk_get_freq(baudclk, &freq);
210 			if (error != 0) {
211 				device_printf(dev, "cannot get frequency\n");
212 				return (error);
213 			}
214 			clock = (uint32_t)freq;
215 		}
216 	}
217 
218 	if (bootverbose && clock == 0)
219 		device_printf(dev, "could not determine frequency\n");
220 
221 	error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
222 	if (error > 0)
223 		return (error);
224 
225 	/* XXX uart_bus_probe has changed the softc, so refresh it */
226 	sc = device_get_softc(dev);
227 
228 	/* Store clock and reset handles for detach */
229 	sc->baudclk = baudclk;
230 	sc->apb_pclk = apb_pclk;
231 	sc->reset = reset;
232 
233 	return (BUS_PROBE_VENDOR);
234 }
235 
236 static int
snps_attach(device_t dev)237 snps_attach(device_t dev)
238 {
239 	phandle_t node;
240 	int ret;
241 
242 	ret = uart_bus_attach(dev);
243 	if (ret == 0) {
244 		node = ofw_bus_get_node(dev);
245 		/* Set up phandle to dev mapping */
246 		OF_device_register_xref(OF_xref_from_node(node), dev);
247 	}
248 
249 	return (ret);
250 }
251 
252 static int
snps_detach(device_t dev)253 snps_detach(device_t dev)
254 {
255 	struct snps_softc *sc;
256 	clk_t baudclk, apb_pclk;
257 	hwreset_t reset;
258 	int error;
259 
260 	sc = device_get_softc(dev);
261 	baudclk = sc->baudclk;
262 	apb_pclk = sc->apb_pclk;
263 	reset = sc->reset;
264 
265 	error = uart_bus_detach(dev);
266 	if (error != 0)
267 		return (error);
268 
269 	if (reset != NULL) {
270 		error = hwreset_assert(reset);
271 		if (error != 0) {
272 			device_printf(dev, "cannot assert reset\n");
273 			return (error);
274 		}
275 		hwreset_release(reset);
276 	}
277 	if (apb_pclk != NULL) {
278 		error = clk_release(apb_pclk);
279 		if (error != 0) {
280 			device_printf(dev, "cannot release peripheral clock\n");
281 			return (error);
282 		}
283 	}
284 	if (baudclk != NULL) {
285 		error = clk_release(baudclk);
286 		if (error != 0) {
287 			device_printf(dev, "cannot release baud clock\n");
288 			return (error);
289 		}
290 	}
291 
292 	return (0);
293 }
294 
295 static device_method_t snps_bus_methods[] = {
296 	/* Device interface */
297 	DEVMETHOD(device_probe,		snps_probe),
298 	DEVMETHOD(device_attach,	snps_attach),
299 	DEVMETHOD(device_detach, 	snps_detach),
300 	DEVMETHOD_END
301 };
302 
303 static driver_t snps_uart_driver = {
304 	uart_driver_name,
305 	snps_bus_methods,
306 	sizeof(struct snps_softc)
307 };
308 
309 DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, 0, 0);
310