2 * Texas Instruments System Control Interface Protocol Driver
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define pr_fmt(fmt) "%s: " fmt, __func__
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/export.h>
23 #include <linux/kernel.h>
24 #include <linux/mailbox_client.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
29 #include <linux/soc/ti/ti-msgmgr.h>
30 #include <linux/soc/ti/ti_sci_protocol.h>
31 #include <linux/reboot.h>
35 /* List of all TI SCI devices active in system */
36 static LIST_HEAD(ti_sci_list
);
37 /* Protection for the entire list */
38 static DEFINE_MUTEX(ti_sci_list_mutex
);
41 * struct ti_sci_xfer - Structure representing a message flow
42 * @tx_message: Transmit message
43 * @rx_len: Receive message length
44 * @xfer_buf: Preallocated buffer to store receive message
45 * Since we work with request-ACK protocol, we can
46 * reuse the same buffer for the rx path as we
47 * use for the tx path.
48 * @done: completion event
51 struct ti_msgmgr_message tx_message
;
54 struct completion done
;
58 * struct ti_sci_xfers_info - Structure to manage transfer information
59 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
61 * @xfer_block: Preallocated Message array
62 * @xfer_alloc_table: Bitmap table for allocated messages.
63 * Index of this bitmap table is also used for message
64 * sequence identifier.
65 * @xfer_lock: Protection for message allocation
67 struct ti_sci_xfers_info
{
68 struct semaphore sem_xfer_count
;
69 struct ti_sci_xfer
*xfer_block
;
70 unsigned long *xfer_alloc_table
;
71 /* protect transfer allocation */
76 * struct ti_sci_desc - Description of SoC integration
77 * @host_id: Host identifier representing the compute entity
78 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
79 * @max_msgs: Maximum number of messages that can be pending
80 * simultaneously in the system
81 * @max_msg_size: Maximum size of data per message that can be handled.
85 int max_rx_timeout_ms
;
91 * struct ti_sci_info - Structure representing a TI SCI instance
92 * @dev: Device pointer
93 * @desc: SoC description for this instance
94 * @nb: Reboot Notifier block
95 * @d: Debugfs file entry
96 * @debug_region: Memory region where the debug message are available
97 * @debug_region_size: Debug region size
98 * @debug_buffer: Buffer allocated to copy debug messages.
99 * @handle: Instance of TI SCI handle to send to clients.
100 * @cl: Mailbox Client
101 * @chan_tx: Transmit mailbox channel
102 * @chan_rx: Receive mailbox channel
103 * @minfo: Message info
105 * @users: Number of users of this instance
109 struct notifier_block nb
;
110 const struct ti_sci_desc
*desc
;
112 void __iomem
*debug_region
;
114 size_t debug_region_size
;
115 struct ti_sci_handle handle
;
116 struct mbox_client cl
;
117 struct mbox_chan
*chan_tx
;
118 struct mbox_chan
*chan_rx
;
119 struct ti_sci_xfers_info minfo
;
120 struct list_head node
;
121 /* protected by ti_sci_list_mutex */
126 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
127 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
128 #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
130 #ifdef CONFIG_DEBUG_FS
133 * ti_sci_debug_show() - Helper to dump the debug log
134 * @s: sequence file pointer
139 static int ti_sci_debug_show(struct seq_file
*s
, void *unused
)
141 struct ti_sci_info
*info
= s
->private;
143 memcpy_fromio(info
->debug_buffer
, info
->debug_region
,
144 info
->debug_region_size
);
146 * We don't trust firmware to leave NULL terminated last byte (hence
147 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
148 * specific data format for debug messages, We just present the data
149 * in the buffer as is - we expect the messages to be self explanatory.
151 seq_puts(s
, info
->debug_buffer
);
156 * ti_sci_debug_open() - debug file open
157 * @inode: inode pointer
158 * @file: file pointer
160 * Return: result of single_open
162 static int ti_sci_debug_open(struct inode
*inode
, struct file
*file
)
164 return single_open(file
, ti_sci_debug_show
, inode
->i_private
);
167 /* log file operations */
168 static const struct file_operations ti_sci_debug_fops
= {
169 .open
= ti_sci_debug_open
,
172 .release
= single_release
,
176 * ti_sci_debugfs_create() - Create log debug file
177 * @pdev: platform device pointer
178 * @info: Pointer to SCI entity information
180 * Return: 0 if all went fine, else corresponding error.
182 static int ti_sci_debugfs_create(struct platform_device
*pdev
,
183 struct ti_sci_info
*info
)
185 struct device
*dev
= &pdev
->dev
;
186 struct resource
*res
;
187 char debug_name
[50] = "ti_sci_debug@";
189 /* Debug region is optional */
190 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
,
192 info
->debug_region
= devm_ioremap_resource(dev
, res
);
193 if (IS_ERR(info
->debug_region
))
195 info
->debug_region_size
= resource_size(res
);
197 info
->debug_buffer
= devm_kcalloc(dev
, info
->debug_region_size
+ 1,
198 sizeof(char), GFP_KERNEL
);
199 if (!info
->debug_buffer
)
201 /* Setup NULL termination */
202 info
->debug_buffer
[info
->debug_region_size
] = 0;
204 info
->d
= debugfs_create_file(strncat(debug_name
, dev_name(dev
),
206 sizeof("ti_sci_debug@")),
207 0444, NULL
, info
, &ti_sci_debug_fops
);
209 return PTR_ERR(info
->d
);
211 dev_dbg(dev
, "Debug region => %p, size = %zu bytes, resource: %pr\n",
212 info
->debug_region
, info
->debug_region_size
, res
);
217 * ti_sci_debugfs_destroy() - clean up log debug file
218 * @pdev: platform device pointer
219 * @info: Pointer to SCI entity information
221 static void ti_sci_debugfs_destroy(struct platform_device
*pdev
,
222 struct ti_sci_info
*info
)
224 if (IS_ERR(info
->debug_region
))
227 debugfs_remove(info
->d
);
229 #else /* CONFIG_DEBUG_FS */
230 static inline int ti_sci_debugfs_create(struct platform_device
*dev
,
231 struct ti_sci_info
*info
)
236 static inline void ti_sci_debugfs_destroy(struct platform_device
*dev
,
237 struct ti_sci_info
*info
)
240 #endif /* CONFIG_DEBUG_FS */
243 * ti_sci_dump_header_dbg() - Helper to dump a message header.
244 * @dev: Device pointer corresponding to the SCI entity
245 * @hdr: pointer to header.
247 static inline void ti_sci_dump_header_dbg(struct device
*dev
,
248 struct ti_sci_msg_hdr
*hdr
)
250 dev_dbg(dev
, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
251 hdr
->type
, hdr
->host
, hdr
->seq
, hdr
->flags
);
255 * ti_sci_rx_callback() - mailbox client callback for receive messages
256 * @cl: client pointer
257 * @m: mailbox message
259 * Processes one received message to appropriate transfer information and
260 * signals completion of the transfer.
262 * NOTE: This function will be invoked in IRQ context, hence should be
263 * as optimal as possible.
265 static void ti_sci_rx_callback(struct mbox_client
*cl
, void *m
)
267 struct ti_sci_info
*info
= cl_to_ti_sci_info(cl
);
268 struct device
*dev
= info
->dev
;
269 struct ti_sci_xfers_info
*minfo
= &info
->minfo
;
270 struct ti_msgmgr_message
*mbox_msg
= m
;
271 struct ti_sci_msg_hdr
*hdr
= (struct ti_sci_msg_hdr
*)mbox_msg
->buf
;
272 struct ti_sci_xfer
*xfer
;
278 * Are we even expecting this?
279 * NOTE: barriers were implicit in locks used for modifying the bitmap
281 if (!test_bit(xfer_id
, minfo
->xfer_alloc_table
)) {
282 dev_err(dev
, "Message for %d is not expected!\n", xfer_id
);
286 xfer
= &minfo
->xfer_block
[xfer_id
];
288 /* Is the message of valid length? */
289 if (mbox_msg
->len
> info
->desc
->max_msg_size
) {
290 dev_err(dev
, "Unable to handle %d xfer(max %d)\n",
291 mbox_msg
->len
, info
->desc
->max_msg_size
);
292 ti_sci_dump_header_dbg(dev
, hdr
);
295 if (mbox_msg
->len
< xfer
->rx_len
) {
296 dev_err(dev
, "Recv xfer %d < expected %d length\n",
297 mbox_msg
->len
, xfer
->rx_len
);
298 ti_sci_dump_header_dbg(dev
, hdr
);
302 ti_sci_dump_header_dbg(dev
, hdr
);
303 /* Take a copy to the rx buffer.. */
304 memcpy(xfer
->xfer_buf
, mbox_msg
->buf
, xfer
->rx_len
);
305 complete(&xfer
->done
);
309 * ti_sci_get_one_xfer() - Allocate one message
310 * @info: Pointer to SCI entity information
311 * @msg_type: Message type
312 * @msg_flags: Flag to set for the message
313 * @tx_message_size: transmit message size
314 * @rx_message_size: receive message size
316 * Helper function which is used by various command functions that are
317 * exposed to clients of this driver for allocating a message traffic event.
319 * This function can sleep depending on pending requests already in the system
320 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
321 * of internal data structures.
323 * Return: 0 if all went fine, else corresponding error.
325 static struct ti_sci_xfer
*ti_sci_get_one_xfer(struct ti_sci_info
*info
,
326 u16 msg_type
, u32 msg_flags
,
327 size_t tx_message_size
,
328 size_t rx_message_size
)
330 struct ti_sci_xfers_info
*minfo
= &info
->minfo
;
331 struct ti_sci_xfer
*xfer
;
332 struct ti_sci_msg_hdr
*hdr
;
334 unsigned long bit_pos
;
339 /* Ensure we have sane transfer sizes */
340 if (rx_message_size
> info
->desc
->max_msg_size
||
341 tx_message_size
> info
->desc
->max_msg_size
||
342 rx_message_size
< sizeof(*hdr
) || tx_message_size
< sizeof(*hdr
))
343 return ERR_PTR(-ERANGE
);
346 * Ensure we have only controlled number of pending messages.
347 * Ideally, we might just have to wait a single message, be
348 * conservative and wait 5 times that..
350 timeout
= msecs_to_jiffies(info
->desc
->max_rx_timeout_ms
) * 5;
351 ret
= down_timeout(&minfo
->sem_xfer_count
, timeout
);
355 /* Keep the locked section as small as possible */
356 spin_lock_irqsave(&minfo
->xfer_lock
, flags
);
357 bit_pos
= find_first_zero_bit(minfo
->xfer_alloc_table
,
358 info
->desc
->max_msgs
);
359 set_bit(bit_pos
, minfo
->xfer_alloc_table
);
360 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
363 * We already ensured in probe that we can have max messages that can
364 * fit in hdr.seq - NOTE: this improves access latencies
365 * to predictable O(1) access, BUT, it opens us to risk if
366 * remote misbehaves with corrupted message sequence responses.
367 * If that happens, we are going to be messed up anyways..
369 xfer_id
= (u8
)bit_pos
;
371 xfer
= &minfo
->xfer_block
[xfer_id
];
373 hdr
= (struct ti_sci_msg_hdr
*)xfer
->tx_message
.buf
;
374 xfer
->tx_message
.len
= tx_message_size
;
375 xfer
->rx_len
= (u8
)rx_message_size
;
377 reinit_completion(&xfer
->done
);
380 hdr
->type
= msg_type
;
381 hdr
->host
= info
->desc
->host_id
;
382 hdr
->flags
= msg_flags
;
388 * ti_sci_put_one_xfer() - Release a message
389 * @minfo: transfer info pointer
390 * @xfer: message that was reserved by ti_sci_get_one_xfer
392 * This holds a spinlock to maintain integrity of internal data structures.
394 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info
*minfo
,
395 struct ti_sci_xfer
*xfer
)
398 struct ti_sci_msg_hdr
*hdr
;
401 hdr
= (struct ti_sci_msg_hdr
*)xfer
->tx_message
.buf
;
405 * Keep the locked section as small as possible
406 * NOTE: we might escape with smp_mb and no lock here..
407 * but just be conservative and symmetric.
409 spin_lock_irqsave(&minfo
->xfer_lock
, flags
);
410 clear_bit(xfer_id
, minfo
->xfer_alloc_table
);
411 spin_unlock_irqrestore(&minfo
->xfer_lock
, flags
);
413 /* Increment the count for the next user to get through */
414 up(&minfo
->sem_xfer_count
);
418 * ti_sci_do_xfer() - Do one transfer
419 * @info: Pointer to SCI entity information
420 * @xfer: Transfer to initiate and wait for response
422 * Return: -ETIMEDOUT in case of no response, if transmit error,
423 * return corresponding error, else if all goes well,
426 static inline int ti_sci_do_xfer(struct ti_sci_info
*info
,
427 struct ti_sci_xfer
*xfer
)
431 struct device
*dev
= info
->dev
;
433 ret
= mbox_send_message(info
->chan_tx
, &xfer
->tx_message
);
439 /* And we wait for the response. */
440 timeout
= msecs_to_jiffies(info
->desc
->max_rx_timeout_ms
);
441 if (!wait_for_completion_timeout(&xfer
->done
, timeout
)) {
442 dev_err(dev
, "Mbox timedout in resp(caller: %pS)\n",
447 * NOTE: we might prefer not to need the mailbox ticker to manage the
448 * transfer queueing since the protocol layer queues things by itself.
449 * Unfortunately, we have to kick the mailbox framework after we have
450 * received our message.
452 mbox_client_txdone(info
->chan_tx
, ret
);
458 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
459 * @info: Pointer to SCI entity information
461 * Updates the SCI information in the internal data structure.
463 * Return: 0 if all went fine, else return appropriate error.
465 static int ti_sci_cmd_get_revision(struct ti_sci_info
*info
)
467 struct device
*dev
= info
->dev
;
468 struct ti_sci_handle
*handle
= &info
->handle
;
469 struct ti_sci_version_info
*ver
= &handle
->version
;
470 struct ti_sci_msg_resp_version
*rev_info
;
471 struct ti_sci_xfer
*xfer
;
474 /* No need to setup flags since it is expected to respond */
475 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_VERSION
,
476 0x0, sizeof(struct ti_sci_msg_hdr
),
480 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
484 rev_info
= (struct ti_sci_msg_resp_version
*)xfer
->xfer_buf
;
486 ret
= ti_sci_do_xfer(info
, xfer
);
488 dev_err(dev
, "Mbox send fail %d\n", ret
);
492 ver
->abi_major
= rev_info
->abi_major
;
493 ver
->abi_minor
= rev_info
->abi_minor
;
494 ver
->firmware_revision
= rev_info
->firmware_revision
;
495 strncpy(ver
->firmware_description
, rev_info
->firmware_description
,
496 sizeof(ver
->firmware_description
));
499 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
504 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
505 * @r: pointer to response buffer
507 * Return: true if the response was an ACK, else returns false.
509 static inline bool ti_sci_is_response_ack(void *r
)
511 struct ti_sci_msg_hdr
*hdr
= r
;
513 return hdr
->flags
& TI_SCI_FLAG_RESP_GENERIC_ACK
? true : false;
517 * ti_sci_set_device_state() - Set device state helper
518 * @handle: pointer to TI SCI handle
519 * @id: Device identifier
520 * @flags: flags to setup for the device
521 * @state: State to move the device to
523 * Return: 0 if all went well, else returns appropriate error value.
525 static int ti_sci_set_device_state(const struct ti_sci_handle
*handle
,
526 u32 id
, u32 flags
, u8 state
)
528 struct ti_sci_info
*info
;
529 struct ti_sci_msg_req_set_device_state
*req
;
530 struct ti_sci_msg_hdr
*resp
;
531 struct ti_sci_xfer
*xfer
;
536 return PTR_ERR(handle
);
540 info
= handle_to_ti_sci_info(handle
);
543 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_DEVICE_STATE
,
544 flags
| TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
545 sizeof(*req
), sizeof(*resp
));
548 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
551 req
= (struct ti_sci_msg_req_set_device_state
*)xfer
->xfer_buf
;
555 ret
= ti_sci_do_xfer(info
, xfer
);
557 dev_err(dev
, "Mbox send fail %d\n", ret
);
561 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
563 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
566 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
572 * ti_sci_get_device_state() - Get device state helper
573 * @handle: Handle to the device
574 * @id: Device Identifier
575 * @clcnt: Pointer to Context Loss Count
576 * @resets: pointer to resets
577 * @p_state: pointer to p_state
578 * @c_state: pointer to c_state
580 * Return: 0 if all went fine, else return appropriate error.
582 static int ti_sci_get_device_state(const struct ti_sci_handle
*handle
,
583 u32 id
, u32
*clcnt
, u32
*resets
,
584 u8
*p_state
, u8
*c_state
)
586 struct ti_sci_info
*info
;
587 struct ti_sci_msg_req_get_device_state
*req
;
588 struct ti_sci_msg_resp_get_device_state
*resp
;
589 struct ti_sci_xfer
*xfer
;
594 return PTR_ERR(handle
);
598 if (!clcnt
&& !resets
&& !p_state
&& !c_state
)
601 info
= handle_to_ti_sci_info(handle
);
604 /* Response is expected, so need of any flags */
605 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_DEVICE_STATE
,
606 0, sizeof(*req
), sizeof(*resp
));
609 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
612 req
= (struct ti_sci_msg_req_get_device_state
*)xfer
->xfer_buf
;
615 ret
= ti_sci_do_xfer(info
, xfer
);
617 dev_err(dev
, "Mbox send fail %d\n", ret
);
621 resp
= (struct ti_sci_msg_resp_get_device_state
*)xfer
->xfer_buf
;
622 if (!ti_sci_is_response_ack(resp
)) {
628 *clcnt
= resp
->context_loss_count
;
630 *resets
= resp
->resets
;
632 *p_state
= resp
->programmed_state
;
634 *c_state
= resp
->current_state
;
636 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
642 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
643 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
644 * @id: Device Identifier
646 * Request for the device - NOTE: the client MUST maintain integrity of
647 * usage count by balancing get_device with put_device. No refcounting is
648 * managed by driver for that purpose.
650 * NOTE: The request is for exclusive access for the processor.
652 * Return: 0 if all went fine, else return appropriate error.
654 static int ti_sci_cmd_get_device(const struct ti_sci_handle
*handle
, u32 id
)
656 return ti_sci_set_device_state(handle
, id
,
657 MSG_FLAG_DEVICE_EXCLUSIVE
,
658 MSG_DEVICE_SW_STATE_ON
);
662 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
663 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
664 * @id: Device Identifier
666 * Request for the device - NOTE: the client MUST maintain integrity of
667 * usage count by balancing get_device with put_device. No refcounting is
668 * managed by driver for that purpose.
670 * Return: 0 if all went fine, else return appropriate error.
672 static int ti_sci_cmd_idle_device(const struct ti_sci_handle
*handle
, u32 id
)
674 return ti_sci_set_device_state(handle
, id
,
675 MSG_FLAG_DEVICE_EXCLUSIVE
,
676 MSG_DEVICE_SW_STATE_RETENTION
);
680 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
681 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
682 * @id: Device Identifier
684 * Request for the device - NOTE: the client MUST maintain integrity of
685 * usage count by balancing get_device with put_device. No refcounting is
686 * managed by driver for that purpose.
688 * Return: 0 if all went fine, else return appropriate error.
690 static int ti_sci_cmd_put_device(const struct ti_sci_handle
*handle
, u32 id
)
692 return ti_sci_set_device_state(handle
, id
,
693 0, MSG_DEVICE_SW_STATE_AUTO_OFF
);
697 * ti_sci_cmd_dev_is_valid() - Is the device valid
698 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
699 * @id: Device Identifier
701 * Return: 0 if all went fine and the device ID is valid, else return
704 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle
*handle
, u32 id
)
708 /* check the device state which will also tell us if the ID is valid */
709 return ti_sci_get_device_state(handle
, id
, NULL
, NULL
, NULL
, &unused
);
713 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
714 * @handle: Pointer to TISCI handle
715 * @id: Device Identifier
716 * @count: Pointer to Context Loss counter to populate
718 * Return: 0 if all went fine, else return appropriate error.
720 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle
*handle
, u32 id
,
723 return ti_sci_get_device_state(handle
, id
, count
, NULL
, NULL
, NULL
);
727 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
728 * @handle: Pointer to TISCI handle
729 * @id: Device Identifier
730 * @r_state: true if requested to be idle
732 * Return: 0 if all went fine, else return appropriate error.
734 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle
*handle
, u32 id
,
743 ret
= ti_sci_get_device_state(handle
, id
, NULL
, NULL
, &state
, NULL
);
747 *r_state
= (state
== MSG_DEVICE_SW_STATE_RETENTION
);
753 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
754 * @handle: Pointer to TISCI handle
755 * @id: Device Identifier
756 * @r_state: true if requested to be stopped
757 * @curr_state: true if currently stopped.
759 * Return: 0 if all went fine, else return appropriate error.
761 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle
*handle
, u32 id
,
762 bool *r_state
, bool *curr_state
)
767 if (!r_state
&& !curr_state
)
771 ti_sci_get_device_state(handle
, id
, NULL
, NULL
, &p_state
, &c_state
);
776 *r_state
= (p_state
== MSG_DEVICE_SW_STATE_AUTO_OFF
);
778 *curr_state
= (c_state
== MSG_DEVICE_HW_STATE_OFF
);
784 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
785 * @handle: Pointer to TISCI handle
786 * @id: Device Identifier
787 * @r_state: true if requested to be ON
788 * @curr_state: true if currently ON and active
790 * Return: 0 if all went fine, else return appropriate error.
792 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle
*handle
, u32 id
,
793 bool *r_state
, bool *curr_state
)
798 if (!r_state
&& !curr_state
)
802 ti_sci_get_device_state(handle
, id
, NULL
, NULL
, &p_state
, &c_state
);
807 *r_state
= (p_state
== MSG_DEVICE_SW_STATE_ON
);
809 *curr_state
= (c_state
== MSG_DEVICE_HW_STATE_ON
);
815 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
816 * @handle: Pointer to TISCI handle
817 * @id: Device Identifier
818 * @curr_state: true if currently transitioning.
820 * Return: 0 if all went fine, else return appropriate error.
822 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle
*handle
, u32 id
,
831 ret
= ti_sci_get_device_state(handle
, id
, NULL
, NULL
, NULL
, &state
);
835 *curr_state
= (state
== MSG_DEVICE_HW_STATE_TRANS
);
841 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
843 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
844 * @id: Device Identifier
845 * @reset_state: Device specific reset bit field
847 * Return: 0 if all went fine, else return appropriate error.
849 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle
*handle
,
850 u32 id
, u32 reset_state
)
852 struct ti_sci_info
*info
;
853 struct ti_sci_msg_req_set_device_resets
*req
;
854 struct ti_sci_msg_hdr
*resp
;
855 struct ti_sci_xfer
*xfer
;
860 return PTR_ERR(handle
);
864 info
= handle_to_ti_sci_info(handle
);
867 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_DEVICE_RESETS
,
868 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
869 sizeof(*req
), sizeof(*resp
));
872 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
875 req
= (struct ti_sci_msg_req_set_device_resets
*)xfer
->xfer_buf
;
877 req
->resets
= reset_state
;
879 ret
= ti_sci_do_xfer(info
, xfer
);
881 dev_err(dev
, "Mbox send fail %d\n", ret
);
885 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
887 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
890 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
896 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
898 * @handle: Pointer to TISCI handle
899 * @id: Device Identifier
900 * @reset_state: Pointer to reset state to populate
902 * Return: 0 if all went fine, else return appropriate error.
904 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle
*handle
,
905 u32 id
, u32
*reset_state
)
907 return ti_sci_get_device_state(handle
, id
, NULL
, reset_state
, NULL
,
912 * ti_sci_set_clock_state() - Set clock state helper
913 * @handle: pointer to TI SCI handle
914 * @dev_id: Device identifier this request is for
915 * @clk_id: Clock identifier for the device for this request.
916 * Each device has it's own set of clock inputs. This indexes
917 * which clock input to modify.
918 * @flags: Header flags as needed
919 * @state: State to request for the clock.
921 * Return: 0 if all went well, else returns appropriate error value.
923 static int ti_sci_set_clock_state(const struct ti_sci_handle
*handle
,
924 u32 dev_id
, u8 clk_id
,
927 struct ti_sci_info
*info
;
928 struct ti_sci_msg_req_set_clock_state
*req
;
929 struct ti_sci_msg_hdr
*resp
;
930 struct ti_sci_xfer
*xfer
;
935 return PTR_ERR(handle
);
939 info
= handle_to_ti_sci_info(handle
);
942 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_CLOCK_STATE
,
943 flags
| TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
944 sizeof(*req
), sizeof(*resp
));
947 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
950 req
= (struct ti_sci_msg_req_set_clock_state
*)xfer
->xfer_buf
;
951 req
->dev_id
= dev_id
;
952 req
->clk_id
= clk_id
;
953 req
->request_state
= state
;
955 ret
= ti_sci_do_xfer(info
, xfer
);
957 dev_err(dev
, "Mbox send fail %d\n", ret
);
961 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
963 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
966 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
972 * ti_sci_cmd_get_clock_state() - Get clock state helper
973 * @handle: pointer to TI SCI handle
974 * @dev_id: Device identifier this request is for
975 * @clk_id: Clock identifier for the device for this request.
976 * Each device has it's own set of clock inputs. This indexes
977 * which clock input to modify.
978 * @programmed_state: State requested for clock to move to
979 * @current_state: State that the clock is currently in
981 * Return: 0 if all went well, else returns appropriate error value.
983 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle
*handle
,
984 u32 dev_id
, u8 clk_id
,
985 u8
*programmed_state
, u8
*current_state
)
987 struct ti_sci_info
*info
;
988 struct ti_sci_msg_req_get_clock_state
*req
;
989 struct ti_sci_msg_resp_get_clock_state
*resp
;
990 struct ti_sci_xfer
*xfer
;
995 return PTR_ERR(handle
);
999 if (!programmed_state
&& !current_state
)
1002 info
= handle_to_ti_sci_info(handle
);
1005 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_CLOCK_STATE
,
1006 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1007 sizeof(*req
), sizeof(*resp
));
1009 ret
= PTR_ERR(xfer
);
1010 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1013 req
= (struct ti_sci_msg_req_get_clock_state
*)xfer
->xfer_buf
;
1014 req
->dev_id
= dev_id
;
1015 req
->clk_id
= clk_id
;
1017 ret
= ti_sci_do_xfer(info
, xfer
);
1019 dev_err(dev
, "Mbox send fail %d\n", ret
);
1023 resp
= (struct ti_sci_msg_resp_get_clock_state
*)xfer
->xfer_buf
;
1025 if (!ti_sci_is_response_ack(resp
)) {
1030 if (programmed_state
)
1031 *programmed_state
= resp
->programmed_state
;
1033 *current_state
= resp
->current_state
;
1036 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1042 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1043 * @handle: pointer to TI SCI handle
1044 * @dev_id: Device identifier this request is for
1045 * @clk_id: Clock identifier for the device for this request.
1046 * Each device has it's own set of clock inputs. This indexes
1047 * which clock input to modify.
1048 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1049 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1050 * @enable_input_term: 'true' if input termination is desired, else 'false'
1052 * Return: 0 if all went well, else returns appropriate error value.
1054 static int ti_sci_cmd_get_clock(const struct ti_sci_handle
*handle
, u32 dev_id
,
1055 u8 clk_id
, bool needs_ssc
, bool can_change_freq
,
1056 bool enable_input_term
)
1060 flags
|= needs_ssc
? MSG_FLAG_CLOCK_ALLOW_SSC
: 0;
1061 flags
|= can_change_freq
? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE
: 0;
1062 flags
|= enable_input_term
? MSG_FLAG_CLOCK_INPUT_TERM
: 0;
1064 return ti_sci_set_clock_state(handle
, dev_id
, clk_id
, flags
,
1065 MSG_CLOCK_SW_STATE_REQ
);
1069 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1070 * @handle: pointer to TI SCI handle
1071 * @dev_id: Device identifier this request is for
1072 * @clk_id: Clock identifier for the device for this request.
1073 * Each device has it's own set of clock inputs. This indexes
1074 * which clock input to modify.
1076 * NOTE: This clock must have been requested by get_clock previously.
1078 * Return: 0 if all went well, else returns appropriate error value.
1080 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle
*handle
,
1081 u32 dev_id
, u8 clk_id
)
1083 return ti_sci_set_clock_state(handle
, dev_id
, clk_id
, 0,
1084 MSG_CLOCK_SW_STATE_UNREQ
);
1088 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1089 * @handle: pointer to TI SCI handle
1090 * @dev_id: Device identifier this request is for
1091 * @clk_id: Clock identifier for the device for this request.
1092 * Each device has it's own set of clock inputs. This indexes
1093 * which clock input to modify.
1095 * NOTE: This clock must have been requested by get_clock previously.
1097 * Return: 0 if all went well, else returns appropriate error value.
1099 static int ti_sci_cmd_put_clock(const struct ti_sci_handle
*handle
,
1100 u32 dev_id
, u8 clk_id
)
1102 return ti_sci_set_clock_state(handle
, dev_id
, clk_id
, 0,
1103 MSG_CLOCK_SW_STATE_AUTO
);
1107 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1108 * @handle: pointer to TI SCI handle
1109 * @dev_id: Device identifier this request is for
1110 * @clk_id: Clock identifier for the device for this request.
1111 * Each device has it's own set of clock inputs. This indexes
1112 * which clock input to modify.
1113 * @req_state: state indicating if the clock is auto managed
1115 * Return: 0 if all went well, else returns appropriate error value.
1117 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle
*handle
,
1118 u32 dev_id
, u8 clk_id
, bool *req_state
)
1126 ret
= ti_sci_cmd_get_clock_state(handle
, dev_id
, clk_id
, &state
, NULL
);
1130 *req_state
= (state
== MSG_CLOCK_SW_STATE_AUTO
);
1135 * ti_sci_cmd_clk_is_on() - Is the clock ON
1136 * @handle: pointer to TI SCI handle
1137 * @dev_id: Device identifier this request is for
1138 * @clk_id: Clock identifier for the device for this request.
1139 * Each device has it's own set of clock inputs. This indexes
1140 * which clock input to modify.
1141 * @req_state: state indicating if the clock is managed by us and enabled
1142 * @curr_state: state indicating if the clock is ready for operation
1144 * Return: 0 if all went well, else returns appropriate error value.
1146 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle
*handle
, u32 dev_id
,
1147 u8 clk_id
, bool *req_state
, bool *curr_state
)
1149 u8 c_state
= 0, r_state
= 0;
1152 if (!req_state
&& !curr_state
)
1155 ret
= ti_sci_cmd_get_clock_state(handle
, dev_id
, clk_id
,
1156 &r_state
, &c_state
);
1161 *req_state
= (r_state
== MSG_CLOCK_SW_STATE_REQ
);
1163 *curr_state
= (c_state
== MSG_CLOCK_HW_STATE_READY
);
1168 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1169 * @handle: pointer to TI SCI handle
1170 * @dev_id: Device identifier this request is for
1171 * @clk_id: Clock identifier for the device for this request.
1172 * Each device has it's own set of clock inputs. This indexes
1173 * which clock input to modify.
1174 * @req_state: state indicating if the clock is managed by us and disabled
1175 * @curr_state: state indicating if the clock is NOT ready for operation
1177 * Return: 0 if all went well, else returns appropriate error value.
1179 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle
*handle
, u32 dev_id
,
1180 u8 clk_id
, bool *req_state
, bool *curr_state
)
1182 u8 c_state
= 0, r_state
= 0;
1185 if (!req_state
&& !curr_state
)
1188 ret
= ti_sci_cmd_get_clock_state(handle
, dev_id
, clk_id
,
1189 &r_state
, &c_state
);
1194 *req_state
= (r_state
== MSG_CLOCK_SW_STATE_UNREQ
);
1196 *curr_state
= (c_state
== MSG_CLOCK_HW_STATE_NOT_READY
);
1201 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1202 * @handle: pointer to TI SCI handle
1203 * @dev_id: Device identifier this request is for
1204 * @clk_id: Clock identifier for the device for this request.
1205 * Each device has it's own set of clock inputs. This indexes
1206 * which clock input to modify.
1207 * @parent_id: Parent clock identifier to set
1209 * Return: 0 if all went well, else returns appropriate error value.
1211 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle
*handle
,
1212 u32 dev_id
, u8 clk_id
, u8 parent_id
)
1214 struct ti_sci_info
*info
;
1215 struct ti_sci_msg_req_set_clock_parent
*req
;
1216 struct ti_sci_msg_hdr
*resp
;
1217 struct ti_sci_xfer
*xfer
;
1222 return PTR_ERR(handle
);
1226 info
= handle_to_ti_sci_info(handle
);
1229 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_CLOCK_PARENT
,
1230 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1231 sizeof(*req
), sizeof(*resp
));
1233 ret
= PTR_ERR(xfer
);
1234 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1237 req
= (struct ti_sci_msg_req_set_clock_parent
*)xfer
->xfer_buf
;
1238 req
->dev_id
= dev_id
;
1239 req
->clk_id
= clk_id
;
1240 req
->parent_id
= parent_id
;
1242 ret
= ti_sci_do_xfer(info
, xfer
);
1244 dev_err(dev
, "Mbox send fail %d\n", ret
);
1248 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
1250 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
1253 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1259 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1260 * @handle: pointer to TI SCI handle
1261 * @dev_id: Device identifier this request is for
1262 * @clk_id: Clock identifier for the device for this request.
1263 * Each device has it's own set of clock inputs. This indexes
1264 * which clock input to modify.
1265 * @parent_id: Current clock parent
1267 * Return: 0 if all went well, else returns appropriate error value.
1269 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle
*handle
,
1270 u32 dev_id
, u8 clk_id
, u8
*parent_id
)
1272 struct ti_sci_info
*info
;
1273 struct ti_sci_msg_req_get_clock_parent
*req
;
1274 struct ti_sci_msg_resp_get_clock_parent
*resp
;
1275 struct ti_sci_xfer
*xfer
;
1280 return PTR_ERR(handle
);
1281 if (!handle
|| !parent_id
)
1284 info
= handle_to_ti_sci_info(handle
);
1287 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_CLOCK_PARENT
,
1288 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1289 sizeof(*req
), sizeof(*resp
));
1291 ret
= PTR_ERR(xfer
);
1292 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1295 req
= (struct ti_sci_msg_req_get_clock_parent
*)xfer
->xfer_buf
;
1296 req
->dev_id
= dev_id
;
1297 req
->clk_id
= clk_id
;
1299 ret
= ti_sci_do_xfer(info
, xfer
);
1301 dev_err(dev
, "Mbox send fail %d\n", ret
);
1305 resp
= (struct ti_sci_msg_resp_get_clock_parent
*)xfer
->xfer_buf
;
1307 if (!ti_sci_is_response_ack(resp
))
1310 *parent_id
= resp
->parent_id
;
1313 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1319 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1320 * @handle: pointer to TI SCI handle
1321 * @dev_id: Device identifier this request is for
1322 * @clk_id: Clock identifier for the device for this request.
1323 * Each device has it's own set of clock inputs. This indexes
1324 * which clock input to modify.
1325 * @num_parents: Returns he number of parents to the current clock.
1327 * Return: 0 if all went well, else returns appropriate error value.
1329 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle
*handle
,
1330 u32 dev_id
, u8 clk_id
,
1333 struct ti_sci_info
*info
;
1334 struct ti_sci_msg_req_get_clock_num_parents
*req
;
1335 struct ti_sci_msg_resp_get_clock_num_parents
*resp
;
1336 struct ti_sci_xfer
*xfer
;
1341 return PTR_ERR(handle
);
1342 if (!handle
|| !num_parents
)
1345 info
= handle_to_ti_sci_info(handle
);
1348 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS
,
1349 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1350 sizeof(*req
), sizeof(*resp
));
1352 ret
= PTR_ERR(xfer
);
1353 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1356 req
= (struct ti_sci_msg_req_get_clock_num_parents
*)xfer
->xfer_buf
;
1357 req
->dev_id
= dev_id
;
1358 req
->clk_id
= clk_id
;
1360 ret
= ti_sci_do_xfer(info
, xfer
);
1362 dev_err(dev
, "Mbox send fail %d\n", ret
);
1366 resp
= (struct ti_sci_msg_resp_get_clock_num_parents
*)xfer
->xfer_buf
;
1368 if (!ti_sci_is_response_ack(resp
))
1371 *num_parents
= resp
->num_parents
;
1374 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1380 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1381 * @handle: pointer to TI SCI handle
1382 * @dev_id: Device identifier this request is for
1383 * @clk_id: Clock identifier for the device for this request.
1384 * Each device has it's own set of clock inputs. This indexes
1385 * which clock input to modify.
1386 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1387 * allowable programmed frequency and does not account for clock
1388 * tolerances and jitter.
1389 * @target_freq: The target clock frequency in Hz. A frequency will be
1390 * processed as close to this target frequency as possible.
1391 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1392 * allowable programmed frequency and does not account for clock
1393 * tolerances and jitter.
1394 * @match_freq: Frequency match in Hz response.
1396 * Return: 0 if all went well, else returns appropriate error value.
1398 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle
*handle
,
1399 u32 dev_id
, u8 clk_id
, u64 min_freq
,
1400 u64 target_freq
, u64 max_freq
,
1403 struct ti_sci_info
*info
;
1404 struct ti_sci_msg_req_query_clock_freq
*req
;
1405 struct ti_sci_msg_resp_query_clock_freq
*resp
;
1406 struct ti_sci_xfer
*xfer
;
1411 return PTR_ERR(handle
);
1412 if (!handle
|| !match_freq
)
1415 info
= handle_to_ti_sci_info(handle
);
1418 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_QUERY_CLOCK_FREQ
,
1419 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1420 sizeof(*req
), sizeof(*resp
));
1422 ret
= PTR_ERR(xfer
);
1423 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1426 req
= (struct ti_sci_msg_req_query_clock_freq
*)xfer
->xfer_buf
;
1427 req
->dev_id
= dev_id
;
1428 req
->clk_id
= clk_id
;
1429 req
->min_freq_hz
= min_freq
;
1430 req
->target_freq_hz
= target_freq
;
1431 req
->max_freq_hz
= max_freq
;
1433 ret
= ti_sci_do_xfer(info
, xfer
);
1435 dev_err(dev
, "Mbox send fail %d\n", ret
);
1439 resp
= (struct ti_sci_msg_resp_query_clock_freq
*)xfer
->xfer_buf
;
1441 if (!ti_sci_is_response_ack(resp
))
1444 *match_freq
= resp
->freq_hz
;
1447 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1453 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1454 * @handle: pointer to TI SCI handle
1455 * @dev_id: Device identifier this request is for
1456 * @clk_id: Clock identifier for the device for this request.
1457 * Each device has it's own set of clock inputs. This indexes
1458 * which clock input to modify.
1459 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1460 * allowable programmed frequency and does not account for clock
1461 * tolerances and jitter.
1462 * @target_freq: The target clock frequency in Hz. A frequency will be
1463 * processed as close to this target frequency as possible.
1464 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1465 * allowable programmed frequency and does not account for clock
1466 * tolerances and jitter.
1468 * Return: 0 if all went well, else returns appropriate error value.
1470 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle
*handle
,
1471 u32 dev_id
, u8 clk_id
, u64 min_freq
,
1472 u64 target_freq
, u64 max_freq
)
1474 struct ti_sci_info
*info
;
1475 struct ti_sci_msg_req_set_clock_freq
*req
;
1476 struct ti_sci_msg_hdr
*resp
;
1477 struct ti_sci_xfer
*xfer
;
1482 return PTR_ERR(handle
);
1486 info
= handle_to_ti_sci_info(handle
);
1489 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SET_CLOCK_FREQ
,
1490 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1491 sizeof(*req
), sizeof(*resp
));
1493 ret
= PTR_ERR(xfer
);
1494 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1497 req
= (struct ti_sci_msg_req_set_clock_freq
*)xfer
->xfer_buf
;
1498 req
->dev_id
= dev_id
;
1499 req
->clk_id
= clk_id
;
1500 req
->min_freq_hz
= min_freq
;
1501 req
->target_freq_hz
= target_freq
;
1502 req
->max_freq_hz
= max_freq
;
1504 ret
= ti_sci_do_xfer(info
, xfer
);
1506 dev_err(dev
, "Mbox send fail %d\n", ret
);
1510 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
1512 ret
= ti_sci_is_response_ack(resp
) ? 0 : -ENODEV
;
1515 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1521 * ti_sci_cmd_clk_get_freq() - Get current frequency
1522 * @handle: pointer to TI SCI handle
1523 * @dev_id: Device identifier this request is for
1524 * @clk_id: Clock identifier for the device for this request.
1525 * Each device has it's own set of clock inputs. This indexes
1526 * which clock input to modify.
1527 * @freq: Currently frequency in Hz
1529 * Return: 0 if all went well, else returns appropriate error value.
1531 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle
*handle
,
1532 u32 dev_id
, u8 clk_id
, u64
*freq
)
1534 struct ti_sci_info
*info
;
1535 struct ti_sci_msg_req_get_clock_freq
*req
;
1536 struct ti_sci_msg_resp_get_clock_freq
*resp
;
1537 struct ti_sci_xfer
*xfer
;
1542 return PTR_ERR(handle
);
1543 if (!handle
|| !freq
)
1546 info
= handle_to_ti_sci_info(handle
);
1549 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_GET_CLOCK_FREQ
,
1550 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1551 sizeof(*req
), sizeof(*resp
));
1553 ret
= PTR_ERR(xfer
);
1554 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1557 req
= (struct ti_sci_msg_req_get_clock_freq
*)xfer
->xfer_buf
;
1558 req
->dev_id
= dev_id
;
1559 req
->clk_id
= clk_id
;
1561 ret
= ti_sci_do_xfer(info
, xfer
);
1563 dev_err(dev
, "Mbox send fail %d\n", ret
);
1567 resp
= (struct ti_sci_msg_resp_get_clock_freq
*)xfer
->xfer_buf
;
1569 if (!ti_sci_is_response_ack(resp
))
1572 *freq
= resp
->freq_hz
;
1575 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1580 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle
*handle
)
1582 struct ti_sci_info
*info
;
1583 struct ti_sci_msg_req_reboot
*req
;
1584 struct ti_sci_msg_hdr
*resp
;
1585 struct ti_sci_xfer
*xfer
;
1590 return PTR_ERR(handle
);
1594 info
= handle_to_ti_sci_info(handle
);
1597 xfer
= ti_sci_get_one_xfer(info
, TI_SCI_MSG_SYS_RESET
,
1598 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED
,
1599 sizeof(*req
), sizeof(*resp
));
1601 ret
= PTR_ERR(xfer
);
1602 dev_err(dev
, "Message alloc failed(%d)\n", ret
);
1605 req
= (struct ti_sci_msg_req_reboot
*)xfer
->xfer_buf
;
1607 ret
= ti_sci_do_xfer(info
, xfer
);
1609 dev_err(dev
, "Mbox send fail %d\n", ret
);
1613 resp
= (struct ti_sci_msg_hdr
*)xfer
->xfer_buf
;
1615 if (!ti_sci_is_response_ack(resp
))
1621 ti_sci_put_one_xfer(&info
->minfo
, xfer
);
1627 * ti_sci_setup_ops() - Setup the operations structures
1628 * @info: pointer to TISCI pointer
1630 static void ti_sci_setup_ops(struct ti_sci_info
*info
)
1632 struct ti_sci_ops
*ops
= &info
->handle
.ops
;
1633 struct ti_sci_core_ops
*core_ops
= &ops
->core_ops
;
1634 struct ti_sci_dev_ops
*dops
= &ops
->dev_ops
;
1635 struct ti_sci_clk_ops
*cops
= &ops
->clk_ops
;
1637 core_ops
->reboot_device
= ti_sci_cmd_core_reboot
;
1639 dops
->get_device
= ti_sci_cmd_get_device
;
1640 dops
->idle_device
= ti_sci_cmd_idle_device
;
1641 dops
->put_device
= ti_sci_cmd_put_device
;
1643 dops
->is_valid
= ti_sci_cmd_dev_is_valid
;
1644 dops
->get_context_loss_count
= ti_sci_cmd_dev_get_clcnt
;
1645 dops
->is_idle
= ti_sci_cmd_dev_is_idle
;
1646 dops
->is_stop
= ti_sci_cmd_dev_is_stop
;
1647 dops
->is_on
= ti_sci_cmd_dev_is_on
;
1648 dops
->is_transitioning
= ti_sci_cmd_dev_is_trans
;
1649 dops
->set_device_resets
= ti_sci_cmd_set_device_resets
;
1650 dops
->get_device_resets
= ti_sci_cmd_get_device_resets
;
1652 cops
->get_clock
= ti_sci_cmd_get_clock
;
1653 cops
->idle_clock
= ti_sci_cmd_idle_clock
;
1654 cops
->put_clock
= ti_sci_cmd_put_clock
;
1655 cops
->is_auto
= ti_sci_cmd_clk_is_auto
;
1656 cops
->is_on
= ti_sci_cmd_clk_is_on
;
1657 cops
->is_off
= ti_sci_cmd_clk_is_off
;
1659 cops
->set_parent
= ti_sci_cmd_clk_set_parent
;
1660 cops
->get_parent
= ti_sci_cmd_clk_get_parent
;
1661 cops
->get_num_parents
= ti_sci_cmd_clk_get_num_parents
;
1663 cops
->get_best_match_freq
= ti_sci_cmd_clk_get_match_freq
;
1664 cops
->set_freq
= ti_sci_cmd_clk_set_freq
;
1665 cops
->get_freq
= ti_sci_cmd_clk_get_freq
;
1669 * ti_sci_get_handle() - Get the TI SCI handle for a device
1670 * @dev: Pointer to device for which we want SCI handle
1672 * NOTE: The function does not track individual clients of the framework
1673 * and is expected to be maintained by caller of TI SCI protocol library.
1674 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1675 * Return: pointer to handle if successful, else:
1676 * -EPROBE_DEFER if the instance is not ready
1677 * -ENODEV if the required node handler is missing
1678 * -EINVAL if invalid conditions are encountered.
1680 const struct ti_sci_handle
*ti_sci_get_handle(struct device
*dev
)
1682 struct device_node
*ti_sci_np
;
1683 struct list_head
*p
;
1684 struct ti_sci_handle
*handle
= NULL
;
1685 struct ti_sci_info
*info
;
1688 pr_err("I need a device pointer\n");
1689 return ERR_PTR(-EINVAL
);
1691 ti_sci_np
= of_get_parent(dev
->of_node
);
1693 dev_err(dev
, "No OF information\n");
1694 return ERR_PTR(-EINVAL
);
1697 mutex_lock(&ti_sci_list_mutex
);
1698 list_for_each(p
, &ti_sci_list
) {
1699 info
= list_entry(p
, struct ti_sci_info
, node
);
1700 if (ti_sci_np
== info
->dev
->of_node
) {
1701 handle
= &info
->handle
;
1706 mutex_unlock(&ti_sci_list_mutex
);
1707 of_node_put(ti_sci_np
);
1710 return ERR_PTR(-EPROBE_DEFER
);
1714 EXPORT_SYMBOL_GPL(ti_sci_get_handle
);
1717 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
1718 * @handle: Handle acquired by ti_sci_get_handle
1720 * NOTE: The function does not track individual clients of the framework
1721 * and is expected to be maintained by caller of TI SCI protocol library.
1722 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1724 * Return: 0 is successfully released
1725 * if an error pointer was passed, it returns the error value back,
1726 * if null was passed, it returns -EINVAL;
1728 int ti_sci_put_handle(const struct ti_sci_handle
*handle
)
1730 struct ti_sci_info
*info
;
1733 return PTR_ERR(handle
);
1737 info
= handle_to_ti_sci_info(handle
);
1738 mutex_lock(&ti_sci_list_mutex
);
1739 if (!WARN_ON(!info
->users
))
1741 mutex_unlock(&ti_sci_list_mutex
);
1745 EXPORT_SYMBOL_GPL(ti_sci_put_handle
);
1747 static void devm_ti_sci_release(struct device
*dev
, void *res
)
1749 const struct ti_sci_handle
**ptr
= res
;
1750 const struct ti_sci_handle
*handle
= *ptr
;
1753 ret
= ti_sci_put_handle(handle
);
1755 dev_err(dev
, "failed to put handle %d\n", ret
);
1759 * devm_ti_sci_get_handle() - Managed get handle
1760 * @dev: device for which we want SCI handle for.
1762 * NOTE: This releases the handle once the device resources are
1763 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1764 * The function does not track individual clients of the framework
1765 * and is expected to be maintained by caller of TI SCI protocol library.
1767 * Return: 0 if all went fine, else corresponding error.
1769 const struct ti_sci_handle
*devm_ti_sci_get_handle(struct device
*dev
)
1771 const struct ti_sci_handle
**ptr
;
1772 const struct ti_sci_handle
*handle
;
1774 ptr
= devres_alloc(devm_ti_sci_release
, sizeof(*ptr
), GFP_KERNEL
);
1776 return ERR_PTR(-ENOMEM
);
1777 handle
= ti_sci_get_handle(dev
);
1779 if (!IS_ERR(handle
)) {
1781 devres_add(dev
, ptr
);
1788 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle
);
1790 static int tisci_reboot_handler(struct notifier_block
*nb
, unsigned long mode
,
1793 struct ti_sci_info
*info
= reboot_to_ti_sci_info(nb
);
1794 const struct ti_sci_handle
*handle
= &info
->handle
;
1796 ti_sci_cmd_core_reboot(handle
);
1798 /* call fail OR pass, we should not be here in the first place */
1802 /* Description for K2G */
1803 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc
= {
1805 /* Conservative duration */
1806 .max_rx_timeout_ms
= 1000,
1807 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1812 static const struct of_device_id ti_sci_of_match
[] = {
1813 {.compatible
= "ti,k2g-sci", .data
= &ti_sci_pmmc_k2g_desc
},
1816 MODULE_DEVICE_TABLE(of
, ti_sci_of_match
);
1818 static int ti_sci_probe(struct platform_device
*pdev
)
1820 struct device
*dev
= &pdev
->dev
;
1821 const struct of_device_id
*of_id
;
1822 const struct ti_sci_desc
*desc
;
1823 struct ti_sci_xfer
*xfer
;
1824 struct ti_sci_info
*info
= NULL
;
1825 struct ti_sci_xfers_info
*minfo
;
1826 struct mbox_client
*cl
;
1831 of_id
= of_match_device(ti_sci_of_match
, dev
);
1833 dev_err(dev
, "OF data missing\n");
1838 info
= devm_kzalloc(dev
, sizeof(*info
), GFP_KERNEL
);
1844 reboot
= of_property_read_bool(dev
->of_node
,
1845 "ti,system-reboot-controller");
1846 INIT_LIST_HEAD(&info
->node
);
1847 minfo
= &info
->minfo
;
1850 * Pre-allocate messages
1851 * NEVER allocate more than what we can indicate in hdr.seq
1852 * if we have data description bug, force a fix..
1854 if (WARN_ON(desc
->max_msgs
>=
1855 1 << 8 * sizeof(((struct ti_sci_msg_hdr
*)0)->seq
)))
1858 minfo
->xfer_block
= devm_kcalloc(dev
,
1860 sizeof(*minfo
->xfer_block
),
1862 if (!minfo
->xfer_block
)
1865 minfo
->xfer_alloc_table
= devm_kzalloc(dev
,
1866 BITS_TO_LONGS(desc
->max_msgs
)
1867 * sizeof(unsigned long),
1869 if (!minfo
->xfer_alloc_table
)
1871 bitmap_zero(minfo
->xfer_alloc_table
, desc
->max_msgs
);
1873 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1874 for (i
= 0, xfer
= minfo
->xfer_block
; i
< desc
->max_msgs
; i
++, xfer
++) {
1875 xfer
->xfer_buf
= devm_kcalloc(dev
, 1, desc
->max_msg_size
,
1877 if (!xfer
->xfer_buf
)
1880 xfer
->tx_message
.buf
= xfer
->xfer_buf
;
1881 init_completion(&xfer
->done
);
1884 ret
= ti_sci_debugfs_create(pdev
, info
);
1886 dev_warn(dev
, "Failed to create debug file\n");
1888 platform_set_drvdata(pdev
, info
);
1892 cl
->tx_block
= false;
1893 cl
->rx_callback
= ti_sci_rx_callback
;
1894 cl
->knows_txdone
= true;
1896 spin_lock_init(&minfo
->xfer_lock
);
1897 sema_init(&minfo
->sem_xfer_count
, desc
->max_msgs
);
1899 info
->chan_rx
= mbox_request_channel_byname(cl
, "rx");
1900 if (IS_ERR(info
->chan_rx
)) {
1901 ret
= PTR_ERR(info
->chan_rx
);
1905 info
->chan_tx
= mbox_request_channel_byname(cl
, "tx");
1906 if (IS_ERR(info
->chan_tx
)) {
1907 ret
= PTR_ERR(info
->chan_tx
);
1910 ret
= ti_sci_cmd_get_revision(info
);
1912 dev_err(dev
, "Unable to communicate with TISCI(%d)\n", ret
);
1916 ti_sci_setup_ops(info
);
1919 info
->nb
.notifier_call
= tisci_reboot_handler
;
1920 info
->nb
.priority
= 128;
1922 ret
= register_restart_handler(&info
->nb
);
1924 dev_err(dev
, "reboot registration fail(%d)\n", ret
);
1929 dev_info(dev
, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1930 info
->handle
.version
.abi_major
, info
->handle
.version
.abi_minor
,
1931 info
->handle
.version
.firmware_revision
,
1932 info
->handle
.version
.firmware_description
);
1934 mutex_lock(&ti_sci_list_mutex
);
1935 list_add_tail(&info
->node
, &ti_sci_list
);
1936 mutex_unlock(&ti_sci_list_mutex
);
1938 return of_platform_populate(dev
->of_node
, NULL
, NULL
, dev
);
1940 if (!IS_ERR(info
->chan_tx
))
1941 mbox_free_channel(info
->chan_tx
);
1942 if (!IS_ERR(info
->chan_rx
))
1943 mbox_free_channel(info
->chan_rx
);
1944 debugfs_remove(info
->d
);
1948 static int ti_sci_remove(struct platform_device
*pdev
)
1950 struct ti_sci_info
*info
;
1951 struct device
*dev
= &pdev
->dev
;
1954 of_platform_depopulate(dev
);
1956 info
= platform_get_drvdata(pdev
);
1958 if (info
->nb
.notifier_call
)
1959 unregister_restart_handler(&info
->nb
);
1961 mutex_lock(&ti_sci_list_mutex
);
1965 list_del(&info
->node
);
1966 mutex_unlock(&ti_sci_list_mutex
);
1969 ti_sci_debugfs_destroy(pdev
, info
);
1971 /* Safe to free channels since no more users */
1972 mbox_free_channel(info
->chan_tx
);
1973 mbox_free_channel(info
->chan_rx
);
1979 static struct platform_driver ti_sci_driver
= {
1980 .probe
= ti_sci_probe
,
1981 .remove
= ti_sci_remove
,
1984 .of_match_table
= of_match_ptr(ti_sci_of_match
),
1987 module_platform_driver(ti_sci_driver
);
1989 MODULE_LICENSE("GPL v2");
1990 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1991 MODULE_AUTHOR("Nishanth Menon");
1992 MODULE_ALIAS("platform:ti-sci");