blob: 8d78888eff701f1e2c8c529bc5dea58ff77bd50a [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
Ibai Erkiaga1327d162019-09-27 12:51:41 +020026struct zynqmp_power {
27 struct mbox_chan tx_chan;
28 struct mbox_chan rx_chan;
29} zynqmp_power;
30
Ibai Erkiaga490f6272019-09-27 11:37:00 +010031static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
32{
33 struct zynqmp_ipi_msg msg;
34 int ret;
Michal Simek53f5d162021-10-15 16:57:39 +020035 u32 buffer[PAYLOAD_ARG_CNT];
36
37 if (!res)
38 res = buffer;
Ibai Erkiaga490f6272019-09-27 11:37:00 +010039
40 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
41 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
42 return -EINVAL;
43
44 if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
45 return -EINVAL;
46
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +010047 debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
Ibai Erkiaga490f6272019-09-27 11:37:00 +010048 msg.buf = (u32 *)req;
49 msg.len = req_len;
50 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
51 if (ret) {
52 debug("%s: Sending message failed\n", __func__);
53 return ret;
54 }
55
56 msg.buf = res;
57 msg.len = res_maxlen;
58 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
59 if (ret)
60 debug("%s: Receiving message failed\n", __func__);
61
62 return ret;
63}
64
65unsigned int zynqmp_firmware_version(void)
66{
67 int ret;
68 u32 ret_payload[PAYLOAD_ARG_CNT];
69 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
70
71 /*
72 * Get PMU version only once and later
73 * just return stored values instead of
74 * asking PMUFW again.
75 **/
76 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Ibai Erkiaga490f6272019-09-27 11:37:00 +010077
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +010078 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
79 ret_payload);
Ibai Erkiaga490f6272019-09-27 11:37:00 +010080 if (ret)
81 panic("PMUFW is not found - Please load it!\n");
82
83 pm_api_version = ret_payload[1];
84 if (pm_api_version < ZYNQMP_PM_VERSION)
85 panic("PMUFW version error. Expected: v%d.%d\n",
86 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
87 }
88
89 return pm_api_version;
90};
91
Michal Simeka3e552b2019-09-27 14:20:00 +020092/**
93 * Send a configuration object to the PMU firmware.
94 *
95 * @cfg_obj: Pointer to the configuration object
96 * @size: Size of @cfg_obj in bytes
97 */
98void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
99{
Michal Simeka3e552b2019-09-27 14:20:00 +0200100 int err;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100101 u32 ret_payload[PAYLOAD_ARG_CNT];
Michal Simeka3e552b2019-09-27 14:20:00 +0200102
Michal Simek12662e72022-01-14 13:25:36 +0100103 if (IS_ENABLED(CONFIG_SPL_BUILD))
104 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
Michal Simeka3e552b2019-09-27 14:20:00 +0200105
Michal Simek380bd082021-11-18 13:00:02 +0100106 flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
107
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100108 err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
109 0, ret_payload);
Michal Simek4c86e082020-04-27 11:51:40 +0200110 if (err == XST_PM_NO_ACCESS) {
111 printf("PMUFW no permission to change config object\n");
112 return;
113 }
114
Michal Simek11c07712022-01-14 13:25:37 +0100115 if (err == XST_PM_ALREADY_CONFIGURED) {
116 debug("PMUFW Node is already configured\n");
117 return;
118 }
119
Michal Simeka3e552b2019-09-27 14:20:00 +0200120 if (err)
Michal Simek4c86e082020-04-27 11:51:40 +0200121 printf("Cannot load PMUFW configuration object (%d)\n", err);
122
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100123 if (ret_payload[0])
124 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
Michal Simek4c86e082020-04-27 11:51:40 +0200125
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100126 if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
Michal Simek4c86e082020-04-27 11:51:40 +0200127 panic("PMUFW config object loading failed in EL3\n");
Michal Simeka3e552b2019-09-27 14:20:00 +0200128}
129
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200130static int zynqmp_power_probe(struct udevice *dev)
131{
Michal Simek44dccd52019-10-10 11:26:16 +0200132 int ret;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200133
134 debug("%s, (dev=%p)\n", __func__, dev);
135
136 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
137 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200138 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200139 return ret;
140 }
141
142 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100143 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200144 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100145 return ret;
146 }
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200147
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100148 ret = zynqmp_firmware_version();
149 printf("PMUFW:\tv%d.%d\n",
150 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
151 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
152
153 return 0;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200154};
155
156static const struct udevice_id zynqmp_power_ids[] = {
157 { .compatible = "xlnx,zynqmp-power" },
158 { }
159};
160
161U_BOOT_DRIVER(zynqmp_power) = {
162 .name = "zynqmp_power",
163 .id = UCLASS_FIRMWARE,
164 .of_match = zynqmp_power_ids,
165 .probe = zynqmp_power_probe,
166};
167#endif
168
Michal Simek40361952019-10-04 15:35:45 +0200169int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
170 u32 arg3, u32 *ret_payload)
Michal Simek866225f2019-10-04 15:45:29 +0200171{
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100172 debug("%s at EL%d, API ID: 0x%0x\n", __func__, current_el(), api_id);
Michal Simek866225f2019-10-04 15:45:29 +0200173
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100174 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
175#if defined(CONFIG_ZYNQMP_IPI)
176 /*
177 * Use fixed payload and arg size as the EL2 call. The firmware
178 * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
179 * firmware API is limited by the SMC call size
180 */
181 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
Michal Simekb05cc382021-10-15 16:57:38 +0200182 int ret;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100183
Michal Simek56901282020-10-05 15:23:28 +0200184 if (api_id == PM_FPGA_LOAD) {
185 /* Swap addr_hi/low because of incompatibility */
186 u32 temp = regs[1];
187
188 regs[1] = regs[2];
189 regs[2] = temp;
190 }
191
Michal Simekb05cc382021-10-15 16:57:38 +0200192 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
193 PAYLOAD_ARG_CNT);
194 if (ret)
195 return ret;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100196#else
Michal Simek9bed8a62019-10-10 11:09:15 +0200197 return -EPERM;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100198#endif
199 } else {
200 /*
201 * Added SIP service call Function Identifier
202 * Make sure to stay in x0 register
203 */
204 struct pt_regs regs;
205
206 regs.regs[0] = PM_SIP_SVC | api_id;
207 regs.regs[1] = ((u64)arg1 << 32) | arg0;
208 regs.regs[2] = ((u64)arg3 << 32) | arg2;
209
210 smc_call(&regs);
211
212 if (ret_payload) {
213 ret_payload[0] = (u32)regs.regs[0];
214 ret_payload[1] = upper_32_bits(regs.regs[0]);
215 ret_payload[2] = (u32)regs.regs[1];
216 ret_payload[3] = upper_32_bits(regs.regs[1]);
217 ret_payload[4] = (u32)regs.regs[2];
218 }
219
Michal Simek9bed8a62019-10-10 11:09:15 +0200220 }
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100221 return (ret_payload) ? ret_payload[0] : 0;
Michal Simek866225f2019-10-04 15:45:29 +0200222}
223
Rajan Vaja14723ed2019-02-15 04:45:32 -0800224static const struct udevice_id zynqmp_firmware_ids[] = {
225 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530226 { .compatible = "xlnx,versal-firmware"},
Rajan Vaja14723ed2019-02-15 04:45:32 -0800227 { }
228};
229
Michal Simeke0283cb2022-02-07 10:27:37 +0100230static int zynqmp_firmware_bind(struct udevice *dev)
231{
232 int ret;
233 struct udevice *child;
234
235 if (IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) {
236 ret = device_bind_driver_to_node(dev, "zynqmp_power_domain",
237 "zynqmp_power_domain",
238 dev_ofnode(dev), &child);
239 if (ret) {
240 printf("zynqmp power domain driver is not bound: %d\n", ret);
241 return ret;
242 }
243 }
244
245 return dm_scan_fdt_dev(dev);
246}
247
Rajan Vaja14723ed2019-02-15 04:45:32 -0800248U_BOOT_DRIVER(zynqmp_firmware) = {
249 .id = UCLASS_FIRMWARE,
Michal Simek6c0e59f2020-01-07 08:50:34 +0100250 .name = "zynqmp_firmware",
Rajan Vaja14723ed2019-02-15 04:45:32 -0800251 .of_match = zynqmp_firmware_ids,
Michal Simeke0283cb2022-02-07 10:27:37 +0100252 .bind = zynqmp_firmware_bind,
Rajan Vaja14723ed2019-02-15 04:45:32 -0800253};