dwc3-pci.c 10.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7 8 9 10 11
/**
 * dwc3-pci.c - PCI Specific glue layer
 *
 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
 *
 * Authors: Felipe Balbi <balbi@ti.com>,
 *	    Sebastian Andrzej Siewior <bigeasy@linutronix.de>
 */

#include <linux/kernel.h>
12
#include <linux/module.h>
13 14
#include <linux/slab.h>
#include <linux/pci.h>
15
#include <linux/workqueue.h>
16
#include <linux/pm_runtime.h>
17
#include <linux/platform_device.h>
18 19
#include <linux/gpio/consumer.h>
#include <linux/acpi.h>
20
#include <linux/delay.h>
21

J
John Youn 已提交
22 23 24 25 26 27 28 29
#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3		0xabcd
#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3_AXI	0xabce
#define PCI_DEVICE_ID_SYNOPSYS_HAPSUSB31	0xabcf
#define PCI_DEVICE_ID_INTEL_BYT			0x0f37
#define PCI_DEVICE_ID_INTEL_MRFLD		0x119e
#define PCI_DEVICE_ID_INTEL_BSW			0x22b7
#define PCI_DEVICE_ID_INTEL_SPTLP		0x9d30
#define PCI_DEVICE_ID_INTEL_SPTH		0xa130
30
#define PCI_DEVICE_ID_INTEL_BXT			0x0aaa
31
#define PCI_DEVICE_ID_INTEL_BXT_M		0x1aaa
32
#define PCI_DEVICE_ID_INTEL_APL			0x5aaa
33
#define PCI_DEVICE_ID_INTEL_KBP			0xa2b0
34
#define PCI_DEVICE_ID_INTEL_GLK			0x31aa
35 36
#define PCI_DEVICE_ID_INTEL_CNPLP		0x9dee
#define PCI_DEVICE_ID_INTEL_CNPH		0xa36e
37

38
#define PCI_INTEL_BXT_DSM_GUID		"732b85d5-b7a7-4a1b-9ba0-4bbd00ffd511"
39 40 41 42
#define PCI_INTEL_BXT_FUNC_PMU_PWR	4
#define PCI_INTEL_BXT_STATE_D0		0
#define PCI_INTEL_BXT_STATE_D3		3

43 44 45 46
/**
 * struct dwc3_pci - Driver private structure
 * @dwc3: child dwc3 platform_device
 * @pci: our link to PCI bus
47
 * @guid: _DSM GUID
48
 * @has_dsm_for_pm: true for devices which need to run _DSM on runtime PM
49 50 51 52
 */
struct dwc3_pci {
	struct platform_device *dwc3;
	struct pci_dev *pci;
53

54
	guid_t guid;
55 56

	unsigned int has_dsm_for_pm:1;
57
	struct work_struct wakeup_work;
58 59
};

60 61 62 63 64 65 66 67 68
static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
static const struct acpi_gpio_params cs_gpios = { 1, 0, false };

static const struct acpi_gpio_mapping acpi_dwc3_byt_gpios[] = {
	{ "reset-gpios", &reset_gpios, 1 },
	{ "cs-gpios", &cs_gpios, 1 },
	{ },
};

69
static int dwc3_pci_quirks(struct dwc3_pci *dwc)
H
Heikki Krogerus 已提交
70
{
71 72 73
	struct platform_device		*dwc3 = dwc->dwc3;
	struct pci_dev			*pdev = dwc->pci;

H
Heikki Krogerus 已提交
74 75
	if (pdev->vendor == PCI_VENDOR_ID_AMD &&
	    pdev->device == PCI_DEVICE_ID_AMD_NL_USB) {
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		struct property_entry properties[] = {
			PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
			PROPERTY_ENTRY_U8("snps,lpm-nyet-threshold", 0xf),
			PROPERTY_ENTRY_BOOL("snps,u2exit_lfps_quirk"),
			PROPERTY_ENTRY_BOOL("snps,u2ss_inp3_quirk"),
			PROPERTY_ENTRY_BOOL("snps,req_p1p2p3_quirk"),
			PROPERTY_ENTRY_BOOL("snps,del_p1p2p3_quirk"),
			PROPERTY_ENTRY_BOOL("snps,del_phy_power_chg_quirk"),
			PROPERTY_ENTRY_BOOL("snps,lfps_filter_quirk"),
			PROPERTY_ENTRY_BOOL("snps,rx_detect_poll_quirk"),
			PROPERTY_ENTRY_BOOL("snps,tx_de_emphasis_quirk"),
			PROPERTY_ENTRY_U8("snps,tx_de_emphasis", 1),
			/*
			 * FIXME these quirks should be removed when AMD NL
			 * tapes out
			 */
			PROPERTY_ENTRY_BOOL("snps,disable_scramble_quirk"),
			PROPERTY_ENTRY_BOOL("snps,dis_u3_susphy_quirk"),
			PROPERTY_ENTRY_BOOL("snps,dis_u2_susphy_quirk"),
95
			PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
96 97 98 99
			{ },
		};

		return platform_device_add_properties(dwc3, properties);
H
Heikki Krogerus 已提交
100 101
	}

102 103 104 105
	if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
		int ret;

		struct property_entry properties[] = {
106
			PROPERTY_ENTRY_STRING("dr_mode", "peripheral"),
107
			PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
108 109 110 111 112 113 114
			{ }
		};

		ret = platform_device_add_properties(dwc3, properties);
		if (ret < 0)
			return ret;

115 116
		if (pdev->device == PCI_DEVICE_ID_INTEL_BXT ||
				pdev->device == PCI_DEVICE_ID_INTEL_BXT_M) {
117
			guid_parse(PCI_INTEL_BXT_DSM_GUID, &dwc->guid);
118 119 120
			dwc->has_dsm_for_pm = true;
		}

121 122 123
		if (pdev->device == PCI_DEVICE_ID_INTEL_BYT) {
			struct gpio_desc *gpio;

124
			ret = devm_acpi_dev_add_driver_gpios(&pdev->dev,
125
					acpi_dwc3_byt_gpios);
126 127
			if (ret)
				dev_dbg(&pdev->dev, "failed to add mapping table\n");
128 129 130 131 132 133 134 135 136 137

			/*
			 * These GPIOs will turn on the USB2 PHY. Note that we have to
			 * put the gpio descriptors again here because the phy driver
			 * might want to grab them, too.
			 */
			gpio = gpiod_get_optional(&pdev->dev, "cs", GPIOD_OUT_LOW);
			if (IS_ERR(gpio))
				return PTR_ERR(gpio);

138 139
			gpiod_set_value_cansleep(gpio, 1);
			gpiod_put(gpio);
140 141 142 143 144 145 146 147 148 149

			gpio = gpiod_get_optional(&pdev->dev, "reset", GPIOD_OUT_LOW);
			if (IS_ERR(gpio))
				return PTR_ERR(gpio);

			if (gpio) {
				gpiod_set_value_cansleep(gpio, 1);
				gpiod_put(gpio);
				usleep_range(10000, 11000);
			}
150 151 152
		}
	}

153 154 155 156
	if (pdev->vendor == PCI_VENDOR_ID_SYNOPSYS &&
	    (pdev->device == PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3 ||
	     pdev->device == PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3_AXI ||
	     pdev->device == PCI_DEVICE_ID_SYNOPSYS_HAPSUSB31)) {
157 158 159 160
		struct property_entry properties[] = {
			PROPERTY_ENTRY_BOOL("snps,usb3_lpm_capable"),
			PROPERTY_ENTRY_BOOL("snps,has-lpm-erratum"),
			PROPERTY_ENTRY_BOOL("snps,dis_enblslpm_quirk"),
161
			PROPERTY_ENTRY_BOOL("linux,sysdev_is_parent"),
162 163 164 165
			{ },
		};

		return platform_device_add_properties(dwc3, properties);
166 167
	}

H
Heikki Krogerus 已提交
168 169
	return 0;
}
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
#ifdef CONFIG_PM
static void dwc3_pci_resume_work(struct work_struct *work)
{
	struct dwc3_pci *dwc = container_of(work, struct dwc3_pci, wakeup_work);
	struct platform_device *dwc3 = dwc->dwc3;
	int ret;

	ret = pm_runtime_get_sync(&dwc3->dev);
	if (ret)
		return;

	pm_runtime_mark_last_busy(&dwc3->dev);
	pm_runtime_put_sync_autosuspend(&dwc3->dev);
}
#endif

B
Bill Pemberton 已提交
187
static int dwc3_pci_probe(struct pci_dev *pci,
188 189
		const struct pci_device_id *id)
{
190
	struct dwc3_pci		*dwc;
191
	struct resource		res[2];
192
	int			ret;
C
Chanho Park 已提交
193
	struct device		*dev = &pci->dev;
194

195
	ret = pcim_enable_device(pci);
196
	if (ret) {
C
Chanho Park 已提交
197 198
		dev_err(dev, "failed to enable pci device\n");
		return -ENODEV;
199 200 201 202
	}

	pci_set_master(pci);

203 204 205 206 207 208
	dwc = devm_kzalloc(dev, sizeof(*dwc), GFP_KERNEL);
	if (!dwc)
		return -ENOMEM;

	dwc->dwc3 = platform_device_alloc("dwc3", PLATFORM_DEVID_AUTO);
	if (!dwc->dwc3)
209
		return -ENOMEM;
210 211 212 213 214 215 216 217 218 219 220 221

	memset(res, 0x00, sizeof(struct resource) * ARRAY_SIZE(res));

	res[0].start	= pci_resource_start(pci, 0);
	res[0].end	= pci_resource_end(pci, 0);
	res[0].name	= "dwc_usb3";
	res[0].flags	= IORESOURCE_MEM;

	res[1].start	= pci->irq;
	res[1].name	= "dwc_usb3";
	res[1].flags	= IORESOURCE_IRQ;

222
	ret = platform_device_add_resources(dwc->dwc3, res, ARRAY_SIZE(res));
223
	if (ret) {
C
Chanho Park 已提交
224
		dev_err(dev, "couldn't add resources to dwc3 device\n");
225
		goto err;
226 227
	}

228 229 230
	dwc->pci = pci;
	dwc->dwc3->dev.parent = dev;
	ACPI_COMPANION_SET(&dwc->dwc3->dev, ACPI_COMPANION(dev));
231

232
	ret = dwc3_pci_quirks(dwc);
233 234 235
	if (ret)
		goto err;

236
	ret = platform_device_add(dwc->dwc3);
237
	if (ret) {
C
Chanho Park 已提交
238
		dev_err(dev, "failed to register dwc3 device\n");
H
Heikki Krogerus 已提交
239
		goto err;
240 241
	}

242
	device_init_wakeup(dev, true);
243
	pci_set_drvdata(pci, dwc);
244
	pm_runtime_put(dev);
245 246 247
#ifdef CONFIG_PM
	INIT_WORK(&dwc->wakeup_work, dwc3_pci_resume_work);
#endif
248

249
	return 0;
H
Heikki Krogerus 已提交
250
err:
251
	platform_device_put(dwc->dwc3);
252 253 254
	return ret;
}

B
Bill Pemberton 已提交
255
static void dwc3_pci_remove(struct pci_dev *pci)
256
{
257 258
	struct dwc3_pci		*dwc = pci_get_drvdata(pci);

259 260 261
#ifdef CONFIG_PM
	cancel_work_sync(&dwc->wakeup_work);
#endif
262 263
	device_init_wakeup(&pci->dev, false);
	pm_runtime_get(&pci->dev);
264
	platform_device_unregister(dwc->dwc3);
265 266
}

267
static const struct pci_device_id dwc3_pci_id_table[] = {
268 269 270 271
	{
		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3),
	},
272 273 274 275
	{
		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB3_AXI),
	},
276 277 278 279
	{
		PCI_DEVICE(PCI_VENDOR_ID_SYNOPSYS,
				PCI_DEVICE_ID_SYNOPSYS_HAPSUSB31),
	},
280
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BSW), },
281
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BYT), },
282
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_MRFLD), },
283 284
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SPTLP), },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_SPTH), },
285
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BXT), },
286
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_BXT_M), },
287
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_APL), },
288
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_KBP), },
289
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_GLK), },
290 291
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CNPLP), },
	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_CNPH), },
292
	{ PCI_DEVICE(PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_NL_USB), },
293 294 295 296
	{  }	/* Terminating Entry */
};
MODULE_DEVICE_TABLE(pci, dwc3_pci_id_table);

F
Felipe Balbi 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309
#if defined(CONFIG_PM) || defined(CONFIG_PM_SLEEP)
static int dwc3_pci_dsm(struct dwc3_pci *dwc, int param)
{
	union acpi_object *obj;
	union acpi_object tmp;
	union acpi_object argv4 = ACPI_INIT_DSM_ARGV4(1, &tmp);

	if (!dwc->has_dsm_for_pm)
		return 0;

	tmp.type = ACPI_TYPE_INTEGER;
	tmp.integer.value = param;

310
	obj = acpi_evaluate_dsm(ACPI_HANDLE(&dwc->pci->dev), &dwc->guid,
F
Felipe Balbi 已提交
311 312 313 314 315 316 317 318 319 320 321 322
			1, PCI_INTEL_BXT_FUNC_PMU_PWR, &argv4);
	if (!obj) {
		dev_err(&dwc->pci->dev, "failed to evaluate _DSM\n");
		return -EIO;
	}

	ACPI_FREE(obj);

	return 0;
}
#endif /* CONFIG_PM || CONFIG_PM_SLEEP */

323 324 325
#ifdef CONFIG_PM
static int dwc3_pci_runtime_suspend(struct device *dev)
{
326 327
	struct dwc3_pci		*dwc = dev_get_drvdata(dev);

328
	if (device_can_wakeup(dev))
329
		return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
330 331 332 333

	return -EBUSY;
}

334 335
static int dwc3_pci_runtime_resume(struct device *dev)
{
336
	struct dwc3_pci		*dwc = dev_get_drvdata(dev);
337 338 339 340 341
	int			ret;

	ret = dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
	if (ret)
		return ret;
342

343 344 345
	queue_work(pm_wq, &dwc->wakeup_work);

	return 0;
346
}
347
#endif /* CONFIG_PM */
348

349
#ifdef CONFIG_PM_SLEEP
350
static int dwc3_pci_suspend(struct device *dev)
351
{
352 353 354 355 356 357 358 359 360 361
	struct dwc3_pci		*dwc = dev_get_drvdata(dev);

	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D3);
}

static int dwc3_pci_resume(struct device *dev)
{
	struct dwc3_pci		*dwc = dev_get_drvdata(dev);

	return dwc3_pci_dsm(dwc, PCI_INTEL_BXT_STATE_D0);
362
}
363
#endif /* CONFIG_PM_SLEEP */
364

D
Doug Wilson 已提交
365
static const struct dev_pm_ops dwc3_pci_dev_pm_ops = {
366
	SET_SYSTEM_SLEEP_PM_OPS(dwc3_pci_suspend, dwc3_pci_resume)
367
	SET_RUNTIME_PM_OPS(dwc3_pci_runtime_suspend, dwc3_pci_runtime_resume,
368 369 370
		NULL)
};

371
static struct pci_driver dwc3_pci_driver = {
372
	.name		= "dwc3-pci",
373 374
	.id_table	= dwc3_pci_id_table,
	.probe		= dwc3_pci_probe,
B
Bill Pemberton 已提交
375
	.remove		= dwc3_pci_remove,
376 377 378
	.driver		= {
		.pm	= &dwc3_pci_dev_pm_ops,
	}
379 380 381
};

MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
F
Felipe Balbi 已提交
382
MODULE_LICENSE("GPL v2");
383 384
MODULE_DESCRIPTION("DesignWare USB3 PCI Glue Layer");

G
Greg Kroah-Hartman 已提交
385
module_pci_driver(dwc3_pci_driver);