dwiic_acpi.c revision 1.12 1 /* $NetBSD: dwiic_acpi.c,v 1.12 2025/09/15 15:31:45 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.12 2025/09/15 15:31:45 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
44 #include <dev/ic/dwiic_var.h>
45
46 struct dwiic_acpi_param {
47 uint16_t hcnt;
48 uint16_t lcnt;
49 uint32_t ht;
50 };
51
52 static int dwiic_acpi_match(device_t, cfdata_t, void *);
53 static void dwiic_acpi_attach(device_t, device_t, void *);
54
55 static void dwiic_acpi_parse_param(struct dwiic_softc *, ACPI_HANDLE,
56 const char *, struct dwiic_acpi_param *);
57 static void dwiic_acpi_configure(struct dwiic_softc *, ACPI_HANDLE);
58
59 CFATTACH_DECL_NEW(dwiic_acpi, sizeof(struct dwiic_softc), dwiic_acpi_match, dwiic_acpi_attach, NULL, NULL);
60
61 static const struct device_compatible_entry compat_data[] = {
62 { .compat = "AMD0010" }, /* AMD FCH */
63 { .compat = "AMDI0010" }, /* AMD FCH */
64 { .compat = "AMDI0510" }, /* AMD Seattle */
65 { .compat = "APMC0D0F" }, /* Ampere eMAG */
66 DEVICE_COMPAT_EOL
67 };
68
69 static int
70 dwiic_acpi_match(device_t parent, cfdata_t cf, void *aux)
71 {
72 struct acpi_attach_args *aa = aux;
73
74 return acpi_compatible_match(aa, compat_data);
75 }
76
77 static void
78 dwiic_acpi_attach(device_t parent, device_t self, void *aux)
79 {
80 struct dwiic_softc * const sc = device_private(self);
81 struct acpi_attach_args *aa = aux;
82 struct acpi_resources res;
83 struct acpi_mem *mem;
84 struct acpi_irq *irq;
85 ACPI_STATUS rv;
86 int error;
87 void *ih;
88
89 sc->sc_dev = self;
90 sc->sc_type = dwiic_type_generic;
91 sc->sc_iot = aa->aa_memt;
92
93 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
94 &res, &acpi_resource_parse_ops_default);
95 if (ACPI_FAILURE(rv))
96 return;
97
98 mem = acpi_res_mem(&res, 0);
99 if (mem == NULL) {
100 aprint_error_dev(self, "couldn't find mem resource\n");
101 goto done;
102 }
103
104 irq = acpi_res_irq(&res, 0);
105 if (irq == NULL) {
106 aprint_error_dev(self, "couldn't find irq resource\n");
107 goto done;
108 }
109
110 error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
111 if (error) {
112 aprint_error_dev(self, "couldn't map registers\n");
113 return;
114 }
115
116 ih = acpi_intr_establish(self,
117 (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
118 IPL_VM, true, dwiic_intr, sc, device_xname(self));
119 if (ih == NULL) {
120 aprint_error_dev(self, "couldn't install interrupt handler\n");
121 bus_space_unmap(sc->sc_iot, sc->sc_ioh, mem->ar_length);
122 goto done;
123 }
124
125 dwiic_acpi_configure(sc, aa->aa_node->ad_handle);
126
127 if (!dwiic_attach(sc))
128 goto done;
129
130 iicbus_attach(self, &sc->sc_i2c_tag);
131
132 pmf_device_register(self, dwiic_suspend, dwiic_resume);
133
134 done:
135 acpi_resource_cleanup(&res);
136 }
137
138 static void
139 dwiic_acpi_parse_param(struct dwiic_softc *sc, ACPI_HANDLE handle, const char *path,
140 struct dwiic_acpi_param *param)
141 {
142 ACPI_BUFFER buf;
143 ACPI_OBJECT *obj;
144
145 memset(param, 0, sizeof(*param));
146
147 if (ACPI_FAILURE(acpi_eval_struct(handle, path, &buf)))
148 return;
149
150 obj = buf.Pointer;
151 if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count != 3)
152 goto done;
153
154 param->hcnt = (uint16_t)obj->Package.Elements[0].Integer.Value;
155 param->lcnt = (uint16_t)obj->Package.Elements[1].Integer.Value;
156 param->ht = (uint32_t)obj->Package.Elements[2].Integer.Value;
157
158 done:
159 ACPI_FREE(buf.Pointer);
160 }
161
162 static void
163 dwiic_acpi_configure(struct dwiic_softc *sc, ACPI_HANDLE handle)
164 {
165 struct dwiic_acpi_param sscn, fmcn;
166
167 dwiic_acpi_parse_param(sc, handle, "SSCN", &sscn);
168 sc->ss_hcnt = sscn.hcnt;
169 sc->ss_lcnt = sscn.lcnt;
170
171 dwiic_acpi_parse_param(sc, handle, "FMCN", &fmcn);
172 sc->fs_hcnt = fmcn.hcnt;
173 sc->fs_lcnt = fmcn.lcnt;
174
175 /* XXX */
176 sc->sda_hold_time = fmcn.ht;
177 }
178