board.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009 RT-Thread Develop Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2011-01-13 weety first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include "board.h"
  27. #include <mmu.h>
  28. /**
  29. * @addtogroup at91sam9260
  30. */
  31. /*@{*/
  32. extern void rt_hw_interrupt_init(void);
  33. extern void rt_hw_clock_init(void);
  34. extern void rt_hw_get_clock(void);
  35. extern void rt_hw_set_dividor(rt_uint8_t hdivn, rt_uint8_t pdivn);
  36. extern void rt_hw_set_clock(rt_uint8_t sdiv, rt_uint8_t pdiv, rt_uint8_t mdiv);
  37. extern void rt_dbgu_isr(void);
  38. static struct mem_desc at91_mem_desc[] = {
  39. { 0x00000000, 0xFFFFFFFF, 0x00000000, RW_NCNB }, /* None cached for 4G memory */
  40. { 0x20000000, 0x24000000-1, 0x20000000, RW_CB }, /* 64M cached SDRAM memory */
  41. { 0x00000000, 0x100000, 0x20000000, RW_CB }, /* isr vector table */
  42. { 0x90000000, 0x90400000 - 1, 0x00200000, RW_NCNB }, /* 4K SRAM0 + 4k SRAM1 */
  43. { 0xA0000000, 0xA4000000-1, 0x20000000, RW_NCNB } /* 64M none-cached SDRAM memory */
  44. };
  45. #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
  46. #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
  47. static rt_uint32_t pit_cycle; /* write-once */
  48. static rt_uint32_t pit_cnt; /* access only w/system irq blocked */
  49. /**
  50. * This function will handle rtos timer
  51. */
  52. void rt_timer_handler(int vector, void *param)
  53. {
  54. #ifdef RT_USING_DBGU
  55. if (at91_sys_read(AT91_DBGU + AT91_US_CSR) & 0x1)
  56. {
  57. rt_dbgu_isr();
  58. }
  59. #endif
  60. if (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS)
  61. {
  62. unsigned nr_ticks;
  63. /* Get number of ticks performed before irq, and ack it */
  64. nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  65. rt_tick_increase();
  66. }
  67. }
  68. static void at91sam926x_pit_reset(void)
  69. {
  70. /* Disable timer and irqs */
  71. at91_sys_write(AT91_PIT_MR, 0);
  72. /* Clear any pending interrupts, wait for PIT to stop counting */
  73. while (PIT_CPIV(at91_sys_read(AT91_PIT_PIVR)) != 0)
  74. ;
  75. /* Start PIT but don't enable IRQ */
  76. //at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
  77. pit_cnt += pit_cycle * PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  78. at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN
  79. | AT91_PIT_PITIEN);
  80. rt_kprintf("PIT_MR=0x%08x\n", at91_sys_read(AT91_PIT_MR));
  81. }
  82. /*
  83. * Set up both clocksource and clockevent support.
  84. */
  85. static void at91sam926x_pit_init(void)
  86. {
  87. rt_uint32_t pit_rate;
  88. rt_uint32_t bits;
  89. /*
  90. * Use our actual MCK to figure out how many MCK/16 ticks per
  91. * 1/HZ period (instead of a compile-time constant LATCH).
  92. */
  93. pit_rate = clk_get_rate(clk_get("mck")) / 16;
  94. rt_kprintf("pit_rate=%dHZ\n", pit_rate);
  95. pit_cycle = (pit_rate + RT_TICK_PER_SECOND/2) / RT_TICK_PER_SECOND;
  96. /* Initialize and enable the timer */
  97. at91sam926x_pit_reset();
  98. }
  99. /**
  100. * This function will init pit for system ticks
  101. */
  102. void rt_hw_timer_init()
  103. {
  104. at91sam926x_pit_init();
  105. /* install interrupt handler */
  106. rt_hw_interrupt_install(AT91_ID_SYS, rt_timer_handler,
  107. RT_NULL, "system");
  108. rt_hw_interrupt_umask(AT91_ID_SYS);
  109. }
  110. void at91_tc1_init()
  111. {
  112. at91_sys_write(AT91_PMC_PCER, 1<<AT91SAM9260_ID_TC0);
  113. writel(AT91_TC_TC0XC0S_NONE | AT91_TC_TC1XC1S_NONE | AT91_TC_TC2XC2S_NONE, AT91SAM9260_BASE_TCB0 + AT91_TC_BMR);
  114. writel(AT91_TC_CLKDIS, AT91SAM9260_BASE_TC0 + AT91_TC_CCR);
  115. writel(AT91_TC_TIMER_CLOCK4, AT91SAM9260_BASE_TC0 + AT91_TC_CMR);
  116. writel(0xffff, AT91SAM9260_BASE_TC0 + AT91_TC_CV);
  117. }
  118. /**
  119. * This function will init at91sam9260 board
  120. */
  121. void rt_hw_board_init()
  122. {
  123. /* initialize mmu */
  124. rt_hw_mmu_init(at91_mem_desc, sizeof(at91_mem_desc)/sizeof(at91_mem_desc[0]));
  125. /* initialize hardware interrupt */
  126. rt_hw_interrupt_init();
  127. /* initialize the system clock */
  128. rt_hw_clock_init();
  129. /* initialize uart */
  130. rt_hw_uart_init();
  131. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  132. /* initialize timer0 */
  133. rt_hw_timer_init();
  134. }
  135. /*@}*/