platform.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Damien Le Moal <damien.lemoal@wdc.com>
  8. */
  9. #include <sbi/riscv_asm.h>
  10. #include <sbi/riscv_encoding.h>
  11. #include <sbi/sbi_console.h>
  12. #include <sbi/sbi_const.h>
  13. #include <sbi/sbi_platform.h>
  14. #include <sbi/sbi_system.h>
  15. #include <sbi_utils/fdt/fdt_fixup.h>
  16. #include <sbi_utils/irqchip/plic.h>
  17. #include <sbi_utils/serial/sifive-uart.h>
  18. #include <sbi_utils/sys/clint.h>
  19. #include "platform.h"
  20. extern const char dt_k210_start[];
  21. unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
  22. unsigned long arg2, unsigned long arg3,
  23. unsigned long arg4)
  24. {
  25. return (unsigned long)&dt_k210_start[0];
  26. }
  27. static struct plic_data plic = {
  28. .addr = K210_PLIC_BASE_ADDR,
  29. .num_src = K210_PLIC_NUM_SOURCES,
  30. };
  31. static struct clint_data clint = {
  32. .addr = K210_CLINT_BASE_ADDR,
  33. .first_hartid = 0,
  34. .hart_count = K210_HART_COUNT,
  35. .has_64bit_mmio = TRUE,
  36. };
  37. static u32 k210_get_clk_freq(void)
  38. {
  39. u32 clksel0, pll0;
  40. u64 pll0_freq, clkr0, clkf0, clkod0, div;
  41. /*
  42. * If the clock selector is not set, use the base frequency.
  43. * Otherwise, use PLL0 frequency with a frequency divisor.
  44. */
  45. clksel0 = k210_read_sysreg(K210_CLKSEL0);
  46. if (!(clksel0 & 0x1))
  47. return K210_CLK0_FREQ;
  48. /*
  49. * Get PLL0 frequency:
  50. * freq = base frequency * clkf0 / (clkr0 * clkod0)
  51. */
  52. pll0 = k210_read_sysreg(K210_PLL0);
  53. clkr0 = 1 + (pll0 & 0x0000000f);
  54. clkf0 = 1 + ((pll0 & 0x000003f0) >> 4);
  55. clkod0 = 1 + ((pll0 & 0x00003c00) >> 10);
  56. pll0_freq = clkf0 * K210_CLK0_FREQ / (clkr0 * clkod0);
  57. /* Get the frequency divisor from the clock selector */
  58. div = 2ULL << ((clksel0 & 0x00000006) >> 1);
  59. return pll0_freq / div;
  60. }
  61. static int k210_system_reset_check(u32 type, u32 reason)
  62. {
  63. return 1;
  64. }
  65. static void k210_system_reset(u32 type, u32 reason)
  66. {
  67. u32 val;
  68. val = k210_read_sysreg(K210_RESET);
  69. val |= K210_RESET_MASK;
  70. k210_write_sysreg(val, K210_RESET);
  71. while (1);
  72. }
  73. static struct sbi_system_reset_device k210_reset = {
  74. .name = "kendryte_k210_reset",
  75. .system_reset_check = k210_system_reset_check,
  76. .system_reset = k210_system_reset
  77. };
  78. static int k210_early_init(bool cold_boot)
  79. {
  80. if (cold_boot)
  81. sbi_system_reset_set_device(&k210_reset);
  82. return 0;
  83. }
  84. static int k210_final_init(bool cold_boot)
  85. {
  86. void *fdt;
  87. if (!cold_boot)
  88. return 0;
  89. fdt = sbi_scratch_thishart_arg1_ptr();
  90. fdt_cpu_fixup(fdt);
  91. fdt_fixups(fdt);
  92. return 0;
  93. }
  94. static int k210_console_init(void)
  95. {
  96. return sifive_uart_init(K210_UART_BASE_ADDR, k210_get_clk_freq(),
  97. K210_UART_BAUDRATE);
  98. }
  99. static int k210_irqchip_init(bool cold_boot)
  100. {
  101. int rc;
  102. u32 hartid = current_hartid();
  103. if (cold_boot) {
  104. rc = plic_cold_irqchip_init(&plic);
  105. if (rc)
  106. return rc;
  107. }
  108. return plic_warm_irqchip_init(&plic, hartid * 2, hartid * 2 + 1);
  109. }
  110. static int k210_ipi_init(bool cold_boot)
  111. {
  112. int rc;
  113. if (cold_boot) {
  114. rc = clint_cold_ipi_init(&clint);
  115. if (rc)
  116. return rc;
  117. }
  118. return clint_warm_ipi_init();
  119. }
  120. static int k210_timer_init(bool cold_boot)
  121. {
  122. int rc;
  123. if (cold_boot) {
  124. rc = clint_cold_timer_init(&clint, NULL);
  125. if (rc)
  126. return rc;
  127. }
  128. return clint_warm_timer_init();
  129. }
  130. const struct sbi_platform_operations platform_ops = {
  131. .early_init = k210_early_init,
  132. .final_init = k210_final_init,
  133. .console_init = k210_console_init,
  134. .irqchip_init = k210_irqchip_init,
  135. .ipi_init = k210_ipi_init,
  136. .timer_init = k210_timer_init,
  137. };
  138. const struct sbi_platform platform = {
  139. .opensbi_version = OPENSBI_VERSION,
  140. .platform_version = SBI_PLATFORM_VERSION(0x0, 0x01),
  141. .name = "Kendryte K210",
  142. .features = 0,
  143. .hart_count = K210_HART_COUNT,
  144. .hart_stack_size = SBI_PLATFORM_DEFAULT_HART_STACK_SIZE,
  145. .platform_ops_addr = (unsigned long)&platform_ops
  146. };