blob: a3dbfd9c647994ec6794701cfc25a327739640ad [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>
Paul Gortmaker73017a52011-07-31 16:14:14 -040032#include <linux/module.h>
Suman Anna75288cc2014-09-10 14:20:59 -050033#include <linux/of_device.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>
Suman Anna8841a662014-11-03 17:05:50 -060038#include <linux/mailbox_controller.h>
39#include <linux/mailbox_client.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070040
Suman Anna5040f532014-06-24 19:43:41 -050041#define MAILBOX_REVISION 0x000
42#define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
43#define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
44#define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
Hiroshi DOYU340a6142006-12-07 15:43:59 -080045
Suman Anna5040f532014-06-24 19:43:41 -050046#define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
47#define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
48
49#define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
50#define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
51#define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
52
53#define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \
54 OMAP2_MAILBOX_IRQSTATUS(u))
55#define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \
56 OMAP2_MAILBOX_IRQENABLE(u))
57#define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \
58 : OMAP2_MAILBOX_IRQENABLE(u))
59
60#define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
61#define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
62
63#define MBOX_REG_SIZE 0x120
64
65#define OMAP4_MBOX_REG_SIZE 0x130
66
67#define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
68#define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
69
70struct omap_mbox_fifo {
71 unsigned long msg;
72 unsigned long fifo_stat;
73 unsigned long msg_stat;
Suman Anna5040f532014-06-24 19:43:41 -050074 unsigned long irqenable;
75 unsigned long irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -050076 unsigned long irqdisable;
Suman Annabe3322e2014-06-24 19:43:42 -050077 u32 intr_bit;
Suman Anna5040f532014-06-24 19:43:41 -050078};
79
80struct omap_mbox_queue {
81 spinlock_t lock;
82 struct kfifo fifo;
83 struct work_struct work;
Suman Anna5040f532014-06-24 19:43:41 -050084 struct omap_mbox *mbox;
85 bool full;
86};
87
Suman Anna72c1c812014-06-24 19:43:43 -050088struct omap_mbox_device {
89 struct device *dev;
90 struct mutex cfg_lock;
91 void __iomem *mbox_base;
92 u32 num_users;
93 u32 num_fifos;
94 struct omap_mbox **mboxes;
Suman Anna8841a662014-11-03 17:05:50 -060095 struct mbox_controller controller;
Suman Anna72c1c812014-06-24 19:43:43 -050096 struct list_head elem;
97};
98
Suman Anna75288cc2014-09-10 14:20:59 -050099struct omap_mbox_fifo_info {
100 int tx_id;
101 int tx_usr;
102 int tx_irq;
103
104 int rx_id;
105 int rx_usr;
106 int rx_irq;
107
108 const char *name;
109};
110
Suman Anna5040f532014-06-24 19:43:41 -0500111struct omap_mbox {
112 const char *name;
113 int irq;
Suman Anna8841a662014-11-03 17:05:50 -0600114 struct omap_mbox_queue *rxq;
Suman Anna5040f532014-06-24 19:43:41 -0500115 struct device *dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500116 struct omap_mbox_device *parent;
Suman Annabe3322e2014-06-24 19:43:42 -0500117 struct omap_mbox_fifo tx_fifo;
118 struct omap_mbox_fifo rx_fifo;
119 u32 ctx[OMAP4_MBOX_NR_REGS];
120 u32 intr_type;
Suman Anna8841a662014-11-03 17:05:50 -0600121 struct mbox_chan *chan;
Suman Anna5040f532014-06-24 19:43:41 -0500122};
123
Suman Anna72c1c812014-06-24 19:43:43 -0500124/* global variables for the mailbox devices */
125static DEFINE_MUTEX(omap_mbox_devices_lock);
126static LIST_HEAD(omap_mbox_devices);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800127
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000128static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
129module_param(mbox_kfifo_size, uint, S_IRUGO);
130MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
131
Suman Anna8841a662014-11-03 17:05:50 -0600132static struct omap_mbox *mbox_chan_to_omap_mbox(struct mbox_chan *chan)
133{
134 if (!chan || !chan->con_priv)
135 return NULL;
136
137 return (struct omap_mbox *)chan->con_priv;
138}
139
Suman Anna72c1c812014-06-24 19:43:43 -0500140static inline
141unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500142{
Suman Anna72c1c812014-06-24 19:43:43 -0500143 return __raw_readl(mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500144}
145
Suman Anna72c1c812014-06-24 19:43:43 -0500146static inline
147void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500148{
Suman Anna72c1c812014-06-24 19:43:43 -0500149 __raw_writel(val, mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500150}
151
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700152/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500153static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700154{
Suman Annabe3322e2014-06-24 19:43:42 -0500155 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500156 return (mbox_msg_t) mbox_read_reg(mbox->parent, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700157}
Suman Anna5040f532014-06-24 19:43:41 -0500158
159static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700160{
Suman Annabe3322e2014-06-24 19:43:42 -0500161 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500162 mbox_write_reg(mbox->parent, msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700163}
Suman Anna5040f532014-06-24 19:43:41 -0500164
165static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700166{
Suman Annabe3322e2014-06-24 19:43:42 -0500167 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500168 return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700169}
Suman Anna5040f532014-06-24 19:43:41 -0500170
171static int mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700172{
Suman Annabe3322e2014-06-24 19:43:42 -0500173 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500174 return mbox_read_reg(mbox->parent, fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700175}
176
177/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500178static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700179{
Suman Annabe3322e2014-06-24 19:43:42 -0500180 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
181 &mbox->tx_fifo : &mbox->rx_fifo;
182 u32 bit = fifo->intr_bit;
183 u32 irqstatus = fifo->irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -0500184
Suman Anna72c1c812014-06-24 19:43:43 -0500185 mbox_write_reg(mbox->parent, bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500186
187 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Anna72c1c812014-06-24 19:43:43 -0500188 mbox_read_reg(mbox->parent, irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700189}
Suman Anna5040f532014-06-24 19:43:41 -0500190
191static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700192{
Suman Annabe3322e2014-06-24 19:43:42 -0500193 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
194 &mbox->tx_fifo : &mbox->rx_fifo;
195 u32 bit = fifo->intr_bit;
196 u32 irqenable = fifo->irqenable;
197 u32 irqstatus = fifo->irqstatus;
198
Suman Anna72c1c812014-06-24 19:43:43 -0500199 u32 enable = mbox_read_reg(mbox->parent, irqenable);
200 u32 status = mbox_read_reg(mbox->parent, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500201
202 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700203}
204
Suman Anna8841a662014-11-03 17:05:50 -0600205void omap_mbox_save_ctx(struct mbox_chan *chan)
Suman Annac869c752013-03-12 17:55:29 -0500206{
Suman Anna5040f532014-06-24 19:43:41 -0500207 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500208 int nr_regs;
Suman Anna8841a662014-11-03 17:05:50 -0600209 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
210
211 if (WARN_ON(!mbox))
212 return;
Suman Annac869c752013-03-12 17:55:29 -0500213
Suman Annabe3322e2014-06-24 19:43:42 -0500214 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500215 nr_regs = OMAP4_MBOX_NR_REGS;
216 else
217 nr_regs = MBOX_NR_REGS;
218 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500219 mbox->ctx[i] = mbox_read_reg(mbox->parent, i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500220
221 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500222 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500223 }
Suman Annac869c752013-03-12 17:55:29 -0500224}
225EXPORT_SYMBOL(omap_mbox_save_ctx);
226
Suman Anna8841a662014-11-03 17:05:50 -0600227void omap_mbox_restore_ctx(struct mbox_chan *chan)
Suman Annac869c752013-03-12 17:55:29 -0500228{
Suman Anna5040f532014-06-24 19:43:41 -0500229 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500230 int nr_regs;
Suman Anna8841a662014-11-03 17:05:50 -0600231 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
232
233 if (WARN_ON(!mbox))
234 return;
Suman Annac869c752013-03-12 17:55:29 -0500235
Suman Annabe3322e2014-06-24 19:43:42 -0500236 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500237 nr_regs = OMAP4_MBOX_NR_REGS;
238 else
239 nr_regs = MBOX_NR_REGS;
240 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500241 mbox_write_reg(mbox->parent, mbox->ctx[i], i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500242 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500243 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500244 }
Suman Annac869c752013-03-12 17:55:29 -0500245}
246EXPORT_SYMBOL(omap_mbox_restore_ctx);
247
Suman Anna8841a662014-11-03 17:05:50 -0600248static void _omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500249{
Suman Annabe3322e2014-06-24 19:43:42 -0500250 u32 l;
251 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
252 &mbox->tx_fifo : &mbox->rx_fifo;
253 u32 bit = fifo->intr_bit;
254 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500255
Suman Anna72c1c812014-06-24 19:43:43 -0500256 l = mbox_read_reg(mbox->parent, irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500257 l |= bit;
Suman Anna72c1c812014-06-24 19:43:43 -0500258 mbox_write_reg(mbox->parent, l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500259}
Suman Annac869c752013-03-12 17:55:29 -0500260
Suman Anna8841a662014-11-03 17:05:50 -0600261static void _omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500262{
Suman Annabe3322e2014-06-24 19:43:42 -0500263 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
264 &mbox->tx_fifo : &mbox->rx_fifo;
265 u32 bit = fifo->intr_bit;
266 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500267
268 /*
269 * Read and update the interrupt configuration register for pre-OMAP4.
270 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
271 */
Suman Annabe3322e2014-06-24 19:43:42 -0500272 if (!mbox->intr_type)
Suman Anna72c1c812014-06-24 19:43:43 -0500273 bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500274
Suman Anna72c1c812014-06-24 19:43:43 -0500275 mbox_write_reg(mbox->parent, bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500276}
Suman Annac869c752013-03-12 17:55:29 -0500277
Suman Anna8841a662014-11-03 17:05:50 -0600278void omap_mbox_enable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800279{
Suman Anna8841a662014-11-03 17:05:50 -0600280 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800281
Suman Anna8841a662014-11-03 17:05:50 -0600282 if (WARN_ON(!mbox))
283 return;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000284
Suman Anna8841a662014-11-03 17:05:50 -0600285 _omap_mbox_enable_irq(mbox, irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800286}
Suman Anna8841a662014-11-03 17:05:50 -0600287EXPORT_SYMBOL(omap_mbox_enable_irq);
288
289void omap_mbox_disable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
290{
291 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
292
293 if (WARN_ON(!mbox))
294 return;
295
296 _omap_mbox_disable_irq(mbox, irq);
297}
298EXPORT_SYMBOL(omap_mbox_disable_irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800299
300/*
301 * Message receiver(workqueue)
302 */
303static void mbox_rx_work(struct work_struct *work)
304{
305 struct omap_mbox_queue *mq =
306 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800307 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000308 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800309
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000310 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
311 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
312 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800313
Suman Anna8841a662014-11-03 17:05:50 -0600314 mbox_chan_received_data(mq->mbox->chan, (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000315 spin_lock_irq(&mq->lock);
316 if (mq->full) {
317 mq->full = false;
Suman Anna8841a662014-11-03 17:05:50 -0600318 _omap_mbox_enable_irq(mq->mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000319 }
320 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800321 }
322}
323
324/*
325 * Mailbox interrupt handler
326 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800327static void __mbox_tx_interrupt(struct omap_mbox *mbox)
328{
Suman Anna8841a662014-11-03 17:05:50 -0600329 _omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800330 ack_mbox_irq(mbox, IRQ_TX);
Suman Anna8841a662014-11-03 17:05:50 -0600331 mbox_chan_txdone(mbox->chan, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800332}
333
334static void __mbox_rx_interrupt(struct omap_mbox *mbox)
335{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000336 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800337 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000338 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800339
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800340 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000341 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Suman Anna8841a662014-11-03 17:05:50 -0600342 _omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000343 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800344 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600345 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800346
347 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800348
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000349 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
350 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800351 }
352
353 /* no more messages in the fifo. clear IRQ source. */
354 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700355nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100356 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800357}
358
359static irqreturn_t mbox_interrupt(int irq, void *p)
360{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400361 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800362
363 if (is_mbox_irq(mbox, IRQ_TX))
364 __mbox_tx_interrupt(mbox);
365
366 if (is_mbox_irq(mbox, IRQ_RX))
367 __mbox_rx_interrupt(mbox);
368
369 return IRQ_HANDLED;
370}
371
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800372static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Suman Anna8841a662014-11-03 17:05:50 -0600373 void (*work)(struct work_struct *))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800374{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800375 struct omap_mbox_queue *mq;
376
Suman Anna8841a662014-11-03 17:05:50 -0600377 if (!work)
378 return NULL;
379
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800380 mq = kzalloc(sizeof(struct omap_mbox_queue), GFP_KERNEL);
381 if (!mq)
382 return NULL;
383
384 spin_lock_init(&mq->lock);
385
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000386 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800387 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800388
Suman Anna8841a662014-11-03 17:05:50 -0600389 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800390 return mq;
Suman Anna8841a662014-11-03 17:05:50 -0600391
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800392error:
393 kfree(mq);
394 return NULL;
395}
396
397static void mbox_queue_free(struct omap_mbox_queue *q)
398{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000399 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800400 kfree(q);
401}
402
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800403static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800404{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800405 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800406 struct omap_mbox_queue *mq;
407
Suman Anna8841a662014-11-03 17:05:50 -0600408 mq = mbox_queue_alloc(mbox, mbox_rx_work);
409 if (!mq)
410 return -ENOMEM;
411 mbox->rxq = mq;
412 mq->mbox = mbox;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800413
Suman Anna8841a662014-11-03 17:05:50 -0600414 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
415 mbox->name, mbox);
416 if (unlikely(ret)) {
417 pr_err("failed to register mailbox interrupt:%d\n", ret);
418 goto fail_request_irq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800419 }
Suman Anna8841a662014-11-03 17:05:50 -0600420
421 _omap_mbox_enable_irq(mbox, IRQ_RX);
422
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800423 return 0;
424
Suman Annaecf305c2013-02-01 20:37:06 -0600425fail_request_irq:
426 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800427 return ret;
428}
429
430static void omap_mbox_fini(struct omap_mbox *mbox)
431{
Suman Anna8841a662014-11-03 17:05:50 -0600432 _omap_mbox_disable_irq(mbox, IRQ_RX);
433 free_irq(mbox->irq, mbox);
434 flush_work(&mbox->rxq->work);
435 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800436}
437
Suman Anna72c1c812014-06-24 19:43:43 -0500438static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
439 const char *mbox_name)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800440{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000441 struct omap_mbox *_mbox, *mbox = NULL;
Suman Anna72c1c812014-06-24 19:43:43 -0500442 struct omap_mbox **mboxes = mdev->mboxes;
443 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800444
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000445 if (!mboxes)
Suman Anna72c1c812014-06-24 19:43:43 -0500446 return NULL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800447
Kevin Hilmanc0377322011-02-11 19:56:43 +0000448 for (i = 0; (_mbox = mboxes[i]); i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500449 if (!strcmp(_mbox->name, mbox_name)) {
Kevin Hilmanc0377322011-02-11 19:56:43 +0000450 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000451 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000452 }
453 }
Suman Anna72c1c812014-06-24 19:43:43 -0500454 return mbox;
455}
456
Suman Anna8841a662014-11-03 17:05:50 -0600457struct mbox_chan *omap_mbox_request_channel(struct mbox_client *cl,
458 const char *chan_name)
Suman Anna72c1c812014-06-24 19:43:43 -0500459{
Suman Anna8841a662014-11-03 17:05:50 -0600460 struct device *dev = cl->dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500461 struct omap_mbox *mbox = NULL;
462 struct omap_mbox_device *mdev;
Suman Anna8841a662014-11-03 17:05:50 -0600463 struct mbox_chan *chan;
464 unsigned long flags;
Suman Anna72c1c812014-06-24 19:43:43 -0500465 int ret;
466
Suman Anna8841a662014-11-03 17:05:50 -0600467 if (!dev)
468 return ERR_PTR(-ENODEV);
469
470 if (dev->of_node) {
471 pr_err("%s: please use mbox_request_channel(), this API is supported only for OMAP non-DT usage\n",
472 __func__);
473 return ERR_PTR(-ENODEV);
474 }
475
Suman Anna72c1c812014-06-24 19:43:43 -0500476 mutex_lock(&omap_mbox_devices_lock);
477 list_for_each_entry(mdev, &omap_mbox_devices, elem) {
Suman Anna8841a662014-11-03 17:05:50 -0600478 mbox = omap_mbox_device_find(mdev, chan_name);
Suman Anna72c1c812014-06-24 19:43:43 -0500479 if (mbox)
480 break;
481 }
482 mutex_unlock(&omap_mbox_devices_lock);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000483
Suman Anna8841a662014-11-03 17:05:50 -0600484 if (!mbox || !mbox->chan)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000485 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800486
Suman Anna8841a662014-11-03 17:05:50 -0600487 chan = mbox->chan;
488 spin_lock_irqsave(&chan->lock, flags);
489 chan->msg_free = 0;
490 chan->msg_count = 0;
491 chan->active_req = NULL;
492 chan->cl = cl;
493 init_completion(&chan->tx_complete);
494 spin_unlock_irqrestore(&chan->lock, flags);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000495
Suman Anna8841a662014-11-03 17:05:50 -0600496 ret = chan->mbox->ops->startup(chan);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300497 if (ret) {
Suman Anna8841a662014-11-03 17:05:50 -0600498 pr_err("Unable to startup the chan (%d)\n", ret);
499 mbox_free_channel(chan);
500 chan = ERR_PTR(ret);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300501 }
502
Suman Anna8841a662014-11-03 17:05:50 -0600503 return chan;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800504}
Suman Anna8841a662014-11-03 17:05:50 -0600505EXPORT_SYMBOL(omap_mbox_request_channel);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800506
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300507static struct class omap_mbox_class = { .name = "mbox", };
508
Suman Anna72c1c812014-06-24 19:43:43 -0500509static int omap_mbox_register(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800510{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000511 int ret;
512 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500513 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800514
Suman Anna72c1c812014-06-24 19:43:43 -0500515 if (!mdev || !mdev->mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800516 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800517
Suman Anna72c1c812014-06-24 19:43:43 -0500518 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000519 for (i = 0; mboxes[i]; i++) {
520 struct omap_mbox *mbox = mboxes[i];
Suman Anna8841a662014-11-03 17:05:50 -0600521 mbox->dev = device_create(&omap_mbox_class, mdev->dev,
522 0, mbox, "%s", mbox->name);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000523 if (IS_ERR(mbox->dev)) {
524 ret = PTR_ERR(mbox->dev);
525 goto err_out;
526 }
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700527 }
Suman Anna72c1c812014-06-24 19:43:43 -0500528
529 mutex_lock(&omap_mbox_devices_lock);
530 list_add(&mdev->elem, &omap_mbox_devices);
531 mutex_unlock(&omap_mbox_devices_lock);
532
Suman Anna8841a662014-11-03 17:05:50 -0600533 ret = mbox_controller_register(&mdev->controller);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700534
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000535err_out:
Suman Anna8841a662014-11-03 17:05:50 -0600536 if (ret) {
537 while (i--)
538 device_unregister(mboxes[i]->dev);
539 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800540 return ret;
541}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800542
Suman Anna72c1c812014-06-24 19:43:43 -0500543static int omap_mbox_unregister(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800544{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000545 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500546 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800547
Suman Anna72c1c812014-06-24 19:43:43 -0500548 if (!mdev || !mdev->mboxes)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000549 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800550
Suman Anna72c1c812014-06-24 19:43:43 -0500551 mutex_lock(&omap_mbox_devices_lock);
552 list_del(&mdev->elem);
553 mutex_unlock(&omap_mbox_devices_lock);
554
Suman Anna8841a662014-11-03 17:05:50 -0600555 mbox_controller_unregister(&mdev->controller);
556
Suman Anna72c1c812014-06-24 19:43:43 -0500557 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000558 for (i = 0; mboxes[i]; i++)
559 device_unregister(mboxes[i]->dev);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000560 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800561}
Suman Anna5040f532014-06-24 19:43:41 -0500562
Suman Anna8841a662014-11-03 17:05:50 -0600563static int omap_mbox_chan_startup(struct mbox_chan *chan)
564{
565 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
566 struct omap_mbox_device *mdev = mbox->parent;
567 int ret = 0;
568
569 mutex_lock(&mdev->cfg_lock);
570 pm_runtime_get_sync(mdev->dev);
571 ret = omap_mbox_startup(mbox);
572 if (ret)
573 pm_runtime_put_sync(mdev->dev);
574 mutex_unlock(&mdev->cfg_lock);
575 return ret;
576}
577
578static void omap_mbox_chan_shutdown(struct mbox_chan *chan)
579{
580 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
581 struct omap_mbox_device *mdev = mbox->parent;
582
583 mutex_lock(&mdev->cfg_lock);
584 omap_mbox_fini(mbox);
585 pm_runtime_put_sync(mdev->dev);
586 mutex_unlock(&mdev->cfg_lock);
587}
588
589static int omap_mbox_chan_send_data(struct mbox_chan *chan, void *data)
590{
591 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
592 int ret = -EBUSY;
593
594 if (!mbox)
595 return -EINVAL;
596
597 if (!mbox_fifo_full(mbox)) {
598 mbox_fifo_write(mbox, (mbox_msg_t)data);
599 ret = 0;
600 }
601
602 /* always enable the interrupt */
603 _omap_mbox_enable_irq(mbox, IRQ_TX);
604 return ret;
605}
606
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700607static const struct mbox_chan_ops omap_mbox_chan_ops = {
Suman Anna8841a662014-11-03 17:05:50 -0600608 .startup = omap_mbox_chan_startup,
609 .send_data = omap_mbox_chan_send_data,
610 .shutdown = omap_mbox_chan_shutdown,
611};
612
Suman Anna75288cc2014-09-10 14:20:59 -0500613static const struct of_device_id omap_mailbox_of_match[] = {
614 {
615 .compatible = "ti,omap2-mailbox",
616 .data = (void *)MBOX_INTR_CFG_TYPE1,
617 },
618 {
619 .compatible = "ti,omap3-mailbox",
620 .data = (void *)MBOX_INTR_CFG_TYPE1,
621 },
622 {
623 .compatible = "ti,omap4-mailbox",
624 .data = (void *)MBOX_INTR_CFG_TYPE2,
625 },
626 {
627 /* end */
628 },
629};
630MODULE_DEVICE_TABLE(of, omap_mailbox_of_match);
631
Suman Anna8841a662014-11-03 17:05:50 -0600632static struct mbox_chan *omap_mbox_of_xlate(struct mbox_controller *controller,
633 const struct of_phandle_args *sp)
634{
635 phandle phandle = sp->args[0];
636 struct device_node *node;
637 struct omap_mbox_device *mdev;
638 struct omap_mbox *mbox;
639
640 mdev = container_of(controller, struct omap_mbox_device, controller);
641 if (WARN_ON(!mdev))
Benson Leung2d805fc2015-05-04 10:36:36 -0700642 return ERR_PTR(-EINVAL);
Suman Anna8841a662014-11-03 17:05:50 -0600643
644 node = of_find_node_by_phandle(phandle);
645 if (!node) {
646 pr_err("%s: could not find node phandle 0x%x\n",
647 __func__, phandle);
Benson Leung2d805fc2015-05-04 10:36:36 -0700648 return ERR_PTR(-ENODEV);
Suman Anna8841a662014-11-03 17:05:50 -0600649 }
650
651 mbox = omap_mbox_device_find(mdev, node->name);
652 of_node_put(node);
Benson Leung2d805fc2015-05-04 10:36:36 -0700653 return mbox ? mbox->chan : ERR_PTR(-ENOENT);
Suman Anna8841a662014-11-03 17:05:50 -0600654}
655
Suman Anna5040f532014-06-24 19:43:41 -0500656static int omap_mbox_probe(struct platform_device *pdev)
657{
658 struct resource *mem;
659 int ret;
Suman Anna8841a662014-11-03 17:05:50 -0600660 struct mbox_chan *chnls;
Suman Anna5040f532014-06-24 19:43:41 -0500661 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna5040f532014-06-24 19:43:41 -0500662 struct omap_mbox_pdata *pdata = pdev->dev.platform_data;
Suman Anna75288cc2014-09-10 14:20:59 -0500663 struct omap_mbox_dev_info *info = NULL;
664 struct omap_mbox_fifo_info *finfo, *finfoblk;
Suman Anna72c1c812014-06-24 19:43:43 -0500665 struct omap_mbox_device *mdev;
Suman Annabe3322e2014-06-24 19:43:42 -0500666 struct omap_mbox_fifo *fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500667 struct device_node *node = pdev->dev.of_node;
668 struct device_node *child;
669 const struct of_device_id *match;
670 u32 intr_type, info_count;
671 u32 num_users, num_fifos;
672 u32 tmp[3];
Suman Anna5040f532014-06-24 19:43:41 -0500673 u32 l;
674 int i;
675
Suman Anna75288cc2014-09-10 14:20:59 -0500676 if (!node && (!pdata || !pdata->info_cnt || !pdata->info)) {
Suman Anna5040f532014-06-24 19:43:41 -0500677 pr_err("%s: platform not supported\n", __func__);
678 return -ENODEV;
679 }
680
Suman Anna75288cc2014-09-10 14:20:59 -0500681 if (node) {
682 match = of_match_device(omap_mailbox_of_match, &pdev->dev);
683 if (!match)
684 return -ENODEV;
685 intr_type = (u32)match->data;
686
687 if (of_property_read_u32(node, "ti,mbox-num-users",
688 &num_users))
689 return -ENODEV;
690
691 if (of_property_read_u32(node, "ti,mbox-num-fifos",
692 &num_fifos))
693 return -ENODEV;
694
695 info_count = of_get_available_child_count(node);
696 if (!info_count) {
697 dev_err(&pdev->dev, "no available mbox devices found\n");
698 return -ENODEV;
699 }
700 } else { /* non-DT device creation */
701 info_count = pdata->info_cnt;
702 info = pdata->info;
703 intr_type = pdata->intr_type;
704 num_users = pdata->num_users;
705 num_fifos = pdata->num_fifos;
706 }
707
708 finfoblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*finfoblk),
709 GFP_KERNEL);
710 if (!finfoblk)
711 return -ENOMEM;
712
713 finfo = finfoblk;
714 child = NULL;
715 for (i = 0; i < info_count; i++, finfo++) {
716 if (node) {
717 child = of_get_next_available_child(node, child);
718 ret = of_property_read_u32_array(child, "ti,mbox-tx",
719 tmp, ARRAY_SIZE(tmp));
720 if (ret)
721 return ret;
722 finfo->tx_id = tmp[0];
723 finfo->tx_irq = tmp[1];
724 finfo->tx_usr = tmp[2];
725
726 ret = of_property_read_u32_array(child, "ti,mbox-rx",
727 tmp, ARRAY_SIZE(tmp));
728 if (ret)
729 return ret;
730 finfo->rx_id = tmp[0];
731 finfo->rx_irq = tmp[1];
732 finfo->rx_usr = tmp[2];
733
734 finfo->name = child->name;
735 } else {
736 finfo->tx_id = info->tx_id;
737 finfo->rx_id = info->rx_id;
738 finfo->tx_usr = info->usr_id;
739 finfo->tx_irq = info->irq_id;
740 finfo->rx_usr = info->usr_id;
741 finfo->rx_irq = info->irq_id;
742 finfo->name = info->name;
743 info++;
744 }
745 if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos ||
746 finfo->tx_usr >= num_users || finfo->rx_usr >= num_users)
747 return -EINVAL;
748 }
749
Suman Anna72c1c812014-06-24 19:43:43 -0500750 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
751 if (!mdev)
752 return -ENOMEM;
753
754 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
755 mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
756 if (IS_ERR(mdev->mbox_base))
757 return PTR_ERR(mdev->mbox_base);
758
Suman Anna5040f532014-06-24 19:43:41 -0500759 /* allocate one extra for marking end of list */
Suman Anna75288cc2014-09-10 14:20:59 -0500760 list = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*list),
Suman Anna5040f532014-06-24 19:43:41 -0500761 GFP_KERNEL);
762 if (!list)
763 return -ENOMEM;
764
Suman Anna8841a662014-11-03 17:05:50 -0600765 chnls = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*chnls),
766 GFP_KERNEL);
767 if (!chnls)
768 return -ENOMEM;
769
Suman Anna75288cc2014-09-10 14:20:59 -0500770 mboxblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*mbox),
Suman Anna5040f532014-06-24 19:43:41 -0500771 GFP_KERNEL);
772 if (!mboxblk)
773 return -ENOMEM;
774
Suman Anna5040f532014-06-24 19:43:41 -0500775 mbox = mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500776 finfo = finfoblk;
777 for (i = 0; i < info_count; i++, finfo++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500778 fifo = &mbox->tx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500779 fifo->msg = MAILBOX_MESSAGE(finfo->tx_id);
780 fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id);
781 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id);
782 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr);
783 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr);
784 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr);
Suman Anna5040f532014-06-24 19:43:41 -0500785
Suman Annabe3322e2014-06-24 19:43:42 -0500786 fifo = &mbox->rx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500787 fifo->msg = MAILBOX_MESSAGE(finfo->rx_id);
788 fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id);
789 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id);
790 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr);
791 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr);
792 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr);
Suman Annabe3322e2014-06-24 19:43:42 -0500793
794 mbox->intr_type = intr_type;
795
Suman Anna72c1c812014-06-24 19:43:43 -0500796 mbox->parent = mdev;
Suman Anna75288cc2014-09-10 14:20:59 -0500797 mbox->name = finfo->name;
798 mbox->irq = platform_get_irq(pdev, finfo->tx_irq);
Suman Anna5040f532014-06-24 19:43:41 -0500799 if (mbox->irq < 0)
800 return mbox->irq;
Suman Anna8841a662014-11-03 17:05:50 -0600801 mbox->chan = &chnls[i];
802 chnls[i].con_priv = mbox;
Suman Anna5040f532014-06-24 19:43:41 -0500803 list[i] = mbox++;
804 }
805
Suman Anna72c1c812014-06-24 19:43:43 -0500806 mutex_init(&mdev->cfg_lock);
807 mdev->dev = &pdev->dev;
Suman Anna75288cc2014-09-10 14:20:59 -0500808 mdev->num_users = num_users;
809 mdev->num_fifos = num_fifos;
Suman Anna72c1c812014-06-24 19:43:43 -0500810 mdev->mboxes = list;
Suman Anna8841a662014-11-03 17:05:50 -0600811
812 /* OMAP does not have a Tx-Done IRQ, but rather a Tx-Ready IRQ */
813 mdev->controller.txdone_irq = true;
814 mdev->controller.dev = mdev->dev;
815 mdev->controller.ops = &omap_mbox_chan_ops;
816 mdev->controller.chans = chnls;
817 mdev->controller.num_chans = info_count;
818 mdev->controller.of_xlate = omap_mbox_of_xlate;
Suman Anna72c1c812014-06-24 19:43:43 -0500819 ret = omap_mbox_register(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500820 if (ret)
821 return ret;
822
Suman Anna72c1c812014-06-24 19:43:43 -0500823 platform_set_drvdata(pdev, mdev);
824 pm_runtime_enable(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500825
Suman Anna72c1c812014-06-24 19:43:43 -0500826 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500827 if (ret < 0) {
Suman Anna72c1c812014-06-24 19:43:43 -0500828 pm_runtime_put_noidle(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500829 goto unregister;
830 }
831
832 /*
833 * just print the raw revision register, the format is not
834 * uniform across all SoCs
835 */
Suman Anna72c1c812014-06-24 19:43:43 -0500836 l = mbox_read_reg(mdev, MAILBOX_REVISION);
837 dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
Suman Anna5040f532014-06-24 19:43:41 -0500838
Suman Anna72c1c812014-06-24 19:43:43 -0500839 ret = pm_runtime_put_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500840 if (ret < 0)
841 goto unregister;
842
Suman Anna75288cc2014-09-10 14:20:59 -0500843 devm_kfree(&pdev->dev, finfoblk);
Suman Anna5040f532014-06-24 19:43:41 -0500844 return 0;
845
846unregister:
Suman Anna72c1c812014-06-24 19:43:43 -0500847 pm_runtime_disable(mdev->dev);
848 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500849 return ret;
850}
851
852static int omap_mbox_remove(struct platform_device *pdev)
853{
Suman Anna72c1c812014-06-24 19:43:43 -0500854 struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
855
856 pm_runtime_disable(mdev->dev);
857 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500858
859 return 0;
860}
861
862static struct platform_driver omap_mbox_driver = {
863 .probe = omap_mbox_probe,
864 .remove = omap_mbox_remove,
865 .driver = {
866 .name = "omap-mailbox",
Suman Anna75288cc2014-09-10 14:20:59 -0500867 .of_match_table = of_match_ptr(omap_mailbox_of_match),
Suman Anna5040f532014-06-24 19:43:41 -0500868 },
869};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800870
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800871static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800872{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300873 int err;
874
875 err = class_register(&omap_mbox_class);
876 if (err)
877 return err;
878
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000879 /* kfifo size sanity check: alignment and minimal size */
880 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000881 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
882 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000883
Suman Anna5040f532014-06-24 19:43:41 -0500884 return platform_driver_register(&omap_mbox_driver);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800885}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300886subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800887
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800888static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800889{
Suman Anna5040f532014-06-24 19:43:41 -0500890 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300891 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800892}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800893module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800894
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700895MODULE_LICENSE("GPL v2");
896MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000897MODULE_AUTHOR("Toshihiro Kobayashi");
898MODULE_AUTHOR("Hiroshi DOYU");