1 /* $NetBSD: dwiic_acpi.c,v 1.8 2021/04/24 23:36:52 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jared McNeill <jmcneill@invisible.ca>. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: dwiic_acpi.c,v 1.8 2021/04/24 23:36:52 thorpej Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/bus.h> 37 #include <sys/cpu.h> 38 #include <sys/device.h> 39 40 #include <dev/acpi/acpireg.h> 41 #include <dev/acpi/acpivar.h> 42 #include <dev/acpi/acpi_intr.h> 43 #include <dev/acpi/acpi_i2c.h> 44 45 #include <dev/ic/dwiic_var.h> 46 47 struct dwiic_acpi_param { 48 uint16_t hcnt; 49 uint16_t lcnt; 50 uint32_t ht; 51 }; 52 53 static int dwiic_acpi_match(device_t, cfdata_t, void *); 54 static void dwiic_acpi_attach(device_t, device_t, void *); 55 56 static void dwiic_acpi_parse_param(struct dwiic_softc *, ACPI_HANDLE, 57 const char *, struct dwiic_acpi_param *); 58 static void dwiic_acpi_configure(struct dwiic_softc *, ACPI_HANDLE); 59 60 CFATTACH_DECL_NEW(dwiic_acpi, sizeof(struct dwiic_softc), dwiic_acpi_match, dwiic_acpi_attach, NULL, NULL); 61 62 static const struct device_compatible_entry compat_data[] = { 63 { .compat = "AMD0010" }, /* AMD FCH */ 64 { .compat = "AMDI0010" }, /* AMD FCH */ 65 { .compat = "AMDI0510" }, /* AMD Seattle */ 66 { .compat = "APMC0D0F" }, /* Ampere eMAG */ 67 DEVICE_COMPAT_EOL 68 }; 69 70 static int 71 dwiic_acpi_match(device_t parent, cfdata_t cf, void *aux) 72 { 73 struct acpi_attach_args *aa = aux; 74 75 return acpi_compatible_match(aa, compat_data); 76 } 77 78 static void 79 dwiic_acpi_attach(device_t parent, device_t self, void *aux) 80 { 81 struct dwiic_softc * const sc = device_private(self); 82 struct acpi_attach_args *aa = aux; 83 struct acpi_resources res; 84 struct acpi_mem *mem; 85 struct acpi_irq *irq; 86 ACPI_STATUS rv; 87 int error; 88 void *ih; 89 90 sc->sc_dev = self; 91 sc->sc_type = dwiic_type_generic; 92 sc->sc_iot = aa->aa_memt; 93 94 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS", 95 &res, &acpi_resource_parse_ops_default); 96 if (ACPI_FAILURE(rv)) 97 return; 98 99 mem = acpi_res_mem(&res, 0); 100 if (mem == NULL) { 101 aprint_error_dev(self, "couldn't find mem resource\n"); 102 goto done; 103 } 104 105 irq = acpi_res_irq(&res, 0); 106 if (irq == NULL) { 107 aprint_error_dev(self, "couldn't find irq resource\n"); 108 goto done; 109 } 110 111 error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh); 112 if (error) { 113 aprint_error_dev(self, "couldn't map registers\n"); 114 return; 115 } 116 117 ih = acpi_intr_establish(self, 118 (uint64_t)(uintptr_t)aa->aa_node->ad_handle, 119 IPL_VM, true, dwiic_intr, sc, device_xname(self)); 120 if (ih == NULL) { 121 aprint_error_dev(self, "couldn't install interrupt handler\n"); 122 bus_space_unmap(sc->sc_iot, sc->sc_ioh, mem->ar_length); 123 goto done; 124 } 125 126 dwiic_acpi_configure(sc, aa->aa_node->ad_handle); 127 128 sc->sc_iba.iba_child_devices = acpi_enter_i2c_devs(self, aa->aa_node); 129 130 dwiic_attach(sc); 131 132 config_found(self, &sc->sc_iba, iicbus_print, CFARG_EOL); 133 134 pmf_device_register(self, dwiic_suspend, dwiic_resume); 135 136 done: 137 acpi_resource_cleanup(&res); 138 } 139 140 static void 141 dwiic_acpi_parse_param(struct dwiic_softc *sc, ACPI_HANDLE handle, const char *path, 142 struct dwiic_acpi_param *param) 143 { 144 ACPI_BUFFER buf; 145 ACPI_OBJECT *obj; 146 147 memset(param, 0, sizeof(*param)); 148 149 if (ACPI_FAILURE(acpi_eval_struct(handle, path, &buf))) 150 return; 151 152 obj = buf.Pointer; 153 if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count != 3) 154 goto done; 155 156 param->hcnt = (uint16_t)obj->Package.Elements[0].Integer.Value; 157 param->lcnt = (uint16_t)obj->Package.Elements[1].Integer.Value; 158 param->ht = (uint32_t)obj->Package.Elements[2].Integer.Value; 159 160 done: 161 ACPI_FREE(buf.Pointer); 162 } 163 164 static void 165 dwiic_acpi_configure(struct dwiic_softc *sc, ACPI_HANDLE handle) 166 { 167 struct dwiic_acpi_param sscn, fmcn; 168 169 dwiic_acpi_parse_param(sc, handle, "SSCN", &sscn); 170 sc->ss_hcnt = sscn.hcnt; 171 sc->ss_lcnt = sscn.lcnt; 172 173 dwiic_acpi_parse_param(sc, handle, "FMCN", &fmcn); 174 sc->fs_hcnt = fmcn.hcnt; 175 sc->fs_lcnt = fmcn.lcnt; 176 177 /* XXX */ 178 sc->sda_hold_time = fmcn.ht; 179 } 180