1 /* $NetBSD: dw_apb_uart.c,v 1.14 2026/08/07 14:31:36 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca> 5 * All rights reserved. 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 31 __KERNEL_RCSID(1, "$NetBSD: dw_apb_uart.c,v 1.14 2026/08/07 14:31:36 thorpej Exp $"); 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/device.h> 36 #include <sys/intr.h> 37 #include <sys/systm.h> 38 #include <sys/time.h> 39 #include <sys/termios.h> 40 41 #include <dev/ic/comvar.h> 42 43 #include <dev/fdt/fdtvar.h> 44 #include <dev/fdt/fdt_console.h> 45 46 static int dw_apb_uart_match(device_t, cfdata_t, void *); 47 static void dw_apb_uart_attach(device_t, device_t, void *); 48 49 static const struct device_compatible_entry compat_data[] = { 50 { .compat = "snps,dw-apb-uart" }, 51 DEVICE_COMPAT_EOL 52 }; 53 54 struct dw_apb_uart_softc { 55 struct com_softc ssc_sc; 56 void *ssc_ih; 57 58 struct clk *ssc_clk; 59 struct clk *ssc_pclk; 60 struct fdtbus_reset *ssc_rst; 61 }; 62 63 CFATTACH_DECL_NEW(dw_apb_uart, sizeof(struct dw_apb_uart_softc), 64 dw_apb_uart_match, dw_apb_uart_attach, NULL, NULL); 65 66 static int 67 dw_apb_uart_match(device_t parent, cfdata_t cf, void *aux) 68 { 69 struct fdt_attach_args * const faa = aux; 70 71 return of_compatible_match(faa->faa_phandle, compat_data); 72 } 73 74 static void 75 dw_apb_uart_attach(device_t parent, device_t self, void *aux) 76 { 77 struct dw_apb_uart_softc * const ssc = device_private(self); 78 struct com_softc * const sc = &ssc->ssc_sc; 79 struct fdt_attach_args * const faa = aux; 80 const int phandle = faa->faa_phandle; 81 bus_space_tag_t bst = faa->faa_bst; 82 bus_space_handle_t bsh; 83 char intrstr[128]; 84 bus_addr_t addr; 85 bus_size_t size; 86 u_int reg_shift, reg_iowidth; 87 int error; 88 89 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 90 aprint_error(": couldn't get registers\n"); 91 return; 92 } 93 94 reg_shift = fdtbus_get_reg_shift(phandle, 2); 95 96 if (of_getprop_uint32(phandle, "reg-io-width", ®_iowidth)) { 97 /* missing or bad reg-io-width property, assume 1 */ 98 reg_iowidth = 1; 99 } 100 101 sc->sc_dev = self; 102 103 ssc->ssc_clk = fdtbus_clock_get_index(phandle, 0); 104 if (ssc->ssc_clk == NULL) { 105 aprint_error(": couldn't get clock\n"); 106 return; 107 } 108 if (clk_enable(ssc->ssc_clk) != 0) { 109 aprint_error(": couldn't enable clock\n"); 110 return; 111 } 112 113 ssc->ssc_pclk = fdtbus_clock_get(phandle, "apb_pclk"); 114 if (ssc->ssc_pclk != NULL && clk_enable(ssc->ssc_pclk) != 0) { 115 aprint_error(": couldn't enable peripheral clock\n"); 116 return; 117 } 118 119 ssc->ssc_rst = fdtbus_reset_get_index(phandle, 0); 120 if (ssc->ssc_rst && fdtbus_reset_deassert(ssc->ssc_rst) != 0) { 121 aprint_error(": couldn't de-assert reset\n"); 122 return; 123 } 124 125 sc->sc_frequency = clk_get_rate(ssc->ssc_clk); 126 sc->sc_type = COM_TYPE_DW_APB; 127 128 error = bus_space_map(bst, addr, size, 0, &bsh); 129 if (error) { 130 aprint_error(": couldn't map %#" PRIx64 ": %d", (uint64_t)addr, error); 131 return; 132 } 133 134 com_init_regs_stride_width(&sc->sc_regs, bst, bsh, addr, reg_shift, reg_iowidth); 135 136 com_attach_subr(sc); 137 138 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 139 aprint_error_dev(self, "failed to decode interrupt\n"); 140 return; 141 } 142 143 ssc->ssc_ih = fdtbus_intr_establish_xname(phandle, 0, 144 IPL_SERIAL, FDT_INTR_MPSAFE, comintr, sc, device_xname(self)); 145 if (ssc->ssc_ih == NULL) { 146 aprint_error_dev(self, "failed to establish interrupt on %s\n", 147 intrstr); 148 } 149 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 150 } 151 152 /* 153 * Console support 154 */ 155 156 static int 157 dw_apb_uart_console_match(int phandle) 158 { 159 return of_compatible_match(phandle, compat_data); 160 } 161 162 static void 163 dw_apb_uart_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 164 { 165 const int phandle = faa->faa_phandle; 166 bus_space_tag_t bst = faa->faa_bst; 167 bus_space_handle_t dummy_bsh; 168 struct com_regs regs; 169 bus_addr_t addr; 170 tcflag_t flags; 171 u_int reg_shift, reg_iowidth; 172 int speed; 173 174 fdtbus_get_reg(phandle, 0, &addr, NULL); 175 speed = fdtbus_get_stdout_speed(); 176 if (speed < 0) 177 speed = 115200; /* default */ 178 flags = fdtbus_get_stdout_flags(); 179 180 reg_shift = fdtbus_get_reg_shift(phandle, 2); 181 182 if (of_getprop_uint32(phandle, "reg-io-width", ®_iowidth)) { 183 /* missing or bad reg-io-width property, assume 1 */ 184 reg_iowidth = 1; 185 } 186 187 memset(&dummy_bsh, 0, sizeof(dummy_bsh)); 188 com_init_regs_stride_width(®s, bst, dummy_bsh, addr, reg_shift, reg_iowidth); 189 190 if (comcnattach1(®s, speed, uart_freq, COM_TYPE_DW_APB, flags)) 191 panic("Cannot initialize dw-apb-uart console"); 192 } 193 194 static const struct fdt_console dw_apb_uart_console = { 195 .match = dw_apb_uart_console_match, 196 .consinit = dw_apb_uart_console_consinit, 197 }; 198 199 FDT_CONSOLE(dw_apb_uart, &dw_apb_uart_console); 200