xref: /openbmc/linux/drivers/usb/cdns3/host.c (revision ab589bac)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cadence USBSS DRD Driver - host side
4  *
5  * Copyright (C) 2018-2019 Cadence Design Systems.
6  * Copyright (C) 2017-2018 NXP
7  *
8  * Authors: Peter Chen <peter.chen@nxp.com>
9  *          Pawel Laszczak <pawell@cadence.com>
10  */
11 
12 #include <linux/platform_device.h>
13 #include "core.h"
14 #include "drd.h"
15 #include "host-export.h"
16 #include <linux/usb/hcd.h>
17 
18 static int __cdns3_host_init(struct cdns3 *cdns)
19 {
20 	struct platform_device *xhci;
21 	int ret;
22 	struct usb_hcd *hcd;
23 
24 	cdns3_drd_host_on(cdns);
25 
26 	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
27 	if (!xhci) {
28 		dev_err(cdns->dev, "couldn't allocate xHCI device\n");
29 		return -ENOMEM;
30 	}
31 
32 	xhci->dev.parent = cdns->dev;
33 	cdns->host_dev = xhci;
34 
35 	ret = platform_device_add_resources(xhci, cdns->xhci_res,
36 					    CDNS3_XHCI_RESOURCES_NUM);
37 	if (ret) {
38 		dev_err(cdns->dev, "couldn't add resources to xHCI device\n");
39 		goto err1;
40 	}
41 
42 	ret = platform_device_add(xhci);
43 	if (ret) {
44 		dev_err(cdns->dev, "failed to register xHCI device\n");
45 		goto err1;
46 	}
47 
48 	/* Glue needs to access xHCI region register for Power management */
49 	hcd = platform_get_drvdata(xhci);
50 	if (hcd)
51 		cdns->xhci_regs = hcd->regs;
52 
53 	return 0;
54 err1:
55 	platform_device_put(xhci);
56 	return ret;
57 }
58 
59 static void cdns3_host_exit(struct cdns3 *cdns)
60 {
61 	platform_device_unregister(cdns->host_dev);
62 	cdns->host_dev = NULL;
63 	cdns3_drd_host_off(cdns);
64 }
65 
66 int cdns3_host_init(struct cdns3 *cdns)
67 {
68 	struct cdns3_role_driver *rdrv;
69 
70 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
71 	if (!rdrv)
72 		return -ENOMEM;
73 
74 	rdrv->start	= __cdns3_host_init;
75 	rdrv->stop	= cdns3_host_exit;
76 	rdrv->state	= CDNS3_ROLE_STATE_INACTIVE;
77 	rdrv->name	= "host";
78 
79 	cdns->roles[USB_ROLE_HOST] = rdrv;
80 
81 	return 0;
82 }
83