mailbox-mpfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Microchip PolarFire SoC (MPFS) system controller/mailbox controller driver
  4. *
  5. * Copyright (c) 2020-2022 Microchip Corporation. All rights reserved.
  6. *
  7. * Author: Conor Dooley <conor.dooley@microchip.com>
  8. *
  9. */
  10. #include <linux/io.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mod_devicetable.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mailbox_controller.h>
  19. #include <soc/microchip/mpfs.h>
  20. #define SERVICES_CR_OFFSET 0x50u
  21. #define SERVICES_SR_OFFSET 0x54u
  22. #define MAILBOX_REG_OFFSET 0x800u
  23. #define MSS_SYS_MAILBOX_DATA_OFFSET 0u
  24. #define SCB_MASK_WIDTH 16u
  25. /* SCBCTRL service control register */
  26. #define SCB_CTRL_REQ (0)
  27. #define SCB_CTRL_REQ_MASK BIT(SCB_CTRL_REQ)
  28. #define SCB_CTRL_BUSY (1)
  29. #define SCB_CTRL_BUSY_MASK BIT(SCB_CTRL_BUSY)
  30. #define SCB_CTRL_ABORT (2)
  31. #define SCB_CTRL_ABORT_MASK BIT(SCB_CTRL_ABORT)
  32. #define SCB_CTRL_NOTIFY (3)
  33. #define SCB_CTRL_NOTIFY_MASK BIT(SCB_CTRL_NOTIFY)
  34. #define SCB_CTRL_POS (16)
  35. #define SCB_CTRL_MASK GENMASK(SCB_CTRL_POS + SCB_MASK_WIDTH - 1, SCB_CTRL_POS)
  36. /* SCBCTRL service status register */
  37. #define SCB_STATUS_REQ (0)
  38. #define SCB_STATUS_REQ_MASK BIT(SCB_STATUS_REQ)
  39. #define SCB_STATUS_BUSY (1)
  40. #define SCB_STATUS_BUSY_MASK BIT(SCB_STATUS_BUSY)
  41. #define SCB_STATUS_ABORT (2)
  42. #define SCB_STATUS_ABORT_MASK BIT(SCB_STATUS_ABORT)
  43. #define SCB_STATUS_NOTIFY (3)
  44. #define SCB_STATUS_NOTIFY_MASK BIT(SCB_STATUS_NOTIFY)
  45. #define SCB_STATUS_POS (16)
  46. #define SCB_STATUS_MASK GENMASK(SCB_STATUS_POS + SCB_MASK_WIDTH - 1, SCB_STATUS_POS)
  47. struct mpfs_mbox {
  48. struct mbox_controller controller;
  49. struct device *dev;
  50. int irq;
  51. void __iomem *ctrl_base;
  52. void __iomem *mbox_base;
  53. void __iomem *int_reg;
  54. struct mbox_chan chans[1];
  55. struct mpfs_mss_response *response;
  56. u16 resp_offset;
  57. };
  58. static bool mpfs_mbox_busy(struct mpfs_mbox *mbox)
  59. {
  60. u32 status;
  61. status = readl_relaxed(mbox->ctrl_base + SERVICES_SR_OFFSET);
  62. return status & SCB_STATUS_BUSY_MASK;
  63. }
  64. static bool mpfs_mbox_last_tx_done(struct mbox_chan *chan)
  65. {
  66. struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
  67. struct mpfs_mss_response *response = mbox->response;
  68. u32 val;
  69. if (mpfs_mbox_busy(mbox))
  70. return false;
  71. /*
  72. * The service status is stored in bits 31:16 of the SERVICES_SR
  73. * register & is only valid when the system controller is not busy.
  74. * Failed services are intended to generated interrupts, but in reality
  75. * this does not happen, so the status must be checked here.
  76. */
  77. val = readl_relaxed(mbox->ctrl_base + SERVICES_SR_OFFSET);
  78. response->resp_status = (val & SCB_STATUS_MASK) >> SCB_STATUS_POS;
  79. return true;
  80. }
  81. static int mpfs_mbox_send_data(struct mbox_chan *chan, void *data)
  82. {
  83. struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
  84. struct mpfs_mss_msg *msg = data;
  85. u32 tx_trigger;
  86. u16 opt_sel;
  87. u32 val = 0u;
  88. mbox->response = msg->response;
  89. mbox->resp_offset = msg->resp_offset;
  90. if (mpfs_mbox_busy(mbox))
  91. return -EBUSY;
  92. if (msg->cmd_data_size) {
  93. u32 index;
  94. u8 extra_bits = msg->cmd_data_size & 3;
  95. u32 *word_buf = (u32 *)msg->cmd_data;
  96. for (index = 0; index < (msg->cmd_data_size / 4); index++)
  97. writel_relaxed(word_buf[index],
  98. mbox->mbox_base + msg->mbox_offset + index * 0x4);
  99. if (extra_bits) {
  100. u8 i;
  101. u8 byte_off = ALIGN_DOWN(msg->cmd_data_size, 4);
  102. u8 *byte_buf = msg->cmd_data + byte_off;
  103. val = readl_relaxed(mbox->mbox_base + msg->mbox_offset + index * 0x4);
  104. for (i = 0u; i < extra_bits; i++) {
  105. val &= ~(0xffu << (i * 8u));
  106. val |= (byte_buf[i] << (i * 8u));
  107. }
  108. writel_relaxed(val, mbox->mbox_base + msg->mbox_offset + index * 0x4);
  109. }
  110. }
  111. opt_sel = ((msg->mbox_offset << 7u) | (msg->cmd_opcode & 0x7fu));
  112. tx_trigger = (opt_sel << SCB_CTRL_POS) & SCB_CTRL_MASK;
  113. tx_trigger |= SCB_CTRL_REQ_MASK | SCB_STATUS_NOTIFY_MASK;
  114. writel_relaxed(tx_trigger, mbox->ctrl_base + SERVICES_CR_OFFSET);
  115. return 0;
  116. }
  117. static void mpfs_mbox_rx_data(struct mbox_chan *chan)
  118. {
  119. struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
  120. struct mpfs_mss_response *response = mbox->response;
  121. u16 num_words = ALIGN((response->resp_size), (4)) / 4U;
  122. u32 i;
  123. if (!response->resp_msg) {
  124. dev_err(mbox->dev, "failed to assign memory for response %d\n", -ENOMEM);
  125. return;
  126. }
  127. /*
  128. * We should *never* get an interrupt while the controller is
  129. * still in the busy state. If we do, something has gone badly
  130. * wrong & the content of the mailbox would not be valid.
  131. */
  132. if (mpfs_mbox_busy(mbox)) {
  133. dev_err(mbox->dev, "got an interrupt but system controller is busy\n");
  134. response->resp_status = 0xDEAD;
  135. return;
  136. }
  137. for (i = 0; i < num_words; i++) {
  138. response->resp_msg[i] =
  139. readl_relaxed(mbox->mbox_base
  140. + mbox->resp_offset + i * 0x4);
  141. }
  142. mbox_chan_received_data(chan, response);
  143. }
  144. static irqreturn_t mpfs_mbox_inbox_isr(int irq, void *data)
  145. {
  146. struct mbox_chan *chan = data;
  147. struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
  148. writel_relaxed(0, mbox->int_reg);
  149. mpfs_mbox_rx_data(chan);
  150. return IRQ_HANDLED;
  151. }
  152. static int mpfs_mbox_startup(struct mbox_chan *chan)
  153. {
  154. struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
  155. int ret = 0;
  156. if (!mbox)
  157. return -EINVAL;
  158. ret = devm_request_irq(mbox->dev, mbox->irq, mpfs_mbox_inbox_isr, 0, "mpfs-mailbox", chan);
  159. if (ret)
  160. dev_err(mbox->dev, "failed to register mailbox interrupt:%d\n", ret);
  161. return ret;
  162. }
  163. static void mpfs_mbox_shutdown(struct mbox_chan *chan)
  164. {
  165. struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
  166. devm_free_irq(mbox->dev, mbox->irq, chan);
  167. }
  168. static const struct mbox_chan_ops mpfs_mbox_ops = {
  169. .send_data = mpfs_mbox_send_data,
  170. .startup = mpfs_mbox_startup,
  171. .shutdown = mpfs_mbox_shutdown,
  172. .last_tx_done = mpfs_mbox_last_tx_done,
  173. };
  174. static int mpfs_mbox_probe(struct platform_device *pdev)
  175. {
  176. struct mpfs_mbox *mbox;
  177. struct resource *regs;
  178. int ret;
  179. mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
  180. if (!mbox)
  181. return -ENOMEM;
  182. mbox->ctrl_base = devm_platform_get_and_ioremap_resource(pdev, 0, &regs);
  183. if (IS_ERR(mbox->ctrl_base))
  184. return PTR_ERR(mbox->ctrl_base);
  185. mbox->int_reg = devm_platform_get_and_ioremap_resource(pdev, 1, &regs);
  186. if (IS_ERR(mbox->int_reg))
  187. return PTR_ERR(mbox->int_reg);
  188. mbox->mbox_base = devm_platform_get_and_ioremap_resource(pdev, 2, &regs);
  189. if (IS_ERR(mbox->mbox_base)) // account for the old dt-binding w/ 2 regs
  190. mbox->mbox_base = mbox->ctrl_base + MAILBOX_REG_OFFSET;
  191. mbox->irq = platform_get_irq(pdev, 0);
  192. if (mbox->irq < 0)
  193. return mbox->irq;
  194. mbox->dev = &pdev->dev;
  195. mbox->chans[0].con_priv = mbox;
  196. mbox->controller.dev = mbox->dev;
  197. mbox->controller.num_chans = 1;
  198. mbox->controller.chans = mbox->chans;
  199. mbox->controller.ops = &mpfs_mbox_ops;
  200. mbox->controller.txdone_poll = true;
  201. mbox->controller.txpoll_period = 10u;
  202. ret = devm_mbox_controller_register(&pdev->dev, &mbox->controller);
  203. if (ret) {
  204. dev_err(&pdev->dev, "Registering MPFS mailbox controller failed\n");
  205. return ret;
  206. }
  207. dev_info(&pdev->dev, "Registered MPFS mailbox controller driver\n");
  208. return 0;
  209. }
  210. static const struct of_device_id mpfs_mbox_of_match[] = {
  211. {.compatible = "microchip,mpfs-mailbox", },
  212. {},
  213. };
  214. MODULE_DEVICE_TABLE(of, mpfs_mbox_of_match);
  215. static struct platform_driver mpfs_mbox_driver = {
  216. .driver = {
  217. .name = "mpfs-mailbox",
  218. .of_match_table = mpfs_mbox_of_match,
  219. },
  220. .probe = mpfs_mbox_probe,
  221. };
  222. module_platform_driver(mpfs_mbox_driver);
  223. MODULE_LICENSE("GPL v2");
  224. MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
  225. MODULE_DESCRIPTION("MPFS mailbox controller driver");