1/*	$NetBSD: zynq7000_uart.c,v 1.3 2022/10/25 22:49:39 jmcneill Exp $	*/
2
3/*-
4 * Copyright (c) 2015  Genetec Corporation.  All rights reserved.
5 * Written by Hashimoto Kenichi for Genetec Corporation.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: zynq7000_uart.c,v 1.3 2022/10/25 22:49:39 jmcneill Exp $");
31
32#include "opt_soc.h"
33#include "opt_console.h"
34
35#include <sys/param.h>
36#include <sys/bus.h>
37#include <sys/device.h>
38
39#include <arm/xilinx/zynq_uartreg.h>
40#include <arm/xilinx/zynq_uartvar.h>
41
42#include <dev/fdt/fdtvar.h>
43
44static const struct device_compatible_entry compat_data[] = {
45	{ .compat = "xlnx,xuartps" },
46	{ .compat = "cdns,uart-r1p8" },
47	DEVICE_COMPAT_EOL
48};
49
50int
51zynquart_match(device_t parent, struct cfdata *cf, void *aux)
52{
53	struct fdt_attach_args * const faa = aux;
54
55	return of_compatible_match(faa->faa_phandle, compat_data);
56}
57
58void
59zynquart_attach(device_t parent, device_t self, void *aux)
60{
61	struct fdt_attach_args * faa = aux;
62	const int phandle = faa->faa_phandle;
63	char intrstr[128];
64	bus_addr_t addr;
65	bus_size_t size;
66
67	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
68		aprint_error(": couldn't get registers\n");
69		return;
70	}
71
72	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
73		aprint_error(": failed to decode interrupt\n");
74		return;
75	}
76
77	if (fdtbus_intr_establish(phandle, 0, IPL_SERIAL, IST_LEVEL,
78				  zynquartintr, device_private(self)) == NULL) {
79		aprint_error(": failed to establish interrupt on %s\n", intrstr);
80		return;
81	}
82
83	zynquart_attach_common(parent, self, faa->faa_bst, addr, size, 0);
84	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
85}
86
87/*
88 * Console support
89 */
90
91static int
92zynq_uart_console_match(int phandle)
93{
94	return of_compatible_match(phandle, compat_data);
95}
96
97static void
98zynq_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
99{
100	const int phandle = faa->faa_phandle;
101	bus_space_tag_t bst = faa->faa_bst;
102	bus_addr_t addr;
103	tcflag_t flags;
104	int speed;
105
106	fdtbus_get_reg(phandle, 0, &addr, NULL);
107	speed = fdtbus_get_stdout_speed();
108	if (speed < 0)
109		speed = 115200;	/* default */
110	flags = fdtbus_get_stdout_flags();
111
112	if (zynquart_cons_attach(bst, addr, speed, flags))
113		panic("Cannot initialize zynq uart console");
114}
115
116static const struct fdt_console zynq_uart_console = {
117	.match = zynq_uart_console_match,
118	.consinit = zynq_uart_console_consinit,
119};
120
121FDT_CONSOLE(zynq_uart, &zynq_uart_console);
122