ch56x_pfic.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_PFIC_H__
  11. #define __CH56X_PFIC_H__
  12. #include "soc.h"
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. /* IREG: IENR/IRER/IRSR/IPRR, make sure irqn be 12~59
  17. */
  18. #define PFIC_IREG1_MASK 0xfffff000
  19. #define PFIC_IREG2_MASK 0x0fffffff
  20. #define PFIC_MAX_IREG_BITS 60
  21. #define _pfic_ireg_bit_get(pfic, IREG, irqn) \
  22. ((pfic->IREG[(irqn) >> 5] >> ((irqn) & 0x1f)) & 1)
  23. #define _pfic_ireg_bit_set(pfic, IREG, irqn) \
  24. do pfic->IREG[(irqn) >> 5] = 1 << ((irqn) & 0x1f); while(0)
  25. #define _pfic_irqn_enable(pfic, irqn) _pfic_ireg_bit_set(pfic, IENR, irqn)
  26. #define _pfic_irqn_disable(pfic, irqn) _pfic_ireg_bit_set(pfic, IRER, irqn)
  27. /* Note: `union _pfic_cfgr` is not used directly in the reg structure */
  28. union _pfic_cfgr
  29. {
  30. uint32_t reg;
  31. struct
  32. {
  33. uint32_t hwstkctrl : 1; // RW, hw stack push/pop control (0:enable)
  34. uint32_t nestctrl : 1; // RW, nested intr enable control (0:enable)
  35. uint32_t nmiset : 1; // WO, write 1 to set NMI pending
  36. uint32_t nmireset : 1; // WO, write 1 to reset NMI pending
  37. uint32_t excset : 1; // WO, write 1 to set exception pending
  38. uint32_t excreset : 1; // WO, write 1 to reset exception pending
  39. uint32_t pficreset : 1; // WO, write 1 to reset PFIC module, auto clear
  40. uint32_t sysreset : 1; // WO, write 1 for a system reset, auto clear
  41. uint32_t resv_8 : 8;
  42. uint32_t keycode : 16; // WO, write protection keycode
  43. };
  44. };
  45. #define PFIC_CFGR_KEY1 0xfa05 // for hwstkctrl & nestctrl
  46. #define PFIC_CFGR_KEY2 0xbcaf // for nmi & exc set/reset
  47. #define PFIC_CFGR_KEY3 0xbeef // for sysreset
  48. #define PFIC_CFGR_NMISET (PFIC_CFGR_KEY2 << 16 | 1 << 2)
  49. #define PFIC_CFGR_NMIRESET (PFIC_CFGR_KEY2 << 16 | 1 << 3)
  50. #define PFIC_CFGR_EXCSET (PFIC_CFGR_KEY2 << 16 | 1 << 4)
  51. #define PFIC_CFGR_EXCRESET (PFIC_CFGR_KEY2 << 16 | 1 << 5)
  52. #define PFIC_CFGR_PFICRESET (1 << 6)
  53. #define PFIC_CFGR_SYSRESET (PFIC_CFGR_KEY3 << 16 | 1 << 7)
  54. #define CFGR_HWSTKCTRL_ENABLE (0 << 0)
  55. #define CFGR_HWSTKCTRL_DISABLE (1 << 0)
  56. #define CFGR_HWSTKCTRL_MASK (1 << 0)
  57. #define CFGR_NESTCTRL_ENABLE (0 << 1)
  58. #define CFGR_NESTCTRL_DISABLE (1 << 1)
  59. #define CFGR_NESTCTRL_MASK (1 << 1)
  60. #define cfgr_nest_hwstk(v) (PFIC_CFGR_KEY1 << 16 | (v))
  61. union _pfic_gisr
  62. {
  63. uint32_t reg;
  64. struct
  65. {
  66. uint32_t neststa : 8; // RO, nested interrupt state (0/1/2)
  67. uint32_t gactsta : 1; // RO, global interrupt active status
  68. uint32_t gpendsta : 1; // RO, global interrupt pending status
  69. uint32_t resv_10 : 22;
  70. };
  71. };
  72. #define PFIC_NESTSTA_NONE 0
  73. #define PFIC_NESTSTA_L1 1
  74. #define PFIC_NESTSTA_L2 2
  75. union _pfic_fiofaddrr
  76. {
  77. uint32_t reg;
  78. struct
  79. {
  80. uint32_t offaddr : 24; // RW
  81. uint32_t irqid : 8; // RW
  82. };
  83. };
  84. union _pfic_sctlr
  85. {
  86. uint32_t reg;
  87. struct
  88. {
  89. uint32_t resv_0 : 1;
  90. uint32_t sleeponexit : 1; // enter (deep) sleep mode on isr exiting
  91. uint32_t sleepdeep : 1; // RW, 0/1 for sleep/deep-sleep mode
  92. uint32_t wfitowfe : 1; // RW, treat WFI as WFE
  93. uint32_t sevonpend : 1; // RW
  94. uint32_t setevent : 1; // WO, set event for WFE
  95. uint32_t resv_6 : 26;
  96. };
  97. };
  98. /*
  99. * 0x000 R32_PFIC_ISR1: PFIC interrupt enable status (# 12-31)
  100. * 0x004 R32_PFIC_ISR2: PFIC interrupt enable status (# 32-59)
  101. * 0x020 R32_PFIC_IPR1: PFIC interrupt pending status (# 12-31)
  102. * 0x024 R32_PFIC_IPR2: PFIC interrupt pending status (# 32-59)
  103. * 0x040 R32_PFIC_ITHRESDR: PFIC interrupt priority threshold (B.7-4)
  104. * 0x044 R32_PFIC_FIBADDRR: PFIC fast intr base address (B.31-28)
  105. * 0x048 R32_PFIC_CFGR: PFIC interrupt configuration register
  106. * 0x04c R32_PFIC_GISR: PFIC global interrupt status register
  107. * 0x060 R32_PFIC_FIOFADDRR0: PFIC fast intr 0 offset address (B.23-0)
  108. * 0x064 R32_PFIC_FIOFADDRR1: PFIC fast intr 1 offset address (B.23-0)
  109. * 0x068 R32_PFIC_FIOFADDRR2: PFIC fast intr 2 offset address (B.23-0)
  110. * 0x06c R32_PFIC_FIOFADDRR3: PFIC fast intr 3 offset address (B.23-0)
  111. * 0x100 R32_PFIC_IENR1: PFIC interrupt enable register (# 12-31)
  112. * 0x104 R32_PFIC_IENR2: PFIC interrupt enable register (# 32-59)
  113. * 0x180 R32_PFIC_IRER1: PFIC interrupt reset register (# 12-31)
  114. * 0x184 R32_PFIC_IRER2: PFIC interrupt reset register (# 32-59)
  115. * 0x200 R32_PFIC_IPSR1: PFIC interrupt pending set register (# 12-31)
  116. * 0x204 R32_PFIC_IPSR2: PFIC interrupt pending set register (# 32-59)
  117. * 0x280 R32_PFIC_IPRR1: PFIC interrupt pending reset register (# 12-31)
  118. * 0x284 R32_PFIC_IPRR2: PFIC interrupt pending reset register (# 32-59)
  119. * 0x300 R32_PFIC_IACTR1: PFIC interrupt active status register (# 12-31)
  120. * 0x304 R32_PFIC_IACTR2: PFIC interrupt active status register (# 32-59)
  121. * 0x400 R32_PFIC_IPRIORx: PFIC interrupt priority registers
  122. * 0xd10 R32_PFIC_SCTLR: PFIC system control register
  123. *
  124. * CAVEAT: gcc (as of 8.2.0) tends to read 32-bit word for bit field test.
  125. * Be careful for those with side effect for read.
  126. */
  127. struct pfic_registers
  128. {
  129. uint32_t ISR[2];
  130. uint32_t resv_08[6];
  131. uint32_t IPR[2];
  132. uint32_t resv_28[6];
  133. uint32_t ITHRESDR;
  134. uint32_t FIBADDRR;
  135. uint32_t CFGR;
  136. union _pfic_gisr GISR;
  137. uint32_t resv_50[4];
  138. union _pfic_fiofaddrr FIOFADDRR[4];
  139. uint32_t resv_70[36];
  140. uint32_t IENR[2];
  141. uint32_t resv_108[30];
  142. uint32_t IRER[2];
  143. uint32_t resv_188[30];
  144. uint32_t IPSR[2];
  145. uint32_t resv_208[30];
  146. uint32_t IPRR[2];
  147. uint32_t resv_288[30];
  148. uint32_t IACTR[2];
  149. uint32_t resv_308[62];
  150. uint8_t IPRIOR[256];
  151. uint32_t resv_500[516];
  152. union _pfic_sctlr SCTLR;
  153. } __packed;
  154. CHECK_STRUCT_SIZE(struct pfic_registers, 0xd14);
  155. union _systick_ctlr
  156. {
  157. uint32_t reg;
  158. struct
  159. {
  160. uint32_t ste : 1; // RW, systick enable
  161. uint32_t stie : 1; // RW, systick interrupt enable
  162. uint32_t stclk : 1; // RW, systick clock source select
  163. uint32_t resv_3 : 5;
  164. uint32_t streload : 1; // W1, write 1 to reload systick
  165. uint32_t resv_9 : 23;
  166. };
  167. };
  168. #define RB_STKCTL_STE 0x001
  169. #define RB_STKCTL_STIE 0x002
  170. #define RB_STKCTL_STCLK 0x004
  171. #define RB_STKCTL_STRELOAD 0x100
  172. #define SYSTICK_SRC_HCLK_8 0
  173. #define SYSTICK_SRC_HCLK 1
  174. union _systick_cntfg
  175. {
  176. uint32_t reg;
  177. struct
  178. {
  179. uint32_t swie : 1; // RW, software interrupt enable
  180. uint32_t cntif : 1; // RW0, counter dec to 0 (write 0 to clear)
  181. uint32_t resv_2 : 30;
  182. };
  183. };
  184. #define RB_CNTFG_SWIE 0X01
  185. #define RB_CNTFG_CNTIF 0X02
  186. /*
  187. * 0x00 R32_STK_CTLR: SysTick control register
  188. * 0x04 R32_STK_CNTL: SysTick counter, Lo.32
  189. * 0x08 R32_STK_CNTH: SysTick counter, Hi.32
  190. * 0x0c R32_STK_CMPLR: SysTick compare-reload register, Lo.32
  191. * 0x10 R32_STK_CMPHR: SysTick compare-reload register, Hi.32
  192. * 0x14 R32_STK_CNTFG: SysTick counter flags
  193. *
  194. * CAVEAT: gcc (as of 8.2.0) tends to read 32-bit word for bit field test.
  195. * Be careful for those with side effect for read.
  196. */
  197. struct systick_registers
  198. {
  199. union _systick_ctlr CTLR;
  200. uint32_t CNTL;
  201. uint32_t CNTH;
  202. uint32_t CMPLR;
  203. uint32_t CMPHR;
  204. union _systick_cntfg CNTFG;
  205. } __packed;
  206. CHECK_STRUCT_SIZE(struct systick_registers, 0x18);
  207. rt_inline void pfic_interrupt_umask(uint8_t irqn)
  208. {
  209. volatile struct pfic_registers *pfic;
  210. if (irqn < PFIC_MAX_IREG_BITS)
  211. {
  212. pfic = (void *)PFIC_REG_BASE;
  213. _pfic_ireg_bit_set(pfic, IENR, irqn);
  214. }
  215. }
  216. rt_inline void pfic_interrupt_mask(uint8_t irqn)
  217. {
  218. volatile struct pfic_registers *pfic;
  219. if (irqn < PFIC_MAX_IREG_BITS)
  220. {
  221. pfic = (void *)PFIC_REG_BASE;
  222. _pfic_ireg_bit_set(pfic, IRER, irqn);
  223. }
  224. }
  225. void pfic_swi_pendset(void);
  226. void pfic_swi_pendreset(void);
  227. void pfic_cfgr_set(uint32_t key_bit);
  228. void systick_init(uint32_t count);
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif