blob: 99a66e11ec1e36a084890f8d0612981f2123969b [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;
93 struct omap_mbox **mboxes;
Suman Anna8841a662014-11-03 17:05:50 -060094 struct mbox_controller controller;
Suman Anna72c1c812014-06-24 19:43:43 -050095 struct list_head elem;
96};
97
Suman Anna75288cc2014-09-10 14:20:59 -050098struct omap_mbox_fifo_info {
99 int tx_id;
100 int tx_usr;
101 int tx_irq;
102
103 int rx_id;
104 int rx_usr;
105 int rx_irq;
106
107 const char *name;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500108 bool send_no_irq;
Suman Anna75288cc2014-09-10 14:20:59 -0500109};
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;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500122 bool send_no_irq;
Suman Anna5040f532014-06-24 19:43:41 -0500123};
124
Suman Anna72c1c812014-06-24 19:43:43 -0500125/* global variables for the mailbox devices */
126static DEFINE_MUTEX(omap_mbox_devices_lock);
127static LIST_HEAD(omap_mbox_devices);
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800128
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000129static unsigned int mbox_kfifo_size = CONFIG_OMAP_MBOX_KFIFO_SIZE;
130module_param(mbox_kfifo_size, uint, S_IRUGO);
131MODULE_PARM_DESC(mbox_kfifo_size, "Size of omap's mailbox kfifo (bytes)");
132
Suman Anna8841a662014-11-03 17:05:50 -0600133static struct omap_mbox *mbox_chan_to_omap_mbox(struct mbox_chan *chan)
134{
135 if (!chan || !chan->con_priv)
136 return NULL;
137
138 return (struct omap_mbox *)chan->con_priv;
139}
140
Suman Anna72c1c812014-06-24 19:43:43 -0500141static inline
142unsigned int mbox_read_reg(struct omap_mbox_device *mdev, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500143{
Suman Anna72c1c812014-06-24 19:43:43 -0500144 return __raw_readl(mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500145}
146
Suman Anna72c1c812014-06-24 19:43:43 -0500147static inline
148void mbox_write_reg(struct omap_mbox_device *mdev, u32 val, size_t ofs)
Suman Anna5040f532014-06-24 19:43:41 -0500149{
Suman Anna72c1c812014-06-24 19:43:43 -0500150 __raw_writel(val, mdev->mbox_base + ofs);
Suman Anna5040f532014-06-24 19:43:41 -0500151}
152
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700153/* Mailbox FIFO handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500154static mbox_msg_t mbox_fifo_read(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700155{
Suman Annabe3322e2014-06-24 19:43:42 -0500156 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500157 return (mbox_msg_t) mbox_read_reg(mbox->parent, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700158}
Suman Anna5040f532014-06-24 19:43:41 -0500159
160static void mbox_fifo_write(struct omap_mbox *mbox, mbox_msg_t msg)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700161{
Suman Annabe3322e2014-06-24 19:43:42 -0500162 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500163 mbox_write_reg(mbox->parent, msg, fifo->msg);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700164}
Suman Anna5040f532014-06-24 19:43:41 -0500165
166static int mbox_fifo_empty(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700167{
Suman Annabe3322e2014-06-24 19:43:42 -0500168 struct omap_mbox_fifo *fifo = &mbox->rx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500169 return (mbox_read_reg(mbox->parent, fifo->msg_stat) == 0);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700170}
Suman Anna5040f532014-06-24 19:43:41 -0500171
172static int mbox_fifo_full(struct omap_mbox *mbox)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700173{
Suman Annabe3322e2014-06-24 19:43:42 -0500174 struct omap_mbox_fifo *fifo = &mbox->tx_fifo;
Suman Anna72c1c812014-06-24 19:43:43 -0500175 return mbox_read_reg(mbox->parent, fifo->fifo_stat);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700176}
177
178/* Mailbox IRQ handle functions */
Suman Anna5040f532014-06-24 19:43:41 -0500179static void ack_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700180{
Suman Annabe3322e2014-06-24 19:43:42 -0500181 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
182 &mbox->tx_fifo : &mbox->rx_fifo;
183 u32 bit = fifo->intr_bit;
184 u32 irqstatus = fifo->irqstatus;
Suman Anna5040f532014-06-24 19:43:41 -0500185
Suman Anna72c1c812014-06-24 19:43:43 -0500186 mbox_write_reg(mbox->parent, bit, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500187
188 /* Flush posted write for irq status to avoid spurious interrupts */
Suman Anna72c1c812014-06-24 19:43:43 -0500189 mbox_read_reg(mbox->parent, irqstatus);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700190}
Suman Anna5040f532014-06-24 19:43:41 -0500191
192static int is_mbox_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700193{
Suman Annabe3322e2014-06-24 19:43:42 -0500194 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
195 &mbox->tx_fifo : &mbox->rx_fifo;
196 u32 bit = fifo->intr_bit;
197 u32 irqenable = fifo->irqenable;
198 u32 irqstatus = fifo->irqstatus;
199
Suman Anna72c1c812014-06-24 19:43:43 -0500200 u32 enable = mbox_read_reg(mbox->parent, irqenable);
201 u32 status = mbox_read_reg(mbox->parent, irqstatus);
Suman Anna5040f532014-06-24 19:43:41 -0500202
203 return (int)(enable & status & bit);
Hiroshi DOYU9ae0ee02009-03-23 18:07:26 -0700204}
205
Suman Anna8841a662014-11-03 17:05:50 -0600206void omap_mbox_save_ctx(struct mbox_chan *chan)
Suman Annac869c752013-03-12 17:55:29 -0500207{
Suman Anna5040f532014-06-24 19:43:41 -0500208 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500209 int nr_regs;
Suman Anna8841a662014-11-03 17:05:50 -0600210 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
211
212 if (WARN_ON(!mbox))
213 return;
Suman Annac869c752013-03-12 17:55:29 -0500214
Suman Annabe3322e2014-06-24 19:43:42 -0500215 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500216 nr_regs = OMAP4_MBOX_NR_REGS;
217 else
218 nr_regs = MBOX_NR_REGS;
219 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500220 mbox->ctx[i] = mbox_read_reg(mbox->parent, i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500221
222 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500223 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500224 }
Suman Annac869c752013-03-12 17:55:29 -0500225}
226EXPORT_SYMBOL(omap_mbox_save_ctx);
227
Suman Anna8841a662014-11-03 17:05:50 -0600228void omap_mbox_restore_ctx(struct mbox_chan *chan)
Suman Annac869c752013-03-12 17:55:29 -0500229{
Suman Anna5040f532014-06-24 19:43:41 -0500230 int i;
Suman Anna5040f532014-06-24 19:43:41 -0500231 int nr_regs;
Suman Anna8841a662014-11-03 17:05:50 -0600232 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
233
234 if (WARN_ON(!mbox))
235 return;
Suman Annac869c752013-03-12 17:55:29 -0500236
Suman Annabe3322e2014-06-24 19:43:42 -0500237 if (mbox->intr_type)
Suman Anna5040f532014-06-24 19:43:41 -0500238 nr_regs = OMAP4_MBOX_NR_REGS;
239 else
240 nr_regs = MBOX_NR_REGS;
241 for (i = 0; i < nr_regs; i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500242 mbox_write_reg(mbox->parent, mbox->ctx[i], i * sizeof(u32));
Suman Anna5040f532014-06-24 19:43:41 -0500243 dev_dbg(mbox->dev, "%s: [%02x] %08x\n", __func__,
Suman Annabe3322e2014-06-24 19:43:42 -0500244 i, mbox->ctx[i]);
Suman Anna5040f532014-06-24 19:43:41 -0500245 }
Suman Annac869c752013-03-12 17:55:29 -0500246}
247EXPORT_SYMBOL(omap_mbox_restore_ctx);
248
Suman Anna8841a662014-11-03 17:05:50 -0600249static void _omap_mbox_enable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500250{
Suman Annabe3322e2014-06-24 19:43:42 -0500251 u32 l;
252 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
253 &mbox->tx_fifo : &mbox->rx_fifo;
254 u32 bit = fifo->intr_bit;
255 u32 irqenable = fifo->irqenable;
Suman Anna5040f532014-06-24 19:43:41 -0500256
Suman Anna72c1c812014-06-24 19:43:43 -0500257 l = mbox_read_reg(mbox->parent, irqenable);
Suman Anna5040f532014-06-24 19:43:41 -0500258 l |= bit;
Suman Anna72c1c812014-06-24 19:43:43 -0500259 mbox_write_reg(mbox->parent, l, irqenable);
Suman Annac869c752013-03-12 17:55:29 -0500260}
Suman Annac869c752013-03-12 17:55:29 -0500261
Suman Anna8841a662014-11-03 17:05:50 -0600262static void _omap_mbox_disable_irq(struct omap_mbox *mbox, omap_mbox_irq_t irq)
Suman Annac869c752013-03-12 17:55:29 -0500263{
Suman Annabe3322e2014-06-24 19:43:42 -0500264 struct omap_mbox_fifo *fifo = (irq == IRQ_TX) ?
265 &mbox->tx_fifo : &mbox->rx_fifo;
266 u32 bit = fifo->intr_bit;
267 u32 irqdisable = fifo->irqdisable;
Suman Anna5040f532014-06-24 19:43:41 -0500268
269 /*
270 * Read and update the interrupt configuration register for pre-OMAP4.
271 * OMAP4 and later SoCs have a dedicated interrupt disabling register.
272 */
Suman Annabe3322e2014-06-24 19:43:42 -0500273 if (!mbox->intr_type)
Suman Anna72c1c812014-06-24 19:43:43 -0500274 bit = mbox_read_reg(mbox->parent, irqdisable) & ~bit;
Suman Anna5040f532014-06-24 19:43:41 -0500275
Suman Anna72c1c812014-06-24 19:43:43 -0500276 mbox_write_reg(mbox->parent, bit, irqdisable);
Suman Annac869c752013-03-12 17:55:29 -0500277}
Suman Annac869c752013-03-12 17:55:29 -0500278
Suman Anna8841a662014-11-03 17:05:50 -0600279void omap_mbox_enable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800280{
Suman Anna8841a662014-11-03 17:05:50 -0600281 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800282
Suman Anna8841a662014-11-03 17:05:50 -0600283 if (WARN_ON(!mbox))
284 return;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000285
Suman Anna8841a662014-11-03 17:05:50 -0600286 _omap_mbox_enable_irq(mbox, irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800287}
Suman Anna8841a662014-11-03 17:05:50 -0600288EXPORT_SYMBOL(omap_mbox_enable_irq);
289
290void omap_mbox_disable_irq(struct mbox_chan *chan, omap_mbox_irq_t irq)
291{
292 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
293
294 if (WARN_ON(!mbox))
295 return;
296
297 _omap_mbox_disable_irq(mbox, irq);
298}
299EXPORT_SYMBOL(omap_mbox_disable_irq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800300
301/*
302 * Message receiver(workqueue)
303 */
304static void mbox_rx_work(struct work_struct *work)
305{
306 struct omap_mbox_queue *mq =
307 container_of(work, struct omap_mbox_queue, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800308 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000309 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800310
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000311 while (kfifo_len(&mq->fifo) >= sizeof(msg)) {
312 len = kfifo_out(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
313 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800314
Suman Anna8841a662014-11-03 17:05:50 -0600315 mbox_chan_received_data(mq->mbox->chan, (void *)msg);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000316 spin_lock_irq(&mq->lock);
317 if (mq->full) {
318 mq->full = false;
Suman Anna8841a662014-11-03 17:05:50 -0600319 _omap_mbox_enable_irq(mq->mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000320 }
321 spin_unlock_irq(&mq->lock);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800322 }
323}
324
325/*
326 * Mailbox interrupt handler
327 */
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800328static void __mbox_tx_interrupt(struct omap_mbox *mbox)
329{
Suman Anna8841a662014-11-03 17:05:50 -0600330 _omap_mbox_disable_irq(mbox, IRQ_TX);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800331 ack_mbox_irq(mbox, IRQ_TX);
Suman Anna8841a662014-11-03 17:05:50 -0600332 mbox_chan_txdone(mbox->chan, 0);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800333}
334
335static void __mbox_rx_interrupt(struct omap_mbox *mbox)
336{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000337 struct omap_mbox_queue *mq = mbox->rxq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800338 mbox_msg_t msg;
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000339 int len;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800340
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800341 while (!mbox_fifo_empty(mbox)) {
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000342 if (unlikely(kfifo_avail(&mq->fifo) < sizeof(msg))) {
Suman Anna8841a662014-11-03 17:05:50 -0600343 _omap_mbox_disable_irq(mbox, IRQ_RX);
Fernando Guzman Lugod2295042010-11-29 20:24:11 +0000344 mq->full = true;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800345 goto nomem;
Fernando Guzman Lugo1ea5d6d2010-02-08 13:35:40 -0600346 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800347
348 msg = mbox_fifo_read(mbox);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800349
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000350 len = kfifo_in(&mq->fifo, (unsigned char *)&msg, sizeof(msg));
351 WARN_ON(len != sizeof(msg));
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800352 }
353
354 /* no more messages in the fifo. clear IRQ source. */
355 ack_mbox_irq(mbox, IRQ_RX);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700356nomem:
Tejun Heoc4873002011-01-26 12:12:50 +0100357 schedule_work(&mbox->rxq->work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800358}
359
360static irqreturn_t mbox_interrupt(int irq, void *p)
361{
Jeff Garzik2a7057e2007-10-26 05:40:22 -0400362 struct omap_mbox *mbox = p;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800363
364 if (is_mbox_irq(mbox, IRQ_TX))
365 __mbox_tx_interrupt(mbox);
366
367 if (is_mbox_irq(mbox, IRQ_RX))
368 __mbox_rx_interrupt(mbox);
369
370 return IRQ_HANDLED;
371}
372
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800373static struct omap_mbox_queue *mbox_queue_alloc(struct omap_mbox *mbox,
Suman Anna8841a662014-11-03 17:05:50 -0600374 void (*work)(struct work_struct *))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800375{
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800376 struct omap_mbox_queue *mq;
377
Suman Anna8841a662014-11-03 17:05:50 -0600378 if (!work)
379 return NULL;
380
Suman Anna86f6f5e2016-04-06 12:37:38 -0500381 mq = kzalloc(sizeof(*mq), GFP_KERNEL);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800382 if (!mq)
383 return NULL;
384
385 spin_lock_init(&mq->lock);
386
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000387 if (kfifo_alloc(&mq->fifo, mbox_kfifo_size, GFP_KERNEL))
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800388 goto error;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800389
Suman Anna8841a662014-11-03 17:05:50 -0600390 INIT_WORK(&mq->work, work);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800391 return mq;
Suman Anna8841a662014-11-03 17:05:50 -0600392
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800393error:
394 kfree(mq);
395 return NULL;
396}
397
398static void mbox_queue_free(struct omap_mbox_queue *q)
399{
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000400 kfifo_free(&q->fifo);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800401 kfree(q);
402}
403
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800404static int omap_mbox_startup(struct omap_mbox *mbox)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800405{
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800406 int ret = 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800407 struct omap_mbox_queue *mq;
408
Suman Anna8841a662014-11-03 17:05:50 -0600409 mq = mbox_queue_alloc(mbox, mbox_rx_work);
410 if (!mq)
411 return -ENOMEM;
412 mbox->rxq = mq;
413 mq->mbox = mbox;
C A Subramaniam5f00ec62009-11-22 10:11:22 -0800414
Suman Anna8841a662014-11-03 17:05:50 -0600415 ret = request_irq(mbox->irq, mbox_interrupt, IRQF_SHARED,
416 mbox->name, mbox);
417 if (unlikely(ret)) {
418 pr_err("failed to register mailbox interrupt:%d\n", ret);
419 goto fail_request_irq;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800420 }
Suman Anna8841a662014-11-03 17:05:50 -0600421
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500422 if (mbox->send_no_irq)
423 mbox->chan->txdone_method = TXDONE_BY_ACK;
424
Suman Anna8841a662014-11-03 17:05:50 -0600425 _omap_mbox_enable_irq(mbox, IRQ_RX);
426
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800427 return 0;
428
Suman Annaecf305c2013-02-01 20:37:06 -0600429fail_request_irq:
430 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800431 return ret;
432}
433
434static void omap_mbox_fini(struct omap_mbox *mbox)
435{
Suman Anna8841a662014-11-03 17:05:50 -0600436 _omap_mbox_disable_irq(mbox, IRQ_RX);
437 free_irq(mbox->irq, mbox);
438 flush_work(&mbox->rxq->work);
439 mbox_queue_free(mbox->rxq);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800440}
441
Suman Anna72c1c812014-06-24 19:43:43 -0500442static struct omap_mbox *omap_mbox_device_find(struct omap_mbox_device *mdev,
443 const char *mbox_name)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800444{
Kevin Hilmanc0377322011-02-11 19:56:43 +0000445 struct omap_mbox *_mbox, *mbox = NULL;
Suman Anna72c1c812014-06-24 19:43:43 -0500446 struct omap_mbox **mboxes = mdev->mboxes;
447 int i;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800448
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000449 if (!mboxes)
Suman Anna72c1c812014-06-24 19:43:43 -0500450 return NULL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800451
Kevin Hilmanc0377322011-02-11 19:56:43 +0000452 for (i = 0; (_mbox = mboxes[i]); i++) {
Suman Anna72c1c812014-06-24 19:43:43 -0500453 if (!strcmp(_mbox->name, mbox_name)) {
Kevin Hilmanc0377322011-02-11 19:56:43 +0000454 mbox = _mbox;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000455 break;
Kevin Hilmanc0377322011-02-11 19:56:43 +0000456 }
457 }
Suman Anna72c1c812014-06-24 19:43:43 -0500458 return mbox;
459}
460
Suman Anna8841a662014-11-03 17:05:50 -0600461struct mbox_chan *omap_mbox_request_channel(struct mbox_client *cl,
462 const char *chan_name)
Suman Anna72c1c812014-06-24 19:43:43 -0500463{
Suman Anna8841a662014-11-03 17:05:50 -0600464 struct device *dev = cl->dev;
Suman Anna72c1c812014-06-24 19:43:43 -0500465 struct omap_mbox *mbox = NULL;
466 struct omap_mbox_device *mdev;
Suman Anna8841a662014-11-03 17:05:50 -0600467 struct mbox_chan *chan;
468 unsigned long flags;
Suman Anna72c1c812014-06-24 19:43:43 -0500469 int ret;
470
Suman Anna8841a662014-11-03 17:05:50 -0600471 if (!dev)
472 return ERR_PTR(-ENODEV);
473
474 if (dev->of_node) {
475 pr_err("%s: please use mbox_request_channel(), this API is supported only for OMAP non-DT usage\n",
476 __func__);
477 return ERR_PTR(-ENODEV);
478 }
479
Suman Anna72c1c812014-06-24 19:43:43 -0500480 mutex_lock(&omap_mbox_devices_lock);
481 list_for_each_entry(mdev, &omap_mbox_devices, elem) {
Suman Anna8841a662014-11-03 17:05:50 -0600482 mbox = omap_mbox_device_find(mdev, chan_name);
Suman Anna72c1c812014-06-24 19:43:43 -0500483 if (mbox)
484 break;
485 }
486 mutex_unlock(&omap_mbox_devices_lock);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000487
Suman Anna8841a662014-11-03 17:05:50 -0600488 if (!mbox || !mbox->chan)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000489 return ERR_PTR(-ENOENT);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800490
Suman Anna8841a662014-11-03 17:05:50 -0600491 chan = mbox->chan;
492 spin_lock_irqsave(&chan->lock, flags);
493 chan->msg_free = 0;
494 chan->msg_count = 0;
495 chan->active_req = NULL;
496 chan->cl = cl;
497 init_completion(&chan->tx_complete);
498 spin_unlock_irqrestore(&chan->lock, flags);
Kanigeri, Hari58256302010-11-29 20:24:14 +0000499
Suman Anna8841a662014-11-03 17:05:50 -0600500 ret = chan->mbox->ops->startup(chan);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300501 if (ret) {
Suman Anna8841a662014-11-03 17:05:50 -0600502 pr_err("Unable to startup the chan (%d)\n", ret);
503 mbox_free_channel(chan);
504 chan = ERR_PTR(ret);
Juan Gutierrez1d8a0e92012-05-13 15:33:04 +0300505 }
506
Suman Anna8841a662014-11-03 17:05:50 -0600507 return chan;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800508}
Suman Anna8841a662014-11-03 17:05:50 -0600509EXPORT_SYMBOL(omap_mbox_request_channel);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800510
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300511static struct class omap_mbox_class = { .name = "mbox", };
512
Suman Anna72c1c812014-06-24 19:43:43 -0500513static int omap_mbox_register(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800514{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000515 int ret;
516 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500517 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800518
Suman Anna72c1c812014-06-24 19:43:43 -0500519 if (!mdev || !mdev->mboxes)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800520 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800521
Suman Anna72c1c812014-06-24 19:43:43 -0500522 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000523 for (i = 0; mboxes[i]; i++) {
524 struct omap_mbox *mbox = mboxes[i];
Suman Anna8841a662014-11-03 17:05:50 -0600525 mbox->dev = device_create(&omap_mbox_class, mdev->dev,
526 0, mbox, "%s", mbox->name);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000527 if (IS_ERR(mbox->dev)) {
528 ret = PTR_ERR(mbox->dev);
529 goto err_out;
530 }
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700531 }
Suman Anna72c1c812014-06-24 19:43:43 -0500532
533 mutex_lock(&omap_mbox_devices_lock);
534 list_add(&mdev->elem, &omap_mbox_devices);
535 mutex_unlock(&omap_mbox_devices_lock);
536
Suman Anna8841a662014-11-03 17:05:50 -0600537 ret = mbox_controller_register(&mdev->controller);
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700538
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000539err_out:
Suman Anna8841a662014-11-03 17:05:50 -0600540 if (ret) {
541 while (i--)
542 device_unregister(mboxes[i]->dev);
543 }
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800544 return ret;
545}
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800546
Suman Anna72c1c812014-06-24 19:43:43 -0500547static int omap_mbox_unregister(struct omap_mbox_device *mdev)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800548{
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000549 int i;
Suman Anna72c1c812014-06-24 19:43:43 -0500550 struct omap_mbox **mboxes;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800551
Suman Anna72c1c812014-06-24 19:43:43 -0500552 if (!mdev || !mdev->mboxes)
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000553 return -EINVAL;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800554
Suman Anna72c1c812014-06-24 19:43:43 -0500555 mutex_lock(&omap_mbox_devices_lock);
556 list_del(&mdev->elem);
557 mutex_unlock(&omap_mbox_devices_lock);
558
Suman Anna8841a662014-11-03 17:05:50 -0600559 mbox_controller_unregister(&mdev->controller);
560
Suman Anna72c1c812014-06-24 19:43:43 -0500561 mboxes = mdev->mboxes;
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000562 for (i = 0; mboxes[i]; i++)
563 device_unregister(mboxes[i]->dev);
Felipe Contreras9c80c8c2010-06-11 15:51:46 +0000564 return 0;
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800565}
Suman Anna5040f532014-06-24 19:43:41 -0500566
Suman Anna8841a662014-11-03 17:05:50 -0600567static int omap_mbox_chan_startup(struct mbox_chan *chan)
568{
569 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
570 struct omap_mbox_device *mdev = mbox->parent;
571 int ret = 0;
572
573 mutex_lock(&mdev->cfg_lock);
574 pm_runtime_get_sync(mdev->dev);
575 ret = omap_mbox_startup(mbox);
576 if (ret)
577 pm_runtime_put_sync(mdev->dev);
578 mutex_unlock(&mdev->cfg_lock);
579 return ret;
580}
581
582static void omap_mbox_chan_shutdown(struct mbox_chan *chan)
583{
584 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
585 struct omap_mbox_device *mdev = mbox->parent;
586
587 mutex_lock(&mdev->cfg_lock);
588 omap_mbox_fini(mbox);
589 pm_runtime_put_sync(mdev->dev);
590 mutex_unlock(&mdev->cfg_lock);
591}
592
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500593static int omap_mbox_chan_send_noirq(struct omap_mbox *mbox, void *data)
Suman Anna8841a662014-11-03 17:05:50 -0600594{
Suman Anna8841a662014-11-03 17:05:50 -0600595 int ret = -EBUSY;
596
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500597 if (!mbox_fifo_full(mbox)) {
598 _omap_mbox_enable_irq(mbox, IRQ_RX);
599 mbox_fifo_write(mbox, (mbox_msg_t)data);
600 ret = 0;
601 _omap_mbox_disable_irq(mbox, IRQ_RX);
602
603 /* we must read and ack the interrupt directly from here */
604 mbox_fifo_read(mbox);
605 ack_mbox_irq(mbox, IRQ_RX);
606 }
607
608 return ret;
609}
610
611static int omap_mbox_chan_send(struct omap_mbox *mbox, void *data)
612{
613 int ret = -EBUSY;
Suman Anna8841a662014-11-03 17:05:50 -0600614
615 if (!mbox_fifo_full(mbox)) {
616 mbox_fifo_write(mbox, (mbox_msg_t)data);
617 ret = 0;
618 }
619
620 /* always enable the interrupt */
621 _omap_mbox_enable_irq(mbox, IRQ_TX);
622 return ret;
623}
624
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500625static int omap_mbox_chan_send_data(struct mbox_chan *chan, void *data)
626{
627 struct omap_mbox *mbox = mbox_chan_to_omap_mbox(chan);
628 int ret;
629
630 if (!mbox)
631 return -EINVAL;
632
633 if (mbox->send_no_irq)
634 ret = omap_mbox_chan_send_noirq(mbox, data);
635 else
636 ret = omap_mbox_chan_send(mbox, data);
637
638 return ret;
639}
640
Andrew Bresticker05ae7972015-05-04 10:36:35 -0700641static const struct mbox_chan_ops omap_mbox_chan_ops = {
Suman Anna8841a662014-11-03 17:05:50 -0600642 .startup = omap_mbox_chan_startup,
643 .send_data = omap_mbox_chan_send_data,
644 .shutdown = omap_mbox_chan_shutdown,
645};
646
Suman Anna75288cc2014-09-10 14:20:59 -0500647static const struct of_device_id omap_mailbox_of_match[] = {
648 {
649 .compatible = "ti,omap2-mailbox",
650 .data = (void *)MBOX_INTR_CFG_TYPE1,
651 },
652 {
653 .compatible = "ti,omap3-mailbox",
654 .data = (void *)MBOX_INTR_CFG_TYPE1,
655 },
656 {
657 .compatible = "ti,omap4-mailbox",
658 .data = (void *)MBOX_INTR_CFG_TYPE2,
659 },
660 {
661 /* end */
662 },
663};
664MODULE_DEVICE_TABLE(of, omap_mailbox_of_match);
665
Suman Anna8841a662014-11-03 17:05:50 -0600666static struct mbox_chan *omap_mbox_of_xlate(struct mbox_controller *controller,
667 const struct of_phandle_args *sp)
668{
669 phandle phandle = sp->args[0];
670 struct device_node *node;
671 struct omap_mbox_device *mdev;
672 struct omap_mbox *mbox;
673
674 mdev = container_of(controller, struct omap_mbox_device, controller);
675 if (WARN_ON(!mdev))
Benson Leung2d805fc2015-05-04 10:36:36 -0700676 return ERR_PTR(-EINVAL);
Suman Anna8841a662014-11-03 17:05:50 -0600677
678 node = of_find_node_by_phandle(phandle);
679 if (!node) {
680 pr_err("%s: could not find node phandle 0x%x\n",
681 __func__, phandle);
Benson Leung2d805fc2015-05-04 10:36:36 -0700682 return ERR_PTR(-ENODEV);
Suman Anna8841a662014-11-03 17:05:50 -0600683 }
684
685 mbox = omap_mbox_device_find(mdev, node->name);
686 of_node_put(node);
Benson Leung2d805fc2015-05-04 10:36:36 -0700687 return mbox ? mbox->chan : ERR_PTR(-ENOENT);
Suman Anna8841a662014-11-03 17:05:50 -0600688}
689
Suman Anna5040f532014-06-24 19:43:41 -0500690static int omap_mbox_probe(struct platform_device *pdev)
691{
692 struct resource *mem;
693 int ret;
Suman Anna8841a662014-11-03 17:05:50 -0600694 struct mbox_chan *chnls;
Suman Anna5040f532014-06-24 19:43:41 -0500695 struct omap_mbox **list, *mbox, *mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500696 struct omap_mbox_fifo_info *finfo, *finfoblk;
Suman Anna72c1c812014-06-24 19:43:43 -0500697 struct omap_mbox_device *mdev;
Suman Annabe3322e2014-06-24 19:43:42 -0500698 struct omap_mbox_fifo *fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500699 struct device_node *node = pdev->dev.of_node;
700 struct device_node *child;
701 const struct of_device_id *match;
702 u32 intr_type, info_count;
703 u32 num_users, num_fifos;
704 u32 tmp[3];
Suman Anna5040f532014-06-24 19:43:41 -0500705 u32 l;
706 int i;
707
Suman Anna4899f78a2016-04-06 12:37:37 -0500708 if (!node) {
709 pr_err("%s: only DT-based devices are supported\n", __func__);
Suman Anna5040f532014-06-24 19:43:41 -0500710 return -ENODEV;
711 }
712
Suman Anna4899f78a2016-04-06 12:37:37 -0500713 match = of_match_device(omap_mailbox_of_match, &pdev->dev);
714 if (!match)
715 return -ENODEV;
716 intr_type = (u32)match->data;
Suman Anna75288cc2014-09-10 14:20:59 -0500717
Suman Anna4899f78a2016-04-06 12:37:37 -0500718 if (of_property_read_u32(node, "ti,mbox-num-users", &num_users))
719 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500720
Suman Anna4899f78a2016-04-06 12:37:37 -0500721 if (of_property_read_u32(node, "ti,mbox-num-fifos", &num_fifos))
722 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500723
Suman Anna4899f78a2016-04-06 12:37:37 -0500724 info_count = of_get_available_child_count(node);
725 if (!info_count) {
726 dev_err(&pdev->dev, "no available mbox devices found\n");
727 return -ENODEV;
Suman Anna75288cc2014-09-10 14:20:59 -0500728 }
729
730 finfoblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*finfoblk),
731 GFP_KERNEL);
732 if (!finfoblk)
733 return -ENOMEM;
734
735 finfo = finfoblk;
736 child = NULL;
737 for (i = 0; i < info_count; i++, finfo++) {
Suman Anna4899f78a2016-04-06 12:37:37 -0500738 child = of_get_next_available_child(node, child);
739 ret = of_property_read_u32_array(child, "ti,mbox-tx", tmp,
740 ARRAY_SIZE(tmp));
741 if (ret)
742 return ret;
743 finfo->tx_id = tmp[0];
744 finfo->tx_irq = tmp[1];
745 finfo->tx_usr = tmp[2];
Suman Anna75288cc2014-09-10 14:20:59 -0500746
Suman Anna4899f78a2016-04-06 12:37:37 -0500747 ret = of_property_read_u32_array(child, "ti,mbox-rx", tmp,
748 ARRAY_SIZE(tmp));
749 if (ret)
750 return ret;
751 finfo->rx_id = tmp[0];
752 finfo->rx_irq = tmp[1];
753 finfo->rx_usr = tmp[2];
Suman Anna75288cc2014-09-10 14:20:59 -0500754
Suman Anna4899f78a2016-04-06 12:37:37 -0500755 finfo->name = child->name;
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500756
Suman Anna4899f78a2016-04-06 12:37:37 -0500757 if (of_find_property(child, "ti,mbox-send-noirq", NULL))
758 finfo->send_no_irq = true;
759
Suman Anna75288cc2014-09-10 14:20:59 -0500760 if (finfo->tx_id >= num_fifos || finfo->rx_id >= num_fifos ||
761 finfo->tx_usr >= num_users || finfo->rx_usr >= num_users)
762 return -EINVAL;
763 }
764
Suman Anna72c1c812014-06-24 19:43:43 -0500765 mdev = devm_kzalloc(&pdev->dev, sizeof(*mdev), GFP_KERNEL);
766 if (!mdev)
767 return -ENOMEM;
768
769 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
770 mdev->mbox_base = devm_ioremap_resource(&pdev->dev, mem);
771 if (IS_ERR(mdev->mbox_base))
772 return PTR_ERR(mdev->mbox_base);
773
Suman Anna5040f532014-06-24 19:43:41 -0500774 /* allocate one extra for marking end of list */
Suman Anna75288cc2014-09-10 14:20:59 -0500775 list = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*list),
Suman Anna5040f532014-06-24 19:43:41 -0500776 GFP_KERNEL);
777 if (!list)
778 return -ENOMEM;
779
Suman Anna8841a662014-11-03 17:05:50 -0600780 chnls = devm_kzalloc(&pdev->dev, (info_count + 1) * sizeof(*chnls),
781 GFP_KERNEL);
782 if (!chnls)
783 return -ENOMEM;
784
Suman Anna75288cc2014-09-10 14:20:59 -0500785 mboxblk = devm_kzalloc(&pdev->dev, info_count * sizeof(*mbox),
Suman Anna5040f532014-06-24 19:43:41 -0500786 GFP_KERNEL);
787 if (!mboxblk)
788 return -ENOMEM;
789
Suman Anna5040f532014-06-24 19:43:41 -0500790 mbox = mboxblk;
Suman Anna75288cc2014-09-10 14:20:59 -0500791 finfo = finfoblk;
792 for (i = 0; i < info_count; i++, finfo++) {
Suman Annabe3322e2014-06-24 19:43:42 -0500793 fifo = &mbox->tx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500794 fifo->msg = MAILBOX_MESSAGE(finfo->tx_id);
795 fifo->fifo_stat = MAILBOX_FIFOSTATUS(finfo->tx_id);
796 fifo->intr_bit = MAILBOX_IRQ_NOTFULL(finfo->tx_id);
797 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->tx_usr);
798 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->tx_usr);
799 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->tx_usr);
Suman Anna5040f532014-06-24 19:43:41 -0500800
Suman Annabe3322e2014-06-24 19:43:42 -0500801 fifo = &mbox->rx_fifo;
Suman Anna75288cc2014-09-10 14:20:59 -0500802 fifo->msg = MAILBOX_MESSAGE(finfo->rx_id);
803 fifo->msg_stat = MAILBOX_MSGSTATUS(finfo->rx_id);
804 fifo->intr_bit = MAILBOX_IRQ_NEWMSG(finfo->rx_id);
805 fifo->irqenable = MAILBOX_IRQENABLE(intr_type, finfo->rx_usr);
806 fifo->irqstatus = MAILBOX_IRQSTATUS(intr_type, finfo->rx_usr);
807 fifo->irqdisable = MAILBOX_IRQDISABLE(intr_type, finfo->rx_usr);
Suman Annabe3322e2014-06-24 19:43:42 -0500808
Dave Gerlach8e3c5952015-09-22 19:14:52 -0500809 mbox->send_no_irq = finfo->send_no_irq;
Suman Annabe3322e2014-06-24 19:43:42 -0500810 mbox->intr_type = intr_type;
811
Suman Anna72c1c812014-06-24 19:43:43 -0500812 mbox->parent = mdev;
Suman Anna75288cc2014-09-10 14:20:59 -0500813 mbox->name = finfo->name;
814 mbox->irq = platform_get_irq(pdev, finfo->tx_irq);
Suman Anna5040f532014-06-24 19:43:41 -0500815 if (mbox->irq < 0)
816 return mbox->irq;
Suman Anna8841a662014-11-03 17:05:50 -0600817 mbox->chan = &chnls[i];
818 chnls[i].con_priv = mbox;
Suman Anna5040f532014-06-24 19:43:41 -0500819 list[i] = mbox++;
820 }
821
Suman Anna72c1c812014-06-24 19:43:43 -0500822 mutex_init(&mdev->cfg_lock);
823 mdev->dev = &pdev->dev;
Suman Anna75288cc2014-09-10 14:20:59 -0500824 mdev->num_users = num_users;
825 mdev->num_fifos = num_fifos;
Suman Anna72c1c812014-06-24 19:43:43 -0500826 mdev->mboxes = list;
Suman Anna8841a662014-11-03 17:05:50 -0600827
828 /* OMAP does not have a Tx-Done IRQ, but rather a Tx-Ready IRQ */
829 mdev->controller.txdone_irq = true;
830 mdev->controller.dev = mdev->dev;
831 mdev->controller.ops = &omap_mbox_chan_ops;
832 mdev->controller.chans = chnls;
833 mdev->controller.num_chans = info_count;
834 mdev->controller.of_xlate = omap_mbox_of_xlate;
Suman Anna72c1c812014-06-24 19:43:43 -0500835 ret = omap_mbox_register(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500836 if (ret)
837 return ret;
838
Suman Anna72c1c812014-06-24 19:43:43 -0500839 platform_set_drvdata(pdev, mdev);
840 pm_runtime_enable(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500841
Suman Anna72c1c812014-06-24 19:43:43 -0500842 ret = pm_runtime_get_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500843 if (ret < 0) {
Suman Anna72c1c812014-06-24 19:43:43 -0500844 pm_runtime_put_noidle(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500845 goto unregister;
846 }
847
848 /*
849 * just print the raw revision register, the format is not
850 * uniform across all SoCs
851 */
Suman Anna72c1c812014-06-24 19:43:43 -0500852 l = mbox_read_reg(mdev, MAILBOX_REVISION);
853 dev_info(mdev->dev, "omap mailbox rev 0x%x\n", l);
Suman Anna5040f532014-06-24 19:43:41 -0500854
Suman Anna72c1c812014-06-24 19:43:43 -0500855 ret = pm_runtime_put_sync(mdev->dev);
Suman Anna5040f532014-06-24 19:43:41 -0500856 if (ret < 0)
857 goto unregister;
858
Suman Anna75288cc2014-09-10 14:20:59 -0500859 devm_kfree(&pdev->dev, finfoblk);
Suman Anna5040f532014-06-24 19:43:41 -0500860 return 0;
861
862unregister:
Suman Anna72c1c812014-06-24 19:43:43 -0500863 pm_runtime_disable(mdev->dev);
864 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500865 return ret;
866}
867
868static int omap_mbox_remove(struct platform_device *pdev)
869{
Suman Anna72c1c812014-06-24 19:43:43 -0500870 struct omap_mbox_device *mdev = platform_get_drvdata(pdev);
871
872 pm_runtime_disable(mdev->dev);
873 omap_mbox_unregister(mdev);
Suman Anna5040f532014-06-24 19:43:41 -0500874
875 return 0;
876}
877
878static struct platform_driver omap_mbox_driver = {
879 .probe = omap_mbox_probe,
880 .remove = omap_mbox_remove,
881 .driver = {
882 .name = "omap-mailbox",
Suman Anna75288cc2014-09-10 14:20:59 -0500883 .of_match_table = of_match_ptr(omap_mailbox_of_match),
Suman Anna5040f532014-06-24 19:43:41 -0500884 },
885};
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800886
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800887static int __init omap_mbox_init(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800888{
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300889 int err;
890
891 err = class_register(&omap_mbox_class);
892 if (err)
893 return err;
894
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000895 /* kfifo size sanity check: alignment and minimal size */
896 mbox_kfifo_size = ALIGN(mbox_kfifo_size, sizeof(mbox_msg_t));
Kanigeri, Hariab66ac32010-11-29 20:24:12 +0000897 mbox_kfifo_size = max_t(unsigned int, mbox_kfifo_size,
898 sizeof(mbox_msg_t));
Ohad Ben-Cohenb5bebe42010-05-05 15:33:09 +0000899
Suman Anna5040f532014-06-24 19:43:41 -0500900 return platform_driver_register(&omap_mbox_driver);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800901}
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300902subsys_initcall(omap_mbox_init);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800903
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800904static void __exit omap_mbox_exit(void)
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800905{
Suman Anna5040f532014-06-24 19:43:41 -0500906 platform_driver_unregister(&omap_mbox_driver);
Hiroshi DOYU6b233982010-05-18 16:15:32 +0300907 class_unregister(&omap_mbox_class);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800908}
Hiroshi DOYUc7c158e2009-11-22 10:11:19 -0800909module_exit(omap_mbox_exit);
Hiroshi DOYU340a6142006-12-07 15:43:59 -0800910
Hiroshi DOYUf48cca82009-03-23 18:07:24 -0700911MODULE_LICENSE("GPL v2");
912MODULE_DESCRIPTION("omap mailbox: interrupt driven messaging");
Ohad Ben-Cohenf3753252010-05-05 15:33:07 +0000913MODULE_AUTHOR("Toshihiro Kobayashi");
914MODULE_AUTHOR("Hiroshi DOYU");