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