Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_bsc_fdt.c revision 1.2.2.2
      1  1.2.2.2   thorpej /*	$NetBSD: bcm2835_bsc_fdt.c,v 1.2.2.2 2021/04/03 22:28:16 thorpej Exp $	*/
      2      1.1  jmcneill 
      3      1.1  jmcneill /*
      4      1.1  jmcneill  * Copyright (c) 2019 Jason R. Thorpe
      5      1.1  jmcneill  * Copyright (c) 2012 Jonathan A. Kollasch
      6      1.1  jmcneill  * All rights reserved.
      7      1.1  jmcneill  *
      8      1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      9      1.1  jmcneill  * modification, are permitted provided that the following conditions
     10      1.1  jmcneill  * are met:
     11      1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     12      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     13      1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     14      1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     15      1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     16      1.1  jmcneill  *
     17      1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18      1.1  jmcneill  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19      1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20      1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     21      1.1  jmcneill  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22      1.1  jmcneill  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23      1.1  jmcneill  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24      1.1  jmcneill  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25      1.1  jmcneill  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26      1.1  jmcneill  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27      1.1  jmcneill  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28      1.1  jmcneill  */
     29      1.1  jmcneill 
     30      1.1  jmcneill #include <sys/cdefs.h>
     31  1.2.2.2   thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_bsc_fdt.c,v 1.2.2.2 2021/04/03 22:28:16 thorpej Exp $");
     32      1.1  jmcneill 
     33      1.1  jmcneill #include <sys/param.h>
     34      1.1  jmcneill #include <sys/bus.h>
     35      1.1  jmcneill #include <sys/device.h>
     36      1.1  jmcneill #include <sys/kernhist.h>
     37      1.1  jmcneill #include <sys/intr.h>
     38      1.1  jmcneill #include <sys/mutex.h>
     39      1.1  jmcneill #include <sys/systm.h>
     40      1.1  jmcneill 
     41      1.1  jmcneill #include <dev/i2c/i2cvar.h>
     42      1.1  jmcneill 
     43      1.1  jmcneill #include <arm/broadcom/bcm2835reg.h>
     44      1.1  jmcneill #include <arm/broadcom/bcm2835_bscreg.h>
     45      1.1  jmcneill #include <arm/broadcom/bcm2835_bscvar.h>
     46      1.1  jmcneill 
     47      1.1  jmcneill #include <dev/fdt/fdtvar.h>
     48      1.1  jmcneill 
     49      1.1  jmcneill static int bsciic_fdt_match(device_t, cfdata_t, void *);
     50      1.1  jmcneill static void bsciic_fdt_attach(device_t, device_t, void *);
     51      1.1  jmcneill 
     52      1.1  jmcneill CFATTACH_DECL_NEW(bsciic_fdt, sizeof(struct bsciic_softc),
     53      1.1  jmcneill     bsciic_fdt_match, bsciic_fdt_attach, NULL, NULL);
     54      1.1  jmcneill 
     55  1.2.2.2   thorpej static const struct device_compatible_entry compat_data[] = {
     56  1.2.2.2   thorpej 	{ .compat = "brcm,bcm2835-i2c" },
     57  1.2.2.2   thorpej 	DEVICE_COMPAT_EOL
     58  1.2.2.2   thorpej };
     59  1.2.2.2   thorpej 
     60      1.1  jmcneill static int
     61      1.1  jmcneill bsciic_fdt_match(device_t parent, cfdata_t match, void *aux)
     62      1.1  jmcneill {
     63      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     64      1.1  jmcneill 
     65  1.2.2.2   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     66      1.1  jmcneill }
     67      1.1  jmcneill 
     68      1.1  jmcneill static void
     69      1.1  jmcneill bsciic_fdt_attach(device_t parent, device_t self, void *aux)
     70      1.1  jmcneill {
     71      1.1  jmcneill 	struct bsciic_softc * const sc = device_private(self);
     72      1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
     73      1.1  jmcneill 	const int phandle = faa->faa_phandle;
     74      1.1  jmcneill 
     75      1.1  jmcneill 	bus_addr_t addr;
     76      1.1  jmcneill 	bus_size_t size;
     77      1.1  jmcneill 
     78      1.1  jmcneill 	sc->sc_dev = self;
     79      1.1  jmcneill 	sc->sc_iot = faa->faa_bst;
     80      1.1  jmcneill 
     81      1.1  jmcneill 	int error = fdtbus_get_reg(phandle, 0, &addr, &size);
     82      1.1  jmcneill 	if (error) {
     83      1.1  jmcneill 		aprint_error(": unable to get device registers\n");
     84      1.1  jmcneill 		return;
     85      1.1  jmcneill 	}
     86      1.1  jmcneill 
     87      1.1  jmcneill 	/* Enable clock */
     88      1.1  jmcneill 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
     89      1.1  jmcneill 	if (sc->sc_clk == NULL) {
     90      1.1  jmcneill 		aprint_error(": couldn't acquire clock\n");
     91      1.1  jmcneill 		return;
     92      1.1  jmcneill 	}
     93      1.1  jmcneill 
     94      1.1  jmcneill 	if (clk_enable(sc->sc_clk) != 0) {
     95      1.1  jmcneill 		aprint_error(": failed to enable clock\n");
     96      1.1  jmcneill 		return;
     97      1.1  jmcneill 	}
     98      1.1  jmcneill 
     99      1.1  jmcneill 	sc->sc_frequency = clk_get_rate(sc->sc_clk);
    100      1.1  jmcneill 
    101      1.1  jmcneill 	if (of_getprop_uint32(phandle, "clock-frequency",
    102      1.1  jmcneill 	    &sc->sc_clkrate) != 0) {
    103      1.1  jmcneill 		sc->sc_clkrate = 100000;
    104      1.1  jmcneill 	}
    105      1.1  jmcneill 
    106      1.1  jmcneill 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
    107      1.1  jmcneill 		aprint_error(": unable to map device\n");
    108      1.1  jmcneill 		return;
    109      1.1  jmcneill 	}
    110      1.1  jmcneill 
    111      1.1  jmcneill 	aprint_naive("\n");
    112      1.1  jmcneill 	aprint_normal(": Broadcom Serial Controller\n");
    113      1.1  jmcneill 
    114      1.1  jmcneill 	bsciic_attach(sc);
    115      1.1  jmcneill 
    116      1.1  jmcneill 	char intrstr[128];
    117      1.1  jmcneill 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    118      1.1  jmcneill 		aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
    119      1.1  jmcneill 		return;
    120      1.1  jmcneill 	}
    121  1.2.2.2   thorpej 	sc->sc_inth = fdtbus_intr_establish_xname(phandle, 0, IPL_VM,
    122  1.2.2.2   thorpej 	    FDT_INTR_MPSAFE, bsciic_intr, sc, device_xname(sc->sc_dev));
    123      1.1  jmcneill 	if (sc->sc_inth == NULL) {
    124      1.1  jmcneill 		aprint_error_dev(sc->sc_dev,
    125      1.1  jmcneill 		    "failed to establish interrupt %s\n", intrstr);
    126      1.1  jmcneill 		return;
    127      1.1  jmcneill 	}
    128      1.1  jmcneill 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
    129      1.1  jmcneill 
    130      1.1  jmcneill 	iic_tag_init(&sc->sc_i2c);
    131      1.1  jmcneill 	sc->sc_i2c.ic_cookie = sc;
    132      1.1  jmcneill 	sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
    133      1.1  jmcneill 	sc->sc_i2c.ic_release_bus = bsciic_release_bus;
    134      1.1  jmcneill 	sc->sc_i2c.ic_exec = bsciic_exec;
    135      1.1  jmcneill 
    136  1.2.2.1   thorpej 	fdtbus_register_i2c_controller(&sc->sc_i2c, phandle);
    137  1.2.2.1   thorpej 
    138      1.1  jmcneill 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print);
    139      1.1  jmcneill }
    140