1 1.10 thorpej /* $NetBSD: bcm2835_com.c,v 1.10 2025/09/06 22:53:47 thorpej Exp $ */ 2 1.1 jmcneill 3 1.1 jmcneill /*- 4 1.1 jmcneill * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca> 5 1.1 jmcneill * All rights reserved. 6 1.1 jmcneill * 7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 8 1.1 jmcneill * modification, are permitted provided that the following conditions 9 1.1 jmcneill * are met: 10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer. 12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 14 1.1 jmcneill * documentation and/or other materials provided with the distribution. 15 1.1 jmcneill * 16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 1.1 jmcneill * SUCH DAMAGE. 27 1.1 jmcneill */ 28 1.1 jmcneill 29 1.1 jmcneill #include <sys/cdefs.h> 30 1.10 thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_com.c,v 1.10 2025/09/06 22:53:47 thorpej Exp $"); 31 1.1 jmcneill 32 1.1 jmcneill #include <sys/param.h> 33 1.1 jmcneill #include <sys/bus.h> 34 1.1 jmcneill #include <sys/device.h> 35 1.1 jmcneill #include <sys/intr.h> 36 1.1 jmcneill #include <sys/systm.h> 37 1.1 jmcneill #include <sys/termios.h> 38 1.1 jmcneill 39 1.1 jmcneill #include <arm/broadcom/bcm2835reg.h> 40 1.1 jmcneill #include <arm/broadcom/bcm2835var.h> 41 1.1 jmcneill #include <arm/broadcom/bcm2835_intr.h> 42 1.1 jmcneill 43 1.4 skrll #include <dev/fdt/fdtvar.h> 44 1.10 thorpej #include <dev/fdt/fdt_console.h> 45 1.4 skrll 46 1.1 jmcneill #include <dev/ic/comvar.h> 47 1.1 jmcneill 48 1.1 jmcneill static int bcm_com_match(device_t, cfdata_t, void *); 49 1.1 jmcneill static void bcm_com_attach(device_t, device_t, void *); 50 1.1 jmcneill 51 1.1 jmcneill CFATTACH_DECL_NEW(bcmcom, sizeof(struct com_softc), 52 1.1 jmcneill bcm_com_match, bcm_com_attach, NULL, NULL); 53 1.1 jmcneill 54 1.7 thorpej static const struct device_compatible_entry compat_data[] = { 55 1.7 thorpej { .compat = "brcm,bcm2835-aux-uart" }, 56 1.7 thorpej DEVICE_COMPAT_EOL 57 1.4 skrll }; 58 1.4 skrll 59 1.1 jmcneill static int 60 1.1 jmcneill bcm_com_match(device_t parent, cfdata_t cf, void *aux) 61 1.1 jmcneill { 62 1.4 skrll struct fdt_attach_args * const faa = aux; 63 1.1 jmcneill 64 1.7 thorpej return of_compatible_match(faa->faa_phandle, compat_data); 65 1.1 jmcneill } 66 1.1 jmcneill 67 1.1 jmcneill static void 68 1.1 jmcneill bcm_com_attach(device_t parent, device_t self, void *aux) 69 1.1 jmcneill { 70 1.1 jmcneill struct com_softc * const sc = device_private(self); 71 1.4 skrll struct fdt_attach_args * const faa = aux; 72 1.4 skrll const int phandle = faa->faa_phandle; 73 1.4 skrll 74 1.6 jmcneill bus_space_tag_t bst = faa->faa_bst; 75 1.1 jmcneill bus_space_handle_t bsh; 76 1.4 skrll bus_addr_t addr; 77 1.4 skrll bus_size_t size; 78 1.1 jmcneill void *ih; 79 1.1 jmcneill 80 1.1 jmcneill sc->sc_dev = self; 81 1.2 jmcneill sc->sc_type = COM_TYPE_BCMAUXUART; 82 1.1 jmcneill 83 1.4 skrll int error = fdtbus_get_reg(phandle, 0, &addr, &size); 84 1.4 skrll if (error) { 85 1.4 skrll aprint_error_dev(sc->sc_dev, "unable to map device\n"); 86 1.1 jmcneill return; 87 1.1 jmcneill } 88 1.1 jmcneill 89 1.4 skrll if (com_is_console(bst, addr, &bsh) == 0 && 90 1.4 skrll bus_space_map(bst, addr, size, 0, &bsh) != 0) { 91 1.1 jmcneill aprint_error(": can't map device\n"); 92 1.1 jmcneill return; 93 1.1 jmcneill } 94 1.1 jmcneill 95 1.4 skrll /* Enable clocks */ 96 1.4 skrll struct clk *clk; 97 1.4 skrll for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) { 98 1.4 skrll if (clk_enable(clk) != 0) { 99 1.4 skrll aprint_error(": failed to enable clock #%d\n", i); 100 1.4 skrll return; 101 1.4 skrll } 102 1.4 skrll /* First clock is UARTCLK */ 103 1.4 skrll if (i == 0) 104 1.4 skrll sc->sc_frequency = clk_get_rate(clk); 105 1.4 skrll } 106 1.4 skrll 107 1.4 skrll sc->sc_frequency *= 2; 108 1.4 skrll 109 1.6 jmcneill com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, 2); 110 1.1 jmcneill 111 1.1 jmcneill com_attach_subr(sc); 112 1.1 jmcneill aprint_naive("\n"); 113 1.1 jmcneill 114 1.4 skrll char intrstr[128]; 115 1.4 skrll if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) { 116 1.4 skrll aprint_error(": failed to decode interrupt\n"); 117 1.4 skrll return; 118 1.4 skrll } 119 1.4 skrll 120 1.8 skrll ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, FDT_INTR_MPSAFE, 121 1.8 skrll comintr, sc, device_xname(sc->sc_dev)); 122 1.1 jmcneill if (ih == NULL) { 123 1.4 skrll aprint_error_dev(self, "failed to establish interrupt %s\n", 124 1.4 skrll intrstr); 125 1.1 jmcneill return; 126 1.1 jmcneill } 127 1.9 mlelstv aprint_normal_dev(self, "interrupting on %s, clock %u Hz\n", 128 1.9 mlelstv intrstr, sc->sc_frequency); 129 1.1 jmcneill } 130 1.4 skrll 131 1.4 skrll static int 132 1.4 skrll bcmaux_com_console_match(int phandle) 133 1.4 skrll { 134 1.4 skrll 135 1.7 thorpej return of_compatible_match(phandle, compat_data); 136 1.4 skrll } 137 1.4 skrll 138 1.4 skrll static void 139 1.4 skrll bcmaux_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq) 140 1.4 skrll { 141 1.4 skrll const int phandle = faa->faa_phandle; 142 1.6 jmcneill bus_space_tag_t bst = faa->faa_bst; 143 1.6 jmcneill bus_space_handle_t dummy_bsh; 144 1.6 jmcneill struct com_regs regs; 145 1.4 skrll bus_addr_t addr; 146 1.4 skrll tcflag_t flags; 147 1.4 skrll int speed; 148 1.4 skrll 149 1.4 skrll fdtbus_get_reg(phandle, 0, &addr, NULL); 150 1.4 skrll speed = fdtbus_get_stdout_speed(); 151 1.4 skrll if (speed < 0) 152 1.4 skrll speed = 115200; /* default */ 153 1.4 skrll flags = fdtbus_get_stdout_flags(); 154 1.4 skrll 155 1.6 jmcneill memset(&dummy_bsh, 0, sizeof(dummy_bsh)); 156 1.6 jmcneill com_init_regs_stride(®s, bst, dummy_bsh, addr, 2); 157 1.6 jmcneill 158 1.6 jmcneill if (comcnattach1(®s, speed, uart_freq, COM_TYPE_BCMAUXUART, 159 1.4 skrll flags)) 160 1.4 skrll panic("Cannot initialize bcm com console"); 161 1.4 skrll 162 1.4 skrll cn_set_magic("+++++"); 163 1.4 skrll } 164 1.4 skrll 165 1.4 skrll static const struct fdt_console bcmaux_com_console = { 166 1.4 skrll .match = bcmaux_com_console_match, 167 1.4 skrll .consinit = bcmaux_com_console_consinit, 168 1.4 skrll }; 169 1.4 skrll 170 1.4 skrll FDT_CONSOLE(bcmcom, &bcmaux_com_console); 171