GNU Linux-libre 4.19.264-gnu1
[releases.git] / drivers / crypto / ccree / cc_pm.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2018 ARM Limited or its affiliates. */
3
4 #include <linux/kernel.h>
5 #include <linux/interrupt.h>
6 #include <linux/pm_runtime.h>
7 #include "cc_driver.h"
8 #include "cc_buffer_mgr.h"
9 #include "cc_request_mgr.h"
10 #include "cc_sram_mgr.h"
11 #include "cc_ivgen.h"
12 #include "cc_hash.h"
13 #include "cc_pm.h"
14 #include "cc_fips.h"
15
16 #define POWER_DOWN_ENABLE 0x01
17 #define POWER_DOWN_DISABLE 0x00
18
19 const struct dev_pm_ops ccree_pm = {
20         SET_RUNTIME_PM_OPS(cc_pm_suspend, cc_pm_resume, NULL)
21 };
22
23 int cc_pm_suspend(struct device *dev)
24 {
25         struct cc_drvdata *drvdata = dev_get_drvdata(dev);
26
27         dev_dbg(dev, "set HOST_POWER_DOWN_EN\n");
28         fini_cc_regs(drvdata);
29         cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_ENABLE);
30         cc_clk_off(drvdata);
31         return 0;
32 }
33
34 int cc_pm_resume(struct device *dev)
35 {
36         int rc;
37         struct cc_drvdata *drvdata = dev_get_drvdata(dev);
38
39         dev_dbg(dev, "unset HOST_POWER_DOWN_EN\n");
40         /* Enables the device source clk */
41         rc = cc_clk_on(drvdata);
42         if (rc) {
43                 dev_err(dev, "failed getting clock back on. We're toast.\n");
44                 return rc;
45         }
46
47         cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_DISABLE);
48         rc = init_cc_regs(drvdata, false);
49         if (rc) {
50                 dev_err(dev, "init_cc_regs (%x)\n", rc);
51                 return rc;
52         }
53         /* check if tee fips error occurred during power down */
54         cc_tee_handle_fips_error(drvdata);
55
56         cc_init_hash_sram(drvdata);
57
58         cc_init_iv_sram(drvdata);
59         return 0;
60 }
61
62 int cc_pm_get(struct device *dev)
63 {
64         int rc = 0;
65         struct cc_drvdata *drvdata = dev_get_drvdata(dev);
66
67         if (drvdata->pm_on)
68                 rc = pm_runtime_get_sync(dev);
69
70         return (rc == 1 ? 0 : rc);
71 }
72
73 int cc_pm_put_suspend(struct device *dev)
74 {
75         int rc = 0;
76         struct cc_drvdata *drvdata = dev_get_drvdata(dev);
77
78         if (drvdata->pm_on) {
79                 pm_runtime_mark_last_busy(dev);
80                 rc = pm_runtime_put_autosuspend(dev);
81         }
82
83         return rc;
84 }
85
86 int cc_pm_init(struct cc_drvdata *drvdata)
87 {
88         struct device *dev = drvdata_to_dev(drvdata);
89
90         /* must be before the enabling to avoid resdundent suspending */
91         pm_runtime_set_autosuspend_delay(dev, CC_SUSPEND_TIMEOUT);
92         pm_runtime_use_autosuspend(dev);
93         /* set us as active - note we won't do PM ops until cc_pm_go()! */
94         return pm_runtime_set_active(dev);
95 }
96
97 /* enable the PM module*/
98 void cc_pm_go(struct cc_drvdata *drvdata)
99 {
100         pm_runtime_enable(drvdata_to_dev(drvdata));
101         drvdata->pm_on = true;
102 }
103
104 void cc_pm_fini(struct cc_drvdata *drvdata)
105 {
106         pm_runtime_disable(drvdata_to_dev(drvdata));
107         drvdata->pm_on = false;
108 }