platform_mhu.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) 2016 BayLibre SAS.
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. * Synchronised with arm_mhu.c from :
  5. * Copyright (C) 2013-2015 Fujitsu Semiconductor Ltd.
  6. * Copyright (C) 2015 Linaro Ltd.
  7. * Author: Jassi Brar <jaswinder.singh@linaro.org>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/mutex.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/module.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/mailbox_controller.h>
  28. #define INTR_SET_OFS 0x0
  29. #define INTR_STAT_OFS 0x4
  30. #define INTR_CLR_OFS 0x8
  31. #define MHU_SEC_OFFSET 0x0
  32. #define MHU_LP_OFFSET 0xc
  33. #define MHU_HP_OFFSET 0x18
  34. #define TX_REG_OFFSET 0x24
  35. #define MHU_CHANS 3
  36. struct platform_mhu_link {
  37. int irq;
  38. void __iomem *tx_reg;
  39. void __iomem *rx_reg;
  40. };
  41. struct platform_mhu {
  42. void __iomem *base;
  43. struct platform_mhu_link mlink[MHU_CHANS];
  44. struct mbox_chan chan[MHU_CHANS];
  45. struct mbox_controller mbox;
  46. };
  47. static irqreturn_t platform_mhu_rx_interrupt(int irq, void *p)
  48. {
  49. struct mbox_chan *chan = p;
  50. struct platform_mhu_link *mlink = chan->con_priv;
  51. u32 val;
  52. val = readl_relaxed(mlink->rx_reg + INTR_STAT_OFS);
  53. if (!val)
  54. return IRQ_NONE;
  55. mbox_chan_received_data(chan, (void *)&val);
  56. writel_relaxed(val, mlink->rx_reg + INTR_CLR_OFS);
  57. return IRQ_HANDLED;
  58. }
  59. static bool platform_mhu_last_tx_done(struct mbox_chan *chan)
  60. {
  61. struct platform_mhu_link *mlink = chan->con_priv;
  62. u32 val = readl_relaxed(mlink->tx_reg + INTR_STAT_OFS);
  63. return (val == 0);
  64. }
  65. static int platform_mhu_send_data(struct mbox_chan *chan, void *data)
  66. {
  67. struct platform_mhu_link *mlink = chan->con_priv;
  68. u32 *arg = data;
  69. writel_relaxed(*arg, mlink->tx_reg + INTR_SET_OFS);
  70. return 0;
  71. }
  72. static int platform_mhu_startup(struct mbox_chan *chan)
  73. {
  74. struct platform_mhu_link *mlink = chan->con_priv;
  75. u32 val;
  76. int ret;
  77. val = readl_relaxed(mlink->tx_reg + INTR_STAT_OFS);
  78. writel_relaxed(val, mlink->tx_reg + INTR_CLR_OFS);
  79. ret = request_irq(mlink->irq, platform_mhu_rx_interrupt,
  80. IRQF_SHARED, "platform_mhu_link", chan);
  81. if (ret) {
  82. dev_err(chan->mbox->dev,
  83. "Unable to acquire IRQ %d\n", mlink->irq);
  84. return ret;
  85. }
  86. return 0;
  87. }
  88. static void platform_mhu_shutdown(struct mbox_chan *chan)
  89. {
  90. struct platform_mhu_link *mlink = chan->con_priv;
  91. free_irq(mlink->irq, chan);
  92. }
  93. static const struct mbox_chan_ops platform_mhu_ops = {
  94. .send_data = platform_mhu_send_data,
  95. .startup = platform_mhu_startup,
  96. .shutdown = platform_mhu_shutdown,
  97. .last_tx_done = platform_mhu_last_tx_done,
  98. };
  99. static int platform_mhu_probe(struct platform_device *pdev)
  100. {
  101. int i, err;
  102. struct platform_mhu *mhu;
  103. struct device *dev = &pdev->dev;
  104. struct resource *res;
  105. int platform_mhu_reg[MHU_CHANS] = {
  106. MHU_SEC_OFFSET, MHU_LP_OFFSET, MHU_HP_OFFSET
  107. };
  108. /* Allocate memory for device */
  109. mhu = devm_kzalloc(dev, sizeof(*mhu), GFP_KERNEL);
  110. if (!mhu)
  111. return -ENOMEM;
  112. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  113. mhu->base = devm_ioremap_resource(dev, res);
  114. if (IS_ERR(mhu->base)) {
  115. dev_err(dev, "ioremap failed\n");
  116. return PTR_ERR(mhu->base);
  117. }
  118. for (i = 0; i < MHU_CHANS; i++) {
  119. mhu->chan[i].con_priv = &mhu->mlink[i];
  120. mhu->mlink[i].irq = platform_get_irq(pdev, i);
  121. if (mhu->mlink[i].irq < 0) {
  122. dev_err(dev, "failed to get irq%d\n", i);
  123. return mhu->mlink[i].irq;
  124. }
  125. mhu->mlink[i].rx_reg = mhu->base + platform_mhu_reg[i];
  126. mhu->mlink[i].tx_reg = mhu->mlink[i].rx_reg + TX_REG_OFFSET;
  127. }
  128. mhu->mbox.dev = dev;
  129. mhu->mbox.chans = &mhu->chan[0];
  130. mhu->mbox.num_chans = MHU_CHANS;
  131. mhu->mbox.ops = &platform_mhu_ops;
  132. mhu->mbox.txdone_irq = false;
  133. mhu->mbox.txdone_poll = true;
  134. mhu->mbox.txpoll_period = 1;
  135. platform_set_drvdata(pdev, mhu);
  136. err = mbox_controller_register(&mhu->mbox);
  137. if (err) {
  138. dev_err(dev, "Failed to register mailboxes %d\n", err);
  139. return err;
  140. }
  141. dev_info(dev, "Platform MHU Mailbox registered\n");
  142. return 0;
  143. }
  144. static int platform_mhu_remove(struct platform_device *pdev)
  145. {
  146. struct platform_mhu *mhu = platform_get_drvdata(pdev);
  147. mbox_controller_unregister(&mhu->mbox);
  148. return 0;
  149. }
  150. static const struct of_device_id platform_mhu_dt_ids[] = {
  151. { .compatible = "amlogic,meson-gxbb-mhu", },
  152. { /* sentinel */ },
  153. };
  154. MODULE_DEVICE_TABLE(of, platform_mhu_dt_ids);
  155. static struct platform_driver platform_mhu_driver = {
  156. .probe = platform_mhu_probe,
  157. .remove = platform_mhu_remove,
  158. .driver = {
  159. .name = "platform-mhu",
  160. .of_match_table = platform_mhu_dt_ids,
  161. },
  162. };
  163. module_platform_driver(platform_mhu_driver);
  164. MODULE_LICENSE("GPL v2");
  165. MODULE_ALIAS("platform:platform-mhu");
  166. MODULE_DESCRIPTION("Platform MHU Driver");
  167. MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");