mailbox.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Mailbox reservation modules for OMAP2
  3. *
  4. * Copyright (C) 2006 Nokia Corporation
  5. * Written by: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  6. * and Paul Mundt <paul.mundt@nokia.com>
  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 <asm/arch/mailbox.h>
  17. #include <asm/arch/irqs.h>
  18. #include <asm/io.h>
  19. #define MAILBOX_REVISION 0x00
  20. #define MAILBOX_SYSCONFIG 0x10
  21. #define MAILBOX_SYSSTATUS 0x14
  22. #define MAILBOX_MESSAGE_0 0x40
  23. #define MAILBOX_MESSAGE_1 0x44
  24. #define MAILBOX_MESSAGE_2 0x48
  25. #define MAILBOX_MESSAGE_3 0x4c
  26. #define MAILBOX_MESSAGE_4 0x50
  27. #define MAILBOX_MESSAGE_5 0x54
  28. #define MAILBOX_FIFOSTATUS_0 0x80
  29. #define MAILBOX_FIFOSTATUS_1 0x84
  30. #define MAILBOX_FIFOSTATUS_2 0x88
  31. #define MAILBOX_FIFOSTATUS_3 0x8c
  32. #define MAILBOX_FIFOSTATUS_4 0x90
  33. #define MAILBOX_FIFOSTATUS_5 0x94
  34. #define MAILBOX_MSGSTATUS_0 0xc0
  35. #define MAILBOX_MSGSTATUS_1 0xc4
  36. #define MAILBOX_MSGSTATUS_2 0xc8
  37. #define MAILBOX_MSGSTATUS_3 0xcc
  38. #define MAILBOX_MSGSTATUS_4 0xd0
  39. #define MAILBOX_MSGSTATUS_5 0xd4
  40. #define MAILBOX_IRQSTATUS_0 0x100
  41. #define MAILBOX_IRQENABLE_0 0x104
  42. #define MAILBOX_IRQSTATUS_1 0x108
  43. #define MAILBOX_IRQENABLE_1 0x10c
  44. #define MAILBOX_IRQSTATUS_2 0x110
  45. #define MAILBOX_IRQENABLE_2 0x114
  46. #define MAILBOX_IRQSTATUS_3 0x118
  47. #define MAILBOX_IRQENABLE_3 0x11c
  48. static unsigned long mbox_base;
  49. #define MAILBOX_IRQ_NOTFULL(n) (1 << (2 * (n) + 1))
  50. #define MAILBOX_IRQ_NEWMSG(n) (1 << (2 * (n)))
  51. struct omap_mbox2_fifo {
  52. unsigned long msg;
  53. unsigned long fifo_stat;
  54. unsigned long msg_stat;
  55. };
  56. struct omap_mbox2_priv {
  57. struct omap_mbox2_fifo tx_fifo;
  58. struct omap_mbox2_fifo rx_fifo;
  59. unsigned long irqenable;
  60. unsigned long irqstatus;
  61. u32 newmsg_bit;
  62. u32 notfull_bit;
  63. };
  64. static struct clk *mbox_ick_handle;
  65. static inline unsigned int mbox_read_reg(unsigned int reg)
  66. {
  67. return __raw_readl(mbox_base + reg);
  68. }
  69. static inline void mbox_write_reg(unsigned int val, unsigned int reg)
  70. {
  71. __raw_writel(val, mbox_base + reg);
  72. }
  73. /* Mailbox H/W preparations */
  74. static inline int omap2_mbox_startup(struct omap_mbox *mbox)
  75. {
  76. unsigned int l;
  77. mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
  78. if (IS_ERR(mbox_ick_handle)) {
  79. printk("Could not get mailboxes_ick\n");
  80. return -ENODEV;
  81. }
  82. clk_enable(mbox_ick_handle);
  83. /* set smart-idle & autoidle */
  84. l = mbox_read_reg(MAILBOX_SYSCONFIG);
  85. l |= 0x00000011;
  86. mbox_write_reg(l, MAILBOX_SYSCONFIG);
  87. return 0;
  88. }
  89. static inline void omap2_mbox_shutdown(struct omap_mbox *mbox)
  90. {
  91. clk_disable(mbox_ick_handle);
  92. clk_put(mbox_ick_handle);
  93. }
  94. /* Mailbox FIFO handle functions */
  95. static inline mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
  96. {
  97. struct omap_mbox2_fifo *fifo =
  98. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  99. return (mbox_msg_t) mbox_read_reg(fifo->msg);
  100. }
  101. static inline void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  102. {
  103. struct omap_mbox2_fifo *fifo =
  104. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  105. mbox_write_reg(msg, fifo->msg);
  106. }
  107. static inline int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
  108. {
  109. struct omap_mbox2_fifo *fifo =
  110. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  111. return (mbox_read_reg(fifo->msg_stat) == 0);
  112. }
  113. static inline int omap2_mbox_fifo_full(struct omap_mbox *mbox)
  114. {
  115. struct omap_mbox2_fifo *fifo =
  116. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  117. return (mbox_read_reg(fifo->fifo_stat));
  118. }
  119. /* Mailbox IRQ handle functions */
  120. static inline void omap2_mbox_enable_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 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  125. l = mbox_read_reg(p->irqenable);
  126. l |= bit;
  127. mbox_write_reg(l, p->irqenable);
  128. }
  129. static inline void omap2_mbox_disable_irq(struct omap_mbox *mbox,
  130. omap_mbox_type_t irq)
  131. {
  132. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  133. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  134. l = mbox_read_reg(p->irqenable);
  135. l &= ~bit;
  136. mbox_write_reg(l, p->irqenable);
  137. }
  138. static inline void omap2_mbox_ack_irq(struct omap_mbox *mbox,
  139. omap_mbox_type_t irq)
  140. {
  141. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  142. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  143. mbox_write_reg(bit, p->irqstatus);
  144. }
  145. static inline int omap2_mbox_is_irq(struct omap_mbox *mbox,
  146. omap_mbox_type_t irq)
  147. {
  148. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  149. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  150. u32 enable = mbox_read_reg(p->irqenable);
  151. u32 status = mbox_read_reg(p->irqstatus);
  152. return (enable & status & bit);
  153. }
  154. static struct omap_mbox_ops omap2_mbox_ops = {
  155. .type = OMAP_MBOX_TYPE2,
  156. .startup = omap2_mbox_startup,
  157. .shutdown = omap2_mbox_shutdown,
  158. .fifo_read = omap2_mbox_fifo_read,
  159. .fifo_write = omap2_mbox_fifo_write,
  160. .fifo_empty = omap2_mbox_fifo_empty,
  161. .fifo_full = omap2_mbox_fifo_full,
  162. .enable_irq = omap2_mbox_enable_irq,
  163. .disable_irq = omap2_mbox_disable_irq,
  164. .ack_irq = omap2_mbox_ack_irq,
  165. .is_irq = omap2_mbox_is_irq,
  166. };
  167. /*
  168. * MAILBOX 0: ARM -> DSP,
  169. * MAILBOX 1: ARM <- DSP.
  170. * MAILBOX 2: ARM -> IVA,
  171. * MAILBOX 3: ARM <- IVA.
  172. */
  173. /* FIXME: the following structs should be filled automatically by the user id */
  174. /* DSP */
  175. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  176. .tx_fifo = {
  177. .msg = MAILBOX_MESSAGE_0,
  178. .fifo_stat = MAILBOX_FIFOSTATUS_0,
  179. },
  180. .rx_fifo = {
  181. .msg = MAILBOX_MESSAGE_1,
  182. .msg_stat = MAILBOX_MSGSTATUS_1,
  183. },
  184. .irqenable = MAILBOX_IRQENABLE_0,
  185. .irqstatus = MAILBOX_IRQSTATUS_0,
  186. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  187. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  188. };
  189. struct omap_mbox mbox_dsp_info = {
  190. .name = "dsp",
  191. .ops = &omap2_mbox_ops,
  192. .priv = &omap2_mbox_dsp_priv,
  193. };
  194. EXPORT_SYMBOL(mbox_dsp_info);
  195. /* IVA */
  196. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  197. .tx_fifo = {
  198. .msg = MAILBOX_MESSAGE_2,
  199. .fifo_stat = MAILBOX_FIFOSTATUS_2,
  200. },
  201. .rx_fifo = {
  202. .msg = MAILBOX_MESSAGE_3,
  203. .msg_stat = MAILBOX_MSGSTATUS_3,
  204. },
  205. .irqenable = MAILBOX_IRQENABLE_3,
  206. .irqstatus = MAILBOX_IRQSTATUS_3,
  207. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  208. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  209. };
  210. static struct omap_mbox mbox_iva_info = {
  211. .name = "iva",
  212. .ops = &omap2_mbox_ops,
  213. .priv = &omap2_mbox_iva_priv,
  214. };
  215. static int __init omap2_mbox_probe(struct platform_device *pdev)
  216. {
  217. struct resource *res;
  218. int ret = 0;
  219. if (pdev->num_resources != 3) {
  220. dev_err(&pdev->dev, "invalid number of resources: %d\n",
  221. pdev->num_resources);
  222. return -ENODEV;
  223. }
  224. /* MBOX base */
  225. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  226. if (unlikely(!res)) {
  227. dev_err(&pdev->dev, "invalid mem resource\n");
  228. return -ENODEV;
  229. }
  230. mbox_base = res->start;
  231. /* DSP IRQ */
  232. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  233. if (unlikely(!res)) {
  234. dev_err(&pdev->dev, "invalid irq resource\n");
  235. return -ENODEV;
  236. }
  237. mbox_dsp_info.irq = res->start;
  238. ret = omap_mbox_register(&mbox_dsp_info);
  239. /* IVA IRQ */
  240. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  241. if (unlikely(!res)) {
  242. dev_err(&pdev->dev, "invalid irq resource\n");
  243. return -ENODEV;
  244. }
  245. mbox_iva_info.irq = res->start;
  246. ret = omap_mbox_register(&mbox_iva_info);
  247. return ret;
  248. }
  249. static int omap2_mbox_remove(struct platform_device *pdev)
  250. {
  251. omap_mbox_unregister(&mbox_dsp_info);
  252. return 0;
  253. }
  254. static struct platform_driver omap2_mbox_driver = {
  255. .probe = omap2_mbox_probe,
  256. .remove = omap2_mbox_remove,
  257. .driver = {
  258. .name = "mailbox",
  259. },
  260. };
  261. static int __init omap2_mbox_init(void)
  262. {
  263. return platform_driver_register(&omap2_mbox_driver);
  264. }
  265. static void __exit omap2_mbox_exit(void)
  266. {
  267. platform_driver_unregister(&omap2_mbox_driver);
  268. }
  269. module_init(omap2_mbox_init);
  270. module_exit(omap2_mbox_exit);
  271. MODULE_LICENSE("GPL");