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