cpu_init.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Initializes CPU and basic hardware such as memory
  2. * controllers, IRQ controller and system timer 0.
  3. *
  4. * (C) Copyright 2007, 2015
  5. * Daniel Hellstrom, Cobham Gaisler, 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 <asm/io.h>
  13. #include <ambapp.h>
  14. #include <grlib/irqmp.h>
  15. #include <grlib/gptimer.h>
  16. #include <debug_uart.h>
  17. #include <config.h>
  18. /* Default Plug&Play I/O area */
  19. #ifndef CONFIG_AMBAPP_IOAREA
  20. #define CONFIG_AMBAPP_IOAREA AMBA_DEFAULT_IOAREA
  21. #endif
  22. /* Select which TIMER that will become the time base */
  23. #ifndef CONFIG_SYS_GRLIB_GPTIMER_INDEX
  24. #define CONFIG_SYS_GRLIB_GPTIMER_INDEX 0
  25. #endif
  26. DECLARE_GLOBAL_DATA_PTR;
  27. ambapp_dev_irqmp *irqmp = NULL;
  28. /*
  29. * Breath some life into the CPU...
  30. *
  31. * Run from FLASH/PROM:
  32. * - until memory controller is set up, only registers available
  33. * - memory controller has already been setup up, stack can be used
  34. * - no global variables available for writing
  35. * - constants available
  36. */
  37. void cpu_init_f(void)
  38. {
  39. #ifdef CONFIG_DEBUG_UART
  40. debug_uart_init();
  41. #endif
  42. }
  43. /* If cache snooping is available in hardware the result will be set
  44. * to 0x800000, otherwise 0.
  45. */
  46. static unsigned int snoop_detect(void)
  47. {
  48. unsigned int result;
  49. asm("lda [%%g0] 2, %0" : "=r"(result));
  50. return result & 0x00800000;
  51. }
  52. int arch_cpu_init(void)
  53. {
  54. ambapp_apbdev apbdev;
  55. int index;
  56. gd->cpu_clk = CONFIG_SYS_CLK_FREQ;
  57. gd->bus_clk = CONFIG_SYS_CLK_FREQ;
  58. gd->ram_size = CONFIG_SYS_SDRAM_SIZE;
  59. gd->arch.snooping_available = snoop_detect();
  60. /* Initialize the AMBA Plug & Play bus structure, the bus
  61. * structure represents the AMBA bus that the CPU is located at.
  62. */
  63. ambapp_bus_init(CONFIG_AMBAPP_IOAREA, CONFIG_SYS_CLK_FREQ, &ambapp_plb);
  64. /* Initialize/clear all the timers in the system.
  65. */
  66. for (index = 0; ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
  67. GAISLER_GPTIMER, index, &apbdev) == 1; index++) {
  68. ambapp_dev_gptimer *timer;
  69. unsigned int bus_freq;
  70. int i, ntimers;
  71. timer = (ambapp_dev_gptimer *)apbdev.address;
  72. /* Different buses may have different frequency, the
  73. * frequency of the bus tell in which frequency the timer
  74. * prescaler operates.
  75. */
  76. bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
  77. /* Initialize prescaler common to all timers to 1MHz */
  78. timer->scalar = timer->scalar_reload =
  79. (((bus_freq / 1000) + 500) / 1000) - 1;
  80. /* Clear all timers */
  81. ntimers = timer->config & 0x7;
  82. for (i = 0; i < ntimers; i++) {
  83. timer->e[i].ctrl = GPTIMER_CTRL_IP;
  84. timer->e[i].rld = 0;
  85. timer->e[i].ctrl = GPTIMER_CTRL_LD;
  86. }
  87. }
  88. return 0;
  89. }
  90. /*
  91. * initialize higher level parts of CPU like time base and timers
  92. */
  93. int cpu_init_r(void)
  94. {
  95. ambapp_apbdev apbdev;
  96. int cpu;
  97. /*
  98. * Find AMBA APB IRQMP Controller,
  99. */
  100. if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER,
  101. GAISLER_IRQMP, 0, &apbdev) != 1) {
  102. panic("%s: IRQ controller not found\n", __func__);
  103. return -1;
  104. }
  105. irqmp = (ambapp_dev_irqmp *)apbdev.address;
  106. /* initialize the IRQMP */
  107. irqmp->ilevel = 0xf; /* all IRQ off */
  108. irqmp->iforce = 0;
  109. irqmp->ipend = 0;
  110. irqmp->iclear = 0xfffe; /* clear all old pending interrupts */
  111. for (cpu = 0; cpu < 16; cpu++) {
  112. /* mask and clear force for all IRQs on CPU[N] */
  113. irqmp->cpu_mask[cpu] = 0;
  114. irqmp->cpu_force[cpu] = 0;
  115. }
  116. return 0;
  117. }
  118. ;
  119. int timer_init(void)
  120. {
  121. ambapp_dev_gptimer_element *tmr;
  122. ambapp_dev_gptimer *gptimer;
  123. ambapp_apbdev apbdev;
  124. unsigned bus_freq;
  125. if (ambapp_apb_find(&ambapp_plb, VENDOR_GAISLER, GAISLER_GPTIMER,
  126. CONFIG_SYS_GRLIB_GPTIMER_INDEX, &apbdev) != 1) {
  127. panic("%s: gptimer not found!\n", __func__);
  128. return -1;
  129. }
  130. gptimer = (ambapp_dev_gptimer *) apbdev.address;
  131. /* Different buses may have different frequency, the
  132. * frequency of the bus tell in which frequency the timer
  133. * prescaler operates.
  134. */
  135. bus_freq = ambapp_bus_freq(&ambapp_plb, apbdev.ahb_bus_index);
  136. /* initialize prescaler common to all timers to 1MHz */
  137. gptimer->scalar = gptimer->scalar_reload =
  138. (((bus_freq / 1000) + 500) / 1000) - 1;
  139. tmr = (ambapp_dev_gptimer_element *)&gptimer->e[0];
  140. tmr->val = 0;
  141. tmr->rld = ~0;
  142. tmr->ctrl = GPTIMER_CTRL_EN | GPTIMER_CTRL_RS | GPTIMER_CTRL_LD;
  143. CONFIG_SYS_TIMER_COUNTER = (void *)&tmr->val;
  144. return 0;
  145. }