mailbox.c 12 KB

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