blob: 516e690ac2d5496a98a2731fef70c5369af008e8 [file] [log] [blame]
Etienne Carriere02fd1262020-09-09 18:44:00 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2020 Linaro Limited.
4 */
5
6#include <common.h>
7#include <dm.h>
Sean Anderson31786fe2020-10-04 21:39:44 -04008#include <dm/device_compat.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +02009#include <errno.h>
10#include <scmi_agent-uclass.h>
11#include <scmi_protocols.h>
12
13#include <dm/device-internal.h>
14#include <linux/compat.h>
15
16/**
17 * struct error_code - Helper structure for SCMI error code conversion
18 * @scmi: SCMI error code
19 * @errno: Related standard error number
20 */
21struct error_code {
22 int scmi;
23 int errno;
24};
25
26static const struct error_code scmi_linux_errmap[] = {
27 { .scmi = SCMI_NOT_SUPPORTED, .errno = -EOPNOTSUPP, },
28 { .scmi = SCMI_INVALID_PARAMETERS, .errno = -EINVAL, },
29 { .scmi = SCMI_DENIED, .errno = -EACCES, },
30 { .scmi = SCMI_NOT_FOUND, .errno = -ENOENT, },
31 { .scmi = SCMI_OUT_OF_RANGE, .errno = -ERANGE, },
32 { .scmi = SCMI_BUSY, .errno = -EBUSY, },
33 { .scmi = SCMI_COMMS_ERROR, .errno = -ECOMM, },
34 { .scmi = SCMI_GENERIC_ERROR, .errno = -EIO, },
35 { .scmi = SCMI_HARDWARE_ERROR, .errno = -EREMOTEIO, },
36 { .scmi = SCMI_PROTOCOL_ERROR, .errno = -EPROTO, },
37};
38
39int scmi_to_linux_errno(s32 scmi_code)
40{
41 int n;
42
43 if (!scmi_code)
44 return 0;
45
46 for (n = 0; n < ARRAY_SIZE(scmi_linux_errmap); n++)
47 if (scmi_code == scmi_linux_errmap[n].scmi)
48 return scmi_linux_errmap[1].errno;
49
50 return -EPROTO;
51}
52
53/*
54 * SCMI agent devices binds devices of various uclasses depeding on
55 * the FDT description. scmi_bind_protocol() is a generic bind sequence
56 * called by the uclass at bind stage, that is uclass post_bind.
57 */
58static int scmi_bind_protocols(struct udevice *dev)
59{
60 int ret = 0;
61 ofnode node;
Etienne Carriere02fd1262020-09-09 18:44:00 +020062
63 dev_for_each_subnode(node, dev) {
Etienne Carriere78928e12020-09-09 18:44:04 +020064 struct driver *drv = NULL;
Etienne Carriere02fd1262020-09-09 18:44:00 +020065 u32 protocol_id;
66
67 if (!ofnode_is_available(node))
68 continue;
69
70 if (ofnode_read_u32(node, "reg", &protocol_id))
71 continue;
72
73 switch (protocol_id) {
Etienne Carriere78928e12020-09-09 18:44:04 +020074 case SCMI_PROTOCOL_ID_CLOCK:
75 if (IS_ENABLED(CONFIG_CLK_SCMI))
Simon Glass65130cd2020-12-28 20:34:56 -070076 drv = DM_DRIVER_GET(scmi_clock);
Etienne Carriere78928e12020-09-09 18:44:04 +020077 break;
Etienne Carrierec6e9af32020-09-09 18:44:06 +020078 case SCMI_PROTOCOL_ID_RESET_DOMAIN:
79 if (IS_ENABLED(CONFIG_RESET_SCMI))
Simon Glass65130cd2020-12-28 20:34:56 -070080 drv = DM_DRIVER_GET(scmi_reset_domain);
Etienne Carrierec6e9af32020-09-09 18:44:06 +020081 break;
Etienne Carriere02fd1262020-09-09 18:44:00 +020082 default:
Etienne Carriere78928e12020-09-09 18:44:04 +020083 break;
84 }
85
86 if (!drv) {
87 dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n",
88 protocol_id);
Etienne Carriere02fd1262020-09-09 18:44:00 +020089 continue;
90 }
91
Simon Glass884870f2020-11-28 17:50:01 -070092 ret = device_bind(dev, drv, ofnode_get_name(node), NULL, node,
93 NULL);
Etienne Carriere02fd1262020-09-09 18:44:00 +020094 if (ret)
95 break;
96 }
97
98 return ret;
99}
100
101static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev)
102{
103 return (const struct scmi_agent_ops *)dev->driver->ops;
104}
105
106int devm_scmi_process_msg(struct udevice *dev, struct scmi_msg *msg)
107{
108 const struct scmi_agent_ops *ops = transport_dev_ops(dev);
109
110 if (ops->process_msg)
111 return ops->process_msg(dev, msg);
112
113 return -EPROTONOSUPPORT;
114}
115
116UCLASS_DRIVER(scmi_agent) = {
117 .id = UCLASS_SCMI_AGENT,
118 .name = "scmi_agent",
119 .post_bind = scmi_bind_protocols,
120};