bcm2835-mailbox.c 5.9 KB

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