mailbox.c 12 KB

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