blob: 839203ec82a1201559f7e7b43170531e1f2ead63 [file] [log] [blame]
Rajan Vaja833be322019-02-15 04:45:32 -08001// SPDX-License-Identifier: GPL-2.0
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +02002/*
3 * Xilinx Zynq MPSoC Firmware driver
4 *
5 * Copyright (C) 2018-2019 Xilinx, Inc.
6 */
Rajan Vaja833be322019-02-15 04:45:32 -08007
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +02008#include <common.h>
Michal Simek58ba2672021-11-18 13:00:02 +01009#include <cpu_func.h>
Rajan Vaja833be322019-02-15 04:45:32 -080010#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Michal Simek81efd2a2019-10-04 15:45:29 +020012#include <zynqmp_firmware.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <asm/cache.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060014#include <asm/ptrace.h>
Rajan Vaja833be322019-02-15 04:45:32 -080015
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020016#if defined(CONFIG_ZYNQMP_IPI)
17#include <mailbox.h>
18#include <asm/arch/sys_proto.h>
19
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010020#define PMUFW_PAYLOAD_ARG_CNT 8
21
Michal Simek5f421122020-04-27 11:51:40 +020022#define XST_PM_NO_ACCESS 2002L
Michal Simek0ce829f2022-01-14 13:25:37 +010023#define XST_PM_ALREADY_CONFIGURED 2009L
Michal Simek5f421122020-04-27 11:51:40 +020024
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020025struct zynqmp_power {
26 struct mbox_chan tx_chan;
27 struct mbox_chan rx_chan;
28} zynqmp_power;
29
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010030static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
31{
32 struct zynqmp_ipi_msg msg;
33 int ret;
Michal Simekae655fb2021-10-15 16:57:39 +020034 u32 buffer[PAYLOAD_ARG_CNT];
35
36 if (!res)
37 res = buffer;
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010038
39 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
40 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
41 return -EINVAL;
42
43 if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
44 return -EINVAL;
45
Ibai Erkiaga22712ea2020-08-04 23:17:27 +010046 debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010047 msg.buf = (u32 *)req;
48 msg.len = req_len;
49 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
50 if (ret) {
51 debug("%s: Sending message failed\n", __func__);
52 return ret;
53 }
54
55 msg.buf = res;
56 msg.len = res_maxlen;
57 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
58 if (ret)
59 debug("%s: Receiving message failed\n", __func__);
60
61 return ret;
62}
63
64unsigned int zynqmp_firmware_version(void)
65{
66 int ret;
67 u32 ret_payload[PAYLOAD_ARG_CNT];
68 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
69
70 /*
71 * Get PMU version only once and later
72 * just return stored values instead of
73 * asking PMUFW again.
74 **/
75 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010076
Ibai Erkiaga22712ea2020-08-04 23:17:27 +010077 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
78 ret_payload);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010079 if (ret)
80 panic("PMUFW is not found - Please load it!\n");
81
82 pm_api_version = ret_payload[1];
83 if (pm_api_version < ZYNQMP_PM_VERSION)
84 panic("PMUFW version error. Expected: v%d.%d\n",
85 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
86 }
87
88 return pm_api_version;
89};
90
Michal Simeka95f0d92019-09-27 14:20:00 +020091/**
92 * Send a configuration object to the PMU firmware.
93 *
94 * @cfg_obj: Pointer to the configuration object
95 * @size: Size of @cfg_obj in bytes
96 */
97void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
98{
Michal Simeka95f0d92019-09-27 14:20:00 +020099 int err;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100100 u32 ret_payload[PAYLOAD_ARG_CNT];
Michal Simeka95f0d92019-09-27 14:20:00 +0200101
Michal Simek957b2482022-01-14 13:25:36 +0100102 if (IS_ENABLED(CONFIG_SPL_BUILD))
103 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
Michal Simeka95f0d92019-09-27 14:20:00 +0200104
Michal Simek58ba2672021-11-18 13:00:02 +0100105 flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
106
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100107 err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
108 0, ret_payload);
Michal Simek5f421122020-04-27 11:51:40 +0200109 if (err == XST_PM_NO_ACCESS) {
110 printf("PMUFW no permission to change config object\n");
111 return;
112 }
113
Michal Simek0ce829f2022-01-14 13:25:37 +0100114 if (err == XST_PM_ALREADY_CONFIGURED) {
115 debug("PMUFW Node is already configured\n");
116 return;
117 }
118
Michal Simeka95f0d92019-09-27 14:20:00 +0200119 if (err)
Michal Simek5f421122020-04-27 11:51:40 +0200120 printf("Cannot load PMUFW configuration object (%d)\n", err);
121
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100122 if (ret_payload[0])
123 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
Michal Simek5f421122020-04-27 11:51:40 +0200124
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100125 if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
Michal Simek5f421122020-04-27 11:51:40 +0200126 panic("PMUFW config object loading failed in EL3\n");
Michal Simeka95f0d92019-09-27 14:20:00 +0200127}
128
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200129static int zynqmp_power_probe(struct udevice *dev)
130{
Michal Simek8ece0722019-10-10 11:26:16 +0200131 int ret;
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200132
133 debug("%s, (dev=%p)\n", __func__, dev);
134
135 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
136 if (ret) {
Michal Simek8ece0722019-10-10 11:26:16 +0200137 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200138 return ret;
139 }
140
141 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100142 if (ret) {
Michal Simek8ece0722019-10-10 11:26:16 +0200143 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100144 return ret;
145 }
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200146
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100147 ret = zynqmp_firmware_version();
148 printf("PMUFW:\tv%d.%d\n",
149 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
150 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
151
152 return 0;
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200153};
154
155static const struct udevice_id zynqmp_power_ids[] = {
156 { .compatible = "xlnx,zynqmp-power" },
157 { }
158};
159
160U_BOOT_DRIVER(zynqmp_power) = {
161 .name = "zynqmp_power",
162 .id = UCLASS_FIRMWARE,
163 .of_match = zynqmp_power_ids,
164 .probe = zynqmp_power_probe,
165};
166#endif
167
Michal Simek4c3de372019-10-04 15:35:45 +0200168int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
169 u32 arg3, u32 *ret_payload)
Michal Simek81efd2a2019-10-04 15:45:29 +0200170{
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100171 debug("%s at EL%d, API ID: 0x%0x\n", __func__, current_el(), api_id);
Michal Simek81efd2a2019-10-04 15:45:29 +0200172
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100173 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
174#if defined(CONFIG_ZYNQMP_IPI)
175 /*
176 * Use fixed payload and arg size as the EL2 call. The firmware
177 * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
178 * firmware API is limited by the SMC call size
179 */
180 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
Michal Simek577385a2021-10-15 16:57:38 +0200181 int ret;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100182
Michal Simekf33d95b2020-10-05 15:23:28 +0200183 if (api_id == PM_FPGA_LOAD) {
184 /* Swap addr_hi/low because of incompatibility */
185 u32 temp = regs[1];
186
187 regs[1] = regs[2];
188 regs[2] = temp;
189 }
190
Michal Simek577385a2021-10-15 16:57:38 +0200191 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
192 PAYLOAD_ARG_CNT);
193 if (ret)
194 return ret;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100195#else
Michal Simek4ca6e952019-10-10 11:09:15 +0200196 return -EPERM;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100197#endif
198 } else {
199 /*
200 * Added SIP service call Function Identifier
201 * Make sure to stay in x0 register
202 */
203 struct pt_regs regs;
Michal Simek81efd2a2019-10-04 15:45:29 +0200204
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100205 regs.regs[0] = PM_SIP_SVC | api_id;
206 regs.regs[1] = ((u64)arg1 << 32) | arg0;
207 regs.regs[2] = ((u64)arg3 << 32) | arg2;
Michal Simek81efd2a2019-10-04 15:45:29 +0200208
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100209 smc_call(&regs);
Michal Simek81efd2a2019-10-04 15:45:29 +0200210
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100211 if (ret_payload) {
212 ret_payload[0] = (u32)regs.regs[0];
213 ret_payload[1] = upper_32_bits(regs.regs[0]);
214 ret_payload[2] = (u32)regs.regs[1];
215 ret_payload[3] = upper_32_bits(regs.regs[1]);
216 ret_payload[4] = (u32)regs.regs[2];
217 }
Michal Simek81efd2a2019-10-04 15:45:29 +0200218
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100219 }
220 return (ret_payload) ? ret_payload[0] : 0;
Michal Simek81efd2a2019-10-04 15:45:29 +0200221}
222
Rajan Vaja833be322019-02-15 04:45:32 -0800223static const struct udevice_id zynqmp_firmware_ids[] = {
224 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladuguf7a71202019-06-23 12:24:57 +0530225 { .compatible = "xlnx,versal-firmware"},
Rajan Vaja833be322019-02-15 04:45:32 -0800226 { }
227};
228
229U_BOOT_DRIVER(zynqmp_firmware) = {
230 .id = UCLASS_FIRMWARE,
Michal Simek33093082020-01-07 08:50:34 +0100231 .name = "zynqmp_firmware",
Rajan Vaja833be322019-02-15 04:45:32 -0800232 .of_match = zynqmp_firmware_ids,
233};