• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Firmware layer
4  *
5  *  Copyright (C) 2014-2022 Xilinx, Inc.
6  *  Copyright (C) 2022 - 2023, Advanced Micro Devices, Inc.
7  *
8  *  Michal Simek <michal.simek@amd.com>
9  *  Davorin Mista <davorin.mista@aggios.com>
10  *  Jolly Shah <jollys@xilinx.com>
11  *  Rajan Vaja <rajanv@xilinx.com>
12  */
13 
14 #include <linux/arm-smccc.h>
15 #include <linux/compiler.h>
16 #include <linux/device.h>
17 #include <linux/init.h>
18 #include <linux/mfd/core.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/slab.h>
24 #include <linux/uaccess.h>
25 #include <linux/hashtable.h>
26 
27 #include <linux/firmware/xlnx-zynqmp.h>
28 #include <linux/firmware/xlnx-event-manager.h>
29 #include "zynqmp-debug.h"
30 
31 /* Max HashMap Order for PM API feature check (1<<7 = 128) */
32 #define PM_API_FEATURE_CHECK_MAX_ORDER  7
33 
34 /* CRL registers and bitfields */
35 #define CRL_APB_BASE			0xFF5E0000U
36 /* BOOT_PIN_CTRL- Used to control the mode pins after boot */
37 #define CRL_APB_BOOT_PIN_CTRL		(CRL_APB_BASE + (0x250U))
38 /* BOOT_PIN_CTRL_MASK- out_val[11:8], out_en[3:0] */
39 #define CRL_APB_BOOTPIN_CTRL_MASK	0xF0FU
40 
41 /* IOCTL/QUERY feature payload size */
42 #define FEATURE_PAYLOAD_SIZE		2
43 
44 static bool feature_check_enabled;
45 static DEFINE_HASHTABLE(pm_api_features_map, PM_API_FEATURE_CHECK_MAX_ORDER);
46 static u32 ioctl_features[FEATURE_PAYLOAD_SIZE];
47 static u32 query_features[FEATURE_PAYLOAD_SIZE];
48 
49 static struct platform_device *em_dev;
50 
51 /**
52  * struct zynqmp_devinfo - Structure for Zynqmp device instance
53  * @dev:		Device Pointer
54  * @feature_conf_id:	Feature conf id
55  */
56 struct zynqmp_devinfo {
57 	struct device *dev;
58 	u32 feature_conf_id;
59 };
60 
61 /**
62  * struct pm_api_feature_data - PM API Feature data
63  * @pm_api_id:		PM API Id, used as key to index into hashmap
64  * @feature_status:	status of PM API feature: valid, invalid
65  * @hentry:		hlist_node that hooks this entry into hashtable
66  */
67 struct pm_api_feature_data {
68 	u32 pm_api_id;
69 	int feature_status;
70 	struct hlist_node hentry;
71 };
72 
73 static const struct mfd_cell firmware_devs[] = {
74 	{
75 		.name = "zynqmp_power_controller",
76 	},
77 };
78 
79 /**
80  * zynqmp_pm_ret_code() - Convert PMU-FW error codes to Linux error codes
81  * @ret_status:		PMUFW return code
82  *
83  * Return: corresponding Linux error code
84  */
zynqmp_pm_ret_code(u32 ret_status)85 static int zynqmp_pm_ret_code(u32 ret_status)
86 {
87 	switch (ret_status) {
88 	case XST_PM_SUCCESS:
89 	case XST_PM_DOUBLE_REQ:
90 		return 0;
91 	case XST_PM_NO_FEATURE:
92 		return -ENOTSUPP;
93 	case XST_PM_INVALID_VERSION:
94 		return -EOPNOTSUPP;
95 	case XST_PM_NO_ACCESS:
96 		return -EACCES;
97 	case XST_PM_ABORT_SUSPEND:
98 		return -ECANCELED;
99 	case XST_PM_MULT_USER:
100 		return -EUSERS;
101 	case XST_PM_INTERNAL:
102 	case XST_PM_CONFLICT:
103 	case XST_PM_INVALID_NODE:
104 	case XST_PM_INVALID_CRC:
105 	default:
106 		return -EINVAL;
107 	}
108 }
109 
do_fw_call_fail(u32 * ret_payload,u32 num_args,...)110 static noinline int do_fw_call_fail(u32 *ret_payload, u32 num_args, ...)
111 {
112 	return -ENODEV;
113 }
114 
115 /*
116  * PM function call wrapper
117  * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
118  */
119 static int (*do_fw_call)(u32 *ret_payload, u32, ...) = do_fw_call_fail;
120 
121 /**
122  * do_fw_call_smc() - Call system-level platform management layer (SMC)
123  * @num_args:		Number of variable arguments should be <= 8
124  * @ret_payload:	Returned value array
125  *
126  * Invoke platform management function via SMC call (no hypervisor present).
127  *
128  * Return: Returns status, either success or error+reason
129  */
do_fw_call_smc(u32 * ret_payload,u32 num_args,...)130 static noinline int do_fw_call_smc(u32 *ret_payload, u32 num_args, ...)
131 {
132 	struct arm_smccc_res res;
133 	u64 args[8] = {0};
134 	va_list arg_list;
135 	u8 i;
136 
137 	if (num_args > 8)
138 		return -EINVAL;
139 
140 	va_start(arg_list, num_args);
141 
142 	for (i = 0; i < num_args; i++)
143 		args[i] = va_arg(arg_list, u64);
144 
145 	va_end(arg_list);
146 
147 	arm_smccc_smc(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &res);
148 
149 	if (ret_payload) {
150 		ret_payload[0] = lower_32_bits(res.a0);
151 		ret_payload[1] = upper_32_bits(res.a0);
152 		ret_payload[2] = lower_32_bits(res.a1);
153 		ret_payload[3] = upper_32_bits(res.a1);
154 	}
155 
156 	return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
157 }
158 
159 /**
160  * do_fw_call_hvc() - Call system-level platform management layer (HVC)
161  * @num_args:		Number of variable arguments should be <= 8
162  * @ret_payload:	Returned value array
163  *
164  * Invoke platform management function via HVC
165  * HVC-based for communication through hypervisor
166  * (no direct communication with ATF).
167  *
168  * Return: Returns status, either success or error+reason
169  */
do_fw_call_hvc(u32 * ret_payload,u32 num_args,...)170 static noinline int do_fw_call_hvc(u32 *ret_payload, u32 num_args, ...)
171 {
172 	struct arm_smccc_res res;
173 	u64 args[8] = {0};
174 	va_list arg_list;
175 	u8 i;
176 
177 	if (num_args > 8)
178 		return -EINVAL;
179 
180 	va_start(arg_list, num_args);
181 
182 	for (i = 0; i < num_args; i++)
183 		args[i] = va_arg(arg_list, u64);
184 
185 	va_end(arg_list);
186 
187 	arm_smccc_hvc(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], &res);
188 
189 	if (ret_payload) {
190 		ret_payload[0] = lower_32_bits(res.a0);
191 		ret_payload[1] = upper_32_bits(res.a0);
192 		ret_payload[2] = lower_32_bits(res.a1);
193 		ret_payload[3] = upper_32_bits(res.a1);
194 	}
195 
196 	return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
197 }
198 
__do_feature_check_call(const u32 api_id,u32 * ret_payload)199 static int __do_feature_check_call(const u32 api_id, u32 *ret_payload)
200 {
201 	int ret;
202 	u64 smc_arg[2];
203 	u32 module_id;
204 	u32 feature_check_api_id;
205 
206 	module_id = FIELD_GET(MODULE_ID_MASK, api_id);
207 
208 	/*
209 	 * Feature check of APIs belonging to PM, XSEM, and TF-A are handled by calling
210 	 * PM_FEATURE_CHECK API. For other modules, call PM_API_FEATURES API.
211 	 */
212 	if (module_id == PM_MODULE_ID || module_id == XSEM_MODULE_ID || module_id == TF_A_MODULE_ID)
213 		feature_check_api_id = PM_FEATURE_CHECK;
214 	else
215 		feature_check_api_id = PM_API_FEATURES;
216 
217 	/*
218 	 * Feature check of TF-A APIs is done in the TF-A layer and it expects for
219 	 * MODULE_ID_MASK bits of SMC's arg[0] to be the same as PM_MODULE_ID.
220 	 */
221 	if (module_id == TF_A_MODULE_ID)
222 		module_id = PM_MODULE_ID;
223 
224 	smc_arg[0] = PM_SIP_SVC | FIELD_PREP(MODULE_ID_MASK, module_id) | feature_check_api_id;
225 	smc_arg[1] = (api_id & API_ID_MASK);
226 
227 	ret = do_fw_call(ret_payload, 2, smc_arg[0], smc_arg[1]);
228 	if (ret)
229 		ret = -EOPNOTSUPP;
230 	else
231 		ret = ret_payload[1];
232 
233 	return ret;
234 }
235 
do_feature_check_call(const u32 api_id)236 static int do_feature_check_call(const u32 api_id)
237 {
238 	int ret;
239 	u32 ret_payload[PAYLOAD_ARG_CNT];
240 	struct pm_api_feature_data *feature_data;
241 
242 	/* Check for existing entry in hash table for given api */
243 	hash_for_each_possible(pm_api_features_map, feature_data, hentry,
244 			       api_id) {
245 		if (feature_data->pm_api_id == api_id)
246 			return feature_data->feature_status;
247 	}
248 
249 	/* Add new entry if not present */
250 	feature_data = kmalloc(sizeof(*feature_data), GFP_ATOMIC);
251 	if (!feature_data)
252 		return -ENOMEM;
253 
254 	feature_data->pm_api_id = api_id;
255 	ret = __do_feature_check_call(api_id, ret_payload);
256 
257 	feature_data->feature_status = ret;
258 	hash_add(pm_api_features_map, &feature_data->hentry, api_id);
259 
260 	if (api_id == PM_IOCTL)
261 		/* Store supported IOCTL IDs mask */
262 		memcpy(ioctl_features, &ret_payload[2], FEATURE_PAYLOAD_SIZE * 4);
263 	else if (api_id == PM_QUERY_DATA)
264 		/* Store supported QUERY IDs mask */
265 		memcpy(query_features, &ret_payload[2], FEATURE_PAYLOAD_SIZE * 4);
266 
267 	return ret;
268 }
269 
270 /**
271  * zynqmp_pm_feature() - Check whether given feature is supported or not and
272  *			 store supported IOCTL/QUERY ID mask
273  * @api_id:		API ID to check
274  *
275  * Return: Returns status, either success or error+reason
276  */
zynqmp_pm_feature(const u32 api_id)277 int zynqmp_pm_feature(const u32 api_id)
278 {
279 	int ret;
280 
281 	if (!feature_check_enabled)
282 		return 0;
283 
284 	ret = do_feature_check_call(api_id);
285 
286 	return ret;
287 }
288 EXPORT_SYMBOL_GPL(zynqmp_pm_feature);
289 
290 /**
291  * zynqmp_pm_is_function_supported() - Check whether given IOCTL/QUERY function
292  *				       is supported or not
293  * @api_id:		PM_IOCTL or PM_QUERY_DATA
294  * @id:			IOCTL or QUERY function IDs
295  *
296  * Return: Returns status, either success or error+reason
297  */
zynqmp_pm_is_function_supported(const u32 api_id,const u32 id)298 int zynqmp_pm_is_function_supported(const u32 api_id, const u32 id)
299 {
300 	int ret;
301 	u32 *bit_mask;
302 
303 	/* Input arguments validation */
304 	if (id >= 64 || (api_id != PM_IOCTL && api_id != PM_QUERY_DATA))
305 		return -EINVAL;
306 
307 	/* Check feature check API version */
308 	ret = do_feature_check_call(PM_FEATURE_CHECK);
309 	if (ret < 0)
310 		return ret;
311 
312 	/* Check if feature check version 2 is supported or not */
313 	if ((ret & FIRMWARE_VERSION_MASK) == PM_API_VERSION_2) {
314 		/*
315 		 * Call feature check for IOCTL/QUERY API to get IOCTL ID or
316 		 * QUERY ID feature status.
317 		 */
318 		ret = do_feature_check_call(api_id);
319 		if (ret < 0)
320 			return ret;
321 
322 		bit_mask = (api_id == PM_IOCTL) ? ioctl_features : query_features;
323 
324 		if ((bit_mask[(id / 32)] & BIT((id % 32))) == 0U)
325 			return -EOPNOTSUPP;
326 	} else {
327 		return -ENODATA;
328 	}
329 
330 	return 0;
331 }
332 EXPORT_SYMBOL_GPL(zynqmp_pm_is_function_supported);
333 
334 /**
335  * zynqmp_pm_invoke_fn() - Invoke the system-level platform management layer
336  *			   caller function depending on the configuration
337  * @pm_api_id:		Requested PM-API call
338  * @ret_payload:	Returned value array
339  * @num_args:		Number of arguments to requested PM-API call
340  *
341  * Invoke platform management function for SMC or HVC call, depending on
342  * configuration.
343  * Following SMC Calling Convention (SMCCC) for SMC64:
344  * Pm Function Identifier,
345  * PM_SIP_SVC + PM_API_ID =
346  *	((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
347  *	((SMC_64) << FUNCID_CC_SHIFT)
348  *	((SIP_START) << FUNCID_OEN_SHIFT)
349  *	((PM_API_ID) & FUNCID_NUM_MASK))
350  *
351  * PM_SIP_SVC	- Registered ZynqMP SIP Service Call.
352  * PM_API_ID	- Platform Management API ID.
353  *
354  * Return: Returns status, either success or error+reason
355  */
zynqmp_pm_invoke_fn(u32 pm_api_id,u32 * ret_payload,u32 num_args,...)356 int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 *ret_payload, u32 num_args, ...)
357 {
358 	/*
359 	 * Added SIP service call Function Identifier
360 	 * Make sure to stay in x0 register
361 	 */
362 	u64 smc_arg[8];
363 	int ret, i;
364 	va_list arg_list;
365 	u32 args[14] = {0};
366 
367 	if (num_args > 14)
368 		return -EINVAL;
369 
370 	va_start(arg_list, num_args);
371 
372 	/* Check if feature is supported or not */
373 	ret = zynqmp_pm_feature(pm_api_id);
374 	if (ret < 0)
375 		return ret;
376 
377 	for (i = 0; i < num_args; i++)
378 		args[i] = va_arg(arg_list, u32);
379 
380 	va_end(arg_list);
381 
382 	smc_arg[0] = PM_SIP_SVC | pm_api_id;
383 	for (i = 0; i < 7; i++)
384 		smc_arg[i + 1] = ((u64)args[(i * 2) + 1] << 32) | args[i * 2];
385 
386 	return do_fw_call(ret_payload, 8, smc_arg[0], smc_arg[1], smc_arg[2], smc_arg[3],
387 			  smc_arg[4], smc_arg[5], smc_arg[6], smc_arg[7]);
388 }
389 
390 static u32 pm_api_version;
391 static u32 pm_tz_version;
392 static u32 pm_family_code;
393 static u32 pm_sub_family_code;
394 
zynqmp_pm_register_sgi(u32 sgi_num,u32 reset)395 int zynqmp_pm_register_sgi(u32 sgi_num, u32 reset)
396 {
397 	int ret;
398 
399 	ret = zynqmp_pm_invoke_fn(TF_A_PM_REGISTER_SGI, NULL, 2, sgi_num, reset);
400 	if (ret != -EOPNOTSUPP && !ret)
401 		return ret;
402 
403 	/* try old implementation as fallback strategy if above fails */
404 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, IOCTL_REGISTER_SGI, sgi_num, reset);
405 }
406 
407 /**
408  * zynqmp_pm_get_api_version() - Get version number of PMU PM firmware
409  * @version:	Returned version value
410  *
411  * Return: Returns status, either success or error+reason
412  */
zynqmp_pm_get_api_version(u32 * version)413 int zynqmp_pm_get_api_version(u32 *version)
414 {
415 	u32 ret_payload[PAYLOAD_ARG_CNT];
416 	int ret;
417 
418 	if (!version)
419 		return -EINVAL;
420 
421 	/* Check is PM API version already verified */
422 	if (pm_api_version > 0) {
423 		*version = pm_api_version;
424 		return 0;
425 	}
426 	ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, ret_payload, 0);
427 	*version = ret_payload[1];
428 
429 	return ret;
430 }
431 EXPORT_SYMBOL_GPL(zynqmp_pm_get_api_version);
432 
433 /**
434  * zynqmp_pm_get_chipid - Get silicon ID registers
435  * @idcode:     IDCODE register
436  * @version:    version register
437  *
438  * Return:      Returns the status of the operation and the idcode and version
439  *              registers in @idcode and @version.
440  */
zynqmp_pm_get_chipid(u32 * idcode,u32 * version)441 int zynqmp_pm_get_chipid(u32 *idcode, u32 *version)
442 {
443 	u32 ret_payload[PAYLOAD_ARG_CNT];
444 	int ret;
445 
446 	if (!idcode || !version)
447 		return -EINVAL;
448 
449 	ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, ret_payload, 0);
450 	*idcode = ret_payload[1];
451 	*version = ret_payload[2];
452 
453 	return ret;
454 }
455 EXPORT_SYMBOL_GPL(zynqmp_pm_get_chipid);
456 
457 /**
458  * zynqmp_pm_get_family_info() - Get family info of platform
459  * @family:	Returned family code value
460  * @subfamily:	Returned sub-family code value
461  *
462  * Return: Returns status, either success or error+reason
463  */
zynqmp_pm_get_family_info(u32 * family,u32 * subfamily)464 int zynqmp_pm_get_family_info(u32 *family, u32 *subfamily)
465 {
466 	u32 ret_payload[PAYLOAD_ARG_CNT];
467 	u32 idcode;
468 	int ret;
469 
470 	/* Check is family or sub-family code already received */
471 	if (pm_family_code && pm_sub_family_code) {
472 		*family = pm_family_code;
473 		*subfamily = pm_sub_family_code;
474 		return 0;
475 	}
476 
477 	ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, ret_payload, 0);
478 	if (ret < 0)
479 		return ret;
480 
481 	idcode = ret_payload[1];
482 	pm_family_code = FIELD_GET(FAMILY_CODE_MASK, idcode);
483 	pm_sub_family_code = FIELD_GET(SUB_FAMILY_CODE_MASK, idcode);
484 	*family = pm_family_code;
485 	*subfamily = pm_sub_family_code;
486 
487 	return 0;
488 }
489 EXPORT_SYMBOL_GPL(zynqmp_pm_get_family_info);
490 
491 /**
492  * zynqmp_pm_get_trustzone_version() - Get secure trustzone firmware version
493  * @version:	Returned version value
494  *
495  * Return: Returns status, either success or error+reason
496  */
zynqmp_pm_get_trustzone_version(u32 * version)497 static int zynqmp_pm_get_trustzone_version(u32 *version)
498 {
499 	u32 ret_payload[PAYLOAD_ARG_CNT];
500 	int ret;
501 
502 	if (!version)
503 		return -EINVAL;
504 
505 	/* Check is PM trustzone version already verified */
506 	if (pm_tz_version > 0) {
507 		*version = pm_tz_version;
508 		return 0;
509 	}
510 	ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, ret_payload, 0);
511 	*version = ret_payload[1];
512 
513 	return ret;
514 }
515 
516 /**
517  * get_set_conduit_method() - Choose SMC or HVC based communication
518  * @np:		Pointer to the device_node structure
519  *
520  * Use SMC or HVC-based functions to communicate with EL2/EL3.
521  *
522  * Return: Returns 0 on success or error code
523  */
get_set_conduit_method(struct device_node * np)524 static int get_set_conduit_method(struct device_node *np)
525 {
526 	const char *method;
527 
528 	if (of_property_read_string(np, "method", &method)) {
529 		pr_warn("%s missing \"method\" property\n", __func__);
530 		return -ENXIO;
531 	}
532 
533 	if (!strcmp("hvc", method)) {
534 		do_fw_call = do_fw_call_hvc;
535 	} else if (!strcmp("smc", method)) {
536 		do_fw_call = do_fw_call_smc;
537 	} else {
538 		pr_warn("%s Invalid \"method\" property: %s\n",
539 			__func__, method);
540 		return -EINVAL;
541 	}
542 
543 	return 0;
544 }
545 
546 /**
547  * zynqmp_pm_query_data() - Get query data from firmware
548  * @qdata:	Variable to the zynqmp_pm_query_data structure
549  * @out:	Returned output value
550  *
551  * Return: Returns status, either success or error+reason
552  */
zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata,u32 * out)553 int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
554 {
555 	int ret;
556 
557 	ret = zynqmp_pm_invoke_fn(PM_QUERY_DATA, out, 4, qdata.qid, qdata.arg1, qdata.arg2,
558 				  qdata.arg3);
559 
560 	/*
561 	 * For clock name query, all bytes in SMC response are clock name
562 	 * characters and return code is always success. For invalid clocks,
563 	 * clock name bytes would be zeros.
564 	 */
565 	return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : ret;
566 }
567 EXPORT_SYMBOL_GPL(zynqmp_pm_query_data);
568 
569 /**
570  * zynqmp_pm_clock_enable() - Enable the clock for given id
571  * @clock_id:	ID of the clock to be enabled
572  *
573  * This function is used by master to enable the clock
574  * including peripherals and PLL clocks.
575  *
576  * Return: Returns status, either success or error+reason
577  */
zynqmp_pm_clock_enable(u32 clock_id)578 int zynqmp_pm_clock_enable(u32 clock_id)
579 {
580 	return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, NULL, 1, clock_id);
581 }
582 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_enable);
583 
584 /**
585  * zynqmp_pm_clock_disable() - Disable the clock for given id
586  * @clock_id:	ID of the clock to be disable
587  *
588  * This function is used by master to disable the clock
589  * including peripherals and PLL clocks.
590  *
591  * Return: Returns status, either success or error+reason
592  */
zynqmp_pm_clock_disable(u32 clock_id)593 int zynqmp_pm_clock_disable(u32 clock_id)
594 {
595 	return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, NULL, 1, clock_id);
596 }
597 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_disable);
598 
599 /**
600  * zynqmp_pm_clock_getstate() - Get the clock state for given id
601  * @clock_id:	ID of the clock to be queried
602  * @state:	1/0 (Enabled/Disabled)
603  *
604  * This function is used by master to get the state of clock
605  * including peripherals and PLL clocks.
606  *
607  * Return: Returns status, either success or error+reason
608  */
zynqmp_pm_clock_getstate(u32 clock_id,u32 * state)609 int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
610 {
611 	u32 ret_payload[PAYLOAD_ARG_CNT];
612 	int ret;
613 
614 	ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, ret_payload, 1, clock_id);
615 	*state = ret_payload[1];
616 
617 	return ret;
618 }
619 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getstate);
620 
621 /**
622  * zynqmp_pm_clock_setdivider() - Set the clock divider for given id
623  * @clock_id:	ID of the clock
624  * @divider:	divider value
625  *
626  * This function is used by master to set divider for any clock
627  * to achieve desired rate.
628  *
629  * Return: Returns status, either success or error+reason
630  */
zynqmp_pm_clock_setdivider(u32 clock_id,u32 divider)631 int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
632 {
633 	return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, NULL, 2, clock_id, divider);
634 }
635 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setdivider);
636 
637 /**
638  * zynqmp_pm_clock_getdivider() - Get the clock divider for given id
639  * @clock_id:	ID of the clock
640  * @divider:	divider value
641  *
642  * This function is used by master to get divider values
643  * for any clock.
644  *
645  * Return: Returns status, either success or error+reason
646  */
zynqmp_pm_clock_getdivider(u32 clock_id,u32 * divider)647 int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
648 {
649 	u32 ret_payload[PAYLOAD_ARG_CNT];
650 	int ret;
651 
652 	ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, ret_payload, 1, clock_id);
653 	*divider = ret_payload[1];
654 
655 	return ret;
656 }
657 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getdivider);
658 
659 /**
660  * zynqmp_pm_clock_setparent() - Set the clock parent for given id
661  * @clock_id:	ID of the clock
662  * @parent_id:	parent id
663  *
664  * This function is used by master to set parent for any clock.
665  *
666  * Return: Returns status, either success or error+reason
667  */
zynqmp_pm_clock_setparent(u32 clock_id,u32 parent_id)668 int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
669 {
670 	return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, NULL, 2, clock_id, parent_id);
671 }
672 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setparent);
673 
674 /**
675  * zynqmp_pm_clock_getparent() - Get the clock parent for given id
676  * @clock_id:	ID of the clock
677  * @parent_id:	parent id
678  *
679  * This function is used by master to get parent index
680  * for any clock.
681  *
682  * Return: Returns status, either success or error+reason
683  */
zynqmp_pm_clock_getparent(u32 clock_id,u32 * parent_id)684 int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
685 {
686 	u32 ret_payload[PAYLOAD_ARG_CNT];
687 	int ret;
688 
689 	ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, ret_payload, 1, clock_id);
690 	*parent_id = ret_payload[1];
691 
692 	return ret;
693 }
694 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getparent);
695 
696 /**
697  * zynqmp_pm_set_pll_frac_mode() - PM API for set PLL mode
698  *
699  * @clk_id:	PLL clock ID
700  * @mode:	PLL mode (PLL_MODE_FRAC/PLL_MODE_INT)
701  *
702  * This function sets PLL mode
703  *
704  * Return: Returns status, either success or error+reason
705  */
zynqmp_pm_set_pll_frac_mode(u32 clk_id,u32 mode)706 int zynqmp_pm_set_pll_frac_mode(u32 clk_id, u32 mode)
707 {
708 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_PLL_FRAC_MODE, clk_id, mode);
709 }
710 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_mode);
711 
712 /**
713  * zynqmp_pm_get_pll_frac_mode() - PM API for get PLL mode
714  *
715  * @clk_id:	PLL clock ID
716  * @mode:	PLL mode
717  *
718  * This function return current PLL mode
719  *
720  * Return: Returns status, either success or error+reason
721  */
zynqmp_pm_get_pll_frac_mode(u32 clk_id,u32 * mode)722 int zynqmp_pm_get_pll_frac_mode(u32 clk_id, u32 *mode)
723 {
724 	return zynqmp_pm_invoke_fn(PM_IOCTL, mode, 3, 0, IOCTL_GET_PLL_FRAC_MODE, clk_id);
725 }
726 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_mode);
727 
728 /**
729  * zynqmp_pm_set_pll_frac_data() - PM API for setting pll fraction data
730  *
731  * @clk_id:	PLL clock ID
732  * @data:	fraction data
733  *
734  * This function sets fraction data.
735  * It is valid for fraction mode only.
736  *
737  * Return: Returns status, either success or error+reason
738  */
zynqmp_pm_set_pll_frac_data(u32 clk_id,u32 data)739 int zynqmp_pm_set_pll_frac_data(u32 clk_id, u32 data)
740 {
741 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_PLL_FRAC_DATA, clk_id, data);
742 }
743 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_data);
744 
745 /**
746  * zynqmp_pm_get_pll_frac_data() - PM API for getting pll fraction data
747  *
748  * @clk_id:	PLL clock ID
749  * @data:	fraction data
750  *
751  * This function returns fraction data value.
752  *
753  * Return: Returns status, either success or error+reason
754  */
zynqmp_pm_get_pll_frac_data(u32 clk_id,u32 * data)755 int zynqmp_pm_get_pll_frac_data(u32 clk_id, u32 *data)
756 {
757 	return zynqmp_pm_invoke_fn(PM_IOCTL, data, 3, 0, IOCTL_GET_PLL_FRAC_DATA, clk_id);
758 }
759 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_data);
760 
761 /**
762  * zynqmp_pm_set_sd_tapdelay() -  Set tap delay for the SD device
763  *
764  * @node_id:	Node ID of the device
765  * @type:	Type of tap delay to set (input/output)
766  * @value:	Value to set fot the tap delay
767  *
768  * This function sets input/output tap delay for the SD device.
769  *
770  * Return:	Returns status, either success or error+reason
771  */
zynqmp_pm_set_sd_tapdelay(u32 node_id,u32 type,u32 value)772 int zynqmp_pm_set_sd_tapdelay(u32 node_id, u32 type, u32 value)
773 {
774 	u32 reg = (type == PM_TAPDELAY_INPUT) ? SD_ITAPDLY : SD_OTAPDLYSEL;
775 	u32 mask = (node_id == NODE_SD_0) ? GENMASK(15, 0) : GENMASK(31, 16);
776 
777 	if (value) {
778 		return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, node_id, IOCTL_SET_SD_TAPDELAY, type,
779 					   value);
780 	}
781 
782 	/*
783 	 * Work around completely misdesigned firmware API on Xilinx ZynqMP.
784 	 * The IOCTL_SET_SD_TAPDELAY firmware call allows the caller to only
785 	 * ever set IOU_SLCR SD_ITAPDLY Register SD0_ITAPDLYENA/SD1_ITAPDLYENA
786 	 * bits, but there is no matching call to clear those bits. If those
787 	 * bits are not cleared, SDMMC tuning may fail.
788 	 *
789 	 * Luckily, there are PM_MMIO_READ/PM_MMIO_WRITE calls which seem to
790 	 * allow complete unrestricted access to all address space, including
791 	 * IOU_SLCR SD_ITAPDLY Register and all the other registers, access
792 	 * to which was supposed to be protected by the current firmware API.
793 	 *
794 	 * Use PM_MMIO_READ/PM_MMIO_WRITE to re-implement the missing counter
795 	 * part of IOCTL_SET_SD_TAPDELAY which clears SDx_ITAPDLYENA bits.
796 	 */
797 	return zynqmp_pm_invoke_fn(PM_MMIO_WRITE, NULL, 2, reg, mask);
798 }
799 EXPORT_SYMBOL_GPL(zynqmp_pm_set_sd_tapdelay);
800 
801 /**
802  * zynqmp_pm_sd_dll_reset() - Reset DLL logic
803  *
804  * @node_id:	Node ID of the device
805  * @type:	Reset type
806  *
807  * This function resets DLL logic for the SD device.
808  *
809  * Return:	Returns status, either success or error+reason
810  */
zynqmp_pm_sd_dll_reset(u32 node_id,u32 type)811 int zynqmp_pm_sd_dll_reset(u32 node_id, u32 type)
812 {
813 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, node_id, IOCTL_SD_DLL_RESET, type);
814 }
815 EXPORT_SYMBOL_GPL(zynqmp_pm_sd_dll_reset);
816 
817 /**
818  * zynqmp_pm_ospi_mux_select() - OSPI Mux selection
819  *
820  * @dev_id:	Device Id of the OSPI device.
821  * @select:	OSPI Mux select value.
822  *
823  * This function select the OSPI Mux.
824  *
825  * Return:	Returns status, either success or error+reason
826  */
zynqmp_pm_ospi_mux_select(u32 dev_id,u32 select)827 int zynqmp_pm_ospi_mux_select(u32 dev_id, u32 select)
828 {
829 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, dev_id, IOCTL_OSPI_MUX_SELECT, select);
830 }
831 EXPORT_SYMBOL_GPL(zynqmp_pm_ospi_mux_select);
832 
833 /**
834  * zynqmp_pm_write_ggs() - PM API for writing global general storage (ggs)
835  * @index:	GGS register index
836  * @value:	Register value to be written
837  *
838  * This function writes value to GGS register.
839  *
840  * Return:      Returns status, either success or error+reason
841  */
zynqmp_pm_write_ggs(u32 index,u32 value)842 int zynqmp_pm_write_ggs(u32 index, u32 value)
843 {
844 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_WRITE_GGS, index, value);
845 }
846 EXPORT_SYMBOL_GPL(zynqmp_pm_write_ggs);
847 
848 /**
849  * zynqmp_pm_read_ggs() - PM API for reading global general storage (ggs)
850  * @index:	GGS register index
851  * @value:	Register value to be written
852  *
853  * This function returns GGS register value.
854  *
855  * Return:	Returns status, either success or error+reason
856  */
zynqmp_pm_read_ggs(u32 index,u32 * value)857 int zynqmp_pm_read_ggs(u32 index, u32 *value)
858 {
859 	return zynqmp_pm_invoke_fn(PM_IOCTL, value, 3, 0, IOCTL_READ_GGS, index);
860 }
861 EXPORT_SYMBOL_GPL(zynqmp_pm_read_ggs);
862 
863 /**
864  * zynqmp_pm_write_pggs() - PM API for writing persistent global general
865  *			     storage (pggs)
866  * @index:	PGGS register index
867  * @value:	Register value to be written
868  *
869  * This function writes value to PGGS register.
870  *
871  * Return:	Returns status, either success or error+reason
872  */
zynqmp_pm_write_pggs(u32 index,u32 value)873 int zynqmp_pm_write_pggs(u32 index, u32 value)
874 {
875 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_WRITE_PGGS, index, value);
876 }
877 EXPORT_SYMBOL_GPL(zynqmp_pm_write_pggs);
878 
879 /**
880  * zynqmp_pm_read_pggs() - PM API for reading persistent global general
881  *			     storage (pggs)
882  * @index:	PGGS register index
883  * @value:	Register value to be written
884  *
885  * This function returns PGGS register value.
886  *
887  * Return:	Returns status, either success or error+reason
888  */
zynqmp_pm_read_pggs(u32 index,u32 * value)889 int zynqmp_pm_read_pggs(u32 index, u32 *value)
890 {
891 	return zynqmp_pm_invoke_fn(PM_IOCTL, value, 3, 0, IOCTL_READ_PGGS, index);
892 }
893 EXPORT_SYMBOL_GPL(zynqmp_pm_read_pggs);
894 
zynqmp_pm_set_tapdelay_bypass(u32 index,u32 value)895 int zynqmp_pm_set_tapdelay_bypass(u32 index, u32 value)
896 {
897 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_TAPDELAY_BYPASS, index, value);
898 }
899 EXPORT_SYMBOL_GPL(zynqmp_pm_set_tapdelay_bypass);
900 
901 /**
902  * zynqmp_pm_set_boot_health_status() - PM API for setting healthy boot status
903  * @value:	Status value to be written
904  *
905  * This function sets healthy bit value to indicate boot health status
906  * to firmware.
907  *
908  * Return:	Returns status, either success or error+reason
909  */
zynqmp_pm_set_boot_health_status(u32 value)910 int zynqmp_pm_set_boot_health_status(u32 value)
911 {
912 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, 0, IOCTL_SET_BOOT_HEALTH_STATUS, value);
913 }
914 
915 /**
916  * zynqmp_pm_reset_assert - Request setting of reset (1 - assert, 0 - release)
917  * @reset:		Reset to be configured
918  * @assert_flag:	Flag stating should reset be asserted (1) or
919  *			released (0)
920  *
921  * Return: Returns status, either success or error+reason
922  */
zynqmp_pm_reset_assert(const enum zynqmp_pm_reset reset,const enum zynqmp_pm_reset_action assert_flag)923 int zynqmp_pm_reset_assert(const enum zynqmp_pm_reset reset,
924 			   const enum zynqmp_pm_reset_action assert_flag)
925 {
926 	return zynqmp_pm_invoke_fn(PM_RESET_ASSERT, NULL, 2, reset, assert_flag);
927 }
928 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_assert);
929 
930 /**
931  * zynqmp_pm_reset_get_status - Get status of the reset
932  * @reset:      Reset whose status should be returned
933  * @status:     Returned status
934  *
935  * Return: Returns status, either success or error+reason
936  */
zynqmp_pm_reset_get_status(const enum zynqmp_pm_reset reset,u32 * status)937 int zynqmp_pm_reset_get_status(const enum zynqmp_pm_reset reset, u32 *status)
938 {
939 	u32 ret_payload[PAYLOAD_ARG_CNT];
940 	int ret;
941 
942 	if (!status)
943 		return -EINVAL;
944 
945 	ret = zynqmp_pm_invoke_fn(PM_RESET_GET_STATUS, ret_payload, 1, reset);
946 	*status = ret_payload[1];
947 
948 	return ret;
949 }
950 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_get_status);
951 
952 /**
953  * zynqmp_pm_fpga_load - Perform the fpga load
954  * @address:	Address to write to
955  * @size:	pl bitstream size
956  * @flags:	Bitstream type
957  *	-XILINX_ZYNQMP_PM_FPGA_FULL:  FPGA full reconfiguration
958  *	-XILINX_ZYNQMP_PM_FPGA_PARTIAL: FPGA partial reconfiguration
959  *
960  * This function provides access to pmufw. To transfer
961  * the required bitstream into PL.
962  *
963  * Return: Returns status, either success or error+reason
964  */
zynqmp_pm_fpga_load(const u64 address,const u32 size,const u32 flags)965 int zynqmp_pm_fpga_load(const u64 address, const u32 size, const u32 flags)
966 {
967 	u32 ret_payload[PAYLOAD_ARG_CNT];
968 	int ret;
969 
970 	ret = zynqmp_pm_invoke_fn(PM_FPGA_LOAD, ret_payload, 4, lower_32_bits(address),
971 				  upper_32_bits(address), size, flags);
972 	if (ret_payload[0])
973 		return -ret_payload[0];
974 
975 	return ret;
976 }
977 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_load);
978 
979 /**
980  * zynqmp_pm_fpga_get_status - Read value from PCAP status register
981  * @value: Value to read
982  *
983  * This function provides access to the pmufw to get the PCAP
984  * status
985  *
986  * Return: Returns status, either success or error+reason
987  */
zynqmp_pm_fpga_get_status(u32 * value)988 int zynqmp_pm_fpga_get_status(u32 *value)
989 {
990 	u32 ret_payload[PAYLOAD_ARG_CNT];
991 	int ret;
992 
993 	if (!value)
994 		return -EINVAL;
995 
996 	ret = zynqmp_pm_invoke_fn(PM_FPGA_GET_STATUS, ret_payload, 0);
997 	*value = ret_payload[1];
998 
999 	return ret;
1000 }
1001 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_get_status);
1002 
1003 /**
1004  * zynqmp_pm_fpga_get_config_status - Get the FPGA configuration status.
1005  * @value: Buffer to store FPGA configuration status.
1006  *
1007  * This function provides access to the pmufw to get the FPGA configuration
1008  * status
1009  *
1010  * Return: 0 on success, a negative value on error
1011  */
zynqmp_pm_fpga_get_config_status(u32 * value)1012 int zynqmp_pm_fpga_get_config_status(u32 *value)
1013 {
1014 	u32 ret_payload[PAYLOAD_ARG_CNT];
1015 	int ret;
1016 
1017 	if (!value)
1018 		return -EINVAL;
1019 
1020 	ret = zynqmp_pm_invoke_fn(PM_FPGA_READ, ret_payload, 4,
1021 				  XILINX_ZYNQMP_PM_FPGA_CONFIG_STAT_OFFSET, 0, 0,
1022 				  XILINX_ZYNQMP_PM_FPGA_READ_CONFIG_REG);
1023 
1024 	*value = ret_payload[1];
1025 
1026 	return ret;
1027 }
1028 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_get_config_status);
1029 
1030 /**
1031  * zynqmp_pm_pinctrl_request - Request Pin from firmware
1032  * @pin: Pin number to request
1033  *
1034  * This function requests pin from firmware.
1035  *
1036  * Return: Returns status, either success or error+reason.
1037  */
zynqmp_pm_pinctrl_request(const u32 pin)1038 int zynqmp_pm_pinctrl_request(const u32 pin)
1039 {
1040 	return zynqmp_pm_invoke_fn(PM_PINCTRL_REQUEST, NULL, 1, pin);
1041 }
1042 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_request);
1043 
1044 /**
1045  * zynqmp_pm_pinctrl_release - Inform firmware that Pin control is released
1046  * @pin: Pin number to release
1047  *
1048  * This function release pin from firmware.
1049  *
1050  * Return: Returns status, either success or error+reason.
1051  */
zynqmp_pm_pinctrl_release(const u32 pin)1052 int zynqmp_pm_pinctrl_release(const u32 pin)
1053 {
1054 	return zynqmp_pm_invoke_fn(PM_PINCTRL_RELEASE, NULL, 1, pin);
1055 }
1056 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_release);
1057 
1058 /**
1059  * zynqmp_pm_pinctrl_set_function - Set requested function for the pin
1060  * @pin: Pin number
1061  * @id: Function ID to set
1062  *
1063  * This function sets requested function for the given pin.
1064  *
1065  * Return: Returns status, either success or error+reason.
1066  */
zynqmp_pm_pinctrl_set_function(const u32 pin,const u32 id)1067 int zynqmp_pm_pinctrl_set_function(const u32 pin, const u32 id)
1068 {
1069 	return zynqmp_pm_invoke_fn(PM_PINCTRL_SET_FUNCTION, NULL, 2, pin, id);
1070 }
1071 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_function);
1072 
1073 /**
1074  * zynqmp_pm_pinctrl_get_config - Get configuration parameter for the pin
1075  * @pin: Pin number
1076  * @param: Parameter to get
1077  * @value: Buffer to store parameter value
1078  *
1079  * This function gets requested configuration parameter for the given pin.
1080  *
1081  * Return: Returns status, either success or error+reason.
1082  */
zynqmp_pm_pinctrl_get_config(const u32 pin,const u32 param,u32 * value)1083 int zynqmp_pm_pinctrl_get_config(const u32 pin, const u32 param,
1084 				 u32 *value)
1085 {
1086 	u32 ret_payload[PAYLOAD_ARG_CNT];
1087 	int ret;
1088 
1089 	if (!value)
1090 		return -EINVAL;
1091 
1092 	ret = zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_GET, ret_payload, 2, pin, param);
1093 	*value = ret_payload[1];
1094 
1095 	return ret;
1096 }
1097 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_get_config);
1098 
1099 /**
1100  * zynqmp_pm_pinctrl_set_config - Set configuration parameter for the pin
1101  * @pin: Pin number
1102  * @param: Parameter to set
1103  * @value: Parameter value to set
1104  *
1105  * This function sets requested configuration parameter for the given pin.
1106  *
1107  * Return: Returns status, either success or error+reason.
1108  */
zynqmp_pm_pinctrl_set_config(const u32 pin,const u32 param,u32 value)1109 int zynqmp_pm_pinctrl_set_config(const u32 pin, const u32 param,
1110 				 u32 value)
1111 {
1112 	int ret;
1113 
1114 	if (pm_family_code == ZYNQMP_FAMILY_CODE &&
1115 	    param == PM_PINCTRL_CONFIG_TRI_STATE) {
1116 		ret = zynqmp_pm_feature(PM_PINCTRL_CONFIG_PARAM_SET);
1117 		if (ret < PM_PINCTRL_PARAM_SET_VERSION)
1118 			return -EOPNOTSUPP;
1119 	}
1120 
1121 	return zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_SET, NULL, 3, pin, param, value);
1122 }
1123 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_config);
1124 
1125 /**
1126  * zynqmp_pm_bootmode_read() - PM Config API for read bootpin status
1127  * @ps_mode: Returned output value of ps_mode
1128  *
1129  * This API function is to be used for notify the power management controller
1130  * to read bootpin status.
1131  *
1132  * Return: status, either success or error+reason
1133  */
zynqmp_pm_bootmode_read(u32 * ps_mode)1134 unsigned int zynqmp_pm_bootmode_read(u32 *ps_mode)
1135 {
1136 	unsigned int ret;
1137 	u32 ret_payload[PAYLOAD_ARG_CNT];
1138 
1139 	ret = zynqmp_pm_invoke_fn(PM_MMIO_READ, ret_payload, 1, CRL_APB_BOOT_PIN_CTRL);
1140 
1141 	*ps_mode = ret_payload[1];
1142 
1143 	return ret;
1144 }
1145 EXPORT_SYMBOL_GPL(zynqmp_pm_bootmode_read);
1146 
1147 /**
1148  * zynqmp_pm_bootmode_write() - PM Config API for Configure bootpin
1149  * @ps_mode: Value to be written to the bootpin ctrl register
1150  *
1151  * This API function is to be used for notify the power management controller
1152  * to configure bootpin.
1153  *
1154  * Return: Returns status, either success or error+reason
1155  */
zynqmp_pm_bootmode_write(u32 ps_mode)1156 int zynqmp_pm_bootmode_write(u32 ps_mode)
1157 {
1158 	return zynqmp_pm_invoke_fn(PM_MMIO_WRITE, NULL, 3, CRL_APB_BOOT_PIN_CTRL,
1159 				   CRL_APB_BOOTPIN_CTRL_MASK, ps_mode);
1160 }
1161 EXPORT_SYMBOL_GPL(zynqmp_pm_bootmode_write);
1162 
1163 /**
1164  * zynqmp_pm_init_finalize() - PM call to inform firmware that the caller
1165  *			       master has initialized its own power management
1166  *
1167  * Return: Returns status, either success or error+reason
1168  *
1169  * This API function is to be used for notify the power management controller
1170  * about the completed power management initialization.
1171  */
zynqmp_pm_init_finalize(void)1172 int zynqmp_pm_init_finalize(void)
1173 {
1174 	return zynqmp_pm_invoke_fn(PM_PM_INIT_FINALIZE, NULL, 0);
1175 }
1176 EXPORT_SYMBOL_GPL(zynqmp_pm_init_finalize);
1177 
1178 /**
1179  * zynqmp_pm_set_suspend_mode()	- Set system suspend mode
1180  * @mode:	Mode to set for system suspend
1181  *
1182  * This API function is used to set mode of system suspend.
1183  *
1184  * Return: Returns status, either success or error+reason
1185  */
zynqmp_pm_set_suspend_mode(u32 mode)1186 int zynqmp_pm_set_suspend_mode(u32 mode)
1187 {
1188 	return zynqmp_pm_invoke_fn(PM_SET_SUSPEND_MODE, NULL, 1, mode);
1189 }
1190 EXPORT_SYMBOL_GPL(zynqmp_pm_set_suspend_mode);
1191 
1192 /**
1193  * zynqmp_pm_request_node() - Request a node with specific capabilities
1194  * @node:		Node ID of the slave
1195  * @capabilities:	Requested capabilities of the slave
1196  * @qos:		Quality of service (not supported)
1197  * @ack:		Flag to specify whether acknowledge is requested
1198  *
1199  * This function is used by master to request particular node from firmware.
1200  * Every master must request node before using it.
1201  *
1202  * Return: Returns status, either success or error+reason
1203  */
zynqmp_pm_request_node(const u32 node,const u32 capabilities,const u32 qos,const enum zynqmp_pm_request_ack ack)1204 int zynqmp_pm_request_node(const u32 node, const u32 capabilities,
1205 			   const u32 qos, const enum zynqmp_pm_request_ack ack)
1206 {
1207 	return zynqmp_pm_invoke_fn(PM_REQUEST_NODE, NULL, 4, node, capabilities, qos, ack);
1208 }
1209 EXPORT_SYMBOL_GPL(zynqmp_pm_request_node);
1210 
1211 /**
1212  * zynqmp_pm_release_node() - Release a node
1213  * @node:	Node ID of the slave
1214  *
1215  * This function is used by master to inform firmware that master
1216  * has released node. Once released, master must not use that node
1217  * without re-request.
1218  *
1219  * Return: Returns status, either success or error+reason
1220  */
zynqmp_pm_release_node(const u32 node)1221 int zynqmp_pm_release_node(const u32 node)
1222 {
1223 	return zynqmp_pm_invoke_fn(PM_RELEASE_NODE, NULL, 1, node);
1224 }
1225 EXPORT_SYMBOL_GPL(zynqmp_pm_release_node);
1226 
1227 /**
1228  * zynqmp_pm_get_rpu_mode() - Get RPU mode
1229  * @node_id:	Node ID of the device
1230  * @rpu_mode:	return by reference value
1231  *		either split or lockstep
1232  *
1233  * Return:	return 0 on success or error+reason.
1234  *		if success, then  rpu_mode will be set
1235  *		to current rpu mode.
1236  */
zynqmp_pm_get_rpu_mode(u32 node_id,enum rpu_oper_mode * rpu_mode)1237 int zynqmp_pm_get_rpu_mode(u32 node_id, enum rpu_oper_mode *rpu_mode)
1238 {
1239 	u32 ret_payload[PAYLOAD_ARG_CNT];
1240 	int ret;
1241 
1242 	ret = zynqmp_pm_invoke_fn(PM_IOCTL, ret_payload, 2, node_id, IOCTL_GET_RPU_OPER_MODE);
1243 
1244 	/* only set rpu_mode if no error */
1245 	if (ret == XST_PM_SUCCESS)
1246 		*rpu_mode = ret_payload[0];
1247 
1248 	return ret;
1249 }
1250 EXPORT_SYMBOL_GPL(zynqmp_pm_get_rpu_mode);
1251 
1252 /**
1253  * zynqmp_pm_set_rpu_mode() - Set RPU mode
1254  * @node_id:	Node ID of the device
1255  * @rpu_mode:	Argument 1 to requested IOCTL call. either split or lockstep
1256  *
1257  *		This function is used to set RPU mode to split or
1258  *		lockstep
1259  *
1260  * Return:	Returns status, either success or error+reason
1261  */
zynqmp_pm_set_rpu_mode(u32 node_id,enum rpu_oper_mode rpu_mode)1262 int zynqmp_pm_set_rpu_mode(u32 node_id, enum rpu_oper_mode rpu_mode)
1263 {
1264 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, node_id, IOCTL_SET_RPU_OPER_MODE,
1265 				   (u32)rpu_mode);
1266 }
1267 EXPORT_SYMBOL_GPL(zynqmp_pm_set_rpu_mode);
1268 
1269 /**
1270  * zynqmp_pm_set_tcm_config - configure TCM
1271  * @node_id:	Firmware specific TCM subsystem ID
1272  * @tcm_mode:	Argument 1 to requested IOCTL call
1273  *              either PM_RPU_TCM_COMB or PM_RPU_TCM_SPLIT
1274  *
1275  * This function is used to set RPU mode to split or combined
1276  *
1277  * Return: status: 0 for success, else failure
1278  */
zynqmp_pm_set_tcm_config(u32 node_id,enum rpu_tcm_comb tcm_mode)1279 int zynqmp_pm_set_tcm_config(u32 node_id, enum rpu_tcm_comb tcm_mode)
1280 {
1281 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 3, node_id, IOCTL_TCM_COMB_CONFIG,
1282 				   (u32)tcm_mode);
1283 }
1284 EXPORT_SYMBOL_GPL(zynqmp_pm_set_tcm_config);
1285 
1286 /**
1287  * zynqmp_pm_force_pwrdwn - PM call to request for another PU or subsystem to
1288  *             be powered down forcefully
1289  * @node:  Node ID of the targeted PU or subsystem
1290  * @ack:   Flag to specify whether acknowledge is requested
1291  *
1292  * Return: status, either success or error+reason
1293  */
zynqmp_pm_force_pwrdwn(const u32 node,const enum zynqmp_pm_request_ack ack)1294 int zynqmp_pm_force_pwrdwn(const u32 node,
1295 			   const enum zynqmp_pm_request_ack ack)
1296 {
1297 	return zynqmp_pm_invoke_fn(PM_FORCE_POWERDOWN, NULL, 2, node, ack);
1298 }
1299 EXPORT_SYMBOL_GPL(zynqmp_pm_force_pwrdwn);
1300 
1301 /**
1302  * zynqmp_pm_request_wake - PM call to wake up selected master or subsystem
1303  * @node:  Node ID of the master or subsystem
1304  * @set_addr:  Specifies whether the address argument is relevant
1305  * @address:   Address from which to resume when woken up
1306  * @ack:   Flag to specify whether acknowledge requested
1307  *
1308  * Return: status, either success or error+reason
1309  */
zynqmp_pm_request_wake(const u32 node,const bool set_addr,const u64 address,const enum zynqmp_pm_request_ack ack)1310 int zynqmp_pm_request_wake(const u32 node,
1311 			   const bool set_addr,
1312 			   const u64 address,
1313 			   const enum zynqmp_pm_request_ack ack)
1314 {
1315 	/* set_addr flag is encoded into 1st bit of address */
1316 	return zynqmp_pm_invoke_fn(PM_REQUEST_WAKEUP, NULL, 4, node, address | set_addr,
1317 				   address >> 32, ack);
1318 }
1319 EXPORT_SYMBOL_GPL(zynqmp_pm_request_wake);
1320 
1321 /**
1322  * zynqmp_pm_set_requirement() - PM call to set requirement for PM slaves
1323  * @node:		Node ID of the slave
1324  * @capabilities:	Requested capabilities of the slave
1325  * @qos:		Quality of service (not supported)
1326  * @ack:		Flag to specify whether acknowledge is requested
1327  *
1328  * This API function is to be used for slaves a PU already has requested
1329  * to change its capabilities.
1330  *
1331  * Return: Returns status, either success or error+reason
1332  */
zynqmp_pm_set_requirement(const u32 node,const u32 capabilities,const u32 qos,const enum zynqmp_pm_request_ack ack)1333 int zynqmp_pm_set_requirement(const u32 node, const u32 capabilities,
1334 			      const u32 qos,
1335 			      const enum zynqmp_pm_request_ack ack)
1336 {
1337 	return zynqmp_pm_invoke_fn(PM_SET_REQUIREMENT, NULL, 4, node, capabilities, qos, ack);
1338 }
1339 EXPORT_SYMBOL_GPL(zynqmp_pm_set_requirement);
1340 
1341 /**
1342  * zynqmp_pm_load_pdi - Load and process PDI
1343  * @src:	Source device where PDI is located
1344  * @address:	PDI src address
1345  *
1346  * This function provides support to load PDI from linux
1347  *
1348  * Return: Returns status, either success or error+reason
1349  */
zynqmp_pm_load_pdi(const u32 src,const u64 address)1350 int zynqmp_pm_load_pdi(const u32 src, const u64 address)
1351 {
1352 	return zynqmp_pm_invoke_fn(PM_LOAD_PDI, NULL, 3, src, lower_32_bits(address),
1353 				   upper_32_bits(address));
1354 }
1355 EXPORT_SYMBOL_GPL(zynqmp_pm_load_pdi);
1356 
1357 /**
1358  * zynqmp_pm_aes_engine - Access AES hardware to encrypt/decrypt the data using
1359  * AES-GCM core.
1360  * @address:	Address of the AesParams structure.
1361  * @out:	Returned output value
1362  *
1363  * Return:	Returns status, either success or error code.
1364  */
zynqmp_pm_aes_engine(const u64 address,u32 * out)1365 int zynqmp_pm_aes_engine(const u64 address, u32 *out)
1366 {
1367 	u32 ret_payload[PAYLOAD_ARG_CNT];
1368 	int ret;
1369 
1370 	if (!out)
1371 		return -EINVAL;
1372 
1373 	ret = zynqmp_pm_invoke_fn(PM_SECURE_AES, ret_payload, 2, upper_32_bits(address),
1374 				  lower_32_bits(address));
1375 	*out = ret_payload[1];
1376 
1377 	return ret;
1378 }
1379 EXPORT_SYMBOL_GPL(zynqmp_pm_aes_engine);
1380 
1381 /**
1382  * zynqmp_pm_efuse_access - Provides access to efuse memory.
1383  * @address:	Address of the efuse params structure
1384  * @out:		Returned output value
1385  *
1386  * Return:	Returns status, either success or error code.
1387  */
zynqmp_pm_efuse_access(const u64 address,u32 * out)1388 int zynqmp_pm_efuse_access(const u64 address, u32 *out)
1389 {
1390 	u32 ret_payload[PAYLOAD_ARG_CNT];
1391 	int ret;
1392 
1393 	if (!out)
1394 		return -EINVAL;
1395 
1396 	ret = zynqmp_pm_invoke_fn(PM_EFUSE_ACCESS, ret_payload, 2,
1397 				  upper_32_bits(address),
1398 				  lower_32_bits(address));
1399 	*out = ret_payload[1];
1400 
1401 	return ret;
1402 }
1403 EXPORT_SYMBOL_GPL(zynqmp_pm_efuse_access);
1404 
1405 /**
1406  * zynqmp_pm_sha_hash - Access the SHA engine to calculate the hash
1407  * @address:	Address of the data/ Address of output buffer where
1408  *		hash should be stored.
1409  * @size:	Size of the data.
1410  * @flags:
1411  *	BIT(0) - for initializing csudma driver and SHA3(Here address
1412  *		 and size inputs can be NULL).
1413  *	BIT(1) - to call Sha3_Update API which can be called multiple
1414  *		 times when data is not contiguous.
1415  *	BIT(2) - to get final hash of the whole updated data.
1416  *		 Hash will be overwritten at provided address with
1417  *		 48 bytes.
1418  *
1419  * Return:	Returns status, either success or error code.
1420  */
zynqmp_pm_sha_hash(const u64 address,const u32 size,const u32 flags)1421 int zynqmp_pm_sha_hash(const u64 address, const u32 size, const u32 flags)
1422 {
1423 	u32 lower_addr = lower_32_bits(address);
1424 	u32 upper_addr = upper_32_bits(address);
1425 
1426 	return zynqmp_pm_invoke_fn(PM_SECURE_SHA, NULL, 4, upper_addr, lower_addr, size, flags);
1427 }
1428 EXPORT_SYMBOL_GPL(zynqmp_pm_sha_hash);
1429 
1430 /**
1431  * zynqmp_pm_register_notifier() - PM API for register a subsystem
1432  *                                to be notified about specific
1433  *                                event/error.
1434  * @node:	Node ID to which the event is related.
1435  * @event:	Event Mask of Error events for which wants to get notified.
1436  * @wake:	Wake subsystem upon capturing the event if value 1
1437  * @enable:	Enable the registration for value 1, disable for value 0
1438  *
1439  * This function is used to register/un-register for particular node-event
1440  * combination in firmware.
1441  *
1442  * Return: Returns status, either success or error+reason
1443  */
1444 
zynqmp_pm_register_notifier(const u32 node,const u32 event,const u32 wake,const u32 enable)1445 int zynqmp_pm_register_notifier(const u32 node, const u32 event,
1446 				const u32 wake, const u32 enable)
1447 {
1448 	return zynqmp_pm_invoke_fn(PM_REGISTER_NOTIFIER, NULL, 4, node, event, wake, enable);
1449 }
1450 EXPORT_SYMBOL_GPL(zynqmp_pm_register_notifier);
1451 
1452 /**
1453  * zynqmp_pm_system_shutdown - PM call to request a system shutdown or restart
1454  * @type:	Shutdown or restart? 0 for shutdown, 1 for restart
1455  * @subtype:	Specifies which system should be restarted or shut down
1456  *
1457  * Return:	Returns status, either success or error+reason
1458  */
zynqmp_pm_system_shutdown(const u32 type,const u32 subtype)1459 int zynqmp_pm_system_shutdown(const u32 type, const u32 subtype)
1460 {
1461 	return zynqmp_pm_invoke_fn(PM_SYSTEM_SHUTDOWN, NULL, 2, type, subtype);
1462 }
1463 
1464 /**
1465  * zynqmp_pm_set_feature_config - PM call to request IOCTL for feature config
1466  * @id:         The config ID of the feature to be configured
1467  * @value:      The config value of the feature to be configured
1468  *
1469  * Return:      Returns 0 on success or error value on failure.
1470  */
zynqmp_pm_set_feature_config(enum pm_feature_config_id id,u32 value)1471 int zynqmp_pm_set_feature_config(enum pm_feature_config_id id, u32 value)
1472 {
1473 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, 0, IOCTL_SET_FEATURE_CONFIG, id, value);
1474 }
1475 
1476 /**
1477  * zynqmp_pm_get_feature_config - PM call to get value of configured feature
1478  * @id:         The config id of the feature to be queried
1479  * @payload:    Returned value array
1480  *
1481  * Return:      Returns 0 on success or error value on failure.
1482  */
zynqmp_pm_get_feature_config(enum pm_feature_config_id id,u32 * payload)1483 int zynqmp_pm_get_feature_config(enum pm_feature_config_id id,
1484 				 u32 *payload)
1485 {
1486 	return zynqmp_pm_invoke_fn(PM_IOCTL, payload, 3, 0, IOCTL_GET_FEATURE_CONFIG, id);
1487 }
1488 
1489 /**
1490  * zynqmp_pm_set_sd_config - PM call to set value of SD config registers
1491  * @node:	SD node ID
1492  * @config:	The config type of SD registers
1493  * @value:	Value to be set
1494  *
1495  * Return:	Returns 0 on success or error value on failure.
1496  */
zynqmp_pm_set_sd_config(u32 node,enum pm_sd_config_type config,u32 value)1497 int zynqmp_pm_set_sd_config(u32 node, enum pm_sd_config_type config, u32 value)
1498 {
1499 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, node, IOCTL_SET_SD_CONFIG, config, value);
1500 }
1501 EXPORT_SYMBOL_GPL(zynqmp_pm_set_sd_config);
1502 
1503 /**
1504  * zynqmp_pm_set_gem_config - PM call to set value of GEM config registers
1505  * @node:	GEM node ID
1506  * @config:	The config type of GEM registers
1507  * @value:	Value to be set
1508  *
1509  * Return:	Returns 0 on success or error value on failure.
1510  */
zynqmp_pm_set_gem_config(u32 node,enum pm_gem_config_type config,u32 value)1511 int zynqmp_pm_set_gem_config(u32 node, enum pm_gem_config_type config,
1512 			     u32 value)
1513 {
1514 	return zynqmp_pm_invoke_fn(PM_IOCTL, NULL, 4, node, IOCTL_SET_GEM_CONFIG, config, value);
1515 }
1516 EXPORT_SYMBOL_GPL(zynqmp_pm_set_gem_config);
1517 
1518 /**
1519  * struct zynqmp_pm_shutdown_scope - Struct for shutdown scope
1520  * @subtype:	Shutdown subtype
1521  * @name:	Matching string for scope argument
1522  *
1523  * This struct encapsulates mapping between shutdown scope ID and string.
1524  */
1525 struct zynqmp_pm_shutdown_scope {
1526 	const enum zynqmp_pm_shutdown_subtype subtype;
1527 	const char *name;
1528 };
1529 
1530 static struct zynqmp_pm_shutdown_scope shutdown_scopes[] = {
1531 	[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM] = {
1532 		.subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM,
1533 		.name = "subsystem",
1534 	},
1535 	[ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY] = {
1536 		.subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY,
1537 		.name = "ps_only",
1538 	},
1539 	[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM] = {
1540 		.subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM,
1541 		.name = "system",
1542 	},
1543 };
1544 
1545 static struct zynqmp_pm_shutdown_scope *selected_scope =
1546 		&shutdown_scopes[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM];
1547 
1548 /**
1549  * zynqmp_pm_is_shutdown_scope_valid - Check if shutdown scope string is valid
1550  * @scope_string:	Shutdown scope string
1551  *
1552  * Return:		Return pointer to matching shutdown scope struct from
1553  *			array of available options in system if string is valid,
1554  *			otherwise returns NULL.
1555  */
1556 static struct zynqmp_pm_shutdown_scope*
zynqmp_pm_is_shutdown_scope_valid(const char * scope_string)1557 		zynqmp_pm_is_shutdown_scope_valid(const char *scope_string)
1558 {
1559 	int count;
1560 
1561 	for (count = 0; count < ARRAY_SIZE(shutdown_scopes); count++)
1562 		if (sysfs_streq(scope_string, shutdown_scopes[count].name))
1563 			return &shutdown_scopes[count];
1564 
1565 	return NULL;
1566 }
1567 
shutdown_scope_show(struct device * device,struct device_attribute * attr,char * buf)1568 static ssize_t shutdown_scope_show(struct device *device,
1569 				   struct device_attribute *attr,
1570 				   char *buf)
1571 {
1572 	int i;
1573 
1574 	for (i = 0; i < ARRAY_SIZE(shutdown_scopes); i++) {
1575 		if (&shutdown_scopes[i] == selected_scope) {
1576 			strcat(buf, "[");
1577 			strcat(buf, shutdown_scopes[i].name);
1578 			strcat(buf, "]");
1579 		} else {
1580 			strcat(buf, shutdown_scopes[i].name);
1581 		}
1582 		strcat(buf, " ");
1583 	}
1584 	strcat(buf, "\n");
1585 
1586 	return strlen(buf);
1587 }
1588 
shutdown_scope_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1589 static ssize_t shutdown_scope_store(struct device *device,
1590 				    struct device_attribute *attr,
1591 				    const char *buf, size_t count)
1592 {
1593 	int ret;
1594 	struct zynqmp_pm_shutdown_scope *scope;
1595 
1596 	scope = zynqmp_pm_is_shutdown_scope_valid(buf);
1597 	if (!scope)
1598 		return -EINVAL;
1599 
1600 	ret = zynqmp_pm_system_shutdown(ZYNQMP_PM_SHUTDOWN_TYPE_SETSCOPE_ONLY,
1601 					scope->subtype);
1602 	if (ret) {
1603 		pr_err("unable to set shutdown scope %s\n", buf);
1604 		return ret;
1605 	}
1606 
1607 	selected_scope = scope;
1608 
1609 	return count;
1610 }
1611 
1612 static DEVICE_ATTR_RW(shutdown_scope);
1613 
health_status_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1614 static ssize_t health_status_store(struct device *device,
1615 				   struct device_attribute *attr,
1616 				   const char *buf, size_t count)
1617 {
1618 	int ret;
1619 	unsigned int value;
1620 
1621 	ret = kstrtouint(buf, 10, &value);
1622 	if (ret)
1623 		return ret;
1624 
1625 	ret = zynqmp_pm_set_boot_health_status(value);
1626 	if (ret) {
1627 		dev_err(device, "unable to set healthy bit value to %u\n",
1628 			value);
1629 		return ret;
1630 	}
1631 
1632 	return count;
1633 }
1634 
1635 static DEVICE_ATTR_WO(health_status);
1636 
ggs_show(struct device * device,struct device_attribute * attr,char * buf,u32 reg)1637 static ssize_t ggs_show(struct device *device,
1638 			struct device_attribute *attr,
1639 			char *buf,
1640 			u32 reg)
1641 {
1642 	int ret;
1643 	u32 ret_payload[PAYLOAD_ARG_CNT];
1644 
1645 	ret = zynqmp_pm_read_ggs(reg, ret_payload);
1646 	if (ret)
1647 		return ret;
1648 
1649 	return sprintf(buf, "0x%x\n", ret_payload[1]);
1650 }
1651 
ggs_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count,u32 reg)1652 static ssize_t ggs_store(struct device *device,
1653 			 struct device_attribute *attr,
1654 			 const char *buf, size_t count,
1655 			 u32 reg)
1656 {
1657 	long value;
1658 	int ret;
1659 
1660 	if (reg >= GSS_NUM_REGS)
1661 		return -EINVAL;
1662 
1663 	ret = kstrtol(buf, 16, &value);
1664 	if (ret) {
1665 		count = -EFAULT;
1666 		goto err;
1667 	}
1668 
1669 	ret = zynqmp_pm_write_ggs(reg, value);
1670 	if (ret)
1671 		count = -EFAULT;
1672 err:
1673 	return count;
1674 }
1675 
1676 /* GGS register show functions */
1677 #define GGS0_SHOW(N)						\
1678 	ssize_t ggs##N##_show(struct device *device,		\
1679 			      struct device_attribute *attr,	\
1680 			      char *buf)			\
1681 	{							\
1682 		return ggs_show(device, attr, buf, N);		\
1683 	}
1684 
1685 static GGS0_SHOW(0);
1686 static GGS0_SHOW(1);
1687 static GGS0_SHOW(2);
1688 static GGS0_SHOW(3);
1689 
1690 /* GGS register store function */
1691 #define GGS0_STORE(N)						\
1692 	ssize_t ggs##N##_store(struct device *device,		\
1693 			       struct device_attribute *attr,	\
1694 			       const char *buf,			\
1695 			       size_t count)			\
1696 	{							\
1697 		return ggs_store(device, attr, buf, count, N);	\
1698 	}
1699 
1700 static GGS0_STORE(0);
1701 static GGS0_STORE(1);
1702 static GGS0_STORE(2);
1703 static GGS0_STORE(3);
1704 
pggs_show(struct device * device,struct device_attribute * attr,char * buf,u32 reg)1705 static ssize_t pggs_show(struct device *device,
1706 			 struct device_attribute *attr,
1707 			 char *buf,
1708 			 u32 reg)
1709 {
1710 	int ret;
1711 	u32 ret_payload[PAYLOAD_ARG_CNT];
1712 
1713 	ret = zynqmp_pm_read_pggs(reg, ret_payload);
1714 	if (ret)
1715 		return ret;
1716 
1717 	return sprintf(buf, "0x%x\n", ret_payload[1]);
1718 }
1719 
pggs_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count,u32 reg)1720 static ssize_t pggs_store(struct device *device,
1721 			  struct device_attribute *attr,
1722 			  const char *buf, size_t count,
1723 			  u32 reg)
1724 {
1725 	long value;
1726 	int ret;
1727 
1728 	if (reg >= GSS_NUM_REGS)
1729 		return -EINVAL;
1730 
1731 	ret = kstrtol(buf, 16, &value);
1732 	if (ret) {
1733 		count = -EFAULT;
1734 		goto err;
1735 	}
1736 
1737 	ret = zynqmp_pm_write_pggs(reg, value);
1738 	if (ret)
1739 		count = -EFAULT;
1740 
1741 err:
1742 	return count;
1743 }
1744 
1745 #define PGGS0_SHOW(N)						\
1746 	ssize_t pggs##N##_show(struct device *device,		\
1747 			       struct device_attribute *attr,	\
1748 			       char *buf)			\
1749 	{							\
1750 		return pggs_show(device, attr, buf, N);		\
1751 	}
1752 
1753 #define PGGS0_STORE(N)						\
1754 	ssize_t pggs##N##_store(struct device *device,		\
1755 				struct device_attribute *attr,	\
1756 				const char *buf,		\
1757 				size_t count)			\
1758 	{							\
1759 		return pggs_store(device, attr, buf, count, N);	\
1760 	}
1761 
1762 /* PGGS register show functions */
1763 static PGGS0_SHOW(0);
1764 static PGGS0_SHOW(1);
1765 static PGGS0_SHOW(2);
1766 static PGGS0_SHOW(3);
1767 
1768 /* PGGS register store functions */
1769 static PGGS0_STORE(0);
1770 static PGGS0_STORE(1);
1771 static PGGS0_STORE(2);
1772 static PGGS0_STORE(3);
1773 
1774 /* GGS register attributes */
1775 static DEVICE_ATTR_RW(ggs0);
1776 static DEVICE_ATTR_RW(ggs1);
1777 static DEVICE_ATTR_RW(ggs2);
1778 static DEVICE_ATTR_RW(ggs3);
1779 
1780 /* PGGS register attributes */
1781 static DEVICE_ATTR_RW(pggs0);
1782 static DEVICE_ATTR_RW(pggs1);
1783 static DEVICE_ATTR_RW(pggs2);
1784 static DEVICE_ATTR_RW(pggs3);
1785 
feature_config_id_show(struct device * device,struct device_attribute * attr,char * buf)1786 static ssize_t feature_config_id_show(struct device *device,
1787 				      struct device_attribute *attr,
1788 				      char *buf)
1789 {
1790 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
1791 
1792 	return sysfs_emit(buf, "%d\n", devinfo->feature_conf_id);
1793 }
1794 
feature_config_id_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1795 static ssize_t feature_config_id_store(struct device *device,
1796 				       struct device_attribute *attr,
1797 				       const char *buf, size_t count)
1798 {
1799 	u32 config_id;
1800 	int ret;
1801 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
1802 
1803 	if (!buf)
1804 		return -EINVAL;
1805 
1806 	ret = kstrtou32(buf, 10, &config_id);
1807 	if (ret)
1808 		return ret;
1809 
1810 	devinfo->feature_conf_id = config_id;
1811 
1812 	return count;
1813 }
1814 
1815 static DEVICE_ATTR_RW(feature_config_id);
1816 
feature_config_value_show(struct device * device,struct device_attribute * attr,char * buf)1817 static ssize_t feature_config_value_show(struct device *device,
1818 					 struct device_attribute *attr,
1819 					 char *buf)
1820 {
1821 	int ret;
1822 	u32 ret_payload[PAYLOAD_ARG_CNT];
1823 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
1824 
1825 	ret = zynqmp_pm_get_feature_config(devinfo->feature_conf_id,
1826 					   ret_payload);
1827 	if (ret)
1828 		return ret;
1829 
1830 	return sysfs_emit(buf, "%d\n", ret_payload[1]);
1831 }
1832 
feature_config_value_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)1833 static ssize_t feature_config_value_store(struct device *device,
1834 					  struct device_attribute *attr,
1835 					  const char *buf, size_t count)
1836 {
1837 	u32 value;
1838 	int ret;
1839 	struct zynqmp_devinfo *devinfo = dev_get_drvdata(device);
1840 
1841 	if (!buf)
1842 		return -EINVAL;
1843 
1844 	ret = kstrtou32(buf, 10, &value);
1845 	if (ret)
1846 		return ret;
1847 
1848 	ret = zynqmp_pm_set_feature_config(devinfo->feature_conf_id,
1849 					   value);
1850 	if (ret)
1851 		return ret;
1852 
1853 	return count;
1854 }
1855 
1856 static DEVICE_ATTR_RW(feature_config_value);
1857 
1858 static struct attribute *zynqmp_firmware_attrs[] = {
1859 	&dev_attr_ggs0.attr,
1860 	&dev_attr_ggs1.attr,
1861 	&dev_attr_ggs2.attr,
1862 	&dev_attr_ggs3.attr,
1863 	&dev_attr_pggs0.attr,
1864 	&dev_attr_pggs1.attr,
1865 	&dev_attr_pggs2.attr,
1866 	&dev_attr_pggs3.attr,
1867 	&dev_attr_shutdown_scope.attr,
1868 	&dev_attr_health_status.attr,
1869 	&dev_attr_feature_config_id.attr,
1870 	&dev_attr_feature_config_value.attr,
1871 	NULL,
1872 };
1873 
1874 ATTRIBUTE_GROUPS(zynqmp_firmware);
1875 
zynqmp_firmware_probe(struct platform_device * pdev)1876 static int zynqmp_firmware_probe(struct platform_device *pdev)
1877 {
1878 	struct device *dev = &pdev->dev;
1879 	struct zynqmp_devinfo *devinfo;
1880 	int ret;
1881 
1882 	ret = get_set_conduit_method(dev->of_node);
1883 	if (ret)
1884 		return ret;
1885 
1886 	ret = do_feature_check_call(PM_FEATURE_CHECK);
1887 	if (ret >= 0 && ((ret & FIRMWARE_VERSION_MASK) >= PM_API_VERSION_1))
1888 		feature_check_enabled = true;
1889 
1890 	devinfo = devm_kzalloc(dev, sizeof(*devinfo), GFP_KERNEL);
1891 	if (!devinfo)
1892 		return -ENOMEM;
1893 
1894 	devinfo->dev = dev;
1895 
1896 	platform_set_drvdata(pdev, devinfo);
1897 
1898 	/* Check PM API version number */
1899 	ret = zynqmp_pm_get_api_version(&pm_api_version);
1900 	if (ret)
1901 		return ret;
1902 
1903 	if (pm_api_version < ZYNQMP_PM_VERSION) {
1904 		panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
1905 		      __func__,
1906 		      ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
1907 		      pm_api_version >> 16, pm_api_version & 0xFFFF);
1908 	}
1909 
1910 	pr_info("%s Platform Management API v%d.%d\n", __func__,
1911 		pm_api_version >> 16, pm_api_version & 0xFFFF);
1912 
1913 	/* Get the Family code and sub family code of platform */
1914 	ret = zynqmp_pm_get_family_info(&pm_family_code, &pm_sub_family_code);
1915 	if (ret < 0)
1916 		return ret;
1917 
1918 	/* Check trustzone version number */
1919 	ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
1920 	if (ret)
1921 		panic("Legacy trustzone found without version support\n");
1922 
1923 	if (pm_tz_version < ZYNQMP_TZ_VERSION)
1924 		panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
1925 		      __func__,
1926 		      ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
1927 		      pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1928 
1929 	pr_info("%s Trustzone version v%d.%d\n", __func__,
1930 		pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1931 
1932 	ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, firmware_devs,
1933 			      ARRAY_SIZE(firmware_devs), NULL, 0, NULL);
1934 	if (ret) {
1935 		dev_err(&pdev->dev, "failed to add MFD devices %d\n", ret);
1936 		return ret;
1937 	}
1938 
1939 	zynqmp_pm_api_debugfs_init();
1940 
1941 	if (pm_family_code == VERSAL_FAMILY_CODE) {
1942 		em_dev = platform_device_register_data(&pdev->dev, "xlnx_event_manager",
1943 						       -1, NULL, 0);
1944 		if (IS_ERR(em_dev))
1945 			dev_err_probe(&pdev->dev, PTR_ERR(em_dev), "EM register fail with error\n");
1946 	}
1947 
1948 	return of_platform_populate(dev->of_node, NULL, NULL, dev);
1949 }
1950 
zynqmp_firmware_remove(struct platform_device * pdev)1951 static void zynqmp_firmware_remove(struct platform_device *pdev)
1952 {
1953 	struct pm_api_feature_data *feature_data;
1954 	struct hlist_node *tmp;
1955 	int i;
1956 
1957 	mfd_remove_devices(&pdev->dev);
1958 	zynqmp_pm_api_debugfs_exit();
1959 
1960 	hash_for_each_safe(pm_api_features_map, i, tmp, feature_data, hentry) {
1961 		hash_del(&feature_data->hentry);
1962 		kfree(feature_data);
1963 	}
1964 
1965 	platform_device_unregister(em_dev);
1966 }
1967 
1968 static const struct of_device_id zynqmp_firmware_of_match[] = {
1969 	{.compatible = "xlnx,zynqmp-firmware"},
1970 	{.compatible = "xlnx,versal-firmware"},
1971 	{},
1972 };
1973 MODULE_DEVICE_TABLE(of, zynqmp_firmware_of_match);
1974 
1975 static struct platform_driver zynqmp_firmware_driver = {
1976 	.driver = {
1977 		.name = "zynqmp_firmware",
1978 		.of_match_table = zynqmp_firmware_of_match,
1979 		.dev_groups = zynqmp_firmware_groups,
1980 	},
1981 	.probe = zynqmp_firmware_probe,
1982 	.remove_new = zynqmp_firmware_remove,
1983 };
1984 module_platform_driver(zynqmp_firmware_driver);
1985