agent-mailbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #include <rthw.h>
  11. #include <drivers/mailbox.h>
  12. #include <dt-bindings/size.h>
  13. #define DBG_TAG "scmi.agent.mailbox"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #include "agent.h"
  17. struct scmi_agent_mailbox
  18. {
  19. struct rt_mbox_client mbox_client;
  20. struct rt_mbox_chan *chan;
  21. struct scmi_shared_mem *shmem;
  22. struct rt_spinlock lock;
  23. };
  24. #define raw_to_scmi_mailbox(raw) rt_container_of(raw, struct scmi_agent_mailbox, mbox_client)
  25. static void scmi_mailbox_rx_callback(struct rt_mbox_client *client, void *data)
  26. {
  27. struct rt_scmi_msg *msg = data;
  28. if (msg->rx_callback)
  29. {
  30. msg->rx_callback(msg->sdev, msg->out_msg, msg->out_msg_size);
  31. }
  32. }
  33. static void scmi_mailbox_tx_prepare(struct rt_mbox_client *client, const void *data)
  34. {
  35. struct rt_scmi_msg *msg = (void *)data;
  36. struct scmi_agent_mailbox *ambox = raw_to_scmi_mailbox(client);
  37. scmi_shmem_msg_write(ambox->shmem, msg);
  38. }
  39. static void scmi_mailbox_tx_done(struct rt_mbox_client *client, const void *data,
  40. rt_err_t err)
  41. {
  42. struct scmi_agent_mailbox *ambox = raw_to_scmi_mailbox(client);
  43. if (!err)
  44. {
  45. scmi_shmem_clear_channel(ambox->shmem);
  46. }
  47. }
  48. static rt_err_t scmi_agent_mailbox_setup(struct scmi_agent *agent,
  49. struct rt_device *dev)
  50. {
  51. rt_err_t err;
  52. rt_uint64_t shm_addr, shm_size;
  53. int mbox_chan, mbox_count, shmem_count;
  54. struct rt_ofw_node *np = dev->ofw_node, *shmem_np;
  55. struct scmi_agent_mailbox *ambox = rt_calloc(1, sizeof(*ambox));
  56. if (!ambox)
  57. {
  58. return -RT_ENOMEM;
  59. }
  60. mbox_count = rt_ofw_count_phandle_cells(np, "mboxes", "#mbox-cells");
  61. shmem_count = rt_ofw_count_phandle_cells(np, "shmem", RT_NULL);
  62. if (mbox_count < 0)
  63. {
  64. err = mbox_count;
  65. goto _fail;
  66. }
  67. if (shmem_count < 0)
  68. {
  69. err = shmem_count;
  70. goto _fail;
  71. }
  72. mbox_chan = 0;
  73. if (mbox_count == 2 && shmem_count == 2)
  74. {
  75. mbox_chan = 1;
  76. }
  77. else if (mbox_count == 3)
  78. {
  79. mbox_chan = 2;
  80. }
  81. ambox->mbox_client.dev = dev;
  82. ambox->mbox_client.rx_callback = scmi_mailbox_rx_callback;
  83. ambox->mbox_client.tx_prepare = scmi_mailbox_tx_prepare;
  84. ambox->mbox_client.tx_done = scmi_mailbox_tx_done;
  85. ambox->chan = rt_mbox_request_by_index(&ambox->mbox_client, mbox_chan);
  86. if (rt_is_err_or_null(ambox->chan))
  87. {
  88. err = -RT_EIO;
  89. goto _fail;
  90. }
  91. shmem_np = rt_ofw_parse_phandle(np, "shmem", 0);
  92. if (!rt_ofw_node_is_compatible(shmem_np, "arm,scmi-shmem"))
  93. {
  94. err = -RT_EINVAL;
  95. rt_ofw_node_put(shmem_np);
  96. goto _fail;
  97. }
  98. if ((err = rt_ofw_get_address(shmem_np, 0, &shm_addr, &shm_size)))
  99. {
  100. rt_ofw_node_put(shmem_np);
  101. goto _fail;
  102. }
  103. rt_ofw_node_put(shmem_np);
  104. ambox->shmem = rt_ioremap((void *)shm_addr, shm_size);
  105. if (!ambox->shmem)
  106. {
  107. err = -RT_EIO;
  108. goto _fail;
  109. }
  110. agent->priv = ambox;
  111. return RT_EOK;
  112. _fail:
  113. if (!rt_is_err_or_null(ambox->chan))
  114. {
  115. rt_mbox_release(ambox->chan);
  116. }
  117. if (ambox->shmem)
  118. {
  119. rt_iounmap(ambox->shmem);
  120. }
  121. rt_free(ambox);
  122. return err;
  123. }
  124. static rt_err_t scmi_agent_mailbox_process_msg(struct scmi_agent *agent,
  125. struct rt_scmi_msg *msg)
  126. {
  127. rt_err_t err;
  128. struct scmi_agent_mailbox *ambox = agent->priv;
  129. rt_hw_spin_lock(&ambox->lock.lock);
  130. err = rt_mbox_send(ambox->chan, (const void *)msg, 30);
  131. rt_hw_spin_unlock(&ambox->lock.lock);
  132. return err;
  133. }
  134. struct scmi_agent_ops scmi_agent_mailbox_ops =
  135. {
  136. .name = "mailbox",
  137. .setup = scmi_agent_mailbox_setup,
  138. .process_msg = scmi_agent_mailbox_process_msg,
  139. };