common: Drop net.h from common header
[oweals/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 <zynqmp_firmware.h>
11 #include <asm/cache.h>
12
13 #if defined(CONFIG_ZYNQMP_IPI)
14 #include <mailbox.h>
15 #include <asm/arch/sys_proto.h>
16
17 #define PMUFW_PAYLOAD_ARG_CNT   8
18
19 struct zynqmp_power {
20         struct mbox_chan tx_chan;
21         struct mbox_chan rx_chan;
22 } zynqmp_power;
23
24 static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
25 {
26         struct zynqmp_ipi_msg msg;
27         int ret;
28
29         if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
30             res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
31                 return -EINVAL;
32
33         if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
34                 return -EINVAL;
35
36         msg.buf = (u32 *)req;
37         msg.len = req_len;
38         ret = mbox_send(&zynqmp_power.tx_chan, &msg);
39         if (ret) {
40                 debug("%s: Sending message failed\n", __func__);
41                 return ret;
42         }
43
44         msg.buf = res;
45         msg.len = res_maxlen;
46         ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
47         if (ret)
48                 debug("%s: Receiving message failed\n", __func__);
49
50         return ret;
51 }
52
53 static int send_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
54 {
55         if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3)
56                 return ipi_req(req, req_len, res, res_maxlen);
57
58         return xilinx_pm_request(req[0], 0, 0, 0, 0, res);
59 }
60
61 unsigned int zynqmp_firmware_version(void)
62 {
63         int ret;
64         u32 ret_payload[PAYLOAD_ARG_CNT];
65         static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
66
67         /*
68          * Get PMU version only once and later
69          * just return stored values instead of
70          * asking PMUFW again.
71          **/
72         if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
73                 const u32 request[] = { PM_GET_API_VERSION };
74
75                 ret = send_req(request, ARRAY_SIZE(request), ret_payload, 2);
76                 if (ret)
77                         panic("PMUFW is not found - Please load it!\n");
78
79                 pm_api_version = ret_payload[1];
80                 if (pm_api_version < ZYNQMP_PM_VERSION)
81                         panic("PMUFW version error. Expected: v%d.%d\n",
82                               ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
83         }
84
85         return pm_api_version;
86 };
87
88 /**
89  * Send a configuration object to the PMU firmware.
90  *
91  * @cfg_obj: Pointer to the configuration object
92  * @size:    Size of @cfg_obj in bytes
93  */
94 void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
95 {
96         const u32 request[] = {
97                 PM_SET_CONFIGURATION,
98                 (u32)((u64)cfg_obj)
99         };
100         u32 response;
101         int err;
102
103         printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
104
105         err = send_req(request, ARRAY_SIZE(request), &response, 1);
106         if (err)
107                 panic("Cannot load PMUFW configuration object (%d)\n", err);
108         if (response != 0)
109                 panic("PMUFW returned 0x%08x status!\n", response);
110 }
111
112 static int zynqmp_power_probe(struct udevice *dev)
113 {
114         int ret;
115
116         debug("%s, (dev=%p)\n", __func__, dev);
117
118         ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
119         if (ret) {
120                 debug("%s: Cannot find tx mailbox\n", __func__);
121                 return ret;
122         }
123
124         ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
125         if (ret) {
126                 debug("%s: Cannot find rx mailbox\n", __func__);
127                 return ret;
128         }
129
130         ret = zynqmp_firmware_version();
131         printf("PMUFW:\tv%d.%d\n",
132                ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
133                ret & ZYNQMP_PM_VERSION_MINOR_MASK);
134
135         return 0;
136 };
137
138 static const struct udevice_id zynqmp_power_ids[] = {
139         { .compatible = "xlnx,zynqmp-power" },
140         { }
141 };
142
143 U_BOOT_DRIVER(zynqmp_power) = {
144         .name = "zynqmp_power",
145         .id = UCLASS_FIRMWARE,
146         .of_match = zynqmp_power_ids,
147         .probe = zynqmp_power_probe,
148 };
149 #endif
150
151 int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
152                                      u32 arg3, u32 *ret_payload)
153 {
154         /*
155          * Added SIP service call Function Identifier
156          * Make sure to stay in x0 register
157          */
158         struct pt_regs regs;
159
160         if (current_el() == 3) {
161                 printf("%s: Can't call SMC from EL3 context\n", __func__);
162                 return -EPERM;
163         }
164
165         regs.regs[0] = PM_SIP_SVC | api_id;
166         regs.regs[1] = ((u64)arg1 << 32) | arg0;
167         regs.regs[2] = ((u64)arg3 << 32) | arg2;
168
169         smc_call(&regs);
170
171         if (ret_payload) {
172                 ret_payload[0] = (u32)regs.regs[0];
173                 ret_payload[1] = upper_32_bits(regs.regs[0]);
174                 ret_payload[2] = (u32)regs.regs[1];
175                 ret_payload[3] = upper_32_bits(regs.regs[1]);
176                 ret_payload[4] = (u32)regs.regs[2];
177         }
178
179         return regs.regs[0];
180 }
181
182 static const struct udevice_id zynqmp_firmware_ids[] = {
183         { .compatible = "xlnx,zynqmp-firmware" },
184         { .compatible = "xlnx,versal-firmware"},
185         { }
186 };
187
188 U_BOOT_DRIVER(zynqmp_firmware) = {
189         .id = UCLASS_FIRMWARE,
190         .name = "zynqmp-firmware",
191         .of_match = zynqmp_firmware_ids,
192 };