board.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 at91sam9g45
  30. */
  31. /*@{*/
  32. #if defined(__CC_ARM)
  33. extern int Image$$ER_ZI$$ZI$$Limit;
  34. #define HEAP_BEGIN (&Image$$ER_ZI$$ZI$$Limit)
  35. #elif (defined (__GNUC__))
  36. extern unsigned char __bss_end;
  37. #define HEAP_BEGIN (&__bss_end)
  38. #elif (defined (__ICCARM__))
  39. #pragma section=".noinit"
  40. #define HEAP_BEGIN (__section_end(".noinit"))
  41. #endif
  42. #define HEAP_END (((rt_uint32_t)HEAP_BEGIN & 0xF0000000) + 0x04000000)
  43. extern void rt_hw_interrupt_init(void);
  44. extern void rt_hw_clock_init(void);
  45. extern void rt_hw_get_clock(void);
  46. extern void rt_hw_set_dividor(rt_uint8_t hdivn, rt_uint8_t pdivn);
  47. extern void rt_hw_set_clock(rt_uint8_t sdiv, rt_uint8_t pdiv, rt_uint8_t mdiv);
  48. extern void rt_dbgu_isr(void);
  49. #define SAM9G45_BLOCK_SIZE 0x10000000 // 256M
  50. #define MMU_SECTION_SIZE 0x100000 // 1M
  51. #define PERIPHERALS_ADDR // 1M
  52. #define SECTION_END(sa) ((sa) + MMU_SECTION_SIZE - 1) // sa: start address
  53. #define BLOCK_END(ba) ((ba) + SAM9G45_BLOCK_SIZE - 1) // ba: block address
  54. static struct mem_desc at91_mem_desc[] = { /* FIXME, hornby, to confirm MMU and memory */
  55. { 0x00000000, 0xFFFFFFFF , 0x00000000, RW_NCNB }, /* None cached for 4G memory */
  56. //{ 0x00000000, SECTION_END(0x00000000), 0x00000000, RW_CNB }, /* TLB for ITCM, ITCM map to address zero, 32KB */
  57. //{ 0x00200000, SECTION_END(0x00200000), 0x00200000, RW_CNB }, /* TLB for DTCM, 32KB */
  58. //{ 0x00300000, SECTION_END(0x00300000), 0x00300000, RW_CNB }, /* TLB for internal RAM, 64KB, we use it as global variable area */
  59. //{ 0x00600000, SECTION_END(0x00600000), 0x00600000, RW_NCNB }, /* TLB for UDPHS(DMA) */
  60. //{ 0x00700000, SECTION_END(0x00700000), 0x00700000, RW_NCNB }, /* TLB for UHP OHCI */
  61. //{ 0x00800000, SECTION_END(0x00800000), 0x00800000, RW_NCNB }, /* TLB for UHP EHCI */
  62. //{ 0x30000000, 0x30000000+0x00100000-1, 0x30000000, RW_CB }, /* 1M external SRAM for program code and stack */
  63. //{ 0x40000000, BLOCK_END(0x40000000), 0x40000000, RW_NCNB }, /* 256M for nand-flash controller */
  64. //{ 0x60000000, BLOCK_END(0x60000000), 0x60000000, RW_NCNB }, /* 256M for FPGA */
  65. //{ 0x70000000, 0x70000000+0x08000000-1, 0x70000000, RW_NCNB }, /* 128M for main DDR-SDRAM for print data */
  66. { 0x00000000, SECTION_END(0x00000000), 0x70000000, RW_CB }, /* isr */
  67. { 0x70000000, 0x70000000+0x08000000-1, 0x70000000, RW_CB }, /* 128M for main DDR-SDRAM for print data */
  68. //{ 0xFFF00000, SECTION_END(0xFFF00000), 0xFFF00000, RW_NCNB }, /* Internal Peripherals, 1MB */
  69. };
  70. #define PIT_CPIV(x) ((x) & AT91C_PITC_CPIV)
  71. #define PIT_PICNT(x) (((x) & AT91C_PITC_PICNT) >> 20)
  72. static rt_uint32_t pit_cycle; /* write-once */
  73. static rt_uint32_t pit_cnt; /* access only w/system irq blocked */
  74. /**
  75. * This function will handle rtos timer
  76. */
  77. void rt_timer_handler(int vector, void *param)
  78. {
  79. #ifdef RT_USING_DBGU
  80. if (readl(AT91C_DBGU_CSR) & AT91C_US_RXRDY)
  81. {
  82. rt_dbgu_isr();
  83. }
  84. #endif
  85. if (readl(AT91C_PITC_PISR) & AT91C_PITC_PITS)
  86. {
  87. unsigned nr_ticks;
  88. /* Get number of ticks performed before irq, and ack it */
  89. nr_ticks = PIT_PICNT(readl(AT91C_PITC_PIVR));
  90. while (nr_ticks--)
  91. rt_tick_increase();
  92. }
  93. }
  94. static void at91sam9g45_pit_reset(void)
  95. {
  96. /* Disable timer and irqs */
  97. AT91C_BASE_PITC->PITC_PIMR = 0;
  98. /* Clear any pending interrupts, wait for PIT to stop counting */
  99. while (PIT_CPIV(readl(AT91C_PITC_PIVR)) != 0)
  100. ;
  101. /* Start PIT but don't enable IRQ */
  102. //AT91C_BASE_PITC->PITC_PIMR = (pit_cycle - 1) | AT91C_PITC_PITEN;
  103. pit_cnt += pit_cycle * PIT_PICNT(readl(AT91C_PITC_PIVR));
  104. AT91C_BASE_PITC->PITC_PIMR =
  105. (pit_cycle - 1) | AT91C_PITC_PITEN | AT91C_PITC_PITIEN;
  106. rt_kprintf("PIT_MR=0x%08x\n", readl(AT91C_PITC_PIMR));
  107. }
  108. /*
  109. * Set up both clocksource and clockevent support.
  110. */
  111. static void at91sam9g45_pit_init(void)
  112. {
  113. rt_uint32_t pit_rate;
  114. //rt_uint32_t bits;
  115. /*
  116. * Use our actual MCK to figure out how many MCK/16 ticks per
  117. * 1/HZ period (instead of a compile-time constant LATCH).
  118. */
  119. pit_rate = clk_get_rate(clk_get("mck")) / 16;
  120. rt_kprintf("pit_rate=%dHZ\n", pit_rate);
  121. pit_cycle = (pit_rate + RT_TICK_PER_SECOND/2) / RT_TICK_PER_SECOND;
  122. /* Initialize and enable the timer */
  123. at91sam9g45_pit_reset();
  124. }
  125. /**
  126. * This function will init pit for system ticks
  127. */
  128. void rt_hw_timer_init()
  129. {
  130. at91sam9g45_pit_init();
  131. /* install interrupt handler */
  132. rt_hw_interrupt_install(AT91C_ID_SYS, rt_timer_handler,
  133. RT_NULL, "system");
  134. rt_hw_interrupt_umask(AT91C_ID_SYS);
  135. }
  136. void at91_tc1_init()
  137. {
  138. AT91C_BASE_PMC->PMC_PCER = 1<<AT91C_ID_TC;
  139. writel(AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_NONE | AT91C_TCB_TC2XC2S_NONE, AT91C_TCB0_BMR);
  140. writel(AT91C_TC_CLKDIS, AT91C_TC0_CCR);
  141. writel(AT91C_TC_CLKS_TIMER_DIV4_CLOCK, AT91C_TC0_CMR);
  142. writel(0xffff, AT91C_TC0_CV);
  143. }
  144. #define BPS 115200 /* serial console port baudrate */
  145. static void at91_usart_putc(char c)
  146. {
  147. while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY))
  148. ;
  149. AT91C_BASE_DBGU->DBGU_THR = c;
  150. }
  151. /**
  152. * This function is used to display a string on console, normally, it's
  153. * invoked by rt_kprintf
  154. *
  155. * @param str the displayed string
  156. */
  157. void rt_hw_console_output(const char* str)
  158. {
  159. while (*str)
  160. {
  161. if (*str=='\n')
  162. {
  163. at91_usart_putc('\r');
  164. }
  165. at91_usart_putc(*str++);
  166. }
  167. }
  168. static void rt_hw_console_init(void)
  169. {
  170. int div;
  171. int mode = 0;
  172. AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RSTTX | AT91C_US_RSTRX |
  173. AT91C_US_RXDIS | AT91C_US_TXDIS;
  174. mode |= AT91C_US_USMODE_NORMAL | AT91C_US_CLKS_CLOCK |
  175. AT91C_US_CHMODE_NORMAL;
  176. mode |= AT91C_US_CHRL_8_BITS;
  177. mode |= AT91C_US_NBSTOP_1_BIT;
  178. mode |= AT91C_US_PAR_NONE;
  179. AT91C_BASE_DBGU->DBGU_MR = mode;
  180. div = (clk_get_rate(clk_get("mck")) / 16 + BPS/2) / BPS;
  181. AT91C_BASE_DBGU->DBGU_BRGR = div;
  182. AT91C_BASE_DBGU->DBGU_CR = AT91C_US_RXEN | AT91C_US_TXEN;
  183. }
  184. /**
  185. * This function will init at91sam9g45 board
  186. */
  187. void rt_hw_board_init()
  188. {
  189. /* initialize the system clock */
  190. rt_hw_clock_init();
  191. /* initialize console */
  192. rt_hw_console_init();
  193. /* initialize mmu */
  194. rt_hw_mmu_init(at91_mem_desc, sizeof(at91_mem_desc)/sizeof(at91_mem_desc[0]));
  195. /* initialize hardware interrupt */
  196. rt_hw_interrupt_init();
  197. /* initialize early device */
  198. #ifdef RT_USING_COMPONENTS_INIT
  199. rt_components_board_init();
  200. #endif
  201. #ifdef RT_USING_CONSOLE
  202. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  203. #endif
  204. /* initialize timer0 */
  205. rt_hw_timer_init();
  206. /* initialize board */
  207. #ifdef RT_USING_HEAP
  208. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  209. #endif
  210. }
  211. /*@}*/