1 1.9 thorpej /* $NetBSD: dwiic_fdt.c,v 1.9 2025/09/16 11:55:17 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.9 thorpej __KERNEL_RCSID(0, "$NetBSD: dwiic_fdt.c,v 1.9 2025/09/16 11:55:17 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.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.7 skrll /* enable clock(s) */ 93 1.7 skrll struct clk *clk; 94 1.7 skrll u_int c; 95 1.7 skrll for (c = 0; (clk = fdtbus_clock_get_index(phandle, c)) != NULL; c++) { 96 1.7 skrll if (clk_enable(clk) != 0) { 97 1.7 skrll aprint_error(": couldn't enable clock #%d\n", c); 98 1.7 skrll goto failed_clock; 99 1.7 skrll } 100 1.7 skrll } 101 1.7 skrll if (c == 0) { 102 1.7 skrll aprint_error(": missing clock\n"); 103 1.6 skrll goto failed_clock; 104 1.6 skrll } 105 1.6 skrll 106 1.7 skrll /* de-assert optional reset. */ 107 1.6 skrll struct fdtbus_reset *rst = fdtbus_reset_get_index(phandle, 0); 108 1.7 skrll if (rst != NULL) { 109 1.7 skrll if (fdtbus_reset_deassert(rst) != 0) { 110 1.7 skrll aprint_error(": couldn't de-assert the reset\n"); 111 1.7 skrll goto failed_reset; 112 1.7 skrll } 113 1.6 skrll } 114 1.6 skrll 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.6 skrll 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.6 skrll goto failed_intr; 129 1.1 jakllsch } 130 1.1 jakllsch 131 1.5 riastrad if (!dwiic_attach(&sc->sc_dwiic)) 132 1.6 skrll 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.8 thorpej iicbus_attach(self, &sc->sc_dwiic.sc_i2c_tag); 137 1.1 jakllsch 138 1.1 jakllsch return; 139 1.6 skrll 140 1.6 skrll failed_attach: 141 1.6 skrll fdtbus_intr_disestablish(phandle, sc->sc_dwiic.sc_ih); 142 1.6 skrll 143 1.6 skrll failed_intrstr: 144 1.6 skrll failed_intr: 145 1.6 skrll failed_reset: 146 1.6 skrll failed_clock: 147 1.6 skrll bus_space_unmap(sc->sc_dwiic.sc_iot, sc->sc_dwiic.sc_ioh, size); 148 1.1 jakllsch } 149