apple_smc_acpi.c revision 1.4 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),
65 apple_smc_acpi_match,
66 apple_smc_acpi_attach,
67 apple_smc_acpi_detach,
68 NULL /* activate */,
69 apple_smc_acpi_rescan,
70 apple_smc_acpi_child_detached);
71
72 static const char *const apple_smc_ids[] = {
73 "APP0001",
74 NULL
75 };
76
77 static int
78 apple_smc_acpi_match(device_t parent, cfdata_t match, void *aux)
79 {
80 struct acpi_attach_args *aa = aux;
81
82 if (aa->aa_node->ad_type != ACPI_TYPE_DEVICE)
83 return 0;
84
85 if (!acpi_match_hid(aa->aa_node->ad_devinfo, apple_smc_ids))
86 return 0;
87
88 return 1;
89 }
90
91 static void
92 apple_smc_acpi_attach(device_t parent, device_t self, void *aux)
93 {
94 struct apple_smc_acpi_softc *sc = device_private(self);
95 struct apple_smc_tag *smc = &sc->sc_smc;
96 struct acpi_attach_args *aa = aux;
97 struct acpi_resources res;
98 struct acpi_io *io;
99 int rv;
100
101 smc->smc_dev = self;
102
103 aprint_normal("\n");
104 aprint_naive("\n");
105
106 rv = acpi_resource_parse(self, aa->aa_node->ad_handle, "_CRS",
107 &res, &acpi_resource_parse_ops_default);
108 if (ACPI_FAILURE(rv)) {
109 aprint_error_dev(self, "couldn't parse SMC resources: %s\n",
110 AcpiFormatException(rv));
111 goto out0;
112 }
113
114 io = acpi_res_io(&res, 0);
115 if (io == NULL) {
116 aprint_error_dev(self, "no I/O resource\n");
117 goto out1;
118 }
119
120 if (io->ar_length < APPLE_SMC_REGSIZE) {
121 aprint_error_dev(self, "I/O resources too small: %"PRId32"\n",
122 io->ar_length);
123 goto out1;
124 }
125
126 if (bus_space_map(aa->aa_iot, io->ar_base, io->ar_length, 0,
127 &smc->smc_bsh) != 0) {
128 aprint_error_dev(self, "unable to map I/O registers\n");
129 goto out1;
130 }
131
132 smc->smc_bst = aa->aa_iot;
133 smc->smc_size = io->ar_length;
134
135 apple_smc_attach(smc);
136
137 out1: acpi_resource_cleanup(&res);
138 out0: return;
139 }
140
141 static int
142 apple_smc_acpi_detach(device_t self, int flags)
143 {
144 struct apple_smc_acpi_softc *sc = device_private(self);
145 struct apple_smc_tag *smc = &sc->sc_smc;
146 int error;
147
148 if (smc->smc_size != 0) {
149 error = apple_smc_detach(smc, flags);
150 if (error)
151 return error;
152
153 bus_space_unmap(smc->smc_bst, smc->smc_bsh, smc->smc_size);
154 smc->smc_size = 0;
155 }
156
157 return 0;
158 }
159
160 static int
161 apple_smc_acpi_rescan(device_t self, const char *ifattr, const int *locs)
162 {
163 struct apple_smc_acpi_softc *const sc = device_private(self);
164
165 return apple_smc_rescan(&sc->sc_smc, ifattr, locs);
166 }
167
168 static void
169 apple_smc_acpi_child_detached(device_t self, device_t child)
170 {
171 struct apple_smc_acpi_softc *const sc = device_private(self);
172
173 apple_smc_child_detached(&sc->sc_smc, child);
174 }
175
176 MODULE(MODULE_CLASS_DRIVER, apple_smc_acpi, "apple_smc");
178
179 #ifdef _MODULE
180 #include "ioconf.c"
181 #endif
182
183 static int
184 apple_smc_acpi_modcmd(modcmd_t cmd, void *arg __unused)
185 {
186 #ifdef _MODULE
187 int error;
188 #endif
189
190 switch (cmd) {
191 case MODULE_CMD_INIT:
192 #ifdef _MODULE
193 error = config_init_component(cfdriver_ioconf_apple_smc_acpi,
194 cfattach_ioconf_apple_smc_acpi,
195 cfdata_ioconf_apple_smc_acpi);
196 if (error)
197 return error;
198 #endif
199 return 0;
200
201 case MODULE_CMD_FINI:
202 #ifdef _MODULE
203 error = config_fini_component(cfdriver_ioconf_apple_smc_acpi,
204 cfattach_ioconf_apple_smc_acpi,
205 cfdata_ioconf_apple_smc_acpi);
206 if (error)
207 return error;
208 #endif
209 return 0;
210
211 default:
212 return ENOTTY;
213 }
214 }
215