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