1 // SPDX-License-Identifier: GPL-2.0
3 * Xilinx Zynq MPSoC Firmware layer
5 * Copyright (C) 2014-2021 Xilinx, Inc.
13 #include <linux/arm-smccc.h>
14 #include <linux/compiler.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/mfd/core.h>
18 #include <linux/module.h>
20 #include <linux/of_platform.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23 #include <linux/hashtable.h>
25 #include <linux/firmware/xlnx-zynqmp.h>
26 #include <linux/firmware/xlnx-event-manager.h>
27 #include "zynqmp-debug.h"
29 /* Max HashMap Order for PM API feature check (1<<7 = 128) */
30 #define PM_API_FEATURE_CHECK_MAX_ORDER 7
32 /* CRL registers and bitfields */
33 #define CRL_APB_BASE 0xFF5E0000U
34 /* BOOT_PIN_CTRL- Used to control the mode pins after boot */
35 #define CRL_APB_BOOT_PIN_CTRL (CRL_APB_BASE + (0x250U))
36 /* BOOT_PIN_CTRL_MASK- out_val[11:8], out_en[3:0] */
37 #define CRL_APB_BOOTPIN_CTRL_MASK 0xF0FU
39 static bool feature_check_enabled;
40 static DEFINE_HASHTABLE(pm_api_features_map, PM_API_FEATURE_CHECK_MAX_ORDER);
42 static struct platform_device *em_dev;
45 * struct pm_api_feature_data - PM API Feature data
46 * @pm_api_id: PM API Id, used as key to index into hashmap
47 * @feature_status: status of PM API feature: valid, invalid
48 * @hentry: hlist_node that hooks this entry into hashtable
50 struct pm_api_feature_data {
53 struct hlist_node hentry;
56 static const struct mfd_cell firmware_devs[] = {
58 .name = "zynqmp_power_controller",
63 * zynqmp_pm_ret_code() - Convert PMU-FW error codes to Linux error codes
64 * @ret_status: PMUFW return code
66 * Return: corresponding Linux error code
68 static int zynqmp_pm_ret_code(u32 ret_status)
72 case XST_PM_DOUBLE_REQ:
74 case XST_PM_NO_FEATURE:
76 case XST_PM_NO_ACCESS:
78 case XST_PM_ABORT_SUSPEND:
80 case XST_PM_MULT_USER:
84 case XST_PM_INVALID_NODE:
90 static noinline int do_fw_call_fail(u64 arg0, u64 arg1, u64 arg2,
97 * PM function call wrapper
98 * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
100 static int (*do_fw_call)(u64, u64, u64, u32 *ret_payload) = do_fw_call_fail;
103 * do_fw_call_smc() - Call system-level platform management layer (SMC)
104 * @arg0: Argument 0 to SMC call
105 * @arg1: Argument 1 to SMC call
106 * @arg2: Argument 2 to SMC call
107 * @ret_payload: Returned value array
109 * Invoke platform management function via SMC call (no hypervisor present).
111 * Return: Returns status, either success or error+reason
113 static noinline int do_fw_call_smc(u64 arg0, u64 arg1, u64 arg2,
116 struct arm_smccc_res res;
118 arm_smccc_smc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
121 ret_payload[0] = lower_32_bits(res.a0);
122 ret_payload[1] = upper_32_bits(res.a0);
123 ret_payload[2] = lower_32_bits(res.a1);
124 ret_payload[3] = upper_32_bits(res.a1);
127 return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
131 * do_fw_call_hvc() - Call system-level platform management layer (HVC)
132 * @arg0: Argument 0 to HVC call
133 * @arg1: Argument 1 to HVC call
134 * @arg2: Argument 2 to HVC call
135 * @ret_payload: Returned value array
137 * Invoke platform management function via HVC
138 * HVC-based for communication through hypervisor
139 * (no direct communication with ATF).
141 * Return: Returns status, either success or error+reason
143 static noinline int do_fw_call_hvc(u64 arg0, u64 arg1, u64 arg2,
146 struct arm_smccc_res res;
148 arm_smccc_hvc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
151 ret_payload[0] = lower_32_bits(res.a0);
152 ret_payload[1] = upper_32_bits(res.a0);
153 ret_payload[2] = lower_32_bits(res.a1);
154 ret_payload[3] = upper_32_bits(res.a1);
157 return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
161 * zynqmp_pm_feature() - Check weather given feature is supported or not
162 * @api_id: API ID to check
164 * Return: Returns status, either success or error+reason
166 int zynqmp_pm_feature(const u32 api_id)
169 u32 ret_payload[PAYLOAD_ARG_CNT];
171 struct pm_api_feature_data *feature_data;
173 if (!feature_check_enabled)
176 /* Check for existing entry in hash table for given api */
177 hash_for_each_possible(pm_api_features_map, feature_data, hentry,
179 if (feature_data->pm_api_id == api_id)
180 return feature_data->feature_status;
183 /* Add new entry if not present */
184 feature_data = kmalloc(sizeof(*feature_data), GFP_KERNEL);
188 feature_data->pm_api_id = api_id;
189 smc_arg[0] = PM_SIP_SVC | PM_FEATURE_CHECK;
192 ret = do_fw_call(smc_arg[0], smc_arg[1], 0, ret_payload);
196 ret = ret_payload[1];
198 feature_data->feature_status = ret;
199 hash_add(pm_api_features_map, &feature_data->hentry, api_id);
203 EXPORT_SYMBOL_GPL(zynqmp_pm_feature);
206 * zynqmp_pm_invoke_fn() - Invoke the system-level platform management layer
207 * caller function depending on the configuration
208 * @pm_api_id: Requested PM-API call
209 * @arg0: Argument 0 to requested PM-API call
210 * @arg1: Argument 1 to requested PM-API call
211 * @arg2: Argument 2 to requested PM-API call
212 * @arg3: Argument 3 to requested PM-API call
213 * @ret_payload: Returned value array
215 * Invoke platform management function for SMC or HVC call, depending on
217 * Following SMC Calling Convention (SMCCC) for SMC64:
218 * Pm Function Identifier,
219 * PM_SIP_SVC + PM_API_ID =
220 * ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
221 * ((SMC_64) << FUNCID_CC_SHIFT)
222 * ((SIP_START) << FUNCID_OEN_SHIFT)
223 * ((PM_API_ID) & FUNCID_NUM_MASK))
225 * PM_SIP_SVC - Registered ZynqMP SIP Service Call.
226 * PM_API_ID - Platform Management API ID.
228 * Return: Returns status, either success or error+reason
230 int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 arg0, u32 arg1,
231 u32 arg2, u32 arg3, u32 *ret_payload)
234 * Added SIP service call Function Identifier
235 * Make sure to stay in x0 register
240 /* Check if feature is supported or not */
241 ret = zynqmp_pm_feature(pm_api_id);
245 smc_arg[0] = PM_SIP_SVC | pm_api_id;
246 smc_arg[1] = ((u64)arg1 << 32) | arg0;
247 smc_arg[2] = ((u64)arg3 << 32) | arg2;
249 return do_fw_call(smc_arg[0], smc_arg[1], smc_arg[2], ret_payload);
252 static u32 pm_api_version;
253 static u32 pm_tz_version;
256 * zynqmp_pm_get_api_version() - Get version number of PMU PM firmware
257 * @version: Returned version value
259 * Return: Returns status, either success or error+reason
261 int zynqmp_pm_get_api_version(u32 *version)
263 u32 ret_payload[PAYLOAD_ARG_CNT];
269 /* Check is PM API version already verified */
270 if (pm_api_version > 0) {
271 *version = pm_api_version;
274 ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, 0, 0, 0, 0, ret_payload);
275 *version = ret_payload[1];
279 EXPORT_SYMBOL_GPL(zynqmp_pm_get_api_version);
282 * zynqmp_pm_get_chipid - Get silicon ID registers
283 * @idcode: IDCODE register
284 * @version: version register
286 * Return: Returns the status of the operation and the idcode and version
287 * registers in @idcode and @version.
289 int zynqmp_pm_get_chipid(u32 *idcode, u32 *version)
291 u32 ret_payload[PAYLOAD_ARG_CNT];
294 if (!idcode || !version)
297 ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
298 *idcode = ret_payload[1];
299 *version = ret_payload[2];
303 EXPORT_SYMBOL_GPL(zynqmp_pm_get_chipid);
306 * zynqmp_pm_get_trustzone_version() - Get secure trustzone firmware version
307 * @version: Returned version value
309 * Return: Returns status, either success or error+reason
311 static int zynqmp_pm_get_trustzone_version(u32 *version)
313 u32 ret_payload[PAYLOAD_ARG_CNT];
319 /* Check is PM trustzone version already verified */
320 if (pm_tz_version > 0) {
321 *version = pm_tz_version;
324 ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, 0, 0,
326 *version = ret_payload[1];
332 * get_set_conduit_method() - Choose SMC or HVC based communication
333 * @np: Pointer to the device_node structure
335 * Use SMC or HVC-based functions to communicate with EL2/EL3.
337 * Return: Returns 0 on success or error code
339 static int get_set_conduit_method(struct device_node *np)
343 if (of_property_read_string(np, "method", &method)) {
344 pr_warn("%s missing \"method\" property\n", __func__);
348 if (!strcmp("hvc", method)) {
349 do_fw_call = do_fw_call_hvc;
350 } else if (!strcmp("smc", method)) {
351 do_fw_call = do_fw_call_smc;
353 pr_warn("%s Invalid \"method\" property: %s\n",
362 * zynqmp_pm_query_data() - Get query data from firmware
363 * @qdata: Variable to the zynqmp_pm_query_data structure
364 * @out: Returned output value
366 * Return: Returns status, either success or error+reason
368 int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
372 ret = zynqmp_pm_invoke_fn(PM_QUERY_DATA, qdata.qid, qdata.arg1,
373 qdata.arg2, qdata.arg3, out);
376 * For clock name query, all bytes in SMC response are clock name
377 * characters and return code is always success. For invalid clocks,
378 * clock name bytes would be zeros.
380 return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : ret;
382 EXPORT_SYMBOL_GPL(zynqmp_pm_query_data);
385 * zynqmp_pm_clock_enable() - Enable the clock for given id
386 * @clock_id: ID of the clock to be enabled
388 * This function is used by master to enable the clock
389 * including peripherals and PLL clocks.
391 * Return: Returns status, either success or error+reason
393 int zynqmp_pm_clock_enable(u32 clock_id)
395 return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, clock_id, 0, 0, 0, NULL);
397 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_enable);
400 * zynqmp_pm_clock_disable() - Disable the clock for given id
401 * @clock_id: ID of the clock to be disable
403 * This function is used by master to disable the clock
404 * including peripherals and PLL clocks.
406 * Return: Returns status, either success or error+reason
408 int zynqmp_pm_clock_disable(u32 clock_id)
410 return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, clock_id, 0, 0, 0, NULL);
412 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_disable);
415 * zynqmp_pm_clock_getstate() - Get the clock state for given id
416 * @clock_id: ID of the clock to be queried
417 * @state: 1/0 (Enabled/Disabled)
419 * This function is used by master to get the state of clock
420 * including peripherals and PLL clocks.
422 * Return: Returns status, either success or error+reason
424 int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
426 u32 ret_payload[PAYLOAD_ARG_CNT];
429 ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, clock_id, 0,
431 *state = ret_payload[1];
435 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getstate);
438 * zynqmp_pm_clock_setdivider() - Set the clock divider for given id
439 * @clock_id: ID of the clock
440 * @divider: divider value
442 * This function is used by master to set divider for any clock
443 * to achieve desired rate.
445 * Return: Returns status, either success or error+reason
447 int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
449 return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, clock_id, divider,
452 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setdivider);
455 * zynqmp_pm_clock_getdivider() - Get the clock divider for given id
456 * @clock_id: ID of the clock
457 * @divider: divider value
459 * This function is used by master to get divider values
462 * Return: Returns status, either success or error+reason
464 int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
466 u32 ret_payload[PAYLOAD_ARG_CNT];
469 ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, clock_id, 0,
471 *divider = ret_payload[1];
475 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getdivider);
478 * zynqmp_pm_clock_setrate() - Set the clock rate for given id
479 * @clock_id: ID of the clock
480 * @rate: rate value in hz
482 * This function is used by master to set rate for any clock.
484 * Return: Returns status, either success or error+reason
486 int zynqmp_pm_clock_setrate(u32 clock_id, u64 rate)
488 return zynqmp_pm_invoke_fn(PM_CLOCK_SETRATE, clock_id,
493 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setrate);
496 * zynqmp_pm_clock_getrate() - Get the clock rate for given id
497 * @clock_id: ID of the clock
498 * @rate: rate value in hz
500 * This function is used by master to get rate
503 * Return: Returns status, either success or error+reason
505 int zynqmp_pm_clock_getrate(u32 clock_id, u64 *rate)
507 u32 ret_payload[PAYLOAD_ARG_CNT];
510 ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETRATE, clock_id, 0,
512 *rate = ((u64)ret_payload[2] << 32) | ret_payload[1];
516 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getrate);
519 * zynqmp_pm_clock_setparent() - Set the clock parent for given id
520 * @clock_id: ID of the clock
521 * @parent_id: parent id
523 * This function is used by master to set parent for any clock.
525 * Return: Returns status, either success or error+reason
527 int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
529 return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, clock_id,
530 parent_id, 0, 0, NULL);
532 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setparent);
535 * zynqmp_pm_clock_getparent() - Get the clock parent for given id
536 * @clock_id: ID of the clock
537 * @parent_id: parent id
539 * This function is used by master to get parent index
542 * Return: Returns status, either success or error+reason
544 int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
546 u32 ret_payload[PAYLOAD_ARG_CNT];
549 ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, clock_id, 0,
551 *parent_id = ret_payload[1];
555 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getparent);
558 * zynqmp_pm_set_pll_frac_mode() - PM API for set PLL mode
560 * @clk_id: PLL clock ID
561 * @mode: PLL mode (PLL_MODE_FRAC/PLL_MODE_INT)
563 * This function sets PLL mode
565 * Return: Returns status, either success or error+reason
567 int zynqmp_pm_set_pll_frac_mode(u32 clk_id, u32 mode)
569 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_SET_PLL_FRAC_MODE,
572 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_mode);
575 * zynqmp_pm_get_pll_frac_mode() - PM API for get PLL mode
577 * @clk_id: PLL clock ID
580 * This function return current PLL mode
582 * Return: Returns status, either success or error+reason
584 int zynqmp_pm_get_pll_frac_mode(u32 clk_id, u32 *mode)
586 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_GET_PLL_FRAC_MODE,
589 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_mode);
592 * zynqmp_pm_set_pll_frac_data() - PM API for setting pll fraction data
594 * @clk_id: PLL clock ID
595 * @data: fraction data
597 * This function sets fraction data.
598 * It is valid for fraction mode only.
600 * Return: Returns status, either success or error+reason
602 int zynqmp_pm_set_pll_frac_data(u32 clk_id, u32 data)
604 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_SET_PLL_FRAC_DATA,
607 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_data);
610 * zynqmp_pm_get_pll_frac_data() - PM API for getting pll fraction data
612 * @clk_id: PLL clock ID
613 * @data: fraction data
615 * This function returns fraction data value.
617 * Return: Returns status, either success or error+reason
619 int zynqmp_pm_get_pll_frac_data(u32 clk_id, u32 *data)
621 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_GET_PLL_FRAC_DATA,
624 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_data);
627 * zynqmp_pm_set_sd_tapdelay() - Set tap delay for the SD device
629 * @node_id: Node ID of the device
630 * @type: Type of tap delay to set (input/output)
631 * @value: Value to set fot the tap delay
633 * This function sets input/output tap delay for the SD device.
635 * Return: Returns status, either success or error+reason
637 int zynqmp_pm_set_sd_tapdelay(u32 node_id, u32 type, u32 value)
639 return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, IOCTL_SET_SD_TAPDELAY,
642 EXPORT_SYMBOL_GPL(zynqmp_pm_set_sd_tapdelay);
645 * zynqmp_pm_sd_dll_reset() - Reset DLL logic
647 * @node_id: Node ID of the device
650 * This function resets DLL logic for the SD device.
652 * Return: Returns status, either success or error+reason
654 int zynqmp_pm_sd_dll_reset(u32 node_id, u32 type)
656 return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, IOCTL_SD_DLL_RESET,
659 EXPORT_SYMBOL_GPL(zynqmp_pm_sd_dll_reset);
662 * zynqmp_pm_ospi_mux_select() - OSPI Mux selection
664 * @dev_id: Device Id of the OSPI device.
665 * @select: OSPI Mux select value.
667 * This function select the OSPI Mux.
669 * Return: Returns status, either success or error+reason
671 int zynqmp_pm_ospi_mux_select(u32 dev_id, u32 select)
673 return zynqmp_pm_invoke_fn(PM_IOCTL, dev_id, IOCTL_OSPI_MUX_SELECT,
676 EXPORT_SYMBOL_GPL(zynqmp_pm_ospi_mux_select);
679 * zynqmp_pm_write_ggs() - PM API for writing global general storage (ggs)
680 * @index: GGS register index
681 * @value: Register value to be written
683 * This function writes value to GGS register.
685 * Return: Returns status, either success or error+reason
687 int zynqmp_pm_write_ggs(u32 index, u32 value)
689 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_WRITE_GGS,
692 EXPORT_SYMBOL_GPL(zynqmp_pm_write_ggs);
695 * zynqmp_pm_read_ggs() - PM API for reading global general storage (ggs)
696 * @index: GGS register index
697 * @value: Register value to be written
699 * This function returns GGS register value.
701 * Return: Returns status, either success or error+reason
703 int zynqmp_pm_read_ggs(u32 index, u32 *value)
705 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_READ_GGS,
708 EXPORT_SYMBOL_GPL(zynqmp_pm_read_ggs);
711 * zynqmp_pm_write_pggs() - PM API for writing persistent global general
713 * @index: PGGS register index
714 * @value: Register value to be written
716 * This function writes value to PGGS register.
718 * Return: Returns status, either success or error+reason
720 int zynqmp_pm_write_pggs(u32 index, u32 value)
722 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_WRITE_PGGS, index, value,
725 EXPORT_SYMBOL_GPL(zynqmp_pm_write_pggs);
728 * zynqmp_pm_read_pggs() - PM API for reading persistent global general
730 * @index: PGGS register index
731 * @value: Register value to be written
733 * This function returns PGGS register value.
735 * Return: Returns status, either success or error+reason
737 int zynqmp_pm_read_pggs(u32 index, u32 *value)
739 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_READ_PGGS, index, 0,
742 EXPORT_SYMBOL_GPL(zynqmp_pm_read_pggs);
745 * zynqmp_pm_set_boot_health_status() - PM API for setting healthy boot status
746 * @value: Status value to be written
748 * This function sets healthy bit value to indicate boot health status
751 * Return: Returns status, either success or error+reason
753 int zynqmp_pm_set_boot_health_status(u32 value)
755 return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_SET_BOOT_HEALTH_STATUS,
760 * zynqmp_pm_reset_assert - Request setting of reset (1 - assert, 0 - release)
761 * @reset: Reset to be configured
762 * @assert_flag: Flag stating should reset be asserted (1) or
765 * Return: Returns status, either success or error+reason
767 int zynqmp_pm_reset_assert(const enum zynqmp_pm_reset reset,
768 const enum zynqmp_pm_reset_action assert_flag)
770 return zynqmp_pm_invoke_fn(PM_RESET_ASSERT, reset, assert_flag,
773 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_assert);
776 * zynqmp_pm_reset_get_status - Get status of the reset
777 * @reset: Reset whose status should be returned
778 * @status: Returned status
780 * Return: Returns status, either success or error+reason
782 int zynqmp_pm_reset_get_status(const enum zynqmp_pm_reset reset, u32 *status)
784 u32 ret_payload[PAYLOAD_ARG_CNT];
790 ret = zynqmp_pm_invoke_fn(PM_RESET_GET_STATUS, reset, 0,
792 *status = ret_payload[1];
796 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_get_status);
799 * zynqmp_pm_fpga_load - Perform the fpga load
800 * @address: Address to write to
801 * @size: pl bitstream size
802 * @flags: Bitstream type
803 * -XILINX_ZYNQMP_PM_FPGA_FULL: FPGA full reconfiguration
804 * -XILINX_ZYNQMP_PM_FPGA_PARTIAL: FPGA partial reconfiguration
806 * This function provides access to pmufw. To transfer
807 * the required bitstream into PL.
809 * Return: Returns status, either success or error+reason
811 int zynqmp_pm_fpga_load(const u64 address, const u32 size, const u32 flags)
813 return zynqmp_pm_invoke_fn(PM_FPGA_LOAD, lower_32_bits(address),
814 upper_32_bits(address), size, flags, NULL);
816 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_load);
819 * zynqmp_pm_fpga_get_status - Read value from PCAP status register
820 * @value: Value to read
822 * This function provides access to the pmufw to get the PCAP
825 * Return: Returns status, either success or error+reason
827 int zynqmp_pm_fpga_get_status(u32 *value)
829 u32 ret_payload[PAYLOAD_ARG_CNT];
835 ret = zynqmp_pm_invoke_fn(PM_FPGA_GET_STATUS, 0, 0, 0, 0, ret_payload);
836 *value = ret_payload[1];
840 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_get_status);
843 * zynqmp_pm_pinctrl_request - Request Pin from firmware
844 * @pin: Pin number to request
846 * This function requests pin from firmware.
848 * Return: Returns status, either success or error+reason.
850 int zynqmp_pm_pinctrl_request(const u32 pin)
852 return zynqmp_pm_invoke_fn(PM_PINCTRL_REQUEST, pin, 0, 0, 0, NULL);
854 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_request);
857 * zynqmp_pm_pinctrl_release - Inform firmware that Pin control is released
858 * @pin: Pin number to release
860 * This function release pin from firmware.
862 * Return: Returns status, either success or error+reason.
864 int zynqmp_pm_pinctrl_release(const u32 pin)
866 return zynqmp_pm_invoke_fn(PM_PINCTRL_RELEASE, pin, 0, 0, 0, NULL);
868 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_release);
871 * zynqmp_pm_pinctrl_get_function - Read function id set for the given pin
873 * @id: Buffer to store function ID
875 * This function provides the function currently set for the given pin.
877 * Return: Returns status, either success or error+reason
879 int zynqmp_pm_pinctrl_get_function(const u32 pin, u32 *id)
881 u32 ret_payload[PAYLOAD_ARG_CNT];
887 ret = zynqmp_pm_invoke_fn(PM_PINCTRL_GET_FUNCTION, pin, 0,
889 *id = ret_payload[1];
893 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_get_function);
896 * zynqmp_pm_pinctrl_set_function - Set requested function for the pin
898 * @id: Function ID to set
900 * This function sets requested function for the given pin.
902 * Return: Returns status, either success or error+reason.
904 int zynqmp_pm_pinctrl_set_function(const u32 pin, const u32 id)
906 return zynqmp_pm_invoke_fn(PM_PINCTRL_SET_FUNCTION, pin, id,
909 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_function);
912 * zynqmp_pm_pinctrl_get_config - Get configuration parameter for the pin
914 * @param: Parameter to get
915 * @value: Buffer to store parameter value
917 * This function gets requested configuration parameter for the given pin.
919 * Return: Returns status, either success or error+reason.
921 int zynqmp_pm_pinctrl_get_config(const u32 pin, const u32 param,
924 u32 ret_payload[PAYLOAD_ARG_CNT];
930 ret = zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_GET, pin, param,
932 *value = ret_payload[1];
936 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_get_config);
939 * zynqmp_pm_pinctrl_set_config - Set configuration parameter for the pin
941 * @param: Parameter to set
942 * @value: Parameter value to set
944 * This function sets requested configuration parameter for the given pin.
946 * Return: Returns status, either success or error+reason.
948 int zynqmp_pm_pinctrl_set_config(const u32 pin, const u32 param,
951 return zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_SET, pin,
952 param, value, 0, NULL);
954 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_config);
957 * zynqmp_pm_bootmode_read() - PM Config API for read bootpin status
958 * @ps_mode: Returned output value of ps_mode
960 * This API function is to be used for notify the power management controller
961 * to read bootpin status.
963 * Return: status, either success or error+reason
965 unsigned int zynqmp_pm_bootmode_read(u32 *ps_mode)
968 u32 ret_payload[PAYLOAD_ARG_CNT];
970 ret = zynqmp_pm_invoke_fn(PM_MMIO_READ, CRL_APB_BOOT_PIN_CTRL, 0,
973 *ps_mode = ret_payload[1];
977 EXPORT_SYMBOL_GPL(zynqmp_pm_bootmode_read);
980 * zynqmp_pm_bootmode_write() - PM Config API for Configure bootpin
981 * @ps_mode: Value to be written to the bootpin ctrl register
983 * This API function is to be used for notify the power management controller
984 * to configure bootpin.
986 * Return: Returns status, either success or error+reason
988 int zynqmp_pm_bootmode_write(u32 ps_mode)
990 return zynqmp_pm_invoke_fn(PM_MMIO_WRITE, CRL_APB_BOOT_PIN_CTRL,
991 CRL_APB_BOOTPIN_CTRL_MASK, ps_mode, 0, NULL);
993 EXPORT_SYMBOL_GPL(zynqmp_pm_bootmode_write);
996 * zynqmp_pm_init_finalize() - PM call to inform firmware that the caller
997 * master has initialized its own power management
999 * Return: Returns status, either success or error+reason
1001 * This API function is to be used for notify the power management controller
1002 * about the completed power management initialization.
1004 int zynqmp_pm_init_finalize(void)
1006 return zynqmp_pm_invoke_fn(PM_PM_INIT_FINALIZE, 0, 0, 0, 0, NULL);
1008 EXPORT_SYMBOL_GPL(zynqmp_pm_init_finalize);
1011 * zynqmp_pm_set_suspend_mode() - Set system suspend mode
1012 * @mode: Mode to set for system suspend
1014 * This API function is used to set mode of system suspend.
1016 * Return: Returns status, either success or error+reason
1018 int zynqmp_pm_set_suspend_mode(u32 mode)
1020 return zynqmp_pm_invoke_fn(PM_SET_SUSPEND_MODE, mode, 0, 0, 0, NULL);
1022 EXPORT_SYMBOL_GPL(zynqmp_pm_set_suspend_mode);
1025 * zynqmp_pm_request_node() - Request a node with specific capabilities
1026 * @node: Node ID of the slave
1027 * @capabilities: Requested capabilities of the slave
1028 * @qos: Quality of service (not supported)
1029 * @ack: Flag to specify whether acknowledge is requested
1031 * This function is used by master to request particular node from firmware.
1032 * Every master must request node before using it.
1034 * Return: Returns status, either success or error+reason
1036 int zynqmp_pm_request_node(const u32 node, const u32 capabilities,
1037 const u32 qos, const enum zynqmp_pm_request_ack ack)
1039 return zynqmp_pm_invoke_fn(PM_REQUEST_NODE, node, capabilities,
1042 EXPORT_SYMBOL_GPL(zynqmp_pm_request_node);
1045 * zynqmp_pm_release_node() - Release a node
1046 * @node: Node ID of the slave
1048 * This function is used by master to inform firmware that master
1049 * has released node. Once released, master must not use that node
1050 * without re-request.
1052 * Return: Returns status, either success or error+reason
1054 int zynqmp_pm_release_node(const u32 node)
1056 return zynqmp_pm_invoke_fn(PM_RELEASE_NODE, node, 0, 0, 0, NULL);
1058 EXPORT_SYMBOL_GPL(zynqmp_pm_release_node);
1061 * zynqmp_pm_set_requirement() - PM call to set requirement for PM slaves
1062 * @node: Node ID of the slave
1063 * @capabilities: Requested capabilities of the slave
1064 * @qos: Quality of service (not supported)
1065 * @ack: Flag to specify whether acknowledge is requested
1067 * This API function is to be used for slaves a PU already has requested
1068 * to change its capabilities.
1070 * Return: Returns status, either success or error+reason
1072 int zynqmp_pm_set_requirement(const u32 node, const u32 capabilities,
1074 const enum zynqmp_pm_request_ack ack)
1076 return zynqmp_pm_invoke_fn(PM_SET_REQUIREMENT, node, capabilities,
1079 EXPORT_SYMBOL_GPL(zynqmp_pm_set_requirement);
1082 * zynqmp_pm_load_pdi - Load and process PDI
1083 * @src: Source device where PDI is located
1084 * @address: PDI src address
1086 * This function provides support to load PDI from linux
1088 * Return: Returns status, either success or error+reason
1090 int zynqmp_pm_load_pdi(const u32 src, const u64 address)
1092 return zynqmp_pm_invoke_fn(PM_LOAD_PDI, src,
1093 lower_32_bits(address),
1094 upper_32_bits(address), 0, NULL);
1096 EXPORT_SYMBOL_GPL(zynqmp_pm_load_pdi);
1099 * zynqmp_pm_aes_engine - Access AES hardware to encrypt/decrypt the data using
1101 * @address: Address of the AesParams structure.
1102 * @out: Returned output value
1104 * Return: Returns status, either success or error code.
1106 int zynqmp_pm_aes_engine(const u64 address, u32 *out)
1108 u32 ret_payload[PAYLOAD_ARG_CNT];
1114 ret = zynqmp_pm_invoke_fn(PM_SECURE_AES, upper_32_bits(address),
1115 lower_32_bits(address),
1117 *out = ret_payload[1];
1121 EXPORT_SYMBOL_GPL(zynqmp_pm_aes_engine);
1124 * zynqmp_pm_register_notifier() - PM API for register a subsystem
1125 * to be notified about specific
1127 * @node: Node ID to which the event is related.
1128 * @event: Event Mask of Error events for which wants to get notified.
1129 * @wake: Wake subsystem upon capturing the event if value 1
1130 * @enable: Enable the registration for value 1, disable for value 0
1132 * This function is used to register/un-register for particular node-event
1133 * combination in firmware.
1135 * Return: Returns status, either success or error+reason
1138 int zynqmp_pm_register_notifier(const u32 node, const u32 event,
1139 const u32 wake, const u32 enable)
1141 return zynqmp_pm_invoke_fn(PM_REGISTER_NOTIFIER, node, event,
1142 wake, enable, NULL);
1144 EXPORT_SYMBOL_GPL(zynqmp_pm_register_notifier);
1147 * zynqmp_pm_system_shutdown - PM call to request a system shutdown or restart
1148 * @type: Shutdown or restart? 0 for shutdown, 1 for restart
1149 * @subtype: Specifies which system should be restarted or shut down
1151 * Return: Returns status, either success or error+reason
1153 int zynqmp_pm_system_shutdown(const u32 type, const u32 subtype)
1155 return zynqmp_pm_invoke_fn(PM_SYSTEM_SHUTDOWN, type, subtype,
1160 * struct zynqmp_pm_shutdown_scope - Struct for shutdown scope
1161 * @subtype: Shutdown subtype
1162 * @name: Matching string for scope argument
1164 * This struct encapsulates mapping between shutdown scope ID and string.
1166 struct zynqmp_pm_shutdown_scope {
1167 const enum zynqmp_pm_shutdown_subtype subtype;
1171 static struct zynqmp_pm_shutdown_scope shutdown_scopes[] = {
1172 [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM] = {
1173 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM,
1174 .name = "subsystem",
1176 [ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY] = {
1177 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY,
1180 [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM] = {
1181 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM,
1186 static struct zynqmp_pm_shutdown_scope *selected_scope =
1187 &shutdown_scopes[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM];
1190 * zynqmp_pm_is_shutdown_scope_valid - Check if shutdown scope string is valid
1191 * @scope_string: Shutdown scope string
1193 * Return: Return pointer to matching shutdown scope struct from
1194 * array of available options in system if string is valid,
1195 * otherwise returns NULL.
1197 static struct zynqmp_pm_shutdown_scope*
1198 zynqmp_pm_is_shutdown_scope_valid(const char *scope_string)
1202 for (count = 0; count < ARRAY_SIZE(shutdown_scopes); count++)
1203 if (sysfs_streq(scope_string, shutdown_scopes[count].name))
1204 return &shutdown_scopes[count];
1209 static ssize_t shutdown_scope_show(struct device *device,
1210 struct device_attribute *attr,
1215 for (i = 0; i < ARRAY_SIZE(shutdown_scopes); i++) {
1216 if (&shutdown_scopes[i] == selected_scope) {
1218 strcat(buf, shutdown_scopes[i].name);
1221 strcat(buf, shutdown_scopes[i].name);
1230 static ssize_t shutdown_scope_store(struct device *device,
1231 struct device_attribute *attr,
1232 const char *buf, size_t count)
1235 struct zynqmp_pm_shutdown_scope *scope;
1237 scope = zynqmp_pm_is_shutdown_scope_valid(buf);
1241 ret = zynqmp_pm_system_shutdown(ZYNQMP_PM_SHUTDOWN_TYPE_SETSCOPE_ONLY,
1244 pr_err("unable to set shutdown scope %s\n", buf);
1248 selected_scope = scope;
1253 static DEVICE_ATTR_RW(shutdown_scope);
1255 static ssize_t health_status_store(struct device *device,
1256 struct device_attribute *attr,
1257 const char *buf, size_t count)
1262 ret = kstrtouint(buf, 10, &value);
1266 ret = zynqmp_pm_set_boot_health_status(value);
1268 dev_err(device, "unable to set healthy bit value to %u\n",
1276 static DEVICE_ATTR_WO(health_status);
1278 static ssize_t ggs_show(struct device *device,
1279 struct device_attribute *attr,
1284 u32 ret_payload[PAYLOAD_ARG_CNT];
1286 ret = zynqmp_pm_read_ggs(reg, ret_payload);
1290 return sprintf(buf, "0x%x\n", ret_payload[1]);
1293 static ssize_t ggs_store(struct device *device,
1294 struct device_attribute *attr,
1295 const char *buf, size_t count,
1301 if (reg >= GSS_NUM_REGS)
1304 ret = kstrtol(buf, 16, &value);
1310 ret = zynqmp_pm_write_ggs(reg, value);
1317 /* GGS register show functions */
1318 #define GGS0_SHOW(N) \
1319 ssize_t ggs##N##_show(struct device *device, \
1320 struct device_attribute *attr, \
1323 return ggs_show(device, attr, buf, N); \
1326 static GGS0_SHOW(0);
1327 static GGS0_SHOW(1);
1328 static GGS0_SHOW(2);
1329 static GGS0_SHOW(3);
1331 /* GGS register store function */
1332 #define GGS0_STORE(N) \
1333 ssize_t ggs##N##_store(struct device *device, \
1334 struct device_attribute *attr, \
1338 return ggs_store(device, attr, buf, count, N); \
1341 static GGS0_STORE(0);
1342 static GGS0_STORE(1);
1343 static GGS0_STORE(2);
1344 static GGS0_STORE(3);
1346 static ssize_t pggs_show(struct device *device,
1347 struct device_attribute *attr,
1352 u32 ret_payload[PAYLOAD_ARG_CNT];
1354 ret = zynqmp_pm_read_pggs(reg, ret_payload);
1358 return sprintf(buf, "0x%x\n", ret_payload[1]);
1361 static ssize_t pggs_store(struct device *device,
1362 struct device_attribute *attr,
1363 const char *buf, size_t count,
1369 if (reg >= GSS_NUM_REGS)
1372 ret = kstrtol(buf, 16, &value);
1378 ret = zynqmp_pm_write_pggs(reg, value);
1386 #define PGGS0_SHOW(N) \
1387 ssize_t pggs##N##_show(struct device *device, \
1388 struct device_attribute *attr, \
1391 return pggs_show(device, attr, buf, N); \
1394 #define PGGS0_STORE(N) \
1395 ssize_t pggs##N##_store(struct device *device, \
1396 struct device_attribute *attr, \
1400 return pggs_store(device, attr, buf, count, N); \
1403 /* PGGS register show functions */
1404 static PGGS0_SHOW(0);
1405 static PGGS0_SHOW(1);
1406 static PGGS0_SHOW(2);
1407 static PGGS0_SHOW(3);
1409 /* PGGS register store functions */
1410 static PGGS0_STORE(0);
1411 static PGGS0_STORE(1);
1412 static PGGS0_STORE(2);
1413 static PGGS0_STORE(3);
1415 /* GGS register attributes */
1416 static DEVICE_ATTR_RW(ggs0);
1417 static DEVICE_ATTR_RW(ggs1);
1418 static DEVICE_ATTR_RW(ggs2);
1419 static DEVICE_ATTR_RW(ggs3);
1421 /* PGGS register attributes */
1422 static DEVICE_ATTR_RW(pggs0);
1423 static DEVICE_ATTR_RW(pggs1);
1424 static DEVICE_ATTR_RW(pggs2);
1425 static DEVICE_ATTR_RW(pggs3);
1427 static struct attribute *zynqmp_firmware_attrs[] = {
1428 &dev_attr_ggs0.attr,
1429 &dev_attr_ggs1.attr,
1430 &dev_attr_ggs2.attr,
1431 &dev_attr_ggs3.attr,
1432 &dev_attr_pggs0.attr,
1433 &dev_attr_pggs1.attr,
1434 &dev_attr_pggs2.attr,
1435 &dev_attr_pggs3.attr,
1436 &dev_attr_shutdown_scope.attr,
1437 &dev_attr_health_status.attr,
1441 ATTRIBUTE_GROUPS(zynqmp_firmware);
1443 static int zynqmp_firmware_probe(struct platform_device *pdev)
1445 struct device *dev = &pdev->dev;
1446 struct device_node *np;
1449 np = of_find_compatible_node(NULL, NULL, "xlnx,zynqmp");
1451 np = of_find_compatible_node(NULL, NULL, "xlnx,versal");
1455 feature_check_enabled = true;
1459 ret = get_set_conduit_method(dev->of_node);
1463 /* Check PM API version number */
1464 ret = zynqmp_pm_get_api_version(&pm_api_version);
1468 if (pm_api_version < ZYNQMP_PM_VERSION) {
1469 panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
1471 ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
1472 pm_api_version >> 16, pm_api_version & 0xFFFF);
1475 pr_info("%s Platform Management API v%d.%d\n", __func__,
1476 pm_api_version >> 16, pm_api_version & 0xFFFF);
1478 /* Check trustzone version number */
1479 ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
1481 panic("Legacy trustzone found without version support\n");
1483 if (pm_tz_version < ZYNQMP_TZ_VERSION)
1484 panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
1486 ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
1487 pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1489 pr_info("%s Trustzone version v%d.%d\n", __func__,
1490 pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1492 ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, firmware_devs,
1493 ARRAY_SIZE(firmware_devs), NULL, 0, NULL);
1495 dev_err(&pdev->dev, "failed to add MFD devices %d\n", ret);
1499 zynqmp_pm_api_debugfs_init();
1501 np = of_find_compatible_node(NULL, NULL, "xlnx,versal");
1503 em_dev = platform_device_register_data(&pdev->dev, "xlnx_event_manager",
1506 dev_err_probe(&pdev->dev, PTR_ERR(em_dev), "EM register fail with error\n");
1510 return of_platform_populate(dev->of_node, NULL, NULL, dev);
1513 static int zynqmp_firmware_remove(struct platform_device *pdev)
1515 struct pm_api_feature_data *feature_data;
1516 struct hlist_node *tmp;
1519 mfd_remove_devices(&pdev->dev);
1520 zynqmp_pm_api_debugfs_exit();
1522 hash_for_each_safe(pm_api_features_map, i, tmp, feature_data, hentry) {
1523 hash_del(&feature_data->hentry);
1524 kfree(feature_data);
1527 platform_device_unregister(em_dev);
1532 static const struct of_device_id zynqmp_firmware_of_match[] = {
1533 {.compatible = "xlnx,zynqmp-firmware"},
1534 {.compatible = "xlnx,versal-firmware"},
1537 MODULE_DEVICE_TABLE(of, zynqmp_firmware_of_match);
1539 static struct platform_driver zynqmp_firmware_driver = {
1541 .name = "zynqmp_firmware",
1542 .of_match_table = zynqmp_firmware_of_match,
1543 .dev_groups = zynqmp_firmware_groups,
1545 .probe = zynqmp_firmware_probe,
1546 .remove = zynqmp_firmware_remove,
1548 module_platform_driver(zynqmp_firmware_driver);