1 // SPDX-License-Identifier: GPL-2.0
3 * CDX host controller driver for AMD versal-net platform.
5 * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
8 #include <linux/of_platform.h>
9 #include <linux/slab.h>
10 #include <linux/cdx/cdx_bus.h>
12 #include "cdx_controller.h"
14 #include "mcdi_functions.h"
17 static unsigned int cdx_mcdi_rpc_timeout(struct cdx_mcdi *cdx, unsigned int cmd)
19 return MCDI_RPC_TIMEOUT;
22 static void cdx_mcdi_request(struct cdx_mcdi *cdx,
23 const struct cdx_dword *hdr, size_t hdr_len,
24 const struct cdx_dword *sdu, size_t sdu_len)
26 if (cdx_rpmsg_send(cdx, hdr, hdr_len, sdu, sdu_len))
27 dev_err(&cdx->rpdev->dev, "Failed to send rpmsg data\n");
30 static const struct cdx_mcdi_ops mcdi_ops = {
31 .mcdi_rpc_timeout = cdx_mcdi_rpc_timeout,
32 .mcdi_request = cdx_mcdi_request,
35 void cdx_rpmsg_post_probe(struct cdx_controller *cdx)
37 /* Register CDX controller with CDX bus driver */
38 if (cdx_register_controller(cdx))
39 dev_err(cdx->dev, "Failed to register CDX controller\n");
42 void cdx_rpmsg_pre_remove(struct cdx_controller *cdx)
44 cdx_unregister_controller(cdx);
45 cdx_mcdi_wait_for_quiescence(cdx->priv, MCDI_RPC_TIMEOUT);
48 static int cdx_configure_device(struct cdx_controller *cdx,
49 u8 bus_num, u8 dev_num,
50 struct cdx_device_config *dev_config)
54 switch (dev_config->type) {
55 case CDX_DEV_RESET_CONF:
56 ret = cdx_mcdi_reset_device(cdx->priv, bus_num, dev_num);
65 static int cdx_scan_devices(struct cdx_controller *cdx)
67 struct cdx_mcdi *cdx_mcdi = cdx->priv;
68 u8 bus_num, dev_num, num_cdx_bus;
71 /* MCDI FW Read: Fetch the number of CDX buses on this controller */
72 ret = cdx_mcdi_get_num_buses(cdx_mcdi);
75 "Get number of CDX buses failed: %d\n", ret);
78 num_cdx_bus = (u8)ret;
80 for (bus_num = 0; bus_num < num_cdx_bus; bus_num++) {
83 /* MCDI FW Read: Fetch the number of devices present */
84 ret = cdx_mcdi_get_num_devs(cdx_mcdi, bus_num);
87 "Get devices on CDX bus %d failed: %d\n", bus_num, ret);
90 num_cdx_dev = (u8)ret;
92 for (dev_num = 0; dev_num < num_cdx_dev; dev_num++) {
93 struct cdx_dev_params dev_params;
95 /* MCDI FW: Get the device config */
96 ret = cdx_mcdi_get_dev_config(cdx_mcdi, bus_num,
97 dev_num, &dev_params);
100 "CDX device config get failed for %d(bus):%d(dev), %d\n",
101 bus_num, dev_num, ret);
104 dev_params.cdx = cdx;
106 /* Add the device to the cdx bus */
107 ret = cdx_device_add(&dev_params);
109 dev_err(cdx->dev, "registering cdx dev: %d failed: %d\n",
114 dev_dbg(cdx->dev, "CDX dev: %d on cdx bus: %d created\n",
122 static struct cdx_ops cdx_ops = {
123 .scan = cdx_scan_devices,
124 .dev_configure = cdx_configure_device,
127 static int xlnx_cdx_probe(struct platform_device *pdev)
129 struct cdx_controller *cdx;
130 struct cdx_mcdi *cdx_mcdi;
133 cdx_mcdi = kzalloc(sizeof(*cdx_mcdi), GFP_KERNEL);
137 /* Store the MCDI ops */
138 cdx_mcdi->mcdi_ops = &mcdi_ops;
139 /* MCDI FW: Initialize the FW path */
140 ret = cdx_mcdi_init(cdx_mcdi);
142 dev_err_probe(&pdev->dev, ret, "MCDI Initialization failed\n");
146 cdx = kzalloc(sizeof(*cdx), GFP_KERNEL);
151 platform_set_drvdata(pdev, cdx);
153 cdx->dev = &pdev->dev;
154 cdx->priv = cdx_mcdi;
157 ret = cdx_setup_rpmsg(pdev);
159 if (ret != -EPROBE_DEFER)
160 dev_err(&pdev->dev, "Failed to register CDX RPMsg transport\n");
164 dev_info(&pdev->dev, "Successfully registered CDX controller with RPMsg as transport\n");
170 cdx_mcdi_finish(cdx_mcdi);
177 static int xlnx_cdx_remove(struct platform_device *pdev)
179 struct cdx_controller *cdx = platform_get_drvdata(pdev);
180 struct cdx_mcdi *cdx_mcdi = cdx->priv;
182 cdx_destroy_rpmsg(pdev);
186 cdx_mcdi_finish(cdx_mcdi);
192 static const struct of_device_id cdx_match_table[] = {
193 {.compatible = "xlnx,versal-net-cdx",},
197 MODULE_DEVICE_TABLE(of, cdx_match_table);
199 static struct platform_driver cdx_pdriver = {
201 .name = "cdx-controller",
203 .of_match_table = cdx_match_table,
205 .probe = xlnx_cdx_probe,
206 .remove = xlnx_cdx_remove,
209 static int __init cdx_controller_init(void)
213 ret = platform_driver_register(&cdx_pdriver);
215 pr_err("platform_driver_register() failed: %d\n", ret);
220 static void __exit cdx_controller_exit(void)
222 platform_driver_unregister(&cdx_pdriver);
225 module_init(cdx_controller_init);
226 module_exit(cdx_controller_exit);
228 MODULE_AUTHOR("AMD Inc.");
229 MODULE_DESCRIPTION("CDX controller for AMD devices");
230 MODULE_LICENSE("GPL");