mailbox.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. /* SYSCONFIG: register bit definition */
  30. #define AUTOIDLE (1 << 0)
  31. #define SOFTRESET (1 << 1)
  32. #define SMARTIDLE (2 << 3)
  33. /* SYSSTATUS: register bit definition */
  34. #define RESETDONE (1 << 0)
  35. #define MBOX_REG_SIZE 0x120
  36. #define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
  37. static void __iomem *mbox_base;
  38. struct omap_mbox2_fifo {
  39. unsigned long msg;
  40. unsigned long fifo_stat;
  41. unsigned long msg_stat;
  42. };
  43. struct omap_mbox2_priv {
  44. struct omap_mbox2_fifo tx_fifo;
  45. struct omap_mbox2_fifo rx_fifo;
  46. unsigned long irqenable;
  47. unsigned long irqstatus;
  48. u32 newmsg_bit;
  49. u32 notfull_bit;
  50. u32 ctx[MBOX_NR_REGS];
  51. };
  52. static struct clk *mbox_ick_handle;
  53. static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  54. omap_mbox_type_t irq);
  55. static inline unsigned int mbox_read_reg(size_t ofs)
  56. {
  57. return __raw_readl(mbox_base + ofs);
  58. }
  59. static inline void mbox_write_reg(u32 val, size_t ofs)
  60. {
  61. __raw_writel(val, mbox_base + ofs);
  62. }
  63. /* Mailbox H/W preparations */
  64. static int omap2_mbox_startup(struct omap_mbox *mbox)
  65. {
  66. u32 l;
  67. unsigned long timeout;
  68. mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
  69. if (IS_ERR(mbox_ick_handle)) {
  70. pr_err("Can't get mailboxes_ick\n");
  71. return -ENODEV;
  72. }
  73. clk_enable(mbox_ick_handle);
  74. mbox_write_reg(SOFTRESET, MAILBOX_SYSCONFIG);
  75. timeout = jiffies + msecs_to_jiffies(20);
  76. do {
  77. l = mbox_read_reg(MAILBOX_SYSSTATUS);
  78. if (l & RESETDONE)
  79. break;
  80. } while (time_after(jiffies, timeout));
  81. if (!(l & RESETDONE)) {
  82. pr_err("Can't take mmu out of reset\n");
  83. return -ENODEV;
  84. }
  85. l = mbox_read_reg(MAILBOX_REVISION);
  86. pr_info("omap mailbox rev %d.%d\n", (l & 0xf0) >> 4, (l & 0x0f));
  87. l = SMARTIDLE | AUTOIDLE;
  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 void omap2_mbox_save_ctx(struct omap_mbox *mbox)
  158. {
  159. int i;
  160. struct omap_mbox2_priv *p = mbox->priv;
  161. for (i = 0; i < MBOX_NR_REGS; i++) {
  162. p->ctx[i] = mbox_read_reg(i * sizeof(u32));
  163. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  164. i, p->ctx[i]);
  165. }
  166. }
  167. static void omap2_mbox_restore_ctx(struct omap_mbox *mbox)
  168. {
  169. int i;
  170. struct omap_mbox2_priv *p = mbox->priv;
  171. for (i = 0; i < MBOX_NR_REGS; i++) {
  172. mbox_write_reg(p->ctx[i], i * sizeof(u32));
  173. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  174. i, p->ctx[i]);
  175. }
  176. }
  177. static struct omap_mbox_ops omap2_mbox_ops = {
  178. .type = OMAP_MBOX_TYPE2,
  179. .startup = omap2_mbox_startup,
  180. .shutdown = omap2_mbox_shutdown,
  181. .fifo_read = omap2_mbox_fifo_read,
  182. .fifo_write = omap2_mbox_fifo_write,
  183. .fifo_empty = omap2_mbox_fifo_empty,
  184. .fifo_full = omap2_mbox_fifo_full,
  185. .enable_irq = omap2_mbox_enable_irq,
  186. .disable_irq = omap2_mbox_disable_irq,
  187. .ack_irq = omap2_mbox_ack_irq,
  188. .is_irq = omap2_mbox_is_irq,
  189. .save_ctx = omap2_mbox_save_ctx,
  190. .restore_ctx = omap2_mbox_restore_ctx,
  191. };
  192. /*
  193. * MAILBOX 0: ARM -> DSP,
  194. * MAILBOX 1: ARM <- DSP.
  195. * MAILBOX 2: ARM -> IVA,
  196. * MAILBOX 3: ARM <- IVA.
  197. */
  198. /* FIXME: the following structs should be filled automatically by the user id */
  199. /* DSP */
  200. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  201. .tx_fifo = {
  202. .msg = MAILBOX_MESSAGE(0),
  203. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  204. },
  205. .rx_fifo = {
  206. .msg = MAILBOX_MESSAGE(1),
  207. .msg_stat = MAILBOX_MSGSTATUS(1),
  208. },
  209. .irqenable = MAILBOX_IRQENABLE(0),
  210. .irqstatus = MAILBOX_IRQSTATUS(0),
  211. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  212. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  213. };
  214. struct omap_mbox mbox_dsp_info = {
  215. .name = "dsp",
  216. .ops = &omap2_mbox_ops,
  217. .priv = &omap2_mbox_dsp_priv,
  218. };
  219. EXPORT_SYMBOL(mbox_dsp_info);
  220. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  221. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  222. .tx_fifo = {
  223. .msg = MAILBOX_MESSAGE(2),
  224. .fifo_stat = MAILBOX_FIFOSTATUS(2),
  225. },
  226. .rx_fifo = {
  227. .msg = MAILBOX_MESSAGE(3),
  228. .msg_stat = MAILBOX_MSGSTATUS(3),
  229. },
  230. .irqenable = MAILBOX_IRQENABLE(3),
  231. .irqstatus = MAILBOX_IRQSTATUS(3),
  232. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  233. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  234. };
  235. static struct omap_mbox mbox_iva_info = {
  236. .name = "iva",
  237. .ops = &omap2_mbox_ops,
  238. .priv = &omap2_mbox_iva_priv,
  239. };
  240. #endif
  241. static int __devinit omap2_mbox_probe(struct platform_device *pdev)
  242. {
  243. struct resource *res;
  244. int ret;
  245. /* MBOX base */
  246. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  247. if (unlikely(!res)) {
  248. dev_err(&pdev->dev, "invalid mem resource\n");
  249. return -ENODEV;
  250. }
  251. mbox_base = ioremap(res->start, res->end - res->start);
  252. if (!mbox_base)
  253. return -ENOMEM;
  254. /* DSP or IVA2 IRQ */
  255. ret = platform_get_irq(pdev, 0);
  256. if (ret < 0) {
  257. dev_err(&pdev->dev, "invalid irq resource\n");
  258. goto err_dsp;
  259. }
  260. mbox_dsp_info.irq = ret;
  261. ret = omap_mbox_register(&pdev->dev, &mbox_dsp_info);
  262. if (ret)
  263. goto err_dsp;
  264. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  265. if (cpu_is_omap2420()) {
  266. /* IVA IRQ */
  267. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  268. if (unlikely(!res)) {
  269. dev_err(&pdev->dev, "invalid irq resource\n");
  270. ret = -ENODEV;
  271. goto err_iva1;
  272. }
  273. mbox_iva_info.irq = res->start;
  274. ret = omap_mbox_register(&pdev->dev, &mbox_iva_info);
  275. if (ret)
  276. goto err_iva1;
  277. }
  278. #endif
  279. return 0;
  280. err_iva1:
  281. omap_mbox_unregister(&mbox_dsp_info);
  282. err_dsp:
  283. iounmap(mbox_base);
  284. return ret;
  285. }
  286. static int __devexit omap2_mbox_remove(struct platform_device *pdev)
  287. {
  288. #if defined(CONFIG_ARCH_OMAP2420)
  289. omap_mbox_unregister(&mbox_iva_info);
  290. #endif
  291. omap_mbox_unregister(&mbox_dsp_info);
  292. iounmap(mbox_base);
  293. return 0;
  294. }
  295. static struct platform_driver omap2_mbox_driver = {
  296. .probe = omap2_mbox_probe,
  297. .remove = __devexit_p(omap2_mbox_remove),
  298. .driver = {
  299. .name = "omap2-mailbox",
  300. },
  301. };
  302. static int __init omap2_mbox_init(void)
  303. {
  304. return platform_driver_register(&omap2_mbox_driver);
  305. }
  306. static void __exit omap2_mbox_exit(void)
  307. {
  308. platform_driver_unregister(&omap2_mbox_driver);
  309. }
  310. module_init(omap2_mbox_init);
  311. module_exit(omap2_mbox_exit);
  312. MODULE_LICENSE("GPL v2");
  313. MODULE_DESCRIPTION("omap mailbox: omap2/3 architecture specific functions");
  314. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>, Paul Mundt");
  315. MODULE_ALIAS("platform:omap2-mailbox");