blob: ca50177a33f29ecd7c4ce4151a5c3b606417256f [file] [log] [blame]
Suman Anna2ad51572018-07-11 18:42:11 -05001// SPDX-License-Identifier: GPL-2.0
Hiroshi DOYU340a6142006-12-07 15:43:59 -08002/*
3 * OMAP mailbox driver
4 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07005 * Copyright (C) 2006-2009 Nokia Corporation. All rights reserved.
Suman Anna4899f78a2016-04-06 12:37:37 -05006 * Copyright (C) 2013-2016 Texas Instruments Incorporated - http://www.ti.com
Hiroshi DOYU340a6142006-12-07 15:43:59 -08007 *
Hiroshi DOYUf48cca82009-03-23 18:07:24 -07008 * Contact: Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
Suman Anna5040f532014-06-24 19:43:41 -05009 * Suman Anna <s-anna@ti.com>
Hiroshi DOYU340a6142006-12-07 15:43:59 -080010 */
11
Hiroshi DOYU340a6142006-12-07 15:43:59 -080012#include <linux/interrupt.h>
Felipe Contrerasb3e69142010-06-11 15:51:49 +000013#include <linux/spinlock.h>
14#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +000016#include <linux/kfifo.h>
17#include <linux/err.h>
Paul Gortmaker73017a52011-07-31 16:14:14 -040018#include <linux/module.h>
Suman Anna75288cc2014-09-10 14:20:59 -050019#include <linux/of_device.h>
Suman Anna5040f532014-06-24 19:43:41 -050020#include <linux/platform_device.h>
21#include <linux/pm_runtime.h>
Suman Anna5040f532014-06-24 19:43:41 -050022#include <linux/omap-mailbox.h>
Suman Anna8841a662014-11-03 17:05:50 -060023#include <linux/mailbox_controller.h>
24#include <linux/mailbox_client.h>
Hiroshi DOYU8dff0fa2009-03-23 18:07:32 -070025
Dave Gerlach8e3c5952015-09-22 19:14:52 -050026#include "mailbox.h"
27
Suman Anna5040f532014-06-24 19:43:41 -050028#define MAILBOX_REVISION 0x000
29#define MAILBOX_MESSAGE(m) (0x040 + 4 * (m))
30#define MAILBOX_FIFOSTATUS(m) (0x080 + 4 * (m))
31#define MAILBOX_MSGSTATUS(m) (0x0c0 + 4 * (m))
Hiroshi DOYU340a6142006-12-07 15:43:59 -080032
Suman Anna5040f532014-06-24 19:43:41 -050033#define OMAP2_MAILBOX_IRQSTATUS(u) (0x100 + 8 * (u))
34#define OMAP2_MAILBOX_IRQENABLE(u) (0x104 + 8 * (u))
35
36#define OMAP4_MAILBOX_IRQSTATUS(u) (0x104 + 0x10 * (u))
37#define OMAP4_MAILBOX_IRQENABLE(u) (0x108 + 0x10 * (u))
38#define OMAP4_MAILBOX_IRQENABLE_CLR(u) (0x10c + 0x10 * (u))
39
40#define MAILBOX_IRQSTATUS(type, u) (type ? OMAP4_MAILBOX_IRQSTATUS(u) : \
41 OMAP2_MAILBOX_IRQSTATUS(u))
42#define MAILBOX_IRQENABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE(u) : \
43 OMAP2_MAILBOX_IRQENABLE(u))
44#define MAILBOX_IRQDISABLE(type, u) (type ? OMAP4_MAILBOX_IRQENABLE_CLR(u) \
45 : OMAP2_MAILBOX_IRQENABLE(u))
46
47#define MAILBOX_IRQ_NEWMSG(m) (1 << (2 * (m)))
48#define MAILBOX_IRQ_NOTFULL(m) (1 << (2 * (m) + 1))
49
Suman Anna4899f78a2016-04-06 12:37:37 -050050/* Interrupt register configuration types */
51#define MBOX_INTR_CFG_TYPE1 0
52#define MBOX_INTR_CFG_TYPE2 1
53
Suman Anna5040f532014-06-24 19:43:41 -050054struct omap_mbox_fifo {
55 unsigned long msg;
56 unsigned long fifo_stat;
57 unsigned long msg_stat;
Suman Anna5040f532014-06-24 19:43:41 -050058 unsigned long irqenable;
59 unsigned long irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -050060 unsigned long irqdisable;
Suman Annabe3322e2014-06-24 19:43:42 -050061 u32 intr_bit;
Suman Anna5040f532014-06-24 19:43:41 -050062};
63
64struct omap_mbox_queue {
65 spinlock_t lock;
66 struct kfifo fifo;
67 struct work_struct work;
Suman Anna5040f532014-06-24 19:43:41 -050068 struct omap_mbox *mbox;
69 bool full;
70};
71
Suman Annaea2ec1e2018-07-11 18:42:12 -050072struct omap_mbox_match_data {
73 u32 intr_type;
74};
75
Suman Anna72c1c812014-06-24 19:43:43 -050076struct omap_mbox_device {
77 struct device *dev;
78 struct mutex cfg_lock;
79 void __iomem *mbox_base;
Suman Annaaf1d2f52016-04-06 18:37:18 -050080 u32 *irq_ctx;
Suman Anna72c1c812014-06-24 19:43:43 -050081 u32 num_users;
82 u32 num_fifos;
Suman Anna2240f8a2016-04-06 18:37:17 -050083 u32 intr_type;
Suman Anna72c1c812014-06-24 19:43:43 -050084 struct omap_mbox **mboxes;
Suman Anna8841a662014-11-03 17:05:50 -060085 struct mbox_controller controller;
Suman Anna72c1c812014-06-24 19:43:43 -050086 struct list_head elem;
87};
88
Suman Anna75288cc2014-09-10 14:20:59 -050089struct omap_mbox_fifo_info {
90 int tx_id;
91 int tx_usr;
92 int tx_irq;
93
94 int rx_id;
95 int rx_usr;
96 int rx_irq;
97
98 const char *name;
Dave Gerlach8e3c5952015-09-22 19:14:52 -050099 bool send_no_irq;
Suman Anna75288cc2014-09-10 14:20:59 -0500100};
101
Suman Anna5040f532014-06-24 19:43:41 -0500102struct omap_mbox {
103 const char *name;
104 int irq;
Suman Anna8841a662014-11-03 17:05:50 -0600105 struct omap_mbox_queue *rxq;
Suman Anna5040f532014-06-24 19:43:41 -0500106 struct device *dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500107 struct omap_mbox_device *parent;
Suman Annabe3322e2014-06-24 19:43:42 -0500108 struct omap_mbox_fifo tx_fifo;
109 struct omap_mbox_fifo rx_fifo;
Suman Annabe3322e2014-06-24 19:43:42 -0500110 u32 intr_type;
Suman Anna8841a662014-11-03 17:05:50 -0600111 struct mbox_chan *chan;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500112 bool send_no_irq;
Suman Anna5040f532014-06-24 19:43:41 -0500113};
114
Suman Anna72c1c812014-06-24 19:43:43 -0500115/* global variables for the mailbox devices */
116static DEFINE_MUTEX(omap_mbox_devices_lock);
117static LIST_HEAD(omap_mbox_devices);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800118
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000119static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
120module_param(mbox_kfifo_size, uint, S_IRUGO);
121MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
122
Suman Anna8841a662014-11-03 17:05:50 -0600123static struct omap_mbox *mbox_chan_to_omap_mbox(struct mbox_chan *chan)
124{
125 if (!chan || !chan->con_priv)
126 return NULL;
127
128 return (struct omap_mbox *)chan->con_priv;
129}
130
Suman Anna72c1c812014-06-24 19:43:43 -0500131static inline
132unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500133{
Suman Anna72c1c812014-06-24 19:43:43 -0500134 return __raw_readl(mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500135}
136
Suman Anna72c1c812014-06-24 19:43:43 -0500137static inline
138void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500139{
Suman Anna72c1c812014-06-24 19:43:43 -0500140 __raw_writel(val, mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500141}
142
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700143/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500144static mbox_msg_t mbox_fifo_read(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 Anna2665a4c2016-04-06 12:37:40 -0500147
148 return (mbox_msg_t)mbox_read_reg(mbox->parent, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700149}
Suman Anna5040f532014-06-24 19:43:41 -0500150
151static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700152{
Suman Annabe3322e2014-06-24 19:43:42 -0500153 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500154
Suman Anna72c1c812014-06-24 19:43:43 -0500155 mbox_write_reg(mbox->parent, msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700156}
Suman Anna5040f532014-06-24 19:43:41 -0500157
158static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700159{
Suman Annabe3322e2014-06-24 19:43:42 -0500160 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500161
Suman Anna72c1c812014-06-24 19:43:43 -0500162 return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700163}
Suman Anna5040f532014-06-24 19:43:41 -0500164
165static int mbox_fifo_full(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->tx_fifo;
Suman Anna2665a4c2016-04-06 12:37:40 -0500168
Suman Anna72c1c812014-06-24 19:43:43 -0500169 return mbox_read_reg(mbox->parent, fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700170}
171
172/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500173static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700174{
Suman Annabe3322e2014-06-24 19:43:42 -0500175 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
176 &mbox->tx_fifo : &mbox->rx_fifo;
177 u32 bit = fifo->intr_bit;
178 u32 irqstatus = fifo->irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -0500179
Suman Anna72c1c812014-06-24 19:43:43 -0500180 mbox_write_reg(mbox->parent, bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500181
182 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Anna72c1c812014-06-24 19:43:43 -0500183 mbox_read_reg(mbox->parent, irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700184}
Suman Anna5040f532014-06-24 19:43:41 -0500185
186static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700187{
Suman Annabe3322e2014-06-24 19:43:42 -0500188 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
189 &mbox->tx_fifo : &mbox->rx_fifo;
190 u32 bit = fifo->intr_bit;
191 u32 irqenable = fifo->irqenable;
192 u32 irqstatus = fifo->irqstatus;
193
Suman Anna72c1c812014-06-24 19:43:43 -0500194 u32 enable = mbox_read_reg(mbox->parent, irqenable);
195 u32 status = mbox_read_reg(mbox->parent, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500196
197 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700198}
199
Suman Anna8841a662014-11-03 17:05:50 -0600200static void _omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500201{
Suman Annabe3322e2014-06-24 19:43:42 -0500202 u32 l;
203 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
204 &mbox->tx_fifo : &mbox->rx_fifo;
205 u32 bit = fifo->intr_bit;
206 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500207
Suman Anna72c1c812014-06-24 19:43:43 -0500208 l = mbox_read_reg(mbox->parent, irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500209 l |= bit;
Suman Anna72c1c812014-06-24 19:43:43 -0500210 mbox_write_reg(mbox->parent, l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500211}
Suman Annac869c752013-03-12 17:55:29 -0500212
Suman Anna8841a662014-11-03 17:05:50 -0600213static void _omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500214{
Suman Annabe3322e2014-06-24 19:43:42 -0500215 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
216 &mbox->tx_fifo : &mbox->rx_fifo;
217 u32 bit = fifo->intr_bit;
218 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500219
220 /*
221 * Read and update the interrupt configuration register for pre-OMAP4.
222 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
223 */
Suman Annabe3322e2014-06-24 19:43:42 -0500224 if (!mbox->intr_type)
Suman Anna72c1c812014-06-24 19:43:43 -0500225 bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500226
Suman Anna72c1c812014-06-24 19:43:43 -0500227 mbox_write_reg(mbox->parent, bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500228}
Suman Annac869c752013-03-12 17:55:29 -0500229
Suman Anna8841a662014-11-03 17:05:50 -0600230void omap_mbox_enable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800231{
Suman Anna8841a662014-11-03 17:05:50 -0600232 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800233
Suman Anna8841a662014-11-03 17:05:50 -0600234 if (WARN_ON(!mbox))
235 return;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000236
Suman Anna8841a662014-11-03 17:05:50 -0600237 _omap_mbox_enable_irq(mbox, irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800238}
Suman Anna8841a662014-11-03 17:05:50 -0600239EXPORT_SYMBOL(omap_mbox_enable_irq);
240
241void omap_mbox_disable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
242{
243 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
244
245 if (WARN_ON(!mbox))
246 return;
247
248 _omap_mbox_disable_irq(mbox, irq);
249}
250EXPORT_SYMBOL(omap_mbox_disable_irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800251
252/*
253 * Message receiver(workqueue)
254 */
255static void mbox_rx_work(struct work_struct *work)
256{
257 struct omap_mbox_queue *mq =
258 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800259 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000260 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800261
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000262 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
263 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
264 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800265
Suman Anna8841a662014-11-03 17:05:50 -0600266 mbox_chan_received_data(mq->mbox->chan, (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000267 spin_lock_irq(&mq->lock);
268 if (mq->full) {
269 mq->full = false;
Suman Anna8841a662014-11-03 17:05:50 -0600270 _omap_mbox_enable_irq(mq->mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000271 }
272 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800273 }
274}
275
276/*
277 * Mailbox interrupt handler
278 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800279static void __mbox_tx_interrupt(struct omap_mbox *mbox)
280{
Suman Anna8841a662014-11-03 17:05:50 -0600281 _omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800282 ack_mbox_irq(mbox, IRQ_TX);
Suman Anna8841a662014-11-03 17:05:50 -0600283 mbox_chan_txdone(mbox->chan, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800284}
285
286static void __mbox_rx_interrupt(struct omap_mbox *mbox)
287{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000288 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800289 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000290 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800291
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800292 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000293 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Suman Anna8841a662014-11-03 17:05:50 -0600294 _omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000295 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800296 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600297 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800298
299 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800300
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000301 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
302 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800303 }
304
305 /* no more messages in the fifo. clear IRQ source. */
306 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700307nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100308 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800309}
310
311static irqreturn_t mbox_interrupt(int irq, void *p)
312{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400313 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800314
315 if (is_mbox_irq(mbox, IRQ_TX))
316 __mbox_tx_interrupt(mbox);
317
318 if (is_mbox_irq(mbox, IRQ_RX))
319 __mbox_rx_interrupt(mbox);
320
321 return IRQ_HANDLED;
322}
323
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800324static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Suman Anna8841a662014-11-03 17:05:50 -0600325 void (*work)(struct work_struct *))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800326{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800327 struct omap_mbox_queue *mq;
328
Suman Anna8841a662014-11-03 17:05:50 -0600329 if (!work)
330 return NULL;
331
Suman Anna86f6f5e2016-04-06 12:37:38 -0500332 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800333 if (!mq)
334 return NULL;
335
336 spin_lock_init(&mq->lock);
337
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000338 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800339 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800340
Suman Anna8841a662014-11-03 17:05:50 -0600341 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800342 return mq;
Suman Anna8841a662014-11-03 17:05:50 -0600343
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800344error:
345 kfree(mq);
346 return NULL;
347}
348
349static void mbox_queue_free(struct omap_mbox_queue *q)
350{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000351 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800352 kfree(q);
353}
354
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800355static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800356{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800357 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800358 struct omap_mbox_queue *mq;
359
Suman Anna8841a662014-11-03 17:05:50 -0600360 mq = mbox_queue_alloc(mbox, mbox_rx_work);
361 if (!mq)
362 return -ENOMEM;
363 mbox->rxq = mq;
364 mq->mbox = mbox;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800365
Suman Anna8841a662014-11-03 17:05:50 -0600366 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
367 mbox->name, mbox);
368 if (unlikely(ret)) {
369 pr_err("failed to register mailbox interrupt:%d\n", ret);
370 goto fail_request_irq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800371 }
Suman Anna8841a662014-11-03 17:05:50 -0600372
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500373 if (mbox->send_no_irq)
374 mbox->chan->txdone_method = TXDONE_BY_ACK;
375
Suman Anna8841a662014-11-03 17:05:50 -0600376 _omap_mbox_enable_irq(mbox, IRQ_RX);
377
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800378 return 0;
379
Suman Annaecf305c2013-02-01 20:37:06 -0600380fail_request_irq:
381 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800382 return ret;
383}
384
385static void omap_mbox_fini(struct omap_mbox *mbox)
386{
Suman Anna8841a662014-11-03 17:05:50 -0600387 _omap_mbox_disable_irq(mbox, IRQ_RX);
388 free_irq(mbox->irq, mbox);
389 flush_work(&mbox->rxq->work);
390 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800391}
392
Suman Anna72c1c812014-06-24 19:43:43 -0500393static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
394 const char *mbox_name)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800395{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000396 struct omap_mbox *_mbox, *mbox = NULL;
Suman Anna72c1c812014-06-24 19:43:43 -0500397 struct omap_mbox **mboxes = mdev->mboxes;
398 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800399
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000400 if (!mboxes)
Suman Anna72c1c812014-06-24 19:43:43 -0500401 return NULL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800402
Kevin Hilmanc0377322011-02-11 19:56:43 +0000403 for (i = 0; (_mbox = mboxes[i]); i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500404 if (!strcmp(_mbox->name, mbox_name)) {
Kevin Hilmanc0377322011-02-11 19:56:43 +0000405 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000406 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000407 }
408 }
Suman Anna72c1c812014-06-24 19:43:43 -0500409 return mbox;
410}
411
Suman Anna8841a662014-11-03 17:05:50 -0600412struct mbox_chan *omap_mbox_request_channel(struct mbox_client *cl,
413 const char *chan_name)
Suman Anna72c1c812014-06-24 19:43:43 -0500414{
Suman Anna8841a662014-11-03 17:05:50 -0600415 struct device *dev = cl->dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500416 struct omap_mbox *mbox = NULL;
417 struct omap_mbox_device *mdev;
Suman Anna8841a662014-11-03 17:05:50 -0600418 struct mbox_chan *chan;
419 unsigned long flags;
Suman Anna72c1c812014-06-24 19:43:43 -0500420 int ret;
421
Suman Anna8841a662014-11-03 17:05:50 -0600422 if (!dev)
423 return ERR_PTR(-ENODEV);
424
425 if (dev->of_node) {
426 pr_err("%s: please use mbox_request_channel(), this API is supported only for OMAP non-DT usage\n",
427 __func__);
428 return ERR_PTR(-ENODEV);
429 }
430
Suman Anna72c1c812014-06-24 19:43:43 -0500431 mutex_lock(&omap_mbox_devices_lock);
432 list_for_each_entry(mdev, &omap_mbox_devices, elem) {
Suman Anna8841a662014-11-03 17:05:50 -0600433 mbox = omap_mbox_device_find(mdev, chan_name);
Suman Anna72c1c812014-06-24 19:43:43 -0500434 if (mbox)
435 break;
436 }
437 mutex_unlock(&omap_mbox_devices_lock);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000438
Suman Anna8841a662014-11-03 17:05:50 -0600439 if (!mbox || !mbox->chan)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000440 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800441
Suman Anna8841a662014-11-03 17:05:50 -0600442 chan = mbox->chan;
443 spin_lock_irqsave(&chan->lock, flags);
444 chan->msg_free = 0;
445 chan->msg_count = 0;
446 chan->active_req = NULL;
447 chan->cl = cl;
448 init_completion(&chan->tx_complete);
449 spin_unlock_irqrestore(&chan->lock, flags);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000450
Suman Anna8841a662014-11-03 17:05:50 -0600451 ret = chan->mbox->ops->startup(chan);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300452 if (ret) {
Suman Anna8841a662014-11-03 17:05:50 -0600453 pr_err("Unable to startup the chan (%d)\n", ret);
454 mbox_free_channel(chan);
455 chan = ERR_PTR(ret);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300456 }
457
Suman Anna8841a662014-11-03 17:05:50 -0600458 return chan;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800459}
Suman Anna8841a662014-11-03 17:05:50 -0600460EXPORT_SYMBOL(omap_mbox_request_channel);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800461
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300462static struct class omap_mbox_class = { .name = "mbox", };
463
Suman Anna72c1c812014-06-24 19:43:43 -0500464static int omap_mbox_register(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800465{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000466 int ret;
467 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500468 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800469
Suman Anna72c1c812014-06-24 19:43:43 -0500470 if (!mdev || !mdev->mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800471 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800472
Suman Anna72c1c812014-06-24 19:43:43 -0500473 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000474 for (i = 0; mboxes[i]; i++) {
475 struct omap_mbox *mbox = mboxes[i];
Suman Anna2665a4c2016-04-06 12:37:40 -0500476
Suman Anna8841a662014-11-03 17:05:50 -0600477 mbox->dev = device_create(&omap_mbox_class, mdev->dev,
478 0, mbox, "%s", mbox->name);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000479 if (IS_ERR(mbox->dev)) {
480 ret = PTR_ERR(mbox->dev);
481 goto err_out;
482 }
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700483 }
Suman Anna72c1c812014-06-24 19:43:43 -0500484
485 mutex_lock(&omap_mbox_devices_lock);
486 list_add(&mdev->elem, &omap_mbox_devices);
487 mutex_unlock(&omap_mbox_devices_lock);
488
Thierry Redinga3abf432018-12-20 18:19:57 +0100489 ret = devm_mbox_controller_register(mdev->dev, &mdev->controller);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700490
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000491err_out:
Suman Anna8841a662014-11-03 17:05:50 -0600492 if (ret) {
493 while (i--)
494 device_unregister(mboxes[i]->dev);
495 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800496 return ret;
497}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800498
Suman Anna72c1c812014-06-24 19:43:43 -0500499static int omap_mbox_unregister(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800500{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000501 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500502 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800503
Suman Anna72c1c812014-06-24 19:43:43 -0500504 if (!mdev || !mdev->mboxes)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000505 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800506
Suman Anna72c1c812014-06-24 19:43:43 -0500507 mutex_lock(&omap_mbox_devices_lock);
508 list_del(&mdev->elem);
509 mutex_unlock(&omap_mbox_devices_lock);
510
511 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000512 for (i = 0; mboxes[i]; i++)
513 device_unregister(mboxes[i]->dev);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000514 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800515}
Suman Anna5040f532014-06-24 19:43:41 -0500516
Suman Anna8841a662014-11-03 17:05:50 -0600517static int omap_mbox_chan_startup(struct mbox_chan *chan)
518{
519 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
520 struct omap_mbox_device *mdev = mbox->parent;
521 int ret = 0;
522
523 mutex_lock(&mdev->cfg_lock);
524 pm_runtime_get_sync(mdev->dev);
525 ret = omap_mbox_startup(mbox);
526 if (ret)
527 pm_runtime_put_sync(mdev->dev);
528 mutex_unlock(&mdev->cfg_lock);
529 return ret;
530}
531
532static void omap_mbox_chan_shutdown(struct mbox_chan *chan)
533{
534 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
535 struct omap_mbox_device *mdev = mbox->parent;
536
537 mutex_lock(&mdev->cfg_lock);
538 omap_mbox_fini(mbox);
539 pm_runtime_put_sync(mdev->dev);
540 mutex_unlock(&mdev->cfg_lock);
541}
542
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500543static int omap_mbox_chan_send_noirq(struct omap_mbox *mbox, void *data)
Suman Anna8841a662014-11-03 17:05:50 -0600544{
Suman Anna8841a662014-11-03 17:05:50 -0600545 int ret = -EBUSY;
546
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500547 if (!mbox_fifo_full(mbox)) {
548 _omap_mbox_enable_irq(mbox, IRQ_RX);
549 mbox_fifo_write(mbox, (mbox_msg_t)data);
550 ret = 0;
551 _omap_mbox_disable_irq(mbox, IRQ_RX);
552
553 /* we must read and ack the interrupt directly from here */
554 mbox_fifo_read(mbox);
555 ack_mbox_irq(mbox, IRQ_RX);
556 }
557
558 return ret;
559}
560
561static int omap_mbox_chan_send(struct omap_mbox *mbox, void *data)
562{
563 int ret = -EBUSY;
Suman Anna8841a662014-11-03 17:05:50 -0600564
565 if (!mbox_fifo_full(mbox)) {
566 mbox_fifo_write(mbox, (mbox_msg_t)data);
567 ret = 0;
568 }
569
570 /* always enable the interrupt */
571 _omap_mbox_enable_irq(mbox, IRQ_TX);
572 return ret;
573}
574
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500575static int omap_mbox_chan_send_data(struct mbox_chan *chan, void *data)
576{
577 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
578 int ret;
579
580 if (!mbox)
581 return -EINVAL;
582
583 if (mbox->send_no_irq)
584 ret = omap_mbox_chan_send_noirq(mbox, data);
585 else
586 ret = omap_mbox_chan_send(mbox, data);
587
588 return ret;
589}
590
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700591static const struct mbox_chan_ops omap_mbox_chan_ops = {
Suman Anna8841a662014-11-03 17:05:50 -0600592 .startup = omap_mbox_chan_startup,
593 .send_data = omap_mbox_chan_send_data,
594 .shutdown = omap_mbox_chan_shutdown,
595};
596
Suman Annaaf1d2f52016-04-06 18:37:18 -0500597#ifdef CONFIG_PM_SLEEP
598static int omap_mbox_suspend(struct device *dev)
599{
600 struct omap_mbox_device *mdev = dev_get_drvdata(dev);
Suman Anna9f0cee982016-04-06 18:37:19 -0500601 u32 usr, fifo, reg;
Suman Annaaf1d2f52016-04-06 18:37:18 -0500602
603 if (pm_runtime_status_suspended(dev))
604 return 0;
605
Suman Anna9f0cee982016-04-06 18:37:19 -0500606 for (fifo = 0; fifo < mdev->num_fifos; fifo++) {
607 if (mbox_read_reg(mdev, MAILBOX_MSGSTATUS(fifo))) {
608 dev_err(mdev->dev, "fifo %d has unexpected unread messages\n",
609 fifo);
610 return -EBUSY;
611 }
612 }
613
Suman Annaaf1d2f52016-04-06 18:37:18 -0500614 for (usr = 0; usr < mdev->num_users; usr++) {
615 reg = MAILBOX_IRQENABLE(mdev->intr_type, usr);
616 mdev->irq_ctx[usr] = mbox_read_reg(mdev, reg);
617 }
618
619 return 0;
620}
621
622static int omap_mbox_resume(struct device *dev)
623{
624 struct omap_mbox_device *mdev = dev_get_drvdata(dev);
625 u32 usr, reg;
626
627 if (pm_runtime_status_suspended(dev))
628 return 0;
629
630 for (usr = 0; usr < mdev->num_users; usr++) {
631 reg = MAILBOX_IRQENABLE(mdev->intr_type, usr);
632 mbox_write_reg(mdev, mdev->irq_ctx[usr], reg);
633 }
634
635 return 0;
636}
637#endif
638
639static const struct dev_pm_ops omap_mbox_pm_ops = {
640 SET_SYSTEM_SLEEP_PM_OPS(omap_mbox_suspend, omap_mbox_resume)
641};
642
Suman Annaea2ec1e2018-07-11 18:42:12 -0500643static const struct omap_mbox_match_data omap2_data = { MBOX_INTR_CFG_TYPE1 };
644static const struct omap_mbox_match_data omap4_data = { MBOX_INTR_CFG_TYPE2 };
645
Suman Anna75288cc2014-09-10 14:20:59 -0500646static const struct of_device_id omap_mailbox_of_match[] = {
647 {
648 .compatible = "ti,omap2-mailbox",
Suman Annaea2ec1e2018-07-11 18:42:12 -0500649 .data = &omap2_data,
Suman Anna75288cc2014-09-10 14:20:59 -0500650 },
651 {
652 .compatible = "ti,omap3-mailbox",
Suman Annaea2ec1e2018-07-11 18:42:12 -0500653 .data = &omap2_data,
Suman Anna75288cc2014-09-10 14:20:59 -0500654 },
655 {
656 .compatible = "ti,omap4-mailbox",
Suman Annaea2ec1e2018-07-11 18:42:12 -0500657 .data = &omap4_data,
Suman Anna75288cc2014-09-10 14:20:59 -0500658 },
659 {
660 /* end */
661 },
662};
663MODULE_DEVICE_TABLE(of, omap_mailbox_of_match);
664
Suman Anna8841a662014-11-03 17:05:50 -0600665static struct mbox_chan *omap_mbox_of_xlate(struct mbox_controller *controller,
666 const struct of_phandle_args *sp)
667{
668 phandle phandle = sp->args[0];
669 struct device_node *node;
670 struct omap_mbox_device *mdev;
671 struct omap_mbox *mbox;
672
673 mdev = container_of(controller, struct omap_mbox_device, controller);
674 if (WARN_ON(!mdev))
Benson Leung2d805fc2015-05-04 10:36:36 -0700675 return ERR_PTR(-EINVAL);
Suman Anna8841a662014-11-03 17:05:50 -0600676
677 node = of_find_node_by_phandle(phandle);
678 if (!node) {
679 pr_err("%s: could not find node phandle 0x%x\n",
680 __func__, phandle);
Benson Leung2d805fc2015-05-04 10:36:36 -0700681 return ERR_PTR(-ENODEV);
Suman Anna8841a662014-11-03 17:05:50 -0600682 }
683
684 mbox = omap_mbox_device_find(mdev, node->name);
685 of_node_put(node);
Benson Leung2d805fc2015-05-04 10:36:36 -0700686 return mbox ? mbox->chan : ERR_PTR(-ENOENT);
Suman Anna8841a662014-11-03 17:05:50 -0600687}
688
Suman Anna5040f532014-06-24 19:43:41 -0500689static int omap_mbox_probe(struct platform_device *pdev)
690{
691 struct resource *mem;
692 int ret;
Suman Anna8841a662014-11-03 17:05:50 -0600693 struct mbox_chan *chnls;
Suman Anna5040f532014-06-24 19:43:41 -0500694 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500695 struct omap_mbox_fifo_info *finfo, *finfoblk;
Suman Anna72c1c812014-06-24 19:43:43 -0500696 struct omap_mbox_device *mdev;
Suman Annabe3322e2014-06-24 19:43:42 -0500697 struct omap_mbox_fifo *fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500698 struct device_node *node = pdev->dev.of_node;
699 struct device_node *child;
Suman Annaea2ec1e2018-07-11 18:42:12 -0500700 const struct omap_mbox_match_data *match_data;
Suman Anna75288cc2014-09-10 14:20:59 -0500701 u32 intr_type, info_count;
702 u32 num_users, num_fifos;
703 u32 tmp[3];
Suman Anna5040f532014-06-24 19:43:41 -0500704 u32 l;
705 int i;
706
Suman Anna4899f78a2016-04-06 12:37:37 -0500707 if (!node) {
708 pr_err("%s: only DT-based devices are supported\n", __func__);
Suman Anna5040f532014-06-24 19:43:41 -0500709 return -ENODEV;
710 }
711
Suman Annaea2ec1e2018-07-11 18:42:12 -0500712 match_data = of_device_get_match_data(&pdev->dev);
713 if (!match_data)
Suman Anna4899f78a2016-04-06 12:37:37 -0500714 return -ENODEV;
Suman Annaea2ec1e2018-07-11 18:42:12 -0500715 intr_type = match_data->intr_type;
Suman Anna75288cc2014-09-10 14:20:59 -0500716
Suman Anna4899f78a2016-04-06 12:37:37 -0500717 if (of_property_read_u32(node, "ti,mbox-num-users", &num_users))
718 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500719
Suman Anna4899f78a2016-04-06 12:37:37 -0500720 if (of_property_read_u32(node, "ti,mbox-num-fifos", &num_fifos))
721 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500722
Suman Anna4899f78a2016-04-06 12:37:37 -0500723 info_count = of_get_available_child_count(node);
724 if (!info_count) {
725 dev_err(&pdev->dev, "no available mbox devices found\n");
726 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500727 }
728
Kees Cooka86854d2018-06-12 14:07:58 -0700729 finfoblk = devm_kcalloc(&pdev->dev, info_count, sizeof(*finfoblk),
Suman Anna75288cc2014-09-10 14:20:59 -0500730 GFP_KERNEL);
731 if (!finfoblk)
732 return -ENOMEM;
733
734 finfo = finfoblk;
735 child = NULL;
736 for (i = 0; i < info_count; i++, finfo++) {
Suman Anna4899f78a2016-04-06 12:37:37 -0500737 child = of_get_next_available_child(node, child);
738 ret = of_property_read_u32_array(child, "ti,mbox-tx", tmp,
739 ARRAY_SIZE(tmp));
740 if (ret)
741 return ret;
742 finfo->tx_id = tmp[0];
743 finfo->tx_irq = tmp[1];
744 finfo->tx_usr = tmp[2];
Suman Anna75288cc2014-09-10 14:20:59 -0500745
Suman Anna4899f78a2016-04-06 12:37:37 -0500746 ret = of_property_read_u32_array(child, "ti,mbox-rx", tmp,
747 ARRAY_SIZE(tmp));
748 if (ret)
749 return ret;
750 finfo->rx_id = tmp[0];
751 finfo->rx_irq = tmp[1];
752 finfo->rx_usr = tmp[2];
Suman Anna75288cc2014-09-10 14:20:59 -0500753
Suman Anna4899f78a2016-04-06 12:37:37 -0500754 finfo->name = child->name;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500755
Suman Anna4899f78a2016-04-06 12:37:37 -0500756 if (of_find_property(child, "ti,mbox-send-noirq", NULL))
757 finfo->send_no_irq = true;
758
Suman Anna75288cc2014-09-10 14:20:59 -0500759 if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos ||
760 finfo->tx_usr >= num_users || finfo->rx_usr >= num_users)
761 return -EINVAL;
762 }
763
Suman Anna72c1c812014-06-24 19:43:43 -0500764 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
765 if (!mdev)
766 return -ENOMEM;
767
768 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
769 mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
770 if (IS_ERR(mdev->mbox_base))
771 return PTR_ERR(mdev->mbox_base);
772
Kees Cooka86854d2018-06-12 14:07:58 -0700773 mdev->irq_ctx = devm_kcalloc(&pdev->dev, num_users, sizeof(u32),
Suman Annaaf1d2f52016-04-06 18:37:18 -0500774 GFP_KERNEL);
775 if (!mdev->irq_ctx)
776 return -ENOMEM;
777
Suman Anna5040f532014-06-24 19:43:41 -0500778 /* allocate one extra for marking end of list */
Kees Cooka86854d2018-06-12 14:07:58 -0700779 list = devm_kcalloc(&pdev->dev, info_count + 1, sizeof(*list),
Suman Anna5040f532014-06-24 19:43:41 -0500780 GFP_KERNEL);
781 if (!list)
782 return -ENOMEM;
783
Kees Cooka86854d2018-06-12 14:07:58 -0700784 chnls = devm_kcalloc(&pdev->dev, info_count + 1, sizeof(*chnls),
Suman Anna8841a662014-11-03 17:05:50 -0600785 GFP_KERNEL);
786 if (!chnls)
787 return -ENOMEM;
788
Kees Cooka86854d2018-06-12 14:07:58 -0700789 mboxblk = devm_kcalloc(&pdev->dev, info_count, sizeof(*mbox),
Suman Anna5040f532014-06-24 19:43:41 -0500790 GFP_KERNEL);
791 if (!mboxblk)
792 return -ENOMEM;
793
Suman Anna5040f532014-06-24 19:43:41 -0500794 mbox = mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500795 finfo = finfoblk;
796 for (i = 0; i < info_count; i++, finfo++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500797 fifo = &mbox->tx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500798 fifo->msg = MAILBOX_MESSAGE(finfo->tx_id);
799 fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id);
800 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id);
801 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr);
802 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr);
803 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr);
Suman Anna5040f532014-06-24 19:43:41 -0500804
Suman Annabe3322e2014-06-24 19:43:42 -0500805 fifo = &mbox->rx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500806 fifo->msg = MAILBOX_MESSAGE(finfo->rx_id);
807 fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id);
808 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id);
809 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr);
810 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr);
811 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr);
Suman Annabe3322e2014-06-24 19:43:42 -0500812
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500813 mbox->send_no_irq = finfo->send_no_irq;
Suman Annabe3322e2014-06-24 19:43:42 -0500814 mbox->intr_type = intr_type;
815
Suman Anna72c1c812014-06-24 19:43:43 -0500816 mbox->parent = mdev;
Suman Anna75288cc2014-09-10 14:20:59 -0500817 mbox->name = finfo->name;
818 mbox->irq = platform_get_irq(pdev, finfo->tx_irq);
Suman Anna5040f532014-06-24 19:43:41 -0500819 if (mbox->irq < 0)
820 return mbox->irq;
Suman Anna8841a662014-11-03 17:05:50 -0600821 mbox->chan = &chnls[i];
822 chnls[i].con_priv = mbox;
Suman Anna5040f532014-06-24 19:43:41 -0500823 list[i] = mbox++;
824 }
825
Suman Anna72c1c812014-06-24 19:43:43 -0500826 mutex_init(&mdev->cfg_lock);
827 mdev->dev = &pdev->dev;
Suman Anna75288cc2014-09-10 14:20:59 -0500828 mdev->num_users = num_users;
829 mdev->num_fifos = num_fifos;
Suman Anna2240f8a2016-04-06 18:37:17 -0500830 mdev->intr_type = intr_type;
Suman Anna72c1c812014-06-24 19:43:43 -0500831 mdev->mboxes = list;
Suman Anna8841a662014-11-03 17:05:50 -0600832
833 /* OMAP does not have a Tx-Done IRQ, but rather a Tx-Ready IRQ */
834 mdev->controller.txdone_irq = true;
835 mdev->controller.dev = mdev->dev;
836 mdev->controller.ops = &omap_mbox_chan_ops;
837 mdev->controller.chans = chnls;
838 mdev->controller.num_chans = info_count;
839 mdev->controller.of_xlate = omap_mbox_of_xlate;
Suman Anna72c1c812014-06-24 19:43:43 -0500840 ret = omap_mbox_register(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500841 if (ret)
842 return ret;
843
Suman Anna72c1c812014-06-24 19:43:43 -0500844 platform_set_drvdata(pdev, mdev);
845 pm_runtime_enable(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500846
Suman Anna72c1c812014-06-24 19:43:43 -0500847 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500848 if (ret < 0) {
Suman Anna72c1c812014-06-24 19:43:43 -0500849 pm_runtime_put_noidle(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500850 goto unregister;
851 }
852
853 /*
854 * just print the raw revision register, the format is not
855 * uniform across all SoCs
856 */
Suman Anna72c1c812014-06-24 19:43:43 -0500857 l = mbox_read_reg(mdev, MAILBOX_REVISION);
858 dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
Suman Anna5040f532014-06-24 19:43:41 -0500859
Suman Anna72c1c812014-06-24 19:43:43 -0500860 ret = pm_runtime_put_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500861 if (ret < 0)
862 goto unregister;
863
Suman Anna75288cc2014-09-10 14:20:59 -0500864 devm_kfree(&pdev->dev, finfoblk);
Suman Anna5040f532014-06-24 19:43:41 -0500865 return 0;
866
867unregister:
Suman Anna72c1c812014-06-24 19:43:43 -0500868 pm_runtime_disable(mdev->dev);
869 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500870 return ret;
871}
872
873static int omap_mbox_remove(struct platform_device *pdev)
874{
Suman Anna72c1c812014-06-24 19:43:43 -0500875 struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
876
877 pm_runtime_disable(mdev->dev);
878 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500879
880 return 0;
881}
882
883static struct platform_driver omap_mbox_driver = {
884 .probe = omap_mbox_probe,
885 .remove = omap_mbox_remove,
886 .driver = {
887 .name = "omap-mailbox",
Suman Annaaf1d2f52016-04-06 18:37:18 -0500888 .pm = &omap_mbox_pm_ops,
Suman Anna75288cc2014-09-10 14:20:59 -0500889 .of_match_table = of_match_ptr(omap_mailbox_of_match),
Suman Anna5040f532014-06-24 19:43:41 -0500890 },
891};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800892
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800893static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800894{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300895 int err;
896
897 err = class_register(&omap_mbox_class);
898 if (err)
899 return err;
900
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000901 /* kfifo size sanity check: alignment and minimal size */
902 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000903 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
904 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000905
Arvind Yadav1f90a2162017-11-11 23:39:18 +0530906 err = platform_driver_register(&omap_mbox_driver);
907 if (err)
908 class_unregister(&omap_mbox_class);
909
910 return err;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800911}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300912subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800913
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800914static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800915{
Suman Anna5040f532014-06-24 19:43:41 -0500916 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300917 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800918}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800919module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800920
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700921MODULE_LICENSE("GPL v2");
922MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000923MODULE_AUTHOR("Toshihiro Kobayashi");
924MODULE_AUTHOR("Hiroshi DOYU");