platform.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Atish Patra <atish.patra@wdc.com>
  8. */
  9. #include <libfdt.h>
  10. #include <sbi/riscv_asm.h>
  11. #include <sbi/riscv_io.h>
  12. #include <sbi/riscv_encoding.h>
  13. #include <sbi/sbi_console.h>
  14. #include <sbi/sbi_const.h>
  15. #include <sbi/sbi_platform.h>
  16. #include <sbi_utils/fdt/fdt_fixup.h>
  17. #include <sbi_utils/irqchip/plic.h>
  18. #include <sbi_utils/serial/sifive-uart.h>
  19. #include <sbi_utils/sys/clint.h>
  20. /* clang-format off */
  21. #define FU540_HART_COUNT 5
  22. #define FU540_SYS_CLK 1000000000
  23. #define FU540_CLINT_ADDR 0x2000000
  24. #define FU540_PLIC_ADDR 0xc000000
  25. #define FU540_PLIC_NUM_SOURCES 0x35
  26. #define FU540_PLIC_NUM_PRIORITIES 7
  27. #define FU540_UART0_ADDR 0x10010000
  28. #define FU540_UART1_ADDR 0x10011000
  29. #define FU540_UART_BAUDRATE 115200
  30. /* PRCI clock related macros */
  31. //TODO: Do we need a separate driver for this ?
  32. #define FU540_PRCI_BASE_ADDR 0x10000000
  33. #define FU540_PRCI_CLKMUXSTATUSREG 0x002C
  34. #define FU540_PRCI_CLKMUX_STATUS_TLCLKSEL (0x1 << 1)
  35. /* Full tlb flush always */
  36. #define FU540_TLB_RANGE_FLUSH_LIMIT 0
  37. /* clang-format on */
  38. static struct plic_data plic = {
  39. .addr = FU540_PLIC_ADDR,
  40. .num_src = FU540_PLIC_NUM_SOURCES,
  41. };
  42. static struct clint_data clint = {
  43. .addr = FU540_CLINT_ADDR,
  44. .first_hartid = 0,
  45. .hart_count = FU540_HART_COUNT,
  46. .has_64bit_mmio = TRUE,
  47. };
  48. static void fu540_modify_dt(void *fdt)
  49. {
  50. fdt_cpu_fixup(fdt);
  51. fdt_fixups(fdt);
  52. /*
  53. * SiFive Freedom U540 has an erratum that prevents S-mode software
  54. * to access a PMP protected region using 1GB page table mapping, so
  55. * always add the no-map attribute on this platform.
  56. */
  57. fdt_reserved_memory_nomap_fixup(fdt);
  58. }
  59. static int fu540_final_init(bool cold_boot)
  60. {
  61. void *fdt;
  62. if (!cold_boot)
  63. return 0;
  64. fdt = sbi_scratch_thishart_arg1_ptr();
  65. fu540_modify_dt(fdt);
  66. return 0;
  67. }
  68. static int fu540_console_init(void)
  69. {
  70. unsigned long peri_in_freq;
  71. if (readl((volatile void *)FU540_PRCI_BASE_ADDR +
  72. FU540_PRCI_CLKMUXSTATUSREG) &
  73. FU540_PRCI_CLKMUX_STATUS_TLCLKSEL) {
  74. peri_in_freq = FU540_SYS_CLK;
  75. } else {
  76. peri_in_freq = FU540_SYS_CLK / 2;
  77. }
  78. return sifive_uart_init(FU540_UART0_ADDR, peri_in_freq,
  79. FU540_UART_BAUDRATE);
  80. }
  81. static int fu540_irqchip_init(bool cold_boot)
  82. {
  83. int rc;
  84. u32 hartid = current_hartid();
  85. if (cold_boot) {
  86. rc = plic_cold_irqchip_init(&plic);
  87. if (rc)
  88. return rc;
  89. }
  90. return plic_warm_irqchip_init(&plic, (hartid) ? (2 * hartid - 1) : 0,
  91. (hartid) ? (2 * hartid) : -1);
  92. }
  93. static int fu540_ipi_init(bool cold_boot)
  94. {
  95. int rc;
  96. if (cold_boot) {
  97. rc = clint_cold_ipi_init(&clint);
  98. if (rc)
  99. return rc;
  100. }
  101. return clint_warm_ipi_init();
  102. }
  103. static u64 fu540_get_tlbr_flush_limit(void)
  104. {
  105. return FU540_TLB_RANGE_FLUSH_LIMIT;
  106. }
  107. static int fu540_timer_init(bool cold_boot)
  108. {
  109. int rc;
  110. if (cold_boot) {
  111. rc = clint_cold_timer_init(&clint, NULL);
  112. if (rc)
  113. return rc;
  114. }
  115. return clint_warm_timer_init();
  116. }
  117. static u32 fu540_hart_index2id[FU540_HART_COUNT - 1] = {
  118. [0] = 1,
  119. [1] = 2,
  120. [2] = 3,
  121. [3] = 4,
  122. };
  123. const struct sbi_platform_operations platform_ops = {
  124. .final_init = fu540_final_init,
  125. .console_init = fu540_console_init,
  126. .irqchip_init = fu540_irqchip_init,
  127. .ipi_init = fu540_ipi_init,
  128. .get_tlbr_flush_limit = fu540_get_tlbr_flush_limit,
  129. .timer_init = fu540_timer_init,
  130. };
  131. const struct sbi_platform platform = {
  132. .opensbi_version = OPENSBI_VERSION,
  133. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x01),
  134. .name = "SiFive Freedom U540",
  135. .features = SBI_PLATFORM_DEFAULT_FEATURES,
  136. .hart_count = (FU540_HART_COUNT - 1),
  137. .hart_index2id = fu540_hart_index2id,
  138. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  139. .platform_ops_addr = (unsigned long)&platform_ops
  140. };