phytium_mailbox.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Phytium SoC mailbox driver
  3. *
  4. * Copyright (c) 2020 Phytium Corporation.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/acpi.h>
  16. #include <linux/device.h>
  17. #include <linux/err.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/mailbox_controller.h>
  21. #include <linux/module.h>
  22. #include <linux/of_device.h>
  23. #include <linux/platform_device.h>
  24. #define INTR_STAT 0x0
  25. #define INTR_SET 0x8
  26. #define INTR_CLR 0x10
  27. #define TX_REG 0x100
  28. #define NR_CHANS 1
  29. struct phytium_mbox_link {
  30. unsigned irq;
  31. void __iomem *tx_reg;
  32. void __iomem *rx_reg;
  33. };
  34. struct phytium_mbox {
  35. void __iomem *base;
  36. struct phytium_mbox_link mlink;
  37. struct mbox_chan chan;
  38. struct mbox_controller mbox;
  39. };
  40. static irqreturn_t phytium_mbox_rx_irq(int irq, void *ch)
  41. {
  42. struct mbox_chan *chan = ch;
  43. struct phytium_mbox_link *mlink = chan->con_priv;
  44. u32 val;
  45. val = readl_relaxed(mlink->rx_reg + INTR_STAT);
  46. if (!val)
  47. return IRQ_NONE;
  48. mbox_chan_received_data(chan, (void *)&val);
  49. writel_relaxed(val, mlink->rx_reg + INTR_CLR);
  50. return IRQ_HANDLED;
  51. }
  52. static int phytium_mbox_send_data(struct mbox_chan *chan, void *data)
  53. {
  54. struct phytium_mbox_link *mlink = chan->con_priv;
  55. u32 *arg = data;
  56. writel_relaxed(*arg, mlink->tx_reg + INTR_SET);
  57. return 0;
  58. }
  59. static int phytium_mbox_startup(struct mbox_chan *chan)
  60. {
  61. struct phytium_mbox_link *mlink = chan->con_priv;
  62. u32 val;
  63. int ret;
  64. val = readl_relaxed(mlink->tx_reg + INTR_STAT);
  65. writel_relaxed(val, mlink->tx_reg + INTR_CLR);
  66. ret = request_irq(mlink->irq, phytium_mbox_rx_irq,
  67. IRQF_SHARED, "phytium_mbox_link", chan);
  68. if (ret) {
  69. dev_err(chan->mbox->dev,
  70. "Unable to acquire IRQ %d\n", mlink->irq);
  71. }
  72. return ret;
  73. }
  74. static void phytium_mbox_shutdown(struct mbox_chan *chan)
  75. {
  76. struct phytium_mbox_link *mlink = chan->con_priv;
  77. free_irq(mlink->irq, chan);
  78. }
  79. static bool phytium_mbox_last_tx_done(struct mbox_chan *chan)
  80. {
  81. struct phytium_mbox_link *mlink = chan->con_priv;
  82. u32 val = readl_relaxed(mlink->tx_reg + INTR_STAT);
  83. return (val == (u32)(1U << 31));
  84. }
  85. static const struct mbox_chan_ops phytium_mbox_ops = {
  86. .send_data = phytium_mbox_send_data,
  87. .startup = phytium_mbox_startup,
  88. .shutdown = phytium_mbox_shutdown,
  89. .last_tx_done = phytium_mbox_last_tx_done,
  90. };
  91. static const struct acpi_device_id phytium_mbox_acpi_match[] = {
  92. { "PHYT0009", 0 },
  93. { "FTMB0001", 0 },
  94. { },
  95. };
  96. MODULE_DEVICE_TABLE(acpi, phytium_mbox_acpi_match);
  97. static const struct of_device_id phytium_mbox_of_match[] = {
  98. { .compatible = "phytium,mbox", },
  99. { },
  100. };
  101. MODULE_DEVICE_TABLE(of, phytium_mbox_of_match);
  102. static int phytium_mbox_probe(struct platform_device *pdev)
  103. {
  104. struct phytium_mbox *mbox;
  105. struct resource *res;
  106. int err, irq;
  107. /* Allocate memory for device */
  108. mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
  109. if (!mbox)
  110. return -ENOMEM;
  111. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  112. mbox->base = devm_ioremap_resource(&pdev->dev, res);
  113. if (IS_ERR(mbox->base)) {
  114. dev_err(&pdev->dev, "ioremap base failed\n");
  115. return PTR_ERR(mbox->base);
  116. }
  117. irq = platform_get_irq(pdev, 0);
  118. if (irq < 0) {
  119. dev_err(&pdev->dev, "cannot obtain irq\n");
  120. return irq;
  121. }
  122. mbox->chan.con_priv = &mbox->mlink;
  123. mbox->mlink.irq = irq;
  124. mbox->mlink.rx_reg = mbox->base;
  125. mbox->mlink.tx_reg = mbox->mlink.rx_reg + TX_REG;
  126. mbox->mbox.dev = &pdev->dev;
  127. mbox->mbox.chans = &mbox->chan;
  128. mbox->mbox.num_chans = NR_CHANS;
  129. mbox->mbox.ops = &phytium_mbox_ops;
  130. mbox->mbox.txdone_irq = false;
  131. mbox->mbox.txdone_poll = true;
  132. mbox->mbox.txpoll_period = 1;
  133. platform_set_drvdata(pdev, mbox);
  134. err = mbox_controller_register(&mbox->mbox);
  135. if (err) {
  136. dev_err(&pdev->dev, "Failed to register mailboxes %d\n", err);
  137. goto fail;
  138. }
  139. dev_info(&pdev->dev, "Phytium SoC Mailbox registered\n");
  140. fail:
  141. return err;
  142. }
  143. static int phytium_mbox_remove(struct platform_device *pdev)
  144. {
  145. struct phytium_mbox *mbox = platform_get_drvdata(pdev);
  146. mbox_controller_unregister(&mbox->mbox);
  147. return 0;
  148. }
  149. static struct platform_driver phytium_mbox_driver = {
  150. .probe = phytium_mbox_probe,
  151. .remove = phytium_mbox_remove,
  152. .driver = {
  153. .name = "phytium-mbox",
  154. .of_match_table = of_match_ptr(phytium_mbox_of_match),
  155. .acpi_match_table = ACPI_PTR(phytium_mbox_acpi_match),
  156. },
  157. };
  158. module_platform_driver(phytium_mbox_driver);
  159. MODULE_LICENSE("GPL v2");
  160. MODULE_DESCRIPTION("Phytium SoC Mailbox Driver");
  161. MODULE_AUTHOR("Chen Baozi <chenbaozi@phytium.com.cn>");