12a226927SNipun Gupta // SPDX-License-Identifier: GPL-2.0
22a226927SNipun Gupta /*
32a226927SNipun Gupta * Platform driver for CDX bus.
42a226927SNipun Gupta *
52a226927SNipun Gupta * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
62a226927SNipun Gupta */
72a226927SNipun Gupta
82a226927SNipun Gupta #include <linux/rpmsg.h>
92a226927SNipun Gupta #include <linux/remoteproc.h>
10*a436194dSRob Herring #include <linux/of.h>
11*a436194dSRob Herring #include <linux/platform_device.h>
122a226927SNipun Gupta #include <linux/cdx/cdx_bus.h>
132a226927SNipun Gupta #include <linux/module.h>
142a226927SNipun Gupta
152a226927SNipun Gupta #include "../cdx.h"
162a226927SNipun Gupta #include "cdx_controller.h"
172a226927SNipun Gupta #include "mcdi_functions.h"
182a226927SNipun Gupta #include "mcdi.h"
192a226927SNipun Gupta
202a226927SNipun Gupta static struct rpmsg_device_id cdx_rpmsg_id_table[] = {
212a226927SNipun Gupta { .name = "mcdi_ipc" },
222a226927SNipun Gupta { },
232a226927SNipun Gupta };
242a226927SNipun Gupta MODULE_DEVICE_TABLE(rpmsg, cdx_rpmsg_id_table);
252a226927SNipun Gupta
cdx_rpmsg_send(struct cdx_mcdi * cdx_mcdi,const struct cdx_dword * hdr,size_t hdr_len,const struct cdx_dword * sdu,size_t sdu_len)262a226927SNipun Gupta int cdx_rpmsg_send(struct cdx_mcdi *cdx_mcdi,
272a226927SNipun Gupta const struct cdx_dword *hdr, size_t hdr_len,
282a226927SNipun Gupta const struct cdx_dword *sdu, size_t sdu_len)
292a226927SNipun Gupta {
302a226927SNipun Gupta unsigned char *send_buf;
312a226927SNipun Gupta int ret;
322a226927SNipun Gupta
332a226927SNipun Gupta send_buf = kzalloc(hdr_len + sdu_len, GFP_KERNEL);
342a226927SNipun Gupta if (!send_buf)
352a226927SNipun Gupta return -ENOMEM;
362a226927SNipun Gupta
372a226927SNipun Gupta memcpy(send_buf, hdr, hdr_len);
382a226927SNipun Gupta memcpy(send_buf + hdr_len, sdu, sdu_len);
392a226927SNipun Gupta
402a226927SNipun Gupta ret = rpmsg_send(cdx_mcdi->ept, send_buf, hdr_len + sdu_len);
412a226927SNipun Gupta kfree(send_buf);
422a226927SNipun Gupta
432a226927SNipun Gupta return ret;
442a226927SNipun Gupta }
452a226927SNipun Gupta
cdx_attach_to_rproc(struct platform_device * pdev)462a226927SNipun Gupta static int cdx_attach_to_rproc(struct platform_device *pdev)
472a226927SNipun Gupta {
482a226927SNipun Gupta struct device_node *r5_core_node;
492a226927SNipun Gupta struct cdx_controller *cdx_c;
502a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi;
512a226927SNipun Gupta struct device *dev;
522a226927SNipun Gupta struct rproc *rp;
532a226927SNipun Gupta int ret;
542a226927SNipun Gupta
552a226927SNipun Gupta dev = &pdev->dev;
562a226927SNipun Gupta cdx_c = platform_get_drvdata(pdev);
572a226927SNipun Gupta cdx_mcdi = cdx_c->priv;
582a226927SNipun Gupta
592a226927SNipun Gupta r5_core_node = of_parse_phandle(dev->of_node, "xlnx,rproc", 0);
602a226927SNipun Gupta if (!r5_core_node) {
612a226927SNipun Gupta dev_err(&pdev->dev, "xlnx,rproc: invalid phandle\n");
622a226927SNipun Gupta return -EINVAL;
632a226927SNipun Gupta }
642a226927SNipun Gupta
652a226927SNipun Gupta rp = rproc_get_by_phandle(r5_core_node->phandle);
662a226927SNipun Gupta if (!rp) {
672a226927SNipun Gupta ret = -EPROBE_DEFER;
682a226927SNipun Gupta goto pdev_err;
692a226927SNipun Gupta }
702a226927SNipun Gupta
712a226927SNipun Gupta /* Attach to remote processor */
722a226927SNipun Gupta ret = rproc_boot(rp);
732a226927SNipun Gupta if (ret) {
742a226927SNipun Gupta dev_err(&pdev->dev, "Failed to attach to remote processor\n");
752a226927SNipun Gupta rproc_put(rp);
762a226927SNipun Gupta goto pdev_err;
772a226927SNipun Gupta }
782a226927SNipun Gupta
792a226927SNipun Gupta cdx_mcdi->r5_rproc = rp;
802a226927SNipun Gupta pdev_err:
812a226927SNipun Gupta of_node_put(r5_core_node);
822a226927SNipun Gupta return ret;
832a226927SNipun Gupta }
842a226927SNipun Gupta
cdx_detach_to_r5(struct platform_device * pdev)852a226927SNipun Gupta static void cdx_detach_to_r5(struct platform_device *pdev)
862a226927SNipun Gupta {
872a226927SNipun Gupta struct cdx_controller *cdx_c;
882a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi;
892a226927SNipun Gupta
902a226927SNipun Gupta cdx_c = platform_get_drvdata(pdev);
912a226927SNipun Gupta cdx_mcdi = cdx_c->priv;
922a226927SNipun Gupta
932a226927SNipun Gupta rproc_detach(cdx_mcdi->r5_rproc);
942a226927SNipun Gupta rproc_put(cdx_mcdi->r5_rproc);
952a226927SNipun Gupta }
962a226927SNipun Gupta
cdx_rpmsg_cb(struct rpmsg_device * rpdev,void * data,int len,void * priv,u32 src)972a226927SNipun Gupta static int cdx_rpmsg_cb(struct rpmsg_device *rpdev, void *data,
982a226927SNipun Gupta int len, void *priv, u32 src)
992a226927SNipun Gupta {
1002a226927SNipun Gupta struct cdx_controller *cdx_c = dev_get_drvdata(&rpdev->dev);
1012a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi = cdx_c->priv;
1022a226927SNipun Gupta
1032a226927SNipun Gupta if (len > MCDI_BUF_LEN)
1042a226927SNipun Gupta return -EINVAL;
1052a226927SNipun Gupta
1062a226927SNipun Gupta cdx_mcdi_process_cmd(cdx_mcdi, (struct cdx_dword *)data, len);
1072a226927SNipun Gupta
1082a226927SNipun Gupta return 0;
1092a226927SNipun Gupta }
1102a226927SNipun Gupta
cdx_rpmsg_post_probe_work(struct work_struct * work)1112a226927SNipun Gupta static void cdx_rpmsg_post_probe_work(struct work_struct *work)
1122a226927SNipun Gupta {
1132a226927SNipun Gupta struct cdx_controller *cdx_c;
1142a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi;
1152a226927SNipun Gupta
1162a226927SNipun Gupta cdx_mcdi = container_of(work, struct cdx_mcdi, work);
1172a226927SNipun Gupta cdx_c = dev_get_drvdata(&cdx_mcdi->rpdev->dev);
1182a226927SNipun Gupta cdx_rpmsg_post_probe(cdx_c);
1192a226927SNipun Gupta }
1202a226927SNipun Gupta
cdx_rpmsg_probe(struct rpmsg_device * rpdev)1212a226927SNipun Gupta static int cdx_rpmsg_probe(struct rpmsg_device *rpdev)
1222a226927SNipun Gupta {
1232a226927SNipun Gupta struct rpmsg_channel_info chinfo = {0};
1242a226927SNipun Gupta struct cdx_controller *cdx_c;
1252a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi;
1262a226927SNipun Gupta
1272a226927SNipun Gupta cdx_c = (struct cdx_controller *)cdx_rpmsg_id_table[0].driver_data;
1282a226927SNipun Gupta cdx_mcdi = cdx_c->priv;
1292a226927SNipun Gupta
1302a226927SNipun Gupta chinfo.src = RPMSG_ADDR_ANY;
1312a226927SNipun Gupta chinfo.dst = rpdev->dst;
1322a226927SNipun Gupta strscpy(chinfo.name, cdx_rpmsg_id_table[0].name,
1332a226927SNipun Gupta strlen(cdx_rpmsg_id_table[0].name));
1342a226927SNipun Gupta
1352a226927SNipun Gupta cdx_mcdi->ept = rpmsg_create_ept(rpdev, cdx_rpmsg_cb, NULL, chinfo);
1362a226927SNipun Gupta if (!cdx_mcdi->ept) {
1372a226927SNipun Gupta dev_err_probe(&rpdev->dev, -ENXIO,
1382a226927SNipun Gupta "Failed to create ept for channel %s\n",
1392a226927SNipun Gupta chinfo.name);
1402a226927SNipun Gupta return -EINVAL;
1412a226927SNipun Gupta }
1422a226927SNipun Gupta
1432a226927SNipun Gupta cdx_mcdi->rpdev = rpdev;
1442a226927SNipun Gupta dev_set_drvdata(&rpdev->dev, cdx_c);
1452a226927SNipun Gupta
1462a226927SNipun Gupta schedule_work(&cdx_mcdi->work);
1472a226927SNipun Gupta return 0;
1482a226927SNipun Gupta }
1492a226927SNipun Gupta
cdx_rpmsg_remove(struct rpmsg_device * rpdev)1502a226927SNipun Gupta static void cdx_rpmsg_remove(struct rpmsg_device *rpdev)
1512a226927SNipun Gupta {
1522a226927SNipun Gupta struct cdx_controller *cdx_c = dev_get_drvdata(&rpdev->dev);
1532a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi = cdx_c->priv;
1542a226927SNipun Gupta
1552a226927SNipun Gupta flush_work(&cdx_mcdi->work);
1562a226927SNipun Gupta cdx_rpmsg_pre_remove(cdx_c);
1572a226927SNipun Gupta
1582a226927SNipun Gupta rpmsg_destroy_ept(cdx_mcdi->ept);
1592a226927SNipun Gupta dev_set_drvdata(&rpdev->dev, NULL);
1602a226927SNipun Gupta }
1612a226927SNipun Gupta
1622a226927SNipun Gupta static struct rpmsg_driver cdx_rpmsg_driver = {
1632a226927SNipun Gupta .drv.name = KBUILD_MODNAME,
1642a226927SNipun Gupta .id_table = cdx_rpmsg_id_table,
1652a226927SNipun Gupta .probe = cdx_rpmsg_probe,
1662a226927SNipun Gupta .remove = cdx_rpmsg_remove,
1672a226927SNipun Gupta .callback = cdx_rpmsg_cb,
1682a226927SNipun Gupta };
1692a226927SNipun Gupta
cdx_setup_rpmsg(struct platform_device * pdev)1702a226927SNipun Gupta int cdx_setup_rpmsg(struct platform_device *pdev)
1712a226927SNipun Gupta {
1722a226927SNipun Gupta struct cdx_controller *cdx_c;
1732a226927SNipun Gupta struct cdx_mcdi *cdx_mcdi;
1742a226927SNipun Gupta int ret;
1752a226927SNipun Gupta
1762a226927SNipun Gupta /* Attach to remote processor */
1772a226927SNipun Gupta ret = cdx_attach_to_rproc(pdev);
1782a226927SNipun Gupta if (ret)
1792a226927SNipun Gupta return ret;
1802a226927SNipun Gupta
1812a226927SNipun Gupta cdx_c = platform_get_drvdata(pdev);
1822a226927SNipun Gupta cdx_mcdi = cdx_c->priv;
1832a226927SNipun Gupta
1842a226927SNipun Gupta /* Register RPMsg driver */
1852a226927SNipun Gupta cdx_rpmsg_id_table[0].driver_data = (kernel_ulong_t)cdx_c;
1862a226927SNipun Gupta
1872a226927SNipun Gupta INIT_WORK(&cdx_mcdi->work, cdx_rpmsg_post_probe_work);
1882a226927SNipun Gupta ret = register_rpmsg_driver(&cdx_rpmsg_driver);
1892a226927SNipun Gupta if (ret) {
1902a226927SNipun Gupta dev_err(&pdev->dev,
1912a226927SNipun Gupta "Failed to register cdx RPMsg driver: %d\n", ret);
1922a226927SNipun Gupta cdx_detach_to_r5(pdev);
1932a226927SNipun Gupta }
1942a226927SNipun Gupta
1952a226927SNipun Gupta return ret;
1962a226927SNipun Gupta }
1972a226927SNipun Gupta
cdx_destroy_rpmsg(struct platform_device * pdev)1982a226927SNipun Gupta void cdx_destroy_rpmsg(struct platform_device *pdev)
1992a226927SNipun Gupta {
2002a226927SNipun Gupta unregister_rpmsg_driver(&cdx_rpmsg_driver);
2012a226927SNipun Gupta
2022a226927SNipun Gupta cdx_detach_to_r5(pdev);
2032a226927SNipun Gupta }
204