1 /*-
2  * Copyright (c) 2016 Michael Zhilin <[email protected]>
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include "opt_uart.h"
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/cons.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 
40 #include <machine/bus.h>
41 
42 #include <dev/bhnd/cores/chipc/chipcreg.h>
43 
44 #include <dev/uart/uart.h>
45 #include <dev/uart/uart_bus.h>
46 #include <dev/uart/uart_cpu.h>
47 
48 #include "bcm_socinfo.h"
49 
50 bus_space_tag_t uart_bus_space_io;
51 bus_space_tag_t uart_bus_space_mem;
52 
53 static struct uart_class *chipc_uart_class = &uart_ns8250_class;
54 
55 #define	CHIPC_UART_BAUDRATE	115200
56 
57 int
58 uart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
59 {
60 	return ((b1->bsh == b2->bsh && b1->bst == b2->bst) ? 1 : 0);
61 }
62 
63 static int
64 uart_cpu_init(struct uart_devinfo *di, int uart, int baudrate)
65 {
66 	struct bcm_socinfo	*socinfo;
67 
68 	if (uart >= CHIPC_UART_MAX)
69 		return (EINVAL);
70 
71 	socinfo = bcm_get_socinfo();
72 	di->ops = uart_getops(chipc_uart_class);
73 	di->bas.chan = 0;
74 	di->bas.bst = uart_bus_space_mem;
75 	di->bas.bsh = (bus_space_handle_t) BCM_SOCREG(CHIPC_UART(uart));
76 	di->bas.regshft = 0;
77 	di->bas.rclk = socinfo->uartrate;  /* in Hz */
78 	di->baudrate = baudrate;
79 	di->databits = 8;
80 	di->stopbits = 1;
81 	di->parity = UART_PARITY_NONE;
82 
83 	return (0);
84 }
85 
86 int
87 uart_cpu_getdev(int devtype, struct uart_devinfo *di)
88 {
89 	int			 ivar;
90 
91 	uart_bus_space_io = NULL;
92 	uart_bus_space_mem = mips_bus_space_generic;
93 
94 	/* Check the environment. */
95 	if (uart_getenv(devtype, di, chipc_uart_class) == 0)
96 		return (0);
97 
98 	/* Scan the device hints for the first matching device */
99 	for (int i = 0; i < CHIPC_UART_MAX; i++) {
100 		if (resource_int_value("uart", i, "flags", &ivar))
101 			continue;
102 
103 		/* Check usability */
104 		if (devtype == UART_DEV_CONSOLE && !UART_FLAGS_CONSOLE(ivar))
105 			continue;
106 
107 		if (devtype == UART_DEV_DBGPORT && !UART_FLAGS_DBGPORT(ivar))
108 			continue;
109 
110 		if (resource_int_value("uart", i, "disabled", &ivar) == 0 &&
111 		    ivar == 0)
112 			continue;
113 
114 		/* Found */
115 		if (resource_int_value("uart", i, "baud", &ivar) != 0)
116 			ivar = CHIPC_UART_BAUDRATE;
117 
118 		return (uart_cpu_init(di, i, ivar));
119 	}
120 
121 	/* Default to uart0/115200 */
122 	return (uart_cpu_init(di, 0, CHIPC_UART_BAUDRATE));
123 }
124