mailbox.c 8.7 KB

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