1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <acpi/acpigen.h>
4 #include <acpi/acpi_device.h>
5 #include <console/console.h>
6 #include <device/device.h>
7 #include <device/pci_ids.h>
8 #include <device/pci_ops.h>
9 #include <device/pci.h>
10 #include <intelblocks/pmc.h>
11 #include <intelblocks/pmc_ipc.h>
12 #include <intelblocks/pcie_rp.h>
13 #include <soc/iomap.h>
16 /* PCIe Root Port registers for link status and L23 control. */
17 #define PCH_PCIE_CFG_LSTS 0x52 /* Link Status Register */
18 #define PCH_PCIE_CFG_SPR 0xe0 /* Scratchpad */
19 #define PCH_PCIE_CFG_RPPGEN 0xe2 /* Root Port Power Gating Enable */
20 #define PCH_PCIE_CFG_LCAP_PN 0x4f /* Root Port Number */
22 /* ACPI register names corresponding to PCIe root port registers. */
23 #define ACPI_REG_PCI_LINK_ACTIVE "LASX" /* Link active status */
24 #define ACPI_REG_PCI_L23_RDY_ENTRY "L23E" /* L23_Rdy Entry Request */
25 #define ACPI_REG_PCI_L23_RDY_DETECT "L23R" /* L23_Rdy Detect Transition */
26 #define ACPI_REG_PCI_L23_SAVE_STATE "NCB7" /* Scratch bit to save L23 state */
28 /* ACPI path to the mutex that protects accesses to PMC ModPhy power gating registers */
29 #define RTD3_MUTEX_PATH "\\_SB.PCI0.R3MX"
31 /* ACPI path to control PCIE CLK by P2SB */
32 #define RTD3_PCIE_CLK_ENABLE_PATH "\\_SB.PCI0.SPCO"
34 enum modphy_pg_state
{
39 /* Called from _ON to get PCIe link back to active state. */
40 static void pcie_rtd3_acpi_l23_exit(void)
42 /* Skip if port is not in L2/L3. */
43 acpigen_write_if_lequal_namestr_int(ACPI_REG_PCI_L23_SAVE_STATE
, 1);
45 /* Initiate L2/L3 Ready To Detect transition. */
46 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_DETECT
);
48 /* Wait for transition to detect. */
49 acpigen_write_delay_until_namestr_int(320, ACPI_REG_PCI_L23_RDY_DETECT
, 0);
51 acpigen_write_store_int_to_namestr(0, ACPI_REG_PCI_L23_SAVE_STATE
);
53 /* Once in detect, wait for link active. */
54 acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_LINK_ACTIVE
, 1);
56 acpigen_pop_len(); /* If */
59 /* Called from _OFF to put PCIe link into L2/L3 state. */
60 static void pcie_rtd3_acpi_l23_entry(void)
62 /* Initiate L2/L3 Entry request. */
63 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_RDY_ENTRY
);
65 /* Wait for L2/L3 Entry request to clear. */
66 acpigen_write_delay_until_namestr_int(128, ACPI_REG_PCI_L23_RDY_ENTRY
, 0);
68 acpigen_write_store_int_to_namestr(1, ACPI_REG_PCI_L23_SAVE_STATE
);
71 /* Called from _ON/_OFF to disable/enable ModPHY power gating */
72 static void pcie_rtd3_enable_modphy_pg(unsigned int pcie_rp
, enum modphy_pg_state state
)
74 /* Enter the critical section */
75 acpigen_emit_ext_op(ACQUIRE_OP
);
76 acpigen_emit_namestring(RTD3_MUTEX_PATH
);
77 acpigen_emit_word(ACPI_MUTEX_NO_TIMEOUT
);
79 acpigen_write_store_int_to_namestr(state
, "EMPG");
80 acpigen_write_delay_until_namestr_int(100, "AMPG", state
);
82 /* Exit the critical section */
83 acpigen_emit_ext_op(RELEASE_OP
);
84 acpigen_emit_namestring(RTD3_MUTEX_PATH
);
87 /* Method to enter L2/L3 */
88 static void pcie_rtd3_acpi_method_dl23(void)
90 acpigen_write_method_serialized("DL23", 0);
91 pcie_rtd3_acpi_l23_entry();
92 acpigen_pop_len(); /* Method */
95 /* Method to exit L2/L3 */
96 static void pcie_rtd3_acpi_method_l23d(void)
98 acpigen_write_method_serialized("L23D", 0);
99 pcie_rtd3_acpi_l23_exit();
100 acpigen_pop_len(); /* Method */
103 /* Method to disable PCH modPHY power gating */
104 static void pcie_rtd3_acpi_method_pds0(unsigned int pcie_rp
)
106 acpigen_write_method_serialized("PSD0", 0);
107 pcie_rtd3_enable_modphy_pg(pcie_rp
, PG_DISABLE
);
108 acpigen_pop_len(); /* Method */
111 /* Method to enable/disable the source clock */
112 static void pcie_rtd3_acpi_method_srck(unsigned int pcie_rp
,
113 const struct soc_intel_common_block_pcie_rtd3_config
*config
)
115 acpigen_write_method_serialized("SRCK", 1);
117 if (config
->srcclk_pin
>= 0) {
118 acpigen_write_if_lequal_op_op(ARG0_OP
, 0);
119 pmc_ipc_acpi_set_pci_clock(pcie_rp
, config
->srcclk_pin
, false);
120 acpigen_write_else();
121 pmc_ipc_acpi_set_pci_clock(pcie_rp
, config
->srcclk_pin
, true);
122 acpigen_pop_len(); /* If */
124 acpigen_pop_len(); /* Method */
127 /* Method to enable/disable pcie clock by p2sb*/
128 static void p2sb_acpi_set_pci_clock(u8 srcclk_pin
, bool enable
)
130 acpigen_write_if_cond_ref_of(RTD3_PCIE_CLK_ENABLE_PATH
);
131 acpigen_emit_namestring(RTD3_PCIE_CLK_ENABLE_PATH
);
132 acpigen_write_integer(srcclk_pin
);
133 acpigen_write_integer(enable
);
134 acpigen_write_if_end();
138 pcie_rtd3_acpi_method_on(unsigned int pcie_rp
,
139 const struct soc_intel_common_block_pcie_rtd3_config
*config
,
140 enum pcie_rp_type rp_type
,
141 const struct device
*dev
)
143 const struct device
*parent
= dev
->upstream
->dev
;
145 acpigen_write_method_serialized("_ON", 0);
147 /* When this feature is enabled, ONSK indicates if the previous _OFF was
148 * skipped. If so, since the device was not in Off state, and the current
149 * _ON can be skipped as well.
151 if (config
->skip_on_off_support
)
152 acpigen_write_if_lequal_namestr_int("ONSK", 0);
154 /* The _STA returns current power status of device, so we can skip _ON
157 * Local0 = \_SB.PCI0.RP01.RTD3._STA ()
158 * If ((Local0 == One))
163 acpigen_write_store();
164 acpigen_emit_namestring(acpi_device_path_join(parent
, "RTD3._STA"));
165 acpigen_emit_byte(LOCAL0_OP
);
166 acpigen_write_if_lequal_op_int(LOCAL0_OP
, ONE_OP
);
167 acpigen_write_return_op(ONE_OP
);
168 acpigen_write_if_end();
170 if (config
->use_rp_mutex
)
171 acpigen_write_acquire(acpi_device_path_join(parent
, RP_MUTEX_NAME
),
172 ACPI_MUTEX_NO_TIMEOUT
);
174 /* Disable modPHY power gating for PCH RPs. */
175 if (rp_type
== PCIE_RP_PCH
)
176 pcie_rtd3_enable_modphy_pg(pcie_rp
, PG_DISABLE
);
178 /* Assert enable GPIO to turn on device power. */
179 if (config
->enable_gpio
.pin_count
) {
180 acpigen_enable_tx_gpio(&config
->enable_gpio
);
181 if (config
->enable_delay_ms
)
182 acpigen_write_sleep(config
->enable_delay_ms
);
185 /* Enable SRCCLK for this root port if pin is defined. */
186 if (config
->srcclk_pin
>= 0) {
187 if (CONFIG(PCIE_CLOCK_CONTROL_THROUGH_P2SB
))
188 p2sb_acpi_set_pci_clock(config
->srcclk_pin
, true);
190 pmc_ipc_acpi_set_pci_clock(pcie_rp
, config
->srcclk_pin
, true);
193 /* De-assert reset GPIO to bring device out of reset. */
194 if (config
->reset_gpio
.pin_count
) {
195 acpigen_disable_tx_gpio(&config
->reset_gpio
);
196 if (config
->reset_delay_ms
)
197 acpigen_write_sleep(config
->reset_delay_ms
);
200 /* Trigger L23 ready exit flow unless disabled by config. */
201 if (!config
->disable_l23
)
202 pcie_rtd3_acpi_l23_exit();
204 if (config
->use_rp_mutex
)
205 acpigen_write_release(acpi_device_path_join(parent
, RP_MUTEX_NAME
));
207 if (config
->skip_on_off_support
) {
208 /* If current _ON is skipped, ONSK is decremented so that _ON will be
209 * executed normally until _OFF is skipped again.
211 acpigen_write_else();
212 acpigen_emit_byte(DECREMENT_OP
);
213 acpigen_emit_namestring("ONSK");
215 acpigen_pop_len(); /* Else */
217 acpigen_pop_len(); /* Method */
221 pcie_rtd3_acpi_method_off(int pcie_rp
,
222 const struct soc_intel_common_block_pcie_rtd3_config
*config
,
223 enum pcie_rp_type rp_type
,
224 const struct device
*dev
)
227 const struct device
*parent
= dev
->upstream
->dev
;
229 acpigen_write_method_serialized("_OFF", 0);
231 /* When this feature is enabled, ONSK is checked to see if the device
232 * wants _OFF to be skipped for once. ONSK is normally incremented in the
233 * device method, such as reset _RST, which is invoked during driver reload.
234 * In such case, _OFF needs to be avoided at the end of driver removal.
236 if (config
->skip_on_off_support
)
237 acpigen_write_if_lequal_namestr_int("OFSK", 0);
239 if (config
->use_rp_mutex
)
240 acpigen_write_acquire(acpi_device_path_join(parent
, RP_MUTEX_NAME
),
241 ACPI_MUTEX_NO_TIMEOUT
);
243 /* Trigger L23 ready entry flow unless disabled by config. */
244 if (!config
->disable_l23
)
245 pcie_rtd3_acpi_l23_entry();
247 /* Assert reset GPIO to place device into reset. */
248 if (config
->reset_gpio
.pin_count
) {
249 acpigen_enable_tx_gpio(&config
->reset_gpio
);
250 if (config
->reset_off_delay_ms
)
251 acpigen_write_sleep(config
->reset_off_delay_ms
);
254 /* Enable modPHY power gating for PCH RPs */
255 if (rp_type
== PCIE_RP_PCH
)
256 pcie_rtd3_enable_modphy_pg(pcie_rp
, PG_ENABLE
);
258 /* Disable SRCCLK for this root port if pin is defined. */
259 if (config
->srcclk_pin
>= 0) {
260 if (CONFIG(PCIE_CLOCK_CONTROL_THROUGH_P2SB
))
261 p2sb_acpi_set_pci_clock(config
->srcclk_pin
, false);
263 pmc_ipc_acpi_set_pci_clock(pcie_rp
, config
->srcclk_pin
, false);
266 /* De-assert enable GPIO to turn off device power. */
267 if (config
->enable_gpio
.pin_count
) {
268 acpigen_disable_tx_gpio(&config
->enable_gpio
);
269 if (config
->enable_off_delay_ms
)
270 acpigen_write_sleep(config
->enable_off_delay_ms
);
273 if (config
->use_rp_mutex
)
274 acpigen_write_release(acpi_device_path_join(parent
, RP_MUTEX_NAME
));
276 if (config
->skip_on_off_support
) {
277 /* If current _OFF is skipped, ONSK is incremented so that the
278 * following _ON will also be skipped. In addition, OFSK is decremented
279 * so that next _OFF will be executed normally until the device method
280 * increments OFSK again.
282 acpigen_write_else();
284 acpigen_emit_byte(DECREMENT_OP
);
285 acpigen_emit_namestring("OFSK");
287 acpigen_emit_byte(INCREMENT_OP
);
288 acpigen_emit_namestring("ONSK");
290 acpigen_pop_len(); /* Else */
292 acpigen_pop_len(); /* Method */
296 pcie_rtd3_acpi_method_status(const struct soc_intel_common_block_pcie_rtd3_config
*config
)
298 const struct acpi_gpio
*gpio
;
300 acpigen_write_method("_STA", 0);
302 * Depending on the board configuration we use either the "enable" or
303 * the "reset" pin to detect the status of the device. The logic for
304 * each pin is detailed below.
306 * 1. For the "enable" pin:
307 * | polarity | tx value | get_tx_gpio() | State |
308 * |-------------+----------+---------------+-------|
309 * | active high | 0 | 0 | 0 |
310 * | active high | 1 | 1(active) | 1 |
311 * | active low | 0 | 1(active) | 1 |
312 * | active low | 1 | 0 | 0 |
314 * 2. For the "reset" pin:
315 * | polarity | tx value | get_tx_gpio() | State |
316 * |-------------+----------+---------------+-------|
317 * | active high | 0 | 0 | 1 |
318 * | active high | 1 | 1(active) | 0 |
319 * | active low | 0 | 1(active) | 0 |
320 * | active low | 1 | 0 | 1 |
323 /* Use enable GPIO for status if provided, otherwise use reset GPIO. */
324 if (config
->enable_gpio
.pin_count
) {
325 gpio
= &config
->enable_gpio
;
326 /* Read current GPIO state into Local0. */
327 acpigen_get_tx_gpio(gpio
);
329 gpio
= &config
->reset_gpio
;
330 /* Read current GPIO state into Local0. */
331 acpigen_get_tx_gpio(gpio
);
332 acpigen_write_not(LOCAL0_OP
, LOCAL0_OP
);
334 acpigen_write_return_op(LOCAL0_OP
);
335 acpigen_pop_len(); /* Method */
338 static void write_modphy_opregion(unsigned int pcie_rp
)
340 /* The register containing the Power Gate enable sequence bits is at
341 PCH_PWRM_BASE + 0x10D0, and the bits to check for sequence completion are at
342 PCH_PWRM_BASE + 0x10D4. */
343 const struct opregion opregion
= OPREGION("PMCP", SYSTEMMEMORY
,
344 PCH_PWRM_BASE_ADDRESS
+ 0x1000, 0xff);
345 const struct fieldlist fieldlist
[] = {
346 FIELDLIST_OFFSET(0xD0),
347 FIELDLIST_RESERVED(pcie_rp
),
348 FIELDLIST_NAMESTR("EMPG", 1), /* Enable ModPHY Power Gate */
349 FIELDLIST_OFFSET(0xD4),
350 FIELDLIST_RESERVED(pcie_rp
),
351 FIELDLIST_NAMESTR("AMPG", 1), /* Is ModPHY Power Gate active? */
354 acpigen_write_opregion(&opregion
);
355 acpigen_write_field("PMCP", fieldlist
, ARRAY_SIZE(fieldlist
),
356 FIELD_DWORDACC
| FIELD_NOLOCK
| FIELD_PRESERVE
);
359 static int get_pcie_rp_pmc_idx(enum pcie_rp_type rp_type
, const struct device
*dev
)
365 /* Read port number of root port that this device is attached to. */
366 idx
= pci_read_config8(dev
, PCH_PCIE_CFG_LCAP_PN
);
368 /* Port number is 1-based, PMC IPC method expects 0-based. */
372 /* CPU RPs are indexed by their "virtual wire index" to the PCH */
373 idx
= soc_get_cpu_rp_vw_idx(dev
);
382 static void pcie_rtd3_acpi_fill_ssdt(const struct device
*dev
)
384 static bool mutex_created
= false;
386 const struct soc_intel_common_block_pcie_rtd3_config
*config
= config_of(dev
);
387 static const char *const power_res_states
[] = {"_PR0"};
388 const struct device
*parent
= dev
->upstream
->dev
;
389 const char *scope
= acpi_device_path(parent
);
390 const struct opregion opregion
= OPREGION("PXCS", PCI_CONFIG
, 0, 0xff);
391 const struct fieldlist fieldlist
[] = {
392 FIELDLIST_OFFSET(PCH_PCIE_CFG_LSTS
),
393 FIELDLIST_RESERVED(13),
394 FIELDLIST_NAMESTR(ACPI_REG_PCI_LINK_ACTIVE
, 1),
395 FIELDLIST_OFFSET(PCH_PCIE_CFG_SPR
),
396 FIELDLIST_RESERVED(7),
397 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_SAVE_STATE
, 1),
398 FIELDLIST_OFFSET(PCH_PCIE_CFG_RPPGEN
),
399 FIELDLIST_RESERVED(2),
400 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_ENTRY
, 1),
401 FIELDLIST_NAMESTR(ACPI_REG_PCI_L23_RDY_DETECT
, 1),
406 if (!is_dev_enabled(parent
)) {
407 printk(BIOS_ERR
, "%s: root port not enabled\n", __func__
);
411 printk(BIOS_ERR
, "%s: root port scope not found\n", __func__
);
414 if (!config
->enable_gpio
.pin_count
&& !config
->reset_gpio
.pin_count
) {
415 printk(BIOS_ERR
, "%s: Enable and/or Reset GPIO required for %s.\n",
419 if (config
->srcclk_pin
> CONFIG_MAX_PCIE_CLOCK_SRC
) {
420 printk(BIOS_ERR
, "%s: Invalid clock pin %u for %s.\n", __func__
,
421 config
->srcclk_pin
, scope
);
425 const enum pcie_rp_type rp_type
= soc_get_pcie_rp_type(parent
);
426 pcie_rp
= get_pcie_rp_pmc_idx(rp_type
, parent
);
428 printk(BIOS_ERR
, "%s: Unknown PCIe root port\n", __func__
);
431 if (config
->disable_l23
) {
432 if (config
->ext_pm_support
& ACPI_PCIE_RP_EMIT_L23
) {
433 printk(BIOS_ERR
, "%s: Can not export L23 methods\n", __func__
);
437 if (rp_type
!= PCIE_RP_PCH
) {
438 if (config
->ext_pm_support
& ACPI_PCIE_RP_EMIT_PSD0
) {
439 printk(BIOS_ERR
, "%s: Can not export PSD0 method\n", __func__
);
443 if (config
->srcclk_pin
== -1) {
444 if (config
->ext_pm_support
& ACPI_PCIE_RP_EMIT_SRCK
) {
445 printk(BIOS_ERR
, "%s: Can not export SRCK method since clock source gating is not enabled\n",
451 printk(BIOS_INFO
, "%s: Enable RTD3 for %s (%s)\n", scope
, dev_path(parent
),
452 config
->desc
?: dev
->chip_ops
->name
);
454 /* Create a mutex for exclusive access to the PMC registers. */
455 if (rp_type
== PCIE_RP_PCH
&& !mutex_created
) {
456 acpigen_write_scope("\\_SB.PCI0");
457 acpigen_write_mutex("R3MX", 0);
458 acpigen_write_scope_end();
459 mutex_created
= true;
462 /* The RTD3 power resource is added to the root port, not the device. */
463 acpigen_write_scope(scope
);
465 if (config
->use_rp_mutex
)
466 acpigen_write_mutex(RP_MUTEX_NAME
, 0);
469 acpigen_write_name_string("_DDN", config
->desc
);
471 /* Create OpRegions for MMIO accesses. */
472 acpigen_write_opregion(&opregion
);
473 acpigen_write_field("PXCS", fieldlist
, ARRAY_SIZE(fieldlist
),
474 FIELD_ANYACC
| FIELD_NOLOCK
| FIELD_PRESERVE
);
476 if (config
->ext_pm_support
& ACPI_PCIE_RP_EMIT_L23
) {
477 pcie_rtd3_acpi_method_dl23();
478 pcie_rtd3_acpi_method_l23d();
481 /* Create the OpRegion to access the ModPHY PG registers (PCH RPs only) */
482 if (rp_type
== PCIE_RP_PCH
)
483 write_modphy_opregion(pcie_rp
);
485 if (config
->ext_pm_support
& ACPI_PCIE_RP_EMIT_PSD0
)
486 pcie_rtd3_acpi_method_pds0(pcie_rp
);
488 if (config
->ext_pm_support
& ACPI_PCIE_RP_EMIT_SRCK
)
489 pcie_rtd3_acpi_method_srck(pcie_rp
, config
);
491 /* ACPI Power Resource for controlling the attached device power. */
492 acpigen_write_power_res("RTD3", 0, 0, power_res_states
, ARRAY_SIZE(power_res_states
));
494 if (config
->skip_on_off_support
) {
495 /* OFSK: 0 = _OFF Method will be executed normally when called;
496 * >1 = _OFF will be skipped.
497 * _OFF Method to decrement OFSK and increment ONSK if the
498 * current execution is skipped.
499 * ONSK: 0 = _ON Method will be executed normally when called;
500 * >1 = _ONF will be skipped.
501 * _ON Method to decrement ONSK if the current execution is
504 acpigen_write_name_integer("ONSK", 0);
505 acpigen_write_name_integer("OFSK", 0);
508 pcie_rtd3_acpi_method_status(config
);
509 pcie_rtd3_acpi_method_on(pcie_rp
, config
, rp_type
, dev
);
510 pcie_rtd3_acpi_method_off(pcie_rp
, config
, rp_type
, dev
);
511 acpigen_pop_len(); /* PowerResource */
513 /* Indicate to the OS that device supports hotplug in D3. */
514 dsd
= acpi_dp_new_table("_DSD");
515 acpi_device_add_hotplug_support_in_d3(dsd
);
517 /* Indicate to the OS if the device provides an External facing port. */
518 if (config
->add_acpi_external_facing_port
)
519 acpi_device_add_external_facing_port(dsd
);
521 /* Indicate to the OS if the device has DMA property. */
522 if (config
->add_acpi_dma_property
)
523 acpi_device_add_dma_property(dsd
);
528 * Check the sibling device on the root port to see if it is storage class and add the
529 * property for the OS to enable storage D3, or allow it to be enabled by config.
531 if (config
->is_storage
532 || (dev
->sibling
&& (dev
->sibling
->class >> 16) == PCI_BASE_CLASS_STORAGE
)) {
533 acpigen_write_device(acpi_device_name(dev
));
534 acpigen_write_ADR(0);
535 acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON
);
536 if (CONFIG(D3COLD_SUPPORT
))
537 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_COLD
);
539 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT
);
541 acpi_device_add_storage_d3_enable(NULL
);
543 acpigen_pop_len(); /* Device */
545 printk(BIOS_INFO
, "%s: Added StorageD3Enable property\n", scope
);
548 acpigen_pop_len(); /* Scope */
551 static const char *pcie_rtd3_acpi_name(const struct device
*dev
)
553 /* Attached device name must be "PXSX" for the Linux Kernel to recognize it. */
557 static struct device_operations pcie_rtd3_ops
= {
558 .read_resources
= noop_read_resources
,
559 .set_resources
= noop_set_resources
,
560 .acpi_fill_ssdt
= pcie_rtd3_acpi_fill_ssdt
,
561 .acpi_name
= pcie_rtd3_acpi_name
,
564 static void pcie_rtd3_acpi_enable(struct device
*dev
)
566 dev
->ops
= &pcie_rtd3_ops
;
569 struct chip_operations soc_intel_common_block_pcie_rtd3_ops
= {
570 .name
= "Intel PCIe Runtime D3",
571 .enable_dev
= pcie_rtd3_acpi_enable