Home | History | Annotate | Line # | Download | only in fdt
dwiic_fdt.c revision 1.1.16.1
      1  1.1.16.1   thorpej /* $NetBSD: dwiic_fdt.c,v 1.1.16.1 2021/01/03 16:34:57 thorpej Exp $ */
      2       1.1  jakllsch 
      3       1.1  jakllsch /*-
      4       1.1  jakllsch  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      5       1.1  jakllsch  * All rights reserved.
      6       1.1  jakllsch  *
      7       1.1  jakllsch  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  jakllsch  * by Manuel Bouyer.
      9       1.1  jakllsch  *
     10       1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
     11       1.1  jakllsch  * modification, are permitted provided that the following conditions
     12       1.1  jakllsch  * are met:
     13       1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     14       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     15       1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     18       1.1  jakllsch  *
     19       1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  jakllsch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  jakllsch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  jakllsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  jakllsch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  jakllsch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  jakllsch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  jakllsch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  jakllsch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  jakllsch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  jakllsch  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  jakllsch  */
     31       1.1  jakllsch /*
     32       1.1  jakllsch  * Synopsys DesignWare I2C controller, FDT front-end
     33       1.1  jakllsch  */
     34       1.1  jakllsch 
     35       1.1  jakllsch #include <sys/cdefs.h>
     36  1.1.16.1   thorpej __KERNEL_RCSID(0, "$NetBSD: dwiic_fdt.c,v 1.1.16.1 2021/01/03 16:34:57 thorpej Exp $");
     37       1.1  jakllsch 
     38       1.1  jakllsch #include <sys/param.h>
     39       1.1  jakllsch #include <sys/systm.h>
     40       1.1  jakllsch 
     41       1.1  jakllsch #include <dev/fdt/fdtvar.h>
     42       1.1  jakllsch #include <dev/ic/dwiic_var.h>
     43       1.1  jakllsch 
     44       1.1  jakllsch static const char * const compatible[] = {
     45       1.1  jakllsch 	"snps,designware-i2c",
     46       1.1  jakllsch 	NULL
     47       1.1  jakllsch };
     48       1.1  jakllsch 
     49       1.1  jakllsch struct dwiic_fdt_softc {
     50       1.1  jakllsch 	struct dwiic_softc	sc_dwiic;
     51       1.1  jakllsch };
     52       1.1  jakllsch 
     53       1.1  jakllsch static int	dwiic_fdt_match(device_t, cfdata_t, void *);
     54       1.1  jakllsch static void	dwiic_fdt_attach(device_t, device_t, void *);
     55       1.1  jakllsch 
     56       1.1  jakllsch CFATTACH_DECL_NEW(dwiic_fdt, sizeof(struct dwiic_fdt_softc),
     57       1.1  jakllsch     dwiic_fdt_match, dwiic_fdt_attach, dwiic_detach, NULL);
     58       1.1  jakllsch 
     59       1.1  jakllsch int
     60       1.1  jakllsch dwiic_fdt_match(device_t parent, cfdata_t match, void *aux)
     61       1.1  jakllsch {
     62       1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
     63       1.1  jakllsch 
     64       1.1  jakllsch 	return of_match_compatible(faa->faa_phandle, compatible);
     65       1.1  jakllsch }
     66       1.1  jakllsch 
     67       1.1  jakllsch void
     68       1.1  jakllsch dwiic_fdt_attach(device_t parent, device_t self, void *aux)
     69       1.1  jakllsch {
     70       1.1  jakllsch 	struct dwiic_fdt_softc * const sc = device_private(self);
     71       1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
     72       1.1  jakllsch 	const int phandle = faa->faa_phandle;
     73       1.1  jakllsch 	bus_addr_t addr;
     74       1.1  jakllsch 	bus_size_t size;
     75       1.1  jakllsch 	char intrstr[128];
     76       1.1  jakllsch 
     77       1.1  jakllsch 	sc->sc_dwiic.sc_dev = self;
     78       1.1  jakllsch 	sc->sc_dwiic.sc_power = NULL;
     79       1.1  jakllsch 	sc->sc_dwiic.sc_type = dwiic_type_generic;
     80       1.1  jakllsch 
     81       1.1  jakllsch 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     82       1.1  jakllsch 		aprint_error(": couldn't get registers\n");
     83       1.1  jakllsch 		return;
     84       1.1  jakllsch 	}
     85       1.1  jakllsch 
     86       1.1  jakllsch 	sc->sc_dwiic.sc_iot = faa->faa_bst;
     87       1.1  jakllsch 	if (bus_space_map(sc->sc_dwiic.sc_iot, addr, size, 0, &sc->sc_dwiic.sc_ioh) != 0) {
     88       1.1  jakllsch 		aprint_error(": couldn't map registers\n");
     89       1.1  jakllsch 		return;
     90       1.1  jakllsch 	}
     91       1.1  jakllsch 
     92       1.1  jakllsch 	aprint_naive(": I2C controller\n");
     93       1.1  jakllsch 	aprint_normal(": I2C controller\n");
     94       1.1  jakllsch 
     95       1.1  jakllsch 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
     96       1.1  jakllsch 		aprint_error_dev(self, "failed to decode interrupt\n");
     97       1.1  jakllsch 		return;
     98       1.1  jakllsch 	}
     99       1.1  jakllsch 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    100       1.1  jakllsch 
    101       1.1  jakllsch 	sc->sc_dwiic.sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0,
    102       1.1  jakllsch 		dwiic_intr, &sc->sc_dwiic);
    103       1.1  jakllsch 	if (sc->sc_dwiic.sc_ih == NULL) {
    104       1.1  jakllsch 		aprint_error_dev(self, "couldn't establish interrupt\n");
    105       1.1  jakllsch 		goto out;
    106       1.1  jakllsch 	}
    107       1.1  jakllsch 
    108       1.1  jakllsch 	dwiic_attach(&sc->sc_dwiic);
    109       1.1  jakllsch 
    110       1.1  jakllsch 	pmf_device_register(self, dwiic_suspend, dwiic_resume);
    111       1.1  jakllsch 
    112  1.1.16.1   thorpej 	fdtbus_register_i2c_controller(&sc->sc_dwiic.sc_i2c_tag, phandle);
    113       1.1  jakllsch 
    114       1.1  jakllsch 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_dwiic.sc_i2c_tag, iicbus_print);
    115       1.1  jakllsch 
    116       1.1  jakllsch out:
    117       1.1  jakllsch 	return;
    118       1.1  jakllsch }
    119