xref: /openbmc/linux/drivers/crypto/ccree/cc_pm.c (revision ff148d8a)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2019 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 	int rc;
27 
28 	dev_dbg(dev, "set HOST_POWER_DOWN_EN\n");
29 	rc = cc_suspend_req_queue(drvdata);
30 	if (rc) {
31 		dev_err(dev, "cc_suspend_req_queue (%x)\n", rc);
32 		return rc;
33 	}
34 	fini_cc_regs(drvdata);
35 	cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_ENABLE);
36 	cc_clk_off(drvdata);
37 	return 0;
38 }
39 
40 int cc_pm_resume(struct device *dev)
41 {
42 	int rc;
43 	struct cc_drvdata *drvdata = dev_get_drvdata(dev);
44 
45 	dev_dbg(dev, "unset HOST_POWER_DOWN_EN\n");
46 	/* Enables the device source clk */
47 	rc = cc_clk_on(drvdata);
48 	if (rc) {
49 		dev_err(dev, "failed getting clock back on. We're toast.\n");
50 		return rc;
51 	}
52 
53 	cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_DISABLE);
54 	rc = init_cc_regs(drvdata, false);
55 	if (rc) {
56 		dev_err(dev, "init_cc_regs (%x)\n", rc);
57 		return rc;
58 	}
59 	/* check if tee fips error occurred during power down */
60 	cc_tee_handle_fips_error(drvdata);
61 
62 	rc = cc_resume_req_queue(drvdata);
63 	if (rc) {
64 		dev_err(dev, "cc_resume_req_queue (%x)\n", rc);
65 		return rc;
66 	}
67 
68 	/* must be after the queue resuming as it uses the HW queue*/
69 	cc_init_hash_sram(drvdata);
70 
71 	cc_init_iv_sram(drvdata);
72 	return 0;
73 }
74 
75 int cc_pm_get(struct device *dev)
76 {
77 	int rc = 0;
78 	struct cc_drvdata *drvdata = dev_get_drvdata(dev);
79 
80 	if (cc_req_queue_suspended(drvdata))
81 		rc = pm_runtime_get_sync(dev);
82 	else
83 		pm_runtime_get_noresume(dev);
84 
85 	return rc;
86 }
87 
88 int cc_pm_put_suspend(struct device *dev)
89 {
90 	int rc = 0;
91 	struct cc_drvdata *drvdata = dev_get_drvdata(dev);
92 
93 	if (!cc_req_queue_suspended(drvdata)) {
94 		pm_runtime_mark_last_busy(dev);
95 		rc = pm_runtime_put_autosuspend(dev);
96 	} else {
97 		/* Something wrong happens*/
98 		dev_err(dev, "request to suspend already suspended queue");
99 		rc = -EBUSY;
100 	}
101 	return rc;
102 }
103 
104 int cc_pm_init(struct cc_drvdata *drvdata)
105 {
106 	struct device *dev = drvdata_to_dev(drvdata);
107 
108 	/* must be before the enabling to avoid resdundent suspending */
109 	pm_runtime_set_autosuspend_delay(dev, CC_SUSPEND_TIMEOUT);
110 	pm_runtime_use_autosuspend(dev);
111 	/* activate the PM module */
112 	return pm_runtime_set_active(dev);
113 }
114 
115 /* enable the PM module*/
116 void cc_pm_go(struct cc_drvdata *drvdata)
117 {
118 	pm_runtime_enable(drvdata_to_dev(drvdata));
119 }
120 
121 void cc_pm_fini(struct cc_drvdata *drvdata)
122 {
123 	pm_runtime_disable(drvdata_to_dev(drvdata));
124 }
125