mailbox.c 8.3 KB

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