mailbox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /*
  2. * OMAP mailbox driver
  3. *
  4. * Copyright (C) 2006 Nokia Corporation. All rights reserved.
  5. *
  6. * Contact: Toshihiro Kobayashi <toshihiro.kobayashi@nokia.com>
  7. * Restructured by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * version 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. #include <linux/init.h>
  25. #include <linux/module.h>
  26. #include <linux/sched.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/device.h>
  29. #include <linux/blkdev.h>
  30. #include <linux/err.h>
  31. #include <linux/delay.h>
  32. #include <linux/io.h>
  33. #include <mach/mailbox.h>
  34. #include "mailbox.h"
  35. static struct omap_mbox *mboxes;
  36. static DEFINE_RWLOCK(mboxes_lock);
  37. /* Mailbox Sequence Bit function */
  38. void omap_mbox_init_seq(struct omap_mbox *mbox)
  39. {
  40. mbox_seq_init(mbox);
  41. }
  42. EXPORT_SYMBOL(omap_mbox_init_seq);
  43. /*
  44. * message sender
  45. */
  46. static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
  47. {
  48. int ret = 0, i = 1000;
  49. while (mbox_fifo_full(mbox)) {
  50. if (mbox->ops->type == OMAP_MBOX_TYPE2)
  51. return -1;
  52. if (--i == 0)
  53. return -1;
  54. udelay(1);
  55. }
  56. if (arg && mbox->txq->callback) {
  57. ret = mbox->txq->callback(arg);
  58. if (ret)
  59. goto out;
  60. }
  61. mbox_seq_toggle(mbox, &msg);
  62. mbox_fifo_write(mbox, msg);
  63. out:
  64. return ret;
  65. }
  66. int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
  67. {
  68. struct request *rq;
  69. struct request_queue *q = mbox->txq->queue;
  70. int ret = 0;
  71. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  72. if (unlikely(!rq)) {
  73. ret = -ENOMEM;
  74. goto fail;
  75. }
  76. rq->data = (void *)msg;
  77. blk_insert_request(q, rq, 0, arg);
  78. schedule_work(&mbox->txq->work);
  79. fail:
  80. return ret;
  81. }
  82. EXPORT_SYMBOL(omap_mbox_msg_send);
  83. static void mbox_tx_work(struct work_struct *work)
  84. {
  85. int ret;
  86. struct request *rq;
  87. struct omap_mbox_queue *mq = container_of(work,
  88. struct omap_mbox_queue, work);
  89. struct omap_mbox *mbox = mq->queue->queuedata;
  90. struct request_queue *q = mbox->txq->queue;
  91. while (1) {
  92. spin_lock(q->queue_lock);
  93. rq = elv_next_request(q);
  94. spin_unlock(q->queue_lock);
  95. if (!rq)
  96. break;
  97. ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
  98. if (ret) {
  99. enable_mbox_irq(mbox, IRQ_TX);
  100. return;
  101. }
  102. spin_lock(q->queue_lock);
  103. if (__blk_end_request(rq, 0, 0))
  104. BUG();
  105. spin_unlock(q->queue_lock);
  106. }
  107. }
  108. /*
  109. * Message receiver(workqueue)
  110. */
  111. static void mbox_rx_work(struct work_struct *work)
  112. {
  113. struct omap_mbox_queue *mq =
  114. container_of(work, struct omap_mbox_queue, work);
  115. struct omap_mbox *mbox = mq->queue->queuedata;
  116. struct request_queue *q = mbox->rxq->queue;
  117. struct request *rq;
  118. mbox_msg_t msg;
  119. unsigned long flags;
  120. if (mbox->rxq->callback == NULL) {
  121. sysfs_notify(&mbox->dev.kobj, NULL, "mbox");
  122. return;
  123. }
  124. while (1) {
  125. spin_lock_irqsave(q->queue_lock, flags);
  126. rq = elv_next_request(q);
  127. spin_unlock_irqrestore(q->queue_lock, flags);
  128. if (!rq)
  129. break;
  130. msg = (mbox_msg_t) rq->data;
  131. if (blk_end_request(rq, 0, 0))
  132. BUG();
  133. mbox->rxq->callback((void *)msg);
  134. }
  135. }
  136. /*
  137. * Mailbox interrupt handler
  138. */
  139. static void mbox_txq_fn(struct request_queue * q)
  140. {
  141. }
  142. static void mbox_rxq_fn(struct request_queue * q)
  143. {
  144. }
  145. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  146. {
  147. disable_mbox_irq(mbox, IRQ_TX);
  148. ack_mbox_irq(mbox, IRQ_TX);
  149. schedule_work(&mbox->txq->work);
  150. }
  151. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  152. {
  153. struct request *rq;
  154. mbox_msg_t msg;
  155. struct request_queue *q = mbox->rxq->queue;
  156. disable_mbox_irq(mbox, IRQ_RX);
  157. while (!mbox_fifo_empty(mbox)) {
  158. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  159. if (unlikely(!rq))
  160. goto nomem;
  161. msg = mbox_fifo_read(mbox);
  162. rq->data = (void *)msg;
  163. if (unlikely(mbox_seq_test(mbox, msg))) {
  164. pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
  165. if (mbox->err_notify)
  166. mbox->err_notify();
  167. }
  168. blk_insert_request(q, rq, 0, NULL);
  169. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  170. break;
  171. }
  172. /* no more messages in the fifo. clear IRQ source. */
  173. ack_mbox_irq(mbox, IRQ_RX);
  174. enable_mbox_irq(mbox, IRQ_RX);
  175. nomem:
  176. schedule_work(&mbox->rxq->work);
  177. }
  178. static irqreturn_t mbox_interrupt(int irq, void *p)
  179. {
  180. struct omap_mbox *mbox = p;
  181. if (is_mbox_irq(mbox, IRQ_TX))
  182. __mbox_tx_interrupt(mbox);
  183. if (is_mbox_irq(mbox, IRQ_RX))
  184. __mbox_rx_interrupt(mbox);
  185. return IRQ_HANDLED;
  186. }
  187. /*
  188. * sysfs files
  189. */
  190. static ssize_t
  191. omap_mbox_write(struct device *dev, struct device_attribute *attr,
  192. const char * buf, size_t count)
  193. {
  194. int ret;
  195. mbox_msg_t *p = (mbox_msg_t *)buf;
  196. struct omap_mbox *mbox = dev_get_drvdata(dev);
  197. for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
  198. ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
  199. if (ret)
  200. return -EAGAIN;
  201. p++;
  202. }
  203. return (size_t)((char *)p - buf);
  204. }
  205. static ssize_t
  206. omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
  207. {
  208. unsigned long flags;
  209. struct request *rq;
  210. mbox_msg_t *p = (mbox_msg_t *) buf;
  211. struct omap_mbox *mbox = dev_get_drvdata(dev);
  212. struct request_queue *q = mbox->rxq->queue;
  213. while (1) {
  214. spin_lock_irqsave(q->queue_lock, flags);
  215. rq = elv_next_request(q);
  216. spin_unlock_irqrestore(q->queue_lock, flags);
  217. if (!rq)
  218. break;
  219. *p = (mbox_msg_t) rq->data;
  220. if (blk_end_request(rq, 0, 0))
  221. BUG();
  222. if (unlikely(mbox_seq_test(mbox, *p))) {
  223. pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
  224. continue;
  225. }
  226. p++;
  227. }
  228. pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
  229. return (size_t) ((char *)p - buf);
  230. }
  231. static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
  232. static ssize_t mbox_show(struct class *class, char *buf)
  233. {
  234. return sprintf(buf, "mbox");
  235. }
  236. static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
  237. static struct class omap_mbox_class = {
  238. .name = "omap_mbox",
  239. };
  240. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  241. request_fn_proc * proc,
  242. void (*work) (struct work_struct *))
  243. {
  244. struct request_queue *q;
  245. struct omap_mbox_queue *mq;
  246. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  247. if (!mq)
  248. return NULL;
  249. spin_lock_init(&mq->lock);
  250. q = blk_init_queue(proc, &mq->lock);
  251. if (!q)
  252. goto error;
  253. q->queuedata = mbox;
  254. mq->queue = q;
  255. INIT_WORK(&mq->work, work);
  256. return mq;
  257. error:
  258. kfree(mq);
  259. return NULL;
  260. }
  261. static void mbox_queue_free(struct omap_mbox_queue *q)
  262. {
  263. blk_cleanup_queue(q->queue);
  264. kfree(q);
  265. }
  266. static int omap_mbox_init(struct omap_mbox *mbox)
  267. {
  268. int ret;
  269. struct omap_mbox_queue *mq;
  270. if (likely(mbox->ops->startup)) {
  271. ret = mbox->ops->startup(mbox);
  272. if (unlikely(ret))
  273. return ret;
  274. }
  275. mbox->dev.class = &omap_mbox_class;
  276. dev_set_name(&mbox->dev, "%s", mbox->name);
  277. dev_set_drvdata(&mbox->dev, mbox);
  278. ret = device_register(&mbox->dev);
  279. if (unlikely(ret))
  280. goto fail_device_reg;
  281. ret = device_create_file(&mbox->dev, &dev_attr_mbox);
  282. if (unlikely(ret)) {
  283. printk(KERN_ERR
  284. "device_create_file failed: %d\n", ret);
  285. goto fail_create_mbox;
  286. }
  287. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
  288. mbox->name, mbox);
  289. if (unlikely(ret)) {
  290. printk(KERN_ERR
  291. "failed to register mailbox interrupt:%d\n", ret);
  292. goto fail_request_irq;
  293. }
  294. mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
  295. if (!mq) {
  296. ret = -ENOMEM;
  297. goto fail_alloc_txq;
  298. }
  299. mbox->txq = mq;
  300. mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
  301. if (!mq) {
  302. ret = -ENOMEM;
  303. goto fail_alloc_rxq;
  304. }
  305. mbox->rxq = mq;
  306. return 0;
  307. fail_alloc_rxq:
  308. mbox_queue_free(mbox->txq);
  309. fail_alloc_txq:
  310. free_irq(mbox->irq, mbox);
  311. fail_request_irq:
  312. device_remove_file(&mbox->dev, &dev_attr_mbox);
  313. fail_create_mbox:
  314. device_unregister(&mbox->dev);
  315. fail_device_reg:
  316. if (unlikely(mbox->ops->shutdown))
  317. mbox->ops->shutdown(mbox);
  318. return ret;
  319. }
  320. static void omap_mbox_fini(struct omap_mbox *mbox)
  321. {
  322. mbox_queue_free(mbox->txq);
  323. mbox_queue_free(mbox->rxq);
  324. free_irq(mbox->irq, mbox);
  325. device_remove_file(&mbox->dev, &dev_attr_mbox);
  326. class_unregister(&omap_mbox_class);
  327. if (unlikely(mbox->ops->shutdown))
  328. mbox->ops->shutdown(mbox);
  329. }
  330. static struct omap_mbox **find_mboxes(const char *name)
  331. {
  332. struct omap_mbox **p;
  333. for (p = &mboxes; *p; p = &(*p)->next) {
  334. if (strcmp((*p)->name, name) == 0)
  335. break;
  336. }
  337. return p;
  338. }
  339. struct omap_mbox *omap_mbox_get(const char *name)
  340. {
  341. struct omap_mbox *mbox;
  342. int ret;
  343. read_lock(&mboxes_lock);
  344. mbox = *(find_mboxes(name));
  345. if (mbox == NULL) {
  346. read_unlock(&mboxes_lock);
  347. return ERR_PTR(-ENOENT);
  348. }
  349. read_unlock(&mboxes_lock);
  350. ret = omap_mbox_init(mbox);
  351. if (ret)
  352. return ERR_PTR(-ENODEV);
  353. return mbox;
  354. }
  355. EXPORT_SYMBOL(omap_mbox_get);
  356. void omap_mbox_put(struct omap_mbox *mbox)
  357. {
  358. omap_mbox_fini(mbox);
  359. }
  360. EXPORT_SYMBOL(omap_mbox_put);
  361. int omap_mbox_register(struct omap_mbox *mbox)
  362. {
  363. int ret = 0;
  364. struct omap_mbox **tmp;
  365. if (!mbox)
  366. return -EINVAL;
  367. if (mbox->next)
  368. return -EBUSY;
  369. write_lock(&mboxes_lock);
  370. tmp = find_mboxes(mbox->name);
  371. if (*tmp)
  372. ret = -EBUSY;
  373. else
  374. *tmp = mbox;
  375. write_unlock(&mboxes_lock);
  376. return ret;
  377. }
  378. EXPORT_SYMBOL(omap_mbox_register);
  379. int omap_mbox_unregister(struct omap_mbox *mbox)
  380. {
  381. struct omap_mbox **tmp;
  382. write_lock(&mboxes_lock);
  383. tmp = &mboxes;
  384. while (*tmp) {
  385. if (mbox == *tmp) {
  386. *tmp = mbox->next;
  387. mbox->next = NULL;
  388. write_unlock(&mboxes_lock);
  389. return 0;
  390. }
  391. tmp = &(*tmp)->next;
  392. }
  393. write_unlock(&mboxes_lock);
  394. return -EINVAL;
  395. }
  396. EXPORT_SYMBOL(omap_mbox_unregister);
  397. static int __init omap_mbox_class_init(void)
  398. {
  399. int ret = class_register(&omap_mbox_class);
  400. if (!ret)
  401. ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
  402. return ret;
  403. }
  404. static void __exit omap_mbox_class_exit(void)
  405. {
  406. class_remove_file(&omap_mbox_class, &class_attr_mbox);
  407. class_unregister(&omap_mbox_class);
  408. }
  409. subsys_initcall(omap_mbox_class_init);
  410. module_exit(omap_mbox_class_exit);
  411. MODULE_LICENSE("GPL");