1 // SPDX-License-Identifier: GPL-2.0
3 * NHI specific operations
5 * Copyright (C) 2019, Intel Corporation
6 * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
9 #include <linux/delay.h>
10 #include <linux/suspend.h>
16 /* Ice Lake specific NHI operations */
18 #define ICL_LC_MAILBOX_TIMEOUT 500 /* ms */
20 static int check_for_device(struct device
*dev
, void *data
)
22 return tb_is_switch(dev
);
25 static bool icl_nhi_is_device_connected(struct tb_nhi
*nhi
)
27 struct tb
*tb
= pci_get_drvdata(nhi
->pdev
);
30 ret
= device_for_each_child(&tb
->root_switch
->dev
, NULL
,
35 static int icl_nhi_force_power(struct tb_nhi
*nhi
, bool power
)
40 * The Thunderbolt host controller is present always in Ice Lake
41 * but the firmware may not be loaded and running (depending
42 * whether there is device connected and so on). Each time the
43 * controller is used we need to "Force Power" it first and wait
44 * for the firmware to indicate it is up and running. This "Force
45 * Power" is really not about actually powering on/off the
46 * controller so it is accessible even if "Force Power" is off.
48 * The actual power management happens inside shared ACPI power
49 * resources using standard ACPI methods.
51 pci_read_config_dword(nhi
->pdev
, VS_CAP_22
, &vs_cap
);
53 vs_cap
&= ~VS_CAP_22_DMA_DELAY_MASK
;
54 vs_cap
|= 0x22 << VS_CAP_22_DMA_DELAY_SHIFT
;
55 vs_cap
|= VS_CAP_22_FORCE_POWER
;
57 vs_cap
&= ~VS_CAP_22_FORCE_POWER
;
59 pci_write_config_dword(nhi
->pdev
, VS_CAP_22
, vs_cap
);
62 unsigned int retries
= 350;
65 /* Wait until the firmware tells it is up and running */
67 pci_read_config_dword(nhi
->pdev
, VS_CAP_9
, &val
);
68 if (val
& VS_CAP_9_FW_READY
)
70 usleep_range(3000, 3100);
79 static void icl_nhi_lc_mailbox_cmd(struct tb_nhi
*nhi
, enum icl_lc_mailbox_cmd cmd
)
83 data
= (cmd
<< VS_CAP_19_CMD_SHIFT
) & VS_CAP_19_CMD_MASK
;
84 pci_write_config_dword(nhi
->pdev
, VS_CAP_19
, data
| VS_CAP_19_VALID
);
87 static int icl_nhi_lc_mailbox_cmd_complete(struct tb_nhi
*nhi
, int timeout
)
95 end
= jiffies
+ msecs_to_jiffies(timeout
);
97 pci_read_config_dword(nhi
->pdev
, VS_CAP_18
, &data
);
98 if (data
& VS_CAP_18_DONE
)
100 usleep_range(1000, 1100);
101 } while (time_before(jiffies
, end
));
106 /* Clear the valid bit */
107 pci_write_config_dword(nhi
->pdev
, VS_CAP_19
, 0);
111 static void icl_nhi_set_ltr(struct tb_nhi
*nhi
)
115 pci_read_config_dword(nhi
->pdev
, VS_CAP_16
, &max_ltr
);
117 /* Program the same value for both snoop and no-snoop */
118 ltr
= max_ltr
<< 16 | max_ltr
;
119 pci_write_config_dword(nhi
->pdev
, VS_CAP_15
, ltr
);
122 static int icl_nhi_suspend(struct tb_nhi
*nhi
)
124 struct tb
*tb
= pci_get_drvdata(nhi
->pdev
);
127 if (icl_nhi_is_device_connected(nhi
))
130 if (tb_switch_is_icm(tb
->root_switch
)) {
132 * If there is no device connected we need to perform
133 * both: a handshake through LC mailbox and force power
134 * down before entering D3.
136 icl_nhi_lc_mailbox_cmd(nhi
, ICL_LC_PREPARE_FOR_RESET
);
137 ret
= icl_nhi_lc_mailbox_cmd_complete(nhi
, ICL_LC_MAILBOX_TIMEOUT
);
142 return icl_nhi_force_power(nhi
, false);
145 static int icl_nhi_suspend_noirq(struct tb_nhi
*nhi
, bool wakeup
)
147 struct tb
*tb
= pci_get_drvdata(nhi
->pdev
);
148 enum icl_lc_mailbox_cmd cmd
;
150 if (!pm_suspend_via_firmware())
151 return icl_nhi_suspend(nhi
);
153 if (!tb_switch_is_icm(tb
->root_switch
))
156 cmd
= wakeup
? ICL_LC_GO2SX
: ICL_LC_GO2SX_NO_WAKE
;
157 icl_nhi_lc_mailbox_cmd(nhi
, cmd
);
158 return icl_nhi_lc_mailbox_cmd_complete(nhi
, ICL_LC_MAILBOX_TIMEOUT
);
161 static int icl_nhi_resume(struct tb_nhi
*nhi
)
165 ret
= icl_nhi_force_power(nhi
, true);
169 icl_nhi_set_ltr(nhi
);
173 static void icl_nhi_shutdown(struct tb_nhi
*nhi
)
175 icl_nhi_force_power(nhi
, false);
178 const struct tb_nhi_ops icl_nhi_ops
= {
179 .init
= icl_nhi_resume
,
180 .suspend_noirq
= icl_nhi_suspend_noirq
,
181 .resume_noirq
= icl_nhi_resume
,
182 .runtime_suspend
= icl_nhi_suspend
,
183 .runtime_resume
= icl_nhi_resume
,
184 .shutdown
= icl_nhi_shutdown
,