imx-scu-irq.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2019 NXP
  4. *
  5. * Implementation of the SCU IRQ functions using MU.
  6. *
  7. */
  8. #include <dt-bindings/firmware/imx/rsrc.h>
  9. #include <linux/firmware/imx/ipc.h>
  10. #include <linux/firmware/imx/sci.h>
  11. #include <linux/mailbox_client.h>
  12. #include <linux/suspend.h>
  13. #define IMX_SC_IRQ_FUNC_ENABLE 1
  14. #define IMX_SC_IRQ_FUNC_STATUS 2
  15. #define IMX_SC_IRQ_NUM_GROUP 4
  16. static u32 mu_resource_id;
  17. struct imx_sc_msg_irq_get_status {
  18. struct imx_sc_rpc_msg hdr;
  19. union {
  20. struct {
  21. u16 resource;
  22. u8 group;
  23. u8 reserved;
  24. } __packed req;
  25. struct {
  26. u32 status;
  27. } resp;
  28. } data;
  29. };
  30. struct imx_sc_msg_irq_enable {
  31. struct imx_sc_rpc_msg hdr;
  32. u32 mask;
  33. u16 resource;
  34. u8 group;
  35. u8 enable;
  36. } __packed;
  37. static struct imx_sc_ipc *imx_sc_irq_ipc_handle;
  38. static struct work_struct imx_sc_irq_work;
  39. static ATOMIC_NOTIFIER_HEAD(imx_scu_irq_notifier_chain);
  40. int imx_scu_irq_register_notifier(struct notifier_block *nb)
  41. {
  42. return atomic_notifier_chain_register(
  43. &imx_scu_irq_notifier_chain, nb);
  44. }
  45. EXPORT_SYMBOL(imx_scu_irq_register_notifier);
  46. int imx_scu_irq_unregister_notifier(struct notifier_block *nb)
  47. {
  48. return atomic_notifier_chain_unregister(
  49. &imx_scu_irq_notifier_chain, nb);
  50. }
  51. EXPORT_SYMBOL(imx_scu_irq_unregister_notifier);
  52. static int imx_scu_irq_notifier_call_chain(unsigned long status, u8 *group)
  53. {
  54. return atomic_notifier_call_chain(&imx_scu_irq_notifier_chain,
  55. status, (void *)group);
  56. }
  57. static void imx_scu_irq_work_handler(struct work_struct *work)
  58. {
  59. struct imx_sc_msg_irq_get_status msg;
  60. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  61. u32 irq_status;
  62. int ret;
  63. u8 i;
  64. for (i = 0; i < IMX_SC_IRQ_NUM_GROUP; i++) {
  65. hdr->ver = IMX_SC_RPC_VERSION;
  66. hdr->svc = IMX_SC_RPC_SVC_IRQ;
  67. hdr->func = IMX_SC_IRQ_FUNC_STATUS;
  68. hdr->size = 2;
  69. msg.data.req.resource = mu_resource_id;
  70. msg.data.req.group = i;
  71. ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
  72. if (ret) {
  73. pr_err("get irq group %d status failed, ret %d\n",
  74. i, ret);
  75. return;
  76. }
  77. irq_status = msg.data.resp.status;
  78. if (!irq_status)
  79. continue;
  80. pm_system_wakeup();
  81. imx_scu_irq_notifier_call_chain(irq_status, &i);
  82. }
  83. }
  84. int imx_scu_irq_group_enable(u8 group, u32 mask, u8 enable)
  85. {
  86. struct imx_sc_msg_irq_enable msg;
  87. struct imx_sc_rpc_msg *hdr = &msg.hdr;
  88. int ret;
  89. if (!imx_sc_irq_ipc_handle)
  90. return -EPROBE_DEFER;
  91. hdr->ver = IMX_SC_RPC_VERSION;
  92. hdr->svc = IMX_SC_RPC_SVC_IRQ;
  93. hdr->func = IMX_SC_IRQ_FUNC_ENABLE;
  94. hdr->size = 3;
  95. msg.resource = mu_resource_id;
  96. msg.group = group;
  97. msg.mask = mask;
  98. msg.enable = enable;
  99. ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
  100. if (ret)
  101. pr_err("enable irq failed, group %d, mask %d, ret %d\n",
  102. group, mask, ret);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL(imx_scu_irq_group_enable);
  106. static void imx_scu_irq_callback(struct mbox_client *c, void *msg)
  107. {
  108. schedule_work(&imx_sc_irq_work);
  109. }
  110. int imx_scu_enable_general_irq_channel(struct device *dev)
  111. {
  112. struct of_phandle_args spec;
  113. struct mbox_client *cl;
  114. struct mbox_chan *ch;
  115. int ret = 0, i = 0;
  116. ret = imx_scu_get_handle(&imx_sc_irq_ipc_handle);
  117. if (ret)
  118. return ret;
  119. cl = devm_kzalloc(dev, sizeof(*cl), GFP_KERNEL);
  120. if (!cl)
  121. return -ENOMEM;
  122. cl->dev = dev;
  123. cl->rx_callback = imx_scu_irq_callback;
  124. /* SCU general IRQ uses general interrupt channel 3 */
  125. ch = mbox_request_channel_byname(cl, "gip3");
  126. if (IS_ERR(ch)) {
  127. ret = PTR_ERR(ch);
  128. dev_err(dev, "failed to request mbox chan gip3, ret %d\n", ret);
  129. devm_kfree(dev, cl);
  130. return ret;
  131. }
  132. INIT_WORK(&imx_sc_irq_work, imx_scu_irq_work_handler);
  133. if (!of_parse_phandle_with_args(dev->of_node, "mboxes",
  134. "#mbox-cells", 0, &spec))
  135. i = of_alias_get_id(spec.np, "mu");
  136. /* use mu1 as general mu irq channel if failed */
  137. if (i < 0)
  138. i = 1;
  139. mu_resource_id = IMX_SC_R_MU_0A + i;
  140. return ret;
  141. }
  142. EXPORT_SYMBOL(imx_scu_enable_general_irq_channel);