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