xref: /netbsd-src/sys/arch/arm/fdt/psci_fdt.c (revision 81be3b13bd13e0892c59e7cb1db4091a29bc9fb5)
1*81be3b13Sjmcneill /* $NetBSD: psci_fdt.c,v 1.21 2021/08/06 19:38:53 jmcneill Exp $ */
216ab2903Sjmcneill 
316ab2903Sjmcneill /*-
416ab2903Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
516ab2903Sjmcneill  * All rights reserved.
616ab2903Sjmcneill  *
716ab2903Sjmcneill  * Redistribution and use in source and binary forms, with or without
816ab2903Sjmcneill  * modification, are permitted provided that the following conditions
916ab2903Sjmcneill  * are met:
1016ab2903Sjmcneill  * 1. Redistributions of source code must retain the above copyright
1116ab2903Sjmcneill  *    notice, this list of conditions and the following disclaimer.
1216ab2903Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
1316ab2903Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
1416ab2903Sjmcneill  *    documentation and/or other materials provided with the distribution.
1516ab2903Sjmcneill  *
1616ab2903Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1716ab2903Sjmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1816ab2903Sjmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1916ab2903Sjmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2016ab2903Sjmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2116ab2903Sjmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2216ab2903Sjmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2316ab2903Sjmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2416ab2903Sjmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2516ab2903Sjmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2616ab2903Sjmcneill  * SUCH DAMAGE.
2716ab2903Sjmcneill  */
2816ab2903Sjmcneill 
29aec8c8c9Sjmcneill #include "opt_multiprocessor.h"
30aec8c8c9Sjmcneill 
3116ab2903Sjmcneill #include <sys/cdefs.h>
32*81be3b13Sjmcneill __KERNEL_RCSID(0, "$NetBSD: psci_fdt.c,v 1.21 2021/08/06 19:38:53 jmcneill Exp $");
3316ab2903Sjmcneill 
3416ab2903Sjmcneill #include <sys/param.h>
35a063a49aSskrll #include <sys/atomic.h>
3616ab2903Sjmcneill #include <sys/bus.h>
3716ab2903Sjmcneill #include <sys/device.h>
3816ab2903Sjmcneill #include <sys/kernel.h>
39a063a49aSskrll #include <sys/systm.h>
4016ab2903Sjmcneill 
4116ab2903Sjmcneill #include <dev/fdt/fdtvar.h>
4216ab2903Sjmcneill 
43*81be3b13Sjmcneill #include <arm/arm/smccc.h>
4416ab2903Sjmcneill #include <arm/arm/psci.h>
4566d31a2dSryo #include <arm/fdt/psci_fdtvar.h>
4616ab2903Sjmcneill 
4716ab2903Sjmcneill static int	psci_fdt_match(device_t, cfdata_t, void *);
4816ab2903Sjmcneill static void	psci_fdt_attach(device_t, device_t, void *);
4916ab2903Sjmcneill 
5016ab2903Sjmcneill static int	psci_fdt_init(const int);
5116ab2903Sjmcneill 
526e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
536e54367aSthorpej 	{ .compat = "arm,psci" },
546e54367aSthorpej 	{ .compat = "arm,psci-0.2" },
556e54367aSthorpej 	{ .compat = "arm,psci-1.0" },
566e54367aSthorpej 	DEVICE_COMPAT_EOL
5716ab2903Sjmcneill };
5816ab2903Sjmcneill 
5916ab2903Sjmcneill CFATTACH_DECL_NEW(psci_fdt, 0, psci_fdt_match, psci_fdt_attach, NULL, NULL);
6016ab2903Sjmcneill 
61b0802b6aSjmcneill static void
psci_fdt_power_reset(device_t dev)62f0c65f7aSjmcneill psci_fdt_power_reset(device_t dev)
63b0802b6aSjmcneill {
64b0802b6aSjmcneill 	delay(500000);
65b0802b6aSjmcneill 	psci_system_reset();
66b0802b6aSjmcneill }
67b0802b6aSjmcneill 
68b0802b6aSjmcneill static void
psci_fdt_power_poweroff(device_t dev)69f0c65f7aSjmcneill psci_fdt_power_poweroff(device_t dev)
70b0802b6aSjmcneill {
71b0802b6aSjmcneill 	delay(500000);
72b0802b6aSjmcneill 	psci_system_off();
73b0802b6aSjmcneill }
74b0802b6aSjmcneill 
75b0802b6aSjmcneill static const struct fdtbus_power_controller_func psci_power_funcs = {
76f0c65f7aSjmcneill 	.reset = psci_fdt_power_reset,
77f0c65f7aSjmcneill 	.poweroff = psci_fdt_power_poweroff,
78b0802b6aSjmcneill };
79b0802b6aSjmcneill 
8016ab2903Sjmcneill static int
psci_fdt_match(device_t parent,cfdata_t cf,void * aux)8116ab2903Sjmcneill psci_fdt_match(device_t parent, cfdata_t cf, void *aux)
8216ab2903Sjmcneill {
8316ab2903Sjmcneill 	struct fdt_attach_args * const faa = aux;
8416ab2903Sjmcneill 
856e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
8616ab2903Sjmcneill }
8716ab2903Sjmcneill 
8816ab2903Sjmcneill static void
psci_fdt_attach(device_t parent,device_t self,void * aux)8916ab2903Sjmcneill psci_fdt_attach(device_t parent, device_t self, void *aux)
9016ab2903Sjmcneill {
9116ab2903Sjmcneill 	struct fdt_attach_args * const faa = aux;
9216ab2903Sjmcneill 	const int phandle = faa->faa_phandle;
9316ab2903Sjmcneill 
9416ab2903Sjmcneill 	psci_fdt_init(phandle);
9516ab2903Sjmcneill 
9616ab2903Sjmcneill 	const uint32_t ver = psci_version();
9716ab2903Sjmcneill 	const u_int ver_maj = __SHIFTOUT(ver, PSCI_VERSION_MAJOR);
9816ab2903Sjmcneill 	const u_int ver_min = __SHIFTOUT(ver, PSCI_VERSION_MINOR);
9916ab2903Sjmcneill 
10016ab2903Sjmcneill 	aprint_naive("\n");
10116ab2903Sjmcneill 	aprint_normal(": PSCI %u.%u\n", ver_maj, ver_min);
102b0802b6aSjmcneill 
103b0802b6aSjmcneill 	fdtbus_register_power_controller(self, phandle,
104b0802b6aSjmcneill 	    &psci_power_funcs);
10516ab2903Sjmcneill }
10616ab2903Sjmcneill 
10716ab2903Sjmcneill static int
psci_fdt_init(const int phandle)10816ab2903Sjmcneill psci_fdt_init(const int phandle)
10916ab2903Sjmcneill {
110b6b3ceffSjmcneill 	const char *method, *psciver;
11116ab2903Sjmcneill 	uint32_t val;
11216ab2903Sjmcneill 
113b6b3ceffSjmcneill 	method = fdtbus_get_string(phandle, "method");
114b6b3ceffSjmcneill 	psciver = fdtbus_get_string(phandle, "compatible");
115b6b3ceffSjmcneill 	if (method == NULL || psciver == NULL) {
116b6b3ceffSjmcneill 		aprint_error("PSCI: missing required property on /psci\n");
11716ab2903Sjmcneill 		return EINVAL;
11816ab2903Sjmcneill 	}
11916ab2903Sjmcneill 
12016ab2903Sjmcneill 	if (strcmp(method, "smc") == 0)
12116ab2903Sjmcneill 		psci_init(psci_call_smc);
12216ab2903Sjmcneill 	else if (strcmp(method, "hvc") == 0)
12316ab2903Sjmcneill 		psci_init(psci_call_hvc);
12416ab2903Sjmcneill 	else {
12516ab2903Sjmcneill 		aprint_error("PSCI: unsupported method '%s'\n", method);
12616ab2903Sjmcneill 		return EINVAL;
12716ab2903Sjmcneill 	}
12816ab2903Sjmcneill 
129b6b3ceffSjmcneill 	/*
130b6b3ceffSjmcneill 	 * If the first compatible string is "arm,psci" then we
131b6b3ceffSjmcneill 	 * are dealing with PSCI 0.1
132b6b3ceffSjmcneill 	 */
133b6b3ceffSjmcneill 	if (strcmp(psciver, "arm,psci") == 0) {
13416ab2903Sjmcneill 		psci_clearfunc();
13516ab2903Sjmcneill 		if (of_getprop_uint32(phandle, "cpu_on", &val) == 0)
13616ab2903Sjmcneill 			psci_setfunc(PSCI_FUNC_CPU_ON, val);
13716ab2903Sjmcneill 	}
13816ab2903Sjmcneill 
139*81be3b13Sjmcneill 	smccc_probe();
140*81be3b13Sjmcneill 
14116ab2903Sjmcneill 	return 0;
14216ab2903Sjmcneill }
14316ab2903Sjmcneill 
14466d31a2dSryo int
psci_fdt_preinit(void)145f0c65f7aSjmcneill psci_fdt_preinit(void)
146f0c65f7aSjmcneill {
147f0c65f7aSjmcneill 	const int phandle = OF_finddevice("/psci");
148f0c65f7aSjmcneill 	if (phandle == -1) {
149f0c65f7aSjmcneill 		aprint_error("PSCI: no /psci node found\n");
150f0c65f7aSjmcneill 		return ENODEV;
151f0c65f7aSjmcneill 	}
152f0c65f7aSjmcneill 
153f0c65f7aSjmcneill 	return psci_fdt_init(phandle);
154f0c65f7aSjmcneill }
155f0c65f7aSjmcneill 
156f0c65f7aSjmcneill void
psci_fdt_reset(void)157f0c65f7aSjmcneill psci_fdt_reset(void)
158f0c65f7aSjmcneill {
159f0c65f7aSjmcneill 	if (psci_fdt_preinit() != 0) {
160f0c65f7aSjmcneill 		aprint_error("PSCI: reset failed\n");
161f0c65f7aSjmcneill 		return;
162f0c65f7aSjmcneill 	}
163f0c65f7aSjmcneill 
164f0c65f7aSjmcneill 	psci_system_reset();
165f0c65f7aSjmcneill }
166