xref: /openbmc/linux/drivers/usb/dwc2/pci.c (revision fe70dce9)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2197ba5f4SPaul Zimmerman /*
3197ba5f4SPaul Zimmerman  * pci.c - DesignWare HS OTG Controller PCI driver
4197ba5f4SPaul Zimmerman  *
5197ba5f4SPaul Zimmerman  * Copyright (C) 2004-2013 Synopsys, Inc.
6197ba5f4SPaul Zimmerman  *
7197ba5f4SPaul Zimmerman  * Redistribution and use in source and binary forms, with or without
8197ba5f4SPaul Zimmerman  * modification, are permitted provided that the following conditions
9197ba5f4SPaul Zimmerman  * are met:
10197ba5f4SPaul Zimmerman  * 1. Redistributions of source code must retain the above copyright
11197ba5f4SPaul Zimmerman  *    notice, this list of conditions, and the following disclaimer,
12197ba5f4SPaul Zimmerman  *    without modification.
13197ba5f4SPaul Zimmerman  * 2. Redistributions in binary form must reproduce the above copyright
14197ba5f4SPaul Zimmerman  *    notice, this list of conditions and the following disclaimer in the
15197ba5f4SPaul Zimmerman  *    documentation and/or other materials provided with the distribution.
16197ba5f4SPaul Zimmerman  * 3. The names of the above-listed copyright holders may not be used
17197ba5f4SPaul Zimmerman  *    to endorse or promote products derived from this software without
18197ba5f4SPaul Zimmerman  *    specific prior written permission.
19197ba5f4SPaul Zimmerman  *
20197ba5f4SPaul Zimmerman  * ALTERNATIVELY, this software may be distributed under the terms of the
21197ba5f4SPaul Zimmerman  * GNU General Public License ("GPL") as published by the Free Software
22197ba5f4SPaul Zimmerman  * Foundation; either version 2 of the License, or (at your option) any
23197ba5f4SPaul Zimmerman  * later version.
24197ba5f4SPaul Zimmerman  *
25197ba5f4SPaul Zimmerman  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26197ba5f4SPaul Zimmerman  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27197ba5f4SPaul Zimmerman  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28197ba5f4SPaul Zimmerman  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29197ba5f4SPaul Zimmerman  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30197ba5f4SPaul Zimmerman  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31197ba5f4SPaul Zimmerman  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32197ba5f4SPaul Zimmerman  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33197ba5f4SPaul Zimmerman  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34197ba5f4SPaul Zimmerman  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35197ba5f4SPaul Zimmerman  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36197ba5f4SPaul Zimmerman  */
37197ba5f4SPaul Zimmerman 
38197ba5f4SPaul Zimmerman /*
39197ba5f4SPaul Zimmerman  * Provides the initialization and cleanup entry points for the DWC_otg PCI
40197ba5f4SPaul Zimmerman  * driver
41197ba5f4SPaul Zimmerman  */
42197ba5f4SPaul Zimmerman #include <linux/kernel.h>
43197ba5f4SPaul Zimmerman #include <linux/module.h>
44197ba5f4SPaul Zimmerman #include <linux/moduleparam.h>
45197ba5f4SPaul Zimmerman #include <linux/spinlock.h>
46197ba5f4SPaul Zimmerman #include <linux/interrupt.h>
47197ba5f4SPaul Zimmerman #include <linux/io.h>
48197ba5f4SPaul Zimmerman #include <linux/slab.h>
49197ba5f4SPaul Zimmerman #include <linux/pci.h>
50197ba5f4SPaul Zimmerman #include <linux/usb.h>
51197ba5f4SPaul Zimmerman 
52197ba5f4SPaul Zimmerman #include <linux/usb/hcd.h>
53197ba5f4SPaul Zimmerman #include <linux/usb/ch11.h>
549024c495SJohn Youn #include <linux/platform_device.h>
559024c495SJohn Youn #include <linux/usb/usb_phy_generic.h>
56197ba5f4SPaul Zimmerman 
57197ba5f4SPaul Zimmerman #define PCI_PRODUCT_ID_HAPS_HSOTG	0xabc0
58197ba5f4SPaul Zimmerman 
599024c495SJohn Youn static const char dwc2_driver_name[] = "dwc2-pci";
60197ba5f4SPaul Zimmerman 
619024c495SJohn Youn struct dwc2_pci_glue {
629024c495SJohn Youn 	struct platform_device *dwc2;
639024c495SJohn Youn 	struct platform_device *phy;
64197ba5f4SPaul Zimmerman };
65197ba5f4SPaul Zimmerman 
66ced8a03cSVahram Aharonyan static int dwc2_pci_quirks(struct pci_dev *pdev, struct platform_device *dwc2)
67ced8a03cSVahram Aharonyan {
68ced8a03cSVahram Aharonyan 	if (pdev->vendor == PCI_VENDOR_ID_SYNOPSYS &&
69ced8a03cSVahram Aharonyan 	    pdev->device == PCI_PRODUCT_ID_HAPS_HSOTG) {
70ced8a03cSVahram Aharonyan 		struct property_entry properties[] = {
71ced8a03cSVahram Aharonyan 			{ },
72ced8a03cSVahram Aharonyan 		};
73ced8a03cSVahram Aharonyan 
74ced8a03cSVahram Aharonyan 		return platform_device_add_properties(dwc2, properties);
75ced8a03cSVahram Aharonyan 	}
76ced8a03cSVahram Aharonyan 
77ced8a03cSVahram Aharonyan 	return 0;
78ced8a03cSVahram Aharonyan }
79ced8a03cSVahram Aharonyan 
806fb914d7SGrigor Tovmasyan /**
816fb914d7SGrigor Tovmasyan  * dwc2_pci_probe() - Provides the cleanup entry points for the DWC_otg PCI
826fb914d7SGrigor Tovmasyan  * driver
836fb914d7SGrigor Tovmasyan  *
846fb914d7SGrigor Tovmasyan  * @pci: The programming view of DWC_otg PCI
856fb914d7SGrigor Tovmasyan  */
869024c495SJohn Youn static void dwc2_pci_remove(struct pci_dev *pci)
87197ba5f4SPaul Zimmerman {
889024c495SJohn Youn 	struct dwc2_pci_glue *glue = pci_get_drvdata(pci);
89197ba5f4SPaul Zimmerman 
909024c495SJohn Youn 	platform_device_unregister(glue->dwc2);
919024c495SJohn Youn 	usb_phy_generic_unregister(glue->phy);
929024c495SJohn Youn 	pci_set_drvdata(pci, NULL);
93197ba5f4SPaul Zimmerman }
94197ba5f4SPaul Zimmerman 
959024c495SJohn Youn static int dwc2_pci_probe(struct pci_dev *pci,
96197ba5f4SPaul Zimmerman 			  const struct pci_device_id *id)
97197ba5f4SPaul Zimmerman {
989024c495SJohn Youn 	struct resource		res[2];
999024c495SJohn Youn 	struct platform_device	*dwc2;
1009024c495SJohn Youn 	struct platform_device	*phy;
1019024c495SJohn Youn 	int			ret;
1029024c495SJohn Youn 	struct device		*dev = &pci->dev;
1039024c495SJohn Youn 	struct dwc2_pci_glue	*glue;
104197ba5f4SPaul Zimmerman 
1059024c495SJohn Youn 	ret = pcim_enable_device(pci);
1069024c495SJohn Youn 	if (ret) {
1079024c495SJohn Youn 		dev_err(dev, "failed to enable pci device\n");
108197ba5f4SPaul Zimmerman 		return -ENODEV;
109197ba5f4SPaul Zimmerman 	}
110197ba5f4SPaul Zimmerman 
1119024c495SJohn Youn 	pci_set_master(pci);
112197ba5f4SPaul Zimmerman 
113a127cccdSVardan Mikayelyan 	phy = usb_phy_generic_register();
114a127cccdSVardan Mikayelyan 	if (IS_ERR(phy)) {
115a127cccdSVardan Mikayelyan 		dev_err(dev, "error registering generic PHY (%ld)\n",
116a127cccdSVardan Mikayelyan 			PTR_ERR(phy));
117a127cccdSVardan Mikayelyan 		return PTR_ERR(phy);
118a127cccdSVardan Mikayelyan 	}
119a127cccdSVardan Mikayelyan 
1209024c495SJohn Youn 	dwc2 = platform_device_alloc("dwc2", PLATFORM_DEVID_AUTO);
1219024c495SJohn Youn 	if (!dwc2) {
1229024c495SJohn Youn 		dev_err(dev, "couldn't allocate dwc2 device\n");
123ecd29dabSVardan Mikayelyan 		goto err;
1249024c495SJohn Youn 	}
1259024c495SJohn Youn 
1269024c495SJohn Youn 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
1279024c495SJohn Youn 
1289024c495SJohn Youn 	res[0].start	= pci_resource_start(pci, 0);
1299024c495SJohn Youn 	res[0].end	= pci_resource_end(pci, 0);
1309024c495SJohn Youn 	res[0].name	= "dwc2";
1319024c495SJohn Youn 	res[0].flags	= IORESOURCE_MEM;
1329024c495SJohn Youn 
1339024c495SJohn Youn 	res[1].start	= pci->irq;
1349024c495SJohn Youn 	res[1].name	= "dwc2";
1359024c495SJohn Youn 	res[1].flags	= IORESOURCE_IRQ;
1369024c495SJohn Youn 
1379024c495SJohn Youn 	ret = platform_device_add_resources(dwc2, res, ARRAY_SIZE(res));
1389024c495SJohn Youn 	if (ret) {
1399024c495SJohn Youn 		dev_err(dev, "couldn't add resources to dwc2 device\n");
140ecd29dabSVardan Mikayelyan 		goto err;
1419024c495SJohn Youn 	}
1429024c495SJohn Youn 
1439024c495SJohn Youn 	dwc2->dev.parent = dev;
1449024c495SJohn Youn 
145ced8a03cSVahram Aharonyan 	ret = dwc2_pci_quirks(pci, dwc2);
146ced8a03cSVahram Aharonyan 	if (ret)
147ced8a03cSVahram Aharonyan 		goto err;
148ced8a03cSVahram Aharonyan 
149fb50aacdSVardan Mikayelyan 	glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
150fe70dce9SWei Yongjun 	if (!glue) {
151fe70dce9SWei Yongjun 		ret = -ENOMEM;
152ecd29dabSVardan Mikayelyan 		goto err;
153fe70dce9SWei Yongjun 	}
154fb50aacdSVardan Mikayelyan 
1559024c495SJohn Youn 	ret = platform_device_add(dwc2);
1569024c495SJohn Youn 	if (ret) {
1579024c495SJohn Youn 		dev_err(dev, "failed to register dwc2 device\n");
1589024c495SJohn Youn 		goto err;
1599024c495SJohn Youn 	}
1609024c495SJohn Youn 
1619024c495SJohn Youn 	glue->phy = phy;
1629024c495SJohn Youn 	glue->dwc2 = dwc2;
1639024c495SJohn Youn 	pci_set_drvdata(pci, glue);
1649024c495SJohn Youn 
1659024c495SJohn Youn 	return 0;
1669024c495SJohn Youn err:
1679024c495SJohn Youn 	usb_phy_generic_unregister(phy);
1689024c495SJohn Youn 	platform_device_put(dwc2);
1699024c495SJohn Youn 	return ret;
170197ba5f4SPaul Zimmerman }
171197ba5f4SPaul Zimmerman 
172197ba5f4SPaul Zimmerman static const struct pci_device_id dwc2_pci_ids[] = {
173197ba5f4SPaul Zimmerman 	{
174197ba5f4SPaul Zimmerman 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS, PCI_PRODUCT_ID_HAPS_HSOTG),
175197ba5f4SPaul Zimmerman 	},
176197ba5f4SPaul Zimmerman 	{
177197ba5f4SPaul Zimmerman 		PCI_DEVICE(PCI_VENDOR_ID_STMICRO,
178197ba5f4SPaul Zimmerman 			   PCI_DEVICE_ID_STMICRO_USB_OTG),
179197ba5f4SPaul Zimmerman 	},
180197ba5f4SPaul Zimmerman 	{ /* end: all zeroes */ }
181197ba5f4SPaul Zimmerman };
182197ba5f4SPaul Zimmerman MODULE_DEVICE_TABLE(pci, dwc2_pci_ids);
183197ba5f4SPaul Zimmerman 
184197ba5f4SPaul Zimmerman static struct pci_driver dwc2_pci_driver = {
185197ba5f4SPaul Zimmerman 	.name = dwc2_driver_name,
186197ba5f4SPaul Zimmerman 	.id_table = dwc2_pci_ids,
1879024c495SJohn Youn 	.probe = dwc2_pci_probe,
1889024c495SJohn Youn 	.remove = dwc2_pci_remove,
189197ba5f4SPaul Zimmerman };
190197ba5f4SPaul Zimmerman 
191197ba5f4SPaul Zimmerman module_pci_driver(dwc2_pci_driver);
192197ba5f4SPaul Zimmerman 
193197ba5f4SPaul Zimmerman MODULE_DESCRIPTION("DESIGNWARE HS OTG PCI Bus Glue");
194197ba5f4SPaul Zimmerman MODULE_AUTHOR("Synopsys, Inc.");
195197ba5f4SPaul Zimmerman MODULE_LICENSE("Dual BSD/GPL");
196