From b09b26fe0969faee0e65c2aed3c295d8a9b23784 Mon Sep 17 00:00:00 2001 From: Eric Chen Date: Wed, 30 Aug 2023 17:51:22 +0800 Subject: [PATCH] fby4: sd: Support PLDM host power control Summary: Description Implement pldm host power control handler Motivation In Yosemite4, BMC will communicate with BIC by PLDM command. This change PLDM Support host power control by setting cpld through i2c Test Plan: - host power on: pass - host power off: pass - host power cycle: pass - host power reset: pass - host graceful shutdown: pass Test Log: - host power on pldmtool raw -d 0x80 0x02 0x39 0x0 0x0 0x1 0x0 0x1 pldmtool: Tx: 81 02 39 00 00 01 00 01 pldmtool: Rx: 01 02 39 00 - host power off pldmtool raw -d 0x80 0x02 0x39 0x0 0x0 0x1 0x0 0x2 pldmtool: Tx: 81 02 39 00 00 01 00 02 pldmtool: Rx: 01 02 39 00 - host power cycle pldmtool raw -d 0x80 0x02 0x39 0x0 0x0 0x1 0x0 0x3 pldmtool: Tx: 81 02 39 00 00 01 00 03 pldmtool: Rx: 01 02 39 00 - host power reset pldmtool raw -d 0x80 0x02 0x39 0x0 0x0 0x1 0x0 0x4 pldmtool: Tx: 81 02 39 00 00 01 00 04 pldmtool: Rx: 01 02 39 00 - host power reset pldmtool raw -d 0x80 0x02 0x39 0x0 0x0 0x1 0x0 0x4 pldmtool: Tx: 81 02 39 00 00 01 00 04 pldmtool: Rx: 01 02 39 00 - host graceful shutdown pldmtool raw -d 0x80 0x02 0x39 0x0 0x0 0x1 0x0 0x5 pldmtool: Tx: 81 02 39 00 00 01 00 05 pldmtool: Rx: 01 02 39 00 --- .../yv4-sd/src/platform/plat_pldm_monitor.c | 110 ++++++++++++++++++ .../yv4-sd/src/platform/plat_pldm_monitor.h | 6 +- 2 files changed, 115 insertions(+), 1 deletion(-) diff --git a/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.c b/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.c index 0abaab8b2e..48449c98ed 100644 --- a/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.c +++ b/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.c @@ -22,15 +22,31 @@ #include "pldm.h" #include "plat_gpio.h" +#include "plat_i2c.h" #include "plat_sensor_table.h" #include "plat_pldm_monitor.h" LOG_MODULE_REGISTER(plat_pldm_monitor); +#define POWER_ON_BUTTON_MSEC 1000 +#define RESET_BUTTON_MSEC 1000 +#define GRACEFUL_SHUTDOWN_BUTTON_MSEC 1000 +#define POWER_OFF_BUTTON_MSEC 6000 +#define POWER_CYCLE_INTERVAL_MSEC 5000 + +static uint8_t power_sequence[3] = {PLDM_PLATFORM_HOST_PWR_CTRL_DEFAULT, PLDM_PLATFORM_HOST_PWR_BTN_LOW, + PLDM_PLATFORM_HOST_PWR_CTRL_DEFAULT}; +static uint8_t reset_sequence[3] = {PLDM_PLATFORM_HOST_PWR_CTRL_DEFAULT, PLDM_PLATFORM_HOST_RST_BTN_LOW, + PLDM_PLATFORM_HOST_PWR_CTRL_DEFAULT}; + struct pldm_state_effecter_info plat_state_effecter_table[] = { [0 ... PLDM_PLATFORM_OEM_AST1030_GPIO_PIN_NUM_MAX] = { .entity_type = PLDM_ENTITY_IO_CONTROLLER, }, + [PLDM_PLATFORM_OEM_AST1030_GPIO_PIN_NUM_MAX + 1] = { + .entity_type = PLDM_ENTITY_SUB_CHASSIS, + .effecter_id = 0x0000, + }, }; void plat_pldm_load_state_effecter_table(void) @@ -56,6 +72,9 @@ uint8_t plat_pldm_set_state_effecter_state_handler(const uint8_t *buf, uint16_t set_effecter_state_gpio_handler(buf, len, resp, resp_len, (uint8_t)(info_p->effecter_id & GENMASK(7, 0))); break; + case PLDM_ENTITY_SUB_CHASSIS: + plat_pldm_set_effecter_state_host_power_control(buf, len, resp, resp_len); + break; default: LOG_ERR("Unsupport entity type, (%d)", info_p->entity_type); *completion_code_p = PLDM_ERROR_INVALID_DATA; @@ -90,3 +109,94 @@ uint8_t plat_pldm_get_state_effecter_state_handler(const uint8_t *buf, uint16_t return PLDM_SUCCESS; } + +uint8_t plat_pldm_host_button_sequence(const uint8_t *power_sequence, uint16_t pressing_interval) +{ + uint8_t sts_cnt = 3; + uint8_t retry = 3; + I2C_MSG i2c_msg; + i2c_msg.bus = I2C_BUS1; + i2c_msg.target_addr = 0x21; //server board cpld addr + i2c_msg.tx_len = 2; + i2c_msg.rx_len = 0; + i2c_msg.data[0] = 0x00; + + for (int i = 0; i < sts_cnt; i++) { + i2c_msg.data[1] = power_sequence[i]; + if (i2c_master_write(&i2c_msg, retry)) { + return -1; + } + + if (i == 1) { + k_msleep(pressing_interval); + } + } + return 0; +} + +void plat_pldm_set_effecter_state_host_power_control(const uint8_t *buf, uint16_t len, + uint8_t *resp, uint16_t *resp_len) +{ + CHECK_NULL_ARG(buf); + CHECK_NULL_ARG(resp); + CHECK_NULL_ARG(resp_len); + + struct pldm_set_state_effecter_states_req *req_p = + (struct pldm_set_state_effecter_states_req *)buf; + uint8_t *completion_code_p = resp; + *resp_len = 1; + + if (req_p->composite_effecter_count != PLDM_PLATFORM_OEM_HOST_POWER_CTRL_EFFECTER_STATE_FIELD_COUNT) { + LOG_ERR("Unsupport host power control effecter count, (%d)", req_p->composite_effecter_count); + *completion_code_p = PLDM_ERROR_INVALID_DATA; + return; + } + + set_effecter_state_field_t *host_power_state = &req_p->field[0]; + + if (host_power_state->set_request >= PLDM_SET_REQUEST_MAX) { + LOG_ERR("Invalid host power control set request (%d)", + host_power_state->set_request); + *completion_code_p = PLDM_PLATFORM_UNSUPPORTED_EFFECTERSTATE; + return; + } + + switch (host_power_state->effecter_state) { + case EFFECTER_STATE_POWER_STATUS_ON: + if (plat_pldm_host_button_sequence(power_sequence, POWER_ON_BUTTON_MSEC) != 0) { + LOG_ERR("Failed to do host power on"); + } + break; + case EFFECTER_STATE_POWER_STATUS_OFF: + if (plat_pldm_host_button_sequence(power_sequence, POWER_OFF_BUTTON_MSEC) != 0) { + LOG_ERR("Failed to do host power off"); + } + break; + case EFFECTER_STATE_POWER_STATUS_CYCLE: + if (plat_pldm_host_button_sequence(power_sequence, POWER_OFF_BUTTON_MSEC) != 0) { + LOG_ERR("Failed to do host power cycle"); + } + k_msleep(POWER_CYCLE_INTERVAL_MSEC); + if (plat_pldm_host_button_sequence(power_sequence, POWER_ON_BUTTON_MSEC) != 0) { + LOG_ERR("Failed to do host power cycle"); + } + break; + case EFFECTER_STATE_POWER_STATUS_RESET: + if (plat_pldm_host_button_sequence(reset_sequence, RESET_BUTTON_MSEC) != 0) { + LOG_ERR("Failed to do host power reset"); + } + break; + case EFFECTER_STATE_POWER_STATUS_GRACEFUL_SHUTDOWN: + if (plat_pldm_host_button_sequence(power_sequence, GRACEFUL_SHUTDOWN_BUTTON_MSEC) != 0) { + LOG_ERR("Failed to do host graceful shutdown"); + } + break; + default: + LOG_ERR("Unsupport host power control effecter state, (%d)", + host_power_state->effecter_state); + *completion_code_p = PLDM_ERROR_INVALID_DATA; + break; + } + + return; +} diff --git a/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.h b/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.h index f0e473fa44..ab1b45330c 100644 --- a/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.h +++ b/meta-facebook/yv4-sd/src/platform/plat_pldm_monitor.h @@ -17,7 +17,11 @@ #ifndef PLAT_PLDM_MONITOR_H #define PLAT_PLDM_MONITOR_H -#define MAX_STATE_EFFECTER_IDX 168 +#define MAX_STATE_EFFECTER_IDX 169 + +#define PLDM_PLATFORM_HOST_PWR_CTRL_DEFAULT 0xFF +#define PLDM_PLATFORM_HOST_PWR_BTN_LOW 0xFE +#define PLDM_PLATFORM_HOST_RST_BTN_LOW 0xFD enum pldm_plat_effecter_id_high_byte { PLAT_EFFECTER_ID_GPIO_HIGH_BYTE = (0xFF << 8),