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