hi3660-mailbox.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0
  2. // Copyright (c) 2017-2018 Hisilicon Limited.
  3. // Copyright (c) 2017-2018 Linaro Limited.
  4. #include <linux/bitops.h>
  5. #include <linux/delay.h>
  6. #include <linux/device.h>
  7. #include <linux/err.h>
  8. #include <linux/interrupt.h>
  9. #include <linux/io.h>
  10. #include <linux/iopoll.h>
  11. #include <linux/mailbox_controller.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/slab.h>
  15. #include "mailbox.h"
  16. #define MBOX_CHAN_MAX 32
  17. #define MBOX_RX 0x0
  18. #define MBOX_TX 0x1
  19. #define MBOX_BASE(mbox, ch) ((mbox)->base + ((ch) * 0x40))
  20. #define MBOX_SRC_REG 0x00
  21. #define MBOX_DST_REG 0x04
  22. #define MBOX_DCLR_REG 0x08
  23. #define MBOX_DSTAT_REG 0x0c
  24. #define MBOX_MODE_REG 0x10
  25. #define MBOX_IMASK_REG 0x14
  26. #define MBOX_ICLR_REG 0x18
  27. #define MBOX_SEND_REG 0x1c
  28. #define MBOX_DATA_REG 0x20
  29. #define MBOX_IPC_LOCK_REG 0xa00
  30. #define MBOX_IPC_UNLOCK 0x1acce551
  31. #define MBOX_AUTOMATIC_ACK 1
  32. #define MBOX_STATE_IDLE BIT(4)
  33. #define MBOX_STATE_READY BIT(5)
  34. #define MBOX_STATE_ACK BIT(7)
  35. #define MBOX_MSG_LEN 8
  36. /**
  37. * Hi3660 mailbox channel information
  38. *
  39. * A channel can be used for TX or RX, it can trigger remote
  40. * processor interrupt to notify remote processor and can receive
  41. * interrupt if has incoming message.
  42. *
  43. * @dst_irq: Interrupt vector for remote processor
  44. * @ack_irq: Interrupt vector for local processor
  45. */
  46. struct hi3660_chan_info {
  47. unsigned int dst_irq;
  48. unsigned int ack_irq;
  49. };
  50. /**
  51. * Hi3660 mailbox controller data
  52. *
  53. * Mailbox controller includes 32 channels and can allocate
  54. * channel for message transferring.
  55. *
  56. * @dev: Device to which it is attached
  57. * @base: Base address of the register mapping region
  58. * @chan: Representation of channels in mailbox controller
  59. * @mchan: Representation of channel info
  60. * @controller: Representation of a communication channel controller
  61. */
  62. struct hi3660_mbox {
  63. struct device *dev;
  64. void __iomem *base;
  65. struct mbox_chan chan[MBOX_CHAN_MAX];
  66. struct hi3660_chan_info mchan[MBOX_CHAN_MAX];
  67. struct mbox_controller controller;
  68. };
  69. static struct hi3660_mbox *to_hi3660_mbox(struct mbox_controller *mbox)
  70. {
  71. return container_of(mbox, struct hi3660_mbox, controller);
  72. }
  73. static int hi3660_mbox_check_state(struct mbox_chan *chan)
  74. {
  75. unsigned long ch = (unsigned long)chan->con_priv;
  76. struct hi3660_mbox *mbox = to_hi3660_mbox(chan->mbox);
  77. struct hi3660_chan_info *mchan = &mbox->mchan[ch];
  78. void __iomem *base = MBOX_BASE(mbox, ch);
  79. unsigned long val;
  80. unsigned int ret;
  81. /* Mailbox is ready to use */
  82. if (readl(base + MBOX_MODE_REG) & MBOX_STATE_READY)
  83. return 0;
  84. /* Wait for acknowledge from remote */
  85. ret = readx_poll_timeout_atomic(readl, base + MBOX_MODE_REG,
  86. val, (val & MBOX_STATE_ACK), 1000, 300000);
  87. if (ret) {
  88. dev_err(mbox->dev, "%s: timeout for receiving ack\n", __func__);
  89. return ret;
  90. }
  91. /* clear ack state, mailbox will get back to ready state */
  92. writel(BIT(mchan->ack_irq), base + MBOX_ICLR_REG);
  93. return 0;
  94. }
  95. static int hi3660_mbox_unlock(struct mbox_chan *chan)
  96. {
  97. struct hi3660_mbox *mbox = to_hi3660_mbox(chan->mbox);
  98. unsigned int val, retry = 3;
  99. do {
  100. writel(MBOX_IPC_UNLOCK, mbox->base + MBOX_IPC_LOCK_REG);
  101. val = readl(mbox->base + MBOX_IPC_LOCK_REG);
  102. if (!val)
  103. break;
  104. udelay(10);
  105. } while (retry--);
  106. if (val)
  107. dev_err(mbox->dev, "%s: failed to unlock mailbox\n", __func__);
  108. return (!val) ? 0 : -ETIMEDOUT;
  109. }
  110. static int hi3660_mbox_acquire_channel(struct mbox_chan *chan)
  111. {
  112. unsigned long ch = (unsigned long)chan->con_priv;
  113. struct hi3660_mbox *mbox = to_hi3660_mbox(chan->mbox);
  114. struct hi3660_chan_info *mchan = &mbox->mchan[ch];
  115. void __iomem *base = MBOX_BASE(mbox, ch);
  116. unsigned int val, retry;
  117. for (retry = 10; retry; retry--) {
  118. /* Check if channel is in idle state */
  119. if (readl(base + MBOX_MODE_REG) & MBOX_STATE_IDLE) {
  120. writel(BIT(mchan->ack_irq), base + MBOX_SRC_REG);
  121. /* Check ack bit has been set successfully */
  122. val = readl(base + MBOX_SRC_REG);
  123. if (val & BIT(mchan->ack_irq))
  124. break;
  125. }
  126. }
  127. if (!retry)
  128. dev_err(mbox->dev, "%s: failed to acquire channel\n", __func__);
  129. return retry ? 0 : -ETIMEDOUT;
  130. }
  131. static int hi3660_mbox_startup(struct mbox_chan *chan)
  132. {
  133. int ret;
  134. ret = hi3660_mbox_unlock(chan);
  135. if (ret)
  136. return ret;
  137. ret = hi3660_mbox_acquire_channel(chan);
  138. if (ret)
  139. return ret;
  140. return 0;
  141. }
  142. static int hi3660_mbox_send_data(struct mbox_chan *chan, void *msg)
  143. {
  144. unsigned long ch = (unsigned long)chan->con_priv;
  145. struct hi3660_mbox *mbox = to_hi3660_mbox(chan->mbox);
  146. struct hi3660_chan_info *mchan = &mbox->mchan[ch];
  147. void __iomem *base = MBOX_BASE(mbox, ch);
  148. u32 *buf = msg;
  149. unsigned int i;
  150. int ret;
  151. ret = hi3660_mbox_check_state(chan);
  152. if (ret)
  153. return ret;
  154. /* Clear mask for destination interrupt */
  155. writel_relaxed(~BIT(mchan->dst_irq), base + MBOX_IMASK_REG);
  156. /* Config destination for interrupt vector */
  157. writel_relaxed(BIT(mchan->dst_irq), base + MBOX_DST_REG);
  158. /* Automatic acknowledge mode */
  159. writel_relaxed(MBOX_AUTOMATIC_ACK, base + MBOX_MODE_REG);
  160. /* Fill message data */
  161. for (i = 0; i < MBOX_MSG_LEN; i++)
  162. writel_relaxed(buf[i], base + MBOX_DATA_REG + i * 4);
  163. /* Trigger data transferring */
  164. writel(BIT(mchan->ack_irq), base + MBOX_SEND_REG);
  165. return 0;
  166. }
  167. static const struct mbox_chan_ops hi3660_mbox_ops = {
  168. .startup = hi3660_mbox_startup,
  169. .send_data = hi3660_mbox_send_data,
  170. };
  171. static struct mbox_chan *hi3660_mbox_xlate(struct mbox_controller *controller,
  172. const struct of_phandle_args *spec)
  173. {
  174. struct hi3660_mbox *mbox = to_hi3660_mbox(controller);
  175. struct hi3660_chan_info *mchan;
  176. unsigned int ch = spec->args[0];
  177. if (ch >= MBOX_CHAN_MAX) {
  178. dev_err(mbox->dev, "Invalid channel idx %d\n", ch);
  179. return ERR_PTR(-EINVAL);
  180. }
  181. mchan = &mbox->mchan[ch];
  182. mchan->dst_irq = spec->args[1];
  183. mchan->ack_irq = spec->args[2];
  184. return &mbox->chan[ch];
  185. }
  186. static const struct of_device_id hi3660_mbox_of_match[] = {
  187. { .compatible = "hisilicon,hi3660-mbox", },
  188. {},
  189. };
  190. MODULE_DEVICE_TABLE(of, hi3660_mbox_of_match);
  191. static int hi3660_mbox_probe(struct platform_device *pdev)
  192. {
  193. struct device *dev = &pdev->dev;
  194. struct hi3660_mbox *mbox;
  195. struct mbox_chan *chan;
  196. struct resource *res;
  197. unsigned long ch;
  198. int err;
  199. mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL);
  200. if (!mbox)
  201. return -ENOMEM;
  202. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  203. mbox->base = devm_ioremap_resource(dev, res);
  204. if (IS_ERR(mbox->base))
  205. return PTR_ERR(mbox->base);
  206. mbox->dev = dev;
  207. mbox->controller.dev = dev;
  208. mbox->controller.chans = mbox->chan;
  209. mbox->controller.num_chans = MBOX_CHAN_MAX;
  210. mbox->controller.ops = &hi3660_mbox_ops;
  211. mbox->controller.of_xlate = hi3660_mbox_xlate;
  212. /* Initialize mailbox channel data */
  213. chan = mbox->chan;
  214. for (ch = 0; ch < MBOX_CHAN_MAX; ch++)
  215. chan[ch].con_priv = (void *)ch;
  216. err = devm_mbox_controller_register(dev, &mbox->controller);
  217. if (err) {
  218. dev_err(dev, "Failed to register mailbox %d\n", err);
  219. return err;
  220. }
  221. platform_set_drvdata(pdev, mbox);
  222. dev_info(dev, "Mailbox enabled\n");
  223. return 0;
  224. }
  225. static struct platform_driver hi3660_mbox_driver = {
  226. .probe = hi3660_mbox_probe,
  227. .driver = {
  228. .name = "hi3660-mbox",
  229. .of_match_table = hi3660_mbox_of_match,
  230. },
  231. };
  232. static int __init hi3660_mbox_init(void)
  233. {
  234. return platform_driver_register(&hi3660_mbox_driver);
  235. }
  236. core_initcall(hi3660_mbox_init);
  237. static void __exit hi3660_mbox_exit(void)
  238. {
  239. platform_driver_unregister(&hi3660_mbox_driver);
  240. }
  241. module_exit(hi3660_mbox_exit);
  242. MODULE_LICENSE("GPL");
  243. MODULE_DESCRIPTION("Hisilicon Hi3660 Mailbox Controller");
  244. MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");