mailbox-uclass.c 3.2 KB

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