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