1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpi_pm.h>
4 #include <device/mmio.h>
5 #include <device/device.h>
6 #include <console/console.h>
9 #include <amdblocks/acpi.h>
10 #include <amdblocks/acpimmio.h>
11 #include <amdblocks/gpio.h>
12 #include <amdblocks/smi.h>
19 * acpimmio_gpio0, acpimmio_remote_gpio and acpimmio_iomux are defined in
20 * soc/amd/common/block/acpimmio/mmio_util.c and declared as extern variables/constants in
21 * amdblocks/acpimmio.h which is included in this file.
24 /* MMIO access of new-style GPIO bank configuration registers */
25 static inline void *gpio_ctrl_ptr(gpio_t gpio_num
)
27 if (SOC_GPIO_TOTAL_PINS
< AMD_GPIO_FIRST_REMOTE_GPIO_NUMBER
||
28 /* Verstage on PSP would need to map acpimmio_remote_gpio */
29 (CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK
) && ENV_SEPARATE_VERSTAGE
) ||
30 gpio_num
< AMD_GPIO_FIRST_REMOTE_GPIO_NUMBER
)
31 return acpimmio_gpio0
+ gpio_num
* sizeof(uint32_t);
33 return acpimmio_remote_gpio
+
34 (gpio_num
- AMD_GPIO_FIRST_REMOTE_GPIO_NUMBER
) * sizeof(uint32_t);
37 static inline uint32_t gpio_read32(gpio_t gpio_num
)
39 return read32(gpio_ctrl_ptr(gpio_num
));
42 static inline void gpio_write32(gpio_t gpio_num
, uint32_t value
)
44 write32(gpio_ctrl_ptr(gpio_num
), value
);
47 static inline void *gpio_mux_ptr(gpio_t gpio_num
)
49 if (SOC_GPIO_TOTAL_PINS
< AMD_GPIO_FIRST_REMOTE_GPIO_NUMBER
||
50 /* Verstage on PSP would need to map acpimmio_remote_gpio */
51 (CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK
) && ENV_SEPARATE_VERSTAGE
) ||
52 gpio_num
< AMD_GPIO_FIRST_REMOTE_GPIO_NUMBER
)
53 return acpimmio_iomux
+ gpio_num
;
55 return acpimmio_remote_gpio
+ AMD_GPIO_REMOTE_GPIO_MUX_OFFSET
+
56 (gpio_num
- AMD_GPIO_FIRST_REMOTE_GPIO_NUMBER
);
59 static uint8_t get_gpio_mux(gpio_t gpio_num
)
61 return read8(gpio_mux_ptr(gpio_num
));
64 static void set_gpio_mux(gpio_t gpio_num
, uint8_t function
)
66 write8(gpio_mux_ptr(gpio_num
), function
& AMD_GPIO_MUX_MASK
);
67 get_gpio_mux(gpio_num
); /* Flush posted write */
70 static int get_gpio_gevent(gpio_t gpio
, const struct soc_amd_event
*table
,
75 for (i
= 0; i
< items
; i
++) {
76 if ((table
+ i
)->gpio
== gpio
)
77 return (int)(table
+ i
)->event
;
82 static void program_smi(uint32_t flags
, unsigned int gevent_num
)
86 if (!is_gpio_event_level_triggered(flags
)) {
87 printk(BIOS_ERR
, "%s - Only level trigger allowed for SMI!\n", __func__
);
92 if (is_gpio_event_active_high(flags
))
93 level
= SMI_SCI_LVL_HIGH
;
95 level
= SMI_SCI_LVL_LOW
;
97 configure_gevent_smi(gevent_num
, SMI_MODE_SMI
, level
);
101 * For each general purpose event, GPE, the choice of edge/level triggered
102 * event is represented as a single bit in SMI_SCI_LEVEL register.
104 * In a similar fashion, polarity (rising/falling, hi/lo) of each GPE is
105 * represented as a single bit in SMI_SCI_TRIG register.
107 static void program_sci(uint32_t flags
, unsigned int gevent_num
)
109 struct sci_source sci
;
111 sci
.scimap
= gevent_num
;
112 sci
.gpe
= gevent_num
;
114 if (is_gpio_event_level_triggered(flags
))
115 sci
.level
= SMI_SCI_LVL
;
117 sci
.level
= SMI_SCI_EDG
;
119 if (is_gpio_event_active_high(flags
))
120 sci
.direction
= SMI_SCI_LVL_HIGH
;
122 sci
.direction
= SMI_SCI_LVL_LOW
;
124 configure_scimap(&sci
);
127 static void gpio_update32(gpio_t gpio_num
, uint32_t mask
, uint32_t or)
131 reg
= gpio_read32(gpio_num
);
134 gpio_write32(gpio_num
, reg
);
137 /* Set specified bits of a register to match those of ctrl. */
138 static void gpio_setbits32(gpio_t gpio_num
, uint32_t mask
, uint32_t ctrl
)
140 gpio_update32(gpio_num
, ~mask
, ctrl
& mask
);
143 static void gpio_and32(gpio_t gpio_num
, uint32_t mask
)
145 gpio_update32(gpio_num
, mask
, 0);
148 static void gpio_or32(gpio_t gpio_num
, uint32_t or)
150 gpio_update32(gpio_num
, 0xffffffff, or);
153 static void master_switch_clr(uint32_t mask
)
155 const gpio_t master_reg
= GPIO_MASTER_SWITCH
/ sizeof(uint32_t);
156 gpio_and32(master_reg
, ~mask
);
159 static void master_switch_set(uint32_t or)
161 const gpio_t master_reg
= GPIO_MASTER_SWITCH
/ sizeof(uint32_t);
162 gpio_or32(master_reg
, or);
165 int gpio_get(gpio_t gpio_num
)
169 reg
= gpio_read32(gpio_num
);
170 return !!(reg
& GPIO_PIN_STS
);
173 void gpio_set(gpio_t gpio_num
, int value
)
175 gpio_setbits32(gpio_num
, GPIO_OUTPUT_VALUE
, value
? GPIO_OUTPUT_VALUE
: 0);
178 void gpio_input_pulldown(gpio_t gpio_num
)
180 gpio_setbits32(gpio_num
, GPIO_PULL_MASK
| GPIO_OUTPUT_ENABLE
, GPIO_PULLDOWN_ENABLE
);
183 void gpio_input_pullup(gpio_t gpio_num
)
185 gpio_setbits32(gpio_num
, GPIO_PULL_MASK
| GPIO_OUTPUT_ENABLE
, GPIO_PULLUP_ENABLE
);
188 void gpio_input(gpio_t gpio_num
)
190 gpio_and32(gpio_num
, ~(GPIO_PULL_MASK
| GPIO_OUTPUT_ENABLE
));
193 void gpio_output(gpio_t gpio_num
, int value
)
195 /* set GPIO output value before setting the direction to output to avoid glitches */
196 gpio_set(gpio_num
, value
);
197 gpio_or32(gpio_num
, GPIO_OUTPUT_ENABLE
);
200 const char *gpio_acpi_path(gpio_t gpio
)
205 uint16_t gpio_acpi_pin(gpio_t gpio
)
210 void gpio_save_pin_registers(gpio_t gpio
, struct soc_amd_gpio_register_save
*save
)
212 save
->mux_value
= get_gpio_mux(gpio
);
213 save
->control_value
= gpio_read32(gpio
);
216 void gpio_restore_pin_registers(gpio_t gpio
, struct soc_amd_gpio_register_save
*save
)
218 set_gpio_mux(gpio
, save
->mux_value
);
219 gpio_write32(gpio
, save
->control_value
);
220 gpio_read32(gpio
); /* Flush posted write */
223 static void set_single_gpio(const struct soc_amd_gpio
*g
)
225 static const struct soc_amd_event
*gev_tbl
;
226 static size_t gev_items
;
228 const bool can_set_smi_flags
= !((CONFIG(VBOOT_STARTS_BEFORE_BOOTBLOCK
) &&
229 ENV_SEPARATE_VERSTAGE
) ||
230 CONFIG(SOC_AMD_COMMON_BLOCK_BANKED_GPIOS_NON_SOC_CODEBASE
));
232 set_gpio_mux(g
->gpio
, g
->function
);
234 gpio_setbits32(g
->gpio
, PAD_CFG_MASK
, g
->control
);
235 /* Clear interrupt and wake status (write 1-to-clear bits) */
236 gpio_or32(g
->gpio
, GPIO_INT_STATUS
| GPIO_WAKE_STATUS
);
240 /* Can't set SMI flags from PSP */
241 if (!can_set_smi_flags
)
245 soc_get_gpio_event_table(&gev_tbl
, &gev_items
);
247 gevent_num
= get_gpio_gevent(g
->gpio
, gev_tbl
, gev_items
);
248 if (gevent_num
< 0) {
249 printk(BIOS_WARNING
, "GPIO pin %d has no associated gevent!\n",
254 if (g
->flags
& GPIO_FLAG_SMI
) {
255 program_smi(g
->flags
, gevent_num
);
256 } else if (g
->flags
& GPIO_FLAG_SCI
) {
257 program_sci(g
->flags
, gevent_num
);
261 void gpio_configure_pads_with_override(const struct soc_amd_gpio
*base_cfg
,
262 size_t base_num_pads
,
263 const struct soc_amd_gpio
*override_cfg
,
264 size_t override_num_pads
)
266 const struct soc_amd_gpio
*c
;
269 if (!base_cfg
|| !base_num_pads
)
273 * Disable blocking wake/interrupt status generation while updating
274 * debounce registers. Otherwise when a debounce register is updated
275 * the whole GPIO controller will zero out all interrupt enable status
276 * bits while the delay happens. This could cause us to drop the bits
277 * due to the read-modify-write that happens on each register.
279 * Additionally disable interrupt generation so we don't get any
280 * spurious interrupts while updating the registers.
282 master_switch_clr(GPIO_MASK_STS_EN
| GPIO_INTERRUPT_EN
);
284 for (i
= 0; i
< base_num_pads
; i
++) {
286 /* Check if override exist for GPIO from the base configuration */
287 for (j
= 0; override_cfg
&& j
< override_num_pads
; j
++) {
288 if (c
->gpio
== override_cfg
[j
].gpio
) {
289 c
= &override_cfg
[j
];
297 * Re-enable interrupt status generation.
299 * We leave MASK_STATUS disabled because the kernel may reconfigure the
300 * debounce registers while the drivers load. This will cause interrupts
301 * to be missed during boot.
303 master_switch_set(GPIO_INTERRUPT_EN
);
306 void gpio_configure_pads(const struct soc_amd_gpio
*gpio_list_ptr
, size_t size
)
308 gpio_configure_pads_with_override(gpio_list_ptr
, size
, NULL
, 0);
311 int gpio_interrupt_status(gpio_t gpio
)
313 uint32_t reg
= gpio_read32(gpio
);
315 if (reg
& GPIO_INT_STATUS
) {
316 /* Clear interrupt status, preserve wake status */
317 reg
&= ~GPIO_WAKE_STATUS
;
318 gpio_write32(gpio
, reg
);
325 static void check_and_add_wake_gpio(gpio_t begin
, gpio_t end
, struct gpio_wake_state
*state
)
330 for (i
= begin
; i
< end
; i
++) {
331 reg
= gpio_read32(i
);
332 if (!(reg
& GPIO_WAKE_STATUS
))
334 printk(BIOS_INFO
, "GPIO %d woke system.\n", i
);
335 if (state
->num_valid_wake_gpios
>= ARRAY_SIZE(state
->wake_gpios
))
337 state
->wake_gpios
[state
->num_valid_wake_gpios
++] = i
;
341 static void check_gpios(uint32_t wake_stat
, unsigned int bit_limit
, gpio_t gpio_base
,
342 struct gpio_wake_state
*state
)
348 for (i
= 0; i
< bit_limit
; i
++) {
349 if (!(wake_stat
& BIT(i
)))
351 /* Each wake status register bit is for 4 GPIOs that then will be checked */
352 begin
= gpio_base
+ i
* 4;
354 /* There is no gpio 63. */
357 check_and_add_wake_gpio(begin
, end
, state
);
361 void gpio_fill_wake_state(struct gpio_wake_state
*state
)
363 /* Turn the wake registers into "gpio" index to conform to existing API. */
364 const gpio_t stat0
= GPIO_WAKE_STAT_0
/ sizeof(uint32_t);
365 const gpio_t stat1
= GPIO_WAKE_STAT_1
/ sizeof(uint32_t);
366 const gpio_t control_switch
= GPIO_MASTER_SWITCH
/ sizeof(uint32_t);
368 memset(state
, 0, sizeof(*state
));
370 state
->control_switch
= gpio_read32(control_switch
);
371 state
->wake_stat
[0] = gpio_read32(stat0
);
372 state
->wake_stat
[1] = gpio_read32(stat1
);
374 printk(BIOS_INFO
, "GPIO Control Switch: 0x%08x, Wake Stat 0: 0x%08x, Wake Stat 1: 0x%08x\n",
375 state
->control_switch
, state
->wake_stat
[0], state
->wake_stat
[1]);
377 check_gpios(state
->wake_stat
[0], 32, 0, state
);
378 check_gpios(state
->wake_stat
[1], 14, 128, state
);
381 void gpio_add_events(void)
383 const struct chipset_power_state
*ps
;
384 const struct gpio_wake_state
*state
;
388 if (acpi_fetch_pm_state(&ps
, PS_CLAIMER_ELOG
) < 0)
390 state
= &ps
->gpio_state
;
392 end
= MIN(state
->num_valid_wake_gpios
, ARRAY_SIZE(state
->wake_gpios
));
393 for (i
= 0; i
< end
; i
++)
394 elog_add_event_wake(ELOG_WAKE_SOURCE_GPIO
, state
->wake_gpios
[i
]);