rv770_smc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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 "rv770d.h"
  28. #include "rv770_dpm.h"
  29. #include "rv770_smc.h"
  30. #include "atom.h"
  31. #include "radeon_ucode.h"
  32. #define FIRST_SMC_INT_VECT_REG 0xFFD8
  33. #define FIRST_INT_VECT_S19 0xFFC0
  34. static const u8 rv770_smc_int_vectors[] =
  35. {
  36. 0x08, 0x10, 0x08, 0x10,
  37. 0x08, 0x10, 0x08, 0x10,
  38. 0x08, 0x10, 0x08, 0x10,
  39. 0x08, 0x10, 0x08, 0x10,
  40. 0x08, 0x10, 0x08, 0x10,
  41. 0x08, 0x10, 0x08, 0x10,
  42. 0x08, 0x10, 0x08, 0x10,
  43. 0x08, 0x10, 0x08, 0x10,
  44. 0x08, 0x10, 0x08, 0x10,
  45. 0x08, 0x10, 0x08, 0x10,
  46. 0x08, 0x10, 0x08, 0x10,
  47. 0x08, 0x10, 0x08, 0x10,
  48. 0x08, 0x10, 0x0C, 0xD7,
  49. 0x08, 0x2B, 0x08, 0x10,
  50. 0x03, 0x51, 0x03, 0x51,
  51. 0x03, 0x51, 0x03, 0x51
  52. };
  53. static const u8 rv730_smc_int_vectors[] =
  54. {
  55. 0x08, 0x15, 0x08, 0x15,
  56. 0x08, 0x15, 0x08, 0x15,
  57. 0x08, 0x15, 0x08, 0x15,
  58. 0x08, 0x15, 0x08, 0x15,
  59. 0x08, 0x15, 0x08, 0x15,
  60. 0x08, 0x15, 0x08, 0x15,
  61. 0x08, 0x15, 0x08, 0x15,
  62. 0x08, 0x15, 0x08, 0x15,
  63. 0x08, 0x15, 0x08, 0x15,
  64. 0x08, 0x15, 0x08, 0x15,
  65. 0x08, 0x15, 0x08, 0x15,
  66. 0x08, 0x15, 0x08, 0x15,
  67. 0x08, 0x15, 0x0C, 0xBB,
  68. 0x08, 0x30, 0x08, 0x15,
  69. 0x03, 0x56, 0x03, 0x56,
  70. 0x03, 0x56, 0x03, 0x56
  71. };
  72. static const u8 rv710_smc_int_vectors[] =
  73. {
  74. 0x08, 0x04, 0x08, 0x04,
  75. 0x08, 0x04, 0x08, 0x04,
  76. 0x08, 0x04, 0x08, 0x04,
  77. 0x08, 0x04, 0x08, 0x04,
  78. 0x08, 0x04, 0x08, 0x04,
  79. 0x08, 0x04, 0x08, 0x04,
  80. 0x08, 0x04, 0x08, 0x04,
  81. 0x08, 0x04, 0x08, 0x04,
  82. 0x08, 0x04, 0x08, 0x04,
  83. 0x08, 0x04, 0x08, 0x04,
  84. 0x08, 0x04, 0x08, 0x04,
  85. 0x08, 0x04, 0x08, 0x04,
  86. 0x08, 0x04, 0x0C, 0xCB,
  87. 0x08, 0x1F, 0x08, 0x04,
  88. 0x03, 0x51, 0x03, 0x51,
  89. 0x03, 0x51, 0x03, 0x51
  90. };
  91. static const u8 rv740_smc_int_vectors[] =
  92. {
  93. 0x08, 0x10, 0x08, 0x10,
  94. 0x08, 0x10, 0x08, 0x10,
  95. 0x08, 0x10, 0x08, 0x10,
  96. 0x08, 0x10, 0x08, 0x10,
  97. 0x08, 0x10, 0x08, 0x10,
  98. 0x08, 0x10, 0x08, 0x10,
  99. 0x08, 0x10, 0x08, 0x10,
  100. 0x08, 0x10, 0x08, 0x10,
  101. 0x08, 0x10, 0x08, 0x10,
  102. 0x08, 0x10, 0x08, 0x10,
  103. 0x08, 0x10, 0x08, 0x10,
  104. 0x08, 0x10, 0x08, 0x10,
  105. 0x08, 0x10, 0x0C, 0xD7,
  106. 0x08, 0x2B, 0x08, 0x10,
  107. 0x03, 0x51, 0x03, 0x51,
  108. 0x03, 0x51, 0x03, 0x51
  109. };
  110. static const u8 cedar_smc_int_vectors[] =
  111. {
  112. 0x0B, 0x05, 0x0B, 0x05,
  113. 0x0B, 0x05, 0x0B, 0x05,
  114. 0x0B, 0x05, 0x0B, 0x05,
  115. 0x0B, 0x05, 0x0B, 0x05,
  116. 0x0B, 0x05, 0x0B, 0x05,
  117. 0x0B, 0x05, 0x0B, 0x05,
  118. 0x0B, 0x05, 0x0B, 0x05,
  119. 0x0B, 0x05, 0x0B, 0x05,
  120. 0x0B, 0x05, 0x0B, 0x05,
  121. 0x0B, 0x05, 0x0B, 0x05,
  122. 0x0B, 0x05, 0x0B, 0x05,
  123. 0x0B, 0x05, 0x0B, 0x05,
  124. 0x0B, 0x05, 0x11, 0x8B,
  125. 0x0B, 0x20, 0x0B, 0x05,
  126. 0x04, 0xF6, 0x04, 0xF6,
  127. 0x04, 0xF6, 0x04, 0xF6
  128. };
  129. static const u8 redwood_smc_int_vectors[] =
  130. {
  131. 0x0B, 0x05, 0x0B, 0x05,
  132. 0x0B, 0x05, 0x0B, 0x05,
  133. 0x0B, 0x05, 0x0B, 0x05,
  134. 0x0B, 0x05, 0x0B, 0x05,
  135. 0x0B, 0x05, 0x0B, 0x05,
  136. 0x0B, 0x05, 0x0B, 0x05,
  137. 0x0B, 0x05, 0x0B, 0x05,
  138. 0x0B, 0x05, 0x0B, 0x05,
  139. 0x0B, 0x05, 0x0B, 0x05,
  140. 0x0B, 0x05, 0x0B, 0x05,
  141. 0x0B, 0x05, 0x0B, 0x05,
  142. 0x0B, 0x05, 0x0B, 0x05,
  143. 0x0B, 0x05, 0x11, 0x8B,
  144. 0x0B, 0x20, 0x0B, 0x05,
  145. 0x04, 0xF6, 0x04, 0xF6,
  146. 0x04, 0xF6, 0x04, 0xF6
  147. };
  148. static const u8 juniper_smc_int_vectors[] =
  149. {
  150. 0x0B, 0x05, 0x0B, 0x05,
  151. 0x0B, 0x05, 0x0B, 0x05,
  152. 0x0B, 0x05, 0x0B, 0x05,
  153. 0x0B, 0x05, 0x0B, 0x05,
  154. 0x0B, 0x05, 0x0B, 0x05,
  155. 0x0B, 0x05, 0x0B, 0x05,
  156. 0x0B, 0x05, 0x0B, 0x05,
  157. 0x0B, 0x05, 0x0B, 0x05,
  158. 0x0B, 0x05, 0x0B, 0x05,
  159. 0x0B, 0x05, 0x0B, 0x05,
  160. 0x0B, 0x05, 0x0B, 0x05,
  161. 0x0B, 0x05, 0x0B, 0x05,
  162. 0x0B, 0x05, 0x11, 0x8B,
  163. 0x0B, 0x20, 0x0B, 0x05,
  164. 0x04, 0xF6, 0x04, 0xF6,
  165. 0x04, 0xF6, 0x04, 0xF6
  166. };
  167. static const u8 cypress_smc_int_vectors[] =
  168. {
  169. 0x0B, 0x05, 0x0B, 0x05,
  170. 0x0B, 0x05, 0x0B, 0x05,
  171. 0x0B, 0x05, 0x0B, 0x05,
  172. 0x0B, 0x05, 0x0B, 0x05,
  173. 0x0B, 0x05, 0x0B, 0x05,
  174. 0x0B, 0x05, 0x0B, 0x05,
  175. 0x0B, 0x05, 0x0B, 0x05,
  176. 0x0B, 0x05, 0x0B, 0x05,
  177. 0x0B, 0x05, 0x0B, 0x05,
  178. 0x0B, 0x05, 0x0B, 0x05,
  179. 0x0B, 0x05, 0x0B, 0x05,
  180. 0x0B, 0x05, 0x0B, 0x05,
  181. 0x0B, 0x05, 0x11, 0x8B,
  182. 0x0B, 0x20, 0x0B, 0x05,
  183. 0x04, 0xF6, 0x04, 0xF6,
  184. 0x04, 0xF6, 0x04, 0xF6
  185. };
  186. int rv770_set_smc_sram_address(struct radeon_device *rdev,
  187. u16 smc_address, u16 limit)
  188. {
  189. u32 addr;
  190. if (smc_address & 3)
  191. return -EINVAL;
  192. if ((smc_address + 3) > limit)
  193. return -EINVAL;
  194. addr = smc_address;
  195. addr |= SMC_SRAM_AUTO_INC_DIS;
  196. WREG32(SMC_SRAM_ADDR, addr);
  197. return 0;
  198. }
  199. int rv770_copy_bytes_to_smc(struct radeon_device *rdev,
  200. u16 smc_start_address, const u8 *src,
  201. u16 byte_count, u16 limit)
  202. {
  203. u32 data, original_data, extra_shift;
  204. u16 addr;
  205. int ret;
  206. if (smc_start_address & 3)
  207. return -EINVAL;
  208. if ((smc_start_address + byte_count) > limit)
  209. return -EINVAL;
  210. addr = smc_start_address;
  211. while (byte_count >= 4) {
  212. /* SMC address space is BE */
  213. data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
  214. ret = rv770_set_smc_sram_address(rdev, addr, limit);
  215. if (ret)
  216. return ret;
  217. WREG32(SMC_SRAM_DATA, data);
  218. src += 4;
  219. byte_count -= 4;
  220. addr += 4;
  221. }
  222. /* RMW for final bytes */
  223. if (byte_count > 0) {
  224. data = 0;
  225. ret = rv770_set_smc_sram_address(rdev, addr, limit);
  226. if (ret)
  227. return ret;
  228. original_data = RREG32(SMC_SRAM_DATA);
  229. extra_shift = 8 * (4 - byte_count);
  230. while (byte_count > 0) {
  231. /* SMC address space is BE */
  232. data = (data << 8) + *src++;
  233. byte_count--;
  234. }
  235. data <<= extra_shift;
  236. data |= (original_data & ~((~0UL) << extra_shift));
  237. ret = rv770_set_smc_sram_address(rdev, addr, limit);
  238. if (ret)
  239. return ret;
  240. WREG32(SMC_SRAM_DATA, data);
  241. }
  242. return 0;
  243. }
  244. static int rv770_program_interrupt_vectors(struct radeon_device *rdev,
  245. u32 smc_first_vector, const u8 *src,
  246. u32 byte_count)
  247. {
  248. u32 tmp, i;
  249. if (byte_count % 4)
  250. return -EINVAL;
  251. if (smc_first_vector < FIRST_SMC_INT_VECT_REG) {
  252. tmp = FIRST_SMC_INT_VECT_REG - smc_first_vector;
  253. if (tmp > byte_count)
  254. return 0;
  255. byte_count -= tmp;
  256. src += tmp;
  257. smc_first_vector = FIRST_SMC_INT_VECT_REG;
  258. }
  259. for (i = 0; i < byte_count; i += 4) {
  260. /* SMC address space is BE */
  261. tmp = (src[i] << 24) | (src[i + 1] << 16) | (src[i + 2] << 8) | src[i + 3];
  262. WREG32(SMC_ISR_FFD8_FFDB + i, tmp);
  263. }
  264. return 0;
  265. }
  266. void rv770_start_smc(struct radeon_device *rdev)
  267. {
  268. WREG32_P(SMC_IO, SMC_RST_N, ~SMC_RST_N);
  269. }
  270. void rv770_reset_smc(struct radeon_device *rdev)
  271. {
  272. WREG32_P(SMC_IO, 0, ~SMC_RST_N);
  273. }
  274. void rv770_stop_smc_clock(struct radeon_device *rdev)
  275. {
  276. WREG32_P(SMC_IO, 0, ~SMC_CLK_EN);
  277. }
  278. void rv770_start_smc_clock(struct radeon_device *rdev)
  279. {
  280. WREG32_P(SMC_IO, SMC_CLK_EN, ~SMC_CLK_EN);
  281. }
  282. bool rv770_is_smc_running(struct radeon_device *rdev)
  283. {
  284. u32 tmp;
  285. tmp = RREG32(SMC_IO);
  286. if ((tmp & SMC_RST_N) && (tmp & SMC_CLK_EN))
  287. return true;
  288. else
  289. return false;
  290. }
  291. PPSMC_Result rv770_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
  292. {
  293. u32 tmp;
  294. int i;
  295. PPSMC_Result result;
  296. if (!rv770_is_smc_running(rdev))
  297. return PPSMC_Result_Failed;
  298. WREG32_P(SMC_MSG, HOST_SMC_MSG(msg), ~HOST_SMC_MSG_MASK);
  299. for (i = 0; i < rdev->usec_timeout; i++) {
  300. tmp = RREG32(SMC_MSG) & HOST_SMC_RESP_MASK;
  301. tmp >>= HOST_SMC_RESP_SHIFT;
  302. if (tmp != 0)
  303. break;
  304. udelay(1);
  305. }
  306. tmp = RREG32(SMC_MSG) & HOST_SMC_RESP_MASK;
  307. tmp >>= HOST_SMC_RESP_SHIFT;
  308. result = (PPSMC_Result)tmp;
  309. return result;
  310. }
  311. PPSMC_Result rv770_wait_for_smc_inactive(struct radeon_device *rdev)
  312. {
  313. int i;
  314. PPSMC_Result result = PPSMC_Result_OK;
  315. if (!rv770_is_smc_running(rdev))
  316. return result;
  317. for (i = 0; i < rdev->usec_timeout; i++) {
  318. if (RREG32(SMC_IO) & SMC_STOP_MODE)
  319. break;
  320. udelay(1);
  321. }
  322. return result;
  323. }
  324. static void rv770_clear_smc_sram(struct radeon_device *rdev, u16 limit)
  325. {
  326. u16 i;
  327. for (i = 0; i < limit; i += 4) {
  328. rv770_set_smc_sram_address(rdev, i, limit);
  329. WREG32(SMC_SRAM_DATA, 0);
  330. }
  331. }
  332. int rv770_load_smc_ucode(struct radeon_device *rdev,
  333. u16 limit)
  334. {
  335. int ret;
  336. const u8 *int_vect;
  337. u16 int_vect_start_address;
  338. u16 int_vect_size;
  339. const u8 *ucode_data;
  340. u16 ucode_start_address;
  341. u16 ucode_size;
  342. if (!rdev->smc_fw)
  343. return -EINVAL;
  344. rv770_clear_smc_sram(rdev, limit);
  345. switch (rdev->family) {
  346. case CHIP_RV770:
  347. ucode_start_address = RV770_SMC_UCODE_START;
  348. ucode_size = RV770_SMC_UCODE_SIZE;
  349. int_vect = (const u8 *)&rv770_smc_int_vectors;
  350. int_vect_start_address = RV770_SMC_INT_VECTOR_START;
  351. int_vect_size = RV770_SMC_INT_VECTOR_SIZE;
  352. break;
  353. case CHIP_RV730:
  354. ucode_start_address = RV730_SMC_UCODE_START;
  355. ucode_size = RV730_SMC_UCODE_SIZE;
  356. int_vect = (const u8 *)&rv730_smc_int_vectors;
  357. int_vect_start_address = RV730_SMC_INT_VECTOR_START;
  358. int_vect_size = RV730_SMC_INT_VECTOR_SIZE;
  359. break;
  360. case CHIP_RV710:
  361. ucode_start_address = RV710_SMC_UCODE_START;
  362. ucode_size = RV710_SMC_UCODE_SIZE;
  363. int_vect = (const u8 *)&rv710_smc_int_vectors;
  364. int_vect_start_address = RV710_SMC_INT_VECTOR_START;
  365. int_vect_size = RV710_SMC_INT_VECTOR_SIZE;
  366. break;
  367. case CHIP_RV740:
  368. ucode_start_address = RV740_SMC_UCODE_START;
  369. ucode_size = RV740_SMC_UCODE_SIZE;
  370. int_vect = (const u8 *)&rv740_smc_int_vectors;
  371. int_vect_start_address = RV740_SMC_INT_VECTOR_START;
  372. int_vect_size = RV740_SMC_INT_VECTOR_SIZE;
  373. break;
  374. case CHIP_CEDAR:
  375. ucode_start_address = CEDAR_SMC_UCODE_START;
  376. ucode_size = CEDAR_SMC_UCODE_SIZE;
  377. int_vect = (const u8 *)&cedar_smc_int_vectors;
  378. int_vect_start_address = CEDAR_SMC_INT_VECTOR_START;
  379. int_vect_size = CEDAR_SMC_INT_VECTOR_SIZE;
  380. break;
  381. case CHIP_REDWOOD:
  382. ucode_start_address = REDWOOD_SMC_UCODE_START;
  383. ucode_size = REDWOOD_SMC_UCODE_SIZE;
  384. int_vect = (const u8 *)&redwood_smc_int_vectors;
  385. int_vect_start_address = REDWOOD_SMC_INT_VECTOR_START;
  386. int_vect_size = REDWOOD_SMC_INT_VECTOR_SIZE;
  387. break;
  388. case CHIP_JUNIPER:
  389. ucode_start_address = JUNIPER_SMC_UCODE_START;
  390. ucode_size = JUNIPER_SMC_UCODE_SIZE;
  391. int_vect = (const u8 *)&juniper_smc_int_vectors;
  392. int_vect_start_address = JUNIPER_SMC_INT_VECTOR_START;
  393. int_vect_size = JUNIPER_SMC_INT_VECTOR_SIZE;
  394. break;
  395. case CHIP_CYPRESS:
  396. case CHIP_HEMLOCK:
  397. ucode_start_address = CYPRESS_SMC_UCODE_START;
  398. ucode_size = CYPRESS_SMC_UCODE_SIZE;
  399. int_vect = (const u8 *)&cypress_smc_int_vectors;
  400. int_vect_start_address = CYPRESS_SMC_INT_VECTOR_START;
  401. int_vect_size = CYPRESS_SMC_INT_VECTOR_SIZE;
  402. break;
  403. default:
  404. DRM_ERROR("unknown asic in smc ucode loader\n");
  405. BUG();
  406. }
  407. /* load the ucode */
  408. ucode_data = (const u8 *)rdev->smc_fw->data;
  409. ret = rv770_copy_bytes_to_smc(rdev, ucode_start_address,
  410. ucode_data, ucode_size, limit);
  411. if (ret)
  412. return ret;
  413. /* set up the int vectors */
  414. ret = rv770_program_interrupt_vectors(rdev, int_vect_start_address,
  415. int_vect, int_vect_size);
  416. if (ret)
  417. return ret;
  418. return 0;
  419. }
  420. int rv770_read_smc_sram_dword(struct radeon_device *rdev,
  421. u16 smc_address, u32 *value, u16 limit)
  422. {
  423. int ret;
  424. ret = rv770_set_smc_sram_address(rdev, smc_address, limit);
  425. if (ret)
  426. return ret;
  427. *value = RREG32(SMC_SRAM_DATA);
  428. return 0;
  429. }
  430. int rv770_write_smc_sram_dword(struct radeon_device *rdev,
  431. u16 smc_address, u32 value, u16 limit)
  432. {
  433. int ret;
  434. ret = rv770_set_smc_sram_address(rdev, smc_address, limit);
  435. if (ret)
  436. return ret;
  437. WREG32(SMC_SRAM_DATA, value);
  438. return 0;
  439. }