mailbox.c 12 KB

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