OSDN Git Service

mtd: rawnand: fsmc: Keep bank enable bit set
[uclinux-h8/linux.git] / drivers / firmware / ti_sci.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Texas Instruments System Control Interface Protocol Driver
4  *
5  * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
6  *      Nishanth Menon
7  */
8
9 #define pr_fmt(fmt) "%s: " fmt, __func__
10
11 #include <linux/bitmap.h>
12 #include <linux/debugfs.h>
13 #include <linux/export.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/mailbox_client.h>
17 #include <linux/module.h>
18 #include <linux/of_device.h>
19 #include <linux/semaphore.h>
20 #include <linux/slab.h>
21 #include <linux/soc/ti/ti-msgmgr.h>
22 #include <linux/soc/ti/ti_sci_protocol.h>
23 #include <linux/reboot.h>
24
25 #include "ti_sci.h"
26
27 /* List of all TI SCI devices active in system */
28 static LIST_HEAD(ti_sci_list);
29 /* Protection for the entire list */
30 static DEFINE_MUTEX(ti_sci_list_mutex);
31
32 /**
33  * struct ti_sci_xfer - Structure representing a message flow
34  * @tx_message: Transmit message
35  * @rx_len:     Receive message length
36  * @xfer_buf:   Preallocated buffer to store receive message
37  *              Since we work with request-ACK protocol, we can
38  *              reuse the same buffer for the rx path as we
39  *              use for the tx path.
40  * @done:       completion event
41  */
42 struct ti_sci_xfer {
43         struct ti_msgmgr_message tx_message;
44         u8 rx_len;
45         u8 *xfer_buf;
46         struct completion done;
47 };
48
49 /**
50  * struct ti_sci_xfers_info - Structure to manage transfer information
51  * @sem_xfer_count:     Counting Semaphore for managing max simultaneous
52  *                      Messages.
53  * @xfer_block:         Preallocated Message array
54  * @xfer_alloc_table:   Bitmap table for allocated messages.
55  *                      Index of this bitmap table is also used for message
56  *                      sequence identifier.
57  * @xfer_lock:          Protection for message allocation
58  */
59 struct ti_sci_xfers_info {
60         struct semaphore sem_xfer_count;
61         struct ti_sci_xfer *xfer_block;
62         unsigned long *xfer_alloc_table;
63         /* protect transfer allocation */
64         spinlock_t xfer_lock;
65 };
66
67 /**
68  * struct ti_sci_desc - Description of SoC integration
69  * @default_host_id:    Host identifier representing the compute entity
70  * @max_rx_timeout_ms:  Timeout for communication with SoC (in Milliseconds)
71  * @max_msgs: Maximum number of messages that can be pending
72  *                simultaneously in the system
73  * @max_msg_size: Maximum size of data per message that can be handled.
74  */
75 struct ti_sci_desc {
76         u8 default_host_id;
77         int max_rx_timeout_ms;
78         int max_msgs;
79         int max_msg_size;
80 };
81
82 /**
83  * struct ti_sci_info - Structure representing a TI SCI instance
84  * @dev:        Device pointer
85  * @desc:       SoC description for this instance
86  * @nb: Reboot Notifier block
87  * @d:          Debugfs file entry
88  * @debug_region: Memory region where the debug message are available
89  * @debug_region_size: Debug region size
90  * @debug_buffer: Buffer allocated to copy debug messages.
91  * @handle:     Instance of TI SCI handle to send to clients.
92  * @cl:         Mailbox Client
93  * @chan_tx:    Transmit mailbox channel
94  * @chan_rx:    Receive mailbox channel
95  * @minfo:      Message info
96  * @node:       list head
97  * @host_id:    Host ID
98  * @users:      Number of users of this instance
99  */
100 struct ti_sci_info {
101         struct device *dev;
102         struct notifier_block nb;
103         const struct ti_sci_desc *desc;
104         struct dentry *d;
105         void __iomem *debug_region;
106         char *debug_buffer;
107         size_t debug_region_size;
108         struct ti_sci_handle handle;
109         struct mbox_client cl;
110         struct mbox_chan *chan_tx;
111         struct mbox_chan *chan_rx;
112         struct ti_sci_xfers_info minfo;
113         struct list_head node;
114         u8 host_id;
115         /* protected by ti_sci_list_mutex */
116         int users;
117
118 };
119
120 #define cl_to_ti_sci_info(c)    container_of(c, struct ti_sci_info, cl)
121 #define handle_to_ti_sci_info(h) container_of(h, struct ti_sci_info, handle)
122 #define reboot_to_ti_sci_info(n) container_of(n, struct ti_sci_info, nb)
123
124 #ifdef CONFIG_DEBUG_FS
125
126 /**
127  * ti_sci_debug_show() - Helper to dump the debug log
128  * @s:  sequence file pointer
129  * @unused:     unused.
130  *
131  * Return: 0
132  */
133 static int ti_sci_debug_show(struct seq_file *s, void *unused)
134 {
135         struct ti_sci_info *info = s->private;
136
137         memcpy_fromio(info->debug_buffer, info->debug_region,
138                       info->debug_region_size);
139         /*
140          * We don't trust firmware to leave NULL terminated last byte (hence
141          * we have allocated 1 extra 0 byte). Since we cannot guarantee any
142          * specific data format for debug messages, We just present the data
143          * in the buffer as is - we expect the messages to be self explanatory.
144          */
145         seq_puts(s, info->debug_buffer);
146         return 0;
147 }
148
149 /**
150  * ti_sci_debug_open() - debug file open
151  * @inode:      inode pointer
152  * @file:       file pointer
153  *
154  * Return: result of single_open
155  */
156 static int ti_sci_debug_open(struct inode *inode, struct file *file)
157 {
158         return single_open(file, ti_sci_debug_show, inode->i_private);
159 }
160
161 /* log file operations */
162 static const struct file_operations ti_sci_debug_fops = {
163         .open = ti_sci_debug_open,
164         .read = seq_read,
165         .llseek = seq_lseek,
166         .release = single_release,
167 };
168
169 /**
170  * ti_sci_debugfs_create() - Create log debug file
171  * @pdev:       platform device pointer
172  * @info:       Pointer to SCI entity information
173  *
174  * Return: 0 if all went fine, else corresponding error.
175  */
176 static int ti_sci_debugfs_create(struct platform_device *pdev,
177                                  struct ti_sci_info *info)
178 {
179         struct device *dev = &pdev->dev;
180         struct resource *res;
181         char debug_name[50] = "ti_sci_debug@";
182
183         /* Debug region is optional */
184         res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
185                                            "debug_messages");
186         info->debug_region = devm_ioremap_resource(dev, res);
187         if (IS_ERR(info->debug_region))
188                 return 0;
189         info->debug_region_size = resource_size(res);
190
191         info->debug_buffer = devm_kcalloc(dev, info->debug_region_size + 1,
192                                           sizeof(char), GFP_KERNEL);
193         if (!info->debug_buffer)
194                 return -ENOMEM;
195         /* Setup NULL termination */
196         info->debug_buffer[info->debug_region_size] = 0;
197
198         info->d = debugfs_create_file(strncat(debug_name, dev_name(dev),
199                                               sizeof(debug_name) -
200                                               sizeof("ti_sci_debug@")),
201                                       0444, NULL, info, &ti_sci_debug_fops);
202         if (IS_ERR(info->d))
203                 return PTR_ERR(info->d);
204
205         dev_dbg(dev, "Debug region => %p, size = %zu bytes, resource: %pr\n",
206                 info->debug_region, info->debug_region_size, res);
207         return 0;
208 }
209
210 /**
211  * ti_sci_debugfs_destroy() - clean up log debug file
212  * @pdev:       platform device pointer
213  * @info:       Pointer to SCI entity information
214  */
215 static void ti_sci_debugfs_destroy(struct platform_device *pdev,
216                                    struct ti_sci_info *info)
217 {
218         if (IS_ERR(info->debug_region))
219                 return;
220
221         debugfs_remove(info->d);
222 }
223 #else /* CONFIG_DEBUG_FS */
224 static inline int ti_sci_debugfs_create(struct platform_device *dev,
225                                         struct ti_sci_info *info)
226 {
227         return 0;
228 }
229
230 static inline void ti_sci_debugfs_destroy(struct platform_device *dev,
231                                           struct ti_sci_info *info)
232 {
233 }
234 #endif /* CONFIG_DEBUG_FS */
235
236 /**
237  * ti_sci_dump_header_dbg() - Helper to dump a message header.
238  * @dev:        Device pointer corresponding to the SCI entity
239  * @hdr:        pointer to header.
240  */
241 static inline void ti_sci_dump_header_dbg(struct device *dev,
242                                           struct ti_sci_msg_hdr *hdr)
243 {
244         dev_dbg(dev, "MSGHDR:type=0x%04x host=0x%02x seq=0x%02x flags=0x%08x\n",
245                 hdr->type, hdr->host, hdr->seq, hdr->flags);
246 }
247
248 /**
249  * ti_sci_rx_callback() - mailbox client callback for receive messages
250  * @cl: client pointer
251  * @m:  mailbox message
252  *
253  * Processes one received message to appropriate transfer information and
254  * signals completion of the transfer.
255  *
256  * NOTE: This function will be invoked in IRQ context, hence should be
257  * as optimal as possible.
258  */
259 static void ti_sci_rx_callback(struct mbox_client *cl, void *m)
260 {
261         struct ti_sci_info *info = cl_to_ti_sci_info(cl);
262         struct device *dev = info->dev;
263         struct ti_sci_xfers_info *minfo = &info->minfo;
264         struct ti_msgmgr_message *mbox_msg = m;
265         struct ti_sci_msg_hdr *hdr = (struct ti_sci_msg_hdr *)mbox_msg->buf;
266         struct ti_sci_xfer *xfer;
267         u8 xfer_id;
268
269         xfer_id = hdr->seq;
270
271         /*
272          * Are we even expecting this?
273          * NOTE: barriers were implicit in locks used for modifying the bitmap
274          */
275         if (!test_bit(xfer_id, minfo->xfer_alloc_table)) {
276                 dev_err(dev, "Message for %d is not expected!\n", xfer_id);
277                 return;
278         }
279
280         xfer = &minfo->xfer_block[xfer_id];
281
282         /* Is the message of valid length? */
283         if (mbox_msg->len > info->desc->max_msg_size) {
284                 dev_err(dev, "Unable to handle %zu xfer(max %d)\n",
285                         mbox_msg->len, info->desc->max_msg_size);
286                 ti_sci_dump_header_dbg(dev, hdr);
287                 return;
288         }
289         if (mbox_msg->len < xfer->rx_len) {
290                 dev_err(dev, "Recv xfer %zu < expected %d length\n",
291                         mbox_msg->len, xfer->rx_len);
292                 ti_sci_dump_header_dbg(dev, hdr);
293                 return;
294         }
295
296         ti_sci_dump_header_dbg(dev, hdr);
297         /* Take a copy to the rx buffer.. */
298         memcpy(xfer->xfer_buf, mbox_msg->buf, xfer->rx_len);
299         complete(&xfer->done);
300 }
301
302 /**
303  * ti_sci_get_one_xfer() - Allocate one message
304  * @info:       Pointer to SCI entity information
305  * @msg_type:   Message type
306  * @msg_flags:  Flag to set for the message
307  * @tx_message_size: transmit message size
308  * @rx_message_size: receive message size
309  *
310  * Helper function which is used by various command functions that are
311  * exposed to clients of this driver for allocating a message traffic event.
312  *
313  * This function can sleep depending on pending requests already in the system
314  * for the SCI entity. Further, this also holds a spinlock to maintain integrity
315  * of internal data structures.
316  *
317  * Return: 0 if all went fine, else corresponding error.
318  */
319 static struct ti_sci_xfer *ti_sci_get_one_xfer(struct ti_sci_info *info,
320                                                u16 msg_type, u32 msg_flags,
321                                                size_t tx_message_size,
322                                                size_t rx_message_size)
323 {
324         struct ti_sci_xfers_info *minfo = &info->minfo;
325         struct ti_sci_xfer *xfer;
326         struct ti_sci_msg_hdr *hdr;
327         unsigned long flags;
328         unsigned long bit_pos;
329         u8 xfer_id;
330         int ret;
331         int timeout;
332
333         /* Ensure we have sane transfer sizes */
334         if (rx_message_size > info->desc->max_msg_size ||
335             tx_message_size > info->desc->max_msg_size ||
336             rx_message_size < sizeof(*hdr) || tx_message_size < sizeof(*hdr))
337                 return ERR_PTR(-ERANGE);
338
339         /*
340          * Ensure we have only controlled number of pending messages.
341          * Ideally, we might just have to wait a single message, be
342          * conservative and wait 5 times that..
343          */
344         timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms) * 5;
345         ret = down_timeout(&minfo->sem_xfer_count, timeout);
346         if (ret < 0)
347                 return ERR_PTR(ret);
348
349         /* Keep the locked section as small as possible */
350         spin_lock_irqsave(&minfo->xfer_lock, flags);
351         bit_pos = find_first_zero_bit(minfo->xfer_alloc_table,
352                                       info->desc->max_msgs);
353         set_bit(bit_pos, minfo->xfer_alloc_table);
354         spin_unlock_irqrestore(&minfo->xfer_lock, flags);
355
356         /*
357          * We already ensured in probe that we can have max messages that can
358          * fit in  hdr.seq - NOTE: this improves access latencies
359          * to predictable O(1) access, BUT, it opens us to risk if
360          * remote misbehaves with corrupted message sequence responses.
361          * If that happens, we are going to be messed up anyways..
362          */
363         xfer_id = (u8)bit_pos;
364
365         xfer = &minfo->xfer_block[xfer_id];
366
367         hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
368         xfer->tx_message.len = tx_message_size;
369         xfer->rx_len = (u8)rx_message_size;
370
371         reinit_completion(&xfer->done);
372
373         hdr->seq = xfer_id;
374         hdr->type = msg_type;
375         hdr->host = info->host_id;
376         hdr->flags = msg_flags;
377
378         return xfer;
379 }
380
381 /**
382  * ti_sci_put_one_xfer() - Release a message
383  * @minfo:      transfer info pointer
384  * @xfer:       message that was reserved by ti_sci_get_one_xfer
385  *
386  * This holds a spinlock to maintain integrity of internal data structures.
387  */
388 static void ti_sci_put_one_xfer(struct ti_sci_xfers_info *minfo,
389                                 struct ti_sci_xfer *xfer)
390 {
391         unsigned long flags;
392         struct ti_sci_msg_hdr *hdr;
393         u8 xfer_id;
394
395         hdr = (struct ti_sci_msg_hdr *)xfer->tx_message.buf;
396         xfer_id = hdr->seq;
397
398         /*
399          * Keep the locked section as small as possible
400          * NOTE: we might escape with smp_mb and no lock here..
401          * but just be conservative and symmetric.
402          */
403         spin_lock_irqsave(&minfo->xfer_lock, flags);
404         clear_bit(xfer_id, minfo->xfer_alloc_table);
405         spin_unlock_irqrestore(&minfo->xfer_lock, flags);
406
407         /* Increment the count for the next user to get through */
408         up(&minfo->sem_xfer_count);
409 }
410
411 /**
412  * ti_sci_do_xfer() - Do one transfer
413  * @info:       Pointer to SCI entity information
414  * @xfer:       Transfer to initiate and wait for response
415  *
416  * Return: -ETIMEDOUT in case of no response, if transmit error,
417  *         return corresponding error, else if all goes well,
418  *         return 0.
419  */
420 static inline int ti_sci_do_xfer(struct ti_sci_info *info,
421                                  struct ti_sci_xfer *xfer)
422 {
423         int ret;
424         int timeout;
425         struct device *dev = info->dev;
426
427         ret = mbox_send_message(info->chan_tx, &xfer->tx_message);
428         if (ret < 0)
429                 return ret;
430
431         ret = 0;
432
433         /* And we wait for the response. */
434         timeout = msecs_to_jiffies(info->desc->max_rx_timeout_ms);
435         if (!wait_for_completion_timeout(&xfer->done, timeout)) {
436                 dev_err(dev, "Mbox timedout in resp(caller: %pS)\n",
437                         (void *)_RET_IP_);
438                 ret = -ETIMEDOUT;
439         }
440         /*
441          * NOTE: we might prefer not to need the mailbox ticker to manage the
442          * transfer queueing since the protocol layer queues things by itself.
443          * Unfortunately, we have to kick the mailbox framework after we have
444          * received our message.
445          */
446         mbox_client_txdone(info->chan_tx, ret);
447
448         return ret;
449 }
450
451 /**
452  * ti_sci_cmd_get_revision() - command to get the revision of the SCI entity
453  * @info:       Pointer to SCI entity information
454  *
455  * Updates the SCI information in the internal data structure.
456  *
457  * Return: 0 if all went fine, else return appropriate error.
458  */
459 static int ti_sci_cmd_get_revision(struct ti_sci_info *info)
460 {
461         struct device *dev = info->dev;
462         struct ti_sci_handle *handle = &info->handle;
463         struct ti_sci_version_info *ver = &handle->version;
464         struct ti_sci_msg_resp_version *rev_info;
465         struct ti_sci_xfer *xfer;
466         int ret;
467
468         /* No need to setup flags since it is expected to respond */
469         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_VERSION,
470                                    0x0, sizeof(struct ti_sci_msg_hdr),
471                                    sizeof(*rev_info));
472         if (IS_ERR(xfer)) {
473                 ret = PTR_ERR(xfer);
474                 dev_err(dev, "Message alloc failed(%d)\n", ret);
475                 return ret;
476         }
477
478         rev_info = (struct ti_sci_msg_resp_version *)xfer->xfer_buf;
479
480         ret = ti_sci_do_xfer(info, xfer);
481         if (ret) {
482                 dev_err(dev, "Mbox send fail %d\n", ret);
483                 goto fail;
484         }
485
486         ver->abi_major = rev_info->abi_major;
487         ver->abi_minor = rev_info->abi_minor;
488         ver->firmware_revision = rev_info->firmware_revision;
489         strncpy(ver->firmware_description, rev_info->firmware_description,
490                 sizeof(ver->firmware_description));
491
492 fail:
493         ti_sci_put_one_xfer(&info->minfo, xfer);
494         return ret;
495 }
496
497 /**
498  * ti_sci_is_response_ack() - Generic ACK/NACK message checkup
499  * @r:  pointer to response buffer
500  *
501  * Return: true if the response was an ACK, else returns false.
502  */
503 static inline bool ti_sci_is_response_ack(void *r)
504 {
505         struct ti_sci_msg_hdr *hdr = r;
506
507         return hdr->flags & TI_SCI_FLAG_RESP_GENERIC_ACK ? true : false;
508 }
509
510 /**
511  * ti_sci_set_device_state() - Set device state helper
512  * @handle:     pointer to TI SCI handle
513  * @id:         Device identifier
514  * @flags:      flags to setup for the device
515  * @state:      State to move the device to
516  *
517  * Return: 0 if all went well, else returns appropriate error value.
518  */
519 static int ti_sci_set_device_state(const struct ti_sci_handle *handle,
520                                    u32 id, u32 flags, u8 state)
521 {
522         struct ti_sci_info *info;
523         struct ti_sci_msg_req_set_device_state *req;
524         struct ti_sci_msg_hdr *resp;
525         struct ti_sci_xfer *xfer;
526         struct device *dev;
527         int ret = 0;
528
529         if (IS_ERR(handle))
530                 return PTR_ERR(handle);
531         if (!handle)
532                 return -EINVAL;
533
534         info = handle_to_ti_sci_info(handle);
535         dev = info->dev;
536
537         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_STATE,
538                                    flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
539                                    sizeof(*req), sizeof(*resp));
540         if (IS_ERR(xfer)) {
541                 ret = PTR_ERR(xfer);
542                 dev_err(dev, "Message alloc failed(%d)\n", ret);
543                 return ret;
544         }
545         req = (struct ti_sci_msg_req_set_device_state *)xfer->xfer_buf;
546         req->id = id;
547         req->state = state;
548
549         ret = ti_sci_do_xfer(info, xfer);
550         if (ret) {
551                 dev_err(dev, "Mbox send fail %d\n", ret);
552                 goto fail;
553         }
554
555         resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
556
557         ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
558
559 fail:
560         ti_sci_put_one_xfer(&info->minfo, xfer);
561
562         return ret;
563 }
564
565 /**
566  * ti_sci_get_device_state() - Get device state helper
567  * @handle:     Handle to the device
568  * @id:         Device Identifier
569  * @clcnt:      Pointer to Context Loss Count
570  * @resets:     pointer to resets
571  * @p_state:    pointer to p_state
572  * @c_state:    pointer to c_state
573  *
574  * Return: 0 if all went fine, else return appropriate error.
575  */
576 static int ti_sci_get_device_state(const struct ti_sci_handle *handle,
577                                    u32 id,  u32 *clcnt,  u32 *resets,
578                                     u8 *p_state,  u8 *c_state)
579 {
580         struct ti_sci_info *info;
581         struct ti_sci_msg_req_get_device_state *req;
582         struct ti_sci_msg_resp_get_device_state *resp;
583         struct ti_sci_xfer *xfer;
584         struct device *dev;
585         int ret = 0;
586
587         if (IS_ERR(handle))
588                 return PTR_ERR(handle);
589         if (!handle)
590                 return -EINVAL;
591
592         if (!clcnt && !resets && !p_state && !c_state)
593                 return -EINVAL;
594
595         info = handle_to_ti_sci_info(handle);
596         dev = info->dev;
597
598         /* Response is expected, so need of any flags */
599         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_DEVICE_STATE,
600                                    0, sizeof(*req), sizeof(*resp));
601         if (IS_ERR(xfer)) {
602                 ret = PTR_ERR(xfer);
603                 dev_err(dev, "Message alloc failed(%d)\n", ret);
604                 return ret;
605         }
606         req = (struct ti_sci_msg_req_get_device_state *)xfer->xfer_buf;
607         req->id = id;
608
609         ret = ti_sci_do_xfer(info, xfer);
610         if (ret) {
611                 dev_err(dev, "Mbox send fail %d\n", ret);
612                 goto fail;
613         }
614
615         resp = (struct ti_sci_msg_resp_get_device_state *)xfer->xfer_buf;
616         if (!ti_sci_is_response_ack(resp)) {
617                 ret = -ENODEV;
618                 goto fail;
619         }
620
621         if (clcnt)
622                 *clcnt = resp->context_loss_count;
623         if (resets)
624                 *resets = resp->resets;
625         if (p_state)
626                 *p_state = resp->programmed_state;
627         if (c_state)
628                 *c_state = resp->current_state;
629 fail:
630         ti_sci_put_one_xfer(&info->minfo, xfer);
631
632         return ret;
633 }
634
635 /**
636  * ti_sci_cmd_get_device() - command to request for device managed by TISCI
637  * @handle:     Pointer to TISCI handle as retrieved by *ti_sci_get_handle
638  * @id:         Device Identifier
639  *
640  * Request for the device - NOTE: the client MUST maintain integrity of
641  * usage count by balancing get_device with put_device. No refcounting is
642  * managed by driver for that purpose.
643  *
644  * NOTE: The request is for exclusive access for the processor.
645  *
646  * Return: 0 if all went fine, else return appropriate error.
647  */
648 static int ti_sci_cmd_get_device(const struct ti_sci_handle *handle, u32 id)
649 {
650         return ti_sci_set_device_state(handle, id,
651                                        MSG_FLAG_DEVICE_EXCLUSIVE,
652                                        MSG_DEVICE_SW_STATE_ON);
653 }
654
655 /**
656  * ti_sci_cmd_idle_device() - Command to idle a device managed by TISCI
657  * @handle:     Pointer to TISCI handle as retrieved by *ti_sci_get_handle
658  * @id:         Device Identifier
659  *
660  * Request for the device - NOTE: the client MUST maintain integrity of
661  * usage count by balancing get_device with put_device. No refcounting is
662  * managed by driver for that purpose.
663  *
664  * Return: 0 if all went fine, else return appropriate error.
665  */
666 static int ti_sci_cmd_idle_device(const struct ti_sci_handle *handle, u32 id)
667 {
668         return ti_sci_set_device_state(handle, id,
669                                        MSG_FLAG_DEVICE_EXCLUSIVE,
670                                        MSG_DEVICE_SW_STATE_RETENTION);
671 }
672
673 /**
674  * ti_sci_cmd_put_device() - command to release a device managed by TISCI
675  * @handle:     Pointer to TISCI handle as retrieved by *ti_sci_get_handle
676  * @id:         Device Identifier
677  *
678  * Request for the device - NOTE: the client MUST maintain integrity of
679  * usage count by balancing get_device with put_device. No refcounting is
680  * managed by driver for that purpose.
681  *
682  * Return: 0 if all went fine, else return appropriate error.
683  */
684 static int ti_sci_cmd_put_device(const struct ti_sci_handle *handle, u32 id)
685 {
686         return ti_sci_set_device_state(handle, id,
687                                        0, MSG_DEVICE_SW_STATE_AUTO_OFF);
688 }
689
690 /**
691  * ti_sci_cmd_dev_is_valid() - Is the device valid
692  * @handle:     Pointer to TISCI handle as retrieved by *ti_sci_get_handle
693  * @id:         Device Identifier
694  *
695  * Return: 0 if all went fine and the device ID is valid, else return
696  * appropriate error.
697  */
698 static int ti_sci_cmd_dev_is_valid(const struct ti_sci_handle *handle, u32 id)
699 {
700         u8 unused;
701
702         /* check the device state which will also tell us if the ID is valid */
703         return ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &unused);
704 }
705
706 /**
707  * ti_sci_cmd_dev_get_clcnt() - Get context loss counter
708  * @handle:     Pointer to TISCI handle
709  * @id:         Device Identifier
710  * @count:      Pointer to Context Loss counter to populate
711  *
712  * Return: 0 if all went fine, else return appropriate error.
713  */
714 static int ti_sci_cmd_dev_get_clcnt(const struct ti_sci_handle *handle, u32 id,
715                                     u32 *count)
716 {
717         return ti_sci_get_device_state(handle, id, count, NULL, NULL, NULL);
718 }
719
720 /**
721  * ti_sci_cmd_dev_is_idle() - Check if the device is requested to be idle
722  * @handle:     Pointer to TISCI handle
723  * @id:         Device Identifier
724  * @r_state:    true if requested to be idle
725  *
726  * Return: 0 if all went fine, else return appropriate error.
727  */
728 static int ti_sci_cmd_dev_is_idle(const struct ti_sci_handle *handle, u32 id,
729                                   bool *r_state)
730 {
731         int ret;
732         u8 state;
733
734         if (!r_state)
735                 return -EINVAL;
736
737         ret = ti_sci_get_device_state(handle, id, NULL, NULL, &state, NULL);
738         if (ret)
739                 return ret;
740
741         *r_state = (state == MSG_DEVICE_SW_STATE_RETENTION);
742
743         return 0;
744 }
745
746 /**
747  * ti_sci_cmd_dev_is_stop() - Check if the device is requested to be stopped
748  * @handle:     Pointer to TISCI handle
749  * @id:         Device Identifier
750  * @r_state:    true if requested to be stopped
751  * @curr_state: true if currently stopped.
752  *
753  * Return: 0 if all went fine, else return appropriate error.
754  */
755 static int ti_sci_cmd_dev_is_stop(const struct ti_sci_handle *handle, u32 id,
756                                   bool *r_state,  bool *curr_state)
757 {
758         int ret;
759         u8 p_state, c_state;
760
761         if (!r_state && !curr_state)
762                 return -EINVAL;
763
764         ret =
765             ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
766         if (ret)
767                 return ret;
768
769         if (r_state)
770                 *r_state = (p_state == MSG_DEVICE_SW_STATE_AUTO_OFF);
771         if (curr_state)
772                 *curr_state = (c_state == MSG_DEVICE_HW_STATE_OFF);
773
774         return 0;
775 }
776
777 /**
778  * ti_sci_cmd_dev_is_on() - Check if the device is requested to be ON
779  * @handle:     Pointer to TISCI handle
780  * @id:         Device Identifier
781  * @r_state:    true if requested to be ON
782  * @curr_state: true if currently ON and active
783  *
784  * Return: 0 if all went fine, else return appropriate error.
785  */
786 static int ti_sci_cmd_dev_is_on(const struct ti_sci_handle *handle, u32 id,
787                                 bool *r_state,  bool *curr_state)
788 {
789         int ret;
790         u8 p_state, c_state;
791
792         if (!r_state && !curr_state)
793                 return -EINVAL;
794
795         ret =
796             ti_sci_get_device_state(handle, id, NULL, NULL, &p_state, &c_state);
797         if (ret)
798                 return ret;
799
800         if (r_state)
801                 *r_state = (p_state == MSG_DEVICE_SW_STATE_ON);
802         if (curr_state)
803                 *curr_state = (c_state == MSG_DEVICE_HW_STATE_ON);
804
805         return 0;
806 }
807
808 /**
809  * ti_sci_cmd_dev_is_trans() - Check if the device is currently transitioning
810  * @handle:     Pointer to TISCI handle
811  * @id:         Device Identifier
812  * @curr_state: true if currently transitioning.
813  *
814  * Return: 0 if all went fine, else return appropriate error.
815  */
816 static int ti_sci_cmd_dev_is_trans(const struct ti_sci_handle *handle, u32 id,
817                                    bool *curr_state)
818 {
819         int ret;
820         u8 state;
821
822         if (!curr_state)
823                 return -EINVAL;
824
825         ret = ti_sci_get_device_state(handle, id, NULL, NULL, NULL, &state);
826         if (ret)
827                 return ret;
828
829         *curr_state = (state == MSG_DEVICE_HW_STATE_TRANS);
830
831         return 0;
832 }
833
834 /**
835  * ti_sci_cmd_set_device_resets() - command to set resets for device managed
836  *                                  by TISCI
837  * @handle:     Pointer to TISCI handle as retrieved by *ti_sci_get_handle
838  * @id:         Device Identifier
839  * @reset_state: Device specific reset bit field
840  *
841  * Return: 0 if all went fine, else return appropriate error.
842  */
843 static int ti_sci_cmd_set_device_resets(const struct ti_sci_handle *handle,
844                                         u32 id, u32 reset_state)
845 {
846         struct ti_sci_info *info;
847         struct ti_sci_msg_req_set_device_resets *req;
848         struct ti_sci_msg_hdr *resp;
849         struct ti_sci_xfer *xfer;
850         struct device *dev;
851         int ret = 0;
852
853         if (IS_ERR(handle))
854                 return PTR_ERR(handle);
855         if (!handle)
856                 return -EINVAL;
857
858         info = handle_to_ti_sci_info(handle);
859         dev = info->dev;
860
861         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_DEVICE_RESETS,
862                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
863                                    sizeof(*req), sizeof(*resp));
864         if (IS_ERR(xfer)) {
865                 ret = PTR_ERR(xfer);
866                 dev_err(dev, "Message alloc failed(%d)\n", ret);
867                 return ret;
868         }
869         req = (struct ti_sci_msg_req_set_device_resets *)xfer->xfer_buf;
870         req->id = id;
871         req->resets = reset_state;
872
873         ret = ti_sci_do_xfer(info, xfer);
874         if (ret) {
875                 dev_err(dev, "Mbox send fail %d\n", ret);
876                 goto fail;
877         }
878
879         resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
880
881         ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
882
883 fail:
884         ti_sci_put_one_xfer(&info->minfo, xfer);
885
886         return ret;
887 }
888
889 /**
890  * ti_sci_cmd_get_device_resets() - Get reset state for device managed
891  *                                  by TISCI
892  * @handle:             Pointer to TISCI handle
893  * @id:                 Device Identifier
894  * @reset_state:        Pointer to reset state to populate
895  *
896  * Return: 0 if all went fine, else return appropriate error.
897  */
898 static int ti_sci_cmd_get_device_resets(const struct ti_sci_handle *handle,
899                                         u32 id, u32 *reset_state)
900 {
901         return ti_sci_get_device_state(handle, id, NULL, reset_state, NULL,
902                                        NULL);
903 }
904
905 /**
906  * ti_sci_set_clock_state() - Set clock state helper
907  * @handle:     pointer to TI SCI handle
908  * @dev_id:     Device identifier this request is for
909  * @clk_id:     Clock identifier for the device for this request.
910  *              Each device has it's own set of clock inputs. This indexes
911  *              which clock input to modify.
912  * @flags:      Header flags as needed
913  * @state:      State to request for the clock.
914  *
915  * Return: 0 if all went well, else returns appropriate error value.
916  */
917 static int ti_sci_set_clock_state(const struct ti_sci_handle *handle,
918                                   u32 dev_id, u8 clk_id,
919                                   u32 flags, u8 state)
920 {
921         struct ti_sci_info *info;
922         struct ti_sci_msg_req_set_clock_state *req;
923         struct ti_sci_msg_hdr *resp;
924         struct ti_sci_xfer *xfer;
925         struct device *dev;
926         int ret = 0;
927
928         if (IS_ERR(handle))
929                 return PTR_ERR(handle);
930         if (!handle)
931                 return -EINVAL;
932
933         info = handle_to_ti_sci_info(handle);
934         dev = info->dev;
935
936         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_STATE,
937                                    flags | TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
938                                    sizeof(*req), sizeof(*resp));
939         if (IS_ERR(xfer)) {
940                 ret = PTR_ERR(xfer);
941                 dev_err(dev, "Message alloc failed(%d)\n", ret);
942                 return ret;
943         }
944         req = (struct ti_sci_msg_req_set_clock_state *)xfer->xfer_buf;
945         req->dev_id = dev_id;
946         req->clk_id = clk_id;
947         req->request_state = state;
948
949         ret = ti_sci_do_xfer(info, xfer);
950         if (ret) {
951                 dev_err(dev, "Mbox send fail %d\n", ret);
952                 goto fail;
953         }
954
955         resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
956
957         ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
958
959 fail:
960         ti_sci_put_one_xfer(&info->minfo, xfer);
961
962         return ret;
963 }
964
965 /**
966  * ti_sci_cmd_get_clock_state() - Get clock state helper
967  * @handle:     pointer to TI SCI handle
968  * @dev_id:     Device identifier this request is for
969  * @clk_id:     Clock identifier for the device for this request.
970  *              Each device has it's own set of clock inputs. This indexes
971  *              which clock input to modify.
972  * @programmed_state:   State requested for clock to move to
973  * @current_state:      State that the clock is currently in
974  *
975  * Return: 0 if all went well, else returns appropriate error value.
976  */
977 static int ti_sci_cmd_get_clock_state(const struct ti_sci_handle *handle,
978                                       u32 dev_id, u8 clk_id,
979                                       u8 *programmed_state, u8 *current_state)
980 {
981         struct ti_sci_info *info;
982         struct ti_sci_msg_req_get_clock_state *req;
983         struct ti_sci_msg_resp_get_clock_state *resp;
984         struct ti_sci_xfer *xfer;
985         struct device *dev;
986         int ret = 0;
987
988         if (IS_ERR(handle))
989                 return PTR_ERR(handle);
990         if (!handle)
991                 return -EINVAL;
992
993         if (!programmed_state && !current_state)
994                 return -EINVAL;
995
996         info = handle_to_ti_sci_info(handle);
997         dev = info->dev;
998
999         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_STATE,
1000                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1001                                    sizeof(*req), sizeof(*resp));
1002         if (IS_ERR(xfer)) {
1003                 ret = PTR_ERR(xfer);
1004                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1005                 return ret;
1006         }
1007         req = (struct ti_sci_msg_req_get_clock_state *)xfer->xfer_buf;
1008         req->dev_id = dev_id;
1009         req->clk_id = clk_id;
1010
1011         ret = ti_sci_do_xfer(info, xfer);
1012         if (ret) {
1013                 dev_err(dev, "Mbox send fail %d\n", ret);
1014                 goto fail;
1015         }
1016
1017         resp = (struct ti_sci_msg_resp_get_clock_state *)xfer->xfer_buf;
1018
1019         if (!ti_sci_is_response_ack(resp)) {
1020                 ret = -ENODEV;
1021                 goto fail;
1022         }
1023
1024         if (programmed_state)
1025                 *programmed_state = resp->programmed_state;
1026         if (current_state)
1027                 *current_state = resp->current_state;
1028
1029 fail:
1030         ti_sci_put_one_xfer(&info->minfo, xfer);
1031
1032         return ret;
1033 }
1034
1035 /**
1036  * ti_sci_cmd_get_clock() - Get control of a clock from TI SCI
1037  * @handle:     pointer to TI SCI handle
1038  * @dev_id:     Device identifier this request is for
1039  * @clk_id:     Clock identifier for the device for this request.
1040  *              Each device has it's own set of clock inputs. This indexes
1041  *              which clock input to modify.
1042  * @needs_ssc: 'true' if Spread Spectrum clock is desired, else 'false'
1043  * @can_change_freq: 'true' if frequency change is desired, else 'false'
1044  * @enable_input_term: 'true' if input termination is desired, else 'false'
1045  *
1046  * Return: 0 if all went well, else returns appropriate error value.
1047  */
1048 static int ti_sci_cmd_get_clock(const struct ti_sci_handle *handle, u32 dev_id,
1049                                 u8 clk_id, bool needs_ssc, bool can_change_freq,
1050                                 bool enable_input_term)
1051 {
1052         u32 flags = 0;
1053
1054         flags |= needs_ssc ? MSG_FLAG_CLOCK_ALLOW_SSC : 0;
1055         flags |= can_change_freq ? MSG_FLAG_CLOCK_ALLOW_FREQ_CHANGE : 0;
1056         flags |= enable_input_term ? MSG_FLAG_CLOCK_INPUT_TERM : 0;
1057
1058         return ti_sci_set_clock_state(handle, dev_id, clk_id, flags,
1059                                       MSG_CLOCK_SW_STATE_REQ);
1060 }
1061
1062 /**
1063  * ti_sci_cmd_idle_clock() - Idle a clock which is in our control
1064  * @handle:     pointer to TI SCI handle
1065  * @dev_id:     Device identifier this request is for
1066  * @clk_id:     Clock identifier for the device for this request.
1067  *              Each device has it's own set of clock inputs. This indexes
1068  *              which clock input to modify.
1069  *
1070  * NOTE: This clock must have been requested by get_clock previously.
1071  *
1072  * Return: 0 if all went well, else returns appropriate error value.
1073  */
1074 static int ti_sci_cmd_idle_clock(const struct ti_sci_handle *handle,
1075                                  u32 dev_id, u8 clk_id)
1076 {
1077         return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1078                                       MSG_CLOCK_SW_STATE_UNREQ);
1079 }
1080
1081 /**
1082  * ti_sci_cmd_put_clock() - Release a clock from our control back to TISCI
1083  * @handle:     pointer to TI SCI handle
1084  * @dev_id:     Device identifier this request is for
1085  * @clk_id:     Clock identifier for the device for this request.
1086  *              Each device has it's own set of clock inputs. This indexes
1087  *              which clock input to modify.
1088  *
1089  * NOTE: This clock must have been requested by get_clock previously.
1090  *
1091  * Return: 0 if all went well, else returns appropriate error value.
1092  */
1093 static int ti_sci_cmd_put_clock(const struct ti_sci_handle *handle,
1094                                 u32 dev_id, u8 clk_id)
1095 {
1096         return ti_sci_set_clock_state(handle, dev_id, clk_id, 0,
1097                                       MSG_CLOCK_SW_STATE_AUTO);
1098 }
1099
1100 /**
1101  * ti_sci_cmd_clk_is_auto() - Is the clock being auto managed
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.
1107  * @req_state: state indicating if the clock is auto managed
1108  *
1109  * Return: 0 if all went well, else returns appropriate error value.
1110  */
1111 static int ti_sci_cmd_clk_is_auto(const struct ti_sci_handle *handle,
1112                                   u32 dev_id, u8 clk_id, bool *req_state)
1113 {
1114         u8 state = 0;
1115         int ret;
1116
1117         if (!req_state)
1118                 return -EINVAL;
1119
1120         ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id, &state, NULL);
1121         if (ret)
1122                 return ret;
1123
1124         *req_state = (state == MSG_CLOCK_SW_STATE_AUTO);
1125         return 0;
1126 }
1127
1128 /**
1129  * ti_sci_cmd_clk_is_on() - Is the clock ON
1130  * @handle:     pointer to TI SCI handle
1131  * @dev_id:     Device identifier this request is for
1132  * @clk_id:     Clock identifier for the device for this request.
1133  *              Each device has it's own set of clock inputs. This indexes
1134  *              which clock input to modify.
1135  * @req_state: state indicating if the clock is managed by us and enabled
1136  * @curr_state: state indicating if the clock is ready for operation
1137  *
1138  * Return: 0 if all went well, else returns appropriate error value.
1139  */
1140 static int ti_sci_cmd_clk_is_on(const struct ti_sci_handle *handle, u32 dev_id,
1141                                 u8 clk_id, bool *req_state, bool *curr_state)
1142 {
1143         u8 c_state = 0, r_state = 0;
1144         int ret;
1145
1146         if (!req_state && !curr_state)
1147                 return -EINVAL;
1148
1149         ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1150                                          &r_state, &c_state);
1151         if (ret)
1152                 return ret;
1153
1154         if (req_state)
1155                 *req_state = (r_state == MSG_CLOCK_SW_STATE_REQ);
1156         if (curr_state)
1157                 *curr_state = (c_state == MSG_CLOCK_HW_STATE_READY);
1158         return 0;
1159 }
1160
1161 /**
1162  * ti_sci_cmd_clk_is_off() - Is the clock OFF
1163  * @handle:     pointer to TI SCI handle
1164  * @dev_id:     Device identifier this request is for
1165  * @clk_id:     Clock identifier for the device for this request.
1166  *              Each device has it's own set of clock inputs. This indexes
1167  *              which clock input to modify.
1168  * @req_state: state indicating if the clock is managed by us and disabled
1169  * @curr_state: state indicating if the clock is NOT ready for operation
1170  *
1171  * Return: 0 if all went well, else returns appropriate error value.
1172  */
1173 static int ti_sci_cmd_clk_is_off(const struct ti_sci_handle *handle, u32 dev_id,
1174                                  u8 clk_id, bool *req_state, bool *curr_state)
1175 {
1176         u8 c_state = 0, r_state = 0;
1177         int ret;
1178
1179         if (!req_state && !curr_state)
1180                 return -EINVAL;
1181
1182         ret = ti_sci_cmd_get_clock_state(handle, dev_id, clk_id,
1183                                          &r_state, &c_state);
1184         if (ret)
1185                 return ret;
1186
1187         if (req_state)
1188                 *req_state = (r_state == MSG_CLOCK_SW_STATE_UNREQ);
1189         if (curr_state)
1190                 *curr_state = (c_state == MSG_CLOCK_HW_STATE_NOT_READY);
1191         return 0;
1192 }
1193
1194 /**
1195  * ti_sci_cmd_clk_set_parent() - Set the clock source of a specific device clock
1196  * @handle:     pointer to TI SCI handle
1197  * @dev_id:     Device identifier this request is for
1198  * @clk_id:     Clock identifier for the device for this request.
1199  *              Each device has it's own set of clock inputs. This indexes
1200  *              which clock input to modify.
1201  * @parent_id:  Parent clock identifier to set
1202  *
1203  * Return: 0 if all went well, else returns appropriate error value.
1204  */
1205 static int ti_sci_cmd_clk_set_parent(const struct ti_sci_handle *handle,
1206                                      u32 dev_id, u8 clk_id, u8 parent_id)
1207 {
1208         struct ti_sci_info *info;
1209         struct ti_sci_msg_req_set_clock_parent *req;
1210         struct ti_sci_msg_hdr *resp;
1211         struct ti_sci_xfer *xfer;
1212         struct device *dev;
1213         int ret = 0;
1214
1215         if (IS_ERR(handle))
1216                 return PTR_ERR(handle);
1217         if (!handle)
1218                 return -EINVAL;
1219
1220         info = handle_to_ti_sci_info(handle);
1221         dev = info->dev;
1222
1223         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_PARENT,
1224                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1225                                    sizeof(*req), sizeof(*resp));
1226         if (IS_ERR(xfer)) {
1227                 ret = PTR_ERR(xfer);
1228                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1229                 return ret;
1230         }
1231         req = (struct ti_sci_msg_req_set_clock_parent *)xfer->xfer_buf;
1232         req->dev_id = dev_id;
1233         req->clk_id = clk_id;
1234         req->parent_id = parent_id;
1235
1236         ret = ti_sci_do_xfer(info, xfer);
1237         if (ret) {
1238                 dev_err(dev, "Mbox send fail %d\n", ret);
1239                 goto fail;
1240         }
1241
1242         resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1243
1244         ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1245
1246 fail:
1247         ti_sci_put_one_xfer(&info->minfo, xfer);
1248
1249         return ret;
1250 }
1251
1252 /**
1253  * ti_sci_cmd_clk_get_parent() - Get current parent clock source
1254  * @handle:     pointer to TI SCI handle
1255  * @dev_id:     Device identifier this request is for
1256  * @clk_id:     Clock identifier for the device for this request.
1257  *              Each device has it's own set of clock inputs. This indexes
1258  *              which clock input to modify.
1259  * @parent_id:  Current clock parent
1260  *
1261  * Return: 0 if all went well, else returns appropriate error value.
1262  */
1263 static int ti_sci_cmd_clk_get_parent(const struct ti_sci_handle *handle,
1264                                      u32 dev_id, u8 clk_id, u8 *parent_id)
1265 {
1266         struct ti_sci_info *info;
1267         struct ti_sci_msg_req_get_clock_parent *req;
1268         struct ti_sci_msg_resp_get_clock_parent *resp;
1269         struct ti_sci_xfer *xfer;
1270         struct device *dev;
1271         int ret = 0;
1272
1273         if (IS_ERR(handle))
1274                 return PTR_ERR(handle);
1275         if (!handle || !parent_id)
1276                 return -EINVAL;
1277
1278         info = handle_to_ti_sci_info(handle);
1279         dev = info->dev;
1280
1281         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_PARENT,
1282                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1283                                    sizeof(*req), sizeof(*resp));
1284         if (IS_ERR(xfer)) {
1285                 ret = PTR_ERR(xfer);
1286                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1287                 return ret;
1288         }
1289         req = (struct ti_sci_msg_req_get_clock_parent *)xfer->xfer_buf;
1290         req->dev_id = dev_id;
1291         req->clk_id = clk_id;
1292
1293         ret = ti_sci_do_xfer(info, xfer);
1294         if (ret) {
1295                 dev_err(dev, "Mbox send fail %d\n", ret);
1296                 goto fail;
1297         }
1298
1299         resp = (struct ti_sci_msg_resp_get_clock_parent *)xfer->xfer_buf;
1300
1301         if (!ti_sci_is_response_ack(resp))
1302                 ret = -ENODEV;
1303         else
1304                 *parent_id = resp->parent_id;
1305
1306 fail:
1307         ti_sci_put_one_xfer(&info->minfo, xfer);
1308
1309         return ret;
1310 }
1311
1312 /**
1313  * ti_sci_cmd_clk_get_num_parents() - Get num parents of the current clk source
1314  * @handle:     pointer to TI SCI handle
1315  * @dev_id:     Device identifier this request is for
1316  * @clk_id:     Clock identifier for the device for this request.
1317  *              Each device has it's own set of clock inputs. This indexes
1318  *              which clock input to modify.
1319  * @num_parents: Returns he number of parents to the current clock.
1320  *
1321  * Return: 0 if all went well, else returns appropriate error value.
1322  */
1323 static int ti_sci_cmd_clk_get_num_parents(const struct ti_sci_handle *handle,
1324                                           u32 dev_id, u8 clk_id,
1325                                           u8 *num_parents)
1326 {
1327         struct ti_sci_info *info;
1328         struct ti_sci_msg_req_get_clock_num_parents *req;
1329         struct ti_sci_msg_resp_get_clock_num_parents *resp;
1330         struct ti_sci_xfer *xfer;
1331         struct device *dev;
1332         int ret = 0;
1333
1334         if (IS_ERR(handle))
1335                 return PTR_ERR(handle);
1336         if (!handle || !num_parents)
1337                 return -EINVAL;
1338
1339         info = handle_to_ti_sci_info(handle);
1340         dev = info->dev;
1341
1342         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_NUM_CLOCK_PARENTS,
1343                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1344                                    sizeof(*req), sizeof(*resp));
1345         if (IS_ERR(xfer)) {
1346                 ret = PTR_ERR(xfer);
1347                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1348                 return ret;
1349         }
1350         req = (struct ti_sci_msg_req_get_clock_num_parents *)xfer->xfer_buf;
1351         req->dev_id = dev_id;
1352         req->clk_id = clk_id;
1353
1354         ret = ti_sci_do_xfer(info, xfer);
1355         if (ret) {
1356                 dev_err(dev, "Mbox send fail %d\n", ret);
1357                 goto fail;
1358         }
1359
1360         resp = (struct ti_sci_msg_resp_get_clock_num_parents *)xfer->xfer_buf;
1361
1362         if (!ti_sci_is_response_ack(resp))
1363                 ret = -ENODEV;
1364         else
1365                 *num_parents = resp->num_parents;
1366
1367 fail:
1368         ti_sci_put_one_xfer(&info->minfo, xfer);
1369
1370         return ret;
1371 }
1372
1373 /**
1374  * ti_sci_cmd_clk_get_match_freq() - Find a good match for frequency
1375  * @handle:     pointer to TI SCI handle
1376  * @dev_id:     Device identifier this request is for
1377  * @clk_id:     Clock identifier for the device for this request.
1378  *              Each device has it's own set of clock inputs. This indexes
1379  *              which clock input to modify.
1380  * @min_freq:   The minimum allowable frequency in Hz. This is the minimum
1381  *              allowable programmed frequency and does not account for clock
1382  *              tolerances and jitter.
1383  * @target_freq: The target clock frequency in Hz. A frequency will be
1384  *              processed as close to this target frequency as possible.
1385  * @max_freq:   The maximum allowable frequency in Hz. This is the maximum
1386  *              allowable programmed frequency and does not account for clock
1387  *              tolerances and jitter.
1388  * @match_freq: Frequency match in Hz response.
1389  *
1390  * Return: 0 if all went well, else returns appropriate error value.
1391  */
1392 static int ti_sci_cmd_clk_get_match_freq(const struct ti_sci_handle *handle,
1393                                          u32 dev_id, u8 clk_id, u64 min_freq,
1394                                          u64 target_freq, u64 max_freq,
1395                                          u64 *match_freq)
1396 {
1397         struct ti_sci_info *info;
1398         struct ti_sci_msg_req_query_clock_freq *req;
1399         struct ti_sci_msg_resp_query_clock_freq *resp;
1400         struct ti_sci_xfer *xfer;
1401         struct device *dev;
1402         int ret = 0;
1403
1404         if (IS_ERR(handle))
1405                 return PTR_ERR(handle);
1406         if (!handle || !match_freq)
1407                 return -EINVAL;
1408
1409         info = handle_to_ti_sci_info(handle);
1410         dev = info->dev;
1411
1412         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_QUERY_CLOCK_FREQ,
1413                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1414                                    sizeof(*req), sizeof(*resp));
1415         if (IS_ERR(xfer)) {
1416                 ret = PTR_ERR(xfer);
1417                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1418                 return ret;
1419         }
1420         req = (struct ti_sci_msg_req_query_clock_freq *)xfer->xfer_buf;
1421         req->dev_id = dev_id;
1422         req->clk_id = clk_id;
1423         req->min_freq_hz = min_freq;
1424         req->target_freq_hz = target_freq;
1425         req->max_freq_hz = max_freq;
1426
1427         ret = ti_sci_do_xfer(info, xfer);
1428         if (ret) {
1429                 dev_err(dev, "Mbox send fail %d\n", ret);
1430                 goto fail;
1431         }
1432
1433         resp = (struct ti_sci_msg_resp_query_clock_freq *)xfer->xfer_buf;
1434
1435         if (!ti_sci_is_response_ack(resp))
1436                 ret = -ENODEV;
1437         else
1438                 *match_freq = resp->freq_hz;
1439
1440 fail:
1441         ti_sci_put_one_xfer(&info->minfo, xfer);
1442
1443         return ret;
1444 }
1445
1446 /**
1447  * ti_sci_cmd_clk_set_freq() - Set a frequency for clock
1448  * @handle:     pointer to TI SCI handle
1449  * @dev_id:     Device identifier this request is for
1450  * @clk_id:     Clock identifier for the device for this request.
1451  *              Each device has it's own set of clock inputs. This indexes
1452  *              which clock input to modify.
1453  * @min_freq:   The minimum allowable frequency in Hz. This is the minimum
1454  *              allowable programmed frequency and does not account for clock
1455  *              tolerances and jitter.
1456  * @target_freq: The target clock frequency in Hz. A frequency will be
1457  *              processed as close to this target frequency as possible.
1458  * @max_freq:   The maximum allowable frequency in Hz. This is the maximum
1459  *              allowable programmed frequency and does not account for clock
1460  *              tolerances and jitter.
1461  *
1462  * Return: 0 if all went well, else returns appropriate error value.
1463  */
1464 static int ti_sci_cmd_clk_set_freq(const struct ti_sci_handle *handle,
1465                                    u32 dev_id, u8 clk_id, u64 min_freq,
1466                                    u64 target_freq, u64 max_freq)
1467 {
1468         struct ti_sci_info *info;
1469         struct ti_sci_msg_req_set_clock_freq *req;
1470         struct ti_sci_msg_hdr *resp;
1471         struct ti_sci_xfer *xfer;
1472         struct device *dev;
1473         int ret = 0;
1474
1475         if (IS_ERR(handle))
1476                 return PTR_ERR(handle);
1477         if (!handle)
1478                 return -EINVAL;
1479
1480         info = handle_to_ti_sci_info(handle);
1481         dev = info->dev;
1482
1483         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SET_CLOCK_FREQ,
1484                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1485                                    sizeof(*req), sizeof(*resp));
1486         if (IS_ERR(xfer)) {
1487                 ret = PTR_ERR(xfer);
1488                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1489                 return ret;
1490         }
1491         req = (struct ti_sci_msg_req_set_clock_freq *)xfer->xfer_buf;
1492         req->dev_id = dev_id;
1493         req->clk_id = clk_id;
1494         req->min_freq_hz = min_freq;
1495         req->target_freq_hz = target_freq;
1496         req->max_freq_hz = max_freq;
1497
1498         ret = ti_sci_do_xfer(info, xfer);
1499         if (ret) {
1500                 dev_err(dev, "Mbox send fail %d\n", ret);
1501                 goto fail;
1502         }
1503
1504         resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1505
1506         ret = ti_sci_is_response_ack(resp) ? 0 : -ENODEV;
1507
1508 fail:
1509         ti_sci_put_one_xfer(&info->minfo, xfer);
1510
1511         return ret;
1512 }
1513
1514 /**
1515  * ti_sci_cmd_clk_get_freq() - Get current frequency
1516  * @handle:     pointer to TI SCI handle
1517  * @dev_id:     Device identifier this request is for
1518  * @clk_id:     Clock identifier for the device for this request.
1519  *              Each device has it's own set of clock inputs. This indexes
1520  *              which clock input to modify.
1521  * @freq:       Currently frequency in Hz
1522  *
1523  * Return: 0 if all went well, else returns appropriate error value.
1524  */
1525 static int ti_sci_cmd_clk_get_freq(const struct ti_sci_handle *handle,
1526                                    u32 dev_id, u8 clk_id, u64 *freq)
1527 {
1528         struct ti_sci_info *info;
1529         struct ti_sci_msg_req_get_clock_freq *req;
1530         struct ti_sci_msg_resp_get_clock_freq *resp;
1531         struct ti_sci_xfer *xfer;
1532         struct device *dev;
1533         int ret = 0;
1534
1535         if (IS_ERR(handle))
1536                 return PTR_ERR(handle);
1537         if (!handle || !freq)
1538                 return -EINVAL;
1539
1540         info = handle_to_ti_sci_info(handle);
1541         dev = info->dev;
1542
1543         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_GET_CLOCK_FREQ,
1544                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1545                                    sizeof(*req), sizeof(*resp));
1546         if (IS_ERR(xfer)) {
1547                 ret = PTR_ERR(xfer);
1548                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1549                 return ret;
1550         }
1551         req = (struct ti_sci_msg_req_get_clock_freq *)xfer->xfer_buf;
1552         req->dev_id = dev_id;
1553         req->clk_id = clk_id;
1554
1555         ret = ti_sci_do_xfer(info, xfer);
1556         if (ret) {
1557                 dev_err(dev, "Mbox send fail %d\n", ret);
1558                 goto fail;
1559         }
1560
1561         resp = (struct ti_sci_msg_resp_get_clock_freq *)xfer->xfer_buf;
1562
1563         if (!ti_sci_is_response_ack(resp))
1564                 ret = -ENODEV;
1565         else
1566                 *freq = resp->freq_hz;
1567
1568 fail:
1569         ti_sci_put_one_xfer(&info->minfo, xfer);
1570
1571         return ret;
1572 }
1573
1574 static int ti_sci_cmd_core_reboot(const struct ti_sci_handle *handle)
1575 {
1576         struct ti_sci_info *info;
1577         struct ti_sci_msg_req_reboot *req;
1578         struct ti_sci_msg_hdr *resp;
1579         struct ti_sci_xfer *xfer;
1580         struct device *dev;
1581         int ret = 0;
1582
1583         if (IS_ERR(handle))
1584                 return PTR_ERR(handle);
1585         if (!handle)
1586                 return -EINVAL;
1587
1588         info = handle_to_ti_sci_info(handle);
1589         dev = info->dev;
1590
1591         xfer = ti_sci_get_one_xfer(info, TI_SCI_MSG_SYS_RESET,
1592                                    TI_SCI_FLAG_REQ_ACK_ON_PROCESSED,
1593                                    sizeof(*req), sizeof(*resp));
1594         if (IS_ERR(xfer)) {
1595                 ret = PTR_ERR(xfer);
1596                 dev_err(dev, "Message alloc failed(%d)\n", ret);
1597                 return ret;
1598         }
1599         req = (struct ti_sci_msg_req_reboot *)xfer->xfer_buf;
1600
1601         ret = ti_sci_do_xfer(info, xfer);
1602         if (ret) {
1603                 dev_err(dev, "Mbox send fail %d\n", ret);
1604                 goto fail;
1605         }
1606
1607         resp = (struct ti_sci_msg_hdr *)xfer->xfer_buf;
1608
1609         if (!ti_sci_is_response_ack(resp))
1610                 ret = -ENODEV;
1611         else
1612                 ret = 0;
1613
1614 fail:
1615         ti_sci_put_one_xfer(&info->minfo, xfer);
1616
1617         return ret;
1618 }
1619
1620 /*
1621  * ti_sci_setup_ops() - Setup the operations structures
1622  * @info:       pointer to TISCI pointer
1623  */
1624 static void ti_sci_setup_ops(struct ti_sci_info *info)
1625 {
1626         struct ti_sci_ops *ops = &info->handle.ops;
1627         struct ti_sci_core_ops *core_ops = &ops->core_ops;
1628         struct ti_sci_dev_ops *dops = &ops->dev_ops;
1629         struct ti_sci_clk_ops *cops = &ops->clk_ops;
1630
1631         core_ops->reboot_device = ti_sci_cmd_core_reboot;
1632
1633         dops->get_device = ti_sci_cmd_get_device;
1634         dops->idle_device = ti_sci_cmd_idle_device;
1635         dops->put_device = ti_sci_cmd_put_device;
1636
1637         dops->is_valid = ti_sci_cmd_dev_is_valid;
1638         dops->get_context_loss_count = ti_sci_cmd_dev_get_clcnt;
1639         dops->is_idle = ti_sci_cmd_dev_is_idle;
1640         dops->is_stop = ti_sci_cmd_dev_is_stop;
1641         dops->is_on = ti_sci_cmd_dev_is_on;
1642         dops->is_transitioning = ti_sci_cmd_dev_is_trans;
1643         dops->set_device_resets = ti_sci_cmd_set_device_resets;
1644         dops->get_device_resets = ti_sci_cmd_get_device_resets;
1645
1646         cops->get_clock = ti_sci_cmd_get_clock;
1647         cops->idle_clock = ti_sci_cmd_idle_clock;
1648         cops->put_clock = ti_sci_cmd_put_clock;
1649         cops->is_auto = ti_sci_cmd_clk_is_auto;
1650         cops->is_on = ti_sci_cmd_clk_is_on;
1651         cops->is_off = ti_sci_cmd_clk_is_off;
1652
1653         cops->set_parent = ti_sci_cmd_clk_set_parent;
1654         cops->get_parent = ti_sci_cmd_clk_get_parent;
1655         cops->get_num_parents = ti_sci_cmd_clk_get_num_parents;
1656
1657         cops->get_best_match_freq = ti_sci_cmd_clk_get_match_freq;
1658         cops->set_freq = ti_sci_cmd_clk_set_freq;
1659         cops->get_freq = ti_sci_cmd_clk_get_freq;
1660 }
1661
1662 /**
1663  * ti_sci_get_handle() - Get the TI SCI handle for a device
1664  * @dev:        Pointer to device for which we want SCI handle
1665  *
1666  * NOTE: The function does not track individual clients of the framework
1667  * and is expected to be maintained by caller of TI SCI protocol library.
1668  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1669  * Return: pointer to handle if successful, else:
1670  * -EPROBE_DEFER if the instance is not ready
1671  * -ENODEV if the required node handler is missing
1672  * -EINVAL if invalid conditions are encountered.
1673  */
1674 const struct ti_sci_handle *ti_sci_get_handle(struct device *dev)
1675 {
1676         struct device_node *ti_sci_np;
1677         struct list_head *p;
1678         struct ti_sci_handle *handle = NULL;
1679         struct ti_sci_info *info;
1680
1681         if (!dev) {
1682                 pr_err("I need a device pointer\n");
1683                 return ERR_PTR(-EINVAL);
1684         }
1685         ti_sci_np = of_get_parent(dev->of_node);
1686         if (!ti_sci_np) {
1687                 dev_err(dev, "No OF information\n");
1688                 return ERR_PTR(-EINVAL);
1689         }
1690
1691         mutex_lock(&ti_sci_list_mutex);
1692         list_for_each(p, &ti_sci_list) {
1693                 info = list_entry(p, struct ti_sci_info, node);
1694                 if (ti_sci_np == info->dev->of_node) {
1695                         handle = &info->handle;
1696                         info->users++;
1697                         break;
1698                 }
1699         }
1700         mutex_unlock(&ti_sci_list_mutex);
1701         of_node_put(ti_sci_np);
1702
1703         if (!handle)
1704                 return ERR_PTR(-EPROBE_DEFER);
1705
1706         return handle;
1707 }
1708 EXPORT_SYMBOL_GPL(ti_sci_get_handle);
1709
1710 /**
1711  * ti_sci_put_handle() - Release the handle acquired by ti_sci_get_handle
1712  * @handle:     Handle acquired by ti_sci_get_handle
1713  *
1714  * NOTE: The function does not track individual clients of the framework
1715  * and is expected to be maintained by caller of TI SCI protocol library.
1716  * ti_sci_put_handle must be balanced with successful ti_sci_get_handle
1717  *
1718  * Return: 0 is successfully released
1719  * if an error pointer was passed, it returns the error value back,
1720  * if null was passed, it returns -EINVAL;
1721  */
1722 int ti_sci_put_handle(const struct ti_sci_handle *handle)
1723 {
1724         struct ti_sci_info *info;
1725
1726         if (IS_ERR(handle))
1727                 return PTR_ERR(handle);
1728         if (!handle)
1729                 return -EINVAL;
1730
1731         info = handle_to_ti_sci_info(handle);
1732         mutex_lock(&ti_sci_list_mutex);
1733         if (!WARN_ON(!info->users))
1734                 info->users--;
1735         mutex_unlock(&ti_sci_list_mutex);
1736
1737         return 0;
1738 }
1739 EXPORT_SYMBOL_GPL(ti_sci_put_handle);
1740
1741 static void devm_ti_sci_release(struct device *dev, void *res)
1742 {
1743         const struct ti_sci_handle **ptr = res;
1744         const struct ti_sci_handle *handle = *ptr;
1745         int ret;
1746
1747         ret = ti_sci_put_handle(handle);
1748         if (ret)
1749                 dev_err(dev, "failed to put handle %d\n", ret);
1750 }
1751
1752 /**
1753  * devm_ti_sci_get_handle() - Managed get handle
1754  * @dev:        device for which we want SCI handle for.
1755  *
1756  * NOTE: This releases the handle once the device resources are
1757  * no longer needed. MUST NOT BE released with ti_sci_put_handle.
1758  * The function does not track individual clients of the framework
1759  * and is expected to be maintained by caller of TI SCI protocol library.
1760  *
1761  * Return: 0 if all went fine, else corresponding error.
1762  */
1763 const struct ti_sci_handle *devm_ti_sci_get_handle(struct device *dev)
1764 {
1765         const struct ti_sci_handle **ptr;
1766         const struct ti_sci_handle *handle;
1767
1768         ptr = devres_alloc(devm_ti_sci_release, sizeof(*ptr), GFP_KERNEL);
1769         if (!ptr)
1770                 return ERR_PTR(-ENOMEM);
1771         handle = ti_sci_get_handle(dev);
1772
1773         if (!IS_ERR(handle)) {
1774                 *ptr = handle;
1775                 devres_add(dev, ptr);
1776         } else {
1777                 devres_free(ptr);
1778         }
1779
1780         return handle;
1781 }
1782 EXPORT_SYMBOL_GPL(devm_ti_sci_get_handle);
1783
1784 static int tisci_reboot_handler(struct notifier_block *nb, unsigned long mode,
1785                                 void *cmd)
1786 {
1787         struct ti_sci_info *info = reboot_to_ti_sci_info(nb);
1788         const struct ti_sci_handle *handle = &info->handle;
1789
1790         ti_sci_cmd_core_reboot(handle);
1791
1792         /* call fail OR pass, we should not be here in the first place */
1793         return NOTIFY_BAD;
1794 }
1795
1796 /* Description for K2G */
1797 static const struct ti_sci_desc ti_sci_pmmc_k2g_desc = {
1798         .default_host_id = 2,
1799         /* Conservative duration */
1800         .max_rx_timeout_ms = 1000,
1801         /* Limited by MBOX_TX_QUEUE_LEN. K2G can handle upto 128 messages! */
1802         .max_msgs = 20,
1803         .max_msg_size = 64,
1804 };
1805
1806 static const struct of_device_id ti_sci_of_match[] = {
1807         {.compatible = "ti,k2g-sci", .data = &ti_sci_pmmc_k2g_desc},
1808         { /* Sentinel */ },
1809 };
1810 MODULE_DEVICE_TABLE(of, ti_sci_of_match);
1811
1812 static int ti_sci_probe(struct platform_device *pdev)
1813 {
1814         struct device *dev = &pdev->dev;
1815         const struct of_device_id *of_id;
1816         const struct ti_sci_desc *desc;
1817         struct ti_sci_xfer *xfer;
1818         struct ti_sci_info *info = NULL;
1819         struct ti_sci_xfers_info *minfo;
1820         struct mbox_client *cl;
1821         int ret = -EINVAL;
1822         int i;
1823         int reboot = 0;
1824         u32 h_id;
1825
1826         of_id = of_match_device(ti_sci_of_match, dev);
1827         if (!of_id) {
1828                 dev_err(dev, "OF data missing\n");
1829                 return -EINVAL;
1830         }
1831         desc = of_id->data;
1832
1833         info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
1834         if (!info)
1835                 return -ENOMEM;
1836
1837         info->dev = dev;
1838         info->desc = desc;
1839         ret = of_property_read_u32(dev->of_node, "ti,host-id", &h_id);
1840         /* if the property is not present in DT, use a default from desc */
1841         if (ret < 0) {
1842                 info->host_id = info->desc->default_host_id;
1843         } else {
1844                 if (!h_id) {
1845                         dev_warn(dev, "Host ID 0 is reserved for firmware\n");
1846                         info->host_id = info->desc->default_host_id;
1847                 } else {
1848                         info->host_id = h_id;
1849                 }
1850         }
1851
1852         reboot = of_property_read_bool(dev->of_node,
1853                                        "ti,system-reboot-controller");
1854         INIT_LIST_HEAD(&info->node);
1855         minfo = &info->minfo;
1856
1857         /*
1858          * Pre-allocate messages
1859          * NEVER allocate more than what we can indicate in hdr.seq
1860          * if we have data description bug, force a fix..
1861          */
1862         if (WARN_ON(desc->max_msgs >=
1863                     1 << 8 * sizeof(((struct ti_sci_msg_hdr *)0)->seq)))
1864                 return -EINVAL;
1865
1866         minfo->xfer_block = devm_kcalloc(dev,
1867                                          desc->max_msgs,
1868                                          sizeof(*minfo->xfer_block),
1869                                          GFP_KERNEL);
1870         if (!minfo->xfer_block)
1871                 return -ENOMEM;
1872
1873         minfo->xfer_alloc_table = devm_kcalloc(dev,
1874                                                BITS_TO_LONGS(desc->max_msgs),
1875                                                sizeof(unsigned long),
1876                                                GFP_KERNEL);
1877         if (!minfo->xfer_alloc_table)
1878                 return -ENOMEM;
1879         bitmap_zero(minfo->xfer_alloc_table, desc->max_msgs);
1880
1881         /* Pre-initialize the buffer pointer to pre-allocated buffers */
1882         for (i = 0, xfer = minfo->xfer_block; i < desc->max_msgs; i++, xfer++) {
1883                 xfer->xfer_buf = devm_kcalloc(dev, 1, desc->max_msg_size,
1884                                               GFP_KERNEL);
1885                 if (!xfer->xfer_buf)
1886                         return -ENOMEM;
1887
1888                 xfer->tx_message.buf = xfer->xfer_buf;
1889                 init_completion(&xfer->done);
1890         }
1891
1892         ret = ti_sci_debugfs_create(pdev, info);
1893         if (ret)
1894                 dev_warn(dev, "Failed to create debug file\n");
1895
1896         platform_set_drvdata(pdev, info);
1897
1898         cl = &info->cl;
1899         cl->dev = dev;
1900         cl->tx_block = false;
1901         cl->rx_callback = ti_sci_rx_callback;
1902         cl->knows_txdone = true;
1903
1904         spin_lock_init(&minfo->xfer_lock);
1905         sema_init(&minfo->sem_xfer_count, desc->max_msgs);
1906
1907         info->chan_rx = mbox_request_channel_byname(cl, "rx");
1908         if (IS_ERR(info->chan_rx)) {
1909                 ret = PTR_ERR(info->chan_rx);
1910                 goto out;
1911         }
1912
1913         info->chan_tx = mbox_request_channel_byname(cl, "tx");
1914         if (IS_ERR(info->chan_tx)) {
1915                 ret = PTR_ERR(info->chan_tx);
1916                 goto out;
1917         }
1918         ret = ti_sci_cmd_get_revision(info);
1919         if (ret) {
1920                 dev_err(dev, "Unable to communicate with TISCI(%d)\n", ret);
1921                 goto out;
1922         }
1923
1924         ti_sci_setup_ops(info);
1925
1926         if (reboot) {
1927                 info->nb.notifier_call = tisci_reboot_handler;
1928                 info->nb.priority = 128;
1929
1930                 ret = register_restart_handler(&info->nb);
1931                 if (ret) {
1932                         dev_err(dev, "reboot registration fail(%d)\n", ret);
1933                         return ret;
1934                 }
1935         }
1936
1937         dev_info(dev, "ABI: %d.%d (firmware rev 0x%04x '%s')\n",
1938                  info->handle.version.abi_major, info->handle.version.abi_minor,
1939                  info->handle.version.firmware_revision,
1940                  info->handle.version.firmware_description);
1941
1942         mutex_lock(&ti_sci_list_mutex);
1943         list_add_tail(&info->node, &ti_sci_list);
1944         mutex_unlock(&ti_sci_list_mutex);
1945
1946         return of_platform_populate(dev->of_node, NULL, NULL, dev);
1947 out:
1948         if (!IS_ERR(info->chan_tx))
1949                 mbox_free_channel(info->chan_tx);
1950         if (!IS_ERR(info->chan_rx))
1951                 mbox_free_channel(info->chan_rx);
1952         debugfs_remove(info->d);
1953         return ret;
1954 }
1955
1956 static int ti_sci_remove(struct platform_device *pdev)
1957 {
1958         struct ti_sci_info *info;
1959         struct device *dev = &pdev->dev;
1960         int ret = 0;
1961
1962         of_platform_depopulate(dev);
1963
1964         info = platform_get_drvdata(pdev);
1965
1966         if (info->nb.notifier_call)
1967                 unregister_restart_handler(&info->nb);
1968
1969         mutex_lock(&ti_sci_list_mutex);
1970         if (info->users)
1971                 ret = -EBUSY;
1972         else
1973                 list_del(&info->node);
1974         mutex_unlock(&ti_sci_list_mutex);
1975
1976         if (!ret) {
1977                 ti_sci_debugfs_destroy(pdev, info);
1978
1979                 /* Safe to free channels since no more users */
1980                 mbox_free_channel(info->chan_tx);
1981                 mbox_free_channel(info->chan_rx);
1982         }
1983
1984         return ret;
1985 }
1986
1987 static struct platform_driver ti_sci_driver = {
1988         .probe = ti_sci_probe,
1989         .remove = ti_sci_remove,
1990         .driver = {
1991                    .name = "ti-sci",
1992                    .of_match_table = of_match_ptr(ti_sci_of_match),
1993         },
1994 };
1995 module_platform_driver(ti_sci_driver);
1996
1997 MODULE_LICENSE("GPL v2");
1998 MODULE_DESCRIPTION("TI System Control Interface(SCI) driver");
1999 MODULE_AUTHOR("Nishanth Menon");
2000 MODULE_ALIAS("platform:ti-sci");