xref: /freebsd-14.2/sys/dev/uart/uart_cpu_fdt.c (revision d5ad1d0d)
1cb34ed44SMarcel Moolenaar /*-
2cb34ed44SMarcel Moolenaar  * Copyright (c) 2009-2010 The FreeBSD Foundation
3cb34ed44SMarcel Moolenaar  * All rights reserved.
4cb34ed44SMarcel Moolenaar  *
5cb34ed44SMarcel Moolenaar  * This software was developed by Semihalf under sponsorship from
6cb34ed44SMarcel Moolenaar  * the FreeBSD Foundation.
7cb34ed44SMarcel Moolenaar  *
8cb34ed44SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
9cb34ed44SMarcel Moolenaar  * modification, are permitted provided that the following conditions
10cb34ed44SMarcel Moolenaar  * are met:
11cb34ed44SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
12cb34ed44SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
13cb34ed44SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
14cb34ed44SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
15cb34ed44SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
16cb34ed44SMarcel Moolenaar  *
17cb34ed44SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18cb34ed44SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19cb34ed44SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20cb34ed44SMarcel Moolenaar  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21cb34ed44SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22cb34ed44SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23cb34ed44SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24cb34ed44SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25cb34ed44SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26cb34ed44SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27cb34ed44SMarcel Moolenaar  * SUCH DAMAGE.
28cb34ed44SMarcel Moolenaar  */
29cb34ed44SMarcel Moolenaar 
30cb34ed44SMarcel Moolenaar #include <sys/cdefs.h>
31cb34ed44SMarcel Moolenaar __FBSDID("$FreeBSD$");
32cb34ed44SMarcel Moolenaar 
3364958185SIan Lepore #include "opt_platform.h"
3464958185SIan Lepore 
35cb34ed44SMarcel Moolenaar #include <sys/param.h>
36cb34ed44SMarcel Moolenaar #include <sys/bus.h>
37cb34ed44SMarcel Moolenaar #include <sys/kernel.h>
38cb34ed44SMarcel Moolenaar #include <sys/module.h>
39dce533f3SNathan Whitehorn #include <sys/systm.h>
40cb34ed44SMarcel Moolenaar 
41c20809e1SNathan Whitehorn #include <vm/vm.h>
42c20809e1SNathan Whitehorn #include <vm/pmap.h>
43c20809e1SNathan Whitehorn 
44cb34ed44SMarcel Moolenaar #include <machine/bus.h>
45be3b046cSAndrew Turner #ifndef __aarch64__
46cb34ed44SMarcel Moolenaar #include <machine/fdt.h>
47be3b046cSAndrew Turner #endif
48cb34ed44SMarcel Moolenaar 
49cb34ed44SMarcel Moolenaar #include <dev/fdt/fdt_common.h>
50cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus.h>
51cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus_subr.h>
52cb34ed44SMarcel Moolenaar #include <dev/uart/uart.h>
53cb34ed44SMarcel Moolenaar #include <dev/uart/uart_bus.h>
54cb34ed44SMarcel Moolenaar #include <dev/uart/uart_cpu.h>
55bd69e3adSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
56cb34ed44SMarcel Moolenaar 
57be3b046cSAndrew Turner #ifdef __aarch64__
58be3b046cSAndrew Turner extern bus_space_tag_t fdtbus_bs_tag;
59be3b046cSAndrew Turner #endif
60be3b046cSAndrew Turner 
61cb34ed44SMarcel Moolenaar /*
62cb34ed44SMarcel Moolenaar  * UART console routines.
63cb34ed44SMarcel Moolenaar  */
64cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_io;
65cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_mem;
66cb34ed44SMarcel Moolenaar 
6706d8b1edSAndrew Turner int
68cb34ed44SMarcel Moolenaar uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
69cb34ed44SMarcel Moolenaar {
70cb34ed44SMarcel Moolenaar 
714162dff9SAleksandr Rybalko 	if (b1->bst != b2->bst)
724162dff9SAleksandr Rybalko 		return (0);
734162dff9SAleksandr Rybalko 	if (pmap_kextract(b1->bsh) == 0)
744162dff9SAleksandr Rybalko 		return (0);
754162dff9SAleksandr Rybalko 	if (pmap_kextract(b2->bsh) == 0)
764162dff9SAleksandr Rybalko 		return (0);
774162dff9SAleksandr Rybalko 	return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0);
78cb34ed44SMarcel Moolenaar }
79cb34ed44SMarcel Moolenaar 
80dce533f3SNathan Whitehorn static int
81dce533f3SNathan Whitehorn phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node)
82dce533f3SNathan Whitehorn {
83dce533f3SNathan Whitehorn 	char buf[64];
84dce533f3SNathan Whitehorn 
85dce533f3SNathan Whitehorn 	if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0)
86dce533f3SNathan Whitehorn 		return (ENXIO);
87dce533f3SNathan Whitehorn 	if ((*node = OF_finddevice(buf)) == -1)
88dce533f3SNathan Whitehorn 		return (ENXIO);
89dce533f3SNathan Whitehorn 
90dce533f3SNathan Whitehorn 	return (0);
91dce533f3SNathan Whitehorn }
92dce533f3SNathan Whitehorn 
93bd69e3adSIan Lepore static const struct ofw_compat_data *
94bd69e3adSIan Lepore uart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd)
95bd69e3adSIan Lepore {
96bd69e3adSIan Lepore 	const struct ofw_compat_data *ocd;
97bd69e3adSIan Lepore 
98bd69e3adSIan Lepore 	for (ocd = cd; ocd->ocd_str != NULL; ocd++) {
99bd69e3adSIan Lepore 		if (fdt_is_compatible(node, ocd->ocd_str))
100bd69e3adSIan Lepore 			return (ocd);
101bd69e3adSIan Lepore 	}
102bd69e3adSIan Lepore 	return (NULL);
103bd69e3adSIan Lepore }
104bd69e3adSIan Lepore 
105bd69e3adSIan Lepore static uintptr_t
106bd69e3adSIan Lepore uart_fdt_find_by_node(phandle_t node, int class_list)
107bd69e3adSIan Lepore {
108bd69e3adSIan Lepore 	struct ofw_compat_data **cd;
109bd69e3adSIan Lepore 	const struct ofw_compat_data *ocd;
110bd69e3adSIan Lepore 
111bd69e3adSIan Lepore 	if (class_list) {
112bd69e3adSIan Lepore 		SET_FOREACH(cd, uart_fdt_class_set) {
113bd69e3adSIan Lepore 			ocd = uart_fdt_find_compatible(node, *cd);
114bd69e3adSIan Lepore 			if ((ocd != NULL) && (ocd->ocd_data != 0))
115bd69e3adSIan Lepore 				return (ocd->ocd_data);
116bd69e3adSIan Lepore 		}
117bd69e3adSIan Lepore 	} else {
118bd69e3adSIan Lepore 		SET_FOREACH(cd, uart_fdt_class_and_device_set) {
119bd69e3adSIan Lepore 			ocd = uart_fdt_find_compatible(node, *cd);
120bd69e3adSIan Lepore 			if ((ocd != NULL) && (ocd->ocd_data != 0))
121bd69e3adSIan Lepore 				return (ocd->ocd_data);
122bd69e3adSIan Lepore 		}
123bd69e3adSIan Lepore 	}
124bd69e3adSIan Lepore 	return (0);
125bd69e3adSIan Lepore }
126bd69e3adSIan Lepore 
127cb34ed44SMarcel Moolenaar int
128cb34ed44SMarcel Moolenaar uart_cpu_getdev(int devtype, struct uart_devinfo *di)
129cb34ed44SMarcel Moolenaar {
130dce533f3SNathan Whitehorn 	const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout",
131dce533f3SNathan Whitehorn 	    "stdin-path", "stdin", NULL};
132dce533f3SNathan Whitehorn 	const char **name;
133cb34ed44SMarcel Moolenaar 	struct uart_class *class;
134cb34ed44SMarcel Moolenaar 	phandle_t node, chosen;
135cb34ed44SMarcel Moolenaar 	pcell_t shift, br, rclk;
136cb34ed44SMarcel Moolenaar 	u_long start, size, pbase, psize;
137*d5ad1d0dSAndrew Turner 	char *cp;
138cb34ed44SMarcel Moolenaar 	int err;
139cb34ed44SMarcel Moolenaar 
140cb34ed44SMarcel Moolenaar 	uart_bus_space_mem = fdtbus_bs_tag;
141cb34ed44SMarcel Moolenaar 	uart_bus_space_io = NULL;
142cb34ed44SMarcel Moolenaar 
143dce533f3SNathan Whitehorn 	/* Allow overriding the FDT using the environment. */
144cb34ed44SMarcel Moolenaar 	class = &uart_ns8250_class;
145cb34ed44SMarcel Moolenaar 	err = uart_getenv(devtype, di, class);
146cb34ed44SMarcel Moolenaar 	if (!err)
147cb34ed44SMarcel Moolenaar 		return (0);
148cb34ed44SMarcel Moolenaar 
149cb34ed44SMarcel Moolenaar 	if (devtype != UART_DEV_CONSOLE)
150cb34ed44SMarcel Moolenaar 		return (ENXIO);
151cb34ed44SMarcel Moolenaar 
152*d5ad1d0dSAndrew Turner 	/* Has the user forced a specific device node? */
153*d5ad1d0dSAndrew Turner 	cp = kern_getenv("hw.fdt.console");
154*d5ad1d0dSAndrew Turner 	if (cp == NULL) {
155cb34ed44SMarcel Moolenaar 		/*
156cb34ed44SMarcel Moolenaar 		 * Retrieve /chosen/std{in,out}.
157cb34ed44SMarcel Moolenaar 		 */
15894f33b0aSNathan Whitehorn 		node = -1;
15994f33b0aSNathan Whitehorn 		if ((chosen = OF_finddevice("/chosen")) != -1) {
160dce533f3SNathan Whitehorn 			for (name = propnames; *name != NULL; name++) {
161*d5ad1d0dSAndrew Turner 				if (phandle_chosen_propdev(chosen, *name,
162*d5ad1d0dSAndrew Turner 				    &node) == 0)
163dce533f3SNathan Whitehorn 					break;
164dce533f3SNathan Whitehorn 			}
16594f33b0aSNathan Whitehorn 		}
16694f33b0aSNathan Whitehorn 		if (chosen == -1 || *name == NULL)
16794f33b0aSNathan Whitehorn 			node = OF_finddevice("serial0"); /* Last ditch */
168*d5ad1d0dSAndrew Turner 	} else {
169*d5ad1d0dSAndrew Turner 		node = OF_finddevice(cp);
170*d5ad1d0dSAndrew Turner 	}
17194f33b0aSNathan Whitehorn 
17294f33b0aSNathan Whitehorn 	if (node == -1) /* Can't find anything */
173cb34ed44SMarcel Moolenaar 		return (ENXIO);
17494f33b0aSNathan Whitehorn 
175cb34ed44SMarcel Moolenaar 	/*
176bd69e3adSIan Lepore 	 * Check old style of UART definition first. Unfortunately, the common
177bd69e3adSIan Lepore 	 * FDT processing is not possible if we have clock, power domains and
178bd69e3adSIan Lepore 	 * pinmux stuff.
179bd69e3adSIan Lepore 	 */
180bd69e3adSIan Lepore 	class = (struct uart_class *)uart_fdt_find_by_node(node, 0);
181bd69e3adSIan Lepore 	if (class != NULL) {
182cb34ed44SMarcel Moolenaar 		if ((err = uart_fdt_get_clock(node, &rclk)) != 0)
183cb34ed44SMarcel Moolenaar 			return (err);
184bd69e3adSIan Lepore 	} else {
185bd69e3adSIan Lepore 		/* Check class only linker set */
186bd69e3adSIan Lepore 		class =
187bd69e3adSIan Lepore 		    (struct uart_class *)uart_fdt_find_by_node(node, 1);
188bd69e3adSIan Lepore 		if (class == NULL)
189bd69e3adSIan Lepore 			return (ENXIO);
190bd69e3adSIan Lepore 		rclk = 0;
191bd69e3adSIan Lepore 	}
192bd69e3adSIan Lepore 
193cb34ed44SMarcel Moolenaar 	/*
194405ada37SAndrew Turner 	 * Retrieve serial attributes.
195405ada37SAndrew Turner 	 */
196405ada37SAndrew Turner 	if (uart_fdt_get_shift(node, &shift) != 0)
197405ada37SAndrew Turner 		shift = uart_getregshift(class);
198405ada37SAndrew Turner 
199405ada37SAndrew Turner 	if (OF_getprop(node, "current-speed", &br, sizeof(br)) <= 0)
200405ada37SAndrew Turner 		br = 0;
201405ada37SAndrew Turner 	else
202405ada37SAndrew Turner 		br = fdt32_to_cpu(br);
203405ada37SAndrew Turner 
204405ada37SAndrew Turner 	/*
205cb34ed44SMarcel Moolenaar 	 * Finalize configuration.
206cb34ed44SMarcel Moolenaar 	 */
207cb34ed44SMarcel Moolenaar 	di->bas.chan = 0;
208cb34ed44SMarcel Moolenaar 	di->bas.regshft = (u_int)shift;
209cb34ed44SMarcel Moolenaar 	di->baudrate = br;
210cb34ed44SMarcel Moolenaar 	di->bas.rclk = (u_int)rclk;
211cb34ed44SMarcel Moolenaar 	di->ops = uart_getops(class);
212cb34ed44SMarcel Moolenaar 	di->databits = 8;
213cb34ed44SMarcel Moolenaar 	di->stopbits = 1;
214cb34ed44SMarcel Moolenaar 	di->parity = UART_PARITY_NONE;
215cb34ed44SMarcel Moolenaar 	di->bas.bst = uart_bus_space_mem;
216cb34ed44SMarcel Moolenaar 
217cb34ed44SMarcel Moolenaar 	err = fdt_regsize(node, &start, &size);
218cb34ed44SMarcel Moolenaar 	if (err)
219cb34ed44SMarcel Moolenaar 		return (ENXIO);
220cb34ed44SMarcel Moolenaar 	err = fdt_get_range(OF_parent(node), 0, &pbase, &psize);
221cb34ed44SMarcel Moolenaar 	if (err)
222cb34ed44SMarcel Moolenaar 		pbase = 0;
223cb34ed44SMarcel Moolenaar 
224cb34ed44SMarcel Moolenaar 	start += pbase;
225cb34ed44SMarcel Moolenaar 
226cb34ed44SMarcel Moolenaar 	return (bus_space_map(di->bas.bst, start, size, 0, &di->bas.bsh));
227cb34ed44SMarcel Moolenaar }
228