1f37f0550SLorenzo Bianconi // SPDX-License-Identifier: ISC
2f37f0550SLorenzo Bianconi /*
3f37f0550SLorenzo Bianconi  * Copyright (C) 2019 Lorenzo Bianconi <lorenzo@kernel.org>
4f37f0550SLorenzo Bianconi  */
5f37f0550SLorenzo Bianconi 
6f37f0550SLorenzo Bianconi #include <linux/pci.h>
7f37f0550SLorenzo Bianconi 
8f37f0550SLorenzo Bianconi void mt76_pci_disable_aspm(struct pci_dev *pdev)
9f37f0550SLorenzo Bianconi {
10f37f0550SLorenzo Bianconi 	struct pci_dev *parent = pdev->bus->self;
11f37f0550SLorenzo Bianconi 	u16 aspm_conf, parent_aspm_conf = 0;
12f37f0550SLorenzo Bianconi 
13f37f0550SLorenzo Bianconi 	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &aspm_conf);
14f37f0550SLorenzo Bianconi 	aspm_conf &= PCI_EXP_LNKCTL_ASPMC;
15f37f0550SLorenzo Bianconi 	if (parent) {
16f37f0550SLorenzo Bianconi 		pcie_capability_read_word(parent, PCI_EXP_LNKCTL,
17f37f0550SLorenzo Bianconi 					  &parent_aspm_conf);
18f37f0550SLorenzo Bianconi 		parent_aspm_conf &= PCI_EXP_LNKCTL_ASPMC;
19f37f0550SLorenzo Bianconi 	}
20f37f0550SLorenzo Bianconi 
21f37f0550SLorenzo Bianconi 	if (!aspm_conf && (!parent || !parent_aspm_conf)) {
22f37f0550SLorenzo Bianconi 		/* aspm already disabled */
23f37f0550SLorenzo Bianconi 		return;
24f37f0550SLorenzo Bianconi 	}
25f37f0550SLorenzo Bianconi 
26f37f0550SLorenzo Bianconi 	dev_info(&pdev->dev, "disabling ASPM %s %s\n",
27f37f0550SLorenzo Bianconi 		 (aspm_conf & PCI_EXP_LNKCTL_ASPM_L0S) ? "L0s" : "",
28f37f0550SLorenzo Bianconi 		 (aspm_conf & PCI_EXP_LNKCTL_ASPM_L1) ? "L1" : "");
29f37f0550SLorenzo Bianconi 
30f37f0550SLorenzo Bianconi 	if (IS_ENABLED(CONFIG_PCIEASPM)) {
31f37f0550SLorenzo Bianconi 		int err;
32f37f0550SLorenzo Bianconi 
33f37f0550SLorenzo Bianconi 		err = pci_disable_link_state(pdev, aspm_conf);
34f37f0550SLorenzo Bianconi 		if (!err)
35f37f0550SLorenzo Bianconi 			return;
36f37f0550SLorenzo Bianconi 	}
37f37f0550SLorenzo Bianconi 
38f37f0550SLorenzo Bianconi 	/* both device and parent should have the same ASPM setting.
39f37f0550SLorenzo Bianconi 	 * disable ASPM in downstream component first and then upstream.
40f37f0550SLorenzo Bianconi 	 */
41f37f0550SLorenzo Bianconi 	pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL, aspm_conf);
42f37f0550SLorenzo Bianconi 	if (parent)
43f37f0550SLorenzo Bianconi 		pcie_capability_clear_word(parent, PCI_EXP_LNKCTL,
44f37f0550SLorenzo Bianconi 					   aspm_conf);
45f37f0550SLorenzo Bianconi }
46f37f0550SLorenzo Bianconi EXPORT_SYMBOL_GPL(mt76_pci_disable_aspm);
47