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> |
Michal Simek | 380bd08 | 2021-11-18 13:00:02 +0100 | [diff] [blame] | 9 | #include <cpu_func.h> |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 10 | #include <dm.h> |
Michal Simek | e0283cb | 2022-02-07 10:27:37 +0100 | [diff] [blame^] | 11 | #include <dm/lists.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Michal Simek | 866225f | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 13 | #include <zynqmp_firmware.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Simon Glass | 25a5818 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 15 | #include <asm/ptrace.h> |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 16 | |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 17 | #if defined(CONFIG_ZYNQMP_IPI) |
| 18 | #include <mailbox.h> |
| 19 | #include <asm/arch/sys_proto.h> |
| 20 | |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 21 | #define PMUFW_PAYLOAD_ARG_CNT 8 |
| 22 | |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 23 | #define XST_PM_NO_ACCESS 2002L |
Michal Simek | 11c0771 | 2022-01-14 13:25:37 +0100 | [diff] [blame] | 24 | #define XST_PM_ALREADY_CONFIGURED 2009L |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 25 | |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 26 | struct zynqmp_power { |
| 27 | struct mbox_chan tx_chan; |
| 28 | struct mbox_chan rx_chan; |
| 29 | } zynqmp_power; |
| 30 | |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 31 | static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen) |
| 32 | { |
| 33 | struct zynqmp_ipi_msg msg; |
| 34 | int ret; |
Michal Simek | 53f5d16 | 2021-10-15 16:57:39 +0200 | [diff] [blame] | 35 | u32 buffer[PAYLOAD_ARG_CNT]; |
| 36 | |
| 37 | if (!res) |
| 38 | res = buffer; |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 39 | |
| 40 | if (req_len > PMUFW_PAYLOAD_ARG_CNT || |
| 41 | res_maxlen > PMUFW_PAYLOAD_ARG_CNT) |
| 42 | return -EINVAL; |
| 43 | |
| 44 | if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev)) |
| 45 | return -EINVAL; |
| 46 | |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 47 | 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] | 48 | msg.buf = (u32 *)req; |
| 49 | msg.len = req_len; |
| 50 | ret = mbox_send(&zynqmp_power.tx_chan, &msg); |
| 51 | if (ret) { |
| 52 | debug("%s: Sending message failed\n", __func__); |
| 53 | return ret; |
| 54 | } |
| 55 | |
| 56 | msg.buf = res; |
| 57 | msg.len = res_maxlen; |
| 58 | ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100); |
| 59 | if (ret) |
| 60 | debug("%s: Receiving message failed\n", __func__); |
| 61 | |
| 62 | return ret; |
| 63 | } |
| 64 | |
| 65 | unsigned 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) { |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 77 | |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 78 | ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0, |
| 79 | ret_payload); |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 80 | 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 Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 92 | /** |
| 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 | */ |
| 98 | void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size) |
| 99 | { |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 100 | int err; |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 101 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 102 | |
Michal Simek | 12662e7 | 2022-01-14 13:25:36 +0100 | [diff] [blame] | 103 | if (IS_ENABLED(CONFIG_SPL_BUILD)) |
| 104 | printf("Loading new PMUFW cfg obj (%ld bytes)\n", size); |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 105 | |
Michal Simek | 380bd08 | 2021-11-18 13:00:02 +0100 | [diff] [blame] | 106 | flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size)); |
| 107 | |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 108 | err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0, |
| 109 | 0, ret_payload); |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 110 | if (err == XST_PM_NO_ACCESS) { |
| 111 | printf("PMUFW no permission to change config object\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
Michal Simek | 11c0771 | 2022-01-14 13:25:37 +0100 | [diff] [blame] | 115 | if (err == XST_PM_ALREADY_CONFIGURED) { |
| 116 | debug("PMUFW Node is already configured\n"); |
| 117 | return; |
| 118 | } |
| 119 | |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 120 | if (err) |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 121 | printf("Cannot load PMUFW configuration object (%d)\n", err); |
| 122 | |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 123 | if (ret_payload[0]) |
| 124 | printf("PMUFW returned 0x%08x status!\n", ret_payload[0]); |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 125 | |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 126 | if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD)) |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 127 | panic("PMUFW config object loading failed in EL3\n"); |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 130 | static int zynqmp_power_probe(struct udevice *dev) |
| 131 | { |
Michal Simek | 44dccd5 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 132 | int ret; |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 133 | |
| 134 | debug("%s, (dev=%p)\n", __func__, dev); |
| 135 | |
| 136 | ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan); |
| 137 | if (ret) { |
Michal Simek | 44dccd5 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 138 | debug("%s: Cannot find tx mailbox\n", __func__); |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 139 | return ret; |
| 140 | } |
| 141 | |
| 142 | ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan); |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 143 | if (ret) { |
Michal Simek | 44dccd5 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 144 | debug("%s: Cannot find rx mailbox\n", __func__); |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 145 | return ret; |
| 146 | } |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 147 | |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 148 | ret = zynqmp_firmware_version(); |
| 149 | printf("PMUFW:\tv%d.%d\n", |
| 150 | ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT, |
| 151 | ret & ZYNQMP_PM_VERSION_MINOR_MASK); |
| 152 | |
| 153 | return 0; |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | static const struct udevice_id zynqmp_power_ids[] = { |
| 157 | { .compatible = "xlnx,zynqmp-power" }, |
| 158 | { } |
| 159 | }; |
| 160 | |
| 161 | U_BOOT_DRIVER(zynqmp_power) = { |
| 162 | .name = "zynqmp_power", |
| 163 | .id = UCLASS_FIRMWARE, |
| 164 | .of_match = zynqmp_power_ids, |
| 165 | .probe = zynqmp_power_probe, |
| 166 | }; |
| 167 | #endif |
| 168 | |
Michal Simek | 4036195 | 2019-10-04 15:35:45 +0200 | [diff] [blame] | 169 | int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2, |
| 170 | u32 arg3, u32 *ret_payload) |
Michal Simek | 866225f | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 171 | { |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 172 | 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] | 173 | |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 174 | if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) { |
| 175 | #if defined(CONFIG_ZYNQMP_IPI) |
| 176 | /* |
| 177 | * Use fixed payload and arg size as the EL2 call. The firmware |
| 178 | * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the |
| 179 | * firmware API is limited by the SMC call size |
| 180 | */ |
| 181 | u32 regs[] = {api_id, arg0, arg1, arg2, arg3}; |
Michal Simek | b05cc38 | 2021-10-15 16:57:38 +0200 | [diff] [blame] | 182 | int ret; |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 183 | |
Michal Simek | 5690128 | 2020-10-05 15:23:28 +0200 | [diff] [blame] | 184 | if (api_id == PM_FPGA_LOAD) { |
| 185 | /* Swap addr_hi/low because of incompatibility */ |
| 186 | u32 temp = regs[1]; |
| 187 | |
| 188 | regs[1] = regs[2]; |
| 189 | regs[2] = temp; |
| 190 | } |
| 191 | |
Michal Simek | b05cc38 | 2021-10-15 16:57:38 +0200 | [diff] [blame] | 192 | ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload, |
| 193 | PAYLOAD_ARG_CNT); |
| 194 | if (ret) |
| 195 | return ret; |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 196 | #else |
Michal Simek | 9bed8a6 | 2019-10-10 11:09:15 +0200 | [diff] [blame] | 197 | return -EPERM; |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 198 | #endif |
| 199 | } else { |
| 200 | /* |
| 201 | * Added SIP service call Function Identifier |
| 202 | * Make sure to stay in x0 register |
| 203 | */ |
| 204 | struct pt_regs regs; |
| 205 | |
| 206 | regs.regs[0] = PM_SIP_SVC | api_id; |
| 207 | regs.regs[1] = ((u64)arg1 << 32) | arg0; |
| 208 | regs.regs[2] = ((u64)arg3 << 32) | arg2; |
| 209 | |
| 210 | smc_call(®s); |
| 211 | |
| 212 | if (ret_payload) { |
| 213 | ret_payload[0] = (u32)regs.regs[0]; |
| 214 | ret_payload[1] = upper_32_bits(regs.regs[0]); |
| 215 | ret_payload[2] = (u32)regs.regs[1]; |
| 216 | ret_payload[3] = upper_32_bits(regs.regs[1]); |
| 217 | ret_payload[4] = (u32)regs.regs[2]; |
| 218 | } |
| 219 | |
Michal Simek | 9bed8a6 | 2019-10-10 11:09:15 +0200 | [diff] [blame] | 220 | } |
Ibai Erkiaga | 2eabb6b | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 221 | return (ret_payload) ? ret_payload[0] : 0; |
Michal Simek | 866225f | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 222 | } |
| 223 | |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 224 | static const struct udevice_id zynqmp_firmware_ids[] = { |
| 225 | { .compatible = "xlnx,zynqmp-firmware" }, |
Siva Durga Prasad Paladugu | 9510508 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 226 | { .compatible = "xlnx,versal-firmware"}, |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 227 | { } |
| 228 | }; |
| 229 | |
Michal Simek | e0283cb | 2022-02-07 10:27:37 +0100 | [diff] [blame^] | 230 | static int zynqmp_firmware_bind(struct udevice *dev) |
| 231 | { |
| 232 | int ret; |
| 233 | struct udevice *child; |
| 234 | |
| 235 | if (IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) { |
| 236 | ret = device_bind_driver_to_node(dev, "zynqmp_power_domain", |
| 237 | "zynqmp_power_domain", |
| 238 | dev_ofnode(dev), &child); |
| 239 | if (ret) { |
| 240 | printf("zynqmp power domain driver is not bound: %d\n", ret); |
| 241 | return ret; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return dm_scan_fdt_dev(dev); |
| 246 | } |
| 247 | |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 248 | U_BOOT_DRIVER(zynqmp_firmware) = { |
| 249 | .id = UCLASS_FIRMWARE, |
Michal Simek | 6c0e59f | 2020-01-07 08:50:34 +0100 | [diff] [blame] | 250 | .name = "zynqmp_firmware", |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 251 | .of_match = zynqmp_firmware_ids, |
Michal Simek | e0283cb | 2022-02-07 10:27:37 +0100 | [diff] [blame^] | 252 | .bind = zynqmp_firmware_bind, |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 253 | }; |