platform.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 objects.mk PLATFORM_xxx configuration parameters.
  13. */
  14. #include <sbi_utils/ipi/aclint_mswi.h>
  15. #include <sbi_utils/irqchip/plic.h>
  16. #include <sbi_utils/serial/uart8250.h>
  17. #include <sbi_utils/timer/aclint_mtimer.h>
  18. #define PLATFORM_PLIC_ADDR 0xc000000
  19. #define PLATFORM_PLIC_NUM_SOURCES 128
  20. #define PLATFORM_HART_COUNT 4
  21. #define PLATFORM_CLINT_ADDR 0x2000000
  22. #define PLATFORM_ACLINT_MTIMER_FREQ 10000000
  23. #define PLATFORM_ACLINT_MSWI_ADDR (PLATFORM_CLINT_ADDR + \
  24. CLINT_MSWI_OFFSET)
  25. #define PLATFORM_ACLINT_MTIMER_ADDR (PLATFORM_CLINT_ADDR + \
  26. CLINT_MTIMER_OFFSET)
  27. #define PLATFORM_UART_ADDR 0x09000000
  28. #define PLATFORM_UART_INPUT_FREQ 10000000
  29. #define PLATFORM_UART_BAUDRATE 115200
  30. static struct plic_data plic = {
  31. .addr = PLATFORM_PLIC_ADDR,
  32. .num_src = PLATFORM_PLIC_NUM_SOURCES,
  33. };
  34. static struct aclint_mswi_data mswi = {
  35. .addr = PLATFORM_ACLINT_MSWI_ADDR,
  36. .size = ACLINT_MSWI_SIZE,
  37. .first_hartid = 0,
  38. .hart_count = PLATFORM_HART_COUNT,
  39. };
  40. static struct aclint_mtimer_data mtimer = {
  41. .mtime_freq = PLATFORM_ACLINT_MTIMER_FREQ,
  42. .mtime_addr = PLATFORM_ACLINT_MTIMER_ADDR +
  43. ACLINT_DEFAULT_MTIME_OFFSET,
  44. .mtime_size = ACLINT_DEFAULT_MTIME_SIZE,
  45. .mtimecmp_addr = PLATFORM_ACLINT_MTIMER_ADDR +
  46. ACLINT_DEFAULT_MTIMECMP_OFFSET,
  47. .mtimecmp_size = ACLINT_DEFAULT_MTIMECMP_SIZE,
  48. .first_hartid = 0,
  49. .hart_count = PLATFORM_HART_COUNT,
  50. .has_64bit_mmio = true,
  51. };
  52. /*
  53. * Platform early initialization.
  54. */
  55. static int platform_early_init(bool cold_boot)
  56. {
  57. return 0;
  58. }
  59. /*
  60. * Platform final initialization.
  61. */
  62. static int platform_final_init(bool cold_boot)
  63. {
  64. return 0;
  65. }
  66. /*
  67. * Initialize the platform console.
  68. */
  69. static int platform_console_init(void)
  70. {
  71. /* Example if the generic UART8250 driver is used */
  72. return uart8250_init(PLATFORM_UART_ADDR, PLATFORM_UART_INPUT_FREQ,
  73. PLATFORM_UART_BAUDRATE, 0, 1, 0);
  74. }
  75. /*
  76. * Initialize the platform interrupt controller for current HART.
  77. */
  78. static int platform_irqchip_init(bool cold_boot)
  79. {
  80. u32 hartid = current_hartid();
  81. int ret;
  82. /* Example if the generic PLIC driver is used */
  83. if (cold_boot) {
  84. ret = plic_cold_irqchip_init(&plic);
  85. if (ret)
  86. return ret;
  87. }
  88. return plic_warm_irqchip_init(&plic, 2 * hartid, 2 * hartid + 1);
  89. }
  90. /*
  91. * Initialize IPI for current HART.
  92. */
  93. static int platform_ipi_init(bool cold_boot)
  94. {
  95. int ret;
  96. /* Example if the generic ACLINT driver is used */
  97. if (cold_boot) {
  98. ret = aclint_mswi_cold_init(&mswi);
  99. if (ret)
  100. return ret;
  101. }
  102. return aclint_mswi_warm_init();
  103. }
  104. /*
  105. * Initialize platform timer for current HART.
  106. */
  107. static int platform_timer_init(bool cold_boot)
  108. {
  109. int ret;
  110. /* Example if the generic ACLINT driver is used */
  111. if (cold_boot) {
  112. ret = aclint_mtimer_cold_init(&mtimer, NULL);
  113. if (ret)
  114. return ret;
  115. }
  116. return aclint_mtimer_warm_init();
  117. }
  118. /*
  119. * Platform descriptor.
  120. */
  121. const struct sbi_platform_operations platform_ops = {
  122. .early_init = platform_early_init,
  123. .final_init = platform_final_init,
  124. .console_init = platform_console_init,
  125. .irqchip_init = platform_irqchip_init,
  126. .ipi_init = platform_ipi_init,
  127. .timer_init = platform_timer_init
  128. };
  129. const struct sbi_platform platform = {
  130. .opensbi_version = OPENSBI_VERSION,
  131. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x00),
  132. .name = "platform-name",
  133. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  134. .hart_count = 1,
  135. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  136. .platform_ops_addr = (unsigned long)&platform_ops
  137. };