diff options
-rw-r--r-- | board/ryu/board.c | 191 | ||||
-rw-r--r-- | board/ryu/board.h | 75 | ||||
-rw-r--r-- | board/ryu/build.mk | 13 | ||||
-rw-r--r-- | board/ryu/ec.tasklist | 25 | ||||
-rw-r--r-- | board/ryu/gpio.inc | 111 | ||||
-rw-r--r-- | board/ryu/usb_pd_config.h | 141 | ||||
-rw-r--r-- | board/ryu/usb_pd_policy.c | 130 | ||||
-rwxr-xr-x | util/flash_ec | 2 |
8 files changed, 687 insertions, 1 deletions
diff --git a/board/ryu/board.c b/board/ryu/board.c new file mode 100644 index 0000000000..88ae11f796 --- /dev/null +++ b/board/ryu/board.c @@ -0,0 +1,191 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ +/* ryu board configuration */ + +#include "adc.h" +#include "adc_chip.h" +#include "battery.h" +#include "charger.h" +#include "common.h" +#include "console.h" +#include "gpio.h" +#include "hooks.h" +#include "i2c.h" +#include "registers.h" +#include "task.h" +#include "usb_pd.h" +#include "usb_pd_config.h" +#include "util.h" + +void vbus_evt(enum gpio_signal signal) +{ + ccprintf("VBUS %d, %d!\n", signal, gpio_get_level(signal)); + task_wake(TASK_ID_PD); +} + +void unhandled_evt(enum gpio_signal signal) +{ + ccprintf("Unhandled INT %d,%d!\n", signal, gpio_get_level(signal)); +} + +void chipset_evt(enum gpio_signal signal) +{ + ccprintf("AP suspend %d,%d!\n", signal, gpio_get_level(signal)); +} + +#include "gpio_list.h" + +/* Initialize board. */ +static void board_init(void) +{ + /* + * Enable CC lines after all GPIO have been initialized. Note, it is + * important that this is enabled after the CC_DEVICE_ODL lines are + * set low to specify device mode. + */ + gpio_set_level(GPIO_USBC_CC_EN, 1); + + /* Enable interrupts on VBUS transitions. */ + gpio_enable_interrupt(GPIO_CHGR_ACOK); +} +DECLARE_HOOK(HOOK_INIT, board_init, HOOK_PRIO_DEFAULT); + +/* Pins with alternate functions */ +const struct gpio_alt_func gpio_alt_funcs[] = { + {GPIO_B, 0x0008, 0, MODULE_USB_PD},/* SPI1: SCK(PB3) */ + {GPIO_B, 0x0002, 0, MODULE_USB_PD},/* TIM14_CH1: PB1 */ + {GPIO_B, 0x00C0, 0, MODULE_UART}, /* USART1: PB6/PB7 */ + {GPIO_A, 0xC000, 1, MODULE_UART}, /* USART2: PA14/PA15 */ + {GPIO_C, 0x0C00, 1, MODULE_UART}, /* USART3: PC10/PC11 */ + {GPIO_B, 0x0F00, 1, MODULE_I2C}, /* I2C SLAVE:PB10/11 MASTER:PB8/9 */ +}; +const int gpio_alt_funcs_count = ARRAY_SIZE(gpio_alt_funcs); + +/* ADC channels */ +const struct adc_t adc_channels[] = { + /* Vbus sensing. Converted to mV, /10 voltage divider. */ + [ADC_VBUS] = {"VBUS", 30000, 4096, 0, STM32_AIN(0)}, + /* USB PD CC lines sensing. Converted to mV (3000mV/4096). */ + [ADC_CC1_PD] = {"CC1_PD", 3000, 4096, 0, STM32_AIN(1)}, + [ADC_CC2_PD] = {"CC1_PD", 3000, 4096, 0, STM32_AIN(3)}, + /* Charger current sensing. Converted to mA. */ + [ADC_IADP] = {"IADP", 7500, 4096, 0, STM32_AIN(8)}, + [ADC_IBAT] = {"IBAT", 37500, 4096, 0, STM32_AIN(13)}, +}; +BUILD_ASSERT(ARRAY_SIZE(adc_channels) == ADC_CH_COUNT); + +/* I2C ports */ +const struct i2c_port_t i2c_ports[] = { + {"master", I2C_PORT_MASTER, 100, + GPIO_MASTER_I2C_SCL, GPIO_MASTER_I2C_SDA}, + {"slave", I2C_PORT_SLAVE, 100, + GPIO_SLAVE_I2C_SCL, GPIO_SLAVE_I2C_SDA}, +}; +const unsigned int i2c_ports_used = ARRAY_SIZE(i2c_ports); + +void board_set_usb_mux(int port, enum typec_mux mux, int polarity) +{ + /* reset everything */ + gpio_set_level(GPIO_USBC_SS_EN_L, 1); + gpio_set_level(GPIO_USBC_DP_MODE_L, 1); + gpio_set_level(GPIO_USBC_DP_POLARITY, 1); + gpio_set_level(GPIO_USBC_SS1_USB_MODE_L, 1); + gpio_set_level(GPIO_USBC_SS2_USB_MODE_L, 1); + + if (mux == TYPEC_MUX_NONE) + /* everything is already disabled, we can return */ + return; + + if (mux == TYPEC_MUX_USB || mux == TYPEC_MUX_DOCK) { + /* USB 3.0 uses 2 superspeed lanes */ + gpio_set_level(polarity ? GPIO_USBC_SS2_USB_MODE_L : + GPIO_USBC_SS1_USB_MODE_L, 0); + } + + if (mux == TYPEC_MUX_DP || mux == TYPEC_MUX_DOCK) { + /* DP uses available superspeed lanes (x2 or x4) */ + gpio_set_level(GPIO_USBC_DP_POLARITY, polarity); + gpio_set_level(GPIO_USBC_DP_MODE_L, 0); + } + /* switch on superspeed lanes */ + gpio_set_level(GPIO_USBC_SS_EN_L, 0); +} + +/* PD Port polarity as detected by the common PD code */ +extern uint8_t pd_polarity; + +static int command_typec(int argc, char **argv) +{ + const char * const mux_name[] = {"none", "usb", "dp", "dock"}; + enum typec_mux mux = TYPEC_MUX_NONE; + int i; + + if (argc < 2) { + int has_ss = !gpio_get_level(GPIO_USBC_SS_EN_L); + int has_usb = !gpio_get_level(GPIO_USBC_SS1_USB_MODE_L) || + !gpio_get_level(GPIO_USBC_SS2_USB_MODE_L); + int has_dp = !gpio_get_level(GPIO_USBC_DP_MODE_L); + const char *dp_str = gpio_get_level(GPIO_USBC_DP_POLARITY) ? + "DP2" : "DP1"; + const char *usb_str = gpio_get_level(GPIO_USBC_SS1_USB_MODE_L) ? + "USB2" : "USB1"; + /* dump current state */ + ccprintf("Port CC1 %d mV CC2 %d mV (polarity:CC%d)\n", + pd_adc_read(0), pd_adc_read(1), pd_polarity + 1); + if (!has_ss) + ccprintf("No Superspeed connection\n"); + else + ccprintf("Superspeed %s%s%s\n", + has_dp ? dp_str : "", + has_dp && has_usb ? "+" : "", + has_usb ? usb_str : ""); + return EC_SUCCESS; + } + + for (i = 0; i < ARRAY_SIZE(mux_name); i++) + if (!strcasecmp(argv[1], mux_name[i])) + mux = i; + board_set_usb_mux(0, mux, pd_polarity); + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(typec, command_typec, + "[none|usb|dp|dock]", + "Control type-C connector muxing", + NULL); + +/** + * Discharge battery when on AC power for factory test. + */ +int board_discharge_on_ac(int enable) +{ + return charger_discharge_on_ac(enable); +} + +int extpower_is_present(void) +{ + return gpio_get_level(GPIO_CHGR_ACOK); +} + +/* Battery temperature ranges in degrees C */ +static const struct battery_info info = { + /* Design voltage */ + .voltage_max = 4400, + .voltage_normal = 3800, + .voltage_min = 2800, + /* Pre-charge current: I <= 0.01C */ + .precharge_current = 64, /* mA */ + /* Operational temperature range */ + .start_charging_min_c = 0, + .start_charging_max_c = 45, + .charging_min_c = 0, + .charging_max_c = 50, + .discharging_min_c = -20, + .discharging_max_c = 60, +}; + +const struct battery_info *battery_get_info(void) +{ + return &info; +} diff --git a/board/ryu/board.h b/board/ryu/board.h new file mode 100644 index 0000000000..596c25aac4 --- /dev/null +++ b/board/ryu/board.h @@ -0,0 +1,75 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* ryu board configuration */ + +#ifndef __BOARD_H +#define __BOARD_H + +/* 48 MHz SYSCLK clock frequency */ +#define CPU_CLOCK 48000000 + +/* the UART console is on USART2 (PA14/PA15) */ +#undef CONFIG_UART_CONSOLE +#define CONFIG_UART_CONSOLE 2 + +/* Optional features */ +#define CONFIG_STM_HWTIMER32 +#define CONFIG_USB_POWER_DELIVERY +#define CONFIG_USB_PD_DUAL_ROLE +#define CONFIG_USB_PD_INTERNAL_COMP +#define CONFIG_ADC +#define CONFIG_HW_CRC +#define CONFIG_I2C +#undef CONFIG_LID_SWITCH +#define CONFIG_VBOOT_HASH +#undef CONFIG_WATCHDOG_HELP +#undef CONFIG_TASK_PROFILING + +/* Charging configuration */ +#undef CONFIG_BATTERY_RYU /* TODO implement */ +#define CONFIG_BATTERY_SMART +#define CONFIG_BATTERY_REQUESTS_NIL_WHEN_DEAD +#define CONFIG_CHARGER +#define CONFIG_CHARGER_V2 +#define CONFIG_CHARGER_BQ24773 +#define CONFIG_CHARGER_ILIM_PIN_DISABLED +#define CONFIG_CHARGER_SENSE_RESISTOR 5 +#define CONFIG_CHARGER_SENSE_RESISTOR_AC 10 +#define CONFIG_CHARGER_INPUT_CURRENT 2048 +#define CONFIG_CHARGER_DISCHARGE_ON_AC + +/* I2C ports configuration */ +#define I2C_PORT_MASTER 0 +#define I2C_PORT_SLAVE 1 +#define I2C_PORT_EC I2C_PORT_SLAVE +#define I2C_PORT_CHARGER I2C_PORT_MASTER +#define I2C_PORT_BATTERY I2C_PORT_MASTER + +#ifndef __ASSEMBLER__ + +/* Timer selection */ +#define TIM_CLOCK32 2 +#define TIM_ADC 3 + +#include "gpio_signal.h" + +/* ADC signal */ +enum adc_channel { + ADC_CC1_PD = 0, + ADC_CC2_PD, + ADC_VBUS, + ADC_IADP, + ADC_IBAT, + /* Number of ADC channels */ + ADC_CH_COUNT +}; + +/* Discharge battery when on AC power for factory test. */ +int board_discharge_on_ac(int enable); + +#endif /* !__ASSEMBLER__ */ + +#endif /* __BOARD_H */ diff --git a/board/ryu/build.mk b/board/ryu/build.mk new file mode 100644 index 0000000000..83acdab147 --- /dev/null +++ b/board/ryu/build.mk @@ -0,0 +1,13 @@ +# Copyright (c) 2014 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +# +# Board specific files build + +# the IC is STmicro STM32F072VBH6 +CHIP:=stm32 +CHIP_FAMILY:=stm32f0 +CHIP_VARIANT:=stm32f07x + +board-y=board.o +board-$(CONFIG_USB_POWER_DELIVERY)+=usb_pd_policy.o diff --git a/board/ryu/ec.tasklist b/board/ryu/ec.tasklist new file mode 100644 index 0000000000..8ce15522f7 --- /dev/null +++ b/board/ryu/ec.tasklist @@ -0,0 +1,25 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/** + * List of enabled tasks in the priority order + * + * The first one has the lowest priority. + * + * For each task, use the macro TASK_ALWAYS(n, r, d, s) for base tasks and + * TASK_NOTEST(n, r, d, s) for tasks that can be excluded in test binaries, + * where : + * 'n' in the name of the task + * 'r' in the main routine of the task + * 'd' in an opaque parameter passed to the routine at startup + * 's' is the stack size in bytes; must be a multiple of 8 + */ +#define CONFIG_TASK_LIST \ + TASK_ALWAYS(HOOKS, hook_task, NULL, TASK_STACK_SIZE) \ + TASK_ALWAYS(CHARGER, charger_task, NULL, LARGER_TASK_STACK_SIZE) \ +/* DISABLED: TASK_NOTEST(CHIPSET, chipset_task, NULL, TASK_STACK_SIZE)*/ \ + TASK_NOTEST(HOSTCMD, host_command_task, NULL, TASK_STACK_SIZE) \ + TASK_ALWAYS(CONSOLE, console_task, NULL, LARGER_TASK_STACK_SIZE) \ + TASK_ALWAYS(PD, pd_task, NULL, TASK_STACK_SIZE) diff --git a/board/ryu/gpio.inc b/board/ryu/gpio.inc new file mode 100644 index 0000000000..2c90eb3303 --- /dev/null +++ b/board/ryu/gpio.inc @@ -0,0 +1,111 @@ +/* -*- mode:c -*- + * + * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Interrupts */ +GPIO(CHGR_ACOK, A, 2, GPIO_INT_BOTH | GPIO_PULL_UP, vbus_evt) +GPIO(BC_TEMP_ALERT_L, A, 13, GPIO_INT_FALLING, unhandled_evt) +GPIO(BTN_EC_PWR_L, C, 13, GPIO_INT_BOTH, unhandled_evt) +GPIO(USBC_BC12_INT_L, D, 11, GPIO_INT_FALLING | GPIO_PULL_UP, unhandled_evt) +GPIO(LID_OPEN, E, 1, GPIO_INT_BOTH | GPIO_PULL_UP, unhandled_evt) +GPIO(CHARGE_DONE, E, 6, GPIO_INT_RISING, unhandled_evt) +GPIO(LB_INT_L, E, 7, GPIO_INT_FALLING | GPIO_PULL_UP, unhandled_evt) +GPIO(LIGHTBAR_EN_L, E, 8, GPIO_INT_FALLING | GPIO_PULL_UP, unhandled_evt) +GPIO(AP_IN_SUSPEND, E, 9, GPIO_INT_BOTH | GPIO_PULL_UP, chipset_evt) +GPIO(BASE_PRES_L, E, 10, GPIO_INT_BOTH | GPIO_PULL_UP, unhandled_evt) + +/* Buttons */ +GPIO(BTN_VOLD_L, C, 0, GPIO_INPUT | GPIO_PULL_UP, NULL) +GPIO(BTN_VOLU_L, C, 1, GPIO_INPUT | GPIO_PULL_UP, NULL) + +/* PD RX/TX */ +GPIO(USBC_CC1_PD, A, 1, GPIO_ANALOG, NULL) +GPIO(USBC_CC2_PD, A, 3, GPIO_ANALOG, NULL) +GPIO(USBC_CC_EN, A, 4, GPIO_OUT_LOW, NULL) +GPIO(USBC_CC_TX_DATA, A, 6, GPIO_OUT_LOW, NULL) +GPIO(USBC_CC_TX_EN, D, 7, GPIO_OUT_LOW, NULL) + +#if 0 +/* Alternate functions */ +GPIO(USBC_TX_CLKOUT, B, 1, GPIO_OUT_LOW, NULL) +GPIO(USBC_TX_CLKIN, B, 3, GPIO_OUT_LOW, NULL) +#endif + +/* System power */ +GPIO(PMIC_PWRON_L, D, 14, GPIO_ODR_HIGH, NULL) +GPIO(PMIC_RESET_OUT_L, E, 4, GPIO_ODR_HIGH, NULL) +GPIO(EN_PP3300_RSVD, E, 13, GPIO_INPUT, NULL) +/* sensor temp output and PMIC reset input */ +GPIO(AP_OVERHEAT_L, D, 12, GPIO_ODR_HIGH, NULL) + +GPIO(VBUS_SENSE, A, 0, GPIO_ANALOG, NULL) +GPIO(CHGR_IADP, B, 0, GPIO_ANALOG, NULL) +GPIO(CHGR_IBAT, C, 3, GPIO_ANALOG, NULL) + +/* Inductive charging */ +GPIO(CHARGE_EN, D, 13, GPIO_OUT_LOW, NULL) +GPIO(BASE_CHG_VDD_EN, E, 5, GPIO_OUT_LOW, NULL) + +/* USB-C Power and muxes control */ +GPIO(USBC_CHARGE_EN_L, A, 7, GPIO_OUT_LOW, NULL) +GPIO(USBC_5V_EN, A, 10, GPIO_OUT_LOW, NULL) +GPIO(USBC_VCONN1_EN, F, 10, GPIO_OUT_HIGH, NULL) +GPIO(USBC_VCONN2_EN, D, 10, GPIO_OUT_HIGH, NULL) + +GPIO(USBC_CC1_DEVICE_ODL, A, 5, GPIO_ODR_LOW, NULL) +GPIO(USBC_CC2_DEVICE_ODL, F, 9, GPIO_ODR_LOW, NULL) + +GPIO(USBC_DP_MODE_L, D, 1, GPIO_OUT_HIGH, NULL) +GPIO(USBC_DP_POLARITY, D, 2, GPIO_OUT_HIGH, NULL) +GPIO(USBC_SS1_USB_MODE_L, D, 3, GPIO_OUT_HIGH, NULL) +GPIO(USBC_SS2_USB_MODE_L, D, 5, GPIO_OUT_HIGH, NULL) +GPIO(USBC_SS_EN_L, D, 6, GPIO_OUT_HIGH, NULL) + +/* Inputs */ +GPIO(BOARD_ID0, E, 11, GPIO_INPUT, NULL) +GPIO(BOARD_ID1, E, 12, GPIO_INPUT, NULL) +GPIO(SH_SIGNAL, E, 2, GPIO_INPUT, NULL) + +/* Lightbar reset */ +GPIO(LB_RST_L, D, 15, GPIO_ODR_HIGH | GPIO_PULL_UP, NULL) + +#if 0 +/* Alternate functions */ +GPIO(USB_DM, A, 11, GPIO_ANALOG, NULL) +GPIO(USB_DP, A, 12, GPIO_ANALOG, NULL) +GPIO(UART_TX, A, 14, GPIO_OUT_LOW, NULL) +GPIO(UART_RX, A, 15, GPIO_OUT_LOW, NULL) +#endif + +/* + * I2C pins should be configured as inputs until I2C module is + * initialized. This will avoid driving the lines unintentionally. + */ +GPIO(MASTER_I2C_SCL, B, 8, GPIO_INPUT, NULL) +GPIO(MASTER_I2C_SDA, B, 9, GPIO_INPUT, NULL) +GPIO(SLAVE_I2C_SCL, B, 10, GPIO_INPUT, NULL) +GPIO(SLAVE_I2C_SDA, B, 11, GPIO_INPUT, NULL) + +/* Case closed debugging. */ +GPIO(PD_DISABLE_DEBUG, C, 5, GPIO_OUT_LOW, NULL) +GPIO(SPI_FLASH_NSS, B, 12, GPIO_INPUT, NULL) +GPIO(SPI_FLASH_SCK, B, 13, GPIO_INPUT, NULL) +GPIO(SPI_FLASH_MOSI, B, 14, GPIO_INPUT, NULL) +GPIO(SPI_FLASH_MISO, B, 15, GPIO_INPUT, NULL) +GPIO(VDDSPI_EN, C, 12, GPIO_OUT_LOW, NULL) +GPIO(SH_RESET_L, C, 4, GPIO_ODR_HIGH, NULL) +GPIO(SH_BOOT, C, 9, GPIO_ODR_HIGH, NULL) +GPIO(EC_INT_L, F, 2, GPIO_ODR_HIGH, NULL) +GPIO(ENTERING_RW, F, 3, GPIO_OUT_LOW, NULL) +GPIO(WP_L, F, 6, GPIO_INPUT, NULL) + +#if 0 +/* Alternate functions */ +GPIO(SH_UART_TX, C, 11, GPIO_OUT_LOW, NULL) +GPIO(SH_UART_RX, C, 10, GPIO_INPUT, NULL) +GPIO(AP_UART_TX, B, 6, GPIO_OUT_LOW, NULL) +GPIO(AP_UART_RX, B, 7, GPIO_INPUT, NULL) +#endif diff --git a/board/ryu/usb_pd_config.h b/board/ryu/usb_pd_config.h new file mode 100644 index 0000000000..d7bcbc9843 --- /dev/null +++ b/board/ryu/usb_pd_config.h @@ -0,0 +1,141 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* USB Power delivery board configuration */ + +#ifndef __USB_PD_CONFIG_H +#define __USB_PD_CONFIG_H + +/* Timer selection for baseband PD communication */ +#define TIM_CLOCK_PD_TX 14 +#define TIM_CLOCK_PD_RX 1 + +/* use the hardware accelerator for CRC */ +#define CONFIG_HW_CRC + +/* TX is using SPI1 on PB3-5 */ +#define SPI_REGS STM32_SPI1_REGS +#define DMAC_SPI_TX STM32_DMAC_CH3 + +static inline void spi_enable_clock(void) +{ + STM32_RCC_APB2ENR |= STM32_RCC_PB2_SPI1; +} + +/* RX is using COMP1 triggering TIM1 CH1 */ +#define DMAC_TIM_RX STM32_DMAC_CH2 +#define TIM_CCR_IDX 1 +#define TIM_CCR_CS 1 +#define EXTI_COMP_MASK ((1 << 21) | (1 << 22)) +#define IRQ_COMP STM32_IRQ_COMP +/* triggers packet detection on comparator falling edge */ +#define EXTI_XTSR STM32_EXTI_FTSR + +/* the pins used for communication need to be hi-speed */ +static inline void pd_set_pins_speed(void) +{ + /* 40 MHz pin speed on SPI MISO PA6 */ + STM32_GPIO_OSPEEDR(GPIO_A) |= 0x00003000; + /* 40 MHz pin speed on TIM14_CH1 (PB1) */ + STM32_GPIO_OSPEEDR(GPIO_B) |= 0x0000000C; +} + +/* Reset SPI peripheral used for TX */ +static inline void pd_tx_spi_reset(void) +{ + /* Reset SPI1 */ + STM32_RCC_APB2RSTR |= (1 << 12); + STM32_RCC_APB2RSTR &= ~(1 << 12); +} + +/* Drive the CC line from the TX block */ +static inline void pd_tx_enable(int polarity) +{ + /* put SPI function on TX pin : PA6 is SPI MISO */ + gpio_set_alternate_function(GPIO_A, 0x0040, 0); + + /* set the low level reference */ + gpio_set_level(GPIO_USBC_CC_TX_EN, 1); +} + +/* Put the TX driver in Hi-Z state */ +static inline void pd_tx_disable(int polarity) +{ + /* output low on SPI TX (PA6 is SPI1 MISO) to disable the FET */ + STM32_GPIO_MODER(GPIO_A) = (STM32_GPIO_MODER(GPIO_A) + & ~(3 << (2*6))) + | (1 << (2*6)); + + /* put the low level reference in Hi-Z */ + gpio_set_level(GPIO_USBC_CC_TX_EN, 0); +} + +/* we know the plug polarity, do the right configuration */ +static inline void pd_select_polarity(int polarity) +{ + /* + * use the right comparator : CC1 -> PA1 (COMP1 INP) + * CC2 -> PA3 (COMP2 INP) + * use VrefInt / 2 as INM (about 600mV) + */ + STM32_COMP_CSR = (STM32_COMP_CSR + & ~(STM32_COMP_CMP1INSEL_MASK | STM32_COMP_CMP2INSEL_MASK + | STM32_COMP_CMP1EN | STM32_COMP_CMP2EN)) + | STM32_COMP_CMP1INSEL_VREF12 | STM32_COMP_CMP2INSEL_VREF12 + | (polarity ? STM32_COMP_CMP2EN : STM32_COMP_CMP1EN); +} + +/* Initialize pins used for TX and put them in Hi-Z */ +static inline void pd_tx_init(void) +{ + gpio_config_module(MODULE_USB_PD, 1); +} + +static inline void pd_set_host_mode(int enable) +{ + if (enable) { + /* We never charging in power source mode */ + gpio_set_level(GPIO_USBC_CHARGE_EN_L, 1); + /* High-Z is used for host mode. */ + gpio_set_level(GPIO_USBC_CC1_DEVICE_ODL, 1); + gpio_set_level(GPIO_USBC_CC2_DEVICE_ODL, 1); + } else { + /* Kill VBUS power supply */ + gpio_set_level(GPIO_USBC_5V_EN, 0); + /* Pull low for device mode. */ + gpio_set_level(GPIO_USBC_CC1_DEVICE_ODL, 0); + gpio_set_level(GPIO_USBC_CC2_DEVICE_ODL, 0); + /* Enable the charging path*/ + gpio_set_level(GPIO_USBC_CHARGE_EN_L, 0); + } + +} + +static inline int pd_adc_read(int cc) +{ + if (cc == 0) + return adc_read_channel(ADC_CC1_PD); + else + return adc_read_channel(ADC_CC2_PD); +} + +static inline int pd_snk_is_vbus_provided(void) +{ + return gpio_get_level(GPIO_CHGR_ACOK); +} + +/* Standard-current DFP : no-connect voltage is 1.55V */ +#define PD_SRC_VNC 1550 /* mV */ + +/* UFP-side : threshold for DFP connection detection */ +#define PD_SNK_VA 200 /* mV */ + +/* start as a sink in case we have no other power supply/battery */ +#define PD_DEFAULT_STATE PD_STATE_SNK_DISCONNECTED + +/* delay necessary for the voltage transition on the power supply */ +#define PD_POWER_SUPPLY_TRANSITION_DELAY 50000 /* us */ + +#endif /* __USB_PD_CONFIG_H */ diff --git a/board/ryu/usb_pd_policy.c b/board/ryu/usb_pd_policy.c new file mode 100644 index 0000000000..eaeda07ed8 --- /dev/null +++ b/board/ryu/usb_pd_policy.c @@ -0,0 +1,130 @@ +/* Copyright (c) 2014 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "common.h" +#include "console.h" +#include "gpio.h" +#include "hooks.h" +#include "host_command.h" +#include "registers.h" +#include "task.h" +#include "timer.h" +#include "util.h" +#include "usb_pd.h" + +#define CPRINTS(format, args...) cprints(CC_USBPD, format, ## args) + +/* TODO(crosbug.com/p/28869): update source and sink tables to spec. */ +const uint32_t pd_src_pdo[] = { + PDO_FIXED(5000, 500, PDO_FIXED_EXTERNAL), + PDO_FIXED(5000, 900, 0), +}; +const int pd_src_pdo_cnt = ARRAY_SIZE(pd_src_pdo); + +/* TODO(crosbug.com/p/28869): update source and sink tables to spec. */ +const uint32_t pd_snk_pdo[] = { + PDO_BATT(4500, 5500, 15000), + PDO_BATT(11500, 12500, 36000), +}; +const int pd_snk_pdo_cnt = ARRAY_SIZE(pd_snk_pdo); + +/* Cap on the max voltage requested as a sink (in millivolts) */ +static unsigned max_mv = -1; /* no cap */ + +int pd_choose_voltage(int cnt, uint32_t *src_caps, uint32_t *rdo) +{ + int i; + int sel_mv; + int max_uw = 0; + int max_i = -1; + + /* Get max power */ + for (i = 0; i < cnt; i++) { + int uw; + int mv = ((src_caps[i] >> 10) & 0x3FF) * 50; + if ((src_caps[i] & PDO_TYPE_MASK) == PDO_TYPE_BATTERY) { + uw = 250000 * (src_caps[i] & 0x3FF); + } else { + int ma = (src_caps[i] & 0x3FF) * 10; + uw = ma * mv; + } + if ((uw > max_uw) && (mv <= max_mv)) { + max_i = i; + max_uw = uw; + sel_mv = mv; + } + } + if (max_i < 0) + return -EC_ERROR_UNKNOWN; + + /* request all the power ... */ + if ((src_caps[max_i] & PDO_TYPE_MASK) == PDO_TYPE_BATTERY) { + int uw = 250000 * (src_caps[i] & 0x3FF); + *rdo = RDO_BATT(max_i + 1, uw/2, uw, 0); + ccprintf("Request [%d] %dV %d/%d mW\n", + max_i, sel_mv/1000, uw/1000, uw/1000); + } else { + int ma = 10 * (src_caps[max_i] & 0x3FF); + *rdo = RDO_FIXED(max_i + 1, ma / 2, ma, 0); + ccprintf("Request [%d] %dV %d/%d mA\n", + max_i, sel_mv/1000, max_i, ma/2, ma); + } + return EC_SUCCESS; +} + +void pd_set_max_voltage(unsigned mv) +{ + max_mv = mv; +} + +int pd_request_voltage(uint32_t rdo) +{ + int op_ma = rdo & 0x3FF; + int max_ma = (rdo >> 10) & 0x3FF; + int idx = rdo >> 28; + uint32_t pdo; + uint32_t pdo_ma; + + if (!idx || idx > pd_src_pdo_cnt) + return EC_ERROR_INVAL; /* Invalid index */ + + /* check current ... */ + pdo = pd_src_pdo[idx - 1]; + pdo_ma = (pdo & 0x3ff); + if (op_ma > pdo_ma) + return EC_ERROR_INVAL; /* too much op current */ + if (max_ma > pdo_ma) + return EC_ERROR_INVAL; /* too much max current */ + + ccprintf("Switch to %d V %d mA (for %d/%d mA)\n", + ((pdo >> 10) & 0x3ff) * 50, (pdo & 0x3ff) * 10, + ((rdo >> 10) & 0x3ff) * 10, (rdo & 0x3ff) * 10); + + return EC_SUCCESS; +} + +int pd_set_power_supply_ready(void) +{ + /* provide VBUS */ + gpio_set_level(GPIO_USBC_5V_EN, 1); + + return EC_SUCCESS; /* we are ready */ +} + +void pd_power_supply_reset(void) +{ + /* Kill VBUS */ + gpio_set_level(GPIO_USBC_5V_EN, 0); +} + +int pd_board_checks(void) +{ + return EC_SUCCESS; +} + +int pd_power_negotiation_allowed(void) +{ + return 1; +} diff --git a/util/flash_ec b/util/flash_ec index 1900ba0876..1401d341a5 100755 --- a/util/flash_ec +++ b/util/flash_ec @@ -285,7 +285,7 @@ save="$(servo_save)" case "${BOARD}" in big | discovery | nyan | pit | snow | spring ) flash_stm32 ;; - fruitpie | zinger | firefly | samus_pd ) flash_stm32 ;; + fruitpie | zinger | firefly | samus_pd | ryu ) flash_stm32 ;; twinkie) flash_stm32_dfu ;; falco | peppy | rambi | samus | squawks ) flash_lm4 ;; link ) flash_link ;; |