xref: /freebsd-14.2/sys/dev/uart/uart_cpu_fdt.c (revision 718cf2cc)
1cb34ed44SMarcel Moolenaar /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*718cf2ccSPedro F. Giffuni  *
4cb34ed44SMarcel Moolenaar  * Copyright (c) 2009-2010 The FreeBSD Foundation
5cb34ed44SMarcel Moolenaar  * All rights reserved.
6cb34ed44SMarcel Moolenaar  *
7cb34ed44SMarcel Moolenaar  * This software was developed by Semihalf under sponsorship from
8cb34ed44SMarcel Moolenaar  * the FreeBSD Foundation.
9cb34ed44SMarcel Moolenaar  *
10cb34ed44SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
11cb34ed44SMarcel Moolenaar  * modification, are permitted provided that the following conditions
12cb34ed44SMarcel Moolenaar  * are met:
13cb34ed44SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
14cb34ed44SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
15cb34ed44SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
16cb34ed44SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
17cb34ed44SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
18cb34ed44SMarcel Moolenaar  *
19cb34ed44SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20cb34ed44SMarcel Moolenaar  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21cb34ed44SMarcel Moolenaar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22cb34ed44SMarcel Moolenaar  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23cb34ed44SMarcel Moolenaar  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24cb34ed44SMarcel Moolenaar  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25cb34ed44SMarcel Moolenaar  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26cb34ed44SMarcel Moolenaar  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27cb34ed44SMarcel Moolenaar  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28cb34ed44SMarcel Moolenaar  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29cb34ed44SMarcel Moolenaar  * SUCH DAMAGE.
30cb34ed44SMarcel Moolenaar  */
31cb34ed44SMarcel Moolenaar 
32cb34ed44SMarcel Moolenaar #include <sys/cdefs.h>
33cb34ed44SMarcel Moolenaar __FBSDID("$FreeBSD$");
34cb34ed44SMarcel Moolenaar 
3564958185SIan Lepore #include "opt_platform.h"
3664958185SIan Lepore 
37cb34ed44SMarcel Moolenaar #include <sys/param.h>
38cb34ed44SMarcel Moolenaar #include <sys/bus.h>
39cb34ed44SMarcel Moolenaar #include <sys/kernel.h>
40cb34ed44SMarcel Moolenaar #include <sys/module.h>
41dce533f3SNathan Whitehorn #include <sys/systm.h>
42cb34ed44SMarcel Moolenaar 
43c20809e1SNathan Whitehorn #include <vm/vm.h>
44c20809e1SNathan Whitehorn #include <vm/pmap.h>
45c20809e1SNathan Whitehorn 
46cb34ed44SMarcel Moolenaar #include <machine/bus.h>
47cb34ed44SMarcel Moolenaar 
48cb34ed44SMarcel Moolenaar #include <dev/fdt/fdt_common.h>
49cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus.h>
50cb34ed44SMarcel Moolenaar #include <dev/ofw/ofw_bus_subr.h>
51cb34ed44SMarcel Moolenaar #include <dev/uart/uart.h>
52cb34ed44SMarcel Moolenaar #include <dev/uart/uart_bus.h>
53cb34ed44SMarcel Moolenaar #include <dev/uart/uart_cpu.h>
54bd69e3adSIan Lepore #include <dev/uart/uart_cpu_fdt.h>
55cb34ed44SMarcel Moolenaar 
56cb34ed44SMarcel Moolenaar /*
57cb34ed44SMarcel Moolenaar  * UART console routines.
58cb34ed44SMarcel Moolenaar  */
59cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_io;
60cb34ed44SMarcel Moolenaar bus_space_tag_t uart_bus_space_mem;
61cb34ed44SMarcel Moolenaar 
6206d8b1edSAndrew Turner int
63cb34ed44SMarcel Moolenaar uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
64cb34ed44SMarcel Moolenaar {
65cb34ed44SMarcel Moolenaar 
664162dff9SAleksandr Rybalko 	if (b1->bst != b2->bst)
674162dff9SAleksandr Rybalko 		return (0);
684162dff9SAleksandr Rybalko 	if (pmap_kextract(b1->bsh) == 0)
694162dff9SAleksandr Rybalko 		return (0);
704162dff9SAleksandr Rybalko 	if (pmap_kextract(b2->bsh) == 0)
714162dff9SAleksandr Rybalko 		return (0);
724162dff9SAleksandr Rybalko 	return ((pmap_kextract(b1->bsh) == pmap_kextract(b2->bsh)) ? 1 : 0);
73cb34ed44SMarcel Moolenaar }
74cb34ed44SMarcel Moolenaar 
75cb34ed44SMarcel Moolenaar int
76cb34ed44SMarcel Moolenaar uart_cpu_getdev(int devtype, struct uart_devinfo *di)
77cb34ed44SMarcel Moolenaar {
78cb34ed44SMarcel Moolenaar 	struct uart_class *class;
79d4c89391SAndrew Turner 	bus_space_tag_t bst;
80d4c89391SAndrew Turner 	bus_space_handle_t bsh;
81c214a270SRuslan Bukin 	u_int shift, iowidth, rclk;
82d4c89391SAndrew Turner 	int br, err;
83cb34ed44SMarcel Moolenaar 
84dce533f3SNathan Whitehorn 	/* Allow overriding the FDT using the environment. */
85cb34ed44SMarcel Moolenaar 	class = &uart_ns8250_class;
86cb34ed44SMarcel Moolenaar 	err = uart_getenv(devtype, di, class);
87cb34ed44SMarcel Moolenaar 	if (!err)
88cb34ed44SMarcel Moolenaar 		return (0);
89cb34ed44SMarcel Moolenaar 
90cb34ed44SMarcel Moolenaar 	if (devtype != UART_DEV_CONSOLE)
91cb34ed44SMarcel Moolenaar 		return (ENXIO);
92cb34ed44SMarcel Moolenaar 
93c214a270SRuslan Bukin 	err = uart_cpu_fdt_probe(&class, &bst, &bsh, &br, &rclk, &shift, &iowidth);
94d4c89391SAndrew Turner 	if (err != 0)
95cb34ed44SMarcel Moolenaar 		return (err);
96405ada37SAndrew Turner 
97405ada37SAndrew Turner 	/*
98cb34ed44SMarcel Moolenaar 	 * Finalize configuration.
99cb34ed44SMarcel Moolenaar 	 */
100cb34ed44SMarcel Moolenaar 	di->bas.chan = 0;
101d4c89391SAndrew Turner 	di->bas.regshft = shift;
102c214a270SRuslan Bukin 	di->bas.regiowidth = iowidth;
103cb34ed44SMarcel Moolenaar 	di->baudrate = br;
104d4c89391SAndrew Turner 	di->bas.rclk = rclk;
105cb34ed44SMarcel Moolenaar 	di->ops = uart_getops(class);
106cb34ed44SMarcel Moolenaar 	di->databits = 8;
107cb34ed44SMarcel Moolenaar 	di->stopbits = 1;
108cb34ed44SMarcel Moolenaar 	di->parity = UART_PARITY_NONE;
109d4c89391SAndrew Turner 	di->bas.bst = bst;
110d4c89391SAndrew Turner 	di->bas.bsh = bsh;
111cb34ed44SMarcel Moolenaar 
11299c41022SIan Lepore 	uart_bus_space_mem = di->bas.bst;
11399c41022SIan Lepore 	uart_bus_space_io = NULL;
11499c41022SIan Lepore 
11599c41022SIan Lepore 	return (err);
116cb34ed44SMarcel Moolenaar }
117