cpu_init.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Initializes CPU and basic hardware such as memory
  2. * controllers, IRQ controller and system timer 0.
  3. *
  4. * (C) Copyright 2007
  5. * Daniel Hellstrom, Gaisler Research, daniel@gaisler.com
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <asm/asi.h>
  11. #include <asm/leon.h>
  12. #include <ambapp.h>
  13. #include <config.h>
  14. #define TIMER_BASE_CLK 1000000
  15. #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
  16. DECLARE_GLOBAL_DATA_PTR;
  17. /* reset CPU (jump to 0, without reset) */
  18. void start(void);
  19. /* find & initialize the memory controller */
  20. int init_memory_ctrl(void);
  21. ambapp_dev_irqmp *irqmp = NULL;
  22. ambapp_dev_mctrl memctrl;
  23. ambapp_dev_gptimer *gptimer = NULL;
  24. unsigned int gptimer_irq = 0;
  25. int leon3_snooping_avail = 0;
  26. struct {
  27. gd_t gd_area;
  28. bd_t bd;
  29. } global_data;
  30. /*
  31. * Breath some life into the CPU...
  32. *
  33. * Set up the memory map,
  34. * initialize a bunch of registers.
  35. *
  36. * Run from FLASH/PROM:
  37. * - until memory controller is set up, only registers available
  38. * - no global variables available for writing
  39. * - constants available
  40. */
  41. void cpu_init_f(void)
  42. {
  43. /* these varaiable must not be initialized */
  44. ambapp_dev_irqmp *irqmp;
  45. ambapp_apbdev apbdev;
  46. register unsigned int apbmst;
  47. /* find AMBA APB Master */
  48. apbmst = (unsigned int)
  49. ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_APBMST, 1, 0);
  50. if (!apbmst) {
  51. /*
  52. * no AHB/APB bridge, something is wrong
  53. * ==> jump to start (or hang)
  54. */
  55. while (1) ;
  56. }
  57. /* Init memory controller */
  58. if (init_memory_ctrl()) {
  59. while (1) ;
  60. }
  61. /****************************************************
  62. * From here we can use the main memory and the stack.
  63. */
  64. /* Find AMBA APB IRQMP Controller */
  65. if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_IRQMP, &apbdev) != 1) {
  66. /* no IRQ controller, something is wrong
  67. * ==> jump to start (or hang)
  68. */
  69. while (1) ;
  70. }
  71. irqmp = (ambapp_dev_irqmp *) apbdev.address;
  72. /* initialize the IRQMP */
  73. irqmp->ilevel = 0xf; /* all IRQ off */
  74. irqmp->iforce = 0;
  75. irqmp->ipend = 0;
  76. irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
  77. irqmp->cpu_mask[0] = 0; /* mask all IRQs on CPU 0 */
  78. irqmp->cpu_force[0] = 0; /* no force IRQ on CPU 0 */
  79. /* cache */
  80. }
  81. void cpu_init_f2(void)
  82. {
  83. }
  84. /*
  85. * initialize higher level parts of CPU like time base and timers
  86. */
  87. int cpu_init_r(void)
  88. {
  89. ambapp_apbdev apbdev;
  90. /*
  91. * Find AMBA APB IRQMP Controller,
  92. * When we come so far we know there is a IRQMP available
  93. */
  94. ambapp_apb_first(VENDOR_GAISLER, GAISLER_IRQMP, &apbdev);
  95. irqmp = (ambapp_dev_irqmp *) apbdev.address;
  96. /* timer */
  97. if (ambapp_apb_first(VENDOR_GAISLER, GAISLER_GPTIMER, &apbdev) != 1) {
  98. printf("cpu_init_r: gptimer not found!\n");
  99. return 1;
  100. }
  101. gptimer = (ambapp_dev_gptimer *) apbdev.address;
  102. gptimer_irq = apbdev.irq;
  103. /* initialize prescaler common to all timers to 1MHz */
  104. gptimer->scalar = gptimer->scalar_reload =
  105. (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
  106. return (0);
  107. }
  108. /* find & setup memory controller */
  109. int init_memory_ctrl()
  110. {
  111. register ambapp_dev_mctrl *mctrl;
  112. register ambapp_dev_sdctrl *sdctrl;
  113. register ambapp_dev_ddrspa *ddrspa;
  114. register ambapp_dev_ddr2spa *ddr2spa;
  115. register ahbctrl_pp_dev *ahb;
  116. register unsigned int base;
  117. register int not_found_mctrl = -1;
  118. /* find ESA Memory controller */
  119. base = ambapp_apb_next_nomem(VENDOR_ESA, ESA_MCTRL, 0);
  120. if (base) {
  121. mctrl = (ambapp_dev_mctrl *) base;
  122. /* config MCTRL memory controller */
  123. mctrl->mcfg1 = CONFIG_SYS_GRLIB_MEMCFG1 | (mctrl->mcfg1 & 0x300);
  124. mctrl->mcfg2 = CONFIG_SYS_GRLIB_MEMCFG2;
  125. mctrl->mcfg3 = CONFIG_SYS_GRLIB_MEMCFG3;
  126. not_found_mctrl = 0;
  127. }
  128. /* find Gaisler Fault Tolerant Memory controller */
  129. base = ambapp_apb_next_nomem(VENDOR_GAISLER, GAISLER_FTMCTRL, 0);
  130. if (base) {
  131. mctrl = (ambapp_dev_mctrl *) base;
  132. /* config MCTRL memory controller */
  133. mctrl->mcfg1 = CONFIG_SYS_GRLIB_FT_MEMCFG1 | (mctrl->mcfg1 & 0x300);
  134. mctrl->mcfg2 = CONFIG_SYS_GRLIB_FT_MEMCFG2;
  135. mctrl->mcfg3 = CONFIG_SYS_GRLIB_FT_MEMCFG3;
  136. not_found_mctrl = 0;
  137. }
  138. /* find SDRAM controller */
  139. base = ambapp_apb_next_nomem(VENDOR_GAISLER, GAISLER_SDCTRL, 0);
  140. if (base) {
  141. sdctrl = (ambapp_dev_sdctrl *) base;
  142. /* config memory controller */
  143. sdctrl->sdcfg = CONFIG_SYS_GRLIB_SDRAM;
  144. not_found_mctrl = 0;
  145. }
  146. ahb = ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_DDR2SPA, 1, 0);
  147. if (ahb) {
  148. ddr2spa = (ambapp_dev_ddr2spa *) ambapp_ahb_get_info(ahb, 1);
  149. /* Config DDR2 memory controller */
  150. ddr2spa->cfg1 = CONFIG_SYS_GRLIB_DDR2_CFG1;
  151. ddr2spa->cfg3 = CONFIG_SYS_GRLIB_DDR2_CFG3;
  152. not_found_mctrl = 0;
  153. }
  154. ahb = ambapp_ahb_next_nomem(VENDOR_GAISLER, GAISLER_DDRSPA, 1, 0);
  155. if (ahb) {
  156. ddrspa = (ambapp_dev_ddrspa *) ambapp_ahb_get_info(ahb, 1);
  157. /* Config DDR memory controller */
  158. ddrspa->ctrl = CONFIG_SYS_GRLIB_DDR_CFG;
  159. not_found_mctrl = 0;
  160. }
  161. /* failed to find any memory controller */
  162. return not_found_mctrl;
  163. }
  164. /* Uses Timer 0 to get accurate
  165. * pauses. Max 2 raised to 32 ticks
  166. *
  167. */
  168. void cpu_wait_ticks(unsigned long ticks)
  169. {
  170. unsigned long start = get_timer(0);
  171. while (get_timer(start) < ticks) ;
  172. }
  173. /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
  174. * Return irq number for timer int or a negative number for
  175. * dealing with self
  176. */
  177. int timer_interrupt_init_cpu(void)
  178. {
  179. /* SYS_HZ ticks per second */
  180. gptimer->e[0].val = 0;
  181. gptimer->e[0].rld = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
  182. gptimer->e[0].ctrl =
  183. (LEON3_GPTIMER_EN |
  184. LEON3_GPTIMER_RL | LEON3_GPTIMER_LD | LEON3_GPTIMER_IRQEN);
  185. return gptimer_irq;
  186. }
  187. ulong get_tbclk(void)
  188. {
  189. return TIMER_BASE_CLK;
  190. }
  191. /*
  192. * This function is intended for SHORT delays only.
  193. */
  194. unsigned long cpu_usec2ticks(unsigned long usec)
  195. {
  196. if (usec < US_PER_TICK)
  197. return 1;
  198. return usec / US_PER_TICK;
  199. }
  200. unsigned long cpu_ticks2usec(unsigned long ticks)
  201. {
  202. return ticks * US_PER_TICK;
  203. }