xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 46a57283e86c68351377ac3349f1465aee938fbd)
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  * All rights reserved.
672246da4SFelipe Balbi  *
772246da4SFelipe Balbi  * Authors: Felipe Balbi <balbi@ti.com>,
872246da4SFelipe Balbi  *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
972246da4SFelipe Balbi  *
1072246da4SFelipe Balbi  * Redistribution and use in source and binary forms, with or without
1172246da4SFelipe Balbi  * modification, are permitted provided that the following conditions
1272246da4SFelipe Balbi  * are met:
1372246da4SFelipe Balbi  * 1. Redistributions of source code must retain the above copyright
1472246da4SFelipe Balbi  *    notice, this list of conditions, and the following disclaimer,
1572246da4SFelipe Balbi  *    without modification.
1672246da4SFelipe Balbi  * 2. Redistributions in binary form must reproduce the above copyright
1772246da4SFelipe Balbi  *    notice, this list of conditions and the following disclaimer in the
1872246da4SFelipe Balbi  *    documentation and/or other materials provided with the distribution.
1972246da4SFelipe Balbi  * 3. The names of the above-listed copyright holders may not be used
2072246da4SFelipe Balbi  *    to endorse or promote products derived from this software without
2172246da4SFelipe Balbi  *    specific prior written permission.
2272246da4SFelipe Balbi  *
2372246da4SFelipe Balbi  * ALTERNATIVELY, this software may be distributed under the terms of the
2472246da4SFelipe Balbi  * GNU General Public License ("GPL") version 2, as published by the Free
2572246da4SFelipe Balbi  * Software Foundation.
2672246da4SFelipe Balbi  *
2772246da4SFelipe Balbi  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2872246da4SFelipe Balbi  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2972246da4SFelipe Balbi  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3072246da4SFelipe Balbi  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
3172246da4SFelipe Balbi  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
3272246da4SFelipe Balbi  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
3372246da4SFelipe Balbi  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3472246da4SFelipe Balbi  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3572246da4SFelipe Balbi  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3672246da4SFelipe Balbi  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3772246da4SFelipe Balbi  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3872246da4SFelipe Balbi  */
3972246da4SFelipe Balbi 
4072246da4SFelipe Balbi #include <linux/kernel.h>
41*46a57283SStephen Rothwell #include <linux/module.h>
4272246da4SFelipe Balbi #include <linux/slab.h>
4372246da4SFelipe Balbi #include <linux/pci.h>
4472246da4SFelipe Balbi #include <linux/platform_device.h>
4572246da4SFelipe Balbi 
4672246da4SFelipe Balbi /* FIXME define these in <linux/pci_ids.h> */
4772246da4SFelipe Balbi #define PCI_VENDOR_ID_SYNOPSYS		0x16c3
4872246da4SFelipe Balbi #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3	0xabcd
4972246da4SFelipe Balbi 
5072246da4SFelipe Balbi #define DWC3_PCI_DEVS_POSSIBLE	32
5172246da4SFelipe Balbi 
5272246da4SFelipe Balbi struct dwc3_pci {
5372246da4SFelipe Balbi 	struct device		*dev;
5472246da4SFelipe Balbi 	struct platform_device	*dwc3;
5572246da4SFelipe Balbi };
5672246da4SFelipe Balbi 
5772246da4SFelipe Balbi static DECLARE_BITMAP(dwc3_pci_devs, DWC3_PCI_DEVS_POSSIBLE);
5872246da4SFelipe Balbi 
5972246da4SFelipe Balbi static int dwc3_pci_get_device_id(struct dwc3_pci *glue)
6072246da4SFelipe Balbi {
6172246da4SFelipe Balbi 	int		id;
6272246da4SFelipe Balbi 
6372246da4SFelipe Balbi again:
6472246da4SFelipe Balbi 	id = find_first_zero_bit(dwc3_pci_devs, DWC3_PCI_DEVS_POSSIBLE);
6572246da4SFelipe Balbi 	if (id < DWC3_PCI_DEVS_POSSIBLE) {
6672246da4SFelipe Balbi 		int old;
6772246da4SFelipe Balbi 
6872246da4SFelipe Balbi 		old = test_and_set_bit(id, dwc3_pci_devs);
6972246da4SFelipe Balbi 		if (old)
7072246da4SFelipe Balbi 			goto again;
7172246da4SFelipe Balbi 	} else {
7272246da4SFelipe Balbi 		dev_err(glue->dev, "no space for new device\n");
7372246da4SFelipe Balbi 		id = -ENOMEM;
7472246da4SFelipe Balbi 	}
7572246da4SFelipe Balbi 
7672246da4SFelipe Balbi 	return 0;
7772246da4SFelipe Balbi }
7872246da4SFelipe Balbi 
7972246da4SFelipe Balbi static void dwc3_pci_put_device_id(struct dwc3_pci *glue, int id)
8072246da4SFelipe Balbi {
8172246da4SFelipe Balbi 	int			ret;
8272246da4SFelipe Balbi 
8372246da4SFelipe Balbi 	if (id < 0)
8472246da4SFelipe Balbi 		return;
8572246da4SFelipe Balbi 
8672246da4SFelipe Balbi 	ret = test_bit(id, dwc3_pci_devs);
8772246da4SFelipe Balbi 	WARN(!ret, "Device: %s\nID %d not in use\n",
8872246da4SFelipe Balbi 			dev_driver_string(glue->dev), id);
8972246da4SFelipe Balbi 	clear_bit(id, dwc3_pci_devs);
9072246da4SFelipe Balbi }
9172246da4SFelipe Balbi 
9272246da4SFelipe Balbi static int __devinit dwc3_pci_probe(struct pci_dev *pci,
9372246da4SFelipe Balbi 		const struct pci_device_id *id)
9472246da4SFelipe Balbi {
9572246da4SFelipe Balbi 	struct resource		res[2];
9672246da4SFelipe Balbi 	struct platform_device	*dwc3;
9772246da4SFelipe Balbi 	struct dwc3_pci		*glue;
9872246da4SFelipe Balbi 	int			ret = -ENOMEM;
9972246da4SFelipe Balbi 	int			devid;
10072246da4SFelipe Balbi 
10172246da4SFelipe Balbi 	glue = kzalloc(sizeof(*glue), GFP_KERNEL);
10272246da4SFelipe Balbi 	if (!glue) {
10372246da4SFelipe Balbi 		dev_err(&pci->dev, "not enough memory\n");
10472246da4SFelipe Balbi 		goto err0;
10572246da4SFelipe Balbi 	}
10672246da4SFelipe Balbi 
10772246da4SFelipe Balbi 	glue->dev	= &pci->dev;
10872246da4SFelipe Balbi 
10972246da4SFelipe Balbi 	ret = pci_enable_device(pci);
11072246da4SFelipe Balbi 	if (ret) {
11172246da4SFelipe Balbi 		dev_err(&pci->dev, "failed to enable pci device\n");
11272246da4SFelipe Balbi 		goto err1;
11372246da4SFelipe Balbi 	}
11472246da4SFelipe Balbi 
11572246da4SFelipe Balbi 	pci_set_power_state(pci, PCI_D0);
11672246da4SFelipe Balbi 	pci_set_master(pci);
11772246da4SFelipe Balbi 
11872246da4SFelipe Balbi 	devid = dwc3_pci_get_device_id(glue);
11972246da4SFelipe Balbi 	if (devid < 0)
12072246da4SFelipe Balbi 		goto err2;
12172246da4SFelipe Balbi 
12272246da4SFelipe Balbi 	dwc3 = platform_device_alloc("dwc3-pci", devid);
12372246da4SFelipe Balbi 	if (!dwc3) {
12472246da4SFelipe Balbi 		dev_err(&pci->dev, "couldn't allocate dwc3 device\n");
12572246da4SFelipe Balbi 		goto err3;
12672246da4SFelipe Balbi 	}
12772246da4SFelipe Balbi 
12872246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
12972246da4SFelipe Balbi 
13072246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
13172246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
13272246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
13372246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
13472246da4SFelipe Balbi 
13572246da4SFelipe Balbi 	res[1].start	= pci->irq;
13672246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
13772246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
13872246da4SFelipe Balbi 
13972246da4SFelipe Balbi 	ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
14072246da4SFelipe Balbi 	if (ret) {
14172246da4SFelipe Balbi 		dev_err(&pci->dev, "couldn't add resources to dwc3 device\n");
14272246da4SFelipe Balbi 		goto err4;
14372246da4SFelipe Balbi 	}
14472246da4SFelipe Balbi 
14572246da4SFelipe Balbi 	pci_set_drvdata(pci, glue);
14672246da4SFelipe Balbi 
14772246da4SFelipe Balbi 	dma_set_coherent_mask(&dwc3->dev, pci->dev.coherent_dma_mask);
14872246da4SFelipe Balbi 
14972246da4SFelipe Balbi 	dwc3->dev.dma_mask = pci->dev.dma_mask;
15072246da4SFelipe Balbi 	dwc3->dev.dma_parms = pci->dev.dma_parms;
15172246da4SFelipe Balbi 	dwc3->dev.parent = &pci->dev;
15272246da4SFelipe Balbi 	glue->dwc3	= dwc3;
15372246da4SFelipe Balbi 
15472246da4SFelipe Balbi 	ret = platform_device_add(dwc3);
15572246da4SFelipe Balbi 	if (ret) {
15672246da4SFelipe Balbi 		dev_err(&pci->dev, "failed to register dwc3 device\n");
15772246da4SFelipe Balbi 		goto err4;
15872246da4SFelipe Balbi 	}
15972246da4SFelipe Balbi 
16072246da4SFelipe Balbi 	return 0;
16172246da4SFelipe Balbi 
16272246da4SFelipe Balbi err4:
16372246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
16472246da4SFelipe Balbi 	platform_device_put(dwc3);
16572246da4SFelipe Balbi 
16672246da4SFelipe Balbi err3:
16772246da4SFelipe Balbi 	dwc3_pci_put_device_id(glue, devid);
16872246da4SFelipe Balbi 
16972246da4SFelipe Balbi err2:
17072246da4SFelipe Balbi 	pci_disable_device(pci);
17172246da4SFelipe Balbi 
17272246da4SFelipe Balbi err1:
17372246da4SFelipe Balbi 	kfree(pci);
17472246da4SFelipe Balbi 
17572246da4SFelipe Balbi err0:
17672246da4SFelipe Balbi 	return ret;
17772246da4SFelipe Balbi }
17872246da4SFelipe Balbi 
17972246da4SFelipe Balbi static void __devexit dwc3_pci_remove(struct pci_dev *pci)
18072246da4SFelipe Balbi {
18172246da4SFelipe Balbi 	struct dwc3_pci	*glue = pci_get_drvdata(pci);
18272246da4SFelipe Balbi 
18372246da4SFelipe Balbi 	dwc3_pci_put_device_id(glue, glue->dwc3->id);
18472246da4SFelipe Balbi 	platform_device_unregister(glue->dwc3);
18572246da4SFelipe Balbi 	pci_set_drvdata(pci, NULL);
18672246da4SFelipe Balbi 	pci_disable_device(pci);
18772246da4SFelipe Balbi 	kfree(glue);
18872246da4SFelipe Balbi }
18972246da4SFelipe Balbi 
19072246da4SFelipe Balbi static DEFINE_PCI_DEVICE_TABLE(dwc3_pci_id_table) = {
19172246da4SFelipe Balbi 	{
19272246da4SFelipe Balbi 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
19372246da4SFelipe Balbi 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
19472246da4SFelipe Balbi 	},
19572246da4SFelipe Balbi 	{  }	/* Terminating Entry */
19672246da4SFelipe Balbi };
19772246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
19872246da4SFelipe Balbi 
19972246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
20072246da4SFelipe Balbi 	.name		= "pci-dwc3",
20172246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
20272246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
20372246da4SFelipe Balbi 	.remove		= __devexit_p(dwc3_pci_remove),
20472246da4SFelipe Balbi };
20572246da4SFelipe Balbi 
20672246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
20772246da4SFelipe Balbi MODULE_LICENSE("Dual BSD/GPL");
20872246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
20972246da4SFelipe Balbi 
21072246da4SFelipe Balbi static int __devinit dwc3_pci_init(void)
21172246da4SFelipe Balbi {
21272246da4SFelipe Balbi 	return pci_register_driver(&dwc3_pci_driver);
21372246da4SFelipe Balbi }
21472246da4SFelipe Balbi module_init(dwc3_pci_init);
21572246da4SFelipe Balbi 
21672246da4SFelipe Balbi static void __exit dwc3_pci_exit(void)
21772246da4SFelipe Balbi {
21872246da4SFelipe Balbi 	pci_unregister_driver(&dwc3_pci_driver);
21972246da4SFelipe Balbi }
22072246da4SFelipe Balbi module_exit(dwc3_pci_exit);
221