blob: 43fb7fa7787e5ef62b5bc3ff521bc9bcf35cdafd [file] [log] [blame]
Rajan Vaja14723ed2019-02-15 04:45:32 -08001// SPDX-License-Identifier: GPL-2.0
Ibai Erkiaga1327d162019-09-27 12:51:41 +02002/*
3 * Xilinx Zynq MPSoC Firmware driver
4 *
5 * Copyright (C) 2018-2019 Xilinx, Inc.
6 */
Rajan Vaja14723ed2019-02-15 04:45:32 -08007
Ibai Erkiaga1327d162019-09-27 12:51:41 +02008#include <common.h>
Michal Simek380bd082021-11-18 13:00:02 +01009#include <cpu_func.h>
Rajan Vaja14723ed2019-02-15 04:45:32 -080010#include <dm.h>
Michal Simeke0283cb2022-02-07 10:27:37 +010011#include <dm/lists.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Michal Simek866225f2019-10-04 15:45:29 +020013#include <zynqmp_firmware.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass25a58182020-05-10 11:40:06 -060015#include <asm/ptrace.h>
Rajan Vaja14723ed2019-02-15 04:45:32 -080016
Ibai Erkiaga1327d162019-09-27 12:51:41 +020017#if defined(CONFIG_ZYNQMP_IPI)
18#include <mailbox.h>
19#include <asm/arch/sys_proto.h>
20
Ibai Erkiaga490f6272019-09-27 11:37:00 +010021#define PMUFW_PAYLOAD_ARG_CNT 8
22
Michal Simek4c86e082020-04-27 11:51:40 +020023#define XST_PM_NO_ACCESS 2002L
Michal Simek11c07712022-01-14 13:25:37 +010024#define XST_PM_ALREADY_CONFIGURED 2009L
Michal Simek4c86e082020-04-27 11:51:40 +020025
Stefan Herbrechtsmeier49732242023-05-23 14:42:14 +020026static struct zynqmp_power {
Ibai Erkiaga1327d162019-09-27 12:51:41 +020027 struct mbox_chan tx_chan;
28 struct mbox_chan rx_chan;
Stefan Herbrechtsmeier49732242023-05-23 14:42:14 +020029} zynqmp_power __section(".data");
Ibai Erkiaga1327d162019-09-27 12:51:41 +020030
Michal Simekc750c6d2022-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 Simekfac46bc2022-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{
Stefan Herbrechtsmeierec473972023-05-23 14:42:13 +020066 return zynqmp_pmufw_load_config_object(xpm_configobject_close,
67 sizeof(xpm_configobject_close));
Michal Simekfac46bc2022-01-14 13:25:38 +010068}
69
Michal Simekc750c6d2022-01-14 13:25:35 +010070int zynqmp_pmufw_node(u32 id)
71{
Stefan Herbrechtsmeierec473972023-05-23 14:42:13 +020072 static bool check = true;
73 static bool permission = true;
Ashok Reddy Soma2e40ab12022-07-22 02:46:55 -060074
Stefan Herbrechtsmeierec473972023-05-23 14:42:13 +020075 if (check) {
76 check = false;
77
78 if (zynqmp_pmufw_node(NODE_OCM_BANK_0) == -EACCES) {
79 printf("PMUFW: No permission to change config object\n");
80 permission = false;
81 }
82 }
83
84 if (!permission)
Ashok Reddy Soma2e40ab12022-07-22 02:46:55 -060085 return 0;
86
Michal Simekc750c6d2022-01-14 13:25:35 +010087 /* Record power domain id */
88 xpm_configobject[NODE_ID_LOCATION] = id;
89
Stefan Herbrechtsmeierec473972023-05-23 14:42:13 +020090 return zynqmp_pmufw_load_config_object(xpm_configobject,
91 sizeof(xpm_configobject));
Michal Simekc750c6d2022-01-14 13:25:35 +010092}
93
Stefan Herbrechtsmeierf851be12022-06-20 18:36:37 +020094static int do_pm_probe(void)
95{
96 struct udevice *dev;
97 int ret;
98
99 ret = uclass_get_device_by_driver(UCLASS_FIRMWARE,
100 DM_DRIVER_GET(zynqmp_power),
101 &dev);
102 if (ret)
103 debug("%s: Probing device failed: %d\n", __func__, ret);
104
105 return ret;
106}
107
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100108static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
109{
110 struct zynqmp_ipi_msg msg;
111 int ret;
Michal Simek53f5d162021-10-15 16:57:39 +0200112 u32 buffer[PAYLOAD_ARG_CNT];
113
114 if (!res)
115 res = buffer;
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100116
117 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
118 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
119 return -EINVAL;
120
Stefan Herbrechtsmeierf851be12022-06-20 18:36:37 +0200121 if (!(zynqmp_power.tx_chan.dev) || !(zynqmp_power.rx_chan.dev)) {
122 ret = do_pm_probe();
123 if (ret)
124 return ret;
125 }
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100126
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100127 debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100128 msg.buf = (u32 *)req;
129 msg.len = req_len;
130 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
131 if (ret) {
132 debug("%s: Sending message failed\n", __func__);
133 return ret;
134 }
135
136 msg.buf = res;
137 msg.len = res_maxlen;
138 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
139 if (ret)
140 debug("%s: Receiving message failed\n", __func__);
141
142 return ret;
143}
144
145unsigned int zynqmp_firmware_version(void)
146{
147 int ret;
148 u32 ret_payload[PAYLOAD_ARG_CNT];
149 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
150
151 /*
152 * Get PMU version only once and later
153 * just return stored values instead of
154 * asking PMUFW again.
155 **/
156 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100157
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100158 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
159 ret_payload);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100160 if (ret)
161 panic("PMUFW is not found - Please load it!\n");
162
163 pm_api_version = ret_payload[1];
164 if (pm_api_version < ZYNQMP_PM_VERSION)
165 panic("PMUFW version error. Expected: v%d.%d\n",
166 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
167 }
168
169 return pm_api_version;
170};
171
T Karthik Reddy7011efc2022-03-30 11:07:57 +0200172int zynqmp_pm_set_gem_config(u32 node, enum pm_gem_config_type config, u32 value)
173{
174 int ret;
175
176 ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_GEM_CONFIG,
177 config, value, NULL);
178 if (ret)
179 printf("%s: node %d: set_gem_config %d failed\n",
180 __func__, node, config);
181
182 return ret;
183}
184
Ashok Reddy Soma7d9ee462022-02-23 15:36:03 +0100185int zynqmp_pm_set_sd_config(u32 node, enum pm_sd_config_type config, u32 value)
186{
187 int ret;
188
189 ret = xilinx_pm_request(PM_IOCTL, node, IOCTL_SET_SD_CONFIG,
190 config, value, NULL);
191 if (ret)
192 printf("%s: node %d: set_sd_config %d failed\n",
193 __func__, node, config);
194
195 return ret;
196}
197
Ashok Reddy Somaf16347f2023-08-10 23:48:27 -0600198int zynqmp_pm_feature(const u32 api_id)
199{
200 int ret;
201 u32 ret_payload[PAYLOAD_ARG_CNT];
202
203 /* Check feature check API version */
204 ret = xilinx_pm_request(PM_FEATURE_CHECK, api_id, 0, 0, 0,
205 ret_payload);
206
207 /* Return feature check version */
208 return ret_payload[1] & FIRMWARE_VERSION_MASK;
209}
210
Ashok Reddy Soma7d9ee462022-02-23 15:36:03 +0100211int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
212{
213 int ret;
214 u32 *bit_mask;
215 u32 ret_payload[PAYLOAD_ARG_CNT];
216
217 /* Input arguments validation */
218 if (id >= 64 || (api_id != PM_IOCTL && api_id != PM_QUERY_DATA))
219 return -EINVAL;
220
221 /* Check feature check API version */
222 ret = xilinx_pm_request(PM_FEATURE_CHECK, PM_FEATURE_CHECK, 0, 0, 0,
223 ret_payload);
224 if (ret)
225 return ret;
226
227 /* Check if feature check version 2 is supported or not */
228 if ((ret_payload[1] & FIRMWARE_VERSION_MASK) == PM_API_VERSION_2) {
229 /*
230 * Call feature check for IOCTL/QUERY API to get IOCTL ID or
231 * QUERY ID feature status.
232 */
233
234 ret = xilinx_pm_request(PM_FEATURE_CHECK, api_id, 0, 0, 0,
235 ret_payload);
236 if (ret)
237 return ret;
238
239 bit_mask = &ret_payload[2];
240 if ((bit_mask[(id / 32)] & BIT((id % 32))) == 0)
241 return -EOPNOTSUPP;
242 } else {
243 return -ENODATA;
244 }
245
246 return 0;
247}
248
Michal Simeka3e552b2019-09-27 14:20:00 +0200249/**
250 * Send a configuration object to the PMU firmware.
251 *
252 * @cfg_obj: Pointer to the configuration object
253 * @size: Size of @cfg_obj in bytes
Stefan Herbrechtsmeieree3c02a2023-05-23 14:42:10 +0200254 * Return: 0 on success otherwise negative errno.
Michal Simeka3e552b2019-09-27 14:20:00 +0200255 */
Ashok Reddy Somac9f12ed2022-07-22 02:46:54 -0600256int zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
Michal Simeka3e552b2019-09-27 14:20:00 +0200257{
Michal Simeka3e552b2019-09-27 14:20:00 +0200258 int err;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100259 u32 ret_payload[PAYLOAD_ARG_CNT];
Michal Simeka3e552b2019-09-27 14:20:00 +0200260
Michal Simek12662e72022-01-14 13:25:36 +0100261 if (IS_ENABLED(CONFIG_SPL_BUILD))
262 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
Michal Simeka3e552b2019-09-27 14:20:00 +0200263
Michal Simek380bd082021-11-18 13:00:02 +0100264 flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
265
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100266 err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
267 0, ret_payload);
Michal Simek4c86e082020-04-27 11:51:40 +0200268 if (err == XST_PM_NO_ACCESS) {
Ashok Reddy Somac9f12ed2022-07-22 02:46:54 -0600269 return -EACCES;
Michal Simek4c86e082020-04-27 11:51:40 +0200270 }
271
Michal Simek11c07712022-01-14 13:25:37 +0100272 if (err == XST_PM_ALREADY_CONFIGURED) {
273 debug("PMUFW Node is already configured\n");
Ashok Reddy Somac9f12ed2022-07-22 02:46:54 -0600274 return -ENODEV;
Michal Simek11c07712022-01-14 13:25:37 +0100275 }
276
Michal Simeka3e552b2019-09-27 14:20:00 +0200277 if (err)
Michal Simek4c86e082020-04-27 11:51:40 +0200278 printf("Cannot load PMUFW configuration object (%d)\n", err);
279
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100280 if (ret_payload[0])
281 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
Michal Simek4c86e082020-04-27 11:51:40 +0200282
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100283 if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
Michal Simek4c86e082020-04-27 11:51:40 +0200284 panic("PMUFW config object loading failed in EL3\n");
Ashok Reddy Somac9f12ed2022-07-22 02:46:54 -0600285
286 return 0;
Michal Simeka3e552b2019-09-27 14:20:00 +0200287}
288
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200289static int zynqmp_power_probe(struct udevice *dev)
290{
Michal Simek44dccd52019-10-10 11:26:16 +0200291 int ret;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200292
293 debug("%s, (dev=%p)\n", __func__, dev);
294
295 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
296 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200297 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200298 return ret;
299 }
300
301 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100302 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200303 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100304 return ret;
305 }
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200306
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100307 ret = zynqmp_firmware_version();
308 printf("PMUFW:\tv%d.%d\n",
309 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
310 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
311
312 return 0;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200313};
314
315static const struct udevice_id zynqmp_power_ids[] = {
316 { .compatible = "xlnx,zynqmp-power" },
317 { }
318};
319
320U_BOOT_DRIVER(zynqmp_power) = {
321 .name = "zynqmp_power",
322 .id = UCLASS_FIRMWARE,
323 .of_match = zynqmp_power_ids,
324 .probe = zynqmp_power_probe,
325};
326#endif
327
Michal Simek40361952019-10-04 15:35:45 +0200328int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
329 u32 arg3, u32 *ret_payload)
Michal Simek866225f2019-10-04 15:45:29 +0200330{
Michal Simeka4444bf2023-06-01 13:34:34 +0200331 debug("%s at EL%d, API ID: 0x%0x, 0x%0x, 0x%0x, 0x%0x, 0x%0x\n",
332 __func__, current_el(), api_id, arg0, arg1, arg2, arg3);
Michal Simek866225f2019-10-04 15:45:29 +0200333
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100334 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
335#if defined(CONFIG_ZYNQMP_IPI)
336 /*
337 * Use fixed payload and arg size as the EL2 call. The firmware
338 * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
339 * firmware API is limited by the SMC call size
340 */
341 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
Michal Simekb05cc382021-10-15 16:57:38 +0200342 int ret;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100343
Michal Simek56901282020-10-05 15:23:28 +0200344 if (api_id == PM_FPGA_LOAD) {
345 /* Swap addr_hi/low because of incompatibility */
346 u32 temp = regs[1];
347
348 regs[1] = regs[2];
349 regs[2] = temp;
350 }
351
Michal Simekb05cc382021-10-15 16:57:38 +0200352 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
353 PAYLOAD_ARG_CNT);
354 if (ret)
355 return ret;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100356#else
Michal Simek9bed8a62019-10-10 11:09:15 +0200357 return -EPERM;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100358#endif
359 } else {
360 /*
361 * Added SIP service call Function Identifier
362 * Make sure to stay in x0 register
363 */
364 struct pt_regs regs;
365
366 regs.regs[0] = PM_SIP_SVC | api_id;
367 regs.regs[1] = ((u64)arg1 << 32) | arg0;
368 regs.regs[2] = ((u64)arg3 << 32) | arg2;
369
370 smc_call(&regs);
371
372 if (ret_payload) {
373 ret_payload[0] = (u32)regs.regs[0];
374 ret_payload[1] = upper_32_bits(regs.regs[0]);
375 ret_payload[2] = (u32)regs.regs[1];
376 ret_payload[3] = upper_32_bits(regs.regs[1]);
377 ret_payload[4] = (u32)regs.regs[2];
378 }
379
Michal Simek9bed8a62019-10-10 11:09:15 +0200380 }
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100381 return (ret_payload) ? ret_payload[0] : 0;
Michal Simek866225f2019-10-04 15:45:29 +0200382}
383
Rajan Vaja14723ed2019-02-15 04:45:32 -0800384static const struct udevice_id zynqmp_firmware_ids[] = {
385 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530386 { .compatible = "xlnx,versal-firmware"},
Jay Buddhabhatti2a00cef2022-09-19 14:21:06 +0200387 { .compatible = "xlnx,versal-net-firmware"},
Rajan Vaja14723ed2019-02-15 04:45:32 -0800388 { }
389};
390
Michal Simeke0283cb2022-02-07 10:27:37 +0100391static int zynqmp_firmware_bind(struct udevice *dev)
392{
393 int ret;
394 struct udevice *child;
395
Michal Simekf307c682022-02-28 17:13:15 +0100396 if ((IS_ENABLED(CONFIG_SPL_BUILD) &&
397 IS_ENABLED(CONFIG_SPL_POWER_DOMAIN) &&
398 IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) ||
399 (!IS_ENABLED(CONFIG_SPL_BUILD) &&
400 IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN))) {
Michal Simeke0283cb2022-02-07 10:27:37 +0100401 ret = device_bind_driver_to_node(dev, "zynqmp_power_domain",
402 "zynqmp_power_domain",
403 dev_ofnode(dev), &child);
404 if (ret) {
405 printf("zynqmp power domain driver is not bound: %d\n", ret);
406 return ret;
407 }
408 }
409
Stefan Herbrechtsmeierbc75a342023-05-23 14:42:11 +0200410 return 0;
Michal Simeke0283cb2022-02-07 10:27:37 +0100411}
412
Rajan Vaja14723ed2019-02-15 04:45:32 -0800413U_BOOT_DRIVER(zynqmp_firmware) = {
414 .id = UCLASS_FIRMWARE,
Michal Simek6c0e59f2020-01-07 08:50:34 +0100415 .name = "zynqmp_firmware",
Rajan Vaja14723ed2019-02-15 04:45:32 -0800416 .of_match = zynqmp_firmware_ids,
Michal Simeke0283cb2022-02-07 10:27:37 +0100417 .bind = zynqmp_firmware_bind,
Rajan Vaja14723ed2019-02-15 04:45:32 -0800418};