platform.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. * Write a character to the platform console output.
  59. */
  60. static void platform_console_putc(char ch)
  61. {
  62. /* Example if the generic UART8250 driver is used */
  63. uart8250_putc(ch);
  64. }
  65. /*
  66. * Read a character from the platform console input.
  67. */
  68. static int platform_console_getc(void)
  69. {
  70. return uart8250_getc();
  71. }
  72. /*
  73. * Initialize the platform interrupt controller for current HART.
  74. */
  75. static int platform_irqchip_init(bool cold_boot)
  76. {
  77. u32 hartid = current_hartid();
  78. int ret;
  79. /* Example if the generic PLIC driver is used */
  80. if (cold_boot) {
  81. ret = plic_cold_irqchip_init(&plic);
  82. if (ret)
  83. return ret;
  84. }
  85. return plic_warm_irqchip_init(&plic, 2 * hartid, 2 * hartid + 1);
  86. }
  87. /*
  88. * Initialize IPI for current HART.
  89. */
  90. static int platform_ipi_init(bool cold_boot)
  91. {
  92. int ret;
  93. /* Example if the generic CLINT driver is used */
  94. if (cold_boot) {
  95. ret = clint_cold_ipi_init(&clint);
  96. if (ret)
  97. return ret;
  98. }
  99. return clint_warm_ipi_init();
  100. }
  101. /*
  102. * Send IPI to a target HART
  103. */
  104. static void platform_ipi_send(u32 target_hart)
  105. {
  106. /* Example if the generic CLINT driver is used */
  107. clint_ipi_send(target_hart);
  108. }
  109. /*
  110. * Clear IPI for a target HART.
  111. */
  112. static void platform_ipi_clear(u32 target_hart)
  113. {
  114. /* Example if the generic CLINT driver is used */
  115. clint_ipi_clear(target_hart);
  116. }
  117. /*
  118. * Initialize platform timer for current HART.
  119. */
  120. static int platform_timer_init(bool cold_boot)
  121. {
  122. int ret;
  123. /* Example if the generic CLINT driver is used */
  124. if (cold_boot) {
  125. ret = clint_cold_timer_init(&clint, NULL);
  126. if (ret)
  127. return ret;
  128. }
  129. return clint_warm_timer_init();
  130. }
  131. /*
  132. * Get platform timer value.
  133. */
  134. static u64 platform_timer_value(void)
  135. {
  136. /* Example if the generic CLINT driver is used */
  137. return clint_timer_value();
  138. }
  139. /*
  140. * Start platform timer event for current HART.
  141. */
  142. static void platform_timer_event_start(u64 next_event)
  143. {
  144. /* Example if the generic CLINT driver is used */
  145. clint_timer_event_start(next_event);
  146. }
  147. /*
  148. * Stop platform timer event for current HART.
  149. */
  150. static void platform_timer_event_stop(void)
  151. {
  152. /* Example if the generic CLINT driver is used */
  153. clint_timer_event_stop();
  154. }
  155. /*
  156. * Check reset type and reason supported by the platform.
  157. */
  158. static int platform_system_reset_check(u32 type, u32 reason)
  159. {
  160. return 0;
  161. }
  162. /*
  163. * Reset the platform.
  164. */
  165. static void platform_system_reset(u32 type, u32 reason)
  166. {
  167. }
  168. /*
  169. * Platform descriptor.
  170. */
  171. const struct sbi_platform_operations platform_ops = {
  172. .early_init = platform_early_init,
  173. .final_init = platform_final_init,
  174. .console_putc = platform_console_putc,
  175. .console_getc = platform_console_getc,
  176. .console_init = platform_console_init,
  177. .irqchip_init = platform_irqchip_init,
  178. .ipi_send = platform_ipi_send,
  179. .ipi_clear = platform_ipi_clear,
  180. .ipi_init = platform_ipi_init,
  181. .timer_value = platform_timer_value,
  182. .timer_event_stop = platform_timer_event_stop,
  183. .timer_event_start = platform_timer_event_start,
  184. .timer_init = platform_timer_init,
  185. .system_reset_check = platform_system_reset_check,
  186. .system_reset = platform_system_reset
  187. };
  188. const struct sbi_platform platform = {
  189. .opensbi_version = OPENSBI_VERSION,
  190. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x00),
  191. .name = "platform-name",
  192. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  193. .hart_count = 1,
  194. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  195. .platform_ops_addr = (unsigned long)&platform_ops
  196. };