Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright 2019 NXP
0004  *
0005  * Implementation of the SCU IRQ functions using MU.
0006  *
0007  */
0008 
0009 #include <dt-bindings/firmware/imx/rsrc.h>
0010 #include <linux/firmware/imx/ipc.h>
0011 #include <linux/firmware/imx/sci.h>
0012 #include <linux/mailbox_client.h>
0013 #include <linux/suspend.h>
0014 
0015 #define IMX_SC_IRQ_FUNC_ENABLE  1
0016 #define IMX_SC_IRQ_FUNC_STATUS  2
0017 #define IMX_SC_IRQ_NUM_GROUP    4
0018 
0019 static u32 mu_resource_id;
0020 
0021 struct imx_sc_msg_irq_get_status {
0022     struct imx_sc_rpc_msg hdr;
0023     union {
0024         struct {
0025             u16 resource;
0026             u8 group;
0027             u8 reserved;
0028         } __packed req;
0029         struct {
0030             u32 status;
0031         } resp;
0032     } data;
0033 };
0034 
0035 struct imx_sc_msg_irq_enable {
0036     struct imx_sc_rpc_msg hdr;
0037     u32 mask;
0038     u16 resource;
0039     u8 group;
0040     u8 enable;
0041 } __packed;
0042 
0043 static struct imx_sc_ipc *imx_sc_irq_ipc_handle;
0044 static struct work_struct imx_sc_irq_work;
0045 static ATOMIC_NOTIFIER_HEAD(imx_scu_irq_notifier_chain);
0046 
0047 int imx_scu_irq_register_notifier(struct notifier_block *nb)
0048 {
0049     return atomic_notifier_chain_register(
0050         &imx_scu_irq_notifier_chain, nb);
0051 }
0052 EXPORT_SYMBOL(imx_scu_irq_register_notifier);
0053 
0054 int imx_scu_irq_unregister_notifier(struct notifier_block *nb)
0055 {
0056     return atomic_notifier_chain_unregister(
0057         &imx_scu_irq_notifier_chain, nb);
0058 }
0059 EXPORT_SYMBOL(imx_scu_irq_unregister_notifier);
0060 
0061 static int imx_scu_irq_notifier_call_chain(unsigned long status, u8 *group)
0062 {
0063     return atomic_notifier_call_chain(&imx_scu_irq_notifier_chain,
0064         status, (void *)group);
0065 }
0066 
0067 static void imx_scu_irq_work_handler(struct work_struct *work)
0068 {
0069     struct imx_sc_msg_irq_get_status msg;
0070     struct imx_sc_rpc_msg *hdr = &msg.hdr;
0071     u32 irq_status;
0072     int ret;
0073     u8 i;
0074 
0075     for (i = 0; i < IMX_SC_IRQ_NUM_GROUP; i++) {
0076         hdr->ver = IMX_SC_RPC_VERSION;
0077         hdr->svc = IMX_SC_RPC_SVC_IRQ;
0078         hdr->func = IMX_SC_IRQ_FUNC_STATUS;
0079         hdr->size = 2;
0080 
0081         msg.data.req.resource = mu_resource_id;
0082         msg.data.req.group = i;
0083 
0084         ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
0085         if (ret) {
0086             pr_err("get irq group %d status failed, ret %d\n",
0087                    i, ret);
0088             return;
0089         }
0090 
0091         irq_status = msg.data.resp.status;
0092         if (!irq_status)
0093             continue;
0094 
0095         pm_system_wakeup();
0096         imx_scu_irq_notifier_call_chain(irq_status, &i);
0097     }
0098 }
0099 
0100 int imx_scu_irq_group_enable(u8 group, u32 mask, u8 enable)
0101 {
0102     struct imx_sc_msg_irq_enable msg;
0103     struct imx_sc_rpc_msg *hdr = &msg.hdr;
0104     int ret;
0105 
0106     if (!imx_sc_irq_ipc_handle)
0107         return -EPROBE_DEFER;
0108 
0109     hdr->ver = IMX_SC_RPC_VERSION;
0110     hdr->svc = IMX_SC_RPC_SVC_IRQ;
0111     hdr->func = IMX_SC_IRQ_FUNC_ENABLE;
0112     hdr->size = 3;
0113 
0114     msg.resource = mu_resource_id;
0115     msg.group = group;
0116     msg.mask = mask;
0117     msg.enable = enable;
0118 
0119     ret = imx_scu_call_rpc(imx_sc_irq_ipc_handle, &msg, true);
0120     if (ret)
0121         pr_err("enable irq failed, group %d, mask %d, ret %d\n",
0122             group, mask, ret);
0123 
0124     return ret;
0125 }
0126 EXPORT_SYMBOL(imx_scu_irq_group_enable);
0127 
0128 static void imx_scu_irq_callback(struct mbox_client *c, void *msg)
0129 {
0130     schedule_work(&imx_sc_irq_work);
0131 }
0132 
0133 int imx_scu_enable_general_irq_channel(struct device *dev)
0134 {
0135     struct of_phandle_args spec;
0136     struct mbox_client *cl;
0137     struct mbox_chan *ch;
0138     int ret = 0, i = 0;
0139 
0140     ret = imx_scu_get_handle(&imx_sc_irq_ipc_handle);
0141     if (ret)
0142         return ret;
0143 
0144     cl = devm_kzalloc(dev, sizeof(*cl), GFP_KERNEL);
0145     if (!cl)
0146         return -ENOMEM;
0147 
0148     cl->dev = dev;
0149     cl->rx_callback = imx_scu_irq_callback;
0150 
0151     /* SCU general IRQ uses general interrupt channel 3 */
0152     ch = mbox_request_channel_byname(cl, "gip3");
0153     if (IS_ERR(ch)) {
0154         ret = PTR_ERR(ch);
0155         dev_err(dev, "failed to request mbox chan gip3, ret %d\n", ret);
0156         devm_kfree(dev, cl);
0157         return ret;
0158     }
0159 
0160     INIT_WORK(&imx_sc_irq_work, imx_scu_irq_work_handler);
0161 
0162     if (!of_parse_phandle_with_args(dev->of_node, "mboxes",
0163                        "#mbox-cells", 0, &spec))
0164         i = of_alias_get_id(spec.np, "mu");
0165 
0166     /* use mu1 as general mu irq channel if failed */
0167     if (i < 0)
0168         i = 1;
0169 
0170     mu_resource_id = IMX_SC_R_MU_0A + i;
0171 
0172     return ret;
0173 }
0174 EXPORT_SYMBOL(imx_scu_enable_general_irq_channel);