1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2024, Qualcomm Innovation Center, Inc. All rights reserved.
6 #include <linux/bitops.h>
7 #include <linux/interrupt.h>
10 #include <linux/irqdomain.h>
11 #include <linux/mailbox_controller.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
15 #define APSS_CPUCP_IPC_CHAN_SUPPORTED 3
16 #define APSS_CPUCP_MBOX_CMD_OFF 0x4
19 #define APSS_CPUCP_TX_MBOX_CMD(i) (0x100 + ((i) * 8))
22 #define APSS_CPUCP_RX_MBOX_CMD(i) (0x100 + ((i) * 8))
23 #define APSS_CPUCP_RX_MBOX_MAP 0x4000
24 #define APSS_CPUCP_RX_MBOX_STAT 0x4400
25 #define APSS_CPUCP_RX_MBOX_CLEAR 0x4800
26 #define APSS_CPUCP_RX_MBOX_EN 0x4c00
27 #define APSS_CPUCP_RX_MBOX_CMD_MASK GENMASK_ULL(63, 0)
30 * struct qcom_cpucp_mbox - Holder for the mailbox driver
31 * @chans: The mailbox channel
32 * @mbox: The mailbox controller
33 * @tx_base: Base address of the CPUCP tx registers
34 * @rx_base: Base address of the CPUCP rx registers
36 struct qcom_cpucp_mbox
{
37 struct mbox_chan chans
[APSS_CPUCP_IPC_CHAN_SUPPORTED
];
38 struct mbox_controller mbox
;
39 void __iomem
*tx_base
;
40 void __iomem
*rx_base
;
43 static inline int channel_number(struct mbox_chan
*chan
)
45 return chan
- chan
->mbox
->chans
;
48 static irqreturn_t
qcom_cpucp_mbox_irq_fn(int irq
, void *data
)
50 struct qcom_cpucp_mbox
*cpucp
= data
;
54 status
= readq(cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_STAT
);
56 for_each_set_bit(i
, (unsigned long *)&status
, APSS_CPUCP_IPC_CHAN_SUPPORTED
) {
57 u32 val
= readl(cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_CMD(i
) + APSS_CPUCP_MBOX_CMD_OFF
);
58 struct mbox_chan
*chan
= &cpucp
->chans
[i
];
61 /* Provide mutual exclusion with changes to chan->cl */
62 spin_lock_irqsave(&chan
->lock
, flags
);
64 mbox_chan_received_data(chan
, &val
);
65 writeq(BIT(i
), cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_CLEAR
);
66 spin_unlock_irqrestore(&chan
->lock
, flags
);
72 static int qcom_cpucp_mbox_startup(struct mbox_chan
*chan
)
74 struct qcom_cpucp_mbox
*cpucp
= container_of(chan
->mbox
, struct qcom_cpucp_mbox
, mbox
);
75 unsigned long chan_id
= channel_number(chan
);
78 val
= readq(cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_EN
);
80 writeq(val
, cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_EN
);
85 static void qcom_cpucp_mbox_shutdown(struct mbox_chan
*chan
)
87 struct qcom_cpucp_mbox
*cpucp
= container_of(chan
->mbox
, struct qcom_cpucp_mbox
, mbox
);
88 unsigned long chan_id
= channel_number(chan
);
91 val
= readq(cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_EN
);
93 writeq(val
, cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_EN
);
96 static int qcom_cpucp_mbox_send_data(struct mbox_chan
*chan
, void *data
)
98 struct qcom_cpucp_mbox
*cpucp
= container_of(chan
->mbox
, struct qcom_cpucp_mbox
, mbox
);
99 unsigned long chan_id
= channel_number(chan
);
102 writel(*val
, cpucp
->tx_base
+ APSS_CPUCP_TX_MBOX_CMD(chan_id
) + APSS_CPUCP_MBOX_CMD_OFF
);
107 static const struct mbox_chan_ops qcom_cpucp_mbox_chan_ops
= {
108 .startup
= qcom_cpucp_mbox_startup
,
109 .send_data
= qcom_cpucp_mbox_send_data
,
110 .shutdown
= qcom_cpucp_mbox_shutdown
113 static int qcom_cpucp_mbox_probe(struct platform_device
*pdev
)
115 struct device
*dev
= &pdev
->dev
;
116 struct qcom_cpucp_mbox
*cpucp
;
117 struct mbox_controller
*mbox
;
120 cpucp
= devm_kzalloc(dev
, sizeof(*cpucp
), GFP_KERNEL
);
124 cpucp
->rx_base
= devm_of_iomap(dev
, dev
->of_node
, 0, NULL
);
125 if (IS_ERR(cpucp
->rx_base
))
126 return PTR_ERR(cpucp
->rx_base
);
128 cpucp
->tx_base
= devm_of_iomap(dev
, dev
->of_node
, 1, NULL
);
129 if (IS_ERR(cpucp
->tx_base
))
130 return PTR_ERR(cpucp
->tx_base
);
132 writeq(0, cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_EN
);
133 writeq(0, cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_CLEAR
);
134 writeq(0, cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_MAP
);
136 irq
= platform_get_irq(pdev
, 0);
140 ret
= devm_request_irq(dev
, irq
, qcom_cpucp_mbox_irq_fn
,
141 IRQF_TRIGGER_HIGH
| IRQF_NO_SUSPEND
, "apss_cpucp_mbox", cpucp
);
143 return dev_err_probe(dev
, ret
, "Failed to register irq: %d\n", irq
);
145 writeq(APSS_CPUCP_RX_MBOX_CMD_MASK
, cpucp
->rx_base
+ APSS_CPUCP_RX_MBOX_MAP
);
149 mbox
->num_chans
= APSS_CPUCP_IPC_CHAN_SUPPORTED
;
150 mbox
->chans
= cpucp
->chans
;
151 mbox
->ops
= &qcom_cpucp_mbox_chan_ops
;
153 ret
= devm_mbox_controller_register(dev
, mbox
);
155 return dev_err_probe(dev
, ret
, "Failed to create mailbox\n");
160 static const struct of_device_id qcom_cpucp_mbox_of_match
[] = {
161 { .compatible
= "qcom,x1e80100-cpucp-mbox" },
164 MODULE_DEVICE_TABLE(of
, qcom_cpucp_mbox_of_match
);
166 static struct platform_driver qcom_cpucp_mbox_driver
= {
167 .probe
= qcom_cpucp_mbox_probe
,
169 .name
= "qcom_cpucp_mbox",
170 .of_match_table
= qcom_cpucp_mbox_of_match
,
174 static int __init
qcom_cpucp_mbox_init(void)
176 return platform_driver_register(&qcom_cpucp_mbox_driver
);
178 core_initcall(qcom_cpucp_mbox_init
);
180 static void __exit
qcom_cpucp_mbox_exit(void)
182 platform_driver_unregister(&qcom_cpucp_mbox_driver
);
184 module_exit(qcom_cpucp_mbox_exit
);
186 MODULE_DESCRIPTION("QTI CPUCP MBOX Driver");
187 MODULE_LICENSE("GPL");