1 /* $NetBSD: dwiic_acpi.c,v 1.10 2022/10/19 22:28:35 riastradh 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.10 2022/10/19 22:28:35 riastradh 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
dwiic_acpi_match(device_t parent,cfdata_t cf,void * aux)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
dwiic_acpi_attach(device_t parent,device_t self,void * aux)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           if (!dwiic_attach(sc))
131                     goto done;
132 
133           config_found(self, &sc->sc_iba, iicbus_print, CFARGS_NONE);
134 
135           pmf_device_register(self, dwiic_suspend, dwiic_resume);
136 
137 done:
138           acpi_resource_cleanup(&res);
139 }
140 
141 static void
dwiic_acpi_parse_param(struct dwiic_softc * sc,ACPI_HANDLE handle,const char * path,struct dwiic_acpi_param * param)142 dwiic_acpi_parse_param(struct dwiic_softc *sc, ACPI_HANDLE handle, const char *path,
143     struct dwiic_acpi_param *param)
144 {
145           ACPI_BUFFER buf;
146           ACPI_OBJECT *obj;
147 
148           memset(param, 0, sizeof(*param));
149 
150           if (ACPI_FAILURE(acpi_eval_struct(handle, path, &buf)))
151                     return;
152 
153           obj = buf.Pointer;
154           if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count != 3)
155                     goto done;
156 
157           param->hcnt = (uint16_t)obj->Package.Elements[0].Integer.Value;
158           param->lcnt = (uint16_t)obj->Package.Elements[1].Integer.Value;
159           param->ht = (uint32_t)obj->Package.Elements[2].Integer.Value;
160 
161 done:
162           ACPI_FREE(buf.Pointer);
163 }
164 
165 static void
dwiic_acpi_configure(struct dwiic_softc * sc,ACPI_HANDLE handle)166 dwiic_acpi_configure(struct dwiic_softc *sc, ACPI_HANDLE handle)
167 {
168           struct dwiic_acpi_param sscn, fmcn;
169 
170           dwiic_acpi_parse_param(sc, handle, "SSCN", &sscn);
171           sc->ss_hcnt = sscn.hcnt;
172           sc->ss_lcnt = sscn.lcnt;
173 
174           dwiic_acpi_parse_param(sc, handle, "FMCN", &fmcn);
175           sc->fs_hcnt = fmcn.hcnt;
176           sc->fs_lcnt = fmcn.lcnt;
177 
178           /* XXX */
179           sc->sda_hold_time = fmcn.ht;
180 }
181