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