mailbox.c 11 KB

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