Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Gilad Ben-Yossef | 03963ca | 2019-04-18 16:38:53 +0300 | [diff] [blame] | 2 | /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */ |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 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" |
Gilad Ben-Yossef | 6389381 | 2018-01-22 09:27:02 +0000 | [diff] [blame] | 12 | #include "cc_hash.h" |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 13 | #include "cc_pm.h" |
Ofir Drang | 7138377 | 2019-04-18 16:39:10 +0300 | [diff] [blame] | 14 | #include "cc_fips.h" |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 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"); |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 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); |
Ofir Drang | 3499efb | 2019-04-18 16:39:08 +0300 | [diff] [blame] | 35 | cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_ENABLE); |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 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"); |
Ofir Drang | 7766dd7 | 2019-04-18 16:39:06 +0300 | [diff] [blame] | 46 | /* Enables the device source clk */ |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 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 | } |
Ofir Drang | d84f626 | 2019-06-17 11:46:28 +0300 | [diff] [blame] | 52 | /* wait for Crytpcell reset completion */ |
| 53 | if (!cc_wait_for_reset_completion(drvdata)) { |
| 54 | dev_err(dev, "Cryptocell reset not completed"); |
| 55 | return -EBUSY; |
| 56 | } |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 57 | |
Ofir Drang | 7766dd7 | 2019-04-18 16:39:06 +0300 | [diff] [blame] | 58 | cc_iowrite(drvdata, CC_REG(HOST_POWER_DOWN_EN), POWER_DOWN_DISABLE); |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 59 | rc = init_cc_regs(drvdata, false); |
| 60 | if (rc) { |
| 61 | dev_err(dev, "init_cc_regs (%x)\n", rc); |
| 62 | return rc; |
| 63 | } |
Ofir Drang | 7138377 | 2019-04-18 16:39:10 +0300 | [diff] [blame] | 64 | /* check if tee fips error occurred during power down */ |
| 65 | cc_tee_handle_fips_error(drvdata); |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 66 | |
| 67 | rc = cc_resume_req_queue(drvdata); |
| 68 | if (rc) { |
| 69 | dev_err(dev, "cc_resume_req_queue (%x)\n", rc); |
| 70 | return rc; |
| 71 | } |
| 72 | |
Gilad Ben-Yossef | 6389381 | 2018-01-22 09:27:02 +0000 | [diff] [blame] | 73 | /* must be after the queue resuming as it uses the HW queue*/ |
| 74 | cc_init_hash_sram(drvdata); |
| 75 | |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 76 | cc_init_iv_sram(drvdata); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | int cc_pm_get(struct device *dev) |
| 81 | { |
| 82 | int rc = 0; |
| 83 | struct cc_drvdata *drvdata = dev_get_drvdata(dev); |
| 84 | |
| 85 | if (cc_req_queue_suspended(drvdata)) |
| 86 | rc = pm_runtime_get_sync(dev); |
| 87 | else |
| 88 | pm_runtime_get_noresume(dev); |
| 89 | |
| 90 | return rc; |
| 91 | } |
| 92 | |
| 93 | int cc_pm_put_suspend(struct device *dev) |
| 94 | { |
| 95 | int rc = 0; |
| 96 | struct cc_drvdata *drvdata = dev_get_drvdata(dev); |
| 97 | |
| 98 | if (!cc_req_queue_suspended(drvdata)) { |
| 99 | pm_runtime_mark_last_busy(dev); |
| 100 | rc = pm_runtime_put_autosuspend(dev); |
| 101 | } else { |
| 102 | /* Something wrong happens*/ |
| 103 | dev_err(dev, "request to suspend already suspended queue"); |
| 104 | rc = -EBUSY; |
| 105 | } |
| 106 | return rc; |
| 107 | } |
| 108 | |
Ofir Drang | 3db617e | 2019-06-17 11:46:29 +0300 | [diff] [blame] | 109 | bool cc_pm_is_dev_suspended(struct device *dev) |
| 110 | { |
| 111 | /* check device state using runtime api */ |
| 112 | return pm_runtime_suspended(dev); |
| 113 | } |
| 114 | |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 115 | int cc_pm_init(struct cc_drvdata *drvdata) |
| 116 | { |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 117 | struct device *dev = drvdata_to_dev(drvdata); |
| 118 | |
| 119 | /* must be before the enabling to avoid resdundent suspending */ |
| 120 | pm_runtime_set_autosuspend_delay(dev, CC_SUSPEND_TIMEOUT); |
| 121 | pm_runtime_use_autosuspend(dev); |
| 122 | /* activate the PM module */ |
Gilad Ben-Yossef | 1358c13 | 2019-02-07 15:36:11 +0200 | [diff] [blame] | 123 | return pm_runtime_set_active(dev); |
| 124 | } |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 125 | |
Gilad Ben-Yossef | 1358c13 | 2019-02-07 15:36:11 +0200 | [diff] [blame] | 126 | /* enable the PM module*/ |
| 127 | void cc_pm_go(struct cc_drvdata *drvdata) |
| 128 | { |
| 129 | pm_runtime_enable(drvdata_to_dev(drvdata)); |
Gilad Ben-Yossef | 4c3f972 | 2018-01-22 09:27:00 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void cc_pm_fini(struct cc_drvdata *drvdata) |
| 133 | { |
| 134 | pm_runtime_disable(drvdata_to_dev(drvdata)); |
| 135 | } |