imx8m: config: convert to bootm_size
[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 <dm.h>
10 #include <log.h>
11 #include <zynqmp_firmware.h>
12 #include <asm/cache.h>
13 #include <asm/ptrace.h>
14
15 #if defined(CONFIG_ZYNQMP_IPI)
16 #include <mailbox.h>
17 #include <asm/arch/sys_proto.h>
18
19 #define PMUFW_PAYLOAD_ARG_CNT   8
20
21 #define XST_PM_NO_ACCESS        2002L
22
23 struct zynqmp_power {
24         struct mbox_chan tx_chan;
25         struct mbox_chan rx_chan;
26 } zynqmp_power;
27
28 static 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;
32
33         if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
34             res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
35                 return -EINVAL;
36
37         if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
38                 return -EINVAL;
39
40         msg.buf = (u32 *)req;
41         msg.len = req_len;
42         ret = mbox_send(&zynqmp_power.tx_chan, &msg);
43         if (ret) {
44                 debug("%s: Sending message failed\n", __func__);
45                 return ret;
46         }
47
48         msg.buf = res;
49         msg.len = res_maxlen;
50         ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
51         if (ret)
52                 debug("%s: Receiving message failed\n", __func__);
53
54         return ret;
55 }
56
57 static int send_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
58 {
59         if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3)
60                 return ipi_req(req, req_len, res, res_maxlen);
61
62         return xilinx_pm_request(req[0], 0, 0, 0, 0, res);
63 }
64
65 unsigned 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) {
77                 const u32 request[] = { PM_GET_API_VERSION };
78
79                 ret = send_req(request, ARRAY_SIZE(request), ret_payload, 2);
80                 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
92 /**
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  */
98 void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
99 {
100         const u32 request[] = {
101                 PM_SET_CONFIGURATION,
102                 (u32)((u64)cfg_obj)
103         };
104         u32 response = 0;
105         int err;
106
107         printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
108
109         err = send_req(request, ARRAY_SIZE(request), &response, 1);
110         if (err == XST_PM_NO_ACCESS) {
111                 printf("PMUFW no permission to change config object\n");
112                 return;
113         }
114
115         if (err)
116                 printf("Cannot load PMUFW configuration object (%d)\n", err);
117
118         if (response)
119                 printf("PMUFW returned 0x%08x status!\n", response);
120
121         if ((err || response) && IS_ENABLED(CONFIG_SPL_BUILD))
122                 panic("PMUFW config object loading failed in EL3\n");
123 }
124
125 static int zynqmp_power_probe(struct udevice *dev)
126 {
127         int ret;
128
129         debug("%s, (dev=%p)\n", __func__, dev);
130
131         ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
132         if (ret) {
133                 debug("%s: Cannot find tx mailbox\n", __func__);
134                 return ret;
135         }
136
137         ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
138         if (ret) {
139                 debug("%s: Cannot find rx mailbox\n", __func__);
140                 return ret;
141         }
142
143         ret = zynqmp_firmware_version();
144         printf("PMUFW:\tv%d.%d\n",
145                ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
146                ret & ZYNQMP_PM_VERSION_MINOR_MASK);
147
148         return 0;
149 };
150
151 static const struct udevice_id zynqmp_power_ids[] = {
152         { .compatible = "xlnx,zynqmp-power" },
153         { }
154 };
155
156 U_BOOT_DRIVER(zynqmp_power) = {
157         .name = "zynqmp_power",
158         .id = UCLASS_FIRMWARE,
159         .of_match = zynqmp_power_ids,
160         .probe = zynqmp_power_probe,
161 };
162 #endif
163
164 int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
165                                      u32 arg3, u32 *ret_payload)
166 {
167         /*
168          * Added SIP service call Function Identifier
169          * Make sure to stay in x0 register
170          */
171         struct pt_regs regs;
172
173         if (current_el() == 3) {
174                 printf("%s: Can't call SMC from EL3 context\n", __func__);
175                 return -EPERM;
176         }
177
178         regs.regs[0] = PM_SIP_SVC | api_id;
179         regs.regs[1] = ((u64)arg1 << 32) | arg0;
180         regs.regs[2] = ((u64)arg3 << 32) | arg2;
181
182         smc_call(&regs);
183
184         if (ret_payload) {
185                 ret_payload[0] = (u32)regs.regs[0];
186                 ret_payload[1] = upper_32_bits(regs.regs[0]);
187                 ret_payload[2] = (u32)regs.regs[1];
188                 ret_payload[3] = upper_32_bits(regs.regs[1]);
189                 ret_payload[4] = (u32)regs.regs[2];
190         }
191
192         return regs.regs[0];
193 }
194
195 static const struct udevice_id zynqmp_firmware_ids[] = {
196         { .compatible = "xlnx,zynqmp-firmware" },
197         { .compatible = "xlnx,versal-firmware"},
198         { }
199 };
200
201 U_BOOT_DRIVER(zynqmp_firmware) = {
202         .id = UCLASS_FIRMWARE,
203         .name = "zynqmp-firmware",
204         .of_match = zynqmp_firmware_ids,
205 };