1 /* $NetBSD: apple_pmgr.c,v 1.2 2022/05/10 08:10:28 skrll Exp $ */
2
3 /*-
4 * Copyright (c) 2022 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Nick Hudson
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: apple_pmgr.c,v 1.2 2022/05/10 08:10:28 skrll Exp $");
34
35 #include <sys/param.h>
36
37 #include <sys/bus.h>
38 #include <sys/kmem.h>
39 #include <sys/queue.h>
40
41 #include <libfdt.h>
42 #include <dev/fdt/fdtvar.h>
43
44 /*
45 * Power manager registers
46 */
47 #define PMGR_PS_TARGET_MASK __BITS(3, 0)
48 #define PMGR_PS_ACTUAL_MASK __BITS(7, 4)
49 #define PMGR_PS_ACTIVE 0xf
50 #define PMGR_PS_PWRGATE 0x0
51
52 struct apple_pmgr_softc {
53 device_t sc_dev;
54 bus_space_tag_t sc_bst;
55 bus_space_handle_t sc_bsh;
56 };
57
58 #define PMGR_READ(sc, reg) \
59 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
60 #define PMGR_WRITE(sc, reg, val) \
61 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
62
63
64 static const struct device_compatible_entry compat_data[] = {
65 { .compat = "apple,pmgr" },
66 DEVICE_COMPAT_EOL
67 };
68
69 static void
apple_pmgr_enable(device_t dev,const uint32_t * data,bool enable)70 apple_pmgr_enable(device_t dev, const uint32_t *data, bool enable)
71 {
72 struct apple_pmgr_softc * const sc = device_private(dev);
73 const uint32_t pstate = enable ? PMGR_PS_ACTIVE : PMGR_PS_PWRGATE;
74 const int phandle = fdtbus_get_phandle_from_native(be32dec(data));
75
76 fdtbus_powerdomain_enable(phandle);
77
78 bus_size_t off;
79 bus_size_t size;
80 if (fdtbus_get_reg(phandle, 0, &off, &size) != 0 || size != 4) {
81 aprint_error(": invalid 'reg' property\n");
82 return;
83 }
84
85 uint32_t val = PMGR_READ(sc, off);
86
87 val &= ~PMGR_PS_TARGET_MASK;
88 val |= __SHIFTIN(pstate, PMGR_PS_TARGET_MASK);
89 PMGR_WRITE(sc, off, val);
90
91 for (int timo = 0; timo < 100; timo++) {
92 val = PMGR_READ(sc, off);
93 if (__SHIFTOUT(val, PMGR_PS_ACTUAL_MASK) == pstate)
94 break;
95 delay(1);
96 }
97 }
98
99
100 static int
apple_pmgr_match(device_t parent,cfdata_t cf,void * aux)101 apple_pmgr_match(device_t parent, cfdata_t cf, void *aux)
102 {
103 struct fdt_attach_args * const faa = aux;
104
105 return of_compatible_match(faa->faa_phandle, compat_data);
106 }
107
108
109 static void
apple_pmgr_attach(device_t parent,device_t self,void * aux)110 apple_pmgr_attach(device_t parent, device_t self, void *aux)
111 {
112 struct apple_pmgr_softc * const sc = device_private(self);
113 struct fdt_attach_args * const faa = aux;
114 const int phandle = faa->faa_phandle;
115
116 bus_addr_t pmgr_addr;
117 bus_size_t pmgr_size;
118
119 int error = fdtbus_get_reg(phandle, 0, &pmgr_addr, &pmgr_size);
120 if (error) {
121 aprint_error(": couldn't get registers\n");
122 return;
123 }
124
125 sc->sc_dev = self;
126 sc->sc_bst = faa->faa_bst;
127
128 error = bus_space_map(faa->faa_bst, pmgr_addr, pmgr_size, 0,
129 &sc->sc_bsh);
130 if (error) {
131 aprint_error(": couldn't map registers: %d\n", error);
132 return;
133 }
134
135 aprint_naive("\n");
136 aprint_normal(": Apple PMGR\n");
137
138 for (int node = OF_child(phandle); node; node = OF_peer(node)) {
139 static const struct device_compatible_entry compat_ps[] = {
140 { .compat = "apple,pmgr-pwrstate" },
141 DEVICE_COMPAT_EOL
142 };
143
144 if (!of_compatible_match(node, compat_ps))
145 continue;
146
147 static struct fdtbus_powerdomain_controller_func funcs = {
148 .pdc_enable = apple_pmgr_enable,
149 };
150
151 fdtbus_register_powerdomain_controller(self, node, &funcs);
152 }
153 }
154
155 CFATTACH_DECL_NEW(apple_pmgr, sizeof(struct apple_pmgr_softc),
156 apple_pmgr_match, apple_pmgr_attach, NULL, NULL);
157