18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Cadence USBSS DRD Driver - host side
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2018-2019 Cadence Design Systems.
68c2ecf20Sopenharmony_ci * Copyright (C) 2017-2018 NXP
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Authors: Peter Chen <peter.chen@nxp.com>
98c2ecf20Sopenharmony_ci *          Pawel Laszczak <pawell@cadence.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include "core.h"
148c2ecf20Sopenharmony_ci#include "drd.h"
158c2ecf20Sopenharmony_ci#include "host-export.h"
168c2ecf20Sopenharmony_ci#include <linux/usb/hcd.h>
178c2ecf20Sopenharmony_ci#include "../host/xhci.h"
188c2ecf20Sopenharmony_ci#include "../host/xhci-plat.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define XECP_PORT_CAP_REG	0x8000
218c2ecf20Sopenharmony_ci#define XECP_AUX_CTRL_REG1	0x8120
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#define CFG_RXDET_P3_EN		BIT(15)
248c2ecf20Sopenharmony_ci#define LPM_2_STB_SWITCH_EN	BIT(25)
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistatic const struct xhci_plat_priv xhci_plat_cdns3_xhci = {
278c2ecf20Sopenharmony_ci	.quirks = XHCI_SKIP_PHY_INIT,
288c2ecf20Sopenharmony_ci	.suspend_quirk = xhci_cdns3_suspend_quirk,
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic int __cdns3_host_init(struct cdns3 *cdns)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct platform_device *xhci;
348c2ecf20Sopenharmony_ci	int ret;
358c2ecf20Sopenharmony_ci	struct usb_hcd *hcd;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	cdns3_drd_host_on(cdns);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
408c2ecf20Sopenharmony_ci	if (!xhci) {
418c2ecf20Sopenharmony_ci		dev_err(cdns->dev, "couldn't allocate xHCI device\n");
428c2ecf20Sopenharmony_ci		return -ENOMEM;
438c2ecf20Sopenharmony_ci	}
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	xhci->dev.parent = cdns->dev;
468c2ecf20Sopenharmony_ci	cdns->host_dev = xhci;
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	ret = platform_device_add_resources(xhci, cdns->xhci_res,
498c2ecf20Sopenharmony_ci					    CDNS3_XHCI_RESOURCES_NUM);
508c2ecf20Sopenharmony_ci	if (ret) {
518c2ecf20Sopenharmony_ci		dev_err(cdns->dev, "couldn't add resources to xHCI device\n");
528c2ecf20Sopenharmony_ci		goto err1;
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	cdns->xhci_plat_data = kmemdup(&xhci_plat_cdns3_xhci,
568c2ecf20Sopenharmony_ci			sizeof(struct xhci_plat_priv), GFP_KERNEL);
578c2ecf20Sopenharmony_ci	if (!cdns->xhci_plat_data) {
588c2ecf20Sopenharmony_ci		ret = -ENOMEM;
598c2ecf20Sopenharmony_ci		goto err1;
608c2ecf20Sopenharmony_ci	}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	if (cdns->pdata && (cdns->pdata->quirks & CDNS3_DEFAULT_PM_RUNTIME_ALLOW))
638c2ecf20Sopenharmony_ci		cdns->xhci_plat_data->quirks |= XHCI_DEFAULT_PM_RUNTIME_ALLOW;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ret = platform_device_add_data(xhci, cdns->xhci_plat_data,
668c2ecf20Sopenharmony_ci			sizeof(struct xhci_plat_priv));
678c2ecf20Sopenharmony_ci	if (ret)
688c2ecf20Sopenharmony_ci		goto free_memory;
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci	ret = platform_device_add(xhci);
718c2ecf20Sopenharmony_ci	if (ret) {
728c2ecf20Sopenharmony_ci		dev_err(cdns->dev, "failed to register xHCI device\n");
738c2ecf20Sopenharmony_ci		goto free_memory;
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* Glue needs to access xHCI region register for Power management */
778c2ecf20Sopenharmony_ci	hcd = platform_get_drvdata(xhci);
788c2ecf20Sopenharmony_ci	if (hcd)
798c2ecf20Sopenharmony_ci		cdns->xhci_regs = hcd->regs;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	return 0;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_cifree_memory:
848c2ecf20Sopenharmony_ci	kfree(cdns->xhci_plat_data);
858c2ecf20Sopenharmony_cierr1:
868c2ecf20Sopenharmony_ci	platform_device_put(xhci);
878c2ecf20Sopenharmony_ci	return ret;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciint xhci_cdns3_suspend_quirk(struct usb_hcd *hcd)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	struct xhci_hcd	*xhci = hcd_to_xhci(hcd);
938c2ecf20Sopenharmony_ci	u32 value;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (pm_runtime_status_suspended(hcd->self.controller))
968c2ecf20Sopenharmony_ci		return 0;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	/* set usbcmd.EU3S */
998c2ecf20Sopenharmony_ci	value = readl(&xhci->op_regs->command);
1008c2ecf20Sopenharmony_ci	value |= CMD_PM_INDEX;
1018c2ecf20Sopenharmony_ci	writel(value, &xhci->op_regs->command);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (hcd->regs) {
1048c2ecf20Sopenharmony_ci		value = readl(hcd->regs + XECP_AUX_CTRL_REG1);
1058c2ecf20Sopenharmony_ci		value |= CFG_RXDET_P3_EN;
1068c2ecf20Sopenharmony_ci		writel(value, hcd->regs + XECP_AUX_CTRL_REG1);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci		value = readl(hcd->regs + XECP_PORT_CAP_REG);
1098c2ecf20Sopenharmony_ci		value |= LPM_2_STB_SWITCH_EN;
1108c2ecf20Sopenharmony_ci		writel(value, hcd->regs + XECP_PORT_CAP_REG);
1118c2ecf20Sopenharmony_ci	}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	return 0;
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic void cdns3_host_exit(struct cdns3 *cdns)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	kfree(cdns->xhci_plat_data);
1198c2ecf20Sopenharmony_ci	platform_device_unregister(cdns->host_dev);
1208c2ecf20Sopenharmony_ci	cdns->host_dev = NULL;
1218c2ecf20Sopenharmony_ci	cdns3_drd_host_off(cdns);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ciint cdns3_host_init(struct cdns3 *cdns)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	struct cdns3_role_driver *rdrv;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	rdrv = devm_kzalloc(cdns->dev, sizeof(*rdrv), GFP_KERNEL);
1298c2ecf20Sopenharmony_ci	if (!rdrv)
1308c2ecf20Sopenharmony_ci		return -ENOMEM;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	rdrv->start	= __cdns3_host_init;
1338c2ecf20Sopenharmony_ci	rdrv->stop	= cdns3_host_exit;
1348c2ecf20Sopenharmony_ci	rdrv->state	= CDNS3_ROLE_STATE_INACTIVE;
1358c2ecf20Sopenharmony_ci	rdrv->name	= "host";
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	cdns->roles[USB_ROLE_HOST] = rdrv;
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return 0;
1408c2ecf20Sopenharmony_ci}
141