plat_pm_helpers.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2024, Rockchip, Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <errno.h>
  8. #include <arch_helpers.h>
  9. #include <bl31/bl31.h>
  10. #include <common/debug.h>
  11. #include <drivers/console.h>
  12. #include <drivers/delay_timer.h>
  13. #include <lib/mmio.h>
  14. #include <plat/common/platform.h>
  15. #include <platform_def.h>
  16. #include <plat_pm_helpers.h>
  17. #define ROCKCHIP_PM_REG_REGION_MEM_LEN (ROCKCHIP_PM_REG_REGION_MEM_SIZE / sizeof(uint32_t))
  18. /* REG region */
  19. #define RGN_LEN(_rgn) (((_rgn)->end - (_rgn)->start) / (_rgn)->stride + 1)
  20. #ifndef ROCKCHIP_PM_REG_REGION_MEM_SIZE
  21. #define ROCKCHIP_PM_REG_REGION_MEM_SIZE 0
  22. #endif
  23. #ifdef ROCKCHIP_REG_RGN_MEM_BASE
  24. static uint32_t *region_mem = (uint32_t *)ROCKCHIP_REG_RGN_MEM_BASE;
  25. #else
  26. static uint32_t region_mem[ROCKCHIP_PM_REG_REGION_MEM_LEN];
  27. #endif
  28. static int region_mem_idx;
  29. static int alloc_region_mem(uint32_t *buf, int max_len,
  30. struct reg_region *rgns, uint32_t rgn_num)
  31. {
  32. int i;
  33. int total_len = 0, len = 0;
  34. struct reg_region *r = rgns;
  35. assert(buf && rgns && rgn_num);
  36. for (i = 0; i < rgn_num; i++, r++) {
  37. if (total_len < max_len)
  38. r->buf = &buf[total_len];
  39. len = RGN_LEN(r);
  40. total_len += len;
  41. }
  42. if (total_len > max_len) {
  43. ERROR("%s The buffer remain length:%d is too small for region:0x%x, at least %d\n",
  44. __func__, max_len, rgns[0].start, total_len);
  45. panic();
  46. }
  47. return total_len;
  48. }
  49. /**
  50. * Alloc memory to reg_region->buf from region_mem.
  51. * @rgns - struct reg_region array.
  52. * @rgn_num - struct reg_region array length.
  53. */
  54. void rockchip_alloc_region_mem(struct reg_region *rgns, uint32_t rgn_num)
  55. {
  56. int max_len = 0, len;
  57. assert(rgns && rgn_num);
  58. max_len = ROCKCHIP_PM_REG_REGION_MEM_LEN - region_mem_idx;
  59. len = alloc_region_mem(region_mem + region_mem_idx, max_len,
  60. rgns, rgn_num);
  61. region_mem_idx += len;
  62. }
  63. /**
  64. * Save (reg_region->start ~ reg_region->end) to reg_region->buf.
  65. * @rgns - struct reg_region array.
  66. * @rgn_num - struct reg_region array length.
  67. */
  68. void rockchip_reg_rgn_save(struct reg_region *rgns, uint32_t rgn_num)
  69. {
  70. struct reg_region *r;
  71. uint32_t addr;
  72. int i, j;
  73. assert(rgns && rgn_num);
  74. for (i = 0; i < rgn_num; i++) {
  75. r = &rgns[i];
  76. for (j = 0, addr = r->start; addr <= r->end; addr += r->stride, j++)
  77. r->buf[j] = mmio_read_32(addr);
  78. }
  79. }
  80. /**
  81. * Restore reg_region->buf to (reg_region->start ~ reg_region->end).
  82. * @rgns - struct reg_region array.
  83. * @rgn_num - struct reg_region array length.
  84. */
  85. void rockchip_reg_rgn_restore(struct reg_region *rgns, uint32_t rgn_num)
  86. {
  87. struct reg_region *r;
  88. uint32_t addr;
  89. int i, j;
  90. assert(rgns && rgn_num);
  91. for (i = 0; i < rgn_num; i++) {
  92. r = &rgns[i];
  93. for (j = 0, addr = r->start; addr <= r->end; addr += r->stride, j++)
  94. mmio_write_32(addr, r->buf[j] | r->wmsk);
  95. dsb();
  96. }
  97. }
  98. /**
  99. * Restore reg_region->buf to (reg_region->start ~ reg_region->end) reversely.
  100. * @rgns - struct reg_region array.
  101. * @rgn_num - struct reg_region array length.
  102. */
  103. void rockchip_reg_rgn_restore_reverse(struct reg_region *rgns, uint32_t rgn_num)
  104. {
  105. struct reg_region *r;
  106. uint32_t addr;
  107. int i, j;
  108. assert(rgns && rgn_num);
  109. for (i = rgn_num - 1; i >= 0; i--) {
  110. r = &rgns[i];
  111. j = RGN_LEN(r) - 1;
  112. for (addr = r->end; addr >= r->start; addr -= r->stride, j--)
  113. mmio_write_32(addr, r->buf[j] | r->wmsk);
  114. dsb();
  115. }
  116. }
  117. static void rockchip_print_hex(uint32_t val)
  118. {
  119. int i;
  120. unsigned char tmp;
  121. putchar('0');
  122. putchar('x');
  123. for (i = 0; i < 8; val <<= 4, ++i) {
  124. tmp = (val & 0xf0000000) >> 28;
  125. if (tmp < 10)
  126. putchar('0' + tmp);
  127. else
  128. putchar('a' + tmp - 10);
  129. }
  130. }
  131. /**
  132. * Dump registers (base + start_offset ~ base + end_offset)
  133. * @base - the base addr of the register.
  134. * @start_offset - the start offset to dump.
  135. * @end_offset - the end offset to dump.
  136. * @stride - the stride of the registers.
  137. */
  138. void rockchip_regs_dump(uint32_t base,
  139. uint32_t start_offset,
  140. uint32_t end_offset,
  141. uint32_t stride)
  142. {
  143. uint32_t i;
  144. for (i = start_offset; i <= end_offset; i += stride) {
  145. if ((i - start_offset) % 16 == 0) {
  146. putchar('\n');
  147. rockchip_print_hex(base + i);
  148. putchar(':');
  149. putchar(' ');
  150. putchar(' ');
  151. putchar(' ');
  152. putchar(' ');
  153. }
  154. rockchip_print_hex(mmio_read_32(base + i));
  155. putchar(' ');
  156. putchar(' ');
  157. putchar(' ');
  158. putchar(' ');
  159. }
  160. putchar('\n');
  161. }
  162. /**
  163. * Dump reg regions
  164. * @rgns - struct reg_region array.
  165. * @rgn_num - struct reg_region array length.
  166. */
  167. void rockchip_dump_reg_rgns(struct reg_region *rgns, uint32_t rgn_num)
  168. {
  169. struct reg_region *r;
  170. int i;
  171. assert(rgns && rgn_num);
  172. for (i = 0; i < rgn_num; i++) {
  173. r = &rgns[i];
  174. rockchip_regs_dump(0x0, r->start, r->end, r->stride);
  175. }
  176. }