2 * System Control and Power Interface (SCPI) Message Protocol driver
4 * SCPI Message Protocol is used between the System Control Processor(SCP)
5 * and the Application Processors(AP). The Message Handling Unit(MHU)
6 * provides a mechanism for inter-processor communication between SCP's
9 * SCP offers control and management of the core/cluster power states,
10 * various power domain DVFS including the core/cluster, certain system
11 * clocks configuration, thermal sensors and many others.
13 * Copyright (C) 2015 ARM Ltd.
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms and conditions of the GNU General Public License,
17 * version 2, as published by the Free Software Foundation.
19 * This program is distributed in the hope it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
24 * You should have received a copy of the GNU General Public License along
25 * with this program. If not, see <http://www.gnu.org/licenses/>.
28 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
30 #include <linux/bitmap.h>
31 #include <linux/device.h>
32 #include <linux/err.h>
33 #include <linux/export.h>
35 #include <linux/kernel.h>
36 #include <linux/list.h>
37 #include <linux/mailbox_client.h>
38 #include <linux/module.h>
39 #include <linux/of_address.h>
40 #include <linux/of_platform.h>
41 #include <linux/printk.h>
42 #include <linux/pm_opp.h>
43 #include <linux/scpi_protocol.h>
44 #include <linux/slab.h>
45 #include <linux/sort.h>
46 #include <linux/spinlock.h>
48 #define CMD_ID_SHIFT 0
49 #define CMD_ID_MASK 0x7f
50 #define CMD_TOKEN_ID_SHIFT 8
51 #define CMD_TOKEN_ID_MASK 0xff
52 #define CMD_DATA_SIZE_SHIFT 16
53 #define CMD_DATA_SIZE_MASK 0x1ff
54 #define CMD_LEGACY_DATA_SIZE_SHIFT 20
55 #define CMD_LEGACY_DATA_SIZE_MASK 0x1ff
56 #define PACK_SCPI_CMD(cmd_id, tx_sz) \
57 ((((cmd_id) & CMD_ID_MASK) << CMD_ID_SHIFT) | \
58 (((tx_sz) & CMD_DATA_SIZE_MASK) << CMD_DATA_SIZE_SHIFT))
59 #define ADD_SCPI_TOKEN(cmd, token) \
60 ((cmd) |= (((token) & CMD_TOKEN_ID_MASK) << CMD_TOKEN_ID_SHIFT))
61 #define PACK_LEGACY_SCPI_CMD(cmd_id, tx_sz) \
62 ((((cmd_id) & CMD_ID_MASK) << CMD_ID_SHIFT) | \
63 (((tx_sz) & CMD_LEGACY_DATA_SIZE_MASK) << CMD_LEGACY_DATA_SIZE_SHIFT))
65 #define CMD_SIZE(cmd) (((cmd) >> CMD_DATA_SIZE_SHIFT) & CMD_DATA_SIZE_MASK)
66 #define CMD_LEGACY_SIZE(cmd) (((cmd) >> CMD_LEGACY_DATA_SIZE_SHIFT) & \
67 CMD_LEGACY_DATA_SIZE_MASK)
68 #define CMD_UNIQ_MASK (CMD_TOKEN_ID_MASK << CMD_TOKEN_ID_SHIFT | CMD_ID_MASK)
69 #define CMD_XTRACT_UNIQ(cmd) ((cmd) & CMD_UNIQ_MASK)
73 #define MAX_DVFS_DOMAINS 8
74 #define MAX_DVFS_OPPS 16
75 #define DVFS_LATENCY(hdr) (le32_to_cpu(hdr) >> 16)
76 #define DVFS_OPP_COUNT(hdr) ((le32_to_cpu(hdr) >> 8) & 0xff)
78 #define PROTOCOL_REV_MINOR_BITS 16
79 #define PROTOCOL_REV_MINOR_MASK ((1U << PROTOCOL_REV_MINOR_BITS) - 1)
80 #define PROTOCOL_REV_MAJOR(x) ((x) >> PROTOCOL_REV_MINOR_BITS)
81 #define PROTOCOL_REV_MINOR(x) ((x) & PROTOCOL_REV_MINOR_MASK)
83 #define FW_REV_MAJOR_BITS 24
84 #define FW_REV_MINOR_BITS 16
85 #define FW_REV_PATCH_MASK ((1U << FW_REV_MINOR_BITS) - 1)
86 #define FW_REV_MINOR_MASK ((1U << FW_REV_MAJOR_BITS) - 1)
87 #define FW_REV_MAJOR(x) ((x) >> FW_REV_MAJOR_BITS)
88 #define FW_REV_MINOR(x) (((x) & FW_REV_MINOR_MASK) >> FW_REV_MINOR_BITS)
89 #define FW_REV_PATCH(x) ((x) & FW_REV_PATCH_MASK)
91 #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
93 enum scpi_error_codes {
94 SCPI_SUCCESS = 0, /* Success */
95 SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */
96 SCPI_ERR_ALIGN = 2, /* Invalid alignment */
97 SCPI_ERR_SIZE = 3, /* Invalid size */
98 SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */
99 SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */
100 SCPI_ERR_RANGE = 6, /* Value out of range */
101 SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */
102 SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */
103 SCPI_ERR_PWRSTATE = 9, /* Invalid power state */
104 SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */
105 SCPI_ERR_DEVICE = 11, /* Device error */
106 SCPI_ERR_BUSY = 12, /* Device busy */
110 /* SCPI Standard commands */
112 SCPI_CMD_INVALID = 0x00,
113 SCPI_CMD_SCPI_READY = 0x01,
114 SCPI_CMD_SCPI_CAPABILITIES = 0x02,
115 SCPI_CMD_SET_CSS_PWR_STATE = 0x03,
116 SCPI_CMD_GET_CSS_PWR_STATE = 0x04,
117 SCPI_CMD_SET_SYS_PWR_STATE = 0x05,
118 SCPI_CMD_SET_CPU_TIMER = 0x06,
119 SCPI_CMD_CANCEL_CPU_TIMER = 0x07,
120 SCPI_CMD_DVFS_CAPABILITIES = 0x08,
121 SCPI_CMD_GET_DVFS_INFO = 0x09,
122 SCPI_CMD_SET_DVFS = 0x0a,
123 SCPI_CMD_GET_DVFS = 0x0b,
124 SCPI_CMD_GET_DVFS_STAT = 0x0c,
125 SCPI_CMD_CLOCK_CAPABILITIES = 0x0d,
126 SCPI_CMD_GET_CLOCK_INFO = 0x0e,
127 SCPI_CMD_SET_CLOCK_VALUE = 0x0f,
128 SCPI_CMD_GET_CLOCK_VALUE = 0x10,
129 SCPI_CMD_PSU_CAPABILITIES = 0x11,
130 SCPI_CMD_GET_PSU_INFO = 0x12,
131 SCPI_CMD_SET_PSU = 0x13,
132 SCPI_CMD_GET_PSU = 0x14,
133 SCPI_CMD_SENSOR_CAPABILITIES = 0x15,
134 SCPI_CMD_SENSOR_INFO = 0x16,
135 SCPI_CMD_SENSOR_VALUE = 0x17,
136 SCPI_CMD_SENSOR_CFG_PERIODIC = 0x18,
137 SCPI_CMD_SENSOR_CFG_BOUNDS = 0x19,
138 SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1a,
139 SCPI_CMD_SET_DEVICE_PWR_STATE = 0x1b,
140 SCPI_CMD_GET_DEVICE_PWR_STATE = 0x1c,
144 /* SCPI Legacy Commands */
145 enum legacy_scpi_std_cmd {
146 LEGACY_SCPI_CMD_INVALID = 0x00,
147 LEGACY_SCPI_CMD_SCPI_READY = 0x01,
148 LEGACY_SCPI_CMD_SCPI_CAPABILITIES = 0x02,
149 LEGACY_SCPI_CMD_EVENT = 0x03,
150 LEGACY_SCPI_CMD_SET_CSS_PWR_STATE = 0x04,
151 LEGACY_SCPI_CMD_GET_CSS_PWR_STATE = 0x05,
152 LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT = 0x06,
153 LEGACY_SCPI_CMD_GET_PWR_STATE_STAT = 0x07,
154 LEGACY_SCPI_CMD_SYS_PWR_STATE = 0x08,
155 LEGACY_SCPI_CMD_L2_READY = 0x09,
156 LEGACY_SCPI_CMD_SET_AP_TIMER = 0x0a,
157 LEGACY_SCPI_CMD_CANCEL_AP_TIME = 0x0b,
158 LEGACY_SCPI_CMD_DVFS_CAPABILITIES = 0x0c,
159 LEGACY_SCPI_CMD_GET_DVFS_INFO = 0x0d,
160 LEGACY_SCPI_CMD_SET_DVFS = 0x0e,
161 LEGACY_SCPI_CMD_GET_DVFS = 0x0f,
162 LEGACY_SCPI_CMD_GET_DVFS_STAT = 0x10,
163 LEGACY_SCPI_CMD_SET_RTC = 0x11,
164 LEGACY_SCPI_CMD_GET_RTC = 0x12,
165 LEGACY_SCPI_CMD_CLOCK_CAPABILITIES = 0x13,
166 LEGACY_SCPI_CMD_SET_CLOCK_INDEX = 0x14,
167 LEGACY_SCPI_CMD_SET_CLOCK_VALUE = 0x15,
168 LEGACY_SCPI_CMD_GET_CLOCK_VALUE = 0x16,
169 LEGACY_SCPI_CMD_PSU_CAPABILITIES = 0x17,
170 LEGACY_SCPI_CMD_SET_PSU = 0x18,
171 LEGACY_SCPI_CMD_GET_PSU = 0x19,
172 LEGACY_SCPI_CMD_SENSOR_CAPABILITIES = 0x1a,
173 LEGACY_SCPI_CMD_SENSOR_INFO = 0x1b,
174 LEGACY_SCPI_CMD_SENSOR_VALUE = 0x1c,
175 LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC = 0x1d,
176 LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS = 0x1e,
177 LEGACY_SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1f,
178 LEGACY_SCPI_CMD_COUNT
181 /* List all commands that are required to go through the high priority link */
182 static int legacy_hpriority_cmds[] = {
183 LEGACY_SCPI_CMD_GET_CSS_PWR_STATE,
184 LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT,
185 LEGACY_SCPI_CMD_GET_PWR_STATE_STAT,
186 LEGACY_SCPI_CMD_SET_DVFS,
187 LEGACY_SCPI_CMD_GET_DVFS,
188 LEGACY_SCPI_CMD_SET_RTC,
189 LEGACY_SCPI_CMD_GET_RTC,
190 LEGACY_SCPI_CMD_SET_CLOCK_INDEX,
191 LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
192 LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
193 LEGACY_SCPI_CMD_SET_PSU,
194 LEGACY_SCPI_CMD_GET_PSU,
195 LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC,
196 LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS,
199 /* List all commands used by this driver, used as indexes */
201 CMD_SCPI_CAPABILITIES = 0,
208 CMD_SENSOR_CAPABILITIES,
211 CMD_SET_DEVICE_PWR_STATE,
212 CMD_GET_DEVICE_PWR_STATE,
216 static int scpi_std_commands[CMD_MAX_COUNT] = {
217 SCPI_CMD_SCPI_CAPABILITIES,
218 SCPI_CMD_GET_CLOCK_INFO,
219 SCPI_CMD_GET_CLOCK_VALUE,
220 SCPI_CMD_SET_CLOCK_VALUE,
223 SCPI_CMD_GET_DVFS_INFO,
224 SCPI_CMD_SENSOR_CAPABILITIES,
225 SCPI_CMD_SENSOR_INFO,
226 SCPI_CMD_SENSOR_VALUE,
227 SCPI_CMD_SET_DEVICE_PWR_STATE,
228 SCPI_CMD_GET_DEVICE_PWR_STATE,
231 static int scpi_legacy_commands[CMD_MAX_COUNT] = {
232 LEGACY_SCPI_CMD_SCPI_CAPABILITIES,
233 -1, /* GET_CLOCK_INFO */
234 LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
235 LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
236 LEGACY_SCPI_CMD_GET_DVFS,
237 LEGACY_SCPI_CMD_SET_DVFS,
238 LEGACY_SCPI_CMD_GET_DVFS_INFO,
239 LEGACY_SCPI_CMD_SENSOR_CAPABILITIES,
240 LEGACY_SCPI_CMD_SENSOR_INFO,
241 LEGACY_SCPI_CMD_SENSOR_VALUE,
242 -1, /* SET_DEVICE_PWR_STATE */
243 -1, /* GET_DEVICE_PWR_STATE */
247 u32 slot; /* has to be first element */
254 struct list_head node;
255 struct completion done;
259 struct mbox_client cl;
260 struct mbox_chan *chan;
261 void __iomem *tx_payload;
262 void __iomem *rx_payload;
263 struct list_head rx_pending;
264 struct list_head xfers_list;
265 struct scpi_xfer *xfers;
266 spinlock_t rx_lock; /* locking for the rx pending list */
267 struct mutex xfers_lock;
271 struct scpi_drvinfo {
272 u32 protocol_version;
273 u32 firmware_version;
277 DECLARE_BITMAP(cmd_priority, LEGACY_SCPI_CMD_COUNT);
279 struct scpi_ops *scpi_ops;
280 struct scpi_chan *channels;
281 struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS];
285 * The SCP firmware only executes in little-endian mode, so any buffers
286 * shared through SCPI should have their contents converted to little-endian
288 struct scpi_shared_mem {
294 struct legacy_scpi_shared_mem {
299 struct scp_capabilities {
300 __le32 protocol_version;
301 __le32 event_version;
302 __le32 platform_version;
306 struct clk_get_info {
314 struct clk_get_value {
318 struct clk_set_value {
324 struct legacy_clk_set_value {
335 } opps[MAX_DVFS_OPPS];
343 struct sensor_capabilities {
347 struct _scpi_sensor_info {
354 struct sensor_value {
359 struct dev_pstate_set {
364 static struct scpi_drvinfo *scpi_info;
366 static int scpi_linux_errmap[SCPI_ERR_MAX] = {
367 /* better than switch case as long as return value is continuous */
368 0, /* SCPI_SUCCESS */
369 -EINVAL, /* SCPI_ERR_PARAM */
370 -ENOEXEC, /* SCPI_ERR_ALIGN */
371 -EMSGSIZE, /* SCPI_ERR_SIZE */
372 -EINVAL, /* SCPI_ERR_HANDLER */
373 -EACCES, /* SCPI_ERR_ACCESS */
374 -ERANGE, /* SCPI_ERR_RANGE */
375 -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
376 -ENOMEM, /* SCPI_ERR_NOMEM */
377 -EINVAL, /* SCPI_ERR_PWRSTATE */
378 -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
379 -EIO, /* SCPI_ERR_DEVICE */
380 -EBUSY, /* SCPI_ERR_BUSY */
383 static inline int scpi_to_linux_errno(int errno)
385 if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
386 return scpi_linux_errmap[errno];
390 static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
393 struct scpi_xfer *t, *match = NULL;
395 spin_lock_irqsave(&ch->rx_lock, flags);
396 if (list_empty(&ch->rx_pending)) {
397 spin_unlock_irqrestore(&ch->rx_lock, flags);
401 /* Command type is not replied by the SCP Firmware in legacy Mode
402 * We should consider that command is the head of pending RX commands
403 * if the list is not empty. In TX only mode, the list would be empty.
405 if (scpi_info->is_legacy) {
406 match = list_first_entry(&ch->rx_pending, struct scpi_xfer,
408 list_del(&match->node);
410 list_for_each_entry(t, &ch->rx_pending, node)
411 if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
417 /* check if wait_for_completion is in progress or timed-out */
418 if (match && !completion_done(&match->done)) {
421 if (scpi_info->is_legacy) {
422 struct legacy_scpi_shared_mem *mem = ch->rx_payload;
424 /* RX Length is not replied by the legacy Firmware */
427 match->status = le32_to_cpu(mem->status);
428 memcpy_fromio(match->rx_buf, mem->payload, len);
430 struct scpi_shared_mem *mem = ch->rx_payload;
432 len = min(match->rx_len, CMD_SIZE(cmd));
434 match->status = le32_to_cpu(mem->status);
435 memcpy_fromio(match->rx_buf, mem->payload, len);
438 if (match->rx_len > len)
439 memset(match->rx_buf + len, 0, match->rx_len - len);
440 complete(&match->done);
442 spin_unlock_irqrestore(&ch->rx_lock, flags);
445 static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
447 struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
448 struct scpi_shared_mem *mem = ch->rx_payload;
451 if (!scpi_info->is_legacy)
452 cmd = le32_to_cpu(mem->command);
454 scpi_process_cmd(ch, cmd);
457 static void scpi_tx_prepare(struct mbox_client *c, void *msg)
460 struct scpi_xfer *t = msg;
461 struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
462 struct scpi_shared_mem *mem = (struct scpi_shared_mem *)ch->tx_payload;
465 if (scpi_info->is_legacy)
466 memcpy_toio(ch->tx_payload, t->tx_buf, t->tx_len);
468 memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
474 ADD_SCPI_TOKEN(t->cmd, ch->token);
475 spin_lock_irqsave(&ch->rx_lock, flags);
476 list_add_tail(&t->node, &ch->rx_pending);
477 spin_unlock_irqrestore(&ch->rx_lock, flags);
480 if (!scpi_info->is_legacy)
481 mem->command = cpu_to_le32(t->cmd);
484 static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
488 mutex_lock(&ch->xfers_lock);
489 if (list_empty(&ch->xfers_list)) {
490 mutex_unlock(&ch->xfers_lock);
493 t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
495 mutex_unlock(&ch->xfers_lock);
499 static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
501 mutex_lock(&ch->xfers_lock);
502 list_add_tail(&t->node, &ch->xfers_list);
503 mutex_unlock(&ch->xfers_lock);
506 static int scpi_send_message(u8 idx, void *tx_buf, unsigned int tx_len,
507 void *rx_buf, unsigned int rx_len)
512 struct scpi_xfer *msg;
513 struct scpi_chan *scpi_chan;
515 if (scpi_info->commands[idx] < 0)
518 cmd = scpi_info->commands[idx];
520 if (scpi_info->is_legacy)
521 chan = test_bit(cmd, scpi_info->cmd_priority) ? 1 : 0;
523 chan = atomic_inc_return(&scpi_info->next_chan) %
524 scpi_info->num_chans;
525 scpi_chan = scpi_info->channels + chan;
527 msg = get_scpi_xfer(scpi_chan);
531 if (scpi_info->is_legacy) {
532 msg->cmd = PACK_LEGACY_SCPI_CMD(cmd, tx_len);
533 msg->slot = msg->cmd;
535 msg->slot = BIT(SCPI_SLOT);
536 msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
538 msg->tx_buf = tx_buf;
539 msg->tx_len = tx_len;
540 msg->rx_buf = rx_buf;
541 msg->rx_len = rx_len;
542 reinit_completion(&msg->done);
544 ret = mbox_send_message(scpi_chan->chan, msg);
545 if (ret < 0 || !rx_buf)
548 if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
551 /* first status word */
554 if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
555 scpi_process_cmd(scpi_chan, msg->cmd);
557 put_scpi_xfer(msg, scpi_chan);
558 /* SCPI error codes > 0, translate them to Linux scale*/
559 return ret > 0 ? scpi_to_linux_errno(ret) : ret;
562 static u32 scpi_get_version(void)
564 return scpi_info->protocol_version;
568 scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
571 struct clk_get_info clk;
572 __le16 le_clk_id = cpu_to_le16(clk_id);
574 ret = scpi_send_message(CMD_GET_CLOCK_INFO, &le_clk_id,
575 sizeof(le_clk_id), &clk, sizeof(clk));
577 *min = le32_to_cpu(clk.min_rate);
578 *max = le32_to_cpu(clk.max_rate);
583 static unsigned long scpi_clk_get_val(u16 clk_id)
586 struct clk_get_value clk;
587 __le16 le_clk_id = cpu_to_le16(clk_id);
589 ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
590 sizeof(le_clk_id), &clk, sizeof(clk));
592 return ret ? ret : le32_to_cpu(clk.rate);
595 static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
598 struct clk_set_value clk = {
599 .id = cpu_to_le16(clk_id),
600 .rate = cpu_to_le32(rate)
603 return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
604 &stat, sizeof(stat));
607 static int legacy_scpi_clk_set_val(u16 clk_id, unsigned long rate)
610 struct legacy_clk_set_value clk = {
611 .id = cpu_to_le16(clk_id),
612 .rate = cpu_to_le32(rate)
615 return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
616 &stat, sizeof(stat));
619 static int scpi_dvfs_get_idx(u8 domain)
624 ret = scpi_send_message(CMD_GET_DVFS, &domain, sizeof(domain),
625 &dvfs_idx, sizeof(dvfs_idx));
627 return ret ? ret : dvfs_idx;
630 static int scpi_dvfs_set_idx(u8 domain, u8 index)
633 struct dvfs_set dvfs = {domain, index};
635 return scpi_send_message(CMD_SET_DVFS, &dvfs, sizeof(dvfs),
636 &stat, sizeof(stat));
639 static int opp_cmp_func(const void *opp1, const void *opp2)
641 const struct scpi_opp *t1 = opp1, *t2 = opp2;
643 return t1->freq - t2->freq;
646 static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
648 struct scpi_dvfs_info *info;
649 struct scpi_opp *opp;
650 struct dvfs_info buf;
653 if (domain >= MAX_DVFS_DOMAINS)
654 return ERR_PTR(-EINVAL);
656 if (scpi_info->dvfs[domain]) /* data already populated */
657 return scpi_info->dvfs[domain];
659 ret = scpi_send_message(CMD_GET_DVFS_INFO, &domain, sizeof(domain),
664 info = kmalloc(sizeof(*info), GFP_KERNEL);
666 return ERR_PTR(-ENOMEM);
668 info->count = DVFS_OPP_COUNT(buf.header);
669 info->latency = DVFS_LATENCY(buf.header) * 1000; /* uS to nS */
671 info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL);
674 return ERR_PTR(-ENOMEM);
677 for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
678 opp->freq = le32_to_cpu(buf.opps[i].freq);
679 opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
682 sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
684 scpi_info->dvfs[domain] = info;
688 static int scpi_dev_domain_id(struct device *dev)
690 struct of_phandle_args clkspec;
692 if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
696 return clkspec.args[0];
699 static struct scpi_dvfs_info *scpi_dvfs_info(struct device *dev)
701 int domain = scpi_dev_domain_id(dev);
704 return ERR_PTR(domain);
706 return scpi_dvfs_get_info(domain);
709 static int scpi_dvfs_get_transition_latency(struct device *dev)
711 struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
714 return PTR_ERR(info);
719 return info->latency;
722 static int scpi_dvfs_add_opps_to_device(struct device *dev)
725 struct scpi_opp *opp;
726 struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
729 return PTR_ERR(info);
734 for (opp = info->opps, idx = 0; idx < info->count; idx++, opp++) {
735 ret = dev_pm_opp_add(dev, opp->freq, opp->m_volt * 1000);
737 dev_warn(dev, "failed to add opp %uHz %umV\n",
738 opp->freq, opp->m_volt);
740 dev_pm_opp_remove(dev, (--opp)->freq);
747 static int scpi_sensor_get_capability(u16 *sensors)
749 struct sensor_capabilities cap_buf;
752 ret = scpi_send_message(CMD_SENSOR_CAPABILITIES, NULL, 0, &cap_buf,
755 *sensors = le16_to_cpu(cap_buf.sensors);
760 static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
762 __le16 id = cpu_to_le16(sensor_id);
763 struct _scpi_sensor_info _info;
766 ret = scpi_send_message(CMD_SENSOR_INFO, &id, sizeof(id),
767 &_info, sizeof(_info));
769 memcpy(info, &_info, sizeof(*info));
770 info->sensor_id = le16_to_cpu(_info.sensor_id);
776 static int scpi_sensor_get_value(u16 sensor, u64 *val)
778 __le16 id = cpu_to_le16(sensor);
779 struct sensor_value buf;
782 ret = scpi_send_message(CMD_SENSOR_VALUE, &id, sizeof(id),
787 if (scpi_info->is_legacy)
788 /* only 32-bits supported, hi_val can be junk */
789 *val = le32_to_cpu(buf.lo_val);
791 *val = (u64)le32_to_cpu(buf.hi_val) << 32 |
792 le32_to_cpu(buf.lo_val);
797 static int scpi_device_get_power_state(u16 dev_id)
801 __le16 id = cpu_to_le16(dev_id);
803 ret = scpi_send_message(CMD_GET_DEVICE_PWR_STATE, &id,
804 sizeof(id), &pstate, sizeof(pstate));
805 return ret ? ret : pstate;
808 static int scpi_device_set_power_state(u16 dev_id, u8 pstate)
811 struct dev_pstate_set dev_set = {
812 .dev_id = cpu_to_le16(dev_id),
816 return scpi_send_message(CMD_SET_DEVICE_PWR_STATE, &dev_set,
817 sizeof(dev_set), &stat, sizeof(stat));
820 static struct scpi_ops scpi_ops = {
821 .get_version = scpi_get_version,
822 .clk_get_range = scpi_clk_get_range,
823 .clk_get_val = scpi_clk_get_val,
824 .clk_set_val = scpi_clk_set_val,
825 .dvfs_get_idx = scpi_dvfs_get_idx,
826 .dvfs_set_idx = scpi_dvfs_set_idx,
827 .dvfs_get_info = scpi_dvfs_get_info,
828 .device_domain_id = scpi_dev_domain_id,
829 .get_transition_latency = scpi_dvfs_get_transition_latency,
830 .add_opps_to_device = scpi_dvfs_add_opps_to_device,
831 .sensor_get_capability = scpi_sensor_get_capability,
832 .sensor_get_info = scpi_sensor_get_info,
833 .sensor_get_value = scpi_sensor_get_value,
834 .device_get_power_state = scpi_device_get_power_state,
835 .device_set_power_state = scpi_device_set_power_state,
838 struct scpi_ops *get_scpi_ops(void)
840 return scpi_info ? scpi_info->scpi_ops : NULL;
842 EXPORT_SYMBOL_GPL(get_scpi_ops);
844 static int scpi_init_versions(struct scpi_drvinfo *info)
847 struct scp_capabilities caps;
849 ret = scpi_send_message(CMD_SCPI_CAPABILITIES, NULL, 0,
850 &caps, sizeof(caps));
852 info->protocol_version = le32_to_cpu(caps.protocol_version);
853 info->firmware_version = le32_to_cpu(caps.platform_version);
855 /* Ignore error if not implemented */
856 if (scpi_info->is_legacy && ret == -EOPNOTSUPP)
862 static ssize_t protocol_version_show(struct device *dev,
863 struct device_attribute *attr, char *buf)
865 struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
867 return sprintf(buf, "%d.%d\n",
868 PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
869 PROTOCOL_REV_MINOR(scpi_info->protocol_version));
871 static DEVICE_ATTR_RO(protocol_version);
873 static ssize_t firmware_version_show(struct device *dev,
874 struct device_attribute *attr, char *buf)
876 struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
878 return sprintf(buf, "%d.%d.%d\n",
879 FW_REV_MAJOR(scpi_info->firmware_version),
880 FW_REV_MINOR(scpi_info->firmware_version),
881 FW_REV_PATCH(scpi_info->firmware_version));
883 static DEVICE_ATTR_RO(firmware_version);
885 static struct attribute *versions_attrs[] = {
886 &dev_attr_firmware_version.attr,
887 &dev_attr_protocol_version.attr,
890 ATTRIBUTE_GROUPS(versions);
893 scpi_free_channels(struct device *dev, struct scpi_chan *pchan, int count)
897 for (i = 0; i < count && pchan->chan; i++, pchan++) {
898 mbox_free_channel(pchan->chan);
899 devm_kfree(dev, pchan->xfers);
900 devm_iounmap(dev, pchan->rx_payload);
904 static int scpi_remove(struct platform_device *pdev)
907 struct device *dev = &pdev->dev;
908 struct scpi_drvinfo *info = platform_get_drvdata(pdev);
910 scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
912 of_platform_depopulate(dev);
913 sysfs_remove_groups(&dev->kobj, versions_groups);
914 scpi_free_channels(dev, info->channels, info->num_chans);
915 platform_set_drvdata(pdev, NULL);
917 for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
918 kfree(info->dvfs[i]->opps);
919 kfree(info->dvfs[i]);
921 devm_kfree(dev, info->channels);
922 devm_kfree(dev, info);
927 #define MAX_SCPI_XFERS 10
928 static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
931 struct scpi_xfer *xfers;
933 xfers = devm_kzalloc(dev, MAX_SCPI_XFERS * sizeof(*xfers), GFP_KERNEL);
938 for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++) {
939 init_completion(&xfers->done);
940 list_add_tail(&xfers->node, &ch->xfers_list);
946 static const struct of_device_id legacy_scpi_of_match[] = {
947 {.compatible = "arm,scpi-pre-1.0"},
951 static int scpi_probe(struct platform_device *pdev)
955 struct scpi_chan *scpi_chan;
956 struct device *dev = &pdev->dev;
957 struct device_node *np = dev->of_node;
959 scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL);
963 if (of_match_device(legacy_scpi_of_match, &pdev->dev))
964 scpi_info->is_legacy = true;
966 count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
968 dev_err(dev, "no mboxes property in '%pOF'\n", np);
972 scpi_chan = devm_kcalloc(dev, count, sizeof(*scpi_chan), GFP_KERNEL);
976 for (idx = 0; idx < count; idx++) {
977 resource_size_t size;
978 struct scpi_chan *pchan = scpi_chan + idx;
979 struct mbox_client *cl = &pchan->cl;
980 struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
982 ret = of_address_to_resource(shmem, 0, &res);
985 dev_err(dev, "failed to get SCPI payload mem resource\n");
989 size = resource_size(&res);
990 pchan->rx_payload = devm_ioremap(dev, res.start, size);
991 if (!pchan->rx_payload) {
992 dev_err(dev, "failed to ioremap SCPI payload\n");
993 ret = -EADDRNOTAVAIL;
996 pchan->tx_payload = pchan->rx_payload + (size >> 1);
999 cl->rx_callback = scpi_handle_remote_msg;
1000 cl->tx_prepare = scpi_tx_prepare;
1001 cl->tx_block = true;
1003 cl->knows_txdone = false; /* controller can't ack */
1005 INIT_LIST_HEAD(&pchan->rx_pending);
1006 INIT_LIST_HEAD(&pchan->xfers_list);
1007 spin_lock_init(&pchan->rx_lock);
1008 mutex_init(&pchan->xfers_lock);
1010 ret = scpi_alloc_xfer_list(dev, pchan);
1012 pchan->chan = mbox_request_channel(cl, idx);
1013 if (!IS_ERR(pchan->chan))
1015 ret = PTR_ERR(pchan->chan);
1016 if (ret != -EPROBE_DEFER)
1017 dev_err(dev, "failed to get channel%d err %d\n",
1021 scpi_free_channels(dev, scpi_chan, idx);
1026 scpi_info->channels = scpi_chan;
1027 scpi_info->num_chans = count;
1028 scpi_info->commands = scpi_std_commands;
1030 platform_set_drvdata(pdev, scpi_info);
1032 if (scpi_info->is_legacy) {
1033 /* Replace with legacy variants */
1034 scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
1035 scpi_info->commands = scpi_legacy_commands;
1037 /* Fill priority bitmap */
1038 for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
1039 set_bit(legacy_hpriority_cmds[idx],
1040 scpi_info->cmd_priority);
1043 ret = scpi_init_versions(scpi_info);
1045 dev_err(dev, "incorrect or no SCP firmware found\n");
1050 _dev_info(dev, "SCP Protocol %d.%d Firmware %d.%d.%d version\n",
1051 PROTOCOL_REV_MAJOR(scpi_info->protocol_version),
1052 PROTOCOL_REV_MINOR(scpi_info->protocol_version),
1053 FW_REV_MAJOR(scpi_info->firmware_version),
1054 FW_REV_MINOR(scpi_info->firmware_version),
1055 FW_REV_PATCH(scpi_info->firmware_version));
1056 scpi_info->scpi_ops = &scpi_ops;
1058 ret = sysfs_create_groups(&dev->kobj, versions_groups);
1060 dev_err(dev, "unable to create sysfs version group\n");
1062 return of_platform_populate(dev->of_node, NULL, NULL, dev);
1065 static const struct of_device_id scpi_of_match[] = {
1066 {.compatible = "arm,scpi"},
1067 {.compatible = "arm,scpi-pre-1.0"},
1071 MODULE_DEVICE_TABLE(of, scpi_of_match);
1073 static struct platform_driver scpi_driver = {
1075 .name = "scpi_protocol",
1076 .of_match_table = scpi_of_match,
1078 .probe = scpi_probe,
1079 .remove = scpi_remove,
1081 module_platform_driver(scpi_driver);
1083 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1084 MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
1085 MODULE_LICENSE("GPL v2");