mailbox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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/module.h>
  13. #include <linux/clk.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/pm_runtime.h>
  18. #include <plat/mailbox.h>
  19. #include <mach/irqs.h>
  20. #define MAILBOX_REVISION 0x000
  21. #define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
  22. #define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
  23. #define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
  24. #define MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
  25. #define MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
  26. #define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 10 * (u))
  27. #define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 10 * (u))
  28. #define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 10 * (u))
  29. #define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
  30. #define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
  31. #define MBOX_REG_SIZE 0x120
  32. #define OMAP4_MBOX_REG_SIZE 0x130
  33. #define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
  34. #define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
  35. static void __iomem *mbox_base;
  36. struct omap_mbox2_fifo {
  37. unsigned long msg;
  38. unsigned long fifo_stat;
  39. unsigned long msg_stat;
  40. };
  41. struct omap_mbox2_priv {
  42. struct omap_mbox2_fifo tx_fifo;
  43. struct omap_mbox2_fifo rx_fifo;
  44. unsigned long irqenable;
  45. unsigned long irqstatus;
  46. u32 newmsg_bit;
  47. u32 notfull_bit;
  48. u32 ctx[OMAP4_MBOX_NR_REGS];
  49. unsigned long irqdisable;
  50. };
  51. static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  52. omap_mbox_type_t irq);
  53. static inline unsigned int mbox_read_reg(size_t ofs)
  54. {
  55. return __raw_readl(mbox_base + ofs);
  56. }
  57. static inline void mbox_write_reg(u32 val, size_t ofs)
  58. {
  59. __raw_writel(val, mbox_base + ofs);
  60. }
  61. /* Mailbox H/W preparations */
  62. static int omap2_mbox_startup(struct omap_mbox *mbox)
  63. {
  64. u32 l;
  65. pm_runtime_enable(mbox->dev->parent);
  66. pm_runtime_get_sync(mbox->dev->parent);
  67. l = mbox_read_reg(MAILBOX_REVISION);
  68. pr_debug("omap mailbox rev %d.%d\n", (l & 0xf0) >> 4, (l & 0x0f));
  69. omap2_mbox_enable_irq(mbox, IRQ_RX);
  70. return 0;
  71. }
  72. static void omap2_mbox_shutdown(struct omap_mbox *mbox)
  73. {
  74. pm_runtime_put_sync(mbox->dev->parent);
  75. pm_runtime_disable(mbox->dev->parent);
  76. }
  77. /* Mailbox FIFO handle functions */
  78. static mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
  79. {
  80. struct omap_mbox2_fifo *fifo =
  81. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  82. return (mbox_msg_t) mbox_read_reg(fifo->msg);
  83. }
  84. static void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  85. {
  86. struct omap_mbox2_fifo *fifo =
  87. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  88. mbox_write_reg(msg, fifo->msg);
  89. }
  90. static int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
  91. {
  92. struct omap_mbox2_fifo *fifo =
  93. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  94. return (mbox_read_reg(fifo->msg_stat) == 0);
  95. }
  96. static int omap2_mbox_fifo_full(struct omap_mbox *mbox)
  97. {
  98. struct omap_mbox2_fifo *fifo =
  99. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  100. return mbox_read_reg(fifo->fifo_stat);
  101. }
  102. /* Mailbox IRQ handle functions */
  103. static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  104. omap_mbox_type_t irq)
  105. {
  106. struct omap_mbox2_priv *p = mbox->priv;
  107. u32 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  108. l = mbox_read_reg(p->irqenable);
  109. l |= bit;
  110. mbox_write_reg(l, p->irqenable);
  111. }
  112. static void omap2_mbox_disable_irq(struct omap_mbox *mbox,
  113. omap_mbox_type_t irq)
  114. {
  115. struct omap_mbox2_priv *p = mbox->priv;
  116. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  117. if (!cpu_is_omap44xx())
  118. bit = mbox_read_reg(p->irqdisable) & ~bit;
  119. mbox_write_reg(bit, p->irqdisable);
  120. }
  121. static void omap2_mbox_ack_irq(struct omap_mbox *mbox,
  122. omap_mbox_type_t irq)
  123. {
  124. struct omap_mbox2_priv *p = mbox->priv;
  125. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  126. mbox_write_reg(bit, p->irqstatus);
  127. /* Flush posted write for irq status to avoid spurious interrupts */
  128. mbox_read_reg(p->irqstatus);
  129. }
  130. static int omap2_mbox_is_irq(struct omap_mbox *mbox,
  131. omap_mbox_type_t irq)
  132. {
  133. struct omap_mbox2_priv *p = mbox->priv;
  134. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  135. u32 enable = mbox_read_reg(p->irqenable);
  136. u32 status = mbox_read_reg(p->irqstatus);
  137. return (int)(enable & status & bit);
  138. }
  139. static void omap2_mbox_save_ctx(struct omap_mbox *mbox)
  140. {
  141. int i;
  142. struct omap_mbox2_priv *p = mbox->priv;
  143. int nr_regs;
  144. if (cpu_is_omap44xx())
  145. nr_regs = OMAP4_MBOX_NR_REGS;
  146. else
  147. nr_regs = MBOX_NR_REGS;
  148. for (i = 0; i < nr_regs; i++) {
  149. p->ctx[i] = mbox_read_reg(i * sizeof(u32));
  150. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  151. i, p->ctx[i]);
  152. }
  153. }
  154. static void omap2_mbox_restore_ctx(struct omap_mbox *mbox)
  155. {
  156. int i;
  157. struct omap_mbox2_priv *p = mbox->priv;
  158. int nr_regs;
  159. if (cpu_is_omap44xx())
  160. nr_regs = OMAP4_MBOX_NR_REGS;
  161. else
  162. nr_regs = MBOX_NR_REGS;
  163. for (i = 0; i < nr_regs; i++) {
  164. mbox_write_reg(p->ctx[i], i * sizeof(u32));
  165. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  166. i, p->ctx[i]);
  167. }
  168. }
  169. static struct omap_mbox_ops omap2_mbox_ops = {
  170. .type = OMAP_MBOX_TYPE2,
  171. .startup = omap2_mbox_startup,
  172. .shutdown = omap2_mbox_shutdown,
  173. .fifo_read = omap2_mbox_fifo_read,
  174. .fifo_write = omap2_mbox_fifo_write,
  175. .fifo_empty = omap2_mbox_fifo_empty,
  176. .fifo_full = omap2_mbox_fifo_full,
  177. .enable_irq = omap2_mbox_enable_irq,
  178. .disable_irq = omap2_mbox_disable_irq,
  179. .ack_irq = omap2_mbox_ack_irq,
  180. .is_irq = omap2_mbox_is_irq,
  181. .save_ctx = omap2_mbox_save_ctx,
  182. .restore_ctx = omap2_mbox_restore_ctx,
  183. };
  184. /*
  185. * MAILBOX 0: ARM -> DSP,
  186. * MAILBOX 1: ARM <- DSP.
  187. * MAILBOX 2: ARM -> IVA,
  188. * MAILBOX 3: ARM <- IVA.
  189. */
  190. /* FIXME: the following structs should be filled automatically by the user id */
  191. #if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP2)
  192. /* DSP */
  193. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  194. .tx_fifo = {
  195. .msg = MAILBOX_MESSAGE(0),
  196. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  197. },
  198. .rx_fifo = {
  199. .msg = MAILBOX_MESSAGE(1),
  200. .msg_stat = MAILBOX_MSGSTATUS(1),
  201. },
  202. .irqenable = MAILBOX_IRQENABLE(0),
  203. .irqstatus = MAILBOX_IRQSTATUS(0),
  204. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  205. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  206. .irqdisable = MAILBOX_IRQENABLE(0),
  207. };
  208. struct omap_mbox mbox_dsp_info = {
  209. .name = "dsp",
  210. .ops = &omap2_mbox_ops,
  211. .priv = &omap2_mbox_dsp_priv,
  212. };
  213. #endif
  214. #if defined(CONFIG_ARCH_OMAP3)
  215. struct omap_mbox *omap3_mboxes[] = { &mbox_dsp_info, NULL };
  216. #endif
  217. #if defined(CONFIG_SOC_OMAP2420)
  218. /* IVA */
  219. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  220. .tx_fifo = {
  221. .msg = MAILBOX_MESSAGE(2),
  222. .fifo_stat = MAILBOX_FIFOSTATUS(2),
  223. },
  224. .rx_fifo = {
  225. .msg = MAILBOX_MESSAGE(3),
  226. .msg_stat = MAILBOX_MSGSTATUS(3),
  227. },
  228. .irqenable = MAILBOX_IRQENABLE(3),
  229. .irqstatus = MAILBOX_IRQSTATUS(3),
  230. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  231. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  232. .irqdisable = MAILBOX_IRQENABLE(3),
  233. };
  234. static struct omap_mbox mbox_iva_info = {
  235. .name = "iva",
  236. .ops = &omap2_mbox_ops,
  237. .priv = &omap2_mbox_iva_priv,
  238. };
  239. #endif
  240. #ifdef CONFIG_ARCH_OMAP2
  241. struct omap_mbox *omap2_mboxes[] = {
  242. &mbox_dsp_info,
  243. #ifdef CONFIG_SOC_OMAP2420
  244. &mbox_iva_info,
  245. #endif
  246. NULL
  247. };
  248. #endif
  249. #if defined(CONFIG_ARCH_OMAP4)
  250. /* OMAP4 */
  251. static struct omap_mbox2_priv omap2_mbox_1_priv = {
  252. .tx_fifo = {
  253. .msg = MAILBOX_MESSAGE(0),
  254. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  255. },
  256. .rx_fifo = {
  257. .msg = MAILBOX_MESSAGE(1),
  258. .msg_stat = MAILBOX_MSGSTATUS(1),
  259. },
  260. .irqenable = OMAP4_MAILBOX_IRQENABLE(0),
  261. .irqstatus = OMAP4_MAILBOX_IRQSTATUS(0),
  262. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  263. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  264. .irqdisable = OMAP4_MAILBOX_IRQENABLE_CLR(0),
  265. };
  266. struct omap_mbox mbox_1_info = {
  267. .name = "mailbox-1",
  268. .ops = &omap2_mbox_ops,
  269. .priv = &omap2_mbox_1_priv,
  270. };
  271. static struct omap_mbox2_priv omap2_mbox_2_priv = {
  272. .tx_fifo = {
  273. .msg = MAILBOX_MESSAGE(3),
  274. .fifo_stat = MAILBOX_FIFOSTATUS(3),
  275. },
  276. .rx_fifo = {
  277. .msg = MAILBOX_MESSAGE(2),
  278. .msg_stat = MAILBOX_MSGSTATUS(2),
  279. },
  280. .irqenable = OMAP4_MAILBOX_IRQENABLE(0),
  281. .irqstatus = OMAP4_MAILBOX_IRQSTATUS(0),
  282. .notfull_bit = MAILBOX_IRQ_NOTFULL(3),
  283. .newmsg_bit = MAILBOX_IRQ_NEWMSG(2),
  284. .irqdisable = OMAP4_MAILBOX_IRQENABLE_CLR(0),
  285. };
  286. struct omap_mbox mbox_2_info = {
  287. .name = "mailbox-2",
  288. .ops = &omap2_mbox_ops,
  289. .priv = &omap2_mbox_2_priv,
  290. };
  291. struct omap_mbox *omap4_mboxes[] = { &mbox_1_info, &mbox_2_info, NULL };
  292. #endif
  293. static int __devinit omap2_mbox_probe(struct platform_device *pdev)
  294. {
  295. struct resource *mem;
  296. int ret;
  297. struct omap_mbox **list;
  298. if (false)
  299. ;
  300. #if defined(CONFIG_ARCH_OMAP3)
  301. else if (cpu_is_omap34xx()) {
  302. list = omap3_mboxes;
  303. list[0]->irq = platform_get_irq(pdev, 0);
  304. }
  305. #endif
  306. #if defined(CONFIG_ARCH_OMAP2)
  307. else if (cpu_is_omap2430()) {
  308. list = omap2_mboxes;
  309. list[0]->irq = platform_get_irq(pdev, 0);
  310. } else if (cpu_is_omap2420()) {
  311. list = omap2_mboxes;
  312. list[0]->irq = platform_get_irq_byname(pdev, "dsp");
  313. list[1]->irq = platform_get_irq_byname(pdev, "iva");
  314. }
  315. #endif
  316. #if defined(CONFIG_ARCH_OMAP4)
  317. else if (cpu_is_omap44xx()) {
  318. list = omap4_mboxes;
  319. list[0]->irq = list[1]->irq = platform_get_irq(pdev, 0);
  320. }
  321. #endif
  322. else {
  323. pr_err("%s: platform not supported\n", __func__);
  324. return -ENODEV;
  325. }
  326. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  327. mbox_base = ioremap(mem->start, resource_size(mem));
  328. if (!mbox_base)
  329. return -ENOMEM;
  330. ret = omap_mbox_register(&pdev->dev, list);
  331. if (ret) {
  332. iounmap(mbox_base);
  333. return ret;
  334. }
  335. return 0;
  336. }
  337. static int __devexit omap2_mbox_remove(struct platform_device *pdev)
  338. {
  339. omap_mbox_unregister();
  340. iounmap(mbox_base);
  341. return 0;
  342. }
  343. static struct platform_driver omap2_mbox_driver = {
  344. .probe = omap2_mbox_probe,
  345. .remove = __devexit_p(omap2_mbox_remove),
  346. .driver = {
  347. .name = "omap-mailbox",
  348. },
  349. };
  350. static int __init omap2_mbox_init(void)
  351. {
  352. return platform_driver_register(&omap2_mbox_driver);
  353. }
  354. static void __exit omap2_mbox_exit(void)
  355. {
  356. platform_driver_unregister(&omap2_mbox_driver);
  357. }
  358. module_init(omap2_mbox_init);
  359. module_exit(omap2_mbox_exit);
  360. MODULE_LICENSE("GPL v2");
  361. MODULE_DESCRIPTION("omap mailbox: omap2/3/4 architecture specific functions");
  362. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>");
  363. MODULE_AUTHOR("Paul Mundt");
  364. MODULE_ALIAS("platform:omap2-mailbox");