ci_smc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #include "ci_dpm.h"
  31. static int ci_set_smc_sram_address(struct radeon_device *rdev,
  32. u32 smc_address, u32 limit)
  33. {
  34. if (smc_address & 3)
  35. return -EINVAL;
  36. if ((smc_address + 3) > limit)
  37. return -EINVAL;
  38. WREG32(SMC_IND_INDEX_0, smc_address);
  39. WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
  40. return 0;
  41. }
  42. int ci_copy_bytes_to_smc(struct radeon_device *rdev,
  43. u32 smc_start_address,
  44. const u8 *src, u32 byte_count, u32 limit)
  45. {
  46. unsigned long flags;
  47. u32 data, original_data;
  48. u32 addr;
  49. u32 extra_shift;
  50. int ret = 0;
  51. if (smc_start_address & 3)
  52. return -EINVAL;
  53. if ((smc_start_address + byte_count) > limit)
  54. return -EINVAL;
  55. addr = smc_start_address;
  56. spin_lock_irqsave(&rdev->smc_idx_lock, flags);
  57. while (byte_count >= 4) {
  58. /* SMC address space is BE */
  59. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  60. ret = ci_set_smc_sram_address(rdev, addr, limit);
  61. if (ret)
  62. goto done;
  63. WREG32(SMC_IND_DATA_0, data);
  64. src += 4;
  65. byte_count -= 4;
  66. addr += 4;
  67. }
  68. /* RMW for the final bytes */
  69. if (byte_count > 0) {
  70. data = 0;
  71. ret = ci_set_smc_sram_address(rdev, addr, limit);
  72. if (ret)
  73. goto done;
  74. original_data = RREG32(SMC_IND_DATA_0);
  75. extra_shift = 8 * (4 - byte_count);
  76. while (byte_count > 0) {
  77. data = (data << 8) + *src++;
  78. byte_count--;
  79. }
  80. data <<= extra_shift;
  81. data |= (original_data & ~((~0UL) << extra_shift));
  82. ret = ci_set_smc_sram_address(rdev, addr, limit);
  83. if (ret)
  84. goto done;
  85. WREG32(SMC_IND_DATA_0, data);
  86. }
  87. done:
  88. spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
  89. return ret;
  90. }
  91. void ci_start_smc(struct radeon_device *rdev)
  92. {
  93. u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  94. tmp &= ~RST_REG;
  95. WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
  96. }
  97. void ci_reset_smc(struct radeon_device *rdev)
  98. {
  99. u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
  100. tmp |= RST_REG;
  101. WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
  102. }
  103. int ci_program_jump_on_start(struct radeon_device *rdev)
  104. {
  105. static const u8 data[] = { 0xE0, 0x00, 0x80, 0x40 };
  106. return ci_copy_bytes_to_smc(rdev, 0x0, data, 4, sizeof(data)+1);
  107. }
  108. void ci_stop_smc_clock(struct radeon_device *rdev)
  109. {
  110. u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  111. tmp |= CK_DISABLE;
  112. WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
  113. }
  114. void ci_start_smc_clock(struct radeon_device *rdev)
  115. {
  116. u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  117. tmp &= ~CK_DISABLE;
  118. WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
  119. }
  120. bool ci_is_smc_running(struct radeon_device *rdev)
  121. {
  122. u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  123. u32 pc_c = RREG32_SMC(SMC_PC_C);
  124. if (!(clk & CK_DISABLE) && (0x20100 <= pc_c))
  125. return true;
  126. return false;
  127. }
  128. PPSMC_Result ci_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
  129. {
  130. u32 tmp;
  131. int i;
  132. if (!ci_is_smc_running(rdev))
  133. return PPSMC_Result_Failed;
  134. WREG32(SMC_MESSAGE_0, msg);
  135. for (i = 0; i < rdev->usec_timeout; i++) {
  136. tmp = RREG32(SMC_RESP_0);
  137. if (tmp != 0)
  138. break;
  139. udelay(1);
  140. }
  141. tmp = RREG32(SMC_RESP_0);
  142. return (PPSMC_Result)tmp;
  143. }
  144. #if 0
  145. PPSMC_Result ci_wait_for_smc_inactive(struct radeon_device *rdev)
  146. {
  147. u32 tmp;
  148. int i;
  149. if (!ci_is_smc_running(rdev))
  150. return PPSMC_Result_OK;
  151. for (i = 0; i < rdev->usec_timeout; i++) {
  152. tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
  153. if ((tmp & CKEN) == 0)
  154. break;
  155. udelay(1);
  156. }
  157. return PPSMC_Result_OK;
  158. }
  159. #endif
  160. int ci_load_smc_ucode(struct radeon_device *rdev, u32 limit)
  161. {
  162. unsigned long flags;
  163. u32 ucode_start_address;
  164. u32 ucode_size;
  165. const u8 *src;
  166. u32 data;
  167. if (!rdev->smc_fw)
  168. return -EINVAL;
  169. if (rdev->new_fw) {
  170. const struct smc_firmware_header_v1_0 *hdr =
  171. (const struct smc_firmware_header_v1_0 *)rdev->smc_fw->data;
  172. radeon_ucode_print_smc_hdr(&hdr->header);
  173. ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
  174. ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
  175. src = (const u8 *)
  176. (rdev->smc_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
  177. } else {
  178. switch (rdev->family) {
  179. case CHIP_BONAIRE:
  180. ucode_start_address = BONAIRE_SMC_UCODE_START;
  181. ucode_size = BONAIRE_SMC_UCODE_SIZE;
  182. break;
  183. case CHIP_HAWAII:
  184. ucode_start_address = HAWAII_SMC_UCODE_START;
  185. ucode_size = HAWAII_SMC_UCODE_SIZE;
  186. break;
  187. default:
  188. DRM_ERROR("unknown asic in smc ucode loader\n");
  189. BUG();
  190. }
  191. src = (const u8 *)rdev->smc_fw->data;
  192. }
  193. if (ucode_size & 3)
  194. return -EINVAL;
  195. spin_lock_irqsave(&rdev->smc_idx_lock, flags);
  196. WREG32(SMC_IND_INDEX_0, ucode_start_address);
  197. WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0);
  198. while (ucode_size >= 4) {
  199. /* SMC address space is BE */
  200. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  201. WREG32(SMC_IND_DATA_0, data);
  202. src += 4;
  203. ucode_size -= 4;
  204. }
  205. WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
  206. spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
  207. return 0;
  208. }
  209. int ci_read_smc_sram_dword(struct radeon_device *rdev,
  210. u32 smc_address, u32 *value, u32 limit)
  211. {
  212. unsigned long flags;
  213. int ret;
  214. spin_lock_irqsave(&rdev->smc_idx_lock, flags);
  215. ret = ci_set_smc_sram_address(rdev, smc_address, limit);
  216. if (ret == 0)
  217. *value = RREG32(SMC_IND_DATA_0);
  218. spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
  219. return ret;
  220. }
  221. int ci_write_smc_sram_dword(struct radeon_device *rdev,
  222. u32 smc_address, u32 value, u32 limit)
  223. {
  224. unsigned long flags;
  225. int ret;
  226. spin_lock_irqsave(&rdev->smc_idx_lock, flags);
  227. ret = ci_set_smc_sram_address(rdev, smc_address, limit);
  228. if (ret == 0)
  229. WREG32(SMC_IND_DATA_0, value);
  230. spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
  231. return ret;
  232. }