1 /* $NetBSD: ti_com.c,v 1.13 2025/09/06 22:53:48 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: ti_com.c,v 1.13 2025/09/06 22:53:48 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 #include <arch/arm/ti/ti_prcm.h> 47 48 static int ti_com_match(device_t, cfdata_t, void *); 49 static void ti_com_attach(device_t, device_t, void *); 50 51 static const struct device_compatible_entry compat_data[] = { 52 { .compat = "ti,am3352-uart" }, 53 { .compat = "ti,omap3-uart" }, 54 DEVICE_COMPAT_EOL 55 }; 56 57 struct ti_com_softc { 58 struct com_softc ssc_sc; 59 void *ssc_ih; 60 }; 61 62 CFATTACH_DECL_NEW(ti_com, sizeof(struct ti_com_softc), 63 ti_com_match, ti_com_attach, NULL, NULL); 64 65 static int 66 ti_com_match(device_t parent, cfdata_t cf, void *aux) 67 { 68 struct fdt_attach_args * const faa = aux; 69 70 return of_compatible_match(faa->faa_phandle, compat_data); 71 } 72 73 static void 74 ti_com_attach(device_t parent, device_t self, void *aux) 75 { 76 struct ti_com_softc * const ssc = device_private(self); 77 struct com_softc * const sc = &ssc->ssc_sc; 78 struct fdt_attach_args * const faa = aux; 79 const int phandle = faa->faa_phandle; 80 bus_space_tag_t bst = faa->faa_bst; 81 bus_space_handle_t bsh; 82 char intrstr[128]; 83 bus_addr_t addr; 84 bus_size_t size; 85 int error; 86 87 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 88 aprint_error(": couldn't get registers\n"); 89 return; 90 } 91 92 sc->sc_dev = self; 93 94 if (of_getprop_uint32(phandle, "clock-frequency", &sc->sc_frequency) != 0) { 95 aprint_error(": missing 'clock-frequency' property\n"); 96 return; 97 } 98 99 sc->sc_type = COM_TYPE_OMAP; 100 101 error = bus_space_map(bst, addr, size, 0, &bsh); 102 if (error) { 103 aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error); 104 return; 105 } 106 107 if (ti_prcm_enable_hwmod(phandle, 0) != 0) { 108 aprint_error(": couldn't enable module\n"); 109 return; 110 } 111 112 com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, 2); 113 114 com_attach_subr(sc); 115 aprint_naive("\n"); 116 117 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 118 aprint_error_dev(self, "failed to decode interrupt\n"); 119 return; 120 } 121 122 ssc->ssc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, 123 FDT_INTR_MPSAFE, comintr, sc, device_xname(self)); 124 if (ssc->ssc_ih == NULL) { 125 aprint_error_dev(self, "failed to establish interrupt on %s\n", 126 intrstr); 127 } 128 aprint_normal_dev(self, "interrupting on %s\n", intrstr); 129 } 130 131 /* 132 * Console support 133 */ 134 135 static int 136 ti_com_console_match(int phandle) 137 { 138 return of_compatible_match(phandle, compat_data); 139 } 140 141 static void 142 ti_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 143 { 144 const int phandle = faa->faa_phandle; 145 bus_space_tag_t bst = faa->faa_bst; 146 bus_space_handle_t dummy_bsh; 147 struct com_regs regs; 148 bus_addr_t addr; 149 tcflag_t flags; 150 int speed; 151 152 fdtbus_get_reg(phandle, 0, &addr, NULL); 153 speed = fdtbus_get_stdout_speed(); 154 if (speed < 0) 155 speed = 115200; /* default */ 156 flags = fdtbus_get_stdout_flags(); 157 158 memset(&dummy_bsh, 0, sizeof(dummy_bsh)); 159 com_init_regs_stride(®s, bst, dummy_bsh, addr, 2); 160 161 if (comcnattach1(®s, speed, uart_freq, COM_TYPE_NORMAL, flags)) 162 panic("Cannot initialize ti com console"); 163 } 164 165 static const struct fdt_console ti_com_console = { 166 .match = ti_com_console_match, 167 .consinit = ti_com_console_consinit, 168 }; 169 170 FDT_CONSOLE(ti_com, &ti_com_console); 171