1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2019 NXP
4  *
5  * Implementation of the SCU IRQ functions using MU.
6  *
7  */
8 
9 #include <dt-bindings/firmware/imx/rsrc.h>
10 #include <linux/firmware/imx/ipc.h>
11 #include <linux/mailbox_client.h>
12 
13 #define IMX_SC_IRQ_FUNC_ENABLE	1
14 #define IMX_SC_IRQ_FUNC_STATUS	2
15 #define IMX_SC_IRQ_NUM_GROUP	4
16 
17 static u32 mu_resource_id;
18 
19 struct imx_sc_msg_irq_get_status {
20 	struct imx_sc_rpc_msg hdr;
21 	union {
22 		struct {
23 			u16 resource;
24 			u8 group;
25 			u8 reserved;
26 		} __packed req;
27 		struct {
28 			u32 status;
29 		} resp;
30 	} data;
31 };
32 
33 struct imx_sc_msg_irq_enable {
34 	struct imx_sc_rpc_msg hdr;
35 	u32 mask;
36 	u16 resource;
37 	u8 group;
38 	u8 enable;
39 } __packed;
40 
41 static struct imx_sc_ipc *imx_sc_irq_ipc_handle;
42 static struct work_struct imx_sc_irq_work;
43 static ATOMIC_NOTIFIER_HEAD(imx_scu_irq_notifier_chain);
44 
45 int imx_scu_irq_register_notifier(struct notifier_block *nb)
46 {
47 	return atomic_notifier_chain_register(
48 		&imx_scu_irq_notifier_chain, nb);
49 }
50 EXPORT_SYMBOL(imx_scu_irq_register_notifier);
51 
52 int imx_scu_irq_unregister_notifier(struct notifier_block *nb)
53 {
54 	return atomic_notifier_chain_unregister(
55 		&imx_scu_irq_notifier_chain, nb);
56 }
57 EXPORT_SYMBOL(imx_scu_irq_unregister_notifier);
58 
59 static int imx_scu_irq_notifier_call_chain(unsigned long status, u8 *group)
60 {
61 	return atomic_notifier_call_chain(&imx_scu_irq_notifier_chain,
62 		status, (void *)group);
63 }
64 
65 static void imx_scu_irq_work_handler(struct work_struct *work)
66 {
67 	struct imx_sc_msg_irq_get_status msg;
68 	struct imx_sc_rpc_msg *hdr = &msg.hdr;
69 	u32 irq_status;
70 	int ret;
71 	u8 i;
72 
73 	for (i = 0; i < IMX_SC_IRQ_NUM_GROUP; i++) {
74 		hdr->ver = IMX_SC_RPC_VERSION;
75 		hdr->svc = IMX_SC_RPC_SVC_IRQ;
76 		hdr->func = IMX_SC_IRQ_FUNC_STATUS;
77 		hdr->size = 2;
78 
79 		msg.data.req.resource = mu_resource_id;
80 		msg.data.req.group = i;
81 
82 		ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
83 		if (ret) {
84 			pr_err("get irq group %d status failed, ret %d\n",
85 			       i, ret);
86 			return;
87 		}
88 
89 		irq_status = msg.data.resp.status;
90 		if (!irq_status)
91 			continue;
92 
93 		imx_scu_irq_notifier_call_chain(irq_status, &i);
94 	}
95 }
96 
97 int imx_scu_irq_group_enable(u8 group, u32 mask, u8 enable)
98 {
99 	struct imx_sc_msg_irq_enable msg;
100 	struct imx_sc_rpc_msg *hdr = &msg.hdr;
101 	int ret;
102 
103 	if (!imx_sc_irq_ipc_handle)
104 		return -EPROBE_DEFER;
105 
106 	hdr->ver = IMX_SC_RPC_VERSION;
107 	hdr->svc = IMX_SC_RPC_SVC_IRQ;
108 	hdr->func = IMX_SC_IRQ_FUNC_ENABLE;
109 	hdr->size = 3;
110 
111 	msg.resource = mu_resource_id;
112 	msg.group = group;
113 	msg.mask = mask;
114 	msg.enable = enable;
115 
116 	ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
117 	if (ret)
118 		pr_err("enable irq failed, group %d, mask %d, ret %d\n",
119 			group, mask, ret);
120 
121 	return ret;
122 }
123 EXPORT_SYMBOL(imx_scu_irq_group_enable);
124 
125 static void imx_scu_irq_callback(struct mbox_client *c, void *msg)
126 {
127 	schedule_work(&imx_sc_irq_work);
128 }
129 
130 int imx_scu_enable_general_irq_channel(struct device *dev)
131 {
132 	struct of_phandle_args spec;
133 	struct mbox_client *cl;
134 	struct mbox_chan *ch;
135 	int ret = 0, i = 0;
136 
137 	ret = imx_scu_get_handle(&imx_sc_irq_ipc_handle);
138 	if (ret)
139 		return ret;
140 
141 	cl = devm_kzalloc(dev, sizeof(*cl), GFP_KERNEL);
142 	if (!cl)
143 		return -ENOMEM;
144 
145 	cl->dev = dev;
146 	cl->rx_callback = imx_scu_irq_callback;
147 
148 	/* SCU general IRQ uses general interrupt channel 3 */
149 	ch = mbox_request_channel_byname(cl, "gip3");
150 	if (IS_ERR(ch)) {
151 		ret = PTR_ERR(ch);
152 		dev_err(dev, "failed to request mbox chan gip3, ret %d\n", ret);
153 		devm_kfree(dev, cl);
154 		return ret;
155 	}
156 
157 	INIT_WORK(&imx_sc_irq_work, imx_scu_irq_work_handler);
158 
159 	if (!of_parse_phandle_with_args(dev->of_node, "mboxes",
160 				       "#mbox-cells", 0, &spec))
161 		i = of_alias_get_id(spec.np, "mu");
162 
163 	/* use mu1 as general mu irq channel if failed */
164 	if (i < 0)
165 		i = 1;
166 
167 	mu_resource_id = IMX_SC_R_MU_0A + i;
168 
169 	return ret;
170 }
171 EXPORT_SYMBOL(imx_scu_enable_general_irq_channel);
172