firmware-zynqmp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. msg.buf = (u32 *)req;
  32. msg.len = req_len;
  33. ret = mbox_send(&zynqmp_power.tx_chan, &msg);
  34. if (ret) {
  35. debug("%s: Sending message failed\n", __func__);
  36. return ret;
  37. }
  38. msg.buf = res;
  39. msg.len = res_maxlen;
  40. ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
  41. if (ret)
  42. debug("%s: Receiving message failed\n", __func__);
  43. return ret;
  44. }
  45. static int send_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
  46. {
  47. if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3)
  48. return ipi_req(req, req_len, res, res_maxlen);
  49. return xilinx_pm_request(req[0], 0, 0, 0, 0, res);
  50. }
  51. unsigned int zynqmp_firmware_version(void)
  52. {
  53. int ret;
  54. u32 ret_payload[PAYLOAD_ARG_CNT];
  55. static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
  56. /*
  57. * Get PMU version only once and later
  58. * just return stored values instead of
  59. * asking PMUFW again.
  60. **/
  61. if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
  62. const u32 request[] = { PM_GET_API_VERSION };
  63. ret = send_req(request, ARRAY_SIZE(request), ret_payload, 2);
  64. if (ret)
  65. panic("PMUFW is not found - Please load it!\n");
  66. pm_api_version = ret_payload[1];
  67. if (pm_api_version < ZYNQMP_PM_VERSION)
  68. panic("PMUFW version error. Expected: v%d.%d\n",
  69. ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
  70. }
  71. return pm_api_version;
  72. };
  73. /**
  74. * Send a configuration object to the PMU firmware.
  75. *
  76. * @cfg_obj: Pointer to the configuration object
  77. * @size: Size of @cfg_obj in bytes
  78. */
  79. void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
  80. {
  81. const u32 request[] = {
  82. PM_SET_CONFIGURATION,
  83. (u32)((u64)cfg_obj)
  84. };
  85. u32 response = 0;
  86. int err;
  87. printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
  88. err = send_req(request, ARRAY_SIZE(request), &response, 1);
  89. if (err == XST_PM_NO_ACCESS) {
  90. printf("PMUFW no permission to change config object\n");
  91. return;
  92. }
  93. if (err)
  94. printf("Cannot load PMUFW configuration object (%d)\n", err);
  95. if (response)
  96. printf("PMUFW returned 0x%08x status!\n", response);
  97. if ((err || response) && IS_ENABLED(CONFIG_SPL_BUILD))
  98. panic("PMUFW config object loading failed in EL3\n");
  99. }
  100. static int zynqmp_power_probe(struct udevice *dev)
  101. {
  102. int ret;
  103. debug("%s, (dev=%p)\n", __func__, dev);
  104. ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
  105. if (ret) {
  106. debug("%s: Cannot find tx mailbox\n", __func__);
  107. return ret;
  108. }
  109. ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
  110. if (ret) {
  111. debug("%s: Cannot find rx mailbox\n", __func__);
  112. return ret;
  113. }
  114. ret = zynqmp_firmware_version();
  115. printf("PMUFW:\tv%d.%d\n",
  116. ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
  117. ret & ZYNQMP_PM_VERSION_MINOR_MASK);
  118. return 0;
  119. };
  120. static const struct udevice_id zynqmp_power_ids[] = {
  121. { .compatible = "xlnx,zynqmp-power" },
  122. { }
  123. };
  124. U_BOOT_DRIVER(zynqmp_power) = {
  125. .name = "zynqmp_power",
  126. .id = UCLASS_FIRMWARE,
  127. .of_match = zynqmp_power_ids,
  128. .probe = zynqmp_power_probe,
  129. };
  130. #endif
  131. int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
  132. u32 arg3, u32 *ret_payload)
  133. {
  134. /*
  135. * Added SIP service call Function Identifier
  136. * Make sure to stay in x0 register
  137. */
  138. struct pt_regs regs;
  139. if (current_el() == 3) {
  140. printf("%s: Can't call SMC from EL3 context\n", __func__);
  141. return -EPERM;
  142. }
  143. regs.regs[0] = PM_SIP_SVC | api_id;
  144. regs.regs[1] = ((u64)arg1 << 32) | arg0;
  145. regs.regs[2] = ((u64)arg3 << 32) | arg2;
  146. smc_call(&regs);
  147. if (ret_payload) {
  148. ret_payload[0] = (u32)regs.regs[0];
  149. ret_payload[1] = upper_32_bits(regs.regs[0]);
  150. ret_payload[2] = (u32)regs.regs[1];
  151. ret_payload[3] = upper_32_bits(regs.regs[1]);
  152. ret_payload[4] = (u32)regs.regs[2];
  153. }
  154. return regs.regs[0];
  155. }
  156. static const struct udevice_id zynqmp_firmware_ids[] = {
  157. { .compatible = "xlnx,zynqmp-firmware" },
  158. { .compatible = "xlnx,versal-firmware"},
  159. { }
  160. };
  161. U_BOOT_DRIVER(zynqmp_firmware) = {
  162. .id = UCLASS_FIRMWARE,
  163. .name = "zynqmp-firmware",
  164. .of_match = zynqmp_firmware_ids,
  165. };