Debian Codegraph @

src/soc/intel/common/block/gpio/gpio.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #define __SIMPLE_DEVICE__
4
5 #include <assert.h>
6 #include <bootstate.h>
7 #include <console/console.h>
8 #include <device/device.h>
9 #include <fsp/debug.h>
10 #include <intelblocks/cpulib.h>
11 #include <intelblocks/gpio.h>
12 #include <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>
18 #include <soc/pm.h>
19 #include <stdlib.h>
20 #include <types.h>
21
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)
28
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)
35
36 #if CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_PADCFG_PADTOL)
37 #define PAD_DW1_MASK (PAD_CFG1_IOSTERM_MASK | \
38 PAD_CFG1_PULL_MASK | \
39 PAD_CFG1_TOL_MASK | \
40 PAD_CFG1_IOSSTATE_MASK)
41 #else
42 #define PAD_DW1_MASK (PAD_CFG1_IOSTERM_MASK | \
43 PAD_CFG1_PULL_MASK | \
44 PAD_CFG1_IOSSTATE_MASK)
45 #endif
46
47 #define PAD_DW2_MASK (PAD_CFG2_DEBOUNCE_MASK)
48 #define PAD_DW3_MASK (0)
49
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)
56
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)))
73
74 static inline size_t relative_pad_in_comm(const struct pad_community *comm,
75 gpio_t gpio)
76 {
77 return gpio - comm->first_pad;
78 }
79
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)
83 {
84 size_t i;
85
86 if (!comm->groups)
87 die("Failed to get comm->groups.");
88
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) {
94 return i;
95 }
96 }
97 printk(BIOS_ERR, "%s: pad %d is not found in community %s!\n",
98 __func__, relative_pad, comm->name);
99 BUG();
100
101 return i;
102 }
103
104 static inline size_t gpio_group_index_scaled(const struct pad_community *comm,
105 unsigned int relative_pad, size_t scale)
106 {
107 return gpio_group_index(comm, relative_pad) * scale;
108 }
109
110 static inline size_t gpio_within_group(const struct pad_community *comm,
111 unsigned int relative_pad)
112 {
113 size_t i;
114
115 i = gpio_group_index(comm, relative_pad);
116
117 return relative_pad - comm->groups[i].first_pad;
118 }
119
120 static inline uint32_t gpio_bitmask_within_group(
121 const struct pad_community *comm,
122 unsigned int relative_pad)
123 {
124 return 1U << gpio_within_group(comm, relative_pad);
125 }
126
127 static const struct pad_community *gpio_get_community(gpio_t pad)
128 {
129 size_t gpio_communities;
130 size_t i;
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)
135 return comm;
136 }
137 printk(BIOS_ERR, "%s pad %d not found\n", __func__, pad);
138 die("Invalid GPIO pad number\n");
139 return NULL;
140 }
141
142 static void gpio_configure_owner(const struct pad_config *cfg,
143 const struct pad_community *comm)
144 {
145 uint32_t hostsw_own;
146 uint16_t hostsw_own_offset;
147 int pin;
148
149 pin = relative_pad_in_comm(comm, cfg->pad);
150
151 /* Based on the gpio pin number configure the corresponding bit in
152 * HOSTSW_OWN register. Value of 0x1 indicates GPIO Driver onwership.
153 */
154 hostsw_own_offset = comm->host_own_reg_0;
155 hostsw_own_offset += gpio_group_index_scaled(comm, pin,
156 sizeof(uint32_t));
157
158 hostsw_own = pcr_read32(comm->port, hostsw_own_offset);
159
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.
163 */
164 if (cfg->pad_config[1] & PAD_CFG_OWN_GPIO_DRIVER)
165 hostsw_own |= gpio_bitmask_within_group(comm, pin);
166 else
167 hostsw_own &= ~gpio_bitmask_within_group(comm, pin);
168
169 pcr_write32(comm->port, hostsw_own_offset, hostsw_own);
170
171 if (CONFIG(DEBUG_GPIO)) {
172 printk(BIOS_DEBUG, "HOST_OWN[0x%02x, %02zu]: Reg: 0x%x, Value = 0x%08x\n",
173 comm->port, relative_pad_in_comm(comm, cfg->pad), hostsw_own_offset,
174 pcr_read32(comm->port, hostsw_own_offset));
175 }
176 }
177
178 static void gpi_enable_gpe(const struct pad_config *cfg,
179 const struct pad_community *comm, int group, int pin)
180 {
181 uint16_t en_reg;
182 uint32_t en_value;
183
184 /* Do not configure GPE_EN if PAD is not configured for SCI/wake */
185 if (((cfg->pad_config[0]) & PAD_CFG0_ROUTE_SCI) != PAD_CFG0_ROUTE_SCI)
186 return;
187
188 /* Get comm offset and bit mask to be set as per pin */
189 en_reg = GPI_GPE_EN_OFFSET(comm, group);
190 en_value = gpio_bitmask_within_group(comm, pin);
191
192 /* Set enable bits */
193 pcr_or32(comm->port, en_reg, en_value);
194
195 if (CONFIG(DEBUG_GPIO)) {
196 printk(BIOS_DEBUG, "GPE_EN[0x%02x, %02zu]: Reg: 0x%x, Value = 0x%08x\n",
197 comm->port, relative_pad_in_comm(comm, cfg->pad), en_reg,
198 pcr_read32(comm->port, en_reg));
199 }
200 }
201
202 static void gpi_enable_smi(const struct pad_config *cfg,
203 const struct pad_community *comm, int group, int pin)
204 {
205 uint16_t sts_reg;
206 uint16_t en_reg;
207 uint32_t en_value;
208
209 if (((cfg->pad_config[0]) & PAD_CFG0_ROUTE_SMI) != PAD_CFG0_ROUTE_SMI)
210 return;
211
212 sts_reg = GPI_SMI_STS_OFFSET(comm, group);
213 en_reg = GPI_SMI_EN_OFFSET(comm, group);
214 en_value = gpio_bitmask_within_group(comm, pin);
215
216 /* Write back 1 to reset the sts bit */
217 pcr_rmw32(comm->port, sts_reg, en_value, 0);
218
219 /* Set enable bits */
220 pcr_or32(comm->port, en_reg, en_value);
221
222 if (CONFIG(DEBUG_GPIO)) {
223 printk(BIOS_DEBUG, "SMI_EN[0x%02x, %02zu]: Reg: 0x%x, Value = 0x%08x\n",
224 comm->port, relative_pad_in_comm(comm, cfg->pad), en_reg,
225 pcr_read32(comm->port, en_reg));
226 }
227 }
228
229 static void gpi_enable_nmi(const struct pad_config *cfg,
230 const struct pad_community *comm, int group, int pin)
231 {
232 uint16_t sts_reg;
233 uint16_t en_reg;
234 uint32_t en_value;
235
236 if (((cfg->pad_config[0]) & PAD_CFG0_ROUTE_NMI) != PAD_CFG0_ROUTE_NMI)
237 return;
238
239 /* Do not configure NMI if the platform doesn't support it */
240 if (!comm->gpi_nmi_sts_reg_0 || !comm->gpi_nmi_en_reg_0)
241 return;
242
243 sts_reg = GPI_NMI_STS_OFFSET(comm, group);
244 en_reg = GPI_NMI_EN_OFFSET(comm, group);
245 en_value = gpio_bitmask_within_group(comm, pin);
246
247 /* Write back 1 to reset the sts bit */
248 pcr_rmw32(comm->port, sts_reg, en_value, 0);
249
250 /* Set enable bits */
251 pcr_or32(comm->port, en_reg, en_value);
252
253 if (CONFIG(DEBUG_GPIO)) {
254 printk(BIOS_DEBUG, "NMI_EN[0x%02x, %02zu]: Reg: 0x%x, Value = 0x%08x\n",
255 comm->port, relative_pad_in_comm(comm, cfg->pad), en_reg,
256 pcr_read32(comm->port, en_reg));
257 }
258 }
259
260 /* 120 GSIs is the default for IOxAPIC */
261 static uint32_t gpio_ioapic_irqs_used[120 / (sizeof(uint32_t) * BITS_PER_BYTE) + 1];
262 static void set_ioapic_used(uint32_t irq)
263 {
264 size_t word_offset = irq / 32;
265 size_t bit_offset = irq % 32;
266 assert(word_offset < ARRAY_SIZE(gpio_ioapic_irqs_used));
267 gpio_ioapic_irqs_used[word_offset] |= BIT(bit_offset);
268 }
269
270 bool gpio_routes_ioapic_irq(uint32_t irq)
271 {
272 size_t word_offset = irq / 32;
273 size_t bit_offset = irq % 32;
274 assert(word_offset < ARRAY_SIZE(gpio_ioapic_irqs_used));
275 return (gpio_ioapic_irqs_used[word_offset] & BIT(bit_offset)) != 0;
276 }
277
278 static void gpio_configure_itss(const struct pad_config *cfg, uint16_t port,
279 uint16_t pad_cfg_offset)
280 {
281 /* No ITSS configuration in SMM. */
282 if (ENV_SMM)
283 return;
284
285 int irq;
286
287 /* Set up ITSS polarity if pad is routed to APIC.
288 *
289 * The ITSS takes only active high interrupt signals. Therefore,
290 * if the pad configuration indicates an inversion assume the
291 * intent is for the ITSS polarity. Before forwarding on the
292 * request to the APIC there's an inversion setting for how the
293 * signal is forwarded to the APIC. Honor the inversion setting
294 * in the GPIO pad configuration so that a hardware active low
295 * signal looks that way to the APIC (double inversion).
296 */
297 if (!(cfg->pad_config[0] & PAD_CFG0_ROUTE_IOAPIC))
298 return;
299
300 irq = pcr_read32(port, PAD_CFG1_OFFSET(pad_cfg_offset));
301 irq &= PAD_CFG1_IRQ_MASK;
302 if (!irq) {
303 printk(BIOS_ERR, "GPIO %u doesn't support APIC routing,\n",
304 cfg->pad);
305 return;
306 }
307
308 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_ITSS_POL_CFG))
309 itss_set_irq_polarity(irq, !!(cfg->pad_config[0] &
310 PAD_CFG0_RX_POL_INVERT));
311
312 set_ioapic_used(irq);
313 }
314
315 /* Number of DWx config registers can be different for different SOCs */
316 static uint16_t pad_config_offset(const struct pad_community *comm, gpio_t pad)
317 {
318 size_t offset;
319
320 offset = relative_pad_in_comm(comm, pad);
321 offset *= GPIO_DWx_SIZE(GPIO_NUM_PAD_CFG_REGS);
322 return offset + comm->pad_cfg_base;
323 }
324
325 static uint32_t gpio_pad_reset_config_override(const struct pad_community *comm,
326 uint32_t config_value)
327 {
328 const struct reset_mapping *rst_map = comm->reset_map;
329 int i;
330
331 if (rst_map == NULL || comm->num_reset_vals == 0)
332 return config_value;/* Logical reset values equal chipset
333 values */
334 for (i = 0; i < comm->num_reset_vals; i++, rst_map++) {
335 if ((config_value & PAD_CFG0_RESET_MASK) == rst_map->logical) {
336 config_value &= ~PAD_CFG0_RESET_MASK;
337 config_value |= rst_map->chipset;
338 return config_value;
339 }
340 }
341 printk(BIOS_ERR, "%s: Logical to Chipset mapping not found\n",
342 __func__);
343 return config_value;
344 }
345
346 static const int mask[4] = {
347 PAD_DW0_MASK, PAD_DW1_MASK, PAD_DW2_MASK, PAD_DW3_MASK
348 };
349
350 static void gpio_configure_pad(const struct pad_config *cfg)
351 {
352 const struct pad_community *comm;
353 uint16_t config_offset;
354 uint32_t pad_conf, soc_pad_conf;
355 int i, pin, group;
356
357 if (!cfg) {
358 printk(BIOS_ERR, "%s: cfg value is NULL\n", __func__);
359 return;
360 }
361
362 comm = gpio_get_community(cfg->pad);
363 if (!comm) {
364 printk(BIOS_ERR, "%s: Could not find community for pad: 0x%x\n",
365 __func__, cfg->pad);
366 return;
367 }
368
369 config_offset = pad_config_offset(comm, cfg->pad);
370 pin = relative_pad_in_comm(comm, cfg->pad);
371 group = gpio_group_index(comm, pin);
372
373 for (i = 0; i < GPIO_NUM_PAD_CFG_REGS; i++) {
374 pad_conf = pcr_read32(comm->port,
375 PAD_CFG_OFFSET(config_offset, i));
376
377 soc_pad_conf = cfg->pad_config[i];
378 if (i == 0)
379 soc_pad_conf = gpio_pad_reset_config_override(comm,
380 soc_pad_conf);
381 soc_pad_conf &= mask[i];
382 soc_pad_conf |= pad_conf & ~mask[i];
383
384 /* Patch GPIO settings for SoC specifically */
385 soc_pad_conf = soc_gpio_pad_config_fixup(cfg, i, soc_pad_conf);
386
387 pcr_write32(comm->port, PAD_CFG_OFFSET(config_offset, i),
388 soc_pad_conf);
389
390 if (CONFIG(DEBUG_GPIO))
391 printk(BIOS_DEBUG,
392 "gpio_padcfg [0x%02x, %02d] DW%d [0x%08x : 0x%08x : 0x%08x : 0x%08x]\n",
393 comm->port, pin, i,
394 pad_conf, /* old value */
395 cfg->pad_config[i], /* value passed from gpio table */
396 soc_pad_conf, /* new value */
397 pcr_read32(comm->port, PAD_CFG_OFFSET(config_offset, i))); /* updated value */
398 }
399
400 gpio_configure_itss(cfg, comm->port, config_offset);
401 gpio_configure_owner(cfg, comm);
402 gpi_enable_smi(cfg, comm, group, pin);
403 gpi_enable_nmi(cfg, comm, group, pin);
404 gpi_enable_gpe(cfg, comm, group, pin);
405 if (cfg->lock_action)
406 gpio_lock_pad(cfg->pad, cfg->lock_action);
407 }
408
409 void gpio_configure_pads(const struct pad_config *cfg, size_t num_pads)
410 {
411 size_t i;
412
413 for (i = 0; i < num_pads; i++)
414 gpio_configure_pad(cfg + i);
415 }
416
417 /*
418 * This functions checks to see if there is an override config present for the
419 * provided pad_config. If no override config is present, then the input config
420 * is returned. Else, it returns the override config.
421 */
422 static const struct pad_config *gpio_get_config(const struct pad_config *c,
423 const struct pad_config *override_cfg_table,
424 size_t num)
425 {
426 size_t i;
427
428 if (override_cfg_table == NULL)
429 return c;
430
431 for (i = 0; i < num; i++) {
432 if (c->pad == override_cfg_table[i].pad)
433 return override_cfg_table + i;
434 }
435
436 return c;
437 }
438
439 void gpio_configure_pads_with_override(const struct pad_config *base_cfg,
440 size_t base_num_pads,
441 const struct pad_config *override_cfg,
442 size_t override_num_pads)
443 {
444 size_t i;
445 const struct pad_config *c;
446
447 for (i = 0; i < base_num_pads; i++) {
448 c = gpio_get_config(base_cfg + i, override_cfg,
449 override_num_pads);
450 gpio_configure_pad(c);
451 }
452 }
453
454 struct pad_config *new_padbased_table(void)
455 {
456 struct pad_config *padbased_table;
457 padbased_table = malloc(sizeof(struct pad_config) * TOTAL_PADS);
458 memset(padbased_table, 0, sizeof(struct pad_config) * TOTAL_PADS);
459
460 return padbased_table;
461 }
462
463 void gpio_padbased_override(struct pad_config *padbased_table,
464 const struct pad_config *override_cfg,
465 size_t override_num_pads)
466 {
467 for (size_t i = 0; i < override_num_pads; i++) {
468 /* Prevent overflow hack */
469 ASSERT(override_cfg[i].pad < TOTAL_PADS);
470 padbased_table[override_cfg[i].pad] = override_cfg[i];
471 }
472 }
473
474 void gpio_configure_pads_with_padbased(struct pad_config *padbased_table)
475 {
476 size_t i;
477 const struct pad_config *cfg = padbased_table;
478 for (i = 0; i < TOTAL_PADS; i++) {
479 /* Consider unmapped pin as default setting, skip */
480 if (cfg[i].pad == 0 && cfg[i].pad_config[0] == 0)
481 continue;
482 gpio_configure_pad(&cfg[i]);
483 }
484 }
485
486 void *gpio_dwx_address(const gpio_t pad)
487 {
488 /* Calculate Address of DW0 register for given GPIO
489 * pad - GPIO number
490 * returns - address of GPIO
491 */
492 const struct pad_community *comm = gpio_get_community(pad);
493 uint16_t config_offset;
494
495 config_offset = pad_config_offset(comm, pad);
496 return pcr_reg_address(comm->port, config_offset);
497 }
498
499 uint8_t gpio_get_pad_portid(const gpio_t pad)
500 {
501 /* Get the port id of given pad
502 * pad - GPIO number
503 * returns - given pad port id
504 */
505 const struct pad_community *comm = gpio_get_community(pad);
506 return comm->port;
507 }
508
509 void gpio_input_pulldown(gpio_t gpio)
510 {
511 struct pad_config cfg = PAD_CFG_GPI(gpio, DN_20K, DEEP);
512 gpio_configure_pad(&cfg);
513 }
514
515 void gpio_input_pullup(gpio_t gpio)
516 {
517 struct pad_config cfg = PAD_CFG_GPI(gpio, UP_20K, DEEP);
518 gpio_configure_pad(&cfg);
519 }
520
521 void gpio_input(gpio_t gpio)
522 {
523 struct pad_config cfg = PAD_CFG_GPI(gpio, NONE, DEEP);
524 gpio_configure_pad(&cfg);
525 }
526
527 void gpio_output(gpio_t gpio, int value)
528 {
529 struct pad_config cfg = PAD_CFG_GPO(gpio, value, DEEP);
530 gpio_configure_pad(&cfg);
531 }
532
533 int gpio_get(gpio_t gpio_num)
534 {
535 const struct pad_community *comm = gpio_get_community(gpio_num);
536 uint16_t config_offset;
537 uint32_t reg;
538
539 config_offset = pad_config_offset(comm, gpio_num);
540 reg = pcr_read32(comm->port, config_offset);
541
542 return !!(reg & PAD_CFG0_RX_STATE);
543 }
544
545 int gpio_tx_get(gpio_t gpio_num)
546 {
547 const struct pad_community *comm = gpio_get_community(gpio_num);
548 uint16_t config_offset;
549 uint32_t reg;
550
551 config_offset = pad_config_offset(comm, gpio_num);
552 reg = pcr_read32(comm->port, config_offset);
553
554 return !!(reg & PAD_CFG0_TX_STATE);
555 }
556
557 static void
558 gpio_pad_config_lock_using_sbi(const struct gpio_lock_config *pad_info,
559 uint8_t pid, uint16_t offset, const uint32_t bit_mask)
560 {
561 int status;
562 uint8_t response;
563 uint32_t data;
564 struct pcr_sbi_msg msg = {
565 .pid = pid,
566 .offset = offset,
567 .opcode = GPIO_LOCK_UNLOCK,
568 .is_posted = false,
569 .fast_byte_enable = 0xf,
570 .bar = 0,
571 .fid = 0,
572 };
573
574 if (!(pad_info->lock_action & GPIO_LOCK_FULL)) {
575 printk(BIOS_ERR, "%s: Error: no lock_action specified for pad %d!\n",
576 __func__, pad_info->pad);
577 return;
578 }
579
580 if ((pad_info->lock_action & GPIO_LOCK_CONFIG) == GPIO_LOCK_CONFIG) {
581 if (CONFIG(DEBUG_GPIO))
582 printk(BIOS_INFO, "%s: Locking pad %d configuration\n",
583 __func__, pad_info->pad);
584 data = pcr_read32(pid, offset) | bit_mask;
585 status = pcr_execute_sideband_msg(PCH_DEV_P2SB, &msg, &data, &response);
586 if (status || response)
587 printk(BIOS_ERR, "Failed to lock GPIO PAD, response = %d\n", response);
588 }
589
590 if ((pad_info->lock_action & GPIO_LOCK_TX) == GPIO_LOCK_TX) {
591 if (CONFIG(DEBUG_GPIO))
592 printk(BIOS_INFO, "%s: Locking pad %d Tx state\n",
593 __func__, pad_info->pad);
594 offset += sizeof(uint32_t);
595 data = pcr_read32(pid, offset) | bit_mask;
596 msg.offset = offset;
597 status = pcr_execute_sideband_msg(PCH_DEV_P2SB, &msg, &data, &response);
598 if (status || response)
599 printk(BIOS_ERR, "Failed to lock GPIO PAD Tx state, response = %d\n",
600 response);
601 }
602 }
603
604 int gpio_lock_pads(const struct gpio_lock_config *pad_list, const size_t count)
605 {
606 const struct pad_community *comm;
607 uint16_t offset;
608 size_t rel_pad;
609 gpio_t pad;
610
611 if (!CONFIG(SOC_INTEL_COMMON_BLOCK_SMM_LOCK_GPIO_PADS))
612 return -1;
613
614 /*
615 * FSP-S will unlock all the GPIO pads and hide the P2SB device. With
616 * the device hidden, we will not be able to send the sideband interface
617 * message to lock the GPIO configuration. Therefore, we need to unhide
618 * the P2SB device which can only be done in SMM requiring that this
619 * function is called from SMM.
620 */
621 if (!ENV_SMM) {
622 printk(BIOS_ERR, "%s: Error: must be called from SMM!\n", __func__);
623 return -1;
624 }
625
626 if ((pad_list == NULL) || (count == 0)) {
627 printk(BIOS_ERR, "%s: Error: pad_list null or count = 0!\n", __func__);
628 return -1;
629 }
630
631 p2sb_unhide();
632
633 for (int x = 0; x < count; x++) {
634 pad = pad_list[x].pad;
635 comm = gpio_get_community(pad);
636 rel_pad = relative_pad_in_comm(comm, pad);
637 offset = comm->pad_cfg_lock_offset;
638 if (!offset) {
639 printk(BIOS_ERR, "%s: Error: offset not defined for pad %d!\n",
640 __func__, pad);
641 continue;
642 }
643 /* PADCFGLOCK and PADCFGLOCKTX registers for each community are contiguous */
644 offset += gpio_group_index_scaled(comm, rel_pad, 2 * sizeof(uint32_t));
645
646 const uint32_t bit_mask = gpio_bitmask_within_group(comm, rel_pad);
647
648 gpio_pad_config_lock_using_sbi(&pad_list[x], comm->port, offset, bit_mask);
649 }
650
651 p2sb_hide();
652 }
653
654 static void
655 gpio_pad_config_lock_using_pcr(const struct gpio_lock_config *pad_info,
656 uint8_t pid, uint16_t offset, const uint32_t bit_mask)
657 {
658 if ((pad_info->lock_action & GPIO_LOCK_CONFIG) == GPIO_LOCK_CONFIG) {
659 if (CONFIG(DEBUG_GPIO))
660 printk(BIOS_INFO, "%s: Locking pad %d configuration\n",
661 __func__, pad_info->pad);
662 pcr_or32(pid, offset, bit_mask);
663 }
664
665 if ((pad_info->lock_action & GPIO_LOCK_TX) == GPIO_LOCK_TX) {
666 if (CONFIG(DEBUG_GPIO))
667 printk(BIOS_INFO, "%s: Locking pad %d TX state\n",
668 __func__, pad_info->pad);
669 pcr_or32(pid, offset + sizeof(uint32_t), bit_mask);
670 }
671 }
672
673 static int gpio_non_smm_lock_pad(const struct gpio_lock_config *pad_info)
674 {
675 const struct pad_community *comm = gpio_get_community(pad_info->pad);
676 uint16_t offset;
677 size_t rel_pad;
678
679 if (!pad_info) {
680 printk(BIOS_ERR, "%s: Error: pad_info is null!\n", __func__);
681 return -1;
682 }
683
684 if (cpu_soc_is_in_untrusted_mode()) {
685 printk(BIOS_ERR, "%s: Error: IA Untrusted Mode enabled, can't lock pad!\n",
686 __func__);
687 return -1;
688 }
689
690 rel_pad = relative_pad_in_comm(comm, pad_info->pad);
691 offset = comm->pad_cfg_lock_offset;
692 if (!offset) {
693 printk(BIOS_ERR, "%s: Error: offset not defined for pad %d!\n",
694 __func__, pad_info->pad);
695 return -1;
696 }
697
698 /* PADCFGLOCK and PADCFGLOCKTX registers for each community are contiguous */
699 offset += gpio_group_index_scaled(comm, rel_pad, 2 * sizeof(uint32_t));
700 const uint32_t bit_mask = gpio_bitmask_within_group(comm, rel_pad);
701
702 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_LOCK_USING_PCR)) {
703 if (CONFIG(DEBUG_GPIO))
704 printk(BIOS_INFO, "Locking pad configuration using PCR\n");
705 gpio_pad_config_lock_using_pcr(pad_info, comm->port, offset, bit_mask);
706 } else if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_LOCK_USING_SBI)) {
707 if (CONFIG(DEBUG_GPIO))
708 printk(BIOS_INFO, "Locking pad configuration using SBI\n");
709 gpio_pad_config_lock_using_sbi(pad_info, comm->port, offset, bit_mask);
710 } else {
711 printk(BIOS_ERR, "%s: Error: No pad configuration lock method is selected!\n",
712 __func__);
713 }
714
715 return 0;
716 }
717
718 int gpio_lock_pad(const gpio_t pad, enum gpio_lock_action lock_action)
719 {
720 /* Skip locking GPIO PAD in early stages or in recovery mode */
721 if (ENV_ROMSTAGE_OR_BEFORE || vboot_recovery_mode_enabled())
722 return -1;
723
724 const struct gpio_lock_config pads = {
725 .pad = pad,
726 .lock_action = lock_action
727 };
728
729 if (!ENV_SMM && !CONFIG(SOC_INTEL_COMMON_BLOCK_SMM_LOCK_GPIO_PADS))
730 return gpio_non_smm_lock_pad(&pads);
731
732 return gpio_lock_pads(&pads, 1);
733 }
734
735 void gpio_set(gpio_t gpio_num, int value)
736 {
737 const struct pad_community *comm = gpio_get_community(gpio_num);
738 uint16_t config_offset;
739
740 config_offset = pad_config_offset(comm, gpio_num);
741 pcr_rmw32(comm->port, config_offset,
742 ~PAD_CFG0_TX_STATE, (!!value & PAD_CFG0_TX_STATE));
743 }
744
745 uint16_t gpio_acpi_pin(gpio_t gpio_num)
746 {
747 const struct pad_community *comm;
748 size_t group, pin;
749
750 if (CONFIG(SOC_INTEL_COMMON_BLOCK_GPIO_MULTI_ACPI_DEVICES))
751 return relative_pad_in_comm(gpio_get_community(gpio_num),
752 gpio_num);
753
754 comm = gpio_get_community(gpio_num);
755 pin = relative_pad_in_comm(comm, gpio_num);
756 group = gpio_group_index(comm, pin);
757
758 /* If pad base is not set then use GPIO number as ACPI pin number. */
759 if (comm->groups[group].acpi_pad_base == PAD_BASE_NONE)
760 return gpio_num;
761
762 /*
763 * If this group has a non-zero pad base then compute the ACPI pin
764 * number from the pad base and the relative pad in the group.
765 */
766 return comm->groups[group].acpi_pad_base + gpio_within_group(comm, pin);
767 }
768
769 static void print_gpi_status(const struct gpi_status *sts)
770 {
771 int i;
772 int group;
773 int index;
774 int bit_set;
775 int num_groups;
776 int abs_bit;
777 size_t gpio_communities;
778 const struct pad_community *comm;
779
780 comm = soc_gpio_get_community(&gpio_communities);
781 for (i = 0; i < gpio_communities; i++) {
782 num_groups = comm->num_gpi_regs;
783 index = comm->gpi_status_offset;
784 for (group = 0; group < num_groups; group++, index++) {
785 for (bit_set = comm->max_pads_per_group - 1;
786 bit_set >= 0; bit_set--) {
787 if (!(sts->grp[index] & (1 << bit_set)))
788 continue;
789
790 abs_bit = bit_set;
791 abs_bit += group * comm->max_pads_per_group;
792 printk(BIOS_DEBUG, "%s %d\n", comm->name,
793 abs_bit);
794 }
795 }
796 comm++;
797 }
798 }
799
800 void gpi_clear_get_smi_status(struct gpi_status *sts)
801 {
802 int i;
803 int group;
804 int index;
805 uint32_t sts_value;
806 uint32_t en_value;
807 size_t gpio_communities;
808 int num_groups;
809 const struct pad_community *comm;
810
811 comm = soc_gpio_get_community(&gpio_communities);
812 for (i = 0; i < gpio_communities; i++) {
813 num_groups = comm->num_gpi_regs;
814 index = comm->gpi_status_offset;
815 for (group = 0; group < num_groups; group++, index++) {
816 sts_value = pcr_read32(comm->port,
817 GPI_SMI_STS_OFFSET(comm, group));
818 en_value = pcr_read32(comm->port,
819 GPI_SMI_EN_OFFSET(comm, group));
820 sts->grp[index] = sts_value & en_value;
821 /* Clear the set status bits. */
822 pcr_write32(comm->port, GPI_SMI_STS_OFFSET(comm,
823 group), sts->grp[index]);
824 }
825 comm++;
826 }
827
828 if (CONFIG(DEBUG_SMI))
829 print_gpi_status(sts);
830 }
831
832 int gpi_status_get(const struct gpi_status *sts, gpio_t pad)
833 {
834 uint8_t sts_index;
835 const struct pad_community *comm = gpio_get_community(pad);
836
837 pad = relative_pad_in_comm(comm, pad);
838 sts_index = comm->gpi_status_offset;
839 sts_index += gpio_group_index(comm, pad);
840
841 return !!(sts->grp[sts_index] & gpio_bitmask_within_group(comm, pad));
842 }
843
844 static int gpio_route_pmc_gpio_gpe(int pmc_gpe_num)
845 {
846 size_t num_routes;
847 const struct pmc_to_gpio_route *routes;
848 int i;
849
850 routes = soc_pmc_gpio_routes(&num_routes);
851 assert(routes != NULL);
852 for (i = 0; i < num_routes; i++, routes++) {
853 if (pmc_gpe_num == routes->pmc)
854 return routes->gpio;
855 }
856 return -1;
857 }
858
859 void gpio_route_gpe(uint8_t gpe0b, uint8_t gpe0c, uint8_t gpe0d)
860 {
861 int i;
862 uint32_t misccfg_mask;
863 uint32_t misccfg_value;
864 int ret;
865 size_t gpio_communities;
866 const struct pad_community *comm;
867
868 /* Get the group here for community specific MISCCFG register.
869 * If any of these returns -1 then there is some error in devicetree
870 * where the group is probably hardcoded and does not comply with the
871 * PMC group defines. So we return from here and MISCFG is set to
872 * default.
873 */
874 ret = gpio_route_pmc_gpio_gpe(gpe0b);
875 if (ret == -1)
876 return;
877 gpe0b = ret;
878
879 ret = gpio_route_pmc_gpio_gpe(gpe0c);
880 if (ret == -1)
881 return;
882 gpe0c = ret;
883
884 ret = gpio_route_pmc_gpio_gpe(gpe0d);
885 if (ret == -1)
886 return;
887 gpe0d = ret;
888
889 misccfg_value = gpe0b << MISCCFG_GPE0_DW0_SHIFT;
890 misccfg_value |= gpe0c << MISCCFG_GPE0_DW1_SHIFT;
891 misccfg_value |= gpe0d << MISCCFG_GPE0_DW2_SHIFT;
892
893 /* Program GPIO_MISCCFG */
894 misccfg_mask = ~(MISCCFG_GPE0_DW2_MASK |
895 MISCCFG_GPE0_DW1_MASK |
896 MISCCFG_GPE0_DW0_MASK);
897
898 if (CONFIG(DEBUG_GPIO))
899 printk(BIOS_DEBUG, "misccfg_mask:%x misccfg_value:%x\n",
900 misccfg_mask, misccfg_value);
901 comm = soc_gpio_get_community(&gpio_communities);
902 for (i = 0; i < gpio_communities; i++, comm++)
903 pcr_rmw32(comm->port, GPIO_MISCCFG,
904 misccfg_mask, misccfg_value);
905 }
906
907 const char *gpio_acpi_path(gpio_t gpio_num)
908 {
909 const struct pad_community *comm = gpio_get_community(gpio_num);
910 return comm->acpi_path;
911 }
912
913 uint32_t __weak soc_gpio_pad_config_fixup(const struct pad_config *cfg,
914 int dw_reg, uint32_t reg_val)
915 {
916 return reg_val;
917 }
918
919 void gpi_clear_int_cfg(void)
920 {
921 int i, group, num_groups;
922 uint32_t sts_value;
923 size_t gpio_communities;
924 const struct pad_community *comm;
925
926 comm = soc_gpio_get_community(&gpio_communities);
927 for (i = 0; i < gpio_communities; i++, comm++) {
928 num_groups = comm->num_gpi_regs;
929 for (group = 0; group < num_groups; group++) {
930 /* Clear the enable register */
931 pcr_write32(comm->port, GPI_IE_OFFSET(comm, group), 0);
932
933 /* Read and clear the set status register bits*/
934 sts_value = pcr_read32(comm->port,
935 GPI_IS_OFFSET(comm, group));
936 pcr_write32(comm->port,
937 GPI_IS_OFFSET(comm, group), sts_value);
938 }
939 }
940 }
941
942 /* The function performs GPIO Power Management programming. */
943 void gpio_pm_configure(const uint8_t *misccfg_pm_values, size_t num)
944 {
945 int i;
946 size_t gpio_communities;
947 const uint8_t misccfg_pm_mask = (uint8_t)~MISCCFG_GPIO_PM_CONFIG_BITS;
948 const struct pad_community *comm;
949
950 comm = soc_gpio_get_community(&gpio_communities);
951 if (gpio_communities != num)
952 die("Incorrect GPIO community count!\n");
953
954 /* Program GPIO_MISCCFG */
955 for (i = 0; i < num; i++, comm++)
956 pcr_rmw8(comm->port, GPIO_MISCCFG,
957 misccfg_pm_mask, misccfg_pm_values[i]);
958 }
959
960 size_t gpio_get_index_in_group(gpio_t pad)
961 {
962 const struct pad_community *comm;
963 size_t pin;
964
965 comm = gpio_get_community(pad);
966 pin = relative_pad_in_comm(comm, pad);
967 return gpio_within_group(comm, pin);
968 }
969
970 static uint32_t *snapshot;
971
972 static void *allocate_snapshot_space(void)
973 {
974 size_t gpio_communities, total = 0, i;
975 const struct pad_community *comm;
976
977 comm = soc_gpio_get_community(&gpio_communities);
978 for (i = 0; i < gpio_communities; i++, comm++)
979 total += comm->last_pad - comm->first_pad + 1;
980
981 if (total == 0)
982 return NULL;
983
984 return malloc(total * GPIO_NUM_PAD_CFG_REGS * sizeof(uint32_t));
985 }
986
987 void gpio_snapshot(void)
988 {
989 size_t gpio_communities, index, i, pad, reg;
990 const struct pad_community *comm;
991 uint16_t config_offset;
992
993 if (snapshot == NULL) {
994 snapshot = allocate_snapshot_space();
995 if (snapshot == NULL)
996 return;
997 }
998
999 comm = soc_gpio_get_community(&gpio_communities);
1000 for (i = 0, index = 0; i < gpio_communities; i++, comm++) {
1001 for (pad = comm->first_pad; pad <= comm->last_pad; pad++) {
1002 config_offset = pad_config_offset(comm, pad);
1003 for (reg = 0; reg < GPIO_NUM_PAD_CFG_REGS; reg++) {
1004 snapshot[index] = pcr_read32(comm->port,
1005 PAD_CFG_OFFSET(config_offset, reg));
1006 index++;
1007 }
1008 }
1009 }
1010 }
1011
1012 size_t gpio_verify_snapshot(void)
1013 {
1014 size_t gpio_communities, index, i, pad, reg;
1015 const struct pad_community *comm;
1016 uint32_t curr_val;
1017 uint16_t config_offset;
1018 size_t changes = 0;
1019
1020 if (snapshot == NULL)
1021 return 0;
1022
1023 comm = soc_gpio_get_community(&gpio_communities);
1024 for (i = 0, index = 0; i < gpio_communities; i++, comm++) {
1025 for (pad = comm->first_pad; pad <= comm->last_pad; pad++) {
1026 config_offset = pad_config_offset(comm, pad);
1027 for (reg = 0; reg < GPIO_NUM_PAD_CFG_REGS; reg++) {
1028 curr_val = pcr_read32(comm->port,
1029 PAD_CFG_OFFSET(config_offset, reg));
1030 if (curr_val != snapshot[index]) {
1031 printk(BIOS_SPEW,
1032 "%zd(DW%zd): Changed from 0x%x to 0x%x\n",
1033 pad, reg, snapshot[index], curr_val);
1034 changes++;
1035 }
1036 index++;
1037 }
1038 }
1039 }
1040
1041 return changes;
1042 }
1043
1044 static void snapshot_cleanup(void *unused)
1045 {
1046 free(snapshot);
1047 }
1048
1049 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, snapshot_cleanup, NULL);
1050 BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, snapshot_cleanup, NULL);
1051
1052 bool gpio_get_vw_info(gpio_t pad, unsigned int *vw_index, unsigned int *vw_bit)
1053 {
1054 const struct pad_community *comm;
1055 unsigned int offset = 0;
1056 size_t i;
1057
1058 comm = gpio_get_community(pad);
1059 for (i = 0; i < comm->num_vw_entries; i++) {
1060 if (pad >= comm->vw_entries[i].first_pad && pad <= comm->vw_entries[i].last_pad)
1061 break;
1062
1063 offset += 1 + comm->vw_entries[i].last_pad - comm->vw_entries[i].first_pad;
1064 }
1065
1066 if (i == comm->num_vw_entries)
1067 return false;
1068
1069 /* Adjust offset and calculate vw_index based on the mapping type */
1070 if (comm->vw_map) {
1071 offset = pad - comm->vw_entries[i].first_pad;
1072 offset += comm->vw_map[i].start_pos;
1073 *vw_index = comm->vw_map[i].base + offset / 8;
1074 } else {
1075 offset += pad - comm->vw_entries[i].first_pad;
1076 offset += comm->vw_base;
1077 *vw_index = offset / 8;
1078 }
1079 *vw_bit = offset % 8;
1080
1081 return true;
1082 }
1083
1084 unsigned int gpio_get_pad_cpu_portid(gpio_t pad)
1085 {
1086 const struct pad_community *comm = gpio_get_community(pad);
1087 return comm->cpu_port;
1088 }