Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_bsc_fdt.c revision 1.1.2.2
      1 /*	$NetBSD: bcm2835_bsc_fdt.c,v 1.1.2.2 2020/04/08 14:07:28 martin 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.1.2.2 2020/04/08 14:07:28 martin 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 	prop_dictionary_t prop = device_properties(self);
     71 	bool disable = false;
     72 
     73 	bus_addr_t addr;
     74 	bus_size_t size;
     75 
     76 	sc->sc_dev = self;
     77 	sc->sc_iot = faa->faa_bst;
     78 
     79 	int error = fdtbus_get_reg(phandle, 0, &addr, &size);
     80 	if (error) {
     81 		aprint_error(": unable to get device registers\n");
     82 		return;
     83 	}
     84 
     85 	prop_dictionary_get_bool(prop, "disable", &disable);
     86 	if (disable) {
     87 		aprint_naive(": disabled\n");
     88 		aprint_normal(": disabled\n");
     89 		return;
     90 	}
     91 
     92 	/* Enable clock */
     93 	sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
     94 	if (sc->sc_clk == NULL) {
     95 		aprint_error(": couldn't acquire clock\n");
     96 		return;
     97 	}
     98 
     99 	if (clk_enable(sc->sc_clk) != 0) {
    100 		aprint_error(": failed to enable clock\n");
    101 		return;
    102 	}
    103 
    104 	sc->sc_frequency = clk_get_rate(sc->sc_clk);
    105 
    106 	if (of_getprop_uint32(phandle, "clock-frequency",
    107 	    &sc->sc_clkrate) != 0) {
    108 		sc->sc_clkrate = 100000;
    109 	}
    110 
    111 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh)) {
    112 		aprint_error(": unable to map device\n");
    113 		return;
    114 	}
    115 
    116 	aprint_naive("\n");
    117 	aprint_normal(": Broadcom Serial Controller\n");
    118 
    119 	bsciic_attach(sc);
    120 
    121 	char intrstr[128];
    122 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    123 		aprint_error_dev(sc->sc_dev, "failed to decode interrupt\n");
    124 		return;
    125 	}
    126 	sc->sc_inth = fdtbus_intr_establish(phandle, 0, IPL_VM,
    127 	    FDT_INTR_MPSAFE, bsciic_intr, sc);
    128 	if (sc->sc_inth == NULL) {
    129 		aprint_error_dev(sc->sc_dev,
    130 		    "failed to establish interrupt %s\n", intrstr);
    131 		return;
    132 	}
    133 	aprint_normal_dev(sc->sc_dev, "interrupting on %s\n", intrstr);
    134 
    135 	iic_tag_init(&sc->sc_i2c);
    136 	sc->sc_i2c.ic_cookie = sc;
    137 	sc->sc_i2c.ic_acquire_bus = bsciic_acquire_bus;
    138 	sc->sc_i2c.ic_release_bus = bsciic_release_bus;
    139 	sc->sc_i2c.ic_exec = bsciic_exec;
    140 
    141 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_i2c, iicbus_print);
    142 }
    143