Home | History | Annotate | Line # | Download | only in acpi
dwiic_acpi.c revision 1.11
      1 /* $NetBSD: dwiic_acpi.c,v 1.11 2025/09/15 15:18:42 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 (at) 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.11 2025/09/15 15:18:42 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 	if (!dwiic_attach(sc))
    129 		goto done;
    130 
    131 	iicbus_attach(self, &sc->sc_i2c_tag);
    132 
    133 	pmf_device_register(self, dwiic_suspend, dwiic_resume);
    134 
    135 done:
    136 	acpi_resource_cleanup(&res);
    137 }
    138 
    139 static void
    140 dwiic_acpi_parse_param(struct dwiic_softc *sc, ACPI_HANDLE handle, const char *path,
    141     struct dwiic_acpi_param *param)
    142 {
    143 	ACPI_BUFFER buf;
    144 	ACPI_OBJECT *obj;
    145 
    146 	memset(param, 0, sizeof(*param));
    147 
    148 	if (ACPI_FAILURE(acpi_eval_struct(handle, path, &buf)))
    149 		return;
    150 
    151 	obj = buf.Pointer;
    152 	if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count != 3)
    153 		goto done;
    154 
    155 	param->hcnt = (uint16_t)obj->Package.Elements[0].Integer.Value;
    156 	param->lcnt = (uint16_t)obj->Package.Elements[1].Integer.Value;
    157 	param->ht = (uint32_t)obj->Package.Elements[2].Integer.Value;
    158 
    159 done:
    160 	ACPI_FREE(buf.Pointer);
    161 }
    162 
    163 static void
    164 dwiic_acpi_configure(struct dwiic_softc *sc, ACPI_HANDLE handle)
    165 {
    166 	struct dwiic_acpi_param sscn, fmcn;
    167 
    168 	dwiic_acpi_parse_param(sc, handle, "SSCN", &sscn);
    169 	sc->ss_hcnt = sscn.hcnt;
    170 	sc->ss_lcnt = sscn.lcnt;
    171 
    172 	dwiic_acpi_parse_param(sc, handle, "FMCN", &fmcn);
    173 	sc->fs_hcnt = fmcn.hcnt;
    174 	sc->fs_lcnt = fmcn.lcnt;
    175 
    176 	/* XXX */
    177 	sc->sda_hold_time = fmcn.ht;
    178 }
    179