1 1.4 thorpej /* $NetBSD: imx_com.c,v 1.4 2025/09/06 22:53:47 thorpej Exp $ */ 2 1.1 skrll /*- 3 1.1 skrll * Copyright (c) 2019 Genetec Corporation. All rights reserved. 4 1.1 skrll * Written by Hashimoto Kenichi for Genetec Corporation. 5 1.1 skrll * 6 1.1 skrll * Redistribution and use in source and binary forms, with or without 7 1.1 skrll * modification, are permitted provided that the following conditions 8 1.1 skrll * are met: 9 1.1 skrll * 1. Redistributions of source code must retain the above copyright 10 1.1 skrll * notice, this list of conditions and the following disclaimer. 11 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright 12 1.1 skrll * notice, this list of conditions and the following disclaimer in the 13 1.1 skrll * documentation and/or other materials provided with the distribution. 14 1.1 skrll * 15 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 1.1 skrll * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 1.1 skrll * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 1.1 skrll * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 1.1 skrll * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 20 1.1 skrll * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 1.1 skrll * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 22 1.1 skrll * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 1.1 skrll * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 1.1 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 1.1 skrll * SUCH DAMAGE. 26 1.1 skrll */ 27 1.1 skrll 28 1.1 skrll #include <sys/cdefs.h> 29 1.4 thorpej __KERNEL_RCSID(0, "$NetBSD: imx_com.c,v 1.4 2025/09/06 22:53:47 thorpej Exp $"); 30 1.1 skrll 31 1.1 skrll #include "opt_fdt.h" 32 1.1 skrll #include "opt_imxuart.h" 33 1.1 skrll 34 1.1 skrll #include <sys/param.h> 35 1.1 skrll #include <sys/bus.h> 36 1.1 skrll #include <sys/device.h> 37 1.1 skrll 38 1.1 skrll #include <dev/fdt/fdtvar.h> 39 1.4 thorpej #include <dev/fdt/fdt_console.h> 40 1.1 skrll 41 1.1 skrll #include <arm/imx/imxuartreg.h> 42 1.1 skrll #include <arm/imx/imxuartvar.h> 43 1.1 skrll 44 1.1 skrll static int imx_com_match(device_t, struct cfdata *, void *); 45 1.1 skrll static void imx_com_attach(device_t, device_t, void *); 46 1.1 skrll 47 1.1 skrll CFATTACH_DECL_NEW(imx_com, sizeof(struct imxuart_softc), 48 1.1 skrll imx_com_match, imx_com_attach, NULL, NULL); 49 1.1 skrll 50 1.3 thorpej static const struct device_compatible_entry compat_data[] = { 51 1.3 thorpej { .compat = "fsl,imx6q-uart" }, 52 1.3 thorpej DEVICE_COMPAT_EOL 53 1.1 skrll }; 54 1.1 skrll 55 1.1 skrll static int 56 1.1 skrll imx_com_match(device_t parent, struct cfdata *cf, void *aux) 57 1.1 skrll { 58 1.1 skrll struct fdt_attach_args * const faa = aux; 59 1.1 skrll 60 1.3 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 61 1.1 skrll } 62 1.1 skrll 63 1.1 skrll static void 64 1.1 skrll imx_com_attach(device_t parent, device_t self, void *aux) 65 1.1 skrll { 66 1.1 skrll struct imxuart_softc *sc = device_private(self); 67 1.1 skrll struct imxuart_regs *regsp = &sc->sc_regs; 68 1.1 skrll struct fdt_attach_args *faa = aux; 69 1.1 skrll const int phandle = faa->faa_phandle; 70 1.1 skrll bus_space_tag_t bst = faa->faa_bst; 71 1.1 skrll bus_space_handle_t bsh; 72 1.1 skrll char intrstr[128]; 73 1.1 skrll struct clk *per; 74 1.1 skrll bus_addr_t addr; 75 1.1 skrll bus_size_t size; 76 1.1 skrll 77 1.1 skrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 78 1.1 skrll aprint_error(": couldn't get registers\n"); 79 1.1 skrll return; 80 1.1 skrll } 81 1.1 skrll 82 1.1 skrll if (bus_space_map(bst, addr, size, 0, &bsh) != 0) { 83 1.1 skrll aprint_error(": couldn't map registers\n"); 84 1.1 skrll return; 85 1.1 skrll } 86 1.1 skrll 87 1.1 skrll if (fdtbus_clock_enable(phandle, "ipg", false) != 0) { 88 1.1 skrll aprint_error(": couldn't enable ipg clock\n"); 89 1.1 skrll return; 90 1.1 skrll } 91 1.1 skrll 92 1.1 skrll per = fdtbus_clock_get(phandle, "per"); 93 1.1 skrll if (per != NULL && clk_enable(per) != 0) { 94 1.1 skrll aprint_error(": couldn't enable per clock\n"); 95 1.1 skrll return; 96 1.1 skrll } 97 1.1 skrll 98 1.1 skrll sc->sc_dev = self; 99 1.1 skrll regsp->ur_iot = bst; 100 1.1 skrll regsp->ur_iobase = addr; 101 1.1 skrll regsp->ur_ioh = bsh; 102 1.1 skrll 103 1.1 skrll if (per != NULL) { 104 1.1 skrll aprint_normal(", %u Hz", clk_get_rate(per)); 105 1.1 skrll /* XXX */ 106 1.1 skrll imxuart_set_frequency(clk_get_rate(per), 2); 107 1.1 skrll } 108 1.1 skrll 109 1.1 skrll if (imxuart_is_console(regsp->ur_iot, regsp->ur_iobase, ®sp->ur_ioh)) 110 1.1 skrll aprint_normal(" (console)"); 111 1.1 skrll 112 1.1 skrll aprint_normal("\n"); 113 1.1 skrll 114 1.1 skrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 115 1.1 skrll aprint_error_dev(self, "failed to decode interrupt\n"); 116 1.1 skrll return; 117 1.1 skrll } 118 1.1 skrll 119 1.2 jmcneill sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, 120 1.2 jmcneill 0, imxuintr, sc, device_xname(self)); 121 1.1 skrll if (sc->sc_ih == NULL) { 122 1.1 skrll aprint_error_dev(self, "failed to establish interrupt\n"); 123 1.1 skrll return; 124 1.1 skrll } 125 1.1 skrll 126 1.1 skrll aprint_normal_dev(self, "interrupting on %s\n", intrstr); 127 1.1 skrll 128 1.1 skrll imxuart_attach_subr(sc); 129 1.1 skrll } 130 1.1 skrll 131 1.1 skrll /* 132 1.1 skrll * Console support 133 1.1 skrll */ 134 1.1 skrll 135 1.1 skrll static int 136 1.1 skrll imx_com_console_match(int phandle) 137 1.1 skrll { 138 1.3 thorpej return of_compatible_match(phandle, compat_data); 139 1.1 skrll } 140 1.1 skrll 141 1.1 skrll static void 142 1.1 skrll imx_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 143 1.1 skrll { 144 1.1 skrll const int phandle = faa->faa_phandle; 145 1.1 skrll bus_space_tag_t bst = faa->faa_bst; 146 1.1 skrll bus_addr_t addr; 147 1.1 skrll bus_size_t size; 148 1.1 skrll tcflag_t flags; 149 1.1 skrll int speed; 150 1.1 skrll 151 1.1 skrll fdtbus_get_reg(phandle, 0, &addr, &size); 152 1.1 skrll speed = fdtbus_get_stdout_speed(); 153 1.1 skrll if (speed < 0) 154 1.1 skrll speed = 115200; /* default */ 155 1.1 skrll flags = fdtbus_get_stdout_flags(); 156 1.1 skrll 157 1.1 skrll imxuart_set_frequency(uart_freq, 2); 158 1.1 skrll if (imxuart_cnattach(bst, addr, speed, flags) != 0) 159 1.1 skrll panic("cannot attach console UART"); 160 1.1 skrll } 161 1.1 skrll 162 1.1 skrll static const struct fdt_console imx_com_console = { 163 1.1 skrll .match = imx_com_console_match, 164 1.1 skrll .consinit = imx_com_console_consinit, 165 1.1 skrll }; 166 1.1 skrll 167 1.1 skrll FDT_CONSOLE(imx_com, &imx_com_console); 168