omap-mailbox.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * OMAP mailbox driver
  3. *
  4. * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
  5. * Copyright (C) 2013-2016 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
  8. * Suman Anna <s-anna@ti.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * version 2 as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. */
  19. #include <linux/interrupt.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <linux/kfifo.h>
  24. #include <linux/err.h>
  25. #include <linux/module.h>
  26. #include <linux/of_device.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/omap-mailbox.h>
  30. #include <linux/mailbox_controller.h>
  31. #include <linux/mailbox_client.h>
  32. #include "mailbox.h"
  33. #define MAILBOX_REVISION 0x000
  34. #define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
  35. #define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
  36. #define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
  37. #define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
  38. #define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
  39. #define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
  40. #define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
  41. #define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
  42. #define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \
  43. OMAP2_MAILBOX_IRQSTATUS(u))
  44. #define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \
  45. OMAP2_MAILBOX_IRQENABLE(u))
  46. #define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \
  47. : OMAP2_MAILBOX_IRQENABLE(u))
  48. #define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
  49. #define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
  50. /* Interrupt register configuration types */
  51. #define MBOX_INTR_CFG_TYPE1 0
  52. #define MBOX_INTR_CFG_TYPE2 1
  53. struct omap_mbox_fifo {
  54. unsigned long msg;
  55. unsigned long fifo_stat;
  56. unsigned long msg_stat;
  57. unsigned long irqenable;
  58. unsigned long irqstatus;
  59. unsigned long irqdisable;
  60. u32 intr_bit;
  61. };
  62. struct omap_mbox_queue {
  63. spinlock_t lock;
  64. struct kfifo fifo;
  65. struct work_struct work;
  66. struct omap_mbox *mbox;
  67. bool full;
  68. };
  69. struct omap_mbox_device {
  70. struct device *dev;
  71. struct mutex cfg_lock;
  72. void __iomem *mbox_base;
  73. u32 *irq_ctx;
  74. u32 num_users;
  75. u32 num_fifos;
  76. u32 intr_type;
  77. struct omap_mbox **mboxes;
  78. struct mbox_controller controller;
  79. struct list_head elem;
  80. };
  81. struct omap_mbox_fifo_info {
  82. int tx_id;
  83. int tx_usr;
  84. int tx_irq;
  85. int rx_id;
  86. int rx_usr;
  87. int rx_irq;
  88. const char *name;
  89. bool send_no_irq;
  90. };
  91. struct omap_mbox {
  92. const char *name;
  93. int irq;
  94. struct omap_mbox_queue *rxq;
  95. struct device *dev;
  96. struct omap_mbox_device *parent;
  97. struct omap_mbox_fifo tx_fifo;
  98. struct omap_mbox_fifo rx_fifo;
  99. u32 intr_type;
  100. struct mbox_chan *chan;
  101. bool send_no_irq;
  102. };
  103. /* global variables for the mailbox devices */
  104. static DEFINE_MUTEX(omap_mbox_devices_lock);
  105. static LIST_HEAD(omap_mbox_devices);
  106. static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
  107. module_param(mbox_kfifo_size, uint, S_IRUGO);
  108. MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
  109. static struct omap_mbox *mbox_chan_to_omap_mbox(struct mbox_chan *chan)
  110. {
  111. if (!chan || !chan->con_priv)
  112. return NULL;
  113. return (struct omap_mbox *)chan->con_priv;
  114. }
  115. static inline
  116. unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
  117. {
  118. return __raw_readl(mdev->mbox_base + ofs);
  119. }
  120. static inline
  121. void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
  122. {
  123. __raw_writel(val, mdev->mbox_base + ofs);
  124. }
  125. /* Mailbox FIFO handle functions */
  126. static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
  127. {
  128. struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
  129. return (mbox_msg_t)mbox_read_reg(mbox->parent, fifo->msg);
  130. }
  131. static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
  132. {
  133. struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
  134. mbox_write_reg(mbox->parent, msg, fifo->msg);
  135. }
  136. static int mbox_fifo_empty(struct omap_mbox *mbox)
  137. {
  138. struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
  139. return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
  140. }
  141. static int mbox_fifo_full(struct omap_mbox *mbox)
  142. {
  143. struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
  144. return mbox_read_reg(mbox->parent, fifo->fifo_stat);
  145. }
  146. /* Mailbox IRQ handle functions */
  147. static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  148. {
  149. struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
  150. &mbox->tx_fifo : &mbox->rx_fifo;
  151. u32 bit = fifo->intr_bit;
  152. u32 irqstatus = fifo->irqstatus;
  153. mbox_write_reg(mbox->parent, bit, irqstatus);
  154. /* Flush posted write for irq status to avoid spurious interrupts */
  155. mbox_read_reg(mbox->parent, irqstatus);
  156. }
  157. static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  158. {
  159. struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
  160. &mbox->tx_fifo : &mbox->rx_fifo;
  161. u32 bit = fifo->intr_bit;
  162. u32 irqenable = fifo->irqenable;
  163. u32 irqstatus = fifo->irqstatus;
  164. u32 enable = mbox_read_reg(mbox->parent, irqenable);
  165. u32 status = mbox_read_reg(mbox->parent, irqstatus);
  166. return (int)(enable & status & bit);
  167. }
  168. static void _omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  169. {
  170. u32 l;
  171. struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
  172. &mbox->tx_fifo : &mbox->rx_fifo;
  173. u32 bit = fifo->intr_bit;
  174. u32 irqenable = fifo->irqenable;
  175. l = mbox_read_reg(mbox->parent, irqenable);
  176. l |= bit;
  177. mbox_write_reg(mbox->parent, l, irqenable);
  178. }
  179. static void _omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
  180. {
  181. struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
  182. &mbox->tx_fifo : &mbox->rx_fifo;
  183. u32 bit = fifo->intr_bit;
  184. u32 irqdisable = fifo->irqdisable;
  185. /*
  186. * Read and update the interrupt configuration register for pre-OMAP4.
  187. * OMAP4 and later SoCs have a dedicated interrupt disabling register.
  188. */
  189. if (!mbox->intr_type)
  190. bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
  191. mbox_write_reg(mbox->parent, bit, irqdisable);
  192. }
  193. void omap_mbox_enable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
  194. {
  195. struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
  196. if (WARN_ON(!mbox))
  197. return;
  198. _omap_mbox_enable_irq(mbox, irq);
  199. }
  200. EXPORT_SYMBOL(omap_mbox_enable_irq);
  201. void omap_mbox_disable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
  202. {
  203. struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
  204. if (WARN_ON(!mbox))
  205. return;
  206. _omap_mbox_disable_irq(mbox, irq);
  207. }
  208. EXPORT_SYMBOL(omap_mbox_disable_irq);
  209. /*
  210. * Message receiver(workqueue)
  211. */
  212. static void mbox_rx_work(struct work_struct *work)
  213. {
  214. struct omap_mbox_queue *mq =
  215. container_of(work, struct omap_mbox_queue, work);
  216. mbox_msg_t msg;
  217. int len;
  218. while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
  219. len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  220. WARN_ON(len != sizeof(msg));
  221. mbox_chan_received_data(mq->mbox->chan, (void *)msg);
  222. spin_lock_irq(&mq->lock);
  223. if (mq->full) {
  224. mq->full = false;
  225. _omap_mbox_enable_irq(mq->mbox, IRQ_RX);
  226. }
  227. spin_unlock_irq(&mq->lock);
  228. }
  229. }
  230. /*
  231. * Mailbox interrupt handler
  232. */
  233. static void __mbox_tx_interrupt(struct omap_mbox *mbox)
  234. {
  235. _omap_mbox_disable_irq(mbox, IRQ_TX);
  236. ack_mbox_irq(mbox, IRQ_TX);
  237. mbox_chan_txdone(mbox->chan, 0);
  238. }
  239. static void __mbox_rx_interrupt(struct omap_mbox *mbox)
  240. {
  241. struct omap_mbox_queue *mq = mbox->rxq;
  242. mbox_msg_t msg;
  243. int len;
  244. while (!mbox_fifo_empty(mbox)) {
  245. if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
  246. _omap_mbox_disable_irq(mbox, IRQ_RX);
  247. mq->full = true;
  248. goto nomem;
  249. }
  250. msg = mbox_fifo_read(mbox);
  251. len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
  252. WARN_ON(len != sizeof(msg));
  253. }
  254. /* no more messages in the fifo. clear IRQ source. */
  255. ack_mbox_irq(mbox, IRQ_RX);
  256. nomem:
  257. schedule_work(&mbox->rxq->work);
  258. }
  259. static irqreturn_t mbox_interrupt(int irq, void *p)
  260. {
  261. struct omap_mbox *mbox = p;
  262. if (is_mbox_irq(mbox, IRQ_TX))
  263. __mbox_tx_interrupt(mbox);
  264. if (is_mbox_irq(mbox, IRQ_RX))
  265. __mbox_rx_interrupt(mbox);
  266. return IRQ_HANDLED;
  267. }
  268. static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
  269. void (*work)(struct work_struct *))
  270. {
  271. struct omap_mbox_queue *mq;
  272. if (!work)
  273. return NULL;
  274. mq = kzalloc(sizeof(*mq), GFP_KERNEL);
  275. if (!mq)
  276. return NULL;
  277. spin_lock_init(&mq->lock);
  278. if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
  279. goto error;
  280. INIT_WORK(&mq->work, work);
  281. return mq;
  282. error:
  283. kfree(mq);
  284. return NULL;
  285. }
  286. static void mbox_queue_free(struct omap_mbox_queue *q)
  287. {
  288. kfifo_free(&q->fifo);
  289. kfree(q);
  290. }
  291. static int omap_mbox_startup(struct omap_mbox *mbox)
  292. {
  293. int ret = 0;
  294. struct omap_mbox_queue *mq;
  295. mq = mbox_queue_alloc(mbox, mbox_rx_work);
  296. if (!mq)
  297. return -ENOMEM;
  298. mbox->rxq = mq;
  299. mq->mbox = mbox;
  300. ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
  301. mbox->name, mbox);
  302. if (unlikely(ret)) {
  303. pr_err("failed to register mailbox interrupt:%d\n", ret);
  304. goto fail_request_irq;
  305. }
  306. if (mbox->send_no_irq)
  307. mbox->chan->txdone_method = TXDONE_BY_ACK;
  308. _omap_mbox_enable_irq(mbox, IRQ_RX);
  309. return 0;
  310. fail_request_irq:
  311. mbox_queue_free(mbox->rxq);
  312. return ret;
  313. }
  314. static void omap_mbox_fini(struct omap_mbox *mbox)
  315. {
  316. _omap_mbox_disable_irq(mbox, IRQ_RX);
  317. free_irq(mbox->irq, mbox);
  318. flush_work(&mbox->rxq->work);
  319. mbox_queue_free(mbox->rxq);
  320. }
  321. static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
  322. const char *mbox_name)
  323. {
  324. struct omap_mbox *_mbox, *mbox = NULL;
  325. struct omap_mbox **mboxes = mdev->mboxes;
  326. int i;
  327. if (!mboxes)
  328. return NULL;
  329. for (i = 0; (_mbox = mboxes[i]); i++) {
  330. if (!strcmp(_mbox->name, mbox_name)) {
  331. mbox = _mbox;
  332. break;
  333. }
  334. }
  335. return mbox;
  336. }
  337. struct mbox_chan *omap_mbox_request_channel(struct mbox_client *cl,
  338. const char *chan_name)
  339. {
  340. struct device *dev = cl->dev;
  341. struct omap_mbox *mbox = NULL;
  342. struct omap_mbox_device *mdev;
  343. struct mbox_chan *chan;
  344. unsigned long flags;
  345. int ret;
  346. if (!dev)
  347. return ERR_PTR(-ENODEV);
  348. if (dev->of_node) {
  349. pr_err("%s: please use mbox_request_channel(), this API is supported only for OMAP non-DT usage\n",
  350. __func__);
  351. return ERR_PTR(-ENODEV);
  352. }
  353. mutex_lock(&omap_mbox_devices_lock);
  354. list_for_each_entry(mdev, &omap_mbox_devices, elem) {
  355. mbox = omap_mbox_device_find(mdev, chan_name);
  356. if (mbox)
  357. break;
  358. }
  359. mutex_unlock(&omap_mbox_devices_lock);
  360. if (!mbox || !mbox->chan)
  361. return ERR_PTR(-ENOENT);
  362. chan = mbox->chan;
  363. spin_lock_irqsave(&chan->lock, flags);
  364. chan->msg_free = 0;
  365. chan->msg_count = 0;
  366. chan->active_req = NULL;
  367. chan->cl = cl;
  368. init_completion(&chan->tx_complete);
  369. spin_unlock_irqrestore(&chan->lock, flags);
  370. ret = chan->mbox->ops->startup(chan);
  371. if (ret) {
  372. pr_err("Unable to startup the chan (%d)\n", ret);
  373. mbox_free_channel(chan);
  374. chan = ERR_PTR(ret);
  375. }
  376. return chan;
  377. }
  378. EXPORT_SYMBOL(omap_mbox_request_channel);
  379. static struct class omap_mbox_class = { .name = "mbox", };
  380. static int omap_mbox_register(struct omap_mbox_device *mdev)
  381. {
  382. int ret;
  383. int i;
  384. struct omap_mbox **mboxes;
  385. if (!mdev || !mdev->mboxes)
  386. return -EINVAL;
  387. mboxes = mdev->mboxes;
  388. for (i = 0; mboxes[i]; i++) {
  389. struct omap_mbox *mbox = mboxes[i];
  390. mbox->dev = device_create(&omap_mbox_class, mdev->dev,
  391. 0, mbox, "%s", mbox->name);
  392. if (IS_ERR(mbox->dev)) {
  393. ret = PTR_ERR(mbox->dev);
  394. goto err_out;
  395. }
  396. }
  397. mutex_lock(&omap_mbox_devices_lock);
  398. list_add(&mdev->elem, &omap_mbox_devices);
  399. mutex_unlock(&omap_mbox_devices_lock);
  400. ret = mbox_controller_register(&mdev->controller);
  401. err_out:
  402. if (ret) {
  403. while (i--)
  404. device_unregister(mboxes[i]->dev);
  405. }
  406. return ret;
  407. }
  408. static int omap_mbox_unregister(struct omap_mbox_device *mdev)
  409. {
  410. int i;
  411. struct omap_mbox **mboxes;
  412. if (!mdev || !mdev->mboxes)
  413. return -EINVAL;
  414. mutex_lock(&omap_mbox_devices_lock);
  415. list_del(&mdev->elem);
  416. mutex_unlock(&omap_mbox_devices_lock);
  417. mbox_controller_unregister(&mdev->controller);
  418. mboxes = mdev->mboxes;
  419. for (i = 0; mboxes[i]; i++)
  420. device_unregister(mboxes[i]->dev);
  421. return 0;
  422. }
  423. static int omap_mbox_chan_startup(struct mbox_chan *chan)
  424. {
  425. struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
  426. struct omap_mbox_device *mdev = mbox->parent;
  427. int ret = 0;
  428. mutex_lock(&mdev->cfg_lock);
  429. pm_runtime_get_sync(mdev->dev);
  430. ret = omap_mbox_startup(mbox);
  431. if (ret)
  432. pm_runtime_put_sync(mdev->dev);
  433. mutex_unlock(&mdev->cfg_lock);
  434. return ret;
  435. }
  436. static void omap_mbox_chan_shutdown(struct mbox_chan *chan)
  437. {
  438. struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
  439. struct omap_mbox_device *mdev = mbox->parent;
  440. mutex_lock(&mdev->cfg_lock);
  441. omap_mbox_fini(mbox);
  442. pm_runtime_put_sync(mdev->dev);
  443. mutex_unlock(&mdev->cfg_lock);
  444. }
  445. static int omap_mbox_chan_send_noirq(struct omap_mbox *mbox, void *data)
  446. {
  447. int ret = -EBUSY;
  448. if (!mbox_fifo_full(mbox)) {
  449. _omap_mbox_enable_irq(mbox, IRQ_RX);
  450. mbox_fifo_write(mbox, (mbox_msg_t)data);
  451. ret = 0;
  452. _omap_mbox_disable_irq(mbox, IRQ_RX);
  453. /* we must read and ack the interrupt directly from here */
  454. mbox_fifo_read(mbox);
  455. ack_mbox_irq(mbox, IRQ_RX);
  456. }
  457. return ret;
  458. }
  459. static int omap_mbox_chan_send(struct omap_mbox *mbox, void *data)
  460. {
  461. int ret = -EBUSY;
  462. if (!mbox_fifo_full(mbox)) {
  463. mbox_fifo_write(mbox, (mbox_msg_t)data);
  464. ret = 0;
  465. }
  466. /* always enable the interrupt */
  467. _omap_mbox_enable_irq(mbox, IRQ_TX);
  468. return ret;
  469. }
  470. static int omap_mbox_chan_send_data(struct mbox_chan *chan, void *data)
  471. {
  472. struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
  473. int ret;
  474. if (!mbox)
  475. return -EINVAL;
  476. if (mbox->send_no_irq)
  477. ret = omap_mbox_chan_send_noirq(mbox, data);
  478. else
  479. ret = omap_mbox_chan_send(mbox, data);
  480. return ret;
  481. }
  482. static const struct mbox_chan_ops omap_mbox_chan_ops = {
  483. .startup = omap_mbox_chan_startup,
  484. .send_data = omap_mbox_chan_send_data,
  485. .shutdown = omap_mbox_chan_shutdown,
  486. };
  487. #ifdef CONFIG_PM_SLEEP
  488. static int omap_mbox_suspend(struct device *dev)
  489. {
  490. struct omap_mbox_device *mdev = dev_get_drvdata(dev);
  491. u32 usr, fifo, reg;
  492. if (pm_runtime_status_suspended(dev))
  493. return 0;
  494. for (fifo = 0; fifo < mdev->num_fifos; fifo++) {
  495. if (mbox_read_reg(mdev, MAILBOX_MSGSTATUS(fifo))) {
  496. dev_err(mdev->dev, "fifo %d has unexpected unread messages\n",
  497. fifo);
  498. return -EBUSY;
  499. }
  500. }
  501. for (usr = 0; usr < mdev->num_users; usr++) {
  502. reg = MAILBOX_IRQENABLE(mdev->intr_type, usr);
  503. mdev->irq_ctx[usr] = mbox_read_reg(mdev, reg);
  504. }
  505. return 0;
  506. }
  507. static int omap_mbox_resume(struct device *dev)
  508. {
  509. struct omap_mbox_device *mdev = dev_get_drvdata(dev);
  510. u32 usr, reg;
  511. if (pm_runtime_status_suspended(dev))
  512. return 0;
  513. for (usr = 0; usr < mdev->num_users; usr++) {
  514. reg = MAILBOX_IRQENABLE(mdev->intr_type, usr);
  515. mbox_write_reg(mdev, mdev->irq_ctx[usr], reg);
  516. }
  517. return 0;
  518. }
  519. #endif
  520. static const struct dev_pm_ops omap_mbox_pm_ops = {
  521. SET_SYSTEM_SLEEP_PM_OPS(omap_mbox_suspend, omap_mbox_resume)
  522. };
  523. static const struct of_device_id omap_mailbox_of_match[] = {
  524. {
  525. .compatible = "ti,omap2-mailbox",
  526. .data = (void *)MBOX_INTR_CFG_TYPE1,
  527. },
  528. {
  529. .compatible = "ti,omap3-mailbox",
  530. .data = (void *)MBOX_INTR_CFG_TYPE1,
  531. },
  532. {
  533. .compatible = "ti,omap4-mailbox",
  534. .data = (void *)MBOX_INTR_CFG_TYPE2,
  535. },
  536. {
  537. /* end */
  538. },
  539. };
  540. MODULE_DEVICE_TABLE(of, omap_mailbox_of_match);
  541. static struct mbox_chan *omap_mbox_of_xlate(struct mbox_controller *controller,
  542. const struct of_phandle_args *sp)
  543. {
  544. phandle phandle = sp->args[0];
  545. struct device_node *node;
  546. struct omap_mbox_device *mdev;
  547. struct omap_mbox *mbox;
  548. mdev = container_of(controller, struct omap_mbox_device, controller);
  549. if (WARN_ON(!mdev))
  550. return ERR_PTR(-EINVAL);
  551. node = of_find_node_by_phandle(phandle);
  552. if (!node) {
  553. pr_err("%s: could not find node phandle 0x%x\n",
  554. __func__, phandle);
  555. return ERR_PTR(-ENODEV);
  556. }
  557. mbox = omap_mbox_device_find(mdev, node->name);
  558. of_node_put(node);
  559. return mbox ? mbox->chan : ERR_PTR(-ENOENT);
  560. }
  561. static int omap_mbox_probe(struct platform_device *pdev)
  562. {
  563. struct resource *mem;
  564. int ret;
  565. struct mbox_chan *chnls;
  566. struct omap_mbox **list, *mbox, *mboxblk;
  567. struct omap_mbox_fifo_info *finfo, *finfoblk;
  568. struct omap_mbox_device *mdev;
  569. struct omap_mbox_fifo *fifo;
  570. struct device_node *node = pdev->dev.of_node;
  571. struct device_node *child;
  572. const struct of_device_id *match;
  573. u32 intr_type, info_count;
  574. u32 num_users, num_fifos;
  575. u32 tmp[3];
  576. u32 l;
  577. int i;
  578. if (!node) {
  579. pr_err("%s: only DT-based devices are supported\n", __func__);
  580. return -ENODEV;
  581. }
  582. match = of_match_device(omap_mailbox_of_match, &pdev->dev);
  583. if (!match)
  584. return -ENODEV;
  585. intr_type = (u32)match->data;
  586. if (of_property_read_u32(node, "ti,mbox-num-users", &num_users))
  587. return -ENODEV;
  588. if (of_property_read_u32(node, "ti,mbox-num-fifos", &num_fifos))
  589. return -ENODEV;
  590. info_count = of_get_available_child_count(node);
  591. if (!info_count) {
  592. dev_err(&pdev->dev, "no available mbox devices found\n");
  593. return -ENODEV;
  594. }
  595. finfoblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*finfoblk),
  596. GFP_KERNEL);
  597. if (!finfoblk)
  598. return -ENOMEM;
  599. finfo = finfoblk;
  600. child = NULL;
  601. for (i = 0; i < info_count; i++, finfo++) {
  602. child = of_get_next_available_child(node, child);
  603. ret = of_property_read_u32_array(child, "ti,mbox-tx", tmp,
  604. ARRAY_SIZE(tmp));
  605. if (ret)
  606. return ret;
  607. finfo->tx_id = tmp[0];
  608. finfo->tx_irq = tmp[1];
  609. finfo->tx_usr = tmp[2];
  610. ret = of_property_read_u32_array(child, "ti,mbox-rx", tmp,
  611. ARRAY_SIZE(tmp));
  612. if (ret)
  613. return ret;
  614. finfo->rx_id = tmp[0];
  615. finfo->rx_irq = tmp[1];
  616. finfo->rx_usr = tmp[2];
  617. finfo->name = child->name;
  618. if (of_find_property(child, "ti,mbox-send-noirq", NULL))
  619. finfo->send_no_irq = true;
  620. if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos ||
  621. finfo->tx_usr >= num_users || finfo->rx_usr >= num_users)
  622. return -EINVAL;
  623. }
  624. mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
  625. if (!mdev)
  626. return -ENOMEM;
  627. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  628. mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
  629. if (IS_ERR(mdev->mbox_base))
  630. return PTR_ERR(mdev->mbox_base);
  631. mdev->irq_ctx = devm_kzalloc(&pdev->dev, num_users * sizeof(u32),
  632. GFP_KERNEL);
  633. if (!mdev->irq_ctx)
  634. return -ENOMEM;
  635. /* allocate one extra for marking end of list */
  636. list = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*list),
  637. GFP_KERNEL);
  638. if (!list)
  639. return -ENOMEM;
  640. chnls = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*chnls),
  641. GFP_KERNEL);
  642. if (!chnls)
  643. return -ENOMEM;
  644. mboxblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*mbox),
  645. GFP_KERNEL);
  646. if (!mboxblk)
  647. return -ENOMEM;
  648. mbox = mboxblk;
  649. finfo = finfoblk;
  650. for (i = 0; i < info_count; i++, finfo++) {
  651. fifo = &mbox->tx_fifo;
  652. fifo->msg = MAILBOX_MESSAGE(finfo->tx_id);
  653. fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id);
  654. fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id);
  655. fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr);
  656. fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr);
  657. fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr);
  658. fifo = &mbox->rx_fifo;
  659. fifo->msg = MAILBOX_MESSAGE(finfo->rx_id);
  660. fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id);
  661. fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id);
  662. fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr);
  663. fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr);
  664. fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr);
  665. mbox->send_no_irq = finfo->send_no_irq;
  666. mbox->intr_type = intr_type;
  667. mbox->parent = mdev;
  668. mbox->name = finfo->name;
  669. mbox->irq = platform_get_irq(pdev, finfo->tx_irq);
  670. if (mbox->irq < 0)
  671. return mbox->irq;
  672. mbox->chan = &chnls[i];
  673. chnls[i].con_priv = mbox;
  674. list[i] = mbox++;
  675. }
  676. mutex_init(&mdev->cfg_lock);
  677. mdev->dev = &pdev->dev;
  678. mdev->num_users = num_users;
  679. mdev->num_fifos = num_fifos;
  680. mdev->intr_type = intr_type;
  681. mdev->mboxes = list;
  682. /* OMAP does not have a Tx-Done IRQ, but rather a Tx-Ready IRQ */
  683. mdev->controller.txdone_irq = true;
  684. mdev->controller.dev = mdev->dev;
  685. mdev->controller.ops = &omap_mbox_chan_ops;
  686. mdev->controller.chans = chnls;
  687. mdev->controller.num_chans = info_count;
  688. mdev->controller.of_xlate = omap_mbox_of_xlate;
  689. ret = omap_mbox_register(mdev);
  690. if (ret)
  691. return ret;
  692. platform_set_drvdata(pdev, mdev);
  693. pm_runtime_enable(mdev->dev);
  694. ret = pm_runtime_get_sync(mdev->dev);
  695. if (ret < 0) {
  696. pm_runtime_put_noidle(mdev->dev);
  697. goto unregister;
  698. }
  699. /*
  700. * just print the raw revision register, the format is not
  701. * uniform across all SoCs
  702. */
  703. l = mbox_read_reg(mdev, MAILBOX_REVISION);
  704. dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
  705. ret = pm_runtime_put_sync(mdev->dev);
  706. if (ret < 0)
  707. goto unregister;
  708. devm_kfree(&pdev->dev, finfoblk);
  709. return 0;
  710. unregister:
  711. pm_runtime_disable(mdev->dev);
  712. omap_mbox_unregister(mdev);
  713. return ret;
  714. }
  715. static int omap_mbox_remove(struct platform_device *pdev)
  716. {
  717. struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
  718. pm_runtime_disable(mdev->dev);
  719. omap_mbox_unregister(mdev);
  720. return 0;
  721. }
  722. static struct platform_driver omap_mbox_driver = {
  723. .probe = omap_mbox_probe,
  724. .remove = omap_mbox_remove,
  725. .driver = {
  726. .name = "omap-mailbox",
  727. .pm = &omap_mbox_pm_ops,
  728. .of_match_table = of_match_ptr(omap_mailbox_of_match),
  729. },
  730. };
  731. static int __init omap_mbox_init(void)
  732. {
  733. int err;
  734. err = class_register(&omap_mbox_class);
  735. if (err)
  736. return err;
  737. /* kfifo size sanity check: alignment and minimal size */
  738. mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
  739. mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
  740. sizeof(mbox_msg_t));
  741. return platform_driver_register(&omap_mbox_driver);
  742. }
  743. subsys_initcall(omap_mbox_init);
  744. static void __exit omap_mbox_exit(void)
  745. {
  746. platform_driver_unregister(&omap_mbox_driver);
  747. class_unregister(&omap_mbox_class);
  748. }
  749. module_exit(omap_mbox_exit);
  750. MODULE_LICENSE("GPL v2");
  751. MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
  752. MODULE_AUTHOR("Toshihiro Kobayashi");
  753. MODULE_AUTHOR("Hiroshi DOYU");