blob: 2bdf7771f642fdabd2adb20ef39d471f6f1d8356 [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
Ibai Erkiaga1327d162019-09-27 12:51:41 +020021struct zynqmp_power {
22 struct mbox_chan tx_chan;
23 struct mbox_chan rx_chan;
24} zynqmp_power;
25
Ibai Erkiaga490f6272019-09-27 11:37:00 +010026static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
27{
28 struct zynqmp_ipi_msg msg;
29 int ret;
30
31 if (req_len > PMUFW_PAYLOAD_ARG_CNT ||
32 res_maxlen > PMUFW_PAYLOAD_ARG_CNT)
33 return -EINVAL;
34
35 if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev))
36 return -EINVAL;
37
38 msg.buf = (u32 *)req;
39 msg.len = req_len;
40 ret = mbox_send(&zynqmp_power.tx_chan, &msg);
41 if (ret) {
42 debug("%s: Sending message failed\n", __func__);
43 return ret;
44 }
45
46 msg.buf = res;
47 msg.len = res_maxlen;
48 ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100);
49 if (ret)
50 debug("%s: Receiving message failed\n", __func__);
51
52 return ret;
53}
54
Michal Simekb6039aa2019-09-27 14:08:41 +020055static int send_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
56{
Michal Simek73319ee2020-03-23 14:42:34 +010057 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3)
Michal Simekb6039aa2019-09-27 14:08:41 +020058 return ipi_req(req, req_len, res, res_maxlen);
59
Michal Simek40361952019-10-04 15:35:45 +020060 return xilinx_pm_request(req[0], 0, 0, 0, 0, res);
Michal Simekb6039aa2019-09-27 14:08:41 +020061}
62
Ibai Erkiaga490f6272019-09-27 11:37:00 +010063unsigned int zynqmp_firmware_version(void)
64{
65 int ret;
66 u32 ret_payload[PAYLOAD_ARG_CNT];
67 static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID;
68
69 /*
70 * Get PMU version only once and later
71 * just return stored values instead of
72 * asking PMUFW again.
73 **/
74 if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) {
Michal Simekb6039aa2019-09-27 14:08:41 +020075 const u32 request[] = { PM_GET_API_VERSION };
Ibai Erkiaga490f6272019-09-27 11:37:00 +010076
Michal Simekb6039aa2019-09-27 14:08:41 +020077 ret = send_req(request, ARRAY_SIZE(request), ret_payload, 2);
Ibai Erkiaga490f6272019-09-27 11:37:00 +010078 if (ret)
79 panic("PMUFW is not found - Please load it!\n");
80
81 pm_api_version = ret_payload[1];
82 if (pm_api_version < ZYNQMP_PM_VERSION)
83 panic("PMUFW version error. Expected: v%d.%d\n",
84 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR);
85 }
86
87 return pm_api_version;
88};
89
Michal Simeka3e552b2019-09-27 14:20:00 +020090/**
91 * Send a configuration object to the PMU firmware.
92 *
93 * @cfg_obj: Pointer to the configuration object
94 * @size: Size of @cfg_obj in bytes
95 */
96void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size)
97{
98 const u32 request[] = {
99 PM_SET_CONFIGURATION,
100 (u32)((u64)cfg_obj)
101 };
102 u32 response;
103 int err;
104
105 printf("Loading new PMUFW cfg obj (%ld bytes)\n", size);
106
107 err = send_req(request, ARRAY_SIZE(request), &response, 1);
108 if (err)
109 panic("Cannot load PMUFW configuration object (%d)\n", err);
110 if (response != 0)
111 panic("PMUFW returned 0x%08x status!\n", response);
112}
113
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200114static int zynqmp_power_probe(struct udevice *dev)
115{
Michal Simek44dccd52019-10-10 11:26:16 +0200116 int ret;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200117
118 debug("%s, (dev=%p)\n", __func__, dev);
119
120 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
121 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200122 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200123 return ret;
124 }
125
126 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100127 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200128 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100129 return ret;
130 }
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200131
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100132 ret = zynqmp_firmware_version();
133 printf("PMUFW:\tv%d.%d\n",
134 ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT,
135 ret & ZYNQMP_PM_VERSION_MINOR_MASK);
136
137 return 0;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200138};
139
140static const struct udevice_id zynqmp_power_ids[] = {
141 { .compatible = "xlnx,zynqmp-power" },
142 { }
143};
144
145U_BOOT_DRIVER(zynqmp_power) = {
146 .name = "zynqmp_power",
147 .id = UCLASS_FIRMWARE,
148 .of_match = zynqmp_power_ids,
149 .probe = zynqmp_power_probe,
150};
151#endif
152
Michal Simek40361952019-10-04 15:35:45 +0200153int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
154 u32 arg3, u32 *ret_payload)
Michal Simek866225f2019-10-04 15:45:29 +0200155{
156 /*
157 * Added SIP service call Function Identifier
158 * Make sure to stay in x0 register
159 */
160 struct pt_regs regs;
161
Michal Simek9bed8a62019-10-10 11:09:15 +0200162 if (current_el() == 3) {
163 printf("%s: Can't call SMC from EL3 context\n", __func__);
164 return -EPERM;
165 }
Michal Simek866225f2019-10-04 15:45:29 +0200166
Michal Simek40361952019-10-04 15:35:45 +0200167 regs.regs[0] = PM_SIP_SVC | api_id;
Michal Simek866225f2019-10-04 15:45:29 +0200168 regs.regs[1] = ((u64)arg1 << 32) | arg0;
169 regs.regs[2] = ((u64)arg3 << 32) | arg2;
170
171 smc_call(&regs);
172
173 if (ret_payload) {
174 ret_payload[0] = (u32)regs.regs[0];
175 ret_payload[1] = upper_32_bits(regs.regs[0]);
176 ret_payload[2] = (u32)regs.regs[1];
177 ret_payload[3] = upper_32_bits(regs.regs[1]);
178 ret_payload[4] = (u32)regs.regs[2];
179 }
180
181 return regs.regs[0];
182}
183
Rajan Vaja14723ed2019-02-15 04:45:32 -0800184static const struct udevice_id zynqmp_firmware_ids[] = {
185 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530186 { .compatible = "xlnx,versal-firmware"},
Rajan Vaja14723ed2019-02-15 04:45:32 -0800187 { }
188};
189
190U_BOOT_DRIVER(zynqmp_firmware) = {
191 .id = UCLASS_FIRMWARE,
192 .name = "zynqmp-firmware",
Rajan Vaja14723ed2019-02-15 04:45:32 -0800193 .of_match = zynqmp_firmware_ids,
194};