1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments System Control Interface Protocol Driver
5 * Copyright (C) 2015-2022 Texas Instruments Incorporated - https://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/iopoll.h>
16 #include <linux/kernel.h>
17 #include <linux/mailbox_client.h>
18 #include <linux/module.h>
20 #include <linux/of_platform.h>
21 #include <linux/platform_device.h>
22 #include <linux/property.h>
23 #include <linux/semaphore.h>
24 #include <linux/slab.h>
25 #include <linux/soc/ti/ti-msgmgr.h>
26 #include <linux/soc/ti/ti_sci_protocol.h>
27 #include <linux/reboot.h>
31 /* List of all TI SCI devices active in system */
32 static LIST_HEAD(ti_sci_list);
33 /* Protection for the entire list */
34 static DEFINE_MUTEX(ti_sci_list_mutex);
37 * struct ti_sci_xfer - Structure representing a message flow
38 * @tx_message: Transmit message
39 * @rx_len: Receive message length
40 * @xfer_buf: Preallocated buffer to store receive message
41 * Since we work with request-ACK protocol, we can
42 * reuse the same buffer for the rx path as we
43 * use for the tx path.
44 * @done: completion event
47 struct ti_msgmgr_message tx_message;
50 struct completion done;
54 * struct ti_sci_xfers_info - Structure to manage transfer information
55 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
57 * @xfer_block: Preallocated Message array
58 * @xfer_alloc_table: Bitmap table for allocated messages.
59 * Index of this bitmap table is also used for message
60 * sequence identifier.
61 * @xfer_lock: Protection for message allocation
63 struct ti_sci_xfers_info {
64 struct semaphore sem_xfer_count;
65 struct ti_sci_xfer *xfer_block;
66 unsigned long *xfer_alloc_table;
67 /* protect transfer allocation */
72 * struct ti_sci_desc - Description of SoC integration
73 * @default_host_id: Host identifier representing the compute entity
74 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
75 * @max_msgs: Maximum number of messages that can be pending
76 * simultaneously in the system
77 * @max_msg_size: Maximum size of data per message that can be handled.
81 int max_rx_timeout_ms;
87 * struct ti_sci_info - Structure representing a TI SCI instance
88 * @dev: Device pointer
89 * @desc: SoC description for this instance
90 * @nb: Reboot Notifier block
91 * @d: Debugfs file entry
92 * @debug_region: Memory region where the debug message are available
93 * @debug_region_size: Debug region size
94 * @debug_buffer: Buffer allocated to copy debug messages.
95 * @handle: Instance of TI SCI handle to send to clients.
97 * @chan_tx: Transmit mailbox channel
98 * @chan_rx: Receive mailbox channel
99 * @minfo: Message info
102 * @users: Number of users of this instance
106 struct notifier_block nb;
107 const struct ti_sci_desc *desc;
109 void __iomem *debug_region;
111 size_t debug_region_size;
112 struct ti_sci_handle handle;
113 struct mbox_client cl;
114 struct mbox_chan *chan_tx;
115 struct mbox_chan *chan_rx;
116 struct ti_sci_xfers_info minfo;
117 struct list_head node;
119 /* protected by ti_sci_list_mutex */
123 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
124 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
125 #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
127 #ifdef CONFIG_DEBUG_FS
130 * ti_sci_debug_show() - Helper to dump the debug log
131 * @s: sequence file pointer
136 static int ti_sci_debug_show(struct seq_file *s, void *unused)
138 struct ti_sci_info *info = s->private;
140 memcpy_fromio(info->debug_buffer, info->debug_region,
141 info->debug_region_size);
143 * We don't trust firmware to leave NULL terminated last byte (hence
144 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
145 * specific data format for debug messages, We just present the data
146 * in the buffer as is - we expect the messages to be self explanatory.
148 seq_puts(s, info->debug_buffer);
152 /* Provide the log file operations interface*/
153 DEFINE_SHOW_ATTRIBUTE(ti_sci_debug);
156 * ti_sci_debugfs_create() - Create log debug file
157 * @pdev: platform device pointer
158 * @info: Pointer to SCI entity information
160 * Return: 0 if all went fine, else corresponding error.
162 static int ti_sci_debugfs_create(struct platform_device *pdev,
163 struct ti_sci_info *info)
165 struct device *dev = &pdev->dev;
166 struct resource *res;
169 /* Debug region is optional */
170 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
172 info->debug_region = devm_ioremap_resource(dev, res);
173 if (IS_ERR(info->debug_region))
175 info->debug_region_size = resource_size(res);
177 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
178 sizeof(char), GFP_KERNEL);
179 if (!info->debug_buffer)
181 /* Setup NULL termination */
182 info->debug_buffer[info->debug_region_size] = 0;
184 snprintf(debug_name, sizeof(debug_name), "ti_sci_debug@%s",
186 info->d = debugfs_create_file(debug_name, 0444, NULL, info,
189 return PTR_ERR(info->d);
191 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
192 info->debug_region, info->debug_region_size, res);
196 #else /* CONFIG_DEBUG_FS */
197 static inline int ti_sci_debugfs_create(struct platform_device *dev,
198 struct ti_sci_info *info)
203 static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
204 struct ti_sci_info *info)
207 #endif /* CONFIG_DEBUG_FS */
210 * ti_sci_dump_header_dbg() - Helper to dump a message header.
211 * @dev: Device pointer corresponding to the SCI entity
212 * @hdr: pointer to header.
214 static inline void ti_sci_dump_header_dbg(struct device *dev,
215 struct ti_sci_msg_hdr *hdr)
217 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
218 hdr->type, hdr->host, hdr->seq, hdr->flags);
222 * ti_sci_rx_callback() - mailbox client callback for receive messages
223 * @cl: client pointer
224 * @m: mailbox message
226 * Processes one received message to appropriate transfer information and
227 * signals completion of the transfer.
229 * NOTE: This function will be invoked in IRQ context, hence should be
230 * as optimal as possible.
232 static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
234 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
235 struct device *dev = info->dev;
236 struct ti_sci_xfers_info *minfo = &info->minfo;
237 struct ti_msgmgr_message *mbox_msg = m;
238 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
239 struct ti_sci_xfer *xfer;
245 * Are we even expecting this?
246 * NOTE: barriers were implicit in locks used for modifying the bitmap
248 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
249 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
253 xfer = &minfo->xfer_block[xfer_id];
255 /* Is the message of valid length? */
256 if (mbox_msg->len > info->desc->max_msg_size) {
257 dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
258 mbox_msg->len, info->desc->max_msg_size);
259 ti_sci_dump_header_dbg(dev, hdr);
262 if (mbox_msg->len < xfer->rx_len) {
263 dev_err(dev, "Recv xfer %zu < expected %d length\n",
264 mbox_msg->len, xfer->rx_len);
265 ti_sci_dump_header_dbg(dev, hdr);
269 ti_sci_dump_header_dbg(dev, hdr);
270 /* Take a copy to the rx buffer.. */
271 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
272 complete(&xfer->done);
276 * ti_sci_get_one_xfer() - Allocate one message
277 * @info: Pointer to SCI entity information
278 * @msg_type: Message type
279 * @msg_flags: Flag to set for the message
280 * @tx_message_size: transmit message size
281 * @rx_message_size: receive message size
283 * Helper function which is used by various command functions that are
284 * exposed to clients of this driver for allocating a message traffic event.
286 * This function can sleep depending on pending requests already in the system
287 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
288 * of internal data structures.
290 * Return: 0 if all went fine, else corresponding error.
292 static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
293 u16 msg_type, u32 msg_flags,
294 size_t tx_message_size,
295 size_t rx_message_size)
297 struct ti_sci_xfers_info *minfo = &info->minfo;
298 struct ti_sci_xfer *xfer;
299 struct ti_sci_msg_hdr *hdr;
301 unsigned long bit_pos;
306 /* Ensure we have sane transfer sizes */
307 if (rx_message_size > info->desc->max_msg_size ||
308 tx_message_size > info->desc->max_msg_size ||
309 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
310 return ERR_PTR(-ERANGE);
313 * Ensure we have only controlled number of pending messages.
314 * Ideally, we might just have to wait a single message, be
315 * conservative and wait 5 times that..
317 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
318 ret = down_timeout(&minfo->sem_xfer_count, timeout);
322 /* Keep the locked section as small as possible */
323 spin_lock_irqsave(&minfo->xfer_lock, flags);
324 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
325 info->desc->max_msgs);
326 set_bit(bit_pos, minfo->xfer_alloc_table);
327 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
330 * We already ensured in probe that we can have max messages that can
331 * fit in hdr.seq - NOTE: this improves access latencies
332 * to predictable O(1) access, BUT, it opens us to risk if
333 * remote misbehaves with corrupted message sequence responses.
334 * If that happens, we are going to be messed up anyways..
336 xfer_id = (u8)bit_pos;
338 xfer = &minfo->xfer_block[xfer_id];
340 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
341 xfer->tx_message.len = tx_message_size;
342 xfer->tx_message.chan_rx = info->chan_rx;
343 xfer->tx_message.timeout_rx_ms = info->desc->max_rx_timeout_ms;
344 xfer->rx_len = (u8)rx_message_size;
346 reinit_completion(&xfer->done);
349 hdr->type = msg_type;
350 hdr->host = info->host_id;
351 hdr->flags = msg_flags;
357 * ti_sci_put_one_xfer() - Release a message
358 * @minfo: transfer info pointer
359 * @xfer: message that was reserved by ti_sci_get_one_xfer
361 * This holds a spinlock to maintain integrity of internal data structures.
363 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
364 struct ti_sci_xfer *xfer)
367 struct ti_sci_msg_hdr *hdr;
370 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
374 * Keep the locked section as small as possible
375 * NOTE: we might escape with smp_mb and no lock here..
376 * but just be conservative and symmetric.
378 spin_lock_irqsave(&minfo->xfer_lock, flags);
379 clear_bit(xfer_id, minfo->xfer_alloc_table);
380 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
382 /* Increment the count for the next user to get through */
383 up(&minfo->sem_xfer_count);
387 * ti_sci_do_xfer() - Do one transfer
388 * @info: Pointer to SCI entity information
389 * @xfer: Transfer to initiate and wait for response
391 * Return: -ETIMEDOUT in case of no response, if transmit error,
392 * return corresponding error, else if all goes well,
395 static inline int ti_sci_do_xfer(struct ti_sci_info *info,
396 struct ti_sci_xfer *xfer)
400 struct device *dev = info->dev;
401 bool done_state = true;
403 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
409 if (system_state <= SYSTEM_RUNNING) {
410 /* And we wait for the response. */
411 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
412 if (!wait_for_completion_timeout(&xfer->done, timeout))
416 * If we are !running, we cannot use wait_for_completion_timeout
417 * during noirq phase, so we must manually poll the completion.
419 ret = read_poll_timeout_atomic(try_wait_for_completion, done_state,
421 info->desc->max_rx_timeout_ms * 1000,
425 if (ret == -ETIMEDOUT)
426 dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
430 * NOTE: we might prefer not to need the mailbox ticker to manage the
431 * transfer queueing since the protocol layer queues things by itself.
432 * Unfortunately, we have to kick the mailbox framework after we have
433 * received our message.
435 mbox_client_txdone(info->chan_tx, ret);
441 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
442 * @info: Pointer to SCI entity information
444 * Updates the SCI information in the internal data structure.
446 * Return: 0 if all went fine, else return appropriate error.
448 static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
450 struct device *dev = info->dev;
451 struct ti_sci_handle *handle = &info->handle;
452 struct ti_sci_version_info *ver = &handle->version;
453 struct ti_sci_msg_resp_version *rev_info;
454 struct ti_sci_xfer *xfer;
457 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
458 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
459 sizeof(struct ti_sci_msg_hdr),
463 dev_err(dev, "Message alloc failed(%d)\n", ret);
467 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
469 ret = ti_sci_do_xfer(info, xfer);
471 dev_err(dev, "Mbox send fail %d\n", ret);
475 ver->abi_major = rev_info->abi_major;
476 ver->abi_minor = rev_info->abi_minor;
477 ver->firmware_revision = rev_info->firmware_revision;
478 strscpy(ver->firmware_description, rev_info->firmware_description,
479 sizeof(ver->firmware_description));
482 ti_sci_put_one_xfer(&info->minfo, xfer);
487 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
488 * @r: pointer to response buffer
490 * Return: true if the response was an ACK, else returns false.
492 static inline bool ti_sci_is_response_ack(void *r)
494 struct ti_sci_msg_hdr *hdr = r;
496 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
500 * ti_sci_set_device_state() - Set device state helper
501 * @handle: pointer to TI SCI handle
502 * @id: Device identifier
503 * @flags: flags to setup for the device
504 * @state: State to move the device to
506 * Return: 0 if all went well, else returns appropriate error value.
508 static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
509 u32 id, u32 flags, u8 state)
511 struct ti_sci_info *info;
512 struct ti_sci_msg_req_set_device_state *req;
513 struct ti_sci_msg_hdr *resp;
514 struct ti_sci_xfer *xfer;
519 return PTR_ERR(handle);
523 info = handle_to_ti_sci_info(handle);
526 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
527 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
528 sizeof(*req), sizeof(*resp));
531 dev_err(dev, "Message alloc failed(%d)\n", ret);
534 req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
538 ret = ti_sci_do_xfer(info, xfer);
540 dev_err(dev, "Mbox send fail %d\n", ret);
544 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
546 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
549 ti_sci_put_one_xfer(&info->minfo, xfer);
555 * ti_sci_get_device_state() - Get device state helper
556 * @handle: Handle to the device
557 * @id: Device Identifier
558 * @clcnt: Pointer to Context Loss Count
559 * @resets: pointer to resets
560 * @p_state: pointer to p_state
561 * @c_state: pointer to c_state
563 * Return: 0 if all went fine, else return appropriate error.
565 static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
566 u32 id, u32 *clcnt, u32 *resets,
567 u8 *p_state, u8 *c_state)
569 struct ti_sci_info *info;
570 struct ti_sci_msg_req_get_device_state *req;
571 struct ti_sci_msg_resp_get_device_state *resp;
572 struct ti_sci_xfer *xfer;
577 return PTR_ERR(handle);
581 if (!clcnt && !resets && !p_state && !c_state)
584 info = handle_to_ti_sci_info(handle);
587 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
588 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
589 sizeof(*req), sizeof(*resp));
592 dev_err(dev, "Message alloc failed(%d)\n", ret);
595 req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
598 ret = ti_sci_do_xfer(info, xfer);
600 dev_err(dev, "Mbox send fail %d\n", ret);
604 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
605 if (!ti_sci_is_response_ack(resp)) {
611 *clcnt = resp->context_loss_count;
613 *resets = resp->resets;
615 *p_state = resp->programmed_state;
617 *c_state = resp->current_state;
619 ti_sci_put_one_xfer(&info->minfo, xfer);
625 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
626 * that can be shared with other hosts.
627 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
628 * @id: Device Identifier
630 * Request for the device - NOTE: the client MUST maintain integrity of
631 * usage count by balancing get_device with put_device. No refcounting is
632 * managed by driver for that purpose.
634 * Return: 0 if all went fine, else return appropriate error.
636 static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
638 return ti_sci_set_device_state(handle, id, 0,
639 MSG_DEVICE_SW_STATE_ON);
643 * ti_sci_cmd_get_device_exclusive() - command to request for device managed by
644 * TISCI that is exclusively owned by the
646 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
647 * @id: Device Identifier
649 * Request for the device - NOTE: the client MUST maintain integrity of
650 * usage count by balancing get_device with put_device. No refcounting is
651 * managed by driver for that purpose.
653 * Return: 0 if all went fine, else return appropriate error.
655 static int ti_sci_cmd_get_device_exclusive(const struct ti_sci_handle *handle,
658 return ti_sci_set_device_state(handle, id,
659 MSG_FLAG_DEVICE_EXCLUSIVE,
660 MSG_DEVICE_SW_STATE_ON);
664 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
665 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
666 * @id: Device Identifier
668 * Request for the device - NOTE: the client MUST maintain integrity of
669 * usage count by balancing get_device with put_device. No refcounting is
670 * managed by driver for that purpose.
672 * Return: 0 if all went fine, else return appropriate error.
674 static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
676 return ti_sci_set_device_state(handle, id, 0,
677 MSG_DEVICE_SW_STATE_RETENTION);
681 * ti_sci_cmd_idle_device_exclusive() - Command to idle a device managed by
682 * TISCI that is exclusively owned by
684 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
685 * @id: Device Identifier
687 * Request for the device - NOTE: the client MUST maintain integrity of
688 * usage count by balancing get_device with put_device. No refcounting is
689 * managed by driver for that purpose.
691 * Return: 0 if all went fine, else return appropriate error.
693 static int ti_sci_cmd_idle_device_exclusive(const struct ti_sci_handle *handle,
696 return ti_sci_set_device_state(handle, id,
697 MSG_FLAG_DEVICE_EXCLUSIVE,
698 MSG_DEVICE_SW_STATE_RETENTION);
702 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
703 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
704 * @id: Device Identifier
706 * Request for the device - NOTE: the client MUST maintain integrity of
707 * usage count by balancing get_device with put_device. No refcounting is
708 * managed by driver for that purpose.
710 * Return: 0 if all went fine, else return appropriate error.
712 static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
714 return ti_sci_set_device_state(handle, id,
715 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
719 * ti_sci_cmd_dev_is_valid() - Is the device valid
720 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
721 * @id: Device Identifier
723 * Return: 0 if all went fine and the device ID is valid, else return
726 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
730 /* check the device state which will also tell us if the ID is valid */
731 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
735 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
736 * @handle: Pointer to TISCI handle
737 * @id: Device Identifier
738 * @count: Pointer to Context Loss counter to populate
740 * Return: 0 if all went fine, else return appropriate error.
742 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
745 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
749 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
750 * @handle: Pointer to TISCI handle
751 * @id: Device Identifier
752 * @r_state: true if requested to be idle
754 * Return: 0 if all went fine, else return appropriate error.
756 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
765 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
769 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
775 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
776 * @handle: Pointer to TISCI handle
777 * @id: Device Identifier
778 * @r_state: true if requested to be stopped
779 * @curr_state: true if currently stopped.
781 * Return: 0 if all went fine, else return appropriate error.
783 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
784 bool *r_state, bool *curr_state)
789 if (!r_state && !curr_state)
793 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
798 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
800 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
806 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
807 * @handle: Pointer to TISCI handle
808 * @id: Device Identifier
809 * @r_state: true if requested to be ON
810 * @curr_state: true if currently ON and active
812 * Return: 0 if all went fine, else return appropriate error.
814 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
815 bool *r_state, bool *curr_state)
820 if (!r_state && !curr_state)
824 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
829 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
831 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
837 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
838 * @handle: Pointer to TISCI handle
839 * @id: Device Identifier
840 * @curr_state: true if currently transitioning.
842 * Return: 0 if all went fine, else return appropriate error.
844 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
853 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
857 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
863 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
865 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
866 * @id: Device Identifier
867 * @reset_state: Device specific reset bit field
869 * Return: 0 if all went fine, else return appropriate error.
871 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
872 u32 id, u32 reset_state)
874 struct ti_sci_info *info;
875 struct ti_sci_msg_req_set_device_resets *req;
876 struct ti_sci_msg_hdr *resp;
877 struct ti_sci_xfer *xfer;
882 return PTR_ERR(handle);
886 info = handle_to_ti_sci_info(handle);
889 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
890 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
891 sizeof(*req), sizeof(*resp));
894 dev_err(dev, "Message alloc failed(%d)\n", ret);
897 req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
899 req->resets = reset_state;
901 ret = ti_sci_do_xfer(info, xfer);
903 dev_err(dev, "Mbox send fail %d\n", ret);
907 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
909 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
912 ti_sci_put_one_xfer(&info->minfo, xfer);
918 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
920 * @handle: Pointer to TISCI handle
921 * @id: Device Identifier
922 * @reset_state: Pointer to reset state to populate
924 * Return: 0 if all went fine, else return appropriate error.
926 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
927 u32 id, u32 *reset_state)
929 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
934 * ti_sci_set_clock_state() - Set clock state helper
935 * @handle: pointer to TI SCI handle
936 * @dev_id: Device identifier this request is for
937 * @clk_id: Clock identifier for the device for this request.
938 * Each device has it's own set of clock inputs. This indexes
939 * which clock input to modify.
940 * @flags: Header flags as needed
941 * @state: State to request for the clock.
943 * Return: 0 if all went well, else returns appropriate error value.
945 static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
946 u32 dev_id, u32 clk_id,
949 struct ti_sci_info *info;
950 struct ti_sci_msg_req_set_clock_state *req;
951 struct ti_sci_msg_hdr *resp;
952 struct ti_sci_xfer *xfer;
957 return PTR_ERR(handle);
961 info = handle_to_ti_sci_info(handle);
964 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
965 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
966 sizeof(*req), sizeof(*resp));
969 dev_err(dev, "Message alloc failed(%d)\n", ret);
972 req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
973 req->dev_id = dev_id;
975 req->clk_id = clk_id;
978 req->clk_id_32 = clk_id;
980 req->request_state = state;
982 ret = ti_sci_do_xfer(info, xfer);
984 dev_err(dev, "Mbox send fail %d\n", ret);
988 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
990 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
993 ti_sci_put_one_xfer(&info->minfo, xfer);
999 * ti_sci_cmd_get_clock_state() - Get clock state helper
1000 * @handle: pointer to TI SCI handle
1001 * @dev_id: Device identifier this request is for
1002 * @clk_id: Clock identifier for the device for this request.
1003 * Each device has it's own set of clock inputs. This indexes
1004 * which clock input to modify.
1005 * @programmed_state: State requested for clock to move to
1006 * @current_state: State that the clock is currently in
1008 * Return: 0 if all went well, else returns appropriate error value.
1010 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
1011 u32 dev_id, u32 clk_id,
1012 u8 *programmed_state, u8 *current_state)
1014 struct ti_sci_info *info;
1015 struct ti_sci_msg_req_get_clock_state *req;
1016 struct ti_sci_msg_resp_get_clock_state *resp;
1017 struct ti_sci_xfer *xfer;
1022 return PTR_ERR(handle);
1026 if (!programmed_state && !current_state)
1029 info = handle_to_ti_sci_info(handle);
1032 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1033 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1034 sizeof(*req), sizeof(*resp));
1036 ret = PTR_ERR(xfer);
1037 dev_err(dev, "Message alloc failed(%d)\n", ret);
1040 req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1041 req->dev_id = dev_id;
1043 req->clk_id = clk_id;
1046 req->clk_id_32 = clk_id;
1049 ret = ti_sci_do_xfer(info, xfer);
1051 dev_err(dev, "Mbox send fail %d\n", ret);
1055 resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1057 if (!ti_sci_is_response_ack(resp)) {
1062 if (programmed_state)
1063 *programmed_state = resp->programmed_state;
1065 *current_state = resp->current_state;
1068 ti_sci_put_one_xfer(&info->minfo, xfer);
1074 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1075 * @handle: pointer to TI SCI handle
1076 * @dev_id: Device identifier this request is for
1077 * @clk_id: Clock identifier for the device for this request.
1078 * Each device has it's own set of clock inputs. This indexes
1079 * which clock input to modify.
1080 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1081 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1082 * @enable_input_term: 'true' if input termination is desired, else 'false'
1084 * Return: 0 if all went well, else returns appropriate error value.
1086 static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1087 u32 clk_id, bool needs_ssc,
1088 bool can_change_freq, bool enable_input_term)
1092 flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1093 flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1094 flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1096 return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1097 MSG_CLOCK_SW_STATE_REQ);
1101 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1102 * @handle: pointer to TI SCI handle
1103 * @dev_id: Device identifier this request is for
1104 * @clk_id: Clock identifier for the device for this request.
1105 * Each device has it's own set of clock inputs. This indexes
1106 * which clock input to modify.
1108 * NOTE: This clock must have been requested by get_clock previously.
1110 * Return: 0 if all went well, else returns appropriate error value.
1112 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1113 u32 dev_id, u32 clk_id)
1115 return ti_sci_set_clock_state(handle, dev_id, clk_id,
1116 MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
1117 MSG_CLOCK_SW_STATE_UNREQ);
1121 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1122 * @handle: pointer to TI SCI handle
1123 * @dev_id: Device identifier this request is for
1124 * @clk_id: Clock identifier for the device for this request.
1125 * Each device has it's own set of clock inputs. This indexes
1126 * which clock input to modify.
1128 * NOTE: This clock must have been requested by get_clock previously.
1130 * Return: 0 if all went well, else returns appropriate error value.
1132 static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1133 u32 dev_id, u32 clk_id)
1135 return ti_sci_set_clock_state(handle, dev_id, clk_id,
1136 MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE,
1137 MSG_CLOCK_SW_STATE_AUTO);
1141 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1142 * @handle: pointer to TI SCI handle
1143 * @dev_id: Device identifier this request is for
1144 * @clk_id: Clock identifier for the device for this request.
1145 * Each device has it's own set of clock inputs. This indexes
1146 * which clock input to modify.
1147 * @req_state: state indicating if the clock is auto managed
1149 * Return: 0 if all went well, else returns appropriate error value.
1151 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1152 u32 dev_id, u32 clk_id, bool *req_state)
1160 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1164 *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1169 * ti_sci_cmd_clk_is_on() - Is the clock ON
1170 * @handle: pointer to TI SCI handle
1171 * @dev_id: Device identifier this request is for
1172 * @clk_id: Clock identifier for the device for this request.
1173 * Each device has it's own set of clock inputs. This indexes
1174 * which clock input to modify.
1175 * @req_state: state indicating if the clock is managed by us and enabled
1176 * @curr_state: state indicating if the clock is ready for operation
1178 * Return: 0 if all went well, else returns appropriate error value.
1180 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1181 u32 clk_id, bool *req_state, bool *curr_state)
1183 u8 c_state = 0, r_state = 0;
1186 if (!req_state && !curr_state)
1189 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1190 &r_state, &c_state);
1195 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1197 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1202 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1203 * @handle: pointer to TI SCI handle
1204 * @dev_id: Device identifier this request is for
1205 * @clk_id: Clock identifier for the device for this request.
1206 * Each device has it's own set of clock inputs. This indexes
1207 * which clock input to modify.
1208 * @req_state: state indicating if the clock is managed by us and disabled
1209 * @curr_state: state indicating if the clock is NOT ready for operation
1211 * Return: 0 if all went well, else returns appropriate error value.
1213 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1214 u32 clk_id, bool *req_state, bool *curr_state)
1216 u8 c_state = 0, r_state = 0;
1219 if (!req_state && !curr_state)
1222 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1223 &r_state, &c_state);
1228 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1230 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1235 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1236 * @handle: pointer to TI SCI handle
1237 * @dev_id: Device identifier this request is for
1238 * @clk_id: Clock identifier for the device for this request.
1239 * Each device has it's own set of clock inputs. This indexes
1240 * which clock input to modify.
1241 * @parent_id: Parent clock identifier to set
1243 * Return: 0 if all went well, else returns appropriate error value.
1245 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1246 u32 dev_id, u32 clk_id, u32 parent_id)
1248 struct ti_sci_info *info;
1249 struct ti_sci_msg_req_set_clock_parent *req;
1250 struct ti_sci_msg_hdr *resp;
1251 struct ti_sci_xfer *xfer;
1256 return PTR_ERR(handle);
1260 info = handle_to_ti_sci_info(handle);
1263 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1264 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1265 sizeof(*req), sizeof(*resp));
1267 ret = PTR_ERR(xfer);
1268 dev_err(dev, "Message alloc failed(%d)\n", ret);
1271 req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1272 req->dev_id = dev_id;
1274 req->clk_id = clk_id;
1277 req->clk_id_32 = clk_id;
1279 if (parent_id < 255) {
1280 req->parent_id = parent_id;
1282 req->parent_id = 255;
1283 req->parent_id_32 = parent_id;
1286 ret = ti_sci_do_xfer(info, xfer);
1288 dev_err(dev, "Mbox send fail %d\n", ret);
1292 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1294 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1297 ti_sci_put_one_xfer(&info->minfo, xfer);
1303 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1304 * @handle: pointer to TI SCI handle
1305 * @dev_id: Device identifier this request is for
1306 * @clk_id: Clock identifier for the device for this request.
1307 * Each device has it's own set of clock inputs. This indexes
1308 * which clock input to modify.
1309 * @parent_id: Current clock parent
1311 * Return: 0 if all went well, else returns appropriate error value.
1313 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1314 u32 dev_id, u32 clk_id, u32 *parent_id)
1316 struct ti_sci_info *info;
1317 struct ti_sci_msg_req_get_clock_parent *req;
1318 struct ti_sci_msg_resp_get_clock_parent *resp;
1319 struct ti_sci_xfer *xfer;
1324 return PTR_ERR(handle);
1325 if (!handle || !parent_id)
1328 info = handle_to_ti_sci_info(handle);
1331 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1332 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1333 sizeof(*req), sizeof(*resp));
1335 ret = PTR_ERR(xfer);
1336 dev_err(dev, "Message alloc failed(%d)\n", ret);
1339 req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1340 req->dev_id = dev_id;
1342 req->clk_id = clk_id;
1345 req->clk_id_32 = clk_id;
1348 ret = ti_sci_do_xfer(info, xfer);
1350 dev_err(dev, "Mbox send fail %d\n", ret);
1354 resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1356 if (!ti_sci_is_response_ack(resp)) {
1359 if (resp->parent_id < 255)
1360 *parent_id = resp->parent_id;
1362 *parent_id = resp->parent_id_32;
1366 ti_sci_put_one_xfer(&info->minfo, xfer);
1372 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1373 * @handle: pointer to TI SCI handle
1374 * @dev_id: Device identifier this request is for
1375 * @clk_id: Clock identifier for the device for this request.
1376 * Each device has it's own set of clock inputs. This indexes
1377 * which clock input to modify.
1378 * @num_parents: Returns he number of parents to the current clock.
1380 * Return: 0 if all went well, else returns appropriate error value.
1382 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1383 u32 dev_id, u32 clk_id,
1386 struct ti_sci_info *info;
1387 struct ti_sci_msg_req_get_clock_num_parents *req;
1388 struct ti_sci_msg_resp_get_clock_num_parents *resp;
1389 struct ti_sci_xfer *xfer;
1394 return PTR_ERR(handle);
1395 if (!handle || !num_parents)
1398 info = handle_to_ti_sci_info(handle);
1401 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1402 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1403 sizeof(*req), sizeof(*resp));
1405 ret = PTR_ERR(xfer);
1406 dev_err(dev, "Message alloc failed(%d)\n", ret);
1409 req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1410 req->dev_id = dev_id;
1412 req->clk_id = clk_id;
1415 req->clk_id_32 = clk_id;
1418 ret = ti_sci_do_xfer(info, xfer);
1420 dev_err(dev, "Mbox send fail %d\n", ret);
1424 resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1426 if (!ti_sci_is_response_ack(resp)) {
1429 if (resp->num_parents < 255)
1430 *num_parents = resp->num_parents;
1432 *num_parents = resp->num_parents_32;
1436 ti_sci_put_one_xfer(&info->minfo, xfer);
1442 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1443 * @handle: pointer to TI SCI handle
1444 * @dev_id: Device identifier this request is for
1445 * @clk_id: Clock identifier for the device for this request.
1446 * Each device has it's own set of clock inputs. This indexes
1447 * which clock input to modify.
1448 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1449 * allowable programmed frequency and does not account for clock
1450 * tolerances and jitter.
1451 * @target_freq: The target clock frequency in Hz. A frequency will be
1452 * processed as close to this target frequency as possible.
1453 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1454 * allowable programmed frequency and does not account for clock
1455 * tolerances and jitter.
1456 * @match_freq: Frequency match in Hz response.
1458 * Return: 0 if all went well, else returns appropriate error value.
1460 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1461 u32 dev_id, u32 clk_id, u64 min_freq,
1462 u64 target_freq, u64 max_freq,
1465 struct ti_sci_info *info;
1466 struct ti_sci_msg_req_query_clock_freq *req;
1467 struct ti_sci_msg_resp_query_clock_freq *resp;
1468 struct ti_sci_xfer *xfer;
1473 return PTR_ERR(handle);
1474 if (!handle || !match_freq)
1477 info = handle_to_ti_sci_info(handle);
1480 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1481 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1482 sizeof(*req), sizeof(*resp));
1484 ret = PTR_ERR(xfer);
1485 dev_err(dev, "Message alloc failed(%d)\n", ret);
1488 req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1489 req->dev_id = dev_id;
1491 req->clk_id = clk_id;
1494 req->clk_id_32 = clk_id;
1496 req->min_freq_hz = min_freq;
1497 req->target_freq_hz = target_freq;
1498 req->max_freq_hz = max_freq;
1500 ret = ti_sci_do_xfer(info, xfer);
1502 dev_err(dev, "Mbox send fail %d\n", ret);
1506 resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1508 if (!ti_sci_is_response_ack(resp))
1511 *match_freq = resp->freq_hz;
1514 ti_sci_put_one_xfer(&info->minfo, xfer);
1520 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1521 * @handle: pointer to TI SCI handle
1522 * @dev_id: Device identifier this request is for
1523 * @clk_id: Clock identifier for the device for this request.
1524 * Each device has it's own set of clock inputs. This indexes
1525 * which clock input to modify.
1526 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1527 * allowable programmed frequency and does not account for clock
1528 * tolerances and jitter.
1529 * @target_freq: The target clock frequency in Hz. A frequency will be
1530 * processed as close to this target frequency as possible.
1531 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1532 * allowable programmed frequency and does not account for clock
1533 * tolerances and jitter.
1535 * Return: 0 if all went well, else returns appropriate error value.
1537 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1538 u32 dev_id, u32 clk_id, u64 min_freq,
1539 u64 target_freq, u64 max_freq)
1541 struct ti_sci_info *info;
1542 struct ti_sci_msg_req_set_clock_freq *req;
1543 struct ti_sci_msg_hdr *resp;
1544 struct ti_sci_xfer *xfer;
1549 return PTR_ERR(handle);
1553 info = handle_to_ti_sci_info(handle);
1556 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1557 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1558 sizeof(*req), sizeof(*resp));
1560 ret = PTR_ERR(xfer);
1561 dev_err(dev, "Message alloc failed(%d)\n", ret);
1564 req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1565 req->dev_id = dev_id;
1567 req->clk_id = clk_id;
1570 req->clk_id_32 = clk_id;
1572 req->min_freq_hz = min_freq;
1573 req->target_freq_hz = target_freq;
1574 req->max_freq_hz = max_freq;
1576 ret = ti_sci_do_xfer(info, xfer);
1578 dev_err(dev, "Mbox send fail %d\n", ret);
1582 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1584 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1587 ti_sci_put_one_xfer(&info->minfo, xfer);
1593 * ti_sci_cmd_clk_get_freq() - Get current frequency
1594 * @handle: pointer to TI SCI handle
1595 * @dev_id: Device identifier this request is for
1596 * @clk_id: Clock identifier for the device for this request.
1597 * Each device has it's own set of clock inputs. This indexes
1598 * which clock input to modify.
1599 * @freq: Currently frequency in Hz
1601 * Return: 0 if all went well, else returns appropriate error value.
1603 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1604 u32 dev_id, u32 clk_id, u64 *freq)
1606 struct ti_sci_info *info;
1607 struct ti_sci_msg_req_get_clock_freq *req;
1608 struct ti_sci_msg_resp_get_clock_freq *resp;
1609 struct ti_sci_xfer *xfer;
1614 return PTR_ERR(handle);
1615 if (!handle || !freq)
1618 info = handle_to_ti_sci_info(handle);
1621 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1622 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1623 sizeof(*req), sizeof(*resp));
1625 ret = PTR_ERR(xfer);
1626 dev_err(dev, "Message alloc failed(%d)\n", ret);
1629 req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1630 req->dev_id = dev_id;
1632 req->clk_id = clk_id;
1635 req->clk_id_32 = clk_id;
1638 ret = ti_sci_do_xfer(info, xfer);
1640 dev_err(dev, "Mbox send fail %d\n", ret);
1644 resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1646 if (!ti_sci_is_response_ack(resp))
1649 *freq = resp->freq_hz;
1652 ti_sci_put_one_xfer(&info->minfo, xfer);
1657 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1659 struct ti_sci_info *info;
1660 struct ti_sci_msg_req_reboot *req;
1661 struct ti_sci_msg_hdr *resp;
1662 struct ti_sci_xfer *xfer;
1667 return PTR_ERR(handle);
1671 info = handle_to_ti_sci_info(handle);
1674 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1675 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1676 sizeof(*req), sizeof(*resp));
1678 ret = PTR_ERR(xfer);
1679 dev_err(dev, "Message alloc failed(%d)\n", ret);
1682 req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1684 ret = ti_sci_do_xfer(info, xfer);
1686 dev_err(dev, "Mbox send fail %d\n", ret);
1690 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1692 if (!ti_sci_is_response_ack(resp))
1698 ti_sci_put_one_xfer(&info->minfo, xfer);
1704 * ti_sci_get_resource_range - Helper to get a range of resources assigned
1705 * to a host. Resource is uniquely identified by
1707 * @handle: Pointer to TISCI handle.
1708 * @dev_id: TISCI device ID.
1709 * @subtype: Resource assignment subtype that is being requested
1710 * from the given device.
1711 * @s_host: Host processor ID to which the resources are allocated
1712 * @desc: Pointer to ti_sci_resource_desc to be updated with the
1713 * resource range start index and number of resources
1715 * Return: 0 if all went fine, else return appropriate error.
1717 static int ti_sci_get_resource_range(const struct ti_sci_handle *handle,
1718 u32 dev_id, u8 subtype, u8 s_host,
1719 struct ti_sci_resource_desc *desc)
1721 struct ti_sci_msg_resp_get_resource_range *resp;
1722 struct ti_sci_msg_req_get_resource_range *req;
1723 struct ti_sci_xfer *xfer;
1724 struct ti_sci_info *info;
1729 return PTR_ERR(handle);
1730 if (!handle || !desc)
1733 info = handle_to_ti_sci_info(handle);
1736 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_RESOURCE_RANGE,
1737 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1738 sizeof(*req), sizeof(*resp));
1740 ret = PTR_ERR(xfer);
1741 dev_err(dev, "Message alloc failed(%d)\n", ret);
1745 req = (struct ti_sci_msg_req_get_resource_range *)xfer->xfer_buf;
1746 req->secondary_host = s_host;
1747 req->type = dev_id & MSG_RM_RESOURCE_TYPE_MASK;
1748 req->subtype = subtype & MSG_RM_RESOURCE_SUBTYPE_MASK;
1750 ret = ti_sci_do_xfer(info, xfer);
1752 dev_err(dev, "Mbox send fail %d\n", ret);
1756 resp = (struct ti_sci_msg_resp_get_resource_range *)xfer->xfer_buf;
1758 if (!ti_sci_is_response_ack(resp)) {
1760 } else if (!resp->range_num && !resp->range_num_sec) {
1761 /* Neither of the two resource range is valid */
1764 desc->start = resp->range_start;
1765 desc->num = resp->range_num;
1766 desc->start_sec = resp->range_start_sec;
1767 desc->num_sec = resp->range_num_sec;
1771 ti_sci_put_one_xfer(&info->minfo, xfer);
1777 * ti_sci_cmd_get_resource_range - Get a range of resources assigned to host
1778 * that is same as ti sci interface host.
1779 * @handle: Pointer to TISCI handle.
1780 * @dev_id: TISCI device ID.
1781 * @subtype: Resource assignment subtype that is being requested
1782 * from the given device.
1783 * @desc: Pointer to ti_sci_resource_desc to be updated with the
1784 * resource range start index and number of resources
1786 * Return: 0 if all went fine, else return appropriate error.
1788 static int ti_sci_cmd_get_resource_range(const struct ti_sci_handle *handle,
1789 u32 dev_id, u8 subtype,
1790 struct ti_sci_resource_desc *desc)
1792 return ti_sci_get_resource_range(handle, dev_id, subtype,
1793 TI_SCI_IRQ_SECONDARY_HOST_INVALID,
1798 * ti_sci_cmd_get_resource_range_from_shost - Get a range of resources
1799 * assigned to a specified host.
1800 * @handle: Pointer to TISCI handle.
1801 * @dev_id: TISCI device ID.
1802 * @subtype: Resource assignment subtype that is being requested
1803 * from the given device.
1804 * @s_host: Host processor ID to which the resources are allocated
1805 * @desc: Pointer to ti_sci_resource_desc to be updated with the
1806 * resource range start index and number of resources
1808 * Return: 0 if all went fine, else return appropriate error.
1811 int ti_sci_cmd_get_resource_range_from_shost(const struct ti_sci_handle *handle,
1812 u32 dev_id, u8 subtype, u8 s_host,
1813 struct ti_sci_resource_desc *desc)
1815 return ti_sci_get_resource_range(handle, dev_id, subtype, s_host, desc);
1819 * ti_sci_manage_irq() - Helper api to configure/release the irq route between
1820 * the requested source and destination
1821 * @handle: Pointer to TISCI handle.
1822 * @valid_params: Bit fields defining the validity of certain params
1823 * @src_id: Device ID of the IRQ source
1824 * @src_index: IRQ source index within the source device
1825 * @dst_id: Device ID of the IRQ destination
1826 * @dst_host_irq: IRQ number of the destination device
1827 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1828 * @vint: Virtual interrupt to be used within the IA
1829 * @global_event: Global event number to be used for the requesting event
1830 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1831 * @s_host: Secondary host ID to which the irq/event is being
1833 * @type: Request type irq set or release.
1835 * Return: 0 if all went fine, else return appropriate error.
1837 static int ti_sci_manage_irq(const struct ti_sci_handle *handle,
1838 u32 valid_params, u16 src_id, u16 src_index,
1839 u16 dst_id, u16 dst_host_irq, u16 ia_id, u16 vint,
1840 u16 global_event, u8 vint_status_bit, u8 s_host,
1843 struct ti_sci_msg_req_manage_irq *req;
1844 struct ti_sci_msg_hdr *resp;
1845 struct ti_sci_xfer *xfer;
1846 struct ti_sci_info *info;
1851 return PTR_ERR(handle);
1855 info = handle_to_ti_sci_info(handle);
1858 xfer = ti_sci_get_one_xfer(info, type, TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1859 sizeof(*req), sizeof(*resp));
1861 ret = PTR_ERR(xfer);
1862 dev_err(dev, "Message alloc failed(%d)\n", ret);
1865 req = (struct ti_sci_msg_req_manage_irq *)xfer->xfer_buf;
1866 req->valid_params = valid_params;
1867 req->src_id = src_id;
1868 req->src_index = src_index;
1869 req->dst_id = dst_id;
1870 req->dst_host_irq = dst_host_irq;
1873 req->global_event = global_event;
1874 req->vint_status_bit = vint_status_bit;
1875 req->secondary_host = s_host;
1877 ret = ti_sci_do_xfer(info, xfer);
1879 dev_err(dev, "Mbox send fail %d\n", ret);
1883 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1885 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1888 ti_sci_put_one_xfer(&info->minfo, xfer);
1894 * ti_sci_set_irq() - Helper api to configure the irq route between the
1895 * requested source and destination
1896 * @handle: Pointer to TISCI handle.
1897 * @valid_params: Bit fields defining the validity of certain params
1898 * @src_id: Device ID of the IRQ source
1899 * @src_index: IRQ source index within the source device
1900 * @dst_id: Device ID of the IRQ destination
1901 * @dst_host_irq: IRQ number of the destination device
1902 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1903 * @vint: Virtual interrupt to be used within the IA
1904 * @global_event: Global event number to be used for the requesting event
1905 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1906 * @s_host: Secondary host ID to which the irq/event is being
1909 * Return: 0 if all went fine, else return appropriate error.
1911 static int ti_sci_set_irq(const struct ti_sci_handle *handle, u32 valid_params,
1912 u16 src_id, u16 src_index, u16 dst_id,
1913 u16 dst_host_irq, u16 ia_id, u16 vint,
1914 u16 global_event, u8 vint_status_bit, u8 s_host)
1916 pr_debug("%s: IRQ set with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1917 __func__, valid_params, src_id, src_index,
1918 dst_id, dst_host_irq, ia_id, vint, global_event,
1921 return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1922 dst_id, dst_host_irq, ia_id, vint,
1923 global_event, vint_status_bit, s_host,
1924 TI_SCI_MSG_SET_IRQ);
1928 * ti_sci_free_irq() - Helper api to free the irq route between the
1929 * requested source and destination
1930 * @handle: Pointer to TISCI handle.
1931 * @valid_params: Bit fields defining the validity of certain params
1932 * @src_id: Device ID of the IRQ source
1933 * @src_index: IRQ source index within the source device
1934 * @dst_id: Device ID of the IRQ destination
1935 * @dst_host_irq: IRQ number of the destination device
1936 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1937 * @vint: Virtual interrupt to be used within the IA
1938 * @global_event: Global event number to be used for the requesting event
1939 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1940 * @s_host: Secondary host ID to which the irq/event is being
1943 * Return: 0 if all went fine, else return appropriate error.
1945 static int ti_sci_free_irq(const struct ti_sci_handle *handle, u32 valid_params,
1946 u16 src_id, u16 src_index, u16 dst_id,
1947 u16 dst_host_irq, u16 ia_id, u16 vint,
1948 u16 global_event, u8 vint_status_bit, u8 s_host)
1950 pr_debug("%s: IRQ release with valid_params = 0x%x from src = %d, index = %d, to dst = %d, irq = %d,via ia_id = %d, vint = %d, global event = %d,status_bit = %d\n",
1951 __func__, valid_params, src_id, src_index,
1952 dst_id, dst_host_irq, ia_id, vint, global_event,
1955 return ti_sci_manage_irq(handle, valid_params, src_id, src_index,
1956 dst_id, dst_host_irq, ia_id, vint,
1957 global_event, vint_status_bit, s_host,
1958 TI_SCI_MSG_FREE_IRQ);
1962 * ti_sci_cmd_set_irq() - Configure a host irq route between the requested
1963 * source and destination.
1964 * @handle: Pointer to TISCI handle.
1965 * @src_id: Device ID of the IRQ source
1966 * @src_index: IRQ source index within the source device
1967 * @dst_id: Device ID of the IRQ destination
1968 * @dst_host_irq: IRQ number of the destination device
1970 * Return: 0 if all went fine, else return appropriate error.
1972 static int ti_sci_cmd_set_irq(const struct ti_sci_handle *handle, u16 src_id,
1973 u16 src_index, u16 dst_id, u16 dst_host_irq)
1975 u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
1977 return ti_sci_set_irq(handle, valid_params, src_id, src_index, dst_id,
1978 dst_host_irq, 0, 0, 0, 0, 0);
1982 * ti_sci_cmd_set_event_map() - Configure an event based irq route between the
1983 * requested source and Interrupt Aggregator.
1984 * @handle: Pointer to TISCI handle.
1985 * @src_id: Device ID of the IRQ source
1986 * @src_index: IRQ source index within the source device
1987 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
1988 * @vint: Virtual interrupt to be used within the IA
1989 * @global_event: Global event number to be used for the requesting event
1990 * @vint_status_bit: Virtual interrupt status bit to be used for the event
1992 * Return: 0 if all went fine, else return appropriate error.
1994 static int ti_sci_cmd_set_event_map(const struct ti_sci_handle *handle,
1995 u16 src_id, u16 src_index, u16 ia_id,
1996 u16 vint, u16 global_event,
1999 u32 valid_params = MSG_FLAG_IA_ID_VALID | MSG_FLAG_VINT_VALID |
2000 MSG_FLAG_GLB_EVNT_VALID |
2001 MSG_FLAG_VINT_STS_BIT_VALID;
2003 return ti_sci_set_irq(handle, valid_params, src_id, src_index, 0, 0,
2004 ia_id, vint, global_event, vint_status_bit, 0);
2008 * ti_sci_cmd_free_irq() - Free a host irq route between the between the
2009 * requested source and destination.
2010 * @handle: Pointer to TISCI handle.
2011 * @src_id: Device ID of the IRQ source
2012 * @src_index: IRQ source index within the source device
2013 * @dst_id: Device ID of the IRQ destination
2014 * @dst_host_irq: IRQ number of the destination device
2016 * Return: 0 if all went fine, else return appropriate error.
2018 static int ti_sci_cmd_free_irq(const struct ti_sci_handle *handle, u16 src_id,
2019 u16 src_index, u16 dst_id, u16 dst_host_irq)
2021 u32 valid_params = MSG_FLAG_DST_ID_VALID | MSG_FLAG_DST_HOST_IRQ_VALID;
2023 return ti_sci_free_irq(handle, valid_params, src_id, src_index, dst_id,
2024 dst_host_irq, 0, 0, 0, 0, 0);
2028 * ti_sci_cmd_free_event_map() - Free an event map between the requested source
2029 * and Interrupt Aggregator.
2030 * @handle: Pointer to TISCI handle.
2031 * @src_id: Device ID of the IRQ source
2032 * @src_index: IRQ source index within the source device
2033 * @ia_id: Device ID of the IA, if the IRQ flows through this IA
2034 * @vint: Virtual interrupt to be used within the IA
2035 * @global_event: Global event number to be used for the requesting event
2036 * @vint_status_bit: Virtual interrupt status bit to be used for the event
2038 * Return: 0 if all went fine, else return appropriate error.
2040 static int ti_sci_cmd_free_event_map(const struct ti_sci_handle *handle,
2041 u16 src_id, u16 src_index, u16 ia_id,
2042 u16 vint, u16 global_event,
2045 u32 valid_params = MSG_FLAG_IA_ID_VALID |
2046 MSG_FLAG_VINT_VALID | MSG_FLAG_GLB_EVNT_VALID |
2047 MSG_FLAG_VINT_STS_BIT_VALID;
2049 return ti_sci_free_irq(handle, valid_params, src_id, src_index, 0, 0,
2050 ia_id, vint, global_event, vint_status_bit, 0);
2054 * ti_sci_cmd_rm_ring_cfg() - Configure a NAVSS ring
2055 * @handle: Pointer to TI SCI handle.
2056 * @params: Pointer to ti_sci_msg_rm_ring_cfg ring config structure
2058 * Return: 0 if all went well, else returns appropriate error value.
2060 * See @ti_sci_msg_rm_ring_cfg and @ti_sci_msg_rm_ring_cfg_req for
2063 static int ti_sci_cmd_rm_ring_cfg(const struct ti_sci_handle *handle,
2064 const struct ti_sci_msg_rm_ring_cfg *params)
2066 struct ti_sci_msg_rm_ring_cfg_req *req;
2067 struct ti_sci_msg_hdr *resp;
2068 struct ti_sci_xfer *xfer;
2069 struct ti_sci_info *info;
2073 if (IS_ERR_OR_NULL(handle))
2076 info = handle_to_ti_sci_info(handle);
2079 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_RING_CFG,
2080 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2081 sizeof(*req), sizeof(*resp));
2083 ret = PTR_ERR(xfer);
2084 dev_err(dev, "RM_RA:Message config failed(%d)\n", ret);
2087 req = (struct ti_sci_msg_rm_ring_cfg_req *)xfer->xfer_buf;
2088 req->valid_params = params->valid_params;
2089 req->nav_id = params->nav_id;
2090 req->index = params->index;
2091 req->addr_lo = params->addr_lo;
2092 req->addr_hi = params->addr_hi;
2093 req->count = params->count;
2094 req->mode = params->mode;
2095 req->size = params->size;
2096 req->order_id = params->order_id;
2097 req->virtid = params->virtid;
2098 req->asel = params->asel;
2100 ret = ti_sci_do_xfer(info, xfer);
2102 dev_err(dev, "RM_RA:Mbox config send fail %d\n", ret);
2106 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2107 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2110 ti_sci_put_one_xfer(&info->minfo, xfer);
2111 dev_dbg(dev, "RM_RA:config ring %u ret:%d\n", params->index, ret);
2116 * ti_sci_cmd_rm_psil_pair() - Pair PSI-L source to destination thread
2117 * @handle: Pointer to TI SCI handle.
2118 * @nav_id: Device ID of Navigator Subsystem which should be used for
2120 * @src_thread: Source PSI-L thread ID
2121 * @dst_thread: Destination PSI-L thread ID
2123 * Return: 0 if all went well, else returns appropriate error value.
2125 static int ti_sci_cmd_rm_psil_pair(const struct ti_sci_handle *handle,
2126 u32 nav_id, u32 src_thread, u32 dst_thread)
2128 struct ti_sci_msg_psil_pair *req;
2129 struct ti_sci_msg_hdr *resp;
2130 struct ti_sci_xfer *xfer;
2131 struct ti_sci_info *info;
2136 return PTR_ERR(handle);
2140 info = handle_to_ti_sci_info(handle);
2143 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_PAIR,
2144 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2145 sizeof(*req), sizeof(*resp));
2147 ret = PTR_ERR(xfer);
2148 dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2151 req = (struct ti_sci_msg_psil_pair *)xfer->xfer_buf;
2152 req->nav_id = nav_id;
2153 req->src_thread = src_thread;
2154 req->dst_thread = dst_thread;
2156 ret = ti_sci_do_xfer(info, xfer);
2158 dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2162 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2163 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2166 ti_sci_put_one_xfer(&info->minfo, xfer);
2172 * ti_sci_cmd_rm_psil_unpair() - Unpair PSI-L source from destination thread
2173 * @handle: Pointer to TI SCI handle.
2174 * @nav_id: Device ID of Navigator Subsystem which should be used for
2176 * @src_thread: Source PSI-L thread ID
2177 * @dst_thread: Destination PSI-L thread ID
2179 * Return: 0 if all went well, else returns appropriate error value.
2181 static int ti_sci_cmd_rm_psil_unpair(const struct ti_sci_handle *handle,
2182 u32 nav_id, u32 src_thread, u32 dst_thread)
2184 struct ti_sci_msg_psil_unpair *req;
2185 struct ti_sci_msg_hdr *resp;
2186 struct ti_sci_xfer *xfer;
2187 struct ti_sci_info *info;
2192 return PTR_ERR(handle);
2196 info = handle_to_ti_sci_info(handle);
2199 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_RM_PSIL_UNPAIR,
2200 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2201 sizeof(*req), sizeof(*resp));
2203 ret = PTR_ERR(xfer);
2204 dev_err(dev, "RM_PSIL:Message reconfig failed(%d)\n", ret);
2207 req = (struct ti_sci_msg_psil_unpair *)xfer->xfer_buf;
2208 req->nav_id = nav_id;
2209 req->src_thread = src_thread;
2210 req->dst_thread = dst_thread;
2212 ret = ti_sci_do_xfer(info, xfer);
2214 dev_err(dev, "RM_PSIL:Mbox send fail %d\n", ret);
2218 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2219 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2222 ti_sci_put_one_xfer(&info->minfo, xfer);
2228 * ti_sci_cmd_rm_udmap_tx_ch_cfg() - Configure a UDMAP TX channel
2229 * @handle: Pointer to TI SCI handle.
2230 * @params: Pointer to ti_sci_msg_rm_udmap_tx_ch_cfg TX channel config
2233 * Return: 0 if all went well, else returns appropriate error value.
2235 * See @ti_sci_msg_rm_udmap_tx_ch_cfg and @ti_sci_msg_rm_udmap_tx_ch_cfg_req for
2238 static int ti_sci_cmd_rm_udmap_tx_ch_cfg(const struct ti_sci_handle *handle,
2239 const struct ti_sci_msg_rm_udmap_tx_ch_cfg *params)
2241 struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *req;
2242 struct ti_sci_msg_hdr *resp;
2243 struct ti_sci_xfer *xfer;
2244 struct ti_sci_info *info;
2248 if (IS_ERR_OR_NULL(handle))
2251 info = handle_to_ti_sci_info(handle);
2254 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_TX_CH_CFG,
2255 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2256 sizeof(*req), sizeof(*resp));
2258 ret = PTR_ERR(xfer);
2259 dev_err(dev, "Message TX_CH_CFG alloc failed(%d)\n", ret);
2262 req = (struct ti_sci_msg_rm_udmap_tx_ch_cfg_req *)xfer->xfer_buf;
2263 req->valid_params = params->valid_params;
2264 req->nav_id = params->nav_id;
2265 req->index = params->index;
2266 req->tx_pause_on_err = params->tx_pause_on_err;
2267 req->tx_filt_einfo = params->tx_filt_einfo;
2268 req->tx_filt_pswords = params->tx_filt_pswords;
2269 req->tx_atype = params->tx_atype;
2270 req->tx_chan_type = params->tx_chan_type;
2271 req->tx_supr_tdpkt = params->tx_supr_tdpkt;
2272 req->tx_fetch_size = params->tx_fetch_size;
2273 req->tx_credit_count = params->tx_credit_count;
2274 req->txcq_qnum = params->txcq_qnum;
2275 req->tx_priority = params->tx_priority;
2276 req->tx_qos = params->tx_qos;
2277 req->tx_orderid = params->tx_orderid;
2278 req->fdepth = params->fdepth;
2279 req->tx_sched_priority = params->tx_sched_priority;
2280 req->tx_burst_size = params->tx_burst_size;
2281 req->tx_tdtype = params->tx_tdtype;
2282 req->extended_ch_type = params->extended_ch_type;
2284 ret = ti_sci_do_xfer(info, xfer);
2286 dev_err(dev, "Mbox send TX_CH_CFG fail %d\n", ret);
2290 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2291 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2294 ti_sci_put_one_xfer(&info->minfo, xfer);
2295 dev_dbg(dev, "TX_CH_CFG: chn %u ret:%u\n", params->index, ret);
2300 * ti_sci_cmd_rm_udmap_rx_ch_cfg() - Configure a UDMAP RX channel
2301 * @handle: Pointer to TI SCI handle.
2302 * @params: Pointer to ti_sci_msg_rm_udmap_rx_ch_cfg RX channel config
2305 * Return: 0 if all went well, else returns appropriate error value.
2307 * See @ti_sci_msg_rm_udmap_rx_ch_cfg and @ti_sci_msg_rm_udmap_rx_ch_cfg_req for
2310 static int ti_sci_cmd_rm_udmap_rx_ch_cfg(const struct ti_sci_handle *handle,
2311 const struct ti_sci_msg_rm_udmap_rx_ch_cfg *params)
2313 struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *req;
2314 struct ti_sci_msg_hdr *resp;
2315 struct ti_sci_xfer *xfer;
2316 struct ti_sci_info *info;
2320 if (IS_ERR_OR_NULL(handle))
2323 info = handle_to_ti_sci_info(handle);
2326 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_RX_CH_CFG,
2327 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2328 sizeof(*req), sizeof(*resp));
2330 ret = PTR_ERR(xfer);
2331 dev_err(dev, "Message RX_CH_CFG alloc failed(%d)\n", ret);
2334 req = (struct ti_sci_msg_rm_udmap_rx_ch_cfg_req *)xfer->xfer_buf;
2335 req->valid_params = params->valid_params;
2336 req->nav_id = params->nav_id;
2337 req->index = params->index;
2338 req->rx_fetch_size = params->rx_fetch_size;
2339 req->rxcq_qnum = params->rxcq_qnum;
2340 req->rx_priority = params->rx_priority;
2341 req->rx_qos = params->rx_qos;
2342 req->rx_orderid = params->rx_orderid;
2343 req->rx_sched_priority = params->rx_sched_priority;
2344 req->flowid_start = params->flowid_start;
2345 req->flowid_cnt = params->flowid_cnt;
2346 req->rx_pause_on_err = params->rx_pause_on_err;
2347 req->rx_atype = params->rx_atype;
2348 req->rx_chan_type = params->rx_chan_type;
2349 req->rx_ignore_short = params->rx_ignore_short;
2350 req->rx_ignore_long = params->rx_ignore_long;
2351 req->rx_burst_size = params->rx_burst_size;
2353 ret = ti_sci_do_xfer(info, xfer);
2355 dev_err(dev, "Mbox send RX_CH_CFG fail %d\n", ret);
2359 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2360 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2363 ti_sci_put_one_xfer(&info->minfo, xfer);
2364 dev_dbg(dev, "RX_CH_CFG: chn %u ret:%d\n", params->index, ret);
2369 * ti_sci_cmd_rm_udmap_rx_flow_cfg() - Configure UDMAP RX FLOW
2370 * @handle: Pointer to TI SCI handle.
2371 * @params: Pointer to ti_sci_msg_rm_udmap_flow_cfg RX FLOW config
2374 * Return: 0 if all went well, else returns appropriate error value.
2376 * See @ti_sci_msg_rm_udmap_flow_cfg and @ti_sci_msg_rm_udmap_flow_cfg_req for
2379 static int ti_sci_cmd_rm_udmap_rx_flow_cfg(const struct ti_sci_handle *handle,
2380 const struct ti_sci_msg_rm_udmap_flow_cfg *params)
2382 struct ti_sci_msg_rm_udmap_flow_cfg_req *req;
2383 struct ti_sci_msg_hdr *resp;
2384 struct ti_sci_xfer *xfer;
2385 struct ti_sci_info *info;
2389 if (IS_ERR_OR_NULL(handle))
2392 info = handle_to_ti_sci_info(handle);
2395 xfer = ti_sci_get_one_xfer(info, TISCI_MSG_RM_UDMAP_FLOW_CFG,
2396 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2397 sizeof(*req), sizeof(*resp));
2399 ret = PTR_ERR(xfer);
2400 dev_err(dev, "RX_FL_CFG: Message alloc failed(%d)\n", ret);
2403 req = (struct ti_sci_msg_rm_udmap_flow_cfg_req *)xfer->xfer_buf;
2404 req->valid_params = params->valid_params;
2405 req->nav_id = params->nav_id;
2406 req->flow_index = params->flow_index;
2407 req->rx_einfo_present = params->rx_einfo_present;
2408 req->rx_psinfo_present = params->rx_psinfo_present;
2409 req->rx_error_handling = params->rx_error_handling;
2410 req->rx_desc_type = params->rx_desc_type;
2411 req->rx_sop_offset = params->rx_sop_offset;
2412 req->rx_dest_qnum = params->rx_dest_qnum;
2413 req->rx_src_tag_hi = params->rx_src_tag_hi;
2414 req->rx_src_tag_lo = params->rx_src_tag_lo;
2415 req->rx_dest_tag_hi = params->rx_dest_tag_hi;
2416 req->rx_dest_tag_lo = params->rx_dest_tag_lo;
2417 req->rx_src_tag_hi_sel = params->rx_src_tag_hi_sel;
2418 req->rx_src_tag_lo_sel = params->rx_src_tag_lo_sel;
2419 req->rx_dest_tag_hi_sel = params->rx_dest_tag_hi_sel;
2420 req->rx_dest_tag_lo_sel = params->rx_dest_tag_lo_sel;
2421 req->rx_fdq0_sz0_qnum = params->rx_fdq0_sz0_qnum;
2422 req->rx_fdq1_qnum = params->rx_fdq1_qnum;
2423 req->rx_fdq2_qnum = params->rx_fdq2_qnum;
2424 req->rx_fdq3_qnum = params->rx_fdq3_qnum;
2425 req->rx_ps_location = params->rx_ps_location;
2427 ret = ti_sci_do_xfer(info, xfer);
2429 dev_err(dev, "RX_FL_CFG: Mbox send fail %d\n", ret);
2433 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
2434 ret = ti_sci_is_response_ack(resp) ? 0 : -EINVAL;
2437 ti_sci_put_one_xfer(&info->minfo, xfer);
2438 dev_dbg(info->dev, "RX_FL_CFG: %u ret:%d\n", params->flow_index, ret);
2443 * ti_sci_cmd_proc_request() - Command to request a physical processor control
2444 * @handle: Pointer to TI SCI handle
2445 * @proc_id: Processor ID this request is for
2447 * Return: 0 if all went well, else returns appropriate error value.
2449 static int ti_sci_cmd_proc_request(const struct ti_sci_handle *handle,
2452 struct ti_sci_msg_req_proc_request *req;
2453 struct ti_sci_msg_hdr *resp;
2454 struct ti_sci_info *info;
2455 struct ti_sci_xfer *xfer;
2462 return PTR_ERR(handle);
2464 info = handle_to_ti_sci_info(handle);
2467 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_REQUEST,
2468 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2469 sizeof(*req), sizeof(*resp));
2471 ret = PTR_ERR(xfer);
2472 dev_err(dev, "Message alloc failed(%d)\n", ret);
2475 req = (struct ti_sci_msg_req_proc_request *)xfer->xfer_buf;
2476 req->processor_id = proc_id;
2478 ret = ti_sci_do_xfer(info, xfer);
2480 dev_err(dev, "Mbox send fail %d\n", ret);
2484 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2486 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2489 ti_sci_put_one_xfer(&info->minfo, xfer);
2495 * ti_sci_cmd_proc_release() - Command to release a physical processor control
2496 * @handle: Pointer to TI SCI handle
2497 * @proc_id: Processor ID this request is for
2499 * Return: 0 if all went well, else returns appropriate error value.
2501 static int ti_sci_cmd_proc_release(const struct ti_sci_handle *handle,
2504 struct ti_sci_msg_req_proc_release *req;
2505 struct ti_sci_msg_hdr *resp;
2506 struct ti_sci_info *info;
2507 struct ti_sci_xfer *xfer;
2514 return PTR_ERR(handle);
2516 info = handle_to_ti_sci_info(handle);
2519 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_RELEASE,
2520 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2521 sizeof(*req), sizeof(*resp));
2523 ret = PTR_ERR(xfer);
2524 dev_err(dev, "Message alloc failed(%d)\n", ret);
2527 req = (struct ti_sci_msg_req_proc_release *)xfer->xfer_buf;
2528 req->processor_id = proc_id;
2530 ret = ti_sci_do_xfer(info, xfer);
2532 dev_err(dev, "Mbox send fail %d\n", ret);
2536 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2538 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2541 ti_sci_put_one_xfer(&info->minfo, xfer);
2547 * ti_sci_cmd_proc_handover() - Command to handover a physical processor
2548 * control to a host in the processor's access
2550 * @handle: Pointer to TI SCI handle
2551 * @proc_id: Processor ID this request is for
2552 * @host_id: Host ID to get the control of the processor
2554 * Return: 0 if all went well, else returns appropriate error value.
2556 static int ti_sci_cmd_proc_handover(const struct ti_sci_handle *handle,
2557 u8 proc_id, u8 host_id)
2559 struct ti_sci_msg_req_proc_handover *req;
2560 struct ti_sci_msg_hdr *resp;
2561 struct ti_sci_info *info;
2562 struct ti_sci_xfer *xfer;
2569 return PTR_ERR(handle);
2571 info = handle_to_ti_sci_info(handle);
2574 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_PROC_HANDOVER,
2575 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2576 sizeof(*req), sizeof(*resp));
2578 ret = PTR_ERR(xfer);
2579 dev_err(dev, "Message alloc failed(%d)\n", ret);
2582 req = (struct ti_sci_msg_req_proc_handover *)xfer->xfer_buf;
2583 req->processor_id = proc_id;
2584 req->host_id = host_id;
2586 ret = ti_sci_do_xfer(info, xfer);
2588 dev_err(dev, "Mbox send fail %d\n", ret);
2592 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2594 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2597 ti_sci_put_one_xfer(&info->minfo, xfer);
2603 * ti_sci_cmd_proc_set_config() - Command to set the processor boot
2604 * configuration flags
2605 * @handle: Pointer to TI SCI handle
2606 * @proc_id: Processor ID this request is for
2607 * @bootvector: Processor Boot vector (start address)
2608 * @config_flags_set: Configuration flags to be set
2609 * @config_flags_clear: Configuration flags to be cleared.
2611 * Return: 0 if all went well, else returns appropriate error value.
2613 static int ti_sci_cmd_proc_set_config(const struct ti_sci_handle *handle,
2614 u8 proc_id, u64 bootvector,
2615 u32 config_flags_set,
2616 u32 config_flags_clear)
2618 struct ti_sci_msg_req_set_config *req;
2619 struct ti_sci_msg_hdr *resp;
2620 struct ti_sci_info *info;
2621 struct ti_sci_xfer *xfer;
2628 return PTR_ERR(handle);
2630 info = handle_to_ti_sci_info(handle);
2633 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CONFIG,
2634 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2635 sizeof(*req), sizeof(*resp));
2637 ret = PTR_ERR(xfer);
2638 dev_err(dev, "Message alloc failed(%d)\n", ret);
2641 req = (struct ti_sci_msg_req_set_config *)xfer->xfer_buf;
2642 req->processor_id = proc_id;
2643 req->bootvector_low = bootvector & TI_SCI_ADDR_LOW_MASK;
2644 req->bootvector_high = (bootvector & TI_SCI_ADDR_HIGH_MASK) >>
2645 TI_SCI_ADDR_HIGH_SHIFT;
2646 req->config_flags_set = config_flags_set;
2647 req->config_flags_clear = config_flags_clear;
2649 ret = ti_sci_do_xfer(info, xfer);
2651 dev_err(dev, "Mbox send fail %d\n", ret);
2655 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2657 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2660 ti_sci_put_one_xfer(&info->minfo, xfer);
2666 * ti_sci_cmd_proc_set_control() - Command to set the processor boot
2668 * @handle: Pointer to TI SCI handle
2669 * @proc_id: Processor ID this request is for
2670 * @control_flags_set: Control flags to be set
2671 * @control_flags_clear: Control flags to be cleared
2673 * Return: 0 if all went well, else returns appropriate error value.
2675 static int ti_sci_cmd_proc_set_control(const struct ti_sci_handle *handle,
2676 u8 proc_id, u32 control_flags_set,
2677 u32 control_flags_clear)
2679 struct ti_sci_msg_req_set_ctrl *req;
2680 struct ti_sci_msg_hdr *resp;
2681 struct ti_sci_info *info;
2682 struct ti_sci_xfer *xfer;
2689 return PTR_ERR(handle);
2691 info = handle_to_ti_sci_info(handle);
2694 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CTRL,
2695 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2696 sizeof(*req), sizeof(*resp));
2698 ret = PTR_ERR(xfer);
2699 dev_err(dev, "Message alloc failed(%d)\n", ret);
2702 req = (struct ti_sci_msg_req_set_ctrl *)xfer->xfer_buf;
2703 req->processor_id = proc_id;
2704 req->control_flags_set = control_flags_set;
2705 req->control_flags_clear = control_flags_clear;
2707 ret = ti_sci_do_xfer(info, xfer);
2709 dev_err(dev, "Mbox send fail %d\n", ret);
2713 resp = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
2715 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
2718 ti_sci_put_one_xfer(&info->minfo, xfer);
2724 * ti_sci_cmd_proc_get_status() - Command to get the processor boot status
2725 * @handle: Pointer to TI SCI handle
2726 * @proc_id: Processor ID this request is for
2727 * @bv: Processor Boot vector (start address)
2728 * @cfg_flags: Processor specific configuration flags
2729 * @ctrl_flags: Processor specific control flags
2730 * @sts_flags: Processor specific status flags
2732 * Return: 0 if all went well, else returns appropriate error value.
2734 static int ti_sci_cmd_proc_get_status(const struct ti_sci_handle *handle,
2735 u8 proc_id, u64 *bv, u32 *cfg_flags,
2736 u32 *ctrl_flags, u32 *sts_flags)
2738 struct ti_sci_msg_resp_get_status *resp;
2739 struct ti_sci_msg_req_get_status *req;
2740 struct ti_sci_info *info;
2741 struct ti_sci_xfer *xfer;
2748 return PTR_ERR(handle);
2750 info = handle_to_ti_sci_info(handle);
2753 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_STATUS,
2754 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
2755 sizeof(*req), sizeof(*resp));
2757 ret = PTR_ERR(xfer);
2758 dev_err(dev, "Message alloc failed(%d)\n", ret);
2761 req = (struct ti_sci_msg_req_get_status *)xfer->xfer_buf;
2762 req->processor_id = proc_id;
2764 ret = ti_sci_do_xfer(info, xfer);
2766 dev_err(dev, "Mbox send fail %d\n", ret);
2770 resp = (struct ti_sci_msg_resp_get_status *)xfer->tx_message.buf;
2772 if (!ti_sci_is_response_ack(resp)) {
2775 *bv = (resp->bootvector_low & TI_SCI_ADDR_LOW_MASK) |
2776 (((u64)resp->bootvector_high << TI_SCI_ADDR_HIGH_SHIFT) &
2777 TI_SCI_ADDR_HIGH_MASK);
2778 *cfg_flags = resp->config_flags;
2779 *ctrl_flags = resp->control_flags;
2780 *sts_flags = resp->status_flags;
2784 ti_sci_put_one_xfer(&info->minfo, xfer);
2790 * ti_sci_setup_ops() - Setup the operations structures
2791 * @info: pointer to TISCI pointer
2793 static void ti_sci_setup_ops(struct ti_sci_info *info)
2795 struct ti_sci_ops *ops = &info->handle.ops;
2796 struct ti_sci_core_ops *core_ops = &ops->core_ops;
2797 struct ti_sci_dev_ops *dops = &ops->dev_ops;
2798 struct ti_sci_clk_ops *cops = &ops->clk_ops;
2799 struct ti_sci_rm_core_ops *rm_core_ops = &ops->rm_core_ops;
2800 struct ti_sci_rm_irq_ops *iops = &ops->rm_irq_ops;
2801 struct ti_sci_rm_ringacc_ops *rops = &ops->rm_ring_ops;
2802 struct ti_sci_rm_psil_ops *psilops = &ops->rm_psil_ops;
2803 struct ti_sci_rm_udmap_ops *udmap_ops = &ops->rm_udmap_ops;
2804 struct ti_sci_proc_ops *pops = &ops->proc_ops;
2806 core_ops->reboot_device = ti_sci_cmd_core_reboot;
2808 dops->get_device = ti_sci_cmd_get_device;
2809 dops->get_device_exclusive = ti_sci_cmd_get_device_exclusive;
2810 dops->idle_device = ti_sci_cmd_idle_device;
2811 dops->idle_device_exclusive = ti_sci_cmd_idle_device_exclusive;
2812 dops->put_device = ti_sci_cmd_put_device;
2814 dops->is_valid = ti_sci_cmd_dev_is_valid;
2815 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
2816 dops->is_idle = ti_sci_cmd_dev_is_idle;
2817 dops->is_stop = ti_sci_cmd_dev_is_stop;
2818 dops->is_on = ti_sci_cmd_dev_is_on;
2819 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
2820 dops->set_device_resets = ti_sci_cmd_set_device_resets;
2821 dops->get_device_resets = ti_sci_cmd_get_device_resets;
2823 cops->get_clock = ti_sci_cmd_get_clock;
2824 cops->idle_clock = ti_sci_cmd_idle_clock;
2825 cops->put_clock = ti_sci_cmd_put_clock;
2826 cops->is_auto = ti_sci_cmd_clk_is_auto;
2827 cops->is_on = ti_sci_cmd_clk_is_on;
2828 cops->is_off = ti_sci_cmd_clk_is_off;
2830 cops->set_parent = ti_sci_cmd_clk_set_parent;
2831 cops->get_parent = ti_sci_cmd_clk_get_parent;
2832 cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
2834 cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
2835 cops->set_freq = ti_sci_cmd_clk_set_freq;
2836 cops->get_freq = ti_sci_cmd_clk_get_freq;
2838 rm_core_ops->get_range = ti_sci_cmd_get_resource_range;
2839 rm_core_ops->get_range_from_shost =
2840 ti_sci_cmd_get_resource_range_from_shost;
2842 iops->set_irq = ti_sci_cmd_set_irq;
2843 iops->set_event_map = ti_sci_cmd_set_event_map;
2844 iops->free_irq = ti_sci_cmd_free_irq;
2845 iops->free_event_map = ti_sci_cmd_free_event_map;
2847 rops->set_cfg = ti_sci_cmd_rm_ring_cfg;
2849 psilops->pair = ti_sci_cmd_rm_psil_pair;
2850 psilops->unpair = ti_sci_cmd_rm_psil_unpair;
2852 udmap_ops->tx_ch_cfg = ti_sci_cmd_rm_udmap_tx_ch_cfg;
2853 udmap_ops->rx_ch_cfg = ti_sci_cmd_rm_udmap_rx_ch_cfg;
2854 udmap_ops->rx_flow_cfg = ti_sci_cmd_rm_udmap_rx_flow_cfg;
2856 pops->request = ti_sci_cmd_proc_request;
2857 pops->release = ti_sci_cmd_proc_release;
2858 pops->handover = ti_sci_cmd_proc_handover;
2859 pops->set_config = ti_sci_cmd_proc_set_config;
2860 pops->set_control = ti_sci_cmd_proc_set_control;
2861 pops->get_status = ti_sci_cmd_proc_get_status;
2865 * ti_sci_get_handle() - Get the TI SCI handle for a device
2866 * @dev: Pointer to device for which we want SCI handle
2868 * NOTE: The function does not track individual clients of the framework
2869 * and is expected to be maintained by caller of TI SCI protocol library.
2870 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2871 * Return: pointer to handle if successful, else:
2872 * -EPROBE_DEFER if the instance is not ready
2873 * -ENODEV if the required node handler is missing
2874 * -EINVAL if invalid conditions are encountered.
2876 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
2878 struct device_node *ti_sci_np;
2879 struct ti_sci_handle *handle = NULL;
2880 struct ti_sci_info *info;
2883 pr_err("I need a device pointer\n");
2884 return ERR_PTR(-EINVAL);
2886 ti_sci_np = of_get_parent(dev->of_node);
2888 dev_err(dev, "No OF information\n");
2889 return ERR_PTR(-EINVAL);
2892 mutex_lock(&ti_sci_list_mutex);
2893 list_for_each_entry(info, &ti_sci_list, node) {
2894 if (ti_sci_np == info->dev->of_node) {
2895 handle = &info->handle;
2900 mutex_unlock(&ti_sci_list_mutex);
2901 of_node_put(ti_sci_np);
2904 return ERR_PTR(-EPROBE_DEFER);
2908 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
2911 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
2912 * @handle: Handle acquired by ti_sci_get_handle
2914 * NOTE: The function does not track individual clients of the framework
2915 * and is expected to be maintained by caller of TI SCI protocol library.
2916 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
2918 * Return: 0 is successfully released
2919 * if an error pointer was passed, it returns the error value back,
2920 * if null was passed, it returns -EINVAL;
2922 int ti_sci_put_handle(const struct ti_sci_handle *handle)
2924 struct ti_sci_info *info;
2927 return PTR_ERR(handle);
2931 info = handle_to_ti_sci_info(handle);
2932 mutex_lock(&ti_sci_list_mutex);
2933 if (!WARN_ON(!info->users))
2935 mutex_unlock(&ti_sci_list_mutex);
2939 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
2941 static void devm_ti_sci_release(struct device *dev, void *res)
2943 const struct ti_sci_handle **ptr = res;
2944 const struct ti_sci_handle *handle = *ptr;
2947 ret = ti_sci_put_handle(handle);
2949 dev_err(dev, "failed to put handle %d\n", ret);
2953 * devm_ti_sci_get_handle() - Managed get handle
2954 * @dev: device for which we want SCI handle for.
2956 * NOTE: This releases the handle once the device resources are
2957 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
2958 * The function does not track individual clients of the framework
2959 * and is expected to be maintained by caller of TI SCI protocol library.
2961 * Return: 0 if all went fine, else corresponding error.
2963 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
2965 const struct ti_sci_handle **ptr;
2966 const struct ti_sci_handle *handle;
2968 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
2970 return ERR_PTR(-ENOMEM);
2971 handle = ti_sci_get_handle(dev);
2973 if (!IS_ERR(handle)) {
2975 devres_add(dev, ptr);
2982 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
2985 * ti_sci_get_by_phandle() - Get the TI SCI handle using DT phandle
2987 * @property: property name containing phandle on TISCI node
2989 * NOTE: The function does not track individual clients of the framework
2990 * and is expected to be maintained by caller of TI SCI protocol library.
2991 * ti_sci_put_handle must be balanced with successful ti_sci_get_by_phandle
2992 * Return: pointer to handle if successful, else:
2993 * -EPROBE_DEFER if the instance is not ready
2994 * -ENODEV if the required node handler is missing
2995 * -EINVAL if invalid conditions are encountered.
2997 const struct ti_sci_handle *ti_sci_get_by_phandle(struct device_node *np,
2998 const char *property)
3000 struct ti_sci_handle *handle = NULL;
3001 struct device_node *ti_sci_np;
3002 struct ti_sci_info *info;
3005 pr_err("I need a device pointer\n");
3006 return ERR_PTR(-EINVAL);
3009 ti_sci_np = of_parse_phandle(np, property, 0);
3011 return ERR_PTR(-ENODEV);
3013 mutex_lock(&ti_sci_list_mutex);
3014 list_for_each_entry(info, &ti_sci_list, node) {
3015 if (ti_sci_np == info->dev->of_node) {
3016 handle = &info->handle;
3021 mutex_unlock(&ti_sci_list_mutex);
3022 of_node_put(ti_sci_np);
3025 return ERR_PTR(-EPROBE_DEFER);
3029 EXPORT_SYMBOL_GPL(ti_sci_get_by_phandle);
3032 * devm_ti_sci_get_by_phandle() - Managed get handle using phandle
3033 * @dev: Device pointer requesting TISCI handle
3034 * @property: property name containing phandle on TISCI node
3036 * NOTE: This releases the handle once the device resources are
3037 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
3038 * The function does not track individual clients of the framework
3039 * and is expected to be maintained by caller of TI SCI protocol library.
3041 * Return: 0 if all went fine, else corresponding error.
3043 const struct ti_sci_handle *devm_ti_sci_get_by_phandle(struct device *dev,
3044 const char *property)
3046 const struct ti_sci_handle *handle;
3047 const struct ti_sci_handle **ptr;
3049 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
3051 return ERR_PTR(-ENOMEM);
3052 handle = ti_sci_get_by_phandle(dev_of_node(dev), property);
3054 if (!IS_ERR(handle)) {
3056 devres_add(dev, ptr);
3063 EXPORT_SYMBOL_GPL(devm_ti_sci_get_by_phandle);
3066 * ti_sci_get_free_resource() - Get a free resource from TISCI resource.
3067 * @res: Pointer to the TISCI resource
3069 * Return: resource num if all went ok else TI_SCI_RESOURCE_NULL.
3071 u16 ti_sci_get_free_resource(struct ti_sci_resource *res)
3073 unsigned long flags;
3076 raw_spin_lock_irqsave(&res->lock, flags);
3077 for (set = 0; set < res->sets; set++) {
3078 struct ti_sci_resource_desc *desc = &res->desc[set];
3079 int res_count = desc->num + desc->num_sec;
3081 free_bit = find_first_zero_bit(desc->res_map, res_count);
3082 if (free_bit != res_count) {
3083 __set_bit(free_bit, desc->res_map);
3084 raw_spin_unlock_irqrestore(&res->lock, flags);
3086 if (desc->num && free_bit < desc->num)
3087 return desc->start + free_bit;
3089 return desc->start_sec + free_bit;
3092 raw_spin_unlock_irqrestore(&res->lock, flags);
3094 return TI_SCI_RESOURCE_NULL;
3096 EXPORT_SYMBOL_GPL(ti_sci_get_free_resource);
3099 * ti_sci_release_resource() - Release a resource from TISCI resource.
3100 * @res: Pointer to the TISCI resource
3101 * @id: Resource id to be released.
3103 void ti_sci_release_resource(struct ti_sci_resource *res, u16 id)
3105 unsigned long flags;
3108 raw_spin_lock_irqsave(&res->lock, flags);
3109 for (set = 0; set < res->sets; set++) {
3110 struct ti_sci_resource_desc *desc = &res->desc[set];
3112 if (desc->num && desc->start <= id &&
3113 (desc->start + desc->num) > id)
3114 __clear_bit(id - desc->start, desc->res_map);
3115 else if (desc->num_sec && desc->start_sec <= id &&
3116 (desc->start_sec + desc->num_sec) > id)
3117 __clear_bit(id - desc->start_sec, desc->res_map);
3119 raw_spin_unlock_irqrestore(&res->lock, flags);
3121 EXPORT_SYMBOL_GPL(ti_sci_release_resource);
3124 * ti_sci_get_num_resources() - Get the number of resources in TISCI resource
3125 * @res: Pointer to the TISCI resource
3127 * Return: Total number of available resources.
3129 u32 ti_sci_get_num_resources(struct ti_sci_resource *res)
3133 for (set = 0; set < res->sets; set++)
3134 count += res->desc[set].num + res->desc[set].num_sec;
3138 EXPORT_SYMBOL_GPL(ti_sci_get_num_resources);
3141 * devm_ti_sci_get_resource_sets() - Get a TISCI resources assigned to a device
3142 * @handle: TISCI handle
3143 * @dev: Device pointer to which the resource is assigned
3144 * @dev_id: TISCI device id to which the resource is assigned
3145 * @sub_types: Array of sub_types assigned corresponding to device
3146 * @sets: Number of sub_types
3148 * Return: Pointer to ti_sci_resource if all went well else appropriate
3151 static struct ti_sci_resource *
3152 devm_ti_sci_get_resource_sets(const struct ti_sci_handle *handle,
3153 struct device *dev, u32 dev_id, u32 *sub_types,
3156 struct ti_sci_resource *res;
3157 bool valid_set = false;
3158 int i, ret, res_count;
3160 res = devm_kzalloc(dev, sizeof(*res), GFP_KERNEL);
3162 return ERR_PTR(-ENOMEM);
3165 res->desc = devm_kcalloc(dev, res->sets, sizeof(*res->desc),
3168 return ERR_PTR(-ENOMEM);
3170 for (i = 0; i < res->sets; i++) {
3171 ret = handle->ops.rm_core_ops.get_range(handle, dev_id,
3175 dev_dbg(dev, "dev = %d subtype %d not allocated for this host\n",
3176 dev_id, sub_types[i]);
3177 memset(&res->desc[i], 0, sizeof(res->desc[i]));
3181 dev_dbg(dev, "dev/sub_type: %d/%d, start/num: %d/%d | %d/%d\n",
3182 dev_id, sub_types[i], res->desc[i].start,
3183 res->desc[i].num, res->desc[i].start_sec,
3184 res->desc[i].num_sec);
3187 res_count = res->desc[i].num + res->desc[i].num_sec;
3188 res->desc[i].res_map = devm_bitmap_zalloc(dev, res_count,
3190 if (!res->desc[i].res_map)
3191 return ERR_PTR(-ENOMEM);
3193 raw_spin_lock_init(&res->lock);
3198 return ERR_PTR(-EINVAL);
3202 * devm_ti_sci_get_of_resource() - Get a TISCI resource assigned to a device
3203 * @handle: TISCI handle
3204 * @dev: Device pointer to which the resource is assigned
3205 * @dev_id: TISCI device id to which the resource is assigned
3206 * @of_prop: property name by which the resource are represented
3208 * Return: Pointer to ti_sci_resource if all went well else appropriate
3211 struct ti_sci_resource *
3212 devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
3213 struct device *dev, u32 dev_id, char *of_prop)
3215 struct ti_sci_resource *res;
3219 sets = of_property_count_elems_of_size(dev_of_node(dev), of_prop,
3222 dev_err(dev, "%s resource type ids not available\n", of_prop);
3223 return ERR_PTR(sets);
3226 sub_types = kcalloc(sets, sizeof(*sub_types), GFP_KERNEL);
3228 return ERR_PTR(-ENOMEM);
3230 of_property_read_u32_array(dev_of_node(dev), of_prop, sub_types, sets);
3231 res = devm_ti_sci_get_resource_sets(handle, dev, dev_id, sub_types,
3237 EXPORT_SYMBOL_GPL(devm_ti_sci_get_of_resource);
3240 * devm_ti_sci_get_resource() - Get a resource range assigned to the device
3241 * @handle: TISCI handle
3242 * @dev: Device pointer to which the resource is assigned
3243 * @dev_id: TISCI device id to which the resource is assigned
3244 * @sub_type: TISCI resource subytpe representing the resource.
3246 * Return: Pointer to ti_sci_resource if all went well else appropriate
3249 struct ti_sci_resource *
3250 devm_ti_sci_get_resource(const struct ti_sci_handle *handle, struct device *dev,
3251 u32 dev_id, u32 sub_type)
3253 return devm_ti_sci_get_resource_sets(handle, dev, dev_id, &sub_type, 1);
3255 EXPORT_SYMBOL_GPL(devm_ti_sci_get_resource);
3257 static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
3260 struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
3261 const struct ti_sci_handle *handle = &info->handle;
3263 ti_sci_cmd_core_reboot(handle);
3265 /* call fail OR pass, we should not be here in the first place */
3269 /* Description for K2G */
3270 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
3271 .default_host_id = 2,
3272 /* Conservative duration */
3273 .max_rx_timeout_ms = 1000,
3274 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3279 /* Description for AM654 */
3280 static const struct ti_sci_desc ti_sci_pmmc_am654_desc = {
3281 .default_host_id = 12,
3282 /* Conservative duration */
3283 .max_rx_timeout_ms = 10000,
3284 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
3289 static const struct of_device_id ti_sci_of_match[] = {
3290 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
3291 {.compatible = "ti,am654-sci", .data = &ti_sci_pmmc_am654_desc},
3294 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
3296 static int ti_sci_probe(struct platform_device *pdev)
3298 struct device *dev = &pdev->dev;
3299 const struct ti_sci_desc *desc;
3300 struct ti_sci_xfer *xfer;
3301 struct ti_sci_info *info = NULL;
3302 struct ti_sci_xfers_info *minfo;
3303 struct mbox_client *cl;
3309 desc = device_get_match_data(dev);
3311 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
3317 ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
3318 /* if the property is not present in DT, use a default from desc */
3320 info->host_id = info->desc->default_host_id;
3323 dev_warn(dev, "Host ID 0 is reserved for firmware\n");
3324 info->host_id = info->desc->default_host_id;
3326 info->host_id = h_id;
3330 reboot = of_property_read_bool(dev->of_node,
3331 "ti,system-reboot-controller");
3332 INIT_LIST_HEAD(&info->node);
3333 minfo = &info->minfo;
3336 * Pre-allocate messages
3337 * NEVER allocate more than what we can indicate in hdr.seq
3338 * if we have data description bug, force a fix..
3340 if (WARN_ON(desc->max_msgs >=
3341 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
3344 minfo->xfer_block = devm_kcalloc(dev,
3346 sizeof(*minfo->xfer_block),
3348 if (!minfo->xfer_block)
3351 minfo->xfer_alloc_table = devm_bitmap_zalloc(dev,
3354 if (!minfo->xfer_alloc_table)
3357 /* Pre-initialize the buffer pointer to pre-allocated buffers */
3358 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
3359 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
3361 if (!xfer->xfer_buf)
3364 xfer->tx_message.buf = xfer->xfer_buf;
3365 init_completion(&xfer->done);
3368 ret = ti_sci_debugfs_create(pdev, info);
3370 dev_warn(dev, "Failed to create debug file\n");
3372 platform_set_drvdata(pdev, info);
3376 cl->tx_block = false;
3377 cl->rx_callback = ti_sci_rx_callback;
3378 cl->knows_txdone = true;
3380 spin_lock_init(&minfo->xfer_lock);
3381 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
3383 info->chan_rx = mbox_request_channel_byname(cl, "rx");
3384 if (IS_ERR(info->chan_rx)) {
3385 ret = PTR_ERR(info->chan_rx);
3389 info->chan_tx = mbox_request_channel_byname(cl, "tx");
3390 if (IS_ERR(info->chan_tx)) {
3391 ret = PTR_ERR(info->chan_tx);
3394 ret = ti_sci_cmd_get_revision(info);
3396 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
3400 ti_sci_setup_ops(info);
3403 info->nb.notifier_call = tisci_reboot_handler;
3404 info->nb.priority = 128;
3406 ret = register_restart_handler(&info->nb);
3408 dev_err(dev, "reboot registration fail(%d)\n", ret);
3413 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
3414 info->handle.version.abi_major, info->handle.version.abi_minor,
3415 info->handle.version.firmware_revision,
3416 info->handle.version.firmware_description);
3418 mutex_lock(&ti_sci_list_mutex);
3419 list_add_tail(&info->node, &ti_sci_list);
3420 mutex_unlock(&ti_sci_list_mutex);
3422 return of_platform_populate(dev->of_node, NULL, NULL, dev);
3424 if (!IS_ERR(info->chan_tx))
3425 mbox_free_channel(info->chan_tx);
3426 if (!IS_ERR(info->chan_rx))
3427 mbox_free_channel(info->chan_rx);
3428 debugfs_remove(info->d);
3432 static struct platform_driver ti_sci_driver = {
3433 .probe = ti_sci_probe,
3436 .of_match_table = of_match_ptr(ti_sci_of_match),
3437 .suppress_bind_attrs = true,
3440 module_platform_driver(ti_sci_driver);
3442 MODULE_LICENSE("GPL v2");
3443 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
3444 MODULE_AUTHOR("Nishanth Menon");
3445 MODULE_ALIAS("platform:ti-sci");