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