mailbox.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 <linux/io.h>
  17. #include <mach/mailbox.h>
  18. #include <mach/irqs.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 void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  66. omap_mbox_type_t irq);
  67. static inline unsigned int mbox_read_reg(unsigned int reg)
  68. {
  69. return __raw_readl(mbox_base + reg);
  70. }
  71. static inline void mbox_write_reg(unsigned int val, unsigned int reg)
  72. {
  73. __raw_writel(val, mbox_base + reg);
  74. }
  75. /* Mailbox H/W preparations */
  76. static int omap2_mbox_startup(struct omap_mbox *mbox)
  77. {
  78. unsigned int l;
  79. mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
  80. if (IS_ERR(mbox_ick_handle)) {
  81. printk("Could not get mailboxes_ick\n");
  82. return -ENODEV;
  83. }
  84. clk_enable(mbox_ick_handle);
  85. /* set smart-idle & autoidle */
  86. l = mbox_read_reg(MAILBOX_SYSCONFIG);
  87. l |= 0x00000011;
  88. mbox_write_reg(l, MAILBOX_SYSCONFIG);
  89. omap2_mbox_enable_irq(mbox, IRQ_RX);
  90. return 0;
  91. }
  92. static void omap2_mbox_shutdown(struct omap_mbox *mbox)
  93. {
  94. clk_disable(mbox_ick_handle);
  95. clk_put(mbox_ick_handle);
  96. }
  97. /* Mailbox FIFO handle functions */
  98. static mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
  99. {
  100. struct omap_mbox2_fifo *fifo =
  101. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  102. return (mbox_msg_t) mbox_read_reg(fifo->msg);
  103. }
  104. static void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  105. {
  106. struct omap_mbox2_fifo *fifo =
  107. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  108. mbox_write_reg(msg, fifo->msg);
  109. }
  110. static int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
  111. {
  112. struct omap_mbox2_fifo *fifo =
  113. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  114. return (mbox_read_reg(fifo->msg_stat) == 0);
  115. }
  116. static int omap2_mbox_fifo_full(struct omap_mbox *mbox)
  117. {
  118. struct omap_mbox2_fifo *fifo =
  119. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  120. return (mbox_read_reg(fifo->fifo_stat));
  121. }
  122. /* Mailbox IRQ handle functions */
  123. static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  124. omap_mbox_type_t irq)
  125. {
  126. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  127. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  128. l = mbox_read_reg(p->irqenable);
  129. l |= bit;
  130. mbox_write_reg(l, p->irqenable);
  131. }
  132. static void omap2_mbox_disable_irq(struct omap_mbox *mbox,
  133. omap_mbox_type_t irq)
  134. {
  135. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  136. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  137. l = mbox_read_reg(p->irqenable);
  138. l &= ~bit;
  139. mbox_write_reg(l, p->irqenable);
  140. }
  141. static void omap2_mbox_ack_irq(struct omap_mbox *mbox,
  142. omap_mbox_type_t irq)
  143. {
  144. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  145. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  146. mbox_write_reg(bit, p->irqstatus);
  147. }
  148. static int omap2_mbox_is_irq(struct omap_mbox *mbox,
  149. omap_mbox_type_t irq)
  150. {
  151. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  152. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  153. u32 enable = mbox_read_reg(p->irqenable);
  154. u32 status = mbox_read_reg(p->irqstatus);
  155. return (enable & status & bit);
  156. }
  157. static struct omap_mbox_ops omap2_mbox_ops = {
  158. .type = OMAP_MBOX_TYPE2,
  159. .startup = omap2_mbox_startup,
  160. .shutdown = omap2_mbox_shutdown,
  161. .fifo_read = omap2_mbox_fifo_read,
  162. .fifo_write = omap2_mbox_fifo_write,
  163. .fifo_empty = omap2_mbox_fifo_empty,
  164. .fifo_full = omap2_mbox_fifo_full,
  165. .enable_irq = omap2_mbox_enable_irq,
  166. .disable_irq = omap2_mbox_disable_irq,
  167. .ack_irq = omap2_mbox_ack_irq,
  168. .is_irq = omap2_mbox_is_irq,
  169. };
  170. /*
  171. * MAILBOX 0: ARM -> DSP,
  172. * MAILBOX 1: ARM <- DSP.
  173. * MAILBOX 2: ARM -> IVA,
  174. * MAILBOX 3: ARM <- IVA.
  175. */
  176. /* FIXME: the following structs should be filled automatically by the user id */
  177. /* DSP */
  178. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  179. .tx_fifo = {
  180. .msg = MAILBOX_MESSAGE_0,
  181. .fifo_stat = MAILBOX_FIFOSTATUS_0,
  182. },
  183. .rx_fifo = {
  184. .msg = MAILBOX_MESSAGE_1,
  185. .msg_stat = MAILBOX_MSGSTATUS_1,
  186. },
  187. .irqenable = MAILBOX_IRQENABLE_0,
  188. .irqstatus = MAILBOX_IRQSTATUS_0,
  189. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  190. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  191. };
  192. struct omap_mbox mbox_dsp_info = {
  193. .name = "dsp",
  194. .ops = &omap2_mbox_ops,
  195. .priv = &omap2_mbox_dsp_priv,
  196. };
  197. EXPORT_SYMBOL(mbox_dsp_info);
  198. /* IVA */
  199. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  200. .tx_fifo = {
  201. .msg = MAILBOX_MESSAGE_2,
  202. .fifo_stat = MAILBOX_FIFOSTATUS_2,
  203. },
  204. .rx_fifo = {
  205. .msg = MAILBOX_MESSAGE_3,
  206. .msg_stat = MAILBOX_MSGSTATUS_3,
  207. },
  208. .irqenable = MAILBOX_IRQENABLE_3,
  209. .irqstatus = MAILBOX_IRQSTATUS_3,
  210. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  211. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  212. };
  213. static struct omap_mbox mbox_iva_info = {
  214. .name = "iva",
  215. .ops = &omap2_mbox_ops,
  216. .priv = &omap2_mbox_iva_priv,
  217. };
  218. static int __init omap2_mbox_probe(struct platform_device *pdev)
  219. {
  220. struct resource *res;
  221. int ret = 0;
  222. if (pdev->num_resources != 3) {
  223. dev_err(&pdev->dev, "invalid number of resources: %d\n",
  224. pdev->num_resources);
  225. return -ENODEV;
  226. }
  227. /* MBOX base */
  228. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  229. if (unlikely(!res)) {
  230. dev_err(&pdev->dev, "invalid mem resource\n");
  231. return -ENODEV;
  232. }
  233. mbox_base = res->start;
  234. /* DSP IRQ */
  235. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  236. if (unlikely(!res)) {
  237. dev_err(&pdev->dev, "invalid irq resource\n");
  238. return -ENODEV;
  239. }
  240. mbox_dsp_info.irq = res->start;
  241. ret = omap_mbox_register(&mbox_dsp_info);
  242. /* IVA IRQ */
  243. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  244. if (unlikely(!res)) {
  245. dev_err(&pdev->dev, "invalid irq resource\n");
  246. return -ENODEV;
  247. }
  248. mbox_iva_info.irq = res->start;
  249. ret = omap_mbox_register(&mbox_iva_info);
  250. return ret;
  251. }
  252. static int omap2_mbox_remove(struct platform_device *pdev)
  253. {
  254. omap_mbox_unregister(&mbox_dsp_info);
  255. return 0;
  256. }
  257. static struct platform_driver omap2_mbox_driver = {
  258. .probe = omap2_mbox_probe,
  259. .remove = omap2_mbox_remove,
  260. .driver = {
  261. .name = "mailbox",
  262. },
  263. };
  264. static int __init omap2_mbox_init(void)
  265. {
  266. return platform_driver_register(&omap2_mbox_driver);
  267. }
  268. static void __exit omap2_mbox_exit(void)
  269. {
  270. platform_driver_unregister(&omap2_mbox_driver);
  271. }
  272. module_init(omap2_mbox_init);
  273. module_exit(omap2_mbox_exit);
  274. MODULE_LICENSE("GPL");