1 // SPDX-License-Identifier: GPL-2.0-only
3 * System Control and Power Interface (SCPI) Message Protocol driver
5 * SCPI Message Protocol is used between the System Control Processor(SCP)
6 * and the Application Processors(AP). The Message Handling Unit(MHU)
7 * provides a mechanism for inter-processor communication between SCP's
10 * SCP offers control and management of the core/cluster power states,
11 * various power domain DVFS including the core/cluster, certain system
12 * clocks configuration, thermal sensors and many others.
14 * Copyright (C) 2015 ARM Ltd.
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/bitmap.h>
20 #include <linux/bitfield.h>
21 #include <linux/device.h>
22 #include <linux/err.h>
23 #include <linux/export.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/mailbox_client.h>
28 #include <linux/module.h>
29 #include <linux/of_address.h>
30 #include <linux/of_platform.h>
31 #include <linux/printk.h>
32 #include <linux/pm_opp.h>
33 #include <linux/scpi_protocol.h>
34 #include <linux/slab.h>
35 #include <linux/sort.h>
36 #include <linux/spinlock.h>
38 #define CMD_ID_MASK GENMASK(6, 0)
39 #define CMD_TOKEN_ID_MASK GENMASK(15, 8)
40 #define CMD_DATA_SIZE_MASK GENMASK(24, 16)
41 #define CMD_LEGACY_DATA_SIZE_MASK GENMASK(28, 20)
42 #define PACK_SCPI_CMD(cmd_id, tx_sz) \
43 (FIELD_PREP(CMD_ID_MASK, cmd_id) | \
44 FIELD_PREP(CMD_DATA_SIZE_MASK, tx_sz))
45 #define PACK_LEGACY_SCPI_CMD(cmd_id, tx_sz) \
46 (FIELD_PREP(CMD_ID_MASK, cmd_id) | \
47 FIELD_PREP(CMD_LEGACY_DATA_SIZE_MASK, tx_sz))
49 #define CMD_SIZE(cmd) FIELD_GET(CMD_DATA_SIZE_MASK, cmd)
50 #define CMD_UNIQ_MASK (CMD_TOKEN_ID_MASK | CMD_ID_MASK)
51 #define CMD_XTRACT_UNIQ(cmd) ((cmd) & CMD_UNIQ_MASK)
55 #define MAX_DVFS_DOMAINS 8
56 #define MAX_DVFS_OPPS 16
58 #define PROTO_REV_MAJOR_MASK GENMASK(31, 16)
59 #define PROTO_REV_MINOR_MASK GENMASK(15, 0)
61 #define FW_REV_MAJOR_MASK GENMASK(31, 24)
62 #define FW_REV_MINOR_MASK GENMASK(23, 16)
63 #define FW_REV_PATCH_MASK GENMASK(15, 0)
65 #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
67 enum scpi_error_codes {
68 SCPI_SUCCESS = 0, /* Success */
69 SCPI_ERR_PARAM = 1, /* Invalid parameter(s) */
70 SCPI_ERR_ALIGN = 2, /* Invalid alignment */
71 SCPI_ERR_SIZE = 3, /* Invalid size */
72 SCPI_ERR_HANDLER = 4, /* Invalid handler/callback */
73 SCPI_ERR_ACCESS = 5, /* Invalid access/permission denied */
74 SCPI_ERR_RANGE = 6, /* Value out of range */
75 SCPI_ERR_TIMEOUT = 7, /* Timeout has occurred */
76 SCPI_ERR_NOMEM = 8, /* Invalid memory area or pointer */
77 SCPI_ERR_PWRSTATE = 9, /* Invalid power state */
78 SCPI_ERR_SUPPORT = 10, /* Not supported or disabled */
79 SCPI_ERR_DEVICE = 11, /* Device error */
80 SCPI_ERR_BUSY = 12, /* Device busy */
84 /* SCPI Standard commands */
86 SCPI_CMD_INVALID = 0x00,
87 SCPI_CMD_SCPI_READY = 0x01,
88 SCPI_CMD_SCPI_CAPABILITIES = 0x02,
89 SCPI_CMD_SET_CSS_PWR_STATE = 0x03,
90 SCPI_CMD_GET_CSS_PWR_STATE = 0x04,
91 SCPI_CMD_SET_SYS_PWR_STATE = 0x05,
92 SCPI_CMD_SET_CPU_TIMER = 0x06,
93 SCPI_CMD_CANCEL_CPU_TIMER = 0x07,
94 SCPI_CMD_DVFS_CAPABILITIES = 0x08,
95 SCPI_CMD_GET_DVFS_INFO = 0x09,
96 SCPI_CMD_SET_DVFS = 0x0a,
97 SCPI_CMD_GET_DVFS = 0x0b,
98 SCPI_CMD_GET_DVFS_STAT = 0x0c,
99 SCPI_CMD_CLOCK_CAPABILITIES = 0x0d,
100 SCPI_CMD_GET_CLOCK_INFO = 0x0e,
101 SCPI_CMD_SET_CLOCK_VALUE = 0x0f,
102 SCPI_CMD_GET_CLOCK_VALUE = 0x10,
103 SCPI_CMD_PSU_CAPABILITIES = 0x11,
104 SCPI_CMD_GET_PSU_INFO = 0x12,
105 SCPI_CMD_SET_PSU = 0x13,
106 SCPI_CMD_GET_PSU = 0x14,
107 SCPI_CMD_SENSOR_CAPABILITIES = 0x15,
108 SCPI_CMD_SENSOR_INFO = 0x16,
109 SCPI_CMD_SENSOR_VALUE = 0x17,
110 SCPI_CMD_SENSOR_CFG_PERIODIC = 0x18,
111 SCPI_CMD_SENSOR_CFG_BOUNDS = 0x19,
112 SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1a,
113 SCPI_CMD_SET_DEVICE_PWR_STATE = 0x1b,
114 SCPI_CMD_GET_DEVICE_PWR_STATE = 0x1c,
118 /* SCPI Legacy Commands */
119 enum legacy_scpi_std_cmd {
120 LEGACY_SCPI_CMD_INVALID = 0x00,
121 LEGACY_SCPI_CMD_SCPI_READY = 0x01,
122 LEGACY_SCPI_CMD_SCPI_CAPABILITIES = 0x02,
123 LEGACY_SCPI_CMD_EVENT = 0x03,
124 LEGACY_SCPI_CMD_SET_CSS_PWR_STATE = 0x04,
125 LEGACY_SCPI_CMD_GET_CSS_PWR_STATE = 0x05,
126 LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT = 0x06,
127 LEGACY_SCPI_CMD_GET_PWR_STATE_STAT = 0x07,
128 LEGACY_SCPI_CMD_SYS_PWR_STATE = 0x08,
129 LEGACY_SCPI_CMD_L2_READY = 0x09,
130 LEGACY_SCPI_CMD_SET_AP_TIMER = 0x0a,
131 LEGACY_SCPI_CMD_CANCEL_AP_TIME = 0x0b,
132 LEGACY_SCPI_CMD_DVFS_CAPABILITIES = 0x0c,
133 LEGACY_SCPI_CMD_GET_DVFS_INFO = 0x0d,
134 LEGACY_SCPI_CMD_SET_DVFS = 0x0e,
135 LEGACY_SCPI_CMD_GET_DVFS = 0x0f,
136 LEGACY_SCPI_CMD_GET_DVFS_STAT = 0x10,
137 LEGACY_SCPI_CMD_SET_RTC = 0x11,
138 LEGACY_SCPI_CMD_GET_RTC = 0x12,
139 LEGACY_SCPI_CMD_CLOCK_CAPABILITIES = 0x13,
140 LEGACY_SCPI_CMD_SET_CLOCK_INDEX = 0x14,
141 LEGACY_SCPI_CMD_SET_CLOCK_VALUE = 0x15,
142 LEGACY_SCPI_CMD_GET_CLOCK_VALUE = 0x16,
143 LEGACY_SCPI_CMD_PSU_CAPABILITIES = 0x17,
144 LEGACY_SCPI_CMD_SET_PSU = 0x18,
145 LEGACY_SCPI_CMD_GET_PSU = 0x19,
146 LEGACY_SCPI_CMD_SENSOR_CAPABILITIES = 0x1a,
147 LEGACY_SCPI_CMD_SENSOR_INFO = 0x1b,
148 LEGACY_SCPI_CMD_SENSOR_VALUE = 0x1c,
149 LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC = 0x1d,
150 LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS = 0x1e,
151 LEGACY_SCPI_CMD_SENSOR_ASYNC_VALUE = 0x1f,
152 LEGACY_SCPI_CMD_COUNT
155 /* List all commands that are required to go through the high priority link */
156 static int legacy_hpriority_cmds[] = {
157 LEGACY_SCPI_CMD_GET_CSS_PWR_STATE,
158 LEGACY_SCPI_CMD_CFG_PWR_STATE_STAT,
159 LEGACY_SCPI_CMD_GET_PWR_STATE_STAT,
160 LEGACY_SCPI_CMD_SET_DVFS,
161 LEGACY_SCPI_CMD_GET_DVFS,
162 LEGACY_SCPI_CMD_SET_RTC,
163 LEGACY_SCPI_CMD_GET_RTC,
164 LEGACY_SCPI_CMD_SET_CLOCK_INDEX,
165 LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
166 LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
167 LEGACY_SCPI_CMD_SET_PSU,
168 LEGACY_SCPI_CMD_GET_PSU,
169 LEGACY_SCPI_CMD_SENSOR_CFG_PERIODIC,
170 LEGACY_SCPI_CMD_SENSOR_CFG_BOUNDS,
173 /* List all commands used by this driver, used as indexes */
175 CMD_SCPI_CAPABILITIES = 0,
182 CMD_SENSOR_CAPABILITIES,
185 CMD_SET_DEVICE_PWR_STATE,
186 CMD_GET_DEVICE_PWR_STATE,
190 static int scpi_std_commands[CMD_MAX_COUNT] = {
191 SCPI_CMD_SCPI_CAPABILITIES,
192 SCPI_CMD_GET_CLOCK_INFO,
193 SCPI_CMD_GET_CLOCK_VALUE,
194 SCPI_CMD_SET_CLOCK_VALUE,
197 SCPI_CMD_GET_DVFS_INFO,
198 SCPI_CMD_SENSOR_CAPABILITIES,
199 SCPI_CMD_SENSOR_INFO,
200 SCPI_CMD_SENSOR_VALUE,
201 SCPI_CMD_SET_DEVICE_PWR_STATE,
202 SCPI_CMD_GET_DEVICE_PWR_STATE,
205 static int scpi_legacy_commands[CMD_MAX_COUNT] = {
206 LEGACY_SCPI_CMD_SCPI_CAPABILITIES,
207 -1, /* GET_CLOCK_INFO */
208 LEGACY_SCPI_CMD_GET_CLOCK_VALUE,
209 LEGACY_SCPI_CMD_SET_CLOCK_VALUE,
210 LEGACY_SCPI_CMD_GET_DVFS,
211 LEGACY_SCPI_CMD_SET_DVFS,
212 LEGACY_SCPI_CMD_GET_DVFS_INFO,
213 LEGACY_SCPI_CMD_SENSOR_CAPABILITIES,
214 LEGACY_SCPI_CMD_SENSOR_INFO,
215 LEGACY_SCPI_CMD_SENSOR_VALUE,
216 -1, /* SET_DEVICE_PWR_STATE */
217 -1, /* GET_DEVICE_PWR_STATE */
221 u32 slot; /* has to be first element */
228 struct list_head node;
229 struct completion done;
233 struct mbox_client cl;
234 struct mbox_chan *chan;
235 void __iomem *tx_payload;
236 void __iomem *rx_payload;
237 struct list_head rx_pending;
238 struct list_head xfers_list;
239 struct scpi_xfer *xfers;
240 spinlock_t rx_lock; /* locking for the rx pending list */
241 struct mutex xfers_lock;
245 struct scpi_drvinfo {
246 u32 protocol_version;
247 u32 firmware_version;
251 DECLARE_BITMAP(cmd_priority, LEGACY_SCPI_CMD_COUNT);
253 struct scpi_ops *scpi_ops;
254 struct scpi_chan *channels;
255 struct scpi_dvfs_info *dvfs[MAX_DVFS_DOMAINS];
259 * The SCP firmware only executes in little-endian mode, so any buffers
260 * shared through SCPI should have their contents converted to little-endian
262 struct scpi_shared_mem {
268 struct legacy_scpi_shared_mem {
273 struct scp_capabilities {
274 __le32 protocol_version;
275 __le32 event_version;
276 __le32 platform_version;
280 struct clk_get_info {
288 struct clk_set_value {
294 struct legacy_clk_set_value {
307 } opps[MAX_DVFS_OPPS];
315 struct _scpi_sensor_info {
322 struct dev_pstate_set {
327 static struct scpi_drvinfo *scpi_info;
329 static int scpi_linux_errmap[SCPI_ERR_MAX] = {
330 /* better than switch case as long as return value is continuous */
331 0, /* SCPI_SUCCESS */
332 -EINVAL, /* SCPI_ERR_PARAM */
333 -ENOEXEC, /* SCPI_ERR_ALIGN */
334 -EMSGSIZE, /* SCPI_ERR_SIZE */
335 -EINVAL, /* SCPI_ERR_HANDLER */
336 -EACCES, /* SCPI_ERR_ACCESS */
337 -ERANGE, /* SCPI_ERR_RANGE */
338 -ETIMEDOUT, /* SCPI_ERR_TIMEOUT */
339 -ENOMEM, /* SCPI_ERR_NOMEM */
340 -EINVAL, /* SCPI_ERR_PWRSTATE */
341 -EOPNOTSUPP, /* SCPI_ERR_SUPPORT */
342 -EIO, /* SCPI_ERR_DEVICE */
343 -EBUSY, /* SCPI_ERR_BUSY */
346 static inline int scpi_to_linux_errno(int errno)
348 if (errno >= SCPI_SUCCESS && errno < SCPI_ERR_MAX)
349 return scpi_linux_errmap[errno];
353 static void scpi_process_cmd(struct scpi_chan *ch, u32 cmd)
356 struct scpi_xfer *t, *match = NULL;
358 spin_lock_irqsave(&ch->rx_lock, flags);
359 if (list_empty(&ch->rx_pending)) {
360 spin_unlock_irqrestore(&ch->rx_lock, flags);
364 /* Command type is not replied by the SCP Firmware in legacy Mode
365 * We should consider that command is the head of pending RX commands
366 * if the list is not empty. In TX only mode, the list would be empty.
368 if (scpi_info->is_legacy) {
369 match = list_first_entry(&ch->rx_pending, struct scpi_xfer,
371 list_del(&match->node);
373 list_for_each_entry(t, &ch->rx_pending, node)
374 if (CMD_XTRACT_UNIQ(t->cmd) == CMD_XTRACT_UNIQ(cmd)) {
380 /* check if wait_for_completion is in progress or timed-out */
381 if (match && !completion_done(&match->done)) {
384 if (scpi_info->is_legacy) {
385 struct legacy_scpi_shared_mem __iomem *mem =
388 /* RX Length is not replied by the legacy Firmware */
391 match->status = ioread32(&mem->status);
392 memcpy_fromio(match->rx_buf, mem->payload, len);
394 struct scpi_shared_mem __iomem *mem = ch->rx_payload;
396 len = min_t(unsigned int, match->rx_len, CMD_SIZE(cmd));
398 match->status = ioread32(&mem->status);
399 memcpy_fromio(match->rx_buf, mem->payload, len);
402 if (match->rx_len > len)
403 memset(match->rx_buf + len, 0, match->rx_len - len);
404 complete(&match->done);
406 spin_unlock_irqrestore(&ch->rx_lock, flags);
409 static void scpi_handle_remote_msg(struct mbox_client *c, void *msg)
411 struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
412 struct scpi_shared_mem __iomem *mem = ch->rx_payload;
415 if (!scpi_info->is_legacy)
416 cmd = ioread32(&mem->command);
418 scpi_process_cmd(ch, cmd);
421 static void scpi_tx_prepare(struct mbox_client *c, void *msg)
424 struct scpi_xfer *t = msg;
425 struct scpi_chan *ch = container_of(c, struct scpi_chan, cl);
426 struct scpi_shared_mem __iomem *mem = ch->tx_payload;
429 if (scpi_info->is_legacy)
430 memcpy_toio(ch->tx_payload, t->tx_buf, t->tx_len);
432 memcpy_toio(mem->payload, t->tx_buf, t->tx_len);
438 t->cmd |= FIELD_PREP(CMD_TOKEN_ID_MASK, ch->token);
439 spin_lock_irqsave(&ch->rx_lock, flags);
440 list_add_tail(&t->node, &ch->rx_pending);
441 spin_unlock_irqrestore(&ch->rx_lock, flags);
444 if (!scpi_info->is_legacy)
445 iowrite32(t->cmd, &mem->command);
448 static struct scpi_xfer *get_scpi_xfer(struct scpi_chan *ch)
452 mutex_lock(&ch->xfers_lock);
453 if (list_empty(&ch->xfers_list)) {
454 mutex_unlock(&ch->xfers_lock);
457 t = list_first_entry(&ch->xfers_list, struct scpi_xfer, node);
459 mutex_unlock(&ch->xfers_lock);
463 static void put_scpi_xfer(struct scpi_xfer *t, struct scpi_chan *ch)
465 mutex_lock(&ch->xfers_lock);
466 list_add_tail(&t->node, &ch->xfers_list);
467 mutex_unlock(&ch->xfers_lock);
470 static int scpi_send_message(u8 idx, void *tx_buf, unsigned int tx_len,
471 void *rx_buf, unsigned int rx_len)
476 struct scpi_xfer *msg;
477 struct scpi_chan *scpi_chan;
479 if (scpi_info->commands[idx] < 0)
482 cmd = scpi_info->commands[idx];
484 if (scpi_info->is_legacy)
485 chan = test_bit(cmd, scpi_info->cmd_priority) ? 1 : 0;
487 chan = atomic_inc_return(&scpi_info->next_chan) %
488 scpi_info->num_chans;
489 scpi_chan = scpi_info->channels + chan;
491 msg = get_scpi_xfer(scpi_chan);
495 if (scpi_info->is_legacy) {
496 msg->cmd = PACK_LEGACY_SCPI_CMD(cmd, tx_len);
497 msg->slot = msg->cmd;
499 msg->slot = BIT(SCPI_SLOT);
500 msg->cmd = PACK_SCPI_CMD(cmd, tx_len);
502 msg->tx_buf = tx_buf;
503 msg->tx_len = tx_len;
504 msg->rx_buf = rx_buf;
505 msg->rx_len = rx_len;
506 reinit_completion(&msg->done);
508 ret = mbox_send_message(scpi_chan->chan, msg);
509 if (ret < 0 || !rx_buf)
512 if (!wait_for_completion_timeout(&msg->done, MAX_RX_TIMEOUT))
515 /* first status word */
518 if (ret < 0 && rx_buf) /* remove entry from the list if timed-out */
519 scpi_process_cmd(scpi_chan, msg->cmd);
521 put_scpi_xfer(msg, scpi_chan);
522 /* SCPI error codes > 0, translate them to Linux scale*/
523 return ret > 0 ? scpi_to_linux_errno(ret) : ret;
526 static u32 scpi_get_version(void)
528 return scpi_info->protocol_version;
532 scpi_clk_get_range(u16 clk_id, unsigned long *min, unsigned long *max)
535 struct clk_get_info clk;
536 __le16 le_clk_id = cpu_to_le16(clk_id);
538 ret = scpi_send_message(CMD_GET_CLOCK_INFO, &le_clk_id,
539 sizeof(le_clk_id), &clk, sizeof(clk));
541 *min = le32_to_cpu(clk.min_rate);
542 *max = le32_to_cpu(clk.max_rate);
547 static unsigned long scpi_clk_get_val(u16 clk_id)
551 __le16 le_clk_id = cpu_to_le16(clk_id);
553 ret = scpi_send_message(CMD_GET_CLOCK_VALUE, &le_clk_id,
554 sizeof(le_clk_id), &rate, sizeof(rate));
558 return le32_to_cpu(rate);
561 static int scpi_clk_set_val(u16 clk_id, unsigned long rate)
564 struct clk_set_value clk = {
565 .id = cpu_to_le16(clk_id),
566 .rate = cpu_to_le32(rate)
569 return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
570 &stat, sizeof(stat));
573 static int legacy_scpi_clk_set_val(u16 clk_id, unsigned long rate)
576 struct legacy_clk_set_value clk = {
577 .id = cpu_to_le16(clk_id),
578 .rate = cpu_to_le32(rate)
581 return scpi_send_message(CMD_SET_CLOCK_VALUE, &clk, sizeof(clk),
582 &stat, sizeof(stat));
585 static int scpi_dvfs_get_idx(u8 domain)
590 ret = scpi_send_message(CMD_GET_DVFS, &domain, sizeof(domain),
591 &dvfs_idx, sizeof(dvfs_idx));
593 return ret ? ret : dvfs_idx;
596 static int scpi_dvfs_set_idx(u8 domain, u8 index)
599 struct dvfs_set dvfs = {domain, index};
601 return scpi_send_message(CMD_SET_DVFS, &dvfs, sizeof(dvfs),
602 &stat, sizeof(stat));
605 static int opp_cmp_func(const void *opp1, const void *opp2)
607 const struct scpi_opp *t1 = opp1, *t2 = opp2;
609 return t1->freq - t2->freq;
612 static struct scpi_dvfs_info *scpi_dvfs_get_info(u8 domain)
614 struct scpi_dvfs_info *info;
615 struct scpi_opp *opp;
616 struct dvfs_info buf;
619 if (domain >= MAX_DVFS_DOMAINS)
620 return ERR_PTR(-EINVAL);
622 if (scpi_info->dvfs[domain]) /* data already populated */
623 return scpi_info->dvfs[domain];
625 ret = scpi_send_message(CMD_GET_DVFS_INFO, &domain, sizeof(domain),
630 info = kmalloc(sizeof(*info), GFP_KERNEL);
632 return ERR_PTR(-ENOMEM);
634 info->count = buf.opp_count;
635 info->latency = le16_to_cpu(buf.latency) * 1000; /* uS to nS */
637 info->opps = kcalloc(info->count, sizeof(*opp), GFP_KERNEL);
640 return ERR_PTR(-ENOMEM);
643 for (i = 0, opp = info->opps; i < info->count; i++, opp++) {
644 opp->freq = le32_to_cpu(buf.opps[i].freq);
645 opp->m_volt = le32_to_cpu(buf.opps[i].m_volt);
648 sort(info->opps, info->count, sizeof(*opp), opp_cmp_func, NULL);
650 scpi_info->dvfs[domain] = info;
654 static int scpi_dev_domain_id(struct device *dev)
656 struct of_phandle_args clkspec;
658 if (of_parse_phandle_with_args(dev->of_node, "clocks", "#clock-cells",
662 return clkspec.args[0];
665 static struct scpi_dvfs_info *scpi_dvfs_info(struct device *dev)
667 int domain = scpi_dev_domain_id(dev);
670 return ERR_PTR(domain);
672 return scpi_dvfs_get_info(domain);
675 static int scpi_dvfs_get_transition_latency(struct device *dev)
677 struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
680 return PTR_ERR(info);
682 return info->latency;
685 static int scpi_dvfs_add_opps_to_device(struct device *dev)
688 struct scpi_opp *opp;
689 struct scpi_dvfs_info *info = scpi_dvfs_info(dev);
692 return PTR_ERR(info);
697 for (opp = info->opps, idx = 0; idx < info->count; idx++, opp++) {
698 ret = dev_pm_opp_add(dev, opp->freq, opp->m_volt * 1000);
700 dev_warn(dev, "failed to add opp %uHz %umV\n",
701 opp->freq, opp->m_volt);
703 dev_pm_opp_remove(dev, (--opp)->freq);
710 static int scpi_sensor_get_capability(u16 *sensors)
715 ret = scpi_send_message(CMD_SENSOR_CAPABILITIES, NULL, 0, &cap,
718 *sensors = le16_to_cpu(cap);
723 static int scpi_sensor_get_info(u16 sensor_id, struct scpi_sensor_info *info)
725 __le16 id = cpu_to_le16(sensor_id);
726 struct _scpi_sensor_info _info;
729 ret = scpi_send_message(CMD_SENSOR_INFO, &id, sizeof(id),
730 &_info, sizeof(_info));
732 memcpy(info, &_info, sizeof(*info));
733 info->sensor_id = le16_to_cpu(_info.sensor_id);
739 static int scpi_sensor_get_value(u16 sensor, u64 *val)
741 __le16 id = cpu_to_le16(sensor);
745 ret = scpi_send_message(CMD_SENSOR_VALUE, &id, sizeof(id),
746 &value, sizeof(value));
750 if (scpi_info->is_legacy)
751 /* only 32-bits supported, upper 32 bits can be junk */
752 *val = le32_to_cpup((__le32 *)&value);
754 *val = le64_to_cpu(value);
759 static int scpi_device_get_power_state(u16 dev_id)
763 __le16 id = cpu_to_le16(dev_id);
765 ret = scpi_send_message(CMD_GET_DEVICE_PWR_STATE, &id,
766 sizeof(id), &pstate, sizeof(pstate));
767 return ret ? ret : pstate;
770 static int scpi_device_set_power_state(u16 dev_id, u8 pstate)
773 struct dev_pstate_set dev_set = {
774 .dev_id = cpu_to_le16(dev_id),
778 return scpi_send_message(CMD_SET_DEVICE_PWR_STATE, &dev_set,
779 sizeof(dev_set), &stat, sizeof(stat));
782 static struct scpi_ops scpi_ops = {
783 .get_version = scpi_get_version,
784 .clk_get_range = scpi_clk_get_range,
785 .clk_get_val = scpi_clk_get_val,
786 .clk_set_val = scpi_clk_set_val,
787 .dvfs_get_idx = scpi_dvfs_get_idx,
788 .dvfs_set_idx = scpi_dvfs_set_idx,
789 .dvfs_get_info = scpi_dvfs_get_info,
790 .device_domain_id = scpi_dev_domain_id,
791 .get_transition_latency = scpi_dvfs_get_transition_latency,
792 .add_opps_to_device = scpi_dvfs_add_opps_to_device,
793 .sensor_get_capability = scpi_sensor_get_capability,
794 .sensor_get_info = scpi_sensor_get_info,
795 .sensor_get_value = scpi_sensor_get_value,
796 .device_get_power_state = scpi_device_get_power_state,
797 .device_set_power_state = scpi_device_set_power_state,
800 struct scpi_ops *get_scpi_ops(void)
802 return scpi_info ? scpi_info->scpi_ops : NULL;
804 EXPORT_SYMBOL_GPL(get_scpi_ops);
806 static int scpi_init_versions(struct scpi_drvinfo *info)
809 struct scp_capabilities caps;
811 ret = scpi_send_message(CMD_SCPI_CAPABILITIES, NULL, 0,
812 &caps, sizeof(caps));
814 info->protocol_version = le32_to_cpu(caps.protocol_version);
815 info->firmware_version = le32_to_cpu(caps.platform_version);
817 /* Ignore error if not implemented */
818 if (scpi_info->is_legacy && ret == -EOPNOTSUPP)
824 static ssize_t protocol_version_show(struct device *dev,
825 struct device_attribute *attr, char *buf)
827 struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
829 return sprintf(buf, "%lu.%lu\n",
830 FIELD_GET(PROTO_REV_MAJOR_MASK, scpi_info->protocol_version),
831 FIELD_GET(PROTO_REV_MINOR_MASK, scpi_info->protocol_version));
833 static DEVICE_ATTR_RO(protocol_version);
835 static ssize_t firmware_version_show(struct device *dev,
836 struct device_attribute *attr, char *buf)
838 struct scpi_drvinfo *scpi_info = dev_get_drvdata(dev);
840 return sprintf(buf, "%lu.%lu.%lu\n",
841 FIELD_GET(FW_REV_MAJOR_MASK, scpi_info->firmware_version),
842 FIELD_GET(FW_REV_MINOR_MASK, scpi_info->firmware_version),
843 FIELD_GET(FW_REV_PATCH_MASK, scpi_info->firmware_version));
845 static DEVICE_ATTR_RO(firmware_version);
847 static struct attribute *versions_attrs[] = {
848 &dev_attr_firmware_version.attr,
849 &dev_attr_protocol_version.attr,
852 ATTRIBUTE_GROUPS(versions);
854 static void scpi_free_channels(void *data)
856 struct scpi_drvinfo *info = data;
859 for (i = 0; i < info->num_chans; i++)
860 mbox_free_channel(info->channels[i].chan);
863 static int scpi_remove(struct platform_device *pdev)
866 struct scpi_drvinfo *info = platform_get_drvdata(pdev);
868 scpi_info = NULL; /* stop exporting SCPI ops through get_scpi_ops */
870 for (i = 0; i < MAX_DVFS_DOMAINS && info->dvfs[i]; i++) {
871 kfree(info->dvfs[i]->opps);
872 kfree(info->dvfs[i]);
878 #define MAX_SCPI_XFERS 10
879 static int scpi_alloc_xfer_list(struct device *dev, struct scpi_chan *ch)
882 struct scpi_xfer *xfers;
884 xfers = devm_kcalloc(dev, MAX_SCPI_XFERS, sizeof(*xfers), GFP_KERNEL);
889 for (i = 0; i < MAX_SCPI_XFERS; i++, xfers++) {
890 init_completion(&xfers->done);
891 list_add_tail(&xfers->node, &ch->xfers_list);
897 static const struct of_device_id legacy_scpi_of_match[] = {
898 {.compatible = "arm,scpi-pre-1.0"},
902 static const struct of_device_id shmem_of_match[] __maybe_unused = {
903 { .compatible = "amlogic,meson-gxbb-scp-shmem", },
904 { .compatible = "amlogic,meson-axg-scp-shmem", },
905 { .compatible = "arm,juno-scp-shmem", },
906 { .compatible = "arm,scp-shmem", },
910 static int scpi_probe(struct platform_device *pdev)
914 struct device *dev = &pdev->dev;
915 struct device_node *np = dev->of_node;
917 scpi_info = devm_kzalloc(dev, sizeof(*scpi_info), GFP_KERNEL);
921 if (of_match_device(legacy_scpi_of_match, &pdev->dev))
922 scpi_info->is_legacy = true;
924 count = of_count_phandle_with_args(np, "mboxes", "#mbox-cells");
926 dev_err(dev, "no mboxes property in '%pOF'\n", np);
930 scpi_info->channels = devm_kcalloc(dev, count, sizeof(struct scpi_chan),
932 if (!scpi_info->channels)
935 ret = devm_add_action(dev, scpi_free_channels, scpi_info);
939 for (; scpi_info->num_chans < count; scpi_info->num_chans++) {
940 resource_size_t size;
941 int idx = scpi_info->num_chans;
942 struct scpi_chan *pchan = scpi_info->channels + idx;
943 struct mbox_client *cl = &pchan->cl;
944 struct device_node *shmem = of_parse_phandle(np, "shmem", idx);
946 if (!of_match_node(shmem_of_match, shmem))
949 ret = of_address_to_resource(shmem, 0, &res);
952 dev_err(dev, "failed to get SCPI payload mem resource\n");
956 size = resource_size(&res);
957 pchan->rx_payload = devm_ioremap(dev, res.start, size);
958 if (!pchan->rx_payload) {
959 dev_err(dev, "failed to ioremap SCPI payload\n");
960 return -EADDRNOTAVAIL;
962 pchan->tx_payload = pchan->rx_payload + (size >> 1);
965 cl->rx_callback = scpi_handle_remote_msg;
966 cl->tx_prepare = scpi_tx_prepare;
969 cl->knows_txdone = false; /* controller can't ack */
971 INIT_LIST_HEAD(&pchan->rx_pending);
972 INIT_LIST_HEAD(&pchan->xfers_list);
973 spin_lock_init(&pchan->rx_lock);
974 mutex_init(&pchan->xfers_lock);
976 ret = scpi_alloc_xfer_list(dev, pchan);
978 pchan->chan = mbox_request_channel(cl, idx);
979 if (!IS_ERR(pchan->chan))
981 ret = PTR_ERR(pchan->chan);
982 if (ret != -EPROBE_DEFER)
983 dev_err(dev, "failed to get channel%d err %d\n",
989 scpi_info->commands = scpi_std_commands;
991 platform_set_drvdata(pdev, scpi_info);
993 if (scpi_info->is_legacy) {
994 /* Replace with legacy variants */
995 scpi_ops.clk_set_val = legacy_scpi_clk_set_val;
996 scpi_info->commands = scpi_legacy_commands;
998 /* Fill priority bitmap */
999 for (idx = 0; idx < ARRAY_SIZE(legacy_hpriority_cmds); idx++)
1000 set_bit(legacy_hpriority_cmds[idx],
1001 scpi_info->cmd_priority);
1004 ret = scpi_init_versions(scpi_info);
1006 dev_err(dev, "incorrect or no SCP firmware found\n");
1010 if (scpi_info->is_legacy && !scpi_info->protocol_version &&
1011 !scpi_info->firmware_version)
1012 dev_info(dev, "SCP Protocol legacy pre-1.0 firmware\n");
1014 dev_info(dev, "SCP Protocol %lu.%lu Firmware %lu.%lu.%lu version\n",
1015 FIELD_GET(PROTO_REV_MAJOR_MASK,
1016 scpi_info->protocol_version),
1017 FIELD_GET(PROTO_REV_MINOR_MASK,
1018 scpi_info->protocol_version),
1019 FIELD_GET(FW_REV_MAJOR_MASK,
1020 scpi_info->firmware_version),
1021 FIELD_GET(FW_REV_MINOR_MASK,
1022 scpi_info->firmware_version),
1023 FIELD_GET(FW_REV_PATCH_MASK,
1024 scpi_info->firmware_version));
1025 scpi_info->scpi_ops = &scpi_ops;
1027 return devm_of_platform_populate(dev);
1030 static const struct of_device_id scpi_of_match[] = {
1031 {.compatible = "arm,scpi"},
1032 {.compatible = "arm,scpi-pre-1.0"},
1036 MODULE_DEVICE_TABLE(of, scpi_of_match);
1038 static struct platform_driver scpi_driver = {
1040 .name = "scpi_protocol",
1041 .of_match_table = scpi_of_match,
1042 .dev_groups = versions_groups,
1044 .probe = scpi_probe,
1045 .remove = scpi_remove,
1047 module_platform_driver(scpi_driver);
1049 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
1050 MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
1051 MODULE_LICENSE("GPL v2");