imx-mailbox.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2018 Pengutronix, Oleksij Rempel <o.rempel@pengutronix.de>
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mailbox_controller.h>
  10. #include <linux/module.h>
  11. #include <linux/of_device.h>
  12. #include <linux/slab.h>
  13. /* Transmit Register */
  14. #define IMX_MU_xTRn(x) (0x00 + 4 * (x))
  15. /* Receive Register */
  16. #define IMX_MU_xRRn(x) (0x10 + 4 * (x))
  17. /* Status Register */
  18. #define IMX_MU_xSR 0x20
  19. #define IMX_MU_xSR_GIPn(x) BIT(28 + (3 - (x)))
  20. #define IMX_MU_xSR_RFn(x) BIT(24 + (3 - (x)))
  21. #define IMX_MU_xSR_TEn(x) BIT(20 + (3 - (x)))
  22. #define IMX_MU_xSR_BRDIP BIT(9)
  23. /* Control Register */
  24. #define IMX_MU_xCR 0x24
  25. /* General Purpose Interrupt Enable */
  26. #define IMX_MU_xCR_GIEn(x) BIT(28 + (3 - (x)))
  27. /* Receive Interrupt Enable */
  28. #define IMX_MU_xCR_RIEn(x) BIT(24 + (3 - (x)))
  29. /* Transmit Interrupt Enable */
  30. #define IMX_MU_xCR_TIEn(x) BIT(20 + (3 - (x)))
  31. /* General Purpose Interrupt Request */
  32. #define IMX_MU_xCR_GIRn(x) BIT(16 + (3 - (x)))
  33. #define IMX_MU_CHANS 16
  34. #define IMX_MU_CHAN_NAME_SIZE 20
  35. enum imx_mu_chan_type {
  36. IMX_MU_TYPE_TX, /* Tx */
  37. IMX_MU_TYPE_RX, /* Rx */
  38. IMX_MU_TYPE_TXDB, /* Tx doorbell */
  39. IMX_MU_TYPE_RXDB, /* Rx doorbell */
  40. };
  41. struct imx_mu_con_priv {
  42. unsigned int idx;
  43. char irq_desc[IMX_MU_CHAN_NAME_SIZE];
  44. enum imx_mu_chan_type type;
  45. struct mbox_chan *chan;
  46. struct tasklet_struct txdb_tasklet;
  47. };
  48. struct imx_mu_priv {
  49. struct device *dev;
  50. void __iomem *base;
  51. spinlock_t xcr_lock; /* control register lock */
  52. struct mbox_controller mbox;
  53. struct mbox_chan mbox_chans[IMX_MU_CHANS];
  54. struct imx_mu_con_priv con_priv[IMX_MU_CHANS];
  55. struct clk *clk;
  56. int irq;
  57. bool side_b;
  58. };
  59. static struct imx_mu_priv *to_imx_mu_priv(struct mbox_controller *mbox)
  60. {
  61. return container_of(mbox, struct imx_mu_priv, mbox);
  62. }
  63. static void imx_mu_write(struct imx_mu_priv *priv, u32 val, u32 offs)
  64. {
  65. iowrite32(val, priv->base + offs);
  66. }
  67. static u32 imx_mu_read(struct imx_mu_priv *priv, u32 offs)
  68. {
  69. return ioread32(priv->base + offs);
  70. }
  71. static u32 imx_mu_xcr_rmw(struct imx_mu_priv *priv, u32 set, u32 clr)
  72. {
  73. unsigned long flags;
  74. u32 val;
  75. spin_lock_irqsave(&priv->xcr_lock, flags);
  76. val = imx_mu_read(priv, IMX_MU_xCR);
  77. val &= ~clr;
  78. val |= set;
  79. imx_mu_write(priv, val, IMX_MU_xCR);
  80. spin_unlock_irqrestore(&priv->xcr_lock, flags);
  81. return val;
  82. }
  83. static void imx_mu_txdb_tasklet(unsigned long data)
  84. {
  85. struct imx_mu_con_priv *cp = (struct imx_mu_con_priv *)data;
  86. mbox_chan_txdone(cp->chan, 0);
  87. }
  88. static irqreturn_t imx_mu_isr(int irq, void *p)
  89. {
  90. struct mbox_chan *chan = p;
  91. struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
  92. struct imx_mu_con_priv *cp = chan->con_priv;
  93. u32 val, ctrl, dat;
  94. ctrl = imx_mu_read(priv, IMX_MU_xCR);
  95. val = imx_mu_read(priv, IMX_MU_xSR);
  96. switch (cp->type) {
  97. case IMX_MU_TYPE_TX:
  98. val &= IMX_MU_xSR_TEn(cp->idx) &
  99. (ctrl & IMX_MU_xCR_TIEn(cp->idx));
  100. break;
  101. case IMX_MU_TYPE_RX:
  102. val &= IMX_MU_xSR_RFn(cp->idx) &
  103. (ctrl & IMX_MU_xCR_RIEn(cp->idx));
  104. break;
  105. case IMX_MU_TYPE_RXDB:
  106. val &= IMX_MU_xSR_GIPn(cp->idx) &
  107. (ctrl & IMX_MU_xCR_GIEn(cp->idx));
  108. break;
  109. default:
  110. break;
  111. }
  112. if (!val)
  113. return IRQ_NONE;
  114. if (val == IMX_MU_xSR_TEn(cp->idx)) {
  115. imx_mu_xcr_rmw(priv, 0, IMX_MU_xCR_TIEn(cp->idx));
  116. mbox_chan_txdone(chan, 0);
  117. } else if (val == IMX_MU_xSR_RFn(cp->idx)) {
  118. dat = imx_mu_read(priv, IMX_MU_xRRn(cp->idx));
  119. mbox_chan_received_data(chan, (void *)&dat);
  120. } else if (val == IMX_MU_xSR_GIPn(cp->idx)) {
  121. imx_mu_write(priv, IMX_MU_xSR_GIPn(cp->idx), IMX_MU_xSR);
  122. mbox_chan_received_data(chan, NULL);
  123. } else {
  124. dev_warn_ratelimited(priv->dev, "Not handled interrupt\n");
  125. return IRQ_NONE;
  126. }
  127. return IRQ_HANDLED;
  128. }
  129. static int imx_mu_send_data(struct mbox_chan *chan, void *data)
  130. {
  131. struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
  132. struct imx_mu_con_priv *cp = chan->con_priv;
  133. u32 *arg = data;
  134. switch (cp->type) {
  135. case IMX_MU_TYPE_TX:
  136. imx_mu_write(priv, *arg, IMX_MU_xTRn(cp->idx));
  137. imx_mu_xcr_rmw(priv, IMX_MU_xCR_TIEn(cp->idx), 0);
  138. break;
  139. case IMX_MU_TYPE_TXDB:
  140. imx_mu_xcr_rmw(priv, IMX_MU_xCR_GIRn(cp->idx), 0);
  141. tasklet_schedule(&cp->txdb_tasklet);
  142. break;
  143. default:
  144. dev_warn_ratelimited(priv->dev, "Send data on wrong channel type: %d\n", cp->type);
  145. return -EINVAL;
  146. }
  147. return 0;
  148. }
  149. static int imx_mu_startup(struct mbox_chan *chan)
  150. {
  151. struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
  152. struct imx_mu_con_priv *cp = chan->con_priv;
  153. int ret;
  154. if (cp->type == IMX_MU_TYPE_TXDB) {
  155. /* Tx doorbell don't have ACK support */
  156. tasklet_init(&cp->txdb_tasklet, imx_mu_txdb_tasklet,
  157. (unsigned long)cp);
  158. return 0;
  159. }
  160. ret = request_irq(priv->irq, imx_mu_isr, IRQF_SHARED, cp->irq_desc,
  161. chan);
  162. if (ret) {
  163. dev_err(priv->dev,
  164. "Unable to acquire IRQ %d\n", priv->irq);
  165. return ret;
  166. }
  167. switch (cp->type) {
  168. case IMX_MU_TYPE_RX:
  169. imx_mu_xcr_rmw(priv, IMX_MU_xCR_RIEn(cp->idx), 0);
  170. break;
  171. case IMX_MU_TYPE_RXDB:
  172. imx_mu_xcr_rmw(priv, IMX_MU_xCR_GIEn(cp->idx), 0);
  173. break;
  174. default:
  175. break;
  176. }
  177. return 0;
  178. }
  179. static void imx_mu_shutdown(struct mbox_chan *chan)
  180. {
  181. struct imx_mu_priv *priv = to_imx_mu_priv(chan->mbox);
  182. struct imx_mu_con_priv *cp = chan->con_priv;
  183. if (cp->type == IMX_MU_TYPE_TXDB) {
  184. tasklet_kill(&cp->txdb_tasklet);
  185. return;
  186. }
  187. imx_mu_xcr_rmw(priv, 0,
  188. IMX_MU_xCR_TIEn(cp->idx) | IMX_MU_xCR_RIEn(cp->idx));
  189. free_irq(priv->irq, chan);
  190. }
  191. static const struct mbox_chan_ops imx_mu_ops = {
  192. .send_data = imx_mu_send_data,
  193. .startup = imx_mu_startup,
  194. .shutdown = imx_mu_shutdown,
  195. };
  196. static struct mbox_chan * imx_mu_xlate(struct mbox_controller *mbox,
  197. const struct of_phandle_args *sp)
  198. {
  199. u32 type, idx, chan;
  200. if (sp->args_count != 2) {
  201. dev_err(mbox->dev, "Invalid argument count %d\n", sp->args_count);
  202. return ERR_PTR(-EINVAL);
  203. }
  204. type = sp->args[0]; /* channel type */
  205. idx = sp->args[1]; /* index */
  206. chan = type * 4 + idx;
  207. if (chan >= mbox->num_chans) {
  208. dev_err(mbox->dev, "Not supported channel number: %d. (type: %d, idx: %d)\n", chan, type, idx);
  209. return ERR_PTR(-EINVAL);
  210. }
  211. return &mbox->chans[chan];
  212. }
  213. static void imx_mu_init_generic(struct imx_mu_priv *priv)
  214. {
  215. if (priv->side_b)
  216. return;
  217. /* Set default MU configuration */
  218. imx_mu_write(priv, 0, IMX_MU_xCR);
  219. }
  220. static int imx_mu_probe(struct platform_device *pdev)
  221. {
  222. struct device *dev = &pdev->dev;
  223. struct device_node *np = dev->of_node;
  224. struct resource *iomem;
  225. struct imx_mu_priv *priv;
  226. unsigned int i;
  227. int ret;
  228. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  229. if (!priv)
  230. return -ENOMEM;
  231. priv->dev = dev;
  232. iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  233. priv->base = devm_ioremap_resource(&pdev->dev, iomem);
  234. if (IS_ERR(priv->base))
  235. return PTR_ERR(priv->base);
  236. priv->irq = platform_get_irq(pdev, 0);
  237. if (priv->irq < 0)
  238. return priv->irq;
  239. priv->clk = devm_clk_get(dev, NULL);
  240. if (IS_ERR(priv->clk)) {
  241. if (PTR_ERR(priv->clk) != -ENOENT)
  242. return PTR_ERR(priv->clk);
  243. priv->clk = NULL;
  244. }
  245. ret = clk_prepare_enable(priv->clk);
  246. if (ret) {
  247. dev_err(dev, "Failed to enable clock\n");
  248. return ret;
  249. }
  250. for (i = 0; i < IMX_MU_CHANS; i++) {
  251. struct imx_mu_con_priv *cp = &priv->con_priv[i];
  252. cp->idx = i % 4;
  253. cp->type = i >> 2;
  254. cp->chan = &priv->mbox_chans[i];
  255. priv->mbox_chans[i].con_priv = cp;
  256. snprintf(cp->irq_desc, sizeof(cp->irq_desc),
  257. "imx_mu_chan[%i-%i]", cp->type, cp->idx);
  258. }
  259. priv->side_b = of_property_read_bool(np, "fsl,mu-side-b");
  260. spin_lock_init(&priv->xcr_lock);
  261. priv->mbox.dev = dev;
  262. priv->mbox.ops = &imx_mu_ops;
  263. priv->mbox.chans = priv->mbox_chans;
  264. priv->mbox.num_chans = IMX_MU_CHANS;
  265. priv->mbox.of_xlate = imx_mu_xlate;
  266. priv->mbox.txdone_irq = true;
  267. platform_set_drvdata(pdev, priv);
  268. imx_mu_init_generic(priv);
  269. return mbox_controller_register(&priv->mbox);
  270. }
  271. static int imx_mu_remove(struct platform_device *pdev)
  272. {
  273. struct imx_mu_priv *priv = platform_get_drvdata(pdev);
  274. mbox_controller_unregister(&priv->mbox);
  275. clk_disable_unprepare(priv->clk);
  276. return 0;
  277. }
  278. static const struct of_device_id imx_mu_dt_ids[] = {
  279. { .compatible = "fsl,imx6sx-mu" },
  280. { },
  281. };
  282. MODULE_DEVICE_TABLE(of, imx_mu_dt_ids);
  283. static struct platform_driver imx_mu_driver = {
  284. .probe = imx_mu_probe,
  285. .remove = imx_mu_remove,
  286. .driver = {
  287. .name = "imx_mu",
  288. .of_match_table = imx_mu_dt_ids,
  289. },
  290. };
  291. module_platform_driver(imx_mu_driver);
  292. MODULE_AUTHOR("Oleksij Rempel <o.rempel@pengutronix.de>");
  293. MODULE_DESCRIPTION("Message Unit driver for i.MX");
  294. MODULE_LICENSE("GPL v2");