Mike Frysinger | 71b2ef7 | 2022-09-12 18:54:36 | [diff] [blame] | 1 | /* Copyright 2020 The ChromiumOS Authors |
Scott Collyer | cf21f6f | 2020-05-22 05:10:36 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | */ |
| 5 | #ifndef __CROS_EC_UCPD_STM32GX_H |
| 6 | #define __CROS_EC_UCPD_STM32GX_H |
| 7 | |
| 8 | /* STM32 UCPD driver for Chrome EC */ |
| 9 | |
| 10 | #include "usb_pd_tcpm.h" |
| 11 | |
| 12 | /* |
Scott Collyer | 7c931ff | 2021-03-18 05:16:54 | [diff] [blame] | 13 | * UCPD is fed directly from HSI which is @ 16MHz. The ucpd_clk goes to |
| 14 | * a prescaler who's output feeds the 'half-bit' divider which is used |
| 15 | * to generate clock for delay counters and BMC Rx/Tx blocks. The rx is |
| 16 | * designed to work in freq ranges of 6 <--> 18 MHz, however recommended |
| 17 | * range is 9 <--> 18 MHz. |
| 18 | * |
| 19 | * ------- @ 16 MHz --------- @ ~600 kHz ------------- |
| 20 | * HSI ---->| /psc |-------->| /hbit |--------------->| trans_cnt | |
| 21 | * ------- --------- | ------------- |
| 22 | * | ------------- |
| 23 | * |---------->| ifrgap_cnt| |
| 24 | * ------------- |
| 25 | * Requirements: |
| 26 | * 1. hbit_clk ~= 600 kHz: 16 MHz / 600 kHz = 26.67 |
| 27 | * 2. tTransitionWindow - 12 to 20 uSec |
| 28 | * 3. tInterframGap - uSec |
| 29 | * |
| 30 | * hbit_clk = HSI_clk / 27 = 592.6 kHz = 1.687 uSec period |
| 31 | * tTransitionWindow = 1.687 uS * 8 = 13.5 uS |
| 32 | * tInterFrameGap = 1.687 uS * 17 = 28.68 uS |
| 33 | */ |
| 34 | |
| 35 | #define UCPD_PSC_DIV 1 |
| 36 | #define UCPD_HBIT_DIV 27 |
| 37 | #define UCPD_TRANSWIN_CNT 8 |
| 38 | #define UCPD_IFRGAP_CNT 17 |
| 39 | |
Scott Collyer | 7c931ff | 2021-03-18 05:16:54 | [diff] [blame] | 40 | /* |
Scott Collyer | cf21f6f | 2020-05-22 05:10:36 | [diff] [blame] | 41 | * K-codes and ordered set defines. These codes and sets are used to encode |
| 42 | * which type of USB-PD message is being sent. This information can be found in |
| 43 | * the USB-PD spec section 5.4 - 5.6. This info is also included in the STM32G4 |
| 44 | * TRM (RM0440) 45.4.3 |
| 45 | */ |
| 46 | #define UCPD_SYNC1 0x18u |
| 47 | #define UCPD_SYNC2 0x11u |
| 48 | #define UCPD_SYNC3 0x06u |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 49 | #define UCPD_RST1 0x07u |
| 50 | #define UCPD_RST2 0x19u |
| 51 | #define UCPD_EOP 0x0Du |
Scott Collyer | cf21f6f | 2020-05-22 05:10:36 | [diff] [blame] | 52 | |
Abe Levkoy | 59e3950 | 2021-08-18 17:43:25 | [diff] [blame] | 53 | /* This order of this enum matches tcpm_sop_type */ |
Scott Collyer | cf21f6f | 2020-05-22 05:10:36 | [diff] [blame] | 54 | enum ucpd_tx_ordset { |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 55 | TX_ORDERSET_SOP = (UCPD_SYNC1 | (UCPD_SYNC1 << 5u) | |
| 56 | (UCPD_SYNC1 << 10u) | (UCPD_SYNC2 << 15u)), |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 57 | |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 58 | TX_ORDERSET_SOP_PRIME = (UCPD_SYNC1 | (UCPD_SYNC1 << 5u) | |
| 59 | (UCPD_SYNC3 << 10u) | (UCPD_SYNC3 << 15u)), |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 60 | |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 61 | TX_ORDERSET_SOP_PRIME_PRIME = |
| 62 | (UCPD_SYNC1 | (UCPD_SYNC3 << 5u) | (UCPD_SYNC1 << 10u) | |
| 63 | (UCPD_SYNC3 << 15u)), |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 64 | |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 65 | TX_ORDERSET_SOP_PRIME_DEBUG = |
| 66 | (UCPD_SYNC1 | (UCPD_RST2 << 5u) | (UCPD_RST2 << 10u) | |
| 67 | (UCPD_SYNC3 << 15u)), |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 68 | |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 69 | TX_ORDERSET_SOP_PRIME_PRIME_DEBUG = |
| 70 | (UCPD_SYNC1 | (UCPD_RST2 << 5u) | (UCPD_SYNC3 << 10u) | |
| 71 | (UCPD_SYNC2 << 15u)), |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 72 | |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 73 | TX_ORDERSET_HARD_RESET = (UCPD_RST1 | (UCPD_RST1 << 5u) | |
| 74 | (UCPD_RST1 << 10u) | (UCPD_RST2 << 15u)), |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 75 | |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 76 | TX_ORDERSET_CABLE_RESET = (UCPD_RST1 | (UCPD_SYNC1 << 5u) | |
| 77 | (UCPD_RST1 << 10u) | (UCPD_SYNC3 << 15u)), |
Scott Collyer | cf21f6f | 2020-05-22 05:10:36 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * STM32Gx UCPD implementation of tcpci .init method |
| 82 | * |
| 83 | * @param usbc_port -> USB-C Port number |
| 84 | * @return EC_SUCCESS |
| 85 | */ |
| 86 | int stm32gx_ucpd_init(int usbc_port); |
| 87 | |
| 88 | /** |
| 89 | * STM32Gx UCPD implementation of tcpci .release method |
| 90 | * |
| 91 | * @param usbc_port -> USB-C Port number |
| 92 | * @return EC_SUCCESS |
| 93 | */ |
| 94 | int stm32gx_ucpd_release(int usbc_port); |
| 95 | |
| 96 | /** |
| 97 | * STM32Gx UCPD implementation of tcpci .get_cc method |
| 98 | * |
| 99 | * @param usbc_port -> USB-C Port number |
| 100 | * @param *cc1 -> pointer to cc1 result |
| 101 | * @param *cc2 -> pointer to cc2 result |
| 102 | * @return EC_SUCCESS |
| 103 | */ |
| 104 | int stm32gx_ucpd_get_cc(int usbc_port, enum tcpc_cc_voltage_status *cc1, |
| 105 | enum tcpc_cc_voltage_status *cc2); |
| 106 | |
| 107 | /** |
| 108 | * STM32Gx equivalent for TCPCI role_control register |
| 109 | * |
| 110 | * @param usbc_port -> USB-C Port number |
| 111 | * @return EC_SUCCESS |
| 112 | */ |
| 113 | int stm32gx_ucpd_get_role_control(int usbc_port); |
| 114 | |
| 115 | /** |
| 116 | * STM32Gx UCPD implementation of tcpci .set_cc method |
| 117 | * |
| 118 | * @param usbc_port -> USB-C Port number |
| 119 | * @param cc_pull -> Rp or Rd selection |
| 120 | * @param rp -> value of Rp (if cc_pull == Rp) |
| 121 | * @return EC_SUCCESS |
| 122 | */ |
| 123 | int stm32gx_ucpd_set_cc(int usbc_port, int cc_pull, int rp); |
| 124 | |
| 125 | /** |
| 126 | * STM32Gx UCPD implementation of tcpci .set_cc method |
| 127 | * |
| 128 | * @param usbc_port -> USB-C Port number |
| 129 | * @param polarity -> CC1 or CC2 selection |
| 130 | * @return EC_SUCCESS |
| 131 | */ |
| 132 | int stm32gx_ucpd_set_polarity(int usbc_port, enum tcpc_cc_polarity polarity); |
| 133 | |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 134 | /** |
| 135 | * STM32Gx UCPD implementation of tcpci .set_rx_enable method |
| 136 | * |
| 137 | * @param usbc_port -> USB-C Port number |
| 138 | * @param enable -> on/off for USB-PD messages |
| 139 | * @return EC_SUCCESS |
| 140 | */ |
| 141 | int stm32gx_ucpd_set_rx_enable(int port, int enable); |
| 142 | |
| 143 | /** |
| 144 | * STM32Gx UCPD implementation of tcpci .set_msg_header method |
| 145 | * |
| 146 | * @param usbc_port -> USB-C Port number |
| 147 | * @param power_role -> port's current power role |
| 148 | * @param data_role -> port's current data role |
| 149 | * @return EC_SUCCESS |
| 150 | */ |
| 151 | int stm32gx_ucpd_set_msg_header(int port, int power_role, int data_role); |
| 152 | |
| 153 | /** |
| 154 | * STM32Gx UCPD implementation of tcpci .transmit method |
| 155 | * |
| 156 | * @param usbc_port -> USB-C Port number |
| 157 | * @param type -> SOP/SOP'/SOP'' etc |
| 158 | * @param header -> usb pd message header |
| 159 | * @param *data -> pointer to message contents |
| 160 | * @return EC_SUCCESS |
| 161 | */ |
Jack Rosenthal | faf897b | 2022-06-27 20:31:01 | [diff] [blame] | 162 | int stm32gx_ucpd_transmit(int port, enum tcpci_msg_type type, uint16_t header, |
Scott Collyer | ff4f8fd | 2020-11-07 05:12:27 | [diff] [blame] | 163 | const uint32_t *data); |
| 164 | |
| 165 | /** |
| 166 | * STM32Gx UCPD implementation of tcpci .get_message_raw method |
| 167 | * |
| 168 | * @param usbc_port -> USB-C Port number |
| 169 | * @param *payload -> pointer to where message should be written |
| 170 | * @param *head -> pointer to message header |
| 171 | * @return EC_SUCCESS |
| 172 | */ |
| 173 | int stm32gx_ucpd_get_message_raw(int port, uint32_t *payload, int *head); |
| 174 | |
Scott Collyer | 283c0e9 | 2020-12-13 03:12:02 | [diff] [blame] | 175 | /** |
| 176 | * STM32Gx method to remove Rp when VCONN is being supplied |
| 177 | * |
| 178 | * @param usbc_port -> USB-C Port number |
| 179 | * @param enable -> connect/disc Rp |
| 180 | * @return EC_SUCCESS |
| 181 | */ |
| 182 | int stm32gx_ucpd_vconn_disc_rp(int port, int enable); |
| 183 | |
| 184 | /** |
| 185 | * STM32Gx UCPD implementation of tcpci .sop_prime_enable method |
| 186 | * |
| 187 | * @param usbc_port -> USB-C Port number |
| 188 | * @param enable -> control of SOP'/SOP'' messages |
| 189 | * @return EC_SUCCESS |
| 190 | */ |
| 191 | int stm32gx_ucpd_sop_prime_enable(int port, bool enable); |
| 192 | |
| 193 | int stm32gx_ucpd_get_chip_info(int port, int live, |
| 194 | struct ec_response_pd_chip_info_v1 *chip_info); |
| 195 | |
| 196 | /** |
| 197 | * This function is used to enable/disable a ucpd debug feature that is used to |
| 198 | * mark the ucpd message log when there is a usbc detach event. |
| 199 | * |
| 200 | * @param enable -> on/off control for debug feature |
| 201 | */ |
| 202 | void ucpd_cc_detect_notify_enable(int enable); |
| 203 | |
Scott Collyer | 6e904f7 | 2021-02-01 03:56:51 | [diff] [blame] | 204 | /** |
| 205 | * This function is used to enable/disable rx bist test mode in the ucpd |
| 206 | * driver. This mode is controlled at the PE layer. When this mode is enabled, |
| 207 | * the ucpd receiver will not pass BIST data messages to the protocol layer and |
| 208 | * only send GoodCRC replies. |
| 209 | * |
| 210 | * @param usbc_port -> USB-C Port number |
| 211 | * @param enable -> on/off control for rx bist mode |
| 212 | */ |
| 213 | enum ec_error_list stm32gx_ucpd_set_bist_test_mode(const int port, |
| 214 | const bool enable); |
| 215 | |
Scott Collyer | cf21f6f | 2020-05-22 05:10:36 | [diff] [blame] | 216 | #endif /* __CROS_EC_UCPD_STM32GX_H */ |