mailbox.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/init.h>
  24. #include <linux/module.h>
  25. #include <linux/sched.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/device.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/err.h>
  30. #include <linux/delay.h>
  31. #include <linux/io.h>
  32. #include <mach/mailbox.h>
  33. static struct omap_mbox *mboxes;
  34. static DEFINE_RWLOCK(mboxes_lock);
  35. /*
  36. * Mailbox sequence bit API
  37. */
  38. #if defined(CONFIG_ARCH_OMAP1)
  39. # define MBOX_USE_SEQ_BIT
  40. #elif defined(CONFIG_ARCH_OMAP2)
  41. # define MBOX_USE_SEQ_BIT
  42. #endif
  43. #ifdef MBOX_USE_SEQ_BIT
  44. /* seq_rcv should be initialized with any value other than
  45. * 0 and 1 << 31, to allow either value for the first
  46. * message. */
  47. static inline void mbox_seq_init(struct omap_mbox *mbox)
  48. {
  49. /* any value other than 0 and 1 << 31 */
  50. mbox->seq_rcv = 0xffffffff;
  51. }
  52. static inline void mbox_seq_toggle(struct omap_mbox *mbox, mbox_msg_t * msg)
  53. {
  54. /* add seq_snd to msg */
  55. *msg = (*msg & 0x7fffffff) | mbox->seq_snd;
  56. /* flip seq_snd */
  57. mbox->seq_snd ^= 1 << 31;
  58. }
  59. static inline int mbox_seq_test(struct omap_mbox *mbox, mbox_msg_t msg)
  60. {
  61. mbox_msg_t seq = msg & (1 << 31);
  62. if (seq == mbox->seq_rcv)
  63. return -1;
  64. mbox->seq_rcv = seq;
  65. return 0;
  66. }
  67. #else
  68. static inline void mbox_seq_init(struct omap_mbox *mbox)
  69. {
  70. }
  71. static inline void mbox_seq_toggle(struct omap_mbox *mbox, mbox_msg_t * msg)
  72. {
  73. }
  74. static inline int mbox_seq_test(struct omap_mbox *mbox, mbox_msg_t msg)
  75. {
  76. return 0;
  77. }
  78. #endif
  79. /* Mailbox FIFO handle functions */
  80. static inline mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
  81. {
  82. return mbox->ops->fifo_read(mbox);
  83. }
  84. static inline void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  85. {
  86. mbox->ops->fifo_write(mbox, msg);
  87. }
  88. static inline int mbox_fifo_empty(struct omap_mbox *mbox)
  89. {
  90. return mbox->ops->fifo_empty(mbox);
  91. }
  92. static inline int mbox_fifo_full(struct omap_mbox *mbox)
  93. {
  94. return mbox->ops->fifo_full(mbox);
  95. }
  96. /* Mailbox IRQ handle functions */
  97. static inline void enable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  98. {
  99. mbox->ops->enable_irq(mbox, irq);
  100. }
  101. static inline void disable_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  102. {
  103. mbox->ops->disable_irq(mbox, irq);
  104. }
  105. static inline void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  106. {
  107. if (mbox->ops->ack_irq)
  108. mbox->ops->ack_irq(mbox, irq);
  109. }
  110. static inline int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  111. {
  112. return mbox->ops->is_irq(mbox, irq);
  113. }
  114. /* Mailbox Sequence Bit function */
  115. void omap_mbox_init_seq(struct omap_mbox *mbox)
  116. {
  117. mbox_seq_init(mbox);
  118. }
  119. EXPORT_SYMBOL(omap_mbox_init_seq);
  120. /*
  121. * message sender
  122. */
  123. static int __mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void *arg)
  124. {
  125. int ret = 0, i = 1000;
  126. while (mbox_fifo_full(mbox)) {
  127. if (mbox->ops->type == OMAP_MBOX_TYPE2)
  128. return -1;
  129. if (--i == 0)
  130. return -1;
  131. udelay(1);
  132. }
  133. if (arg && mbox->txq->callback) {
  134. ret = mbox->txq->callback(arg);
  135. if (ret)
  136. goto out;
  137. }
  138. mbox_seq_toggle(mbox, &msg);
  139. mbox_fifo_write(mbox, msg);
  140. out:
  141. return ret;
  142. }
  143. int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg, void* arg)
  144. {
  145. struct request *rq;
  146. struct request_queue *q = mbox->txq->queue;
  147. int ret = 0;
  148. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  149. if (unlikely(!rq)) {
  150. ret = -ENOMEM;
  151. goto fail;
  152. }
  153. rq->data = (void *)msg;
  154. blk_insert_request(q, rq, 0, arg);
  155. schedule_work(&mbox->txq->work);
  156. fail:
  157. return ret;
  158. }
  159. EXPORT_SYMBOL(omap_mbox_msg_send);
  160. static void mbox_tx_work(struct work_struct *work)
  161. {
  162. int ret;
  163. struct request *rq;
  164. struct omap_mbox_queue *mq = container_of(work,
  165. struct omap_mbox_queue, work);
  166. struct omap_mbox *mbox = mq->queue->queuedata;
  167. struct request_queue *q = mbox->txq->queue;
  168. while (1) {
  169. spin_lock(q->queue_lock);
  170. rq = elv_next_request(q);
  171. spin_unlock(q->queue_lock);
  172. if (!rq)
  173. break;
  174. ret = __mbox_msg_send(mbox, (mbox_msg_t) rq->data, rq->special);
  175. if (ret) {
  176. enable_mbox_irq(mbox, IRQ_TX);
  177. return;
  178. }
  179. spin_lock(q->queue_lock);
  180. if (__blk_end_request(rq, 0, 0))
  181. BUG();
  182. spin_unlock(q->queue_lock);
  183. }
  184. }
  185. /*
  186. * Message receiver(workqueue)
  187. */
  188. static void mbox_rx_work(struct work_struct *work)
  189. {
  190. struct omap_mbox_queue *mq =
  191. container_of(work, struct omap_mbox_queue, work);
  192. struct omap_mbox *mbox = mq->queue->queuedata;
  193. struct request_queue *q = mbox->rxq->queue;
  194. struct request *rq;
  195. mbox_msg_t msg;
  196. unsigned long flags;
  197. if (mbox->rxq->callback == NULL) {
  198. sysfs_notify(&mbox->dev->kobj, NULL, "mbox");
  199. return;
  200. }
  201. while (1) {
  202. spin_lock_irqsave(q->queue_lock, flags);
  203. rq = elv_next_request(q);
  204. spin_unlock_irqrestore(q->queue_lock, flags);
  205. if (!rq)
  206. break;
  207. msg = (mbox_msg_t) rq->data;
  208. if (blk_end_request(rq, 0, 0))
  209. BUG();
  210. mbox->rxq->callback((void *)msg);
  211. }
  212. }
  213. /*
  214. * Mailbox interrupt handler
  215. */
  216. static void mbox_txq_fn(struct request_queue * q)
  217. {
  218. }
  219. static void mbox_rxq_fn(struct request_queue * q)
  220. {
  221. }
  222. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  223. {
  224. disable_mbox_irq(mbox, IRQ_TX);
  225. ack_mbox_irq(mbox, IRQ_TX);
  226. schedule_work(&mbox->txq->work);
  227. }
  228. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  229. {
  230. struct request *rq;
  231. mbox_msg_t msg;
  232. struct request_queue *q = mbox->rxq->queue;
  233. disable_mbox_irq(mbox, IRQ_RX);
  234. while (!mbox_fifo_empty(mbox)) {
  235. rq = blk_get_request(q, WRITE, GFP_ATOMIC);
  236. if (unlikely(!rq))
  237. goto nomem;
  238. msg = mbox_fifo_read(mbox);
  239. rq->data = (void *)msg;
  240. if (unlikely(mbox_seq_test(mbox, msg))) {
  241. pr_info("mbox: Illegal seq bit!(%08x)\n", msg);
  242. if (mbox->err_notify)
  243. mbox->err_notify();
  244. }
  245. blk_insert_request(q, rq, 0, NULL);
  246. if (mbox->ops->type == OMAP_MBOX_TYPE1)
  247. break;
  248. }
  249. /* no more messages in the fifo. clear IRQ source. */
  250. ack_mbox_irq(mbox, IRQ_RX);
  251. enable_mbox_irq(mbox, IRQ_RX);
  252. nomem:
  253. schedule_work(&mbox->rxq->work);
  254. }
  255. static irqreturn_t mbox_interrupt(int irq, void *p)
  256. {
  257. struct omap_mbox *mbox = p;
  258. if (is_mbox_irq(mbox, IRQ_TX))
  259. __mbox_tx_interrupt(mbox);
  260. if (is_mbox_irq(mbox, IRQ_RX))
  261. __mbox_rx_interrupt(mbox);
  262. return IRQ_HANDLED;
  263. }
  264. /*
  265. * sysfs files
  266. */
  267. static ssize_t
  268. omap_mbox_write(struct device *dev, struct device_attribute *attr,
  269. const char * buf, size_t count)
  270. {
  271. int ret;
  272. mbox_msg_t *p = (mbox_msg_t *)buf;
  273. struct omap_mbox *mbox = dev_get_drvdata(dev);
  274. for (; count >= sizeof(mbox_msg_t); count -= sizeof(mbox_msg_t)) {
  275. ret = omap_mbox_msg_send(mbox, be32_to_cpu(*p), NULL);
  276. if (ret)
  277. return -EAGAIN;
  278. p++;
  279. }
  280. return (size_t)((char *)p - buf);
  281. }
  282. static ssize_t
  283. omap_mbox_read(struct device *dev, struct device_attribute *attr, char *buf)
  284. {
  285. unsigned long flags;
  286. struct request *rq;
  287. mbox_msg_t *p = (mbox_msg_t *) buf;
  288. struct omap_mbox *mbox = dev_get_drvdata(dev);
  289. struct request_queue *q = mbox->rxq->queue;
  290. while (1) {
  291. spin_lock_irqsave(q->queue_lock, flags);
  292. rq = elv_next_request(q);
  293. spin_unlock_irqrestore(q->queue_lock, flags);
  294. if (!rq)
  295. break;
  296. *p = (mbox_msg_t) rq->data;
  297. if (blk_end_request(rq, 0, 0))
  298. BUG();
  299. if (unlikely(mbox_seq_test(mbox, *p))) {
  300. pr_info("mbox: Illegal seq bit!(%08x) ignored\n", *p);
  301. continue;
  302. }
  303. p++;
  304. }
  305. pr_debug("%02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]);
  306. return (size_t) ((char *)p - buf);
  307. }
  308. static DEVICE_ATTR(mbox, S_IRUGO | S_IWUSR, omap_mbox_read, omap_mbox_write);
  309. static ssize_t mbox_show(struct class *class, char *buf)
  310. {
  311. return sprintf(buf, "mbox");
  312. }
  313. static CLASS_ATTR(mbox, S_IRUGO, mbox_show, NULL);
  314. static struct class omap_mbox_class = {
  315. .name = "omap-mailbox",
  316. };
  317. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  318. request_fn_proc * proc,
  319. void (*work) (struct work_struct *))
  320. {
  321. struct request_queue *q;
  322. struct omap_mbox_queue *mq;
  323. mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
  324. if (!mq)
  325. return NULL;
  326. spin_lock_init(&mq->lock);
  327. q = blk_init_queue(proc, &mq->lock);
  328. if (!q)
  329. goto error;
  330. q->queuedata = mbox;
  331. mq->queue = q;
  332. INIT_WORK(&mq->work, work);
  333. return mq;
  334. error:
  335. kfree(mq);
  336. return NULL;
  337. }
  338. static void mbox_queue_free(struct omap_mbox_queue *q)
  339. {
  340. blk_cleanup_queue(q->queue);
  341. kfree(q);
  342. }
  343. static int omap_mbox_init(struct omap_mbox *mbox)
  344. {
  345. int ret;
  346. struct omap_mbox_queue *mq;
  347. if (likely(mbox->ops->startup)) {
  348. ret = mbox->ops->startup(mbox);
  349. if (unlikely(ret))
  350. return ret;
  351. }
  352. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_DISABLED,
  353. mbox->name, mbox);
  354. if (unlikely(ret)) {
  355. printk(KERN_ERR
  356. "failed to register mailbox interrupt:%d\n", ret);
  357. goto fail_request_irq;
  358. }
  359. mq = mbox_queue_alloc(mbox, mbox_txq_fn, mbox_tx_work);
  360. if (!mq) {
  361. ret = -ENOMEM;
  362. goto fail_alloc_txq;
  363. }
  364. mbox->txq = mq;
  365. mq = mbox_queue_alloc(mbox, mbox_rxq_fn, mbox_rx_work);
  366. if (!mq) {
  367. ret = -ENOMEM;
  368. goto fail_alloc_rxq;
  369. }
  370. mbox->rxq = mq;
  371. return 0;
  372. fail_alloc_rxq:
  373. mbox_queue_free(mbox->txq);
  374. fail_alloc_txq:
  375. free_irq(mbox->irq, mbox);
  376. fail_request_irq:
  377. if (unlikely(mbox->ops->shutdown))
  378. mbox->ops->shutdown(mbox);
  379. return ret;
  380. }
  381. static void omap_mbox_fini(struct omap_mbox *mbox)
  382. {
  383. mbox_queue_free(mbox->txq);
  384. mbox_queue_free(mbox->rxq);
  385. free_irq(mbox->irq, mbox);
  386. if (unlikely(mbox->ops->shutdown))
  387. mbox->ops->shutdown(mbox);
  388. }
  389. static struct omap_mbox **find_mboxes(const char *name)
  390. {
  391. struct omap_mbox **p;
  392. for (p = &mboxes; *p; p = &(*p)->next) {
  393. if (strcmp((*p)->name, name) == 0)
  394. break;
  395. }
  396. return p;
  397. }
  398. struct omap_mbox *omap_mbox_get(const char *name)
  399. {
  400. struct omap_mbox *mbox;
  401. int ret;
  402. read_lock(&mboxes_lock);
  403. mbox = *(find_mboxes(name));
  404. if (mbox == NULL) {
  405. read_unlock(&mboxes_lock);
  406. return ERR_PTR(-ENOENT);
  407. }
  408. read_unlock(&mboxes_lock);
  409. ret = omap_mbox_init(mbox);
  410. if (ret)
  411. return ERR_PTR(-ENODEV);
  412. return mbox;
  413. }
  414. EXPORT_SYMBOL(omap_mbox_get);
  415. void omap_mbox_put(struct omap_mbox *mbox)
  416. {
  417. omap_mbox_fini(mbox);
  418. }
  419. EXPORT_SYMBOL(omap_mbox_put);
  420. int omap_mbox_register(struct device *parent, struct omap_mbox *mbox)
  421. {
  422. int ret = 0;
  423. struct omap_mbox **tmp;
  424. if (!mbox)
  425. return -EINVAL;
  426. if (mbox->next)
  427. return -EBUSY;
  428. mbox->dev = device_create(&omap_mbox_class,
  429. parent, 0, mbox, "%s", mbox->name);
  430. if (IS_ERR(mbox->dev))
  431. return PTR_ERR(mbox->dev);
  432. ret = device_create_file(mbox->dev, &dev_attr_mbox);
  433. if (ret)
  434. goto err_sysfs;
  435. write_lock(&mboxes_lock);
  436. tmp = find_mboxes(mbox->name);
  437. if (*tmp) {
  438. ret = -EBUSY;
  439. write_unlock(&mboxes_lock);
  440. goto err_find;
  441. }
  442. *tmp = mbox;
  443. write_unlock(&mboxes_lock);
  444. return 0;
  445. err_find:
  446. device_remove_file(mbox->dev, &dev_attr_mbox);
  447. err_sysfs:
  448. device_unregister(mbox->dev);
  449. return ret;
  450. }
  451. EXPORT_SYMBOL(omap_mbox_register);
  452. int omap_mbox_unregister(struct omap_mbox *mbox)
  453. {
  454. struct omap_mbox **tmp;
  455. write_lock(&mboxes_lock);
  456. tmp = &mboxes;
  457. while (*tmp) {
  458. if (mbox == *tmp) {
  459. *tmp = mbox->next;
  460. mbox->next = NULL;
  461. write_unlock(&mboxes_lock);
  462. device_remove_file(mbox->dev, &dev_attr_mbox);
  463. device_unregister(mbox->dev);
  464. return 0;
  465. }
  466. tmp = &(*tmp)->next;
  467. }
  468. write_unlock(&mboxes_lock);
  469. return -EINVAL;
  470. }
  471. EXPORT_SYMBOL(omap_mbox_unregister);
  472. static int __init omap_mbox_class_init(void)
  473. {
  474. int ret = class_register(&omap_mbox_class);
  475. if (!ret)
  476. ret = class_create_file(&omap_mbox_class, &class_attr_mbox);
  477. return ret;
  478. }
  479. static void __exit omap_mbox_class_exit(void)
  480. {
  481. class_remove_file(&omap_mbox_class, &class_attr_mbox);
  482. class_unregister(&omap_mbox_class);
  483. }
  484. subsys_initcall(omap_mbox_class_init);
  485. module_exit(omap_mbox_class_exit);
  486. MODULE_LICENSE("GPL v2");
  487. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  488. MODULE_AUTHOR("Toshihiro Kobayashi and Hiroshi DOYU");