1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments System Control Interface Protocol Driver
5 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
9 #define pr_fmt(fmt) "%s: " fmt, __func__
11 #include <linux/bitmap.h>
12 #include <linux/debugfs.h>
13 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/mailbox_client.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/semaphore.h>
20 #include <linux/slab.h>
21 #include <linux/soc/ti/ti-msgmgr.h>
22 #include <linux/soc/ti/ti_sci_protocol.h>
23 #include <linux/reboot.h>
27 /* List of all TI SCI devices active in system */
28 static LIST_HEAD(ti_sci_list
);
29 /* Protection for the entire list */
30 static DEFINE_MUTEX(ti_sci_list_mutex
);
33 * struct ti_sci_xfer - Structure representing a message flow
34 * @tx_message: Transmit message
35 * @rx_len: Receive message length
36 * @xfer_buf: Preallocated buffer to store receive message
37 * Since we work with request-ACK protocol, we can
38 * reuse the same buffer for the rx path as we
39 * use for the tx path.
40 * @done: completion event
43 struct ti_msgmgr_message tx_message
;
46 struct completion done
;
50 * struct ti_sci_xfers_info - Structure to manage transfer information
51 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
53 * @xfer_block: Preallocated Message array
54 * @xfer_alloc_table: Bitmap table for allocated messages.
55 * Index of this bitmap table is also used for message
56 * sequence identifier.
57 * @xfer_lock: Protection for message allocation
59 struct ti_sci_xfers_info
{
60 struct semaphore sem_xfer_count
;
61 struct ti_sci_xfer
*xfer_block
;
62 unsigned long *xfer_alloc_table
;
63 /* protect transfer allocation */
68 * struct ti_sci_desc - Description of SoC integration
69 * @default_host_id: Host identifier representing the compute entity
70 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
71 * @max_msgs: Maximum number of messages that can be pending
72 * simultaneously in the system
73 * @max_msg_size: Maximum size of data per message that can be handled.
77 int max_rx_timeout_ms
;
83 * struct ti_sci_info - Structure representing a TI SCI instance
84 * @dev: Device pointer
85 * @desc: SoC description for this instance
86 * @nb: Reboot Notifier block
87 * @d: Debugfs file entry
88 * @debug_region: Memory region where the debug message are available
89 * @debug_region_size: Debug region size
90 * @debug_buffer: Buffer allocated to copy debug messages.
91 * @handle: Instance of TI SCI handle to send to clients.
93 * @chan_tx: Transmit mailbox channel
94 * @chan_rx: Receive mailbox channel
95 * @minfo: Message info
98 * @users: Number of users of this instance
102 struct notifier_block nb
;
103 const struct ti_sci_desc
*desc
;
105 void __iomem
*debug_region
;
107 size_t debug_region_size
;
108 struct ti_sci_handle handle
;
109 struct mbox_client cl
;
110 struct mbox_chan
*chan_tx
;
111 struct mbox_chan
*chan_rx
;
112 struct ti_sci_xfers_info minfo
;
113 struct list_head node
;
115 /* protected by ti_sci_list_mutex */
120 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
121 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
122 #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
124 #ifdef CONFIG_DEBUG_FS
127 * ti_sci_debug_show() - Helper to dump the debug log
128 * @s: sequence file pointer
133 static int ti_sci_debug_show(struct seq_file
*s
, void *unused
)
135 struct ti_sci_info
*info
= s
->private;
137 memcpy_fromio(info
->debug_buffer
, info
->debug_region
,
138 info
->debug_region_size
);
140 * We don't trust firmware to leave NULL terminated last byte (hence
141 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
142 * specific data format for debug messages, We just present the data
143 * in the buffer as is - we expect the messages to be self explanatory.
145 seq_puts(s
, info
->debug_buffer
);
149 /* Provide the log file operations interface*/
150 DEFINE_SHOW_ATTRIBUTE(ti_sci_debug
);
153 * ti_sci_debugfs_create() - Create log debug file
154 * @pdev: platform device pointer
155 * @info: Pointer to SCI entity information
157 * Return: 0 if all went fine, else corresponding error.
159 static int ti_sci_debugfs_create(struct platform_device
*pdev
,
160 struct ti_sci_info
*info
)
162 struct device
*dev
= &pdev
->dev
;
163 struct resource
*res
;
164 char debug_name
[50] = "ti_sci_debug@";
166 /* Debug region is optional */
167 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
169 info
->debug_region
= devm_ioremap_resource(dev
, res
);
170 if (IS_ERR(info
->debug_region
))
172 info
->debug_region_size
= resource_size(res
);
174 info
->debug_buffer
= devm_kcalloc(dev
, info
->debug_region_size
+ 1,
175 sizeof(char), GFP_KERNEL
);
176 if (!info
->debug_buffer
)
178 /* Setup NULL termination */
179 info
->debug_buffer
[info
->debug_region_size
] = 0;
181 info
->d
= debugfs_create_file(strncat(debug_name
, dev_name(dev
),
183 sizeof("ti_sci_debug@")),
184 0444, NULL
, info
, &ti_sci_debug_fops
);
186 return PTR_ERR(info
->d
);
188 dev_dbg(dev
, "Debug region => %p, size = %zu bytes, resource: %pr\n",
189 info
->debug_region
, info
->debug_region_size
, res
);
194 * ti_sci_debugfs_destroy() - clean up log debug file
195 * @pdev: platform device pointer
196 * @info: Pointer to SCI entity information
198 static void ti_sci_debugfs_destroy(struct platform_device
*pdev
,
199 struct ti_sci_info
*info
)
201 if (IS_ERR(info
->debug_region
))
204 debugfs_remove(info
->d
);
206 #else /* CONFIG_DEBUG_FS */
207 static inline int ti_sci_debugfs_create(struct platform_device
*dev
,
208 struct ti_sci_info
*info
)
213 static inline void ti_sci_debugfs_destroy(struct platform_device
*dev
,
214 struct ti_sci_info
*info
)
217 #endif /* CONFIG_DEBUG_FS */
220 * ti_sci_dump_header_dbg() - Helper to dump a message header.
221 * @dev: Device pointer corresponding to the SCI entity
222 * @hdr: pointer to header.
224 static inline void ti_sci_dump_header_dbg(struct device
*dev
,
225 struct ti_sci_msg_hdr
*hdr
)
227 dev_dbg(dev
, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
228 hdr
->type
, hdr
->host
, hdr
->seq
, hdr
->flags
);
232 * ti_sci_rx_callback() - mailbox client callback for receive messages
233 * @cl: client pointer
234 * @m: mailbox message
236 * Processes one received message to appropriate transfer information and
237 * signals completion of the transfer.
239 * NOTE: This function will be invoked in IRQ context, hence should be
240 * as optimal as possible.
242 static void ti_sci_rx_callback(struct mbox_client
*cl
, void *m
)
244 struct ti_sci_info
*info
= cl_to_ti_sci_info(cl
);
245 struct device
*dev
= info
->dev
;
246 struct ti_sci_xfers_info
*minfo
= &info
->minfo
;
247 struct ti_msgmgr_message
*mbox_msg
= m
;
248 struct ti_sci_msg_hdr
*hdr
= (struct ti_sci_msg_hdr
*)mbox_msg
->buf
;
249 struct ti_sci_xfer
*xfer
;
255 * Are we even expecting this?
256 * NOTE: barriers were implicit in locks used for modifying the bitmap
258 if (!test_bit(xfer_id
, minfo
->xfer_alloc_table
)) {
259 dev_err(dev
, "Message for %d is not expected!\n", xfer_id
);
263 xfer
= &minfo
->xfer_block
[xfer_id
];
265 /* Is the message of valid length? */
266 if (mbox_msg
->len
> info
->desc
->max_msg_size
) {
267 dev_err(dev
, "Unable to handle %zu xfer(max %d)\n",
268 mbox_msg
->len
, info
->desc
->max_msg_size
);
269 ti_sci_dump_header_dbg(dev
, hdr
);
272 if (mbox_msg
->len
< xfer
->rx_len
) {
273 dev_err(dev
, "Recv xfer %zu < expected %d length\n",
274 mbox_msg
->len
, xfer
->rx_len
);
275 ti_sci_dump_header_dbg(dev
, hdr
);
279 ti_sci_dump_header_dbg(dev
, hdr
);
280 /* Take a copy to the rx buffer.. */
281 memcpy(xfer
->xfer_buf
, mbox_msg
->buf
, xfer
->rx_len
);
282 complete(&xfer
->done
);
286 * ti_sci_get_one_xfer() - Allocate one message
287 * @info: Pointer to SCI entity information
288 * @msg_type: Message type
289 * @msg_flags: Flag to set for the message
290 * @tx_message_size: transmit message size
291 * @rx_message_size: receive message size
293 * Helper function which is used by various command functions that are
294 * exposed to clients of this driver for allocating a message traffic event.
296 * This function can sleep depending on pending requests already in the system
297 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
298 * of internal data structures.
300 * Return: 0 if all went fine, else corresponding error.
302 static struct ti_sci_xfer
*ti_sci_get_one_xfer(struct ti_sci_info
*info
,
303 u16 msg_type
, u32 msg_flags
,
304 size_t tx_message_size
,
305 size_t rx_message_size
)
307 struct ti_sci_xfers_info
*minfo
= &info
->minfo
;
308 struct ti_sci_xfer
*xfer
;
309 struct ti_sci_msg_hdr
*hdr
;
311 unsigned long bit_pos
;
316 /* Ensure we have sane transfer sizes */
317 if (rx_message_size
> info
->desc
->max_msg_size
||
318 tx_message_size
> info
->desc
->max_msg_size
||
319 rx_message_size
< sizeof(*hdr
) || tx_message_size
< sizeof(*hdr
))
320 return ERR_PTR(-ERANGE
);
323 * Ensure we have only controlled number of pending messages.
324 * Ideally, we might just have to wait a single message, be
325 * conservative and wait 5 times that..
327 timeout
= msecs_to_jiffies(info
->desc
->max_rx_timeout_ms
) * 5;
328 ret
= down_timeout(&minfo
->sem_xfer_count
, timeout
);
332 /* Keep the locked section as small as possible */
333 spin_lock_irqsave(&minfo
->xfer_lock
, flags
);
334 bit_pos
= find_first_zero_bit(minfo
->xfer_alloc_table
,
335 info
->desc
->max_msgs
);
336 set_bit(bit_pos
, minfo
->xfer_alloc_table
);
337 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
340 * We already ensured in probe that we can have max messages that can
341 * fit in hdr.seq - NOTE: this improves access latencies
342 * to predictable O(1) access, BUT, it opens us to risk if
343 * remote misbehaves with corrupted message sequence responses.
344 * If that happens, we are going to be messed up anyways..
346 xfer_id
= (u8
)bit_pos
;
348 xfer
= &minfo
->xfer_block
[xfer_id
];
350 hdr
= (struct ti_sci_msg_hdr
*)xfer
->tx_message
.buf
;
351 xfer
->tx_message
.len
= tx_message_size
;
352 xfer
->rx_len
= (u8
)rx_message_size
;
354 reinit_completion(&xfer
->done
);
357 hdr
->type
= msg_type
;
358 hdr
->host
= info
->host_id
;
359 hdr
->flags
= msg_flags
;
365 * ti_sci_put_one_xfer() - Release a message
366 * @minfo: transfer info pointer
367 * @xfer: message that was reserved by ti_sci_get_one_xfer
369 * This holds a spinlock to maintain integrity of internal data structures.
371 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info
*minfo
,
372 struct ti_sci_xfer
*xfer
)
375 struct ti_sci_msg_hdr
*hdr
;
378 hdr
= (struct ti_sci_msg_hdr
*)xfer
->tx_message
.buf
;
382 * Keep the locked section as small as possible
383 * NOTE: we might escape with smp_mb and no lock here..
384 * but just be conservative and symmetric.
386 spin_lock_irqsave(&minfo
->xfer_lock
, flags
);
387 clear_bit(xfer_id
, minfo
->xfer_alloc_table
);
388 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
390 /* Increment the count for the next user to get through */
391 up(&minfo
->sem_xfer_count
);
395 * ti_sci_do_xfer() - Do one transfer
396 * @info: Pointer to SCI entity information
397 * @xfer: Transfer to initiate and wait for response
399 * Return: -ETIMEDOUT in case of no response, if transmit error,
400 * return corresponding error, else if all goes well,
403 static inline int ti_sci_do_xfer(struct ti_sci_info
*info
,
404 struct ti_sci_xfer
*xfer
)
408 struct device
*dev
= info
->dev
;
410 ret
= mbox_send_message(info
->chan_tx
, &xfer
->tx_message
);
416 /* And we wait for the response. */
417 timeout
= msecs_to_jiffies(info
->desc
->max_rx_timeout_ms
);
418 if (!wait_for_completion_timeout(&xfer
->done
, timeout
)) {
419 dev_err(dev
, "Mbox timedout in resp(caller: %pS)\n",
424 * NOTE: we might prefer not to need the mailbox ticker to manage the
425 * transfer queueing since the protocol layer queues things by itself.
426 * Unfortunately, we have to kick the mailbox framework after we have
427 * received our message.
429 mbox_client_txdone(info
->chan_tx
, ret
);
435 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
436 * @info: Pointer to SCI entity information
438 * Updates the SCI information in the internal data structure.
440 * Return: 0 if all went fine, else return appropriate error.
442 static int ti_sci_cmd_get_revision(struct ti_sci_info
*info
)
444 struct device
*dev
= info
->dev
;
445 struct ti_sci_handle
*handle
= &info
->handle
;
446 struct ti_sci_version_info
*ver
= &handle
->version
;
447 struct ti_sci_msg_resp_version
*rev_info
;
448 struct ti_sci_xfer
*xfer
;
451 /* No need to setup flags since it is expected to respond */
452 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_VERSION
,
453 0x0, sizeof(struct ti_sci_msg_hdr
),
457 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
461 rev_info
= (struct ti_sci_msg_resp_version
*)xfer
->xfer_buf
;
463 ret
= ti_sci_do_xfer(info
, xfer
);
465 dev_err(dev
, "Mbox send fail %d\n", ret
);
469 ver
->abi_major
= rev_info
->abi_major
;
470 ver
->abi_minor
= rev_info
->abi_minor
;
471 ver
->firmware_revision
= rev_info
->firmware_revision
;
472 strncpy(ver
->firmware_description
, rev_info
->firmware_description
,
473 sizeof(ver
->firmware_description
));
476 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
481 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
482 * @r: pointer to response buffer
484 * Return: true if the response was an ACK, else returns false.
486 static inline bool ti_sci_is_response_ack(void *r
)
488 struct ti_sci_msg_hdr
*hdr
= r
;
490 return hdr
->flags
& TI_SCI_FLAG_RESP_GENERIC_ACK
? true : false;
494 * ti_sci_set_device_state() - Set device state helper
495 * @handle: pointer to TI SCI handle
496 * @id: Device identifier
497 * @flags: flags to setup for the device
498 * @state: State to move the device to
500 * Return: 0 if all went well, else returns appropriate error value.
502 static int ti_sci_set_device_state(const struct ti_sci_handle
*handle
,
503 u32 id
, u32 flags
, u8 state
)
505 struct ti_sci_info
*info
;
506 struct ti_sci_msg_req_set_device_state
*req
;
507 struct ti_sci_msg_hdr
*resp
;
508 struct ti_sci_xfer
*xfer
;
513 return PTR_ERR(handle
);
517 info
= handle_to_ti_sci_info(handle
);
520 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_DEVICE_STATE
,
521 flags
| TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
522 sizeof(*req
), sizeof(*resp
));
525 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
528 req
= (struct ti_sci_msg_req_set_device_state
*)xfer
->xfer_buf
;
532 ret
= ti_sci_do_xfer(info
, xfer
);
534 dev_err(dev
, "Mbox send fail %d\n", ret
);
538 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
540 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
543 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
549 * ti_sci_get_device_state() - Get device state helper
550 * @handle: Handle to the device
551 * @id: Device Identifier
552 * @clcnt: Pointer to Context Loss Count
553 * @resets: pointer to resets
554 * @p_state: pointer to p_state
555 * @c_state: pointer to c_state
557 * Return: 0 if all went fine, else return appropriate error.
559 static int ti_sci_get_device_state(const struct ti_sci_handle
*handle
,
560 u32 id
, u32
*clcnt
, u32
*resets
,
561 u8
*p_state
, u8
*c_state
)
563 struct ti_sci_info
*info
;
564 struct ti_sci_msg_req_get_device_state
*req
;
565 struct ti_sci_msg_resp_get_device_state
*resp
;
566 struct ti_sci_xfer
*xfer
;
571 return PTR_ERR(handle
);
575 if (!clcnt
&& !resets
&& !p_state
&& !c_state
)
578 info
= handle_to_ti_sci_info(handle
);
581 /* Response is expected, so need of any flags */
582 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_DEVICE_STATE
,
583 0, sizeof(*req
), sizeof(*resp
));
586 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
589 req
= (struct ti_sci_msg_req_get_device_state
*)xfer
->xfer_buf
;
592 ret
= ti_sci_do_xfer(info
, xfer
);
594 dev_err(dev
, "Mbox send fail %d\n", ret
);
598 resp
= (struct ti_sci_msg_resp_get_device_state
*)xfer
->xfer_buf
;
599 if (!ti_sci_is_response_ack(resp
)) {
605 *clcnt
= resp
->context_loss_count
;
607 *resets
= resp
->resets
;
609 *p_state
= resp
->programmed_state
;
611 *c_state
= resp
->current_state
;
613 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
619 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
620 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
621 * @id: Device Identifier
623 * Request for the device - NOTE: the client MUST maintain integrity of
624 * usage count by balancing get_device with put_device. No refcounting is
625 * managed by driver for that purpose.
627 * NOTE: The request is for exclusive access for the processor.
629 * Return: 0 if all went fine, else return appropriate error.
631 static int ti_sci_cmd_get_device(const struct ti_sci_handle
*handle
, u32 id
)
633 return ti_sci_set_device_state(handle
, id
,
634 MSG_FLAG_DEVICE_EXCLUSIVE
,
635 MSG_DEVICE_SW_STATE_ON
);
639 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
640 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
641 * @id: Device Identifier
643 * Request for the device - NOTE: the client MUST maintain integrity of
644 * usage count by balancing get_device with put_device. No refcounting is
645 * managed by driver for that purpose.
647 * Return: 0 if all went fine, else return appropriate error.
649 static int ti_sci_cmd_idle_device(const struct ti_sci_handle
*handle
, u32 id
)
651 return ti_sci_set_device_state(handle
, id
,
652 MSG_FLAG_DEVICE_EXCLUSIVE
,
653 MSG_DEVICE_SW_STATE_RETENTION
);
657 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
658 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
659 * @id: Device Identifier
661 * Request for the device - NOTE: the client MUST maintain integrity of
662 * usage count by balancing get_device with put_device. No refcounting is
663 * managed by driver for that purpose.
665 * Return: 0 if all went fine, else return appropriate error.
667 static int ti_sci_cmd_put_device(const struct ti_sci_handle
*handle
, u32 id
)
669 return ti_sci_set_device_state(handle
, id
,
670 0, MSG_DEVICE_SW_STATE_AUTO_OFF
);
674 * ti_sci_cmd_dev_is_valid() - Is the device valid
675 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
676 * @id: Device Identifier
678 * Return: 0 if all went fine and the device ID is valid, else return
681 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle
*handle
, u32 id
)
685 /* check the device state which will also tell us if the ID is valid */
686 return ti_sci_get_device_state(handle
, id
, NULL
, NULL
, NULL
, &unused
);
690 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
691 * @handle: Pointer to TISCI handle
692 * @id: Device Identifier
693 * @count: Pointer to Context Loss counter to populate
695 * Return: 0 if all went fine, else return appropriate error.
697 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle
*handle
, u32 id
,
700 return ti_sci_get_device_state(handle
, id
, count
, NULL
, NULL
, NULL
);
704 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
705 * @handle: Pointer to TISCI handle
706 * @id: Device Identifier
707 * @r_state: true if requested to be idle
709 * Return: 0 if all went fine, else return appropriate error.
711 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle
*handle
, u32 id
,
720 ret
= ti_sci_get_device_state(handle
, id
, NULL
, NULL
, &state
, NULL
);
724 *r_state
= (state
== MSG_DEVICE_SW_STATE_RETENTION
);
730 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
731 * @handle: Pointer to TISCI handle
732 * @id: Device Identifier
733 * @r_state: true if requested to be stopped
734 * @curr_state: true if currently stopped.
736 * Return: 0 if all went fine, else return appropriate error.
738 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle
*handle
, u32 id
,
739 bool *r_state
, bool *curr_state
)
744 if (!r_state
&& !curr_state
)
748 ti_sci_get_device_state(handle
, id
, NULL
, NULL
, &p_state
, &c_state
);
753 *r_state
= (p_state
== MSG_DEVICE_SW_STATE_AUTO_OFF
);
755 *curr_state
= (c_state
== MSG_DEVICE_HW_STATE_OFF
);
761 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
762 * @handle: Pointer to TISCI handle
763 * @id: Device Identifier
764 * @r_state: true if requested to be ON
765 * @curr_state: true if currently ON and active
767 * Return: 0 if all went fine, else return appropriate error.
769 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle
*handle
, u32 id
,
770 bool *r_state
, bool *curr_state
)
775 if (!r_state
&& !curr_state
)
779 ti_sci_get_device_state(handle
, id
, NULL
, NULL
, &p_state
, &c_state
);
784 *r_state
= (p_state
== MSG_DEVICE_SW_STATE_ON
);
786 *curr_state
= (c_state
== MSG_DEVICE_HW_STATE_ON
);
792 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
793 * @handle: Pointer to TISCI handle
794 * @id: Device Identifier
795 * @curr_state: true if currently transitioning.
797 * Return: 0 if all went fine, else return appropriate error.
799 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle
*handle
, u32 id
,
808 ret
= ti_sci_get_device_state(handle
, id
, NULL
, NULL
, NULL
, &state
);
812 *curr_state
= (state
== MSG_DEVICE_HW_STATE_TRANS
);
818 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
820 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
821 * @id: Device Identifier
822 * @reset_state: Device specific reset bit field
824 * Return: 0 if all went fine, else return appropriate error.
826 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle
*handle
,
827 u32 id
, u32 reset_state
)
829 struct ti_sci_info
*info
;
830 struct ti_sci_msg_req_set_device_resets
*req
;
831 struct ti_sci_msg_hdr
*resp
;
832 struct ti_sci_xfer
*xfer
;
837 return PTR_ERR(handle
);
841 info
= handle_to_ti_sci_info(handle
);
844 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_DEVICE_RESETS
,
845 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
846 sizeof(*req
), sizeof(*resp
));
849 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
852 req
= (struct ti_sci_msg_req_set_device_resets
*)xfer
->xfer_buf
;
854 req
->resets
= reset_state
;
856 ret
= ti_sci_do_xfer(info
, xfer
);
858 dev_err(dev
, "Mbox send fail %d\n", ret
);
862 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
864 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
867 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
873 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
875 * @handle: Pointer to TISCI handle
876 * @id: Device Identifier
877 * @reset_state: Pointer to reset state to populate
879 * Return: 0 if all went fine, else return appropriate error.
881 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle
*handle
,
882 u32 id
, u32
*reset_state
)
884 return ti_sci_get_device_state(handle
, id
, NULL
, reset_state
, NULL
,
889 * ti_sci_set_clock_state() - Set clock state helper
890 * @handle: pointer to TI SCI handle
891 * @dev_id: Device identifier this request is for
892 * @clk_id: Clock identifier for the device for this request.
893 * Each device has it's own set of clock inputs. This indexes
894 * which clock input to modify.
895 * @flags: Header flags as needed
896 * @state: State to request for the clock.
898 * Return: 0 if all went well, else returns appropriate error value.
900 static int ti_sci_set_clock_state(const struct ti_sci_handle
*handle
,
901 u32 dev_id
, u8 clk_id
,
904 struct ti_sci_info
*info
;
905 struct ti_sci_msg_req_set_clock_state
*req
;
906 struct ti_sci_msg_hdr
*resp
;
907 struct ti_sci_xfer
*xfer
;
912 return PTR_ERR(handle
);
916 info
= handle_to_ti_sci_info(handle
);
919 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_CLOCK_STATE
,
920 flags
| TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
921 sizeof(*req
), sizeof(*resp
));
924 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
927 req
= (struct ti_sci_msg_req_set_clock_state
*)xfer
->xfer_buf
;
928 req
->dev_id
= dev_id
;
929 req
->clk_id
= clk_id
;
930 req
->request_state
= state
;
932 ret
= ti_sci_do_xfer(info
, xfer
);
934 dev_err(dev
, "Mbox send fail %d\n", ret
);
938 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
940 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
943 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
949 * ti_sci_cmd_get_clock_state() - Get clock state helper
950 * @handle: pointer to TI SCI handle
951 * @dev_id: Device identifier this request is for
952 * @clk_id: Clock identifier for the device for this request.
953 * Each device has it's own set of clock inputs. This indexes
954 * which clock input to modify.
955 * @programmed_state: State requested for clock to move to
956 * @current_state: State that the clock is currently in
958 * Return: 0 if all went well, else returns appropriate error value.
960 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle
*handle
,
961 u32 dev_id
, u8 clk_id
,
962 u8
*programmed_state
, u8
*current_state
)
964 struct ti_sci_info
*info
;
965 struct ti_sci_msg_req_get_clock_state
*req
;
966 struct ti_sci_msg_resp_get_clock_state
*resp
;
967 struct ti_sci_xfer
*xfer
;
972 return PTR_ERR(handle
);
976 if (!programmed_state
&& !current_state
)
979 info
= handle_to_ti_sci_info(handle
);
982 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_CLOCK_STATE
,
983 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
984 sizeof(*req
), sizeof(*resp
));
987 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
990 req
= (struct ti_sci_msg_req_get_clock_state
*)xfer
->xfer_buf
;
991 req
->dev_id
= dev_id
;
992 req
->clk_id
= clk_id
;
994 ret
= ti_sci_do_xfer(info
, xfer
);
996 dev_err(dev
, "Mbox send fail %d\n", ret
);
1000 resp
= (struct ti_sci_msg_resp_get_clock_state
*)xfer
->xfer_buf
;
1002 if (!ti_sci_is_response_ack(resp
)) {
1007 if (programmed_state
)
1008 *programmed_state
= resp
->programmed_state
;
1010 *current_state
= resp
->current_state
;
1013 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1019 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1020 * @handle: pointer to TI SCI handle
1021 * @dev_id: Device identifier this request is for
1022 * @clk_id: Clock identifier for the device for this request.
1023 * Each device has it's own set of clock inputs. This indexes
1024 * which clock input to modify.
1025 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1026 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1027 * @enable_input_term: 'true' if input termination is desired, else 'false'
1029 * Return: 0 if all went well, else returns appropriate error value.
1031 static int ti_sci_cmd_get_clock(const struct ti_sci_handle
*handle
, u32 dev_id
,
1032 u8 clk_id
, bool needs_ssc
, bool can_change_freq
,
1033 bool enable_input_term
)
1037 flags
|= needs_ssc
? MSG_FLAG_CLOCK_ALLOW_SSC
: 0;
1038 flags
|= can_change_freq
? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE
: 0;
1039 flags
|= enable_input_term
? MSG_FLAG_CLOCK_INPUT_TERM
: 0;
1041 return ti_sci_set_clock_state(handle
, dev_id
, clk_id
, flags
,
1042 MSG_CLOCK_SW_STATE_REQ
);
1046 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1047 * @handle: pointer to TI SCI handle
1048 * @dev_id: Device identifier this request is for
1049 * @clk_id: Clock identifier for the device for this request.
1050 * Each device has it's own set of clock inputs. This indexes
1051 * which clock input to modify.
1053 * NOTE: This clock must have been requested by get_clock previously.
1055 * Return: 0 if all went well, else returns appropriate error value.
1057 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle
*handle
,
1058 u32 dev_id
, u8 clk_id
)
1060 return ti_sci_set_clock_state(handle
, dev_id
, clk_id
, 0,
1061 MSG_CLOCK_SW_STATE_UNREQ
);
1065 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1066 * @handle: pointer to TI SCI handle
1067 * @dev_id: Device identifier this request is for
1068 * @clk_id: Clock identifier for the device for this request.
1069 * Each device has it's own set of clock inputs. This indexes
1070 * which clock input to modify.
1072 * NOTE: This clock must have been requested by get_clock previously.
1074 * Return: 0 if all went well, else returns appropriate error value.
1076 static int ti_sci_cmd_put_clock(const struct ti_sci_handle
*handle
,
1077 u32 dev_id
, u8 clk_id
)
1079 return ti_sci_set_clock_state(handle
, dev_id
, clk_id
, 0,
1080 MSG_CLOCK_SW_STATE_AUTO
);
1084 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1085 * @handle: pointer to TI SCI handle
1086 * @dev_id: Device identifier this request is for
1087 * @clk_id: Clock identifier for the device for this request.
1088 * Each device has it's own set of clock inputs. This indexes
1089 * which clock input to modify.
1090 * @req_state: state indicating if the clock is auto managed
1092 * Return: 0 if all went well, else returns appropriate error value.
1094 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle
*handle
,
1095 u32 dev_id
, u8 clk_id
, bool *req_state
)
1103 ret
= ti_sci_cmd_get_clock_state(handle
, dev_id
, clk_id
, &state
, NULL
);
1107 *req_state
= (state
== MSG_CLOCK_SW_STATE_AUTO
);
1112 * ti_sci_cmd_clk_is_on() - Is the clock ON
1113 * @handle: pointer to TI SCI handle
1114 * @dev_id: Device identifier this request is for
1115 * @clk_id: Clock identifier for the device for this request.
1116 * Each device has it's own set of clock inputs. This indexes
1117 * which clock input to modify.
1118 * @req_state: state indicating if the clock is managed by us and enabled
1119 * @curr_state: state indicating if the clock is ready for operation
1121 * Return: 0 if all went well, else returns appropriate error value.
1123 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle
*handle
, u32 dev_id
,
1124 u8 clk_id
, bool *req_state
, bool *curr_state
)
1126 u8 c_state
= 0, r_state
= 0;
1129 if (!req_state
&& !curr_state
)
1132 ret
= ti_sci_cmd_get_clock_state(handle
, dev_id
, clk_id
,
1133 &r_state
, &c_state
);
1138 *req_state
= (r_state
== MSG_CLOCK_SW_STATE_REQ
);
1140 *curr_state
= (c_state
== MSG_CLOCK_HW_STATE_READY
);
1145 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1146 * @handle: pointer to TI SCI handle
1147 * @dev_id: Device identifier this request is for
1148 * @clk_id: Clock identifier for the device for this request.
1149 * Each device has it's own set of clock inputs. This indexes
1150 * which clock input to modify.
1151 * @req_state: state indicating if the clock is managed by us and disabled
1152 * @curr_state: state indicating if the clock is NOT ready for operation
1154 * Return: 0 if all went well, else returns appropriate error value.
1156 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle
*handle
, u32 dev_id
,
1157 u8 clk_id
, bool *req_state
, bool *curr_state
)
1159 u8 c_state
= 0, r_state
= 0;
1162 if (!req_state
&& !curr_state
)
1165 ret
= ti_sci_cmd_get_clock_state(handle
, dev_id
, clk_id
,
1166 &r_state
, &c_state
);
1171 *req_state
= (r_state
== MSG_CLOCK_SW_STATE_UNREQ
);
1173 *curr_state
= (c_state
== MSG_CLOCK_HW_STATE_NOT_READY
);
1178 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1179 * @handle: pointer to TI SCI handle
1180 * @dev_id: Device identifier this request is for
1181 * @clk_id: Clock identifier for the device for this request.
1182 * Each device has it's own set of clock inputs. This indexes
1183 * which clock input to modify.
1184 * @parent_id: Parent clock identifier to set
1186 * Return: 0 if all went well, else returns appropriate error value.
1188 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle
*handle
,
1189 u32 dev_id
, u8 clk_id
, u8 parent_id
)
1191 struct ti_sci_info
*info
;
1192 struct ti_sci_msg_req_set_clock_parent
*req
;
1193 struct ti_sci_msg_hdr
*resp
;
1194 struct ti_sci_xfer
*xfer
;
1199 return PTR_ERR(handle
);
1203 info
= handle_to_ti_sci_info(handle
);
1206 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_CLOCK_PARENT
,
1207 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1208 sizeof(*req
), sizeof(*resp
));
1210 ret
= PTR_ERR(xfer
);
1211 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1214 req
= (struct ti_sci_msg_req_set_clock_parent
*)xfer
->xfer_buf
;
1215 req
->dev_id
= dev_id
;
1216 req
->clk_id
= clk_id
;
1217 req
->parent_id
= parent_id
;
1219 ret
= ti_sci_do_xfer(info
, xfer
);
1221 dev_err(dev
, "Mbox send fail %d\n", ret
);
1225 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
1227 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
1230 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1236 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1237 * @handle: pointer to TI SCI handle
1238 * @dev_id: Device identifier this request is for
1239 * @clk_id: Clock identifier for the device for this request.
1240 * Each device has it's own set of clock inputs. This indexes
1241 * which clock input to modify.
1242 * @parent_id: Current clock parent
1244 * Return: 0 if all went well, else returns appropriate error value.
1246 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle
*handle
,
1247 u32 dev_id
, u8 clk_id
, u8
*parent_id
)
1249 struct ti_sci_info
*info
;
1250 struct ti_sci_msg_req_get_clock_parent
*req
;
1251 struct ti_sci_msg_resp_get_clock_parent
*resp
;
1252 struct ti_sci_xfer
*xfer
;
1257 return PTR_ERR(handle
);
1258 if (!handle
|| !parent_id
)
1261 info
= handle_to_ti_sci_info(handle
);
1264 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_CLOCK_PARENT
,
1265 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1266 sizeof(*req
), sizeof(*resp
));
1268 ret
= PTR_ERR(xfer
);
1269 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1272 req
= (struct ti_sci_msg_req_get_clock_parent
*)xfer
->xfer_buf
;
1273 req
->dev_id
= dev_id
;
1274 req
->clk_id
= clk_id
;
1276 ret
= ti_sci_do_xfer(info
, xfer
);
1278 dev_err(dev
, "Mbox send fail %d\n", ret
);
1282 resp
= (struct ti_sci_msg_resp_get_clock_parent
*)xfer
->xfer_buf
;
1284 if (!ti_sci_is_response_ack(resp
))
1287 *parent_id
= resp
->parent_id
;
1290 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1296 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1297 * @handle: pointer to TI SCI handle
1298 * @dev_id: Device identifier this request is for
1299 * @clk_id: Clock identifier for the device for this request.
1300 * Each device has it's own set of clock inputs. This indexes
1301 * which clock input to modify.
1302 * @num_parents: Returns he number of parents to the current clock.
1304 * Return: 0 if all went well, else returns appropriate error value.
1306 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle
*handle
,
1307 u32 dev_id
, u8 clk_id
,
1310 struct ti_sci_info
*info
;
1311 struct ti_sci_msg_req_get_clock_num_parents
*req
;
1312 struct ti_sci_msg_resp_get_clock_num_parents
*resp
;
1313 struct ti_sci_xfer
*xfer
;
1318 return PTR_ERR(handle
);
1319 if (!handle
|| !num_parents
)
1322 info
= handle_to_ti_sci_info(handle
);
1325 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS
,
1326 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1327 sizeof(*req
), sizeof(*resp
));
1329 ret
= PTR_ERR(xfer
);
1330 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1333 req
= (struct ti_sci_msg_req_get_clock_num_parents
*)xfer
->xfer_buf
;
1334 req
->dev_id
= dev_id
;
1335 req
->clk_id
= clk_id
;
1337 ret
= ti_sci_do_xfer(info
, xfer
);
1339 dev_err(dev
, "Mbox send fail %d\n", ret
);
1343 resp
= (struct ti_sci_msg_resp_get_clock_num_parents
*)xfer
->xfer_buf
;
1345 if (!ti_sci_is_response_ack(resp
))
1348 *num_parents
= resp
->num_parents
;
1351 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1357 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1358 * @handle: pointer to TI SCI handle
1359 * @dev_id: Device identifier this request is for
1360 * @clk_id: Clock identifier for the device for this request.
1361 * Each device has it's own set of clock inputs. This indexes
1362 * which clock input to modify.
1363 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1364 * allowable programmed frequency and does not account for clock
1365 * tolerances and jitter.
1366 * @target_freq: The target clock frequency in Hz. A frequency will be
1367 * processed as close to this target frequency as possible.
1368 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1369 * allowable programmed frequency and does not account for clock
1370 * tolerances and jitter.
1371 * @match_freq: Frequency match in Hz response.
1373 * Return: 0 if all went well, else returns appropriate error value.
1375 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle
*handle
,
1376 u32 dev_id
, u8 clk_id
, u64 min_freq
,
1377 u64 target_freq
, u64 max_freq
,
1380 struct ti_sci_info
*info
;
1381 struct ti_sci_msg_req_query_clock_freq
*req
;
1382 struct ti_sci_msg_resp_query_clock_freq
*resp
;
1383 struct ti_sci_xfer
*xfer
;
1388 return PTR_ERR(handle
);
1389 if (!handle
|| !match_freq
)
1392 info
= handle_to_ti_sci_info(handle
);
1395 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_QUERY_CLOCK_FREQ
,
1396 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1397 sizeof(*req
), sizeof(*resp
));
1399 ret
= PTR_ERR(xfer
);
1400 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1403 req
= (struct ti_sci_msg_req_query_clock_freq
*)xfer
->xfer_buf
;
1404 req
->dev_id
= dev_id
;
1405 req
->clk_id
= clk_id
;
1406 req
->min_freq_hz
= min_freq
;
1407 req
->target_freq_hz
= target_freq
;
1408 req
->max_freq_hz
= max_freq
;
1410 ret
= ti_sci_do_xfer(info
, xfer
);
1412 dev_err(dev
, "Mbox send fail %d\n", ret
);
1416 resp
= (struct ti_sci_msg_resp_query_clock_freq
*)xfer
->xfer_buf
;
1418 if (!ti_sci_is_response_ack(resp
))
1421 *match_freq
= resp
->freq_hz
;
1424 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1430 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1431 * @handle: pointer to TI SCI handle
1432 * @dev_id: Device identifier this request is for
1433 * @clk_id: Clock identifier for the device for this request.
1434 * Each device has it's own set of clock inputs. This indexes
1435 * which clock input to modify.
1436 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1437 * allowable programmed frequency and does not account for clock
1438 * tolerances and jitter.
1439 * @target_freq: The target clock frequency in Hz. A frequency will be
1440 * processed as close to this target frequency as possible.
1441 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1442 * allowable programmed frequency and does not account for clock
1443 * tolerances and jitter.
1445 * Return: 0 if all went well, else returns appropriate error value.
1447 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle
*handle
,
1448 u32 dev_id
, u8 clk_id
, u64 min_freq
,
1449 u64 target_freq
, u64 max_freq
)
1451 struct ti_sci_info
*info
;
1452 struct ti_sci_msg_req_set_clock_freq
*req
;
1453 struct ti_sci_msg_hdr
*resp
;
1454 struct ti_sci_xfer
*xfer
;
1459 return PTR_ERR(handle
);
1463 info
= handle_to_ti_sci_info(handle
);
1466 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_CLOCK_FREQ
,
1467 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1468 sizeof(*req
), sizeof(*resp
));
1470 ret
= PTR_ERR(xfer
);
1471 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1474 req
= (struct ti_sci_msg_req_set_clock_freq
*)xfer
->xfer_buf
;
1475 req
->dev_id
= dev_id
;
1476 req
->clk_id
= clk_id
;
1477 req
->min_freq_hz
= min_freq
;
1478 req
->target_freq_hz
= target_freq
;
1479 req
->max_freq_hz
= max_freq
;
1481 ret
= ti_sci_do_xfer(info
, xfer
);
1483 dev_err(dev
, "Mbox send fail %d\n", ret
);
1487 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
1489 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
1492 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1498 * ti_sci_cmd_clk_get_freq() - Get current frequency
1499 * @handle: pointer to TI SCI handle
1500 * @dev_id: Device identifier this request is for
1501 * @clk_id: Clock identifier for the device for this request.
1502 * Each device has it's own set of clock inputs. This indexes
1503 * which clock input to modify.
1504 * @freq: Currently frequency in Hz
1506 * Return: 0 if all went well, else returns appropriate error value.
1508 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle
*handle
,
1509 u32 dev_id
, u8 clk_id
, u64
*freq
)
1511 struct ti_sci_info
*info
;
1512 struct ti_sci_msg_req_get_clock_freq
*req
;
1513 struct ti_sci_msg_resp_get_clock_freq
*resp
;
1514 struct ti_sci_xfer
*xfer
;
1519 return PTR_ERR(handle
);
1520 if (!handle
|| !freq
)
1523 info
= handle_to_ti_sci_info(handle
);
1526 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_CLOCK_FREQ
,
1527 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1528 sizeof(*req
), sizeof(*resp
));
1530 ret
= PTR_ERR(xfer
);
1531 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1534 req
= (struct ti_sci_msg_req_get_clock_freq
*)xfer
->xfer_buf
;
1535 req
->dev_id
= dev_id
;
1536 req
->clk_id
= clk_id
;
1538 ret
= ti_sci_do_xfer(info
, xfer
);
1540 dev_err(dev
, "Mbox send fail %d\n", ret
);
1544 resp
= (struct ti_sci_msg_resp_get_clock_freq
*)xfer
->xfer_buf
;
1546 if (!ti_sci_is_response_ack(resp
))
1549 *freq
= resp
->freq_hz
;
1552 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1557 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle
*handle
)
1559 struct ti_sci_info
*info
;
1560 struct ti_sci_msg_req_reboot
*req
;
1561 struct ti_sci_msg_hdr
*resp
;
1562 struct ti_sci_xfer
*xfer
;
1567 return PTR_ERR(handle
);
1571 info
= handle_to_ti_sci_info(handle
);
1574 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SYS_RESET
,
1575 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1576 sizeof(*req
), sizeof(*resp
));
1578 ret
= PTR_ERR(xfer
);
1579 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1582 req
= (struct ti_sci_msg_req_reboot
*)xfer
->xfer_buf
;
1584 ret
= ti_sci_do_xfer(info
, xfer
);
1586 dev_err(dev
, "Mbox send fail %d\n", ret
);
1590 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
1592 if (!ti_sci_is_response_ack(resp
))
1598 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1604 * ti_sci_setup_ops() - Setup the operations structures
1605 * @info: pointer to TISCI pointer
1607 static void ti_sci_setup_ops(struct ti_sci_info
*info
)
1609 struct ti_sci_ops
*ops
= &info
->handle
.ops
;
1610 struct ti_sci_core_ops
*core_ops
= &ops
->core_ops
;
1611 struct ti_sci_dev_ops
*dops
= &ops
->dev_ops
;
1612 struct ti_sci_clk_ops
*cops
= &ops
->clk_ops
;
1614 core_ops
->reboot_device
= ti_sci_cmd_core_reboot
;
1616 dops
->get_device
= ti_sci_cmd_get_device
;
1617 dops
->idle_device
= ti_sci_cmd_idle_device
;
1618 dops
->put_device
= ti_sci_cmd_put_device
;
1620 dops
->is_valid
= ti_sci_cmd_dev_is_valid
;
1621 dops
->get_context_loss_count
= ti_sci_cmd_dev_get_clcnt
;
1622 dops
->is_idle
= ti_sci_cmd_dev_is_idle
;
1623 dops
->is_stop
= ti_sci_cmd_dev_is_stop
;
1624 dops
->is_on
= ti_sci_cmd_dev_is_on
;
1625 dops
->is_transitioning
= ti_sci_cmd_dev_is_trans
;
1626 dops
->set_device_resets
= ti_sci_cmd_set_device_resets
;
1627 dops
->get_device_resets
= ti_sci_cmd_get_device_resets
;
1629 cops
->get_clock
= ti_sci_cmd_get_clock
;
1630 cops
->idle_clock
= ti_sci_cmd_idle_clock
;
1631 cops
->put_clock
= ti_sci_cmd_put_clock
;
1632 cops
->is_auto
= ti_sci_cmd_clk_is_auto
;
1633 cops
->is_on
= ti_sci_cmd_clk_is_on
;
1634 cops
->is_off
= ti_sci_cmd_clk_is_off
;
1636 cops
->set_parent
= ti_sci_cmd_clk_set_parent
;
1637 cops
->get_parent
= ti_sci_cmd_clk_get_parent
;
1638 cops
->get_num_parents
= ti_sci_cmd_clk_get_num_parents
;
1640 cops
->get_best_match_freq
= ti_sci_cmd_clk_get_match_freq
;
1641 cops
->set_freq
= ti_sci_cmd_clk_set_freq
;
1642 cops
->get_freq
= ti_sci_cmd_clk_get_freq
;
1646 * ti_sci_get_handle() - Get the TI SCI handle for a device
1647 * @dev: Pointer to device for which we want SCI handle
1649 * NOTE: The function does not track individual clients of the framework
1650 * and is expected to be maintained by caller of TI SCI protocol library.
1651 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1652 * Return: pointer to handle if successful, else:
1653 * -EPROBE_DEFER if the instance is not ready
1654 * -ENODEV if the required node handler is missing
1655 * -EINVAL if invalid conditions are encountered.
1657 const struct ti_sci_handle
*ti_sci_get_handle(struct device
*dev
)
1659 struct device_node
*ti_sci_np
;
1660 struct list_head
*p
;
1661 struct ti_sci_handle
*handle
= NULL
;
1662 struct ti_sci_info
*info
;
1665 pr_err("I need a device pointer\n");
1666 return ERR_PTR(-EINVAL
);
1668 ti_sci_np
= of_get_parent(dev
->of_node
);
1670 dev_err(dev
, "No OF information\n");
1671 return ERR_PTR(-EINVAL
);
1674 mutex_lock(&ti_sci_list_mutex
);
1675 list_for_each(p
, &ti_sci_list
) {
1676 info
= list_entry(p
, struct ti_sci_info
, node
);
1677 if (ti_sci_np
== info
->dev
->of_node
) {
1678 handle
= &info
->handle
;
1683 mutex_unlock(&ti_sci_list_mutex
);
1684 of_node_put(ti_sci_np
);
1687 return ERR_PTR(-EPROBE_DEFER
);
1691 EXPORT_SYMBOL_GPL(ti_sci_get_handle
);
1694 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
1695 * @handle: Handle acquired by ti_sci_get_handle
1697 * NOTE: The function does not track individual clients of the framework
1698 * and is expected to be maintained by caller of TI SCI protocol library.
1699 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1701 * Return: 0 is successfully released
1702 * if an error pointer was passed, it returns the error value back,
1703 * if null was passed, it returns -EINVAL;
1705 int ti_sci_put_handle(const struct ti_sci_handle
*handle
)
1707 struct ti_sci_info
*info
;
1710 return PTR_ERR(handle
);
1714 info
= handle_to_ti_sci_info(handle
);
1715 mutex_lock(&ti_sci_list_mutex
);
1716 if (!WARN_ON(!info
->users
))
1718 mutex_unlock(&ti_sci_list_mutex
);
1722 EXPORT_SYMBOL_GPL(ti_sci_put_handle
);
1724 static void devm_ti_sci_release(struct device
*dev
, void *res
)
1726 const struct ti_sci_handle
**ptr
= res
;
1727 const struct ti_sci_handle
*handle
= *ptr
;
1730 ret
= ti_sci_put_handle(handle
);
1732 dev_err(dev
, "failed to put handle %d\n", ret
);
1736 * devm_ti_sci_get_handle() - Managed get handle
1737 * @dev: device for which we want SCI handle for.
1739 * NOTE: This releases the handle once the device resources are
1740 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1741 * The function does not track individual clients of the framework
1742 * and is expected to be maintained by caller of TI SCI protocol library.
1744 * Return: 0 if all went fine, else corresponding error.
1746 const struct ti_sci_handle
*devm_ti_sci_get_handle(struct device
*dev
)
1748 const struct ti_sci_handle
**ptr
;
1749 const struct ti_sci_handle
*handle
;
1751 ptr
= devres_alloc(devm_ti_sci_release
, sizeof(*ptr
), GFP_KERNEL
);
1753 return ERR_PTR(-ENOMEM
);
1754 handle
= ti_sci_get_handle(dev
);
1756 if (!IS_ERR(handle
)) {
1758 devres_add(dev
, ptr
);
1765 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle
);
1767 static int tisci_reboot_handler(struct notifier_block
*nb
, unsigned long mode
,
1770 struct ti_sci_info
*info
= reboot_to_ti_sci_info(nb
);
1771 const struct ti_sci_handle
*handle
= &info
->handle
;
1773 ti_sci_cmd_core_reboot(handle
);
1775 /* call fail OR pass, we should not be here in the first place */
1779 /* Description for K2G */
1780 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc
= {
1781 .default_host_id
= 2,
1782 /* Conservative duration */
1783 .max_rx_timeout_ms
= 1000,
1784 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1789 static const struct of_device_id ti_sci_of_match
[] = {
1790 {.compatible
= "ti,k2g-sci", .data
= &ti_sci_pmmc_k2g_desc
},
1793 MODULE_DEVICE_TABLE(of
, ti_sci_of_match
);
1795 static int ti_sci_probe(struct platform_device
*pdev
)
1797 struct device
*dev
= &pdev
->dev
;
1798 const struct of_device_id
*of_id
;
1799 const struct ti_sci_desc
*desc
;
1800 struct ti_sci_xfer
*xfer
;
1801 struct ti_sci_info
*info
= NULL
;
1802 struct ti_sci_xfers_info
*minfo
;
1803 struct mbox_client
*cl
;
1809 of_id
= of_match_device(ti_sci_of_match
, dev
);
1811 dev_err(dev
, "OF data missing\n");
1816 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
1822 ret
= of_property_read_u32(dev
->of_node
, "ti,host-id", &h_id
);
1823 /* if the property is not present in DT, use a default from desc */
1825 info
->host_id
= info
->desc
->default_host_id
;
1828 dev_warn(dev
, "Host ID 0 is reserved for firmware\n");
1829 info
->host_id
= info
->desc
->default_host_id
;
1831 info
->host_id
= h_id
;
1835 reboot
= of_property_read_bool(dev
->of_node
,
1836 "ti,system-reboot-controller");
1837 INIT_LIST_HEAD(&info
->node
);
1838 minfo
= &info
->minfo
;
1841 * Pre-allocate messages
1842 * NEVER allocate more than what we can indicate in hdr.seq
1843 * if we have data description bug, force a fix..
1845 if (WARN_ON(desc
->max_msgs
>=
1846 1 << 8 * sizeof(((struct ti_sci_msg_hdr
*)0)->seq
)))
1849 minfo
->xfer_block
= devm_kcalloc(dev
,
1851 sizeof(*minfo
->xfer_block
),
1853 if (!minfo
->xfer_block
)
1856 minfo
->xfer_alloc_table
= devm_kcalloc(dev
,
1857 BITS_TO_LONGS(desc
->max_msgs
),
1858 sizeof(unsigned long),
1860 if (!minfo
->xfer_alloc_table
)
1862 bitmap_zero(minfo
->xfer_alloc_table
, desc
->max_msgs
);
1864 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1865 for (i
= 0, xfer
= minfo
->xfer_block
; i
< desc
->max_msgs
; i
++, xfer
++) {
1866 xfer
->xfer_buf
= devm_kcalloc(dev
, 1, desc
->max_msg_size
,
1868 if (!xfer
->xfer_buf
)
1871 xfer
->tx_message
.buf
= xfer
->xfer_buf
;
1872 init_completion(&xfer
->done
);
1875 ret
= ti_sci_debugfs_create(pdev
, info
);
1877 dev_warn(dev
, "Failed to create debug file\n");
1879 platform_set_drvdata(pdev
, info
);
1883 cl
->tx_block
= false;
1884 cl
->rx_callback
= ti_sci_rx_callback
;
1885 cl
->knows_txdone
= true;
1887 spin_lock_init(&minfo
->xfer_lock
);
1888 sema_init(&minfo
->sem_xfer_count
, desc
->max_msgs
);
1890 info
->chan_rx
= mbox_request_channel_byname(cl
, "rx");
1891 if (IS_ERR(info
->chan_rx
)) {
1892 ret
= PTR_ERR(info
->chan_rx
);
1896 info
->chan_tx
= mbox_request_channel_byname(cl
, "tx");
1897 if (IS_ERR(info
->chan_tx
)) {
1898 ret
= PTR_ERR(info
->chan_tx
);
1901 ret
= ti_sci_cmd_get_revision(info
);
1903 dev_err(dev
, "Unable to communicate with TISCI(%d)\n", ret
);
1907 ti_sci_setup_ops(info
);
1910 info
->nb
.notifier_call
= tisci_reboot_handler
;
1911 info
->nb
.priority
= 128;
1913 ret
= register_restart_handler(&info
->nb
);
1915 dev_err(dev
, "reboot registration fail(%d)\n", ret
);
1920 dev_info(dev
, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1921 info
->handle
.version
.abi_major
, info
->handle
.version
.abi_minor
,
1922 info
->handle
.version
.firmware_revision
,
1923 info
->handle
.version
.firmware_description
);
1925 mutex_lock(&ti_sci_list_mutex
);
1926 list_add_tail(&info
->node
, &ti_sci_list
);
1927 mutex_unlock(&ti_sci_list_mutex
);
1929 return of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
1931 if (!IS_ERR(info
->chan_tx
))
1932 mbox_free_channel(info
->chan_tx
);
1933 if (!IS_ERR(info
->chan_rx
))
1934 mbox_free_channel(info
->chan_rx
);
1935 debugfs_remove(info
->d
);
1939 static int ti_sci_remove(struct platform_device
*pdev
)
1941 struct ti_sci_info
*info
;
1942 struct device
*dev
= &pdev
->dev
;
1945 of_platform_depopulate(dev
);
1947 info
= platform_get_drvdata(pdev
);
1949 if (info
->nb
.notifier_call
)
1950 unregister_restart_handler(&info
->nb
);
1952 mutex_lock(&ti_sci_list_mutex
);
1956 list_del(&info
->node
);
1957 mutex_unlock(&ti_sci_list_mutex
);
1960 ti_sci_debugfs_destroy(pdev
, info
);
1962 /* Safe to free channels since no more users */
1963 mbox_free_channel(info
->chan_tx
);
1964 mbox_free_channel(info
->chan_rx
);
1970 static struct platform_driver ti_sci_driver
= {
1971 .probe
= ti_sci_probe
,
1972 .remove
= ti_sci_remove
,
1975 .of_match_table
= of_match_ptr(ti_sci_of_match
),
1978 module_platform_driver(ti_sci_driver
);
1980 MODULE_LICENSE("GPL v2");
1981 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1982 MODULE_AUTHOR("Nishanth Menon");
1983 MODULE_ALIAS("platform:ti-sci");