uart_cpu_chipc.c revision 302190
197403Sobrien/*-
297403Sobrien * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com>
397403Sobrien *
497403Sobrien * All rights reserved.
597403Sobrien *
697403Sobrien * Redistribution and use in source and binary forms, with or without
797403Sobrien * modification, are permitted provided that the following conditions
897403Sobrien * are met:
997403Sobrien * 1. Redistributions of source code must retain the above copyright
1097403Sobrien *    notice, this list of conditions and the following disclaimer.
1197403Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1297403Sobrien *    notice, this list of conditions and the following disclaimer in the
1397403Sobrien *    documentation and/or other materials provided with the distribution.
1497403Sobrien *
1597403Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1697403Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1797403Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1897403Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1997403Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2097403Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2197403Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2297403Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2397403Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2497403Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2597403Sobrien * SUCH DAMAGE.
2697403Sobrien */
2797403Sobrien
2897403Sobrien#include <sys/cdefs.h>
2997403Sobrien__FBSDID("$FreeBSD: head/sys/mips/broadcom/uart_cpu_chipc.c 302190 2016-06-25 04:34:54Z landonf $");
3097403Sobrien
3197403Sobrien#include "opt_uart.h"
3297403Sobrien
3397403Sobrien#include <sys/param.h>
3497403Sobrien#include <sys/systm.h>
3597403Sobrien#include <sys/bus.h>
3697403Sobrien#include <sys/cons.h>
3797403Sobrien#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
50bus_space_tag_t uart_bus_space_io;
51bus_space_tag_t uart_bus_space_mem;
52
53static struct uart_class *chipc_uart_class = &uart_ns8250_class;
54
55#define	CHIPC_UART_BAUDRATE	115200
56
57int
58uart_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
63static int
64uart_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
86int
87uart_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