mailbox.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <plat/mailbox.h>
  18. #include <mach/irqs.h>
  19. #define DRV_NAME "omap2-mailbox"
  20. #define MAILBOX_REVISION 0x000
  21. #define MAILBOX_SYSCONFIG 0x010
  22. #define MAILBOX_SYSSTATUS 0x014
  23. #define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
  24. #define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
  25. #define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
  26. #define MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
  27. #define MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
  28. #define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 10 * (u))
  29. #define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 10 * (u))
  30. #define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 10 * (u))
  31. #define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
  32. #define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
  33. /* SYSCONFIG: register bit definition */
  34. #define AUTOIDLE (1 << 0)
  35. #define SOFTRESET (1 << 1)
  36. #define SMARTIDLE (2 << 3)
  37. /* SYSSTATUS: register bit definition */
  38. #define RESETDONE (1 << 0)
  39. #define MBOX_REG_SIZE 0x120
  40. #define OMAP4_MBOX_REG_SIZE 0x130
  41. #define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
  42. #define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
  43. static void __iomem *mbox_base;
  44. struct omap_mbox2_fifo {
  45. unsigned long msg;
  46. unsigned long fifo_stat;
  47. unsigned long msg_stat;
  48. };
  49. struct omap_mbox2_priv {
  50. struct omap_mbox2_fifo tx_fifo;
  51. struct omap_mbox2_fifo rx_fifo;
  52. unsigned long irqenable;
  53. unsigned long irqstatus;
  54. u32 newmsg_bit;
  55. u32 notfull_bit;
  56. u32 ctx[OMAP4_MBOX_NR_REGS];
  57. unsigned long irqdisable;
  58. };
  59. static struct clk *mbox_ick_handle;
  60. static void omap2_mbox_enable_irq(struct omap_mbox *mbox,
  61. omap_mbox_type_t irq);
  62. static inline unsigned int mbox_read_reg(size_t ofs)
  63. {
  64. return __raw_readl(mbox_base + ofs);
  65. }
  66. static inline void mbox_write_reg(u32 val, size_t ofs)
  67. {
  68. __raw_writel(val, mbox_base + ofs);
  69. }
  70. /* Mailbox H/W preparations */
  71. static int omap2_mbox_startup(struct omap_mbox *mbox)
  72. {
  73. u32 l;
  74. unsigned long timeout;
  75. mbox_ick_handle = clk_get(NULL, "mailboxes_ick");
  76. if (IS_ERR(mbox_ick_handle)) {
  77. printk(KERN_ERR "Could not get mailboxes_ick: %d\n",
  78. PTR_ERR(mbox_ick_handle));
  79. return PTR_ERR(mbox_ick_handle);
  80. }
  81. clk_enable(mbox_ick_handle);
  82. mbox_write_reg(SOFTRESET, MAILBOX_SYSCONFIG);
  83. timeout = jiffies + msecs_to_jiffies(20);
  84. do {
  85. l = mbox_read_reg(MAILBOX_SYSSTATUS);
  86. if (l & RESETDONE)
  87. break;
  88. } while (!time_after(jiffies, timeout));
  89. if (!(l & RESETDONE)) {
  90. pr_err("Can't take mmu out of reset\n");
  91. return -ENODEV;
  92. }
  93. l = mbox_read_reg(MAILBOX_REVISION);
  94. pr_info("omap mailbox rev %d.%d\n", (l & 0xf0) >> 4, (l & 0x0f));
  95. l = SMARTIDLE | AUTOIDLE;
  96. mbox_write_reg(l, MAILBOX_SYSCONFIG);
  97. omap2_mbox_enable_irq(mbox, IRQ_RX);
  98. return 0;
  99. }
  100. static void omap2_mbox_shutdown(struct omap_mbox *mbox)
  101. {
  102. clk_disable(mbox_ick_handle);
  103. clk_put(mbox_ick_handle);
  104. mbox_ick_handle = NULL;
  105. }
  106. /* Mailbox FIFO handle functions */
  107. static mbox_msg_t omap2_mbox_fifo_read(struct omap_mbox *mbox)
  108. {
  109. struct omap_mbox2_fifo *fifo =
  110. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  111. return (mbox_msg_t) mbox_read_reg(fifo->msg);
  112. }
  113. static void omap2_mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  114. {
  115. struct omap_mbox2_fifo *fifo =
  116. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  117. mbox_write_reg(msg, fifo->msg);
  118. }
  119. static int omap2_mbox_fifo_empty(struct omap_mbox *mbox)
  120. {
  121. struct omap_mbox2_fifo *fifo =
  122. &((struct omap_mbox2_priv *)mbox->priv)->rx_fifo;
  123. return (mbox_read_reg(fifo->msg_stat) == 0);
  124. }
  125. static int omap2_mbox_fifo_full(struct omap_mbox *mbox)
  126. {
  127. struct omap_mbox2_fifo *fifo =
  128. &((struct omap_mbox2_priv *)mbox->priv)->tx_fifo;
  129. return mbox_read_reg(fifo->fifo_stat);
  130. }
  131. /* Mailbox IRQ handle functions */
  132. static void omap2_mbox_enable_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_disable_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 l, bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  146. l = mbox_read_reg(p->irqdisable);
  147. l &= ~bit;
  148. mbox_write_reg(l, p->irqdisable);
  149. }
  150. static void omap2_mbox_ack_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. mbox_write_reg(bit, p->irqstatus);
  156. /* Flush posted write for irq status to avoid spurious interrupts */
  157. mbox_read_reg(p->irqstatus);
  158. }
  159. static int omap2_mbox_is_irq(struct omap_mbox *mbox,
  160. omap_mbox_type_t irq)
  161. {
  162. struct omap_mbox2_priv *p = (struct omap_mbox2_priv *)mbox->priv;
  163. u32 bit = (irq == IRQ_TX) ? p->notfull_bit : p->newmsg_bit;
  164. u32 enable = mbox_read_reg(p->irqenable);
  165. u32 status = mbox_read_reg(p->irqstatus);
  166. return (int)(enable & status & bit);
  167. }
  168. static void omap2_mbox_save_ctx(struct omap_mbox *mbox)
  169. {
  170. int i;
  171. struct omap_mbox2_priv *p = mbox->priv;
  172. int nr_regs;
  173. if (cpu_is_omap44xx())
  174. nr_regs = OMAP4_MBOX_NR_REGS;
  175. else
  176. nr_regs = MBOX_NR_REGS;
  177. for (i = 0; i < nr_regs; i++) {
  178. p->ctx[i] = mbox_read_reg(i * sizeof(u32));
  179. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  180. i, p->ctx[i]);
  181. }
  182. }
  183. static void omap2_mbox_restore_ctx(struct omap_mbox *mbox)
  184. {
  185. int i;
  186. struct omap_mbox2_priv *p = mbox->priv;
  187. int nr_regs;
  188. if (cpu_is_omap44xx())
  189. nr_regs = OMAP4_MBOX_NR_REGS;
  190. else
  191. nr_regs = MBOX_NR_REGS;
  192. for (i = 0; i < nr_regs; i++) {
  193. mbox_write_reg(p->ctx[i], i * sizeof(u32));
  194. dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
  195. i, p->ctx[i]);
  196. }
  197. }
  198. static struct omap_mbox_ops omap2_mbox_ops = {
  199. .type = OMAP_MBOX_TYPE2,
  200. .startup = omap2_mbox_startup,
  201. .shutdown = omap2_mbox_shutdown,
  202. .fifo_read = omap2_mbox_fifo_read,
  203. .fifo_write = omap2_mbox_fifo_write,
  204. .fifo_empty = omap2_mbox_fifo_empty,
  205. .fifo_full = omap2_mbox_fifo_full,
  206. .enable_irq = omap2_mbox_enable_irq,
  207. .disable_irq = omap2_mbox_disable_irq,
  208. .ack_irq = omap2_mbox_ack_irq,
  209. .is_irq = omap2_mbox_is_irq,
  210. .save_ctx = omap2_mbox_save_ctx,
  211. .restore_ctx = omap2_mbox_restore_ctx,
  212. };
  213. /*
  214. * MAILBOX 0: ARM -> DSP,
  215. * MAILBOX 1: ARM <- DSP.
  216. * MAILBOX 2: ARM -> IVA,
  217. * MAILBOX 3: ARM <- IVA.
  218. */
  219. /* FIXME: the following structs should be filled automatically by the user id */
  220. /* DSP */
  221. static struct omap_mbox2_priv omap2_mbox_dsp_priv = {
  222. .tx_fifo = {
  223. .msg = MAILBOX_MESSAGE(0),
  224. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  225. },
  226. .rx_fifo = {
  227. .msg = MAILBOX_MESSAGE(1),
  228. .msg_stat = MAILBOX_MSGSTATUS(1),
  229. },
  230. .irqenable = MAILBOX_IRQENABLE(0),
  231. .irqstatus = MAILBOX_IRQSTATUS(0),
  232. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  233. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  234. .irqdisable = MAILBOX_IRQENABLE(0),
  235. };
  236. /* OMAP4 specific data structure. Use the cpu_is_omap4xxx()
  237. to use this*/
  238. static struct omap_mbox2_priv omap2_mbox_1_priv = {
  239. .tx_fifo = {
  240. .msg = MAILBOX_MESSAGE(0),
  241. .fifo_stat = MAILBOX_FIFOSTATUS(0),
  242. },
  243. .rx_fifo = {
  244. .msg = MAILBOX_MESSAGE(1),
  245. .msg_stat = MAILBOX_MSGSTATUS(1),
  246. },
  247. .irqenable = OMAP4_MAILBOX_IRQENABLE(0),
  248. .irqstatus = OMAP4_MAILBOX_IRQSTATUS(0),
  249. .notfull_bit = MAILBOX_IRQ_NOTFULL(0),
  250. .newmsg_bit = MAILBOX_IRQ_NEWMSG(1),
  251. .irqdisable = OMAP4_MAILBOX_IRQENABLE_CLR(0),
  252. };
  253. struct omap_mbox mbox_1_info = {
  254. .name = "mailbox-1",
  255. .ops = &omap2_mbox_ops,
  256. .priv = &omap2_mbox_1_priv,
  257. };
  258. EXPORT_SYMBOL(mbox_1_info);
  259. struct omap_mbox mbox_dsp_info = {
  260. .name = "dsp",
  261. .ops = &omap2_mbox_ops,
  262. .priv = &omap2_mbox_dsp_priv,
  263. };
  264. EXPORT_SYMBOL(mbox_dsp_info);
  265. static struct omap_mbox2_priv omap2_mbox_2_priv = {
  266. .tx_fifo = {
  267. .msg = MAILBOX_MESSAGE(3),
  268. .fifo_stat = MAILBOX_FIFOSTATUS(3),
  269. },
  270. .rx_fifo = {
  271. .msg = MAILBOX_MESSAGE(2),
  272. .msg_stat = MAILBOX_MSGSTATUS(2),
  273. },
  274. .irqenable = OMAP4_MAILBOX_IRQENABLE(0),
  275. .irqstatus = OMAP4_MAILBOX_IRQSTATUS(0),
  276. .notfull_bit = MAILBOX_IRQ_NOTFULL(3),
  277. .newmsg_bit = MAILBOX_IRQ_NEWMSG(2),
  278. .irqdisable = OMAP4_MAILBOX_IRQENABLE_CLR(0),
  279. };
  280. struct omap_mbox mbox_2_info = {
  281. .name = "mailbox-2",
  282. .ops = &omap2_mbox_ops,
  283. .priv = &omap2_mbox_2_priv,
  284. };
  285. EXPORT_SYMBOL(mbox_2_info);
  286. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  287. static struct omap_mbox2_priv omap2_mbox_iva_priv = {
  288. .tx_fifo = {
  289. .msg = MAILBOX_MESSAGE(2),
  290. .fifo_stat = MAILBOX_FIFOSTATUS(2),
  291. },
  292. .rx_fifo = {
  293. .msg = MAILBOX_MESSAGE(3),
  294. .msg_stat = MAILBOX_MSGSTATUS(3),
  295. },
  296. .irqenable = MAILBOX_IRQENABLE(3),
  297. .irqstatus = MAILBOX_IRQSTATUS(3),
  298. .notfull_bit = MAILBOX_IRQ_NOTFULL(2),
  299. .newmsg_bit = MAILBOX_IRQ_NEWMSG(3),
  300. .irqdisable = MAILBOX_IRQENABLE(3),
  301. };
  302. static struct omap_mbox mbox_iva_info = {
  303. .name = "iva",
  304. .ops = &omap2_mbox_ops,
  305. .priv = &omap2_mbox_iva_priv,
  306. };
  307. #endif
  308. static int __devinit omap2_mbox_probe(struct platform_device *pdev)
  309. {
  310. struct resource *res;
  311. int ret;
  312. /* MBOX base */
  313. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  314. if (unlikely(!res)) {
  315. dev_err(&pdev->dev, "invalid mem resource\n");
  316. return -ENODEV;
  317. }
  318. mbox_base = ioremap(res->start, resource_size(res));
  319. if (!mbox_base)
  320. return -ENOMEM;
  321. /* DSP or IVA2 IRQ */
  322. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  323. if (unlikely(!res)) {
  324. dev_err(&pdev->dev, "invalid irq resource\n");
  325. ret = -ENODEV;
  326. goto err_dsp;
  327. }
  328. if (cpu_is_omap44xx()) {
  329. mbox_1_info.irq = res->start;
  330. ret = omap_mbox_register(&pdev->dev, &mbox_1_info);
  331. } else {
  332. mbox_dsp_info.irq = res->start;
  333. ret = omap_mbox_register(&pdev->dev, &mbox_dsp_info);
  334. }
  335. if (ret)
  336. goto err_dsp;
  337. if (cpu_is_omap44xx()) {
  338. mbox_2_info.irq = res->start;
  339. ret = omap_mbox_register(&pdev->dev, &mbox_2_info);
  340. if (ret) {
  341. omap_mbox_unregister(&mbox_1_info);
  342. goto err_dsp;
  343. }
  344. }
  345. #if defined(CONFIG_ARCH_OMAP2420) /* IVA */
  346. if (cpu_is_omap2420()) {
  347. /* IVA IRQ */
  348. res = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
  349. if (unlikely(!res)) {
  350. dev_err(&pdev->dev, "invalid irq resource\n");
  351. ret = -ENODEV;
  352. goto err_iva1;
  353. }
  354. mbox_iva_info.irq = res->start;
  355. ret = omap_mbox_register(&pdev->dev, &mbox_iva_info);
  356. if (ret)
  357. goto err_iva1;
  358. }
  359. #endif
  360. return 0;
  361. err_iva1:
  362. omap_mbox_unregister(&mbox_dsp_info);
  363. err_dsp:
  364. iounmap(mbox_base);
  365. return ret;
  366. }
  367. static int __devexit omap2_mbox_remove(struct platform_device *pdev)
  368. {
  369. #if defined(CONFIG_ARCH_OMAP2420)
  370. omap_mbox_unregister(&mbox_iva_info);
  371. #endif
  372. if (cpu_is_omap44xx()) {
  373. omap_mbox_unregister(&mbox_2_info);
  374. omap_mbox_unregister(&mbox_1_info);
  375. } else
  376. omap_mbox_unregister(&mbox_dsp_info);
  377. iounmap(mbox_base);
  378. return 0;
  379. }
  380. static struct platform_driver omap2_mbox_driver = {
  381. .probe = omap2_mbox_probe,
  382. .remove = __devexit_p(omap2_mbox_remove),
  383. .driver = {
  384. .name = DRV_NAME,
  385. },
  386. };
  387. static int __init omap2_mbox_init(void)
  388. {
  389. return platform_driver_register(&omap2_mbox_driver);
  390. }
  391. static void __exit omap2_mbox_exit(void)
  392. {
  393. platform_driver_unregister(&omap2_mbox_driver);
  394. }
  395. module_init(omap2_mbox_init);
  396. module_exit(omap2_mbox_exit);
  397. MODULE_LICENSE("GPL v2");
  398. MODULE_DESCRIPTION("omap mailbox: omap2/3/4 architecture specific functions");
  399. MODULE_AUTHOR("Hiroshi DOYU <Hiroshi.DOYU@nokia.com>, Paul Mundt");
  400. MODULE_ALIAS("platform:"DRV_NAME);