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