cpu_init.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <config.h>
  13. #define TIMER_BASE_CLK 1000000
  14. #define US_PER_TICK (1000000 / CONFIG_SYS_HZ)
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* reset CPU (jump to 0, without reset) */
  17. void start(void);
  18. struct {
  19. gd_t gd_area;
  20. bd_t bd;
  21. } global_data;
  22. /*
  23. * Breath some life into the CPU...
  24. *
  25. * Set up the memory map,
  26. * initialize a bunch of registers.
  27. *
  28. * Run from FLASH/PROM:
  29. * - until memory controller is set up, only registers available
  30. * - no global variables available for writing
  31. * - constants available
  32. */
  33. void cpu_init_f(void)
  34. {
  35. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  36. /* initialize the IRQMP */
  37. leon2->Interrupt_Force = 0;
  38. leon2->Interrupt_Pending = 0;
  39. leon2->Interrupt_Clear = 0xfffe; /* clear all old pending interrupts */
  40. leon2->Interrupt_Mask = 0xfffe0000; /* mask all IRQs */
  41. /* cache */
  42. /* I/O port setup */
  43. #ifdef LEON2_IO_PORT_DIR
  44. leon2->PIO_Direction = LEON2_IO_PORT_DIR;
  45. #endif
  46. #ifdef LEON2_IO_PORT_DATA
  47. leon2->PIO_Data = LEON2_IO_PORT_DATA;
  48. #endif
  49. #ifdef LEON2_IO_PORT_INT
  50. leon2->PIO_Interrupt = LEON2_IO_PORT_INT;
  51. #else
  52. leon2->PIO_Interrupt = 0;
  53. #endif
  54. }
  55. void cpu_init_f2(void)
  56. {
  57. }
  58. /*
  59. * initialize higher level parts of CPU like time base and timers
  60. */
  61. int cpu_init_r(void)
  62. {
  63. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  64. /* initialize prescaler common to all timers to 1MHz */
  65. leon2->Scaler_Counter = leon2->Scaler_Reload =
  66. (((CONFIG_SYS_CLK_FREQ / 1000) + 500) / 1000) - 1;
  67. return (0);
  68. }
  69. /* Uses Timer 0 to get accurate
  70. * pauses. Max 2 raised to 32 ticks
  71. *
  72. */
  73. void cpu_wait_ticks(unsigned long ticks)
  74. {
  75. unsigned long start = get_timer(0);
  76. while (get_timer(start) < ticks) ;
  77. }
  78. /* initiate and setup timer0 interrupt to configured HZ. Base clock is 1MHz.
  79. * Return irq number for timer int or a negative number for
  80. * dealing with self
  81. */
  82. int timer_interrupt_init_cpu(void)
  83. {
  84. LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;
  85. /* SYS_HZ ticks per second */
  86. leon2->Timer_Counter_1 = 0;
  87. leon2->Timer_Reload_1 = (TIMER_BASE_CLK / CONFIG_SYS_HZ) - 1;
  88. leon2->Timer_Control_1 =
  89. (LEON2_TIMER_CTRL_EN | LEON2_TIMER_CTRL_RS | LEON2_TIMER_CTRL_LD);
  90. return LEON2_TIMER1_IRQNO;
  91. }
  92. ulong get_tbclk(void)
  93. {
  94. return TIMER_BASE_CLK;
  95. }
  96. /*
  97. * This function is intended for SHORT delays only.
  98. */
  99. unsigned long cpu_usec2ticks(unsigned long usec)
  100. {
  101. if (usec < US_PER_TICK)
  102. return 1;
  103. return usec / US_PER_TICK;
  104. }
  105. unsigned long cpu_ticks2usec(unsigned long ticks)
  106. {
  107. return ticks * US_PER_TICK;
  108. }