omap-mailbox.c 20 KB

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