dwiic_acpi.c revision 1.4 1 /* $NetBSD: dwiic_acpi.c,v 1.4 2020/12/07 10:02:51 jmcneill 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.4 2020/12/07 10:02:51 jmcneill 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 char * const compatible[] = {
63 "AMDI0510", /* AMD Seattle */
64 "APMC0D0F", /* Ampere eMAG */
65 NULL
66 };
67
68 static int
69 dwiic_acpi_match(device_t parent, cfdata_t cf, void *aux)
70 {
71 struct acpi_attach_args *aa = aux;
72
73 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
74 return 0;
75
76 return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
77 }
78
79 static void
80 dwiic_acpi_attach(device_t parent, device_t self, void *aux)
81 {
82 struct dwiic_softc * const sc = device_private(self);
83 struct acpi_attach_args *aa = aux;
84 struct acpi_resources res;
85 struct acpi_mem *mem;
86 struct acpi_irq *irq;
87 ACPI_STATUS rv;
88 int error;
89 void *ih;
90
91 sc->sc_dev = self;
92 sc->sc_type = dwiic_type_generic;
93 sc->sc_iot = aa->aa_memt;
94
95 rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
96 &res, &acpi_resource_parse_ops_default);
97 if (ACPI_FAILURE(rv))
98 return;
99
100 mem = acpi_res_mem(&res, 0);
101 if (mem == NULL) {
102 aprint_error_dev(self, "couldn't find mem resource\n");
103 goto done;
104 }
105
106 irq = acpi_res_irq(&res, 0);
107 if (irq == NULL) {
108 aprint_error_dev(self, "couldn't find irq resource\n");
109 goto done;
110 }
111
112 error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
113 if (error) {
114 aprint_error_dev(self, "couldn't map registers\n");
115 return;
116 }
117
118 ih = acpi_intr_establish(self,
119 (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
120 IPL_VM, true, dwiic_intr, sc, device_xname(self));
121 if (ih == NULL) {
122 aprint_error_dev(self, "couldn't install interrupt handler\n");
123 bus_space_unmap(sc->sc_iot, sc->sc_ioh, mem->ar_length);
124 goto done;
125 }
126
127 dwiic_acpi_configure(sc, aa->aa_node->ad_handle);
128
129 sc->sc_iba.iba_child_devices = acpi_enter_i2c_devs(aa->aa_node);
130
131 dwiic_attach(sc);
132
133 config_found_ia(self, "i2cbus", &sc->sc_iba, iicbus_print);
134
135 pmf_device_register(self, dwiic_suspend, dwiic_resume);
136
137 done:
138 acpi_resource_cleanup(&res);
139 }
140
141 static void
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
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