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