2 * Texas Instruments System Control Interface Protocol Driver
4 * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #define pr_fmt(fmt) "%s: " fmt, __func__
19 #include <linux/bitmap.h>
20 #include <linux/debugfs.h>
21 #include <linux/export.h>
23 #include <linux/kernel.h>
24 #include <linux/mailbox_client.h>
25 #include <linux/module.h>
26 #include <linux/of_device.h>
27 #include <linux/semaphore.h>
28 #include <linux/slab.h>
29 #include <linux/soc/ti/ti-msgmgr.h>
30 #include <linux/soc/ti/ti_sci_protocol.h>
31 #include <linux/reboot.h>
35 /* List of all TI SCI devices active in system */
36 static LIST_HEAD(ti_sci_list);
37 /* Protection for the entire list */
38 static DEFINE_MUTEX(ti_sci_list_mutex);
41 * struct ti_sci_xfer - Structure representing a message flow
42 * @tx_message: Transmit message
43 * @rx_len: Receive message length
44 * @xfer_buf: Preallocated buffer to store receive message
45 * Since we work with request-ACK protocol, we can
46 * reuse the same buffer for the rx path as we
47 * use for the tx path.
48 * @done: completion event
51 struct ti_msgmgr_message tx_message;
54 struct completion done;
58 * struct ti_sci_xfers_info - Structure to manage transfer information
59 * @sem_xfer_count: Counting Semaphore for managing max simultaneous
61 * @xfer_block: Preallocated Message array
62 * @xfer_alloc_table: Bitmap table for allocated messages.
63 * Index of this bitmap table is also used for message
64 * sequence identifier.
65 * @xfer_lock: Protection for message allocation
67 struct ti_sci_xfers_info {
68 struct semaphore sem_xfer_count;
69 struct ti_sci_xfer *xfer_block;
70 unsigned long *xfer_alloc_table;
71 /* protect transfer allocation */
76 * struct ti_sci_desc - Description of SoC integration
77 * @host_id: Host identifier representing the compute entity
78 * @max_rx_timeout_ms: Timeout for communication with SoC (in Milliseconds)
79 * @max_msgs: Maximum number of messages that can be pending
80 * simultaneously in the system
81 * @max_msg_size: Maximum size of data per message that can be handled.
85 int max_rx_timeout_ms;
91 * struct ti_sci_info - Structure representing a TI SCI instance
92 * @dev: Device pointer
93 * @desc: SoC description for this instance
94 * @nb: Reboot Notifier block
95 * @d: Debugfs file entry
96 * @debug_region: Memory region where the debug message are available
97 * @debug_region_size: Debug region size
98 * @debug_buffer: Buffer allocated to copy debug messages.
99 * @handle: Instance of TI SCI handle to send to clients.
100 * @cl: Mailbox Client
101 * @chan_tx: Transmit mailbox channel
102 * @chan_rx: Receive mailbox channel
103 * @minfo: Message info
105 * @users: Number of users of this instance
109 struct notifier_block nb;
110 const struct ti_sci_desc *desc;
112 void __iomem *debug_region;
114 size_t debug_region_size;
115 struct ti_sci_handle handle;
116 struct mbox_client cl;
117 struct mbox_chan *chan_tx;
118 struct mbox_chan *chan_rx;
119 struct ti_sci_xfers_info minfo;
120 struct list_head node;
121 /* protected by ti_sci_list_mutex */
126 #define cl_to_ti_sci_info(c) container_of(c, struct ti_sci_info, cl)
127 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
128 #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
130 #ifdef CONFIG_DEBUG_FS
133 * ti_sci_debug_show() - Helper to dump the debug log
134 * @s: sequence file pointer
139 static int ti_sci_debug_show(struct seq_file *s, void *unused)
141 struct ti_sci_info *info = s->private;
143 memcpy_fromio(info->debug_buffer, info->debug_region,
144 info->debug_region_size);
146 * We don't trust firmware to leave NULL terminated last byte (hence
147 * we have allocated 1 extra 0 byte). Since we cannot guarantee any
148 * specific data format for debug messages, We just present the data
149 * in the buffer as is - we expect the messages to be self explanatory.
151 seq_puts(s, info->debug_buffer);
156 * ti_sci_debug_open() - debug file open
157 * @inode: inode pointer
158 * @file: file pointer
160 * Return: result of single_open
162 static int ti_sci_debug_open(struct inode *inode, struct file *file)
164 return single_open(file, ti_sci_debug_show, inode->i_private);
167 /* log file operations */
168 static const struct file_operations ti_sci_debug_fops = {
169 .open = ti_sci_debug_open,
172 .release = single_release,
176 * ti_sci_debugfs_create() - Create log debug file
177 * @pdev: platform device pointer
178 * @info: Pointer to SCI entity information
180 * Return: 0 if all went fine, else corresponding error.
182 static int ti_sci_debugfs_create(struct platform_device *pdev,
183 struct ti_sci_info *info)
185 struct device *dev = &pdev->dev;
186 struct resource *res;
187 char debug_name[50] = "ti_sci_debug@";
189 /* Debug region is optional */
190 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
192 info->debug_region = devm_ioremap_resource(dev, res);
193 if (IS_ERR(info->debug_region))
195 info->debug_region_size = resource_size(res);
197 info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
198 sizeof(char), GFP_KERNEL);
199 if (!info->debug_buffer)
201 /* Setup NULL termination */
202 info->debug_buffer[info->debug_region_size] = 0;
204 info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
206 0444, NULL, info, &ti_sci_debug_fops);
208 return PTR_ERR(info->d);
210 dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
211 info->debug_region, info->debug_region_size, res);
216 * ti_sci_debugfs_destroy() - clean up log debug file
217 * @pdev: platform device pointer
218 * @info: Pointer to SCI entity information
220 static void ti_sci_debugfs_destroy(struct platform_device *pdev,
221 struct ti_sci_info *info)
223 if (IS_ERR(info->debug_region))
226 debugfs_remove(info->d);
228 #else /* CONFIG_DEBUG_FS */
229 static inline int ti_sci_debugfs_create(struct platform_device *dev,
230 struct ti_sci_info *info)
235 static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
236 struct ti_sci_info *info)
239 #endif /* CONFIG_DEBUG_FS */
242 * ti_sci_dump_header_dbg() - Helper to dump a message header.
243 * @dev: Device pointer corresponding to the SCI entity
244 * @hdr: pointer to header.
246 static inline void ti_sci_dump_header_dbg(struct device *dev,
247 struct ti_sci_msg_hdr *hdr)
249 dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
250 hdr->type, hdr->host, hdr->seq, hdr->flags);
254 * ti_sci_rx_callback() - mailbox client callback for receive messages
255 * @cl: client pointer
256 * @m: mailbox message
258 * Processes one received message to appropriate transfer information and
259 * signals completion of the transfer.
261 * NOTE: This function will be invoked in IRQ context, hence should be
262 * as optimal as possible.
264 static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
266 struct ti_sci_info *info = cl_to_ti_sci_info(cl);
267 struct device *dev = info->dev;
268 struct ti_sci_xfers_info *minfo = &info->minfo;
269 struct ti_msgmgr_message *mbox_msg = m;
270 struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
271 struct ti_sci_xfer *xfer;
277 * Are we even expecting this?
278 * NOTE: barriers were implicit in locks used for modifying the bitmap
280 if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
281 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
285 xfer = &minfo->xfer_block[xfer_id];
287 /* Is the message of valid length? */
288 if (mbox_msg->len > info->desc->max_msg_size) {
289 dev_err(dev, "Unable to handle %d xfer(max %d)\n",
290 mbox_msg->len, info->desc->max_msg_size);
291 ti_sci_dump_header_dbg(dev, hdr);
294 if (mbox_msg->len < xfer->rx_len) {
295 dev_err(dev, "Recv xfer %d < expected %d length\n",
296 mbox_msg->len, xfer->rx_len);
297 ti_sci_dump_header_dbg(dev, hdr);
301 ti_sci_dump_header_dbg(dev, hdr);
302 /* Take a copy to the rx buffer.. */
303 memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
304 complete(&xfer->done);
308 * ti_sci_get_one_xfer() - Allocate one message
309 * @info: Pointer to SCI entity information
310 * @msg_type: Message type
311 * @msg_flags: Flag to set for the message
312 * @tx_message_size: transmit message size
313 * @rx_message_size: receive message size
315 * Helper function which is used by various command functions that are
316 * exposed to clients of this driver for allocating a message traffic event.
318 * This function can sleep depending on pending requests already in the system
319 * for the SCI entity. Further, this also holds a spinlock to maintain integrity
320 * of internal data structures.
322 * Return: 0 if all went fine, else corresponding error.
324 static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
325 u16 msg_type, u32 msg_flags,
326 size_t tx_message_size,
327 size_t rx_message_size)
329 struct ti_sci_xfers_info *minfo = &info->minfo;
330 struct ti_sci_xfer *xfer;
331 struct ti_sci_msg_hdr *hdr;
333 unsigned long bit_pos;
338 /* Ensure we have sane transfer sizes */
339 if (rx_message_size > info->desc->max_msg_size ||
340 tx_message_size > info->desc->max_msg_size ||
341 rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
342 return ERR_PTR(-ERANGE);
345 * Ensure we have only controlled number of pending messages.
346 * Ideally, we might just have to wait a single message, be
347 * conservative and wait 5 times that..
349 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
350 ret = down_timeout(&minfo->sem_xfer_count, timeout);
354 /* Keep the locked section as small as possible */
355 spin_lock_irqsave(&minfo->xfer_lock, flags);
356 bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
357 info->desc->max_msgs);
358 set_bit(bit_pos, minfo->xfer_alloc_table);
359 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
362 * We already ensured in probe that we can have max messages that can
363 * fit in hdr.seq - NOTE: this improves access latencies
364 * to predictable O(1) access, BUT, it opens us to risk if
365 * remote misbehaves with corrupted message sequence responses.
366 * If that happens, we are going to be messed up anyways..
368 xfer_id = (u8)bit_pos;
370 xfer = &minfo->xfer_block[xfer_id];
372 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
373 xfer->tx_message.len = tx_message_size;
374 xfer->rx_len = (u8)rx_message_size;
376 reinit_completion(&xfer->done);
379 hdr->type = msg_type;
380 hdr->host = info->desc->host_id;
381 hdr->flags = msg_flags;
387 * ti_sci_put_one_xfer() - Release a message
388 * @minfo: transfer info pointer
389 * @xfer: message that was reserved by ti_sci_get_one_xfer
391 * This holds a spinlock to maintain integrity of internal data structures.
393 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
394 struct ti_sci_xfer *xfer)
397 struct ti_sci_msg_hdr *hdr;
400 hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
404 * Keep the locked section as small as possible
405 * NOTE: we might escape with smp_mb and no lock here..
406 * but just be conservative and symmetric.
408 spin_lock_irqsave(&minfo->xfer_lock, flags);
409 clear_bit(xfer_id, minfo->xfer_alloc_table);
410 spin_unlock_irqrestore(&minfo->xfer_lock, flags);
412 /* Increment the count for the next user to get through */
413 up(&minfo->sem_xfer_count);
417 * ti_sci_do_xfer() - Do one transfer
418 * @info: Pointer to SCI entity information
419 * @xfer: Transfer to initiate and wait for response
421 * Return: -ETIMEDOUT in case of no response, if transmit error,
422 * return corresponding error, else if all goes well,
425 static inline int ti_sci_do_xfer(struct ti_sci_info *info,
426 struct ti_sci_xfer *xfer)
430 struct device *dev = info->dev;
432 ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
438 /* And we wait for the response. */
439 timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
440 if (!wait_for_completion_timeout(&xfer->done, timeout)) {
441 dev_err(dev, "Mbox timedout in resp(caller: %pF)\n",
446 * NOTE: we might prefer not to need the mailbox ticker to manage the
447 * transfer queueing since the protocol layer queues things by itself.
448 * Unfortunately, we have to kick the mailbox framework after we have
449 * received our message.
451 mbox_client_txdone(info->chan_tx, ret);
457 * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
458 * @info: Pointer to SCI entity information
460 * Updates the SCI information in the internal data structure.
462 * Return: 0 if all went fine, else return appropriate error.
464 static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
466 struct device *dev = info->dev;
467 struct ti_sci_handle *handle = &info->handle;
468 struct ti_sci_version_info *ver = &handle->version;
469 struct ti_sci_msg_resp_version *rev_info;
470 struct ti_sci_xfer *xfer;
473 /* No need to setup flags since it is expected to respond */
474 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
475 0x0, sizeof(struct ti_sci_msg_hdr),
479 dev_err(dev, "Message alloc failed(%d)\n", ret);
483 rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
485 ret = ti_sci_do_xfer(info, xfer);
487 dev_err(dev, "Mbox send fail %d\n", ret);
491 ver->abi_major = rev_info->abi_major;
492 ver->abi_minor = rev_info->abi_minor;
493 ver->firmware_revision = rev_info->firmware_revision;
494 strncpy(ver->firmware_description, rev_info->firmware_description,
495 sizeof(ver->firmware_description));
498 ti_sci_put_one_xfer(&info->minfo, xfer);
503 * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
504 * @r: pointer to response buffer
506 * Return: true if the response was an ACK, else returns false.
508 static inline bool ti_sci_is_response_ack(void *r)
510 struct ti_sci_msg_hdr *hdr = r;
512 return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
516 * ti_sci_set_device_state() - Set device state helper
517 * @handle: pointer to TI SCI handle
518 * @id: Device identifier
519 * @flags: flags to setup for the device
520 * @state: State to move the device to
522 * Return: 0 if all went well, else returns appropriate error value.
524 static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
525 u32 id, u32 flags, u8 state)
527 struct ti_sci_info *info;
528 struct ti_sci_msg_req_set_device_state *req;
529 struct ti_sci_msg_hdr *resp;
530 struct ti_sci_xfer *xfer;
535 return PTR_ERR(handle);
539 info = handle_to_ti_sci_info(handle);
542 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
543 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
544 sizeof(*req), sizeof(*resp));
547 dev_err(dev, "Message alloc failed(%d)\n", ret);
550 req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
554 ret = ti_sci_do_xfer(info, xfer);
556 dev_err(dev, "Mbox send fail %d\n", ret);
560 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
562 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
565 ti_sci_put_one_xfer(&info->minfo, xfer);
571 * ti_sci_get_device_state() - Get device state helper
572 * @handle: Handle to the device
573 * @id: Device Identifier
574 * @clcnt: Pointer to Context Loss Count
575 * @resets: pointer to resets
576 * @p_state: pointer to p_state
577 * @c_state: pointer to c_state
579 * Return: 0 if all went fine, else return appropriate error.
581 static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
582 u32 id, u32 *clcnt, u32 *resets,
583 u8 *p_state, u8 *c_state)
585 struct ti_sci_info *info;
586 struct ti_sci_msg_req_get_device_state *req;
587 struct ti_sci_msg_resp_get_device_state *resp;
588 struct ti_sci_xfer *xfer;
593 return PTR_ERR(handle);
597 if (!clcnt && !resets && !p_state && !c_state)
600 info = handle_to_ti_sci_info(handle);
603 /* Response is expected, so need of any flags */
604 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
605 0, sizeof(*req), sizeof(*resp));
608 dev_err(dev, "Message alloc failed(%d)\n", ret);
611 req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
614 ret = ti_sci_do_xfer(info, xfer);
616 dev_err(dev, "Mbox send fail %d\n", ret);
620 resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
621 if (!ti_sci_is_response_ack(resp)) {
627 *clcnt = resp->context_loss_count;
629 *resets = resp->resets;
631 *p_state = resp->programmed_state;
633 *c_state = resp->current_state;
635 ti_sci_put_one_xfer(&info->minfo, xfer);
641 * ti_sci_cmd_get_device() - command to request for device managed by TISCI
642 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
643 * @id: Device Identifier
645 * Request for the device - NOTE: the client MUST maintain integrity of
646 * usage count by balancing get_device with put_device. No refcounting is
647 * managed by driver for that purpose.
649 * NOTE: The request is for exclusive access for the processor.
651 * Return: 0 if all went fine, else return appropriate error.
653 static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
655 return ti_sci_set_device_state(handle, id,
656 MSG_FLAG_DEVICE_EXCLUSIVE,
657 MSG_DEVICE_SW_STATE_ON);
661 * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
662 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
663 * @id: Device Identifier
665 * Request for the device - NOTE: the client MUST maintain integrity of
666 * usage count by balancing get_device with put_device. No refcounting is
667 * managed by driver for that purpose.
669 * Return: 0 if all went fine, else return appropriate error.
671 static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
673 return ti_sci_set_device_state(handle, id,
674 MSG_FLAG_DEVICE_EXCLUSIVE,
675 MSG_DEVICE_SW_STATE_RETENTION);
679 * ti_sci_cmd_put_device() - command to release a device managed by TISCI
680 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
681 * @id: Device Identifier
683 * Request for the device - NOTE: the client MUST maintain integrity of
684 * usage count by balancing get_device with put_device. No refcounting is
685 * managed by driver for that purpose.
687 * Return: 0 if all went fine, else return appropriate error.
689 static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
691 return ti_sci_set_device_state(handle, id,
692 0, MSG_DEVICE_SW_STATE_AUTO_OFF);
696 * ti_sci_cmd_dev_is_valid() - Is the device valid
697 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
698 * @id: Device Identifier
700 * Return: 0 if all went fine and the device ID is valid, else return
703 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
707 /* check the device state which will also tell us if the ID is valid */
708 return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
712 * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
713 * @handle: Pointer to TISCI handle
714 * @id: Device Identifier
715 * @count: Pointer to Context Loss counter to populate
717 * Return: 0 if all went fine, else return appropriate error.
719 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
722 return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
726 * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
727 * @handle: Pointer to TISCI handle
728 * @id: Device Identifier
729 * @r_state: true if requested to be idle
731 * Return: 0 if all went fine, else return appropriate error.
733 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
742 ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
746 *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
752 * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
753 * @handle: Pointer to TISCI handle
754 * @id: Device Identifier
755 * @r_state: true if requested to be stopped
756 * @curr_state: true if currently stopped.
758 * Return: 0 if all went fine, else return appropriate error.
760 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
761 bool *r_state, bool *curr_state)
766 if (!r_state && !curr_state)
770 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
775 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
777 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
783 * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
784 * @handle: Pointer to TISCI handle
785 * @id: Device Identifier
786 * @r_state: true if requested to be ON
787 * @curr_state: true if currently ON and active
789 * Return: 0 if all went fine, else return appropriate error.
791 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
792 bool *r_state, bool *curr_state)
797 if (!r_state && !curr_state)
801 ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
806 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
808 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
814 * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
815 * @handle: Pointer to TISCI handle
816 * @id: Device Identifier
817 * @curr_state: true if currently transitioning.
819 * Return: 0 if all went fine, else return appropriate error.
821 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
830 ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
834 *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
840 * ti_sci_cmd_set_device_resets() - command to set resets for device managed
842 * @handle: Pointer to TISCI handle as retrieved by *ti_sci_get_handle
843 * @id: Device Identifier
844 * @reset_state: Device specific reset bit field
846 * Return: 0 if all went fine, else return appropriate error.
848 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
849 u32 id, u32 reset_state)
851 struct ti_sci_info *info;
852 struct ti_sci_msg_req_set_device_resets *req;
853 struct ti_sci_msg_hdr *resp;
854 struct ti_sci_xfer *xfer;
859 return PTR_ERR(handle);
863 info = handle_to_ti_sci_info(handle);
866 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
867 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
868 sizeof(*req), sizeof(*resp));
871 dev_err(dev, "Message alloc failed(%d)\n", ret);
874 req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
876 req->resets = reset_state;
878 ret = ti_sci_do_xfer(info, xfer);
880 dev_err(dev, "Mbox send fail %d\n", ret);
884 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
886 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
889 ti_sci_put_one_xfer(&info->minfo, xfer);
895 * ti_sci_cmd_get_device_resets() - Get reset state for device managed
897 * @handle: Pointer to TISCI handle
898 * @id: Device Identifier
899 * @reset_state: Pointer to reset state to populate
901 * Return: 0 if all went fine, else return appropriate error.
903 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
904 u32 id, u32 *reset_state)
906 return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
911 * ti_sci_set_clock_state() - Set clock state helper
912 * @handle: pointer to TI SCI handle
913 * @dev_id: Device identifier this request is for
914 * @clk_id: Clock identifier for the device for this request.
915 * Each device has it's own set of clock inputs. This indexes
916 * which clock input to modify.
917 * @flags: Header flags as needed
918 * @state: State to request for the clock.
920 * Return: 0 if all went well, else returns appropriate error value.
922 static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
923 u32 dev_id, u8 clk_id,
926 struct ti_sci_info *info;
927 struct ti_sci_msg_req_set_clock_state *req;
928 struct ti_sci_msg_hdr *resp;
929 struct ti_sci_xfer *xfer;
934 return PTR_ERR(handle);
938 info = handle_to_ti_sci_info(handle);
941 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
942 flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
943 sizeof(*req), sizeof(*resp));
946 dev_err(dev, "Message alloc failed(%d)\n", ret);
949 req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
950 req->dev_id = dev_id;
951 req->clk_id = clk_id;
952 req->request_state = state;
954 ret = ti_sci_do_xfer(info, xfer);
956 dev_err(dev, "Mbox send fail %d\n", ret);
960 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
962 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
965 ti_sci_put_one_xfer(&info->minfo, xfer);
971 * ti_sci_cmd_get_clock_state() - Get clock state helper
972 * @handle: pointer to TI SCI handle
973 * @dev_id: Device identifier this request is for
974 * @clk_id: Clock identifier for the device for this request.
975 * Each device has it's own set of clock inputs. This indexes
976 * which clock input to modify.
977 * @programmed_state: State requested for clock to move to
978 * @current_state: State that the clock is currently in
980 * Return: 0 if all went well, else returns appropriate error value.
982 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
983 u32 dev_id, u8 clk_id,
984 u8 *programmed_state, u8 *current_state)
986 struct ti_sci_info *info;
987 struct ti_sci_msg_req_get_clock_state *req;
988 struct ti_sci_msg_resp_get_clock_state *resp;
989 struct ti_sci_xfer *xfer;
994 return PTR_ERR(handle);
998 if (!programmed_state && !current_state)
1001 info = handle_to_ti_sci_info(handle);
1004 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1005 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1006 sizeof(*req), sizeof(*resp));
1008 ret = PTR_ERR(xfer);
1009 dev_err(dev, "Message alloc failed(%d)\n", ret);
1012 req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1013 req->dev_id = dev_id;
1014 req->clk_id = clk_id;
1016 ret = ti_sci_do_xfer(info, xfer);
1018 dev_err(dev, "Mbox send fail %d\n", ret);
1022 resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1024 if (!ti_sci_is_response_ack(resp)) {
1029 if (programmed_state)
1030 *programmed_state = resp->programmed_state;
1032 *current_state = resp->current_state;
1035 ti_sci_put_one_xfer(&info->minfo, xfer);
1041 * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1042 * @handle: pointer to TI SCI handle
1043 * @dev_id: Device identifier this request is for
1044 * @clk_id: Clock identifier for the device for this request.
1045 * Each device has it's own set of clock inputs. This indexes
1046 * which clock input to modify.
1047 * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1048 * @can_change_freq: 'true' if frequency change is desired, else 'false'
1049 * @enable_input_term: 'true' if input termination is desired, else 'false'
1051 * Return: 0 if all went well, else returns appropriate error value.
1053 static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1054 u8 clk_id, bool needs_ssc, bool can_change_freq,
1055 bool enable_input_term)
1059 flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1060 flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1061 flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1063 return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1064 MSG_CLOCK_SW_STATE_REQ);
1068 * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1069 * @handle: pointer to TI SCI handle
1070 * @dev_id: Device identifier this request is for
1071 * @clk_id: Clock identifier for the device for this request.
1072 * Each device has it's own set of clock inputs. This indexes
1073 * which clock input to modify.
1075 * NOTE: This clock must have been requested by get_clock previously.
1077 * Return: 0 if all went well, else returns appropriate error value.
1079 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1080 u32 dev_id, u8 clk_id)
1082 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1083 MSG_CLOCK_SW_STATE_UNREQ);
1087 * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1088 * @handle: pointer to TI SCI handle
1089 * @dev_id: Device identifier this request is for
1090 * @clk_id: Clock identifier for the device for this request.
1091 * Each device has it's own set of clock inputs. This indexes
1092 * which clock input to modify.
1094 * NOTE: This clock must have been requested by get_clock previously.
1096 * Return: 0 if all went well, else returns appropriate error value.
1098 static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1099 u32 dev_id, u8 clk_id)
1101 return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1102 MSG_CLOCK_SW_STATE_AUTO);
1106 * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
1107 * @handle: pointer to TI SCI handle
1108 * @dev_id: Device identifier this request is for
1109 * @clk_id: Clock identifier for the device for this request.
1110 * Each device has it's own set of clock inputs. This indexes
1111 * which clock input to modify.
1112 * @req_state: state indicating if the clock is auto managed
1114 * Return: 0 if all went well, else returns appropriate error value.
1116 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1117 u32 dev_id, u8 clk_id, bool *req_state)
1125 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1129 *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1134 * ti_sci_cmd_clk_is_on() - Is the clock ON
1135 * @handle: pointer to TI SCI handle
1136 * @dev_id: Device identifier this request is for
1137 * @clk_id: Clock identifier for the device for this request.
1138 * Each device has it's own set of clock inputs. This indexes
1139 * which clock input to modify.
1140 * @req_state: state indicating if the clock is managed by us and enabled
1141 * @curr_state: state indicating if the clock is ready for operation
1143 * Return: 0 if all went well, else returns appropriate error value.
1145 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1146 u8 clk_id, bool *req_state, bool *curr_state)
1148 u8 c_state = 0, r_state = 0;
1151 if (!req_state && !curr_state)
1154 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1155 &r_state, &c_state);
1160 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1162 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1167 * ti_sci_cmd_clk_is_off() - Is the clock OFF
1168 * @handle: pointer to TI SCI handle
1169 * @dev_id: Device identifier this request is for
1170 * @clk_id: Clock identifier for the device for this request.
1171 * Each device has it's own set of clock inputs. This indexes
1172 * which clock input to modify.
1173 * @req_state: state indicating if the clock is managed by us and disabled
1174 * @curr_state: state indicating if the clock is NOT ready for operation
1176 * Return: 0 if all went well, else returns appropriate error value.
1178 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1179 u8 clk_id, bool *req_state, bool *curr_state)
1181 u8 c_state = 0, r_state = 0;
1184 if (!req_state && !curr_state)
1187 ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1188 &r_state, &c_state);
1193 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1195 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1200 * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1201 * @handle: pointer to TI SCI handle
1202 * @dev_id: Device identifier this request is for
1203 * @clk_id: Clock identifier for the device for this request.
1204 * Each device has it's own set of clock inputs. This indexes
1205 * which clock input to modify.
1206 * @parent_id: Parent clock identifier to set
1208 * Return: 0 if all went well, else returns appropriate error value.
1210 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1211 u32 dev_id, u8 clk_id, u8 parent_id)
1213 struct ti_sci_info *info;
1214 struct ti_sci_msg_req_set_clock_parent *req;
1215 struct ti_sci_msg_hdr *resp;
1216 struct ti_sci_xfer *xfer;
1221 return PTR_ERR(handle);
1225 info = handle_to_ti_sci_info(handle);
1228 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1229 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1230 sizeof(*req), sizeof(*resp));
1232 ret = PTR_ERR(xfer);
1233 dev_err(dev, "Message alloc failed(%d)\n", ret);
1236 req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1237 req->dev_id = dev_id;
1238 req->clk_id = clk_id;
1239 req->parent_id = parent_id;
1241 ret = ti_sci_do_xfer(info, xfer);
1243 dev_err(dev, "Mbox send fail %d\n", ret);
1247 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1249 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1252 ti_sci_put_one_xfer(&info->minfo, xfer);
1258 * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1259 * @handle: pointer to TI SCI handle
1260 * @dev_id: Device identifier this request is for
1261 * @clk_id: Clock identifier for the device for this request.
1262 * Each device has it's own set of clock inputs. This indexes
1263 * which clock input to modify.
1264 * @parent_id: Current clock parent
1266 * Return: 0 if all went well, else returns appropriate error value.
1268 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1269 u32 dev_id, u8 clk_id, u8 *parent_id)
1271 struct ti_sci_info *info;
1272 struct ti_sci_msg_req_get_clock_parent *req;
1273 struct ti_sci_msg_resp_get_clock_parent *resp;
1274 struct ti_sci_xfer *xfer;
1279 return PTR_ERR(handle);
1280 if (!handle || !parent_id)
1283 info = handle_to_ti_sci_info(handle);
1286 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1287 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1288 sizeof(*req), sizeof(*resp));
1290 ret = PTR_ERR(xfer);
1291 dev_err(dev, "Message alloc failed(%d)\n", ret);
1294 req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1295 req->dev_id = dev_id;
1296 req->clk_id = clk_id;
1298 ret = ti_sci_do_xfer(info, xfer);
1300 dev_err(dev, "Mbox send fail %d\n", ret);
1304 resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1306 if (!ti_sci_is_response_ack(resp))
1309 *parent_id = resp->parent_id;
1312 ti_sci_put_one_xfer(&info->minfo, xfer);
1318 * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1319 * @handle: pointer to TI SCI handle
1320 * @dev_id: Device identifier this request is for
1321 * @clk_id: Clock identifier for the device for this request.
1322 * Each device has it's own set of clock inputs. This indexes
1323 * which clock input to modify.
1324 * @num_parents: Returns he number of parents to the current clock.
1326 * Return: 0 if all went well, else returns appropriate error value.
1328 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1329 u32 dev_id, u8 clk_id,
1332 struct ti_sci_info *info;
1333 struct ti_sci_msg_req_get_clock_num_parents *req;
1334 struct ti_sci_msg_resp_get_clock_num_parents *resp;
1335 struct ti_sci_xfer *xfer;
1340 return PTR_ERR(handle);
1341 if (!handle || !num_parents)
1344 info = handle_to_ti_sci_info(handle);
1347 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1348 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1349 sizeof(*req), sizeof(*resp));
1351 ret = PTR_ERR(xfer);
1352 dev_err(dev, "Message alloc failed(%d)\n", ret);
1355 req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1356 req->dev_id = dev_id;
1357 req->clk_id = clk_id;
1359 ret = ti_sci_do_xfer(info, xfer);
1361 dev_err(dev, "Mbox send fail %d\n", ret);
1365 resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1367 if (!ti_sci_is_response_ack(resp))
1370 *num_parents = resp->num_parents;
1373 ti_sci_put_one_xfer(&info->minfo, xfer);
1379 * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1380 * @handle: pointer to TI SCI handle
1381 * @dev_id: Device identifier this request is for
1382 * @clk_id: Clock identifier for the device for this request.
1383 * Each device has it's own set of clock inputs. This indexes
1384 * which clock input to modify.
1385 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1386 * allowable programmed frequency and does not account for clock
1387 * tolerances and jitter.
1388 * @target_freq: The target clock frequency in Hz. A frequency will be
1389 * processed as close to this target frequency as possible.
1390 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1391 * allowable programmed frequency and does not account for clock
1392 * tolerances and jitter.
1393 * @match_freq: Frequency match in Hz response.
1395 * Return: 0 if all went well, else returns appropriate error value.
1397 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1398 u32 dev_id, u8 clk_id, u64 min_freq,
1399 u64 target_freq, u64 max_freq,
1402 struct ti_sci_info *info;
1403 struct ti_sci_msg_req_query_clock_freq *req;
1404 struct ti_sci_msg_resp_query_clock_freq *resp;
1405 struct ti_sci_xfer *xfer;
1410 return PTR_ERR(handle);
1411 if (!handle || !match_freq)
1414 info = handle_to_ti_sci_info(handle);
1417 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1418 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1419 sizeof(*req), sizeof(*resp));
1421 ret = PTR_ERR(xfer);
1422 dev_err(dev, "Message alloc failed(%d)\n", ret);
1425 req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1426 req->dev_id = dev_id;
1427 req->clk_id = clk_id;
1428 req->min_freq_hz = min_freq;
1429 req->target_freq_hz = target_freq;
1430 req->max_freq_hz = max_freq;
1432 ret = ti_sci_do_xfer(info, xfer);
1434 dev_err(dev, "Mbox send fail %d\n", ret);
1438 resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1440 if (!ti_sci_is_response_ack(resp))
1443 *match_freq = resp->freq_hz;
1446 ti_sci_put_one_xfer(&info->minfo, xfer);
1452 * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1453 * @handle: pointer to TI SCI handle
1454 * @dev_id: Device identifier this request is for
1455 * @clk_id: Clock identifier for the device for this request.
1456 * Each device has it's own set of clock inputs. This indexes
1457 * which clock input to modify.
1458 * @min_freq: The minimum allowable frequency in Hz. This is the minimum
1459 * allowable programmed frequency and does not account for clock
1460 * tolerances and jitter.
1461 * @target_freq: The target clock frequency in Hz. A frequency will be
1462 * processed as close to this target frequency as possible.
1463 * @max_freq: The maximum allowable frequency in Hz. This is the maximum
1464 * allowable programmed frequency and does not account for clock
1465 * tolerances and jitter.
1467 * Return: 0 if all went well, else returns appropriate error value.
1469 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1470 u32 dev_id, u8 clk_id, u64 min_freq,
1471 u64 target_freq, u64 max_freq)
1473 struct ti_sci_info *info;
1474 struct ti_sci_msg_req_set_clock_freq *req;
1475 struct ti_sci_msg_hdr *resp;
1476 struct ti_sci_xfer *xfer;
1481 return PTR_ERR(handle);
1485 info = handle_to_ti_sci_info(handle);
1488 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1489 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1490 sizeof(*req), sizeof(*resp));
1492 ret = PTR_ERR(xfer);
1493 dev_err(dev, "Message alloc failed(%d)\n", ret);
1496 req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1497 req->dev_id = dev_id;
1498 req->clk_id = clk_id;
1499 req->min_freq_hz = min_freq;
1500 req->target_freq_hz = target_freq;
1501 req->max_freq_hz = max_freq;
1503 ret = ti_sci_do_xfer(info, xfer);
1505 dev_err(dev, "Mbox send fail %d\n", ret);
1509 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1511 ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1514 ti_sci_put_one_xfer(&info->minfo, xfer);
1520 * ti_sci_cmd_clk_get_freq() - Get current frequency
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 * @freq: Currently frequency in Hz
1528 * Return: 0 if all went well, else returns appropriate error value.
1530 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1531 u32 dev_id, u8 clk_id, u64 *freq)
1533 struct ti_sci_info *info;
1534 struct ti_sci_msg_req_get_clock_freq *req;
1535 struct ti_sci_msg_resp_get_clock_freq *resp;
1536 struct ti_sci_xfer *xfer;
1541 return PTR_ERR(handle);
1542 if (!handle || !freq)
1545 info = handle_to_ti_sci_info(handle);
1548 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1549 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1550 sizeof(*req), sizeof(*resp));
1552 ret = PTR_ERR(xfer);
1553 dev_err(dev, "Message alloc failed(%d)\n", ret);
1556 req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1557 req->dev_id = dev_id;
1558 req->clk_id = clk_id;
1560 ret = ti_sci_do_xfer(info, xfer);
1562 dev_err(dev, "Mbox send fail %d\n", ret);
1566 resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1568 if (!ti_sci_is_response_ack(resp))
1571 *freq = resp->freq_hz;
1574 ti_sci_put_one_xfer(&info->minfo, xfer);
1579 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1581 struct ti_sci_info *info;
1582 struct ti_sci_msg_req_reboot *req;
1583 struct ti_sci_msg_hdr *resp;
1584 struct ti_sci_xfer *xfer;
1589 return PTR_ERR(handle);
1593 info = handle_to_ti_sci_info(handle);
1596 xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1597 TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1598 sizeof(*req), sizeof(*resp));
1600 ret = PTR_ERR(xfer);
1601 dev_err(dev, "Message alloc failed(%d)\n", ret);
1604 req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1606 ret = ti_sci_do_xfer(info, xfer);
1608 dev_err(dev, "Mbox send fail %d\n", ret);
1612 resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1614 if (!ti_sci_is_response_ack(resp))
1620 ti_sci_put_one_xfer(&info->minfo, xfer);
1626 * ti_sci_setup_ops() - Setup the operations structures
1627 * @info: pointer to TISCI pointer
1629 static void ti_sci_setup_ops(struct ti_sci_info *info)
1631 struct ti_sci_ops *ops = &info->handle.ops;
1632 struct ti_sci_core_ops *core_ops = &ops->core_ops;
1633 struct ti_sci_dev_ops *dops = &ops->dev_ops;
1634 struct ti_sci_clk_ops *cops = &ops->clk_ops;
1636 core_ops->reboot_device = ti_sci_cmd_core_reboot;
1638 dops->get_device = ti_sci_cmd_get_device;
1639 dops->idle_device = ti_sci_cmd_idle_device;
1640 dops->put_device = ti_sci_cmd_put_device;
1642 dops->is_valid = ti_sci_cmd_dev_is_valid;
1643 dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
1644 dops->is_idle = ti_sci_cmd_dev_is_idle;
1645 dops->is_stop = ti_sci_cmd_dev_is_stop;
1646 dops->is_on = ti_sci_cmd_dev_is_on;
1647 dops->is_transitioning = ti_sci_cmd_dev_is_trans;
1648 dops->set_device_resets = ti_sci_cmd_set_device_resets;
1649 dops->get_device_resets = ti_sci_cmd_get_device_resets;
1651 cops->get_clock = ti_sci_cmd_get_clock;
1652 cops->idle_clock = ti_sci_cmd_idle_clock;
1653 cops->put_clock = ti_sci_cmd_put_clock;
1654 cops->is_auto = ti_sci_cmd_clk_is_auto;
1655 cops->is_on = ti_sci_cmd_clk_is_on;
1656 cops->is_off = ti_sci_cmd_clk_is_off;
1658 cops->set_parent = ti_sci_cmd_clk_set_parent;
1659 cops->get_parent = ti_sci_cmd_clk_get_parent;
1660 cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
1662 cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
1663 cops->set_freq = ti_sci_cmd_clk_set_freq;
1664 cops->get_freq = ti_sci_cmd_clk_get_freq;
1668 * ti_sci_get_handle() - Get the TI SCI handle for a device
1669 * @dev: Pointer to device for which we want SCI handle
1671 * NOTE: The function does not track individual clients of the framework
1672 * and is expected to be maintained by caller of TI SCI protocol library.
1673 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1674 * Return: pointer to handle if successful, else:
1675 * -EPROBE_DEFER if the instance is not ready
1676 * -ENODEV if the required node handler is missing
1677 * -EINVAL if invalid conditions are encountered.
1679 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
1681 struct device_node *ti_sci_np;
1682 struct list_head *p;
1683 struct ti_sci_handle *handle = NULL;
1684 struct ti_sci_info *info;
1687 pr_err("I need a device pointer\n");
1688 return ERR_PTR(-EINVAL);
1690 ti_sci_np = of_get_parent(dev->of_node);
1692 dev_err(dev, "No OF information\n");
1693 return ERR_PTR(-EINVAL);
1696 mutex_lock(&ti_sci_list_mutex);
1697 list_for_each(p, &ti_sci_list) {
1698 info = list_entry(p, struct ti_sci_info, node);
1699 if (ti_sci_np == info->dev->of_node) {
1700 handle = &info->handle;
1705 mutex_unlock(&ti_sci_list_mutex);
1706 of_node_put(ti_sci_np);
1709 return ERR_PTR(-EPROBE_DEFER);
1713 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
1716 * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
1717 * @handle: Handle acquired by ti_sci_get_handle
1719 * NOTE: The function does not track individual clients of the framework
1720 * and is expected to be maintained by caller of TI SCI protocol library.
1721 * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1723 * Return: 0 is successfully released
1724 * if an error pointer was passed, it returns the error value back,
1725 * if null was passed, it returns -EINVAL;
1727 int ti_sci_put_handle(const struct ti_sci_handle *handle)
1729 struct ti_sci_info *info;
1732 return PTR_ERR(handle);
1736 info = handle_to_ti_sci_info(handle);
1737 mutex_lock(&ti_sci_list_mutex);
1738 if (!WARN_ON(!info->users))
1740 mutex_unlock(&ti_sci_list_mutex);
1744 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
1746 static void devm_ti_sci_release(struct device *dev, void *res)
1748 const struct ti_sci_handle **ptr = res;
1749 const struct ti_sci_handle *handle = *ptr;
1752 ret = ti_sci_put_handle(handle);
1754 dev_err(dev, "failed to put handle %d\n", ret);
1758 * devm_ti_sci_get_handle() - Managed get handle
1759 * @dev: device for which we want SCI handle for.
1761 * NOTE: This releases the handle once the device resources are
1762 * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1763 * The function does not track individual clients of the framework
1764 * and is expected to be maintained by caller of TI SCI protocol library.
1766 * Return: 0 if all went fine, else corresponding error.
1768 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
1770 const struct ti_sci_handle **ptr;
1771 const struct ti_sci_handle *handle;
1773 ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
1775 return ERR_PTR(-ENOMEM);
1776 handle = ti_sci_get_handle(dev);
1778 if (!IS_ERR(handle)) {
1780 devres_add(dev, ptr);
1787 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
1789 static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
1792 struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
1793 const struct ti_sci_handle *handle = &info->handle;
1795 ti_sci_cmd_core_reboot(handle);
1797 /* call fail OR pass, we should not be here in the first place */
1801 /* Description for K2G */
1802 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
1804 /* Conservative duration */
1805 .max_rx_timeout_ms = 1000,
1806 /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1811 static const struct of_device_id ti_sci_of_match[] = {
1812 {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
1815 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
1817 static int ti_sci_probe(struct platform_device *pdev)
1819 struct device *dev = &pdev->dev;
1820 const struct of_device_id *of_id;
1821 const struct ti_sci_desc *desc;
1822 struct ti_sci_xfer *xfer;
1823 struct ti_sci_info *info = NULL;
1824 struct ti_sci_xfers_info *minfo;
1825 struct mbox_client *cl;
1830 of_id = of_match_device(ti_sci_of_match, dev);
1832 dev_err(dev, "OF data missing\n");
1837 info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1843 reboot = of_property_read_bool(dev->of_node,
1844 "ti,system-reboot-controller");
1845 INIT_LIST_HEAD(&info->node);
1846 minfo = &info->minfo;
1849 * Pre-allocate messages
1850 * NEVER allocate more than what we can indicate in hdr.seq
1851 * if we have data description bug, force a fix..
1853 if (WARN_ON(desc->max_msgs >=
1854 1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
1857 minfo->xfer_block = devm_kcalloc(dev,
1859 sizeof(*minfo->xfer_block),
1861 if (!minfo->xfer_block)
1864 minfo->xfer_alloc_table = devm_kzalloc(dev,
1865 BITS_TO_LONGS(desc->max_msgs)
1866 * sizeof(unsigned long),
1868 if (!minfo->xfer_alloc_table)
1870 bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
1872 /* Pre-initialize the buffer pointer to pre-allocated buffers */
1873 for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
1874 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
1876 if (!xfer->xfer_buf)
1879 xfer->tx_message.buf = xfer->xfer_buf;
1880 init_completion(&xfer->done);
1883 ret = ti_sci_debugfs_create(pdev, info);
1885 dev_warn(dev, "Failed to create debug file\n");
1887 platform_set_drvdata(pdev, info);
1891 cl->tx_block = false;
1892 cl->rx_callback = ti_sci_rx_callback;
1893 cl->knows_txdone = true;
1895 spin_lock_init(&minfo->xfer_lock);
1896 sema_init(&minfo->sem_xfer_count, desc->max_msgs);
1898 info->chan_rx = mbox_request_channel_byname(cl, "rx");
1899 if (IS_ERR(info->chan_rx)) {
1900 ret = PTR_ERR(info->chan_rx);
1904 info->chan_tx = mbox_request_channel_byname(cl, "tx");
1905 if (IS_ERR(info->chan_tx)) {
1906 ret = PTR_ERR(info->chan_tx);
1909 ret = ti_sci_cmd_get_revision(info);
1911 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
1915 ti_sci_setup_ops(info);
1918 info->nb.notifier_call = tisci_reboot_handler;
1919 info->nb.priority = 128;
1921 ret = register_restart_handler(&info->nb);
1923 dev_err(dev, "reboot registration fail(%d)\n", ret);
1928 dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1929 info->handle.version.abi_major, info->handle.version.abi_minor,
1930 info->handle.version.firmware_revision,
1931 info->handle.version.firmware_description);
1933 mutex_lock(&ti_sci_list_mutex);
1934 list_add_tail(&info->node, &ti_sci_list);
1935 mutex_unlock(&ti_sci_list_mutex);
1937 return of_platform_populate(dev->of_node, NULL, NULL, dev);
1939 if (!IS_ERR(info->chan_tx))
1940 mbox_free_channel(info->chan_tx);
1941 if (!IS_ERR(info->chan_rx))
1942 mbox_free_channel(info->chan_rx);
1943 debugfs_remove(info->d);
1947 static int ti_sci_remove(struct platform_device *pdev)
1949 struct ti_sci_info *info;
1950 struct device *dev = &pdev->dev;
1953 of_platform_depopulate(dev);
1955 info = platform_get_drvdata(pdev);
1957 if (info->nb.notifier_call)
1958 unregister_restart_handler(&info->nb);
1960 mutex_lock(&ti_sci_list_mutex);
1964 list_del(&info->node);
1965 mutex_unlock(&ti_sci_list_mutex);
1968 ti_sci_debugfs_destroy(pdev, info);
1970 /* Safe to free channels since no more users */
1971 mbox_free_channel(info->chan_tx);
1972 mbox_free_channel(info->chan_rx);
1978 static struct platform_driver ti_sci_driver = {
1979 .probe = ti_sci_probe,
1980 .remove = ti_sci_remove,
1983 .of_match_table = of_match_ptr(ti_sci_of_match),
1986 module_platform_driver(ti_sci_driver);
1988 MODULE_LICENSE("GPL v2");
1989 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1990 MODULE_AUTHOR("Nishanth Menon");
1991 MODULE_ALIAS("platform:ti-sci");