Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright 2018 NXP
0004  *  Author: Dong Aisheng <aisheng.dong@nxp.com>
0005  *
0006  * Implementation of the SCU IPC functions using MUs (client side).
0007  *
0008  */
0009 
0010 #include <linux/err.h>
0011 #include <linux/firmware/imx/ipc.h>
0012 #include <linux/firmware/imx/sci.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/irq.h>
0015 #include <linux/kernel.h>
0016 #include <linux/mailbox_client.h>
0017 #include <linux/module.h>
0018 #include <linux/mutex.h>
0019 #include <linux/of_platform.h>
0020 #include <linux/platform_device.h>
0021 
0022 #define SCU_MU_CHAN_NUM     8
0023 #define MAX_RX_TIMEOUT      (msecs_to_jiffies(30))
0024 
0025 struct imx_sc_chan {
0026     struct imx_sc_ipc *sc_ipc;
0027 
0028     struct mbox_client cl;
0029     struct mbox_chan *ch;
0030     int idx;
0031     struct completion tx_done;
0032 };
0033 
0034 struct imx_sc_ipc {
0035     /* SCU uses 4 Tx and 4 Rx channels */
0036     struct imx_sc_chan chans[SCU_MU_CHAN_NUM];
0037     struct device *dev;
0038     struct mutex lock;
0039     struct completion done;
0040     bool fast_ipc;
0041 
0042     /* temporarily store the SCU msg */
0043     u32 *msg;
0044     u8 rx_size;
0045     u8 count;
0046 };
0047 
0048 /*
0049  * This type is used to indicate error response for most functions.
0050  */
0051 enum imx_sc_error_codes {
0052     IMX_SC_ERR_NONE = 0,    /* Success */
0053     IMX_SC_ERR_VERSION = 1, /* Incompatible API version */
0054     IMX_SC_ERR_CONFIG = 2,  /* Configuration error */
0055     IMX_SC_ERR_PARM = 3,    /* Bad parameter */
0056     IMX_SC_ERR_NOACCESS = 4,    /* Permission error (no access) */
0057     IMX_SC_ERR_LOCKED = 5,  /* Permission error (locked) */
0058     IMX_SC_ERR_UNAVAILABLE = 6, /* Unavailable (out of resources) */
0059     IMX_SC_ERR_NOTFOUND = 7,    /* Not found */
0060     IMX_SC_ERR_NOPOWER = 8, /* No power */
0061     IMX_SC_ERR_IPC = 9,     /* Generic IPC error */
0062     IMX_SC_ERR_BUSY = 10,   /* Resource is currently busy/active */
0063     IMX_SC_ERR_FAIL = 11,   /* General I/O failure */
0064     IMX_SC_ERR_LAST
0065 };
0066 
0067 static int imx_sc_linux_errmap[IMX_SC_ERR_LAST] = {
0068     0,   /* IMX_SC_ERR_NONE */
0069     -EINVAL, /* IMX_SC_ERR_VERSION */
0070     -EINVAL, /* IMX_SC_ERR_CONFIG */
0071     -EINVAL, /* IMX_SC_ERR_PARM */
0072     -EACCES, /* IMX_SC_ERR_NOACCESS */
0073     -EACCES, /* IMX_SC_ERR_LOCKED */
0074     -ERANGE, /* IMX_SC_ERR_UNAVAILABLE */
0075     -EEXIST, /* IMX_SC_ERR_NOTFOUND */
0076     -EPERM,  /* IMX_SC_ERR_NOPOWER */
0077     -EPIPE,  /* IMX_SC_ERR_IPC */
0078     -EBUSY,  /* IMX_SC_ERR_BUSY */
0079     -EIO,    /* IMX_SC_ERR_FAIL */
0080 };
0081 
0082 static struct imx_sc_ipc *imx_sc_ipc_handle;
0083 
0084 static inline int imx_sc_to_linux_errno(int errno)
0085 {
0086     if (errno >= IMX_SC_ERR_NONE && errno < IMX_SC_ERR_LAST)
0087         return imx_sc_linux_errmap[errno];
0088     return -EIO;
0089 }
0090 
0091 /*
0092  * Get the default handle used by SCU
0093  */
0094 int imx_scu_get_handle(struct imx_sc_ipc **ipc)
0095 {
0096     if (!imx_sc_ipc_handle)
0097         return -EPROBE_DEFER;
0098 
0099     *ipc = imx_sc_ipc_handle;
0100     return 0;
0101 }
0102 EXPORT_SYMBOL(imx_scu_get_handle);
0103 
0104 /* Callback called when the word of a message is ack-ed, eg read by SCU */
0105 static void imx_scu_tx_done(struct mbox_client *cl, void *mssg, int r)
0106 {
0107     struct imx_sc_chan *sc_chan = container_of(cl, struct imx_sc_chan, cl);
0108 
0109     complete(&sc_chan->tx_done);
0110 }
0111 
0112 static void imx_scu_rx_callback(struct mbox_client *c, void *msg)
0113 {
0114     struct imx_sc_chan *sc_chan = container_of(c, struct imx_sc_chan, cl);
0115     struct imx_sc_ipc *sc_ipc = sc_chan->sc_ipc;
0116     struct imx_sc_rpc_msg *hdr;
0117     u32 *data = msg;
0118     int i;
0119 
0120     if (!sc_ipc->msg) {
0121         dev_warn(sc_ipc->dev, "unexpected rx idx %d 0x%08x, ignore!\n",
0122                 sc_chan->idx, *data);
0123         return;
0124     }
0125 
0126     if (sc_ipc->fast_ipc) {
0127         hdr = msg;
0128         sc_ipc->rx_size = hdr->size;
0129         sc_ipc->msg[0] = *data++;
0130 
0131         for (i = 1; i < sc_ipc->rx_size; i++)
0132             sc_ipc->msg[i] = *data++;
0133 
0134         complete(&sc_ipc->done);
0135 
0136         return;
0137     }
0138 
0139     if (sc_chan->idx == 0) {
0140         hdr = msg;
0141         sc_ipc->rx_size = hdr->size;
0142         dev_dbg(sc_ipc->dev, "msg rx size %u\n", sc_ipc->rx_size);
0143         if (sc_ipc->rx_size > 4)
0144             dev_warn(sc_ipc->dev, "RPC does not support receiving over 4 words: %u\n",
0145                  sc_ipc->rx_size);
0146     }
0147 
0148     sc_ipc->msg[sc_chan->idx] = *data;
0149     sc_ipc->count++;
0150 
0151     dev_dbg(sc_ipc->dev, "mu %u msg %u 0x%x\n", sc_chan->idx,
0152         sc_ipc->count, *data);
0153 
0154     if ((sc_ipc->rx_size != 0) && (sc_ipc->count == sc_ipc->rx_size))
0155         complete(&sc_ipc->done);
0156 }
0157 
0158 static int imx_scu_ipc_write(struct imx_sc_ipc *sc_ipc, void *msg)
0159 {
0160     struct imx_sc_rpc_msg hdr = *(struct imx_sc_rpc_msg *)msg;
0161     struct imx_sc_chan *sc_chan;
0162     u32 *data = msg;
0163     int ret;
0164     int size;
0165     int i;
0166 
0167     /* Check size */
0168     if (hdr.size > IMX_SC_RPC_MAX_MSG)
0169         return -EINVAL;
0170 
0171     dev_dbg(sc_ipc->dev, "RPC SVC %u FUNC %u SIZE %u\n", hdr.svc,
0172         hdr.func, hdr.size);
0173 
0174     size = sc_ipc->fast_ipc ? 1 : hdr.size;
0175     for (i = 0; i < size; i++) {
0176         sc_chan = &sc_ipc->chans[i % 4];
0177 
0178         /*
0179          * SCU requires that all messages words are written
0180          * sequentially but linux MU driver implements multiple
0181          * independent channels for each register so ordering between
0182          * different channels must be ensured by SCU API interface.
0183          *
0184          * Wait for tx_done before every send to ensure that no
0185          * queueing happens at the mailbox channel level.
0186          */
0187         if (!sc_ipc->fast_ipc) {
0188             wait_for_completion(&sc_chan->tx_done);
0189             reinit_completion(&sc_chan->tx_done);
0190         }
0191 
0192         ret = mbox_send_message(sc_chan->ch, &data[i]);
0193         if (ret < 0)
0194             return ret;
0195     }
0196 
0197     return 0;
0198 }
0199 
0200 /*
0201  * RPC command/response
0202  */
0203 int imx_scu_call_rpc(struct imx_sc_ipc *sc_ipc, void *msg, bool have_resp)
0204 {
0205     uint8_t saved_svc, saved_func;
0206     struct imx_sc_rpc_msg *hdr;
0207     int ret;
0208 
0209     if (WARN_ON(!sc_ipc || !msg))
0210         return -EINVAL;
0211 
0212     mutex_lock(&sc_ipc->lock);
0213     reinit_completion(&sc_ipc->done);
0214 
0215     if (have_resp) {
0216         sc_ipc->msg = msg;
0217         saved_svc = ((struct imx_sc_rpc_msg *)msg)->svc;
0218         saved_func = ((struct imx_sc_rpc_msg *)msg)->func;
0219     }
0220     sc_ipc->count = 0;
0221     ret = imx_scu_ipc_write(sc_ipc, msg);
0222     if (ret < 0) {
0223         dev_err(sc_ipc->dev, "RPC send msg failed: %d\n", ret);
0224         goto out;
0225     }
0226 
0227     if (have_resp) {
0228         if (!wait_for_completion_timeout(&sc_ipc->done,
0229                          MAX_RX_TIMEOUT)) {
0230             dev_err(sc_ipc->dev, "RPC send msg timeout\n");
0231             mutex_unlock(&sc_ipc->lock);
0232             return -ETIMEDOUT;
0233         }
0234 
0235         /* response status is stored in hdr->func field */
0236         hdr = msg;
0237         ret = hdr->func;
0238         /*
0239          * Some special SCU firmware APIs do NOT have return value
0240          * in hdr->func, but they do have response data, those special
0241          * APIs are defined as void function in SCU firmware, so they
0242          * should be treated as return success always.
0243          */
0244         if ((saved_svc == IMX_SC_RPC_SVC_MISC) &&
0245             (saved_func == IMX_SC_MISC_FUNC_UNIQUE_ID ||
0246              saved_func == IMX_SC_MISC_FUNC_GET_BUTTON_STATUS))
0247             ret = 0;
0248     }
0249 
0250 out:
0251     sc_ipc->msg = NULL;
0252     mutex_unlock(&sc_ipc->lock);
0253 
0254     dev_dbg(sc_ipc->dev, "RPC SVC done\n");
0255 
0256     return imx_sc_to_linux_errno(ret);
0257 }
0258 EXPORT_SYMBOL(imx_scu_call_rpc);
0259 
0260 static int imx_scu_probe(struct platform_device *pdev)
0261 {
0262     struct device *dev = &pdev->dev;
0263     struct imx_sc_ipc *sc_ipc;
0264     struct imx_sc_chan *sc_chan;
0265     struct mbox_client *cl;
0266     char *chan_name;
0267     struct of_phandle_args args;
0268     int num_channel;
0269     int ret;
0270     int i;
0271 
0272     sc_ipc = devm_kzalloc(dev, sizeof(*sc_ipc), GFP_KERNEL);
0273     if (!sc_ipc)
0274         return -ENOMEM;
0275 
0276     ret = of_parse_phandle_with_args(pdev->dev.of_node, "mboxes",
0277                      "#mbox-cells", 0, &args);
0278     if (ret)
0279         return ret;
0280 
0281     sc_ipc->fast_ipc = of_device_is_compatible(args.np, "fsl,imx8-mu-scu");
0282 
0283     num_channel = sc_ipc->fast_ipc ? 2 : SCU_MU_CHAN_NUM;
0284     for (i = 0; i < num_channel; i++) {
0285         if (i < num_channel / 2)
0286             chan_name = kasprintf(GFP_KERNEL, "tx%d", i);
0287         else
0288             chan_name = kasprintf(GFP_KERNEL, "rx%d",
0289                           i - num_channel / 2);
0290 
0291         if (!chan_name)
0292             return -ENOMEM;
0293 
0294         sc_chan = &sc_ipc->chans[i];
0295         cl = &sc_chan->cl;
0296         cl->dev = dev;
0297         cl->tx_block = false;
0298         cl->knows_txdone = true;
0299         cl->rx_callback = imx_scu_rx_callback;
0300 
0301         if (!sc_ipc->fast_ipc) {
0302             /* Initial tx_done completion as "done" */
0303             cl->tx_done = imx_scu_tx_done;
0304             init_completion(&sc_chan->tx_done);
0305             complete(&sc_chan->tx_done);
0306         }
0307 
0308         sc_chan->sc_ipc = sc_ipc;
0309         sc_chan->idx = i % (num_channel / 2);
0310         sc_chan->ch = mbox_request_channel_byname(cl, chan_name);
0311         if (IS_ERR(sc_chan->ch)) {
0312             ret = PTR_ERR(sc_chan->ch);
0313             if (ret != -EPROBE_DEFER)
0314                 dev_err(dev, "Failed to request mbox chan %s ret %d\n",
0315                     chan_name, ret);
0316             kfree(chan_name);
0317             return ret;
0318         }
0319 
0320         dev_dbg(dev, "request mbox chan %s\n", chan_name);
0321         /* chan_name is not used anymore by framework */
0322         kfree(chan_name);
0323     }
0324 
0325     sc_ipc->dev = dev;
0326     mutex_init(&sc_ipc->lock);
0327     init_completion(&sc_ipc->done);
0328 
0329     imx_sc_ipc_handle = sc_ipc;
0330 
0331     ret = imx_scu_soc_init(dev);
0332     if (ret)
0333         dev_warn(dev, "failed to initialize SoC info: %d\n", ret);
0334 
0335     ret = imx_scu_enable_general_irq_channel(dev);
0336     if (ret)
0337         dev_warn(dev,
0338             "failed to enable general irq channel: %d\n", ret);
0339 
0340     dev_info(dev, "NXP i.MX SCU Initialized\n");
0341 
0342     return devm_of_platform_populate(dev);
0343 }
0344 
0345 static const struct of_device_id imx_scu_match[] = {
0346     { .compatible = "fsl,imx-scu", },
0347     { /* Sentinel */ }
0348 };
0349 
0350 static struct platform_driver imx_scu_driver = {
0351     .driver = {
0352         .name = "imx-scu",
0353         .of_match_table = imx_scu_match,
0354     },
0355     .probe = imx_scu_probe,
0356 };
0357 builtin_platform_driver(imx_scu_driver);
0358 
0359 MODULE_AUTHOR("Dong Aisheng <aisheng.dong@nxp.com>");
0360 MODULE_DESCRIPTION("IMX SCU firmware protocol driver");
0361 MODULE_LICENSE("GPL v2");