1*11f1e5f6Sskrll /* $NetBSD: apple_smc.c,v 1.1 2022/05/10 08:09:57 skrll Exp $ */
2*11f1e5f6Sskrll /* $OpenBSD: apple_smc.c,v 1.11 2022/03/25 15:52:03 kettenis Exp $ */
3*11f1e5f6Sskrll
4*11f1e5f6Sskrll /*-
5*11f1e5f6Sskrll * Copyright (c) 2022 The NetBSD Foundation, Inc.
6*11f1e5f6Sskrll * All rights reserved.
7*11f1e5f6Sskrll *
8*11f1e5f6Sskrll * This code is derived from software contributed to The NetBSD Foundation
9*11f1e5f6Sskrll * by Nick Hudson
10*11f1e5f6Sskrll *
11*11f1e5f6Sskrll * Redistribution and use in source and binary forms, with or without
12*11f1e5f6Sskrll * modification, are permitted provided that the following conditions
13*11f1e5f6Sskrll * are met:
14*11f1e5f6Sskrll * 1. Redistributions of source code must retain the above copyright
15*11f1e5f6Sskrll * notice, this list of conditions and the following disclaimer.
16*11f1e5f6Sskrll * 2. Redistributions in binary form must reproduce the above copyright
17*11f1e5f6Sskrll * notice, this list of conditions and the following disclaimer in the
18*11f1e5f6Sskrll * documentation and/or other materials provided with the distribution.
19*11f1e5f6Sskrll *
20*11f1e5f6Sskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21*11f1e5f6Sskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22*11f1e5f6Sskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23*11f1e5f6Sskrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24*11f1e5f6Sskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25*11f1e5f6Sskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26*11f1e5f6Sskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27*11f1e5f6Sskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28*11f1e5f6Sskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*11f1e5f6Sskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30*11f1e5f6Sskrll * POSSIBILITY OF SUCH DAMAGE.
31*11f1e5f6Sskrll */
32*11f1e5f6Sskrll
33*11f1e5f6Sskrll /*
34*11f1e5f6Sskrll * Copyright (c) 2021 Mark Kettenis <kettenis@openbsd.org>
35*11f1e5f6Sskrll *
36*11f1e5f6Sskrll * Permission to use, copy, modify, and distribute this software for any
37*11f1e5f6Sskrll * purpose with or without fee is hereby granted, provided that the above
38*11f1e5f6Sskrll * copyright notice and this permission notice appear in all copies.
39*11f1e5f6Sskrll *
40*11f1e5f6Sskrll * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41*11f1e5f6Sskrll * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42*11f1e5f6Sskrll * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43*11f1e5f6Sskrll * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44*11f1e5f6Sskrll * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45*11f1e5f6Sskrll * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46*11f1e5f6Sskrll * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47*11f1e5f6Sskrll */
48*11f1e5f6Sskrll
49*11f1e5f6Sskrll #include <sys/param.h>
50*11f1e5f6Sskrll
51*11f1e5f6Sskrll #include <sys/device.h>
52*11f1e5f6Sskrll #include <sys/kernel.h>
53*11f1e5f6Sskrll #include <sys/kmem.h>
54*11f1e5f6Sskrll
55*11f1e5f6Sskrll #include <dev/fdt/fdtvar.h>
56*11f1e5f6Sskrll
57*11f1e5f6Sskrll #include <arm/apple/apple_rtkit.h>
58*11f1e5f6Sskrll
59*11f1e5f6Sskrll extern void (*cpuresetfn)(void);
60*11f1e5f6Sskrll extern void (*powerdownfn)(void);
61*11f1e5f6Sskrll
62*11f1e5f6Sskrll /* SMC mailbox endpoint */
63*11f1e5f6Sskrll #define SMC_EP 32
64*11f1e5f6Sskrll
65*11f1e5f6Sskrll /* SMC commands */
66*11f1e5f6Sskrll #define SMC_READ_KEY 0x10
67*11f1e5f6Sskrll #define SMC_WRITE_KEY 0x11
68*11f1e5f6Sskrll #define SMC_GET_KEY_BY_INDEX 0x12
69*11f1e5f6Sskrll #define SMC_GET_KEY_INFO 0x13
70*11f1e5f6Sskrll #define SMC_GET_SRAM_ADDR 0x17
71*11f1e5f6Sskrll #define SMC_SRAM_SIZE 0x4000
72*11f1e5f6Sskrll
73*11f1e5f6Sskrll #define SMC_DATA __BITS(63, 32)
74*11f1e5f6Sskrll #define SMC_WLEN __BITS(31, 24)
75*11f1e5f6Sskrll #define SMC_LENGTH __BITS(23, 16)
76*11f1e5f6Sskrll #define SMC_ID __BITS(15, 12)
77*11f1e5f6Sskrll #define SMC_CMD __BITS(7, 0)
78*11f1e5f6Sskrll
79*11f1e5f6Sskrll /* SMC errors */
80*11f1e5f6Sskrll #define SMC_ERROR_MASK __BITS(7, 0)
81*11f1e5f6Sskrll #define SMC_ERROR(d) __SHIFTOUT((d), SMC_ERROR_MASK)
82*11f1e5f6Sskrll #define SMC_OK 0x00
83*11f1e5f6Sskrll #define SMC_KEYNOTFOUND 0x84
84*11f1e5f6Sskrll
85*11f1e5f6Sskrll /* SMC keys */
86*11f1e5f6Sskrll #define SMC_KEY(s) ((s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3])
87*11f1e5f6Sskrll
88*11f1e5f6Sskrll struct smc_key_info {
89*11f1e5f6Sskrll uint8_t size;
90*11f1e5f6Sskrll uint8_t type[4];
91*11f1e5f6Sskrll uint8_t flags;
92*11f1e5f6Sskrll };
93*11f1e5f6Sskrll
94*11f1e5f6Sskrll /* SMC GPIO commands */
95*11f1e5f6Sskrll #define SMC_GPIO_CMD_OUTPUT (0x01 << 24)
96*11f1e5f6Sskrll
97*11f1e5f6Sskrll /* RTC related constants */
98*11f1e5f6Sskrll #define RTC_OFFSET_LEN 6
99*11f1e5f6Sskrll #define SMC_CLKM_LEN 6
100*11f1e5f6Sskrll
101*11f1e5f6Sskrll #define APLSMC_BE __BIT(0)
102*11f1e5f6Sskrll #define APLSMC_HIDDEN __BIT(1)
103*11f1e5f6Sskrll
104*11f1e5f6Sskrll #define APLSMC_MAX_SENSORS 19
105*11f1e5f6Sskrll
106*11f1e5f6Sskrll struct apple_smc_softc {
107*11f1e5f6Sskrll device_t sc_dev;
108*11f1e5f6Sskrll bus_space_tag_t sc_bst;
109*11f1e5f6Sskrll bus_space_handle_t sc_bsh;
110*11f1e5f6Sskrll bus_space_handle_t sc_sram_bsh;
111*11f1e5f6Sskrll
112*11f1e5f6Sskrll struct rtkit_state *sc_rs;
113*11f1e5f6Sskrll uint8_t sc_msgid;
114*11f1e5f6Sskrll uint64_t sc_data;
115*11f1e5f6Sskrll
116*11f1e5f6Sskrll kmutex_t sc_mutex;
117*11f1e5f6Sskrll kcondvar_t sc_cv;
118*11f1e5f6Sskrll bool sc_done;
119*11f1e5f6Sskrll
120*11f1e5f6Sskrll };
121*11f1e5f6Sskrll
122*11f1e5f6Sskrll struct apple_smc_gpio_pin {
123*11f1e5f6Sskrll u_int pin_no;
124*11f1e5f6Sskrll u_int pin_flags;
125*11f1e5f6Sskrll bool pin_actlo;
126*11f1e5f6Sskrll };
127*11f1e5f6Sskrll
128*11f1e5f6Sskrll struct apple_smc_softc *apple_smc_sc;
129*11f1e5f6Sskrll
130*11f1e5f6Sskrll
131*11f1e5f6Sskrll void apple_smc_callback(void *, uint64_t);
132*11f1e5f6Sskrll int apple_smc_send_cmd(struct apple_smc_softc *, uint8_t, uint32_t, uint16_t);
133*11f1e5f6Sskrll int apple_smc_wait_cmd(struct apple_smc_softc *sc);
134*11f1e5f6Sskrll int apple_smc_read_key(struct apple_smc_softc *, uint32_t, void *, size_t);
135*11f1e5f6Sskrll
136*11f1e5f6Sskrll
137*11f1e5f6Sskrll static const struct device_compatible_entry compat_data[] = {
138*11f1e5f6Sskrll { .compat = "apple,smc" },
139*11f1e5f6Sskrll DEVICE_COMPAT_EOL
140*11f1e5f6Sskrll };
141*11f1e5f6Sskrll
142*11f1e5f6Sskrll void
apple_smc_callback(void * arg,uint64_t data)143*11f1e5f6Sskrll apple_smc_callback(void *arg, uint64_t data)
144*11f1e5f6Sskrll {
145*11f1e5f6Sskrll struct apple_smc_softc * const sc = arg;
146*11f1e5f6Sskrll
147*11f1e5f6Sskrll mutex_enter(&sc->sc_mutex);
148*11f1e5f6Sskrll sc->sc_data = data;
149*11f1e5f6Sskrll sc->sc_done = true;
150*11f1e5f6Sskrll cv_signal(&sc->sc_cv);
151*11f1e5f6Sskrll mutex_exit(&sc->sc_mutex);
152*11f1e5f6Sskrll }
153*11f1e5f6Sskrll
154*11f1e5f6Sskrll int
apple_smc_send_cmd(struct apple_smc_softc * sc,uint8_t cmd,uint32_t key,uint16_t len)155*11f1e5f6Sskrll apple_smc_send_cmd(struct apple_smc_softc *sc, uint8_t cmd, uint32_t key,
156*11f1e5f6Sskrll uint16_t len)
157*11f1e5f6Sskrll {
158*11f1e5f6Sskrll uint64_t data =
159*11f1e5f6Sskrll __SHIFTIN(key, SMC_DATA) |
160*11f1e5f6Sskrll __SHIFTIN(cmd, SMC_CMD) |
161*11f1e5f6Sskrll __SHIFTIN(len, SMC_LENGTH) |
162*11f1e5f6Sskrll __SHIFTIN((sc->sc_msgid++ & 0xf), SMC_ID)
163*11f1e5f6Sskrll ;
164*11f1e5f6Sskrll
165*11f1e5f6Sskrll return rtkit_send_endpoint(sc->sc_rs, SMC_EP, data);
166*11f1e5f6Sskrll }
167*11f1e5f6Sskrll
168*11f1e5f6Sskrll int
apple_smc_wait_cmd(struct apple_smc_softc * sc)169*11f1e5f6Sskrll apple_smc_wait_cmd(struct apple_smc_softc *sc)
170*11f1e5f6Sskrll {
171*11f1e5f6Sskrll int error;
172*11f1e5f6Sskrll if (cold) {
173*11f1e5f6Sskrll int timo;
174*11f1e5f6Sskrll
175*11f1e5f6Sskrll /* Poll for completion. */
176*11f1e5f6Sskrll for (timo = 1000; timo > 0; timo--) {
177*11f1e5f6Sskrll error = rtkit_poll(sc->sc_rs);
178*11f1e5f6Sskrll if (error == 0)
179*11f1e5f6Sskrll return 0;
180*11f1e5f6Sskrll delay(10);
181*11f1e5f6Sskrll }
182*11f1e5f6Sskrll
183*11f1e5f6Sskrll return EWOULDBLOCK;
184*11f1e5f6Sskrll }
185*11f1e5f6Sskrll
186*11f1e5f6Sskrll mutex_enter(&sc->sc_mutex);
187*11f1e5f6Sskrll sc->sc_done = false;
188*11f1e5f6Sskrll while (!sc->sc_done) {
189*11f1e5f6Sskrll error = cv_timedwait(&sc->sc_cv, &sc->sc_mutex, hz / 10);
190*11f1e5f6Sskrll if (error)
191*11f1e5f6Sskrll break;
192*11f1e5f6Sskrll }
193*11f1e5f6Sskrll mutex_exit(&sc->sc_mutex);
194*11f1e5f6Sskrll
195*11f1e5f6Sskrll return error;
196*11f1e5f6Sskrll }
197*11f1e5f6Sskrll
198*11f1e5f6Sskrll int
apple_smc_read_key(struct apple_smc_softc * sc,uint32_t key,void * data,size_t len)199*11f1e5f6Sskrll apple_smc_read_key(struct apple_smc_softc *sc, uint32_t key, void *data, size_t len)
200*11f1e5f6Sskrll {
201*11f1e5f6Sskrll int error;
202*11f1e5f6Sskrll
203*11f1e5f6Sskrll apple_smc_send_cmd(sc, SMC_READ_KEY, key, len);
204*11f1e5f6Sskrll error = apple_smc_wait_cmd(sc);
205*11f1e5f6Sskrll if (error)
206*11f1e5f6Sskrll return error;
207*11f1e5f6Sskrll switch (SMC_ERROR(sc->sc_data)) {
208*11f1e5f6Sskrll case SMC_OK:
209*11f1e5f6Sskrll break;
210*11f1e5f6Sskrll case SMC_KEYNOTFOUND:
211*11f1e5f6Sskrll return EINVAL;
212*11f1e5f6Sskrll break;
213*11f1e5f6Sskrll default:
214*11f1e5f6Sskrll return EIO;
215*11f1e5f6Sskrll break;
216*11f1e5f6Sskrll }
217*11f1e5f6Sskrll
218*11f1e5f6Sskrll len = MIN(len, (sc->sc_data >> 16) & 0xffff);
219*11f1e5f6Sskrll if (len > sizeof(uint32_t)) {
220*11f1e5f6Sskrll bus_space_read_region_1(sc->sc_bst, sc->sc_sram_bsh, 0,
221*11f1e5f6Sskrll data, len);
222*11f1e5f6Sskrll } else {
223*11f1e5f6Sskrll uint32_t tmp = (sc->sc_data >> 32);
224*11f1e5f6Sskrll memcpy(data, &tmp, len);
225*11f1e5f6Sskrll }
226*11f1e5f6Sskrll
227*11f1e5f6Sskrll return 0;
228*11f1e5f6Sskrll }
229*11f1e5f6Sskrll
230*11f1e5f6Sskrll static int
apple_smc_write_key(struct apple_smc_softc * sc,uint32_t key,void * data,size_t len)231*11f1e5f6Sskrll apple_smc_write_key(struct apple_smc_softc *sc, uint32_t key, void *data, size_t len)
232*11f1e5f6Sskrll {
233*11f1e5f6Sskrll bus_space_write_region_1(sc->sc_bst, sc->sc_sram_bsh, 0, data, len);
234*11f1e5f6Sskrll bus_space_barrier(sc->sc_bst, sc->sc_sram_bsh, 0, len,
235*11f1e5f6Sskrll BUS_SPACE_BARRIER_WRITE);
236*11f1e5f6Sskrll apple_smc_send_cmd(sc, SMC_WRITE_KEY, key, len);
237*11f1e5f6Sskrll
238*11f1e5f6Sskrll return apple_smc_wait_cmd(sc);
239*11f1e5f6Sskrll }
240*11f1e5f6Sskrll
241*11f1e5f6Sskrll static void *
apple_smc_gpio_acquire(device_t dev,const void * data,size_t len,int flags)242*11f1e5f6Sskrll apple_smc_gpio_acquire(device_t dev, const void *data, size_t len, int flags)
243*11f1e5f6Sskrll {
244*11f1e5f6Sskrll struct apple_smc_gpio_pin *pin;
245*11f1e5f6Sskrll const u_int *gpio = data;
246*11f1e5f6Sskrll
247*11f1e5f6Sskrll if (len != 12)
248*11f1e5f6Sskrll return NULL;
249*11f1e5f6Sskrll
250*11f1e5f6Sskrll const u_int pinno = be32toh(gpio[1]);
251*11f1e5f6Sskrll const bool actlo = be32toh(gpio[2]) & 1;
252*11f1e5f6Sskrll
253*11f1e5f6Sskrll if (pinno >= 256)
254*11f1e5f6Sskrll return NULL;
255*11f1e5f6Sskrll
256*11f1e5f6Sskrll pin = kmem_alloc(sizeof(*pin), KM_SLEEP);
257*11f1e5f6Sskrll pin->pin_no = pinno;
258*11f1e5f6Sskrll pin->pin_flags = flags;
259*11f1e5f6Sskrll pin->pin_actlo = actlo;
260*11f1e5f6Sskrll
261*11f1e5f6Sskrll return pin;
262*11f1e5f6Sskrll }
263*11f1e5f6Sskrll
264*11f1e5f6Sskrll
265*11f1e5f6Sskrll static void
apple_smc_gpio_release(device_t dev,void * priv)266*11f1e5f6Sskrll apple_smc_gpio_release(device_t dev, void *priv)
267*11f1e5f6Sskrll {
268*11f1e5f6Sskrll struct apple_smc_gpio_pin *pin = priv;
269*11f1e5f6Sskrll
270*11f1e5f6Sskrll kmem_free(pin, sizeof(*pin));
271*11f1e5f6Sskrll }
272*11f1e5f6Sskrll
273*11f1e5f6Sskrll static void
apple_smc_gpio_write(device_t dev,void * priv,int val,bool raw)274*11f1e5f6Sskrll apple_smc_gpio_write(device_t dev, void *priv, int val, bool raw)
275*11f1e5f6Sskrll {
276*11f1e5f6Sskrll struct apple_smc_softc * const sc = device_private(dev);
277*11f1e5f6Sskrll struct apple_smc_gpio_pin *pin = priv;
278*11f1e5f6Sskrll const u_int pn = pin->pin_no;
279*11f1e5f6Sskrll static const char *digits = "0123456789abcdef";
280*11f1e5f6Sskrll uint32_t key = SMC_KEY("gP\0\0");
281*11f1e5f6Sskrll uint32_t data;
282*11f1e5f6Sskrll
283*11f1e5f6Sskrll key |= __SHIFTIN(digits[__SHIFTOUT(pn, __BITS(3, 0))], __BITS(7, 0));
284*11f1e5f6Sskrll key |= __SHIFTIN(digits[__SHIFTOUT(pn, __BITS(7, 4))], __BITS(15, 8));
285*11f1e5f6Sskrll
286*11f1e5f6Sskrll if (pin->pin_actlo)
287*11f1e5f6Sskrll val = !val;
288*11f1e5f6Sskrll data = SMC_GPIO_CMD_OUTPUT | !!val;
289*11f1e5f6Sskrll
290*11f1e5f6Sskrll apple_smc_write_key(sc, key, &data, sizeof(data));
291*11f1e5f6Sskrll }
292*11f1e5f6Sskrll
293*11f1e5f6Sskrll static int
apple_smc_match(device_t parent,cfdata_t cf,void * aux)294*11f1e5f6Sskrll apple_smc_match(device_t parent, cfdata_t cf, void *aux)
295*11f1e5f6Sskrll {
296*11f1e5f6Sskrll struct fdt_attach_args * const faa = aux;
297*11f1e5f6Sskrll
298*11f1e5f6Sskrll return of_compatible_match(faa->faa_phandle, compat_data);
299*11f1e5f6Sskrll }
300*11f1e5f6Sskrll
301*11f1e5f6Sskrll
302*11f1e5f6Sskrll static struct fdtbus_gpio_controller_func apple_smc_gpio_funcs = {
303*11f1e5f6Sskrll .acquire = apple_smc_gpio_acquire,
304*11f1e5f6Sskrll .release = apple_smc_gpio_release,
305*11f1e5f6Sskrll // .read = apple_smc_gpio_read,
306*11f1e5f6Sskrll .write = apple_smc_gpio_write
307*11f1e5f6Sskrll };
308*11f1e5f6Sskrll
309*11f1e5f6Sskrll
310*11f1e5f6Sskrll static void
apple_smc_attach(device_t parent,device_t self,void * aux)311*11f1e5f6Sskrll apple_smc_attach(device_t parent, device_t self, void *aux)
312*11f1e5f6Sskrll {
313*11f1e5f6Sskrll struct apple_smc_softc * const sc = device_private(self);
314*11f1e5f6Sskrll struct fdt_attach_args * const faa = aux;
315*11f1e5f6Sskrll const int phandle = faa->faa_phandle;
316*11f1e5f6Sskrll bus_addr_t addr;
317*11f1e5f6Sskrll bus_size_t size;
318*11f1e5f6Sskrll int error;
319*11f1e5f6Sskrll
320*11f1e5f6Sskrll if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
321*11f1e5f6Sskrll aprint_error(": couldn't get registers\n");
322*11f1e5f6Sskrll return;
323*11f1e5f6Sskrll }
324*11f1e5f6Sskrll
325*11f1e5f6Sskrll sc->sc_dev = self;
326*11f1e5f6Sskrll sc->sc_bst = faa->faa_bst;
327*11f1e5f6Sskrll if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
328*11f1e5f6Sskrll aprint_error(": couldn't map registers\n");
329*11f1e5f6Sskrll return;
330*11f1e5f6Sskrll }
331*11f1e5f6Sskrll
332*11f1e5f6Sskrll mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
333*11f1e5f6Sskrll cv_init(&sc->sc_cv, "applesmc");
334*11f1e5f6Sskrll
335*11f1e5f6Sskrll sc->sc_rs = rtkit_init(phandle, NULL);
336*11f1e5f6Sskrll if (sc->sc_rs == NULL) {
337*11f1e5f6Sskrll aprint_error(": can't map mailbox channel\n");
338*11f1e5f6Sskrll return;
339*11f1e5f6Sskrll }
340*11f1e5f6Sskrll
341*11f1e5f6Sskrll error = rtkit_boot(sc->sc_rs);
342*11f1e5f6Sskrll if (error) {
343*11f1e5f6Sskrll aprint_error(": can't boot firmware\n");
344*11f1e5f6Sskrll return;
345*11f1e5f6Sskrll }
346*11f1e5f6Sskrll
347*11f1e5f6Sskrll error = rtkit_start_endpoint(sc->sc_rs, SMC_EP, apple_smc_callback, sc);
348*11f1e5f6Sskrll if (error) {
349*11f1e5f6Sskrll aprint_error(": can't start SMC endpoint\n");
350*11f1e5f6Sskrll return;
351*11f1e5f6Sskrll }
352*11f1e5f6Sskrll
353*11f1e5f6Sskrll apple_smc_send_cmd(sc, SMC_GET_SRAM_ADDR, 0, 0);
354*11f1e5f6Sskrll error = apple_smc_wait_cmd(sc);
355*11f1e5f6Sskrll if (error) {
356*11f1e5f6Sskrll aprint_error(": can't get SRAM address\n");
357*11f1e5f6Sskrll return;
358*11f1e5f6Sskrll }
359*11f1e5f6Sskrll
360*11f1e5f6Sskrll if (bus_space_map(sc->sc_bst, sc->sc_data, SMC_SRAM_SIZE, 0,
361*11f1e5f6Sskrll &sc->sc_sram_bsh)) {
362*11f1e5f6Sskrll aprint_error(": can't map SRAM\n");
363*11f1e5f6Sskrll return;
364*11f1e5f6Sskrll }
365*11f1e5f6Sskrll
366*11f1e5f6Sskrll aprint_naive("\n");
367*11f1e5f6Sskrll aprint_normal(": Apple SMC\n");
368*11f1e5f6Sskrll
369*11f1e5f6Sskrll apple_smc_sc = sc;
370*11f1e5f6Sskrll
371*11f1e5f6Sskrll const int gpio = of_find_firstchild_byname(phandle, "gpio");
372*11f1e5f6Sskrll if (gpio > 0) {
373*11f1e5f6Sskrll fdtbus_register_gpio_controller(self, gpio,
374*11f1e5f6Sskrll &apple_smc_gpio_funcs);
375*11f1e5f6Sskrll }
376*11f1e5f6Sskrll }
377*11f1e5f6Sskrll
378*11f1e5f6Sskrll
379*11f1e5f6Sskrll CFATTACH_DECL_NEW(apple_rtkitsmc, sizeof(struct apple_smc_softc),
380*11f1e5f6Sskrll apple_smc_match, apple_smc_attach, NULL, NULL);
381