xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 3fa4d7344be0afebd80382ffeea6b1787cccf971)
172246da4SFelipe Balbi /**
272246da4SFelipe Balbi  * dwc3-pci.c - PCI Specific glue layer
372246da4SFelipe Balbi  *
472246da4SFelipe Balbi  * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
572246da4SFelipe Balbi  *
672246da4SFelipe Balbi  * Authors: Felipe Balbi <balbi@ti.com>,
772246da4SFelipe Balbi  *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
872246da4SFelipe Balbi  *
972246da4SFelipe Balbi  * Redistribution and use in source and binary forms, with or without
1072246da4SFelipe Balbi  * modification, are permitted provided that the following conditions
1172246da4SFelipe Balbi  * are met:
1272246da4SFelipe Balbi  * 1. Redistributions of source code must retain the above copyright
1372246da4SFelipe Balbi  *    notice, this list of conditions, and the following disclaimer,
1472246da4SFelipe Balbi  *    without modification.
1572246da4SFelipe Balbi  * 2. Redistributions in binary form must reproduce the above copyright
1672246da4SFelipe Balbi  *    notice, this list of conditions and the following disclaimer in the
1772246da4SFelipe Balbi  *    documentation and/or other materials provided with the distribution.
1872246da4SFelipe Balbi  * 3. The names of the above-listed copyright holders may not be used
1972246da4SFelipe Balbi  *    to endorse or promote products derived from this software without
2072246da4SFelipe Balbi  *    specific prior written permission.
2172246da4SFelipe Balbi  *
2272246da4SFelipe Balbi  * ALTERNATIVELY, this software may be distributed under the terms of the
2372246da4SFelipe Balbi  * GNU General Public License ("GPL") version 2, as published by the Free
2472246da4SFelipe Balbi  * Software Foundation.
2572246da4SFelipe Balbi  *
2672246da4SFelipe Balbi  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2772246da4SFelipe Balbi  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2872246da4SFelipe Balbi  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2972246da4SFelipe Balbi  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3072246da4SFelipe Balbi  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3172246da4SFelipe Balbi  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3272246da4SFelipe Balbi  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3372246da4SFelipe Balbi  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3472246da4SFelipe Balbi  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3572246da4SFelipe Balbi  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3672246da4SFelipe Balbi  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3772246da4SFelipe Balbi  */
3872246da4SFelipe Balbi 
3972246da4SFelipe Balbi #include <linux/kernel.h>
4046a57283SStephen Rothwell #include <linux/module.h>
4172246da4SFelipe Balbi #include <linux/slab.h>
4272246da4SFelipe Balbi #include <linux/pci.h>
4372246da4SFelipe Balbi #include <linux/platform_device.h>
4472246da4SFelipe Balbi 
45e3ec3eb7SFelipe Balbi #include <linux/usb/otg.h>
46*3fa4d734SSebastian Andrzej Siewior #include <linux/usb/usb_phy_gen_xceiv.h>
47e3ec3eb7SFelipe Balbi 
4872246da4SFelipe Balbi /* FIXME define these in <linux/pci_ids.h> */
4972246da4SFelipe Balbi #define PCI_VENDOR_ID_SYNOPSYS		0x16c3
5072246da4SFelipe Balbi #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3	0xabcd
5172246da4SFelipe Balbi 
5272246da4SFelipe Balbi struct dwc3_pci {
5372246da4SFelipe Balbi 	struct device		*dev;
5472246da4SFelipe Balbi 	struct platform_device	*dwc3;
55e3ec3eb7SFelipe Balbi 	struct platform_device	*usb2_phy;
56e3ec3eb7SFelipe Balbi 	struct platform_device	*usb3_phy;
5772246da4SFelipe Balbi };
5872246da4SFelipe Balbi 
5941ac7b3aSBill Pemberton static int dwc3_pci_register_phys(struct dwc3_pci *glue)
60e3ec3eb7SFelipe Balbi {
61*3fa4d734SSebastian Andrzej Siewior 	struct usb_phy_gen_xceiv_platform_data pdata;
62e3ec3eb7SFelipe Balbi 	struct platform_device	*pdev;
63e3ec3eb7SFelipe Balbi 	int			ret;
64e3ec3eb7SFelipe Balbi 
65e3ec3eb7SFelipe Balbi 	memset(&pdata, 0x00, sizeof(pdata));
66e3ec3eb7SFelipe Balbi 
67*3fa4d734SSebastian Andrzej Siewior 	pdev = platform_device_alloc("usb_phy_gen_xceiv", 0);
68e3ec3eb7SFelipe Balbi 	if (!pdev)
69e3ec3eb7SFelipe Balbi 		return -ENOMEM;
70e3ec3eb7SFelipe Balbi 
71e3ec3eb7SFelipe Balbi 	glue->usb2_phy = pdev;
72e3ec3eb7SFelipe Balbi 	pdata.type = USB_PHY_TYPE_USB2;
73e3ec3eb7SFelipe Balbi 
74e3ec3eb7SFelipe Balbi 	ret = platform_device_add_data(glue->usb2_phy, &pdata, sizeof(pdata));
75e3ec3eb7SFelipe Balbi 	if (ret)
76e3ec3eb7SFelipe Balbi 		goto err1;
77e3ec3eb7SFelipe Balbi 
78*3fa4d734SSebastian Andrzej Siewior 	pdev = platform_device_alloc("usb_phy_gen_xceiv", 1);
79e3ec3eb7SFelipe Balbi 	if (!pdev) {
80e3ec3eb7SFelipe Balbi 		ret = -ENOMEM;
81e3ec3eb7SFelipe Balbi 		goto err1;
82e3ec3eb7SFelipe Balbi 	}
83e3ec3eb7SFelipe Balbi 
84e3ec3eb7SFelipe Balbi 	glue->usb3_phy = pdev;
85e3ec3eb7SFelipe Balbi 	pdata.type = USB_PHY_TYPE_USB3;
86e3ec3eb7SFelipe Balbi 
87e3ec3eb7SFelipe Balbi 	ret = platform_device_add_data(glue->usb3_phy, &pdata, sizeof(pdata));
88e3ec3eb7SFelipe Balbi 	if (ret)
89e3ec3eb7SFelipe Balbi 		goto err2;
90e3ec3eb7SFelipe Balbi 
91e3ec3eb7SFelipe Balbi 	ret = platform_device_add(glue->usb2_phy);
92e3ec3eb7SFelipe Balbi 	if (ret)
93e3ec3eb7SFelipe Balbi 		goto err2;
94e3ec3eb7SFelipe Balbi 
95e3ec3eb7SFelipe Balbi 	ret = platform_device_add(glue->usb3_phy);
96e3ec3eb7SFelipe Balbi 	if (ret)
97e3ec3eb7SFelipe Balbi 		goto err3;
98e3ec3eb7SFelipe Balbi 
99e3ec3eb7SFelipe Balbi 	return 0;
100e3ec3eb7SFelipe Balbi 
101e3ec3eb7SFelipe Balbi err3:
102e3ec3eb7SFelipe Balbi 	platform_device_del(glue->usb2_phy);
103e3ec3eb7SFelipe Balbi 
104e3ec3eb7SFelipe Balbi err2:
105e3ec3eb7SFelipe Balbi 	platform_device_put(glue->usb3_phy);
106e3ec3eb7SFelipe Balbi 
107e3ec3eb7SFelipe Balbi err1:
108e3ec3eb7SFelipe Balbi 	platform_device_put(glue->usb2_phy);
109e3ec3eb7SFelipe Balbi 
110e3ec3eb7SFelipe Balbi 	return ret;
111e3ec3eb7SFelipe Balbi }
112e3ec3eb7SFelipe Balbi 
11341ac7b3aSBill Pemberton static int dwc3_pci_probe(struct pci_dev *pci,
11472246da4SFelipe Balbi 		const struct pci_device_id *id)
11572246da4SFelipe Balbi {
11672246da4SFelipe Balbi 	struct resource		res[2];
11772246da4SFelipe Balbi 	struct platform_device	*dwc3;
11872246da4SFelipe Balbi 	struct dwc3_pci		*glue;
11972246da4SFelipe Balbi 	int			ret = -ENOMEM;
120802ca850SChanho Park 	struct device		*dev = &pci->dev;
12172246da4SFelipe Balbi 
122802ca850SChanho Park 	glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
12372246da4SFelipe Balbi 	if (!glue) {
124802ca850SChanho Park 		dev_err(dev, "not enough memory\n");
125802ca850SChanho Park 		return -ENOMEM;
12672246da4SFelipe Balbi 	}
12772246da4SFelipe Balbi 
128802ca850SChanho Park 	glue->dev = dev;
12972246da4SFelipe Balbi 
13072246da4SFelipe Balbi 	ret = pci_enable_device(pci);
13172246da4SFelipe Balbi 	if (ret) {
132802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
133802ca850SChanho Park 		return -ENODEV;
13472246da4SFelipe Balbi 	}
13572246da4SFelipe Balbi 
13672246da4SFelipe Balbi 	pci_set_master(pci);
13772246da4SFelipe Balbi 
138e3ec3eb7SFelipe Balbi 	ret = dwc3_pci_register_phys(glue);
139e3ec3eb7SFelipe Balbi 	if (ret) {
140e3ec3eb7SFelipe Balbi 		dev_err(dev, "couldn't register PHYs\n");
141e3ec3eb7SFelipe Balbi 		return ret;
142e3ec3eb7SFelipe Balbi 	}
143e3ec3eb7SFelipe Balbi 
144124dafdeSSebastian Andrzej Siewior 	dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
14572246da4SFelipe Balbi 	if (!dwc3) {
146802ca850SChanho Park 		dev_err(dev, "couldn't allocate dwc3 device\n");
14728f1a0d9SFelipe Balbi 		ret = -ENOMEM;
148802ca850SChanho Park 		goto err1;
14972246da4SFelipe Balbi 	}
15072246da4SFelipe Balbi 
15172246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
15272246da4SFelipe Balbi 
15372246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
15472246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
15572246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
15672246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
15772246da4SFelipe Balbi 
15872246da4SFelipe Balbi 	res[1].start	= pci->irq;
15972246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
16072246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
16172246da4SFelipe Balbi 
16272246da4SFelipe Balbi 	ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
16372246da4SFelipe Balbi 	if (ret) {
164802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
165124dafdeSSebastian Andrzej Siewior 		goto err1;
16672246da4SFelipe Balbi 	}
16772246da4SFelipe Balbi 
16872246da4SFelipe Balbi 	pci_set_drvdata(pci, glue);
16972246da4SFelipe Balbi 
170802ca850SChanho Park 	dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
17172246da4SFelipe Balbi 
172802ca850SChanho Park 	dwc3->dev.dma_mask = dev->dma_mask;
173802ca850SChanho Park 	dwc3->dev.dma_parms = dev->dma_parms;
174802ca850SChanho Park 	dwc3->dev.parent = dev;
17572246da4SFelipe Balbi 	glue->dwc3 = dwc3;
17672246da4SFelipe Balbi 
17772246da4SFelipe Balbi 	ret = platform_device_add(dwc3);
17872246da4SFelipe Balbi 	if (ret) {
179802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
180802ca850SChanho Park 		goto err3;
18172246da4SFelipe Balbi 	}
18272246da4SFelipe Balbi 
18372246da4SFelipe Balbi 	return 0;
18472246da4SFelipe Balbi 
185802ca850SChanho Park err3:
18672246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
18772246da4SFelipe Balbi 	platform_device_put(dwc3);
188802ca850SChanho Park err1:
18972246da4SFelipe Balbi 	pci_disable_device(pci);
19072246da4SFelipe Balbi 
19172246da4SFelipe Balbi 	return ret;
19272246da4SFelipe Balbi }
19372246da4SFelipe Balbi 
194fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
19572246da4SFelipe Balbi {
19672246da4SFelipe Balbi 	struct dwc3_pci	*glue = pci_get_drvdata(pci);
19772246da4SFelipe Balbi 
198f28c42c5SPeter Chen 	platform_device_unregister(glue->dwc3);
199e3ec3eb7SFelipe Balbi 	platform_device_unregister(glue->usb2_phy);
200e3ec3eb7SFelipe Balbi 	platform_device_unregister(glue->usb3_phy);
20172246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
20272246da4SFelipe Balbi 	pci_disable_device(pci);
20372246da4SFelipe Balbi }
20472246da4SFelipe Balbi 
20572246da4SFelipe Balbi static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
20672246da4SFelipe Balbi 	{
20772246da4SFelipe Balbi 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
20872246da4SFelipe Balbi 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
20972246da4SFelipe Balbi 	},
21072246da4SFelipe Balbi 	{  }	/* Terminating Entry */
21172246da4SFelipe Balbi };
21272246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
21372246da4SFelipe Balbi 
21468907a77SFelipe Balbi #ifdef CONFIG_PM
21568907a77SFelipe Balbi static int dwc3_pci_suspend(struct device *dev)
21668907a77SFelipe Balbi {
21768907a77SFelipe Balbi 	struct pci_dev	*pci = to_pci_dev(dev);
21868907a77SFelipe Balbi 
21968907a77SFelipe Balbi 	pci_disable_device(pci);
22068907a77SFelipe Balbi 
22168907a77SFelipe Balbi 	return 0;
22268907a77SFelipe Balbi }
22368907a77SFelipe Balbi 
22468907a77SFelipe Balbi static int dwc3_pci_resume(struct device *dev)
22568907a77SFelipe Balbi {
22668907a77SFelipe Balbi 	struct pci_dev	*pci = to_pci_dev(dev);
22768907a77SFelipe Balbi 	int		ret;
22868907a77SFelipe Balbi 
22968907a77SFelipe Balbi 	ret = pci_enable_device(pci);
23068907a77SFelipe Balbi 	if (ret) {
23168907a77SFelipe Balbi 		dev_err(dev, "can't re-enable device --> %d\n", ret);
23268907a77SFelipe Balbi 		return ret;
23368907a77SFelipe Balbi 	}
23468907a77SFelipe Balbi 
23568907a77SFelipe Balbi 	pci_set_master(pci);
23668907a77SFelipe Balbi 
23768907a77SFelipe Balbi 	return 0;
23868907a77SFelipe Balbi }
23968907a77SFelipe Balbi 
24068907a77SFelipe Balbi static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
24168907a77SFelipe Balbi 	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
24268907a77SFelipe Balbi };
24368907a77SFelipe Balbi 
24468907a77SFelipe Balbi #define DEV_PM_OPS	(&dwc3_pci_dev_pm_ops)
24568907a77SFelipe Balbi #else
24668907a77SFelipe Balbi #define DEV_PM_OPS	NULL
24768907a77SFelipe Balbi #endif /* CONFIG_PM */
24868907a77SFelipe Balbi 
24972246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
2500949e99bSFelipe Balbi 	.name		= "dwc3-pci",
25172246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
25272246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
2537690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
25468907a77SFelipe Balbi 	.driver		= {
25568907a77SFelipe Balbi 		.pm	= DEV_PM_OPS,
25668907a77SFelipe Balbi 	},
25772246da4SFelipe Balbi };
25872246da4SFelipe Balbi 
25972246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
26072246da4SFelipe Balbi MODULE_LICENSE("Dual BSD/GPL");
26172246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
26272246da4SFelipe Balbi 
26395656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
264