Home | History | Annotate | Line # | Download | only in broadcom
      1 /* $NetBSD: bcm2835_com.c,v 1.10 2025/09/06 22:53:47 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.10 2025/09/06 22:53:47 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/fdt/fdtvar.h>
     44 #include <dev/fdt/fdt_console.h>
     45 
     46 #include <dev/ic/comvar.h>
     47 
     48 static int	bcm_com_match(device_t, cfdata_t, void *);
     49 static void	bcm_com_attach(device_t, device_t, void *);
     50 
     51 CFATTACH_DECL_NEW(bcmcom, sizeof(struct com_softc),
     52 	bcm_com_match, bcm_com_attach, NULL, NULL);
     53 
     54 static const struct device_compatible_entry compat_data[] = {
     55 	{ .compat = "brcm,bcm2835-aux-uart" },
     56 	DEVICE_COMPAT_EOL
     57 };
     58 
     59 static int
     60 bcm_com_match(device_t parent, cfdata_t cf, void *aux)
     61 {
     62 	struct fdt_attach_args * const faa = aux;
     63 
     64 	return of_compatible_match(faa->faa_phandle, compat_data);
     65 }
     66 
     67 static void
     68 bcm_com_attach(device_t parent, device_t self, void *aux)
     69 {
     70 	struct com_softc * const sc = device_private(self);
     71 	struct fdt_attach_args * const faa = aux;
     72 	const int phandle = faa->faa_phandle;
     73 
     74 	bus_space_tag_t bst = faa->faa_bst;
     75 	bus_space_handle_t bsh;
     76 	bus_addr_t addr;
     77 	bus_size_t size;
     78 	void *ih;
     79 
     80 	sc->sc_dev = self;
     81 	sc->sc_type = COM_TYPE_BCMAUXUART;
     82 
     83 	int error = fdtbus_get_reg(phandle, 0, &addr, &size);
     84 	if (error) {
     85 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
     86 		return;
     87 	}
     88 
     89 	if (com_is_console(bst, addr, &bsh) == 0 &&
     90 	    bus_space_map(bst, addr, size, 0, &bsh) != 0) {
     91 		aprint_error(": can't map device\n");
     92 		return;
     93 	}
     94 
     95 	/* Enable clocks */
     96 	struct clk *clk;
     97 	for (int i = 0; (clk = fdtbus_clock_get_index(phandle, i)); i++) {
     98 		if (clk_enable(clk) != 0) {
     99 			aprint_error(": failed to enable clock #%d\n", i);
    100 			return;
    101 		}
    102 		/* First clock is UARTCLK */
    103 		if (i == 0)
    104 			sc->sc_frequency = clk_get_rate(clk);
    105 	}
    106 
    107 	sc->sc_frequency *= 2;
    108 
    109 	com_init_regs_stride(&sc->sc_regs, bst, bsh, addr, 2);
    110 
    111 	com_attach_subr(sc);
    112 	aprint_naive("\n");
    113 
    114 	char intrstr[128];
    115 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    116 		aprint_error(": failed to decode interrupt\n");
    117 		return;
    118 	}
    119 
    120 	ih = fdtbus_intr_establish_xname(phandle, 0, IPL_SERIAL, FDT_INTR_MPSAFE,
    121 	    comintr, sc, device_xname(sc->sc_dev));
    122 	if (ih == NULL) {
    123 		aprint_error_dev(self, "failed to establish interrupt %s\n",
    124 		    intrstr);
    125 		return;
    126 	}
    127 	aprint_normal_dev(self, "interrupting on %s, clock %u Hz\n",
    128 	   intrstr, sc->sc_frequency);
    129 }
    130 
    131 static int
    132 bcmaux_com_console_match(int phandle)
    133 {
    134 
    135 	return of_compatible_match(phandle, compat_data);
    136 }
    137 
    138 static void
    139 bcmaux_com_console_consinit(struct fdt_attach_args *faa, u_int uart_freq)
    140 {
    141 	const int phandle = faa->faa_phandle;
    142 	bus_space_tag_t bst = faa->faa_bst;
    143 	bus_space_handle_t dummy_bsh;
    144 	struct com_regs regs;
    145 	bus_addr_t addr;
    146 	tcflag_t flags;
    147 	int speed;
    148 
    149 	fdtbus_get_reg(phandle, 0, &addr, NULL);
    150 	speed = fdtbus_get_stdout_speed();
    151 	if (speed < 0)
    152 		speed = 115200;	/* default */
    153 	flags = fdtbus_get_stdout_flags();
    154 
    155 	memset(&dummy_bsh, 0, sizeof(dummy_bsh));
    156 	com_init_regs_stride(&regs, bst, dummy_bsh, addr, 2);
    157 
    158 	if (comcnattach1(&regs, speed, uart_freq, COM_TYPE_BCMAUXUART,
    159 	    flags))
    160 		panic("Cannot initialize bcm com console");
    161 
    162 	cn_set_magic("+++++");
    163 }
    164 
    165 static const struct fdt_console bcmaux_com_console = {
    166 	.match = bcmaux_com_console_match,
    167 	.consinit = bcmaux_com_console_consinit,
    168 };
    169 
    170 FDT_CONSOLE(bcmcom, &bcmaux_com_console);
    171