xref: /freebsd-14.2/sys/dev/uart/uart_cpu_fdt.c (revision 3d2a63f5)
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>
45cb34ed44SMarcel Moolenaar #include <machine/fdt.h>
46cb34ed44SMarcel Moolenaar 
47cb34ed44SMarcel Moolenaar #include <dev/fdt/fdt_common.h>
48cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus.h>
49cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus_subr.h>
50cb34ed44SMarcel Moolenaar #include <dev/uart/uart.h>
51cb34ed44SMarcel Moolenaar #include <dev/uart/uart_bus.h>
52cb34ed44SMarcel Moolenaar #include <dev/uart/uart_cpu.h>
53bd69e3adSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
54cb34ed44SMarcel Moolenaar 
55cb34ed44SMarcel Moolenaar /*
56cb34ed44SMarcel Moolenaar  * UART console routines.
57cb34ed44SMarcel Moolenaar  */
58cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_io;
59cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_mem;
60cb34ed44SMarcel Moolenaar 
61cb34ed44SMarcel Moolenaar static int
62cb34ed44SMarcel Moolenaar uart_fdt_get_clock(phandle_t node, pcell_t *cell)
63cb34ed44SMarcel Moolenaar {
64cb34ed44SMarcel Moolenaar 	pcell_t clock;
65cb34ed44SMarcel Moolenaar 
6665e79cf4SWarner Losh 	/* clock-frequency is a FreeBSD-only extention. */
67cb34ed44SMarcel Moolenaar 	if ((OF_getprop(node, "clock-frequency", &clock,
68cb34ed44SMarcel Moolenaar 	    sizeof(clock))) <= 0)
6965e79cf4SWarner Losh 		clock = 0;
70cb34ed44SMarcel Moolenaar 
71cb34ed44SMarcel Moolenaar 	if (clock == 0)
72cb34ed44SMarcel Moolenaar 		/* Try to retrieve parent 'bus-frequency' */
73cb34ed44SMarcel Moolenaar 		/* XXX this should go to simple-bus fixup or so */
74cb34ed44SMarcel Moolenaar 		if ((OF_getprop(OF_parent(node), "bus-frequency", &clock,
75cb34ed44SMarcel Moolenaar 		    sizeof(clock))) <= 0)
76cb34ed44SMarcel Moolenaar 			clock = 0;
77cb34ed44SMarcel Moolenaar 
78cb34ed44SMarcel Moolenaar 	*cell = fdt32_to_cpu(clock);
79cb34ed44SMarcel Moolenaar 	return (0);
80cb34ed44SMarcel Moolenaar }
81cb34ed44SMarcel Moolenaar 
82cb34ed44SMarcel Moolenaar static int
83cb34ed44SMarcel Moolenaar uart_fdt_get_shift(phandle_t node, pcell_t *cell)
84cb34ed44SMarcel Moolenaar {
85cb34ed44SMarcel Moolenaar 
86*3d2a63f5SAndrew Turner 	if ((OF_getencprop(node, "reg-shift", cell, sizeof(*cell))) <= 0)
87*3d2a63f5SAndrew Turner 		*cell = 0;
88cb34ed44SMarcel Moolenaar 	return (0);
89cb34ed44SMarcel Moolenaar }
90cb34ed44SMarcel Moolenaar 
91cb34ed44SMarcel Moolenaar int
92cb34ed44SMarcel Moolenaar uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
93cb34ed44SMarcel Moolenaar {
94cb34ed44SMarcel Moolenaar 
954162dff9SAleksandr Rybalko 	if (b1->bst != b2->bst)
964162dff9SAleksandr Rybalko 		return (0);
974162dff9SAleksandr Rybalko 	if (pmap_kextract(b1->bsh) == 0)
984162dff9SAleksandr Rybalko 		return (0);
994162dff9SAleksandr Rybalko 	if (pmap_kextract(b2->bsh) == 0)
1004162dff9SAleksandr Rybalko 		return (0);
1014162dff9SAleksandr Rybalko 	return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0);
102cb34ed44SMarcel Moolenaar }
103cb34ed44SMarcel Moolenaar 
104dce533f3SNathan Whitehorn static int
105dce533f3SNathan Whitehorn phandle_chosen_propdev(phandle_t chosen, const char *name, phandle_t *node)
106dce533f3SNathan Whitehorn {
107dce533f3SNathan Whitehorn 	char buf[64];
108dce533f3SNathan Whitehorn 
109dce533f3SNathan Whitehorn 	if (OF_getprop(chosen, name, buf, sizeof(buf)) <= 0)
110dce533f3SNathan Whitehorn 		return (ENXIO);
111dce533f3SNathan Whitehorn 	if ((*node = OF_finddevice(buf)) == -1)
112dce533f3SNathan Whitehorn 		return (ENXIO);
113dce533f3SNathan Whitehorn 
114dce533f3SNathan Whitehorn 	return (0);
115dce533f3SNathan Whitehorn }
116dce533f3SNathan Whitehorn 
117bd69e3adSIan Lepore static const struct ofw_compat_data *
118bd69e3adSIan Lepore uart_fdt_find_compatible(phandle_t node, const struct ofw_compat_data *cd)
119bd69e3adSIan Lepore {
120bd69e3adSIan Lepore 	const struct ofw_compat_data *ocd;
121bd69e3adSIan Lepore 
122bd69e3adSIan Lepore 	for (ocd = cd; ocd->ocd_str != NULL; ocd++) {
123bd69e3adSIan Lepore 		if (fdt_is_compatible(node, ocd->ocd_str))
124bd69e3adSIan Lepore 			return (ocd);
125bd69e3adSIan Lepore 	}
126bd69e3adSIan Lepore 	return (NULL);
127bd69e3adSIan Lepore }
128bd69e3adSIan Lepore 
129bd69e3adSIan Lepore static uintptr_t
130bd69e3adSIan Lepore uart_fdt_find_by_node(phandle_t node, int class_list)
131bd69e3adSIan Lepore {
132bd69e3adSIan Lepore 	struct ofw_compat_data **cd;
133bd69e3adSIan Lepore 	const struct ofw_compat_data *ocd;
134bd69e3adSIan Lepore 
135bd69e3adSIan Lepore 	if (class_list) {
136bd69e3adSIan Lepore 		SET_FOREACH(cd, uart_fdt_class_set) {
137bd69e3adSIan Lepore 			ocd = uart_fdt_find_compatible(node, *cd);
138bd69e3adSIan Lepore 			if ((ocd != NULL) && (ocd->ocd_data != 0))
139bd69e3adSIan Lepore 				return (ocd->ocd_data);
140bd69e3adSIan Lepore 		}
141bd69e3adSIan Lepore 	} else {
142bd69e3adSIan Lepore 		SET_FOREACH(cd, uart_fdt_class_and_device_set) {
143bd69e3adSIan Lepore 			ocd = uart_fdt_find_compatible(node, *cd);
144bd69e3adSIan Lepore 			if ((ocd != NULL) && (ocd->ocd_data != 0))
145bd69e3adSIan Lepore 				return (ocd->ocd_data);
146bd69e3adSIan Lepore 		}
147bd69e3adSIan Lepore 	}
148bd69e3adSIan Lepore 	return (0);
149bd69e3adSIan Lepore }
150bd69e3adSIan Lepore 
151cb34ed44SMarcel Moolenaar int
152cb34ed44SMarcel Moolenaar uart_cpu_getdev(int devtype, struct uart_devinfo *di)
153cb34ed44SMarcel Moolenaar {
154dce533f3SNathan Whitehorn 	const char *propnames[] = {"stdout-path", "linux,stdout-path", "stdout",
155dce533f3SNathan Whitehorn 	    "stdin-path", "stdin", NULL};
156dce533f3SNathan Whitehorn 	const char **name;
157cb34ed44SMarcel Moolenaar 	struct uart_class *class;
158cb34ed44SMarcel Moolenaar 	phandle_t node, chosen;
159cb34ed44SMarcel Moolenaar 	pcell_t shift, br, rclk;
160cb34ed44SMarcel Moolenaar 	u_long start, size, pbase, psize;
161cb34ed44SMarcel Moolenaar 	int err;
162cb34ed44SMarcel Moolenaar 
163cb34ed44SMarcel Moolenaar 	uart_bus_space_mem = fdtbus_bs_tag;
164cb34ed44SMarcel Moolenaar 	uart_bus_space_io = NULL;
165cb34ed44SMarcel Moolenaar 
166dce533f3SNathan Whitehorn 	/* Allow overriding the FDT using the environment. */
167cb34ed44SMarcel Moolenaar 	class = &uart_ns8250_class;
168cb34ed44SMarcel Moolenaar 	err = uart_getenv(devtype, di, class);
169cb34ed44SMarcel Moolenaar 	if (!err)
170cb34ed44SMarcel Moolenaar 		return (0);
171cb34ed44SMarcel Moolenaar 
172cb34ed44SMarcel Moolenaar 	if (devtype != UART_DEV_CONSOLE)
173cb34ed44SMarcel Moolenaar 		return (ENXIO);
174cb34ed44SMarcel Moolenaar 
175cb34ed44SMarcel Moolenaar 	/*
176cb34ed44SMarcel Moolenaar 	 * Retrieve /chosen/std{in,out}.
177cb34ed44SMarcel Moolenaar 	 */
17894f33b0aSNathan Whitehorn 	node = -1;
17994f33b0aSNathan Whitehorn 	if ((chosen = OF_finddevice("/chosen")) != -1) {
180dce533f3SNathan Whitehorn 		for (name = propnames; *name != NULL; name++) {
181dce533f3SNathan Whitehorn 			if (phandle_chosen_propdev(chosen, *name, &node) == 0)
182dce533f3SNathan Whitehorn 				break;
183dce533f3SNathan Whitehorn 		}
18494f33b0aSNathan Whitehorn 	}
18594f33b0aSNathan Whitehorn 	if (chosen == -1 || *name == NULL)
18694f33b0aSNathan Whitehorn 		node = OF_finddevice("serial0"); /* Last ditch */
18794f33b0aSNathan Whitehorn 
18894f33b0aSNathan Whitehorn 	if (node == -1) /* Can't find anything */
189cb34ed44SMarcel Moolenaar 		return (ENXIO);
19094f33b0aSNathan Whitehorn 
191cb34ed44SMarcel Moolenaar 	/*
192cb34ed44SMarcel Moolenaar 	 * Retrieve serial attributes.
193cb34ed44SMarcel Moolenaar 	 */
194cb34ed44SMarcel Moolenaar 	uart_fdt_get_shift(node, &shift);
195cb34ed44SMarcel Moolenaar 	if (OF_getprop(node, "current-speed", &br, sizeof(br)) <= 0)
196cb34ed44SMarcel Moolenaar 		br = 0;
197bd69e3adSIan Lepore 	else
198cb34ed44SMarcel Moolenaar 		br = fdt32_to_cpu(br);
199cb34ed44SMarcel Moolenaar 
200bd69e3adSIan Lepore 	/*
201bd69e3adSIan Lepore 	 * Check old style of UART definition first. Unfortunately, the common
202bd69e3adSIan Lepore 	 * FDT processing is not possible if we have clock, power domains and
203bd69e3adSIan Lepore 	 * pinmux stuff.
204bd69e3adSIan Lepore 	 */
205bd69e3adSIan Lepore 	class = (struct uart_class *)uart_fdt_find_by_node(node, 0);
206bd69e3adSIan Lepore 	if (class != NULL) {
207cb34ed44SMarcel Moolenaar 		if ((err = uart_fdt_get_clock(node, &rclk)) != 0)
208cb34ed44SMarcel Moolenaar 			return (err);
209bd69e3adSIan Lepore 	} else {
210bd69e3adSIan Lepore 		/* Check class only linker set */
211bd69e3adSIan Lepore 		class =
212bd69e3adSIan Lepore 		    (struct uart_class *)uart_fdt_find_by_node(node, 1);
213bd69e3adSIan Lepore 		if (class == NULL)
214bd69e3adSIan Lepore 			return (ENXIO);
215bd69e3adSIan Lepore 		rclk = 0;
216bd69e3adSIan Lepore 	}
217bd69e3adSIan Lepore 
218cb34ed44SMarcel Moolenaar 	/*
219cb34ed44SMarcel Moolenaar 	 * Finalize configuration.
220cb34ed44SMarcel Moolenaar 	 */
221cb34ed44SMarcel Moolenaar 	di->bas.chan = 0;
222cb34ed44SMarcel Moolenaar 	di->bas.regshft = (u_int)shift;
223cb34ed44SMarcel Moolenaar 	di->baudrate = br;
224cb34ed44SMarcel Moolenaar 	di->bas.rclk = (u_int)rclk;
225cb34ed44SMarcel Moolenaar 	di->ops = uart_getops(class);
226cb34ed44SMarcel Moolenaar 	di->databits = 8;
227cb34ed44SMarcel Moolenaar 	di->stopbits = 1;
228cb34ed44SMarcel Moolenaar 	di->parity = UART_PARITY_NONE;
229cb34ed44SMarcel Moolenaar 	di->bas.bst = uart_bus_space_mem;
230cb34ed44SMarcel Moolenaar 
231cb34ed44SMarcel Moolenaar 	err = fdt_regsize(node, &start, &size);
232cb34ed44SMarcel Moolenaar 	if (err)
233cb34ed44SMarcel Moolenaar 		return (ENXIO);
234cb34ed44SMarcel Moolenaar 	err = fdt_get_range(OF_parent(node), 0, &pbase, &psize);
235cb34ed44SMarcel Moolenaar 	if (err)
236cb34ed44SMarcel Moolenaar 		pbase = 0;
237cb34ed44SMarcel Moolenaar 
238cb34ed44SMarcel Moolenaar 	start += pbase;
239cb34ed44SMarcel Moolenaar 
240cb34ed44SMarcel Moolenaar 	return (bus_space_map(di->bas.bst, start, size, 0, &di->bas.bsh));
241cb34ed44SMarcel Moolenaar }
242