omap-mailbox.c 22 KB

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