blob: d9a503974d5272d451deeaa304c00b9fe6719eb7 [file] [log] [blame]
Hiroshi DOYU340a6142006-12-07 15:43:59 -08001/*
2 * OMAP mailbox driver
3 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07004 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
Suman Anna5040f532014-06-24 19:43:41 -05005 * Copyright (C) 2013-2014 Texas Instruments Inc.
Hiroshi DOYU340a6142006-12-07 15:43:59 -08006 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07007 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Suman Anna5040f532014-06-24 19:43:41 -05008 * Suman Anna <s-anna@ti.com>
Hiroshi DOYU340a6142006-12-07 15:43:59 -08009 *
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
Hiroshi DOYU340a6142006-12-07 15:43:59 -080026#include <linux/interrupt.h>
Felipe Contrerasb3e69142010-06-11 15:51:49 +000027#include <linux/spinlock.h>
28#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000030#include <linux/kfifo.h>
31#include <linux/err.h>
Kanigeri, Hari58256302010-11-29 20:24:14 +000032#include <linux/notifier.h>
Paul Gortmaker73017a52011-07-31 16:14:14 -040033#include <linux/module.h>
Suman Anna5040f532014-06-24 19:43:41 -050034#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>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070038
Suman Anna5040f532014-06-24 19:43:41 -050039#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))
Hiroshi DOYU340a6142006-12-07 15:43:59 -080043
Suman Anna5040f532014-06-24 19:43:41 -050044#define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
45#define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
46
47#define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
48#define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
49#define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
50
51#define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \
52 OMAP2_MAILBOX_IRQSTATUS(u))
53#define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \
54 OMAP2_MAILBOX_IRQENABLE(u))
55#define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \
56 : OMAP2_MAILBOX_IRQENABLE(u))
57
58#define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
59#define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
60
61#define MBOX_REG_SIZE 0x120
62
63#define OMAP4_MBOX_REG_SIZE 0x130
64
65#define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
66#define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
67
68struct omap_mbox_fifo {
69 unsigned long msg;
70 unsigned long fifo_stat;
71 unsigned long msg_stat;
Suman Anna5040f532014-06-24 19:43:41 -050072 unsigned long irqenable;
73 unsigned long irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -050074 unsigned long irqdisable;
Suman Annabe3322e2014-06-24 19:43:42 -050075 u32 intr_bit;
Suman Anna5040f532014-06-24 19:43:41 -050076};
77
78struct omap_mbox_queue {
79 spinlock_t lock;
80 struct kfifo fifo;
81 struct work_struct work;
82 struct tasklet_struct tasklet;
83 struct omap_mbox *mbox;
84 bool full;
85};
86
87struct omap_mbox {
88 const char *name;
89 int irq;
90 struct omap_mbox_queue *txq, *rxq;
91 struct device *dev;
Suman Annabe3322e2014-06-24 19:43:42 -050092 struct omap_mbox_fifo tx_fifo;
93 struct omap_mbox_fifo rx_fifo;
94 u32 ctx[OMAP4_MBOX_NR_REGS];
95 u32 intr_type;
Suman Anna5040f532014-06-24 19:43:41 -050096 int use_count;
97 struct blocking_notifier_head notifier;
98};
99
100static void __iomem *mbox_base;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000101static struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800102
Hiroshi DOYU72b917e2010-02-18 00:48:55 -0600103static DEFINE_MUTEX(mbox_configured_lock);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800104
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000105static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
106module_param(mbox_kfifo_size, uint, S_IRUGO);
107MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
108
Suman Anna5040f532014-06-24 19:43:41 -0500109static inline unsigned int mbox_read_reg(size_t ofs)
110{
111 return __raw_readl(mbox_base + ofs);
112}
113
114static inline void mbox_write_reg(u32 val, size_t ofs)
115{
116 __raw_writel(val, mbox_base + ofs);
117}
118
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700119/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500120static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700121{
Suman Annabe3322e2014-06-24 19:43:42 -0500122 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna5040f532014-06-24 19:43:41 -0500123 return (mbox_msg_t) mbox_read_reg(fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700124}
Suman Anna5040f532014-06-24 19:43:41 -0500125
126static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700127{
Suman Annabe3322e2014-06-24 19:43:42 -0500128 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna5040f532014-06-24 19:43:41 -0500129 mbox_write_reg(msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700130}
Suman Anna5040f532014-06-24 19:43:41 -0500131
132static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700133{
Suman Annabe3322e2014-06-24 19:43:42 -0500134 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna5040f532014-06-24 19:43:41 -0500135 return (mbox_read_reg(fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700136}
Suman Anna5040f532014-06-24 19:43:41 -0500137
138static int mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700139{
Suman Annabe3322e2014-06-24 19:43:42 -0500140 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna5040f532014-06-24 19:43:41 -0500141 return mbox_read_reg(fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700142}
143
144/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500145static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700146{
Suman Annabe3322e2014-06-24 19:43:42 -0500147 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;
Suman Anna5040f532014-06-24 19:43:41 -0500151
Suman Annabe3322e2014-06-24 19:43:42 -0500152 mbox_write_reg(bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500153
154 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Annabe3322e2014-06-24 19:43:42 -0500155 mbox_read_reg(irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700156}
Suman Anna5040f532014-06-24 19:43:41 -0500157
158static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700159{
Suman Annabe3322e2014-06-24 19:43:42 -0500160 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
161 &mbox->tx_fifo : &mbox->rx_fifo;
162 u32 bit = fifo->intr_bit;
163 u32 irqenable = fifo->irqenable;
164 u32 irqstatus = fifo->irqstatus;
165
166 u32 enable = mbox_read_reg(irqenable);
167 u32 status = mbox_read_reg(irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500168
169 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700170}
171
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800172/*
173 * message sender
174 */
C A Subramaniamb2b63622009-11-22 10:11:20 -0800175int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800176{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000177 struct omap_mbox_queue *mq = mbox->txq;
178 int ret = 0, len;
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800179
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000180 spin_lock_bh(&mq->lock);
Tejun Heoec247512009-04-23 11:05:20 +0900181
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000182 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
183 ret = -ENOMEM;
184 goto out;
185 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800186
Suman Annafe714a42014-06-24 19:43:39 -0500187 if (kfifo_is_empty(&mq->fifo) && !mbox_fifo_full(mbox)) {
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000188 mbox_fifo_write(mbox, msg);
189 goto out;
190 }
191
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000192 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
193 WARN_ON(len != sizeof(msg));
194
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800195 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800196
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000197out:
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000198 spin_unlock_bh(&mq->lock);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000199 return ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800200}
201EXPORT_SYMBOL(omap_mbox_msg_send);
202
Suman Annac869c752013-03-12 17:55:29 -0500203void omap_mbox_save_ctx(struct omap_mbox *mbox)
204{
Suman Anna5040f532014-06-24 19:43:41 -0500205 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500206 int nr_regs;
Suman Annac869c752013-03-12 17:55:29 -0500207
Suman Annabe3322e2014-06-24 19:43:42 -0500208 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500209 nr_regs = OMAP4_MBOX_NR_REGS;
210 else
211 nr_regs = MBOX_NR_REGS;
212 for (i = 0; i < nr_regs; i++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500213 mbox->ctx[i] = mbox_read_reg(i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500214
215 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500216 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500217 }
Suman Annac869c752013-03-12 17:55:29 -0500218}
219EXPORT_SYMBOL(omap_mbox_save_ctx);
220
221void omap_mbox_restore_ctx(struct omap_mbox *mbox)
222{
Suman Anna5040f532014-06-24 19:43:41 -0500223 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500224 int nr_regs;
Suman Annac869c752013-03-12 17:55:29 -0500225
Suman Annabe3322e2014-06-24 19:43:42 -0500226 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500227 nr_regs = OMAP4_MBOX_NR_REGS;
228 else
229 nr_regs = MBOX_NR_REGS;
230 for (i = 0; i < nr_regs; i++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500231 mbox_write_reg(mbox->ctx[i], i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500232
233 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500234 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500235 }
Suman Annac869c752013-03-12 17:55:29 -0500236}
237EXPORT_SYMBOL(omap_mbox_restore_ctx);
238
239void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
240{
Suman Annabe3322e2014-06-24 19:43:42 -0500241 u32 l;
242 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
243 &mbox->tx_fifo : &mbox->rx_fifo;
244 u32 bit = fifo->intr_bit;
245 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500246
Suman Annabe3322e2014-06-24 19:43:42 -0500247 l = mbox_read_reg(irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500248 l |= bit;
Suman Annabe3322e2014-06-24 19:43:42 -0500249 mbox_write_reg(l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500250}
251EXPORT_SYMBOL(omap_mbox_enable_irq);
252
253void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
254{
Suman Annabe3322e2014-06-24 19:43:42 -0500255 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
256 &mbox->tx_fifo : &mbox->rx_fifo;
257 u32 bit = fifo->intr_bit;
258 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500259
260 /*
261 * Read and update the interrupt configuration register for pre-OMAP4.
262 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
263 */
Suman Annabe3322e2014-06-24 19:43:42 -0500264 if (!mbox->intr_type)
265 bit = mbox_read_reg(irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500266
Suman Annabe3322e2014-06-24 19:43:42 -0500267 mbox_write_reg(bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500268}
269EXPORT_SYMBOL(omap_mbox_disable_irq);
270
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800271static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800272{
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800273 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000274 struct omap_mbox_queue *mq = mbox->txq;
275 mbox_msg_t msg;
276 int ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800277
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000278 while (kfifo_len(&mq->fifo)) {
Suman Annafe714a42014-06-24 19:43:39 -0500279 if (mbox_fifo_full(mbox)) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800280 omap_mbox_enable_irq(mbox, IRQ_TX);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000281 break;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800282 }
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000283
284 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
285 sizeof(msg));
286 WARN_ON(ret != sizeof(msg));
287
288 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800289 }
290}
291
292/*
293 * Message receiver(workqueue)
294 */
295static void mbox_rx_work(struct work_struct *work)
296{
297 struct omap_mbox_queue *mq =
298 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800299 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000300 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800301
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000302 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
303 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
304 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800305
Kanigeri, Hari58256302010-11-29 20:24:14 +0000306 blocking_notifier_call_chain(&mq->mbox->notifier, len,
307 (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000308 spin_lock_irq(&mq->lock);
309 if (mq->full) {
310 mq->full = false;
311 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
312 }
313 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800314 }
315}
316
317/*
318 * Mailbox interrupt handler
319 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800320static void __mbox_tx_interrupt(struct omap_mbox *mbox)
321{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800322 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800323 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800324 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800325}
326
327static void __mbox_rx_interrupt(struct omap_mbox *mbox)
328{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000329 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800330 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000331 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800332
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800333 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000334 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600335 omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000336 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800337 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600338 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800339
340 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800341
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000342 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
343 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800344 }
345
346 /* no more messages in the fifo. clear IRQ source. */
347 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700348nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100349 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800350}
351
352static irqreturn_t mbox_interrupt(int irq, void *p)
353{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400354 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800355
356 if (is_mbox_irq(mbox, IRQ_TX))
357 __mbox_tx_interrupt(mbox);
358
359 if (is_mbox_irq(mbox, IRQ_RX))
360 __mbox_rx_interrupt(mbox);
361
362 return IRQ_HANDLED;
363}
364
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800365static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800366 void (*work) (struct work_struct *),
367 void (*tasklet)(unsigned long))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800368{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800369 struct omap_mbox_queue *mq;
370
371 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
372 if (!mq)
373 return NULL;
374
375 spin_lock_init(&mq->lock);
376
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000377 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800378 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800379
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800380 if (work)
381 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800382
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800383 if (tasklet)
384 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800385 return mq;
386error:
387 kfree(mq);
388 return NULL;
389}
390
391static void mbox_queue_free(struct omap_mbox_queue *q)
392{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000393 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800394 kfree(q);
395}
396
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800397static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800398{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800399 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800400 struct omap_mbox_queue *mq;
401
Kanigeri, Hari58256302010-11-29 20:24:14 +0000402 mutex_lock(&mbox_configured_lock);
Suman Anna5040f532014-06-24 19:43:41 -0500403 ret = pm_runtime_get_sync(mbox->dev->parent);
404 if (unlikely(ret < 0))
405 goto fail_startup;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800406
Kanigeri, Hari58256302010-11-29 20:24:14 +0000407 if (!mbox->use_count++) {
Kanigeri, Hari58256302010-11-29 20:24:14 +0000408 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
409 if (!mq) {
410 ret = -ENOMEM;
411 goto fail_alloc_txq;
412 }
413 mbox->txq = mq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800414
Kanigeri, Hari58256302010-11-29 20:24:14 +0000415 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
416 if (!mq) {
417 ret = -ENOMEM;
418 goto fail_alloc_rxq;
419 }
420 mbox->rxq = mq;
421 mq->mbox = mbox;
Suman Annaecf305c2013-02-01 20:37:06 -0600422 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
423 mbox->name, mbox);
424 if (unlikely(ret)) {
425 pr_err("failed to register mailbox interrupt:%d\n",
426 ret);
427 goto fail_request_irq;
428 }
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300429
430 omap_mbox_enable_irq(mbox, IRQ_RX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800431 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000432 mutex_unlock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800433 return 0;
434
Suman Annaecf305c2013-02-01 20:37:06 -0600435fail_request_irq:
436 mbox_queue_free(mbox->rxq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000437fail_alloc_rxq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800438 mbox_queue_free(mbox->txq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000439fail_alloc_txq:
Suman Anna5040f532014-06-24 19:43:41 -0500440 pm_runtime_put_sync(mbox->dev->parent);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000441 mbox->use_count--;
442fail_startup:
Kanigeri, Hari58256302010-11-29 20:24:14 +0000443 mutex_unlock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800444 return ret;
445}
446
447static void omap_mbox_fini(struct omap_mbox *mbox)
448{
Kanigeri, Hari58256302010-11-29 20:24:14 +0000449 mutex_lock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800450
Kanigeri, Hari58256302010-11-29 20:24:14 +0000451 if (!--mbox->use_count) {
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300452 omap_mbox_disable_irq(mbox, IRQ_RX);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000453 free_irq(mbox->irq, mbox);
454 tasklet_kill(&mbox->txq->tasklet);
Tejun Heo43829732012-08-20 14:51:24 -0700455 flush_work(&mbox->rxq->work);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000456 mbox_queue_free(mbox->txq);
457 mbox_queue_free(mbox->rxq);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800458 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000459
Suman Anna5040f532014-06-24 19:43:41 -0500460 pm_runtime_put_sync(mbox->dev->parent);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000461
462 mutex_unlock(&mbox_configured_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800463}
464
Kanigeri, Hari58256302010-11-29 20:24:14 +0000465struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800466{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000467 struct omap_mbox *_mbox, *mbox = NULL;
468 int i, ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800469
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000470 if (!mboxes)
471 return ERR_PTR(-EINVAL);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800472
Kevin Hilmanc0377322011-02-11 19:56:43 +0000473 for (i = 0; (_mbox = mboxes[i]); i++) {
474 if (!strcmp(_mbox->name, name)) {
475 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000476 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000477 }
478 }
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000479
480 if (!mbox)
481 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800482
Kanigeri, Hari58256302010-11-29 20:24:14 +0000483 if (nb)
484 blocking_notifier_chain_register(&mbox->notifier, nb);
485
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300486 ret = omap_mbox_startup(mbox);
487 if (ret) {
488 blocking_notifier_chain_unregister(&mbox->notifier, nb);
489 return ERR_PTR(-ENODEV);
490 }
491
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800492 return mbox;
493}
494EXPORT_SYMBOL(omap_mbox_get);
495
Kanigeri, Hari58256302010-11-29 20:24:14 +0000496void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800497{
Kanigeri, Hari58256302010-11-29 20:24:14 +0000498 blocking_notifier_chain_unregister(&mbox->notifier, nb);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800499 omap_mbox_fini(mbox);
500}
501EXPORT_SYMBOL(omap_mbox_put);
502
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300503static struct class omap_mbox_class = { .name = "mbox", };
504
Suman Anna5040f532014-06-24 19:43:41 -0500505static int omap_mbox_register(struct device *parent, struct omap_mbox **list)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800506{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000507 int ret;
508 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800509
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000510 mboxes = list;
511 if (!mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800512 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800513
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000514 for (i = 0; mboxes[i]; i++) {
515 struct omap_mbox *mbox = mboxes[i];
516 mbox->dev = device_create(&omap_mbox_class,
517 parent, 0, mbox, "%s", mbox->name);
518 if (IS_ERR(mbox->dev)) {
519 ret = PTR_ERR(mbox->dev);
520 goto err_out;
521 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000522
523 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700524 }
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700525 return 0;
526
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000527err_out:
528 while (i--)
529 device_unregister(mboxes[i]->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800530 return ret;
531}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800532
Suman Anna5040f532014-06-24 19:43:41 -0500533static int omap_mbox_unregister(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800534{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000535 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800536
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000537 if (!mboxes)
538 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800539
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000540 for (i = 0; mboxes[i]; i++)
541 device_unregister(mboxes[i]->dev);
542 mboxes = NULL;
543 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800544}
Suman Anna5040f532014-06-24 19:43:41 -0500545
546static int omap_mbox_probe(struct platform_device *pdev)
547{
548 struct resource *mem;
549 int ret;
550 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna5040f532014-06-24 19:43:41 -0500551 struct omap_mbox_pdata *pdata = pdev->dev.platform_data;
552 struct omap_mbox_dev_info *info;
Suman Annabe3322e2014-06-24 19:43:42 -0500553 struct omap_mbox_fifo *fifo;
Suman Anna5040f532014-06-24 19:43:41 -0500554 u32 intr_type;
555 u32 l;
556 int i;
557
558 if (!pdata || !pdata->info_cnt || !pdata->info) {
559 pr_err("%s: platform not supported\n", __func__);
560 return -ENODEV;
561 }
562
563 /* allocate one extra for marking end of list */
564 list = devm_kzalloc(&pdev->dev, (pdata->info_cnt + 1) * sizeof(*list),
565 GFP_KERNEL);
566 if (!list)
567 return -ENOMEM;
568
569 mboxblk = devm_kzalloc(&pdev->dev, pdata->info_cnt * sizeof(*mbox),
570 GFP_KERNEL);
571 if (!mboxblk)
572 return -ENOMEM;
573
Suman Anna5040f532014-06-24 19:43:41 -0500574 info = pdata->info;
575 intr_type = pdata->intr_type;
576 mbox = mboxblk;
Suman Annabe3322e2014-06-24 19:43:42 -0500577 for (i = 0; i < pdata->info_cnt; i++, info++) {
578 fifo = &mbox->tx_fifo;
579 fifo->msg = MAILBOX_MESSAGE(info->tx_id);
580 fifo->fifo_stat = MAILBOX_FIFOSTATUS(info->tx_id);
581 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(info->tx_id);
582 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, info->usr_id);
583 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, info->usr_id);
584 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, info->usr_id);
Suman Anna5040f532014-06-24 19:43:41 -0500585
Suman Annabe3322e2014-06-24 19:43:42 -0500586 fifo = &mbox->rx_fifo;
587 fifo->msg = MAILBOX_MESSAGE(info->rx_id);
588 fifo->msg_stat = MAILBOX_MSGSTATUS(info->rx_id);
589 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(info->rx_id);
590 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, info->usr_id);
591 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, info->usr_id);
592 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, info->usr_id);
593
594 mbox->intr_type = intr_type;
595
Suman Anna5040f532014-06-24 19:43:41 -0500596 mbox->name = info->name;
597 mbox->irq = platform_get_irq(pdev, info->irq_id);
598 if (mbox->irq < 0)
599 return mbox->irq;
600 list[i] = mbox++;
601 }
602
603 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
604 mbox_base = devm_ioremap_resource(&pdev->dev, mem);
605 if (IS_ERR(mbox_base))
606 return PTR_ERR(mbox_base);
607
608 ret = omap_mbox_register(&pdev->dev, list);
609 if (ret)
610 return ret;
611
612 platform_set_drvdata(pdev, list);
613 pm_runtime_enable(&pdev->dev);
614
615 ret = pm_runtime_get_sync(&pdev->dev);
616 if (ret < 0) {
617 pm_runtime_put_noidle(&pdev->dev);
618 goto unregister;
619 }
620
621 /*
622 * just print the raw revision register, the format is not
623 * uniform across all SoCs
624 */
625 l = mbox_read_reg(MAILBOX_REVISION);
626 dev_info(&pdev->dev, "omap mailbox rev 0x%x\n", l);
627
628 ret = pm_runtime_put_sync(&pdev->dev);
629 if (ret < 0)
630 goto unregister;
631
632 return 0;
633
634unregister:
635 pm_runtime_disable(&pdev->dev);
636 omap_mbox_unregister();
637 return ret;
638}
639
640static int omap_mbox_remove(struct platform_device *pdev)
641{
642 pm_runtime_disable(&pdev->dev);
643 omap_mbox_unregister();
644
645 return 0;
646}
647
648static struct platform_driver omap_mbox_driver = {
649 .probe = omap_mbox_probe,
650 .remove = omap_mbox_remove,
651 .driver = {
652 .name = "omap-mailbox",
653 .owner = THIS_MODULE,
654 },
655};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800656
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800657static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800658{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300659 int err;
660
661 err = class_register(&omap_mbox_class);
662 if (err)
663 return err;
664
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000665 /* kfifo size sanity check: alignment and minimal size */
666 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000667 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
668 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000669
Suman Anna5040f532014-06-24 19:43:41 -0500670 return platform_driver_register(&omap_mbox_driver);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800671}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300672subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800673
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800674static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800675{
Suman Anna5040f532014-06-24 19:43:41 -0500676 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300677 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800678}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800679module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800680
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700681MODULE_LICENSE("GPL v2");
682MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000683MODULE_AUTHOR("Toshihiro Kobayashi");
684MODULE_AUTHOR("Hiroshi DOYU");