blob: dfad798a2e7d93b3d93038f7b2cde1bdcce08a67 [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>
Tanmay Shaha7c0d3f2023-12-04 13:56:19 -080011#include <dm/device_compat.h>
Michal Simek0fd9f362022-02-07 10:27:37 +010012#include <dm/lists.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Michal Simek81efd2a2019-10-04 15:45:29 +020014#include <zynqmp_firmware.h>
Simon Glass274e0b02020-05-10 11:39:56 -060015#include <asm/cache.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060016#include <asm/ptrace.h>
Rajan Vaja833be322019-02-15 04:45:32 -080017
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020018#if defined(CONFIG_ZYNQMP_IPI)
19#include <mailbox.h>
20#include <asm/arch/sys_proto.h>
21
Ibai Erkiaga99b2c282019-09-27 11:37:00 +010022#define PMUFW_PAYLOAD_ARG_CNT 8
23
Michal Simek5f421122020-04-27 11:51:40 +020024#define XST_PM_NO_ACCESS 2002L
Michal Simek0ce829f2022-01-14 13:25:37 +010025#define XST_PM_ALREADY_CONFIGURED 2009L
Michal Simek5f421122020-04-27 11:51:40 +020026
Stefan Herbrechtsmeier43358c82023-05-23 14:42:14 +020027static struct zynqmp_power {
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020028 struct mbox_chan tx_chan;
29 struct mbox_chan rx_chan;
Stefan Herbrechtsmeier43358c82023-05-23 14:42:14 +020030} zynqmp_power __section(".data");
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +020031
Michal Simek783e74c2022-01-14 13:25:35 +010032#define NODE_ID_LOCATION 5
33
34static unsigned int xpm_configobject[] = {
35 /**********************************************************************/
36 /* HEADER */
37 2, /* Number of remaining words in the header */
38 1, /* Number of sections included in config object */
39 PM_CONFIG_OBJECT_TYPE_OVERLAY, /* Type of Config object as overlay */
40 /**********************************************************************/
41 /* SLAVE SECTION */
42
43 PM_CONFIG_SLAVE_SECTION_ID, /* Section ID */
44 1, /* Number of slaves */
45
46 0, /* Node ID which will be changed below */
47 PM_SLAVE_FLAG_IS_SHAREABLE,
48 PM_CONFIG_IPI_PSU_CORTEXA53_0_MASK |
49 PM_CONFIG_IPI_PSU_CORTEXR5_0_MASK |
50 PM_CONFIG_IPI_PSU_CORTEXR5_1_MASK, /* IPI Mask */
51};
52
Michal Simek07168c62022-01-14 13:25:38 +010053static unsigned int xpm_configobject_close[] = {
54 /**********************************************************************/
55 /* HEADER */
56 2, /* Number of remaining words in the header */
57 1, /* Number of sections included in config object */
58 PM_CONFIG_OBJECT_TYPE_OVERLAY, /* Type of Config object as overlay */
59 /**********************************************************************/
60 /* SET CONFIG SECTION */
61 PM_CONFIG_SET_CONFIG_SECTION_ID,
62 0U, /* Loading permission to Overlay config object */
63};
64
65int zynqmp_pmufw_config_close(void)
66{
Stefan Herbrechtsmeierc4c14102023-05-23 14:42:13 +020067 return zynqmp_pmufw_load_config_object(xpm_configobject_close,
68 sizeof(xpm_configobject_close));
Michal Simek07168c62022-01-14 13:25:38 +010069}
70
Michal Simek783e74c2022-01-14 13:25:35 +010071int zynqmp_pmufw_node(u32 id)
72{
Stefan Herbrechtsmeierc4c14102023-05-23 14:42:13 +020073 static bool check = true;
74 static bool permission = true;
75
76 if (check) {
77 check = false;
Ashok Reddy Somaa3e5db22022-07-22 02:46:55 -060078
Stefan Herbrechtsmeierc4c14102023-05-23 14:42:13 +020079 if (zynqmp_pmufw_node(NODE_OCM_BANK_0) == -EACCES) {
80 printf("PMUFW: No permission to change config object\n");
81 permission = false;
82 }
83 }
84
85 if (!permission)
Ashok Reddy Somaa3e5db22022-07-22 02:46:55 -060086 return 0;
87
Michal Simek783e74c2022-01-14 13:25:35 +010088 /* Record power domain id */
89 xpm_configobject[NODE_ID_LOCATION] = id;
90
Stefan Herbrechtsmeierc4c14102023-05-23 14:42:13 +020091 return zynqmp_pmufw_load_config_object(xpm_configobject,
92 sizeof(xpm_configobject));
Michal Simek783e74c2022-01-14 13:25:35 +010093}
94
Stefan Herbrechtsmeier44b9f792022-06-20 18:36:37 +020095static int do_pm_probe(void)
96{
97 struct udevice *dev;
98 int ret;
99
100 ret = uclass_get_device_by_driver(UCLASS_FIRMWARE,
101 DM_DRIVER_GET(zynqmp_power),
102 &dev);
103 if (ret)
104 debug("%s: Probing device failed: %d\n", __func__, ret);
105
106 return ret;
107}
108
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100109static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
110{
111 struct zynqmp_ipi_msg msg;
112 int ret;
Michal Simekae655fb2021-10-15 16:57:39 +0200113 u32 buffer[PAYLOAD_ARG_CNT];
114
115 if (!res)
116 res = buffer;
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100117
118 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
119 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
120 return -EINVAL;
121
Stefan Herbrechtsmeier44b9f792022-06-20 18:36:37 +0200122 if (!(zynqmp_power.tx_chan.dev) || !(zynqmp_power.rx_chan.dev)) {
123 ret = do_pm_probe();
124 if (ret)
125 return ret;
126 }
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100127
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100128 debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100129 msg.buf = (u32 *)req;
130 msg.len = req_len;
131 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
132 if (ret) {
133 debug("%s: Sending message failed\n", __func__);
134 return ret;
135 }
136
137 msg.buf = res;
138 msg.len = res_maxlen;
139 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
140 if (ret)
141 debug("%s: Receiving message failed\n", __func__);
142
143 return ret;
144}
145
146unsigned int zynqmp_firmware_version(void)
147{
148 int ret;
149 u32 ret_payload[PAYLOAD_ARG_CNT];
150 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
151
152 /*
153 * Get PMU version only once and later
154 * just return stored values instead of
155 * asking PMUFW again.
156 **/
157 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100158
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100159 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
160 ret_payload);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100161 if (ret)
162 panic("PMUFW is not found - Please load it!\n");
163
164 pm_api_version = ret_payload[1];
165 if (pm_api_version < ZYNQMP_PM_VERSION)
166 panic("PMUFW version error. Expected: v%d.%d\n",
167 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
168 }
169
170 return pm_api_version;
171};
172
T Karthik Reddy22a8bf82022-03-30 11:07:57 +0200173int zynqmp_pm_set_gem_config(u32 node, enum pm_gem_config_type config, u32 value)
174{
175 int ret;
176
177 ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_GEM_CONFIG,
178 config, value, NULL);
179 if (ret)
180 printf("%s: node %d: set_gem_config %d failed\n",
181 __func__, node, config);
182
183 return ret;
184}
185
Ashok Reddy Soma87cdd352022-02-23 15:36:03 +0100186int zynqmp_pm_set_sd_config(u32 node, enum pm_sd_config_type config, u32 value)
187{
188 int ret;
189
190 ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_SD_CONFIG,
191 config, value, NULL);
192 if (ret)
193 printf("%s: node %d: set_sd_config %d failed\n",
194 __func__, node, config);
195
196 return ret;
197}
198
Ashok Reddy Soma40bf26b2023-08-10 23:48:27 -0600199int zynqmp_pm_feature(const u32 api_id)
200{
201 int ret;
202 u32 ret_payload[PAYLOAD_ARG_CNT];
203
204 /* Check feature check API version */
205 ret = xilinx_pm_request(PM_FEATURE_CHECK, api_id, 0, 0, 0,
206 ret_payload);
Venkatesh Yadav Abbarapu94454fc2023-10-11 08:26:47 +0530207 if (ret)
208 return ret;
Ashok Reddy Soma40bf26b2023-08-10 23:48:27 -0600209
210 /* Return feature check version */
211 return ret_payload[1] & FIRMWARE_VERSION_MASK;
212}
213
Ashok Reddy Soma87cdd352022-02-23 15:36:03 +0100214int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
215{
216 int ret;
217 u32 *bit_mask;
218 u32 ret_payload[PAYLOAD_ARG_CNT];
219
220 /* Input arguments validation */
221 if (id >= 64 || (api_id != PM_IOCTL && api_id != PM_QUERY_DATA))
222 return -EINVAL;
223
224 /* Check feature check API version */
225 ret = xilinx_pm_request(PM_FEATURE_CHECK, PM_FEATURE_CHECK, 0, 0, 0,
226 ret_payload);
227 if (ret)
228 return ret;
229
230 /* Check if feature check version 2 is supported or not */
231 if ((ret_payload[1] & FIRMWARE_VERSION_MASK) == PM_API_VERSION_2) {
232 /*
233 * Call feature check for IOCTL/QUERY API to get IOCTL ID or
234 * QUERY ID feature status.
235 */
236
237 ret = xilinx_pm_request(PM_FEATURE_CHECK, api_id, 0, 0, 0,
238 ret_payload);
239 if (ret)
240 return ret;
241
242 bit_mask = &ret_payload[2];
243 if ((bit_mask[(id / 32)] & BIT((id % 32))) == 0)
244 return -EOPNOTSUPP;
245 } else {
246 return -ENODATA;
247 }
248
249 return 0;
250}
251
Michal Simeka95f0d92019-09-27 14:20:00 +0200252/**
253 * Send a configuration object to the PMU firmware.
254 *
255 * @cfg_obj: Pointer to the configuration object
256 * @size: Size of @cfg_obj in bytes
Stefan Herbrechtsmeier764301a2023-05-23 14:42:10 +0200257 * Return: 0 on success otherwise negative errno.
Michal Simeka95f0d92019-09-27 14:20:00 +0200258 */
Ashok Reddy Soma406bc8a2022-07-22 02:46:54 -0600259int zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
Michal Simeka95f0d92019-09-27 14:20:00 +0200260{
Michal Simeka95f0d92019-09-27 14:20:00 +0200261 int err;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100262 u32 ret_payload[PAYLOAD_ARG_CNT];
Michal Simeka95f0d92019-09-27 14:20:00 +0200263
Michal Simek957b2482022-01-14 13:25:36 +0100264 if (IS_ENABLED(CONFIG_SPL_BUILD))
265 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
Michal Simeka95f0d92019-09-27 14:20:00 +0200266
Michal Simek58ba2672021-11-18 13:00:02 +0100267 flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
268
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100269 err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
270 0, ret_payload);
Michal Simek5f421122020-04-27 11:51:40 +0200271 if (err == XST_PM_NO_ACCESS) {
Ashok Reddy Soma406bc8a2022-07-22 02:46:54 -0600272 return -EACCES;
Michal Simek5f421122020-04-27 11:51:40 +0200273 }
274
Michal Simek0ce829f2022-01-14 13:25:37 +0100275 if (err == XST_PM_ALREADY_CONFIGURED) {
276 debug("PMUFW Node is already configured\n");
Ashok Reddy Soma406bc8a2022-07-22 02:46:54 -0600277 return -ENODEV;
Michal Simek0ce829f2022-01-14 13:25:37 +0100278 }
279
Michal Simeka95f0d92019-09-27 14:20:00 +0200280 if (err)
Michal Simek5f421122020-04-27 11:51:40 +0200281 printf("Cannot load PMUFW configuration object (%d)\n", err);
282
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100283 if (ret_payload[0])
284 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
Michal Simek5f421122020-04-27 11:51:40 +0200285
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100286 if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
Michal Simek5f421122020-04-27 11:51:40 +0200287 panic("PMUFW config object loading failed in EL3\n");
Ashok Reddy Soma406bc8a2022-07-22 02:46:54 -0600288
289 return 0;
Michal Simeka95f0d92019-09-27 14:20:00 +0200290}
291
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200292static int zynqmp_power_probe(struct udevice *dev)
293{
Tanmay Shaha7c0d3f2023-12-04 13:56:19 -0800294 struct udevice *ipi_dev;
295 ofnode ipi_node;
Michal Simek8ece0722019-10-10 11:26:16 +0200296 int ret;
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200297
298 debug("%s, (dev=%p)\n", __func__, dev);
299
Tanmay Shaha7c0d3f2023-12-04 13:56:19 -0800300 /*
301 * Probe all IPI parent node driver. It is important to have IPI
302 * devices available when requested by mbox_get_by* API.
303 * If IPI device isn't available, then mailbox request fails and
304 * that causes system boot failure.
305 * To avoid this make sure all IPI parent drivers are probed here,
306 * and IPI parent driver binds each child node to mailbox driver.
307 * This way mbox_get_by_* API will have correct mailbox device
308 * driver probed.
309 */
310 ofnode_for_each_compatible_node(ipi_node, "xlnx,zynqmp-ipi-mailbox") {
311 ret = uclass_get_device_by_ofnode(UCLASS_NOP, ipi_node, &ipi_dev);
312 if (ret) {
313 dev_err(dev, "failed to get IPI device from node %s\n",
314 ofnode_get_name(ipi_node));
315 return ret;
316 }
317 }
318
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200319 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
320 if (ret) {
Michal Simek8ece0722019-10-10 11:26:16 +0200321 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200322 return ret;
323 }
324
325 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100326 if (ret) {
Michal Simek8ece0722019-10-10 11:26:16 +0200327 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100328 return ret;
329 }
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200330
Ibai Erkiaga99b2c282019-09-27 11:37:00 +0100331 ret = zynqmp_firmware_version();
332 printf("PMUFW:\tv%d.%d\n",
333 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
334 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
335
336 return 0;
Ibai Erkiagad47ed6f2019-09-27 12:51:41 +0200337};
338
339static const struct udevice_id zynqmp_power_ids[] = {
340 { .compatible = "xlnx,zynqmp-power" },
341 { }
342};
343
344U_BOOT_DRIVER(zynqmp_power) = {
345 .name = "zynqmp_power",
346 .id = UCLASS_FIRMWARE,
347 .of_match = zynqmp_power_ids,
348 .probe = zynqmp_power_probe,
349};
350#endif
351
Michal Simek4c3de372019-10-04 15:35:45 +0200352int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
353 u32 arg3, u32 *ret_payload)
Michal Simek81efd2a2019-10-04 15:45:29 +0200354{
Michal Simeka1982142023-06-01 13:34:34 +0200355 debug("%s at EL%d, API ID: 0x%0x, 0x%0x, 0x%0x, 0x%0x, 0x%0x\n",
356 __func__, current_el(), api_id, arg0, arg1, arg2, arg3);
Michal Simek81efd2a2019-10-04 15:45:29 +0200357
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100358 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
359#if defined(CONFIG_ZYNQMP_IPI)
360 /*
361 * Use fixed payload and arg size as the EL2 call. The firmware
362 * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
363 * firmware API is limited by the SMC call size
364 */
365 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
Michal Simek577385a2021-10-15 16:57:38 +0200366 int ret;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100367
Michal Simekf33d95b2020-10-05 15:23:28 +0200368 if (api_id == PM_FPGA_LOAD) {
369 /* Swap addr_hi/low because of incompatibility */
370 u32 temp = regs[1];
371
372 regs[1] = regs[2];
373 regs[2] = temp;
374 }
375
Michal Simek577385a2021-10-15 16:57:38 +0200376 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
377 PAYLOAD_ARG_CNT);
378 if (ret)
379 return ret;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100380#else
Michal Simek4ca6e952019-10-10 11:09:15 +0200381 return -EPERM;
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100382#endif
383 } else {
384 /*
385 * Added SIP service call Function Identifier
386 * Make sure to stay in x0 register
387 */
388 struct pt_regs regs;
Michal Simek81efd2a2019-10-04 15:45:29 +0200389
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100390 regs.regs[0] = PM_SIP_SVC | api_id;
391 regs.regs[1] = ((u64)arg1 << 32) | arg0;
392 regs.regs[2] = ((u64)arg3 << 32) | arg2;
Michal Simek81efd2a2019-10-04 15:45:29 +0200393
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100394 smc_call(&regs);
Michal Simek81efd2a2019-10-04 15:45:29 +0200395
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100396 if (ret_payload) {
397 ret_payload[0] = (u32)regs.regs[0];
398 ret_payload[1] = upper_32_bits(regs.regs[0]);
399 ret_payload[2] = (u32)regs.regs[1];
400 ret_payload[3] = upper_32_bits(regs.regs[1]);
401 ret_payload[4] = (u32)regs.regs[2];
402 }
Michal Simek81efd2a2019-10-04 15:45:29 +0200403
Ibai Erkiaga22712ea2020-08-04 23:17:27 +0100404 }
405 return (ret_payload) ? ret_payload[0] : 0;
Michal Simek81efd2a2019-10-04 15:45:29 +0200406}
407
Rajan Vaja833be322019-02-15 04:45:32 -0800408static const struct udevice_id zynqmp_firmware_ids[] = {
409 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladuguf7a71202019-06-23 12:24:57 +0530410 { .compatible = "xlnx,versal-firmware"},
Jay Buddhabhattibc62eb22022-09-19 14:21:06 +0200411 { .compatible = "xlnx,versal-net-firmware"},
Rajan Vaja833be322019-02-15 04:45:32 -0800412 { }
413};
414
Michal Simek0fd9f362022-02-07 10:27:37 +0100415static int zynqmp_firmware_bind(struct udevice *dev)
416{
417 int ret;
418 struct udevice *child;
419
Michal Simek259af932022-02-28 17:13:15 +0100420 if ((IS_ENABLED(CONFIG_SPL_BUILD) &&
421 IS_ENABLED(CONFIG_SPL_POWER_DOMAIN) &&
422 IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) ||
423 (!IS_ENABLED(CONFIG_SPL_BUILD) &&
424 IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN))) {
Michal Simek0fd9f362022-02-07 10:27:37 +0100425 ret = device_bind_driver_to_node(dev, "zynqmp_power_domain",
426 "zynqmp_power_domain",
427 dev_ofnode(dev), &child);
428 if (ret) {
429 printf("zynqmp power domain driver is not bound: %d\n", ret);
430 return ret;
431 }
432 }
433
Stefan Herbrechtsmeier00e61562023-05-23 14:42:11 +0200434 return 0;
Michal Simek0fd9f362022-02-07 10:27:37 +0100435}
436
Rajan Vaja833be322019-02-15 04:45:32 -0800437U_BOOT_DRIVER(zynqmp_firmware) = {
438 .id = UCLASS_FIRMWARE,
Michal Simek33093082020-01-07 08:50:34 +0100439 .name = "zynqmp_firmware",
Rajan Vaja833be322019-02-15 04:45:32 -0800440 .of_match = zynqmp_firmware_ids,
Michal Simek0fd9f362022-02-07 10:27:37 +0100441 .bind = zynqmp_firmware_bind,
Rajan Vaja833be322019-02-15 04:45:32 -0800442};