si_smc.c 7.2 KB

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