1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip PolarFire SoC (MPFS) system controller/mailbox controller driver
5 * Copyright (c) 2020 Microchip Corporation. All rights reserved.
7 * Author: Conor Dooley <conor.dooley@microchip.com>
12 #include <linux/err.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/mailbox_controller.h>
19 #include <soc/microchip/mpfs.h>
21 #define SERVICES_CR_OFFSET 0x50u
22 #define SERVICES_SR_OFFSET 0x54u
23 #define MAILBOX_REG_OFFSET 0x800u
24 #define MSS_SYS_MAILBOX_DATA_OFFSET 0u
25 #define SCB_MASK_WIDTH 16u
27 /* SCBCTRL service control register */
29 #define SCB_CTRL_REQ (0)
30 #define SCB_CTRL_REQ_MASK BIT(SCB_CTRL_REQ)
32 #define SCB_CTRL_BUSY (1)
33 #define SCB_CTRL_BUSY_MASK BIT(SCB_CTRL_BUSY)
35 #define SCB_CTRL_ABORT (2)
36 #define SCB_CTRL_ABORT_MASK BIT(SCB_CTRL_ABORT)
38 #define SCB_CTRL_NOTIFY (3)
39 #define SCB_CTRL_NOTIFY_MASK BIT(SCB_CTRL_NOTIFY)
41 #define SCB_CTRL_POS (16)
42 #define SCB_CTRL_MASK GENMASK_ULL(SCB_CTRL_POS + SCB_MASK_WIDTH, SCB_CTRL_POS)
44 /* SCBCTRL service status register */
46 #define SCB_STATUS_REQ (0)
47 #define SCB_STATUS_REQ_MASK BIT(SCB_STATUS_REQ)
49 #define SCB_STATUS_BUSY (1)
50 #define SCB_STATUS_BUSY_MASK BIT(SCB_STATUS_BUSY)
52 #define SCB_STATUS_ABORT (2)
53 #define SCB_STATUS_ABORT_MASK BIT(SCB_STATUS_ABORT)
55 #define SCB_STATUS_NOTIFY (3)
56 #define SCB_STATUS_NOTIFY_MASK BIT(SCB_STATUS_NOTIFY)
58 #define SCB_STATUS_POS (16)
59 #define SCB_STATUS_MASK GENMASK_ULL(SCB_STATUS_POS + SCB_MASK_WIDTH, SCB_STATUS_POS)
62 struct mbox_controller controller;
65 void __iomem *mbox_base;
66 void __iomem *int_reg;
67 struct mbox_chan chans[1];
68 struct mpfs_mss_response *response;
72 static bool mpfs_mbox_busy(struct mpfs_mbox *mbox)
76 status = readl_relaxed(mbox->mbox_base + SERVICES_SR_OFFSET);
78 return status & SCB_STATUS_BUSY_MASK;
81 static int mpfs_mbox_send_data(struct mbox_chan *chan, void *data)
83 struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
84 struct mpfs_mss_msg *msg = data;
89 mbox->response = msg->response;
90 mbox->resp_offset = msg->resp_offset;
92 if (mpfs_mbox_busy(mbox))
95 if (msg->cmd_data_size) {
97 u8 extra_bits = msg->cmd_data_size & 3;
98 u32 *word_buf = (u32 *)msg->cmd_data;
100 for (index = 0; index < (msg->cmd_data_size / 4); index++)
101 writel_relaxed(word_buf[index],
102 mbox->mbox_base + MAILBOX_REG_OFFSET + index * 0x4);
105 u8 byte_off = ALIGN_DOWN(msg->cmd_data_size, 4);
106 u8 *byte_buf = msg->cmd_data + byte_off;
108 val = readl_relaxed(mbox->mbox_base +
109 MAILBOX_REG_OFFSET + index * 0x4);
111 for (i = 0u; i < extra_bits; i++) {
112 val &= ~(0xffu << (i * 8u));
113 val |= (byte_buf[i] << (i * 8u));
117 mbox->mbox_base + MAILBOX_REG_OFFSET + index * 0x4);
121 opt_sel = ((msg->mbox_offset << 7u) | (msg->cmd_opcode & 0x7fu));
122 tx_trigger = (opt_sel << SCB_CTRL_POS) & SCB_CTRL_MASK;
123 tx_trigger |= SCB_CTRL_REQ_MASK | SCB_STATUS_NOTIFY_MASK;
124 writel_relaxed(tx_trigger, mbox->mbox_base + SERVICES_CR_OFFSET);
129 static void mpfs_mbox_rx_data(struct mbox_chan *chan)
131 struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
132 struct mpfs_mss_response *response = mbox->response;
133 u16 num_words = ALIGN((response->resp_size), (4)) / 4U;
136 if (!response->resp_msg) {
137 dev_err(mbox->dev, "failed to assign memory for response %d\n", -ENOMEM);
141 if (!mpfs_mbox_busy(mbox)) {
142 for (i = 0; i < num_words; i++) {
143 response->resp_msg[i] =
144 readl_relaxed(mbox->mbox_base + MAILBOX_REG_OFFSET
145 + mbox->resp_offset + i * 0x4);
149 mbox_chan_received_data(chan, response);
152 static irqreturn_t mpfs_mbox_inbox_isr(int irq, void *data)
154 struct mbox_chan *chan = data;
155 struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
157 writel_relaxed(0, mbox->int_reg);
159 mpfs_mbox_rx_data(chan);
161 mbox_chan_txdone(chan, 0);
165 static int mpfs_mbox_startup(struct mbox_chan *chan)
167 struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
173 ret = devm_request_irq(mbox->dev, mbox->irq, mpfs_mbox_inbox_isr, 0, "mpfs-mailbox", chan);
175 dev_err(mbox->dev, "failed to register mailbox interrupt:%d\n", ret);
180 static void mpfs_mbox_shutdown(struct mbox_chan *chan)
182 struct mpfs_mbox *mbox = (struct mpfs_mbox *)chan->con_priv;
184 devm_free_irq(mbox->dev, mbox->irq, chan);
187 static const struct mbox_chan_ops mpfs_mbox_ops = {
188 .send_data = mpfs_mbox_send_data,
189 .startup = mpfs_mbox_startup,
190 .shutdown = mpfs_mbox_shutdown,
193 static int mpfs_mbox_probe(struct platform_device *pdev)
195 struct mpfs_mbox *mbox;
196 struct resource *regs;
199 mbox = devm_kzalloc(&pdev->dev, sizeof(*mbox), GFP_KERNEL);
203 mbox->mbox_base = devm_platform_get_and_ioremap_resource(pdev, 0, ®s);
204 if (IS_ERR(mbox->mbox_base))
205 return PTR_ERR(mbox->mbox_base);
207 mbox->int_reg = devm_platform_get_and_ioremap_resource(pdev, 1, ®s);
208 if (IS_ERR(mbox->int_reg))
209 return PTR_ERR(mbox->int_reg);
211 mbox->irq = platform_get_irq(pdev, 0);
215 mbox->dev = &pdev->dev;
217 mbox->chans[0].con_priv = mbox;
218 mbox->controller.dev = mbox->dev;
219 mbox->controller.num_chans = 1;
220 mbox->controller.chans = mbox->chans;
221 mbox->controller.ops = &mpfs_mbox_ops;
222 mbox->controller.txdone_irq = true;
224 ret = devm_mbox_controller_register(&pdev->dev, &mbox->controller);
226 dev_err(&pdev->dev, "Registering MPFS mailbox controller failed\n");
229 dev_info(&pdev->dev, "Registered MPFS mailbox controller driver\n");
234 static const struct of_device_id mpfs_mbox_of_match[] = {
235 {.compatible = "microchip,mpfs-mailbox", },
238 MODULE_DEVICE_TABLE(of, mpfs_mbox_of_match);
240 static struct platform_driver mpfs_mbox_driver = {
242 .name = "mpfs-mailbox",
243 .of_match_table = mpfs_mbox_of_match,
245 .probe = mpfs_mbox_probe,
247 module_platform_driver(mpfs_mbox_driver);
249 MODULE_LICENSE("GPL v2");
250 MODULE_AUTHOR("Conor Dooley <conor.dooley@microchip.com>");
251 MODULE_DESCRIPTION("MPFS mailbox controller driver");