xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 7d26b58735f5badf2b7ce3320c6ba21b603c77a9)
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 
458300dd23SFelipe Balbi #include "core.h"
468300dd23SFelipe Balbi 
4772246da4SFelipe Balbi /* FIXME define these in <linux/pci_ids.h> */
4872246da4SFelipe Balbi #define PCI_VENDOR_ID_SYNOPSYS		0x16c3
4972246da4SFelipe Balbi #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3	0xabcd
5072246da4SFelipe Balbi 
5172246da4SFelipe Balbi struct dwc3_pci {
5272246da4SFelipe Balbi 	struct device		*dev;
5372246da4SFelipe Balbi 	struct platform_device	*dwc3;
5472246da4SFelipe Balbi };
5572246da4SFelipe Balbi 
5672246da4SFelipe Balbi static int __devinit dwc3_pci_probe(struct pci_dev *pci,
5772246da4SFelipe Balbi 		const struct pci_device_id *id)
5872246da4SFelipe Balbi {
5972246da4SFelipe Balbi 	struct resource		res[2];
6072246da4SFelipe Balbi 	struct platform_device	*dwc3;
6172246da4SFelipe Balbi 	struct dwc3_pci		*glue;
6272246da4SFelipe Balbi 	int			ret = -ENOMEM;
6372246da4SFelipe Balbi 	int			devid;
64802ca850SChanho Park 	struct device		*dev = &pci->dev;
6572246da4SFelipe Balbi 
66802ca850SChanho Park 	glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
6772246da4SFelipe Balbi 	if (!glue) {
68802ca850SChanho Park 		dev_err(dev, "not enough memory\n");
69802ca850SChanho Park 		return -ENOMEM;
7072246da4SFelipe Balbi 	}
7172246da4SFelipe Balbi 
72802ca850SChanho Park 	glue->dev = dev;
7372246da4SFelipe Balbi 
7472246da4SFelipe Balbi 	ret = pci_enable_device(pci);
7572246da4SFelipe Balbi 	if (ret) {
76802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
77802ca850SChanho Park 		return -ENODEV;
7872246da4SFelipe Balbi 	}
7972246da4SFelipe Balbi 
8072246da4SFelipe Balbi 	pci_set_power_state(pci, PCI_D0);
8172246da4SFelipe Balbi 	pci_set_master(pci);
8272246da4SFelipe Balbi 
838300dd23SFelipe Balbi 	devid = dwc3_get_device_id();
84*7d26b587SPaul Zimmerman 	if (devid < 0) {
85*7d26b587SPaul Zimmerman 		ret = -ENOMEM;
86802ca850SChanho Park 		goto err1;
87*7d26b587SPaul Zimmerman 	}
8872246da4SFelipe Balbi 
890949e99bSFelipe Balbi 	dwc3 = platform_device_alloc("dwc3", devid);
9072246da4SFelipe Balbi 	if (!dwc3) {
91802ca850SChanho Park 		dev_err(dev, "couldn't allocate dwc3 device\n");
92802ca850SChanho Park 		goto err1;
9372246da4SFelipe Balbi 	}
9472246da4SFelipe Balbi 
9572246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
9672246da4SFelipe Balbi 
9772246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
9872246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
9972246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
10072246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
10172246da4SFelipe Balbi 
10272246da4SFelipe Balbi 	res[1].start	= pci->irq;
10372246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
10472246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
10572246da4SFelipe Balbi 
10672246da4SFelipe Balbi 	ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
10772246da4SFelipe Balbi 	if (ret) {
108802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
109802ca850SChanho Park 		goto err2;
11072246da4SFelipe Balbi 	}
11172246da4SFelipe Balbi 
11272246da4SFelipe Balbi 	pci_set_drvdata(pci, glue);
11372246da4SFelipe Balbi 
114802ca850SChanho Park 	dma_set_coherent_mask(&dwc3->dev, dev->coherent_dma_mask);
11572246da4SFelipe Balbi 
116802ca850SChanho Park 	dwc3->dev.dma_mask = dev->dma_mask;
117802ca850SChanho Park 	dwc3->dev.dma_parms = dev->dma_parms;
118802ca850SChanho Park 	dwc3->dev.parent = dev;
11972246da4SFelipe Balbi 	glue->dwc3 = dwc3;
12072246da4SFelipe Balbi 
12172246da4SFelipe Balbi 	ret = platform_device_add(dwc3);
12272246da4SFelipe Balbi 	if (ret) {
123802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
124802ca850SChanho Park 		goto err3;
12572246da4SFelipe Balbi 	}
12672246da4SFelipe Balbi 
12772246da4SFelipe Balbi 	return 0;
12872246da4SFelipe Balbi 
129802ca850SChanho Park err3:
13072246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
13172246da4SFelipe Balbi 	platform_device_put(dwc3);
13272246da4SFelipe Balbi 
133802ca850SChanho Park err2:
1348300dd23SFelipe Balbi 	dwc3_put_device_id(devid);
13572246da4SFelipe Balbi 
136802ca850SChanho Park err1:
13772246da4SFelipe Balbi 	pci_disable_device(pci);
13872246da4SFelipe Balbi 
13972246da4SFelipe Balbi 	return ret;
14072246da4SFelipe Balbi }
14172246da4SFelipe Balbi 
14272246da4SFelipe Balbi static void __devexit dwc3_pci_remove(struct pci_dev *pci)
14372246da4SFelipe Balbi {
14472246da4SFelipe Balbi 	struct dwc3_pci	*glue = pci_get_drvdata(pci);
14572246da4SFelipe Balbi 
1468300dd23SFelipe Balbi 	dwc3_put_device_id(glue->dwc3->id);
14772246da4SFelipe Balbi 	platform_device_unregister(glue->dwc3);
14872246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
14972246da4SFelipe Balbi 	pci_disable_device(pci);
15072246da4SFelipe Balbi }
15172246da4SFelipe Balbi 
15272246da4SFelipe Balbi static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
15372246da4SFelipe Balbi 	{
15472246da4SFelipe Balbi 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
15572246da4SFelipe Balbi 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
15672246da4SFelipe Balbi 	},
15772246da4SFelipe Balbi 	{  }	/* Terminating Entry */
15872246da4SFelipe Balbi };
15972246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
16072246da4SFelipe Balbi 
16172246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
1620949e99bSFelipe Balbi 	.name		= "dwc3-pci",
16372246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
16472246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
16572246da4SFelipe Balbi 	.remove		= __devexit_p(dwc3_pci_remove),
16672246da4SFelipe Balbi };
16772246da4SFelipe Balbi 
16872246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
16972246da4SFelipe Balbi MODULE_LICENSE("Dual BSD/GPL");
17072246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
17172246da4SFelipe Balbi 
17272246da4SFelipe Balbi static int __devinit dwc3_pci_init(void)
17372246da4SFelipe Balbi {
17472246da4SFelipe Balbi 	return pci_register_driver(&dwc3_pci_driver);
17572246da4SFelipe Balbi }
17672246da4SFelipe Balbi module_init(dwc3_pci_init);
17772246da4SFelipe Balbi 
17872246da4SFelipe Balbi static void __exit dwc3_pci_exit(void)
17972246da4SFelipe Balbi {
18072246da4SFelipe Balbi 	pci_unregister_driver(&dwc3_pci_driver);
18172246da4SFelipe Balbi }
18272246da4SFelipe Balbi module_exit(dwc3_pci_exit);
183