dwiic_acpi.c revision 1.2.4.2 1 1.2.4.2 christos /* $NetBSD: dwiic_acpi.c,v 1.2.4.2 2019/06/10 22:07:05 christos 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.2 christos __KERNEL_RCSID(0, "$NetBSD: dwiic_acpi.c,v 1.2.4.2 2019/06/10 22:07:05 christos 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.2 christos "AMDI0510",
64 1.2.4.2 christos NULL
65 1.2.4.2 christos };
66 1.2.4.2 christos
67 1.2.4.2 christos static int
68 1.2.4.2 christos dwiic_acpi_match(device_t parent, cfdata_t cf, void *aux)
69 1.2.4.2 christos {
70 1.2.4.2 christos struct acpi_attach_args *aa = aux;
71 1.2.4.2 christos
72 1.2.4.2 christos if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
73 1.2.4.2 christos return 0;
74 1.2.4.2 christos
75 1.2.4.2 christos return acpi_match_hid(aa->aa_node->ad_devinfo, compatible);
76 1.2.4.2 christos }
77 1.2.4.2 christos
78 1.2.4.2 christos static void
79 1.2.4.2 christos dwiic_acpi_attach(device_t parent, device_t self, void *aux)
80 1.2.4.2 christos {
81 1.2.4.2 christos struct dwiic_softc * const sc = device_private(self);
82 1.2.4.2 christos struct acpi_attach_args *aa = aux;
83 1.2.4.2 christos struct acpi_resources res;
84 1.2.4.2 christos struct acpi_mem *mem;
85 1.2.4.2 christos struct acpi_irq *irq;
86 1.2.4.2 christos ACPI_STATUS rv;
87 1.2.4.2 christos int error;
88 1.2.4.2 christos void *ih;
89 1.2.4.2 christos
90 1.2.4.2 christos sc->sc_dev = self;
91 1.2.4.2 christos sc->sc_type = dwiic_type_generic;
92 1.2.4.2 christos sc->sc_iot = aa->aa_memt;
93 1.2.4.2 christos
94 1.2.4.2 christos rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
95 1.2.4.2 christos &res, &acpi_resource_parse_ops_default);
96 1.2.4.2 christos if (ACPI_FAILURE(rv))
97 1.2.4.2 christos return;
98 1.2.4.2 christos
99 1.2.4.2 christos mem = acpi_res_mem(&res, 0);
100 1.2.4.2 christos if (mem == NULL) {
101 1.2.4.2 christos aprint_error_dev(self, "couldn't find mem resource\n");
102 1.2.4.2 christos goto done;
103 1.2.4.2 christos }
104 1.2.4.2 christos
105 1.2.4.2 christos irq = acpi_res_irq(&res, 0);
106 1.2.4.2 christos if (irq == NULL) {
107 1.2.4.2 christos aprint_error_dev(self, "couldn't find irq resource\n");
108 1.2.4.2 christos goto done;
109 1.2.4.2 christos }
110 1.2.4.2 christos
111 1.2.4.2 christos error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
112 1.2.4.2 christos if (error) {
113 1.2.4.2 christos aprint_error_dev(self, "couldn't map registers\n");
114 1.2.4.2 christos return;
115 1.2.4.2 christos }
116 1.2.4.2 christos
117 1.2.4.2 christos ih = acpi_intr_establish(self, (uint64_t)aa->aa_node->ad_handle,
118 1.2.4.2 christos IPL_VM, true, dwiic_intr, sc, device_xname(self));
119 1.2.4.2 christos if (ih == NULL) {
120 1.2.4.2 christos aprint_error_dev(self, "couldn't install interrupt handler\n");
121 1.2.4.2 christos bus_space_unmap(sc->sc_iot, sc->sc_ioh, mem->ar_length);
122 1.2.4.2 christos goto done;
123 1.2.4.2 christos }
124 1.2.4.2 christos
125 1.2.4.2 christos dwiic_acpi_configure(sc, aa->aa_node->ad_handle);
126 1.2.4.2 christos
127 1.2.4.2 christos sc->sc_iba.iba_child_devices = acpi_enter_i2c_devs(aa->aa_node);
128 1.2.4.2 christos
129 1.2.4.2 christos dwiic_attach(sc);
130 1.2.4.2 christos
131 1.2.4.2 christos config_found_ia(self, "i2cbus", &sc->sc_iba, iicbus_print);
132 1.2.4.2 christos
133 1.2.4.2 christos pmf_device_register(self, dwiic_suspend, dwiic_resume);
134 1.2.4.2 christos
135 1.2.4.2 christos done:
136 1.2.4.2 christos acpi_resource_cleanup(&res);
137 1.2.4.2 christos }
138 1.2.4.2 christos
139 1.2.4.2 christos static void
140 1.2.4.2 christos dwiic_acpi_parse_param(struct dwiic_softc *sc, ACPI_HANDLE handle, const char *path,
141 1.2.4.2 christos struct dwiic_acpi_param *param)
142 1.2.4.2 christos {
143 1.2.4.2 christos ACPI_BUFFER buf;
144 1.2.4.2 christos ACPI_OBJECT *obj;
145 1.2.4.2 christos
146 1.2.4.2 christos memset(param, 0, sizeof(*param));
147 1.2.4.2 christos
148 1.2.4.2 christos if (ACPI_FAILURE(acpi_eval_struct(handle, path, &buf)))
149 1.2.4.2 christos return;
150 1.2.4.2 christos
151 1.2.4.2 christos obj = buf.Pointer;
152 1.2.4.2 christos if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count != 3)
153 1.2.4.2 christos goto done;
154 1.2.4.2 christos
155 1.2.4.2 christos param->hcnt = (uint16_t)obj->Package.Elements[0].Integer.Value;
156 1.2.4.2 christos param->lcnt = (uint16_t)obj->Package.Elements[1].Integer.Value;
157 1.2.4.2 christos param->ht = (uint32_t)obj->Package.Elements[2].Integer.Value;
158 1.2.4.2 christos
159 1.2.4.2 christos done:
160 1.2.4.2 christos ACPI_FREE(buf.Pointer);
161 1.2.4.2 christos }
162 1.2.4.2 christos
163 1.2.4.2 christos static void
164 1.2.4.2 christos dwiic_acpi_configure(struct dwiic_softc *sc, ACPI_HANDLE handle)
165 1.2.4.2 christos {
166 1.2.4.2 christos struct dwiic_acpi_param sscn, fmcn;
167 1.2.4.2 christos
168 1.2.4.2 christos dwiic_acpi_parse_param(sc, handle, "SSCN", &sscn);
169 1.2.4.2 christos sc->ss_hcnt = sscn.hcnt;
170 1.2.4.2 christos sc->ss_lcnt = sscn.lcnt;
171 1.2.4.2 christos
172 1.2.4.2 christos dwiic_acpi_parse_param(sc, handle, "FMCN", &fmcn);
173 1.2.4.2 christos sc->fs_hcnt = fmcn.hcnt;
174 1.2.4.2 christos sc->fs_lcnt = fmcn.lcnt;
175 1.2.4.2 christos
176 1.2.4.2 christos /* XXX */
177 1.2.4.2 christos sc->sda_hold_time = fmcn.ht;
178 1.2.4.2 christos }
179