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