2 * Copyright Altera Corporation (C) 2013-2014. All rights reserved
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program. If not, see <http://www.gnu.org/licenses/>.
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/mailbox_controller.h>
22 #include <linux/module.h>
24 #include <linux/platform_device.h>
26 #define DRIVER_NAME "altera-mailbox"
28 #define MAILBOX_CMD_REG 0x00
29 #define MAILBOX_PTR_REG 0x04
30 #define MAILBOX_STS_REG 0x08
31 #define MAILBOX_INTMASK_REG 0x0C
33 #define INT_PENDING_MSK 0x1
34 #define INT_SPACE_MSK 0x2
36 #define STS_PENDING_MSK 0x1
37 #define STS_FULL_MSK 0x2
38 #define STS_FULL_OFT 0x1
40 #define MBOX_PENDING(status) (((status) & STS_PENDING_MSK))
41 #define MBOX_FULL(status) (((status) & STS_FULL_MSK) >> STS_FULL_OFT)
43 enum altera_mbox_msg
{
48 #define MBOX_POLLING_MS 5 /* polling interval 5ms */
51 bool is_sender
; /* 1-sender, 0-receiver */
54 void __iomem
*mbox_base
;
56 struct mbox_controller controller
;
58 /* If the controller supports only RX polling mode */
59 struct timer_list rxpoll_timer
;
62 static struct altera_mbox
*mbox_chan_to_altera_mbox(struct mbox_chan
*chan
)
64 if (!chan
|| !chan
->con_priv
)
67 return (struct altera_mbox
*)chan
->con_priv
;
70 static inline int altera_mbox_full(struct altera_mbox
*mbox
)
74 status
= readl_relaxed(mbox
->mbox_base
+ MAILBOX_STS_REG
);
75 return MBOX_FULL(status
);
78 static inline int altera_mbox_pending(struct altera_mbox
*mbox
)
82 status
= readl_relaxed(mbox
->mbox_base
+ MAILBOX_STS_REG
);
83 return MBOX_PENDING(status
);
86 static void altera_mbox_rx_intmask(struct altera_mbox
*mbox
, bool enable
)
90 mask
= readl_relaxed(mbox
->mbox_base
+ MAILBOX_INTMASK_REG
);
92 mask
|= INT_PENDING_MSK
;
94 mask
&= ~INT_PENDING_MSK
;
95 writel_relaxed(mask
, mbox
->mbox_base
+ MAILBOX_INTMASK_REG
);
98 static void altera_mbox_tx_intmask(struct altera_mbox
*mbox
, bool enable
)
102 mask
= readl_relaxed(mbox
->mbox_base
+ MAILBOX_INTMASK_REG
);
104 mask
|= INT_SPACE_MSK
;
106 mask
&= ~INT_SPACE_MSK
;
107 writel_relaxed(mask
, mbox
->mbox_base
+ MAILBOX_INTMASK_REG
);
110 static bool altera_mbox_is_sender(struct altera_mbox
*mbox
)
113 /* Write a magic number to PTR register and read back this register.
114 * This register is read-write if it is a sender.
116 #define MBOX_MAGIC 0xA5A5AA55
117 writel_relaxed(MBOX_MAGIC
, mbox
->mbox_base
+ MAILBOX_PTR_REG
);
118 reg
= readl_relaxed(mbox
->mbox_base
+ MAILBOX_PTR_REG
);
119 if (reg
== MBOX_MAGIC
) {
121 writel_relaxed(0, mbox
->mbox_base
+ MAILBOX_PTR_REG
);
127 static void altera_mbox_rx_data(struct mbox_chan
*chan
)
129 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
132 if (altera_mbox_pending(mbox
)) {
134 readl_relaxed(mbox
->mbox_base
+ MAILBOX_PTR_REG
);
136 readl_relaxed(mbox
->mbox_base
+ MAILBOX_CMD_REG
);
137 mbox_chan_received_data(chan
, (void *)data
);
141 static void altera_mbox_poll_rx(unsigned long data
)
143 struct mbox_chan
*chan
= (struct mbox_chan
*)data
;
144 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
146 altera_mbox_rx_data(chan
);
148 mod_timer(&mbox
->rxpoll_timer
,
149 jiffies
+ msecs_to_jiffies(MBOX_POLLING_MS
));
152 static irqreturn_t
altera_mbox_tx_interrupt(int irq
, void *p
)
154 struct mbox_chan
*chan
= (struct mbox_chan
*)p
;
155 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
157 altera_mbox_tx_intmask(mbox
, false);
158 mbox_chan_txdone(chan
, 0);
163 static irqreturn_t
altera_mbox_rx_interrupt(int irq
, void *p
)
165 struct mbox_chan
*chan
= (struct mbox_chan
*)p
;
167 altera_mbox_rx_data(chan
);
171 static int altera_mbox_startup_sender(struct mbox_chan
*chan
)
174 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
176 if (mbox
->intr_mode
) {
177 ret
= request_irq(mbox
->irq
, altera_mbox_tx_interrupt
, 0,
181 "failed to register mailbox interrupt:%d\n",
190 static int altera_mbox_startup_receiver(struct mbox_chan
*chan
)
193 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
195 if (mbox
->intr_mode
) {
196 ret
= request_irq(mbox
->irq
, altera_mbox_rx_interrupt
, 0,
199 mbox
->intr_mode
= false;
200 goto polling
; /* use polling if failed */
203 altera_mbox_rx_intmask(mbox
, true);
208 /* Setup polling timer */
209 setup_timer(&mbox
->rxpoll_timer
, altera_mbox_poll_rx
,
210 (unsigned long)chan
);
211 mod_timer(&mbox
->rxpoll_timer
,
212 jiffies
+ msecs_to_jiffies(MBOX_POLLING_MS
));
217 static int altera_mbox_send_data(struct mbox_chan
*chan
, void *data
)
219 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
220 u32
*udata
= (u32
*)data
;
224 if (!mbox
->is_sender
) {
226 "failed to send. This is receiver mailbox.\n");
230 if (altera_mbox_full(mbox
))
233 /* Enable interrupt before send */
235 altera_mbox_tx_intmask(mbox
, true);
237 /* Pointer register must write before command register */
238 writel_relaxed(udata
[MBOX_PTR
], mbox
->mbox_base
+ MAILBOX_PTR_REG
);
239 writel_relaxed(udata
[MBOX_CMD
], mbox
->mbox_base
+ MAILBOX_CMD_REG
);
244 static bool altera_mbox_last_tx_done(struct mbox_chan
*chan
)
246 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
248 /* Return false if mailbox is full */
249 return altera_mbox_full(mbox
) ? false : true;
252 static bool altera_mbox_peek_data(struct mbox_chan
*chan
)
254 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
256 return altera_mbox_pending(mbox
) ? true : false;
259 static int altera_mbox_startup(struct mbox_chan
*chan
)
261 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
268 ret
= altera_mbox_startup_sender(chan
);
270 ret
= altera_mbox_startup_receiver(chan
);
275 static void altera_mbox_shutdown(struct mbox_chan
*chan
)
277 struct altera_mbox
*mbox
= mbox_chan_to_altera_mbox(chan
);
279 if (mbox
->intr_mode
) {
280 /* Unmask all interrupt masks */
281 writel_relaxed(~0, mbox
->mbox_base
+ MAILBOX_INTMASK_REG
);
282 free_irq(mbox
->irq
, chan
);
283 } else if (!mbox
->is_sender
) {
284 del_timer_sync(&mbox
->rxpoll_timer
);
288 static struct mbox_chan_ops altera_mbox_ops
= {
289 .send_data
= altera_mbox_send_data
,
290 .startup
= altera_mbox_startup
,
291 .shutdown
= altera_mbox_shutdown
,
292 .last_tx_done
= altera_mbox_last_tx_done
,
293 .peek_data
= altera_mbox_peek_data
,
296 static int altera_mbox_probe(struct platform_device
*pdev
)
298 struct altera_mbox
*mbox
;
299 struct resource
*regs
;
300 struct mbox_chan
*chans
;
303 mbox
= devm_kzalloc(&pdev
->dev
, sizeof(*mbox
),
308 /* Allocated one channel */
309 chans
= devm_kzalloc(&pdev
->dev
, sizeof(*chans
), GFP_KERNEL
);
313 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
315 mbox
->mbox_base
= devm_ioremap_resource(&pdev
->dev
, regs
);
316 if (IS_ERR(mbox
->mbox_base
))
317 return PTR_ERR(mbox
->mbox_base
);
319 /* Check is it a sender or receiver? */
320 mbox
->is_sender
= altera_mbox_is_sender(mbox
);
322 mbox
->irq
= platform_get_irq(pdev
, 0);
324 mbox
->intr_mode
= true;
326 mbox
->dev
= &pdev
->dev
;
328 /* Hardware supports only one channel. */
329 chans
[0].con_priv
= mbox
;
330 mbox
->controller
.dev
= mbox
->dev
;
331 mbox
->controller
.num_chans
= 1;
332 mbox
->controller
.chans
= chans
;
333 mbox
->controller
.ops
= &altera_mbox_ops
;
335 if (mbox
->is_sender
) {
336 if (mbox
->intr_mode
) {
337 mbox
->controller
.txdone_irq
= true;
339 mbox
->controller
.txdone_poll
= true;
340 mbox
->controller
.txpoll_period
= MBOX_POLLING_MS
;
344 ret
= mbox_controller_register(&mbox
->controller
);
346 dev_err(&pdev
->dev
, "Register mailbox failed\n");
350 platform_set_drvdata(pdev
, mbox
);
355 static int altera_mbox_remove(struct platform_device
*pdev
)
357 struct altera_mbox
*mbox
= platform_get_drvdata(pdev
);
362 mbox_controller_unregister(&mbox
->controller
);
367 static const struct of_device_id altera_mbox_match
[] = {
368 { .compatible
= "altr,mailbox-1.0" },
372 MODULE_DEVICE_TABLE(of
, altera_mbox_match
);
374 static struct platform_driver altera_mbox_driver
= {
375 .probe
= altera_mbox_probe
,
376 .remove
= altera_mbox_remove
,
379 .of_match_table
= altera_mbox_match
,
383 module_platform_driver(altera_mbox_driver
);
385 MODULE_LICENSE("GPL v2");
386 MODULE_DESCRIPTION("Altera mailbox specific functions");
387 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
388 MODULE_ALIAS("platform:altera-mailbox");