1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2016 BayLibre SAS.
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 * Synchronised with arm_mhu.c from :
6 * Copyright (C) 2013-2015 Fujitsu Semiconductor Ltd.
7 * Copyright (C) 2015 Linaro Ltd.
8 * Author: Jassi Brar <jaswinder.singh@linaro.org>
11 #include <linux/interrupt.h>
12 #include <linux/spinlock.h>
13 #include <linux/mutex.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #include <linux/err.h>
18 #include <linux/mod_devicetable.h>
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/mailbox_controller.h>
23 #define INTR_SET_OFS 0x0
24 #define INTR_STAT_OFS 0x4
25 #define INTR_CLR_OFS 0x8
27 #define MHU_SEC_OFFSET 0x0
28 #define MHU_LP_OFFSET 0xc
29 #define MHU_HP_OFFSET 0x18
30 #define TX_REG_OFFSET 0x24
34 struct platform_mhu_link
{
42 struct platform_mhu_link mlink
[MHU_CHANS
];
43 struct mbox_chan chan
[MHU_CHANS
];
44 struct mbox_controller mbox
;
47 static irqreturn_t
platform_mhu_rx_interrupt(int irq
, void *p
)
49 struct mbox_chan
*chan
= p
;
50 struct platform_mhu_link
*mlink
= chan
->con_priv
;
53 val
= readl_relaxed(mlink
->rx_reg
+ INTR_STAT_OFS
);
57 mbox_chan_received_data(chan
, (void *)&val
);
59 writel_relaxed(val
, mlink
->rx_reg
+ INTR_CLR_OFS
);
64 static bool platform_mhu_last_tx_done(struct mbox_chan
*chan
)
66 struct platform_mhu_link
*mlink
= chan
->con_priv
;
67 u32 val
= readl_relaxed(mlink
->tx_reg
+ INTR_STAT_OFS
);
72 static int platform_mhu_send_data(struct mbox_chan
*chan
, void *data
)
74 struct platform_mhu_link
*mlink
= chan
->con_priv
;
77 writel_relaxed(*arg
, mlink
->tx_reg
+ INTR_SET_OFS
);
82 static int platform_mhu_startup(struct mbox_chan
*chan
)
84 struct platform_mhu_link
*mlink
= chan
->con_priv
;
88 val
= readl_relaxed(mlink
->tx_reg
+ INTR_STAT_OFS
);
89 writel_relaxed(val
, mlink
->tx_reg
+ INTR_CLR_OFS
);
91 ret
= request_irq(mlink
->irq
, platform_mhu_rx_interrupt
,
92 IRQF_SHARED
, "platform_mhu_link", chan
);
94 dev_err(chan
->mbox
->dev
,
95 "Unable to acquire IRQ %d\n", mlink
->irq
);
102 static void platform_mhu_shutdown(struct mbox_chan
*chan
)
104 struct platform_mhu_link
*mlink
= chan
->con_priv
;
106 free_irq(mlink
->irq
, chan
);
109 static const struct mbox_chan_ops platform_mhu_ops
= {
110 .send_data
= platform_mhu_send_data
,
111 .startup
= platform_mhu_startup
,
112 .shutdown
= platform_mhu_shutdown
,
113 .last_tx_done
= platform_mhu_last_tx_done
,
116 static int platform_mhu_probe(struct platform_device
*pdev
)
119 struct platform_mhu
*mhu
;
120 struct device
*dev
= &pdev
->dev
;
121 int platform_mhu_reg
[MHU_CHANS
] = {
122 MHU_SEC_OFFSET
, MHU_LP_OFFSET
, MHU_HP_OFFSET
125 /* Allocate memory for device */
126 mhu
= devm_kzalloc(dev
, sizeof(*mhu
), GFP_KERNEL
);
130 mhu
->base
= devm_platform_ioremap_resource(pdev
, 0);
131 if (IS_ERR(mhu
->base
)) {
132 dev_err(dev
, "ioremap failed\n");
133 return PTR_ERR(mhu
->base
);
136 for (i
= 0; i
< MHU_CHANS
; i
++) {
137 mhu
->chan
[i
].con_priv
= &mhu
->mlink
[i
];
138 mhu
->mlink
[i
].irq
= platform_get_irq(pdev
, i
);
139 if (mhu
->mlink
[i
].irq
< 0)
140 return mhu
->mlink
[i
].irq
;
141 mhu
->mlink
[i
].rx_reg
= mhu
->base
+ platform_mhu_reg
[i
];
142 mhu
->mlink
[i
].tx_reg
= mhu
->mlink
[i
].rx_reg
+ TX_REG_OFFSET
;
146 mhu
->mbox
.chans
= &mhu
->chan
[0];
147 mhu
->mbox
.num_chans
= MHU_CHANS
;
148 mhu
->mbox
.ops
= &platform_mhu_ops
;
149 mhu
->mbox
.txdone_irq
= false;
150 mhu
->mbox
.txdone_poll
= true;
151 mhu
->mbox
.txpoll_period
= 1;
153 platform_set_drvdata(pdev
, mhu
);
155 err
= devm_mbox_controller_register(dev
, &mhu
->mbox
);
157 dev_err(dev
, "Failed to register mailboxes %d\n", err
);
161 dev_info(dev
, "Platform MHU Mailbox registered\n");
165 static const struct of_device_id platform_mhu_dt_ids
[] = {
166 { .compatible
= "amlogic,meson-gxbb-mhu", },
169 MODULE_DEVICE_TABLE(of
, platform_mhu_dt_ids
);
171 static struct platform_driver platform_mhu_driver
= {
172 .probe
= platform_mhu_probe
,
174 .name
= "platform-mhu",
175 .of_match_table
= platform_mhu_dt_ids
,
179 module_platform_driver(platform_mhu_driver
);
181 MODULE_LICENSE("GPL v2");
182 MODULE_ALIAS("platform:platform-mhu");
183 MODULE_DESCRIPTION("Platform MHU Driver");
184 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");