xref: /linux/drivers/usb/cdns3/host.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
17733f6c3SPawel Laszczak // SPDX-License-Identifier: GPL-2.0
27733f6c3SPawel Laszczak /*
30b490046SPawel Laszczak  * Cadence USBSS and USBSSP DRD Driver - host side
47733f6c3SPawel Laszczak  *
57733f6c3SPawel Laszczak  * Copyright (C) 2018-2019 Cadence Design Systems.
67733f6c3SPawel Laszczak  * Copyright (C) 2017-2018 NXP
77733f6c3SPawel Laszczak  *
87733f6c3SPawel Laszczak  * Authors: Peter Chen <peter.chen@nxp.com>
97733f6c3SPawel Laszczak  *          Pawel Laszczak <pawell@cadence.com>
107733f6c3SPawel Laszczak  */
117733f6c3SPawel Laszczak 
127733f6c3SPawel Laszczak #include <linux/platform_device.h>
138581fd40SJakub Kicinski #include <linux/slab.h>
147733f6c3SPawel Laszczak #include "core.h"
157733f6c3SPawel Laszczak #include "drd.h"
165053691aSBen Dooks (Codethink) #include "host-export.h"
17b1234e3bSPeter Chen #include <linux/usb/hcd.h>
18ed227648SPeter Chen #include "../host/xhci.h"
19ed227648SPeter Chen #include "../host/xhci-plat.h"
20ed227648SPeter Chen 
2118a6be67SPawel Laszczak /*
2218a6be67SPawel Laszczak  * The XECP_PORT_CAP_REG and XECP_AUX_CTRL_REG1 exist only
2318a6be67SPawel Laszczak  * in Cadence USB3 dual-role controller, so it can't be used
2418a6be67SPawel Laszczak  * with Cadence CDNSP dual-role controller.
2518a6be67SPawel Laszczak  */
26ed227648SPeter Chen #define XECP_PORT_CAP_REG	0x8000
27ed227648SPeter Chen #define XECP_AUX_CTRL_REG1	0x8120
28ed227648SPeter Chen 
29ed227648SPeter Chen #define CFG_RXDET_P3_EN		BIT(15)
30ed227648SPeter Chen #define LPM_2_STB_SWITCH_EN	BIT(25)
31ed227648SPeter Chen 
xhci_cdns3_plat_start(struct usb_hcd * hcd)329d5333c9SLi Jun static void xhci_cdns3_plat_start(struct usb_hcd *hcd)
339d5333c9SLi Jun {
349d5333c9SLi Jun 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
359d5333c9SLi Jun 	u32 value;
369d5333c9SLi Jun 
379d5333c9SLi Jun 	/* set usbcmd.EU3S */
389d5333c9SLi Jun 	value = readl(&xhci->op_regs->command);
399d5333c9SLi Jun 	value |= CMD_PM_INDEX;
409d5333c9SLi Jun 	writel(value, &xhci->op_regs->command);
419d5333c9SLi Jun 
429d5333c9SLi Jun 	if (hcd->regs) {
439d5333c9SLi Jun 		value = readl(hcd->regs + XECP_AUX_CTRL_REG1);
449d5333c9SLi Jun 		value |= CFG_RXDET_P3_EN;
459d5333c9SLi Jun 		writel(value, hcd->regs + XECP_AUX_CTRL_REG1);
469d5333c9SLi Jun 
479d5333c9SLi Jun 		value = readl(hcd->regs + XECP_PORT_CAP_REG);
489d5333c9SLi Jun 		value |= LPM_2_STB_SWITCH_EN;
499d5333c9SLi Jun 		writel(value, hcd->regs + XECP_PORT_CAP_REG);
509d5333c9SLi Jun 	}
519d5333c9SLi Jun }
529d5333c9SLi Jun 
xhci_cdns3_resume_quirk(struct usb_hcd * hcd)539d5333c9SLi Jun static int xhci_cdns3_resume_quirk(struct usb_hcd *hcd)
549d5333c9SLi Jun {
559d5333c9SLi Jun 	xhci_cdns3_plat_start(hcd);
569d5333c9SLi Jun 	return 0;
579d5333c9SLi Jun }
5888171f67SPawel Laszczak 
59ed227648SPeter Chen static const struct xhci_plat_priv xhci_plat_cdns3_xhci = {
601cc6edd8SPeter Chen 	.quirks = XHCI_SKIP_PHY_INIT | XHCI_AVOID_BEI,
619d5333c9SLi Jun 	.plat_start = xhci_cdns3_plat_start,
629d5333c9SLi Jun 	.resume_quirk = xhci_cdns3_resume_quirk,
63ed227648SPeter Chen };
647733f6c3SPawel Laszczak 
65*e5fa8db0SPawel Laszczak static const struct xhci_plat_priv xhci_plat_cdnsp_xhci = {
66*e5fa8db0SPawel Laszczak 	.quirks = XHCI_CDNS_SCTX_QUIRK,
67*e5fa8db0SPawel Laszczak };
6818a6be67SPawel Laszczak 
__cdns_host_init(struct cdns * cdns)690b490046SPawel Laszczak static int __cdns_host_init(struct cdns *cdns)
707733f6c3SPawel Laszczak {
717733f6c3SPawel Laszczak 	struct platform_device *xhci;
727733f6c3SPawel Laszczak 	int ret;
73b1234e3bSPeter Chen 	struct usb_hcd *hcd;
747733f6c3SPawel Laszczak 
750b490046SPawel Laszczak 	cdns_drd_host_on(cdns);
767733f6c3SPawel Laszczak 
777733f6c3SPawel Laszczak 	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
787733f6c3SPawel Laszczak 	if (!xhci) {
797733f6c3SPawel Laszczak 		dev_err(cdns->dev, "couldn't allocate xHCI device\n");
807733f6c3SPawel Laszczak 		return -ENOMEM;
817733f6c3SPawel Laszczak 	}
827733f6c3SPawel Laszczak 
837733f6c3SPawel Laszczak 	xhci->dev.parent = cdns->dev;
847733f6c3SPawel Laszczak 	cdns->host_dev = xhci;
857733f6c3SPawel Laszczak 
867733f6c3SPawel Laszczak 	ret = platform_device_add_resources(xhci, cdns->xhci_res,
870b490046SPawel Laszczak 					    CDNS_XHCI_RESOURCES_NUM);
887733f6c3SPawel Laszczak 	if (ret) {
897733f6c3SPawel Laszczak 		dev_err(cdns->dev, "couldn't add resources to xHCI device\n");
907733f6c3SPawel Laszczak 		goto err1;
917733f6c3SPawel Laszczak 	}
927733f6c3SPawel Laszczak 
9318a6be67SPawel Laszczak 	if (cdns->version < CDNSP_CONTROLLER_V2)
947cea9657SPeter Chen 		cdns->xhci_plat_data = kmemdup(&xhci_plat_cdns3_xhci,
957cea9657SPeter Chen 				sizeof(struct xhci_plat_priv), GFP_KERNEL);
9618a6be67SPawel Laszczak 	else
9718a6be67SPawel Laszczak 		cdns->xhci_plat_data = kmemdup(&xhci_plat_cdnsp_xhci,
9818a6be67SPawel Laszczak 				sizeof(struct xhci_plat_priv), GFP_KERNEL);
9918a6be67SPawel Laszczak 
1007cea9657SPeter Chen 	if (!cdns->xhci_plat_data) {
1017cea9657SPeter Chen 		ret = -ENOMEM;
1027cea9657SPeter Chen 		goto err1;
1037cea9657SPeter Chen 	}
1047cea9657SPeter Chen 
105448373d9SRoger Quadros 	if (cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW))
1067cea9657SPeter Chen 		cdns->xhci_plat_data->quirks |= XHCI_DEFAULT_PM_RUNTIME_ALLOW;
1077cea9657SPeter Chen 
1087cea9657SPeter Chen 	ret = platform_device_add_data(xhci, cdns->xhci_plat_data,
109ed227648SPeter Chen 			sizeof(struct xhci_plat_priv));
110ed227648SPeter Chen 	if (ret)
1117cea9657SPeter Chen 		goto free_memory;
112ed227648SPeter Chen 
1137733f6c3SPawel Laszczak 	ret = platform_device_add(xhci);
1147733f6c3SPawel Laszczak 	if (ret) {
1157733f6c3SPawel Laszczak 		dev_err(cdns->dev, "failed to register xHCI device\n");
1167cea9657SPeter Chen 		goto free_memory;
1177733f6c3SPawel Laszczak 	}
1187733f6c3SPawel Laszczak 
119b1234e3bSPeter Chen 	/* Glue needs to access xHCI region register for Power management */
120b1234e3bSPeter Chen 	hcd = platform_get_drvdata(xhci);
121b1234e3bSPeter Chen 	if (hcd)
122b1234e3bSPeter Chen 		cdns->xhci_regs = hcd->regs;
123b1234e3bSPeter Chen 
1247733f6c3SPawel Laszczak 	return 0;
1257cea9657SPeter Chen 
1267cea9657SPeter Chen free_memory:
1277cea9657SPeter Chen 	kfree(cdns->xhci_plat_data);
1287733f6c3SPawel Laszczak err1:
1297733f6c3SPawel Laszczak 	platform_device_put(xhci);
1307733f6c3SPawel Laszczak 	return ret;
1317733f6c3SPawel Laszczak }
1327733f6c3SPawel Laszczak 
cdns_host_exit(struct cdns * cdns)1330b490046SPawel Laszczak static void cdns_host_exit(struct cdns *cdns)
1347733f6c3SPawel Laszczak {
1357cea9657SPeter Chen 	kfree(cdns->xhci_plat_data);
1367733f6c3SPawel Laszczak 	platform_device_unregister(cdns->host_dev);
1377733f6c3SPawel Laszczak 	cdns->host_dev = NULL;
1380b490046SPawel Laszczak 	cdns_drd_host_off(cdns);
1397733f6c3SPawel Laszczak }
1407733f6c3SPawel Laszczak 
cdns_host_init(struct cdns * cdns)1410b490046SPawel Laszczak int cdns_host_init(struct cdns *cdns)
1427733f6c3SPawel Laszczak {
1430b490046SPawel Laszczak 	struct cdns_role_driver *rdrv;
1447733f6c3SPawel Laszczak 
1457733f6c3SPawel Laszczak 	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
1467733f6c3SPawel Laszczak 	if (!rdrv)
1477733f6c3SPawel Laszczak 		return -ENOMEM;
1487733f6c3SPawel Laszczak 
1490b490046SPawel Laszczak 	rdrv->start	= __cdns_host_init;
1500b490046SPawel Laszczak 	rdrv->stop	= cdns_host_exit;
1510b490046SPawel Laszczak 	rdrv->state	= CDNS_ROLE_STATE_INACTIVE;
1527733f6c3SPawel Laszczak 	rdrv->name	= "host";
1537733f6c3SPawel Laszczak 
1547733f6c3SPawel Laszczak 	cdns->roles[USB_ROLE_HOST] = rdrv;
1557733f6c3SPawel Laszczak 
1567733f6c3SPawel Laszczak 	return 0;
1577733f6c3SPawel Laszczak }
158