platform.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. */
  6. #include <sbi/riscv_asm.h>
  7. #include <sbi/riscv_encoding.h>
  8. #include <sbi/sbi_const.h>
  9. #include <sbi/sbi_platform.h>
  10. /*
  11. * Include these files as needed.
  12. * See config.mk PLATFORM_xxx configuration parameters.
  13. */
  14. #include <sbi_utils/irqchip/plic.h>
  15. #include <sbi_utils/serial/uart8250.h>
  16. #include <sbi_utils/sys/clint.h>
  17. #define PLATFORM_PLIC_ADDR 0xc000000
  18. #define PLATFORM_PLIC_NUM_SOURCES 128
  19. #define PLATFORM_HART_COUNT 4
  20. #define PLATFORM_CLINT_ADDR 0x2000000
  21. #define PLATFORM_UART_ADDR 0x09000000
  22. #define PLATFORM_UART_INPUT_FREQ 10000000
  23. #define PLATFORM_UART_BAUDRATE 115200
  24. static struct plic_data plic = {
  25. .addr = PLATFORM_PLIC_ADDR,
  26. .num_src = PLATFORM_PLIC_NUM_SOURCES,
  27. };
  28. static struct clint_data clint = {
  29. .addr = PLATFORM_CLINT_ADDR,
  30. .first_hartid = 0,
  31. .hart_count = PLATFORM_HART_COUNT,
  32. .has_64bit_mmio = TRUE,
  33. };
  34. /*
  35. * Platform early initialization.
  36. */
  37. static int platform_early_init(bool cold_boot)
  38. {
  39. return 0;
  40. }
  41. /*
  42. * Platform final initialization.
  43. */
  44. static int platform_final_init(bool cold_boot)
  45. {
  46. return 0;
  47. }
  48. /*
  49. * Initialize the platform console.
  50. */
  51. static int platform_console_init(void)
  52. {
  53. /* Example if the generic UART8250 driver is used */
  54. return uart8250_init(PLATFORM_UART_ADDR, PLATFORM_UART_INPUT_FREQ,
  55. PLATFORM_UART_BAUDRATE, 0, 1);
  56. }
  57. /*
  58. * Initialize the platform interrupt controller for current HART.
  59. */
  60. static int platform_irqchip_init(bool cold_boot)
  61. {
  62. u32 hartid = current_hartid();
  63. int ret;
  64. /* Example if the generic PLIC driver is used */
  65. if (cold_boot) {
  66. ret = plic_cold_irqchip_init(&plic);
  67. if (ret)
  68. return ret;
  69. }
  70. return plic_warm_irqchip_init(&plic, 2 * hartid, 2 * hartid + 1);
  71. }
  72. /*
  73. * Initialize IPI for current HART.
  74. */
  75. static int platform_ipi_init(bool cold_boot)
  76. {
  77. int ret;
  78. /* Example if the generic CLINT driver is used */
  79. if (cold_boot) {
  80. ret = clint_cold_ipi_init(&clint);
  81. if (ret)
  82. return ret;
  83. }
  84. return clint_warm_ipi_init();
  85. }
  86. /*
  87. * Send IPI to a target HART
  88. */
  89. static void platform_ipi_send(u32 target_hart)
  90. {
  91. /* Example if the generic CLINT driver is used */
  92. clint_ipi_send(target_hart);
  93. }
  94. /*
  95. * Clear IPI for a target HART.
  96. */
  97. static void platform_ipi_clear(u32 target_hart)
  98. {
  99. /* Example if the generic CLINT driver is used */
  100. clint_ipi_clear(target_hart);
  101. }
  102. /*
  103. * Initialize platform timer for current HART.
  104. */
  105. static int platform_timer_init(bool cold_boot)
  106. {
  107. int ret;
  108. /* Example if the generic CLINT driver is used */
  109. if (cold_boot) {
  110. ret = clint_cold_timer_init(&clint, NULL);
  111. if (ret)
  112. return ret;
  113. }
  114. return clint_warm_timer_init();
  115. }
  116. /*
  117. * Get platform timer value.
  118. */
  119. static u64 platform_timer_value(void)
  120. {
  121. /* Example if the generic CLINT driver is used */
  122. return clint_timer_value();
  123. }
  124. /*
  125. * Start platform timer event for current HART.
  126. */
  127. static void platform_timer_event_start(u64 next_event)
  128. {
  129. /* Example if the generic CLINT driver is used */
  130. clint_timer_event_start(next_event);
  131. }
  132. /*
  133. * Stop platform timer event for current HART.
  134. */
  135. static void platform_timer_event_stop(void)
  136. {
  137. /* Example if the generic CLINT driver is used */
  138. clint_timer_event_stop();
  139. }
  140. /*
  141. * Check reset type and reason supported by the platform.
  142. */
  143. static int platform_system_reset_check(u32 type, u32 reason)
  144. {
  145. return 0;
  146. }
  147. /*
  148. * Reset the platform.
  149. */
  150. static void platform_system_reset(u32 type, u32 reason)
  151. {
  152. }
  153. /*
  154. * Platform descriptor.
  155. */
  156. const struct sbi_platform_operations platform_ops = {
  157. .early_init = platform_early_init,
  158. .final_init = platform_final_init,
  159. .console_init = platform_console_init,
  160. .irqchip_init = platform_irqchip_init,
  161. .ipi_send = platform_ipi_send,
  162. .ipi_clear = platform_ipi_clear,
  163. .ipi_init = platform_ipi_init,
  164. .timer_value = platform_timer_value,
  165. .timer_event_stop = platform_timer_event_stop,
  166. .timer_event_start = platform_timer_event_start,
  167. .timer_init = platform_timer_init,
  168. .system_reset_check = platform_system_reset_check,
  169. .system_reset = platform_system_reset
  170. };
  171. const struct sbi_platform platform = {
  172. .opensbi_version = OPENSBI_VERSION,
  173. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x00),
  174. .name = "platform-name",
  175. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  176. .hart_count = 1,
  177. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  178. .platform_ops_addr = (unsigned long)&platform_ops
  179. };