firmware-zynqmp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx Zynq MPSoC Firmware driver
  4. *
  5. * Copyright (C) 2018-2019 Xilinx, Inc.
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <log.h>
  10. #include <zynqmp_firmware.h>
  11. #include <asm/cache.h>
  12. #include <asm/ptrace.h>
  13. #if defined(CONFIG_ZYNQMP_IPI)
  14. #include <mailbox.h>
  15. #include <asm/arch/sys_proto.h>
  16. #define PMUFW_PAYLOAD_ARG_CNT 8
  17. #define XST_PM_NO_ACCESS 2002L
  18. struct zynqmp_power {
  19. struct mbox_chan tx_chan;
  20. struct mbox_chan rx_chan;
  21. } zynqmp_power;
  22. static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
  23. {
  24. struct zynqmp_ipi_msg msg;
  25. int ret;
  26. if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
  27. res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
  28. return -EINVAL;
  29. if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
  30. return -EINVAL;
  31. debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
  32. msg.buf = (u32 *)req;
  33. msg.len = req_len;
  34. ret = mbox_send(&zynqmp_power.tx_chan, &msg);
  35. if (ret) {
  36. debug("%s: Sending message failed\n", __func__);
  37. return ret;
  38. }
  39. msg.buf = res;
  40. msg.len = res_maxlen;
  41. ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
  42. if (ret)
  43. debug("%s: Receiving message failed\n", __func__);
  44. return ret;
  45. }
  46. unsigned int zynqmp_firmware_version(void)
  47. {
  48. int ret;
  49. u32 ret_payload[PAYLOAD_ARG_CNT];
  50. static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
  51. /*
  52. * Get PMU version only once and later
  53. * just return stored values instead of
  54. * asking PMUFW again.
  55. **/
  56. if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
  57. ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
  58. ret_payload);
  59. if (ret)
  60. panic("PMUFW is not found - Please load it!\n");
  61. pm_api_version = ret_payload[1];
  62. if (pm_api_version < ZYNQMP_PM_VERSION)
  63. panic("PMUFW version error. Expected: v%d.%d\n",
  64. ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
  65. }
  66. return pm_api_version;
  67. };
  68. /**
  69. * Send a configuration object to the PMU firmware.
  70. *
  71. * @cfg_obj: Pointer to the configuration object
  72. * @size: Size of @cfg_obj in bytes
  73. */
  74. void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
  75. {
  76. int err;
  77. u32 ret_payload[PAYLOAD_ARG_CNT];
  78. printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
  79. err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
  80. 0, ret_payload);
  81. if (err == XST_PM_NO_ACCESS) {
  82. printf("PMUFW no permission to change config object\n");
  83. return;
  84. }
  85. if (err)
  86. printf("Cannot load PMUFW configuration object (%d)\n", err);
  87. if (ret_payload[0])
  88. printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
  89. if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
  90. panic("PMUFW config object loading failed in EL3\n");
  91. }
  92. static int zynqmp_power_probe(struct udevice *dev)
  93. {
  94. int ret;
  95. debug("%s, (dev=%p)\n", __func__, dev);
  96. ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
  97. if (ret) {
  98. debug("%s: Cannot find tx mailbox\n", __func__);
  99. return ret;
  100. }
  101. ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
  102. if (ret) {
  103. debug("%s: Cannot find rx mailbox\n", __func__);
  104. return ret;
  105. }
  106. ret = zynqmp_firmware_version();
  107. printf("PMUFW:\tv%d.%d\n",
  108. ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
  109. ret & ZYNQMP_PM_VERSION_MINOR_MASK);
  110. return 0;
  111. };
  112. static const struct udevice_id zynqmp_power_ids[] = {
  113. { .compatible = "xlnx,zynqmp-power" },
  114. { }
  115. };
  116. U_BOOT_DRIVER(zynqmp_power) = {
  117. .name = "zynqmp_power",
  118. .id = UCLASS_FIRMWARE,
  119. .of_match = zynqmp_power_ids,
  120. .probe = zynqmp_power_probe,
  121. };
  122. #endif
  123. int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
  124. u32 arg3, u32 *ret_payload)
  125. {
  126. debug("%s at EL%d, API ID: 0x%0x\n", __func__, current_el(), api_id);
  127. if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
  128. #if defined(CONFIG_ZYNQMP_IPI)
  129. /*
  130. * Use fixed payload and arg size as the EL2 call. The firmware
  131. * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
  132. * firmware API is limited by the SMC call size
  133. */
  134. u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
  135. if (api_id == PM_FPGA_LOAD) {
  136. /* Swap addr_hi/low because of incompatibility */
  137. u32 temp = regs[1];
  138. regs[1] = regs[2];
  139. regs[2] = temp;
  140. }
  141. ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload, PAYLOAD_ARG_CNT);
  142. #else
  143. return -EPERM;
  144. #endif
  145. } else {
  146. /*
  147. * Added SIP service call Function Identifier
  148. * Make sure to stay in x0 register
  149. */
  150. struct pt_regs regs;
  151. regs.regs[0] = PM_SIP_SVC | api_id;
  152. regs.regs[1] = ((u64)arg1 << 32) | arg0;
  153. regs.regs[2] = ((u64)arg3 << 32) | arg2;
  154. smc_call(&regs);
  155. if (ret_payload) {
  156. ret_payload[0] = (u32)regs.regs[0];
  157. ret_payload[1] = upper_32_bits(regs.regs[0]);
  158. ret_payload[2] = (u32)regs.regs[1];
  159. ret_payload[3] = upper_32_bits(regs.regs[1]);
  160. ret_payload[4] = (u32)regs.regs[2];
  161. }
  162. }
  163. return (ret_payload) ? ret_payload[0] : 0;
  164. }
  165. static const struct udevice_id zynqmp_firmware_ids[] = {
  166. { .compatible = "xlnx,zynqmp-firmware" },
  167. { .compatible = "xlnx,versal-firmware"},
  168. { }
  169. };
  170. U_BOOT_DRIVER(zynqmp_firmware) = {
  171. .id = UCLASS_FIRMWARE,
  172. .name = "zynqmp_firmware",
  173. .of_match = zynqmp_firmware_ids,
  174. };