cc_pm.c 2.7 KB

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