si_smc.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 "sid.h"
  28. #include "ppsmc.h"
  29. #include "radeon_ucode.h"
  30. int si_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 si_copy_bytes_to_smc(struct radeon_device *rdev,
  42. u32 smc_start_address,
  43. const u8 *src, u32 byte_count, u32 limit)
  44. {
  45. int ret;
  46. u32 data, original_data, addr, extra_shift;
  47. if (smc_start_address & 3)
  48. return -EINVAL;
  49. if ((smc_start_address + byte_count) > limit)
  50. return -EINVAL;
  51. addr = smc_start_address;
  52. while (byte_count >= 4) {
  53. /* SMC address space is BE */
  54. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  55. ret = si_set_smc_sram_address(rdev, addr, limit);
  56. if (ret)
  57. return ret;
  58. WREG32(SMC_IND_DATA_0, data);
  59. src += 4;
  60. byte_count -= 4;
  61. addr += 4;
  62. }
  63. /* RMW for the final bytes */
  64. if (byte_count > 0) {
  65. data = 0;
  66. ret = si_set_smc_sram_address(rdev, addr, limit);
  67. if (ret)
  68. return ret;
  69. original_data = RREG32(SMC_IND_DATA_0);
  70. extra_shift = 8 * (4 - byte_count);
  71. while (byte_count > 0) {
  72. /* SMC address space is BE */
  73. data = (data << 8) + *src++;
  74. byte_count--;
  75. }
  76. data <<= extra_shift;
  77. data |= (original_data & ~((~0UL) << extra_shift));
  78. ret = si_set_smc_sram_address(rdev, addr, limit);
  79. if (ret)
  80. return ret;
  81. WREG32(SMC_IND_DATA_0, data);
  82. }
  83. return 0;
  84. }
  85. void si_start_smc(struct radeon_device *rdev)
  86. {
  87. u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  88. tmp &= ~RST_REG;
  89. WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
  90. }
  91. void si_reset_smc(struct radeon_device *rdev)
  92. {
  93. u32 tmp;
  94. RREG32(CB_CGTT_SCLK_CTRL);
  95. RREG32(CB_CGTT_SCLK_CTRL);
  96. RREG32(CB_CGTT_SCLK_CTRL);
  97. RREG32(CB_CGTT_SCLK_CTRL);
  98. tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  99. tmp |= RST_REG;
  100. WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
  101. }
  102. int si_program_jump_on_start(struct radeon_device *rdev)
  103. {
  104. static u8 data[] = { 0x0E, 0x00, 0x40, 0x40 };
  105. return si_copy_bytes_to_smc(rdev, 0x0, data, 4, sizeof(data)+1);
  106. }
  107. void si_stop_smc_clock(struct radeon_device *rdev)
  108. {
  109. u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  110. tmp |= CK_DISABLE;
  111. WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
  112. }
  113. void si_start_smc_clock(struct radeon_device *rdev)
  114. {
  115. u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  116. tmp &= ~CK_DISABLE;
  117. WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
  118. }
  119. bool si_is_smc_running(struct radeon_device *rdev)
  120. {
  121. u32 rst = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  122. u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  123. if (!(rst & RST_REG) && !(clk & CK_DISABLE))
  124. return true;
  125. return false;
  126. }
  127. PPSMC_Result si_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
  128. {
  129. u32 tmp;
  130. int i;
  131. if (!si_is_smc_running(rdev))
  132. return PPSMC_Result_Failed;
  133. WREG32(SMC_MESSAGE_0, msg);
  134. for (i = 0; i < rdev->usec_timeout; i++) {
  135. tmp = RREG32(SMC_RESP_0);
  136. if (tmp != 0)
  137. break;
  138. udelay(1);
  139. }
  140. tmp = RREG32(SMC_RESP_0);
  141. return (PPSMC_Result)tmp;
  142. }
  143. PPSMC_Result si_wait_for_smc_inactive(struct radeon_device *rdev)
  144. {
  145. u32 tmp;
  146. int i;
  147. if (!si_is_smc_running(rdev))
  148. return PPSMC_Result_OK;
  149. for (i = 0; i < rdev->usec_timeout; i++) {
  150. tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  151. if ((tmp & CKEN) == 0)
  152. break;
  153. udelay(1);
  154. }
  155. return PPSMC_Result_OK;
  156. }
  157. int si_load_smc_ucode(struct radeon_device *rdev, u32 limit)
  158. {
  159. u32 ucode_start_address;
  160. u32 ucode_size;
  161. const u8 *src;
  162. u32 data;
  163. if (!rdev->smc_fw)
  164. return -EINVAL;
  165. switch (rdev->family) {
  166. case CHIP_TAHITI:
  167. ucode_start_address = TAHITI_SMC_UCODE_START;
  168. ucode_size = TAHITI_SMC_UCODE_SIZE;
  169. break;
  170. case CHIP_PITCAIRN:
  171. ucode_start_address = PITCAIRN_SMC_UCODE_START;
  172. ucode_size = PITCAIRN_SMC_UCODE_SIZE;
  173. break;
  174. case CHIP_VERDE:
  175. ucode_start_address = VERDE_SMC_UCODE_START;
  176. ucode_size = VERDE_SMC_UCODE_SIZE;
  177. break;
  178. case CHIP_OLAND:
  179. ucode_start_address = OLAND_SMC_UCODE_START;
  180. ucode_size = OLAND_SMC_UCODE_SIZE;
  181. break;
  182. case CHIP_HAINAN:
  183. ucode_start_address = HAINAN_SMC_UCODE_START;
  184. ucode_size = HAINAN_SMC_UCODE_SIZE;
  185. break;
  186. default:
  187. DRM_ERROR("unknown asic in smc ucode loader\n");
  188. BUG();
  189. }
  190. if (ucode_size & 3)
  191. return -EINVAL;
  192. src = (const u8 *)rdev->smc_fw->data;
  193. WREG32(SMC_IND_INDEX_0, ucode_start_address);
  194. WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0);
  195. while (ucode_size >= 4) {
  196. /* SMC address space is BE */
  197. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  198. WREG32(SMC_IND_DATA_0, data);
  199. src += 4;
  200. ucode_size -= 4;
  201. }
  202. WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
  203. return 0;
  204. }
  205. int si_read_smc_sram_dword(struct radeon_device *rdev, u32 smc_address,
  206. u32 *value, u32 limit)
  207. {
  208. int ret;
  209. ret = si_set_smc_sram_address(rdev, smc_address, limit);
  210. if (ret)
  211. return ret;
  212. *value = RREG32(SMC_IND_DATA_0);
  213. return 0;
  214. }
  215. int si_write_smc_sram_dword(struct radeon_device *rdev, u32 smc_address,
  216. u32 value, u32 limit)
  217. {
  218. int ret;
  219. ret = si_set_smc_sram_address(rdev, smc_address, limit);
  220. if (ret)
  221. return ret;
  222. WREG32(SMC_IND_DATA_0, value);
  223. return 0;
  224. }