1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018 Hisilicon Limited.
3 // Copyright (c) 2017-2018 Linaro Limited.
5 #include <linux/bitops.h>
6 #include <linux/delay.h>
7 #include <linux/device.h>
9 #include <linux/interrupt.h>
11 #include <linux/iopoll.h>
12 #include <linux/mailbox_controller.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
19 #define MBOX_CHAN_MAX 32
24 #define MBOX_BASE(mbox, ch) ((mbox)->base + ((ch) * 0x40))
25 #define MBOX_SRC_REG 0x00
26 #define MBOX_DST_REG 0x04
27 #define MBOX_DCLR_REG 0x08
28 #define MBOX_DSTAT_REG 0x0c
29 #define MBOX_MODE_REG 0x10
30 #define MBOX_IMASK_REG 0x14
31 #define MBOX_ICLR_REG 0x18
32 #define MBOX_SEND_REG 0x1c
33 #define MBOX_DATA_REG 0x20
35 #define MBOX_IPC_LOCK_REG 0xa00
36 #define MBOX_IPC_UNLOCK 0x1acce551
38 #define MBOX_AUTOMATIC_ACK 1
40 #define MBOX_STATE_IDLE BIT(4)
41 #define MBOX_STATE_READY BIT(5)
42 #define MBOX_STATE_ACK BIT(7)
44 #define MBOX_MSG_LEN 8
47 * Hi3660 mailbox channel information
49 * A channel can be used for TX or RX, it can trigger remote
50 * processor interrupt to notify remote processor and can receive
51 * interrupt if has incoming message.
53 * @dst_irq: Interrupt vector for remote processor
54 * @ack_irq: Interrupt vector for local processor
56 struct hi3660_chan_info
{
62 * Hi3660 mailbox controller data
64 * Mailbox controller includes 32 channels and can allocate
65 * channel for message transferring.
67 * @dev: Device to which it is attached
68 * @base: Base address of the register mapping region
69 * @chan: Representation of channels in mailbox controller
70 * @mchan: Representation of channel info
71 * @controller: Representation of a communication channel controller
76 struct mbox_chan chan
[MBOX_CHAN_MAX
];
77 struct hi3660_chan_info mchan
[MBOX_CHAN_MAX
];
78 struct mbox_controller controller
;
81 static struct hi3660_mbox
*to_hi3660_mbox(struct mbox_controller
*mbox
)
83 return container_of(mbox
, struct hi3660_mbox
, controller
);
86 static int hi3660_mbox_check_state(struct mbox_chan
*chan
)
88 unsigned long ch
= (unsigned long)chan
->con_priv
;
89 struct hi3660_mbox
*mbox
= to_hi3660_mbox(chan
->mbox
);
90 struct hi3660_chan_info
*mchan
= &mbox
->mchan
[ch
];
91 void __iomem
*base
= MBOX_BASE(mbox
, ch
);
95 /* Mailbox is ready to use */
96 if (readl(base
+ MBOX_MODE_REG
) & MBOX_STATE_READY
)
99 /* Wait for acknowledge from remote */
100 ret
= readx_poll_timeout_atomic(readl
, base
+ MBOX_MODE_REG
,
101 val
, (val
& MBOX_STATE_ACK
), 1000, 300000);
103 dev_err(mbox
->dev
, "%s: timeout for receiving ack\n", __func__
);
107 /* clear ack state, mailbox will get back to ready state */
108 writel(BIT(mchan
->ack_irq
), base
+ MBOX_ICLR_REG
);
113 static int hi3660_mbox_unlock(struct mbox_chan
*chan
)
115 struct hi3660_mbox
*mbox
= to_hi3660_mbox(chan
->mbox
);
116 unsigned int val
, retry
= 3;
119 writel(MBOX_IPC_UNLOCK
, mbox
->base
+ MBOX_IPC_LOCK_REG
);
121 val
= readl(mbox
->base
+ MBOX_IPC_LOCK_REG
);
129 dev_err(mbox
->dev
, "%s: failed to unlock mailbox\n", __func__
);
131 return (!val
) ? 0 : -ETIMEDOUT
;
134 static int hi3660_mbox_acquire_channel(struct mbox_chan
*chan
)
136 unsigned long ch
= (unsigned long)chan
->con_priv
;
137 struct hi3660_mbox
*mbox
= to_hi3660_mbox(chan
->mbox
);
138 struct hi3660_chan_info
*mchan
= &mbox
->mchan
[ch
];
139 void __iomem
*base
= MBOX_BASE(mbox
, ch
);
140 unsigned int val
, retry
;
142 for (retry
= 10; retry
; retry
--) {
143 /* Check if channel is in idle state */
144 if (readl(base
+ MBOX_MODE_REG
) & MBOX_STATE_IDLE
) {
145 writel(BIT(mchan
->ack_irq
), base
+ MBOX_SRC_REG
);
147 /* Check ack bit has been set successfully */
148 val
= readl(base
+ MBOX_SRC_REG
);
149 if (val
& BIT(mchan
->ack_irq
))
155 dev_err(mbox
->dev
, "%s: failed to acquire channel\n", __func__
);
157 return retry
? 0 : -ETIMEDOUT
;
160 static int hi3660_mbox_startup(struct mbox_chan
*chan
)
164 ret
= hi3660_mbox_unlock(chan
);
168 ret
= hi3660_mbox_acquire_channel(chan
);
175 static int hi3660_mbox_send_data(struct mbox_chan
*chan
, void *msg
)
177 unsigned long ch
= (unsigned long)chan
->con_priv
;
178 struct hi3660_mbox
*mbox
= to_hi3660_mbox(chan
->mbox
);
179 struct hi3660_chan_info
*mchan
= &mbox
->mchan
[ch
];
180 void __iomem
*base
= MBOX_BASE(mbox
, ch
);
185 ret
= hi3660_mbox_check_state(chan
);
189 /* Clear mask for destination interrupt */
190 writel_relaxed(~BIT(mchan
->dst_irq
), base
+ MBOX_IMASK_REG
);
192 /* Config destination for interrupt vector */
193 writel_relaxed(BIT(mchan
->dst_irq
), base
+ MBOX_DST_REG
);
195 /* Automatic acknowledge mode */
196 writel_relaxed(MBOX_AUTOMATIC_ACK
, base
+ MBOX_MODE_REG
);
198 /* Fill message data */
199 for (i
= 0; i
< MBOX_MSG_LEN
; i
++)
200 writel_relaxed(buf
[i
], base
+ MBOX_DATA_REG
+ i
* 4);
202 /* Trigger data transferring */
203 writel(BIT(mchan
->ack_irq
), base
+ MBOX_SEND_REG
);
207 static const struct mbox_chan_ops hi3660_mbox_ops
= {
208 .startup
= hi3660_mbox_startup
,
209 .send_data
= hi3660_mbox_send_data
,
212 static struct mbox_chan
*hi3660_mbox_xlate(struct mbox_controller
*controller
,
213 const struct of_phandle_args
*spec
)
215 struct hi3660_mbox
*mbox
= to_hi3660_mbox(controller
);
216 struct hi3660_chan_info
*mchan
;
217 unsigned int ch
= spec
->args
[0];
219 if (ch
>= MBOX_CHAN_MAX
) {
220 dev_err(mbox
->dev
, "Invalid channel idx %d\n", ch
);
221 return ERR_PTR(-EINVAL
);
224 mchan
= &mbox
->mchan
[ch
];
225 mchan
->dst_irq
= spec
->args
[1];
226 mchan
->ack_irq
= spec
->args
[2];
228 return &mbox
->chan
[ch
];
231 static const struct of_device_id hi3660_mbox_of_match
[] = {
232 { .compatible
= "hisilicon,hi3660-mbox", },
236 MODULE_DEVICE_TABLE(of
, hi3660_mbox_of_match
);
238 static int hi3660_mbox_probe(struct platform_device
*pdev
)
240 struct device
*dev
= &pdev
->dev
;
241 struct hi3660_mbox
*mbox
;
242 struct mbox_chan
*chan
;
243 struct resource
*res
;
247 mbox
= devm_kzalloc(dev
, sizeof(*mbox
), GFP_KERNEL
);
251 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
252 mbox
->base
= devm_ioremap_resource(dev
, res
);
253 if (IS_ERR(mbox
->base
))
254 return PTR_ERR(mbox
->base
);
257 mbox
->controller
.dev
= dev
;
258 mbox
->controller
.chans
= mbox
->chan
;
259 mbox
->controller
.num_chans
= MBOX_CHAN_MAX
;
260 mbox
->controller
.ops
= &hi3660_mbox_ops
;
261 mbox
->controller
.of_xlate
= hi3660_mbox_xlate
;
263 /* Initialize mailbox channel data */
265 for (ch
= 0; ch
< MBOX_CHAN_MAX
; ch
++)
266 chan
[ch
].con_priv
= (void *)ch
;
268 err
= devm_mbox_controller_register(dev
, &mbox
->controller
);
270 dev_err(dev
, "Failed to register mailbox %d\n", err
);
274 platform_set_drvdata(pdev
, mbox
);
275 dev_info(dev
, "Mailbox enabled\n");
279 static struct platform_driver hi3660_mbox_driver
= {
280 .probe
= hi3660_mbox_probe
,
282 .name
= "hi3660-mbox",
283 .of_match_table
= hi3660_mbox_of_match
,
287 static int __init
hi3660_mbox_init(void)
289 return platform_driver_register(&hi3660_mbox_driver
);
291 core_initcall(hi3660_mbox_init
);
293 static void __exit
hi3660_mbox_exit(void)
295 platform_driver_unregister(&hi3660_mbox_driver
);
297 module_exit(hi3660_mbox_exit
);
299 MODULE_LICENSE("GPL");
300 MODULE_DESCRIPTION("Hisilicon Hi3660 Mailbox Controller");
301 MODULE_AUTHOR("Leo Yan <leo.yan@linaro.org>");