]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - drivers/firmware/xilinx/zynqmp.c
drivers: firmware: Add Pdi load API support
[zynq/linux.git] / drivers / firmware / xilinx / zynqmp.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Xilinx Zynq MPSoC Firmware layer
4  *
5  *  Copyright (C) 2014-2018 Xilinx, Inc.
6  *
7  *  Michal Simek <michal.simek@xilinx.com>
8  *  Davorin Mista <davorin.mista@aggios.com>
9  *  Jolly Shah <jollys@xilinx.com>
10  *  Rajan Vaja <rajanv@xilinx.com>
11  */
12
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>
19 #include <linux/of.h>
20 #include <linux/of_platform.h>
21 #include <linux/slab.h>
22 #include <linux/uaccess.h>
23
24 #include <linux/firmware/xlnx-zynqmp.h>
25 #include "zynqmp-debug.h"
26
27 static unsigned long register_address;
28
29 static const struct zynqmp_eemi_ops *eemi_ops_tbl;
30
31 static bool feature_check_enabled;
32 static u32 zynqmp_pm_features[PM_API_MAX];
33
34 static const struct mfd_cell firmware_devs[] = {
35         {
36                 .name = "zynqmp_power_controller",
37         },
38 };
39
40 /**
41  * zynqmp_pm_ret_code() - Convert PMU-FW error codes to Linux error codes
42  * @ret_status:         PMUFW return code
43  *
44  * Return: corresponding Linux error code
45  */
46 static int zynqmp_pm_ret_code(u32 ret_status)
47 {
48         switch (ret_status) {
49         case XST_PM_SUCCESS:
50         case XST_PM_DOUBLE_REQ:
51                 return 0;
52         case XST_PM_NO_FEATURE:
53                 return -ENOTSUPP;
54         case XST_PM_NO_ACCESS:
55                 return -EACCES;
56         case XST_PM_ABORT_SUSPEND:
57                 return -ECANCELED;
58         case XST_PM_INTERNAL:
59         case XST_PM_CONFLICT:
60         case XST_PM_INVALID_NODE:
61         default:
62                 return -EINVAL;
63         }
64 }
65
66 static noinline int do_fw_call_fail(u64 arg0, u64 arg1, u64 arg2,
67                                     u32 *ret_payload)
68 {
69         return -ENODEV;
70 }
71
72 /*
73  * PM function call wrapper
74  * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
75  */
76 static int (*do_fw_call)(u64, u64, u64, u32 *ret_payload) = do_fw_call_fail;
77
78 /**
79  * do_fw_call_smc() - Call system-level platform management layer (SMC)
80  * @arg0:               Argument 0 to SMC call
81  * @arg1:               Argument 1 to SMC call
82  * @arg2:               Argument 2 to SMC call
83  * @ret_payload:        Returned value array
84  *
85  * Invoke platform management function via SMC call (no hypervisor present).
86  *
87  * Return: Returns status, either success or error+reason
88  */
89 static noinline int do_fw_call_smc(u64 arg0, u64 arg1, u64 arg2,
90                                    u32 *ret_payload)
91 {
92         struct arm_smccc_res res;
93
94         arm_smccc_smc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
95
96         if (ret_payload) {
97                 ret_payload[0] = lower_32_bits(res.a0);
98                 ret_payload[1] = upper_32_bits(res.a0);
99                 ret_payload[2] = lower_32_bits(res.a1);
100                 ret_payload[3] = upper_32_bits(res.a1);
101         }
102
103         return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
104 }
105
106 /**
107  * do_fw_call_hvc() - Call system-level platform management layer (HVC)
108  * @arg0:               Argument 0 to HVC call
109  * @arg1:               Argument 1 to HVC call
110  * @arg2:               Argument 2 to HVC call
111  * @ret_payload:        Returned value array
112  *
113  * Invoke platform management function via HVC
114  * HVC-based for communication through hypervisor
115  * (no direct communication with ATF).
116  *
117  * Return: Returns status, either success or error+reason
118  */
119 static noinline int do_fw_call_hvc(u64 arg0, u64 arg1, u64 arg2,
120                                    u32 *ret_payload)
121 {
122         struct arm_smccc_res res;
123
124         arm_smccc_hvc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
125
126         if (ret_payload) {
127                 ret_payload[0] = lower_32_bits(res.a0);
128                 ret_payload[1] = upper_32_bits(res.a0);
129                 ret_payload[2] = lower_32_bits(res.a1);
130                 ret_payload[3] = upper_32_bits(res.a1);
131         }
132
133         return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
134 }
135
136 /**
137  * zynqmp_pm_feature() - Check weather given feature is supported or not
138  * @api_id:             API ID to check
139  *
140  * Return: Returns status, either success or error+reason
141  */
142 static int zynqmp_pm_feature(u32 api_id)
143 {
144         int ret;
145         u32 ret_payload[PAYLOAD_ARG_CNT];
146         u64 smc_arg[2];
147
148         if (!feature_check_enabled)
149                 return 0;
150
151         /* Return value if feature is already checked */
152         if (zynqmp_pm_features[api_id] != PM_FEATURE_UNCHECKED)
153                 return zynqmp_pm_features[api_id];
154
155         smc_arg[0] = PM_SIP_SVC | PM_FEATURE_CHECK;
156         smc_arg[1] = api_id;
157
158         ret = do_fw_call(smc_arg[0], smc_arg[1], 0, ret_payload);
159         if (ret) {
160                 zynqmp_pm_features[api_id] = PM_FEATURE_INVALID;
161                 return PM_FEATURE_INVALID;
162         }
163
164         zynqmp_pm_features[api_id] = ret_payload[1];
165
166         return zynqmp_pm_features[api_id];
167 }
168
169 /**
170  * zynqmp_pm_invoke_fn() - Invoke the system-level platform management layer
171  *                         caller function depending on the configuration
172  * @pm_api_id:          Requested PM-API call
173  * @arg0:               Argument 0 to requested PM-API call
174  * @arg1:               Argument 1 to requested PM-API call
175  * @arg2:               Argument 2 to requested PM-API call
176  * @arg3:               Argument 3 to requested PM-API call
177  * @ret_payload:        Returned value array
178  *
179  * Invoke platform management function for SMC or HVC call, depending on
180  * configuration.
181  * Following SMC Calling Convention (SMCCC) for SMC64:
182  * Pm Function Identifier,
183  * PM_SIP_SVC + PM_API_ID =
184  *      ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
185  *      ((SMC_64) << FUNCID_CC_SHIFT)
186  *      ((SIP_START) << FUNCID_OEN_SHIFT)
187  *      ((PM_API_ID) & FUNCID_NUM_MASK))
188  *
189  * PM_SIP_SVC   - Registered ZynqMP SIP Service Call.
190  * PM_API_ID    - Platform Management API ID.
191  *
192  * Return: Returns status, either success or error+reason
193  */
194 int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 arg0, u32 arg1,
195                         u32 arg2, u32 arg3, u32 *ret_payload)
196 {
197         /*
198          * Added SIP service call Function Identifier
199          * Make sure to stay in x0 register
200          */
201         u64 smc_arg[4];
202
203         if (zynqmp_pm_feature(pm_api_id) == PM_FEATURE_INVALID)
204                 return -ENOTSUPP;
205
206         smc_arg[0] = PM_SIP_SVC | pm_api_id;
207         smc_arg[1] = ((u64)arg1 << 32) | arg0;
208         smc_arg[2] = ((u64)arg3 << 32) | arg2;
209
210         return do_fw_call(smc_arg[0], smc_arg[1], smc_arg[2], ret_payload);
211 }
212
213 static u32 pm_api_version;
214 static u32 pm_tz_version;
215
216 /**
217  * zynqmp_pm_get_api_version() - Get version number of PMU PM firmware
218  * @version:    Returned version value
219  *
220  * Return: Returns status, either success or error+reason
221  */
222 static int zynqmp_pm_get_api_version(u32 *version)
223 {
224         u32 ret_payload[PAYLOAD_ARG_CNT];
225         int ret;
226
227         if (!version)
228                 return -EINVAL;
229
230         /* Check is PM API version already verified */
231         if (pm_api_version > 0) {
232                 *version = pm_api_version;
233                 return 0;
234         }
235         ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, 0, 0, 0, 0, ret_payload);
236         *version = ret_payload[1];
237
238         return ret;
239 }
240
241 /**
242  * zynqmp_pm_get_chipid - Get silicon ID registers
243  * @idcode:     IDCODE register
244  * @version:    version register
245  *
246  * Return:      Returns the status of the operation and the idcode and version
247  *              registers in @idcode and @version.
248  */
249 static int zynqmp_pm_get_chipid(u32 *idcode, u32 *version)
250 {
251         u32 ret_payload[PAYLOAD_ARG_CNT];
252         int ret;
253
254         if (!idcode || !version)
255                 return -EINVAL;
256
257         ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
258         *idcode = ret_payload[1];
259         *version = ret_payload[2];
260
261         return ret;
262 }
263
264 /**
265  * zynqmp_pm_get_trustzone_version() - Get secure trustzone firmware version
266  * @version:    Returned version value
267  *
268  * Return: Returns status, either success or error+reason
269  */
270 static int zynqmp_pm_get_trustzone_version(u32 *version)
271 {
272         u32 ret_payload[PAYLOAD_ARG_CNT];
273         int ret;
274
275         if (!version)
276                 return -EINVAL;
277
278         /* Check is PM trustzone version already verified */
279         if (pm_tz_version > 0) {
280                 *version = pm_tz_version;
281                 return 0;
282         }
283         ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, 0, 0,
284                                   0, 0, ret_payload);
285         *version = ret_payload[1];
286
287         return ret;
288 }
289
290 /**
291  * get_set_conduit_method() - Choose SMC or HVC based communication
292  * @np:         Pointer to the device_node structure
293  *
294  * Use SMC or HVC-based functions to communicate with EL2/EL3.
295  *
296  * Return: Returns 0 on success or error code
297  */
298 static int get_set_conduit_method(struct device_node *np)
299 {
300         const char *method;
301
302         if (of_property_read_string(np, "method", &method)) {
303                 pr_warn("%s missing \"method\" property\n", __func__);
304                 return -ENXIO;
305         }
306
307         if (!strcmp("hvc", method)) {
308                 do_fw_call = do_fw_call_hvc;
309         } else if (!strcmp("smc", method)) {
310                 do_fw_call = do_fw_call_smc;
311         } else {
312                 pr_warn("%s Invalid \"method\" property: %s\n",
313                         __func__, method);
314                 return -EINVAL;
315         }
316
317         return 0;
318 }
319
320 /**
321  * zynqmp_pm_query_data() - Get query data from firmware
322  * @qdata:      Variable to the zynqmp_pm_query_data structure
323  * @out:        Returned output value
324  *
325  * Return: Returns status, either success or error+reason
326  */
327 static int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
328 {
329         int ret;
330
331         ret = zynqmp_pm_invoke_fn(PM_QUERY_DATA, qdata.qid, qdata.arg1,
332                                   qdata.arg2, qdata.arg3, out);
333
334         /*
335          * For clock name query, all bytes in SMC response are clock name
336          * characters and return code is always success. For invalid clocks,
337          * clock name bytes would be zeros.
338          */
339         return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : ret;
340 }
341
342 /**
343  * zynqmp_pm_clock_enable() - Enable the clock for given id
344  * @clock_id:   ID of the clock to be enabled
345  *
346  * This function is used by master to enable the clock
347  * including peripherals and PLL clocks.
348  *
349  * Return: Returns status, either success or error+reason
350  */
351 static int zynqmp_pm_clock_enable(u32 clock_id)
352 {
353         return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, clock_id, 0, 0, 0, NULL);
354 }
355
356 /**
357  * zynqmp_pm_clock_disable() - Disable the clock for given id
358  * @clock_id:   ID of the clock to be disable
359  *
360  * This function is used by master to disable the clock
361  * including peripherals and PLL clocks.
362  *
363  * Return: Returns status, either success or error+reason
364  */
365 static int zynqmp_pm_clock_disable(u32 clock_id)
366 {
367         return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, clock_id, 0, 0, 0, NULL);
368 }
369
370 /**
371  * zynqmp_pm_clock_getstate() - Get the clock state for given id
372  * @clock_id:   ID of the clock to be queried
373  * @state:      1/0 (Enabled/Disabled)
374  *
375  * This function is used by master to get the state of clock
376  * including peripherals and PLL clocks.
377  *
378  * Return: Returns status, either success or error+reason
379  */
380 static int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
381 {
382         u32 ret_payload[PAYLOAD_ARG_CNT];
383         int ret;
384
385         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, clock_id, 0,
386                                   0, 0, ret_payload);
387         *state = ret_payload[1];
388
389         return ret;
390 }
391
392 /**
393  * zynqmp_pm_clock_setdivider() - Set the clock divider for given id
394  * @clock_id:   ID of the clock
395  * @divider:    divider value
396  *
397  * This function is used by master to set divider for any clock
398  * to achieve desired rate.
399  *
400  * Return: Returns status, either success or error+reason
401  */
402 static int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
403 {
404         return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, clock_id, divider,
405                                    0, 0, NULL);
406 }
407
408 /**
409  * zynqmp_pm_clock_getdivider() - Get the clock divider for given id
410  * @clock_id:   ID of the clock
411  * @divider:    divider value
412  *
413  * This function is used by master to get divider values
414  * for any clock.
415  *
416  * Return: Returns status, either success or error+reason
417  */
418 static int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
419 {
420         u32 ret_payload[PAYLOAD_ARG_CNT];
421         int ret;
422
423         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, clock_id, 0,
424                                   0, 0, ret_payload);
425         *divider = ret_payload[1];
426
427         return ret;
428 }
429
430 /**
431  * zynqmp_pm_clock_setrate() - Set the clock rate for given id
432  * @clock_id:   ID of the clock
433  * @rate:       rate value in hz
434  *
435  * This function is used by master to set rate for any clock.
436  *
437  * Return: Returns status, either success or error+reason
438  */
439 static int zynqmp_pm_clock_setrate(u32 clock_id, u64 rate)
440 {
441         return zynqmp_pm_invoke_fn(PM_CLOCK_SETRATE, clock_id,
442                                    lower_32_bits(rate),
443                                    upper_32_bits(rate),
444                                    0, NULL);
445 }
446
447 /**
448  * zynqmp_pm_clock_getrate() - Get the clock rate for given id
449  * @clock_id:   ID of the clock
450  * @rate:       rate value in hz
451  *
452  * This function is used by master to get rate
453  * for any clock.
454  *
455  * Return: Returns status, either success or error+reason
456  */
457 static int zynqmp_pm_clock_getrate(u32 clock_id, u64 *rate)
458 {
459         u32 ret_payload[PAYLOAD_ARG_CNT];
460         int ret;
461
462         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETRATE, clock_id, 0,
463                                   0, 0, ret_payload);
464         *rate = ((u64)ret_payload[2] << 32) | ret_payload[1];
465
466         return ret;
467 }
468
469 /**
470  * zynqmp_pm_clock_setparent() - Set the clock parent for given id
471  * @clock_id:   ID of the clock
472  * @parent_id:  parent id
473  *
474  * This function is used by master to set parent for any clock.
475  *
476  * Return: Returns status, either success or error+reason
477  */
478 static int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
479 {
480         return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, clock_id,
481                                    parent_id, 0, 0, NULL);
482 }
483
484 /**
485  * zynqmp_pm_clock_getparent() - Get the clock parent for given id
486  * @clock_id:   ID of the clock
487  * @parent_id:  parent id
488  *
489  * This function is used by master to get parent index
490  * for any clock.
491  *
492  * Return: Returns status, either success or error+reason
493  */
494 static int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
495 {
496         u32 ret_payload[PAYLOAD_ARG_CNT];
497         int ret;
498
499         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, clock_id, 0,
500                                   0, 0, ret_payload);
501         *parent_id = ret_payload[1];
502
503         return ret;
504 }
505
506 /**
507  * zynqmp_is_valid_ioctl() - Check whether IOCTL ID is valid or not
508  * @ioctl_id:   IOCTL ID
509  *
510  * Return: 1 if IOCTL is valid else 0
511  */
512 static inline int zynqmp_is_valid_ioctl(u32 ioctl_id)
513 {
514         switch (ioctl_id) {
515         case IOCTL_SET_PLL_FRAC_MODE:
516         case IOCTL_GET_PLL_FRAC_MODE:
517         case IOCTL_SET_PLL_FRAC_DATA:
518         case IOCTL_GET_PLL_FRAC_DATA:
519         case IOCTL_GET_RPU_OPER_MODE:
520         case IOCTL_SET_RPU_OPER_MODE:
521         case IOCTL_RPU_BOOT_ADDR_CONFIG:
522         case IOCTL_TCM_COMB_CONFIG:
523         case IOCTL_SET_TAPDELAY_BYPASS:
524         case IOCTL_SET_SGMII_MODE:
525         case IOCTL_SD_DLL_RESET:
526         case IOCTL_SET_SD_TAPDELAY:
527         case IOCTL_WRITE_GGS:
528         case IOCTL_READ_GGS:
529         case IOCTL_WRITE_PGGS:
530         case IOCTL_READ_PGGS:
531         case IOCTL_ULPI_RESET:
532         case IOCTL_SET_BOOT_HEALTH_STATUS:
533         case IOCTL_AFI:
534                 return 1;
535         default:
536                 return 0;
537         }
538 }
539
540 /**
541  * zynqmp_pm_ioctl() - PM IOCTL API for device control and configs
542  * @node_id:    Node ID of the device
543  * @ioctl_id:   ID of the requested IOCTL
544  * @arg1:       Argument 1 to requested IOCTL call
545  * @arg2:       Argument 2 to requested IOCTL call
546  * @out:        Returned output value
547  *
548  * This function calls IOCTL to firmware for device control and configuration.
549  *
550  * Return: Returns status, either success or error+reason
551  */
552 static int zynqmp_pm_ioctl(u32 node_id, u32 ioctl_id, u32 arg1, u32 arg2,
553                            u32 *out)
554 {
555         if (!zynqmp_is_valid_ioctl(ioctl_id))
556                 return -EINVAL;
557
558         return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, ioctl_id,
559                                    arg1, arg2, out);
560 }
561
562 /**
563  * zynqmp_pm_reset_assert - Request setting of reset (1 - assert, 0 - release)
564  * @reset:              Reset to be configured
565  * @assert_flag:        Flag stating should reset be asserted (1) or
566  *                      released (0)
567  *
568  * Return: Returns status, either success or error+reason
569  */
570 static int zynqmp_pm_reset_assert(const enum zynqmp_pm_reset reset,
571                                   const enum zynqmp_pm_reset_action assert_flag)
572 {
573         return zynqmp_pm_invoke_fn(PM_RESET_ASSERT, reset, assert_flag,
574                                    0, 0, NULL);
575 }
576
577 /**
578  * zynqmp_pm_reset_get_status - Get status of the reset
579  * @reset:      Reset whose status should be returned
580  * @status:     Returned status
581  *
582  * Return: Returns status, either success or error+reason
583  */
584 static int zynqmp_pm_reset_get_status(const enum zynqmp_pm_reset reset,
585                                       u32 *status)
586 {
587         u32 ret_payload[PAYLOAD_ARG_CNT];
588         int ret;
589
590         if (!status)
591                 return -EINVAL;
592
593         ret = zynqmp_pm_invoke_fn(PM_RESET_GET_STATUS, reset, 0,
594                                   0, 0, ret_payload);
595         *status = ret_payload[1];
596
597         return ret;
598 }
599
600 /**
601  * zynqmp_pm_init_finalize() - PM call to inform firmware that the caller
602  *                             master has initialized its own power management
603  *
604  * This API function is to be used for notify the power management controller
605  * about the completed power management initialization.
606  *
607  * Return: Returns status, either success or error+reason
608  */
609 static int zynqmp_pm_init_finalize(void)
610 {
611         return zynqmp_pm_invoke_fn(PM_PM_INIT_FINALIZE, 0, 0, 0, 0, NULL);
612 }
613
614 /**
615  * zynqmp_pm_set_suspend_mode() - Set system suspend mode
616  * @mode:       Mode to set for system suspend
617  *
618  * This API function is used to set mode of system suspend.
619  *
620  * Return: Returns status, either success or error+reason
621  */
622 static int zynqmp_pm_set_suspend_mode(u32 mode)
623 {
624         return zynqmp_pm_invoke_fn(PM_SET_SUSPEND_MODE, mode, 0, 0, 0, NULL);
625 }
626
627 /**
628  * zynqmp_pm_request_node() - Request a node with specific capabilities
629  * @node:               Node ID of the slave
630  * @capabilities:       Requested capabilities of the slave
631  * @qos:                Quality of service (not supported)
632  * @ack:                Flag to specify whether acknowledge is requested
633  *
634  * This function is used by master to request particular node from firmware.
635  * Every master must request node before using it.
636  *
637  * Return: Returns status, either success or error+reason
638  */
639 static int zynqmp_pm_request_node(const u32 node, const u32 capabilities,
640                                   const u32 qos,
641                                   const enum zynqmp_pm_request_ack ack)
642 {
643         return zynqmp_pm_invoke_fn(PM_REQUEST_NODE, node, capabilities,
644                                    qos, ack, NULL);
645 }
646
647 /**
648  * zynqmp_pm_release_node() - Release a node
649  * @node:       Node ID of the slave
650  *
651  * This function is used by master to inform firmware that master
652  * has released node. Once released, master must not use that node
653  * without re-request.
654  *
655  * Return: Returns status, either success or error+reason
656  */
657 static int zynqmp_pm_release_node(const u32 node)
658 {
659         return zynqmp_pm_invoke_fn(PM_RELEASE_NODE, node, 0, 0, 0, NULL);
660 }
661
662 /**
663  * zynqmp_pm_set_requirement() - PM call to set requirement for PM slaves
664  * @node:               Node ID of the slave
665  * @capabilities:       Requested capabilities of the slave
666  * @qos:                Quality of service (not supported)
667  * @ack:                Flag to specify whether acknowledge is requested
668  *
669  * This API function is to be used for slaves a PU already has requested
670  * to change its capabilities.
671  *
672  * Return: Returns status, either success or error+reason
673  */
674 static int zynqmp_pm_set_requirement(const u32 node, const u32 capabilities,
675                                      const u32 qos,
676                                      const enum zynqmp_pm_request_ack ack)
677 {
678         return zynqmp_pm_invoke_fn(PM_SET_REQUIREMENT, node, capabilities,
679                                    qos, ack, NULL);
680 }
681
682 /**
683  * zynqmp_pm_load_pdi - Load and process pdi
684  * @src:        Source device where PDI is located
685  * @address:    Pdi src address
686  *
687  * This function provides support to load pdi from linux
688  *
689  * Return: Returns status, either success or error+reason
690  */
691 static int zynqmp_pm_load_pdi(const u32 src, const u64 address)
692 {
693         return zynqmp_pm_invoke_fn(PM_LOAD_PDI, src,
694                                    lower_32_bits(address),
695                                    upper_32_bits(address), 0, NULL);
696 }
697
698 /**
699  * zynqmp_pm_fpga_load - Perform the fpga load
700  * @address:    Address to write to
701  * @size:       pl bitstream size
702  * @flags:
703  *      BIT(0) - Bitstream type.
704  *               0 - Full Bitstream.
705  *               1 - Partial Bitstream.
706  *      BIT(1) - Authentication.
707  *               1 - Enable.
708  *               0 - Disable.
709  *      BIT(2) - Encryption.
710  *               1 - Enable.
711  *               0 - Disable.
712  * NOTE -
713  *      The current implementation supports only Full Bit-stream.
714  *
715  * This function provides access to xilfpga library to transfer
716  * the required bitstream into PL.
717  *
718  * Return:      Returns status, either success or error+reason
719  */
720 static int zynqmp_pm_fpga_load(const u64 address, const u32 size,
721                                const u32 flags)
722 {
723         return zynqmp_pm_invoke_fn(PM_FPGA_LOAD, (u32)address,
724                                    ((u32)(address >> 32)), size, flags, NULL);
725 }
726
727 /**
728  * zynqmp_pm_fpga_get_status - Read value from PCAP status register
729  * @value:      Value to read
730  *
731  * This function provides access to the xilfpga library to get
732  * the PCAP status
733  *
734  * Return:      Returns status, either success or error+reason
735  */
736 static int zynqmp_pm_fpga_get_status(u32 *value)
737 {
738         u32 ret_payload[PAYLOAD_ARG_CNT];
739         int ret;
740
741         if (!value)
742                 return -EINVAL;
743
744         ret = zynqmp_pm_invoke_fn(PM_FPGA_GET_STATUS, 0, 0, 0, 0, ret_payload);
745         *value = ret_payload[1];
746
747         return ret;
748 }
749
750 /**
751  * zynqmp_pm_fpga_read - Perform the fpga configuration readback
752  * @reg_numframes: Configuration register offset (or) Number of frames to read
753  * @phys_address: Physical Address of the buffer
754  * @readback_type: Type of fpga readback operation
755  * @value: Value to read
756  *
757  * This function provides access to xilfpga library to perform
758  * fpga configuration readback.
759  *
760  * Return:      Returns status, either success or error+reason
761  */
762 static int zynqmp_pm_fpga_read(const u32 reg_numframes, const u64 phys_address,
763                                u32 readback_type, u32 *value)
764 {
765         u32 ret_payload[PAYLOAD_ARG_CNT];
766         int ret;
767
768         if (!value)
769                 return -EINVAL;
770
771         ret = zynqmp_pm_invoke_fn(PM_FPGA_READ, reg_numframes,
772                                   lower_32_bits(phys_address),
773                                   upper_32_bits(phys_address), readback_type,
774                                   ret_payload);
775         *value = ret_payload[1];
776
777         return ret;
778 }
779
780 /**
781  * zynqmp_pm_sha_hash - Access the SHA engine to calculate the hash
782  * @address:    Address of the data/ Address of output buffer where
783  *              hash should be stored.
784  * @size:       Size of the data.
785  * @flags:
786  *      BIT(0) - for initializing csudma driver and SHA3(Here address
787  *               and size inputs can be NULL).
788  *      BIT(1) - to call Sha3_Update API which can be called multiple
789  *               times when data is not contiguous.
790  *      BIT(2) - to get final hash of the whole updated data.
791  *               Hash will be overwritten at provided address with
792  *               48 bytes.
793  *
794  * Return:      Returns status, either success or error code.
795  */
796 static int zynqmp_pm_sha_hash(const u64 address, const u32 size,
797                               const u32 flags)
798 {
799         u32 lower_32_bits = (u32)address;
800         u32 upper_32_bits = (u32)(address >> 32);
801
802         return zynqmp_pm_invoke_fn(PM_SECURE_SHA, upper_32_bits, lower_32_bits,
803                                    size, flags, NULL);
804 }
805
806 /**
807  * zynqmp_pm_rsa - Access RSA hardware to encrypt/decrypt the data with RSA.
808  * @address:    Address of the data
809  * @size:       Size of the data.
810  * @flags:
811  *              BIT(0) - Encryption/Decryption
812  *                       0 - RSA decryption with private key
813  *                       1 - RSA encryption with public key.
814  *
815  * Return:      Returns status, either success or error code.
816  */
817 static int zynqmp_pm_rsa(const u64 address, const u32 size, const u32 flags)
818 {
819         u32 lower_32_bits = (u32)address;
820         u32 upper_32_bits = (u32)(address >> 32);
821
822         return zynqmp_pm_invoke_fn(PM_SECURE_RSA, upper_32_bits, lower_32_bits,
823                                    size, flags, NULL);
824 }
825
826 /**
827  * zynqmp_pm_aes - Access AES hardware to encrypt/decrypt the data using
828  * AES-GCM core.
829  * @address:    Address of the AesParams structure.
830  * @out:        Returned output value
831  *
832  * Return:      Returns status, either success or error code.
833  */
834 static int zynqmp_pm_aes_engine(const u64 address, u32 *out)
835 {
836         u32 ret_payload[PAYLOAD_ARG_CNT];
837         int ret;
838
839         if (!out)
840                 return -EINVAL;
841
842         ret = zynqmp_pm_invoke_fn(PM_SECURE_AES, upper_32_bits(address),
843                                   lower_32_bits(address),
844                                   0, 0, ret_payload);
845         *out = ret_payload[1];
846         return ret;
847 }
848
849 /**
850  * zynqmp_pm_request_suspend - PM call to request for another PU or subsystem to
851  *                                      be suspended gracefully.
852  * @node:       Node ID of the targeted PU or subsystem
853  * @ack:        Flag to specify whether acknowledge is requested
854  * @latency:    Requested wakeup latency (not supported)
855  * @state:      Requested state (not supported)
856  *
857  * Return:      Returns status, either success or error+reason
858  */
859 static int zynqmp_pm_request_suspend(const u32 node,
860                                      const enum zynqmp_pm_request_ack ack,
861                                      const u32 latency,
862                                      const u32 state)
863 {
864         return zynqmp_pm_invoke_fn(PM_REQUEST_SUSPEND, node, ack,
865                                    latency, state, NULL);
866 }
867
868 /**
869  * zynqmp_pm_force_powerdown - PM call to request for another PU or subsystem to
870  *                              be powered down forcefully
871  * @target:     Node ID of the targeted PU or subsystem
872  * @ack:        Flag to specify whether acknowledge is requested
873  *
874  * Return:      Returns status, either success or error+reason
875  */
876 static int zynqmp_pm_force_powerdown(const u32 target,
877                                      const enum zynqmp_pm_request_ack ack)
878 {
879         return zynqmp_pm_invoke_fn(PM_FORCE_POWERDOWN, target, ack, 0, 0, NULL);
880 }
881
882 /**
883  * zynqmp_pm_request_wakeup - PM call to wake up selected master or subsystem
884  * @node:       Node ID of the master or subsystem
885  * @set_addr:   Specifies whether the address argument is relevant
886  * @address:    Address from which to resume when woken up
887  * @ack:        Flag to specify whether acknowledge requested
888  *
889  * Return:      Returns status, either success or error+reason
890  */
891 static int zynqmp_pm_request_wakeup(const u32 node,
892                                     const bool set_addr,
893                                     const u64 address,
894                                     const enum zynqmp_pm_request_ack ack)
895 {
896         /* set_addr flag is encoded into 1st bit of address */
897         return zynqmp_pm_invoke_fn(PM_REQUEST_WAKEUP, node, address | set_addr,
898                                    address >> 32, ack, NULL);
899 }
900
901 /**
902  * zynqmp_pm_set_wakeup_source - PM call to specify the wakeup source
903  *                                      while suspended
904  * @target:     Node ID of the targeted PU or subsystem
905  * @wakeup_node:Node ID of the wakeup peripheral
906  * @enable:     Enable or disable the specified peripheral as wake source
907  *
908  * Return:      Returns status, either success or error+reason
909  */
910 static int zynqmp_pm_set_wakeup_source(const u32 target,
911                                        const u32 wakeup_node,
912                                        const u32 enable)
913 {
914         return zynqmp_pm_invoke_fn(PM_SET_WAKEUP_SOURCE, target,
915                                    wakeup_node, enable, 0, NULL);
916 }
917
918 /**
919  * zynqmp_pm_system_shutdown - PM call to request a system shutdown or restart
920  * @type:       Shutdown or restart? 0 for shutdown, 1 for restart
921  * @subtype:    Specifies which system should be restarted or shut down
922  *
923  * Return:      Returns status, either success or error+reason
924  */
925 static int zynqmp_pm_system_shutdown(const u32 type, const u32 subtype)
926 {
927         return zynqmp_pm_invoke_fn(PM_SYSTEM_SHUTDOWN, type, subtype,
928                                    0, 0, NULL);
929 }
930
931 /**
932  * zynqmp_pm_set_max_latency - PM call to set wakeup latency requirements
933  * @node:       Node ID of the slave
934  * @latency:    Requested maximum wakeup latency
935  *
936  * Return:      Returns status, either success or error+reason
937  */
938 static int zynqmp_pm_set_max_latency(const u32 node, const u32 latency)
939 {
940         return zynqmp_pm_invoke_fn(PM_SET_MAX_LATENCY, node, latency,
941                                    0, 0, NULL);
942 }
943
944 /**
945  * zynqmp_pm_set_configuration - PM call to set system configuration
946  * @physical_addr:      Physical 32-bit address of data structure in memory
947  *
948  * Return:              Returns status, either success or error+reason
949  */
950 static int zynqmp_pm_set_configuration(const u32 physical_addr)
951 {
952         return zynqmp_pm_invoke_fn(PM_SET_CONFIGURATION, physical_addr, 0,
953                                    0, 0, NULL);
954 }
955
956 /**
957  * zynqmp_pm_get_node_status - PM call to request a node's current power state
958  * @node:               ID of the component or sub-system in question
959  * @status:             Current operating state of the requested node
960  * @requirements:       Current requirements asserted on the node,
961  *                      used for slave nodes only.
962  * @usage:              Usage information, used for slave nodes only:
963  *                      PM_USAGE_NO_MASTER      - No master is currently using
964  *                                                the node
965  *                      PM_USAGE_CURRENT_MASTER - Only requesting master is
966  *                                                currently using the node
967  *                      PM_USAGE_OTHER_MASTER   - Only other masters are
968  *                                                currently using the node
969  *                      PM_USAGE_BOTH_MASTERS   - Both the current and at least
970  *                                                one other master is currently
971  *                                                using the node
972  *
973  * Return:              Returns status, either success or error+reason
974  */
975 static int zynqmp_pm_get_node_status(const u32 node, u32 *const status,
976                                      u32 *const requirements, u32 *const usage)
977 {
978         u32 ret_payload[PAYLOAD_ARG_CNT];
979         int ret;
980
981         if (!status)
982                 return -EINVAL;
983
984         ret = zynqmp_pm_invoke_fn(PM_GET_NODE_STATUS, node, 0, 0,
985                                   0, ret_payload);
986         if (ret_payload[0] == XST_PM_SUCCESS) {
987                 *status = ret_payload[1];
988                 if (requirements)
989                         *requirements = ret_payload[2];
990                 if (usage)
991                         *usage = ret_payload[3];
992         }
993
994         return ret;
995 }
996
997 /**
998  * zynqmp_pm_get_operating_characteristic - PM call to request operating
999  *                                              characteristic information
1000  * @node:       Node ID of the slave
1001  * @type:       Type of the operating characteristic requested
1002  * @result:     Used to return the requsted operating characteristic
1003  *
1004  * Return:      Returns status, either success or error+reason
1005  */
1006 static int zynqmp_pm_get_operating_characteristic(const u32 node,
1007                 const enum zynqmp_pm_opchar_type type,
1008                 u32 *const result)
1009 {
1010         u32 ret_payload[PAYLOAD_ARG_CNT];
1011         int ret;
1012
1013         if (!result)
1014                 return -EINVAL;
1015
1016         ret = zynqmp_pm_invoke_fn(PM_GET_OPERATING_CHARACTERISTIC,
1017                                   node, type, 0, 0, ret_payload);
1018         if (ret_payload[0] == XST_PM_SUCCESS)
1019                 *result = ret_payload[1];
1020
1021         return ret;
1022 }
1023
1024 /**
1025  * zynqmp_pm_pinctrl_request - Request Pin from firmware
1026  * @pin:        Pin number to request
1027  *
1028  * This function requests pin from firmware.
1029  *
1030  * Return:      Returns status, either success or error+reason.
1031  */
1032 static int zynqmp_pm_pinctrl_request(const u32 pin)
1033 {
1034         return zynqmp_pm_invoke_fn(PM_PINCTRL_REQUEST, pin, 0, 0, 0, NULL);
1035 }
1036
1037 /**
1038  * zynqmp_pm_pinctrl_release - Inform firmware that Pin control is released
1039  * @pin:        Pin number to release
1040  *
1041  * This function release pin from firmware.
1042  *
1043  * Return:      Returns status, either success or error+reason.
1044  */
1045 static int zynqmp_pm_pinctrl_release(const u32 pin)
1046 {
1047         return zynqmp_pm_invoke_fn(PM_PINCTRL_RELEASE, pin, 0, 0, 0, NULL);
1048 }
1049
1050 /**
1051  * zynqmp_pm_pinctrl_get_function - Read function id set for the given pin
1052  * @pin:        Pin number
1053  * @id:         Buffer to store function ID
1054  *
1055  * This function provides the function currently set for the given pin.
1056  *
1057  * Return:      Returns status, either success or error+reason
1058  */
1059 static int zynqmp_pm_pinctrl_get_function(const u32 pin, u32 *id)
1060 {
1061         u32 ret_payload[PAYLOAD_ARG_CNT];
1062         int ret;
1063
1064         if (!id)
1065                 return -EINVAL;
1066
1067         ret = zynqmp_pm_invoke_fn(PM_PINCTRL_GET_FUNCTION, pin, 0,
1068                                   0, 0, ret_payload);
1069         *id = ret_payload[1];
1070
1071         return ret;
1072 }
1073
1074 /**
1075  * zynqmp_pm_pinctrl_set_function - Set requested function for the pin
1076  * @pin:        Pin number
1077  * @id:         Function ID to set
1078  *
1079  * This function sets requested function for the given pin.
1080  *
1081  * Return:      Returns status, either success or error+reason.
1082  */
1083 static int zynqmp_pm_pinctrl_set_function(const u32 pin, const u32 id)
1084 {
1085         return zynqmp_pm_invoke_fn(PM_PINCTRL_SET_FUNCTION, pin, id,
1086                                    0, 0, NULL);
1087 }
1088
1089 /**
1090  * zynqmp_pm_pinctrl_get_config - Get configuration parameter for the pin
1091  * @pin:        Pin number
1092  * @param:      Parameter to get
1093  * @value:      Buffer to store parameter value
1094  *
1095  * This function gets requested configuration parameter for the given pin.
1096  *
1097  * Return:      Returns status, either success or error+reason.
1098  */
1099 static int zynqmp_pm_pinctrl_get_config(const u32 pin, const u32 param,
1100                                         u32 *value)
1101 {
1102         u32 ret_payload[PAYLOAD_ARG_CNT];
1103         int ret;
1104
1105         if (!value)
1106                 return -EINVAL;
1107
1108         ret = zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_GET, pin, param,
1109                                   0, 0, ret_payload);
1110         *value = ret_payload[1];
1111
1112         return ret;
1113 }
1114
1115 /**
1116  * zynqmp_pm_pinctrl_set_config - Set configuration parameter for the pin
1117  * @pin:        Pin number
1118  * @param:      Parameter to set
1119  * @value:      Parameter value to set
1120  *
1121  * This function sets requested configuration parameter for the given pin.
1122  *
1123  * Return:      Returns status, either success or error+reason.
1124  */
1125 static int zynqmp_pm_pinctrl_set_config(const u32 pin, const u32 param,
1126                                         u32 value)
1127 {
1128         return zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_SET, pin,
1129                                    param, value, 0, NULL);
1130 }
1131
1132 /**
1133  * zynqmp_pm_config_reg_access - PM Config API for Config register access
1134  * @register_access_id: ID of the requested REGISTER_ACCESS
1135  * @address:            Address of the register to be accessed
1136  * @mask:               Mask to be written to the register
1137  * @value:              Value to be written to the register
1138  * @out:                Returned output value
1139  *
1140  * This function calls REGISTER_ACCESS to configure CSU/PMU registers.
1141  *
1142  * Return:      Returns status, either success or error+reason
1143  */
1144
1145 static int zynqmp_pm_config_reg_access(u32 register_access_id, u32 address,
1146                                        u32 mask, u32 value, u32 *out)
1147 {
1148         return zynqmp_pm_invoke_fn(PM_REGISTER_ACCESS, register_access_id,
1149                                    address, mask, value, out);
1150 }
1151
1152 /**
1153  * zynqmp_pm_efuse_access - Provides access to efuse memory.
1154  * @address:    Address of the efuse params structure
1155  * @out:                Returned output value
1156  *
1157  * Return:      Returns status, either success or error code.
1158  */
1159 static int zynqmp_pm_efuse_access(const u64 address, u32 *out)
1160 {
1161         u32 ret_payload[PAYLOAD_ARG_CNT];
1162         int ret;
1163
1164         if (!out)
1165                 return -EINVAL;
1166
1167         ret = zynqmp_pm_invoke_fn(PM_EFUSE_ACCESS, upper_32_bits(address),
1168                                   lower_32_bits(address), 0, 0, ret_payload);
1169         *out = ret_payload[1];
1170
1171         return ret;
1172 }
1173
1174 static int zynqmp_pm_secure_load(const u64 src_addr, u64 key_addr, u64 *dst)
1175 {
1176         u32 ret_payload[PAYLOAD_ARG_CNT];
1177         int ret_value;
1178
1179         if (!dst)
1180                 return -EINVAL;
1181
1182         ret_value = zynqmp_pm_invoke_fn(PM_SECURE_IMAGE,
1183                                         lower_32_bits(src_addr),
1184                                         upper_32_bits(src_addr),
1185                                         lower_32_bits(key_addr),
1186                                         upper_32_bits(key_addr),
1187                                         ret_payload);
1188         *dst = ((u64)ret_payload[1] << 32) | ret_payload[2];
1189
1190         return ret_value;
1191 }
1192
1193 static const struct zynqmp_eemi_ops eemi_ops = {
1194         .get_api_version = zynqmp_pm_get_api_version,
1195         .get_chipid = zynqmp_pm_get_chipid,
1196         .query_data = zynqmp_pm_query_data,
1197         .clock_enable = zynqmp_pm_clock_enable,
1198         .clock_disable = zynqmp_pm_clock_disable,
1199         .clock_getstate = zynqmp_pm_clock_getstate,
1200         .clock_setdivider = zynqmp_pm_clock_setdivider,
1201         .clock_getdivider = zynqmp_pm_clock_getdivider,
1202         .clock_setrate = zynqmp_pm_clock_setrate,
1203         .clock_getrate = zynqmp_pm_clock_getrate,
1204         .clock_setparent = zynqmp_pm_clock_setparent,
1205         .clock_getparent = zynqmp_pm_clock_getparent,
1206         .ioctl = zynqmp_pm_ioctl,
1207         .reset_assert = zynqmp_pm_reset_assert,
1208         .reset_get_status = zynqmp_pm_reset_get_status,
1209         .init_finalize = zynqmp_pm_init_finalize,
1210         .set_suspend_mode = zynqmp_pm_set_suspend_mode,
1211         .request_node = zynqmp_pm_request_node,
1212         .release_node = zynqmp_pm_release_node,
1213         .set_requirement = zynqmp_pm_set_requirement,
1214         .fpga_load = zynqmp_pm_fpga_load,
1215         .fpga_get_status = zynqmp_pm_fpga_get_status,
1216         .fpga_read = zynqmp_pm_fpga_read,
1217         .sha_hash = zynqmp_pm_sha_hash,
1218         .rsa = zynqmp_pm_rsa,
1219         .request_suspend = zynqmp_pm_request_suspend,
1220         .force_powerdown = zynqmp_pm_force_powerdown,
1221         .request_wakeup = zynqmp_pm_request_wakeup,
1222         .set_wakeup_source = zynqmp_pm_set_wakeup_source,
1223         .system_shutdown = zynqmp_pm_system_shutdown,
1224         .set_max_latency = zynqmp_pm_set_max_latency,
1225         .set_configuration = zynqmp_pm_set_configuration,
1226         .get_node_status = zynqmp_pm_get_node_status,
1227         .get_operating_characteristic = zynqmp_pm_get_operating_characteristic,
1228         .pinctrl_request = zynqmp_pm_pinctrl_request,
1229         .pinctrl_release = zynqmp_pm_pinctrl_release,
1230         .pinctrl_get_function = zynqmp_pm_pinctrl_get_function,
1231         .pinctrl_set_function = zynqmp_pm_pinctrl_set_function,
1232         .pinctrl_get_config = zynqmp_pm_pinctrl_get_config,
1233         .pinctrl_set_config = zynqmp_pm_pinctrl_set_config,
1234         .register_access = zynqmp_pm_config_reg_access,
1235         .aes = zynqmp_pm_aes_engine,
1236         .efuse_access = zynqmp_pm_efuse_access,
1237         .secure_image = zynqmp_pm_secure_load,
1238         .pdi_load = zynqmp_pm_load_pdi,
1239 };
1240
1241 /**
1242  * zynqmp_pm_get_eemi_ops - Get eemi ops functions
1243  *
1244  * Return: Pointer of eemi_ops structure
1245  */
1246 const struct zynqmp_eemi_ops *zynqmp_pm_get_eemi_ops(void)
1247 {
1248         if (eemi_ops_tbl)
1249                 return eemi_ops_tbl;
1250         else
1251                 return ERR_PTR(-EPROBE_DEFER);
1252
1253 }
1254 EXPORT_SYMBOL_GPL(zynqmp_pm_get_eemi_ops);
1255
1256 /**
1257  * struct zynqmp_pm_shutdown_scope - Struct for shutdown scope
1258  * @subtype:    Shutdown subtype
1259  * @name:       Matching string for scope argument
1260  *
1261  * This struct encapsulates mapping between shutdown scope ID and string.
1262  */
1263 struct zynqmp_pm_shutdown_scope {
1264         const enum zynqmp_pm_shutdown_subtype subtype;
1265         const char *name;
1266 };
1267
1268 static struct zynqmp_pm_shutdown_scope shutdown_scopes[] = {
1269         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM] = {
1270                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM,
1271                 .name = "subsystem",
1272         },
1273         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY] = {
1274                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY,
1275                 .name = "ps_only",
1276         },
1277         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM] = {
1278                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM,
1279                 .name = "system",
1280         },
1281 };
1282
1283 static struct zynqmp_pm_shutdown_scope *selected_scope =
1284                 &shutdown_scopes[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM];
1285
1286 /**
1287  * zynqmp_pm_is_shutdown_scope_valid - Check if shutdown scope string is valid
1288  * @scope_string:       Shutdown scope string
1289  *
1290  * Return:              Return pointer to matching shutdown scope struct from
1291  *                      array of available options in system if string is valid,
1292  *                      otherwise returns NULL.
1293  */
1294 static struct zynqmp_pm_shutdown_scope*
1295                 zynqmp_pm_is_shutdown_scope_valid(const char *scope_string)
1296 {
1297         int count;
1298
1299         for (count = 0; count < ARRAY_SIZE(shutdown_scopes); count++)
1300                 if (sysfs_streq(scope_string, shutdown_scopes[count].name))
1301                         return &shutdown_scopes[count];
1302
1303         return NULL;
1304 }
1305
1306 /**
1307  * shutdown_scope_show - Show shutdown_scope sysfs attribute
1308  * @kobj:       Kobject structure
1309  * @attr:       Kobject attribute structure
1310  * @buf:        Requested available shutdown_scope attributes string
1311  *
1312  * User-space interface for viewing the available scope options for system
1313  * shutdown. Scope option for next shutdown call is marked with [].
1314  *
1315  * Usage: cat /sys/firmware/zynqmp/shutdown_scope
1316  *
1317  * Return:      Number of bytes printed into the buffer.
1318  */
1319 static ssize_t shutdown_scope_show(struct kobject *kobj,
1320                                    struct kobj_attribute *attr,
1321                                    char *buf)
1322 {
1323         int i;
1324
1325         for (i = 0; i < ARRAY_SIZE(shutdown_scopes); i++) {
1326                 if (&shutdown_scopes[i] == selected_scope) {
1327                         strcat(buf, "[");
1328                         strcat(buf, shutdown_scopes[i].name);
1329                         strcat(buf, "]");
1330                 } else {
1331                         strcat(buf, shutdown_scopes[i].name);
1332                 }
1333                 strcat(buf, " ");
1334         }
1335         strcat(buf, "\n");
1336
1337         return strlen(buf);
1338 }
1339
1340 /**
1341  * shutdown_scope_store - Store shutdown_scope sysfs attribute
1342  * @kobj:       Kobject structure
1343  * @attr:       Kobject attribute structure
1344  * @buf:        User entered shutdown_scope attribute string
1345  * @count:      Buffer size
1346  *
1347  * User-space interface for setting the scope for the next system shutdown.
1348  * Usage: echo <scope> > /sys/firmware/zynqmp/shutdown_scope
1349  *
1350  * The Linux shutdown functionality implemented via PSCI system_off does not
1351  * include an option to set a scope, i.e. which parts of the system to shut
1352  * down.
1353  *
1354  * This API function allows to set the shutdown scope for the next shutdown
1355  * request by passing it to the ATF running in EL3. When the next shutdown
1356  * is performed, the platform specific portion of PSCI-system_off can use
1357  * the chosen shutdown scope.
1358  *
1359  * subsystem:   Only the APU along with all of its peripherals not used by other
1360  *              processing units will be shut down. This may result in the FPD
1361  *              power domain being shut down provided that no other processing
1362  *              unit uses FPD peripherals or DRAM.
1363  * ps_only:     The complete PS will be shut down, including the RPU, PMU, etc.
1364  *              Only the PL domain (FPGA) remains untouched.
1365  * system:      The complete system/device is shut down.
1366  *
1367  * Return:      count argument if request succeeds, the corresponding error
1368  *              code otherwise
1369  */
1370 static ssize_t shutdown_scope_store(struct kobject *kobj,
1371                                     struct kobj_attribute *attr,
1372                                     const char *buf, size_t count)
1373 {
1374         int ret;
1375         struct zynqmp_pm_shutdown_scope *scope;
1376
1377         scope = zynqmp_pm_is_shutdown_scope_valid(buf);
1378         if (!scope)
1379                 return -EINVAL;
1380
1381         ret = zynqmp_pm_system_shutdown(ZYNQMP_PM_SHUTDOWN_TYPE_SETSCOPE_ONLY,
1382                                         scope->subtype);
1383         if (ret) {
1384                 pr_err("unable to set shutdown scope %s\n", buf);
1385                 return ret;
1386         }
1387
1388         selected_scope = scope;
1389
1390         return count;
1391 }
1392
1393 static struct kobj_attribute zynqmp_attr_shutdown_scope =
1394                                                 __ATTR_RW(shutdown_scope);
1395
1396 /**
1397  * health_status_store - Store health_status sysfs attribute
1398  * @kobj:       Kobject structure
1399  * @attr:       Kobject attribute structure
1400  * @buf:        User entered health_status attribute string
1401  * @count:      Buffer size
1402  *
1403  * User-space interface for setting the boot health status.
1404  * Usage: echo <value> > /sys/firmware/zynqmp/health_status
1405  *
1406  * Value:
1407  *      1 - Set healthy bit to 1
1408  *      0 - Unset healthy bit
1409  *
1410  * Return:      count argument if request succeeds, the corresponding error
1411  *              code otherwise
1412  */
1413 static ssize_t health_status_store(struct kobject *kobj,
1414                                    struct kobj_attribute *attr,
1415                                    const char *buf, size_t count)
1416 {
1417         int ret;
1418         unsigned int value;
1419
1420         ret = kstrtouint(buf, 10, &value);
1421         if (ret)
1422                 return ret;
1423
1424         ret = zynqmp_pm_ioctl(0, IOCTL_SET_BOOT_HEALTH_STATUS, value, 0, NULL);
1425         if (ret) {
1426                 pr_err("unable to set healthy bit value to %u\n", value);
1427                 return ret;
1428         }
1429
1430         return count;
1431 }
1432
1433 static struct kobj_attribute zynqmp_attr_health_status =
1434                                                 __ATTR_WO(health_status);
1435
1436 /**
1437  * config_reg_store - Write config_reg sysfs attribute
1438  * @kobj:       Kobject structure
1439  * @attr:       Kobject attribute structure
1440  * @buf:        User entered health_status attribute string
1441  * @count:      Buffer size
1442  *
1443  * User-space interface for setting the config register.
1444  *
1445  * To write any CSU/PMU register
1446  * echo <address> <mask> <values> > /sys/firmware/zynqmp/config_reg
1447  * Usage:
1448  * echo 0x345AB234 0xFFFFFFFF 0x1234ABCD > /sys/firmware/zynqmp/config_reg
1449  *
1450  * To Read any CSU/PMU register, write address to the variable like below
1451  * echo <address> > /sys/firmware/zynqmp/config_reg
1452  *
1453  * Return:      count argument if request succeeds, the corresponding error
1454  *              code otherwise
1455  */
1456 static ssize_t config_reg_store(struct kobject *kobj,
1457                                 struct kobj_attribute *attr,
1458                                 const char *buf, size_t count)
1459 {
1460         char *kern_buff, *inbuf, *tok;
1461         unsigned long address, value, mask;
1462         int ret;
1463
1464         kern_buff = kzalloc(count, GFP_KERNEL);
1465         if (!kern_buff)
1466                 return -ENOMEM;
1467
1468         ret = strlcpy(kern_buff, buf, count);
1469         if (ret < 0) {
1470                 ret = -EFAULT;
1471                 goto err;
1472         }
1473
1474         inbuf = kern_buff;
1475
1476         /* Read the addess */
1477         tok = strsep(&inbuf, " ");
1478         if (!tok) {
1479                 ret = -EFAULT;
1480                 goto err;
1481         }
1482         ret = kstrtol(tok, 16, &address);
1483         if (ret) {
1484                 ret = -EFAULT;
1485                 goto err;
1486         }
1487         /* Read the write value */
1488         tok = strsep(&inbuf, " ");
1489         /*
1490          * If parameter provided is only address, then its a read operation.
1491          * Store the address in a global variable and retrieve whenever
1492          * required.
1493          */
1494         if (!tok) {
1495                 register_address = address;
1496                 goto err;
1497         }
1498         register_address = address;
1499
1500         ret = kstrtol(tok, 16, &mask);
1501         if (ret) {
1502                 ret = -EFAULT;
1503                 goto err;
1504         }
1505         tok = strsep(&inbuf, " ");
1506         if (!tok) {
1507                 ret = -EFAULT;
1508                 goto err;
1509         }
1510         ret = kstrtol(tok, 16, &value);
1511         if (!tok) {
1512                 ret = -EFAULT;
1513                 goto err;
1514         }
1515         ret = zynqmp_pm_config_reg_access(CONFIG_REG_WRITE, address,
1516                                           mask, value, NULL);
1517         if (ret)
1518                 pr_err("unable to write value to %lx\n", value);
1519 err:
1520         kfree(kern_buff);
1521         if (ret)
1522                 return ret;
1523         return count;
1524 }
1525
1526 /**
1527  * config_reg_show - Read config_reg sysfs attribute
1528  * @kobj:       Kobject structure
1529  * @attr:       Kobject attribute structure
1530  * @buf:        User entered health_status attribute string
1531  *
1532  * User-space interface for getting the config register.
1533  *
1534  * To Read any CSU/PMU register, write address to the variable like below
1535  * echo <address> > /sys/firmware/zynqmp/config_reg
1536  *
1537  * Then Read the address using below command
1538  * cat /sys/firmware/zynqmp/config_reg
1539  *
1540  * Return: number of chars written to buf.
1541  */
1542 static ssize_t config_reg_show(struct kobject *kobj,
1543                                struct kobj_attribute *attr,
1544                                char *buf)
1545 {
1546         int ret;
1547         u32 ret_payload[PAYLOAD_ARG_CNT];
1548
1549         ret = zynqmp_pm_config_reg_access(CONFIG_REG_READ, register_address,
1550                                           0, 0, ret_payload);
1551         if (ret)
1552                 return ret;
1553
1554         return sprintf(buf, "0x%x\n", ret_payload[1]);
1555 }
1556
1557 static struct kobj_attribute zynqmp_attr_config_reg =
1558                                         __ATTR_RW(config_reg);
1559
1560 static struct attribute *attrs[] = {
1561         &zynqmp_attr_shutdown_scope.attr,
1562         &zynqmp_attr_health_status.attr,
1563         &zynqmp_attr_config_reg.attr,
1564         NULL,
1565 };
1566
1567 static const struct attribute_group attr_group = {
1568         .attrs = attrs,
1569         NULL,
1570 };
1571
1572 static int zynqmp_pm_sysfs_init(void)
1573 {
1574         struct kobject *zynqmp_kobj;
1575         int ret;
1576
1577         zynqmp_kobj = kobject_create_and_add("zynqmp", firmware_kobj);
1578         if (!zynqmp_kobj) {
1579                 pr_err("zynqmp: Firmware kobj add failed.\n");
1580                 return -ENOMEM;
1581         }
1582
1583         ret = sysfs_create_group(zynqmp_kobj, &attr_group);
1584         if (ret) {
1585                 pr_err("%s() sysfs creation fail with error %d\n",
1586                        __func__, ret);
1587                 goto err;
1588         }
1589
1590         ret = zynqmp_pm_ggs_init(zynqmp_kobj);
1591         if (ret) {
1592                 pr_err("%s() GGS init fail with error %d\n",
1593                        __func__, ret);
1594                 goto err;
1595         }
1596 err:
1597         return ret;
1598 }
1599
1600 static int zynqmp_firmware_probe(struct platform_device *pdev)
1601 {
1602         struct device *dev = &pdev->dev;
1603         struct device_node *np;
1604         int ret;
1605
1606         np = of_find_compatible_node(NULL, NULL, "xlnx,zynqmp");
1607         if (!np) {
1608                 np = of_find_compatible_node(NULL, NULL, "xlnx,versal");
1609                 if (!np)
1610                         return 0;
1611
1612                 feature_check_enabled = true;
1613         }
1614         of_node_put(np);
1615
1616         ret = get_set_conduit_method(dev->of_node);
1617         if (ret)
1618                 return ret;
1619
1620         /* Check PM API version number */
1621         zynqmp_pm_get_api_version(&pm_api_version);
1622         if (pm_api_version < ZYNQMP_PM_VERSION) {
1623                 panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
1624                       __func__,
1625                       ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
1626                       pm_api_version >> 16, pm_api_version & 0xFFFF);
1627         }
1628
1629         pr_info("%s Platform Management API v%d.%d\n", __func__,
1630                 pm_api_version >> 16, pm_api_version & 0xFFFF);
1631
1632         /* Check trustzone version number */
1633         ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
1634         if (ret)
1635                 panic("Legacy trustzone found without version support\n");
1636
1637         if (pm_tz_version < ZYNQMP_TZ_VERSION)
1638                 panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
1639                       __func__,
1640                       ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
1641                       pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1642
1643         pr_info("%s Trustzone version v%d.%d\n", __func__,
1644                 pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1645
1646         /* Assign eemi_ops_table */
1647         eemi_ops_tbl = &eemi_ops;
1648
1649         ret = zynqmp_pm_sysfs_init();
1650         if (ret) {
1651                 pr_err("%s() sysfs init fail with error %d\n", __func__, ret);
1652                 return ret;
1653         }
1654
1655         zynqmp_pm_api_debugfs_init();
1656
1657         ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, firmware_devs,
1658                               ARRAY_SIZE(firmware_devs), NULL, 0, NULL);
1659         if (ret) {
1660                 dev_err(&pdev->dev, "failed to add MFD devices %d\n", ret);
1661                 return ret;
1662         }
1663
1664         return of_platform_populate(dev->of_node, NULL, NULL, dev);
1665 }
1666
1667 static int zynqmp_firmware_remove(struct platform_device *pdev)
1668 {
1669         mfd_remove_devices(&pdev->dev);
1670         zynqmp_pm_api_debugfs_exit();
1671
1672         return 0;
1673 }
1674
1675 static const struct of_device_id zynqmp_firmware_of_match[] = {
1676         {.compatible = "xlnx,zynqmp-firmware"},
1677         {.compatible = "xlnx,versal-firmware-wip"},
1678         {},
1679 };
1680 MODULE_DEVICE_TABLE(of, zynqmp_firmware_of_match);
1681
1682 static struct platform_driver zynqmp_firmware_driver = {
1683         .driver = {
1684                 .name = "zynqmp_firmware",
1685                 .of_match_table = zynqmp_firmware_of_match,
1686         },
1687         .probe = zynqmp_firmware_probe,
1688         .remove = zynqmp_firmware_remove,
1689 };
1690 module_platform_driver(zynqmp_firmware_driver);