mailbox.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/module.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/device.h>
  26. #include <linux/delay.h>
  27. #include <linux/slab.h>
  28. #include <plat/mailbox.h>
  29. static struct workqueue_struct *mboxd;
  30. static struct omap_mbox *mboxes;
  31. static DEFINE_RWLOCK(mboxes_lock);
  32. static int mbox_configured;
  33. static DEFINE_MUTEX(mbox_configured_lock);
  34. /* Mailbox FIFO handle functions */
  35. static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
  36. {
  37. return mbox->ops->fifo_read(mbox);
  38. }
  39. static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  40. {
  41. mbox->ops->fifo_write(mbox, msg);
  42. }
  43. static inline int mbox_fifo_empty(struct omap_mbox *mbox)
  44. {
  45. return mbox->ops->fifo_empty(mbox);
  46. }
  47. static inline int mbox_fifo_full(struct omap_mbox *mbox)
  48. {
  49. return mbox->ops->fifo_full(mbox);
  50. }
  51. /* Mailbox IRQ handle functions */
  52. static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  53. {
  54. if (mbox->ops->ack_irq)
  55. mbox->ops->ack_irq(mbox, irq);
  56. }
  57. static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  58. {
  59. return mbox->ops->is_irq(mbox, irq);
  60. }
  61. /*
  62. * message sender
  63. */
  64. static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
  65. {
  66. int ret = 0, i = 1000;
  67. while (mbox_fifo_full(mbox)) {
  68. if (mbox->ops->type == OMAP_MBOX_TYPE2)
  69. return -1;
  70. if (--i == 0)
  71. return -1;
  72. udelay(1);
  73. }
  74. mbox_fifo_write(mbox, msg);
  75. return ret;
  76. }
  77. int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
  78. {
  79. struct request *rq;
  80. struct request_queue *q = mbox->txq->queue;
  81. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  82. if (unlikely(!rq))
  83. return -ENOMEM;
  84. blk_insert_request(q, rq, 0, (void *) msg);
  85. tasklet_schedule(&mbox->txq->tasklet);
  86. return 0;
  87. }
  88. EXPORT_SYMBOL(omap_mbox_msg_send);
  89. static void mbox_tx_tasklet(unsigned long tx_data)
  90. {
  91. int ret;
  92. struct request *rq;
  93. struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
  94. struct request_queue *q = mbox->txq->queue;
  95. while (1) {
  96. rq = blk_fetch_request(q);
  97. if (!rq)
  98. break;
  99. ret = __mbox_msg_send(mbox, (mbox_msg_t)rq->special);
  100. if (ret) {
  101. omap_mbox_enable_irq(mbox, IRQ_TX);
  102. blk_requeue_request(q, rq);
  103. return;
  104. }
  105. blk_end_request_all(rq, 0);
  106. }
  107. }
  108. /*
  109. * Message receiver(workqueue)
  110. */
  111. static void mbox_rx_work(struct work_struct *work)
  112. {
  113. struct omap_mbox_queue *mq =
  114. container_of(work, struct omap_mbox_queue, work);
  115. struct omap_mbox *mbox = mq->queue->queuedata;
  116. struct request_queue *q = mbox->rxq->queue;
  117. struct request *rq;
  118. mbox_msg_t msg;
  119. unsigned long flags;
  120. while (1) {
  121. spin_lock_irqsave(q->queue_lock, flags);
  122. rq = blk_fetch_request(q);
  123. spin_unlock_irqrestore(q->queue_lock, flags);
  124. if (!rq)
  125. break;
  126. msg = (mbox_msg_t)rq->special;
  127. blk_end_request_all(rq, 0);
  128. if (mbox->rxq->callback)
  129. mbox->rxq->callback((void *)msg);
  130. }
  131. }
  132. /*
  133. * Mailbox interrupt handler
  134. */
  135. static void mbox_txq_fn(struct request_queue *q)
  136. {
  137. }
  138. static void mbox_rxq_fn(struct request_queue *q)
  139. {
  140. }
  141. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  142. {
  143. omap_mbox_disable_irq(mbox, IRQ_TX);
  144. ack_mbox_irq(mbox, IRQ_TX);
  145. tasklet_schedule(&mbox->txq->tasklet);
  146. }
  147. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  148. {
  149. struct request *rq;
  150. mbox_msg_t msg;
  151. struct request_queue *q = mbox->rxq->queue;
  152. while (!mbox_fifo_empty(mbox)) {
  153. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  154. if (unlikely(!rq))
  155. goto nomem;
  156. msg = mbox_fifo_read(mbox);
  157. blk_insert_request(q, rq, 0, (void *)msg);
  158. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  159. break;
  160. }
  161. /* no more messages in the fifo. clear IRQ source. */
  162. ack_mbox_irq(mbox, IRQ_RX);
  163. nomem:
  164. queue_work(mboxd, &mbox->rxq->work);
  165. }
  166. static irqreturn_t mbox_interrupt(int irq, void *p)
  167. {
  168. struct omap_mbox *mbox = p;
  169. if (is_mbox_irq(mbox, IRQ_TX))
  170. __mbox_tx_interrupt(mbox);
  171. if (is_mbox_irq(mbox, IRQ_RX))
  172. __mbox_rx_interrupt(mbox);
  173. return IRQ_HANDLED;
  174. }
  175. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  176. request_fn_proc *proc,
  177. void (*work) (struct work_struct *),
  178. void (*tasklet)(unsigned long))
  179. {
  180. struct request_queue *q;
  181. struct omap_mbox_queue *mq;
  182. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  183. if (!mq)
  184. return NULL;
  185. spin_lock_init(&mq->lock);
  186. q = blk_init_queue(proc, &mq->lock);
  187. if (!q)
  188. goto error;
  189. q->queuedata = mbox;
  190. mq->queue = q;
  191. if (work)
  192. INIT_WORK(&mq->work, work);
  193. if (tasklet)
  194. tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
  195. return mq;
  196. error:
  197. kfree(mq);
  198. return NULL;
  199. }
  200. static void mbox_queue_free(struct omap_mbox_queue *q)
  201. {
  202. blk_cleanup_queue(q->queue);
  203. kfree(q);
  204. }
  205. static int omap_mbox_startup(struct omap_mbox *mbox)
  206. {
  207. int ret = 0;
  208. struct omap_mbox_queue *mq;
  209. if (likely(mbox->ops->startup)) {
  210. mutex_lock(&mbox_configured_lock);
  211. if (!mbox_configured)
  212. ret = mbox->ops->startup(mbox);
  213. if (unlikely(ret)) {
  214. mutex_unlock(&mbox_configured_lock);
  215. return ret;
  216. }
  217. mbox_configured++;
  218. mutex_unlock(&mbox_configured_lock);
  219. }
  220. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  221. mbox->name, mbox);
  222. if (unlikely(ret)) {
  223. printk(KERN_ERR
  224. "failed to register mailbox interrupt:%d\n", ret);
  225. goto fail_request_irq;
  226. }
  227. mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
  228. if (!mq) {
  229. ret = -ENOMEM;
  230. goto fail_alloc_txq;
  231. }
  232. mbox->txq = mq;
  233. mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
  234. if (!mq) {
  235. ret = -ENOMEM;
  236. goto fail_alloc_rxq;
  237. }
  238. mbox->rxq = mq;
  239. return 0;
  240. fail_alloc_rxq:
  241. mbox_queue_free(mbox->txq);
  242. fail_alloc_txq:
  243. free_irq(mbox->irq, mbox);
  244. fail_request_irq:
  245. if (unlikely(mbox->ops->shutdown))
  246. mbox->ops->shutdown(mbox);
  247. return ret;
  248. }
  249. static void omap_mbox_fini(struct omap_mbox *mbox)
  250. {
  251. free_irq(mbox->irq, mbox);
  252. tasklet_kill(&mbox->txq->tasklet);
  253. flush_work(&mbox->rxq->work);
  254. mbox_queue_free(mbox->txq);
  255. mbox_queue_free(mbox->rxq);
  256. if (unlikely(mbox->ops->shutdown)) {
  257. mutex_lock(&mbox_configured_lock);
  258. if (mbox_configured > 0)
  259. mbox_configured--;
  260. if (!mbox_configured)
  261. mbox->ops->shutdown(mbox);
  262. mutex_unlock(&mbox_configured_lock);
  263. }
  264. }
  265. static struct omap_mbox **find_mboxes(const char *name)
  266. {
  267. struct omap_mbox **p;
  268. for (p = &mboxes; *p; p = &(*p)->next) {
  269. if (strcmp((*p)->name, name) == 0)
  270. break;
  271. }
  272. return p;
  273. }
  274. struct omap_mbox *omap_mbox_get(const char *name)
  275. {
  276. struct omap_mbox *mbox;
  277. int ret;
  278. read_lock(&mboxes_lock);
  279. mbox = *(find_mboxes(name));
  280. if (mbox == NULL) {
  281. read_unlock(&mboxes_lock);
  282. return ERR_PTR(-ENOENT);
  283. }
  284. read_unlock(&mboxes_lock);
  285. ret = omap_mbox_startup(mbox);
  286. if (ret)
  287. return ERR_PTR(-ENODEV);
  288. return mbox;
  289. }
  290. EXPORT_SYMBOL(omap_mbox_get);
  291. void omap_mbox_put(struct omap_mbox *mbox)
  292. {
  293. omap_mbox_fini(mbox);
  294. }
  295. EXPORT_SYMBOL(omap_mbox_put);
  296. int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
  297. {
  298. int ret = 0;
  299. struct omap_mbox **tmp;
  300. if (!mbox)
  301. return -EINVAL;
  302. if (mbox->next)
  303. return -EBUSY;
  304. write_lock(&mboxes_lock);
  305. tmp = find_mboxes(mbox->name);
  306. if (*tmp) {
  307. ret = -EBUSY;
  308. write_unlock(&mboxes_lock);
  309. goto err_find;
  310. }
  311. *tmp = mbox;
  312. write_unlock(&mboxes_lock);
  313. return 0;
  314. err_find:
  315. return ret;
  316. }
  317. EXPORT_SYMBOL(omap_mbox_register);
  318. int omap_mbox_unregister(struct omap_mbox *mbox)
  319. {
  320. struct omap_mbox **tmp;
  321. write_lock(&mboxes_lock);
  322. tmp = &mboxes;
  323. while (*tmp) {
  324. if (mbox == *tmp) {
  325. *tmp = mbox->next;
  326. mbox->next = NULL;
  327. write_unlock(&mboxes_lock);
  328. return 0;
  329. }
  330. tmp = &(*tmp)->next;
  331. }
  332. write_unlock(&mboxes_lock);
  333. return -EINVAL;
  334. }
  335. EXPORT_SYMBOL(omap_mbox_unregister);
  336. static int __init omap_mbox_init(void)
  337. {
  338. mboxd = create_workqueue("mboxd");
  339. if (!mboxd)
  340. return -ENOMEM;
  341. return 0;
  342. }
  343. module_init(omap_mbox_init);
  344. static void __exit omap_mbox_exit(void)
  345. {
  346. destroy_workqueue(mboxd);
  347. }
  348. module_exit(omap_mbox_exit);
  349. MODULE_LICENSE("GPL v2");
  350. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  351. MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");