board.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 <mmu.h>
  15. #include <interrupt.h>
  16. #define TIMER_HW_BASE AM33XX_DMTIMER_7_REGS
  17. #define DMTIMER_TCLR_AR (0x00000002u)
  18. #define DMTIMER_TCLR_CE (0x00000040u)
  19. #define DMTIMER_TCLR_PRE (0x00000020u)
  20. #define DMTIMER_TCLR_ST (0x00000001u)
  21. #define DMTIMER_IRQENABLE_SET_OVF_EN_FLAG (0x00000002u)
  22. #define DMTIMER_IRQSTATUS_RAW_OVF_IT_FLAG (0x00000002u)
  23. #define CM_DPLL_CLKSEL_CLK_CLKSEL (0x00000003u)
  24. #define CM_DPLL_CLKSEL_CLK_CLKSEL_SEL3 (0x2u)
  25. #define CM_PER_CLKCTRL_MODULEMODE_ENABLE (0x2u)
  26. #define CM_PER_CLKCTRL_MODULEMODE (0x00000003u)
  27. #define CM_PER_CLKCTRL_IDLEST (0x00030000u)
  28. #define CM_PER_CLKCTRL_IDLEST_FUNC (0x0u)
  29. #define CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_L4LS_GCLK (0x00000100u)
  30. #define CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_TIMER2_GCLK (0x00004000u)
  31. #define CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_TIMER7_GCLK (1<<13)
  32. static void rt_hw_timer_isr(int vector, void* param)
  33. {
  34. rt_tick_increase();
  35. DMTIMER_IRQSTATUS(TIMER_HW_BASE) = DMTIMER_IRQSTATUS_RAW_OVF_IT_FLAG;
  36. }
  37. static void timer_clk_init(void)
  38. {
  39. unsigned long prcm_base;
  40. prcm_base = AM33XX_PRCM_REGS;
  41. /* software forced wakeup */
  42. CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) |= 0x2;
  43. /* Waiting for the L4LS clock */
  44. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & (1<<8)))
  45. ;
  46. /* Select the clock source for the Timer2 instance. */
  47. CM_DPLL_CLKSEL_TIMER7_CLK(prcm_base) &= ~(CM_DPLL_CLKSEL_CLK_CLKSEL);
  48. /* 32k clock source */
  49. CM_DPLL_CLKSEL_TIMER7_CLK(prcm_base) |= CM_DPLL_CLKSEL_CLK_CLKSEL_SEL3;
  50. while ((CM_DPLL_CLKSEL_TIMER7_CLK(prcm_base) & CM_DPLL_CLKSEL_CLK_CLKSEL) !=
  51. CM_DPLL_CLKSEL_CLK_CLKSEL_SEL3);
  52. /* Writing to MODULEMODE field of CM_PER_TIMER7_CLKCTRL register. */
  53. CM_PER_TIMER7_CLKCTRL(prcm_base) |= CM_PER_CLKCTRL_MODULEMODE_ENABLE;
  54. /* Waiting for MODULEMODE field to reflect the written value. */
  55. while ((CM_PER_TIMER7_CLKCTRL(prcm_base) & CM_PER_CLKCTRL_MODULEMODE) !=
  56. CM_PER_CLKCTRL_MODULEMODE_ENABLE);
  57. /*
  58. * Waiting for IDLEST field in CM_PER_TIMER7_CLKCTRL register
  59. * for the module is fully functional.
  60. */
  61. while ((CM_PER_TIMER7_CLKCTRL(prcm_base) & CM_PER_CLKCTRL_IDLEST) !=
  62. CM_PER_CLKCTRL_IDLEST_FUNC);
  63. /* Waiting for the L4LS clock */
  64. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_L4LS_GCLK));
  65. /* Waiting for the TIMER7 clock */
  66. while (!(CM_PER_L4LS_CLKSTCTRL_REG(prcm_base) & CM_PER_L4LS_CLKSTCTRL_CLKACTIVITY_TIMER7_GCLK));
  67. }
  68. int rt_hw_timer_init(void)
  69. {
  70. rt_uint32_t counter;
  71. timer_clk_init();
  72. /* soft reset the timer */
  73. DMTIMER_TIOCP_CFG(TIMER_HW_BASE) |= 1;
  74. while ((DMTIMER_TIOCP_CFG(TIMER_HW_BASE) & 0x1) == 1)
  75. ;
  76. /* calculate count */
  77. counter = 0xffffffff - (32768UL/RT_TICK_PER_SECOND);
  78. /* set initial count */
  79. DMTIMER_TCRR(TIMER_HW_BASE) = counter;
  80. /* set reload count */
  81. DMTIMER_TLDR(TIMER_HW_BASE) = counter;
  82. /* set mode: auto reload */
  83. DMTIMER_TCLR(TIMER_HW_BASE) |= DMTIMER_TCLR_AR;
  84. /* interrupt enable for match */
  85. DMTIMER_IRQENABLE_SET(TIMER_HW_BASE) = DMTIMER_IRQENABLE_SET_OVF_EN_FLAG;
  86. DMTIMER_IRQSTATUS(TIMER_HW_BASE) = DMTIMER_IRQSTATUS_RAW_OVF_IT_FLAG;
  87. rt_hw_interrupt_install(TINT7, rt_hw_timer_isr, RT_NULL, "tick");
  88. rt_hw_interrupt_control(TINT7, 0, 0);
  89. rt_hw_interrupt_umask(TINT7);
  90. while (DMTIMER_TWPS(TIMER_HW_BASE) != 0)
  91. ;
  92. /* start timer */
  93. DMTIMER_TCLR(TIMER_HW_BASE) |= DMTIMER_TCLR_ST;
  94. while (DMTIMER_TWPS(TIMER_HW_BASE) != 0)
  95. ;
  96. return 0;
  97. }
  98. INIT_BOARD_EXPORT(rt_hw_timer_init);
  99. /**
  100. * This function will initialize beaglebone board
  101. */
  102. void rt_hw_board_init(void)
  103. {
  104. rt_hw_mmu_init();
  105. /* init hardware interrupt */
  106. rt_hw_interrupt_init();
  107. /* Heap initialization */
  108. #if defined(RT_USING_HEAP)
  109. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  110. #endif
  111. rt_components_board_init();
  112. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  113. }
  114. void rt_hw_cpu_reset(void)
  115. {
  116. unsigned long prcm_base = AM33XX_PRCM_REGS;
  117. REG32(PRM_DEVICE(prcm_base)) = 0x1;
  118. RT_ASSERT(0);
  119. }
  120. MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reboot, reboot the cpu);