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