PD Buddy Sink Firmware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fusb302b.h 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * PD Buddy - USB Power Delivery for everyone
  3. * Copyright (C) 2017 Clayton G. Hobbs <clay@lakeserv.net>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef PDB_FUSB302B_H
  19. #define PDB_FUSB302B_H
  20. #include <stdint.h>
  21. #include <pdb_fusb.h>
  22. #include <pdb_msg.h>
  23. /* Device ID register */
  24. #define FUSB_DEVICE_ID 0x01
  25. #define FUSB_DEVICE_ID_VERSION_ID_SHIFT 4
  26. #define FUSB_DEVICE_ID_VERSION_ID (0xF << FUSB_DEVICE_ID_VERSION_ID_SHIFT)
  27. #define FUSB_DEVICE_ID_PRODUCT_ID_SHIFT 2
  28. #define FUSB_DEVICE_ID_PRODUCT_ID (0x3 << FUSB_DEVICE_ID_PRODUCT_ID_SHIFT)
  29. #define FUSB_DEVICE_ID_REVISION_ID_SHIFT 0
  30. #define FUSB_DEVICE_ID_REVISION_ID (0x3 << FUSB_DEVICE_ID_REVISION_ID_SHIFT)
  31. /* Switches0 register */
  32. #define FUSB_SWITCHES0 0x02
  33. #define FUSB_SWITCHES0_PU_EN2 (1 << 7)
  34. #define FUSB_SWITCHES0_PU_EN1 (1 << 6)
  35. #define FUSB_SWITCHES0_VCONN_CC2 (1 << 5)
  36. #define FUSB_SWITCHES0_VCONN_CC1 (1 << 4)
  37. #define FUSB_SWITCHES0_MEAS_CC2 (1 << 3)
  38. #define FUSB_SWITCHES0_MEAS_CC1 (1 << 2)
  39. #define FUSB_SWITCHES0_PDWN_2 (1 << 1)
  40. #define FUSB_SWITCHES0_PDWN_1 1
  41. /* Switches1 register */
  42. #define FUSB_SWITCHES1 0x03
  43. #define FUSB_SWITCHES1_POWERROLE (1 << 7)
  44. #define FUSB_SWITCHES1_SPECREV_SHIFT 5
  45. #define FUSB_SWITCHES1_SPECREV (0x3 << FUSB_SWITCHES1_SPECREV_SHIFT)
  46. #define FUSB_SWITCHES1_DATAROLE (1 << 4)
  47. #define FUSB_SWITCHES1_AUTO_CRC (1 << 2)
  48. #define FUSB_SWITCHES1_TXCC2 (1 << 1)
  49. #define FUSB_SWITCHES1_TXCC1 1
  50. /* Measure register */
  51. #define FUSB_MEASURE 0x04
  52. #define FUSB_MEASURE_MEAS_VBUS (1 << 6)
  53. #define FUSB_MEASURE_MDAC_SHIFT 0
  54. #define FUSB_MEASURE_MDAC (0x3F << FUSB_MEASURE_MDAC_SHIFT)
  55. /* Slice register */
  56. #define FUSB_SLICE 0x05
  57. #define FUSB_SLICE_SDAC_HYS_SHIFT 6
  58. #define FUSB_SLICE_SDAC_HYS (0x3 << FUSB_SLICE_SDAC_HYS_SHIFT)
  59. #define FUSB_SLICE_SDAC_SHIFT 0
  60. #define FUSB_SLICE_SDAC (0x3F << FUSB_SLICE_SDAC_SHIFT)
  61. /* Control0 register */
  62. #define FUSB_CONTROL0 0x06
  63. #define FUSB_CONTROL0_TX_FLUSH (1 << 6)
  64. #define FUSB_CONTROL0_INT_MASK (1 << 5)
  65. #define FUSB_CONTROL0_HOST_CUR_SHIFT 2
  66. #define FUSB_CONTROL0_HOST_CUR (0x3 << FUSB_CONTROL0_HOST_CUR_SHIFT)
  67. #define FUSB_CONTROL0_AUTO_PRE (1 << 1)
  68. #define FUSB_CONTROL0_TX_START 1
  69. /* Control1 register */
  70. #define FUSB_CONTROL1 0x07
  71. #define FUSB_CONTROL1_ENSOP2DB (1 << 6)
  72. #define FUSB_CONTROL1_ENSOP1DB (1 << 5)
  73. #define FUSB_CONTROL1_BIST_MODE2 (1 << 4)
  74. #define FUSB_CONTROL1_RX_FLUSH (1 << 2)
  75. #define FUSB_CONTROL1_ENSOP2 (1 << 1)
  76. #define FUSB_CONTROL1_ENSOP1 1
  77. /* Control2 register */
  78. #define FUSB_CONTROL2 0x08
  79. #define FUSB_CONTROL2_TOG_SAVE_PWR_SHIFT 6
  80. #define FUSB_CONTROL2_TOG_SAVE_PWR (0x3 << FUSB_CONTROL2_TOG_SAVE_PWR)
  81. #define FUSB_CONTROL2_TOG_RD_ONLY (1 << 5)
  82. #define FUSB_CONTROL2_WAKE_EN (1 << 3)
  83. #define FUSB_CONTROL2_MODE_SHIFT 1
  84. #define FUSB_CONTROL2_MODE (0x3 << FUSB_CONTROL2_MODE_SHIFT)
  85. #define FUSB_CONTROL2_TOGGLE 1
  86. /* Control3 register */
  87. #define FUSB_CONTROL3 0x09
  88. #define FUSB_CONTROL3_SEND_HARD_RESET (1 << 6)
  89. #define FUSB_CONTROL3_BIST_TMODE (1 << 5)
  90. #define FUSB_CONTROL3_AUTO_HARDRESET (1 << 4)
  91. #define FUSB_CONTROL3_AUTO_SOFTRESET (1 << 3)
  92. #define FUSB_CONTROL3_N_RETRIES_SHIFT 1
  93. #define FUSB_CONTROL3_N_RETRIES (0x3 << FUSB_CONTROL3_N_RETRIES_SHIFT)
  94. #define FUSB_CONTROL3_AUTO_RETRY 1
  95. /* Mask1 register */
  96. #define FUSB_MASK1 0x0A
  97. #define FUSB_MASK1_M_VBUSOK (1 << 7)
  98. #define FUSB_MASK1_M_ACTIVITY (1 << 6)
  99. #define FUSB_MASK1_M_COMP_CHNG (1 << 5)
  100. #define FUSB_MASK1_M_CRC_CHK (1 << 4)
  101. #define FUSB_MASK1_M_ALERT (1 << 3)
  102. #define FUSB_MASK1_M_WAKE (1 << 2)
  103. #define FUSB_MASK1_M_COLLISION (1 << 1)
  104. #define FUSB_MASK1_M_BC_LVL (1 << 0)
  105. /* Power register */
  106. #define FUSB_POWER 0x0B
  107. #define FUSB_POWER_PWR3 (1 << 3)
  108. #define FUSB_POWER_PWR2 (1 << 2)
  109. #define FUSB_POWER_PWR1 (1 << 1)
  110. #define FUSB_POWER_PWR0 1
  111. /* Reset register */
  112. #define FUSB_RESET 0x0C
  113. #define FUSB_RESET_PD_RESET (1 << 1)
  114. #define FUSB_RESET_SW_RES 1
  115. /* OCPreg register */
  116. #define FUSB_OCPREG 0x0D
  117. #define FUSB_OCPREG_OCP_RANGE (1 << 3)
  118. #define FUSB_OCPREG_OCP_CUR_SHIFT 0
  119. #define FUSB_OCPREG_OCP_CUR (0x7 << FUSB_OCPREG_OCP_CUR_SHIFT)
  120. /* Maska register */
  121. #define FUSB_MASKA 0x0E
  122. #define FUSB_MASKA_M_OCP_TEMP (1 << 7)
  123. #define FUSB_MASKA_M_TOGDONE (1 << 6)
  124. #define FUSB_MASKA_M_SOFTFAIL (1 << 5)
  125. #define FUSB_MASKA_M_RETRYFAIL (1 << 4)
  126. #define FUSB_MASKA_M_HARDSENT (1 << 3)
  127. #define FUSB_MASKA_M_TXSENT (1 << 2)
  128. #define FUSB_MASKA_M_SOFTRST (1 << 1)
  129. #define FUSB_MASKA_M_HARDRST 1
  130. /* Maskb register */
  131. #define FUSB_MASKB 0x0F
  132. #define FUSB_MASKB_M_GCRCSENT 1
  133. /* Control4 register */
  134. #define FUSB_CONTROL4 0x10
  135. #define FUSB_CONTROL4_TOG_EXIT_AUD 1
  136. /* Status0a register */
  137. #define FUSB_STATUS0A 0x3C
  138. #define FUSB_STATUS0A_SOFTFAIL (1 << 5)
  139. #define FUSB_STATUS0A_RETRYFAIL (1 << 4)
  140. #define FUSB_STATUS0A_POWER3 (1 << 3)
  141. #define FUSB_STATUS0A_POWER2 (1 << 2)
  142. #define FUSB_STATUS0A_SOFTRST (1 << 1)
  143. #define FUSB_STATUS0A_HARDRST 1
  144. /* Status1a register */
  145. #define FUSB_STATUS1A 0x3D
  146. #define FUSB_STATUS1A_TOGSS_SHIFT 3
  147. #define FUSB_STATUS1A_TOGSS (0x7 << FUSB_STATUS1A_TOGSS_SHIFT)
  148. #define FUSB_STATUS1A_RXSOP2DB (1 << 2)
  149. #define FUSB_STATUS1A_RXSOP1DB (1 << 1)
  150. #define FUSB_STATUS1A_RXSOP 1
  151. /* Interrupta register */
  152. #define FUSB_INTERRUPTA 0x3E
  153. #define FUSB_INTERRUPTA_I_OCP_TEMP (1 << 7)
  154. #define FUSB_INTERRUPTA_I_TOGDONE (1 << 6)
  155. #define FUSB_INTERRUPTA_I_SOFTFAIL (1 << 5)
  156. #define FUSB_INTERRUPTA_I_RETRYFAIL (1 << 4)
  157. #define FUSB_INTERRUPTA_I_HARDSENT (1 << 3)
  158. #define FUSB_INTERRUPTA_I_TXSENT (1 << 2)
  159. #define FUSB_INTERRUPTA_I_SOFTRST (1 << 1)
  160. #define FUSB_INTERRUPTA_I_HARDRST 1
  161. /* Interruptb register */
  162. #define FUSB_INTERRUPTB 0x3F
  163. #define FUSB_INTERRUPTB_I_GCRCSENT 1
  164. /* Status0 register */
  165. #define FUSB_STATUS0 0x40
  166. #define FUSB_STATUS0_VBUSOK (1 << 7)
  167. #define FUSB_STATUS0_ACTIVITY (1 << 6)
  168. #define FUSB_STATUS0_COMP (1 << 5)
  169. #define FUSB_STATUS0_CRC_CHK (1 << 4)
  170. #define FUSB_STATUS0_ALERT (1 << 3)
  171. #define FUSB_STATUS0_WAKE (1 << 2)
  172. #define FUSB_STATUS0_BC_LVL_SHIFT 0
  173. #define FUSB_STATUS0_BC_LVL (0x3 << FUSB_STATUS0_BC_LVL_SHIFT)
  174. /* Status1 register */
  175. #define FUSB_STATUS1 0x41
  176. #define FUSB_STATUS1_RXSOP2 (1 << 7)
  177. #define FUSB_STATUS1_RXSOP1 (1 << 6)
  178. #define FUSB_STATUS1_RX_EMPTY (1 << 5)
  179. #define FUSB_STATUS1_RX_FULL (1 << 4)
  180. #define FUSB_STATUS1_TX_EMPTY (1 << 3)
  181. #define FUSB_STATUS1_TX_FULL (1 << 2)
  182. #define FUSB_STATUS1_OVRTEMP (1 << 1)
  183. #define FUSB_STATUS1_OCP 1
  184. /* Interrupt register */
  185. #define FUSB_INTERRUPT 0x42
  186. #define FUSB_INTERRUPT_I_VBUSOK (1 << 7)
  187. #define FUSB_INTERRUPT_I_ACTIVITY (1 << 6)
  188. #define FUSB_INTERRUPT_I_COMP_CHNG (1 << 5)
  189. #define FUSB_INTERRUPT_I_CRC_CHK (1 << 4)
  190. #define FUSB_INTERRUPT_I_ALERT (1 << 3)
  191. #define FUSB_INTERRUPT_I_WAKE (1 << 2)
  192. #define FUSB_INTERRUPT_I_COLLISION (1 << 1)
  193. #define FUSB_INTERRUPT_I_BC_LVL 1
  194. /* FIFOs register */
  195. #define FUSB_FIFOS 0x43
  196. #define FUSB_FIFO_TX_TXON 0xA1
  197. #define FUSB_FIFO_TX_SOP1 0x12
  198. #define FUSB_FIFO_TX_SOP2 0x13
  199. #define FUSB_FIFO_TX_SOP3 0x1B
  200. #define FUSB_FIFO_TX_RESET1 0x15
  201. #define FUSB_FIFO_TX_RESET2 0x16
  202. #define FUSB_FIFO_TX_PACKSYM 0x80
  203. #define FUSB_FIFO_TX_JAM_CRC 0xFF
  204. #define FUSB_FIFO_TX_EOP 0x14
  205. #define FUSB_FIFO_TX_TXOFF 0xFE
  206. #define FUSB_FIFO_RX_TOKEN_BITS 0xE0
  207. #define FUSB_FIFO_RX_SOP 0xE0
  208. #define FUSB_FIFO_RX_SOP1 0xC0
  209. #define FUSB_FIFO_RX_SOP2 0xA0
  210. #define FUSB_FIFO_RX_SOP1DB 0x80
  211. #define FUSB_FIFO_RX_SOP2DB 0x60
  212. /*
  213. * FUSB status union
  214. *
  215. * Provides a nicer structure than just an array of uint8_t for working with
  216. * the FUSB302B status and interrupt flags.
  217. */
  218. union fusb_status {
  219. uint8_t bytes[7];
  220. struct {
  221. uint8_t status0a;
  222. uint8_t status1a;
  223. uint8_t interrupta;
  224. uint8_t interruptb;
  225. uint8_t status0;
  226. uint8_t status1;
  227. uint8_t interrupt;
  228. };
  229. };
  230. /* FUSB functions */
  231. /*
  232. * Send a USB Power Delivery message to the FUSB302B
  233. */
  234. void fusb_send_message(struct pdb_fusb_config *cfg, const union pd_msg *msg);
  235. /*
  236. * Read a USB Power Delivery message from the FUSB302B
  237. */
  238. uint8_t fusb_read_message(struct pdb_fusb_config *cfg, union pd_msg *msg);
  239. /*
  240. * Tell the FUSB302B to send a hard reset signal
  241. */
  242. void fusb_send_hardrst(struct pdb_fusb_config *cfg);
  243. /*
  244. * Read the FUSB302B status and interrupt flags into *status
  245. */
  246. void fusb_get_status(struct pdb_fusb_config *cfg, union fusb_status *status);
  247. /*
  248. * Read the FUSB302B BC_LVL as an enum fusb_typec_current
  249. */
  250. enum fusb_typec_current fusb_get_typec_current(struct pdb_fusb_config *cfg);
  251. /*
  252. * Initialization routine for the FUSB302B
  253. */
  254. void fusb_setup(struct pdb_fusb_config *);
  255. /*
  256. * Reset the FUSB302B
  257. */
  258. void fusb_reset(struct pdb_fusb_config *cfg);
  259. #endif /* PDB_FUSB302B_H */