mailbox.c 8.7 KB

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