mailbox.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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/kernel.h>
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <mach/mailbox.h>
  18. #include <mach/irqs.h>
  19. #define MAILBOX_REVISION 0x000
  20. #define MAILBOX_SYSCONFIG 0x010
  21. #define MAILBOX_SYSSTATUS 0x014
  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 MAILBOX_IRQ_NEWMSG(u) (1 << (2 * (u)))
  28. #define MAILBOX_IRQ_NOTFULL(u) (1 << (2 * (u) + 1))
  29. static void __iomem *mbox_base;
  30. struct omap_mbox2_fifo {
  31. unsigned long msg;
  32. unsigned long fifo_stat;
  33. unsigned long msg_stat;
  34. };
  35. struct omap_mbox2_priv {
  36. struct omap_mbox2_fifo tx_fifo;
  37. struct omap_mbox2_fifo rx_fifo;
  38. unsigned long irqenable;
  39. unsigned long irqstatus;
  40. u32 newmsg_bit;
  41. u32 notfull_bit;
  42. };
  43. static struct clk *mbox_ick_handle;
  44. static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  45. omap_mbox_type_t irq);
  46. static inline unsigned int mbox_read_reg(size_t ofs)
  47. {
  48. return __raw_readl(mbox_base + ofs);
  49. }
  50. static inline void mbox_write_reg(u32 val, size_t ofs)
  51. {
  52. __raw_writel(val, mbox_base + ofs);
  53. }
  54. /* Mailbox H/W preparations */
  55. static int omap2_mbox_startup(struct omap_mbox *mbox)
  56. {
  57. unsigned int l;
  58. mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
  59. if (IS_ERR(mbox_ick_handle)) {
  60. printk("Could not get mailboxes_ick\n");
  61. return -ENODEV;
  62. }
  63. clk_enable(mbox_ick_handle);
  64. /* set smart-idle & autoidle */
  65. l = mbox_read_reg(MAILBOX_SYSCONFIG);
  66. l |= 0x00000011;
  67. mbox_write_reg(l, MAILBOX_SYSCONFIG);
  68. omap2_mbox_enable_irq(mbox, IRQ_RX);
  69. return 0;
  70. }
  71. static void omap2_mbox_shutdown(struct omap_mbox *mbox)
  72. {
  73. clk_disable(mbox_ick_handle);
  74. clk_put(mbox_ick_handle);
  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,
  103. omap_mbox_type_t irq)
  104. {
  105. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  106. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  107. l = mbox_read_reg(p->irqenable);
  108. l |= bit;
  109. mbox_write_reg(l, p->irqenable);
  110. }
  111. static void omap2_mbox_disable_irq(struct omap_mbox *mbox,
  112. omap_mbox_type_t irq)
  113. {
  114. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  115. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  116. l = mbox_read_reg(p->irqenable);
  117. l &= ~bit;
  118. mbox_write_reg(l, p->irqenable);
  119. }
  120. static void omap2_mbox_ack_irq(struct omap_mbox *mbox,
  121. omap_mbox_type_t irq)
  122. {
  123. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  124. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  125. mbox_write_reg(bit, p->irqstatus);
  126. }
  127. static int omap2_mbox_is_irq(struct omap_mbox *mbox,
  128. omap_mbox_type_t irq)
  129. {
  130. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  131. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  132. u32 enable = mbox_read_reg(p->irqenable);
  133. u32 status = mbox_read_reg(p->irqstatus);
  134. return (enable & status & bit);
  135. }
  136. static struct omap_mbox_ops omap2_mbox_ops = {
  137. .type = OMAP_MBOX_TYPE2,
  138. .startup = omap2_mbox_startup,
  139. .shutdown = omap2_mbox_shutdown,
  140. .fifo_read = omap2_mbox_fifo_read,
  141. .fifo_write = omap2_mbox_fifo_write,
  142. .fifo_empty = omap2_mbox_fifo_empty,
  143. .fifo_full = omap2_mbox_fifo_full,
  144. .enable_irq = omap2_mbox_enable_irq,
  145. .disable_irq = omap2_mbox_disable_irq,
  146. .ack_irq = omap2_mbox_ack_irq,
  147. .is_irq = omap2_mbox_is_irq,
  148. };
  149. /*
  150. * MAILBOX 0: ARM -> DSP,
  151. * MAILBOX 1: ARM <- DSP.
  152. * MAILBOX 2: ARM -> IVA,
  153. * MAILBOX 3: ARM <- IVA.
  154. */
  155. /* FIXME: the following structs should be filled automatically by the user id */
  156. /* DSP */
  157. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  158. .tx_fifo = {
  159. .msg = MAILBOX_MESSAGE(0),
  160. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  161. },
  162. .rx_fifo = {
  163. .msg = MAILBOX_MESSAGE(1),
  164. .msg_stat = MAILBOX_MSGSTATUS(1),
  165. },
  166. .irqenable = MAILBOX_IRQENABLE(0),
  167. .irqstatus = MAILBOX_IRQSTATUS(0),
  168. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  169. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  170. };
  171. struct omap_mbox mbox_dsp_info = {
  172. .name = "dsp",
  173. .ops = &omap2_mbox_ops,
  174. .priv = &omap2_mbox_dsp_priv,
  175. };
  176. EXPORT_SYMBOL(mbox_dsp_info);
  177. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  178. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  179. .tx_fifo = {
  180. .msg = MAILBOX_MESSAGE(2),
  181. .fifo_stat = MAILBOX_FIFOSTATUS(2),
  182. },
  183. .rx_fifo = {
  184. .msg = MAILBOX_MESSAGE(3),
  185. .msg_stat = MAILBOX_MSGSTATUS(3),
  186. },
  187. .irqenable = MAILBOX_IRQENABLE(3),
  188. .irqstatus = MAILBOX_IRQSTATUS(3),
  189. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  190. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  191. };
  192. static struct omap_mbox mbox_iva_info = {
  193. .name = "iva",
  194. .ops = &omap2_mbox_ops,
  195. .priv = &omap2_mbox_iva_priv,
  196. };
  197. #endif
  198. static int __init omap2_mbox_probe(struct platform_device *pdev)
  199. {
  200. struct resource *res;
  201. int ret;
  202. /* MBOX base */
  203. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  204. if (unlikely(!res)) {
  205. dev_err(&pdev->dev, "invalid mem resource\n");
  206. return -ENODEV;
  207. }
  208. mbox_base = ioremap(res->start, res->end - res->start);
  209. if (!mbox_base)
  210. return -ENOMEM;
  211. /* DSP or IVA2 IRQ */
  212. mbox_dsp_info.irq = platform_get_irq(pdev, 0);
  213. if (mbox_dsp_info.irq < 0) {
  214. dev_err(&pdev->dev, "invalid irq resource\n");
  215. ret = -ENODEV;
  216. goto err_dsp;
  217. }
  218. ret = omap_mbox_register(&mbox_dsp_info);
  219. if (ret)
  220. goto err_dsp;
  221. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  222. if (cpu_is_omap2420()) {
  223. /* IVA IRQ */
  224. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  225. if (unlikely(!res)) {
  226. dev_err(&pdev->dev, "invalid irq resource\n");
  227. ret = -ENODEV;
  228. goto err_iva1;
  229. }
  230. mbox_iva_info.irq = res->start;
  231. ret = omap_mbox_register(&mbox_iva_info);
  232. if (ret)
  233. goto err_iva1;
  234. }
  235. #endif
  236. return 0;
  237. err_iva1:
  238. omap_mbox_unregister(&mbox_dsp_info);
  239. err_dsp:
  240. iounmap(mbox_base);
  241. return ret;
  242. }
  243. static int omap2_mbox_remove(struct platform_device *pdev)
  244. {
  245. #if defined(CONFIG_ARCH_OMAP2420)
  246. omap_mbox_unregister(&mbox_iva_info);
  247. #endif
  248. omap_mbox_unregister(&mbox_dsp_info);
  249. iounmap(mbox_base);
  250. return 0;
  251. }
  252. static struct platform_driver omap2_mbox_driver = {
  253. .probe = omap2_mbox_probe,
  254. .remove = omap2_mbox_remove,
  255. .driver = {
  256. .name = "mailbox",
  257. },
  258. };
  259. static int __init omap2_mbox_init(void)
  260. {
  261. return platform_driver_register(&omap2_mbox_driver);
  262. }
  263. static void __exit omap2_mbox_exit(void)
  264. {
  265. platform_driver_unregister(&omap2_mbox_driver);
  266. }
  267. module_init(omap2_mbox_init);
  268. module_exit(omap2_mbox_exit);
  269. MODULE_LICENSE("GPL v2");
  270. MODULE_DESCRIPTION("omap mailbox: omap2/3 architecture specific functions");
  271. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>, Paul Mundt");