| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Cadence PCI Glue driver. |
| 4 |
* |
| 5 |
* Copyright (C) 2019 Cadence. |
| 6 |
* |
| 7 |
* Author: Pawel Laszczak <[email protected]> |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <linux/platform_device.h> |
| 12 |
#include <linux/dma-mapping.h> |
| 13 |
#include <linux/kernel.h> |
| 14 |
#include <linux/module.h> |
| 15 |
#include <linux/slab.h> |
| 16 |
#include <linux/pci.h> |
| 17 |
|
| 18 |
#include "core.h" |
| 19 |
#include "gadget-export.h" |
| 20 |
|
| 21 |
#define PCI_BAR_HOST 0 |
| 22 |
#define PCI_BAR_OTG 0 |
| 23 |
#define PCI_BAR_DEV 2 |
| 24 |
|
| 25 |
#define PCI_DEV_FN_HOST_DEVICE 0 |
| 26 |
#define PCI_DEV_FN_OTG 1 |
| 27 |
|
| 28 |
#define PCI_DRIVER_NAME "cdns-pci-usbssp" |
| 29 |
#define PLAT_DRIVER_NAME "cdns-usbssp" |
| 30 |
|
| 31 |
#define CHICKEN_APB_TIMEOUT_VALUE 0x1C20 |
| 32 |
|
| 33 |
static struct pci_dev *cdnsp_get_second_fun(struct pci_dev *pdev) |
| 34 |
{ |
| 35 |
/* |
| 36 |
* Gets the second function. |
| 37 |
* Platform has two function. The fist keeps resources for |
| 38 |
* Host/Device while the secon keeps resources for DRD/OTG. |
| 39 |
*/ |
| 40 |
if (pdev->device == PCI_DEVICE_ID_CDNS_USBSSP) |
| 41 |
return pci_get_device(pdev->vendor, PCI_DEVICE_ID_CDNS_USBSS, NULL); |
| 42 |
if (pdev->device == PCI_DEVICE_ID_CDNS_USBSS) |
| 43 |
return pci_get_device(pdev->vendor, PCI_DEVICE_ID_CDNS_USBSSP, NULL); |
| 44 |
|
| 45 |
return NULL; |
| 46 |
} |
| 47 |
|
| 48 |
static int cdnsp_pci_probe(struct pci_dev *pdev, |
| 49 |
const struct pci_device_id *id) |
| 50 |
{ |
| 51 |
struct device *dev = &pdev->dev; |
| 52 |
struct pci_dev *func; |
| 53 |
struct resource *res; |
| 54 |
struct cdns *cdnsp; |
| 55 |
int ret; |
| 56 |
|
| 57 |
/* |
| 58 |
* For GADGET/HOST PCI (devfn) function number is 0, |
| 59 |
* for OTG PCI (devfn) function number is 1. |
| 60 |
*/ |
| 61 |
if (!id || (pdev->devfn != PCI_DEV_FN_HOST_DEVICE && |
| 62 |
pdev->devfn != PCI_DEV_FN_OTG)) |
| 63 |
return -EINVAL; |
| 64 |
|
| 65 |
func = cdnsp_get_second_fun(pdev); |
| 66 |
if (!func) |
| 67 |
return -EINVAL; |
| 68 |
|
| 69 |
if (func->class == PCI_CLASS_SERIAL_USB_XHCI || |
| 70 |
pdev->class == PCI_CLASS_SERIAL_USB_XHCI) { |
| 71 |
ret = -EINVAL; |
| 72 |
goto put_pci; |
| 73 |
} |
| 74 |
|
| 75 |
ret = pcim_enable_device(pdev); |
| 76 |
if (ret) { |
| 77 |
dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret); |
| 78 |
goto put_pci; |
| 79 |
} |
| 80 |
|
| 81 |
pci_set_master(pdev); |
| 82 |
if (pci_is_enabled(func)) { |
| 83 |
cdnsp = pci_get_drvdata(func); |
| 84 |
} else { |
| 85 |
cdnsp = kzalloc_obj(*cdnsp); |
| 86 |
if (!cdnsp) { |
| 87 |
ret = -ENOMEM; |
| 88 |
goto put_pci; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* For GADGET device function number is 0. */ |
| 93 |
if (pdev->devfn == 0) { |
| 94 |
resource_size_t rsrc_start, rsrc_len; |
| 95 |
|
| 96 |
/* Function 0: host(BAR_0) + device(BAR_1).*/ |
| 97 |
dev_dbg(dev, "Initialize resources\n"); |
| 98 |
rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV); |
| 99 |
rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV); |
| 100 |
res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev"); |
| 101 |
if (!res) { |
| 102 |
dev_dbg(dev, "controller already in use\n"); |
| 103 |
ret = -EBUSY; |
| 104 |
goto free_cdnsp; |
| 105 |
} |
| 106 |
|
| 107 |
cdnsp->dev_regs = devm_ioremap(dev, rsrc_start, rsrc_len); |
| 108 |
if (!cdnsp->dev_regs) { |
| 109 |
dev_dbg(dev, "error mapping memory\n"); |
| 110 |
ret = -EFAULT; |
| 111 |
goto free_cdnsp; |
| 112 |
} |
| 113 |
|
| 114 |
cdnsp->dev_irq = pdev->irq; |
| 115 |
dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n", |
| 116 |
&rsrc_start); |
| 117 |
|
| 118 |
res = &cdnsp->xhci_res[0]; |
| 119 |
res->start = pci_resource_start(pdev, PCI_BAR_HOST); |
| 120 |
res->end = pci_resource_end(pdev, PCI_BAR_HOST); |
| 121 |
res->name = "xhci"; |
| 122 |
res->flags = IORESOURCE_MEM; |
| 123 |
dev_dbg(dev, "USBSS-XHCI physical base addr: %pa\n", |
| 124 |
&res->start); |
| 125 |
|
| 126 |
/* Interrupt for XHCI, */ |
| 127 |
res = &cdnsp->xhci_res[1]; |
| 128 |
res->start = pdev->irq; |
| 129 |
res->name = "host"; |
| 130 |
res->flags = IORESOURCE_IRQ; |
| 131 |
} else { |
| 132 |
res = &cdnsp->otg_res; |
| 133 |
res->start = pci_resource_start(pdev, PCI_BAR_OTG); |
| 134 |
res->end = pci_resource_end(pdev, PCI_BAR_OTG); |
| 135 |
res->name = "otg"; |
| 136 |
res->flags = IORESOURCE_MEM; |
| 137 |
dev_dbg(dev, "CDNSP-DRD physical base addr: %pa\n", |
| 138 |
&res->start); |
| 139 |
|
| 140 |
/* Interrupt for OTG/DRD. */ |
| 141 |
cdnsp->otg_irq = pdev->irq; |
| 142 |
} |
| 143 |
|
| 144 |
/* |
| 145 |
* Cadence PCI based platform require some longer timeout for APB |
| 146 |
* to fixes domain clock synchronization issue after resuming |
| 147 |
* controller from L1 state. |
| 148 |
*/ |
| 149 |
cdnsp->override_apb_timeout = CHICKEN_APB_TIMEOUT_VALUE; |
| 150 |
pci_set_drvdata(pdev, cdnsp); |
| 151 |
|
| 152 |
if (pci_is_enabled(func)) { |
| 153 |
cdnsp->dev = dev; |
| 154 |
cdnsp->gadget_init = cdnsp_gadget_init; |
| 155 |
|
| 156 |
ret = cdns_init(cdnsp); |
| 157 |
if (ret) |
| 158 |
goto free_cdnsp; |
| 159 |
} |
| 160 |
|
| 161 |
device_wakeup_enable(&pdev->dev); |
| 162 |
if (pci_dev_run_wake(pdev)) |
| 163 |
pm_runtime_put_noidle(&pdev->dev); |
| 164 |
|
| 165 |
return 0; |
| 166 |
|
| 167 |
free_cdnsp: |
| 168 |
if (!pci_is_enabled(func)) |
| 169 |
kfree(cdnsp); |
| 170 |
|
| 171 |
put_pci: |
| 172 |
pci_dev_put(func); |
| 173 |
|
| 174 |
return ret; |
| 175 |
} |
| 176 |
|
| 177 |
static void cdnsp_pci_remove(struct pci_dev *pdev) |
| 178 |
{ |
| 179 |
struct cdns *cdnsp; |
| 180 |
struct pci_dev *func; |
| 181 |
|
| 182 |
func = cdnsp_get_second_fun(pdev); |
| 183 |
cdnsp = (struct cdns *)pci_get_drvdata(pdev); |
| 184 |
|
| 185 |
if (pci_dev_run_wake(pdev)) |
| 186 |
pm_runtime_get_noresume(&pdev->dev); |
| 187 |
|
| 188 |
if (pci_is_enabled(func)) { |
| 189 |
cdns_remove(cdnsp); |
| 190 |
} else { |
| 191 |
kfree(cdnsp); |
| 192 |
} |
| 193 |
|
| 194 |
pci_dev_put(func); |
| 195 |
} |
| 196 |
|
| 197 |
static int __maybe_unused cdnsp_pci_suspend(struct device *dev) |
| 198 |
{ |
| 199 |
struct cdns *cdns = dev_get_drvdata(dev); |
| 200 |
|
| 201 |
return cdns_suspend(cdns); |
| 202 |
} |
| 203 |
|
| 204 |
static int __maybe_unused cdnsp_pci_resume(struct device *dev) |
| 205 |
{ |
| 206 |
struct cdns *cdns = dev_get_drvdata(dev); |
| 207 |
unsigned long flags; |
| 208 |
int ret; |
| 209 |
|
| 210 |
spin_lock_irqsave(&cdns->lock, flags); |
| 211 |
ret = cdns_resume(cdns); |
| 212 |
spin_unlock_irqrestore(&cdns->lock, flags); |
| 213 |
cdns_set_active(cdns, 1); |
| 214 |
|
| 215 |
return ret; |
| 216 |
} |
| 217 |
|
| 218 |
static const struct dev_pm_ops cdnsp_pci_pm_ops = { |
| 219 |
SET_SYSTEM_SLEEP_PM_OPS(cdnsp_pci_suspend, cdnsp_pci_resume) |
| 220 |
}; |
| 221 |
|
| 222 |
static const struct pci_device_id cdnsp_pci_ids[] = { |
| 223 |
{ PCI_DEVICE(PCI_VENDOR_ID_CDNS, PCI_DEVICE_ID_CDNS_USBSSP), |
| 224 |
.class = PCI_CLASS_SERIAL_USB_DEVICE }, |
| 225 |
{ PCI_DEVICE(PCI_VENDOR_ID_CDNS, PCI_DEVICE_ID_CDNS_USBSSP), |
| 226 |
.class = PCI_CLASS_SERIAL_USB_CDNS }, |
| 227 |
{ PCI_DEVICE(PCI_VENDOR_ID_CDNS, PCI_DEVICE_ID_CDNS_USBSS), |
| 228 |
.class = PCI_CLASS_SERIAL_USB_CDNS }, |
| 229 |
{ 0, } |
| 230 |
}; |
| 231 |
|
| 232 |
static struct pci_driver cdnsp_pci_driver = { |
| 233 |
.name = "cdnsp-pci", |
| 234 |
.id_table = cdnsp_pci_ids, |
| 235 |
.probe = cdnsp_pci_probe, |
| 236 |
.remove = cdnsp_pci_remove, |
| 237 |
.driver = { |
| 238 |
.pm = &cdnsp_pci_pm_ops, |
| 239 |
} |
| 240 |
}; |
| 241 |
|
| 242 |
module_pci_driver(cdnsp_pci_driver); |
| 243 |
MODULE_DEVICE_TABLE(pci, cdnsp_pci_ids); |
| 244 |
|
| 245 |
MODULE_ALIAS("pci:cdnsp"); |
| 246 |
MODULE_AUTHOR("Pawel Laszczak <[email protected]>"); |
| 247 |
MODULE_LICENSE("GPL v2"); |
| 248 |
MODULE_DESCRIPTION("Cadence CDNSP PCI driver"); |