xref: /netbsd-src/sys/dev/acpi/apple_smc_acpi.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: apple_smc_acpi.c,v 1.4 2017/05/22 14:07:00 riastradh Exp $	*/
2 
3 /*
4  * Apple System Management Controller: ACPI Attachment
5  */
6 
7 /*-
8  * Copyright (c) 2013 The NetBSD Foundation, Inc.
9  * All rights reserved.
10  *
11  * This code is derived from software contributed to The NetBSD Foundation
12  * by Taylor R. Campbell.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: apple_smc_acpi.c,v 1.4 2017/05/22 14:07:00 riastradh Exp $");
38 
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 
44 #include <dev/acpi/acpireg.h>
45 #include <dev/acpi/acpivar.h>
46 
47 #include <dev/ic/apple_smcreg.h>
48 #include <dev/ic/apple_smcvar.h>
49 
50 #define _COMPONENT		ACPI_RESOURCE_COMPONENT
51 ACPI_MODULE_NAME		("apple_smc_acpi")
52 
53 struct apple_smc_acpi_softc {
54 	struct apple_smc_tag	sc_smc;
55 };
56 
57 static int	apple_smc_acpi_match(device_t, cfdata_t, void *);
58 static void	apple_smc_acpi_attach(device_t, device_t, void *);
59 static int	apple_smc_acpi_detach(device_t, int);
60 static int	apple_smc_acpi_rescan(device_t, const char *, const int *);
61 static void	apple_smc_acpi_child_detached(device_t, device_t);
62 
63 CFATTACH_DECL2_NEW(apple_smc_acpi, sizeof(struct apple_smc_acpi_softc),
64     apple_smc_acpi_match,
65     apple_smc_acpi_attach,
66     apple_smc_acpi_detach,
67     NULL /* activate */,
68     apple_smc_acpi_rescan,
69     apple_smc_acpi_child_detached);
70 
71 static const char *const apple_smc_ids[] = {
72 	"APP0001",
73 	NULL
74 };
75 
76 static int
77 apple_smc_acpi_match(device_t parent, cfdata_t match, void *aux)
78 {
79 	struct acpi_attach_args *aa = aux;
80 
81 	if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
82 		return 0;
83 
84 	if (!acpi_match_hid(aa->aa_node->ad_devinfo, apple_smc_ids))
85 		return 0;
86 
87 	return 1;
88 }
89 
90 static void
91 apple_smc_acpi_attach(device_t parent, device_t self, void *aux)
92 {
93 	struct apple_smc_acpi_softc *sc = device_private(self);
94 	struct apple_smc_tag *smc = &sc->sc_smc;
95 	struct acpi_attach_args *aa = aux;
96 	struct acpi_resources res;
97 	struct acpi_io *io;
98 	int rv;
99 
100 	smc->smc_dev = self;
101 
102 	aprint_normal("\n");
103 	aprint_naive("\n");
104 
105 	rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
106 	    &res, &acpi_resource_parse_ops_default);
107 	if (ACPI_FAILURE(rv)) {
108 		aprint_error_dev(self, "couldn't parse SMC resources: %s\n",
109 		    AcpiFormatException(rv));
110 		goto out0;
111 	}
112 
113 	io = acpi_res_io(&res, 0);
114 	if (io == NULL) {
115 		aprint_error_dev(self, "no I/O resource\n");
116 		goto out1;
117 	}
118 
119 	if (io->ar_length < APPLE_SMC_REGSIZE) {
120 		aprint_error_dev(self, "I/O resources too small: %"PRId32"\n",
121 		    io->ar_length);
122 		goto out1;
123 	}
124 
125 	if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0,
126 		&smc->smc_bsh) != 0) {
127 		aprint_error_dev(self, "unable to map I/O registers\n");
128 		goto out1;
129 	}
130 
131 	smc->smc_bst = aa->aa_iot;
132 	smc->smc_size = io->ar_length;
133 
134 	apple_smc_attach(smc);
135 
136 out1:	acpi_resource_cleanup(&res);
137 out0:	return;
138 }
139 
140 static int
141 apple_smc_acpi_detach(device_t self, int flags)
142 {
143 	struct apple_smc_acpi_softc *sc = device_private(self);
144 	struct apple_smc_tag *smc = &sc->sc_smc;
145 	int error;
146 
147 	if (smc->smc_size != 0) {
148 		error = apple_smc_detach(smc, flags);
149 		if (error)
150 			return error;
151 
152 		bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size);
153 		smc->smc_size = 0;
154 	}
155 
156 	return 0;
157 }
158 
159 static int
160 apple_smc_acpi_rescan(device_t self, const char *ifattr, const int *locs)
161 {
162 	struct apple_smc_acpi_softc *const sc = device_private(self);
163 
164 	return apple_smc_rescan(&sc->sc_smc, ifattr, locs);
165 }
166 
167 static void
168 apple_smc_acpi_child_detached(device_t self, device_t child)
169 {
170 	struct apple_smc_acpi_softc *const sc = device_private(self);
171 
172 	apple_smc_child_detached(&sc->sc_smc, child);
173 }
174 
175 MODULE(MODULE_CLASS_DRIVER, apple_smc_acpi, "apple_smc");
176 
177 #ifdef _MODULE
178 #include "ioconf.c"
179 #endif
180 
181 static int
182 apple_smc_acpi_modcmd(modcmd_t cmd, void *arg __unused)
183 {
184 #ifdef _MODULE
185 	int error;
186 #endif
187 
188 	switch (cmd) {
189 	case MODULE_CMD_INIT:
190 #ifdef _MODULE
191 		error = config_init_component(cfdriver_ioconf_apple_smc_acpi,
192 		    cfattach_ioconf_apple_smc_acpi,
193 		    cfdata_ioconf_apple_smc_acpi);
194 		if (error)
195 			return error;
196 #endif
197 		return 0;
198 
199 	case MODULE_CMD_FINI:
200 #ifdef _MODULE
201 		error = config_fini_component(cfdriver_ioconf_apple_smc_acpi,
202 		    cfattach_ioconf_apple_smc_acpi,
203 		    cfdata_ioconf_apple_smc_acpi);
204 		if (error)
205 			return error;
206 #endif
207 		return 0;
208 
209 	default:
210 		return ENOTTY;
211 	}
212 }
213