mailbox-uclass.c 3.2 KB

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