mailbox.c 8.2 KB

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