mailbox_agent.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2020 Linaro Limited.
  4. */
  5. #define LOG_CATEGORY UCLASS_SCMI_AGENT
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <errno.h>
  9. #include <mailbox.h>
  10. #include <scmi_agent.h>
  11. #include <scmi_agent-uclass.h>
  12. #include <dm/device_compat.h>
  13. #include <dm/devres.h>
  14. #include <linux/compat.h>
  15. #include "smt.h"
  16. #define TIMEOUT_US_10MS 10000
  17. /**
  18. * struct scmi_mbox_channel - Description of an SCMI mailbox transport
  19. * @smt: Shared memory buffer
  20. * @mbox: Mailbox channel description
  21. * @timeout_us: Timeout in microseconds for the mailbox transfer
  22. */
  23. struct scmi_mbox_channel {
  24. struct scmi_smt smt;
  25. struct mbox_chan mbox;
  26. ulong timeout_us;
  27. };
  28. static int scmi_mbox_process_msg(struct udevice *dev, struct scmi_msg *msg)
  29. {
  30. struct scmi_mbox_channel *chan = dev_get_priv(dev);
  31. int ret;
  32. ret = scmi_write_msg_to_smt(dev, &chan->smt, msg);
  33. if (ret)
  34. return ret;
  35. /* Give shm addr to mbox in case it is meaningful */
  36. ret = mbox_send(&chan->mbox, chan->smt.buf);
  37. if (ret) {
  38. dev_err(dev, "Message send failed: %d\n", ret);
  39. goto out;
  40. }
  41. /* Receive the response */
  42. ret = mbox_recv(&chan->mbox, chan->smt.buf, chan->timeout_us);
  43. if (ret) {
  44. dev_err(dev, "Response failed: %d, abort\n", ret);
  45. goto out;
  46. }
  47. ret = scmi_read_resp_from_smt(dev, &chan->smt, msg);
  48. out:
  49. scmi_clear_smt_channel(&chan->smt);
  50. return ret;
  51. }
  52. int scmi_mbox_probe(struct udevice *dev)
  53. {
  54. struct scmi_mbox_channel *chan = dev_get_priv(dev);
  55. int ret;
  56. chan->timeout_us = TIMEOUT_US_10MS;
  57. ret = mbox_get_by_index(dev, 0, &chan->mbox);
  58. if (ret) {
  59. dev_err(dev, "Failed to find mailbox: %d\n", ret);
  60. goto out;
  61. }
  62. ret = scmi_dt_get_smt_buffer(dev, &chan->smt);
  63. if (ret)
  64. dev_err(dev, "Failed to get shm resources: %d\n", ret);
  65. out:
  66. if (ret)
  67. devm_kfree(dev, chan);
  68. return ret;
  69. }
  70. static const struct udevice_id scmi_mbox_ids[] = {
  71. { .compatible = "arm,scmi" },
  72. { }
  73. };
  74. static const struct scmi_agent_ops scmi_mbox_ops = {
  75. .process_msg = scmi_mbox_process_msg,
  76. };
  77. U_BOOT_DRIVER(scmi_mbox) = {
  78. .name = "scmi-over-mailbox",
  79. .id = UCLASS_SCMI_AGENT,
  80. .of_match = scmi_mbox_ids,
  81. .priv_auto = sizeof(struct scmi_mbox_channel),
  82. .probe = scmi_mbox_probe,
  83. .ops = &scmi_mbox_ops,
  84. };