mailbox.c 8.4 KB

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