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