xref: /linux/drivers/usb/dwc3/dwc3-pci.c (revision 474799f073763b868e158b04b74f8ddd1da38da2)
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  *
95945f789SFelipe Balbi  * This program is free software: you can redistribute it and/or modify
105945f789SFelipe Balbi  * it under the terms of the GNU General Public License version 2  of
115945f789SFelipe Balbi  * the License as published by the Free Software Foundation.
1272246da4SFelipe Balbi  *
135945f789SFelipe Balbi  * This program is distributed in the hope that it will be useful,
145945f789SFelipe Balbi  * but WITHOUT ANY WARRANTY; without even the implied warranty of
155945f789SFelipe Balbi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165945f789SFelipe Balbi  * GNU General Public License for more details.
1772246da4SFelipe Balbi  */
1872246da4SFelipe Balbi 
1972246da4SFelipe Balbi #include <linux/kernel.h>
2046a57283SStephen Rothwell #include <linux/module.h>
2172246da4SFelipe Balbi #include <linux/slab.h>
2272246da4SFelipe Balbi #include <linux/pci.h>
2372246da4SFelipe Balbi #include <linux/platform_device.h>
24a89d977cSHeikki Krogerus #include <linux/gpio/consumer.h>
25a89d977cSHeikki Krogerus #include <linux/acpi.h>
2672246da4SFelipe Balbi 
278f317b47SHuang Rui #include "platform_data.h"
288f317b47SHuang Rui 
2972246da4SFelipe Balbi #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3		0xabcd
3041adc59cSJohn Youn #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3_AXI	0xabce
31e8095a25SJohn Youn #define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB31	0xabcf
32b62cd96dSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BYT			0x0f37
3385601f8cSDavid Cohen #define PCI_DEVICE_ID_INTEL_MRFLD		0x119e
349a5a0783SJohn Youn #define PCI_DEVICE_ID_INTEL_BSW			0x22b7
3584a2b61bSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_SPTLP		0x9d30
3684a2b61bSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_SPTH		0xa130
37b4c580a4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BXT			0x0aaa
381ffb4d5cSHeikki Krogerus #define PCI_DEVICE_ID_INTEL_BXT_M		0x1aaa
39b4c580a4SHeikki Krogerus #define PCI_DEVICE_ID_INTEL_APL			0x5aaa
4072246da4SFelipe Balbi 
41a89d977cSHeikki Krogerus static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
42a89d977cSHeikki Krogerus static const struct acpi_gpio_params cs_gpios = { 1, 0, false };
43a89d977cSHeikki Krogerus 
44a89d977cSHeikki Krogerus static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
45a89d977cSHeikki Krogerus 	{ "reset-gpios", &reset_gpios, 1 },
46a89d977cSHeikki Krogerus 	{ "cs-gpios", &cs_gpios, 1 },
47a89d977cSHeikki Krogerus 	{ },
48a89d977cSHeikki Krogerus };
49a89d977cSHeikki Krogerus 
502cd9ddf7SHeikki Krogerus static int dwc3_pci_quirks(struct pci_dev *pdev)
512cd9ddf7SHeikki Krogerus {
522cd9ddf7SHeikki Krogerus 	if (pdev->vendor == PCI_VENDOR_ID_AMD &&
532cd9ddf7SHeikki Krogerus 	    pdev->device == PCI_DEVICE_ID_AMD_NL_USB) {
542cd9ddf7SHeikki Krogerus 		struct dwc3_platform_data pdata;
552cd9ddf7SHeikki Krogerus 
562cd9ddf7SHeikki Krogerus 		memset(&pdata, 0, sizeof(pdata));
572cd9ddf7SHeikki Krogerus 
582cd9ddf7SHeikki Krogerus 		pdata.has_lpm_erratum = true;
592cd9ddf7SHeikki Krogerus 		pdata.lpm_nyet_threshold = 0xf;
602cd9ddf7SHeikki Krogerus 
612cd9ddf7SHeikki Krogerus 		pdata.u2exit_lfps_quirk = true;
622cd9ddf7SHeikki Krogerus 		pdata.u2ss_inp3_quirk = true;
632cd9ddf7SHeikki Krogerus 		pdata.req_p1p2p3_quirk = true;
642cd9ddf7SHeikki Krogerus 		pdata.del_p1p2p3_quirk = true;
652cd9ddf7SHeikki Krogerus 		pdata.del_phy_power_chg_quirk = true;
662cd9ddf7SHeikki Krogerus 		pdata.lfps_filter_quirk = true;
672cd9ddf7SHeikki Krogerus 		pdata.rx_detect_poll_quirk = true;
682cd9ddf7SHeikki Krogerus 
692cd9ddf7SHeikki Krogerus 		pdata.tx_de_emphasis_quirk = true;
702cd9ddf7SHeikki Krogerus 		pdata.tx_de_emphasis = 1;
712cd9ddf7SHeikki Krogerus 
722cd9ddf7SHeikki Krogerus 		/*
732cd9ddf7SHeikki Krogerus 		 * FIXME these quirks should be removed when AMD NL
742cd9ddf7SHeikki Krogerus 		 * taps out
752cd9ddf7SHeikki Krogerus 		 */
762cd9ddf7SHeikki Krogerus 		pdata.disable_scramble_quirk = true;
772cd9ddf7SHeikki Krogerus 		pdata.dis_u3_susphy_quirk = true;
782cd9ddf7SHeikki Krogerus 		pdata.dis_u2_susphy_quirk = true;
792cd9ddf7SHeikki Krogerus 
802cd9ddf7SHeikki Krogerus 		return platform_device_add_data(pci_get_drvdata(pdev), &pdata,
812cd9ddf7SHeikki Krogerus 						sizeof(pdata));
822cd9ddf7SHeikki Krogerus 	}
832cd9ddf7SHeikki Krogerus 
84a89d977cSHeikki Krogerus 	if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
85a89d977cSHeikki Krogerus 	    pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
86a89d977cSHeikki Krogerus 		struct gpio_desc *gpio;
87a89d977cSHeikki Krogerus 
88a89d977cSHeikki Krogerus 		acpi_dev_add_driver_gpios(ACPI_COMPANION(&pdev->dev),
89a89d977cSHeikki Krogerus 					  acpi_dwc3_byt_gpios);
90a89d977cSHeikki Krogerus 
912df033caSUwe Kleine-König 		/*
922df033caSUwe Kleine-König 		 * These GPIOs will turn on the USB2 PHY. Note that we have to
932df033caSUwe Kleine-König 		 * put the gpio descriptors again here because the phy driver
942df033caSUwe Kleine-König 		 * might want to grab them, too.
952df033caSUwe Kleine-König 		 */
962df033caSUwe Kleine-König 		gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
972df033caSUwe Kleine-König 		if (IS_ERR(gpio))
982df033caSUwe Kleine-König 			return PTR_ERR(gpio);
992df033caSUwe Kleine-König 
100a89d977cSHeikki Krogerus 		gpiod_set_value_cansleep(gpio, 1);
101a89d977cSHeikki Krogerus 		gpiod_put(gpio);
102a89d977cSHeikki Krogerus 
1032df033caSUwe Kleine-König 		gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
1042df033caSUwe Kleine-König 		if (IS_ERR(gpio))
1052df033caSUwe Kleine-König 			return PTR_ERR(gpio);
1062df033caSUwe Kleine-König 
1072df033caSUwe Kleine-König 		if (gpio) {
108a89d977cSHeikki Krogerus 			gpiod_set_value_cansleep(gpio, 1);
109a89d977cSHeikki Krogerus 			gpiod_put(gpio);
110a89d977cSHeikki Krogerus 			usleep_range(10000, 11000);
111a89d977cSHeikki Krogerus 		}
112a89d977cSHeikki Krogerus 	}
113a89d977cSHeikki Krogerus 
114bb7f3d6dSJohn Youn 	if (pdev->vendor == PCI_VENDOR_ID_SYNOPSYS &&
115bb7f3d6dSJohn Youn 	    (pdev->device == PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 ||
116bb7f3d6dSJohn Youn 	     pdev->device == PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3_AXI ||
117bb7f3d6dSJohn Youn 	     pdev->device == PCI_DEVICE_ID_SYNOPSYS_HAPSUSB31)) {
118bb7f3d6dSJohn Youn 
119bb7f3d6dSJohn Youn 		struct dwc3_platform_data pdata;
120bb7f3d6dSJohn Youn 
121bb7f3d6dSJohn Youn 		memset(&pdata, 0, sizeof(pdata));
122bb7f3d6dSJohn Youn 		pdata.usb3_lpm_capable = true;
123bb7f3d6dSJohn Youn 		pdata.has_lpm_erratum = true;
12494218ee3SJohn Youn 		pdata.dis_enblslpm_quirk = true;
125bb7f3d6dSJohn Youn 
126bb7f3d6dSJohn Youn 		return platform_device_add_data(pci_get_drvdata(pdev), &pdata,
127bb7f3d6dSJohn Youn 						sizeof(pdata));
128bb7f3d6dSJohn Youn 	}
129bb7f3d6dSJohn Youn 
1302cd9ddf7SHeikki Krogerus 	return 0;
1312cd9ddf7SHeikki Krogerus }
13272246da4SFelipe Balbi 
13341ac7b3aSBill Pemberton static int dwc3_pci_probe(struct pci_dev *pci,
13472246da4SFelipe Balbi 		const struct pci_device_id *id)
13572246da4SFelipe Balbi {
13672246da4SFelipe Balbi 	struct resource		res[2];
13772246da4SFelipe Balbi 	struct platform_device	*dwc3;
138b09e99eeSAndy Shevchenko 	int			ret;
139802ca850SChanho Park 	struct device		*dev = &pci->dev;
14072246da4SFelipe Balbi 
141f1c7e710SAndy Shevchenko 	ret = pcim_enable_device(pci);
14272246da4SFelipe Balbi 	if (ret) {
143802ca850SChanho Park 		dev_err(dev, "failed to enable pci device\n");
144802ca850SChanho Park 		return -ENODEV;
14572246da4SFelipe Balbi 	}
14672246da4SFelipe Balbi 
14772246da4SFelipe Balbi 	pci_set_master(pci);
14872246da4SFelipe Balbi 
149124dafdeSSebastian Andrzej Siewior 	dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
15072246da4SFelipe Balbi 	if (!dwc3) {
151802ca850SChanho Park 		dev_err(dev, "couldn't allocate dwc3 device\n");
152f1c7e710SAndy Shevchenko 		return -ENOMEM;
15372246da4SFelipe Balbi 	}
15472246da4SFelipe Balbi 
15572246da4SFelipe Balbi 	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));
15672246da4SFelipe Balbi 
15772246da4SFelipe Balbi 	res[0].start	= pci_resource_start(pci, 0);
15872246da4SFelipe Balbi 	res[0].end	= pci_resource_end(pci, 0);
15972246da4SFelipe Balbi 	res[0].name	= "dwc_usb3";
16072246da4SFelipe Balbi 	res[0].flags	= IORESOURCE_MEM;
16172246da4SFelipe Balbi 
16272246da4SFelipe Balbi 	res[1].start	= pci->irq;
16372246da4SFelipe Balbi 	res[1].name	= "dwc_usb3";
16472246da4SFelipe Balbi 	res[1].flags	= IORESOURCE_IRQ;
16572246da4SFelipe Balbi 
16672246da4SFelipe Balbi 	ret = platform_device_add_resources(dwc3, res, ARRAY_SIZE(res));
16772246da4SFelipe Balbi 	if (ret) {
168802ca850SChanho Park 		dev_err(dev, "couldn't add resources to dwc3 device\n");
169f1c7e710SAndy Shevchenko 		return ret;
17072246da4SFelipe Balbi 	}
17172246da4SFelipe Balbi 
1722cd9ddf7SHeikki Krogerus 	pci_set_drvdata(pci, dwc3);
1738f317b47SHuang Rui 
174802ca850SChanho Park 	dwc3->dev.parent = dev;
1759caeb06eSHeikki Krogerus 	ACPI_COMPANION_SET(&dwc3->dev, ACPI_COMPANION(dev));
17672246da4SFelipe Balbi 
177*474799f0SHeikki Krogerus 	ret = dwc3_pci_quirks(pci);
178*474799f0SHeikki Krogerus 	if (ret)
179*474799f0SHeikki Krogerus 		goto err;
180*474799f0SHeikki Krogerus 
18172246da4SFelipe Balbi 	ret = platform_device_add(dwc3);
18272246da4SFelipe Balbi 	if (ret) {
183802ca850SChanho Park 		dev_err(dev, "failed to register dwc3 device\n");
1842cd9ddf7SHeikki Krogerus 		goto err;
18572246da4SFelipe Balbi 	}
18672246da4SFelipe Balbi 
18772246da4SFelipe Balbi 	return 0;
1882cd9ddf7SHeikki Krogerus err:
18972246da4SFelipe Balbi 	platform_device_put(dwc3);
19072246da4SFelipe Balbi 	return ret;
19172246da4SFelipe Balbi }
19272246da4SFelipe Balbi 
193fb4e98abSBill Pemberton static void dwc3_pci_remove(struct pci_dev *pci)
19472246da4SFelipe Balbi {
195a89d977cSHeikki Krogerus 	acpi_dev_remove_driver_gpios(ACPI_COMPANION(&pci->dev));
1962cd9ddf7SHeikki Krogerus 	platform_device_unregister(pci_get_drvdata(pci));
19772246da4SFelipe Balbi }
19872246da4SFelipe Balbi 
199782df20cSJingoo Han static const struct pci_device_id dwc3_pci_id_table[] = {
20072246da4SFelipe Balbi 	{
20172246da4SFelipe Balbi 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
20272246da4SFelipe Balbi 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
20372246da4SFelipe Balbi 	},
20441adc59cSJohn Youn 	{
20541adc59cSJohn Youn 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
20641adc59cSJohn Youn 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3_AXI),
20741adc59cSJohn Youn 	},
208e8095a25SJohn Youn 	{
209e8095a25SJohn Youn 		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
210e8095a25SJohn Youn 				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB31),
211e8095a25SJohn Youn 	},
2127d643664SAlan Cox 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BSW), },
213b62cd96dSHeikki Krogerus 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BYT), },
21485601f8cSDavid Cohen 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_MRFLD), },
21584a2b61bSHeikki Krogerus 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SPTLP), },
21684a2b61bSHeikki Krogerus 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SPTH), },
217b4c580a4SHeikki Krogerus 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BXT), },
2181ffb4d5cSHeikki Krogerus 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BXT_M), },
219b4c580a4SHeikki Krogerus 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_APL), },
2209755449dSHuang Rui 	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_NL_USB), },
22172246da4SFelipe Balbi 	{  }	/* Terminating Entry */
22272246da4SFelipe Balbi };
22372246da4SFelipe Balbi MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);
22472246da4SFelipe Balbi 
22572246da4SFelipe Balbi static struct pci_driver dwc3_pci_driver = {
2260949e99bSFelipe Balbi 	.name		= "dwc3-pci",
22772246da4SFelipe Balbi 	.id_table	= dwc3_pci_id_table,
22872246da4SFelipe Balbi 	.probe		= dwc3_pci_probe,
2297690417dSBill Pemberton 	.remove		= dwc3_pci_remove,
23072246da4SFelipe Balbi };
23172246da4SFelipe Balbi 
23272246da4SFelipe Balbi MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
2335945f789SFelipe Balbi MODULE_LICENSE("GPL v2");
23472246da4SFelipe Balbi MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");
23572246da4SFelipe Balbi 
23695656336SGreg Kroah-Hartman module_pci_driver(dwc3_pci_driver);
237