bpmp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <assert.h>
  8. #include <bpmp.h>
  9. #include <common/debug.h>
  10. #include <drivers/delay_timer.h>
  11. #include <errno.h>
  12. #include <lib/mmio.h>
  13. #include <plat/common/platform.h>
  14. #include <stdbool.h>
  15. #include <string.h>
  16. #include <tegra_def.h>
  17. #define BPMP_TIMEOUT 500 /* 500ms */
  18. static uint32_t channel_base[NR_CHANNELS];
  19. static uint32_t bpmp_init_state = BPMP_INIT_PENDING;
  20. static uint32_t channel_field(unsigned int ch)
  21. {
  22. return mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET) & CH_MASK(ch);
  23. }
  24. static bool master_free(unsigned int ch)
  25. {
  26. return channel_field(ch) == MA_FREE(ch);
  27. }
  28. static bool master_acked(unsigned int ch)
  29. {
  30. return channel_field(ch) == MA_ACKD(ch);
  31. }
  32. static void signal_slave(unsigned int ch)
  33. {
  34. mmio_write_32(TEGRA_RES_SEMA_BASE + CLR_OFFSET, CH_MASK(ch));
  35. }
  36. static void free_master(unsigned int ch)
  37. {
  38. mmio_write_32(TEGRA_RES_SEMA_BASE + CLR_OFFSET,
  39. MA_ACKD(ch) ^ MA_FREE(ch));
  40. }
  41. /* should be called with local irqs disabled */
  42. int32_t tegra_bpmp_send_receive_atomic(int mrq, const void *ob_data, int ob_sz,
  43. void *ib_data, int ib_sz)
  44. {
  45. unsigned int ch = (unsigned int)plat_my_core_pos();
  46. mb_data_t *p = (mb_data_t *)(uintptr_t)channel_base[ch];
  47. int32_t ret = -ETIMEDOUT, timeout = 0;
  48. if (bpmp_init_state == BPMP_INIT_COMPLETE) {
  49. /* loop until BPMP is free */
  50. for (timeout = 0; timeout < BPMP_TIMEOUT; timeout++) {
  51. if (master_free(ch) == true) {
  52. break;
  53. }
  54. mdelay(1);
  55. }
  56. if (timeout != BPMP_TIMEOUT) {
  57. /* generate the command struct */
  58. p->code = mrq;
  59. p->flags = DO_ACK;
  60. (void)memcpy((void *)p->data, ob_data, (size_t)ob_sz);
  61. /* signal command ready to the BPMP */
  62. signal_slave(ch);
  63. mmio_write_32(TEGRA_PRI_ICTLR_BASE + CPU_IEP_FIR_SET,
  64. (1U << INT_SHR_SEM_OUTBOX_FULL));
  65. /* loop until the command is executed */
  66. for (timeout = 0; timeout < BPMP_TIMEOUT; timeout++) {
  67. if (master_acked(ch) == true) {
  68. break;
  69. }
  70. mdelay(1);
  71. }
  72. if (timeout != BPMP_TIMEOUT) {
  73. /* get the command response */
  74. (void)memcpy(ib_data, (const void *)p->data,
  75. (size_t)ib_sz);
  76. /* return error code */
  77. ret = p->code;
  78. /* free this channel */
  79. free_master(ch);
  80. }
  81. }
  82. } else {
  83. /* return error code */
  84. ret = -EINVAL;
  85. }
  86. if (timeout == BPMP_TIMEOUT) {
  87. ERROR("Timed out waiting for bpmp's response\n");
  88. }
  89. return ret;
  90. }
  91. int tegra_bpmp_init(void)
  92. {
  93. uint32_t val, base, timeout = BPMP_TIMEOUT;
  94. unsigned int ch;
  95. int ret = 0;
  96. if (bpmp_init_state == BPMP_INIT_PENDING) {
  97. /* check if the bpmp processor is alive. */
  98. do {
  99. val = mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET);
  100. if (val != SIGN_OF_LIFE) {
  101. mdelay(1);
  102. timeout--;
  103. }
  104. } while ((val != SIGN_OF_LIFE) && (timeout > 0U));
  105. if (val == SIGN_OF_LIFE) {
  106. /* check if clock for the atomics block is enabled */
  107. val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_CLK_ENB_V);
  108. if ((val & CAR_ENABLE_ATOMICS) == 0) {
  109. ERROR("Clock to the atomics block is disabled\n");
  110. }
  111. /* check if the atomics block is out of reset */
  112. val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_CLR_V);
  113. if ((val & CAR_ENABLE_ATOMICS) == CAR_ENABLE_ATOMICS) {
  114. ERROR("Reset to the atomics block is asserted\n");
  115. }
  116. /* base address to get the result from Atomics */
  117. base = TEGRA_ATOMICS_BASE + RESULT0_REG_OFFSET;
  118. /* channel area is setup by BPMP before signaling handshake */
  119. for (ch = 0; ch < NR_CHANNELS; ch++) {
  120. /* issue command to get the channel base address */
  121. mmio_write_32(base, (ch << TRIGGER_ID_SHIFT) |
  122. ATOMIC_CMD_GET);
  123. /* get the base address for the channel */
  124. channel_base[ch] = mmio_read_32(base);
  125. /* increment result register offset */
  126. base += 4U;
  127. }
  128. /* mark state as "initialized" */
  129. bpmp_init_state = BPMP_INIT_COMPLETE;
  130. /* the channel values have to be visible across all cpus */
  131. flush_dcache_range((uint64_t)channel_base,
  132. sizeof(channel_base));
  133. flush_dcache_range((uint64_t)&bpmp_init_state,
  134. sizeof(bpmp_init_state));
  135. INFO("%s: done\n", __func__);
  136. } else {
  137. ERROR("BPMP not powered on\n");
  138. /* bpmp is not present in the system */
  139. bpmp_init_state = BPMP_NOT_PRESENT;
  140. /* communication timed out */
  141. ret = -ETIMEDOUT;
  142. }
  143. }
  144. return ret;
  145. }
  146. void tegra_bpmp_suspend(void)
  147. {
  148. /* freeze the interface */
  149. if (bpmp_init_state == BPMP_INIT_COMPLETE) {
  150. bpmp_init_state = BPMP_SUSPEND_ENTRY;
  151. flush_dcache_range((uint64_t)&bpmp_init_state,
  152. sizeof(bpmp_init_state));
  153. }
  154. }
  155. void tegra_bpmp_resume(void)
  156. {
  157. uint32_t val, timeout = 0;
  158. if (bpmp_init_state == BPMP_SUSPEND_ENTRY) {
  159. /* check if the bpmp processor is alive. */
  160. do {
  161. val = mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET);
  162. if (val != SIGN_OF_LIFE) {
  163. mdelay(1);
  164. timeout++;
  165. }
  166. } while ((val != SIGN_OF_LIFE) && (timeout < BPMP_TIMEOUT));
  167. if (val == SIGN_OF_LIFE) {
  168. INFO("%s: BPMP took %d ms to resume\n", __func__, timeout);
  169. /* mark state as "initialized" */
  170. bpmp_init_state = BPMP_INIT_COMPLETE;
  171. /* state has to be visible across all cpus */
  172. flush_dcache_range((uint64_t)&bpmp_init_state,
  173. sizeof(bpmp_init_state));
  174. } else {
  175. ERROR("BPMP not powered on\n");
  176. }
  177. }
  178. }