1
0

board.c 4.8 KB

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