1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * host.c - DesignWare USB3 DRD Controller Host Glue 4 * 5 * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com 6 * 7 * Authors: Felipe Balbi <balbi@ti.com>, 8 */ 9 10 #include <linux/irq.h> 11 #include <linux/of.h> 12 #include <linux/platform_device.h> 13 14 #include "../host/xhci-plat.h" 15 #include "core.h" 16 17 static const struct xhci_plat_priv dwc3_xhci_plat_priv = { 18 .quirks = XHCI_SKIP_PHY_INIT, 19 }; 20 21 static void dwc3_host_fill_xhci_irq_res(struct dwc3 *dwc, 22 int irq, char *name) 23 { 24 struct platform_device *pdev = to_platform_device(dwc->dev); 25 struct device_node *np = dev_of_node(&pdev->dev); 26 27 dwc->xhci_resources[1].start = irq; 28 dwc->xhci_resources[1].end = irq; 29 dwc->xhci_resources[1].flags = IORESOURCE_IRQ | irq_get_trigger_type(irq); 30 if (!name && np) 31 dwc->xhci_resources[1].name = of_node_full_name(pdev->dev.of_node); 32 else 33 dwc->xhci_resources[1].name = name; 34 } 35 36 static int dwc3_host_get_irq(struct dwc3 *dwc) 37 { 38 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); 39 int irq; 40 41 irq = platform_get_irq_byname_optional(dwc3_pdev, "host"); 42 if (irq > 0) { 43 dwc3_host_fill_xhci_irq_res(dwc, irq, "host"); 44 goto out; 45 } 46 47 if (irq == -EPROBE_DEFER) 48 goto out; 49 50 irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); 51 if (irq > 0) { 52 dwc3_host_fill_xhci_irq_res(dwc, irq, "dwc_usb3"); 53 goto out; 54 } 55 56 if (irq == -EPROBE_DEFER) 57 goto out; 58 59 irq = platform_get_irq(dwc3_pdev, 0); 60 if (irq > 0) { 61 dwc3_host_fill_xhci_irq_res(dwc, irq, NULL); 62 goto out; 63 } 64 65 if (!irq) 66 irq = -EINVAL; 67 68 out: 69 return irq; 70 } 71 72 int dwc3_host_init(struct dwc3 *dwc) 73 { 74 struct property_entry props[4]; 75 struct platform_device *xhci; 76 int ret, irq; 77 int prop_idx = 0; 78 79 irq = dwc3_host_get_irq(dwc); 80 if (irq < 0) 81 return irq; 82 83 xhci = platform_device_alloc("xhci-hcd", PLATFORM_DEVID_AUTO); 84 if (!xhci) { 85 dev_err(dwc->dev, "couldn't allocate xHCI device\n"); 86 return -ENOMEM; 87 } 88 89 xhci->dev.parent = dwc->dev; 90 91 dwc->xhci = xhci; 92 93 ret = platform_device_add_resources(xhci, dwc->xhci_resources, 94 DWC3_XHCI_RESOURCES_NUM); 95 if (ret) { 96 dev_err(dwc->dev, "couldn't add resources to xHCI device\n"); 97 goto err; 98 } 99 100 ret = platform_device_add_data(xhci, &dwc3_xhci_plat_priv, 101 sizeof(dwc3_xhci_plat_priv)); 102 if (ret) 103 goto err; 104 105 memset(props, 0, sizeof(struct property_entry) * ARRAY_SIZE(props)); 106 107 if (dwc->usb3_lpm_capable) 108 props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb3-lpm-capable"); 109 110 if (dwc->usb2_lpm_disable) 111 props[prop_idx++] = PROPERTY_ENTRY_BOOL("usb2-lpm-disable"); 112 113 /** 114 * WORKAROUND: dwc3 revisions <=3.00a have a limitation 115 * where Port Disable command doesn't work. 116 * 117 * The suggested workaround is that we avoid Port Disable 118 * completely. 119 * 120 * This following flag tells XHCI to do just that. 121 */ 122 if (DWC3_VER_IS_WITHIN(DWC3, ANY, 300A)) 123 props[prop_idx++] = PROPERTY_ENTRY_BOOL("quirk-broken-port-ped"); 124 125 if (prop_idx) { 126 ret = device_create_managed_software_node(&xhci->dev, props, NULL); 127 if (ret) { 128 dev_err(dwc->dev, "failed to add properties to xHCI\n"); 129 goto err; 130 } 131 } 132 133 ret = platform_device_add(xhci); 134 if (ret) { 135 dev_err(dwc->dev, "failed to register xHCI device\n"); 136 goto err; 137 } 138 139 return 0; 140 err: 141 platform_device_put(xhci); 142 return ret; 143 } 144 145 void dwc3_host_exit(struct dwc3 *dwc) 146 { 147 platform_device_unregister(dwc->xhci); 148 dwc->xhci = NULL; 149 } 150