board.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #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 (0x24000000)
  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. static struct mem_desc at91_mem_desc[] = {
  50. { 0x00000000, 0xFFFFFFFF, 0x00000000, RW_NCNB }, /* None cached for 4G memory */
  51. { 0x20000000, 0x24000000-1, 0x20000000, RW_CB }, /* 64M cached SDRAM memory */
  52. { 0x00000000, 0x100000, 0x20000000, RW_CB }, /* isr vector table */
  53. { 0x90000000, 0x90400000 - 1, 0x00200000, RW_NCNB }, /* 4K SRAM0 + 4k SRAM1 */
  54. { 0xA0000000, 0xA4000000-1, 0x20000000, RW_NCNB } /* 64M none-cached SDRAM memory */
  55. };
  56. #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
  57. #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
  58. static rt_uint32_t pit_cycle; /* write-once */
  59. static rt_uint32_t pit_cnt; /* access only w/system irq blocked */
  60. /**
  61. * This function will handle rtos timer
  62. */
  63. void rt_timer_handler(int vector, void *param)
  64. {
  65. #ifdef RT_USING_DBGU
  66. if (at91_sys_read(AT91_DBGU + AT91_US_CSR) & 0x1)
  67. {
  68. rt_dbgu_isr();
  69. }
  70. #endif
  71. if (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS)
  72. {
  73. unsigned nr_ticks;
  74. /* Get number of ticks performed before irq, and ack it */
  75. nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  76. rt_tick_increase();
  77. }
  78. }
  79. static void at91sam926x_pit_reset(void)
  80. {
  81. /* Disable timer and irqs */
  82. at91_sys_write(AT91_PIT_MR, 0);
  83. /* Clear any pending interrupts, wait for PIT to stop counting */
  84. while (PIT_CPIV(at91_sys_read(AT91_PIT_PIVR)) != 0)
  85. ;
  86. /* Start PIT but don't enable IRQ */
  87. //at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
  88. pit_cnt += pit_cycle * PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  89. at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN
  90. | AT91_PIT_PITIEN);
  91. rt_kprintf("PIT_MR=0x%08x\n", at91_sys_read(AT91_PIT_MR));
  92. }
  93. /*
  94. * Set up both clocksource and clockevent support.
  95. */
  96. static void at91sam926x_pit_init(void)
  97. {
  98. rt_uint32_t pit_rate;
  99. rt_uint32_t bits;
  100. /*
  101. * Use our actual MCK to figure out how many MCK/16 ticks per
  102. * 1/HZ period (instead of a compile-time constant LATCH).
  103. */
  104. pit_rate = clk_get_rate(clk_get("mck")) / 16;
  105. rt_kprintf("pit_rate=%dHZ\n", pit_rate);
  106. pit_cycle = (pit_rate + RT_TICK_PER_SECOND/2) / RT_TICK_PER_SECOND;
  107. /* Initialize and enable the timer */
  108. at91sam926x_pit_reset();
  109. }
  110. /**
  111. * This function will init pit for system ticks
  112. */
  113. void rt_hw_timer_init()
  114. {
  115. at91sam926x_pit_init();
  116. /* install interrupt handler */
  117. rt_hw_interrupt_install(AT91_ID_SYS, rt_timer_handler,
  118. RT_NULL, "system");
  119. rt_hw_interrupt_umask(AT91_ID_SYS);
  120. }
  121. void at91_tc1_init()
  122. {
  123. at91_sys_write(AT91_PMC_PCER, 1<<AT91SAM9260_ID_TC0);
  124. writel(AT91_TC_TC0XC0S_NONE | AT91_TC_TC1XC1S_NONE | AT91_TC_TC2XC2S_NONE, AT91SAM9260_BASE_TCB0 + AT91_TC_BMR);
  125. writel(AT91_TC_CLKDIS, AT91SAM9260_BASE_TC0 + AT91_TC_CCR);
  126. writel(AT91_TC_TIMER_CLOCK4, AT91SAM9260_BASE_TC0 + AT91_TC_CMR);
  127. writel(0xffff, AT91SAM9260_BASE_TC0 + AT91_TC_CV);
  128. }
  129. #define RXRDY 0x01
  130. #define TXRDY (1 << 1)
  131. #define BPS 115200 /* serial baudrate */
  132. typedef struct uartport
  133. {
  134. volatile rt_uint32_t CR;
  135. volatile rt_uint32_t MR;
  136. volatile rt_uint32_t IER;
  137. volatile rt_uint32_t IDR;
  138. volatile rt_uint32_t IMR;
  139. volatile rt_uint32_t CSR;
  140. volatile rt_uint32_t RHR;
  141. volatile rt_uint32_t THR;
  142. volatile rt_uint32_t BRGR;
  143. volatile rt_uint32_t RTOR;
  144. volatile rt_uint32_t TTGR;
  145. volatile rt_uint32_t reserved0[5];
  146. volatile rt_uint32_t FIDI;
  147. volatile rt_uint32_t NER;
  148. volatile rt_uint32_t reserved1;
  149. volatile rt_uint32_t IFR;
  150. volatile rt_uint32_t reserved2[44];
  151. volatile rt_uint32_t RPR;
  152. volatile rt_uint32_t RCR;
  153. volatile rt_uint32_t TPR;
  154. volatile rt_uint32_t TCR;
  155. volatile rt_uint32_t RNPR;
  156. volatile rt_uint32_t RNCR;
  157. volatile rt_uint32_t TNPR;
  158. volatile rt_uint32_t TNCR;
  159. volatile rt_uint32_t PTCR;
  160. volatile rt_uint32_t PTSR;
  161. }uartport;
  162. #define CIDR FIDI
  163. #define EXID NER
  164. #define FNR reserved1
  165. #define DBGU ((struct uartport *)AT91SAM9260_BASE_DBGU)
  166. static void at91_usart_putc(char c)
  167. {
  168. while (!(DBGU->CSR & TXRDY));
  169. DBGU->THR = c;
  170. }
  171. /**
  172. * This function is used to display a string on console, normally, it's
  173. * invoked by rt_kprintf
  174. *
  175. * @param str the displayed string
  176. */
  177. void rt_hw_console_output(const char* str)
  178. {
  179. while (*str)
  180. {
  181. if (*str=='\n')
  182. {
  183. at91_usart_putc('\r');
  184. }
  185. at91_usart_putc(*str++);
  186. }
  187. }
  188. static void rt_hw_console_init(void)
  189. {
  190. int div;
  191. int mode = 0;
  192. DBGU->CR = AT91_US_RSTTX | AT91_US_RSTRX |
  193. AT91_US_RXDIS | AT91_US_TXDIS;
  194. mode |= AT91_US_USMODE_NORMAL | AT91_US_USCLKS_MCK |
  195. AT91_US_CHMODE_NORMAL;
  196. mode |= AT91_US_CHRL_8;
  197. mode |= AT91_US_NBSTOP_1;
  198. mode |= AT91_US_PAR_NONE;
  199. DBGU->MR = mode;
  200. div = (clk_get_rate(clk_get("mck")) / 16 + BPS/2) / BPS;
  201. DBGU->BRGR = div;
  202. DBGU->CR = AT91_US_RXEN | AT91_US_TXEN;
  203. }
  204. /**
  205. * This function will init at91sam9260 board
  206. */
  207. void rt_hw_board_init()
  208. {
  209. /* initialize the system clock */
  210. rt_hw_clock_init();
  211. /* initialize console */
  212. rt_hw_console_init();
  213. /* initialize mmu */
  214. rt_hw_mmu_init(at91_mem_desc, sizeof(at91_mem_desc)/sizeof(at91_mem_desc[0]));
  215. /* initialize hardware interrupt */
  216. rt_hw_interrupt_init();
  217. /* initialize early device */
  218. #ifdef RT_USING_COMPONENTS_INIT
  219. rt_components_board_init();
  220. #endif
  221. #ifdef RT_USING_CONSOLE
  222. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  223. #endif
  224. /* initialize timer0 */
  225. rt_hw_timer_init();
  226. /* initialize board */
  227. #ifdef RT_USING_HEAP
  228. rt_system_heap_init((void *)HEAP_BEGIN, (void *)HEAP_END);
  229. #endif
  230. }
  231. /*@}*/