1299994Sadrian/*-
2299994Sadrian * Copyright (c) 2016 Michael Zhilin <mizhka@gmail.com>
3299994Sadrian *
4299994Sadrian * All rights reserved.
5299994Sadrian *
6299994Sadrian * Redistribution and use in source and binary forms, with or without
7299994Sadrian * modification, are permitted provided that the following conditions
8299994Sadrian * are met:
9299994Sadrian * 1. Redistributions of source code must retain the above copyright
10299994Sadrian *    notice, this list of conditions and the following disclaimer.
11299994Sadrian * 2. Redistributions in binary form must reproduce the above copyright
12299994Sadrian *    notice, this list of conditions and the following disclaimer in the
13299994Sadrian *    documentation and/or other materials provided with the distribution.
14299994Sadrian *
15299994Sadrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16299994Sadrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17299994Sadrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18299994Sadrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19299994Sadrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20299994Sadrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21299994Sadrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22299994Sadrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23299994Sadrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24299994Sadrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25299994Sadrian * SUCH DAMAGE.
26299994Sadrian */
27299994Sadrian
28299994Sadrian#include <sys/cdefs.h>
29299994Sadrian__FBSDID("$FreeBSD$");
30299994Sadrian
31299994Sadrian#include "opt_uart.h"
32299994Sadrian
33299994Sadrian#include <sys/param.h>
34299994Sadrian#include <sys/systm.h>
35299994Sadrian#include <sys/bus.h>
36299994Sadrian#include <sys/cons.h>
37299994Sadrian#include <sys/lock.h>
38299994Sadrian#include <sys/mutex.h>
39299994Sadrian
40299994Sadrian#include <machine/bus.h>
41299994Sadrian
42302190Slandonf#include <dev/bhnd/cores/chipc/chipcreg.h>
43302190Slandonf
44299994Sadrian#include <dev/uart/uart.h>
45299994Sadrian#include <dev/uart/uart_bus.h>
46299994Sadrian#include <dev/uart/uart_cpu.h>
47299994Sadrian
48299994Sadrian#include "bcm_socinfo.h"
49299994Sadrian
50299994Sadrianbus_space_tag_t uart_bus_space_io;
51299994Sadrianbus_space_tag_t uart_bus_space_mem;
52299994Sadrian
53302190Slandonfstatic struct uart_class *chipc_uart_class = &uart_ns8250_class;
54302190Slandonf
55302190Slandonf#define	CHIPC_UART_BAUDRATE	115200
56302190Slandonf
57299994Sadrianint
58299994Sadrianuart_cpu_eqres(struct uart_bas *b1, struct uart_bas *b2)
59299994Sadrian{
60299994Sadrian	return ((b1->bsh == b2->bsh && b1->bst == b2->bst) ? 1 : 0);
61299994Sadrian}
62299994Sadrian
63302190Slandonfstatic int
64302190Slandonfuart_cpu_init(struct uart_devinfo *di, int uart, int baudrate)
65299994Sadrian{
66299994Sadrian	struct bcm_socinfo	*socinfo;
67299994Sadrian
68302190Slandonf	if (uart >= CHIPC_UART_MAX)
69302190Slandonf		return (EINVAL);
70302190Slandonf
71299994Sadrian	socinfo = bcm_get_socinfo();
72302190Slandonf	di->ops = uart_getops(chipc_uart_class);
73299994Sadrian	di->bas.chan = 0;
74302190Slandonf	di->bas.bst = uart_bus_space_mem;
75302190Slandonf	di->bas.bsh = (bus_space_handle_t) BCM_SOCREG(CHIPC_UART(uart));
76299994Sadrian	di->bas.regshft = 0;
77299994Sadrian	di->bas.rclk = socinfo->uartrate;  /* in Hz */
78302190Slandonf	di->baudrate = baudrate;
79299994Sadrian	di->databits = 8;
80299994Sadrian	di->stopbits = 1;
81299994Sadrian	di->parity = UART_PARITY_NONE;
82302190Slandonf
83302190Slandonf	return (0);
84302190Slandonf}
85302190Slandonf
86302190Slandonfint
87302190Slandonfuart_cpu_getdev(int devtype, struct uart_devinfo *di)
88302190Slandonf{
89302190Slandonf	int			 ivar;
90302190Slandonf
91299994Sadrian	uart_bus_space_io = NULL;
92299994Sadrian	uart_bus_space_mem = mips_bus_space_generic;
93302190Slandonf
94302190Slandonf	/* Check the environment. */
95302190Slandonf	if (uart_getenv(devtype, di, chipc_uart_class) == 0)
96302190Slandonf		return (0);
97302190Slandonf
98302190Slandonf	/* Scan the device hints for the first matching device */
99302190Slandonf	for (int i = 0; i < CHIPC_UART_MAX; i++) {
100302190Slandonf		if (resource_int_value("uart", i, "flags", &ivar))
101302190Slandonf			continue;
102302190Slandonf
103302190Slandonf		/* Check usability */
104302190Slandonf		if (devtype == UART_DEV_CONSOLE && !UART_FLAGS_CONSOLE(ivar))
105302190Slandonf			continue;
106302190Slandonf
107302190Slandonf		if (devtype == UART_DEV_DBGPORT && !UART_FLAGS_DBGPORT(ivar))
108302190Slandonf			continue;
109302190Slandonf
110302190Slandonf		if (resource_int_value("uart", i, "disabled", &ivar) == 0 &&
111302190Slandonf		    ivar == 0)
112302190Slandonf			continue;
113302190Slandonf
114302190Slandonf		/* Found */
115302190Slandonf		if (resource_int_value("uart", i, "baud", &ivar) != 0)
116302190Slandonf			ivar = CHIPC_UART_BAUDRATE;
117302190Slandonf
118302190Slandonf		return (uart_cpu_init(di, i, ivar));
119302190Slandonf	}
120302190Slandonf
121302190Slandonf	/* Default to uart0/115200 */
122302190Slandonf	return (uart_cpu_init(di, 0, CHIPC_UART_BAUDRATE));
123299994Sadrian}
124