1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Cadence USBHS-DEV controller - PCI Glue driver.
4 *
5 * Copyright (C) 2023 Cadence.
6 *
7 * Author: Pawel Laszczak <pawell@cadence.com>
8 *
9 */
10
11 #include <linux/pm_runtime.h>
12 #include <linux/slab.h>
13 #include <linux/pci.h>
14
15 #include "cdns2-gadget.h"
16
17 #define PCI_DRIVER_NAME "cdns-pci-usbhs"
18 #define CDNS_VENDOR_ID 0x17cd
19 #define CDNS_DEVICE_ID 0x0120
20 #define PCI_BAR_DEV 0
21 #define PCI_DEV_FN_DEVICE 0
22
cdns2_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)23 static int cdns2_pci_probe(struct pci_dev *pdev,
24 const struct pci_device_id *id)
25 {
26 resource_size_t rsrc_start, rsrc_len;
27 struct device *dev = &pdev->dev;
28 struct cdns2_device *priv_dev;
29 struct resource *res;
30 int ret;
31
32 /* For GADGET PCI (devfn) function number is 0. */
33 if (!id || pdev->devfn != PCI_DEV_FN_DEVICE ||
34 pdev->class != PCI_CLASS_SERIAL_USB_DEVICE)
35 return -EINVAL;
36
37 ret = pcim_enable_device(pdev);
38 if (ret) {
39 dev_err(&pdev->dev, "Enabling PCI device has failed %d\n", ret);
40 return ret;
41 }
42
43 pci_set_master(pdev);
44
45 priv_dev = devm_kzalloc(&pdev->dev, sizeof(*priv_dev), GFP_KERNEL);
46 if (!priv_dev)
47 return -ENOMEM;
48
49 dev_dbg(dev, "Initialize resources\n");
50 rsrc_start = pci_resource_start(pdev, PCI_BAR_DEV);
51 rsrc_len = pci_resource_len(pdev, PCI_BAR_DEV);
52
53 res = devm_request_mem_region(dev, rsrc_start, rsrc_len, "dev");
54 if (!res) {
55 dev_dbg(dev, "controller already in use\n");
56 return -EBUSY;
57 }
58
59 priv_dev->regs = devm_ioremap(dev, rsrc_start, rsrc_len);
60 if (!priv_dev->regs) {
61 dev_dbg(dev, "error mapping memory\n");
62 return -EFAULT;
63 }
64
65 priv_dev->irq = pdev->irq;
66 dev_dbg(dev, "USBSS-DEV physical base addr: %pa\n",
67 &rsrc_start);
68
69 priv_dev->dev = dev;
70
71 priv_dev->eps_supported = 0x000f000f;
72 priv_dev->onchip_tx_buf = 16;
73 priv_dev->onchip_rx_buf = 16;
74
75 ret = cdns2_gadget_init(priv_dev);
76 if (ret)
77 return ret;
78
79 pci_set_drvdata(pdev, priv_dev);
80
81 device_wakeup_enable(&pdev->dev);
82 if (pci_dev_run_wake(pdev))
83 pm_runtime_put_noidle(&pdev->dev);
84
85 return 0;
86 }
87
cdns2_pci_remove(struct pci_dev * pdev)88 static void cdns2_pci_remove(struct pci_dev *pdev)
89 {
90 struct cdns2_device *priv_dev = pci_get_drvdata(pdev);
91
92 if (pci_dev_run_wake(pdev))
93 pm_runtime_get_noresume(&pdev->dev);
94
95 cdns2_gadget_remove(priv_dev);
96 }
97
cdns2_pci_suspend(struct device * dev)98 static int cdns2_pci_suspend(struct device *dev)
99 {
100 struct cdns2_device *priv_dev = dev_get_drvdata(dev);
101
102 return cdns2_gadget_suspend(priv_dev);
103 }
104
cdns2_pci_resume(struct device * dev)105 static int cdns2_pci_resume(struct device *dev)
106 {
107 struct cdns2_device *priv_dev = dev_get_drvdata(dev);
108
109 return cdns2_gadget_resume(priv_dev, 1);
110 }
111
112 static const struct dev_pm_ops cdns2_pci_pm_ops = {
113 SYSTEM_SLEEP_PM_OPS(cdns2_pci_suspend, cdns2_pci_resume)
114 };
115
116 static const struct pci_device_id cdns2_pci_ids[] = {
117 { PCI_VENDOR_ID_CDNS, CDNS_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,
118 PCI_CLASS_SERIAL_USB_DEVICE, PCI_ANY_ID },
119 { 0, }
120 };
121
122 static struct pci_driver cdns2_pci_driver = {
123 .name = "cdns2-pci",
124 .id_table = &cdns2_pci_ids[0],
125 .probe = cdns2_pci_probe,
126 .remove = cdns2_pci_remove,
127 .driver = {
128 .pm = pm_ptr(&cdns2_pci_pm_ops),
129 }
130 };
131
132 module_pci_driver(cdns2_pci_driver);
133 MODULE_DEVICE_TABLE(pci, cdns2_pci_ids);
134
135 MODULE_ALIAS("pci:cdns2");
136 MODULE_AUTHOR("Pawel Laszczak <pawell@cadence.com>");
137 MODULE_LICENSE("GPL");
138 MODULE_DESCRIPTION("Cadence CDNS2 PCI driver");
139