Home | History | Annotate | Line # | Download | only in fdt
dwiic_fdt.c revision 1.5.8.1
      1  1.5.8.1  perseant /* $NetBSD: dwiic_fdt.c,v 1.5.8.1 2025/08/02 05:56:37 perseant 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.5.8.1  perseant __KERNEL_RCSID(0, "$NetBSD: dwiic_fdt.c,v 1.5.8.1 2025/08/02 05:56:37 perseant 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.4   thorpej static const struct device_compatible_entry compat_data[] = {
     45      1.4   thorpej 	{ .compat = "snps,designware-i2c" },
     46      1.4   thorpej 	DEVICE_COMPAT_EOL
     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.4   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     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.5.8.1  perseant 	/* enable clock(s) */
     93  1.5.8.1  perseant 	struct clk *clk;
     94  1.5.8.1  perseant 	u_int c;
     95  1.5.8.1  perseant 	for (c = 0; (clk = fdtbus_clock_get_index(phandle, c)) != NULL; c++) {
     96  1.5.8.1  perseant 		if (clk_enable(clk) != 0) {
     97  1.5.8.1  perseant 			aprint_error(": couldn't enable clock #%d\n", c);
     98  1.5.8.1  perseant 			goto failed_clock;
     99  1.5.8.1  perseant 		}
    100  1.5.8.1  perseant 	}
    101  1.5.8.1  perseant 	if (c == 0) {
    102  1.5.8.1  perseant 		aprint_error(": missing clock\n");
    103  1.5.8.1  perseant 		goto failed_clock;
    104  1.5.8.1  perseant 	}
    105  1.5.8.1  perseant 
    106  1.5.8.1  perseant 	/* de-assert optional reset. */
    107  1.5.8.1  perseant 	struct fdtbus_reset *rst = fdtbus_reset_get_index(phandle, 0);
    108  1.5.8.1  perseant 	if (rst != NULL) {
    109  1.5.8.1  perseant 		if (fdtbus_reset_deassert(rst) != 0) {
    110  1.5.8.1  perseant 			aprint_error(": couldn't de-assert the reset\n");
    111  1.5.8.1  perseant 			goto failed_reset;
    112  1.5.8.1  perseant 		}
    113  1.5.8.1  perseant 	}
    114  1.5.8.1  perseant 
    115      1.1  jakllsch 	aprint_naive(": I2C controller\n");
    116      1.1  jakllsch 	aprint_normal(": I2C controller\n");
    117      1.1  jakllsch 
    118      1.1  jakllsch 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    119      1.1  jakllsch 		aprint_error_dev(self, "failed to decode interrupt\n");
    120  1.5.8.1  perseant 		goto failed_intrstr;
    121      1.1  jakllsch 	}
    122      1.1  jakllsch 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    123      1.1  jakllsch 
    124      1.3  jmcneill 	sc->sc_dwiic.sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
    125      1.3  jmcneill 		dwiic_intr, &sc->sc_dwiic, device_xname(self));
    126      1.1  jakllsch 	if (sc->sc_dwiic.sc_ih == NULL) {
    127      1.1  jakllsch 		aprint_error_dev(self, "couldn't establish interrupt\n");
    128  1.5.8.1  perseant 		goto failed_intr;
    129      1.1  jakllsch 	}
    130      1.1  jakllsch 
    131      1.5  riastrad 	if (!dwiic_attach(&sc->sc_dwiic))
    132  1.5.8.1  perseant 		goto failed_attach;
    133      1.1  jakllsch 
    134      1.1  jakllsch 	pmf_device_register(self, dwiic_suspend, dwiic_resume);
    135      1.1  jakllsch 
    136      1.2   thorpej 	fdtbus_register_i2c_controller(&sc->sc_dwiic.sc_i2c_tag, phandle);
    137      1.1  jakllsch 
    138      1.1  jakllsch 	fdtbus_attach_i2cbus(self, phandle, &sc->sc_dwiic.sc_i2c_tag, iicbus_print);
    139      1.1  jakllsch 
    140      1.1  jakllsch 	return;
    141  1.5.8.1  perseant 
    142  1.5.8.1  perseant failed_attach:
    143  1.5.8.1  perseant 	fdtbus_intr_disestablish(phandle, sc->sc_dwiic.sc_ih);
    144  1.5.8.1  perseant 
    145  1.5.8.1  perseant failed_intrstr:
    146  1.5.8.1  perseant failed_intr:
    147  1.5.8.1  perseant failed_reset:
    148  1.5.8.1  perseant failed_clock:
    149  1.5.8.1  perseant 	bus_space_unmap(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh, size);
    150      1.1  jakllsch }
    151