mailbox.c 12 KB

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