summaryrefslogtreecommitdiff
path: root/sys/dev/acpi/apple_smc_acpi.c
blob: 317015ed5faf3a8f9675e9db727bdea466577e2f (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*	$NetBSD: apple_smc_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $	*/

/*
 * Apple System Management Controller: ACPI Attachment
 */

/*-
 * Copyright (c) 2013 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Taylor R. Campbell.
 *
 * 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: apple_smc_acpi.c,v 1.5 2021/01/29 15:49:55 thorpej Exp $");

#include <sys/types.h>
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/module.h>

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

#include <dev/ic/apple_smcreg.h>
#include <dev/ic/apple_smcvar.h>

#define _COMPONENT		ACPI_RESOURCE_COMPONENT
ACPI_MODULE_NAME		("apple_smc_acpi")

struct apple_smc_acpi_softc {
	struct apple_smc_tag	sc_smc;
};

static int	apple_smc_acpi_match(device_t, cfdata_t, void *);
static void	apple_smc_acpi_attach(device_t, device_t, void *);
static int	apple_smc_acpi_detach(device_t, int);
static int	apple_smc_acpi_rescan(device_t, const char *, const int *);
static void	apple_smc_acpi_child_detached(device_t, device_t);

CFATTACH_DECL2_NEW(apple_smc_acpi, sizeof(struct apple_smc_acpi_softc),
    apple_smc_acpi_match,
    apple_smc_acpi_attach,
    apple_smc_acpi_detach,
    NULL /* activate */,
    apple_smc_acpi_rescan,
    apple_smc_acpi_child_detached);

static const struct device_compatible_entry compat_data[] = {
	{ .compat = "APP0001" },
	DEVICE_COMPAT_EOL
};

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

	return acpi_compatible_match(aa, compat_data);
}

static void
apple_smc_acpi_attach(device_t parent, device_t self, void *aux)
{
	struct apple_smc_acpi_softc *sc = device_private(self);
	struct apple_smc_tag *smc = &sc->sc_smc;
	struct acpi_attach_args *aa = aux;
	struct acpi_resources res;
	struct acpi_io *io;
	int rv;

	smc->smc_dev = self;

	aprint_normal("\n");
	aprint_naive("\n");

	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
	    &res, &acpi_resource_parse_ops_default);
	if (ACPI_FAILURE(rv)) {
		aprint_error_dev(self, "couldn't parse SMC resources: %s\n",
		    AcpiFormatException(rv));
		goto out0;
	}

	io = acpi_res_io(&res, 0);
	if (io == NULL) {
		aprint_error_dev(self, "no I/O resource\n");
		goto out1;
	}

	if (io->ar_length < APPLE_SMC_REGSIZE) {
		aprint_error_dev(self, "I/O resources too small: %"PRId32"\n",
		    io->ar_length);
		goto out1;
	}

	if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0,
		&smc->smc_bsh) != 0) {
		aprint_error_dev(self, "unable to map I/O registers\n");
		goto out1;
	}

	smc->smc_bst = aa->aa_iot;
	smc->smc_size = io->ar_length;

	apple_smc_attach(smc);

out1:	acpi_resource_cleanup(&res);
out0:	return;
}

static int
apple_smc_acpi_detach(device_t self, int flags)
{
	struct apple_smc_acpi_softc *sc = device_private(self);
	struct apple_smc_tag *smc = &sc->sc_smc;
	int error;

	if (smc->smc_size != 0) {
		error = apple_smc_detach(smc, flags);
		if (error)
			return error;

		bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size);
		smc->smc_size = 0;
	}

	return 0;
}

static int
apple_smc_acpi_rescan(device_t self, const char *ifattr, const int *locs)
{
	struct apple_smc_acpi_softc *const sc = device_private(self);

	return apple_smc_rescan(&sc->sc_smc, ifattr, locs);
}

static void
apple_smc_acpi_child_detached(device_t self, device_t child)
{
	struct apple_smc_acpi_softc *const sc = device_private(self);

	apple_smc_child_detached(&sc->sc_smc, child);
}

MODULE(MODULE_CLASS_DRIVER, apple_smc_acpi, "apple_smc");

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
apple_smc_acpi_modcmd(modcmd_t cmd, void *arg __unused)
{
#ifdef _MODULE
	int error;
#endif

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = config_init_component(cfdriver_ioconf_apple_smc_acpi,
		    cfattach_ioconf_apple_smc_acpi,
		    cfdata_ioconf_apple_smc_acpi);
		if (error)
			return error;
#endif
		return 0;

	case MODULE_CMD_FINI:
#ifdef _MODULE
		error = config_fini_component(cfdriver_ioconf_apple_smc_acpi,
		    cfattach_ioconf_apple_smc_acpi,
		    cfdata_ioconf_apple_smc_acpi);
		if (error)
			return error;
#endif
		return 0;

	default:
		return ENOTTY;
	}
}