mailbox.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. mbox->rxq->callback((void *)msg);
  128. }
  129. }
  130. /*
  131. * Mailbox interrupt handler
  132. */
  133. static void mbox_txq_fn(struct request_queue *q)
  134. {
  135. }
  136. static void mbox_rxq_fn(struct request_queue *q)
  137. {
  138. }
  139. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  140. {
  141. omap_mbox_disable_irq(mbox, IRQ_TX);
  142. ack_mbox_irq(mbox, IRQ_TX);
  143. tasklet_schedule(&mbox->txq->tasklet);
  144. }
  145. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  146. {
  147. struct request *rq;
  148. mbox_msg_t msg;
  149. struct request_queue *q = mbox->rxq->queue;
  150. while (!mbox_fifo_empty(mbox)) {
  151. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  152. if (unlikely(!rq))
  153. goto nomem;
  154. msg = mbox_fifo_read(mbox);
  155. blk_insert_request(q, rq, 0, (void *)msg);
  156. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  157. break;
  158. }
  159. /* no more messages in the fifo. clear IRQ source. */
  160. ack_mbox_irq(mbox, IRQ_RX);
  161. nomem:
  162. queue_work(mboxd, &mbox->rxq->work);
  163. }
  164. static irqreturn_t mbox_interrupt(int irq, void *p)
  165. {
  166. struct omap_mbox *mbox = p;
  167. if (is_mbox_irq(mbox, IRQ_TX))
  168. __mbox_tx_interrupt(mbox);
  169. if (is_mbox_irq(mbox, IRQ_RX))
  170. __mbox_rx_interrupt(mbox);
  171. return IRQ_HANDLED;
  172. }
  173. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  174. request_fn_proc *proc,
  175. void (*work) (struct work_struct *),
  176. void (*tasklet)(unsigned long))
  177. {
  178. struct request_queue *q;
  179. struct omap_mbox_queue *mq;
  180. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  181. if (!mq)
  182. return NULL;
  183. spin_lock_init(&mq->lock);
  184. q = blk_init_queue(proc, &mq->lock);
  185. if (!q)
  186. goto error;
  187. q->queuedata = mbox;
  188. mq->queue = q;
  189. if (work)
  190. INIT_WORK(&mq->work, work);
  191. if (tasklet)
  192. tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
  193. return mq;
  194. error:
  195. kfree(mq);
  196. return NULL;
  197. }
  198. static void mbox_queue_free(struct omap_mbox_queue *q)
  199. {
  200. blk_cleanup_queue(q->queue);
  201. kfree(q);
  202. }
  203. static int omap_mbox_startup(struct omap_mbox *mbox)
  204. {
  205. int ret = 0;
  206. struct omap_mbox_queue *mq;
  207. if (likely(mbox->ops->startup)) {
  208. write_lock(&mboxes_lock);
  209. if (!mbox_configured)
  210. ret = mbox->ops->startup(mbox);
  211. if (unlikely(ret)) {
  212. write_unlock(&mboxes_lock);
  213. return ret;
  214. }
  215. mbox_configured++;
  216. write_unlock(&mboxes_lock);
  217. }
  218. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  219. mbox->name, mbox);
  220. if (unlikely(ret)) {
  221. printk(KERN_ERR
  222. "failed to register mailbox interrupt:%d\n", ret);
  223. goto fail_request_irq;
  224. }
  225. mq = mbox_queue_alloc(mbox, mbox_txq_fn, NULL, mbox_tx_tasklet);
  226. if (!mq) {
  227. ret = -ENOMEM;
  228. goto fail_alloc_txq;
  229. }
  230. mbox->txq = mq;
  231. mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work, NULL);
  232. if (!mq) {
  233. ret = -ENOMEM;
  234. goto fail_alloc_rxq;
  235. }
  236. mbox->rxq = mq;
  237. return 0;
  238. fail_alloc_rxq:
  239. mbox_queue_free(mbox->txq);
  240. fail_alloc_txq:
  241. free_irq(mbox->irq, mbox);
  242. fail_request_irq:
  243. if (unlikely(mbox->ops->shutdown))
  244. mbox->ops->shutdown(mbox);
  245. return ret;
  246. }
  247. static void omap_mbox_fini(struct omap_mbox *mbox)
  248. {
  249. mbox_queue_free(mbox->txq);
  250. mbox_queue_free(mbox->rxq);
  251. free_irq(mbox->irq, mbox);
  252. if (unlikely(mbox->ops->shutdown)) {
  253. write_lock(&mboxes_lock);
  254. if (mbox_configured > 0)
  255. mbox_configured--;
  256. if (!mbox_configured)
  257. mbox->ops->shutdown(mbox);
  258. write_unlock(&mboxes_lock);
  259. }
  260. }
  261. static struct omap_mbox **find_mboxes(const char *name)
  262. {
  263. struct omap_mbox **p;
  264. for (p = &mboxes; *p; p = &(*p)->next) {
  265. if (strcmp((*p)->name, name) == 0)
  266. break;
  267. }
  268. return p;
  269. }
  270. struct omap_mbox *omap_mbox_get(const char *name)
  271. {
  272. struct omap_mbox *mbox;
  273. int ret;
  274. read_lock(&mboxes_lock);
  275. mbox = *(find_mboxes(name));
  276. if (mbox == NULL) {
  277. read_unlock(&mboxes_lock);
  278. return ERR_PTR(-ENOENT);
  279. }
  280. read_unlock(&mboxes_lock);
  281. ret = omap_mbox_startup(mbox);
  282. if (ret)
  283. return ERR_PTR(-ENODEV);
  284. return mbox;
  285. }
  286. EXPORT_SYMBOL(omap_mbox_get);
  287. void omap_mbox_put(struct omap_mbox *mbox)
  288. {
  289. omap_mbox_fini(mbox);
  290. }
  291. EXPORT_SYMBOL(omap_mbox_put);
  292. int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
  293. {
  294. int ret = 0;
  295. struct omap_mbox **tmp;
  296. if (!mbox)
  297. return -EINVAL;
  298. if (mbox->next)
  299. return -EBUSY;
  300. write_lock(&mboxes_lock);
  301. tmp = find_mboxes(mbox->name);
  302. if (*tmp) {
  303. ret = -EBUSY;
  304. write_unlock(&mboxes_lock);
  305. goto err_find;
  306. }
  307. *tmp = mbox;
  308. write_unlock(&mboxes_lock);
  309. return 0;
  310. err_find:
  311. return ret;
  312. }
  313. EXPORT_SYMBOL(omap_mbox_register);
  314. int omap_mbox_unregister(struct omap_mbox *mbox)
  315. {
  316. struct omap_mbox **tmp;
  317. write_lock(&mboxes_lock);
  318. tmp = &mboxes;
  319. while (*tmp) {
  320. if (mbox == *tmp) {
  321. *tmp = mbox->next;
  322. mbox->next = NULL;
  323. write_unlock(&mboxes_lock);
  324. return 0;
  325. }
  326. tmp = &(*tmp)->next;
  327. }
  328. write_unlock(&mboxes_lock);
  329. return -EINVAL;
  330. }
  331. EXPORT_SYMBOL(omap_mbox_unregister);
  332. static int __init omap_mbox_init(void)
  333. {
  334. mboxd = create_workqueue("mboxd");
  335. if (!mboxd)
  336. return -ENOMEM;
  337. return 0;
  338. }
  339. module_init(omap_mbox_init);
  340. static void __exit omap_mbox_exit(void)
  341. {
  342. destroy_workqueue(mboxd);
  343. }
  344. module_exit(omap_mbox_exit);
  345. MODULE_LICENSE("GPL v2");
  346. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  347. MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");