mailbox.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 <asm/io.h>
  33. #include <asm/arch/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. blkdev_dequeue_request(rq);
  104. end_that_request_last(rq, 0);
  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. spin_lock_irqsave(q->queue_lock, flags);
  132. blkdev_dequeue_request(rq);
  133. end_that_request_last(rq, 0);
  134. spin_unlock_irqrestore(q->queue_lock, flags);
  135. mbox->rxq->callback((void *)msg);
  136. }
  137. }
  138. /*
  139. * Mailbox interrupt handler
  140. */
  141. static void mbox_txq_fn(struct request_queue * q)
  142. {
  143. }
  144. static void mbox_rxq_fn(struct request_queue * q)
  145. {
  146. }
  147. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  148. {
  149. disable_mbox_irq(mbox, IRQ_TX);
  150. ack_mbox_irq(mbox, IRQ_TX);
  151. schedule_work(&mbox->txq->work);
  152. }
  153. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  154. {
  155. struct request *rq;
  156. mbox_msg_t msg;
  157. struct request_queue *q = mbox->rxq->queue;
  158. disable_mbox_irq(mbox, IRQ_RX);
  159. while (!mbox_fifo_empty(mbox)) {
  160. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  161. if (unlikely(!rq))
  162. goto nomem;
  163. msg = mbox_fifo_read(mbox);
  164. rq->data = (void *)msg;
  165. if (unlikely(mbox_seq_test(mbox, msg))) {
  166. pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
  167. if (mbox->err_notify)
  168. mbox->err_notify();
  169. }
  170. blk_insert_request(q, rq, 0, NULL);
  171. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  172. break;
  173. }
  174. /* no more messages in the fifo. clear IRQ source. */
  175. ack_mbox_irq(mbox, IRQ_RX);
  176. enable_mbox_irq(mbox, IRQ_RX);
  177. nomem:
  178. schedule_work(&mbox->rxq->work);
  179. }
  180. static irqreturn_t mbox_interrupt(int irq, void *p)
  181. {
  182. struct omap_mbox *mbox = (struct omap_mbox *)p;
  183. if (is_mbox_irq(mbox, IRQ_TX))
  184. __mbox_tx_interrupt(mbox);
  185. if (is_mbox_irq(mbox, IRQ_RX))
  186. __mbox_rx_interrupt(mbox);
  187. return IRQ_HANDLED;
  188. }
  189. /*
  190. * sysfs files
  191. */
  192. static ssize_t
  193. omap_mbox_write(struct device *dev, struct device_attribute *attr,
  194. const char * buf, size_t count)
  195. {
  196. int ret;
  197. mbox_msg_t *p = (mbox_msg_t *)buf;
  198. struct omap_mbox *mbox = dev_get_drvdata(dev);
  199. for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
  200. ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
  201. if (ret)
  202. return -EAGAIN;
  203. p++;
  204. }
  205. return (size_t)((char *)p - buf);
  206. }
  207. static ssize_t
  208. omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. unsigned long flags;
  211. struct request *rq;
  212. mbox_msg_t *p = (mbox_msg_t *) buf;
  213. struct omap_mbox *mbox = dev_get_drvdata(dev);
  214. struct request_queue *q = mbox->rxq->queue;
  215. while (1) {
  216. spin_lock_irqsave(q->queue_lock, flags);
  217. rq = elv_next_request(q);
  218. spin_unlock_irqrestore(q->queue_lock, flags);
  219. if (!rq)
  220. break;
  221. *p = (mbox_msg_t) rq->data;
  222. spin_lock_irqsave(q->queue_lock, flags);
  223. blkdev_dequeue_request(rq);
  224. end_that_request_last(rq, 0);
  225. spin_unlock_irqrestore(q->queue_lock, flags);
  226. if (unlikely(mbox_seq_test(mbox, *p))) {
  227. pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
  228. continue;
  229. }
  230. p++;
  231. }
  232. pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
  233. return (size_t) ((char *)p - buf);
  234. }
  235. static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
  236. static ssize_t mbox_show(struct class *class, char *buf)
  237. {
  238. return sprintf(buf, "mbox");
  239. }
  240. static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
  241. static struct class omap_mbox_class = {
  242. .name = "omap_mbox",
  243. };
  244. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  245. request_fn_proc * proc,
  246. void (*work) (struct work_struct *))
  247. {
  248. struct request_queue *q;
  249. struct omap_mbox_queue *mq;
  250. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  251. if (!mq)
  252. return NULL;
  253. spin_lock_init(&mq->lock);
  254. q = blk_init_queue(proc, &mq->lock);
  255. if (!q)
  256. goto error;
  257. q->queuedata = mbox;
  258. mq->queue = q;
  259. INIT_WORK(&mq->work, work);
  260. return mq;
  261. error:
  262. kfree(mq);
  263. return NULL;
  264. }
  265. static void mbox_queue_free(struct omap_mbox_queue *q)
  266. {
  267. blk_cleanup_queue(q->queue);
  268. kfree(q);
  269. }
  270. static int omap_mbox_init(struct omap_mbox *mbox)
  271. {
  272. int ret;
  273. struct omap_mbox_queue *mq;
  274. if (likely(mbox->ops->startup)) {
  275. ret = mbox->ops->startup(mbox);
  276. if (unlikely(ret))
  277. return ret;
  278. }
  279. mbox->dev.class = &omap_mbox_class;
  280. strlcpy(mbox->dev.bus_id, mbox->name, KOBJ_NAME_LEN);
  281. dev_set_drvdata(&mbox->dev, mbox);
  282. ret = device_register(&mbox->dev);
  283. if (unlikely(ret))
  284. goto fail_device_reg;
  285. ret = device_create_file(&mbox->dev, &dev_attr_mbox);
  286. if (unlikely(ret)) {
  287. printk(KERN_ERR
  288. "device_create_file failed: %d\n", ret);
  289. goto fail_create_mbox;
  290. }
  291. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
  292. mbox->name, mbox);
  293. if (unlikely(ret)) {
  294. printk(KERN_ERR
  295. "failed to register mailbox interrupt:%d\n", ret);
  296. goto fail_request_irq;
  297. }
  298. enable_mbox_irq(mbox, IRQ_RX);
  299. mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
  300. if (!mq) {
  301. ret = -ENOMEM;
  302. goto fail_alloc_txq;
  303. }
  304. mbox->txq = mq;
  305. mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
  306. if (!mq) {
  307. ret = -ENOMEM;
  308. goto fail_alloc_rxq;
  309. }
  310. mbox->rxq = mq;
  311. return 0;
  312. fail_alloc_rxq:
  313. mbox_queue_free(mbox->txq);
  314. fail_alloc_txq:
  315. free_irq(mbox->irq, mbox);
  316. fail_request_irq:
  317. device_remove_file(&mbox->dev, &dev_attr_mbox);
  318. fail_create_mbox:
  319. device_unregister(&mbox->dev);
  320. fail_device_reg:
  321. if (unlikely(mbox->ops->shutdown))
  322. mbox->ops->shutdown(mbox);
  323. return ret;
  324. }
  325. static void omap_mbox_fini(struct omap_mbox *mbox)
  326. {
  327. mbox_queue_free(mbox->txq);
  328. mbox_queue_free(mbox->rxq);
  329. free_irq(mbox->irq, mbox);
  330. device_remove_file(&mbox->dev, &dev_attr_mbox);
  331. class_unregister(&omap_mbox_class);
  332. if (unlikely(mbox->ops->shutdown))
  333. mbox->ops->shutdown(mbox);
  334. }
  335. static struct omap_mbox **find_mboxes(const char *name)
  336. {
  337. struct omap_mbox **p;
  338. for (p = &mboxes; *p; p = &(*p)->next) {
  339. if (strcmp((*p)->name, name) == 0)
  340. break;
  341. }
  342. return p;
  343. }
  344. struct omap_mbox *omap_mbox_get(const char *name)
  345. {
  346. struct omap_mbox *mbox;
  347. int ret;
  348. read_lock(&mboxes_lock);
  349. mbox = *(find_mboxes(name));
  350. if (mbox == NULL) {
  351. read_unlock(&mboxes_lock);
  352. return ERR_PTR(-ENOENT);
  353. }
  354. read_unlock(&mboxes_lock);
  355. ret = omap_mbox_init(mbox);
  356. if (ret)
  357. return ERR_PTR(-ENODEV);
  358. return mbox;
  359. }
  360. EXPORT_SYMBOL(omap_mbox_get);
  361. void omap_mbox_put(struct omap_mbox *mbox)
  362. {
  363. omap_mbox_fini(mbox);
  364. }
  365. EXPORT_SYMBOL(omap_mbox_put);
  366. int omap_mbox_register(struct omap_mbox *mbox)
  367. {
  368. int ret = 0;
  369. struct omap_mbox **tmp;
  370. if (!mbox)
  371. return -EINVAL;
  372. if (mbox->next)
  373. return -EBUSY;
  374. write_lock(&mboxes_lock);
  375. tmp = find_mboxes(mbox->name);
  376. if (*tmp)
  377. ret = -EBUSY;
  378. else
  379. *tmp = mbox;
  380. write_unlock(&mboxes_lock);
  381. return ret;
  382. }
  383. EXPORT_SYMBOL(omap_mbox_register);
  384. int omap_mbox_unregister(struct omap_mbox *mbox)
  385. {
  386. struct omap_mbox **tmp;
  387. write_lock(&mboxes_lock);
  388. tmp = &mboxes;
  389. while (*tmp) {
  390. if (mbox == *tmp) {
  391. *tmp = mbox->next;
  392. mbox->next = NULL;
  393. write_unlock(&mboxes_lock);
  394. return 0;
  395. }
  396. tmp = &(*tmp)->next;
  397. }
  398. write_unlock(&mboxes_lock);
  399. return -EINVAL;
  400. }
  401. EXPORT_SYMBOL(omap_mbox_unregister);
  402. static int __init omap_mbox_class_init(void)
  403. {
  404. int ret = class_register(&omap_mbox_class);
  405. if (!ret)
  406. ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
  407. return ret;
  408. }
  409. static void __exit omap_mbox_class_exit(void)
  410. {
  411. class_remove_file(&omap_mbox_class, &class_attr_mbox);
  412. class_unregister(&omap_mbox_class);
  413. }
  414. subsys_initcall(omap_mbox_class_init);
  415. module_exit(omap_mbox_class_exit);
  416. MODULE_LICENSE("GPL");