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 | |
| 40 | msg.buf = (u32 *)req; |
| 41 | msg.len = req_len; |
| 42 | ret = mbox_send(&zynqmp_power.tx_chan, &msg); |
| 43 | if (ret) { |
| 44 | debug("%s: Sending message failed\n", __func__); |
| 45 | return ret; |
| 46 | } |
| 47 | |
| 48 | msg.buf = res; |
| 49 | msg.len = res_maxlen; |
| 50 | ret = mbox_recv(&zynqmp_power.rx_chan, &msg, 100); |
| 51 | if (ret) |
| 52 | debug("%s: Receiving message failed\n", __func__); |
| 53 | |
| 54 | return ret; |
| 55 | } |
| 56 | |
Michal Simek | b6039aa | 2019-09-27 14:08:41 +0200 | [diff] [blame] | 57 | static int send_req(const u32 *req, size_t req_len, u32 *res, size_t res_maxlen) |
| 58 | { |
Michal Simek | 73319ee | 2020-03-23 14:42:34 +0100 | [diff] [blame] | 59 | if (IS_ENABLED(CONFIG_SPL_BUILD) || current_el() == 3) |
Michal Simek | b6039aa | 2019-09-27 14:08:41 +0200 | [diff] [blame] | 60 | return ipi_req(req, req_len, res, res_maxlen); |
| 61 | |
Michal Simek | 4036195 | 2019-10-04 15:35:45 +0200 | [diff] [blame] | 62 | return xilinx_pm_request(req[0], 0, 0, 0, 0, res); |
Michal Simek | b6039aa | 2019-09-27 14:08:41 +0200 | [diff] [blame] | 63 | } |
| 64 | |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 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) { |
Michal Simek | b6039aa | 2019-09-27 14:08:41 +0200 | [diff] [blame] | 77 | const u32 request[] = { PM_GET_API_VERSION }; |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 78 | |
Michal Simek | b6039aa | 2019-09-27 14:08:41 +0200 | [diff] [blame] | 79 | ret = send_req(request, ARRAY_SIZE(request), ret_payload, 2); |
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 | { |
| 100 | const u32 request[] = { |
| 101 | PM_SET_CONFIGURATION, |
| 102 | (u32)((u64)cfg_obj) |
| 103 | }; |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 104 | u32 response = 0; |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 105 | int err; |
| 106 | |
| 107 | printf("Loading new PMUFW cfg obj (%ld bytes)\n", size); |
| 108 | |
| 109 | err = send_req(request, ARRAY_SIZE(request), &response, 1); |
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 | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 115 | if (err) |
Michal Simek | 4c86e08 | 2020-04-27 11:51:40 +0200 | [diff] [blame] | 116 | printf("Cannot load PMUFW configuration object (%d)\n", err); |
| 117 | |
| 118 | if (response) |
| 119 | printf("PMUFW returned 0x%08x status!\n", response); |
| 120 | |
| 121 | if ((err || response) && IS_ENABLED(CONFIG_SPL_BUILD)) |
| 122 | panic("PMUFW config object loading failed in EL3\n"); |
Michal Simek | a3e552b | 2019-09-27 14:20:00 +0200 | [diff] [blame] | 123 | } |
| 124 | |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 125 | static int zynqmp_power_probe(struct udevice *dev) |
| 126 | { |
Michal Simek | 44dccd5 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 127 | int ret; |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 128 | |
| 129 | debug("%s, (dev=%p)\n", __func__, dev); |
| 130 | |
| 131 | ret = mbox_get_by_name(dev, "tx", &zynqmp_power.tx_chan); |
| 132 | if (ret) { |
Michal Simek | 44dccd5 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 133 | debug("%s: Cannot find tx mailbox\n", __func__); |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 134 | return ret; |
| 135 | } |
| 136 | |
| 137 | ret = mbox_get_by_name(dev, "rx", &zynqmp_power.rx_chan); |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 138 | if (ret) { |
Michal Simek | 44dccd5 | 2019-10-10 11:26:16 +0200 | [diff] [blame] | 139 | debug("%s: Cannot find rx mailbox\n", __func__); |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 140 | return ret; |
| 141 | } |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 142 | |
Ibai Erkiaga | 490f627 | 2019-09-27 11:37:00 +0100 | [diff] [blame] | 143 | ret = zynqmp_firmware_version(); |
| 144 | printf("PMUFW:\tv%d.%d\n", |
| 145 | ret >> ZYNQMP_PM_VERSION_MAJOR_SHIFT, |
| 146 | ret & ZYNQMP_PM_VERSION_MINOR_MASK); |
| 147 | |
| 148 | return 0; |
Ibai Erkiaga | 1327d16 | 2019-09-27 12:51:41 +0200 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | static const struct udevice_id zynqmp_power_ids[] = { |
| 152 | { .compatible = "xlnx,zynqmp-power" }, |
| 153 | { } |
| 154 | }; |
| 155 | |
| 156 | U_BOOT_DRIVER(zynqmp_power) = { |
| 157 | .name = "zynqmp_power", |
| 158 | .id = UCLASS_FIRMWARE, |
| 159 | .of_match = zynqmp_power_ids, |
| 160 | .probe = zynqmp_power_probe, |
| 161 | }; |
| 162 | #endif |
| 163 | |
Michal Simek | 4036195 | 2019-10-04 15:35:45 +0200 | [diff] [blame] | 164 | int __maybe_unused xilinx_pm_request(u32 api_id, u32 arg0, u32 arg1, u32 arg2, |
| 165 | u32 arg3, u32 *ret_payload) |
Michal Simek | 866225f | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 166 | { |
| 167 | /* |
| 168 | * Added SIP service call Function Identifier |
| 169 | * Make sure to stay in x0 register |
| 170 | */ |
| 171 | struct pt_regs regs; |
| 172 | |
Michal Simek | 9bed8a6 | 2019-10-10 11:09:15 +0200 | [diff] [blame] | 173 | if (current_el() == 3) { |
| 174 | printf("%s: Can't call SMC from EL3 context\n", __func__); |
| 175 | return -EPERM; |
| 176 | } |
Michal Simek | 866225f | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 177 | |
Michal Simek | 4036195 | 2019-10-04 15:35:45 +0200 | [diff] [blame] | 178 | regs.regs[0] = PM_SIP_SVC | api_id; |
Michal Simek | 866225f | 2019-10-04 15:45:29 +0200 | [diff] [blame] | 179 | regs.regs[1] = ((u64)arg1 << 32) | arg0; |
| 180 | regs.regs[2] = ((u64)arg3 << 32) | arg2; |
| 181 | |
| 182 | smc_call(®s); |
| 183 | |
| 184 | if (ret_payload) { |
| 185 | ret_payload[0] = (u32)regs.regs[0]; |
| 186 | ret_payload[1] = upper_32_bits(regs.regs[0]); |
| 187 | ret_payload[2] = (u32)regs.regs[1]; |
| 188 | ret_payload[3] = upper_32_bits(regs.regs[1]); |
| 189 | ret_payload[4] = (u32)regs.regs[2]; |
| 190 | } |
| 191 | |
| 192 | return regs.regs[0]; |
| 193 | } |
| 194 | |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 195 | static const struct udevice_id zynqmp_firmware_ids[] = { |
| 196 | { .compatible = "xlnx,zynqmp-firmware" }, |
Siva Durga Prasad Paladugu | 9510508 | 2019-06-23 12:24:57 +0530 | [diff] [blame] | 197 | { .compatible = "xlnx,versal-firmware"}, |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 198 | { } |
| 199 | }; |
| 200 | |
| 201 | U_BOOT_DRIVER(zynqmp_firmware) = { |
| 202 | .id = UCLASS_FIRMWARE, |
| 203 | .name = "zynqmp-firmware", |
Rajan Vaja | 14723ed | 2019-02-15 04:45:32 -0800 | [diff] [blame] | 204 | .of_match = zynqmp_firmware_ids, |
| 205 | }; |