board.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-11-20 Bernard the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <finsh.h>
  13. #include "board.h"
  14. #include <interrupt.h>
  15. #ifdef RT_USING_VMM
  16. #include <vmm.h>
  17. static rt_uint32_t DMTIMER = 0;
  18. #define TIMER_HW_BASE (DMTIMER)
  19. #else
  20. #define TIMER_HW_BASE AM33XX_DMTIMER_7_REGS
  21. #endif
  22. #define DMTIMER_TCLR_AR (0x00000002u)
  23. #define DMTIMER_TCLR_CE (0x00000040u)
  24. #define DMTIMER_TCLR_PRE (0x00000020u)
  25. #define DMTIMER_TCLR_ST (0x00000001u)
  26. #define DMTIMER_IRQENABLE_SET_OVF_EN_FLAG (0x00000002u)
  27. #define DMTIMER_IRQSTATUS_RAW_OVF_IT_FLAG (0x00000002u)
  28. #define CM_DPLL_CLKSEL_CLK_CLKSEL (0x00000003u)
  29. #define CM_DPLL_CLKSEL_CLK_CLKSEL_SEL3 (0x2u)
  30. #define CM_PER_CLKCTRL_MODULEMODE_ENABLE (0x2u)
  31. #define CM_PER_CLKCTRL_MODULEMODE (0x00000003u)
  32. #define CM_PER_CLKCTRL_IDLEST (0x00030000u)
  33. #define CM_PER_CLKCTRL_IDLEST_FUNC (0x0u)
  34. #define CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_L4LS_GCLK (0x00000100u)
  35. #define CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_TIMER2_GCLK (0x00004000u)
  36. #define CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_TIMER7_GCLK (1<<13)
  37. static void rt_hw_timer_isr(int vector, void* param)
  38. {
  39. rt_tick_increase();
  40. DMTIMER_IRQSTATUS(TIMER_HW_BASE) = DMTIMER_IRQSTATUS_RAW_OVF_IT_FLAG;
  41. }
  42. static void timer_clk_init(void)
  43. {
  44. unsigned long prcm_base;
  45. #ifdef RT_USING_VMM
  46. prcm_base = vmm_find_iomap("PRCM");
  47. #else
  48. prcm_base = AM33XX_PRCM_REGS;
  49. #endif
  50. /* software forced wakeup */
  51. CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) |= 0x2;
  52. /* Waiting for the L4LS clock */
  53. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<8)))
  54. ;
  55. /* Select the clock source for the Timer2 instance. */
  56. CM_DPLL_CLKSEL_TIMER7_CLK(prcm_base) &= ~(CM_DPLL_CLKSEL_CLK_CLKSEL);
  57. /* 32k clock source */
  58. CM_DPLL_CLKSEL_TIMER7_CLK(prcm_base) |= CM_DPLL_CLKSEL_CLK_CLKSEL_SEL3;
  59. while ((CM_DPLL_CLKSEL_TIMER7_CLK(prcm_base) & CM_DPLL_CLKSEL_CLK_CLKSEL) !=
  60. CM_DPLL_CLKSEL_CLK_CLKSEL_SEL3);
  61. /* Writing to MODULEMODE field of CM_PER_TIMER7_CLKCTRL register. */
  62. CM_PER_TIMER7_CLKCTRL(prcm_base) |= CM_PER_CLKCTRL_MODULEMODE_ENABLE;
  63. /* Waiting for MODULEMODE field to reflect the written value. */
  64. while ((CM_PER_TIMER7_CLKCTRL(prcm_base) & CM_PER_CLKCTRL_MODULEMODE) !=
  65. CM_PER_CLKCTRL_MODULEMODE_ENABLE);
  66. /*
  67. * Waiting for IDLEST field in CM_PER_TIMER7_CLKCTRL register
  68. * for the module is fully functional.
  69. */
  70. while ((CM_PER_TIMER7_CLKCTRL(prcm_base) & CM_PER_CLKCTRL_IDLEST) !=
  71. CM_PER_CLKCTRL_IDLEST_FUNC);
  72. /* Waiting for the L4LS clock */
  73. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_L4LS_GCLK));
  74. /* Waiting for the TIMER7 clock */
  75. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_TIMER7_GCLK));
  76. }
  77. int rt_hw_timer_init(void)
  78. {
  79. rt_uint32_t counter;
  80. #ifdef RT_USING_VMM
  81. DMTIMER = vmm_find_iomap("TIMER7");
  82. #endif
  83. timer_clk_init();
  84. /* soft reset the timer */
  85. DMTIMER_TIOCP_CFG(TIMER_HW_BASE) |= 1;
  86. while ((DMTIMER_TIOCP_CFG(TIMER_HW_BASE) & 0x1) == 1)
  87. ;
  88. /* calculate count */
  89. counter = 0xffffffff - (32768UL/RT_TICK_PER_SECOND);
  90. /* set initial count */
  91. DMTIMER_TCRR(TIMER_HW_BASE) = counter;
  92. /* set reload count */
  93. DMTIMER_TLDR(TIMER_HW_BASE) = counter;
  94. /* set mode: auto reload */
  95. DMTIMER_TCLR(TIMER_HW_BASE) |= DMTIMER_TCLR_AR;
  96. /* interrupt enable for match */
  97. DMTIMER_IRQENABLE_SET(TIMER_HW_BASE) = DMTIMER_IRQENABLE_SET_OVF_EN_FLAG;
  98. DMTIMER_IRQSTATUS(TIMER_HW_BASE) = DMTIMER_IRQSTATUS_RAW_OVF_IT_FLAG;
  99. rt_hw_interrupt_install(TINT7, rt_hw_timer_isr, RT_NULL, "tick");
  100. rt_hw_interrupt_control(TINT7, 0, 0);
  101. rt_hw_interrupt_umask(TINT7);
  102. while (DMTIMER_TWPS(TIMER_HW_BASE) != 0)
  103. ;
  104. /* start timer */
  105. DMTIMER_TCLR(TIMER_HW_BASE) |= DMTIMER_TCLR_ST;
  106. while (DMTIMER_TWPS(TIMER_HW_BASE) != 0)
  107. ;
  108. return 0;
  109. }
  110. INIT_BOARD_EXPORT(rt_hw_timer_init);
  111. /**
  112. * This function will initialize beaglebone board
  113. */
  114. void rt_hw_board_init(void)
  115. {
  116. rt_components_board_init();
  117. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  118. }
  119. void rt_hw_cpu_reset(void)
  120. {
  121. unsigned long prcm_base;
  122. #ifdef RT_USING_VMM
  123. prcm_base = vmm_find_iomap("PRCM");
  124. #else
  125. prcm_base = AM33XX_PRCM_REGS;
  126. #endif
  127. REG32(PRM_DEVICE(prcm_base)) = 0x1;
  128. RT_ASSERT(0);
  129. }
  130. MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reboot the cpu);