mailbox.c 12 KB

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