board.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_clock_init(void);
  33. extern void rt_hw_get_clock(void);
  34. extern void rt_hw_set_dividor(rt_uint8_t hdivn, rt_uint8_t pdivn);
  35. extern void rt_hw_set_clock(rt_uint8_t sdiv, rt_uint8_t pdiv, rt_uint8_t mdiv);
  36. extern void rt_dbgu_isr(void);
  37. static struct mem_desc at91_mem_desc[] = {
  38. { 0x00000000, 0xFFFFFFFF, 0x00000000, RW_NCNB }, /* None cached for 4G memory */
  39. { 0x20000000, 0x24000000-1, 0x20000000, RW_CB }, /* 64M cached SDRAM memory */
  40. { 0x00000000, 0x100000, 0x20000000, RW_CB }, /* isr vector table */
  41. { 0x90000000, 0x90400000 - 1, 0x00200000, RW_NCNB }, /* 4K SRAM0 + 4k SRAM1 */
  42. { 0xA0000000, 0xA4000000-1, 0x20000000, RW_NCNB } /* 64M none-cached SDRAM memory */
  43. };
  44. #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
  45. #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
  46. static rt_uint32_t pit_cycle; /* write-once */
  47. static rt_uint32_t pit_cnt; /* access only w/system irq blocked */
  48. /**
  49. * This function will handle rtos timer
  50. */
  51. void rt_timer_handler(int vector, void *param)
  52. {
  53. #ifdef RT_USING_DBGU
  54. if (at91_sys_read(AT91_DBGU + AT91_US_CSR) & 0x1)
  55. {
  56. rt_dbgu_isr();
  57. }
  58. #endif
  59. if (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS)
  60. {
  61. unsigned nr_ticks;
  62. /* Get number of ticks performed before irq, and ack it */
  63. nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  64. rt_tick_increase();
  65. }
  66. }
  67. static void at91sam926x_pit_reset(void)
  68. {
  69. /* Disable timer and irqs */
  70. at91_sys_write(AT91_PIT_MR, 0);
  71. /* Clear any pending interrupts, wait for PIT to stop counting */
  72. while (PIT_CPIV(at91_sys_read(AT91_PIT_PIVR)) != 0)
  73. ;
  74. /* Start PIT but don't enable IRQ */
  75. //at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
  76. pit_cnt += pit_cycle * PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  77. at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN
  78. | AT91_PIT_PITIEN);
  79. rt_kprintf("PIT_MR=0x%08x\n", at91_sys_read(AT91_PIT_MR));
  80. }
  81. /*
  82. * Set up both clocksource and clockevent support.
  83. */
  84. static void at91sam926x_pit_init(void)
  85. {
  86. rt_uint32_t pit_rate;
  87. rt_uint32_t bits;
  88. /*
  89. * Use our actual MCK to figure out how many MCK/16 ticks per
  90. * 1/HZ period (instead of a compile-time constant LATCH).
  91. */
  92. pit_rate = clk_get_rate(clk_get("mck")) / 16;
  93. rt_kprintf("pit_rate=%dHZ\n", pit_rate);
  94. pit_cycle = (pit_rate + RT_TICK_PER_SECOND/2) / RT_TICK_PER_SECOND;
  95. /* Initialize and enable the timer */
  96. at91sam926x_pit_reset();
  97. }
  98. /**
  99. * This function will init pit for system ticks
  100. */
  101. void rt_hw_timer_init()
  102. {
  103. at91sam926x_pit_init();
  104. /* install interrupt handler */
  105. rt_hw_interrupt_install(AT91_ID_SYS, rt_timer_handler,
  106. RT_NULL, "system");
  107. rt_hw_interrupt_umask(AT91_ID_SYS);
  108. }
  109. void at91_tc1_init()
  110. {
  111. at91_sys_write(AT91_PMC_PCER, 1<<AT91SAM9260_ID_TC0);
  112. writel(AT91_TC_TC0XC0S_NONE | AT91_TC_TC1XC1S_NONE | AT91_TC_TC2XC2S_NONE, AT91SAM9260_BASE_TCB0 + AT91_TC_BMR);
  113. writel(AT91_TC_CLKDIS, AT91SAM9260_BASE_TC0 + AT91_TC_CCR);
  114. writel(AT91_TC_TIMER_CLOCK4, AT91SAM9260_BASE_TC0 + AT91_TC_CMR);
  115. writel(0xffff, AT91SAM9260_BASE_TC0 + AT91_TC_CV);
  116. }
  117. /**
  118. * This function will init at91sam9260 board
  119. */
  120. void rt_hw_board_init()
  121. {
  122. /* initialize the system clock */
  123. rt_hw_clock_init();
  124. /* initialize uart */
  125. rt_hw_uart_init();
  126. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  127. /* initialize mmu */
  128. rt_hw_mmu_init(at91_mem_desc, sizeof(at91_mem_desc)/sizeof(at91_mem_desc[0]));
  129. /* initialize timer0 */
  130. rt_hw_timer_init();
  131. }
  132. /*@}*/