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> |
Michal Simek | 0fd9f36 | 2022-02-07 10:27:37 +0100 | [diff] [blame] | 11 | #include <dm/lists.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 13 | #include <zynqmp_firmware.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 14 | #include <asm/cache.h> |
Simon Glass | 6b9f010 | 2020-05-10 11:40:06 -0600 | [diff] [blame] | 15 | #include <asm/ptrace.h> |
Rajan Vaja | 833be32 | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 16 | |
Ibai Erkiaga | d47ed6f | 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 | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 21 | #define PMUFW_PAYLOAD_ARG_CNT 8 |
| 22 | |
Michal Simek | 5f42112 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 23 | #define XST_PM_NO_ACCESS 2002L |
Michal Simek | 0ce829f | 2022-01-14 13:25:37 +0100 | [diff] [blame] | 24 | #define XST_PM_ALREADY_CONFIGURED 2009L |
Michal Simek | 5f42112 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 25 | |
Ibai Erkiaga | d47ed6f | 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 | |
Michal Simek | 783e74c | 2022-01-14 13:25:35 +0100 | [diff] [blame] | 31 | #define NODE_ID_LOCATION 5 |
| 32 | |
| 33 | static unsigned int xpm_configobject[] = { |
| 34 | /**********************************************************************/ |
| 35 | /* HEADER */ |
| 36 | 2, /* Number of remaining words in the header */ |
| 37 | 1, /* Number of sections included in config object */ |
| 38 | PM_CONFIG_OBJECT_TYPE_OVERLAY, /* Type of Config object as overlay */ |
| 39 | /**********************************************************************/ |
| 40 | /* SLAVE SECTION */ |
| 41 | |
| 42 | PM_CONFIG_SLAVE_SECTION_ID, /* Section ID */ |
| 43 | 1, /* Number of slaves */ |
| 44 | |
| 45 | 0, /* Node ID which will be changed below */ |
| 46 | PM_SLAVE_FLAG_IS_SHAREABLE, |
| 47 | PM_CONFIG_IPI_PSU_CORTEXA53_0_MASK | |
| 48 | PM_CONFIG_IPI_PSU_CORTEXR5_0_MASK | |
| 49 | PM_CONFIG_IPI_PSU_CORTEXR5_1_MASK, /* IPI Mask */ |
| 50 | }; |
| 51 | |
Michal Simek | 07168c6 | 2022-01-14 13:25:38 +0100 | [diff] [blame] | 52 | static unsigned int xpm_configobject_close[] = { |
| 53 | /**********************************************************************/ |
| 54 | /* HEADER */ |
| 55 | 2, /* Number of remaining words in the header */ |
| 56 | 1, /* Number of sections included in config object */ |
| 57 | PM_CONFIG_OBJECT_TYPE_OVERLAY, /* Type of Config object as overlay */ |
| 58 | /**********************************************************************/ |
| 59 | /* SET CONFIG SECTION */ |
| 60 | PM_CONFIG_SET_CONFIG_SECTION_ID, |
| 61 | 0U, /* Loading permission to Overlay config object */ |
| 62 | }; |
| 63 | |
| 64 | int zynqmp_pmufw_config_close(void) |
| 65 | { |
| 66 | zynqmp_pmufw_load_config_object(xpm_configobject_close, |
| 67 | sizeof(xpm_configobject_close)); |
| 68 | return 0; |
| 69 | } |
| 70 | |
Michal Simek | 783e74c | 2022-01-14 13:25:35 +0100 | [diff] [blame] | 71 | int zynqmp_pmufw_node(u32 id) |
| 72 | { |
| 73 | /* Record power domain id */ |
| 74 | xpm_configobject[NODE_ID_LOCATION] = id; |
| 75 | |
| 76 | zynqmp_pmufw_load_config_object(xpm_configobject, |
| 77 | sizeof(xpm_configobject)); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 82 | static int ipi_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen) |
| 83 | { |
| 84 | struct zynqmp_ipi_msg msg; |
| 85 | int ret; |
Michal Simek | ae655fb | 2021-10-15 16:57:39 +0200 | [diff] [blame] | 86 | u32 buffer[PAYLOAD_ARG_CNT]; |
| 87 | |
| 88 | if (!res) |
| 89 | res = buffer; |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 90 | |
| 91 | if (req_len > PMUFW_PAYLOAD_ARG_CNT || |
| 92 | res_maxlen > PMUFW_PAYLOAD_ARG_CNT) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | if (!(zynqmp_power.tx_chan.dev) || !(&zynqmp_power.rx_chan.dev)) |
| 96 | return -EINVAL; |
| 97 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 98 | 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] | 99 | msg.buf = (u32 *)req; |
| 100 | msg.len = req_len; |
| 101 | ret = mbox_send(&zynqmp_power.tx_chan, &msg); |
| 102 | if (ret) { |
| 103 | debug("%s: Sending message failed\n", __func__); |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | msg.buf = res; |
| 108 | msg.len = res_maxlen; |
| 109 | ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100); |
| 110 | if (ret) |
| 111 | debug("%s: Receiving message failed\n", __func__); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | unsigned int zynqmp_firmware_version(void) |
| 117 | { |
| 118 | int ret; |
| 119 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
| 120 | static u32 pm_api_version = ZYNQMP_PM_VERSION_INVALID; |
| 121 | |
| 122 | /* |
| 123 | * Get PMU version only once and later |
| 124 | * just return stored values instead of |
| 125 | * asking PMUFW again. |
| 126 | **/ |
| 127 | if (pm_api_version == ZYNQMP_PM_VERSION_INVALID) { |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 128 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 129 | ret = xilinx_pm_request(PM_GET_API_VERSION, 0, 0, 0, 0, |
| 130 | ret_payload); |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 131 | if (ret) |
| 132 | panic("PMUFW is not found - Please load it!\n"); |
| 133 | |
| 134 | pm_api_version = ret_payload[1]; |
| 135 | if (pm_api_version < ZYNQMP_PM_VERSION) |
| 136 | panic("PMUFW version error. Expected: v%d.%d\n", |
| 137 | ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR); |
| 138 | } |
| 139 | |
| 140 | return pm_api_version; |
| 141 | }; |
| 142 | |
Michal Simek | a95f0d9 | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 143 | /** |
| 144 | * Send a configuration object to the PMU firmware. |
| 145 | * |
| 146 | * @cfg_obj: Pointer to the configuration object |
| 147 | * @size: Size of @cfg_obj in bytes |
| 148 | */ |
| 149 | void zynqmp_pmufw_load_config_object(const void *cfg_obj, size_t size) |
| 150 | { |
Michal Simek | a95f0d9 | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 151 | int err; |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 152 | u32 ret_payload[PAYLOAD_ARG_CNT]; |
Michal Simek | a95f0d9 | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 153 | |
Michal Simek | 957b248 | 2022-01-14 13:25:36 +0100 | [diff] [blame] | 154 | if (IS_ENABLED(CONFIG_SPL_BUILD)) |
| 155 | printf("Loading new PMUFW cfg obj (%ld bytes)\n", size); |
Michal Simek | a95f0d9 | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 156 | |
Michal Simek | 58ba267 | 2021-11-18 13:00:02 +0100 | [diff] [blame] | 157 | flush_dcache_range((ulong)cfg_obj, (ulong)(cfg_obj + size)); |
| 158 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 159 | err = xilinx_pm_request(PM_SET_CONFIGURATION, (u32)(u64)cfg_obj, 0, 0, |
| 160 | 0, ret_payload); |
Michal Simek | 5f42112 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 161 | if (err == XST_PM_NO_ACCESS) { |
| 162 | printf("PMUFW no permission to change config object\n"); |
| 163 | return; |
| 164 | } |
| 165 | |
Michal Simek | 0ce829f | 2022-01-14 13:25:37 +0100 | [diff] [blame] | 166 | if (err == XST_PM_ALREADY_CONFIGURED) { |
| 167 | debug("PMUFW Node is already configured\n"); |
| 168 | return; |
| 169 | } |
| 170 | |
Michal Simek | a95f0d9 | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 171 | if (err) |
Michal Simek | 5f42112 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 172 | printf("Cannot load PMUFW configuration object (%d)\n", err); |
| 173 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 174 | if (ret_payload[0]) |
| 175 | printf("PMUFW returned 0x%08x status!\n", ret_payload[0]); |
Michal Simek | 5f42112 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 176 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 177 | if ((err || ret_payload[0]) && IS_ENABLED(CONFIG_SPL_BUILD)) |
Michal Simek | 5f42112 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 178 | panic("PMUFW config object loading failed in EL3\n"); |
Michal Simek | a95f0d9 | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Ibai Erkiaga | d47ed6f | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 181 | static int zynqmp_power_probe(struct udevice *dev) |
| 182 | { |
Michal Simek | 8ece072 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 183 | int ret; |
Ibai Erkiaga | d47ed6f | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 184 | |
| 185 | debug("%s, (dev=%p)\n", __func__, dev); |
| 186 | |
| 187 | ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan); |
| 188 | if (ret) { |
Michal Simek | 8ece072 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 189 | debug("%s: Cannot find tx mailbox\n", __func__); |
Ibai Erkiaga | d47ed6f | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan); |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 194 | if (ret) { |
Michal Simek | 8ece072 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 195 | debug("%s: Cannot find rx mailbox\n", __func__); |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 196 | return ret; |
| 197 | } |
Ibai Erkiaga | d47ed6f | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 198 | |
Ibai Erkiaga | 99b2c28 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 199 | ret = zynqmp_firmware_version(); |
| 200 | printf("PMUFW:\tv%d.%d\n", |
| 201 | ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT, |
| 202 | ret & ZYNQMP_PM_VERSION_MINOR_MASK); |
| 203 | |
| 204 | return 0; |
Ibai Erkiaga | d47ed6f | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | static const struct udevice_id zynqmp_power_ids[] = { |
| 208 | { .compatible = "xlnx,zynqmp-power" }, |
| 209 | { } |
| 210 | }; |
| 211 | |
| 212 | U_BOOT_DRIVER(zynqmp_power) = { |
| 213 | .name = "zynqmp_power", |
| 214 | .id = UCLASS_FIRMWARE, |
| 215 | .of_match = zynqmp_power_ids, |
| 216 | .probe = zynqmp_power_probe, |
| 217 | }; |
| 218 | #endif |
| 219 | |
Michal Simek | 4c3de37 | 2019-10-04 15:35:45 +0200 | [diff] [blame] | 220 | int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2, |
| 221 | u32 arg3, u32 *ret_payload) |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 222 | { |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 223 | 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] | 224 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 225 | if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) { |
| 226 | #if defined(CONFIG_ZYNQMP_IPI) |
| 227 | /* |
| 228 | * Use fixed payload and arg size as the EL2 call. The firmware |
| 229 | * is capable to handle PMUFW_PAYLOAD_ARG_CNT bytes but the |
| 230 | * firmware API is limited by the SMC call size |
| 231 | */ |
| 232 | u32 regs[] = {api_id, arg0, arg1, arg2, arg3}; |
Michal Simek | 577385a | 2021-10-15 16:57:38 +0200 | [diff] [blame] | 233 | int ret; |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 234 | |
Michal Simek | f33d95b | 2020-10-05 15:23:28 +0200 | [diff] [blame] | 235 | if (api_id == PM_FPGA_LOAD) { |
| 236 | /* Swap addr_hi/low because of incompatibility */ |
| 237 | u32 temp = regs[1]; |
| 238 | |
| 239 | regs[1] = regs[2]; |
| 240 | regs[2] = temp; |
| 241 | } |
| 242 | |
Michal Simek | 577385a | 2021-10-15 16:57:38 +0200 | [diff] [blame] | 243 | ret = ipi_req(regs, PAYLOAD_ARG_CNT, ret_payload, |
| 244 | PAYLOAD_ARG_CNT); |
| 245 | if (ret) |
| 246 | return ret; |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 247 | #else |
Michal Simek | 4ca6e95 | 2019-10-10 11:09:15 +0200 | [diff] [blame] | 248 | return -EPERM; |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 249 | #endif |
| 250 | } else { |
| 251 | /* |
| 252 | * Added SIP service call Function Identifier |
| 253 | * Make sure to stay in x0 register |
| 254 | */ |
| 255 | struct pt_regs regs; |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 256 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 257 | regs.regs[0] = PM_SIP_SVC | api_id; |
| 258 | regs.regs[1] = ((u64)arg1 << 32) | arg0; |
| 259 | regs.regs[2] = ((u64)arg3 << 32) | arg2; |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 260 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 261 | smc_call(®s); |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 262 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 263 | if (ret_payload) { |
| 264 | ret_payload[0] = (u32)regs.regs[0]; |
| 265 | ret_payload[1] = upper_32_bits(regs.regs[0]); |
| 266 | ret_payload[2] = (u32)regs.regs[1]; |
| 267 | ret_payload[3] = upper_32_bits(regs.regs[1]); |
| 268 | ret_payload[4] = (u32)regs.regs[2]; |
| 269 | } |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 270 | |
Ibai Erkiaga | 22712ea | 2020-08-04 23:17:27 +0100 | [diff] [blame] | 271 | } |
| 272 | return (ret_payload) ? ret_payload[0] : 0; |
Michal Simek | 81efd2a | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Rajan Vaja | 833be32 | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 275 | static const struct udevice_id zynqmp_firmware_ids[] = { |
| 276 | { .compatible = "xlnx,zynqmp-firmware" }, |
Siva Durga Prasad Paladugu | f7a7120 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 277 | { .compatible = "xlnx,versal-firmware"}, |
Rajan Vaja | 833be32 | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 278 | { } |
| 279 | }; |
| 280 | |
Michal Simek | 0fd9f36 | 2022-02-07 10:27:37 +0100 | [diff] [blame] | 281 | static int zynqmp_firmware_bind(struct udevice *dev) |
| 282 | { |
| 283 | int ret; |
| 284 | struct udevice *child; |
| 285 | |
| 286 | if (IS_ENABLED(CONFIG_ZYNQMP_POWER_DOMAIN)) { |
| 287 | ret = device_bind_driver_to_node(dev, "zynqmp_power_domain", |
| 288 | "zynqmp_power_domain", |
| 289 | dev_ofnode(dev), &child); |
| 290 | if (ret) { |
| 291 | printf("zynqmp power domain driver is not bound: %d\n", ret); |
| 292 | return ret; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return dm_scan_fdt_dev(dev); |
| 297 | } |
| 298 | |
Rajan Vaja | 833be32 | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 299 | U_BOOT_DRIVER(zynqmp_firmware) = { |
| 300 | .id = UCLASS_FIRMWARE, |
Michal Simek | 3309308 | 2020-01-07 08:50:34 +0100 | [diff] [blame] | 301 | .name = "zynqmp_firmware", |
Rajan Vaja | 833be32 | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 302 | .of_match = zynqmp_firmware_ids, |
Michal Simek | 0fd9f36 | 2022-02-07 10:27:37 +0100 | [diff] [blame] | 303 | .bind = zynqmp_firmware_bind, |
Rajan Vaja | 833be32 | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 304 | }; |