blob: d837def4c3907b9332f449600cadb5405697fc82 [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 Anna4899f78a2016-04-06 12:37:37 -05005 * Copyright (C) 2013-2016 Texas Instruments Incorporated - http://www.ti.com
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.
Hiroshi DOYU340a6142006-12-07 15:43:59 -080018 */
19
Hiroshi DOYU340a6142006-12-07 15:43:59 -080020#include <linux/interrupt.h>
Felipe Contrerasb3e69142010-06-11 15:51:49 +000021#include <linux/spinlock.h>
22#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000024#include <linux/kfifo.h>
25#include <linux/err.h>
Paul Gortmaker73017a52011-07-31 16:14:14 -040026#include <linux/module.h>
Suman Anna75288cc2014-09-10 14:20:59 -050027#include <linux/of_device.h>
Suman Anna5040f532014-06-24 19:43:41 -050028#include <linux/platform_device.h>
29#include <linux/pm_runtime.h>
Suman Anna5040f532014-06-24 19:43:41 -050030#include <linux/omap-mailbox.h>
Suman Anna8841a662014-11-03 17:05:50 -060031#include <linux/mailbox_controller.h>
32#include <linux/mailbox_client.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070033
Dave Gerlach8e3c5952015-09-22 19:14:52 -050034#include "mailbox.h"
35
Suman Anna5040f532014-06-24 19:43:41 -050036#define MAILBOX_REVISION 0x000
37#define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
38#define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
39#define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
Hiroshi DOYU340a6142006-12-07 15:43:59 -080040
Suman Anna5040f532014-06-24 19:43:41 -050041#define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
42#define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
43
44#define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
45#define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
46#define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
47
48#define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \
49 OMAP2_MAILBOX_IRQSTATUS(u))
50#define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \
51 OMAP2_MAILBOX_IRQENABLE(u))
52#define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \
53 : OMAP2_MAILBOX_IRQENABLE(u))
54
55#define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
56#define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
57
58#define MBOX_REG_SIZE 0x120
59
60#define OMAP4_MBOX_REG_SIZE 0x130
61
62#define MBOX_NR_REGS (MBOX_REG_SIZE / sizeof(u32))
63#define OMAP4_MBOX_NR_REGS (OMAP4_MBOX_REG_SIZE / sizeof(u32))
64
Suman Anna4899f78a2016-04-06 12:37:37 -050065/* Interrupt register configuration types */
66#define MBOX_INTR_CFG_TYPE1 0
67#define MBOX_INTR_CFG_TYPE2 1
68
Suman Anna5040f532014-06-24 19:43:41 -050069struct omap_mbox_fifo {
70 unsigned long msg;
71 unsigned long fifo_stat;
72 unsigned long msg_stat;
Suman Anna5040f532014-06-24 19:43:41 -050073 unsigned long irqenable;
74 unsigned long irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -050075 unsigned long irqdisable;
Suman Annabe3322e2014-06-24 19:43:42 -050076 u32 intr_bit;
Suman Anna5040f532014-06-24 19:43:41 -050077};
78
79struct omap_mbox_queue {
80 spinlock_t lock;
81 struct kfifo fifo;
82 struct work_struct work;
Suman Anna5040f532014-06-24 19:43:41 -050083 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;
Suman Anna2240f8a2016-04-06 18:37:17 -050093 u32 intr_type;
Suman Anna72c1c812014-06-24 19:43:43 -050094 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;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500109 bool send_no_irq;
Suman Anna75288cc2014-09-10 14:20:59 -0500110};
111
Suman Anna5040f532014-06-24 19:43:41 -0500112struct omap_mbox {
113 const char *name;
114 int irq;
Suman Anna8841a662014-11-03 17:05:50 -0600115 struct omap_mbox_queue *rxq;
Suman Anna5040f532014-06-24 19:43:41 -0500116 struct device *dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500117 struct omap_mbox_device *parent;
Suman Annabe3322e2014-06-24 19:43:42 -0500118 struct omap_mbox_fifo tx_fifo;
119 struct omap_mbox_fifo rx_fifo;
120 u32 ctx[OMAP4_MBOX_NR_REGS];
121 u32 intr_type;
Suman Anna8841a662014-11-03 17:05:50 -0600122 struct mbox_chan *chan;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500123 bool send_no_irq;
Suman Anna5040f532014-06-24 19:43:41 -0500124};
125
Suman Anna72c1c812014-06-24 19:43:43 -0500126/* global variables for the mailbox devices */
127static DEFINE_MUTEX(omap_mbox_devices_lock);
128static LIST_HEAD(omap_mbox_devices);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800129
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000130static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
131module_param(mbox_kfifo_size, uint, S_IRUGO);
132MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
133
Suman Anna8841a662014-11-03 17:05:50 -0600134static struct omap_mbox *mbox_chan_to_omap_mbox(struct mbox_chan *chan)
135{
136 if (!chan || !chan->con_priv)
137 return NULL;
138
139 return (struct omap_mbox *)chan->con_priv;
140}
141
Suman Anna72c1c812014-06-24 19:43:43 -0500142static inline
143unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500144{
Suman Anna72c1c812014-06-24 19:43:43 -0500145 return __raw_readl(mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500146}
147
Suman Anna72c1c812014-06-24 19:43:43 -0500148static inline
149void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500150{
Suman Anna72c1c812014-06-24 19:43:43 -0500151 __raw_writel(val, mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500152}
153
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700154/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500155static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700156{
Suman Annabe3322e2014-06-24 19:43:42 -0500157 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500158
159 return (mbox_msg_t)mbox_read_reg(mbox->parent, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700160}
Suman Anna5040f532014-06-24 19:43:41 -0500161
162static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700163{
Suman Annabe3322e2014-06-24 19:43:42 -0500164 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500165
Suman Anna72c1c812014-06-24 19:43:43 -0500166 mbox_write_reg(mbox->parent, msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700167}
Suman Anna5040f532014-06-24 19:43:41 -0500168
169static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700170{
Suman Annabe3322e2014-06-24 19:43:42 -0500171 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500172
Suman Anna72c1c812014-06-24 19:43:43 -0500173 return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700174}
Suman Anna5040f532014-06-24 19:43:41 -0500175
176static int mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700177{
Suman Annabe3322e2014-06-24 19:43:42 -0500178 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500179
Suman Anna72c1c812014-06-24 19:43:43 -0500180 return mbox_read_reg(mbox->parent, fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700181}
182
183/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500184static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700185{
Suman Annabe3322e2014-06-24 19:43:42 -0500186 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
187 &mbox->tx_fifo : &mbox->rx_fifo;
188 u32 bit = fifo->intr_bit;
189 u32 irqstatus = fifo->irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -0500190
Suman Anna72c1c812014-06-24 19:43:43 -0500191 mbox_write_reg(mbox->parent, bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500192
193 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Anna72c1c812014-06-24 19:43:43 -0500194 mbox_read_reg(mbox->parent, irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700195}
Suman Anna5040f532014-06-24 19:43:41 -0500196
197static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700198{
Suman Annabe3322e2014-06-24 19:43:42 -0500199 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
200 &mbox->tx_fifo : &mbox->rx_fifo;
201 u32 bit = fifo->intr_bit;
202 u32 irqenable = fifo->irqenable;
203 u32 irqstatus = fifo->irqstatus;
204
Suman Anna72c1c812014-06-24 19:43:43 -0500205 u32 enable = mbox_read_reg(mbox->parent, irqenable);
206 u32 status = mbox_read_reg(mbox->parent, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500207
208 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700209}
210
Suman Anna8841a662014-11-03 17:05:50 -0600211void omap_mbox_save_ctx(struct mbox_chan *chan)
Suman Annac869c752013-03-12 17:55:29 -0500212{
Suman Anna5040f532014-06-24 19:43:41 -0500213 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500214 int nr_regs;
Suman Anna8841a662014-11-03 17:05:50 -0600215 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
216
217 if (WARN_ON(!mbox))
218 return;
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
Suman Anna8841a662014-11-03 17:05:50 -0600233void omap_mbox_restore_ctx(struct mbox_chan *chan)
Suman Annac869c752013-03-12 17:55:29 -0500234{
Suman Anna5040f532014-06-24 19:43:41 -0500235 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500236 int nr_regs;
Suman Anna8841a662014-11-03 17:05:50 -0600237 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
238
239 if (WARN_ON(!mbox))
240 return;
Suman Annac869c752013-03-12 17:55:29 -0500241
Suman Annabe3322e2014-06-24 19:43:42 -0500242 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500243 nr_regs = OMAP4_MBOX_NR_REGS;
244 else
245 nr_regs = MBOX_NR_REGS;
246 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500247 mbox_write_reg(mbox->parent, mbox->ctx[i], i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500248 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500249 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500250 }
Suman Annac869c752013-03-12 17:55:29 -0500251}
252EXPORT_SYMBOL(omap_mbox_restore_ctx);
253
Suman Anna8841a662014-11-03 17:05:50 -0600254static void _omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500255{
Suman Annabe3322e2014-06-24 19:43:42 -0500256 u32 l;
257 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
258 &mbox->tx_fifo : &mbox->rx_fifo;
259 u32 bit = fifo->intr_bit;
260 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500261
Suman Anna72c1c812014-06-24 19:43:43 -0500262 l = mbox_read_reg(mbox->parent, irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500263 l |= bit;
Suman Anna72c1c812014-06-24 19:43:43 -0500264 mbox_write_reg(mbox->parent, l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500265}
Suman Annac869c752013-03-12 17:55:29 -0500266
Suman Anna8841a662014-11-03 17:05:50 -0600267static void _omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500268{
Suman Annabe3322e2014-06-24 19:43:42 -0500269 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
270 &mbox->tx_fifo : &mbox->rx_fifo;
271 u32 bit = fifo->intr_bit;
272 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500273
274 /*
275 * Read and update the interrupt configuration register for pre-OMAP4.
276 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
277 */
Suman Annabe3322e2014-06-24 19:43:42 -0500278 if (!mbox->intr_type)
Suman Anna72c1c812014-06-24 19:43:43 -0500279 bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500280
Suman Anna72c1c812014-06-24 19:43:43 -0500281 mbox_write_reg(mbox->parent, bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500282}
Suman Annac869c752013-03-12 17:55:29 -0500283
Suman Anna8841a662014-11-03 17:05:50 -0600284void omap_mbox_enable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800285{
Suman Anna8841a662014-11-03 17:05:50 -0600286 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800287
Suman Anna8841a662014-11-03 17:05:50 -0600288 if (WARN_ON(!mbox))
289 return;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000290
Suman Anna8841a662014-11-03 17:05:50 -0600291 _omap_mbox_enable_irq(mbox, irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800292}
Suman Anna8841a662014-11-03 17:05:50 -0600293EXPORT_SYMBOL(omap_mbox_enable_irq);
294
295void omap_mbox_disable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
296{
297 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
298
299 if (WARN_ON(!mbox))
300 return;
301
302 _omap_mbox_disable_irq(mbox, irq);
303}
304EXPORT_SYMBOL(omap_mbox_disable_irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800305
306/*
307 * Message receiver(workqueue)
308 */
309static void mbox_rx_work(struct work_struct *work)
310{
311 struct omap_mbox_queue *mq =
312 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800313 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000314 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800315
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000316 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
317 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
318 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800319
Suman Anna8841a662014-11-03 17:05:50 -0600320 mbox_chan_received_data(mq->mbox->chan, (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000321 spin_lock_irq(&mq->lock);
322 if (mq->full) {
323 mq->full = false;
Suman Anna8841a662014-11-03 17:05:50 -0600324 _omap_mbox_enable_irq(mq->mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000325 }
326 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800327 }
328}
329
330/*
331 * Mailbox interrupt handler
332 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800333static void __mbox_tx_interrupt(struct omap_mbox *mbox)
334{
Suman Anna8841a662014-11-03 17:05:50 -0600335 _omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800336 ack_mbox_irq(mbox, IRQ_TX);
Suman Anna8841a662014-11-03 17:05:50 -0600337 mbox_chan_txdone(mbox->chan, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800338}
339
340static void __mbox_rx_interrupt(struct omap_mbox *mbox)
341{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000342 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800343 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000344 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800345
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800346 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000347 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Suman Anna8841a662014-11-03 17:05:50 -0600348 _omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000349 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800350 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600351 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800352
353 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800354
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000355 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
356 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800357 }
358
359 /* no more messages in the fifo. clear IRQ source. */
360 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700361nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100362 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800363}
364
365static irqreturn_t mbox_interrupt(int irq, void *p)
366{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400367 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800368
369 if (is_mbox_irq(mbox, IRQ_TX))
370 __mbox_tx_interrupt(mbox);
371
372 if (is_mbox_irq(mbox, IRQ_RX))
373 __mbox_rx_interrupt(mbox);
374
375 return IRQ_HANDLED;
376}
377
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800378static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Suman Anna8841a662014-11-03 17:05:50 -0600379 void (*work)(struct work_struct *))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800380{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800381 struct omap_mbox_queue *mq;
382
Suman Anna8841a662014-11-03 17:05:50 -0600383 if (!work)
384 return NULL;
385
Suman Anna86f6f5e2016-04-06 12:37:38 -0500386 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800387 if (!mq)
388 return NULL;
389
390 spin_lock_init(&mq->lock);
391
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000392 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800393 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800394
Suman Anna8841a662014-11-03 17:05:50 -0600395 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800396 return mq;
Suman Anna8841a662014-11-03 17:05:50 -0600397
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800398error:
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;
413
Suman Anna8841a662014-11-03 17:05:50 -0600414 mq = mbox_queue_alloc(mbox, mbox_rx_work);
415 if (!mq)
416 return -ENOMEM;
417 mbox->rxq = mq;
418 mq->mbox = mbox;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800419
Suman Anna8841a662014-11-03 17:05:50 -0600420 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
421 mbox->name, mbox);
422 if (unlikely(ret)) {
423 pr_err("failed to register mailbox interrupt:%d\n", ret);
424 goto fail_request_irq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800425 }
Suman Anna8841a662014-11-03 17:05:50 -0600426
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500427 if (mbox->send_no_irq)
428 mbox->chan->txdone_method = TXDONE_BY_ACK;
429
Suman Anna8841a662014-11-03 17:05:50 -0600430 _omap_mbox_enable_irq(mbox, IRQ_RX);
431
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800432 return 0;
433
Suman Annaecf305c2013-02-01 20:37:06 -0600434fail_request_irq:
435 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800436 return ret;
437}
438
439static void omap_mbox_fini(struct omap_mbox *mbox)
440{
Suman Anna8841a662014-11-03 17:05:50 -0600441 _omap_mbox_disable_irq(mbox, IRQ_RX);
442 free_irq(mbox->irq, mbox);
443 flush_work(&mbox->rxq->work);
444 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800445}
446
Suman Anna72c1c812014-06-24 19:43:43 -0500447static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
448 const char *mbox_name)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800449{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000450 struct omap_mbox *_mbox, *mbox = NULL;
Suman Anna72c1c812014-06-24 19:43:43 -0500451 struct omap_mbox **mboxes = mdev->mboxes;
452 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800453
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000454 if (!mboxes)
Suman Anna72c1c812014-06-24 19:43:43 -0500455 return NULL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800456
Kevin Hilmanc0377322011-02-11 19:56:43 +0000457 for (i = 0; (_mbox = mboxes[i]); i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500458 if (!strcmp(_mbox->name, mbox_name)) {
Kevin Hilmanc0377322011-02-11 19:56:43 +0000459 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000460 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000461 }
462 }
Suman Anna72c1c812014-06-24 19:43:43 -0500463 return mbox;
464}
465
Suman Anna8841a662014-11-03 17:05:50 -0600466struct mbox_chan *omap_mbox_request_channel(struct mbox_client *cl,
467 const char *chan_name)
Suman Anna72c1c812014-06-24 19:43:43 -0500468{
Suman Anna8841a662014-11-03 17:05:50 -0600469 struct device *dev = cl->dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500470 struct omap_mbox *mbox = NULL;
471 struct omap_mbox_device *mdev;
Suman Anna8841a662014-11-03 17:05:50 -0600472 struct mbox_chan *chan;
473 unsigned long flags;
Suman Anna72c1c812014-06-24 19:43:43 -0500474 int ret;
475
Suman Anna8841a662014-11-03 17:05:50 -0600476 if (!dev)
477 return ERR_PTR(-ENODEV);
478
479 if (dev->of_node) {
480 pr_err("%s: please use mbox_request_channel(), this API is supported only for OMAP non-DT usage\n",
481 __func__);
482 return ERR_PTR(-ENODEV);
483 }
484
Suman Anna72c1c812014-06-24 19:43:43 -0500485 mutex_lock(&omap_mbox_devices_lock);
486 list_for_each_entry(mdev, &omap_mbox_devices, elem) {
Suman Anna8841a662014-11-03 17:05:50 -0600487 mbox = omap_mbox_device_find(mdev, chan_name);
Suman Anna72c1c812014-06-24 19:43:43 -0500488 if (mbox)
489 break;
490 }
491 mutex_unlock(&omap_mbox_devices_lock);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000492
Suman Anna8841a662014-11-03 17:05:50 -0600493 if (!mbox || !mbox->chan)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000494 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800495
Suman Anna8841a662014-11-03 17:05:50 -0600496 chan = mbox->chan;
497 spin_lock_irqsave(&chan->lock, flags);
498 chan->msg_free = 0;
499 chan->msg_count = 0;
500 chan->active_req = NULL;
501 chan->cl = cl;
502 init_completion(&chan->tx_complete);
503 spin_unlock_irqrestore(&chan->lock, flags);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000504
Suman Anna8841a662014-11-03 17:05:50 -0600505 ret = chan->mbox->ops->startup(chan);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300506 if (ret) {
Suman Anna8841a662014-11-03 17:05:50 -0600507 pr_err("Unable to startup the chan (%d)\n", ret);
508 mbox_free_channel(chan);
509 chan = ERR_PTR(ret);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300510 }
511
Suman Anna8841a662014-11-03 17:05:50 -0600512 return chan;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800513}
Suman Anna8841a662014-11-03 17:05:50 -0600514EXPORT_SYMBOL(omap_mbox_request_channel);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800515
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300516static struct class omap_mbox_class = { .name = "mbox", };
517
Suman Anna72c1c812014-06-24 19:43:43 -0500518static int omap_mbox_register(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800519{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000520 int ret;
521 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500522 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800523
Suman Anna72c1c812014-06-24 19:43:43 -0500524 if (!mdev || !mdev->mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800525 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800526
Suman Anna72c1c812014-06-24 19:43:43 -0500527 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000528 for (i = 0; mboxes[i]; i++) {
529 struct omap_mbox *mbox = mboxes[i];
Suman Anna2665a4c2016-04-06 12:37:40 -0500530
Suman Anna8841a662014-11-03 17:05:50 -0600531 mbox->dev = device_create(&omap_mbox_class, mdev->dev,
532 0, mbox, "%s", mbox->name);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000533 if (IS_ERR(mbox->dev)) {
534 ret = PTR_ERR(mbox->dev);
535 goto err_out;
536 }
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700537 }
Suman Anna72c1c812014-06-24 19:43:43 -0500538
539 mutex_lock(&omap_mbox_devices_lock);
540 list_add(&mdev->elem, &omap_mbox_devices);
541 mutex_unlock(&omap_mbox_devices_lock);
542
Suman Anna8841a662014-11-03 17:05:50 -0600543 ret = mbox_controller_register(&mdev->controller);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700544
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000545err_out:
Suman Anna8841a662014-11-03 17:05:50 -0600546 if (ret) {
547 while (i--)
548 device_unregister(mboxes[i]->dev);
549 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800550 return ret;
551}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800552
Suman Anna72c1c812014-06-24 19:43:43 -0500553static int omap_mbox_unregister(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800554{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000555 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500556 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800557
Suman Anna72c1c812014-06-24 19:43:43 -0500558 if (!mdev || !mdev->mboxes)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000559 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800560
Suman Anna72c1c812014-06-24 19:43:43 -0500561 mutex_lock(&omap_mbox_devices_lock);
562 list_del(&mdev->elem);
563 mutex_unlock(&omap_mbox_devices_lock);
564
Suman Anna8841a662014-11-03 17:05:50 -0600565 mbox_controller_unregister(&mdev->controller);
566
Suman Anna72c1c812014-06-24 19:43:43 -0500567 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000568 for (i = 0; mboxes[i]; i++)
569 device_unregister(mboxes[i]->dev);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000570 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800571}
Suman Anna5040f532014-06-24 19:43:41 -0500572
Suman Anna8841a662014-11-03 17:05:50 -0600573static int omap_mbox_chan_startup(struct mbox_chan *chan)
574{
575 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
576 struct omap_mbox_device *mdev = mbox->parent;
577 int ret = 0;
578
579 mutex_lock(&mdev->cfg_lock);
580 pm_runtime_get_sync(mdev->dev);
581 ret = omap_mbox_startup(mbox);
582 if (ret)
583 pm_runtime_put_sync(mdev->dev);
584 mutex_unlock(&mdev->cfg_lock);
585 return ret;
586}
587
588static void omap_mbox_chan_shutdown(struct mbox_chan *chan)
589{
590 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
591 struct omap_mbox_device *mdev = mbox->parent;
592
593 mutex_lock(&mdev->cfg_lock);
594 omap_mbox_fini(mbox);
595 pm_runtime_put_sync(mdev->dev);
596 mutex_unlock(&mdev->cfg_lock);
597}
598
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500599static int omap_mbox_chan_send_noirq(struct omap_mbox *mbox, void *data)
Suman Anna8841a662014-11-03 17:05:50 -0600600{
Suman Anna8841a662014-11-03 17:05:50 -0600601 int ret = -EBUSY;
602
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500603 if (!mbox_fifo_full(mbox)) {
604 _omap_mbox_enable_irq(mbox, IRQ_RX);
605 mbox_fifo_write(mbox, (mbox_msg_t)data);
606 ret = 0;
607 _omap_mbox_disable_irq(mbox, IRQ_RX);
608
609 /* we must read and ack the interrupt directly from here */
610 mbox_fifo_read(mbox);
611 ack_mbox_irq(mbox, IRQ_RX);
612 }
613
614 return ret;
615}
616
617static int omap_mbox_chan_send(struct omap_mbox *mbox, void *data)
618{
619 int ret = -EBUSY;
Suman Anna8841a662014-11-03 17:05:50 -0600620
621 if (!mbox_fifo_full(mbox)) {
622 mbox_fifo_write(mbox, (mbox_msg_t)data);
623 ret = 0;
624 }
625
626 /* always enable the interrupt */
627 _omap_mbox_enable_irq(mbox, IRQ_TX);
628 return ret;
629}
630
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500631static int omap_mbox_chan_send_data(struct mbox_chan *chan, void *data)
632{
633 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
634 int ret;
635
636 if (!mbox)
637 return -EINVAL;
638
639 if (mbox->send_no_irq)
640 ret = omap_mbox_chan_send_noirq(mbox, data);
641 else
642 ret = omap_mbox_chan_send(mbox, data);
643
644 return ret;
645}
646
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700647static const struct mbox_chan_ops omap_mbox_chan_ops = {
Suman Anna8841a662014-11-03 17:05:50 -0600648 .startup = omap_mbox_chan_startup,
649 .send_data = omap_mbox_chan_send_data,
650 .shutdown = omap_mbox_chan_shutdown,
651};
652
Suman Anna75288cc2014-09-10 14:20:59 -0500653static const struct of_device_id omap_mailbox_of_match[] = {
654 {
655 .compatible = "ti,omap2-mailbox",
656 .data = (void *)MBOX_INTR_CFG_TYPE1,
657 },
658 {
659 .compatible = "ti,omap3-mailbox",
660 .data = (void *)MBOX_INTR_CFG_TYPE1,
661 },
662 {
663 .compatible = "ti,omap4-mailbox",
664 .data = (void *)MBOX_INTR_CFG_TYPE2,
665 },
666 {
667 /* end */
668 },
669};
670MODULE_DEVICE_TABLE(of, omap_mailbox_of_match);
671
Suman Anna8841a662014-11-03 17:05:50 -0600672static struct mbox_chan *omap_mbox_of_xlate(struct mbox_controller *controller,
673 const struct of_phandle_args *sp)
674{
675 phandle phandle = sp->args[0];
676 struct device_node *node;
677 struct omap_mbox_device *mdev;
678 struct omap_mbox *mbox;
679
680 mdev = container_of(controller, struct omap_mbox_device, controller);
681 if (WARN_ON(!mdev))
Benson Leung2d805fc2015-05-04 10:36:36 -0700682 return ERR_PTR(-EINVAL);
Suman Anna8841a662014-11-03 17:05:50 -0600683
684 node = of_find_node_by_phandle(phandle);
685 if (!node) {
686 pr_err("%s: could not find node phandle 0x%x\n",
687 __func__, phandle);
Benson Leung2d805fc2015-05-04 10:36:36 -0700688 return ERR_PTR(-ENODEV);
Suman Anna8841a662014-11-03 17:05:50 -0600689 }
690
691 mbox = omap_mbox_device_find(mdev, node->name);
692 of_node_put(node);
Benson Leung2d805fc2015-05-04 10:36:36 -0700693 return mbox ? mbox->chan : ERR_PTR(-ENOENT);
Suman Anna8841a662014-11-03 17:05:50 -0600694}
695
Suman Anna5040f532014-06-24 19:43:41 -0500696static int omap_mbox_probe(struct platform_device *pdev)
697{
698 struct resource *mem;
699 int ret;
Suman Anna8841a662014-11-03 17:05:50 -0600700 struct mbox_chan *chnls;
Suman Anna5040f532014-06-24 19:43:41 -0500701 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500702 struct omap_mbox_fifo_info *finfo, *finfoblk;
Suman Anna72c1c812014-06-24 19:43:43 -0500703 struct omap_mbox_device *mdev;
Suman Annabe3322e2014-06-24 19:43:42 -0500704 struct omap_mbox_fifo *fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500705 struct device_node *node = pdev->dev.of_node;
706 struct device_node *child;
707 const struct of_device_id *match;
708 u32 intr_type, info_count;
709 u32 num_users, num_fifos;
710 u32 tmp[3];
Suman Anna5040f532014-06-24 19:43:41 -0500711 u32 l;
712 int i;
713
Suman Anna4899f78a2016-04-06 12:37:37 -0500714 if (!node) {
715 pr_err("%s: only DT-based devices are supported\n", __func__);
Suman Anna5040f532014-06-24 19:43:41 -0500716 return -ENODEV;
717 }
718
Suman Anna4899f78a2016-04-06 12:37:37 -0500719 match = of_match_device(omap_mailbox_of_match, &pdev->dev);
720 if (!match)
721 return -ENODEV;
722 intr_type = (u32)match->data;
Suman Anna75288cc2014-09-10 14:20:59 -0500723
Suman Anna4899f78a2016-04-06 12:37:37 -0500724 if (of_property_read_u32(node, "ti,mbox-num-users", &num_users))
725 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500726
Suman Anna4899f78a2016-04-06 12:37:37 -0500727 if (of_property_read_u32(node, "ti,mbox-num-fifos", &num_fifos))
728 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500729
Suman Anna4899f78a2016-04-06 12:37:37 -0500730 info_count = of_get_available_child_count(node);
731 if (!info_count) {
732 dev_err(&pdev->dev, "no available mbox devices found\n");
733 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500734 }
735
736 finfoblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*finfoblk),
737 GFP_KERNEL);
738 if (!finfoblk)
739 return -ENOMEM;
740
741 finfo = finfoblk;
742 child = NULL;
743 for (i = 0; i < info_count; i++, finfo++) {
Suman Anna4899f78a2016-04-06 12:37:37 -0500744 child = of_get_next_available_child(node, child);
745 ret = of_property_read_u32_array(child, "ti,mbox-tx", tmp,
746 ARRAY_SIZE(tmp));
747 if (ret)
748 return ret;
749 finfo->tx_id = tmp[0];
750 finfo->tx_irq = tmp[1];
751 finfo->tx_usr = tmp[2];
Suman Anna75288cc2014-09-10 14:20:59 -0500752
Suman Anna4899f78a2016-04-06 12:37:37 -0500753 ret = of_property_read_u32_array(child, "ti,mbox-rx", tmp,
754 ARRAY_SIZE(tmp));
755 if (ret)
756 return ret;
757 finfo->rx_id = tmp[0];
758 finfo->rx_irq = tmp[1];
759 finfo->rx_usr = tmp[2];
Suman Anna75288cc2014-09-10 14:20:59 -0500760
Suman Anna4899f78a2016-04-06 12:37:37 -0500761 finfo->name = child->name;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500762
Suman Anna4899f78a2016-04-06 12:37:37 -0500763 if (of_find_property(child, "ti,mbox-send-noirq", NULL))
764 finfo->send_no_irq = true;
765
Suman Anna75288cc2014-09-10 14:20:59 -0500766 if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos ||
767 finfo->tx_usr >= num_users || finfo->rx_usr >= num_users)
768 return -EINVAL;
769 }
770
Suman Anna72c1c812014-06-24 19:43:43 -0500771 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
772 if (!mdev)
773 return -ENOMEM;
774
775 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
776 mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
777 if (IS_ERR(mdev->mbox_base))
778 return PTR_ERR(mdev->mbox_base);
779
Suman Anna5040f532014-06-24 19:43:41 -0500780 /* allocate one extra for marking end of list */
Suman Anna75288cc2014-09-10 14:20:59 -0500781 list = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*list),
Suman Anna5040f532014-06-24 19:43:41 -0500782 GFP_KERNEL);
783 if (!list)
784 return -ENOMEM;
785
Suman Anna8841a662014-11-03 17:05:50 -0600786 chnls = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*chnls),
787 GFP_KERNEL);
788 if (!chnls)
789 return -ENOMEM;
790
Suman Anna75288cc2014-09-10 14:20:59 -0500791 mboxblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*mbox),
Suman Anna5040f532014-06-24 19:43:41 -0500792 GFP_KERNEL);
793 if (!mboxblk)
794 return -ENOMEM;
795
Suman Anna5040f532014-06-24 19:43:41 -0500796 mbox = mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500797 finfo = finfoblk;
798 for (i = 0; i < info_count; i++, finfo++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500799 fifo = &mbox->tx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500800 fifo->msg = MAILBOX_MESSAGE(finfo->tx_id);
801 fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id);
802 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id);
803 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr);
804 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr);
805 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr);
Suman Anna5040f532014-06-24 19:43:41 -0500806
Suman Annabe3322e2014-06-24 19:43:42 -0500807 fifo = &mbox->rx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500808 fifo->msg = MAILBOX_MESSAGE(finfo->rx_id);
809 fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id);
810 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id);
811 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr);
812 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr);
813 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr);
Suman Annabe3322e2014-06-24 19:43:42 -0500814
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500815 mbox->send_no_irq = finfo->send_no_irq;
Suman Annabe3322e2014-06-24 19:43:42 -0500816 mbox->intr_type = intr_type;
817
Suman Anna72c1c812014-06-24 19:43:43 -0500818 mbox->parent = mdev;
Suman Anna75288cc2014-09-10 14:20:59 -0500819 mbox->name = finfo->name;
820 mbox->irq = platform_get_irq(pdev, finfo->tx_irq);
Suman Anna5040f532014-06-24 19:43:41 -0500821 if (mbox->irq < 0)
822 return mbox->irq;
Suman Anna8841a662014-11-03 17:05:50 -0600823 mbox->chan = &chnls[i];
824 chnls[i].con_priv = mbox;
Suman Anna5040f532014-06-24 19:43:41 -0500825 list[i] = mbox++;
826 }
827
Suman Anna72c1c812014-06-24 19:43:43 -0500828 mutex_init(&mdev->cfg_lock);
829 mdev->dev = &pdev->dev;
Suman Anna75288cc2014-09-10 14:20:59 -0500830 mdev->num_users = num_users;
831 mdev->num_fifos = num_fifos;
Suman Anna2240f8a2016-04-06 18:37:17 -0500832 mdev->intr_type = intr_type;
Suman Anna72c1c812014-06-24 19:43:43 -0500833 mdev->mboxes = list;
Suman Anna8841a662014-11-03 17:05:50 -0600834
835 /* OMAP does not have a Tx-Done IRQ, but rather a Tx-Ready IRQ */
836 mdev->controller.txdone_irq = true;
837 mdev->controller.dev = mdev->dev;
838 mdev->controller.ops = &omap_mbox_chan_ops;
839 mdev->controller.chans = chnls;
840 mdev->controller.num_chans = info_count;
841 mdev->controller.of_xlate = omap_mbox_of_xlate;
Suman Anna72c1c812014-06-24 19:43:43 -0500842 ret = omap_mbox_register(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500843 if (ret)
844 return ret;
845
Suman Anna72c1c812014-06-24 19:43:43 -0500846 platform_set_drvdata(pdev, mdev);
847 pm_runtime_enable(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500848
Suman Anna72c1c812014-06-24 19:43:43 -0500849 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500850 if (ret < 0) {
Suman Anna72c1c812014-06-24 19:43:43 -0500851 pm_runtime_put_noidle(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500852 goto unregister;
853 }
854
855 /*
856 * just print the raw revision register, the format is not
857 * uniform across all SoCs
858 */
Suman Anna72c1c812014-06-24 19:43:43 -0500859 l = mbox_read_reg(mdev, MAILBOX_REVISION);
860 dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
Suman Anna5040f532014-06-24 19:43:41 -0500861
Suman Anna72c1c812014-06-24 19:43:43 -0500862 ret = pm_runtime_put_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500863 if (ret < 0)
864 goto unregister;
865
Suman Anna75288cc2014-09-10 14:20:59 -0500866 devm_kfree(&pdev->dev, finfoblk);
Suman Anna5040f532014-06-24 19:43:41 -0500867 return 0;
868
869unregister:
Suman Anna72c1c812014-06-24 19:43:43 -0500870 pm_runtime_disable(mdev->dev);
871 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500872 return ret;
873}
874
875static int omap_mbox_remove(struct platform_device *pdev)
876{
Suman Anna72c1c812014-06-24 19:43:43 -0500877 struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
878
879 pm_runtime_disable(mdev->dev);
880 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500881
882 return 0;
883}
884
885static struct platform_driver omap_mbox_driver = {
886 .probe = omap_mbox_probe,
887 .remove = omap_mbox_remove,
888 .driver = {
889 .name = "omap-mailbox",
Suman Anna75288cc2014-09-10 14:20:59 -0500890 .of_match_table = of_match_ptr(omap_mailbox_of_match),
Suman Anna5040f532014-06-24 19:43:41 -0500891 },
892};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800893
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800894static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800895{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300896 int err;
897
898 err = class_register(&omap_mbox_class);
899 if (err)
900 return err;
901
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000902 /* kfifo size sanity check: alignment and minimal size */
903 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000904 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
905 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000906
Suman Anna5040f532014-06-24 19:43:41 -0500907 return platform_driver_register(&omap_mbox_driver);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800908}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300909subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800910
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800911static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800912{
Suman Anna5040f532014-06-24 19:43:41 -0500913 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300914 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800915}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800916module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800917
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700918MODULE_LICENSE("GPL v2");
919MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000920MODULE_AUTHOR("Toshihiro Kobayashi");
921MODULE_AUTHOR("Hiroshi DOYU");