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