ci_smc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright 2011 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Alex Deucher
  23. */
  24. #include <linux/firmware.h>
  25. #include "drmP.h"
  26. #include "radeon.h"
  27. #include "cikd.h"
  28. #include "ppsmc.h"
  29. #include "radeon_ucode.h"
  30. static int ci_set_smc_sram_address(struct radeon_device *rdev,
  31. u32 smc_address, u32 limit)
  32. {
  33. if (smc_address & 3)
  34. return -EINVAL;
  35. if ((smc_address + 3) > limit)
  36. return -EINVAL;
  37. WREG32(SMC_IND_INDEX_0, smc_address);
  38. WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
  39. return 0;
  40. }
  41. int ci_copy_bytes_to_smc(struct radeon_device *rdev,
  42. u32 smc_start_address,
  43. const u8 *src, u32 byte_count, u32 limit)
  44. {
  45. u32 data, original_data;
  46. u32 addr;
  47. u32 extra_shift;
  48. int ret;
  49. if (smc_start_address & 3)
  50. return -EINVAL;
  51. if ((smc_start_address + byte_count) > limit)
  52. return -EINVAL;
  53. addr = smc_start_address;
  54. while (byte_count >= 4) {
  55. /* SMC address space is BE */
  56. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  57. ret = ci_set_smc_sram_address(rdev, addr, limit);
  58. if (ret)
  59. return ret;
  60. WREG32(SMC_IND_DATA_0, data);
  61. src += 4;
  62. byte_count -= 4;
  63. addr += 4;
  64. }
  65. /* RMW for the final bytes */
  66. if (byte_count > 0) {
  67. data = 0;
  68. ret = ci_set_smc_sram_address(rdev, addr, limit);
  69. if (ret)
  70. return ret;
  71. original_data = RREG32(SMC_IND_DATA_0);
  72. extra_shift = 8 * (4 - byte_count);
  73. while (byte_count > 0) {
  74. data = (data << 8) + *src++;
  75. byte_count--;
  76. }
  77. data <<= extra_shift;
  78. data |= (original_data & ~((~0UL) << extra_shift));
  79. ret = ci_set_smc_sram_address(rdev, addr, limit);
  80. if (ret)
  81. return ret;
  82. WREG32(SMC_IND_DATA_0, data);
  83. }
  84. return 0;
  85. }
  86. void ci_start_smc(struct radeon_device *rdev)
  87. {
  88. u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  89. tmp &= ~RST_REG;
  90. WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
  91. }
  92. void ci_reset_smc(struct radeon_device *rdev)
  93. {
  94. u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  95. tmp |= RST_REG;
  96. WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
  97. }
  98. int ci_program_jump_on_start(struct radeon_device *rdev)
  99. {
  100. static u8 data[] = { 0xE0, 0x00, 0x80, 0x40 };
  101. return ci_copy_bytes_to_smc(rdev, 0x0, data, 4, sizeof(data)+1);
  102. }
  103. void ci_stop_smc_clock(struct radeon_device *rdev)
  104. {
  105. u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  106. tmp |= CK_DISABLE;
  107. WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
  108. }
  109. void ci_start_smc_clock(struct radeon_device *rdev)
  110. {
  111. u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  112. tmp &= ~CK_DISABLE;
  113. WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
  114. }
  115. bool ci_is_smc_running(struct radeon_device *rdev)
  116. {
  117. u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  118. u32 pc_c = RREG32_SMC(SMC_PC_C);
  119. if (!(clk & CK_DISABLE) && (0x20100 <= pc_c))
  120. return true;
  121. return false;
  122. }
  123. PPSMC_Result ci_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
  124. {
  125. u32 tmp;
  126. int i;
  127. if (!ci_is_smc_running(rdev))
  128. return PPSMC_Result_Failed;
  129. WREG32(SMC_MESSAGE_0, msg);
  130. for (i = 0; i < rdev->usec_timeout; i++) {
  131. tmp = RREG32(SMC_RESP_0);
  132. if (tmp != 0)
  133. break;
  134. udelay(1);
  135. }
  136. tmp = RREG32(SMC_RESP_0);
  137. return (PPSMC_Result)tmp;
  138. }
  139. PPSMC_Result ci_wait_for_smc_inactive(struct radeon_device *rdev)
  140. {
  141. u32 tmp;
  142. int i;
  143. if (!ci_is_smc_running(rdev))
  144. return PPSMC_Result_OK;
  145. for (i = 0; i < rdev->usec_timeout; i++) {
  146. tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  147. if ((tmp & CKEN) == 0)
  148. break;
  149. udelay(1);
  150. }
  151. return PPSMC_Result_OK;
  152. }
  153. int ci_load_smc_ucode(struct radeon_device *rdev, u32 limit)
  154. {
  155. u32 ucode_start_address;
  156. u32 ucode_size;
  157. const u8 *src;
  158. u32 data;
  159. if (!rdev->smc_fw)
  160. return -EINVAL;
  161. switch (rdev->family) {
  162. case CHIP_BONAIRE:
  163. ucode_start_address = BONAIRE_SMC_UCODE_START;
  164. ucode_size = BONAIRE_SMC_UCODE_SIZE;
  165. break;
  166. default:
  167. DRM_ERROR("unknown asic in smc ucode loader\n");
  168. BUG();
  169. }
  170. if (ucode_size & 3)
  171. return -EINVAL;
  172. src = (const u8 *)rdev->smc_fw->data;
  173. WREG32(SMC_IND_INDEX_0, ucode_start_address);
  174. WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0);
  175. while (ucode_size >= 4) {
  176. /* SMC address space is BE */
  177. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  178. WREG32(SMC_IND_DATA_0, data);
  179. src += 4;
  180. ucode_size -= 4;
  181. }
  182. WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
  183. return 0;
  184. }
  185. int ci_read_smc_sram_dword(struct radeon_device *rdev,
  186. u32 smc_address, u32 *value, u32 limit)
  187. {
  188. int ret;
  189. ret = ci_set_smc_sram_address(rdev, smc_address, limit);
  190. if (ret)
  191. return ret;
  192. *value = RREG32(SMC_IND_DATA_0);
  193. return 0;
  194. }
  195. int ci_write_smc_sram_dword(struct radeon_device *rdev,
  196. u32 smc_address, u32 value, u32 limit)
  197. {
  198. int ret;
  199. ret = ci_set_smc_sram_address(rdev, smc_address, limit);
  200. if (ret)
  201. return ret;
  202. WREG32(SMC_IND_DATA_0, value);
  203. return 0;
  204. }