mailbox-uclass.c 3.0 KB

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