firmware: zynqmp: Do not report error if node is already configured
[platform/kernel/u-boot.git] / drivers / firmware / firmware-zynqmp.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Firmware driver
4  *
5  * Copyright (C) 2018-2019 Xilinx, Inc.
6  */
7
8 #include <common.h>
9 #include <cpu_func.h>
10 #include <dm.h>
11 #include <log.h>
12 #include <zynqmp_firmware.h>
13 #include <asm/cache.h>
14 #include <asm/ptrace.h>
15
16 #if defined(CONFIG_ZYNQMP_IPI)
17 #include <mailbox.h>
18 #include <asm/arch/sys_proto.h>
19
20 #define PMUFW_PAYLOAD_ARG_CNT   8
21
22 #define XST_PM_NO_ACCESS        2002L
23 #define XST_PM_ALREADY_CONFIGURED       2009L
24
25 struct zynqmp_power {
26         struct mbox_chan tx_chan;
27         struct mbox_chan rx_chan;
28 } zynqmp_power;
29
30 static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
31 {
32         struct zynqmp_ipi_msg msg;
33         int ret;
34         u32 buffer[PAYLOAD_ARG_CNT];
35
36         if (!res)
37                 res = buffer;
38
39         if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
40             res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
41                 return -EINVAL;
42
43         if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
44                 return -EINVAL;
45
46         debug("%s, Sending IPI message with ID: 0x%0x\n", __func__, req[0]);
47         msg.buf = (u32 *)req;
48         msg.len = req_len;
49         ret = mbox_send(&zynqmp_power.tx_chan, &msg);
50         if (ret) {
51                 debug("%s: Sending message failed\n", __func__);
52                 return ret;
53         }
54
55         msg.buf = res;
56         msg.len = res_maxlen;
57         ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
58         if (ret)
59                 debug("%s: Receiving message failed\n", __func__);
60
61         return ret;
62 }
63
64 unsigned int zynqmp_firmware_version(void)
65 {
66         int ret;
67         u32 ret_payload[PAYLOAD_ARG_CNT];
68         static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
69
70         /*
71          * Get PMU version only once and later
72          * just return stored values instead of
73          * asking PMUFW again.
74          **/
75         if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
76
77                 ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0,
78                                         ret_payload);
79                 if (ret)
80                         panic("PMUFW is not found - Please load it!\n");
81
82                 pm_api_version = ret_payload[1];
83                 if (pm_api_version < ZYNQMP_PM_VERSION)
84                         panic("PMUFW version error. Expected: v%d.%d\n",
85                               ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
86         }
87
88         return pm_api_version;
89 };
90
91 /**
92  * Send a configuration object to the PMU firmware.
93  *
94  * @cfg_obj: Pointer to the configuration object
95  * @size:    Size of @cfg_obj in bytes
96  */
97 void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
98 {
99         int err;
100         u32 ret_payload[PAYLOAD_ARG_CNT];
101
102         if (IS_ENABLED(CONFIG_SPL_BUILD))
103                 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
104
105         flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size));
106
107         err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0,
108                                 0, ret_payload);
109         if (err == XST_PM_NO_ACCESS) {
110                 printf("PMUFW no permission to change config object\n");
111                 return;
112         }
113
114         if (err == XST_PM_ALREADY_CONFIGURED) {
115                 debug("PMUFW Node is already configured\n");
116                 return;
117         }
118
119         if (err)
120                 printf("Cannot load PMUFW configuration object (%d)\n", err);
121
122         if (ret_payload[0])
123                 printf("PMUFW returned 0x%08x status!\n", ret_payload[0]);
124
125         if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD))
126                 panic("PMUFW config object loading failed in EL3\n");
127 }
128
129 static int zynqmp_power_probe(struct udevice *dev)
130 {
131         int ret;
132
133         debug("%s, (dev=%p)\n", __func__, dev);
134
135         ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
136         if (ret) {
137                 debug("%s: Cannot find tx mailbox\n", __func__);
138                 return ret;
139         }
140
141         ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
142         if (ret) {
143                 debug("%s: Cannot find rx mailbox\n", __func__);
144                 return ret;
145         }
146
147         ret = zynqmp_firmware_version();
148         printf("PMUFW:\tv%d.%d\n",
149                ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
150                ret & ZYNQMP_PM_VERSION_MINOR_MASK);
151
152         return 0;
153 };
154
155 static const struct udevice_id zynqmp_power_ids[] = {
156         { .compatible = "xlnx,zynqmp-power" },
157         { }
158 };
159
160 U_BOOT_DRIVER(zynqmp_power) = {
161         .name = "zynqmp_power",
162         .id = UCLASS_FIRMWARE,
163         .of_match = zynqmp_power_ids,
164         .probe = zynqmp_power_probe,
165 };
166 #endif
167
168 int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
169                                      u32 arg3, u32 *ret_payload)
170 {
171         debug("%s at EL%d, API ID: 0x%0x\n", __func__, current_el(), api_id);
172
173         if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) {
174 #if defined(CONFIG_ZYNQMP_IPI)
175                 /*
176                  * Use fixed payload and arg size as the EL2 call. The firmware
177                  * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the
178                  * firmware API is limited by the SMC call size
179                  */
180                 u32 regs[] = {api_id, arg0, arg1, arg2, arg3};
181                 int ret;
182
183                 if (api_id == PM_FPGA_LOAD) {
184                         /* Swap addr_hi/low because of incompatibility */
185                         u32 temp = regs[1];
186
187                         regs[1] = regs[2];
188                         regs[2] = temp;
189                 }
190
191                 ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload,
192                               PAYLOAD_ARG_CNT);
193                 if (ret)
194                         return ret;
195 #else
196                 return -EPERM;
197 #endif
198         } else {
199                 /*
200                  * Added SIP service call Function Identifier
201                  * Make sure to stay in x0 register
202                  */
203                 struct pt_regs regs;
204
205                 regs.regs[0] = PM_SIP_SVC | api_id;
206                 regs.regs[1] = ((u64)arg1 << 32) | arg0;
207                 regs.regs[2] = ((u64)arg3 << 32) | arg2;
208
209                 smc_call(&regs);
210
211                 if (ret_payload) {
212                         ret_payload[0] = (u32)regs.regs[0];
213                         ret_payload[1] = upper_32_bits(regs.regs[0]);
214                         ret_payload[2] = (u32)regs.regs[1];
215                         ret_payload[3] = upper_32_bits(regs.regs[1]);
216                         ret_payload[4] = (u32)regs.regs[2];
217                 }
218
219         }
220         return (ret_payload) ? ret_payload[0] : 0;
221 }
222
223 static const struct udevice_id zynqmp_firmware_ids[] = {
224         { .compatible = "xlnx,zynqmp-firmware" },
225         { .compatible = "xlnx,versal-firmware"},
226         { }
227 };
228
229 U_BOOT_DRIVER(zynqmp_firmware) = {
230         .id = UCLASS_FIRMWARE,
231         .name = "zynqmp_firmware",
232         .of_match = zynqmp_firmware_ids,
233 };