board.c 6.6 KB

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