mailbox-uclass.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2016, NVIDIA CORPORATION.
  4. */
  5. #include <common.h>
  6. #include <dm.h>
  7. #include <mailbox.h>
  8. #include <mailbox-uclass.h>
  9. #include <time.h>
  10. static inline struct mbox_ops *mbox_dev_ops(struct udevice *dev)
  11. {
  12. return (struct mbox_ops *)dev->driver->ops;
  13. }
  14. static int mbox_of_xlate_default(struct mbox_chan *chan,
  15. struct ofnode_phandle_args *args)
  16. {
  17. debug("%s(chan=%p)\n", __func__, chan);
  18. if (args->args_count != 1) {
  19. debug("Invaild args_count: %d\n", args->args_count);
  20. return -EINVAL;
  21. }
  22. chan->id = args->args[0];
  23. return 0;
  24. }
  25. int mbox_get_by_index(struct udevice *dev, int index, struct mbox_chan *chan)
  26. {
  27. struct ofnode_phandle_args args;
  28. int ret;
  29. struct udevice *dev_mbox;
  30. struct mbox_ops *ops;
  31. debug("%s(dev=%p, index=%d, chan=%p)\n", __func__, dev, index, chan);
  32. ret = dev_read_phandle_with_args(dev, "mboxes", "#mbox-cells", 0, index,
  33. &args);
  34. if (ret) {
  35. debug("%s: dev_read_phandle_with_args failed: %d\n", __func__,
  36. ret);
  37. return ret;
  38. }
  39. ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX, args.node, &dev_mbox);
  40. if (ret) {
  41. debug("%s: uclass_get_device_by_of_offset failed: %d\n",
  42. __func__, ret);
  43. /* Test with parent node */
  44. ret = uclass_get_device_by_ofnode(UCLASS_MAILBOX,
  45. ofnode_get_parent(args.node),
  46. &dev_mbox);
  47. if (ret) {
  48. debug("%s: mbox node from parent failed: %d\n",
  49. __func__, ret);
  50. return ret;
  51. };
  52. }
  53. ops = mbox_dev_ops(dev_mbox);
  54. chan->dev = dev_mbox;
  55. if (ops->of_xlate)
  56. ret = ops->of_xlate(chan, &args);
  57. else
  58. ret = mbox_of_xlate_default(chan, &args);
  59. if (ret) {
  60. debug("of_xlate() failed: %d\n", ret);
  61. return ret;
  62. }
  63. if (ops->request)
  64. ret = ops->request(chan);
  65. if (ret) {
  66. debug("ops->request() failed: %d\n", ret);
  67. return ret;
  68. }
  69. return 0;
  70. }
  71. int mbox_get_by_name(struct udevice *dev, const char *name,
  72. struct mbox_chan *chan)
  73. {
  74. int index;
  75. debug("%s(dev=%p, name=%s, chan=%p)\n", __func__, dev, name, chan);
  76. index = dev_read_stringlist_search(dev, "mbox-names", name);
  77. if (index < 0) {
  78. debug("fdt_stringlist_search() failed: %d\n", index);
  79. return index;
  80. }
  81. return mbox_get_by_index(dev, index, chan);
  82. }
  83. int mbox_free(struct mbox_chan *chan)
  84. {
  85. struct mbox_ops *ops = mbox_dev_ops(chan->dev);
  86. debug("%s(chan=%p)\n", __func__, chan);
  87. if (ops->free)
  88. return ops->free(chan);
  89. return 0;
  90. }
  91. int mbox_send(struct mbox_chan *chan, const void *data)
  92. {
  93. struct mbox_ops *ops = mbox_dev_ops(chan->dev);
  94. debug("%s(chan=%p, data=%p)\n", __func__, chan, data);
  95. return ops->send(chan, data);
  96. }
  97. int mbox_recv(struct mbox_chan *chan, void *data, ulong timeout_us)
  98. {
  99. struct mbox_ops *ops = mbox_dev_ops(chan->dev);
  100. ulong start_time;
  101. int ret;
  102. debug("%s(chan=%p, data=%p, timeout_us=%ld)\n", __func__, chan, data,
  103. timeout_us);
  104. start_time = timer_get_us();
  105. /*
  106. * Account for partial us ticks, but if timeout_us is 0, ensure we
  107. * still don't wait at all.
  108. */
  109. if (timeout_us)
  110. timeout_us++;
  111. for (;;) {
  112. ret = ops->recv(chan, data);
  113. if (ret != -ENODATA)
  114. return ret;
  115. if ((timer_get_us() - start_time) >= timeout_us)
  116. return -ETIMEDOUT;
  117. }
  118. }
  119. UCLASS_DRIVER(mailbox) = {
  120. .id = UCLASS_MAILBOX,
  121. .name = "mailbox",
  122. };