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