mailbox.c 8.4 KB

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