ch56x_sys.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-07-15 Emuzit first version
  9. */
  10. #ifndef __CH56X_SYS_H__
  11. #define __CH56X_SYS_H__
  12. #include "soc.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define sys_safe_access_enter(sys) \
  17. do { \
  18. sys->SAFE_ACCESS_SIG.reg = SAFE_ACCESS_SIG_1; \
  19. sys->SAFE_ACCESS_SIG.reg = SAFE_ACCESS_SIG_2; \
  20. } while(0)
  21. #define sys_safe_access_leave(sys) \
  22. do sys->SAFE_ACCESS_SIG.reg = 0; while(0)
  23. union _sys_safe_access_sig
  24. {
  25. uint8_t reg;
  26. struct
  27. {
  28. uint8_t safe_acc_mode : 2; // RO, current safe access, 11b => RWA ok
  29. uint8_t resv_2 : 2;
  30. uint8_t safe_acc_timer : 3; // RO, current safe access time count
  31. uint8_t resv_7 : 1;
  32. };
  33. };
  34. #define RB_SAFE_ACC_MODE 0x03
  35. #define RB_SAFE_ACC_TIMER 0x70
  36. #define SAFE_ACCESS_SIG_1 0x57
  37. #define SAFE_ACCESS_SIG_2 0xa8
  38. union _sys_glob_rom_cfg
  39. {
  40. uint8_t reg;
  41. struct
  42. {
  43. uint8_t rom_ext_re : 1; // RO, allow programmer to read FlashROM
  44. uint8_t code_ram_we : 1; // RWA, code SRAM writaboe
  45. uint8_t rom_data_we : 1; // RWA, FlashROM data erasable/writable
  46. uint8_t rom_code_we : 1; // RWA, FlashROM code erasable/writable
  47. uint8_t rom_code_ofs : 1; // RWA, FlashROM offset for user code
  48. uint8_t resv_5 : 3;
  49. };
  50. };
  51. #define RB_ROM_EXT_RE 0x01
  52. #define RB_CODE_RAM_WE 0x02
  53. #define RB_ROM_DATA_WE 0x04
  54. #define RB_ROM_CODE_WE 0x08
  55. #define RB_ROM_CODE_OFS 0x10
  56. #define ROM_CODE_OFS_0x00000 0
  57. #define ROM_CODE_OFS_0x04000 1
  58. union _sys_rst_boot_stat
  59. {
  60. uint8_t reg;
  61. struct
  62. {
  63. uint8_t reset_flag : 2; // RO, last reset cause
  64. uint8_t cfg_reset_en : 1; // RO, external reset pin (#RST) status
  65. uint8_t cfg_boot_en : 1; // RO, reset as 1
  66. uint8_t cfg_debug_en : 1; // RO
  67. uint8_t boot_loader : 1; // RO
  68. uint8_t resv_6 : 2;
  69. };
  70. };
  71. #define RB_RESET_FLAG 0x03
  72. #define RB_CFG_RESET_EN 0x04
  73. #define RB_CFG_BOOT_EN 0x08
  74. #define RB_CFG_DEBUG_EN 0x10
  75. #define RB_BOOT_LOADER 0x20
  76. #define RESET_FLAG_IS_SOFT 0
  77. #define RESET_FLAG_IS_POR 1
  78. #define RESET_FLAG_IS_WDOG 2
  79. #define RESET_FLAG_IS_RSTPIN 3
  80. union _sys_rst_wdog_ctrl
  81. {
  82. uint8_t reg;
  83. struct
  84. {
  85. uint8_t software_reset : 1; // WA/WZ, system software reset, auto clear
  86. #if defined(SOC_SERIES_CH569)
  87. uint8_t wdog_rst_en : 1; // RWA, enable watchdog overflow to reset
  88. uint8_t wdog_int_en : 1; // RWA, enable watchdog overflow interrupt
  89. uint8_t wdog_int_flag : 1; // RW1, watchdog counter overflow
  90. #else
  91. uint8_t resv_2 : 3;
  92. #endif
  93. uint8_t resv_4 : 4; // RO, B.7-6 must write 01b
  94. };
  95. };
  96. #define RB_SOFTWARE_RESET 0x01
  97. #ifdef SOC_SERIES_CH569
  98. #define RB_WDOG_RST_EN 0x02
  99. #define RB_WDOG_INT_EN 0x04
  100. #define RB_WDOG_INT_FLAG 0x08
  101. #endif
  102. #define wdog_ctrl_wdat(v) (0x40 | (v))
  103. union _sys_clk_pll_div
  104. {
  105. uint8_t reg;
  106. struct
  107. {
  108. uint8_t pll_div : 4; // RWA, min 2
  109. uint8_t resv_4 : 4; // RWA, B.7-6 must write 01b
  110. };
  111. };
  112. #define clk_pll_div_wdat(div) (0x40 | (div))
  113. union _sys_clk_cfg_ctrl
  114. {
  115. uint8_t reg;
  116. struct
  117. {
  118. uint8_t pll_sleep : 1; // RWA, PLL sleep control
  119. uint8_t sel_pll : 1; // RWA, clock source select
  120. uint8_t resv_6 : 6; // RWA, must write 10b
  121. };
  122. };
  123. #define RB_CLK_PLL_SLEEP 0x01
  124. #define RB_CLK_SEL_PLL 0x02
  125. #define CLK_PLL_SLEEP_DISABLE 0
  126. #define CLK_PLL_SLEEP_ENABLE 1
  127. #define CLK_SEL_PLL_HSE_30M 0
  128. #define CLK_SEL_PLL_USB_480M 1
  129. #define clk_cfg_ctrl_wdat(v) (0x80 | (v))
  130. union _sys_clk_mod_aux
  131. {
  132. uint8_t reg;
  133. struct
  134. {
  135. uint8_t int_125m_en : 1; // RWA, USB PHY 125MHz to ETH
  136. uint8_t ext_125m_en : 1; // RWA, external 125MHz to ETH
  137. uint8_t mco_sel_msk : 2; // RWA, MCO output select
  138. uint8_t mco_en : 1; // RWA, MCO output enable
  139. uint8_t resv_5 : 3;
  140. };
  141. };
  142. #define RB_INT_125M_EN 0x01
  143. #define RB_EXT_125M_EN 0x02
  144. #define RB_MCO_SEL_MSK 0x0c
  145. #define RB_MCO_EN 0x10
  146. #define MCO_SEL_MSK_125M 0
  147. #define MCO_SEL_MSK_25M 1
  148. #define MCO_SEL_MSK_2_5M 2
  149. /* All bits are RWA (need safe_access_sig), 0/1 : clock on/off
  150. */
  151. union _sys_slp_clk_off0
  152. {
  153. uint8_t reg;
  154. struct
  155. {
  156. uint8_t tmr0 : 1;
  157. uint8_t tmr1 : 1;
  158. uint8_t tmr2 : 1;
  159. uint8_t pwmx : 1;
  160. uint8_t uart0 : 1;
  161. uint8_t uart1 : 1;
  162. uint8_t uart2 : 1;
  163. uint8_t uart3 : 1;
  164. };
  165. };
  166. #define RB_SLP_CLK_TMR0 0x01
  167. #define RB_SLP_CLK_TMR1 0x02
  168. #define RB_SLP_CLK_TMR2 0x04
  169. #define RB_SLP_CLK_PWMX 0x08
  170. #define RB_SLP_CLK_UART0 0x10
  171. #define RB_SLP_CLK_UART1 0x20
  172. #define RB_SLP_CLK_UART2 0x40
  173. #define RB_SLP_CLK_UART3 0x80
  174. #define SYS_SLP_CLK_ON 0
  175. #define SYS_SLP_CLK_OFF 1
  176. /* All writable bits are RWA (need safe_access_sig), 0/1 : clock on/off
  177. */
  178. union _sys_slp_clk_off1
  179. {
  180. uint8_t reg;
  181. struct
  182. {
  183. uint8_t spi0 : 1;
  184. uint8_t spi1 : 1;
  185. #if defined(SOC_CH567)
  186. uint8_t sdc : 1;
  187. uint8_t led : 1;
  188. uint8_t usb0 : 1;
  189. uint8_t usb1 : 1;
  190. uint8_t resv_6 : 1;
  191. #elif defined(SOC_CH568)
  192. uint8_t sdc : 1;
  193. uint8_t led : 1;
  194. uint8_t resv_4 : 1;
  195. uint8_t usb1 : 1;
  196. uint8_t sata : 1;
  197. uint8_t ecdc : 1;
  198. #else
  199. uint8_t emmc : 1;
  200. uint8_t hspi : 1;
  201. uint8_t usbhs : 1;
  202. uint8_t usbss : 1;
  203. uint8_t serd : 1;
  204. uint8_t dvp : 1;
  205. #endif
  206. };
  207. };
  208. #define RB_SLP_CLK_SPI0 0x01
  209. #define RB_SLP_CLK_SPI1 0x02
  210. #if defined(SOC_WCH_CH567)
  211. #define RB_SLP_CLK_SDC 0x04
  212. #define RB_SLP_CLK_LED 0x08
  213. #define RB_SLP_CLK_USB0 0x10
  214. #define RB_SLP_CLK_USB1 0x20
  215. #define RB_SLP_CLK_ECDC 0x80
  216. #elif defined(SOC_WCH_CH568)
  217. #define RB_SLP_CLK_SDC 0x04
  218. #define RB_SLP_CLK_LED 0x08
  219. #define RB_SLP_CLK_USB1 0x20
  220. #define RB_SLP_CLK_SATA 0x40
  221. #define RB_SLP_CLK_ECDC 0x80
  222. #else
  223. #define RB_SLP_CLK_EMMC 0x04
  224. #define RB_SLP_CLK_HSPI 0x08
  225. #define RB_SLP_CLK_USBHS 0x10
  226. #define RB_SLP_CLK_USBSS 0x20
  227. #define RB_SLP_CLK_SERD 0x40
  228. #define RB_SLP_CLK_DVP 0x80
  229. #endif
  230. /* All writable bits are RWA (need safe_access_sig)
  231. */
  232. union _sys_slp_wake_ctrl
  233. {
  234. uint8_t reg;
  235. struct
  236. {
  237. #if defined(SOC_WCH_CH567)
  238. uint8_t usb0_wake : 1;
  239. uint8_t usb1_wake : 1;
  240. uint8_t resv_2 : 2;
  241. uint8_t gpio_wake : 1;
  242. uint8_t resv_5 : 3;
  243. #elif defined(SOC_WCH_CH568)
  244. uint8_t resv_0 : 1;
  245. uint8_t usb1_wake : 1;
  246. uint8_t sata_wake : 1;
  247. uint8_t resv_3 : 1;
  248. uint8_t gpio_wake : 1;
  249. uint8_t resv_5 : 3;
  250. #else
  251. uint8_t usbhs_wake : 1;
  252. uint8_t usbss_wake : 1;
  253. uint8_t clk_eth : 1;
  254. uint8_t clk_ecdc : 1;
  255. uint8_t gpio_wake : 1;
  256. uint8_t eth_wake : 1;
  257. uint8_t resv_6 : 2;
  258. #endif
  259. };
  260. };
  261. #if defined(SOC_WCH_CH567)
  262. #define RB_SLP_USB0_WAKE 0x01
  263. #define RB_SLP_USB1_WAKE 0x02
  264. #define RB_SLP_GPIO_WAKE 0x10
  265. #elif defined(SOC_WCH_CH568)
  266. #define RB_SLP_USB1_WAKE 0x02
  267. #define RB_SLP_SATA_WAKE 0x04
  268. #define RB_SLP_GPIO_WAKE 0x10
  269. #else
  270. #define RB_SLP_USBHS_WAKE 0x01
  271. #define RB_SLP_USBSS_WAKE 0x02
  272. #define RB_SLP_CLK_ETH 0x04
  273. #define RB_SLP_CLK_ECDC 0x08
  274. #define RB_SLP_GPIO_WAKE 0x10
  275. #define RB_SLP_ETH_WAKE 0x20
  276. #endif
  277. union _sys_slp_power_ctrl
  278. {
  279. uint8_t reg;
  280. struct
  281. {
  282. uint8_t usbhs_pwrdn : 1; // RWA, USBHS power down (0:PWRUP)
  283. uint8_t resv_2 : 7;
  284. };
  285. };
  286. #define RB_SLP_USBHS_PWRDN 0x01
  287. union _sys_serd_ana_cfg1
  288. {
  289. uint16_t reg;
  290. struct
  291. {
  292. uint8_t serd_pll_cfg; // RWA, reset as 0x5a
  293. uint8_t serd_30m_sel : 1; // RWA
  294. uint8_t serd_dn_tst : 1; // RWA
  295. uint8_t resv_10 : 6;
  296. };
  297. };
  298. #define RB_SERD_PLL_CFG 0x0ff
  299. #define RB_SERD_30M_SEL 0x100
  300. #define RB_SERD_DN_TST 0x200
  301. union _sys_serd_ana_cfg2
  302. {
  303. uint32_t reg;
  304. struct
  305. {
  306. uint32_t serd_trx_cfg : 25; // RWA, reset as 423015h
  307. uint32_t resv_25 : 7;
  308. };
  309. };
  310. #define RB_SERD_TRX_CFG 0x1000000
  311. /*
  312. * 0x00 R8_SAFE_ACCESS_SIG: safe access signature register
  313. * 0x01 R8_CHIP_ID: RF, chip ID register
  314. * 0x02 R8_SAFE_ACCESS_ID: RF, read as 02h
  315. * 0x03 R8_WDOG_COUNT RW, watchdog counter
  316. * 0x04 R8_GLOB_ROM_CFG: ROM config register
  317. * 0x05 R8_RST_BOOT_STAT: RO, boot state register
  318. * 0x06 R8_RST_WDOG_CTRL: software reset & watchdog control register
  319. * 0x07 R8_GLOB_RESET_KEEP: RW, only power-on-reset can clear this register
  320. * 0x08 R8_CLK_PLL_DIV: RWA, PLL output divisor register
  321. * 0x0a R8_CLK_CFG_CTRL: RWA, clock config register
  322. * 0x0b R8_CLK_MOD_AUX: RWA, clock auxiliary register
  323. * 0x0c R8_SLP_CLK_OFF0: RWA, sleep control register 0
  324. * 0x0d R8_SLP_CLK_OFF1: RWA, sleep control register 1
  325. * 0x0e R8_SLP_WAKE_CTRL: RWA, wakeup control register
  326. * 0x0f R8_SLP_POWER_CTRL: RWA, low power management register
  327. * 0x20 R16_SERD_ANA_CFG1: RWA, SerDes PHY analog param config register 1
  328. * 0x24 R32_SERD_ANA_CFG2: RWA, SerDes PHY analog param config register 2
  329. *
  330. * CAVEAT: gcc (as of 8.2.0) tends to read 32-bit word for bit field test.
  331. * Be careful for those with side effect for read.
  332. */
  333. struct sys_registers
  334. {
  335. union _sys_safe_access_sig SAFE_ACCESS_SIG;
  336. uint8_t CHIP_ID;
  337. uint8_t SAFE_ACCESS_ID;
  338. uint8_t WDOG_COUNT;
  339. union _sys_glob_rom_cfg GLOB_ROM_CFG;
  340. union _sys_rst_boot_stat RST_BOOT_STAT;
  341. union _sys_rst_wdog_ctrl RST_WDOG_CTRL;
  342. uint8_t GLOB_RESET_KEEP;
  343. union _sys_clk_pll_div CLK_PLL_DIV;
  344. uint8_t resv_9;
  345. union _sys_clk_cfg_ctrl CLK_CFG_CTRL;
  346. union _sys_clk_mod_aux CLK_MOD_AUX;
  347. union _sys_slp_clk_off0 SLP_CLK_OFF0;
  348. union _sys_slp_clk_off1 SLP_CLK_OFF1;
  349. union _sys_slp_wake_ctrl SLP_WAKE_CTRL;
  350. union _sys_slp_power_ctrl SLP_POWER_CTRL;
  351. #if defined(SOC_SERIES_CH569)
  352. uint32_t resv_10[4];
  353. union _sys_serd_ana_cfg1 SERD_ANA_CFG1;
  354. uint16_t resv_22;
  355. union _sys_serd_ana_cfg2 SERD_ANA_CFG2;
  356. #endif
  357. } __packed;
  358. CHECK_STRUCT_SIZE(struct sys_registers, 0x28);
  359. uint32_t sys_hclk_calc(void);
  360. uint32_t sys_hclk_get(void);
  361. int sys_hclk_set(uint32_t freq);
  362. int sys_clk_off_by_irqn(uint8_t irqn, int off);
  363. void sys_slp_clk_off0(uint8_t bits, int off);
  364. void sys_slp_clk_off1(uint8_t bits, int off);
  365. #ifdef __cplusplus
  366. }
  367. #endif
  368. #endif