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/scpi_protocol.h>
43 #include <linux/slab.h>
44 #include <linux/sort.h>
45 #include <linux/spinlock.h>
47 #define CMD_ID_SHIFT 0
48 #define CMD_ID_MASK 0x7f
49 #define CMD_TOKEN_ID_SHIFT 8
50 #define CMD_TOKEN_ID_MASK 0xff
51 #define CMD_DATA_SIZE_SHIFT 16
52 #define CMD_DATA_SIZE_MASK 0x1ff
53 #define PACK_SCPI_CMD(cmd_id, tx_sz) \
54 ((((cmd_id) & CMD_ID_MASK) << CMD_ID_SHIFT) | \
55 (((tx_sz) & CMD_DATA_SIZE_MASK) << CMD_DATA_SIZE_SHIFT))
56 #define ADD_SCPI_TOKEN(cmd, token) \
57 ((cmd) |= (((token) & CMD_TOKEN_ID_MASK) << CMD_TOKEN_ID_SHIFT))
59 #define CMD_SIZE(cmd) (((cmd) >> CMD_DATA_SIZE_SHIFT) & CMD_DATA_SIZE_MASK)
60 #define CMD_UNIQ_MASK (CMD_TOKEN_ID_MASK << CMD_TOKEN_ID_SHIFT | CMD_ID_MASK)
61 #define CMD_XTRACT_UNIQ(cmd) ((cmd) & CMD_UNIQ_MASK)
65 #define MAX_DVFS_DOMAINS 8
66 #define MAX_DVFS_OPPS 8
67 #define DVFS_LATENCY(hdr) (le32_to_cpu(hdr) >> 16)
68 #define DVFS_OPP_COUNT(hdr) ((le32_to_cpu(hdr) >> 8) & 0xff)
70 #define PROTOCOL_REV_MINOR_BITS 16
71 #define PROTOCOL_REV_MINOR_MASK ((1U << PROTOCOL_REV_MINOR_BITS) - 1)
72 #define PROTOCOL_REV_MAJOR(x) ((x) >> PROTOCOL_REV_MINOR_BITS)
73 #define PROTOCOL_REV_MINOR(x) ((x) & PROTOCOL_REV_MINOR_MASK)
75 #define FW_REV_MAJOR_BITS 24
76 #define FW_REV_MINOR_BITS 16
77 #define FW_REV_PATCH_MASK ((1U << FW_REV_MINOR_BITS) - 1)
78 #define FW_REV_MINOR_MASK ((1U << FW_REV_MAJOR_BITS) - 1)
79 #define FW_REV_MAJOR(x) ((x) >> FW_REV_MAJOR_BITS)
80 #define FW_REV_MINOR(x) (((x) & FW_REV_MINOR_MASK) >> FW_REV_MINOR_BITS)
81 #define FW_REV_PATCH(x) ((x) & FW_REV_PATCH_MASK)
83 #define MAX_RX_TIMEOUT (msecs_to_jiffies(30))
85 enum scpi_error_codes
{
86 SCPI_SUCCESS
= 0, /* Success */
87 SCPI_ERR_PARAM
= 1, /* Invalid parameter(s) */
88 SCPI_ERR_ALIGN
= 2, /* Invalid alignment */
89 SCPI_ERR_SIZE
= 3, /* Invalid size */
90 SCPI_ERR_HANDLER
= 4, /* Invalid handler/callback */
91 SCPI_ERR_ACCESS
= 5, /* Invalid access/permission denied */
92 SCPI_ERR_RANGE
= 6, /* Value out of range */
93 SCPI_ERR_TIMEOUT
= 7, /* Timeout has occurred */
94 SCPI_ERR_NOMEM
= 8, /* Invalid memory area or pointer */
95 SCPI_ERR_PWRSTATE
= 9, /* Invalid power state */
96 SCPI_ERR_SUPPORT
= 10, /* Not supported or disabled */
97 SCPI_ERR_DEVICE
= 11, /* Device error */
98 SCPI_ERR_BUSY
= 12, /* Device busy */
103 SCPI_CMD_INVALID
= 0x00,
104 SCPI_CMD_SCPI_READY
= 0x01,
105 SCPI_CMD_SCPI_CAPABILITIES
= 0x02,
106 SCPI_CMD_SET_CSS_PWR_STATE
= 0x03,
107 SCPI_CMD_GET_CSS_PWR_STATE
= 0x04,
108 SCPI_CMD_SET_SYS_PWR_STATE
= 0x05,
109 SCPI_CMD_SET_CPU_TIMER
= 0x06,
110 SCPI_CMD_CANCEL_CPU_TIMER
= 0x07,
111 SCPI_CMD_DVFS_CAPABILITIES
= 0x08,
112 SCPI_CMD_GET_DVFS_INFO
= 0x09,
113 SCPI_CMD_SET_DVFS
= 0x0a,
114 SCPI_CMD_GET_DVFS
= 0x0b,
115 SCPI_CMD_GET_DVFS_STAT
= 0x0c,
116 SCPI_CMD_CLOCK_CAPABILITIES
= 0x0d,
117 SCPI_CMD_GET_CLOCK_INFO
= 0x0e,
118 SCPI_CMD_SET_CLOCK_VALUE
= 0x0f,
119 SCPI_CMD_GET_CLOCK_VALUE
= 0x10,
120 SCPI_CMD_PSU_CAPABILITIES
= 0x11,
121 SCPI_CMD_GET_PSU_INFO
= 0x12,
122 SCPI_CMD_SET_PSU
= 0x13,
123 SCPI_CMD_GET_PSU
= 0x14,
124 SCPI_CMD_SENSOR_CAPABILITIES
= 0x15,
125 SCPI_CMD_SENSOR_INFO
= 0x16,
126 SCPI_CMD_SENSOR_VALUE
= 0x17,
127 SCPI_CMD_SENSOR_CFG_PERIODIC
= 0x18,
128 SCPI_CMD_SENSOR_CFG_BOUNDS
= 0x19,
129 SCPI_CMD_SENSOR_ASYNC_VALUE
= 0x1a,
130 SCPI_CMD_SET_DEVICE_PWR_STATE
= 0x1b,
131 SCPI_CMD_GET_DEVICE_PWR_STATE
= 0x1c,
136 u32 slot
; /* has to be first element */
143 struct list_head node
;
144 struct completion done
;
148 struct mbox_client cl
;
149 struct mbox_chan
*chan
;
150 void __iomem
*tx_payload
;
151 void __iomem
*rx_payload
;
152 struct list_head rx_pending
;
153 struct list_head xfers_list
;
154 struct scpi_xfer
*xfers
;
155 spinlock_t rx_lock
; /* locking for the rx pending list */
156 struct mutex xfers_lock
;
160 struct scpi_drvinfo
{
161 u32 protocol_version
;
162 u32 firmware_version
;
165 struct scpi_ops
*scpi_ops
;
166 struct scpi_chan
*channels
;
167 struct scpi_dvfs_info
*dvfs
[MAX_DVFS_DOMAINS
];
171 * The SCP firmware only executes in little-endian mode, so any buffers
172 * shared through SCPI should have their contents converted to little-endian
174 struct scpi_shared_mem
{
180 struct scp_capabilities
{
181 __le32 protocol_version
;
182 __le32 event_version
;
183 __le32 platform_version
;
187 struct clk_get_info
{
195 struct clk_get_value
{
199 struct clk_set_value
{
210 } opps
[MAX_DVFS_OPPS
];
218 struct sensor_capabilities
{
222 struct _scpi_sensor_info
{
229 struct sensor_value
{
234 struct dev_pstate_set
{
239 static struct scpi_drvinfo
*scpi_info
;
241 static int scpi_linux_errmap
[SCPI_ERR_MAX
] = {
242 /* better than switch case as long as return value is continuous */
243 0, /* SCPI_SUCCESS */
244 -EINVAL
, /* SCPI_ERR_PARAM */
245 -ENOEXEC
, /* SCPI_ERR_ALIGN */
246 -EMSGSIZE
, /* SCPI_ERR_SIZE */
247 -EINVAL
, /* SCPI_ERR_HANDLER */
248 -EACCES
, /* SCPI_ERR_ACCESS */
249 -ERANGE
, /* SCPI_ERR_RANGE */
250 -ETIMEDOUT
, /* SCPI_ERR_TIMEOUT */
251 -ENOMEM
, /* SCPI_ERR_NOMEM */
252 -EINVAL
, /* SCPI_ERR_PWRSTATE */
253 -EOPNOTSUPP
, /* SCPI_ERR_SUPPORT */
254 -EIO
, /* SCPI_ERR_DEVICE */
255 -EBUSY
, /* SCPI_ERR_BUSY */
258 static inline int scpi_to_linux_errno(int errno
)
260 if (errno
>= SCPI_SUCCESS
&& errno
< SCPI_ERR_MAX
)
261 return scpi_linux_errmap
[errno
];
265 static void scpi_process_cmd(struct scpi_chan
*ch
, u32 cmd
)
268 struct scpi_xfer
*t
, *match
= NULL
;
270 spin_lock_irqsave(&ch
->rx_lock
, flags
);
271 if (list_empty(&ch
->rx_pending
)) {
272 spin_unlock_irqrestore(&ch
->rx_lock
, flags
);
276 list_for_each_entry(t
, &ch
->rx_pending
, node
)
277 if (CMD_XTRACT_UNIQ(t
->cmd
) == CMD_XTRACT_UNIQ(cmd
)) {
282 /* check if wait_for_completion is in progress or timed-out */
283 if (match
&& !completion_done(&match
->done
)) {
284 struct scpi_shared_mem
*mem
= ch
->rx_payload
;
285 unsigned int len
= min(match
->rx_len
, CMD_SIZE(cmd
));
287 match
->status
= le32_to_cpu(mem
->status
);
288 memcpy_fromio(match
->rx_buf
, mem
->payload
, len
);
289 if (match
->rx_len
> len
)
290 memset(match
->rx_buf
+ len
, 0, match
->rx_len
- len
);
291 complete(&match
->done
);
293 spin_unlock_irqrestore(&ch
->rx_lock
, flags
);
296 static void scpi_handle_remote_msg(struct mbox_client
*c
, void *msg
)
298 struct scpi_chan
*ch
= container_of(c
, struct scpi_chan
, cl
);
299 struct scpi_shared_mem
*mem
= ch
->rx_payload
;
300 u32 cmd
= le32_to_cpu(mem
->command
);
302 scpi_process_cmd(ch
, cmd
);
305 static void scpi_tx_prepare(struct mbox_client
*c
, void *msg
)
308 struct scpi_xfer
*t
= msg
;
309 struct scpi_chan
*ch
= container_of(c
, struct scpi_chan
, cl
);
310 struct scpi_shared_mem
*mem
= (struct scpi_shared_mem
*)ch
->tx_payload
;
313 memcpy_toio(mem
->payload
, t
->tx_buf
, t
->tx_len
);
317 ADD_SCPI_TOKEN(t
->cmd
, ch
->token
);
318 spin_lock_irqsave(&ch
->rx_lock
, flags
);
319 list_add_tail(&t
->node
, &ch
->rx_pending
);
320 spin_unlock_irqrestore(&ch
->rx_lock
, flags
);
322 mem
->command
= cpu_to_le32(t
->cmd
);
325 static struct scpi_xfer
*get_scpi_xfer(struct scpi_chan
*ch
)
329 mutex_lock(&ch
->xfers_lock
);
330 if (list_empty(&ch
->xfers_list
)) {
331 mutex_unlock(&ch
->xfers_lock
);
334 t
= list_first_entry(&ch
->xfers_list
, struct scpi_xfer
, node
);
336 mutex_unlock(&ch
->xfers_lock
);
340 static void put_scpi_xfer(struct scpi_xfer
*t
, struct scpi_chan
*ch
)
342 mutex_lock(&ch
->xfers_lock
);
343 list_add_tail(&t
->node
, &ch
->xfers_list
);
344 mutex_unlock(&ch
->xfers_lock
);
347 static int scpi_send_message(u8 cmd
, void *tx_buf
, unsigned int tx_len
,
348 void *rx_buf
, unsigned int rx_len
)
352 struct scpi_xfer
*msg
;
353 struct scpi_chan
*scpi_chan
;
355 chan
= atomic_inc_return(&scpi_info
->next_chan
) % scpi_info
->num_chans
;
356 scpi_chan
= scpi_info
->channels
+ chan
;
358 msg
= get_scpi_xfer(scpi_chan
);
362 msg
->slot
= BIT(SCPI_SLOT
);
363 msg
->cmd
= PACK_SCPI_CMD(cmd
, tx_len
);
364 msg
->tx_buf
= tx_buf
;
365 msg
->tx_len
= tx_len
;
366 msg
->rx_buf
= rx_buf
;
367 msg
->rx_len
= rx_len
;
368 init_completion(&msg
->done
);
370 ret
= mbox_send_message(scpi_chan
->chan
, msg
);
371 if (ret
< 0 || !rx_buf
)
374 if (!wait_for_completion_timeout(&msg
->done
, MAX_RX_TIMEOUT
))
377 /* first status word */
380 if (ret
< 0 && rx_buf
) /* remove entry from the list if timed-out */
381 scpi_process_cmd(scpi_chan
, msg
->cmd
);
383 put_scpi_xfer(msg
, scpi_chan
);
384 /* SCPI error codes > 0, translate them to Linux scale*/
385 return ret
> 0 ? scpi_to_linux_errno(ret
) : ret
;
388 static u32
scpi_get_version(void)
390 return scpi_info
->protocol_version
;
394 scpi_clk_get_range(u16 clk_id
, unsigned long *min
, unsigned long *max
)
397 struct clk_get_info clk
;
398 __le16 le_clk_id
= cpu_to_le16(clk_id
);
400 ret
= scpi_send_message(SCPI_CMD_GET_CLOCK_INFO
, &le_clk_id
,
401 sizeof(le_clk_id
), &clk
, sizeof(clk
));
403 *min
= le32_to_cpu(clk
.min_rate
);
404 *max
= le32_to_cpu(clk
.max_rate
);
409 static unsigned long scpi_clk_get_val(u16 clk_id
)
412 struct clk_get_value clk
;
413 __le16 le_clk_id
= cpu_to_le16(clk_id
);
415 ret
= scpi_send_message(SCPI_CMD_GET_CLOCK_VALUE
, &le_clk_id
,
416 sizeof(le_clk_id
), &clk
, sizeof(clk
));
417 return ret
? ret
: le32_to_cpu(clk
.rate
);
420 static int scpi_clk_set_val(u16 clk_id
, unsigned long rate
)
423 struct clk_set_value clk
= {
424 .id
= cpu_to_le16(clk_id
),
425 .rate
= cpu_to_le32(rate
)
428 return scpi_send_message(SCPI_CMD_SET_CLOCK_VALUE
, &clk
, sizeof(clk
),
429 &stat
, sizeof(stat
));
432 static int scpi_dvfs_get_idx(u8 domain
)
437 ret
= scpi_send_message(SCPI_CMD_GET_DVFS
, &domain
, sizeof(domain
),
438 &dvfs_idx
, sizeof(dvfs_idx
));
439 return ret
? ret
: dvfs_idx
;
442 static int scpi_dvfs_set_idx(u8 domain
, u8 index
)
445 struct dvfs_set dvfs
= {domain
, index
};
447 return scpi_send_message(SCPI_CMD_SET_DVFS
, &dvfs
, sizeof(dvfs
),
448 &stat
, sizeof(stat
));
451 static int opp_cmp_func(const void *opp1
, const void *opp2
)
453 const struct scpi_opp
*t1
= opp1
, *t2
= opp2
;
455 return t1
->freq
- t2
->freq
;
458 static struct scpi_dvfs_info
*scpi_dvfs_get_info(u8 domain
)
460 struct scpi_dvfs_info
*info
;
461 struct scpi_opp
*opp
;
462 struct dvfs_info buf
;
465 if (domain
>= MAX_DVFS_DOMAINS
)
466 return ERR_PTR(-EINVAL
);
468 if (scpi_info
->dvfs
[domain
]) /* data already populated */
469 return scpi_info
->dvfs
[domain
];
471 ret
= scpi_send_message(SCPI_CMD_GET_DVFS_INFO
, &domain
, sizeof(domain
),
477 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
479 return ERR_PTR(-ENOMEM
);
481 info
->count
= DVFS_OPP_COUNT(buf
.header
);
482 info
->latency
= DVFS_LATENCY(buf
.header
) * 1000; /* uS to nS */
484 info
->opps
= kcalloc(info
->count
, sizeof(*opp
), GFP_KERNEL
);
487 return ERR_PTR(-ENOMEM
);
490 for (i
= 0, opp
= info
->opps
; i
< info
->count
; i
++, opp
++) {
491 opp
->freq
= le32_to_cpu(buf
.opps
[i
].freq
);
492 opp
->m_volt
= le32_to_cpu(buf
.opps
[i
].m_volt
);
495 sort(info
->opps
, info
->count
, sizeof(*opp
), opp_cmp_func
, NULL
);
497 scpi_info
->dvfs
[domain
] = info
;
501 static int scpi_sensor_get_capability(u16
*sensors
)
503 struct sensor_capabilities cap_buf
;
506 ret
= scpi_send_message(SCPI_CMD_SENSOR_CAPABILITIES
, NULL
, 0, &cap_buf
,
509 *sensors
= le16_to_cpu(cap_buf
.sensors
);
514 static int scpi_sensor_get_info(u16 sensor_id
, struct scpi_sensor_info
*info
)
516 __le16 id
= cpu_to_le16(sensor_id
);
517 struct _scpi_sensor_info _info
;
520 ret
= scpi_send_message(SCPI_CMD_SENSOR_INFO
, &id
, sizeof(id
),
521 &_info
, sizeof(_info
));
523 memcpy(info
, &_info
, sizeof(*info
));
524 info
->sensor_id
= le16_to_cpu(_info
.sensor_id
);
530 static int scpi_sensor_get_value(u16 sensor
, u64
*val
)
532 __le16 id
= cpu_to_le16(sensor
);
533 struct sensor_value buf
;
536 ret
= scpi_send_message(SCPI_CMD_SENSOR_VALUE
, &id
, sizeof(id
),
539 *val
= (u64
)le32_to_cpu(buf
.hi_val
) << 32 |
540 le32_to_cpu(buf
.lo_val
);
545 static int scpi_device_get_power_state(u16 dev_id
)
549 __le16 id
= cpu_to_le16(dev_id
);
551 ret
= scpi_send_message(SCPI_CMD_GET_DEVICE_PWR_STATE
, &id
,
552 sizeof(id
), &pstate
, sizeof(pstate
));
553 return ret
? ret
: pstate
;
556 static int scpi_device_set_power_state(u16 dev_id
, u8 pstate
)
559 struct dev_pstate_set dev_set
= {
560 .dev_id
= cpu_to_le16(dev_id
),
564 return scpi_send_message(SCPI_CMD_SET_DEVICE_PWR_STATE
, &dev_set
,
565 sizeof(dev_set
), &stat
, sizeof(stat
));
568 static struct scpi_ops scpi_ops
= {
569 .get_version
= scpi_get_version
,
570 .clk_get_range
= scpi_clk_get_range
,
571 .clk_get_val
= scpi_clk_get_val
,
572 .clk_set_val
= scpi_clk_set_val
,
573 .dvfs_get_idx
= scpi_dvfs_get_idx
,
574 .dvfs_set_idx
= scpi_dvfs_set_idx
,
575 .dvfs_get_info
= scpi_dvfs_get_info
,
576 .sensor_get_capability
= scpi_sensor_get_capability
,
577 .sensor_get_info
= scpi_sensor_get_info
,
578 .sensor_get_value
= scpi_sensor_get_value
,
579 .device_get_power_state
= scpi_device_get_power_state
,
580 .device_set_power_state
= scpi_device_set_power_state
,
583 struct scpi_ops
*get_scpi_ops(void)
585 return scpi_info
? scpi_info
->scpi_ops
: NULL
;
587 EXPORT_SYMBOL_GPL(get_scpi_ops
);
589 static int scpi_init_versions(struct scpi_drvinfo
*info
)
592 struct scp_capabilities caps
;
594 ret
= scpi_send_message(SCPI_CMD_SCPI_CAPABILITIES
, NULL
, 0,
595 &caps
, sizeof(caps
));
597 info
->protocol_version
= le32_to_cpu(caps
.protocol_version
);
598 info
->firmware_version
= le32_to_cpu(caps
.platform_version
);
603 static ssize_t
protocol_version_show(struct device
*dev
,
604 struct device_attribute
*attr
, char *buf
)
606 struct scpi_drvinfo
*scpi_info
= dev_get_drvdata(dev
);
608 return sprintf(buf
, "%d.%d\n",
609 PROTOCOL_REV_MAJOR(scpi_info
->protocol_version
),
610 PROTOCOL_REV_MINOR(scpi_info
->protocol_version
));
612 static DEVICE_ATTR_RO(protocol_version
);
614 static ssize_t
firmware_version_show(struct device
*dev
,
615 struct device_attribute
*attr
, char *buf
)
617 struct scpi_drvinfo
*scpi_info
= dev_get_drvdata(dev
);
619 return sprintf(buf
, "%d.%d.%d\n",
620 FW_REV_MAJOR(scpi_info
->firmware_version
),
621 FW_REV_MINOR(scpi_info
->firmware_version
),
622 FW_REV_PATCH(scpi_info
->firmware_version
));
624 static DEVICE_ATTR_RO(firmware_version
);
626 static struct attribute
*versions_attrs
[] = {
627 &dev_attr_firmware_version
.attr
,
628 &dev_attr_protocol_version
.attr
,
631 ATTRIBUTE_GROUPS(versions
);
634 scpi_free_channels(struct device
*dev
, struct scpi_chan
*pchan
, int count
)
638 for (i
= 0; i
< count
&& pchan
->chan
; i
++, pchan
++) {
639 mbox_free_channel(pchan
->chan
);
640 devm_kfree(dev
, pchan
->xfers
);
641 devm_iounmap(dev
, pchan
->rx_payload
);
645 static int scpi_remove(struct platform_device
*pdev
)
648 struct device
*dev
= &pdev
->dev
;
649 struct scpi_drvinfo
*info
= platform_get_drvdata(pdev
);
651 scpi_info
= NULL
; /* stop exporting SCPI ops through get_scpi_ops */
653 of_platform_depopulate(dev
);
654 sysfs_remove_groups(&dev
->kobj
, versions_groups
);
655 scpi_free_channels(dev
, info
->channels
, info
->num_chans
);
656 platform_set_drvdata(pdev
, NULL
);
658 for (i
= 0; i
< MAX_DVFS_DOMAINS
&& info
->dvfs
[i
]; i
++) {
659 kfree(info
->dvfs
[i
]->opps
);
660 kfree(info
->dvfs
[i
]);
662 devm_kfree(dev
, info
->channels
);
663 devm_kfree(dev
, info
);
668 #define MAX_SCPI_XFERS 10
669 static int scpi_alloc_xfer_list(struct device
*dev
, struct scpi_chan
*ch
)
672 struct scpi_xfer
*xfers
;
674 xfers
= devm_kzalloc(dev
, MAX_SCPI_XFERS
* sizeof(*xfers
), GFP_KERNEL
);
679 for (i
= 0; i
< MAX_SCPI_XFERS
; i
++, xfers
++)
680 list_add_tail(&xfers
->node
, &ch
->xfers_list
);
684 static int scpi_probe(struct platform_device
*pdev
)
688 struct scpi_chan
*scpi_chan
;
689 struct device
*dev
= &pdev
->dev
;
690 struct device_node
*np
= dev
->of_node
;
692 scpi_info
= devm_kzalloc(dev
, sizeof(*scpi_info
), GFP_KERNEL
);
696 count
= of_count_phandle_with_args(np
, "mboxes", "#mbox-cells");
698 dev_err(dev
, "no mboxes property in '%s'\n", np
->full_name
);
702 scpi_chan
= devm_kcalloc(dev
, count
, sizeof(*scpi_chan
), GFP_KERNEL
);
706 for (idx
= 0; idx
< count
; idx
++) {
707 resource_size_t size
;
708 struct scpi_chan
*pchan
= scpi_chan
+ idx
;
709 struct mbox_client
*cl
= &pchan
->cl
;
710 struct device_node
*shmem
= of_parse_phandle(np
, "shmem", idx
);
712 ret
= of_address_to_resource(shmem
, 0, &res
);
715 dev_err(dev
, "failed to get SCPI payload mem resource\n");
719 size
= resource_size(&res
);
720 pchan
->rx_payload
= devm_ioremap(dev
, res
.start
, size
);
721 if (!pchan
->rx_payload
) {
722 dev_err(dev
, "failed to ioremap SCPI payload\n");
723 ret
= -EADDRNOTAVAIL
;
726 pchan
->tx_payload
= pchan
->rx_payload
+ (size
>> 1);
729 cl
->rx_callback
= scpi_handle_remote_msg
;
730 cl
->tx_prepare
= scpi_tx_prepare
;
733 cl
->knows_txdone
= false; /* controller can't ack */
735 INIT_LIST_HEAD(&pchan
->rx_pending
);
736 INIT_LIST_HEAD(&pchan
->xfers_list
);
737 spin_lock_init(&pchan
->rx_lock
);
738 mutex_init(&pchan
->xfers_lock
);
740 ret
= scpi_alloc_xfer_list(dev
, pchan
);
742 pchan
->chan
= mbox_request_channel(cl
, idx
);
743 if (!IS_ERR(pchan
->chan
))
745 ret
= PTR_ERR(pchan
->chan
);
746 if (ret
!= -EPROBE_DEFER
)
747 dev_err(dev
, "failed to get channel%d err %d\n",
751 scpi_free_channels(dev
, scpi_chan
, idx
);
756 scpi_info
->channels
= scpi_chan
;
757 scpi_info
->num_chans
= count
;
758 platform_set_drvdata(pdev
, scpi_info
);
760 ret
= scpi_init_versions(scpi_info
);
762 dev_err(dev
, "incorrect or no SCP firmware found\n");
767 _dev_info(dev
, "SCP Protocol %d.%d Firmware %d.%d.%d version\n",
768 PROTOCOL_REV_MAJOR(scpi_info
->protocol_version
),
769 PROTOCOL_REV_MINOR(scpi_info
->protocol_version
),
770 FW_REV_MAJOR(scpi_info
->firmware_version
),
771 FW_REV_MINOR(scpi_info
->firmware_version
),
772 FW_REV_PATCH(scpi_info
->firmware_version
));
773 scpi_info
->scpi_ops
= &scpi_ops
;
775 ret
= sysfs_create_groups(&dev
->kobj
, versions_groups
);
777 dev_err(dev
, "unable to create sysfs version group\n");
779 return of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
782 static const struct of_device_id scpi_of_match
[] = {
783 {.compatible
= "arm,scpi"},
787 MODULE_DEVICE_TABLE(of
, scpi_of_match
);
789 static struct platform_driver scpi_driver
= {
791 .name
= "scpi_protocol",
792 .of_match_table
= scpi_of_match
,
795 .remove
= scpi_remove
,
797 module_platform_driver(scpi_driver
);
799 MODULE_AUTHOR("Sudeep Holla <sudeep.holla@arm.com>");
800 MODULE_DESCRIPTION("ARM SCPI mailbox protocol driver");
801 MODULE_LICENSE("GPL v2");