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