bcm2835-mailbox.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010,2015 Broadcom
  4. * Copyright (C) 2013-2014 Lubomir Rintel
  5. * Copyright (C) 2013 Craig McGeachie
  6. *
  7. * Parts of the driver are based on:
  8. * - arch/arm/mach-bcm2708/vcio.c file written by Gray Girling that was
  9. * obtained from branch "rpi-3.6.y" of git://github.com/raspberrypi/
  10. * linux.git
  11. * - drivers/mailbox/bcm2835-ipc.c by Lubomir Rintel at
  12. * https://github.com/hackerspace/rpi-linux/blob/lr-raspberry-pi/drivers/
  13. * mailbox/bcm2835-ipc.c
  14. * - documentation available on the following web site:
  15. * https://github.com/raspberrypi/firmware/wiki/Mailbox-property-interface
  16. */
  17. #include <linux/device.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/irq.h>
  22. #include <linux/kernel.h>
  23. #include <linux/mailbox_controller.h>
  24. #include <linux/module.h>
  25. #include <linux/of_address.h>
  26. #include <linux/of_irq.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/spinlock.h>
  29. /* Mailboxes */
  30. #define ARM_0_MAIL0 0x00
  31. #define ARM_0_MAIL1 0x20
  32. /*
  33. * Mailbox registers. We basically only support mailbox 0 & 1. We
  34. * deliver to the VC in mailbox 1, it delivers to us in mailbox 0. See
  35. * BCM2835-ARM-Peripherals.pdf section 1.3 for an explanation about
  36. * the placement of memory barriers.
  37. */
  38. #define MAIL0_RD (ARM_0_MAIL0 + 0x00)
  39. #define MAIL0_POL (ARM_0_MAIL0 + 0x10)
  40. #define MAIL0_STA (ARM_0_MAIL0 + 0x18)
  41. #define MAIL0_CNF (ARM_0_MAIL0 + 0x1C)
  42. #define MAIL1_WRT (ARM_0_MAIL1 + 0x00)
  43. #define MAIL1_STA (ARM_0_MAIL1 + 0x18)
  44. /* Status register: FIFO state. */
  45. #define ARM_MS_FULL BIT(31)
  46. #define ARM_MS_EMPTY BIT(30)
  47. /* Configuration register: Enable interrupts. */
  48. #define ARM_MC_IHAVEDATAIRQEN BIT(0)
  49. struct bcm2835_mbox {
  50. void __iomem *regs;
  51. spinlock_t lock;
  52. struct mbox_controller controller;
  53. };
  54. static struct bcm2835_mbox *bcm2835_link_mbox(struct mbox_chan *link)
  55. {
  56. return container_of(link->mbox, struct bcm2835_mbox, controller);
  57. }
  58. static irqreturn_t bcm2835_mbox_irq(int irq, void *dev_id)
  59. {
  60. struct bcm2835_mbox *mbox = dev_id;
  61. struct device *dev = mbox->controller.dev;
  62. struct mbox_chan *link = &mbox->controller.chans[0];
  63. while (!(readl(mbox->regs + MAIL0_STA) & ARM_MS_EMPTY)) {
  64. u32 msg = readl(mbox->regs + MAIL0_RD);
  65. dev_dbg(dev, "Reply 0x%08X\n", msg);
  66. mbox_chan_received_data(link, &msg);
  67. }
  68. return IRQ_HANDLED;
  69. }
  70. static int bcm2835_send_data(struct mbox_chan *link, void *data)
  71. {
  72. struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
  73. u32 msg = *(u32 *)data;
  74. spin_lock(&mbox->lock);
  75. writel(msg, mbox->regs + MAIL1_WRT);
  76. dev_dbg(mbox->controller.dev, "Request 0x%08X\n", msg);
  77. spin_unlock(&mbox->lock);
  78. return 0;
  79. }
  80. static int bcm2835_startup(struct mbox_chan *link)
  81. {
  82. struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
  83. /* Enable the interrupt on data reception */
  84. writel(ARM_MC_IHAVEDATAIRQEN, mbox->regs + MAIL0_CNF);
  85. return 0;
  86. }
  87. static void bcm2835_shutdown(struct mbox_chan *link)
  88. {
  89. struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
  90. writel(0, mbox->regs + MAIL0_CNF);
  91. }
  92. static bool bcm2835_last_tx_done(struct mbox_chan *link)
  93. {
  94. struct bcm2835_mbox *mbox = bcm2835_link_mbox(link);
  95. bool ret;
  96. spin_lock(&mbox->lock);
  97. ret = !(readl(mbox->regs + MAIL1_STA) & ARM_MS_FULL);
  98. spin_unlock(&mbox->lock);
  99. return ret;
  100. }
  101. static const struct mbox_chan_ops bcm2835_mbox_chan_ops = {
  102. .send_data = bcm2835_send_data,
  103. .startup = bcm2835_startup,
  104. .shutdown = bcm2835_shutdown,
  105. .last_tx_done = bcm2835_last_tx_done
  106. };
  107. static struct mbox_chan *bcm2835_mbox_index_xlate(struct mbox_controller *mbox,
  108. const struct of_phandle_args *sp)
  109. {
  110. if (sp->args_count != 0)
  111. return ERR_PTR(-EINVAL);
  112. return &mbox->chans[0];
  113. }
  114. static int bcm2835_mbox_probe(struct platform_device *pdev)
  115. {
  116. struct device *dev = &pdev->dev;
  117. int ret = 0;
  118. struct bcm2835_mbox *mbox;
  119. mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
  120. if (mbox == NULL)
  121. return -ENOMEM;
  122. spin_lock_init(&mbox->lock);
  123. ret = devm_request_irq(dev, irq_of_parse_and_map(dev->of_node, 0),
  124. bcm2835_mbox_irq, 0, dev_name(dev), mbox);
  125. if (ret) {
  126. dev_err(dev, "Failed to register a mailbox IRQ handler: %d\n",
  127. ret);
  128. return -ENODEV;
  129. }
  130. mbox->regs = devm_platform_ioremap_resource(pdev, 0);
  131. if (IS_ERR(mbox->regs)) {
  132. ret = PTR_ERR(mbox->regs);
  133. return ret;
  134. }
  135. mbox->controller.txdone_poll = true;
  136. mbox->controller.txpoll_period = 5;
  137. mbox->controller.ops = &bcm2835_mbox_chan_ops;
  138. mbox->controller.of_xlate = &bcm2835_mbox_index_xlate;
  139. mbox->controller.dev = dev;
  140. mbox->controller.num_chans = 1;
  141. mbox->controller.chans = devm_kzalloc(dev,
  142. sizeof(*mbox->controller.chans), GFP_KERNEL);
  143. if (!mbox->controller.chans)
  144. return -ENOMEM;
  145. ret = devm_mbox_controller_register(dev, &mbox->controller);
  146. if (ret)
  147. return ret;
  148. platform_set_drvdata(pdev, mbox);
  149. dev_info(dev, "mailbox enabled\n");
  150. return ret;
  151. }
  152. static const struct of_device_id bcm2835_mbox_of_match[] = {
  153. { .compatible = "brcm,bcm2835-mbox", },
  154. {},
  155. };
  156. MODULE_DEVICE_TABLE(of, bcm2835_mbox_of_match);
  157. static struct platform_driver bcm2835_mbox_driver = {
  158. .driver = {
  159. .name = "bcm2835-mbox",
  160. .of_match_table = bcm2835_mbox_of_match,
  161. },
  162. .probe = bcm2835_mbox_probe,
  163. };
  164. module_platform_driver(bcm2835_mbox_driver);
  165. MODULE_AUTHOR("Lubomir Rintel <[email protected]>");
  166. MODULE_DESCRIPTION("BCM2835 mailbox IPC driver");
  167. MODULE_LICENSE("GPL v2");