xref: /freebsd-12.1/sys/dev/uart/uart_dev_snps.c (revision 0693b1d2)
1b738dafdSJared McNeill /*-
2b738dafdSJared McNeill  * Copyright (c) 2016 Jared McNeill <[email protected]>
3b738dafdSJared McNeill  * All rights reserved.
4b738dafdSJared McNeill  *
5b738dafdSJared McNeill  * Redistribution and use in source and binary forms, with or without
6b738dafdSJared McNeill  * modification, are permitted provided that the following conditions
7b738dafdSJared McNeill  * are met:
8b738dafdSJared McNeill  * 1. Redistributions of source code must retain the above copyright
9b738dafdSJared McNeill  *    notice, this list of conditions and the following disclaimer.
10b738dafdSJared McNeill  * 2. Redistributions in binary form must reproduce the above copyright
11b738dafdSJared McNeill  *    notice, this list of conditions and the following disclaimer in the
12b738dafdSJared McNeill  *    documentation and/or other materials provided with the distribution.
13b738dafdSJared McNeill  *
14b738dafdSJared McNeill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15b738dafdSJared McNeill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16b738dafdSJared McNeill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17b738dafdSJared McNeill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18b738dafdSJared McNeill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19b738dafdSJared McNeill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20b738dafdSJared McNeill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21b738dafdSJared McNeill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22b738dafdSJared McNeill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23b738dafdSJared McNeill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24b738dafdSJared McNeill  * SUCH DAMAGE.
25b738dafdSJared McNeill  *
26b738dafdSJared McNeill  * $FreeBSD$
27b738dafdSJared McNeill  */
28b738dafdSJared McNeill 
29b738dafdSJared McNeill #include <sys/cdefs.h>
30b738dafdSJared McNeill __FBSDID("$FreeBSD$");
31b738dafdSJared McNeill 
32b738dafdSJared McNeill #include <sys/param.h>
33b738dafdSJared McNeill #include <sys/systm.h>
34b738dafdSJared McNeill #include <sys/bus.h>
35b738dafdSJared McNeill #include <sys/kernel.h>
36b738dafdSJared McNeill #include <sys/module.h>
37b738dafdSJared McNeill #include <machine/bus.h>
38b738dafdSJared McNeill 
39b738dafdSJared McNeill #include <dev/uart/uart.h>
40b738dafdSJared McNeill #include <dev/uart/uart_bus.h>
41b738dafdSJared McNeill #include <dev/uart/uart_cpu_fdt.h>
42b738dafdSJared McNeill #include <dev/uart/uart_dev_ns8250.h>
43b738dafdSJared McNeill 
44b738dafdSJared McNeill #include <dev/ofw/ofw_bus.h>
45b738dafdSJared McNeill #include <dev/ofw/ofw_bus_subr.h>
46b738dafdSJared McNeill 
47b738dafdSJared McNeill #ifdef EXT_RESOURCES
48b738dafdSJared McNeill #include <dev/extres/clk/clk.h>
49b738dafdSJared McNeill #include <dev/extres/hwreset/hwreset.h>
50b738dafdSJared McNeill #endif
51b738dafdSJared McNeill 
52b738dafdSJared McNeill #include "uart_if.h"
53b738dafdSJared McNeill 
54b738dafdSJared McNeill struct snps_softc {
55b738dafdSJared McNeill 	struct ns8250_softc	ns8250;
56b738dafdSJared McNeill 
57b738dafdSJared McNeill #ifdef EXT_RESOURCES
58b738dafdSJared McNeill 	clk_t			baudclk;
59b738dafdSJared McNeill 	clk_t			apb_pclk;
60b738dafdSJared McNeill 	hwreset_t		reset;
61b738dafdSJared McNeill #endif
62b738dafdSJared McNeill };
63b738dafdSJared McNeill 
64837db847SEmmanuel Vadot /*
65837db847SEmmanuel Vadot  * To use early printf on 64 bits Allwinner SoC, add to kernel config
66837db847SEmmanuel Vadot  * options SOCDEV_PA=0x0
67837db847SEmmanuel Vadot  * options SOCDEV_VA=0x40000000
68837db847SEmmanuel Vadot  * options EARLY_PRINTF
69837db847SEmmanuel Vadot  *
70837db847SEmmanuel Vadot  * To use early printf on 32 bits Allwinner SoC, add to kernel config
71837db847SEmmanuel Vadot  * options SOCDEV_PA=0x01C00000
72837db847SEmmanuel Vadot  * options SOCDEV_VA=0x10000000
73837db847SEmmanuel Vadot  * options EARLY_PRINTF
74837db847SEmmanuel Vadot  *
75837db847SEmmanuel Vadot  * remove the if 0
76837db847SEmmanuel Vadot */
77837db847SEmmanuel Vadot #if 0
78837db847SEmmanuel Vadot #ifdef EARLY_PRINTF
79837db847SEmmanuel Vadot static void
80837db847SEmmanuel Vadot uart_snps_early_putc(int c)
81837db847SEmmanuel Vadot {
82837db847SEmmanuel Vadot 	volatile uint32_t *stat;
83837db847SEmmanuel Vadot 	volatile uint32_t *tx;
84837db847SEmmanuel Vadot 
85837db847SEmmanuel Vadot #ifdef ALLWINNER_64
86837db847SEmmanuel Vadot 	stat = (uint32_t *) (SOCDEV_VA + 0x1C2807C);
87837db847SEmmanuel Vadot 	tx = (uint32_t *) (SOCDEV_VA + 0x1C28000);
88837db847SEmmanuel Vadot #endif
89837db847SEmmanuel Vadot #ifdef ALLWINNER_32
90837db847SEmmanuel Vadot 	stat = (uint32_t *) (SOCDEV_VA + 0x2807C);
91837db847SEmmanuel Vadot 	tx = (uint32_t *) (SOCDEV_VA + 0x28000);
92837db847SEmmanuel Vadot #endif
93837db847SEmmanuel Vadot 
94837db847SEmmanuel Vadot 	while ((*stat & (1 << 2)) == 0)
95837db847SEmmanuel Vadot 		continue;
96837db847SEmmanuel Vadot 	*tx = c;
97837db847SEmmanuel Vadot }
98837db847SEmmanuel Vadot early_putc_t *early_putc = uart_snps_early_putc;
99837db847SEmmanuel Vadot #endif /* EARLY_PRINTF */
100837db847SEmmanuel Vadot #endif
101837db847SEmmanuel Vadot 
102b738dafdSJared McNeill static kobj_method_t snps_methods[] = {
103b738dafdSJared McNeill 	KOBJMETHOD(uart_probe,		ns8250_bus_probe),
104381388b9SMatt Macy 	KOBJMETHOD(uart_attach,		ns8250_bus_attach),
105b738dafdSJared McNeill 	KOBJMETHOD(uart_detach,		ns8250_bus_detach),
106b738dafdSJared McNeill 	KOBJMETHOD(uart_flush,		ns8250_bus_flush),
107b738dafdSJared McNeill 	KOBJMETHOD(uart_getsig,		ns8250_bus_getsig),
108b738dafdSJared McNeill 	KOBJMETHOD(uart_ioctl,		ns8250_bus_ioctl),
109b738dafdSJared McNeill 	KOBJMETHOD(uart_ipend,		ns8250_bus_ipend),
110b738dafdSJared McNeill 	KOBJMETHOD(uart_param,		ns8250_bus_param),
111b738dafdSJared McNeill 	KOBJMETHOD(uart_receive,	ns8250_bus_receive),
112b738dafdSJared McNeill 	KOBJMETHOD(uart_setsig,		ns8250_bus_setsig),
113b738dafdSJared McNeill 	KOBJMETHOD(uart_transmit,	ns8250_bus_transmit),
114b738dafdSJared McNeill 	KOBJMETHOD(uart_grab,		ns8250_bus_grab),
115b738dafdSJared McNeill 	KOBJMETHOD(uart_ungrab,		ns8250_bus_ungrab),
116b738dafdSJared McNeill 	KOBJMETHOD_END
117b738dafdSJared McNeill };
118b738dafdSJared McNeill 
119b738dafdSJared McNeill struct uart_class uart_snps_class = {
120b738dafdSJared McNeill 	"snps",
121b738dafdSJared McNeill 	snps_methods,
122b738dafdSJared McNeill 	sizeof(struct snps_softc),
123b738dafdSJared McNeill 	.uc_ops = &uart_ns8250_ops,
124b738dafdSJared McNeill 	.uc_range = 8,
125b738dafdSJared McNeill 	.uc_rclk = 0,
126b738dafdSJared McNeill };
127b738dafdSJared McNeill 
128b738dafdSJared McNeill static struct ofw_compat_data compat_data[] = {
129b738dafdSJared McNeill 	{ "snps,dw-apb-uart",		(uintptr_t)&uart_snps_class },
130*0693b1d2SMarcin Wojtas 	{ "marvell,armada-38x-uart",	(uintptr_t)&uart_snps_class },
131b738dafdSJared McNeill 	{ NULL,				(uintptr_t)NULL }
132b738dafdSJared McNeill };
13327708858SRuslan Bukin UART_FDT_CLASS(compat_data);
134b738dafdSJared McNeill 
135b738dafdSJared McNeill #ifdef EXT_RESOURCES
136b738dafdSJared McNeill static int
snps_get_clocks(device_t dev,clk_t * baudclk,clk_t * apb_pclk)137b738dafdSJared McNeill snps_get_clocks(device_t dev, clk_t *baudclk, clk_t *apb_pclk)
138b738dafdSJared McNeill {
139b738dafdSJared McNeill 
140b738dafdSJared McNeill 	*baudclk = NULL;
141b738dafdSJared McNeill 	*apb_pclk = NULL;
142b738dafdSJared McNeill 
143b738dafdSJared McNeill 	/* Baud clock is either named "baudclk", or there is a single
144b738dafdSJared McNeill 	 * unnamed clock.
145b738dafdSJared McNeill 	 */
146dac93553SMichal Meloun 	if (clk_get_by_ofw_name(dev, 0, "baudclk", baudclk) != 0 &&
147dac93553SMichal Meloun 	    clk_get_by_ofw_index(dev, 0, 0, baudclk) != 0)
148b738dafdSJared McNeill 		return (ENOENT);
149b738dafdSJared McNeill 
150b738dafdSJared McNeill 	/* APB peripheral clock is optional */
151dac93553SMichal Meloun 	(void)clk_get_by_ofw_name(dev, 0, "apb_pclk", apb_pclk);
152b738dafdSJared McNeill 
153b738dafdSJared McNeill 	return (0);
154b738dafdSJared McNeill }
155b738dafdSJared McNeill #endif
156b738dafdSJared McNeill 
157b738dafdSJared McNeill static int
snps_probe(device_t dev)158b738dafdSJared McNeill snps_probe(device_t dev)
159b738dafdSJared McNeill {
160b738dafdSJared McNeill 	struct snps_softc *sc;
161b738dafdSJared McNeill 	struct uart_class *uart_class;
162b738dafdSJared McNeill 	phandle_t node;
163c214a270SRuslan Bukin 	uint32_t shift, iowidth, clock;
164b738dafdSJared McNeill 	uint64_t freq;
165b738dafdSJared McNeill 	int error;
166b738dafdSJared McNeill #ifdef EXT_RESOURCES
167b738dafdSJared McNeill 	clk_t baudclk, apb_pclk;
168b738dafdSJared McNeill 	hwreset_t reset;
169b738dafdSJared McNeill #endif
170b738dafdSJared McNeill 
171b738dafdSJared McNeill 	if (!ofw_bus_status_okay(dev))
172b738dafdSJared McNeill 		return (ENXIO);
173b738dafdSJared McNeill 
174b738dafdSJared McNeill 	uart_class = (struct uart_class *)ofw_bus_search_compatible(dev,
175b738dafdSJared McNeill 	    compat_data)->ocd_data;
176b738dafdSJared McNeill 	if (uart_class == NULL)
177b738dafdSJared McNeill 		return (ENXIO);
178b738dafdSJared McNeill 
179b738dafdSJared McNeill 	freq = 0;
180b738dafdSJared McNeill 	sc = device_get_softc(dev);
181b738dafdSJared McNeill 	sc->ns8250.base.sc_class = uart_class;
182b738dafdSJared McNeill 
183b738dafdSJared McNeill 	node = ofw_bus_get_node(dev);
184b738dafdSJared McNeill 	if (OF_getencprop(node, "reg-shift", &shift, sizeof(shift)) <= 0)
185b738dafdSJared McNeill 		shift = 0;
186c214a270SRuslan Bukin 	if (OF_getencprop(node, "reg-io-width", &iowidth, sizeof(iowidth)) <= 0)
187c214a270SRuslan Bukin 		iowidth = 1;
188b738dafdSJared McNeill 	if (OF_getencprop(node, "clock-frequency", &clock, sizeof(clock)) <= 0)
189b738dafdSJared McNeill 		clock = 0;
190b738dafdSJared McNeill 
191b738dafdSJared McNeill #ifdef EXT_RESOURCES
192dac93553SMichal Meloun 	if (hwreset_get_by_ofw_idx(dev, 0, 0, &reset) == 0) {
193b738dafdSJared McNeill 		error = hwreset_deassert(reset);
194b738dafdSJared McNeill 		if (error != 0) {
195b738dafdSJared McNeill 			device_printf(dev, "cannot de-assert reset\n");
196b738dafdSJared McNeill 			return (error);
197b738dafdSJared McNeill 		}
198b738dafdSJared McNeill 	}
199b738dafdSJared McNeill 
200b738dafdSJared McNeill 	if (snps_get_clocks(dev, &baudclk, &apb_pclk) == 0) {
201b738dafdSJared McNeill 		error = clk_enable(baudclk);
202b738dafdSJared McNeill 		if (error != 0) {
203b738dafdSJared McNeill 			device_printf(dev, "cannot enable baud clock\n");
204b738dafdSJared McNeill 			return (error);
205b738dafdSJared McNeill 		}
206b738dafdSJared McNeill 		if (apb_pclk != NULL) {
207b738dafdSJared McNeill 			error = clk_enable(apb_pclk);
208b738dafdSJared McNeill 			if (error != 0) {
209b738dafdSJared McNeill 				device_printf(dev,
210b738dafdSJared McNeill 				    "cannot enable peripheral clock\n");
211b738dafdSJared McNeill 				return (error);
212b738dafdSJared McNeill 			}
213b738dafdSJared McNeill 		}
214b738dafdSJared McNeill 
215b738dafdSJared McNeill 		if (clock == 0) {
216b738dafdSJared McNeill 			error = clk_get_freq(baudclk, &freq);
217b738dafdSJared McNeill 			if (error != 0) {
218b738dafdSJared McNeill 				device_printf(dev, "cannot get frequency\n");
219b738dafdSJared McNeill 				return (error);
220b738dafdSJared McNeill 			}
221b738dafdSJared McNeill 			clock = (uint32_t)freq;
222b738dafdSJared McNeill 		}
223b738dafdSJared McNeill 	}
224b738dafdSJared McNeill #endif
225b738dafdSJared McNeill 
226b738dafdSJared McNeill 	if (bootverbose && clock == 0)
227b738dafdSJared McNeill 		device_printf(dev, "could not determine frequency\n");
228b738dafdSJared McNeill 
229381388b9SMatt Macy 	error = uart_bus_probe(dev, (int)shift, (int)iowidth, (int)clock, 0, 0, UART_F_BUSY_DETECT);
230b738dafdSJared McNeill 	if (error != 0)
231b738dafdSJared McNeill 		return (error);
232b738dafdSJared McNeill 
233b738dafdSJared McNeill #ifdef EXT_RESOURCES
234b738dafdSJared McNeill 	/* XXX uart_bus_probe has changed the softc, so refresh it */
235b738dafdSJared McNeill 	sc = device_get_softc(dev);
236b738dafdSJared McNeill 
237b738dafdSJared McNeill 	/* Store clock and reset handles for detach */
238b738dafdSJared McNeill 	sc->baudclk = baudclk;
239b738dafdSJared McNeill 	sc->apb_pclk = apb_pclk;
240b738dafdSJared McNeill 	sc->reset = reset;
241b738dafdSJared McNeill #endif
242b738dafdSJared McNeill 
243b738dafdSJared McNeill 	return (0);
244b738dafdSJared McNeill }
245b738dafdSJared McNeill 
246b738dafdSJared McNeill static int
snps_detach(device_t dev)247b738dafdSJared McNeill snps_detach(device_t dev)
248b738dafdSJared McNeill {
249b738dafdSJared McNeill #ifdef EXT_RESOURCES
250b738dafdSJared McNeill 	struct snps_softc *sc;
251b738dafdSJared McNeill 	clk_t baudclk, apb_pclk;
252b738dafdSJared McNeill 	hwreset_t reset;
253b738dafdSJared McNeill #endif
254b738dafdSJared McNeill 	int error;
255b738dafdSJared McNeill 
256b738dafdSJared McNeill #ifdef EXT_RESOURCES
257b738dafdSJared McNeill 	sc = device_get_softc(dev);
258b738dafdSJared McNeill 	baudclk = sc->baudclk;
259b738dafdSJared McNeill 	apb_pclk = sc->apb_pclk;
260b738dafdSJared McNeill 	reset = sc->reset;
261b738dafdSJared McNeill #endif
262b738dafdSJared McNeill 
263b738dafdSJared McNeill 	error = uart_bus_detach(dev);
264b738dafdSJared McNeill 	if (error != 0)
265b738dafdSJared McNeill 		return (error);
266b738dafdSJared McNeill 
267b738dafdSJared McNeill #ifdef EXT_RESOURCES
268b738dafdSJared McNeill 	if (reset != NULL) {
269b738dafdSJared McNeill 		error = hwreset_assert(reset);
270b738dafdSJared McNeill 		if (error != 0) {
271b738dafdSJared McNeill 			device_printf(dev, "cannot assert reset\n");
272b738dafdSJared McNeill 			return (error);
273b738dafdSJared McNeill 		}
274b738dafdSJared McNeill 		hwreset_release(reset);
275b738dafdSJared McNeill 	}
276b738dafdSJared McNeill 	if (apb_pclk != NULL) {
277b738dafdSJared McNeill 		error = clk_release(apb_pclk);
278b738dafdSJared McNeill 		if (error != 0) {
279b738dafdSJared McNeill 			device_printf(dev, "cannot release peripheral clock\n");
280b738dafdSJared McNeill 			return (error);
281b738dafdSJared McNeill 		}
282b738dafdSJared McNeill 	}
283b738dafdSJared McNeill 	if (baudclk != NULL) {
284b738dafdSJared McNeill 		error = clk_release(baudclk);
285b738dafdSJared McNeill 		if (error != 0) {
286b738dafdSJared McNeill 			device_printf(dev, "cannot release baud clock\n");
287b738dafdSJared McNeill 			return (error);
288b738dafdSJared McNeill 		}
289b738dafdSJared McNeill 	}
290b738dafdSJared McNeill #endif
291b738dafdSJared McNeill 
292b738dafdSJared McNeill 	return (0);
293b738dafdSJared McNeill }
294b738dafdSJared McNeill 
295b738dafdSJared McNeill static device_method_t snps_bus_methods[] = {
296b738dafdSJared McNeill 	/* Device interface */
297b738dafdSJared McNeill 	DEVMETHOD(device_probe,		snps_probe),
298b738dafdSJared McNeill 	DEVMETHOD(device_attach,	uart_bus_attach),
299b738dafdSJared McNeill 	DEVMETHOD(device_detach, 	snps_detach),
300b738dafdSJared McNeill 	DEVMETHOD_END
301b738dafdSJared McNeill };
302b738dafdSJared McNeill 
303b738dafdSJared McNeill static driver_t snps_uart_driver = {
304b738dafdSJared McNeill 	uart_driver_name,
305b738dafdSJared McNeill 	snps_bus_methods,
306b738dafdSJared McNeill 	sizeof(struct snps_softc)
307b738dafdSJared McNeill };
308b738dafdSJared McNeill 
309b738dafdSJared McNeill DRIVER_MODULE(uart_snps, simplebus, snps_uart_driver, uart_devclass, 0, 0);
310