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/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/mailbox_controller.h>
22 #define INTR_SET_OFS 0x0
23 #define INTR_STAT_OFS 0x4
24 #define INTR_CLR_OFS 0x8
26 #define MHU_SEC_OFFSET 0x0
27 #define MHU_LP_OFFSET 0xc
28 #define MHU_HP_OFFSET 0x18
29 #define TX_REG_OFFSET 0x24
33 struct platform_mhu_link
{
41 struct platform_mhu_link mlink
[MHU_CHANS
];
42 struct mbox_chan chan
[MHU_CHANS
];
43 struct mbox_controller mbox
;
46 static irqreturn_t
platform_mhu_rx_interrupt(int irq
, void *p
)
48 struct mbox_chan
*chan
= p
;
49 struct platform_mhu_link
*mlink
= chan
->con_priv
;
52 val
= readl_relaxed(mlink
->rx_reg
+ INTR_STAT_OFS
);
56 mbox_chan_received_data(chan
, (void *)&val
);
58 writel_relaxed(val
, mlink
->rx_reg
+ INTR_CLR_OFS
);
63 static bool platform_mhu_last_tx_done(struct mbox_chan
*chan
)
65 struct platform_mhu_link
*mlink
= chan
->con_priv
;
66 u32 val
= readl_relaxed(mlink
->tx_reg
+ INTR_STAT_OFS
);
71 static int platform_mhu_send_data(struct mbox_chan
*chan
, void *data
)
73 struct platform_mhu_link
*mlink
= chan
->con_priv
;
76 writel_relaxed(*arg
, mlink
->tx_reg
+ INTR_SET_OFS
);
81 static int platform_mhu_startup(struct mbox_chan
*chan
)
83 struct platform_mhu_link
*mlink
= chan
->con_priv
;
87 val
= readl_relaxed(mlink
->tx_reg
+ INTR_STAT_OFS
);
88 writel_relaxed(val
, mlink
->tx_reg
+ INTR_CLR_OFS
);
90 ret
= request_irq(mlink
->irq
, platform_mhu_rx_interrupt
,
91 IRQF_SHARED
, "platform_mhu_link", chan
);
93 dev_err(chan
->mbox
->dev
,
94 "Unable to acquire IRQ %d\n", mlink
->irq
);
101 static void platform_mhu_shutdown(struct mbox_chan
*chan
)
103 struct platform_mhu_link
*mlink
= chan
->con_priv
;
105 free_irq(mlink
->irq
, chan
);
108 static const struct mbox_chan_ops platform_mhu_ops
= {
109 .send_data
= platform_mhu_send_data
,
110 .startup
= platform_mhu_startup
,
111 .shutdown
= platform_mhu_shutdown
,
112 .last_tx_done
= platform_mhu_last_tx_done
,
115 static int platform_mhu_probe(struct platform_device
*pdev
)
118 struct platform_mhu
*mhu
;
119 struct device
*dev
= &pdev
->dev
;
120 struct resource
*res
;
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 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
131 mhu
->base
= devm_ioremap_resource(dev
, res
);
132 if (IS_ERR(mhu
->base
)) {
133 dev_err(dev
, "ioremap failed\n");
134 return PTR_ERR(mhu
->base
);
137 for (i
= 0; i
< MHU_CHANS
; i
++) {
138 mhu
->chan
[i
].con_priv
= &mhu
->mlink
[i
];
139 mhu
->mlink
[i
].irq
= platform_get_irq(pdev
, i
);
140 if (mhu
->mlink
[i
].irq
< 0) {
141 dev_err(dev
, "failed to get irq%d\n", i
);
142 return mhu
->mlink
[i
].irq
;
144 mhu
->mlink
[i
].rx_reg
= mhu
->base
+ platform_mhu_reg
[i
];
145 mhu
->mlink
[i
].tx_reg
= mhu
->mlink
[i
].rx_reg
+ TX_REG_OFFSET
;
149 mhu
->mbox
.chans
= &mhu
->chan
[0];
150 mhu
->mbox
.num_chans
= MHU_CHANS
;
151 mhu
->mbox
.ops
= &platform_mhu_ops
;
152 mhu
->mbox
.txdone_irq
= false;
153 mhu
->mbox
.txdone_poll
= true;
154 mhu
->mbox
.txpoll_period
= 1;
156 platform_set_drvdata(pdev
, mhu
);
158 err
= devm_mbox_controller_register(dev
, &mhu
->mbox
);
160 dev_err(dev
, "Failed to register mailboxes %d\n", err
);
164 dev_info(dev
, "Platform MHU Mailbox registered\n");
168 static const struct of_device_id platform_mhu_dt_ids
[] = {
169 { .compatible
= "amlogic,meson-gxbb-mhu", },
172 MODULE_DEVICE_TABLE(of
, platform_mhu_dt_ids
);
174 static struct platform_driver platform_mhu_driver
= {
175 .probe
= platform_mhu_probe
,
177 .name
= "platform-mhu",
178 .of_match_table
= platform_mhu_dt_ids
,
182 module_platform_driver(platform_mhu_driver
);
184 MODULE_LICENSE("GPL v2");
185 MODULE_ALIAS("platform:platform-mhu");
186 MODULE_DESCRIPTION("Platform MHU Driver");
187 MODULE_AUTHOR("Neil Armstrong <narmstrong@baylibre.com>");