meson-secure-pwrc.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: (GPL-2.0+ OR MIT)
  2. /*
  3. * Copyright (c) 2019 Amlogic, Inc.
  4. * Author: Jianxin Pan <jianxin.pan@amlogic.com>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/io.h>
  8. #include <linux/of_device.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/pm_domain.h>
  11. #include <dt-bindings/power/meson-a1-power.h>
  12. #include <linux/arm-smccc.h>
  13. #include <linux/firmware/meson/meson_sm.h>
  14. #include <linux/module.h>
  15. #define PWRC_ON 1
  16. #define PWRC_OFF 0
  17. struct meson_secure_pwrc_domain {
  18. struct generic_pm_domain base;
  19. unsigned int index;
  20. struct meson_secure_pwrc *pwrc;
  21. };
  22. struct meson_secure_pwrc {
  23. struct meson_secure_pwrc_domain *domains;
  24. struct genpd_onecell_data xlate;
  25. struct meson_sm_firmware *fw;
  26. };
  27. struct meson_secure_pwrc_domain_desc {
  28. unsigned int index;
  29. unsigned int flags;
  30. char *name;
  31. bool (*is_off)(struct meson_secure_pwrc_domain *pwrc_domain);
  32. };
  33. struct meson_secure_pwrc_domain_data {
  34. unsigned int count;
  35. struct meson_secure_pwrc_domain_desc *domains;
  36. };
  37. static bool pwrc_secure_is_off(struct meson_secure_pwrc_domain *pwrc_domain)
  38. {
  39. int is_off = 1;
  40. if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_GET, &is_off,
  41. pwrc_domain->index, 0, 0, 0, 0) < 0)
  42. pr_err("failed to get power domain status\n");
  43. return is_off;
  44. }
  45. static int meson_secure_pwrc_off(struct generic_pm_domain *domain)
  46. {
  47. int ret = 0;
  48. struct meson_secure_pwrc_domain *pwrc_domain =
  49. container_of(domain, struct meson_secure_pwrc_domain, base);
  50. if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
  51. pwrc_domain->index, PWRC_OFF, 0, 0, 0) < 0) {
  52. pr_err("failed to set power domain off\n");
  53. ret = -EINVAL;
  54. }
  55. return ret;
  56. }
  57. static int meson_secure_pwrc_on(struct generic_pm_domain *domain)
  58. {
  59. int ret = 0;
  60. struct meson_secure_pwrc_domain *pwrc_domain =
  61. container_of(domain, struct meson_secure_pwrc_domain, base);
  62. if (meson_sm_call(pwrc_domain->pwrc->fw, SM_A1_PWRC_SET, NULL,
  63. pwrc_domain->index, PWRC_ON, 0, 0, 0) < 0) {
  64. pr_err("failed to set power domain on\n");
  65. ret = -EINVAL;
  66. }
  67. return ret;
  68. }
  69. #define SEC_PD(__name, __flag) \
  70. [PWRC_##__name##_ID] = \
  71. { \
  72. .name = #__name, \
  73. .index = PWRC_##__name##_ID, \
  74. .is_off = pwrc_secure_is_off, \
  75. .flags = __flag, \
  76. }
  77. static struct meson_secure_pwrc_domain_desc a1_pwrc_domains[] = {
  78. SEC_PD(DSPA, 0),
  79. SEC_PD(DSPB, 0),
  80. /* UART should keep working in ATF after suspend and before resume */
  81. SEC_PD(UART, GENPD_FLAG_ALWAYS_ON),
  82. /* DMC is for DDR PHY ana/dig and DMC, and should be always on */
  83. SEC_PD(DMC, GENPD_FLAG_ALWAYS_ON),
  84. SEC_PD(I2C, 0),
  85. SEC_PD(PSRAM, 0),
  86. SEC_PD(ACODEC, 0),
  87. SEC_PD(AUDIO, 0),
  88. SEC_PD(OTP, 0),
  89. SEC_PD(DMA, 0),
  90. SEC_PD(SD_EMMC, 0),
  91. SEC_PD(RAMA, 0),
  92. /* SRAMB is used as ATF runtime memory, and should be always on */
  93. SEC_PD(RAMB, GENPD_FLAG_ALWAYS_ON),
  94. SEC_PD(IR, 0),
  95. SEC_PD(SPICC, 0),
  96. SEC_PD(SPIFC, 0),
  97. SEC_PD(USB, 0),
  98. /* NIC is for the Arm NIC-400 interconnect, and should be always on */
  99. SEC_PD(NIC, GENPD_FLAG_ALWAYS_ON),
  100. SEC_PD(PDMIN, 0),
  101. SEC_PD(RSA, 0),
  102. };
  103. static int meson_secure_pwrc_probe(struct platform_device *pdev)
  104. {
  105. int i;
  106. struct device_node *sm_np;
  107. struct meson_secure_pwrc *pwrc;
  108. const struct meson_secure_pwrc_domain_data *match;
  109. match = of_device_get_match_data(&pdev->dev);
  110. if (!match) {
  111. dev_err(&pdev->dev, "failed to get match data\n");
  112. return -ENODEV;
  113. }
  114. sm_np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gxbb-sm");
  115. if (!sm_np) {
  116. dev_err(&pdev->dev, "no secure-monitor node\n");
  117. return -ENODEV;
  118. }
  119. pwrc = devm_kzalloc(&pdev->dev, sizeof(*pwrc), GFP_KERNEL);
  120. if (!pwrc)
  121. return -ENOMEM;
  122. pwrc->fw = meson_sm_get(sm_np);
  123. of_node_put(sm_np);
  124. if (!pwrc->fw)
  125. return -EPROBE_DEFER;
  126. pwrc->xlate.domains = devm_kcalloc(&pdev->dev, match->count,
  127. sizeof(*pwrc->xlate.domains),
  128. GFP_KERNEL);
  129. if (!pwrc->xlate.domains)
  130. return -ENOMEM;
  131. pwrc->domains = devm_kcalloc(&pdev->dev, match->count,
  132. sizeof(*pwrc->domains), GFP_KERNEL);
  133. if (!pwrc->domains)
  134. return -ENOMEM;
  135. pwrc->xlate.num_domains = match->count;
  136. platform_set_drvdata(pdev, pwrc);
  137. for (i = 0 ; i < match->count ; ++i) {
  138. struct meson_secure_pwrc_domain *dom = &pwrc->domains[i];
  139. if (!match->domains[i].index)
  140. continue;
  141. dom->pwrc = pwrc;
  142. dom->index = match->domains[i].index;
  143. dom->base.name = match->domains[i].name;
  144. dom->base.flags = match->domains[i].flags;
  145. dom->base.power_on = meson_secure_pwrc_on;
  146. dom->base.power_off = meson_secure_pwrc_off;
  147. pm_genpd_init(&dom->base, NULL, match->domains[i].is_off(dom));
  148. pwrc->xlate.domains[i] = &dom->base;
  149. }
  150. return of_genpd_add_provider_onecell(pdev->dev.of_node, &pwrc->xlate);
  151. }
  152. static struct meson_secure_pwrc_domain_data meson_secure_a1_pwrc_data = {
  153. .domains = a1_pwrc_domains,
  154. .count = ARRAY_SIZE(a1_pwrc_domains),
  155. };
  156. static const struct of_device_id meson_secure_pwrc_match_table[] = {
  157. {
  158. .compatible = "amlogic,meson-a1-pwrc",
  159. .data = &meson_secure_a1_pwrc_data,
  160. },
  161. { /* sentinel */ }
  162. };
  163. MODULE_DEVICE_TABLE(of, meson_secure_pwrc_match_table);
  164. static struct platform_driver meson_secure_pwrc_driver = {
  165. .probe = meson_secure_pwrc_probe,
  166. .driver = {
  167. .name = "meson_secure_pwrc",
  168. .of_match_table = meson_secure_pwrc_match_table,
  169. },
  170. };
  171. module_platform_driver(meson_secure_pwrc_driver);
  172. MODULE_LICENSE("Dual MIT/GPL");