1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #define __SIMPLE_DEVICE__
7 #include <console/console.h>
8 #include <device/device.h>
10 #include <intelblocks/cpulib.h>
11 #include <intelblocks/gpio.h>
13 #include <intelblocks/itss.h>
14 #include <intelblocks/p2sb.h>
15 #include <intelblocks/pcr.h>
16 #include <security/vboot/vboot_common.h>
17 #include <soc/pci_devs.h>
22 #define GPIO_DWx_SIZE(x) (sizeof(uint32_t) * (x))
23 #define PAD_CFG_OFFSET(x, dw_num) ((x) + GPIO_DWx_SIZE(dw_num))
24 #define PAD_CFG0_OFFSET(x) PAD_CFG_OFFSET(x, 0)
25 #define PAD_CFG1_OFFSET(x) PAD_CFG_OFFSET(x, 1)
26 #define PAD_CFG2_OFFSET(x) PAD_CFG_OFFSET(x, 2)
27 #define PAD_CFG3_OFFSET(x) PAD_CFG_OFFSET(x, 3)
29 #define PAD_DW0_MASK (PAD_CFG0_TX_STATE | \
30 PAD_CFG0_TX_DISABLE | PAD_CFG0_RX_DISABLE | PAD_CFG0_MODE_MASK |\
31 PAD_CFG0_ROUTE_MASK | PAD_CFG0_RXTENCFG_MASK | \
32 PAD_CFG0_RXINV_MASK | PAD_CFG0_PREGFRXSEL | \
33 PAD_CFG0_TRIG_MASK | PAD_CFG0_RXRAW1_MASK | PAD_CFG0_NAFVWE_ENABLE |\
34 PAD_CFG0_RXPADSTSEL_MASK | PAD_CFG0_RESET_MASK)
36 #if CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_PADCFG_PADTOL)
37 #define PAD_DW1_MASK (PAD_CFG1_IOSTERM_MASK | \
38 PAD_CFG1_PULL_MASK | \
40 PAD_CFG1_IOSSTATE_MASK)
42 #define PAD_DW1_MASK (PAD_CFG1_IOSTERM_MASK | \
43 PAD_CFG1_PULL_MASK | \
44 PAD_CFG1_IOSSTATE_MASK)
47 #define PAD_DW2_MASK (PAD_CFG2_DEBOUNCE_MASK)
48 #define PAD_DW3_MASK (0)
50 #define MISCCFG_GPE0_DW0_SHIFT 8
51 #define MISCCFG_GPE0_DW0_MASK (0xf << MISCCFG_GPE0_DW0_SHIFT)
52 #define MISCCFG_GPE0_DW1_SHIFT 12
53 #define MISCCFG_GPE0_DW1_MASK (0xf << MISCCFG_GPE0_DW1_SHIFT)
54 #define MISCCFG_GPE0_DW2_SHIFT 16
55 #define MISCCFG_GPE0_DW2_MASK (0xf << MISCCFG_GPE0_DW2_SHIFT)
57 #define GPI_SMI_STS_OFFSET(comm, group) ((comm)->gpi_smi_sts_reg_0 + \
58 ((group) * sizeof(uint32_t)))
59 #define GPI_SMI_EN_OFFSET(comm, group) ((comm)->gpi_smi_en_reg_0 + \
60 ((group) * sizeof(uint32_t)))
61 #define GPI_NMI_STS_OFFSET(comm, group) ((comm)->gpi_nmi_sts_reg_0 + \
62 ((group) * sizeof(uint32_t)))
63 #define GPI_NMI_EN_OFFSET(comm, group) ((comm)->gpi_nmi_en_reg_0 + \
64 ((group) * sizeof(uint32_t)))
65 #define GPI_IS_OFFSET(comm, group) ((comm)->gpi_int_sts_reg_0 + \
66 ((group) * sizeof(uint32_t)))
67 #define GPI_IE_OFFSET(comm, group) ((comm)->gpi_int_en_reg_0 + \
68 ((group) * sizeof(uint32_t)))
69 #define GPI_GPE_STS_OFFSET(comm, group) ((comm)->gpi_gpe_sts_reg_0 + \
70 ((group) * sizeof(uint32_t)))
71 #define GPI_GPE_EN_OFFSET(comm, group) ((comm)->gpi_gpe_en_reg_0 + \
72 ((group) * sizeof(uint32_t)))
74 static inline size_t relative_pad_in_comm(const struct pad_community
*comm
,
77 return gpio
- comm
->first_pad
;
80 /* find the group within the community that the pad is a part of */
81 static inline size_t gpio_group_index(const struct pad_community
*comm
,
82 unsigned int relative_pad
)
87 die("Failed to get comm->groups.");
89 /* find the base pad number for this pad's group */
90 for (i
= 0; i
< comm
->num_groups
; i
++) {
91 if (relative_pad
>= comm
->groups
[i
].first_pad
&&
92 relative_pad
< comm
->groups
[i
].first_pad
+
93 comm
->groups
[i
].size
) {
97 printk(BIOS_ERR
, "%s: pad %d is not found in community %s!\n",
98 __func__
, relative_pad
, comm
->name
);
104 static inline size_t gpio_group_index_scaled(const struct pad_community
*comm
,
105 unsigned int relative_pad
, size_t scale
)
107 return gpio_group_index(comm
, relative_pad
) * scale
;
110 static inline size_t gpio_within_group(const struct pad_community
*comm
,
111 unsigned int relative_pad
)
115 i
= gpio_group_index(comm
, relative_pad
);
117 return relative_pad
- comm
->groups
[i
].first_pad
;
120 static inline uint32_t gpio_bitmask_within_group(
121 const struct pad_community
*comm
,
122 unsigned int relative_pad
)
124 return 1U << gpio_within_group(comm
, relative_pad
);
127 static const struct pad_community
*gpio_get_community(gpio_t pad
)
129 size_t gpio_communities
;
131 const struct pad_community
*comm
;
132 comm
= soc_gpio_get_community(&gpio_communities
);
133 for (i
= 0; i
< gpio_communities
; i
++, comm
++) {
134 if (pad
>= comm
->first_pad
&& pad
<= comm
->last_pad
)
137 printk(BIOS_ERR
, "%s pad %d not found\n", __func__
, pad
);
138 die("Invalid GPIO pad number\n");
142 static void gpio_configure_owner(const struct pad_config
*cfg
,
143 const struct pad_community
*comm
)
146 uint16_t hostsw_own_offset
;
149 pin
= relative_pad_in_comm(comm
, cfg
->pad
);
151 /* Based on the gpio pin number configure the corresponding bit in
152 * HOSTSW_OWN register. Value of 0x1 indicates GPIO Driver onwership.
154 hostsw_own_offset
= comm
->host_own_reg_0
;
155 hostsw_own_offset
+= gpio_group_index_scaled(comm
, pin
,
158 hostsw_own
= pcr_read32(comm
->port
, hostsw_own_offset
);
160 /* The 4th bit in pad_config 1 (RO) is used to indicate if the pad
161 * needs GPIO driver ownership. Set the bit if GPIO driver ownership
162 * requested, otherwise clear the bit.
164 if (cfg
->pad_config
[1] & PAD_CFG_OWN_GPIO_DRIVER
)
165 hostsw_own
|= gpio_bitmask_within_group(comm
, pin
);
167 hostsw_own
&= ~gpio_bitmask_within_group(comm
, pin
);
169 pcr_write32(comm
->port
, hostsw_own_offset
, hostsw_own
);
172 static void gpi_enable_gpe(const struct pad_config
*cfg
,
173 const struct pad_community
*comm
, int group
, int pin
)
178 /* Do not configure GPE_EN if PAD is not configured for SCI/wake */
179 if (((cfg
->pad_config
[0]) & PAD_CFG0_ROUTE_SCI
) != PAD_CFG0_ROUTE_SCI
)
182 /* Get comm offset and bit mask to be set as per pin */
183 en_reg
= GPI_GPE_EN_OFFSET(comm
, group
);
184 en_value
= gpio_bitmask_within_group(comm
, pin
);
186 /* Set enable bits */
187 pcr_or32(comm
->port
, en_reg
, en_value
);
189 if (CONFIG(DEBUG_GPIO
)) {
190 printk(BIOS_DEBUG
, "GPE_EN[0x%02x, %02zd]: Reg: 0x%x, Value = 0x%x\n",
191 comm
->port
, relative_pad_in_comm(comm
, cfg
->pad
), en_reg
,
192 pcr_read32(comm
->port
, en_reg
));
196 static void gpi_enable_smi(const struct pad_config
*cfg
,
197 const struct pad_community
*comm
, int group
, int pin
)
203 if (((cfg
->pad_config
[0]) & PAD_CFG0_ROUTE_SMI
) != PAD_CFG0_ROUTE_SMI
)
206 sts_reg
= GPI_SMI_STS_OFFSET(comm
, group
);
207 en_reg
= GPI_SMI_EN_OFFSET(comm
, group
);
208 en_value
= gpio_bitmask_within_group(comm
, pin
);
210 /* Write back 1 to reset the sts bit */
211 pcr_rmw32(comm
->port
, sts_reg
, en_value
, 0);
213 /* Set enable bits */
214 pcr_or32(comm
->port
, en_reg
, en_value
);
217 static void gpi_enable_nmi(const struct pad_config
*cfg
,
218 const struct pad_community
*comm
, int group
, int pin
)
224 if (((cfg
->pad_config
[0]) & PAD_CFG0_ROUTE_NMI
) != PAD_CFG0_ROUTE_NMI
)
227 /* Do not configure NMI if the platform doesn't support it */
228 if (!comm
->gpi_nmi_sts_reg_0
|| !comm
->gpi_nmi_en_reg_0
)
231 sts_reg
= GPI_NMI_STS_OFFSET(comm
, group
);
232 en_reg
= GPI_NMI_EN_OFFSET(comm
, group
);
233 en_value
= gpio_bitmask_within_group(comm
, pin
);
235 /* Write back 1 to reset the sts bit */
236 pcr_rmw32(comm
->port
, sts_reg
, en_value
, 0);
238 /* Set enable bits */
239 pcr_or32(comm
->port
, en_reg
, en_value
);
242 /* 120 GSIs is the default for IOxAPIC */
243 static uint32_t gpio_ioapic_irqs_used
[120 / (sizeof(uint32_t) * BITS_PER_BYTE
) + 1];
244 static void set_ioapic_used(uint32_t irq
)
246 size_t word_offset
= irq
/ 32;
247 size_t bit_offset
= irq
% 32;
248 assert(word_offset
< ARRAY_SIZE(gpio_ioapic_irqs_used
));
249 gpio_ioapic_irqs_used
[word_offset
] |= BIT(bit_offset
);
252 bool gpio_routes_ioapic_irq(uint32_t irq
)
254 size_t word_offset
= irq
/ 32;
255 size_t bit_offset
= irq
% 32;
256 assert(word_offset
< ARRAY_SIZE(gpio_ioapic_irqs_used
));
257 return (gpio_ioapic_irqs_used
[word_offset
] & BIT(bit_offset
)) != 0;
260 static void gpio_configure_itss(const struct pad_config
*cfg
, uint16_t port
,
261 uint16_t pad_cfg_offset
)
263 /* No ITSS configuration in SMM. */
269 /* Set up ITSS polarity if pad is routed to APIC.
271 * The ITSS takes only active high interrupt signals. Therefore,
272 * if the pad configuration indicates an inversion assume the
273 * intent is for the ITSS polarity. Before forwarding on the
274 * request to the APIC there's an inversion setting for how the
275 * signal is forwarded to the APIC. Honor the inversion setting
276 * in the GPIO pad configuration so that a hardware active low
277 * signal looks that way to the APIC (double inversion).
279 if (!(cfg
->pad_config
[0] & PAD_CFG0_ROUTE_SWAPPED
) &&
280 !(cfg
->pad_config
[0] & PAD_CFG0_ROUTE_IOAPIC
))
283 irq
= pcr_read32(port
, PAD_CFG1_OFFSET(pad_cfg_offset
));
284 irq
&= PAD_CFG1_IRQ_MASK
;
286 printk(BIOS_ERR
, "GPIO %u doesn't support APIC routing,\n",
291 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_ITSS_POL_CFG
) &&
292 !(cfg
->pad_config
[0] & PAD_CFG0_ROUTE_SWAPPED
))
293 itss_set_irq_polarity(irq
, !!(cfg
->pad_config
[0] &
294 PAD_CFG0_RX_POL_INVERT
));
296 set_ioapic_used(irq
);
299 /* Number of DWx config registers can be different for different SOCs */
300 static uint16_t pad_config_offset(const struct pad_community
*comm
, gpio_t pad
)
304 offset
= relative_pad_in_comm(comm
, pad
);
305 offset
*= GPIO_DWx_SIZE(GPIO_NUM_PAD_CFG_REGS
);
306 return offset
+ comm
->pad_cfg_base
;
309 static uint32_t gpio_pad_reset_config_override(const struct pad_community
*comm
,
310 uint32_t config_value
)
312 const struct reset_mapping
*rst_map
= comm
->reset_map
;
315 if (rst_map
== NULL
|| comm
->num_reset_vals
== 0)
316 return config_value
;/* Logical reset values equal chipset
318 for (i
= 0; i
< comm
->num_reset_vals
; i
++, rst_map
++) {
319 if ((config_value
& PAD_CFG0_RESET_MASK
) == rst_map
->logical
) {
320 config_value
&= ~PAD_CFG0_RESET_MASK
;
321 config_value
|= rst_map
->chipset
;
325 printk(BIOS_ERR
, "%s: Logical to Chipset mapping not found\n",
330 static const int mask
[4] = {
331 PAD_DW0_MASK
, PAD_DW1_MASK
, PAD_DW2_MASK
, PAD_DW3_MASK
334 static void gpio_configure_pad(const struct pad_config
*cfg
)
336 const struct pad_community
*comm
;
337 uint16_t config_offset
;
338 uint32_t pad_conf
, soc_pad_conf
;
342 printk(BIOS_ERR
, "%s: cfg value is NULL\n", __func__
);
346 comm
= gpio_get_community(cfg
->pad
);
348 printk(BIOS_ERR
, "%s: Could not find community for pad: 0x%x\n",
353 config_offset
= pad_config_offset(comm
, cfg
->pad
);
354 pin
= relative_pad_in_comm(comm
, cfg
->pad
);
355 group
= gpio_group_index(comm
, pin
);
357 for (i
= 0; i
< GPIO_NUM_PAD_CFG_REGS
; i
++) {
358 pad_conf
= pcr_read32(comm
->port
,
359 PAD_CFG_OFFSET(config_offset
, i
));
361 soc_pad_conf
= cfg
->pad_config
[i
];
363 soc_pad_conf
= gpio_pad_reset_config_override(comm
,
365 soc_pad_conf
&= mask
[i
];
366 soc_pad_conf
|= pad_conf
& ~mask
[i
];
368 /* Patch GPIO settings for SoC specifically */
369 soc_pad_conf
= soc_gpio_pad_config_fixup(cfg
, i
, soc_pad_conf
);
371 if (CONFIG(DEBUG_GPIO
))
373 "gpio_padcfg [0x%02x, %02d] DW%d [0x%08x : 0x%08x"
376 pad_conf
,/* old value */
377 cfg
->pad_config
[i
],/* value passed from gpio table */
378 soc_pad_conf
);/*new value*/
379 pcr_write32(comm
->port
, PAD_CFG_OFFSET(config_offset
, i
),
383 gpio_configure_itss(cfg
, comm
->port
, config_offset
);
384 gpio_configure_owner(cfg
, comm
);
385 gpi_enable_smi(cfg
, comm
, group
, pin
);
386 gpi_enable_nmi(cfg
, comm
, group
, pin
);
387 gpi_enable_gpe(cfg
, comm
, group
, pin
);
388 if (cfg
->lock_action
)
389 gpio_lock_pad(cfg
->pad
, cfg
->lock_action
);
392 void gpio_configure_pads(const struct pad_config
*cfg
, size_t num_pads
)
396 for (i
= 0; i
< num_pads
; i
++)
397 gpio_configure_pad(cfg
+ i
);
401 * This functions checks to see if there is an override config present for the
402 * provided pad_config. If no override config is present, then the input config
403 * is returned. Else, it returns the override config.
405 static const struct pad_config
*gpio_get_config(const struct pad_config
*c
,
406 const struct pad_config
*override_cfg_table
,
411 if (override_cfg_table
== NULL
)
414 for (i
= 0; i
< num
; i
++) {
415 if (c
->pad
== override_cfg_table
[i
].pad
)
416 return override_cfg_table
+ i
;
422 void gpio_configure_pads_with_override(const struct pad_config
*base_cfg
,
423 size_t base_num_pads
,
424 const struct pad_config
*override_cfg
,
425 size_t override_num_pads
)
428 const struct pad_config
*c
;
430 for (i
= 0; i
< base_num_pads
; i
++) {
431 c
= gpio_get_config(base_cfg
+ i
, override_cfg
,
433 gpio_configure_pad(c
);
437 struct pad_config
*new_padbased_table(void)
439 struct pad_config
*padbased_table
;
440 padbased_table
= malloc(sizeof(struct pad_config
) * TOTAL_PADS
);
441 memset(padbased_table
, 0, sizeof(struct pad_config
) * TOTAL_PADS
);
443 return padbased_table
;
446 void gpio_padbased_override(struct pad_config
*padbased_table
,
447 const struct pad_config
*override_cfg
,
448 size_t override_num_pads
)
450 for (size_t i
= 0; i
< override_num_pads
; i
++) {
451 /* Prevent overflow hack */
452 ASSERT(override_cfg
[i
].pad
< TOTAL_PADS
);
453 padbased_table
[override_cfg
[i
].pad
] = override_cfg
[i
];
457 void gpio_configure_pads_with_padbased(struct pad_config
*padbased_table
)
460 const struct pad_config
*cfg
= padbased_table
;
461 for (i
= 0; i
< TOTAL_PADS
; i
++) {
462 /* Consider unmapped pin as default setting, skip */
463 if (cfg
[i
].pad
== 0 && cfg
[i
].pad_config
[0] == 0)
465 gpio_configure_pad(&cfg
[i
]);
469 void *gpio_dwx_address(const gpio_t pad
)
471 /* Calculate Address of DW0 register for given GPIO
473 * returns - address of GPIO
475 const struct pad_community
*comm
= gpio_get_community(pad
);
476 uint16_t config_offset
;
478 config_offset
= pad_config_offset(comm
, pad
);
479 return pcr_reg_address(comm
->port
, config_offset
);
482 uint8_t gpio_get_pad_portid(const gpio_t pad
)
484 /* Get the port id of given pad
486 * returns - given pad port id
488 const struct pad_community
*comm
= gpio_get_community(pad
);
492 void gpio_input_pulldown(gpio_t gpio
)
494 struct pad_config cfg
= PAD_CFG_GPI(gpio
, DN_20K
, DEEP
);
495 gpio_configure_pad(&cfg
);
498 void gpio_input_pullup(gpio_t gpio
)
500 struct pad_config cfg
= PAD_CFG_GPI(gpio
, UP_20K
, DEEP
);
501 gpio_configure_pad(&cfg
);
504 void gpio_input(gpio_t gpio
)
506 struct pad_config cfg
= PAD_CFG_GPI(gpio
, NONE
, DEEP
);
507 gpio_configure_pad(&cfg
);
510 void gpio_output(gpio_t gpio
, int value
)
512 struct pad_config cfg
= PAD_CFG_GPO(gpio
, value
, DEEP
);
513 gpio_configure_pad(&cfg
);
516 int gpio_get(gpio_t gpio_num
)
518 const struct pad_community
*comm
= gpio_get_community(gpio_num
);
519 uint16_t config_offset
;
522 config_offset
= pad_config_offset(comm
, gpio_num
);
523 reg
= pcr_read32(comm
->port
, config_offset
);
525 return !!(reg
& PAD_CFG0_RX_STATE
);
528 int gpio_tx_get(gpio_t gpio_num
)
530 const struct pad_community
*comm
= gpio_get_community(gpio_num
);
531 uint16_t config_offset
;
534 config_offset
= pad_config_offset(comm
, gpio_num
);
535 reg
= pcr_read32(comm
->port
, config_offset
);
537 return !!(reg
& PAD_CFG0_TX_STATE
);
541 gpio_pad_config_lock_using_sbi(const struct gpio_lock_config
*pad_info
,
542 uint8_t pid
, uint16_t offset
, const uint32_t bit_mask
)
547 struct pcr_sbi_msg msg
= {
550 .opcode
= GPIO_LOCK_UNLOCK
,
552 .fast_byte_enable
= 0xf,
557 if (!(pad_info
->lock_action
& GPIO_LOCK_FULL
)) {
558 printk(BIOS_ERR
, "%s: Error: no lock_action specified for pad %d!\n",
559 __func__
, pad_info
->pad
);
563 if ((pad_info
->lock_action
& GPIO_LOCK_CONFIG
) == GPIO_LOCK_CONFIG
) {
564 if (CONFIG(DEBUG_GPIO
))
565 printk(BIOS_INFO
, "%s: Locking pad %d configuration\n",
566 __func__
, pad_info
->pad
);
567 data
= pcr_read32(pid
, offset
) | bit_mask
;
568 status
= pcr_execute_sideband_msg(PCH_DEV_P2SB
, &msg
, &data
, &response
);
569 if (status
|| response
)
570 printk(BIOS_ERR
, "Failed to lock GPIO PAD, response = %d\n", response
);
573 if ((pad_info
->lock_action
& GPIO_LOCK_TX
) == GPIO_LOCK_TX
) {
574 if (CONFIG(DEBUG_GPIO
))
575 printk(BIOS_INFO
, "%s: Locking pad %d Tx state\n",
576 __func__
, pad_info
->pad
);
577 offset
+= sizeof(uint32_t);
578 data
= pcr_read32(pid
, offset
) | bit_mask
;
580 status
= pcr_execute_sideband_msg(PCH_DEV_P2SB
, &msg
, &data
, &response
);
581 if (status
|| response
)
582 printk(BIOS_ERR
, "Failed to lock GPIO PAD Tx state, response = %d\n",
587 int gpio_lock_pads(const struct gpio_lock_config
*pad_list
, const size_t count
)
589 const struct pad_community
*comm
;
594 if (!CONFIG(SOC_INTEL_COMMON_BLOCK_SMM_LOCK_GPIO_PADS
))
598 * FSP-S will unlock all the GPIO pads and hide the P2SB device. With
599 * the device hidden, we will not be able to send the sideband interface
600 * message to lock the GPIO configuration. Therefore, we need to unhide
601 * the P2SB device which can only be done in SMM requiring that this
602 * function is called from SMM.
605 printk(BIOS_ERR
, "%s: Error: must be called from SMM!\n", __func__
);
609 if ((pad_list
== NULL
) || (count
== 0)) {
610 printk(BIOS_ERR
, "%s: Error: pad_list null or count = 0!\n", __func__
);
616 for (int x
= 0; x
< count
; x
++) {
617 pad
= pad_list
[x
].pad
;
618 comm
= gpio_get_community(pad
);
619 rel_pad
= relative_pad_in_comm(comm
, pad
);
620 offset
= comm
->pad_cfg_lock_offset
;
622 printk(BIOS_ERR
, "%s: Error: offset not defined for pad %d!\n",
626 /* PADCFGLOCK and PADCFGLOCKTX registers for each community are contiguous */
627 offset
+= gpio_group_index_scaled(comm
, rel_pad
, 2 * sizeof(uint32_t));
629 const uint32_t bit_mask
= gpio_bitmask_within_group(comm
, rel_pad
);
631 gpio_pad_config_lock_using_sbi(&pad_list
[x
], comm
->port
, offset
, bit_mask
);
638 gpio_pad_config_lock_using_pcr(const struct gpio_lock_config
*pad_info
,
639 uint8_t pid
, uint16_t offset
, const uint32_t bit_mask
)
641 if ((pad_info
->lock_action
& GPIO_LOCK_CONFIG
) == GPIO_LOCK_CONFIG
) {
642 if (CONFIG(DEBUG_GPIO
))
643 printk(BIOS_INFO
, "%s: Locking pad %d configuration\n",
644 __func__
, pad_info
->pad
);
645 pcr_or32(pid
, offset
, bit_mask
);
648 if ((pad_info
->lock_action
& GPIO_LOCK_TX
) == GPIO_LOCK_TX
) {
649 if (CONFIG(DEBUG_GPIO
))
650 printk(BIOS_INFO
, "%s: Locking pad %d TX state\n",
651 __func__
, pad_info
->pad
);
652 pcr_or32(pid
, offset
+ sizeof(uint32_t), bit_mask
);
656 static int gpio_non_smm_lock_pad(const struct gpio_lock_config
*pad_info
)
658 const struct pad_community
*comm
= gpio_get_community(pad_info
->pad
);
663 printk(BIOS_ERR
, "%s: Error: pad_info is null!\n", __func__
);
667 if (cpu_soc_is_in_untrusted_mode()) {
668 printk(BIOS_ERR
, "%s: Error: IA Untrusted Mode enabled, can't lock pad!\n",
673 rel_pad
= relative_pad_in_comm(comm
, pad_info
->pad
);
674 offset
= comm
->pad_cfg_lock_offset
;
676 printk(BIOS_ERR
, "%s: Error: offset not defined for pad %d!\n",
677 __func__
, pad_info
->pad
);
681 /* PADCFGLOCK and PADCFGLOCKTX registers for each community are contiguous */
682 offset
+= gpio_group_index_scaled(comm
, rel_pad
, 2 * sizeof(uint32_t));
683 const uint32_t bit_mask
= gpio_bitmask_within_group(comm
, rel_pad
);
685 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_LOCK_USING_PCR
)) {
686 if (CONFIG(DEBUG_GPIO
))
687 printk(BIOS_INFO
, "Locking pad configuration using PCR\n");
688 gpio_pad_config_lock_using_pcr(pad_info
, comm
->port
, offset
, bit_mask
);
689 } else if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_LOCK_USING_SBI
)) {
690 if (CONFIG(DEBUG_GPIO
))
691 printk(BIOS_INFO
, "Locking pad configuration using SBI\n");
692 gpio_pad_config_lock_using_sbi(pad_info
, comm
->port
, offset
, bit_mask
);
694 printk(BIOS_ERR
, "%s: Error: No pad configuration lock method is selected!\n",
701 int gpio_lock_pad(const gpio_t pad
, enum gpio_lock_action lock_action
)
703 /* Skip locking GPIO PAD in early stages or in recovery mode */
704 if (ENV_ROMSTAGE_OR_BEFORE
|| vboot_recovery_mode_enabled())
707 const struct gpio_lock_config pads
= {
709 .lock_action
= lock_action
712 if (!ENV_SMM
&& !CONFIG(SOC_INTEL_COMMON_BLOCK_SMM_LOCK_GPIO_PADS
))
713 return gpio_non_smm_lock_pad(&pads
);
715 return gpio_lock_pads(&pads
, 1);
718 void gpio_set(gpio_t gpio_num
, int value
)
720 const struct pad_community
*comm
= gpio_get_community(gpio_num
);
721 uint16_t config_offset
;
723 config_offset
= pad_config_offset(comm
, gpio_num
);
724 pcr_rmw32(comm
->port
, config_offset
,
725 ~PAD_CFG0_TX_STATE
, (!!value
& PAD_CFG0_TX_STATE
));
728 uint16_t gpio_acpi_pin(gpio_t gpio_num
)
730 const struct pad_community
*comm
;
733 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_MULTI_ACPI_DEVICES
))
734 return relative_pad_in_comm(gpio_get_community(gpio_num
),
737 comm
= gpio_get_community(gpio_num
);
738 pin
= relative_pad_in_comm(comm
, gpio_num
);
739 group
= gpio_group_index(comm
, pin
);
741 /* If pad base is not set then use GPIO number as ACPI pin number. */
742 if (comm
->groups
[group
].acpi_pad_base
== PAD_BASE_NONE
)
746 * If this group has a non-zero pad base then compute the ACPI pin
747 * number from the pad base and the relative pad in the group.
749 return comm
->groups
[group
].acpi_pad_base
+ gpio_within_group(comm
, pin
);
752 static void print_gpi_status(const struct gpi_status
*sts
)
760 size_t gpio_communities
;
761 const struct pad_community
*comm
;
763 comm
= soc_gpio_get_community(&gpio_communities
);
764 for (i
= 0; i
< gpio_communities
; i
++) {
765 num_groups
= comm
->num_gpi_regs
;
766 index
= comm
->gpi_status_offset
;
767 for (group
= 0; group
< num_groups
; group
++, index
++) {
768 for (bit_set
= comm
->max_pads_per_group
- 1;
769 bit_set
>= 0; bit_set
--) {
770 if (!(sts
->grp
[index
] & (1 << bit_set
)))
774 abs_bit
+= group
* comm
->max_pads_per_group
;
775 printk(BIOS_DEBUG
, "%s %d\n", comm
->name
,
783 void gpi_clear_get_smi_status(struct gpi_status
*sts
)
790 size_t gpio_communities
;
792 const struct pad_community
*comm
;
794 comm
= soc_gpio_get_community(&gpio_communities
);
795 for (i
= 0; i
< gpio_communities
; i
++) {
796 num_groups
= comm
->num_gpi_regs
;
797 index
= comm
->gpi_status_offset
;
798 for (group
= 0; group
< num_groups
; group
++, index
++) {
799 sts_value
= pcr_read32(comm
->port
,
800 GPI_SMI_STS_OFFSET(comm
, group
));
801 en_value
= pcr_read32(comm
->port
,
802 GPI_SMI_EN_OFFSET(comm
, group
));
803 sts
->grp
[index
] = sts_value
& en_value
;
804 /* Clear the set status bits. */
805 pcr_write32(comm
->port
, GPI_SMI_STS_OFFSET(comm
,
806 group
), sts
->grp
[index
]);
811 if (CONFIG(DEBUG_SMI
))
812 print_gpi_status(sts
);
815 int gpi_status_get(const struct gpi_status
*sts
, gpio_t pad
)
818 const struct pad_community
*comm
= gpio_get_community(pad
);
820 pad
= relative_pad_in_comm(comm
, pad
);
821 sts_index
= comm
->gpi_status_offset
;
822 sts_index
+= gpio_group_index(comm
, pad
);
824 return !!(sts
->grp
[sts_index
] & gpio_bitmask_within_group(comm
, pad
));
827 static int gpio_route_pmc_gpio_gpe(int pmc_gpe_num
)
830 const struct pmc_to_gpio_route
*routes
;
833 routes
= soc_pmc_gpio_routes(&num_routes
);
834 assert(routes
!= NULL
);
835 for (i
= 0; i
< num_routes
; i
++, routes
++) {
836 if (pmc_gpe_num
== routes
->pmc
)
842 void gpio_route_gpe(uint8_t gpe0b
, uint8_t gpe0c
, uint8_t gpe0d
)
845 uint32_t misccfg_mask
;
846 uint32_t misccfg_value
;
848 size_t gpio_communities
;
849 const struct pad_community
*comm
;
851 /* Get the group here for community specific MISCCFG register.
852 * If any of these returns -1 then there is some error in devicetree
853 * where the group is probably hardcoded and does not comply with the
854 * PMC group defines. So we return from here and MISCFG is set to
857 ret
= gpio_route_pmc_gpio_gpe(gpe0b
);
862 ret
= gpio_route_pmc_gpio_gpe(gpe0c
);
867 ret
= gpio_route_pmc_gpio_gpe(gpe0d
);
872 misccfg_value
= gpe0b
<< MISCCFG_GPE0_DW0_SHIFT
;
873 misccfg_value
|= gpe0c
<< MISCCFG_GPE0_DW1_SHIFT
;
874 misccfg_value
|= gpe0d
<< MISCCFG_GPE0_DW2_SHIFT
;
876 /* Program GPIO_MISCCFG */
877 misccfg_mask
= ~(MISCCFG_GPE0_DW2_MASK
|
878 MISCCFG_GPE0_DW1_MASK
|
879 MISCCFG_GPE0_DW0_MASK
);
881 if (CONFIG(DEBUG_GPIO
))
882 printk(BIOS_DEBUG
, "misccfg_mask:%x misccfg_value:%x\n",
883 misccfg_mask
, misccfg_value
);
884 comm
= soc_gpio_get_community(&gpio_communities
);
885 for (i
= 0; i
< gpio_communities
; i
++, comm
++)
886 pcr_rmw32(comm
->port
, GPIO_MISCCFG
,
887 misccfg_mask
, misccfg_value
);
890 const char *gpio_acpi_path(gpio_t gpio_num
)
892 const struct pad_community
*comm
= gpio_get_community(gpio_num
);
893 return comm
->acpi_path
;
896 uint32_t __weak
soc_gpio_pad_config_fixup(const struct pad_config
*cfg
,
897 int dw_reg
, uint32_t reg_val
)
902 void gpi_clear_int_cfg(void)
904 int i
, group
, num_groups
;
906 size_t gpio_communities
;
907 const struct pad_community
*comm
;
909 comm
= soc_gpio_get_community(&gpio_communities
);
910 for (i
= 0; i
< gpio_communities
; i
++, comm
++) {
911 num_groups
= comm
->num_gpi_regs
;
912 for (group
= 0; group
< num_groups
; group
++) {
913 /* Clear the enable register */
914 pcr_write32(comm
->port
, GPI_IE_OFFSET(comm
, group
), 0);
916 /* Read and clear the set status register bits*/
917 sts_value
= pcr_read32(comm
->port
,
918 GPI_IS_OFFSET(comm
, group
));
919 pcr_write32(comm
->port
,
920 GPI_IS_OFFSET(comm
, group
), sts_value
);
925 /* The function performs GPIO Power Management programming. */
926 void gpio_pm_configure(const uint8_t *misccfg_pm_values
, size_t num
)
929 size_t gpio_communities
;
930 const uint8_t misccfg_pm_mask
= (uint8_t)~MISCCFG_GPIO_PM_CONFIG_BITS
;
931 const struct pad_community
*comm
;
933 comm
= soc_gpio_get_community(&gpio_communities
);
934 if (gpio_communities
!= num
)
935 die("Incorrect GPIO community count!\n");
937 /* Program GPIO_MISCCFG */
938 for (i
= 0; i
< num
; i
++, comm
++)
939 pcr_rmw8(comm
->port
, GPIO_MISCCFG
,
940 misccfg_pm_mask
, misccfg_pm_values
[i
]);
943 size_t gpio_get_index_in_group(gpio_t pad
)
945 const struct pad_community
*comm
;
948 comm
= gpio_get_community(pad
);
949 pin
= relative_pad_in_comm(comm
, pad
);
950 return gpio_within_group(comm
, pin
);
953 static uint32_t *snapshot
;
955 static void *allocate_snapshot_space(void)
957 size_t gpio_communities
, total
= 0, i
;
958 const struct pad_community
*comm
;
960 comm
= soc_gpio_get_community(&gpio_communities
);
961 for (i
= 0; i
< gpio_communities
; i
++, comm
++)
962 total
+= comm
->last_pad
- comm
->first_pad
+ 1;
967 return malloc(total
* GPIO_NUM_PAD_CFG_REGS
* sizeof(uint32_t));
970 void gpio_snapshot(void)
972 size_t gpio_communities
, index
, i
, pad
, reg
;
973 const struct pad_community
*comm
;
974 uint16_t config_offset
;
976 if (snapshot
== NULL
) {
977 snapshot
= allocate_snapshot_space();
978 if (snapshot
== NULL
)
982 comm
= soc_gpio_get_community(&gpio_communities
);
983 for (i
= 0, index
= 0; i
< gpio_communities
; i
++, comm
++) {
984 for (pad
= comm
->first_pad
; pad
<= comm
->last_pad
; pad
++) {
985 config_offset
= pad_config_offset(comm
, pad
);
986 for (reg
= 0; reg
< GPIO_NUM_PAD_CFG_REGS
; reg
++) {
987 snapshot
[index
] = pcr_read32(comm
->port
,
988 PAD_CFG_OFFSET(config_offset
, reg
));
995 size_t gpio_verify_snapshot(void)
997 size_t gpio_communities
, index
, i
, pad
, reg
;
998 const struct pad_community
*comm
;
1000 uint16_t config_offset
;
1003 if (snapshot
== NULL
)
1006 comm
= soc_gpio_get_community(&gpio_communities
);
1007 for (i
= 0, index
= 0; i
< gpio_communities
; i
++, comm
++) {
1008 for (pad
= comm
->first_pad
; pad
<= comm
->last_pad
; pad
++) {
1009 config_offset
= pad_config_offset(comm
, pad
);
1010 for (reg
= 0; reg
< GPIO_NUM_PAD_CFG_REGS
; reg
++) {
1011 curr_val
= pcr_read32(comm
->port
,
1012 PAD_CFG_OFFSET(config_offset
, reg
));
1013 if (curr_val
!= snapshot
[index
]) {
1015 "%zd(DW%zd): Changed from 0x%x to 0x%x\n",
1016 pad
, reg
, snapshot
[index
], curr_val
);
1027 static void snapshot_cleanup(void *unused
)
1032 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME
, BS_ON_ENTRY
, snapshot_cleanup
, NULL
);
1033 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD
, BS_ON_EXIT
, snapshot_cleanup
, NULL
);
1035 bool gpio_get_vw_info(gpio_t pad
, unsigned int *vw_index
, unsigned int *vw_bit
)
1037 const struct pad_community
*comm
;
1038 unsigned int offset
= 0;
1041 comm
= gpio_get_community(pad
);
1042 for (i
= 0; i
< comm
->num_vw_entries
; i
++) {
1043 if (pad
>= comm
->vw_entries
[i
].first_pad
&& pad
<= comm
->vw_entries
[i
].last_pad
)
1046 offset
+= 1 + comm
->vw_entries
[i
].last_pad
- comm
->vw_entries
[i
].first_pad
;
1049 if (i
== comm
->num_vw_entries
)
1052 offset
+= pad
- comm
->vw_entries
[i
].first_pad
;
1053 *vw_index
= comm
->vw_base
+ offset
/ 8;
1054 *vw_bit
= offset
% 8;
1059 unsigned int gpio_get_pad_cpu_portid(gpio_t pad
)
1061 const struct pad_community
*comm
= gpio_get_community(pad
);
1062 return comm
->cpu_port
;