1/* $NetBSD: fdt_powerdomain.c,v 1.2 2024/06/12 06:39:28 rin 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: fdt_powerdomain.c,v 1.2 2024/06/12 06:39:28 rin 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
45struct fdtbus_powerdomain_controller {
46	device_t pdc_dev;
47	int pdc_phandle;
48
49	void *pdc_cookie;
50
51	int pdc_cells;
52
53	const struct fdtbus_powerdomain_controller_func *pdc_funcs;
54
55	LIST_ENTRY(fdtbus_powerdomain_controller) pdc_next;
56};
57
58static LIST_HEAD(, fdtbus_powerdomain_controller) fdtbus_powerdomain_controllers =
59    LIST_HEAD_INITIALIZER(fdtbus_powerdomain_controllers);
60
61int
62fdtbus_register_powerdomain_controller(device_t dev, int phandle,
63    const struct fdtbus_powerdomain_controller_func *funcs)
64{
65	struct fdtbus_powerdomain_controller *pdc;
66
67	uint32_t cells;
68	if (of_getprop_uint32(phandle, "#power-domain-cells", &cells) != 0) {
69		aprint_debug_dev(dev, "missing #power-domain-cells");
70		return EINVAL;
71	}
72
73	pdc = kmem_alloc(sizeof(*pdc), KM_SLEEP);
74	pdc->pdc_dev = dev;
75	pdc->pdc_phandle = phandle;
76	pdc->pdc_funcs = funcs;
77	pdc->pdc_cells = cells;
78
79	LIST_INSERT_HEAD(&fdtbus_powerdomain_controllers, pdc, pdc_next);
80
81	return 0;
82}
83
84static struct fdtbus_powerdomain_controller *
85fdtbus_powerdomain_lookup(int phandle)
86{
87	struct fdtbus_powerdomain_controller *pdc;
88
89	LIST_FOREACH(pdc, &fdtbus_powerdomain_controllers, pdc_next) {
90		if (pdc->pdc_phandle == phandle)
91			return pdc;
92	}
93
94	return NULL;
95}
96
97static int
98fdtbus_powerdomain_enable_internal(int phandle, int index, bool enable)
99{
100	int len;
101	const uint32_t *pds = fdtbus_get_prop(phandle, "power-domains", &len);
102
103	if (pds == NULL)
104		return EINVAL;
105
106	for (const uint32_t *pd = pds; pd < pds + len / sizeof(*pd); index--) {
107		uint32_t pd_node =
108		   fdtbus_get_phandle_from_native(be32toh(pd[0]));
109		struct fdtbus_powerdomain_controller *pdc =
110		    fdtbus_powerdomain_lookup(pd_node);
111
112		if (pdc == NULL)
113			return ENXIO;
114
115		if (index < 0 || index == 0)
116			pdc->pdc_funcs->pdc_enable(pdc->pdc_dev, pd, enable);
117		if (index == 0)
118			break;
119
120		pd += pdc->pdc_cells + 1;
121	}
122
123	return 0;
124}
125
126int
127fdtbus_powerdomain_enable_index(int phandle, int index)
128{
129	return fdtbus_powerdomain_enable_internal(phandle, index, true);
130}
131
132int
133fdtbus_powerdomain_disable_index(int phandle, int index)
134{
135	return fdtbus_powerdomain_enable_internal(phandle, index, false);
136}
137
138int
139fdtbus_powerdomain_enable(int node)
140{
141	return fdtbus_powerdomain_enable_index(node, -1);
142}
143
144int
145fdtbus_powerdomain_disable(int node)
146{
147	return fdtbus_powerdomain_disable_index(node, -1);
148}
149