blob: 66edc1693016794642a2ca04414ac5a1d71df66d [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;
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
Michal Simekb6039aa2019-09-27 14:08:41 +020057static int send_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen)
58{
Michal Simek73319ee2020-03-23 14:42:34 +010059 if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3)
Michal Simekb6039aa2019-09-27 14:08:41 +020060 return ipi_req(req, req_len, res, res_maxlen);
61
Michal Simek40361952019-10-04 15:35:45 +020062 return xilinx_pm_request(req[0], 0, 0, 0, 0, res);
Michal Simekb6039aa2019-09-27 14:08:41 +020063}
64
Ibai Erkiaga490f6272019-09-27 11:37:00 +010065unsigned 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) {
Michal Simekb6039aa2019-09-27 14:08:41 +020077 const u32 request[] = { PM_GET_API_VERSION };
Ibai Erkiaga490f6272019-09-27 11:37:00 +010078
Michal Simekb6039aa2019-09-27 14:08:41 +020079 ret = send_req(request, ARRAY_SIZE(request), ret_payload, 2);
Ibai Erkiaga490f6272019-09-27 11:37:00 +010080 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
Michal Simeka3e552b2019-09-27 14:20:00 +020092/**
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 */
98void 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 };
Michal Simek4c86e082020-04-27 11:51:40 +0200104 u32 response = 0;
Michal Simeka3e552b2019-09-27 14:20:00 +0200105 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);
Michal Simek4c86e082020-04-27 11:51:40 +0200110 if (err == XST_PM_NO_ACCESS) {
111 printf("PMUFW no permission to change config object\n");
112 return;
113 }
114
Michal Simeka3e552b2019-09-27 14:20:00 +0200115 if (err)
Michal Simek4c86e082020-04-27 11:51:40 +0200116 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");
Michal Simeka3e552b2019-09-27 14:20:00 +0200123}
124
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200125static int zynqmp_power_probe(struct udevice *dev)
126{
Michal Simek44dccd52019-10-10 11:26:16 +0200127 int ret;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200128
129 debug("%s, (dev=%p)\n", __func__, dev);
130
131 ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan);
132 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200133 debug("%s: Cannot find tx mailbox\n", __func__);
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200134 return ret;
135 }
136
137 ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100138 if (ret) {
Michal Simek44dccd52019-10-10 11:26:16 +0200139 debug("%s: Cannot find rx mailbox\n", __func__);
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100140 return ret;
141 }
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200142
Ibai Erkiaga490f6272019-09-27 11:37:00 +0100143 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;
Ibai Erkiaga1327d162019-09-27 12:51:41 +0200149};
150
151static const struct udevice_id zynqmp_power_ids[] = {
152 { .compatible = "xlnx,zynqmp-power" },
153 { }
154};
155
156U_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
Michal Simek40361952019-10-04 15:35:45 +0200164int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2,
165 u32 arg3, u32 *ret_payload)
Michal Simek866225f2019-10-04 15:45:29 +0200166{
167 /*
168 * Added SIP service call Function Identifier
169 * Make sure to stay in x0 register
170 */
171 struct pt_regs regs;
172
Michal Simek9bed8a62019-10-10 11:09:15 +0200173 if (current_el() == 3) {
174 printf("%s: Can't call SMC from EL3 context\n", __func__);
175 return -EPERM;
176 }
Michal Simek866225f2019-10-04 15:45:29 +0200177
Michal Simek40361952019-10-04 15:35:45 +0200178 regs.regs[0] = PM_SIP_SVC | api_id;
Michal Simek866225f2019-10-04 15:45:29 +0200179 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
Rajan Vaja14723ed2019-02-15 04:45:32 -0800195static const struct udevice_id zynqmp_firmware_ids[] = {
196 { .compatible = "xlnx,zynqmp-firmware" },
Siva Durga Prasad Paladugu95105082019-06-23 12:24:57 +0530197 { .compatible = "xlnx,versal-firmware"},
Rajan Vaja14723ed2019-02-15 04:45:32 -0800198 { }
199};
200
201U_BOOT_DRIVER(zynqmp_firmware) = {
202 .id = UCLASS_FIRMWARE,
203 .name = "zynqmp-firmware",
Rajan Vaja14723ed2019-02-15 04:45:32 -0800204 .of_match = zynqmp_firmware_ids,
205};