1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2019,2023 NXP
5 * Implementation of the SCU IRQ functions using MU.
9 #include <dt-bindings/firmware/imx/rsrc.h>
10 #include <linux/firmware/imx/ipc.h>
11 #include <linux/firmware/imx/sci.h>
12 #include <linux/kobject.h>
13 #include <linux/mailbox_client.h>
15 #include <linux/suspend.h>
16 #include <linux/sysfs.h>
18 #define IMX_SC_IRQ_FUNC_ENABLE 1
19 #define IMX_SC_IRQ_FUNC_STATUS 2
20 #define IMX_SC_IRQ_NUM_GROUP 9
22 static u32 mu_resource_id
;
24 struct imx_sc_msg_irq_get_status
{
25 struct imx_sc_rpc_msg hdr
;
38 struct imx_sc_msg_irq_enable
{
39 struct imx_sc_rpc_msg hdr
;
53 static struct kobject
*wakeup_obj
;
54 static ssize_t
wakeup_source_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
);
55 static struct kobj_attribute wakeup_source_attr
=
56 __ATTR(wakeup_src
, 0660, wakeup_source_show
, NULL
);
58 static struct scu_wakeup scu_irq_wakeup
[IMX_SC_IRQ_NUM_GROUP
];
60 static struct imx_sc_ipc
*imx_sc_irq_ipc_handle
;
61 static struct work_struct imx_sc_irq_work
;
62 static BLOCKING_NOTIFIER_HEAD(imx_scu_irq_notifier_chain
);
64 int imx_scu_irq_register_notifier(struct notifier_block
*nb
)
66 return blocking_notifier_chain_register(
67 &imx_scu_irq_notifier_chain
, nb
);
69 EXPORT_SYMBOL(imx_scu_irq_register_notifier
);
71 int imx_scu_irq_unregister_notifier(struct notifier_block
*nb
)
73 return blocking_notifier_chain_unregister(
74 &imx_scu_irq_notifier_chain
, nb
);
76 EXPORT_SYMBOL(imx_scu_irq_unregister_notifier
);
78 static int imx_scu_irq_notifier_call_chain(unsigned long status
, u8
*group
)
80 return blocking_notifier_call_chain(&imx_scu_irq_notifier_chain
,
81 status
, (void *)group
);
84 static void imx_scu_irq_work_handler(struct work_struct
*work
)
90 for (i
= 0; i
< IMX_SC_IRQ_NUM_GROUP
; i
++) {
91 if (scu_irq_wakeup
[i
].mask
) {
92 scu_irq_wakeup
[i
].valid
= false;
93 scu_irq_wakeup
[i
].wakeup_src
= 0;
96 ret
= imx_scu_irq_get_status(i
, &irq_status
);
98 pr_err("get irq group %d status failed, ret %d\n",
105 if (scu_irq_wakeup
[i
].mask
& irq_status
) {
106 scu_irq_wakeup
[i
].valid
= true;
107 scu_irq_wakeup
[i
].wakeup_src
= irq_status
& scu_irq_wakeup
[i
].mask
;
109 scu_irq_wakeup
[i
].wakeup_src
= irq_status
;
113 imx_scu_irq_notifier_call_chain(irq_status
, &i
);
117 int imx_scu_irq_get_status(u8 group
, u32
*irq_status
)
119 struct imx_sc_msg_irq_get_status msg
;
120 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
123 hdr
->ver
= IMX_SC_RPC_VERSION
;
124 hdr
->svc
= IMX_SC_RPC_SVC_IRQ
;
125 hdr
->func
= IMX_SC_IRQ_FUNC_STATUS
;
128 msg
.data
.req
.resource
= mu_resource_id
;
129 msg
.data
.req
.group
= group
;
131 ret
= imx_scu_call_rpc(imx_sc_irq_ipc_handle
, &msg
, true);
136 *irq_status
= msg
.data
.resp
.status
;
140 EXPORT_SYMBOL(imx_scu_irq_get_status
);
142 int imx_scu_irq_group_enable(u8 group
, u32 mask
, u8 enable
)
144 struct imx_sc_msg_irq_enable msg
;
145 struct imx_sc_rpc_msg
*hdr
= &msg
.hdr
;
148 if (!imx_sc_irq_ipc_handle
)
149 return -EPROBE_DEFER
;
151 hdr
->ver
= IMX_SC_RPC_VERSION
;
152 hdr
->svc
= IMX_SC_RPC_SVC_IRQ
;
153 hdr
->func
= IMX_SC_IRQ_FUNC_ENABLE
;
156 msg
.resource
= mu_resource_id
;
161 ret
= imx_scu_call_rpc(imx_sc_irq_ipc_handle
, &msg
, true);
163 pr_err("enable irq failed, group %d, mask %d, ret %d\n",
167 scu_irq_wakeup
[group
].mask
|= mask
;
169 scu_irq_wakeup
[group
].mask
&= ~mask
;
173 EXPORT_SYMBOL(imx_scu_irq_group_enable
);
175 static void imx_scu_irq_callback(struct mbox_client
*c
, void *msg
)
177 schedule_work(&imx_sc_irq_work
);
180 static ssize_t
wakeup_source_show(struct kobject
*kobj
, struct kobj_attribute
*attr
, char *buf
)
184 for (i
= 0; i
< IMX_SC_IRQ_NUM_GROUP
; i
++) {
185 if (!scu_irq_wakeup
[i
].wakeup_src
)
188 if (scu_irq_wakeup
[i
].valid
)
189 sprintf(buf
, "Wakeup source group = %d, irq = 0x%x\n",
190 i
, scu_irq_wakeup
[i
].wakeup_src
);
192 sprintf(buf
, "Spurious SCU wakeup, group = %d, irq = 0x%x\n",
193 i
, scu_irq_wakeup
[i
].wakeup_src
);
199 int imx_scu_enable_general_irq_channel(struct device
*dev
)
201 struct of_phandle_args spec
;
202 struct mbox_client
*cl
;
203 struct mbox_chan
*ch
;
206 ret
= imx_scu_get_handle(&imx_sc_irq_ipc_handle
);
210 cl
= devm_kzalloc(dev
, sizeof(*cl
), GFP_KERNEL
);
215 cl
->rx_callback
= imx_scu_irq_callback
;
217 /* SCU general IRQ uses general interrupt channel 3 */
218 ch
= mbox_request_channel_byname(cl
, "gip3");
221 dev_err(dev
, "failed to request mbox chan gip3, ret %d\n", ret
);
226 INIT_WORK(&imx_sc_irq_work
, imx_scu_irq_work_handler
);
228 if (!of_parse_phandle_with_args(dev
->of_node
, "mboxes",
229 "#mbox-cells", 0, &spec
))
230 i
= of_alias_get_id(spec
.np
, "mu");
232 /* use mu1 as general mu irq channel if failed */
236 mu_resource_id
= IMX_SC_R_MU_0A
+ i
;
238 /* Create directory under /sysfs/firmware */
239 wakeup_obj
= kobject_create_and_add("scu_wakeup_source", firmware_kobj
);
245 ret
= sysfs_create_file(wakeup_obj
, &wakeup_source_attr
.attr
);
247 dev_err(dev
, "Cannot create wakeup source src file......\n");
248 kobject_put(wakeup_obj
);
255 mbox_free_channel(ch
);
259 EXPORT_SYMBOL(imx_scu_enable_general_irq_channel
);