summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/dwiic_acpi.c
blob: 6c8b32c817022f34d4f36b24c1cf94b8e7d5a9b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* $NetBSD: dwiic_acpi.c,v 1.10 2022/10/19 22:28:35 riastradh Exp $ */

/*-
 * Copyright (c) 2018 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Jared McNeill <jmcneill@invisible.ca>.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: dwiic_acpi.c,v 1.10 2022/10/19 22:28:35 riastradh Exp $");

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cpu.h>
#include <sys/device.h>

#include <dev/acpi/acpireg.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_intr.h>
#include <dev/acpi/acpi_i2c.h>

#include <dev/ic/dwiic_var.h>

struct dwiic_acpi_param {
	uint16_t hcnt;
	uint16_t lcnt;
	uint32_t ht;
};

static int	dwiic_acpi_match(device_t, cfdata_t, void *);
static void	dwiic_acpi_attach(device_t, device_t, void *);

static void	dwiic_acpi_parse_param(struct dwiic_softc *, ACPI_HANDLE,
					  const char *, struct dwiic_acpi_param *);
static void	dwiic_acpi_configure(struct dwiic_softc *, ACPI_HANDLE);

CFATTACH_DECL_NEW(dwiic_acpi, sizeof(struct dwiic_softc), dwiic_acpi_match, dwiic_acpi_attach, NULL, NULL);

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "AMD0010" },	/* AMD FCH */
	{ .compat = "AMDI0010" },	/* AMD FCH */
	{ .compat = "AMDI0510" },	/* AMD Seattle */
	{ .compat = "APMC0D0F" },	/* Ampere eMAG */
	DEVICE_COMPAT_EOL
};

static int
dwiic_acpi_match(device_t parent, cfdata_t cf, void *aux)
{
	struct acpi_attach_args *aa = aux;

	return acpi_compatible_match(aa, compat_data);
}

static void
dwiic_acpi_attach(device_t parent, device_t self, void *aux)
{
	struct dwiic_softc * const sc = device_private(self);
	struct acpi_attach_args *aa = aux;
	struct acpi_resources res;
	struct acpi_mem *mem;
	struct acpi_irq *irq;
	ACPI_STATUS rv;
	int error;
	void *ih;

	sc->sc_dev = self;
	sc->sc_type = dwiic_type_generic;
	sc->sc_iot = aa->aa_memt;

	rv = acpi_resource_parse(sc->sc_dev, aa->aa_node->ad_handle, "_CRS",
	    &res, &acpi_resource_parse_ops_default);
	if (ACPI_FAILURE(rv))
		return;

	mem = acpi_res_mem(&res, 0);
	if (mem == NULL) {
		aprint_error_dev(self, "couldn't find mem resource\n");
		goto done;
	}

	irq = acpi_res_irq(&res, 0);
	if (irq == NULL) {
		aprint_error_dev(self, "couldn't find irq resource\n");
		goto done;
	}

	error = bus_space_map(sc->sc_iot, mem->ar_base, mem->ar_length, 0, &sc->sc_ioh);
	if (error) {
		aprint_error_dev(self, "couldn't map registers\n");
		return;
	}

	ih = acpi_intr_establish(self,
	    (uint64_t)(uintptr_t)aa->aa_node->ad_handle,
	    IPL_VM, true, dwiic_intr, sc, device_xname(self));
	if (ih == NULL) {
		aprint_error_dev(self, "couldn't install interrupt handler\n");
		bus_space_unmap(sc->sc_iot, sc->sc_ioh, mem->ar_length);
		goto done;
	}

	dwiic_acpi_configure(sc, aa->aa_node->ad_handle);

	sc->sc_iba.iba_child_devices = acpi_enter_i2c_devs(self, aa->aa_node);

	if (!dwiic_attach(sc))
		goto done;

	config_found(self, &sc->sc_iba, iicbus_print, CFARGS_NONE);

	pmf_device_register(self, dwiic_suspend, dwiic_resume);

done:
	acpi_resource_cleanup(&res);
}

static void
dwiic_acpi_parse_param(struct dwiic_softc *sc, ACPI_HANDLE handle, const char *path,
    struct dwiic_acpi_param *param)
{
	ACPI_BUFFER buf;
	ACPI_OBJECT *obj;

	memset(param, 0, sizeof(*param));

	if (ACPI_FAILURE(acpi_eval_struct(handle, path, &buf)))
		return;

	obj = buf.Pointer;
	if (obj->Type != ACPI_TYPE_PACKAGE || obj->Package.Count != 3)
		goto done;

	param->hcnt = (uint16_t)obj->Package.Elements[0].Integer.Value;
	param->lcnt = (uint16_t)obj->Package.Elements[1].Integer.Value;
	param->ht = (uint32_t)obj->Package.Elements[2].Integer.Value;

done:
	ACPI_FREE(buf.Pointer);
}

static void
dwiic_acpi_configure(struct dwiic_softc *sc, ACPI_HANDLE handle)
{
	struct dwiic_acpi_param sscn, fmcn;

	dwiic_acpi_parse_param(sc, handle, "SSCN", &sscn);
	sc->ss_hcnt = sscn.hcnt;
	sc->ss_lcnt = sscn.lcnt;

	dwiic_acpi_parse_param(sc, handle, "FMCN", &fmcn);
	sc->fs_hcnt = fmcn.hcnt;
	sc->fs_lcnt = fmcn.lcnt;

	/* XXX */
	sc->sda_hold_time = fmcn.ht;
}