mailbox.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. /* Flush posted write for irq status to avoid spurious interrupts */
  148. mbox_read_reg(p->irqstatus);
  149. }
  150. static int omap2_mbox_is_irq(struct omap_mbox *mbox,
  151. omap_mbox_type_t irq)
  152. {
  153. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  154. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  155. u32 enable = mbox_read_reg(p->irqenable);
  156. u32 status = mbox_read_reg(p->irqstatus);
  157. return (enable & status & bit);
  158. }
  159. static void omap2_mbox_save_ctx(struct omap_mbox *mbox)
  160. {
  161. int i;
  162. struct omap_mbox2_priv *p = mbox->priv;
  163. for (i = 0; i < MBOX_NR_REGS; i++) {
  164. p->ctx[i] = mbox_read_reg(i * sizeof(u32));
  165. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  166. i, p->ctx[i]);
  167. }
  168. }
  169. static void omap2_mbox_restore_ctx(struct omap_mbox *mbox)
  170. {
  171. int i;
  172. struct omap_mbox2_priv *p = mbox->priv;
  173. for (i = 0; i < MBOX_NR_REGS; i++) {
  174. mbox_write_reg(p->ctx[i], i * sizeof(u32));
  175. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  176. i, p->ctx[i]);
  177. }
  178. }
  179. static struct omap_mbox_ops omap2_mbox_ops = {
  180. .type = OMAP_MBOX_TYPE2,
  181. .startup = omap2_mbox_startup,
  182. .shutdown = omap2_mbox_shutdown,
  183. .fifo_read = omap2_mbox_fifo_read,
  184. .fifo_write = omap2_mbox_fifo_write,
  185. .fifo_empty = omap2_mbox_fifo_empty,
  186. .fifo_full = omap2_mbox_fifo_full,
  187. .enable_irq = omap2_mbox_enable_irq,
  188. .disable_irq = omap2_mbox_disable_irq,
  189. .ack_irq = omap2_mbox_ack_irq,
  190. .is_irq = omap2_mbox_is_irq,
  191. .save_ctx = omap2_mbox_save_ctx,
  192. .restore_ctx = omap2_mbox_restore_ctx,
  193. };
  194. /*
  195. * MAILBOX 0: ARM -> DSP,
  196. * MAILBOX 1: ARM <- DSP.
  197. * MAILBOX 2: ARM -> IVA,
  198. * MAILBOX 3: ARM <- IVA.
  199. */
  200. /* FIXME: the following structs should be filled automatically by the user id */
  201. /* DSP */
  202. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  203. .tx_fifo = {
  204. .msg = MAILBOX_MESSAGE(0),
  205. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  206. },
  207. .rx_fifo = {
  208. .msg = MAILBOX_MESSAGE(1),
  209. .msg_stat = MAILBOX_MSGSTATUS(1),
  210. },
  211. .irqenable = MAILBOX_IRQENABLE(0),
  212. .irqstatus = MAILBOX_IRQSTATUS(0),
  213. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  214. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  215. };
  216. struct omap_mbox mbox_dsp_info = {
  217. .name = "dsp",
  218. .ops = &omap2_mbox_ops,
  219. .priv = &omap2_mbox_dsp_priv,
  220. };
  221. EXPORT_SYMBOL(mbox_dsp_info);
  222. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  223. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  224. .tx_fifo = {
  225. .msg = MAILBOX_MESSAGE(2),
  226. .fifo_stat = MAILBOX_FIFOSTATUS(2),
  227. },
  228. .rx_fifo = {
  229. .msg = MAILBOX_MESSAGE(3),
  230. .msg_stat = MAILBOX_MSGSTATUS(3),
  231. },
  232. .irqenable = MAILBOX_IRQENABLE(3),
  233. .irqstatus = MAILBOX_IRQSTATUS(3),
  234. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  235. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  236. };
  237. static struct omap_mbox mbox_iva_info = {
  238. .name = "iva",
  239. .ops = &omap2_mbox_ops,
  240. .priv = &omap2_mbox_iva_priv,
  241. };
  242. #endif
  243. static int __devinit omap2_mbox_probe(struct platform_device *pdev)
  244. {
  245. struct resource *res;
  246. int ret;
  247. /* MBOX base */
  248. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  249. if (unlikely(!res)) {
  250. dev_err(&pdev->dev, "invalid mem resource\n");
  251. return -ENODEV;
  252. }
  253. mbox_base = ioremap(res->start, resource_size(res));
  254. if (!mbox_base)
  255. return -ENOMEM;
  256. /* DSP or IVA2 IRQ */
  257. ret = platform_get_irq(pdev, 0);
  258. if (ret < 0) {
  259. dev_err(&pdev->dev, "invalid irq resource\n");
  260. goto err_dsp;
  261. }
  262. mbox_dsp_info.irq = ret;
  263. ret = omap_mbox_register(&pdev->dev, &mbox_dsp_info);
  264. if (ret)
  265. goto err_dsp;
  266. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  267. if (cpu_is_omap2420()) {
  268. /* IVA IRQ */
  269. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  270. if (unlikely(!res)) {
  271. dev_err(&pdev->dev, "invalid irq resource\n");
  272. ret = -ENODEV;
  273. goto err_iva1;
  274. }
  275. mbox_iva_info.irq = res->start;
  276. ret = omap_mbox_register(&pdev->dev, &mbox_iva_info);
  277. if (ret)
  278. goto err_iva1;
  279. }
  280. #endif
  281. return 0;
  282. err_iva1:
  283. omap_mbox_unregister(&mbox_dsp_info);
  284. err_dsp:
  285. iounmap(mbox_base);
  286. return ret;
  287. }
  288. static int __devexit omap2_mbox_remove(struct platform_device *pdev)
  289. {
  290. #if defined(CONFIG_ARCH_OMAP2420)
  291. omap_mbox_unregister(&mbox_iva_info);
  292. #endif
  293. omap_mbox_unregister(&mbox_dsp_info);
  294. iounmap(mbox_base);
  295. return 0;
  296. }
  297. static struct platform_driver omap2_mbox_driver = {
  298. .probe = omap2_mbox_probe,
  299. .remove = __devexit_p(omap2_mbox_remove),
  300. .driver = {
  301. .name = "omap2-mailbox",
  302. },
  303. };
  304. static int __init omap2_mbox_init(void)
  305. {
  306. return platform_driver_register(&omap2_mbox_driver);
  307. }
  308. static void __exit omap2_mbox_exit(void)
  309. {
  310. platform_driver_unregister(&omap2_mbox_driver);
  311. }
  312. module_init(omap2_mbox_init);
  313. module_exit(omap2_mbox_exit);
  314. MODULE_LICENSE("GPL v2");
  315. MODULE_DESCRIPTION("omap mailbox: omap2/3 architecture specific functions");
  316. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>, Paul Mundt");
  317. MODULE_ALIAS("platform:omap2-mailbox");