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