blob: b0cd647aa518b506cd75e27aa1c1e90bc920447a [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>
Michal Simek0fd9f362022-02-07 10:27:37 +010011#include <dm/lists.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Michal Simek81efd2a2019-10-04 15:45:29 +020013#include <zynqmp_firmware.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060015#include <asm/ptrace.h>
Rajan Vaja833be322019-02-15 04:45:32 -080016
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020017#if defined(CONFIG_ZYNQMP_IPI)
18#include <mailbox.h>
19#include <asm/arch/sys_proto.h>
20
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010021#define PMUFW_PAYLOAD_ARG_CNT 8
22
Michal Simek5f421122020-04-27 11:51:40 +020023#define XST_PM_NO_ACCESS 2002L
Michal Simek0ce829f2022-01-14 13:25:37 +010024#define XST_PM_ALREADY_CONFIGURED 2009L
Michal Simek5f421122020-04-27 11:51:40 +020025
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020026struct zynqmp_power {
27 struct mbox_chan tx_chan;
28 struct mbox_chan rx_chan;
Stefan Herbrechtsmeier44b9f792022-06-20 18:36:37 +020029} zynqmp_power = {};
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020030
Michal Simek783e74c2022-01-14 13:25:35 +010031#define NODE_ID_LOCATION 5
32
33static unsigned int xpm_configobject[] = {
34 /**********************************************************************/
35 /* HEADER */
36 2, /* Number of remaining words in the header */
37 1, /* Number of sections included in config object */
38 PM_CONFIG_OBJECT_TYPE_OVERLAY, /* Type of Config object as overlay */
39 /**********************************************************************/
40 /* SLAVE SECTION */
41
42 PM_CONFIG_SLAVE_SECTION_ID, /* Section ID */
43 1, /* Number of slaves */
44
45 0, /* Node ID which will be changed below */
46 PM_SLAVE_FLAG_IS_SHAREABLE,
47 PM_CONFIG_IPI_PSU_CORTEXA53_0_MASK |
48 PM_CONFIG_IPI_PSU_CORTEXR5_0_MASK |
49 PM_CONFIG_IPI_PSU_CORTEXR5_1_MASK, /* IPI Mask */
50};
51
Michal Simek07168c62022-01-14 13:25:38 +010052static unsigned int xpm_configobject_close[] = {
53 /**********************************************************************/
54 /* HEADER */
55 2, /* Number of remaining words in the header */
56 1, /* Number of sections included in config object */
57 PM_CONFIG_OBJECT_TYPE_OVERLAY, /* Type of Config object as overlay */
58 /**********************************************************************/
59 /* SET CONFIG SECTION */
60 PM_CONFIG_SET_CONFIG_SECTION_ID,
61 0U, /* Loading permission to Overlay config object */
62};
63
64int zynqmp_pmufw_config_close(void)
65{
66 zynqmp_pmufw_load_config_object(xpm_configobject_close,
67 sizeof(xpm_configobject_close));
68 return 0;
69}
70
Michal Simek783e74c2022-01-14 13:25:35 +010071int zynqmp_pmufw_node(u32 id)
72{
73 /* Record power domain id */
74 xpm_configobject[NODE_ID_LOCATION] = id;
75
76 zynqmp_pmufw_load_config_object(xpm_configobject,
77 sizeof(xpm_configobject));
78
79 return 0;
80}
81
Stefan Herbrechtsmeier44b9f792022-06-20 18:36:37 +020082static int do_pm_probe(void)
83{
84 struct udevice *dev;
85 int ret;
86
87 ret = uclass_get_device_by_driver(UCLASS_FIRMWARE,
88 DM_DRIVER_GET(zynqmp_power),
89 &dev);
90 if (ret)
91 debug("%s: Probing device failed: %d\n", __func__, ret);
92
93 return ret;
94}
95
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010096static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
97{
98 struct zynqmp_ipi_msg msg;
99 int ret;
Michal Simekae655fb2021-10-15 16:57:39 +0200100 u32 buffer[PAYLOAD_ARG_CNT];
101
102 if (!res)
103 res = buffer;
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100104
105 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
106 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
107 return -EINVAL;
108
Stefan Herbrechtsmeier44b9f792022-06-20 18:36:37 +0200109 if (!(zynqmp_power.tx_chan.dev) || !(zynqmp_power.rx_chan.dev)) {
110 ret = do_pm_probe();
111 if (ret)
112 return ret;
113 }
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100114
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100115 debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100116 msg.buf = (u32 *)req;
117 msg.len = req_len;
118 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
119 if (ret) {
120 debug("%s: Sending message failed\n", __func__);
121 return ret;
122 }
123
124 msg.buf = res;
125 msg.len = res_maxlen;
126 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
127 if (ret)
128 debug("%s: Receiving message failed\n", __func__);
129
130 return ret;
131}
132
133unsigned int zynqmp_firmware_version(void)
134{
135 int ret;
136 u32 ret_payload[PAYLOAD_ARG_CNT];
137 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
138
139 /*
140 * Get PMU version only once and later
141 * just return stored values instead of
142 * asking PMUFW again.
143 **/
144 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100145
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100146 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
147 ret_payload);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100148 if (ret)
149 panic("PMUFW is not found - Please load it!\n");
150
151 pm_api_version = ret_payload[1];
152 if (pm_api_version < ZYNQMP_PM_VERSION)
153 panic("PMUFW version error. Expected: v%d.%d\n",
154 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
155 }
156
157 return pm_api_version;
158};
159
T Karthik Reddy22a8bf82022-03-30 11:07:57 +0200160int zynqmp_pm_set_gem_config(u32 node, enum pm_gem_config_type config, u32 value)
161{
162 int ret;
163
164 ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_GEM_CONFIG,
165 config, value, NULL);
166 if (ret)
167 printf("%s: node %d: set_gem_config %d failed\n",
168 __func__, node, config);
169
170 return ret;
171}
172
Ashok Reddy Soma87cdd352022-02-23 15:36:03 +0100173int zynqmp_pm_set_sd_config(u32 node, enum pm_sd_config_type config, u32 value)
174{
175 int ret;
176
177 ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_SD_CONFIG,
178 config, value, NULL);
179 if (ret)
180 printf("%s: node %d: set_sd_config %d failed\n",
181 __func__, node, config);
182
183 return ret;
184}
185
186int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
187{
188 int ret;
189 u32 *bit_mask;
190 u32 ret_payload[PAYLOAD_ARG_CNT];
191
192 /* Input arguments validation */
193 if (id >= 64 || (api_id != PM_IOCTL && api_id != PM_QUERY_DATA))
194 return -EINVAL;
195
196 /* Check feature check API version */
197 ret = xilinx_pm_request(PM_FEATURE_CHECK, PM_FEATURE_CHECK, 0, 0, 0,
198 ret_payload);
199 if (ret)
200 return ret;
201
202 /* Check if feature check version 2 is supported or not */
203 if ((ret_payload[1] & FIRMWARE_VERSION_MASK) == PM_API_VERSION_2) {
204 /*
205 * Call feature check for IOCTL/QUERY API to get IOCTL ID or
206 * QUERY ID feature status.
207 */
208
209 ret = xilinx_pm_request(PM_FEATURE_CHECK, api_id, 0, 0, 0,
210 ret_payload);
211 if (ret)
212 return ret;
213
214 bit_mask = &ret_payload[2];
215 if ((bit_mask[(id / 32)] & BIT((id % 32))) == 0)
216 return -EOPNOTSUPP;
217 } else {
218 return -ENODATA;
219 }
220
221 return 0;
222}
223
Michal Simeka95f0d92019-09-27 14:20:00 +0200224/**
225 * Send a configuration object to the PMU firmware.
226 *
227 * @cfg_obj: Pointer to the configuration object
228 * @size: Size of @cfg_obj in bytes
229 */
230void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
231{
Michal Simeka95f0d92019-09-27 14:20:00 +0200232 int err;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100233 u32 ret_payload[PAYLOAD_ARG_CNT];
Michal Simeka95f0d92019-09-27 14:20:00 +0200234
Michal Simek957b2482022-01-14 13:25:36 +0100235 if (IS_ENABLED(CONFIG_SPL_BUILD))
236 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
Michal Simeka95f0d92019-09-27 14:20:00 +0200237
Michal Simek58ba2672021-11-18 13:00:02 +0100238 flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
239
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100240 err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
241 0, ret_payload);
Michal Simek5f421122020-04-27 11:51:40 +0200242 if (err == XST_PM_NO_ACCESS) {
243 printf("PMUFW no permission to change config object\n");
244 return;
245 }
246
Michal Simek0ce829f2022-01-14 13:25:37 +0100247 if (err == XST_PM_ALREADY_CONFIGURED) {
248 debug("PMUFW Node is already configured\n");
249 return;
250 }
251
Michal Simeka95f0d92019-09-27 14:20:00 +0200252 if (err)
Michal Simek5f421122020-04-27 11:51:40 +0200253 printf("Cannot load PMUFW configuration object (%d)\n", err);
254
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100255 if (ret_payload[0])
256 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
Michal Simek5f421122020-04-27 11:51:40 +0200257
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100258 if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
Michal Simek5f421122020-04-27 11:51:40 +0200259 panic("PMUFW config object loading failed in EL3\n");
Michal Simeka95f0d92019-09-27 14:20:00 +0200260}
261
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200262static int zynqmp_power_probe(struct udevice *dev)
263{
Michal Simek8ece0722019-10-10 11:26:16 +0200264 int ret;
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200265
266 debug("%s, (dev=%p)\n", __func__, dev);
267
268 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
269 if (ret) {
Michal Simek8ece0722019-10-10 11:26:16 +0200270 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200271 return ret;
272 }
273
274 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100275 if (ret) {
Michal Simek8ece0722019-10-10 11:26:16 +0200276 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100277 return ret;
278 }
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200279
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100280 ret = zynqmp_firmware_version();
281 printf("PMUFW:\tv%d.%d\n",
282 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
283 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
284
285 return 0;
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200286};
287
288static const struct udevice_id zynqmp_power_ids[] = {
289 { .compatible = "xlnx,zynqmp-power" },
290 { }
291};
292
293U_BOOT_DRIVER(zynqmp_power) = {
294 .name = "zynqmp_power",
295 .id = UCLASS_FIRMWARE,
296 .of_match = zynqmp_power_ids,
297 .probe = zynqmp_power_probe,
298};
299#endif
300
Michal Simek4c3de372019-10-04 15:35:45 +0200301int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
302 u32 arg3, u32 *ret_payload)
Michal Simek81efd2a2019-10-04 15:45:29 +0200303{
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100304 debug("%s at EL%d, API ID: 0x%0x\n", __func__, current_el(), api_id);
Michal Simek81efd2a2019-10-04 15:45:29 +0200305
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100306 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
307#if defined(CONFIG_ZYNQMP_IPI)
308 /*
309 * Use fixed payload and arg size as the EL2 call. The firmware
310 * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
311 * firmware API is limited by the SMC call size
312 */
313 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
Michal Simek577385a2021-10-15 16:57:38 +0200314 int ret;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100315
Michal Simekf33d95b2020-10-05 15:23:28 +0200316 if (api_id == PM_FPGA_LOAD) {
317 /* Swap addr_hi/low because of incompatibility */
318 u32 temp = regs[1];
319
320 regs[1] = regs[2];
321 regs[2] = temp;
322 }
323
Michal Simek577385a2021-10-15 16:57:38 +0200324 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
325 PAYLOAD_ARG_CNT);
326 if (ret)
327 return ret;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100328#else
Michal Simek4ca6e952019-10-10 11:09:15 +0200329 return -EPERM;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100330#endif
331 } else {
332 /*
333 * Added SIP service call Function Identifier
334 * Make sure to stay in x0 register
335 */
336 struct pt_regs regs;
Michal Simek81efd2a2019-10-04 15:45:29 +0200337
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100338 regs.regs[0] = PM_SIP_SVC | api_id;
339 regs.regs[1] = ((u64)arg1 << 32) | arg0;
340 regs.regs[2] = ((u64)arg3 << 32) | arg2;
Michal Simek81efd2a2019-10-04 15:45:29 +0200341
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100342 smc_call(&regs);
Michal Simek81efd2a2019-10-04 15:45:29 +0200343
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100344 if (ret_payload) {
345 ret_payload[0] = (u32)regs.regs[0];
346 ret_payload[1] = upper_32_bits(regs.regs[0]);
347 ret_payload[2] = (u32)regs.regs[1];
348 ret_payload[3] = upper_32_bits(regs.regs[1]);
349 ret_payload[4] = (u32)regs.regs[2];
350 }
Michal Simek81efd2a2019-10-04 15:45:29 +0200351
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100352 }
353 return (ret_payload) ? ret_payload[0] : 0;
Michal Simek81efd2a2019-10-04 15:45:29 +0200354}
355
Rajan Vaja833be322019-02-15 04:45:32 -0800356static const struct udevice_id zynqmp_firmware_ids[] = {
357 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladuguf7a71202019-06-23 12:24:57 +0530358 { .compatible = "xlnx,versal-firmware"},
Rajan Vaja833be322019-02-15 04:45:32 -0800359 { }
360};
361
Michal Simek0fd9f362022-02-07 10:27:37 +0100362static int zynqmp_firmware_bind(struct udevice *dev)
363{
364 int ret;
365 struct udevice *child;
366
Michal Simek259af932022-02-28 17:13:15 +0100367 if ((IS_ENABLED(CONFIG_SPL_BUILD) &&
368 IS_ENABLED(CONFIG_SPL_POWER_DOMAIN) &&
369 IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) ||
370 (!IS_ENABLED(CONFIG_SPL_BUILD) &&
371 IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN))) {
Michal Simek0fd9f362022-02-07 10:27:37 +0100372 ret = device_bind_driver_to_node(dev, "zynqmp_power_domain",
373 "zynqmp_power_domain",
374 dev_ofnode(dev), &child);
375 if (ret) {
376 printf("zynqmp power domain driver is not bound: %d\n", ret);
377 return ret;
378 }
379 }
380
381 return dm_scan_fdt_dev(dev);
382}
383
Rajan Vaja833be322019-02-15 04:45:32 -0800384U_BOOT_DRIVER(zynqmp_firmware) = {
385 .id = UCLASS_FIRMWARE,
Michal Simek33093082020-01-07 08:50:34 +0100386 .name = "zynqmp_firmware",
Rajan Vaja833be322019-02-15 04:45:32 -0800387 .of_match = zynqmp_firmware_ids,
Michal Simek0fd9f362022-02-07 10:27:37 +0100388 .bind = zynqmp_firmware_bind,
Rajan Vaja833be322019-02-15 04:45:32 -0800389};