fdt_timer_mtimer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2021 Western Digital Corporation or its affiliates.
  5. *
  6. * Authors:
  7. * Anup Patel <anup.patel@wdc.com>
  8. */
  9. #include <libfdt.h>
  10. #include <sbi/sbi_error.h>
  11. #include <sbi_utils/fdt/fdt_helper.h>
  12. #include <sbi_utils/timer/fdt_timer.h>
  13. #include <sbi_utils/timer/aclint_mtimer.h>
  14. #define MTIMER_MAX_NR 16
  15. struct timer_mtimer_quirks {
  16. unsigned int mtime_offset;
  17. bool has_64bit_mmio;
  18. bool without_mtime;
  19. };
  20. static unsigned long mtimer_count = 0;
  21. static struct aclint_mtimer_data mtimer[MTIMER_MAX_NR];
  22. static struct aclint_mtimer_data *mt_reference = NULL;
  23. static int timer_mtimer_cold_init(void *fdt, int nodeoff,
  24. const struct fdt_match *match)
  25. {
  26. int i, rc;
  27. unsigned long addr[2], size[2];
  28. struct aclint_mtimer_data *mt;
  29. if (MTIMER_MAX_NR <= mtimer_count)
  30. return SBI_ENOSPC;
  31. mt = &mtimer[mtimer_count];
  32. rc = fdt_parse_aclint_node(fdt, nodeoff, true,
  33. &addr[0], &size[0], &addr[1], &size[1],
  34. &mt->first_hartid, &mt->hart_count);
  35. if (rc)
  36. return rc;
  37. mt->has_64bit_mmio = true;
  38. mt->has_shared_mtime = false;
  39. rc = fdt_parse_timebase_frequency(fdt, &mt->mtime_freq);
  40. if (rc)
  41. return rc;
  42. if (match->data) { /* SiFive CLINT */
  43. const struct timer_mtimer_quirks *quirks = match->data;
  44. /* Set CLINT addresses */
  45. mt->mtimecmp_addr = addr[0] + ACLINT_DEFAULT_MTIMECMP_OFFSET;
  46. mt->mtimecmp_size = ACLINT_DEFAULT_MTIMECMP_SIZE;
  47. if (!quirks->without_mtime) {
  48. mt->mtime_addr = addr[0] + ACLINT_DEFAULT_MTIME_OFFSET;
  49. mt->mtime_size = size[0] - mt->mtimecmp_size;
  50. /* Adjust MTIMER address and size for CLINT device */
  51. mt->mtime_addr += quirks->mtime_offset;
  52. mt->mtime_size -= quirks->mtime_offset;
  53. } else {
  54. mt->mtime_addr = mt->mtime_size = 0;
  55. }
  56. mt->mtimecmp_addr += quirks->mtime_offset;
  57. /* Apply additional CLINT quirks */
  58. mt->has_64bit_mmio = quirks->has_64bit_mmio;
  59. } else { /* RISC-V ACLINT MTIMER */
  60. /* Set ACLINT MTIMER addresses */
  61. mt->mtime_addr = addr[0];
  62. mt->mtime_size = size[0];
  63. mt->mtimecmp_addr = addr[1];
  64. mt->mtimecmp_size = size[1];
  65. }
  66. /* Check if MTIMER device has shared MTIME address */
  67. if (mt->mtime_size) {
  68. mt->has_shared_mtime = false;
  69. for (i = 0; i < mtimer_count; i++) {
  70. if (mtimer[i].mtime_addr == mt->mtime_addr) {
  71. mt->has_shared_mtime = true;
  72. break;
  73. }
  74. }
  75. } else {
  76. /* Assume shared time CSR */
  77. mt->has_shared_mtime = true;
  78. }
  79. /* Initialize the MTIMER device */
  80. rc = aclint_mtimer_cold_init(mt, mt_reference);
  81. if (rc)
  82. return rc;
  83. /*
  84. * Select first MTIMER device with no associated HARTs as our
  85. * reference MTIMER device. This is only a temporary strategy
  86. * of selecting reference MTIMER device. In future, we might
  87. * define an optional DT property or some other mechanism to
  88. * help us select the reference MTIMER device.
  89. */
  90. if (!mt->hart_count && !mt_reference) {
  91. mt_reference = mt;
  92. /*
  93. * Set reference for already propbed MTIMER devices
  94. * with non-shared MTIME
  95. */
  96. for (i = 0; i < mtimer_count; i++)
  97. if (!mtimer[i].has_shared_mtime)
  98. aclint_mtimer_set_reference(&mtimer[i], mt);
  99. }
  100. /* Explicitly sync-up MTIMER devices not associated with any HARTs */
  101. if (!mt->hart_count)
  102. aclint_mtimer_sync(mt);
  103. mtimer_count++;
  104. return 0;
  105. }
  106. static const struct timer_mtimer_quirks sifive_clint_quirks = {
  107. .mtime_offset = CLINT_MTIMER_OFFSET,
  108. .has_64bit_mmio = true,
  109. };
  110. static const struct timer_mtimer_quirks thead_clint_quirks = {
  111. .mtime_offset = CLINT_MTIMER_OFFSET,
  112. .without_mtime = true,
  113. };
  114. static const struct fdt_match timer_mtimer_match[] = {
  115. { .compatible = "riscv,clint0", .data = &sifive_clint_quirks },
  116. { .compatible = "sifive,clint0", .data = &sifive_clint_quirks },
  117. { .compatible = "thead,c900-clint", .data = &thead_clint_quirks },
  118. { .compatible = "riscv,aclint-mtimer" },
  119. { },
  120. };
  121. struct fdt_timer fdt_timer_mtimer = {
  122. .match_table = timer_mtimer_match,
  123. .cold_init = timer_mtimer_cold_init,
  124. .warm_init = aclint_mtimer_warm_init,
  125. .exit = NULL,
  126. };