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