mailbox.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * OMAP mailbox driver
  3. *
  4. * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
  5. *
  6. * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/interrupt.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/mutex.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <linux/kfifo.h>
  29. #include <linux/err.h>
  30. #include <plat/mailbox.h>
  31. static struct workqueue_struct *mboxd;
  32. static struct omap_mbox **mboxes;
  33. static bool rq_full;
  34. static int mbox_configured;
  35. static DEFINE_MUTEX(mbox_configured_lock);
  36. static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
  37. module_param(mbox_kfifo_size, uint, S_IRUGO);
  38. MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
  39. /* Mailbox FIFO handle functions */
  40. static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
  41. {
  42. return mbox->ops->fifo_read(mbox);
  43. }
  44. static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  45. {
  46. mbox->ops->fifo_write(mbox, msg);
  47. }
  48. static inline int mbox_fifo_empty(struct omap_mbox *mbox)
  49. {
  50. return mbox->ops->fifo_empty(mbox);
  51. }
  52. static inline int mbox_fifo_full(struct omap_mbox *mbox)
  53. {
  54. return mbox->ops->fifo_full(mbox);
  55. }
  56. /* Mailbox IRQ handle functions */
  57. static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  58. {
  59. if (mbox->ops->ack_irq)
  60. mbox->ops->ack_irq(mbox, irq);
  61. }
  62. static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  63. {
  64. return mbox->ops->is_irq(mbox, irq);
  65. }
  66. /*
  67. * message sender
  68. */
  69. static int __mbox_poll_for_space(struct omap_mbox *mbox)
  70. {
  71. int ret = 0, i = 1000;
  72. while (mbox_fifo_full(mbox)) {
  73. if (mbox->ops->type == OMAP_MBOX_TYPE2)
  74. return -1;
  75. if (--i == 0)
  76. return -1;
  77. udelay(1);
  78. }
  79. return ret;
  80. }
  81. int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
  82. {
  83. struct omap_mbox_queue *mq = mbox->txq;
  84. int ret = 0, len;
  85. spin_lock(&mq->lock);
  86. if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
  87. ret = -ENOMEM;
  88. goto out;
  89. }
  90. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  91. WARN_ON(len != sizeof(msg));
  92. tasklet_schedule(&mbox->txq->tasklet);
  93. out:
  94. spin_unlock(&mq->lock);
  95. return ret;
  96. }
  97. EXPORT_SYMBOL(omap_mbox_msg_send);
  98. static void mbox_tx_tasklet(unsigned long tx_data)
  99. {
  100. struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
  101. struct omap_mbox_queue *mq = mbox->txq;
  102. mbox_msg_t msg;
  103. int ret;
  104. while (kfifo_len(&mq->fifo)) {
  105. if (__mbox_poll_for_space(mbox)) {
  106. omap_mbox_enable_irq(mbox, IRQ_TX);
  107. break;
  108. }
  109. ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
  110. sizeof(msg));
  111. WARN_ON(ret != sizeof(msg));
  112. mbox_fifo_write(mbox, msg);
  113. }
  114. }
  115. /*
  116. * Message receiver(workqueue)
  117. */
  118. static void mbox_rx_work(struct work_struct *work)
  119. {
  120. struct omap_mbox_queue *mq =
  121. container_of(work, struct omap_mbox_queue, work);
  122. mbox_msg_t msg;
  123. int len;
  124. while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
  125. len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  126. WARN_ON(len != sizeof(msg));
  127. if (mq->callback)
  128. mq->callback((void *)msg);
  129. }
  130. }
  131. /*
  132. * Mailbox interrupt handler
  133. */
  134. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  135. {
  136. omap_mbox_disable_irq(mbox, IRQ_TX);
  137. ack_mbox_irq(mbox, IRQ_TX);
  138. tasklet_schedule(&mbox->txq->tasklet);
  139. }
  140. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  141. {
  142. struct omap_mbox_queue *mq = mbox->rxq;
  143. mbox_msg_t msg;
  144. int len;
  145. while (!mbox_fifo_empty(mbox)) {
  146. if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
  147. omap_mbox_disable_irq(mbox, IRQ_RX);
  148. rq_full = true;
  149. goto nomem;
  150. }
  151. msg = mbox_fifo_read(mbox);
  152. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  153. WARN_ON(len != sizeof(msg));
  154. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  155. break;
  156. }
  157. /* no more messages in the fifo. clear IRQ source. */
  158. ack_mbox_irq(mbox, IRQ_RX);
  159. nomem:
  160. queue_work(mboxd, &mbox->rxq->work);
  161. }
  162. static irqreturn_t mbox_interrupt(int irq, void *p)
  163. {
  164. struct omap_mbox *mbox = p;
  165. if (is_mbox_irq(mbox, IRQ_TX))
  166. __mbox_tx_interrupt(mbox);
  167. if (is_mbox_irq(mbox, IRQ_RX))
  168. __mbox_rx_interrupt(mbox);
  169. return IRQ_HANDLED;
  170. }
  171. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  172. void (*work) (struct work_struct *),
  173. void (*tasklet)(unsigned long))
  174. {
  175. struct omap_mbox_queue *mq;
  176. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  177. if (!mq)
  178. return NULL;
  179. spin_lock_init(&mq->lock);
  180. if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
  181. goto error;
  182. if (work)
  183. INIT_WORK(&mq->work, work);
  184. if (tasklet)
  185. tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
  186. return mq;
  187. error:
  188. kfree(mq);
  189. return NULL;
  190. }
  191. static void mbox_queue_free(struct omap_mbox_queue *q)
  192. {
  193. kfifo_free(&q->fifo);
  194. kfree(q);
  195. }
  196. static int omap_mbox_startup(struct omap_mbox *mbox)
  197. {
  198. int ret = 0;
  199. struct omap_mbox_queue *mq;
  200. if (mbox->ops->startup) {
  201. mutex_lock(&mbox_configured_lock);
  202. if (!mbox_configured)
  203. ret = mbox->ops->startup(mbox);
  204. if (ret) {
  205. mutex_unlock(&mbox_configured_lock);
  206. return ret;
  207. }
  208. mbox_configured++;
  209. mutex_unlock(&mbox_configured_lock);
  210. }
  211. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  212. mbox->name, mbox);
  213. if (ret) {
  214. printk(KERN_ERR
  215. "failed to register mailbox interrupt:%d\n", ret);
  216. goto fail_request_irq;
  217. }
  218. mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
  219. if (!mq) {
  220. ret = -ENOMEM;
  221. goto fail_alloc_txq;
  222. }
  223. mbox->txq = mq;
  224. mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
  225. if (!mq) {
  226. ret = -ENOMEM;
  227. goto fail_alloc_rxq;
  228. }
  229. mbox->rxq = mq;
  230. return 0;
  231. fail_alloc_rxq:
  232. mbox_queue_free(mbox->txq);
  233. fail_alloc_txq:
  234. free_irq(mbox->irq, mbox);
  235. fail_request_irq:
  236. if (mbox->ops->shutdown)
  237. mbox->ops->shutdown(mbox);
  238. return ret;
  239. }
  240. static void omap_mbox_fini(struct omap_mbox *mbox)
  241. {
  242. free_irq(mbox->irq, mbox);
  243. tasklet_kill(&mbox->txq->tasklet);
  244. flush_work(&mbox->rxq->work);
  245. mbox_queue_free(mbox->txq);
  246. mbox_queue_free(mbox->rxq);
  247. if (mbox->ops->shutdown) {
  248. mutex_lock(&mbox_configured_lock);
  249. if (mbox_configured > 0)
  250. mbox_configured--;
  251. if (!mbox_configured)
  252. mbox->ops->shutdown(mbox);
  253. mutex_unlock(&mbox_configured_lock);
  254. }
  255. }
  256. struct omap_mbox *omap_mbox_get(const char *name)
  257. {
  258. struct omap_mbox *mbox;
  259. int ret;
  260. if (!mboxes)
  261. return ERR_PTR(-EINVAL);
  262. for (mbox = *mboxes; mbox; mbox++)
  263. if (!strcmp(mbox->name, name))
  264. break;
  265. if (!mbox)
  266. return ERR_PTR(-ENOENT);
  267. ret = omap_mbox_startup(mbox);
  268. if (ret)
  269. return ERR_PTR(-ENODEV);
  270. return mbox;
  271. }
  272. EXPORT_SYMBOL(omap_mbox_get);
  273. void omap_mbox_put(struct omap_mbox *mbox)
  274. {
  275. omap_mbox_fini(mbox);
  276. }
  277. EXPORT_SYMBOL(omap_mbox_put);
  278. static struct class omap_mbox_class = { .name = "mbox", };
  279. int omap_mbox_register(struct device *parent, struct omap_mbox **list)
  280. {
  281. int ret;
  282. int i;
  283. mboxes = list;
  284. if (!mboxes)
  285. return -EINVAL;
  286. for (i = 0; mboxes[i]; i++) {
  287. struct omap_mbox *mbox = mboxes[i];
  288. mbox->dev = device_create(&omap_mbox_class,
  289. parent, 0, mbox, "%s", mbox->name);
  290. if (IS_ERR(mbox->dev)) {
  291. ret = PTR_ERR(mbox->dev);
  292. goto err_out;
  293. }
  294. }
  295. return 0;
  296. err_out:
  297. while (i--)
  298. device_unregister(mboxes[i]->dev);
  299. return ret;
  300. }
  301. EXPORT_SYMBOL(omap_mbox_register);
  302. int omap_mbox_unregister(void)
  303. {
  304. int i;
  305. if (!mboxes)
  306. return -EINVAL;
  307. for (i = 0; mboxes[i]; i++)
  308. device_unregister(mboxes[i]->dev);
  309. mboxes = NULL;
  310. return 0;
  311. }
  312. EXPORT_SYMBOL(omap_mbox_unregister);
  313. static int __init omap_mbox_init(void)
  314. {
  315. int err;
  316. err = class_register(&omap_mbox_class);
  317. if (err)
  318. return err;
  319. mboxd = create_workqueue("mboxd");
  320. if (!mboxd)
  321. return -ENOMEM;
  322. /* kfifo size sanity check: alignment and minimal size */
  323. mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
  324. mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size, sizeof(mbox_msg_t));
  325. return 0;
  326. }
  327. subsys_initcall(omap_mbox_init);
  328. static void __exit omap_mbox_exit(void)
  329. {
  330. destroy_workqueue(mboxd);
  331. class_unregister(&omap_mbox_class);
  332. }
  333. module_exit(omap_mbox_exit);
  334. MODULE_LICENSE("GPL v2");
  335. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  336. MODULE_AUTHOR("Toshihiro Kobayashi");
  337. MODULE_AUTHOR("Hiroshi DOYU");