blob: b44fede3079938708060511bc6a69c73e20fadb1 [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>
Rajan Vaja14723ed2019-02-15 04:45:32 -08009#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Michal Simek866225f2019-10-04 15:45:29 +020011#include <zynqmp_firmware.h>
Simon Glass90526e92020-05-10 11:39:56 -060012#include <asm/cache.h>
Simon Glass25a58182020-05-10 11:40:06 -060013#include <asm/ptrace.h>
Rajan Vaja14723ed2019-02-15 04:45:32 -080014
Ibai Erkiaga1327d162019-09-27 12:51:41 +020015#if defined(CONFIG_ZYNQMP_IPI)
16#include <mailbox.h>
17#include <asm/arch/sys_proto.h>
18
Ibai Erkiaga490f6272019-09-27 11:37:00 +010019#define PMUFW_PAYLOAD_ARG_CNT 8
20
Michal Simek4c86e082020-04-27 11:51:40 +020021#define XST_PM_NO_ACCESS 2002L
22
Ibai Erkiaga1327d162019-09-27 12:51:41 +020023struct zynqmp_power {
24 struct mbox_chan tx_chan;
25 struct mbox_chan rx_chan;
26} zynqmp_power;
27
Ibai Erkiaga490f6272019-09-27 11:37:00 +010028static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
29{
30 struct zynqmp_ipi_msg msg;
31 int ret;
Michal Simek53f5d162021-10-15 16:57:39 +020032 u32 buffer[PAYLOAD_ARG_CNT];
33
34 if (!res)
35 res = buffer;
Ibai Erkiaga490f6272019-09-27 11:37:00 +010036
37 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
38 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
39 return -EINVAL;
40
41 if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
42 return -EINVAL;
43
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +010044 debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
Ibai Erkiaga490f6272019-09-27 11:37:00 +010045 msg.buf = (u32 *)req;
46 msg.len = req_len;
47 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
48 if (ret) {
49 debug("%s: Sending message failed\n", __func__);
50 return ret;
51 }
52
53 msg.buf = res;
54 msg.len = res_maxlen;
55 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
56 if (ret)
57 debug("%s: Receiving message failed\n", __func__);
58
59 return ret;
60}
61
62unsigned int zynqmp_firmware_version(void)
63{
64 int ret;
65 u32 ret_payload[PAYLOAD_ARG_CNT];
66 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
67
68 /*
69 * Get PMU version only once and later
70 * just return stored values instead of
71 * asking PMUFW again.
72 **/
73 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Ibai Erkiaga490f6272019-09-27 11:37:00 +010074
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +010075 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
76 ret_payload);
Ibai Erkiaga490f6272019-09-27 11:37:00 +010077 if (ret)
78 panic("PMUFW is not found - Please load it!\n");
79
80 pm_api_version = ret_payload[1];
81 if (pm_api_version < ZYNQMP_PM_VERSION)
82 panic("PMUFW version error. Expected: v%d.%d\n",
83 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
84 }
85
86 return pm_api_version;
87};
88
Michal Simeka3e552b2019-09-27 14:20:00 +020089/**
90 * Send a configuration object to the PMU firmware.
91 *
92 * @cfg_obj: Pointer to the configuration object
93 * @size: Size of @cfg_obj in bytes
94 */
95void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
96{
Michal Simeka3e552b2019-09-27 14:20:00 +020097 int err;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +010098 u32 ret_payload[PAYLOAD_ARG_CNT];
Michal Simeka3e552b2019-09-27 14:20:00 +020099
100 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
101
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100102 err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
103 0, ret_payload);
Michal Simek4c86e082020-04-27 11:51:40 +0200104 if (err == XST_PM_NO_ACCESS) {
105 printf("PMUFW no permission to change config object\n");
106 return;
107 }
108
Michal Simeka3e552b2019-09-27 14:20:00 +0200109 if (err)
Michal Simek4c86e082020-04-27 11:51:40 +0200110 printf("Cannot load PMUFW configuration object (%d)\n", err);
111
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100112 if (ret_payload[0])
113 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
Michal Simek4c86e082020-04-27 11:51:40 +0200114
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100115 if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
Michal Simek4c86e082020-04-27 11:51:40 +0200116 panic("PMUFW config object loading failed in EL3\n");
Michal Simeka3e552b2019-09-27 14:20:00 +0200117}
118
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200119static int zynqmp_power_probe(struct udevice *dev)
120{
Michal Simek44dccd52019-10-10 11:26:16 +0200121 int ret;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200122
123 debug("%s, (dev=%p)\n", __func__, dev);
124
125 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
126 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200127 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200128 return ret;
129 }
130
131 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100132 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200133 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100134 return ret;
135 }
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200136
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100137 ret = zynqmp_firmware_version();
138 printf("PMUFW:\tv%d.%d\n",
139 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
140 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
141
142 return 0;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200143};
144
145static const struct udevice_id zynqmp_power_ids[] = {
146 { .compatible = "xlnx,zynqmp-power" },
147 { }
148};
149
150U_BOOT_DRIVER(zynqmp_power) = {
151 .name = "zynqmp_power",
152 .id = UCLASS_FIRMWARE,
153 .of_match = zynqmp_power_ids,
154 .probe = zynqmp_power_probe,
155};
156#endif
157
Michal Simek40361952019-10-04 15:35:45 +0200158int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
159 u32 arg3, u32 *ret_payload)
Michal Simek866225f2019-10-04 15:45:29 +0200160{
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100161 debug("%s at EL%d, API ID: 0x%0x\n", __func__, current_el(), api_id);
Michal Simek866225f2019-10-04 15:45:29 +0200162
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100163 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
164#if defined(CONFIG_ZYNQMP_IPI)
165 /*
166 * Use fixed payload and arg size as the EL2 call. The firmware
167 * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
168 * firmware API is limited by the SMC call size
169 */
170 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
Michal Simekb05cc382021-10-15 16:57:38 +0200171 int ret;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100172
Michal Simek56901282020-10-05 15:23:28 +0200173 if (api_id == PM_FPGA_LOAD) {
174 /* Swap addr_hi/low because of incompatibility */
175 u32 temp = regs[1];
176
177 regs[1] = regs[2];
178 regs[2] = temp;
179 }
180
Michal Simekb05cc382021-10-15 16:57:38 +0200181 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
182 PAYLOAD_ARG_CNT);
183 if (ret)
184 return ret;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100185#else
Michal Simek9bed8a62019-10-10 11:09:15 +0200186 return -EPERM;
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100187#endif
188 } else {
189 /*
190 * Added SIP service call Function Identifier
191 * Make sure to stay in x0 register
192 */
193 struct pt_regs regs;
194
195 regs.regs[0] = PM_SIP_SVC | api_id;
196 regs.regs[1] = ((u64)arg1 << 32) | arg0;
197 regs.regs[2] = ((u64)arg3 << 32) | arg2;
198
199 smc_call(&regs);
200
201 if (ret_payload) {
202 ret_payload[0] = (u32)regs.regs[0];
203 ret_payload[1] = upper_32_bits(regs.regs[0]);
204 ret_payload[2] = (u32)regs.regs[1];
205 ret_payload[3] = upper_32_bits(regs.regs[1]);
206 ret_payload[4] = (u32)regs.regs[2];
207 }
208
Michal Simek9bed8a62019-10-10 11:09:15 +0200209 }
Ibai Erkiaga2eabb6b2020-08-04 23:17:27 +0100210 return (ret_payload) ? ret_payload[0] : 0;
Michal Simek866225f2019-10-04 15:45:29 +0200211}
212
Rajan Vaja14723ed2019-02-15 04:45:32 -0800213static const struct udevice_id zynqmp_firmware_ids[] = {
214 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530215 { .compatible = "xlnx,versal-firmware"},
Rajan Vaja14723ed2019-02-15 04:45:32 -0800216 { }
217};
218
219U_BOOT_DRIVER(zynqmp_firmware) = {
220 .id = UCLASS_FIRMWARE,
Michal Simek6c0e59f2020-01-07 08:50:34 +0100221 .name = "zynqmp_firmware",
Rajan Vaja14723ed2019-02-15 04:45:32 -0800222 .of_match = zynqmp_firmware_ids,
223};