mailbox-omap2.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Mailbox reservation modules for OMAP2/3
  3. *
  4. * Copyright (C) 2006-2009 Nokia Corporation
  5. * Written by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  6. * and Paul Mundt
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file "COPYING" in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/clk.h>
  15. #include <linux/err.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/io.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/platform_data/mailbox-omap.h>
  20. #include "omap-mbox.h"
  21. #define MAILBOX_REVISION 0x000
  22. #define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
  23. #define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
  24. #define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
  25. #define MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
  26. #define MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
  27. #define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
  28. #define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
  29. #define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
  30. #define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
  31. #define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
  32. #define MBOX_REG_SIZE 0x120
  33. #define OMAP4_MBOX_REG_SIZE 0x130
  34. #define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
  35. #define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
  36. static void __iomem *mbox_base;
  37. struct omap_mbox2_fifo {
  38. unsigned long msg;
  39. unsigned long fifo_stat;
  40. unsigned long msg_stat;
  41. };
  42. struct omap_mbox2_priv {
  43. struct omap_mbox2_fifo tx_fifo;
  44. struct omap_mbox2_fifo rx_fifo;
  45. unsigned long irqenable;
  46. unsigned long irqstatus;
  47. u32 newmsg_bit;
  48. u32 notfull_bit;
  49. u32 ctx[OMAP4_MBOX_NR_REGS];
  50. unsigned long irqdisable;
  51. u32 intr_type;
  52. };
  53. static inline unsigned int mbox_read_reg(size_t ofs)
  54. {
  55. return __raw_readl(mbox_base + ofs);
  56. }
  57. static inline void mbox_write_reg(u32 val, size_t ofs)
  58. {
  59. __raw_writel(val, mbox_base + ofs);
  60. }
  61. /* Mailbox H/W preparations */
  62. static int omap2_mbox_startup(struct omap_mbox *mbox)
  63. {
  64. u32 l;
  65. pm_runtime_enable(mbox->dev->parent);
  66. pm_runtime_get_sync(mbox->dev->parent);
  67. l = mbox_read_reg(MAILBOX_REVISION);
  68. pr_debug("omap mailbox rev %d.%d\n", (l & 0xf0) >> 4, (l & 0x0f));
  69. return 0;
  70. }
  71. static void omap2_mbox_shutdown(struct omap_mbox *mbox)
  72. {
  73. pm_runtime_put_sync(mbox->dev->parent);
  74. pm_runtime_disable(mbox->dev->parent);
  75. }
  76. /* Mailbox FIFO handle functions */
  77. static mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
  78. {
  79. struct omap_mbox2_fifo *fifo =
  80. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  81. return (mbox_msg_t) mbox_read_reg(fifo->msg);
  82. }
  83. static void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  84. {
  85. struct omap_mbox2_fifo *fifo =
  86. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  87. mbox_write_reg(msg, fifo->msg);
  88. }
  89. static int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
  90. {
  91. struct omap_mbox2_fifo *fifo =
  92. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  93. return (mbox_read_reg(fifo->msg_stat) == 0);
  94. }
  95. static int omap2_mbox_fifo_full(struct omap_mbox *mbox)
  96. {
  97. struct omap_mbox2_fifo *fifo =
  98. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  99. return mbox_read_reg(fifo->fifo_stat);
  100. }
  101. /* Mailbox IRQ handle functions */
  102. static void omap2_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  103. {
  104. struct omap_mbox2_priv *p = mbox->priv;
  105. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  106. l = mbox_read_reg(p->irqenable);
  107. l |= bit;
  108. mbox_write_reg(l, p->irqenable);
  109. }
  110. static void omap2_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  111. {
  112. struct omap_mbox2_priv *p = mbox->priv;
  113. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  114. /*
  115. * Read and update the interrupt configuration register for pre-OMAP4.
  116. * OMAP4 and later SoCs have a dedicated interrupt disabling register.
  117. */
  118. if (!p->intr_type)
  119. bit = mbox_read_reg(p->irqdisable) & ~bit;
  120. mbox_write_reg(bit, p->irqdisable);
  121. }
  122. static void omap2_mbox_ack_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  123. {
  124. struct omap_mbox2_priv *p = mbox->priv;
  125. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  126. mbox_write_reg(bit, p->irqstatus);
  127. /* Flush posted write for irq status to avoid spurious interrupts */
  128. mbox_read_reg(p->irqstatus);
  129. }
  130. static int omap2_mbox_is_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  131. {
  132. struct omap_mbox2_priv *p = mbox->priv;
  133. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  134. u32 enable = mbox_read_reg(p->irqenable);
  135. u32 status = mbox_read_reg(p->irqstatus);
  136. return (int)(enable & status & bit);
  137. }
  138. static void omap2_mbox_save_ctx(struct omap_mbox *mbox)
  139. {
  140. int i;
  141. struct omap_mbox2_priv *p = mbox->priv;
  142. int nr_regs;
  143. if (p->intr_type)
  144. nr_regs = OMAP4_MBOX_NR_REGS;
  145. else
  146. nr_regs = MBOX_NR_REGS;
  147. for (i = 0; i < nr_regs; i++) {
  148. p->ctx[i] = mbox_read_reg(i * sizeof(u32));
  149. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  150. i, p->ctx[i]);
  151. }
  152. }
  153. static void omap2_mbox_restore_ctx(struct omap_mbox *mbox)
  154. {
  155. int i;
  156. struct omap_mbox2_priv *p = mbox->priv;
  157. int nr_regs;
  158. if (p->intr_type)
  159. nr_regs = OMAP4_MBOX_NR_REGS;
  160. else
  161. nr_regs = MBOX_NR_REGS;
  162. for (i = 0; i < nr_regs; i++) {
  163. mbox_write_reg(p->ctx[i], i * sizeof(u32));
  164. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  165. i, p->ctx[i]);
  166. }
  167. }
  168. static struct omap_mbox_ops omap2_mbox_ops = {
  169. .type = OMAP_MBOX_TYPE2,
  170. .startup = omap2_mbox_startup,
  171. .shutdown = omap2_mbox_shutdown,
  172. .fifo_read = omap2_mbox_fifo_read,
  173. .fifo_write = omap2_mbox_fifo_write,
  174. .fifo_empty = omap2_mbox_fifo_empty,
  175. .fifo_full = omap2_mbox_fifo_full,
  176. .enable_irq = omap2_mbox_enable_irq,
  177. .disable_irq = omap2_mbox_disable_irq,
  178. .ack_irq = omap2_mbox_ack_irq,
  179. .is_irq = omap2_mbox_is_irq,
  180. .save_ctx = omap2_mbox_save_ctx,
  181. .restore_ctx = omap2_mbox_restore_ctx,
  182. };
  183. static int omap2_mbox_probe(struct platform_device *pdev)
  184. {
  185. struct resource *mem;
  186. int ret;
  187. struct omap_mbox **list, *mbox, *mboxblk;
  188. struct omap_mbox2_priv *priv, *privblk;
  189. struct omap_mbox_pdata *pdata = pdev->dev.platform_data;
  190. struct omap_mbox_dev_info *info;
  191. int i;
  192. if (!pdata || !pdata->info_cnt || !pdata->info) {
  193. pr_err("%s: platform not supported\n", __func__);
  194. return -ENODEV;
  195. }
  196. /* allocate one extra for marking end of list */
  197. list = kzalloc((pdata->info_cnt + 1) * sizeof(*list), GFP_KERNEL);
  198. if (!list)
  199. return -ENOMEM;
  200. mboxblk = mbox = kzalloc(pdata->info_cnt * sizeof(*mbox), GFP_KERNEL);
  201. if (!mboxblk) {
  202. ret = -ENOMEM;
  203. goto free_list;
  204. }
  205. privblk = priv = kzalloc(pdata->info_cnt * sizeof(*priv), GFP_KERNEL);
  206. if (!privblk) {
  207. ret = -ENOMEM;
  208. goto free_mboxblk;
  209. }
  210. info = pdata->info;
  211. for (i = 0; i < pdata->info_cnt; i++, info++, priv++) {
  212. priv->tx_fifo.msg = MAILBOX_MESSAGE(info->tx_id);
  213. priv->tx_fifo.fifo_stat = MAILBOX_FIFOSTATUS(info->tx_id);
  214. priv->rx_fifo.msg = MAILBOX_MESSAGE(info->rx_id);
  215. priv->rx_fifo.msg_stat = MAILBOX_MSGSTATUS(info->rx_id);
  216. priv->notfull_bit = MAILBOX_IRQ_NOTFULL(info->tx_id);
  217. priv->newmsg_bit = MAILBOX_IRQ_NEWMSG(info->rx_id);
  218. if (pdata->intr_type) {
  219. priv->irqenable = OMAP4_MAILBOX_IRQENABLE(info->usr_id);
  220. priv->irqstatus = OMAP4_MAILBOX_IRQSTATUS(info->usr_id);
  221. priv->irqdisable =
  222. OMAP4_MAILBOX_IRQENABLE_CLR(info->usr_id);
  223. } else {
  224. priv->irqenable = MAILBOX_IRQENABLE(info->usr_id);
  225. priv->irqstatus = MAILBOX_IRQSTATUS(info->usr_id);
  226. priv->irqdisable = MAILBOX_IRQENABLE(info->usr_id);
  227. }
  228. priv->intr_type = pdata->intr_type;
  229. mbox->priv = priv;
  230. mbox->name = info->name;
  231. mbox->ops = &omap2_mbox_ops;
  232. mbox->irq = platform_get_irq(pdev, info->irq_id);
  233. if (mbox->irq < 0) {
  234. ret = mbox->irq;
  235. goto free_privblk;
  236. }
  237. list[i] = mbox++;
  238. }
  239. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  240. if (!mem) {
  241. ret = -ENOENT;
  242. goto free_privblk;
  243. }
  244. mbox_base = ioremap(mem->start, resource_size(mem));
  245. if (!mbox_base) {
  246. ret = -ENOMEM;
  247. goto free_privblk;
  248. }
  249. ret = omap_mbox_register(&pdev->dev, list);
  250. if (ret)
  251. goto unmap_mbox;
  252. platform_set_drvdata(pdev, list);
  253. return 0;
  254. unmap_mbox:
  255. iounmap(mbox_base);
  256. free_privblk:
  257. kfree(privblk);
  258. free_mboxblk:
  259. kfree(mboxblk);
  260. free_list:
  261. kfree(list);
  262. return ret;
  263. }
  264. static int omap2_mbox_remove(struct platform_device *pdev)
  265. {
  266. struct omap_mbox2_priv *privblk;
  267. struct omap_mbox **list = platform_get_drvdata(pdev);
  268. struct omap_mbox *mboxblk = list[0];
  269. privblk = mboxblk->priv;
  270. omap_mbox_unregister();
  271. iounmap(mbox_base);
  272. kfree(privblk);
  273. kfree(mboxblk);
  274. kfree(list);
  275. return 0;
  276. }
  277. static struct platform_driver omap2_mbox_driver = {
  278. .probe = omap2_mbox_probe,
  279. .remove = omap2_mbox_remove,
  280. .driver = {
  281. .name = "omap-mailbox",
  282. },
  283. };
  284. static int __init omap2_mbox_init(void)
  285. {
  286. return platform_driver_register(&omap2_mbox_driver);
  287. }
  288. static void __exit omap2_mbox_exit(void)
  289. {
  290. platform_driver_unregister(&omap2_mbox_driver);
  291. }
  292. module_init(omap2_mbox_init);
  293. module_exit(omap2_mbox_exit);
  294. MODULE_LICENSE("GPL v2");
  295. MODULE_DESCRIPTION("omap mailbox: omap2/3/4 architecture specific functions");
  296. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  297. MODULE_AUTHOR("Paul Mundt");
  298. MODULE_ALIAS("platform:omap2-mailbox");