xref: /freebsd/sys/dev/uart/uart_dev_snps.c (revision d6eb98610fa65663bf0df4574b7cb2c5c4ffda71)
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <machine/bus.h>
38 
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>
43 
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 
47 #ifdef EXT_RESOURCES
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 #endif
51 
52 #include "uart_if.h"
53 
54 struct snps_softc {
55 	struct ns8250_softc	ns8250;
56 
57 #ifdef EXT_RESOURCES
58 	clk_t			baudclk;
59 	clk_t			apb_pclk;
60 	hwreset_t		reset;
61 #endif
62 };
63 
64 /*
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
69  *
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
74  *
75  * remove the if 0
76 */
77 #if 0
78 #ifdef EARLY_PRINTF
79 static void
80 uart_snps_early_putc(int c)
81 {
82 	volatile uint32_t *stat;
83 	volatile uint32_t *tx;
84 
85 #ifdef ALLWINNER_64
86 	stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C);
87 	tx = (uint32_t *) (SOCDEV_VA + 0x1C28000);
88 #endif
89 #ifdef ALLWINNER_32
90 	stat = (uint32_t *) (SOCDEV_VA + 0x2807C);
91 	tx = (uint32_t *) (SOCDEV_VA + 0x28000);
92 #endif
93 
94 	while ((*stat & (1 << 2)) == 0)
95 		continue;
96 	*tx = c;
97 }
98 early_putc_t *early_putc = uart_snps_early_putc;
99 #endif /* EARLY_PRINTF */
100 #endif
101 
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),
116 	KOBJMETHOD_END
117 };
118 
119 struct uart_class uart_snps_class = {
120 	"snps",
121 	snps_methods,
122 	sizeof(struct snps_softc),
123 	.uc_ops = &uart_ns8250_ops,
124 	.uc_range = 8,
125 	.uc_rclk = 0,
126 };
127 
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 }
132 };
133 UART_FDT_CLASS(compat_data);
134 
135 #ifdef EXT_RESOURCES
136 static int
137 snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
138 {
139 
140 	*baudclk = NULL;
141 	*apb_pclk = NULL;
142 
143 	/* Baud clock is either named "baudclk", or there is a single
144 	 * unnamed clock.
145 	 */
146 	if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
147 	    clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
148 		return (ENOENT);
149 
150 	/* APB peripheral clock is optional */
151 	(void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
152 
153 	return (0);
154 }
155 #endif
156 
157 static int
158 snps_probe(device_t dev)
159 {
160 	struct snps_softc *sc;
161 	struct uart_class *uart_class;
162 	phandle_t node;
163 	uint32_t shift, iowidth, clock;
164 	uint64_t freq;
165 	int error;
166 #ifdef EXT_RESOURCES
167 	clk_t baudclk, apb_pclk;
168 	hwreset_t reset;
169 #endif
170 
171 	if (!ofw_bus_status_okay(dev))
172 		return (ENXIO);
173 
174 	uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
175 	    compat_data)->ocd_data;
176 	if (uart_class == NULL)
177 		return (ENXIO);
178 
179 	freq = 0;
180 	sc = device_get_softc(dev);
181 	sc->ns8250.base.sc_class = uart_class;
182 
183 	node = ofw_bus_get_node(dev);
184 	if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
185 		shift = 0;
186 	if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
187 		iowidth = 1;
188 	if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
189 		clock = 0;
190 
191 #ifdef EXT_RESOURCES
192 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
193 		error = hwreset_deassert(reset);
194 		if (error != 0) {
195 			device_printf(dev, "cannot de-assert reset\n");
196 			return (error);
197 		}
198 	}
199 
200 	if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
201 		error = clk_enable(baudclk);
202 		if (error != 0) {
203 			device_printf(dev, "cannot enable baud clock\n");
204 			return (error);
205 		}
206 		if (apb_pclk != NULL) {
207 			error = clk_enable(apb_pclk);
208 			if (error != 0) {
209 				device_printf(dev,
210 				    "cannot enable peripheral clock\n");
211 				return (error);
212 			}
213 		}
214 
215 		if (clock == 0) {
216 			error = clk_get_freq(baudclk, &freq);
217 			if (error != 0) {
218 				device_printf(dev, "cannot get frequency\n");
219 				return (error);
220 			}
221 			clock = (uint32_t)freq;
222 		}
223 	}
224 #endif
225 
226 	if (bootverbose && clock == 0)
227 		device_printf(dev, "could not determine frequency\n");
228 
229 	error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
230 	if (error != 0)
231 		return (error);
232 
233 #ifdef EXT_RESOURCES
234 	/* XXX uart_bus_probe has changed the softc, so refresh it */
235 	sc = device_get_softc(dev);
236 
237 	/* Store clock and reset handles for detach */
238 	sc->baudclk = baudclk;
239 	sc->apb_pclk = apb_pclk;
240 	sc->reset = reset;
241 #endif
242 
243 	return (0);
244 }
245 
246 static int
247 snps_detach(device_t dev)
248 {
249 #ifdef EXT_RESOURCES
250 	struct snps_softc *sc;
251 	clk_t baudclk, apb_pclk;
252 	hwreset_t reset;
253 #endif
254 	int error;
255 
256 #ifdef EXT_RESOURCES
257 	sc = device_get_softc(dev);
258 	baudclk = sc->baudclk;
259 	apb_pclk = sc->apb_pclk;
260 	reset = sc->reset;
261 #endif
262 
263 	error = uart_bus_detach(dev);
264 	if (error != 0)
265 		return (error);
266 
267 #ifdef EXT_RESOURCES
268 	if (reset != NULL) {
269 		error = hwreset_assert(reset);
270 		if (error != 0) {
271 			device_printf(dev, "cannot assert reset\n");
272 			return (error);
273 		}
274 		hwreset_release(reset);
275 	}
276 	if (apb_pclk != NULL) {
277 		error = clk_release(apb_pclk);
278 		if (error != 0) {
279 			device_printf(dev, "cannot release peripheral clock\n");
280 			return (error);
281 		}
282 	}
283 	if (baudclk != NULL) {
284 		error = clk_release(baudclk);
285 		if (error != 0) {
286 			device_printf(dev, "cannot release baud clock\n");
287 			return (error);
288 		}
289 	}
290 #endif
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,	uart_bus_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, uart_devclass, 0, 0);
310