blob: 3819f2fa993e57b5841be2f7d2595f054e7dc977 [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
Patrick Delaunay5dd84d42021-02-24 11:19:44 +01006#define LOG_CATEGORY UCLASS_SCMI_AGENT
7
Etienne Carriere02fd1262020-09-09 18:44:00 +02008#include <common.h>
9#include <dm.h>
10#include <errno.h>
11#include <scmi_agent-uclass.h>
12#include <scmi_protocols.h>
Patrick Delaunayd4c9f682021-02-24 11:19:45 +010013#include <dm/device_compat.h>
Etienne Carriere02fd1262020-09-09 18:44:00 +020014#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 */
22struct error_code {
23 int scmi;
24 int errno;
25};
26
27static 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
40int 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 */
59static int scmi_bind_protocols(struct udevice *dev)
60{
61 int ret = 0;
62 ofnode node;
Etienne Carriere02fd1262020-09-09 18:44:00 +020063
64 dev_for_each_subnode(node, dev) {
Etienne Carriere78928e12020-09-09 18:44:04 +020065 struct driver *drv = NULL;
Etienne Carriere02fd1262020-09-09 18:44:00 +020066 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) {
Etienne Carriere78928e12020-09-09 18:44:04 +020075 case SCMI_PROTOCOL_ID_CLOCK:
76 if (IS_ENABLED(CONFIG_CLK_SCMI))
Simon Glass65130cd2020-12-28 20:34:56 -070077 drv = DM_DRIVER_GET(scmi_clock);
Etienne Carriere78928e12020-09-09 18:44:04 +020078 break;
Etienne Carrierec6e9af32020-09-09 18:44:06 +020079 case SCMI_PROTOCOL_ID_RESET_DOMAIN:
80 if (IS_ENABLED(CONFIG_RESET_SCMI))
Simon Glass65130cd2020-12-28 20:34:56 -070081 drv = DM_DRIVER_GET(scmi_reset_domain);
Etienne Carrierec6e9af32020-09-09 18:44:06 +020082 break;
Etienne Carriered68663a2021-03-08 22:38:06 +010083 case SCMI_PROTOCOL_ID_VOLTAGE_DOMAIN:
84 if (IS_ENABLED(CONFIG_DM_REGULATOR_SCMI)) {
85 node = ofnode_find_subnode(node, "regulators");
86 if (!ofnode_valid(node)) {
87 dev_err(dev, "no regulators node\n");
88 return -ENXIO;
89 }
90 drv = DM_DRIVER_GET(scmi_voltage_domain);
91 }
92 break;
Etienne Carriere02fd1262020-09-09 18:44:00 +020093 default:
Etienne Carriere78928e12020-09-09 18:44:04 +020094 break;
95 }
96
97 if (!drv) {
98 dev_dbg(dev, "Ignore unsupported SCMI protocol %#x\n",
99 protocol_id);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200100 continue;
101 }
102
Simon Glass884870f2020-11-28 17:50:01 -0700103 ret = device_bind(dev, drv, ofnode_get_name(node), NULL, node,
104 NULL);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200105 if (ret)
106 break;
107 }
108
109 return ret;
110}
111
112static const struct scmi_agent_ops *transport_dev_ops(struct udevice *dev)
113{
114 return (const struct scmi_agent_ops *)dev->driver->ops;
115}
116
117int devm_scmi_process_msg(struct udevice *dev, struct scmi_msg *msg)
118{
Etienne Carriere2f26c042022-02-21 09:22:40 +0100119 const struct scmi_agent_ops *ops;
120 struct udevice *parent = dev;
121
122 /* Find related SCMI agent device */
123 do {
124 parent = dev_get_parent(parent);
125 } while (parent && device_get_uclass_id(parent) != UCLASS_SCMI_AGENT);
126
127 if (!parent) {
128 dev_err(dev, "Invalid SCMI device, agent not found\n");
129 return -ENODEV;
130 }
131
132 ops = transport_dev_ops(parent);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200133
134 if (ops->process_msg)
Etienne Carriere2f26c042022-02-21 09:22:40 +0100135 return ops->process_msg(parent, msg);
Etienne Carriere02fd1262020-09-09 18:44:00 +0200136
137 return -EPROTONOSUPPORT;
138}
139
140UCLASS_DRIVER(scmi_agent) = {
141 .id = UCLASS_SCMI_AGENT,
142 .name = "scmi_agent",
143 .post_bind = scmi_bind_protocols,
144};