blob: a27e00e63a8a8ef66aa6c4f380b51b1dbd376e9a [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
Suman Anna72c1c812014-06-24 19:43:43 -050087struct omap_mbox_device {
88 struct device *dev;
89 struct mutex cfg_lock;
90 void __iomem *mbox_base;
91 u32 num_users;
92 u32 num_fifos;
93 struct omap_mbox **mboxes;
94 struct list_head elem;
95};
96
Suman Anna5040f532014-06-24 19:43:41 -050097struct omap_mbox {
98 const char *name;
99 int irq;
100 struct omap_mbox_queue *txq, *rxq;
101 struct device *dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500102 struct omap_mbox_device *parent;
Suman Annabe3322e2014-06-24 19:43:42 -0500103 struct omap_mbox_fifo tx_fifo;
104 struct omap_mbox_fifo rx_fifo;
105 u32 ctx[OMAP4_MBOX_NR_REGS];
106 u32 intr_type;
Suman Anna5040f532014-06-24 19:43:41 -0500107 int use_count;
108 struct blocking_notifier_head notifier;
109};
110
Suman Anna72c1c812014-06-24 19:43:43 -0500111/* global variables for the mailbox devices */
112static DEFINE_MUTEX(omap_mbox_devices_lock);
113static LIST_HEAD(omap_mbox_devices);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800114
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000115static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
116module_param(mbox_kfifo_size, uint, S_IRUGO);
117MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
118
Suman Anna72c1c812014-06-24 19:43:43 -0500119static inline
120unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500121{
Suman Anna72c1c812014-06-24 19:43:43 -0500122 return __raw_readl(mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500123}
124
Suman Anna72c1c812014-06-24 19:43:43 -0500125static inline
126void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500127{
Suman Anna72c1c812014-06-24 19:43:43 -0500128 __raw_writel(val, mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500129}
130
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700131/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500132static mbox_msg_t mbox_fifo_read(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 Anna72c1c812014-06-24 19:43:43 -0500135 return (mbox_msg_t) mbox_read_reg(mbox->parent, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700136}
Suman Anna5040f532014-06-24 19:43:41 -0500137
138static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
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 Anna72c1c812014-06-24 19:43:43 -0500141 mbox_write_reg(mbox->parent, msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700142}
Suman Anna5040f532014-06-24 19:43:41 -0500143
144static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700145{
Suman Annabe3322e2014-06-24 19:43:42 -0500146 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500147 return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700148}
Suman Anna5040f532014-06-24 19:43:41 -0500149
150static int mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700151{
Suman Annabe3322e2014-06-24 19:43:42 -0500152 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500153 return mbox_read_reg(mbox->parent, fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700154}
155
156/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500157static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700158{
Suman Annabe3322e2014-06-24 19:43:42 -0500159 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
160 &mbox->tx_fifo : &mbox->rx_fifo;
161 u32 bit = fifo->intr_bit;
162 u32 irqstatus = fifo->irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -0500163
Suman Anna72c1c812014-06-24 19:43:43 -0500164 mbox_write_reg(mbox->parent, bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500165
166 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Anna72c1c812014-06-24 19:43:43 -0500167 mbox_read_reg(mbox->parent, irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700168}
Suman Anna5040f532014-06-24 19:43:41 -0500169
170static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700171{
Suman Annabe3322e2014-06-24 19:43:42 -0500172 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
173 &mbox->tx_fifo : &mbox->rx_fifo;
174 u32 bit = fifo->intr_bit;
175 u32 irqenable = fifo->irqenable;
176 u32 irqstatus = fifo->irqstatus;
177
Suman Anna72c1c812014-06-24 19:43:43 -0500178 u32 enable = mbox_read_reg(mbox->parent, irqenable);
179 u32 status = mbox_read_reg(mbox->parent, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500180
181 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700182}
183
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800184/*
185 * message sender
186 */
C A Subramaniamb2b63622009-11-22 10:11:20 -0800187int omap_mbox_msg_send(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800188{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000189 struct omap_mbox_queue *mq = mbox->txq;
190 int ret = 0, len;
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800191
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000192 spin_lock_bh(&mq->lock);
Tejun Heoec247512009-04-23 11:05:20 +0900193
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000194 if (kfifo_avail(&mq->fifo) < sizeof(msg)) {
195 ret = -ENOMEM;
196 goto out;
197 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800198
Suman Annafe714a42014-06-24 19:43:39 -0500199 if (kfifo_is_empty(&mq->fifo) && !mbox_fifo_full(mbox)) {
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000200 mbox_fifo_write(mbox, msg);
201 goto out;
202 }
203
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000204 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
205 WARN_ON(len != sizeof(msg));
206
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800207 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800208
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000209out:
Kanigeri, Haria42743c2010-11-29 20:24:13 +0000210 spin_unlock_bh(&mq->lock);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000211 return ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800212}
213EXPORT_SYMBOL(omap_mbox_msg_send);
214
Suman Annac869c752013-03-12 17:55:29 -0500215void omap_mbox_save_ctx(struct omap_mbox *mbox)
216{
Suman Anna5040f532014-06-24 19:43:41 -0500217 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500218 int nr_regs;
Suman Annac869c752013-03-12 17:55:29 -0500219
Suman Annabe3322e2014-06-24 19:43:42 -0500220 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500221 nr_regs = OMAP4_MBOX_NR_REGS;
222 else
223 nr_regs = MBOX_NR_REGS;
224 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500225 mbox->ctx[i] = mbox_read_reg(mbox->parent, i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500226
227 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500228 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500229 }
Suman Annac869c752013-03-12 17:55:29 -0500230}
231EXPORT_SYMBOL(omap_mbox_save_ctx);
232
233void omap_mbox_restore_ctx(struct omap_mbox *mbox)
234{
Suman Anna5040f532014-06-24 19:43:41 -0500235 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500236 int nr_regs;
Suman Annac869c752013-03-12 17:55:29 -0500237
Suman Annabe3322e2014-06-24 19:43:42 -0500238 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500239 nr_regs = OMAP4_MBOX_NR_REGS;
240 else
241 nr_regs = MBOX_NR_REGS;
242 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500243 mbox_write_reg(mbox->parent, mbox->ctx[i], i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500244
245 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500246 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500247 }
Suman Annac869c752013-03-12 17:55:29 -0500248}
249EXPORT_SYMBOL(omap_mbox_restore_ctx);
250
251void omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
252{
Suman Annabe3322e2014-06-24 19:43:42 -0500253 u32 l;
254 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
255 &mbox->tx_fifo : &mbox->rx_fifo;
256 u32 bit = fifo->intr_bit;
257 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500258
Suman Anna72c1c812014-06-24 19:43:43 -0500259 l = mbox_read_reg(mbox->parent, irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500260 l |= bit;
Suman Anna72c1c812014-06-24 19:43:43 -0500261 mbox_write_reg(mbox->parent, l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500262}
263EXPORT_SYMBOL(omap_mbox_enable_irq);
264
265void omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
266{
Suman Annabe3322e2014-06-24 19:43:42 -0500267 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
268 &mbox->tx_fifo : &mbox->rx_fifo;
269 u32 bit = fifo->intr_bit;
270 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500271
272 /*
273 * Read and update the interrupt configuration register for pre-OMAP4.
274 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
275 */
Suman Annabe3322e2014-06-24 19:43:42 -0500276 if (!mbox->intr_type)
Suman Anna72c1c812014-06-24 19:43:43 -0500277 bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500278
Suman Anna72c1c812014-06-24 19:43:43 -0500279 mbox_write_reg(mbox->parent, bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500280}
281EXPORT_SYMBOL(omap_mbox_disable_irq);
282
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800283static void mbox_tx_tasklet(unsigned long tx_data)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800284{
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800285 struct omap_mbox *mbox = (struct omap_mbox *)tx_data;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000286 struct omap_mbox_queue *mq = mbox->txq;
287 mbox_msg_t msg;
288 int ret;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800289
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000290 while (kfifo_len(&mq->fifo)) {
Suman Annafe714a42014-06-24 19:43:39 -0500291 if (mbox_fifo_full(mbox)) {
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800292 omap_mbox_enable_irq(mbox, IRQ_TX);
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000293 break;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800294 }
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000295
296 ret = kfifo_out(&mq->fifo, (unsigned char *)&msg,
297 sizeof(msg));
298 WARN_ON(ret != sizeof(msg));
299
300 mbox_fifo_write(mbox, msg);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800301 }
302}
303
304/*
305 * Message receiver(workqueue)
306 */
307static void mbox_rx_work(struct work_struct *work)
308{
309 struct omap_mbox_queue *mq =
310 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800311 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000312 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800313
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000314 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
315 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
316 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800317
Kanigeri, Hari58256302010-11-29 20:24:14 +0000318 blocking_notifier_call_chain(&mq->mbox->notifier, len,
319 (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000320 spin_lock_irq(&mq->lock);
321 if (mq->full) {
322 mq->full = false;
323 omap_mbox_enable_irq(mq->mbox, IRQ_RX);
324 }
325 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800326 }
327}
328
329/*
330 * Mailbox interrupt handler
331 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800332static void __mbox_tx_interrupt(struct omap_mbox *mbox)
333{
Hiroshi DOYUeb188582009-11-22 10:11:22 -0800334 omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800335 ack_mbox_irq(mbox, IRQ_TX);
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800336 tasklet_schedule(&mbox->txq->tasklet);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800337}
338
339static void __mbox_rx_interrupt(struct omap_mbox *mbox)
340{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000341 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800342 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000343 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800344
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800345 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000346 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600347 omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000348 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800349 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600350 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800351
352 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800353
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000354 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
355 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800356 }
357
358 /* no more messages in the fifo. clear IRQ source. */
359 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700360nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100361 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800362}
363
364static irqreturn_t mbox_interrupt(int irq, void *p)
365{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400366 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800367
368 if (is_mbox_irq(mbox, IRQ_TX))
369 __mbox_tx_interrupt(mbox);
370
371 if (is_mbox_irq(mbox, IRQ_RX))
372 __mbox_rx_interrupt(mbox);
373
374 return IRQ_HANDLED;
375}
376
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800377static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800378 void (*work) (struct work_struct *),
379 void (*tasklet)(unsigned long))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800380{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800381 struct omap_mbox_queue *mq;
382
383 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
384 if (!mq)
385 return NULL;
386
387 spin_lock_init(&mq->lock);
388
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000389 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800390 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800391
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800392 if (work)
393 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800394
C A Subramaniam5ed8d322009-11-22 10:11:24 -0800395 if (tasklet)
396 tasklet_init(&mq->tasklet, tasklet, (unsigned long)mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800397 return mq;
398error:
399 kfree(mq);
400 return NULL;
401}
402
403static void mbox_queue_free(struct omap_mbox_queue *q)
404{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000405 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800406 kfree(q);
407}
408
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800409static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800410{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800411 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800412 struct omap_mbox_queue *mq;
Suman Anna72c1c812014-06-24 19:43:43 -0500413 struct omap_mbox_device *mdev = mbox->parent;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800414
Suman Anna72c1c812014-06-24 19:43:43 -0500415 mutex_lock(&mdev->cfg_lock);
416 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500417 if (unlikely(ret < 0))
418 goto fail_startup;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800419
Kanigeri, Hari58256302010-11-29 20:24:14 +0000420 if (!mbox->use_count++) {
Kanigeri, Hari58256302010-11-29 20:24:14 +0000421 mq = mbox_queue_alloc(mbox, NULL, mbox_tx_tasklet);
422 if (!mq) {
423 ret = -ENOMEM;
424 goto fail_alloc_txq;
425 }
426 mbox->txq = mq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800427
Kanigeri, Hari58256302010-11-29 20:24:14 +0000428 mq = mbox_queue_alloc(mbox, mbox_rx_work, NULL);
429 if (!mq) {
430 ret = -ENOMEM;
431 goto fail_alloc_rxq;
432 }
433 mbox->rxq = mq;
434 mq->mbox = mbox;
Suman Annaecf305c2013-02-01 20:37:06 -0600435 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
436 mbox->name, mbox);
437 if (unlikely(ret)) {
438 pr_err("failed to register mailbox interrupt:%d\n",
439 ret);
440 goto fail_request_irq;
441 }
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300442
443 omap_mbox_enable_irq(mbox, IRQ_RX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800444 }
Suman Anna72c1c812014-06-24 19:43:43 -0500445 mutex_unlock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800446 return 0;
447
Suman Annaecf305c2013-02-01 20:37:06 -0600448fail_request_irq:
449 mbox_queue_free(mbox->rxq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000450fail_alloc_rxq:
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800451 mbox_queue_free(mbox->txq);
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000452fail_alloc_txq:
Suman Anna72c1c812014-06-24 19:43:43 -0500453 pm_runtime_put_sync(mdev->dev);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000454 mbox->use_count--;
455fail_startup:
Suman Anna72c1c812014-06-24 19:43:43 -0500456 mutex_unlock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800457 return ret;
458}
459
460static void omap_mbox_fini(struct omap_mbox *mbox)
461{
Suman Anna72c1c812014-06-24 19:43:43 -0500462 struct omap_mbox_device *mdev = mbox->parent;
463
464 mutex_lock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800465
Kanigeri, Hari58256302010-11-29 20:24:14 +0000466 if (!--mbox->use_count) {
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300467 omap_mbox_disable_irq(mbox, IRQ_RX);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000468 free_irq(mbox->irq, mbox);
469 tasklet_kill(&mbox->txq->tasklet);
Tejun Heo43829732012-08-20 14:51:24 -0700470 flush_work(&mbox->rxq->work);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000471 mbox_queue_free(mbox->txq);
472 mbox_queue_free(mbox->rxq);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800473 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000474
Suman Anna72c1c812014-06-24 19:43:43 -0500475 pm_runtime_put_sync(mdev->dev);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000476
Suman Anna72c1c812014-06-24 19:43:43 -0500477 mutex_unlock(&mdev->cfg_lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800478}
479
Suman Anna72c1c812014-06-24 19:43:43 -0500480static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
481 const char *mbox_name)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800482{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000483 struct omap_mbox *_mbox, *mbox = NULL;
Suman Anna72c1c812014-06-24 19:43:43 -0500484 struct omap_mbox **mboxes = mdev->mboxes;
485 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800486
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000487 if (!mboxes)
Suman Anna72c1c812014-06-24 19:43:43 -0500488 return NULL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800489
Kevin Hilmanc0377322011-02-11 19:56:43 +0000490 for (i = 0; (_mbox = mboxes[i]); i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500491 if (!strcmp(_mbox->name, mbox_name)) {
Kevin Hilmanc0377322011-02-11 19:56:43 +0000492 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000493 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000494 }
495 }
Suman Anna72c1c812014-06-24 19:43:43 -0500496 return mbox;
497}
498
499struct omap_mbox *omap_mbox_get(const char *name, struct notifier_block *nb)
500{
501 struct omap_mbox *mbox = NULL;
502 struct omap_mbox_device *mdev;
503 int ret;
504
505 mutex_lock(&omap_mbox_devices_lock);
506 list_for_each_entry(mdev, &omap_mbox_devices, elem) {
507 mbox = omap_mbox_device_find(mdev, name);
508 if (mbox)
509 break;
510 }
511 mutex_unlock(&omap_mbox_devices_lock);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000512
513 if (!mbox)
514 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800515
Kanigeri, Hari58256302010-11-29 20:24:14 +0000516 if (nb)
517 blocking_notifier_chain_register(&mbox->notifier, nb);
518
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300519 ret = omap_mbox_startup(mbox);
520 if (ret) {
521 blocking_notifier_chain_unregister(&mbox->notifier, nb);
522 return ERR_PTR(-ENODEV);
523 }
524
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800525 return mbox;
526}
527EXPORT_SYMBOL(omap_mbox_get);
528
Kanigeri, Hari58256302010-11-29 20:24:14 +0000529void omap_mbox_put(struct omap_mbox *mbox, struct notifier_block *nb)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800530{
Kanigeri, Hari58256302010-11-29 20:24:14 +0000531 blocking_notifier_chain_unregister(&mbox->notifier, nb);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800532 omap_mbox_fini(mbox);
533}
534EXPORT_SYMBOL(omap_mbox_put);
535
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300536static struct class omap_mbox_class = { .name = "mbox", };
537
Suman Anna72c1c812014-06-24 19:43:43 -0500538static int omap_mbox_register(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800539{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000540 int ret;
541 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500542 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800543
Suman Anna72c1c812014-06-24 19:43:43 -0500544 if (!mdev || !mdev->mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800545 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800546
Suman Anna72c1c812014-06-24 19:43:43 -0500547 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000548 for (i = 0; mboxes[i]; i++) {
549 struct omap_mbox *mbox = mboxes[i];
550 mbox->dev = device_create(&omap_mbox_class,
Suman Anna72c1c812014-06-24 19:43:43 -0500551 mdev->dev, 0, mbox, "%s", mbox->name);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000552 if (IS_ERR(mbox->dev)) {
553 ret = PTR_ERR(mbox->dev);
554 goto err_out;
555 }
Kanigeri, Hari58256302010-11-29 20:24:14 +0000556
557 BLOCKING_INIT_NOTIFIER_HEAD(&mbox->notifier);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700558 }
Suman Anna72c1c812014-06-24 19:43:43 -0500559
560 mutex_lock(&omap_mbox_devices_lock);
561 list_add(&mdev->elem, &omap_mbox_devices);
562 mutex_unlock(&omap_mbox_devices_lock);
563
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700564 return 0;
565
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000566err_out:
567 while (i--)
568 device_unregister(mboxes[i]->dev);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800569 return ret;
570}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800571
Suman Anna72c1c812014-06-24 19:43:43 -0500572static int omap_mbox_unregister(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800573{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000574 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500575 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800576
Suman Anna72c1c812014-06-24 19:43:43 -0500577 if (!mdev || !mdev->mboxes)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000578 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800579
Suman Anna72c1c812014-06-24 19:43:43 -0500580 mutex_lock(&omap_mbox_devices_lock);
581 list_del(&mdev->elem);
582 mutex_unlock(&omap_mbox_devices_lock);
583
584 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000585 for (i = 0; mboxes[i]; i++)
586 device_unregister(mboxes[i]->dev);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000587 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800588}
Suman Anna5040f532014-06-24 19:43:41 -0500589
590static int omap_mbox_probe(struct platform_device *pdev)
591{
592 struct resource *mem;
593 int ret;
594 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna5040f532014-06-24 19:43:41 -0500595 struct omap_mbox_pdata *pdata = pdev->dev.platform_data;
596 struct omap_mbox_dev_info *info;
Suman Anna72c1c812014-06-24 19:43:43 -0500597 struct omap_mbox_device *mdev;
Suman Annabe3322e2014-06-24 19:43:42 -0500598 struct omap_mbox_fifo *fifo;
Suman Anna5040f532014-06-24 19:43:41 -0500599 u32 intr_type;
600 u32 l;
601 int i;
602
603 if (!pdata || !pdata->info_cnt || !pdata->info) {
604 pr_err("%s: platform not supported\n", __func__);
605 return -ENODEV;
606 }
607
Suman Anna72c1c812014-06-24 19:43:43 -0500608 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
609 if (!mdev)
610 return -ENOMEM;
611
612 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
613 mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
614 if (IS_ERR(mdev->mbox_base))
615 return PTR_ERR(mdev->mbox_base);
616
Suman Anna5040f532014-06-24 19:43:41 -0500617 /* allocate one extra for marking end of list */
618 list = devm_kzalloc(&pdev->dev, (pdata->info_cnt + 1) * sizeof(*list),
619 GFP_KERNEL);
620 if (!list)
621 return -ENOMEM;
622
623 mboxblk = devm_kzalloc(&pdev->dev, pdata->info_cnt * sizeof(*mbox),
624 GFP_KERNEL);
625 if (!mboxblk)
626 return -ENOMEM;
627
Suman Anna5040f532014-06-24 19:43:41 -0500628 info = pdata->info;
629 intr_type = pdata->intr_type;
630 mbox = mboxblk;
Suman Annabe3322e2014-06-24 19:43:42 -0500631 for (i = 0; i < pdata->info_cnt; i++, info++) {
632 fifo = &mbox->tx_fifo;
633 fifo->msg = MAILBOX_MESSAGE(info->tx_id);
634 fifo->fifo_stat = MAILBOX_FIFOSTATUS(info->tx_id);
635 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(info->tx_id);
636 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, info->usr_id);
637 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, info->usr_id);
638 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, info->usr_id);
Suman Anna5040f532014-06-24 19:43:41 -0500639
Suman Annabe3322e2014-06-24 19:43:42 -0500640 fifo = &mbox->rx_fifo;
641 fifo->msg = MAILBOX_MESSAGE(info->rx_id);
642 fifo->msg_stat = MAILBOX_MSGSTATUS(info->rx_id);
643 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(info->rx_id);
644 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, info->usr_id);
645 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, info->usr_id);
646 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, info->usr_id);
647
648 mbox->intr_type = intr_type;
649
Suman Anna72c1c812014-06-24 19:43:43 -0500650 mbox->parent = mdev;
Suman Anna5040f532014-06-24 19:43:41 -0500651 mbox->name = info->name;
652 mbox->irq = platform_get_irq(pdev, info->irq_id);
653 if (mbox->irq < 0)
654 return mbox->irq;
655 list[i] = mbox++;
656 }
657
Suman Anna72c1c812014-06-24 19:43:43 -0500658 mutex_init(&mdev->cfg_lock);
659 mdev->dev = &pdev->dev;
660 mdev->num_users = pdata->num_users;
661 mdev->num_fifos = pdata->num_fifos;
662 mdev->mboxes = list;
663 ret = omap_mbox_register(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500664 if (ret)
665 return ret;
666
Suman Anna72c1c812014-06-24 19:43:43 -0500667 platform_set_drvdata(pdev, mdev);
668 pm_runtime_enable(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500669
Suman Anna72c1c812014-06-24 19:43:43 -0500670 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500671 if (ret < 0) {
Suman Anna72c1c812014-06-24 19:43:43 -0500672 pm_runtime_put_noidle(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500673 goto unregister;
674 }
675
676 /*
677 * just print the raw revision register, the format is not
678 * uniform across all SoCs
679 */
Suman Anna72c1c812014-06-24 19:43:43 -0500680 l = mbox_read_reg(mdev, MAILBOX_REVISION);
681 dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
Suman Anna5040f532014-06-24 19:43:41 -0500682
Suman Anna72c1c812014-06-24 19:43:43 -0500683 ret = pm_runtime_put_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500684 if (ret < 0)
685 goto unregister;
686
687 return 0;
688
689unregister:
Suman Anna72c1c812014-06-24 19:43:43 -0500690 pm_runtime_disable(mdev->dev);
691 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500692 return ret;
693}
694
695static int omap_mbox_remove(struct platform_device *pdev)
696{
Suman Anna72c1c812014-06-24 19:43:43 -0500697 struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
698
699 pm_runtime_disable(mdev->dev);
700 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500701
702 return 0;
703}
704
705static struct platform_driver omap_mbox_driver = {
706 .probe = omap_mbox_probe,
707 .remove = omap_mbox_remove,
708 .driver = {
709 .name = "omap-mailbox",
710 .owner = THIS_MODULE,
711 },
712};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800713
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800714static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800715{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300716 int err;
717
718 err = class_register(&omap_mbox_class);
719 if (err)
720 return err;
721
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000722 /* kfifo size sanity check: alignment and minimal size */
723 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000724 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
725 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000726
Suman Anna5040f532014-06-24 19:43:41 -0500727 return platform_driver_register(&omap_mbox_driver);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800728}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300729subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800730
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800731static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800732{
Suman Anna5040f532014-06-24 19:43:41 -0500733 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300734 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800735}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800736module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800737
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700738MODULE_LICENSE("GPL v2");
739MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000740MODULE_AUTHOR("Toshihiro Kobayashi");
741MODULE_AUTHOR("Hiroshi DOYU");