1*abcecf9bSskrll /* $NetBSD: apple_pmgr.c,v 1.2 2022/05/10 08:10:28 skrll Exp $ */
24f480f1aSskrll
34f480f1aSskrll /*-
44f480f1aSskrll * Copyright (c) 2022 The NetBSD Foundation, Inc.
54f480f1aSskrll * All rights reserved.
64f480f1aSskrll *
74f480f1aSskrll * This code is derived from software contributed to The NetBSD Foundation
84f480f1aSskrll * by Nick Hudson
94f480f1aSskrll *
104f480f1aSskrll * Redistribution and use in source and binary forms, with or without
114f480f1aSskrll * modification, are permitted provided that the following conditions
124f480f1aSskrll * are met:
134f480f1aSskrll * 1. Redistributions of source code must retain the above copyright
144f480f1aSskrll * notice, this list of conditions and the following disclaimer.
154f480f1aSskrll * 2. Redistributions in binary form must reproduce the above copyright
164f480f1aSskrll * notice, this list of conditions and the following disclaimer in the
174f480f1aSskrll * documentation and/or other materials provided with the distribution.
184f480f1aSskrll *
194f480f1aSskrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
204f480f1aSskrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
214f480f1aSskrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
224f480f1aSskrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
234f480f1aSskrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
244f480f1aSskrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
254f480f1aSskrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
264f480f1aSskrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
274f480f1aSskrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
284f480f1aSskrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
294f480f1aSskrll * POSSIBILITY OF SUCH DAMAGE.
304f480f1aSskrll */
314f480f1aSskrll
324f480f1aSskrll #include <sys/cdefs.h>
33*abcecf9bSskrll __KERNEL_RCSID(0, "$NetBSD: apple_pmgr.c,v 1.2 2022/05/10 08:10:28 skrll Exp $");
344f480f1aSskrll
354f480f1aSskrll #include <sys/param.h>
364f480f1aSskrll
374f480f1aSskrll #include <sys/bus.h>
384f480f1aSskrll #include <sys/kmem.h>
394f480f1aSskrll #include <sys/queue.h>
404f480f1aSskrll
414f480f1aSskrll #include <libfdt.h>
424f480f1aSskrll #include <dev/fdt/fdtvar.h>
434f480f1aSskrll
444f480f1aSskrll /*
454f480f1aSskrll * Power manager registers
464f480f1aSskrll */
474f480f1aSskrll #define PMGR_PS_TARGET_MASK __BITS(3, 0)
484f480f1aSskrll #define PMGR_PS_ACTUAL_MASK __BITS(7, 4)
494f480f1aSskrll #define PMGR_PS_ACTIVE 0xf
504f480f1aSskrll #define PMGR_PS_PWRGATE 0x0
514f480f1aSskrll
524f480f1aSskrll struct apple_pmgr_softc {
534f480f1aSskrll device_t sc_dev;
544f480f1aSskrll bus_space_tag_t sc_bst;
554f480f1aSskrll bus_space_handle_t sc_bsh;
564f480f1aSskrll };
574f480f1aSskrll
584f480f1aSskrll #define PMGR_READ(sc, reg) \
594f480f1aSskrll bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
604f480f1aSskrll #define PMGR_WRITE(sc, reg, val) \
614f480f1aSskrll bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
624f480f1aSskrll
634f480f1aSskrll
644f480f1aSskrll static const struct device_compatible_entry compat_data[] = {
654f480f1aSskrll { .compat = "apple,pmgr" },
664f480f1aSskrll DEVICE_COMPAT_EOL
674f480f1aSskrll };
684f480f1aSskrll
694f480f1aSskrll static void
apple_pmgr_enable(device_t dev,const uint32_t * data,bool enable)704f480f1aSskrll apple_pmgr_enable(device_t dev, const uint32_t *data, bool enable)
714f480f1aSskrll {
724f480f1aSskrll struct apple_pmgr_softc * const sc = device_private(dev);
734f480f1aSskrll const uint32_t pstate = enable ? PMGR_PS_ACTIVE : PMGR_PS_PWRGATE;
744f480f1aSskrll const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
754f480f1aSskrll
764f480f1aSskrll fdtbus_powerdomain_enable(phandle);
774f480f1aSskrll
784f480f1aSskrll bus_size_t off;
794f480f1aSskrll bus_size_t size;
804f480f1aSskrll if (fdtbus_get_reg(phandle, 0, &off, &size) != 0 || size != 4) {
814f480f1aSskrll aprint_error(": invalid 'reg' property\n");
824f480f1aSskrll return;
834f480f1aSskrll }
844f480f1aSskrll
854f480f1aSskrll uint32_t val = PMGR_READ(sc, off);
864f480f1aSskrll
874f480f1aSskrll val &= ~PMGR_PS_TARGET_MASK;
884f480f1aSskrll val |= __SHIFTIN(pstate, PMGR_PS_TARGET_MASK);
894f480f1aSskrll PMGR_WRITE(sc, off, val);
904f480f1aSskrll
914f480f1aSskrll for (int timo = 0; timo < 100; timo++) {
924f480f1aSskrll val = PMGR_READ(sc, off);
934f480f1aSskrll if (__SHIFTOUT(val, PMGR_PS_ACTUAL_MASK) == pstate)
944f480f1aSskrll break;
954f480f1aSskrll delay(1);
964f480f1aSskrll }
974f480f1aSskrll }
984f480f1aSskrll
994f480f1aSskrll
1004f480f1aSskrll static int
apple_pmgr_match(device_t parent,cfdata_t cf,void * aux)1014f480f1aSskrll apple_pmgr_match(device_t parent, cfdata_t cf, void *aux)
1024f480f1aSskrll {
1034f480f1aSskrll struct fdt_attach_args * const faa = aux;
1044f480f1aSskrll
1054f480f1aSskrll return of_compatible_match(faa->faa_phandle, compat_data);
1064f480f1aSskrll }
1074f480f1aSskrll
1084f480f1aSskrll
1094f480f1aSskrll static void
apple_pmgr_attach(device_t parent,device_t self,void * aux)1104f480f1aSskrll apple_pmgr_attach(device_t parent, device_t self, void *aux)
1114f480f1aSskrll {
1124f480f1aSskrll struct apple_pmgr_softc * const sc = device_private(self);
1134f480f1aSskrll struct fdt_attach_args * const faa = aux;
1144f480f1aSskrll const int phandle = faa->faa_phandle;
1154f480f1aSskrll
1164f480f1aSskrll bus_addr_t pmgr_addr;
1174f480f1aSskrll bus_size_t pmgr_size;
1184f480f1aSskrll
1194f480f1aSskrll int error = fdtbus_get_reg(phandle, 0, &pmgr_addr, &pmgr_size);
1204f480f1aSskrll if (error) {
1214f480f1aSskrll aprint_error(": couldn't get registers\n");
1224f480f1aSskrll return;
1234f480f1aSskrll }
1244f480f1aSskrll
1254f480f1aSskrll sc->sc_dev = self;
1264f480f1aSskrll sc->sc_bst = faa->faa_bst;
1274f480f1aSskrll
1284f480f1aSskrll error = bus_space_map(faa->faa_bst, pmgr_addr, pmgr_size, 0,
1294f480f1aSskrll &sc->sc_bsh);
1304f480f1aSskrll if (error) {
1314f480f1aSskrll aprint_error(": couldn't map registers: %d\n", error);
1324f480f1aSskrll return;
1334f480f1aSskrll }
1344f480f1aSskrll
135*abcecf9bSskrll aprint_naive("\n");
136*abcecf9bSskrll aprint_normal(": Apple PMGR\n");
137*abcecf9bSskrll
1384f480f1aSskrll for (int node = OF_child(phandle); node; node = OF_peer(node)) {
1394f480f1aSskrll static const struct device_compatible_entry compat_ps[] = {
1404f480f1aSskrll { .compat = "apple,pmgr-pwrstate" },
1414f480f1aSskrll DEVICE_COMPAT_EOL
1424f480f1aSskrll };
1434f480f1aSskrll
1444f480f1aSskrll if (!of_compatible_match(node, compat_ps))
1454f480f1aSskrll continue;
1464f480f1aSskrll
1474f480f1aSskrll static struct fdtbus_powerdomain_controller_func funcs = {
1484f480f1aSskrll .pdc_enable = apple_pmgr_enable,
1494f480f1aSskrll };
1504f480f1aSskrll
1514f480f1aSskrll fdtbus_register_powerdomain_controller(self, node, &funcs);
1524f480f1aSskrll }
1534f480f1aSskrll }
1544f480f1aSskrll
1554f480f1aSskrll CFATTACH_DECL_NEW(apple_pmgr, sizeof(struct apple_pmgr_softc),
1564f480f1aSskrll apple_pmgr_match, apple_pmgr_attach, NULL, NULL);
157