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