15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2a33e5d63SAndy Shevchenko /*
3d07e8819SFelipe Balbi * host.c - DesignWare USB3 DRD Controller Host Glue
4d07e8819SFelipe Balbi *
510623b87SAlexander A. Klimov * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com
6d07e8819SFelipe Balbi *
7d07e8819SFelipe Balbi * Authors: Felipe Balbi <balbi@ti.com>,
8d07e8819SFelipe Balbi */
9d07e8819SFelipe Balbi
1074b39dfaSLad Prabhakar #include <linux/irq.h>
1174b39dfaSLad Prabhakar #include <linux/of.h>
12d07e8819SFelipe Balbi #include <linux/platform_device.h>
13*000f9944SThinh Nguyen #include <linux/usb.h>
14*000f9944SThinh Nguyen #include <linux/usb/hcd.h>
15d07e8819SFelipe Balbi
16*000f9944SThinh Nguyen #include "../host/xhci-plat.h"
17d07e8819SFelipe Balbi #include "core.h"
18d07e8819SFelipe Balbi
dwc3_xhci_plat_start(struct usb_hcd * hcd)19*000f9944SThinh Nguyen static void dwc3_xhci_plat_start(struct usb_hcd *hcd)
20*000f9944SThinh Nguyen {
21*000f9944SThinh Nguyen struct platform_device *pdev;
22*000f9944SThinh Nguyen struct dwc3 *dwc;
23*000f9944SThinh Nguyen
24*000f9944SThinh Nguyen if (!usb_hcd_is_primary_hcd(hcd))
25*000f9944SThinh Nguyen return;
26*000f9944SThinh Nguyen
27*000f9944SThinh Nguyen pdev = to_platform_device(hcd->self.controller);
28*000f9944SThinh Nguyen dwc = dev_get_drvdata(pdev->dev.parent);
29*000f9944SThinh Nguyen
30*000f9944SThinh Nguyen dwc3_enable_susphy(dwc, true);
31*000f9944SThinh Nguyen }
32*000f9944SThinh Nguyen
33*000f9944SThinh Nguyen static const struct xhci_plat_priv dwc3_xhci_plat_quirk = {
34*000f9944SThinh Nguyen .plat_start = dwc3_xhci_plat_start,
35*000f9944SThinh Nguyen };
36*000f9944SThinh Nguyen
dwc3_host_fill_xhci_irq_res(struct dwc3 * dwc,int irq,char * name)3774b39dfaSLad Prabhakar static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc,
3874b39dfaSLad Prabhakar int irq, char *name)
3974b39dfaSLad Prabhakar {
4074b39dfaSLad Prabhakar struct platform_device *pdev = to_platform_device(dwc->dev);
4174b39dfaSLad Prabhakar struct device_node *np = dev_of_node(&pdev->dev);
4274b39dfaSLad Prabhakar
4374b39dfaSLad Prabhakar dwc->xhci_resources[1].start = irq;
4474b39dfaSLad Prabhakar dwc->xhci_resources[1].end = irq;
4574b39dfaSLad Prabhakar dwc->xhci_resources[1].flags = IORESOURCE_IRQ | irq_get_trigger_type(irq);
4674b39dfaSLad Prabhakar if (!name && np)
4774b39dfaSLad Prabhakar dwc->xhci_resources[1].name = of_node_full_name(pdev->dev.of_node);
4874b39dfaSLad Prabhakar else
4974b39dfaSLad Prabhakar dwc->xhci_resources[1].name = name;
5074b39dfaSLad Prabhakar }
5174b39dfaSLad Prabhakar
dwc3_host_get_irq(struct dwc3 * dwc)526d729a55SFelipe Balbi static int dwc3_host_get_irq(struct dwc3 *dwc)
536d729a55SFelipe Balbi {
546d729a55SFelipe Balbi struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
556d729a55SFelipe Balbi int irq;
566d729a55SFelipe Balbi
57f146b40bSHans de Goede irq = platform_get_irq_byname_optional(dwc3_pdev, "host");
5874b39dfaSLad Prabhakar if (irq > 0) {
5974b39dfaSLad Prabhakar dwc3_host_fill_xhci_irq_res(dwc, irq, "host");
606d729a55SFelipe Balbi goto out;
6174b39dfaSLad Prabhakar }
626d729a55SFelipe Balbi
636d729a55SFelipe Balbi if (irq == -EPROBE_DEFER)
646d729a55SFelipe Balbi goto out;
656d729a55SFelipe Balbi
66f146b40bSHans de Goede irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3");
6774b39dfaSLad Prabhakar if (irq > 0) {
6874b39dfaSLad Prabhakar dwc3_host_fill_xhci_irq_res(dwc, irq, "dwc_usb3");
696d729a55SFelipe Balbi goto out;
7074b39dfaSLad Prabhakar }
716d729a55SFelipe Balbi
726d729a55SFelipe Balbi if (irq == -EPROBE_DEFER)
736d729a55SFelipe Balbi goto out;
746d729a55SFelipe Balbi
756d729a55SFelipe Balbi irq = platform_get_irq(dwc3_pdev, 0);
76db2c2b16SMingxuan Xiang if (irq > 0)
7774b39dfaSLad Prabhakar dwc3_host_fill_xhci_irq_res(dwc, irq, NULL);
786d729a55SFelipe Balbi
796d729a55SFelipe Balbi out:
806d729a55SFelipe Balbi return irq;
816d729a55SFelipe Balbi }
826d729a55SFelipe Balbi
dwc3_host_init(struct dwc3 * dwc)83d07e8819SFelipe Balbi int dwc3_host_init(struct dwc3 *dwc)
84d07e8819SFelipe Balbi {
85b3e8e687SPrashanth K struct property_entry props[5];
86d07e8819SFelipe Balbi struct platform_device *xhci;
87322832f2SRoger Quadros int ret, irq;
88e42a5dbbSFelipe Balbi int prop_idx = 0;
89322832f2SRoger Quadros
906d729a55SFelipe Balbi irq = dwc3_host_get_irq(dwc);
916d729a55SFelipe Balbi if (irq < 0)
92322832f2SRoger Quadros return irq;
93322832f2SRoger Quadros
9452758bcbSVivek Gautam xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO);
95d07e8819SFelipe Balbi if (!xhci) {
96d07e8819SFelipe Balbi dev_err(dwc->dev, "couldn't allocate xHCI device\n");
9708f871a3SHeikki Krogerus return -ENOMEM;
98d07e8819SFelipe Balbi }
99d07e8819SFelipe Balbi
100d07e8819SFelipe Balbi xhci->dev.parent = dwc->dev;
101d07e8819SFelipe Balbi
102d07e8819SFelipe Balbi dwc->xhci = xhci;
103d07e8819SFelipe Balbi
10451249dcaSIdo Shayevitz ret = platform_device_add_resources(xhci, dwc->xhci_resources,
10551249dcaSIdo Shayevitz DWC3_XHCI_RESOURCES_NUM);
106d07e8819SFelipe Balbi if (ret) {
107d07e8819SFelipe Balbi dev_err(dwc->dev, "couldn't add resources to xHCI device\n");
1083cd703f4SMarek Szyprowski goto err;
109d07e8819SFelipe Balbi }
110d07e8819SFelipe Balbi
11195b57df4SHeikki Krogerus memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props));
112b2f463e1SPratyush Anand
113b3e8e687SPrashanth K props[prop_idx++] = PROPERTY_ENTRY_BOOL("xhci-sg-trb-cache-size-quirk");
114b3e8e687SPrashanth K
115e42a5dbbSFelipe Balbi if (dwc->usb3_lpm_capable)
1165eb5afb0SDmitry Torokhov props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb3-lpm-capable");
117e42a5dbbSFelipe Balbi
118022a0208SThinh Nguyen if (dwc->usb2_lpm_disable)
1195eb5afb0SDmitry Torokhov props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb2-lpm-disable");
120022a0208SThinh Nguyen
121e42a5dbbSFelipe Balbi /**
122e42a5dbbSFelipe Balbi * WORKAROUND: dwc3 revisions <=3.00a have a limitation
123e42a5dbbSFelipe Balbi * where Port Disable command doesn't work.
124e42a5dbbSFelipe Balbi *
125e42a5dbbSFelipe Balbi * The suggested workaround is that we avoid Port Disable
126e42a5dbbSFelipe Balbi * completely.
127e42a5dbbSFelipe Balbi *
128e42a5dbbSFelipe Balbi * This following flag tells XHCI to do just that.
129e42a5dbbSFelipe Balbi */
1309af21dd6SThinh Nguyen if (DWC3_VER_IS_WITHIN(DWC3, ANY, 300A))
1315eb5afb0SDmitry Torokhov props[prop_idx++] = PROPERTY_ENTRY_BOOL("quirk-broken-port-ped");
132e42a5dbbSFelipe Balbi
133e42a5dbbSFelipe Balbi if (prop_idx) {
134b1638ee5SHeikki Krogerus ret = device_create_managed_software_node(&xhci->dev, props, NULL);
135b2f463e1SPratyush Anand if (ret) {
13695b57df4SHeikki Krogerus dev_err(dwc->dev, "failed to add properties to xHCI\n");
1373cd703f4SMarek Szyprowski goto err;
138b2f463e1SPratyush Anand }
13995b57df4SHeikki Krogerus }
140b2f463e1SPratyush Anand
141*000f9944SThinh Nguyen ret = platform_device_add_data(xhci, &dwc3_xhci_plat_quirk,
142*000f9944SThinh Nguyen sizeof(struct xhci_plat_priv));
143*000f9944SThinh Nguyen if (ret)
144*000f9944SThinh Nguyen goto err;
145*000f9944SThinh Nguyen
146d07e8819SFelipe Balbi ret = platform_device_add(xhci);
147d07e8819SFelipe Balbi if (ret) {
148d07e8819SFelipe Balbi dev_err(dwc->dev, "failed to register xHCI device\n");
1493cd703f4SMarek Szyprowski goto err;
150d07e8819SFelipe Balbi }
151d07e8819SFelipe Balbi
152fd2304f4SThinh Nguyen if (dwc->sys_wakeup) {
153fd2304f4SThinh Nguyen /* Restore wakeup setting if switched from device */
154fd2304f4SThinh Nguyen device_wakeup_enable(dwc->sysdev);
155fd2304f4SThinh Nguyen
156fd2304f4SThinh Nguyen /* Pass on wakeup setting to the new xhci platform device */
157fd2304f4SThinh Nguyen device_init_wakeup(&xhci->dev, true);
158fd2304f4SThinh Nguyen }
159fd2304f4SThinh Nguyen
160d07e8819SFelipe Balbi return 0;
1613cd703f4SMarek Szyprowski err:
162d07e8819SFelipe Balbi platform_device_put(xhci);
163d07e8819SFelipe Balbi return ret;
164d07e8819SFelipe Balbi }
165d07e8819SFelipe Balbi
dwc3_host_exit(struct dwc3 * dwc)166d07e8819SFelipe Balbi void dwc3_host_exit(struct dwc3 *dwc)
167d07e8819SFelipe Balbi {
168fd2304f4SThinh Nguyen if (dwc->sys_wakeup)
169fd2304f4SThinh Nguyen device_init_wakeup(&dwc->xhci->dev, false);
170fd2304f4SThinh Nguyen
171*000f9944SThinh Nguyen dwc3_enable_susphy(dwc, false);
172d07e8819SFelipe Balbi platform_device_unregister(dwc->xhci);
173a872ab30SJohan Hovold dwc->xhci = NULL;
174d07e8819SFelipe Balbi }
175