drv_sys.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-11-11 Wayne First version
  10. *
  11. ******************************************************************************/
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include "NuMicro.h"
  15. #include "drv_sys.h"
  16. #define SYS_MIN_INT_SOURCE 1
  17. #define SYS_MAX_INT_SOURCE 63
  18. #define SYS_NUM_OF_AICREG 16
  19. #define INT_IRQ 0x00
  20. #define INT_FIQ 0x01
  21. extern rt_uint32_t rt_interrupt_nest;
  22. rt_uint32_t rt_interrupt_from_thread, rt_interrupt_to_thread;
  23. rt_uint32_t rt_thread_switch_interrupt_flag;
  24. struct rt_irq_desc irq_desc[SYS_MAX_INT_SOURCE + 1];
  25. void rt_hw_interrupt_dummy_handler(int vector, void *param)
  26. {
  27. rt_kprintf("Unhandled interrupt %d occurred!!!\n", vector);
  28. RT_ASSERT(0);
  29. }
  30. rt_uint32_t rt_hw_interrupt_get_active(rt_uint32_t fiq_irq)
  31. {
  32. rt_uint32_t active = 0;
  33. #if 0
  34. rt_uint32_t volatile _mIPER, _mISNR;
  35. _mIPER = (inpw(REG_AIC_IPER) >> 2) & 0x3f;
  36. _mISNR = inpw(REG_AIC_ISNR);
  37. if ((_mISNR != 0) && (_mIPER == _mISNR))
  38. active = _mISNR;
  39. #else
  40. if (fiq_irq != INT_FIQ)
  41. {
  42. active = inpw(REG_AIC_IRQNUM);
  43. }
  44. else
  45. active = inpw(REG_AIC_FIQNUM);
  46. #endif
  47. return active;
  48. }
  49. void rt_hw_interrupt_set_priority(int vector, int IntTypeLevel)
  50. {
  51. rt_uint32_t _mRegAddr;
  52. rt_uint32_t shift;
  53. if ((vector > SYS_MAX_INT_SOURCE) || (vector < SYS_MIN_INT_SOURCE))
  54. return;
  55. _mRegAddr = REG_AIC_SRCCTL0 + ((vector / 4) * 4);
  56. shift = (vector % 4) * 8;
  57. IntTypeLevel &= 0x7;
  58. outpw(_mRegAddr, (inpw(_mRegAddr) & ~(0x07 << shift)) | (IntTypeLevel << shift));
  59. }
  60. void rt_hw_interrupt_ack(rt_uint32_t fiq_irq, rt_uint32_t id)
  61. {
  62. if (fiq_irq != INT_FIQ)
  63. outpw(REG_AIC_EOIS, 1);
  64. else
  65. outpw(REG_AIC_EOFS, 1);
  66. }
  67. void rt_interrupt_dispatch(rt_uint32_t fiq_irq)
  68. {
  69. rt_isr_handler_t isr_func;
  70. rt_uint32_t irq;
  71. void *param;
  72. /* get irq number */
  73. irq = rt_hw_interrupt_get_active(fiq_irq);
  74. /* get interrupt service routine */
  75. isr_func = irq_desc[irq].handler;
  76. param = irq_desc[irq].param;
  77. /* turn to interrupt service routine */
  78. isr_func(irq, param);
  79. rt_hw_interrupt_ack(fiq_irq, irq);
  80. #ifdef RT_USING_INTERRUPT_INFO
  81. irq_desc[irq].counter ++;
  82. #endif
  83. }
  84. void rt_hw_interrupt_init(void)
  85. {
  86. int i;
  87. *((volatile unsigned int *)REG_AIC_INTDIS0) = 0xFFFFFFFF; // disable all interrupt channel
  88. *((volatile unsigned int *)REG_AIC_INTDIS1) = 0xFFFFFFFF; // disable all interrupt channel
  89. /* init interrupt nest, and context in thread sp */
  90. rt_interrupt_nest = 0;
  91. rt_interrupt_from_thread = 0;
  92. rt_interrupt_to_thread = 0;
  93. rt_thread_switch_interrupt_flag = 0;
  94. for (i = 1; i <= SYS_MAX_INT_SOURCE; i++)
  95. {
  96. rt_hw_interrupt_install(i, rt_hw_interrupt_dummy_handler, RT_NULL, (char *)"dummy");
  97. rt_hw_interrupt_mask(i);
  98. }
  99. }
  100. rt_isr_handler_t rt_hw_interrupt_install(int vector, rt_isr_handler_t handler, void *param, const char *name)
  101. {
  102. rt_isr_handler_t old_handler = RT_NULL;
  103. if (vector > SYS_MAX_INT_SOURCE)
  104. return RT_NULL;
  105. /* Set default priority IRQ_LEVEL_7 */
  106. rt_hw_interrupt_set_priority(vector, IRQ_LEVEL_7);
  107. old_handler = irq_desc[vector].handler;
  108. if (handler != RT_NULL)
  109. {
  110. irq_desc[vector].handler = (rt_isr_handler_t)handler;
  111. irq_desc[vector].param = param;
  112. #ifdef RT_USING_INTERRUPT_INFO
  113. rt_snprintf(irq_desc[vector].name, RT_NAME_MAX - 1, "%s", name);
  114. irq_desc[vector].counter = 0;
  115. #endif
  116. }
  117. return old_handler;
  118. }
  119. /* Disable interrupt */
  120. void rt_hw_interrupt_mask(int vector)
  121. {
  122. sysDisableInterrupt((IRQn_Type)vector);
  123. }
  124. void rt_hw_interrupt_umask(int vector)
  125. {
  126. sysEnableInterrupt((IRQn_Type)vector);
  127. }
  128. /* TYPE
  129. * #define LOW_LEVEL_SENSITIVE 0x00
  130. * #define HIGH_LEVEL_SENSITIVE 0x40
  131. * #define NEGATIVE_EDGE_TRIGGER 0x80
  132. * #define POSITIVE_EDGE_TRIGGER 0xC0
  133. */
  134. void rt_hw_interrupt_set_type(int vector, int type)
  135. {
  136. rt_uint32_t _mRegAddr;
  137. rt_uint32_t shift;
  138. if ((vector > SYS_MAX_INT_SOURCE) || (vector < SYS_MIN_INT_SOURCE))
  139. return ;
  140. _mRegAddr = REG_AIC_SRCCTL0 + ((vector / 4) * 4);
  141. shift = (vector % 4) * 8;
  142. type &= 0xC0;
  143. outpw(_mRegAddr, (inpw(_mRegAddr) & ~(0xC0 << shift)) | (type << shift));
  144. }
  145. void rt_low_level_init(void)
  146. {
  147. }
  148. void nu_clock_base_init(void)
  149. {
  150. nu_sys_ipclk_enable(CPUCKEN);
  151. nu_sys_ipclk_enable(HCLKCKEN);
  152. nu_sys_ipclk_enable(HCLK1CKEN);
  153. nu_sys_ipclk_enable(HCLK3CKEN);
  154. nu_sys_ipclk_enable(HCLK4CKEN);
  155. nu_sys_ipclk_enable(PCLK0CKEN);
  156. nu_sys_ipclk_enable(PCLK1CKEN);
  157. nu_sys_ipclk_enable(SRAMCKEN);
  158. nu_sys_ipclk_enable(SDICCKEN);
  159. nu_sys_ipclk_enable(PCLK2CKEN);
  160. nu_sys_ipclk_enable(PCLKEN0_Reserved_3);
  161. }
  162. void machine_reset(void)
  163. {
  164. rt_kprintf("machine_reset...\n");
  165. rt_hw_interrupt_disable();
  166. /* Unlock */
  167. SYS_UnlockReg();
  168. nu_sys_ip_reset(CHIPRST);
  169. while (1);
  170. }
  171. void machine_shutdown(void)
  172. {
  173. rt_kprintf("machine_shutdown...\n");
  174. rt_hw_interrupt_disable();
  175. /* Unlock */
  176. SYS_UnlockReg();
  177. while (1);
  178. }
  179. void nu_sys_ip_reset(E_SYS_IPRST eIPRstIdx)
  180. {
  181. uint32_t u32IPRSTRegAddr;
  182. uint32_t u32IPRSTRegBit;
  183. rt_uint32_t level;
  184. if (eIPRstIdx >= SYS_IPRST_CNT)
  185. return;
  186. u32IPRSTRegAddr = REG_SYS_AHBIPRST + (4ul * (eIPRstIdx / 32));
  187. u32IPRSTRegBit = eIPRstIdx % 32;
  188. /* Enter critical section */
  189. level = rt_hw_interrupt_disable();
  190. /* Enable IP reset */
  191. outpw(u32IPRSTRegAddr, inpw(u32IPRSTRegAddr) | (1 << u32IPRSTRegBit));
  192. /* Disable IP reset */
  193. outpw(u32IPRSTRegAddr, inpw(u32IPRSTRegAddr) & ~(1 << u32IPRSTRegBit));
  194. /* Leave critical section */
  195. rt_hw_interrupt_enable(level);
  196. }
  197. static void _nu_sys_ipclk(E_SYS_IPCLK eIPClkIdx, uint32_t bEnable)
  198. {
  199. uint32_t u32IPCLKRegAddr;
  200. uint32_t u32IPCLKRegBit;
  201. rt_uint32_t level;
  202. if (eIPClkIdx >= SYS_IPCLK_CNT)
  203. return;
  204. u32IPCLKRegAddr = REG_CLK_HCLKEN + (4ul * (eIPClkIdx / 32));
  205. u32IPCLKRegBit = eIPClkIdx % 32;
  206. /* Enter critical section */
  207. level = rt_hw_interrupt_disable();
  208. if (bEnable)
  209. {
  210. /* Enable IP CLK */
  211. outpw(u32IPCLKRegAddr, inpw(u32IPCLKRegAddr) | (1 << u32IPCLKRegBit));
  212. }
  213. else
  214. {
  215. /* Disable IP CLK */
  216. outpw(u32IPCLKRegAddr, inpw(u32IPCLKRegAddr) & ~(1 << u32IPCLKRegBit));
  217. }
  218. /* Leave critical section */
  219. rt_hw_interrupt_enable(level);
  220. }
  221. void nu_sys_ipclk_enable(E_SYS_IPCLK eIPClkIdx)
  222. {
  223. _nu_sys_ipclk(eIPClkIdx, 1);
  224. }
  225. void nu_sys_ipclk_disable(E_SYS_IPCLK eIPClkIdx)
  226. {
  227. _nu_sys_ipclk(eIPClkIdx, 0);
  228. }
  229. E_SYS_USB0_ID nu_sys_usb0_role(void)
  230. {
  231. /* Check Role on USB0 dual-role port. */
  232. /*
  233. [17] USB0_IDS
  234. USB0_ID Status
  235. 0 = USB port 0 used as a USB device port.
  236. 1 = USB port 0 used as a USB host port.
  237. */
  238. return ((inpw(REG_SYS_MISCISR) & (1 << 17)) > 0) ? USB0_ID_HOST : USB0_ID_DEVICE;
  239. }
  240. #ifdef RT_USING_FINSH
  241. #include <finsh.h>
  242. FINSH_FUNCTION_EXPORT_ALIAS(rt_hw_cpu_reset, reset, restart the system);
  243. #ifdef FINSH_USING_MSH
  244. int cmd_reset(int argc, char **argv)
  245. {
  246. rt_hw_cpu_reset();
  247. return 0;
  248. }
  249. MSH_CMD_EXPORT_ALIAS(cmd_reset, reset, restart the system);
  250. int cmd_shutdown(int argc, char **argv)
  251. {
  252. rt_hw_cpu_shutdown();
  253. return 0;
  254. }
  255. MSH_CMD_EXPORT_ALIAS(cmd_shutdown, shutdown, shutdown the system);
  256. int nu_clocks(int argc, char **argv)
  257. {
  258. rt_kprintf("SYS_UPLL = %d MHz\n", sysGetClock(SYS_UPLL));
  259. rt_kprintf("SYS_APLL = %d MHz\n", sysGetClock(SYS_APLL));
  260. rt_kprintf("SYS_SYSTEM = %d MHz\n", sysGetClock(SYS_SYSTEM));
  261. rt_kprintf("SYS_HCLK = %d MHz\n", sysGetClock(SYS_HCLK));
  262. rt_kprintf("SYS_PCLK01 = %d MHz\n", sysGetClock(SYS_PCLK01));
  263. rt_kprintf("SYS_PCLK2 = %d MHz\n", sysGetClock(SYS_PCLK2));
  264. rt_kprintf("SYS_CPU = %d MHz\n", sysGetClock(SYS_CPU));
  265. rt_kprintf("CLK_HCLKEN = %08X\n", inpw(REG_CLK_HCLKEN));
  266. rt_kprintf("CLK_PCLKEN0 = %08X\n", inpw(REG_CLK_PCLKEN0));
  267. rt_kprintf("CLK_PCLKEN1 = %08X\n", inpw(REG_CLK_PCLKEN1));
  268. rt_kprintf("AIC_INTMSK0 = %08X\n", inpw(REG_AIC_INTMSK0));
  269. rt_kprintf("AIC_INTMSK1 = %08X\n", inpw(REG_AIC_INTMSK1));
  270. rt_kprintf("AIC_INTEN0 = %08X\n", inpw(REG_AIC_INTEN0));
  271. rt_kprintf("AIC_INTEN1 = %08X\n", inpw(REG_AIC_INTEN1));
  272. rt_kprintf("AIC_INTDIS0 = %08X\n", inpw(REG_AIC_INTDIS0));
  273. rt_kprintf("AIC_INTDIS1 = %08X\n", inpw(REG_AIC_INTDIS1));
  274. return 0;
  275. }
  276. MSH_CMD_EXPORT(nu_clocks, get all system clocks);
  277. #endif
  278. #ifdef RT_USING_INTERRUPT_INFO
  279. int list_interrupt(int argc, char **argv)
  280. {
  281. int i;
  282. for (i = 1; i <= SYS_MAX_INT_SOURCE; i++)
  283. {
  284. if (irq_desc[i].handler != rt_hw_interrupt_dummy_handler)
  285. {
  286. rt_kprintf("[%d] %s: %d\n", i, irq_desc[i].name, irq_desc[i].counter);
  287. }
  288. }
  289. return 0;
  290. }
  291. MSH_CMD_EXPORT(list_interrupt, list registered interrupts);
  292. #endif
  293. #endif