xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 5632c827cbd3617613530ba0e99344192d0a31ca)
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;
6472246da4SFelipe Balbi 
6572246da4SFelipe Balbi 	glue = kzalloc(sizeof(*glue), GFP_KERNEL);
6672246da4SFelipe Balbi 	if (!glue) {
6772246da4SFelipe Balbi 		dev_err(&pci->dev, "not enough memory\n");
6872246da4SFelipe Balbi 		goto err0;
6972246da4SFelipe Balbi 	}
7072246da4SFelipe Balbi 
7172246da4SFelipe Balbi 	glue->dev	= &pci->dev;
7272246da4SFelipe Balbi 
7372246da4SFelipe Balbi 	ret = pci_enable_device(pci);
7472246da4SFelipe Balbi 	if (ret) {
7572246da4SFelipe Balbi 		dev_err(&pci->dev, "failed to enable pci device\n");
7672246da4SFelipe Balbi 		goto err1;
7772246da4SFelipe Balbi 	}
7872246da4SFelipe Balbi 
7972246da4SFelipe Balbi 	pci_set_power_state(pci, PCI_D0);
8072246da4SFelipe Balbi 	pci_set_master(pci);
8172246da4SFelipe Balbi 
828300dd23SFelipe Balbi 	devid = dwc3_get_device_id();
8372246da4SFelipe Balbi 	if (devid < 0)
8472246da4SFelipe Balbi 		goto err2;
8572246da4SFelipe Balbi 
860949e99bSFelipe Balbi 	dwc3 = platform_device_alloc("dwc3", devid);
8772246da4SFelipe Balbi 	if (!dwc3) {
8872246da4SFelipe Balbi 		dev_err(&pci->dev, "couldn't allocate dwc3 device\n");
8972246da4SFelipe Balbi 		goto err3;
9072246da4SFelipe Balbi 	}
9172246da4SFelipe Balbi 
9272246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
9372246da4SFelipe Balbi 
9472246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
9572246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
9672246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
9772246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
9872246da4SFelipe Balbi 
9972246da4SFelipe Balbi 	res[1].start	= pci->irq;
10072246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
10172246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
10272246da4SFelipe Balbi 
10372246da4SFelipe Balbi 	ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
10472246da4SFelipe Balbi 	if (ret) {
10572246da4SFelipe Balbi 		dev_err(&pci->dev, "couldn't add resources to dwc3 device\n");
10672246da4SFelipe Balbi 		goto err4;
10772246da4SFelipe Balbi 	}
10872246da4SFelipe Balbi 
10972246da4SFelipe Balbi 	pci_set_drvdata(pci, glue);
11072246da4SFelipe Balbi 
11172246da4SFelipe Balbi 	dma_set_coherent_mask(&dwc3->dev, pci->dev.coherent_dma_mask);
11272246da4SFelipe Balbi 
11372246da4SFelipe Balbi 	dwc3->dev.dma_mask = pci->dev.dma_mask;
11472246da4SFelipe Balbi 	dwc3->dev.dma_parms = pci->dev.dma_parms;
11572246da4SFelipe Balbi 	dwc3->dev.parent = &pci->dev;
11672246da4SFelipe Balbi 	glue->dwc3	= dwc3;
11772246da4SFelipe Balbi 
11872246da4SFelipe Balbi 	ret = platform_device_add(dwc3);
11972246da4SFelipe Balbi 	if (ret) {
12072246da4SFelipe Balbi 		dev_err(&pci->dev, "failed to register dwc3 device\n");
12172246da4SFelipe Balbi 		goto err4;
12272246da4SFelipe Balbi 	}
12372246da4SFelipe Balbi 
12472246da4SFelipe Balbi 	return 0;
12572246da4SFelipe Balbi 
12672246da4SFelipe Balbi err4:
12772246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
12872246da4SFelipe Balbi 	platform_device_put(dwc3);
12972246da4SFelipe Balbi 
13072246da4SFelipe Balbi err3:
1318300dd23SFelipe Balbi 	dwc3_put_device_id(devid);
13272246da4SFelipe Balbi 
13372246da4SFelipe Balbi err2:
13472246da4SFelipe Balbi 	pci_disable_device(pci);
13572246da4SFelipe Balbi 
13672246da4SFelipe Balbi err1:
137*5632c827SJulia Lawall 	kfree(glue);
13872246da4SFelipe Balbi 
13972246da4SFelipe Balbi err0:
14072246da4SFelipe Balbi 	return ret;
14172246da4SFelipe Balbi }
14272246da4SFelipe Balbi 
14372246da4SFelipe Balbi static void __devexit dwc3_pci_remove(struct pci_dev *pci)
14472246da4SFelipe Balbi {
14572246da4SFelipe Balbi 	struct dwc3_pci	*glue = pci_get_drvdata(pci);
14672246da4SFelipe Balbi 
1478300dd23SFelipe Balbi 	dwc3_put_device_id(glue->dwc3->id);
14872246da4SFelipe Balbi 	platform_device_unregister(glue->dwc3);
14972246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
15072246da4SFelipe Balbi 	pci_disable_device(pci);
15172246da4SFelipe Balbi 	kfree(glue);
15272246da4SFelipe Balbi }
15372246da4SFelipe Balbi 
15472246da4SFelipe Balbi static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
15572246da4SFelipe Balbi 	{
15672246da4SFelipe Balbi 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
15772246da4SFelipe Balbi 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
15872246da4SFelipe Balbi 	},
15972246da4SFelipe Balbi 	{  }	/* Terminating Entry */
16072246da4SFelipe Balbi };
16172246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
16272246da4SFelipe Balbi 
16372246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
1640949e99bSFelipe Balbi 	.name		= "dwc3-pci",
16572246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
16672246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
16772246da4SFelipe Balbi 	.remove		= __devexit_p(dwc3_pci_remove),
16872246da4SFelipe Balbi };
16972246da4SFelipe Balbi 
17072246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
17172246da4SFelipe Balbi MODULE_LICENSE("Dual BSD/GPL");
17272246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
17372246da4SFelipe Balbi 
17472246da4SFelipe Balbi static int __devinit dwc3_pci_init(void)
17572246da4SFelipe Balbi {
17672246da4SFelipe Balbi 	return pci_register_driver(&dwc3_pci_driver);
17772246da4SFelipe Balbi }
17872246da4SFelipe Balbi module_init(dwc3_pci_init);
17972246da4SFelipe Balbi 
18072246da4SFelipe Balbi static void __exit dwc3_pci_exit(void)
18172246da4SFelipe Balbi {
18272246da4SFelipe Balbi 	pci_unregister_driver(&dwc3_pci_driver);
18372246da4SFelipe Balbi }
18472246da4SFelipe Balbi module_exit(dwc3_pci_exit);
185