1121985Sjhb// SPDX-License-Identifier: GPL-2.0+
2121985Sjhb/*
3121985Sjhb * Copyright 2022-2023 Arm Limited and/or its affiliates <open-source-office@arm.com>
4121985Sjhb *
5121985Sjhb * Authors:
6121985Sjhb *   Abdellatif El Khlifi <abdellatif.elkhlifi@arm.com>
7121985Sjhb */
8121985Sjhb#include <common.h>
9121985Sjhb#include <arm_ffa.h>
10121985Sjhb#include <dm.h>
11121985Sjhb#include <log.h>
12121985Sjhb#include <asm/global_data.h>
13121985Sjhb#include <asm/sandbox_arm_ffa_priv.h>
14121985Sjhb#include <dm/device-internal.h>
15121985Sjhb#include <linux/errno.h>
16121985Sjhb
17121985SjhbDECLARE_GLOBAL_DATA_PTR;
18121985Sjhb
19121985Sjhb/**
20121985Sjhb * sandbox_ffa_discover() - perform sandbox FF-A discovery
21121985Sjhb * @dev: The sandbox FF-A bus device
22121985Sjhb * Try to discover the FF-A framework. Discovery is performed by
23121985Sjhb * querying the FF-A framework version from secure world using the FFA_VERSION ABI.
24121985Sjhb * Return:
25121985Sjhb *
26121985Sjhb * 0 on success. Otherwise, failure
27121985Sjhb */
28121985Sjhbstatic int sandbox_ffa_discover(struct udevice *dev)
29121985Sjhb{
30121985Sjhb	int ret;
31121985Sjhb	struct udevice *emul;
32121985Sjhb
33121985Sjhb	log_debug("Emulated FF-A framework discovery\n");
34121985Sjhb
35121985Sjhb	ret = ffa_emul_find(dev, &emul);
36121985Sjhb	if (ret) {
37121985Sjhb		log_err("Cannot find FF-A emulator\n");
38121985Sjhb		return ret;
39121985Sjhb	}
40121985Sjhb
41121985Sjhb	ret = ffa_get_version_hdlr(dev);
42121985Sjhb	if (ret)
43121985Sjhb		return ret;
44121985Sjhb
45121985Sjhb	return 0;
46121985Sjhb}
47121985Sjhb
48121985Sjhb/**
49121985Sjhb * sandbox_ffa_probe() - The sandbox FF-A driver probe function
50121985Sjhb * @dev:	the sandbox-arm-ffa device
51121985Sjhb * Save the emulator device in uc_priv.
52121985Sjhb * Return:
53121985Sjhb *
54121985Sjhb * 0 on success.
55124188Sjhb */
56121985Sjhbstatic int sandbox_ffa_probe(struct udevice *dev)
57122051Snyan{
58122051Snyan	int ret;
59122051Snyan	struct ffa_priv *uc_priv = dev_get_uclass_priv(dev);
60121985Sjhb
61122051Snyan	ret = uclass_first_device_err(UCLASS_FFA_EMUL, &uc_priv->emul);
62121985Sjhb	if (ret) {
63121985Sjhb		log_err("Cannot find FF-A emulator\n");
64121985Sjhb		return ret;
65121985Sjhb	}
66121985Sjhb
67124188Sjhb	return 0;
68124188Sjhb}
69124188Sjhb
70124188Sjhb/**
71121985Sjhb * sandbox_ffa_bind() - The sandbox FF-A driver bind function
72124188Sjhb * @dev:	the sandbox-arm-ffa device
73124188Sjhb * Try to discover the emulated FF-A bus.
74124188Sjhb * Return:
75124188Sjhb *
76124188Sjhb * 0 on success.
77124188Sjhb */
78124188Sjhbstatic int sandbox_ffa_bind(struct udevice *dev)
79121985Sjhb{
80124188Sjhb	int ret;
81124188Sjhb
82121985Sjhb	ret = sandbox_ffa_discover(dev);
83124188Sjhb	if (ret)
84124188Sjhb		return ret;
85121985Sjhb
86124188Sjhb	return 0;
87121985Sjhb}
88124188Sjhb
89121985Sjhb/* Sandbox Arm FF-A emulator operations */
90121985Sjhb
91124188Sjhbstatic const struct ffa_bus_ops sandbox_ffa_ops = {
92121985Sjhb	.partition_info_get = ffa_get_partitions_info_hdlr,
93124188Sjhb	.sync_send_receive = ffa_msg_send_direct_req_hdlr,
94121985Sjhb	.rxtx_unmap = ffa_unmap_rxtx_buffers_hdlr,
95121985Sjhb};
96128875Sjhb
97128875Sjhbstatic const struct udevice_id sandbox_ffa_id[] = {
98128875Sjhb	{ "sandbox,arm-ffa", 0 },
99128875Sjhb	{ },
100121985Sjhb};
101121985Sjhb
102121985Sjhb/* Declaring the sandbox FF-A driver under UCLASS_FFA */
103129009SnyanU_BOOT_DRIVER(sandbox_arm_ffa) = {
104128929Sjhb	.name		= "sandbox_arm_ffa",
105129009Snyan	.of_match = sandbox_ffa_id,
106121985Sjhb	.id		= UCLASS_FFA,
107121985Sjhb	.bind		= sandbox_ffa_bind,
108121985Sjhb	.probe		= sandbox_ffa_probe,
109121985Sjhb	.ops		= &sandbox_ffa_ops,
110121985Sjhb};
111121985Sjhb