]> Git Repo - J-linux.git/blob - drivers/firmware/xilinx/zynqmp.c
PM: sleep: Fix runtime PM based cpuidle support
[J-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-2021 Xilinx, Inc.
6  *
7  *  Michal Simek <[email protected]>
8  *  Davorin Mista <[email protected]>
9  *  Jolly Shah <[email protected]>
10  *  Rajan Vaja <[email protected]>
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 #include <linux/hashtable.h>
24
25 #include <linux/firmware/xlnx-zynqmp.h>
26 #include "zynqmp-debug.h"
27
28 /* Max HashMap Order for PM API feature check (1<<7 = 128) */
29 #define PM_API_FEATURE_CHECK_MAX_ORDER  7
30
31 static bool feature_check_enabled;
32 static DEFINE_HASHTABLE(pm_api_features_map, PM_API_FEATURE_CHECK_MAX_ORDER);
33
34 /**
35  * struct pm_api_feature_data - PM API Feature data
36  * @pm_api_id:          PM API Id, used as key to index into hashmap
37  * @feature_status:     status of PM API feature: valid, invalid
38  * @hentry:             hlist_node that hooks this entry into hashtable
39  */
40 struct pm_api_feature_data {
41         u32 pm_api_id;
42         int feature_status;
43         struct hlist_node hentry;
44 };
45
46 static const struct mfd_cell firmware_devs[] = {
47         {
48                 .name = "zynqmp_power_controller",
49         },
50 };
51
52 /**
53  * zynqmp_pm_ret_code() - Convert PMU-FW error codes to Linux error codes
54  * @ret_status:         PMUFW return code
55  *
56  * Return: corresponding Linux error code
57  */
58 static int zynqmp_pm_ret_code(u32 ret_status)
59 {
60         switch (ret_status) {
61         case XST_PM_SUCCESS:
62         case XST_PM_DOUBLE_REQ:
63                 return 0;
64         case XST_PM_NO_FEATURE:
65                 return -ENOTSUPP;
66         case XST_PM_NO_ACCESS:
67                 return -EACCES;
68         case XST_PM_ABORT_SUSPEND:
69                 return -ECANCELED;
70         case XST_PM_MULT_USER:
71                 return -EUSERS;
72         case XST_PM_INTERNAL:
73         case XST_PM_CONFLICT:
74         case XST_PM_INVALID_NODE:
75         default:
76                 return -EINVAL;
77         }
78 }
79
80 static noinline int do_fw_call_fail(u64 arg0, u64 arg1, u64 arg2,
81                                     u32 *ret_payload)
82 {
83         return -ENODEV;
84 }
85
86 /*
87  * PM function call wrapper
88  * Invoke do_fw_call_smc or do_fw_call_hvc, depending on the configuration
89  */
90 static int (*do_fw_call)(u64, u64, u64, u32 *ret_payload) = do_fw_call_fail;
91
92 /**
93  * do_fw_call_smc() - Call system-level platform management layer (SMC)
94  * @arg0:               Argument 0 to SMC call
95  * @arg1:               Argument 1 to SMC call
96  * @arg2:               Argument 2 to SMC call
97  * @ret_payload:        Returned value array
98  *
99  * Invoke platform management function via SMC call (no hypervisor present).
100  *
101  * Return: Returns status, either success or error+reason
102  */
103 static noinline int do_fw_call_smc(u64 arg0, u64 arg1, u64 arg2,
104                                    u32 *ret_payload)
105 {
106         struct arm_smccc_res res;
107
108         arm_smccc_smc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
109
110         if (ret_payload) {
111                 ret_payload[0] = lower_32_bits(res.a0);
112                 ret_payload[1] = upper_32_bits(res.a0);
113                 ret_payload[2] = lower_32_bits(res.a1);
114                 ret_payload[3] = upper_32_bits(res.a1);
115         }
116
117         return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
118 }
119
120 /**
121  * do_fw_call_hvc() - Call system-level platform management layer (HVC)
122  * @arg0:               Argument 0 to HVC call
123  * @arg1:               Argument 1 to HVC call
124  * @arg2:               Argument 2 to HVC call
125  * @ret_payload:        Returned value array
126  *
127  * Invoke platform management function via HVC
128  * HVC-based for communication through hypervisor
129  * (no direct communication with ATF).
130  *
131  * Return: Returns status, either success or error+reason
132  */
133 static noinline int do_fw_call_hvc(u64 arg0, u64 arg1, u64 arg2,
134                                    u32 *ret_payload)
135 {
136         struct arm_smccc_res res;
137
138         arm_smccc_hvc(arg0, arg1, arg2, 0, 0, 0, 0, 0, &res);
139
140         if (ret_payload) {
141                 ret_payload[0] = lower_32_bits(res.a0);
142                 ret_payload[1] = upper_32_bits(res.a0);
143                 ret_payload[2] = lower_32_bits(res.a1);
144                 ret_payload[3] = upper_32_bits(res.a1);
145         }
146
147         return zynqmp_pm_ret_code((enum pm_ret_status)res.a0);
148 }
149
150 /**
151  * zynqmp_pm_feature() - Check weather given feature is supported or not
152  * @api_id:             API ID to check
153  *
154  * Return: Returns status, either success or error+reason
155  */
156 static int zynqmp_pm_feature(u32 api_id)
157 {
158         int ret;
159         u32 ret_payload[PAYLOAD_ARG_CNT];
160         u64 smc_arg[2];
161         struct pm_api_feature_data *feature_data;
162
163         if (!feature_check_enabled)
164                 return 0;
165
166         /* Check for existing entry in hash table for given api */
167         hash_for_each_possible(pm_api_features_map, feature_data, hentry,
168                                api_id) {
169                 if (feature_data->pm_api_id == api_id)
170                         return feature_data->feature_status;
171         }
172
173         /* Add new entry if not present */
174         feature_data = kmalloc(sizeof(*feature_data), GFP_KERNEL);
175         if (!feature_data)
176                 return -ENOMEM;
177
178         feature_data->pm_api_id = api_id;
179         smc_arg[0] = PM_SIP_SVC | PM_FEATURE_CHECK;
180         smc_arg[1] = api_id;
181
182         ret = do_fw_call(smc_arg[0], smc_arg[1], 0, ret_payload);
183         if (ret)
184                 ret = -EOPNOTSUPP;
185         else
186                 ret = ret_payload[1];
187
188         feature_data->feature_status = ret;
189         hash_add(pm_api_features_map, &feature_data->hentry, api_id);
190
191         return ret;
192 }
193
194 /**
195  * zynqmp_pm_invoke_fn() - Invoke the system-level platform management layer
196  *                         caller function depending on the configuration
197  * @pm_api_id:          Requested PM-API call
198  * @arg0:               Argument 0 to requested PM-API call
199  * @arg1:               Argument 1 to requested PM-API call
200  * @arg2:               Argument 2 to requested PM-API call
201  * @arg3:               Argument 3 to requested PM-API call
202  * @ret_payload:        Returned value array
203  *
204  * Invoke platform management function for SMC or HVC call, depending on
205  * configuration.
206  * Following SMC Calling Convention (SMCCC) for SMC64:
207  * Pm Function Identifier,
208  * PM_SIP_SVC + PM_API_ID =
209  *      ((SMC_TYPE_FAST << FUNCID_TYPE_SHIFT)
210  *      ((SMC_64) << FUNCID_CC_SHIFT)
211  *      ((SIP_START) << FUNCID_OEN_SHIFT)
212  *      ((PM_API_ID) & FUNCID_NUM_MASK))
213  *
214  * PM_SIP_SVC   - Registered ZynqMP SIP Service Call.
215  * PM_API_ID    - Platform Management API ID.
216  *
217  * Return: Returns status, either success or error+reason
218  */
219 int zynqmp_pm_invoke_fn(u32 pm_api_id, u32 arg0, u32 arg1,
220                         u32 arg2, u32 arg3, u32 *ret_payload)
221 {
222         /*
223          * Added SIP service call Function Identifier
224          * Make sure to stay in x0 register
225          */
226         u64 smc_arg[4];
227         int ret;
228
229         /* Check if feature is supported or not */
230         ret = zynqmp_pm_feature(pm_api_id);
231         if (ret < 0)
232                 return ret;
233
234         smc_arg[0] = PM_SIP_SVC | pm_api_id;
235         smc_arg[1] = ((u64)arg1 << 32) | arg0;
236         smc_arg[2] = ((u64)arg3 << 32) | arg2;
237
238         return do_fw_call(smc_arg[0], smc_arg[1], smc_arg[2], ret_payload);
239 }
240
241 static u32 pm_api_version;
242 static u32 pm_tz_version;
243
244 /**
245  * zynqmp_pm_get_api_version() - Get version number of PMU PM firmware
246  * @version:    Returned version value
247  *
248  * Return: Returns status, either success or error+reason
249  */
250 int zynqmp_pm_get_api_version(u32 *version)
251 {
252         u32 ret_payload[PAYLOAD_ARG_CNT];
253         int ret;
254
255         if (!version)
256                 return -EINVAL;
257
258         /* Check is PM API version already verified */
259         if (pm_api_version > 0) {
260                 *version = pm_api_version;
261                 return 0;
262         }
263         ret = zynqmp_pm_invoke_fn(PM_GET_API_VERSION, 0, 0, 0, 0, ret_payload);
264         *version = ret_payload[1];
265
266         return ret;
267 }
268 EXPORT_SYMBOL_GPL(zynqmp_pm_get_api_version);
269
270 /**
271  * zynqmp_pm_get_chipid - Get silicon ID registers
272  * @idcode:     IDCODE register
273  * @version:    version register
274  *
275  * Return:      Returns the status of the operation and the idcode and version
276  *              registers in @idcode and @version.
277  */
278 int zynqmp_pm_get_chipid(u32 *idcode, u32 *version)
279 {
280         u32 ret_payload[PAYLOAD_ARG_CNT];
281         int ret;
282
283         if (!idcode || !version)
284                 return -EINVAL;
285
286         ret = zynqmp_pm_invoke_fn(PM_GET_CHIPID, 0, 0, 0, 0, ret_payload);
287         *idcode = ret_payload[1];
288         *version = ret_payload[2];
289
290         return ret;
291 }
292 EXPORT_SYMBOL_GPL(zynqmp_pm_get_chipid);
293
294 /**
295  * zynqmp_pm_get_trustzone_version() - Get secure trustzone firmware version
296  * @version:    Returned version value
297  *
298  * Return: Returns status, either success or error+reason
299  */
300 static int zynqmp_pm_get_trustzone_version(u32 *version)
301 {
302         u32 ret_payload[PAYLOAD_ARG_CNT];
303         int ret;
304
305         if (!version)
306                 return -EINVAL;
307
308         /* Check is PM trustzone version already verified */
309         if (pm_tz_version > 0) {
310                 *version = pm_tz_version;
311                 return 0;
312         }
313         ret = zynqmp_pm_invoke_fn(PM_GET_TRUSTZONE_VERSION, 0, 0,
314                                   0, 0, ret_payload);
315         *version = ret_payload[1];
316
317         return ret;
318 }
319
320 /**
321  * get_set_conduit_method() - Choose SMC or HVC based communication
322  * @np:         Pointer to the device_node structure
323  *
324  * Use SMC or HVC-based functions to communicate with EL2/EL3.
325  *
326  * Return: Returns 0 on success or error code
327  */
328 static int get_set_conduit_method(struct device_node *np)
329 {
330         const char *method;
331
332         if (of_property_read_string(np, "method", &method)) {
333                 pr_warn("%s missing \"method\" property\n", __func__);
334                 return -ENXIO;
335         }
336
337         if (!strcmp("hvc", method)) {
338                 do_fw_call = do_fw_call_hvc;
339         } else if (!strcmp("smc", method)) {
340                 do_fw_call = do_fw_call_smc;
341         } else {
342                 pr_warn("%s Invalid \"method\" property: %s\n",
343                         __func__, method);
344                 return -EINVAL;
345         }
346
347         return 0;
348 }
349
350 /**
351  * zynqmp_pm_query_data() - Get query data from firmware
352  * @qdata:      Variable to the zynqmp_pm_query_data structure
353  * @out:        Returned output value
354  *
355  * Return: Returns status, either success or error+reason
356  */
357 int zynqmp_pm_query_data(struct zynqmp_pm_query_data qdata, u32 *out)
358 {
359         int ret;
360
361         ret = zynqmp_pm_invoke_fn(PM_QUERY_DATA, qdata.qid, qdata.arg1,
362                                   qdata.arg2, qdata.arg3, out);
363
364         /*
365          * For clock name query, all bytes in SMC response are clock name
366          * characters and return code is always success. For invalid clocks,
367          * clock name bytes would be zeros.
368          */
369         return qdata.qid == PM_QID_CLOCK_GET_NAME ? 0 : ret;
370 }
371 EXPORT_SYMBOL_GPL(zynqmp_pm_query_data);
372
373 /**
374  * zynqmp_pm_clock_enable() - Enable the clock for given id
375  * @clock_id:   ID of the clock to be enabled
376  *
377  * This function is used by master to enable the clock
378  * including peripherals and PLL clocks.
379  *
380  * Return: Returns status, either success or error+reason
381  */
382 int zynqmp_pm_clock_enable(u32 clock_id)
383 {
384         return zynqmp_pm_invoke_fn(PM_CLOCK_ENABLE, clock_id, 0, 0, 0, NULL);
385 }
386 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_enable);
387
388 /**
389  * zynqmp_pm_clock_disable() - Disable the clock for given id
390  * @clock_id:   ID of the clock to be disable
391  *
392  * This function is used by master to disable the clock
393  * including peripherals and PLL clocks.
394  *
395  * Return: Returns status, either success or error+reason
396  */
397 int zynqmp_pm_clock_disable(u32 clock_id)
398 {
399         return zynqmp_pm_invoke_fn(PM_CLOCK_DISABLE, clock_id, 0, 0, 0, NULL);
400 }
401 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_disable);
402
403 /**
404  * zynqmp_pm_clock_getstate() - Get the clock state for given id
405  * @clock_id:   ID of the clock to be queried
406  * @state:      1/0 (Enabled/Disabled)
407  *
408  * This function is used by master to get the state of clock
409  * including peripherals and PLL clocks.
410  *
411  * Return: Returns status, either success or error+reason
412  */
413 int zynqmp_pm_clock_getstate(u32 clock_id, u32 *state)
414 {
415         u32 ret_payload[PAYLOAD_ARG_CNT];
416         int ret;
417
418         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETSTATE, clock_id, 0,
419                                   0, 0, ret_payload);
420         *state = ret_payload[1];
421
422         return ret;
423 }
424 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getstate);
425
426 /**
427  * zynqmp_pm_clock_setdivider() - Set the clock divider for given id
428  * @clock_id:   ID of the clock
429  * @divider:    divider value
430  *
431  * This function is used by master to set divider for any clock
432  * to achieve desired rate.
433  *
434  * Return: Returns status, either success or error+reason
435  */
436 int zynqmp_pm_clock_setdivider(u32 clock_id, u32 divider)
437 {
438         return zynqmp_pm_invoke_fn(PM_CLOCK_SETDIVIDER, clock_id, divider,
439                                    0, 0, NULL);
440 }
441 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setdivider);
442
443 /**
444  * zynqmp_pm_clock_getdivider() - Get the clock divider for given id
445  * @clock_id:   ID of the clock
446  * @divider:    divider value
447  *
448  * This function is used by master to get divider values
449  * for any clock.
450  *
451  * Return: Returns status, either success or error+reason
452  */
453 int zynqmp_pm_clock_getdivider(u32 clock_id, u32 *divider)
454 {
455         u32 ret_payload[PAYLOAD_ARG_CNT];
456         int ret;
457
458         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETDIVIDER, clock_id, 0,
459                                   0, 0, ret_payload);
460         *divider = ret_payload[1];
461
462         return ret;
463 }
464 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getdivider);
465
466 /**
467  * zynqmp_pm_clock_setrate() - Set the clock rate for given id
468  * @clock_id:   ID of the clock
469  * @rate:       rate value in hz
470  *
471  * This function is used by master to set rate for any clock.
472  *
473  * Return: Returns status, either success or error+reason
474  */
475 int zynqmp_pm_clock_setrate(u32 clock_id, u64 rate)
476 {
477         return zynqmp_pm_invoke_fn(PM_CLOCK_SETRATE, clock_id,
478                                    lower_32_bits(rate),
479                                    upper_32_bits(rate),
480                                    0, NULL);
481 }
482 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setrate);
483
484 /**
485  * zynqmp_pm_clock_getrate() - Get the clock rate for given id
486  * @clock_id:   ID of the clock
487  * @rate:       rate value in hz
488  *
489  * This function is used by master to get rate
490  * for any clock.
491  *
492  * Return: Returns status, either success or error+reason
493  */
494 int zynqmp_pm_clock_getrate(u32 clock_id, u64 *rate)
495 {
496         u32 ret_payload[PAYLOAD_ARG_CNT];
497         int ret;
498
499         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETRATE, clock_id, 0,
500                                   0, 0, ret_payload);
501         *rate = ((u64)ret_payload[2] << 32) | ret_payload[1];
502
503         return ret;
504 }
505 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getrate);
506
507 /**
508  * zynqmp_pm_clock_setparent() - Set the clock parent for given id
509  * @clock_id:   ID of the clock
510  * @parent_id:  parent id
511  *
512  * This function is used by master to set parent for any clock.
513  *
514  * Return: Returns status, either success or error+reason
515  */
516 int zynqmp_pm_clock_setparent(u32 clock_id, u32 parent_id)
517 {
518         return zynqmp_pm_invoke_fn(PM_CLOCK_SETPARENT, clock_id,
519                                    parent_id, 0, 0, NULL);
520 }
521 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_setparent);
522
523 /**
524  * zynqmp_pm_clock_getparent() - Get the clock parent for given id
525  * @clock_id:   ID of the clock
526  * @parent_id:  parent id
527  *
528  * This function is used by master to get parent index
529  * for any clock.
530  *
531  * Return: Returns status, either success or error+reason
532  */
533 int zynqmp_pm_clock_getparent(u32 clock_id, u32 *parent_id)
534 {
535         u32 ret_payload[PAYLOAD_ARG_CNT];
536         int ret;
537
538         ret = zynqmp_pm_invoke_fn(PM_CLOCK_GETPARENT, clock_id, 0,
539                                   0, 0, ret_payload);
540         *parent_id = ret_payload[1];
541
542         return ret;
543 }
544 EXPORT_SYMBOL_GPL(zynqmp_pm_clock_getparent);
545
546 /**
547  * zynqmp_pm_set_pll_frac_mode() - PM API for set PLL mode
548  *
549  * @clk_id:     PLL clock ID
550  * @mode:       PLL mode (PLL_MODE_FRAC/PLL_MODE_INT)
551  *
552  * This function sets PLL mode
553  *
554  * Return: Returns status, either success or error+reason
555  */
556 int zynqmp_pm_set_pll_frac_mode(u32 clk_id, u32 mode)
557 {
558         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_SET_PLL_FRAC_MODE,
559                                    clk_id, mode, NULL);
560 }
561 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_mode);
562
563 /**
564  * zynqmp_pm_get_pll_frac_mode() - PM API for get PLL mode
565  *
566  * @clk_id:     PLL clock ID
567  * @mode:       PLL mode
568  *
569  * This function return current PLL mode
570  *
571  * Return: Returns status, either success or error+reason
572  */
573 int zynqmp_pm_get_pll_frac_mode(u32 clk_id, u32 *mode)
574 {
575         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_GET_PLL_FRAC_MODE,
576                                    clk_id, 0, mode);
577 }
578 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_mode);
579
580 /**
581  * zynqmp_pm_set_pll_frac_data() - PM API for setting pll fraction data
582  *
583  * @clk_id:     PLL clock ID
584  * @data:       fraction data
585  *
586  * This function sets fraction data.
587  * It is valid for fraction mode only.
588  *
589  * Return: Returns status, either success or error+reason
590  */
591 int zynqmp_pm_set_pll_frac_data(u32 clk_id, u32 data)
592 {
593         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_SET_PLL_FRAC_DATA,
594                                    clk_id, data, NULL);
595 }
596 EXPORT_SYMBOL_GPL(zynqmp_pm_set_pll_frac_data);
597
598 /**
599  * zynqmp_pm_get_pll_frac_data() - PM API for getting pll fraction data
600  *
601  * @clk_id:     PLL clock ID
602  * @data:       fraction data
603  *
604  * This function returns fraction data value.
605  *
606  * Return: Returns status, either success or error+reason
607  */
608 int zynqmp_pm_get_pll_frac_data(u32 clk_id, u32 *data)
609 {
610         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_GET_PLL_FRAC_DATA,
611                                    clk_id, 0, data);
612 }
613 EXPORT_SYMBOL_GPL(zynqmp_pm_get_pll_frac_data);
614
615 /**
616  * zynqmp_pm_set_sd_tapdelay() -  Set tap delay for the SD device
617  *
618  * @node_id:    Node ID of the device
619  * @type:       Type of tap delay to set (input/output)
620  * @value:      Value to set fot the tap delay
621  *
622  * This function sets input/output tap delay for the SD device.
623  *
624  * Return:      Returns status, either success or error+reason
625  */
626 int zynqmp_pm_set_sd_tapdelay(u32 node_id, u32 type, u32 value)
627 {
628         return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, IOCTL_SET_SD_TAPDELAY,
629                                    type, value, NULL);
630 }
631 EXPORT_SYMBOL_GPL(zynqmp_pm_set_sd_tapdelay);
632
633 /**
634  * zynqmp_pm_sd_dll_reset() - Reset DLL logic
635  *
636  * @node_id:    Node ID of the device
637  * @type:       Reset type
638  *
639  * This function resets DLL logic for the SD device.
640  *
641  * Return:      Returns status, either success or error+reason
642  */
643 int zynqmp_pm_sd_dll_reset(u32 node_id, u32 type)
644 {
645         return zynqmp_pm_invoke_fn(PM_IOCTL, node_id, IOCTL_SD_DLL_RESET,
646                                    type, 0, NULL);
647 }
648 EXPORT_SYMBOL_GPL(zynqmp_pm_sd_dll_reset);
649
650 /**
651  * zynqmp_pm_ospi_mux_select() - OSPI Mux selection
652  *
653  * @dev_id:     Device Id of the OSPI device.
654  * @select:     OSPI Mux select value.
655  *
656  * This function select the OSPI Mux.
657  *
658  * Return:      Returns status, either success or error+reason
659  */
660 int zynqmp_pm_ospi_mux_select(u32 dev_id, u32 select)
661 {
662         return zynqmp_pm_invoke_fn(PM_IOCTL, dev_id, IOCTL_OSPI_MUX_SELECT,
663                                    select, 0, NULL);
664 }
665 EXPORT_SYMBOL_GPL(zynqmp_pm_ospi_mux_select);
666
667 /**
668  * zynqmp_pm_write_ggs() - PM API for writing global general storage (ggs)
669  * @index:      GGS register index
670  * @value:      Register value to be written
671  *
672  * This function writes value to GGS register.
673  *
674  * Return:      Returns status, either success or error+reason
675  */
676 int zynqmp_pm_write_ggs(u32 index, u32 value)
677 {
678         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_WRITE_GGS,
679                                    index, value, NULL);
680 }
681 EXPORT_SYMBOL_GPL(zynqmp_pm_write_ggs);
682
683 /**
684  * zynqmp_pm_read_ggs() - PM API for reading global general storage (ggs)
685  * @index:      GGS register index
686  * @value:      Register value to be written
687  *
688  * This function returns GGS register value.
689  *
690  * Return:      Returns status, either success or error+reason
691  */
692 int zynqmp_pm_read_ggs(u32 index, u32 *value)
693 {
694         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_READ_GGS,
695                                    index, 0, value);
696 }
697 EXPORT_SYMBOL_GPL(zynqmp_pm_read_ggs);
698
699 /**
700  * zynqmp_pm_write_pggs() - PM API for writing persistent global general
701  *                           storage (pggs)
702  * @index:      PGGS register index
703  * @value:      Register value to be written
704  *
705  * This function writes value to PGGS register.
706  *
707  * Return:      Returns status, either success or error+reason
708  */
709 int zynqmp_pm_write_pggs(u32 index, u32 value)
710 {
711         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_WRITE_PGGS, index, value,
712                                    NULL);
713 }
714 EXPORT_SYMBOL_GPL(zynqmp_pm_write_pggs);
715
716 /**
717  * zynqmp_pm_read_pggs() - PM API for reading persistent global general
718  *                           storage (pggs)
719  * @index:      PGGS register index
720  * @value:      Register value to be written
721  *
722  * This function returns PGGS register value.
723  *
724  * Return:      Returns status, either success or error+reason
725  */
726 int zynqmp_pm_read_pggs(u32 index, u32 *value)
727 {
728         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_READ_PGGS, index, 0,
729                                    value);
730 }
731 EXPORT_SYMBOL_GPL(zynqmp_pm_read_pggs);
732
733 /**
734  * zynqmp_pm_set_boot_health_status() - PM API for setting healthy boot status
735  * @value:      Status value to be written
736  *
737  * This function sets healthy bit value to indicate boot health status
738  * to firmware.
739  *
740  * Return:      Returns status, either success or error+reason
741  */
742 int zynqmp_pm_set_boot_health_status(u32 value)
743 {
744         return zynqmp_pm_invoke_fn(PM_IOCTL, 0, IOCTL_SET_BOOT_HEALTH_STATUS,
745                                    value, 0, NULL);
746 }
747
748 /**
749  * zynqmp_pm_reset_assert - Request setting of reset (1 - assert, 0 - release)
750  * @reset:              Reset to be configured
751  * @assert_flag:        Flag stating should reset be asserted (1) or
752  *                      released (0)
753  *
754  * Return: Returns status, either success or error+reason
755  */
756 int zynqmp_pm_reset_assert(const enum zynqmp_pm_reset reset,
757                            const enum zynqmp_pm_reset_action assert_flag)
758 {
759         return zynqmp_pm_invoke_fn(PM_RESET_ASSERT, reset, assert_flag,
760                                    0, 0, NULL);
761 }
762 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_assert);
763
764 /**
765  * zynqmp_pm_reset_get_status - Get status of the reset
766  * @reset:      Reset whose status should be returned
767  * @status:     Returned status
768  *
769  * Return: Returns status, either success or error+reason
770  */
771 int zynqmp_pm_reset_get_status(const enum zynqmp_pm_reset reset, u32 *status)
772 {
773         u32 ret_payload[PAYLOAD_ARG_CNT];
774         int ret;
775
776         if (!status)
777                 return -EINVAL;
778
779         ret = zynqmp_pm_invoke_fn(PM_RESET_GET_STATUS, reset, 0,
780                                   0, 0, ret_payload);
781         *status = ret_payload[1];
782
783         return ret;
784 }
785 EXPORT_SYMBOL_GPL(zynqmp_pm_reset_get_status);
786
787 /**
788  * zynqmp_pm_fpga_load - Perform the fpga load
789  * @address:    Address to write to
790  * @size:       pl bitstream size
791  * @flags:      Bitstream type
792  *      -XILINX_ZYNQMP_PM_FPGA_FULL:  FPGA full reconfiguration
793  *      -XILINX_ZYNQMP_PM_FPGA_PARTIAL: FPGA partial reconfiguration
794  *
795  * This function provides access to pmufw. To transfer
796  * the required bitstream into PL.
797  *
798  * Return: Returns status, either success or error+reason
799  */
800 int zynqmp_pm_fpga_load(const u64 address, const u32 size, const u32 flags)
801 {
802         return zynqmp_pm_invoke_fn(PM_FPGA_LOAD, lower_32_bits(address),
803                                    upper_32_bits(address), size, flags, NULL);
804 }
805 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_load);
806
807 /**
808  * zynqmp_pm_fpga_get_status - Read value from PCAP status register
809  * @value: Value to read
810  *
811  * This function provides access to the pmufw to get the PCAP
812  * status
813  *
814  * Return: Returns status, either success or error+reason
815  */
816 int zynqmp_pm_fpga_get_status(u32 *value)
817 {
818         u32 ret_payload[PAYLOAD_ARG_CNT];
819         int ret;
820
821         if (!value)
822                 return -EINVAL;
823
824         ret = zynqmp_pm_invoke_fn(PM_FPGA_GET_STATUS, 0, 0, 0, 0, ret_payload);
825         *value = ret_payload[1];
826
827         return ret;
828 }
829 EXPORT_SYMBOL_GPL(zynqmp_pm_fpga_get_status);
830
831 /**
832  * zynqmp_pm_pinctrl_request - Request Pin from firmware
833  * @pin: Pin number to request
834  *
835  * This function requests pin from firmware.
836  *
837  * Return: Returns status, either success or error+reason.
838  */
839 int zynqmp_pm_pinctrl_request(const u32 pin)
840 {
841         return zynqmp_pm_invoke_fn(PM_PINCTRL_REQUEST, pin, 0, 0, 0, NULL);
842 }
843 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_request);
844
845 /**
846  * zynqmp_pm_pinctrl_release - Inform firmware that Pin control is released
847  * @pin: Pin number to release
848  *
849  * This function release pin from firmware.
850  *
851  * Return: Returns status, either success or error+reason.
852  */
853 int zynqmp_pm_pinctrl_release(const u32 pin)
854 {
855         return zynqmp_pm_invoke_fn(PM_PINCTRL_RELEASE, pin, 0, 0, 0, NULL);
856 }
857 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_release);
858
859 /**
860  * zynqmp_pm_pinctrl_get_function - Read function id set for the given pin
861  * @pin: Pin number
862  * @id: Buffer to store function ID
863  *
864  * This function provides the function currently set for the given pin.
865  *
866  * Return: Returns status, either success or error+reason
867  */
868 int zynqmp_pm_pinctrl_get_function(const u32 pin, u32 *id)
869 {
870         u32 ret_payload[PAYLOAD_ARG_CNT];
871         int ret;
872
873         if (!id)
874                 return -EINVAL;
875
876         ret = zynqmp_pm_invoke_fn(PM_PINCTRL_GET_FUNCTION, pin, 0,
877                                   0, 0, ret_payload);
878         *id = ret_payload[1];
879
880         return ret;
881 }
882 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_get_function);
883
884 /**
885  * zynqmp_pm_pinctrl_set_function - Set requested function for the pin
886  * @pin: Pin number
887  * @id: Function ID to set
888  *
889  * This function sets requested function for the given pin.
890  *
891  * Return: Returns status, either success or error+reason.
892  */
893 int zynqmp_pm_pinctrl_set_function(const u32 pin, const u32 id)
894 {
895         return zynqmp_pm_invoke_fn(PM_PINCTRL_SET_FUNCTION, pin, id,
896                                    0, 0, NULL);
897 }
898 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_function);
899
900 /**
901  * zynqmp_pm_pinctrl_get_config - Get configuration parameter for the pin
902  * @pin: Pin number
903  * @param: Parameter to get
904  * @value: Buffer to store parameter value
905  *
906  * This function gets requested configuration parameter for the given pin.
907  *
908  * Return: Returns status, either success or error+reason.
909  */
910 int zynqmp_pm_pinctrl_get_config(const u32 pin, const u32 param,
911                                  u32 *value)
912 {
913         u32 ret_payload[PAYLOAD_ARG_CNT];
914         int ret;
915
916         if (!value)
917                 return -EINVAL;
918
919         ret = zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_GET, pin, param,
920                                   0, 0, ret_payload);
921         *value = ret_payload[1];
922
923         return ret;
924 }
925 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_get_config);
926
927 /**
928  * zynqmp_pm_pinctrl_set_config - Set configuration parameter for the pin
929  * @pin: Pin number
930  * @param: Parameter to set
931  * @value: Parameter value to set
932  *
933  * This function sets requested configuration parameter for the given pin.
934  *
935  * Return: Returns status, either success or error+reason.
936  */
937 int zynqmp_pm_pinctrl_set_config(const u32 pin, const u32 param,
938                                  u32 value)
939 {
940         return zynqmp_pm_invoke_fn(PM_PINCTRL_CONFIG_PARAM_SET, pin,
941                                    param, value, 0, NULL);
942 }
943 EXPORT_SYMBOL_GPL(zynqmp_pm_pinctrl_set_config);
944
945 /**
946  * zynqmp_pm_init_finalize() - PM call to inform firmware that the caller
947  *                             master has initialized its own power management
948  *
949  * Return: Returns status, either success or error+reason
950  *
951  * This API function is to be used for notify the power management controller
952  * about the completed power management initialization.
953  */
954 int zynqmp_pm_init_finalize(void)
955 {
956         return zynqmp_pm_invoke_fn(PM_PM_INIT_FINALIZE, 0, 0, 0, 0, NULL);
957 }
958 EXPORT_SYMBOL_GPL(zynqmp_pm_init_finalize);
959
960 /**
961  * zynqmp_pm_set_suspend_mode() - Set system suspend mode
962  * @mode:       Mode to set for system suspend
963  *
964  * This API function is used to set mode of system suspend.
965  *
966  * Return: Returns status, either success or error+reason
967  */
968 int zynqmp_pm_set_suspend_mode(u32 mode)
969 {
970         return zynqmp_pm_invoke_fn(PM_SET_SUSPEND_MODE, mode, 0, 0, 0, NULL);
971 }
972 EXPORT_SYMBOL_GPL(zynqmp_pm_set_suspend_mode);
973
974 /**
975  * zynqmp_pm_request_node() - Request a node with specific capabilities
976  * @node:               Node ID of the slave
977  * @capabilities:       Requested capabilities of the slave
978  * @qos:                Quality of service (not supported)
979  * @ack:                Flag to specify whether acknowledge is requested
980  *
981  * This function is used by master to request particular node from firmware.
982  * Every master must request node before using it.
983  *
984  * Return: Returns status, either success or error+reason
985  */
986 int zynqmp_pm_request_node(const u32 node, const u32 capabilities,
987                            const u32 qos, const enum zynqmp_pm_request_ack ack)
988 {
989         return zynqmp_pm_invoke_fn(PM_REQUEST_NODE, node, capabilities,
990                                    qos, ack, NULL);
991 }
992 EXPORT_SYMBOL_GPL(zynqmp_pm_request_node);
993
994 /**
995  * zynqmp_pm_release_node() - Release a node
996  * @node:       Node ID of the slave
997  *
998  * This function is used by master to inform firmware that master
999  * has released node. Once released, master must not use that node
1000  * without re-request.
1001  *
1002  * Return: Returns status, either success or error+reason
1003  */
1004 int zynqmp_pm_release_node(const u32 node)
1005 {
1006         return zynqmp_pm_invoke_fn(PM_RELEASE_NODE, node, 0, 0, 0, NULL);
1007 }
1008 EXPORT_SYMBOL_GPL(zynqmp_pm_release_node);
1009
1010 /**
1011  * zynqmp_pm_set_requirement() - PM call to set requirement for PM slaves
1012  * @node:               Node ID of the slave
1013  * @capabilities:       Requested capabilities of the slave
1014  * @qos:                Quality of service (not supported)
1015  * @ack:                Flag to specify whether acknowledge is requested
1016  *
1017  * This API function is to be used for slaves a PU already has requested
1018  * to change its capabilities.
1019  *
1020  * Return: Returns status, either success or error+reason
1021  */
1022 int zynqmp_pm_set_requirement(const u32 node, const u32 capabilities,
1023                               const u32 qos,
1024                               const enum zynqmp_pm_request_ack ack)
1025 {
1026         return zynqmp_pm_invoke_fn(PM_SET_REQUIREMENT, node, capabilities,
1027                                    qos, ack, NULL);
1028 }
1029 EXPORT_SYMBOL_GPL(zynqmp_pm_set_requirement);
1030
1031 /**
1032  * zynqmp_pm_load_pdi - Load and process PDI
1033  * @src:       Source device where PDI is located
1034  * @address:   PDI src address
1035  *
1036  * This function provides support to load PDI from linux
1037  *
1038  * Return: Returns status, either success or error+reason
1039  */
1040 int zynqmp_pm_load_pdi(const u32 src, const u64 address)
1041 {
1042         return zynqmp_pm_invoke_fn(PM_LOAD_PDI, src,
1043                                    lower_32_bits(address),
1044                                    upper_32_bits(address), 0, NULL);
1045 }
1046 EXPORT_SYMBOL_GPL(zynqmp_pm_load_pdi);
1047
1048 /**
1049  * zynqmp_pm_aes_engine - Access AES hardware to encrypt/decrypt the data using
1050  * AES-GCM core.
1051  * @address:    Address of the AesParams structure.
1052  * @out:        Returned output value
1053  *
1054  * Return:      Returns status, either success or error code.
1055  */
1056 int zynqmp_pm_aes_engine(const u64 address, u32 *out)
1057 {
1058         u32 ret_payload[PAYLOAD_ARG_CNT];
1059         int ret;
1060
1061         if (!out)
1062                 return -EINVAL;
1063
1064         ret = zynqmp_pm_invoke_fn(PM_SECURE_AES, upper_32_bits(address),
1065                                   lower_32_bits(address),
1066                                   0, 0, ret_payload);
1067         *out = ret_payload[1];
1068
1069         return ret;
1070 }
1071 EXPORT_SYMBOL_GPL(zynqmp_pm_aes_engine);
1072
1073 /**
1074  * zynqmp_pm_system_shutdown - PM call to request a system shutdown or restart
1075  * @type:       Shutdown or restart? 0 for shutdown, 1 for restart
1076  * @subtype:    Specifies which system should be restarted or shut down
1077  *
1078  * Return:      Returns status, either success or error+reason
1079  */
1080 int zynqmp_pm_system_shutdown(const u32 type, const u32 subtype)
1081 {
1082         return zynqmp_pm_invoke_fn(PM_SYSTEM_SHUTDOWN, type, subtype,
1083                                    0, 0, NULL);
1084 }
1085
1086 /**
1087  * struct zynqmp_pm_shutdown_scope - Struct for shutdown scope
1088  * @subtype:    Shutdown subtype
1089  * @name:       Matching string for scope argument
1090  *
1091  * This struct encapsulates mapping between shutdown scope ID and string.
1092  */
1093 struct zynqmp_pm_shutdown_scope {
1094         const enum zynqmp_pm_shutdown_subtype subtype;
1095         const char *name;
1096 };
1097
1098 static struct zynqmp_pm_shutdown_scope shutdown_scopes[] = {
1099         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM] = {
1100                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SUBSYSTEM,
1101                 .name = "subsystem",
1102         },
1103         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY] = {
1104                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_PS_ONLY,
1105                 .name = "ps_only",
1106         },
1107         [ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM] = {
1108                 .subtype = ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM,
1109                 .name = "system",
1110         },
1111 };
1112
1113 static struct zynqmp_pm_shutdown_scope *selected_scope =
1114                 &shutdown_scopes[ZYNQMP_PM_SHUTDOWN_SUBTYPE_SYSTEM];
1115
1116 /**
1117  * zynqmp_pm_is_shutdown_scope_valid - Check if shutdown scope string is valid
1118  * @scope_string:       Shutdown scope string
1119  *
1120  * Return:              Return pointer to matching shutdown scope struct from
1121  *                      array of available options in system if string is valid,
1122  *                      otherwise returns NULL.
1123  */
1124 static struct zynqmp_pm_shutdown_scope*
1125                 zynqmp_pm_is_shutdown_scope_valid(const char *scope_string)
1126 {
1127         int count;
1128
1129         for (count = 0; count < ARRAY_SIZE(shutdown_scopes); count++)
1130                 if (sysfs_streq(scope_string, shutdown_scopes[count].name))
1131                         return &shutdown_scopes[count];
1132
1133         return NULL;
1134 }
1135
1136 static ssize_t shutdown_scope_show(struct device *device,
1137                                    struct device_attribute *attr,
1138                                    char *buf)
1139 {
1140         int i;
1141
1142         for (i = 0; i < ARRAY_SIZE(shutdown_scopes); i++) {
1143                 if (&shutdown_scopes[i] == selected_scope) {
1144                         strcat(buf, "[");
1145                         strcat(buf, shutdown_scopes[i].name);
1146                         strcat(buf, "]");
1147                 } else {
1148                         strcat(buf, shutdown_scopes[i].name);
1149                 }
1150                 strcat(buf, " ");
1151         }
1152         strcat(buf, "\n");
1153
1154         return strlen(buf);
1155 }
1156
1157 static ssize_t shutdown_scope_store(struct device *device,
1158                                     struct device_attribute *attr,
1159                                     const char *buf, size_t count)
1160 {
1161         int ret;
1162         struct zynqmp_pm_shutdown_scope *scope;
1163
1164         scope = zynqmp_pm_is_shutdown_scope_valid(buf);
1165         if (!scope)
1166                 return -EINVAL;
1167
1168         ret = zynqmp_pm_system_shutdown(ZYNQMP_PM_SHUTDOWN_TYPE_SETSCOPE_ONLY,
1169                                         scope->subtype);
1170         if (ret) {
1171                 pr_err("unable to set shutdown scope %s\n", buf);
1172                 return ret;
1173         }
1174
1175         selected_scope = scope;
1176
1177         return count;
1178 }
1179
1180 static DEVICE_ATTR_RW(shutdown_scope);
1181
1182 static ssize_t health_status_store(struct device *device,
1183                                    struct device_attribute *attr,
1184                                    const char *buf, size_t count)
1185 {
1186         int ret;
1187         unsigned int value;
1188
1189         ret = kstrtouint(buf, 10, &value);
1190         if (ret)
1191                 return ret;
1192
1193         ret = zynqmp_pm_set_boot_health_status(value);
1194         if (ret) {
1195                 dev_err(device, "unable to set healthy bit value to %u\n",
1196                         value);
1197                 return ret;
1198         }
1199
1200         return count;
1201 }
1202
1203 static DEVICE_ATTR_WO(health_status);
1204
1205 static ssize_t ggs_show(struct device *device,
1206                         struct device_attribute *attr,
1207                         char *buf,
1208                         u32 reg)
1209 {
1210         int ret;
1211         u32 ret_payload[PAYLOAD_ARG_CNT];
1212
1213         ret = zynqmp_pm_read_ggs(reg, ret_payload);
1214         if (ret)
1215                 return ret;
1216
1217         return sprintf(buf, "0x%x\n", ret_payload[1]);
1218 }
1219
1220 static ssize_t ggs_store(struct device *device,
1221                          struct device_attribute *attr,
1222                          const char *buf, size_t count,
1223                          u32 reg)
1224 {
1225         long value;
1226         int ret;
1227
1228         if (reg >= GSS_NUM_REGS)
1229                 return -EINVAL;
1230
1231         ret = kstrtol(buf, 16, &value);
1232         if (ret) {
1233                 count = -EFAULT;
1234                 goto err;
1235         }
1236
1237         ret = zynqmp_pm_write_ggs(reg, value);
1238         if (ret)
1239                 count = -EFAULT;
1240 err:
1241         return count;
1242 }
1243
1244 /* GGS register show functions */
1245 #define GGS0_SHOW(N)                                            \
1246         ssize_t ggs##N##_show(struct device *device,            \
1247                               struct device_attribute *attr,    \
1248                               char *buf)                        \
1249         {                                                       \
1250                 return ggs_show(device, attr, buf, N);          \
1251         }
1252
1253 static GGS0_SHOW(0);
1254 static GGS0_SHOW(1);
1255 static GGS0_SHOW(2);
1256 static GGS0_SHOW(3);
1257
1258 /* GGS register store function */
1259 #define GGS0_STORE(N)                                           \
1260         ssize_t ggs##N##_store(struct device *device,           \
1261                                struct device_attribute *attr,   \
1262                                const char *buf,                 \
1263                                size_t count)                    \
1264         {                                                       \
1265                 return ggs_store(device, attr, buf, count, N);  \
1266         }
1267
1268 static GGS0_STORE(0);
1269 static GGS0_STORE(1);
1270 static GGS0_STORE(2);
1271 static GGS0_STORE(3);
1272
1273 static ssize_t pggs_show(struct device *device,
1274                          struct device_attribute *attr,
1275                          char *buf,
1276                          u32 reg)
1277 {
1278         int ret;
1279         u32 ret_payload[PAYLOAD_ARG_CNT];
1280
1281         ret = zynqmp_pm_read_pggs(reg, ret_payload);
1282         if (ret)
1283                 return ret;
1284
1285         return sprintf(buf, "0x%x\n", ret_payload[1]);
1286 }
1287
1288 static ssize_t pggs_store(struct device *device,
1289                           struct device_attribute *attr,
1290                           const char *buf, size_t count,
1291                           u32 reg)
1292 {
1293         long value;
1294         int ret;
1295
1296         if (reg >= GSS_NUM_REGS)
1297                 return -EINVAL;
1298
1299         ret = kstrtol(buf, 16, &value);
1300         if (ret) {
1301                 count = -EFAULT;
1302                 goto err;
1303         }
1304
1305         ret = zynqmp_pm_write_pggs(reg, value);
1306         if (ret)
1307                 count = -EFAULT;
1308
1309 err:
1310         return count;
1311 }
1312
1313 #define PGGS0_SHOW(N)                                           \
1314         ssize_t pggs##N##_show(struct device *device,           \
1315                                struct device_attribute *attr,   \
1316                                char *buf)                       \
1317         {                                                       \
1318                 return pggs_show(device, attr, buf, N);         \
1319         }
1320
1321 #define PGGS0_STORE(N)                                          \
1322         ssize_t pggs##N##_store(struct device *device,          \
1323                                 struct device_attribute *attr,  \
1324                                 const char *buf,                \
1325                                 size_t count)                   \
1326         {                                                       \
1327                 return pggs_store(device, attr, buf, count, N); \
1328         }
1329
1330 /* PGGS register show functions */
1331 static PGGS0_SHOW(0);
1332 static PGGS0_SHOW(1);
1333 static PGGS0_SHOW(2);
1334 static PGGS0_SHOW(3);
1335
1336 /* PGGS register store functions */
1337 static PGGS0_STORE(0);
1338 static PGGS0_STORE(1);
1339 static PGGS0_STORE(2);
1340 static PGGS0_STORE(3);
1341
1342 /* GGS register attributes */
1343 static DEVICE_ATTR_RW(ggs0);
1344 static DEVICE_ATTR_RW(ggs1);
1345 static DEVICE_ATTR_RW(ggs2);
1346 static DEVICE_ATTR_RW(ggs3);
1347
1348 /* PGGS register attributes */
1349 static DEVICE_ATTR_RW(pggs0);
1350 static DEVICE_ATTR_RW(pggs1);
1351 static DEVICE_ATTR_RW(pggs2);
1352 static DEVICE_ATTR_RW(pggs3);
1353
1354 static struct attribute *zynqmp_firmware_attrs[] = {
1355         &dev_attr_ggs0.attr,
1356         &dev_attr_ggs1.attr,
1357         &dev_attr_ggs2.attr,
1358         &dev_attr_ggs3.attr,
1359         &dev_attr_pggs0.attr,
1360         &dev_attr_pggs1.attr,
1361         &dev_attr_pggs2.attr,
1362         &dev_attr_pggs3.attr,
1363         &dev_attr_shutdown_scope.attr,
1364         &dev_attr_health_status.attr,
1365         NULL,
1366 };
1367
1368 ATTRIBUTE_GROUPS(zynqmp_firmware);
1369
1370 static int zynqmp_firmware_probe(struct platform_device *pdev)
1371 {
1372         struct device *dev = &pdev->dev;
1373         struct device_node *np;
1374         int ret;
1375
1376         np = of_find_compatible_node(NULL, NULL, "xlnx,zynqmp");
1377         if (!np) {
1378                 np = of_find_compatible_node(NULL, NULL, "xlnx,versal");
1379                 if (!np)
1380                         return 0;
1381
1382                 feature_check_enabled = true;
1383         }
1384         of_node_put(np);
1385
1386         ret = get_set_conduit_method(dev->of_node);
1387         if (ret)
1388                 return ret;
1389
1390         /* Check PM API version number */
1391         zynqmp_pm_get_api_version(&pm_api_version);
1392         if (pm_api_version < ZYNQMP_PM_VERSION) {
1393                 panic("%s Platform Management API version error. Expected: v%d.%d - Found: v%d.%d\n",
1394                       __func__,
1395                       ZYNQMP_PM_VERSION_MAJOR, ZYNQMP_PM_VERSION_MINOR,
1396                       pm_api_version >> 16, pm_api_version & 0xFFFF);
1397         }
1398
1399         pr_info("%s Platform Management API v%d.%d\n", __func__,
1400                 pm_api_version >> 16, pm_api_version & 0xFFFF);
1401
1402         /* Check trustzone version number */
1403         ret = zynqmp_pm_get_trustzone_version(&pm_tz_version);
1404         if (ret)
1405                 panic("Legacy trustzone found without version support\n");
1406
1407         if (pm_tz_version < ZYNQMP_TZ_VERSION)
1408                 panic("%s Trustzone version error. Expected: v%d.%d - Found: v%d.%d\n",
1409                       __func__,
1410                       ZYNQMP_TZ_VERSION_MAJOR, ZYNQMP_TZ_VERSION_MINOR,
1411                       pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1412
1413         pr_info("%s Trustzone version v%d.%d\n", __func__,
1414                 pm_tz_version >> 16, pm_tz_version & 0xFFFF);
1415
1416         ret = mfd_add_devices(&pdev->dev, PLATFORM_DEVID_NONE, firmware_devs,
1417                               ARRAY_SIZE(firmware_devs), NULL, 0, NULL);
1418         if (ret) {
1419                 dev_err(&pdev->dev, "failed to add MFD devices %d\n", ret);
1420                 return ret;
1421         }
1422
1423         zynqmp_pm_api_debugfs_init();
1424
1425         return of_platform_populate(dev->of_node, NULL, NULL, dev);
1426 }
1427
1428 static int zynqmp_firmware_remove(struct platform_device *pdev)
1429 {
1430         struct pm_api_feature_data *feature_data;
1431         struct hlist_node *tmp;
1432         int i;
1433
1434         mfd_remove_devices(&pdev->dev);
1435         zynqmp_pm_api_debugfs_exit();
1436
1437         hash_for_each_safe(pm_api_features_map, i, tmp, feature_data, hentry) {
1438                 hash_del(&feature_data->hentry);
1439                 kfree(feature_data);
1440         }
1441
1442         return 0;
1443 }
1444
1445 static const struct of_device_id zynqmp_firmware_of_match[] = {
1446         {.compatible = "xlnx,zynqmp-firmware"},
1447         {.compatible = "xlnx,versal-firmware"},
1448         {},
1449 };
1450 MODULE_DEVICE_TABLE(of, zynqmp_firmware_of_match);
1451
1452 static struct platform_driver zynqmp_firmware_driver = {
1453         .driver = {
1454                 .name = "zynqmp_firmware",
1455                 .of_match_table = zynqmp_firmware_of_match,
1456                 .dev_groups = zynqmp_firmware_groups,
1457         },
1458         .probe = zynqmp_firmware_probe,
1459         .remove = zynqmp_firmware_remove,
1460 };
1461 module_platform_driver(zynqmp_firmware_driver);
This page took 0.113236 seconds and 4 git commands to generate.