platform.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * Initialize platform timer for current HART.
  88. */
  89. static int platform_timer_init(bool cold_boot)
  90. {
  91. int ret;
  92. /* Example if the generic CLINT driver is used */
  93. if (cold_boot) {
  94. ret = clint_cold_timer_init(&clint, NULL);
  95. if (ret)
  96. return ret;
  97. }
  98. return clint_warm_timer_init();
  99. }
  100. /*
  101. * Platform descriptor.
  102. */
  103. const struct sbi_platform_operations platform_ops = {
  104. .early_init = platform_early_init,
  105. .final_init = platform_final_init,
  106. .console_init = platform_console_init,
  107. .irqchip_init = platform_irqchip_init,
  108. .ipi_init = platform_ipi_init,
  109. .timer_init = platform_timer_init
  110. };
  111. const struct sbi_platform platform = {
  112. .opensbi_version = OPENSBI_VERSION,
  113. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x00),
  114. .name = "platform-name",
  115. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  116. .hart_count = 1,
  117. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  118. .platform_ops_addr = (unsigned long)&platform_ops
  119. };